org-pcomplete.el 11 KB

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