org-pcomplete.el 15 KB

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