org-indent.el 12 KB

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