org-indent: Fix indentation in inline tasks
[org-mode.git] / lisp / org-indent.el
blobda11324e36e8cfc1d23c6335b597e8c727139048
1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009-2015 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 (defvar org-indent-inlinetask-first-star (org-add-props "*" '(face org-warning))
56 "First star of inline tasks, with correct face.")
57 (defvar org-indent-agent-timer nil
58 "Timer running the initialize agent.")
59 (defvar org-indent-agentized-buffers nil
60 "List of buffers watched by the initialize agent.")
61 (defvar org-indent-agent-resume-timer nil
62 "Timer to reschedule agent after switching to other idle processes.")
63 (defvar org-indent-agent-active-delay '(0 2 0)
64 "Time to run agent before switching to other idle processes.
65 Delay used when the buffer to initialize is current.")
66 (defvar org-indent-agent-passive-delay '(0 0 400000)
67 "Time to run agent before switching to other idle processes.
68 Delay used when the buffer to initialize isn't current.")
69 (defvar org-indent-agent-resume-delay '(0 0 100000)
70 "Minimal time for other idle processes before switching back to agent.")
71 (defvar org-indent-initial-marker nil
72 "Position of initialization before interrupt.
73 This is used locally in each buffer being initialized.")
74 (defvar org-hide-leading-stars-before-indent-mode nil
75 "Used locally.")
76 (defvar org-indent-modified-headline-flag nil
77 "Non-nil means the last deletion operated on a headline.
78 It is modified by `org-indent-notify-modified-headline'.")
81 (defcustom org-indent-boundary-char ?\s
82 "The end of the virtual indentation strings, a single-character string.
83 The default is just a space, but if you wish, you can use \"|\" or so.
84 This can be useful on a terminal window - under a windowing system,
85 it may be prettier to customize the `org-indent' face."
86 :group 'org-indent
87 :type 'character)
89 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
90 "Non-nil means setting the variable `org-indent-mode' will \
91 turn off indentation adaptation.
92 For details see the variable `org-adapt-indentation'."
93 :group 'org-indent
94 :type 'boolean)
96 (defcustom org-indent-mode-turns-on-hiding-stars t
97 "Non-nil means setting the variable `org-indent-mode' will \
98 turn on `org-hide-leading-stars'."
99 :group 'org-indent
100 :type 'boolean)
102 (defcustom org-indent-indentation-per-level 2
103 "Indentation per level in number of characters."
104 :group 'org-indent
105 :type 'integer)
107 (defface org-indent '((t (:inherit org-hide)))
108 "Face for outline indentation.
109 The default is to make it look like whitespace. But you may find it
110 useful to make it ever so slightly different."
111 :group 'org-faces)
113 (defsubst org-indent-remove-properties (beg end)
114 "Remove indentations between BEG and END."
115 (org-with-silent-modifications
116 (remove-text-properties beg end '(line-prefix nil wrap-prefix nil))))
118 ;;;###autoload
119 (define-minor-mode org-indent-mode
120 "When active, indent text according to outline structure.
122 Internally this works by adding `line-prefix' and `wrap-prefix'
123 properties, after each buffer modification, on the modified zone.
125 The process is synchronous. Though, initial indentation of
126 buffer, which can take a few seconds on large buffers, is done
127 during idle time."
128 nil " Ind" nil
129 (cond
130 ((and org-indent-mode (featurep 'xemacs))
131 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
132 (setq org-indent-mode nil))
133 ((and org-indent-mode
134 (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
135 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
136 (ding)
137 (sit-for 1)
138 (setq org-indent-mode nil))
139 (org-indent-mode
140 ;; mode was turned on.
141 (org-set-local 'indent-tabs-mode nil)
142 (org-set-local 'org-indent-initial-marker (copy-marker 1))
143 (when org-indent-mode-turns-off-org-adapt-indentation
144 (org-set-local 'org-adapt-indentation nil))
145 (when org-indent-mode-turns-on-hiding-stars
146 (org-set-local 'org-hide-leading-stars-before-indent-mode
147 org-hide-leading-stars)
148 (org-set-local 'org-hide-leading-stars t))
149 (org-add-hook 'filter-buffer-substring-functions
150 (lambda (fun start end delete)
151 (org-indent-remove-properties-from-string
152 (funcall fun start end delete)))
153 nil t)
154 (org-add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
155 (org-add-hook 'before-change-functions
156 'org-indent-notify-modified-headline nil 'local)
157 (and font-lock-mode (org-restart-font-lock))
158 (org-indent-remove-properties (point-min) (point-max))
159 ;; Submit current buffer to initialize agent. If it's the first
160 ;; buffer submitted, also start the agent. Current buffer is
161 ;; pushed in both cases to avoid a race condition.
162 (if org-indent-agentized-buffers
163 (push (current-buffer) org-indent-agentized-buffers)
164 (push (current-buffer) org-indent-agentized-buffers)
165 (setq org-indent-agent-timer
166 (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
168 ;; mode was turned off (or we refused to turn it on)
169 (kill-local-variable 'org-adapt-indentation)
170 (setq org-indent-agentized-buffers
171 (delq (current-buffer) org-indent-agentized-buffers))
172 (when (markerp org-indent-initial-marker)
173 (set-marker org-indent-initial-marker nil))
174 (when (boundp 'org-hide-leading-stars-before-indent-mode)
175 (org-set-local 'org-hide-leading-stars
176 org-hide-leading-stars-before-indent-mode))
177 (remove-hook 'filter-buffer-substring-functions
178 (lambda (fun start end delete)
179 (org-indent-remove-properties-from-string
180 (funcall fun start end delete))))
181 (remove-hook 'after-change-functions 'org-indent-refresh-maybe 'local)
182 (remove-hook 'before-change-functions
183 'org-indent-notify-modified-headline 'local)
184 (org-with-wide-buffer
185 (org-indent-remove-properties (point-min) (point-max)))
186 (and font-lock-mode (org-restart-font-lock))
187 (redraw-display))))
189 (defun org-indent-indent-buffer ()
190 "Add indentation properties to the accessible part of the buffer."
191 (interactive)
192 (if (not (derived-mode-p 'org-mode))
193 (error "Not in Org mode")
194 (message "Setting buffer indentation. It may take a few seconds...")
195 (org-indent-remove-properties (point-min) (point-max))
196 (org-indent-add-properties (point-min) (point-max))
197 (message "Indentation of buffer set.")))
199 (defun org-indent-remove-properties-from-string (string)
200 "Remove indentation properties from STRING."
201 (remove-text-properties 0 (length string)
202 '(line-prefix nil wrap-prefix nil) string)
203 string)
205 (defun org-indent-initialize-agent ()
206 "Start or resume current buffer initialization.
207 Only buffers in `org-indent-agentized-buffers' trigger an action.
208 When no more buffer is being watched, the agent suppress itself."
209 (when org-indent-agent-resume-timer
210 (cancel-timer org-indent-agent-resume-timer))
211 (setq org-indent-agentized-buffers
212 (org-remove-if-not #'buffer-live-p org-indent-agentized-buffers))
213 (cond
214 ;; Job done: kill agent.
215 ((not org-indent-agentized-buffers) (cancel-timer org-indent-agent-timer))
216 ;; Current buffer is agentized: start/resume initialization
217 ;; somewhat aggressively.
218 ((memq (current-buffer) org-indent-agentized-buffers)
219 (org-indent-initialize-buffer (current-buffer)
220 org-indent-agent-active-delay))
221 ;; Else, start/resume initialization of the last agentized buffer,
222 ;; softly.
223 (t (org-indent-initialize-buffer (car org-indent-agentized-buffers)
224 org-indent-agent-passive-delay))))
226 (defun org-indent-initialize-buffer (buffer delay)
227 "Set virtual indentation for the buffer BUFFER, asynchronously.
228 Give hand to other idle processes if it takes longer than DELAY,
229 a time value."
230 (with-current-buffer buffer
231 (when org-indent-mode
232 (org-with-wide-buffer
233 (let ((interruptp
234 ;; Always nil unless interrupted.
235 (catch 'interrupt
236 (and org-indent-initial-marker
237 (marker-position org-indent-initial-marker)
238 (org-indent-add-properties org-indent-initial-marker
239 (point-max)
240 delay)
241 nil))))
242 (move-marker org-indent-initial-marker interruptp)
243 ;; Job is complete: un-agentize buffer.
244 (unless interruptp
245 (setq org-indent-agentized-buffers
246 (delq buffer org-indent-agentized-buffers))))))))
248 (defun org-indent-set-line-properties (level indentation &optional heading)
249 "Set prefix properties on current line an move to next one.
251 LEVEL is the current level of heading. INDENTATION is the
252 expected indentation when wrapping line.
254 When optional argument HEADING is non-nil, assume line is at
255 a heading. Moreover, if is is `inlinetask', the first star will
256 have `org-warning' face."
257 (let* ((stars (if (<= level 1) ""
258 (make-string (* org-indent-indentation-per-level
259 (1- level))
260 ?*)))
261 (line
262 (cond
263 ((and (org-bound-and-true-p org-inlinetask-show-first-star)
264 (eq heading 'inlinetask))
265 (concat org-indent-inlinetask-first-star
266 (org-add-props (substring stars 1) nil 'face 'org-hide)))
267 (heading (org-add-props stars nil 'face 'org-hide))
268 (t (concat (org-add-props (concat stars (make-string level ?*))
269 nil 'face 'org-indent)
270 (char-to-string org-indent-boundary-char)))))
271 (wrap
272 (org-add-props
273 (concat stars
274 (make-string level ?*)
275 (if heading " "
276 (make-string (+ indentation (min level 1)) ?\s)))
277 nil 'face 'org-indent)))
278 ;; Add properties down to the next line to indent empty lines.
279 (add-text-properties (line-beginning-position) (line-beginning-position 2)
280 `(line-prefix ,line wrap-prefix ,wrap)))
281 (forward-line))
283 (defun org-indent-add-properties (beg end &optional delay)
284 "Add indentation properties between BEG and END.
286 When DELAY is non-nil, it must be a time value. In that case,
287 the process is asynchronous and can be interrupted, either by
288 user request, or after DELAY. This is done by throwing the
289 `interrupt' tag along with the buffer position where the process
290 stopped."
291 (save-match-data
292 (org-with-wide-buffer
293 (goto-char beg)
294 (beginning-of-line)
295 ;; Initialize prefix at BEG, according to current entry's level.
296 (let* ((case-fold-search t)
297 (limited-re (org-get-limited-outline-regexp))
298 (level (or (org-current-level) 0))
299 (time-limit (and delay (time-add (current-time) delay))))
300 ;; For each line, set `line-prefix' and `wrap-prefix'
301 ;; properties depending on the type of line (headline, inline
302 ;; task, item or other).
303 (org-with-silent-modifications
304 (while (and (<= (point) end) (not (eobp)))
305 (cond
306 ;; When in asynchronous mode, check if interrupt is
307 ;; required.
308 ((and delay (input-pending-p)) (throw 'interrupt (point)))
309 ;; In asynchronous mode, take a break of
310 ;; `org-indent-agent-resume-delay' every DELAY to avoid
311 ;; blocking any other idle timer or process output.
312 ((and delay (time-less-p time-limit (current-time)))
313 (setq org-indent-agent-resume-timer
314 (run-with-idle-timer
315 (time-add (current-idle-time) org-indent-agent-resume-delay)
316 nil #'org-indent-initialize-agent))
317 (throw 'interrupt (point)))
318 ;; Headline or inline task.
319 ((looking-at org-outline-regexp)
320 (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
321 (type (or (org-looking-at-p limited-re) 'inlinetask)))
322 (org-indent-set-line-properties nstars 0 type)
323 ;; At an headline, define new value for LEVEL.
324 (unless (eq type 'inlinetask) (setq level nstars))))
325 ;; List item: `wrap-prefix' is set where body starts.
326 ((org-at-item-p)
327 (org-indent-set-line-properties
328 level (org-list-item-body-column (point))))
329 ;; Regular line.
331 (org-indent-set-line-properties level (org-get-indentation))))))))))
333 (defun org-indent-notify-modified-headline (beg end)
334 "Set `org-indent-modified-headline-flag' depending on context.
336 BEG and END are the positions of the beginning and end of the
337 range of deleted text.
339 This function is meant to be called by `before-change-functions'.
340 Flag will be non-nil if command is going to modify or delete an
341 headline."
342 (when org-indent-mode
343 (setq org-indent-modified-headline-flag
344 (save-excursion
345 (goto-char beg)
346 (save-match-data
347 (or (and (org-at-heading-p) (< beg (match-end 0)))
348 (re-search-forward org-outline-regexp-bol end t)))))))
350 (defun org-indent-refresh-maybe (beg end dummy)
351 "Refresh indentation properties in an adequate portion of buffer.
352 BEG and END are the positions of the beginning and end of the
353 range of inserted text. DUMMY is an unused argument.
355 This function is meant to be called by `after-change-functions'."
356 (when org-indent-mode
357 (save-match-data
358 ;; If a headline was modified or inserted, set properties until
359 ;; next headline.
360 (if (or org-indent-modified-headline-flag
361 (save-excursion
362 (goto-char beg)
363 (beginning-of-line)
364 (re-search-forward org-outline-regexp-bol end t)))
365 (let ((end (save-excursion
366 (goto-char end)
367 (org-with-limited-levels (outline-next-heading))
368 (point))))
369 (setq org-indent-modified-headline-flag nil)
370 (org-indent-add-properties beg end))
371 ;; Otherwise, only set properties on modified area.
372 (org-indent-add-properties beg end)))))
374 (provide 'org-indent)
376 ;; Local variables:
377 ;; generated-autoload-file: "org-loaddefs.el"
378 ;; End:
380 ;;; org-indent.el ends here