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