org-indent.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  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.
  14. ;;
  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.
  19. ;;
  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/>.
  22. ;;
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;; This is an implementation of dynamic virtual indentation. It works
  27. ;; by adding text properties to a buffer to make sure lines are
  28. ;; indented according to outline structure.
  29. ;;; Code:
  30. (require 'org-macs)
  31. (require 'org-compat)
  32. (require 'org)
  33. (eval-when-compile
  34. (require 'cl))
  35. (declare-function org-inlinetask-get-task-level "org-inlinetask" ())
  36. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  37. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  38. (defgroup org-indent nil
  39. "Options concerning dynamic virtual outline indentation."
  40. :tag "Org Indent"
  41. :group 'org)
  42. (defconst org-indent-max 40
  43. "Maximum indentation in characters.")
  44. (defconst org-indent-max-levels 40
  45. "Maximum indentation in characters.")
  46. (defvar org-indent-strings nil
  47. "Vector with all indentation strings.
  48. It will be set in `org-indent-initialize'.")
  49. (defvar org-indent-stars nil
  50. "Vector with all indentation star strings.
  51. It will be set in `org-indent-initialize'.")
  52. (defvar org-hide-leading-stars-before-indent-mode nil
  53. "Used locally.")
  54. (defcustom org-indent-boundary-char ?\ ; comment to protect space char
  55. "The end of the virtual indentation strings, a single-character string.
  56. The default is just a space, but if you wish, you can use \"|\" or so.
  57. This can be useful on a terminal window - under a windowing system,
  58. it may be prettier to customize the org-indent face."
  59. :group 'org-indent
  60. :set (lambda (var val)
  61. (set var val)
  62. (and org-indent-strings (org-indent-initialize)))
  63. :type 'character)
  64. (defcustom org-indent-mode-turns-off-org-adapt-indentation t
  65. "Non-nil means setting the variable `org-indent-mode' will \
  66. turn off indentation adaptation.
  67. For details see the variable `org-adapt-indentation'."
  68. :group 'org-indent
  69. :type 'boolean)
  70. (defcustom org-indent-mode-turns-on-hiding-stars t
  71. "Non-nil means setting the variable `org-indent-mode' will \
  72. turn on `org-hide-leading-stars'."
  73. :group 'org-indent
  74. :type 'boolean)
  75. (defcustom org-indent-indentation-per-level 2
  76. "Indentation per level in number of characters."
  77. :group 'org-indent
  78. :type 'integer)
  79. (defcustom org-indent-fix-section-after-idle-time 0.2
  80. "Seconds of idle time before fixing virtual indentation of section.
  81. The hooking-in of virtual indentation is not yet perfect. Occasionally,
  82. a change does not trigger to proper change of indentation. For this we
  83. have a timer action that fixes indentation in the current section after
  84. a short amount idle time. If we ever get the integration to work perfectly,
  85. this variable can be set to nil to get rid of the timer."
  86. :group 'org-indent
  87. :type '(choice
  88. (const "Do not install idle timer" nil)
  89. (number :tag "Idle time")))
  90. (defun org-indent-initialize ()
  91. "Initialize the indentation strings and set the idle timer."
  92. ;; We use an idle timer to "repair" the current section, because the
  93. ;; redisplay seems to have some problems.
  94. (unless org-indent-strings
  95. (when org-indent-fix-section-after-idle-time
  96. (run-with-idle-timer
  97. org-indent-fix-section-after-idle-time
  98. t 'org-indent-refresh-view)))
  99. ;; Initialize the indentation and star vectors
  100. (setq org-indent-strings (make-vector (1+ org-indent-max) nil))
  101. (setq org-indent-stars (make-vector (1+ org-indent-max) nil))
  102. (aset org-indent-strings 0 nil)
  103. (aset org-indent-stars 0 nil)
  104. (loop for i from 1 to org-indent-max do
  105. (aset org-indent-strings i
  106. (org-add-props
  107. (concat (make-string (1- i) ?\ )
  108. (char-to-string org-indent-boundary-char))
  109. nil 'face 'org-indent)))
  110. (loop for i from 1 to org-indent-max-levels do
  111. (aset org-indent-stars i
  112. (org-add-props (make-string i ?*)
  113. nil 'face 'org-hide))))
  114. ;;;###autoload
  115. (define-minor-mode org-indent-mode
  116. "When active, indent text according to outline structure.
  117. Internally this works by adding `line-prefix' and `wrap-prefix'
  118. properties to all lines. These properties are updated locally in idle
  119. time."
  120. nil " Ind" nil
  121. (cond
  122. ((org-bound-and-true-p org-inhibit-startup)
  123. (setq org-indent-mode nil))
  124. ((and org-indent-mode (featurep 'xemacs))
  125. (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
  126. (setq org-indent-mode nil))
  127. ((and org-indent-mode
  128. (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
  129. (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
  130. (ding)
  131. (sit-for 1)
  132. (setq org-indent-mode nil))
  133. (org-indent-mode
  134. ;; mode was turned on.
  135. (org-set-local 'indent-tabs-mode nil)
  136. (or org-indent-strings (org-indent-initialize))
  137. (org-indent-indent-buffer)
  138. (when org-indent-mode-turns-off-org-adapt-indentation
  139. (org-set-local 'org-adapt-indentation nil))
  140. (when org-indent-mode-turns-on-hiding-stars
  141. (org-set-local 'org-hide-leading-stars-before-indent-mode
  142. org-hide-leading-stars)
  143. (org-set-local 'org-hide-leading-stars t))
  144. (make-local-variable 'buffer-substring-filters)
  145. (add-to-list 'buffer-substring-filters
  146. 'org-indent-remove-properties-from-string)
  147. (org-add-hook 'org-after-demote-entry-hook
  148. 'org-indent-refresh-subtree nil 'local)
  149. (org-add-hook 'org-after-promote-entry-hook
  150. 'org-indent-refresh-subtree nil 'local)
  151. (and font-lock-mode (org-restart-font-lock)))
  152. (t
  153. ;; mode was turned off (or we refused to turn it on)
  154. (save-excursion
  155. (save-restriction
  156. (org-indent-remove-properties (point-min) (point-max))
  157. (kill-local-variable 'org-adapt-indentation)
  158. (when (boundp 'org-hide-leading-stars-before-indent-mode)
  159. (org-set-local 'org-hide-leading-stars
  160. org-hide-leading-stars-before-indent-mode))
  161. (setq buffer-substring-filters
  162. (delq 'org-indent-remove-properties-from-string
  163. buffer-substring-filters))
  164. (remove-hook 'org-after-promote-entry-hook
  165. 'org-indent-refresh-subtree 'local)
  166. (remove-hook 'org-after-demote-entry-hook
  167. 'org-indent-refresh-subtree 'local)
  168. (and font-lock-mode (org-restart-font-lock))
  169. (redraw-display))))))
  170. (defface org-indent
  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."
  175. :group 'org-faces)
  176. (defun org-indent-indent-buffer ()
  177. "Add indentation properties for the whole buffer."
  178. (interactive)
  179. (when org-indent-mode
  180. (save-excursion
  181. (save-restriction
  182. (widen)
  183. (org-indent-remove-properties (point-min) (point-max))
  184. (org-indent-add-properties (point-min) (point-max))))))
  185. (defun org-indent-remove-properties (beg end)
  186. "Remove indentations between BEG and END."
  187. (let ((inhibit-modification-hooks t))
  188. (with-silent-modifications
  189. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil)))))
  190. (defun org-indent-remove-properties-from-string (string)
  191. "Remove indentation properties from STRING."
  192. (remove-text-properties 0 (length string)
  193. '(line-prefix nil wrap-prefix nil) string)
  194. string)
  195. (defun org-indent-add-properties (beg end)
  196. "Add indentation properties between BEG and END."
  197. (save-excursion
  198. (goto-char beg)
  199. (beginning-of-line)
  200. ;; 1. Initialize prefix at BEG. This is done by storing two
  201. ;; variables: INLINE-PF and PF, representing respectively
  202. ;; current `line-prefix' when line is inside an inline task or
  203. ;; not.
  204. (let* ((inhibit-modification-hooks t)
  205. (case-fold-search t)
  206. (limited-re (org-get-limited-outline-regexp))
  207. (inline-end-re (and (featurep 'org-inlinetask)
  208. (concat (org-inlinetask-outline-regexp)
  209. "end[ \t]*$")))
  210. (pf (org-with-limited-levels
  211. (save-excursion
  212. (and (ignore-errors (org-back-to-heading t))
  213. (looking-at org-outline-regexp)
  214. (aref org-indent-strings
  215. (- (match-end 0) (match-beginning 0)))))))
  216. (pf-inline (and inline-end-re
  217. (org-inlinetask-in-task-p)
  218. (aref org-indent-strings
  219. (1+ (org-inlinetask-get-task-level))))))
  220. ;; 2. For each line, `line-prefix' is based on the value of the
  221. ;; previous `line-prefix' (stored in PF and INLINE-PF).
  222. ;; `wrap-prefix' computation is done with the current
  223. ;; `line-prefix' value.
  224. (with-silent-modifications
  225. (while (< (point) end)
  226. (cond
  227. ;; Empty line: do nothing.
  228. ((eolp) (forward-line 1))
  229. ;; List item: `line-prefix' doesn't change, but
  230. ;; `wrap-prefix' is set where body starts.
  231. ((org-at-item-p)
  232. (let* ((line (or pf-inline pf))
  233. (wrap (aref org-indent-strings
  234. (+ (org-list-item-body-column (point))
  235. (length line)))))
  236. (add-text-properties (point) (point-at-eol)
  237. `(line-prefix ,line wrap-prefix ,wrap))
  238. (forward-line 1)))
  239. ;; Normal line: `line-prefix' doesn't change, but
  240. ;; `wrap-prefix' also takes into account indentation.
  241. ((not (looking-at org-outline-regexp))
  242. (let* ((line (or pf-inline pf))
  243. (wrap (aref org-indent-strings
  244. (+ (length line) (org-get-indentation)))))
  245. (add-text-properties (point) (point-at-eol)
  246. `(line-prefix ,line wrap-prefix ,wrap))
  247. (forward-line 1)))
  248. ;; Headline: `line-prefix' is nil, `wrap-prefix' is set
  249. ;; where headline starts and its value becomes a reference
  250. ;; for following lines.
  251. ((looking-at limited-re)
  252. (let ((wrap (aref org-indent-strings
  253. (- (match-end 0) (match-beginning 0)))))
  254. (add-text-properties (point) (point-at-eol)
  255. `(line-prefix nil wrap-prefix ,wrap))
  256. (setq pf wrap)
  257. (forward-line 1)))
  258. ;; End of inline task: both `line-prefix' and `wrap-prefix'
  259. ;; are nil. PF-INLINE is also nil, as following lines are
  260. ;; out of the inline task.
  261. ((looking-at inline-end-re)
  262. (add-text-properties (point) (point-at-eol)
  263. '(line-prefix nil wrap-prefix nil))
  264. (setq pf-inline nil)
  265. (forward-line 1))
  266. ;; Beginnig of inline task: determine if the tasks contains
  267. ;; text (and set PF-INLINE accordingly) or is only one line
  268. ;; long by looking the status of the following line. In any
  269. ;; case, `line-prefix' is nil and `wrap-prefix' is set
  270. ;; where headline starts.
  271. (t
  272. (let ((wrap (progn
  273. (looking-at org-outline-regexp)
  274. (aref org-indent-strings
  275. (- (match-end 0) (match-beginning 0))))))
  276. (add-text-properties (point) (point-at-eol)
  277. `(line-prefix nil wrap-prefix ,wrap))
  278. (forward-line 1)
  279. (setq pf-inline (and (not (eobp))
  280. (org-inlinetask-in-task-p)
  281. wrap))))))))))
  282. (defun org-indent-refresh-view (&rest ignore)
  283. "Refresh indentation properties in the visible portion of buffer.
  284. IGNORE all arguments that might be passed to the function."
  285. (interactive)
  286. (when org-indent-mode
  287. (save-excursion
  288. (let ((beg (window-start))
  289. (end (window-end nil t)))
  290. (org-indent-add-properties beg end)))))
  291. (defun org-indent-refresh-subtree ()
  292. "Refresh indentation properties in the current outline subtree.
  293. Point is assumed to be at an headline."
  294. (interactive)
  295. (when org-indent-mode
  296. (save-excursion
  297. (let ((beg (point-at-bol))
  298. (end (save-excursion (org-end-of-subtree t t))))
  299. (org-indent-add-properties beg end)))))
  300. (defun org-indent-refresh-buffer ()
  301. "Refresh indentation properties in the whole buffer."
  302. (interactive)
  303. (when org-indent-mode
  304. (org-indent-mode -1)
  305. (org-indent-mode 1)))
  306. (provide 'org-indent)
  307. ;;; org-indent.el ends here