-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathtabs.js
1187 lines (1054 loc) · 43.2 KB
/
tabs.js
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
// Copyright (c) 2006-2011 by Martin Stubenschrott <[email protected]>
// Copyright (c) 2007-2009 by Doug Kearns <[email protected]>
// Copyright (c) 2008-2009 by Kris Maglione <maglione.k at Gmail>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the License.txt file included with this file.
/** @scope modules */
// TODO: many methods do not work with Thunderbird correctly yet
/**
* @instance tabs
*/
const Tabs = Module("tabs", {
requires: ["config"],
init: function () {
this.updateSelectionHistory([config.tabbrowser.mCurrentTab, null]);
// used for the "gb" and "gB" mappings to remember the last :buffer[!] command
this._lastBufferSwitchArgs = "";
this._lastBufferSwitchSpecial = true;
},
_updateTabCount: function () {
statusline.updateField("tabcount", true);
},
_onTabSelect: function () {
// TODO: is all of that necessary?
// I vote no. --Kris
modes.reset();
statusline.updateField("tabcount", true);
this.updateSelectionHistory();
if (options.focuscontent)
setTimeout(function () { liberator.focusContent(true); }, 10); // just make sure, that no widget has focus
},
/**
* @property {Object} The previously accessed tab or null if no tab
* other than the current one has been accessed.
*/
get alternate() {
var tab = this._alternates[1] ? this._alternates[1].get() : null;
return (tab && tab.parentNode) ? tab : null;
},
/**
* @property {Generator} A genenerator that returns all browsers
* in the current window.
*/
get browsers() {
return (function* () {
let browsers = config.tabbrowser.browsers;
for (let i = 0; i < browsers.length; i++)
yield [i, browsers[i]];
})();
},
/**
* @property {number} The number of tabs in the current window.
*/
get count() config.tabbrowser.visibleTabs.length,
/**
* @property {Object} The local options store for the current tab.
*/
get options() {
let store = this.localStore;
if (!("options" in store))
store.options = {};
return store.options;
},
/**
* Returns the local state store for the tab at the specified
* <b>tabIndex</b>. If <b>tabIndex</b> is not specified then the
* current tab is used.
*
* @param {number} tabIndex
* @returns {Object}
*/
// FIXME: why not a tab arg? Why this and the property?
// : To the latter question, because this works for any tab, the
// property doesn't. And the property is so oft-used that it's
// convenient. To the former question, because I think this is mainly
// useful for autocommands, and they get index arguments. --Kris
getLocalStore: function (tabIndex) {
let tab = this.getTab(tabIndex);
if (!tab.liberatorStore)
tab.liberatorStore = {};
return tab.liberatorStore;
},
/**
* @property {Object} The local state store for the currently selected
* tab.
*/
get localStore() this.getLocalStore(),
/**
* @property {Object[]} The array of closed tabs for the current
* session.
*/
get closedTabs() JSON.parse(services.get("sessionStore").getClosedTabData(window)),
/**
* Returns the index of <b>tab</b> or the index of the currently
* selected tab if <b>tab</b> is not specified. This is a 0-based
* index.
*
* @param {Object} tab A tab from the current tab list.
* @returns {number}
*/
index: function (tab) {
if (tab)
return Array.indexOf(config.tabbrowser.visibleTabs, tab);
else
return Array.indexOf(config.tabbrowser.visibleTabs, config.tabbrowser.tabContainer.selectedItem);
},
// TODO: implement filter
/**
* Returns an array of all tabs in the tab list.
*
* @returns {Object[]}
*/
// FIXME: why not return the tab element?
// : unused? Remove me.
get: function () {
let buffers = [];
for (let [i, browser] of this.browsers) {
let title = browser.contentTitle || "(Untitled)";
let uri = browser.currentURI.spec;
let number = i + 1;
buffers.push([number, title, uri]);
}
return buffers;
},
/**
* Returns the index of the tab containing <b>content</b>.
*
* @param {Object} content Either a content window or a content
* document.
*/
// FIXME: Only called once...necessary?
getContentIndex: function (content) {
for (let [i, browser] of this.browsers) {
if (browser.contentWindow == content || browser.contentDocument == content)
return i;
}
return -1;
},
/**
* Returns the tab at the specified <b>index</b> or the currently
* selected tab if <b>index</b> is not specified. This is a 0-based
* index.
*
* @param {number} index The index of the tab required.
* @returns {Object}
*/
getTab: function (index) {
if (index != undefined)
return config.tabbrowser.mTabs[index];
else
return config.tabbrowser.mCurrentTab;
},
/**
* Lists all tabs matching <b>filter</b>.
*
* @param {string} filter A filter matching a substring of the tab's
* document title or URL.
* @param {boolean} showAll
*/
list: function (filter, showAll) {
completion.listCompleter("buffer", filter, null, completion.buffer[showAll ? "ALL" : "VISIBLE"]);
},
/**
* Moves a tab to a new position in the tab list.
*
* @param {Object} tab The tab to move.
* @param {string} spec See {@link Tabs.indexFromSpec}.
* @param {boolean} wrap Whether an out of bounds <b>spec</b> causes
* the destination position to wrap around the start/end of the tab
* list.
*/
move: function (tab, spec, wrap) {
let index = Tabs.indexFromSpec(spec, wrap, false);
index = tabs.getTab(index)._tPos;
config.tabbrowser.moveTabTo(tab, index);
},
/**
* Removes the specified <b>tab</b> from the tab list.
*
* @param {Object} tab The tab to remove.
* @param {number} count How many tabs to remove.
* @param {number} orientation Focus orientation
* 1 - Focus the tab to the right of the remove tab.
* 0 - Focus the alternate tab of the remove tab. if alternate tab is none, same as 1
* -1 - Focus the tab to the left of the remove tab.
* @param {number} forceQuitOnLastTab Whether to quit if the tab being
* deleted is the only tab in the tab list:
* 1 - quit without saving session
* 2 - quit and save session
* @param {boolean} force Close even if the tab is an app tab.
*/
// FIXME: what is quitOnLastTab {1,2} all about then, eh? --djk
remove: function (tab, count, orientation, forceQuitOnLastTab, force) {
let vTabs = config.tabbrowser.visibleTabs;
let removeOrBlankTab = {
Firefox: function (tab) {
if (vTabs.length > 1)
config.tabbrowser.removeTab(tab);
else {
let url = buffer.URL;
if (url != "about:blank" || url != "about:newtab" ||
window.getWebNavigation().sessionHistory.count > 0) {
liberator.open("", liberator.NEW_BACKGROUND_TAB);
config.tabbrowser.removeTab(tab);
}
else
liberator.beep();
}
},
Thunderbird: function (tab) {
if (config.tabbrowser.mTabs.length > 1 && !tab.hasAttribute("first-tab"))
config.tabbrowser.closeTab(tab);
else
liberator.beep();
},
}[config.hostApplication] || function () {};
if (typeof count != "number" || count < 1)
count = 1;
if (options.getPref("browser.tabs.closeWindowWithLastTab") && !forceQuitOnLastTab)
forceQuitOnLastTab = 1;
if (forceQuitOnLastTab >= 1 && config.tabbrowser.mTabs.length <= count) {
if (liberator.windows.length > 1)
window.close();
else
liberator.quit(forceQuitOnLastTab == 2);
return;
}
if (!orientation)
orientation = 0;
let index = vTabs.indexOf(tab);
// should close even if the tab is not visible such as ":tabclose arg"
if (index < 0) {
// XXX: should consider the count variable ?
if (tab.pinned && !force)
liberator.echoerr("Cannot close an app tab [" + tab.label + "]. Use :tabclose!");
else
removeOrBlankTab(tab);
return;
}
let start, end, selIndex = 0;
if (orientation < 0) {
start = Math.max(0, index - count + 1);
end = index;
}
else {
start = index;
end = Math.min(index + count, vTabs.length) - 1;
selIndex = end + 1;
}
if (!force) {
for (; start <= end && vTabs[start].pinned; start++)
liberator.echoerr("Cannot close an app tab [" + vTabs[start].label + "]. Use :tabclose!");
if (start > end)
return;
}
if ((orientation < 0 && 0 < start - 1) || selIndex >= vTabs.length)
selIndex = start - 1;
let currentIndex = vTabs.indexOf(tabs.getTab());
if (start <= currentIndex && currentIndex <= end) {
let selTab = vTabs[selIndex];
if (orientation == 0 &&
config.tabbrowser.mTabContainer.contains(this.alternate) &&
!this.alternate.hidden) // XXX: should be in the visible tabs ?
{
let lastTabIndex = vTabs.indexOf(this.alternate);
if (lastTabIndex < start || end < lastTabIndex)
selTab = this.alternate;
}
config.tabbrowser.mTabContainer.selectedItem = selTab;
}
for (let i = end; i >= start; i--) {
removeOrBlankTab(vTabs[i]);
vTabs.splice(i, 1);
}
},
/**
* Removes all tabs from the tab list except the specified <b>tab</b>.
*
* @param {Object} tab The tab to keep.
*/
keepOnly: function (tab) {
config.tabbrowser.removeAllTabsBut(tab);
},
/**
* Selects the tab at the position specified by <b>spec</b>.
*
* @param {string} spec See {@link Tabs.indexFromSpec}
* @param {boolean} wrap Whether an out of bounds <b>spec</b> causes
* the selection position to wrap around the start/end of the tab
* list.
* @param {boolean} allTabs
*/
select: function (spec, wrap, allTabs) {
let index = Tabs.indexFromSpec(spec, wrap, allTabs);
// FIXME:
if (index == -1)
liberator.beep();
else
config.tabbrowser.mTabContainer.selectedIndex = index;
},
/**
* Reloads the specified tab.
*
* @param {Object} tab The tab to reload.
* @param {boolean} bypassCache Whether to bypass the cache when
* reloading.
*/
reload: function (tab, bypassCache) {
if (bypassCache) {
const flags = Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
config.tabbrowser.getBrowserForTab(tab).reloadWithFlags(flags);
}
else
config.tabbrowser.reloadTab(tab);
},
/**
* Reloads all tabs.
*
* @param {boolean} bypassCache Whether to bypass the cache when
* reloading.
*/
reloadAll: function (bypassCache) {
if (bypassCache) {
for (let i = 0; i < config.tabbrowser.mTabs.length; i++) {
try {
this.reload(config.tabbrowser.mTabs[i], bypassCache);
}
catch (e) {
// FIXME: can we do anything useful here without stopping the
// other tabs from reloading?
}
}
}
else
config.tabbrowser.reloadAllTabs();
},
/**
* Stops loading the specified tab.
*
* @param {Object} tab The tab to stop loading.
*/
stop: function (tab) {
if (config.stop)
config.stop(tab);
else
tab.linkedBrowser.stop();
},
/**
* Stops loading all tabs.
*/
stopAll: function () {
for (let [, browser] of this.browsers)
browser.stop();
},
/**
* Returns tabs containing the specified <b>buffer</b>.
* @param {string} buffer
* @return {array} array of tabs
*/
getTabsFromBuffer: function (buffer) {
if (!buffer || typeof buffer != "string")
return [];
if (buffer == "#")
return [tabs.alternate];
let matches = buffer.match(/^(\d+):?/);
if (matches)
return [tabs.getTab(parseInt(matches[1], 10) - 1)];
else if (liberator.has("tabgroup") && tabGroup.TV) {
matches = buffer.match(/^(.+?)\.(\d+):?/);
if (matches) {
let [, groupName, tabNum] = matches;
tabNum = parseInt(tabNum, 10);
let group = tabGroup.getGroup(groupName);
if (group) {
let tabItem = group.getChild(tabNum - 1);
if (tabItem)
return [tabItem.tab];
}
}
}
matches = [];
let lowerBuffer = buffer.toLowerCase();
let first = tabs.index();
let nbrowsers = config.tabbrowser.browsers.length;
for (let [i, ] of tabs.browsers) {
let index = (i + first) % nbrowsers;
let browser = config.tabbrowser.browsers[index];
let tab = tabs.getTab(index);
let url = browser.contentDocument.location.href;
let title = tab.label.toLowerCase();
if (url == buffer)
return [tab];
if (url.indexOf(buffer) >= 0 || title.indexOf(lowerBuffer) >= 0)
matches.push(tab);
}
return matches;
},
/**
* Selects the tab containing the specified <b>buffer</b>.
*
* @param {string} buffer A string which matches the URL or title of a
* buffer, if it is null, the last used string is used again.
* @param {boolean} allowNonUnique Whether to select the first of
* multiple matches.
* @param {number} count If there are multiple matches select the
* count'th match.
* @param {boolean} reverse Whether to search the buffer list in
* reverse order.
*
*/
// FIXME: help!
switchTo: function (buffer, allowNonUnique, count, reverse) {
if (buffer == "")
return;
if (buffer != null) {
// store this command, so it can be repeated with "B"
this._lastBufferSwitchArgs = buffer;
this._lastBufferSwitchSpecial = allowNonUnique;
}
else {
buffer = this._lastBufferSwitchArgs;
if (allowNonUnique === undefined || allowNonUnique == null) // XXX
allowNonUnique = this._lastBufferSwitchSpecial;
}
if (!count || count < 1)
count = 1;
if (typeof reverse != "boolean")
reverse = false;
let tabItems = tabs.getTabsFromBuffer(buffer);
if (tabItems.length == 0)
liberator.echoerr("No matching buffer for: " + buffer);
else if (tabItems.length == 1)
config.tabbrowser.mTabContainer.selectedItem = tabItems[0];
else if (!allowNonUnique)
liberator.echoerr("More than one match for: " + buffer);
else {
let length = tabItems.length;
if (reverse) {
index = length - count;
while (index < 0)
index += length;
}
else
index = count % length;
config.tabbrowser.mTabContainer.selectedItem = tabItems[index];
}
},
/**
* Clones the specified <b>tab</b> and append it to the tab list.
*
* @param {Object} tab The tab to clone.
* @param {boolean} activate Whether to select the newly cloned tab.
*/
cloneTab: function (tab, activate) {
let newTab = config.tabbrowser.addTab();
Tabs.copyTab(newTab, tab);
if (activate)
config.tabbrowser.mTabContainer.selectedItem = newTab;
return newTab;
},
/**
* Detaches the specified <b>tab</b> and open it in a new window. If no
* tab is specified the currently selected tab is detached.
*
* @param {Object} tab The tab to detach.
*/
detachTab: function (tab) {
if (!tab)
tab = config.tabbrowser.mTabContainer.selectedItem;
services.get("ww")
.openWindow(window, window.getBrowserURL(), null, "chrome,dialog=no,all", tab);
},
/**
* Selects the alternate tab.
*/
selectAlternateTab: function () {
let alternate = tabs.alternate;
liberator.assert(alternate != null && tabs.getTab() != alternate, "No alternate page");
config.tabbrowser.tabContainer.selectedItem = alternate;
},
// NOTE: when restarting a session FF selects the first tab and then the
// tab that was selected when the session was created. As a result the
// alternate after a restart is often incorrectly tab 1 when there
// shouldn't be one yet.
/**
* Sets the current and alternate tabs, updating the tab selection
* history.
*
* @param {Array(Object)} tabs The current and alternate tab.
* @see tabs#alternate
*/
updateSelectionHistory: function (tabs) {
var tab1, tab2;
if (tabs && tabs.length > 1) {
tab1 = tabs[0] ? Cu.getWeakReference(tabs[0]) : null,
tab2 = tabs[1] ? Cu.getWeakReference(tabs[1]) : null;
}
else {
tab1 = Cu.getWeakReference(this.getTab()),
tab2 = this._alternates[0];
}
this._alternates = [tab1, tab2];
}
}, {
copyTab: function (to, from) {
if (!from)
from = config.tabbrowser.mTabContainer.selectedItem;
let tabState = services.get("sessionStore").getTabState(from);
services.get("sessionStore").setTabState(to, tabState);
},
/**
* @param spec can either be:
* - an absolute integer
* - "" for the current tab
* - "+1" for the next tab
* - "-3" for the tab, which is 3 positions left of the current
* - "$" for the last tab
* @param wrap
* @param allTabs
*/
indexFromSpec: function (spec, wrap, allTabs) {
let tabs = allTabs ? config.tabbrowser.mTabs : config.tabbrowser.visibleTabs;
let position = allTabs ?
config.tabbrowser.mTabContainer.selectedIndex :
tabs.indexOf(config.tabbrowser.mCurrentTab);
let length = tabs.length;
let last = length - 1;
if (spec === undefined || spec === "")
return position;
if (typeof spec === "number")
position = spec;
else if (spec === "$")
position = last;
else if (/^[+-]\d+$/.test(spec))
position += parseInt(spec, 10);
else if (/^\d+$/.test(spec))
position = parseInt(spec, 10);
else
return -1;
if (position > last)
position = wrap ? position % length : last;
else if (position < 0)
position = wrap ? ((position % length) + length) % length : 0;
if (config.hostApplication === "Firefox")
return tabs[position]._tPos;
return position;
}
}, {
commands: function () {
commands.add(["bd[elete]", "bw[ipeout]", "bun[load]", "tabc[lose]"],
"Delete current buffer",
function (args) {
let special = args.bang;
let count = args.count;
let arg = args.literalArg;
let orientation = 1;
if (args["-select"] === "lastactive") {
orientation = 0;
} else if (args["-select"] === "left") {
orientation = -1;
}
if (arg) {
let removed = 0;
let matches = arg.match(/^(\d+):?/);
if (matches) {
tabs.remove(tabs.getTab(parseInt(matches[1], 10) - 1), 1, orientation, 0, special);
removed = 1;
}
else {
let str = arg.toLowerCase();
let browsers = config.tabbrowser.browsers;
for (let i = browsers.length - 1; i >= 0; i--) {
let host, title, uri = browsers[i].currentURI.spec;
if (browsers[i].currentURI.schemeIs("about")) {
host = "";
title = "(Untitled)";
}
else {
host = browsers[i].currentURI.host;
title = browsers[i].contentTitle;
}
[host, title, uri] = [host, title, uri].map(String.toLowerCase);
if (host.indexOf(str) >= 0 || uri == str ||
(special && (title.indexOf(str) >= 0 || uri.indexOf(str) >= 0))) {
tabs.remove(tabs.getTab(i), 1, orientation, 0, special);
removed++;
}
}
}
if (removed == 1)
liberator.echomsg("Removed tab: " + arg);
else if (removed > 1)
liberator.echomsg("Removed " + removed + " tabs");
else
liberator.echoerr("No matching tab for: " + arg);
}
else // just remove the current tab
tabs.remove(tabs.getTab(), Math.max(count, 1), orientation, 0, special);
}, {
argCount: "?",
bang: true,
count: true,
options: [
[["-select", "-s"], commands.OPTION_STRING, null,
[["lastactive", "Select last active tab"],
["left", "Select the tab to the left"],
["right", "Select the tab to the right"]]],
],
completer: function (context) completion.buffer(context),
literal: 0
});
commands.add(["keepa[lt]"],
"Execute a command without changing the current alternate buffer",
function (args) {
let alternate = tabs.alternate;
try {
liberator.execute(args[0], null, true);
}
finally {
tabs.updateSelectionHistory([tabs.getTab(), alternate]);
}
}, {
argCount: "+",
completer: function (context) completion.ex(context),
literal: 0
});
commands.add(["tab"],
"Execute a command and tell it to output in a new tab",
function (args) {
try {
liberator.forceNewTab = true;
liberator.execute(args.string, null, true);
}
finally {
liberator.forceNewTab = false;
}
}, {
argCount: "+",
completer: function (context) completion.ex(context),
literal: 0
});
commands.add(["tabd[o]", "bufd[o]"],
"Execute a command in each tab",
function (args) {
let count = tabs.count;
for (let i = 0; i < Math.min(tabs.count, count); i++) {
tabs.select(i);
liberator.execute(args.string, null, true);
}
}, {
argCount: "1",
completer: function (context) completion.ex(context),
literal: 0
});
commands.add(["tabl[ast]", "bl[ast]"],
"Switch to the last tab",
function () tabs.select("$", false),
{ argCount: "0" });
// TODO: "Zero count" if 0 specified as arg
commands.add(["tabp[revious]", "tp[revious]", "tabN[ext]", "tN[ext]", "bp[revious]", "bN[ext]"],
"Switch to the previous tab or go [count] tabs back",
function (args) {
let count = args.count;
let arg = args[0];
// count is ignored if an arg is specified, as per Vim
if (arg) {
if (/^\d+$/.test(arg))
tabs.select("-" + arg, true);
else
liberator.echoerr("Trailing characters");
}
else if (count > 0)
tabs.select("-" + count, true);
else
tabs.select("-1", true);
}, {
argCount: "?",
count: true
});
// TODO: "Zero count" if 0 specified as arg
commands.add(["tabn[ext]", "tn[ext]", "bn[ext]"],
"Switch to the next or [count]th tab",
function (args) {
let count = args.count;
let arg = args[0];
if (arg || count > 0) {
let index;
// count is ignored if an arg is specified, as per Vim
if (arg) {
liberator.assert(/^\d+$/.test(arg), "Trailing characters");
index = arg - 1;
}
else
index = count - 1;
if (index < tabs.count)
tabs.select(index, true);
else
liberator.beep();
}
else
tabs.select("+1", true);
}, {
argCount: "?",
count: true
});
commands.add(["tabr[ewind]", "tabfir[st]", "br[ewind]", "bf[irst]"],
"Switch to the first tab",
function () { tabs.select(0, false); },
{ argCount: "0" });
if (config.hasTabbrowser) {
// TODO: "Zero count" if 0 specified as arg, multiple args and count ranges?
commands.add(["b[uffer]"],
"Switch to a buffer",
function (args) {
let special = args.bang;
let count = args.count;
let arg = args.literalArg;
if (arg && count > 0) {
liberator.assert(/^\d+$/.test(arg), "Trailing characters");
tabs.switchTo(arg, special);
}
else if (count > 0)
tabs.switchTo(count.toString(), special);
else
tabs.switchTo(arg, special);
}, {
argCount: "?",
bang: true,
count: true,
completer: function (context) completion.buffer(context, completion.buffer.ALL),
literal: 0
});
commands.add(["buffers", "files", "ls", "tabs"],
"Show a list of all buffers",
function (args) { tabs.list(args.literalArg, args.bang); }, {
argCount: "?",
bang: true,
literal: 0
});
commands.add(["quita[ll]", "qa[ll]"],
"Quit " + config.name,
function (args) { liberator.quit(false, args.bang); }, {
argCount: "0",
bang: true
});
commands.add(["reloada[ll]"],
"Reload all tab pages",
function (args) { tabs.reloadAll(args.bang); }, {
argCount: "0",
bang: true
});
commands.add(["stopa[ll]"],
"Stop loading all tab pages",
function () { tabs.stopAll(); },
{ argCount: "0" });
// TODO: add count support
commands.add(["tabm[ove]"],
"Move the current tab after tab N",
function (args) {
let arg = args[0];
// FIXME: tabmove! N should probably produce an error
liberator.assert(!arg || /^([+-]?\d+)$/.test(arg), "Trailing characters");
// if not specified, move to after the last tab
tabs.move(config.tabbrowser.mCurrentTab, arg || "$", args.bang);
}, {
argCount: "?",
bang: true
});
commands.add(["tabo[nly]"],
"Close all other tabs",
function () { tabs.keepOnly(config.tabbrowser.mCurrentTab); },
{ argCount: "0" });
commands.add(["tabopen", "t[open]", "tabnew"],
"Open one or more URLs in a new tab",
function (args) {
let special = args.bang;
args = args.string;
if (options.get("activate").has("all", "tabopen"))
special = !special;
let where = special ? liberator.NEW_TAB : liberator.NEW_BACKGROUND_TAB;
if (args)
liberator.open(args, { where: where });
else
liberator.open("", { where: where });
}, {
bang: true,
canonicalize: function (cmd) cmd.replace(/^(to?|tope?|topen|tabopen|tabnew)\b/, 'open'),
completer: function (context) completion.url(context),
literal: 0,
privateData: true
});
commands.add(["tabde[tach]"],
"Detach current tab to its own window",
function () {
liberator.assert(tabs.count > 1, "Can't detach the last tab");
tabs.detachTab(null);
},
{ argCount: "0" });
commands.add(["tabdu[plicate]"],
"Duplicate current tab",
function (args) {
let tab = tabs.getTab();
let activate = args.bang ? true : false;
if (options.get("activate").has("tabopen", "all"))
activate = !activate;
for (let i in util.range(0, Math.max(1, args.count)))
tabs.cloneTab(tab, activate);
}, {
argCount: "0",
bang: true,
count: true
});
// TODO: match window by title too?
// : accept the full :tabmove arg spec for the tab index arg?
// : better name or merge with :tabmove?
commands.add(["taba[ttach]"],
"Attach the current tab to another window",
function (args) {
liberator.assert(args.length <= 2 && !args.some(function (i) !/^\d+$/.test(i)),
"Trailing characters");
let [winIndex, tabIndex] = args.map(function(i) { return parseInt(i, 10) });
let win = liberator.windows[winIndex - 1];
liberator.assert(win, "Window " + winIndex + " does not exist");
liberator.assert(win != window, "Cannot reattach to the same window");
let browser = win.getBrowser();
let dummy = browser.addTab("about:blank");
browser.stop();
// XXX: the implementation of DnD in tabbrowser.xml suggests
// that we may not be guaranteed of having a docshell here
// without this reference?
browser.docShell;
let last = browser.mTabs.length - 1;
browser.moveTabTo(dummy, util.Math.constrain(tabIndex || last, 0, last));
browser.selectedTab = dummy; // required
browser.swapBrowsersAndCloseOther(dummy, config.tabbrowser.mCurrentTab);
}, {
argCount: "+",
completer: function (context, args) {
if (args.completeArg == 0) {
context.filters.push(function ({ item: win }) win != window);
completion.window(context);
}
}
});
}
if (liberator.has("tabs_undo")) {
commands.add(["u[ndo]"],
"Undo closing of a tab",
function (args) {
if (args.length)
args = args[0];
else
args = args.count || 0;
let m = /^(\d+)(:|$)/.exec(args || '1');
if (m)
window.undoCloseTab(Number(m[1]) - 1);
else if (args) {
for (let [i, item] in Iterator(tabs.closedTabs))
if (item.state.entries[item.state.index - 1].url == args) {
window.undoCloseTab(i);
return;
}
liberator.echoerr("No matching closed tab: " + args);
}
}, {
argCount: "?",
completer: function (context) {
context.anchored = false;
context.compare = CompletionContext.Sort.unsorted;
context.filters = [CompletionContext.Filter.textDescription];
context.keys = { text: function ([i, { state: s }]) (i + 1) + ": " + s.entries[s.index - 1].url, description: "[1].title", icon: "[1].image" };
context.completions = Iterator(tabs.closedTabs);
},
count: true,
literal: 0
});
commands.add(["undoa[ll]"],
"Undo closing of all closed tabs",
function (args) {
for (let i in Iterator(tabs.closedTabs))
window.undoCloseTab(0);
},
{ argCount: "0" });
}
if (liberator.has("session")) {