org-pcomplete.el 15 KB

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