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