org-indent.el 9.9 KB

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