2 * (C) Copyright 2009 Nicholas A. Zigarovich
4 * Use, modification, and distribution are subject to the terms specified in the
12 * - A session-load-window-replace command which is like
13 * window-current-replace, but which also recycles existing windows.
14 * - A session-load-window-current-flatten command which loads all buffers in a
15 * multi-window section in the current window.
16 * - Session files should be more human readable.
17 * - Ability to store arbitrary session data, for example, a buffer's history.
18 * - The previous two features depend on a parser/generator for retroj's
19 * structured plaintext format.
23 * - This module does not work correctly with daemon mode.
27 * - Inhibit loading of the homepage in windows' initial buffers when auto-
29 * - Auto-save the session when the last conkeror window is closed by a
30 * window manager event. Currently no session is saved.
31 * - Consider how and which errors should be handled. Too often we silently
32 * fail and return without telling the user why we are doing so.
38 //// Manual sessions. ////
41 let _session_dir_default = Cc["@mozilla.org/file/directory_service;1"]
42 .getService(Ci.nsIProperties).get("ProfD", Ci.nsIFile);
43 _session_dir_default.append("sessions");
44 if (! _session_dir_default.exists())
45 _session_dir_default.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
47 define_variable("session_dir", _session_dir_default,
48 "Default directory for save/load interactive commands.");
51 * session_get generates and returns a structure containing session
52 * data for the current group of open windows.
54 function session_get () {
57 for_each_window(function (w) {
59 w.buffers.for_each(function (b) {
60 if (b instanceof content_buffer)
61 buffers.push(b.display_uri_string);
63 if (buffers.length == 0)
72 * session_load loads the given session (given as a data structure),
73 * with optional window as the first window to load the session into,
74 * with optional buffer_idx as the buffer index at which to begin
75 * overwriting existing buffers.
77 function session_load (session, window, buffer_idx) {
78 if (! (session[0] && session[0][0]))
79 throw new Error("Invalid 'session' argument.");
82 let bi = buffer_idx != undefined ?
83 buffer_idx : window.buffers.count;
85 // first kill special buffers slated for recycling.
86 let (b, i = (bi == 0 ? 1 : bi),
89 while ((b = window.buffers.get_buffer(i))) {
90 if (b instanceof content_buffer) {
97 (b = window.buffers.get_buffer(0)) &&
98 !(b instanceof content_buffer))
101 create_buffer(window,
102 buffer_creator(content_buffer),
103 OPEN_NEW_BUFFER_BACKGROUND);
104 kill_buffer(b, true);
108 // it is now safe to recycle the remaining buffers.
109 for (let i = 0; session[s][i] != undefined; ++i, ++bi) {
110 let b = window.buffers.get_buffer(bi);
113 let history = b.web_navigation.sessionHistory;
114 history.PurgeHistory(history.count);
116 b.load(session[s][i]);
118 let c = buffer_creator(content_buffer, $load = session[s][i]);
119 create_buffer(window, c, OPEN_NEW_BUFFER_BACKGROUND);
122 for (let b = window.buffers.get_buffer(bi); b;
123 b = window.buffers.get_buffer(bi))
125 kill_buffer(b, true);
130 function make_init_hook (session) {
131 function init_hook (window) {
132 for (let i = 1; session[i] != undefined; ++i) {
133 let c = buffer_creator(content_buffer, $load = session[i]);
134 create_buffer(window, c, OPEN_NEW_BUFFER_BACKGROUND);
140 for (; session[s] != undefined; ++s) {
141 let w = make_window(buffer_creator(content_buffer,
142 $load = session[s][0]));
143 add_hook.call(w, "window_initialize_late_hook",
144 make_init_hook(session[s]));
149 * session_load_window_new loads the given session into new windows.
151 function session_load_window_new (session) {
152 session_load(session);
156 * session_load_window_current loads the given session, with the
157 * session's first window being appended to window. No existing
158 * buffers will be overwritten.
160 function session_load_window_current (session, window) {
161 let w = window ? window : get_recent_conkeror_window();
162 session_load(session, w);
166 * session_load_window_current loads the given session, with the
167 * session's first window replacing the given window. All buffers in
168 * the given window will be overwritten.
170 function session_load_window_current_replace (session, window) {
171 let w = window ? window : get_recent_conkeror_window();
172 session_load(session, w, 0);
176 * session_write writes the given session to the file given by path.
178 function session_write (path, session) {
179 if (! (path instanceof Ci.nsIFile))
180 path = make_file(path);
182 session = session_get();
183 write_text_file(path, JSON.stringify(session));
187 * session_read reads session data from the given file path,
188 * and returns a decoded session structure.
190 function session_read (path) {
191 if (! (path instanceof Ci.nsIFile))
192 path = make_file(path);
193 return JSON.parse(read_text_file(path));
197 * session_remove deletes the given session file.
199 function session_remove (path) {
200 if (! (path instanceof Ci.nsIFile))
201 path = make_file(path);
205 let _session_prompt_file = function (I) {
207 yield I.minibuffer.read_file_path(
208 $prompt = "Session file:",
209 $initial_value = session_dir.path,
215 let _session_file_not_found = function (I, file) {
216 let mb = I ? I.minibuffer : get_recent_conkeror_window().minibuffer;
217 let msg = "Session file not found: " + file.path;
222 interactive("session-save",
223 "Save the current session.",
225 session_write(make_file(yield _session_prompt_file(I)),
229 interactive("session-load-window-new",
230 "Load a session in a new window.",
232 let file = make_file(yield _session_prompt_file(I));
234 _session_file_not_found(I, file);
236 session_load_window_new(session_read(file));
239 interactive("session-load-window-current",
240 "Load a session in new buffers in the current window.",
242 let file = make_file(yield _session_prompt_file(I));
244 _session_file_not_found(I, file);
246 session_load_window_current(session_read(file), I.window);
249 interactive("session-load-window-current-replace",
250 "Replace all buffers in the current window with buffers "+
251 "in the saved session.",
253 let file = make_file(yield _session_prompt_file(I));
255 _session_file_not_found(I, file);
257 session_load_window_current_replace(session_read(file),
261 interactive("session-remove",
262 "Remove a session file.",
264 let file = make_file(yield _session_prompt_file(I));
266 _session_file_not_found(I, file);
268 session_remove(file);
272 //// Auto-save sessions. ////
275 define_variable("session_auto_save_file", "auto-save",
276 "Default filename for the auto-save session.");
278 define_variable("session_auto_save_auto_load", false,
279 'Whether to load the auto-saved session when the browser is started. '+
280 'May be true, false, or "prompt".');
282 function session_auto_save_load_window_new () {
283 session_load_window_new(_session_auto_save_cached);
286 function session_auto_save_load_window_current (window) {
287 session_load_window_current(_session_auto_save_cached, window);
290 function session_auto_save_load_window_current_replace (window) {
291 session_load_window_current_replace(_session_auto_save_cached, window);
294 define_variable("session_auto_save_auto_load_fn",
296 "Function to be called to load the auto-saved session at start-up " +
297 "when URLs are given on the command-line. May be " +
298 "session_auto_save_load_window_new, " +
299 "session_auto_save_load_window_current, or null. If null, the" +
300 "session will not be auto-loaded when URLs are given.");
303 // undefined - we have not tried to cache the auto-save.
304 // null - we have tried to cache the auto-save, but it didn't exist.
305 // object - the cached session object for the auto-save.
306 let _session_auto_save_cached = undefined;
308 let _session_auto_save_file_get = function () {
309 if (session_auto_save_file instanceof Ci.nsIFile)
310 return session_auto_save_file;
311 let f = session_dir.clone();
312 f.append(session_auto_save_file);
316 function session_auto_save_save () {
317 let f = _session_auto_save_file_get();
318 let s = session_get();
325 function session_auto_save_remove () {
326 let f = _session_auto_save_file_get();
331 let _session_auto_save_auto_load = function (user_gave_urls) {
332 if (! session_auto_save_auto_load)
334 if (! _session_auto_save_cached) {
335 _session_file_not_found(null, _session_auto_save_file_get());
339 let window = get_recent_conkeror_window();
340 if (session_auto_save_auto_load == true)
342 else if (session_auto_save_auto_load == "prompt" && !user_gave_urls) {
343 do_load = (yield window.minibuffer.read_single_character_option(
344 $prompt = "Load auto-saved session? (y/n)",
345 $options = ["y", "n"]
348 throw new Error("Invalid value for session_auto_save_auto_load: " +
349 session_auto_save_auto_load);
352 if (user_gave_urls) {
353 if (session_auto_save_auto_load_fn)
354 session_auto_save_auto_load_fn(window);
356 session_auto_save_load_window_current_replace(window);
359 interactive("session-auto-save-load-window-new",
360 "Load the auto-save session in a new window.",
362 if (_session_auto_save_cached == null)
363 _session_file_not_found(I, _session_auto_save_file_get());
365 session_auto_save_load_window_new();
368 interactive("session-auto-save-load-window-current",
369 "Load the auto-save session in new buffers in the current window.",
371 if (_session_auto_save_cached == null)
372 _session_file_not_found(I, _session_auto_save_file_get());
374 session_auto_save_load_window_current(I.window);
377 interactive("session-auto-save-load-window-current-replace",
378 "Replace all buffers in the current window with buffers in the "+
379 "auto-saved session.",
381 if (_session_auto_save_cached == null)
382 _session_file_not_found(I, _session_auto_save_file_get());
384 session_auto_save_load_window_current_replace(I.window);
387 interactive("session-auto-save-remove",
388 "Remove the auto-save session",
389 session_auto_save_remove);
392 //// auto-save-session-mode ////
395 let _session_auto_save_mode_bootstrap = function (b) {
396 remove_hook("window_initialize_late_hook", _session_auto_save_mode_bootstrap);
397 add_hook("create_buffer_hook", session_auto_save_save);
398 add_hook("kill_buffer_hook", session_auto_save_save);
399 add_hook("content_buffer_location_change_hook", session_auto_save_save);
400 let user_gave_urls = false;
401 for (let i = 0; i < command_line.length; ++i) {
402 if (command_line[i][0] != '-') {
403 user_gave_urls = true;
407 co_call(_session_auto_save_auto_load(user_gave_urls));
410 let _session_auto_save_mode_enable = function () {
411 if (_session_auto_save_cached == undefined) {
412 let f = _session_auto_save_file_get();
413 _session_auto_save_cached = f.exists() ? session_read(f) : null;
415 if (conkeror_started) {
416 add_hook("create_buffer_hook", session_auto_save_save);
417 add_hook("kill_buffer_hook", session_auto_save_save);
418 add_hook("content_buffer_location_change_hook", session_auto_save_save);
420 add_hook("window_initialize_late_hook", _session_auto_save_mode_bootstrap);
423 let _session_auto_save_mode_disable = function () {
424 remove_hook("create_buffer_hook", session_auto_save_save);
425 remove_hook("kill_buffer_hook", session_auto_save_save);
426 remove_hook("content_buffer_location_change_hook", session_auto_save_save);
428 remove_hook("window_initialize_late_hook", _session_auto_save_mode_bootstrap);
431 define_global_mode("session_auto_save_mode",
432 _session_auto_save_mode_enable,
433 _session_auto_save_mode_disable);
435 session_auto_save_mode(true);