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