org-macs.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. ;;; String manipulation
  149. (defsubst org-trim (s &optional keep-lead)
  150. "Remove whitespace at the beginning and the end of string S.
  151. When optional argument KEEP-LEAD is non-nil, removing blank lines
  152. at the beginning of the string does not affect leading indentation."
  153. (replace-regexp-in-string
  154. (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
  155. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  156. (defun org-string-nw-p (s)
  157. "Return S if S is a string containing a non-blank character.
  158. Otherwise, return nil."
  159. (and (stringp s)
  160. (string-match-p "[^ \r\t\n]" s)
  161. s))
  162. (defun org-split-string (string &optional separators)
  163. "Splits STRING into substrings at SEPARATORS.
  164. SEPARATORS is a regular expression. When nil, it defaults to
  165. \"[ \f\t\n\r\v]+\".
  166. Unlike to `split-string', matching SEPARATORS at the beginning
  167. and end of string are ignored."
  168. (let ((separators (or separators "[ \f\t\n\r\v]+")))
  169. (when (string-match (concat "\\`" separators) string)
  170. (setq string (replace-match "" nil nil string)))
  171. (when (string-match (concat separators "\\'") string)
  172. (setq string (replace-match "" nil nil string)))
  173. (split-string string separators)))
  174. (defun org-string-display (string)
  175. "Return STRING as it is displayed in the current buffer.
  176. This function takes into consideration `invisible' and `display'
  177. text properties."
  178. (let* ((build-from-parts
  179. (lambda (s property filter)
  180. ;; Build a new string out of string S. On every group of
  181. ;; contiguous characters with the same PROPERTY value,
  182. ;; call FILTER on the properties list at the beginning of
  183. ;; the group. If it returns a string, replace the
  184. ;; characters in the group with it. Otherwise, preserve
  185. ;; those characters.
  186. (let ((len (length s))
  187. (new "")
  188. (i 0)
  189. (cursor 0))
  190. (while (setq i (text-property-not-all i len property nil s))
  191. (let ((end (next-single-property-change i property s len))
  192. (value (funcall filter (text-properties-at i s))))
  193. (when value
  194. (setq new (concat new (substring s cursor i) value))
  195. (setq cursor end))
  196. (setq i end)))
  197. (concat new (substring s cursor)))))
  198. (prune-invisible
  199. (lambda (s)
  200. (funcall build-from-parts s 'invisible
  201. (lambda (props)
  202. ;; If `invisible' property in PROPS means text
  203. ;; is to be invisible, return the empty string.
  204. ;; Otherwise return nil so that the part is
  205. ;; skipped.
  206. (and (or (eq t buffer-invisibility-spec)
  207. (assoc-string (plist-get props 'invisible)
  208. buffer-invisibility-spec))
  209. "")))))
  210. (replace-display
  211. (lambda (s)
  212. (funcall build-from-parts s 'display
  213. (lambda (props)
  214. ;; If there is any string specification in
  215. ;; `display' property return it. Also attach
  216. ;; other text properties on the part to that
  217. ;; string (face...).
  218. (let* ((display (plist-get props 'display))
  219. (value (if (stringp display) display
  220. (cl-some #'stringp display))))
  221. (when value
  222. (apply
  223. #'propertize
  224. ;; Displayed string could contain
  225. ;; invisible parts, but no nested display.
  226. (funcall prune-invisible value)
  227. (plist-put props
  228. 'display
  229. (and (not (stringp display))
  230. (cl-remove-if #'stringp
  231. display)))))))))))
  232. ;; `display' property overrides `invisible' one. So we first
  233. ;; replace characters with `display' property. Then we remove
  234. ;; invisible characters.
  235. (funcall prune-invisible (funcall replace-display string))))
  236. (defun org-string-width (string)
  237. "Return width of STRING when displayed in the current buffer.
  238. Unlike to `string-width', this function takes into consideration
  239. `invisible' and `display' text properties."
  240. (string-width (org-string-display string)))
  241. (defun org-not-nil (v)
  242. "If V not nil, and also not the string \"nil\", then return V.
  243. Otherwise return nil."
  244. (and v (not (equal v "nil")) v))
  245. (defun org-unbracket-string (pre post string)
  246. "Remove PRE/POST from the beginning/end of STRING.
  247. Both PRE and POST must be pre-/suffixes of STRING, or neither is
  248. removed."
  249. (if (and (string-prefix-p pre string)
  250. (string-suffix-p post string))
  251. (substring string (length pre) (- (length post)))
  252. string))
  253. (defsubst org-current-line-string (&optional to-here)
  254. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  255. ;;; List manipulation
  256. (defsubst org-get-alist-option (option key)
  257. (cond ((eq key t) t)
  258. ((eq option t) t)
  259. ((assoc key option) (cdr (assoc key option)))
  260. (t (let ((r (cdr (assq 'default option))))
  261. (if (listp r) (delq nil r) r)))))
  262. (defsubst org-last (list)
  263. "Return the last element of LIST."
  264. (car (last list)))
  265. (defun org-plist-delete (plist property)
  266. "Delete PROPERTY from PLIST.
  267. This is in contrast to merely setting it to 0."
  268. (let (p)
  269. (while plist
  270. (if (not (eq property (car plist)))
  271. (setq p (plist-put p (car plist) (nth 1 plist))))
  272. (setq plist (cddr plist)))
  273. p))
  274. (defsubst org-uniquify (list)
  275. "Non-destructively remove duplicate elements from LIST."
  276. (let ((res (copy-sequence list))) (delete-dups res)))
  277. ;;; Regexp matching
  278. (defsubst org-pos-in-match-range (pos n)
  279. (and (match-beginning n)
  280. (<= (match-beginning n) pos)
  281. (>= (match-end n) pos)))
  282. (defun org-match-line (regexp)
  283. "Match REGEXP at the beginning of the current line."
  284. (save-excursion
  285. (beginning-of-line)
  286. (looking-at regexp)))
  287. ;;; Motion
  288. (defsubst org-goto-line (N)
  289. (save-restriction
  290. (widen)
  291. (goto-char (point-min))
  292. (forward-line (1- N))))
  293. (defsubst org-current-line (&optional pos)
  294. (save-excursion
  295. (and pos (goto-char pos))
  296. ;; works also in narrowed buffer, because we start at 1, not point-min
  297. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  298. ;;; Text properties
  299. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  300. rear-nonsticky t mouse-map t fontified t
  301. org-emphasis t)
  302. "Properties to remove when a string without properties is wanted.")
  303. (defsubst org-no-properties (s &optional restricted)
  304. "Remove all text properties from string S.
  305. When RESTRICTED is non-nil, only remove the properties listed
  306. in `org-rm-props'."
  307. (if restricted (remove-text-properties 0 (length s) org-rm-props s)
  308. (set-text-properties 0 (length s) nil s))
  309. s)
  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. (defsubst org-get-at-bol (property)
  319. "Get text property PROPERTY at the beginning of line."
  320. (get-text-property (point-at-bol) property))
  321. ;;; Local variables
  322. (defconst org-unique-local-variables
  323. '(org-element--cache
  324. org-element--cache-objects
  325. org-element--cache-sync-keys
  326. org-element--cache-sync-requests
  327. org-element--cache-sync-timer)
  328. "List of local variables that cannot be transferred to another buffer.")
  329. (defun org-get-local-variables ()
  330. "Return a list of all local variables in an Org mode buffer."
  331. (delq nil
  332. (mapcar
  333. (lambda (x)
  334. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  335. (name (car binding)))
  336. (and (not (get name 'org-state))
  337. (not (memq name org-unique-local-variables))
  338. (string-match-p
  339. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  340. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  341. (symbol-name name))
  342. binding)))
  343. (with-temp-buffer
  344. (org-mode)
  345. (buffer-local-variables)))))
  346. (defun org-clone-local-variables (from-buffer &optional regexp)
  347. "Clone local variables from FROM-BUFFER.
  348. Optional argument REGEXP selects variables to clone."
  349. (dolist (pair (buffer-local-variables from-buffer))
  350. (pcase pair
  351. (`(,name . ,value) ;ignore unbound variables
  352. (when (and (not (memq name org-unique-local-variables))
  353. (or (null regexp) (string-match-p regexp (symbol-name name))))
  354. (ignore-errors (set (make-local-variable name) value)))))))
  355. ;;; Miscellaneous
  356. (defsubst org-check-external-command (cmd &optional use no-error)
  357. "Check if external program CMD for USE exists, error if not.
  358. When the program does exist, return its path.
  359. When it does not exist and NO-ERROR is set, return nil.
  360. Otherwise, throw an error. The optional argument USE can describe what this
  361. program is needed for, so that the error message can be more informative."
  362. (or (executable-find cmd)
  363. (if no-error
  364. nil
  365. (error "Can't find `%s'%s" cmd
  366. (if use (format " (%s)" use) "")))))
  367. (defun org-let (list &rest body)
  368. (eval (cons 'let (cons list body))))
  369. (put 'org-let 'lisp-indent-function 1)
  370. (defun org-let2 (list1 list2 &rest body)
  371. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  372. (put 'org-let2 'lisp-indent-function 2)
  373. (defsubst org-call-with-arg (command arg)
  374. "Call COMMAND interactively, but pretend prefix arg was ARG."
  375. (let ((current-prefix-arg arg)) (call-interactively command)))
  376. (defvar org-outline-regexp) ; defined in org.el
  377. (defvar org-odd-levels-only) ; defined in org.el
  378. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  379. (defun org-get-limited-outline-regexp ()
  380. "Return outline-regexp with limited number of levels.
  381. The number of levels is controlled by `org-inlinetask-min-level'"
  382. (cond ((not (derived-mode-p 'org-mode))
  383. outline-regexp)
  384. ((not (featurep 'org-inlinetask))
  385. org-outline-regexp)
  386. (t
  387. (let* ((limit-level (1- org-inlinetask-min-level))
  388. (nstars (if org-odd-levels-only
  389. (1- (* limit-level 2))
  390. limit-level)))
  391. (format "\\*\\{1,%d\\} " nstars)))))
  392. (defun org-read-function (prompt &optional allow-empty?)
  393. "Prompt for a function.
  394. If ALLOW-EMPTY? is non-nil, return nil rather than raising an
  395. error when the user input is empty."
  396. (let ((func (completing-read prompt obarray #'fboundp t)))
  397. (cond ((not (string= func ""))
  398. (intern func))
  399. (allow-empty? nil)
  400. (t (user-error "Empty input is not valid")))))
  401. (provide 'org-macs)
  402. ;;; org-macs.el ends here