config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

org-pcomplete.el (16780B)


      1 ;;; org-pcomplete.el --- In-buffer Completion Code -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2004-2024 Free Software Foundation, Inc.
      4 ;;
      5 ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
      6 ;;         John Wiegley <johnw at gnu dot org>
      7 ;; Keywords: outlines, hypermedia, calendar, text
      8 ;; URL: https://orgmode.org
      9 ;;
     10 ;; This file is part of GNU Emacs.
     11 ;;
     12 ;; GNU Emacs is free software: you can redistribute it and/or modify
     13 ;; it under the terms of the GNU General Public License as published by
     14 ;; the Free Software Foundation, either version 3 of the License, or
     15 ;; (at your option) any later version.
     16 
     17 ;; GNU Emacs is distributed in the hope that it will be useful,
     18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20 ;; GNU General Public License for more details.
     21 
     22 ;; You should have received a copy of the GNU General Public License
     23 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     24 
     25 ;;; Commentary:
     26 
     27 ;; This library implements completion support in Org mode buffers.
     28 
     29 ;;; Code:
     30 
     31 ;;;; Require other packages
     32 
     33 (require 'org-macs)
     34 (org-assert-version)
     35 
     36 (require 'org-macs)
     37 (require 'org-compat)
     38 (require 'pcomplete)
     39 
     40 (declare-function org-at-heading-p "org" (&optional ignored))
     41 (declare-function org-babel-combine-header-arg-lists "ob-core" (original &rest others))
     42 (declare-function org-babel-get-src-block-info "ob-core" (&optional no-eval datum))
     43 (declare-function org-before-first-heading-p "org" ())
     44 (declare-function org-buffer-property-keys "org" (&optional specials defaults columns))
     45 (declare-function org-element-at-point "org-element" (&optional pom cached-only))
     46 (declare-function org-element-property "org-element-ast" (property node &optional dflt force-undefer))
     47 (declare-function org-element-end "org-element" (node))
     48 (declare-function org-element-type-p "org-element-ast" (node types))
     49 (declare-function org-end-of-meta-data "org" (&optional full))
     50 (declare-function org-entry-properties "org" (&optional pom which))
     51 (declare-function org-export-backend-options "ox" (cl-x) t)
     52 (declare-function org-get-buffer-tags "org" ())
     53 (declare-function org-get-export-keywords "org" ())
     54 (declare-function org-get-heading "org" (&optional no-tags no-todo no-priority no-comment))
     55 (declare-function org-get-tags "org" (&optional pos local))
     56 (declare-function org-link-heading-search-string "ol" (&optional string))
     57 (declare-function org-tag-alist-to-string "org" (alist &optional skip-key))
     58 (declare-function org-time-stamp-format "org" (&optional with-time inactive custom))
     59 
     60 (defvar org-babel-common-header-args-w-values)
     61 (defvar org-current-tag-alist)
     62 (defvar org-priority-default)
     63 (defvar org-drawer-regexp)
     64 (defvar org-element-affiliated-keywords)
     65 (defvar org-entities)
     66 (defvar org-export-default-language)
     67 (defvar org-export-exclude-tags)
     68 (defvar org-export-select-tags)
     69 (defvar org-file-tags)
     70 (defvar org-priority-highest)
     71 (defvar org-link-abbrev-alist)
     72 (defvar org-link-abbrev-alist-local)
     73 (defvar org-priority-lowest)
     74 (defvar org-options-keywords)
     75 (defvar org-outline-regexp)
     76 (defvar org-property-re)
     77 (defvar org-startup-options)
     78 (defvar org-tag-re)
     79 (defvar org-todo-keywords-1)
     80 (defvar org-todo-line-regexp)
     81 
     82 
     83 ;;; Internal Functions
     84 
     85 (defun org-thing-at-point ()
     86   "Examine the thing at point and let the caller know what it is.
     87 The return value is a string naming the thing at point."
     88   (let ((line-to-here (org-current-line-string t))
     89 	(case-fold-search t))
     90     (cond
     91      ;; Parameters on a clock table opening line.
     92      ((org-match-line "[ \t]*#\\+BEGIN: clocktable[ \t]")
     93       (cons "block-option" "clocktable"))
     94      ;; Flags and parameters on a source block opening line.
     95      ((org-match-line "[ \t]*#\\+BEGIN_SRC[ \t]")
     96       (cons "block-option" "src"))
     97      ;; Value for a known keyword.
     98      ((org-match-line "[ \t]*#\\+\\(\\S-+\\):")
     99       (cons "file-option" (match-string-no-properties 1)))
    100      ;; Keyword name.
    101      ((and (org-match-line "[ \t]*#\\+[a-zA-Z_]*$")
    102 	   (looking-at-p "[ \t]*$"))
    103       (cons "file-option" nil))
    104      ;; Link abbreviation.
    105      ((save-excursion
    106 	(skip-chars-backward "-A-Za-z0-9_")
    107 	(and (eq ?\[ (char-before))
    108 	     (eq ?\[ (char-before (1- (point))))))
    109       (cons "link" nil))
    110      ;; Entities.  Some of them accept numbers, but only at their end.
    111      ;; So, we first skip numbers, then letters.
    112      ((eq ?\\ (save-excursion
    113 		(skip-chars-backward "0-9")
    114 		(skip-chars-backward "a-zA-Z")
    115 		(char-before)))
    116       (cons "tex" nil))
    117      ;; Tags on a headline.
    118      ((and (org-match-line
    119 	    (format "\\*+ \\(?:.+? \\)?\\(:\\)\\(\\(?::\\|%s\\)+\\)?[ \t]*$"
    120 		    org-tag-re))
    121 	   (or (org-point-in-group (point) 2)
    122 	       (= (point) (match-end 1))))
    123       (cons "tag" nil))
    124      ;; TODO keywords on an empty headline.
    125      ((and (string-match "^\\*+ +\\S-*$" line-to-here)
    126 	   (looking-at-p "[ \t]*$"))
    127       (cons "todo" nil))
    128      ;; Heading after a star for search strings or links.
    129      ((save-excursion
    130 	(skip-chars-backward "^*" (line-beginning-position))
    131 	(and (eq ?* (char-before))
    132 	     (eq (char-before (1- (point))) '?\[)
    133 	     (eq (char-before (- (point) 2)) '?\[)))
    134       (cons "searchhead" nil))
    135      ;; Property or drawer name, depending on point.  If point is at
    136      ;; a valid location for a node property, offer completion on all
    137      ;; node properties in the buffer. Otherwise, offer completion on
    138      ;; all drawer names, including "PROPERTIES".
    139      ((and (string-match "^[ \t]*:\\S-*$" line-to-here)
    140 	   (looking-at-p "[ \t]*$"))
    141       (let ((origin (line-beginning-position)))
    142 	(if (org-before-first-heading-p) (cons "drawer" nil)
    143 	  (save-excursion
    144 	    (org-end-of-meta-data)
    145 	    (if (or (= origin (point))
    146 		    (not (org-match-line "[ \t]*:PROPERTIES:[ \t]*$")))
    147 		(cons "drawer" nil)
    148 	      (while (org-match-line org-property-re)
    149 		(forward-line))
    150 	      (if (= origin (point)) (cons "prop" nil)
    151 		(cons "drawer" nil)))))))
    152      (t nil))))
    153 
    154 (defun org-pcomplete-case-double (list)
    155   "Return list with both upcase and downcase version of all strings in LIST."
    156   (let (e res)
    157     (while (setq e (pop list))
    158       (setq res (cons (downcase e) (cons (upcase e) res))))
    159     (nreverse res)))
    160 
    161 
    162 ;;; Completion API
    163 
    164 (defun org-command-at-point ()
    165   "Return the qualified name of the Org completion entity at point.
    166 When completing for #+STARTUP, for example, this function returns
    167 \"file-option/startup\"."
    168   (let ((thing (org-thing-at-point)))
    169     (cond
    170      ((string= "file-option" (car thing))
    171       (concat (car thing)
    172 	      (and (cdr thing) (concat "/" (downcase (cdr thing))))))
    173      ((string= "block-option" (car thing))
    174       (concat (car thing) "/" (downcase (cdr thing))))
    175      (t (car thing)))))
    176 
    177 (defun org-parse-arguments ()
    178   "Parse whitespace separated arguments in the current region."
    179   (if (equal (cons "searchhead" nil) (org-thing-at-point))
    180       ;; [[* foo<point> bar link::search option.
    181       ;; Arguments are not simply space-separated.
    182       (save-excursion
    183         (let ((origin (point)))
    184           (skip-chars-backward "^*" (line-beginning-position))
    185           (cons (list (buffer-substring-no-properties (point) origin))
    186                 (list (point)))))
    187     (let ((begin (line-beginning-position))
    188 	  (end (line-end-position))
    189 	  begins args)
    190       (save-restriction
    191         (narrow-to-region begin end)
    192         (save-excursion
    193 	  (goto-char (point-min))
    194 	  (while (not (eobp))
    195 	    (skip-chars-forward " \t\n[")
    196 	    (setq begins (cons (point) begins))
    197 	    (skip-chars-forward "^ \t\n[")
    198 	    (setq args (cons (buffer-substring-no-properties
    199 			      (car begins) (point))
    200 			     args)))
    201 	  (cons (reverse args) (reverse begins)))))))
    202 
    203 (defun org-pcomplete-initial ()
    204   "Call the right completion function for first argument completions."
    205   (ignore
    206    (funcall (or (pcomplete-find-completion-function
    207 		 (car (org-thing-at-point)))
    208 		pcomplete-default-completion-function))))
    209 
    210 
    211 ;;; Completion functions
    212 
    213 (defun pcomplete/org-mode/file-option ()
    214   "Complete against all valid file options."
    215   (require 'org-element)
    216   (pcomplete-here
    217    (org-pcomplete-case-double
    218     (append (mapcar (lambda (keyword) (concat keyword " "))
    219 		    org-options-keywords)
    220 	    (mapcar (lambda (keyword) (concat keyword ": "))
    221 		    org-element-affiliated-keywords)
    222 	    (let (block-names)
    223 	      (dolist (name
    224 		       '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
    225 			 "VERSE")
    226 		       block-names)
    227 		(push (format "END_%s" name) block-names)
    228 		(push (concat "BEGIN_"
    229 			      name
    230 			      ;; Since language is compulsory in
    231 			      ;; export blocks source blocks, add
    232 			      ;; a space.
    233 			      (and (member name '("EXPORT" "SRC")) " "))
    234 		      block-names)
    235 		(push (format "ATTR_%s: " name) block-names)))
    236 	    (mapcar (lambda (keyword) (concat keyword ": "))
    237 		    (org-get-export-keywords))))
    238    (substring pcomplete-stub 2)))
    239 
    240 (defun pcomplete/org-mode/file-option/author ()
    241   "Complete arguments for the #+AUTHOR file option."
    242   (pcomplete-here (list user-full-name)))
    243 
    244 (defun pcomplete/org-mode/file-option/date ()
    245   "Complete arguments for the #+DATE file option."
    246   (pcomplete-here (list (format-time-string (org-time-stamp-format)))))
    247 
    248 (defun pcomplete/org-mode/file-option/email ()
    249   "Complete arguments for the #+EMAIL file option."
    250   (pcomplete-here (list user-mail-address)))
    251 
    252 (defun pcomplete/org-mode/file-option/exclude_tags ()
    253   "Complete arguments for the #+EXCLUDE_TAGS file option."
    254   (require 'ox)
    255   (pcomplete-here
    256    (and org-export-exclude-tags
    257 	(list (mapconcat #'identity org-export-exclude-tags " ")))))
    258 
    259 (defun pcomplete/org-mode/file-option/filetags ()
    260   "Complete arguments for the #+FILETAGS file option."
    261   (pcomplete-here (and org-file-tags (mapconcat #'identity org-file-tags " "))))
    262 
    263 (defun pcomplete/org-mode/file-option/language ()
    264   "Complete arguments for the #+LANGUAGE file option."
    265   (require 'ox)
    266   (pcomplete-here
    267    (pcomplete-uniquify-list
    268     (list org-export-default-language "en"))))
    269 
    270 (defun pcomplete/org-mode/file-option/priorities ()
    271   "Complete arguments for the #+PRIORITIES file option."
    272   (pcomplete-here (list (format "%c %c %c"
    273 				org-priority-highest
    274 				org-priority-lowest
    275 				org-priority-default))))
    276 
    277 (defun pcomplete/org-mode/file-option/select_tags ()
    278   "Complete arguments for the #+SELECT_TAGS file option."
    279   (require 'ox)
    280   (pcomplete-here
    281    (and org-export-select-tags
    282 	(list (mapconcat #'identity org-export-select-tags " ")))))
    283 
    284 (defun pcomplete/org-mode/file-option/startup ()
    285   "Complete arguments for the #+STARTUP file option."
    286   (while (pcomplete-here
    287 	  (let ((opts (pcomplete-uniquify-list
    288 		       (mapcar #'car org-startup-options))))
    289 	    ;; Some options are mutually exclusive, and shouldn't be completed
    290 	    ;; against if certain other options have already been seen.
    291 	    (dolist (arg pcomplete-args)
    292 	      (cond
    293 	       ((string= arg "hidestars")
    294 		(setq opts (delete "showstars" opts)))))
    295 	    opts))))
    296 
    297 (defun pcomplete/org-mode/file-option/tags ()
    298   "Complete arguments for the #+TAGS file option."
    299   (pcomplete-here
    300    (list (org-tag-alist-to-string org-current-tag-alist))))
    301 
    302 (defun pcomplete/org-mode/file-option/title ()
    303   "Complete arguments for the #+TITLE file option."
    304   (pcomplete-here
    305    (let ((visited-file (buffer-file-name (buffer-base-buffer))))
    306      (list (or (and visited-file
    307 		    (file-name-sans-extension
    308 		     (file-name-nondirectory visited-file)))
    309 	       (buffer-name (buffer-base-buffer)))))))
    310 
    311 
    312 (defun pcomplete/org-mode/file-option/options ()
    313   "Complete arguments for the #+OPTIONS file option."
    314   (while (pcomplete-here
    315 	  (pcomplete-uniquify-list
    316 	   (append
    317 	    ;; Hard-coded OPTION items always available.
    318 	    '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
    319 	      "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
    320 	      "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
    321 	      "|:" "tags:" "tasks:" "<:" "todo:")
    322 	    ;; OPTION items from registered backends.
    323 	    (let (items)
    324 	      (dolist (backend (bound-and-true-p
    325 				org-export-registered-backends))
    326 		(dolist (option (org-export-backend-options backend))
    327 		  (let ((item (nth 2 option)))
    328 		    (when item (push (concat item ":") items)))))
    329 	      items))))))
    330 
    331 (defun pcomplete/org-mode/file-option/infojs_opt ()
    332   "Complete arguments for the #+INFOJS_OPT file option."
    333   (while (pcomplete-here
    334 	  (pcomplete-uniquify-list
    335 	   (mapcar (lambda (item) (format "%s:" (car item)))
    336 		   (bound-and-true-p org-html-infojs-opts-table))))))
    337 
    338 (defun pcomplete/org-mode/file-option/bind ()
    339   "Complete arguments for the #+BIND file option, which are variable names."
    340   (let (vars)
    341     (mapatoms
    342      (lambda (a) (when (boundp a) (setq vars (cons (symbol-name a) vars)))))
    343     (pcomplete-here vars)))
    344 
    345 (defun pcomplete/org-mode/link ()
    346   "Complete against defined #+LINK patterns."
    347   (pcomplete-here
    348    (pcomplete-uniquify-list
    349     (copy-sequence
    350      (mapcar (lambda (e) (concat (car e) ":"))
    351 	     (append org-link-abbrev-alist-local
    352 		     org-link-abbrev-alist))))))
    353 
    354 (defun pcomplete/org-mode/tex ()
    355   "Complete against TeX-style HTML entity names."
    356   (require 'org-entities)
    357   (while (pcomplete-here
    358 	  (pcomplete-uniquify-list
    359 	   (remove nil (mapcar #'car-safe org-entities)))
    360 	  (substring pcomplete-stub 1))))
    361 
    362 (defun pcomplete/org-mode/todo ()
    363   "Complete against known TODO keywords."
    364   (pcomplete-here (pcomplete-uniquify-list (copy-sequence org-todo-keywords-1))))
    365 
    366 (defun pcomplete/org-mode/searchhead ()
    367   "Complete against all headings.
    368 This needs more work, to handle headings with lots of spaces in them."
    369   (while (pcomplete-here
    370 	  (save-excursion
    371 	    (goto-char (point-min))
    372 	    (let (tbl)
    373 	      (while (re-search-forward org-outline-regexp nil t)
    374 		;; Remove the leading asterisk from
    375 		;; `org-link-heading-search-string' result.
    376 		(push (substring (org-link-heading-search-string) 1) tbl))
    377 	      (pcomplete-uniquify-list tbl))))))
    378 
    379 (defun pcomplete/org-mode/tag ()
    380   "Complete a tag name.  Omit tags already set."
    381   (while (pcomplete-here
    382 	  (mapcar (lambda (x) (concat x ":"))
    383 		  (let ((lst (pcomplete-uniquify-list
    384 			      (or (remq
    385 				   nil
    386 				   (mapcar (lambda (x) (org-string-nw-p (car x)))
    387 					   org-current-tag-alist))
    388 				  (mapcar #'car (org-get-buffer-tags))))))
    389 		    (dolist (tag (org-get-tags nil t))
    390 		      (setq lst (delete tag lst)))
    391 		    lst))
    392 	  (and (string-match ".*:" pcomplete-stub)
    393 	       (substring pcomplete-stub (match-end 0)))
    394 	  t)))
    395 
    396 (defun pcomplete/org-mode/drawer ()
    397   "Complete a drawer name, including \"PROPERTIES\"."
    398   (pcomplete-here
    399    (org-pcomplete-case-double
    400     (mapcar (lambda (x) (concat x ":"))
    401 	    (let ((names (list "PROPERTIES")))
    402 	      (save-excursion
    403 		(goto-char (point-min))
    404 		(while (re-search-forward org-drawer-regexp nil t)
    405 		  (let ((drawer (org-element-at-point)))
    406 		    (when (org-element-type-p drawer '(drawer property-drawer))
    407 		      (push (org-element-property :drawer-name drawer) names)
    408 		      (goto-char (org-element-end drawer))))))
    409 	      (pcomplete-uniquify-list names))))
    410    (substring pcomplete-stub 1)))	;remove initial colon
    411 
    412 (defun pcomplete/org-mode/prop ()
    413   "Complete a property name.  Omit properties already set."
    414   (pcomplete-here
    415    (org-pcomplete-case-double
    416     (mapcar (lambda (x)
    417 	      (concat x ": "))
    418 	    (let ((lst (pcomplete-uniquify-list
    419 			(copy-sequence (org-buffer-property-keys nil t t)))))
    420 	      (dolist (prop (org-entry-properties))
    421 		(setq lst (delete (car prop) lst)))
    422 	      lst)))
    423    (substring pcomplete-stub 1)))
    424 
    425 (defun pcomplete/org-mode/block-option/src ()
    426   "Complete the arguments of a source block.
    427 Complete a language in the first field, the header arguments and
    428 switches."
    429   (pcomplete-here
    430    (mapcar
    431     (lambda(x) (symbol-name (nth 3 x)))
    432     (cdr (car (cdr (memq :key-type (plist-get
    433 				    (symbol-plist
    434 				     'org-babel-load-languages)
    435 				    'custom-type)))))))
    436   (let* ((info (org-babel-get-src-block-info 'no-eval))
    437 	 (lang (car info))
    438 	 (lang-headers (intern (concat "org-babel-header-args:" lang)))
    439 	 (headers (org-babel-combine-header-arg-lists
    440 		   org-babel-common-header-args-w-values
    441 		   (and (boundp lang-headers) (eval lang-headers t)))))
    442     (while (pcomplete-here
    443 	    (append (mapcar
    444 		     (lambda (arg) (format ":%s" (symbol-name (car arg))))
    445 		     headers)
    446 		    '("-n" "-r" "-l"))))))
    447 
    448 (defun pcomplete/org-mode/block-option/clocktable ()
    449   "Complete keywords in a clocktable line."
    450   (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
    451 			   ":tstart" ":tend" ":block" ":step"
    452 			   ":stepskip0" ":fileskip0"
    453 			   ":emphasize" ":link" ":narrow" ":indent"
    454 			   ":hidefiles" ":tcolumns" ":level" ":compact"
    455 			   ":timestamp" ":formula" ":formatter"
    456 			   ":wstart" ":mstart"))))
    457 
    458 
    459 ;;; Finish up
    460 
    461 (provide 'org-pcomplete)
    462 
    463 ;;; org-pcomplete.el ends here