New minor mode org-indent-mode
[org-mode.git] / lisp / org-indent.el
blobcaeb65dfa88ad55cb10ce0044518700147916383
1 ;;; org-indent.el --- Dynamic indentation for Org-mode
2 ;; Copyright (C) 2008 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: 6.28trans
8 ;;
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)
14 ;; 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; 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
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.
33 (require 'org-macs)
34 (require 'org-compat)
35 (require 'org)
36 (eval-when-compile
37 (require 'cl))
40 (defgroup org-indent nil
41 "Options concerning dynamic virtual outline indentation."
42 :tag "Org Structure"
43 :group 'org)
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."
62 :group 'org-indent
63 :set (lambda (var val)
64 (set var val)
65 (and org-indent-strings (org-indent-initialize)))
66 :type 'character)
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'."
71 :group 'org-indent
72 :type 'boolean)
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'."
76 :group 'org-indent
77 :type 'boolean)
79 (defcustom org-indent-indentation-per-level 2
80 "Indentation per level in number of characters."
81 :group 'org-indent
82 :type 'integer)
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."
91 :group 'org-indent
92 :type '(choice
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
102 (run-with-idle-timer
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
112 (org-add-props
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))))
121 ;;;###autoload
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?"
128 nil " Ind" nil
129 (if (org-bound-and-true-p org-inhibit-startup)
130 (setq org-indent-mode nil)
131 (if org-indent-mode
132 (progn
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))
149 (save-excursion
150 (save-restriction
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))))))
164 (defface org-indent
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."
169 :group 'org-faces)
171 (defun org-indent-indent-buffer ()
172 "Add indentation properties for the whole buffer."
173 (interactive)
174 (when org-indent-mode
175 (save-excursion
176 (save-restriction
177 (widen)
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."
183 (org-unmodified
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)
190 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)
197 (org-unmodified
198 (save-excursion
199 (goto-char beg)
200 (while (not exit)
201 (setq e end)
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)
208 (1- level)))
209 (add-text-properties
210 (point-at-bol) (point-at-eol)
211 (list 'line-prefix
212 (aref org-indent-stars nstars)
213 'wrap-prefix
214 (aref org-indent-strings
215 (* level org-indent-indentation-per-level)))))
216 (when (and b (> e b))
217 (add-text-properties
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."
226 (interactive)
227 (when org-indent-mode
228 (let (beg end)
229 (save-excursion
230 (when (ignore-errors (org-back-to-heading))
231 (setq beg (point))
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."
239 (interactive)
240 (when org-indent-mode
241 (let ((beg (point)) (end limit))
242 (save-excursion
243 (and (ignore-errors (org-back-to-heading t))
244 (setq beg (point))))
245 (org-indent-remove-properties beg end)
246 (org-indent-add-properties beg end)))
247 (goto-char limit))
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."
252 (interactive)
253 (when org-indent-mode
254 (save-excursion
255 (let (beg end)
256 (setq beg (point))
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."
264 (interactive)
265 (when org-indent-mode
266 (org-indent-mode -1)
267 (org-indent-mode 1)))
269 (provide 'org-indent)
271 ;;; org-indent.el ends here