org-attach: Fix git annex test directories
[org-mode/org-tableheadings.git] / lisp / org-indent.el
blobbd09dc04f4d4eebf1d4a2d0ccbf0286215b17e68
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 ((and org-indent-mode (featurep 'xemacs))
133 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
134 (setq org-indent-mode nil))
135 ((and org-indent-mode
136 (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
137 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
138 (ding)
139 (sit-for 1)
140 (setq org-indent-mode nil))
141 (org-indent-mode
142 ;; mode was turned on.
143 (setq-local indent-tabs-mode nil)
144 (setq-local org-indent--initial-marker (copy-marker 1))
145 (when org-indent-mode-turns-off-org-adapt-indentation
146 (setq-local org-adapt-indentation nil))
147 (when org-indent-mode-turns-on-hiding-stars
148 (setq-local org-hide-leading-stars-before-indent-mode
149 org-hide-leading-stars)
150 (setq-local org-hide-leading-stars t))
151 (org-add-hook 'filter-buffer-substring-functions
152 (lambda (fun start end delete)
153 (org-indent-remove-properties-from-string
154 (funcall fun start end delete)))
155 nil t)
156 (org-add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
157 (org-add-hook 'before-change-functions
158 'org-indent-notify-modified-headline nil 'local)
159 (and font-lock-mode (org-restart-font-lock))
160 (org-indent-remove-properties (point-min) (point-max))
161 ;; Submit current buffer to initialize agent. If it's the first
162 ;; buffer submitted, also start the agent. Current buffer is
163 ;; pushed in both cases to avoid a race condition.
164 (if org-indent-agentized-buffers
165 (push (current-buffer) org-indent-agentized-buffers)
166 (push (current-buffer) org-indent-agentized-buffers)
167 (setq org-indent-agent-timer
168 (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
170 ;; mode was turned off (or we refused to turn it on)
171 (kill-local-variable 'org-adapt-indentation)
172 (setq org-indent-agentized-buffers
173 (delq (current-buffer) org-indent-agentized-buffers))
174 (when (markerp org-indent--initial-marker)
175 (set-marker org-indent--initial-marker nil))
176 (when (boundp 'org-hide-leading-stars-before-indent-mode)
177 (setq-local org-hide-leading-stars
178 org-hide-leading-stars-before-indent-mode))
179 (remove-hook 'filter-buffer-substring-functions
180 (lambda (fun start end delete)
181 (org-indent-remove-properties-from-string
182 (funcall fun start end delete))))
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 (org-with-wide-buffer
187 (org-indent-remove-properties (point-min) (point-max)))
188 (and font-lock-mode (org-restart-font-lock))
189 (redraw-display))))
191 (defun org-indent-indent-buffer ()
192 "Add indentation properties to the accessible part of the buffer."
193 (interactive)
194 (if (not (derived-mode-p 'org-mode))
195 (error "Not in Org mode")
196 (message "Setting buffer indentation. It may take a few seconds...")
197 (org-indent-remove-properties (point-min) (point-max))
198 (org-indent-add-properties (point-min) (point-max))
199 (message "Indentation of buffer set.")))
201 (defun org-indent-remove-properties-from-string (string)
202 "Remove indentation properties from STRING."
203 (remove-text-properties 0 (length string)
204 '(line-prefix nil wrap-prefix nil) string)
205 string)
207 (defun org-indent-initialize-agent ()
208 "Start or resume current buffer initialization.
209 Only buffers in `org-indent-agentized-buffers' trigger an action.
210 When no more buffer is being watched, the agent suppress itself."
211 (when org-indent-agent-resume-timer
212 (cancel-timer org-indent-agent-resume-timer))
213 (setq org-indent-agentized-buffers
214 (cl-remove-if-not #'buffer-live-p org-indent-agentized-buffers))
215 (cond
216 ;; Job done: kill agent.
217 ((not org-indent-agentized-buffers) (cancel-timer org-indent-agent-timer))
218 ;; Current buffer is agentized: start/resume initialization
219 ;; somewhat aggressively.
220 ((memq (current-buffer) org-indent-agentized-buffers)
221 (org-indent-initialize-buffer (current-buffer)
222 org-indent-agent-active-delay))
223 ;; Else, start/resume initialization of the last agentized buffer,
224 ;; softly.
225 (t (org-indent-initialize-buffer (car org-indent-agentized-buffers)
226 org-indent-agent-passive-delay))))
228 (defun org-indent-initialize-buffer (buffer delay)
229 "Set virtual indentation for the buffer BUFFER, asynchronously.
230 Give hand to other idle processes if it takes longer than DELAY,
231 a time value."
232 (with-current-buffer buffer
233 (when org-indent-mode
234 (org-with-wide-buffer
235 (let ((interruptp
236 ;; Always nil unless interrupted.
237 (catch 'interrupt
238 (and org-indent--initial-marker
239 (marker-position org-indent--initial-marker)
240 (org-indent-add-properties org-indent--initial-marker
241 (point-max)
242 delay)
243 nil))))
244 (move-marker org-indent--initial-marker interruptp)
245 ;; Job is complete: un-agentize buffer.
246 (unless interruptp
247 (setq org-indent-agentized-buffers
248 (delq buffer org-indent-agentized-buffers))))))))
250 (defun org-indent-set-line-properties (level indentation &optional heading)
251 "Set prefix properties on current line an move to next one.
253 LEVEL is the current level of heading. INDENTATION is the
254 expected indentation when wrapping line.
256 When optional argument HEADING is non-nil, assume line is at
257 a heading. Moreover, if is is `inlinetask', the first star will
258 have `org-warning' face."
259 (let* ((stars (if (<= level 1) ""
260 (make-string (* (1- org-indent-indentation-per-level)
261 (1- level))
262 ?*)))
263 (line
264 (cond
265 ((and (org-bound-and-true-p org-inlinetask-show-first-star)
266 (eq heading 'inlinetask))
267 (concat org-indent-inlinetask-first-star
268 (org-add-props (substring stars 1) nil 'face 'org-hide)))
269 (heading (org-add-props stars nil 'face 'org-hide))
270 (t (concat (org-add-props (concat stars (make-string level ?*))
271 nil 'face 'org-indent)
272 (and (> level 0)
273 (char-to-string org-indent-boundary-char))))))
274 (wrap
275 (org-add-props
276 (concat stars
277 (make-string level ?*)
278 (if heading " "
279 (make-string (+ indentation (min level 1)) ?\s)))
280 nil 'face 'org-indent)))
281 ;; Add properties down to the next line to indent empty lines.
282 (add-text-properties (line-beginning-position) (line-beginning-position 2)
283 `(line-prefix ,line wrap-prefix ,wrap)))
284 (forward-line))
286 (defun org-indent-add-properties (beg end &optional delay)
287 "Add indentation properties between BEG and END.
289 When DELAY is non-nil, it must be a time value. In that case,
290 the process is asynchronous and can be interrupted, either by
291 user request, or after DELAY. This is done by throwing the
292 `interrupt' tag along with the buffer position where the process
293 stopped."
294 (save-match-data
295 (org-with-wide-buffer
296 (goto-char beg)
297 (beginning-of-line)
298 ;; Initialize prefix at BEG, according to current entry's level.
299 (let* ((case-fold-search t)
300 (limited-re (org-get-limited-outline-regexp))
301 (level (or (org-current-level) 0))
302 (time-limit (and delay (time-add (current-time) delay))))
303 ;; For each line, set `line-prefix' and `wrap-prefix'
304 ;; properties depending on the type of line (headline, inline
305 ;; task, item or other).
306 (org-with-silent-modifications
307 (while (and (<= (point) end) (not (eobp)))
308 (cond
309 ;; When in asynchronous mode, check if interrupt is
310 ;; required.
311 ((and delay (input-pending-p)) (throw 'interrupt (point)))
312 ;; In asynchronous mode, take a break of
313 ;; `org-indent-agent-resume-delay' every DELAY to avoid
314 ;; blocking any other idle timer or process output.
315 ((and delay (time-less-p time-limit (current-time)))
316 (setq org-indent-agent-resume-timer
317 (run-with-idle-timer
318 (time-add (current-idle-time) org-indent-agent-resume-delay)
319 nil #'org-indent-initialize-agent))
320 (throw 'interrupt (point)))
321 ;; Headline or inline task.
322 ((looking-at org-outline-regexp)
323 (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
324 (type (or (org-looking-at-p limited-re) 'inlinetask)))
325 (org-indent-set-line-properties nstars 0 type)
326 ;; At an headline, define new value for LEVEL.
327 (unless (eq type 'inlinetask) (setq level nstars))))
328 ;; List item: `wrap-prefix' is set where body starts.
329 ((org-at-item-p)
330 (org-indent-set-line-properties
331 level (org-list-item-body-column (point))))
332 ;; Regular line.
334 (org-indent-set-line-properties level (org-get-indentation))))))))))
336 (defun org-indent-notify-modified-headline (beg end)
337 "Set `org-indent-modified-headline-flag' depending on context.
339 BEG and END are the positions of the beginning and end of the
340 range of deleted text.
342 This function is meant to be called by `before-change-functions'.
343 Flag will be non-nil if command is going to modify or delete an
344 headline."
345 (when org-indent-mode
346 (setq org-indent-modified-headline-flag
347 (org-with-wide-buffer
348 (goto-char beg)
349 (save-match-data
350 (or (and (org-at-heading-p) (< beg (match-end 0)))
351 (re-search-forward
352 (org-with-limited-levels org-outline-regexp-bol) end t)))))))
354 (defun org-indent-refresh-maybe (beg end _)
355 "Refresh indentation properties in an adequate portion of buffer.
356 BEG and END are the positions of the beginning and end of the
357 range of inserted text. DUMMY is an unused argument.
359 This function is meant to be called by `after-change-functions'."
360 (when org-indent-mode
361 (save-match-data
362 ;; If a headline was modified or inserted, set properties until
363 ;; next headline.
364 (org-with-wide-buffer
365 (if (or org-indent-modified-headline-flag
366 (save-excursion
367 (goto-char beg)
368 (beginning-of-line)
369 (re-search-forward
370 (org-with-limited-levels org-outline-regexp-bol) end t)))
371 (let ((end (save-excursion
372 (goto-char end)
373 (org-with-limited-levels (outline-next-heading))
374 (point))))
375 (setq org-indent-modified-headline-flag nil)
376 (org-indent-add-properties beg end))
377 ;; Otherwise, only set properties on modified area.
378 (org-indent-add-properties beg end))))))
380 (provide 'org-indent)
382 ;; Local variables:
383 ;; generated-autoload-file: "org-loaddefs.el"
384 ;; End:
386 ;;; org-indent.el ends here