Function: sgml-lexical-context
sgml-lexical-context is an autoloaded and byte-compiled function
defined in sgml-mode.el.gz.
Signature
(sgml-lexical-context &optional LIMIT)
Documentation
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, cdata, pi, or text.
Optional argument LIMIT is the position to start parsing from. If nil, start from a preceding tag at indentation.
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/sgml-mode.el.gz
(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', `cdata', `pi', or `text'.
Optional argument LIMIT is the position to start parsing from.
If nil, start from a preceding tag at indentation."
(save-excursion
(let ((pos (point))
text-start state)
(if limit
(goto-char limit)
;; Skip tags backwards until we find one at indentation
(while (and (ignore-errors (sgml-parse-tag-backward))
(not (sgml-at-indentation-p)))))
(with-syntax-table sgml-tag-syntax-table
(while (< (point) pos)
;; When entering this loop we're inside text.
(setq text-start (point))
(skip-chars-forward "^<" pos)
(setq state
(cond
((= (point) pos)
;; We got to the end without seeing a tag.
nil)
((looking-at "<!\\[[A-Z]+\\[")
;; We've found a CDATA section or similar.
(let ((cdata-start (point)))
(unless (search-forward "]]>" pos 'move)
(list 0 nil nil 'cdata nil nil nil nil cdata-start))))
((looking-at comment-start-skip)
;; parse-partial-sexp doesn't handle <!-- comments -->,
;; or only if ?- is in sgml-specials, so match explicitly
(let ((start (point)))
(unless (re-search-forward comment-end-skip pos 'move)
(list 0 nil nil nil t nil nil nil start))))
((and sgml-xml-mode (looking-at "<\\?"))
;; Processing Instructions.
;; In SGML, it's basically a normal tag of the form
;; <?NAME ...> but in XML, it takes the form <? ... ?>.
(let ((pi-start (point)))
(unless (search-forward "?>" pos 'move)
(list 0 nil nil 'pi nil nil nil nil pi-start))))
(t
;; We've reached a tag. Parse it.
;; FIXME: Handle net-enabling start-tags
(parse-partial-sexp (point) pos 0))))))
(cond
((memq (nth 3 state) '(cdata pi)) (cons (nth 3 state) (nth 8 state)))
((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 text-start))))))