1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This is an implementation of dynamic virtual indentation. It works
28 ;; by adding text properties to a buffer to make sure lines are
29 ;; indented according to outline structure.
31 ;; The process is synchronous, toggled at every buffer modification.
32 ;; Though, the initialization (indentation of text already in the
33 ;; buffer), which can take a few seconds in large buffers, happens on
45 (declare-function org-inlinetask-get-task-level
"org-inlinetask" ())
46 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
47 (declare-function org-list-item-body-column
"org-list" (item))
48 (defvar org-inlinetask-show-first-star
)
50 (defgroup org-indent nil
51 "Options concerning dynamic virtual outline indentation."
55 (defconst org-indent-max
40
56 "Maximum indentation in characters.")
57 (defconst org-indent-max-levels
20
58 "Maximum added level through virtual indentation, in characters.
60 It is computed by multiplying `org-indent-indentation-per-level'
61 minus one by actual level of the headline minus one.")
63 (defvar org-indent-strings nil
64 "Vector with all indentation strings.
65 It will be set in `org-indent-initialize'.")
66 (defvar org-indent-stars nil
67 "Vector with all indentation star strings.
68 It will be set in `org-indent-initialize'.")
69 (defvar org-indent-inlinetask-first-star
(org-add-props "*" '(face org-warning
))
70 "First star of inline tasks, with correct face.")
71 (defvar org-indent-agent-timer nil
72 "Timer running the initialize agent.")
73 (defvar org-indent-agentized-buffers nil
74 "List of buffers watched by the initialize agent.")
75 (defvar org-indent-agent-resume-timer nil
76 "Timer to reschedule agent after switching to other idle processes.")
77 (defvar org-indent-agent-active-delay
'(0 2 0)
78 "Time to run agent before switching to other idle processes.
79 Delay used when the buffer to initialize is current.")
80 (defvar org-indent-agent-passive-delay
'(0 0 400000)
81 "Time to run agent before switching to other idle processes.
82 Delay used when the buffer to initialize isn't current.")
83 (defvar org-indent-agent-resume-delay
'(0 0 100000)
84 "Minimal time for other idle processes before switching back to agent.")
85 (defvar org-indent-initial-marker nil
86 "Position of initialization before interrupt.
87 This is used locally in each buffer being initialized.")
88 (defvar org-hide-leading-stars-before-indent-mode nil
90 (defvar org-indent-modified-headline-flag nil
91 "Non-nil means the last deletion operated on an headline.
92 It is modified by `org-indent-notify-modified-headline'.")
95 (defcustom org-indent-boundary-char ?\
; comment to protect space char
96 "The end of the virtual indentation strings, a single-character string.
97 The default is just a space, but if you wish, you can use \"|\" or so.
98 This can be useful on a terminal window - under a windowing system,
99 it may be prettier to customize the org-indent face."
101 :set
(lambda (var val
)
103 (and org-indent-strings
(org-indent-initialize)))
106 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
107 "Non-nil means setting the variable `org-indent-mode' will \
108 turn off indentation adaptation.
109 For details see the variable `org-adapt-indentation'."
113 (defcustom org-indent-mode-turns-on-hiding-stars t
114 "Non-nil means setting the variable `org-indent-mode' will \
115 turn on `org-hide-leading-stars'."
119 (defcustom org-indent-indentation-per-level
2
120 "Indentation per level in number of characters."
125 (org-compatible-face nil nil
)
126 "Face for outline indentation.
127 The default is to make it look like whitespace. But you may find it
128 useful to make it ever so slightly different."
131 (defun org-indent-initialize ()
132 "Initialize the indentation strings."
133 (setq org-indent-strings
(make-vector (1+ org-indent-max
) nil
))
134 (setq org-indent-stars
(make-vector (1+ org-indent-max
) nil
))
135 (aset org-indent-strings
0 nil
)
136 (aset org-indent-stars
0 nil
)
137 (loop for i from
1 to org-indent-max do
138 (aset org-indent-strings i
140 (concat (make-string (1- i
) ?\
)
141 (char-to-string org-indent-boundary-char
))
142 nil
'face
'org-indent
)))
143 (loop for i from
1 to org-indent-max-levels do
144 (aset org-indent-stars i
145 (org-add-props (make-string i ?
*)
146 nil
'face
'org-hide
))))
148 (defsubst org-indent-remove-properties
(beg end
)
149 "Remove indentations between BEG and END."
150 (with-silent-modifications
151 (remove-text-properties beg end
'(line-prefix nil wrap-prefix nil
))))
154 (define-minor-mode org-indent-mode
155 "When active, indent text according to outline structure.
157 Internally this works by adding `line-prefix' and `wrap-prefix'
158 properties, after each buffer modification, on the modified zone.
160 The process is synchronous. Though, initial indentation of
161 buffer, which can take a few seconds on large buffers, is done
162 during idle time." nil
" Ind" nil
164 ((org-bound-and-true-p org-inhibit-startup
)
165 (setq org-indent-mode nil
))
166 ((and org-indent-mode
(featurep 'xemacs
))
167 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
168 (setq org-indent-mode nil
))
169 ((and org-indent-mode
170 (not (org-version-check "23.1.50" "Org Indent mode" :predicate
)))
171 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
174 (setq org-indent-mode nil
))
176 ;; mode was turned on.
177 (org-set-local 'indent-tabs-mode nil
)
178 (or org-indent-strings
(org-indent-initialize))
179 (org-set-local 'org-indent-initial-marker
(copy-marker 1))
180 (when org-indent-mode-turns-off-org-adapt-indentation
181 (org-set-local 'org-adapt-indentation nil
))
182 (when org-indent-mode-turns-on-hiding-stars
183 (org-set-local 'org-hide-leading-stars-before-indent-mode
184 org-hide-leading-stars
)
185 (org-set-local 'org-hide-leading-stars t
))
186 (make-local-variable 'buffer-substring-filters
)
187 (add-to-list 'buffer-substring-filters
188 'org-indent-remove-properties-from-string
)
189 (org-add-hook 'after-change-functions
'org-indent-refresh-maybe nil
'local
)
190 (org-add-hook 'before-change-functions
191 'org-indent-notify-modified-headline nil
'local
)
192 (and font-lock-mode
(org-restart-font-lock))
193 (org-indent-remove-properties (point-min) (point-max))
194 ;; Submit current buffer to initialize agent. If it's the first
195 ;; buffer submitted, also start the agent. Current buffer is
196 ;; pushed in both cases to avoid a race condition.
197 (if org-indent-agentized-buffers
198 (push (current-buffer) org-indent-agentized-buffers
)
199 (push (current-buffer) org-indent-agentized-buffers
)
200 (setq org-indent-agent-timer
201 (run-with-idle-timer 0.2 t
#'org-indent-initialize-agent
))))
203 ;; mode was turned off (or we refused to turn it on)
204 (kill-local-variable 'org-adapt-indentation
)
205 (setq org-indent-agentized-buffers
206 (delq (current-buffer) org-indent-agentized-buffers
))
207 (when (markerp org-indent-initial-marker
)
208 (set-marker org-indent-initial-marker nil
))
209 (when (boundp 'org-hide-leading-stars-before-indent-mode
)
210 (org-set-local 'org-hide-leading-stars
211 org-hide-leading-stars-before-indent-mode
))
212 (setq buffer-substring-filters
213 (delq 'org-indent-remove-properties-from-string
214 buffer-substring-filters
))
215 (remove-hook 'after-change-functions
'org-indent-refresh-maybe
'local
)
216 (remove-hook 'before-change-functions
217 'org-indent-notify-modified-headline
'local
)
218 (org-with-wide-buffer
219 (org-indent-remove-properties (point-min) (point-max)))
220 (and font-lock-mode
(org-restart-font-lock))
223 (defun org-indent-indent-buffer ()
224 "Add indentation properties to the accessible part of the buffer."
226 (if (not (eq major-mode
'org-mode
))
227 (error "Not in Org mode")
228 (message "Setting buffer indentation. It may take a few seconds...")
229 (org-indent-remove-properties (point-min) (point-max))
230 (org-indent-add-properties (point-min) (point-max))
231 (message "Indentation of buffer set.")))
233 (defun org-indent-remove-properties-from-string (string)
234 "Remove indentation properties from STRING."
235 (remove-text-properties 0 (length string
)
236 '(line-prefix nil wrap-prefix nil
) string
)
239 (defun org-indent-initialize-agent ()
240 "Start or resume current buffer initialization.
241 Only buffers in `org-indent-agentized-buffers' trigger an action.
242 When no more buffer is being watched, the agent suppress itself."
243 (when org-indent-agent-resume-timer
244 (cancel-timer org-indent-agent-resume-timer
))
245 (setq org-indent-agentized-buffers
246 (org-remove-if-not #'buffer-live-p org-indent-agentized-buffers
))
248 ;; Job done: kill agent.
249 ((not org-indent-agentized-buffers
) (cancel-timer org-indent-agent-timer
))
250 ;; Current buffer is agentized: start/resume initialization
251 ;; somewhat aggressively.
252 ((memq (current-buffer) org-indent-agentized-buffers
)
253 (org-indent-initialize-buffer (current-buffer)
254 org-indent-agent-active-delay
))
255 ;; Else, start/resume initialization of the last agentized buffer,
257 (t (org-indent-initialize-buffer (car org-indent-agentized-buffers
)
258 org-indent-agent-passive-delay
))))
260 (defun org-indent-initialize-buffer (buffer delay
)
261 "Set virtual indentation for the buffer BUFFER, asynchronously.
262 Give hand to other idle processes if it takes longer than DELAY,
264 (with-current-buffer buffer
265 (when org-indent-mode
266 (org-with-wide-buffer
268 ;; Always nil unless interrupted.
270 (and org-indent-initial-marker
271 (marker-position org-indent-initial-marker
)
272 (org-indent-add-properties org-indent-initial-marker
276 (move-marker org-indent-initial-marker interruptp
)
277 ;; Job is complete: un-agentize buffer.
279 (setq org-indent-agentized-buffers
280 (delq buffer org-indent-agentized-buffers
))))))))
282 (defsubst org-indent-set-line-properties
(l w h
)
283 "Set prefix properties on current line an move to next one.
285 Prefix properties `line-prefix' and `wrap-prefix' in current line
286 are set to, respectively, length L and W.
288 If H is non-nil, `line-prefix' will be starred. If H is
289 `inline', the first star will have `org-warning' face.
291 Assume point is at beginning of line."
294 (let ((stars (aref org-indent-stars
295 (min l org-indent-max-levels
))))
297 (if (org-bound-and-true-p org-inlinetask-show-first-star
)
298 (concat org-indent-inlinetask-first-star
301 (h (aref org-indent-stars
302 (min l org-indent-max-levels
)))
303 (t (aref org-indent-strings
304 (min l org-indent-max
)))))
305 (wrap (aref org-indent-strings
(min w org-indent-max
))))
306 ;; Add properties down to the next line to indent empty lines.
307 (add-text-properties (point) (min (1+ (point-at-eol)) (point-max))
308 `(line-prefix ,line wrap-prefix
,wrap
)))
311 (defun org-indent-add-properties (beg end
&optional delay
)
312 "Add indentation properties between BEG and END.
314 When DELAY is non-nil, it must be a time value. In that case,
315 the process is asynchronous and can be interrupted, either by
316 user request, or after DELAY. This is done by throwing the
317 `interrupt' tag along with the buffer position where the process
320 (org-with-wide-buffer
323 ;; 1. Initialize prefix at BEG. This is done by storing two
324 ;; variables: INLINE-PF and PF, representing respectively
325 ;; length of current `line-prefix' when line is inside an
326 ;; inline task or not.
327 (let* ((case-fold-search t
)
328 (limited-re (org-get-limited-outline-regexp))
329 (added-ind-per-lvl (1- org-indent-indentation-per-level
))
331 (and (ignore-errors (let ((outline-regexp limited-re
))
332 (org-back-to-heading t
)))
333 (+ (* org-indent-indentation-per-level
334 (- (match-end 0) (match-beginning 0) 2)) 2))))
335 (pf-inline (and (featurep 'org-inlinetask
)
336 (org-inlinetask-in-task-p)
337 (+ (* org-indent-indentation-per-level
338 (1- (org-inlinetask-get-task-level))) 2)))
339 (time-limit (and delay
(time-add (current-time) delay
))))
340 ;; 2. For each line, set `line-prefix' and `wrap-prefix'
341 ;; properties depending on the type of line (headline,
342 ;; inline task, item or other).
343 (with-silent-modifications
344 (while (and (<= (point) end
) (not (eobp)))
346 ;; When in asynchronous mode, check if interrupt is
348 ((and delay
(input-pending-p)) (throw 'interrupt
(point)))
349 ;; In asynchronous mode, take a break of
350 ;; `org-indent-agent-resume-delay' every DELAY to avoid
351 ;; blocking any other idle timer or process output.
352 ((and delay
(time-less-p time-limit
(current-time)))
353 (setq org-indent-agent-resume-timer
355 (time-add (current-idle-time)
356 org-indent-agent-resume-delay
)
357 nil
#'org-indent-initialize-agent
))
358 (throw 'interrupt
(point)))
359 ;; Headline or inline task.
360 ((looking-at org-outline-regexp
)
361 (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
362 (line (* added-ind-per-lvl
(1- nstars
)))
363 (wrap (+ line
(1+ nstars
))))
365 ;; Headline: new value for PF.
366 ((looking-at limited-re
)
367 (org-indent-set-line-properties line wrap t
)
369 ;; End of inline task: PF-INLINE is now nil.
370 ((looking-at "\\*+ end[ \t]*$")
371 (org-indent-set-line-properties line wrap
'inline
)
372 (setq pf-inline nil
))
373 ;; Start of inline task. Determine if it contains
374 ;; text, or if it is only one line long. Set
375 ;; PF-INLINE accordingly.
376 (t (org-indent-set-line-properties line wrap
'inline
)
377 (setq pf-inline
(and (org-inlinetask-in-task-p) wrap
))))))
378 ;; List item: `wrap-prefix' is set where body starts.
380 (let* ((line (or pf-inline pf
0))
381 (wrap (+ (org-list-item-body-column (point)) line
)))
382 (org-indent-set-line-properties line wrap nil
)))
383 ;; Normal line: use PF-INLINE, PF or nil as prefixes.
384 (t (let* ((line (or pf-inline pf
0))
385 (wrap (+ line
(org-get-indentation))))
386 (org-indent-set-line-properties line wrap nil
))))))))))
388 (defun org-indent-notify-modified-headline (beg end
)
389 "Set `org-indent-modified-headline-flag' depending on context.
391 BEG and END are the positions of the beginning and end of the
392 range of deleted text.
394 This function is meant to be called by `before-change-functions'.
395 Flag will be non-nil if command is going to modify or delete an
397 (when org-indent-mode
398 (setq org-indent-modified-headline-flag
402 (or (and (org-at-heading-p) (< beg
(match-end 0)))
403 (re-search-forward org-outline-regexp-bol end t
)))))))
405 (defun org-indent-refresh-maybe (beg end dummy
)
406 "Refresh indentation properties in an adequate portion of buffer.
407 BEG and END are the positions of the beginning and end of the
408 range of inserted text. DUMMY is an unused argument.
410 This function is meant to be called by `after-change-functions'."
411 (when org-indent-mode
413 ;; If an headline was modified or inserted, set properties until
415 (if (or org-indent-modified-headline-flag
419 (re-search-forward org-outline-regexp-bol end t
)))
420 (let ((end (save-excursion
422 (org-with-limited-levels (outline-next-heading))
424 (setq org-indent-modified-headline-flag nil
)
425 (org-indent-add-properties beg end
))
426 ;; Otherwise, only set properties on modified area.
427 (org-indent-add-properties beg end
)))))
429 (provide 'org-indent
)
431 ;;; org-indent.el ends here