utils/server.mk: use --transform, not alias --xform
[org-mode.git] / lisp / org-indent.el
blobd006df86747600d0a27fea6a420a111e25943b61
1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
3 ;;
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
7 ;;
8 ;; This file is part of GNU Emacs.
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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
34 ;; idle time.
36 ;;; Code:
38 (require 'org-macs)
39 (require 'org-compat)
40 (require 'org)
42 (eval-when-compile
43 (require 'cl))
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."
52 :tag "Org Indent"
53 :group 'org)
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
89 "Used locally.")
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."
100 :group 'org-indent
101 :set (lambda (var val)
102 (set var val)
103 (and org-indent-strings (org-indent-initialize)))
104 :type 'character)
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'."
110 :group 'org-indent
111 :type 'boolean)
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'."
116 :group 'org-indent
117 :type 'boolean)
119 (defcustom org-indent-indentation-per-level 2
120 "Indentation per level in number of characters."
121 :group 'org-indent
122 :type 'integer)
124 (defface org-indent
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."
129 :group 'org-faces)
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
139 (org-add-props
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))))
153 ;;;###autoload
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
163 (cond
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!")
172 (ding)
173 (sit-for 1)
174 (setq org-indent-mode nil))
175 (org-indent-mode
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 'filter-buffer-substring-functions)
187 (add-hook 'filter-buffer-substring-functions
188 (lambda (fun start end delete)
189 (org-indent-remove-properties-from-string
190 (funcall fun start end delete))))
191 (org-add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
192 (org-add-hook 'before-change-functions
193 'org-indent-notify-modified-headline nil 'local)
194 (and font-lock-mode (org-restart-font-lock))
195 (org-indent-remove-properties (point-min) (point-max))
196 ;; Submit current buffer to initialize agent. If it's the first
197 ;; buffer submitted, also start the agent. Current buffer is
198 ;; pushed in both cases to avoid a race condition.
199 (if org-indent-agentized-buffers
200 (push (current-buffer) org-indent-agentized-buffers)
201 (push (current-buffer) org-indent-agentized-buffers)
202 (setq org-indent-agent-timer
203 (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
205 ;; mode was turned off (or we refused to turn it on)
206 (kill-local-variable 'org-adapt-indentation)
207 (setq org-indent-agentized-buffers
208 (delq (current-buffer) org-indent-agentized-buffers))
209 (when (markerp org-indent-initial-marker)
210 (set-marker org-indent-initial-marker nil))
211 (when (boundp 'org-hide-leading-stars-before-indent-mode)
212 (org-set-local 'org-hide-leading-stars
213 org-hide-leading-stars-before-indent-mode))
214 (remove-hook 'filter-buffer-substring-functions
215 (lambda (fun start end delete)
216 (org-indent-remove-properties-from-string
217 (funcall fun start end delete))))
218 (remove-hook 'after-change-functions 'org-indent-refresh-maybe 'local)
219 (remove-hook 'before-change-functions
220 'org-indent-notify-modified-headline 'local)
221 (org-with-wide-buffer
222 (org-indent-remove-properties (point-min) (point-max)))
223 (and font-lock-mode (org-restart-font-lock))
224 (redraw-display))))
226 (defun org-indent-indent-buffer ()
227 "Add indentation properties to the accessible part of the buffer."
228 (interactive)
229 (if (not (derived-mode-p 'org-mode))
230 (error "Not in Org mode")
231 (message "Setting buffer indentation. It may take a few seconds...")
232 (org-indent-remove-properties (point-min) (point-max))
233 (org-indent-add-properties (point-min) (point-max))
234 (message "Indentation of buffer set.")))
236 (defun org-indent-remove-properties-from-string (string)
237 "Remove indentation properties from STRING."
238 (remove-text-properties 0 (length string)
239 '(line-prefix nil wrap-prefix nil) string)
240 string)
242 (defun org-indent-initialize-agent ()
243 "Start or resume current buffer initialization.
244 Only buffers in `org-indent-agentized-buffers' trigger an action.
245 When no more buffer is being watched, the agent suppress itself."
246 (when org-indent-agent-resume-timer
247 (cancel-timer org-indent-agent-resume-timer))
248 (setq org-indent-agentized-buffers
249 (org-remove-if-not #'buffer-live-p org-indent-agentized-buffers))
250 (cond
251 ;; Job done: kill agent.
252 ((not org-indent-agentized-buffers) (cancel-timer org-indent-agent-timer))
253 ;; Current buffer is agentized: start/resume initialization
254 ;; somewhat aggressively.
255 ((memq (current-buffer) org-indent-agentized-buffers)
256 (org-indent-initialize-buffer (current-buffer)
257 org-indent-agent-active-delay))
258 ;; Else, start/resume initialization of the last agentized buffer,
259 ;; softly.
260 (t (org-indent-initialize-buffer (car org-indent-agentized-buffers)
261 org-indent-agent-passive-delay))))
263 (defun org-indent-initialize-buffer (buffer delay)
264 "Set virtual indentation for the buffer BUFFER, asynchronously.
265 Give hand to other idle processes if it takes longer than DELAY,
266 a time value."
267 (with-current-buffer buffer
268 (when org-indent-mode
269 (org-with-wide-buffer
270 (let ((interruptp
271 ;; Always nil unless interrupted.
272 (catch 'interrupt
273 (and org-indent-initial-marker
274 (marker-position org-indent-initial-marker)
275 (org-indent-add-properties org-indent-initial-marker
276 (point-max)
277 delay)
278 nil))))
279 (move-marker org-indent-initial-marker interruptp)
280 ;; Job is complete: un-agentize buffer.
281 (unless interruptp
282 (setq org-indent-agentized-buffers
283 (delq buffer org-indent-agentized-buffers))))))))
285 (defsubst org-indent-set-line-properties (l w h)
286 "Set prefix properties on current line an move to next one.
288 Prefix properties `line-prefix' and `wrap-prefix' in current line
289 are set to, respectively, length L and W.
291 If H is non-nil, `line-prefix' will be starred. If H is
292 `inline', the first star will have `org-warning' face.
294 Assume point is at beginning of line."
295 (let ((line (cond
296 ((eq 'inline h)
297 (let ((stars (aref org-indent-stars
298 (min l org-indent-max-levels))))
299 (and stars
300 (if (org-bound-and-true-p org-inlinetask-show-first-star)
301 (concat org-indent-inlinetask-first-star
302 (substring stars 1))
303 stars))))
304 (h (aref org-indent-stars
305 (min l org-indent-max-levels)))
306 (t (aref org-indent-strings
307 (min l org-indent-max)))))
308 (wrap (aref org-indent-strings (min w org-indent-max))))
309 ;; Add properties down to the next line to indent empty lines.
310 (add-text-properties (point) (min (1+ (point-at-eol)) (point-max))
311 `(line-prefix ,line wrap-prefix ,wrap)))
312 (forward-line 1))
314 (defun org-indent-add-properties (beg end &optional delay)
315 "Add indentation properties between BEG and END.
317 When DELAY is non-nil, it must be a time value. In that case,
318 the process is asynchronous and can be interrupted, either by
319 user request, or after DELAY. This is done by throwing the
320 `interrupt' tag along with the buffer position where the process
321 stopped."
322 (save-match-data
323 (org-with-wide-buffer
324 (goto-char beg)
325 (beginning-of-line)
326 ;; 1. Initialize prefix at BEG. This is done by storing two
327 ;; variables: INLINE-PF and PF, representing respectively
328 ;; length of current `line-prefix' when line is inside an
329 ;; inline task or not.
330 (let* ((case-fold-search t)
331 (limited-re (org-get-limited-outline-regexp))
332 (added-ind-per-lvl (1- org-indent-indentation-per-level))
333 (pf (save-excursion
334 (and (ignore-errors (let ((outline-regexp limited-re))
335 (org-back-to-heading t)))
336 (+ (* org-indent-indentation-per-level
337 (- (match-end 0) (match-beginning 0) 2)) 2))))
338 (pf-inline (and (featurep 'org-inlinetask)
339 (org-inlinetask-in-task-p)
340 (+ (* org-indent-indentation-per-level
341 (1- (org-inlinetask-get-task-level))) 2)))
342 (time-limit (and delay (time-add (current-time) delay))))
343 ;; 2. For each line, set `line-prefix' and `wrap-prefix'
344 ;; properties depending on the type of line (headline,
345 ;; inline task, item or other).
346 (with-silent-modifications
347 (while (and (<= (point) end) (not (eobp)))
348 (cond
349 ;; When in asynchronous mode, check if interrupt is
350 ;; required.
351 ((and delay (input-pending-p)) (throw 'interrupt (point)))
352 ;; In asynchronous mode, take a break of
353 ;; `org-indent-agent-resume-delay' every DELAY to avoid
354 ;; blocking any other idle timer or process output.
355 ((and delay (time-less-p time-limit (current-time)))
356 (setq org-indent-agent-resume-timer
357 (run-with-idle-timer
358 (time-add (current-idle-time)
359 org-indent-agent-resume-delay)
360 nil #'org-indent-initialize-agent))
361 (throw 'interrupt (point)))
362 ;; Headline or inline task.
363 ((looking-at org-outline-regexp)
364 (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
365 (line (* added-ind-per-lvl (1- nstars)))
366 (wrap (+ line (1+ nstars))))
367 (cond
368 ;; Headline: new value for PF.
369 ((looking-at limited-re)
370 (org-indent-set-line-properties line wrap t)
371 (setq pf wrap))
372 ;; End of inline task: PF-INLINE is now nil.
373 ((looking-at "\\*+ end[ \t]*$")
374 (org-indent-set-line-properties line wrap 'inline)
375 (setq pf-inline nil))
376 ;; Start of inline task. Determine if it contains
377 ;; text, or if it is only one line long. Set
378 ;; PF-INLINE accordingly.
379 (t (org-indent-set-line-properties line wrap 'inline)
380 (setq pf-inline (and (org-inlinetask-in-task-p) wrap))))))
381 ;; List item: `wrap-prefix' is set where body starts.
382 ((org-at-item-p)
383 (let* ((line (or pf-inline pf 0))
384 (wrap (+ (org-list-item-body-column (point)) line)))
385 (org-indent-set-line-properties line wrap nil)))
386 ;; Normal line: use PF-INLINE, PF or nil as prefixes.
387 (t (let* ((line (or pf-inline pf 0))
388 (wrap (+ line (org-get-indentation))))
389 (org-indent-set-line-properties line wrap nil))))))))))
391 (defun org-indent-notify-modified-headline (beg end)
392 "Set `org-indent-modified-headline-flag' depending on context.
394 BEG and END are the positions of the beginning and end of the
395 range of deleted text.
397 This function is meant to be called by `before-change-functions'.
398 Flag will be non-nil if command is going to modify or delete an
399 headline."
400 (when org-indent-mode
401 (setq org-indent-modified-headline-flag
402 (save-excursion
403 (goto-char beg)
404 (save-match-data
405 (or (and (org-at-heading-p) (< beg (match-end 0)))
406 (re-search-forward org-outline-regexp-bol end t)))))))
408 (defun org-indent-refresh-maybe (beg end dummy)
409 "Refresh indentation properties in an adequate portion of buffer.
410 BEG and END are the positions of the beginning and end of the
411 range of inserted text. DUMMY is an unused argument.
413 This function is meant to be called by `after-change-functions'."
414 (when org-indent-mode
415 (save-match-data
416 ;; If an headline was modified or inserted, set properties until
417 ;; next headline.
418 (if (or org-indent-modified-headline-flag
419 (save-excursion
420 (goto-char beg)
421 (beginning-of-line)
422 (re-search-forward org-outline-regexp-bol end t)))
423 (let ((end (save-excursion
424 (goto-char end)
425 (org-with-limited-levels (outline-next-heading))
426 (point))))
427 (setq org-indent-modified-headline-flag nil)
428 (org-indent-add-properties beg end))
429 ;; Otherwise, only set properties on modified area.
430 (org-indent-add-properties beg end)))))
432 (provide 'org-indent)
434 ;;; org-indent.el ends here