-
Notifications
You must be signed in to change notification settings - Fork 1
/
undo-kill-buffer.js
74 lines (65 loc) · 2.85 KB
/
undo-kill-buffer.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
// undo kill buffer
var killed_buffer_urls = [];
var killed_buffer_titles = [];
var killed_buffer_histories = [];
add_hook("buffer_kill_before_hook",
function (buffer) {
var hist = buffer.web_navigation.sessionHistory;
if (buffer.display_uri_string && hist) {
killed_buffer_urls.push(buffer.display_uri_string);
killed_buffer_titles.push(buffer.title || "");
killed_buffer_histories.push(hist);
}
return true;
});
// add hook running when killing a buffer
function kill_buffer (buffer, force) {
if (!buffer)
return;
var buffers = buffer.window.buffers;
if (buffers.count == 1 && buffer == buffers.current) {
if (can_kill_last_buffer || force) {
delete_window(buffer.window);
return;
} else
throw interactive_error("Can't kill last buffer.");
}
buffer_kill_before_hook.run(buffer);
buffers.kill_buffer(buffer);
}
interactive("undo-kill-buffer", "Re-open a recently killed buffer",
function restore_killed_buffer (I) {
if (killed_buffer_urls.length !== 0) {
var url;
if (I.prefix_argument) {
url = yield I.minibuffer.read(
$prompt = "Restore killed url:",
$completer = new all_word_completer($completions = killed_buffer_urls,
$get_description = function (x) {
var idx = killed_buffer_urls.indexOf(x);
return killed_buffer_titles[idx];
}),
$default_completion = killed_buffer_urls[killed_buffer_urls.length - 1],
$auto_complete = "url",
$auto_complete_initial = true,
$auto_complete_delay = 0,
$require_match = true);
} else {
url = killed_buffer_urls[killed_buffer_urls.length-1];
}
var window = I.window;
var creator = buffer_creator(content_buffer);
var idx = killed_buffer_urls.indexOf(url);
var buf = creator(window, null);
buf.web_navigation.sessionHistory = killed_buffer_histories[idx];
var original_index = buf.web_navigation.sessionHistory.index;
buf.web_navigation.gotoIndex(original_index);
window.buffers.current = buf;
killed_buffer_urls.splice(idx,1);
killed_buffer_titles.splice(idx,1);
killed_buffer_histories.splice(idx,1);
} else {
I.window.minibuffer.message("No more killed buffer");
}
});
define_key(content_buffer_normal_keymap, "C-/", "undo-kill-buffer");