Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 2 contributors
Commits on May 17, 2011
mike spindel Merges support for nxml-mode and fixes several edge cases in sgml-mode.
Thanks to amitp@cs.stanford.edu for the nXML patch.
4240a9d
Commits on May 18, 2011
@amitp Merge upstream changes. da0d522
Showing with 63 additions and 26 deletions.
  1. +63 −26 hl-tags-mode.el
View
89 hl-tags-mode.el
@@ -18,52 +18,86 @@
;;; Commentary:
-;; hl-tags-mode is a minor mode for NXML editing that highlights the
-;; current start and end tag.
+;; hl-tags-mode is a minor mode for SGML and XML editing that
+;; highlights the current start and end tag.
;;
;; To use hl-tags-mode, add the following to your .emacs:
;;
;; (require 'hl-tags-mode)
+;; (add-hook 'sgml-mode-hook (lambda () (hl-tags-mode 1)))
;; (add-hook 'nxml-mode-hook (lambda () (hl-tags-mode 1)))
-
;;; Code:
+(eval-when-compile (require 'cl))
+
+
(defvar hl-tags-start-overlay nil)
(make-variable-buffer-local 'hl-tags-start-overlay)
(defvar hl-tags-end-overlay nil)
(make-variable-buffer-local 'hl-tags-end-overlay)
-(defun hl-tags-context ()
- "Return ((start . end) . (start . end)) points for start/end tags, or nil"
+(defun hl-tags-sgml-get-context ()
+ (save-excursion (car (last (sgml-get-context)))))
+
+(defun hl-tags-sgml-pair (ctx)
+ (if ctx (cons (sgml-tag-start ctx) (sgml-tag-end ctx))
+ '(1 . 1)))
+
+(defun hl-tags-context-sgml-mode ()
(save-excursion
- (when (= (point) (nxml-token-before))
- (nxml-token-after))
- (when (memq xmltok-type '(start-tag end-tag))
- (let (start1 end1 start2 end2)
- ;; Note: this is hackery; there's probably a better way to do
- ;; this. We want to know where the current element's start
- ;; and end tags are.
- (unless (looking-at ">") (forward-char))
- (nxml-up-element)
- (setq end2 (point))
- (nxml-backward-single-balanced-item)
- (setq start1 (point))
- (nxml-down-element)
- (setq end1 (point))
- (nxml-up-element)
- (nxml-backward-down-element)
- (setq start2 (point))
- (cons (cons start1 end1) (cons start2 end2))))))
+ (when (looking-at "<") (forward-char 1))
+ (let* ((ctx (hl-tags-sgml-get-context))
+ (boundaries
+ (and ctx (case (sgml-tag-type ctx)
+ ('empty (cons ctx nil))
+ ('close
+ (goto-char (sgml-tag-start ctx))
+ (cons (hl-tags-sgml-get-context) ctx))
+ ('open
+ (goto-char (sgml-tag-start ctx))
+ (sgml-skip-tag-forward 1)
+ (backward-char 1)
+ (cons ctx (hl-tags-sgml-get-context)))))))
+ (when boundaries
+ (cons (hl-tags-sgml-pair (car boundaries))
+ (hl-tags-sgml-pair (cdr boundaries)))))))
+
+(defun hl-tags-context-nxml-mode ()
+ (condition-case nil
+ (save-excursion
+ (let (start1 end1 start2 end2)
+ (when (looking-at "<") (forward-char))
+ (nxml-up-element 1)
+ (setq end2 (point))
+
+ (nxml-backward-single-balanced-item)
+ (setq start2 (point))
+
+ (nxml-up-element -1)
+ (setq end1 (point))
+
+ (nxml-forward-single-balanced-item)
+ (setq start1 (point))
+
+ (cons (cons start1 end1) (cons start2 end2))))
+ (error nil)))
+
+(defun hl-tags-context ()
+ "Return a pair ((start . end) . (start . end)) containing the
+boundaries of the current start and end tag , or nil."
+ (if (eq major-mode 'nxml-mode)
+ (hl-tags-context-nxml-mode)
+ (hl-tags-context-sgml-mode)))
(defun hl-tags-update ()
(let ((ctx (hl-tags-context)))
(if (null ctx)
(hl-tags-hide)
(hl-tags-show)
- (move-overlay hl-tags-end-overlay (caar ctx) (cdar ctx))
- (move-overlay hl-tags-start-overlay (cadr ctx) (cddr ctx)))))
+ (move-overlay hl-tags-start-overlay (caar ctx) (cdar ctx))
+ (move-overlay hl-tags-end-overlay (cadr ctx) (cddr ctx)))))
(defun hl-tags-show ()
(unless hl-tags-start-overlay
@@ -81,8 +115,11 @@
"Toggle hl-tags-mode."
nil "" nil
(if hl-tags-mode
- (add-hook 'post-command-hook 'hl-tags-update nil t)
+ (progn
+ (add-hook 'post-command-hook 'hl-tags-update nil t)
+ (add-hook 'change-major-mode-hook 'hl-tags-hide nil t))
(remove-hook 'post-command-hook 'hl-tags-update t)
+ (remove-hook 'change-major-mode-hook 'hl-tags-hide t)
(hl-tags-hide)))

No commit comments for this range

Something went wrong with that request. Please try again.