org-macs.el 28 KB

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