1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
39 (defgroup org-indent nil
40 "Options concerning dynamic virtual outline indentation."
44 (defconst org-indent-max
40
45 "Maximum indentation in characters")
46 (defconst org-indent-max-levels
40
47 "Maximum indentation in characters")
49 (defvar org-indent-strings nil
50 "Vector with all indentation strings.
51 It will be set in `org-indent-initialize'.")
52 (defvar org-indent-stars nil
53 "Vector with all indentation star strings.
54 It will be set in `org-indent-initialize'.")
55 (defvar org-hide-leading-stars-before-indent-mode nil
58 (defcustom org-indent-boundary-char ?\
; comment to protect space char
59 "The end of the virtual indentation strings, a single-character string.
60 The default is just a space, but if you wish, you can use \"|\" or so.
61 This can be useful on a terminal window - under a windowing system,
62 it may be prettier to customize the org-indent face."
64 :set
(lambda (var val
)
66 (and org-indent-strings
(org-indent-initialize)))
69 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
70 "Non-nil means, turning on org-indent-mode turns off indentation adaptation.
71 For details see the variable `org-adapt-indentation'."
75 (defcustom org-indent-mode-turns-on-hiding-stars t
76 "Non-nil means, turning on org-indent-mode turns on `org-hide-leading-stars'."
80 (defcustom org-indent-indentation-per-level
2
81 "Indentation per level in number of characters."
85 (defcustom org-indent-fix-section-after-idle-time
0.2
86 "Seconds of idle time before fixing virtual indentation of section.
87 The hooking-in of virtual indentation is not yet perfect. Occasionally,
88 a change does not trigger to proper change of indentation. For this we
89 have a timer action that fixes indentation in the current section after
90 a short amount idle time. If we ever get the integration to work perfectly,
91 this variable can be set to nil to get rid of the timer."
94 (const "Do not install idle timer" nil
)
95 (number :tag
"Idle time")))
97 (defun org-indent-initialize ()
98 "Initialize the indentation strings and set the idle timer."
99 ;; We use an idle timer to "repair" the current section, because the
100 ;; redisplay seems to have some problems.
101 (unless org-indent-strings
102 (when org-indent-fix-section-after-idle-time
104 org-indent-fix-section-after-idle-time
105 t
'org-indent-refresh-section
)))
106 ;; Initialize the indentation and star vectors
107 (setq org-indent-strings
(make-vector (1+ org-indent-max
) nil
))
108 (setq org-indent-stars
(make-vector (1+ org-indent-max
) nil
))
109 (aset org-indent-strings
0 nil
)
110 (aset org-indent-stars
0 nil
)
111 (loop for i from
1 to org-indent-max do
112 (aset org-indent-strings i
114 (concat (make-string (1- i
) ?\
)
115 (char-to-string org-indent-boundary-char
))
116 nil
'face
'org-indent
)))
117 (loop for i from
1 to org-indent-max-levels do
118 (aset org-indent-stars i
119 (org-add-props (make-string i ?
*)
120 nil
'face
'org-hide
))))
123 (define-minor-mode org-indent-mode
124 "When active, indent text according to outline structure.
126 Internally this works by adding `line-prefix' properties to all non-headlines.
127 These properties are updated locally in idle time.
128 FIXME: How to update when broken?"
130 (if (org-bound-and-true-p org-inhibit-startup
)
131 (setq org-indent-mode nil
)
134 (or org-indent-strings
(org-indent-initialize))
135 (when org-indent-mode-turns-off-org-adapt-indentation
136 (org-set-local 'org-adapt-indentation nil
))
137 (when org-indent-mode-turns-on-hiding-stars
138 (org-set-local 'org-hide-leading-stars-before-indent-mode
139 org-hide-leading-stars
)
140 (org-set-local 'org-hide-leading-stars t
))
141 (make-local-variable 'buffer-substring-filters
)
142 (add-to-list 'buffer-substring-filters
143 'org-indent-remove-properties-from-string
)
144 (org-add-hook 'org-after-demote-entry-hook
145 'org-indent-refresh-section nil
'local
)
146 (org-add-hook 'org-after-promote-entry-hook
147 'org-indent-refresh-section nil
'local
)
148 (org-add-hook 'org-font-lock-hook
149 'org-indent-refresh-to nil
'local
)
150 (and font-lock-mode
(org-restart-font-lock))
154 (org-indent-remove-properties (point-min) (point-max))
155 (kill-local-variable 'org-adapt-indentation
)
156 (when (boundp 'org-hide-leading-stars-before-indent-mode
)
157 (org-set-local 'org-hide-leading-stars
158 org-hide-leading-stars-before-indent-mode
))
159 (setq buffer-substring-filters
160 (delq 'org-indent-remove-properties-from-string
161 buffer-substring-filters
))
162 (remove-hook 'org-after-promote-entry-hook
163 'org-indent-refresh-section
'local
)
164 (remove-hook 'org-after-demote-entry-hook
165 'org-indent-refresh-section
'local
)
166 (and font-lock-mode
(org-restart-font-lock))
167 (redraw-display))))))
171 (org-compatible-face nil nil
)
172 "Face for outline indentation.
173 The default is to make it look like whitespace. But you may find it
174 useful to make it ever so slightly different."
177 (defun org-indent-indent-buffer ()
178 "Add indentation properties for the whole buffer."
180 (when org-indent-mode
184 (org-indent-remove-properties (point-min) (point-max))
185 (org-indent-add-properties (point-min) (point-max))))))
187 (defun org-indent-remove-properties (beg end
)
188 "Remove indentations between BEG and END."
190 (remove-text-properties beg end
'(line-prefix nil wrap-prefix nil
))))
192 (defun org-indent-remove-properties-from-string (string)
193 "Remove indentations between BEG and END."
194 (remove-text-properties 0 (length string
)
195 '(line-prefix nil wrap-prefix nil
) string
)
198 (defvar org-indent-outline-re
(concat "^" org-outline-regexp
)
199 "Outline heading regexp.")
201 (defun org-indent-add-properties (beg end
)
202 "Add indentation properties between BEG and END.
203 Assumes that BEG is at the beginning of a line."
204 (when (or t org-indent-mode
)
205 (let (ov b e n level exit nstars
)
211 (if (not (re-search-forward org-indent-outline-re nil t
))
212 (setq e
(point-max) exit t
)
213 (setq e
(match-beginning 0))
214 (if (>= e end
) (setq exit t
))
215 (setq level
(- (match-end 0) (match-beginning 0) 1))
216 (setq nstars
(- (* (1- level
) org-indent-indentation-per-level
)
219 (point-at-bol) (point-at-eol)
221 (aref org-indent-stars nstars
)
223 (aref org-indent-strings
224 (* level org-indent-indentation-per-level
)))))
225 (when (and b
(> e b
))
227 b e
(list 'line-prefix
(aref org-indent-strings n
)
228 'wrap-prefix
(aref org-indent-strings n
))))
229 (setq b
(1+ (point-at-eol))
230 n
(* level org-indent-indentation-per-level
))))))))
232 (defun org-indent-refresh-section ()
233 "Refresh indentation properties in the current outline section.
234 Point is assumed to be at the beginning of a headline."
236 (when org-indent-mode
239 (when (ignore-errors (org-back-to-heading))
241 (setq end
(or (save-excursion (or (outline-next-heading) (point)))))
242 (org-indent-remove-properties beg end
)
243 (org-indent-add-properties beg end
))))))
245 (defun org-indent-refresh-to (limit)
246 "Refresh indentation properties in the current outline section.
247 Point is assumed to be at the beginning of a headline."
249 (when org-indent-mode
250 (let ((beg (point)) (end limit
))
252 (and (ignore-errors (org-back-to-heading t
))
254 (org-indent-remove-properties beg end
)
255 (org-indent-add-properties beg end
)))
258 (defun org-indent-refresh-subtree ()
259 "Refresh indentation properties in the current outline subtree.
260 Point is assumed to be at the beginning of a headline."
262 (when org-indent-mode
266 (setq end
(save-excursion (org-end-of-subtree t t
)))
267 (org-indent-remove-properties beg end
)
268 (org-indent-add-properties beg end
)))))
270 (defun org-indent-refresh-buffer ()
271 "Refresh indentation properties in the current outline subtree.
272 Point is assumed to be at the beginning of a headline."
274 (when org-indent-mode
276 (org-indent-mode 1)))
278 (provide 'org-indent
)
280 ;; arch-tag: b76736bc-9f4a-43cd-977c-ecfd6689846a
281 ;;; org-indent.el ends here