1 ;;; psgml.el --- SGML-editing mode with parsing support
2 ;; $Id: psgml.el,v 2.70 2005/03/02 19:44:04 lenst Exp $
4 ;; Copyright (C) 1993-2002 Lennart Staflin
5 ;; Copyright (C) 1992 Free Software Foundation, Inc.
7 ;; Author: Lennart Staflin <lenst@lysator.liu.se>
8 ;; James Clark <jjc@clark.com>
9 ;; Maintainer: Lennart Staflin <lenst@lysator.liu.se>
10 ;; Keywords: languages
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License
15 ;; as published by the Free Software Foundation; either version 2
16 ;; of the License, or (at your option) any later version.
18 ;; This program 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 this program; if not, write to the Free Software
25 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;; Major mode for editing the SGML document-markup language.
32 ;; Send bugs to lenst@lysator.liu.se
36 ;; - Identify structural errors (but it is not a validator)
37 ;; - Menus for inserting tags with only the contextually valid tags
38 ;; - Edit attribute values in separate window with information about types
42 ;; - Indent according to element nesting depth
44 ;; - Structure editing: move and kill by element
45 ;; - Find next data context
49 ;; - only accepts the reference concrete syntax, though it does allow
50 ;; unlimited lengths on names
55 (defconst psgml-version "1.3.2"
56 "Version of psgml package.")
58 (defconst psgml-maintainer-address "lenst@lysator.liu.se")
60 (eval-when-compile (require 'cl))
63 (defvar sgml-debug nil)
65 (defmacro sgml-debug (&rest x)
66 (list 'if 'sgml-debug (cons 'message x)))
71 (defvar sgml-mode-abbrev-table nil
72 "Abbrev table in use in SGML mode.")
73 (define-abbrev-table 'sgml-mode-abbrev-table ())
76 (defconst sgml-have-re-char-clases (string-match "[[:alpha:]]" "x")
77 "Non-nil if this Emacs supports regexp character classes.
78 E.g. `[-.[:alnum:]]'."))
80 (defconst sgml-default-nonsticky (boundp 'text-property-default-nonsticky)
81 "Non-nil means use `text-property-default-nonsticky' locally.
82 Otherwise put explicit properties.")
85 (defvar sgml-xml-p nil
86 "Is this an XML document?")
87 (make-variable-buffer-local 'sgml-xml-p)
89 ;;; User settable options:
91 (defvar sgml-insert-defaulted-attributes nil
92 "*Controls whether defaulted attributes (not #FIXED) are inserted explicitly
93 or not. nil means don't insert, t means insert.")
96 "Standard Generalized Markup Language"
100 "SGML-editing mode with parsing support"
104 (defgroup psgml-insert nil
105 "Inserting features of psgml"
109 (defgroup psgml-dtd nil
110 "DTD, CATALOG and DOCTYPE customizations in psgml"
114 (defcustom sgml-insert-missing-element-comment t
115 "*If true, and sgml-auto-insert-required-elements also true,
116 `sgml-insert-element' will insert a comment if there is an element required
117 but there is more than one to choose from."
119 :group 'psgml-insert)
121 (defcustom sgml-insert-end-tag-on-new-line nil
122 "*If true, `sgml-insert-element' will put the end-tag on a new line
123 after the start-tag. Useful on slow terminals if you find the end-tag after
124 the cursor irritating."
126 :group 'psgml-insert)
128 (defvar sgml-doctype nil
129 "*If non-nil the name of a file that contains the doctype declaration to use.
130 Setting this variable automatically makes it local to the current buffer.")
131 (put 'sgml-doctype 'sgml-type 'string)
132 (make-variable-buffer-local 'sgml-doctype)
134 (defcustom sgml-system-identifiers-are-preferred nil
135 "*Controls lookup of external entities.
136 nil means look up external entities by searching the catalogs
137 in `sgml-local-catalogs' and `sgml-catalog-files' and only if the
138 entity is not found in the catalogs, use a given system identifier.
139 Non-nil means use a system identifier for the entity if one is given.
140 If no system identifier is given the catalogs will searched."
144 (defcustom sgml-range-indicator-max-length 9
145 "*Control of menu indicators.
146 Maximum number of characters used from the first and last entry
147 of a submenu to indicate the range of that menu."
151 (defcustom sgml-default-doctype-name nil
152 "*Document type name to use if no document type declaration is present."
153 :type '(choice string (const nil))
155 (put 'sgml-default-doctype-name 'sgml-type 'string-or-nil)
157 (defcustom sgml-markup-faces
158 ;; Fixme: are the font-lock correspondences here the most appopriate
159 ;; ones? I don't recall whence this set came. -- fx
160 `((start-tag . ,(if (facep 'font-lock-function-name-face)
161 'font-lock-function-name-face
163 (end-tag . ,(if (facep 'font-lock-function-name-face)
164 'font-lock-function-name-face
166 (comment . ,(if (facep 'font-lock-comment-face)
167 'font-lock-comment-face
169 (pi . ,(if (facep 'font-lock-type-face)
172 (sgml . ,(if (facep 'font-lock-type-face)
175 (doctype . ,(if (facep 'font-lock-keyword-face)
176 'font-lock-keyword-face
178 (entity . ,(if (facep 'font-lock-string-face)
179 'font-lock-string-face
181 (shortref . ,(if (facep 'font-lock-string-face)
182 'font-lock-string-face
184 (ignored . ,(if (facep 'font-lock-constant-face)
185 'font-lock-constant-face
187 (ms-start . ,(if (facep 'font-lock-constant-face)
188 'font-lock-constant-face
190 (ms-end . ,(if (facep 'font-lock-constant-face)
191 'font-lock-constant-face
193 "*List of markup to face mappings.
194 Element are of the form (MARKUP-TYPE . FACE).
195 Possible values for MARKUP-TYPE are:
196 comment - comment declaration
197 doctype - doctype declaration
199 ignored - ignored marked section
200 ms-end - marked section start, if not ignored
201 ms-start- marked section end, if not ignored
202 pi - processing instruction
203 sgml - SGML declaration
205 entity - general entity reference
206 shortref- short reference"
207 :type '(repeat (cons symbol face))
210 (defvar sgml-buggy-subst-char-in-region
211 (or (not (boundp 'emacs-minor-version))
212 (not (natnump emacs-minor-version))
213 (and (eq emacs-major-version 19)
214 (< emacs-minor-version 23)))
215 "*If non-nil, work around a bug in subst-char-in-region.
216 The bug sets the buffer modified. If this is set, folding commands
219 (defcustom sgml-set-face nil
220 "*If non-nil, psgml will set the face of parsed markup."
223 (put 'sgml-set-face 'sgml-desc "Set face of parsed markup")
225 (defcustom sgml-live-element-indicator nil
226 "*If non-nil, indicate current element in mode line.
231 (defcustom sgml-auto-activate-dtd nil
232 "*If non-nil, loading a sgml-file will automatically try to activate its DTD.
233 Activation means either to parse the document type declaration or to
234 load a previously saved parsed DTD. The name of the activated DTD
235 will be shown in the mode line."
238 (put 'sgml-auto-activate-dtd 'sgml-desc "Auto Activate DTD")
240 (defcustom sgml-offer-save t
241 "*If non-nil, ask about saving modified buffers before \\[sgml-validate] is run."
245 (defvar sgml-parent-document nil
246 "*How to handle the current file as part of a bigger document.
248 The variable describes how the current file's content fit into the element
249 hierarchy. The value should have the form
251 (PARENT-FILE CONTEXT-ELEMENT* TOP-ELEMENT (HAS-SEEN-ELEMENT*)?)
253 PARENT-FILE is a string, the name of the file containing the
255 CONTEXT-ELEMENT is a string, that is the name of an element type.
256 It can occur 0 or more times and is used to set up
257 exceptions and short reference map. Good candidates
258 for these elements are the elements open when the
259 entity pointing to the current file is used.
260 TOP-ELEMENT is a string that is the name of the element type
261 of the top level element in the current file. The file
262 should contain one instance of this element, unless
263 the last \(Lisp) element of `sgml-parent-document' is a
264 list. If it is a list, the top level of the file
265 should follow the content model of top-element.
266 HAS-SEEN-ELEMENT is a string that is the name of an element type. This
267 element is satisfied in the content model of top-element.
269 Setting this variable automatically makes it local to the current buffer.")
270 (make-variable-buffer-local 'sgml-parent-document)
271 (put 'sgml-parent-document 'sgml-type 'list)
273 (defcustom sgml-tag-region-if-active t ;; wing change
274 "*If non-nil, the Tags menu will tag a region if the region is
275 considered active by emacs. If nil, region must be active and
276 `transient-mark-mode' must be on for the region to be tagged."
280 (defcustom sgml-normalize-trims t
281 "*If non-nil, sgml-normalize will trim off white space from end of element
282 when adding end tag."
286 (defvar sgml-omittag t
287 "*Non-nil means use OMITTAG YES.
289 Setting this variable automatically makes it local to the current buffer.")
291 (make-variable-buffer-local 'sgml-omittag)
292 (put 'sgml-omittag 'sgml-desc "OMITTAG")
294 (defvar sgml-shorttag t
295 "*Non-nil means use SHORTTAG YES.
297 Setting this variable automatically makes it local to the current buffer.")
299 (make-variable-buffer-local 'sgml-shorttag)
300 (put 'sgml-shorttag 'sgml-desc "SHORTTAG")
302 (defvar sgml-namecase-general t
303 "*Non-nil means use NAMECASE GENERAL YES.
305 Setting this variable automatically makes it local to the current buffer.")
307 (make-variable-buffer-local 'sgml-namecase-general)
308 (put 'sgml-namecase-general 'sgml-desc "NAMECASE GENERAL")
312 ;;[lenst/1998-03-09 19:51:55]
313 (defconst sgml-namecase-entity nil)
315 (defcustom sgml-general-insert-case 'lower
316 "*The case that will be used for general names in inserted markup.
317 This can be the symbol `lower' or `upper'. Only effective if
318 `sgml-namecase-general' is true."
319 :type '(choice (const lower) (const upper))
320 :group 'psgml-insert)
321 (put 'sgml-general-insert-case 'sgml-type '(lower upper))
323 (defvar sgml-entity-insert-case nil)
326 (defvar sgml-minimize-attributes nil
327 "*Determines minimization of attributes inserted by edit-attributes.
328 Actually two things are done
329 1. If non-nil, omit attribute name, if attribute value is from a token group.
330 2. If `max', omit attributes with default value.
332 Setting this variable automatically makes it local to the current buffer.")
334 (make-variable-buffer-local 'sgml-minimize-attributes)
335 (put 'sgml-minimize-attributes 'sgml-type
336 '(("No" . nil) ("Yes" . t) ("Max" . max)))
338 (defvar sgml-always-quote-attributes t
339 "*Non-nil means quote all attribute values inserted after editing attributes.
340 Setting this variable automatically makes it local to the current buffer.")
342 (make-variable-buffer-local 'sgml-always-quote-attributes)
344 (defcustom sgml-auto-insert-required-elements t
345 "*If non-nil, automatically insert required elements in the content
346 of an inserted element."
348 :group 'psgml-insert)
350 (defcustom sgml-balanced-tag-edit t
351 "*If non-nil, always insert start-end tag pairs."
353 :group 'psgml-insert)
355 (defcustom sgml-omittag-transparent (not sgml-balanced-tag-edit)
356 "*If non-nil, will show legal tags inside elements with omittable start tags
357 and legal tags beyond omittable end tags."
361 (defcustom sgml-leave-point-after-insert nil
362 "*If non-nil, the point will remain after inserted tag(s).
363 If nil, the point will be placed before the inserted tag(s)."
365 :group 'psgml-insert)
367 (defcustom sgml-warn-about-undefined-elements t
368 "*If non-nil, print a warning when a tag for an undefined element is found."
372 (defcustom sgml-warn-about-undefined-entities t
373 "*If non-nil, print a warning when an undefined entity is found."
377 (defcustom sgml-ignore-undefined-elements nil
378 "*If non-nil, recover from an undefined element by ignoring the tag.
379 If nil, recover from an undefined element by assuming it can occur any
380 where and has content model ANY."
384 (defcustom sgml-recompile-out-of-date-cdtd 'ask
385 "*If non-nil, out of date compiled DTDs will be automatically recompiled.
386 If the value is `ask', PSGML will ask before recompiling. A `nil'
387 value will cause PSGML to silently load an out of date compiled DTD.
388 A DTD that refers to undefined external entities is always out of
389 date, thus in such case it can be useful to set this variable to
393 (put 'sgml-recompile-out-of-date-cdtd 'sgml-type '(("No" . nil)
397 (defcustom sgml-trace-entity-lookup nil
398 "*If non-nil, log messages about catalog files used to look for
403 (defvar sgml-indent-step 2
404 "*How much to increment indent for every element level.
405 If nil, no indentation.
406 Setting this variable automatically makes it local to the current buffer.")
407 (make-variable-buffer-local 'sgml-indent-step)
408 (put 'sgml-indent-step 'sgml-type '(("None" . nil) 0 1 2 3 4 5 6 7 8))
410 (defvar sgml-indent-data nil
411 "*If non-nil, indent in data/mixed context also.
412 Setting this variable automatically makes it local to the current buffer.")
413 (make-variable-buffer-local 'sgml-indent-data)
415 (defvar sgml-content-indent-function 'sgml-indent-according-to-level)
416 (defvar sgml-attribute-indent-function 'sgml-indent-according-to-stag)
419 (defcustom sgml-inhibit-indent-tags nil
420 "*List of tags within which indentation is inhibited.
421 The tags should be given as strings."
426 (defvar sgml-menu-name "SGML"
427 "*The name of the menu on which the SGML mode options appear.
428 This is intended to be overridden by submodes of sgml-mode.")
431 (defvar sgml-data-directory (or (locate-data-directory "psgml-dtds")
432 (locate-data-directory "psgml")
433 (locate-data-directory "sgml"))
434 "*Directory for pre-supplied data files (DTD's and such).
435 Set this before loading psgml.")
437 ;; XEmacs note: this isn't in use, we're using `split-path'.
438 (defun sgml-parse-colon-path (cd-path)
439 "Explode a colon-separated list of paths into a string list."
443 cd-list (cd-start 0) cd-colon)
444 (if (boundp 'path-separator)
445 (setq cd-sep path-separator))
446 (setq cd-path (concat cd-path cd-sep))
447 (while (setq cd-colon (string-match cd-sep cd-path cd-start))
450 (list (if (= cd-start cd-colon)
452 (substitute-in-file-name
453 (substring cd-path cd-start cd-colon))))))
454 (setq cd-start (+ cd-colon 1)))
457 ;; XEmacs change: use `split-path'
458 (defcustom sgml-system-path (split-path (or (getenv "SGML_SEARCH_PATH") "."))
459 "*List of directories used to look for system identifiers.
460 The directory listed in `sgml-data-directory' is always searched in
461 addition to the directories listed here."
462 :type '(repeat directory)
464 (put 'sgml-system-path 'sgml-type 'file-list)
466 ;; XEmacs change: use `split-path'
467 (defcustom sgml-public-map (split-path (or (getenv "SGML_PATH")
468 ;; Wing/Krause change
469 (concat "%S" path-separator
473 "*Mapping from public identifiers to file names.
474 This is a list of possible file names. To find the file for a public
475 identifier the elements of the list are used one at the time from the
476 beginning. If the element is a string a file name is constructed from
477 the string by substitution of the whole public identifier for %P,
478 owner for %O, public text class for %C, and public text description
479 for %D. The text class will be converted to lower case and the owner
480 and description will be transliterated according to the variable
481 `sgml-public-transliterations'. If the file exists it will be the file
482 used for the public identifier. An element can also be a dotted pair
483 \(regexp . filename), the filename is a string treated as above, but
484 only if the regular expression, regexp, matches the public
488 (put 'sgml-public-map 'sgml-type 'list)
490 (defcustom sgml-local-catalogs nil
491 "*A list of SGML entity catalogs to be searched first when parsing the buffer.
492 This is used in addition to `sgml-catalog-files', and `sgml-public-map'.
493 This variable is automatically local to the buffer."
496 (make-variable-buffer-local 'sgml-local-catalogs)
497 (put 'sgml-local-catalogs 'sgml-type 'file-list)
499 ;; XEmacs chanage: use `split-path'.
500 (defcustom sgml-catalog-files (split-path
501 (or (getenv "SGML_CATALOG_FILES")
502 ;; Wing/Krause addition
503 (concat "CATALOG" path-separator
506 sgml-data-directory))))
507 "*List of catalog entry files.
508 The files are in the format defined in the SGML Open Draft Technical
509 Resolution on Entity Management."
512 (put 'sgml-catalog-files 'sgml-type 'file-list)
514 (defcustom sgml-ecat-files (list
518 (expand-file-name "ECAT" sgml-data-directory))
519 "*List of catalog files for PSGML."
522 (put 'sgml-ecat-files 'sgml-type 'file-list)
524 (defcustom sgml-local-ecat-files nil
525 "*List of local catalog files for PSGML.
526 Automatically becomes buffer local if set."
530 (make-variable-buffer-local 'sgml-local-ecat-files)
531 (put 'sgml-local-ecat-files 'sgml-type 'file-list)
533 ;; XEmacs change: transliterate colons to hyphens for Windows reasons.
534 (defcustom sgml-public-transliterations '((? . ?_) (?/ . ?%) (?: . ?-))
535 "*Transliteration for characters that should be avoided in file names.
536 This is a list of dotted pairs (FROM . TO); where FROM is the the
537 character to be translated to TO. This is used when parts of a public
538 identifier are used to construct a file name."
539 :type '(repeat (cons character character))
542 (defvar sgml-default-dtd-file nil
543 "*This is the default file name for saved DTD.
544 This is set by sgml-mode from the buffer file name.
545 Can be changed in the Local variables section of the file.")
546 (put 'sgml-default-dtd-file 'sgml-type 'string)
547 (put 'sgml-default-dtd-file 'sgml-desc "Default (saved) DTD File")
549 (defvar sgml-exposed-tags '()
550 "*The list of tag names that remain visible, despite \\[sgml-hide-tags].
551 Each name is a lowercase string, and start-tags and end-tags must be
554 `sgml-exposed-tags' is local to each buffer in which it has been set;
555 use `setq-default' to set it to a value that is shared among buffers.")
556 (make-variable-buffer-local 'sgml-exposed-tags)
557 (put 'sgml-exposed-tags 'sgml-type 'list)
560 (defcustom sgml-custom-markup nil
561 "*Menu entries to be added to the Markup menu.
562 The value should be a list of lists of two strings. The first
563 string is the menu line and the second string is the text inserted
564 when the menu item is chosen. The second string can contain a \\r
565 where the cursor should be left. Also if a selection is made
566 according the same rules as for the Tags menu, the selection is
567 replaced with the second string and \\r is replaced with the
572 ((\"Version1\" \"<![%Version1[\\r]]>\")
573 (\"New page\" \"<?NewPage>\"))
575 :type '(repeat (list :inline t
576 (string :tag "Menu Line")
577 (string :tag "Inserted String")))
580 (defcustom sgml-custom-dtd nil
581 "Menu entries to be added to the DTD menu.
582 The value should be a list of entries to be added to the DTD menu.
583 Every entry should be a list. The first element of the entry is a string
584 used as the menu entry. The second element is a string containing a
585 doctype declaration (this can be nil if no doctype). The rest of the
586 list should be a list of variables and values. For backward
587 compatibility a single string instead of a variable is assigned to
588 `sgml-default-dtd-file'. All variables are made buffer local and are also
589 added to the buffers local variables list.
593 sgml-default-dtd-file \"~/sgml/html.ced\"
594 sgml-omittag nil sgml-shorttag nil)
595 (\"HTML+\" \"<!doctype htmlplus system 'htmlplus.dtd'>\"
596 \"~/sgml/htmlplus.ced\"
597 sgml-omittag t sgml-shorttag nil)
598 (\"DOCBOOK\" \"<!doctype docbook system 'docbook.dtd'>\"
599 \"~/sgml/docbook.ced\"
600 sgml-omittag nil sgml-shorttag t)))
602 :type '(repeat (list (string :tag "Menu Entry")
603 (choice (const :tag "No doctype")
604 (string :tag "Declaration"))
607 (symbol :tag "Variable")
608 (sexp :tag "Value")))))
612 ;;; Faces used in edit attribute buffer:
613 (put 'sgml-default 'face 'underline) ; Face for #DEFAULT
614 (put 'sgml-fixed 'face 'underline) ; Face of #FIXED "..."
617 ;;; nsgmls is a free SGML parser in the SP suite available from
618 ;;; ftp.jclark.com:pub/sp
619 ;;; Its error messages can be parsed by next-error.
620 ;;; The -s option suppresses output.
623 (defcustom sgml-validate-command (concat "nsgmls -s -m "
624 (expand-file-name "CATALOG"
627 "*The shell command to validate an SGML document.
629 This is a `format' control string that by default should contain two
630 `%s' conversion specifications: the first will be replaced by the
631 value of `sgml-declaration' \(or the empty string, if nil\); the
632 second will be replaced by the current buffer's file name \(or the
633 empty string, if nil\).
635 If `sgml-validate-files' is non-nil, the format string should contain
636 one `%s' conversion specification for each element of its result.
638 If sgml-validate-command is a list, then every element should be a
639 string. The strings will be tried in order and %-sequences in the
640 string will be replaced according to the list below, if the string contains
641 %-sequences with no replacement value the next string will be tried.
643 %b means the visited file of the current buffer
644 %s means the SGML declaration specified in the `sgml-declaration' variable
645 %d means the file containing the DOCTYPE declaration, if not in the buffer
647 :type '(choice string (repeat string))
649 (make-variable-buffer-local 'sgml-validate-command)
651 (defcustom sgml-xml-validate-command "onsgmls -wxml -s %s %s"
652 "*The shell command to validate an SGML document being edited in
653 `xml-mode'. See `sgml-validate-command' for details."
654 :type '(choice string (repeat string))
657 (defcustom sgml-validate-files nil
658 "If non-nil, a function of no arguments that returns a list of file names.
659 These file names will serve as the arguments to the `sgml-validate-command'
660 format control string instead of the defaults."
664 (defvar sgml-validate-error-regexps
665 '((".*:\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[EXW]: " 1 2 3)
666 ("\\(error\\|warning\\) at \\([^,]+\\), line \\([0-9]+\\)" 2 3)
667 ("\n[a-zA-Z]?:?[^0-9 \n\t:]+:[ \t]*\\([^ \n\t:]+\\):\
668 \\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4))
669 "Alist of regexps to recognize error messages from `sgml-validate'.
670 See `compilation-error-regexp-alist'.")
672 (defcustom sgml-declaration nil
673 "*If non-nil, the name of the SGML declaration file."
674 :type '(choice (const nil) file)
676 (put 'sgml-declaration 'sgml-type 'file-or-nil)
678 (defcustom sgml-xml-declaration nil
679 "*If non-nil, the name of the SGML declaration for XML files."
680 :type '(choice (const nil) file)
682 (put 'sgml-xml-declaration 'sgml-type 'file-or-nil)
684 (defcustom sgml-mode-hook nil
685 "A hook or list of hooks to be run when entering `sgml-mode'."
689 (defvar sgml-mode-map nil
690 "Keymap for SGML mode")
692 (defvar sgml-show-context-function
693 'sgml-show-context-standard
694 "*Function to called to show context of and element.
695 Should return a string suitable form printing in the echo area.")
697 (defconst sgml-file-options
701 sgml-namecase-general
702 sgml-general-insert-case
703 sgml-minimize-attributes
704 sgml-always-quote-attributes
709 sgml-default-dtd-file
712 sgml-local-ecat-files
714 "Options for the current file, can be saved or set from menu."
717 (defconst sgml-user-options
720 sgml-live-element-indicator
721 sgml-auto-activate-dtd
723 sgml-tag-region-if-active
725 sgml-auto-insert-required-elements
726 sgml-balanced-tag-edit
727 sgml-omittag-transparent
728 sgml-leave-point-after-insert
729 sgml-insert-missing-element-comment
730 sgml-insert-end-tag-on-new-line
731 sgml-warn-about-undefined-elements
732 sgml-warn-about-undefined-entities
733 sgml-ignore-undefined-elements
734 sgml-recompile-out-of-date-cdtd
735 sgml-default-doctype-name
737 sgml-validate-command
739 sgml-system-identifiers-are-preferred
740 sgml-trace-entity-lookup
741 sgml-system-path ;; XEmacs addition
745 sgml-general-insert-case
747 "User options that can be saved or set from menu."
750 ;;; Internal variables
752 (defvar sgml-validate-command-history nil
753 "The minibuffer history list for `sgml-validate''s COMMAND argument.")
755 (defvar sgml-active-dtd-indicator nil
756 "Displayed in the mode line")
759 ;;;; User options handling
761 (defun sgml-variable-description (var)
762 (or (get var 'sgml-desc)
763 (let ((desc (symbol-name var)))
764 (if (string= "sgml-" (substring desc 0 5))
765 (setq desc (substring desc 5)))
766 (loop for c across-ref desc
767 do (if (eq c ?-) (setf c ? )))
770 (defun sgml-variable-type (var)
771 (or (get var 'sgml-type)
772 (if (memq (symbol-value var) '(t nil))
775 (defun sgml-set-local-variable (var val)
776 "Set the value of variable VAR to VAL in buffer and local variables list."
777 (set (make-local-variable var) val)
781 (case-fold-search t))
782 (goto-char (max (point-min) (- (point-max) 3000)))
783 (cond ((search-forward "Local Variables:" nil t)
784 (setq suffix (buffer-substring (point)
785 (save-excursion (end-of-line 1)
788 (buffer-substring (save-excursion (beginning-of-line 1)
790 (match-beginning 0))))
792 (goto-char (point-max))
796 "<!-- Keep this comment at the end of the file\n"
804 (let* ((endpos (save-excursion
805 (search-forward (format "\n%send:" prefix))))
806 (varpos (search-forward (format "\n%s%s:" prefix var) endpos t)))
808 (delete-region (point)
809 (save-excursion (end-of-line 1)
811 (insert (format "%S" val) suffix))
814 (beginning-of-line 1)
815 (insert prefix (format "%s:%S" var val) suffix ?\n)))))))
817 (defun sgml-valid-option (var)
818 (let ((type (sgml-variable-type var))
819 (val (symbol-value var)))
820 (cond ((eq 'string type)
822 ((eq 'list-or-string type)
828 (defun sgml-save-options ()
829 "Save user options for SGML mode that have buffer local values."
831 (loop for var in sgml-file-options do
832 (when (sgml-valid-option var)
833 (sgml-set-local-variable var (symbol-value var)))))
836 ;;;; Run hook with args
838 (unless (fboundp 'run-hook-with-args)
839 (defun run-hook-with-args (hook &rest args)
840 "Run HOOK with the specified arguments ARGS.
841 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
842 value, that value may be a function or a list of functions to be
843 called to run the hook. If the value is a function, it is called with
844 the given arguments and its return value is returned. If it is a list
845 of functions, those functions are called, in order,
846 with the given arguments ARGS.
847 It is best not to depend on the value return by `run-hook-with-args',
851 (let ((value (symbol-value hook)))
852 (if (and (listp value) (not (eq (car value) 'lambda)))
853 (mapcar '(lambda (foo) (apply foo args))
855 (apply value args))))))
860 ;;;; SGML mode: template functions
862 (defun sgml-markup (entry text)
866 (sgml-insert-markup (, text))))))
868 (defun sgml-insert-markup (text)
869 (let ((end (sgml-mouse-region))
873 (setq old-text (buffer-substring (point) end))
874 (delete-region (point) end))
875 (setq before (point))
881 (when (search-forward "\r" after t)
883 (when old-text (insert old-text))))
885 (defun sgml-mouse-region ()
888 (running-xemacs ;; XEmacs change
890 ((null (mark-marker)) nil)
891 (t (setq start (region-beginning)
893 ((and transient-mark-mode
895 (setq start (region-beginning)
897 ((and mouse-secondary-overlay
899 (overlay-buffer mouse-secondary-overlay)))
900 (setq start (overlay-start mouse-secondary-overlay)
901 end (overlay-end mouse-secondary-overlay))
902 (delete-overlay mouse-secondary-overlay)))
908 ;;;; SGML mode: indentation
910 (defun sgml-indent-or-tab ()
911 "Indent line in proper way for current major mode."
913 (if (null sgml-indent-step)
915 (funcall indent-line-function)))
920 (autoload 'reporter-submit-bug-report "reporter"))
922 (defun sgml-submit-bug-report ()
923 "Submit via mail a bug report on PSGML."
925 (and (y-or-n-p "Do you really want to submit a report on PSGML? ")
926 (reporter-submit-bug-report
927 psgml-maintainer-address
928 (concat "psgml.el " psgml-version)
931 'sgml-always-quote-attributes
932 'sgml-auto-activate-dtd
933 'sgml-auto-insert-required-elements
934 'sgml-balanced-tag-edit
941 'sgml-leave-point-after-insert
942 'sgml-live-element-indicator
944 'sgml-local-ecat-files
946 'sgml-minimize-attributes
947 'sgml-normalize-trims
949 'sgml-omittag-transparent
950 'sgml-parent-document
954 'sgml-namecase-general
955 'sgml-tag-region-if-active
956 'sgml-use-text-properties
959 ;;;; SGML mode: syntax table
961 (defvar sgml-mode-syntax-table
962 (let ((s (copy-syntax-table text-mode-syntax-table)))
963 (modify-syntax-entry ?< "." s)
964 (modify-syntax-entry ?> "." s)
968 ;;;; SGML mode: keys and menus
972 (setq sgml-mode-map (make-sparse-keymap)))
974 (defvar sgml-prefix-f-map (make-sparse-keymap))
975 (defvar sgml-prefix-u-map (make-sparse-keymap))
977 (define-key sgml-mode-map "\C-c\C-f" sgml-prefix-f-map)
978 (define-key sgml-mode-map "\C-c\C-u" sgml-prefix-u-map)
982 (define-key sgml-mode-map "\t" 'sgml-indent-or-tab)
983 ;(define-key sgml-mode-map "<" 'sgml-insert-tag)
984 (define-key sgml-mode-map ">" 'sgml-close-angle)
985 (define-key sgml-mode-map "/" 'sgml-slash)
986 (define-key sgml-mode-map "\C-c#" 'sgml-make-character-reference)
987 (define-key sgml-mode-map "\C-c-" 'sgml-untag-element)
988 (define-key sgml-mode-map "\C-c+" 'sgml-insert-attribute)
989 (define-key sgml-mode-map "\C-c/" 'sgml-insert-end-tag)
990 (define-key sgml-mode-map "\C-c<" 'sgml-insert-tag)
991 (define-key sgml-mode-map "\C-c=" 'sgml-change-element-name)
992 (define-key sgml-mode-map "\C-c\C-a" 'sgml-edit-attributes)
993 (define-key sgml-mode-map "\C-c\C-c" 'sgml-show-context)
994 (define-key sgml-mode-map "\C-c\C-d" 'sgml-next-data-field)
995 (define-key sgml-mode-map "\C-c\C-e" 'sgml-insert-element)
996 (define-key sgml-mode-map "\C-c\C-f\C-e" 'sgml-fold-element)
997 (define-key sgml-mode-map "\C-c\C-f\C-r" 'sgml-fold-region)
998 (define-key sgml-mode-map "\C-c\C-f\C-s" 'sgml-fold-subelement)
999 (define-key sgml-mode-map "\C-c\C-f\C-x" 'sgml-expand-element)
1000 (define-key sgml-mode-map "\C-c\C-i" 'sgml-add-element-to-element)
1001 (define-key sgml-mode-map "\C-c\C-k" 'sgml-kill-markup)
1002 (define-key sgml-mode-map "\C-c\r" 'sgml-split-element)
1003 (define-key sgml-mode-map "\C-c\C-n" 'sgml-up-element)
1004 (define-key sgml-mode-map "\C-c\C-o" 'sgml-next-trouble-spot)
1005 (define-key sgml-mode-map "\C-c\C-p" 'sgml-load-doctype)
1006 (define-key sgml-mode-map "\C-c\C-q" 'sgml-fill-element)
1007 (define-key sgml-mode-map "\C-c\C-r" 'sgml-tag-region)
1008 (define-key sgml-mode-map "\C-c\C-s" 'sgml-show-structure)
1009 ;(define-key sgml-mode-map "\C-c\C-t" 'sgml-list-valid-tags)
1010 (define-key sgml-mode-map "\C-c\C-t" 'sgml-show-current-element-type)
1011 (define-key sgml-mode-map "\C-c\C-u\C-a" 'sgml-unfold-all)
1012 (define-key sgml-mode-map "\C-c\C-u\C-d" 'sgml-custom-dtd)
1013 (define-key sgml-mode-map "\C-c\C-u\C-e" 'sgml-unfold-element)
1014 (define-key sgml-mode-map "\C-c\C-u\C-l" 'sgml-unfold-line)
1015 (define-key sgml-mode-map "\C-c\C-u\C-m" 'sgml-custom-markup)
1016 (define-key sgml-mode-map "\C-c\C-v" 'sgml-validate)
1017 (define-key sgml-mode-map "\C-c\C-w" 'sgml-what-element)
1018 (define-key sgml-mode-map "\C-c\C-z" 'sgml-trim-and-leave-element)
1020 (define-key sgml-mode-map "\e\C-a" 'sgml-beginning-of-element)
1021 (define-key sgml-mode-map "\e\C-e" 'sgml-end-of-element)
1022 (define-key sgml-mode-map "\e\C-f" 'sgml-forward-element)
1023 (define-key sgml-mode-map "\e\C-b" 'sgml-backward-element)
1024 (define-key sgml-mode-map "\e\C-d" 'sgml-down-element)
1025 (define-key sgml-mode-map "\e\C-u" 'sgml-backward-up-element)
1026 (define-key sgml-mode-map "\e\C-k" 'sgml-kill-element)
1027 (define-key sgml-mode-map "\e\C-@" 'sgml-mark-element)
1028 ;;(define-key sgml-mode-map [?\M-\C-\ ] 'sgml-mark-element)
1029 (define-key sgml-mode-map [(meta control h)] 'sgml-mark-current-element)
1030 (define-key sgml-mode-map "\e\C-t" 'sgml-transpose-element)
1031 (define-key sgml-mode-map "\M-\t" 'sgml-complete)
1036 sgml-main-menu sgml-mode-map "Main menu"
1038 ["Parse DTD" sgml-parse-prolog t]
1040 ["General DTD info" sgml-general-dtd-info t]
1041 ["Describe element type" sgml-describe-element-type t]
1042 ["Describe entity" sgml-describe-entity t]
1043 ["List elements" sgml-list-elements t]
1044 ["List attributes" sgml-list-attributes t]
1045 ["List terminals" sgml-list-terminals t]
1046 ["List content elements" sgml-list-content-elements t]
1047 ["List occur in elements" sgml-list-occur-in-elements t])
1049 ["Insert Element" sgml-element-menu t]
1050 ["Insert Start-Tag" sgml-start-tag-menu t]
1051 ["Insert End-Tag" sgml-end-tag-menu t]
1052 ["End Current Element" sgml-insert-end-tag t]
1053 ["Tag Region" sgml-tag-region-menu t]
1054 ["Insert Attribute" sgml-attrib-menu t]
1055 ["Insert Entity" sgml-entities-menu t]
1056 ["Add Element to Element" sgml-add-element-menu t]
1058 ("Custom markup" "---"))
1060 ["Show Context" sgml-show-context t]
1061 ["What Element" sgml-what-element t]
1062 ["List Valid Tags" sgml-list-valid-tags t]
1063 ["Validate" sgml-validate t]
1066 ["Next trouble spot" sgml-next-trouble-spot t]
1067 ["Next data field" sgml-next-data-field t]
1068 ["Forward element" sgml-forward-element t]
1069 ["Backward element" sgml-backward-element t]
1070 ["Up element" sgml-up-element t]
1071 ["Down element" sgml-down-element t]
1072 ["Backward up element" sgml-backward-up-element t]
1073 ["Beginning of element" sgml-beginning-of-element t]
1074 ["End of element" sgml-end-of-element t])
1076 ["Fold Element" sgml-fold-element t]
1077 ["Fold Subelement" sgml-fold-subelement t]
1078 ["Unfold Line" sgml-unfold-line t]
1079 ["Unfold Element" sgml-unfold-element t]
1080 ["Expand" sgml-expand-element t]
1081 ["Fold Region" sgml-fold-region t]
1082 ["Unfold All" sgml-unfold-all t]
1083 ["Hide Tags" sgml-hide-tags t]
1084 ["Hide Attributes" sgml-hide-attributes t]
1085 ["Show All Tags" sgml-show-tags t])
1087 ["Normalize Document" sgml-normalize t]
1088 ["Normalize Element" sgml-normalize-element t]
1089 ["Expand All Short References" sgml-expand-all-shortrefs (not sgml-xml-p)]
1090 ["Expand Entity Reference" sgml-expand-entity-reference t]
1091 ["Make Character Reference" sgml-make-character-reference t]
1092 ["Unmake Character Reference" (sgml-make-character-reference t) t]
1093 ["Fill Element" sgml-fill-element t]
1094 ["Change Element Name..." sgml-change-element-name t]
1095 ["Edit Attributes..." sgml-edit-attributes t]
1096 ["Kill Markup" sgml-kill-markup t]
1097 ["Kill Element" sgml-kill-element t]
1098 ["Untag Element" sgml-untag-element t]
1099 ["Trim and leave element" sgml-trim-and-leave-element t]
1100 ["Decode Character Entities" sgml-charent-to-display-char t]
1101 ["Encode Characters" sgml-display-char-to-charent t]
1103 ("File Options" "---")
1104 ("User Options" "---")
1105 ["Reset Buffer" normal-mode t]
1106 ["Submit Bug Report" sgml-submit-bug-report t]
1109 ;; XEmacs change: psgml-xemacs hasn't been necessarily pulled in before
1113 (autoload 'sgml-make-options-menu "psgml-xemacs")))
1115 (defun sgml-options-menu-items (vars)
1116 ;; XEmacs change: use sgml-make-options-menu on XEmacs
1118 (sgml-make-options-menu vars)
1119 (mapcar (lambda (var)
1120 (let ((desc (format "%s [%s]"
1121 (sgml-variable-description var)
1122 (sgml-option-value-indicator var)))
1123 (type (sgml-variable-type var)))
1128 (if (consp c) (car c) (format "%s" c))
1129 `(setq ,var ',(if (consp c) (cdr c) c))
1133 (vector desc `(sgml-do-set-option ',var) t)))))
1136 (defun sgml-option-value-indicator (var)
1137 (let ((type (sgml-variable-type var))
1138 (val (symbol-value var)))
1141 (if val "Yes" "No"))
1144 (substring val 0 (min (length val) 4))
1146 ((and (atom type) val)
1149 (or (car (rassq val type))
1154 (defvar sgml-last-options-menu-values ())
1156 (defun sgml-any-option-changed (oldvalues vars)
1157 (not (loop for val in oldvalues
1159 always (eq val (symbol-value var)))))
1161 (defun sgml-update-options-menu (menuname option-vars &optional save-func)
1162 (let ((last-values (assoc menuname sgml-last-options-menu-values)))
1163 (when (or (null last-values)
1164 (sgml-any-option-changed (cdr last-values)
1167 ;; XEmacs change: `sgml-menu-name'
1168 (easy-menu-change (list sgml-menu-name) menuname
1169 (nconc (sgml-options-menu-items option-vars)
1172 (vector (format "Save %s" menuname)
1175 (message "Error in update menu: %s" err)))
1177 (setq last-values (cons menuname nil))
1178 (push last-values sgml-last-options-menu-values))
1179 (setf (cdr last-values) (mapcar (function symbol-value) option-vars)))))
1182 (defun sgml-update-all-options-menus ()
1183 (sgml-update-options-menu "File Options" sgml-file-options
1185 (sgml-update-options-menu "User Options" sgml-user-options)
1188 (defun sgml-compute-insert-dtd-items ()
1189 (loop for e in sgml-custom-dtd collect
1191 (` (sgml-doctype-insert (, (cadr e)) '(, (cddr e))))
1194 (defun sgml-compute-custom-markup-items ()
1195 (loop for e in sgml-custom-markup collect
1197 (` (sgml-insert-markup (, (cadr e))))
1200 (defun sgml-build-custom-menus ()
1201 "Build custom parts of Markup and DTD menus."
1202 (let ((button3 (lookup-key (current-local-map) [button3])))
1203 (unless (or (null button3)
1205 (local-set-key [button3] button3))
1206 (when sgml-custom-dtd
1207 (easy-menu-change '("SGML" "Insert Markup") "Insert DTD"
1208 (sgml-compute-insert-dtd-items)))
1209 (when sgml-custom-markup
1210 (easy-menu-change '("SGML" "Insert Markup") "Custom markup"
1211 (sgml-compute-custom-markup-items))))
1215 ;;;; Post command hook
1217 (defvar sgml-auto-activate-dtd-tried nil)
1218 (make-variable-buffer-local 'sgml-auto-activate-dtd-tried)
1220 (defvar sgml-buffer-parse-state nil
1221 "If the buffers DTD has been activated this contains the parser state.
1222 The parser state has been created with `sgml-make-pstate' and contains
1223 the information about the DTD and the parse tree. This parse state is
1224 actually only the state that persists between commands.")
1225 (make-variable-buffer-local 'sgml-buffer-parse-state)
1227 (eval-and-compile ; Interface to psgml-parse
1228 (loop for fun in '(sgml-need-dtd sgml-update-display
1232 do (autoload fun "psgml-parse")))
1235 (defun sgml-command-post ()
1236 (when (and (null sgml-buffer-parse-state)
1237 sgml-auto-activate-dtd
1238 (null sgml-auto-activate-dtd-tried)
1239 (not (zerop (buffer-size)))
1241 (setq sgml-auto-activate-dtd-tried t)
1244 (sgml-fontify-buffer 0)))
1245 (when sgml-buffer-parse-state
1246 (sgml-update-display)))
1249 ;;;; SGML mode: major mode definition
1251 ;;; This section is mostly from sgml-mode by James Clark.
1255 "Major mode for editing SGML.
1256 \\<sgml-mode-map>Makes > display the matching <. Makes / display matching /.
1257 Use \\[sgml-validate] to validate your document with an SGML parser.
1259 You can find information with:
1260 \\[sgml-show-context] Show the nesting of elements at cursor position.
1261 \\[sgml-list-valid-tags] Show the tags valid at cursor position.
1263 Insert tags with completion of contextually valid tags with \\[sgml-insert-tag].
1264 End the current element with \\[sgml-insert-end-tag]. Insert an element (i.e.
1265 both start and end tag) with \\[sgml-insert-element]. Or tag a region with
1266 \\[sgml-tag-region].
1268 To tag a region with the mouse, use transient mark mode or secondary selection.
1271 \\[sgml-backward-element] Moves backwards over the previous element.
1272 \\[sgml-forward-element] Moves forward over the next element.
1273 \\[sgml-down-element] Move forward and down one level in the element structure.
1274 \\[sgml-backward-up-element] Move backward out of this element level.
1275 \\[sgml-beginning-of-element] Move to after the start tag of the current element.
1276 \\[sgml-end-of-element] Move to before the end tag of the current element.
1277 \\[sgml-kill-element] Kill the element following the cursor.
1279 Finding interesting positions
1280 \\[sgml-next-data-field] Move forward to next point where data is allowed.
1281 \\[sgml-next-trouble-spot] Move forward to next point where something is
1282 amiss with the structure.
1284 Folding and unfolding
1285 \\[sgml-fold-element] Fold the lines comprising the current element, leaving
1286 the first line visible.
1287 \\[sgml-fold-subelement] Fold the elements in the content of the current element.
1288 Leaving the first line of every element visible.
1289 \\[sgml-unfold-line] Show hidden lines in current line.
1293 sgml-omittag Set this to reflect OMITTAG in the SGML declaration.
1294 sgml-shorttag Set this to reflect SHORTTAG in the SGML declaration.
1295 sgml-namecase-general Set this to reflect NAMECASE GENERAL in the SGML declaration.
1296 sgml-auto-insert-required-elements If non-nil, automatically insert required
1297 elements in the content of an inserted element.
1298 sgml-balanced-tag-edit If non-nil, always insert start-end tag pairs.
1299 sgml-omittag-transparent If non-nil, will show legal tags inside elements
1300 with omitable start tags and legal tags beyond omitable end tags.
1301 sgml-leave-point-after-insert If non-nil, the point will remain after
1303 sgml-warn-about-undefined-elements If non-nil, print a warning when a tag
1304 for a undefined element is found.
1305 sgml-max-menu-size Max number of entries in Tags and Entities menus before
1306 they are split into several panes.
1307 sgml-always-quote-attributes If non-nil, quote all attribute values
1308 inserted after finishing edit attributes.
1309 sgml-minimize-attributes Determines minimization of attributes inserted by
1311 sgml-normalize-trims If non-nil, sgml-normalize will trim off white space
1312 from end of element when adding end tag.
1313 sgml-indent-step How much to increment indent for every element level.
1314 sgml-indent-data If non-nil, indent in data/mixed context also.
1315 sgml-set-face If non-nil, psgml will set the face of parsed markup.
1316 sgml-markup-faces The faces used when the above variable is non-nil.
1317 sgml-system-path List of directories used to look for system identifiers.
1318 sgml-public-map Mapping from public identifiers to file names.
1319 sgml-offer-save If non-nil, ask about saving modified buffers before
1320 \\[sgml-validate] is run.
1325 (kill-all-local-variables)
1326 (setq sgml-xml-p nil)
1327 (setq local-abbrev-table sgml-mode-abbrev-table)
1328 (use-local-map sgml-mode-map)
1329 (setq mode-name "SGML")
1330 (setq major-mode 'sgml-mode)
1332 ;; A start or end tag by itself on a line separates a paragraph.
1333 ;; This is desirable because SGML discards a newline that appears
1334 ;; immediately after a start tag or immediately before an end tag.
1336 (set (make-local-variable 'paragraph-separate)
1337 (if sgml-have-re-char-clases
1339 ^[ \t]*</?\\([_[:alpha:]]\\([-:._[:alnum:]= \t\n]\\|\
1340 \"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
1342 ^[ \t]*</?\\([_A-Za-z]\\([-:._A-Za-z0-9= \t\n]\\|\
1343 \"[^\"]*\"\\|'[^']*'\\)*\\)?>$"))
1344 (set (make-local-variable 'paragraph-start)
1347 (set-syntax-table sgml-mode-syntax-table)
1348 (make-local-variable 'comment-start)
1349 (setq comment-start "<!-- ")
1350 (make-local-variable 'comment-end)
1351 (setq comment-end " -->")
1352 (make-local-variable 'comment-indent-function)
1353 (setq comment-indent-function 'sgml-comment-indent)
1354 (make-local-variable 'comment-start-skip)
1355 ;; This will allow existing comments within declarations to be
1356 ;; recognized. [Does not work well with auto-fill, Lst/940205]
1357 ;;(setq comment-start-skip "--[ \t]*")
1358 (setq comment-start-skip "<!--[ \t]*")
1360 (make-local-variable 'indent-line-function)
1361 (setq indent-line-function 'sgml-indent-line)
1362 (make-local-variable 'mode-line-format)
1363 (if running-xemacs ;; XEmacs change
1364 ;; Modify mode-line-format with subst (suggested by wing)
1365 ;; Apart from requiring CL at runtime, this doesn't work in Emacs
1366 ;; 21. It's the sort of thing which-func is supposed to do...
1367 (setq mode-line-format
1368 (subst '("" mode-name sgml-active-dtd-indicator) 'mode-name
1370 (set (make-local-variable 'which-func-format) 'sgml-active-dtd-indicator))
1371 (make-local-variable 'sgml-default-dtd-file)
1372 (when (setq sgml-default-dtd-file (sgml-default-dtd-file))
1373 (unless (file-exists-p sgml-default-dtd-file)
1374 (setq sgml-default-dtd-file nil)))
1375 ;;; This doesn't DTRT with Emacs 21.1 newcomment -- intermediate lines
1376 ;;; are prefixed by `!--'. -- fx
1377 ;;; (set (make-local-variable 'comment-style) 'multi-line)
1378 (when sgml-default-nonsticky
1379 (make-local-variable 'text-property-default-nonsticky)
1380 ;; see `sgml-set-face-for':
1381 (add-to-list 'text-property-default-nonsticky '(face . t)))
1382 (make-local-hook 'post-command-hook)
1383 (add-hook 'post-command-hook 'sgml-command-post 'append 'local)
1384 (unless running-xemacs ;; XEmacs change
1385 ;; XEmacs 20.4 doesn't handle local activate-menubar-hook
1386 ;; it tries to call the function `t' when using the menubar
1387 (make-local-hook 'activate-menubar-hook))
1388 (add-hook 'activate-menubar-hook 'sgml-update-all-options-menus
1390 (run-hooks 'text-mode-hook 'sgml-mode-hook)
1391 (easy-menu-add sgml-main-menu)
1392 (sgml-build-custom-menus))
1394 ;; It would be nice to generalize the `auto-mode-interpreter-regexp'
1395 ;; machinery so that we could select xml-mode on the basis of the
1396 ;; leading xml PI. -- fx
1398 ;; XEmacs hack: autoload a dummy autoload instead of a derived mode.
1399 ;;;###autoload(autoload 'xml-mode "psgml" nil t)
1400 (define-derived-mode xml-mode sgml-mode "XML"
1401 "Major mode for editing XML documents.
1402 In this mode `sgml-validate-command' and `sgml-declaration' are
1403 initialized from `sgml-xml-validate-command' and
1404 `sgml-xml-declaration' respectively.
1406 Can be used without a DTD. In that case, warnings about undefined
1407 elements and entities are suppressed and various commands' behaviour
1408 is modified to account for the lack of information. For instance, the
1409 element names offered for selection or completion are those in the
1410 parse of the document, but other names may be entered.
1412 Note that without a DTD, indenting lines will only work if
1413 `sgml-indent-data' is non-nil."
1414 ;; XEmacs change: use HTML font lock keywords. Urk, this is bad!
1417 (require 'psgml-html)
1418 (put 'xml-mode 'font-lock-defaults '(html-font-lock-keywords nil t))))
1420 ;; XML-friendly settings
1421 (setq sgml-omittag nil)
1422 (setq sgml-shorttag nil)
1423 (setq sgml-namecase-general nil)
1424 (setq sgml-minimize-attributes nil)
1425 (setq sgml-always-quote-attributes t)
1426 (setq sgml-validate-command sgml-xml-validate-command)
1427 (make-local-variable 'sgml-declaration)
1428 (setq sgml-declaration sgml-xml-declaration))
1431 (defun sgml-default-dtd-file ()
1432 (and (buffer-file-name)
1433 (let ((base (file-name-nondirectory (buffer-file-name))))
1435 (cond ((string-match "\\.[^.]+$" base)
1436 (substring base 0 (match-beginning 0)))
1441 (defun sgml-comment-indent ()
1442 (if (and (looking-at "--")
1443 (not (and (eq (char-after (1- (point))) ?!)
1444 (eq (char-after (- (point) 2)) ?<))))
1446 (skip-chars-backward " \t")
1447 (max comment-column (1+ (current-column))))
1450 (defconst sgml-start-tag-regex
1451 (if sgml-have-re-char-clases
1452 "<[_[:alpha:]]\\([-:.[:alnum:]= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
1453 "<[_A-Za-z]\\([-:.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*")
1454 "Regular expression that matches a non-empty start tag.
1455 Any terminating > or / is not matched.")
1457 (defvar sgml-mode-markup-syntax-table nil
1458 "Syntax table used for scanning SGML markup.")
1460 (if sgml-mode-markup-syntax-table
1462 (setq sgml-mode-markup-syntax-table (make-syntax-table))
1463 (modify-syntax-entry ?< "(>" sgml-mode-markup-syntax-table)
1464 (modify-syntax-entry ?> ")<" sgml-mode-markup-syntax-table)
1465 (modify-syntax-entry ?- "_ 1234" sgml-mode-markup-syntax-table)
1466 (modify-syntax-entry ?\' "\"" sgml-mode-markup-syntax-table))
1468 (defvar sgml-angle-distance 4000
1469 "*If non-nil, is the maximum distance to search for matching <.")
1471 (defun sgml-close-angle (arg)
1472 "Insert > and display matching <."
1474 (insert-char ?> arg)
1476 (let ((oldpos (point))
1480 (if sgml-angle-distance
1481 (narrow-to-region (max (point-min)
1482 (- (point) sgml-angle-distance))
1484 ;; See if it's the end of a marked section.
1485 (and (> (- (point) (point-min)) 3)
1486 (eq (char-after (- (point) 2)) ?\])
1487 (eq (char-after (- (point) 3)) ?\])
1488 (re-search-backward (if sgml-have-re-char-clases
1489 "<!\\[\\(-?[[:alnum:]. \t\n&;]\\|\
1490 --\\([^-]\\|-[^-]\\)*--\\)*\\["
1491 "<!\\[\\(-?[A-Za-z0-9. \t\n&;]\\|\
1492 --\\([^-]\\|-[^-]\\)*--\\)*\\[")
1495 (let ((msspos (point)))
1496 (if (and (search-forward "]]>" oldpos t)
1497 (eq (point) oldpos))
1498 (setq blinkpos msspos))))
1499 ;; This handles cases where the > ends one of the following:
1500 ;; markup declaration starting with <! (possibly including a
1501 ;; declaration subset); start tag; end tag; SGML declaration.
1506 (let ((oldtable (syntax-table))
1507 (parse-sexp-ignore-comments t))
1510 (set-syntax-table sgml-mode-markup-syntax-table)
1511 (setq blinkpos (scan-sexps oldpos -1)))
1512 (set-syntax-table oldtable)))
1515 (goto-char blinkpos)
1517 ;; Check that it's a valid delimiter in context.
1519 (if sgml-have-re-char-clases
1520 "<\\(\\?\\|/?[[:alpha:]>]\\|!\\([[[:alpha:]]\\|--\\)\\)"
1521 "<\\(\\?\\|/?[A-Za-z>]\\|!\\([[A-Za-z]\\|--\\)\\)")))
1522 ;; Check that it's not a net-enabling start tag
1523 ;; nor an unclosed start-tag.
1524 (looking-at (concat sgml-start-tag-regex "[/<]"))
1525 ;; Nor an unclosed end-tag.
1526 (looking-at (if sgml-have-re-char-clases
1527 "</[[:alpha:]][-:.[:alnum:]]*[ \t]*<"
1528 "</[A-Za-z][-:.A-Za-z0-9]*[ \t]*<")))
1529 (setq blinkpos nil)))
1532 ;; See if it's the end of a processing instruction.
1534 (if (search-backward "<?" (point-min) t)
1535 (let ((pipos (point)))
1536 (if (and (search-forward ">" oldpos t)
1537 (eq (point) oldpos))
1538 (setq blinkpos pipos))))))
1541 (goto-char blinkpos)
1542 (if (pos-visible-in-window-p)
1544 (message "Matches %s"
1545 (buffer-substring blinkpos
1546 (progn (end-of-line)
1549 ;;; I doubt that null end tags are used much for large elements,
1550 ;;; so use a small distance here.
1551 (defvar sgml-slash-distance 1000
1552 "*If non-nil, is the maximum distance to search for matching /.")
1554 (defun sgml-slash (arg)
1555 "Insert / and display any previous matching /.
1556 Two /s are treated as matching if the first / ends a net-enabling
1557 start tag, and the second / is the corresponding null end tag."
1559 (insert-char ?/ arg)
1561 (let ((oldpos (point))
1566 (if sgml-slash-distance
1567 (narrow-to-region (max (point-min)
1568 (- (point) sgml-slash-distance))
1570 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
1571 (eq (match-end 0) (1- oldpos)))
1573 (goto-char (1- oldpos))
1574 (while (and (not blinkpos)
1575 (search-backward "/" (point-min) t))
1576 (let ((tagend (save-excursion
1577 (if (re-search-backward sgml-start-tag-regex
1581 (if (eq tagend (point))
1583 (setq blinkpos (point))
1584 (setq level (1- level)))
1585 (setq level (1+ level)))))))
1588 (goto-char blinkpos)
1589 (if (pos-visible-in-window-p)
1591 (message "Matches %s"
1592 (buffer-substring (progn
1595 (1+ blinkpos))))))))))
1598 (autoload 'compile-internal "compile" ""))
1600 (defun sgml-default-validate-command ()
1602 ((consp sgml-validate-command)
1603 (let ((validate-subst
1605 (cons ?b (and (buffer-file-name)
1606 (file-name-nondirectory (buffer-file-name))))
1607 (cons ?s (sgml-declaration))
1608 (cons ?v sgml-declaration)
1609 (cons ?d sgml-doctype))))
1610 (loop for template in sgml-validate-command
1612 (sgml-subst-expand template validate-subst))))
1614 (apply 'format sgml-validate-command
1615 (if sgml-validate-files
1616 (funcall sgml-validate-files)
1617 (list (or sgml-declaration "")
1618 (let ((name (buffer-file-name)))
1620 (file-name-nondirectory name)
1623 (defun sgml-validate (command)
1624 "Validate an SGML document.
1625 Runs COMMAND, a shell command, in a separate process asynchronously
1626 with output going to the buffer *compilation*.
1627 You can then use the command \\[next-error] to find the next error message
1628 and move to the line in the SGML document that caused it."
1630 (list (read-from-minibuffer "Validate command: "
1631 (sgml-default-validate-command)
1632 nil nil 'sgml-validate-command-history)))
1634 (save-some-buffers nil nil))
1635 (compile-internal command "No more errors" "SGML validation"
1637 sgml-validate-error-regexps))
1639 (defalias 'sgml-restore-buffer-modified-p
1640 (if (fboundp 'restore-buffer-modified-p)
1641 'restore-buffer-modified-p ; doesn't update mode line
1642 'set-buffer-modified-p))
1644 ;;;; Autoloads and hooks
1646 (autoload 'sgml-doctype-insert "psgml-edit"
1649 (autoload 'sgml-indent-line "psgml-edit" nil)
1650 (autoload 'sgml-element-endable-p "psgml-edit" nil)
1651 (autoload 'sgml-do-set-option "psgml-edit" nil)
1653 ;;; Generated by sgml-build-autoloads
1655 (autoload 'sgml-load-dtd "psgml-parse" "Load a saved DTD from FILE." t)
1656 (autoload 'sgml-show-or-clear-log "psgml-parse" "Show the *SGML LOG* buffer if it is not showing, or clear and
1657 remove it if it is showing." t)
1658 (autoload 'sgml-load-doctype "psgml-parse" "Load the documents DTD.
1659 Either from parent document or by parsing the document prolog." t)
1660 (autoload 'sgml-parse-prolog "psgml-parse" "Parse the document prolog to learn the DTD." t)
1661 (autoload 'sgml-beginning-of-element "psgml-edit" "Move to after the start-tag of the current element.
1662 If the start-tag is implied, move to the start of the element." t)
1663 (autoload 'sgml-end-of-element "psgml-edit" "Move to before the end-tag of the current element." t)
1664 (autoload 'sgml-backward-up-element "psgml-edit" "Move backward out of this element level.
1665 That is move to before the start-tag or where a start-tag is implied." t)
1666 (autoload 'sgml-up-element "psgml-edit" "Move forward out of this element level.
1667 That is move to after the end-tag or where an end-tag is implied." t)
1668 (autoload 'sgml-forward-element "psgml-edit" "Move forward over next element." t)
1669 (autoload 'sgml-backward-element "psgml-edit" "Move backward over previous element at this level.
1670 With implied tags this is ambiguous." t)
1671 (autoload 'sgml-down-element "psgml-edit" "Move forward and down one level in the element structure." t)
1672 (autoload 'sgml-kill-element "psgml-edit" "Kill the element following the cursor." t)
1673 (autoload 'sgml-transpose-element "psgml-edit" "Interchange element before point with element after point, leave point after." t)
1674 (autoload 'sgml-mark-element "psgml-edit" "Set mark after next element." t)
1675 (autoload 'sgml-mark-current-element "psgml-edit" "Set mark at end of current element, and leave point before current element." t)
1676 (autoload 'sgml-change-element-name "psgml-edit" "Replace the name of the current element with a new name.
1677 Eventual attributes of the current element will be translated if
1679 (autoload 'sgml-untag-element "psgml-edit" "Remove tags from current element." t)
1680 (autoload 'sgml-kill-markup "psgml-edit" "Kill next tag, markup declaration or process instruction." t)
1681 (autoload 'sgml-fold-region "psgml-edit" "Hide (or if prefixarg unhide) region.
1682 If called from a program first two arguments are start and end of
1683 region. And optional third argument true unhides." t)
1684 (autoload 'sgml-fold-element "psgml-edit" "Fold the lines comprising the current element, leaving the first line visible.
1685 This uses the selective display feature." t)
1686 (autoload 'sgml-fold-subelement "psgml-edit" "Fold all elements current elements content, leaving the first lines visible.
1687 This uses the selective display feature." t)
1688 (autoload 'sgml-unfold-line "psgml-edit" "Show hidden lines in current line." t)
1689 (autoload 'sgml-unfold-element "psgml-edit" "Show all hidden lines in current element." t)
1690 (autoload 'sgml-expand-element "psgml-edit" "As sgml-fold-subelement, but unfold first." t)
1691 (autoload 'sgml-unfold-all "psgml-edit" "Show all hidden lines in buffer." t)
1692 (autoload 'sgml-next-data-field "psgml-edit" "Move forward to next point where data is allowed." t)
1693 (autoload 'sgml-next-trouble-spot "psgml-edit" "Move forward to next point where something is amiss with the structure." t)
1694 (autoload 'sgml-list-valid-tags "psgml-edit" "Display a list of the contextually valid tags." t)
1695 (autoload 'sgml-show-context "psgml-edit" "Display where the cursor is in the element hierarchy." t)
1696 (autoload 'sgml-what-element "psgml-edit" "Display what element is under the cursor." t)
1697 (autoload 'sgml-insert-tag "psgml-edit" "Insert a tag, reading tag name in minibuffer with completion.
1698 If the variable `sgml-balanced-tag-edit' is t, also inserts the
1699 corresponding end tag. If `sgml-leave-point-after-insert' is t, the point
1700 is left after the inserted tag(s), unless the element has some required
1701 content. If `sgml-leave-point-after-insert' is nil, the point is left
1702 after the first tag inserted." t)
1703 (autoload 'sgml-insert-element "psgml-edit" "Reads element name from minibuffer and inserts start and end tags." t)
1704 (autoload 'sgml-tag-region "psgml-edit" "Reads element name from minibuffer and inserts start and end tags." t)
1705 (autoload 'sgml-insert-end-tag "psgml-edit" "Insert end-tag for the current open element." t)
1706 (autoload 'sgml-insert-attribute "psgml-edit" "Read attribute name and value from minibuffer and insert attribute spec." t)
1707 (autoload 'sgml-split-element "psgml-edit" "Split the current element at point.
1708 If repeated, the containing element will be split before the beginning
1709 of then current element." t)
1710 (autoload 'sgml-custom-dtd "psgml-edit" "Insert a DTD declaration from the sgml-custom-dtd alist." t)
1711 (autoload 'sgml-custom-markup "psgml-edit" "Insert markup from the sgml-custom-markup alist." t)
1712 (autoload 'sgml-tags-menu "psgml-edit" "Pop up a menu with valid tags and insert the chosen tag.
1713 If the variable sgml-balanced-tag-edit is t, also inserts the
1714 corresponding end tag. If sgml-leave-point-after-insert is t, the point
1715 is left after the inserted tag(s), unless the element has some required
1716 content. If sgml-leave-point-after-insert is nil the point is left
1717 after the first tag inserted." t)
1718 (autoload 'sgml-element-menu "psgml-edit" "Pop up a menu with valid elements and insert choice.
1719 If sgml-leave-point-after-insert is nil the point is left after the first
1721 (autoload 'sgml-add-element-menu "psgml-edit" nil t)
1722 (autoload 'sgml-start-tag-menu "psgml-edit" "Pop up a menu with valid start-tags and insert choice." t)
1723 (autoload 'sgml-end-tag-menu "psgml-edit" "Pop up a menu with valid end-tags and insert choice." t)
1724 (autoload 'sgml-tag-region-menu "psgml-edit" "Pop up a menu with valid elements and tag current region with the choice." t)
1725 (autoload 'sgml-entities-menu "psgml-edit" nil t)
1726 (autoload 'sgml-attrib-menu "psgml-edit" "Pop up a menu of the attributes of the current element
1727 \(or the element with start-tag before point)." t)
1728 (autoload 'sgml-right-menu "psgml-edit" "Pop up a menu with valid tags and insert the choosen tag.
1729 If the variable sgml-balanced-tag-edit is t, also inserts the
1730 corresponding end tag. If sgml-leave-point-after-insert is t, the point
1731 is left after the inserted tag(s), unless the element has som required
1732 content. If sgml-leave-point-after-insert is nil the point is left
1733 after the first tag inserted." t)
1734 (autoload 'sgml-fill-element "psgml-edit" "Fill biggest enclosing element with mixed content.
1735 If current element has pure element content, recursively fill the
1737 (autoload 'sgml-edit-attributes "psgml-edit" "Edit attributes of current element.
1738 Editing is done in a separate window." t)
1739 (autoload 'sgml-edit-attrib-finish "psgml-edit" "Finish editing and insert attribute values in original buffer." t)
1740 (autoload 'sgml-edit-attrib-default "psgml-edit" "Set current attribute value to default." t)
1741 (autoload 'sgml-edit-attrib-clear "psgml-edit" "Kill the value of current attribute." t)
1742 (autoload 'sgml-edit-attrib-field-start "psgml-edit" "Go to the start of the attribute value field." t)
1743 (autoload 'sgml-edit-attrib-field-end "psgml-edit" "Go to the end of the attribute value field." t)
1744 (autoload 'sgml-edit-attrib-next "psgml-edit" "Move to next attribute value." t)
1745 (autoload 'sgml-hide-tags "psgml-edit" "Hide all tags in buffer." t)
1746 (autoload 'sgml-show-tags "psgml-edit" "Show hidden tags in buffer." t)
1747 (autoload 'sgml-hide-attributes "psgml-edit" "Hide all attribute specifications in the buffer." t)
1748 (autoload 'sgml-show-attributes "psgml-edit" "Show all attribute specifications in the buffer." t)
1749 (autoload 'sgml-expand-all-shortrefs "psgml-edit" "Expand all short references in the buffer.
1750 Short references to text entities are expanded to the replacement text
1751 of the entity; other short references are expanded into general entity
1752 references. If argument TO-ENTITY is non-nil, or if called
1753 interactively with a numeric prefix argument, all short references are
1754 replaced by general entity references." t)
1755 (autoload 'sgml-normalize "psgml-edit" "Normalize buffer by filling in omitted tags and expanding empty tags.
1756 Argument TO-ENTITY controls how short references are expanded as with
1757 `sgml-expand-all-shortrefs'. An optional argument ELEMENT can be the
1758 element to normalize instead of the whole buffer, if used no short
1759 references will be expanded." t)
1760 (autoload 'sgml-normalize-element "psgml-edit" nil t)
1761 (autoload 'sgml-make-character-reference "psgml-edit" "Convert character after point into a character reference.
1762 If called with a numeric argument, convert a character reference back
1763 to a normal character. If called from a program, set optional
1764 argument INVERT to non-nil." t)
1765 (autoload 'sgml-expand-entity-reference "psgml-edit" "Insert the text of the entity referenced at point." t)
1766 (autoload 'sgml-trim-and-leave-element "psgml-edit" "Remove blanks at end of current element and move point to after element." t)
1767 (autoload 'sgml-edit-external-entity "psgml-edit" "Open a new window and display the external entity at the point." t)
1768 (autoload 'sgml-complete "psgml-edit" "Complete the word/tag/entity before point.
1769 If it is a tag (starts with < or </) complete with valid tags.
1770 If it is an entity (starts with &) complete with declared entities.
1771 If it is a markup declaration (starts with <!) complete with markup
1773 If it is something else complete with ispell-complete-word." t)
1774 (autoload 'sgml-file-options-menu "psgml-edit" nil t)
1775 (autoload 'sgml-user-options-menu "psgml-edit" nil t)
1776 (autoload 'sgml-add-element-to-element "psgml-edit" "Add an element of type GI to the current element.
1777 The element will be added at the last legal position if FIRST is `nil',
1778 otherwise it will be added at the first legal position." t)
1779 (autoload 'sgml-show-current-element-type "psgml-edit" "Show information about the current element and its type." t)
1780 (autoload 'sgml-show-structure "psgml-edit" "Show the document structure in a separate buffer." t)
1781 (autoload 'sgml-save-dtd "psgml-dtd" "Save the parsed dtd on FILE." t)
1782 (autoload 'sgml-list-elements "psgml-info" "List the elements and their attributes in the current DTD." t)
1783 (autoload 'sgml-list-attributes "psgml-info" "List the attributes and in which elements they occur." t)
1784 (autoload 'sgml-list-terminals "psgml-info" "List the elements that can have data in their content." t)
1785 (autoload 'sgml-list-content-elements "psgml-info" "List all element types and the element types that can occur in its content." t)
1786 (autoload 'sgml-list-occur-in-elements "psgml-info" "List all element types and where it can occur." t)
1787 (autoload 'sgml-describe-entity "psgml-info" "Describe the properties of an entity as declared in the current DTD." t)
1788 (autoload 'sgml-describe-element-type "psgml-info" "Describe the properties of an element type as declared in the current DTD." t)
1789 (autoload 'sgml-describe-dtd "psgml-info" "Display information about the current DTD." t)
1790 (autoload 'sgml-charent-to-display-char "psgml-charent" "Replace character entities with their display character equivalents" t)
1791 (autoload 'sgml-display-char-to-charent "psgml-charent" "Replace displayable characters with their character entity equivalents" t)
1794 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.\\(?:sgml?\\|dtd\\)\\'" . sgml-mode))
1795 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.xml\\'" . xml-mode))
1796 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.xsd\\'" . xml-mode))
1799 ;;;; Last provisions
1802 (provide 'sgml-mode)
1805 ;;; psgml.el ends here