org-pcomplete.el 14 KB

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