diff --git a/docs/changelog.rst b/docs/changelog.rst index 1e4a2ed8e..5f9000459 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,9 @@ Changelog 0.13.4 [future] --------------------- +- A new option :opt:`strip_trailing_spaces` to optionally remove trailing + spaces from lines when copying to clipboard. + - macOS: Fix :kbd:`cmd+period` key not working (:iss:`1318`) diff --git a/kitty/config_data.py b/kitty/config_data.py index 0fb89493b..07be6a170 100644 --- a/kitty/config_data.py +++ b/kitty/config_data.py @@ -398,6 +398,11 @@ such as macOS that do not have the concept of primary selections. Note that this is a security risk, as all programs, including websites open in your browser can read the contents of the clipboard.''')) +o('strip_trailing_spaces', 'never', option_type=choices('never', 'smart', 'always'), long_text=_(''' +Remove spaces at the end of lines when copying to clipboard. +A value of :code:`smart` will do it when using normal selections, but not rectangle +selections. :code:`always` will always do it.''')) + o('rectangle_select_modifiers', 'ctrl+alt', option_type=to_modifiers, long_text=_(''' The modifiers to use rectangular selection (i.e. to select text in a rectangular block with the mouse)''')) diff --git a/kitty/screen.c b/kitty/screen.c index a91f832e9..b77147e0f 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1918,6 +1918,13 @@ start_selection(Screen *self, PyObject *args) { Py_RETURN_NONE; } +static PyObject* +is_rectangle_select(Screen *self, PyObject *a UNUSED) { + PyObject *ans = self->selection.rectangle_select ? Py_True : Py_False; + Py_INCREF(ans); + return ans; +} + static PyObject* text_for_selection(Screen *self, PyObject *a UNUSED) { FullSelectionBoundary start, end; @@ -2211,6 +2218,7 @@ static PyMethodDef methods[] = { MND(set_margins, METH_VARARGS) MND(rescale_images, METH_NOARGS) MND(text_for_selection, METH_NOARGS) + MND(is_rectangle_select, METH_NOARGS) MND(scroll, METH_VARARGS) MND(send_escape_code_to_child, METH_VARARGS) MND(toggle_alt_screen, METH_NOARGS) diff --git a/kitty/window.py b/kitty/window.py index ccc3b42f6..80c1bcdf2 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -451,7 +451,11 @@ class Window: # }}} def text_for_selection(self): - return ''.join(self.screen.text_for_selection()) + lines = self.screen.text_for_selection() + if self.opts.strip_trailing_spaces == 'always' or ( + self.opts.strip_trailing_spaces == 'smart' and not self.screen.is_rectangle_select()): + lines = [l.rstrip() for l in lines] + return ''.join(lines) def destroy(self): self.destroyed = True