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

# 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/>. 

 

"""Misc. utility commands exposed to the user.""" 

 

import functools 

import os 

import signal 

import traceback 

 

try: 

import hunter 

except ImportError: 

hunter = None 

 

import sip 

from PyQt5.QtCore import QUrl 

# so it's available for :debug-pyeval 

from PyQt5.QtWidgets import QApplication # pylint: disable=unused-import 

 

from qutebrowser.browser import qutescheme 

from qutebrowser.utils import log, objreg, usertypes, message, debug, utils 

from qutebrowser.commands import cmdutils, runners, cmdexc 

from qutebrowser.config import config, configdata 

from qutebrowser.misc import consolewidget 

from qutebrowser.utils.version import pastebin_version 

 

 

@cmdutils.register(maxsplit=1, no_cmd_split=True, no_replace_variables=True) 

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

def later(ms: int, command, win_id): 

"""Execute a command after some time. 

 

Args: 

ms: How many milliseconds to wait. 

command: The command to run, with optional args. 

""" 

if ms < 0: 

raise cmdexc.CommandError("I can't run something in the past!") 

commandrunner = runners.CommandRunner(win_id) 

app = objreg.get('app') 

timer = usertypes.Timer(name='later', parent=app) 

try: 

timer.setSingleShot(True) 

try: 

timer.setInterval(ms) 

except OverflowError: 

raise cmdexc.CommandError("Numeric argument is too large for " 

"internal int representation.") 

timer.timeout.connect( 

functools.partial(commandrunner.run_safely, command)) 

timer.timeout.connect(timer.deleteLater) 

timer.start() 

except: 

timer.deleteLater() 

raise 

 

 

@cmdutils.register(maxsplit=1, no_cmd_split=True, no_replace_variables=True) 

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

def repeat(times: int, command, win_id): 

"""Repeat a given command. 

 

Args: 

times: How many times to repeat. 

command: The command to run, with optional args. 

""" 

if times < 0: 

raise cmdexc.CommandError("A negative count doesn't make sense.") 

commandrunner = runners.CommandRunner(win_id) 

for _ in range(times): 

commandrunner.run_safely(command) 

 

 

@cmdutils.register(maxsplit=1, no_cmd_split=True, no_replace_variables=True) 

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

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

def run_with_count(count_arg: int, command, win_id, count=1): 

"""Run a command with the given count. 

 

If run_with_count itself is run with a count, it multiplies count_arg. 

 

Args: 

count_arg: The count to pass to the command. 

command: The command to run, with optional args. 

count: The count that run_with_count itself received. 

""" 

runners.CommandRunner(win_id).run(command, count_arg * count) 

 

 

@cmdutils.register() 

def message_error(text): 

"""Show an error message in the statusbar. 

 

Args: 

text: The text to show. 

""" 

message.error(text) 

 

 

@cmdutils.register() 

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

def message_info(text, count=1): 

"""Show an info message in the statusbar. 

 

Args: 

text: The text to show. 

count: How many times to show the message 

""" 

for _ in range(count): 

message.info(text) 

 

 

@cmdutils.register() 

def message_warning(text): 

"""Show a warning message in the statusbar. 

 

Args: 

text: The text to show. 

""" 

message.warning(text) 

 

 

@cmdutils.register() 

def clear_messages(): 

"""Clear all message notifications.""" 

message.global_bridge.clear_messages.emit() 

 

 

@cmdutils.register(debug=True) 

@cmdutils.argument('typ', choices=['exception', 'segfault']) 

def debug_crash(typ='exception'): 

"""Crash for debugging purposes. 

 

Args: 

typ: either 'exception' or 'segfault'. 

""" 

if typ == 'segfault': 

os.kill(os.getpid(), signal.SIGSEGV) 

raise Exception("Segfault failed (wat.)") 

else: 

raise Exception("Forced crash") 

 

 

@cmdutils.register(debug=True) 

def debug_all_objects(): 

"""Print a list of all objects to the debug log.""" 

s = debug.get_all_objects() 

log.misc.debug(s) 

 

 

@cmdutils.register(debug=True) 

def debug_cache_stats(): 

"""Print LRU cache stats.""" 

prefix_info = configdata.is_valid_prefix.cache_info() 

# pylint: disable=protected-access 

render_stylesheet_info = config._render_stylesheet.cache_info() 

# pylint: enable=protected-access 

 

history_info = None 

try: 

from PyQt5.QtWebKit import QWebHistoryInterface 

interface = QWebHistoryInterface.defaultInterface() 

if interface is not None: 

history_info = interface.historyContains.cache_info() 

except ImportError: 

pass 

 

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

window='last-focused') 

# pylint: disable=protected-access 

tab_bar = tabbed_browser.tabBar() 

tabbed_browser_info = tab_bar._minimum_tab_size_hint_helper.cache_info() 

# pylint: enable=protected-access 

 

log.misc.debug('is_valid_prefix: {}'.format(prefix_info)) 

log.misc.debug('_render_stylesheet: {}'.format(render_stylesheet_info)) 

log.misc.debug('history: {}'.format(history_info)) 

log.misc.debug('tab width cache: {}'.format(tabbed_browser_info)) 

 

 

@cmdutils.register(debug=True) 

def debug_console(): 

"""Show the debugging console.""" 

try: 

con_widget = objreg.get('debug-console') 

except KeyError: 

log.misc.debug('initializing debug console') 

con_widget = consolewidget.ConsoleWidget() 

objreg.register('debug-console', con_widget) 

 

if con_widget.isVisible(): 

log.misc.debug('hiding debug console') 

con_widget.hide() 

else: 

log.misc.debug('showing debug console') 

con_widget.show() 

 

 

@cmdutils.register(debug=True, maxsplit=0, no_cmd_split=True) 

def debug_trace(expr=""): 

"""Trace executed code via hunter. 

 

Args: 

expr: What to trace, passed to hunter. 

""" 

if hunter is None: 

raise cmdexc.CommandError("You need to install 'hunter' to use this " 

"command!") 

try: 

eval('hunter.trace({})'.format(expr)) 

except Exception as e: 

raise cmdexc.CommandError("{}: {}".format(e.__class__.__name__, e)) 

 

 

@cmdutils.register(maxsplit=0, debug=True, no_cmd_split=True) 

def debug_pyeval(s, file=False, quiet=False): 

"""Evaluate a python string and display the results as a web page. 

 

Args: 

s: The string to evaluate. 

file: Interpret s as a path to file, also implies --quiet. 

quiet: Don't show the output in a new tab. 

""" 

if file: 

quiet = True 

path = os.path.expanduser(s) 

try: 

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

s = f.read() 

except OSError as e: 

raise cmdexc.CommandError(str(e)) 

try: 

exec(s) 

out = "No error" 

except Exception: 

out = traceback.format_exc() 

else: 

try: 

r = eval(s) 

out = repr(r) 

except Exception: 

out = traceback.format_exc() 

 

qutescheme.pyeval_output = out 

if quiet: 

log.misc.debug("pyeval output: {}".format(out)) 

else: 

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

window='last-focused') 

tabbed_browser.openurl(QUrl('qute://pyeval'), newtab=True) 

 

 

@cmdutils.register(debug=True) 

def debug_set_fake_clipboard(s=None): 

"""Put data into the fake clipboard and enable logging, used for tests. 

 

Args: 

s: The text to put into the fake clipboard, or unset to enable logging. 

""" 

if s is None: 

utils.log_clipboard = True 

else: 

utils.fake_clipboard = s 

 

 

@cmdutils.register() 

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

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

def repeat_command(win_id, count=None): 

"""Repeat the last executed command. 

 

Args: 

count: Which count to pass the command. 

""" 

mode_manager = objreg.get('mode-manager', scope='window', window=win_id) 

293 ↛ 295line 293 didn't jump to line 295, because the condition on line 293 was never false if mode_manager.mode not in runners.last_command: 

raise cmdexc.CommandError("You didn't do anything yet.") 

cmd = runners.last_command[mode_manager.mode] 

commandrunner = runners.CommandRunner(win_id) 

commandrunner.run(cmd[0], count if count is not None else cmd[1]) 

 

 

@cmdutils.register(debug=True, name='debug-log-capacity') 

def log_capacity(capacity: int): 

"""Change the number of log lines to be stored in RAM. 

 

Args: 

capacity: Number of lines for the log. 

""" 

if capacity < 0: 

raise cmdexc.CommandError("Can't set a negative log capacity!") 

else: 

log.ram_handler.change_log_capacity(capacity) 

 

 

@cmdutils.register(debug=True) 

@cmdutils.argument('level', choices=sorted( 

(level.lower() for level in log.LOG_LEVELS), 

key=lambda e: log.LOG_LEVELS[e.upper()])) 

def debug_log_level(level: str): 

"""Change the log level for console logging. 

 

Args: 

level: The log level to set. 

""" 

log.change_console_formatter(log.LOG_LEVELS[level.upper()]) 

log.console_handler.setLevel(log.LOG_LEVELS[level.upper()]) 

 

 

@cmdutils.register(debug=True) 

def debug_log_filter(filters: str): 

"""Change the log filter for console logging. 

 

Args: 

filters: A comma separated list of logger names. Can also be "none" to 

clear any existing filters. 

""" 

335 ↛ 336line 335 didn't jump to line 336, because the condition on line 335 was never true if log.console_filter is None: 

raise cmdexc.CommandError("No log.console_filter. Not attached " 

"to a console?") 

 

339 ↛ 340line 339 didn't jump to line 340, because the condition on line 339 was never true if filters.strip().lower() == 'none': 

log.console_filter.names = None 

return 

 

343 ↛ 344line 343 didn't jump to line 344, because the condition on line 343 was never true if not set(filters.split(',')).issubset(log.LOGGER_NAMES): 

raise cmdexc.CommandError("filters: Invalid value {} - expected one " 

"of: {}".format(filters, 

', '.join(log.LOGGER_NAMES))) 

 

log.console_filter.names = filters.split(',') 

 

 

@cmdutils.register() 

@cmdutils.argument('current_win_id', win_id=True) 

def window_only(current_win_id): 

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

for win_id, window in objreg.window_registry.items(): 

 

# We could be in the middle of destroying a window here 

if sip.isdeleted(window): 

continue 

 

if win_id != current_win_id: 

window.close() 

 

 

@cmdutils.register() 

def nop(): 

"""Do nothing.""" 

return 

 

 

@cmdutils.register() 

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

def version(win_id, paste=False): 

"""Show version information. 

 

Args: 

paste: Paste to pastebin. 

""" 

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

window=win_id) 

tabbed_browser.openurl(QUrl('qute://version'), newtab=True) 

 

383 ↛ 384line 383 didn't jump to line 384, because the condition on line 383 was never true if paste: 

pastebin_version()