comparison lisp/progmodes/cc-defs.el @ 26817:03befb219d03

Installed version 5.26
author Gerd Moellmann <gerd@gnu.org>
date Sun, 12 Dec 1999 18:24:19 +0000
parents 5b0864259a4b
children 3393922ea102
comparison
equal deleted inserted replaced
26816:e719053e967a 26817:03befb219d03
1 ;;; cc-defs.el --- compile time definitions for CC Mode 1 ;;; cc-defs.el --- compile time definitions for CC Mode
2 2
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc. 3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
4 4
5 ;; Authors: 1998 Barry A. Warsaw and Martin Stjernholm 5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw 6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen 7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman 8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org 9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el) 10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
26 ;; You should have received a copy of the GNU General Public License 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 27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA. 29 ;; Boston, MA 02111-1307, USA.
30 30
31
32 ;; Get all the necessary compile time definitions. 31 ;; Get all the necessary compile time definitions.
33 (require 'custom) 32 (require 'custom)
34 (require 'cc-menus)
35 (require 'derived) ;only necessary in Emacs 20 33 (require 'derived) ;only necessary in Emacs 20
36 34
37 ;; cc-mode-19.el contains compatibility macros that should be compiled 35 ;; cc-mode-19.el contains compatibility macros that should be compiled
38 ;; in if needed. 36 ;; in if needed.
39 (if (or (not (fboundp 'functionp)) 37 (if (or (not (fboundp 'functionp))
46 (not (fboundp 'when)) 44 (not (fboundp 'when))
47 (not (fboundp 'unless))) 45 (not (fboundp 'unless)))
48 (require 'cc-mode-19)) 46 (require 'cc-mode-19))
49 47
50 48
51 (defsubst c-point (position) 49 (defmacro c-point (position)
52 ;; Returns the value of point at certain commonly referenced POSITIONs. 50 ;; Returns the value of point at certain commonly referenced POSITIONs.
53 ;; POSITION can be one of the following symbols: 51 ;; POSITION can be one of the following symbols:
54 ;; 52 ;;
55 ;; bol -- beginning of line 53 ;; bol -- beginning of line
56 ;; eol -- end of line 54 ;; eol -- end of line
61 ;; iopl -- indentation of previous line 59 ;; iopl -- indentation of previous line
62 ;; bonl -- beginning of next line 60 ;; bonl -- beginning of next line
63 ;; bopl -- beginning of previous line 61 ;; bopl -- beginning of previous line
64 ;; 62 ;;
65 ;; This function does not modify point or mark. 63 ;; This function does not modify point or mark.
66 (let ((here (point))) 64 `(save-excursion
67 (cond 65 ,(if (and (eq (car-safe position) 'quote)
68 ((eq position 'bol) (beginning-of-line)) 66 (symbolp (eval position)))
69 ((eq position 'eol) (end-of-line)) 67 (let ((position (eval position)))
70 ((eq position 'boi) (back-to-indentation)) 68 (cond
71 ((eq position 'bonl) (forward-line 1)) 69 ((eq position 'bol) `(beginning-of-line))
72 ((eq position 'bopl) (forward-line -1)) 70 ((eq position 'eol) `(end-of-line))
73 ((eq position 'iopl) 71 ((eq position 'boi) `(back-to-indentation))
74 (forward-line -1) 72 ((eq position 'bonl) `(forward-line 1))
75 (back-to-indentation)) 73 ((eq position 'bopl) `(forward-line -1))
76 ((eq position 'ionl) 74 ((eq position 'bod) `(c-beginning-of-defun-1))
77 (forward-line 1) 75 ((eq position 'eod) `(c-end-of-defun-1))
78 (back-to-indentation)) 76 ((eq position 'iopl) `(progn
79 ((eq position 'eod) (c-end-of-defun)) 77 (forward-line -1)
80 ((eq position 'bod) 78 (back-to-indentation)))
81 (if (and (fboundp 'buffer-syntactic-context-depth) 79 ((eq position 'ionl) `(progn
82 c-enable-xemacs-performance-kludge-p) 80 (forward-line 1)
83 ;; XEmacs only. This can improve the performance of 81 (back-to-indentation)))
84 ;; c-parse-state to between 3 and 60 times faster when 82 (t (error "unknown buffer position requested: %s" position))))
85 ;; braces are hung. It can also degrade performance by 83 ;;(message "c-point long expansion")
86 ;; about as much when braces are not hung. 84 `(let ((position ,position))
87 (let (pos) 85 (cond
88 (while (not pos) 86 ((eq position 'bol) (beginning-of-line))
89 (save-restriction 87 ((eq position 'eol) (end-of-line))
90 (widen) 88 ((eq position 'boi) (back-to-indentation))
91 (setq pos (scan-lists (point) -1 89 ((eq position 'bonl) (forward-line 1))
92 (buffer-syntactic-context-depth) 90 ((eq position 'bopl) (forward-line -1))
93 nil t))) 91 ((eq position 'bod) (c-beginning-of-defun-1))
94 (cond 92 ((eq position 'eod) (c-end-of-defun-1))
95 ((bobp) (setq pos (point-min))) 93 ((eq position 'iopl) (progn
96 ((not pos) 94 (forward-line -1)
97 (let ((distance (skip-chars-backward "^{"))) 95 (back-to-indentation)))
98 ;; unbalanced parenthesis, while illegal C code, 96 ((eq position 'ionl) (progn
99 ;; shouldn't cause an infloop! See unbal.c 97 (forward-line 1)
100 (when (zerop distance) 98 (back-to-indentation)))
101 ;; Punt! 99 (t (error "unknown buffer position requested: %s" position)))))
102 (beginning-of-defun) 100 (point)))
103 (setq pos (point)))))
104 ((= pos 0))
105 ((not (eq (char-after pos) ?{))
106 (goto-char pos)
107 (setq pos nil))
108 ))
109 (goto-char pos))
110 ;; Emacs, which doesn't have buffer-syntactic-context-depth
111 ;;
112 ;; NOTE: This should be the only explicit use of
113 ;; beginning-of-defun in CC Mode. Eventually something better
114 ;; than b-o-d will be available and this should be the only
115 ;; place the code needs to change. Everything else should use
116 ;; (goto-char (c-point 'bod))
117 (beginning-of-defun)
118 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at
119 ;; the open brace.
120 (and defun-prompt-regexp
121 (looking-at defun-prompt-regexp)
122 (goto-char (match-end 0)))
123 ))
124 (t (error "unknown buffer position requested: %s" position))
125 )
126 (prog1
127 (point)
128 (goto-char here))))
129 101
130 102
131 (defmacro c-safe (&rest body) 103 (defmacro c-safe (&rest body)
132 ;; safely execute BODY, return nil if an error occurred 104 ;; safely execute BODY, return nil if an error occurred
133 (` (condition-case nil 105 `(condition-case nil
134 (progn (,@ body)) 106 (progn ,@body)
135 (error nil)))) 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))
136 163
137 (defmacro c-forward-sexp (&optional arg) 164 (defmacro c-forward-sexp (&optional arg)
138 ;; like forward-sexp except 165 ;; like forward-sexp except
139 ;; 1. this is much stripped down from the XEmacs version 166 ;; 1. this is much stripped down from the XEmacs version
140 ;; 2. this cannot be used as a command, so we're insulated from 167 ;; 2. this cannot be used as a command, so we're insulated from
150 (defmacro c-backward-sexp (&optional arg) 177 (defmacro c-backward-sexp (&optional arg)
151 ;; See c-forward-sexp and reverse directions 178 ;; See c-forward-sexp and reverse directions
152 (or arg (setq arg 1)) 179 (or arg (setq arg 1))
153 `(c-forward-sexp ,(if (numberp arg) (- arg) `(- ,arg)))) 180 `(c-forward-sexp ,(if (numberp arg) (- arg) `(- ,arg))))
154 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
155 (defmacro c-add-syntax (symbol &optional relpos) 228 (defmacro c-add-syntax (symbol &optional relpos)
156 ;; a simple macro to append the syntax in symbol to the syntax list. 229 ;; a simple macro to append the syntax in symbol to the syntax list.
157 ;; try to increase performance by using this macro 230 ;; try to increase performance by using this macro
158 (` (setq syntax (cons (cons (, symbol) (, relpos)) syntax)))) 231 `(setq syntax (cons (cons ,symbol ,relpos) syntax)))
159 232
160 (defsubst c-auto-newline () 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 ()
161 ;; if auto-newline feature is turned on, insert a newline character 249 ;; if auto-newline feature is turned on, insert a newline character
162 ;; and return t, otherwise return nil. 250 ;; and return t, otherwise return nil.
163 (and c-auto-newline 251 `(and c-auto-newline
164 (not (c-in-literal)) 252 (not (c-in-literal))
165 (not (newline)))) 253 (not (newline))))
166 254
167 (defsubst c-intersect-lists (list alist) 255 (defsubst c-intersect-lists (list alist)
168 ;; return the element of ALIST that matches the first element found 256 ;; return the element of ALIST that matches the first element found
169 ;; in LIST. Uses assq. 257 ;; in LIST. Uses assq.
170 (let (match) 258 (let (match)
186 (prog1 (current-column) 274 (prog1 (current-column)
187 (if preserve-point 275 (if preserve-point
188 (goto-char here)) 276 (goto-char here))
189 ))) 277 )))
190 278
191 (defsubst c-update-modeline () 279 (defmacro c-update-modeline ()
192 ;; set the c-auto-hungry-string for the correct designation on the modeline 280 ;; set the c-auto-hungry-string for the correct designation on the modeline
193 (setq c-auto-hungry-string 281 `(progn
194 (if c-auto-newline 282 (setq c-auto-hungry-string
195 (if c-hungry-delete-key "/ah" "/a") 283 (if c-auto-newline
196 (if c-hungry-delete-key "/h" nil))) 284 (if c-hungry-delete-key "/ah" "/a")
197 (force-mode-line-update)) 285 (if c-hungry-delete-key "/h" nil)))
286 (force-mode-line-update)))
198 287
199 (defsubst c-keep-region-active () 288 (defsubst c-keep-region-active ()
200 ;; Do whatever is necessary to keep the region active in XEmacs. 289 ;; Do whatever is necessary to keep the region active in XEmacs.
201 ;; Ignore byte-compiler warnings you might see. This is not needed 290 ;; Ignore byte-compiler warnings you might see. This is not needed
202 ;; for Emacs. 291 ;; for Emacs.
207 ;; Return t when the region is active. The determination of region 296 ;; Return t when the region is active. The determination of region
208 ;; activeness is different in both Emacs and XEmacs. 297 ;; activeness is different in both Emacs and XEmacs.
209 (cond 298 (cond
210 ;; XEmacs 299 ;; XEmacs
211 ((and (fboundp 'region-active-p) 300 ((and (fboundp 'region-active-p)
301 (boundp 'zmacs-regions)
212 zmacs-regions) 302 zmacs-regions)
213 (region-active-p)) 303 (region-active-p))
214 ;; Emacs 304 ;; Emacs
215 ((boundp 'mark-active) mark-active) 305 ((boundp 'mark-active) mark-active)
216 ;; fallback; shouldn't get here 306 ;; fallback; shouldn't get here
217 (t (mark t)))) 307 (t (mark t))))
218 308
219 (defsubst c-major-mode-is (mode) 309 (defsubst c-major-mode-is (mode)
220 (eq (derived-mode-class major-mode) mode)) 310 (eq (derived-mode-class major-mode) mode))
221 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
222 323
223 (provide 'cc-defs) 324 (provide 'cc-defs)
224 ;;; cc-defs.el ends here 325 ;;; cc-defs.el ends here