Mercurial > emacs
annotate lisp/textmodes/sgml-mode.el @ 3254:e1f5f8f23d90
(do-install): Delete redundant code to copy etc twice.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 29 May 1993 16:38:15 +0000 |
parents | 8cf203c42bee |
children | 4db64c9c1f05 |
rev | line source |
---|---|
809 | 1 ;;; sgml-mode.el --- SGML-editing mode |
2 | |
844
bf829a2d63b4
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
828
diff
changeset
|
3 ;; Copyright (C) 1992 Free Software Foundation, Inc. |
bf829a2d63b4
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
828
diff
changeset
|
4 |
869 | 5 ;; Author: James Clark <jjc@clark.com> |
809 | 6 ;; Adapted-By: ESR |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
809
diff
changeset
|
7 ;; Keywords: wp |
809 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
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) | |
14 ;; any later version. | |
15 | |
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. | |
20 | |
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. | |
24 | |
25 ;;; Commentary: | |
26 | |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2307
diff
changeset
|
27 ;; Major mode for editing the SGML document-markup language. |
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2307
diff
changeset
|
28 ;; |
809 | 29 ;; Some suggestions for your .emacs file: |
30 ;; | |
31 ;; (autoload 'sgml-mode "sgml-mode" "SGML mode" t) | |
32 ;; | |
33 ;; (setq auto-mode-alist | |
34 ;; (append (list (cons "\\.sgm$" 'sgml-mode) | |
35 ;; (cons "\\.sgml$" 'sgml-mode) | |
36 ;; (cons "\\.dtd$" 'sgml-mode)) | |
37 ;; auto-mode-alist)) | |
38 | |
39 ;;; Code: | |
40 | |
41 (provide 'sgml-mode) | |
42 (require 'compile) | |
43 | |
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. | |
48 | |
49 (defconst sgml-validate-command | |
50 "sgmls -s" | |
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.") | |
54 | |
55 (defvar sgml-saved-validate-command nil | |
56 "The command last used to validate in this buffer.") | |
57 | |
58 (defvar sgml-mode-map nil "Keymap for SGML mode") | |
59 | |
60 (if sgml-mode-map | |
61 () | |
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)) | |
66 | |
3121
8cf203c42bee
(sgml-validate): compile1 renamed to compile-internal.
Richard M. Stallman <rms@gnu.org>
parents:
2315
diff
changeset
|
67 ;;;###autoload |
809 | 68 (defun sgml-mode () |
69 "Major mode for editing SGML. | |
70 Makes > display the matching <. Makes / display matching /. | |
71 Use \\[sgml-validate] to validate your document with an SGML parser." | |
72 (interactive) | |
73 (kill-all-local-variables) | |
74 (setq local-abbrev-table text-mode-abbrev-table) | |
75 (use-local-map sgml-mode-map) | |
76 (setq mode-name "SGML") | |
77 (setq major-mode 'sgml-mode) | |
78 (make-local-variable 'paragraph-start) | |
79 ;; A start or end tag by itself on a line separates a paragraph. | |
80 ;; This is desirable because SGML discards a newline that appears | |
81 ;; immediately after a start tag or immediately before an end tag. | |
82 (setq paragraph-start | |
83 "^[ \t\n]\\|\ | |
84 \\(</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$\\)") | |
85 (make-local-variable 'paragraph-separate) | |
86 (setq paragraph-separate | |
87 "^[ \t\n]*$\\|\ | |
88 ^</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$") | |
89 (make-local-variable 'sgml-saved-validate-command) | |
90 (set-syntax-table text-mode-syntax-table) | |
91 (make-local-variable 'comment-start) | |
92 (setq comment-start "<!-- ") | |
93 (make-local-variable 'comment-end) | |
94 (setq comment-end " -->") | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
869
diff
changeset
|
95 (make-local-variable 'comment-indent-function) |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
869
diff
changeset
|
96 (setq comment-indent-function 'sgml-comment-indent) |
809 | 97 (make-local-variable 'comment-start-skip) |
98 ;; This will allow existing comments within declarations to be | |
99 ;; recognized. | |
100 (setq comment-start-skip "--[ \t]*") | |
101 (run-hooks 'text-mode-hook 'sgml-mode-hook)) | |
102 | |
103 (defun sgml-comment-indent () | |
104 (if (and (looking-at "--") | |
105 (not (and (eq (char-after (1- (point))) ?!) | |
106 (eq (char-after (- (point) 2)) ?<)))) | |
107 (progn | |
108 (skip-chars-backward " \t") | |
109 (max comment-column (1+ (current-column)))) | |
110 0)) | |
111 | |
112 (defconst sgml-start-tag-regex | |
113 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*" | |
114 "Regular expression that matches a non-empty start tag. | |
115 Any terminating > or / is not matched.") | |
116 | |
117 (defvar sgml-mode-markup-syntax-table nil | |
118 "Syntax table used for scanning SGML markup.") | |
119 | |
120 (if sgml-mode-markup-syntax-table | |
121 () | |
122 (setq sgml-mode-markup-syntax-table (make-syntax-table)) | |
123 (modify-syntax-entry ?< "(>" sgml-mode-markup-syntax-table) | |
124 (modify-syntax-entry ?> ")<" sgml-mode-markup-syntax-table) | |
125 (modify-syntax-entry ?- "_ 1234" sgml-mode-markup-syntax-table) | |
126 (modify-syntax-entry ?\' "\"" sgml-mode-markup-syntax-table)) | |
127 | |
128 (defconst sgml-angle-distance 4000 | |
3121
8cf203c42bee
(sgml-validate): compile1 renamed to compile-internal.
Richard M. Stallman <rms@gnu.org>
parents:
2315
diff
changeset
|
129 "*If non-nil, is the maximum distance to search for matching <.") |
809 | 130 |
131 (defun sgml-close-angle (arg) | |
132 "Insert > and display matching <." | |
133 (interactive "p") | |
134 (insert-char ?> arg) | |
135 (if (> arg 0) | |
136 (let ((oldpos (point)) | |
137 (blinkpos)) | |
138 (save-excursion | |
139 (save-restriction | |
140 (if sgml-angle-distance | |
141 (narrow-to-region (max (point-min) | |
142 (- (point) sgml-angle-distance)) | |
143 oldpos)) | |
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 --\\([^-]\\|-[^-]\\)*--\\)*\\[" | |
150 (point-min) | |
151 t) | |
152 (let ((msspos (point))) | |
153 (if (and (search-forward "]]>" oldpos t) | |
154 (eq (point) oldpos)) | |
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. | |
159 (if blinkpos | |
160 () | |
161 (goto-char oldpos) | |
162 (condition-case () | |
163 (let ((oldtable (syntax-table)) | |
164 (parse-sexp-ignore-comments t)) | |
165 (unwind-protect | |
166 (progn | |
167 (set-syntax-table sgml-mode-markup-syntax-table) | |
168 (setq blinkpos (scan-sexps oldpos -1))) | |
169 (set-syntax-table oldtable))) | |
170 (error nil)) | |
171 (and blinkpos | |
172 (goto-char blinkpos) | |
173 (or | |
174 ;; Check that it's a valid delimiter in context. | |
175 (not (looking-at | |
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))) | |
183 (if blinkpos | |
184 () | |
185 ;; See if it's the end of a processing instruction. | |
186 (goto-char oldpos) | |
187 (if (search-backward "<?" (point-min) t) | |
188 (let ((pipos (point))) | |
189 (if (and (search-forward ">" oldpos t) | |
190 (eq (point) oldpos)) | |
191 (setq blinkpos pipos)))))) | |
192 (if blinkpos | |
193 (progn | |
194 (goto-char blinkpos) | |
195 (if (pos-visible-in-window-p) | |
196 (sit-for 1) | |
197 (message "Matches %s" | |
198 (buffer-substring blinkpos | |
199 (progn (end-of-line) | |
200 (point))))))))))) | |
201 | |
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 | |
3121
8cf203c42bee
(sgml-validate): compile1 renamed to compile-internal.
Richard M. Stallman <rms@gnu.org>
parents:
2315
diff
changeset
|
205 "*If non-nil, is the maximum distance to search for matching /.") |
809 | 206 |
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." | |
211 (interactive "p") | |
212 (insert-char ?/ arg) | |
213 (if (> arg 0) | |
214 (let ((oldpos (point)) | |
215 (blinkpos) | |
216 (level 0)) | |
217 (save-excursion | |
218 (save-restriction | |
219 (if sgml-slash-distance | |
220 (narrow-to-region (max (point-min) | |
221 (- (point) sgml-slash-distance)) | |
222 oldpos)) | |
223 (if (and (re-search-backward sgml-start-tag-regex (point-min) t) | |
224 (eq (match-end 0) (1- oldpos))) | |
225 () | |
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 | |
231 (point-min) t) | |
232 (match-end 0) | |
233 nil)))) | |
234 (if (eq tagend (point)) | |
235 (if (eq level 0) | |
236 (setq blinkpos (point)) | |
237 (setq level (1- level))) | |
238 (setq level (1+ level))))))) | |
239 (if blinkpos | |
240 (progn | |
241 (goto-char blinkpos) | |
242 (if (pos-visible-in-window-p) | |
243 (sit-for 1) | |
244 (message "Matches %s" | |
245 (buffer-substring (progn | |
246 (beginning-of-line) | |
247 (point)) | |
248 (1+ blinkpos)))))))))) | |
249 | |
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." | |
256 (interactive | |
257 (list (read-string "Validate command: " | |
258 (or sgml-saved-validate-command | |
259 (concat sgml-validate-command | |
260 " " | |
261 (let ((name (buffer-file-name))) | |
262 (and name | |
263 (file-name-nondirectory name)))))))) | |
264 (setq sgml-saved-validate-command command) | |
3121
8cf203c42bee
(sgml-validate): compile1 renamed to compile-internal.
Richard M. Stallman <rms@gnu.org>
parents:
2315
diff
changeset
|
265 (compile-internal command "No more errors")) |
809 | 266 |
267 ;;; sgml-mode.el ends here |