org-macs.el 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2019 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. (require 'cl-lib)
  28. (require 'format-spec)
  29. (declare-function org-string-collate-lessp "org-compat" (s1 s2 &optional locale ignore-case))
  30. (defvar org-ts-regexp0)
  31. ;;; Macros
  32. (defmacro org-with-gensyms (symbols &rest body)
  33. (declare (debug (sexp body)) (indent 1))
  34. `(let ,(mapcar (lambda (s)
  35. `(,s (make-symbol (concat "--" (symbol-name ',s)))))
  36. symbols)
  37. ,@body))
  38. ;; Use `with-silent-modifications' to ignore cosmetic changes and
  39. ;; `org-unmodified' to ignore real text modifications.
  40. (defmacro org-unmodified (&rest body)
  41. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  42. (declare (debug (body)))
  43. (org-with-gensyms (was-modified)
  44. `(let ((,was-modified (buffer-modified-p)))
  45. (unwind-protect
  46. (let ((buffer-undo-list t)
  47. (inhibit-modification-hooks t))
  48. ,@body)
  49. (set-buffer-modified-p ,was-modified)))))
  50. (defmacro org-without-partial-completion (&rest body)
  51. (declare (debug (body)))
  52. `(if (and (boundp 'partial-completion-mode)
  53. partial-completion-mode
  54. (fboundp 'partial-completion-mode))
  55. (unwind-protect
  56. (progn
  57. (partial-completion-mode -1)
  58. ,@body)
  59. (partial-completion-mode 1))
  60. ,@body))
  61. (defmacro org-with-point-at (pom &rest body)
  62. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  63. (declare (debug (form body)) (indent 1))
  64. (org-with-gensyms (mpom)
  65. `(let ((,mpom ,pom))
  66. (save-excursion
  67. (when (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  68. (org-with-wide-buffer
  69. (goto-char (or ,mpom (point)))
  70. ,@body)))))
  71. (defmacro org-with-remote-undo (buffer &rest body)
  72. "Execute BODY while recording undo information in two buffers."
  73. (declare (debug (form body)) (indent 1))
  74. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  75. `(let ((,cline (org-current-line))
  76. (,cmd this-command)
  77. (,buf1 (current-buffer))
  78. (,buf2 ,buffer)
  79. (,undo1 buffer-undo-list)
  80. (,undo2 (with-current-buffer ,buffer buffer-undo-list))
  81. ,c1 ,c2)
  82. ,@body
  83. (when org-agenda-allow-remote-undo
  84. (setq ,c1 (org-verify-change-for-undo
  85. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  86. ,c2 (org-verify-change-for-undo
  87. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  88. (when (or ,c1 ,c2)
  89. ;; make sure there are undo boundaries
  90. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  91. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  92. ;; remember which buffer to undo
  93. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  94. org-agenda-undo-list))))))
  95. (defmacro org-no-read-only (&rest body)
  96. "Inhibit read-only for BODY."
  97. (declare (debug (body)))
  98. `(let ((inhibit-read-only t)) ,@body))
  99. (defmacro org-save-outline-visibility (use-markers &rest body)
  100. "Save and restore outline visibility around BODY.
  101. If USE-MARKERS is non-nil, use markers for the positions. This
  102. means that the buffer may change while running BODY, but it also
  103. means that the buffer should stay alive during the operation,
  104. because otherwise all these markers will point to nowhere."
  105. (declare (debug (form body)) (indent 1))
  106. (org-with-gensyms (data invisible-types markers?)
  107. `(let* ((,invisible-types '(org-hide-block org-hide-drawer outline))
  108. (,markers? ,use-markers)
  109. (,data
  110. (mapcar (lambda (o)
  111. (let ((beg (overlay-start o))
  112. (end (overlay-end o))
  113. (type (overlay-get o 'invisible)))
  114. (and beg end
  115. (> end beg)
  116. (memq type ,invisible-types)
  117. (list (if ,markers? (copy-marker beg) beg)
  118. (if ,markers? (copy-marker end t) end)
  119. type))))
  120. (org-with-wide-buffer
  121. (overlays-in (point-min) (point-max))))))
  122. (unwind-protect (progn ,@body)
  123. (org-with-wide-buffer
  124. (dolist (type ,invisible-types)
  125. (remove-overlays (point-min) (point-max) 'invisible type))
  126. (pcase-dolist (`(,beg ,end ,type) (delq nil ,data))
  127. (org-flag-region beg end t type)
  128. (when ,markers?
  129. (set-marker beg nil)
  130. (set-marker end nil))))))))
  131. (defmacro org-with-wide-buffer (&rest body)
  132. "Execute body while temporarily widening the buffer."
  133. (declare (debug (body)))
  134. `(save-excursion
  135. (save-restriction
  136. (widen)
  137. ,@body)))
  138. (defmacro org-with-limited-levels (&rest body)
  139. "Execute BODY with limited number of outline levels."
  140. (declare (debug (body)))
  141. `(progn
  142. (defvar org-called-with-limited-levels)
  143. (defvar org-outline-regexp)
  144. (defvar outline-regexp)
  145. (defvar org-outline-regexp-bol)
  146. (let* ((org-called-with-limited-levels t)
  147. (org-outline-regexp (org-get-limited-outline-regexp))
  148. (outline-regexp org-outline-regexp)
  149. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  150. ,@body)))
  151. (defmacro org-eval-in-environment (environment form)
  152. (declare (debug (form form)) (indent 1))
  153. `(eval (list 'let ,environment ',form)))
  154. ;;;###autoload
  155. (defmacro org-load-noerror-mustsuffix (file)
  156. "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
  157. `(load ,file 'noerror nil nil 'mustsuffix))
  158. (defmacro org-preserve-local-variables (&rest body)
  159. "Execute BODY while preserving local variables."
  160. (declare (debug (body)))
  161. `(let ((local-variables
  162. (org-with-wide-buffer
  163. (goto-char (point-max))
  164. (let ((case-fold-search t))
  165. (and (re-search-backward "^[ \t]*# +Local Variables:"
  166. (max (- (point) 3000) 1)
  167. t)
  168. (delete-and-extract-region (point) (point-max)))))))
  169. (unwind-protect (progn ,@body)
  170. (when local-variables
  171. (org-with-wide-buffer
  172. (goto-char (point-max))
  173. ;; If last section is folded, make sure to also hide file
  174. ;; local variables after inserting them back.
  175. (let ((overlay
  176. (cl-find-if (lambda (o)
  177. (eq 'outline (overlay-get o 'invisible)))
  178. (overlays-at (1- (point))))))
  179. (unless (bolp) (insert "\n"))
  180. (insert local-variables)
  181. (when overlay
  182. (move-overlay overlay (overlay-start overlay) (point-max)))))))))
  183. (defmacro org-no-popups (&rest body)
  184. "Suppress popup windows and evaluate BODY."
  185. `(let (pop-up-frames display-buffer-alist)
  186. ,@body))
  187. (defmacro org-table-with-shrunk-field (&rest body)
  188. "Save field shrunk state, execute BODY and restore state."
  189. (declare (debug (body)))
  190. (org-with-gensyms (end shrunk size)
  191. `(let* ((,shrunk (save-match-data (org-table--shrunk-field)))
  192. (,end (and ,shrunk (copy-marker (overlay-end ,shrunk) t)))
  193. (,size (and ,shrunk (- ,end (overlay-start ,shrunk)))))
  194. (when ,shrunk (delete-overlay ,shrunk))
  195. (unwind-protect (progn ,@body)
  196. (when ,shrunk (move-overlay ,shrunk (- ,end ,size) ,end))))))
  197. ;;; Buffer and windows
  198. (defun org-base-buffer (buffer)
  199. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  200. (when buffer
  201. (or (buffer-base-buffer buffer)
  202. buffer)))
  203. (defun org-find-base-buffer-visiting (file)
  204. "Like `find-buffer-visiting' but always return the base buffer and
  205. not an indirect buffer."
  206. (let ((buf (or (get-file-buffer file)
  207. (find-buffer-visiting file))))
  208. (org-base-buffer buf)))
  209. (defun org-switch-to-buffer-other-window (&rest args)
  210. "Switch to buffer in a second window on the current frame.
  211. In particular, do not allow pop-up frames.
  212. Returns the newly created buffer."
  213. (org-no-popups (apply #'switch-to-buffer-other-window args)))
  214. (defun org-fit-window-to-buffer (&optional window max-height min-height
  215. shrink-only)
  216. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  217. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  218. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  219. `shrink-window-if-larger-than-buffer' instead, the height limit is
  220. ignored in this case."
  221. (cond ((if (fboundp 'window-full-width-p)
  222. (not (window-full-width-p window))
  223. ;; Do nothing if another window would suffer.
  224. (> (frame-width) (window-width window))))
  225. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  226. (fit-window-to-buffer window max-height min-height))
  227. ((fboundp 'shrink-window-if-larger-than-buffer)
  228. (shrink-window-if-larger-than-buffer window)))
  229. (or window (selected-window)))
  230. ;;; File
  231. (defun org-file-newer-than-p (file time)
  232. "Non-nil if FILE is newer than TIME.
  233. FILE is a filename, as a string, TIME is a list of integers, as
  234. returned by, e.g., `current-time'."
  235. (and (file-exists-p file)
  236. ;; Only compare times up to whole seconds as some file-systems
  237. ;; (e.g. HFS+) do not retain any finer granularity. As
  238. ;; a consequence, make sure we return non-nil when the two
  239. ;; times are equal.
  240. (not (time-less-p (cl-subseq (nth 5 (file-attributes file)) 0 2)
  241. (cl-subseq time 0 2)))))
  242. (defun org-compile-file (source process ext &optional err-msg log-buf spec)
  243. "Compile a SOURCE file using PROCESS.
  244. PROCESS is either a function or a list of shell commands, as
  245. strings. EXT is a file extension, without the leading dot, as
  246. a string. It is used to check if the process actually succeeded.
  247. PROCESS must create a file with the same base name and directory
  248. as SOURCE, but ending with EXT. The function then returns its
  249. filename. Otherwise, it raises an error. The error message can
  250. then be refined by providing string ERR-MSG, which is appended to
  251. the standard message.
  252. If PROCESS is a function, it is called with a single argument:
  253. the SOURCE file.
  254. If it is a list of commands, each of them is called using
  255. `shell-command'. By default, in each command, %b, %f, %F, %o and
  256. %O are replaced with, respectively, SOURCE base name, name, full
  257. name, directory and absolute output file name. It is possible,
  258. however, to use more place-holders by specifying them in optional
  259. argument SPEC, as an alist following the pattern
  260. (CHARACTER . REPLACEMENT-STRING).
  261. When PROCESS is a list of commands, optional argument LOG-BUF can
  262. be set to a buffer or a buffer name. `shell-command' then uses
  263. it for output."
  264. (let* ((base-name (file-name-base source))
  265. (full-name (file-truename source))
  266. (out-dir (or (file-name-directory source) "./"))
  267. (output (expand-file-name (concat base-name "." ext) out-dir))
  268. (time (current-time))
  269. (err-msg (if (stringp err-msg) (concat ". " err-msg) "")))
  270. (save-window-excursion
  271. (pcase process
  272. ((pred functionp) (funcall process (shell-quote-argument source)))
  273. ((pred consp)
  274. (let ((log-buf (and log-buf (get-buffer-create log-buf)))
  275. (spec (append spec
  276. `((?b . ,(shell-quote-argument base-name))
  277. (?f . ,(shell-quote-argument source))
  278. (?F . ,(shell-quote-argument full-name))
  279. (?o . ,(shell-quote-argument out-dir))
  280. (?O . ,(shell-quote-argument output))))))
  281. (dolist (command process)
  282. (shell-command (format-spec command spec) log-buf))
  283. (when log-buf (with-current-buffer log-buf (compilation-mode)))))
  284. (_ (error "No valid command to process %S%s" source err-msg))))
  285. ;; Check for process failure. Output file is expected to be
  286. ;; located in the same directory as SOURCE.
  287. (unless (org-file-newer-than-p output time)
  288. (error (format "File %S wasn't produced%s" output err-msg)))
  289. output))
  290. ;;; Indentation
  291. (defun org-do-remove-indentation (&optional n)
  292. "Remove the maximum common indentation from the buffer.
  293. When optional argument N is a positive integer, remove exactly
  294. that much characters from indentation, if possible. Return nil
  295. if it fails."
  296. (catch :exit
  297. (goto-char (point-min))
  298. ;; Find maximum common indentation, if not specified.
  299. (let ((n (or n
  300. (let ((min-ind (point-max)))
  301. (save-excursion
  302. (while (re-search-forward "^[ \t]*\\S-" nil t)
  303. (let ((ind (1- (current-column))))
  304. (if (zerop ind) (throw :exit nil)
  305. (setq min-ind (min min-ind ind))))))
  306. min-ind))))
  307. (if (zerop n) (throw :exit nil)
  308. ;; Remove exactly N indentation, but give up if not possible.
  309. (while (not (eobp))
  310. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  311. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  312. ((< ind n) (throw :exit nil))
  313. (t (indent-line-to (- ind n))))
  314. (forward-line)))
  315. ;; Signal success.
  316. t))))
  317. ;;; Input
  318. (defun org-read-function (prompt &optional allow-empty?)
  319. "Prompt for a function.
  320. If ALLOW-EMPTY? is non-nil, return nil rather than raising an
  321. error when the user input is empty."
  322. (let ((func (completing-read prompt obarray #'fboundp t)))
  323. (cond ((not (string= func ""))
  324. (intern func))
  325. (allow-empty? nil)
  326. (t (user-error "Empty input is not valid")))))
  327. (defun org-completing-read (&rest args)
  328. "Completing-read with SPACE being a normal character."
  329. (let ((enable-recursive-minibuffers t)
  330. (minibuffer-local-completion-map
  331. (copy-keymap minibuffer-local-completion-map)))
  332. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  333. (define-key minibuffer-local-completion-map "?" 'self-insert-command)
  334. (define-key minibuffer-local-completion-map (kbd "C-c !")
  335. 'org-time-stamp-inactive)
  336. (apply #'completing-read args)))
  337. (defun org--mks-read-key (allowed-keys prompt)
  338. "Read a key and ensure it is a member of ALLOWED-KEYS.
  339. TAB, SPC and RET are treated equivalently."
  340. (let* ((key (char-to-string
  341. (pcase (read-char-exclusive prompt)
  342. ((or ?\s ?\t ?\r) ?\t)
  343. (char char)))))
  344. (if (member key allowed-keys)
  345. key
  346. (message "Invalid key: `%s'" key)
  347. (sit-for 1)
  348. (org--mks-read-key allowed-keys prompt))))
  349. (defun org-mks (table title &optional prompt specials)
  350. "Select a member of an alist with multiple keys.
  351. TABLE is the alist which should contain entries where the car is a string.
  352. There should be two types of entries.
  353. 1. prefix descriptions like (\"a\" \"Description\")
  354. This indicates that `a' is a prefix key for multi-letter selection, and
  355. that there are entries following with keys like \"ab\", \"ax\"...
  356. 2. Select-able members must have more than two elements, with the first
  357. being the string of keys that lead to selecting it, and the second a
  358. short description string of the item.
  359. The command will then make a temporary buffer listing all entries
  360. that can be selected with a single key, and all the single key
  361. prefixes. When you press the key for a single-letter entry, it is selected.
  362. When you press a prefix key, the commands (and maybe further prefixes)
  363. under this key will be shown and offered for selection.
  364. TITLE will be placed over the selection in the temporary buffer,
  365. PROMPT will be used when prompting for a key. SPECIALS is an
  366. alist with (\"key\" \"description\") entries. When one of these
  367. is selected, only the bare key is returned."
  368. (save-window-excursion
  369. (let ((inhibit-quit t)
  370. (buffer (org-switch-to-buffer-other-window "*Org Select*"))
  371. (prompt (or prompt "Select: "))
  372. current)
  373. (unwind-protect
  374. (catch 'exit
  375. (while t
  376. (erase-buffer)
  377. (insert title "\n\n")
  378. (let ((des-keys nil)
  379. (allowed-keys '("\C-g"))
  380. (tab-alternatives '("\s" "\t" "\r"))
  381. (cursor-type nil))
  382. ;; Populate allowed keys and descriptions keys
  383. ;; available with CURRENT selector.
  384. (let ((re (format "\\`%s\\(.\\)\\'"
  385. (if current (regexp-quote current) "")))
  386. (prefix (if current (concat current " ") "")))
  387. (dolist (entry table)
  388. (pcase entry
  389. ;; Description.
  390. (`(,(and key (pred (string-match re))) ,desc)
  391. (let ((k (match-string 1 key)))
  392. (push k des-keys)
  393. ;; Keys ending in tab, space or RET are equivalent.
  394. (if (member k tab-alternatives)
  395. (push "\t" allowed-keys)
  396. (push k allowed-keys))
  397. (insert prefix "[" k "]" "..." " " desc "..." "\n")))
  398. ;; Usable entry.
  399. (`(,(and key (pred (string-match re))) ,desc . ,_)
  400. (let ((k (match-string 1 key)))
  401. (insert prefix "[" k "]" " " desc "\n")
  402. (push k allowed-keys)))
  403. (_ nil))))
  404. ;; Insert special entries, if any.
  405. (when specials
  406. (insert "----------------------------------------------------\
  407. ---------------------------\n")
  408. (pcase-dolist (`(,key ,description) specials)
  409. (insert (format "[%s] %s\n" key description))
  410. (push key allowed-keys)))
  411. ;; Display UI and let user select an entry or
  412. ;; a sub-level prefix.
  413. (goto-char (point-min))
  414. (unless (pos-visible-in-window-p (point-max))
  415. (org-fit-window-to-buffer))
  416. (let ((pressed (org--mks-read-key allowed-keys prompt)))
  417. (setq current (concat current pressed))
  418. (cond
  419. ((equal pressed "\C-g") (user-error "Abort"))
  420. ;; Selection is a prefix: open a new menu.
  421. ((member pressed des-keys))
  422. ;; Selection matches an association: return it.
  423. ((let ((entry (assoc current table)))
  424. (and entry (throw 'exit entry))))
  425. ;; Selection matches a special entry: return the
  426. ;; selection prefix.
  427. ((assoc current specials) (throw 'exit current))
  428. (t (error "No entry available")))))))
  429. (when buffer (kill-buffer buffer))))))
  430. ;;; List manipulation
  431. (defsubst org-get-alist-option (option key)
  432. (cond ((eq key t) t)
  433. ((eq option t) t)
  434. ((assoc key option) (cdr (assoc key option)))
  435. (t (let ((r (cdr (assq 'default option))))
  436. (if (listp r) (delq nil r) r)))))
  437. (defsubst org-last (list)
  438. "Return the last element of LIST."
  439. (car (last list)))
  440. (defsubst org-uniquify (list)
  441. "Non-destructively remove duplicate elements from LIST."
  442. (let ((res (copy-sequence list))) (delete-dups res)))
  443. (defun org-uniquify-alist (alist)
  444. "Merge elements of ALIST with the same key.
  445. For example, in this alist:
  446. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  447. => \\='((a 1 3) (b 2))
  448. merge (a 1) and (a 3) into (a 1 3).
  449. The function returns the new ALIST."
  450. (let (rtn)
  451. (dolist (e alist rtn)
  452. (let (n)
  453. (if (not (assoc (car e) rtn))
  454. (push e rtn)
  455. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  456. (setq rtn (assq-delete-all (car e) rtn))
  457. (push n rtn))))))
  458. (defun org-delete-all (elts list)
  459. "Remove all elements in ELTS from LIST.
  460. Comparison is done with `equal'. It is a destructive operation
  461. that may remove elements by altering the list structure."
  462. (while elts
  463. (setq list (delete (pop elts) list)))
  464. list)
  465. (defun org-plist-delete (plist property)
  466. "Delete PROPERTY from PLIST.
  467. This is in contrast to merely setting it to 0."
  468. (let (p)
  469. (while plist
  470. (if (not (eq property (car plist)))
  471. (setq p (plist-put p (car plist) (nth 1 plist))))
  472. (setq plist (cddr plist)))
  473. p))
  474. (defun org-combine-plists (&rest plists)
  475. "Create a single property list from all plists in PLISTS.
  476. The process starts by copying the first list, and then setting properties
  477. from the other lists. Settings in the last list are the most significant
  478. ones and overrule settings in the other lists."
  479. (let ((rtn (copy-sequence (pop plists)))
  480. p v ls)
  481. (while plists
  482. (setq ls (pop plists))
  483. (while ls
  484. (setq p (pop ls) v (pop ls))
  485. (setq rtn (plist-put rtn p v))))
  486. rtn))
  487. ;;; Local variables
  488. (defconst org-unique-local-variables
  489. '(org-element--cache
  490. org-element--cache-objects
  491. org-element--cache-sync-keys
  492. org-element--cache-sync-requests
  493. org-element--cache-sync-timer)
  494. "List of local variables that cannot be transferred to another buffer.")
  495. (defun org-get-local-variables ()
  496. "Return a list of all local variables in an Org mode buffer."
  497. (delq nil
  498. (mapcar
  499. (lambda (x)
  500. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  501. (name (car binding)))
  502. (and (not (get name 'org-state))
  503. (not (memq name org-unique-local-variables))
  504. (string-match-p
  505. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  506. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  507. (symbol-name name))
  508. binding)))
  509. (with-temp-buffer
  510. (org-mode)
  511. (buffer-local-variables)))))
  512. (defun org-clone-local-variables (from-buffer &optional regexp)
  513. "Clone local variables from FROM-BUFFER.
  514. Optional argument REGEXP selects variables to clone."
  515. (dolist (pair (buffer-local-variables from-buffer))
  516. (pcase pair
  517. (`(,name . ,value) ;ignore unbound variables
  518. (when (and (not (memq name org-unique-local-variables))
  519. (or (null regexp) (string-match-p regexp (symbol-name name))))
  520. (ignore-errors (set (make-local-variable name) value)))))))
  521. ;;; Miscellaneous
  522. (defsubst org-call-with-arg (command arg)
  523. "Call COMMAND interactively, but pretend prefix arg was ARG."
  524. (let ((current-prefix-arg arg)) (call-interactively command)))
  525. (defsubst org-check-external-command (cmd &optional use no-error)
  526. "Check if external program CMD for USE exists, error if not.
  527. When the program does exist, return its path.
  528. When it does not exist and NO-ERROR is set, return nil.
  529. Otherwise, throw an error. The optional argument USE can describe what this
  530. program is needed for, so that the error message can be more informative."
  531. (or (executable-find cmd)
  532. (if no-error
  533. nil
  534. (error "Can't find `%s'%s" cmd
  535. (if use (format " (%s)" use) "")))))
  536. (defun org-display-warning (message)
  537. "Display the given MESSAGE as a warning."
  538. (display-warning 'org message :warning))
  539. (defun org-unlogged-message (&rest args)
  540. "Display a message, but avoid logging it in the *Messages* buffer."
  541. (let ((message-log-max nil))
  542. (apply #'message args)))
  543. (defun org-let (list &rest body)
  544. (eval (cons 'let (cons list body))))
  545. (put 'org-let 'lisp-indent-function 1)
  546. (defun org-let2 (list1 list2 &rest body)
  547. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  548. (put 'org-let2 'lisp-indent-function 2)
  549. (defun org-eval (form)
  550. "Eval FORM and return result."
  551. (condition-case error
  552. (eval form)
  553. (error (format "%%![Error: %s]" error))))
  554. (defvar org-outline-regexp) ; defined in org.el
  555. (defvar org-odd-levels-only) ; defined in org.el
  556. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  557. (defun org-get-limited-outline-regexp ()
  558. "Return outline-regexp with limited number of levels.
  559. The number of levels is controlled by `org-inlinetask-min-level'"
  560. (cond ((not (derived-mode-p 'org-mode))
  561. outline-regexp)
  562. ((not (featurep 'org-inlinetask))
  563. org-outline-regexp)
  564. (t
  565. (let* ((limit-level (1- org-inlinetask-min-level))
  566. (nstars (if org-odd-levels-only
  567. (1- (* limit-level 2))
  568. limit-level)))
  569. (format "\\*\\{1,%d\\} " nstars)))))
  570. ;;; Motion
  571. (defsubst org-goto-line (N)
  572. (save-restriction
  573. (widen)
  574. (goto-char (point-min))
  575. (forward-line (1- N))))
  576. (defsubst org-current-line (&optional pos)
  577. (save-excursion
  578. (and pos (goto-char pos))
  579. ;; works also in narrowed buffer, because we start at 1, not point-min
  580. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  581. ;;; Overlays
  582. (defun org-overlay-display (ovl text &optional face evap)
  583. "Make overlay OVL display TEXT with face FACE."
  584. (overlay-put ovl 'display text)
  585. (when face (overlay-put ovl 'face face))
  586. (when evap (overlay-put ovl 'evaporate t)))
  587. (defun org-overlay-before-string (ovl text &optional face evap)
  588. "Make overlay OVL display TEXT with face FACE."
  589. (when face (org-add-props text nil 'face face))
  590. (overlay-put ovl 'before-string text)
  591. (when evap (overlay-put ovl 'evaporate t)))
  592. (defun org-find-overlays (prop &optional pos delete)
  593. "Find all overlays specifying PROP at POS or point.
  594. If DELETE is non-nil, delete all those overlays."
  595. (let (found)
  596. (dolist (ov (overlays-at (or pos (point))) found)
  597. (cond ((not (overlay-get ov prop)))
  598. (delete (delete-overlay ov))
  599. (t (push ov found))))))
  600. (defun org-flag-region (from to flag spec)
  601. "Hide or show lines from FROM to TO, according to FLAG.
  602. SPEC is the invisibility spec, as a symbol."
  603. (remove-overlays from to 'invisible spec)
  604. ;; Use `front-advance' since text right before to the beginning of
  605. ;; the overlay belongs to the visible line than to the contents.
  606. (when flag
  607. (let ((o (make-overlay from to nil 'front-advance)))
  608. (overlay-put o 'evaporate t)
  609. (overlay-put o 'invisible spec)
  610. (overlay-put o 'isearch-open-invisible #'delete-overlay))))
  611. ;;; Regexp matching
  612. (defsubst org-pos-in-match-range (pos n)
  613. (and (match-beginning n)
  614. (<= (match-beginning n) pos)
  615. (>= (match-end n) pos)))
  616. (defun org-skip-whitespace ()
  617. "Skip over space, tabs and newline characters."
  618. (skip-chars-forward " \t\n\r"))
  619. (defun org-match-line (regexp)
  620. "Match REGEXP at the beginning of the current line."
  621. (save-excursion
  622. (beginning-of-line)
  623. (looking-at regexp)))
  624. (defun org-match-any-p (re list)
  625. "Non-nil if regexp RE matches an element in LIST."
  626. (cl-some (lambda (x) (string-match-p re x)) list))
  627. (defun org-in-regexp (regexp &optional nlines visually)
  628. "Check if point is inside a match of REGEXP.
  629. Normally only the current line is checked, but you can include
  630. NLINES extra lines around point into the search. If VISUALLY is
  631. set, require that the cursor is not after the match but really
  632. on, so that the block visually is on the match.
  633. Return nil or a cons cell (BEG . END) where BEG and END are,
  634. respectively, the positions at the beginning and the end of the
  635. match."
  636. (catch :exit
  637. (let ((pos (point))
  638. (eol (line-end-position (if nlines (1+ nlines) 1))))
  639. (save-excursion
  640. (beginning-of-line (- 1 (or nlines 0)))
  641. (while (and (re-search-forward regexp eol t)
  642. (<= (match-beginning 0) pos))
  643. (let ((end (match-end 0)))
  644. (when (or (> end pos) (and (= end pos) (not visually)))
  645. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  646. (defun org-point-in-group (point group &optional context)
  647. "Check if POINT is in match-group GROUP.
  648. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  649. match. If the match group does not exist or point is not inside it,
  650. return nil."
  651. (and (match-beginning group)
  652. (>= point (match-beginning group))
  653. (<= point (match-end group))
  654. (if context
  655. (list context (match-beginning group) (match-end group))
  656. t)))
  657. ;;; String manipulation
  658. (defun org-string< (a b)
  659. (org-string-collate-lessp a b))
  660. (defun org-string<= (a b)
  661. (or (string= a b) (org-string-collate-lessp a b)))
  662. (defun org-string>= (a b)
  663. (not (org-string-collate-lessp a b)))
  664. (defun org-string> (a b)
  665. (and (not (string= a b))
  666. (not (org-string-collate-lessp a b))))
  667. (defun org-string<> (a b)
  668. (not (string= a b)))
  669. (defsubst org-trim (s &optional keep-lead)
  670. "Remove whitespace at the beginning and the end of string S.
  671. When optional argument KEEP-LEAD is non-nil, removing blank lines
  672. at the beginning of the string does not affect leading indentation."
  673. (replace-regexp-in-string
  674. (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
  675. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  676. (defun org-string-nw-p (s)
  677. "Return S if S is a string containing a non-blank character.
  678. Otherwise, return nil."
  679. (and (stringp s)
  680. (string-match-p "[^ \r\t\n]" s)
  681. s))
  682. (defun org-reverse-string (string)
  683. "Return the reverse of STRING."
  684. (apply #'string (nreverse (string-to-list string))))
  685. (defun org-split-string (string &optional separators)
  686. "Splits STRING into substrings at SEPARATORS.
  687. SEPARATORS is a regular expression. When nil, it defaults to
  688. \"[ \f\t\n\r\v]+\".
  689. Unlike `split-string', matching SEPARATORS at the beginning and
  690. end of string are ignored."
  691. (let ((separators (or separators "[ \f\t\n\r\v]+")))
  692. (if (not (string-match separators string)) (list string)
  693. (let ((i (match-end 0))
  694. (results
  695. (and (/= 0 (match-beginning 0)) ;skip leading separator
  696. (list (substring string 0 (match-beginning 0))))))
  697. (while (string-match separators string i)
  698. (push (substring string i (match-beginning 0))
  699. results)
  700. (setq i (match-end 0)))
  701. (nreverse (if (= i (length string))
  702. results ;skip trailing separator
  703. (cons (substring string i) results)))))))
  704. (defun org--string-from-props (s property beg end)
  705. "Return the visible part of string S.
  706. Visible part is determined according to text PROPERTY, which is
  707. either `invisible' or `display'. BEG and END are 0-indices
  708. delimiting S."
  709. (let ((width 0)
  710. (cursor beg))
  711. (while (setq beg (text-property-not-all beg end property nil s))
  712. (let* ((next (next-single-property-change beg property s end))
  713. (props (text-properties-at beg s))
  714. (spec (plist-get props property))
  715. (value
  716. (pcase property
  717. (`invisible
  718. ;; If `invisible' property in PROPS means text is to
  719. ;; be invisible, return 0. Otherwise return nil so
  720. ;; as to resume search.
  721. (and (or (eq t buffer-invisibility-spec)
  722. (assoc-string spec buffer-invisibility-spec))
  723. 0))
  724. (`display
  725. (pcase spec
  726. (`nil nil)
  727. (`(space . ,props)
  728. (let ((width (plist-get props :width)))
  729. (and (wholenump width) width)))
  730. (`(image . ,_)
  731. (ceiling (car (image-size spec))))
  732. ((pred stringp)
  733. ;; Displayed string could contain invisible parts,
  734. ;; but no nested display.
  735. (org--string-from-props spec 'invisible 0 (length spec)))
  736. (_
  737. ;; Un-handled `display' value. Ignore it.
  738. ;; Consider the original string instead.
  739. nil)))
  740. (_ (error "Unknown property: %S" property)))))
  741. (when value
  742. (cl-incf width
  743. ;; When looking for `display' parts, we still need
  744. ;; to look for `invisible' property elsewhere.
  745. (+ (cond ((eq property 'display)
  746. (org--string-from-props s 'invisible cursor beg))
  747. ((= cursor beg) 0)
  748. (t (string-width (substring s cursor beg))))
  749. value))
  750. (setq cursor next))
  751. (setq beg next)))
  752. (+ width
  753. ;; Look for `invisible' property in the last part of the
  754. ;; string. See above.
  755. (cond ((eq property 'display)
  756. (org--string-from-props s 'invisible cursor end))
  757. ((= cursor end) 0)
  758. (t (string-width (substring s cursor end)))))))
  759. (defun org-string-width (string)
  760. "Return width of STRING when displayed in the current buffer.
  761. Unlike `string-width', this function takes into consideration
  762. `invisible' and `display' text properties. It supports the
  763. latter in a limited way, mostly for combinations used in Org.
  764. Results may be off sometimes if it cannot handle a given
  765. `display' value."
  766. (org--string-from-props string 'display 0 (length string)))
  767. (defun org-not-nil (v)
  768. "If V not nil, and also not the string \"nil\", then return V.
  769. Otherwise return nil."
  770. (and v (not (equal v "nil")) v))
  771. (defun org-unbracket-string (pre post string)
  772. "Remove PRE/POST from the beginning/end of STRING.
  773. Both PRE and POST must be pre-/suffixes of STRING, or neither is
  774. removed. Return the new string. If STRING is nil, return nil."
  775. (declare (indent 2))
  776. (and string
  777. (if (and (string-prefix-p pre string)
  778. (string-suffix-p post string))
  779. (substring string (length pre) (- (length post)))
  780. string)))
  781. (defun org-strip-quotes (string)
  782. "Strip double quotes from around STRING, if applicable.
  783. If STRING is nil, return nil."
  784. (org-unbracket-string "\"" "\"" string))
  785. (defsubst org-current-line-string (&optional to-here)
  786. "Return current line, as a string.
  787. If optional argument TO-HERE is non-nil, return string from
  788. beginning of line up to point."
  789. (buffer-substring (line-beginning-position)
  790. (if to-here (point) (line-end-position))))
  791. (defun org-shorten-string (s maxlength)
  792. "Shorten string S so that it is no longer than MAXLENGTH characters.
  793. If the string is shorter or has length MAXLENGTH, just return the
  794. original string. If it is longer, the functions finds a space in the
  795. string, breaks this string off at that locations and adds three dots
  796. as ellipsis. Including the ellipsis, the string will not be longer
  797. than MAXLENGTH. If finding a good breaking point in the string does
  798. not work, the string is just chopped off in the middle of a word
  799. if necessary."
  800. (if (<= (length s) maxlength)
  801. s
  802. (let* ((n (max (- maxlength 4) 1))
  803. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  804. (if (string-match re s)
  805. (concat (match-string 1 s) "...")
  806. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  807. (defun org-remove-tabs (s &optional width)
  808. "Replace tabulators in S with spaces.
  809. Assumes that s is a single line, starting in column 0."
  810. (setq width (or width tab-width))
  811. (while (string-match "\t" s)
  812. (setq s (replace-match
  813. (make-string
  814. (- (* width (/ (+ (match-beginning 0) width) width))
  815. (match-beginning 0)) ?\ )
  816. t t s)))
  817. s)
  818. (defun org-wrap (string &optional width lines)
  819. "Wrap string to either a number of lines, or a width in characters.
  820. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  821. that costs. If there is a word longer than WIDTH, the text is actually
  822. wrapped to the length of that word.
  823. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  824. many lines, whatever width that takes.
  825. The return value is a list of lines, without newlines at the end."
  826. (let* ((words (split-string string))
  827. (maxword (apply 'max (mapcar 'org-string-width words)))
  828. w ll)
  829. (cond (width
  830. (org--do-wrap words (max maxword width)))
  831. (lines
  832. (setq w maxword)
  833. (setq ll (org--do-wrap words maxword))
  834. (if (<= (length ll) lines)
  835. ll
  836. (setq ll words)
  837. (while (> (length ll) lines)
  838. (setq w (1+ w))
  839. (setq ll (org--do-wrap words w)))
  840. ll))
  841. (t (error "Cannot wrap this")))))
  842. (defun org--do-wrap (words width)
  843. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  844. (let (lines line)
  845. (while words
  846. (setq line (pop words))
  847. (while (and words (< (+ (length line) (length (car words))) width))
  848. (setq line (concat line " " (pop words))))
  849. (setq lines (push line lines)))
  850. (nreverse lines)))
  851. (defun org-remove-indentation (code &optional n)
  852. "Remove maximum common indentation in string CODE and return it.
  853. N may optionally be the number of columns to remove. Return CODE
  854. as-is if removal failed."
  855. (with-temp-buffer
  856. (insert code)
  857. (if (org-do-remove-indentation n) (buffer-string) code)))
  858. ;;; Text properties
  859. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  860. rear-nonsticky t mouse-map t fontified t
  861. org-emphasis t)
  862. "Properties to remove when a string without properties is wanted.")
  863. (defsubst org-no-properties (s &optional restricted)
  864. "Remove all text properties from string S.
  865. When RESTRICTED is non-nil, only remove the properties listed
  866. in `org-rm-props'."
  867. (if restricted (remove-text-properties 0 (length s) org-rm-props s)
  868. (set-text-properties 0 (length s) nil s))
  869. s)
  870. (defun org-add-props (string plist &rest props)
  871. "Add text properties to entire string, from beginning to end.
  872. PLIST may be a list of properties, PROPS are individual properties and values
  873. that will be added to PLIST. Returns the string that was modified."
  874. (declare (indent 2))
  875. (add-text-properties
  876. 0 (length string) (if props (append plist props) plist) string)
  877. string)
  878. (defun org-make-parameter-alist (flat)
  879. "Return alist based on FLAT.
  880. FLAT is a list with alternating symbol names and values. The
  881. returned alist is a list of lists with the symbol name in car and
  882. the value in cdr."
  883. (when flat
  884. (cons (list (car flat) (cadr flat))
  885. (org-make-parameter-alist (cddr flat)))))
  886. (defsubst org-get-at-bol (property)
  887. "Get text property PROPERTY at the beginning of line."
  888. (get-text-property (point-at-bol) property))
  889. (defun org-get-at-eol (property n)
  890. "Get text property PROPERTY at the end of line less N characters."
  891. (get-text-property (- (point-at-eol) n) property))
  892. (defun org-find-text-property-in-string (prop s)
  893. "Return the first non-nil value of property PROP in string S."
  894. (or (get-text-property 0 prop s)
  895. (get-text-property (or (next-single-property-change 0 prop s) 0)
  896. prop s)))
  897. (defun org-invisible-p (&optional pos)
  898. "Non-nil if the character after POS is invisible.
  899. If POS is nil, use `point' instead."
  900. (get-char-property (or pos (point)) 'invisible))
  901. (defun org-truely-invisible-p ()
  902. "Check if point is at a character currently not visible.
  903. This version does not only check the character property, but also
  904. `visible-mode'."
  905. (unless (bound-and-true-p visible-mode)
  906. (org-invisible-p)))
  907. (defun org-invisible-p2 ()
  908. "Check if point is at a character currently not visible.
  909. If the point is at EOL (and not at the beginning of a buffer too),
  910. move it back by one char before doing this check."
  911. (save-excursion
  912. (when (and (eolp) (not (bobp)))
  913. (backward-char 1))
  914. (org-invisible-p)))
  915. ;;; Time
  916. (defun org-2ft (s)
  917. "Convert S to a floating point time.
  918. If S is already a number, just return it. If it is a string,
  919. parse it as a time string and apply `float-time' to it. If S is
  920. nil, just return 0."
  921. (cond
  922. ((numberp s) s)
  923. ((stringp s)
  924. (condition-case nil
  925. (float-time (apply #'encode-time (org-parse-time-string s)))
  926. (error 0)))
  927. (t 0)))
  928. (defun org-time= (a b)
  929. (let ((a (org-2ft a))
  930. (b (org-2ft b)))
  931. (and (> a 0) (> b 0) (= a b))))
  932. (defun org-time< (a b)
  933. (let ((a (org-2ft a))
  934. (b (org-2ft b)))
  935. (and (> a 0) (> b 0) (< a b))))
  936. (defun org-time<= (a b)
  937. (let ((a (org-2ft a))
  938. (b (org-2ft b)))
  939. (and (> a 0) (> b 0) (<= a b))))
  940. (defun org-time> (a b)
  941. (let ((a (org-2ft a))
  942. (b (org-2ft b)))
  943. (and (> a 0) (> b 0) (> a b))))
  944. (defun org-time>= (a b)
  945. (let ((a (org-2ft a))
  946. (b (org-2ft b)))
  947. (and (> a 0) (> b 0) (>= a b))))
  948. (defun org-time<> (a b)
  949. (let ((a (org-2ft a))
  950. (b (org-2ft b)))
  951. (and (> a 0) (> b 0) (\= a b))))
  952. (defun org-parse-time-string (s &optional nodefault)
  953. "Parse Org time string S.
  954. If time is not given, defaults to 0:00. However, with optional
  955. NODEFAULT, hour and minute fields are nil if not given.
  956. Throw an error if S does not contain a valid Org time string.
  957. Note that the first match for YYYY-MM-DD will be used (e.g.,
  958. \"-52000-02-03\" will be taken as \"2000-02-03\").
  959. This should be a lot faster than the `parse-time-string'."
  960. (unless (string-match org-ts-regexp0 s)
  961. (error "Not an Org time string: %s" s))
  962. (list 0
  963. (cond ((match-beginning 8) (string-to-number (match-string 8 s)))
  964. (nodefault nil)
  965. (t 0))
  966. (cond ((match-beginning 7) (string-to-number (match-string 7 s)))
  967. (nodefault nil)
  968. (t 0))
  969. (string-to-number (match-string 4 s))
  970. (string-to-number (match-string 3 s))
  971. (string-to-number (match-string 2 s))
  972. nil nil nil))
  973. (defun org-matcher-time (s)
  974. "Interpret a time comparison value S as a floating point time.
  975. S can be an Org time stamp, a modifier, e.g., \"<+2d>\", or the
  976. following special strings: \"<now>\", \"<today>\",
  977. \"<tomorrow>\", and \"<yesterday>\".
  978. Return 0. if S is not recognized as a valid value."
  979. (let ((today (float-time (apply #'encode-time
  980. (append '(0 0 0) (nthcdr 3 (decode-time)))))))
  981. (save-match-data
  982. (cond
  983. ((string= s "<now>") (float-time))
  984. ((string= s "<today>") today)
  985. ((string= s "<tomorrow>") (+ 86400.0 today))
  986. ((string= s "<yesterday>") (- today 86400.0))
  987. ((string-match "\\`<\\([-+][0-9]+\\)\\([hdwmy]\\)>\\'" s)
  988. (+ today
  989. (* (string-to-number (match-string 1 s))
  990. (cdr (assoc (match-string 2 s)
  991. '(("d" . 86400.0) ("w" . 604800.0)
  992. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  993. ((string-match org-ts-regexp0 s) (org-2ft s))
  994. (t 0.)))))
  995. (provide 'org-macs)
  996. ;;; org-macs.el ends here