org-macs.el 14 KB

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