forked from senny/emacs-xcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emacs-xcode.el
120 lines (107 loc) · 4.19 KB
/
emacs-xcode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
;; emacs-xcode.el --- an interface to the XCode IDE.
;;
;; Copyright (C) 2009 Yves Senn <[email protected]>
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Contributors
;;
;; - Yves Senn <yves senn * gmx ch>
;; - Peter Jones <[email protected]>
;;
;;; Conventions
;;
;; Conventions used in this file: Name internal variables and functions
;; "xcode--<descriptive-name>", and name xcode command invocations
;; "xcode/command-name", like xcode/build.
;;* emacs-xcode
(defvar *xcode-project-root* nil)
(defun xcode--project-root ()
(or *xcode-project-root*
(setq *xcode-project-root* (xcode--project-lookup))))
(defun xcode--project-lookup (&optional current-directory)
(when (null current-directory) (setq current-directory default-directory))
(cond ((xcode--project-for-directory (directory-files current-directory)) (expand-file-name current-directory))
((equal (expand-file-name current-directory) "/") nil)
(t (xcode--project-lookup (concat (file-name-as-directory current-directory) "..")))))
(defun xcode--project-for-directory (files)
(let ((project-file nil))
(dolist (file files project-file)
(if (> (length file) 10)
(when (string-equal (substring file -10) ".xcodeproj") (setq project-file file))))))
(defun xcode--project-command (options)
(concat "cd " (xcode--project-root) "; " options))
(defun xcode/build-compile ()
(interactive)
(compile (xcode--project-command (xcode--build-command))))
(defun xcode/build-list-sdks ()
(interactive)
(message (shell-command-to-string (xcode--project-command "xcodebuild -showsdks"))))
(defun xcode--build-command (&optional target configuration sdk)
(let ((build-command "xcodebuild"))
(if (not target)
(setq build-command (concat build-command " -activetarget"))
(setq build-command (concat build-command " -target " target)))
(if (not configuration)
(setq build-command (concat build-command " -activeconfiguration"))
(setq build-command (concat build-command " -configuration " configuration)))
(when sdk (setq build-command (concat build-command " -sdk " sdk)))
build-command))
(defun xcode/toggle-header-and-source nil
"Toggle between source and header files"
(interactive)
(let ((fname buffer-file-name) oname)
(setq oname
(cond
((string-match "\\.h$" fname) (replace-match ".m" nil nil fname))
((string-match "\\.m$" fname) (replace-match ".h" nil nil fname))
(t fname)))
(find-file oname)))
(defun bh-compile ()
(interactive)
(let ((df (directory-files "."))
(has-proj-file nil)
)
(while (and df (not has-proj-file))
(let ((fn (car df)))
(if (> (length fn) 10)
(if (string-equal (substring fn -10) ".xcodeproj")
(setq has-proj-file t)
)
)
)
(setq df (cdr df))
)
(if has-proj-file
(compile "xcodebuild -configuration Debug")
(compile "make")
)
)
)
(defun bh-choose-header-mode ()
(interactive)
(if (string-equal (substring (buffer-file-name) -2) ".h")
(progn
;; OK, we got a .h file, if a .m file exists we'll assume it's
;; an objective c file. Otherwise, we'll look for a .cpp file.
(let ((dot-m-file (concat (substring (buffer-file-name) 0 -1) "m"))
(dot-cpp-file (concat (substring (buffer-file-name) 0 -1) "cpp")))
(if (file-exists-p dot-m-file)
(progn
(objc-mode)
)
(if (file-exists-p dot-cpp-file)
(c++-mode)))))))
(add-hook 'find-file-hook 'bh-choose-header-mode)
(provide 'xcode)