1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
8 ;; This file is part of GNU Emacs.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
40 (defvar org-inlinetask-min-level
)
41 (declare-function org-inlinetask-get-task-level
"org-inlinetask" ())
42 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
44 (defgroup org-indent nil
45 "Options concerning dynamic virtual outline indentation."
49 (defconst org-indent-max
40
50 "Maximum indentation in characters.")
51 (defconst org-indent-max-levels
40
52 "Maximum indentation in characters.")
54 (defvar org-indent-strings nil
55 "Vector with all indentation strings.
56 It will be set in `org-indent-initialize'.")
57 (defvar org-indent-stars nil
58 "Vector with all indentation star strings.
59 It will be set in `org-indent-initialize'.")
60 (defvar org-hide-leading-stars-before-indent-mode nil
63 (defcustom org-indent-boundary-char ?\
; comment to protect space char
64 "The end of the virtual indentation strings, a single-character string.
65 The default is just a space, but if you wish, you can use \"|\" or so.
66 This can be useful on a terminal window - under a windowing system,
67 it may be prettier to customize the org-indent face."
69 :set
(lambda (var val
)
71 (and org-indent-strings
(org-indent-initialize)))
74 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
75 "Non-nil means setting the variable `org-indent-mode' will \
76 turn off indentation adaptation.
77 For details see the variable `org-adapt-indentation'."
81 (defcustom org-indent-mode-turns-on-hiding-stars t
82 "Non-nil means setting the variable `org-indent-mode' will \
83 turn on `org-hide-leading-stars'."
87 (defcustom org-indent-indentation-per-level
2
88 "Indentation per level in number of characters."
92 (defcustom org-indent-fix-section-after-idle-time
0.2
93 "Seconds of idle time before fixing virtual indentation of section.
94 The hooking-in of virtual indentation is not yet perfect. Occasionally,
95 a change does not trigger to proper change of indentation. For this we
96 have a timer action that fixes indentation in the current section after
97 a short amount idle time. If we ever get the integration to work perfectly,
98 this variable can be set to nil to get rid of the timer."
101 (const "Do not install idle timer" nil
)
102 (number :tag
"Idle time")))
104 (defun org-indent-initialize ()
105 "Initialize the indentation strings and set the idle timer."
106 ;; We use an idle timer to "repair" the current section, because the
107 ;; redisplay seems to have some problems.
108 (unless org-indent-strings
109 (when org-indent-fix-section-after-idle-time
111 org-indent-fix-section-after-idle-time
112 t
'org-indent-refresh-section
)))
113 ;; Initialize the indentation and star vectors
114 (setq org-indent-strings
(make-vector (1+ org-indent-max
) nil
))
115 (setq org-indent-stars
(make-vector (1+ org-indent-max
) nil
))
116 (aset org-indent-strings
0 nil
)
117 (aset org-indent-stars
0 nil
)
118 (loop for i from
1 to org-indent-max do
119 (aset org-indent-strings i
121 (concat (make-string (1- i
) ?\
)
122 (char-to-string org-indent-boundary-char
))
123 nil
'face
'org-indent
)))
124 (loop for i from
1 to org-indent-max-levels do
125 (aset org-indent-stars i
126 (org-add-props (make-string i ?
*)
127 nil
'face
'org-hide
))))
130 (define-minor-mode org-indent-mode
131 "When active, indent text according to outline structure.
133 Internally this works by adding `line-prefix' properties to all non-headlines.
134 These properties are updated locally in idle time.
135 FIXME: How to update when broken?"
138 ((org-bound-and-true-p org-inhibit-startup
)
139 (setq org-indent-mode nil
))
140 ((and org-indent-mode
(featurep 'xemacs
))
141 (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
142 (setq org-indent-mode nil
))
143 ((and org-indent-mode
144 (not (org-version-check "23.1.50" "Org Indent mode" :predicate
)))
145 (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
148 (setq org-indent-mode nil
))
150 ;; mode was turned on.
151 (org-set-local 'indent-tabs-mode nil
)
152 (or org-indent-strings
(org-indent-initialize))
153 (when org-indent-mode-turns-off-org-adapt-indentation
154 (org-set-local 'org-adapt-indentation nil
))
155 (when org-indent-mode-turns-on-hiding-stars
156 (org-set-local 'org-hide-leading-stars-before-indent-mode
157 org-hide-leading-stars
)
158 (org-set-local 'org-hide-leading-stars t
))
159 (make-local-variable 'buffer-substring-filters
)
160 (add-to-list 'buffer-substring-filters
161 'org-indent-remove-properties-from-string
)
162 (org-add-hook 'org-after-demote-entry-hook
163 'org-indent-refresh-section nil
'local
)
164 (org-add-hook 'org-after-promote-entry-hook
165 'org-indent-refresh-section nil
'local
)
166 (org-add-hook 'org-font-lock-hook
167 'org-indent-refresh-to nil
'local
)
168 (and font-lock-mode
(org-restart-font-lock))
171 ;; mode was turned off (or we refused to turn it on)
174 (org-indent-remove-properties (point-min) (point-max))
175 (kill-local-variable 'org-adapt-indentation
)
176 (when (boundp 'org-hide-leading-stars-before-indent-mode
)
177 (org-set-local 'org-hide-leading-stars
178 org-hide-leading-stars-before-indent-mode
))
179 (setq buffer-substring-filters
180 (delq 'org-indent-remove-properties-from-string
181 buffer-substring-filters
))
182 (remove-hook 'org-after-promote-entry-hook
183 'org-indent-refresh-section
'local
)
184 (remove-hook 'org-after-demote-entry-hook
185 'org-indent-refresh-section
'local
)
186 (and font-lock-mode
(org-restart-font-lock))
187 (redraw-display))))))
191 (org-compatible-face nil nil
)
192 "Face for outline indentation.
193 The default is to make it look like whitespace. But you may find it
194 useful to make it ever so slightly different."
197 (defun org-indent-indent-buffer ()
198 "Add indentation properties for the whole buffer."
200 (when org-indent-mode
204 (org-indent-remove-properties (point-min) (point-max))
205 (org-indent-add-properties (point-min) (point-max))))))
207 (defun org-indent-remove-properties (beg end
)
208 "Remove indentations between BEG and END."
209 (let ((inhibit-modification-hooks t
))
210 (with-silent-modifications
211 (remove-text-properties beg end
'(line-prefix nil wrap-prefix nil
)))))
213 (defun org-indent-remove-properties-from-string (string)
214 "Remove indentation properties from STRING."
215 (remove-text-properties 0 (length string
)
216 '(line-prefix nil wrap-prefix nil
) string
)
219 (defvar org-indent-outline-re org-outline-regexp-bol
220 "Outline heading regexp.")
222 (defun org-indent-add-properties (beg end
)
223 "Add indentation properties between BEG and END.
224 Assumes that BEG is at the beginning of a line."
225 (let* ((inhibit-modification-hooks t
)
226 (inlinetaskp (featurep 'org-inlinetask
))
227 (get-real-level (lambda (pos lvl
)
230 (if (and inlinetaskp
(org-inlinetask-in-task-p))
231 (org-inlinetask-get-task-level)
238 (with-silent-modifications
243 (if (not (re-search-forward org-indent-outline-re nil t
))
244 (setq e
(point-max) exit t
)
245 (setq e
(match-beginning 0))
246 (if (>= e end
) (setq exit t
))
247 (unless (and inlinetaskp
(org-inlinetask-in-task-p))
248 (setq level
(- (match-end 0) (match-beginning 0) 1)))
249 (setq nstars
(* (1- (funcall get-real-level e level
))
250 (1- org-indent-indentation-per-level
)))
252 (point-at-bol) (point-at-eol)
254 (aref org-indent-stars nstars
)
256 (aref org-indent-strings
257 (* (funcall get-real-level e level
)
258 org-indent-indentation-per-level
)))))
261 b e
(list 'line-prefix
(aref org-indent-strings n
)
262 'wrap-prefix
(aref org-indent-strings n
))))
263 (setq b
(1+ (point-at-eol))
264 n
(* (funcall get-real-level b level
)
265 org-indent-indentation-per-level
)))))))
267 (defvar org-inlinetask-min-level
)
268 (defun org-indent-refresh-section ()
269 "Refresh indentation properties in the current outline section.
270 Point is assumed to be at the beginning of a headline."
272 (when org-indent-mode
275 (when (ignore-errors (let ((org-outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
276 (if (featurep 'org-inlinetask
)
277 (1- org-inlinetask-min-level
)
279 (org-back-to-heading)))
281 (setq end
(or (save-excursion (or (outline-next-heading) (point)))))
282 (org-indent-remove-properties beg end
)
283 (org-indent-add-properties beg end
))))))
285 (defun org-indent-refresh-to (limit)
286 "Refresh indentation properties in the current outline section.
287 Point is assumed to be at the beginning of a headline."
289 (when org-indent-mode
290 (let ((beg (point)) (end limit
))
292 (and (ignore-errors (let ((org-outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
293 (if (featurep 'org-inlinetask
)
294 (1- org-inlinetask-min-level
)
296 (org-back-to-heading)))
298 (org-indent-remove-properties beg end
)
299 (org-indent-add-properties beg end
)))
302 (defun org-indent-refresh-subtree ()
303 "Refresh indentation properties in the current outline subtree.
304 Point is assumed to be at the beginning of a headline."
306 (when org-indent-mode
310 (setq end
(save-excursion (org-end-of-subtree t t
)))
311 (org-indent-remove-properties beg end
)
312 (org-indent-add-properties beg end
)))))
314 (defun org-indent-refresh-buffer ()
315 "Refresh indentation properties in the current outline subtree.
316 Point is assumed to be at the beginning of a headline."
318 (when org-indent-mode
320 (org-indent-mode 1)))
322 (provide 'org-indent
)
324 ;;; org-indent.el ends here