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 to `split-string', matching SEPARATORS at the beginning
  230. and 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
  286. #'propertize
  287. ;; Displayed string could contain
  288. ;; invisible parts, but no nested display.
  289. (funcall prune-invisible value)
  290. (plist-put props
  291. 'display
  292. (and (not (stringp display))
  293. (cl-remove-if #'stringp
  294. display)))))))))))
  295. ;; `display' property overrides `invisible' one. So we first
  296. ;; replace characters with `display' property. Then we remove
  297. ;; invisible characters.
  298. (funcall prune-invisible (funcall replace-display string))))
  299. (defun org-string-width (string)
  300. "Return width of STRING when displayed in the current buffer.
  301. Unlike to `string-width', this function takes into consideration
  302. `invisible' and `display' text properties."
  303. (string-width (org-string-display string)))
  304. (defun org-not-nil (v)
  305. "If V not nil, and also not the string \"nil\", then return V.
  306. Otherwise return nil."
  307. (and v (not (equal v "nil")) v))
  308. (defun org-unbracket-string (pre post string)
  309. "Remove PRE/POST from the beginning/end of STRING.
  310. Both PRE and POST must be pre-/suffixes of STRING, or neither is
  311. removed."
  312. (if (and (string-prefix-p pre string)
  313. (string-suffix-p post string))
  314. (substring string (length pre) (- (length post)))
  315. string))
  316. (defsubst org-current-line-string (&optional to-here)
  317. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  318. (defun org-shorten-string (s maxlength)
  319. "Shorten string S so that it is no longer than MAXLENGTH characters.
  320. If the string is shorter or has length MAXLENGTH, just return the
  321. original string. If it is longer, the functions finds a space in the
  322. string, breaks this string off at that locations and adds three dots
  323. as ellipsis. Including the ellipsis, the string will not be longer
  324. than MAXLENGTH. If finding a good breaking point in the string does
  325. not work, the string is just chopped off in the middle of a word
  326. if necessary."
  327. (if (<= (length s) maxlength)
  328. s
  329. (let* ((n (max (- maxlength 4) 1))
  330. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  331. (if (string-match re s)
  332. (concat (match-string 1 s) "...")
  333. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  334. (defun org-remove-tabs (s &optional width)
  335. "Replace tabulators in S with spaces.
  336. Assumes that s is a single line, starting in column 0."
  337. (setq width (or width tab-width))
  338. (while (string-match "\t" s)
  339. (setq s (replace-match
  340. (make-string
  341. (- (* width (/ (+ (match-beginning 0) width) width))
  342. (match-beginning 0)) ?\ )
  343. t t s)))
  344. s)
  345. (defun org-wrap (string &optional width lines)
  346. "Wrap string to either a number of lines, or a width in characters.
  347. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  348. that costs. If there is a word longer than WIDTH, the text is actually
  349. wrapped to the length of that word.
  350. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  351. many lines, whatever width that takes.
  352. The return value is a list of lines, without newlines at the end."
  353. (let* ((words (split-string string))
  354. (maxword (apply 'max (mapcar 'org-string-width words)))
  355. w ll)
  356. (cond (width
  357. (org--do-wrap words (max maxword width)))
  358. (lines
  359. (setq w maxword)
  360. (setq ll (org--do-wrap words maxword))
  361. (if (<= (length ll) lines)
  362. ll
  363. (setq ll words)
  364. (while (> (length ll) lines)
  365. (setq w (1+ w))
  366. (setq ll (org--do-wrap words w)))
  367. ll))
  368. (t (error "Cannot wrap this")))))
  369. (defun org--do-wrap (words width)
  370. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  371. (let (lines line)
  372. (while words
  373. (setq line (pop words))
  374. (while (and words (< (+ (length line) (length (car words))) width))
  375. (setq line (concat line " " (pop words))))
  376. (setq lines (push line lines)))
  377. (nreverse lines)))
  378. (defun org-remove-indentation (code &optional n)
  379. "Remove maximum common indentation in string CODE and return it.
  380. N may optionally be the number of columns to remove. Return CODE
  381. as-is if removal failed."
  382. (with-temp-buffer
  383. (insert code)
  384. (if (org-do-remove-indentation n) (buffer-string) code)))
  385. (defun org-do-remove-indentation (&optional n)
  386. "Remove the maximum common indentation from the buffer.
  387. When optional argument N is a positive integer, remove exactly
  388. that much characters from indentation, if possible. Return nil
  389. if it fails."
  390. (catch :exit
  391. (goto-char (point-min))
  392. ;; Find maximum common indentation, if not specified.
  393. (let ((n (or n
  394. (let ((min-ind (point-max)))
  395. (save-excursion
  396. (while (re-search-forward "^[ \t]*\\S-" nil t)
  397. (let ((ind (1- (current-column))))
  398. (if (zerop ind) (throw :exit nil)
  399. (setq min-ind (min min-ind ind))))))
  400. min-ind))))
  401. (if (zerop n) (throw :exit nil)
  402. ;; Remove exactly N indentation, but give up if not possible.
  403. (while (not (eobp))
  404. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  405. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  406. ((< ind n) (throw :exit nil))
  407. (t (indent-line-to (- ind n))))
  408. (forward-line)))
  409. ;; Signal success.
  410. t))))
  411. ;;; List manipulation
  412. (defsubst org-get-alist-option (option key)
  413. (cond ((eq key t) t)
  414. ((eq option t) t)
  415. ((assoc key option) (cdr (assoc key option)))
  416. (t (let ((r (cdr (assq 'default option))))
  417. (if (listp r) (delq nil r) r)))))
  418. (defsubst org-last (list)
  419. "Return the last element of LIST."
  420. (car (last list)))
  421. (defsubst org-uniquify (list)
  422. "Non-destructively remove duplicate elements from LIST."
  423. (let ((res (copy-sequence list))) (delete-dups res)))
  424. (defun org-uniquify-alist (alist)
  425. "Merge elements of ALIST with the same key.
  426. For example, in this alist:
  427. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  428. => \\='((a 1 3) (b 2))
  429. merge (a 1) and (a 3) into (a 1 3).
  430. The function returns the new ALIST."
  431. (let (rtn)
  432. (dolist (e alist rtn)
  433. (let (n)
  434. (if (not (assoc (car e) rtn))
  435. (push e rtn)
  436. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  437. (setq rtn (assq-delete-all (car e) rtn))
  438. (push n rtn))))))
  439. (defun org-delete-all (elts list)
  440. "Remove all elements in ELTS from LIST.
  441. Comparison is done with `equal'. It is a destructive operation
  442. that may remove elements by altering the list structure."
  443. (while elts
  444. (setq list (delete (pop elts) list)))
  445. list)
  446. (defun org-plist-delete (plist property)
  447. "Delete PROPERTY from PLIST.
  448. This is in contrast to merely setting it to 0."
  449. (let (p)
  450. (while plist
  451. (if (not (eq property (car plist)))
  452. (setq p (plist-put p (car plist) (nth 1 plist))))
  453. (setq plist (cddr plist)))
  454. p))
  455. (defun org-combine-plists (&rest plists)
  456. "Create a single property list from all plists in PLISTS.
  457. The process starts by copying the first list, and then setting properties
  458. from the other lists. Settings in the last list are the most significant
  459. ones and overrule settings in the other lists."
  460. (let ((rtn (copy-sequence (pop plists)))
  461. p v ls)
  462. (while plists
  463. (setq ls (pop plists))
  464. (while ls
  465. (setq p (pop ls) v (pop ls))
  466. (setq rtn (plist-put rtn p v))))
  467. rtn))
  468. ;;; Regexp matching
  469. (defsubst org-pos-in-match-range (pos n)
  470. (and (match-beginning n)
  471. (<= (match-beginning n) pos)
  472. (>= (match-end n) pos)))
  473. (defun org-skip-whitespace ()
  474. "Skip over space, tabs and newline characters."
  475. (skip-chars-forward " \t\n\r"))
  476. (defun org-match-line (regexp)
  477. "Match REGEXP at the beginning of the current line."
  478. (save-excursion
  479. (beginning-of-line)
  480. (looking-at regexp)))
  481. (defun org-in-regexp (regexp &optional nlines visually)
  482. "Check if point is inside a match of REGEXP.
  483. Normally only the current line is checked, but you can include
  484. NLINES extra lines around point into the search. If VISUALLY is
  485. set, require that the cursor is not after the match but really
  486. on, so that the block visually is on the match.
  487. Return nil or a cons cell (BEG . END) where BEG and END are,
  488. respectively, the positions at the beginning and the end of the
  489. match."
  490. (catch :exit
  491. (let ((pos (point))
  492. (eol (line-end-position (if nlines (1+ nlines) 1))))
  493. (save-excursion
  494. (beginning-of-line (- 1 (or nlines 0)))
  495. (while (and (re-search-forward regexp eol t)
  496. (<= (match-beginning 0) pos))
  497. (let ((end (match-end 0)))
  498. (when (or (> end pos) (and (= end pos) (not visually)))
  499. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  500. (defun org-point-in-group (point group &optional context)
  501. "Check if POINT is in match-group GROUP.
  502. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  503. match. If the match group does not exist or point is not inside it,
  504. return nil."
  505. (and (match-beginning group)
  506. (>= point (match-beginning group))
  507. (<= point (match-end group))
  508. (if context
  509. (list context (match-beginning group) (match-end group))
  510. t)))
  511. ;;; Motion
  512. (defsubst org-goto-line (N)
  513. (save-restriction
  514. (widen)
  515. (goto-char (point-min))
  516. (forward-line (1- N))))
  517. (defsubst org-current-line (&optional pos)
  518. (save-excursion
  519. (and pos (goto-char pos))
  520. ;; works also in narrowed buffer, because we start at 1, not point-min
  521. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  522. ;;; Text properties
  523. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  524. rear-nonsticky t mouse-map t fontified t
  525. org-emphasis t)
  526. "Properties to remove when a string without properties is wanted.")
  527. (defsubst org-no-properties (s &optional restricted)
  528. "Remove all text properties from string S.
  529. When RESTRICTED is non-nil, only remove the properties listed
  530. in `org-rm-props'."
  531. (if restricted (remove-text-properties 0 (length s) org-rm-props s)
  532. (set-text-properties 0 (length s) nil s))
  533. s)
  534. (defun org-add-props (string plist &rest props)
  535. "Add text properties to entire string, from beginning to end.
  536. PLIST may be a list of properties, PROPS are individual properties and values
  537. that will be added to PLIST. Returns the string that was modified."
  538. (declare (indent 2))
  539. (add-text-properties
  540. 0 (length string) (if props (append plist props) plist) string)
  541. string)
  542. (defun org-make-parameter-alist (flat)
  543. "Return alist based on FLAT.
  544. FLAT is a list with alternating symbol names and values. The
  545. returned alist is a list of lists with the symbol name in car and
  546. the value in cdr."
  547. (when flat
  548. (cons (list (car flat) (cadr flat))
  549. (org-make-parameter-alist (cddr flat)))))
  550. (defsubst org-get-at-bol (property)
  551. "Get text property PROPERTY at the beginning of line."
  552. (get-text-property (point-at-bol) property))
  553. (defun org-get-at-eol (property n)
  554. "Get text property PROPERTY at the end of line less N characters."
  555. (get-text-property (- (point-at-eol) n) property))
  556. (defun org-find-text-property-in-string (prop s)
  557. "Return the first non-nil value of property PROP in string S."
  558. (or (get-text-property 0 prop s)
  559. (get-text-property (or (next-single-property-change 0 prop s) 0)
  560. prop s)))
  561. (defun org-invisible-p (&optional pos)
  562. "Non-nil if the character after POS is invisible.
  563. If POS is nil, use `point' instead."
  564. (get-char-property (or pos (point)) 'invisible))
  565. (defun org-truely-invisible-p ()
  566. "Check if point is at a character currently not visible.
  567. This version does not only check the character property, but also
  568. `visible-mode'."
  569. (unless (bound-and-true-p visible-mode)
  570. (org-invisible-p)))
  571. (defun org-invisible-p2 ()
  572. "Check if point is at a character currently not visible.
  573. If the point is at EOL (and not at the beginning of a buffer too),
  574. move it back by one char before doing this check."
  575. (save-excursion
  576. (when (and (eolp) (not (bobp)))
  577. (backward-char 1))
  578. (org-invisible-p)))
  579. ;;; Local variables
  580. (defconst org-unique-local-variables
  581. '(org-element--cache
  582. org-element--cache-objects
  583. org-element--cache-sync-keys
  584. org-element--cache-sync-requests
  585. org-element--cache-sync-timer)
  586. "List of local variables that cannot be transferred to another buffer.")
  587. (defun org-get-local-variables ()
  588. "Return a list of all local variables in an Org mode buffer."
  589. (delq nil
  590. (mapcar
  591. (lambda (x)
  592. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  593. (name (car binding)))
  594. (and (not (get name 'org-state))
  595. (not (memq name org-unique-local-variables))
  596. (string-match-p
  597. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  598. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  599. (symbol-name name))
  600. binding)))
  601. (with-temp-buffer
  602. (org-mode)
  603. (buffer-local-variables)))))
  604. (defun org-clone-local-variables (from-buffer &optional regexp)
  605. "Clone local variables from FROM-BUFFER.
  606. Optional argument REGEXP selects variables to clone."
  607. (dolist (pair (buffer-local-variables from-buffer))
  608. (pcase pair
  609. (`(,name . ,value) ;ignore unbound variables
  610. (when (and (not (memq name org-unique-local-variables))
  611. (or (null regexp) (string-match-p regexp (symbol-name name))))
  612. (ignore-errors (set (make-local-variable name) value)))))))
  613. ;;; Miscellaneous
  614. (defsubst org-call-with-arg (command arg)
  615. "Call COMMAND interactively, but pretend prefix arg was ARG."
  616. (let ((current-prefix-arg arg)) (call-interactively command)))
  617. (defsubst org-check-external-command (cmd &optional use no-error)
  618. "Check if external program CMD for USE exists, error if not.
  619. When the program does exist, return its path.
  620. When it does not exist and NO-ERROR is set, return nil.
  621. Otherwise, throw an error. The optional argument USE can describe what this
  622. program is needed for, so that the error message can be more informative."
  623. (or (executable-find cmd)
  624. (if no-error
  625. nil
  626. (error "Can't find `%s'%s" cmd
  627. (if use (format " (%s)" use) "")))))
  628. (defun org-display-warning (message)
  629. "Display the given MESSAGE as a warning."
  630. (display-warning 'org message :warning))
  631. (defun org-let (list &rest body)
  632. (eval (cons 'let (cons list body))))
  633. (put 'org-let 'lisp-indent-function 1)
  634. (defun org-let2 (list1 list2 &rest body)
  635. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  636. (put 'org-let2 'lisp-indent-function 2)
  637. (defun org-eval (form)
  638. "Eval FORM and return result."
  639. (condition-case error
  640. (eval form)
  641. (error (format "%%![Error: %s]" error))))
  642. (defvar org-outline-regexp) ; defined in org.el
  643. (defvar org-odd-levels-only) ; defined in org.el
  644. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  645. (defun org-get-limited-outline-regexp ()
  646. "Return outline-regexp with limited number of levels.
  647. The number of levels is controlled by `org-inlinetask-min-level'"
  648. (cond ((not (derived-mode-p 'org-mode))
  649. outline-regexp)
  650. ((not (featurep 'org-inlinetask))
  651. org-outline-regexp)
  652. (t
  653. (let* ((limit-level (1- org-inlinetask-min-level))
  654. (nstars (if org-odd-levels-only
  655. (1- (* limit-level 2))
  656. limit-level)))
  657. (format "\\*\\{1,%d\\} " nstars)))))
  658. (provide 'org-macs)
  659. ;;; org-macs.el ends here