Merge branch 'maint'
[org-mode.git] / lisp / org-indent.el
blobe1a33172eed56bd93f60ad1d0070a07337d66f3a
1 ;;; org-indent.el --- Dynamic indentation for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This is an implementation of dynamic virtual indentation. It works
29 ;; by adding text properties to a buffer to make sure lines are
30 ;; indented according to outline structure.
32 ;; The process is synchronous, toggled at every buffer modification.
33 ;; Though, the initialization (indentation of text already in the
34 ;; buffer), which can take a few seconds in large buffers, happens on
35 ;; idle time.
37 ;;; Code:
39 (require 'org-macs)
40 (require 'org-compat)
41 (require 'org)
43 (require 'cl-lib)
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 (org-indent-mode
131 ;; mode was turned on.
132 (setq-local indent-tabs-mode nil)
133 (setq-local org-indent--initial-marker (copy-marker 1))
134 (when org-indent-mode-turns-off-org-adapt-indentation
135 (setq-local org-adapt-indentation nil))
136 (when org-indent-mode-turns-on-hiding-stars
137 (setq-local org-hide-leading-stars-before-indent-mode
138 org-hide-leading-stars)
139 (setq-local org-hide-leading-stars t))
140 (add-hook 'filter-buffer-substring-functions
141 (lambda (fun start end delete)
142 (org-indent-remove-properties-from-string
143 (funcall fun start end delete)))
144 nil t)
145 (add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
146 (add-hook 'before-change-functions
147 'org-indent-notify-modified-headline nil 'local)
148 (and font-lock-mode (org-restart-font-lock))
149 (org-indent-remove-properties (point-min) (point-max))
150 ;; Submit current buffer to initialize agent. If it's the first
151 ;; buffer submitted, also start the agent. Current buffer is
152 ;; pushed in both cases to avoid a race condition.
153 (if org-indent-agentized-buffers
154 (push (current-buffer) org-indent-agentized-buffers)
155 (push (current-buffer) org-indent-agentized-buffers)
156 (setq org-indent-agent-timer
157 (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
159 ;; mode was turned off (or we refused to turn it on)
160 (kill-local-variable 'org-adapt-indentation)
161 (setq org-indent-agentized-buffers
162 (delq (current-buffer) org-indent-agentized-buffers))
163 (when (markerp org-indent--initial-marker)
164 (set-marker org-indent--initial-marker nil))
165 (when (boundp 'org-hide-leading-stars-before-indent-mode)
166 (setq-local org-hide-leading-stars
167 org-hide-leading-stars-before-indent-mode))
168 (remove-hook 'filter-buffer-substring-functions
169 (lambda (fun start end delete)
170 (org-indent-remove-properties-from-string
171 (funcall fun start end delete))))
172 (remove-hook 'after-change-functions 'org-indent-refresh-maybe 'local)
173 (remove-hook 'before-change-functions
174 'org-indent-notify-modified-headline 'local)
175 (org-with-wide-buffer
176 (org-indent-remove-properties (point-min) (point-max)))
177 (and font-lock-mode (org-restart-font-lock))
178 (redraw-display))))
180 (defun org-indent-indent-buffer ()
181 "Add indentation properties to the accessible part of the buffer."
182 (interactive)
183 (if (not (derived-mode-p 'org-mode))
184 (error "Not in Org mode")
185 (message "Setting buffer indentation. It may take a few seconds...")
186 (org-indent-remove-properties (point-min) (point-max))
187 (org-indent-add-properties (point-min) (point-max))
188 (message "Indentation of buffer set.")))
190 (defun org-indent-remove-properties-from-string (string)
191 "Remove indentation properties from STRING."
192 (remove-text-properties 0 (length string)
193 '(line-prefix nil wrap-prefix nil) string)
194 string)
196 (defun org-indent-initialize-agent ()
197 "Start or resume current buffer initialization.
198 Only buffers in `org-indent-agentized-buffers' trigger an action.
199 When no more buffer is being watched, the agent suppress itself."
200 (when org-indent-agent-resume-timer
201 (cancel-timer org-indent-agent-resume-timer))
202 (setq org-indent-agentized-buffers
203 (cl-remove-if-not #'buffer-live-p org-indent-agentized-buffers))
204 (cond
205 ;; Job done: kill agent.
206 ((not org-indent-agentized-buffers) (cancel-timer org-indent-agent-timer))
207 ;; Current buffer is agentized: start/resume initialization
208 ;; somewhat aggressively.
209 ((memq (current-buffer) org-indent-agentized-buffers)
210 (org-indent-initialize-buffer (current-buffer)
211 org-indent-agent-active-delay))
212 ;; Else, start/resume initialization of the last agentized buffer,
213 ;; softly.
214 (t (org-indent-initialize-buffer (car org-indent-agentized-buffers)
215 org-indent-agent-passive-delay))))
217 (defun org-indent-initialize-buffer (buffer delay)
218 "Set virtual indentation for the buffer BUFFER, asynchronously.
219 Give hand to other idle processes if it takes longer than DELAY,
220 a time value."
221 (with-current-buffer buffer
222 (when org-indent-mode
223 (org-with-wide-buffer
224 (let ((interruptp
225 ;; Always nil unless interrupted.
226 (catch 'interrupt
227 (and org-indent--initial-marker
228 (marker-position org-indent--initial-marker)
229 (equal (marker-buffer org-indent--initial-marker)
230 buffer)
231 (org-indent-add-properties org-indent--initial-marker
232 (point-max)
233 delay)
234 nil))))
235 (move-marker org-indent--initial-marker interruptp)
236 ;; Job is complete: un-agentize buffer.
237 (unless interruptp
238 (setq org-indent-agentized-buffers
239 (delq buffer org-indent-agentized-buffers))))))))
241 (defun org-indent-set-line-properties (level indentation &optional heading)
242 "Set prefix properties on current line an move to next one.
244 LEVEL is the current level of heading. INDENTATION is the
245 expected indentation when wrapping line.
247 When optional argument HEADING is non-nil, assume line is at
248 a heading. Moreover, if is is `inlinetask', the first star will
249 have `org-warning' face."
250 (let* ((stars (if (<= level 1) ""
251 (make-string (* (1- org-indent-indentation-per-level)
252 (1- level))
253 ?*)))
254 (line
255 (cond
256 ((and (bound-and-true-p org-inlinetask-show-first-star)
257 (eq heading 'inlinetask))
258 (concat org-indent-inlinetask-first-star
259 (org-add-props (substring stars 1) nil 'face 'org-hide)))
260 (heading (org-add-props stars nil 'face 'org-hide))
261 (t (concat (org-add-props (concat stars (make-string level ?*))
262 nil 'face 'org-indent)
263 (and (> level 0)
264 (char-to-string org-indent-boundary-char))))))
265 (wrap
266 (org-add-props
267 (concat stars
268 (make-string level ?*)
269 (if heading " "
270 (make-string (+ indentation (min level 1)) ?\s)))
271 nil 'face 'org-indent)))
272 ;; Add properties down to the next line to indent empty lines.
273 (add-text-properties (line-beginning-position) (line-beginning-position 2)
274 `(line-prefix ,line wrap-prefix ,wrap)))
275 (forward-line))
277 (defun org-indent-add-properties (beg end &optional delay)
278 "Add indentation properties between BEG and END.
280 When DELAY is non-nil, it must be a time value. In that case,
281 the process is asynchronous and can be interrupted, either by
282 user request, or after DELAY. This is done by throwing the
283 `interrupt' tag along with the buffer position where the process
284 stopped."
285 (save-match-data
286 (org-with-wide-buffer
287 (goto-char beg)
288 (beginning-of-line)
289 ;; Initialize prefix at BEG, according to current entry's level.
290 (let* ((case-fold-search t)
291 (limited-re (org-get-limited-outline-regexp))
292 (level (or (org-current-level) 0))
293 (time-limit (and delay (time-add (current-time) delay))))
294 ;; For each line, set `line-prefix' and `wrap-prefix'
295 ;; properties depending on the type of line (headline, inline
296 ;; task, item or other).
297 (org-with-silent-modifications
298 (while (and (<= (point) end) (not (eobp)))
299 (cond
300 ;; When in asynchronous mode, check if interrupt is
301 ;; required.
302 ((and delay (input-pending-p)) (throw 'interrupt (point)))
303 ;; In asynchronous mode, take a break of
304 ;; `org-indent-agent-resume-delay' every DELAY to avoid
305 ;; blocking any other idle timer or process output.
306 ((and delay (time-less-p time-limit (current-time)))
307 (setq org-indent-agent-resume-timer
308 (run-with-idle-timer
309 (time-add (current-idle-time) org-indent-agent-resume-delay)
310 nil #'org-indent-initialize-agent))
311 (throw 'interrupt (point)))
312 ;; Headline or inline task.
313 ((looking-at org-outline-regexp)
314 (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
315 (type (or (looking-at-p limited-re) 'inlinetask)))
316 (org-indent-set-line-properties nstars 0 type)
317 ;; At an headline, define new value for LEVEL.
318 (unless (eq type 'inlinetask) (setq level nstars))))
319 ;; List item: `wrap-prefix' is set where body starts.
320 ((org-at-item-p)
321 (org-indent-set-line-properties
322 level (org-list-item-body-column (point))))
323 ;; Regular line.
325 (org-indent-set-line-properties level (org-get-indentation))))))))))
327 (defun org-indent-notify-modified-headline (beg end)
328 "Set `org-indent-modified-headline-flag' depending on context.
330 BEG and END are the positions of the beginning and end of the
331 range of deleted text.
333 This function is meant to be called by `before-change-functions'.
334 Flag will be non-nil if command is going to modify or delete an
335 headline."
336 (when org-indent-mode
337 (setq org-indent-modified-headline-flag
338 (org-with-wide-buffer
339 (goto-char beg)
340 (save-match-data
341 (or (and (org-at-heading-p) (< beg (match-end 0)))
342 (re-search-forward
343 (org-with-limited-levels org-outline-regexp-bol) end t)))))))
345 (defun org-indent-refresh-maybe (beg end _)
346 "Refresh indentation properties in an adequate portion of buffer.
347 BEG and END are the positions of the beginning and end of the
348 range of inserted text. DUMMY is an unused argument.
350 This function is meant to be called by `after-change-functions'."
351 (when org-indent-mode
352 (save-match-data
353 ;; If a headline was modified or inserted, set properties until
354 ;; next headline.
355 (org-with-wide-buffer
356 (if (or org-indent-modified-headline-flag
357 (save-excursion
358 (goto-char beg)
359 (beginning-of-line)
360 (re-search-forward
361 (org-with-limited-levels org-outline-regexp-bol) end t)))
362 (let ((end (save-excursion
363 (goto-char end)
364 (org-with-limited-levels (outline-next-heading))
365 (point))))
366 (setq org-indent-modified-headline-flag nil)
367 (org-indent-add-properties beg end))
368 ;; Otherwise, only set properties on modified area.
369 (org-indent-add-properties beg end))))))
371 (provide 'org-indent)
373 ;; Local variables:
374 ;; generated-autoload-file: "org-loaddefs.el"
375 ;; End:
377 ;;; org-indent.el ends here