From 9a384c504547424f6ba2fc9eb72d1464719bcde4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 25 Jun 2020 08:46:05 +0530 Subject: [PATCH] Allow passing the current selection to kittens Fixes #2796 --- docs/changelog.rst | 2 ++ docs/kittens/custom.rst | 6 ++++-- kitty/boss.py | 12 +++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 0be59d85e..34f739eec 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -15,6 +15,8 @@ To update |kitty|, :doc:`follow the instructions `. - macOS: Fix the LC_TYPE env var being set to UTF-8 on systems in which the language and country code do not form a valid locale (:iss:`1233`) +- Allow passing the current selection to kittens (:iss:`2796`) + 0.18.1 [2020-06-23] -------------------- diff --git a/docs/kittens/custom.rst b/docs/kittens/custom.rst index 415a316c8..eb5b3cc0e 100644 --- a/docs/kittens/custom.rst +++ b/docs/kittens/custom.rst @@ -58,7 +58,8 @@ You can pass arguments to kittens by defining them in the map directive in These will be available as the ``args`` parameter in the ``main()`` and ``handle_result()`` functions. Note also that the current working directory of the kitten is set to the working directory of whatever program is -running in the active kitty window. +running in the active kitty window. The special argument ``@selection`` +is replaced by the currently selected text in the active kitty window. Passing the contents of the screen to the kitten @@ -87,7 +88,8 @@ This will send the plain text of the active window to the kitten's :file:`STDIN`. For text with formatting escape codes, use ``ansi`` instead. If you want line wrap markers as well, use ``screen-ansi`` or just ``screen``. For the scrollback buffer as well, use -``history``, ``ansi-history`` or ``screen-history``. +``history``, ``ansi-history`` or ``screen-history``. To get +the currently selected text, use ``selection``. Using kittens to script kitty, without any terminal UI diff --git a/kitty/boss.py b/kitty/boss.py index 4c804db83..c52383199 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -851,6 +851,9 @@ class Boss: add_history='history' in type_of_input, add_wrap_markers='screen' in type_of_input ).encode('utf-8') + elif type_of_input == 'selection': + sel = self.data_for_at(which='@selection', window=w) + data = sel.encode('utf-8') if sel else None elif type_of_input is None: data = None else: @@ -858,9 +861,16 @@ class Boss: else: data = input_data if isinstance(input_data, bytes) else input_data.encode('utf-8') copts = common_opts_as_dict(self.opts) + final_args: List[str] = [] + for x in args: + if x == '@selection': + sel = self.data_for_at(which='@selection', window=w) + if sel: + x = sel + final_args.append(x) overlay_window = tab.new_special_window( SpecialWindow( - [kitty_exe(), '+runpy', 'from kittens.runner import main; main()'] + args, + [kitty_exe(), '+runpy', 'from kittens.runner import main; main()'] + final_args, stdin=data, env={ 'KITTY_COMMON_OPTS': json.dumps(copts),