org-macs.el 15 KB

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