org-complete.el 8.4 KB

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