org-indent.el 9.5 KB

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