Skip to content

Instantly share code, notes, and snippets.

@progfolio
Created July 24, 2022 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save progfolio/99bb935c2eccaa33ffac9be29ce29cb8 to your computer and use it in GitHub Desktop.
Save progfolio/99bb935c2eccaa33ffac9be29ce29cb8 to your computer and use it in GitHub Desktop.
slow-keys.el rewrite
;;; slow-keys.el --- Slow keys mode to avoid RSI -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Manuel Uberti
;; Author: Manuel Uberti <manuel.uberti@inventati.org>
;; URL: https://github.com/manuel-uberti/slow-keys
;; Version: 0.1.0
;; Package-Requires: ((emacs "24.1"))
;; Keywords: convenience
;; This file is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation; either version 3, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;; For a full copy of the GNU General Public License see
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides the minor mode `slow-keys-mode' that forces you to type
;; slowly.
;; It is a porting of Chris Done's original code, that can be found here:
;; https://github.com/chrisdone/chrisdone-emacs/blob/master/packages/slow-keys/slow-keys.el
;;; Code:
(defgroup slow-keys nil
"Customization of user options for slow-keys."
:group 'convenience)
(defcustom slow-keys-sleep-for 0.5
"Time, in seconds, to wait before start typing again."
:type 'number)
(defcustom slow-keys-min-delay 0.1
"The minimum delay, in seconds, between key presses."
:type 'number)
(defcustom slow-keys-ignored-commands '(mwheel-scroll)
"List of commands which are not throttled by `slow-keys-mode'."
:type 'list)
(defcustom slow-keys-warning-hook '(slow-keys--reminder)
"Hook run when key presses exceed `slow-keys-min-delay'."
:type 'hook)
(defun slow-keys--reminder ()
"Remind the user to take a break."
(message "Slow down.")
(sleep-for slow-keys-sleep-for))
(defvar slow-keys--timer nil "Timer used to keep track of key press rate.")
(defvar slow-keys--count -1)
(defun slow-keys--reset ()
"Reset `slow-keys--count'."
(setq slow-keys--count -1))
(defun slow-keys--maybe-warn ()
"Check whether typing or running a command is done slowly enough."
(unless (or executing-kbd-macro (memq this-command slow-keys-ignored-commands))
(setq slow-keys--count (1+ slow-keys--count))
(if (not (zerop slow-keys--count))
(run-hooks 'slow-keys-warning-hook)
(when slow-keys--timer (cancel-timer slow-keys--timer))
(setq slow-keys--timer (run-at-time slow-keys-min-delay nil #'slow-keys--reset)))))
;;;###autoload
(define-minor-mode slow-keys-mode
"Type slowly to avoid RSI."
:lighter " Slow"
:require 'slow-keys
:global t
(funcall (if slow-keys-mode #'add-hook #'remove-hook)
'post-command-hook #'slow-keys--maybe-warn))
(provide 'slow-keys)
;;; slow-keys.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment