org-pcomplete.el 14 KB

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