org-pcomplete.el 13 KB

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