1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2008 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 ;; This file 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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This is an implementation of dynamic virtual indentation. It works
30 ;; by adding text properties to a buffer to make sure lines are
31 ;; indented according to outline structure.
40 (defgroup org-indent nil
41 "Options concerning dynamic virtual outline indentation."
45 (defconst org-indent-max
40
46 "Maximum indentation in characters")
47 (defconst org-indent-max-levels
40
48 "Maximum indentation in characters")
50 (defvar org-indent-strings nil
51 "Vector with all indentation strings.
52 It will be set in `org-indent-initialize'.")
53 (defvar org-indent-stars nil
54 "Vector with all indentation star strings.
55 It will be set in `org-indent-initialize'.")
57 (defcustom org-indent-boundary-char ?\
; comment to protect space char
58 "The end of the virtual indentation strings, a single-character string.
59 The default is just a space, but if you wish, you can use \"|\" or so.
60 This can be useful on a terminal window - under a windowing system,
61 it may be prettier to customize the org-indent face."
63 :set
(lambda (var val
)
65 (and org-indent-strings
(org-indent-initialize)))
68 (defcustom org-indent-mode-turns-off-org-adapt-indentation t
69 "Non-nil means, turning on org-indent-mode turns off indentation adaptation.
70 For details see the variable `org-adapt-indentation'."
74 (defcustom org-indent-mode-turns-on-hiding-stars t
75 "Non-nil means, turning on org-indent-mode turns on `org-hide-leading-stars'."
79 (defcustom org-indent-indentation-per-level
2
80 "Indentation per level in number of characters."
84 (defcustom org-indent-fix-section-after-idle-time
0.2
85 "Seconds of idle time before fixing virtual indentation of section.
86 The hooking-in of virtual indentation is not yet perfect. Occasionally,
87 a change does not trigger to proper change of indentation. For this we
88 have a timer action that fixes indentation in the current section after
89 a short amount idle time. If we ever get the integration to work perfectly,
90 this variable can be set to nil to get rid of the timer."
93 (const "Do not install idle timer" nil
)
94 (number :tag
"Idle time")))
96 (defun org-indent-initialize ()
97 "Initialize the indentation strings and set the idle timer."
98 ;; We use an idle timer to "repair" the current section, because the
99 ;; redisplay seems to have some problems.
100 (unless org-indent-strings
101 (when org-indent-fix-section-after-idle-time
103 org-indent-fix-section-after-idle-time
104 t
'org-indent-refresh-section
)))
105 ;; Initialize the indentation and star vectors
106 (setq org-indent-strings
(make-vector (1+ org-indent-max
) nil
))
107 (setq org-indent-stars
(make-vector (1+ org-indent-max
) nil
))
108 (aset org-indent-strings
0 "")
109 (aset org-indent-stars
0 "")
110 (loop for i from
1 to org-indent-max do
111 (aset org-indent-strings i
113 (concat (make-string (1- i
) ?\
)
114 (char-to-string org-indent-boundary-char
))
115 nil
'face
'org-indent
)))
116 (loop for i from
1 to org-indent-max-levels do
117 (aset org-indent-stars i
118 (org-add-props (make-string i ?
*)
119 nil
'face
'org-hide
))))
122 (define-minor-mode org-indent-mode
123 "When active, indent text according to outline structure.
125 Internally this works by adding `line-prefix' properties to all non-headlines.
126 These properties are updated locally in idle time.
127 FIXME: How to update when broken?"
129 (if (org-bound-and-true-p org-inhibit-startup
)
130 (setq org-indent-mode nil
)
133 (or org-indent-strings
(org-indent-initialize))
134 (when org-indent-mode-turns-off-org-adapt-indentation
135 (org-set-local 'org-adapt-indentation nil
))
136 (when org-indent-mode-turns-on-hiding-stars
137 (org-set-local 'org-hide-leading-stars t
))
138 (make-local-variable 'buffer-substring-filters
)
139 (add-to-list 'buffer-substring-filters
140 'org-indent-remove-properties-from-string
)
141 (org-add-hook 'org-after-demote-entry-hook
142 'org-indent-refresh-section nil
'local
)
143 (org-add-hook 'org-after-promote-entry-hook
144 'org-indent-refresh-section nil
'local
)
145 (org-add-hook 'org-font-lock-hook
146 'org-indent-refresh-to nil
'local
)
147 (and font-lock-mode
(org-restart-font-lock))
151 (org-indent-remove-properties (point-min) (point-max))
152 (kill-local-variable 'org-adapt-indentation
)
153 (setq buffer-substring-filters
154 (delq 'org-indent-remove-properties-from-string
155 buffer-substring-filters
))
156 (remove-hook 'org-after-promote-entry-hook
157 'org-indent-refresh-section
'local
)
158 (remove-hook 'org-after-demote-entry-hook
159 'org-indent-refresh-section
'local
)
160 (and font-lock-mode
(org-restart-font-lock))
161 (redraw-display))))))
165 (org-compatible-face nil nil
)
166 "Face for outline indentation.
167 The default is to make it look like whitespace. But you may find it
168 useful to make it evver so slightly different."
171 (defun org-indent-indent-buffer ()
172 "Add indentation properties for the whole buffer."
174 (when org-indent-mode
178 (org-indent-remove-properties (point-min) (point-max))
179 (org-indent-add-properties (point-min) (point-max))))))
181 (defun org-indent-remove-properties (beg end
)
182 "Remove indentations between BEG and END."
184 (remove-text-properties beg end
'(line-prefix nil wrap-prefix nil
))))
186 (defun org-indent-remove-properties-from-string (string)
187 "Remove indentations between BEG and END."
188 (remove-text-properties 0 (length string
)
189 '(line-prefix nil wrap-prefix nil
) string
)
192 (defun org-indent-add-properties (beg end
)
193 "Add indentation properties between BEG and END.
194 Assumes that BEG is at the beginning of a line."
195 (when (or t org-indent-mode
)
196 (let (ov b e n level exit nstars
)
202 (if (not (re-search-forward org-outline-regexp nil t
))
203 (setq e
(point-max) exit t
)
204 (setq e
(match-beginning 0))
205 (if (>= e end
) (setq exit t
))
206 (setq level
(- (match-end 0) (match-beginning 0) 1))
207 (setq nstars
(- (* (1- level
) org-indent-indentation-per-level
)
210 (point-at-bol) (point-at-eol)
212 (aref org-indent-stars nstars
)
214 (aref org-indent-strings
215 (* level org-indent-indentation-per-level
)))))
216 (when (and b
(> e b
))
218 b e
(list 'line-prefix
(aref org-indent-strings n
)
219 'wrap-prefix
(aref org-indent-strings n
))))
220 (setq b
(1+ (point-at-eol))
221 n
(* level org-indent-indentation-per-level
))))))))
223 (defun org-indent-refresh-section ()
224 "Refresh indentation properties in the current outline section.
225 Point is assumed to be at the beginning of a headline."
227 (when org-indent-mode
230 (when (ignore-errors (org-back-to-heading))
232 (setq end
(or (save-excursion (or (outline-next-heading) (point)))))
233 (org-indent-remove-properties beg end
)
234 (org-indent-add-properties beg end
))))))
236 (defun org-indent-refresh-to (limit)
237 "Refresh indentation properties in the current outline section.
238 Point is assumed to be at the beginning of a headline."
240 (when org-indent-mode
241 (let ((beg (point)) (end limit
))
243 (and (ignore-errors (org-back-to-heading t
))
245 (org-indent-remove-properties beg end
)
246 (org-indent-add-properties beg end
)))
249 (defun org-indent-refresh-subtree ()
250 "Refresh indentation properties in the current outline subtree.
251 Point is assumed to be at the beginning of a headline."
253 (when org-indent-mode
257 (setq end
(save-excursion (org-end-of-subtree t t
)))
258 (org-indent-remove-properties beg end
)
259 (org-indent-add-properties beg end
)))))
261 (defun org-indent-refresh-buffer ()
262 "Refresh indentation properties in the current outline subtree.
263 Point is assumed to be at the beginning of a headline."
265 (when org-indent-mode
267 (org-indent-mode 1)))
269 (provide 'org-indent
)
271 ;;; org-indent.el ends here