org-macs.el 26 KB

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