]> code.delx.au - gnu-emacs-elpa/blob - company-xcode.el
Define groups for back-ends; remove explicit :group declarations
[gnu-emacs-elpa] / company-xcode.el
1 ;;; company-xcode.el --- company-mode completion back-end for Xcode projects
2
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
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 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (eval-when-compile (require 'cl))
30
31 (defgroup company-xcode nil
32 "Completion back-end for Xcode projects."
33 :group 'company)
34
35 (defcustom company-xcode-xcodeindex-executable (executable-find "xcodeindex")
36 "Location of xcodeindex executable."
37 :type 'file)
38
39 (defvar company-xcode-tags nil)
40
41 (defun company-xcode-reset ()
42 "Reset the cached tags."
43 (interactive)
44 (setq company-xcode-tags nil))
45
46 (defcustom company-xcode-types
47 '("Class" "Constant" "Enum" "Macro" "Modeled Class" "Structure"
48 "Type" "Union" "Function")
49 "The types of symbols offered by `company-xcode'.
50 No context-enabled completion is available. Types like methods will be
51 offered regardless of whether the class supports them. The defaults should be
52 valid in most contexts."
53 :set (lambda (variable value)
54 (set variable value)
55 (company-xcode-reset))
56 :type '(set (const "Category") (const "Class") (const "Class Method")
57 (const "Class Variable") (const "Constant") (const "Enum")
58 (const "Field") (const "Instance Method")
59 (const "Instance Variable") (const "Macro")
60 (const "Modeled Class") (const "Modeled Method")
61 (const "Modeled Property") (const "Property") (const "Protocol")
62 (const "Structure") (const "Type") (const "Union")
63 (const "Variable") (const "Function")))
64
65 (defvar company-xcode-project 'unknown)
66 (make-variable-buffer-local 'company-xcode-project)
67
68 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69
70 (defun company-xcode-fetch (project-bundle)
71 (setq project-bundle (directory-file-name project-bundle))
72 (message "Retrieving dump from %s..." project-bundle)
73 (with-temp-buffer
74 (let ((default-directory (file-name-directory project-bundle)))
75 (call-process company-xcode-xcodeindex-executable nil (current-buffer)
76 nil "dump" "-project"
77 (file-name-nondirectory project-bundle) "-quiet")
78 (goto-char (point-min))
79 (let ((regexp (concat "^\\([^\t\n]*\\)\t[^\t\n]*\t"
80 (regexp-opt company-xcode-types)
81 "\t[^\t\n]*\t[^\t\n]*"))
82 candidates)
83 (while (re-search-forward regexp nil t)
84 (add-to-list 'candidates (match-string 1)))
85 (message "Retrieving dump from %s...done" project-bundle)
86 candidates))))
87
88 (defun company-xcode-find-project ()
89 (let ((dir (if buffer-file-name
90 (file-name-directory buffer-file-name)
91 (expand-file-name default-directory)))
92 (prev-dir nil)
93 file)
94 (while (not (or file (equal dir prev-dir)))
95 (setq file (car (directory-files dir t ".xcodeproj\\'" t))
96 prev-dir dir
97 dir (file-name-directory (directory-file-name dir))))
98 file))
99
100 (defun company-xcode-tags ()
101 (when (eq company-xcode-project 'unknown)
102 (setq company-xcode-project (company-xcode-find-project)))
103 (when company-xcode-project
104 (cdr (or (assoc company-xcode-project company-xcode-tags)
105 (car (push (cons company-xcode-project
106 (company-xcode-fetch company-xcode-project))
107 company-xcode-tags))))))
108 ;;;###autoload
109 (defun company-xcode (command &optional arg &rest ignored)
110 "`company-mode' completion back-end for Xcode projects."
111 (interactive (list 'interactive))
112 (case command
113 (interactive (company-begin-backend 'company-xcode))
114 (prefix (and company-xcode-xcodeindex-executable
115 (company-xcode-tags)
116 (not (company-in-string-or-comment))
117 (or (company-grab-symbol) 'stop)))
118 (candidates (let ((completion-ignore-case nil))
119 (company-xcode-tags)
120 (all-completions arg (company-xcode-tags))))))
121
122
123 (provide 'company-xcode)
124 ;;; company-xcode.el ends here