-
Notifications
You must be signed in to change notification settings - Fork 3
/
win-switch.el
1627 lines (1421 loc) · 67.3 KB
/
win-switch.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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; win-switch.el --- fast, dynamic bindings for window-switching/resizing
;; Copyright (C) 2011-2015 Christopher R. Genovese, all rights reserved.
;; Author: Christopher Genovese <[email protected]>
;; Maintainer: Christopher R. Genovese <[email protected]>
;; URL: http://www.stat.cmu.edu/~genovese/emacs/win-switch/
;; Version: 1.1.4
;; Update#: 25
;; Created: Wed 28 Jul 2011 at 00:27 EDT
;; Last-Updated: Sun 09 Oct 2016 at 12:20 EDT
;; By: Christopher R. Genovese
;; Keywords: window, switch, key bindings, ergonomic, efficient
;; Compatibility: GNU Emacs 22, GNU Emacs 23.4, Gnu Emacs 24.4.1
;; Tested on these versions on Mac OS X 10.7.5.
;; Testing or feedback for other platforms/versions
;; would be very much appreciated.
;;; Commentary:
;;
;; If you use multiple windows in an Emacs frame, you may find yourself
;; moving through the window configuration using `other-window' (C-x o)
;; again and again. Because the order of windows in the window list
;; need not relate intuitively to windows' positions, moving
;; efficiently can require context-specific prefix arguments along the
;; way. The tiring outcome is that navigation through a complex window
;; configuration demands many keystrokes and nontrivial attention.
;; This package is designed to solve that problem.
;;
;; While the `windmove' package provides functions for moving
;; intuitively among windows, the natural key bindings for these
;; functions (e.g., the arrow keys with some modifier) require a
;; distant and thus inefficient hand movement. Moreover, one often
;; wants to mix a variety of window-based operations (other-window,
;; previous-window, directional movement, resizing) in rapid
;; succession.
;;
;; This package builds on the windmove functionality by defining a
;; command `win-switch-dispatch' that engages a dynamic, transient
;; keyboard override, allowing one to efficiently move among defined
;; windows (and frames) -- and even resize, split, delete them -- with
;; minimal fuss and effort. When the override is engaged, the movement
;; and resizing commands are bound to simple keys that can be pressed
;; quickly with one hand. The override ends either when the user exits
;; explicitly or after a configurable idle time threshold. The happy
;; outcome is fast and seamless navigation.
;;
;; To use the package, execute the following code either directly
;; or in your .emacs file:
;;
;; (require 'win-switch)
;; (global-set-key "\C-xo" 'win-switch-dispatch)
;;
;; or use whatever keybinding you ordinarily have set to `other-window'.
;; Alternatively, you can use one of a variety of predefined configuration
;; commands, as in
;;
;; (require 'win-switch)
;; (win-switch-setup-keys-ijkl "\C-xo")
;;
;; which has the same effect as the above.
;;
;; Now, when executing a window switch (i.e., hitting C-xo), Emacs enters
;; window switching mode, which lasts until either the user exits the
;; mode or the idle time exceeds the threshold `win-switch-idle-time'.
;; During this override, selected keys move among windows (or frames)
;; or resize the windows. The following keys are bound by default:
;;
;; + i select the window above the current window.
;; + k select the window below the current window.
;; + j select the window left of the current window.
;; + l select the window right of the current window.
;; + o cycle forward through the window list in the current frame.
;; + p cycle backward through the window list in the current frame.
;; + SPACE cycles among existing frames.
;; + u (and RETURN) exit window switching mode.
;; + I and K vertically enlarge and shrink the current window, respectively.
;; + L and J horizontally enlarge and shrink the current window, respectively.
;; + h and ; split the current window, horizontally and vertically, respectively.
;; + ESCAPE acts as an "emergency" exit
;;
;; All other keys exit window switching mode and execute their original function.
;;
;; By default, window selection wraps around when moving across a frame
;; edge and window switching mode is forgone when there are only two
;; windows. But these features, the key bindings, and other parameters
;; can all be customized, either with the customization facility or
;; with defvar and setter functions.
;;
;; The default keybindings are designed for fast and intuitve,
;; one-handed operation, but if desired the key bindings can be easily
;; adjusted or reset. Several alternative key configurations are pre-defined
;; (see `win-switch-setup-keys-ijkl', `win-switch-setup-keys-arrow-ctrl',
;; `win-switch-setup-keys-arrow-meta', and `win-switch-setup-keys-esdf'
;; below). The keys also can be rebound in groups via the variables
;; `win-switch-<name>-keys' where <name> can be one of up, down, left,
;; right, next-window, previous-window, enlarge-vertically,
;; shrink-vertically, enlarge-horizontally, shrink-horizontally,
;; other-frame, exit, split-vertically, split-horizontally, delete-window,
;; or emergency-exit. These variables should not be set directly,
;; but rather should be set either by customize or by
;; using the functions `win-switch-add-key', `win-switch-delete-key',
;; and `win-switch-set-keys'. For example:
;;
;; (win-switch-add-key "O" 'previous-window)
;; (win-switch-delete-key "p" 'previous-window)
;; (win-switch-set-keys '(" " "," "m") 'other-frame)
;;
;; Note that the last arguments here are win-switch commands not elisp
;; functions. (Note also that the emergency-exit keys do a hard exit in
;; case of an unexpected error in user-defined code such as in
;; customized feedback functions. This command may be removed in future
;; versions.) At least one exit key must always be defined. Revised
;; bindings can be set in in the hook `win-switch-load-hook' before
;; loading the package. (Also see `win-switch-define-key' for setting
;; general commands in the win-switch keymap, and
;; `win-switch-set-once-key' for setting commands in the once only
;; keymap used by `win-switch-dispatch-once'.)
;;
;; Besides key bindings, the most important customization options are
;; the following:
;;
;; + `win-switch-idle-time'
;; + `win-switch-window-threshold'
;; + `win-switch-other-window-first'
;; + `win-switch-wrap-around' (set via `win-switch-set-wrap-around')
;;
;; The idle time should be set so that one does not have to either rush
;; or wait. (While explicit exit always works, it is nice to have
;; window-switching mode end on its own at just the right time.) This
;; may require some personalized fiddling to find a comfortable value,
;; though the default should be pretty good. The window-threshold and
;; other-window-first control when and if window switching mode is
;; entered. And wrap-around determines if moving across the edge of the
;; frame wraps around to the window on the other side.
;;
;; The other customizable parameters are as follows:
;;
;; + `win-switch-provide-visual-feedback'
;; + `win-switch-feedback-background-color'
;; + `win-switch-feedback-foreground-color'
;; + `win-switch-on-feedback-function'
;; + `win-switch-off-feedback-function'
;; + `win-switch-other-window-function'
;;
;; The feedback mechanisms are intended to make it salient when
;; window switching mode is on or off and can be customized at
;; several scales. The other-window-function replaces `other-window'
;; for moving between window; the primary motivation is to allow
;; `icicle-other-window-or-frame' under icicles.
;;
;; And four hooks can be set as well:
;;
;; + `win-switch-load-hook'
;; + `win-switch-on-hook'
;; + `win-switch-off-hook'
;; + `win-switch-abort-hook'
;;
;; The following functions are used to set options:
;;
;; + `win-switch-set-wrap-around'
;; + `win-switch-add-key'
;; + `win-switch-delete-key'
;; + `win-switch-set-keys'
;;
;; There are three main entry points for using this functionality
;;
;; + `win-switch-dispatch' (alias `win-switch-mode')
;; + `win-switch-dispatch-once'
;; + `win-switch-dispatch-with'
;;
;; The first is the main function, the second is a prefix command that
;; gives one switch only but allows easy maneuvering in up to five
;; windows with a single keystroke. (The `once' keys can be set using
;; the `win-switch-set-once-keys' command.) The last constructs
;; commands for keybindings that dispatch after some other command.
;; (See `win-switch-setup-keys-arrow' for a nice example of its use.)
;;
;; NOTE: win-switch is not a formal major or minor mode, more of an
;; overriding mode. This started as a way to explore dynamic
;; keybindings, an idea that is generalized considerably in
;; my packages `quick-nav' and `power-keys'. The latter
;; introduces some programming abstractions that can be used
;; to easily install dynamic keymaps of several flavors.
;; I plan to use the `power-keys' mechanisms for this package
;; in a later version.
;;
;; Code Contents
;; 1. (@> "User-Configurable Parameters")
;; 2. (@> "User-Configurable Key Bindings")
;; 3. (@> "Preventing Default Shadowing")
;; 4. (@> "Internal Configuration Data")
;; 5. (@> "Internal Functions and Macros")
;; 6. (@> "Actions and Key Commands")
;; 7. (@> "Customization Initializers and Option Setters")
;; 8. (@> "User Entry Points")
;; 9. (@> "Pre-defined Configurations")
;;
;;; Change Log:
;;
;; * 08 Oct 2016 -- (win-switch-on-feedback, win-switch-off-feedback)
;; Speed up by changing mode-line face only on
;; selected frame, and only when needed.
;;
;; (win-switch-exit-by-timeout) Streamlined pushback
;; of exit keys onto unread-command-events, matching
;; change to win-switch-exit-and-redo in last commit.
;;
;; * 10 Feb 2015 -- Added '--' to private function names,
;; streamlined handling of unread-command-events.
;;
;; * 09 Feb 2015 -- Added abort-hook to check for no-entry conditions,
;; cleaned up timer functions and dispatch checks.
;;
;; * 08 Feb 2015 -- Minor bug and doc fixes; fixed win-switch-dispatch-once,
;; ignores errors when there are no windows to move to.
;;
;; * 02 Feb 2013 -- Fixed customization type for win-switch-window-threshold
;; Fixed comment describing L and J keys' functionality
;;
;; * 02 Nov 2012 -- Changed the last stray 'first to 'car
;;
;; * 04 Aug 2012 -- Changed a few stray first/rest's to car/cdr's.
;;
;; * 17 Mar 2012 -- Fixed *two* silly typos in fset and in a string constant.
;; in win-switch-setup-keys-arrows. The former was causing
;; load failure from package.el.
;;
;; * 17 Jan 2012 -- Removed linkd minor mode and Package-Requires header
;; because they were causing problems with loading
;; the package through package.el.
;;
;; * 18 Dec 2011 -- Fixed error in keylist management functions that
;; masked changes to keys on other lists. This
;; affected `win-switch-custom-set-keys',
;; `win-switch-add-key', and `win-switch-delete-key'.
;; Also fixed a few documentation typos.
;; Thanks to Mark Hepburn and Brett Presnell for
;; finding the error.
;;
;; * 11 Sep 2011 -- Fixed typo in `win-switch-setup-keys-arrow-ctrl'
;; and `win-switch-setup-keys-arrow-meta'.
;; Thanks to mpdflaccuesupport.
;;
;; * 27 Aug 2011 -- Fixed missing hyphen in `win-switch-authors-configuration'
;;
;; * 20 Aug 2011 -- Updated documentation, moved split and delete
;; keys after the others in the file and added function
;; to suppress them if desired. Added timer to end
;; `win-switch-off-alert' message.
;;
;; * 19 Aug 2011 -- Adjusted for Emacs shadowing default bindings
;; in sub-keymaps. Whenever `win-switch-map' is
;; set or adjusted, the default binding is added
;; if necessary to the map and sub-keymaps.
;; Also, allowed `win-switch-other-window-first'
;; to be a function to allow context sensitive
;; behavior. Added `win-switch-delete-window-keys'
;; and put delete-window in command list.
;;
;; * 05 Aug 2011 -- Fixed setting functions for the once keys,
;; added author config FYI, and minor bug fixes.
;;
;; * 02 Aug 2011 -- Added setup-key functions, win-switch-dispatch-with,
;; win-switch-dispatch-once, minor bug fixes in
;; in setters, some clode clean up.
;;
;; * 01 Aug 2011 -- Added additional hooks, adjusted window-threshold
;; handling, added add-key and delete-key setters,
;; finalized the documentation, and ran tests.
;;
;; * 30 Jul 2011 -- Added customization framework, frame switching, and
;; custom feedback and other-window functions.
;;
;; * 29 Jul 2011 -- Adjusted keymap initialization, changed behavior
;; of `win-switch-dispatch' with prefix arguments
;;
;; * 28 Jul 2011 -- Completed and tested main functionality; rediscovered
;; several times that messing with overriding-loca-map
;; can be dangerous.
;;
;;; License:
;;
;; This program 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.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;; Code:
(require 'windmove)
;; (@* "User-Configurable Parameters")
(defgroup win-switch nil
"All customization options for win-switch mode."
:prefix "win-switch-"
:group 'convenience
:link `(url-link :tag "Send Bug Report"
,(concat "mailto:" "[email protected]"
"?subject=win-switch.el bug: \
&body=Describe bug here, starting with `emacs -q'. \
Be sure to include your platform and your Emacs and win-switch versions."))
:link `(url-link :tag "Download"
"http://www.github.com/genovese/emacs-utils/win-switch")
:link `(url-link :tag "Description"
"http://www.emacsiki.org/cgi-bin/wiki/WinSwitch"))
(defgroup win-switch-keys nil
"Command key bindings for win-switch mode."
:prefix "win-switch-"
:group 'win-switch)
;;;###autoload
(defcustom win-switch-idle-time 0.75
"Cancel window switching mode when idle time exceeds this threshold.
The time is measured in seconds and can be an integer or
floating-point number."
:type 'number
:group 'win-switch)
;;;###autoload
(defcustom win-switch-window-threshold 2
"Number of windows above which dispatch always enters switching mode.
When the current frame has more than this many windows,
`win-switch-dispatch' enters window-switching mode
unconditionally; otherwise, it acts like like
`win-switch-other-window-function' (which is `other-window' by
default).
Besides its effect on window switching behavior, this option also
affects how `win-switch-dispatch' interprets its prefix argument.
See the documentation for `win-switch-dispatch' for details."
:type 'integer
:group 'win-switch)
;;;###autoload
(defcustom win-switch-other-window-first t
"Whether to move to next window before entering window switching mode.
Should be either a boolean or a boolean function that takes no arguments.
If equal to t or if a function and the function returns a non-nil value,
`win-switch-dispatch' calls `win-switch-next-window' before changing
window-switching modes."
:type '(choice boolean function)
:group 'win-switch)
;;;###autoload
(defcustom win-switch-wrap-around t
"Whether movement off the edge of the frame wraps around.
To set this variable in Lisp code, do not set the variable
directly but rather call the function
`win-switch-set-wrap-around' with argument 1 to turn wrapping
on and -1 to turn wrapping off."
:type 'boolean
:set (lambda (symbol value) (win-switch-set-wrap-around (if value 1 -1)))
:initialize (lambda (symbol value)
(setq windmove-wrap-around value)
(custom-initialize-default symbol value))
:require 'windmove
:group 'win-switch)
;;;###autoload
(defcustom win-switch-provide-visual-feedback t
"Whether to provide visual feedback during window switching mode."
:type 'boolean
:group 'win-switch)
;;;###autoload
(defcustom win-switch-feedback-background-color "red"
"Mode line background color of active window during switching mode."
:type 'string
:group 'win-switch)
;;;###autoload
(defcustom win-switch-feedback-foreground-color "white"
"Mode line foreground color of active window during switching mode."
:type 'string
:group 'win-switch)
;;;###autoload
(defcustom win-switch-on-feedback-function nil
"Function to turn on visual feedback, or nil for default behavior.
This function of zero arguments is called when entering window
switching mode, and it should set up conditions that make salient
that window switching mode is turned on. Setting this function
should usually be paired with setting
`win-switch-off-feedback-function' to ensure that what is set on
entry is unset on exit. See `win-switch-on-feedback' for the
default behavior."
:type '(choice (const :tag "Default" nil) ; nil here because other-window has distinct calling sequence
function)
:group 'win-switch)
;;;###autoload
(defcustom win-switch-off-feedback-function nil
"Function to turn off visual feedback, or nil for default behavior.
This function of zero arguments is called when exiting window
switching mode, and it should make salient that window switching
mode is turned off and clear any conditions that were set on
entry. Setting this function should usually be paired with
setting `win-switch-on-feedback-function' to ensure that what is
unset on exit had been set on entry. See the function
`win-switch-off-feedback' for the default behavior."
:type '(choice (const :tag "Default" nil) ; nil here because other-window has distinct calling sequence
function)
:group 'win-switch)
;;;###autoload
(defcustom win-switch-other-window-function nil
"Function to switch windows or nil for default, `other-window'."
:type '(choice (const :tag "Default" nil) ; nil here because other-window has distinct calling sequence
function)
:group 'win-switch)
;;;###autoload
(defvar win-switch-load-hook nil
"List of functions to be called when win-switch module is loaded.")
;;;###autoload
(defvar win-switch-on-hook nil
"List of functions to be called as window switching mode is entered.
These functions are called just before the overriding key map is set up
and before the timer is started.")
;;;###autoload
(defvar win-switch-off-hook nil
"List of functions to be called after window switching mode is exited.
These functions are called after the timer is cleared and the
overriding key map is restored")
;;;###autoload
(defvar win-switch-abort-hook nil
"List of functions that check if `win-switch-dispatch' should be aborted.
These functions are called in succession at `win-switch-dispatch'
just before entering window-switching mode and after checking the
usual conditions on the window threshold and prefix arguments. If
any function in this list returns a non-nil value,
window-switching mode is not entered. If the returned value is
'abort, then no action is taken; for any other non-nil value
`win-switch-next-window' is still called. The primary purpose of
these hooks is to allow for conditions where the persistent mode
can cause conflicts or other problems.")
;; (@* "User-Configurable Key Bindings")
;;;###autoload
(defcustom win-switch-up-keys '("i")
"List of key sequences that select the window above the current one.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'up)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-down-keys '("k")
"List of key sequences that select the window below the current one.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'down)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-left-keys '("j")
"List of key sequences that select the window left of the current one.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'left)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-right-keys '("l")
"List of key sequences that select the window left of the current one.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'right)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-next-window-keys '("o")
"List of key sequences that select the next window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'next-window)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-previous-window-keys '("p")
"List of key sequences that select the next window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'previous-window)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-enlarge-vertically-keys '("I")
"List of key sequences that vertically enlarges current window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'enlarge-vertically)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-shrink-vertically-keys '("K")
"List of key sequences that vertically shrinks current window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'shrink-vertically)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-shrink-horizontally-keys '("J")
"List of key sequences that horizontally shrinks current window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'shrink-horizontally)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-enlarge-horizontally-keys '("L")
"List of key sequences that horizontally enlarges current window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'enlarge-horizontally)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-other-frame-keys '(" ")
"List of key sequences that select the next frame.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'other-frame)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-exit-keys '("u" [return])
"List of key sequences that will exit window switching mode.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'exit)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-split-horizontally-keys '(";") ; visual mnemonic
"List of key sequences that horizontally splits current window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'split-horizontally)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-split-vertically-keys '("h") ; visual not letter mnemonic
"List of key sequences that vertically splits current window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'split-vertically)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-delete-window-keys '("0")
"List of key sequences that deletes current window.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'delete-window)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-emergency-exit-keys '("\M-\C-g")
"List of additional key sequences that will exit window switching mode.
This exits window switching without any niceties, feedback, or
hooks and so should be used only as a last resort. It is intended
only as a precaution for cases in which an unexpected
problem (e.g., in user defined hooks or function-valued options)
makes it impossible to exit window switching mode by another way.
This should not need really be necessary and may be removed in
future versions.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-keys' as follows:
(win-switch-set-keys <key-list> 'emergency-exit)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set 'win-switch-custom-set-keys
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-once-double-next-keys '("u")
"List of keys that will advance two windows in `win-switch-dispatch-once'.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-once-keys' as follows:
(win-switch-set-once-keys <key-list> 'once-double-next)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set (lambda (sym value) (win-switch-set-once-keys value 'once-double-next))
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;;;###autoload
(defcustom win-switch-once-double-prev-keys '("y")
"List of keys that will move back two windows in `win-switch-dispatch-once'.
To set this variable from Lisp code, do not just set it directly, but
rather use the function `win-switch-set-once-keys' as follows:
(win-switch-set-once-keys <key-list> 'once-double-prev)
where <key-list> is a list of key bindings."
:type '(repeat (sexp :format "%v"))
:set (lambda (sym value) (win-switch-set-once-keys value 'once-double-prev))
:initialize 'custom-initialize-default
:group 'win-switch-keys)
;; (@* "Preventing Default Shadowing")
;; Because of an issue (which I think should be considered a bug) in
;; Emacs, adding a sparse prefix key map can shadow the default binding
;; for unbound keys with the same prefix. For instance, if the user adds
;; a binding to a key involving the Meta modifier when no such binding
;; was included before, a sub-keymap is created under meta-prefix-char
;; (default escape or 27). But the default for `win-switch-map' will
;; *not* be found by the lookup process for any unbound meta key
;; sequence. This can cause problems because window-switching mode will
;; not be exited automatically in this case as it should.
;;
;; The function `win-switch--fix-keymap-defaults' adds the necessary
;; defaults in the keymap and sub-keymaps. It is used in the key setting
;; functions and in the `win-switch-map' keymap definition itself.
;;
(defun win-switch--fix-keymap-defaults (map)
"Adjust keymap MAP to include proper exit defaults.
Returns a modified version of MAP that may share some structure
with the original. In the modified map itself and in any
sub-keymaps, a default binding is added, if a default is not
already present. The default is bound to
`win-switch-exit-and-redo'. This ensures that unbound key
sequences exit from window switching mode. Without this, adding a
sparse prefix key map can shadow the default binding for unbound
keys with the same prefix."
(let ((fixed-map
(mapcar (lambda (entry)
(if (and (consp entry)
(keymapp (cdr entry))
(null (lookup-key (cdr entry) [t])))
(cons (car entry)
(win-switch--fix-keymap-defaults (cdr entry)))
entry)) map)))
(unless (lookup-key fixed-map [t])
(define-key fixed-map [t] 'win-switch-exit-and-redo))
fixed-map))
;; (@* "Internal Configuration Data")
(defvar win-switch-commands
'((win-switch-up-keys . win-switch-up)
(win-switch-down-keys . win-switch-down)
(win-switch-left-keys . win-switch-left)
(win-switch-right-keys . win-switch-right)
(win-switch-next-window-keys . win-switch-next-window)
(win-switch-previous-window-keys . win-switch-previous-window)
(win-switch-enlarge-vertically-keys . enlarge-window)
(win-switch-shrink-vertically-keys . shrink-window)
(win-switch-shrink-horizontally-keys . shrink-window-horizontally)
(win-switch-enlarge-horizontally-keys . enlarge-window-horizontally)
(win-switch-other-frame-keys . win-switch-other-frame)
(win-switch-exit-keys . win-switch-exit)
(win-switch-split-vertically-keys . split-window-vertically)
(win-switch-split-horizontally-keys . split-window-horizontally)
(win-switch-delete-window-keys . delete-window)
(win-switch-emergency-exit-keys . win-switch-emergency-exit))
"Associates pre-defined key lists to window-switching mode commands.")
(defvar win-switch-once-commands
'((win-switch-once-double-next-keys . win-switch-double-next-window)
(win-switch-once-double-prev-keys . win-switch-double-previous-window))
"Associates once-only key lists to dispatch-once commands.
See `win-switch-dispatch-once'.")
(defvar win-switch-map
(let ((map (make-sparse-keymap)))
;; must have an exit or we will regret it
(when (null win-switch-exit-keys)
(error "The exit keys list for win-switch must remain non-empty"))
;; assign specified commands
(dolist (cmdpair win-switch-commands)
(dolist (key (symbol-value (car cmdpair)))
(define-key map key (cdr cmdpair))))
;; all other keys exit and then perform their original function
;; see comment above regarding the need for the `esc' submap
(define-key map [t] 'win-switch-exit-and-redo)
;; fix up the defaults
(win-switch--fix-keymap-defaults map))
"Keymap that is active during window switching mode.
The functions `win-switch-set-keys', `win-switch-add-key', and
`win-switch-delete-key', can be used to set parts of this keymap
corresponding to the available commands. To add an arbitrary
command to this keymap, use `win-switch-define-key' rather than
changing this variable directly. If you do change this keymap
directly, using `define-key' for instance, be very careful to
leave an exit key available")
(defvar win-switch-once-map
(let ((map (make-sparse-keymap "Window Switching")))
(dolist (cmdpair (append win-switch-commands win-switch-once-commands nil))
(let ((keysym (car cmdpair))
(cmd (cdr cmdpair)))
(dolist (key (symbol-value keysym))
(unless (or (eq keysym 'win-switch-exit-keys)
(eq keysym 'win-switch-emergency-exit-keys))
(define-key map key cmd)))))
map)
"Keymap referenced by `win-switch-dispatch-once' to make simple moves.
Commands using this keymap do *not* enter window-switching mode, so
no exit keys or commands are required (or helpful). It is safe
to assign directly to this keymap. See `win-switch-dispatch-once.'")
(defvar win-switch-timer nil
"When non-nil, measures the idle time until window switching mode expires.")
(defvar win-switch-engaged nil
"Non-nil when window switching mode is on, nil otherwise.")
(defvar win-switch-overriding-map-stack nil
"Stack to hold saved values of `overridiing-local-map'.")
(defvar win-switch-saved-mode-line-faces nil
"Holds cons with previous mode-line background and foreground.")
(defvar win-switch-visited-frames-list nil
"Holds list of frames visited during a single window-switching interval.")
;; (@* "Internal Functions and Macros")
(defun win-switch--start-timer (secs func)
"Run for SECS seconds before excecuting function FUNC, if SECS is non-nil.
Uses `win-switch-timer', if valid, canceling it before restarting.
If non-nil, SECS should be a number or time; FUNC should be a
symbol or function."
(when secs
(when (and win-switch-timer (timerp win-switch-timer))
(cancel-timer win-switch-timer))
(setq win-switch-timer (run-with-idle-timer secs nil func))))
(defun win-switch--clear-timer ()
"Cancel and nullify `win-switch-timer', if valid."
(when win-switch-timer
(when (timerp win-switch-timer)
(cancel-timer win-switch-timer))
(setq win-switch-timer nil)))
(defmacro win-switch--override-map (map)
"Save keymap bound to symbol MAP and set MAP to `win-switch-map'."
`(progn
(when (and ,map
(not (eq ,map (car win-switch-overriding-map-stack))))
(push ,map win-switch-overriding-map-stack))
(setq ,map win-switch-map)))
(defmacro win-switch--restore-map (map)
"Reset symbol MAP's value to most recently saved keymap."
`(setq ,map (pop win-switch-overriding-map-stack)))
(defun win-switch--enough-windows-p (&optional maybe-frame)
"Are there enough windows on MAYBE-FRAME to enter window-switching mode?
Frame defaults to the selected frame if MAYBE-FRAME is nil."
(let* ((frame (or maybe-frame (selected-frame))))
(or (<= win-switch-window-threshold 0)
(nthcdr win-switch-window-threshold (window-list frame)))))
;; (@* "Actions and Key Commands")
(defun win-switch-up (&optional arg)
(interactive "P")
(ignore-errors
(windmove-do-window-select 'up arg)))
(defun win-switch-down (&optional arg)
(interactive "P")
(ignore-errors
(windmove-do-window-select 'down arg)))
(defun win-switch-left (&optional arg)
(interactive "P")
(ignore-errors
(windmove-do-window-select 'left arg)))
(defun win-switch-right (&optional arg)
(interactive "P")
(ignore-errors
(windmove-do-window-select 'right arg)))
(defun win-switch-next-window (arg &optional interactive?)
"Move to next window in window list.
ARG is in raw prefix argument format, and INTERACTIVE?
is non-nil if the function was called interactively.
When `win-switch-other-window-function' is non-nil,
call that function, either interactively when INTERACTIVE?
is non-nil or passing arg otherwise. When it is nil,
calls `other-window' with the numeric value of ARG.
This is a wrapper that allows the user to override
the standard window switching behavior, for instance
when using icicles."
(interactive "P\np")
(if win-switch-other-window-function
(if interactive?
(call-interactively 'win-switch-other-window-function)
(funcall win-switch-other-window-function arg))
(other-window (prefix-numeric-value arg))))
(defun win-switch-previous-window (arg &optional interactive?)
"Move to previous window in window list."
(interactive "p\np")
(win-switch-next-window (- arg) interactive?))
(defun win-switch-double-next-window ()
"Advance two windows in window list."
(interactive)
(win-switch-next-window 2))
(defun win-switch-double-previous-window ()
"Move to second previous window in window list."
(interactive)
(win-switch-next-window -2))
;; ATTN: This must be cleaned up. See comment below at win-switch-on-feedback
(defun win-switch-other-frame (&optional arg)
"Select the ARGth different visible frame on current display, and raise it.
Like `other-frame' but correctly cleans up visual feedback settings."
(interactive "p")
(when (and win-switch-provide-visual-feedback
(not win-switch-off-feedback-function))
(win-switch-off-feedback-mode-line))
(other-frame arg)
(when (and win-switch-provide-visual-feedback
(not win-switch-on-feedback-function))
(win-switch-on-feedback-mode-line)))
(defun win-switch-on-alert ()
"Alert users, usually in echo area, that window switching is on."
(message "Window Switching Mode On..."))
(defun win-switch-off-alert ()
"Alert users, usually in echo area, that window switching is off."
(message "Window Switching Mode Off.")
(run-with-timer 0.5 nil (lambda () (message nil))))
;; ATTN: Issue -- CRG 2016-10-09
;; The change to the mode-line feedback in each frame as of v1.1.3
;; gave noticeably improved responsiveness. In fact, with many frames,
;; changing the mode-line face in all frames at the beginning of window
;; switching was noticeably and annoyingly lagged.
;;
;; However, setting the background in each frame makes the coordination
;; difficult, as we don't want to leave any frames with the mode-line
;; changed after win-switching exits. In particular, setting it in each
;; frame required that we replace `other-frame' by
;; `win-switch-other-frame', which handls the mode-line changes if
;; required when a new frame is visited. This is leaky and not DRY, and
;; it requires decoupling the mode-line feedback change from the state
;; changs on beginning and end. It is clear now that the feedback
;; function should be a parameter with a default setting and pre- and