diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 7838e0138..f86faa0da 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1120,7 +1120,8 @@ class CommandDispatcher: raise cmdexc.CommandError("Quickmark '{}' not found!".format(name)) @cmdutils.register(instance='command-dispatcher', scope='window') - def bookmark_add(self, url=None, title=None): + @cmdutils.argument('toggle', flag='t') + def bookmark_add(self, url=None, title=None, toggle=False): """Save the current page as a bookmark, or a specific url. If no url and title are provided, then save the current page as a @@ -1134,6 +1135,8 @@ class CommandDispatcher: Args: url: url to save as a bookmark. If None, use url of current page. title: title of the new bookmark. + toggle: remove the bookmark instead of raising an error if it + already exists. """ if url and not title: raise cmdexc.CommandError('Title must be provided if url has ' @@ -1149,12 +1152,13 @@ class CommandDispatcher: if not title: title = self._current_title() try: - bookmark_manager.add(url, title) + if_added = bookmark_manager.add(url, title, toggle) except urlmarks.Error as e: raise cmdexc.CommandError(str(e)) else: + mes = "Bookmarked {}!" if if_added else "Removed bookmark {}!" message.info(self._win_id, - "Bookmarked {}!".format(url.toDisplayString())) + mes.format(url.toDisplayString())) @cmdutils.register(instance='command-dispatcher', scope='window', maxsplit=0) diff --git a/qutebrowser/browser/urlmarks.py b/qutebrowser/browser/urlmarks.py index 89bec3d9a..9889c8536 100644 --- a/qutebrowser/browser/urlmarks.py +++ b/qutebrowser/browser/urlmarks.py @@ -272,12 +272,17 @@ class BookmarkManager(UrlMarkManager): elif len(parts) == 1: self.marks[parts[0]] = '' - def add(self, url, title): + def add(self, url, title, toggle=False): """Add a new bookmark. + Return True if the bookmark was added, and False if it was + removed (which only happens if toggle is True). + Args: url: The url to add as bookmark. title: The title for the new bookmark. + toggle: remove the bookmark instead of raising an error if it + already exists. """ if not url.isValid(): errstr = urlutils.get_errstring(url) @@ -286,8 +291,13 @@ class BookmarkManager(UrlMarkManager): urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) if urlstr in self.marks: - raise AlreadyExistsError("Bookmark already exists!") + if toggle: + del self.marks[urlstr] + return False + else: + raise AlreadyExistsError("Bookmark already exists!") else: self.marks[urlstr] = title self.changed.emit() self.added.emit(title, urlstr) + return True diff --git a/tests/end2end/features/urlmarks.feature b/tests/end2end/features/urlmarks.feature index 3490cc4a0..383cb492a 100644 --- a/tests/end2end/features/urlmarks.feature +++ b/tests/end2end/features/urlmarks.feature @@ -97,6 +97,12 @@ Feature: quickmarks and bookmarks And I run :bookmark-del Then the bookmark file should not contain "http://localhost:*/data/numbers/6.txt " + Scenario: Toggling a bookmark + When I open data/numbers/7.txt + And I run :bookmark-add + And I run :bookmark-add --toggle + Then the bookmark file should not contain "http://localhost:*/data/numbers/7.txt " + ## quickmarks Scenario: Saving a quickmark (:quickmark-add)