org-pcomplete.el 13 KB

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