;;; sgml-mode.el --- SGML- and HTML-editing modes ;; Copyright (C) 1992,95,96,98,2001,2002 Free Software Foundation, Inc. ;; Author: James Clark ;; Maintainer: FSF ;; Adapted-By: ESR, Daniel Pfeiffer , ;; F.Potorti@cnuce.cnr.it ;; Keywords: wp, hypermedia, comm, languages ;; This file is part of GNU Emacs. ;; GNU Emacs is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Commentary: ;; Configurable major mode for editing document in the SGML standard general ;; markup language. As an example contains a mode for editing the derived ;; HTML hypertext markup language. ;;; Code: (eval-when-compile (require 'skeleton) (require 'outline)) (defgroup sgml nil "SGML editing mode" :group 'languages) (defcustom sgml-basic-offset 2 "*Specifies the basic indentation level for `sgml-indent-line'." :type 'integer :group 'sgml) (defcustom sgml-transformation 'identity "*Default value for `skeleton-transformation' (which see) in SGML mode." :type 'function :group 'sgml) (put 'sgml-transformation 'variable-interactive "aTransformation function: ") (defcustom sgml-mode-hook nil "Hook run by command `sgml-mode'. `text-mode-hook' is run first." :group 'sgml :type 'hook) ;; As long as Emacs' syntax can't be complemented with predicates to context ;; sensitively confirm the syntax of characters, we have to live with this ;; kludgy kind of tradeoff. (defvar sgml-specials '(?\") "List of characters that have a special meaning for SGML mode. This list is used when first loading the `sgml-mode' library. The supported characters and potential disadvantages are: ?\\\" Makes \" in text start a string. ?' Makes ' in text start a string. ?- Makes -- in text start a comment. When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in DTDs, start a string. To partially avoid this problem this also makes these self insert as named entities depending on `sgml-quick-keys'. Including ?- has the problem of affecting dashes that have nothing to do with comments, so we normally turn it off.") (defvar sgml-quick-keys nil "Use <, >, &, /, SPC and `sgml-specials' keys \"electrically\" when non-nil. This takes effect when first loading the `sgml-mode' library.") (defvar sgml-mode-map (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets. (menu-map (make-sparse-keymap "SGML"))) (define-key map "\C-c\C-i" 'sgml-tags-invisible) (define-key map "/" 'sgml-slash) (define-key map "\C-c\C-n" 'sgml-name-char) (define-key map "\C-c\C-t" 'sgml-tag) (define-key map "\C-c\C-a" 'sgml-attributes) (define-key map "\C-c\C-b" 'sgml-skip-tag-backward) (define-key map [?\C-c left] 'sgml-skip-tag-backward) (define-key map "\C-c\C-f" 'sgml-skip-tag-forward) (define-key map [?\C-c right] 'sgml-skip-tag-forward) (define-key map "\C-c\C-d" 'sgml-delete-tag) (define-key map "\C-c\^?" 'sgml-delete-tag) (define-key map "\C-c?" 'sgml-tag-help) (define-key map "\C-c/" 'sgml-close-tag) (define-key map "\C-c8" 'sgml-name-8bit-mode) (define-key map "\C-c\C-v" 'sgml-validate) (when sgml-quick-keys (define-key map "&" 'sgml-name-char) (define-key map "<" 'sgml-tag) (define-key map " " 'sgml-auto-attributes) (define-key map ">" 'sgml-maybe-end-tag) (when (memq ?\" sgml-specials) (define-key map "\"" 'sgml-name-self)) (when (memq ?' sgml-specials) (define-key map "'" 'sgml-name-self))) (define-key map (vector (make-char 'latin-iso8859-1)) 'sgml-maybe-name-self) (let ((c 127) (map (nth 1 map))) (while (< (setq c (1+ c)) 256) (aset map c 'sgml-maybe-name-self))) (define-key map [menu-bar sgml] (cons "SGML" menu-map)) (define-key menu-map [sgml-validate] '("Validate" . sgml-validate)) (define-key menu-map [sgml-name-8bit-mode] '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode)) (define-key menu-map [sgml-tags-invisible] '("Toggle Tag Visibility" . sgml-tags-invisible)) (define-key menu-map [sgml-tag-help] '("Describe Tag" . sgml-tag-help)) (define-key menu-map [sgml-delete-tag] '("Delete Tag" . sgml-delete-tag)) (define-key menu-map [sgml-skip-tag-forward] '("Forward Tag" . sgml-skip-tag-forward)) (define-key menu-map [sgml-skip-tag-backward] '("Backward Tag" . sgml-skip-tag-backward)) (define-key menu-map [sgml-attributes] '("Insert Attributes" . sgml-attributes)) (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag)) map) "Keymap for SGML mode. See also `sgml-specials'.") (defun sgml-make-syntax-table (specials) (let ((table (make-syntax-table text-mode-syntax-table))) (modify-syntax-entry ?< "(>" table) (modify-syntax-entry ?> ")<" table) (modify-syntax-entry ?: "_" table) (modify-syntax-entry ?_ "_" table) (modify-syntax-entry ?. "_" table) (if (memq ?- specials) (modify-syntax-entry ?- "_ 1234" table)) (if (memq ?\" specials) (modify-syntax-entry ?\" "\"\"" table)) (if (memq ?' specials) (modify-syntax-entry ?\' "\"'" table)) table)) (defvar sgml-mode-syntax-table (sgml-make-syntax-table sgml-specials) "Syntax table used in SGML mode. See also `sgml-specials'.") (defconst sgml-tag-syntax-table (let ((table (sgml-make-syntax-table '(?- ?\" ?\')))) (dolist (char '(?\( ?\) ?\{ ?\} ?\[ ?\] ?$ ?% ?& ?* ?+ ?/)) (modify-syntax-entry char "." table)) table) "Syntax table used to parse SGML tags.") (defcustom sgml-name-8bit-mode nil "*When non-nil, insert non-ASCII characters as named entities." :type 'boolean :group 'sgml) (defvar sgml-char-names [nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos" "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol" nil nil nil nil nil nil nil nil nil nil "colon" "semi" "lt" "eq" "gt" "quest" "commat" nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar" "lsquo" nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil "lcub" "verbar" "rcub" "tilde" nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect" "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr" "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot" "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest" "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil" "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml" "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig" "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil" "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml" "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide" "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"] "Vector of symbolic character names without `&' and `;'.") (put 'sgml-table 'char-table-extra-slots 0) (defvar sgml-char-names-table (let ((table (make-char-table 'sgml-table)) (i 32) elt) (while (< i 256) (setq elt (aref sgml-char-names i)) (if elt (aset table (make-char 'latin-iso8859-1 i) elt)) (setq i (1+ i))) table) "A table for mapping non-ASCII characters into SGML entity names. Currently, only Latin-1 characters are supported.") ;; nsgmls is a free SGML parser in the SP suite available from ;; ftp.jclark.com and otherwise packaged for GNU systems. ;; Its error messages can be parsed by next-error. ;; The -s option suppresses output. (defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls' "*The command to validate an SGML document. The file name of current buffer file name will be appended to this, separated by a space." :type 'string :version "21.1" :group 'sgml) (defvar sgml-saved-validate-command nil "The command last used to validate in this buffer.") ;; I doubt that null end tags are used much for large elements, ;; so use a small distance here. (defcustom sgml-slash-distance 1000 "*If non-nil, is the maximum distance to search for matching `/'." :type '(choice (const nil) integer) :group 'sgml) (defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*") (defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)")) (defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*") (defconst sgml-start-tag-regex (concat "<" sgml-name-re sgml-attrs-re) "Regular expression that matches a non-empty start tag. Any terminating `>' or `/' is not matched.") ;; internal (defconst sgml-font-lock-keywords-1 `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face) (,(concat "<\\(/?" sgml-name-re"\\)") 1 font-lock-function-name-face) ;; FIXME: this doesn't cover the variables using a default value. (,(concat "\\(" sgml-name-re "\\)=[\"']") 1 font-lock-variable-name-face) (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face))) (defconst sgml-font-lock-keywords-2 (append sgml-font-lock-keywords-1 '((eval . (cons (concat "<" (regexp-opt (mapcar 'car sgml-tag-face-alist) t) "\\([ \t][^>]*\\)?>\\([^<]+\\)") '(3 (cdr (assoc (downcase (match-string 1)) sgml-tag-face-alist)))))))) ;; for font-lock, but must be defvar'ed after ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.") (defvar sgml-font-lock-syntactic-keywords ;; Use the `b' style of comments to avoid interference with the -- ... -- ;; comments recognized when `sgml-specials' includes ?-. ;; FIXME: beware of blabla !! '(("\\(<\\)!--" (1 "< b")) ("--[ \t\n]*\\(>\\)" (1 "> b"))) "Syntactic keywords for `sgml-mode'.") ;; internal (defvar sgml-face-tag-alist () "Alist of face and tag name for facemenu.") (defvar sgml-tag-face-alist () "Tag names and face or list of faces to fontify with when invisible. When `font-lock-maximum-decoration' is 1 this is always used for fontifying. When more these are fontified together with `sgml-font-lock-keywords'.") (defvar sgml-display-text () "Tag names as lowercase symbols, and display string when invisible.") ;; internal (defvar sgml-tags-invisible nil) (defcustom sgml-tag-alist '(("![" ("ignore" t) ("include" t)) ("!attlist") ("!doctype") ("!element") ("!entity")) "*Alist of tag names for completing read and insertion rules. This alist is made up as ((\"tag\" . TAGRULE) ...) TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor followed by an ATTRIBUTERULE (for an always present attribute) or an attribute alist. The attribute alist is made up as ((\"attribute\" . ATTRIBUTERULE) ...) ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by an optional alist of possible values." :type '(repeat (cons (string :tag "Tag Name") (repeat :tag "Tag Rule" sexp))) :group 'sgml) (defcustom sgml-tag-help '(("!" . "Empty declaration for comment") ("![" . "Embed declarations with parser directive") ("!attlist" . "Tag attributes declaration") ("!doctype" . "Document type (DTD) declaration") ("!element" . "Tag declaration") ("!entity" . "Entity (macro) declaration")) "*Alist of tag name and short description." :type '(repeat (cons (string :tag "Tag Name") (string :tag "Description"))) :group 'sgml) (defcustom sgml-xml-mode nil "*When non-nil, tag insertion functions will be XML-compliant. If this variable is customized, the custom value is used always. Otherwise, it is set to be buffer-local when the file has a DOCTYPE or an XML declaration." :type 'boolean :version "21.2" :group 'sgml) (defvar sgml-empty-tags nil "List of tags whose !ELEMENT definition says EMPTY.") (defvar sgml-unclosed-tags nil "List of tags whose !ELEMENT definition says the end-tag is optional.") (defun sgml-xml-guess () "Guess whether the current buffer is XML." (save-excursion (goto-char (point-min)) (when (or (string= "xml" (file-name-extension (or buffer-file-name ""))) (looking-at "\\s-*<\\?xml") (when (re-search-forward (eval-when-compile (mapconcat 'identity '("")) (concat "<" face ">")) (error "Face not configured for %s mode" mode-name))) ;;;###autoload (define-derived-mode sgml-mode text-mode "SGML" "Major mode for editing SGML documents. Makes > match <. Keys <, &, SPC within <>, \", / and ' can be electric depending on `sgml-quick-keys'. An argument of N to a tag-inserting command means to wrap it around the next N words. In Transient Mark mode, when the mark is active, N defaults to -1, which means to wrap it around the current region. If you like upcased tags, put (setq sgml-transformation 'upcase) in your `.emacs' file. Use \\[sgml-validate] to validate your document with an SGML parser. Do \\[describe-variable] sgml- SPC to see available variables. Do \\[describe-key] on the following bindings to discover what they do. \\{sgml-mode-map}" (make-local-variable 'sgml-saved-validate-command) (make-local-variable 'facemenu-end-add-face) ;;(make-local-variable 'facemenu-remove-face-function) ;; A start or end tag by itself on a line separates a paragraph. ;; This is desirable because SGML discards a newline that appears ;; immediately after a start tag or immediately before an end tag. (set (make-local-variable 'paragraph-start) (concat "[ \t]*$\\|\ \[ \t]*")) (set (make-local-variable 'paragraph-separate) (concat paragraph-start "$")) (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*") (set (make-local-variable 'indent-line-function) 'sgml-indent-line) (set (make-local-variable 'comment-start) "") (set (make-local-variable 'comment-indent-function) 'sgml-comment-indent) (set (make-local-variable 'skeleton-further-elements) '((completion-ignore-case t))) (set (make-local-variable 'skeleton-end-hook) (lambda () (or (eolp) (not (or (eq v2 '\n) (eq (car-safe v2) '\n))) (newline-and-indent)))) (set (make-local-variable 'font-lock-defaults) '((sgml-font-lock-keywords sgml-font-lock-keywords-1 sgml-font-lock-keywords-2) nil t nil nil (font-lock-syntactic-keywords . sgml-font-lock-syntactic-keywords))) (set (make-local-variable 'facemenu-add-face-function) 'sgml-mode-facemenu-add-face-function) (sgml-xml-guess) (if sgml-xml-mode (setq mode-name "XML") (set (make-local-variable 'skeleton-transformation) sgml-transformation)) ;; This will allow existing comments within declarations to be ;; recognized. (set (make-local-variable 'comment-start-skip) "\\(?:\\)?") ;; This definition probably is not useful in derived modes. (set (make-local-variable 'imenu-generic-expression) (concat " arg 0) (let ((oldpos (point)) (blinkpos) (level 0)) (save-excursion (save-restriction (if sgml-slash-distance (narrow-to-region (max (point-min) (- (point) sgml-slash-distance)) oldpos)) (if (and (re-search-backward sgml-start-tag-regex (point-min) t) (eq (match-end 0) (1- oldpos))) () (goto-char (1- oldpos)) (while (and (not blinkpos) (search-backward "/" (point-min) t)) (let ((tagend (save-excursion (if (re-search-backward sgml-start-tag-regex (point-min) t) (match-end 0) nil)))) (if (eq tagend (point)) (if (eq level 0) (setq blinkpos (point)) (setq level (1- level))) (setq level (1+ level))))))) (when blinkpos (goto-char blinkpos) (if (pos-visible-in-window-p) (sit-for 1) (message "Matches %s" (buffer-substring (line-beginning-position) (1+ blinkpos))))))))) ;; Why doesn't this use the iso-cvt table or, preferably, generate the ;; inverse of the extensive table in the SGML Quail input method? -- fx ;; I guess that's moot since it only works with Latin-1 anyhow. (defun sgml-name-char (&optional char) "Insert a symbolic character name according to `sgml-char-names'. Non-ASCII chars may be inserted either with the meta key, as in M-SPC for no-break space or M-- for a soft hyphen; or via an input method or encoded keyboard operation." (interactive "*") (insert ?&) (or char (setq char (read-quoted-char "Enter char or octal number"))) (delete-backward-char 1) (insert char) (undo-boundary) (delete-backward-char 1) (cond ((< char 256) (insert ?& (or (aref sgml-char-names char) (format "#%d" char)) ?\;)) ((aref sgml-char-names-table char) (insert ?& (aref sgml-char-names-table char) ?\;)) ((let ((c (encode-char char 'ucs))) (when c (insert (format "&#%d;" c)) t))) (t ; should be an error? -- fx (insert char)))) (defun sgml-name-self () "Insert a symbolic character name according to `sgml-char-names'." (interactive "*") (sgml-name-char last-command-char)) (defun sgml-maybe-name-self () "Insert a symbolic character name according to `sgml-char-names'." (interactive "*") (if sgml-name-8bit-mode (let ((mc last-command-char)) (if (< mc 256) (setq mc (unibyte-char-to-multibyte mc))) (or mc (setq mc last-command-char)) (sgml-name-char mc)) (self-insert-command 1))) (defun sgml-name-8bit-mode () "Toggle whether to insert named entities instead of non-ASCII characters. This only works for Latin-1 input." (interactive) (setq sgml-name-8bit-mode (not sgml-name-8bit-mode)) (message "sgml name entity mode is now %s" (if sgml-name-8bit-mode "ON" "OFF"))) ;; When an element of a skeleton is a string "str", it is passed ;; through skeleton-transformation and inserted. If "str" is to be ;; inserted literally, one should obtain it as the return value of a ;; function, e.g. (identity "str"). (define-skeleton sgml-tag "Prompt for a tag and insert it, optionally with attributes. Completion and configuration are done according to `sgml-tag-alist'. If you like tags and attributes in uppercase do \\[set-variable] skeleton-transformation RET upcase RET, or put this in your `.emacs': (setq sgml-transformation 'upcase)" (funcall skeleton-transformation (completing-read "Tag: " sgml-tag-alist)) ?< str | (("") -1 '(undo-boundary) (identity "<")) | ; see comment above `(("") '(setq v2 (sgml-attributes ,str t)) ?> (cond ((string= "![" ,str) (backward-char) '(("") " [ " _ " ]]")) ((and (eq v2 t) sgml-xml-mode (member ,str sgml-empty-tags)) '(("") -1 "/>")) ((or (and (eq v2 t) (not sgml-xml-mode)) (string-match "^[/!?]" ,str)) nil) ((symbolp v2) ;; Make sure we don't fall into an infinite loop. ;; For xhtml's `tr' tag, we should maybe use \n instead. (if (eq v2 t) (setq v2 nil)) ;; We use `identity' to prevent skeleton from passing ;; `str' through skeleton-transformation a second time. '(("") v2 _ v2 ")) ((eq (car v2) t) (cons '("") (cdr v2))) (t (append '(("") (car v2)) (cdr v2) '(resume: (car v2) _ ")))))) (autoload 'skeleton-read "skeleton") (defun sgml-attributes (tag &optional quiet) "When at top level of a tag, interactively insert attributes. Completion and configuration of TAG are done according to `sgml-tag-alist'. If QUIET, do not print a message when there are no attributes for TAG." (interactive (list (save-excursion (sgml-beginning-of-tag t)))) (or (stringp tag) (error "Wrong context for adding attribute")) (if tag (let ((completion-ignore-case t) (alist (cdr (assoc (downcase tag) sgml-tag-alist))) car attribute i) (if (or (symbolp (car alist)) (symbolp (car (car alist)))) (setq car (car alist) alist (cdr alist))) (or quiet (message "No attributes configured.")) (if (stringp (car alist)) (progn (insert (if (eq (preceding-char) ? ) "" ? ) (funcall skeleton-transformation (car alist))) (sgml-value alist)) (setq i (length alist)) (while (> i 0) (insert ? ) (insert (funcall skeleton-transformation (setq attribute (skeleton-read '(completing-read "Attribute: " alist))))) (if (string= "" attribute) (setq i 0) (sgml-value (assoc (downcase attribute) alist)) (setq i (1- i)))) (if (eq (preceding-char) ? ) (delete-backward-char 1))) car))) (defun sgml-auto-attributes (arg) "Self insert the character typed; at top level of tag, prompt for attributes. With prefix argument, only self insert." (interactive "*P") (let ((point (point)) tag) (if (or arg (not sgml-tag-alist) ; no message when nothing configured (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t)))) (eq (aref tag 0) ?/)) (self-insert-command (prefix-numeric-value arg)) (sgml-attributes tag) (setq last-command-char ? ) (or (> (point) point) (self-insert-command 1))))) (defun sgml-tag-help (&optional tag) "Display description of tag TAG. If TAG is omitted, use the tag at point." (interactive) (or tag (save-excursion (if (eq (following-char) ?<) (forward-char)) (setq tag (sgml-beginning-of-tag)))) (or (stringp tag) (error "No tag selected")) (setq tag (downcase tag)) (message "%s" (or (cdr (assoc (downcase tag) sgml-tag-help)) (and (eq (aref tag 0) ?/) (cdr (assoc (downcase (substring tag 1)) sgml-tag-help))) "No description available"))) (defun sgml-maybe-end-tag (&optional arg) "Name self unless in position to end a tag or a prefix ARG is given." (interactive "P") (if (or arg (eq (car (sgml-lexical-context)) 'tag)) (self-insert-command (prefix-numeric-value arg)) (sgml-name-self))) (defun sgml-skip-tag-backward (arg) "Skip to beginning of tag or matching opening tag if present. With prefix argument ARG, repeat this ARG times." (interactive "p") (while (>= arg 1) (search-backward "<" nil t) (if (looking-at "]+\\)") ;; end tag, skip any nested pairs (let ((case-fold-search t) (re (concat "= arg 1) (skip-chars-forward "^<>") (if (eq (following-char) ?>) (up-list -1)) (if (looking-at "<\\([^/ \n\t>]+\\)") ;; start tag, skip any nested same pairs _and_ closing tag (let ((case-fold-search t) (re (concat "= arg 1) (save-excursion (let* (close open) (if (looking-at "[ \t\n]*<") ;; just before tag (if (eq (char-after (match-end 0)) ?/) ;; closing tag (progn (setq close (point)) (goto-char (match-end 0)))) ;; on tag? (or (save-excursion (setq close (sgml-beginning-of-tag) close (and (stringp close) (eq (aref close 0) ?/) (point)))) ;; not on closing tag (let ((point (point))) (sgml-skip-tag-backward 1) (if (or (not (eq (following-char) ?<)) (save-excursion (forward-list 1) (<= (point) point))) (error "Not on or before tag"))))) (if close (progn (sgml-skip-tag-backward 1) (setq open (point)) (goto-char close) (kill-sexp 1)) (setq open (point)) (sgml-skip-tag-forward 1) (backward-list) (forward-char) (if (eq (aref (sgml-beginning-of-tag) 0) ?/) (kill-sexp 1))) (goto-char open) (kill-sexp 1))) (setq arg (1- arg)))) ;; Put read-only last to enable setting this even when read-only enabled. (or (get 'sgml-tag 'invisible) (setplist 'sgml-tag (append '(invisible t intangible t point-entered sgml-point-entered rear-nonsticky t read-only t) (symbol-plist 'sgml-tag)))) (defun sgml-tags-invisible (arg) "Toggle visibility of existing tags." (interactive "P") (let ((modified (buffer-modified-p)) (inhibit-read-only t) (inhibit-modification-hooks t) ;; Avoid spurious the `file-locked' checks. (buffer-file-name nil) ;; This is needed in case font lock gets called, ;; since it moves point and might call sgml-point-entered. ;; How could it get called? -stef (inhibit-point-motion-hooks t) string) (unwind-protect (save-excursion (goto-char (point-min)) (if (set (make-local-variable 'sgml-tags-invisible) (if arg (>= (prefix-numeric-value arg) 0) (not sgml-tags-invisible))) (while (re-search-forward sgml-tag-name-re nil t) (setq string (cdr (assq (intern-soft (downcase (match-string 1))) sgml-display-text))) (goto-char (match-beginning 0)) (and (stringp string) (not (overlays-at (point))) (let ((ol (make-overlay (point) (match-beginning 1)))) (overlay-put ol 'before-string string) (overlay-put ol 'sgml-tag t))) (put-text-property (point) (progn (forward-list) (point)) 'category 'sgml-tag)) (let ((pos (point-min))) (while (< (setq pos (next-overlay-change pos)) (point-max)) (dolist (ol (overlays-at pos)) (if (overlay-get ol 'sgml-tag) (delete-overlay ol))))) (remove-text-properties (point-min) (point-max) '(category nil)))) (restore-buffer-modified-p modified)) (run-hooks 'sgml-tags-invisible-hook) (message ""))) (defun sgml-point-entered (x y) ;; Show preceding or following hidden tag, depending of cursor direction. (let ((inhibit-point-motion-hooks t)) (save-excursion (message "Invisible tag: %s" ;; Strip properties, otherwise, the text is invisible. (buffer-substring-no-properties (point) (if (or (and (> x y) (not (eq (following-char) ?<))) (and (< x y) (eq (preceding-char) ?>))) (backward-list) (forward-list))))))) (autoload 'compile-internal "compile") (defun sgml-validate (command) "Validate an SGML document. Runs COMMAND, a shell command, in a separate process asynchronously with output going to the buffer `*compilation*'. You can then use the command \\[next-error] to find the next error message and move to the line in the SGML document that caused it." (interactive (list (read-string "Validate command: " (or sgml-saved-validate-command (concat sgml-validate-command " " (let ((name (buffer-file-name))) (and name (file-name-nondirectory name)))))))) (setq sgml-saved-validate-command command) (save-some-buffers (not compilation-ask-about-save) nil) (compile-internal command "No more errors")) (defun sgml-lexical-context (&optional limit) "Return the lexical context at point as (TYPE . START). START is the location of the start of the lexical element. TYPE is one of `string', `comment', `tag', or `text'. If non-nil LIMIT is a nearby position before point outside of any tag." ;; As usual, it's difficult to get a reliable answer without parsing the ;; whole buffer. We'll assume that a tag at indentation is outside of ;; any string or tag or comment or ... (save-excursion (let ((pos (point)) (state nil) textstart) (if limit (goto-char limit) ;; Hopefully this regexp will match something that's not inside ;; a tag and also hopefully the match is nearby. (re-search-backward "^[ \t]*<[_:[:alpha:]/%!?#]" nil 'move)) (setq textstart (point)) (with-syntax-table sgml-tag-syntax-table (while (< (point) pos) ;; When entering this loop we're inside text. (setq textstart (point)) (skip-chars-forward "^<" pos) ;; We skipped text and reached a tag. Parse it. ;; FIXME: Handle net-enabling start-tags and . (setq state (parse-partial-sexp (point) pos 0))) (cond ((nth 3 state) (cons 'string (nth 8 state))) ((nth 4 state) (cons 'comment (nth 8 state))) ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state))) (t (cons 'text textstart))))))) (defun sgml-beginning-of-tag (&optional top-level) "Skip to beginning of tag and return its name. If this can't be done, return nil." (let ((context (sgml-lexical-context))) (if (eq (car context) 'tag) (progn (goto-char (cdr context)) (when (looking-at sgml-tag-name-re) (match-string-no-properties 1))) (if top-level nil (when (not (eq (car context) 'text)) (goto-char (cdr context)) (sgml-beginning-of-tag t)))))) (defun sgml-value (alist) "Interactively insert value taken from attribute-rule ALIST. See `sgml-tag-alist' for info about attribute rules." (setq alist (cdr alist)) (if (stringp (car alist)) (insert "=\"" (car alist) ?\") (if (and (eq (car alist) t) (not sgml-xml-mode)) (when (cdr alist) (insert "=\"") (setq alist (skeleton-read '(completing-read "Value: " (cdr alist)))) (if (string< "" alist) (insert alist ?\") (delete-backward-char 2))) (insert "=\"") (when alist (insert (skeleton-read '(completing-read "Value: " alist)))) (insert ?\")))) (defun sgml-quote (start end &optional unquotep) "Quote SGML text in region. With prefix argument, unquote the region." (interactive "r\np") (if (< start end) (goto-char start) (goto-char end) (setq end start)) (if unquotep (while (re-search-forward "&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)[;\n]" end t) (replace-match (if (match-end 3) ">" (if (match-end 2) "<" "&")))) (while (re-search-forward "[&<>]" end t) (replace-match (cdr (assq (char-before) '((?& . "&") (?< . "<") (?> . ">")))))))) (defsubst sgml-at-indentation-p () "Return true if point is at the first non-whitespace character on the line." (save-excursion (skip-chars-backward " \t") (bolp))) ;; Parsing (defstruct (sgml-tag (:constructor sgml-make-tag (type start end name))) type start end name) (defsubst sgml-parse-tag-name () "Skip past a tag-name, and return the name." (buffer-substring-no-properties (point) (progn (skip-syntax-forward "w_") (point)))) (defsubst sgml-looking-back-at (s) (let ((limit (max (- (point) (length s)) (point-min)))) (equal s (buffer-substring-no-properties limit (point))))) (defun sgml-parse-tag-backward () "Parse an SGML tag backward, and return information about the tag. Assume that parsing starts from within a textual context. Leave point at the beginning of the tag." (let (tag-type tag-start tag-end name) (search-backward ">") (setq tag-end (1+ (point))) (cond ((sgml-looking-back-at "--") ; comment (setq tag-type 'comment tag-start (search-backward "")) (cdata (insert "]]>")) (pi (insert " ?>")) (jsp (insert " %>")) (tag (insert " />")) (text (let ((context (save-excursion (sgml-get-context)))) (if context (progn (insert "") (indent-according-to-mode))))) (otherwise (error "Nothing to close")))) (defun sgml-empty-tag-p (tag-name) "Return non-nil if TAG-NAME is an implicitly empty tag." (and (not sgml-xml-mode) (member-ignore-case tag-name sgml-empty-tags))) (defun sgml-unclosed-tag-p (tag-name) "Return non-nil if TAG-NAME is a tag for which an end-tag is optional." (and (not sgml-xml-mode) (member-ignore-case tag-name sgml-unclosed-tags))) (defun sgml-calculate-indent () "Calculate the column to which this line should be indented." (let ((lcon (sgml-lexical-context))) ;; Indent comment-start markers inside