org-pcomplete.el 14 KB

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