org-macs.el (69350B)
1 ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2004-2024 Free Software Foundation, Inc. 4 5 ;; Author: Carsten Dominik <carsten.dominik@gmail.com> 6 ;; Keywords: outlines, hypermedia, calendar, text 7 ;; URL: https://orgmode.org 8 ;; 9 ;; This file is part of GNU Emacs. 10 ;; 11 ;; GNU Emacs is free software: you can redistribute it and/or modify 12 ;; it under the terms of the GNU General Public License as published by 13 ;; the Free Software Foundation, either version 3 of the License, or 14 ;; (at your option) any later version. 15 16 ;; GNU Emacs is distributed in the hope that it will be useful, 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; GNU General Public License for more details. 20 21 ;; You should have received a copy of the GNU General Public License 22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 24 ;; 25 ;;; Commentary: 26 27 ;; This file contains macro definitions, defsubst definitions, other 28 ;; stuff needed for compilation and top-level forms in Org mode, as 29 ;; well lots of small functions that are not Org mode specific but 30 ;; simply generally useful stuff. 31 32 ;;; Code: 33 34 (require 'cl-lib) 35 (require 'format-spec) 36 (eval-when-compile (require 'subr-x)) ; For `when-let*', Emacs < 29 37 38 ;;; Org version verification. 39 40 (defvar org--inhibit-version-check nil 41 "When non-nil, skip the detection of mixed-versions situations. 42 For internal use only. See Emacs bug #62762. 43 This variable is only supposed to be changed by Emacs build scripts. 44 When nil, Org tries to detect when Org source files were compiled with 45 a different version of Org (which tends to lead to incorrect `.elc' files), 46 or when the current Emacs session has loaded a mix of files from different 47 Org versions (typically the one bundled with Emacs and another one installed 48 from GNU ELPA), which can happen if some parts of Org were loaded before 49 `load-path' was changed (e.g. before the GNU-ELPA-installed Org is activated 50 by `package-activate-all').") 51 (defmacro org-assert-version () 52 "Assert compile time and runtime version match." 53 ;; We intentionally use a more permissive `org-release' instead of 54 ;; `org-git-version' to work around deficiencies in Elisp 55 ;; compilation after pulling latest changes. Unchanged files will 56 ;; not be re-compiled and thus their macro-expanded 57 ;; `org-assert-version' calls would fail using strict 58 ;; `org-git-version' check because the generated Org version strings 59 ;; will not match. 60 `(unless (or ,org--inhibit-version-check (equal (org-release) ,(org-release))) 61 (warn "Org version mismatch. 62 This warning usually appears when a built-in Org version is loaded 63 prior to the more recent Org version. 64 65 Version mismatch is commonly encountered in the following situations: 66 67 1. Emacs is loaded using literate Org config and more recent Org 68 version is loaded inside the file loaded by `org-babel-load-file'. 69 `org-babel-load-file' triggers the built-in Org version clashing 70 the newer Org version attempt to be loaded later. 71 72 It is recommended to move the Org loading code before the 73 `org-babel-load-file' call. 74 75 2. New Org version is loaded manually by setting `load-path', but some 76 other package depending on Org is loaded before the `load-path' is 77 configured. 78 This \"other package\" is triggering built-in Org version, again 79 causing the version mismatch. 80 81 It is recommended to set `load-path' as early in the config as 82 possible. 83 84 3. New Org version is loaded using straight.el package manager and 85 other package depending on Org is loaded before straight triggers 86 loading of the newer Org version. 87 88 It is recommended to put 89 90 %s 91 92 early in the config. Ideally, right after the straight.el 93 bootstrap. Moving `use-package' :straight declaration may not be 94 sufficient if the corresponding `use-package' statement is 95 deferring the loading. 96 97 4. A new Org version is synchronized with Emacs git repository and 98 stale .elc files are still left from the previous build. 99 100 It is recommended to remove .elc files from lisp/org directory and 101 re-compile." 102 ;; Avoid `warn' replacing "'" with "’" (see `format-message'). 103 "(straight-use-package 'org)"))) 104 105 ;; We rely on org-macs when generating Org version. Checking Org 106 ;; version here will interfere with Org build process. 107 ;; (org-assert-version) 108 109 (declare-function org-mode "org" ()) 110 (declare-function org-agenda-files "org" (&optional unrestricted archives)) 111 (declare-function org-time-string-to-seconds "org" (s)) 112 (declare-function org-fold-show-context "org-fold" (&optional key)) 113 (declare-function org-fold-save-outline-visibility "org-fold" (use-markers &rest body)) 114 (declare-function org-fold-next-visibility-change "org-fold" (&optional pos limit ignore-hidden-p previous-p)) 115 (declare-function org-fold-core-with-forced-fontification "org-fold" (&rest body)) 116 (declare-function org-fold-folded-p "org-fold" (&optional pos limit ignore-hidden-p previous-p)) 117 (declare-function org-time-convert-to-list "org-compat" (time)) 118 (declare-function org-buffer-text-pixel-width "org-compat" ()) 119 120 (defvar org-ts-regexp0) 121 (defvar ffap-url-regexp) 122 123 124 ;;; Macros 125 126 (defmacro org-require-package (symbol &optional name noerror) 127 "Try to load library SYMBOL and display error otherwise. 128 With optional parameter NAME, use NAME as package name instead of 129 SYMBOL. Show warning instead of error when NOERROR is non-nil." 130 `(unless (require ,symbol nil t) 131 (,(if noerror 'warn 'user-error) 132 "`%s' failed to load required package \"%s\"" 133 this-command ,(or name symbol)))) 134 135 (defmacro org-with-gensyms (symbols &rest body) 136 (declare (debug (sexp body)) (indent 1)) 137 `(let ,(mapcar (lambda (s) 138 `(,s (make-symbol (concat "--" (symbol-name ',s))))) 139 symbols) 140 ,@body)) 141 142 ;; Use `with-silent-modifications' to ignore cosmetic changes and 143 ;; `org-unmodified' to ignore real text modifications. 144 (defmacro org-unmodified (&rest body) 145 "Run BODY while preserving the buffer's `buffer-modified-p' state." 146 (declare (debug (body))) 147 (org-with-gensyms (was-modified) 148 `(let ((,was-modified (buffer-modified-p))) 149 (unwind-protect 150 (let ((buffer-undo-list t) 151 (inhibit-modification-hooks t)) 152 ,@body) 153 (set-buffer-modified-p ,was-modified))))) 154 155 (defmacro org-with-base-buffer (buffer &rest body) 156 "Run BODY in base buffer for BUFFER. 157 If BUFFER is nil, use base buffer for `current-buffer'." 158 (declare (debug (body)) (indent 1)) 159 `(with-current-buffer (or (buffer-base-buffer ,buffer) 160 (or ,buffer (current-buffer))) 161 ,@body)) 162 163 (defmacro org-with-point-at (epom &rest body) 164 "Move to buffer and point of EPOM for the duration of BODY. 165 EPOM is an element, point, or marker." 166 (declare (debug (form body)) (indent 1)) 167 (require 'org-element-ast) 168 (org-with-gensyms (mepom) 169 `(let ((,mepom ,epom)) 170 (save-excursion 171 (cond 172 ((markerp ,mepom) 173 (set-buffer (marker-buffer ,mepom))) 174 ((numberp ,mepom)) 175 (t 176 (when (org-element-property :buffer ,mepom) 177 (set-buffer (org-element-property :buffer ,mepom))) 178 (setq ,mepom (org-element-property :begin ,mepom)))) 179 (org-with-wide-buffer 180 (goto-char (or ,mepom (point))) 181 ,@body))))) 182 183 (defmacro org-with-remote-undo (buffer &rest body) 184 "Execute BODY while recording undo information in current buffer and BUFFER. 185 This function is only useful when called from Agenda buffer." 186 (declare (debug (form body)) (indent 1)) 187 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2) 188 `(let ((,cline (org-current-line)) 189 (,cmd this-command) 190 (,buf1 (current-buffer)) 191 (,buf2 ,buffer) 192 (,undo1 buffer-undo-list) 193 (,undo2 (with-current-buffer ,buffer buffer-undo-list)) 194 ,c1 ,c2) 195 ,@body 196 (when org-agenda-allow-remote-undo 197 (setq ,c1 (org-verify-change-for-undo 198 ,undo1 (with-current-buffer ,buf1 buffer-undo-list)) 199 ,c2 (org-verify-change-for-undo 200 ,undo2 (with-current-buffer ,buf2 buffer-undo-list))) 201 (when (or ,c1 ,c2) 202 ;; make sure there are undo boundaries 203 (and ,c1 (with-current-buffer ,buf1 (undo-boundary))) 204 (and ,c2 (with-current-buffer ,buf2 (undo-boundary))) 205 ;; remember which buffer to undo 206 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2) 207 org-agenda-undo-list)))))) 208 209 (defmacro org-no-read-only (&rest body) 210 "Inhibit read-only for BODY." 211 (declare (debug (body))) 212 `(let ((inhibit-read-only t)) ,@body)) 213 214 (defalias 'org-save-outline-visibility #'org-fold-save-outline-visibility) 215 216 (defmacro org-with-wide-buffer (&rest body) 217 "Execute BODY while temporarily widening the buffer." 218 (declare (debug (body))) 219 `(save-excursion 220 (save-restriction 221 (widen) 222 ,@body))) 223 224 (defmacro org-with-limited-levels (&rest body) 225 "Execute BODY with limited number of outline levels." 226 (declare (debug (body))) 227 `(progn 228 (defvar org-called-with-limited-levels) 229 (defvar org-outline-regexp) 230 (defvar outline-regexp) 231 (defvar org-outline-regexp-bol) 232 (let* ((org-called-with-limited-levels t) 233 (org-outline-regexp (org-get-limited-outline-regexp)) 234 (outline-regexp org-outline-regexp) 235 (org-outline-regexp-bol (org-get-limited-outline-regexp t))) 236 ,@body))) 237 238 (defmacro org-eval-in-environment (environment form) 239 (declare (debug (form form)) (indent 1) (obsolete cl-progv "2021")) 240 `(eval (list 'let ,environment ',form))) 241 242 ;;;###autoload 243 (defmacro org-load-noerror-mustsuffix (file) 244 "Load FILE with optional arguments NOERROR and MUSTSUFFIX." 245 `(load ,file 'noerror nil nil 'mustsuffix)) 246 247 (defmacro org-preserve-local-variables (&rest body) 248 "Execute BODY while preserving local variables." 249 (declare (debug (body))) 250 `(let ((local-variables 251 (org-with-wide-buffer 252 (goto-char (point-max)) 253 (let ((case-fold-search t)) 254 (and (re-search-backward "^[ \t]*# +Local Variables:" 255 (max (- (point) 3000) 1) 256 t) 257 (let ((buffer-undo-list t)) 258 (delete-and-extract-region (point) (point-max))))))) 259 (tick-counter-before (buffer-modified-tick))) 260 (unwind-protect (progn ,@body) 261 (when local-variables 262 (org-with-wide-buffer 263 (goto-char (point-max)) 264 (unless (bolp) (insert "\n")) 265 (let ((modified (< tick-counter-before (buffer-modified-tick))) 266 (buffer-undo-list t)) 267 (insert local-variables) 268 (unless modified 269 (restore-buffer-modified-p nil)))))))) 270 271 ;;;###autoload 272 (defmacro org-element-with-disabled-cache (&rest body) 273 "Run BODY without active org-element-cache." 274 (declare (debug (form body)) (indent 0)) 275 `(cl-letf (((symbol-function #'org-element--cache-active-p) (lambda (&rest _) nil))) 276 ,@body)) 277 278 279 ;;; Buffer and windows 280 281 (defun org-base-buffer (buffer) 282 "Return the base buffer of BUFFER, if it has one. Else return the buffer." 283 (when buffer 284 (or (buffer-base-buffer buffer) 285 buffer))) 286 287 (defun org-find-base-buffer-visiting (file) 288 "Like `find-buffer-visiting' but always return the base buffer. 289 FILE is the file name passed to `find-buffer-visiting'." 290 (let ((buf (or (get-file-buffer file) 291 (find-buffer-visiting file)))) 292 (org-base-buffer buf))) 293 294 (defvar-local org-file-buffer-created nil 295 "Non-nil when current buffer is created from `org-with-file-buffer'. 296 The value is FILE argument passed to `org-with-file-buffer'.") 297 (defmacro org-with-file-buffer (file &rest body) 298 "Evaluate BODY with current buffer visiting FILE. 299 When no live buffer is visiting FILE, create one and kill after 300 evaluating BODY. 301 During evaluation, when the buffer was created, `org-file-buffer-created' 302 variable is set to FILE." 303 (declare (debug (form body)) (indent 1)) 304 (org-with-gensyms (mark-function filename buffer) 305 `(let ((,mark-function (lambda () (setq-local org-file-buffer-created ,file))) 306 (,filename ,file) 307 ,buffer) 308 (add-hook 'find-file-hook ,mark-function) 309 (unwind-protect 310 (progn 311 (setq ,buffer (find-file-noselect ,filename t)) 312 (with-current-buffer ,buffer 313 (prog1 (progn ,@body) 314 (with-current-buffer ,buffer 315 (when (equal ,filename org-file-buffer-created) 316 (kill-buffer)))))) 317 (remove-hook 'find-file-hook ,mark-function))))) 318 319 (defun org-fit-window-to-buffer (&optional window max-height min-height 320 shrink-only) 321 "Fit WINDOW to the buffer, but only if it is not a side-by-side window. 322 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are 323 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call 324 `shrink-window-if-larger-than-buffer' instead, the height limit is 325 ignored in this case." 326 (cond ((not (window-full-width-p window)) 327 ;; Do nothing if another window would suffer. 328 ) 329 ((not shrink-only) 330 (fit-window-to-buffer window max-height min-height)) 331 (t (shrink-window-if-larger-than-buffer window))) 332 (or window (selected-window))) 333 334 (defun org-buffer-list (&optional predicate exclude-tmp) 335 "Return a list of Org buffers. 336 PREDICATE can be `export', `files' or `agenda'. 337 338 export restrict the list to Export buffers. 339 files restrict the list to buffers visiting Org files. 340 agenda restrict the list to buffers visiting agenda files. 341 342 If EXCLUDE-TMP is non-nil, ignore temporary buffers." 343 (let* ((bfn nil) 344 (agenda-files (and (eq predicate 'agenda) 345 (mapcar 'file-truename (org-agenda-files t)))) 346 (filter 347 (cond 348 ((eq predicate 'files) 349 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode)))) 350 ((eq predicate 'export) 351 (lambda (b) (string-match "\\*Org .*Export" (buffer-name b)))) 352 ((eq predicate 'agenda) 353 (lambda (b) 354 (with-current-buffer b 355 (and (derived-mode-p 'org-mode) 356 (setq bfn (buffer-file-name b)) 357 (member (file-truename bfn) agenda-files))))) 358 (t (lambda (b) (with-current-buffer b 359 (or (derived-mode-p 'org-mode) 360 (string-match "\\*Org .*Export" 361 (buffer-name b))))))))) 362 (delq nil 363 (mapcar 364 (lambda(b) 365 (if (and (funcall filter b) 366 (or (not exclude-tmp) 367 (not (string-match "tmp" (buffer-name b))))) 368 b 369 nil)) 370 (buffer-list))))) 371 372 373 374 ;;; File 375 376 (defun org-file-newer-than-p (file time) 377 "Non-nil if FILE modification time is greater than TIME. 378 TIME should be obtained earlier for the same FILE name using 379 380 \(file-attribute-modification-time (file-attributes file)) 381 382 If TIME is nil (file did not exist) then any existing FILE 383 is considered as a newer one. Some file systems have coarse 384 timestamp resolution, for example 1 second on HFS+ or 2 seconds on FAT, 385 so nil may be returned when file is updated twice within a short period 386 of time. File timestamp and system clock `current-time' may have 387 different resolution, so attempts to compare them may give unexpected 388 results. 389 390 Consider `file-newer-than-file-p' to check up to date state 391 in target-prerequisite files relation." 392 (let ((mtime (file-attribute-modification-time (file-attributes file)))) 393 (and mtime (or (not time) (time-less-p time mtime))))) 394 395 396 ;;; Indentation 397 398 (defmacro org-current-text-indentation () 399 "Like `current-indentation', but ignore display/invisible properties." 400 `(let ((buffer-invisibility-spec nil)) 401 (current-indentation))) 402 403 (defun org-do-remove-indentation (&optional n skip-fl) 404 "Remove the maximum common indentation from the buffer. 405 Do not consider invisible text when calculating indentation. 406 407 When optional argument N is a positive integer, remove exactly 408 that much characters from indentation, if possible. When 409 optional argument SKIP-FL is non-nil, skip the first 410 line. Return nil if it fails." 411 (catch :exit 412 (goto-char (point-min)) 413 ;; Find maximum common indentation, if not specified. 414 (let ((n (or n 415 (let ((min-ind (point-max))) 416 (save-excursion 417 (when skip-fl (forward-line)) 418 (while (re-search-forward "^[ \t]*\\S-" nil t) 419 (let ((ind (org-current-text-indentation))) 420 (if (zerop ind) (throw :exit nil) 421 (setq min-ind (min min-ind ind)))))) 422 min-ind)))) 423 (if (zerop n) (throw :exit nil) 424 ;; Remove exactly N indentation, but give up if not possible. 425 (when skip-fl (forward-line)) 426 (while (not (eobp)) 427 (let* ((buffer-invisibility-spec nil) ; do not treat invisible text specially 428 (ind (progn (skip-chars-forward " \t") (current-column)))) 429 (cond ((< ind n) 430 (if (eolp) (delete-region (line-beginning-position) (point)) 431 (throw :exit nil))) 432 (t (delete-region (line-beginning-position) 433 (progn (move-to-column n t) 434 (point))))) 435 (forward-line))) 436 ;; Signal success. 437 t)))) 438 439 440 441 ;;; Input 442 443 (defun org-read-function (prompt &optional allow-empty?) 444 "Prompt for a function. 445 If ALLOW-EMPTY? is non-nil, return nil rather than raising an 446 error when the user input is empty." 447 (let ((func (completing-read prompt obarray #'fboundp t))) 448 (cond ((not (string= func "")) 449 (intern func)) 450 (allow-empty? nil) 451 (t (user-error "Empty input is not valid"))))) 452 453 (declare-function org-timestamp-inactive "org" (&optional arg)) 454 455 (defun org-completing-read (&rest args) 456 "Completing-read with SPACE being a normal character." 457 (let ((enable-recursive-minibuffers t) 458 (minibuffer-local-completion-map 459 (copy-keymap minibuffer-local-completion-map))) 460 (define-key minibuffer-local-completion-map " " #'self-insert-command) 461 (define-key minibuffer-local-completion-map "?" #'self-insert-command) 462 (define-key minibuffer-local-completion-map (kbd "C-c !") 463 #'org-timestamp-inactive) 464 (apply #'completing-read args))) 465 466 (defun org--mks-read-key (allowed-keys prompt navigation-keys) 467 "Read a key and ensure it is a member of ALLOWED-KEYS. 468 Enable keys to scroll the window if NAVIGATION-KEYS is set. 469 TAB, SPC and RET are treated equivalently." 470 (setq header-line-format (when navigation-keys "Use C-n, C-p, C-v, M-v to navigate.")) 471 (let ((char-key (read-char-exclusive prompt))) 472 (if (and navigation-keys (memq char-key '(14 16 22 134217846))) 473 (progn 474 (org-scroll char-key) 475 (org--mks-read-key allowed-keys prompt navigation-keys)) 476 (let ((key (char-to-string 477 (pcase char-key 478 ((or ?\s ?\t ?\r) ?\t) 479 (char char))))) 480 (if (member key allowed-keys) 481 key 482 (message "Invalid key: `%s'" key) 483 (sit-for 1) 484 (org--mks-read-key allowed-keys prompt navigation-keys)))))) 485 486 (defun org-mks (table title &optional prompt specials) 487 "Select a member of an alist with multiple keys. 488 489 TABLE is the alist which should contain entries where the car is a string. 490 There should be two types of entries. 491 492 1. prefix descriptions like (\"a\" \"Description\") 493 This indicates that `a' is a prefix key for multi-letter selection, and 494 that there are entries following with keys like \"ab\", \"ax\"... 495 496 2. Select-able members must have more than two elements, with the first 497 being the string of keys that lead to selecting it, and the second a 498 short description string of the item. 499 500 The command will then make a temporary buffer listing all entries 501 that can be selected with a single key, and all the single key 502 prefixes. When you press the key for a single-letter entry, it is selected. 503 When you press a prefix key, the commands (and maybe further prefixes) 504 under this key will be shown and offered for selection. 505 506 TITLE will be placed over the selection in the temporary buffer, 507 PROMPT will be used when prompting for a key. SPECIALS is an 508 alist with (\"key\" \"description\") entries. When one of these 509 is selected, only the bare key is returned." 510 (save-window-excursion 511 (let ((inhibit-quit t) 512 (buffer (switch-to-buffer-other-window "*Org Select*")) 513 (prompt (or prompt "Select: ")) 514 case-fold-search 515 current) 516 (unwind-protect 517 (catch 'exit 518 (while t 519 (erase-buffer) 520 (insert title "\n\n") 521 (let ((des-keys nil) 522 (allowed-keys '("\C-g")) 523 (tab-alternatives '("\s" "\t" "\r")) 524 (cursor-type nil)) 525 ;; Populate allowed keys and descriptions keys 526 ;; available with CURRENT selector. 527 (let ((re (format "\\`%s\\(.\\)\\'" 528 (if current (regexp-quote current) ""))) 529 (prefix (if current (concat current " ") ""))) 530 (dolist (entry table) 531 (pcase entry 532 ;; Description. 533 (`(,(and key (pred (string-match re))) ,desc) 534 (let ((k (match-string 1 key))) 535 (push k des-keys) 536 ;; Keys ending in tab, space or RET are equivalent. 537 (if (member k tab-alternatives) 538 (push "\t" allowed-keys) 539 (push k allowed-keys)) 540 (insert prefix "[" k "]" "..." " " desc "..." "\n"))) 541 ;; Usable entry. 542 (`(,(and key (pred (string-match re))) ,desc . ,_) 543 (let ((k (match-string 1 key))) 544 (insert prefix "[" k "]" " " desc "\n") 545 (push k allowed-keys))) 546 (_ nil)))) 547 ;; Insert special entries, if any. 548 (when specials 549 (insert "----------------------------------------------------\ 550 ---------------------------\n") 551 (pcase-dolist (`(,key ,description) specials) 552 (insert (format "[%s] %s\n" key description)) 553 (push key allowed-keys))) 554 ;; Display UI and let user select an entry or 555 ;; a sub-level prefix. 556 (goto-char (point-min)) 557 (org-fit-window-to-buffer) 558 (message "") ; With this line the prompt appears in 559 ; the minibuffer. Else keystrokes may 560 ; appear, which is spurious. 561 (let ((pressed (org--mks-read-key 562 allowed-keys prompt 563 (not (pos-visible-in-window-p (1- (point-max))))))) 564 (setq current (concat current pressed)) 565 (cond 566 ((equal pressed "\C-g") (user-error "Abort")) 567 ;; Selection is a prefix: open a new menu. 568 ((member pressed des-keys)) 569 ;; Selection matches an association: return it. 570 ((let ((entry (assoc current table))) 571 (and entry (throw 'exit entry)))) 572 ;; Selection matches a special entry: return the 573 ;; selection prefix. 574 ((assoc current specials) (throw 'exit current)) 575 (t (error "No entry available"))))))) 576 (when buffer 577 (when-let* ((window (get-buffer-window buffer t))) 578 (quit-window 'kill window)) 579 (kill-buffer buffer)))))) 580 581 582 ;;; List manipulation 583 584 (defsubst org-get-alist-option (option key) 585 (cond ((eq key t) t) 586 ((eq option t) t) 587 ((assoc key option) (cdr (assoc key option))) 588 (t (let ((r (cdr (assq 'default option)))) 589 (if (listp r) (delq nil r) r))))) 590 591 (defsubst org-last (list) 592 "Return the last element of LIST." 593 (car (last list))) 594 595 (defsubst org-uniquify (list) 596 "Non-destructively remove duplicate elements from LIST." 597 (let ((res (copy-sequence list))) (delete-dups res))) 598 599 (defun org-uniquify-alist (alist) 600 "Merge elements of ALIST with the same key. 601 602 For example, in this alist: 603 604 \(org-uniquify-alist \\='((a 1) (b 2) (a 3))) 605 => ((a 1 3) (b 2)) 606 607 merge (a 1) and (a 3) into (a 1 3). 608 609 The function returns the new ALIST." 610 (let (rtn) 611 (dolist (e alist rtn) 612 (let (n) 613 (if (not (assoc (car e) rtn)) 614 (push e rtn) 615 (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e)))) 616 (setq rtn (assq-delete-all (car e) rtn)) 617 (push n rtn)))))) 618 619 (defun org-delete-all (elts list) 620 "Remove all elements in ELTS from LIST. 621 Comparison is done with `equal'. It is a destructive operation 622 that may remove elements by altering the list structure." 623 (while elts 624 (setq list (delete (pop elts) list))) 625 list) 626 627 (defun org-plist-delete-all (plist props) 628 "Delete all elements in PROPS from PLIST." 629 (dolist (e props plist) 630 (setq plist (org-plist-delete plist e)))) 631 632 (defun org-plist-delete (plist property) 633 "Delete PROPERTY from PLIST. 634 This is in contrast to merely setting it to 0." 635 (let (p) 636 (while plist 637 (if (not (eq property (car plist))) 638 (setq p (plist-put p (car plist) (nth 1 plist)))) 639 (setq plist (cddr plist))) 640 p)) 641 642 (defun org-combine-plists (&rest plists) 643 "Create a single property list from all plists in PLISTS. 644 The process starts by copying the first list, and then setting properties 645 from the other lists. Settings in the last list are the most significant 646 ones and overrule settings in the other lists." 647 (let ((rtn (copy-sequence (pop plists))) 648 p v ls) 649 (while plists 650 (setq ls (pop plists)) 651 (while ls 652 (setq p (pop ls) v (pop ls)) 653 (setq rtn (plist-put rtn p v)))) 654 rtn)) 655 656 657 658 ;;; Local variables 659 660 (defconst org-unique-local-variables 661 '(org-element--cache 662 org-element--headline-cache 663 org-element--cache-change-tic 664 org-element--cache-last-buffer-size 665 org-element--cache-change-warning 666 org-element--cache-gapless 667 org-element--cache-hash-left 668 org-element--cache-hash-right 669 org-element--cache-size 670 org-element--headline-cache-size 671 org-element--cache-sync-keys-value 672 org-element--cache-diagnostics-ring 673 org-element--cache-diagnostics-ring-size 674 org-element--cache-sync-keys 675 org-element--cache-sync-requests 676 org-element--cache-sync-timer) 677 "List of local variables that cannot be transferred to another buffer.") 678 679 (defun org-get-local-variables () 680 "Return a list of all local variables in an Org mode buffer." 681 (delq nil 682 (mapcar 683 (lambda (x) 684 (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x)))) 685 (name (car binding))) 686 (and (not (get name 'org-state)) 687 (not (memq name org-unique-local-variables)) 688 (string-match-p 689 "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\ 690 auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)" 691 (symbol-name name)) 692 binding))) 693 (with-temp-buffer 694 (org-mode) 695 (buffer-local-variables))))) 696 697 (defun org-clone-local-variables (from-buffer &optional regexp) 698 "Clone local variables from FROM-BUFFER. 699 Optional argument REGEXP selects variables to clone." 700 (dolist (pair (buffer-local-variables from-buffer)) 701 (pcase pair 702 (`(,name . ,value) ;ignore unbound variables 703 (when (and (not (memq name org-unique-local-variables)) 704 (or (null regexp) (string-match-p regexp (symbol-name name)))) 705 (ignore-errors (set (make-local-variable name) value))))))) 706 707 708 ;;; Miscellaneous 709 710 (defsubst org-call-with-arg (command arg) 711 "Call COMMAND interactively, but pretend prefix arg was ARG." 712 (let ((current-prefix-arg arg)) (call-interactively command))) 713 714 (defsubst org-check-external-command (cmd &optional use no-error) 715 "Check if external program CMD for USE exists, error if not. 716 When the program does exist, return its path. 717 When it does not exist and NO-ERROR is set, return nil. 718 Otherwise, throw an error. The optional argument USE can describe what this 719 program is needed for, so that the error message can be more informative." 720 (or (executable-find cmd) 721 (if no-error 722 nil 723 (error "Can't find `%s'%s" cmd 724 (if use (format " (%s)" use) ""))))) 725 726 (defun org-display-warning (message) 727 "Display the given MESSAGE as a warning." 728 (display-warning 'org message :warning)) 729 730 (defun org-unlogged-message (&rest args) 731 "Display a message, but avoid logging it in the *Messages* buffer." 732 (let ((message-log-max nil)) 733 (apply #'message args))) 734 735 (defmacro org-dlet (binders &rest body) 736 "Like `let*' but using dynamic scoping." 737 (declare (indent 1) (debug let)) 738 (let ((vars (mapcar (lambda (binder) 739 (if (consp binder) (car binder) binder)) 740 binders))) 741 `(progn 742 (with-no-warnings 743 ,@(mapcar (lambda (var) `(defvar ,var)) vars)) 744 (let* ,binders ,@body)))) 745 746 (defmacro org-pushnew-to-end (val var) 747 "Like `cl-pushnew' but pushes to the end of the list. 748 Uses `equal' for comparisons. 749 750 Beware: this performs O(N) memory allocations, so if you use it in a loop, you 751 get an unnecessary O(N²) space complexity, so you're usually better off using 752 `cl-pushnew' (with a final `reverse' if you care about the order of elements)." 753 (declare (debug (form gv-place))) 754 (let ((v (make-symbol "v"))) 755 `(let ((,v ,val)) 756 (unless (member ,v ,var) 757 (setf ,var (append ,var (list ,v))))))) 758 759 (defun org-eval (form) 760 "Eval FORM and return result." 761 (condition-case-unless-debug error 762 (eval form t) 763 (error (format "%%![Error: %s]" error)))) 764 765 (defvar org--headline-re-cache-no-bol nil 766 "Plist holding association between headline level regexp.") 767 (defvar org--headline-re-cache-bol nil 768 "Plist holding association between headline level regexp.") 769 (defsubst org-headline-re (true-level &optional no-bol) 770 "Generate headline regexp for TRUE-LEVEL. 771 When NO-BOL is non-nil, regexp will not demand the regexp to start at 772 beginning of line." 773 (or (plist-get 774 (if no-bol 775 org--headline-re-cache-no-bol 776 org--headline-re-cache-bol) 777 true-level) 778 (let ((re (rx-to-string 779 (if no-bol 780 `(seq (** 1 ,true-level "*") " ") 781 `(seq line-start (** 1 ,true-level "*") " "))))) 782 (if no-bol 783 (setq org--headline-re-cache-no-bol 784 (plist-put 785 org--headline-re-cache-no-bol 786 true-level re)) 787 (setq org--headline-re-cache-bol 788 (plist-put 789 org--headline-re-cache-bol 790 true-level re))) 791 re))) 792 793 (defvar org-outline-regexp) ; defined in org.el 794 (defvar org-outline-regexp-bol) ; defined in org.el 795 (defvar org-odd-levels-only) ; defined in org.el 796 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el 797 (defun org-get-limited-outline-regexp (&optional with-bol) 798 "Return outline-regexp with limited number of levels. 799 The number of levels is controlled by `org-inlinetask-min-level'. 800 Match at beginning of line when WITH-BOL is non-nil." 801 (cond ((not (derived-mode-p 'org-mode)) 802 (if (string-prefix-p "^" outline-regexp) 803 (if with-bol outline-regexp (substring outline-regexp 1)) 804 (if with-bol (concat "^" outline-regexp) outline-regexp))) 805 ((not (featurep 'org-inlinetask)) 806 (if with-bol org-outline-regexp-bol org-outline-regexp)) 807 (t 808 (let* ((limit-level (1- org-inlinetask-min-level)) 809 (nstars (if org-odd-levels-only 810 (1- (* limit-level 2)) 811 limit-level))) 812 (org-headline-re nstars (not with-bol)))))) 813 814 (defun org--line-empty-p (n) 815 "Is the Nth next line empty?" 816 (and (not (bobp)) 817 (save-excursion 818 (forward-line n) 819 (skip-chars-forward " \t") 820 (eolp)))) 821 822 (defun org-previous-line-empty-p () 823 "Is the previous line a blank line? 824 When NEXT is non-nil, check the next line instead." 825 (org--line-empty-p -1)) 826 827 (defun org-next-line-empty-p () 828 "Is the previous line a blank line? 829 When NEXT is non-nil, check the next line instead." 830 (org--line-empty-p 1)) 831 832 (defun org-id-uuid () 833 "Return string with random (version 4) UUID." 834 (let ((rnd (md5 (format "%s%s%s%s%s%s%s" 835 (random) 836 (org-time-convert-to-list nil) 837 (user-uid) 838 (emacs-pid) 839 (user-full-name) 840 user-mail-address 841 (recent-keys))))) 842 (format "%s-%s-4%s-%s%s-%s" 843 (substring rnd 0 8) 844 (substring rnd 8 12) 845 (substring rnd 13 16) 846 (format "%x" 847 (logior 848 #b10000000 849 (logand 850 #b10111111 851 (string-to-number 852 (substring rnd 16 18) 16)))) 853 (substring rnd 18 20) 854 (substring rnd 20 32)))) 855 856 857 ;;; Motion 858 859 (defsubst org-goto-line (N) 860 (save-restriction 861 (widen) 862 (goto-char (point-min)) 863 (forward-line (1- N)))) 864 865 (defsubst org-current-line (&optional pos) 866 (save-excursion 867 (and pos (goto-char pos)) 868 ;; works also in narrowed buffer, because we start at 1, not point-min 869 (+ (if (bolp) 1 0) (count-lines 1 (point))))) 870 871 872 873 ;;; Overlays and text properties 874 875 (defun org-overlay-display (ovl text &optional face evap) 876 "Make overlay OVL display TEXT with face FACE." 877 (overlay-put ovl 'display text) 878 (when face (overlay-put ovl 'face face)) 879 (when evap (overlay-put ovl 'evaporate t))) 880 881 (defun org-overlay-before-string (ovl text &optional face evap) 882 "Make overlay OVL display TEXT with face FACE." 883 (when face (org-add-props text nil 'face face)) 884 (overlay-put ovl 'before-string text) 885 (when evap (overlay-put ovl 'evaporate t))) 886 887 (defun org-find-overlays (prop &optional pos delete) 888 "Find all overlays specifying PROP at POS or point. 889 If DELETE is non-nil, delete all those overlays." 890 (let (found) 891 (dolist (ov (overlays-at (or pos (point))) found) 892 (cond ((not (overlay-get ov prop))) 893 (delete (delete-overlay ov)) 894 (t (push ov found)))))) 895 896 (defun org-find-text-property-region (pos prop) 897 "Find a region around POS containing same non-nil value of PROP text property. 898 Return nil when PROP is not set at POS." 899 (let* ((beg (and (get-text-property pos prop) pos)) 900 (end beg)) 901 (when beg 902 (unless (or (equal beg (point-min)) 903 (not (eq (get-text-property beg prop) 904 (get-text-property (1- beg) prop)))) 905 (setq beg (previous-single-property-change pos prop nil (point-min)))) 906 (unless (or (equal end (point-max)) 907 ;; (not (eq (get-text-property end prop) 908 ;; (get-text-property (1+ end) prop))) 909 ) 910 (setq end (next-single-property-change pos prop nil (point-max)))) 911 (cons beg end)))) 912 913 914 ;;; Regexp matching 915 916 (defsubst org-pos-in-match-range (pos n) 917 (and (match-beginning n) 918 (<= (match-beginning n) pos) 919 (>= (match-end n) pos))) 920 921 (defsubst org-skip-whitespace () 922 "Skip over space, tabs and newline characters." 923 (skip-chars-forward " \t\n\r")) 924 925 (defun org-match-line (regexp) 926 "Match REGEXP at the beginning of the current line." 927 (save-excursion 928 (forward-line 0) 929 (looking-at regexp))) 930 931 (defun org-match-any-p (re list) 932 "Non-nil if regexp RE matches an element in LIST." 933 (cl-some (lambda (x) (string-match-p re x)) list)) 934 935 (defun org-in-regexp (regexp &optional nlines visually) 936 "Check if point is inside a match of REGEXP. 937 938 Normally only the current line is checked, but you can include 939 NLINES extra lines around point into the search. If VISUALLY is 940 set, require that the cursor is not after the match but really 941 on, so that the block visually is on the match. 942 943 Return nil or a cons cell (BEG . END) where BEG and END are, 944 respectively, the positions at the beginning and the end of the 945 match." 946 (catch :exit 947 (let ((pos (point)) 948 (eol (line-end-position (if nlines (1+ nlines) 1)))) 949 (save-excursion 950 (forward-line (- (or nlines 0))) 951 (while (and (re-search-forward regexp eol t) 952 (<= (match-beginning 0) pos)) 953 (let ((end (match-end 0))) 954 (when (or (> end pos) (and (= end pos) (not visually))) 955 (throw :exit (cons (match-beginning 0) (match-end 0)))))))))) 956 957 (defun org-point-in-group (point group &optional context) 958 "Check if POINT is in match-group GROUP. 959 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the 960 match. If the match group does not exist or point is not inside it, 961 return nil." 962 (and (match-beginning group) 963 (>= point (match-beginning group)) 964 (<= point (match-end group)) 965 (if context 966 (list context (match-beginning group) (match-end group)) 967 t))) 968 969 (defun org-url-p (s) 970 "Non-nil if string S is a URL." 971 (require 'ffap) 972 (and ffap-url-regexp (string-match-p ffap-url-regexp s))) 973 974 (defconst org-uuid-regexp 975 "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" 976 "Regular expression matching a universal unique identifier (UUID).") 977 978 (defun org-uuidgen-p (s) 979 "Is S an ID created by UUIDGEN?" 980 (string-match org-uuid-regexp (downcase s))) 981 982 983 984 ;;; String manipulation 985 986 (defcustom org-sort-function #'string-collate-lessp 987 "Function used to compare strings when sorting. 988 This function affects how Org mode sorts headlines, agenda items, 989 table lines, etc. 990 991 The function must accept either 2 or 4 arguments: strings to compare 992 and, optionally, LOCALE and IGNORE-CASE - locale name and flag to make 993 comparison case-insensitive. 994 995 The default value uses sorting rules according to OS language. Users 996 who want to make sorting language-independent, may customize the value 997 to `org-sort-function-fallback'. 998 999 Note that some string sorting rules are known to be not accurate on 1000 MacOS. See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=59275. 1001 MacOS users may customize the value to 1002 `org-sort-function-fallback'." 1003 :group 'org 1004 :package-version '(Org . "9.7") 1005 :type '(choice 1006 (const :tag "According to OS language" string-collate-lessp) 1007 (const :tag "Using string comparison" org-sort-function-fallback) 1008 (function :tag "Custom function"))) 1009 1010 (defun org-sort-function-fallback (a b &optional _ ignore-case) 1011 "Return non-nil when downcased string A < string B. 1012 Use `compare-strings' for comparison. Honor IGNORE-CASE." 1013 (let ((ans (compare-strings a nil nil b nil nil ignore-case))) 1014 (cond 1015 ((and (numberp ans) (< ans 0)) t) 1016 (t nil)))) 1017 1018 (defun org-string< (a b &optional locale ignore-case) 1019 "Return non-nil when string A < string B. 1020 LOCALE is the locale name. IGNORE-CASE, when non-nil, makes comparison 1021 ignore case." 1022 (if (= 4 (cdr (func-arity org-sort-function))) 1023 (funcall org-sort-function a b locale ignore-case) 1024 (funcall org-sort-function a b))) 1025 1026 (defun org-string<= (a b &optional locale ignore-case) 1027 "Return non-nil when string A <= string B. 1028 LOCALE is the locale name. IGNORE-CASE, when non-nil, makes comparison 1029 ignore case." 1030 (or (string= a b) (org-string< a b locale ignore-case))) 1031 1032 (defun org-string>= (a b &optional locale ignore-case) 1033 "Return non-nil when string A >= string B. 1034 LOCALE is the locale name. IGNORE-CASE, when non-nil, makes comparison 1035 ignore case." 1036 (not (org-string< a b locale ignore-case))) 1037 1038 (defun org-string> (a b &optional locale ignore-case) 1039 "Return non-nil when string A > string B. 1040 LOCALE is the locale name. IGNORE-CASE, when non-nil, makes comparison 1041 ignore case." 1042 (and (not (string= a b)) 1043 (not (org-string< a b locale ignore-case)))) 1044 1045 (defun org-string<> (a b) 1046 "Return non-nil when string A and string B are not equal." 1047 (not (string= a b))) 1048 1049 (defsubst org-trim (s &optional keep-lead) 1050 "Remove whitespace at the beginning and the end of string S. 1051 When optional argument KEEP-LEAD is non-nil, removing blank lines 1052 at the beginning of the string does not affect leading indentation." 1053 (replace-regexp-in-string 1054 (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") "" 1055 (replace-regexp-in-string "[ \t\n\r]+\\'" "" s))) 1056 1057 (defun org-string-nw-p (s) 1058 "Return S if S is a string containing a non-blank character. 1059 Otherwise, return nil." 1060 (and (stringp s) 1061 (string-match-p "[^ \r\t\n]" s) 1062 s)) 1063 1064 (defun org-reverse-string (string) 1065 "Return the reverse of STRING." 1066 (apply #'string (nreverse (string-to-list string)))) 1067 1068 (defun org-split-string (string &optional separators) 1069 "Splits STRING into substrings at SEPARATORS. 1070 1071 SEPARATORS is a regular expression. When nil, it defaults to 1072 \"[ \\f\\t\\n\\r\\v]+\". 1073 1074 Unlike `split-string', matching SEPARATORS at the beginning and 1075 end of string are ignored." 1076 (let ((separators (or separators "[ \f\t\n\r\v]+"))) 1077 (if (not (string-match separators string)) (list string) 1078 (let ((i (match-end 0)) 1079 (results 1080 (and (/= 0 (match-beginning 0)) ;skip leading separator 1081 (list (substring string 0 (match-beginning 0)))))) 1082 (while (string-match separators string i) 1083 (push (substring string i (match-beginning 0)) 1084 results) 1085 (setq i (match-end 0))) 1086 (nreverse (if (= i (length string)) 1087 results ;skip trailing separator 1088 (cons (substring string i) results))))))) 1089 1090 (defun org--string-from-props (s property beg end) 1091 "Return the visible part of string S. 1092 Visible part is determined according to text PROPERTY, which is 1093 either `invisible' or `display'. BEG and END are 0-indices 1094 delimiting S." 1095 (let ((width 0) 1096 (cursor beg)) 1097 (while (setq beg (text-property-not-all beg end property nil s)) 1098 (let* ((next (next-single-property-change beg property s end)) 1099 (spec (get-text-property beg property s)) 1100 (value 1101 (pcase property 1102 (`invisible 1103 ;; If `invisible' property means text is to be 1104 ;; invisible, return 0. Otherwise return nil so as 1105 ;; to resume search. 1106 (and (or (eq t buffer-invisibility-spec) 1107 (assoc-string spec buffer-invisibility-spec)) 1108 0)) 1109 (`display 1110 (pcase spec 1111 (`nil nil) 1112 (`(space . ,props) 1113 (let ((width (plist-get props :width))) 1114 (and (wholenump width) width))) 1115 (`(image . ,_) 1116 (and (fboundp 'image-size) 1117 (ceiling (car (image-size spec))))) 1118 ((pred stringp) 1119 ;; Displayed string could contain invisible parts, 1120 ;; but no nested display. 1121 (org--string-from-props spec 'invisible 0 (length spec))) 1122 (_ 1123 ;; Un-handled `display' value. Ignore it. 1124 ;; Consider the original string instead. 1125 nil))) 1126 (_ (error "Unknown property: %S" property))))) 1127 (when value 1128 (cl-incf width 1129 ;; When looking for `display' parts, we still need 1130 ;; to look for `invisible' property elsewhere. 1131 (+ (cond ((eq property 'display) 1132 (org--string-from-props s 'invisible cursor beg)) 1133 ((= cursor beg) 0) 1134 (t (string-width (substring s cursor beg)))) 1135 value)) 1136 (setq cursor next)) 1137 (setq beg next))) 1138 (+ width 1139 ;; Look for `invisible' property in the last part of the 1140 ;; string. See above. 1141 (cond ((eq property 'display) 1142 (org--string-from-props s 'invisible cursor end)) 1143 ((= cursor end) 0) 1144 (t (string-width (substring s cursor end))))))) 1145 1146 (defun org--string-width-1 (string) 1147 "Return width of STRING when displayed in the current buffer. 1148 Unlike `string-width', this function takes into consideration 1149 `invisible' and `display' text properties. It supports the 1150 latter in a limited way, mostly for combinations used in Org. 1151 Results may be off sometimes if it cannot handle a given 1152 `display' value." 1153 (org--string-from-props string 'display 0 (length string))) 1154 1155 (defun org-string-width (string &optional pixels default-face) 1156 "Return width of STRING when displayed in the current buffer. 1157 Return width in pixels when PIXELS is non-nil. 1158 When PIXELS is nil, DEFAULT-FACE is the face used to calculate relative 1159 STRING width. When REFERENCE-FACE is nil, `default' face is used." 1160 (if (and (version< emacs-version "28") (not pixels)) 1161 ;; FIXME: Fallback to old limited version, because 1162 ;; `window-pixel-width' is buggy in older Emacs. 1163 (org--string-width-1 string) 1164 ;; Wrap/line prefix will make `window-text-pixel-size' return too 1165 ;; large value including the prefix. 1166 (remove-text-properties 0 (length string) 1167 '(wrap-prefix t line-prefix t) 1168 string) 1169 ;; Face should be removed to make sure that all the string symbols 1170 ;; are using default face with constant width. Constant char width 1171 ;; is critical to get right string width from pixel width (not needed 1172 ;; when PIXELS are requested though). 1173 (unless pixels 1174 (put-text-property 0 (length string) 'face (or default-face 'default) string)) 1175 (let (;; We need to remove the folds to make sure that folded table 1176 ;; alignment is not messed up. 1177 (current-invisibility-spec 1178 (or (and (not (listp buffer-invisibility-spec)) 1179 buffer-invisibility-spec) 1180 (let (result) 1181 (dolist (el buffer-invisibility-spec) 1182 (unless (or (memq el 1183 '(org-fold-drawer 1184 org-fold-block 1185 org-fold-outline)) 1186 (and (listp el) 1187 (memq (car el) 1188 '(org-fold-drawer 1189 org-fold-block 1190 org-fold-outline)))) 1191 (push el result))) 1192 result))) 1193 (current-char-property-alias-alist char-property-alias-alist)) 1194 (with-current-buffer (get-buffer-create " *Org string width*") 1195 (setq-local display-line-numbers nil) 1196 (setq-local line-prefix nil) 1197 (setq-local wrap-prefix nil) 1198 (setq-local buffer-invisibility-spec 1199 (if (listp current-invisibility-spec) 1200 (mapcar (lambda (el) 1201 ;; Consider ellipsis to have 0 width. 1202 ;; It is what Emacs 28+ does, but we have 1203 ;; to force it in earlier Emacs versions. 1204 (if (and (consp el) (cdr el)) 1205 (list (car el)) 1206 el)) 1207 current-invisibility-spec) 1208 current-invisibility-spec)) 1209 (setq-local char-property-alias-alist 1210 current-char-property-alias-alist) 1211 (let (pixel-width symbol-width) 1212 (with-silent-modifications 1213 (erase-buffer) 1214 (insert string) 1215 (setq pixel-width (org-buffer-text-pixel-width)) 1216 (unless pixels 1217 (erase-buffer) 1218 (insert (propertize "a" 'face (or default-face 'default))) 1219 (setq symbol-width (org-buffer-text-pixel-width)))) 1220 (if pixels 1221 pixel-width 1222 (ceiling pixel-width symbol-width))))))) 1223 1224 (defmacro org-current-text-column () 1225 "Like `current-column' but ignore display properties. 1226 Throw an error when `tab-width' is not 8. 1227 1228 This function forces `tab-width' value because it is used as a part of 1229 the parser, to ensure parser consistency when calculating list 1230 indentation." 1231 `(progn 1232 (unless (= 8 tab-width) (error "Tab width in Org files must be 8, not %d. Please adjust your `tab-width' settings for Org mode" tab-width)) 1233 (string-width (buffer-substring-no-properties 1234 (line-beginning-position) (point))))) 1235 1236 (defun org-not-nil (v) 1237 "If V not nil, and also not the string \"nil\", then return V. 1238 Otherwise return nil." 1239 (and v (not (equal v "nil")) v)) 1240 1241 (defun org-unbracket-string (pre post string) 1242 "Remove PRE/POST from the beginning/end of STRING. 1243 Both PRE and POST must be pre-/suffixes of STRING, or neither is 1244 removed. Return the new string. If STRING is nil, return nil." 1245 (declare (indent 2)) 1246 (and string 1247 (if (and (string-prefix-p pre string) 1248 (string-suffix-p post string)) 1249 (substring string (length pre) 1250 (and (not (string-equal "" post)) (- (length post)))) 1251 string))) 1252 1253 (defun org-strip-quotes (string) 1254 "Strip double quotes from around STRING, if applicable. 1255 If STRING is nil, return nil." 1256 (org-unbracket-string "\"" "\"" string)) 1257 1258 (defsubst org-current-line-string (&optional to-here) 1259 "Return current line, as a string. 1260 If optional argument TO-HERE is non-nil, return string from 1261 beginning of line up to point." 1262 (buffer-substring (line-beginning-position) 1263 (if to-here (point) (line-end-position)))) 1264 1265 (defun org-shorten-string (s maxlength) 1266 "Shorten string S so that it is no longer than MAXLENGTH characters. 1267 If the string is shorter or has length MAXLENGTH, just return the 1268 original string. If it is longer, the functions finds a space in the 1269 string, breaks this string off at that locations and adds three dots 1270 as ellipsis. Including the ellipsis, the string will not be longer 1271 than MAXLENGTH. If finding a good breaking point in the string does 1272 not work, the string is just chopped off in the middle of a word 1273 if necessary." 1274 (if (<= (length s) maxlength) 1275 s 1276 (let* ((n (max (- maxlength 4) 1)) 1277 (re (concat "\\`\\(.\\{1," (number-to-string n) 1278 "\\}[^ ]\\)\\([ ]\\|\\'\\)"))) 1279 (if (string-match re s) 1280 (concat (match-string 1 s) "...") 1281 (concat (substring s 0 (max (- maxlength 3) 0)) "..."))))) 1282 1283 (defun org-remove-tabs (s &optional width) 1284 "Replace tabulators in S with spaces. 1285 Assumes that s is a single line, starting in column 0." 1286 (setq width (or width tab-width)) 1287 (while (string-match "\t" s) 1288 (setq s (replace-match 1289 (make-string 1290 (- (* width (/ (+ (match-beginning 0) width) width)) 1291 (match-beginning 0)) ?\ ) 1292 t t s))) 1293 s) 1294 1295 (defun org-remove-blank-lines (s) 1296 "Remove blank lines in S." 1297 (replace-regexp-in-string (rx "\n" (1+ (0+ space) "\n")) "\n" s)) 1298 1299 (defun org-wrap (string &optional width lines) 1300 "Wrap string to either a number of lines, or a width in characters. 1301 If WIDTH is non-nil, the string is wrapped to that width, however many lines 1302 that costs. If there is a word longer than WIDTH, the text is actually 1303 wrapped to the length of that word. 1304 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that 1305 many lines, whatever width that takes. 1306 The return value is a list of lines, without newlines at the end." 1307 (let* ((words (split-string string)) 1308 (maxword (apply #'max (mapcar #'org-string-width words))) 1309 w ll) 1310 (cond (width 1311 (org--do-wrap words (max maxword width))) 1312 (lines 1313 (setq w maxword) 1314 (setq ll (org--do-wrap words maxword)) 1315 (if (<= (length ll) lines) 1316 ll 1317 (setq ll words) 1318 (while (> (length ll) lines) 1319 (setq w (1+ w)) 1320 (setq ll (org--do-wrap words w))) 1321 ll)) 1322 (t (error "Cannot wrap this"))))) 1323 1324 (defun org--do-wrap (words width) 1325 "Create lines of maximum width WIDTH (in characters) from word list WORDS." 1326 (let (lines line) 1327 (while words 1328 (setq line (pop words)) 1329 (while (and words (< (+ (length line) (length (car words))) width)) 1330 (setq line (concat line " " (pop words)))) 1331 (setq lines (push line lines))) 1332 (nreverse lines))) 1333 1334 (defun org-remove-indentation (code &optional n) 1335 "Remove maximum common indentation in string CODE and return it. 1336 N may optionally be the number of columns to remove. Return CODE 1337 as-is if removal failed." 1338 (with-temp-buffer 1339 (insert code) 1340 (if (org-do-remove-indentation n) (buffer-string) code))) 1341 1342 (defun org-fill-template (template alist) 1343 "Find each %key of ALIST in TEMPLATE and replace it." 1344 (let ((case-fold-search nil)) 1345 (dolist (entry (sort (copy-sequence alist) 1346 ; Sort from longest key to shortest, so that 1347 ; "noweb-ref" and "tangle-mode" get processed 1348 ; before "noweb" and "tangle", respectively. 1349 (lambda (a b) (< (length (car b)) (length (car a)))))) 1350 (setq template 1351 (replace-regexp-in-string 1352 (concat "%" (regexp-quote (car entry))) 1353 (or (cdr entry) "") template t t))) 1354 template)) 1355 1356 (defun org-replace-escapes (string table) 1357 "Replace %-escapes in STRING with values in TABLE. 1358 TABLE is an association list with keys like \"%a\" and string values. 1359 The sequences in STRING may contain normal field width and padding information, 1360 for example \"%-5s\". Replacements happen in the sequence given by TABLE, 1361 so values can contain further %-escapes if they are define later in TABLE." 1362 (let ((tbl (copy-alist table)) 1363 (case-fold-search nil) 1364 (pchg 0) 1365 re rpl) 1366 (dolist (e tbl) 1367 (setq re (concat "%-?[0-9.]*" (substring (car e) 1))) 1368 (when (and (cdr e) (string-match re (cdr e))) 1369 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0))) 1370 (safe (copy-sequence "SREF"))) 1371 (add-text-properties 0 3 (list 'sref sref) safe) 1372 (setcdr e (replace-match safe t t (cdr e))))) 1373 (while (string-match re string) 1374 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s") 1375 (cdr e))) 1376 (setq string (replace-match rpl t t string)))) 1377 (while (setq pchg (next-property-change pchg string)) 1378 (let ((sref (get-text-property pchg 'sref string))) 1379 (when (and sref (string-match "SREF" string pchg)) 1380 (setq string (replace-match sref t t string))))) 1381 string)) 1382 1383 1384 ;;; Text properties 1385 1386 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t 1387 rear-nonsticky t mouse-map t fontified t 1388 org-emphasis t) 1389 "Properties to remove when a string without properties is wanted.") 1390 1391 (defun org-buffer-substring-fontified (beg end) 1392 "Return fontified region between BEG and END." 1393 (when (bound-and-true-p jit-lock-mode) 1394 (when (text-property-not-all beg end 'fontified t) 1395 (save-excursion (save-match-data (font-lock-fontify-region beg end))))) 1396 (buffer-substring beg end)) 1397 1398 (defun org-looking-at-fontified (re) 1399 "Call `looking-at' RE and make sure that the match is fontified." 1400 (prog1 (looking-at re) 1401 (when (bound-and-true-p jit-lock-mode) 1402 (when (text-property-not-all 1403 (match-beginning 0) (match-end 0) 1404 'fontified t) 1405 (save-excursion 1406 (save-match-data 1407 (font-lock-fontify-region (match-beginning 0) 1408 (match-end 0)))))))) 1409 1410 (defsubst org-no-properties (s &optional restricted) 1411 "Remove all text properties from string S. 1412 When RESTRICTED is non-nil, only remove the properties listed 1413 in `org-rm-props'." 1414 (if restricted (remove-text-properties 0 (length s) org-rm-props s) 1415 (set-text-properties 0 (length s) nil s)) 1416 s) 1417 (defun org-add-props (string plist &rest props) 1418 "Add text properties to entire string, from beginning to end. 1419 PLIST may be a list of properties, PROPS are individual properties and values 1420 that will be added to PLIST. Returns the string that was modified." 1421 (declare (indent 2)) 1422 (add-text-properties 1423 0 (length string) (if props (append plist props) plist) string) 1424 string) 1425 1426 (defun org-make-parameter-alist (plist) 1427 "Return alist based on PLIST. 1428 PLIST is a property list with alternating symbol names and values. 1429 The returned alist is a list of lists with the symbol name in `car' 1430 and the value in `cadr'." 1431 (when plist 1432 (cons (list (car plist) (cadr plist)) 1433 (org-make-parameter-alist (cddr plist))))) 1434 1435 (defsubst org-get-at-bol (property) 1436 "Get text property PROPERTY at the beginning of line." 1437 (get-text-property (line-beginning-position) property)) 1438 1439 (defun org-get-at-eol (property n) 1440 "Get text property PROPERTY at the end of line less N characters." 1441 (get-text-property (- (line-end-position) n) property)) 1442 1443 (defun org-find-text-property-in-string (prop s) 1444 "Return the first non-nil value of property PROP in string S." 1445 (or (get-text-property 0 prop s) 1446 (get-text-property (or (next-single-property-change 0 prop s) 0) 1447 prop s))) 1448 1449 ;; FIXME: move to org-fold? 1450 (defun org-invisible-p (&optional pos folding-only) 1451 "Non-nil if the character after POS is invisible. 1452 If POS is nil, use `point' instead. When optional argument 1453 FOLDING-ONLY is non-nil, only consider invisible parts due to 1454 folding of a headline, a block or a drawer, i.e., not because of 1455 fontification." 1456 (let ((value (invisible-p (or pos (point))))) 1457 (cond ((not value) nil) 1458 (folding-only (org-fold-folded-p (or pos (point)))) 1459 (t value)))) 1460 1461 (defun org-truly-invisible-p () 1462 "Check if point is at a character currently not visible. 1463 This version does not only check the character property, but also 1464 `visible-mode'." 1465 (unless (bound-and-true-p visible-mode) 1466 (org-invisible-p))) 1467 1468 (defun org-invisible-p2 () 1469 "Check if point is at a character currently not visible. 1470 If the point is at EOL (and not at the beginning of a buffer too), 1471 move it back by one char before doing this check." 1472 (save-excursion 1473 (when (and (eolp) (not (bobp))) 1474 (backward-char 1)) 1475 (org-invisible-p))) 1476 1477 (defun org-region-invisible-p (beg end) 1478 "Check if region if completely hidden." 1479 (org-with-wide-buffer 1480 (and (org-invisible-p beg) 1481 (org-invisible-p (org-fold-next-visibility-change beg end))))) 1482 1483 (defun org-find-visible () 1484 "Return closest visible buffer position, or `point-max'." 1485 (if (org-invisible-p) 1486 (org-fold-next-visibility-change (point)) 1487 (point))) 1488 1489 (defun org-find-invisible () 1490 "Return closest invisible buffer position, or `point-max'." 1491 (if (org-invisible-p) 1492 (point) 1493 (org-fold-next-visibility-change (point)))) 1494 1495 1496 ;;; Time 1497 1498 (defun org-2ft (s) 1499 "Convert S to a floating point time. 1500 If S is already a number, just return it. If it is a string, 1501 parse it as a time string and apply `float-time' to it. If S is 1502 nil, just return 0." 1503 (cond 1504 ((numberp s) s) 1505 ((stringp s) 1506 (condition-case nil 1507 (org-time-string-to-seconds s) 1508 (error 0))) 1509 (t 0))) 1510 1511 (defun org-time= (a b) 1512 (let ((a (org-2ft a)) 1513 (b (org-2ft b))) 1514 (and (> a 0) (> b 0) (= a b)))) 1515 1516 (defun org-time< (a b) 1517 (let ((a (org-2ft a)) 1518 (b (org-2ft b))) 1519 (and (> a 0) (> b 0) (< a b)))) 1520 1521 (defun org-time<= (a b) 1522 (let ((a (org-2ft a)) 1523 (b (org-2ft b))) 1524 (and (> a 0) (> b 0) (<= a b)))) 1525 1526 (defun org-time> (a b) 1527 (let ((a (org-2ft a)) 1528 (b (org-2ft b))) 1529 (and (> a 0) (> b 0) (> a b)))) 1530 1531 (defun org-time>= (a b) 1532 (let ((a (org-2ft a)) 1533 (b (org-2ft b))) 1534 (and (> a 0) (> b 0) (>= a b)))) 1535 1536 (defun org-time<> (a b) 1537 (let ((a (org-2ft a)) 1538 (b (org-2ft b))) 1539 (and (> a 0) (> b 0) (\= a b)))) 1540 1541 (defmacro org-encode-time (&rest time) 1542 "Compatibility and convenience helper for `encode-time'. 1543 TIME may be a 9 components list (SECONDS ... YEAR IGNORED DST ZONE) 1544 as the recommended way since Emacs-27 or 6 or 9 separate arguments 1545 similar to the only possible variant for Emacs-26 and earlier. 1546 6 elements list as the only argument causes wrong type argument till 1547 Emacs-29. 1548 1549 Warning: use -1 for DST to guess the actual value, nil means no 1550 daylight saving time and may be wrong at particular time. 1551 1552 DST value is ignored prior to Emacs-27. Since Emacs-27 DST value matters 1553 even when multiple arguments is passed to this macro and such 1554 behavior is different from `encode-time'. See 1555 Info node `(elisp)Time Conversion' for details and caveats, 1556 preferably the latest version." 1557 (if (version< emacs-version "27.1") 1558 (if (cdr time) 1559 `(encode-time ,@time) 1560 `(apply #'encode-time ,@time)) 1561 (if (ignore-errors (with-no-warnings (encode-time '(0 0 0 1 1 1971)))) 1562 (pcase (length time) ; Emacs-29 since d75e2c12eb 1563 (1 `(encode-time ,@time)) 1564 ((or 6 9) `(encode-time (list ,@time))) 1565 (_ (error "`org-encode-time' may be called with 1, 6, or 9 arguments but %d given" 1566 (length time)))) 1567 (pcase (length time) 1568 (1 `(encode-time ,@time)) 1569 (6 `(encode-time (list ,@time nil -1 nil))) 1570 (9 `(encode-time (list ,@time))) 1571 (_ (error "`org-encode-time' may be called with 1, 6, or 9 arguments but %d given" 1572 (length time))))))) 1573 1574 (defun org-parse-time-string (s &optional nodefault) 1575 "Parse Org time string S. 1576 1577 If time is not given, defaults to 0:00. However, with optional 1578 NODEFAULT, hour and minute fields are nil if not given. 1579 1580 Throw an error if S does not contain a valid Org time string. 1581 Note that the first match for YYYY-MM-DD will be used (e.g., 1582 \"-52000-02-03\" will be taken as \"2000-02-03\"). 1583 1584 This should be a lot faster than the `parse-time-string'." 1585 (unless (string-match org-ts-regexp0 s) 1586 (error "Not an Org time string: %s" s)) 1587 (list 0 1588 (cond ((match-beginning 8) (string-to-number (match-string 8 s))) 1589 (nodefault nil) 1590 (t 0)) 1591 (cond ((match-beginning 7) (string-to-number (match-string 7 s))) 1592 (nodefault nil) 1593 (t 0)) 1594 (string-to-number (match-string 4 s)) 1595 (string-to-number (match-string 3 s)) 1596 (string-to-number (match-string 2 s)) 1597 nil -1 nil)) 1598 1599 (defun org-matcher-time (s) 1600 "Interpret a time comparison value S as a floating point time. 1601 1602 S can be an Org time stamp, a modifier, e.g., \"<+2d>\", or the 1603 following special strings: \"<now>\", \"<today>\", 1604 \"<tomorrow>\", and \"<yesterday>\". 1605 1606 Return 0. if S is not recognized as a valid value." 1607 (let ((today (float-time (org-encode-time 1608 (append '(0 0 0) (nthcdr 3 (decode-time))))))) 1609 (save-match-data 1610 (cond 1611 ((string= s "<now>") (float-time)) 1612 ((string= s "<today>") today) 1613 ((string= s "<tomorrow>") (+ 86400.0 today)) 1614 ((string= s "<yesterday>") (- today 86400.0)) 1615 ((string-match "\\`<\\([-+][0-9]+\\)\\([hdwmy]\\)>\\'" s) 1616 (+ (if (string= (match-string 2 s) "h") (float-time) today) 1617 (* (string-to-number (match-string 1 s)) 1618 (cdr (assoc (match-string 2 s) 1619 '(("h" . 3600.0) 1620 ("d" . 86400.0) ("w" . 604800.0) 1621 ("m" . 2678400.0) ("y" . 31557600.0))))))) 1622 ((string-match org-ts-regexp0 s) (org-2ft s)) 1623 (t 0.))))) 1624 1625 1626 ;;; Misc 1627 1628 (defun org-scroll (key &optional additional-keys) 1629 "Receive KEY and scroll the current window accordingly. 1630 When ADDITIONAL-KEYS is not nil, also include SPC and DEL in the 1631 allowed keys for scrolling, as expected in the export dispatch 1632 window." 1633 (let ((scrlup (if additional-keys '(?\s ?\C-v) ?\C-v)) 1634 (scrldn (if additional-keys `(?\d ?\M-v) ?\M-v))) 1635 (pcase key 1636 (?\C-n (if (not (pos-visible-in-window-p (point-max))) 1637 (ignore-errors (scroll-up 1)) 1638 (message "End of buffer") 1639 (sit-for 1))) 1640 (?\C-p (if (not (pos-visible-in-window-p (point-min))) 1641 (ignore-errors (scroll-down 1)) 1642 (message "Beginning of buffer") 1643 (sit-for 1))) 1644 ;; SPC or 1645 ((guard (memq key scrlup)) 1646 (if (not (pos-visible-in-window-p (point-max))) 1647 (scroll-up nil) 1648 (message "End of buffer") 1649 (sit-for 1))) 1650 ;; DEL 1651 ((guard (memq key scrldn)) 1652 (if (not (pos-visible-in-window-p (point-min))) 1653 (scroll-down nil) 1654 (message "Beginning of buffer") 1655 (sit-for 1)))))) 1656 1657 (cl-defun org-knuth-hash (number &optional (base 32)) 1658 "Calculate Knuth's multiplicative hash for NUMBER. 1659 BASE is the maximum bitcount. 1660 Credit: https://stackoverflow.com/questions/11871245/knuth-multiplicative-hash#41537995" 1661 (cl-assert (and (<= 0 base 32))) 1662 (ash (* number 2654435769) (- base 32))) 1663 1664 (defvar org-sxhash-hashes (make-hash-table :weakness 'key :test 'equal)) 1665 (defvar org-sxhash-objects (make-hash-table :weakness 'value)) 1666 (defun org-sxhash-safe (obj &optional counter) 1667 "Like `sxhash' for OBJ, but collision-free for in-memory objects. 1668 When COUNTER is non-nil, return safe hash for (COUNTER . OBJ)." 1669 ;; Note: third-party code may modify OBJ by side effect. 1670 ;; Should not affect anything as long as `org-sxhash-safe' 1671 ;; is used to calculate hash. 1672 (or (and (not counter) (gethash obj org-sxhash-hashes)) 1673 (let* ((hash (sxhash (if counter (cons counter obj) obj))) 1674 (obj-old (gethash hash org-sxhash-objects))) 1675 (if obj-old ; collision 1676 (org-sxhash-safe obj (if counter (1+ counter) 1)) 1677 ;; No collision. Remember and return normal hash. 1678 (puthash hash obj org-sxhash-objects) 1679 (puthash obj hash org-sxhash-hashes))))) 1680 1681 (defun org-compile-file (source process ext &optional err-msg log-buf spec) 1682 "Compile a SOURCE file using PROCESS. 1683 1684 See `org-compile-file-commands' for information on PROCESS, EXT, and SPEC. 1685 If PROCESS fails, an error will be raised. The error message can 1686 then be refined by providing string ERR-MSG, which is appended to 1687 the standard message. 1688 1689 PROCESS must create a file with the same base name and directory 1690 as SOURCE, but ending with EXT. The function then returns its 1691 filename. Otherwise, it raises an error. 1692 1693 When PROCESS is a list of commands, optional argument LOG-BUF can 1694 be set to a buffer or a buffer name. `shell-command' then uses 1695 it for output." 1696 (let* ((commands (org-compile-file-commands source process ext spec err-msg)) 1697 (output (concat (file-name-sans-extension source) "." ext)) 1698 ;; Resolve symlinks in default-directory to correctly handle 1699 ;; absolute source paths or relative paths with .. 1700 (relname (if (file-name-absolute-p source) 1701 (let ((pwd (file-truename default-directory))) 1702 (file-relative-name source pwd)) 1703 source)) 1704 (log-buf (and log-buf (get-buffer-create log-buf))) 1705 (time (file-attribute-modification-time (file-attributes output)))) 1706 (save-window-excursion 1707 (dolist (command commands) 1708 (cond 1709 ((functionp command) 1710 (funcall command (shell-quote-argument relname))) 1711 ((stringp command) 1712 (let ((shell-command-dont-erase-buffer t)) 1713 (shell-command command log-buf)))))) 1714 ;; Check for process failure. Output file is expected to be 1715 ;; located in the same directory as SOURCE. 1716 (unless (org-file-newer-than-p output time) 1717 (ignore (defvar org-batch-test)) 1718 ;; Display logs when running tests. 1719 (when (bound-and-true-p org-batch-test) 1720 (message "org-compile-file log ::\n-----\n%s\n-----\n" 1721 (with-current-buffer log-buf (buffer-string)))) 1722 (error 1723 (format 1724 "File %S wasn't produced%s" 1725 output 1726 (if (org-string-nw-p err-msg) 1727 (concat " " (org-trim err-msg)) 1728 err-msg)))) 1729 output)) 1730 1731 (defun org-compile-file-commands (source process ext &optional spec err-msg) 1732 "Return list of commands used to compile SOURCE file. 1733 1734 The commands are formed from PROCESS, which is either a function or 1735 a list of shell commands, as strings. EXT is a file extension, without 1736 the leading dot, as a string. After PROCESS has been executed, 1737 a file with the same basename and directory as SOURCE but with the 1738 file extension EXT is expected to be produced. 1739 Failure to produce this file will be interpreted as PROCESS failing. 1740 1741 If PROCESS is a function, it is called with a single argument: 1742 the SOURCE file. 1743 1744 If PROCESS is a list of commands, each of them is called using 1745 `shell-command'. By default, in each command, %b, %f, %F, %o and 1746 %O are replaced with, respectively, SOURCE base name, relative 1747 file name, absolute file name, relative directory and absolute 1748 output file name. It is possible, however, to use more 1749 place-holders by specifying them in optional argument SPEC, as an 1750 alist following the pattern 1751 1752 (CHARACTER . REPLACEMENT-STRING). 1753 1754 Throw an error if PROCESS does not satisfy the described patterns. 1755 The error string will be appended with ERR-MSG, when it is a string." 1756 (let* ((basename (file-name-base source)) 1757 ;; Resolve symlinks in default-directory to correctly handle 1758 ;; absolute source paths or relative paths with .. 1759 (pwd (file-truename default-directory)) 1760 (absname (expand-file-name source pwd)) 1761 (relname (if (file-name-absolute-p source) 1762 (file-relative-name source pwd) 1763 source)) 1764 (relpath (or (file-name-directory relname) "./")) 1765 (output (concat (file-name-sans-extension absname) "." ext)) 1766 (err-msg (if (stringp err-msg) (concat ". " err-msg) ""))) 1767 (pcase process 1768 ((pred functionp) (list process)) 1769 ((pred consp) 1770 (let ((spec (append spec 1771 `((?b . ,(shell-quote-argument basename)) 1772 (?f . ,(shell-quote-argument relname)) 1773 (?F . ,(shell-quote-argument absname)) 1774 (?o . ,(shell-quote-argument relpath)) 1775 (?O . ,(shell-quote-argument output)))))) 1776 (mapcar (lambda (command) (format-spec command spec)) process))) 1777 (_ (error "No valid command to process %S%s" source err-msg))))) 1778 1779 (defun org-display-buffer-split (buffer alist) 1780 "Display BUFFER in the current frame split in two parts. 1781 The frame will display two buffers - current buffer and BUFFER. 1782 ALIST is an association list of action symbols and values. See 1783 Info node `(elisp) Buffer Display Action Alists' for details of 1784 such alists. 1785 1786 Use `display-buffer-in-direction' internally. 1787 1788 This is an action function for buffer display, see Info 1789 node `(elisp) Buffer Display Action Functions'. It should be 1790 called only by `display-buffer' or a function directly or 1791 indirectly called by the latter." 1792 (let ((window-configuration (current-window-configuration))) 1793 (ignore-errors (delete-other-windows)) 1794 (or (display-buffer-in-direction buffer alist) 1795 (display-buffer-pop-up-window buffer alist) 1796 (prog1 nil 1797 (set-window-configuration window-configuration))))) 1798 1799 (defun org-display-buffer-in-window (buffer alist) 1800 "Display BUFFER in specific window. 1801 The window is defined according to the `window' slot in the ALIST. 1802 Then `same-frame' slot in the ALIST is set, only display buffer when 1803 window is present in the current frame. 1804 1805 This is an action function for buffer display, see Info 1806 node `(elisp) Buffer Display Action Functions'. It should be 1807 called only by `display-buffer' or a function directly or 1808 indirectly called by the latter." 1809 (let ((window (alist-get 'window alist))) 1810 (when (and window 1811 (window-live-p window) 1812 (or (not (alist-get 'same-frame alist)) 1813 (eq (window-frame) (window-frame window)))) 1814 (window--display-buffer buffer window 'reuse alist)))) 1815 1816 (provide 'org-macs) 1817 1818 ;; Local variables: 1819 ;; generated-autoload-file: "org-loaddefs.el" 1820 ;; End: 1821 1822 ;;; org-macs.el ends here