Skip to content

Commit

Permalink
Cursor movement at shell prompts on click
Browse files Browse the repository at this point in the history
Fixes #3825
  • Loading branch information
kovidgoyal committed Aug 5, 2021
1 parent 7799d13 commit 376db2a
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 213 deletions.
194 changes: 0 additions & 194 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,200 +53,6 @@ kitty
* :doc:`graphics-protocol`
* :doc:`keyboard-protocol`
* Lots more in :doc:`protocol-extensions`
=======
:doc:`Panel <kittens/panel>`
Draw a GPU accelerated dock panel on your desktop showing the output
from an arbitrary terminal program.


:doc:`Clipboard <kittens/clipboard>`
Copy/paste to the clipboard from shell scripts, even over SSH.

You can also :doc:`Learn to create your own kittens <kittens/custom>`.


Configuring kitty
-------------------

|kitty| is highly configurable, everything from keyboard shortcuts to
painting frames-per-second. Press :sc:`edit_config_file` in kitty
to open its fully commented sample config file in your text editor.
For details see the :doc:`configuration docs <conf>`.


Remote control
------------------

|kitty| has a very powerful system that allows you to control it from the
:doc:`shell prompt, even over SSH <remote-control>`. You can change colors,
fonts, open new :term:`windows <window>`, :term:`tabs <tab>`, set their titles,
change window layout, get text
from one window and send text to another, etc, etc. The possibilities are
endless. See the :doc:`tutorial <remote-control>` to get started.

.. _sessions:

Startup Sessions
------------------

You can control the :term:`tabs <tab>`, `:term:`kitty window <window>` layout,
working directory, startup programs,
etc. by creating a "session" file and using the :option:`kitty --session`
command line flag or the :opt:`startup_session` option in :file:`kitty.conf`.
For example:

.. code-block:: session
# Set the layout for the current tab
layout tall
# Set the working directory for windows in the current tab
cd ~
# Create a window and run the specified command in it
launch zsh
# Create a window with some environment variables set and run
# vim in it
launch --env FOO=BAR vim
# Set the title for the next window
launch --title "Chat with x" irssi --profile x
# Create a new tab (the part after new_tab is the optional tab
# name which will be displayed in the tab bar, if omitted, the
# title of the active window will be used instead)
new_tab my tab
cd ~/somewhere
# Set the layouts allowed in this tab
enabled_layouts tall, stack
# Set the current layout
layout stack
launch zsh
# Create a new OS window
new_os_window
# set new window size to 80x25 cells
os_window_size 80c 25c
# set the --class for the new OS window
os_window_class mywindow
launch sh
# Make the current window the active (focused) window
focus
launch emacs
.. note::
The :doc:`launch <launch>` command when used in a session file
cannot create new OS windows, or tabs.


Mouse features
-------------------

* You can click on a URL to open it in a browser.
* You can double click to select a word and then drag to select more words.
* You can triple click to select a line and then drag to select more lines.
* You can triple click while holding :kbd:`ctrl+alt` to select from clicked
point to end of line.
* You can right click to extend a previous selection.
* You can hold down :kbd:`ctrl+alt` and drag with the mouse to select in
columns.
* Selecting text automatically copies it to the primary clipboard (on
platforms with a primary clipboard).
* You can middle click to paste from the primary clipboard (on platforms
with a primary clipboard).
* You can select text with kitty even when a terminal program has grabbed
the mouse by holding down the :kbd:`shift` key.

All these actions can be customized in :file:`kitty.conf` as described
:ref:`here <conf-kitty-mouse.mousemap>`.


Font control
-----------------

|kitty| has extremely flexible and powerful font selection features. You can
specify individual families for the regular, bold, italic and bold+italic
fonts. You can even specify specific font families for specific ranges of
unicode characters. This allows precise control over text rendering. It can
come in handy for applications like powerline, without the need to use patched
fonts. See the various font related configuration directives in
:ref:`conf-kitty-fonts`.


.. _scrollback:

The scrollback buffer
-----------------------

|kitty| supports scrolling back to view history, just like most terminals. You
can use either keyboard shortcuts or the mouse scroll wheel to do so. However,
|kitty| has an extra, neat feature. Sometimes you need to explore the
scrollback buffer in more detail, maybe search for some text or refer to it
side-by-side while typing in a follow-up command. |kitty| allows you to do this
by pressing the :sc:`show_scrollback` key-combination, which will open the
scrollback buffer in your favorite pager program (which is ``less`` by default).
Colors and text formatting are preserved. You can explore the scrollback buffer
comfortably within the pager.

Additionally, you can pipe the contents of the scrollback buffer to an
arbitrary, command running in a new :term:`window`, :term:`tab` or :term:`overlay`,
for example::

map f1 launch --stdin-source=@screen_scrollback --stdin-add-formatting less +G -R

Would open the scrollback buffer in a new :term:`window` when you press the :kbd:`F1`
key. See :sc:`show_scrollback` for details.

If you want to use it with an editor such as vim to get more powerful features,
you can see tips for doing so, in
`this thread <https://github.com/kovidgoyal/kitty/issues/719>`_.

If you wish to store very large amounts of scrollback to view using the piping or
:sc:`show_scrollback` features, you can use the :opt:`scrollback_pager_history_size`
option.

You can also view the output of the last command to run in the shell, by
pressing :sc:`show_last_command_output`. See :ref:`shell_integration` for
details.

.. _cpbuf:

Multiple copy/paste buffers
-----------------------------

In addition to being able to copy/paste from the system clipboard, in |kitty| you
can also setup an arbitrary number of copy paste buffers. To do so, simply add
something like the following to your :file:`kitty.conf`::

map f1 copy_to_buffer a
map f2 paste_from_buffer a

This will allow you to press :kbd:`F1` to copy the current selection to an
internal buffer named ``a`` and :kbd:`F2` to paste from that buffer. The buffer
names are arbitrary strings, so you can define as many such buffers as you
need.

Marks
-------------

kitty has the ability to mark text on the screen based on regular expressions.
This can be useful to highlight words or phrases when browsing output from long
running programs or similar. To learn how this feature works, see :doc:`marks`.


Frequently Asked Questions
---------------------------------

The list of Frequently Asked Questions (*FAQ*) is :doc:`available here <faq>`.


Cool integrations for kitty with other CLI tools
--------------------------------------------------

kitty provides extremely powerful interfaces such as :doc:`remote-control` and
:doc:`kittens/custom` and :doc:`kittens/icat`
that allow it to be integrated with other tools seamlessly. For a list of such
user created integrations, see: :doc:`integrations`.


>>>>>>> 1d167ada (Move shell integration docs into own file)


.. figure:: screenshots/screenshot.png
Expand Down
4 changes: 4 additions & 0 deletions kitty/fast_data_types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,10 @@ def click_mouse_url(os_window_id: int, tab_id: int, window_id: int) -> bool:
pass


def move_cursor_to_mouse_if_in_prompt(os_window_id: int, tab_id: int, window_id: int) -> bool:
pass


def mouse_selection(os_window_id: int, tab_id: int, window_id: int, code: int, button: int) -> None:
pass

Expand Down
9 changes: 9 additions & 0 deletions kitty/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ mouse_open_url(Window *w) {
return screen_open_url(screen);
}

bool
move_cursor_to_mouse_if_at_shell_prompt(Window *w) {
Screen *screen = w->render_data.screen;
int y = screen_cursor_at_a_shell_prompt(screen);
if (y < 0 || (unsigned)y > w->mouse_pos.cell_y) return false;
return screen_fake_move_cursor_to_position(screen, w->mouse_pos.cell_x, w->mouse_pos.cell_y);
}


typedef struct PendingClick {
id_type window_id;
int button, count, modifiers;
Expand Down
20 changes: 15 additions & 5 deletions kitty/options/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,26 @@
automatically end it and no release event will be dispatched.
''')

mma('Click the link under the mouse cursor when no selection is created',
'click_url_or_select left click ungrabbed mouse_click_url_or_select',
mma('Click the link under the mouse or move the cursor',
'click_url_or_select left click ungrabbed mouse_handle_click selection link prompt',
long_text='''
First check for a selection and if one exists do nothing. Then check for a link
under the mouse cursor and if one exists, click it. Finally check if the click happened at
the current shell prompt and if so, move the cursor to the click location. Note that this
requires :doc:`shell-integration` to work.
'''
)

mma('Click the link under the mouse cursor when no selection is created even if grabbed',
'click_url_or_select_grabbed shift+left click grabbed,ungrabbed mouse_click_url_or_select',
mma('Click the link under the mouse or move the cursor even when grabbed',
'click_url_or_select_grabbed shift+left click grabbed,ungrabbed mouse_handle_click selection link prompt',
long_text='''
Same as above, except that the action is performed even when the mouse is grabbed
by the program running in the terminal.
'''
)

mma('Click the link under the mouse cursor',
'click_url ctrl+shift+left release grabbed,ungrabbed mouse_click_url',
'click_url ctrl+shift+left release grabbed,ungrabbed mouse_handle_click link',
long_text='Variant with :kbd:`ctrl+shift` is present because the simple'
' click based version has an unavoidable delay of :opt:`click_interval`, to disambiguate clicks from double clicks.'
)
Expand Down
10 changes: 5 additions & 5 deletions kitty/options/types.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kitty/options/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class InvalidMods(ValueError):
@func_with_args(
'pass_selection_to_program', 'new_window', 'new_tab', 'new_os_window',
'new_window_with_cwd', 'new_tab_with_cwd', 'new_os_window_with_cwd',
'launch'
'launch', 'mouse_handle_click'
)
def shlex_parse(func: str, rest: str) -> FuncArgsType:
return func, to_cmdline(rest)
Expand Down
41 changes: 40 additions & 1 deletion kitty/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "wcwidth-std.h"
#include "control-codes.h"
#include "charsets.h"
#include "keys.h"

static const ScreenModes empty_modes = {0, .mDECAWM=true, .mDECTCEM=true, .mDECARM=true};

Expand Down Expand Up @@ -1344,6 +1345,45 @@ screen_cursor_to_line(Screen *self, unsigned int line) {
screen_cursor_position(self, line, self->cursor->x + 1);
}

int
screen_cursor_at_a_shell_prompt(const Screen *self) {
if (self->cursor->y >= self->lines || self->linebuf != self->main_linebuf) return false;
for (index_type y=self->cursor->y + 1; y-- > 0; ) {
linebuf_init_line(self->linebuf, y);
if (self->linebuf->line->is_output_start) return -1;
if (self->linebuf->line->is_prompt_start) return y;
}
return -1;
}

bool
screen_fake_move_cursor_to_position(Screen *self, index_type x, index_type y) {
SelectionBoundary a = {.x=x, .y=y}, b = {.x=self->cursor->x, .y=self->cursor->y};
SelectionBoundary *start, *end; int key;
if (a.y < b.y || (a.y == b.y && a.x < b.x)) { start = &a; end = &b; key = GLFW_FKEY_LEFT; }
else { start = &b; end = &a; key = GLFW_FKEY_RIGHT; }
unsigned count = 0;

for (unsigned y = start->y, x = start->x; y <= end->y; y++) {
unsigned x_limit = y == end->y ? end->x : self->columns;
while (x < x_limit) {
unsigned w = MAX(1u, linebuf_char_width_at(self->linebuf, x, y));
x += w;
count += w;
}
x = 0;
}
if (count) {
GLFWkeyevent ev = { .key = key, .action = GLFW_PRESS };
char output[KEY_BUFFER_SIZE+1] = {0};
int num = encode_glfw_key_event(&ev, false, 0, output);
if (num != SEND_TEXT_TO_CHILD) {
for (unsigned i = 0; i < count; i++) write_to_child(self, output, num);
}
}
return count > 0;
}

// }}}

// Editing {{{
Expand Down Expand Up @@ -1921,7 +1961,6 @@ screen_update_cell_data(Screen *self, void *address, FONTS_DATA_HANDLE fonts_dat
if (was_dirty) clear_selection(&self->url_ranges);
}


static bool
selection_boundary_less_than(const SelectionBoundary *a, const SelectionBoundary *b) {
// y -values must be absolutized (aka adjusted with scrolled_by)
Expand Down
2 changes: 2 additions & 0 deletions kitty/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ uint8_t screen_current_key_encoding_flags(Screen *self);
void screen_report_key_encoding_flags(Screen *self);
void screen_xtmodkeys(Screen *self, uint32_t p1, uint32_t p2);
bool screen_detect_url(Screen *screen, unsigned int x, unsigned int y);
int screen_cursor_at_a_shell_prompt(const Screen *);
bool screen_fake_move_cursor_to_position(Screen *, index_type x, index_type y);
#define DECLARE_CH_SCREEN_HANDLER(name) void screen_##name(Screen *screen);
DECLARE_CH_SCREEN_HANDLER(bell)
DECLARE_CH_SCREEN_HANDLER(backspace)
Expand Down
12 changes: 12 additions & 0 deletions kitty/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,16 @@ click_mouse_url(id_type os_window_id, id_type tab_id, id_type window_id) {
return clicked;
}

static bool
move_cursor_to_mouse_if_in_prompt(id_type os_window_id, id_type tab_id, id_type window_id) {
bool moved = false;
WITH_WINDOW(os_window_id, tab_id, window_id);
moved = move_cursor_to_mouse_if_at_shell_prompt(window);
END_WITH_WINDOW;
return moved;
}


static PyObject*
pymouse_selection(PyObject *self UNUSED, PyObject *args) {
id_type os_window_id, tab_id, window_id;
Expand All @@ -1050,6 +1060,7 @@ THREE_ID_OBJ(update_window_title)
THREE_ID(remove_window)
THREE_ID(detach_window)
THREE_ID(attach_window)
THREE_ID(move_cursor_to_mouse_if_in_prompt)
PYWRAP1(resolve_key_mods) { int mods, kitty_mod; PA("ii", &kitty_mod, &mods); return PyLong_FromLong(resolve_mods(kitty_mod, mods)); }
PYWRAP1(add_tab) { return PyLong_FromUnsignedLongLong(add_tab(PyLong_AsUnsignedLongLong(args))); }
PYWRAP1(add_window) { PyObject *title; id_type a, b; PA("KKO", &a, &b, &title); return PyLong_FromUnsignedLongLong(add_window(a, b, title)); }
Expand All @@ -1069,6 +1080,7 @@ static PyMethodDef module_methods[] = {
MW(set_options, METH_VARARGS),
MW(get_options, METH_NOARGS),
MW(click_mouse_url, METH_VARARGS),
MW(move_cursor_to_mouse_if_in_prompt, METH_VARARGS),
MW(mouse_selection, METH_VARARGS),
MW(set_in_sequence_mode, METH_O),
MW(resolve_key_mods, METH_VARARGS),
Expand Down
Loading

0 comments on commit 376db2a

Please sign in to comment.