org-pcomplete.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ;;; org-pcomplete.el --- In-buffer completion code
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; John Wiegley <johnw at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.5
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Code:
  26. ;;;; Require other packages
  27. (eval-when-compile
  28. (require 'cl))
  29. (require 'org-macs)
  30. (require 'pcomplete)
  31. (declare-function org-split-string "org" (string &optional separators))
  32. (declare-function org-get-current-options "org-exp" ())
  33. (declare-function org-make-org-heading-search-string "org"
  34. (&optional string heading))
  35. (declare-function org-get-buffer-tags "org" ())
  36. (declare-function org-get-tags "org" ())
  37. (declare-function org-buffer-property-keys "org"
  38. (&optional include-specials include-defaults include-columns))
  39. (declare-function org-entry-properties "org" (&optional pom which specific))
  40. ;;;; Customization variables
  41. (defgroup org-complete nil
  42. "Outline-based notes management and organizer."
  43. :tag "Org"
  44. :group 'org)
  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. (cons "prop" nil))
  81. (t nil))))
  82. (defun org-command-at-point ()
  83. "Return the qualified name of the Org completion entity at point.
  84. When completing for #+STARTUP, for example, this function returns
  85. \"file-option/startup\"."
  86. (let ((thing (org-thing-at-point)))
  87. (cond
  88. ((string= "file-option" (car thing))
  89. (concat (car thing) "/" (downcase (cdr thing))))
  90. ((string= "block-option" (car thing))
  91. (concat (car thing) "/" (downcase (cdr thing))))
  92. (t
  93. (car thing)))))
  94. (defun org-parse-arguments ()
  95. "Parse whitespace separated arguments in the current region."
  96. (let ((begin (line-beginning-position))
  97. (end (line-end-position))
  98. begins args)
  99. (save-restriction
  100. (narrow-to-region begin end)
  101. (save-excursion
  102. (goto-char (point-min))
  103. (while (not (eobp))
  104. (skip-chars-forward " \t\n[")
  105. (setq begins (cons (point) begins))
  106. (skip-chars-forward "^ \t\n[")
  107. (setq args (cons (buffer-substring-no-properties
  108. (car begins) (point))
  109. args)))
  110. (cons (reverse args) (reverse begins))))))
  111. (defun org-pcomplete-initial ()
  112. "Calls the right completion function for first argument completions."
  113. (ignore
  114. (funcall (or (pcomplete-find-completion-function
  115. (car (org-thing-at-point)))
  116. pcomplete-default-completion-function))))
  117. (defvar org-additional-option-like-keywords)
  118. (defun pcomplete/org-mode/file-option ()
  119. "Complete against all valid file options."
  120. (require 'org-exp)
  121. (pcomplete-here
  122. (org-pcomplete-case-double
  123. (mapcar (lambda (x)
  124. (if (= ?: (aref x (1- (length x))))
  125. (concat x " ")
  126. x))
  127. (delq nil
  128. (pcomplete-uniqify-list
  129. (append
  130. (mapcar (lambda (x)
  131. (if (string-match "^#\\+\\([A-Z_]+:?\\)" x)
  132. (match-string 1 x)))
  133. (org-split-string (org-get-current-options) "\n"))
  134. org-additional-option-like-keywords)))))
  135. (substring pcomplete-stub 2)))
  136. (defvar org-startup-options)
  137. (defun pcomplete/org-mode/file-option/startup ()
  138. "Complete arguments for the #+STARTUP file option."
  139. (while (pcomplete-here
  140. (let ((opts (pcomplete-uniqify-list
  141. (mapcar 'car org-startup-options))))
  142. ;; Some options are mutually exclusive, and shouldn't be completed
  143. ;; against if certain other options have already been seen.
  144. (dolist (arg pcomplete-args)
  145. (cond
  146. ((string= arg "hidestars")
  147. (setq opts (delete "showstars" opts)))))
  148. opts))))
  149. (defun pcomplete/org-mode/file-option/bind ()
  150. "Complete arguments for the #+BIND file option, which are variable names"
  151. (let (vars)
  152. (mapatoms
  153. (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
  154. (pcomplete-here vars)))
  155. (defvar org-link-abbrev-alist-local)
  156. (defvar org-link-abbrev-alist)
  157. (defun pcomplete/org-mode/link ()
  158. "Complete against defined #+LINK patterns."
  159. (pcomplete-here
  160. (pcomplete-uniqify-list
  161. (copy-sequence
  162. (append (mapcar 'car org-link-abbrev-alist-local)
  163. (mapcar 'car org-link-abbrev-alist))))))
  164. (defvar org-entities)
  165. (defun pcomplete/org-mode/tex ()
  166. "Complete against TeX-style HTML entity names."
  167. (require 'org-entities)
  168. (while (pcomplete-here
  169. (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
  170. (substring pcomplete-stub 1))))
  171. (defvar org-todo-keywords-1)
  172. (defun pcomplete/org-mode/todo ()
  173. "Complete against known TODO keywords."
  174. (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
  175. (defvar org-todo-line-regexp)
  176. (defun pcomplete/org-mode/searchhead ()
  177. "Complete against all headings.
  178. This needs more work, to handle headings with lots of spaces in them."
  179. (while
  180. (pcomplete-here
  181. (save-excursion
  182. (goto-char (point-min))
  183. (let (tbl)
  184. (while (re-search-forward org-todo-line-regexp nil t)
  185. (push (org-make-org-heading-search-string
  186. (match-string-no-properties 3) t)
  187. tbl))
  188. (pcomplete-uniqify-list tbl)))
  189. (substring pcomplete-stub 1))))
  190. (defvar org-tag-alist)
  191. (defun pcomplete/org-mode/tag ()
  192. "Complete a tag name. Omit tags already set."
  193. (while (pcomplete-here
  194. (mapcar (lambda (x)
  195. (concat x ":"))
  196. (let ((lst (pcomplete-uniqify-list
  197. (or (remove
  198. nil
  199. (mapcar (lambda (x)
  200. (and (stringp (car x)) (car x)))
  201. org-tag-alist))
  202. (mapcar 'car (org-get-buffer-tags))))))
  203. (dolist (tag (org-get-tags))
  204. (setq lst (delete tag lst)))
  205. lst))
  206. (and (string-match ".*:" pcomplete-stub)
  207. (substring pcomplete-stub (match-end 0))))))
  208. (defun pcomplete/org-mode/prop ()
  209. "Complete a property name. Omit properties already set."
  210. (pcomplete-here
  211. (mapcar (lambda (x)
  212. (concat x ": "))
  213. (let ((lst (pcomplete-uniqify-list
  214. (copy-sequence
  215. (org-buffer-property-keys nil t t)))))
  216. (dolist (prop (org-entry-properties))
  217. (setq lst (delete (car prop) lst)))
  218. lst))
  219. (substring pcomplete-stub 1)))
  220. (defun pcomplete/org-mode/block-option/src ()
  221. "Complete the arguments of a begin_src block.
  222. Complete a language in the first field, the header arguments and switches."
  223. (pcomplete-here
  224. (mapcar
  225. (lambda(x) (symbol-name (nth 3 x)))
  226. (cdr (car (cdr (memq :key-type (plist-get
  227. (symbol-plist
  228. 'org-babel-load-languages)
  229. 'custom-type)))))))
  230. (while (pcomplete-here
  231. '("-n" "-r" "-l"
  232. ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
  233. ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
  234. ":session" ":shebang" ":tangle" ":var"))))
  235. (defun pcomplete/org-mode/block-option/clocktable ()
  236. "Complete keywords in a clocktable line"
  237. (while (pcomplete-here '(":maxlevel" ":scope"
  238. ":tstart" ":tend" ":block" ":step"
  239. ":stepskip0" ":fileskip0"
  240. ":emphasize" ":link" ":narrow" ":indent"
  241. ":tcolumns" ":level" ":compact" ":timestamp"
  242. ":formula" ":formatter"))))
  243. (defun org-pcomplete-case-double (list)
  244. "Return list with both upcase and downcase version of all strings in LIST."
  245. (let (e res)
  246. (while (setq e (pop list))
  247. (setq res (cons (downcase e) (cons (upcase e) res))))
  248. (nreverse res)))
  249. ;;;; Finish up
  250. (provide 'org-pcomplete)
  251. ;; arch-tag:
  252. ;;; org-pcomplete.el ends here