org-indent.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.01trans
  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. (defgroup org-indent nil
  37. "Options concerning dynamic virtual outline indentation."
  38. :tag "Org Indent"
  39. :group 'org)
  40. (defconst org-indent-max 40
  41. "Maximum indentation in characters.")
  42. (defconst org-indent-max-levels 40
  43. "Maximum indentation in characters.")
  44. (defvar org-indent-strings nil
  45. "Vector with all indentation strings.
  46. It will be set in `org-indent-initialize'.")
  47. (defvar org-indent-stars nil
  48. "Vector with all indentation star strings.
  49. It will be set in `org-indent-initialize'.")
  50. (defvar org-hide-leading-stars-before-indent-mode nil
  51. "Used locally.")
  52. (defcustom org-indent-boundary-char ?\ ; comment to protect space char
  53. "The end of the virtual indentation strings, a single-character string.
  54. The default is just a space, but if you wish, you can use \"|\" or so.
  55. This can be useful on a terminal window - under a windowing system,
  56. it may be prettier to customize the org-indent face."
  57. :group 'org-indent
  58. :set (lambda (var val)
  59. (set var val)
  60. (and org-indent-strings (org-indent-initialize)))
  61. :type 'character)
  62. (defcustom org-indent-mode-turns-off-org-adapt-indentation t
  63. "Non-nil means setting the variable `org-indent-mode' will \
  64. turn off indentation adaptation.
  65. For details see the variable `org-adapt-indentation'."
  66. :group 'org-indent
  67. :type 'boolean)
  68. (defcustom org-indent-mode-turns-on-hiding-stars t
  69. "Non-nil means setting the variable `org-indent-mode' will \
  70. turn on `org-hide-leading-stars'."
  71. :group 'org-indent
  72. :type 'boolean)
  73. (defcustom org-indent-indentation-per-level 2
  74. "Indentation per level in number of characters."
  75. :group 'org-indent
  76. :type 'integer)
  77. (defcustom org-indent-fix-section-after-idle-time 0.2
  78. "Seconds of idle time before fixing virtual indentation of section.
  79. The hooking-in of virtual indentation is not yet perfect. Occasionally,
  80. a change does not trigger to proper change of indentation. For this we
  81. have a timer action that fixes indentation in the current section after
  82. a short amount idle time. If we ever get the integration to work perfectly,
  83. this variable can be set to nil to get rid of the timer."
  84. :group 'org-indent
  85. :type '(choice
  86. (const "Do not install idle timer" nil)
  87. (number :tag "Idle time")))
  88. (defun org-indent-initialize ()
  89. "Initialize the indentation strings and set the idle timer."
  90. ;; We use an idle timer to "repair" the current section, because the
  91. ;; redisplay seems to have some problems.
  92. (unless org-indent-strings
  93. (when org-indent-fix-section-after-idle-time
  94. (run-with-idle-timer
  95. org-indent-fix-section-after-idle-time
  96. t 'org-indent-refresh-section)))
  97. ;; Initialize the indentation and star vectors
  98. (setq org-indent-strings (make-vector (1+ org-indent-max) nil))
  99. (setq org-indent-stars (make-vector (1+ org-indent-max) nil))
  100. (aset org-indent-strings 0 nil)
  101. (aset org-indent-stars 0 nil)
  102. (loop for i from 1 to org-indent-max do
  103. (aset org-indent-strings i
  104. (org-add-props
  105. (concat (make-string (1- i) ?\ )
  106. (char-to-string org-indent-boundary-char))
  107. nil 'face 'org-indent)))
  108. (loop for i from 1 to org-indent-max-levels do
  109. (aset org-indent-stars i
  110. (org-add-props (make-string i ?*)
  111. nil 'face 'org-hide))))
  112. ;;;###autoload
  113. (define-minor-mode org-indent-mode
  114. "When active, indent text according to outline structure.
  115. Internally this works by adding `line-prefix' properties to all non-headlines.
  116. These properties are updated locally in idle time.
  117. FIXME: How to update when broken?"
  118. nil " Ind" nil
  119. (cond
  120. ((org-bound-and-true-p org-inhibit-startup)
  121. (setq org-indent-mode nil))
  122. ((and org-indent-mode (featurep 'xemacs))
  123. (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
  124. (setq org-indent-mode nil))
  125. ((and org-indent-mode
  126. (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
  127. (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
  128. (ding)
  129. (sit-for 1)
  130. (setq org-indent-mode nil))
  131. (org-indent-mode
  132. ;; mode was turned on.
  133. (org-set-local 'indent-tabs-mode nil)
  134. (or org-indent-strings (org-indent-initialize))
  135. (when org-indent-mode-turns-off-org-adapt-indentation
  136. (org-set-local 'org-adapt-indentation nil))
  137. (when org-indent-mode-turns-on-hiding-stars
  138. (org-set-local 'org-hide-leading-stars-before-indent-mode
  139. org-hide-leading-stars)
  140. (org-set-local 'org-hide-leading-stars t))
  141. (make-local-variable 'buffer-substring-filters)
  142. (add-to-list 'buffer-substring-filters
  143. 'org-indent-remove-properties-from-string)
  144. (org-add-hook 'org-after-demote-entry-hook
  145. 'org-indent-refresh-section nil 'local)
  146. (org-add-hook 'org-after-promote-entry-hook
  147. 'org-indent-refresh-section nil 'local)
  148. (org-add-hook 'org-font-lock-hook
  149. 'org-indent-refresh-to nil 'local)
  150. (and font-lock-mode (org-restart-font-lock))
  151. )
  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-section 'local)
  166. (remove-hook 'org-after-demote-entry-hook
  167. 'org-indent-refresh-section '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. (org-unmodified
  189. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil)))))
  190. (defun org-indent-remove-properties-from-string (string)
  191. "Remove indentations between BEG and END."
  192. (remove-text-properties 0 (length string)
  193. '(line-prefix nil wrap-prefix nil) string)
  194. string)
  195. (defvar org-indent-outline-re (concat "^" org-outline-regexp)
  196. "Outline heading regexp.")
  197. (defun org-indent-add-properties (beg end)
  198. "Add indentation properties between BEG and END.
  199. Assumes that BEG is at the beginning of a line."
  200. (when (or t org-indent-mode)
  201. (let ((inhibit-modification-hooks t)
  202. ov b e n level exit nstars)
  203. (org-unmodified
  204. (save-excursion
  205. (goto-char beg)
  206. (while (not exit)
  207. (setq e end)
  208. (if (not (re-search-forward org-indent-outline-re nil t))
  209. (setq e (point-max) exit t)
  210. (setq e (match-beginning 0))
  211. (if (>= e end) (setq exit t))
  212. (setq level (- (match-end 0) (match-beginning 0) 1))
  213. (setq nstars (- (* (1- level) org-indent-indentation-per-level)
  214. (1- level)))
  215. (add-text-properties
  216. (point-at-bol) (point-at-eol)
  217. (list 'line-prefix
  218. (aref org-indent-stars nstars)
  219. 'wrap-prefix
  220. (aref org-indent-strings
  221. (* level org-indent-indentation-per-level)))))
  222. (when (and b (> e b))
  223. (add-text-properties
  224. b e (list 'line-prefix (aref org-indent-strings n)
  225. 'wrap-prefix (aref org-indent-strings n))))
  226. (setq b (1+ (point-at-eol))
  227. n (* (or level 0) org-indent-indentation-per-level))))))))
  228. (defun org-indent-refresh-section ()
  229. "Refresh indentation properties in the current outline section.
  230. Point is assumed to be at the beginning of a headline."
  231. (interactive)
  232. (when org-indent-mode
  233. (let (beg end)
  234. (save-excursion
  235. (when (ignore-errors (org-back-to-heading))
  236. (setq beg (point))
  237. (setq end (or (save-excursion (or (outline-next-heading) (point)))))
  238. (org-indent-remove-properties beg end)
  239. (org-indent-add-properties beg end))))))
  240. (defun org-indent-refresh-to (limit)
  241. "Refresh indentation properties in the current outline section.
  242. Point is assumed to be at the beginning of a headline."
  243. (interactive)
  244. (when org-indent-mode
  245. (let ((beg (point)) (end limit))
  246. (save-excursion
  247. (and (ignore-errors (org-back-to-heading t))
  248. (setq beg (point))))
  249. (org-indent-remove-properties beg end)
  250. (org-indent-add-properties beg end)))
  251. (goto-char limit))
  252. (defun org-indent-refresh-subtree ()
  253. "Refresh indentation properties in the current outline subtree.
  254. Point is assumed to be at the beginning of a headline."
  255. (interactive)
  256. (when org-indent-mode
  257. (save-excursion
  258. (let (beg end)
  259. (setq beg (point))
  260. (setq end (save-excursion (org-end-of-subtree t t)))
  261. (org-indent-remove-properties beg end)
  262. (org-indent-add-properties beg end)))))
  263. (defun org-indent-refresh-buffer ()
  264. "Refresh indentation properties in the current outline subtree.
  265. Point is assumed to be at the beginning of a headline."
  266. (interactive)
  267. (when org-indent-mode
  268. (org-indent-mode -1)
  269. (org-indent-mode 1)))
  270. (provide 'org-indent)
  271. ;; arch-tag: b76736bc-9f4a-43cd-977c-ecfd6689846a
  272. ;;; org-indent.el ends here