org-indent.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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-list-item-body-column "org-list" (item))
  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 20
  45. "Maximum added level through virtual indentation, in
  46. characters.
  47. It is computed by multiplying `org-indent-indentation-per-level'
  48. minus one by actual level of the headline minus one.")
  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
  56. "Used locally.")
  57. (defvar org-indent-deleted-headline-flag nil
  58. "Non nil if the last deletion acted on an headline.
  59. It is modified by `org-indent-notify-deleted-headline'.")
  60. (defcustom org-indent-boundary-char ?\ ; comment to protect space char
  61. "The end of the virtual indentation strings, a single-character string.
  62. The default is just a space, but if you wish, you can use \"|\" or so.
  63. This can be useful on a terminal window - under a windowing system,
  64. it may be prettier to customize the org-indent face."
  65. :group 'org-indent
  66. :set (lambda (var val)
  67. (set var val)
  68. (and org-indent-strings (org-indent-initialize)))
  69. :type 'character)
  70. (defcustom org-indent-mode-turns-off-org-adapt-indentation t
  71. "Non-nil means setting the variable `org-indent-mode' will \
  72. turn off indentation adaptation.
  73. For details see the variable `org-adapt-indentation'."
  74. :group 'org-indent
  75. :type 'boolean)
  76. (defcustom org-indent-mode-turns-on-hiding-stars t
  77. "Non-nil means setting the variable `org-indent-mode' will \
  78. turn on `org-hide-leading-stars'."
  79. :group 'org-indent
  80. :type 'boolean)
  81. (defcustom org-indent-indentation-per-level 2
  82. "Indentation per level in number of characters."
  83. :group 'org-indent
  84. :type 'integer)
  85. (defun org-indent-initialize ()
  86. "Initialize the indentation strings."
  87. (setq org-indent-strings (make-vector (1+ org-indent-max) nil))
  88. (setq org-indent-stars (make-vector (1+ org-indent-max) nil))
  89. (aset org-indent-strings 0 nil)
  90. (aset org-indent-stars 0 nil)
  91. (loop for i from 1 to org-indent-max do
  92. (aset org-indent-strings i
  93. (org-add-props
  94. (concat (make-string (1- i) ?\ )
  95. (char-to-string org-indent-boundary-char))
  96. nil 'face 'org-indent)))
  97. (loop for i from 1 to org-indent-max-levels do
  98. (aset org-indent-stars i
  99. (org-add-props (make-string i ?*)
  100. nil 'face 'org-hide))))
  101. ;;;###autoload
  102. (define-minor-mode org-indent-mode
  103. "When active, indent text according to outline structure.
  104. Internally this works by adding `line-prefix' and `wrap-prefix'
  105. properties, after each buffer modifiation, on the modified zone."
  106. nil " Ind" nil
  107. (cond
  108. ((org-bound-and-true-p org-inhibit-startup)
  109. (setq org-indent-mode nil))
  110. ((and org-indent-mode (featurep 'xemacs))
  111. (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
  112. (setq org-indent-mode nil))
  113. ((and org-indent-mode
  114. (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
  115. (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
  116. (ding)
  117. (sit-for 1)
  118. (setq org-indent-mode nil))
  119. (org-indent-mode
  120. ;; mode was turned on.
  121. (org-set-local 'indent-tabs-mode nil)
  122. (or org-indent-strings (org-indent-initialize))
  123. (org-indent-indent-buffer)
  124. (when org-indent-mode-turns-off-org-adapt-indentation
  125. (org-set-local 'org-adapt-indentation nil))
  126. (when org-indent-mode-turns-on-hiding-stars
  127. (org-set-local 'org-hide-leading-stars-before-indent-mode
  128. org-hide-leading-stars)
  129. (org-set-local 'org-hide-leading-stars t))
  130. (make-local-variable 'buffer-substring-filters)
  131. (add-to-list 'buffer-substring-filters
  132. 'org-indent-remove-properties-from-string)
  133. (org-add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
  134. (org-add-hook 'before-change-functions
  135. 'org-indent-notify-deleted-headline nil 'local)
  136. (and font-lock-mode (org-restart-font-lock)))
  137. (t
  138. ;; mode was turned off (or we refused to turn it on)
  139. (save-excursion
  140. (save-restriction
  141. (org-indent-remove-properties (point-min) (point-max))
  142. (kill-local-variable 'org-adapt-indentation)
  143. (when (boundp 'org-hide-leading-stars-before-indent-mode)
  144. (org-set-local 'org-hide-leading-stars
  145. org-hide-leading-stars-before-indent-mode))
  146. (setq buffer-substring-filters
  147. (delq 'org-indent-remove-properties-from-string
  148. buffer-substring-filters))
  149. (remove-hook 'after-change-functions 'org-indent-refresh-maybe 'local)
  150. (remove-hook 'before-change-functions
  151. 'org-indent-notify-deleted-headline 'local)
  152. (and font-lock-mode (org-restart-font-lock))
  153. (redraw-display))))))
  154. (defface org-indent
  155. (org-compatible-face nil nil)
  156. "Face for outline indentation.
  157. The default is to make it look like whitespace. But you may find it
  158. useful to make it ever so slightly different."
  159. :group 'org-faces)
  160. (defun org-indent-indent-buffer ()
  161. "Add indentation properties for the whole buffer."
  162. (interactive)
  163. (if (not (org-mode-p))
  164. (error "Buffer major mode must be Org")
  165. (message "Initializing buffer indentation. It may take a few seconds...")
  166. (org-with-wide-buffer
  167. (with-silent-modifications
  168. (org-indent-remove-properties (point-min) (point-max))
  169. (org-indent-add-properties (point-min) (point-max))))
  170. (message "Indentation of buffer initialized.")))
  171. (defsubst org-indent-remove-properties (beg end)
  172. "Remove indentations between BEG and END."
  173. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil)))
  174. (defun org-indent-remove-properties-from-string (string)
  175. "Remove indentation properties from STRING."
  176. (remove-text-properties 0 (length string)
  177. '(line-prefix nil wrap-prefix nil) string)
  178. string)
  179. (defun org-indent-add-properties (beg end)
  180. "Add indentation properties between BEG and END."
  181. (org-with-wide-buffer
  182. (goto-char beg)
  183. (beginning-of-line)
  184. ;; 1. Initialize prefix at BEG. This is done by storing two
  185. ;; variables: INLINE-PF and PF, representing respectively
  186. ;; length of current `line-prefix' when line is inside an
  187. ;; inline task or not.
  188. (let* ((case-fold-search t)
  189. (limited-re (org-get-limited-outline-regexp))
  190. (added-ind-per-lvl (1- org-indent-indentation-per-level))
  191. (pf (let ((outline-regexp limited-re))
  192. (save-excursion
  193. (and (ignore-errors (org-back-to-heading t))
  194. (looking-at org-outline-regexp)
  195. (+ (* org-indent-indentation-per-level
  196. (- (match-end 0) (match-beginning 0) 2)) 2)))))
  197. (pf-inline (and (featurep 'org-inlinetask)
  198. (org-inlinetask-in-task-p)
  199. (+ (* org-indent-indentation-per-level
  200. (1- (org-inlinetask-get-task-level))) 2)))
  201. (set-prop-and-move
  202. (function
  203. ;; Set prefix properties `line-prefix' and `wrap-prefix'
  204. ;; in current line to, respectively, length L and W and
  205. ;; move forward. If H is non-nil, `line-prefix' will be
  206. ;; starred. Assume point is at bol.
  207. (lambda (l w h)
  208. (let ((line (if h (aref org-indent-stars
  209. (min l org-indent-max-levels))
  210. (aref org-indent-strings
  211. (min l org-indent-max))))
  212. (wrap (aref org-indent-strings (min w org-indent-max))))
  213. (add-text-properties (point) (point-at-eol)
  214. `(line-prefix ,line wrap-prefix ,wrap)))
  215. (forward-line 1)))))
  216. ;; 2. For each line, set `line-prefix' and `wrap-prefix'
  217. ;; properties depending on the type of line (headline, inline
  218. ;; task, item or other).
  219. (while (< (point) end)
  220. (cond
  221. ;; Empty line: do nothing.
  222. ((eolp) (forward-line 1))
  223. ;; Headline or inline task.
  224. ((looking-at "\\*+ ")
  225. (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
  226. (line (* added-ind-per-lvl (1- nstars)))
  227. (wrap (+ line (1+ nstars))))
  228. (cond
  229. ;; Headline: new value for PF.
  230. ((looking-at limited-re)
  231. (funcall set-prop-and-move line wrap t)
  232. (setq pf wrap))
  233. ;; End of inline task: PF-INLINE is now nil.
  234. ((looking-at "\\*+ end[ \t]*$")
  235. (funcall set-prop-and-move line wrap t)
  236. (setq pf-inline nil))
  237. ;; Start of inline task. Determine if it contains text,
  238. ;; or is only one line long. Set PF-INLINE accordingly.
  239. (t (funcall set-prop-and-move line wrap t)
  240. (setq pf-inline (and (org-inlinetask-in-task-p) wrap))))))
  241. ;; List item: `wrap-prefix' is set where body starts.
  242. ((org-at-item-p)
  243. (let* ((line (or pf-inline pf 0))
  244. (wrap (+ (org-list-item-body-column (point)) line)))
  245. (funcall set-prop-and-move line wrap nil)))
  246. ;; Normal line: use PF-INLINE, PF or nil as prefixes.
  247. (t (let* ((line (or pf-inline pf 0))
  248. (wrap (+ line (org-get-indentation))))
  249. (funcall set-prop-and-move line wrap nil))))))))
  250. (defun org-indent-notify-deleted-headline (beg end)
  251. "Set `org-indent-deleted-headline-flag' depending on the current command.
  252. BEG and END are the positions of the beginning and end of the
  253. range of deleted text.
  254. This function is meant to be called by `before-change-functions'.
  255. Flag will be non-nil if command is going to delete an headline."
  256. (setq org-indent-deleted-headline-flag
  257. (and (/= beg end)
  258. (save-excursion
  259. (goto-char beg)
  260. (save-match-data
  261. (re-search-forward org-outline-regexp-bol end t))))))
  262. (defun org-indent-refresh-maybe (beg end dummy)
  263. "Refresh indentation properties in an adequate portion of buffer.
  264. BEG and END are the positions of the beginning and end of the
  265. range of inserted text. DUMMY is an unused argument.
  266. This function is meant to be called by `after-change-functions'."
  267. (when org-indent-mode
  268. (save-match-data
  269. (cond
  270. ;; An headline was deleted.
  271. (org-indent-deleted-headline-flag
  272. (setq org-indent-deleted-headline-flag nil)
  273. (let ((end (save-excursion (outline-next-heading) (point))))
  274. (org-indent-remove-properties beg end)
  275. (org-indent-add-properties beg end)))
  276. ;; An headline was inserted.
  277. ((and (/= beg end)
  278. (save-excursion
  279. (goto-char beg)
  280. (re-search-forward org-outline-regexp-bol end t)))
  281. (let ((end (save-excursion
  282. (goto-char end) (outline-next-heading) (point))))
  283. (org-indent-remove-properties beg end)
  284. (org-indent-add-properties beg end)))
  285. ;; At an headline, modifying stars.
  286. ((save-excursion (goto-char beg)
  287. (and (org-at-heading-p) (< beg (match-end 0))))
  288. (let ((beg (point-at-bol))
  289. (end (save-excursion (outline-next-heading) (point))))
  290. (org-indent-remove-properties beg end)
  291. (org-indent-add-properties beg end)))
  292. ;; Else, refresh properties of modified area.
  293. (t (org-indent-add-properties beg end))))))
  294. (provide 'org-indent)
  295. ;;; org-indent.el ends here