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

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

 

"""KeyChainParser for "hint" and "normal" modes. 

 

Module attributes: 

STARTCHARS: Possible chars for starting a commandline input. 

""" 

 

import traceback 

import enum 

 

from PyQt5.QtCore import pyqtSlot, Qt 

 

from qutebrowser.commands import cmdexc 

from qutebrowser.config import config 

from qutebrowser.keyinput import keyparser 

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

 

 

STARTCHARS = ":/?" 

LastPress = enum.Enum('LastPress', ['none', 'filtertext', 'keystring']) 

 

 

class NormalKeyParser(keyparser.CommandKeyParser): 

 

"""KeyParser for normal mode with added STARTCHARS detection and more. 

 

Attributes: 

_partial_timer: Timer to clear partial keypresses. 

""" 

 

def __init__(self, win_id, parent=None): 

super().__init__(win_id, parent, supports_count=True, 

supports_chains=True) 

self._read_config('normal') 

self._partial_timer = usertypes.Timer(self, 'partial-match') 

self._partial_timer.setSingleShot(True) 

self._inhibited = False 

self._inhibited_timer = usertypes.Timer(self, 'normal-inhibited') 

self._inhibited_timer.setSingleShot(True) 

 

def __repr__(self): 

return utils.get_repr(self) 

 

def _handle_single_key(self, e): 

"""Override _handle_single_key to abort if the key is a startchar. 

 

Args: 

e: the KeyPressEvent from Qt. 

 

Return: 

A self.Match member. 

""" 

txt = e.text().strip() 

72 ↛ 73line 72 didn't jump to line 73, because the condition on line 72 was never true if self._inhibited: 

self._debug_log("Ignoring key '{}', because the normal mode is " 

"currently inhibited.".format(txt)) 

return self.Match.none 

match = super()._handle_single_key(e) 

if match == self.Match.partial: 

timeout = config.val.input.partial_timeout 

79 ↛ 83line 79 didn't jump to line 83, because the condition on line 79 was never false if timeout != 0: 

self._partial_timer.setInterval(timeout) 

self._partial_timer.timeout.connect(self._clear_partial_match) 

self._partial_timer.start() 

return match 

 

def set_inhibited_timeout(self, timeout): 

"""Ignore keypresses for the given duration.""" 

if timeout != 0: 

self._debug_log("Inhibiting the normal mode for {}ms.".format( 

timeout)) 

self._inhibited = True 

self._inhibited_timer.setInterval(timeout) 

self._inhibited_timer.timeout.connect(self._clear_inhibited) 

self._inhibited_timer.start() 

 

@pyqtSlot() 

def _clear_partial_match(self): 

"""Clear a partial keystring after a timeout.""" 

self._debug_log("Clearing partial keystring {}".format( 

self._keystring)) 

self._keystring = '' 

self.keystring_updated.emit(self._keystring) 

 

@pyqtSlot() 

def _clear_inhibited(self): 

"""Reset inhibition state after a timeout.""" 

self._debug_log("Releasing inhibition state of normal mode.") 

self._inhibited = False 

 

@pyqtSlot() 

def _stop_timers(self): 

super()._stop_timers() 

self._partial_timer.stop() 

try: 

self._partial_timer.timeout.disconnect(self._clear_partial_match) 

except TypeError: 

# no connections 

pass 

self._inhibited_timer.stop() 

try: 

self._inhibited_timer.timeout.disconnect(self._clear_inhibited) 

except TypeError: 

# no connections 

pass 

 

 

class PromptKeyParser(keyparser.CommandKeyParser): 

 

"""KeyParser for yes/no prompts.""" 

 

def __init__(self, win_id, parent=None): 

super().__init__(win_id, parent, supports_count=False, 

supports_chains=True) 

# We don't want an extra section for this in the config, so we just 

# abuse the prompt section. 

self._read_config('prompt') 

 

def __repr__(self): 

return utils.get_repr(self) 

 

 

class HintKeyParser(keyparser.CommandKeyParser): 

 

"""KeyChainParser for hints. 

 

Attributes: 

_filtertext: The text to filter with. 

_last_press: The nature of the last keypress, a LastPress member. 

""" 

 

def __init__(self, win_id, parent=None): 

super().__init__(win_id, parent, supports_count=False, 

supports_chains=True) 

self._filtertext = '' 

self._last_press = LastPress.none 

self._read_config('hint') 

self.keystring_updated.connect(self.on_keystring_updated) 

 

def _handle_special_key(self, e): 

"""Override _handle_special_key to handle string filtering. 

 

Return True if the keypress has been handled, and False if not. 

 

Args: 

e: the KeyPressEvent from Qt. 

 

Return: 

True if event has been handled, False otherwise. 

""" 

log.keyboard.debug("Got special key 0x{:x} text {}".format( 

e.key(), e.text())) 

hintmanager = objreg.get('hintmanager', scope='tab', 

window=self._win_id, tab='current') 

if e.key() == Qt.Key_Backspace: 

log.keyboard.debug("Got backspace, mode {}, filtertext '{}', " 

"keystring '{}'".format(self._last_press, 

self._filtertext, 

self._keystring)) 

if self._last_press == LastPress.filtertext and self._filtertext: 

self._filtertext = self._filtertext[:-1] 

hintmanager.filter_hints(self._filtertext) 

return True 

elif self._last_press == LastPress.keystring and self._keystring: 

self._keystring = self._keystring[:-1] 

self.keystring_updated.emit(self._keystring) 

if not self._keystring and self._filtertext: 

# Switch back to hint filtering mode (this can happen only 

# in numeric mode after the number has been deleted). 

hintmanager.filter_hints(self._filtertext) 

self._last_press = LastPress.filtertext 

return True 

else: 

return super()._handle_special_key(e) 

elif hintmanager.current_mode() != 'number': 

return super()._handle_special_key(e) 

elif not e.text(): 

return super()._handle_special_key(e) 

else: 

self._filtertext += e.text() 

hintmanager.filter_hints(self._filtertext) 

self._last_press = LastPress.filtertext 

return True 

 

def handle(self, e): 

"""Handle a new keypress and call the respective handlers. 

 

Args: 

e: the KeyPressEvent from Qt 

 

Returns: 

True if the match has been handled, False otherwise. 

""" 

match = self._handle_single_key(e) 

if match == self.Match.partial: 

self.keystring_updated.emit(self._keystring) 

self._last_press = LastPress.keystring 

return True 

elif match == self.Match.definitive: 

self._last_press = LastPress.none 

return True 

elif match == self.Match.other: 

return None 

elif match == self.Match.none: 

# We couldn't find a keychain so we check if it's a special key. 

return self._handle_special_key(e) 

else: 

raise ValueError("Got invalid match type {}!".format(match)) 

 

def execute(self, cmdstr, keytype, count=None): 

"""Handle a completed keychain.""" 

if not isinstance(keytype, self.Type): 

raise TypeError("Type {} is no Type member!".format(keytype)) 

if keytype == self.Type.chain: 

hintmanager = objreg.get('hintmanager', scope='tab', 

window=self._win_id, tab='current') 

hintmanager.handle_partial_key(cmdstr) 

else: 

# execute as command 

super().execute(cmdstr, keytype, count) 

 

def update_bindings(self, strings, preserve_filter=False): 

"""Update bindings when the hint strings changed. 

 

Args: 

strings: A list of hint strings. 

preserve_filter: Whether to keep the current value of 

`self._filtertext`. 

""" 

self.bindings = {s: s for s in strings} 

if not preserve_filter: 

self._filtertext = '' 

 

@pyqtSlot(str) 

def on_keystring_updated(self, keystr): 

"""Update hintmanager when the keystring was updated.""" 

hintmanager = objreg.get('hintmanager', scope='tab', 

window=self._win_id, tab='current') 

hintmanager.handle_partial_key(keystr) 

 

 

class CaretKeyParser(keyparser.CommandKeyParser): 

 

"""KeyParser for caret mode.""" 

 

passthrough = True 

 

def __init__(self, win_id, parent=None): 

super().__init__(win_id, parent, supports_count=True, 

supports_chains=True) 

self._read_config('caret') 

 

 

class RegisterKeyParser(keyparser.CommandKeyParser): 

 

"""KeyParser for modes that record a register key. 

 

Attributes: 

_mode: One of KeyMode.set_mark, KeyMode.jump_mark, KeyMode.record_macro 

and KeyMode.run_macro. 

""" 

 

def __init__(self, win_id, mode, parent=None): 

super().__init__(win_id, parent, supports_count=False, 

supports_chains=False) 

self._mode = mode 

self._read_config('register') 

 

def handle(self, e): 

"""Override handle to always match the next key and use the register. 

 

Args: 

e: the KeyPressEvent from Qt. 

 

Return: 

True if event has been handled, False otherwise. 

""" 

if super().handle(e): 

return True 

 

key = e.text() 

 

if key == '' or utils.keyevent_to_string(e) is None: 

# this is not a proper register key, let it pass and keep going 

return False 

 

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

window=self._win_id) 

macro_recorder = objreg.get('macro-recorder') 

 

try: 

if self._mode == usertypes.KeyMode.set_mark: 

tabbed_browser.set_mark(key) 

elif self._mode == usertypes.KeyMode.jump_mark: 

tabbed_browser.jump_mark(key) 

elif self._mode == usertypes.KeyMode.record_macro: 

macro_recorder.record_macro(key) 

elif self._mode == usertypes.KeyMode.run_macro: 

macro_recorder.run_macro(self._win_id, key) 

else: 

raise ValueError( 

"{} is not a valid register mode".format(self._mode)) 

except cmdexc.Error as err: 

message.error(str(err), stack=traceback.format_exc()) 

 

self.request_leave.emit(self._mode, "valid register key", True) 

 

return True