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