org-macs.el 13 KB

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