Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2022 14:58
What would you like to do?
(defmacro ct/tab-bar-select-action-define (num)
`(defun ,(intern (format "ct/tab-bar-select-%i" num)) ()
,(format "Select tab %i by its absolute number." num)
(tab-bar-select-tab ,num)))
;; Manual calls work
(ct/tab-bar-select-action-define 1)
(ct/tab-bar-select-action-define 2)
(ct/tab-bar-select-action-define 3)
(ct/tab-bar-select-action-define 4)
(ct/tab-bar-select-action-define 5)
(ct/tab-bar-select-action-define 6)
(ct/tab-bar-select-action-define 7)
(ct/tab-bar-select-action-define 8)
(ct/tab-bar-select-action-define 9))
;; Helper to undefine the functions again
(fmakunbound 'ct/tab-bar-select-1)
(fmakunbound 'ct/tab-bar-select-2)
(fmakunbound 'ct/tab-bar-select-3)
(fmakunbound 'ct/tab-bar-select-4)
(fmakunbound 'ct/tab-bar-select-5)
(fmakunbound 'ct/tab-bar-select-6)
(fmakunbound 'ct/tab-bar-select-7)
(fmakunbound 'ct/tab-bar-select-8)
(fmakunbound 'ct/tab-bar-select-9))
(ct/tab-bar-select-action-define 1)
(let (tab-number)
(dotimes (i 9)
(setq tab-number (+ 1 i))
(message (format ">%i" tab-number))
(ct/tab-bar-select-action-define tab-number)))
;; Compiler-macro error for cl--block-wrapper: (error "Format specifier doesn’t match argument type")
;; intern: Format specifier doesn’t match argument type
Copy link

Thanks to @daviwil's feedback and documented fixed in I realize that passing arguments to macros isn't worth the trouble when you have to inline the loop anyway -- so might as well do it directly:

  ;; Bind 1-9 in the tab prefix map to switch to that tab.
  (mapcar (lambda (tab-number)
            (let ((funname (intern (format "ct/tab-bar-select-%d" tab-number)))
                  (docstring (format "Select tab %d by its absolute number." tab-number))
                  (key (kbd (format "%d" tab-number))))
              (eval-expression `(defun ,funname ()
                                  (tab-bar-select-tab ,tab-number)))
              (eval-expression `(define-key tab-prefix-map key ',funname))))
          (number-sequence 1 9))

This enabled SPC t 1--SPC t 9 to switch to the tab by number directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment