Function: web-mode-tag-attributes-sort

web-mode-tag-attributes-sort is an interactive and natively compiled function defined in web-mode.el.

Signature

(web-mode-tag-attributes-sort &optional POS)

Documentation

Sort the attributes inside the current html tag.

Key Bindings

web-mode-map C-c C-t a

Source Code

;; Defined in /nix/store/2qgnz1rp30nzfz6w0apx4kdm14if1bbs-emacs-packages-deps/share/emacs/site-lisp/elpa/web-mode-20260831.219/web-mode.el
(defun web-mode-tag-attributes-sort (&optional pos)
  "Sort the attributes inside the current html tag."
  (interactive)
  (unless pos (setq pos (point)))
  (save-excursion
    (let (attrs (continue t) min max tag-beg tag-end attr attr-name attr-beg attr-end indent sorter ins)
      (if (not (member (get-text-property pos 'tag-type) '(start void)))
          nil
          (setq tag-beg (web-mode-tag-beginning-position pos)
                tag-end (web-mode-tag-end-position))
          ;;        (message "%S %S" tag-beg tag-end)
          (goto-char tag-beg)
          (while continue
            (if (or (not (web-mode-attribute-next))
                    (>= (point) tag-end))
                (setq continue nil)
                ;;(message "attr=%S" (point))
                (setq attr-beg (web-mode-attribute-beginning-position)
                      attr-end (1+ (web-mode-attribute-end-position)))
                (when (null min)
                  (setq min attr-beg))
                (setq max attr-end)
                (goto-char attr-beg)
                (setq attr (buffer-substring-no-properties attr-beg attr-end))
                (if (string-match "^\\([[:alnum:]-]+\\)=" attr)
                    (setq attr-name (match-string-no-properties 1 attr))
                    (setq attr-name attr))
                (setq indent (looking-back "^[ \t]*" (point-min)))
                (setq attrs (append attrs (list (list attr-beg attr-end attr-name attr indent))))
                ) ;if
            ) ;while
          ) ;if in tag
      (when attrs
        (setq sorter (function
                      (lambda (elt1 elt2)
                       (string< (nth 2 elt1) (nth 2 elt2))
                       )))
        (setq attrs (sort attrs sorter))
        (delete-region (1- min) max)
        (setq ins "")
        (dolist (elt attrs)
          (if (and (nth 4 elt) (> (length ins) 1))
              (setq ins (concat ins "\n"))
              (setq ins (concat ins " ")))
          (setq ins (concat ins (nth 3 elt)))
          )
        (goto-char (1- min))
        (insert ins)
        (web-mode-tag-beginning)
        (setq min (line-beginning-position))
        (web-mode-tag-end)
        (setq max (line-end-position))
        (indent-region min max)
        )
      ;;(message "attrs=%S" attrs)
      )))