org-macs.el 12 KB

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