org-macs.el 15 KB

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