org-pcomplete.el 16 KB

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