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