2019-10-09 13:13:56 +02:00
|
|
|
;;; keyboard.el --- Managing keyboard preferences with Elisp -*- lexical-binding: t -*-
|
2020-09-01 11:17:43 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 11:17:43 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Setting key repeat and other values.
|
|
|
|
;;
|
|
|
|
;; Be wary of suspiciously round numbers. Especially those divisible by ten!
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(require 'string)
|
|
|
|
(require 'number)
|
2020-09-02 16:23:46 +02:00
|
|
|
(require 'cl-lib)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Constants
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; TODO: Support clamping functions for repeat-{rate,delay} to ensure only valid
|
|
|
|
;; values are sent to xset.
|
2020-09-01 11:17:43 +02:00
|
|
|
(defcustom keyboard-repeat-rate 80
|
2019-10-09 13:13:56 +02:00
|
|
|
"The number of key repeat signals sent per second.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defcustom keyboard-repeat-delay 170
|
2019-10-09 13:13:56 +02:00
|
|
|
"The number of milliseconds before autorepeat starts.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst keyboard-repeat-rate-copy keyboard-repeat-rate
|
|
|
|
"Copy of `keyboard-repeat-rate' to support `keyboard-reset-key-repeat'.")
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst keyboard-repeat-delay-copy keyboard-repeat-delay
|
|
|
|
"Copy of `keyboard-repeat-delay' to support `keyboard-reset-key-repeat'.")
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defcustom keyboard-install-preferences? t
|
2019-10-09 13:13:56 +02:00
|
|
|
"When t, install keyboard preferences.")
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Functions
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-message (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Message X in a structured way."
|
2020-09-01 00:28:47 +02:00
|
|
|
(message (string-format "[keyboard.el] %s" x)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(cl-defun keyboard-set-key-repeat (&key
|
|
|
|
(rate keyboard-repeat-rate)
|
|
|
|
(delay keyboard-repeat-delay))
|
2019-10-09 13:13:56 +02:00
|
|
|
"Use xset to set the key-repeat RATE and DELAY."
|
2020-08-31 18:05:31 +02:00
|
|
|
(prelude-start-process
|
2020-09-01 11:17:43 +02:00
|
|
|
:name "keyboard-set-key-repeat"
|
2020-09-01 00:28:47 +02:00
|
|
|
:command (string-format "xset r rate %s %s" delay rate)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; NOTE: Settings like this are machine-dependent. For instance I only need to
|
|
|
|
;; do this on my laptop and other devices where I don't have access to my split
|
|
|
|
;; keyboard.
|
|
|
|
;; NOTE: Running keysym Caps_Lock is not idempotent. If this is called more
|
|
|
|
;; than once, xmodmap will start to error about non-existent Caps_Lock symbol.
|
|
|
|
;; For more information see here:
|
|
|
|
;; https://unix.stackexchange.com/questions/108207/how-to-map-caps-lock-as-the-compose-key-using-xmodmap-portably-and-idempotently
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-swap-caps-lock-and-escape ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Swaps the caps lock and escape keys using xmodmap."
|
|
|
|
(interactive)
|
2020-08-31 18:05:31 +02:00
|
|
|
;; TODO: Ensure these work once the tokenizing in prelude-start-process works
|
2020-01-18 23:34:55 +01:00
|
|
|
;; as expected.
|
2020-09-01 14:44:18 +02:00
|
|
|
(start-process "keyboard-swap-caps-lock-and-escape"
|
|
|
|
nil "/usr/bin/xmodmap" "-e" "remove Lock = Caps_Lock")
|
|
|
|
(start-process "keyboard-swap-caps-lock-and-escape"
|
|
|
|
nil "/usr/bin/xmodmap" "-e" "keysym Caps_Lock = Escape"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-inc-repeat-rate ()
|
|
|
|
"Increment `keyboard-repeat-rate'."
|
2019-10-09 13:13:56 +02:00
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(setq keyboard-repeat-rate (number-inc keyboard-repeat-rate))
|
|
|
|
(keyboard-set-key-repeat :rate keyboard-repeat-rate)
|
|
|
|
(keyboard-message
|
|
|
|
(string-format "Rate: %s" keyboard-repeat-rate)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-dec-repeat-rate ()
|
|
|
|
"Decrement `keyboard-repeat-rate'."
|
2019-10-09 13:13:56 +02:00
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(setq keyboard-repeat-rate (number-dec keyboard-repeat-rate))
|
|
|
|
(keyboard-set-key-repeat :rate keyboard-repeat-rate)
|
|
|
|
(keyboard-message
|
|
|
|
(string-format "Rate: %s" keyboard-repeat-rate)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-inc-repeat-delay ()
|
|
|
|
"Increment `keyboard-repeat-delay'."
|
2019-10-09 13:13:56 +02:00
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(setq keyboard-repeat-delay (number-inc keyboard-repeat-delay))
|
|
|
|
(keyboard-set-key-repeat :delay keyboard-repeat-delay)
|
|
|
|
(keyboard-message
|
|
|
|
(string-format "Delay: %s" keyboard-repeat-delay)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-dec-repeat-delay ()
|
|
|
|
"Decrement `keyboard-repeat-delay'."
|
2019-10-09 13:13:56 +02:00
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(setq keyboard-repeat-delay (number-dec keyboard-repeat-delay))
|
|
|
|
(keyboard-set-key-repeat :delay keyboard-repeat-delay)
|
|
|
|
(keyboard-message
|
|
|
|
(string-format "Delay: %s" keyboard-repeat-delay)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-print-key-repeat ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Print the currently set values for key repeat."
|
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(keyboard-message
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-format "Rate: %s. Delay: %s"
|
2020-09-01 11:17:43 +02:00
|
|
|
keyboard-repeat-rate
|
|
|
|
keyboard-repeat-delay)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-set-preferences ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Reset the keyboard preferences to their default values.
|
|
|
|
NOTE: This function exists because occasionally I unplug and re-plug in a
|
|
|
|
keyboard and all of the preferences that I set using xset disappear."
|
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(keyboard-swap-caps-lock-and-escape)
|
|
|
|
(keyboard-set-key-repeat :rate keyboard-repeat-rate
|
|
|
|
:delay keyboard-repeat-delay)
|
2019-10-09 13:13:56 +02:00
|
|
|
;; TODO: Implement this message function as a macro that pulls the current
|
|
|
|
;; file name.
|
2020-09-01 11:17:43 +02:00
|
|
|
(keyboard-message "Keyboard preferences set!"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun keyboard-reset-key-repeat ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Set key repeat rate and delay to original values."
|
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(keyboard-set-key-repeat :rate keyboard-repeat-rate-copy
|
|
|
|
:delay keyboard-repeat-delay-copy)
|
|
|
|
(keyboard-message "Key repeat preferences reset."))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(when keyboard-install-preferences?
|
|
|
|
(keyboard-set-preferences))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(provide 'keyboard)
|
|
|
|
;;; keyboard.el ends here
|