org-indent.el 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. (if (org-bound-and-true-p org-inhibit-startup)
  117. (setq org-indent-mode nil)
  118. (if org-indent-mode
  119. (progn
  120. (org-set-local 'indent-tabs-mode nil)
  121. (or org-indent-strings (org-indent-initialize))
  122. (when org-indent-mode-turns-off-org-adapt-indentation
  123. (org-set-local 'org-adapt-indentation nil))
  124. (when org-indent-mode-turns-on-hiding-stars
  125. (org-set-local 'org-hide-leading-stars-before-indent-mode
  126. org-hide-leading-stars)
  127. (org-set-local 'org-hide-leading-stars t))
  128. (make-local-variable 'buffer-substring-filters)
  129. (add-to-list 'buffer-substring-filters
  130. 'org-indent-remove-properties-from-string)
  131. (org-add-hook 'org-after-demote-entry-hook
  132. 'org-indent-refresh-section nil 'local)
  133. (org-add-hook 'org-after-promote-entry-hook
  134. 'org-indent-refresh-section nil 'local)
  135. (org-add-hook 'org-font-lock-hook
  136. 'org-indent-refresh-to nil 'local)
  137. (and font-lock-mode (org-restart-font-lock))
  138. )
  139. (save-excursion
  140. (save-restriction
  141. (org-indent-remove-properties (point-min) (point-max))
  142. (kill-local-variable 'org-adapt-indentation)
  143. (when (boundp 'org-hide-leading-stars-before-indent-mode)
  144. (org-set-local 'org-hide-leading-stars
  145. org-hide-leading-stars-before-indent-mode))
  146. (setq buffer-substring-filters
  147. (delq 'org-indent-remove-properties-from-string
  148. buffer-substring-filters))
  149. (remove-hook 'org-after-promote-entry-hook
  150. 'org-indent-refresh-section 'local)
  151. (remove-hook 'org-after-demote-entry-hook
  152. 'org-indent-refresh-section 'local)
  153. (and font-lock-mode (org-restart-font-lock))
  154. (redraw-display))))))
  155. (defface org-indent
  156. (org-compatible-face nil nil)
  157. "Face for outline indentation.
  158. The default is to make it look like whitespace. But you may find it
  159. useful to make it ever so slightly different."
  160. :group 'org-faces)
  161. (defun org-indent-indent-buffer ()
  162. "Add indentation properties for the whole buffer."
  163. (interactive)
  164. (when org-indent-mode
  165. (save-excursion
  166. (save-restriction
  167. (widen)
  168. (org-indent-remove-properties (point-min) (point-max))
  169. (org-indent-add-properties (point-min) (point-max))))))
  170. (defun org-indent-remove-properties (beg end)
  171. "Remove indentations between BEG and END."
  172. (org-unmodified
  173. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil))))
  174. (defun org-indent-remove-properties-from-string (string)
  175. "Remove indentations between BEG and END."
  176. (remove-text-properties 0 (length string)
  177. '(line-prefix nil wrap-prefix nil) string)
  178. string)
  179. (defvar org-indent-outline-re (concat "^" org-outline-regexp)
  180. "Outline heading regexp.")
  181. (defun org-indent-add-properties (beg end)
  182. "Add indentation properties between BEG and END.
  183. Assumes that BEG is at the beginning of a line."
  184. (when (or t org-indent-mode)
  185. (let (ov b e n level exit nstars)
  186. (org-unmodified
  187. (save-excursion
  188. (goto-char beg)
  189. (while (not exit)
  190. (setq e end)
  191. (if (not (re-search-forward org-indent-outline-re nil t))
  192. (setq e (point-max) exit t)
  193. (setq e (match-beginning 0))
  194. (if (>= e end) (setq exit t))
  195. (setq level (- (match-end 0) (match-beginning 0) 1))
  196. (setq nstars (- (* (1- level) org-indent-indentation-per-level)
  197. (1- level)))
  198. (add-text-properties
  199. (point-at-bol) (point-at-eol)
  200. (list 'line-prefix
  201. (aref org-indent-stars nstars)
  202. 'wrap-prefix
  203. (aref org-indent-strings
  204. (* level org-indent-indentation-per-level)))))
  205. (when (and b (> e b))
  206. (add-text-properties
  207. b e (list 'line-prefix (aref org-indent-strings n)
  208. 'wrap-prefix (aref org-indent-strings n))))
  209. (setq b (1+ (point-at-eol))
  210. n (* (or level 0) org-indent-indentation-per-level))))))))
  211. (defun org-indent-refresh-section ()
  212. "Refresh indentation properties in the current outline section.
  213. Point is assumed to be at the beginning of a headline."
  214. (interactive)
  215. (when org-indent-mode
  216. (let (beg end)
  217. (save-excursion
  218. (when (ignore-errors (org-back-to-heading))
  219. (setq beg (point))
  220. (setq end (or (save-excursion (or (outline-next-heading) (point)))))
  221. (org-indent-remove-properties beg end)
  222. (org-indent-add-properties beg end))))))
  223. (defun org-indent-refresh-to (limit)
  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 (point)) (end limit))
  229. (save-excursion
  230. (and (ignore-errors (org-back-to-heading t))
  231. (setq beg (point))))
  232. (org-indent-remove-properties beg end)
  233. (org-indent-add-properties beg end)))
  234. (goto-char limit))
  235. (defun org-indent-refresh-subtree ()
  236. "Refresh indentation properties in the current outline subtree.
  237. Point is assumed to be at the beginning of a headline."
  238. (interactive)
  239. (when org-indent-mode
  240. (save-excursion
  241. (let (beg end)
  242. (setq beg (point))
  243. (setq end (save-excursion (org-end-of-subtree t t)))
  244. (org-indent-remove-properties beg end)
  245. (org-indent-add-properties beg end)))))
  246. (defun org-indent-refresh-buffer ()
  247. "Refresh indentation properties in the current outline subtree.
  248. Point is assumed to be at the beginning of a headline."
  249. (interactive)
  250. (when org-indent-mode
  251. (org-indent-mode -1)
  252. (org-indent-mode 1)))
  253. (provide 'org-indent)
  254. ;; arch-tag: b76736bc-9f4a-43cd-977c-ecfd6689846a
  255. ;;; org-indent.el ends here