org-macs.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2017 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 <https://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. ;;; Macros
  28. (defmacro org-with-gensyms (symbols &rest body)
  29. (declare (debug (sexp body)) (indent 1))
  30. `(let ,(mapcar (lambda (s)
  31. `(,s (make-symbol (concat "--" (symbol-name ',s)))))
  32. symbols)
  33. ,@body))
  34. (defmacro org-preserve-lc (&rest body)
  35. (declare (debug (body)))
  36. (org-with-gensyms (line col)
  37. `(let ((,line (org-current-line))
  38. (,col (current-column)))
  39. (unwind-protect
  40. (progn ,@body)
  41. (org-goto-line ,line)
  42. (org-move-to-column ,col)))))
  43. ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
  44. ;; `org-unmodified' to ignore real text modifications
  45. (defmacro org-unmodified (&rest body)
  46. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  47. (declare (debug (body)))
  48. (org-with-gensyms (was-modified)
  49. `(let ((,was-modified (buffer-modified-p)))
  50. (unwind-protect
  51. (let ((buffer-undo-list t)
  52. (inhibit-modification-hooks t))
  53. ,@body)
  54. (set-buffer-modified-p ,was-modified)))))
  55. (defmacro org-without-partial-completion (&rest body)
  56. (declare (debug (body)))
  57. `(if (and (boundp 'partial-completion-mode)
  58. partial-completion-mode
  59. (fboundp 'partial-completion-mode))
  60. (unwind-protect
  61. (progn
  62. (partial-completion-mode -1)
  63. ,@body)
  64. (partial-completion-mode 1))
  65. ,@body))
  66. (defmacro org-with-point-at (pom &rest body)
  67. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  68. (declare (debug (form body)) (indent 1))
  69. (org-with-gensyms (mpom)
  70. `(let ((,mpom ,pom))
  71. (save-excursion
  72. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  73. (org-with-wide-buffer
  74. (goto-char (or ,mpom (point)))
  75. ,@body)))))
  76. (defmacro org-with-remote-undo (buffer &rest body)
  77. "Execute BODY while recording undo information in two buffers."
  78. (declare (debug (form body)) (indent 1))
  79. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  80. `(let ((,cline (org-current-line))
  81. (,cmd this-command)
  82. (,buf1 (current-buffer))
  83. (,buf2 ,buffer)
  84. (,undo1 buffer-undo-list)
  85. (,undo2 (with-current-buffer ,buffer buffer-undo-list))
  86. ,c1 ,c2)
  87. ,@body
  88. (when org-agenda-allow-remote-undo
  89. (setq ,c1 (org-verify-change-for-undo
  90. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  91. ,c2 (org-verify-change-for-undo
  92. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  93. (when (or ,c1 ,c2)
  94. ;; make sure there are undo boundaries
  95. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  96. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  97. ;; remember which buffer to undo
  98. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  99. org-agenda-undo-list))))))
  100. (defmacro org-no-read-only (&rest body)
  101. "Inhibit read-only for BODY."
  102. (declare (debug (body)))
  103. `(let ((inhibit-read-only t)) ,@body))
  104. (defmacro org-save-outline-visibility (use-markers &rest body)
  105. "Save and restore outline visibility around BODY.
  106. If USE-MARKERS is non-nil, use markers for the positions.
  107. This means that the buffer may change while running BODY,
  108. but it also means that the buffer should stay alive
  109. during the operation, because otherwise all these markers will
  110. point nowhere."
  111. (declare (debug (form body)) (indent 1))
  112. (org-with-gensyms (data)
  113. `(let ((,data (org-outline-overlay-data ,use-markers)))
  114. (unwind-protect
  115. (prog1 (progn ,@body)
  116. (org-set-outline-overlay-data ,data))
  117. (when ,use-markers
  118. (dolist (c ,data)
  119. (when (markerp (car c)) (move-marker (car c) nil))
  120. (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
  121. (defmacro org-with-wide-buffer (&rest body)
  122. "Execute body while temporarily widening the buffer."
  123. (declare (debug (body)))
  124. `(save-excursion
  125. (save-restriction
  126. (widen)
  127. ,@body)))
  128. (defmacro org-with-limited-levels (&rest body)
  129. "Execute BODY with limited number of outline levels."
  130. (declare (debug (body)))
  131. `(progn
  132. (defvar org-called-with-limited-levels)
  133. (defvar org-outline-regexp)
  134. (defvar outline-regexp)
  135. (defvar org-outline-regexp-bol)
  136. (let* ((org-called-with-limited-levels t)
  137. (org-outline-regexp (org-get-limited-outline-regexp))
  138. (outline-regexp org-outline-regexp)
  139. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  140. ,@body)))
  141. (defmacro org-eval-in-environment (environment form)
  142. (declare (debug (form form)) (indent 1))
  143. `(eval (list 'let ,environment ',form)))
  144. ;;;###autoload
  145. (defmacro org-load-noerror-mustsuffix (file)
  146. "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
  147. `(load ,file 'noerror nil nil 'mustsuffix))
  148. ;;; Logic
  149. (defsubst org-xor (a b)
  150. "Exclusive `or'."
  151. (if a (not b) b))
  152. ;;; String manipulation
  153. (defsubst org-trim (s &optional keep-lead)
  154. "Remove whitespace at the beginning and the end of string S.
  155. When optional argument KEEP-LEAD is non-nil, removing blank lines
  156. at the beginning of the string does not affect leading indentation."
  157. (replace-regexp-in-string
  158. (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
  159. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  160. (defun org-string-nw-p (s)
  161. "Return S if S is a string containing a non-blank character.
  162. Otherwise, return nil."
  163. (and (stringp s)
  164. (string-match-p "[^ \r\t\n]" s)
  165. s))
  166. (defun org-split-string (string &optional separators)
  167. "Splits STRING into substrings at SEPARATORS.
  168. SEPARATORS is a regular expression. When nil, it defaults to
  169. \"[ \f\t\n\r\v]+\".
  170. Unlike to `split-string', matching SEPARATORS at the beginning
  171. and end of string are ignored."
  172. (let ((separators (or separators "[ \f\t\n\r\v]+")))
  173. (when (string-match (concat "\\`" separators) string)
  174. (setq string (replace-match "" nil nil string)))
  175. (when (string-match (concat separators "\\'") string)
  176. (setq string (replace-match "" nil nil string)))
  177. (split-string string separators)))
  178. (defun org-string-display (string)
  179. "Return STRING as it is displayed in the current buffer.
  180. This function takes into consideration `invisible' and `display'
  181. text properties."
  182. (let* ((build-from-parts
  183. (lambda (s property filter)
  184. ;; Build a new string out of string S. On every group of
  185. ;; contiguous characters with the same PROPERTY value,
  186. ;; call FILTER on the properties list at the beginning of
  187. ;; the group. If it returns a string, replace the
  188. ;; characters in the group with it. Otherwise, preserve
  189. ;; those characters.
  190. (let ((len (length s))
  191. (new "")
  192. (i 0)
  193. (cursor 0))
  194. (while (setq i (text-property-not-all i len property nil s))
  195. (let ((end (next-single-property-change i property s len))
  196. (value (funcall filter (text-properties-at i s))))
  197. (when value
  198. (setq new (concat new (substring s cursor i) value))
  199. (setq cursor end))
  200. (setq i end)))
  201. (concat new (substring s cursor)))))
  202. (prune-invisible
  203. (lambda (s)
  204. (funcall build-from-parts s 'invisible
  205. (lambda (props)
  206. ;; If `invisible' property in PROPS means text
  207. ;; is to be invisible, return the empty string.
  208. ;; Otherwise return nil so that the part is
  209. ;; skipped.
  210. (and (or (eq t buffer-invisibility-spec)
  211. (assoc-string (plist-get props 'invisible)
  212. buffer-invisibility-spec))
  213. "")))))
  214. (replace-display
  215. (lambda (s)
  216. (funcall build-from-parts s 'display
  217. (lambda (props)
  218. ;; If there is any string specification in
  219. ;; `display' property return it. Also attach
  220. ;; other text properties on the part to that
  221. ;; string (face...).
  222. (let* ((display (plist-get props 'display))
  223. (value (if (stringp display) display
  224. (cl-some #'stringp display))))
  225. (when value
  226. (apply
  227. #'propertize
  228. ;; Displayed string could contain
  229. ;; invisible parts, but no nested display.
  230. (funcall prune-invisible value)
  231. (plist-put props
  232. 'display
  233. (and (not (stringp display))
  234. (cl-remove-if #'stringp
  235. display)))))))))))
  236. ;; `display' property overrides `invisible' one. So we first
  237. ;; replace characters with `display' property. Then we remove
  238. ;; invisible characters.
  239. (funcall prune-invisible (funcall replace-display string))))
  240. (defun org-string-width (string)
  241. "Return width of STRING when displayed in the current buffer.
  242. Unlike to `string-width', this function takes into consideration
  243. `invisible' and `display' text properties."
  244. (string-width (org-string-display string)))
  245. (defun org-not-nil (v)
  246. "If V not nil, and also not the string \"nil\", then return V.
  247. Otherwise return nil."
  248. (and v (not (equal v "nil")) v))
  249. (defun org-unbracket-string (pre post string)
  250. "Remove PRE/POST from the beginning/end of STRING.
  251. Both PRE and POST must be pre-/suffixes of STRING, or neither is
  252. removed."
  253. (if (and (string-prefix-p pre string)
  254. (string-suffix-p post string))
  255. (substring string (length pre) (- (length post)))
  256. string))
  257. (defsubst org-current-line-string (&optional to-here)
  258. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  259. ;;; List manipulation
  260. (defsubst org-get-alist-option (option key)
  261. (cond ((eq key t) t)
  262. ((eq option t) t)
  263. ((assoc key option) (cdr (assoc key option)))
  264. (t (let ((r (cdr (assq 'default option))))
  265. (if (listp r) (delq nil r) r)))))
  266. (defsubst org-last (list)
  267. "Return the last element of LIST."
  268. (car (last list)))
  269. (defun org-plist-delete (plist property)
  270. "Delete PROPERTY from PLIST.
  271. This is in contrast to merely setting it to 0."
  272. (let (p)
  273. (while plist
  274. (if (not (eq property (car plist)))
  275. (setq p (plist-put p (car plist) (nth 1 plist))))
  276. (setq plist (cddr plist)))
  277. p))
  278. (defsubst org-uniquify (list)
  279. "Non-destructively remove duplicate elements from LIST."
  280. (let ((res (copy-sequence list))) (delete-dups res)))
  281. ;;; Regexp matching
  282. (defsubst org-pos-in-match-range (pos n)
  283. (and (match-beginning n)
  284. (<= (match-beginning n) pos)
  285. (>= (match-end n) pos)))
  286. (defun org-match-line (regexp)
  287. "Match REGEXP at the beginning of the current line."
  288. (save-excursion
  289. (beginning-of-line)
  290. (looking-at regexp)))
  291. ;;; Motion
  292. (defsubst org-goto-line (N)
  293. (save-restriction
  294. (widen)
  295. (goto-char (point-min))
  296. (forward-line (1- N))))
  297. (defsubst org-current-line (&optional pos)
  298. (save-excursion
  299. (and pos (goto-char pos))
  300. ;; works also in narrowed buffer, because we start at 1, not point-min
  301. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  302. ;;; Text properties
  303. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  304. rear-nonsticky t mouse-map t fontified t
  305. org-emphasis t)
  306. "Properties to remove when a string without properties is wanted.")
  307. (defsubst org-no-properties (s &optional restricted)
  308. "Remove all text properties from string S.
  309. When RESTRICTED is non-nil, only remove the properties listed
  310. in `org-rm-props'."
  311. (if restricted (remove-text-properties 0 (length s) org-rm-props s)
  312. (set-text-properties 0 (length s) nil s))
  313. s)
  314. (defun org-make-parameter-alist (flat)
  315. "Return alist based on FLAT.
  316. FLAT is a list with alternating symbol names and values. The
  317. returned alist is a list of lists with the symbol name in car and
  318. the value in cdr."
  319. (when flat
  320. (cons (list (car flat) (cadr flat))
  321. (org-make-parameter-alist (cddr flat)))))
  322. (defsubst org-get-at-bol (property)
  323. "Get text property PROPERTY at the beginning of line."
  324. (get-text-property (point-at-bol) property))
  325. ;;; Local variables
  326. (defconst org-unique-local-variables
  327. '(org-element--cache
  328. org-element--cache-objects
  329. org-element--cache-sync-keys
  330. org-element--cache-sync-requests
  331. org-element--cache-sync-timer)
  332. "List of local variables that cannot be transferred to another buffer.")
  333. (defun org-get-local-variables ()
  334. "Return a list of all local variables in an Org mode buffer."
  335. (delq nil
  336. (mapcar
  337. (lambda (x)
  338. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  339. (name (car binding)))
  340. (and (not (get name 'org-state))
  341. (not (memq name org-unique-local-variables))
  342. (string-match-p
  343. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  344. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  345. (symbol-name name))
  346. binding)))
  347. (with-temp-buffer
  348. (org-mode)
  349. (buffer-local-variables)))))
  350. (defun org-clone-local-variables (from-buffer &optional regexp)
  351. "Clone local variables from FROM-BUFFER.
  352. Optional argument REGEXP selects variables to clone."
  353. (dolist (pair (buffer-local-variables from-buffer))
  354. (pcase pair
  355. (`(,name . ,value) ;ignore unbound variables
  356. (when (and (not (memq name org-unique-local-variables))
  357. (or (null regexp) (string-match-p regexp (symbol-name name))))
  358. (ignore-errors (set (make-local-variable name) value)))))))
  359. ;;; Miscellaneous
  360. (defsubst org-check-external-command (cmd &optional use no-error)
  361. "Check if external program CMD for USE exists, error if not.
  362. When the program does exist, return its path.
  363. When it does not exist and NO-ERROR is set, return nil.
  364. Otherwise, throw an error. The optional argument USE can describe what this
  365. program is needed for, so that the error message can be more informative."
  366. (or (executable-find cmd)
  367. (if no-error
  368. nil
  369. (error "Can't find `%s'%s" cmd
  370. (if use (format " (%s)" use) "")))))
  371. (defun org-let (list &rest body)
  372. (eval (cons 'let (cons list body))))
  373. (put 'org-let 'lisp-indent-function 1)
  374. (defun org-let2 (list1 list2 &rest body)
  375. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  376. (put 'org-let2 'lisp-indent-function 2)
  377. (defsubst org-call-with-arg (command arg)
  378. "Call COMMAND interactively, but pretend prefix arg was ARG."
  379. (let ((current-prefix-arg arg)) (call-interactively command)))
  380. (defvar org-outline-regexp) ; defined in org.el
  381. (defvar org-odd-levels-only) ; defined in org.el
  382. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  383. (defun org-get-limited-outline-regexp ()
  384. "Return outline-regexp with limited number of levels.
  385. The number of levels is controlled by `org-inlinetask-min-level'"
  386. (cond ((not (derived-mode-p 'org-mode))
  387. outline-regexp)
  388. ((not (featurep 'org-inlinetask))
  389. org-outline-regexp)
  390. (t
  391. (let* ((limit-level (1- org-inlinetask-min-level))
  392. (nstars (if org-odd-levels-only
  393. (1- (* limit-level 2))
  394. limit-level)))
  395. (format "\\*\\{1,%d\\} " nstars)))))
  396. (defun org-read-function (prompt &optional allow-empty?)
  397. "Prompt for a function.
  398. If ALLOW-EMPTY? is non-nil, return nil rather than raising an
  399. error when the user input is empty."
  400. (let ((func (completing-read prompt obarray #'fboundp t)))
  401. (cond ((not (string= func ""))
  402. (intern func))
  403. (allow-empty? nil)
  404. (t (user-error "Empty input is not valid")))))
  405. (provide 'org-macs)
  406. ;;; org-macs.el ends here