Add readline-like shortcuts

This commit is contained in:
Florian Bruhin 2014-05-22 10:49:19 +02:00
parent 3a3d8fddee
commit 2bcf46194b
4 changed files with 156 additions and 1 deletions

1
TODO
View File

@ -58,7 +58,6 @@ Improvements / minor features
- Close tabs on right click - Close tabs on right click
- Up/Down not for history - Up/Down not for history
- search highlighting - search highlighting
- readline like shortcuts (like C-w) for command prompt
- max height for completion (be smaller if possible) - max height for completion (be smaller if possible)
- tab should directly insert word and space if there's only one option - tab should directly insert word and space if there's only one option
- vertical tabbar - vertical tabbar

View File

@ -66,6 +66,7 @@ from qutebrowser.browser.cookies import CookieJar
from qutebrowser.utils.message import MessageBridge from qutebrowser.utils.message import MessageBridge
from qutebrowser.utils.misc import (dotted_getattr, get_standard_dir, from qutebrowser.utils.misc import (dotted_getattr, get_standard_dir,
actute_warning) actute_warning)
from qutebrowser.utils.readline import ReadlineBridge
from qutebrowser.utils.debug import set_trace # pylint: disable=unused-import from qutebrowser.utils.debug import set_trace # pylint: disable=unused-import
@ -252,6 +253,7 @@ class QuteBrowser(QApplication):
self.setApplicationName("qutebrowser") self.setApplicationName("qutebrowser")
self.setApplicationVersion(qutebrowser.__version__) self.setApplicationVersion(qutebrowser.__version__)
self.messagebridge = MessageBridge() self.messagebridge = MessageBridge()
self.rl_bridge = ReadlineBridge()
def _handle_segfault(self): def _handle_segfault(self):
"""Handle a segfault from a previous run.""" """Handle a segfault from a previous run."""

View File

@ -690,6 +690,9 @@ DATA = OrderedDict([
('<Escape>', 'leave-mode'), ('<Escape>', 'leave-mode'),
)), )),
# FIXME we should probably have a common section for input modes with a
# text field.
('keybind.command', sect.ValueList( ('keybind.command', sect.ValueList(
types.KeyBindingName(), types.KeyBinding(), types.KeyBindingName(), types.KeyBinding(),
('<Escape>', 'leave-mode'), ('<Escape>', 'leave-mode'),
@ -699,6 +702,17 @@ DATA = OrderedDict([
('<Shift-Tab>', 'completion-item-prev'), ('<Shift-Tab>', 'completion-item-prev'),
('<Tab>', 'completion-item-next'), ('<Tab>', 'completion-item-next'),
('<Return>', 'command-accept'), ('<Return>', 'command-accept'),
('<Ctrl-B>', 'rl-backward-char'),
('<Ctrl-F>', 'rl-forward-char'),
('<Alt-B>', 'rl-backward-word'),
('<Alt-F>', 'rl-forward-word'),
('<Ctrl-A>', 'rl-beginning-of-line'),
('<Ctrl-E>', 'rl-end-of-line'),
('<Ctrl-U>', 'rl-unix-line-discard'),
('<Ctrl-K>', 'rl-kill-line'),
('<Alt-D>', 'rl-kill-word'),
('<Ctrl-W>', 'rl-unix-word-rubout'),
('<Ctrl-Y>', 'rl-yank'),
)), )),
('keybind.prompt', sect.ValueList( ('keybind.prompt', sect.ValueList(
@ -708,6 +722,17 @@ DATA = OrderedDict([
('<Return>', 'prompt-accept'), ('<Return>', 'prompt-accept'),
('y', 'prompt-yes'), ('y', 'prompt-yes'),
('n', 'prompt-no'), ('n', 'prompt-no'),
('<Ctrl-B>', 'rl-backward-char'),
('<Ctrl-F>', 'rl-forward-char'),
('<Alt-B>', 'rl-backward-word'),
('<Alt-F>', 'rl-forward-word'),
('<Ctrl-A>', 'rl-beginning-of-line'),
('<Ctrl-E>', 'rl-end-of-line'),
('<Ctrl-U>', 'rl-unix-line-discard'),
('<Ctrl-K>', 'rl-kill-line'),
('<Alt-D>', 'rl-kill-word'),
('<Ctrl-W>', 'rl-unix-word-rubout'),
('<Ctrl-Y>', 'rl-yank'),
)), )),
('aliases', sect.ValueList( ('aliases', sect.ValueList(

View File

@ -0,0 +1,129 @@
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Bridge to provide readline-like shortcuts for QLineEdits."""
from PyQt5.QtWidgets import QApplication, QLineEdit
import qutebrowser.commands.utils as cmd
import qutebrowser.keyinput.modeman as modeman
class ReadlineBridge:
"""Bridge which provides readline-like commands for the current QLineEdit.
Attributes:
deleted: Mapping from widgets to their last deleted text.
"""
def __init__(self):
self.deleted = {}
@property
def widget(self):
"""Get the currently active QLineEdit."""
w = QApplication.instance().focusWidget()
if isinstance(w, QLineEdit):
return w
else:
return None
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_backward_char(self):
"""Readline: Move back a character."""
if self.widget is None:
return
self.widget.cursorBackward(False)
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_forward_char(self):
"""Readline: Move forward a character."""
if self.widget is None:
return
self.widget.cursorForward(False)
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_backward_word(self):
"""Readline: Move back to the start of the current or previous word."""
if self.widget is None:
return
self.widget.cursorWordBackward(False)
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_forward_word(self):
"""Readline: Move forward to the end of the next word."""
if self.widget is None:
return
self.widget.cursorWordForward(False)
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_beginning_of_line(self):
"""Readline: Move to the start of the current line."""
if self.widget is None:
return
self.widget.home(False)
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_end_of_line(self):
"""Readline: Move to the end of the line."""
if self.widget is None:
return
self.widget.end(False)
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_unix_line_discard(self):
"""Readline: Kill backward from cursor to the beginning of the line."""
if self.widget is None:
return
self.widget.home(True)
self.deleted[self.widget] = self.widget.selectedText()
self.widget.del_()
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_kill_line(self):
"""Readline: Kill the text from point to the end of the line."""
if self.widget is None:
return
self.widget.home(True)
self.deleted[self.widget] = self.widget.selectedText()
self.widget.del_()
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_unix_word_rubout(self):
"""Readline: Kill the word behind point."""
if self.widget is None:
return
self.widget.cursorWordBackward(True)
self.deleted[self.widget] = self.widget.selectedText()
self.widget.del_()
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_kill_word(self):
"""Readline: Kill from point to the end of the current word."""
if self.widget is None:
return
self.widget.cursorWordForward(True)
self.deleted[self.widget] = self.widget.selectedText()
self.widget.del_()
@cmd.register(instance='rl_bridge', hide=True, modes=['command', 'prompt'])
def rl_yank(self):
"""Readline: Yank the top of the kill ring into the buffer at point."""
if self.widget is None or self.widget not in self.deleted:
return
self.widget.insert(self.deleted[self.widget])