-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtorus.el
2690 lines (2464 loc) · 98.7 KB
/
torus.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
;;; torus.el --- A buffer groups manager -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Chimay
;; Author : Chimay
;; Name: Torus
;; Package-Version: 1.10
;; Package-requires: ((emacs "26"))
;; Keywords: files, buffers, groups, persistent, history, layout, tabs
;; URL: https://github.com/chimay/torus
;;; Commentary:
;; If you ever dreamed about creating and switching buffer groups at will
;; in Emacs, Torus is the tool you want.
;;
;; In short, this plugin let you organize your buffers by creating as
;; many buffer groups as you need, add the files you want to it and
;; quickly navigate between :
;;
;; - Buffers of the same group
;; - Buffer groups
;; - Workspaces, ie sets of buffer groups
;;
;; Note that :
;;
;; - A location is a pair (buffer (or filename) . position)
;; - A buffer group, in fact a location group, is called a circle
;; - A set of buffer groups is called a torus (a circle of circles)
;;
;; Original idea by Stefan Kamphausen, see https://www.skamphausen.de/cgi-bin/ska/mtorus
;;
;; See https://github.com/chimay/torus/blob/master/README.org for more details
;;; License
;;; ------------------------------
;; This file is not part of Emacs.
;; 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, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Credits:
;;; ------------------------------
;; Stefan Kamphausen, https://www.skamphausen.de/cgi-bin/ska/mtorus
;; Sebastian Freundt, https://sourceforge.net/projects/mtorus.berlios/
;;; Code:
;;; ------------------------------------------------------------
;;; Requires
;;; ------------------------------
(eval-when-compile
(require 'cl-lib)
(require 'cl-extra)
(require 'seq)
(require 'subr-x))
(declare-function cl-copy-seq "cl-lib")
(declare-function cl-subseq "cl-extra")
(declare-function cl-position "cl-lib")
(declare-function cl-find "cl-lib")
(declare-function cl-remove "cl-lib")
(declare-function seq-intersection "seq")
(declare-function seq-filter "seq")
(declare-function seq-group-by "seq")
(declare-function string-join "subr-x")
;;; Custom
;;; ------------------------------
(defgroup torus nil
"An interface to navigating groups of buffers."
:tag "Torus"
:link '(url-link :tag "Home Page"
"https://github.com/chimay/torus")
:link '(emacs-commentary-link
:tag "Commentary in torus.el" "torus.el")
:prefix "torus-"
:group 'environment
:group 'extensions
:group 'convenience)
(defcustom torus-prefix-key "s-t"
"Prefix key for the torus key mappings.
Will be processed by `kbd'."
:type 'string
:group 'torus)
(defcustom torus-binding-level 1
"Whether to activate optional keybindings."
:type 'integer
:group 'torus)
(defcustom torus-verbosity 1
"Level of verbosity.
1 = normal
2 = light debug
3 = heavy debug."
:type 'integer
:group 'torus)
(defcustom torus-dirname user-emacs-directory
"The directory where the torus are read and written."
:type 'string
:group 'torus)
(defcustom torus-load-on-startup nil
"Whether to load torus on startup of Emacs."
:type 'boolean
:group 'torus)
(defcustom torus-save-on-exit nil
"Whether to save torus on exit of Emacs."
:type 'boolean
:group 'torus)
(defcustom torus-autoread-file nil
"The file to load on startup when `torus-load-on-startup' is t."
:type 'string
:group 'torus)
(defcustom torus-autowrite-file nil
"The file to write before quitting Emacs when `torus-save-on-exit' is t."
:type 'string
:group 'torus)
(defcustom torus-backup-number 3
"Number of backups of torus files."
:type 'integer
:group 'torus)
(defcustom torus-history-maximum-elements 30
"Maximum number of elements in `torus-history' and `torus-meta-history'."
:type 'integer
:group 'torus)
(defcustom torus-maximum-horizontal-split 3
"Maximum number of horizontal split, see `torus-split-horizontally'."
:type 'integer
:group 'torus)
(defcustom torus-maximum-vertical-split 4
"Maximum number of vertical split, see `torus-split-vertically'."
:type 'integer
:group 'torus)
(defcustom torus-display-tab-bar nil
"Whether to display a tab bar in `header-line-format'."
:type 'boolean
:group 'torus)
(defcustom torus-separator-torus-circle " >> "
"String between torus and circle in the dashboard."
:type 'string
:group 'torus)
(defcustom torus-separator-circle-location " > "
"String between circle and location(s) in the dashboard."
:type 'string
:group 'torus)
(defcustom torus-location-separator " | "
"String between location(s) in the dashboard."
:type 'string
:group 'torus)
(defcustom torus-prefix-separator "/"
"String between the prefix and the circle names.
The name of the new circles will be of the form :
\"User_input_prefix `torus-prefix-separator' Name_of_the_added_circle\"
without the spaces. If the user enter a blank prefix,
the added circle names remain untouched."
:type 'string
:group 'torus)
(defcustom torus-join-separator " & "
"String between the names when joining.
The name of the new object will be of the form :
\"Object-1 `torus-join-separator' Object-2\"
without the spaces."
:type 'string
:group 'torus)
;;; Variables
;;; ------------------------------
(defvar torus-meta nil
"List of existing toruses.
You can create new torus with `torus-add-torus'or `torus-add-copy-of-torus'.
Some functions also create a new torus to work with.")
(defvar torus-torus nil
"The torus is a list of circles.
A circle is a list of locations, stored in the form :
\(\"circle name\" locations)
A location is a pair (file . position)
Most recent entries are in the beginning of the lists.")
(defvar torus-history nil
"Alist containing the history of locations in the torus.
Each element is of the form :
\((file . position) . circle)")
(defvar torus-layout nil
"Alist containing split layout of circles.
Each element is of the form:
\(circle . layout)")
(defvar torus-input-history nil
"History of user input.")
(defvar torus-index nil
"Alist giving circles corresponding to torus locations.
Each element has the form :
\((file . position) . circle)
Allow to search among all files of the torus.")
(defvar torus-meta-history nil
"Alist containing the history of locations in all toruses.
Each element is of the form :
\((file . position) . (circle . torus))")
(defvar torus-meta-index nil
"Alist giving circles and toruses corresponding to torus locations.
Each element has the form :
\((file . position) . (circle . torus))
Allows to search among all files of the meta torus.")
(defvar torus-line-col nil
"Alist storing locations and corresponding lines & columns in files.
Each element is of the form :
\((file . position) . (line . column))
Allows to display lines & columns.")
(defvar torus-markers nil
"Alist containing markers to opened files.
Each element is of the form :
\((file . position) . marker)
Contain only the files opened in buffers.")
(defvar torus-original-header-lines nil
"Alist containing orginal header lines, before torus changed it.
Each element is of the form :
\(buffer . original-header-line)")
;;; Extensions
;;; ------------
(defvar torus-extension ".el"
"Extension for torus files.")
;;; Prompts
;;; ------------
(defvar torus--message-reset-choice
"Reset [a] all [m] meta [t] torus [h] history [H] meta-history [l] layout [n] input history\n\
[i] index [I] meta-index [p] line & col [C-m] markers [o] orig header line")
(defvar torus--message-print-choice
"Print [a] all [m] meta [t] torus [h] history [H] meta-history [l] layout [n] input history\n\
[i] index [I] meta-index [p] line & col [C-m] marker [o] orig header line")
(defvar torus--message-alternate-choice
"Alternate [m] in meta torus [t] in torus [c] in circle [T] toruses [C] circles")
(defvar torus--message-reverse-choice
"Reverse [l] locations [c] circle [d] deep : locations & circles")
(defvar torus--message-autogroup-choice
"Autogroup by [p] path [d] directory [e] extension")
(defvar torus--message-batch-choice
"Run on circle files [e] Elisp code [c] Elisp command \n\
[!] Shell command [&] Async Shell command")
(defvar torus--message-layout-choice
"Layout [m] manual [o] one window [h] horizontal [v] vertical [g] grid \n\
main window on [l] left [r] right [t] top [b] bottom")
(defvar torus--message-file-does-not-exist
"File %s does not exist anymore. It will be removed from the torus.")
(defvar torus--message-empty-circle
"No location in circle %s. You can use torus-add-location to fill the circle.")
(defvar torus--message-empty-torus
"Torus is empty. Please use torus-add-location.")
(defvar torus--message-empty-meta
"Meta Torus is empty. Please use torus-add-location.")
(defvar torus--message-existent-location
"Location %s already exists in circle %s")
(defvar torus--message-prefix-circle
"Prefix for the circle of torus %s (leave blank for none) ? ")
(defvar torus--message-circle-name-collision
"Circle name collision. Please add/adjust prefixes to avoid confusion.")
(defvar torus--message-replace-torus
"This will replace the current torus variables. Continue ? ")
;;; Mappings
;;; ------------------------------
(defvar torus-map)
(define-prefix-command 'torus-map)
(defvar torus-map-mouse-torus (make-sparse-keymap))
(defvar torus-map-mouse-circle (make-sparse-keymap))
(defvar torus-map-mouse-location (make-sparse-keymap))
;;; Toolbox
;;; ------------------------------
(defun torus--eval-string (string)
"Eval Elisp code in STRING."
(eval (car (read-from-string (format "(progn %s)" string)))))
(defun torus--equal-car-p (one two)
"Whether the cars of ONE and TWO are equal."
(equal (car one) (car two)))
(defmacro torus--set-ref (ptr list)
"Set pointer PTR as reference to LIST."
`(setq ,ptr ,list))
;; (defun torus--set-ref (ptr list)
;; "Set pointer PTR as reference to LIST.
;; PTR must be quoted."
;; (set ptr list))
(defun torus--set-deref (ptr list)
"Change the list referenced by PTR to LIST."
(setcar ptr (car list))
(setcdr ptr (cdr list))
ptr)
(defun torus--value-assoc (key alist)
"Return value associated with KEY in ALIST."
(cdr (assoc key alist)))
(defun torus--key-rassoc (value alist)
"Return key associated with VALUE in ALIST."
(car (rassoc value alist)))
(defun torus--assoc-delete-all (key alist)
"Remove all elements with key matching KEY in ALIST."
(cl-remove key alist :test 'equal :key 'car))
(when (fboundp 'assoc-delete-all)
(defalias 'torus--assoc-delete-all 'assoc-delete-all))
(defun torus--reverse-assoc-delete-all (value alist)
"Remove all elements with value matching VALUE in ALIST."
(cl-remove value alist :test 'equal :key 'cdr))
(defun torus--directory (object)
"Return the last directory component of OBJECT."
(let* ((filename (pcase object
(`(,(and (pred stringp) one) . ,(pred integerp)) one)
((pred stringp) object)))
(grandpa (file-name-directory (directory-file-name
(file-name-directory
(directory-file-name filename)))))
(relative (file-relative-name filename grandpa)))
(directory-file-name (file-name-directory relative))))
(defun torus--extension-description (object)
"Return the extension description of OBJECT."
(let* ((filename (pcase object
(`(,(and (pred stringp) one) . ,(pred integerp)) one)
((pred stringp) object)))
(extension (file-name-extension filename)))
(when (> torus-verbosity 1)
(message "filename extension : %s %s" filename extension))
(pcase extension
('nil "Nil")
('"" "Ends with a dot")
('"sh" "Shell POSIX")
('"zsh" "Shell Zsh")
('"bash" "Shell Bash")
('"org" "Org mode")
('"el" "Emacs Lisp")
('"vim" "Vim Script")
('"py" "Python")
('"rb" "Ruby")
(_ extension))))
;;; Private Functions
;;; ------------------------------
;;; Strings
;;; ------------
(defun torus--buffer-or-filename (location)
"Return buffer name of LOCATION if existent in `torus-markers', file basename otherwise."
(unless (consp location)
(error "Function torus--buffer-or-filename : wrong type argument"))
(let* ((bookmark (cdr (assoc location torus-markers)))
(buffer (when bookmark
(marker-buffer bookmark))))
(if buffer
(buffer-name buffer)
(file-name-nondirectory (car location)))))
(defun torus--position (location)
"Return position in LOCATION in raw format or in line & column if available.
Line & Columns are available in `torus-line-col'"
(let ((entry (assoc location torus-line-col)))
(if entry
(format " at line %s col %s" (cadr entry) (cddr entry))
(format " at position %s" (cdr location)))))
(defun torus--concise (object)
"Return OBJECT in concise string format.
If OBJECT is a string : simply returns OBJECT.
If OBJECT is \(File . Position) : returns \"File at Position.\"
If OBJECT is \((File . Position) . Circle) : returns
\"Circle > File at Position.\""
(let ((location))
(pcase object
(`((,(and (pred stringp) file) . ,(and (pred integerp) position)) .
(,(and (pred stringp) circle) . ,(and (pred stringp) torus)))
(setq location (cons file position))
(concat torus
torus-separator-torus-circle
circle
torus-separator-circle-location
(torus--buffer-or-filename location)
(torus--position location)))
(`((,(and (pred stringp) file) . ,(and (pred integerp) position)) .
,(and (pred stringp) circle))
(setq location (cons file position))
(concat circle
torus-separator-circle-location
(torus--buffer-or-filename location)
(torus--position location)))
(`(,(and (pred stringp) file) . ,(and (pred integerp) position))
(setq location (cons file position))
(concat (torus--buffer-or-filename location)
(torus--position location)))
((pred stringp) object)
(_ (error "Function torus--concise : wrong type argument")))))
(defun torus--equal-concise-p (one two)
"Whether the concise representations of ONE and TWO are equal."
(equal (torus--concise one)
(torus--concise two)))
(defun torus--short (location)
"Return LOCATION in short string format.
Shorter than concise. Used for dashboard and tabs."
(unless (consp location)
(error "Function torus--short : wrong type argument"))
(let* ((entry (assoc location torus-line-col))
(position (if entry
(format " : %s" (cadr entry))
(format " . %s" (cdr location)))))
(if (equal location (cadar torus-torus))
(concat "[ "
(torus--buffer-or-filename location)
position
" ]")
(concat (torus--buffer-or-filename location)
position))))
(defun torus--dashboard ()
"Display summary of current torus, circle and location."
(if torus-meta
(if (> (length (car torus-torus)) 1)
(let*
((locations (string-join (mapcar #'torus--short
(cdar torus-torus)) " | ")))
(format (concat " %s"
torus-separator-torus-circle
"%s"
torus-separator-circle-location
"%s")
(caar torus-meta)
(caar torus-torus)
locations))
(message torus--message-empty-circle (car (car torus-torus))))
(message torus--message-empty-meta)))
(defun torus--prefix-circles (prefix torus-name)
"Return vars of TORUS-NAME with PREFIX to the circle names."
(unless (and (stringp prefix) (stringp torus-name))
(error "Function torus--prefix-circles : wrong type argument"))
(let* ((entry (cdr (assoc torus-name torus-meta)))
(torus (copy-tree (cdr (assoc "torus" entry))))
(history (copy-tree (cdr (assoc "history" entry)))))
(if (> (length prefix) 0)
(progn
(message "Prefix is %s" prefix)
(dolist (elem torus)
(setcar elem
(concat prefix torus-prefix-separator (car elem))))
(dolist (elem history)
(setcdr elem
(concat prefix torus-prefix-separator (cdr elem)))))
(message "Prefix is blank"))
(list torus history)))
;;; Files
;;; ------------
(defun torus--inside-p (&optional buffer)
"Whether BUFFER (the current location if nil) belongs to the torus."
(let ((filename (buffer-file-name (if buffer
buffer
(current-buffer))))
(locations (append (mapcar 'caar torus-meta-index)
(mapcar 'caar torus-index))))
(member filename locations)))
(defun torus--roll-backups (filename)
"Roll backups of FILENAME."
(unless (stringp filename)
(error "Function torus--roll-backups : wrong type argument"))
(let ((file-list (list filename))
(file-src)
(file-dest))
(dolist (iter (number-sequence 1 torus-backup-number))
(push (concat filename "." (prin1-to-string iter)) file-list))
(while (> (length file-list) 1)
(setq file-dest (pop file-list))
(setq file-src (car file-list))
(when (> torus-verbosity 2)
(message "files %s %s" file-src file-dest))
(when (and file-src (file-exists-p file-src))
(when (> torus-verbosity 2)
(message "copy %s -> %s" file-src file-dest))
(copy-file file-src file-dest t)))))
;;; Build
;;; ------------
(defun torus--build-index ()
"Build `torus-index'."
(setq torus-index nil)
(dolist (circle torus-torus)
(dolist (location (cdr circle))
(let ((location-circle (cons location (car circle))))
(unless (member location-circle torus-index)
(push location-circle torus-index)))))
(setq torus-index (reverse torus-index)))
(defun torus--build-meta-index ()
"Build `torus-meta-index'."
(setq torus-meta-index nil)
(let ((torus-name)
(torus)
(circle-torus)
(index-entry))
(dolist (elem torus-meta)
(setq torus-name (car elem))
(setq torus (cdr (assoc "torus" elem)))
(dolist (circle torus)
(setq circle-torus (cons (car circle) torus-name))
(dolist (location (cdr circle))
(setq index-entry (cons location circle-torus))
(unless (member index-entry torus-meta-index)
(push index-entry torus-meta-index))))))
(setq torus-meta-index (reverse torus-meta-index)))
;;; Updates
;;; ------------
(defun torus--update-history ()
"Add current location to `torus-history'."
(when (and torus-torus
(listp torus-torus)
(car torus-torus)
(listp (car torus-torus))
(> (length (car torus-torus)) 1))
(let* ((circle (car torus-torus))
(circle-name (car circle))
(location (car (cdr circle)))
(location-circle (cons location circle-name)))
(push location-circle torus-history)
(delete-dups torus-history)
(setq torus-history
(cl-subseq torus-history 0
(min (length torus-history)
torus-history-maximum-elements))))))
(defun torus--update-meta-history ()
"Add current location to `torus-meta-history'."
(when (and torus-meta
(listp torus-meta)
(car torus-meta)
(listp (car torus-meta))
(> (length (car torus-meta)) 1))
(let* ((circle (car torus-torus))
(circle-name (car circle))
(torus-name (caar torus-meta))
(location (car (cdr circle)))
(location-circle-torus (cons location
(cons circle-name torus-name))))
(when (> torus-verbosity 2)
(message "Loc circ tor %s" location-circle-torus))
(push location-circle-torus torus-meta-history)
(delete-dups torus-meta-history)
(setq torus-meta-history
(cl-subseq torus-meta-history 0
(min (length torus-meta-history)
torus-history-maximum-elements))))))
(defun torus--update-position ()
"Update position in current location.
Do nothing if file does not match current buffer."
(when (and torus-torus
(listp torus-torus)
(car torus-torus)
(listp (car torus-torus))
(> (length (car torus-torus)) 1))
(let* ((torus-name (caar torus-meta))
(circle-name (caar torus-torus))
(circle-torus (cons circle-name torus-name))
(old-location (car (cdr (car torus-torus))))
(old-here (cdr old-location))
(old-location-circle (cons old-location circle-name))
(old-location-circle-torus (cons old-location circle-torus))
(file (car old-location))
(here (point))
(marker (point-marker))
(line-col (cons (line-number-at-pos) (current-column)))
(new-location (cons file here))
(new-location-circle (cons new-location circle-name))
(new-location-circle-torus (cons new-location circle-torus))
(new-location-line-col (cons new-location line-col))
(new-location-marker (cons new-location marker)))
(when (> torus-verbosity 2)
(message "Update position -->")
(message "here old : %s %s" here old-here)
(message "old-location : %s" old-location)
(message "loc history : %s" (caar torus-history))
(message "loc meta history : %s" (caar torus-meta-history))
(message "assoc index : %s" (assoc old-location torus-index))
(message "assoc meta index : %s" (assoc old-location torus-meta-index)))
(when (and (equal file (buffer-file-name (current-buffer)))
(equal old-location (caar torus-history))
(equal old-location (caar torus-meta-history))
(not (equal here old-here)))
(when (> torus-verbosity 2)
(message "Old location : %s" old-location)
(message "New location : %s" new-location))
(setcar (cdr (car torus-torus)) new-location)
(if (member old-location-circle torus-index)
(setcar (member old-location-circle torus-index)
new-location-circle)
(torus--build-index))
(if (member old-location-circle-torus torus-meta-index)
(setcar (member old-location-circle-torus torus-meta-index)
new-location-circle-torus)
(torus--build-meta-index))
(if (member old-location-circle torus-history)
(setcar (member old-location-circle torus-history)
new-location-circle)
(torus--update-history))
(if (member old-location-circle-torus torus-meta-history)
(setcar (member old-location-circle-torus torus-meta-history)
new-location-circle-torus)
(torus--update-meta-history))
(if (assoc old-location torus-line-col)
(progn
(setcdr (assoc old-location torus-line-col) line-col)
(setcar (assoc old-location torus-line-col) new-location))
(push new-location-line-col torus-line-col))
(if (assoc old-location torus-markers)
(progn
(setcdr (assoc old-location torus-markers) marker)
(setcar (assoc old-location torus-markers) new-location))
(push new-location-marker torus-markers))))))
(defun torus--update-layout ()
"Fill `torus-layout' from missing elements. Delete useless ones."
(let ((circles (mapcar #'car torus-torus)))
(dolist (elem circles)
(unless (assoc elem torus-layout)
(push (cons elem ?m) torus-layout)))
(dolist (elem torus-layout)
(unless (member (car elem) circles)
(setq torus-layout (torus--assoc-delete-all (car elem) torus-layout))))
(setq torus-layout (reverse torus-layout))))
(defun torus--apply-or-fill-layout ()
"Apply layout of current circle, or add default is not present."
(let ((circle-name (caar torus-torus)))
(if (consp (assoc circle-name torus-layout))
(torus-layout-menu (cdr (assoc (caar torus-torus) torus-layout)))
(push (cons circle-name ?m) torus-layout))))
(defun torus--update-input-history (name)
"Add NAME to `torus-input-history' if not already there."
(push name torus-input-history)
(delete-dups torus-input-history)
(setq torus-input-history
(cl-subseq torus-input-history 0
(min (length torus-input-history)
torus-history-maximum-elements))))
(defun torus--update-meta ()
"Update current torus in `torus-meta'."
(torus--update-position)
(when torus-meta
(let ((entry (cdar torus-meta)))
(if (equal '("torus" "history" "layout" "input history")
(mapcar 'car entry))
(progn
(if (assoc "input history" entry)
(setcdr (assoc "input history" (cdar torus-meta)) (cl-copy-seq torus-input-history))
(push (cons "input history" torus-input-history) (cdar torus-meta)))
(if (assoc "layout" entry)
(setcdr (assoc "layout" (cdar torus-meta)) (copy-tree torus-layout))
(push (cons "layout" torus-layout) (cdar torus-meta)))
(if (assoc "history" entry)
(setcdr (assoc "history" (cdar torus-meta)) (copy-tree torus-history))
(push (cons "history" torus-history) (cdar torus-meta)))
(if (assoc "torus" entry)
(setcdr (assoc "torus" (cdar torus-meta)) (copy-tree torus-torus))
(push (cons "torus" torus-torus) (cdar torus-meta))))
;; Reordering if needed
(push (cons "input history" torus-input-history) (cdar torus-meta))
(push (cons "layout" torus-layout) (cdar torus-meta))
(push (cons "history" torus-history) (cdar torus-meta))
(push (cons "torus" torus-torus) (cdar torus-meta))
(setf (cdar torus-meta) (cl-subseq (cdar torus-meta) 0 4))))))
(defun torus--update-from-meta ()
"Update main torus variables from `torus-meta'."
(when (and torus-meta
(listp torus-meta)
(listp (car torus-meta)))
(let ((entry (cdr (car torus-meta))))
(if (assoc "torus" entry)
(setq torus-torus (copy-tree (cdr (assoc "torus" entry))))
(setq torus-torus nil))
(if (assoc "history" entry)
(setq torus-history (copy-tree (cdr (assoc "history" entry))))
(setq torus-history nil))
(if (assoc "layout" entry)
(setq torus-layout (copy-tree (cdr (assoc "layout" entry))))
(setq torus-layout nil))
(if (assoc "input history" entry)
(setq torus-input-history (cl-copy-seq (cdr (assoc "input history" entry))))
(setq torus-input-history nil)))))
(defun torus--jump ()
"Jump to current location (buffer & position) in torus.
Add the location to `torus-markers' if not already present."
(when (and torus-torus
(listp torus-torus)
(car torus-torus)
(listp (car torus-torus))
(> (length (car torus-torus)) 1))
(let* ((location (car (cdr (car torus-torus))))
(circle-name (caar torus-torus))
(torus-name (caar torus-meta))
(circle-torus (cons circle-name torus-name))
(location-circle (cons location circle-name))
(location-circle-torus (cons location circle-torus))
(file (car location))
(position (cdr location))
(bookmark (cdr (assoc location torus-markers)))
(buffer (when bookmark
(marker-buffer bookmark))))
(if (and bookmark buffer (buffer-live-p buffer))
(progn
(when (> torus-verbosity 2)
(message "Found %s in markers" bookmark))
(when (not (equal buffer (current-buffer)))
(switch-to-buffer buffer))
(goto-char bookmark))
(when (> torus-verbosity 2)
(message "Found %s in torus" location))
(when bookmark
(setq torus-markers (torus--assoc-delete-all location torus-markers)))
(if (file-exists-p file)
(progn
(when (> torus-verbosity 1)
(message "Opening file %s at %s" file position))
(find-file file)
(goto-char position)
(push (cons location (point-marker)) torus-markers))
(message (format torus--message-file-does-not-exist file))
(setcdr (car torus-torus) (cl-remove location (cdr (car torus-torus))))
(setq torus-line-col (torus--assoc-delete-all location torus-line-col))
(setq torus-markers (torus--assoc-delete-all location torus-markers))
(setq torus-index (cl-remove location-circle torus-index))
(setq torus-meta-index (cl-remove location-circle-torus torus-meta-index))
(setq torus-history (cl-remove location-circle torus-history))
(setq torus-meta-history (cl-remove location-circle-torus torus-meta-history))))
(torus--update-history)
(torus--update-meta-history)
(torus--tab-bar))
(recenter)))
;;; Switch
;;; ------------
(defun torus--switch (location-circle)
"Jump to circle and location countained in LOCATION-CIRCLE."
(unless (and location-circle
(consp location-circle)
(consp (car location-circle)))
(error "Function torus--switch : wrong type argument"))
(torus--update-position)
(let* ((circle-name (cdr location-circle))
(circle (assoc circle-name torus-torus))
(index (cl-position circle torus-torus :test #'equal))
(before (cl-subseq torus-torus 0 index))
(after (cl-subseq torus-torus index)))
(if index
(setq torus-torus (append after before))
(message "Circle not found.")))
(let* ((circle (cdr (car torus-torus)))
(location (car location-circle))
(index (cl-position location circle :test #'equal))
(before (cl-subseq circle 0 index))
(after (cl-subseq circle index)))
(if index
(setcdr (car torus-torus) (append after before))
(message "Location not found.")))
(torus--jump)
(torus--apply-or-fill-layout))
(defun torus--meta-switch (location-circle-torus)
"Jump to torus, circle and location countained in LOCATION-CIRCLE-TORUS."
(unless (and location-circle-torus
(consp location-circle-torus)
(consp (car location-circle-torus))
(consp (cdr location-circle-torus)))
(error "Function torus--switch : wrong type argument"))
(when (> torus-verbosity 2)
(message "meta switch : location-circle-torus : %s" location-circle-torus))
(torus--update-meta)
(let* ((torus-name (cdr (cdr location-circle-torus)))
(torus (assoc torus-name torus-meta))
(index (cl-position torus torus-meta :test #'equal))
(before (cl-subseq torus-meta 0 index))
(after (cl-subseq torus-meta index)))
(if index
(setq torus-meta (append after before))
(message "Torus not found.")))
(torus--update-from-meta)
(torus--build-index)
(torus--build-meta-index)
(torus--update-layout)
(let* ((circle-name (car (cdr location-circle-torus)))
(circle (assoc circle-name torus-torus))
(index (cl-position circle torus-torus :test #'equal))
(before (cl-subseq torus-torus 0 index))
(after (cl-subseq torus-torus index)))
(if index
(setq torus-torus (append after before))
(message "Circle not found.")))
(let* ((circle (cdr (car torus-torus)))
(location (car location-circle-torus))
(index (cl-position location circle :test #'equal))
(before (cl-subseq circle 0 index))
(after (cl-subseq circle index)))
(if index
(setcdr (car torus-torus) (append after before))
(message "Location not found.")))
(torus--jump)
(torus--apply-or-fill-layout))
;;; Windows
;;; ------------
(defsubst torus--windows ()
"Windows displaying a torus buffer."
(seq-filter (lambda (elem) (torus--inside-p (window-buffer elem)))
(window-list)))
(defun torus--main-windows ()
"Return main window of layout."
(let* ((windows (torus--windows))
(columns (mapcar #'window-text-width windows))
(max-columns (when columns
(eval `(max ,@columns))))
(widest)
(lines)
(max-lines)
(biggest))
(when windows
(dolist (index (number-sequence 0 (1- (length windows))))
(when (equal (nth index columns) max-columns)
(push (nth index windows) widest)))
(setq lines (mapcar #'window-text-height widest))
(setq max-lines (eval `(max ,@lines)))
(dolist (index (number-sequence 0 (1- (length widest))))
(when (equal (nth index lines) max-lines)
(push (nth index widest) biggest)))
(when (> torus-verbosity 2)
(message "toruw windows : %s" windows)
(message "columns : %s" columns)
(message "max-columns : %s" max-columns)
(message "widest : %s" widest)
(message "lines : %s" lines)
(message "max-line : %s" max-lines)
(message "biggest : %s" biggest))
biggest)))
(defun torus--prefix-argument-split (prefix)
"Handle prefix argument PREFIX. Used to split."
(pcase prefix
('(4)
(split-window-below)
(other-window 1))
('(16)
(split-window-right)
(other-window 1))))
;;; Tab bar
;;; ------------
(defun torus--eval-tab ()
"Build tab bar."
(when torus-meta
(let*
((locations (mapcar #'torus--short (cdar torus-torus)))
(tab-string))
(setq tab-string
(propertize (format (concat " %s"
torus-separator-torus-circle)
(caar torus-meta))
'keymap torus-map-mouse-torus))
(setq tab-string
(concat tab-string
(propertize (format (concat "%s"
torus-separator-circle-location)
(caar torus-torus))
'keymap torus-map-mouse-circle)))
(dolist (filepos locations)
(setq tab-string
(concat tab-string (propertize filepos
'keymap torus-map-mouse-location)))
(setq tab-string (concat tab-string torus-location-separator)))
tab-string)))
(defun torus--tab-bar ()
"Display tab bar."
(let* ((main-windows (torus--main-windows))
(current-window (selected-window))
(buffer (current-buffer))
(original (assoc buffer torus-original-header-lines))
(eval-tab '(:eval (torus--eval-tab))))
(when (> torus-verbosity 2)
(pp torus-original-header-lines)
(message "original : %s" original)
(message "cdr original : %s" (cdr original)))
(if (and torus-display-tab-bar
(member current-window main-windows))
(progn
(unless original
(push (cons buffer header-line-format)
torus-original-header-lines))
(unless (equal header-line-format eval-tab)
(when (> torus-verbosity 2)
(message "Set :eval in header-line-format."))
(setq header-line-format eval-tab)))
(when original
(setq header-line-format (cdr original))
(setq torus-original-header-lines
(torus--assoc-delete-all buffer
torus-original-header-lines)))
(message (torus--dashboard)))))
;;; Hooks & Advices
;;; ------------------------------
;;;###autoload
(defun torus-quit ()
"Write torus before quit."
(when torus-save-on-exit
(if torus-autowrite-file