org-indent: handle more modifications of headlines
[org-mode.git] / lisp / org-indent.el
bloba592a943833734d463470f6df6335e2777074db4
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 ;;; Code:
33 (require 'org-macs)
34 (require 'org-compat)
35 (require 'org)
37 (eval-when-compile
38 (require 'cl))
40 (declare-function org-inlinetask-get-task-level "org-inlinetask" ())
41 (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
42 (declare-function org-list-item-body-column "org-list" (item))
44 (defgroup org-indent nil
45 "Options concerning dynamic virtual outline indentation."
46 :tag "Org Indent"
47 :group 'org)
49 (defconst org-indent-max 40
50 "Maximum indentation in characters.")
51 (defconst org-indent-max-levels 20
52 "Maximum added level through virtual indentation, in
53 characters.
55 It is computed by multiplying `org-indent-indentation-per-level'
56 minus one by actual level of the headline minus one.")
58 (defvar org-indent-strings nil
59 "Vector with all indentation strings.
60 It will be set in `org-indent-initialize'.")
61 (defvar org-indent-stars nil
62 "Vector with all indentation star strings.
63 It will be set in `org-indent-initialize'.")
64 (defvar org-indent-inlinetask-first-star (org-add-props "*" '(face org-warning))
65 "First star of inline tasks, with correct face.")
66 (defvar org-indent-initial-marker nil
67 "Position of initialization before interrupt.")
68 (defvar org-indent-initial-timer nil
69 "Timer used for initialization.")
70 (defvar org-indent-initial-lock nil
71 "Lock used of initialization.")
72 (defvar org-hide-leading-stars-before-indent-mode nil
73 "Used locally.")
74 (defvar org-indent-modified-headline-flag nil
75 "Non nil if the last deletion acted on an headline.
76 It is modified by `org-indent-notify-modified-headline'.")
79 (defcustom org-indent-boundary-char ?\ ; comment to protect space char
80 "The end of the virtual indentation strings, a single-character string.
81 The default is just a space, but if you wish, you can use \"|\" or so.
82 This can be useful on a terminal window - under a windowing system,
83 it may be prettier to customize the org-indent face."
84 :group 'org-indent
85 :set (lambda (var val)
86 (set var val)
87 (and org-indent-strings (org-indent-initialize)))
88 :type 'character)
90 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
91 "Non-nil means setting the variable `org-indent-mode' will \
92 turn off indentation adaptation.
93 For details see the variable `org-adapt-indentation'."
94 :group 'org-indent
95 :type 'boolean)
97 (defcustom org-indent-mode-turns-on-hiding-stars t
98 "Non-nil means setting the variable `org-indent-mode' will \
99 turn on `org-hide-leading-stars'."
100 :group 'org-indent
101 :type 'boolean)
103 (defcustom org-indent-indentation-per-level 2
104 "Indentation per level in number of characters."
105 :group 'org-indent
106 :type 'integer)
108 (defun org-indent-initialize ()
109 "Initialize the indentation strings."
110 (setq org-indent-strings (make-vector (1+ org-indent-max) nil))
111 (setq org-indent-stars (make-vector (1+ org-indent-max) nil))
112 (aset org-indent-strings 0 nil)
113 (aset org-indent-stars 0 nil)
114 (loop for i from 1 to org-indent-max do
115 (aset org-indent-strings i
116 (org-add-props
117 (concat (make-string (1- i) ?\ )
118 (char-to-string org-indent-boundary-char))
119 nil 'face 'org-indent)))
120 (loop for i from 1 to org-indent-max-levels do
121 (aset org-indent-stars i
122 (org-add-props (make-string i ?*)
123 nil 'face 'org-hide))))
125 ;;;###autoload
126 (define-minor-mode org-indent-mode
127 "When active, indent text according to outline structure.
130 Internally this works by adding `line-prefix' and `wrap-prefix'
131 properties, after each buffer modifiation, on the modified zone."
132 nil " Ind" nil
133 (cond
134 ((org-bound-and-true-p org-inhibit-startup)
135 (setq org-indent-mode nil))
136 ((and org-indent-mode (featurep 'xemacs))
137 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
138 (setq org-indent-mode nil))
139 ((and org-indent-mode
140 (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
141 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
142 (ding)
143 (sit-for 1)
144 (setq org-indent-mode nil))
145 (org-indent-mode
146 ;; mode was turned on.
147 (org-set-local 'indent-tabs-mode nil)
148 (or org-indent-strings (org-indent-initialize))
149 (org-set-local 'org-indent-initial-marker (copy-marker 1))
150 (org-set-local 'org-indent-initial-lock nil)
151 (when org-indent-mode-turns-off-org-adapt-indentation
152 (org-set-local 'org-adapt-indentation nil))
153 (when org-indent-mode-turns-on-hiding-stars
154 (org-set-local 'org-hide-leading-stars-before-indent-mode
155 org-hide-leading-stars)
156 (org-set-local 'org-hide-leading-stars t))
157 (make-local-variable 'buffer-substring-filters)
158 (add-to-list 'buffer-substring-filters
159 'org-indent-remove-properties-from-string)
160 (org-add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
161 (org-add-hook 'before-change-functions
162 'org-indent-notify-modified-headline nil 'local)
163 (and font-lock-mode (org-restart-font-lock))
164 (with-silent-modifications
165 (org-indent-remove-properties (point-min) (point-max)))
166 (org-set-local 'org-indent-initial-timer
167 (run-with-idle-timer 0.2 t #'org-indent-initialize-buffer)))
169 ;; mode was turned off (or we refused to turn it on)
170 (save-excursion
171 (save-restriction
172 (when org-indent-initialize-marker
173 (set-marker org-indent-initialize-marker nil))
174 (with-silent-modifications
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 'after-change-functions 'org-indent-refresh-maybe 'local)
184 (remove-hook 'before-change-functions
185 'org-indent-notify-modified-headline 'local)
186 (and font-lock-mode (org-restart-font-lock))
187 (redraw-display))))))
190 (defface org-indent
191 (org-compatible-face nil nil)
192 "Face for outline indentation.
193 The default is to make it look like whitespace. But you may find it
194 useful to make it ever so slightly different."
195 :group 'org-faces)
197 (defun org-indent-indent-buffer ()
198 "Add indentation properties for the whole buffer."
199 (interactive)
200 (if (not (org-mode-p))
201 (error "Buffer major mode must be Org")
202 (message "Setting buffer indentation. It may take a few seconds...")
203 (org-with-wide-buffer
204 (org-indent-remove-properties (point-min) (point-max))
205 (org-indent-add-properties (point-min) (point-max)))
206 (message "Indentation of buffer set.")))
208 (defsubst org-indent-remove-properties (beg end)
209 "Remove indentations between BEG and END."
210 (remove-text-properties beg end '(line-prefix nil wrap-prefix nil)))
212 (defun org-indent-remove-properties-from-string (string)
213 "Remove indentation properties from STRING."
214 (remove-text-properties 0 (length string)
215 '(line-prefix nil wrap-prefix nil) string)
216 string)
218 (defun org-indent-initialize-buffer ()
219 "Set virtual indentation for the whole buffer asynchronously."
220 (when (and org-indent-mode (not org-indent-initial-lock))
221 (org-with-wide-buffer
222 (setq org-indent-initial-lock t)
223 (let ((interruptp
224 ;; Always nil unless interrupted.
225 (catch 'interrupt
226 (and org-indent-initial-marker
227 (marker-position org-indent-initial-marker)
228 (org-indent-add-properties org-indent-initial-marker
229 (point-max) t)
230 nil))))
231 (move-marker org-indent-initial-marker interruptp)
232 ;; Job is complete: stop idle timer.
233 (unless interruptp (cancel-timer org-indent-initial-timer))))
234 (setq org-indent-initial-lock nil)))
236 (defun org-indent-add-properties (beg end &optional async)
237 "Add indentation properties between BEG and END.
239 If ASYNC is non-nil, allow to interrupt the process. This is
240 done by throwing the `interrupt' tag along with the buffer
241 position where the process stopped. Be sure to catch this tag if
242 you want to use this feature."
243 (save-match-data
244 (org-with-wide-buffer
245 (goto-char beg)
246 (beginning-of-line)
247 ;; 1. Initialize prefix at BEG. This is done by storing two
248 ;; variables: INLINE-PF and PF, representing respectively
249 ;; length of current `line-prefix' when line is inside an
250 ;; inline task or not.
251 (let* ((case-fold-search t)
252 (limited-re (org-get-limited-outline-regexp))
253 (added-ind-per-lvl (1- org-indent-indentation-per-level))
254 (pf (save-excursion
255 (and (ignore-errors (let ((outline-regexp limited-re))
256 (org-back-to-heading t)))
257 (+ (* org-indent-indentation-per-level
258 (- (match-end 0) (match-beginning 0) 2)) 2))))
259 (pf-inline (and (featurep 'org-inlinetask)
260 (org-inlinetask-in-task-p)
261 (+ (* org-indent-indentation-per-level
262 (1- (org-inlinetask-get-task-level))) 2)))
263 (set-prop-and-move
264 (function
265 ;; Set prefix properties `line-prefix' and `wrap-prefix'
266 ;; in current line to, respectively, length L and W and
267 ;; move forward. If H is non-nil, `line-prefix' will be
268 ;; starred. If H is `inline', the first star will have
269 ;; `org-warning' face. Assume point is at bol.
270 (lambda (l w h)
271 (let ((line (cond
272 ((eq 'inline h)
273 (let ((stars (aref org-indent-stars
274 (min l org-indent-max-levels))))
275 (and stars
276 (concat org-indent-inlinetask-first-star
277 (substring stars 1)))))
278 (h (aref org-indent-stars
279 (min l org-indent-max-levels)))
280 (t (aref org-indent-strings
281 (min l org-indent-max)))))
282 (wrap (aref org-indent-strings (min w org-indent-max))))
283 (add-text-properties (point) (point-at-eol)
284 `(line-prefix ,line wrap-prefix ,wrap)))
285 (forward-line 1)))))
286 ;; 2. For each line, set `line-prefix' and `wrap-prefix'
287 ;; properties depending on the type of line (headline, inline
288 ;; task, item or other).
289 (while (< (point) end)
290 (cond
291 ;; When in async mode, check if interrupt is required.
292 ((and async (input-pending-p)) (throw 'interrupt (point)))
293 ;; Empty line: do nothing.
294 ((eolp) (forward-line 1))
295 ;; Headline or inline task.
296 ((looking-at org-outline-regexp)
297 (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
298 (line (* added-ind-per-lvl (1- nstars)))
299 (wrap (+ line (1+ nstars))))
300 (cond
301 ;; Headline: new value for PF.
302 ((looking-at limited-re)
303 (funcall set-prop-and-move line wrap t)
304 (setq pf wrap))
305 ;; End of inline task: PF-INLINE is now nil.
306 ((looking-at "\\*+ end[ \t]*$")
307 (funcall set-prop-and-move line wrap 'inline)
308 (setq pf-inline nil))
309 ;; Start of inline task. Determine if it contains text,
310 ;; or is only one line long. Set PF-INLINE accordingly.
311 (t (funcall set-prop-and-move line wrap 'inline)
312 (setq pf-inline (and (org-inlinetask-in-task-p) wrap))))))
313 ;; List item: `wrap-prefix' is set where body starts.
314 ((org-at-item-p)
315 (let* ((line (or pf-inline pf 0))
316 (wrap (+ (org-list-item-body-column (point)) line)))
317 (funcall set-prop-and-move line wrap nil)))
318 ;; Normal line: use PF-INLINE, PF or nil as prefixes.
319 (t (let* ((line (or pf-inline pf 0))
320 (wrap (+ line (org-get-indentation))))
321 (funcall set-prop-and-move line wrap nil)))))))))
323 (defun org-indent-notify-modified-headline (beg end)
324 "Set `org-indent-modified-headline-flag' depending on the current command.
326 BEG and END are the positions of the beginning and end of the
327 range of deleted text.
329 This function is meant to be called by `before-change-functions'.
330 Flag will be non-nil if command is going to modify or delete an
331 headline."
332 (when org-indent-mode
333 (setq org-indent-modified-headline-flag
334 (save-excursion
335 (goto-char beg)
336 (save-match-data
337 (or (and (org-at-heading-p) (< beg (match-end 0)))
338 (re-search-forward org-outline-regexp-bol end t)))))))
340 (defun org-indent-refresh-maybe (beg end dummy)
341 "Refresh indentation properties in an adequate portion of buffer.
342 BEG and END are the positions of the beginning and end of the
343 range of inserted text. DUMMY is an unused argument.
345 This function is meant to be called by `after-change-functions'."
346 (when org-indent-mode
347 (save-match-data
348 ;; If an headline was modified or inserted, set properties until
349 ;; next headline.
350 (if (or org-indent-modified-headline-flag
351 (save-excursion
352 (goto-char beg)
353 (re-search-forward org-outline-regexp-bol end t)))
354 (let ((end (save-excursion
355 (goto-char end)
356 (org-with-limited-levels (outline-next-heading))
357 (point))))
358 (setq org-indent-modified-headline-flag nil)
359 (org-indent-add-properties beg end))
360 ;; Otherwise, only set properties on modified area.
361 (org-indent-add-properties beg end)))))
363 (provide 'org-indent)
365 ;;; org-indent.el ends here