Add optional argument --only-active-window to :session-save.

The new optional argument --only-active-window makes :session-save to
save only the tabs in the currently active window.
This commit is contained in:
Daniel Fiser 2017-02-05 15:58:35 +01:00
parent e487fe441e
commit c092840c04
2 changed files with 17 additions and 7 deletions

View File

@ -723,7 +723,7 @@ Load a session.
[[session-save]] [[session-save]]
=== session-save === session-save
Syntax: +:session-save [*--current*] [*--quiet*] [*--force*] ['name']+ Syntax: +:session-save [*--current*] [*--quiet*] [*--force*] [*--only-active-window*] ['name']+
Save a session. Save a session.
@ -735,6 +735,7 @@ Save a session.
* +*-c*+, +*--current*+: Save the current session instead of the default. * +*-c*+, +*--current*+: Save the current session instead of the default.
* +*-q*+, +*--quiet*+: Don't show confirmation message. * +*-q*+, +*--quiet*+: Don't show confirmation message.
* +*-f*+, +*--force*+: Force saving internal sessions (starting with an underline). * +*-f*+, +*--force*+: Force saving internal sessions (starting with an underline).
* +*-o*+, +*--only-active-window*+: Saves only tabs of the currently active window.
[[set]] [[set]]
=== set === set

View File

@ -214,10 +214,18 @@ class SessionManager(QObject):
data['history'].append(item_data) data['history'].append(item_data)
return data return data
def _save_all(self): def _save_all(self, only_active_window = False):
"""Get a dict with data for all windows/tabs.""" """Get a dict with data for all windows/tabs."""
data = {'windows': []} data = {'windows': []}
for win_id in objreg.window_registry: winlist = objreg.window_registry
if only_active_window:
active_window = QApplication.instance().activeWindow()
winlist = [getattr(active_window, 'win_id', None)]
if winlist[0] is None:
# Fall back to all windows
winlist = objreg.window_registry
for win_id in winlist:
tabbed_browser = objreg.get('tabbed-browser', scope='window', tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id) window=win_id)
main_window = objreg.get('main-window', scope='window', main_window = objreg.get('main-window', scope='window',
@ -255,7 +263,8 @@ class SessionManager(QObject):
name = 'default' name = 'default'
return name return name
def save(self, name, last_window=False, load_next_time=False): def save(self, name, last_window=False, load_next_time=False,
only_active_window=False):
"""Save a named session. """Save a named session.
Args: Args:
@ -278,7 +287,7 @@ class SessionManager(QObject):
log.sessions.error("last_window_session is None while saving!") log.sessions.error("last_window_session is None while saving!")
return return
else: else:
data = self._save_all() data = self._save_all(only_active_window = only_active_window)
log.sessions.vdebug("Saving data: {}".format(data)) log.sessions.vdebug("Saving data: {}".format(data))
try: try:
with qtutils.savefile_open(path) as f: with qtutils.savefile_open(path) as f:
@ -418,7 +427,7 @@ class SessionManager(QObject):
@cmdutils.register(name=['session-save', 'w'], instance='session-manager') @cmdutils.register(name=['session-save', 'w'], instance='session-manager')
@cmdutils.argument('name', completion=usertypes.Completion.sessions) @cmdutils.argument('name', completion=usertypes.Completion.sessions)
def session_save(self, name: str=default, current=False, quiet=False, def session_save(self, name: str=default, current=False, quiet=False,
force=False): force=False, only_active_window=False):
"""Save a session. """Save a session.
Args: Args:
@ -439,7 +448,7 @@ class SessionManager(QObject):
name = self._current name = self._current
assert not name.startswith('_') assert not name.startswith('_')
try: try:
name = self.save(name) name = self.save(name, only_active_window = only_active_window)
except SessionError as e: except SessionError as e:
raise cmdexc.CommandError("Error while saving session: {}" raise cmdexc.CommandError("Error while saving session: {}"
.format(e)) .format(e))