diff options
| author | Stefan Monnier | 2002-07-13 19:23:05 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2002-07-13 19:23:05 +0000 |
| commit | 7492ed8e8d7b04da2b4de972742b892e2233cd66 (patch) | |
| tree | d4cdead66c8f41818dfdd668b93881302cadd5d7 | |
| parent | 4105dd524e297345ea8cd9f0ccd01eb263165f03 (diff) | |
| download | emacs-7492ed8e8d7b04da2b4de972742b892e2233cd66.tar.gz emacs-7492ed8e8d7b04da2b4de972742b892e2233cd66.zip | |
(sgml-quote): Use narrowing. Improve the regexp used when unquoting.
(sgml-pretty-print): New function.
(sgml-get-context): Better handling of improperly nested tags.
(sgml-show-context): Don't use the FULL arg of sgml-get-context.
| -rw-r--r-- | lisp/textmodes/sgml-mode.el | 82 |
1 files changed, 64 insertions, 18 deletions
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el index 28eea74f9f3..bad9dcc4a34 100644 --- a/lisp/textmodes/sgml-mode.el +++ b/lisp/textmodes/sgml-mode.el | |||
| @@ -942,20 +942,51 @@ See `sgml-tag-alist' for info about attribute rules." | |||
| 942 | (insert ?\")))) | 942 | (insert ?\")))) |
| 943 | 943 | ||
| 944 | (defun sgml-quote (start end &optional unquotep) | 944 | (defun sgml-quote (start end &optional unquotep) |
| 945 | "Quote SGML text in region. | 945 | "Quote SGML text in region START ... END. |
| 946 | With prefix argument, unquote the region." | 946 | Only &, < and > are quoted, the rest is left untouched. |
| 947 | (interactive "r\np") | 947 | With prefix argument UNQUOTEP, unquote the region." |
| 948 | (if (< start end) | 948 | (interactive "r\nP") |
| 949 | (goto-char start) | 949 | (save-restriction |
| 950 | (goto-char end) | 950 | (narrow-to-region start end) |
| 951 | (setq end start)) | 951 | (goto-char (point-min)) |
| 952 | (if unquotep | 952 | (if unquotep |
| 953 | (while (re-search-forward "&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)[;\n]" end t) | 953 | ;; FIXME: We should unquote other named character references as well. |
| 954 | (replace-match (if (match-end 3) ">" (if (match-end 2) "<" "&")))) | 954 | (while (re-search-forward |
| 955 | (while (re-search-forward "[&<>]" end t) | 955 | "\\(&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)\\)[][<>&;\n\t \"%!'(),/=?]" |
| 956 | (replace-match (cdr (assq (char-before) '((?& . "&") | 956 | nil t) |
| 957 | (?< . "<") | 957 | (replace-match (if (match-end 4) ">" (if (match-end 3) "<" "&")) t t |
| 958 | (?> . ">")))))))) | 958 | nil (if (eq (char-before (match-end 0)) ?\;) 0 1))) |
| 959 | (while (re-search-forward "[&<>]" nil t) | ||
| 960 | (replace-match (cdr (assq (char-before) '((?& . "&") | ||
| 961 | (?< . "<") | ||
| 962 | (?> . ">")))) | ||
| 963 | t t))))) | ||
| 964 | |||
| 965 | (defun sgml-pretty-print (beg end) | ||
| 966 | "Simple-minded pretty printer for SGML. | ||
| 967 | Re-indents the code and inserts newlines between BEG and END. | ||
| 968 | You might want to turn on `auto-fill-mode' to get better results." | ||
| 969 | ;; TODO: | ||
| 970 | ;; - insert newline between some start-tag and text. | ||
| 971 | ;; - don't insert newline in front of some end-tags. | ||
| 972 | (interactive "r") | ||
| 973 | (save-excursion | ||
| 974 | (if (< beg end) | ||
| 975 | (goto-char beg) | ||
| 976 | (goto-char end) | ||
| 977 | (setq end beg) | ||
| 978 | (setq beg (point))) | ||
| 979 | ;; Don't use narrowing because it screws up auto-indent. | ||
| 980 | (setq end (copy-marker end t)) | ||
| 981 | (with-syntax-table sgml-tag-syntax-table | ||
| 982 | (while (re-search-forward "<" end t) | ||
| 983 | (goto-char (match-beginning 0)) | ||
| 984 | (unless (or ;;(looking-at "</") | ||
| 985 | (progn (skip-chars-backward " \t") (bolp))) | ||
| 986 | (reindent-then-newline-and-indent)) | ||
| 987 | (forward-sexp 1))) | ||
| 988 | ;; (indent-region beg end) | ||
| 989 | )) | ||
| 959 | 990 | ||
| 960 | 991 | ||
| 961 | ;; Parsing | 992 | ;; Parsing |
| @@ -1050,7 +1081,7 @@ immediately enclosing the current position." | |||
| 1050 | (> (sgml-tag-end tag-info) | 1081 | (> (sgml-tag-end tag-info) |
| 1051 | (sgml-tag-end (car context)))) | 1082 | (sgml-tag-end (car context)))) |
| 1052 | (setq context (cdr context))) | 1083 | (setq context (cdr context))) |
| 1053 | 1084 | ||
| 1054 | (cond | 1085 | (cond |
| 1055 | 1086 | ||
| 1056 | ;; start-tag | 1087 | ;; start-tag |
| @@ -1071,9 +1102,18 @@ immediately enclosing the current position." | |||
| 1071 | (t | 1102 | (t |
| 1072 | ;; The open and close tags don't match. | 1103 | ;; The open and close tags don't match. |
| 1073 | (if (not sgml-xml-mode) | 1104 | (if (not sgml-xml-mode) |
| 1074 | ;; Assume the open tag is simply not closed. | ||
| 1075 | (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info)) | 1105 | (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info)) |
| 1076 | (message "Unclosed tag <%s>" (sgml-tag-name tag-info))) | 1106 | (message "Unclosed tag <%s>" (sgml-tag-name tag-info)) |
| 1107 | (let ((tmp ignore)) | ||
| 1108 | ;; We could just assume that the tag is simply not closed | ||
| 1109 | ;; but it's a bad assumption when tags *are* closed but | ||
| 1110 | ;; not properly nested. | ||
| 1111 | (while (and (cdr tmp) | ||
| 1112 | (not (eq t (compare-strings | ||
| 1113 | (sgml-tag-name tag-info) nil nil | ||
| 1114 | (cadr tmp) nil nil t)))) | ||
| 1115 | (setq tmp (cdr tmp))) | ||
| 1116 | (if (cdr tmp) (setcdr tmp (cddr tmp))))) | ||
| 1077 | (message "Unmatched tags <%s> and </%s>" | 1117 | (message "Unmatched tags <%s> and </%s>" |
| 1078 | (sgml-tag-name tag-info) (pop ignore)))))) | 1118 | (sgml-tag-name tag-info) (pop ignore)))))) |
| 1079 | 1119 | ||
| @@ -1092,7 +1132,13 @@ immediately enclosing the current position." | |||
| 1092 | If FULL is non-nil, parse back to the beginning of the buffer." | 1132 | If FULL is non-nil, parse back to the beginning of the buffer." |
| 1093 | (interactive "P") | 1133 | (interactive "P") |
| 1094 | (with-output-to-temp-buffer "*XML Context*" | 1134 | (with-output-to-temp-buffer "*XML Context*" |
| 1095 | (pp (save-excursion (sgml-get-context full))))) | 1135 | (save-excursion |
| 1136 | (let ((context (sgml-get-context))) | ||
| 1137 | (when full | ||
| 1138 | (let ((more nil)) | ||
| 1139 | (while (setq more (sgml-get-context)) | ||
| 1140 | (setq context (nconc more context))))) | ||
| 1141 | (pp context))))) | ||
| 1096 | 1142 | ||
| 1097 | 1143 | ||
| 1098 | ;; Editing shortcuts | 1144 | ;; Editing shortcuts |