org-macs.el 13 KB

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