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