Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: 

 

# Copyright 2014-2018 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/>. 

 

"""Command dispatcher for TabbedBrowser.""" 

 

import os 

import os.path 

import shlex 

import functools 

import typing 

 

from PyQt5.QtWidgets import QApplication, QTabBar, QDialog 

from PyQt5.QtCore import pyqtSlot, Qt, QUrl, QEvent, QUrlQuery 

from PyQt5.QtGui import QKeyEvent 

from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog 

 

from qutebrowser.commands import userscripts, cmdexc, cmdutils, runners 

from qutebrowser.config import config, configdata 

from qutebrowser.browser import (urlmarks, browsertab, inspector, navigate, 

webelem, downloads) 

from qutebrowser.keyinput import modeman 

from qutebrowser.utils import (message, usertypes, log, qtutils, urlutils, 

objreg, utils, standarddir) 

from qutebrowser.utils.usertypes import KeyMode 

from qutebrowser.misc import editor, guiprocess 

from qutebrowser.completion.models import urlmodel, miscmodels 

from qutebrowser.mainwindow import mainwindow 

 

 

class CommandDispatcher: 

 

"""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 

currentWidget() for TabbedBrowser.cmd because at the time 

cmdutils.register() decorators are run, currentWidget() will return None. 

 

Attributes: 

_editor: The ExternalEditor object. 

_win_id: The window ID the CommandDispatcher is associated with. 

_tabbed_browser: The TabbedBrowser used. 

""" 

 

def __init__(self, win_id, tabbed_browser): 

self._win_id = win_id 

self._tabbed_browser = tabbed_browser 

 

def __repr__(self): 

return utils.get_repr(self) 

 

def _new_tabbed_browser(self, private): 

"""Get a tabbed-browser from a new window.""" 

new_window = mainwindow.MainWindow(private=private) 

new_window.show() 

return new_window.tabbed_browser 

 

def _count(self): 

"""Convenience method to get the widget count.""" 

return self._tabbed_browser.count() 

 

def _set_current_index(self, idx): 

"""Convenience method to set the current widget index.""" 

cmdutils.check_overflow(idx, 'int') 

self._tabbed_browser.setCurrentIndex(idx) 

 

def _current_index(self): 

"""Convenience method to get the current widget index.""" 

return self._tabbed_browser.currentIndex() 

 

def _current_url(self): 

"""Convenience method to get the current url.""" 

try: 

return self._tabbed_browser.current_url() 

except qtutils.QtValueError as e: 

msg = "Current URL is invalid" 

if e.reason: 

msg += " ({})".format(e.reason) 

msg += "!" 

raise cmdexc.CommandError(msg) 

 

def _current_title(self): 

"""Convenience method to get the current title.""" 

return self._current_widget().title() 

 

def _current_widget(self): 

"""Get the currently active widget from a command.""" 

widget = self._tabbed_browser.currentWidget() 

if widget is None: 

raise cmdexc.CommandError("No WebView available yet!") 

return widget 

 

def _open(self, url, tab=False, background=False, window=False, 

related=False, private=None): 

"""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. 

window: Whether to open in a new window 

private: If opening a new window, open it in private browsing mode. 

If not given, inherit the current window's mode. 

""" 

urlutils.raise_cmdexc_if_invalid(url) 

tabbed_browser = self._tabbed_browser 

cmdutils.check_exclusive((tab, background, window, private), 'tbwp') 

if window and private is None: 

private = self._tabbed_browser.private 

 

if window or private: 

tabbed_browser = self._new_tabbed_browser(private) 

tabbed_browser.tabopen(url) 

elif tab: 

tabbed_browser.tabopen(url, background=False, related=related) 

elif background: 

tabbed_browser.tabopen(url, background=True, related=related) 

else: 

widget = self._current_widget() 

widget.openurl(url) 

 

def _cntwidget(self, count=None): 

"""Return a widget based on a count/idx. 

 

Args: 

count: The tab index, or None. 

 

Return: 

The current widget if count is None. 

The widget with the given tab ID if count is given. 

None if no widget was found. 

""" 

if count is None: 

return self._tabbed_browser.currentWidget() 

elif 1 <= count <= self._count(): 

cmdutils.check_overflow(count + 1, 'int') 

return self._tabbed_browser.widget(count - 1) 

else: 

return None 

 

def _tab_focus_last(self, *, show_error=True): 

"""Select the tab which was last focused.""" 

try: 

tab = objreg.get('last-focused-tab', scope='window', 

window=self._win_id) 

except KeyError: 

if not show_error: 

return 

raise cmdexc.CommandError("No last focused tab!") 

idx = self._tabbed_browser.indexOf(tab) 

if idx == -1: 

raise cmdexc.CommandError("Last focused tab vanished!") 

self._set_current_index(idx) 

 

def _get_selection_override(self, prev, next_, opposite): 

"""Helper function for tab_close to get the tab to select. 

 

Args: 

prev: Force selecting the tab before the current tab. 

next_: Force selecting the tab after the current tab. 

opposite: Force selecting the tab in the opposite direction of 

what's configured in 'tabs.select_on_remove'. 

 

Return: 

QTabBar.SelectLeftTab, QTabBar.SelectRightTab, or None if no change 

should be made. 

""" 

cmdutils.check_exclusive((prev, next_, opposite), 'pno') 

if prev: 

return QTabBar.SelectLeftTab 

elif next_: 

return QTabBar.SelectRightTab 

elif opposite: 

conf_selection = config.val.tabs.select_on_remove 

if conf_selection == QTabBar.SelectLeftTab: 

return QTabBar.SelectRightTab 

elif conf_selection == QTabBar.SelectRightTab: 

return QTabBar.SelectLeftTab 

elif conf_selection == QTabBar.SelectPreviousTab: 

raise cmdexc.CommandError( 

"-o is not supported with 'tabs.select_on_remove' set to " 

"'last-used'!") 

else: # pragma: no cover 

raise ValueError("Invalid select_on_remove value " 

"{!r}!".format(conf_selection)) 

return None 

 

def _tab_close(self, tab, prev=False, next_=False, opposite=False): 

"""Helper function for tab_close be able to handle message.async. 

 

Args: 

tab: Tab object to select be closed. 

prev: Force selecting the tab before the current tab. 

next_: Force selecting the tab after the current tab. 

opposite: Force selecting the tab in the opposite direction of 

what's configured in 'tabs.select_on_remove'. 

count: The tab index to close, or None 

""" 

tabbar = self._tabbed_browser.tabBar() 

selection_override = self._get_selection_override(prev, next_, 

opposite) 

 

if selection_override is None: 

self._tabbed_browser.close_tab(tab) 

else: 

old_selection_behavior = tabbar.selectionBehaviorOnRemove() 

tabbar.setSelectionBehaviorOnRemove(selection_override) 

self._tabbed_browser.close_tab(tab) 

tabbar.setSelectionBehaviorOnRemove(old_selection_behavior) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def tab_close(self, prev=False, next_=False, opposite=False, 

force=False, count=None): 

"""Close the current/[count]th tab. 

 

Args: 

prev: Force selecting the tab before the current tab. 

next_: Force selecting the tab after the current tab. 

opposite: Force selecting the tab in the opposite direction of 

what's configured in 'tabs.select_on_remove'. 

force: Avoid confirmation for pinned tabs. 

count: The tab index to close, or None 

""" 

tab = self._cntwidget(count) 

if tab is None: 

return 

close = functools.partial(self._tab_close, tab, prev, 

next_, opposite) 

 

self._tabbed_browser.tab_close_prompt_if_pinned(tab, force, close) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

name='tab-pin') 

@cmdutils.argument('count', count=True) 

def tab_pin(self, count=None): 

"""Pin/Unpin the current/[count]th tab. 

 

Pinning a tab shrinks it to the size of its title text. 

Attempting to close a pinned tab will cause a confirmation, 

unless --force is passed. 

 

Args: 

count: The tab index to pin or unpin, or None 

""" 

tab = self._cntwidget(count) 

if tab is None: 

return 

 

to_pin = not tab.data.pinned 

self._tabbed_browser.set_tab_pinned(tab, to_pin) 

 

@cmdutils.register(instance='command-dispatcher', name='open', 

maxsplit=0, scope='window') 

@cmdutils.argument('url', completion=urlmodel.url) 

@cmdutils.argument('count', count=True) 

def openurl(self, url=None, related=False, 

bg=False, tab=False, window=False, count=None, secure=False, 

private=False): 

"""Open a URL in the current/[count]th tab. 

 

If the URL contains newlines, each line gets opened in its own tab. 

 

Args: 

url: The URL to open. 

bg: Open in a new background tab. 

tab: Open in a new tab. 

window: Open in a new window. 

related: If opening a new tab, position the tab as related to the 

current one (like clicking on a link). 

count: The tab index to open the URL in, or None. 

secure: Force HTTPS. 

private: Open a new window in private browsing mode. 

""" 

if url is None: 

urls = [config.val.url.default_page] 

else: 

urls = self._parse_url_input(url) 

 

for i, cur_url in enumerate(urls): 

if secure: 

cur_url.setScheme('https') 

if not window and i > 0: 

tab = False 

bg = True 

 

if tab or bg or window or private: 

self._open(cur_url, tab, bg, window, related=related, 

private=private) 

else: 

curtab = self._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._tabbed_browser.tabopen(cur_url) 

else: 

# Explicit count with a tab that doesn't exist. 

return 

elif curtab.data.pinned: 

message.info("Tab is pinned!") 

else: 

curtab.openurl(cur_url) 

 

def _parse_url(self, url, *, force_search=False): 

"""Parse a URL or quickmark or search query. 

 

Args: 

url: The URL to parse. 

force_search: Whether to force a search even if the content can be 

interpreted as a URL or a path. 

 

Return: 

A URL that can be opened. 

""" 

try: 

return objreg.get('quickmark-manager').get(url) 

except urlmarks.Error: 

try: 

return urlutils.fuzzy_url(url, force_search=force_search) 

except urlutils.InvalidUrlError as e: 

# We don't use cmdexc.CommandError here as this can be 

# called async from edit_url 

message.error(str(e)) 

return None 

 

def _parse_url_input(self, url): 

"""Parse a URL or newline-separated list of URLs. 

 

Args: 

url: The URL or list to parse. 

 

Return: 

A list of URLs that can be opened. 

""" 

if isinstance(url, QUrl): 

yield url 

return 

 

force_search = False 

urllist = [u for u in url.split('\n') if u.strip()] 

if (len(urllist) > 1 and not urlutils.is_url(urllist[0]) and 

urlutils.get_path_if_valid(urllist[0], check_exists=True) 

is None): 

urllist = [url] 

force_search = True 

for cur_url in urllist: 

parsed = self._parse_url(cur_url, force_search=force_search) 

if parsed is not None: 

yield parsed 

 

@cmdutils.register(instance='command-dispatcher', name='reload', 

scope='window') 

@cmdutils.argument('count', count=True) 

def reloadpage(self, force=False, count=None): 

"""Reload the current/[count]th tab. 

 

Args: 

count: The tab index to reload, or None. 

force: Bypass the page cache. 

""" 

tab = self._cntwidget(count) 

if tab is not None: 

tab.reload(force=force) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def stop(self, count=None): 

"""Stop loading in the current/[count]th tab. 

 

Args: 

count: The tab index to stop, or None. 

""" 

tab = self._cntwidget(count) 

if tab is not None: 

tab.stop() 

 

def _print_preview(self, tab): 

"""Show a print preview.""" 

def print_callback(ok): 

if not ok: 

message.error("Printing failed!") 

 

tab.printing.check_preview_support() 

diag = QPrintPreviewDialog(tab) 

diag.setAttribute(Qt.WA_DeleteOnClose) 

diag.setWindowFlags(diag.windowFlags() | Qt.WindowMaximizeButtonHint | 

Qt.WindowMinimizeButtonHint) 

diag.paintRequested.connect(functools.partial( 

tab.printing.to_printer, callback=print_callback)) 

diag.exec_() 

 

def _print_pdf(self, tab, filename): 

"""Print to the given PDF file.""" 

tab.printing.check_pdf_support() 

filename = os.path.expanduser(filename) 

directory = os.path.dirname(filename) 

if directory and not os.path.exists(directory): 

os.mkdir(directory) 

tab.printing.to_pdf(filename) 

log.misc.debug("Print to file: {}".format(filename)) 

 

def _print(self, tab): 

"""Print with a QPrintDialog.""" 

def print_callback(ok): 

"""Called when printing finished.""" 

if not ok: 

message.error("Printing failed!") 

diag.deleteLater() 

 

def do_print(): 

"""Called when the dialog was closed.""" 

tab.printing.to_printer(diag.printer(), print_callback) 

 

diag = QPrintDialog(tab) 

if utils.is_mac: 

# For some reason we get a segfault when using open() on macOS 

ret = diag.exec_() 

if ret == QDialog.Accepted: 

do_print() 

else: 

diag.open(do_print) 

 

@cmdutils.register(instance='command-dispatcher', name='print', 

scope='window') 

@cmdutils.argument('count', count=True) 

@cmdutils.argument('pdf', flag='f', metavar='file') 

def printpage(self, preview=False, count=None, *, pdf=None): 

"""Print the current/[count]th tab. 

 

Args: 

preview: Show preview instead of printing. 

count: The tab index to print, or None. 

pdf: The file path to write the PDF to. 

""" 

tab = self._cntwidget(count) 

if tab is None: 

return 

 

try: 

if pdf: 

tab.printing.check_pdf_support() 

else: 

tab.printing.check_printer_support() 

if preview: 

tab.printing.check_preview_support() 

except browsertab.WebTabError as e: 

raise cmdexc.CommandError(e) 

 

if preview: 

self._print_preview(tab) 

elif pdf: 

self._print_pdf(tab, pdf) 

else: 

self._print(tab) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def tab_clone(self, bg=False, window=False): 

"""Duplicate the current tab. 

 

Args: 

bg: Open in a background tab. 

window: Open in a new window. 

 

Return: 

The new QWebView. 

""" 

cmdutils.check_exclusive((bg, window), 'bw') 

curtab = self._current_widget() 

cur_title = self._tabbed_browser.page_title(self._current_index()) 

try: 

history = curtab.history.serialize() 

except browsertab.WebTabError as e: 

raise cmdexc.CommandError(e) 

 

# The new tab could be in a new tabbed_browser (e.g. because of 

# tabs.tabs_are_windows being set) 

if window: 

new_tabbed_browser = self._new_tabbed_browser( 

private=self._tabbed_browser.private) 

else: 

new_tabbed_browser = self._tabbed_browser 

newtab = new_tabbed_browser.tabopen(background=bg) 

new_tabbed_browser = objreg.get('tabbed-browser', scope='window', 

window=newtab.win_id) 

idx = new_tabbed_browser.indexOf(newtab) 

 

new_tabbed_browser.set_page_title(idx, cur_title) 

if config.val.tabs.favicons.show: 

new_tabbed_browser.setTabIcon(idx, curtab.icon()) 

if config.val.tabs.tabs_are_windows: 

new_tabbed_browser.window().setWindowIcon(curtab.icon()) 

 

newtab.data.keep_icon = True 

newtab.history.deserialize(history) 

newtab.zoom.set_factor(curtab.zoom.factor()) 

new_tabbed_browser.set_tab_pinned(newtab, curtab.data.pinned) 

return newtab 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('index', completion=miscmodels.other_buffer) 

def tab_take(self, index): 

"""Take a tab from another window. 

 

Args: 

index: The [win_id/]index of the tab to take. Or a substring 

in which case the closest match will be taken. 

""" 

tabbed_browser, tab = self._resolve_buffer_index(index) 

 

if tabbed_browser is self._tabbed_browser: 

raise cmdexc.CommandError("Can't take a tab from the same window") 

 

self._open(tab.url(), tab=True) 

tabbed_browser.close_tab(tab, add_undo=False) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('win_id', completion=miscmodels.window) 

@cmdutils.argument('count', count=True) 

def tab_give(self, win_id: int = None, count=None): 

"""Give the current tab to a new or existing window if win_id given. 

 

If no win_id is given, the tab will get detached into a new window. 

 

Args: 

win_id: The window ID of the window to give the current tab to. 

count: Overrides win_id (index starts at 1 for win_id=0). 

""" 

if count is not None: 

win_id = count - 1 

 

if win_id == self._win_id: 

raise cmdexc.CommandError("Can't give a tab to the same window") 

 

if win_id is None: 

if self._count() < 2: 

raise cmdexc.CommandError("Cannot detach from a window with " 

"only one tab") 

 

tabbed_browser = self._new_tabbed_browser( 

private=self._tabbed_browser.private) 

else: 

if win_id not in objreg.window_registry: 

raise cmdexc.CommandError( 

"There's no window with id {}!".format(win_id)) 

 

tabbed_browser = objreg.get('tabbed-browser', scope='window', 

window=win_id) 

 

tabbed_browser.tabopen(self._current_url()) 

self._tabbed_browser.close_tab(self._current_widget(), add_undo=False) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

deprecated='Use :tab-give instead!') 

def tab_detach(self): 

"""Deprecated way to detach a tab.""" 

self.tab_give() 

 

def _back_forward(self, tab, bg, window, count, forward): 

"""Helper function for :back/:forward.""" 

history = self._current_widget().history 

# Catch common cases before e.g. cloning tab 

if not forward and not history.can_go_back(): 

raise cmdexc.CommandError("At beginning of history.") 

elif forward and not history.can_go_forward(): 

raise cmdexc.CommandError("At end of history.") 

 

if tab or bg or window: 

widget = self.tab_clone(bg, window) 

else: 

widget = self._current_widget() 

 

try: 

if forward: 

widget.history.forward(count) 

else: 

widget.history.back(count) 

except browsertab.WebTabError as e: 

raise cmdexc.CommandError(e) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def back(self, tab=False, bg=False, window=False, count=1): 

"""Go back in the history of the current tab. 

 

Args: 

tab: Go back in a new tab. 

bg: Go back in a background tab. 

window: Go back in a new window. 

count: How many pages to go back. 

""" 

self._back_forward(tab, bg, window, count, forward=False) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def forward(self, tab=False, bg=False, window=False, count=1): 

"""Go forward in the history of the current tab. 

 

Args: 

tab: Go forward in a new tab. 

bg: Go forward in a background tab. 

window: Go forward in a new window. 

count: How many pages to go forward. 

""" 

self._back_forward(tab, bg, window, count, forward=True) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('where', choices=['prev', 'next', 'up', 'increment', 

'decrement']) 

@cmdutils.argument('count', count=True) 

def navigate(self, where: str, tab=False, bg=False, window=False, count=1): 

"""Open typical prev/next links or navigate using the URL path. 

 

This tries to automatically click on typical _Previous Page_ or 

_Next Page_ links using some heuristics. 

 

Alternatively it can navigate by changing the current URL. 

 

Args: 

where: What to open. 

 

- `prev`: Open a _previous_ link. 

- `next`: Open a _next_ link. 

- `up`: Go up a level in the current URL. 

- `increment`: Increment the last number in the URL. 

Uses the 

link:settings.html#url.incdec_segments[url.incdec_segments] 

config option. 

- `decrement`: Decrement the last number in the URL. 

Uses the 

link:settings.html#url.incdec_segments[url.incdec_segments] 

config option. 

 

tab: Open in a new tab. 

bg: Open in a background tab. 

window: Open in a new window. 

count: For `increment` and `decrement`, the number to change the 

URL by. For `up`, the number of levels to go up in the URL. 

""" 

# save the pre-jump position in the special ' mark 

self.set_mark("'") 

 

cmdutils.check_exclusive((tab, bg, window), 'tbw') 

widget = self._current_widget() 

url = self._current_url().adjusted(QUrl.RemoveFragment) 

 

handlers = { 

'prev': functools.partial(navigate.prevnext, prev=True), 

'next': functools.partial(navigate.prevnext, prev=False), 

'up': navigate.path_up, 

'decrement': functools.partial(navigate.incdec, 

inc_or_dec='decrement'), 

'increment': functools.partial(navigate.incdec, 

inc_or_dec='increment'), 

} 

 

try: 

if where in ['prev', 'next']: 

handler = handlers[where] 

handler(browsertab=widget, win_id=self._win_id, baseurl=url, 

tab=tab, background=bg, window=window) 

elif where in ['up', 'increment', 'decrement']: 

new_url = handlers[where](url, count) 

self._open(new_url, tab, bg, window, related=True) 

else: # pragma: no cover 

raise ValueError("Got called with invalid value {} for " 

"`where'.".format(where)) 

except navigate.Error as e: 

raise cmdexc.CommandError(e) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def scroll_px(self, dx: int, dy: int, count=1): 

"""Scroll the current tab by 'count * dx/dy' pixels. 

 

Args: 

dx: How much to scroll in x-direction. 

dy: How much to scroll in y-direction. 

count: multiplier 

""" 

dx *= count 

dy *= count 

cmdutils.check_overflow(dx, 'int') 

cmdutils.check_overflow(dy, 'int') 

self._current_widget().scroller.delta(dx, dy) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def scroll(self, direction: typing.Union[str, int], count=1): 

"""Scroll the current tab in the given direction. 

 

Note you can use `:run-with-count` to have a keybinding with a bigger 

scroll increment. 

 

Args: 

direction: In which direction to scroll 

(up/down/left/right/top/bottom). 

count: multiplier 

""" 

tab = self._current_widget() 

funcs = { 

'up': tab.scroller.up, 

'down': tab.scroller.down, 

'left': tab.scroller.left, 

'right': tab.scroller.right, 

'top': tab.scroller.top, 

'bottom': tab.scroller.bottom, 

'page-up': tab.scroller.page_up, 

'page-down': tab.scroller.page_down, 

} 

try: 

func = funcs[direction] 

except KeyError: 

expected_values = ', '.join(sorted(funcs)) 

raise cmdexc.CommandError("Invalid value {!r} for direction - " 

"expected one of: {}".format( 

direction, expected_values)) 

 

if direction in ['top', 'bottom']: 

func() 

else: 

func(count=count) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

@cmdutils.argument('horizontal', flag='x') 

def scroll_to_perc(self, perc: float = None, horizontal=False, count=None): 

"""Scroll to a specific percentage of the page. 

 

The percentage can be given either as argument or as count. 

If no percentage is given, the page is scrolled to the end. 

 

Args: 

perc: Percentage to scroll. 

horizontal: Scroll horizontally instead of vertically. 

count: Percentage to scroll. 

""" 

# save the pre-jump position in the special ' mark 

self.set_mark("'") 

 

if perc is None and count is None: 

perc = 100 

elif count is not None: 

perc = count 

 

if horizontal: 

x = perc 

y = None 

else: 

x = None 

y = perc 

 

self._current_widget().scroller.to_perc(x, y) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

@cmdutils.argument('top_navigate', metavar='ACTION', 

choices=('prev', 'decrement')) 

@cmdutils.argument('bottom_navigate', metavar='ACTION', 

choices=('next', 'increment')) 

def scroll_page(self, x: float, y: float, *, 

top_navigate: str = None, bottom_navigate: str = None, 

count=1): 

"""Scroll the frame page-wise. 

 

Args: 

x: How many pages to scroll to the right. 

y: How many pages to scroll down. 

bottom_navigate: :navigate action (next, increment) to run when 

scrolling down at the bottom of the page. 

top_navigate: :navigate action (prev, decrement) to run when 

scrolling up at the top of the page. 

count: multiplier 

""" 

tab = self._current_widget() 

if not tab.url().isValid(): 

# See https://github.com/qutebrowser/qutebrowser/issues/701 

return 

 

if bottom_navigate is not None and tab.scroller.at_bottom(): 

self.navigate(bottom_navigate) 

return 

elif top_navigate is not None and tab.scroller.at_top(): 

self.navigate(top_navigate) 

return 

 

try: 

tab.scroller.delta_page(count * x, count * y) 

except OverflowError: 

raise cmdexc.CommandError( 

"Numeric argument is too large for internal int " 

"representation.") 

 

def _yank_url(self, what): 

"""Helper method for yank() to get the URL to copy.""" 

assert what in ['url', 'pretty-url'], what 

flags = QUrl.RemovePassword 

if what == 'pretty-url': 

flags |= QUrl.DecodeReserved 

else: 

flags |= QUrl.FullyEncoded 

url = QUrl(self._current_url()) 

url_query = QUrlQuery() 

url_query_str = urlutils.query_string(url) 

if '&' not in url_query_str and ';' in url_query_str: 

url_query.setQueryDelimiters('=', ';') 

url_query.setQuery(url_query_str) 

for key in dict(url_query.queryItems()): 

if key in config.val.url.yank_ignored_parameters: 

url_query.removeQueryItem(key) 

url.setQuery(url_query) 

return url.toString(flags) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('what', choices=['selection', 'url', 'pretty-url', 

'title', 'domain']) 

def yank(self, what='url', sel=False, keep=False): 

"""Yank something to the clipboard or primary selection. 

 

Args: 

what: What to yank. 

 

- `url`: The current URL. 

- `pretty-url`: The URL in pretty decoded form. 

- `title`: The current page's title. 

- `domain`: The current scheme, domain, and port number. 

- `selection`: The selection under the cursor. 

 

sel: Use the primary selection instead of the clipboard. 

keep: Stay in visual mode after yanking the selection. 

""" 

if what == 'title': 

s = self._tabbed_browser.page_title(self._current_index()) 

elif what == 'domain': 

port = self._current_url().port() 

s = '{}://{}{}'.format(self._current_url().scheme(), 

self._current_url().host(), 

':' + str(port) if port > -1 else '') 

elif what in ['url', 'pretty-url']: 

s = self._yank_url(what) 

what = 'URL' # For printing 

elif what == 'selection': 

def _selection_callback(s): 

if not s: 

message.info("Nothing to yank") 

return 

self._yank_to_target(s, sel, what, keep) 

 

caret = self._current_widget().caret 

caret.selection(callback=_selection_callback) 

return 

else: # pragma: no cover 

raise ValueError("Invalid value {!r} for `what'.".format(what)) 

 

self._yank_to_target(s, sel, what, keep) 

 

def _yank_to_target(self, s, sel, what, keep): 

if sel and utils.supports_selection(): 

target = "primary selection" 

else: 

sel = False 

target = "clipboard" 

 

utils.set_clipboard(s, selection=sel) 

if what != 'selection': 

message.info("Yanked {} to {}: {}".format(what, target, s)) 

else: 

message.info("{} {} yanked to {}".format( 

len(s), "char" if len(s) == 1 else "chars", target)) 

if not keep: 

modeman.leave(self._win_id, KeyMode.caret, "yank selected", 

maybe=True) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def zoom_in(self, count=1): 

"""Increase the zoom level for the current tab. 

 

Args: 

count: How many steps to zoom in. 

""" 

tab = self._current_widget() 

try: 

perc = tab.zoom.offset(count) 

except ValueError as e: 

raise cmdexc.CommandError(e) 

message.info("Zoom level: {}%".format(int(perc)), replace=True) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def zoom_out(self, count=1): 

"""Decrease the zoom level for the current tab. 

 

Args: 

count: How many steps to zoom out. 

""" 

tab = self._current_widget() 

try: 

perc = tab.zoom.offset(-count) 

except ValueError as e: 

raise cmdexc.CommandError(e) 

message.info("Zoom level: {}%".format(int(perc)), replace=True) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def zoom(self, zoom=None, count=None): 

"""Set the zoom level for the current tab. 

 

The zoom can be given as argument or as [count]. If neither is 

given, the zoom is set to the default zoom. If both are given, 

use [count]. 

 

Args: 

zoom: The zoom percentage to set. 

count: The zoom percentage to set. 

""" 

if zoom is not None: 

try: 

zoom = int(zoom.rstrip('%')) 

except ValueError: 

raise cmdexc.CommandError("zoom: Invalid int value {}" 

.format(zoom)) 

 

level = count if count is not None else zoom 

if level is None: 

level = config.val.zoom.default 

tab = self._current_widget() 

 

try: 

tab.zoom.set_factor(float(level) / 100) 

except ValueError: 

raise cmdexc.CommandError("Can't zoom {}%!".format(level)) 

message.info("Zoom level: {}%".format(int(level)), replace=True) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def tab_only(self, prev=False, next_=False, force=False): 

"""Close all tabs except for the current one. 

 

Args: 

prev: Keep tabs before the current. 

next_: Keep tabs after the current. 

force: Avoid confirmation for pinned tabs. 

""" 

cmdutils.check_exclusive((prev, next_), 'pn') 

cur_idx = self._tabbed_browser.currentIndex() 

assert cur_idx != -1 

 

def _to_close(i): 

"""Helper method to check if a tab should be closed or not.""" 

return not (i == cur_idx or 

(prev and i < cur_idx) or 

(next_ and i > cur_idx)) 

 

# close as many tabs as we can 

first_tab = True 

pinned_tabs_cleanup = False 

for i, tab in enumerate(self._tabbed_browser.widgets()): 

if _to_close(i): 

if force or not tab.data.pinned: 

self._tabbed_browser.close_tab(tab, new_undo=first_tab) 

first_tab = False 

else: 

pinned_tabs_cleanup = tab 

 

# Check to see if we would like to close any pinned tabs 

if pinned_tabs_cleanup: 

self._tabbed_browser.tab_close_prompt_if_pinned( 

pinned_tabs_cleanup, 

force, 

lambda: self.tab_only( 

prev=prev, next_=next_, force=True), 

text="Are you sure you want to close pinned tabs?") 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def undo(self): 

"""Re-open the last closed tab or tabs.""" 

try: 

self._tabbed_browser.undo() 

except IndexError: 

raise cmdexc.CommandError("Nothing to undo!") 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def tab_prev(self, count=1): 

"""Switch to the previous tab, or switch [count] tabs back. 

 

Args: 

count: How many tabs to switch back. 

""" 

if self._count() == 0: 

# Running :tab-prev after last tab was closed 

# See https://github.com/qutebrowser/qutebrowser/issues/1448 

return 

newidx = self._current_index() - count 

if newidx >= 0: 

self._set_current_index(newidx) 

elif config.val.tabs.wrap: 

self._set_current_index(newidx % self._count()) 

else: 

raise cmdexc.CommandError("First tab") 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def tab_next(self, count=1): 

"""Switch to the next tab, or switch [count] tabs forward. 

 

Args: 

count: How many tabs to switch forward. 

""" 

if self._count() == 0: 

# Running :tab-next after last tab was closed 

# See https://github.com/qutebrowser/qutebrowser/issues/1448 

return 

newidx = self._current_index() + count 

if newidx < self._count(): 

self._set_current_index(newidx) 

elif config.val.tabs.wrap: 

self._set_current_index(newidx % self._count()) 

else: 

raise cmdexc.CommandError("Last tab") 

 

def _resolve_buffer_index(self, index): 

"""Resolve a buffer index to the tabbedbrowser and tab. 

 

Args: 

index: The [win_id/]index of the tab to be selected. Or a substring 

in which case the closest match will be focused. 

""" 

index_parts = index.split('/', 1) 

 

try: 

for part in index_parts: 

int(part) 

except ValueError: 

model = miscmodels.buffer() 

model.set_pattern(index) 

if model.count() > 0: 

index = model.data(model.first_item()) 

index_parts = index.split('/', 1) 

else: 

raise cmdexc.CommandError( 

"No matching tab for: {}".format(index)) 

 

if len(index_parts) == 2: 

win_id = int(index_parts[0]) 

idx = int(index_parts[1]) 

elif len(index_parts) == 1: 

idx = int(index_parts[0]) 

active_win = objreg.get('app').activeWindow() 

if active_win is None: 

# Not sure how you enter a command without an active window... 

raise cmdexc.CommandError( 

"No window specified and couldn't find active window!") 

win_id = active_win.win_id 

 

if win_id not in objreg.window_registry: 

raise cmdexc.CommandError( 

"There's no window with id {}!".format(win_id)) 

 

tabbed_browser = objreg.get('tabbed-browser', scope='window', 

window=win_id) 

if not 0 < idx <= tabbed_browser.count(): 

raise cmdexc.CommandError( 

"There's no tab with index {}!".format(idx)) 

 

return (tabbed_browser, tabbed_browser.widget(idx-1)) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0) 

@cmdutils.argument('index', completion=miscmodels.buffer) 

@cmdutils.argument('count', count=True) 

def buffer(self, index=None, count=None): 

"""Select tab by index or url/title best match. 

 

Focuses window if necessary when index is given. If both index and 

count are given, use count. 

 

With neither index nor count given, open the qute://tabs page. 

 

Args: 

index: The [win_id/]index of the tab to focus. Or a substring 

in which case the closest match will be focused. 

count: The tab index to focus, starting with 1. 

""" 

if count is None and index is None: 

self.openurl('qute://tabs/', tab=True) 

return 

 

if count is not None: 

index = str(count) 

 

tabbed_browser, tab = self._resolve_buffer_index(index) 

 

window = tabbed_browser.window() 

window.activateWindow() 

window.raise_() 

tabbed_browser.setCurrentWidget(tab) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('index', choices=['last']) 

@cmdutils.argument('count', count=True) 

def tab_focus(self, index: typing.Union[str, int] = None, 

count=None, no_last=False): 

"""Select the tab given as argument/[count]. 

 

If neither count nor index are given, it behaves like tab-next. 

If both are given, use count. 

 

Args: 

index: The tab index to focus, starting with 1. The special value 

`last` focuses the last focused tab (regardless of count). 

Negative indices count from the end, such that -1 is the 

last tab. 

count: The tab index to focus, starting with 1. 

no_last: Whether to avoid focusing last tab if already focused. 

""" 

index = count if count is not None else index 

 

if index == 'last': 

self._tab_focus_last() 

return 

elif not no_last and index == self._current_index() + 1: 

self._tab_focus_last(show_error=False) 

return 

elif index is None: 

self.tab_next() 

return 

 

if index < 0: 

index = self._count() + index + 1 

 

if 1 <= index <= self._count(): 

self._set_current_index(index - 1) 

else: 

raise cmdexc.CommandError("There's no tab with index {}!".format( 

index)) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('index', choices=['+', '-']) 

@cmdutils.argument('count', count=True) 

def tab_move(self, index: typing.Union[str, int] = None, count=None): 

"""Move the current tab according to the argument and [count]. 

 

If neither is given, move it to the first position. 

 

Args: 

index: `+` or `-` to move relative to the current tab by 

count, or a default of 1 space. 

A tab index to move to that index. 

count: If moving relatively: Offset. 

If moving absolutely: New position (default: 0). This 

overrides the index argument, if given. 

""" 

if index in ['+', '-']: 

# relative moving 

new_idx = self._current_index() 

delta = 1 if count is None else count 

if index == '-': 

new_idx -= delta 

elif index == '+': # pragma: no branch 

new_idx += delta 

 

if config.val.tabs.wrap: 

new_idx %= self._count() 

else: 

# absolute moving 

if count is not None: 

new_idx = count - 1 

elif index is not None: 

new_idx = index - 1 if index >= 0 else index + self._count() 

else: 

new_idx = 0 

 

if not 0 <= new_idx < self._count(): 

raise cmdexc.CommandError("Can't move tab to position {}!".format( 

new_idx + 1)) 

 

cur_idx = self._current_index() 

cmdutils.check_overflow(cur_idx, 'int') 

cmdutils.check_overflow(new_idx, 'int') 

self._tabbed_browser.tabBar().moveTab(cur_idx, new_idx) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0, no_replace_variables=True) 

def spawn(self, cmdline, userscript=False, verbose=False, 

output=False, detach=False): 

"""Spawn a command in a shell. 

 

Args: 

userscript: Run the command as a userscript. You can use an 

absolute path, or store the userscript in one of those 

locations: 

- `~/.local/share/qutebrowser/userscripts` 

(or `$XDG_DATA_DIR`) 

- `/usr/share/qutebrowser/userscripts` 

verbose: Show notifications when the command started/exited. 

output: Whether the output should be shown in a new tab. 

detach: Whether the command should be detached from qutebrowser. 

cmdline: The commandline to execute. 

""" 

cmdutils.check_exclusive((userscript, detach), 'ud') 

try: 

cmd, *args = shlex.split(cmdline) 

except ValueError as e: 

raise cmdexc.CommandError("Error while splitting command: " 

"{}".format(e)) 

 

args = runners.replace_variables(self._win_id, args) 

 

log.procs.debug("Executing {} with args {}, userscript={}".format( 

cmd, args, userscript)) 

 

@pyqtSlot() 

def _on_proc_finished(): 

if output: 

tb = objreg.get('tabbed-browser', scope='window', 

window='last-focused') 

tb.openurl(QUrl('qute://spawn-output'), newtab=True) 

 

if userscript: 

def _selection_callback(s): 

try: 

runner = self._run_userscript(s, cmd, args, verbose) 

runner.finished.connect(_on_proc_finished) 

except cmdexc.CommandError as e: 

message.error(str(e)) 

 

# ~ expansion is handled by the userscript module. 

# dirty hack for async call because of: 

# https://bugreports.qt.io/browse/QTBUG-53134 

# until it fixed or blocked async call implemented: 

# https://github.com/qutebrowser/qutebrowser/issues/3327 

caret = self._current_widget().caret 

caret.selection(callback=_selection_callback) 

else: 

cmd = os.path.expanduser(cmd) 

proc = guiprocess.GUIProcess(what='command', verbose=verbose, 

parent=self._tabbed_browser) 

if detach: 

proc.start_detached(cmd, args) 

else: 

proc.start(cmd, args) 

proc.finished.connect(_on_proc_finished) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def home(self): 

"""Open main startpage in current tab.""" 

self.openurl(config.val.url.start_pages[0]) 

 

def _run_userscript(self, selection, cmd, args, verbose): 

"""Run a userscript given as argument. 

 

Args: 

cmd: The userscript to run. 

args: Arguments to pass to the userscript. 

verbose: Show notifications when the command started/exited. 

""" 

env = { 

'QUTE_MODE': 'command', 

'QUTE_SELECTED_TEXT': selection, 

} 

 

idx = self._current_index() 

if idx != -1: 

env['QUTE_TITLE'] = self._tabbed_browser.page_title(idx) 

 

# FIXME:qtwebengine: If tab is None, run_async will fail! 

tab = self._tabbed_browser.currentWidget() 

 

try: 

url = self._tabbed_browser.current_url() 

except qtutils.QtValueError: 

pass 

else: 

env['QUTE_URL'] = url.toString(QUrl.FullyEncoded) 

 

try: 

runner = userscripts.run_async( 

tab, cmd, *args, win_id=self._win_id, env=env, verbose=verbose) 

except userscripts.Error as e: 

raise cmdexc.CommandError(e) 

return runner 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def quickmark_save(self): 

"""Save the current page as a quickmark.""" 

quickmark_manager = objreg.get('quickmark-manager') 

quickmark_manager.prompt_save(self._current_url()) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0) 

@cmdutils.argument('name', completion=miscmodels.quickmark) 

def quickmark_load(self, name, tab=False, bg=False, window=False): 

"""Load a quickmark. 

 

Args: 

name: The name of the quickmark to load. 

tab: Load the quickmark in a new tab. 

bg: Load the quickmark in a new background tab. 

window: Load the quickmark in a new window. 

""" 

try: 

url = objreg.get('quickmark-manager').get(name) 

except urlmarks.Error as e: 

raise cmdexc.CommandError(str(e)) 

self._open(url, tab, bg, window) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0) 

@cmdutils.argument('name', completion=miscmodels.quickmark) 

def quickmark_del(self, name=None): 

"""Delete a quickmark. 

 

Args: 

name: The name of the quickmark to delete. If not given, delete the 

quickmark for the current page (choosing one arbitrarily 

if there are more than one). 

""" 

quickmark_manager = objreg.get('quickmark-manager') 

if name is None: 

url = self._current_url() 

try: 

name = quickmark_manager.get_by_qurl(url) 

except urlmarks.DoesNotExistError as e: 

raise cmdexc.CommandError(str(e)) 

try: 

quickmark_manager.delete(name) 

except KeyError: 

raise cmdexc.CommandError("Quickmark '{}' not found!".format(name)) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

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 

bookmark. 

If a url and title have been provided, then save the given url as 

a bookmark with the provided title. 

 

You can view all saved bookmarks on the 

link:qute://bookmarks[bookmarks page]. 

 

Args: 

url: url to save as a bookmark. If not given, 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 ' 

'been provided') 

bookmark_manager = objreg.get('bookmark-manager') 

if not url: 

url = self._current_url() 

else: 

try: 

url = urlutils.fuzzy_url(url) 

except urlutils.InvalidUrlError as e: 

raise cmdexc.CommandError(e) 

if not title: 

title = self._current_title() 

try: 

was_added = bookmark_manager.add(url, title, toggle=toggle) 

except urlmarks.Error as e: 

raise cmdexc.CommandError(str(e)) 

else: 

msg = "Bookmarked {}" if was_added else "Removed bookmark {}" 

message.info(msg.format(url.toDisplayString())) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0) 

@cmdutils.argument('url', completion=miscmodels.bookmark) 

def bookmark_load(self, url, tab=False, bg=False, window=False, 

delete=False): 

"""Load a bookmark. 

 

Args: 

url: The url of the bookmark to load. 

tab: Load the bookmark in a new tab. 

bg: Load the bookmark in a new background tab. 

window: Load the bookmark in a new window. 

delete: Whether to delete the bookmark afterwards. 

""" 

try: 

qurl = urlutils.fuzzy_url(url) 

except urlutils.InvalidUrlError as e: 

raise cmdexc.CommandError(e) 

self._open(qurl, tab, bg, window) 

if delete: 

self.bookmark_del(url) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0) 

@cmdutils.argument('url', completion=miscmodels.bookmark) 

def bookmark_del(self, url=None): 

"""Delete a bookmark. 

 

Args: 

url: The url of the bookmark to delete. If not given, use the 

current page's url. 

""" 

if url is None: 

url = self._current_url().toString(QUrl.RemovePassword | 

QUrl.FullyEncoded) 

try: 

objreg.get('bookmark-manager').delete(url) 

except KeyError: 

raise cmdexc.CommandError("Bookmark '{}' not found!".format(url)) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def follow_selected(self, *, tab=False): 

"""Follow the selected text. 

 

Args: 

tab: Load the selected link in a new tab. 

""" 

try: 

self._current_widget().caret.follow_selected(tab=tab) 

except browsertab.WebTabError as e: 

raise cmdexc.CommandError(str(e)) 

 

@cmdutils.register(instance='command-dispatcher', name='inspector', 

scope='window') 

def toggle_inspector(self): 

"""Toggle the web inspector. 

 

Note: Due a bug in Qt, the inspector will show incorrect request 

headers in the network tab. 

""" 

tab = self._current_widget() 

# FIXME:qtwebengine have a proper API for this 

page = tab._widget.page() # pylint: disable=protected-access 

 

try: 

if tab.data.inspector is None: 

tab.data.inspector = inspector.create() 

tab.data.inspector.inspect(page) 

else: 

tab.data.inspector.toggle(page) 

except inspector.WebInspectorError as e: 

raise cmdexc.CommandError(e) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def download(self, url=None, *, mhtml_=False, dest=None): 

"""Download a given URL, or current page if no URL given. 

 

Args: 

url: The URL to download. If not given, download the current page. 

dest: The file path to write the download to, or None to ask. 

mhtml_: Download the current page and all assets as mhtml file. 

""" 

# FIXME:qtwebengine do this with the QtWebEngine download manager? 

download_manager = objreg.get('qtnetwork-download-manager') 

target = None 

if dest is not None: 

dest = downloads.transform_path(dest) 

if dest is None: 

raise cmdexc.CommandError("Invalid target filename") 

target = downloads.FileDownloadTarget(dest) 

 

tab = self._current_widget() 

user_agent = tab.user_agent() 

 

if url: 

if mhtml_: 

raise cmdexc.CommandError("Can only download the current page" 

" as mhtml.") 

url = urlutils.qurl_from_user_input(url) 

urlutils.raise_cmdexc_if_invalid(url) 

download_manager.get(url, user_agent=user_agent, target=target) 

elif mhtml_: 

tab = self._current_widget() 

if tab.backend == usertypes.Backend.QtWebEngine: 

webengine_download_manager = objreg.get( 

'webengine-download-manager') 

try: 

webengine_download_manager.get_mhtml(tab, target) 

except browsertab.UnsupportedOperationError as e: 

raise cmdexc.CommandError(e) 

else: 

download_manager.get_mhtml(tab, target) 

else: 

qnam = tab.networkaccessmanager() 

 

suggested_fn = downloads.suggested_fn_from_title( 

self._current_url().path(), tab.title() 

) 

 

download_manager.get( 

self._current_url(), 

user_agent=user_agent, 

qnam=qnam, 

target=target, 

suggested_fn=suggested_fn 

) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def view_source(self, edit=False): 

"""Show the source of the current page in a new tab. 

 

Args: 

edit: Edit the source in the editor instead of opening a tab. 

""" 

tab = self._current_widget() 

try: 

current_url = self._current_url() 

except cmdexc.CommandError as e: 

message.error(str(e)) 

return 

if current_url.scheme() == 'view-source': 

raise cmdexc.CommandError("Already viewing source!") 

 

if edit: 

ed = editor.ExternalEditor(self._tabbed_browser) 

tab.dump_async(ed.edit) 

else: 

tab.action.show_source() 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

debug=True) 

def debug_dump_page(self, dest, plain=False): 

"""Dump the current page's content to a file. 

 

Args: 

dest: Where to write the file to. 

plain: Write plain text instead of HTML. 

""" 

tab = self._current_widget() 

dest = os.path.expanduser(dest) 

 

def callback(data): 

"""Write the data to disk.""" 

try: 

with open(dest, 'w', encoding='utf-8') as f: 

f.write(data) 

except OSError as e: 

message.error('Could not write page: {}'.format(e)) 

else: 

message.info("Dumped page to {}.".format(dest)) 

 

tab.dump_async(callback, plain=plain) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def history(self, tab=True, bg=False, window=False): 

"""Show browsing history. 

 

Args: 

tab: Open in a new tab. 

bg: Open in a background tab. 

window: Open in a new window. 

""" 

url = QUrl('qute://history/') 

self._open(url, tab, bg, window) 

 

@cmdutils.register(instance='command-dispatcher', name='help', 

scope='window') 

@cmdutils.argument('topic', completion=miscmodels.helptopic) 

def show_help(self, tab=False, bg=False, window=False, topic=None): 

r"""Show help about a command or setting. 

 

Args: 

tab: Open in a new tab. 

bg: Open in a background tab. 

window: Open in a new window. 

topic: The topic to show help for. 

 

- :__command__ for commands. 

- __section__.__option__ for settings. 

""" 

if topic is None: 

path = 'index.html' 

elif topic.startswith(':'): 

command = topic[1:] 

if command not in cmdutils.cmd_dict: 

raise cmdexc.CommandError("Invalid command {}!".format( 

command)) 

path = 'commands.html#{}'.format(command) 

elif topic in configdata.DATA: 

path = 'settings.html#{}'.format(topic) 

else: 

raise cmdexc.CommandError("Invalid help topic {}!".format(topic)) 

url = QUrl('qute://help/{}'.format(path)) 

self._open(url, tab, bg, window) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def messages(self, level='info', plain=False, tab=False, bg=False, 

window=False): 

"""Show a log of past messages. 

 

Args: 

level: Include messages with `level` or higher severity. 

Valid values: vdebug, debug, info, warning, error, critical. 

plain: Whether to show plaintext (as opposed to html). 

tab: Open in a new tab. 

bg: Open in a background tab. 

window: Open in a new window. 

""" 

if level.upper() not in log.LOG_LEVELS: 

raise cmdexc.CommandError("Invalid log level {}!".format(level)) 

if plain: 

url = QUrl('qute://plainlog?level={}'.format(level)) 

else: 

url = QUrl('qute://log?level={}'.format(level)) 

self._open(url, tab, bg, window) 

 

def _open_editor_cb(self, elem): 

"""Open editor after the focus elem was found in open_editor.""" 

if elem is None: 

message.error("No element focused!") 

return 

if not elem.is_editable(strict=True): 

message.error("Focused element is not editable!") 

return 

 

text = elem.value() 

if text is None: 

message.error("Could not get text from the focused element.") 

return 

assert isinstance(text, str), text 

 

caret_position = elem.caret_position() 

 

ed = editor.ExternalEditor(watch=True, parent=self._tabbed_browser) 

ed.file_updated.connect(functools.partial( 

self.on_file_updated, elem)) 

ed.editing_finished.connect(lambda: mainwindow.raise_window( 

objreg.last_focused_window(), alert=False)) 

ed.edit(text, caret_position) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def open_editor(self): 

"""Open an external editor with the currently selected form field. 

 

The editor which should be launched can be configured via the 

`editor.command` config option. 

""" 

tab = self._current_widget() 

tab.elements.find_focused(self._open_editor_cb) 

 

def on_file_updated(self, elem, text): 

"""Write the editor text into the form field and clean up tempfile. 

 

Callback for GUIProcess when the edited text was updated. 

 

Args: 

elem: The WebElementWrapper which was modified. 

text: The new text to insert. 

""" 

try: 

elem.set_value(text) 

except webelem.OrphanedError as e: 

message.error('Edited element vanished') 

except webelem.Error as e: 

raise cmdexc.CommandError(str(e)) 

 

@cmdutils.register(instance='command-dispatcher', maxsplit=0, 

scope='window') 

def insert_text(self, text): 

"""Insert text at cursor position. 

 

Args: 

text: The text to insert. 

""" 

tab = self._current_widget() 

 

def _insert_text_cb(elem): 

if elem is None: 

message.error("No element focused!") 

return 

try: 

elem.insert_text(text) 

except webelem.Error as e: 

message.error(str(e)) 

return 

 

tab.elements.find_focused(_insert_text_cb) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('filter_', choices=['id']) 

def click_element(self, filter_: str, value, *, 

target: usertypes.ClickTarget = 

usertypes.ClickTarget.normal, 

force_event=False): 

"""Click the element matching the given filter. 

 

The given filter needs to result in exactly one element, otherwise, an 

error is shown. 

 

Args: 

filter_: How to filter the elements. 

id: Get an element based on its ID. 

value: The value to filter for. 

target: How to open the clicked element (normal/tab/tab-bg/window). 

force_event: Force generating a fake click event. 

""" 

tab = self._current_widget() 

 

def single_cb(elem): 

"""Click a single element.""" 

if elem is None: 

message.error("No element found with id {}!".format(value)) 

return 

try: 

elem.click(target, force_event=force_event) 

except webelem.Error as e: 

message.error(str(e)) 

return 

 

# def multiple_cb(elems): 

# """Click multiple elements (with only one expected).""" 

# if not elems: 

# message.error("No element found!") 

# return 

# elif len(elems) != 1: 

# message.error("{} elements found!".format(len(elems))) 

# return 

# elems[0].click(target) 

 

handlers = { 

'id': (tab.elements.find_id, single_cb), 

} 

handler, callback = handlers[filter_] 

handler(value, callback) 

 

def _search_cb(self, found, *, tab, old_scroll_pos, options, text, prev): 

"""Callback called from search/search_next/search_prev. 

 

Args: 

found: Whether the text was found. 

tab: The AbstractTab in which the search was made. 

old_scroll_pos: The scroll position (QPoint) before the search. 

options: The options (dict) the search was made with. 

text: The text searched for. 

prev: Whether we're searching backwards (i.e. :search-prev) 

""" 

# :search/:search-next without reverse -> down 

# :search/:search-next with reverse -> up 

# :search-prev without reverse -> up 

# :search-prev with reverse -> down 

going_up = options['reverse'] ^ prev 

 

if found: 

# Check if the scroll position got smaller and show info. 

if not going_up and tab.scroller.pos_px().y() < old_scroll_pos.y(): 

message.info("Search hit BOTTOM, continuing at TOP") 

elif going_up and tab.scroller.pos_px().y() > old_scroll_pos.y(): 

message.info("Search hit TOP, continuing at BOTTOM") 

else: 

message.warning("Text '{}' not found on page!".format(text), 

replace=True) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0) 

def search(self, text="", reverse=False): 

"""Search for a text on the current page. With no text, clear results. 

 

Args: 

text: The text to search for. 

reverse: Reverse search direction. 

""" 

self.set_mark("'") 

tab = self._current_widget() 

if tab.search.search_displayed: 

tab.search.clear() 

 

if not text: 

return 

 

options = { 

'ignore_case': config.val.search.ignore_case, 

'reverse': reverse, 

} 

 

self._tabbed_browser.search_text = text 

self._tabbed_browser.search_options = dict(options) 

 

cb = functools.partial(self._search_cb, tab=tab, 

old_scroll_pos=tab.scroller.pos_px(), 

options=options, text=text, prev=False) 

options['result_cb'] = cb 

 

tab.search.search(text, **options) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def search_next(self, count=1): 

"""Continue the search to the ([count]th) next term. 

 

Args: 

count: How many elements to ignore. 

""" 

tab = self._current_widget() 

window_text = self._tabbed_browser.search_text 

window_options = self._tabbed_browser.search_options 

 

if window_text is None: 

raise cmdexc.CommandError("No search done yet.") 

 

self.set_mark("'") 

 

if window_text is not None and window_text != tab.search.text: 

tab.search.clear() 

tab.search.search(window_text, **window_options) 

count -= 1 

 

if count == 0: 

return 

 

cb = functools.partial(self._search_cb, tab=tab, 

old_scroll_pos=tab.scroller.pos_px(), 

options=window_options, text=window_text, 

prev=False) 

 

for _ in range(count - 1): 

tab.search.next_result() 

tab.search.next_result(result_cb=cb) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

@cmdutils.argument('count', count=True) 

def search_prev(self, count=1): 

"""Continue the search to the ([count]th) previous term. 

 

Args: 

count: How many elements to ignore. 

""" 

tab = self._current_widget() 

window_text = self._tabbed_browser.search_text 

window_options = self._tabbed_browser.search_options 

 

if window_text is None: 

raise cmdexc.CommandError("No search done yet.") 

 

self.set_mark("'") 

 

if window_text is not None and window_text != tab.search.text: 

tab.search.clear() 

tab.search.search(window_text, **window_options) 

count -= 1 

 

if count == 0: 

return 

 

cb = functools.partial(self._search_cb, tab=tab, 

old_scroll_pos=tab.scroller.pos_px(), 

options=window_options, text=window_text, 

prev=True) 

 

for _ in range(count - 1): 

tab.search.prev_result() 

tab.search.prev_result(result_cb=cb) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_next_line(self, count=1): 

"""Move the cursor or selection to the next line. 

 

Args: 

count: How many lines to move. 

""" 

self._current_widget().caret.move_to_next_line(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_prev_line(self, count=1): 

"""Move the cursor or selection to the prev line. 

 

Args: 

count: How many lines to move. 

""" 

self._current_widget().caret.move_to_prev_line(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_next_char(self, count=1): 

"""Move the cursor or selection to the next char. 

 

Args: 

count: How many lines to move. 

""" 

self._current_widget().caret.move_to_next_char(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_prev_char(self, count=1): 

"""Move the cursor or selection to the previous char. 

 

Args: 

count: How many chars to move. 

""" 

self._current_widget().caret.move_to_prev_char(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_end_of_word(self, count=1): 

"""Move the cursor or selection to the end of the word. 

 

Args: 

count: How many words to move. 

""" 

self._current_widget().caret.move_to_end_of_word(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_next_word(self, count=1): 

"""Move the cursor or selection to the next word. 

 

Args: 

count: How many words to move. 

""" 

self._current_widget().caret.move_to_next_word(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_prev_word(self, count=1): 

"""Move the cursor or selection to the previous word. 

 

Args: 

count: How many words to move. 

""" 

self._current_widget().caret.move_to_prev_word(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

def move_to_start_of_line(self): 

"""Move the cursor or selection to the start of the line.""" 

self._current_widget().caret.move_to_start_of_line() 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

def move_to_end_of_line(self): 

"""Move the cursor or selection to the end of line.""" 

self._current_widget().caret.move_to_end_of_line() 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_start_of_next_block(self, count=1): 

"""Move the cursor or selection to the start of next block. 

 

Args: 

count: How many blocks to move. 

""" 

self._current_widget().caret.move_to_start_of_next_block(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_start_of_prev_block(self, count=1): 

"""Move the cursor or selection to the start of previous block. 

 

Args: 

count: How many blocks to move. 

""" 

self._current_widget().caret.move_to_start_of_prev_block(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_end_of_next_block(self, count=1): 

"""Move the cursor or selection to the end of next block. 

 

Args: 

count: How many blocks to move. 

""" 

self._current_widget().caret.move_to_end_of_next_block(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

@cmdutils.argument('count', count=True) 

def move_to_end_of_prev_block(self, count=1): 

"""Move the cursor or selection to the end of previous block. 

 

Args: 

count: How many blocks to move. 

""" 

self._current_widget().caret.move_to_end_of_prev_block(count) 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

def move_to_start_of_document(self): 

"""Move the cursor or selection to the start of the document.""" 

self._current_widget().caret.move_to_start_of_document() 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

def move_to_end_of_document(self): 

"""Move the cursor or selection to the end of the document.""" 

self._current_widget().caret.move_to_end_of_document() 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

def toggle_selection(self): 

"""Toggle caret selection mode.""" 

self._current_widget().caret.toggle_selection() 

 

@cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], 

scope='window') 

def drop_selection(self): 

"""Drop selection and keep selection mode enabled.""" 

self._current_widget().caret.drop_selection() 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

debug=True) 

@cmdutils.argument('count', count=True) 

def debug_webaction(self, action, count=1): 

"""Execute a webaction. 

 

Available actions: 

http://doc.qt.io/archives/qt-5.5/qwebpage.html#WebAction-enum (WebKit) 

http://doc.qt.io/qt-5/qwebenginepage.html#WebAction-enum (WebEngine) 

 

Args: 

action: The action to execute, e.g. MoveToNextChar. 

count: How many times to repeat the action. 

""" 

tab = self._current_widget() 

for _ in range(count): 

try: 

tab.action.run_string(action) 

except browsertab.WebTabError as e: 

raise cmdexc.CommandError(str(e)) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

maxsplit=0, no_cmd_split=True) 

def jseval(self, js_code, file=False, quiet=False, *, 

world: typing.Union[usertypes.JsWorld, int] = None): 

"""Evaluate a JavaScript string. 

 

Args: 

js_code: The string/file to evaluate. 

file: Interpret js-code as a path to a file. 

If the path is relative, the file is searched in a js/ subdir 

in qutebrowser's data dir, e.g. 

`~/.local/share/qutebrowser/js`. 

quiet: Don't show resulting JS object. 

world: Ignored on QtWebKit. On QtWebEngine, a world ID or name to 

run the snippet in. 

""" 

if world is None: 

world = usertypes.JsWorld.jseval 

 

if quiet: 

jseval_cb = None 

else: 

def jseval_cb(out): 

"""Show the data returned from JS.""" 

if out is None: 

# Getting the actual error (if any) seems to be difficult. 

# The error does end up in 

# BrowserPage.javaScriptConsoleMessage(), but 

# distinguishing between :jseval errors and errors from the 

# webpage is not trivial... 

message.info('No output or error') 

else: 

# The output can be a string, number, dict, array, etc. But 

# *don't* output too much data, as this will make 

# qutebrowser hang 

out = str(out) 

if len(out) > 5000: 

out = out[:5000] + ' [...trimmed...]' 

message.info(out) 

 

if file: 

path = os.path.expanduser(js_code) 

if not os.path.isabs(path): 

path = os.path.join(standarddir.data(), 'js', path) 

 

try: 

with open(path, 'r', encoding='utf-8') as f: 

js_code = f.read() 

except OSError as e: 

raise cmdexc.CommandError(str(e)) 

 

widget = self._current_widget() 

widget.run_js_async(js_code, callback=jseval_cb, world=world) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def fake_key(self, keystring, global_=False): 

"""Send a fake keypress or key string to the website or qutebrowser. 

 

:fake-key xy - sends the keychain 'xy' 

:fake-key <Ctrl-x> - sends Ctrl-x 

:fake-key <Escape> - sends the escape key 

 

Args: 

keystring: The keystring to send. 

global_: If given, the keys are sent to the qutebrowser UI. 

""" 

try: 

keyinfos = utils.parse_keystring(keystring) 

except utils.KeyParseError as e: 

raise cmdexc.CommandError(str(e)) 

 

for keyinfo in keyinfos: 

press_event = QKeyEvent(QEvent.KeyPress, keyinfo.key, 

keyinfo.modifiers, keyinfo.text) 

release_event = QKeyEvent(QEvent.KeyRelease, keyinfo.key, 

keyinfo.modifiers, keyinfo.text) 

 

if global_: 

window = QApplication.focusWindow() 

if window is None: 

raise cmdexc.CommandError("No focused window!") 

QApplication.postEvent(window, press_event) 

QApplication.postEvent(window, release_event) 

else: 

tab = self._current_widget() 

tab.send_event(press_event) 

tab.send_event(release_event) 

 

@cmdutils.register(instance='command-dispatcher', scope='window', 

debug=True, backend=usertypes.Backend.QtWebKit) 

def debug_clear_ssl_errors(self): 

"""Clear remembered SSL error answers.""" 

self._current_widget().clear_ssl_errors() 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def edit_url(self, url=None, bg=False, tab=False, window=False, 

private=False, related=False): 

"""Navigate to a url formed in an external editor. 

 

The editor which should be launched can be configured via the 

`editor.command` config option. 

 

Args: 

url: URL to edit; defaults to the current page url. 

bg: Open in a new background tab. 

tab: Open in a new tab. 

window: Open in a new window. 

private: Open a new window in private browsing mode. 

related: If opening a new tab, position the tab as related to the 

current one (like clicking on a link). 

""" 

cmdutils.check_exclusive((tab, bg, window), 'tbw') 

 

old_url = self._current_url().toString() 

 

ed = editor.ExternalEditor(self._tabbed_browser) 

 

# Passthrough for openurl args (e.g. -t, -b, -w) 

ed.file_updated.connect(functools.partial( 

self._open_if_changed, old_url=old_url, bg=bg, tab=tab, 

window=window, private=private, related=related)) 

 

ed.edit(url or old_url) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def set_mark(self, key): 

"""Set a mark at the current scroll position in the current tab. 

 

Args: 

key: mark identifier; capital indicates a global mark 

""" 

self._tabbed_browser.set_mark(key) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def jump_mark(self, key): 

"""Jump to the mark named by `key`. 

 

Args: 

key: mark identifier; capital indicates a global mark 

""" 

self._tabbed_browser.jump_mark(key) 

 

def _open_if_changed(self, url=None, old_url=None, bg=False, tab=False, 

window=False, private=False, related=False): 

"""Open a URL unless it's already open in the tab. 

 

Args: 

old_url: The original URL to compare against. 

url: The URL to open. 

bg: Open in a new background tab. 

tab: Open in a new tab. 

window: Open in a new window. 

private: Open a new window in private browsing mode. 

related: If opening a new tab, position the tab as related to the 

current one (like clicking on a link). 

""" 

if bg or tab or window or private or related or url != old_url: 

self.openurl(url=url, bg=bg, tab=tab, window=window, 

private=private, related=related) 

 

@cmdutils.register(instance='command-dispatcher', scope='window') 

def fullscreen(self, leave=False): 

"""Toggle fullscreen mode. 

 

Args: 

leave: Only leave fullscreen if it was entered by the page. 

""" 

if leave: 

tab = self._current_widget() 

try: 

tab.action.exit_fullscreen() 

except browsertab.UnsupportedOperationError: 

pass 

return 

 

window = self._tabbed_browser.window() 

window.setWindowState(window.windowState() ^ Qt.WindowFullScreen)