org-indent.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ;;; org-indent.el --- Dynamic indentation for Org-mode
  2. ;; Copyright (C) 2009, 2010 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: 7.5
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs 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 of the License, or
  14. ;; (at your option) any later version.
  15. ;;
  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.
  20. ;;
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This is an implementation of dynamic virtual indentation. It works
  28. ;; by adding text properties to a buffer to make sure lines are
  29. ;; indented according to outline structure.
  30. ;;; Code:
  31. (require 'org-macs)
  32. (require 'org-compat)
  33. (require 'org)
  34. (eval-when-compile
  35. (require 'cl))
  36. (defvar org-inlinetask-min-level)
  37. (declare-function org-inlinetask-get-task-level "org-inlinetask" ())
  38. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  39. (defgroup org-indent nil
  40. "Options concerning dynamic virtual outline indentation."
  41. :tag "Org Indent"
  42. :group 'org)
  43. (defconst org-indent-max 40
  44. "Maximum indentation in characters.")
  45. (defconst org-indent-max-levels 40
  46. "Maximum indentation in characters.")
  47. (defvar org-indent-strings nil
  48. "Vector with all indentation strings.
  49. It will be set in `org-indent-initialize'.")
  50. (defvar org-indent-stars nil
  51. "Vector with all indentation star strings.
  52. It will be set in `org-indent-initialize'.")
  53. (defvar org-hide-leading-stars-before-indent-mode nil
  54. "Used locally.")
  55. (defcustom org-indent-boundary-char ?\ ; comment to protect space char
  56. "The end of the virtual indentation strings, a single-character string.
  57. The default is just a space, but if you wish, you can use \"|\" or so.
  58. This can be useful on a terminal window - under a windowing system,
  59. it may be prettier to customize the org-indent face."
  60. :group 'org-indent
  61. :set (lambda (var val)
  62. (set var val)
  63. (and org-indent-strings (org-indent-initialize)))
  64. :type 'character)
  65. (defcustom org-indent-mode-turns-off-org-adapt-indentation t
  66. "Non-nil means setting the variable `org-indent-mode' will \
  67. turn off indentation adaptation.
  68. For details see the variable `org-adapt-indentation'."
  69. :group 'org-indent
  70. :type 'boolean)
  71. (defcustom org-indent-mode-turns-on-hiding-stars t
  72. "Non-nil means setting the variable `org-indent-mode' will \
  73. turn on `org-hide-leading-stars'."
  74. :group 'org-indent
  75. :type 'boolean)
  76. (defcustom org-indent-indentation-per-level 2
  77. "Indentation per level in number of characters."
  78. :group 'org-indent
  79. :type 'integer)
  80. (defcustom org-indent-fix-section-after-idle-time 0.2
  81. "Seconds of idle time before fixing virtual indentation of section.
  82. The hooking-in of virtual indentation is not yet perfect. Occasionally,
  83. a change does not trigger to proper change of indentation. For this we
  84. have a timer action that fixes indentation in the current section after
  85. a short amount idle time. If we ever get the integration to work perfectly,
  86. this variable can be set to nil to get rid of the timer."
  87. :group 'org-indent
  88. :type '(choice
  89. (const "Do not install idle timer" nil)
  90. (number :tag "Idle time")))
  91. (defun org-indent-initialize ()
  92. "Initialize the indentation strings and set the idle timer."
  93. ;; We use an idle timer to "repair" the current section, because the
  94. ;; redisplay seems to have some problems.
  95. (unless org-indent-strings
  96. (when org-indent-fix-section-after-idle-time
  97. (run-with-idle-timer
  98. org-indent-fix-section-after-idle-time
  99. t 'org-indent-refresh-section)))
  100. ;; Initialize the indentation and star vectors
  101. (setq org-indent-strings (make-vector (1+ org-indent-max) nil))
  102. (setq org-indent-stars (make-vector (1+ org-indent-max) nil))
  103. (aset org-indent-strings 0 nil)
  104. (aset org-indent-stars 0 nil)
  105. (loop for i from 1 to org-indent-max do
  106. (aset org-indent-strings i
  107. (org-add-props
  108. (concat (make-string (1- i) ?\ )
  109. (char-to-string org-indent-boundary-char))
  110. nil 'face 'org-indent)))
  111. (loop for i from 1 to org-indent-max-levels do
  112. (aset org-indent-stars i
  113. (org-add-props (make-string i ?*)
  114. nil 'face 'org-hide))))
  115. ;;;###autoload
  116. (define-minor-mode org-indent-mode
  117. "When active, indent text according to outline structure.
  118. Internally this works by adding `line-prefix' properties to all non-headlines.
  119. These properties are updated locally in idle time.
  120. FIXME: How to update when broken?"
  121. nil " Ind" nil
  122. (cond
  123. ((org-bound-and-true-p org-inhibit-startup)
  124. (setq org-indent-mode nil))
  125. ((and org-indent-mode (featurep 'xemacs))
  126. (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
  127. (setq org-indent-mode nil))
  128. ((and org-indent-mode
  129. (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
  130. (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
  131. (ding)
  132. (sit-for 1)
  133. (setq org-indent-mode nil))
  134. (org-indent-mode
  135. ;; mode was turned on.
  136. (org-set-local 'indent-tabs-mode nil)
  137. (or org-indent-strings (org-indent-initialize))
  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-section nil 'local)
  149. (org-add-hook 'org-after-promote-entry-hook
  150. 'org-indent-refresh-section nil 'local)
  151. (org-add-hook 'org-font-lock-hook
  152. 'org-indent-refresh-to nil 'local)
  153. (and font-lock-mode (org-restart-font-lock))
  154. )
  155. (t
  156. ;; mode was turned off (or we refused to turn it on)
  157. (save-excursion
  158. (save-restriction
  159. (org-indent-remove-properties (point-min) (point-max))
  160. (kill-local-variable 'org-adapt-indentation)
  161. (when (boundp 'org-hide-leading-stars-before-indent-mode)
  162. (org-set-local 'org-hide-leading-stars
  163. org-hide-leading-stars-before-indent-mode))
  164. (setq buffer-substring-filters
  165. (delq 'org-indent-remove-properties-from-string
  166. buffer-substring-filters))
  167. (remove-hook 'org-after-promote-entry-hook
  168. 'org-indent-refresh-section 'local)
  169. (remove-hook 'org-after-demote-entry-hook
  170. 'org-indent-refresh-section 'local)
  171. (and font-lock-mode (org-restart-font-lock))
  172. (redraw-display))))))
  173. (defface org-indent
  174. (org-compatible-face nil nil)
  175. "Face for outline indentation.
  176. The default is to make it look like whitespace. But you may find it
  177. useful to make it ever so slightly different."
  178. :group 'org-faces)
  179. (defun org-indent-indent-buffer ()
  180. "Add indentation properties for the whole buffer."
  181. (interactive)
  182. (when org-indent-mode
  183. (save-excursion
  184. (save-restriction
  185. (widen)
  186. (org-indent-remove-properties (point-min) (point-max))
  187. (org-indent-add-properties (point-min) (point-max))))))
  188. (defun org-indent-remove-properties (beg end)
  189. "Remove indentations between BEG and END."
  190. (let ((inhibit-modification-hooks t))
  191. (with-silent-modifications
  192. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil)))))
  193. (defun org-indent-remove-properties-from-string (string)
  194. "Remove indentations between BEG and END."
  195. (remove-text-properties 0 (length string)
  196. '(line-prefix nil wrap-prefix nil) string)
  197. string)
  198. (defvar org-indent-outline-re (concat "^" org-outline-regexp)
  199. "Outline heading regexp.")
  200. (defun org-indent-add-properties (beg end)
  201. "Add indentation properties between BEG and END.
  202. Assumes that BEG is at the beginning of a line."
  203. (let* ((inhibit-modification-hooks t)
  204. (inlinetaskp (featurep 'org-inlinetask))
  205. (get-real-level (lambda (pos lvl)
  206. (save-excursion
  207. (goto-char pos)
  208. (if (and inlinetaskp (org-inlinetask-in-task-p))
  209. (org-inlinetask-get-task-level)
  210. lvl))))
  211. (b beg)
  212. (e end)
  213. (level 0)
  214. (n 0)
  215. exit nstars)
  216. (with-silent-modifications
  217. (save-excursion
  218. (goto-char beg)
  219. (while (not exit)
  220. (setq e end)
  221. (if (not (re-search-forward org-indent-outline-re nil t))
  222. (setq e (point-max) exit t)
  223. (setq e (match-beginning 0))
  224. (if (>= e end) (setq exit t))
  225. (unless (and inlinetaskp (org-inlinetask-in-task-p))
  226. (setq level (- (match-end 0) (match-beginning 0) 1)))
  227. (setq nstars (* (1- (funcall get-real-level e level))
  228. (1- org-indent-indentation-per-level)))
  229. (add-text-properties
  230. (point-at-bol) (point-at-eol)
  231. (list 'line-prefix
  232. (aref org-indent-stars nstars)
  233. 'wrap-prefix
  234. (aref org-indent-strings
  235. (* (funcall get-real-level e level)
  236. org-indent-indentation-per-level)))))
  237. (when (> e b)
  238. (add-text-properties
  239. b e (list 'line-prefix (aref org-indent-strings n)
  240. 'wrap-prefix (aref org-indent-strings n))))
  241. (setq b (1+ (point-at-eol))
  242. n (* (funcall get-real-level b level)
  243. org-indent-indentation-per-level)))))))
  244. (defvar org-inlinetask-min-level)
  245. (defun org-indent-refresh-section ()
  246. "Refresh indentation properties in the current outline section.
  247. Point is assumed to be at the beginning of a headline."
  248. (interactive)
  249. (when org-indent-mode
  250. (let (beg end)
  251. (save-excursion
  252. (when (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
  253. (if (featurep 'org-inlinetask)
  254. (1- org-inlinetask-min-level)
  255. ""))))
  256. (org-back-to-heading)))
  257. (setq beg (point))
  258. (setq end (or (save-excursion (or (outline-next-heading) (point)))))
  259. (org-indent-remove-properties beg end)
  260. (org-indent-add-properties beg end))))))
  261. (defun org-indent-refresh-to (limit)
  262. "Refresh indentation properties in the current outline section.
  263. Point is assumed to be at the beginning of a headline."
  264. (interactive)
  265. (when org-indent-mode
  266. (let ((beg (point)) (end limit))
  267. (save-excursion
  268. (and (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
  269. (if (featurep 'org-inlinetask)
  270. (1- org-inlinetask-min-level)
  271. ""))))
  272. (org-back-to-heading)))
  273. (setq beg (point))))
  274. (org-indent-remove-properties beg end)
  275. (org-indent-add-properties beg end)))
  276. (goto-char limit))
  277. (defun org-indent-refresh-subtree ()
  278. "Refresh indentation properties in the current outline subtree.
  279. Point is assumed to be at the beginning of a headline."
  280. (interactive)
  281. (when org-indent-mode
  282. (save-excursion
  283. (let (beg end)
  284. (setq beg (point))
  285. (setq end (save-excursion (org-end-of-subtree t t)))
  286. (org-indent-remove-properties beg end)
  287. (org-indent-add-properties beg end)))))
  288. (defun org-indent-refresh-buffer ()
  289. "Refresh indentation properties in the current outline subtree.
  290. Point is assumed to be at the beginning of a headline."
  291. (interactive)
  292. (when org-indent-mode
  293. (org-indent-mode -1)
  294. (org-indent-mode 1)))
  295. (provide 'org-indent)
  296. ;; arch-tag: b76736bc-9f4a-43cd-977c-ecfd6689846a
  297. ;;; org-indent.el ends here