org-pcomplete.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. (defun org-thing-at-point ()
  44. "Examine the thing at point and let the caller know what it is.
  45. The return value is a string naming the thing at point."
  46. (let ((beg1 (save-excursion
  47. (skip-chars-backward (org-re "[:alnum:]_@"))
  48. (point)))
  49. (beg (save-excursion
  50. (skip-chars-backward "a-zA-Z0-9_:$")
  51. (point)))
  52. (line-to-here (buffer-substring (point-at-bol) (point))))
  53. (cond
  54. ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here)
  55. (cons "block-option" "clocktable"))
  56. ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here)
  57. (cons "block-option" "src"))
  58. ((save-excursion
  59. (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
  60. (line-beginning-position) t))
  61. (cons "file-option" (match-string-no-properties 1)))
  62. ((string-match "\\`[ \t]*#\\+[a-zA-Z]*\\'" line-to-here)
  63. (cons "file-option" nil))
  64. ((equal (char-before beg) ?\[)
  65. (cons "link" nil))
  66. ((equal (char-before beg) ?\\)
  67. (cons "tex" nil))
  68. ((string-match "\\`\\*+[ \t]+\\'"
  69. (buffer-substring (point-at-bol) beg))
  70. (cons "todo" nil))
  71. ((equal (char-before beg) ?*)
  72. (cons "searchhead" nil))
  73. ((and (equal (char-before beg1) ?:)
  74. (equal (char-after (point-at-bol)) ?*))
  75. (cons "tag" nil))
  76. ((and (equal (char-before beg1) ?:)
  77. (not (equal (char-after (point-at-bol)) ?*))
  78. (save-excursion
  79. (move-beginning-of-line 1)
  80. (skip-chars-backward "[ \t\n]")
  81. (or (looking-back org-drawer-regexp)
  82. (looking-back org-property-re))))
  83. (cons "prop" nil))
  84. ((and (equal (char-before beg1) ?:)
  85. (not (equal (char-after (point-at-bol)) ?*)))
  86. (cons "drawer" nil))
  87. (t nil))))
  88. (defun org-command-at-point ()
  89. "Return the qualified name of the Org completion entity at point.
  90. When completing for #+STARTUP, for example, this function returns
  91. \"file-option/startup\"."
  92. (let ((thing (org-thing-at-point)))
  93. (cond
  94. ((string= "file-option" (car thing))
  95. (concat (car thing) "/" (downcase (cdr thing))))
  96. ((string= "block-option" (car thing))
  97. (concat (car thing) "/" (downcase (cdr thing))))
  98. (t
  99. (car thing)))))
  100. (defun org-parse-arguments ()
  101. "Parse whitespace separated arguments in the current region."
  102. (let ((begin (line-beginning-position))
  103. (end (line-end-position))
  104. begins args)
  105. (save-restriction
  106. (narrow-to-region begin end)
  107. (save-excursion
  108. (goto-char (point-min))
  109. (while (not (eobp))
  110. (skip-chars-forward " \t\n[")
  111. (setq begins (cons (point) begins))
  112. (skip-chars-forward "^ \t\n[")
  113. (setq args (cons (buffer-substring-no-properties
  114. (car begins) (point))
  115. args)))
  116. (cons (reverse args) (reverse begins))))))
  117. (defun org-pcomplete-initial ()
  118. "Calls the right completion function for first argument completions."
  119. (ignore
  120. (funcall (or (pcomplete-find-completion-function
  121. (car (org-thing-at-point)))
  122. pcomplete-default-completion-function))))
  123. (defvar org-additional-option-like-keywords)
  124. (defun pcomplete/org-mode/file-option ()
  125. "Complete against all valid file options."
  126. (require 'org-exp)
  127. (pcomplete-here
  128. (org-pcomplete-case-double
  129. (mapcar (lambda (x)
  130. (if (= ?: (aref x (1- (length x))))
  131. (concat x " ")
  132. x))
  133. (delq nil
  134. (pcomplete-uniqify-list
  135. (append
  136. (mapcar (lambda (x)
  137. (if (string-match "^#\\+\\([A-Z_]+:?\\)" x)
  138. (match-string 1 x)))
  139. (org-split-string (org-get-current-options) "\n"))
  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. (defun pcomplete/org-mode/file-option/bind ()
  156. "Complete arguments for the #+BIND file option, which are variable names"
  157. (let (vars)
  158. (mapatoms
  159. (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
  160. (pcomplete-here vars)))
  161. (defvar org-link-abbrev-alist-local)
  162. (defvar org-link-abbrev-alist)
  163. (defun pcomplete/org-mode/link ()
  164. "Complete against defined #+LINK patterns."
  165. (pcomplete-here
  166. (pcomplete-uniqify-list
  167. (copy-sequence
  168. (append (mapcar 'car org-link-abbrev-alist-local)
  169. (mapcar 'car org-link-abbrev-alist))))))
  170. (defvar org-entities)
  171. (defun pcomplete/org-mode/tex ()
  172. "Complete against TeX-style HTML entity names."
  173. (require 'org-entities)
  174. (while (pcomplete-here
  175. (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
  176. (substring pcomplete-stub 1))))
  177. (defvar org-todo-keywords-1)
  178. (defun pcomplete/org-mode/todo ()
  179. "Complete against known TODO keywords."
  180. (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
  181. (defvar org-todo-line-regexp)
  182. (defun pcomplete/org-mode/searchhead ()
  183. "Complete against all headings.
  184. This needs more work, to handle headings with lots of spaces in them."
  185. (while
  186. (pcomplete-here
  187. (save-excursion
  188. (goto-char (point-min))
  189. (let (tbl)
  190. (while (re-search-forward org-todo-line-regexp nil t)
  191. (push (org-make-org-heading-search-string
  192. (match-string-no-properties 3) t)
  193. tbl))
  194. (pcomplete-uniqify-list tbl)))
  195. (substring pcomplete-stub 1))))
  196. (defvar org-tag-alist)
  197. (defun pcomplete/org-mode/tag ()
  198. "Complete a tag name. Omit tags already set."
  199. (while (pcomplete-here
  200. (mapcar (lambda (x)
  201. (concat x ":"))
  202. (let ((lst (pcomplete-uniqify-list
  203. (or (remove
  204. nil
  205. (mapcar (lambda (x)
  206. (and (stringp (car x)) (car x)))
  207. org-tag-alist))
  208. (mapcar 'car (org-get-buffer-tags))))))
  209. (dolist (tag (org-get-tags))
  210. (setq lst (delete tag lst)))
  211. lst))
  212. (and (string-match ".*:" pcomplete-stub)
  213. (substring pcomplete-stub (match-end 0))))))
  214. (defun pcomplete/org-mode/prop ()
  215. "Complete a property name. Omit properties already set."
  216. (pcomplete-here
  217. (mapcar (lambda (x)
  218. (concat x ": "))
  219. (let ((lst (pcomplete-uniqify-list
  220. (copy-sequence
  221. (org-buffer-property-keys nil t t)))))
  222. (dolist (prop (org-entry-properties))
  223. (setq lst (delete (car prop) lst)))
  224. lst))
  225. (substring pcomplete-stub 1)))
  226. (defun pcomplete/org-mode/drawer ()
  227. "Complete a drawer name."
  228. (let ((spc (save-excursion
  229. (move-beginning-of-line 1)
  230. (looking-at "^\\([ \t]*\\):")
  231. (match-string 1)))
  232. (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
  233. (pcomplete-here cpllist
  234. (substring pcomplete-stub 1)
  235. (unless (or (not (delete
  236. nil
  237. (mapcar (lambda(x)
  238. (string-match (substring pcomplete-stub 1) x))
  239. cpllist)))
  240. (looking-at "[ \t]*\n.*:END:"))
  241. (save-excursion (insert "\n" spc ":END:"))))))
  242. (defun pcomplete/org-mode/block-option/src ()
  243. "Complete the arguments of a begin_src block.
  244. Complete a language in the first field, the header arguments and switches."
  245. (pcomplete-here
  246. (mapcar
  247. (lambda(x) (symbol-name (nth 3 x)))
  248. (cdr (car (cdr (memq :key-type (plist-get
  249. (symbol-plist
  250. 'org-babel-load-languages)
  251. 'custom-type)))))))
  252. (while (pcomplete-here
  253. '("-n" "-r" "-l"
  254. ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
  255. ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
  256. ":session" ":shebang" ":tangle" ":var"))))
  257. (defun pcomplete/org-mode/block-option/clocktable ()
  258. "Complete keywords in a clocktable line"
  259. (while (pcomplete-here '(":maxlevel" ":scope"
  260. ":tstart" ":tend" ":block" ":step"
  261. ":stepskip0" ":fileskip0"
  262. ":emphasize" ":link" ":narrow" ":indent"
  263. ":tcolumns" ":level" ":compact" ":timestamp"
  264. ":formula" ":formatter"))))
  265. (defun org-pcomplete-case-double (list)
  266. "Return list with both upcase and downcase version of all strings in LIST."
  267. (let (e res)
  268. (while (setq e (pop list))
  269. (setq res (cons (downcase e) (cons (upcase e) res))))
  270. (nreverse res)))
  271. ;;;; Finish up
  272. (provide 'org-pcomplete)
  273. ;;; org-pcomplete.el ends here