org-pcomplete.el 14 KB

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