1 ;; syntax.el --- Syntax-table hacking stuff, moved from syntax.c
3 ;; Copyright (C) 1993, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Sun Microsystems.
6 ;; This file is part of XEmacs.
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)
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.
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.
23 ;;; Synched up with: FSF 19.28.
27 ;; This file is dumped with XEmacs.
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.
32 ;; Significantly hacked upon by Ben Wing.
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))
41 (defun simple-set-syntax-entry (char spec table)
42 (put-char-table char spec table))
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."
48 (setq code (car code)))
49 (aref (syntax-designator-chars) (logand code 127)))
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))
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)))
69 (setq code (car code)))
70 (if (or (not (integerp code))
71 (> (logand code 127) (length codes)))
73 (with-output-to-string
74 (let* ((spec (elt codes (logand code 127)))
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 ?>)))
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)))))))
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."
108 (ch0 (aref string 0))
109 (len (length string))
110 (code (string-match (regexp-quote (char-to-string ch0))
111 (syntax-designator-chars)))
115 (error "Invalid syntax designator: %S" string))
117 (setq ch (aref string i))
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)))
129 (?< (setq b3 (logior b3 128)))
130 (?> (setq b3 (logior b3 8)))))
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
137 (error "Invalid syntax description flag: %S" string))))
138 ;; default single char style if `b' has not been seen
141 (?< (setq b3 (logior b3 128)))
142 (?> (setq b3 (logior b3 8)))))
143 (setq code (logior code (lsh b3 16)))
145 ;; tough luck if you want to make space a paren!
146 (/= (aref string 1) ?\ ))
147 (setq code (cons code (aref string 1))))
150 (defun modify-syntax-entry (char-range spec &optional syntax-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 SYNTAX-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'.
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
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."
185 ;; I really don't know why this is interactive
186 ;; help-form should at least be made useful while reading the second arg
187 "cSet syntax for character: \nsSet syntax for %c to: ")
188 (simple-set-syntax-entry
190 (syntax-string-to-code spec)
191 (cond ((syntax-table-p syntax-table)
196 (wrong-type-argument 'syntax-table-p syntax-table))))
199 (defun map-syntax-table (__function __syntax_table &optional __range)
200 "Map FUNCTION over entries in SYNTAX-TABLE, collapsing inheritance.
201 This is similar to `map-char-table', but works only on syntax tables, and
202 collapses any entries that call for inheritance by invisibly substituting
203 the inherited values from the standard syntax table."
204 (check-argument-type 'syntax-table-p __syntax_table)
205 (map-char-table #'(lambda (__key __value)
206 (if (eq ?@ (char-syntax-from-code __value))
207 (map-char-table #'(lambda (__key __value)
210 (standard-syntax-table)
212 (funcall __function __key __value)))
213 __syntax_table __range))
216 ; (let ((o (copy-syntax-table))
217 ; (n (copy-syntax-table))
218 ; (codes (syntax-designator-chars))
219 ; (flags "12345678abp"))
221 ; (let ((spec (concat (char-to-string (elt codes
222 ; (random (length codes))))))
223 ; (if (= (random 4) 0)
226 ; (let* ((n (random 4))
227 ; (s (make-string n 0)))
230 ; (aset s n (aref flags (random (length flags)))))
232 ; (message "%S..." spec)
233 ; (modify-syntax-entry ?a spec o)
234 ; (xmodify-syntax-entry ?a spec n)
235 ; (or (= (aref o ?a) (aref n ?a))
237 ; (format "fucked with %S: %x %x"
238 ; spec (aref o ?a) (aref n ?a))))))))
241 (defun describe-syntax-table (table stream)
247 #'(lambda (first last value stream)
248 (if (equal first last)
249 (cond ((vectorp first)
250 (princ (format "%s, row %d\t"
259 (princ (text-char-description first) stream)
260 (princ "\t" stream)))
261 (cond ((vectorp first)
262 (princ (format "%s, rows %d .. %d\t"
269 (princ (format "%s .. %s\t" first last) stream))
271 (princ (format "%s .. %s\t"
272 (text-char-description first)
273 (text-char-description last))
275 (describe-syntax-code value stream))
276 #'(lambda (first last value stream)
277 (let* ((tem (text-char-description first))
279 ;;(limit (cond ((numberp ctl-arrow) ctl-arrow)
280 ;; ((memq ctl-arrow '(t nil)) 256)
286 (princ " .. " stream)
287 (setq tem (text-char-description last))
289 (setq pos (+ pos (length tem) 4))))
290 (while (progn (write-char ?\ stream)
293 (describe-syntax-code value stream)))))
295 #'(lambda (range value)
298 (setq first-char range
301 ((and (equal value prev-val)
303 (and (characterp range)
304 (characterp first-char)
305 (or (not (featurep 'mule))
306 (eq (char-charset range)
307 (char-charset first-char)))
308 (= (char-int last-char) (1- (char-int range))))
311 (eq (aref range 0) (aref first-char 0))
312 (= (aref last-char 1) (1- (aref range 1))))))
313 (setq last-char range))
315 (funcall describe-one first-char last-char prev-val stream)
316 (setq first-char range
322 (funcall describe-one first-char last-char prev-val stream))))
324 (defun describe-syntax-code (code stream)
325 (let ((match (and (consp code) (cdr code)))
326 (invalid (gettext "**invalid**")) ;(empty "") ;constants
327 (standard-output (or stream standard-output))
328 ;; #### I18N3 should temporarily set buffer to output-translatable
329 (in #'(lambda (string)
330 (princ ",\n\t\t\t\t ")
332 (syntax-string (syntax-code-to-string code)))
334 (setq code (car code)))
335 (if (null syntax-string)
337 (princ syntax-string)
338 (princ "\tmeaning: ")
339 (princ (aref ["whitespace" "punctuation" "word-constituent"
340 "symbol-constituent" "open-paren" "close-paren"
341 "expression-prefix" "string-quote" "paired-delimiter"
342 "escape" "character-quote" "comment-begin" "comment-end"
343 "inherit" "extended-word-constituent"]
349 (princ (text-char-description match))))
350 (let* ((spec (elt syntax-string 0))
352 (start1 (/= 0 (logand b3 128))) ;logtest!
353 (start1b (/= 0 (logand b3 64)))
354 (start2 (/= 0 (logand b3 32)))
355 (start2b (/= 0 (logand b3 16)))
356 (end1 (/= 0 (logand b3 8)))
357 (end1b (/= 0 (logand b3 4)))
358 (end2 (/= 0 (logand b3 2)))
359 (end2b (/= 0 (logand b3 1)))
360 (prefix (/= 0 (logand code 128)))
361 (single-char-p (or (= spec ?<) (= spec ?>))))
366 (gettext "first character of comment-start sequence A"))))
369 (gettext "second character of comment-start sequence A")))
374 (gettext "first character of comment-end sequence A"))))
377 (gettext "second character of comment-end sequence A")))
382 (gettext "first character of comment-start sequence B"))))
385 (gettext "second character of comment-start sequence B")))
390 (gettext "first character of comment-end sequence B"))))
393 (gettext "second character of comment-end sequence B")))
396 (gettext "prefix character for `backward-prefix-chars'"))))
399 (defun symbol-near-point ()
400 "Return the first textual item to the nearest point."
402 ;alg stolen from etag.el
404 (if (or (bobp) (not (memq (char-syntax (char-before)) '(?w ?_))))
405 (while (not (looking-at "\\sw\\|\\s_\\|\\'"))
407 (while (looking-at "\\sw\\|\\s_")
409 (if (re-search-backward "\\sw\\|\\s_" nil t)
411 (progn (forward-char 1)
412 (buffer-substring (point)
413 (progn (forward-sexp -1)
414 (while (looking-at "\\s'")
419 ;;; syntax.el ends here