Stumpwm: Using temporary keymaps

Posted: March 9, 2007 in Common Lisp

I’m a happy user of Stumpwm for about 2 month. Finally I found a window manager which can be configured to do exactly what I want, just like emacs.

Here’s a some hack from my .stumpwmrc.

These functions let me use a different keymap for a given amount of seconds.

(defvar *th-top-map* *top-map*
  "A backup of the *top-map* when stumpwm started.")

#+sbcl
(defparameter th-temporary-top-map-timer
  (sb-ext:make-timer (lambda () (th-back-to-top-map)))
  "The timer for the temporary *top-map*")

#+sbcl
(defun th-use-temporary-top-map (keymap seconds)
  (when (sb-ext:timer-scheduled-p th-temporary-top-map-timer)
    (sb-ext:unschedule-timer th-temporary-top-map-timer))
  (th-push-top-map keymap)
  (sb-ext:schedule-timer th-temporary-top-map-timer 2))

(defun th-push-top-map (keymap)
  (when (not (eq *top-map* keymap))
    (push-top-map keymap)))

(defun th-back-to-top-map ()
  (when *top-map-list*
    (setf *top-map* *th-top-map*
          *top-map-list* nil)
    (sync-keys)))


An example usage are my volume controls.

(defparameter *alsa-map*
  (let ((m (make-sparse-keymap)))
    (define-key m (kbd "ESC") "back-to-top-map")
    (define-key m (kbd "+")   "amixer-raise")
    (define-key m (kbd "-")   "amixer-lower")
    (define-key m (kbd "m")   "amixer-toggle-mute")
    (define-key m (kbd "v")   "amixer-show")
    m))

(define-keymap-help *alsa-map* "help-alsa")

(define-stumpwm-command "amixer-raise" ()
  (echo-string (current-screen)
               (run-shell-command
                (concatenate 'string "amixer sset Master 1%+ | grep \"^[ ]*Front\"") t))
  (th-use-temporary-top-map *alsa-map* 2))

(define-stumpwm-command "amixer-lower" ()
  (echo-string (current-screen)
               (run-shell-command
                (concatenate 'string "amixer sset Master 1%- | grep \"^[ ]*Front\"") t))
  (th-use-temporary-top-map *alsa-map* 2))

(define-stumpwm-command "amixer-show" ()
  (echo-string (current-screen)
               (run-shell-command "amixer sget Master | grep \"^[ ]*Front\"" t)))

(define-stumpwm-command "amixer-toggle-mute" ()
  (run-shell-command "amixer sset Master toggle"))


With that I can increase the volume for 5 steps by typing Pause a + + + + +. Pause is my prefix key, a activates *alsa-map* and + increases the volume. Two seconds after the last command of *alsa-map* the default koymap will be restored.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s