1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This is an implementation of dynamic virtual indentation. It works
29 ;; by adding text properties to a buffer to make sure lines are
30 ;; indented according to outline structure.
41 (defvar org-inlinetask-min-level
)
42 (declare-function org-inlinetask-get-task-level
"org-inlinetask" ())
43 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
45 (defgroup org-indent nil
46 "Options concerning dynamic virtual outline indentation."
50 (defconst org-indent-max
40
51 "Maximum indentation in characters.")
52 (defconst org-indent-max-levels
40
53 "Maximum indentation in characters.")
55 (defvar org-indent-strings nil
56 "Vector with all indentation strings.
57 It will be set in `org-indent-initialize'.")
58 (defvar org-indent-stars nil
59 "Vector with all indentation star strings.
60 It will be set in `org-indent-initialize'.")
61 (defvar org-hide-leading-stars-before-indent-mode nil
64 (defcustom org-indent-boundary-char ?\
; comment to protect space char
65 "The end of the virtual indentation strings, a single-character string.
66 The default is just a space, but if you wish, you can use \"|\" or so.
67 This can be useful on a terminal window - under a windowing system,
68 it may be prettier to customize the org-indent face."
70 :set
(lambda (var val
)
72 (and org-indent-strings
(org-indent-initialize)))
75 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
76 "Non-nil means setting the variable `org-indent-mode' will \
77 turn off indentation adaptation.
78 For details see the variable `org-adapt-indentation'."
82 (defcustom org-indent-mode-turns-on-hiding-stars t
83 "Non-nil means setting the variable `org-indent-mode' will \
84 turn on `org-hide-leading-stars'."
88 (defcustom org-indent-indentation-per-level
2
89 "Indentation per level in number of characters."
93 (defcustom org-indent-fix-section-after-idle-time
0.2
94 "Seconds of idle time before fixing virtual indentation of section.
95 The hooking-in of virtual indentation is not yet perfect. Occasionally,
96 a change does not trigger to proper change of indentation. For this we
97 have a timer action that fixes indentation in the current section after
98 a short amount idle time. If we ever get the integration to work perfectly,
99 this variable can be set to nil to get rid of the timer."
102 (const "Do not install idle timer" nil
)
103 (number :tag
"Idle time")))
105 (defun org-indent-initialize ()
106 "Initialize the indentation strings and set the idle timer."
107 ;; We use an idle timer to "repair" the current section, because the
108 ;; redisplay seems to have some problems.
109 (unless org-indent-strings
110 (when org-indent-fix-section-after-idle-time
112 org-indent-fix-section-after-idle-time
113 t
'org-indent-refresh-section
)))
114 ;; Initialize the indentation and star vectors
115 (setq org-indent-strings
(make-vector (1+ org-indent-max
) nil
))
116 (setq org-indent-stars
(make-vector (1+ org-indent-max
) nil
))
117 (aset org-indent-strings
0 nil
)
118 (aset org-indent-stars
0 nil
)
119 (loop for i from
1 to org-indent-max do
120 (aset org-indent-strings i
122 (concat (make-string (1- i
) ?\
)
123 (char-to-string org-indent-boundary-char
))
124 nil
'face
'org-indent
)))
125 (loop for i from
1 to org-indent-max-levels do
126 (aset org-indent-stars i
127 (org-add-props (make-string i ?
*)
128 nil
'face
'org-hide
))))
131 (define-minor-mode org-indent-mode
132 "When active, indent text according to outline structure.
134 Internally this works by adding `line-prefix' properties to all non-headlines.
135 These properties are updated locally in idle time.
136 FIXME: How to update when broken?"
139 ((org-bound-and-true-p org-inhibit-startup
)
140 (setq org-indent-mode nil
))
141 ((and org-indent-mode
(featurep 'xemacs
))
142 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
143 (setq org-indent-mode nil
))
144 ((and org-indent-mode
145 (not (org-version-check "23.1.50" "Org Indent mode" :predicate
)))
146 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
149 (setq org-indent-mode nil
))
151 ;; mode was turned on.
152 (org-set-local 'indent-tabs-mode nil
)
153 (or org-indent-strings
(org-indent-initialize))
154 (when org-indent-mode-turns-off-org-adapt-indentation
155 (org-set-local 'org-adapt-indentation nil
))
156 (when org-indent-mode-turns-on-hiding-stars
157 (org-set-local 'org-hide-leading-stars-before-indent-mode
158 org-hide-leading-stars
)
159 (org-set-local 'org-hide-leading-stars t
))
160 (make-local-variable 'buffer-substring-filters
)
161 (add-to-list 'buffer-substring-filters
162 'org-indent-remove-properties-from-string
)
163 (org-add-hook 'org-after-demote-entry-hook
164 'org-indent-refresh-section nil
'local
)
165 (org-add-hook 'org-after-promote-entry-hook
166 'org-indent-refresh-section nil
'local
)
167 (org-add-hook 'org-font-lock-hook
168 'org-indent-refresh-to nil
'local
)
169 (and font-lock-mode
(org-restart-font-lock))
172 ;; mode was turned off (or we refused to turn it on)
175 (org-indent-remove-properties (point-min) (point-max))
176 (kill-local-variable 'org-adapt-indentation
)
177 (when (boundp 'org-hide-leading-stars-before-indent-mode
)
178 (org-set-local 'org-hide-leading-stars
179 org-hide-leading-stars-before-indent-mode
))
180 (setq buffer-substring-filters
181 (delq 'org-indent-remove-properties-from-string
182 buffer-substring-filters
))
183 (remove-hook 'org-after-promote-entry-hook
184 'org-indent-refresh-section
'local
)
185 (remove-hook 'org-after-demote-entry-hook
186 'org-indent-refresh-section
'local
)
187 (and font-lock-mode
(org-restart-font-lock))
188 (redraw-display))))))
192 (org-compatible-face nil nil
)
193 "Face for outline indentation.
194 The default is to make it look like whitespace. But you may find it
195 useful to make it ever so slightly different."
198 (defun org-indent-indent-buffer ()
199 "Add indentation properties for the whole buffer."
201 (when org-indent-mode
205 (org-indent-remove-properties (point-min) (point-max))
206 (org-indent-add-properties (point-min) (point-max))))))
208 (defun org-indent-remove-properties (beg end
)
209 "Remove indentations between BEG and END."
210 (let ((inhibit-modification-hooks t
))
211 (with-silent-modifications
212 (remove-text-properties beg end
'(line-prefix nil wrap-prefix nil
)))))
214 (defun org-indent-remove-properties-from-string (string)
215 "Remove indentations between BEG and END."
216 (remove-text-properties 0 (length string
)
217 '(line-prefix nil wrap-prefix nil
) string
)
220 (defvar org-indent-outline-re
(concat "^" org-outline-regexp
)
221 "Outline heading regexp.")
223 (defun org-indent-add-properties (beg end
)
224 "Add indentation properties between BEG and END.
225 Assumes that BEG is at the beginning of a line."
226 (let* ((inhibit-modification-hooks t
)
227 (inlinetaskp (featurep 'org-inlinetask
))
228 (get-real-level (lambda (pos lvl
)
231 (if (and inlinetaskp
(org-inlinetask-in-task-p))
232 (org-inlinetask-get-task-level)
239 (with-silent-modifications
244 (if (not (re-search-forward org-indent-outline-re nil t
))
245 (setq e
(point-max) exit t
)
246 (setq e
(match-beginning 0))
247 (if (>= e end
) (setq exit t
))
248 (unless (and inlinetaskp
(org-inlinetask-in-task-p))
249 (setq level
(- (match-end 0) (match-beginning 0) 1)))
250 (setq nstars
(* (1- (funcall get-real-level e level
))
251 (1- org-indent-indentation-per-level
)))
253 (point-at-bol) (point-at-eol)
255 (aref org-indent-stars nstars
)
257 (aref org-indent-strings
258 (* (funcall get-real-level e level
)
259 org-indent-indentation-per-level
)))))
262 b e
(list 'line-prefix
(aref org-indent-strings n
)
263 'wrap-prefix
(aref org-indent-strings n
))))
264 (setq b
(1+ (point-at-eol))
265 n
(* (funcall get-real-level b level
)
266 org-indent-indentation-per-level
)))))))
268 (defvar org-inlinetask-min-level
)
269 (defun org-indent-refresh-section ()
270 "Refresh indentation properties in the current outline section.
271 Point is assumed to be at the beginning of a headline."
273 (when org-indent-mode
276 (when (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
277 (if (featurep 'org-inlinetask
)
278 (1- org-inlinetask-min-level
)
280 (org-back-to-heading)))
282 (setq end
(or (save-excursion (or (outline-next-heading) (point)))))
283 (org-indent-remove-properties beg end
)
284 (org-indent-add-properties beg end
))))))
286 (defun org-indent-refresh-to (limit)
287 "Refresh indentation properties in the current outline section.
288 Point is assumed to be at the beginning of a headline."
290 (when org-indent-mode
291 (let ((beg (point)) (end limit
))
293 (and (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
294 (if (featurep 'org-inlinetask
)
295 (1- org-inlinetask-min-level
)
297 (org-back-to-heading)))
299 (org-indent-remove-properties beg end
)
300 (org-indent-add-properties beg end
)))
303 (defun org-indent-refresh-subtree ()
304 "Refresh indentation properties in the current outline subtree.
305 Point is assumed to be at the beginning of a headline."
307 (when org-indent-mode
311 (setq end
(save-excursion (org-end-of-subtree t t
)))
312 (org-indent-remove-properties beg end
)
313 (org-indent-add-properties beg end
)))))
315 (defun org-indent-refresh-buffer ()
316 "Refresh indentation properties in the current outline subtree.
317 Point is assumed to be at the beginning of a headline."
319 (when org-indent-mode
321 (org-indent-mode 1)))
323 (provide 'org-indent
)
325 ;; arch-tag: b76736bc-9f4a-43cd-977c-ecfd6689846a
326 ;;; org-indent.el ends here