-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
elscreen.el
1600 lines (1442 loc) · 63.4 KB
/
elscreen.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
;;; elscreen.el --- Emacs window session manager
;; Author: Naoto Morishima <[email protected]>
;; Based on: screens.el
;; by Heikki T. Suopanki <[email protected]>
;; Created: June 22, 1996
;; Revised: April 11, 2012 by Emanuel Evans
;; Maintainer: Akinori MUSHA <[email protected]>
;; Homepage: https://github.com/knu/elscreen
;; Version: 20180321
;; Package-Requires: ((emacs "24"))
;; Keywords: window, convenience
;; This file is NOT part of GNU Emacs.
;;; 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 2, 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, 675 Mass Ave, Cambridge, MA 02139,
;; USA.
;;; Commentary:
;; This is a fork of ElScreen designed to be installed with
;; package.el. It does not require APEL (it also probably does not
;; work with XEmacs). In order to start using ElScreen, add
;; (elscreen-start) to your emacs configuration. For help on how to
;; use elscreen, try \C-z ?.
;;; Code:
;;; User Customizable Variables:
(require 'dired)
(declare-function iswitchb-read-buffer "iswitchb")
(defconst elscreen-version "20180321")
(defgroup elscreen nil
"ElScreen -- Screen Manager for Emacs"
:tag "ElScreen"
:group 'environment)
(defcustom elscreen-prefix-key "\C-z"
"*Prefix key for ElScreen commands."
:tag "Prefix Key of ElScreen"
:type '(string :size 10)
:set (lambda (symbol value)
(when (boundp 'elscreen-map)
(elscreen-set-prefix-key value))
(custom-set-default symbol value))
:group 'elscreen)
(defcustom elscreen-default-buffer-name "*scratch*"
"*Name of a buffer in new screen."
:tag "Name of Default Buffer"
:type '(string :size 24)
:group 'elscreen)
(defcustom elscreen-default-buffer-initial-major-mode initial-major-mode
"*Major mode command symbol to use for the default buffer."
:tag "Major Mode for Default Buffer"
:type 'function
:group 'elscreen)
(defcustom elscreen-default-buffer-initial-message initial-scratch-message
"*Initial message displayed in default buffer.
If this is nil, no message will be displayed."
:tag "Message to Display in the Default Buffer"
:type '(choice (text :tag "Message")
(const :tag "none" nil))
:group 'elscreen)
(defcustom elscreen-mode-to-nickname-alist
'(("^dired-mode$" .
(lambda ()
(format "Dired(%s)" dired-directory)))
("^Info-mode$" .
(lambda ()
(format "Info(%s)" (file-name-nondirectory Info-current-file))))
("^mew-draft-mode$" .
(lambda ()
(format "Mew(%s)" (buffer-name (current-buffer)))))
("^mew-" . "Mew")
("^irchat-" . "IRChat")
("^liece-" . "Liece")
("^lookup-" . "Lookup"))
"*Alist composed of the pair of name of major-mode and corresponding screen-name."
:tag "Alist to Derive Screen Names from Major Modes"
:type '(alist :key-type string :value-type (choice string function))
:set (lambda (symbol value)
(custom-set-default symbol value)
(when (fboundp 'elscreen-rebuild-mode-to-nickname-alist)
(elscreen-rebuild-mode-to-nickname-alist)))
:group 'elscreen)
(defcustom elscreen-buffer-to-nickname-alist
'(("[Ss]hell" . "shell")
("compilation" . "compile")
("-telnet" . "telnet")
("dict" . "OnlineDict")
("*WL:Message*" . "Wanderlust"))
"*Alist composed of the pair of regular expression of
buffer-name and corresponding screen-name."
:tag "Alist to Derive Screen Names from Major Modes"
:type '(alist :key-type string :value-type (choice string function))
:set (lambda (symbol value)
(custom-set-default symbol value)
(when (fboundp 'elscreen-rebuild-buffer-to-nickname-alist)
(elscreen-rebuild-buffer-to-nickname-alist)))
:group 'elscreen)
(defcustom elscreen-display-screen-number t
"*Non-nil to display the number of current screen in the mode line."
:tag "Show/Hide Screen Number on the mode-line"
:type 'boolean
:group 'elscreen)
(defcustom elscreen-display-tab t
"*Specify how the tabs at the top of frame should be displayed.
t means to display tabs whose width should be calculated automatically.
A value of integer means to display tabs with fixed width of this value.
nil means don't display tabs."
:tag "Specify how the tabs at the top of frame should be displayed"
:type '(choice (const :tag "Show (automatic width tab)" t)
(integer :tag "Show (fixed width tab)" :size 4 :value 16)
(const :tag "Hide" nil))
:set (lambda (symbol value)
(when (or (booleanp value)
(and (numberp value)
(> value 0)))
(custom-set-default symbol value)
(when (fboundp 'elscreen-tab-update)
(elscreen-tab-update t))))
:group 'elscreen)
(make-obsolete-variable 'elscreen-tab-display-create-screen
'elscreen-tab-display-control "2012-04-11")
(defcustom elscreen-tab-display-control t
"*Non-nil to display control tab at the most left side."
:tag "Show/Hide the Control Tab"
:type 'boolean
:set (lambda (symbol value)
(custom-set-default symbol value)
(when (fboundp 'elscreen-tab-update)
(elscreen-tab-update t)))
:group 'elscreen)
(defcustom elscreen-tab-display-kill-screen 'left
"*Location of the icons to kill a screen on each tab. Possible values are 'left, 'right, or nil (to hide them)."
:tag "Location of Buttons to Kill Screen on Each Tab"
:type '(choice (const :tag "Left" left)
(const :tag "Right" right)
(const :tag "None" nil))
:set (lambda (symbol value)
(custom-set-default symbol value)
(when (fboundp 'elscreen-tab-update)
(elscreen-tab-update t)))
:group 'elscreen)
(defface elscreen-tab-background-face
'((((type x w32 mac ns) (class color))
:background "Gray50")
(((class color))
(:background "black")))
"Face to fontify background of tab line."
:group 'elscreen)
(defface elscreen-tab-control-face
'((((type x w32 mac ns) (class color))
(:background "white" :foreground "black" :underline "Gray50"))
(((class color))
(:background "white" :foreground "black" :underline t))
(t (:underline t)))
"Face for control tab."
:group 'elscreen)
(defface elscreen-tab-current-screen-face
'((((class color))
(:background "white" :foreground "black"))
(t (:underline t)))
"Face for current screen tab."
:group 'elscreen)
(defface elscreen-tab-other-screen-face
'((((type x w32 mac ns) (class color))
:background "Gray85" :foreground "Gray50")
(((class color))
(:background "blue" :foreground "black" :underline t)))
"Face for tabs other than current screen one."
:group 'elscreen)
;;; Key & Menu bindings:
(defvar elscreen-map (make-sparse-keymap)
"Keymap for ElScreen.")
(define-key elscreen-map "\C-c" 'elscreen-create)
(define-key elscreen-map "c" 'elscreen-create)
(define-key elscreen-map "C" 'elscreen-clone)
(define-key elscreen-map "\C-k" 'elscreen-kill)
(define-key elscreen-map "k" 'elscreen-kill)
(define-key elscreen-map "\M-k" 'elscreen-kill-screen-and-buffers)
(define-key elscreen-map "K" 'elscreen-kill-others)
(define-key elscreen-map "\C-p" 'elscreen-previous)
(define-key elscreen-map "p" 'elscreen-previous)
(define-key elscreen-map "\C-n" 'elscreen-next)
(define-key elscreen-map "n" 'elscreen-next)
(define-key elscreen-map "\C-a" 'elscreen-toggle)
(define-key elscreen-map "a" 'elscreen-toggle)
(define-key elscreen-map "'" 'elscreen-goto)
(define-key elscreen-map "\"" 'elscreen-select-and-goto)
(define-key elscreen-map "0" 'elscreen-jump-0)
(define-key elscreen-map "1" 'elscreen-jump)
(define-key elscreen-map "2" 'elscreen-jump)
(define-key elscreen-map "3" 'elscreen-jump)
(define-key elscreen-map "4" 'elscreen-jump)
(define-key elscreen-map "5" 'elscreen-jump)
(define-key elscreen-map "6" 'elscreen-jump)
(define-key elscreen-map "7" 'elscreen-jump)
(define-key elscreen-map "8" 'elscreen-jump)
(define-key elscreen-map "9" 'elscreen-jump-9)
(define-key elscreen-map "\C-s" 'elscreen-swap)
(define-key elscreen-map "\C-w" 'elscreen-display-screen-name-list)
(define-key elscreen-map "w" 'elscreen-display-screen-name-list)
(define-key elscreen-map "\C-m" 'elscreen-display-last-message)
(define-key elscreen-map "m" 'elscreen-display-last-message)
(define-key elscreen-map "\C-t" 'elscreen-display-time)
(define-key elscreen-map "t" 'elscreen-display-time)
(define-key elscreen-map "A" 'elscreen-screen-nickname)
(define-key elscreen-map "b" 'elscreen-find-and-goto-by-buffer)
(define-key elscreen-map "\C-f" 'elscreen-find-file)
(define-key elscreen-map "\C-r" 'elscreen-find-file-read-only)
(define-key elscreen-map "d" 'elscreen-dired)
(define-key elscreen-map "\M-x" 'elscreen-execute-extended-command)
(define-key elscreen-map "i" 'elscreen-toggle-display-screen-number)
(define-key elscreen-map "T" 'elscreen-toggle-display-tab)
(define-key elscreen-map "?" 'elscreen-help)
(define-key elscreen-map "v" 'elscreen-display-version)
(define-key elscreen-map "j" 'elscreen-link)
(define-key elscreen-map "s" 'elscreen-split)
(defun elscreen-set-prefix-key (prefix-key)
(when (not (eq elscreen-prefix-key prefix-key))
(when elscreen-prefix-key
(global-set-key elscreen-prefix-key
(get 'elscreen-prefix-key
'global-map-original-definition))
(define-key minibuffer-local-map elscreen-prefix-key
(get 'elscreen-prefix-key 'minibuffer-local-map-original-definition)))
(put 'elscreen-prefix-key 'global-map-original-definition
(lookup-key global-map prefix-key))
(put 'elscreen-prefix-key 'minibuffer-local-map-original-definition
(lookup-key minibuffer-local-map prefix-key)))
(global-set-key prefix-key elscreen-map)
(define-key minibuffer-local-map prefix-key 'undefined)
(setq elscreen-prefix-key prefix-key))
(defvar elscreen-help "ElScreen keys:
\\[elscreen-create] Create a new screen and switch to it
\\[elscreen-clone] Create a new screen with the window-configuration of current screen
\\[elscreen-kill] Kill current screen
\\[elscreen-kill-screen-and-buffers] Kill current screen and buffers
\\[elscreen-kill-others] Kill other screens
\\[elscreen-next] Switch to the \"next\" screen in a cyclic order
\\[elscreen-previous] Switch to the \"previous\" screen in a cyclic order
\\[elscreen-toggle] Toggle to the screen selected previously
\\[elscreen-select-and-goto] Jump to the specified screen
\\[elscreen-jump-0]
: Jump to the screen #
\\[elscreen-jump-9]
\\[elscreen-swap] Swap current screen with previous one
\\[elscreen-display-screen-name-list] Show list of screens
\\[elscreen-screen-nickname] Name current screen
\\[elscreen-display-last-message] Show last message
\\[elscreen-display-time] Show time
\\[elscreen-find-and-goto-by-buffer] Switch to the screen in which specified buffer is displayed
\\[elscreen-find-file] Create new screen and open file
\\[elscreen-find-file-read-only] Create new screen and open file but don't allow changes
\\[elscreen-dired] Create new screen and run dired
\\[elscreen-execute-extended-command] Read function name, then call it with new screen
\\[elscreen-toggle-display-screen-number] Show/hide the screen number in the mode line
\\[elscreen-toggle-display-tab] Show/hide the tab at the top of screen
\\[elscreen-display-version] Show ElScreen version
\\[elscreen-help] Show this help"
"Help shown by elscreen-help-mode")
;;; alist utility functions (taken from APEL):
(defun elscreen--put-alist (key value alist)
"Set cdr of an element (KEY . ...) in ALIST to VALUE and return ALIST.
If there is no such element, create a new pair (KEY . VALUE) and
return a new alist whose car is the new pair and cdr is ALIST."
(let ((elm (assoc key alist)))
(if elm
(progn
(setcdr elm value)
alist)
(cons (cons key value) alist))))
(defun elscreen--set-alist (symbol key value)
"Set cdr of an element (KEY . ...) in the alist bound to SYMBOL to VALUE."
(or (boundp symbol)
(set symbol nil))
(set symbol (elscreen--put-alist key value (symbol-value symbol))))
(defun elscreen--del-alist (key alist)
"Delete an element whose car equals KEY from ALIST.
Return the modified ALIST."
(let ((pair (assoc key alist)))
(if pair
(delq pair alist)
alist)))
(defun elscreen-window-history-supported-p ()
(and (fboundp 'window-prev-buffers)
(fboundp 'window-next-buffers)
(fboundp 'set-window-prev-buffers)
(fboundp 'set-window-next-buffers)))
(defun elscreen-get-all-window-history-alist ()
(when (elscreen-window-history-supported-p)
(mapcar (lambda (window)
(let ((prevs (window-prev-buffers window))
(nexts (window-next-buffers window)))
(cons window (cons prevs nexts))))
(window-list))))
(defun elscreen-restore-all-window-history-alist (history-alist)
(when (elscreen-window-history-supported-p)
(mapc (lambda (entry)
(let* ((window (car entry))
(histories (cdr entry))
(prevs (car histories))
(nexts (cdr histories)))
(when (window-valid-p window)
(set-window-prev-buffers window prevs)
(set-window-next-buffers window nexts))))
history-alist)))
(defun elscreen--remove-alist (symbol key)
"Delete an element whose car equals KEY from the alist bound to SYMBOL."
(and (boundp symbol)
(set symbol (elscreen--del-alist key (symbol-value symbol)))))
;;; Internal Functions:
(defvar elscreen-frame-confs nil
"Alist that contains the information about screen configurations.")
(defun elscreen-current-window-configuration ()
(list (current-window-configuration) (point-marker)))
(defun elscreen-default-window-configuration ()
(let ((default-buffer (get-buffer elscreen-default-buffer-name)))
(save-window-excursion
(set-window-dedicated-p (selected-window) nil)
(delete-other-windows)
(if default-buffer
(switch-to-buffer default-buffer)
(switch-to-buffer (get-buffer-create elscreen-default-buffer-name))
(funcall elscreen-default-buffer-initial-major-mode)
(insert elscreen-default-buffer-initial-message)
(set-buffer-modified-p nil))
(elscreen-current-window-configuration))))
(defun elscreen-apply-window-configuration (elscreen-window-configuration)
(let ((window-configuration (car elscreen-window-configuration))
(marker (cadr elscreen-window-configuration)))
(set-window-configuration window-configuration)
(when (marker-buffer marker)
(goto-char marker))))
(defsubst elscreen-copy-tree (tree)
(if (fboundp 'copy-tree)
(copy-tree tree)
(elscreen-copy-tree-1 tree)))
(defun elscreen-copy-tree-1 (tree)
(let (clone)
(while (consp tree)
(setq clone (cons (or (and (consp (car tree))
(elscreen-copy-tree-1 (car tree)))
(car tree))
clone))
(setq tree (cdr tree)))
(nconc (nreverse clone) tree)))
(defmacro elscreen-save-screen-excursion (&rest body)
"Execute BODY, preserving ElScreen meta data.
Return the value of the last form in BODY."
`(let ((original-buffer-list (buffer-list))
(original-buffer-live-p nil)
(original-elscreen-window-configuration
(elscreen-current-window-configuration))
(original-frame-confs (elscreen-copy-tree elscreen-frame-confs))
(original-window-histories (elscreen-get-all-window-history-alist)))
(unwind-protect
(save-window-excursion ,@body)
(setq elscreen-frame-confs original-frame-confs)
(elscreen-apply-window-configuration
original-elscreen-window-configuration)
(mapc
(lambda (buffer)
(when (buffer-live-p buffer)
(bury-buffer buffer)
(setq original-buffer-live-p t)))
original-buffer-list)
(when original-buffer-live-p
(while (not (memq (car (buffer-list)) original-buffer-list))
(bury-buffer (car (buffer-list)))))
(elscreen-restore-all-window-history-alist original-window-histories))))
(defsubst elscreen-get-frame-confs (frame)
(assoc-default frame elscreen-frame-confs))
(defun elscreen-make-frame-confs (frame &optional keep-window-configuration)
(when (null (elscreen-get-frame-confs frame))
(let ((selected-frame (selected-frame))
elscreen-window-configuration)
(save-current-buffer
(select-frame frame)
(setq elscreen-window-configuration
(if keep-window-configuration
(elscreen-current-window-configuration)
(elscreen-default-window-configuration)))
(elscreen--set-alist 'elscreen-frame-confs frame
(list
(cons 'screen-property
(list
(cons 0 (list
(cons 'window-configuration
elscreen-window-configuration)))))
(cons 'screen-history (list 0))
(cons 'modified-inquirer nil)
(cons 'screen-to-name-alist-cache nil)))
(elscreen-apply-window-configuration elscreen-window-configuration)
(elscreen-notify-screen-modification 'force-immediately)
(select-frame selected-frame)))))
(defun elscreen-delete-frame-confs (frame)
(elscreen--remove-alist 'elscreen-frame-confs frame))
(add-hook 'after-make-frame-functions 'elscreen-make-frame-confs)
(add-hook 'delete-frame-functions 'elscreen-delete-frame-confs)
(defsubst elscreen-get-conf-list (type)
(assoc-default type (elscreen-get-frame-confs (selected-frame))))
(defsubst elscreen-set-conf-list (type conf-list)
(let ((frame-conf (elscreen-get-frame-confs (selected-frame))))
(elscreen--set-alist 'frame-conf type conf-list)))
(defun elscreen-get-screen-property (screen)
(let ((screen-property-list (elscreen-get-conf-list 'screen-property)))
(assoc-default screen screen-property-list)))
(defun elscreen-set-screen-property (screen property)
(let ((screen-property-list (elscreen-get-conf-list 'screen-property)))
(elscreen--set-alist 'screen-property-list screen property)
(elscreen-set-conf-list 'screen-property screen-property-list)))
(defun elscreen-delete-screen-property (screen)
(let ((screen-property-list (elscreen-get-conf-list 'screen-property)))
(elscreen--remove-alist 'screen-property-list screen)
(elscreen-set-conf-list 'screen-property screen-property-list)))
(defun elscreen-get-number-of-screens ()
"Return total number of screens."
(length (elscreen-get-conf-list 'screen-property)))
(defun elscreen-one-screen-p ()
"Return t if there is only one screen."
(= (elscreen-get-number-of-screens) 1))
(defun elscreen-get-screen-list ()
"Return a list of screen numbers."
(mapcar 'car (elscreen-get-conf-list 'screen-property)))
(defun elscreen-screen-live-p (screen)
"Return t when SCREEN is alive."
(not (null (elscreen-get-screen-property screen))))
(defun elscreen-get-window-configuration (screen)
"Return pair of window-configuration and marker of SCREEN
from `elscreen-frame-confs', a cons cell."
(let ((screen-property (elscreen-get-screen-property screen)))
(assoc-default 'window-configuration screen-property)))
(defun elscreen-set-window-configuration (screen winconf)
"Set pair of window-configuration and marker of SCREEN to WINCONF."
(let ((screen-property (elscreen-get-screen-property screen)))
(elscreen--set-alist 'screen-property 'window-configuration winconf)
(elscreen-set-screen-property screen screen-property)))
(defun elscreen-get-screen-nickname (screen)
"Return nickname of SCREEN from `elscreen-frame-confs', a string."
(let ((screen-property (elscreen-get-screen-property screen)))
(assoc-default 'nickname screen-property)))
(defun elscreen-set-screen-nickname (screen nickname)
"Set nickname of SCREEN to NICKNAME."
(let ((screen-property (elscreen-get-screen-property screen)))
(elscreen--set-alist 'screen-property 'nickname nickname)
(elscreen-set-screen-property screen screen-property)))
(defun elscreen-delete-screen-nickname (screen)
"Remove nickname of SCREEN from `elscreen-frame-confs'."
(let ((screen-property (elscreen-get-screen-property screen)))
(elscreen--remove-alist 'screen-property 'nickname)
(elscreen-set-screen-property screen screen-property)))
(defun elscreen-append-screen-to-history (screen)
(let ((screen-history (elscreen-get-conf-list 'screen-history)))
(setcdr (last screen-history) (list screen))))
(defun elscreen-delete-screen-from-history (screen)
(let ((screen-history (elscreen-get-conf-list 'screen-history)))
(setq screen-history (delq screen screen-history))
(elscreen-set-conf-list 'screen-history screen-history)))
(defun elscreen-set-current-screen (screen)
(let ((screen-history (elscreen-get-conf-list 'screen-history)))
(setq screen-history (cons screen (delq screen screen-history)))
(elscreen-set-conf-list 'screen-history screen-history)))
(defun elscreen-get-current-screen ()
(car (elscreen-get-conf-list 'screen-history)))
(defun elscreen-get-previous-screen ()
(cadr (elscreen-get-conf-list 'screen-history)))
(defun elscreen-status-label (screen &optional default)
(let ((default (or default " "))
(current-screen (elscreen-get-current-screen))
(previous-screen (elscreen-get-previous-screen)))
(cond
((eq screen current-screen) "+")
((eq screen previous-screen) "-")
(t default))))
(defvar elscreen-notify-screen-modification-suppress-flag nil)
(defmacro elscreen-notify-screen-modification-suppress (&rest body)
`(let ((elscreen-notify-screen-modification-suppress-flag t))
,@body))
(defvar elscreen-screen-update-hook nil)
(defun elscreen-run-screen-update-hook ()
(when elscreen-frame-confs
(elscreen-notify-screen-modification-suppress
(run-hooks 'elscreen-screen-update-hook)))
(remove-hook 'post-command-hook 'elscreen-run-screen-update-hook))
(defun elscreen-screen-modified-p (inquirer)
(let* ((inquirer-list (elscreen-get-conf-list 'modified-inquirer))
(modified (null (memq inquirer inquirer-list))))
(add-to-list 'inquirer-list inquirer)
(elscreen-set-conf-list 'modified-inquirer inquirer-list)
modified))
(defun elscreen-set-screen-modified ()
(elscreen-set-conf-list 'modified-inquirer nil)
(add-hook 'post-command-hook 'elscreen-run-screen-update-hook))
(defvar elscreen-screen-modified-hook-pwc nil)
(defun elscreen-notify-screen-modification (&optional mode)
(when (and (not (window-minibuffer-p))
(not elscreen-notify-screen-modification-suppress-flag)
(or (eq mode 'force)
(eq mode 'force-immediately)
(null elscreen-screen-modified-hook-pwc)
(not (fboundp 'compare-window-configurations))
(not (compare-window-configurations
(current-window-configuration)
elscreen-screen-modified-hook-pwc))))
(setq elscreen-screen-modified-hook-pwc
(current-window-configuration))
(elscreen-set-screen-modified)
(when (eq mode 'force-immediately)
(elscreen-run-screen-update-hook))))
(defmacro elscreen-screen-modified-hook-setup (&rest hooks-and-functions)
(cons
'progn
(mapcar
(lambda (hook-or-function)
(let ((mode ''normal))
(when (listp hook-or-function)
(setq mode (nth 1 hook-or-function))
(setq hook-or-function (nth 0 hook-or-function)))
(cond
((string-match "-\\(hooks?\\|functions\\)$"
(symbol-name hook-or-function))
`(add-hook (quote ,hook-or-function)
(lambda (&rest ignore)
(elscreen-notify-screen-modification ,mode))))
(t ;; Assume hook-or-function is function
`(defadvice ,hook-or-function (around
elscreen-screen-modified-advice
activate)
ad-do-it
(elscreen-notify-screen-modification ,mode))))))
hooks-and-functions)))
(elscreen-screen-modified-hook-setup
(recenter 'force) (change-major-mode-hook 'force)
other-window
window-configuration-change-hook window-size-change-functions
(handle-switch-frame 'force) ;; GNU Emacs 21
(delete-frame 'force)
(Info-find-node-2 'force))
(defun elscreen-get-screen-to-name-alist-cache ()
(elscreen-get-conf-list 'screen-to-name-alist-cache))
(defun elscreen-set-screen-to-name-alist-cache (alist)
(elscreen-set-conf-list 'screen-to-name-alist-cache alist))
(defvar elscreen-mode-to-nickname-alist-symbol-list nil)
(defvar elscreen-mode-to-nickname-alist-internal nil)
(defun elscreen-rebuild-mode-to-nickname-alist ()
(setq elscreen-mode-to-nickname-alist-internal
(apply 'append
(mapcar 'symbol-value
elscreen-mode-to-nickname-alist-symbol-list)))
(elscreen-notify-screen-modification 'force-immediately))
(defun elscreen-set-mode-to-nickname-alist (mode-to-nickname-alist-symbol)
(add-to-list 'elscreen-mode-to-nickname-alist-symbol-list
mode-to-nickname-alist-symbol)
(elscreen-rebuild-mode-to-nickname-alist))
(elscreen-set-mode-to-nickname-alist 'elscreen-mode-to-nickname-alist)
(defvar elscreen-buffer-to-nickname-alist-symbol-list nil)
(defvar elscreen-buffer-to-nickname-alist-internal nil)
(defun elscreen-rebuild-buffer-to-nickname-alist ()
(setq elscreen-buffer-to-nickname-alist-internal
(apply 'append
(mapcar 'symbol-value
elscreen-buffer-to-nickname-alist-symbol-list)))
(elscreen-notify-screen-modification 'force-immediately))
(defun elscreen-set-buffer-to-nickname-alist (buffer-to-nickname-alist-symbol)
(add-to-list 'elscreen-buffer-to-nickname-alist-symbol-list
buffer-to-nickname-alist-symbol)
(elscreen-rebuild-buffer-to-nickname-alist))
(elscreen-set-buffer-to-nickname-alist 'elscreen-buffer-to-nickname-alist)
(defsubst elscreen-get-alist-to-nickname (alist op mode-or-buffer-name)
(catch 'found
(progn
(mapc
(lambda (map)
(let ((nickname nil)
(condition-data (car map))
(string-or-function (cdr map)))
(when (funcall op condition-data mode-or-buffer-name)
(cond
((functionp string-or-function)
(setq nickname
(condition-case nil
(funcall string-or-function)
(wrong-number-of-arguments
(funcall string-or-function (current-buffer))))))
(t
(setq nickname string-or-function)))
(throw 'found (cons 'nickname nickname)))))
alist)
nil)))
(defun elscreen-get-screen-to-name-alist (&optional truncate-length padding)
(when (elscreen-screen-modified-p 'elscreen-get-screen-to-name-alist)
(elscreen-notify-screen-modification-suppress
(elscreen-set-window-configuration (elscreen-get-current-screen)
(elscreen-current-window-configuration))
(let* ((screen-list (sort (elscreen-get-screen-list) '<))
screen-name screen-to-name-alist nickname-type-map)
(elscreen-save-screen-excursion
(mapc
(lambda (screen)
;; If nickname exists, use it.
(setq screen-name (elscreen-get-screen-nickname screen))
;; Nickname does not exist, so examine major-mode and buffer-name.
(when (null screen-name)
(elscreen-goto-internal screen)
(setq nickname-type-map
(mapcar
(lambda (window)
(with-current-buffer (window-buffer window)
(or (elscreen-get-alist-to-nickname
elscreen-mode-to-nickname-alist-internal
'string-match (symbol-name major-mode))
(elscreen-get-alist-to-nickname
elscreen-buffer-to-nickname-alist-internal
'string-match (buffer-name))
(cons 'buffer-name (buffer-name)))))
(window-list)))
(let (nickname-list)
(while (> (length nickname-type-map) 0)
(let ((type (caar nickname-type-map))
(name (cdar nickname-type-map)))
(when name
(setq nickname-list (cons name nickname-list)))
(setq nickname-type-map
(if (eq type 'nickname)
(delete (car nickname-type-map) nickname-type-map)
(cdr nickname-type-map)))))
(setq screen-name
(mapconcat 'identity (reverse nickname-list) ":"))))
(elscreen--set-alist 'screen-to-name-alist screen screen-name))
screen-list))
(elscreen-set-screen-to-name-alist-cache screen-to-name-alist))))
;; Arguments of truncate-length and padding are deprecated.
(if truncate-length
(let ((screen-to-name-alist
(copy-alist (elscreen-get-screen-to-name-alist-cache))))
(elscreen-message "Arguments for `elscreen-get-screen-to-name-alist' are deprecated. Use elscreen-truncate-screen-name for each screen-name.")
(mapc
(lambda (screen-to-name)
(setcdr screen-to-name
(elscreen-truncate-screen-name (cdr screen-to-name)
truncate-length padding)))
screen-to-name-alist)
screen-to-name-alist)
(elscreen-get-screen-to-name-alist-cache)))
(defun elscreen-truncate-screen-name (screen-name truncate-length &optional padding)
(let ((truncate-length (max truncate-length 4)))
(cond
((> (string-width screen-name) truncate-length)
(concat (truncate-string-to-width screen-name (- truncate-length 3)
nil ?.)
"..."))
(padding
(truncate-string-to-width screen-name truncate-length nil ?\ ))
(t
screen-name))))
(defun elscreen-goto-internal (screen)
"Set the configuration of windows, buffers and markers previousuly
stored as SCREEN."
(let ((elscreen-window-configuration
(elscreen-get-window-configuration screen)))
(elscreen-apply-window-configuration elscreen-window-configuration)))
(defvar elscreen-create-hook nil)
(defun elscreen-create-internal (&optional noerror)
"Create a new screen.
If NOERROR is not nil, no message is displayed in mini buffer
when error is occurred."
(cond
((>= (elscreen-get-number-of-screens) 10)
(unless noerror
(elscreen-message "No more screens."))
nil)
(t
(let ((screen-list (sort (elscreen-get-screen-list) '<))
(screen 0))
(elscreen-set-window-configuration
(elscreen-get-current-screen)
(elscreen-current-window-configuration))
(while (eq (nth screen screen-list) screen)
(setq screen (+ screen 1)))
(elscreen-set-window-configuration
screen (elscreen-default-window-configuration))
(elscreen-append-screen-to-history screen)
(elscreen-notify-screen-modification 'force)
(run-hooks 'elscreen-create-hook)
screen))))
(defun elscreen-kill-internal (screen)
(elscreen-delete-screen-property screen)
(elscreen-delete-screen-from-history screen)
screen)
(defun elscreen-find-screens (condition)
(let ((screen-list (sort (elscreen-get-screen-list) '<))
result)
(save-current-buffer
(elscreen-set-window-configuration
(elscreen-get-current-screen)
(elscreen-current-window-configuration))
(elscreen-notify-screen-modification-suppress
(elscreen-save-screen-excursion
(mapc
(lambda (screen)
(when (funcall condition screen)
(setq result (cons screen result))))
screen-list))
result))))
(defun elscreen-find-screen (condition)
(catch 'elscreen-find-screen
(elscreen-find-screens `(lambda (screen)
(when (funcall ,condition screen)
(throw 'elscreen-find-screen screen))))))
(defun elscreen-find-screen-by-buffer (buffer &optional create)
(let* ((buffer (if (bufferp buffer) buffer (get-buffer buffer)))
(screen (when buffer
(elscreen-find-screen
`(lambda (screen)
(elscreen-goto-internal screen)
(not (null (get-buffer-window ,buffer))))))))
(when (and buffer (null screen) create)
(cond
((setq screen (elscreen-create-internal 'noerror))
(save-window-excursion
(elscreen-goto-internal screen)
(switch-to-buffer buffer t)
(elscreen-set-window-configuration
screen (elscreen-current-window-configuration))))
(t
(setq screen (elscreen-get-current-screen))
(elscreen-goto-internal screen)
(save-selected-window
(select-window (split-window))
(switch-to-buffer buffer t)))))
screen))
(defun elscreen-find-screen-by-major-mode (major-mode-or-re)
(let ((major-mode-re (cond
((stringp major-mode-or-re)
major-mode-or-re)
((symbolp major-mode-or-re)
(format "^%s$" (regexp-quote
(symbol-name major-mode-or-re))))
(t nil))))
(when major-mode-re
(elscreen-find-screen
(lambda (screen)
(elscreen-goto-internal screen)
(save-selected-window
(catch 'found
(mapc
(lambda (window)
(select-window window)
(when (string-match major-mode-re (symbol-name major-mode))
(throw 'found t)))
(window-list))
nil)))))))
(defvar elscreen-last-message "Welcome to ElScreen!"
"Last shown message.")
(defun elscreen-message (message &optional sec)
"Display MESSAGE in mini-buffer for SEC seconds.
Default value for SEC is 3."
(when message
(setq elscreen-last-message message)
(message "%s" message)
(sit-for (or sec 3)))
(message nil))
;;; User Interfaces:
(defun elscreen-create ()
"Create a new screen and switch to it."
(interactive)
(let ((screen (elscreen-create-internal)))
(if screen
(elscreen-goto screen))))
(defun elscreen-clone (&optional screen)
"Create a new screen with the window-configuration of SCREEN.
If SCREEN is ommitted, current-screen is used."
(interactive)
(let ((screen (or screen (elscreen-get-current-screen)))
clone elscreen-window-configuration)
(cond
((not (elscreen-screen-live-p screen))
(elscreen-message "There is no such screen, cannot clone"))
((setq clone (elscreen-create-internal))
(save-window-excursion
(elscreen-goto-internal screen)
(setq elscreen-window-configuration
(elscreen-current-window-configuration)))
(elscreen-set-window-configuration clone elscreen-window-configuration)
(elscreen-goto clone)))))
(defvar elscreen-kill-hook nil)
(defun elscreen-kill (&optional screen)
"Kill SCREEN. If optional argument SCREEN is
ommitted, current-screen is killed."
(interactive "P")
(let ((screen (or (and (numberp screen) screen)
(elscreen-get-current-screen))))
(cond
((not (elscreen-screen-live-p screen))
(elscreen-message "There is no such screen, cannot kill")
nil)
((elscreen-one-screen-p)
(elscreen-message "There is only one screen, cannot kill")
nil)
(t
(elscreen-kill-internal screen)
(elscreen-goto-internal (elscreen-get-current-screen))
(elscreen-notify-screen-modification 'force)
(run-hooks 'elscreen-kill-hook)
screen))))
(defun elscreen-kill-screen-and-buffers (&optional screen)
"Kill buffers on SCREEN and SCREEN itself. If optional
argument SCREEN is omitted, current screen is killed."
(interactive)
(let* ((screen (or screen (elscreen-get-current-screen)))
(elscreen-window-configuration
(elscreen-get-window-configuration screen)))
(when (elscreen-kill screen)
(save-window-excursion
(elscreen-apply-window-configuration elscreen-window-configuration)
(mapc
(lambda (buffer)
(when (buffer-live-p buffer)
(kill-buffer buffer)))
(mapcar 'window-buffer (window-list))))
screen)))
(defun elscreen-kill-others (&optional screen)
"Kill screens other than SCREEN. If optional argument SCREEN
is ommitted, current screen will survive."
(interactive)
(let* ((screen (or screen (elscreen-get-current-screen)))
(screen-list (when (elscreen-screen-live-p screen)
(delq screen (sort (elscreen-get-screen-list) '<))))
screen-list-string)
(cond
((not (elscreen-screen-live-p screen)) ;; XXX
(when (called-interactively-p 'any)
(elscreen-message "There is no such screen")))
((null screen-list)
(when (called-interactively-p 'any)
(elscreen-message "There is only one screen, cannot kill")))
((or
(not (called-interactively-p 'any))
(yes-or-no-p (format "Really kill screens other than %d? " screen)))
(setq screen-list-string (mapconcat
(lambda (screen)
(elscreen-kill-internal screen)
(number-to-string screen))
screen-list ","))
(elscreen-goto-internal screen)
(elscreen-notify-screen-modification 'force-immediately)
(when (called-interactively-p 'any)
(elscreen-message (format "screen %s killed" screen-list-string)))))
screen-list))
(defvar elscreen-goto-hook nil)
(defun elscreen-goto (screen)
"Switch to screen SCREEN."
(interactive "NGoto screen number: ")
(cond
((eq (elscreen-get-current-screen) screen))
((elscreen-screen-live-p screen)
(elscreen-set-window-configuration
(elscreen-get-current-screen)
(elscreen-current-window-configuration))
(elscreen-set-current-screen screen)
(elscreen-goto-internal screen)
(elscreen-notify-screen-modification 'force)
(run-hooks 'elscreen-goto-hook)
screen)
(t
(elscreen-message (format "No screen %d" screen))
nil)))
(defun elscreen-next ()
"Switch to the next screen."
(interactive)
(cond
((elscreen-one-screen-p)
(elscreen-message
(format "You cannot escape from screen %d!"
(elscreen-get-current-screen))))
(t
(let* ((screen-list (sort (elscreen-get-screen-list) '<))
(next-screen
(or (nth 1 (memq (elscreen-get-current-screen) screen-list))
(car screen-list))))
(elscreen-goto next-screen)))))
(defun elscreen-previous ()
"Switch to the previous screen."
(interactive)
(cond