3 ms·
Not OP, but I recently installed the Emacs flavor of the widely used Emmet HTML generation tool, and I found myself wanting to customize some aspects of its beh
by throwanem 11y ago
Not OP, but I recently installed the Emacs flavor of the widely used Emmet HTML generation tool, and I found myself wanting to customize some aspects of its behavior so that they better suited our in-house style.
I could've edited the Emmet source code in place, but to do so would've meant losing my customizations next time I upgraded the package. I could have forked it, but that would mean having to manually merge changes into the fork every time a new release comes out.
Instead, after a couple hours of reading the Emmet source and fiddling around in an Emacs Lisp buffer, I ended up with this:
(defcustom emmet-verticalize-html-attributes nil
"Whether to verticalize attributes in HTML tags.
\"Verticalize\" here means to separate each pair of attributes by
newline and enough indentation to line up the first character of
attribute names. For example, the unverticalized production
<div id=\"foo\" class=\"bar\"></div>
when verticalized becomes
<div id=\"foo\"
class=\"bar\"></div>"
:type 'boolean
:group 'emmet)
(defcustom emmet-verticalize-minimum-attributes 3
"The minimum number of attributes required for
verticalization. If a tag has fewer than this many
attributes. they will not be verticalized, even when
verticalization is enabled.
Values less than 2 have no effect, since verticalization only has
an effect on tags with at least two attributes."
:type 'integer
:group 'emmet)
(defun emmet-verticalize-html-tag (orig &rest args)
(let ((html (apply orig args))
(attribute-count 0))
(if (null emmet-verticalize-html-attributes)
html
(save-match-data
(with-temp-buffer
(insert html)
(html-mode)
(goto-char (point-min))
(while (re-search-forward (pcre-to-elisp "^\\<[^\\/]\\S+") nil t)
(while (and (not (= (point) (point-max)))
(not (looking-at (pcre-to-elisp "\\/?>")))
(re-search-forward (pcre-to-elisp "\\w[\\w\\d_-]+\\=\\\".*?\\\"" ) nil t))
(setq attribute-count (1+ attribute-count))
(and (not (looking-at (pcre-to-elisp "\\/?>")))
(newline-and-indent))))
(indent-region (point-min) (point-max))
(if (>= attribute-count emmet-verticalize-minimum-attributes)
(buffer-substring-no-properties (point-min) (point-max))
html))))))
(advice-add 'emmet-make-html-tag :around #'emmet-verticalize-html-tag)
(defcustom emmet-indent-html t
"Whether or not to indent generated HTML according to the same rules
used by `html-mode'.
When non-nil, Emmet-generated HTML will be indented via
`html-mode', with a newline after every closing angle bracket.
When nil, Emmet-generated HTML won't be post-processed in this
way. (The version of Emmet I'm using, 1.0.8, applies no
indentation whatsoever.)"
:type 'boolean
:group 'emmet)
(defun emmet-indent-html-snippet (html)
(if (not emmet-indent-html)
html
(save-match-data
(with-temp-buffer
(insert html)
(goto-char (point-min))
(while (and (re-search-forward (pcre-to-elisp "\\>") nil t)
(not (eobp)))
(newline))
(goto-char (point-min))
(while (search-forward "\n\n" nil t)
(replace-match "\n" nil t))
(html-mode)
(indent-region (point-min) (point-max))
(buffer-substring-no-properties (point-min) (point-max))))))
(advice-add 'emmet-make-html-tag :filter-return #'emmet-indent-html-snippet)
...which not only implements the behavior I desire, and does so without modifying the original source or breaking my ability to upgrade Emmet, but also adds customization options to those provided by stock Emmet, so that I can adjust the way my Emmet extensions behave without having to touch the code. (Not that touching the code is any great hardship, but it's nice to be able to adjust the way these extensions behave in the same interface I use to make other minor adjustments to the way Emmet works.)
Shortly after I copied that code into the Emacs buffer I'm using (via Firefox's "It's All Text!" extension) to edit this comment, but before posting, I inadvertently killed Emacs's message buffer, which contains a running log of everything displayed in the editor's echo area. I wasn't prompted for confirmation before killing that buffer, but I felt I should've been. So I opened an Emacs Lisp buffer and wrote this:
(defun defend-message-buffer (kill-fun &rest args)
(let* ((buf (car args))
(buf-name (if (stringp buf)
buf
(substring-no-properties (buffer-name buf)))))
(if (string= buf-name "*Messages*")
(and (yes-or-no-p "Really kill the message buffer? ")
(funcall kill-fun buf))
(funcall kill-fun buf))))
(advice-add 'kill-buffer :around #'defend-message-buffer)
And now I am.