org-indent.el 10 KB

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