dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

html-mode-expansions.el (3573B)


      1 ;;; html-mode-expansions.el --- HTML-specific expansions for expand-region  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2011-2023  Free Software Foundation, Inc
      4 
      5 ;; Author: Magnar Sveen <magnars@gmail.com>
      6 ;; Keywords: marking region
      7 
      8 ;; This program is free software; you can redistribute it and/or modify
      9 ;; it under the terms of the GNU General Public License as published by
     10 ;; the Free Software Foundation, either version 3 of the License, or
     11 ;; (at your option) any later version.
     12 
     13 ;; This program is distributed in the hope that it will be useful,
     14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 ;; GNU General Public License for more details.
     17 
     18 ;; You should have received a copy of the GNU General Public License
     19 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
     20 
     21 ;;; Commentary:
     22 
     23 ;; Extra expansions for HTML that I've found useful so far:
     24 ;;
     25 ;;     er/mark-html-attribute
     26 ;;     er/mark-inner-tag
     27 ;;     er/mark-outer-tag
     28 ;;
     29 ;; Feel free to contribute any other expansions for HTML at
     30 ;;
     31 ;;     https://github.com/magnars/expand-region.el
     32 
     33 ;;; Code:
     34 
     35 (require 'expand-region-core)
     36 (require 'sgml-mode)
     37 
     38 (defun er/mark-html-attribute ()
     39   "Mark html-attribute.
     40 Presumes that point is at the assignment part of attr=\"value\".
     41 If point is inside the value-string, the quotes will be marked
     42 first anyway.  Does not support html-attributes with spaces
     43 around the equal sign or unquoted attributes atm."
     44   (interactive)
     45   (when (or (looking-at "\\(\\s_\\|\\sw\\)*=")
     46             (er/looking-back-exact "="))
     47     (search-backward " ")
     48     (forward-char 1)
     49     (set-mark (point))
     50     (search-forward "=")
     51     (forward-sexp 1)
     52     (exchange-point-and-mark)))
     53 
     54 (defun er--looking-at-marked-tag ()
     55   "Is point looking at a tag that is entirely marked?"
     56   (and (looking-at "<")
     57        (>= (mark)
     58            (save-excursion
     59              (sgml-skip-tag-forward 1)
     60              (point)))))
     61 
     62 (defun er--inside-tag-p ()
     63   "Is point inside a tag?"
     64   (save-excursion
     65     (not (null (sgml-get-context)))))
     66 
     67 (defun er/mark-outer-tag ()
     68   "Mark from opening to closing tag, including the tags."
     69   (interactive)
     70   (when (and (er--inside-tag-p)
     71              (or (not (looking-at "<"))
     72                  (er--looking-at-marked-tag)))
     73     (goto-char (aref (car (last (sgml-get-context))) 2)))
     74   (when (looking-at "<")
     75     (set-mark (point))
     76     (sgml-skip-tag-forward 1)
     77     (exchange-point-and-mark)))
     78 
     79 (defun er/mark-inner-tag ()
     80   "Mark the contents of an open tag, not including the tags."
     81   (interactive)
     82   (goto-char (aref (car (last (sgml-get-context))) 3))
     83   (set-mark (point))
     84   (backward-char 1)
     85   (sgml-skip-tag-forward 1)
     86   (search-backward "</")
     87   (exchange-point-and-mark))
     88 
     89 (defun er/add-html-mode-expansions ()
     90   "Adds HTML-specific expansions for buffers in html-mode"
     91   (set (make-local-variable 'er/try-expand-list) (append
     92                                                   er/try-expand-list
     93                                                   '(er/mark-html-attribute
     94                                                     er/mark-inner-tag
     95                                                     er/mark-outer-tag))))
     96 
     97 (er/enable-mode-expansions 'html-mode #'er/add-html-mode-expansions)
     98 (er/enable-mode-expansions 'rhtml-mode #'er/add-html-mode-expansions)
     99 (er/enable-mode-expansions 'nxhtml-mode #'er/add-html-mode-expansions)
    100 (er/enable-mode-expansions 'web-mode #'er/add-html-mode-expansions)
    101 
    102 (provide 'html-mode-expansions)
    103 
    104 ;; html-mode-expansions.el ends here