org-indent.el 9.9 KB

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