Find file
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (85 sloc) 3.43 KB
;;; html-mode-expansions.el --- HTML-specific expansions for expand-region
;; Copyright (C) 2011 Magnar Sveen
;; Author: Magnar Sveen <>
;; Keywords: marking region
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <>.
;;; Commentary:
;; Extra expansions for HTML that I've found useful so far:
;; er/mark-html-attribute
;; er/mark-inner-tag
;; er/mark-outer-tag
;; Feel free to contribute any other expansions for HTML at
;;; Code:
(require 'expand-region-core)
(require 'sgml-mode)
(defun er/mark-html-attribute ()
"Mark html-attribute presumes that point is at the assignment part of attr=\"value\".
If point is inside the value-string, the quotes will be marked
first anyway. Does not support html-attributes with spaces
around the equal sign or unquotes attributes atm."
(when (or (looking-at "\\(\\s_\\|\\sw\\)*=")
(er/looking-back-exact "="))
(search-backward " ")
(forward-char 1)
(set-mark (point))
(search-forward "=")
(forward-sexp 1)
(defun er--looking-at-marked-tag ()
"Is point looking at a tag that is entirely marked?"
(and (looking-at "<")
(>= (mark)
(sgml-skip-tag-forward 1)
(defun er--inside-tag-p ()
"Is point inside a tag?"
(not (null (sgml-get-context)))))
(defun er/mark-outer-tag ()
"Mark from opening to closing tag, including the tags."
(when (and (er--inside-tag-p)
(or (not (looking-at "<"))
(goto-char (aref (car (last (sgml-get-context))) 2)))
(when (looking-at "<")
(set-mark (point))
(sgml-skip-tag-forward 1)
(defun er/mark-inner-tag ()
"Mark the contents of an open tag, not including the tags."
(goto-char (aref (car (last (sgml-get-context))) 3))
(set-mark (point))
(backward-char 1)
(sgml-skip-tag-forward 1)
(search-backward "</")
(defun er/add-html-mode-expansions ()
"Adds HTML-specific expansions for buffers in html-mode"
(set (make-local-variable 'er/try-expand-list) (append
(er/enable-mode-expansions 'html-mode 'er/add-html-mode-expansions)
(er/enable-mode-expansions 'rhtml-mode 'er/add-html-mode-expansions)
(er/enable-mode-expansions 'nxhtml-mode 'er/add-html-mode-expansions)
(er/enable-mode-expansions 'web-mode 'er/add-html-mode-expansions)
(provide 'html-mode-expansions)
;; html-mode-expansions.el ends here