html-mode-expansions.el (3539B)
1 ;;; html-mode-expansions.el --- HTML-specific expansions for expand-region 2 3 ;; Copyright (C) 2011-2020 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 presumes that point is at the assignment part of attr=\"value\". 40 If point is inside the value-string, the quotes will be marked 41 first anyway. Does not support html-attributes with spaces 42 around the equal sign or unquotes attributes atm." 43 (interactive) 44 (when (or (looking-at "\\(\\s_\\|\\sw\\)*=") 45 (er/looking-back-exact "=")) 46 (search-backward " ") 47 (forward-char 1) 48 (set-mark (point)) 49 (search-forward "=") 50 (forward-sexp 1) 51 (exchange-point-and-mark))) 52 53 (defun er--looking-at-marked-tag () 54 "Is point looking at a tag that is entirely marked?" 55 (and (looking-at "<") 56 (>= (mark) 57 (save-excursion 58 (sgml-skip-tag-forward 1) 59 (point))))) 60 61 (defun er--inside-tag-p () 62 "Is point inside a tag?" 63 (save-excursion 64 (not (null (sgml-get-context))))) 65 66 (defun er/mark-outer-tag () 67 "Mark from opening to closing tag, including the tags." 68 (interactive) 69 (when (and (er--inside-tag-p) 70 (or (not (looking-at "<")) 71 (er--looking-at-marked-tag))) 72 (goto-char (aref (car (last (sgml-get-context))) 2))) 73 (when (looking-at "<") 74 (set-mark (point)) 75 (sgml-skip-tag-forward 1) 76 (exchange-point-and-mark))) 77 78 (defun er/mark-inner-tag () 79 "Mark the contents of an open tag, not including the tags." 80 (interactive) 81 (goto-char (aref (car (last (sgml-get-context))) 3)) 82 (set-mark (point)) 83 (backward-char 1) 84 (sgml-skip-tag-forward 1) 85 (search-backward "</") 86 (exchange-point-and-mark)) 87 88 (defun er/add-html-mode-expansions () 89 "Adds HTML-specific expansions for buffers in html-mode" 90 (set (make-local-variable 'er/try-expand-list) (append 91 er/try-expand-list 92 '(er/mark-html-attribute 93 er/mark-inner-tag 94 er/mark-outer-tag)))) 95 96 (er/enable-mode-expansions 'html-mode 'er/add-html-mode-expansions) 97 (er/enable-mode-expansions 'rhtml-mode 'er/add-html-mode-expansions) 98 (er/enable-mode-expansions 'nxhtml-mode 'er/add-html-mode-expansions) 99 (er/enable-mode-expansions 'web-mode 'er/add-html-mode-expansions) 100 101 (provide 'html-mode-expansions) 102 103 ;; html-mode-expansions.el ends here