209
|
1 ;; syntax.el --- Syntax-table hacking stuff, moved from syntax.c
|
|
2
|
|
3 ;; Copyright (C) 1993, 1997 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1995 Sun Microsystems.
|
|
5
|
|
6 ;; This file is part of XEmacs.
|
|
7
|
|
8 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
9 ;; under the terms of the GNU General Public License as published by
|
|
10 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
11 ;; any later version.
|
|
12
|
|
13 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 ;; General Public License for more details.
|
|
17
|
|
18 ;; You should have received a copy of the GNU General Public License
|
|
19 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
20 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
21 ;; Boston, MA 02111-1307, USA.
|
|
22
|
|
23 ;;; Synched up with: FSF 19.28.
|
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; This file is dumped with XEmacs.
|
|
28
|
|
29 ;; Note: FSF does not have a file syntax.el. This stuff is
|
|
30 ;; in syntax.c. See comments there about not merging past 19.28.
|
|
31
|
|
32 ;; Significantly hacked upon by Ben Wing.
|
|
33
|
|
34 ;;; Code:
|
|
35
|
|
36 (defun make-syntax-table (&optional oldtable)
|
|
37 "Return a new syntax table.
|
|
38 It inherits all characters from the standard syntax table."
|
|
39 (make-char-table 'syntax))
|
|
40
|
|
41 (defun simple-set-syntax-entry (char spec table)
|
|
42 (put-char-table char spec table))
|
|
43
|
|
44 (defun char-syntax-from-code (code)
|
|
45 "Extract the syntax designator from the internal syntax code CODE.
|
|
46 CODE is the value actually contained in the syntax table."
|
|
47 (if (consp code)
|
|
48 (setq code (car code)))
|
|
49 (aref (syntax-designator-chars) (logand code 127)))
|
|
50
|
|
51 (defun set-char-syntax-in-code (code desig)
|
|
52 "Return a new internal syntax code whose syntax designator is DESIG.
|
|
53 Other characteristics are the same as in CODE."
|
|
54 (let ((newcode (if (consp code) (car code) code)))
|
|
55 (setq newcode (logior (string-match
|
|
56 (regexp-quote (char-to-string desig))
|
|
57 (syntax-designator-chars))
|
|
58 (logand newcode (lognot 127))))
|
|
59 (if (consp code) (cons newcode (cdr code))
|
|
60 newcode)))
|
|
61
|
|
62 (defun syntax-code-to-string (code)
|
|
63 "Return a string equivalent to internal syntax code CODE.
|
|
64 The string can be passed to `modify-syntax-entry'.
|
|
65 If CODE is invalid, return nil."
|
|
66 (let ((match (and (consp code) (cdr code)))
|
|
67 (codes (syntax-designator-chars)))
|
|
68 (if (consp code)
|
|
69 (setq code (car code)))
|
|
70 (if (or (not (integerp code))
|
|
71 (> (logand code 127) (length codes)))
|
|
72 nil
|
|
73 (with-output-to-string
|
|
74 (let* ((spec (elt codes (logand code 127)))
|
|
75 (b3 (lsh code -16))
|
|
76 (start1 (/= 0 (logand b3 128))) ;logtest!
|
|
77 (start1b (/= 0 (logand b3 64)))
|
|
78 (start2 (/= 0 (logand b3 32)))
|
|
79 (start2b (/= 0 (logand b3 16)))
|
|
80 (end1 (/= 0 (logand b3 8)))
|
|
81 (end1b (/= 0 (logand b3 4)))
|
|
82 (end2 (/= 0 (logand b3 2)))
|
|
83 (end2b (/= 0 (logand b3 1)))
|
|
84 (prefix (/= 0 (logand code 128)))
|
|
85 (single-char-p (or (= spec ?<) (= spec ?>)))
|
|
86 )
|
|
87 (write-char spec)
|
|
88 (write-char (if match match 32))
|
|
89 ;;; (if start1 (if single-char-p (write-char ?a) (write-char ?1)))
|
|
90 (if start1 (if single-char-p (write-char ? ) (write-char ?1)))
|
|
91 (if start2 (write-char ?2))
|
|
92 ;;; (if end1 (if single-char-p (write-char ?a) (write-char ?3)))
|
|
93 (if end1 (if single-char-p (write-char ? ) (write-char ?3)))
|
|
94 (if end2 (write-char ?4))
|
|
95 (if start1b (if single-char-p (write-char ?b) (write-char ?5)))
|
|
96 (if start2b (write-char ?6))
|
|
97 (if end1b (if single-char-p (write-char ?b) (write-char ?7)))
|
|
98 (if end2b (write-char ?8))
|
|
99 (if prefix (write-char ?p)))))))
|
|
100
|
|
101 (defun syntax-string-to-code (string)
|
|
102 "Return the internal syntax code equivalent to STRING.
|
|
103 STRING should be something acceptable as the second argument to
|
|
104 `modify-syntax-entry'.
|
|
105 If STRING is invalid, signal an error."
|
|
106 (let* ((bflag nil)
|
|
107 (b3 0)
|
|
108 (ch0 (aref string 0))
|
|
109 (len (length string))
|
|
110 (code (string-match (regexp-quote (char-to-string ch0))
|
|
111 (syntax-designator-chars)))
|
|
112 (i 2)
|
|
113 ch)
|
|
114 (or code
|
|
115 (error "Invalid syntax designator: %S" string))
|
|
116 (while (< i len)
|
|
117 (setq ch (aref string i))
|
|
118 (incf i)
|
|
119 (case ch
|
|
120 (?1 (setq b3 (logior b3 128)))
|
|
121 (?2 (setq b3 (logior b3 32)))
|
|
122 (?3 (setq b3 (logior b3 8)))
|
|
123 (?4 (setq b3 (logior b3 2)))
|
|
124 (?5 (setq b3 (logior b3 64)))
|
|
125 (?6 (setq b3 (logior b3 16)))
|
|
126 (?7 (setq b3 (logior b3 4)))
|
|
127 (?8 (setq b3 (logior b3 1)))
|
|
128 (?a (case ch0
|
|
129 (?< (setq b3 (logior b3 128)))
|
|
130 (?> (setq b3 (logior b3 8)))))
|
|
131 (?b (case ch0
|
|
132 (?< (setq b3 (logior b3 64) bflag t))
|
|
133 (?> (setq b3 (logior b3 4) bflag t))))
|
|
134 (?p (setq code (logior code (lsh 1 7))))
|
|
135 (?\ nil) ;; ignore for compatibility
|
|
136 (otherwise
|
|
137 (error "Invalid syntax description flag: %S" string))))
|
|
138 ;; default single char style if `b' has not been seen
|
|
139 (if (not bflag)
|
|
140 (case ch0
|
|
141 (?< (setq b3 (logior b3 128)))
|
|
142 (?> (setq b3 (logior b3 8)))))
|
|
143 (setq code (logior code (lsh b3 16)))
|
|
144 (if (and (> len 1)
|
|
145 ;; tough luck if you want to make space a paren!
|
|
146 (/= (aref string 1) ?\ ))
|
|
147 (setq code (cons code (aref string 1))))
|
|
148 code))
|
|
149
|
|
150 (defun modify-syntax-entry (char-range spec &optional table)
|
|
151 "Set syntax for the characters CHAR-RANGE according to string SPEC.
|
|
152 CHAR-RANGE is a single character or a range of characters,
|
|
153 as per `put-char-table'.
|
|
154 The syntax is changed only for table TABLE, which defaults to
|
|
155 the current buffer's syntax table.
|
|
156 The first character of SPEC should be one of the following:
|
|
157 Space whitespace syntax. w word constituent.
|
|
158 _ symbol constituent. . punctuation.
|
|
159 \( open-parenthesis. \) close-parenthesis.
|
|
160 \" string quote. \\ character-quote.
|
|
161 $ paired delimiter. ' expression quote or prefix operator.
|
|
162 < comment starter. > comment ender.
|
|
163 / character-quote. @ inherit from `standard-syntax-table'.
|
|
164
|
|
165 Only single-character comment start and end sequences are represented thus.
|
|
166 Two-character sequences are represented as described below.
|
|
167 The second character of SPEC is the matching parenthesis,
|
|
168 used only if the first character is `(' or `)'.
|
|
169 Any additional characters are flags.
|
|
170 Defined flags are the characters 1, 2, 3, 4, 5, 6, 7, 8, p, a, and b.
|
|
171 1 means C is the first of a two-char comment start sequence of style a.
|
|
172 2 means C is the second character of such a sequence.
|
|
173 3 means C is the first of a two-char comment end sequence of style a.
|
|
174 4 means C is the second character of such a sequence.
|
|
175 5 means C is the first of a two-char comment start sequence of style b.
|
|
176 6 means C is the second character of such a sequence.
|
|
177 7 means C is the first of a two-char comment end sequence of style b.
|
|
178 8 means C is the second character of such a sequence.
|
|
179 p means C is a prefix character for `backward-prefix-chars';
|
|
180 such characters are treated as whitespace when they occur
|
|
181 between expressions.
|
|
182 a means C is comment starter or comment ender for comment style a (default)
|
|
183 b means C is comment starter or comment ender for comment style b."
|
|
184 (interactive
|
|
185 ;; I really don't know why this is interactive
|
|
186 ;; help-form should at least be made useful whilst reading the second arg
|
|
187 "cSet syntax for character: \nsSet syntax for %c to: ")
|
|
188 (cond ((syntax-table-p table))
|
|
189 ((not table)
|
|
190 (setq table (syntax-table)))
|
|
191 (t
|
|
192 (setq table
|
|
193 (wrong-type-argument 'syntax-table-p table))))
|
|
194 (let ((code (syntax-string-to-code spec)))
|
|
195 (simple-set-syntax-entry char-range code table))
|
|
196 nil)
|
|
197
|
|
198 (defun map-syntax-table (__function __table &optional __range)
|
|
199 "Map FUNCTION over entries in syntax table TABLE, collapsing inheritance.
|
|
200 This is similar to `map-char-table', but works only on syntax tables, and
|
|
201 collapses any entries that call for inheritance by invisibly substituting
|
|
202 the inherited values from the standard syntax table."
|
|
203 (check-argument-type 'syntax-table-p __table)
|
|
204 (map-char-table #'(lambda (__key __value)
|
|
205 (if (eq ?@ (char-syntax-from-code __value))
|
|
206 (map-char-table #'(lambda (__key __value)
|
|
207 (funcall __function
|
|
208 __key __value))
|
|
209 (standard-syntax-table)
|
|
210 __key)
|
|
211 (funcall __function __key __value)))
|
|
212 __table __range))
|
|
213
|
|
214 ;(defun test-xm ()
|
|
215 ; (let ((o (copy-syntax-table))
|
|
216 ; (n (copy-syntax-table))
|
|
217 ; (codes (syntax-designator-chars))
|
|
218 ; (flags "12345678abp"))
|
|
219 ; (while t
|
|
220 ; (let ((spec (concat (char-to-string (elt codes
|
|
221 ; (random (length codes))))))
|
|
222 ; (if (= (random 4) 0)
|
|
223 ; "b"
|
|
224 ; " ")
|
|
225 ; (let* ((n (random 4))
|
|
226 ; (s (make-string n 0)))
|
|
227 ; (while (> n 0)
|
|
228 ; (setq n (1- n))
|
|
229 ; (aset s n (aref flags (random (length flags)))))
|
|
230 ; s))))
|
|
231 ; (message "%S..." spec)
|
|
232 ; (modify-syntax-entry ?a spec o)
|
|
233 ; (xmodify-syntax-entry ?a spec n)
|
|
234 ; (or (= (aref o ?a) (aref n ?a))
|
|
235 ; (error "%s"
|
|
236 ; (format "fucked with %S: %x %x"
|
|
237 ; spec (aref o ?a) (aref n ?a))))))))
|
|
238
|
|
239
|
|
240 (defun describe-syntax-table (table stream)
|
|
241 (let (first-char
|
|
242 last-char
|
|
243 prev-val
|
|
244 (describe-one
|
|
245 (if (featurep 'mule)
|
|
246 #'(lambda (first last value stream)
|
|
247 (if (equal first last)
|
|
248 (cond ((vectorp first)
|
|
249 (princ (format "%s, row %d\t"
|
|
250 (charset-name
|
|
251 (aref first 0))
|
|
252 (aref first 1))
|
|
253 stream))
|
|
254 ((symbolp first)
|
|
255 (princ first stream)
|
|
256 (princ "\t" stream))
|
|
257 (t
|
|
258 (princ (text-char-description first) stream)
|
|
259 (princ "\t" stream)))
|
|
260 (cond ((vectorp first)
|
|
261 (princ (format "%s, rows %d .. %d\t"
|
|
262 (charset-name
|
|
263 (aref first 0))
|
|
264 (aref first 1)
|
|
265 (aref last 1))
|
|
266 stream))
|
|
267 ((symbolp first)
|
|
268 (princ (format "%s .. %s\t" first last) stream))
|
|
269 (t
|
|
270 (princ (format "%s .. %s\t"
|
|
271 (text-char-description first)
|
|
272 (text-char-description last))
|
|
273 stream))))
|
|
274 (describe-syntax-code value stream))
|
|
275 #'(lambda (first last value stream)
|
|
276 (let* ((tem (text-char-description first))
|
|
277 (pos (length tem))
|
|
278 ;;(limit (cond ((numberp ctl-arrow) ctl-arrow)
|
|
279 ;; ((memq ctl-arrow '(t nil)) 256)
|
|
280 ;; (t 160)))
|
|
281 )
|
|
282 (princ tem stream)
|
|
283 (if (> last first)
|
|
284 (progn
|
|
285 (princ " .. " stream)
|
|
286 (setq tem (text-char-description last))
|
|
287 (princ tem stream)
|
|
288 (setq pos (+ pos (length tem) 4))))
|
|
289 (while (progn (write-char ?\ stream)
|
|
290 (setq pos (1+ pos))
|
|
291 (< pos 16))))
|
|
292 (describe-syntax-code value stream)))))
|
|
293 (map-syntax-table
|
|
294 #'(lambda (range value)
|
|
295 (cond
|
|
296 ((not first-char)
|
|
297 (setq first-char range
|
|
298 last-char range
|
|
299 prev-val value))
|
|
300 ((and (equal value prev-val)
|
|
301 (or
|
|
302 (and (characterp range)
|
|
303 (characterp first-char)
|
|
304 (or (not (featurep 'mule))
|
|
305 (eq (char-charset range)
|
|
306 (char-charset first-char)))
|
|
307 (= (char-int last-char) (1- (char-int range))))
|
|
308 (and (vectorp range)
|
|
309 (vectorp first-char)
|
|
310 (eq (aref range 0) (aref first-char 0))
|
|
311 (= (aref last-char 1) (1- (aref range 1))))))
|
|
312 (setq last-char range))
|
|
313 (t
|
|
314 (funcall describe-one first-char last-char prev-val stream)
|
|
315 (setq first-char range
|
|
316 last-char range
|
|
317 prev-val value)))
|
|
318 nil)
|
|
319 table)
|
|
320 (if first-char
|
|
321 (funcall describe-one first-char last-char prev-val stream))))
|
|
322
|
|
323 (defun describe-syntax-code (code stream)
|
|
324 (let ((match (and (consp code) (cdr code)))
|
|
325 (invalid (gettext "**invalid**")) ;(empty "") ;constants
|
|
326 (standard-output (or stream standard-output))
|
|
327 ;; #### I18N3 should temporarily set buffer to output-translatable
|
|
328 (in #'(lambda (string)
|
|
329 (princ ",\n\t\t\t\t ")
|
|
330 (princ string)))
|
|
331 (syntax-string (syntax-code-to-string code)))
|
|
332 (if (consp code)
|
|
333 (setq code (car code)))
|
|
334 (if (null syntax-string)
|
|
335 (princ invalid)
|
|
336 (princ syntax-string)
|
|
337 (princ "\tmeaning: ")
|
|
338 (princ (aref ["whitespace" "punctuation" "word-constituent"
|
|
339 "symbol-constituent" "open-paren" "close-paren"
|
|
340 "expression-prefix" "string-quote" "paired-delimiter"
|
|
341 "escape" "character-quote" "comment-begin" "comment-end"
|
|
342 "inherit" "extended-word-constituent"]
|
|
343 (logand code 127)))
|
|
344
|
|
345 (if match
|
|
346 (progn
|
|
347 (princ ", matches ")
|
|
348 (princ (text-char-description match))))
|
|
349 (let* ((spec (elt syntax-string 0))
|
|
350 (b3 (lsh code -16))
|
|
351 (start1 (/= 0 (logand b3 128))) ;logtest!
|
|
352 (start1b (/= 0 (logand b3 64)))
|
|
353 (start2 (/= 0 (logand b3 32)))
|
|
354 (start2b (/= 0 (logand b3 16)))
|
|
355 (end1 (/= 0 (logand b3 8)))
|
|
356 (end1b (/= 0 (logand b3 4)))
|
|
357 (end2 (/= 0 (logand b3 2)))
|
|
358 (end2b (/= 0 (logand b3 1)))
|
|
359 (prefix (/= 0 (logand code 128)))
|
|
360 (single-char-p (or (= spec ?<) (= spec ?>))))
|
|
361 (if start1
|
|
362 (if single-char-p
|
|
363 (princ ", style A")
|
|
364 (funcall in
|
|
365 (gettext "first character of comment-start sequence A"))))
|
|
366 (if start2
|
|
367 (funcall in
|
|
368 (gettext "second character of comment-start sequence A")))
|
|
369 (if end1
|
|
370 (if single-char-p
|
|
371 (princ ", style A")
|
|
372 (funcall in
|
|
373 (gettext "first character of comment-end sequence A"))))
|
|
374 (if end2
|
|
375 (funcall in
|
|
376 (gettext "second character of comment-end sequence A")))
|
|
377 (if start1b
|
|
378 (if single-char-p
|
|
379 (princ ", style B")
|
|
380 (funcall in
|
|
381 (gettext "first character of comment-start sequence B"))))
|
|
382 (if start2b
|
|
383 (funcall in
|
|
384 (gettext "second character of comment-start sequence B")))
|
|
385 (if end1b
|
|
386 (if single-char-p
|
|
387 (princ ", style B")
|
|
388 (funcall in
|
|
389 (gettext "first character of comment-end sequence B"))))
|
|
390 (if end2b
|
|
391 (funcall in
|
|
392 (gettext "second character of comment-end sequence B")))
|
|
393 (if prefix
|
|
394 (funcall in
|
|
395 (gettext "prefix character for `backward-prefix-chars'"))))
|
|
396 (terpri stream))))
|
|
397
|
|
398 (defun symbol-near-point ()
|
|
399 "Return the first textual item to the nearest point."
|
|
400 (interactive)
|
|
401 ;alg stolen from etag.el
|
|
402 (save-excursion
|
|
403 (if (or (bobp) (not (memq (char-syntax (char-before)) '(?w ?_))))
|
|
404 (while (not (looking-at "\\sw\\|\\s_\\|\\'"))
|
|
405 (forward-char 1)))
|
|
406 (while (looking-at "\\sw\\|\\s_")
|
|
407 (forward-char 1))
|
|
408 (if (re-search-backward "\\sw\\|\\s_" nil t)
|
|
409 (regexp-quote
|
|
410 (progn (forward-char 1)
|
|
411 (buffer-substring (point)
|
|
412 (progn (forward-sexp -1)
|
|
413 (while (looking-at "\\s'")
|
|
414 (forward-char 1))
|
|
415 (point)))))
|
|
416 nil)))
|
|
417
|
|
418 ;;; syntax.el ends here
|