org-indent.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. (defvar org-inlinetask-min-level)
  36. (declare-function org-inlinetask-get-task-level "org-inlinetask" ())
  37. (declare-function org-inlinetask-in-task-p "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-section)))
  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' properties to all non-headlines.
  118. These properties are updated locally in idle time.
  119. FIXME: How to update when broken?"
  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. (when org-indent-mode-turns-off-org-adapt-indentation
  138. (org-set-local 'org-adapt-indentation nil))
  139. (when org-indent-mode-turns-on-hiding-stars
  140. (org-set-local 'org-hide-leading-stars-before-indent-mode
  141. org-hide-leading-stars)
  142. (org-set-local 'org-hide-leading-stars t))
  143. (make-local-variable 'buffer-substring-filters)
  144. (add-to-list 'buffer-substring-filters
  145. 'org-indent-remove-properties-from-string)
  146. (org-add-hook 'org-after-demote-entry-hook
  147. 'org-indent-refresh-section nil 'local)
  148. (org-add-hook 'org-after-promote-entry-hook
  149. 'org-indent-refresh-section nil 'local)
  150. (org-add-hook 'org-font-lock-hook
  151. 'org-indent-refresh-to nil 'local)
  152. (and font-lock-mode (org-restart-font-lock))
  153. )
  154. (t
  155. ;; mode was turned off (or we refused to turn it on)
  156. (save-excursion
  157. (save-restriction
  158. (org-indent-remove-properties (point-min) (point-max))
  159. (kill-local-variable 'org-adapt-indentation)
  160. (when (boundp 'org-hide-leading-stars-before-indent-mode)
  161. (org-set-local 'org-hide-leading-stars
  162. org-hide-leading-stars-before-indent-mode))
  163. (setq buffer-substring-filters
  164. (delq 'org-indent-remove-properties-from-string
  165. buffer-substring-filters))
  166. (remove-hook 'org-after-promote-entry-hook
  167. 'org-indent-refresh-section 'local)
  168. (remove-hook 'org-after-demote-entry-hook
  169. 'org-indent-refresh-section 'local)
  170. (and font-lock-mode (org-restart-font-lock))
  171. (redraw-display))))))
  172. (defface org-indent
  173. (org-compatible-face nil nil)
  174. "Face for outline indentation.
  175. The default is to make it look like whitespace. But you may find it
  176. useful to make it ever so slightly different."
  177. :group 'org-faces)
  178. (defun org-indent-indent-buffer ()
  179. "Add indentation properties for the whole buffer."
  180. (interactive)
  181. (when org-indent-mode
  182. (save-excursion
  183. (save-restriction
  184. (widen)
  185. (org-indent-remove-properties (point-min) (point-max))
  186. (org-indent-add-properties (point-min) (point-max))))))
  187. (defun org-indent-remove-properties (beg end)
  188. "Remove indentations between BEG and END."
  189. (let ((inhibit-modification-hooks t))
  190. (with-silent-modifications
  191. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil)))))
  192. (defun org-indent-remove-properties-from-string (string)
  193. "Remove indentation properties from STRING."
  194. (remove-text-properties 0 (length string)
  195. '(line-prefix nil wrap-prefix nil) string)
  196. string)
  197. (defvar org-indent-outline-re org-outline-regexp-bol
  198. "Outline heading regexp.")
  199. (defun org-indent-add-properties (beg end)
  200. "Add indentation properties between BEG and END.
  201. Assumes that BEG is at the beginning of a line."
  202. (let* ((inhibit-modification-hooks t)
  203. (inlinetaskp (featurep 'org-inlinetask))
  204. (get-real-level (lambda (pos lvl)
  205. (save-excursion
  206. (goto-char pos)
  207. (if (and inlinetaskp (org-inlinetask-in-task-p))
  208. (org-inlinetask-get-task-level)
  209. lvl))))
  210. (b beg)
  211. (e end)
  212. (level 0)
  213. (n 0)
  214. exit nstars)
  215. (with-silent-modifications
  216. (save-excursion
  217. (goto-char beg)
  218. (while (not exit)
  219. (setq e end)
  220. (if (not (re-search-forward org-indent-outline-re nil t))
  221. (setq e (point-max) exit t)
  222. (setq e (match-beginning 0))
  223. (if (>= e end) (setq exit t))
  224. (unless (and inlinetaskp (org-inlinetask-in-task-p))
  225. (setq level (- (match-end 0) (match-beginning 0) 1)))
  226. (setq nstars (* (1- (funcall get-real-level e level))
  227. (1- org-indent-indentation-per-level)))
  228. (add-text-properties
  229. (point-at-bol) (point-at-eol)
  230. (list 'line-prefix
  231. (aref org-indent-stars nstars)
  232. 'wrap-prefix
  233. (aref org-indent-strings
  234. (* (funcall get-real-level e level)
  235. org-indent-indentation-per-level)))))
  236. (when (> e b)
  237. (add-text-properties
  238. b e (list 'line-prefix (aref org-indent-strings n)
  239. 'wrap-prefix (aref org-indent-strings n))))
  240. (setq b (1+ (point-at-eol))
  241. n (* (funcall get-real-level b level)
  242. org-indent-indentation-per-level)))))))
  243. (defvar org-inlinetask-min-level)
  244. (defun org-indent-refresh-section ()
  245. "Refresh indentation properties in the current outline section.
  246. Point is assumed to be at the beginning of a headline."
  247. (interactive)
  248. (when org-indent-mode
  249. (let (beg end)
  250. (save-excursion
  251. (when (ignore-errors (let ((org-outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
  252. (if (featurep 'org-inlinetask)
  253. (1- org-inlinetask-min-level)
  254. ""))))
  255. (org-back-to-heading)))
  256. (setq beg (point))
  257. (setq end (or (save-excursion (or (outline-next-heading) (point)))))
  258. (org-indent-remove-properties beg end)
  259. (org-indent-add-properties beg end))))))
  260. (defun org-indent-refresh-to (limit)
  261. "Refresh indentation properties in the current outline section.
  262. Point is assumed to be at the beginning of a headline."
  263. (interactive)
  264. (when org-indent-mode
  265. (let ((beg (point)) (end limit))
  266. (save-excursion
  267. (and (ignore-errors (let ((org-outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
  268. (if (featurep 'org-inlinetask)
  269. (1- org-inlinetask-min-level)
  270. ""))))
  271. (org-back-to-heading)))
  272. (setq beg (point))))
  273. (org-indent-remove-properties beg end)
  274. (org-indent-add-properties beg end)))
  275. (goto-char limit))
  276. (defun org-indent-refresh-subtree ()
  277. "Refresh indentation properties in the current outline subtree.
  278. Point is assumed to be at the beginning of a headline."
  279. (interactive)
  280. (when org-indent-mode
  281. (save-excursion
  282. (let (beg end)
  283. (setq beg (point))
  284. (setq end (save-excursion (org-end-of-subtree t t)))
  285. (org-indent-remove-properties beg end)
  286. (org-indent-add-properties beg end)))))
  287. (defun org-indent-refresh-buffer ()
  288. "Refresh indentation properties in the current outline subtree.
  289. Point is assumed to be at the beginning of a headline."
  290. (interactive)
  291. (when org-indent-mode
  292. (org-indent-mode -1)
  293. (org-indent-mode 1)))
  294. (provide 'org-indent)
  295. ;;; org-indent.el ends here