]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-defs.el
(makefile-mode-abbrev-table): New variable.
[gnu-emacs] / lisp / progmodes / cc-defs.el
1 ;;; cc-defs.el --- compile time definitions for CC Mode
2
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
4
5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
13
14 ;; This file is part of GNU Emacs.
15
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
20
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
30
31 ;; Get all the necessary compile time definitions.
32 (require 'custom)
33 (require 'derived) ;only necessary in Emacs 20
34
35 ;; cc-mode-19.el contains compatibility macros that should be compiled
36 ;; in if needed.
37 (if (or (not (fboundp 'functionp))
38 (not (condition-case nil
39 (progn (char-before) t)
40 (error nil)))
41 (not (condition-case nil
42 (progn (char-after) t)
43 (error nil)))
44 (not (fboundp 'when))
45 (not (fboundp 'unless)))
46 (require 'cc-mode-19))
47
48 \f
49 (defmacro c-point (position)
50 ;; Returns the value of point at certain commonly referenced POSITIONs.
51 ;; POSITION can be one of the following symbols:
52 ;;
53 ;; bol -- beginning of line
54 ;; eol -- end of line
55 ;; bod -- beginning of defun
56 ;; eod -- end of defun
57 ;; boi -- back to indentation
58 ;; ionl -- indentation of next line
59 ;; iopl -- indentation of previous line
60 ;; bonl -- beginning of next line
61 ;; bopl -- beginning of previous line
62 ;;
63 ;; This function does not modify point or mark.
64 `(save-excursion
65 ,(if (and (eq (car-safe position) 'quote)
66 (symbolp (eval position)))
67 (let ((position (eval position)))
68 (cond
69 ((eq position 'bol) `(beginning-of-line))
70 ((eq position 'eol) `(end-of-line))
71 ((eq position 'boi) `(back-to-indentation))
72 ((eq position 'bonl) `(forward-line 1))
73 ((eq position 'bopl) `(forward-line -1))
74 ((eq position 'bod) `(c-beginning-of-defun-1))
75 ((eq position 'eod) `(c-end-of-defun-1))
76 ((eq position 'iopl) `(progn
77 (forward-line -1)
78 (back-to-indentation)))
79 ((eq position 'ionl) `(progn
80 (forward-line 1)
81 (back-to-indentation)))
82 (t (error "unknown buffer position requested: %s" position))))
83 ;;(message "c-point long expansion")
84 `(let ((position ,position))
85 (cond
86 ((eq position 'bol) (beginning-of-line))
87 ((eq position 'eol) (end-of-line))
88 ((eq position 'boi) (back-to-indentation))
89 ((eq position 'bonl) (forward-line 1))
90 ((eq position 'bopl) (forward-line -1))
91 ((eq position 'bod) (c-beginning-of-defun-1))
92 ((eq position 'eod) (c-end-of-defun-1))
93 ((eq position 'iopl) (progn
94 (forward-line -1)
95 (back-to-indentation)))
96 ((eq position 'ionl) (progn
97 (forward-line 1)
98 (back-to-indentation)))
99 (t (error "unknown buffer position requested: %s" position)))))
100 (point)))
101
102 \f
103 (defmacro c-safe (&rest body)
104 ;; safely execute BODY, return nil if an error occurred
105 `(condition-case nil
106 (progn ,@body)
107 (error nil)))
108
109 (defsubst c-beginning-of-defun-1 ()
110 ;; Wrapper around beginning-of-defun.
111 ;;
112 ;; NOTE: This function should contain the only explicit use of
113 ;; beginning-of-defun in CC Mode. Eventually something better than
114 ;; b-o-d will be available and this should be the only place the
115 ;; code needs to change. Everything else should use
116 ;; (c-beginning-of-defun-1)
117 (if (and (fboundp 'buffer-syntactic-context-depth)
118 c-enable-xemacs-performance-kludge-p)
119 ;; XEmacs only. This can improve the performance of
120 ;; c-parse-state to between 3 and 60 times faster when
121 ;; braces are hung. It can also degrade performance by
122 ;; about as much when braces are not hung.
123 (let (pos)
124 (while (not pos)
125 (save-restriction
126 (widen)
127 (setq pos (scan-lists (point) -1
128 (buffer-syntactic-context-depth)
129 nil t)))
130 (cond
131 ((bobp) (setq pos (point-min)))
132 ((not pos)
133 (let ((distance (skip-chars-backward "^{")))
134 ;; unbalanced parenthesis, while illegal C code,
135 ;; shouldn't cause an infloop! See unbal.c
136 (when (zerop distance)
137 ;; Punt!
138 (beginning-of-defun)
139 (setq pos (point)))))
140 ((= pos 0))
141 ((not (eq (char-after pos) ?{))
142 (goto-char pos)
143 (setq pos nil))
144 ))
145 (goto-char pos))
146 ;; Emacs, which doesn't have buffer-syntactic-context-depth
147 (beginning-of-defun))
148 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
149 ;; open brace.
150 (and defun-prompt-regexp
151 (looking-at defun-prompt-regexp)
152 (goto-char (match-end 0))))
153
154 (defsubst c-end-of-defun-1 ()
155 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
156 (while (and (c-safe (down-list 1) t)
157 (not (eq (char-before) ?{)))
158 ;; skip down into the next defun-block
159 (forward-char -1)
160 (c-forward-sexp))
161 (c-beginning-of-defun-1)
162 (c-forward-sexp))
163
164 (defmacro c-forward-sexp (&optional arg)
165 ;; like forward-sexp except
166 ;; 1. this is much stripped down from the XEmacs version
167 ;; 2. this cannot be used as a command, so we're insulated from
168 ;; XEmacs' losing efforts to make forward-sexp more user
169 ;; friendly
170 ;; 3. Preserves the semantics most of CC Mode is based on
171 (or arg (setq arg 1))
172 `(goto-char (or (scan-sexps (point) ,arg)
173 ,(if (numberp arg)
174 (if (> arg 0) `(point-max) `(point-min))
175 `(if (> ,arg 0) (point-max) (point-min))))))
176
177 (defmacro c-backward-sexp (&optional arg)
178 ;; See c-forward-sexp and reverse directions
179 (or arg (setq arg 1))
180 `(c-forward-sexp ,(if (numberp arg) (- arg) `(- ,arg))))
181
182 (defsubst c-beginning-of-macro (&optional lim)
183 ;; Go to the beginning of a cpp macro definition. Leaves point at
184 ;; the beginning of the macro and returns t if in a cpp macro
185 ;; definition, otherwise returns nil and leaves point unchanged.
186 ;; `lim' is currently ignored, but the interface requires it.
187 (let ((here (point)))
188 (beginning-of-line)
189 (while (eq (char-before (1- (point))) ?\\)
190 (forward-line -1))
191 (back-to-indentation)
192 (if (and (<= (point) here)
193 (eq (char-after) ?#))
194 t
195 (goto-char here)
196 nil)))
197
198 (defsubst c-forward-comment (count)
199 ;; Insulation from various idiosyncrasies in implementations of
200 ;; `forward-comment'. Note: Some emacsen considers incorrectly that
201 ;; any line comment ending with a backslash continues to the next
202 ;; line. I can't think of any way to work around that in a reliable
203 ;; way without changing the buffer though. Suggestions welcome. ;)
204 (let ((here (point)))
205 (if (>= count 0)
206 (when (forward-comment count)
207 ;; Emacs includes the ending newline in a b-style
208 ;; (c++) comment, but XEmacs don't. We depend on the
209 ;; Emacs behavior (which also is symmetric).
210 (if (and (eolp) (nth 7 (parse-partial-sexp here (point))))
211 (condition-case nil (forward-char 1)))
212 t)
213 ;; When we got newline terminated comments,
214 ;; forward-comment in all supported emacsen so far will
215 ;; stop at eol of each line not ending with a comment when
216 ;; moving backwards. The following corrects for it when
217 ;; count is -1. The other common case, when count is
218 ;; large and negative, works regardless. It's too much
219 ;; work to correct for the rest of the cases.
220 (skip-chars-backward " \t\n\r\f")
221 (if (bobp)
222 ;; Some emacsen return t when moving backwards at bob.
223 nil
224 (re-search-forward "[\n\r]" here t)
225 (if (forward-comment count)
226 (if (eolp) (forward-comment -1) t))))))
227
228 (defmacro c-add-syntax (symbol &optional relpos)
229 ;; a simple macro to append the syntax in symbol to the syntax list.
230 ;; try to increase performance by using this macro
231 `(setq syntax (cons (cons ,symbol ,relpos) syntax)))
232
233 (defmacro c-add-class-syntax (symbol classkey)
234 ;; The inclass and class-close syntactic symbols are added in
235 ;; several places and some work is needed to fix everything.
236 ;; Therefore it's collected here.
237 `(save-restriction
238 (widen)
239 (let ((symbol ,symbol)
240 (classkey ,classkey))
241 (goto-char (aref classkey 1))
242 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
243 (c-add-syntax symbol (point))
244 (c-add-syntax symbol (aref classkey 0))
245 (if (and c-inexpr-class-key (c-looking-at-inexpr-block))
246 (c-add-syntax 'inexpr-class))))))
247
248 (defmacro c-auto-newline ()
249 ;; if auto-newline feature is turned on, insert a newline character
250 ;; and return t, otherwise return nil.
251 `(and c-auto-newline
252 (not (c-in-literal))
253 (not (newline))))
254
255 (defsubst c-intersect-lists (list alist)
256 ;; return the element of ALIST that matches the first element found
257 ;; in LIST. Uses assq.
258 (let (match)
259 (while (and list
260 (not (setq match (assq (car list) alist))))
261 (setq list (cdr list)))
262 match))
263
264 (defsubst c-lookup-lists (list alist1 alist2)
265 ;; first, find the first entry from LIST that is present in ALIST1,
266 ;; then find the entry in ALIST2 for that entry.
267 (assq (car (c-intersect-lists list alist1)) alist2))
268
269 (defsubst c-langelem-col (langelem &optional preserve-point)
270 ;; convenience routine to return the column of langelem's relpos.
271 ;; Leaves point at the relpos unless preserve-point is non-nil.
272 (let ((here (point)))
273 (goto-char (cdr langelem))
274 (prog1 (current-column)
275 (if preserve-point
276 (goto-char here))
277 )))
278
279 (defmacro c-update-modeline ()
280 ;; set the c-auto-hungry-string for the correct designation on the modeline
281 `(progn
282 (setq c-auto-hungry-string
283 (if c-auto-newline
284 (if c-hungry-delete-key "/ah" "/a")
285 (if c-hungry-delete-key "/h" nil)))
286 (force-mode-line-update)))
287
288 (defsubst c-keep-region-active ()
289 ;; Do whatever is necessary to keep the region active in XEmacs.
290 ;; Ignore byte-compiler warnings you might see. This is not needed
291 ;; for Emacs.
292 (and (boundp 'zmacs-region-stays)
293 (setq zmacs-region-stays t)))
294
295 (defsubst c-region-is-active-p ()
296 ;; Return t when the region is active. The determination of region
297 ;; activeness is different in both Emacs and XEmacs.
298 (cond
299 ;; XEmacs
300 ((and (fboundp 'region-active-p)
301 (boundp 'zmacs-regions)
302 zmacs-regions)
303 (region-active-p))
304 ;; Emacs
305 ((boundp 'mark-active) mark-active)
306 ;; fallback; shouldn't get here
307 (t (mark t))))
308
309 (defsubst c-major-mode-is (mode)
310 (eq (derived-mode-class major-mode) mode))
311
312 (defmacro c-with-syntax-table (table &rest code)
313 ;; Temporarily switches to the specified syntax table in a failsafe
314 ;; way to execute code.
315 `(let ((c-with-syntax-table-orig-table (syntax-table)))
316 (unwind-protect
317 (progn
318 (set-syntax-table ,table)
319 ,@code)
320 (set-syntax-table c-with-syntax-table-orig-table))))
321 (put 'c-with-syntax-table 'lisp-indent-function 1)
322
323 \f
324 (provide 'cc-defs)
325 ;;; cc-defs.el ends here