]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-defs.el
(c-point): In XEmacs, use scan-lists + buffer-syntactic-context-depth.
[gnu-emacs] / lisp / progmodes / cc-defs.el
1 ;;; cc-defs.el --- definitions for CC Mode
2
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
4
5 ;; Authors: 1992-1997 Barry A. Warsaw
6 ;; 1987 Dave Detlefs and Stewart Clamen
7 ;; 1985 Richard M. Stallman
8 ;; Maintainer: cc-mode-help@python.org
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
10 ;; Version: See cc-mode.el
11 ;; Keywords: c languages oop
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 \f
31 ;; Figure out what features this Emacs has
32 ;;;###autoload
33 (defconst c-emacs-features
34 (let ((infodock-p (boundp 'infodock-version))
35 (comments
36 ;; XEmacs 19 and beyond use 8-bit modify-syntax-entry flags.
37 ;; Emacs 19 uses a 1-bit flag. We will have to set up our
38 ;; syntax tables differently to handle this.
39 (let ((table (copy-syntax-table))
40 entry)
41 (modify-syntax-entry ?a ". 12345678" table)
42 (cond
43 ;; XEmacs 19, and beyond Emacs 19.34
44 ((arrayp table)
45 (setq entry (aref table ?a))
46 ;; In Emacs, table entries are cons cells
47 (if (consp entry) (setq entry (car entry))))
48 ;; XEmacs 20
49 ((fboundp 'get-char-table) (setq entry (get-char-table ?a table)))
50 ;; before and including Emacs 19.34
51 ((and (fboundp 'char-table-p)
52 (char-table-p table))
53 (setq entry (car (char-table-range table [?a]))))
54 ;; incompatible
55 (t (error "CC Mode is incompatible with this version of Emacs")))
56 (if (= (logand (lsh entry -16) 255) 255)
57 '8-bit
58 '1-bit))))
59 (if infodock-p
60 (list comments 'infodock)
61 (list comments)))
62 "A list of features extant in the Emacs you are using.
63 There are many flavors of Emacs out there, each with different
64 features supporting those needed by CC Mode. Here's the current
65 supported list, along with the values for this variable:
66
67 XEmacs 19: (8-bit)
68 XEmacs 20: (8-bit)
69 Emacs 19: (1-bit)
70
71 Infodock (based on XEmacs) has an additional symbol on this list:
72 'infodock.")
73
74
75 \f
76 (defsubst c-point (position)
77 ;; Returns the value of point at certain commonly referenced POSITIONs.
78 ;; POSITION can be one of the following symbols:
79 ;;
80 ;; bol -- beginning of line
81 ;; eol -- end of line
82 ;; bod -- beginning of defun
83 ;; boi -- back to indentation
84 ;; ionl -- indentation of next line
85 ;; iopl -- indentation of previous line
86 ;; bonl -- beginning of next line
87 ;; bopl -- beginning of previous line
88 ;;
89 ;; This function does not modify point or mark.
90 (let ((here (point)))
91 (cond
92 ((eq position 'bol) (beginning-of-line))
93 ((eq position 'eol) (end-of-line))
94 ((eq position 'boi) (back-to-indentation))
95 ((eq position 'bonl) (forward-line 1))
96 ((eq position 'bopl) (forward-line -1))
97 ((eq position 'iopl)
98 (forward-line -1)
99 (back-to-indentation))
100 ((eq position 'ionl)
101 (forward-line 1)
102 (back-to-indentation))
103 ((eq position 'bod)
104 (if (and (fboundp 'buffer-syntactic-context-depth)
105 c-enable-xemacs-performance-kludge-p)
106 ;; XEmacs only. This can improve the performance of
107 ;; c-parse-state to between 3 and 60 times faster when
108 ;; braces are hung. It can cause c-parse-state to be
109 ;; slightly slower when braces are not hung, but general
110 ;; editing appears to be still about as fast.
111 (let (pos)
112 (while (not pos)
113 (save-restriction
114 (widen)
115 (setq pos (scan-lists (point) -1
116 (buffer-syntactic-context-depth)
117 nil t)))
118 (cond
119 ((bobp) (setq pos (point-min)))
120 ((not pos)
121 (let ((distance (skip-chars-backward "^{")))
122 ;; unbalanced parenthesis, while illegal C code,
123 ;; shouldn't cause an infloop! See unbal.c
124 (when (zerop distance)
125 ;; Punt!
126 (beginning-of-defun)
127 (setq pos (point)))))
128 ((= pos 0))
129 ((not (eq (char-after pos) ?{))
130 (goto-char pos)
131 (setq pos nil))
132 ))
133 (goto-char pos))
134 ;; Emacs, which doesn't have buffer-syntactic-context-depth
135 ;;
136 ;; NOTE: This should be the only explicit use of
137 ;; beginning-of-defun in CC Mode. Eventually something better
138 ;; than b-o-d will be available and this should be the only
139 ;; place the code needs to change. Everything else should use
140 ;; (goto-char (c-point 'bod))
141 (beginning-of-defun)
142 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at
143 ;; the open brace.
144 (and defun-prompt-regexp
145 (looking-at defun-prompt-regexp)
146 (goto-char (match-end 0)))
147 ))
148 (t (error "unknown buffer position requested: %s" position))
149 )
150 (prog1
151 (point)
152 (goto-char here))))
153
154 (defmacro c-safe (&rest body)
155 ;; safely execute BODY, return nil if an error occurred
156 (` (condition-case nil
157 (progn (,@ body))
158 (error nil))))
159
160 (defmacro c-add-syntax (symbol &optional relpos)
161 ;; a simple macro to append the syntax in symbol to the syntax list.
162 ;; try to increase performance by using this macro
163 (` (setq syntax (cons (cons (, symbol) (, relpos)) syntax))))
164
165 (defsubst c-auto-newline ()
166 ;; if auto-newline feature is turned on, insert a newline character
167 ;; and return t, otherwise return nil.
168 (and c-auto-newline
169 (not (c-in-literal))
170 (not (newline))))
171
172 (defsubst c-intersect-lists (list alist)
173 ;; return the element of ALIST that matches the first element found
174 ;; in LIST. Uses assq.
175 (let (match)
176 (while (and list
177 (not (setq match (assq (car list) alist))))
178 (setq list (cdr list)))
179 match))
180
181 (defsubst c-lookup-lists (list alist1 alist2)
182 ;; first, find the first entry from LIST that is present in ALIST1,
183 ;; then find the entry in ALIST2 for that entry.
184 (assq (car (c-intersect-lists list alist1)) alist2))
185
186 (defsubst c-langelem-col (langelem &optional preserve-point)
187 ;; convenience routine to return the column of langelem's relpos.
188 ;; Leaves point at the relpos unless preserve-point is non-nil.
189 (let ((here (point)))
190 (goto-char (cdr langelem))
191 (prog1 (current-column)
192 (if preserve-point
193 (goto-char here))
194 )))
195
196 (defsubst c-update-modeline ()
197 ;; set the c-auto-hungry-string for the correct designation on the modeline
198 (setq c-auto-hungry-string
199 (if c-auto-newline
200 (if c-hungry-delete-key "/ah" "/a")
201 (if c-hungry-delete-key "/h" nil)))
202 (force-mode-line-update))
203
204 (defsubst c-keep-region-active ()
205 ;; Do whatever is necessary to keep the region active in XEmacs.
206 ;; Ignore byte-compiler warnings you might see. This is not needed
207 ;; for Emacs.
208 (and (boundp 'zmacs-region-stays)
209 (setq zmacs-region-stays t)))
210
211 \f
212 (provide 'cc-defs)
213 ;;; cc-defs.el ends here