org-pcomplete.el 14 KB

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