-
Notifications
You must be signed in to change notification settings - Fork 1
/
objc-c-mode.el
284 lines (238 loc) · 8.55 KB
/
objc-c-mode.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
;;; objc-c-mode.el --- improvements for the XEmacs Objective-C mode
;; Author: Michael Weber <[email protected]>
;; Version: 20020527
;; Keywords: Objective-C, ObjC
;; Depends: cc-mode, font-lock
;; Tested with: XEmacs 21.4 (patch 6) "Common Lisp" [Lucid]
;;; Documentation:
;;; ==============
;;; To use this style for your Objective-C buffers, just add
;;; (require 'objc-c-mode)
;;;
;;; to your XEmacs dot-file. It creates a new style `objc',
;;; which is set as default for all objc-mode buffers.
;;;
;;; To further customize, try this:
;;; (defconst my-c-style
;;; '("objc"
;;; (c-indent-comments-syntactically-p . t)
;;; (c-comment-only-line-offset . 0)
;;; ;;; whatever else here...
;;;
;;; (c-cleanup-list . (brace-else-brace
;;; brace-elseif-brace
;;; empty-defun-braces
;;; defun-close-semi
;;; compact-empty-funcall
;;; )))
;;; "My C Programming style")
;;;
;;; (defun my-c-mode-common-hook ()
;;; (c-add-style "PERSONAL" my-c-style t)
;;; (setq comment-column 40
;;; tab-width 4
;;; c-basic-offset tab-width)
;;;
;;; (c-toggle-auto-state 1))
;;;
;;; ;; activate customizations for all cc-mode derived modes
;;; (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
;;; If you want to specifically change the "objc" style,
;;; probably use something like:
;;; (defconst my-objc-style
;;; '(("objc"
;;; (...)
;;; "My ObjC style")))
;;; (defun my-objc-mode-hook ()
;;; (c-add-style "objc" my-objc-style))
;;; (add-hook 'objc-mode-hook 'my-objc-mode-hook)
;;; Code:
;;; =====
(require 'cc-mode)
;;; Default values. Do not change them here, instead change them like
;;; any other style variable in your customized style.
;;; NOTE: These numbers are ignored anyway, below they are set based on
;;; `c-basic-offset' which is usually what one wants anyway...
(defcustom-c-stylevar objc-method-arg-min-delta-to-bracket 2
"*Minimun number of chars to the opening bracket.
Consider this ObjC snippet:
[foo blahBlah: fred
|<-x->|barBaz: barney
If `x' is less than this number then `c-lineup-ObjC-method-call-colons'
will defer the indentation decision to the next function. By default
this is `c-lineup-ObjC-method-call', which would align it like:
[foo blahBlahBlah: fred
thisIsTooDamnLong: barney
This behaviour can be overridden by customizing the indentation of
`objc-method-call-cont' in the \"objc\" style."
:group 'c)
(defcustom-c-stylevar objc-method-arg-unfinished-offset 4
"*Offset relative to bracket if first selector is on a new line.
[aaaaaaaaa
|<-x->|bbbbbbb: cccccc
ddddd: eeee];"
:group 'c)
(defcustom-c-stylevar objc-method-parameter-offset 4
"*Offset for selector parameter on a new line (relative to first selector.
[aaaaaaa bbbbbbbbbb:
|<-x->|cccccccc
ddd: eeee
ffff: ggg];"
:group 'c)
;; These are the real defaults (set here, because otherwise the
;; indentation whines about them not being defined... *shrug*
(setq c-style-variables (append
'(objc-method-arg-min-delta-to-bracket
objc-method-arg-unfinished-offset
objc-method-parameter-offset)
c-style-variables)
c-offsets-alist (append
'((objc-method-arg-min-delta-to-bracket . *)
(objc-method-arg-unfinished-offset . +)
(objc-method-parameter-offset . +))
c-offsets-alist))
(defun c-lineup-ObjC-method-call-colons (langelem)
"Line up the colons of selector args with the first selector.
If no decision can be made return NIL, so that other lineup methods can be
tried. This is typically chained with `c-lineup-ObjC-method-call'."
(save-excursion
(catch 'no-idea
(let* ((method-arg-len (progn
(back-to-indentation)
(if (search-forward ":" (c-point 'eol) 'move)
(- (point) (c-point 'boi))
; no complete argument to indent yet
(throw 'no-idea nil))))
(extra (save-excursion
; indent parameter to argument if needed
(back-to-indentation)
(c-backward-syntactic-ws (cdr langelem))
(if (eq ?: (char-before))
(c-get-offset '(objc-method-parameter-offset . nil))
0)))
(open-bracket-col (c-langelem-col langelem))
(arg-ralign-colon-ofs (progn
(forward-char) ; skip over '['
; skip over object/class name
; and first argument
(c-forward-sexp 2)
(if (search-forward ":" (c-point 'eol) 'move)
(- (current-column) open-bracket-col
method-arg-len extra)
; previous arg has no param
(c-get-offset '(objc-method-arg-unfinished-offset . nil))))))
(if (>= arg-ralign-colon-ofs
(c-get-offset '(objc-method-arg-min-delta-to-bracket . nil)))
(+ arg-ralign-colon-ofs extra)
(throw 'no-idea nil)
)))))
;;; create and add style
(c-add-style "objc"
'("gnu"
(c-offsets-alist . ((objc-method-call-cont .
(c-lineup-ObjC-method-call-colons
c-lineup-ObjC-method-call
+))
))
))
(setq c-default-style (cons '(objc-mode . "objc")
c-default-style))
;;
;; Now for the font-locking part... :)
;;
(require 'font-lock)
(put 'objc-mode 'font-lock-defaults
'((objc-font-lock-keywords
objc-font-lock-keywords-1
objc-font-lock-keywords-2
objc-font-lock-keywords-3)
;; TODO: ?_ is not a good idea in ObjC specifiers...
nil nil ((?_ . "w")) beginning-of-defun))
(let* ((ctoken "\\(?:\\sw\\|\\s_\\|[:~*&]\\)+")
(objc-keywords "YES\\|NO\\|[Nn]il\\|self\\|super")
(objc-type-types "id\\|Class\\|SEL\\|IMP\\|BOOL")
(addon-objc-font-lock-keywords-1
(list ;; nothing to do here...
))
(addon-objc-font-lock-keywords-2
(append addon-objc-font-lock-keywords-1
(list
;; first part of selector (in a declaration)
(list (concat "^[+-][ \t]*"
"\\((" ctoken "[ \t]*[*]*)\\)?[ \t]*"
"\\(\\sw+\\)"
)
'(2 font-lock-function-name-face))
;; part of a selector
'("\\sw*:" 0 font-lock-function-name-face t)
;; Fontify all type specifiers.
(cons (concat "\\<\\(" objc-type-types "\\)\\>")
'font-lock-type-face)
;; Fontify all builtin keywords
(cons (concat "\\<\\(" objc-keywords "\\)\\>")
'font-lock-keyword-face)
;; Fontify specific keywords
(cons (concat "^" (regexp-opt (list
"@implementation" "@interface"
"@protocol" "@end" "@public"
"@private" "@protected"
) t))
'font-lock-keyword-face)
(list (concat "\\("
(regexp-opt (list
"@class" "@defs" "@encode" "@selector"
"@protocol"
))
"\\)[ \t]*(")
1 'font-lock-keyword-face)
'("^#[ \t]*import[ \t]+\\(<[^>\"\n]+>\\)"
1 font-lock-string-face)
)
))
(addon-objc-font-lock-keywords-3
(append addon-objc-font-lock-keywords-2
(list
;; get argument-less selectors' highlighting right
;; [[foo _bar_] _baz_] -> bar, baz are highlighted
(cons (concat "\\(\\sw+\\)" "[ \t]*" "[]]")
'(1 (let ((non-ws-before-match (char-before
(save-excursion
(goto-char (match-beginning 1))
;; expensive!
(c-backward-syntactic-ws (c-point 'bol))
))))
(unless (or (eq ?: non-ws-before-match)
(eq ?\[ non-ws-before-match))
'font-lock-function-name-face))))
(cons (concat "\\<\\(" objc-type-types "\\)\\>"
"\\([ \t*&]+\\sw+\\>\\)*")
;; taken verbatim from font-lock.el
'(font-lock-match-c++-style-declaration-item-and-skip-to-next
(goto-char (or (match-beginning 8) (match-end 1)))
(goto-char (match-end 1))
(1 (if (match-beginning 4)
font-lock-function-name-face
font-lock-variable-name-face))))
(cons (concat "\\<"
(regexp-opt (list
"NS_DURING" "NS_HANDLER" "NS_ENDHANDLER"
"RECREATE_AUTORELEASE_POOL"
"CREATE_AUTORELEASE_POOL"
"ASSIGNCOPY" "ASSIGN" "RETAIN"
"DESTROY" "AUTORELEASE" "RELEASE"
) t)
"\\>")
'font-lock-preprocessor-face)
)))
)
(setq objc-font-lock-keywords-1 (append c-font-lock-keywords-1
addon-objc-font-lock-keywords-1)
objc-font-lock-keywords-2 (append c-font-lock-keywords-2
addon-objc-font-lock-keywords-2)
objc-font-lock-keywords-3 (append c-font-lock-keywords-3
addon-objc-font-lock-keywords-3)
))
(defvar objc-font-lock-keywords objc-font-lock-keywords-1
"Default expressions to highlight in ObjC mode.")
(provide 'objc-c-mode)
;;; objc-c-mode.el ends here