org-pcomplete.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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-match-line
  98. (format "\\*+ \\(?:.+? \\)?\\(:\\)\\(\\(?::\\|%s\\)+\\)?[ \t]*$"
  99. org-tag-re))
  100. (or (org-point-in-group (point) 2)
  101. (= (point) (match-end 1))))
  102. (cons "tag" nil))
  103. ;; TODO keywords on an empty headline.
  104. ((and (string-match "^\\*+ +\\S-*$" line-to-here)
  105. (looking-at-p "[ \t]*$"))
  106. (cons "todo" nil))
  107. ;; Heading after a star for search strings or links.
  108. ((save-excursion
  109. (skip-chars-backward "^*" (line-beginning-position))
  110. (and (eq ?* (char-before))
  111. (eq (char-before (1- (point))) '?\[)
  112. (eq (char-before (- (point) 2)) '?\[)))
  113. (cons "searchhead" nil))
  114. ;; Property or drawer name, depending on point. If point is at
  115. ;; a valid location for a node property, offer completion on all
  116. ;; node properties in the buffer. Otherwise, offer completion on
  117. ;; all drawer names, including "PROPERTIES".
  118. ((and (string-match "^[ \t]*:\\S-*$" line-to-here)
  119. (looking-at-p "[ \t]*$"))
  120. (let ((origin (line-beginning-position)))
  121. (if (org-before-first-heading-p) (cons "drawer" nil)
  122. (save-excursion
  123. (org-end-of-meta-data)
  124. (if (or (= origin (point))
  125. (not (org-match-line "[ \t]*:PROPERTIES:[ \t]*$")))
  126. (cons "drawer" nil)
  127. (while (org-match-line org-property-re)
  128. (forward-line))
  129. (if (= origin (point)) (cons "prop" nil)
  130. (cons "drawer" nil)))))))
  131. (t nil))))
  132. (defun org-pcomplete-case-double (list)
  133. "Return list with both upcase and downcase version of all strings in LIST."
  134. (let (e res)
  135. (while (setq e (pop list))
  136. (setq res (cons (downcase e) (cons (upcase e) res))))
  137. (nreverse res)))
  138. ;;; Completion API
  139. (defun org-command-at-point ()
  140. "Return the qualified name of the Org completion entity at point.
  141. When completing for #+STARTUP, for example, this function returns
  142. \"file-option/startup\"."
  143. (let ((thing (org-thing-at-point)))
  144. (cond
  145. ((string= "file-option" (car thing))
  146. (concat (car thing)
  147. (and (cdr thing) (concat "/" (downcase (cdr thing))))))
  148. ((string= "block-option" (car thing))
  149. (concat (car thing) "/" (downcase (cdr thing))))
  150. (t (car thing)))))
  151. (defun org-parse-arguments ()
  152. "Parse whitespace separated arguments in the current region."
  153. (let ((begin (line-beginning-position))
  154. (end (line-end-position))
  155. begins args)
  156. (save-restriction
  157. (narrow-to-region begin end)
  158. (save-excursion
  159. (goto-char (point-min))
  160. (while (not (eobp))
  161. (skip-chars-forward " \t\n[")
  162. (setq begins (cons (point) begins))
  163. (skip-chars-forward "^ \t\n[")
  164. (setq args (cons (buffer-substring-no-properties
  165. (car begins) (point))
  166. args)))
  167. (cons (reverse args) (reverse begins))))))
  168. (defun org-pcomplete-initial ()
  169. "Calls the right completion function for first argument completions."
  170. (ignore
  171. (funcall (or (pcomplete-find-completion-function
  172. (car (org-thing-at-point)))
  173. pcomplete-default-completion-function))))
  174. ;;; Completion functions
  175. (defun pcomplete/org-mode/file-option ()
  176. "Complete against all valid file options."
  177. (require 'org-element)
  178. (pcomplete-here
  179. (org-pcomplete-case-double
  180. (append (mapcar (lambda (keyword) (concat keyword " "))
  181. org-options-keywords)
  182. (mapcar (lambda (keyword) (concat keyword ": "))
  183. org-element-affiliated-keywords)
  184. (let (block-names)
  185. (dolist (name
  186. '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
  187. "VERSE")
  188. block-names)
  189. (push (format "END_%s" name) block-names)
  190. (push (concat "BEGIN_"
  191. name
  192. ;; Since language is compulsory in
  193. ;; export blocks source blocks, add
  194. ;; a space.
  195. (and (member name '("EXPORT" "SRC")) " "))
  196. block-names)
  197. (push (format "ATTR_%s: " name) block-names)))
  198. (mapcar (lambda (keyword) (concat keyword ": "))
  199. (org-get-export-keywords))))
  200. (substring pcomplete-stub 2)))
  201. (defun pcomplete/org-mode/file-option/author ()
  202. "Complete arguments for the #+AUTHOR file option."
  203. (pcomplete-here (list user-full-name)))
  204. (defun pcomplete/org-mode/file-option/date ()
  205. "Complete arguments for the #+DATE file option."
  206. (pcomplete-here (list (format-time-string (car org-time-stamp-formats)))))
  207. (defun pcomplete/org-mode/file-option/email ()
  208. "Complete arguments for the #+EMAIL file option."
  209. (pcomplete-here (list user-mail-address)))
  210. (defun pcomplete/org-mode/file-option/exclude_tags ()
  211. "Complete arguments for the #+EXCLUDE_TAGS file option."
  212. (require 'ox)
  213. (pcomplete-here
  214. (and org-export-exclude-tags
  215. (list (mapconcat 'identity org-export-exclude-tags " ")))))
  216. (defun pcomplete/org-mode/file-option/filetags ()
  217. "Complete arguments for the #+FILETAGS file option."
  218. (pcomplete-here (and org-file-tags (mapconcat 'identity org-file-tags " "))))
  219. (defun pcomplete/org-mode/file-option/language ()
  220. "Complete arguments for the #+LANGUAGE file option."
  221. (require 'ox)
  222. (pcomplete-here
  223. (pcomplete-uniquify-list
  224. (list org-export-default-language "en"))))
  225. (defun pcomplete/org-mode/file-option/priorities ()
  226. "Complete arguments for the #+PRIORITIES file option."
  227. (pcomplete-here (list (format "%c %c %c"
  228. org-highest-priority
  229. org-lowest-priority
  230. org-default-priority))))
  231. (defun pcomplete/org-mode/file-option/select_tags ()
  232. "Complete arguments for the #+SELECT_TAGS file option."
  233. (require 'ox)
  234. (pcomplete-here
  235. (and org-export-select-tags
  236. (list (mapconcat 'identity org-export-select-tags " ")))))
  237. (defun pcomplete/org-mode/file-option/startup ()
  238. "Complete arguments for the #+STARTUP file option."
  239. (while (pcomplete-here
  240. (let ((opts (pcomplete-uniquify-list
  241. (mapcar 'car org-startup-options))))
  242. ;; Some options are mutually exclusive, and shouldn't be completed
  243. ;; against if certain other options have already been seen.
  244. (dolist (arg pcomplete-args)
  245. (cond
  246. ((string= arg "hidestars")
  247. (setq opts (delete "showstars" opts)))))
  248. opts))))
  249. (defun pcomplete/org-mode/file-option/tags ()
  250. "Complete arguments for the #+TAGS file option."
  251. (pcomplete-here
  252. (list (org-tag-alist-to-string org-current-tag-alist))))
  253. (defun pcomplete/org-mode/file-option/title ()
  254. "Complete arguments for the #+TITLE file option."
  255. (pcomplete-here
  256. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  257. (list (or (and visited-file
  258. (file-name-sans-extension
  259. (file-name-nondirectory visited-file)))
  260. (buffer-name (buffer-base-buffer)))))))
  261. (defun pcomplete/org-mode/file-option/options ()
  262. "Complete arguments for the #+OPTIONS file option."
  263. (while (pcomplete-here
  264. (pcomplete-uniquify-list
  265. (append
  266. ;; Hard-coded OPTION items always available.
  267. '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
  268. "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
  269. "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
  270. "|:" "tags:" "tasks:" "<:" "todo:")
  271. ;; OPTION items from registered back-ends.
  272. (let (items)
  273. (dolist (backend (bound-and-true-p
  274. org-export-registered-backends))
  275. (dolist (option (org-export-backend-options backend))
  276. (let ((item (nth 2 option)))
  277. (when item (push (concat item ":") items)))))
  278. items))))))
  279. (defun pcomplete/org-mode/file-option/infojs_opt ()
  280. "Complete arguments for the #+INFOJS_OPT file option."
  281. (while (pcomplete-here
  282. (pcomplete-uniquify-list
  283. (mapcar (lambda (item) (format "%s:" (car item)))
  284. (bound-and-true-p org-html-infojs-opts-table))))))
  285. (defun pcomplete/org-mode/file-option/bind ()
  286. "Complete arguments for the #+BIND file option, which are variable names."
  287. (let (vars)
  288. (mapatoms
  289. (lambda (a) (when (boundp a) (setq vars (cons (symbol-name a) vars)))))
  290. (pcomplete-here vars)))
  291. (defun pcomplete/org-mode/link ()
  292. "Complete against defined #+LINK patterns."
  293. (pcomplete-here
  294. (pcomplete-uniquify-list
  295. (copy-sequence
  296. (mapcar (lambda (e) (concat (car e) ":"))
  297. (append org-link-abbrev-alist-local
  298. org-link-abbrev-alist))))))
  299. (defun pcomplete/org-mode/tex ()
  300. "Complete against TeX-style HTML entity names."
  301. (require 'org-entities)
  302. (while (pcomplete-here
  303. (pcomplete-uniquify-list (remove nil (mapcar 'car-safe org-entities)))
  304. (substring pcomplete-stub 1))))
  305. (defun pcomplete/org-mode/todo ()
  306. "Complete against known TODO keywords."
  307. (pcomplete-here (pcomplete-uniquify-list (copy-sequence org-todo-keywords-1))))
  308. (defun pcomplete/org-mode/searchhead ()
  309. "Complete against all headings.
  310. This needs more work, to handle headings with lots of spaces in them."
  311. (while (pcomplete-here
  312. (save-excursion
  313. (goto-char (point-min))
  314. (let (tbl)
  315. (while (re-search-forward org-outline-regexp nil t)
  316. (push (org-make-org-heading-search-string
  317. (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