1 ;;; sgml-mode.el --- SGML-editing mode
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: James Clark <jjc@clark.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 1, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; Some suggestions for your .emacs file:
29 ;; (autoload 'sgml-mode "sgml-mode" "SGML mode" t)
31 ;; (setq auto-mode-alist
32 ;; (append (list (cons "\\.sgm$" 'sgml-mode)
33 ;; (cons "\\.sgml$" 'sgml-mode)
34 ;; (cons "\\.dtd$" 'sgml-mode))
42 ;;; sgmls is a free SGML parser available from
43 ;;; ftp.uu.net:pub/text-processing/sgml
44 ;;; Its error messages can be parsed by next-error.
45 ;;; The -s option suppresses output.
47 (defconst sgml-validate-command
49 "*The command to validate an SGML document.
50 The file name of current buffer file name will be appended to this,
51 separated by a space.")
53 (defvar sgml-saved-validate-command nil
54 "The command last used to validate in this buffer.")
56 (defvar sgml-mode-map nil
"Keymap for SGML mode")
60 (setq sgml-mode-map
(make-sparse-keymap))
61 (define-key sgml-mode-map
">" 'sgml-close-angle
)
62 (define-key sgml-mode-map
"/" 'sgml-slash
)
63 (define-key sgml-mode-map
"\C-c\C-v" 'sgml-validate
))
66 "Major mode for editing SGML.
67 Makes > display the matching <. Makes / display matching /.
68 Use \\[sgml-validate] to validate your document with an SGML parser."
70 (kill-all-local-variables)
71 (setq local-abbrev-table text-mode-abbrev-table
)
72 (use-local-map sgml-mode-map
)
73 (setq mode-name
"SGML")
74 (setq major-mode
'sgml-mode
)
75 (make-local-variable 'paragraph-start
)
76 ;; A start or end tag by itself on a line separates a paragraph.
77 ;; This is desirable because SGML discards a newline that appears
78 ;; immediately after a start tag or immediately before an end tag.
81 \\(</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$\\)")
82 (make-local-variable 'paragraph-separate
)
83 (setq paragraph-separate
85 ^</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$")
86 (make-local-variable 'sgml-saved-validate-command
)
87 (set-syntax-table text-mode-syntax-table
)
88 (make-local-variable 'comment-start
)
89 (setq comment-start
"<!-- ")
90 (make-local-variable 'comment-end
)
91 (setq comment-end
" -->")
92 (make-local-variable 'comment-indent-hook
)
93 (setq comment-indent-hook
'sgml-comment-indent
)
94 (make-local-variable 'comment-start-skip
)
95 ;; This will allow existing comments within declarations to be
97 (setq comment-start-skip
"--[ \t]*")
98 (run-hooks 'text-mode-hook
'sgml-mode-hook
))
100 (defun sgml-comment-indent ()
101 (if (and (looking-at "--")
102 (not (and (eq (char-after (1- (point))) ?
!)
103 (eq (char-after (- (point) 2)) ?
<))))
105 (skip-chars-backward " \t")
106 (max comment-column
(1+ (current-column))))
109 (defconst sgml-start-tag-regex
110 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
111 "Regular expression that matches a non-empty start tag.
112 Any terminating > or / is not matched.")
114 (defvar sgml-mode-markup-syntax-table nil
115 "Syntax table used for scanning SGML markup.")
117 (if sgml-mode-markup-syntax-table
119 (setq sgml-mode-markup-syntax-table
(make-syntax-table))
120 (modify-syntax-entry ?
< "(>" sgml-mode-markup-syntax-table
)
121 (modify-syntax-entry ?
> ")<" sgml-mode-markup-syntax-table
)
122 (modify-syntax-entry ?-
"_ 1234" sgml-mode-markup-syntax-table
)
123 (modify-syntax-entry ?
\' "\"" sgml-mode-markup-syntax-table
))
125 (defconst sgml-angle-distance
4000
126 "*If non-nil, is the maximum distance to search for matching <
127 when > is inserted.")
129 (defun sgml-close-angle (arg)
130 "Insert > and display matching <."
134 (let ((oldpos (point))
138 (if sgml-angle-distance
139 (narrow-to-region (max (point-min)
140 (- (point) sgml-angle-distance
))
142 ;; See if it's the end of a marked section.
143 (and (> (- (point) (point-min)) 3)
144 (eq (char-after (- (point) 2)) ?\
])
145 (eq (char-after (- (point) 3)) ?\
])
146 (re-search-backward "<!\\[\\(-?[A-Za-z0-9. \t\n&;]\\|\
147 --\\([^-]\\|-[^-]\\)*--\\)*\\["
150 (let ((msspos (point)))
151 (if (and (search-forward "]]>" oldpos t
)
153 (setq blinkpos msspos
))))
154 ;; This handles cases where the > ends one of the following:
155 ;; markup declaration starting with <! (possibly including a
156 ;; declaration subset); start tag; end tag; SGML declaration.
161 (let ((oldtable (syntax-table))
162 (parse-sexp-ignore-comments t
))
165 (set-syntax-table sgml-mode-markup-syntax-table
)
166 (setq blinkpos
(scan-sexps oldpos -
1)))
167 (set-syntax-table oldtable
)))
172 ;; Check that it's a valid delimiter in context.
174 "<\\(\\?\\|/?[A-Za-z>]\\|!\\([[A-Za-z]\\|--\\)\\)"))
175 ;; Check that it's not a net-enabling start tag
176 ;; nor an unclosed start-tag.
177 (looking-at (concat sgml-start-tag-regex
"[/<]"))
178 ;; Nor an unclosed end-tag.
179 (looking-at "</[A-Za-z][-.A-Za-z0-9]*[ \t]*<"))
180 (setq blinkpos nil
)))
183 ;; See if it's the end of a processing instruction.
185 (if (search-backward "<?" (point-min) t
)
186 (let ((pipos (point)))
187 (if (and (search-forward ">" oldpos t
)
189 (setq blinkpos pipos
))))))
193 (if (pos-visible-in-window-p)
195 (message "Matches %s"
196 (buffer-substring blinkpos
200 ;;; I doubt that null end tags are used much for large elements,
201 ;;; so use a small distance here.
202 (defconst sgml-slash-distance
1000
203 "*If non-nil, is the maximum distance to search for matching /
204 when / is inserted.")
206 (defun sgml-slash (arg)
207 "Insert / and display any previous matching /.
208 Two /s are treated as matching if the first / ends a net-enabling
209 start tag, and the second / is the corresponding null end tag."
213 (let ((oldpos (point))
218 (if sgml-slash-distance
219 (narrow-to-region (max (point-min)
220 (- (point) sgml-slash-distance
))
222 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
223 (eq (match-end 0) (1- oldpos
)))
225 (goto-char (1- oldpos
))
226 (while (and (not blinkpos
)
227 (search-backward "/" (point-min) t
))
228 (let ((tagend (save-excursion
229 (if (re-search-backward sgml-start-tag-regex
233 (if (eq tagend
(point))
235 (setq blinkpos
(point))
236 (setq level
(1- level
)))
237 (setq level
(1+ level
)))))))
241 (if (pos-visible-in-window-p)
243 (message "Matches %s"
244 (buffer-substring (progn
247 (1+ blinkpos
))))))))))
249 (defun sgml-validate (command)
250 "Validate an SGML document.
251 Runs COMMAND, a shell command, in a separate process asynchronously
252 with output going to the buffer *compilation*.
253 You can then use the command \\[next-error] to find the next error message
254 and move to the line in the SGML document that caused it."
256 (list (read-string "Validate command: "
257 (or sgml-saved-validate-command
258 (concat sgml-validate-command
260 (let ((name (buffer-file-name)))
262 (file-name-nondirectory name
))))))))
263 (setq sgml-saved-validate-command command
)
264 (compile1 command
"No more errors"))
266 ;;; sgml-mode.el ends here