forked from kiwanami/emacs-window-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2wm.el
3871 lines (3423 loc) · 140 KB
/
e2wm.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
;;; e2wm.el --- simple window manager for emacs
;; Copyright (C) 2010, 2011 SAKURAI Masashi
;; Author: SAKURAI Masashi <m.sakurai atmark kiwanami.net>
;; Version: 1.2
;; Keywords: tools, window manager
;; 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 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This is an demonstration implementation of introducing window management to Emacs.
;; * Management of list of editable buffers
;; * Assignment of windows for pop-up buffers
;; * Switching window layout like the perspective in eclipse
;; * Plug-in extension
;; The current implementation has following perspectives:
;; * code : main coding layout
;; * two : side by side layout
;; * doc : reading documentation layout
;; * dashboard : showing plug-ins like dashboard in Mac OSX
;; * array : selecting buffers like expose in Mac OSX
;;; Installation:
;; (1) Put e2wm.el and window-layout.el in load-path.
;; (2) Put the following code in your .emacs file,
;; (require 'e2wm)
;; (3) M-x e2wm:start-management to start e2wm.
;; To stop e2wm, M-x e2wm:stop-management or [C-c ; Q].
;;; Customization
;; * Layout recipe (`e2wm:c-PST-NAME-recipe'):
;; Layout recipe RECIPE (e.g., `e2wm:c-code-recipe') is a recursive
;; tree-like structure defined as follows:
;; (SPLIT-TYPE [SPLIT-OPTION]
;; WINDOW-or-RECIPE ; left or upper side
;; WINDOW-or-RECIPE) ; right or lower side
;; WINDOW is a name (symbol) of the window. This is used in the
;; `:name' property of the window information list (winfo, see the
;; next section).
;; Split types (SPLIT-TYPE):
;; - : split vertically
;; | : split horizontally
;; Split option list SPLIT-OPTION is a plist with the following
;; properties. (the prefix 'left' can be replaced by 'right', 'upper'
;; and 'lower'.):
;; :left-size : (column or row number) window size
;; :left-max-size : (column or row number) if window size is larger
;; : than this value, the window is shrunken.
;; :left-size-ratio : (0.0 - 1.0) window size ratio. the size of
;; : the other side is the rest.
;;
;; Note:
;; The split option can be omitted.
;; The size parameters, :size, :max-size and :size-ratio, are mutually
;; exclusive. The size of a window is related with one of the other
;; side window. So, if both side windows set size parameters, the
;; window size may not be adjusted as you write.
;; * Window information (`e2wm:c-PST-NAME-winfo'):
;; Window information (e.g., `e2wm:c-code-winfo') is a list of window
;; options (plist). Besides the options defined in window-layout.el,
;; `:name' (mandatory), `:buffer', `:default-hide' and `:fix-size',
;; e2wm has additional options.
;; :name [*] : the window name.
;; :buffer : A buffer name or a buffer object to show the window.
;; : If nil or omitted, the current buffer remains.
;; :default-hide : If t, the window is hided initially.
;; : (type: t or nil, default: nil)
;; :fix-size : If t, when the windows are laid out again, the
;; : window size is remained.
;; : (type: t or nil, default: nil)
;; :plugin : Plug-in name.
;; : (type: symbol)
;; :plugin-args : Arguments for the plug-in. See each plug-in
;; : documentation for use of this option.
;; : (type: any lisp objecct)
;;; Development memo:
;; See readme for further documentation.
;; ** Side effects
;;
;; * advice
;; - buffer系
;; switch-to-buffer, pop-to-buffer
;; - window-configuration系
;; current-window-configuration
;; window-configuration-frame
;; compare-window-configurations
;; set-window-configuration
;; window-configuration-p
;; * hook
;; kill-buffer-hook
;; window-configuration-change-hook
;; completion-setup-hook
;; after-save-hook
;; * override variable
;; special-display-function
;; ** Local words
;; pst : Perspective
;; e2wm:c- : Configuration variables
;; e2wm:$ : Structure functions
;; ** Source code layout
;; Configurations / e2wm:c-
;; Fundamental functions
;; Buffer history management / e2wm:history-
;; Framework for perspectives / e2wm:pst-
;; Framework for perspective set / e2wm:pstset-
;; Advices and hooks (switch-to-buffer, pop-to-buffer and so on)
;; Framework for plug-ins / e2wm:plugin-
;; Menu definition / e2wm:menu-
;; Plug-in definitions / e2wm:def-plugin-
;; Perspective definitions / e2wm:dp-
;; code / e2wm:dp-code-
;; doc / e2wm:dp-doc-
;; two / e2wm:dp-two-
;; dashboard / e2wm:dp-dashboard-
;; array / e2wm:dp-array-
;; Start-up and exit e2wm
;;; Code:
(require 'cl)
(require 'imenu)
(require 'easymenu)
(require 'windmove)
(require 'window-layout)
(eval-when-compile (defvar prev-selected-buffer))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ### Customize
(defvar e2wm:c-max-history-num 20 "Number of buffer history.")
(defvar e2wm:c-recordable-buffer-p
(lambda (buf)
(buffer-local-value 'buffer-file-name buf))
"Return non-nil, if the buffer is an editable buffer.")
(defvar e2wm:c-document-buffer-p ;
(lambda (buf)
(string-match "\\*\\(Help\\|info\\|w3m\\|WoMan\\)" (buffer-name buf)))
"Retrun non-nil, if the buffer is a document buffer.")
(defun e2wm:c-blank-buffer ()
(let ((buf (get-buffer-create " *e2wm:blank*")))
(with-current-buffer buf
(setq buffer-read-only nil)
(buffer-disable-undo buf)
(erase-buffer)
(setq buffer-read-only t))
buf))
(defvar e2wm:prefix-key "C-c ; " "Prefix key")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ### Macro / Utilities
(defmacro e2wm:aif (test-form then-form &rest else-forms)
"Anaphoric IF."
(declare (debug (form form &rest form)))
`(let ((it ,test-form))
(if it ,then-form ,@else-forms)))
(put 'e2wm:aif 'lisp-indent-function 2)
(defmacro e2wm:aand (test &rest rest)
"Anaphoric AND."
(declare (debug (form &rest form)))
`(let ((it ,test))
(if it ,(if rest (macroexpand-all `(e2wm:aand ,@rest)) 'it))))
(defmacro e2wm:not-minibuffer (&rest body)
"Evaluate a body form when the minibuffer is not active."
(declare (debug (&rest form)))
`(when (= 0 (minibuffer-depth))
,@body))
(defmacro e2wm:safe-call (method object &rest args)
"Safely method calling."
(let ((sym (gensym)))
`(let ((,sym (,method ,object)))
(if ,sym
(funcall ,sym ,@args)))))
;; for a list of structure
(defun e2wm:find (name name-func seq)
"Return the element that the return value of the NAME-FUNC
equals to NAME in the given sequence SEQ."
(loop for i in seq
if (eq name (funcall name-func i))
return i))
(defmacro e2wm:delete! (name name-func seq)
"Destructively delete the element."
`(setq ,seq
(delete-if
(lambda (i) (eq ,name (funcall ',name-func i)))
,seq)))
;; debug
(eval-and-compile
(defvar e2wm:debug nil "Debug output switch.")) ; debug
(defvar e2wm:debug-count 0 "[internal] Debug output counter.") ; debug
(defmacro e2wm:message (&rest args) ; debug
"Output a message into the debug buffer: *e2wm:debug*."
(when e2wm:debug
`(progn
(with-current-buffer (get-buffer-create "*e2wm:debug*")
(save-excursion
(goto-char (point-max))
(insert (format "%5i %s\n" e2wm:debug-count (format ,@args)))))
(incf e2wm:debug-count))))
(defun e2wm:message-mark () ; debug
"Output a mark text into the debug buffer: *e2wm:debug*."
(interactive)
(e2wm:message "==================== mark ==== %s"
(format-time-string "%H:%M:%S" (current-time))))
;; keymap
(defun e2wm:define-keymap (keymap-list &optional prefix)
"[utility] Return a keymap object with given keymap definitions
that is a list of cons cells ([keyboard macro string]
. [function])."
(let ((map (make-sparse-keymap)))
(mapc
(lambda (i)
(define-key map
(if (stringp (car i))
(read-kbd-macro
(if prefix
(replace-regexp-in-string "prefix" prefix (car i))
(car i)))
(car i))
(cdr i)))
keymap-list)
map))
(defun e2wm:add-keymap (keymap keymap-list &optional prefix)
"[utility] Add given keymap definitions to KEYMAP."
(mapc
(lambda (i)
(define-key keymap
(if (stringp (car i))
(read-kbd-macro
(if prefix
(replace-regexp-in-string "prefix" prefix (car i))
(car i)))
(car i))
(cdr i)))
keymap-list)
keymap)
;; window overriding
(defvar e2wm:ad-now-overriding nil
"[internal] Recursive execution flag. If e2wm is evaluating
overriding functions, this variable is set t.")
(defmacro e2wm:with-advice (&rest body)
"[internal] Avoid infinite recursion in the overriding
functions, such as `switch-to-buffer' and `pop-to-buffer'.
If the original function should be called, use this macro."
(declare (debug (&rest form)))
`(let ((e2wm:ad-now-overriding t))
,@body))
;; text / string
(defun e2wm:string-trim (txt)
"Remove white space characters at head and tail
from the given string."
(let ((ret txt))
(setq ret (if (string-match "^\\s-*" ret)
(substring ret (match-end 0))
ret))
(or
(loop for i downfrom (1- (length ret)) downto 0 do
(if (/= 32 (char-syntax (aref ret i)))
(return (substring ret 0 (1+ i)))))
"")))
(defun e2wm:strtime (time)
"[utility] Format time object."
(if (equal (cdddr (decode-time time))
(cdddr (decode-time (current-time))))
(format-time-string "Today %H:%M:%S" time)
(format-time-string "%Y/%m/%d %H:%M:%S" time)))
(defface e2wm:face-title
'((((class color) (background light))
:foreground "Deeppink2" :height 1.5 :inherit variable-pitch)
(((class color) (background dark))
:foreground "yellow" :weight bold :height 1.5 :inherit variable-pitch)
(t :height 1.5 :weight bold :inherit variable-pitch))
"Face for e2wm titles at level 1."
:group 'e2wm)
(defface e2wm:face-subtitle
'((((class color) (background light))
(:foreground "Gray10" :height 1.2 :inherit variable-pitch))
(((class color) (background dark))
(:foreground "Gray90" :height 1.2 :inherit variable-pitch))
(t :height 1.2 :inherit variable-pitch))
"Face for e2wm titles at level 2."
:group 'e2wm)
(defface e2wm:face-item
'((t :inherit variable-pitch :foreground "DarkSlateBlue"))
"Face for e2wm items."
:group 'e2wm)
(defun e2wm:rt (text face)
"[utility] Put the face property to TEXT."
(unless (stringp text) (setq text (format "%s" text)))
(put-text-property 0 (length text) 'face face text) text)
(defun e2wm:rt-format (text &rest args)
"[utility] Format strings with faces. TEXT is format
string. ARGS is a list of cons cell, ([string] . [face name])."
(apply 'format (e2wm:rt text 'e2wm:face-item)
(loop for i in args
if (consp i)
collect (e2wm:rt (car i) (cdr i))
else
collect (e2wm:rt i 'e2wm:face-subtitle))))
(defun e2wm:tp (text prop value)
"[utility] Put a text property to the first character of TEXT."
(if (< 0 (length text))
(put-text-property 0 1 prop value text))
text)
(defun e2wm:format-byte-unit (size)
"[utility] Format a number with the human readable unit."
(cond ((null size) "NA")
((> size (* 1048576 4))
(format "%s Mb" (e2wm:num (round (/ size 1048576)))))
((> size (* 1024 4))
(format "%s Kb" (e2wm:num (round (/ size 1024)))))
(t
(format "%s bytes" (e2wm:num size)))))
(defun e2wm:num (number)
"[utility] Format a number."
(let ((base (format "%s" number)))
(flet
((rec (str len)
(let ((pos (- len 3)))
(if (< pos 1) str
(concat (rec (substring str 0 pos) pos)
"," (substring str pos))))))
(rec base (length base)))))
(defun e2wm:max-length (rows)
"[utility] Return the max length of string in the given
sequence. ROWS is a list of string."
(loop for i in rows
with lmax = 0
for ln = (if i (length i) 0)
do (setq lmax (max lmax ln))
finally return lmax))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ### Base API / History Management
(defun e2wm:frame-param-get (name &optional frame)
"[internal] Return a value from frame properties."
(frame-parameter (or frame (selected-frame)) name))
(defun e2wm:frame-param-set (name val &optional frame)
"[internal] Set a frame property."
(set-frame-parameter (or frame (selected-frame)) name val))
(defun e2wm:document-buffer-p (buffer)
"If BUFFER is document, return t. See the variable `e2wm:c-document-buffer-p'."
(if (and buffer (buffer-live-p buffer))
(funcall e2wm:c-document-buffer-p buffer)))
;; History data structure
;;
;; 1: buffer a |
;; 2: buffer b | history-backup
;; currently displayed
;; -> 3: buffer c |
;; 4: buffer d |
;; 5: buffer e | history
;;
(defun e2wm:history-get ()
"Return a list of buffer history from the current frame."
(e2wm:frame-param-get 'e2wm:buffer-history))
(defun e2wm:history-save (buffer-history)
"Save the given list as buffer history in the current frame."
(e2wm:frame-param-set
'e2wm:buffer-history buffer-history)
buffer-history)
(defun e2wm:history-get-backup ()
"Return a list of buffer backup-history."
(e2wm:frame-param-get
'e2wm:buffer-history-backup))
(defun e2wm:history-save-backup (buffer-history-backup)
"Save the given list as buffer backup-history."
(e2wm:frame-param-set
'e2wm:buffer-history-backup
buffer-history-backup)
buffer-history-backup)
(defun e2wm:history-recordable-p (buffer)
"If BUFFER should be record in buffer history, return t.
See the variable `e2wm:c-recordable-buffer-p'."
(if (and buffer (buffer-live-p buffer))
(funcall e2wm:c-recordable-buffer-p buffer)))
(defun e2wm:history-add (buffer)
"Add BUFFER to buffer history.
This function does following jobs:
* clear all dead buffers
* sort the list of buffer history by LRU
* add a buffer and truncate the tail element
* save the buffer history."
(e2wm:message "#HISTORY-ADD : %s" buffer)
(e2wm:aif (get-buffer buffer)
(let* ((prev-history (e2wm:history-get))
(last-buffer (car prev-history))
(history
(mapcar
'car
(sort
(loop for h in (append
(cdr prev-history)
(e2wm:history-get-backup))
for b = (get-buffer h)
if (and b (buffer-live-p b))
collect (cons b (float-time
(buffer-local-value
'buffer-display-time b))))
(lambda (i j)
(> (cdr i) (cdr j)))))))
(when last-buffer
(setq history (cons last-buffer history)))
(when (e2wm:history-recordable-p it)
(e2wm:history-save-backup nil)
(setq history
(cons it
(if (member it history)
(remove it history)
(if (< e2wm:c-max-history-num (length history))
(nbutlast history) history))))
(e2wm:history-save history)))))
(defun e2wm:history-back ()
"Move backward in the buffer history. This function does not update windows."
(let ((history (e2wm:history-get))
(history-backup (e2wm:history-get-backup)))
(when (and history (cdr history))
(push (pop history) history-backup))
(e2wm:history-save history)
(e2wm:history-save-backup history-backup)))
(defun e2wm:history-forward ()
"Move forward in the buffer history. This function does not update windows."
(let ((history (e2wm:history-get))
(history-backup (e2wm:history-get-backup)))
(when history-backup
(push (pop history-backup) history))
(e2wm:history-save history)
(e2wm:history-save-backup history-backup)))
(defun e2wm:history-delete (buffer)
"Delete BUFFER from the buffer history. This function does not update windows."
(let ((history (e2wm:history-get))
(history-backup (e2wm:history-get-backup)))
(setq history (remove buffer history))
(setq history-backup (remove buffer history-backup))
(when (and (null history) history-backup)
(push (pop history-backup) history))
(e2wm:history-save history)
(e2wm:history-save-backup history-backup)))
(defun e2wm:history-get-next (buffer)
"Return the buffer that is previous to BUFFER in the buffer history.
If no buffer is found, return BUFFER."
(let* ((history (append (reverse (e2wm:history-get-backup))
(e2wm:history-get))))
(or (loop for i in history
with prev = nil
do
(when
(eql buffer i) (return prev))
(setq prev i))
buffer)))
(defun e2wm:history-get-prev (buffer)
"Return the buffer that is next to BUFFER in the buffer history.
If no buffer is found, return BUFFER."
(let* ((history (append (reverse (e2wm:history-get-backup))
(e2wm:history-get))))
(or (loop for i in history
with found = nil
do
(cond
(found (return i))
((eql buffer i) (setq found t))))
buffer)))
(defun e2wm:history-get-nearest (buffer n)
"Return a list of N buffers that is near to the BUFFER but is
*not* the BUFFER. If some buffers are not found, return nil."
(let* ((history (append (reverse (e2wm:history-get-backup))
(e2wm:history-get)))
(prevs nil)
(nexts nil))
(loop for i in history
for c from 0
with found = nil
if (and found (< n c))
return found
else
do (if (eql buffer i)
(setq found t)
(if found
(push i nexts)
(push i prevs))))
(if (or prevs nexts)
(loop for i in (subseq (append prevs (reverse nexts)) 0 n)
with last-non-nil = nil
if i collect i and do (setq last-non-nil i)
else collect last-non-nil)
nil)))
(defun e2wm:history-get-main-buffer ()
"Return the main buffer that should be display as the current
editing buffer."
(e2wm:aif (e2wm:history-get)
(car it) (e2wm:c-blank-buffer)))
(defun e2wm:managed-p (&optional frame)
"Return t, if e2wm manages the current frame."
(e2wm:pst-get-instance frame))
(defun e2wm:internal-buffer-p (buf)
"Return t, if BUF is internal buffer created by e2wm.
The current implementation check the buffer name. TODO: improve the internal sign."
(e2wm:aand buf (string-match "\\*WM:" (buffer-name it))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ### Perspective Framework
(defvar e2wm:pst-list nil "[internal] Perspective class registory.")
(setq e2wm:pst-list nil)
(defvar e2wm:prev-selected-buffer nil
"The dynamically bound variable pointed to the previous buffer.
This is available in `init', `start' and `leave' function.
Note that `prev-selected-buffer' is obsolete now.")
;; structure [ e2wm:$pst-class ]
;;
;; This structure defines a perspective.
;; The symbol (*) means the slot must be defined.
;;
;; name : (*) The identifier symbol for this perspective
;; extend : A symbol for the base perspective class.
;; : If the functions described below are set nil, the functions of the base class are called.
;; : Note that the functions can call the dynamically bound function `e2wm:$pst-class-super' to
;; : evaluate the base class ones.
;; init : (*) The constructor function for this perspective. (Arguments: none)
;; : This function just set up the wset structure object and return it.
;; : Actual laying out and setting up hooks should be done in the `start' function.
;; : The dynamically bound variable `e2wm:prev-selected-buffer' can be used at the
;; : functions `init' and `start'.
;; title : (*) A string for the title of this perspective.
;; main : A symbol for the window name which is selected initially.
;; : If this slot is nil, the e2wm doesn't care of window focus.
;; start : This function lays out windows and set up buffers and some variables for this perspective.
;; : (Arguments: `wm')
;; : If this slot is nil, the e2wm does nothing at the beginning of the perspective.
;; : This function may be called again, after the evaluating `leave' and
;; : suspending the e2wm management.
;; update : This function updates the windows and plug-ins. (Arguments: `wm')
;; : If this slow it nil, the e2wm just refreshes the windows and plug-ins.
;; switch : This function overrides `switch-to-buffer'. (Arguments: `buffer')
;; : If this slot is nil or this function returns nil,
;; : the original function `switch-to-buffer' is evaluated.
;; : If this function returns non-nil, the original function `switch-to-buffer' is not evaluated.
;; : If the plug-ins need to be updated, this function should call the
;; : `e2wm:pst-update-windows' to update the plug-ins.
;; popup : This function overrides `pop-to-buffer' and `special-display-func'. (Arguments: `buffer')
;; : See the `switch' spec for detail.
;; leave : This function cleans up buffers and some variables for leaving the perspective.
;; : (Arguments: `wm')
;; : If this slot is nil, the e2wm does nothing during leaving the perspective.
;; keymap : A symbol for the key map of this perspective.
;; save : This function is called by the hook `after-save-hook' when this perspective is active.
(defstruct e2wm:$pst-class
name title extend
init main start update switch popup leave
keymap save)
(defun e2wm:pst-class-register (pst-class)
"Register a perspective class. If the class which name is
the same as PST-CLASS has been already registered, the given class
overrides the previous one."
(when (e2wm:aand (e2wm:$pst-class-extend pst-class)
(symbolp it))
;; A symbol for the super class is replaced by the class object.
(setf (e2wm:$pst-class-extend pst-class)
(e2wm:pst-class-get (e2wm:$pst-class-extend pst-class))))
(e2wm:pst-class-remove pst-class)
(push pst-class e2wm:pst-list))
(defun e2wm:pst-class-remove (pst-class)
"Remove the class from the perspective class registry."
(setq e2wm:pst-list
(loop with name = (e2wm:$pst-class-name pst-class)
for i in e2wm:pst-list
unless (equal name (e2wm:$pst-class-name i))
collect i)))
(defun e2wm:pst-class-get (name)
"Return the class object which name is NAME."
(e2wm:find name 'e2wm:$pst-class-name e2wm:pst-list))
(defun e2wm:pst-class-abstract-p (pst-class)
"Return non-`nil' when PST-CLASS does not have all mandatory
slots (i.e., `:init' and `:title')."
(and pst-class
(not (e2wm:$pst-class-init pst-class))
(not (e2wm:$pst-class-title pst-class))))
;; structure [e2wm:$pst]
;;
;; This structure represents an instance of the perspective.
;;
;; name : A symbol for this perspective
;; wm : wlf layout object
;; type : A reference to the perspective class object
(defstruct e2wm:$pst name wm type)
(defun e2wm:$pst-get-prop (name pst)
"[internal] Return the value of this perspective."
(let ((slot-name (intern (format "e2wm:$pst-class-%s" name))))
(e2wm:$pst-class-get-prop-gen slot-name (e2wm:$pst-type pst))))
(defun e2wm:$pst-class-get-prop-gen (slot-name pst-class)
"[internal] Return the slot value of this perspective class."
(or (funcall slot-name pst-class)
(e2wm:aif (e2wm:$pst-class-extend pst-class)
(e2wm:$pst-class-get-prop-gen slot-name it))))
(defun e2wm:method-call (method-name class error-on-nil &rest args)
"[internal] Call the method which belongs to the perspective class.
If ERROR-ON-NIL is non-nil and the CLASS has no value at the slot,
raise the error signal with ERROR-ON-NIL."
(lexical-let ((method (e2wm:$pst-class-get-prop-gen method-name class))
(super-class (e2wm:$pst-class-extend class))
;; put all arguments in lexical scope to use it in
;; `e2wm:$pst-class-super':
(method-name method-name)
(class class)
(error-on-nil error-on-nil)
(args args))
(cond
((null method)
(if error-on-nil (error error-on-nil) nil))
(t
(flet ((e2wm:$pst-class-super () (apply
#'e2wm:method-call
method-name super-class
error-on-nil args)))
(apply method args))))))
(defmacro e2wm:pst-method-call (method-name pst-instance &rest args)
"[internal] Short cut macro. (pst-instance -> pst-class)"
`(e2wm:method-call
',method-name
(e2wm:$pst-type ,pst-instance) nil ,@args))
(defun e2wm:$pst-class-super ()
"Dynamically bound super class method of the current perspective method.
WARNING: Call this function only *inside* of a perspective method
which class (`e2wm:$pst-class') has the `:extend' slot.
This function is a dummy implementation for suppressing compile
warning and providing information to user via docstring and error
message. Directly calling this function raises error.
See `e2wm:method-call' for implementation."
(error "e2wm:$pst-class-super is called outside of perspective method."))
(defun e2wm:$pst-title (pst)
(e2wm:$pst-get-prop 'title pst))
(defun e2wm:$pst-main (pst)
(e2wm:$pst-get-prop 'main pst))
(defun e2wm:$pst-keymap (pst)
(e2wm:aif (e2wm:$pst-get-prop 'keymap pst)
(symbol-value it) nil))
(defun e2wm:$pst-start (pst)
(e2wm:$pst-class-start (e2wm:$pst-type pst)))
(defun e2wm:$pst-update (pst)
(e2wm:$pst-class-update (e2wm:$pst-type pst)))
(defun e2wm:$pst-switch (pst)
(e2wm:$pst-class-switch (e2wm:$pst-type pst)))
(defun e2wm:$pst-popup (pst)
(e2wm:$pst-class-popup (e2wm:$pst-type pst)))
(defun e2wm:$pst-leave (pst)
(e2wm:$pst-class-leave (e2wm:$pst-type pst)))
(defun e2wm:$pst-save (pst)
(e2wm:$pst-class-save (e2wm:$pst-type pst)))
(defun e2wm:$pst-super (pst)
(e2wm:$pst-class-extend (e2wm:$pst-type pst)))
(defun e2wm:pst-get-instance (&optional frame)
(e2wm:frame-param-get 'e2wm:pst frame))
(defun e2wm:pst-set-instance (pst-instance)
(e2wm:frame-param-set 'e2wm:pst pst-instance))
(defun e2wm:pst-get-prev-pst ()
(e2wm:frame-param-get 'e2wm:prev-pst))
(defun e2wm:pst-set-prev-pst (pst-name)
(e2wm:frame-param-set 'e2wm:prev-pst pst-name))
(defun e2wm:pst-copy-instance ()
"[internal] Copy the current perspective instance."
(let ((i (e2wm:pst-get-instance)))
(make-e2wm:$pst
:name (e2wm:$pst-name i)
:wm (wlf:copy-windows (e2wm:pst-get-wm))
:type (e2wm:$pst-type i))))
(defun e2wm:pst-get-wm ()
"Return the window layout object of the current perspective."
(e2wm:aif (e2wm:pst-get-instance)
(e2wm:$pst-wm it)))
(defun e2wm:pst-update-windows (&optional rebuild-windows)
"Update all buffers of the windows and plug-ins. If
REBUILD-WINDOWS is non-nil, windows are destroyed and new windows
are created."
(e2wm:message "#PST-UPDATE")
(e2wm:with-advice
(let* ((instance (e2wm:pst-get-instance))
(wm (e2wm:$pst-wm instance)))
;;(e2wm:debug-windows (e2wm:pst-get-wm))
(unless rebuild-windows
(wlf:wset-fix-windows wm))
(when (or rebuild-windows
(not (wlf:wset-live-p wm)))
(e2wm:message " #PST-UPDATE > REBUILD")
(wlf:refresh wm)
(e2wm:aif (e2wm:$pst-main instance)
(wlf:select wm it)))
;; Update task for the current perspective
;; (Plug-ins are updated by `e2wm:dp-base-update')
(e2wm:pst-method-call e2wm:$pst-class-update instance wm)
)) t)
(defun e2wm:pst-switch-to-buffer (buf)
"[internal] Delegate the `switch' function of the current perspective."
(e2wm:message "#PST-SWITCH %s" buf)
(e2wm:pst-method-call e2wm:$pst-class-switch (e2wm:pst-get-instance) buf))
(defun e2wm:pst-pop-to-buffer (buf)
"[internal] Delegate the `popup' function of the current perspective."
(e2wm:message "#PST-POPUP %s" buf)
(e2wm:pst-method-call e2wm:$pst-class-popup (e2wm:pst-get-instance) buf))
(defun e2wm:pst-change (next-pst-name)
"Leave the current perspective and start the new perspective."
(e2wm:message "#PST-CHANGE %s" next-pst-name)
(let ((prev-pst-instance (e2wm:pst-get-instance))
(next-pst-class (e2wm:pst-class-get next-pst-name))
(e2wm:prev-selected-buffer (current-buffer))
(prev-selected-buffer (current-buffer)))
(when (e2wm:internal-buffer-p e2wm:prev-selected-buffer)
(setq e2wm:prev-selected-buffer nil)
(setq prev-selected-buffer nil))
(cond
((null next-pst-class)
(error "Perspective [%s] is not found." next-pst-name))
(t
(e2wm:aif prev-pst-instance
(progn
(e2wm:pst-method-call e2wm:$pst-class-leave it (e2wm:$pst-wm it))
(unless (eql next-pst-name (e2wm:$pst-name it))
(e2wm:pst-set-prev-pst (e2wm:$pst-name it)))))
(let* ((next-pst-wm
(e2wm:method-call 'e2wm:$pst-class-init
next-pst-class
(format "[%s] init method is nil!" next-pst-name)))
(next-pst-instance
(make-e2wm:$pst :name next-pst-name
:wm next-pst-wm
:type next-pst-class)))
(e2wm:pst-set-instance next-pst-instance)
(e2wm:pst-change-keymap (e2wm:$pst-keymap next-pst-instance))
(e2wm:pst-method-call e2wm:$pst-class-start next-pst-instance
(e2wm:$pst-wm next-pst-instance)))))
(e2wm:pst-update-windows t)))
(defun e2wm:pst-change-prev ()
"[internal] Change to the previous perspective."
(e2wm:aif (e2wm:pst-get-prev-pst)
(progn
(e2wm:message "#PREV-PST : %s" it)
(e2wm:pst-change it))))
(defvar e2wm:pst-minor-mode-keymap
(e2wm:define-keymap
'(("prefix Q" . e2wm:stop-management)
("prefix l" . e2wm:pst-update-windows-command)
("prefix n" . e2wm:pst-history-down-command)
("prefix p" . e2wm:pst-history-up-command)
("prefix <DEL>" . e2wm:pst-change-prev-pst-command)
("prefix t" . e2wm:pst-window-toggle-main-sub)
("prefix h" . e2wm:def-plugin-history-jump-to-number)
) e2wm:prefix-key)
"Common key map for all perspectives. (See `e2wm:pst-change-keymap')")
(defun e2wm:pst-change-keymap (new-keymap)
"[internal] Add the perspective key map to the common key map."
(let ((map (copy-keymap
(or new-keymap e2wm:pst-minor-mode-keymap))))
(when new-keymap
(set-keymap-parent map e2wm:pst-minor-mode-keymap))
(e2wm:aif (assq 'e2wm:pst-minor-mode minor-mode-map-alist)
(setf (cdr it) map))))
(defun e2wm:pst-resume (pst-instance)
"[internal] Resume the perspective which is suspended by the function `e2wm:pst-finish'."
(e2wm:message "#PST-RESUME %s" pst-instance)
;; This function assumes that the window configuration is
;; restored by `set-window-configuration'.
(e2wm:pst-set-instance pst-instance)
(e2wm:pst-change-keymap (e2wm:$pst-keymap pst-instance))
(e2wm:pst-method-call e2wm:$pst-class-start pst-instance (e2wm:$pst-wm pst-instance)))
(defun e2wm:pst-finish ()
"[internal] Suspend the current perspective for finishing e2wm or
switching the window configuration to the non-e2wm frame during
the other application calling `set-window-configuration'."
(e2wm:message "#PST-FINISH")
(let ((prev-pst-instance (e2wm:pst-get-instance)))
(when prev-pst-instance
(e2wm:pst-method-call e2wm:$pst-class-leave prev-pst-instance
(e2wm:$pst-wm prev-pst-instance)))
(e2wm:pst-set-instance nil)))
(defun e2wm:pst-window-option-get (wm window-name)
"Return a plist of the plug-in option at the WINDOW-NAME window."
(wlf:window-options
(wlf:get-winfo window-name (wlf:wset-winfo-list wm))))
(defun e2wm:pst-window-plugin-get (wm window-name)
"Return a symbol of the plug-in at the WINDOW-NAME window. "
(plist-get (e2wm:pst-window-option-get wm window-name)
':plugin))
(defun e2wm:pst-window-plugin-set (wm window-name plugin-name)
"Set the plug-in at the WINDOW-NAME window."
(plist-put (e2wm:pst-window-option-get wm window-name)
':plugin plugin-name))
(defun e2wm:pst-buffer-get (window-name)
"Return the buffer object at the WINDOW-NAME window."
(let ((wm (e2wm:pst-get-wm)))
(when (wlf:window-name-p wm window-name)
(wlf:get-buffer wm window-name))))
(defun e2wm:pst-buffer-set (window-name buffer &optional showp selectp)
"Set the given BUFFER at the WINDOW-NAME window.
If SHOWP is non-nil, the hided window is displayed.
If SELECTP is non-nil, the window is selected."
(let ((wm (e2wm:pst-get-wm)))
(when (wlf:window-name-p wm window-name)
(when (and showp (not (wlf:get-window wm window-name)))
(wlf:show wm window-name))
(wlf:set-buffer wm window-name buffer selectp)
(when showp
(set-window-point
(wlf:get-window wm window-name)
(with-current-buffer buffer (point)))))))
(defun e2wm:pst-window-select (window-name)
"Select the WINDOW-NAME window."
(let ((wm (e2wm:pst-get-wm)))
(when (wlf:window-name-p wm window-name)
(wlf:select wm window-name))))
(defun e2wm:pst-window-select-main ()
"Select the `main' window which is defined by the perspective.
If the perspective has no `main' window, this function does nothing."
(let ((main (e2wm:$pst-main (e2wm:pst-get-instance)))
(wm (e2wm:pst-get-wm)))
(when (and main (wlf:window-name-p wm main))
(wlf:select wm main))))
(defun e2wm:pst-window-select-sub ()
"Select the `sub' window."
(let ((sub (wlf:get-window (e2wm:pst-get-wm) 'sub)))
(select-window sub)))
(defun e2wm:pst-window-toggle-main-sub ()
"Select the `sub' window if we're in the main, select `main' otherwise."
(interactive)
(let ((main (e2wm:$pst-main (e2wm:pst-get-instance)))
(wm (e2wm:pst-get-wm)))
(if (and
main
(wlf:window-name-p wm main)
(eq (wlf:get-window (e2wm:pst-get-wm) 'main) (selected-window)))
(e2wm:pst-window-select-sub)
(e2wm:pst-window-select-main))))
(defun e2wm:pst-window-toggle (window-name &optional selectp next-window)
"Toggle visibility of the window specified by WINDOW-NAME.
If SELECTP is non-nil, it selects that window when opening it.
NEXT-WINDOW specifies the window to select when closing the
WINDOW-NAME window. This function returns the name of the
selected window, or nil if none is selected."
(let ((wm (e2wm:pst-get-wm)))
(when (wlf:window-name-p wm window-name)
(wlf:toggle wm window-name)
(if (wlf:window-displayed-p wm window-name)
(when selectp (wlf:select wm window-name) window-name)
(when next-window (wlf:select wm next-window) next-window)))))
(defun e2wm:pst-show-history-main ()
"Display the history top buffer at the `main' window which is
defined by the perspective."
(e2wm:with-advice
(let* ((instance (e2wm:pst-get-instance))
(wm (e2wm:$pst-wm instance)))
(e2wm:aif (e2wm:$pst-main instance)
(wlf:set-buffer wm it (e2wm:history-get-main-buffer)))
(e2wm:pst-update-windows))))
(defun e2wm:pst-after-save-hook ()
"[internal] Hook for `after-save-hook'."
(e2wm:message "$$ AFTER SAVE HOOK %S" this-command)
;; Ignore save events those are triggered by timers.
(when this-command
(e2wm:pst-method-call e2wm:$pst-class-save (e2wm:pst-get-instance))
(e2wm:pst-update-windows)))
;;; Commands / Key bindings / Minor Mode
;;;--------------------------------------------------
(defun e2wm:pst-change-command ()
(interactive)
(let* ((pst-list (mapcar
(lambda (i)