qutebrowser/qutebrowser/browser/commands.py

844 lines
31 KiB
Python
Raw Normal View History

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2014-06-19 09:04:37 +02:00
2014-04-17 09:44:26 +02:00
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2014-05-27 15:56:44 +02:00
"""Command dispatcher for TabbedBrowser."""
2014-04-17 09:44:26 +02:00
2014-09-22 21:51:09 +02:00
import re
2014-04-29 18:00:22 +02:00
import os
2014-05-03 14:25:22 +02:00
import subprocess
2014-09-22 21:13:42 +02:00
import posixpath
2014-04-29 18:00:22 +02:00
from functools import partial
2014-04-22 12:10:27 +02:00
from PyQt5.QtWidgets import QApplication
2014-06-25 10:03:13 +02:00
from PyQt5.QtCore import Qt, QUrl
2014-04-17 09:44:26 +02:00
from PyQt5.QtGui import QClipboard
from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog
from PyQt5.QtWebKitWidgets import QWebInspector
2014-09-15 17:59:54 +02:00
import pygments
import pygments.lexers
import pygments.formatters
2014-04-17 09:44:26 +02:00
from qutebrowser.commands import userscripts, cmdexc, cmdutils
2014-08-26 19:10:14 +02:00
from qutebrowser.config import config
2014-09-08 10:30:05 +02:00
from qutebrowser.browser import hints, quickmarks, webelem
from qutebrowser.utils import (message, editor, usertypes, log, qtutils,
urlutils)
2014-04-17 09:44:26 +02:00
class CommandDispatcher:
2014-04-17 09:44:26 +02:00
"""Command dispatcher for TabbedBrowser.
Contains all commands which are related to the current tab.
We can't simply add these commands to BrowserTab directly and use
2014-05-17 22:38:07 +02:00
currentWidget() for TabbedBrowser.cmd because at the time
2014-04-17 09:44:26 +02:00
cmdutils.register() decorators are run, currentWidget() will return None.
Attributes:
2014-04-17 17:44:27 +02:00
_tabs: The TabbedBrowser object.
_editor: The ExternalEditor object.
2014-04-17 09:44:26 +02:00
"""
def __init__(self, parent):
"""Constructor.
Args:
parent: The TabbedBrowser for this dispatcher.
"""
2014-04-17 17:44:27 +02:00
self._tabs = parent
self._editor = None
2014-04-17 09:44:26 +02:00
def _current_widget(self):
"""Get the currently active widget from a command."""
widget = self._tabs.currentWidget()
if widget is None:
raise cmdexc.CommandError("No WebView available yet!")
return widget
def _open(self, url, tab, background):
"""Helper function to open a page.
Args:
url: The URL to open as QUrl.
tab: Whether to open in a new tab.
background: Whether to open in the background.
"""
if tab and background:
raise cmdexc.CommandError("Only one of -t/-b can be given!")
elif tab:
self._tabs.tabopen(url, background=False, explicit=True)
elif background:
self._tabs.tabopen(url, background=True, explicit=True)
else:
widget = self._current_widget()
widget.openurl(url)
2014-04-17 09:44:26 +02:00
def _scroll_percent(self, perc=None, count=None, orientation=None):
"""Inner logic for scroll_percent_(x|y).
Args:
perc: How many percent to scroll, or None
count: How many percent to scroll, or None
orientation: Qt.Horizontal or Qt.Vertical
"""
if perc is None and count is None:
perc = 100
elif perc is None:
2014-09-02 21:54:07 +02:00
perc = count
2014-08-26 19:10:14 +02:00
perc = qtutils.check_overflow(perc, 'int', fatal=False)
frame = self._current_widget().page().currentFrame()
m = frame.scrollBarMaximum(orientation)
if m == 0:
return
frame.setScrollBarValue(orientation, int(m * perc / 100))
2014-04-17 09:44:26 +02:00
2014-05-17 22:38:07 +02:00
def _tab_move_absolute(self, idx):
"""Get an index for moving a tab absolutely.
Args:
idx: The index to get, as passed as count.
"""
if idx is None:
return 0
elif idx == 0:
return self._tabs.count() - 1
else:
return idx - 1
def _tab_move_relative(self, direction, delta):
"""Get an index for moving a tab relatively.
Args:
direction: + or - for relative moving, None for absolute.
delta: Delta to the current tab.
"""
if delta is None:
2014-08-25 15:40:48 +02:00
# We don't set delta to 1 in the function arguments because this
# gets called from tab_move which has delta set to None by default.
delta = 1
2014-05-17 22:38:07 +02:00
if direction == '-':
return self._tabs.currentIndex() - delta
elif direction == '+':
return self._tabs.currentIndex() + delta
2014-08-03 00:39:39 +02:00
def _tab_focus_last(self):
"""Select the tab which was last focused."""
if self._tabs.last_focused is None:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("No last focused tab!")
2014-08-03 00:39:39 +02:00
idx = self._tabs.indexOf(self._tabs.last_focused)
if idx == -1:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Last focused tab vanished!")
2014-08-03 00:39:39 +02:00
self._tabs.setCurrentIndex(idx)
2014-05-17 22:38:07 +02:00
def _editor_cleanup(self, oshandle, filename):
"""Clean up temporary file when the editor was closed."""
os.close(oshandle)
try:
os.remove(filename)
except PermissionError:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Failed to delete tempfile...")
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-05-17 22:38:07 +02:00
def tab_close(self, count=None):
"""Close the current/[count]th tab.
Args:
count: The tab index to close, or None
Emit:
quit: If last tab was closed and last-close in config is set to
quit.
"""
tab = self._tabs.cntwidget(count)
if tab is None:
return
2014-05-18 08:18:20 +02:00
self._tabs.close_tab(tab)
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher', name='open',
split=False)
2014-09-13 00:22:27 +02:00
def openurl(self, url, bg=False, tab=False, count=None):
"""Open a URL in the current/[count]th tab.
2014-04-17 09:44:26 +02:00
Args:
2014-09-13 00:22:27 +02:00
url: The URL to open.
bg: Open in a new background tab.
tab: Open in a new tab.
2014-04-17 09:44:26 +02:00
count: The tab index to open the URL in, or None.
"""
try:
2014-09-13 00:22:27 +02:00
url = urlutils.fuzzy_url(url)
2014-06-20 23:57:52 +02:00
except urlutils.FuzzyUrlError as e:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(e)
2014-09-02 21:54:07 +02:00
if tab:
self._tabs.tabopen(url, background=False, explicit=True)
elif bg:
self._tabs.tabopen(url, background=True, explicit=True)
2014-04-17 09:44:26 +02:00
else:
2014-09-02 21:54:07 +02:00
curtab = self._tabs.cntwidget(count)
if curtab is None:
if count is None:
# We want to open a URL in the current tab, but none exists
# yet.
self._tabs.tabopen(url)
else:
# Explicit count with a tab that doesn't exist.
return
else:
curtab.openurl(url)
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher', name='reload')
2014-04-17 09:44:26 +02:00
def reloadpage(self, count=None):
"""Reload the current/[count]th tab.
Args:
count: The tab index to reload, or None.
"""
2014-04-17 17:44:27 +02:00
tab = self._tabs.cntwidget(count)
2014-04-17 09:44:26 +02:00
if tab is not None:
tab.reload()
@cmdutils.register(instance='command-dispatcher')
2014-04-17 09:44:26 +02:00
def stop(self, count=None):
"""Stop loading in the current/[count]th tab.
Args:
count: The tab index to stop, or None.
"""
2014-04-17 17:44:27 +02:00
tab = self._tabs.cntwidget(count)
2014-04-17 09:44:26 +02:00
if tab is not None:
tab.stop()
@cmdutils.register(instance='command-dispatcher', name='print')
2014-09-02 21:54:07 +02:00
def printpage(self, preview=False, count=None):
2014-04-17 09:44:26 +02:00
"""Print the current/[count]th tab.
Args:
2014-09-13 00:22:27 +02:00
preview: Show preview instead of printing.
2014-04-17 09:44:26 +02:00
count: The tab index to print, or None.
"""
2014-08-26 19:10:14 +02:00
if not qtutils.check_print_compat():
2014-09-02 20:44:58 +02:00
# WORKAROUND (remove this when we bump the requirements to 5.3.0)
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(
"Printing on Qt < 5.3.0 on Windows is broken, please upgrade!")
2014-04-17 17:44:27 +02:00
tab = self._tabs.cntwidget(count)
2014-04-17 09:44:26 +02:00
if tab is not None:
2014-09-02 21:54:07 +02:00
if preview:
diag = QPrintPreviewDialog()
diag.setAttribute(Qt.WA_DeleteOnClose)
diag.paintRequested.connect(tab.print)
diag.exec_()
else:
diag = QPrintDialog()
diag.setAttribute(Qt.WA_DeleteOnClose)
diag.open(lambda: tab.print(diag.printer()))
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-04-17 09:44:26 +02:00
def back(self, count=1):
"""Go back in the history of the current tab.
Args:
count: How many pages to go back.
"""
for _ in range(count):
self._current_widget().go_back()
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-04-17 09:44:26 +02:00
def forward(self, count=1):
"""Go forward in the history of the current tab.
Args:
count: How many pages to go forward.
"""
for _ in range(count):
self._current_widget().go_forward()
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-09-02 21:54:07 +02:00
def hint(self, group=webelem.Group.all, target=hints.Target.normal,
2014-09-14 23:56:19 +02:00
*args: {'nargs': '*'}):
2014-04-19 17:50:11 +02:00
"""Start hinting.
Args:
2014-07-29 02:24:04 +02:00
group: The hinting mode to use.
2014-08-02 23:46:27 +02:00
- `all`: All clickable elements.
- `links`: Only links.
- `images`: Only images.
target: What to do with the selected element.
- `normal`: Open the link in the current tab.
- `tab`: Open the link in a new tab.
- `tab-bg`: Open the link in a new background tab.
- `yank`: Yank the link to the clipboard.
- `yank-primary`: Yank the link to the primary selection.
- `fill`: Fill the commandline with the command given as
argument.
2014-08-02 23:46:27 +02:00
- `rapid`: Open the link in a new tab and stay in hinting mode.
- `download`: Download the link.
2014-08-02 23:58:52 +02:00
- `userscript`: Call an userscript with `$QUTE_URL` set to the
2014-08-03 00:40:28 +02:00
link.
- `spawn`: Spawn a command.
2014-08-02 23:46:27 +02:00
*args: Arguments for spawn/userscript/fill.
- With `spawn`: The executable and arguments to spawn.
`{hint-url}` will get replaced by the selected
URL.
- With `userscript`: The userscript to execute.
- With `fill`: The command to fill the statusbar with.
`{hint-url}` will get replaced by the selected
URL.
2014-04-19 17:50:11 +02:00
"""
widget = self._current_widget()
2014-05-27 16:04:45 +02:00
frame = widget.page().mainFrame()
if frame is None:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("No frame focused!")
2014-09-02 21:54:07 +02:00
widget.hintmanager.start(frame, self._tabs.current_url(), group,
2014-09-14 23:16:35 +02:00
target, *args)
@cmdutils.register(instance='command-dispatcher', hide=True)
def follow_hint(self):
"""Follow the currently selected hint."""
self._current_widget().hintmanager.follow_hint()
2014-09-22 22:36:31 +02:00
def _navigate_incdec(self, url, tab, incdec):
"""Helper method for :navigate when `where' is increment/decrement.
Args:
url: The current url.
tab: Whether to open the link in a new tab.
incdec: Either 'increment' or 'decrement'.
"""
encoded = bytes(url.toEncoded()).decode('ascii')
# Get the last number in a string
match = re.match(r'(.*\D|^)(\d+)(.*)', encoded)
if not match:
raise cmdexc.CommandError("No number found in URL!")
pre, number, post = match.groups()
if not number:
raise cmdexc.CommandError("No number found in URL!")
try:
val = int(number)
except ValueError:
raise cmdexc.CommandError("Could not parse number '{}'.".format(
number))
if incdec == 'decrement':
if val <= 0:
raise cmdexc.CommandError("Can't decrement {}!".format(val))
val -= 1
elif incdec == 'increment':
val += 1
else:
raise ValueError("Invalid value {} for indec!".format(incdec))
urlstr = ''.join([pre, str(val), post]).encode('ascii')
new_url = QUrl.fromEncoded(urlstr)
self._open(new_url, tab, background=False)
def _navigate_up(self, url, tab):
"""Helper method for :navigate when `where' is up.
Args:
url: The current url.
tab: Whether to open the link in a new tab.
"""
path = url.path()
if not path or path == '/':
raise cmdexc.CommandError("Can't go up!")
new_path = posixpath.join(path, posixpath.pardir)
url.setPath(new_path)
self._open(url, tab, background=False)
@cmdutils.register(instance='command-dispatcher')
2014-09-22 21:51:09 +02:00
def navigate(self, where: ('prev', 'next', 'up', 'increment', 'decrement'),
tab=False):
2014-09-22 21:13:42 +02:00
"""Open typical prev/next links or navigate using the URL path.
2014-08-03 00:33:39 +02:00
This tries to automatically click on typical _Previous Page_ or
_Next Page_ links using some heuristics.
2014-09-02 21:54:07 +02:00
2014-09-22 21:13:42 +02:00
Alternatively it can navigate by changing the current URL.
2014-09-02 21:54:07 +02:00
Args:
where: What to open.
2014-05-01 15:27:32 +02:00
- `prev`: Open a _previous_ link.
- `next`: Open a _next_ link.
2014-09-22 21:13:42 +02:00
- `up`: Go up a level in the current URL.
2014-09-22 21:51:09 +02:00
- `increment`: Increment the last number in the URL.
- `decrement`: Decrement the last number in the URL.
2014-08-03 00:33:39 +02:00
2014-09-13 00:22:27 +02:00
tab: Open in a new tab.
2014-08-03 00:33:39 +02:00
"""
2014-09-22 21:08:11 +02:00
widget = self._current_widget()
frame = widget.page().currentFrame()
url = self._tabs.current_url()
if frame is None:
raise cmdexc.CommandError("No frame focused!")
if where == 'prev':
2014-09-22 21:08:11 +02:00
widget.hintmanager.follow_prevnext(frame, url, prev=True,
newtab=tab)
elif where == 'next':
2014-09-22 21:08:11 +02:00
widget.hintmanager.follow_prevnext(frame, url, prev=False,
newtab=tab)
2014-09-22 21:13:42 +02:00
elif where == 'up':
2014-09-22 22:36:31 +02:00
self._navigate_up(url, tab)
2014-09-22 21:51:09 +02:00
elif where in ('decrement', 'increment'):
2014-09-22 22:36:31 +02:00
self._navigate_incdec(url, tab, where)
2014-09-22 21:08:11 +02:00
else:
raise ValueError("Got called with invalid value {} for "
"`where'.".format(where))
2014-05-01 15:27:32 +02:00
@cmdutils.register(instance='command-dispatcher', hide=True)
def scroll(self, dx: float, dy: float, count=1):
2014-08-03 00:33:39 +02:00
"""Scroll the current tab by 'count * dx/dy'.
2014-04-17 09:44:26 +02:00
Args:
dx: How much to scroll in x-direction.
dy: How much to scroll in x-direction.
count: multiplier
"""
2014-09-02 21:54:07 +02:00
dx *= count
dy *= count
cmdutils.check_overflow(dx, 'int')
cmdutils.check_overflow(dy, 'int')
self._current_widget().page().currentFrame().scroll(dx, dy)
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher', hide=True)
def scroll_perc(self, perc: float=None,
horizontal: {'flag': 'x'}=False, count=None):
2014-09-02 21:54:07 +02:00
"""Scroll to a specific percentage of the page.
2014-08-03 00:33:39 +02:00
The percentage can be given either as argument or as count.
If no percentage is given, the page is scrolled to the end.
2014-04-17 09:44:26 +02:00
Args:
perc: Percentage to scroll.
2014-09-13 00:22:27 +02:00
horizontal: Scroll horizontally instead of vertically.
2014-04-17 09:44:26 +02:00
count: Percentage to scroll.
"""
2014-09-02 21:54:07 +02:00
self._scroll_percent(perc, count,
Qt.Horizontal if horizontal else Qt.Vertical)
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher', hide=True)
def scroll_page(self, x: float, y: float, count=1):
2014-04-17 09:44:26 +02:00
"""Scroll the frame page-wise.
Args:
2014-08-03 00:33:39 +02:00
x: How many pages to scroll to the right.
y: How many pages to scroll down.
2014-04-17 09:44:26 +02:00
count: multiplier
"""
frame = self._current_widget().page().currentFrame()
size = frame.geometry()
2014-09-02 21:54:07 +02:00
dx = count * x * size.width()
dy = count * y * size.height()
cmdutils.check_overflow(dx, 'int')
cmdutils.check_overflow(dy, 'int')
frame.scroll(dx, dy)
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-09-02 21:54:07 +02:00
def yank(self, title=False, sel=False):
"""Yank the current URL/title to the clipboard or primary selection.
2014-04-17 09:44:26 +02:00
2014-05-18 08:14:11 +02:00
Args:
2014-09-13 00:22:27 +02:00
sel: Use the primary selection instead of the clipboard.
title: Yank the title instead of the URL.
2014-05-18 08:14:11 +02:00
"""
clipboard = QApplication.clipboard()
2014-09-02 21:54:07 +02:00
if title:
s = self._tabs.tabText(self._tabs.currentIndex())
2014-05-19 11:56:51 +02:00
else:
2014-09-02 21:54:07 +02:00
s = self._tabs.current_url().toString(
QUrl.FullyEncoded | QUrl.RemovePassword)
if sel and clipboard.supportsSelection():
2014-05-19 11:56:51 +02:00
mode = QClipboard.Selection
target = "primary selection"
else:
mode = QClipboard.Clipboard
target = "clipboard"
2014-09-02 21:54:07 +02:00
log.misc.debug("Yanking to {}: '{}'".format(target, s))
clipboard.setText(s, mode)
what = 'Title' if title else 'URL'
message.info("{} yanked to {}".format(what, target))
2014-04-17 09:44:26 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-04-17 09:44:26 +02:00
def zoom_in(self, count=1):
"""Increase the zoom level for the current tab.
2014-04-17 09:44:26 +02:00
Args:
2014-08-03 00:33:39 +02:00
count: How many steps to zoom in.
2014-04-17 09:44:26 +02:00
"""
tab = self._current_widget()
2014-04-17 09:44:26 +02:00
tab.zoom(count)
@cmdutils.register(instance='command-dispatcher')
2014-04-17 09:44:26 +02:00
def zoom_out(self, count=1):
"""Decrease the zoom level for the current tab.
2014-04-17 09:44:26 +02:00
Args:
2014-08-03 00:33:39 +02:00
count: How many steps to zoom out.
2014-04-17 09:44:26 +02:00
"""
tab = self._current_widget()
2014-04-17 09:44:26 +02:00
tab.zoom(-count)
2014-04-29 18:00:22 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-05-17 23:22:10 +02:00
def zoom(self, zoom=None, count=None):
2014-08-03 00:33:39 +02:00
"""Set the zoom level for the current tab.
The zoom can be given as argument or as [count]. If neither of both is
given, the zoom is set to 100%.
2014-05-09 14:20:26 +02:00
Args:
2014-08-03 00:33:39 +02:00
zoom: The zoom percentage to set.
count: The zoom percentage to set.
2014-05-09 14:20:26 +02:00
"""
2014-05-09 19:12:08 +02:00
try:
level = cmdutils.arg_or_count(zoom, count, default=100)
except ValueError as e:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(e)
tab = self._current_widget()
2014-05-09 14:20:26 +02:00
tab.zoom_perc(level)
@cmdutils.register(instance='command-dispatcher')
2014-05-17 22:38:07 +02:00
def tab_only(self):
"""Close all tabs except for the current one."""
for tab in self._tabs.widgets():
if tab is self._current_widget():
2014-05-17 22:38:07 +02:00
continue
self._tabs.close_tab(tab)
@cmdutils.register(instance='command-dispatcher')
2014-05-17 23:22:10 +02:00
def undo(self):
2014-08-03 00:33:39 +02:00
"""Re-open a closed tab (optionally skipping [count] closed tabs)."""
2014-05-17 22:38:07 +02:00
if self._tabs.url_stack:
2014-05-18 08:19:27 +02:00
self._tabs.tabopen(self._tabs.url_stack.pop())
2014-05-17 22:38:07 +02:00
else:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Nothing to undo!")
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-05-17 23:22:10 +02:00
def tab_prev(self, count=1):
2014-08-03 00:33:39 +02:00
"""Switch to the previous tab, or switch [count] tabs back.
2014-05-17 22:38:07 +02:00
Args:
count: How many tabs to switch back.
"""
newidx = self._tabs.currentIndex() - count
if newidx >= 0:
self._tabs.setCurrentIndex(newidx)
2014-08-06 08:10:32 +02:00
elif config.get('tabs', 'wrap'):
2014-05-17 22:38:07 +02:00
self._tabs.setCurrentIndex(newidx % self._tabs.count())
else:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("First tab")
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-05-17 23:22:10 +02:00
def tab_next(self, count=1):
2014-08-03 00:33:39 +02:00
"""Switch to the next tab, or switch [count] tabs forward.
2014-05-17 22:38:07 +02:00
Args:
count: How many tabs to switch forward.
"""
newidx = self._tabs.currentIndex() + count
if newidx < self._tabs.count():
self._tabs.setCurrentIndex(newidx)
2014-08-06 08:10:32 +02:00
elif config.get('tabs', 'wrap'):
2014-05-17 22:38:07 +02:00
self._tabs.setCurrentIndex(newidx % self._tabs.count())
else:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Last tab")
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-09-13 00:22:27 +02:00
def paste(self, sel=False, tab=False, bg=False):
2014-05-17 22:38:07 +02:00
"""Open a page from the clipboard.
Args:
2014-09-13 00:22:27 +02:00
sel: Use the primary selection instead of the clipboard.
tab: Open in a new tab.
bg: Open in a background tab.
2014-05-17 22:38:07 +02:00
"""
clipboard = QApplication.clipboard()
if sel and clipboard.supportsSelection():
mode = QClipboard.Selection
target = "Primary selection"
else:
mode = QClipboard.Clipboard
target = "Clipboard"
text = clipboard.text(mode)
if not text:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("{} is empty.".format(target))
log.misc.debug("{} contained: '{}'".format(target, text))
try:
url = urlutils.fuzzy_url(text)
2014-06-20 23:57:52 +02:00
except urlutils.FuzzyUrlError as e:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(e)
self._open(url, tab, bg)
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher')
def tab_focus(self, index: (int, 'last')=None, count=None):
2014-05-17 22:38:07 +02:00
"""Select the tab given as argument/[count].
Args:
2014-08-03 00:39:39 +02:00
index: The tab index to focus, starting with 1. The special value
`last` focuses the last focused tab.
2014-08-03 00:33:39 +02:00
count: The tab index to focus, starting with 1.
2014-05-17 22:38:07 +02:00
"""
2014-08-03 00:39:39 +02:00
if index == 'last':
self._tab_focus_last()
return
2014-05-17 22:38:07 +02:00
try:
idx = cmdutils.arg_or_count(index, count, default=1,
countzero=self._tabs.count())
except ValueError as e:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(e)
2014-05-17 22:38:07 +02:00
cmdutils.check_overflow(idx + 1, 'int')
if 1 <= idx <= self._tabs.count():
self._tabs.setCurrentIndex(idx - 1)
else:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("There's no tab with index {}!".format(
idx))
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher')
def tab_move(self, direction: ('+', '-')=None, count=None):
2014-05-17 22:38:07 +02:00
"""Move the current tab.
Args:
2014-09-13 00:22:27 +02:00
direction: `+` or `-` for relative moving, not given for absolute
moving.
2014-08-03 00:33:39 +02:00
count: If moving absolutely: New position (default: 0)
2014-05-17 22:38:07 +02:00
If moving relatively: Offset.
"""
if direction is None:
new_idx = self._tab_move_absolute(count)
elif direction in '+-':
try:
new_idx = self._tab_move_relative(direction, count)
except ValueError:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Count must be given for relative "
"moving!")
2014-05-17 22:38:07 +02:00
else:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Invalid direction '{}'!".format(
direction))
2014-05-17 22:38:07 +02:00
if not 0 <= new_idx < self._tabs.count():
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Can't move tab to position {}!".format(
2014-05-17 22:38:07 +02:00
new_idx))
tab = self._current_widget()
2014-05-17 22:38:07 +02:00
cur_idx = self._tabs.currentIndex()
icon = self._tabs.tabIcon(cur_idx)
label = self._tabs.tabText(cur_idx)
cmdutils.check_overflow(cur_idx, 'int')
cmdutils.check_overflow(new_idx, 'int')
self._tabs.setUpdatesEnabled(False)
try:
self._tabs.removeTab(cur_idx)
self._tabs.insertTab(new_idx, tab, icon, label)
self._tabs.setCurrentIndex(new_idx)
finally:
self._tabs.setUpdatesEnabled(True)
2014-05-17 22:38:07 +02:00
@cmdutils.register(instance='command-dispatcher', split=False)
def spawn(self, *args):
"""Spawn a command in a shell.
2014-05-03 14:25:22 +02:00
Note the {url} variable which gets replaced by the current URL might be
useful here.
2014-05-03 14:25:22 +02:00
//
We use subprocess rather than Qt's QProcess here because we really
don't care about the process anymore as soon as it's spawned.
2014-05-03 14:25:22 +02:00
Args:
2014-08-03 00:33:39 +02:00
*args: The commandline to execute.
2014-05-03 14:25:22 +02:00
"""
log.procs.debug("Executing: {}".format(args))
subprocess.Popen(args)
2014-05-03 14:25:22 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-05-09 13:09:37 +02:00
def home(self):
"""Open main startpage in current tab."""
self.openurl(config.get('general', 'startpage')[0])
@cmdutils.register(instance='command-dispatcher')
2014-09-14 23:56:19 +02:00
def run_userscript(self, cmd, *args: {'nargs': '*'}):
2014-07-29 01:45:42 +02:00
"""Run an userscript given as argument.
Args:
cmd: The userscript to run.
args: Arguments to pass to the userscript.
"""
2014-07-29 02:05:15 +02:00
url = self._tabs.current_url()
userscripts.run(cmd, *args, url=url)
2014-05-21 19:53:58 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-05-22 16:44:47 +02:00
def quickmark_save(self):
"""Save the current page as a quickmark."""
quickmarks.prompt_save(self._tabs.current_url())
2014-05-22 16:44:47 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-09-02 21:54:07 +02:00
def quickmark_load(self, name, tab=False, bg=False):
2014-09-07 21:03:20 +02:00
"""Load a quickmark.
Args:
name: The name of the quickmark to load.
2014-09-13 00:22:27 +02:00
tab: Load the quickmark in a new tab.
bg: Load the quickmark in a new background tab.
2014-09-07 21:03:20 +02:00
"""
2014-06-20 22:57:32 +02:00
urlstr = quickmarks.get(name)
2014-06-20 23:57:52 +02:00
url = QUrl(urlstr)
if not url.isValid():
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Invalid URL {} ({})".format(
2014-06-21 16:42:58 +02:00
urlstr, url.errorString()))
self._open(url, tab, bg)
2014-05-22 16:44:47 +02:00
@cmdutils.register(instance='command-dispatcher', name='inspector')
def toggle_inspector(self):
"""Toggle the web inspector."""
cur = self._current_widget()
if cur.inspector is None:
2014-06-03 20:28:51 +02:00
if not config.get('general', 'developer-extras'):
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(
"Please enable developer-extras before using the "
"webinspector!")
cur.inspector = QWebInspector()
2014-05-27 16:04:45 +02:00
cur.inspector.setPage(cur.page())
cur.inspector.show()
elif cur.inspector.isVisible():
cur.inspector.hide()
else:
2014-06-03 20:28:51 +02:00
if not config.get('general', 'developer-extras'):
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(
"Please enable developer-extras before using the "
"webinspector!")
else:
cur.inspector.show()
@cmdutils.register(instance='command-dispatcher')
2014-06-19 17:58:46 +02:00
def download_page(self):
"""Download the current page."""
page = self._current_widget().page()
self._tabs.download_get.emit(self._tabs.current_url(), page)
2014-06-19 17:58:46 +02:00
@cmdutils.register(instance='command-dispatcher')
2014-09-15 17:59:54 +02:00
def view_source(self):
"""Show the source of the current page."""
2014-09-15 22:01:13 +02:00
# pylint doesn't seem to like pygments...
# pylint: disable=no-member
2014-09-15 17:59:54 +02:00
widget = self._current_widget()
if widget.viewing_source:
raise cmdexc.CommandError("Already viewing source!")
frame = widget.page().currentFrame()
url = self._tabs.current_url()
html = frame.toHtml()
lexer = pygments.lexers.HtmlLexer()
2014-09-15 18:19:56 +02:00
formatter = pygments.formatters.HtmlFormatter(
full=True, linenos='table')
2014-09-15 17:59:54 +02:00
highlighted = pygments.highlight(html, lexer, formatter)
tab = self._tabs.tabopen(explicit=True)
tab.setHtml(highlighted, url)
tab.viewing_source = True
@cmdutils.register(instance='command-dispatcher', name='help',
2014-09-08 06:57:22 +02:00
completion=[usertypes.Completion.helptopic])
2014-09-08 12:18:54 +02:00
def show_help(self, topic=None):
2014-09-08 07:44:32 +02:00
r"""Show help about a command or setting.
2014-09-08 06:57:22 +02:00
Args:
topic: The topic to show help for.
- :__command__ for commands.
- __section__\->__option__ for settings.
"""
2014-09-08 12:18:54 +02:00
if topic is None:
path = 'index.html'
elif topic.startswith(':'):
2014-09-08 06:57:22 +02:00
command = topic[1:]
if command not in cmdutils.cmd_dict:
raise cmdexc.CommandError("Invalid command {}!".format(
command))
path = 'commands.html#{}'.format(command)
elif '->' in topic:
parts = topic.split('->')
if len(parts) != 2:
raise cmdexc.CommandError("Invalid help topic {}!".format(
topic))
try:
config.get(*parts)
except config.NoSectionError:
raise cmdexc.CommandError("Invalid section {}!".format(
parts[0]))
except config.NoOptionError:
raise cmdexc.CommandError("Invalid option {}!".format(
parts[1]))
path = 'settings.html#{}'.format(topic.replace('->', '-'))
else:
raise cmdexc.CommandError("Invalid help topic {}!".format(topic))
self.openurl('qute://help/{}'.format(path))
@cmdutils.register(instance='command-dispatcher',
2014-08-26 19:10:14 +02:00
modes=[usertypes.KeyMode.insert],
2014-05-17 23:22:10 +02:00
hide=True)
def open_editor(self):
2014-08-03 00:33:39 +02:00
"""Open an external editor with the currently selected form field.
The editor which should be launched can be configured via the
`general -> editor` config option.
//
We use QProcess rather than subprocess here because it makes it a lot
easier to execute some code as soon as the process has been finished
and do everything async.
"""
frame = self._current_widget().page().currentFrame()
2014-09-04 08:00:05 +02:00
try:
elem = webelem.focus_elem(frame)
except webelem.IsNullError:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("No element focused!")
2014-09-04 08:00:05 +02:00
if not elem.is_editable(strict=True):
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Focused element is not editable!")
2014-09-04 08:00:05 +02:00
if elem.is_content_editable():
text = str(elem)
else:
text = elem.evaluateJavaScript('this.value')
2014-08-26 19:10:14 +02:00
self._editor = editor.ExternalEditor(self._tabs)
2014-05-21 17:29:09 +02:00
self._editor.editing_finished.connect(
partial(self.on_editing_finished, elem))
self._editor.edit(text)
def on_editing_finished(self, elem, text):
2014-04-30 10:46:20 +02:00
"""Write the editor text into the form field and clean up tempfile.
2014-04-29 18:00:22 +02:00
2014-04-30 10:46:20 +02:00
Callback for QProcess when the editor was closed.
Args:
2014-09-04 08:00:05 +02:00
elem: The WebElementWrapper which was modified.
text: The new text to insert.
2014-04-29 18:00:22 +02:00
"""
2014-09-04 08:00:05 +02:00
try:
if elem.is_content_editable():
log.misc.debug("Filling element {} via setPlainText.".format(
elem.debug_text()))
elem.setPlainText(text)
else:
log.misc.debug("Filling element {} via javascript.".format(
elem.debug_text()))
text = webelem.javascript_escape(text)
elem.evaluateJavaScript("this.value='{}'".format(text))
except webelem.IsNullError:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Element vanished while editing!")