customizable processing of Org-mode text used as comments in tangled source-code...
[org-mode.git] / lisp / org-indent.el
blob0f8c0253e4ec12dbd637f7fdc331e4fd057238b0
1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009-2011 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))
49 (defgroup org-indent nil
50 "Options concerning dynamic virtual outline indentation."
51 :tag "Org Indent"
52 :group 'org)
54 (defconst org-indent-max 40
55 "Maximum indentation in characters.")
56 (defconst org-indent-max-levels 20
57 "Maximum added level through virtual indentation, in characters.
59 It is computed by multiplying `org-indent-indentation-per-level'
60 minus one by actual level of the headline minus one.")
62 (defvar org-indent-strings nil
63 "Vector with all indentation strings.
64 It will be set in `org-indent-initialize'.")
65 (defvar org-indent-stars nil
66 "Vector with all indentation star strings.
67 It will be set in `org-indent-initialize'.")
68 (defvar org-indent-inlinetask-first-star (org-add-props "*" '(face org-warning))
69 "First star of inline tasks, with correct face.")
70 (defvar org-indent-agent-timer nil
71 "Timer running the initialize agent.")
72 (defvar org-indent-agentized-buffers nil
73 "List of buffers watched by the initialize agent.")
74 (defvar org-indent-agent-resume-timer nil
75 "Timer to reschedule agent after switching to other idle processes.")
76 (defvar org-indent-agent-active-delay '(0 2 0)
77 "Time to run agent before switching to other idle processes.
78 Delay used when the buffer to initialize is current.")
79 (defvar org-indent-agent-passive-delay '(0 0 400000)
80 "Time to run agent before switching to other idle processes.
81 Delay used when the buffer to initialize isn't current.")
82 (defvar org-indent-agent-resume-delay '(0 0 100000)
83 "Minimal time for other idle processes before switching back to agent.")
84 (defvar org-indent-initial-marker nil
85 "Position of initialization before interrupt.
86 This is used locally in each buffer being initialized.")
87 (defvar org-hide-leading-stars-before-indent-mode nil
88 "Used locally.")
89 (defvar org-indent-modified-headline-flag nil
90 "Non-nil means the last deletion operated on an headline.
91 It is modified by `org-indent-notify-modified-headline'.")
94 (defcustom org-indent-boundary-char ?\ ; comment to protect space char
95 "The end of the virtual indentation strings, a single-character string.
96 The default is just a space, but if you wish, you can use \"|\" or so.
97 This can be useful on a terminal window - under a windowing system,
98 it may be prettier to customize the org-indent face."
99 :group 'org-indent
100 :set (lambda (var val)
101 (set var val)
102 (and org-indent-strings (org-indent-initialize)))
103 :type 'character)
105 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
106 "Non-nil means setting the variable `org-indent-mode' will \
107 turn off indentation adaptation.
108 For details see the variable `org-adapt-indentation'."
109 :group 'org-indent
110 :type 'boolean)
112 (defcustom org-indent-mode-turns-on-hiding-stars t
113 "Non-nil means setting the variable `org-indent-mode' will \
114 turn on `org-hide-leading-stars'."
115 :group 'org-indent
116 :type 'boolean)
118 (defcustom org-indent-indentation-per-level 2
119 "Indentation per level in number of characters."
120 :group 'org-indent
121 :type 'integer)
123 (defface org-indent
124 (org-compatible-face nil nil)
125 "Face for outline indentation.
126 The default is to make it look like whitespace. But you may find it
127 useful to make it ever so slightly different."
128 :group 'org-faces)
130 (defun org-indent-initialize ()
131 "Initialize the indentation strings."
132 (setq org-indent-strings (make-vector (1+ org-indent-max) nil))
133 (setq org-indent-stars (make-vector (1+ org-indent-max) nil))
134 (aset org-indent-strings 0 nil)
135 (aset org-indent-stars 0 nil)
136 (loop for i from 1 to org-indent-max do
137 (aset org-indent-strings i
138 (org-add-props
139 (concat (make-string (1- i) ?\ )
140 (char-to-string org-indent-boundary-char))
141 nil 'face 'org-indent)))
142 (loop for i from 1 to org-indent-max-levels do
143 (aset org-indent-stars i
144 (org-add-props (make-string i ?*)
145 nil 'face 'org-hide))))
147 (defsubst org-indent-remove-properties (beg end)
148 "Remove indentations between BEG and END."
149 (with-silent-modifications
150 (remove-text-properties beg end '(line-prefix nil wrap-prefix nil))))
152 ;;;###autoload
153 (define-minor-mode org-indent-mode
154 "When active, indent text according to outline structure.
156 Internally this works by adding `line-prefix' and `wrap-prefix'
157 properties, after each buffer modification, on the modified zone.
159 The process is synchronous. Though, initial indentation of
160 buffer, which can take a few seconds on large buffers, is done
161 during idle time." nil " Ind" nil
162 (cond
163 ((org-bound-and-true-p org-inhibit-startup)
164 (setq org-indent-mode nil))
165 ((and org-indent-mode (featurep 'xemacs))
166 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
167 (setq org-indent-mode nil))
168 ((and org-indent-mode
169 (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
170 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
171 (ding)
172 (sit-for 1)
173 (setq org-indent-mode nil))
174 (org-indent-mode
175 ;; mode was turned on.
176 (org-set-local 'indent-tabs-mode nil)
177 (or org-indent-strings (org-indent-initialize))
178 (org-set-local 'org-indent-initial-marker (copy-marker 1))
179 (when org-indent-mode-turns-off-org-adapt-indentation
180 (org-set-local 'org-adapt-indentation nil))
181 (when org-indent-mode-turns-on-hiding-stars
182 (org-set-local 'org-hide-leading-stars-before-indent-mode
183 org-hide-leading-stars)
184 (org-set-local 'org-hide-leading-stars t))
185 (make-local-variable 'buffer-substring-filters)
186 (add-to-list 'buffer-substring-filters
187 'org-indent-remove-properties-from-string)
188 (org-add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
189 (org-add-hook 'before-change-functions
190 'org-indent-notify-modified-headline nil 'local)
191 (and font-lock-mode (org-restart-font-lock))
192 (org-indent-remove-properties (point-min) (point-max))
193 ;; Submit current buffer to initialize agent. If it's the first
194 ;; buffer submitted, also start the agent. Current buffer is
195 ;; pushed in both cases to avoid a race condition.
196 (if org-indent-agentized-buffers
197 (push (current-buffer) org-indent-agentized-buffers)
198 (push (current-buffer) org-indent-agentized-buffers)
199 (setq org-indent-agent-timer
200 (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
202 ;; mode was turned off (or we refused to turn it on)
203 (kill-local-variable 'org-adapt-indentation)
204 (setq org-indent-agentized-buffers
205 (delq (current-buffer) org-indent-agentized-buffers))
206 (when (markerp org-indent-initial-marker)
207 (set-marker org-indent-initial-marker nil))
208 (when (boundp 'org-hide-leading-stars-before-indent-mode)
209 (org-set-local 'org-hide-leading-stars
210 org-hide-leading-stars-before-indent-mode))
211 (setq buffer-substring-filters
212 (delq 'org-indent-remove-properties-from-string
213 buffer-substring-filters))
214 (remove-hook 'after-change-functions 'org-indent-refresh-maybe 'local)
215 (remove-hook 'before-change-functions
216 'org-indent-notify-modified-headline 'local)
217 (org-with-wide-buffer
218 (org-indent-remove-properties (point-min) (point-max)))
219 (and font-lock-mode (org-restart-font-lock))
220 (redraw-display))))
222 (defun org-indent-indent-buffer ()
223 "Add indentation properties to the accessible part of the buffer."
224 (interactive)
225 (if (not (org-mode-p))
226 (error "Not in Org mode")
227 (message "Setting buffer indentation. It may take a few seconds...")
228 (org-indent-remove-properties (point-min) (point-max))
229 (org-indent-add-properties (point-min) (point-max))
230 (message "Indentation of buffer set.")))
232 (defun org-indent-remove-properties-from-string (string)
233 "Remove indentation properties from STRING."
234 (remove-text-properties 0 (length string)
235 '(line-prefix nil wrap-prefix nil) string)
236 string)
238 (defun org-indent-initialize-agent ()
239 "Start or resume current buffer initialization.
240 Only buffers in `org-indent-agentized-buffers' trigger an action.
241 When no more buffer is being watched, the agent suppress itself."
242 (when org-indent-agent-resume-timer
243 (cancel-timer org-indent-agent-resume-timer))
244 (setq org-indent-agentized-buffers
245 (org-remove-if-not #'buffer-live-p org-indent-agentized-buffers))
246 (cond
247 ;; Job done: kill agent.
248 ((not org-indent-agentized-buffers) (cancel-timer org-indent-agent-timer))
249 ;; Current buffer is agentized: start/resume initialization
250 ;; somewhat aggressively.
251 ((memq (current-buffer) org-indent-agentized-buffers)
252 (org-indent-initialize-buffer (current-buffer)
253 org-indent-agent-active-delay))
254 ;; Else, start/resume initialization of the last agentized buffer,
255 ;; softly.
256 (t (org-indent-initialize-buffer (car org-indent-agentized-buffers)
257 org-indent-agent-passive-delay))))
259 (defun org-indent-initialize-buffer (buffer delay)
260 "Set virtual indentation for the buffer BUFFER, asynchronously.
261 Give hand to other idle processes if it takes longer than DELAY,
262 a time value."
263 (with-current-buffer buffer
264 (when org-indent-mode
265 (org-with-wide-buffer
266 (let ((interruptp
267 ;; Always nil unless interrupted.
268 (catch 'interrupt
269 (and org-indent-initial-marker
270 (marker-position org-indent-initial-marker)
271 (org-indent-add-properties org-indent-initial-marker
272 (point-max)
273 delay)
274 nil))))
275 (move-marker org-indent-initial-marker interruptp)
276 ;; Job is complete: un-agentize buffer.
277 (unless interruptp
278 (setq org-indent-agentized-buffers
279 (delq buffer org-indent-agentized-buffers))))))))
281 (defsubst org-indent-set-line-properties (l w h)
282 "Set prefix properties on current line an move to next one.
284 Prefix properties `line-prefix' and `wrap-prefix' in current line
285 are set to, respectively, length L and W.
287 If H is non-nil, `line-prefix' will be starred. If H is
288 `inline', the first star will have `org-warning' face.
290 Assume point is at beginning of line."
291 (let ((line (cond
292 ((eq 'inline h)
293 (let ((stars (aref org-indent-stars
294 (min l org-indent-max-levels))))
295 (and stars
296 (concat org-indent-inlinetask-first-star
297 (substring stars 1)))))
298 (h (aref org-indent-stars
299 (min l org-indent-max-levels)))
300 (t (aref org-indent-strings
301 (min l org-indent-max)))))
302 (wrap (aref org-indent-strings (min w org-indent-max))))
303 ;; Add properties down to the next line to indent empty lines.
304 (add-text-properties (point) (min (1+ (point-at-eol)) (point-max))
305 `(line-prefix ,line wrap-prefix ,wrap)))
306 (forward-line 1))
308 (defun org-indent-add-properties (beg end &optional delay)
309 "Add indentation properties between BEG and END.
311 When DELAY is non-nil, it must be a time value. In that case,
312 the process is asynchronous and can be interrupted, either by
313 user request, or after DELAY. This is done by throwing the
314 `interrupt' tag along with the buffer position where the process
315 stopped."
316 (save-match-data
317 (org-with-wide-buffer
318 (goto-char beg)
319 (beginning-of-line)
320 ;; 1. Initialize prefix at BEG. This is done by storing two
321 ;; variables: INLINE-PF and PF, representing respectively
322 ;; length of current `line-prefix' when line is inside an
323 ;; inline task or not.
324 (let* ((case-fold-search t)
325 (limited-re (org-get-limited-outline-regexp))
326 (added-ind-per-lvl (1- org-indent-indentation-per-level))
327 (pf (save-excursion
328 (and (ignore-errors (let ((outline-regexp limited-re))
329 (org-back-to-heading t)))
330 (+ (* org-indent-indentation-per-level
331 (- (match-end 0) (match-beginning 0) 2)) 2))))
332 (pf-inline (and (featurep 'org-inlinetask)
333 (org-inlinetask-in-task-p)
334 (+ (* org-indent-indentation-per-level
335 (1- (org-inlinetask-get-task-level))) 2)))
336 (time-limit (and delay (time-add (current-time) delay))))
337 ;; 2. For each line, set `line-prefix' and `wrap-prefix'
338 ;; properties depending on the type of line (headline,
339 ;; inline task, item or other).
340 (with-silent-modifications
341 (while (and (<= (point) end) (not (eobp)))
342 (cond
343 ;; When in asynchronous mode, check if interrupt is
344 ;; required.
345 ((and delay (input-pending-p)) (throw 'interrupt (point)))
346 ;; In asynchronous mode, take a break of
347 ;; `org-indent-agent-resume-delay' every DELAY to avoid
348 ;; blocking any other idle timer or process output.
349 ((and delay (time-less-p time-limit (current-time)))
350 (setq org-indent-agent-resume-timer
351 (run-with-idle-timer
352 (time-add (current-idle-time)
353 org-indent-agent-resume-delay)
354 nil #'org-indent-initialize-agent))
355 (throw 'interrupt (point)))
356 ;; Headline or inline task.
357 ((looking-at org-outline-regexp)
358 (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
359 (line (* added-ind-per-lvl (1- nstars)))
360 (wrap (+ line (1+ nstars))))
361 (cond
362 ;; Headline: new value for PF.
363 ((looking-at limited-re)
364 (org-indent-set-line-properties line wrap t)
365 (setq pf wrap))
366 ;; End of inline task: PF-INLINE is now nil.
367 ((looking-at "\\*+ end[ \t]*$")
368 (org-indent-set-line-properties line wrap 'inline)
369 (setq pf-inline nil))
370 ;; Start of inline task. Determine if it contains
371 ;; text, or if it is only one line long. Set
372 ;; PF-INLINE accordingly.
373 (t (org-indent-set-line-properties line wrap 'inline)
374 (setq pf-inline (and (org-inlinetask-in-task-p) wrap))))))
375 ;; List item: `wrap-prefix' is set where body starts.
376 ((org-at-item-p)
377 (let* ((line (or pf-inline pf 0))
378 (wrap (+ (org-list-item-body-column (point)) line)))
379 (org-indent-set-line-properties line wrap nil)))
380 ;; Normal line: use PF-INLINE, PF or nil as prefixes.
381 (t (let* ((line (or pf-inline pf 0))
382 (wrap (+ line (org-get-indentation))))
383 (org-indent-set-line-properties line wrap nil))))))))))
385 (defun org-indent-notify-modified-headline (beg end)
386 "Set `org-indent-modified-headline-flag' depending on context.
388 BEG and END are the positions of the beginning and end of the
389 range of deleted text.
391 This function is meant to be called by `before-change-functions'.
392 Flag will be non-nil if command is going to modify or delete an
393 headline."
394 (when org-indent-mode
395 (setq org-indent-modified-headline-flag
396 (save-excursion
397 (goto-char beg)
398 (save-match-data
399 (or (and (org-at-heading-p) (< beg (match-end 0)))
400 (re-search-forward org-outline-regexp-bol end t)))))))
402 (defun org-indent-refresh-maybe (beg end dummy)
403 "Refresh indentation properties in an adequate portion of buffer.
404 BEG and END are the positions of the beginning and end of the
405 range of inserted text. DUMMY is an unused argument.
407 This function is meant to be called by `after-change-functions'."
408 (when org-indent-mode
409 (save-match-data
410 ;; If an headline was modified or inserted, set properties until
411 ;; next headline.
412 (if (or org-indent-modified-headline-flag
413 (save-excursion
414 (goto-char beg)
415 (re-search-forward org-outline-regexp-bol end t)))
416 (let ((end (save-excursion
417 (goto-char end)
418 (org-with-limited-levels (outline-next-heading))
419 (point))))
420 (setq org-indent-modified-headline-flag nil)
421 (org-indent-add-properties beg end))
422 ;; Otherwise, only set properties on modified area.
423 (org-indent-add-properties beg end)))))
425 (provide 'org-indent)
427 ;;; org-indent.el ends here