* lisp/thingatpt.el (forward-whitespace): Typo.
[emacs.git] / lisp / org / org-indent.el
blob50dd6ac027a3c58fab8557f1afb2b3222d92fa0a
1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009-2011 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 ;; Version: 7.4
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 ;;; Code:
34 (require 'org-macs)
35 (require 'org-compat)
36 (require 'org)
38 (eval-when-compile
39 (require 'cl))
41 (defvar org-inlinetask-min-level)
42 (declare-function org-inlinetask-get-task-level "org-inlinetask" ())
43 (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
45 (defgroup org-indent nil
46 "Options concerning dynamic virtual outline indentation."
47 :tag "Org Indent"
48 :group 'org)
50 (defconst org-indent-max 40
51 "Maximum indentation in characters.")
52 (defconst org-indent-max-levels 40
53 "Maximum indentation in characters.")
55 (defvar org-indent-strings nil
56 "Vector with all indentation strings.
57 It will be set in `org-indent-initialize'.")
58 (defvar org-indent-stars nil
59 "Vector with all indentation star strings.
60 It will be set in `org-indent-initialize'.")
61 (defvar org-hide-leading-stars-before-indent-mode nil
62 "Used locally.")
64 (defcustom org-indent-boundary-char ?\ ; comment to protect space char
65 "The end of the virtual indentation strings, a single-character string.
66 The default is just a space, but if you wish, you can use \"|\" or so.
67 This can be useful on a terminal window - under a windowing system,
68 it may be prettier to customize the org-indent face."
69 :group 'org-indent
70 :set (lambda (var val)
71 (set var val)
72 (and org-indent-strings (org-indent-initialize)))
73 :type 'character)
75 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
76 "Non-nil means setting the variable `org-indent-mode' will \
77 turn off indentation adaptation.
78 For details see the variable `org-adapt-indentation'."
79 :group 'org-indent
80 :type 'boolean)
82 (defcustom org-indent-mode-turns-on-hiding-stars t
83 "Non-nil means setting the variable `org-indent-mode' will \
84 turn on `org-hide-leading-stars'."
85 :group 'org-indent
86 :type 'boolean)
88 (defcustom org-indent-indentation-per-level 2
89 "Indentation per level in number of characters."
90 :group 'org-indent
91 :type 'integer)
93 (defcustom org-indent-fix-section-after-idle-time 0.2
94 "Seconds of idle time before fixing virtual indentation of section.
95 The hooking-in of virtual indentation is not yet perfect. Occasionally,
96 a change does not trigger to proper change of indentation. For this we
97 have a timer action that fixes indentation in the current section after
98 a short amount idle time. If we ever get the integration to work perfectly,
99 this variable can be set to nil to get rid of the timer."
100 :group 'org-indent
101 :type '(choice
102 (const "Do not install idle timer" nil)
103 (number :tag "Idle time")))
105 (defun org-indent-initialize ()
106 "Initialize the indentation strings and set the idle timer."
107 ;; We use an idle timer to "repair" the current section, because the
108 ;; redisplay seems to have some problems.
109 (unless org-indent-strings
110 (when org-indent-fix-section-after-idle-time
111 (run-with-idle-timer
112 org-indent-fix-section-after-idle-time
113 t 'org-indent-refresh-section)))
114 ;; Initialize the indentation and star vectors
115 (setq org-indent-strings (make-vector (1+ org-indent-max) nil))
116 (setq org-indent-stars (make-vector (1+ org-indent-max) nil))
117 (aset org-indent-strings 0 nil)
118 (aset org-indent-stars 0 nil)
119 (loop for i from 1 to org-indent-max do
120 (aset org-indent-strings i
121 (org-add-props
122 (concat (make-string (1- i) ?\ )
123 (char-to-string org-indent-boundary-char))
124 nil 'face 'org-indent)))
125 (loop for i from 1 to org-indent-max-levels do
126 (aset org-indent-stars i
127 (org-add-props (make-string i ?*)
128 nil 'face 'org-hide))))
130 ;;;###autoload
131 (define-minor-mode org-indent-mode
132 "When active, indent text according to outline structure.
134 Internally this works by adding `line-prefix' properties to all non-headlines.
135 These properties are updated locally in idle time.
136 FIXME: How to update when broken?"
137 nil " Ind" nil
138 (cond
139 ((org-bound-and-true-p org-inhibit-startup)
140 (setq org-indent-mode nil))
141 ((and org-indent-mode (featurep 'xemacs))
142 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
143 (setq org-indent-mode nil))
144 ((and org-indent-mode
145 (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
146 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
147 (ding)
148 (sit-for 1)
149 (setq org-indent-mode nil))
150 (org-indent-mode
151 ;; mode was turned on.
152 (org-set-local 'indent-tabs-mode nil)
153 (or org-indent-strings (org-indent-initialize))
154 (when org-indent-mode-turns-off-org-adapt-indentation
155 (org-set-local 'org-adapt-indentation nil))
156 (when org-indent-mode-turns-on-hiding-stars
157 (org-set-local 'org-hide-leading-stars-before-indent-mode
158 org-hide-leading-stars)
159 (org-set-local 'org-hide-leading-stars t))
160 (make-local-variable 'buffer-substring-filters)
161 (add-to-list 'buffer-substring-filters
162 'org-indent-remove-properties-from-string)
163 (org-add-hook 'org-after-demote-entry-hook
164 'org-indent-refresh-section nil 'local)
165 (org-add-hook 'org-after-promote-entry-hook
166 'org-indent-refresh-section nil 'local)
167 (org-add-hook 'org-font-lock-hook
168 'org-indent-refresh-to nil 'local)
169 (and font-lock-mode (org-restart-font-lock))
172 ;; mode was turned off (or we refused to turn it on)
173 (save-excursion
174 (save-restriction
175 (org-indent-remove-properties (point-min) (point-max))
176 (kill-local-variable 'org-adapt-indentation)
177 (when (boundp 'org-hide-leading-stars-before-indent-mode)
178 (org-set-local 'org-hide-leading-stars
179 org-hide-leading-stars-before-indent-mode))
180 (setq buffer-substring-filters
181 (delq 'org-indent-remove-properties-from-string
182 buffer-substring-filters))
183 (remove-hook 'org-after-promote-entry-hook
184 'org-indent-refresh-section 'local)
185 (remove-hook 'org-after-demote-entry-hook
186 'org-indent-refresh-section 'local)
187 (and font-lock-mode (org-restart-font-lock))
188 (redraw-display))))))
191 (defface org-indent
192 (org-compatible-face nil nil)
193 "Face for outline indentation.
194 The default is to make it look like whitespace. But you may find it
195 useful to make it ever so slightly different."
196 :group 'org-faces)
198 (defun org-indent-indent-buffer ()
199 "Add indentation properties for the whole buffer."
200 (interactive)
201 (when org-indent-mode
202 (save-excursion
203 (save-restriction
204 (widen)
205 (org-indent-remove-properties (point-min) (point-max))
206 (org-indent-add-properties (point-min) (point-max))))))
208 (defun org-indent-remove-properties (beg end)
209 "Remove indentations between BEG and END."
210 (let ((inhibit-modification-hooks t))
211 (with-silent-modifications
212 (remove-text-properties beg end '(line-prefix nil wrap-prefix nil)))))
214 (defun org-indent-remove-properties-from-string (string)
215 "Remove indentations between BEG and END."
216 (remove-text-properties 0 (length string)
217 '(line-prefix nil wrap-prefix nil) string)
218 string)
220 (defvar org-indent-outline-re (concat "^" org-outline-regexp)
221 "Outline heading regexp.")
223 (defun org-indent-add-properties (beg end)
224 "Add indentation properties between BEG and END.
225 Assumes that BEG is at the beginning of a line."
226 (let* ((inhibit-modification-hooks t)
227 (inlinetaskp (featurep 'org-inlinetask))
228 (get-real-level (lambda (pos lvl)
229 (save-excursion
230 (goto-char pos)
231 (if (and inlinetaskp (org-inlinetask-in-task-p))
232 (org-inlinetask-get-task-level)
233 lvl))))
234 (b beg)
235 (e end)
236 (level 0)
237 (n 0)
238 exit nstars)
239 (with-silent-modifications
240 (save-excursion
241 (goto-char beg)
242 (while (not exit)
243 (setq e end)
244 (if (not (re-search-forward org-indent-outline-re nil t))
245 (setq e (point-max) exit t)
246 (setq e (match-beginning 0))
247 (if (>= e end) (setq exit t))
248 (unless (and inlinetaskp (org-inlinetask-in-task-p))
249 (setq level (- (match-end 0) (match-beginning 0) 1)))
250 (setq nstars (* (1- (funcall get-real-level e level))
251 (1- org-indent-indentation-per-level)))
252 (add-text-properties
253 (point-at-bol) (point-at-eol)
254 (list 'line-prefix
255 (aref org-indent-stars nstars)
256 'wrap-prefix
257 (aref org-indent-strings
258 (* (funcall get-real-level e level)
259 org-indent-indentation-per-level)))))
260 (when (> e b)
261 (add-text-properties
262 b e (list 'line-prefix (aref org-indent-strings n)
263 'wrap-prefix (aref org-indent-strings n))))
264 (setq b (1+ (point-at-eol))
265 n (* (funcall get-real-level b level)
266 org-indent-indentation-per-level)))))))
268 (defvar org-inlinetask-min-level)
269 (defun org-indent-refresh-section ()
270 "Refresh indentation properties in the current outline section.
271 Point is assumed to be at the beginning of a headline."
272 (interactive)
273 (when org-indent-mode
274 (let (beg end)
275 (save-excursion
276 (when (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
277 (if (featurep 'org-inlinetask)
278 (1- org-inlinetask-min-level)
279 ""))))
280 (org-back-to-heading)))
281 (setq beg (point))
282 (setq end (or (save-excursion (or (outline-next-heading) (point)))))
283 (org-indent-remove-properties beg end)
284 (org-indent-add-properties beg end))))))
286 (defun org-indent-refresh-to (limit)
287 "Refresh indentation properties in the current outline section.
288 Point is assumed to be at the beginning of a headline."
289 (interactive)
290 (when org-indent-mode
291 (let ((beg (point)) (end limit))
292 (save-excursion
293 (and (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
294 (if (featurep 'org-inlinetask)
295 (1- org-inlinetask-min-level)
296 ""))))
297 (org-back-to-heading)))
298 (setq beg (point))))
299 (org-indent-remove-properties beg end)
300 (org-indent-add-properties beg end)))
301 (goto-char limit))
303 (defun org-indent-refresh-subtree ()
304 "Refresh indentation properties in the current outline subtree.
305 Point is assumed to be at the beginning of a headline."
306 (interactive)
307 (when org-indent-mode
308 (save-excursion
309 (let (beg end)
310 (setq beg (point))
311 (setq end (save-excursion (org-end-of-subtree t t)))
312 (org-indent-remove-properties beg end)
313 (org-indent-add-properties beg end)))))
315 (defun org-indent-refresh-buffer ()
316 "Refresh indentation properties in the current outline subtree.
317 Point is assumed to be at the beginning of a headline."
318 (interactive)
319 (when org-indent-mode
320 (org-indent-mode -1)
321 (org-indent-mode 1)))
323 (provide 'org-indent)
325 ;;; org-indent.el ends here