]> code.delx.au - gnu-emacs-elpa/blob - packages/company/company-xcode.el
Remove version numbers in packages/ directory
[gnu-emacs-elpa] / packages / company / company-xcode.el
1 ;;; company-xcode.el --- a company-mode completion back-end for Xcode projects
2
3 ;; Copyright (C) 2009, 2010 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 ;;; Code:
23
24 (require 'company)
25 (eval-when-compile (require 'cl))
26
27 (defcustom company-xcode-xcodeindex-executable (executable-find "xcodeindex")
28 "*Location of xcodeindex executable"
29 :group 'company-xcode
30 :type 'file)
31
32 (defvar company-xcode-tags nil)
33
34 (defun company-xcode-reset ()
35 "Reset the cached tags."
36 (interactive)
37 (setq company-xcode-tags nil))
38
39 (defcustom company-xcode-types
40 '("Class" "Constant" "Enum" "Macro" "Modeled Class" "Structure"
41 "Type" "Union" "Function")
42 "*The types of symbols offered by `company-xcode'
43 No context-enabled completion is available. Types like methods will be
44 offered regardless of whether the class supports them. The defaults should be
45 valid in most contexts."
46 :set (lambda (variable value)
47 (set variable value)
48 (company-xcode-reset))
49 :group 'company-xcode
50 :type '(set (const "Category") (const "Class") (const "Class Method")
51 (const "Class Variable") (const "Constant") (const "Enum")
52 (const "Field") (const "Instance Method")
53 (const "Instance Variable") (const "Macro")
54 (const "Modeled Class") (const "Modeled Method")
55 (const "Modeled Property") (const "Property") (const "Protocol")
56 (const "Structure") (const "Type") (const "Union")
57 (const "Variable") (const "Function")))
58
59 (defvar company-xcode-project 'unknown)
60 (make-variable-buffer-local 'company-xcode-project)
61
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63
64 (defun company-xcode-fetch (project-bundle)
65 (setq project-bundle (directory-file-name project-bundle))
66 (message "Retrieving dump from %s..." project-bundle)
67 (with-temp-buffer
68 (let ((default-directory (file-name-directory project-bundle)))
69 (call-process company-xcode-xcodeindex-executable nil (current-buffer)
70 nil "dump" "-project"
71 (file-name-nondirectory project-bundle) "-quiet")
72 (goto-char (point-min))
73 (let ((regexp (concat "^\\([^\t\n]*\\)\t[^\t\n]*\t"
74 (regexp-opt company-xcode-types)
75 "\t[^\t\n]*\t[^\t\n]*"))
76 candidates)
77 (while (re-search-forward regexp nil t)
78 (add-to-list 'candidates (match-string 1)))
79 (message "Retrieving dump from %s...done" project-bundle)
80 candidates))))
81
82 (defun company-xcode-find-project ()
83 (let ((dir (if buffer-file-name
84 (file-name-directory buffer-file-name)
85 (expand-file-name default-directory)))
86 (prev-dir nil)
87 file)
88 (while (not (or file (equal dir prev-dir)))
89 (setq file (car (directory-files dir t ".xcodeproj\\'" t))
90 prev-dir dir
91 dir (file-name-directory (directory-file-name dir))))
92 file))
93
94 (defun company-xcode-tags ()
95 (when (eq company-xcode-project 'unknown)
96 (setq company-xcode-project (company-xcode-find-project)))
97 (when company-xcode-project
98 (cdr (or (assoc company-xcode-project company-xcode-tags)
99 (car (push (cons company-xcode-project
100 (company-xcode-fetch company-xcode-project))
101 company-xcode-tags))))))
102 ;;;###autoload
103 (defun company-xcode (command &optional arg &rest ignored)
104 "A `company-mode' completion back-end for Xcode projects."
105 (interactive (list 'interactive))
106 (case command
107 ('interactive (company-begin-backend 'company-xcode))
108 ('prefix (and company-xcode-xcodeindex-executable
109 (company-xcode-tags)
110 (not (company-in-string-or-comment))
111 (or (company-grab-symbol) 'stop)))
112 ('candidates (let ((completion-ignore-case nil))
113 (company-xcode-tags)
114 (all-completions arg (company-xcode-tags))))))
115
116
117 (provide 'company-xcode)
118 ;;; company-xcode.el ends here