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