-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjb-echo-keys.el
123 lines (114 loc) · 5.15 KB
/
pjb-echo-keys.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE: pjb-echo-keys.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; Echo emacs key chords as they are typed.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;; 2021-02-09 <PJB> By suggestion of [email protected], added
;;;; the option of displaying repeated command count.
;;;; 2014-06-05 <PJB> Extracted from ~/.emacs.
;;;;BUGS
;;;; We should define a customization for *echo-key-coallesce-repeats*.
;;;;LEGAL
;;;; AGPL3
;;;;
;;;; Copyright Pascal J. Bourguignon 2014 - 2021
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Affero General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, 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 Affero General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Affero General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************
(require 'cl)
(defvar *echo-keys-last* nil "Last command processed by `echo-keys'.")
(defvar *echo-keys-count* 0 "Number of times the `*echo-keys-last*` command was repeated.")
(defvar *echo-key-width* 40 "Default width of the *echo-keys* window.")
(defvar *echo-key-password-disable* nil "Temporarily disable echo key for password input.")
(defvar *echo-key-coallesce-repeats* nil
"When true, displays <key> <command> [N times] instead of n lines <key> <commands>.")
(defun echo-keys ()
(let ((deactivate-mark deactivate-mark)
(keys (this-command-keys)))
(when (and keys (not *echo-key-password-disable*))
(save-excursion
(with-current-buffer (get-buffer-create "*echo-key*")
(goto-char (point-max))
(if (eql this-command 'self-insert-command)
(let ((desc (key-description keys)))
(if (= 1 (length desc))
(insert desc)
(insert " " desc " "))
(setf *echo-keys-last* this-command
*echo-keys-count* 1))
(if (and *echo-key-coallesce-repeats*
(eql *echo-keys-last* this-command))
(progn
(incf *echo-keys-count*)
;; update the last line
(forward-line -1)
(if (= 2 *echo-keys-count*)
(progn
(end-of-line)
(insert (format " [%d times]" *echo-keys-count*)))
(save-match-data
(when (re-search-forward " \\[\\([0-9]+\\) times\\]" nil t)
(delete-region (match-beginning 1) (match-end 1))
(goto-char (match-beginning 1))
(insert (format "%d" *echo-keys-count*)))))
(forward-line 1))
(progn
(insert (if (eq 'self-insert-command *echo-keys-last*)
"\n"
"")
(format "%-12s %s\n"
(key-description keys)
this-command))
(setf *echo-keys-last* this-command
*echo-keys-count* 1))))
(dolist (window (window-list))
(when (eq (window-buffer window) (current-buffer))
(with-selected-window window
;; We need to use both to get the effect.
(set-window-point window (point))
(end-of-buffer)))))))))
(defun toggle-echo-keys ()
"Toggle displaying the *echo-key* buffer."
(interactive)
(if (member 'echo-keys (default-value 'pre-command-hook))
(let ((echo-buffer (get-buffer "*echo-key*")))
(remove-hook 'pre-command-hook 'echo-keys)
(dolist (window (window-list))
(when (eq (window-buffer window) echo-buffer)
(delete-window window))))
(progn
(delete-other-windows)
(split-window nil (- (window-width) *echo-key-width*) t)
(other-window 1)
(switch-to-buffer (get-buffer-create "*echo-key*"))
(toggle-truncate-lines +1)
(set-window-dedicated-p (selected-window) t)
(other-window 1)
(add-hook 'pre-command-hook 'echo-keys))))
(defadvice echo-key--read-passwd--disable (before read-passwd)
(message "echo-key--read-passwd--disable")
(setf *echo-key-password-disable* t))
(defadvice echo-key--read-passwd--enable (after read-passwd)
(message "echo-key--read-passwd--enable")
(setf *echo-key-password-disable* nil))
(provide 'pjb-echo-keys)
;;;; THE END ;;;;