1 ;;; sgml-mode.el --- SGML-editing mode
4 ;; Last-Modified: 14 Jul 1992
8 ;; Copyright (C) 1992 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 1, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; Some suggestions for your .emacs file:
30 ;; (autoload 'sgml-mode "sgml-mode" "SGML mode" t)
32 ;; (setq auto-mode-alist
33 ;; (append (list (cons "\\.sgm$" 'sgml-mode)
34 ;; (cons "\\.sgml$" 'sgml-mode)
35 ;; (cons "\\.dtd$" 'sgml-mode))
43 ;;; sgmls is a free SGML parser available from
44 ;;; ftp.uu.net:pub/text-processing/sgml
45 ;;; Its error messages can be parsed by next-error.
46 ;;; The -s option suppresses output.
48 (defconst sgml-validate-command
50 "*The command to validate an SGML document.
51 The file name of current buffer file name will be appended to this,
52 separated by a space.")
54 (defvar sgml-saved-validate-command nil
55 "The command last used to validate in this buffer.")
57 (defvar sgml-mode-map nil
"Keymap for SGML mode")
61 (setq sgml-mode-map
(make-sparse-keymap))
62 (define-key sgml-mode-map
">" 'sgml-close-angle
)
63 (define-key sgml-mode-map
"/" 'sgml-slash
)
64 (define-key sgml-mode-map
"\C-c\C-v" 'sgml-validate
))
67 "Major mode for editing SGML.
68 Makes > display the matching <. Makes / display matching /.
69 Use \\[sgml-validate] to validate your document with an SGML parser."
71 (kill-all-local-variables)
72 (setq local-abbrev-table text-mode-abbrev-table
)
73 (use-local-map sgml-mode-map
)
74 (setq mode-name
"SGML")
75 (setq major-mode
'sgml-mode
)
76 (make-local-variable 'paragraph-start
)
77 ;; A start or end tag by itself on a line separates a paragraph.
78 ;; This is desirable because SGML discards a newline that appears
79 ;; immediately after a start tag or immediately before an end tag.
82 \\(</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$\\)")
83 (make-local-variable 'paragraph-separate
)
84 (setq paragraph-separate
86 ^</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$")
87 (make-local-variable 'sgml-saved-validate-command
)
88 (set-syntax-table text-mode-syntax-table
)
89 (make-local-variable 'comment-start
)
90 (setq comment-start
"<!-- ")
91 (make-local-variable 'comment-end
)
92 (setq comment-end
" -->")
93 (make-local-variable 'comment-indent-hook
)
94 (setq comment-indent-hook
'sgml-comment-indent
)
95 (make-local-variable 'comment-start-skip
)
96 ;; This will allow existing comments within declarations to be
98 (setq comment-start-skip
"--[ \t]*")
99 (run-hooks 'text-mode-hook
'sgml-mode-hook
))
101 (defun sgml-comment-indent ()
102 (if (and (looking-at "--")
103 (not (and (eq (char-after (1- (point))) ?
!)
104 (eq (char-after (- (point) 2)) ?
<))))
106 (skip-chars-backward " \t")
107 (max comment-column
(1+ (current-column))))
110 (defconst sgml-start-tag-regex
111 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
112 "Regular expression that matches a non-empty start tag.
113 Any terminating > or / is not matched.")
115 (defvar sgml-mode-markup-syntax-table nil
116 "Syntax table used for scanning SGML markup.")
118 (if sgml-mode-markup-syntax-table
120 (setq sgml-mode-markup-syntax-table
(make-syntax-table))
121 (modify-syntax-entry ?
< "(>" sgml-mode-markup-syntax-table
)
122 (modify-syntax-entry ?
> ")<" sgml-mode-markup-syntax-table
)
123 (modify-syntax-entry ?-
"_ 1234" sgml-mode-markup-syntax-table
)
124 (modify-syntax-entry ?
\' "\"" sgml-mode-markup-syntax-table
))
126 (defconst sgml-angle-distance
4000
127 "*If non-nil, is the maximum distance to search for matching <
128 when > is inserted.")
130 (defun sgml-close-angle (arg)
131 "Insert > and display matching <."
135 (let ((oldpos (point))
139 (if sgml-angle-distance
140 (narrow-to-region (max (point-min)
141 (- (point) sgml-angle-distance
))
143 ;; See if it's the end of a marked section.
144 (and (> (- (point) (point-min)) 3)
145 (eq (char-after (- (point) 2)) ?\
])
146 (eq (char-after (- (point) 3)) ?\
])
147 (re-search-backward "<!\\[\\(-?[A-Za-z0-9. \t\n&;]\\|\
148 --\\([^-]\\|-[^-]\\)*--\\)*\\["
151 (let ((msspos (point)))
152 (if (and (search-forward "]]>" oldpos t
)
154 (setq blinkpos msspos
))))
155 ;; This handles cases where the > ends one of the following:
156 ;; markup declaration starting with <! (possibly including a
157 ;; declaration subset); start tag; end tag; SGML declaration.
162 (let ((oldtable (syntax-table))
163 (parse-sexp-ignore-comments t
))
166 (set-syntax-table sgml-mode-markup-syntax-table
)
167 (setq blinkpos
(scan-sexps oldpos -
1)))
168 (set-syntax-table oldtable
)))
173 ;; Check that it's a valid delimiter in context.
175 "<\\(\\?\\|/?[A-Za-z>]\\|!\\([[A-Za-z]\\|--\\)\\)"))
176 ;; Check that it's not a net-enabling start tag
177 ;; nor an unclosed start-tag.
178 (looking-at (concat sgml-start-tag-regex
"[/<]"))
179 ;; Nor an unclosed end-tag.
180 (looking-at "</[A-Za-z][-.A-Za-z0-9]*[ \t]*<"))
181 (setq blinkpos nil
)))
184 ;; See if it's the end of a processing instruction.
186 (if (search-backward "<?" (point-min) t
)
187 (let ((pipos (point)))
188 (if (and (search-forward ">" oldpos t
)
190 (setq blinkpos pipos
))))))
194 (if (pos-visible-in-window-p)
196 (message "Matches %s"
197 (buffer-substring blinkpos
201 ;;; I doubt that null end tags are used much for large elements,
202 ;;; so use a small distance here.
203 (defconst sgml-slash-distance
1000
204 "*If non-nil, is the maximum distance to search for matching /
205 when / is inserted.")
207 (defun sgml-slash (arg)
208 "Insert / and display any previous matching /.
209 Two /s are treated as matching if the first / ends a net-enabling
210 start tag, and the second / is the corresponding null end tag."
214 (let ((oldpos (point))
219 (if sgml-slash-distance
220 (narrow-to-region (max (point-min)
221 (- (point) sgml-slash-distance
))
223 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
224 (eq (match-end 0) (1- oldpos
)))
226 (goto-char (1- oldpos
))
227 (while (and (not blinkpos
)
228 (search-backward "/" (point-min) t
))
229 (let ((tagend (save-excursion
230 (if (re-search-backward sgml-start-tag-regex
234 (if (eq tagend
(point))
236 (setq blinkpos
(point))
237 (setq level
(1- level
)))
238 (setq level
(1+ level
)))))))
242 (if (pos-visible-in-window-p)
244 (message "Matches %s"
245 (buffer-substring (progn
248 (1+ blinkpos
))))))))))
250 (defun sgml-validate (command)
251 "Validate an SGML document.
252 Runs COMMAND, a shell command, in a separate process asynchronously
253 with output going to the buffer *compilation*.
254 You can then use the command \\[next-error] to find the next error message
255 and move to the line in the SGML document that caused it."
257 (list (read-string "Validate command: "
258 (or sgml-saved-validate-command
259 (concat sgml-validate-command
261 (let ((name (buffer-file-name)))
263 (file-name-nondirectory name
))))))))
264 (setq sgml-saved-validate-command command
)
265 (compile1 command
"No more errors"))
267 ;;; sgml-mode.el ends here