-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathkeycast.el
801 lines (700 loc) · 30.5 KB
/
keycast.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
;;; keycast.el --- Show current command and its binding -*- lexical-binding:t -*-
;; Copyright (C) 2018-2024 Jonas Bernoulli
;; Author: Jonas Bernoulli <emacs.keycast@jonas.bernoulli.dev>
;; Homepage: https://github.com/tarsius/keycast
;; Keywords: multimedia
;; Package-Version: 1.4.1
;; Package-Requires: ((emacs "26.1") (compat "30.0.0.0"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;; 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 of the License,
;; or (at your option) any later version.
;;
;; This file 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.
;;
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides four modes that display the current command and
;; its key or mouse binding.
;;
;; - `keycast-mode-line-mode' shows the current binding at the bottom of
;; the selected window, in its mode line.
;;
;; - `keycast-header-line-mode' shows the current binding at the top of
;; the selected window, in its header line.
;;
;; - `keycast-tab-bar-mode' shows the current binding at the top of
;; the selected frame, in its tab bar.
;;
;; - `keycast-log-mode' displays a list of recent bindings in a dedicated
;; frame.
;;; Code:
(require 'cl-lib)
(require 'compat)
(require 'format-spec)
(eval-when-compile (require 'subr-x))
;;; Options
;;;; Common
(defgroup keycast nil
"Show the current command and its key binding in the mode line."
:group 'applications)
(defcustom keycast-show-minibuffer-exit-command 'log-only
"Whether to show the command that exited the minibuffer.
If `log-only', the default, then show the command that exited the
minibuffer in the log buffer, but in places where just the latest
command is shown, show the command that used the minibuffer.
This is the default because if you use, e.g., `find-file' once,
then it is more reasonable to show
RET minibuffer-complete-and-exit
x self-insert-command x5
C-x C-f find-file
than this would be
C-x C-f find-file
x self-insert-command x5
C-x C-f find-file
Otherwise this has to be a boolean and its value applies to all
Keycast modes.
`execute-extended-command' is not affected by this option. For
that \"M-x the-selected-command\" is displayed unconditionally."
:group 'keycast
:type '(choice (const :tag "Exiting command in log only" log-only)
(const :tag "Exiting command everywhere" t)
(const :tag "Using command everywhere" nil)))
;;;; Mode-Line
(defcustom keycast-mode-line-insert-after 'mode-line-buffer-identification
"The position in `mode-line-format' where `keycast-mode-line' is inserted.
Enabling `keycast-mode-line-mode' inserts the element
`keycast-mode-line' into `mode-line-format' after the
element specified here."
:group 'keycast
:type '(choice :tag "Insert after"
(const mode-line-buffer-identification)
(const moody-mode-line-buffer-identification)
variable
sexp))
(defcustom keycast-mode-line-remove-tail-elements t
"Whether enabling `keycast-mode-line-mode' removes elements to the right.
When this is non-nil, then enabling `keycast-mode-line-mode' not
only inserts `keycast-mode-line' into `mode-line-format' but also
removes all elements to the right of where that was inserted."
:group 'keycast
:type 'boolean)
(defcustom keycast-mode-line-window-predicate 'keycast-active-frame-bottom-right-p
"Whether to display the binding in the mode line of the selected window.
This predicate is used while updating the mode line of a window
to determine whether the current command and its key binding
should be displayed in its mode line. The function is called
with no argument and acts on `selected-window'.
`moody-window-active-p'
Return non-nil if the temporarily selected window is the
active window, i.e., if it is the selected window as far
as the user is concerned. Load the `moody' library to be
able to use this.
`powerline-selected-window-active'
This function behaves like `moody-window-active-p', but
is defined in the `powerline' library. Load that library
to be able to use this.
`keycast-bottom-right-window-p'
Return non-nil if the temporarily selected window is the
right-most bottom window of its frame.
`keycast-active-frame-bottom-right-p'
Return non-nil if the temporarily selected window is the
right-most bottom window of the frame that is active as
far as the user is concerned. If neither the `moody' nor
the `powerline' library is loaded, then behave the same
as `keycast-bottom-right-window-p'."
:group 'keycast
:type 'function)
(defcustom keycast-mode-line-format "%10s%k%c%r"
"The format spec used by `keycast-mode-line'.
%s Some spaces, intended to be used like so: %10s.
%k The key using the `keycast-key' face and padding.
%K The key with no styling and without any padding.
%c The command using the `keycast-command' face.
%C The command with no styling.
%r The times the command was repeated.
%R The times the command was repeated using the `shadow' face."
:package-version '(keycast . "1.0.3")
:group 'keycast
:type 'string)
;;;; Header-Line
(defcustom keycast-header-line-insert-after ""
"The position in `header-line-format' where `keycast-header-line' is inserted.
Enabling `keycast-header-line-mode' inserts the element
`keycast-header-line' into `header-line-format' after the
element specified here."
:group 'keycast
:type '(choice :tag "Insert after"
string
variable
sexp))
(defcustom keycast-header-line-remove-tail-elements t
"Whether enabling `keycast-header-line-mode' removes elements to the right.
When this is non-nil, then enabling `keycast-header-linemode' not
only inserts `keycast-header-line' into `header-line-format' but
also removes all elements to the right of where that was inserted."
:group 'keycast
:type 'boolean)
(defcustom keycast-header-line-format "%k%c%r "
"The format spec used by `keycast-header-line'.
%s Some spaces, intended to be used like so: %10s.
%k The key using the `keycast-key' face and padding.
%K The key with no styling and without any padding.
%c The command using the `keycast-command' face.
%C The command with no styling.
%r The times the command was repeated.
%R The times the command was repeated using the `shadow' face."
:package-version '(keycast . "1.0.3")
:group 'keycast
:type 'string)
;;;; Tab-Bar
(defcustom keycast-tab-bar-location 'tab-bar-format-align-right
"The location in `tab-bar-format' where `keycast-tab-bar' is inserted.
Enabling `keycast-tab-bar-mode' inserts the element
`keycast-tab-bar' into `tab-tab-bar-format' at the location
specified here.
If the value is `beginning' or `end', then insert as the first or
last element. If the value is `replace', then insert as the only
element until the mode is disabled again.
Otherwise the value has to be a function that should be a member
of the format list. `keycast-tab-bar' is inserted after that
function if it is a member or at the end of the list if not, in
which case a warning is shown.
As a special case it the value is `tab-bar-format-align-right'
but that isn't a member yet, then insert that followed by
`keycast-tab-bar', without showing a warning."
:package-version '(keycast . "2.0.0")
:group 'keycast
:type
'(choice
(const :tag "Insert after tab-bar-format-add-tab" tab-bar-format-add-tab)
(const :tag "Insert after tab-bar-format-align-right" tab-bar-format-align-right)
(const :tag "Insert after tab-bar-format-global" tab-bar-format-global)
(function :tag "Insert after function")
(const :tag "Insert as first element" beginning)
(const :tag "Insert as last element" end)
(const :tag "Replace all other elements" replace)))
(defcustom keycast-tab-bar-format "%k%c%r"
"The format spec used by `keycast-tab-bar'.
%s Some spaces, intended to be used like so: %10s.
%k The key using the `keycast-key' face and padding.
%K The key with no styling and without any padding.
%c The command using the `keycast-command' face.
%C The command with no styling.
%r The times the command was repeated.
%R The times the command was repeated using the `shadow' face."
:group 'keycast
:type 'string)
(defcustom keycast-tab-bar-minimal-width 40
"The minimal width of `keycast-tab-bar'."
:package-version '(keycast . "2.0.0")
:group 'keycast
:type 'integer)
;;;; Log-Buffer
(defcustom keycast-log-format "%-20K%C%R\n"
"The format spec used by `keycast-log-mode'.
%s `keycast-separator-width' spaces.
%k The key using the `keycast-key' face and padding.
%K The key with no styling and without any padding.
%c The command using the `keycast-command' face.
%C The command with no styling.
%r The times the command was repeated.
%R The times the command was repeated using the `shadow' face."
:package-version '(keycast . "2.0.0")
:group 'keycast
:type 'string)
(defcustom keycast-log-frame-alist
'((minibuffer . nil)
(no-other-frame . t)
(no-other-window . t)
(no-delete-other-windows . t)
(no-accept-focus . t)
(no-focus-on-map . t)
(desktop-dont-save . t)
(menu-bar-lines . 0)
(tool-bar-lines . 0)
(tab-bar-lines . 0)
(vertical-scroll-bars . nil)
(horizontal-scroll-bars . nil))
"Alist of frame parameters used by `keycast-log-mode's frame."
:package-version '(keycast . "2.0.0")
:group 'keycast
:type 'alist)
(defcustom keycast-log-newest-first t
"Whether `keycast-log-mode' inserts events at beginning of buffer."
:package-version '(keycast . "2.0.0")
:group 'keycast
:type 'boolean)
(defcustom keycast-log-buffer-name " *keycast*"
"The name of the buffer used by `keycast-log-mode'."
:package-version '(keycast . "2.0.0")
:group 'keycast
:type 'string)
;;;; Common
(defcustom keycast-substitute-alist
'((keycast-log-erase-buffer nil nil)
(transient-update nil nil))
"Alist used to substituted events and/or commands for display.
Occasionally it might be necessary to pretend you pressed another
key than the one you actually pressed (because watchers don't
care about your weird key bindings), or to hide certain commands
\(such as `self-insert-command'). This option allows doing that
and more.
Each element has the form (MATCH EVENT COMMAND). MATCH is an
event or a command. When a command is invoked then this package
looks for a MATCH for that. If and only if there is no match for
that, then it looks for a MATCH for its key binding. If there is
a match of either kind, then the respective EVENT and COMMAND are
used.
If either EVENT or COMMAND is nil, then neither the event nor the
command is shown (regardless of the value of the other).
Otherwise if EVENT is t then the actual event is shown, else it
has to be a string to be shown instead. Likewise COMMAND can be
t to show the actual COMMAND, or a symbol to be shown instead."
:group 'keycast
:type '(repeat
(list (choice :format "%{Actual event/command%}: %[Value Menu%] %v"
(string :tag "Event")
(symbol :tag "Command")
(const :tag "Lambda" t))
(choice :format "%{Display event%}: %[Value Menu%] %v"
(const :tag "Omit binding" nil)
(const :tag "Use actual event" t)
(string :tag "Substitute event"))
(choice :format "%{Display command%}: %[Value Menu%] %v"
(const :tag "Omit binding" nil)
(const :tag "Use actual command" t)
(symbol :tag "Substitute command")))))
;;;; Faces
(defface keycast-key
'((t ( :inherit fixed-pitch
:weight bold
:background "#d5cfbf"
:foreground "#000000"
:box (:line-width -3 :style released-button))))
"When Keycast mode is enabled, face used for the key in the mode line."
:group 'keycast)
(defface keycast-command '((t (:weight bold)))
"When Keycast mode is enabled, face used for the command in the mode line."
:group 'keycast)
;;; Common
(defvar keycast-mode-line-mode)
(defvar keycast-header-line-mode)
(defvar keycast-tab-bar-mode)
(defvar keycast-log-mode)
(defun keycast--mode-active-p (&optional line)
(or keycast-mode-line-mode
keycast-header-line-mode
keycast-tab-bar-mode
(and (not line) keycast-log-mode)))
(defvar keycast--this-command-desc nil)
(defvar keycast--this-command-keys nil)
(defvar keycast--minibuffer-exited nil)
(defvar keycast--command-repetitions 0)
(defvar keycast--reading-passwd nil)
(defvar keycast--prefix-argument-commands
'(universal-argument
universal-argument-more
negative-argument
digit-argument))
(defun keycast--minibuffer-exit ()
(setq keycast--minibuffer-exited
(cons (this-single-command-keys) this-command))
;; If the command that used the minibuffer is immediately
;; repeated, then don't treat that as a repetition because
;; at least the command that exited the minibuffer was used
;; in between (but `last-command' doesn't account for that).
(setq keycast--command-repetitions -2))
(defun keycast--update ()
(let ((key (this-single-command-keys))
(cmd this-command))
(cond
((memq this-original-command keycast--prefix-argument-commands)
;; These commands set `this-command' to `last-command', to
;; prevent `last-command' being the prefix command, for the
;; command that uses the prefix argument.
(setq cmd this-original-command))
((and (not cmd)
(length> key 0)
(eq (aref key (1- (length key))) ?\C-g))
;; If a valid but incomplete prefix sequence is followed by
;; an unbound key, then Emacs calls the `undefined' command
;; but does not set `this-command', which is nil instead.
(setq cmd 'undefined)))
(cond
;; Special handling for `execute-extended-command'.
((eq this-original-command 'execute-extended-command)
;; Instead of "M-x t h e - c o m m a n d RET", just use
;; "M-x", because we follow that up with the command anyway.
(setq key [?\M-x]))
;; If a command uses the minibuffer then `post-command-hook'
;; gets called twice on behalf of that command:
((equal key [])
;; 1. When the minibuffer is entered. The key is void in that
;; case, which allows us to detect that it is the first call,
;; but also means we have to get the actual key like this.
(setq key (this-single-command-raw-keys)))
(keycast--minibuffer-exited
;; 2. When the minibuffer is exited (unless it is aborted).
(when (eq keycast-show-minibuffer-exit-command t)
(setq key (car keycast--minibuffer-exited))
(setq cmd (cdr keycast--minibuffer-exited)))))
(setq keycast--this-command-keys key)
(setq keycast--this-command-desc (keycast--format-command cmd))
(if (or (eq last-command cmd)
(< keycast--command-repetitions 0))
(cl-incf keycast--command-repetitions)
(setq keycast--command-repetitions 0)))
(when keycast-mode-line-mode
(keycast--maybe-edit-local-format
'mode-line-format
'keycast-mode-line
'keycast--mode-line-modified-buffers))
(when keycast-header-line-mode
(keycast--maybe-edit-local-format
'header-line-format
'keycast-header-line
'keycast--header-line-modified-buffers))
(when (and keycast-log-mode
(not keycast--reading-passwd))
(if (and keycast--minibuffer-exited
(eq keycast-show-minibuffer-exit-command 'log-only)
(not (eq this-original-command 'execute-extended-command)))
(let ((keycast--this-command-keys (car keycast--minibuffer-exited))
(keycast--this-command-desc (keycast--format-command
(cdr keycast--minibuffer-exited))))
(keycast-log-update-buffer))
(keycast-log-update-buffer)))
(setq keycast--minibuffer-exited nil)
(when (keycast--mode-active-p 'line)
(force-mode-line-update (minibufferp))))
(defun keycast--format-command (cmd)
(cond ((symbolp cmd) cmd)
((eq (car-safe cmd) 'lambda) "<lambda>")
(t (format "<%s>" (type-of cmd)))))
(defun keycast--maybe-edit-local-format (format item record)
(let ((value (buffer-local-value format (current-buffer))))
(unless (or (eq value (default-value format))
(keycast--tree-member item value))
(set record (cons (current-buffer) (symbol-value record)))
(set format (if (keycast--format-atom-p value)
(list "" item value)
(nconc (list "" item) value))))))
(defun keycast--format-atom-p (format)
(or (stringp format)
(and (not (stringp (car-safe format)))
(not (listp (car-safe format))))))
(defun keycast--format (format)
(and (not keycast--reading-passwd)
(let* ((key (ignore-errors
(key-description keycast--this-command-keys)))
(cmd keycast--this-command-desc)
(elt (or (assoc cmd keycast-substitute-alist)
(assoc key keycast-substitute-alist))))
(when elt
(pcase-let ((`(,_ ,k ,c) elt))
(unless (eq k t) (setq key k))
(unless (eq c t) (setq cmd c))))
(and key cmd
(let ((k (if (and (bound-and-true-p mode-line-compact)
(eq format keycast-mode-line-format))
key
(let ((pad (max 2 (- 5 (length key)))))
(concat (make-string (ceiling pad 2) ?\s) key
(make-string (floor pad 2) ?\s)))))
(c (format " %s" cmd))
(r (if (> keycast--command-repetitions 0)
(format " x%s" (1+ keycast--command-repetitions))
"")))
(format-spec
format
`((?s . "")
(?k . ,(propertize k 'face 'keycast-key))
(?K . ,key)
(?c . ,(propertize c 'face 'keycast-command))
(?C . ,c)
(?r . ,r)
(?R . ,(propertize r 'face 'shadow)))))))))
(define-advice read-passwd
(:around (fn prompt &optional confirm default) keycast)
"Suppress echoing keys while reading passwords."
(let ((keycast--reading-passwd t))
(funcall fn prompt confirm default)))
(defun keycast-bottom-right-window-p ()
(and (window-at-side-p nil 'right)
(window-at-side-p nil 'bottom)))
(defun keycast-active-frame-bottom-right-p ()
(and (keycast-bottom-right-window-p)
(keycast--active-frame-p)))
(defun keycast--active-frame-p ()
(cond ((boundp 'moody--active-window)
(eq (window-frame) (window-frame moody--active-window)))
((boundp 'powerline-selected-window)
(eq (window-frame) (window-frame powerline-selected-window)))
(t t)))
(defun keycast--tree-member (elt tree &optional delete)
;; Also known as auto-compile--tree-member.
(and (listp tree)
(if-let* ((pos (cl-position elt tree))
(mem (nthcdr pos tree)))
(cond ((not delete) mem)
((cdr mem)
(setcar mem (cadr mem))
(setcdr mem (cddr mem))
nil)
((nbutlast tree) nil))
(catch 'found
(dolist (sub tree)
(when-let ((found (keycast--tree-member elt sub delete)))
(throw 'found found)))))))
;;; Mode-Line
(defvar keycast--mode-line-removed-tail nil)
(defvar keycast--temporary-mode-line nil)
(defvar keycast--mode-line-modified-buffers nil)
(defvar keycast--mode-line-enabled nil)
;;;###autoload
(define-minor-mode keycast-mode-line-mode
"Show current command and its key binding in the mode line."
:global t
(cond
(keycast-mode-line-mode
(when keycast--mode-line-enabled
(keycast-mode-line-mode -1)
(setq keycast-mode-line-mode t))
(let ((format (default-value 'mode-line-format)))
(cond ((not format)
(setq keycast--temporary-mode-line t)
(setq-default mode-line-format (list "")))
((keycast--format-atom-p format)
(setq-default mode-line-format (list "" format)))))
(let ((cons (keycast--tree-member keycast-mode-line-insert-after
(default-value 'mode-line-format))))
(unless cons
(setq keycast-mode-line-mode nil)
(user-error
"Cannot turn on %s. %s not found in %s. Try customizing %s."
'keycast-mode-line-mode keycast-mode-line-insert-after
'mode-line-format 'keycast-mode-line-insert-after))
(cond (keycast-mode-line-remove-tail-elements
(setq keycast--mode-line-removed-tail (cdr cons))
(setcdr cons (list 'keycast-mode-line)))
(t
(setcdr cons (cl-pushnew 'keycast-mode-line (cdr cons)))))
(add-hook 'post-command-hook #'keycast--update t)
(add-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit t))
(setq keycast--mode-line-enabled t))
(t
(let ((cons (keycast--tree-member 'keycast-mode-line
(default-value 'mode-line-format))))
(cond (keycast--temporary-mode-line
(setq-default mode-line-format nil)
(setq keycast--temporary-mode-line nil))
(keycast--mode-line-removed-tail
(setcar cons (car keycast--mode-line-removed-tail))
(setcdr cons (cdr keycast--mode-line-removed-tail))
(setq keycast--mode-line-removed-tail nil))
((keycast--tree-member 'keycast-mode-line
(default-value 'mode-line-format)
'delete))))
(while-let ((buf (pop keycast--mode-line-modified-buffers)))
(when (and (buffer-live-p buf)
(condition-case nil
(listp (buffer-local-value 'mode-line-format buf))
(void-variable nil)))
(with-current-buffer buf
(keycast--tree-member 'keycast-mode-line mode-line-format 'delete))))
(unless (keycast--mode-active-p)
(remove-hook 'post-command-hook #'keycast--update)
(remove-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit))
(setq keycast--mode-line-enabled nil))))
(defvar keycast-mode-line
'(:eval
(and (funcall keycast-mode-line-window-predicate)
(keycast--format keycast-mode-line-format))))
(put 'keycast-mode-line 'risky-local-variable t)
(make-variable-buffer-local 'keycast-mode-line)
;;; Header-Line
(defvar keycast--temporary-header-line nil)
(defvar keycast--header-line-removed-tail nil)
(defvar keycast--header-line-modified-buffers nil)
;;;###autoload
(define-minor-mode keycast-header-line-mode
"Show current command and its key binding in the header line."
:global t
(cond
(keycast-header-line-mode
(cond ((not (default-value 'header-line-format))
(setq keycast--temporary-header-line t)
(setq-default header-line-format (list "")))
((keycast--format-atom-p header-line-format)
(setq-default header-line-format (list "" header-line-format))))
(let ((cons (keycast--tree-member keycast-header-line-insert-after
(default-value 'header-line-format))))
(unless cons
(setq keycast-header-line-mode nil)
(user-error
"Cannot turn on %s. %s not found in %s. Try customizing %s."
'keycast-header-line-mode keycast-header-line-insert-after
'header-line-format 'keycast-header-line-insert-after))
(cond (keycast-header-line-remove-tail-elements
(setq keycast--header-line-removed-tail (cdr cons))
(setcdr cons (list 'keycast-header-line)))
(t
(setcdr cons (cl-pushnew 'keycast-header-line (cdr cons)))))
(add-hook 'post-command-hook #'keycast--update t)
(add-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit t)))
(t
(let ((cons (keycast--tree-member 'keycast-header-line
(default-value 'header-line-format))))
(cond (keycast--temporary-header-line
(setq keycast--temporary-header-line nil)
(setq-default header-line-format nil))
(keycast--header-line-removed-tail
(setcar cons (car keycast--header-line-removed-tail))
(setcdr cons (cdr keycast--header-line-removed-tail)))
((keycast--tree-member 'keycast-header-line
(default-value 'header-line-format)
'delete))))
(setq keycast--header-line-removed-tail nil)
(dolist (buf keycast--header-line-modified-buffers)
(when (buffer-live-p buf)
(with-current-buffer buf
(unless (stringp header-line-format)
(setq header-line-format
(delq 'keycast-header-line header-line-format))))))
(unless (keycast--mode-active-p)
(remove-hook 'post-command-hook #'keycast--update)
(remove-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit)))))
(defvar keycast-header-line
'(:eval
(and (funcall keycast-mode-line-window-predicate)
(keycast--format keycast-header-line-format))))
(put 'keycast-header-line 'risky-local-variable t)
(make-variable-buffer-local 'keycast-header-line)
;;; Tab-Bar
(eval-when-compile
(defvar tab-bar-format)
(defvar tab-bar-mode))
(declare-function tab-bar-mode "tab-bar" (&optional arg))
(defvar keycast--temporary-tab-bar nil)
(defvar keycast--previous-tab-bar-format nil)
;;;###autoload
(define-minor-mode keycast-tab-bar-mode
"Show current command and its key binding in the tab bar."
:global t
(when (< emacs-major-version 28)
(user-error "`keycast-tab-bar-mode' requires Emacs 28.1"))
(cond
(keycast-tab-bar-mode
(unless tab-bar-mode
(setq keycast--temporary-tab-bar t)
(tab-bar-mode 1))
(cl-case keycast-tab-bar-location
(replace
(setq keycast--previous-tab-bar-format tab-bar-format)
(setq tab-bar-format (list 'keycast-tab-bar)))
(beginning
(setq tab-bar-format (cons 'keycast-tab-bar tab-bar-format)))
(end
(setq tab-bar-format (nconc tab-bar-format (list 'keycast-tab-bar))))
(t
(let ((mem (memq keycast-tab-bar-location tab-bar-format)))
(if mem
(setcdr mem (cl-pushnew 'keycast-tab-bar (cdr mem)))
(setq tab-bar-format
(nconc tab-bar-format
(if (eq keycast-tab-bar-location
'tab-bar-format-align-right)
(list 'tab-bar-format-align-right
'keycast-tab-bar)
(message "%s not found in %s; adding at end instead"
keycast-tab-bar-location 'tab-bar-format)
(list 'keycast-tab-bar))))))))
(add-hook 'post-command-hook #'keycast--update t)
(add-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit t))
(t
(when keycast--temporary-tab-bar
(setq keycast--temporary-tab-bar nil)
(tab-bar-mode -1))
(cond (keycast--previous-tab-bar-format
(setq tab-bar-format keycast--previous-tab-bar-format)
(setq keycast--previous-tab-bar-format nil))
(t
(setq tab-bar-format (delq 'keycast-tab-bar tab-bar-format))))
(unless (keycast--mode-active-p)
(remove-hook 'post-command-hook #'keycast--update)
(remove-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit)))))
(defun keycast-tab-bar ()
"Produce key binding information for the tab bar."
(and keycast-tab-bar-mode
(keycast--active-frame-p)
(and-let* ((output (keycast--format keycast-tab-bar-format)))
(concat output
(make-string (max 0 (- keycast-tab-bar-minimal-width
(length output)))
?\s)))))
;;; Log-Buffer
;;;###autoload
(define-minor-mode keycast-log-mode
"Log invoked commands and their key bindings in a buffer."
:global t
(cond
(keycast-log-mode
(add-hook 'post-command-hook #'keycast--update t)
(add-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit t)
(keycast-log--set-focus-properties t)
(keycast-log-update-buffer))
((not (keycast--mode-active-p))
(remove-hook 'post-command-hook #'keycast--update)
(remove-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit)
(keycast-log--set-focus-properties nil))))
(defun keycast-log-update-buffer ()
(let ((buf (get-buffer keycast-log-buffer-name)))
(unless (buffer-live-p buf)
(setq buf (get-buffer-create keycast-log-buffer-name))
(with-current-buffer buf
(setq buffer-read-only t)
(setq mode-line-format nil)))
(unless (get-buffer-window buf t)
(display-buffer-pop-up-frame
buf `((pop-up-frame-parameters . ,keycast-log-frame-alist))))
(when-let ((output (keycast--format keycast-log-format)))
(with-current-buffer buf
(goto-char (if keycast-log-newest-first (point-min) (point-max)))
(let ((inhibit-read-only t))
(when (and (> keycast--command-repetitions 0)
(string-match-p "%[rR]" keycast-log-format))
(unless keycast-log-newest-first
(backward-char))
(ignore-errors
(delete-region (line-beginning-position)
(1+ (line-end-position)))))
(insert output))
(goto-char (if keycast-log-newest-first (point-min) (point-max)))))))
(defun keycast-log--set-focus-properties (value)
(when-let* ((buffer (get-buffer keycast-log-buffer-name))
(window (get-buffer-window buffer t))
(frame (window-frame window)))
(when (cdr (assq 'no-accept-focus keycast-log-frame-alist))
(set-frame-parameter frame 'no-accept-focus value))
(when (cdr (assq 'no-accept-on-map keycast-log-frame-alist))
(set-frame-parameter frame 'no-focus-on-map value))))
(defun keycast-log-erase-buffer ()
"Erase the contents of `keycast-log-mode's buffer."
(interactive)
(let ((buf (get-buffer keycast-log-buffer-name)))
(when (buffer-live-p buf)
(with-current-buffer buf
(let ((inhibit-read-only t))
(erase-buffer))))))
;;; _
(provide 'keycast)
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
;;; keycast.el ends here