Skip to content

Instantly share code, notes, and snippets.

@DivineDominion
Created January 31, 2022 14:58
Show Gist options
  • Save DivineDominion/4488ba95e0cf55f967006e50aca979cc to your computer and use it in GitHub Desktop.
Save DivineDominion/4488ba95e0cf55f967006e50aca979cc to your computer and use it in GitHub Desktop.
(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)
(interactive)
(tab-bar-select-tab ,num)))
;; Manual calls work
(progn
(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
(progn
(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
@DivineDominion
Copy link
Author

Thanks to @daviwil's feedback and documented fixed in https://gist.github.com/daviwil/3215f99db0ac0cb122a9a1850f400594 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 ()
                                  ,docstring
                                  (interactive)
                                  (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