0
|
1 ;;; vrml-mode.el --- major mode for editing VRML (.wrl) files
|
|
2
|
|
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1996 Ben Wing.
|
|
5
|
|
6 ;; Author: Ben Wing <wing@666.com>
|
|
7 ;; Keywords: languages vrml modes
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
16
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
0
|
25
|
|
26 ;;; Synched up with: Not in FSF.
|
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; Mostly bastardized from tcl.el.
|
|
31
|
|
32 ;; HOW TO INSTALL:
|
|
33 ;; Put the following forms in your .emacs to enable autoloading of VRML
|
|
34 ;; mode, and auto-recognition of ".wrl" files.
|
|
35 ;;
|
|
36 ;; (autoload 'vrml-mode "vrml" "VRML mode." t)
|
|
37 ;; (setq auto-mode-alist (append '(("\\.wrl\\'" . vrml-mode))
|
|
38 ;; auto-mode-alist))
|
|
39 ;;
|
|
40
|
|
41 ;;; Code:
|
|
42
|
|
43 ;;
|
|
44 ;; User variables.
|
|
45 ;;
|
|
46
|
|
47 (defvar vrml-indent-level 3
|
|
48 "*Indentation of VRML statements with respect to containing block.")
|
|
49
|
|
50 (defvar vrml-auto-newline nil
|
|
51 "*Non-nil means automatically newline before and after braces
|
|
52 inserted in VRML code.")
|
|
53
|
|
54 (defvar vrml-tab-always-indent t
|
|
55 "*Control effect of TAB key.
|
|
56 If t (the default), always indent current line.
|
|
57 If nil and point is not in the indentation area at the beginning of
|
|
58 the line, a TAB is inserted.
|
|
59 Other values cause the first possible action from the following list
|
|
60 to take place:
|
|
61
|
|
62 1. Move from beginning of line to correct indentation.
|
|
63 2. Delete an empty comment.
|
|
64 3. Move forward to start of comment, indenting if necessary.
|
|
65 4. Move forward to end of line, indenting if necessary.
|
|
66 5. Create an empty comment.
|
|
67 6. Move backward to start of comment, indenting if necessary.")
|
|
68
|
|
69 (defvar vrml-use-hairy-comment-detector t
|
2
|
70 "*If not `nil', then the more complicated, but slower, comment
|
0
|
71 detecting function is used.")
|
|
72
|
|
73 (defvar vrml-mode-abbrev-table nil
|
|
74 "Abbrev table used while in VRML mode.")
|
|
75 (define-abbrev-table 'vrml-mode-abbrev-table ())
|
|
76
|
|
77 (defvar vrml-mode-map ()
|
|
78 "Keymap used in VRML mode.")
|
|
79 (if (null vrml-mode-map)
|
|
80 (progn
|
|
81 (setq vrml-mode-map (make-sparse-keymap))
|
|
82 (set-keymap-name vrml-mode-map 'vrml-mode-map)
|
|
83 (define-key vrml-mode-map "{" 'vrml-electric-brace)
|
|
84 (define-key vrml-mode-map "}" 'vrml-electric-brace)
|
|
85 (define-key vrml-mode-map "\e\C-q" 'indent-vrml-exp)
|
|
86 (define-key vrml-mode-map "\177" 'backward-delete-char-untabify)
|
|
87 (define-key vrml-mode-map "\t" 'vrml-indent-command)
|
|
88 (define-key vrml-mode-map "\M-;" 'vrml-indent-for-comment)
|
|
89 ))
|
|
90
|
|
91 (defvar vrml-mode-syntax-table nil
|
|
92 "Syntax table in use in vrml-mode buffers.")
|
|
93
|
|
94 (if vrml-mode-syntax-table
|
|
95 ()
|
|
96 (setq vrml-mode-syntax-table (make-syntax-table))
|
|
97 (modify-syntax-entry ?\n ">" vrml-mode-syntax-table)
|
|
98 (modify-syntax-entry ?\f ">" vrml-mode-syntax-table)
|
|
99 (modify-syntax-entry ?\# "<" vrml-mode-syntax-table)
|
|
100 (modify-syntax-entry ?\\ "\\" vrml-mode-syntax-table)
|
|
101 (modify-syntax-entry ?% "_" vrml-mode-syntax-table)
|
|
102 (modify-syntax-entry ?@ "_" vrml-mode-syntax-table)
|
|
103 (modify-syntax-entry ?& "_" vrml-mode-syntax-table)
|
|
104 (modify-syntax-entry ?* "_" vrml-mode-syntax-table)
|
|
105 (modify-syntax-entry ?- "_" vrml-mode-syntax-table)
|
|
106 (modify-syntax-entry ?: "_" vrml-mode-syntax-table)
|
|
107 (modify-syntax-entry ?! "_" vrml-mode-syntax-table)
|
|
108 (modify-syntax-entry ?$ "_" vrml-mode-syntax-table)
|
|
109 (modify-syntax-entry ?/ "_" vrml-mode-syntax-table)
|
|
110 (modify-syntax-entry ?~ "_" vrml-mode-syntax-table)
|
|
111 (modify-syntax-entry ?< "_" vrml-mode-syntax-table)
|
|
112 (modify-syntax-entry ?= "_" vrml-mode-syntax-table)
|
|
113 (modify-syntax-entry ?> "_" vrml-mode-syntax-table)
|
|
114 (modify-syntax-entry ?| "_" vrml-mode-syntax-table)
|
|
115 (modify-syntax-entry ?+ "." vrml-mode-syntax-table)
|
|
116 (modify-syntax-entry ?\' "\"" vrml-mode-syntax-table))
|
|
117
|
|
118 (defvar vrml-mode-hook nil
|
|
119 "Hook run on entry to VRML mode.")
|
|
120
|
|
121 (defvar vrml-keyword-list
|
|
122 '(
|
|
123 ; shape nodes:
|
|
124 "AsciiText" "Cone" "Cube" "Cylinder" "IndexedFaceSet" "IndexedLineSet"
|
|
125 "PointSet" "Sphere"
|
|
126 ; geometry and material nodes:
|
|
127 "Coordinate3" "FontStyle" "Info" "LOD" "Material" "MaterialBinding"
|
|
128 "Normal" "NormalBinding" "Texture2" "Texture2Transform"
|
|
129 "TextureCoordinate2" "ShapeHints"
|
|
130 ; transformation nodes:
|
|
131 "MatrixTransform" "Rotation" "Scale" "Transform" "Translation"
|
|
132 ;camera nodes:
|
|
133 "OrthographicCamera" "PerspectiveCamera"
|
|
134 ;lighting nodes:
|
|
135 "DirectionalLight" "PointLight" "SpotLight"
|
|
136 ;group nodes:
|
|
137 "Group" "Separator" "Switch" "TransformSeparator" "WWWAnchor"
|
|
138 ;other:
|
|
139 "WWWInline"
|
|
140 ;new VRML 2.0 nodes (#### not yet classified)
|
|
141 "Anchor" "Appearance" "AudioClip" "Background" "Billboard" "Box"
|
|
142 "Collision" "Color" "ColorInterpolator" "Coordinate"
|
|
143 "CoordinateInterpolator" "CylinderSensor" "DiskSensor" "ElevationGrid"
|
|
144 "Extrusion" "Fog" "FontStyle" "ImageTexture" "Inline" "MovieTexture"
|
|
145 "NavigationInfo" "NormalInterpolator" "OrientationInterpolator"
|
|
146 "PixelTexture" "PlaneSensor" "PositionInterpolator" "ProximitySensor"
|
|
147 "ScalarInterpolator" "Script" "Shape" "Sound" "SphereSensor" "Text"
|
|
148 "TextureTransform" "TextureCoordinate" "TimeSensor" "TouchSensor"
|
|
149 "Viewpoint" "VisibilitySensor" "WorldInfo"
|
|
150 ;VRML 2.0 node fields
|
|
151 "eventIn" "eventOut" "field" "exposedField"
|
|
152 ;misc. VRML 2.0 keywords (DEF, PROTO, EXTERNPROTO handled below)
|
|
153 "USE" "ROUTE" "TO" "IS" "TRUE" "FALSE" "NULL"
|
|
154 ))
|
|
155
|
|
156 (defconst vrml-font-lock-keywords
|
|
157 (list
|
|
158 ;; Names of functions (and other "defining things").
|
|
159 (list "\\(DEF\\|PROTO\\|EXTERNPROTO\\)[ \t\n]+\\([^ \t\n]+\\)"
|
|
160 2 'font-lock-function-name-face)
|
|
161
|
|
162 ;; Keywords. Only recognized if surrounded by whitespace.
|
|
163 ;; FIXME consider using "not word or symbol", not
|
|
164 ;; "whitespace".
|
|
165 (cons (concat "\\(\\s-\\|^\\)\\("
|
|
166 ;; FIXME Use regexp-quote?
|
|
167 (mapconcat 'identity vrml-keyword-list "\\|")
|
|
168 "\\)\\(\\s-\\|$\\)")
|
|
169 2)
|
|
170 )
|
|
171 "Keywords to highlight for VRML. See variable `font-lock-keywords'.")
|
|
172
|
|
173 ;;;###autoload
|
|
174 (defun vrml-mode ()
|
|
175 "Major mode for editing VRML code.
|
|
176 Expression and list commands understand all VRML brackets.
|
|
177 Tab indents for VRML code.
|
|
178 Paragraphs are separated by blank lines only.
|
|
179 Delete converts tabs to spaces as it moves back.
|
|
180
|
|
181 Variables controlling indentation style:
|
|
182 vrml-indent-level
|
|
183 Indentation of VRML statements within surrounding block.
|
|
184
|
|
185 Variables controlling user interaction with mode (see variable
|
|
186 documentation for details):
|
|
187 vrml-tab-always-indent
|
|
188 Controls action of TAB key.
|
|
189 vrml-auto-newline
|
|
190 Non-nil means automatically newline before and after braces
|
|
191 inserted in VRML code.
|
|
192
|
|
193 Turning on VRML mode calls the value of the variable `vrml-mode-hook'
|
|
194 with no args, if that value is non-nil. Read the documentation for
|
|
195 `vrml-mode-hook' to see what kinds of interesting hook functions
|
|
196 already exist.
|
|
197
|
|
198 Commands:
|
|
199 \\{vrml-mode-map}"
|
|
200 (interactive)
|
|
201 (kill-all-local-variables)
|
|
202 (use-local-map vrml-mode-map)
|
|
203 (setq major-mode 'vrml-mode)
|
|
204 (setq mode-name "VRML")
|
|
205 (setq local-abbrev-table vrml-mode-abbrev-table)
|
|
206 (set-syntax-table vrml-mode-syntax-table)
|
|
207
|
|
208 (make-local-variable 'paragraph-start)
|
|
209 (make-local-variable 'paragraph-separate)
|
|
210 (if (fboundp 'move-to-left-margin)
|
|
211 (progn
|
|
212 ;; In FSF Emacs 19.29 / XEmacs 19.14, you aren't supposed to
|
|
213 ;; start these with a ^.
|
|
214 (setq paragraph-start "$\\|")
|
|
215 (setq paragraph-separate paragraph-start))
|
|
216 (setq paragraph-start (concat "^$\\|" page-delimiter))
|
|
217 (setq paragraph-separate paragraph-start))
|
|
218 (make-local-variable 'paragraph-ignore-fill-prefix)
|
|
219 (setq paragraph-ignore-fill-prefix t)
|
|
220 (make-local-variable 'fill-paragraph-function)
|
|
221 (setq fill-paragraph-function 'vrml-do-fill-paragraph)
|
|
222
|
|
223 (make-local-variable 'indent-line-function)
|
|
224 (setq indent-line-function 'vrml-indent-line)
|
|
225 (make-local-variable 'require-final-newline)
|
|
226 (setq require-final-newline t)
|
|
227
|
|
228 (make-local-variable 'comment-start)
|
|
229 (setq comment-start "# ")
|
|
230 (make-local-variable 'comment-start-skip)
|
|
231 (setq comment-start-skip "#+ *")
|
|
232 (make-local-variable 'comment-column)
|
|
233 (setq comment-column 40)
|
|
234 (make-local-variable 'comment-end)
|
|
235 (setq comment-end "")
|
|
236
|
|
237 (make-local-variable 'outline-regexp)
|
|
238 (setq outline-regexp "[^\n\^M]")
|
|
239 (make-local-variable 'outline-level)
|
|
240 (setq outline-level 'vrml-outline-level)
|
|
241
|
|
242 (make-local-variable 'font-lock-keywords)
|
|
243 (setq font-lock-keywords vrml-font-lock-keywords)
|
|
244
|
|
245 (make-local-variable 'parse-sexp-ignore-comments)
|
|
246 (setq parse-sexp-ignore-comments t)
|
|
247
|
|
248 (make-local-variable 'defun-prompt-regexp)
|
|
249 (setq defun-prompt-regexp "^[^ \t\n#}][^\n}]+}*[ \t]+")
|
|
250
|
|
251 ;; Settings for new dabbrev code.
|
|
252 (make-local-variable 'dabbrev-case-fold-search)
|
|
253 (setq dabbrev-case-fold-search nil)
|
|
254 (make-local-variable 'dabbrev-case-replace)
|
|
255 (setq dabbrev-case-replace nil)
|
|
256 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
|
|
257 (setq dabbrev-abbrev-skip-leading-regexp "[$!]")
|
|
258 (make-local-variable 'dabbrev-abbrev-char-regexp)
|
|
259 (setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_")
|
|
260
|
|
261 (run-hooks 'vrml-mode-hook))
|
|
262
|
|
263 ;; This is used for closing braces. If vrml-auto-newline is set, can
|
|
264 ;; insert a newline both before and after the brace, depending on
|
|
265 ;; context. FIXME should this be configurable? Does anyone use this?
|
|
266 (defun vrml-electric-brace (arg)
|
|
267 "Insert character and correct line's indentation."
|
|
268 (interactive "p")
|
|
269 ;; If auto-newlining and there is stuff on the same line, insert a
|
|
270 ;; newline first.
|
|
271 (if vrml-auto-newline
|
|
272 (progn
|
|
273 (if (save-excursion
|
|
274 (skip-chars-backward " \t")
|
|
275 (bolp))
|
|
276 ()
|
|
277 (vrml-indent-line)
|
|
278 (newline))
|
|
279 ;; In auto-newline case, must insert a newline after each
|
|
280 ;; brace. So an explicit loop is needed.
|
|
281 (while (> arg 0)
|
|
282 (insert last-command-char)
|
|
283 (vrml-indent-line)
|
|
284 (newline)
|
|
285 (setq arg (1- arg))))
|
|
286 (self-insert-command arg))
|
|
287 (vrml-indent-line))
|
|
288
|
|
289
|
|
290
|
|
291 (defun vrml-indent-command (&optional arg)
|
|
292 "Indent current line as VRML code, or in some cases insert a tab character.
|
|
293 If vrml-tab-always-indent is t (the default), always indent current line.
|
|
294 If vrml-tab-always-indent is nil and point is not in the indentation
|
|
295 area at the beginning of the line, a TAB is inserted.
|
|
296 Other values of vrml-tab-always-indent cause the first possible action
|
|
297 from the following list to take place:
|
|
298
|
|
299 1. Move from beginning of line to correct indentation.
|
|
300 2. Delete an empty comment.
|
|
301 3. Move forward to start of comment, indenting if necessary.
|
|
302 4. Move forward to end of line, indenting if necessary.
|
|
303 5. Create an empty comment.
|
|
304 6. Move backward to start of comment, indenting if necessary."
|
|
305 (interactive "p")
|
|
306 (cond
|
|
307 ((not vrml-tab-always-indent)
|
|
308 ;; Indent if in indentation area, otherwise insert TAB.
|
|
309 (if (<= (current-column) (current-indentation))
|
|
310 (vrml-indent-line)
|
|
311 (self-insert-command arg)))
|
|
312 ((eq vrml-tab-always-indent t)
|
|
313 ;; Always indent.
|
|
314 (vrml-indent-line))
|
|
315 (t
|
|
316 ;; "Perl-mode" style TAB command.
|
|
317 (let* ((ipoint (point))
|
|
318 (eolpoint (progn
|
|
319 (end-of-line)
|
|
320 (point)))
|
|
321 (comment-p (vrml-in-comment)))
|
|
322 (cond
|
|
323 ((= ipoint (save-excursion
|
|
324 (beginning-of-line)
|
|
325 (point)))
|
|
326 (beginning-of-line)
|
|
327 (vrml-indent-line)
|
|
328 ;; If indenting didn't leave us in column 0, go to the
|
|
329 ;; indentation. Otherwise leave point at end of line. This
|
|
330 ;; is a hack.
|
|
331 (if (= (point) (save-excursion
|
|
332 (beginning-of-line)
|
|
333 (point)))
|
|
334 (end-of-line)
|
|
335 (back-to-indentation)))
|
|
336 ((and comment-p (looking-at "[ \t]*$"))
|
|
337 ;; Empty comment, so delete it. We also delete any ";"
|
|
338 ;; characters at the end of the line. I think this is
|
|
339 ;; friendlier, but I don't know how other people will feel.
|
|
340 (backward-char)
|
|
341 (skip-chars-backward " \t;")
|
|
342 (delete-region (point) eolpoint))
|
|
343 ((and comment-p (< ipoint (point)))
|
|
344 ;; Before comment, so skip to it.
|
|
345 (vrml-indent-line)
|
|
346 (indent-for-comment))
|
|
347 ((/= ipoint eolpoint)
|
|
348 ;; Go to end of line (since we're not there yet).
|
|
349 (goto-char eolpoint)
|
|
350 (vrml-indent-line))
|
|
351 ((not comment-p)
|
|
352 (vrml-indent-line)
|
|
353 (vrml-indent-for-comment))
|
|
354 (t
|
|
355 ;; Go to start of comment. We don't leave point where it is
|
|
356 ;; because we want to skip comment-start-skip.
|
|
357 (vrml-indent-line)
|
|
358 (indent-for-comment)))))))
|
|
359
|
|
360 (defun vrml-indent-line ()
|
|
361 "Indent current line as VRML code.
|
|
362 Return the amount the indentation changed by."
|
|
363 (let ((indent (calculate-vrml-indent nil))
|
|
364 beg shift-amt
|
|
365 (case-fold-search nil)
|
|
366 (pos (- (point-max) (point))))
|
|
367 (beginning-of-line)
|
|
368 (setq beg (point))
|
|
369 (cond ((eq indent nil)
|
|
370 (setq indent (current-indentation)))
|
|
371 (t
|
|
372 (skip-chars-forward " \t")
|
|
373 (if (listp indent) (setq indent (car indent)))
|
|
374 (cond ((= (following-char) ?})
|
|
375 (setq indent (- indent vrml-indent-level)))
|
|
376 ((= (following-char) ?\])
|
|
377 (setq indent (- indent 1))))))
|
|
378 (skip-chars-forward " \t")
|
|
379 (setq shift-amt (- indent (current-column)))
|
|
380 (if (zerop shift-amt)
|
|
381 (if (> (- (point-max) pos) (point))
|
|
382 (goto-char (- (point-max) pos)))
|
|
383 (delete-region beg (point))
|
|
384 (indent-to indent)
|
|
385 ;; If initial point was within line's indentation,
|
|
386 ;; position after the indentation. Else stay at same point in text.
|
|
387 (if (> (- (point-max) pos) (point))
|
|
388 (goto-char (- (point-max) pos))))
|
|
389 shift-amt))
|
|
390
|
|
391 (defun calculate-vrml-indent (&optional parse-start)
|
|
392 "Return appropriate indentation for current line as VRML code.
|
|
393 In usual case returns an integer: the column to indent to.
|
|
394 Returns nil if line starts inside a string, t if in a comment."
|
|
395 (save-excursion
|
|
396 (beginning-of-line)
|
|
397 (let* ((indent-point (point))
|
|
398 (case-fold-search nil)
|
|
399 state
|
|
400 containing-sexp
|
|
401 found-next-line)
|
|
402 (if parse-start
|
|
403 (goto-char parse-start)
|
|
404 (beginning-of-defun))
|
|
405 (while (< (point) indent-point)
|
|
406 (setq parse-start (point))
|
|
407 (setq state (parse-partial-sexp (point) indent-point 0))
|
|
408 (setq containing-sexp (car (cdr state))))
|
|
409 (cond ((or (nth 3 state) (nth 4 state))
|
|
410 ;; Inside comment or string. Return nil or t if should
|
|
411 ;; not change this line
|
|
412 (nth 4 state))
|
|
413 ((null containing-sexp)
|
|
414 ;; Line is at top level.
|
|
415 0)
|
|
416 (t
|
|
417 (goto-char containing-sexp)
|
|
418 (let* ((expr-start (point)))
|
|
419 ;; Find the first statement in the block and indent
|
|
420 ;; like it. The first statement in the block might be
|
|
421 ;; on the same line, so what we do is skip all
|
|
422 ;; "virtually blank" lines, looking for a non-blank
|
|
423 ;; one. A line is virtually blank if it only contains
|
|
424 ;; a comment and whitespace. We do it this funky way
|
|
425 ;; because we want to know if we've found a statement
|
|
426 ;; on some line _after_ the line holding the sexp
|
|
427 ;; opener.
|
|
428 (goto-char containing-sexp)
|
|
429 (forward-char)
|
|
430 (if (and (< (point) indent-point)
|
|
431 (looking-at "[ \t]*\\(#.*\\)?$"))
|
|
432 (progn
|
|
433 (forward-line)
|
|
434 (while (and (< (point) indent-point)
|
|
435 (looking-at "[ \t]*\\(#.*\\)?$"))
|
|
436 (setq found-next-line t)
|
|
437 (forward-line))))
|
|
438 (if (not (or (= (char-after containing-sexp) ?{)
|
|
439 (and (= (char-after containing-sexp) ?\[)
|
|
440 (save-excursion
|
|
441 (goto-char containing-sexp)
|
|
442 (skip-chars-backward " \t\n")
|
|
443 (forward-char -8)
|
|
444 (looking-at "children")))))
|
|
445 (progn
|
|
446 ;; Line is continuation line, or the sexp opener
|
2
|
447 ;; is not a curly brace, or we are looking at
|
0
|
448 ;; an `expr' expression (which must be split
|
|
449 ;; specially). So indentation is column of first
|
|
450 ;; good spot after sexp opener. If there is no
|
|
451 ;; nonempty line before the indentation point, we
|
|
452 ;; use the column of the character after the sexp
|
|
453 ;; opener.
|
|
454 (if (>= (point) indent-point)
|
|
455 (progn
|
|
456 (goto-char containing-sexp)
|
|
457 (forward-char))
|
|
458 (skip-chars-forward " \t"))
|
|
459 (current-column))
|
|
460 ;; After a curly brace, and not a continuation line.
|
|
461 ;; So take indentation from first good line after
|
|
462 ;; start of block, unless that line is on the same
|
|
463 ;; line as the opening brace. In this case use the
|
|
464 ;; indentation of the opening brace's line, plus
|
|
465 ;; another indent step. If we are in the body part
|
|
466 ;; of an "if" or "while" then the indentation is
|
|
467 ;; taken from the line holding the start of the
|
|
468 ;; statement.
|
|
469 (if (and (< (point) indent-point)
|
|
470 found-next-line)
|
|
471 (current-indentation)
|
|
472 (if t ; commands-p
|
|
473 (goto-char expr-start)
|
|
474 (goto-char containing-sexp))
|
|
475 (+ (current-indentation) vrml-indent-level)))))))))
|
|
476
|
|
477
|
|
478
|
|
479 (defun indent-vrml-exp ()
|
|
480 "Indent each line of the VRML grouping following point."
|
|
481 (interactive)
|
|
482 (let ((indent-stack (list nil))
|
|
483 (contain-stack (list (point)))
|
|
484 (case-fold-search nil)
|
|
485 outer-loop-done inner-loop-done state ostate
|
|
486 this-indent last-sexp
|
|
487 (next-depth 0)
|
|
488 last-depth)
|
|
489 (save-excursion
|
|
490 (forward-sexp 1))
|
|
491 (save-excursion
|
|
492 (setq outer-loop-done nil)
|
|
493 (while (and (not (eobp)) (not outer-loop-done))
|
|
494 (setq last-depth next-depth)
|
|
495 ;; Compute how depth changes over this line
|
|
496 ;; plus enough other lines to get to one that
|
|
497 ;; does not end inside a comment or string.
|
|
498 ;; Meanwhile, do appropriate indentation on comment lines.
|
|
499 (setq inner-loop-done nil)
|
|
500 (while (and (not inner-loop-done)
|
|
501 (not (and (eobp) (setq outer-loop-done t))))
|
|
502 (setq ostate state)
|
|
503 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
|
|
504 nil nil state))
|
|
505 (setq next-depth (car state))
|
|
506 (if (and (car (cdr (cdr state)))
|
|
507 (>= (car (cdr (cdr state))) 0))
|
|
508 (setq last-sexp (car (cdr (cdr state)))))
|
|
509 (if (or (nth 4 ostate))
|
|
510 (vrml-indent-line))
|
|
511 (if (or (nth 3 state))
|
|
512 (forward-line 1)
|
|
513 (setq inner-loop-done t)))
|
|
514 (if (<= next-depth 0)
|
|
515 (setq outer-loop-done t))
|
|
516 (if outer-loop-done
|
|
517 nil
|
|
518 ;; If this line had ..))) (((.. in it, pop out of the levels
|
|
519 ;; that ended anywhere in this line, even if the final depth
|
|
520 ;; doesn't indicate that they ended.
|
|
521 (while (> last-depth (nth 6 state))
|
|
522 (setq indent-stack (cdr indent-stack)
|
|
523 contain-stack (cdr contain-stack)
|
|
524 last-depth (1- last-depth)))
|
|
525 (if (/= last-depth next-depth)
|
|
526 (setq last-sexp nil))
|
|
527 ;; Add levels for any parens that were started in this line.
|
|
528 (while (< last-depth next-depth)
|
|
529 (setq indent-stack (cons nil indent-stack)
|
|
530 contain-stack (cons nil contain-stack)
|
|
531 last-depth (1+ last-depth)))
|
|
532 (if (null (car contain-stack))
|
|
533 (setcar contain-stack
|
|
534 (or (car (cdr state))
|
|
535 (save-excursion
|
|
536 (forward-sexp -1)
|
|
537 (point)))))
|
|
538 (forward-line 1)
|
|
539 (skip-chars-forward " \t")
|
|
540 (if (eolp)
|
|
541 nil
|
|
542 (if (and (car indent-stack)
|
|
543 (>= (car indent-stack) 0))
|
|
544 ;; Line is on an existing nesting level.
|
|
545 (setq this-indent (car indent-stack))
|
|
546 ;; Just started a new nesting level.
|
|
547 ;; Compute the standard indent for this level.
|
|
548 (let ((val (calculate-vrml-indent
|
|
549 (if (car indent-stack)
|
|
550 (- (car indent-stack))))))
|
|
551 (setcar indent-stack
|
|
552 (setq this-indent val))
|
|
553 ))
|
|
554 (cond ((not (numberp this-indent)))
|
|
555 ((= (following-char) ?})
|
|
556 (setq this-indent (- this-indent vrml-indent-level)))
|
|
557 ((= (following-char) ?\])
|
|
558 (setq this-indent (- this-indent 1))))
|
|
559 ;; Put chosen indentation into effect.
|
|
560 (or (null this-indent)
|
|
561 (= (current-column)
|
|
562 this-indent)
|
|
563 (progn
|
|
564 (delete-region (point) (progn (beginning-of-line) (point)))
|
|
565 (indent-to
|
|
566 this-indent))))))))
|
|
567 )
|
|
568
|
|
569 ;;
|
|
570 ;; Auto-fill support.
|
|
571 ;;
|
|
572
|
|
573 (defun vrml-real-command-p ()
|
|
574 "Return nil if point is not at the beginning of a command.
|
|
575 A command is the first word on an otherwise empty line, or the
|
|
576 first word following an opening brace."
|
|
577 (save-excursion
|
|
578 (skip-chars-backward " \t")
|
|
579 (cond
|
|
580 ((bobp) t)
|
|
581 ((bolp)
|
|
582 (backward-char)
|
|
583 ;; Note -- continued comments are not supported here. I
|
|
584 ;; consider those to be a wart on the language.
|
|
585 (not (eq ?\\ (preceding-char))))
|
|
586 (t
|
|
587 (memq (preceding-char) '(?{))))))
|
|
588
|
|
589 ;; FIXME doesn't actually return t. See last case.
|
|
590 (defun vrml-real-comment-p ()
|
|
591 "Return t if point is just after the `#' beginning a real comment.
|
|
592 Does not check to see if previous char is actually `#'.
|
|
593 A real comment is either at the beginning of the buffer,
|
30
|
594 preceded only by whitespace on the line, or has a preceding
|
0
|
595 semicolon, opening brace, or opening bracket on the same line."
|
|
596 (save-excursion
|
|
597 (backward-char)
|
|
598 (vrml-real-command-p)))
|
|
599
|
|
600 (defun vrml-hairy-scan-for-comment (state end always-stop)
|
|
601 "Determine if point is in a comment.
|
|
602 Returns a list of the form `(FLAG . STATE)'. STATE can be used
|
|
603 as input to future invocations. FLAG is nil if not in comment,
|
|
604 t otherwise. If in comment, leaves point at beginning of comment.
|
|
605 See also `vrml-simple-scan-for-comment', a simpler version that is
|
|
606 often right."
|
|
607 (let ((bol (save-excursion
|
|
608 (goto-char end)
|
|
609 (beginning-of-line)
|
|
610 (point)))
|
|
611 real-comment
|
|
612 last-cstart)
|
|
613 (while (and (not last-cstart) (< (point) end))
|
|
614 (setq real-comment nil) ;In case we've looped around and it is
|
|
615 ;set.
|
|
616 (setq state (parse-partial-sexp (point) end nil nil state t))
|
|
617 (if (nth 4 state)
|
|
618 (progn
|
|
619 ;; If ALWAYS-STOP is set, stop even if we don't have a
|
|
620 ;; real comment, or if the comment isn't on the same line
|
|
621 ;; as the end.
|
|
622 (if always-stop (setq last-cstart (point)))
|
|
623 ;; If we have a real comment, then set the comment
|
|
624 ;; starting point if we are on the same line as the ending
|
|
625 ;; location.
|
|
626 (setq real-comment (vrml-real-comment-p))
|
|
627 (if real-comment
|
|
628 (progn
|
|
629 (and (> (point) bol) (setq last-cstart (point)))
|
|
630 ;; NOTE Emacs 19 has a misfeature whereby calling
|
|
631 ;; parse-partial-sexp with COMMENTSTOP set and with
|
|
632 ;; an initial list that says point is in a comment
|
|
633 ;; will cause an immediate return. So we must skip
|
|
634 ;; over the comment ourselves.
|
|
635 (beginning-of-line 2)))
|
|
636 ;; Frob the state to make it look like we aren't in a
|
|
637 ;; comment.
|
|
638 (setcar (nthcdr 4 state) nil))))
|
|
639 (and last-cstart
|
|
640 (goto-char last-cstart))
|
|
641 (cons real-comment state)))
|
|
642
|
|
643 (defun vrml-hairy-in-comment ()
|
|
644 "Return t if point is in a comment, and leave point at beginning
|
|
645 of comment."
|
|
646 (let ((save (point)))
|
|
647 (beginning-of-defun)
|
|
648 (car (vrml-hairy-scan-for-comment nil save nil))))
|
|
649
|
|
650 (defun vrml-simple-in-comment ()
|
|
651 "Return t if point is in comment, and leave point at beginning
|
|
652 of comment. This is faster than `vrml-hairy-in-comment', but is
|
|
653 correct less often."
|
|
654 (let ((save (point))
|
|
655 comment)
|
|
656 (beginning-of-line)
|
|
657 (while (and (< (point) save) (not comment))
|
|
658 (search-forward "#" save 'move)
|
|
659 (setq comment (vrml-real-comment-p)))
|
|
660 comment))
|
|
661
|
|
662 (defun vrml-in-comment ()
|
|
663 "Return t if point is in comment, and leave point at beginning
|
|
664 of comment."
|
|
665 (if vrml-use-hairy-comment-detector
|
|
666 (vrml-hairy-in-comment)
|
|
667 (vrml-simple-in-comment)))
|
|
668
|
|
669 (defun vrml-do-fill-paragraph (ignore)
|
|
670 "fill-paragraph function for VRML mode. Only fills in a comment."
|
|
671 (let (in-comment col where)
|
|
672 (save-excursion
|
|
673 (end-of-line)
|
|
674 (setq in-comment (vrml-in-comment))
|
|
675 (if in-comment
|
|
676 (progn
|
|
677 (setq where (1+ (point)))
|
|
678 (setq col (1- (current-column))))))
|
|
679 (and in-comment
|
|
680 (save-excursion
|
|
681 (back-to-indentation)
|
|
682 (= col (current-column)))
|
|
683 ;; In a comment. Set the fill prefix, and find the paragraph
|
|
684 ;; boundaries by searching for lines that look like
|
|
685 ;; comment-only lines.
|
|
686 (let ((fill-prefix (buffer-substring (progn
|
|
687 (beginning-of-line)
|
|
688 (point))
|
|
689 where))
|
|
690 p-start p-end)
|
|
691 ;; Search backwards.
|
|
692 (save-excursion
|
|
693 (while (looking-at "^[ \t]*#")
|
|
694 (forward-line -1))
|
|
695 (forward-line)
|
|
696 (setq p-start (point)))
|
|
697
|
|
698 ;; Search forwards.
|
|
699 (save-excursion
|
|
700 (while (looking-at "^[ \t]*#")
|
|
701 (forward-line))
|
|
702 (setq p-end (point)))
|
|
703
|
|
704 ;; Narrow and do the fill.
|
|
705 (save-restriction
|
|
706 (narrow-to-region p-start p-end)
|
|
707 (fill-paragraph ignore)))))
|
|
708 t)
|
|
709
|
|
710 (defun vrml-do-auto-fill ()
|
|
711 "Auto-fill function for VRML mode. Only auto-fills in a comment."
|
|
712 (if (> (current-column) fill-column)
|
|
713 (let ((fill-prefix "# ")
|
|
714 in-comment col)
|
|
715 (save-excursion
|
|
716 (setq in-comment (vrml-in-comment))
|
|
717 (if in-comment
|
|
718 (setq col (1- (current-column)))))
|
|
719 (if in-comment
|
|
720 (progn
|
|
721 (do-auto-fill)
|
|
722 (save-excursion
|
|
723 (back-to-indentation)
|
|
724 (delete-region (point) (save-excursion
|
|
725 (beginning-of-line)
|
|
726 (point)))
|
|
727 (indent-to-column col)))))))
|
|
728
|
|
729 (defun vrml-indent-for-comment ()
|
|
730 "Indent this line's comment to comment column, or insert an empty comment.
|
|
731 Is smart about syntax of VRML comments.
|
|
732 Parts of this were taken from indent-for-comment (simple.el)."
|
|
733 (interactive "*")
|
|
734 (end-of-line)
|
|
735 (or (vrml-in-comment)
|
|
736 (progn
|
|
737 ;; Not in a comment, so we have to insert one. Create an
|
|
738 ;; empty comment (since there isn't one on this line).
|
|
739 (skip-chars-backward " \t")
|
|
740 (let ((eolpoint (point)))
|
|
741 (beginning-of-line)
|
|
742 (if (/= (point) eolpoint)
|
|
743 (progn
|
|
744 (goto-char eolpoint)
|
|
745 (insert
|
|
746 "# ")
|
|
747 (backward-char))))))
|
|
748 ;; Point is just after the "#" starting a comment. Move it as
|
|
749 ;; appropriate.
|
|
750 (let* ((indent (funcall comment-indent-function))
|
|
751 (begpos (progn
|
|
752 (backward-char)
|
|
753 (point))))
|
|
754 (if (/= begpos indent)
|
|
755 (progn
|
|
756 (skip-chars-backward " \t" (save-excursion
|
|
757 (beginning-of-line)
|
|
758 (point)))
|
|
759 (delete-region (point) begpos)
|
|
760 (indent-to indent)))
|
|
761 (looking-at comment-start-skip) ; Always true.
|
|
762 (goto-char (match-end 0))
|
|
763 ;; I don't like the effect of the next two.
|
|
764 ;;(skip-chars-backward " \t" (match-beginning 0))
|
|
765 ;;(skip-chars-backward "^ \t" (match-beginning 0))
|
|
766 ))
|
|
767
|
|
768 ;;; vrml-mode.el ends here
|