# HG changeset patch # User Stefan Monnier # Date 1054775663 0 # Node ID cc38ef483a770e1b5c1f7577ce8db4f2dc64d885 # Parent 3ee34a66739ec2d680b93d07dbc93f23881e25c9 (sgml-parse-tag-backward): Try and detect when we're starting from within a tag. (sgml-get-context): When called from inside a tag, do something useful. Rename the arg now that it's never used for `full' context anymore. (sgml-calculate-indent): Make `lcon' an argument. Return nil when we don't know what to do. If the initial lcon turns out to be wrong, try again. (sgml-indent-line): If sgml-calculate-indent returns nil, don't indent. diff -r 3ee34a66739e -r cc38ef483a77 lisp/textmodes/sgml-mode.el --- a/lisp/textmodes/sgml-mode.el Thu Jun 05 00:36:28 2003 +0000 +++ b/lisp/textmodes/sgml-mode.el Thu Jun 05 01:14:23 2003 +0000 @@ -368,8 +368,8 @@ (looking-at "\\s-*<\\?xml") (when (re-search-forward (eval-when-compile - (mapconcat 'identity - '("" limit 'move) + (or (re-search-backward "[<>]" limit 'move) (error "No tag found")) + (when (eq (char-after) ?<) + ;; Oops!! Looks like we were not in a textual context after all!. + ;; Let's try to recover. + (with-syntax-table sgml-tag-syntax-table + (forward-sexp) + (forward-char -1))) (setq tag-end (1+ (point))) (cond ((sgml-looking-back-at "--") ; comment @@ -1082,15 +1088,17 @@ (goto-char tag-start) (sgml-make-tag tag-type tag-start tag-end name))) -(defun sgml-get-context (&optional full) +(defun sgml-get-context (&optional until) "Determine the context of the current position. -If FULL is `empty', return even if the context is empty (i.e. +By default, parse until we find a start-tag as the first thing on a line. +If UNTIL is `empty', return even if the context is empty (i.e. we just skipped over some element and got to a beginning of line). -If FULL is non-nil, parse back to the beginning of the buffer, otherwise -parse until we find a start-tag as the first thing on a line. The context is a list of tag-info structures. The last one is the tag -immediately enclosing the current position." +immediately enclosing the current position. + +Point is assumed to be outside of any tag. If we discover that it's +not the case, the first tag returned is the one inside which we are." (let ((here (point)) (ignore nil) (context nil) @@ -1101,12 +1109,13 @@ ;; enclosing start-tags we'll have to ignore. (skip-chars-backward " \t\n") ; Make sure we're not at indentation. (while - (and (or ignore - (not (if full (eq full 'empty) context)) + (and (not (eq until 'now)) + (or ignore + (not (if until (eq until 'empty) context)) (not (sgml-at-indentation-p)) (and context (/= (point) (sgml-tag-start (car context))) - (sgml-unclosed-tag-p (sgml-tag-name (car context))))) + (sgml-unclosed-tag-p (sgml-tag-name (car context))))) (setq tag-info (ignore-errors (sgml-parse-tag-backward)))) ;; This tag may enclose things we thought were tags. If so, @@ -1117,6 +1126,10 @@ (setq context (cdr context))) (cond + ((> (sgml-tag-end tag-info) here) + ;; Oops!! Looks like we were not outside of any tag, after all. + (push tag-info context) + (setq until 'now)) ;; start-tag ((eq (sgml-tag-type tag-info) 'open) @@ -1207,99 +1220,113 @@ (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))) +(defun sgml-calculate-indent (&optional lcon) + "Calculate the column to which this line should be indented. +LCON is the lexical context, if any." + (unless lcon (setq lcon (sgml-lexical-context))) + + ;; Indent comment-start markers inside