-
Notifications
You must be signed in to change notification settings - Fork 13
/
NotebookTabbing.wl
807 lines (702 loc) · 19 KB
/
NotebookTabbing.wl
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
(* ::Package:: *)
BeginPackage["NotebookTabbing`"];
(* ::Subsection:: *)
(*Exposed Interface*)
(* ::Subsubsection:: *)
(*Formatting*)
$TabBackgroundActive::usage="Active background for tabs";
$TabBackgroundInactive::usage="Inactive tab background";
$TabBorderActive::usage="Active tab border color";
$TabBorderInactive::usage="Active tab border color";
$TabForegroundActive::usage="Tab active text color";
$TabForegroundInactive::usage="Tab inactive text color";
$TabShape::usage="The shape for new tabs";
(* ::Subsubsection:: *)
(*Caches*)
$NotebookCache::usage="The notebook cache";
$ActiveNotebookTab::usage="The active tab tag for a notebook";
(* ::Subsubsection:: *)
(*Tab Objects*)
TabObject::usage="Generates a tab without bindings applied";
TabButton::usage="Binds the switch commands to the tab";
SwitchTabs::usage="Switches to the given tab";
(* ::Subsubsection:: *)
(*Docked Cell Interface*)
TabDockedCell::usage="Configures a tab bar docked cell";
TabDockedCellFileInterface::usage="Configures a tab bar to work with opening and saving files";
SetDockedTabs::usage="Sets up a docked cell of tabs";
(* ::Subsubsection:: *)
(*Docked Tab Editing*)
AddDockedTab::usage="Adds a tab to the docked cell bar";
RemoveDockedTab::usage="Removes a tab from the docked cell bar";
ChangeTabName::usage="Changes the display name of the tab with the specified tag";
ChangeTabTag::usage="Changes the tag value of the tab with the specified tag";
(* ::Subsubsection:: *)
(*Saving and Opening*)
TabNotebookCachePages::usage="Sticks the tab pages into the NotebookDynamicExpression";
TabOpenFile::usage="Opens a file in a new tab";
TabFileSave::usage="Saves a given tab to a file. Only does saving as .nb right now.";
TabNotebookSave::usage="Saves a tab notebook to a folder with a subfolder for the tab files";
TabNotebookSaveBindings::usage="Sets up the save bindings for the notebook";
(* ::Subsubsection:: *)
(*Info*)
GetTabName::usage="Gets the name assigned to a tab in a given notebook";
GetTabInfo::usage="Gets the tabs and names for a notebook";
Begin["`Private`"];
(* ::Subsection:: *)
(*Internals*)
(* ::Subsubsection:: *)
(*Formatting*)
$TabBackgroundActive=GrayLevel[.7];
$TabBorderActive=Blue;
$TabForegroundActive=White;
$TabBackgroundInactive=GrayLevel[.9];
$TabBorderInactive=GrayLevel[.7];
$TabForegroundInactive=GrayLevel[.3];
$TabDockBackgroundColor=GrayLevel[.95];
$TabShape=
With[{height=10,flatspan=30},
Polygon@Join[
Table[
{x,height*LogisticSigmoid[x]},
{x,Range[-4,4,.5]}
],
Table[
{flatspan+8+x,height*LogisticSigmoid[-x]},
{x,Range[-4,4,.5]}
]
]
];
(* ::Subsubsection:: *)
(*Caches*)
(*If[Not@MatchQ[$NotebookCache,_Association],$NotebookCache=<||>];If[Not@MatchQ[$ActiveNotebookTab,_Association],
$ActiveNotebookTab=<||>
];*)
$NotebookCache[nb_]:=
CurrentValue[nb,{TaggingRules,"NotebookTabs"},<||>];
$NotebookCache/:Set[$NotebookCache[nb_],assoc_]:=
(CurrentValue[nb,{TaggingRules,"NotebookTabs"}]=assoc)
$NotebookCache/:SetDelayed[$NotebookCache[nb_],assoc_]:=
(CurrentValue[nb,{TaggingRules,"NotebookTabs"}]:=assoc);
$NotebookCache/:Set[$NotebookCache[nb_][tab_],val_]:=
Lookup[
CurrentValue[nb,{TaggingRules,"NotebookTabs"}]=
Append[
CurrentValue[nb,{TaggingRules,"NotebookTabs"},<||>],
tab->val
],
tab
];
$NotebookCache/:SetDelayed[$NotebookCache[nb_][tab_],val_]:=
(CurrentValue[nb,{TaggingRules,"NotebookTabs"}]=
Append[
CurrentValue[nb,{TaggingRules,"NotebookTabs"},<||>],
tab:>val
];)
$ActiveNotebookTab[nb_]:=
CurrentValue[nb,{TaggingRules,"ActiveTab"},Missing["KeyAbsent",nb]];
$ActiveNotebookTab/:Set[$ActiveNotebookTab[nb_],tab_]:=
(CurrentValue[nb,{TaggingRules,"ActiveTab"}]=tab);
$ActiveNotebookTab/:SetDelayed[$ActiveNotebookTab[nb_],tab_]:=
(CurrentValue[nb,{TaggingRules,"ActiveTab"}]:=tab)
(* ::Subsubsection:: *)
(*Tab Objects*)
TabObject[
notebook:_NotebookObject|Automatic:Automatic,
name_,tag_,ops___]:=
With[{nb:=Replace[notebook,Automatic:>EvaluationNotebook[]]},
Interpretation[
DynamicWrapper[
Graphics[{
Dynamic@
If[($ActiveNotebookTab[nb]===tag),
$TabBackgroundActive,
$TabBackgroundInactive
],
Dynamic@
If[($ActiveNotebookTab[nb]===tag),
EdgeForm@$TabBorderActive,
EdgeForm@$TabBorderInactive
],
$TabShape,{
Dynamic@
If[($ActiveNotebookTab[nb]===tag),
$TabForegroundActive,
$TabForegroundInactive
],
Inset[
name,
Scaled[{.2,0}],
{Left,Bottom}
]
}
},
ImageMargins->{{0,0},{0,3}},
ImagePadding->{{0,0},{0,3}},
PlotRangePadding->None,
ops
],
If[DownValues[SwitchTabs]==={},
CloudGet@
"https://www.wolframcloud.com/objects/user-affd7b1c-ecb6-4ccc-8cc4-4d107e2bf04a/NotebookTabbing.m"
]
],
tag]
];
SwitchTabs::nonb="NotebookObject `` does not exist";
SwitchTabs[notebook_NotebookObject,tab_]:=
If[$ActiveNotebookTab[notebook]=!=tab,
(*Cache current state*)
Replace[
$NotebookCache[notebook],
_Missing:>Set[$NotebookCache[notebook],<||>]
];
With[{nb=NotebookGet@notebook},
If[nb=!=$Failed,
Replace[
$ActiveNotebookTab[notebook],
Except[Missing["KeyAbsent",notebook]]:>
Set[
$NotebookCache[notebook][$ActiveNotebookTab[notebook]],
Compress@First@nb
]
];
(*Set new tab*)
$ActiveNotebookTab[notebook]=tab;
(*Insert cached notebook values into notebook*)
NotebookPut[
ReplacePart[NotebookGet@notebook,
1->
Uncompress@
Replace[
$NotebookCache[notebook][tab],
_Missing:>
Set[
$NotebookCache[notebook][tab],
Compress@{}
]
]
],
notebook
],
Message[SwitchTabs::nonb,notebook];$Failed
]
];
tab
];
SwitchTabs[Automatic,tab_]:=
SwitchTabs[EvaluationNotebook[],tab]
TabButton[
notebook:_NotebookObject|Automatic:Automatic,
name_,tag_,
command_:SwitchTabs]:=
EventHandler[
TabObject[notebook,name,tag],
"MouseClicked":>
(
If[command===SwitchTabs&&DownValues[SwitchTabs]==={},
CloudGet@"https://www.wolframcloud.com/objects/user-affd7b1c-ecb6-4ccc-8cc4-4d107e2bf04a/NotebookTabbing.m"
];
command[notebook,tag]
)
];
(* ::Subsubsection:: *)
(*Docked Cell Interface*)
TabDockedCell[notebook_,tabSpecs:{__Rule},ops___]:=
Cell[BoxData@
TagBox[
PaneBox[
RowBox@
Table[
ToBoxes@
TabButton[
Automatic,
Sequence@@t
],
{t,tabSpecs}],
ImageSize->Scaled[1]
],
"Tabs"],
ops,
CellMargins->{{0, 0}, {0, 0}},
CellFrame->{{0,0},{2,0}},
CellFrameColor->Dynamic@$TabBorderActive,
CellFrameMargins->{{0, 0}, {-2, 3}},
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qeET9I0XnlPcU56zUYDDUoP9Dp/CVF1zeoq3tT+I3lLfq9XlBQQGNjY+Ry
ucjpdKqlhsPhoPHxcaqvr+f+Gzw2mBv97gEShIEPvAZirnXTZK+qqpKJKAAU
HWE9Ho9HZn8mk+kW+jwFuoELTEnRnG0Dj0vR86pcp8XmqKiooOPjYxlGoVCI
uNQTCLA80ejoaNhisVBGRgZhPKwXAGei5P9cr8D/CWjRzZmTNc7OzngsFIlE
KBwO/0uHtdl8Pl94e3s7vLu7Szs7OzFQp5ZbW1tydXU1z+s1YzRROK9deg32
/4AWqq2t5T3SJ3LRFK/B5vV6aXZ2lubn52lubi4G183MzCRkenpaLaemppTi
4mLKzMycx7w1YP4smsbp6amqsb+/T0VFRYnun7SA1reaBq85a7jdbrVtcXGR
NjY2aG1tjdbX11NidXWV+0UmJyc1rX/MVV1dHdXU1Dzomqg2MjLCOcjnqKus
rIzzSd7b21N17Xa7uvacs4qipATnIJcnJydKSUlJpLCw8H3WwDPLysPDw6qG
3+9X40gnx1iD8395eVn9rsjPz3+GNSorK9U1Ly8vp+bm5rT9s/EYuHtnZyef
Mb/k5OTwmeniNVhZWZFZd2FhQY2D40nVtLgODg5k+CbM07jYI66Ghgay2Wwy
8owODw/V91gnVdPimpiY4D2oQEP71nLxfsA5JPP4dONN2TguoDQ1NUWQT1/C
p3aHuEQOy0tLS2nPkxbX5uYmn4mUnZ19VYzBjPlSNUpLS+Wjo6NYDvLcpkIw
GFSH0t/fz/H+lpWVla/dIzhTnNCNDA0N8cEaSUD4P9DfNwp0AthrvBa3xP2h
3ef8/U2NjY3U0dFBra2t1NbWlhTt7e3U1dVFvI5Wq5VaWlq0s+OipPumyc3N
vQHtn3Hf3sGzPy8vz4fyLnLvJzzfxvOPYAd4Ufc98GCuvwZfoPunUvQ74GPw
IXgX5+yLIvbY99XfExVSWA==
"], "Byte", ColorSpace -> "RGB", Interleaving -> True];
TabDockedCellFileInterface[notebook_,tabSpecs:{__Rule},ops___]:=
With[{c=TabDockedCell[notebook,tabSpecs,ops]},
Replace[
c,
Cell[BoxData[TagBox[
PaneBox[
RowBox@{
tabs___
},
po___],
"Tabs"
]],
co___]:>
With[{addition=Row[{
Button[
Mouseover[
Framed[saveIcon,
RoundingRadius->5,
FrameStyle->None
],
Framed[saveIcon,
Background->GrayLevel[.98],
RoundingRadius->5,
FrameStyle->GrayLevel[.7]
]
],
TabFileSave[notebook,$ActiveNotebookTab[notebook]],
Method->"Queued",
Appearance->"Frameless"
],
Button[
Mouseover[
Framed[openIcon,
RoundingRadius->5,
FrameStyle->None
],
Framed[openIcon,
Background->GrayLevel[.98],
RoundingRadius->5,
FrameStyle->GrayLevel[.7]
]
],
TabOpenFile[notebook],
Method->"Queued",
Appearance->"Frameless"
],
Spacer@5},
(*Frame\[Rule]True,
FrameStyle\[Rule]GrayLevel[.85],*)
BaselinePosition->Scaled[.2]
]},
Cell[
BoxData[
RowBox@{
TagBox[
PaneBox[
RowBox@{
tabs
},
ImageSize->With[{w=First@ImageDimensions@Rasterize@addition},
Dynamic[
(First@(WindowSize/.AbsoluteOptions[EvaluationNotebook[],WindowSize]))-w]
],
po
],
"Tabs"
],
ToBoxes@addition
}
],
co]
]
]
];
tabRow[tabNum_Integer,topMost:_Integer:1]:=
With[{tab=Graphics[{EdgeForm@$TabBorderInactive,$TabBackgroundInactive,$TabShape},
PlotRangePadding->None,
Method->{"ShrinkWrap"->True},
ImageSize->100,
ImagePadding->None]},
With[{baseSize=ImageDimensions@tab},
Graphics[
Table[
Inset[
tab,
{.85*(tabNum-i)*First@baseSize,0},
{Left,Center},
baseSize
],
{i,tabNum}]//Append[
Delete[#,-topMost],
#[[-topMost]]
]&,
ImageSize->{.85*tabNum*First@baseSize,Last@baseSize},
PlotRangePadding->None,
ImageMargins->None
]
]
]
SetDockedTabs[
notebook:_NotebookObject|Automatic:Automatic,
tabSpecs:{__Rule},
cellFunction:(DockedCells->_):(DockedCells->TabDockedCell),
ops___
]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
SetOptions[nb,
DockedCells->(Last@cellFunction)[notebook,tabSpecs,ops]
];
$ActiveNotebookTab[nb]=Last@First@tabSpecs;
If[MatchQ[$NotebookCache[nb],_Missing],
$NotebookCache[nb]=<||>
];
$NotebookCache[nb][Last@First@tabSpecs]=First@NotebookGet@nb;
];
SetDockedTabs[
notebook:_NotebookObject|Automatic:Automatic,
t_Rule,
ops___]:=
SetDockedTabs[notebook,{t},ops];
(* ::Subsubsection:: *)
(*Docked Tab Editing*)
AddDockedTab[
notebook:_NotebookObject|Automatic:Automatic,
name_,tag_,
cachedNotebook:_List:{}]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
With[{b=TabButton[notebook,name,tag]},
Replace[DockedCells/.Options[nb,DockedCells],
Cell[
stuff_,
ops___
]:>(
SetOptions[nb,
DockedCells->Cell[
stuff/.TagBox[PaneBox[
RowBox@tabs_List,
pops___],
"Tabs"]:>
TagBox[PaneBox[
RowBox@Append[tabs,ToBoxes@b],
pops],
"Tabs"],
ops]
];
$NotebookCache[nb][tag]=cachedNotebook;
)
];
]
];
AddDockedTab[
notebook:_NotebookObject|Automatic:Automatic,
name_->tag_,
cachedNotebook:_List:{}]:=
AddDockedTab[notebook,name,tag,cachedNotebook];
RemoveDockedTab[
notebook:_NotebookObject|Automatic:Automatic,
tag_]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
Replace[DockedCells/.Options[nb,DockedCells],
Cell[
stuff_,
ops___
]:>(
SetOptions[nb,
DockedCells->Cell[
stuff/.TagBox[PaneBox[
RowBox@tabs_List,
pops___],
"Tabs"]:>
TagBox[
PaneBox[
RowBox@
DeleteCases[tabs,
TagBox[
InterpretationBox[_,tag],
_
]
],
pops],
"Tabs"],
ops]
];
$NotebookCache[nb][tag]=.;
)
];
];
ChangeTabName[notebook:_NotebookObject|Automatic:Automatic,
tag_,newName_]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
Replace[DockedCells/.Options[nb,DockedCells],
Cell[
stuff_,
ops___
]:>(SetOptions[nb,
DockedCells->Cell[
stuff/.InterpretationBox[o_,tag]:>
With[{eval=
o/.InsetBox[_,blah___]:>InsetBox[newName,blah]},
InterpretationBox[eval,tag]
],
ops]
];
)
];
];
ChangeTabTag[notebook:_NotebookObject|Automatic:Automatic,tag_,newTag_]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
Replace[DockedCells/.Options[nb,DockedCells],
Cell[
stuff_,
ops___
]:>(SetOptions[nb,
DockedCells->Cell[
stuff/.TagBox[InterpretationBox[o_,tag],h_]:>
With[{d=(o/.HoldPattern[e_===tag]:>(e===newTag))},
TagBox[
InterpretationBox[
d,
newTag],
h/.HoldPattern[SwitchTabs[n_,tag]]:>SwitchTabs[n,newTag]
]
],
ops]
];
$NotebookCache[nb][newTag]=$NotebookCache[nb][tag];
$NotebookCache[nb][tag]=.;
)
];
];
(* ::Subsubsection:: *)
(*Saving Opening*)
TabNotebookCachePages[notebook_]:=
With[{
n=Extract[First@Options[notebook,NotebookDynamicExpression],2,Hold],
cache=Replace[$NotebookCache[notebook],_Missing-><||>],
active=Replace[$ActiveNotebookTab[notebook],_Missing->None],
dc=DockedCells/.Options[notebook,DockedCells]},
With[{nbDynamicExpr=
Replace[n,{
Hold[
If[MatchQ[$NotebookCache[_],_Missing],__]|
Null|
None]:>
(NotebookDynamicExpression:>$$cmd),
Hold[CompoundExpression[
If[MatchQ[$NotebookCache[_],_Missing],__],
a__
]
]:>(
NotebookDynamicExpression:>
CompoundExpression[
$$cmd,
a
]
),
Hold[a_]:>(
NotebookDynamicExpression:>
CompoundExpression[
$$cmd,
a
]
)
}/.$$cmd:>
With[{nb=EvaluationNotebook[]},
If[
MatchQ[$NotebookCache[nb],_Missing],
$NotebookCache[nb]=cache;
$ActiveNotebookTab[nb]=active;
With[{d=(dc/._NotebookObject->nb)},
SetOptions[nb,DockedCells->d]
]
]
]
]},
SetOptions[notebook,nbDynamicExpr]
]
];
TabOpenFile[notebook_,file_String?FileExistsQ]:=
With[{stuff=
Switch[file,
_?StringMatchQ[__~~".m"|".wl"],{Cell[BoxData@Import[file,"Text"],"Input"]},
(*_?StringMatchQ[__~~".m"|".wl"],*)
_,Import[file]
]},
AddDockedTab[notebook,
FileNameTake@file,
file,
Replace[
stuff,{
Notebook[cellStuff_,___]:>cellStuff,
e:Except[_List]:>{Cell[BoxData@ToBoxes@e,"Output"]},
l:Except[{(_Cell|_CellGroup)...}]:>(Cell[BoxData@ToBoxes@#,"Output"]&/@l)
}]
]
];
TabOpenFile[notebook_]:=
With[{f=SystemDialogInput["FileOpen",".nb"]},
TabOpenFile[notebook,f]
];
TabFileSave[notebook:_NotebookObject|Automatic:Automatic,
tab_,file_String?(StringMatchQ["*.nb"])]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
Export[file,
DeleteCases[
If[$ActiveNotebookTab[nb]===tab,
NotebookGet@nb,
ReplacePart[
NotebookGet@nb,
1->$NotebookCache[nb][tab]
]
],
(DockedCells->_)
]
]
];
TabFileSave[
notebook:_NotebookObject|Automatic:Automatic,
tab_]:=
With[{f=SystemDialogInput["FileSave",".nb"]},
If[f=!=$Canceled,
TabFileSave[notebook,tab,f]
]
];
TabNotebookSave[
notebook:_NotebookObject|Automatic:Automatic,
fileBaseName_:Automatic]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
With[{baseName=
Replace[fileBaseName,
Automatic:>
Replace[Quiet@NotebookFileName@nb,
$Failed:>SystemDialogInput@"FileSave"
]
]},
If[baseName=!=$Canceled,
With[{folder=DirectoryName@baseName},
NotebookSave[nb,
FileNameJoin@{folder,
FileNameTake@baseName
}];
Quiet@CreateDirectory@FileNameJoin@{folder,
FileBaseName@baseName<>"_saved_tabs"};
Do[
TabFileSave[nb,
First@pair,
FileNameJoin@{folder,
FileBaseName@baseName<>"_saved_tabs",
{ToString@First@pair,"_",ToString@Last@pair}<>".nb"
}
],
{pair,GetTabInfo[nb]}
]
]
]
]
];
TabNotebookSaveBindings[notebook:_NotebookObject|Automatic:Automatic]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
With[{o=NotebookEventActions/.Options[nb,NotebookEventActions]},
SetOptions[nb,
NotebookEventActions->Join[
DeleteCases[
Replace[o,None->{}],
(Rule|RuleDelayed)[{"MenuCommand","Save"|"SaveRename"},_]
],{
{"MenuCommand","Save"}:>(
TabNotebookCachePages@nb;NotebookSave@nb
),
{"MenuCommand","SaveRename"}:>(
TabNotebookCachePages@nb;NotebookSave@nb
)
}]
]
]
];
(* ::Subsubsection:: *)
(*Info*)
GetTabName[notebook_,tag_]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
Replace[DockedCells/.Options[nb,DockedCells],{
Cell[
stuff_,
ops___
]:>Replace[
Cases[stuff,
InterpretationBox[o_,tag]:>
Cases[o,InsetBox[n_,___]:>n,\[Infinity]],
\[Infinity]],{
{}:>None,
{{n_,___},___}:>n
}],
_->None
}]
];
GetTabInfo[notebook_]:=
With[{nb=Replace[notebook,Automatic:>EvaluationNotebook[]]},
Replace[DockedCells/.Options[nb,DockedCells],{
Cell[
stuff_,
ops___
]:>
Cases[stuff,
InterpretationBox[o_,t_]:>
(t->
Replace[
Cases[o,InsetBox[n_,___]:>n,\[Infinity]],
{
{}:>None,
{n_,___}:>n
}]
),
\[Infinity]],
_->None
}]
]
End[];
EndPackage[];
(*CopyFile[StringReplace[NotebookFileName[],".nb"\[Rule]".m"],CloudObject["NotebookTabbing.m",
Permissions\[Rule]"Public"
]
]//First*)