org-pcomplete.el 16 KB

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