Coverage for qutebrowser/misc/editor.py : 100%

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
# 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/>.
QFileSystemWatcher)
"""Class to simplify editing a text in an external editor.
Attributes: _text: The current text before the editor is opened. _filename: The name of the file to be edited. _remove_file: Whether the file should be removed when the editor is closed. _proc: The GUIProcess of the editor. _watcher: A QFileSystemWatcher to watch the edited file for changes. Only set if watch=True.
Signals: file_updated: The text in the edited file was updated. arg: The new text. editing_finished: The editor process was closed. """
"""Clean up temporary files after the editor closed."""
# Could not create initial file.
# NOTE: Do not replace this with "raise CommandError" as it's # executed async.
def on_proc_closed(self, _exitcode, exitstatus): """Write the editor text into the form field and clean up tempfile.
Callback for QProcess when the editor was closed. """ # No error/cleanup here, since we already handle this in # on_proc_error. # do a final read to make sure we don't miss the last signal
def on_proc_error(self, _err):
"""Edit a given text.
Args: text: The initial text to edit. caret_position: The position of the caret in the text. """ # Close while the external process is running, as otherwise systems # with exclusive write access (e.g. Windows) may fail to update # the file from the external editor, see # https://github.com/qutebrowser/qutebrowser/issues/1767 # pylint: disable=bad-continuation mode='w', prefix='qutebrowser-editor-', encoding=config.val.editor.encoding, delete=False) as fobj: # pylint: enable=bad-continuation
def _on_file_changed(self, path): # NOTE: Do not replace this with "raise CommandError" as it's # executed async.
"""Edit the file with the given filename."""
"""Start the editor with the file opened as self._filename.
Args: line: the line number to pass to the editor column: the column number to pass to the editor """
.format(self._filename))
r"""Calculate line and column numbers given a text and caret position.
Both line and column are 1-based indexes, because that's what most editors use as line and column starting index. By "most" we mean at least vim, nvim, gvim, emacs, atom, sublimetext, notepad++, brackets, visual studio, QtCreator and so on.
To find the line we just count how many newlines there are before the caret and add 1.
To find the column we calculate the difference between the caret and the last newline before the caret.
For example in the text `aaa\nbb|bbb` (| represents the caret): caret_position = 6 text[:caret_position] = `aaa\nbb` text[:caret_position].count('\n') = 1 caret_position - text[:caret_position].rfind('\n') = 3
Thus line, column = 2, 3, and the caret is indeed in the second line, third column
Args: text: the text for which the numbers must be calculated caret_position: the position of the caret in the text, or None
Return: A (line, column) tuple of (int, int) """
"""Substitute a single placeholder.
If the `arg` input to this function is a valid placeholder it will be substituted with the appropriate value, otherwise it will be left unchanged.
Args: arg: an argument of editor.command. line: the previously-calculated line number for the text caret. column: the previously-calculated column number for the text caret.
Return: The substituted placeholder or the original argument. """ '{}': self._filename, '{file}': self._filename, '{line}': str(line), '{line0}': str(line-1), '{column}': str(column), '{column0}': str(column-1) }
|