org-pcomplete.el 13 KB

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