diff options
-rw-r--r-- | sx-switchto.el | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/sx-switchto.el b/sx-switchto.el index e5889e1..1a2c3a0 100644 --- a/sx-switchto.el +++ b/sx-switchto.el @@ -43,13 +43,31 @@ ("f" sx-tab-featured) ("i" sx-inbox) ("n" sx-tab-newest) - ("s" sx-question-list-switch-site) ("t" sx-tab-switch) ("U" sx-tab-unanswered-my-tags) ("v" sx-tab-topvoted) ("w" sx-tab-week) )) + +;;; These are keys which depend on context. +;;;; For instance, it makes no sense to have `switch-site' bound to a +;;;; key on a buffer with no `sx-question-list--site' variable. +(defmacro sx--define-conditional-key (keymap key def &rest body) + "In KEYMAP, define key sequence KEY as DEF conditionally. +This is like `define-key', except the definition \"disapears\" +whenever BODY evaluates to nil." + (declare (indent 3) + (debug (form form form &rest sexp))) + `(define-key ,keymap ,key + '(menu-item + ,(format "maybe-%s" (or (car (cdr-safe def)) def)) ignore + :filter (lambda (&optional _) + (when (progn ,@body) ,def))))) + +(sx--define-conditional-key sx-switchto-map "s" #'sx-question-list-switch-site + (and (boundp 'sx-question-list--site) sx-question-list--site)) + (provide 'sx-switchto) ;;; sx-switchto.el ends here |