org-pcomplete.el 15 KB

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