org-pcomplete.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. ;;; org-pcomplete.el --- In-buffer completion code
  2. ;; Copyright (C) 2004-2012 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-get-current-options "org-exp" ())
  32. (declare-function org-make-org-heading-search-string "org"
  33. (&optional string heading))
  34. (declare-function org-get-buffer-tags "org" ())
  35. (declare-function org-get-tags "org" ())
  36. (declare-function org-buffer-property-keys "org"
  37. (&optional include-specials include-defaults include-columns))
  38. (declare-function org-entry-properties "org" (&optional pom which specific))
  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. (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 (org-re "[: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 (org-looking-back (substring org-drawer-regexp 0 -1))
  87. (org-looking-back org-property-re))))
  88. (cons "prop" nil))
  89. ((and (equal (char-before beg1) ?:)
  90. (not (equal (char-after (point-at-bol)) ?*)))
  91. (cons "drawer" nil))
  92. (t nil))))
  93. (defun org-command-at-point ()
  94. "Return the qualified name of the Org completion entity at point.
  95. When completing for #+STARTUP, for example, this function returns
  96. \"file-option/startup\"."
  97. (let ((thing (org-thing-at-point)))
  98. (cond
  99. ((string= "file-option" (car thing))
  100. (concat (car thing) "/" (downcase (cdr thing))))
  101. ((string= "block-option" (car thing))
  102. (concat (car thing) "/" (downcase (cdr thing))))
  103. (t
  104. (car thing)))))
  105. (defun org-parse-arguments ()
  106. "Parse whitespace separated arguments in the current region."
  107. (let ((begin (line-beginning-position))
  108. (end (line-end-position))
  109. begins args)
  110. (save-restriction
  111. (narrow-to-region begin end)
  112. (save-excursion
  113. (goto-char (point-min))
  114. (while (not (eobp))
  115. (skip-chars-forward " \t\n[")
  116. (setq begins (cons (point) begins))
  117. (skip-chars-forward "^ \t\n[")
  118. (setq args (cons (buffer-substring-no-properties
  119. (car begins) (point))
  120. args)))
  121. (cons (reverse args) (reverse begins))))))
  122. (defun org-pcomplete-initial ()
  123. "Calls the right completion function for first argument completions."
  124. (ignore
  125. (funcall (or (pcomplete-find-completion-function
  126. (car (org-thing-at-point)))
  127. pcomplete-default-completion-function))))
  128. (defvar org-options-keywords) ; From org.el
  129. (defvar org-additional-option-like-keywords) ; From org.el
  130. (defun pcomplete/org-mode/file-option ()
  131. "Complete against all valid file options."
  132. (require 'org-exp)
  133. (pcomplete-here
  134. (org-pcomplete-case-double
  135. (mapcar (lambda (x)
  136. (if (= ?: (aref x (1- (length x))))
  137. (concat x " ")
  138. x))
  139. (append org-options-keywords
  140. org-additional-option-like-keywords)))
  141. (substring pcomplete-stub 2)))
  142. (defvar org-startup-options)
  143. (defun pcomplete/org-mode/file-option/startup ()
  144. "Complete arguments for the #+STARTUP file option."
  145. (while (pcomplete-here
  146. (let ((opts (pcomplete-uniqify-list
  147. (mapcar 'car org-startup-options))))
  148. ;; Some options are mutually exclusive, and shouldn't be completed
  149. ;; against if certain other options have already been seen.
  150. (dolist (arg pcomplete-args)
  151. (cond
  152. ((string= arg "hidestars")
  153. (setq opts (delete "showstars" opts)))))
  154. opts))))
  155. (defmacro pcomplete/org-mode/file-option/x (option)
  156. "Complete arguments for OPTION."
  157. `(while
  158. (pcomplete-here
  159. (pcomplete-uniqify-list
  160. (delq nil
  161. (mapcar (lambda(o)
  162. (when (string-match (concat "^[ \t]*#\\+"
  163. ,option ":[ \t]+\\(.*\\)[ \t]*$") o)
  164. (match-string 1 o)))
  165. (split-string (org-get-current-options) "\n")))))))
  166. (defun pcomplete/org-mode/file-option/options ()
  167. "Complete arguments for the #+OPTIONS file option."
  168. (pcomplete/org-mode/file-option/x "OPTIONS"))
  169. (defun pcomplete/org-mode/file-option/title ()
  170. "Complete arguments for the #+TITLE file option."
  171. (pcomplete/org-mode/file-option/x "TITLE"))
  172. (defun pcomplete/org-mode/file-option/author ()
  173. "Complete arguments for the #+AUTHOR file option."
  174. (pcomplete/org-mode/file-option/x "AUTHOR"))
  175. (defun pcomplete/org-mode/file-option/email ()
  176. "Complete arguments for the #+EMAIL file option."
  177. (pcomplete/org-mode/file-option/x "EMAIL"))
  178. (defun pcomplete/org-mode/file-option/date ()
  179. "Complete arguments for the #+DATE file option."
  180. (pcomplete/org-mode/file-option/x "DATE"))
  181. (defun pcomplete/org-mode/file-option/bind ()
  182. "Complete arguments for the #+BIND file option, which are variable names."
  183. (let (vars)
  184. (mapatoms
  185. (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
  186. (pcomplete-here vars)))
  187. (defvar org-link-abbrev-alist-local)
  188. (defvar org-link-abbrev-alist)
  189. (defun pcomplete/org-mode/link ()
  190. "Complete against defined #+LINK patterns."
  191. (pcomplete-here
  192. (pcomplete-uniqify-list
  193. (copy-sequence
  194. (append (mapcar 'car org-link-abbrev-alist-local)
  195. (mapcar 'car org-link-abbrev-alist))))))
  196. (defvar org-entities)
  197. (defun pcomplete/org-mode/tex ()
  198. "Complete against TeX-style HTML entity names."
  199. (require 'org-entities)
  200. (while (pcomplete-here
  201. (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
  202. (substring pcomplete-stub 1))))
  203. (defvar org-todo-keywords-1)
  204. (defun pcomplete/org-mode/todo ()
  205. "Complete against known TODO keywords."
  206. (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
  207. (defvar org-todo-line-regexp)
  208. (defun pcomplete/org-mode/searchhead ()
  209. "Complete against all headings.
  210. This needs more work, to handle headings with lots of spaces in them."
  211. (while
  212. (pcomplete-here
  213. (save-excursion
  214. (goto-char (point-min))
  215. (let (tbl)
  216. (while (re-search-forward org-todo-line-regexp nil t)
  217. (push (org-make-org-heading-search-string
  218. (match-string-no-properties 3) t)
  219. tbl))
  220. (pcomplete-uniqify-list tbl)))
  221. (substring pcomplete-stub 1))))
  222. (defvar org-tag-alist)
  223. (defun pcomplete/org-mode/tag ()
  224. "Complete a tag name. Omit tags already set."
  225. (while (pcomplete-here
  226. (mapcar (lambda (x)
  227. (concat x ":"))
  228. (let ((lst (pcomplete-uniqify-list
  229. (or (remove
  230. nil
  231. (mapcar (lambda (x)
  232. (and (stringp (car x)) (car x)))
  233. org-tag-alist))
  234. (mapcar 'car (org-get-buffer-tags))))))
  235. (dolist (tag (org-get-tags))
  236. (setq lst (delete tag lst)))
  237. lst))
  238. (and (string-match ".*:" pcomplete-stub)
  239. (substring pcomplete-stub (match-end 0))))))
  240. (defun pcomplete/org-mode/prop ()
  241. "Complete a property name. Omit properties already set."
  242. (pcomplete-here
  243. (mapcar (lambda (x)
  244. (concat x ": "))
  245. (let ((lst (pcomplete-uniqify-list
  246. (copy-sequence
  247. (org-buffer-property-keys nil t t)))))
  248. (dolist (prop (org-entry-properties))
  249. (setq lst (delete (car prop) lst)))
  250. lst))
  251. (substring pcomplete-stub 1)))
  252. (defvar org-drawers)
  253. (defun pcomplete/org-mode/drawer ()
  254. "Complete a drawer name."
  255. (let ((spc (save-excursion
  256. (move-beginning-of-line 1)
  257. (looking-at "^\\([ \t]*\\):")
  258. (match-string 1)))
  259. (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
  260. (pcomplete-here cpllist
  261. (substring pcomplete-stub 1)
  262. (unless (or (not (delete
  263. nil
  264. (mapcar (lambda(x)
  265. (string-match (substring pcomplete-stub 1) x))
  266. cpllist)))
  267. (looking-at "[ \t]*\n.*:END:"))
  268. (save-excursion (insert "\n" spc ":END:"))))))
  269. (defun pcomplete/org-mode/block-option/src ()
  270. "Complete the arguments of a begin_src block.
  271. Complete a language in the first field, the header arguments and switches."
  272. (pcomplete-here
  273. (mapcar
  274. (lambda(x) (symbol-name (nth 3 x)))
  275. (cdr (car (cdr (memq :key-type (plist-get
  276. (symbol-plist
  277. 'org-babel-load-languages)
  278. 'custom-type)))))))
  279. (while (pcomplete-here
  280. '("-n" "-r" "-l"
  281. ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
  282. ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
  283. ":session" ":shebang" ":tangle" ":var"))))
  284. (defun pcomplete/org-mode/block-option/clocktable ()
  285. "Complete keywords in a clocktable line."
  286. (while (pcomplete-here '(":maxlevel" ":scope"
  287. ":tstart" ":tend" ":block" ":step"
  288. ":stepskip0" ":fileskip0"
  289. ":emphasize" ":link" ":narrow" ":indent"
  290. ":tcolumns" ":level" ":compact" ":timestamp"
  291. ":formula" ":formatter"))))
  292. (defun org-pcomplete-case-double (list)
  293. "Return list with both upcase and downcase version of all strings in LIST."
  294. (let (e res)
  295. (while (setq e (pop list))
  296. (setq res (cons (downcase e) (cons (upcase e) res))))
  297. (nreverse res)))
  298. ;;;; Finish up
  299. (provide 'org-pcomplete)
  300. ;;; org-pcomplete.el ends here