org-macs.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2016 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
  24. ;; well lots of small functions that are not Org mode specific but
  25. ;; simply 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. (defmacro org-with-gensyms (symbols &rest body)
  40. (declare (debug (sexp body)) (indent 1))
  41. `(let ,(mapcar (lambda (s)
  42. `(,s (make-symbol (concat "--" (symbol-name ',s)))))
  43. symbols)
  44. ,@body))
  45. (defmacro org-called-interactively-p (&optional kind)
  46. (declare (debug (&optional ("quote" symbolp)))) ;Why not just t?
  47. (if (or (> emacs-major-version 23)
  48. (and (>= emacs-major-version 23)
  49. (>= emacs-minor-version 2)))
  50. ;; defined with no argument in <=23.1
  51. `(with-no-warnings (called-interactively-p ,kind))
  52. `(interactive-p)))
  53. (defmacro org-bound-and-true-p (var)
  54. "Return the value of symbol VAR if it is bound, else nil."
  55. (declare (debug (symbolp)))
  56. `(and (boundp (quote ,var)) ,var))
  57. (defun org-string-nw-p (s)
  58. "Return S if S is a string containing a non-blank character.
  59. Otherwise, return nil."
  60. (and (stringp s)
  61. (string-match-p "[^ \r\t\n]" 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. (defun org-substitute-posix-classes (re)
  68. "Substitute posix classes in regular expression RE."
  69. (let ((ss re))
  70. (save-match-data
  71. (while (string-match "\\[:alnum:\\]" ss)
  72. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  73. (while (string-match "\\[:word:\\]" ss)
  74. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  75. (while (string-match "\\[:alpha:\\]" ss)
  76. (setq ss (replace-match "a-zA-Z" t t ss)))
  77. (while (string-match "\\[:punct:\\]" ss)
  78. (setq ss (replace-match "\001-@[-`{-~" t t ss)))
  79. ss)))
  80. (defmacro org-preserve-lc (&rest body)
  81. (declare (debug (body)))
  82. (org-with-gensyms (line col)
  83. `(let ((,line (org-current-line))
  84. (,col (current-column)))
  85. (unwind-protect
  86. (progn ,@body)
  87. (org-goto-line ,line)
  88. (org-move-to-column ,col)))))
  89. ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
  90. ;; `org-unmodified' to ignore real text modifications
  91. (defmacro org-unmodified (&rest body)
  92. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  93. (declare (debug (body)))
  94. (org-with-gensyms (was-modified)
  95. `(let ((,was-modified (buffer-modified-p)))
  96. (unwind-protect
  97. (let ((buffer-undo-list t)
  98. (inhibit-modification-hooks t))
  99. ,@body)
  100. (set-buffer-modified-p ,was-modified)))))
  101. (defmacro org-without-partial-completion (&rest body)
  102. (declare (debug (body)))
  103. `(if (and (boundp 'partial-completion-mode)
  104. partial-completion-mode
  105. (fboundp 'partial-completion-mode))
  106. (unwind-protect
  107. (progn
  108. (partial-completion-mode -1)
  109. ,@body)
  110. (partial-completion-mode 1))
  111. ,@body))
  112. ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
  113. (defmacro org-maybe-intangible (props)
  114. "Add \\='(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
  115. In Emacs 21, invisible text is not avoided by the command loop, so the
  116. intangible property is needed to make sure point skips this text.
  117. In Emacs 22, this is not necessary. The intangible text property has
  118. led to problems with flyspell. These problems are fixed in flyspell.el,
  119. but we still avoid setting the property in Emacs 22 and later.
  120. We use a macro so that the test can happen at compilation time."
  121. (if (< emacs-major-version 22)
  122. `(append '(intangible t) ,props)
  123. props))
  124. (defmacro org-with-point-at (pom &rest body)
  125. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  126. (declare (debug (form body)) (indent 1))
  127. (org-with-gensyms (mpom)
  128. `(let ((,mpom ,pom))
  129. (save-excursion
  130. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  131. (org-with-wide-buffer
  132. (goto-char (or ,mpom (point)))
  133. ,@body)))))
  134. (defmacro org-no-warnings (&rest body)
  135. (declare (debug (body)))
  136. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  137. (defmacro org-with-remote-undo (buffer &rest body)
  138. "Execute BODY while recording undo information in two buffers."
  139. (declare (debug (form body)) (indent 1))
  140. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  141. `(let ((,cline (org-current-line))
  142. (,cmd this-command)
  143. (,buf1 (current-buffer))
  144. (,buf2 ,buffer)
  145. (,undo1 buffer-undo-list)
  146. (,undo2 (with-current-buffer ,buffer buffer-undo-list))
  147. ,c1 ,c2)
  148. ,@body
  149. (when org-agenda-allow-remote-undo
  150. (setq ,c1 (org-verify-change-for-undo
  151. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  152. ,c2 (org-verify-change-for-undo
  153. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  154. (when (or ,c1 ,c2)
  155. ;; make sure there are undo boundaries
  156. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  157. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  158. ;; remember which buffer to undo
  159. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  160. org-agenda-undo-list))))))
  161. (defmacro org-no-read-only (&rest body)
  162. "Inhibit read-only for BODY."
  163. (declare (debug (body)))
  164. `(let ((inhibit-read-only t)) ,@body))
  165. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  166. rear-nonsticky t mouse-map t fontified t
  167. org-emphasis t)
  168. "Properties to remove when a string without properties is wanted.")
  169. (defsubst org-no-properties (s &optional restricted)
  170. "Remove all text properties from string S.
  171. When RESTRICTED is non-nil, only remove the properties listed
  172. in `org-rm-props'."
  173. (if (fboundp 'set-text-properties)
  174. (set-text-properties 0 (length s) nil s)
  175. (if restricted
  176. (remove-text-properties 0 (length s) org-rm-props s)
  177. (set-text-properties 0 (length s) nil s)))
  178. s)
  179. (defsubst org-get-alist-option (option key)
  180. (cond ((eq key t) t)
  181. ((eq option t) t)
  182. ((assoc key option) (cdr (assoc key option)))
  183. (t (let ((r (cdr (assq 'default option))))
  184. (if (listp r) (delq nil r) r)))))
  185. (defsubst org-check-external-command (cmd &optional use no-error)
  186. "Check if external program CMD for USE exists, error if not.
  187. When the program does exist, return its path.
  188. When it does not exist and NO-ERROR is set, return nil.
  189. Otherwise, throw an error. The optional argument USE can describe what this
  190. program is needed for, so that the error message can be more informative."
  191. (or (executable-find cmd)
  192. (if no-error
  193. nil
  194. (error "Can't find `%s'%s" cmd
  195. (if use (format " (%s)" use) "")))))
  196. (defsubst org-inhibit-invisibility ()
  197. "Modified `buffer-invisibility-spec' for Emacs 21.
  198. Some ops with invisible text do not work correctly on Emacs 21. For these
  199. we turn off invisibility temporarily. Use this in a `let' form."
  200. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  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-match-line (re)
  230. "Looking-at at the beginning of the current line."
  231. (save-excursion
  232. (goto-char (point-at-bol))
  233. (looking-at re)))
  234. (defun org-plist-delete (plist property)
  235. "Delete PROPERTY from PLIST.
  236. This is in contrast to merely setting it to 0."
  237. (let (p)
  238. (while plist
  239. (if (not (eq property (car plist)))
  240. (setq p (plist-put p (car plist) (nth 1 plist))))
  241. (setq plist (cddr plist)))
  242. p))
  243. (defun org-replace-match-keep-properties (newtext &optional fixedcase
  244. literal string)
  245. "Like `replace-match', but add the text properties found original text."
  246. (setq newtext (org-add-props newtext (text-properties-at
  247. (match-beginning 0) string)))
  248. (replace-match newtext fixedcase literal string))
  249. (defmacro org-save-outline-visibility (use-markers &rest body)
  250. "Save and restore outline visibility around BODY.
  251. If USE-MARKERS is non-nil, use markers for the positions.
  252. This means that the buffer may change while running BODY,
  253. but it also means that the buffer should stay alive
  254. during the operation, because otherwise all these markers will
  255. point nowhere."
  256. (declare (debug (form body)) (indent 1))
  257. (org-with-gensyms (data)
  258. `(let ((,data (org-outline-overlay-data ,use-markers)))
  259. (unwind-protect
  260. (prog1 (progn ,@body)
  261. (org-set-outline-overlay-data ,data))
  262. (when ,use-markers
  263. (dolist (c ,data)
  264. (when (markerp (car c)) (move-marker (car c) nil))
  265. (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
  266. (defmacro org-with-wide-buffer (&rest body)
  267. "Execute body while temporarily widening the buffer."
  268. (declare (debug (body)))
  269. `(save-excursion
  270. (save-restriction
  271. (widen)
  272. ,@body)))
  273. (defmacro org-with-limited-levels (&rest body)
  274. "Execute BODY with limited number of outline levels."
  275. (declare (debug (body)))
  276. `(progn
  277. (defvar org-called-with-limited-levels)
  278. (defvar org-outline-regexp)
  279. (defvar outline-regexp)
  280. (defvar org-outline-regexp-bol)
  281. (let* ((org-called-with-limited-levels t)
  282. (org-outline-regexp (org-get-limited-outline-regexp))
  283. (outline-regexp org-outline-regexp)
  284. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  285. ,@body)))
  286. (defvar org-outline-regexp) ; defined in org.el
  287. (defvar org-odd-levels-only) ; defined in org.el
  288. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  289. (defun org-get-limited-outline-regexp ()
  290. "Return outline-regexp with limited number of levels.
  291. The number of levels is controlled by `org-inlinetask-min-level'"
  292. (cond ((not (derived-mode-p 'org-mode))
  293. outline-regexp)
  294. ((not (featurep 'org-inlinetask))
  295. org-outline-regexp)
  296. (t
  297. (let* ((limit-level (1- org-inlinetask-min-level))
  298. (nstars (if org-odd-levels-only
  299. (1- (* limit-level 2))
  300. limit-level)))
  301. (format "\\*\\{1,%d\\} " nstars)))))
  302. (defun org-format-seconds (string seconds)
  303. "Compatibility function replacing format-seconds."
  304. (if (fboundp 'format-seconds)
  305. (format-seconds string seconds)
  306. (format-time-string string (seconds-to-time seconds))))
  307. (defmacro org-eval-in-environment (environment form)
  308. (declare (debug (form form)) (indent 1))
  309. `(eval (list 'let ,environment ',form)))
  310. (defun org-make-parameter-alist (flat)
  311. "Return alist based on FLAT.
  312. FLAT is a list with alternating symbol names and values. The
  313. returned alist is a list of lists with the symbol name in car and
  314. the value in cdr."
  315. (when flat
  316. (cons (list (car flat) (cadr flat))
  317. (org-make-parameter-alist (cddr flat)))))
  318. ;;;###autoload
  319. (defmacro org-load-noerror-mustsuffix (file)
  320. "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
  321. `(load ,file 'noerror nil nil 'mustsuffix))
  322. (provide 'org-macs)
  323. ;;; org-macs.el ends here