org-macs.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. ;;; org-macs.el --- Top-level definitions for Org-mode
  2. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains macro definitions, defsubst definitions, other
  23. ;; stuff needed for compilation and top-level forms in Org-mode, as well
  24. ;; lots of small functions that are not org-mode specific but simply
  25. ;; generally useful stuff.
  26. ;;; Code:
  27. (eval-and-compile
  28. (unless (fboundp 'declare-function)
  29. (defmacro declare-function (fn file &optional _arglist _fileonly)
  30. `(autoload ',fn ,file)))
  31. (if (>= emacs-major-version 23)
  32. (defsubst org-char-to-string(c)
  33. "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
  34. (char-to-string c))
  35. (defsubst org-char-to-string (c)
  36. "Defsubst to decode UTF-8 character values in emacs 22."
  37. (string (decode-char 'ucs c)))))
  38. (declare-function org-add-props "org-compat" (string plist &rest props))
  39. (declare-function org-string-match-p "org-compat" (&rest args))
  40. (defmacro org-with-gensyms (symbols &rest body)
  41. (declare (debug (sexp body)) (indent 1))
  42. `(let ,(mapcar (lambda (s)
  43. `(,s (make-symbol (concat "--" (symbol-name ',s)))))
  44. symbols)
  45. ,@body))
  46. (defmacro org-called-interactively-p (&optional kind)
  47. (declare (debug (&optional ("quote" symbolp)))) ;Why not just t?
  48. (if (featurep 'xemacs)
  49. `(interactive-p)
  50. (if (or (> emacs-major-version 23)
  51. (and (>= emacs-major-version 23)
  52. (>= emacs-minor-version 2)))
  53. ;; defined with no argument in <=23.1
  54. `(with-no-warnings (called-interactively-p ,kind))
  55. `(interactive-p))))
  56. (defmacro org-bound-and-true-p (var)
  57. "Return the value of symbol VAR if it is bound, else nil."
  58. (declare (debug (symbolp)))
  59. `(and (boundp (quote ,var)) ,var))
  60. (defun org-string-nw-p (s)
  61. "Return S if S is a string containing a non-blank character.
  62. Otherwise, return nil."
  63. (and (stringp s)
  64. (org-string-match-p "[^ \r\t\n]" s)
  65. s))
  66. (defun org-not-nil (v)
  67. "If V not nil, and also not the string \"nil\", then return V.
  68. Otherwise return nil."
  69. (and v (not (equal v "nil")) v))
  70. (defun org-substitute-posix-classes (re)
  71. "Substitute posix classes in regular expression RE."
  72. (let ((ss re))
  73. (save-match-data
  74. (while (string-match "\\[:alnum:\\]" ss)
  75. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  76. (while (string-match "\\[:word:\\]" ss)
  77. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  78. (while (string-match "\\[:alpha:\\]" ss)
  79. (setq ss (replace-match "a-zA-Z" t t ss)))
  80. (while (string-match "\\[:punct:\\]" ss)
  81. (setq ss (replace-match "\001-@[-`{-~" t t ss)))
  82. ss)))
  83. (defmacro org-re (s)
  84. "Replace posix classes in regular expression."
  85. (declare (debug (form)))
  86. (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
  87. (defmacro org-preserve-lc (&rest body)
  88. (declare (debug (body)))
  89. (org-with-gensyms (line col)
  90. `(let ((,line (org-current-line))
  91. (,col (current-column)))
  92. (unwind-protect
  93. (progn ,@body)
  94. (org-goto-line ,line)
  95. (org-move-to-column ,col)))))
  96. ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
  97. ;; `org-unmodified' to ignore real text modifications
  98. (defmacro org-unmodified (&rest body)
  99. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  100. (declare (debug (body)))
  101. (org-with-gensyms (was-modified)
  102. `(let ((,was-modified (buffer-modified-p)))
  103. (unwind-protect
  104. (let ((buffer-undo-list t)
  105. (inhibit-modification-hooks t))
  106. ,@body)
  107. (set-buffer-modified-p ,was-modified)))))
  108. (defmacro org-without-partial-completion (&rest body)
  109. (declare (debug (body)))
  110. `(if (and (boundp 'partial-completion-mode)
  111. partial-completion-mode
  112. (fboundp 'partial-completion-mode))
  113. (unwind-protect
  114. (progn
  115. (partial-completion-mode -1)
  116. ,@body)
  117. (partial-completion-mode 1))
  118. ,@body))
  119. ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
  120. (defmacro org-maybe-intangible (props)
  121. "Add \\='(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
  122. In Emacs 21, invisible text is not avoided by the command loop, so the
  123. intangible property is needed to make sure point skips this text.
  124. In Emacs 22, this is not necessary. The intangible text property has
  125. led to problems with flyspell. These problems are fixed in flyspell.el,
  126. but we still avoid setting the property in Emacs 22 and later.
  127. We use a macro so that the test can happen at compilation time."
  128. (if (< emacs-major-version 22)
  129. `(append '(intangible t) ,props)
  130. props))
  131. (defmacro org-with-point-at (pom &rest body)
  132. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  133. (declare (debug (form body)) (indent 1))
  134. (org-with-gensyms (mpom)
  135. `(let ((,mpom ,pom))
  136. (save-excursion
  137. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  138. (org-with-wide-buffer
  139. (goto-char (or ,mpom (point)))
  140. ,@body)))))
  141. (defmacro org-no-warnings (&rest body)
  142. (declare (debug (body)))
  143. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  144. (defmacro org-with-remote-undo (buffer &rest body)
  145. "Execute BODY while recording undo information in two buffers."
  146. (declare (debug (form body)) (indent 1))
  147. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  148. `(let ((,cline (org-current-line))
  149. (,cmd this-command)
  150. (,buf1 (current-buffer))
  151. (,buf2 ,buffer)
  152. (,undo1 buffer-undo-list)
  153. (,undo2 (with-current-buffer ,buffer buffer-undo-list))
  154. ,c1 ,c2)
  155. ,@body
  156. (when org-agenda-allow-remote-undo
  157. (setq ,c1 (org-verify-change-for-undo
  158. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  159. ,c2 (org-verify-change-for-undo
  160. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  161. (when (or ,c1 ,c2)
  162. ;; make sure there are undo boundaries
  163. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  164. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  165. ;; remember which buffer to undo
  166. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  167. org-agenda-undo-list))))))
  168. (defmacro org-no-read-only (&rest body)
  169. "Inhibit read-only for BODY."
  170. (declare (debug (body)))
  171. `(let ((inhibit-read-only t)) ,@body))
  172. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  173. rear-nonsticky t mouse-map t fontified t
  174. org-emphasis t)
  175. "Properties to remove when a string without properties is wanted.")
  176. (defsubst org-match-string-no-properties (num &optional string)
  177. (if (featurep 'xemacs)
  178. (let ((s (match-string num string)))
  179. (and s (remove-text-properties 0 (length s) org-rm-props s))
  180. s)
  181. (match-string-no-properties num string)))
  182. (defsubst org-no-properties (s &optional restricted)
  183. "Remove all text properties from string S.
  184. When RESTRICTED is non-nil, only remove the properties listed
  185. in `org-rm-props'."
  186. (if (fboundp 'set-text-properties)
  187. (set-text-properties 0 (length s) nil s)
  188. (if restricted
  189. (remove-text-properties 0 (length s) org-rm-props s)
  190. (set-text-properties 0 (length s) nil s)))
  191. s)
  192. (defsubst org-get-alist-option (option key)
  193. (cond ((eq key t) t)
  194. ((eq option t) t)
  195. ((assoc key option) (cdr (assoc key option)))
  196. (t (let ((r (cdr (assq 'default option))))
  197. (if (listp r) (delq nil r) r)))))
  198. (defsubst org-check-external-command (cmd &optional use no-error)
  199. "Check if external program CMD for USE exists, error if not.
  200. When the program does exist, return its path.
  201. When it does not exist and NO-ERROR is set, return nil.
  202. Otherwise, throw an error. The optional argument USE can describe what this
  203. program is needed for, so that the error message can be more informative."
  204. (or (executable-find cmd)
  205. (if no-error
  206. nil
  207. (error "Can't find `%s'%s" cmd
  208. (if use (format " (%s)" use) "")))))
  209. (defsubst org-inhibit-invisibility ()
  210. "Modified `buffer-invisibility-spec' for Emacs 21.
  211. Some ops with invisible text do not work correctly on Emacs 21. For these
  212. we turn off invisibility temporarily. Use this in a `let' form."
  213. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  214. (defsubst org-last (list)
  215. "Return the last element of LIST."
  216. (car (last list)))
  217. (defun org-let (list &rest body)
  218. (eval (cons 'let (cons list body))))
  219. (put 'org-let 'lisp-indent-function 1)
  220. (defun org-let2 (list1 list2 &rest body)
  221. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  222. (put 'org-let2 'lisp-indent-function 2)
  223. (defsubst org-call-with-arg (command arg)
  224. "Call COMMAND interactively, but pretend prefix arg was ARG."
  225. (let ((current-prefix-arg arg)) (call-interactively command)))
  226. (defsubst org-current-line (&optional pos)
  227. (save-excursion
  228. (and pos (goto-char pos))
  229. ;; works also in narrowed buffer, because we start at 1, not point-min
  230. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  231. (defsubst org-goto-line (N)
  232. (save-restriction
  233. (widen)
  234. (goto-char (point-min))
  235. (forward-line (1- N))))
  236. (defsubst org-current-line-string (&optional to-here)
  237. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  238. (defsubst org-pos-in-match-range (pos n)
  239. (and (match-beginning n)
  240. (<= (match-beginning n) pos)
  241. (>= (match-end n) pos)))
  242. (defun org-match-line (re)
  243. "Looking-at at the beginning of the current line."
  244. (save-excursion
  245. (goto-char (point-at-bol))
  246. (looking-at re)))
  247. (defun org-plist-delete (plist property)
  248. "Delete PROPERTY from PLIST.
  249. This is in contrast to merely setting it to 0."
  250. (let (p)
  251. (while plist
  252. (if (not (eq property (car plist)))
  253. (setq p (plist-put p (car plist) (nth 1 plist))))
  254. (setq plist (cddr plist)))
  255. p))
  256. (defun org-replace-match-keep-properties (newtext &optional fixedcase
  257. literal string)
  258. "Like `replace-match', but add the text properties found original text."
  259. (setq newtext (org-add-props newtext (text-properties-at
  260. (match-beginning 0) string)))
  261. (replace-match newtext fixedcase literal string))
  262. (defmacro org-save-outline-visibility (use-markers &rest body)
  263. "Save and restore outline visibility around BODY.
  264. If USE-MARKERS is non-nil, use markers for the positions.
  265. This means that the buffer may change while running BODY,
  266. but it also means that the buffer should stay alive
  267. during the operation, because otherwise all these markers will
  268. point nowhere."
  269. (declare (debug (form body)) (indent 1))
  270. (org-with-gensyms (data rtn)
  271. `(let ((,data (org-outline-overlay-data ,use-markers))
  272. ,rtn)
  273. (unwind-protect
  274. (progn
  275. (setq ,rtn (progn ,@body))
  276. (org-set-outline-overlay-data ,data))
  277. (when ,use-markers
  278. (mapc (lambda (c)
  279. (and (markerp (car c)) (move-marker (car c) nil))
  280. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  281. ,data)))
  282. ,rtn)))
  283. (defmacro org-with-wide-buffer (&rest body)
  284. "Execute body while temporarily widening the buffer."
  285. (declare (debug (body)))
  286. `(save-excursion
  287. (save-restriction
  288. (widen)
  289. ,@body)))
  290. (defmacro org-with-limited-levels (&rest body)
  291. "Execute BODY with limited number of outline levels."
  292. (declare (debug (body)))
  293. `(progn
  294. (defvar org-called-with-limited-levels)
  295. (defvar org-outline-regexp)
  296. (defvar outline-regexp)
  297. (defvar org-outline-regexp-bol)
  298. (let* ((org-called-with-limited-levels t)
  299. (org-outline-regexp (org-get-limited-outline-regexp))
  300. (outline-regexp org-outline-regexp)
  301. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  302. ,@body)))
  303. (defvar org-outline-regexp) ; defined in org.el
  304. (defvar org-odd-levels-only) ; defined in org.el
  305. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  306. (defun org-get-limited-outline-regexp ()
  307. "Return outline-regexp with limited number of levels.
  308. The number of levels is controlled by `org-inlinetask-min-level'"
  309. (cond ((not (derived-mode-p 'org-mode))
  310. outline-regexp)
  311. ((not (featurep 'org-inlinetask))
  312. org-outline-regexp)
  313. (t
  314. (let* ((limit-level (1- org-inlinetask-min-level))
  315. (nstars (if org-odd-levels-only
  316. (1- (* limit-level 2))
  317. limit-level)))
  318. (format "\\*\\{1,%d\\} " nstars)))))
  319. (defun org-format-seconds (string seconds)
  320. "Compatibility function replacing format-seconds."
  321. (if (fboundp 'format-seconds)
  322. (format-seconds string seconds)
  323. (format-time-string string (seconds-to-time seconds))))
  324. (defmacro org-eval-in-environment (environment form)
  325. (declare (debug (form form)) (indent 1))
  326. `(eval (list 'let ,environment ',form)))
  327. (defun org-make-parameter-alist (flat)
  328. "Return alist based on FLAT.
  329. FLAT is a list with alternating symbol names and values. The
  330. returned alist is a list of lists with the symbol name in car and
  331. the value in cdr."
  332. (when flat
  333. (cons (list (car flat) (cadr flat))
  334. (org-make-parameter-alist (cddr flat)))))
  335. ;;;###autoload
  336. (defmacro org-load-noerror-mustsuffix (file)
  337. "Load FILE with optional arguments NOERROR and MUSTSUFFIX. Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
  338. (if (featurep 'xemacs)
  339. `(load ,file 'noerror)
  340. `(load ,file 'noerror nil nil 'mustsuffix)))
  341. (provide 'org-macs)
  342. ;;; org-macs.el ends here