org-macs.el 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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: https://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. (declare-function org-defkey "org" (keymap key def))
  28. ;;; Macros
  29. (defmacro org-with-gensyms (symbols &rest body)
  30. (declare (debug (sexp body)) (indent 1))
  31. `(let ,(mapcar (lambda (s)
  32. `(,s (make-symbol (concat "--" (symbol-name ',s)))))
  33. symbols)
  34. ,@body))
  35. (defmacro org-preserve-lc (&rest body)
  36. (declare (debug (body)))
  37. (org-with-gensyms (line col)
  38. `(let ((,line (org-current-line))
  39. (,col (current-column)))
  40. (unwind-protect
  41. (progn ,@body)
  42. (org-goto-line ,line)
  43. (org-move-to-column ,col)))))
  44. ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
  45. ;; `org-unmodified' to ignore real text modifications
  46. (defmacro org-unmodified (&rest body)
  47. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  48. (declare (debug (body)))
  49. (org-with-gensyms (was-modified)
  50. `(let ((,was-modified (buffer-modified-p)))
  51. (unwind-protect
  52. (let ((buffer-undo-list t)
  53. (inhibit-modification-hooks t))
  54. ,@body)
  55. (set-buffer-modified-p ,was-modified)))))
  56. (defmacro org-without-partial-completion (&rest body)
  57. (declare (debug (body)))
  58. `(if (and (boundp 'partial-completion-mode)
  59. partial-completion-mode
  60. (fboundp 'partial-completion-mode))
  61. (unwind-protect
  62. (progn
  63. (partial-completion-mode -1)
  64. ,@body)
  65. (partial-completion-mode 1))
  66. ,@body))
  67. (defmacro org-with-point-at (pom &rest body)
  68. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  69. (declare (debug (form body)) (indent 1))
  70. (org-with-gensyms (mpom)
  71. `(let ((,mpom ,pom))
  72. (save-excursion
  73. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  74. (org-with-wide-buffer
  75. (goto-char (or ,mpom (point)))
  76. ,@body)))))
  77. (defmacro org-with-remote-undo (buffer &rest body)
  78. "Execute BODY while recording undo information in two buffers."
  79. (declare (debug (form body)) (indent 1))
  80. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  81. `(let ((,cline (org-current-line))
  82. (,cmd this-command)
  83. (,buf1 (current-buffer))
  84. (,buf2 ,buffer)
  85. (,undo1 buffer-undo-list)
  86. (,undo2 (with-current-buffer ,buffer buffer-undo-list))
  87. ,c1 ,c2)
  88. ,@body
  89. (when org-agenda-allow-remote-undo
  90. (setq ,c1 (org-verify-change-for-undo
  91. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  92. ,c2 (org-verify-change-for-undo
  93. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  94. (when (or ,c1 ,c2)
  95. ;; make sure there are undo boundaries
  96. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  97. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  98. ;; remember which buffer to undo
  99. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  100. org-agenda-undo-list))))))
  101. (defmacro org-no-read-only (&rest body)
  102. "Inhibit read-only for BODY."
  103. (declare (debug (body)))
  104. `(let ((inhibit-read-only t)) ,@body))
  105. (defmacro org-save-outline-visibility (use-markers &rest body)
  106. "Save and restore outline visibility around BODY.
  107. If USE-MARKERS is non-nil, use markers for the positions.
  108. This means that the buffer may change while running BODY,
  109. but it also means that the buffer should stay alive
  110. during the operation, because otherwise all these markers will
  111. point nowhere."
  112. (declare (debug (form body)) (indent 1))
  113. (org-with-gensyms (data)
  114. `(let ((,data (org-outline-overlay-data ,use-markers)))
  115. (unwind-protect
  116. (prog1 (progn ,@body)
  117. (org-set-outline-overlay-data ,data))
  118. (when ,use-markers
  119. (dolist (c ,data)
  120. (when (markerp (car c)) (move-marker (car c) nil))
  121. (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
  122. (defmacro org-with-wide-buffer (&rest body)
  123. "Execute body while temporarily widening the buffer."
  124. (declare (debug (body)))
  125. `(save-excursion
  126. (save-restriction
  127. (widen)
  128. ,@body)))
  129. (defmacro org-with-limited-levels (&rest body)
  130. "Execute BODY with limited number of outline levels."
  131. (declare (debug (body)))
  132. `(progn
  133. (defvar org-called-with-limited-levels)
  134. (defvar org-outline-regexp)
  135. (defvar outline-regexp)
  136. (defvar org-outline-regexp-bol)
  137. (let* ((org-called-with-limited-levels t)
  138. (org-outline-regexp (org-get-limited-outline-regexp))
  139. (outline-regexp org-outline-regexp)
  140. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  141. ,@body)))
  142. (defmacro org-eval-in-environment (environment form)
  143. (declare (debug (form form)) (indent 1))
  144. `(eval (list 'let ,environment ',form)))
  145. ;;;###autoload
  146. (defmacro org-load-noerror-mustsuffix (file)
  147. "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
  148. `(load ,file 'noerror nil nil 'mustsuffix))
  149. (defmacro org-preserve-local-variables (&rest body)
  150. "Execute BODY while preserving local variables."
  151. (declare (debug (body)))
  152. `(let ((local-variables
  153. (org-with-wide-buffer
  154. (goto-char (point-max))
  155. (let ((case-fold-search t))
  156. (and (re-search-backward "^[ \t]*# +Local Variables:"
  157. (max (- (point) 3000) 1)
  158. t)
  159. (delete-and-extract-region (point) (point-max)))))))
  160. (unwind-protect (progn ,@body)
  161. (when local-variables
  162. (org-with-wide-buffer
  163. (goto-char (point-max))
  164. (unless (bolp) (insert "\n"))
  165. (insert local-variables))))))
  166. ;;; Buffer
  167. (defun org-base-buffer (buffer)
  168. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  169. (if (not buffer)
  170. buffer
  171. (or (buffer-base-buffer buffer)
  172. buffer)))
  173. (defun org-find-base-buffer-visiting (file)
  174. "Like `find-buffer-visiting' but always return the base buffer and
  175. not an indirect buffer."
  176. (let ((buf (or (get-file-buffer file)
  177. (find-buffer-visiting file))))
  178. (if buf
  179. (or (buffer-base-buffer buf) buf)
  180. nil)))
  181. ;;; Input
  182. (defun org-read-function (prompt &optional allow-empty?)
  183. "Prompt for a function.
  184. If ALLOW-EMPTY? is non-nil, return nil rather than raising an
  185. error when the user input is empty."
  186. (let ((func (completing-read prompt obarray #'fboundp t)))
  187. (cond ((not (string= func ""))
  188. (intern func))
  189. (allow-empty? nil)
  190. (t (user-error "Empty input is not valid")))))
  191. (defun org-completing-read (&rest args)
  192. "Completing-read with SPACE being a normal character."
  193. (let ((enable-recursive-minibuffers t)
  194. (minibuffer-local-completion-map
  195. (copy-keymap minibuffer-local-completion-map)))
  196. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  197. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  198. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  199. 'org-time-stamp-inactive)
  200. (apply #'completing-read args)))
  201. ;;; Logic
  202. (defsubst org-xor (a b)
  203. "Exclusive `or'."
  204. (if a (not b) b))
  205. ;;; Overlays
  206. (defun org-overlay-display (ovl text &optional face evap)
  207. "Make overlay OVL display TEXT with face FACE."
  208. (overlay-put ovl 'display text)
  209. (if face (overlay-put ovl 'face face))
  210. (if evap (overlay-put ovl 'evaporate t)))
  211. (defun org-overlay-before-string (ovl text &optional face evap)
  212. "Make overlay OVL display TEXT with face FACE."
  213. (if face (org-add-props text nil 'face face))
  214. (overlay-put ovl 'before-string text)
  215. (if evap (overlay-put ovl 'evaporate t)))
  216. (defun org-find-overlays (prop &optional pos delete)
  217. "Find all overlays specifying PROP at POS or point.
  218. If DELETE is non-nil, delete all those overlays."
  219. (let (found)
  220. (dolist (ov (overlays-at (or pos (point))) found)
  221. (cond ((not (overlay-get ov prop)))
  222. (delete (delete-overlay ov))
  223. (t (push ov found))))))
  224. ;;; Indentation
  225. (defun org-get-indentation (&optional line)
  226. "Get the indentation of the current line, interpreting tabs.
  227. When LINE is given, assume it represents a line and compute its indentation."
  228. (if line
  229. (when (string-match "^ *" (org-remove-tabs line))
  230. (match-end 0))
  231. (save-excursion
  232. (beginning-of-line 1)
  233. (skip-chars-forward " \t")
  234. (current-column))))
  235. (defun org-do-remove-indentation (&optional n)
  236. "Remove the maximum common indentation from the buffer.
  237. When optional argument N is a positive integer, remove exactly
  238. that much characters from indentation, if possible. Return nil
  239. if it fails."
  240. (catch :exit
  241. (goto-char (point-min))
  242. ;; Find maximum common indentation, if not specified.
  243. (let ((n (or n
  244. (let ((min-ind (point-max)))
  245. (save-excursion
  246. (while (re-search-forward "^[ \t]*\\S-" nil t)
  247. (let ((ind (1- (current-column))))
  248. (if (zerop ind) (throw :exit nil)
  249. (setq min-ind (min min-ind ind))))))
  250. min-ind))))
  251. (if (zerop n) (throw :exit nil)
  252. ;; Remove exactly N indentation, but give up if not possible.
  253. (while (not (eobp))
  254. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  255. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  256. ((< ind n) (throw :exit nil))
  257. (t (indent-line-to (- ind n))))
  258. (forward-line)))
  259. ;; Signal success.
  260. t))))
  261. ;;; String manipulation
  262. (defsubst org-trim (s &optional keep-lead)
  263. "Remove whitespace at the beginning and the end of string S.
  264. When optional argument KEEP-LEAD is non-nil, removing blank lines
  265. at the beginning of the string does not affect leading indentation."
  266. (replace-regexp-in-string
  267. (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
  268. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  269. (defun org-string-nw-p (s)
  270. "Return S if S is a string containing a non-blank character.
  271. Otherwise, return nil."
  272. (and (stringp s)
  273. (string-match-p "[^ \r\t\n]" s)
  274. s))
  275. (defun org-reverse-string (string)
  276. "Return the reverse of STRING."
  277. (apply #'string (nreverse (string-to-list string))))
  278. (defun org-split-string (string &optional separators)
  279. "Splits STRING into substrings at SEPARATORS.
  280. SEPARATORS is a regular expression. When nil, it defaults to
  281. \"[ \f\t\n\r\v]+\".
  282. Unlike `split-string', matching SEPARATORS at the beginning and
  283. end of string are ignored."
  284. (let ((separators (or separators "[ \f\t\n\r\v]+")))
  285. (when (string-match (concat "\\`" separators) string)
  286. (setq string (replace-match "" nil nil string)))
  287. (when (string-match (concat separators "\\'") string)
  288. (setq string (replace-match "" nil nil string)))
  289. (split-string string separators)))
  290. (defun org-string-display (string)
  291. "Return STRING as it is displayed in the current buffer.
  292. This function takes into consideration `invisible' and `display'
  293. text properties."
  294. (let* ((build-from-parts
  295. (lambda (s property filter)
  296. ;; Build a new string out of string S. On every group of
  297. ;; contiguous characters with the same PROPERTY value,
  298. ;; call FILTER on the properties list at the beginning of
  299. ;; the group. If it returns a string, replace the
  300. ;; characters in the group with it. Otherwise, preserve
  301. ;; those characters.
  302. (let ((len (length s))
  303. (new "")
  304. (i 0)
  305. (cursor 0))
  306. (while (setq i (text-property-not-all i len property nil s))
  307. (let ((end (next-single-property-change i property s len))
  308. (value (funcall filter (text-properties-at i s))))
  309. (when value
  310. (setq new (concat new (substring s cursor i) value))
  311. (setq cursor end))
  312. (setq i end)))
  313. (concat new (substring s cursor)))))
  314. (prune-invisible
  315. (lambda (s)
  316. (funcall build-from-parts s 'invisible
  317. (lambda (props)
  318. ;; If `invisible' property in PROPS means text
  319. ;; is to be invisible, return the empty string.
  320. ;; Otherwise return nil so that the part is
  321. ;; skipped.
  322. (and (or (eq t buffer-invisibility-spec)
  323. (assoc-string (plist-get props 'invisible)
  324. buffer-invisibility-spec))
  325. "")))))
  326. (replace-display
  327. (lambda (s)
  328. (funcall build-from-parts s 'display
  329. (lambda (props)
  330. ;; If there is any string specification in
  331. ;; `display' property return it. Also attach
  332. ;; other text properties on the part to that
  333. ;; string (face...).
  334. (let* ((display (plist-get props 'display))
  335. (value (if (stringp display) display
  336. (cl-some #'stringp display))))
  337. (when value
  338. (apply #'propertize
  339. ;; Displayed string could contain
  340. ;; invisible parts, but no nested
  341. ;; display.
  342. (funcall prune-invisible value)
  343. 'display
  344. (and (not (stringp display))
  345. (cl-remove-if #'stringp display))
  346. props))))))))
  347. ;; `display' property overrides `invisible' one. So we first
  348. ;; replace characters with `display' property. Then we remove
  349. ;; invisible characters.
  350. (funcall prune-invisible (funcall replace-display string))))
  351. (defun org-string-width (string)
  352. "Return width of STRING when displayed in the current buffer.
  353. Unlike `string-width', this function takes into consideration
  354. `invisible' and `display' text properties."
  355. (string-width (org-string-display string)))
  356. (defun org-not-nil (v)
  357. "If V not nil, and also not the string \"nil\", then return V.
  358. Otherwise return nil."
  359. (and v (not (equal v "nil")) v))
  360. (defun org-unbracket-string (pre post string)
  361. "Remove PRE/POST from the beginning/end of STRING.
  362. Both PRE and POST must be pre-/suffixes of STRING, or neither is
  363. removed."
  364. (if (and (string-prefix-p pre string)
  365. (string-suffix-p post string))
  366. (substring string (length pre) (- (length post)))
  367. string))
  368. (defsubst org-current-line-string (&optional to-here)
  369. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  370. (defun org-shorten-string (s maxlength)
  371. "Shorten string S so that it is no longer than MAXLENGTH characters.
  372. If the string is shorter or has length MAXLENGTH, just return the
  373. original string. If it is longer, the functions finds a space in the
  374. string, breaks this string off at that locations and adds three dots
  375. as ellipsis. Including the ellipsis, the string will not be longer
  376. than MAXLENGTH. If finding a good breaking point in the string does
  377. not work, the string is just chopped off in the middle of a word
  378. if necessary."
  379. (if (<= (length s) maxlength)
  380. s
  381. (let* ((n (max (- maxlength 4) 1))
  382. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  383. (if (string-match re s)
  384. (concat (match-string 1 s) "...")
  385. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  386. (defun org-remove-tabs (s &optional width)
  387. "Replace tabulators in S with spaces.
  388. Assumes that s is a single line, starting in column 0."
  389. (setq width (or width tab-width))
  390. (while (string-match "\t" s)
  391. (setq s (replace-match
  392. (make-string
  393. (- (* width (/ (+ (match-beginning 0) width) width))
  394. (match-beginning 0)) ?\ )
  395. t t s)))
  396. s)
  397. (defun org-wrap (string &optional width lines)
  398. "Wrap string to either a number of lines, or a width in characters.
  399. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  400. that costs. If there is a word longer than WIDTH, the text is actually
  401. wrapped to the length of that word.
  402. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  403. many lines, whatever width that takes.
  404. The return value is a list of lines, without newlines at the end."
  405. (let* ((words (split-string string))
  406. (maxword (apply 'max (mapcar 'org-string-width words)))
  407. w ll)
  408. (cond (width
  409. (org--do-wrap words (max maxword width)))
  410. (lines
  411. (setq w maxword)
  412. (setq ll (org--do-wrap words maxword))
  413. (if (<= (length ll) lines)
  414. ll
  415. (setq ll words)
  416. (while (> (length ll) lines)
  417. (setq w (1+ w))
  418. (setq ll (org--do-wrap words w)))
  419. ll))
  420. (t (error "Cannot wrap this")))))
  421. (defun org--do-wrap (words width)
  422. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  423. (let (lines line)
  424. (while words
  425. (setq line (pop words))
  426. (while (and words (< (+ (length line) (length (car words))) width))
  427. (setq line (concat line " " (pop words))))
  428. (setq lines (push line lines)))
  429. (nreverse lines)))
  430. (defun org-remove-indentation (code &optional n)
  431. "Remove maximum common indentation in string CODE and return it.
  432. N may optionally be the number of columns to remove. Return CODE
  433. as-is if removal failed."
  434. (with-temp-buffer
  435. (insert code)
  436. (if (org-do-remove-indentation n) (buffer-string) code)))
  437. ;;; List manipulation
  438. (defsubst org-get-alist-option (option key)
  439. (cond ((eq key t) t)
  440. ((eq option t) t)
  441. ((assoc key option) (cdr (assoc key option)))
  442. (t (let ((r (cdr (assq 'default option))))
  443. (if (listp r) (delq nil r) r)))))
  444. (defsubst org-last (list)
  445. "Return the last element of LIST."
  446. (car (last list)))
  447. (defsubst org-uniquify (list)
  448. "Non-destructively remove duplicate elements from LIST."
  449. (let ((res (copy-sequence list))) (delete-dups res)))
  450. (defun org-uniquify-alist (alist)
  451. "Merge elements of ALIST with the same key.
  452. For example, in this alist:
  453. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  454. => \\='((a 1 3) (b 2))
  455. merge (a 1) and (a 3) into (a 1 3).
  456. The function returns the new ALIST."
  457. (let (rtn)
  458. (dolist (e alist rtn)
  459. (let (n)
  460. (if (not (assoc (car e) rtn))
  461. (push e rtn)
  462. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  463. (setq rtn (assq-delete-all (car e) rtn))
  464. (push n rtn))))))
  465. (defun org-delete-all (elts list)
  466. "Remove all elements in ELTS from LIST.
  467. Comparison is done with `equal'. It is a destructive operation
  468. that may remove elements by altering the list structure."
  469. (while elts
  470. (setq list (delete (pop elts) list)))
  471. list)
  472. (defun org-plist-delete (plist property)
  473. "Delete PROPERTY from PLIST.
  474. This is in contrast to merely setting it to 0."
  475. (let (p)
  476. (while plist
  477. (if (not (eq property (car plist)))
  478. (setq p (plist-put p (car plist) (nth 1 plist))))
  479. (setq plist (cddr plist)))
  480. p))
  481. (defun org-combine-plists (&rest plists)
  482. "Create a single property list from all plists in PLISTS.
  483. The process starts by copying the first list, and then setting properties
  484. from the other lists. Settings in the last list are the most significant
  485. ones and overrule settings in the other lists."
  486. (let ((rtn (copy-sequence (pop plists)))
  487. p v ls)
  488. (while plists
  489. (setq ls (pop plists))
  490. (while ls
  491. (setq p (pop ls) v (pop ls))
  492. (setq rtn (plist-put rtn p v))))
  493. rtn))
  494. ;;; Regexp matching
  495. (defsubst org-pos-in-match-range (pos n)
  496. (and (match-beginning n)
  497. (<= (match-beginning n) pos)
  498. (>= (match-end n) pos)))
  499. (defun org-skip-whitespace ()
  500. "Skip over space, tabs and newline characters."
  501. (skip-chars-forward " \t\n\r"))
  502. (defun org-match-line (regexp)
  503. "Match REGEXP at the beginning of the current line."
  504. (save-excursion
  505. (beginning-of-line)
  506. (looking-at regexp)))
  507. (defun org-match-any-p (re list)
  508. "Non-nil if regexp RE matches an element in LIST."
  509. (cl-some (lambda (x) (string-match-p re x)) list))
  510. (defun org-in-regexp (regexp &optional nlines visually)
  511. "Check if point is inside a match of REGEXP.
  512. Normally only the current line is checked, but you can include
  513. NLINES extra lines around point into the search. If VISUALLY is
  514. set, require that the cursor is not after the match but really
  515. on, so that the block visually is on the match.
  516. Return nil or a cons cell (BEG . END) where BEG and END are,
  517. respectively, the positions at the beginning and the end of the
  518. match."
  519. (catch :exit
  520. (let ((pos (point))
  521. (eol (line-end-position (if nlines (1+ nlines) 1))))
  522. (save-excursion
  523. (beginning-of-line (- 1 (or nlines 0)))
  524. (while (and (re-search-forward regexp eol t)
  525. (<= (match-beginning 0) pos))
  526. (let ((end (match-end 0)))
  527. (when (or (> end pos) (and (= end pos) (not visually)))
  528. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  529. (defun org-point-in-group (point group &optional context)
  530. "Check if POINT is in match-group GROUP.
  531. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  532. match. If the match group does not exist or point is not inside it,
  533. return nil."
  534. (and (match-beginning group)
  535. (>= point (match-beginning group))
  536. (<= point (match-end group))
  537. (if context
  538. (list context (match-beginning group) (match-end group))
  539. t)))
  540. ;;; Motion
  541. (defsubst org-goto-line (N)
  542. (save-restriction
  543. (widen)
  544. (goto-char (point-min))
  545. (forward-line (1- N))))
  546. (defsubst org-current-line (&optional pos)
  547. (save-excursion
  548. (and pos (goto-char pos))
  549. ;; works also in narrowed buffer, because we start at 1, not point-min
  550. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  551. ;;; Text properties
  552. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  553. rear-nonsticky t mouse-map t fontified t
  554. org-emphasis t)
  555. "Properties to remove when a string without properties is wanted.")
  556. (defsubst org-no-properties (s &optional restricted)
  557. "Remove all text properties from string S.
  558. When RESTRICTED is non-nil, only remove the properties listed
  559. in `org-rm-props'."
  560. (if restricted (remove-text-properties 0 (length s) org-rm-props s)
  561. (set-text-properties 0 (length s) nil s))
  562. s)
  563. (defun org-add-props (string plist &rest props)
  564. "Add text properties to entire string, from beginning to end.
  565. PLIST may be a list of properties, PROPS are individual properties and values
  566. that will be added to PLIST. Returns the string that was modified."
  567. (declare (indent 2))
  568. (add-text-properties
  569. 0 (length string) (if props (append plist props) plist) string)
  570. string)
  571. (defun org-make-parameter-alist (flat)
  572. "Return alist based on FLAT.
  573. FLAT is a list with alternating symbol names and values. The
  574. returned alist is a list of lists with the symbol name in car and
  575. the value in cdr."
  576. (when flat
  577. (cons (list (car flat) (cadr flat))
  578. (org-make-parameter-alist (cddr flat)))))
  579. (defsubst org-get-at-bol (property)
  580. "Get text property PROPERTY at the beginning of line."
  581. (get-text-property (point-at-bol) property))
  582. (defun org-get-at-eol (property n)
  583. "Get text property PROPERTY at the end of line less N characters."
  584. (get-text-property (- (point-at-eol) n) property))
  585. (defun org-find-text-property-in-string (prop s)
  586. "Return the first non-nil value of property PROP in string S."
  587. (or (get-text-property 0 prop s)
  588. (get-text-property (or (next-single-property-change 0 prop s) 0)
  589. prop s)))
  590. (defun org-invisible-p (&optional pos)
  591. "Non-nil if the character after POS is invisible.
  592. If POS is nil, use `point' instead."
  593. (get-char-property (or pos (point)) 'invisible))
  594. (defun org-truely-invisible-p ()
  595. "Check if point is at a character currently not visible.
  596. This version does not only check the character property, but also
  597. `visible-mode'."
  598. (unless (bound-and-true-p visible-mode)
  599. (org-invisible-p)))
  600. (defun org-invisible-p2 ()
  601. "Check if point is at a character currently not visible.
  602. If the point is at EOL (and not at the beginning of a buffer too),
  603. move it back by one char before doing this check."
  604. (save-excursion
  605. (when (and (eolp) (not (bobp)))
  606. (backward-char 1))
  607. (org-invisible-p)))
  608. ;;; Local variables
  609. (defconst org-unique-local-variables
  610. '(org-element--cache
  611. org-element--cache-objects
  612. org-element--cache-sync-keys
  613. org-element--cache-sync-requests
  614. org-element--cache-sync-timer)
  615. "List of local variables that cannot be transferred to another buffer.")
  616. (defun org-get-local-variables ()
  617. "Return a list of all local variables in an Org mode buffer."
  618. (delq nil
  619. (mapcar
  620. (lambda (x)
  621. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  622. (name (car binding)))
  623. (and (not (get name 'org-state))
  624. (not (memq name org-unique-local-variables))
  625. (string-match-p
  626. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  627. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  628. (symbol-name name))
  629. binding)))
  630. (with-temp-buffer
  631. (org-mode)
  632. (buffer-local-variables)))))
  633. (defun org-clone-local-variables (from-buffer &optional regexp)
  634. "Clone local variables from FROM-BUFFER.
  635. Optional argument REGEXP selects variables to clone."
  636. (dolist (pair (buffer-local-variables from-buffer))
  637. (pcase pair
  638. (`(,name . ,value) ;ignore unbound variables
  639. (when (and (not (memq name org-unique-local-variables))
  640. (or (null regexp) (string-match-p regexp (symbol-name name))))
  641. (ignore-errors (set (make-local-variable name) value)))))))
  642. ;;; Visibility
  643. (defun org-outline-overlay-data (&optional use-markers)
  644. "Return a list of the locations of all outline overlays.
  645. These are overlays with the `invisible' property value `outline'.
  646. The return value is a list of cons cells, with start and stop
  647. positions for each overlay.
  648. If USE-MARKERS is set, return the positions as markers."
  649. (let (beg end)
  650. (org-with-wide-buffer
  651. (delq nil
  652. (mapcar (lambda (o)
  653. (when (eq (overlay-get o 'invisible) 'outline)
  654. (setq beg (overlay-start o)
  655. end (overlay-end o))
  656. (and beg end (> end beg)
  657. (if use-markers
  658. (cons (copy-marker beg)
  659. (copy-marker end t))
  660. (cons beg end)))))
  661. (overlays-in (point-min) (point-max)))))))
  662. (defun org-set-outline-overlay-data (data)
  663. "Create visibility overlays for all positions in DATA.
  664. DATA should have been made by `org-outline-overlay-data'."
  665. (org-with-wide-buffer
  666. (org-show-all)
  667. (dolist (c data) (org-flag-region (car c) (cdr c) t 'outline))))
  668. ;;; Miscellaneous
  669. (defsubst org-call-with-arg (command arg)
  670. "Call COMMAND interactively, but pretend prefix arg was ARG."
  671. (let ((current-prefix-arg arg)) (call-interactively command)))
  672. (defsubst org-check-external-command (cmd &optional use no-error)
  673. "Check if external program CMD for USE exists, error if not.
  674. When the program does exist, return its path.
  675. When it does not exist and NO-ERROR is set, return nil.
  676. Otherwise, throw an error. The optional argument USE can describe what this
  677. program is needed for, so that the error message can be more informative."
  678. (or (executable-find cmd)
  679. (if no-error
  680. nil
  681. (error "Can't find `%s'%s" cmd
  682. (if use (format " (%s)" use) "")))))
  683. (defun org-display-warning (message)
  684. "Display the given MESSAGE as a warning."
  685. (display-warning 'org message :warning))
  686. (defun org-unlogged-message (&rest args)
  687. "Display a message, but avoid logging it in the *Messages* buffer."
  688. (let ((message-log-max nil))
  689. (apply #'message args)))
  690. (defun org-let (list &rest body)
  691. (eval (cons 'let (cons list body))))
  692. (put 'org-let 'lisp-indent-function 1)
  693. (defun org-let2 (list1 list2 &rest body)
  694. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  695. (put 'org-let2 'lisp-indent-function 2)
  696. (defun org-eval (form)
  697. "Eval FORM and return result."
  698. (condition-case error
  699. (eval form)
  700. (error (format "%%![Error: %s]" error))))
  701. (defvar org-outline-regexp) ; defined in org.el
  702. (defvar org-odd-levels-only) ; defined in org.el
  703. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  704. (defun org-get-limited-outline-regexp ()
  705. "Return outline-regexp with limited number of levels.
  706. The number of levels is controlled by `org-inlinetask-min-level'"
  707. (cond ((not (derived-mode-p 'org-mode))
  708. outline-regexp)
  709. ((not (featurep 'org-inlinetask))
  710. org-outline-regexp)
  711. (t
  712. (let* ((limit-level (1- org-inlinetask-min-level))
  713. (nstars (if org-odd-levels-only
  714. (1- (* limit-level 2))
  715. limit-level)))
  716. (format "\\*\\{1,%d\\} " nstars)))))
  717. (provide 'org-macs)
  718. ;;; org-macs.el ends here