-
Notifications
You must be signed in to change notification settings - Fork 71
/
perspective.el
2304 lines (2048 loc) · 97.1 KB
/
perspective.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
;;; perspective.el --- switch between named "perspectives" of the editor -*- lexical-binding: t; -*-
;; Copyright (C) 2008-2020 Natalie Weizenbaum <[email protected]>
;;
;; Licensed under the same terms as Emacs and under the MIT license.
;; Author: Natalie Weizenbaum <[email protected]>
;; URL: http://github.com/nex3/perspective-el
;; Package-Requires: ((emacs "24.4") (cl-lib "0.5"))
;; Version: 2.19
;; Created: 2008-03-05
;; By: Natalie Weizenbaum <[email protected]>
;; Keywords: workspace, convenience, frames
;;; Commentary:
;; This package provides tagged workspaces in Emacs, similar to
;; workspaces in windows managers such as Awesome and XMonad (and
;; somewhat similar to multiple desktops in Gnome or Spaces in OS X).
;; Perspective provides multiple workspaces (or "perspectives") for each Emacs
;; frame. This makes it easy to work on many separate projects without getting
;; lost in all the buffers.
;; Each perspective is composed of a window configuration and a set of
;; buffers. Switching to a perspective activates its window
;; configuration, and when in a perspective only its buffers are
;; available by default.
;;; Code:
(require 'cl-lib)
(require 'ido)
(require 'rx)
(require 'subr-x)
(require 'thingatpt)
;;; --- customization
(defgroup perspective-mode 'nil
"Customization for Perspective mode"
:group 'frames)
(defcustom persp-initial-frame-name "main"
"Name used for the initial perspective when enabling `persp-mode'."
:type 'string
:group 'perspective-mode)
(defcustom persp-show-modestring t
"Determines if the list of perspectives is shown in the modeline.
If the value is 'header, the list of perspectives is shown in the
header line instead."
:group 'perspective-mode
:type '(choice (const :tag "Off" nil)
(const :tag "Modeline" t)
(const :tag "Header" header)))
(defcustom persp-modestring-dividers '("[" "]" "|")
"Plist of strings used to create the string shown in the modeline.
First string is the start of the modestring, second is the
closing of the mode string, and the last is the divider between
perspectives."
:group 'perspective-mode
:type '(list (string :tag "Open")
(string :tag "Close")
(string :tag "Divider")))
(defcustom persp-modestring-short nil
"When t, show a shortened modeline string.
A shortened modeline string only displays the current perspective
instead of the full perspective list."
:group 'perspective-mode
:type 'boolean)
(defcustom persp-mode-prefix-key (if (version< emacs-version "28.0") (kbd "C-x x") nil)
"Prefix key to activate perspective-map."
:group 'perspective-mode
:set (lambda (sym value)
(when (and (bound-and-true-p persp-mode-map)
(bound-and-true-p perspective-map))
(persp-mode-set-prefix-key value))
(set-default sym value))
:type '(choice (const :tag "None" nil)
key-sequence))
(defcustom persp-interactive-completion-function
(if ido-mode 'ido-completing-read 'completing-read)
"Function used by Perspective to interactively complete user input."
:group 'perspective-mode
:type 'function)
(defcustom persp-switch-wrap t
"Whether `persp-next' and `persp-prev' should wrap."
:group 'perspective-mode
:type 'boolean)
(defcustom persp-sort 'name
"What order to sort perspectives.
If 'name, then sort alphabetically.
If 'access, then sort by last time accessed (latest first).
If 'created, then sort by time created (latest first)."
:group 'perspective-mode
:type '(choice (const :tag "By Name" name)
(const :tag "By Time Accessed" access)
(const :tag "By Time Created" created)))
(defcustom persp-frame-global-perspective-name "GLOBAL"
"The name for a frames global perspective."
:group 'perspective-mode
:type 'string)
(defcustom persp-frame-global-perspective-include-scratch-buffer nil
"If non-nil include `persp-frame-global-perspective-name's scratch buffer to
buffer switch options."
:group 'perspective-mode
:type 'boolean)
(defcustom persp-state-default-file nil
"When non-nil, it provides a default argument for `persp-state-save` and `persp-state-load` to work with.
`persp-state-save` overwrites this file without prompting, which
makes it easy to use in, e.g., `kill-emacs-hook` to automatically
save state when exiting Emacs."
:group 'perspective-mode
:type 'file)
(defcustom persp-suppress-no-prefix-key-warning nil
"When non-nil, do not warn the user about `persp-mode-prefix-key' not being set."
:group 'perspective-mode
:type 'boolean)
(defcustom persp-avoid-killing-last-buffer-in-perspective t
"Avoid killing the last buffer in a perspective.
This should not be set to nil unless there's a bug. This was
formerly a feature flag (persp-feature-flag-prevent-killing-last-buffer-in-perspective),
but it seems likely to stick around as a just-in-case for a while. It makes sense
to upgrade this from an experimental feature flag to a toggle.
TODO: Eventually eliminate this setting?"
:group 'perspective-mode
:type 'boolean)
(defalias 'persp-avoid-killing-last-buffer-in-perspective
'persp-feature-flag-prevent-killing-last-buffer-in-perspective)
(defcustom persp-purge-initial-persp-on-save nil
"When non-nil, kills all the buffers in the initial perspective upon state save.
When calling `persp-state-save`, all the buffers in the initial
perspective (\"main\" by default) are killed, expect the buffers
whose name match the regexes in
`persp-purge-initial-persp-on-save-exceptions'."
:group 'perspective-mode
:type 'boolean)
(defcustom persp-purge-initial-persp-on-save-exceptions nil
"Buffer whose name match with any regexp of this list
won't be killed upon state save if persp-purge-initial-persp-on-save is t"
:group 'perspective-mode
:type '(repeat regexp))
;;; --- implementation
;;; XXX: Nasty kludge to deal with the byte compiler, eager macroexpansion, and
;;; frame parameters being already set when this file is being compiled during a
;;; package upgrade. This enumerates all frame-parameters starting with
;;; persp--*, saves them in persp--kludge-save-frame-params, and then blanks
;;; them out of the frame parameters. They will be restored in the matching
;;; eval-when-compile form at the bottom of this source file. See
;;; https://github.com/nex3/perspective-el/issues/93.
(eval-when-compile
(defvar persp--kludge-save-frame-params)
(setq persp--kludge-save-frame-params
(cl-loop for kv in (frame-parameters nil)
if (string-prefix-p "persp--" (symbol-name (car kv)))
collect kv))
(modify-frame-parameters
nil
;; Set persp-- frame parameters to nil. The expression below creates an alist
;; where the keys are the relevant frame parameters and the values are nil.
(mapcar (lambda (x) (list (car x))) persp--kludge-save-frame-params)))
(defmacro persp-let-frame-parameters (bindings &rest body)
"Like `let', but for frame parameters.
Temporariliy set frame parameters according to BINDINGS then eval BODY.
After BODY is evaluated, frame parameters are reset to their original values."
(declare (indent 1))
(let ((current-frame-parameters (mapcar (lambda (binding) (cons (car binding) (frame-parameter nil (car binding)))) bindings)))
`(unwind-protect
(progn ,@(mapcar (lambda (binding) `(set-frame-parameter nil (quote ,(car binding)) ,(cadr binding))) bindings)
,@body)
;; Revert the frame-parameters
(modify-frame-parameters nil (quote ,current-frame-parameters)))))
(cl-defstruct (perspective
(:conc-name persp-)
(:constructor make-persp-internal))
name buffers killed local-variables
(last-switch-time (current-time))
(created-time (current-time))
(window-configuration (current-window-configuration))
(point-marker (point-marker)))
(defmacro with-current-perspective (&rest body)
"Operate on BODY when we are in a perspective."
(declare (indent 0))
`(when (persp-curr)
,@body))
(defmacro with-perspective (name &rest body)
"Switch to the perspective given by NAME while evaluating BODY."
(declare (indent 1))
(let ((old (cl-gensym)))
`(progn
(let ((,old (with-current-perspective (persp-current-name)))
(last-persp-cache (persp-last))
(result))
(unwind-protect
(progn
(persp-switch ,name 'norecord)
(setq result (progn ,@body)))
(when ,old (persp-switch ,old 'norecord)))
(set-frame-parameter nil 'persp--last last-persp-cache)
result))))
(defun persp--make-ignore-buffer-rx ()
(defvar ido-ignore-buffers)
(if ido-ignore-buffers
;; convert a list of regexps to one
(rx-to-string (append (list 'or)
(mapcar (lambda (rx) `(regexp ,rx))
ido-ignore-buffers)))
;; return a regex which matches nothing, and therefore should ignore nothing
"$^"))
;; NOTE: This macro is used as a place for setf expressions so be careful with
;; how you modify it as you may break things in surprising ways.
(defmacro persp-current-buffers ()
"Return a list of all buffers in the current perspective."
`(persp-buffers (persp-curr)))
(defun persp-current-buffers* (&optional include-global)
"Same as `persp-current-buffers' but if INCLUDE-GLOBAL include buffers from
the frame global perspective."
(if (not include-global)
(persp-current-buffers)
(delete-dups
(append (persp-current-buffers)
(when (member persp-frame-global-perspective-name (persp-names))
(with-perspective persp-frame-global-perspective-name
(if persp-frame-global-perspective-include-scratch-buffer
(persp-current-buffers)
(remove (persp-get-scratch-buffer) (persp-current-buffers)))))))))
(defun persp-current-buffer-names (&optional include-global)
"Return a list of names of all living buffers in the current perspective.
Include the names of the buffers in the frame global perspective when
INCLUDE-GLOBAL."
(let ((ignore-rx (persp--make-ignore-buffer-rx)))
(cl-loop for buf in (persp-current-buffers* include-global)
if (and (buffer-live-p buf)
(not (string-match-p ignore-rx (buffer-name buf))))
collect (buffer-name buf))))
(defun persp-is-current-buffer (buf &optional include-global)
"Return T if BUF is in the current perspective. When INCLUDE-GLOBAL, also
return T if BUF is in the frame global perspective."
(memq buf (persp-current-buffers* include-global)))
(defun persp-buffer-filter (buf &optional include-global)
"Return F if BUF is in the current perspective. When INCLUDE-GLOBAL, also
return F if BUF is in the frame global perspective. Used for filtering in buffer
display modes like ibuffer."
(not (persp-is-current-buffer buf include-global)))
(defun persp-buffer-list-filter (bufs &optional include-global)
"Return the subset of BUFS which is in the current perspective. When
EXCLUDE-GLOBAL include buffers that are members of the frame global perspective."
(cl-loop for buf in bufs
if (persp-is-current-buffer (get-buffer buf) include-global)
collect buf))
(defun persp-valid-name-p (name)
"Return T if NAME is a valid perspective name."
(and (not (null name))
(not (string= "" name))))
(defun persp-current-name ()
"Get the name of the current perspective."
(persp-name (persp-curr)))
(defun persp-scratch-buffer (&optional name)
(let* ((current-name (persp-current-name))
(name (or name current-name))
(initial-persp (equal name persp-initial-frame-name)))
(concat "*scratch*"
(unless initial-persp
(format " (%s)" name)))))
(defun persp-get-scratch-buffer (&optional name)
"Return the \"*scratch* (NAME)\" buffer.
Create it if the current perspective doesn't have one yet."
(let* ((scratch-buffer-name (persp-scratch-buffer name))
(scratch-buffer (get-buffer scratch-buffer-name)))
;; Do not interfere with an existing scratch buffer's status.
(unless scratch-buffer
(setq scratch-buffer (get-buffer-create scratch-buffer-name))
(with-current-buffer scratch-buffer
(when (eq major-mode 'fundamental-mode)
(funcall initial-major-mode))
(when (and (zerop (buffer-size))
initial-scratch-message)
(insert (substitute-command-keys initial-scratch-message))
(set-buffer-modified-p nil))))
scratch-buffer))
(defun persp-switch-to-scratch-buffer ()
"Switch to the current perspective's scratch buffer.
Create the scratch buffer if there isn't one yet."
(interactive)
(switch-to-buffer (persp-get-scratch-buffer)))
(defalias 'persp-killed-p 'persp-killed
"Return whether the perspective CL-X has been killed.")
(defvar persp-started-after-server-mode nil
"XXX: A nasty workaround for a strange timing bug which occurs
if the Emacs server was started before Perspective initialized.
For some reason, persp-delete-frame gets called multiple times
in unexpected ways. To reproduce: (0) make sure server-start is
called before persp-mode is turned on and comment out the use
of persp-started-after-server-mode, (1) get a session going
with a main frame, (2) switch perspectives a couple of
times, (3) use emacsclient -c to edit a file in a new
frame, (4) C-x 5 0 to kill that frame. This will cause an
unintended perspective switch in the primary frame, and mark
the previous perspective as deleted. There is also a note in
the *Messages* buffer. TODO: It would be good to get to the
bottom of this problem, rather than just paper over it.")
(defvar persp-before-switch-hook nil
"A hook that's run before `persp-switch'.
Run with the previous perspective as `persp-curr'.")
(defvar persp-switch-hook nil
"A hook that's run after `persp-switch'.
Run with the newly created perspective as `persp-curr'.")
(defvar persp-mode-hook nil
"A hook that's run after `persp-mode' has been activated.")
(defvar persp-created-hook nil
"A hook that's run after a perspective has been created.
Run with the newly created perspective as `persp-curr'.")
(defvar persp-killed-hook nil
"A hook that's run just before a perspective is destroyed.
Run with the perspective to be destroyed as `persp-curr'.")
(defvar persp-activated-hook nil
"A hook that's run after a perspective has been activated.
Run with the activated perspective active.")
(defvar persp-before-rename-hook nil
"A hook run immediately before renaming a perspective.")
(defvar persp-after-rename-hook nil
"A hook run immediately after renaming a perspective.")
(defvar persp-state-before-save-hook nil
"A hook run immediately before saving persp state to disk.")
(defvar persp-state-after-save-hook nil
"A hook run immediately after saving persp state to disk.")
(defvar persp-state-before-load-hook nil
"A hook run immediately before loading persp state from disk.")
(defvar persp-state-after-load-hook nil
"A hook run immediately after loading persp state from disk.")
(defvar persp-mode-map (make-sparse-keymap)
"Keymap for perspective-mode.")
(defvar perspective-map nil
"Sub-keymap for perspective-mode")
(define-prefix-command 'perspective-map)
(when persp-mode-prefix-key
(define-key persp-mode-map persp-mode-prefix-key 'perspective-map))
(define-key perspective-map (kbd "s") 'persp-switch)
(define-key perspective-map (kbd "k") 'persp-remove-buffer)
(define-key perspective-map (kbd "c") 'persp-kill)
(define-key perspective-map (kbd "r") 'persp-rename)
(define-key perspective-map (kbd "a") 'persp-add-buffer)
(define-key perspective-map (kbd "A") 'persp-set-buffer)
(define-key perspective-map (kbd "b") 'persp-switch-to-buffer)
(define-key perspective-map (kbd "B") 'persp-switch-to-scratch-buffer)
(define-key perspective-map (kbd "i") 'persp-import)
(define-key perspective-map (kbd "n") 'persp-next)
(define-key perspective-map (kbd "<right>") 'persp-next)
(define-key perspective-map (kbd "p") 'persp-prev)
(define-key perspective-map (kbd "<left>") 'persp-prev)
(define-key perspective-map (kbd "m") 'persp-merge)
(define-key perspective-map (kbd "u") 'persp-unmerge)
(define-key perspective-map (kbd "g") 'persp-add-buffer-to-frame-global)
(define-key perspective-map (kbd "C-s") 'persp-state-save)
(define-key perspective-map (kbd "C-l") 'persp-state-load)
(define-key perspective-map (kbd "`") 'persp-switch-by-number)
(define-key perspective-map (kbd "1") (lambda () (interactive) (persp-switch-by-number 1)))
(define-key perspective-map (kbd "2") (lambda () (interactive) (persp-switch-by-number 2)))
(define-key perspective-map (kbd "3") (lambda () (interactive) (persp-switch-by-number 3)))
(define-key perspective-map (kbd "4") (lambda () (interactive) (persp-switch-by-number 4)))
(define-key perspective-map (kbd "5") (lambda () (interactive) (persp-switch-by-number 5)))
(define-key perspective-map (kbd "6") (lambda () (interactive) (persp-switch-by-number 6)))
(define-key perspective-map (kbd "7") (lambda () (interactive) (persp-switch-by-number 7)))
(define-key perspective-map (kbd "8") (lambda () (interactive) (persp-switch-by-number 8)))
(define-key perspective-map (kbd "9") (lambda () (interactive) (persp-switch-by-number 9)))
(define-key perspective-map (kbd "0") (lambda () (interactive) (persp-switch-by-number 10)))
(with-eval-after-load 'which-key
(declare-function which-key-add-keymap-based-replacements "which-key.el")
(when (fboundp 'which-key-add-keymap-based-replacements)
(which-key-add-keymap-based-replacements perspective-map
"1" "switch to 1"
"2" "switch to 2"
"3" "switch to 3"
"4" "switch to 4"
"5" "switch to 5"
"6" "switch to 6"
"7" "switch to 7"
"8" "switch to 8"
"9" "switch to 9"
"0" "switch to 10")))
(defun perspectives-hash (&optional frame)
"Return a hash containing all perspectives in FRAME.
FRAME defaults to the currently selected frame. The keys are the
perspectives' names. The values are persp structs, with the
fields NAME, WINDOW-CONFIGURATION, BUFFERS, KILLED, POINT-MARKER,
and LOCAL-VARIABLES.
NAME is the name of the perspective.
WINDOW-CONFIGURATION is the configuration given by
`current-window-configuration' last time the perspective was
saved (if this isn't the current perspective, this is when the
perspective was last active).
BUFFERS is a list of buffer objects that are associated with this
perspective.
KILLED is non-nil if the perspective has been killed.
POINT-MARKER is the point position in the active buffer.
Otherwise, when multiple windows are visiting the same buffer,
all but one of their points will be overwritten.
LOCAL-VARIABLES is an alist from variable names to their
perspective-local values."
;; XXX: This must return a non-nil value to avoid breaking frames initialized
;; with after-make-frame-functions bound to nil.
(or (frame-parameter frame 'persp--hash)
(make-hash-table)))
(defun persp-mode-guard ()
(unless (bound-and-true-p persp-mode)
(persp-error "persp-mode is not active")))
(defun persp-curr (&optional frame)
"Get the current perspective in FRAME.
FRAME defaults to the currently selected frame."
;; XXX: This must return a non-nil value to avoid breaking frames initialized
;; with after-make-frame-functions bound to nil.
(persp-mode-guard)
(or (frame-parameter frame 'persp--curr)
(make-persp-internal)))
(defun persp-last (&optional frame)
"Get the last active perspective in FRAME.
FRAME defaults to the currently selected frame."
;; XXX: Unlike persp-curr, it is unsafe to return a default value of
;; (make-persp-internal) here, since some code assumes (persp-last) can return
;; nil.
(frame-parameter frame 'persp--last))
(defun persp-mode-set-prefix-key (newkey)
"Set NEWKEY as the prefix key to activate persp-mode."
(substitute-key-definition 'perspective-map nil persp-mode-map)
(when newkey
(define-key persp-mode-map newkey 'perspective-map)))
(defvar persp-protected nil
"Whether a perspective error should cause persp-mode to be disabled.
Dynamically bound by `persp-protect'.")
(defface persp-selected-face
'((t (:weight bold :foreground "Blue")))
"The face used to highlight the current perspective on the modeline.")
(defmacro persp-protect (&rest body)
"Wrap BODY to disable persp-mode when it errors out.
This prevents the persp-mode from completely breaking Emacs."
(declare (indent 0))
(let ((persp-protected t))
`(condition-case err
(progn ,@body)
(persp-error
(message "Fatal persp-mode error: %S" err)
(persp-mode -1)))))
(defun persp-error (&rest args)
"Like `error', but mark it as a persp-specific error.
Used along with `persp-protect' to ensure that persp-mode doesn't
bring down Emacs.
ARGS will be interpreted by `format-message'."
(if persp-protected
(signal 'persp-error (list (apply 'format args)))
(apply 'error args)))
(defun check-persp (persp)
"Raise an error if PERSP has been killed."
(cond
((not persp)
(persp-error "Expected perspective, was nil"))
((persp-killed-p persp)
(persp-error "Using killed perspective `%s'" (persp-name persp)))))
(defmacro make-persp (&rest args)
"Create a new perspective struct and put it in `perspectives-hash'.
ARGS is a list of keyword arguments followed by an optional BODY.
The keyword arguments set the fields of the perspective struct.
If BODY is given, it is executed to set the window configuration
for the perspective.
Save point, and current buffer before executing BODY, and then
restore them after. If the current buffer is changed in BODY,
that change is lost when getting out, hence the current buffer
will need to be changed again after calling `make-persp'."
(declare (indent defun))
(let ((keywords))
(while (keywordp (car args))
(dotimes (_ 2) (push (pop args) keywords)))
(setq keywords (reverse keywords))
`(let ((persp (make-persp-internal ,@keywords)))
(with-current-perspective
(setf (persp-local-variables persp) (persp-local-variables (persp-curr))))
(puthash (persp-name persp) persp (perspectives-hash))
(with-perspective (persp-name persp)
,(when args
;; Body form given
`(save-excursion ,@args))
;; If the `current-buffer' changes while in `save-excursion',
;; that change isn't kept when getting out, since the current
;; buffer is saved before executing BODY and restored after.
(run-hooks 'persp-created-hook))
persp)))
(defun persp-save ()
"Save the current perspective state.
Specifically, save the current window configuration and
perspective-local variables to `persp-curr'"
(with-current-perspective
(setf (persp-local-variables (persp-curr))
(mapcar
(lambda (c)
(let ((name (car c)))
(list name (symbol-value name))))
(persp-local-variables (persp-curr))))
(setf (persp-window-configuration (persp-curr)) (current-window-configuration))
(setf (persp-point-marker (persp-curr)) (point-marker))))
(defun persp-names ()
"Return a list of the names of all perspectives on the `selected-frame'.
If `persp-sort' is 'name (the default), then return them sorted
alphabetically. If `persp-sort' is 'access, then return them
sorted by the last time the perspective was switched to, the
current perspective being the first. If `persp-sort' is 'created,
then return them in the order they were created, with the newest
first."
(let ((persps (hash-table-values (perspectives-hash))))
(cond ((eq persp-sort 'name)
(sort (mapcar 'persp-name persps) 'string<))
((eq persp-sort 'access)
(mapcar 'persp-name
(sort persps (lambda (a b)
(time-less-p (persp-last-switch-time b)
(persp-last-switch-time a))))))
((eq persp-sort 'created)
(mapcar 'persp-name
(sort persps (lambda (a b)
(time-less-p (persp-created-time b)
(persp-created-time a)))))))))
(defun persp-all-names (&optional not-frame)
"Return a list of the perspective names for all frames.
Excludes NOT-FRAME, if given."
(cl-reduce 'cl-union
(mapcar
(lambda (frame)
(unless (equal frame not-frame)
(with-selected-frame frame (persp-names))))
(frame-list))))
(defun persp-prompt (&optional default require-match)
"Prompt for the name of a perspective.
DEFAULT is a default value for the prompt.
REQUIRE-MATCH can take the same values as in `completing-read'."
(funcall persp-interactive-completion-function
(concat "Perspective name"
(if default (concat " (default " default ")") "")
": ")
(persp-names)
nil require-match nil nil default))
(defun persp-reset-windows ()
"Remove all windows, ensure the remaining one has no window parameters.
This prevents the propagation of reserved window parameters like
window-side creating perspectives."
(let ((ignore-window-parameters t)
;; Required up to Emacs 27.2 to prevent `delete-window' from
;; updating `window-prev-buffers' for all windows. Allowing
;; to create a fresh window (aka `split-window'), with empty
;; `window-prev-buffers'. If the latter is not empty, other
;; perspectives may pull in buffers of the current one, as a
;; side effect when `persp-reactivate-buffers' is called and
;; the perspective is then switched.
(switch-to-buffer-preserve-window-point nil))
(delete-other-windows
;; XXX: Ugly workaround for problems related to
;; https://github.com/nex3/perspective-el/issues/163 and
;; https://github.com/nex3/perspective-el/issues/167
(when (eq (minibuffer-window) (selected-window))
(previous-window (minibuffer-window))))
(when (ignore-errors
;; Create a fresh window without any window parameters, the
;; selected window is still in a window that may have window
;; parameters we don't want.
(split-window))
;; Delete the selected window so that the only window left has no window
;; parameters.
(delete-window))))
(defun persp-new (name)
"Return a perspective named NAME, or create a new one if missing.
The new perspective will start with only an `initial-major-mode'
buffer called \"*scratch* (NAME)\"."
(or (gethash name (perspectives-hash))
(make-persp :name name
(switch-to-buffer (persp-get-scratch-buffer name))
(persp-reset-windows))))
(defun persp-reactivate-buffers (buffers)
"Raise BUFFERS to the top of the most-recently-selected list.
Returns BUFFERS with all non-living buffers removed.
See also `other-buffer'."
(cl-loop for buf in (reverse buffers)
when (buffer-live-p buf)
collect buf into living-buffers
and do (switch-to-buffer buf)
finally return (nreverse living-buffers)))
(defun persp-set-local-variables (vars)
"Set the local variables given in VARS.
VARS should be an alist of variable names to values."
(dolist (var vars) (apply 'set var)))
(defun persp-intersperse (list interspersed-val)
"Intersperse a value into a list.
Return a new list made from taking LIST and inserting
INTERSPERSED-VAL between every pair of items.
For example, (persp-intersperse '(1 2 3) 'a) gives '(1 a 2 a 3)."
(reverse
(cl-reduce
(lambda (list el) (if list (cl-list* el interspersed-val list) (list el)))
list :initial-value nil)))
(defconst persp-mode-line-map
(let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1] 'persp-mode-line-click)
map))
(defconst persp-header-line-map
(let ((map (make-sparse-keymap)))
(define-key map [header-line down-mouse-1] 'persp-mode-line-click)
map))
(defun persp-mode-line-click (event)
"Select the clicked perspective.
EVENT is the click event triggering this function call."
(interactive "e")
(persp-switch (format "%s" (car (posn-string (event-start event)))))
;; XXX: Force update of modestring because otherwise it's inconsistent with
;; the order of perspectives maintained by persp-sort. The call to
;; persp-update-modestring inside persp-switch happens too early.
(persp-update-modestring))
(defun persp-mode-line ()
"Return the string displayed in the modeline representing the perspectives."
(frame-parameter nil 'persp--modestring))
(defun persp-update-modestring ()
"Update the string to reflect the current perspectives.
Has no effect when `persp-show-modestring' is nil."
(when persp-show-modestring
(let ((open (list (nth 0 persp-modestring-dividers)))
(close (list (nth 1 persp-modestring-dividers)))
(sep (nth 2 persp-modestring-dividers)))
(set-frame-parameter nil 'persp--modestring
(append open
(if persp-modestring-short
(list (persp-current-name))
(persp-intersperse (mapcar 'persp-format-name
(persp-names)) sep))
close)))))
(defun persp-format-name (name)
"Format the perspective name given by NAME for display in the mode line or header line."
(let ((string-name (format "%s" name)))
(if (equal name (persp-current-name))
(propertize string-name 'face 'persp-selected-face)
(cond ((eq persp-show-modestring 'header)
(propertize string-name
'local-map persp-header-line-map
'mouse-face 'header-line-highlight))
((eq persp-show-modestring t)
(propertize string-name
'local-map persp-mode-line-map
'mouse-face 'mode-line-highlight))))))
(defun persp-get-quick (char &optional prev)
"Return the name of the first perspective that begins with CHAR.
Perspectives are sorted alphabetically.
PREV can be the name of a perspective. If it's passed,
this will try to return the perspective alphabetically after PREV.
This is used for cycling between perspectives."
(persp-get-quick-helper char prev (persp-names)))
(defun persp-get-quick-helper (char prev names)
"Helper for `persp-get-quick' using CHAR, PREV, and NAMES."
(if (null names) nil
(let ((name (car names)))
(cond
((and (null prev) (eq (string-to-char name) char)) name)
((equal name prev)
(if (and (not (null (cdr names))) (eq (string-to-char (cadr names)) char))
(cadr names)
(persp-get-quick char)))
(t (persp-get-quick-helper char prev (cdr names)))))))
(defun persp-switch-last ()
"Switch to the perspective accessed before the current one."
(interactive)
(unless (persp-last)
(persp-error "There is no last perspective"))
(persp-switch (persp-name (persp-last))))
(defun persp-switch (name &optional norecord)
"Switch to the perspective given by NAME.
If it doesn't exist, create a new perspective and switch to that.
Switching to a perspective means that all buffers associated with
that perspective are reactivated (see `persp-reactivate-buffers'),
the perspective's window configuration is restored, and the
perspective's local variables are set.
If NORECORD is non-nil, do not update the
`persp-last-switch-time' for the switched perspective."
(interactive "i")
(unless (persp-valid-name-p name)
(setq name (persp-prompt (and (persp-last) (persp-name (persp-last))))))
(if (and (persp-curr) (equal name (persp-current-name))) name
(let ((persp (persp-new name)))
(set-frame-parameter nil 'persp--last (persp-curr))
(unless norecord
(run-hooks 'persp-before-switch-hook))
(persp-activate persp)
(when (fboundp 'persp--set-xref-marker-ring) (persp--set-xref-marker-ring))
(unless norecord
(setf (persp-last-switch-time persp) (current-time))
(run-hooks 'persp-switch-hook))
name)))
(defun persp-switch-by-number (num)
"Switch to the perspective given by NUMBER."
(interactive "NSwitch to perspective number: ")
(let* ((persps (persp-names))
(max-persps (length persps)))
(if (<= num max-persps)
(persp-switch (nth (- num 1) persps))
(message "Perspective number %s not available, only %s exist%s"
num
max-persps
(if (= 1 max-persps) "s" ""))))
;; XXX: Have to force the modestring to update in this case, since the call
;; inside persp-switch happens too early. Otherwise, it may be inconsistent
;; with persp-sort.
(persp-update-modestring))
(defun persp-activate (persp)
"Activate the perspective given by the persp struct PERSP."
(check-persp persp)
(persp-save)
(set-frame-parameter nil 'persp--curr persp)
(persp-reset-windows)
(persp-set-local-variables (persp-local-variables persp))
(setf (persp-buffers persp) (persp-reactivate-buffers (persp-buffers persp)))
(set-window-configuration (persp-window-configuration persp))
(when (marker-position (persp-point-marker persp))
(goto-char (persp-point-marker persp)))
(persp-update-modestring)
;; force update of `current-buffer'
(set-buffer (window-buffer))
(run-hooks 'persp-activated-hook))
(defun persp-switch-quick (char)
"Switch to the first perspective, alphabetically, that begins with CHAR.
Sets `this-command' (and thus `last-command') to (persp-switch-quick . CHAR).
See `persp-switch', `persp-get-quick'."
(interactive "c")
(let ((persp (if (and (consp last-command) (eq (car last-command) this-command))
(persp-get-quick char (cdr last-command))
(persp-get-quick char))))
(setq this-command (cons this-command persp))
(if persp (persp-switch persp)
(persp-error (concat "No perspective name begins with " (string char))))))
(defun persp-next ()
"Switch to next perspective (to the right)."
(interactive)
(let* ((names (persp-names))
(pos (cl-position (persp-current-name) names)))
(cond
((null pos) (persp-find-some))
((= pos (1- (length names)))
(if persp-switch-wrap (persp-switch (nth 0 names))))
(t (persp-switch (nth (1+ pos) names))))))
(defun persp-prev ()
"Switch to previous perspective (to the left)."
(interactive)
(let* ((names (persp-names))
(pos (cl-position (persp-current-name) names)))
(cond
((null pos) (persp-find-some))
((= pos 0)
(if persp-switch-wrap (persp-switch (nth (1- (length names)) names))))
(t (persp-switch (nth (1- pos) names))))))
(defun persp-find-some ()
"Return the name of a valid perspective.
This function tries to return the \"most appropriate\"
perspective to switch to. It tries:
* The perspective given by `persp-last'.
* The \"first\" perspective, based on the ordering of persp-names.
* The main perspective.
* The first existing perspective, alphabetically.
If none of these perspectives can be found, this function will
create a new main perspective and return \"main\"."
(cond
((persp-last) (persp-name (persp-last)))
((> (length (persp-names)) 1) (car (persp-names)))
((gethash persp-initial-frame-name (perspectives-hash)) persp-initial-frame-name)
;; TODO: redundant?
((> (hash-table-count (perspectives-hash)) 0) (car (persp-names)))
(t (persp-activate
(make-persp :name persp-initial-frame-name :buffers (buffer-list)
:window-configuration (current-window-configuration)
:point-marker (point-marker)))
persp-initial-frame-name)))
(defun persp-add-buffer (buffer-or-name)
"Associate BUFFER-OR-NAME with the current perspective.
See also `persp-switch' and `persp-remove-buffer'."
(interactive
(list
(let ((read-buffer-function nil))
(read-buffer "Add buffer to perspective: "))))
(let ((buffer (get-buffer buffer-or-name)))
(if (not (buffer-live-p buffer))
(message "buffer %s doesn't exist" buffer-or-name)
(unless (persp-is-current-buffer buffer)
(push buffer (persp-current-buffers))))))
(defun persp-add-buffer-to-frame-global (buffer-or-name)
"Associate BUFFER-OR-NAME with the frame global perspective.
See also `persp-add-buffer'."
(interactive
(list
(let ((read-buffer-function nil))
(read-buffer "Add buffer to frame global perspective: "))))
(with-perspective persp-frame-global-perspective-name
(persp-add-buffer buffer-or-name)))
(defun persp-set-buffer (buffer-or-name)
"Associate BUFFER-OR-NAME with the current perspective and remove it from any other."
(interactive
(list
(let ((read-buffer-function nil))
(read-buffer "Set buffer to perspective: "))))
(let ((buffer (get-buffer buffer-or-name)))
(if (not (buffer-live-p buffer))
(message "buffer %s doesn't exist" buffer-or-name)
(persp-add-buffer buffer)
;; Do not use the combination "while `persp-buffer-in-other-p'",
;; if the buffer is not removed from other perspectives, it will
;; go into an infinite loop.
(cl-loop for other-persp in (remove (persp-current-name) (persp-all-names))
do (with-perspective other-persp
(persp-forget-buffer buffer))))))
(defun persp-set-frame-global-perspective (buffer-or-name)
"Associate BUFFER-OR-NAME with the frame global perspective and remove it from
any other.
See also `persp-set-buffer'."
(list
(let ((read-buffer-function nil))
(read-buffer "Set buffer to frame global perspective: ")))
(with-perspective persp-frame-global-perspective-name
(persp-set-buffer buffer-or-name)))
(cl-defun persp-buffer-in-other-p (buffer)
"Returns nil if BUFFER is only in the current perspective.
Otherwise, returns (FRAME . NAME), the frame and name of another
perspective that has the buffer.
Prefers perspectives in the selected frame."
(cl-loop for frame in (sort (frame-list) (lambda (_frame1 frame2) (eq frame2 (selected-frame))))
do (cl-loop for persp being the hash-values of (perspectives-hash frame)
if (and (not (and (equal frame (selected-frame))
(equal (persp-name persp) (persp-name (persp-curr frame)))))
(memq buffer (persp-buffers persp)))
do (cl-return-from persp-buffer-in-other-p
(cons frame (persp-name persp)))))
nil)
(defun persp-switch-to-buffer (buffer-or-name)
"Like `switch-to-buffer', but switches to another perspective if necessary."
(interactive
(list
(let ((read-buffer-function nil))
(read-buffer-to-switch "Switch to buffer: "))))
(let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
(if (persp-is-current-buffer buffer)
(switch-to-buffer buffer)
(let ((other-persp (persp-buffer-in-other-p buffer)))
(when (eq (car-safe other-persp) (selected-frame))
(persp-switch (cdr other-persp)))
(switch-to-buffer buffer)))))
(cl-defun persp-maybe-kill-buffer ()
"Don't kill a buffer if it's the only buffer in a perspective.
This is the default behaviour of `kill-buffer'. Perspectives
with only one buffer should keep it alive to prevent adding a
buffer from another perspective, replacing the killed buffer.
Will also cleanup killed buffers form each perspective's list
of buffers containing the buffer to be killed.
This is a hook for `kill-buffer-query-functions'. Don't call
this directly, otherwise the current buffer may be removed or
killed from perspectives.
See also `persp-remove-buffer'."
;; List candidates where the buffer to be killed should be removed
;; instead, whom are perspectives with more than one buffer. This
;; is to allow the buffer to live for perspectives that have it as
;; their only buffer.
(persp-protect
(let* ((buffer (current-buffer))
(bufstr (buffer-name buffer))
candidates-for-removal candidates-for-keeping)
;; XXX: For performance reasons, always allow killing off obviously
;; temporary buffers. According to Emacs convention, these buffers' names
;; start with a space.
(when (string-match-p (rx string-start (one-or-more blank)) bufstr)
(cl-return-from persp-maybe-kill-buffer t))
(dolist (name (persp-names))
(let ((buffer-names (persp-get-buffer-names name)))
(when (member bufstr buffer-names)