org-pcomplete.el 14 KB

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