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