Function: hydra--format

hydra--format is a byte-compiled function defined in hydra.el.

Signature

(hydra--format NAME BODY DOCSTRING HEADS)

Documentation

Generate a format statement from STR.

"%`..." expressions are extracted into "%S".
_NAME, BODY, DOCSTRING and HEADS are parameters of defhydra.
The expressions can be auto-expanded according to NAME.

Source Code

;; Defined in ~/.emacs.d/elpa/hydra-20250316.1254/hydra.el
(defun hydra--format (_name body docstring heads)
  "Generate a `format' statement from STR.
\"%`...\" expressions are extracted into \"%S\".
_NAME, BODY, DOCSTRING and HEADS are parameters of `defhydra'.
The expressions can be auto-expanded according to NAME."
  (unless (memq 'elisp--witness--lisp (mapcar #'cadr heads))
    (setq docstring (hydra--strip-align-markers docstring))
    (setq docstring (replace-regexp-in-string "___" "_β_" docstring))
    (let ((rest (if (eq (plist-get (cddr body) :hint) 'none)
                    ""
                  (hydra--hint body heads)))
          (start 0)
          (inner-regex (format "\\(%s\\)\\(%s\\)" hydra-width-spec-regex hydra-key-regex))
          varlist
          offset)
      (while (setq start
                   (string-match
                    (format
                     "\\(?:%%\\( ?-?[0-9]*s?\\)\\(`[a-z-A-Z/0-9]+\\|(\\)\\)\\|\\(?:_%s_\\)\\|\\(?:[?]%s[?]\\)\\|__"
                     inner-regex
                     inner-regex)
                    docstring start))
        (cond ((string= "__" (match-string 0 docstring))
               (setq docstring (replace-match "_" nil t docstring))
               (setq start (1- (match-end 0))))
              ((eq ?? (aref (match-string 0 docstring) 0))
               (let* ((key (match-string 6 docstring))
                      (head (assoc key heads)))
                 (if head
                     (progn
                       (push (nth 2 head) varlist)
                       (setq docstring
                             (replace-match
                              (or
                               hydra-doc-format-spec
                               (concat "%" (match-string 3 docstring) "s"))
                              t nil docstring)))
                   (setq start (match-end 0))
                   (warn "Unrecognized key: ?%s?" key))))
              ((eq ?_ (aref (match-string 0 docstring) 0))
               (let* ((key (match-string 4 docstring))
                      (key (if (equal key "β") "_" key))
                      normal-key
                      (head (or (assoc key heads)
                                (when (setq normal-key
                                            (cdr (assoc
                                                  key hydra-docstring-keys-translate-alist)))
                                  (assoc normal-key heads)))))
                 (if head
                     (progn
                       (push (hydra-fontify-head (if normal-key
                                                     (cons key (cdr head))
                                                   head)
                                                 body)
                             varlist)
                       (let ((replacement
                              (or
                               hydra-key-format-spec
                               (concat "%" (match-string 3 docstring) "s"))))
                         (setq docstring
                               (replace-match replacement t nil docstring))
                         (setq start (+ start (length replacement)))))
                   (setq start (match-end 0))
                   (warn "Unrecognized key: _%s_" key))))

              (t
               (let* ((varp (if (eq ?` (aref (match-string 2 docstring) 0)) 1 0))
                      (spec (match-string 1 docstring))
                      (lspec (length spec)))
                 (setq offset
                       (with-temp-buffer
                         (insert (substring docstring (+ 1 start varp
                                                         (length spec))))
                         (goto-char (point-min))
                         (push (read (current-buffer)) varlist)
                         (- (point) (point-min))))
                 (when (or (zerop lspec)
                           (/= (aref spec (1- (length spec))) ?s))
                   (setq spec (concat spec "S")))
                 (setq docstring
                       (concat
                        (substring docstring 0 start)
                        "%" spec
                        (substring docstring (+ start offset 1 lspec varp))))))))
      (hydra--format-1 docstring rest varlist))))