org-macs.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. ;;; org-macs.el --- Top-level definitions for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.35c
  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. ;; 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. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file contains macro definitions, defsubst definitions, other
  25. ;; stuff needed for compilation and top-level forms in Org-mode, as well
  26. ;; lots of small functions that are not org-mode specific but simply
  27. ;; generally useful stuff.
  28. ;;; Code:
  29. (eval-and-compile
  30. (unless (fboundp 'declare-function)
  31. (defmacro declare-function (fn file &optional arglist fileonly))))
  32. (declare-function org-add-props "org-compat" (string plist &rest props))
  33. (defmacro org-bound-and-true-p (var)
  34. "Return the value of symbol VAR if it is bound, else nil."
  35. `(and (boundp (quote ,var)) ,var))
  36. (defmacro org-unmodified (&rest body)
  37. "Execute body without changing `buffer-modified-p'.
  38. Also, do not record undo information."
  39. `(set-buffer-modified-p
  40. (prog1 (buffer-modified-p)
  41. (let ((buffer-undo-list t)
  42. before-change-functions after-change-functions)
  43. ,@body))))
  44. (defmacro org-re (s)
  45. "Replace posix classes in regular expression."
  46. (if (featurep 'xemacs)
  47. (let ((ss s))
  48. (save-match-data
  49. (while (string-match "\\[:alnum:\\]" ss)
  50. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  51. (while (string-match "\\[:word:\\]" ss)
  52. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  53. (while (string-match "\\[:alpha:\\]" ss)
  54. (setq ss (replace-match "a-zA-Z" t t ss)))
  55. (while (string-match "\\[:punct:\\]" ss)
  56. (setq ss (replace-match "\001-@[-`{-~" t t ss)))
  57. ss))
  58. s))
  59. (defmacro org-preserve-lc (&rest body)
  60. `(let ((_line (org-current-line))
  61. (_col (current-column)))
  62. (unwind-protect
  63. (progn ,@body)
  64. (org-goto-line _line)
  65. (org-move-to-column _col))))
  66. (defmacro org-without-partial-completion (&rest body)
  67. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  68. partial-completion-mode)))
  69. (unwind-protect
  70. (progn
  71. (if pc-mode (partial-completion-mode -1))
  72. ,@body)
  73. (if pc-mode (partial-completion-mode 1)))))
  74. (defmacro org-maybe-intangible (props)
  75. "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
  76. In emacs 21, invisible text is not avoided by the command loop, so the
  77. intangible property is needed to make sure point skips this text.
  78. In Emacs 22, this is not necessary. The intangible text property has
  79. led to problems with flyspell. These problems are fixed in flyspell.el,
  80. but we still avoid setting the property in Emacs 22 and later.
  81. We use a macro so that the test can happen at compilation time."
  82. (if (< emacs-major-version 22)
  83. `(append '(intangible t) ,props)
  84. props))
  85. (defmacro org-with-point-at (pom &rest body)
  86. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  87. `(save-excursion
  88. (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
  89. (save-excursion
  90. (goto-char (or ,pom (point)))
  91. ,@body)))
  92. (put 'org-with-point-at 'lisp-indent-function 1)
  93. (defmacro org-no-warnings (&rest body)
  94. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  95. (defmacro org-if-unprotected (&rest body)
  96. "Execute BODY if there is no `org-protected' text property at point."
  97. `(unless (get-text-property (point) 'org-protected)
  98. ,@body))
  99. (defmacro org-if-unprotected-1 (&rest body)
  100. "Execute BODY if there is no `org-protected' text property at point-1."
  101. `(unless (get-text-property (1- (point)) 'org-protected)
  102. ,@body))
  103. (defmacro org-if-unprotected-at (pos &rest body)
  104. "Execute BODY if there is no `org-protected' text property at POS."
  105. `(unless (get-text-property ,pos 'org-protected)
  106. ,@body))
  107. (put 'org-if-unprotected-at 'lisp-indent-function 1)
  108. (defun org-re-search-forward-unprotected (&rest args)
  109. "Like re-search-forward, but stop only in unprotected places."
  110. (catch 'exit
  111. (while t
  112. (unless (apply 're-search-forward args)
  113. (throw 'exit nil))
  114. (unless (get-text-property (match-beginning 0) 'org-protected)
  115. (throw 'exit (point))))))
  116. (defmacro org-with-remote-undo (_buffer &rest _body)
  117. "Execute BODY while recording undo information in two buffers."
  118. `(let ((_cline (org-current-line))
  119. (_cmd this-command)
  120. (_buf1 (current-buffer))
  121. (_buf2 ,_buffer)
  122. (_undo1 buffer-undo-list)
  123. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  124. _c1 _c2)
  125. ,@_body
  126. (when org-agenda-allow-remote-undo
  127. (setq _c1 (org-verify-change-for-undo
  128. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  129. _c2 (org-verify-change-for-undo
  130. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  131. (when (or _c1 _c2)
  132. ;; make sure there are undo boundaries
  133. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  134. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  135. ;; remember which buffer to undo
  136. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  137. org-agenda-undo-list)))))
  138. (defmacro org-no-read-only (&rest body)
  139. "Inhibit read-only for BODY."
  140. `(let ((inhibit-read-only t)) ,@body))
  141. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  142. rear-nonsticky t mouse-map t fontified t)
  143. "Properties to remove when a string without properties is wanted.")
  144. (defsubst org-match-string-no-properties (num &optional string)
  145. (if (featurep 'xemacs)
  146. (let ((s (match-string num string)))
  147. (and s (remove-text-properties 0 (length s) org-rm-props s))
  148. s)
  149. (match-string-no-properties num string)))
  150. (defsubst org-no-properties (s)
  151. (if (fboundp 'set-text-properties)
  152. (set-text-properties 0 (length s) nil s)
  153. (remove-text-properties 0 (length s) org-rm-props s))
  154. s)
  155. (defsubst org-get-alist-option (option key)
  156. (cond ((eq key t) t)
  157. ((eq option t) t)
  158. ((assoc key option) (cdr (assoc key option)))
  159. (t (cdr (assq 'default option)))))
  160. (defsubst org-check-external-command (cmd &optional use no-error)
  161. "Check if external program CMD for USE exists, error if not.
  162. When the program does exist, return its path.
  163. When it does not exist and NO-ERROR is set, return nil.
  164. Otherwise, throw an error. The optional argument USE can describe what this
  165. program is needed for, so that the error message can be more informative."
  166. (or (executable-find cmd)
  167. (if no-error
  168. nil
  169. (error "Can't find `%s'%s" cmd
  170. (if use (format " (%s)" use) "")))))
  171. (defsubst org-inhibit-invisibility ()
  172. "Modified `buffer-invisibility-spec' for Emacs 21.
  173. Some ops with invisible text do not work correctly on Emacs 21. For these
  174. we turn off invisibility temporarily. Use this in a `let' form."
  175. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  176. (defsubst org-set-local (var value)
  177. "Make VAR local in current buffer and set it to VALUE."
  178. (set (make-local-variable var) value))
  179. (defsubst org-mode-p ()
  180. "Check if the current buffer is in Org-mode."
  181. (eq major-mode 'org-mode))
  182. (defsubst org-last (list)
  183. "Return the last element of LIST."
  184. (car (last list)))
  185. (defun org-let (list &rest body)
  186. (eval (cons 'let (cons list body))))
  187. (put 'org-let 'lisp-indent-function 1)
  188. (defun org-let2 (list1 list2 &rest body)
  189. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  190. (put 'org-let2 'lisp-indent-function 2)
  191. (defsubst org-call-with-arg (command arg)
  192. "Call COMMAND interactively, but pretend prefix arg was ARG."
  193. (let ((current-prefix-arg arg)) (call-interactively command)))
  194. (defsubst org-current-line (&optional pos)
  195. (save-excursion
  196. (and pos (goto-char pos))
  197. ;; works also in narrowed buffer, because we start at 1, not point-min
  198. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  199. (defsubst org-goto-line (N)
  200. (save-restriction
  201. (widen)
  202. (goto-char (point-min))
  203. (forward-line (1- N))))
  204. (defsubst org-current-line-string (&optional to-here)
  205. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  206. (defsubst org-pos-in-match-range (pos n)
  207. (and (match-beginning n)
  208. (<= (match-beginning n) pos)
  209. (>= (match-end n) pos)))
  210. (defun org-autoload (file functions)
  211. "Establish autoload for all FUNCTIONS in FILE, if not bound already."
  212. (let ((d (format "Documentation will be available after `%s.el' is loaded."
  213. file))
  214. f)
  215. (while (setq f (pop functions))
  216. (or (fboundp f) (autoload f file d t)))))
  217. (defun org-match-line (re)
  218. "Looking-at at the beginning of the current line."
  219. (save-excursion
  220. (goto-char (point-at-bol))
  221. (looking-at re)))
  222. (defun org-plist-delete (plist property)
  223. "Delete PROPERTY from PLIST.
  224. This is in contrast to merely setting it to 0."
  225. (let (p)
  226. (while plist
  227. (if (not (eq property (car plist)))
  228. (setq p (plist-put p (car plist) (nth 1 plist))))
  229. (setq plist (cddr plist)))
  230. p))
  231. (defun org-replace-match-keep-properties (newtext &optional fixedcase
  232. literal string)
  233. "Like `replace-match', but add the text properties found original text."
  234. (setq newtext (org-add-props newtext (text-properties-at
  235. (match-beginning 0) string)))
  236. (replace-match newtext fixedcase literal string))
  237. (defmacro org-with-limited-levels (&rest body)
  238. "Execute BODY with limited number of outline levels."
  239. `(let* ((outline-regexp (org-get-limited-outline-regexp)))
  240. ,@body))
  241. (defvar org-odd-levels-only) ; defined in org.el
  242. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  243. (defun org-get-limited-outline-regexp ()
  244. "Return outline-regexp with limited number of levels.
  245. The number of levels is controlled by "
  246. (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
  247. outline-regexp
  248. (let* ((limit-level (1- org-inlinetask-min-level))
  249. (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
  250. (format "\\*\\{1,%d\\} " nstars))))
  251. (provide 'org-macs)
  252. ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
  253. ;;; org-macs.el ends here