commit e5841801447358ed8f2f57e3dc0adc2873fb500e
parent 7d04a4c57facdf6080ce5492979b2652cd675a25
Author: lumidify <nobody@lumidify.org>
Date: Mon, 1 Nov 2021 10:46:58 +0100
Add G for moving to specific line
Diffstat:
2 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/keys_basic.c b/keys_basic.c
@@ -374,6 +374,38 @@ get_key_repeat(void) {
return num;
}
+static struct action
+move_to_line(ledit_buffer *buffer, char *text, int len) {
+ (void)text;
+ (void)len;
+ int repeat = get_key_repeat();
+ int line;
+ if (repeat > 0)
+ line = repeat > buffer->lines_num ? buffer->lines_num : repeat;
+ else if (repeat == 0)
+ line = buffer->lines_num;
+ else
+ ledit_window_show_message(buffer->window, "Invalid key", -1);
+ if (repeat >= 0) {
+ ledit_buffer_wipe_line_cursor_attrs(buffer, buffer->cur_line);
+ buffer->cur_line = line - 1;
+ buffer->cur_index = 0;
+ int text_w, text_h;
+ PangoRectangle strong, weak;
+ ledit_window_get_textview_size(buffer->window, &text_w, &text_h);
+ ledit_line *ll = ledit_buffer_get_line(buffer, buffer->cur_line);
+ pango_layout_get_cursor_pos(ll->layout, 0, &strong, &weak);
+ /* if cursor is not on screen anymore, move to middle of screen */
+ if (ll->y_offset < buffer->display_offset ||
+ ll->y_offset + strong.height / PANGO_SCALE > buffer->display_offset + text_h) {
+ ledit_buffer_scroll(buffer, ll->y_offset - text_h / 2);
+ }
+ ledit_buffer_set_line_cursor_attrs(buffer, buffer->cur_line, buffer->cur_index);
+ }
+ discard_repetition_stack();
+ return (struct action){ACTION_NONE, NULL};
+}
+
static void
scroll_lines(ledit_buffer *buffer, int lines, int dir) {
int final_lines;
diff --git a/keys_basic_config.h b/keys_basic_config.h
@@ -63,6 +63,7 @@ static struct action scroll_with_cursor_up(ledit_buffer *buffer, char *text, int
static struct action scroll_with_cursor_down(ledit_buffer *buffer, char *text, int len);
static struct action scroll_lines_up(ledit_buffer *buffer, char *text, int len);
static struct action scroll_lines_down(ledit_buffer *buffer, char *text, int len);
+static struct action move_to_line(ledit_buffer *buffer, char *text, int len);
/* FIXME: maybe sort these and use binary search
-> but that would mess with the catch-all keys */
@@ -119,6 +120,7 @@ static struct key keys_en[] = {
{"y", ControlMask, 0, NORMAL, KEY_ANY, KEY_NUMBERALLOWED, &scroll_with_cursor_up},
{"d", ControlMask, 0, NORMAL, KEY_ANY, KEY_NUMBERALLOWED, &scroll_lines_down},
{"u", ControlMask, 0, NORMAL, KEY_ANY, KEY_NUMBERALLOWED, &scroll_lines_up},
+ {"G", 0, 0, NORMAL, KEY_ANY, KEY_NUMBERALLOWED, &move_to_line},
{"", 0, 0, INSERT, KEY_ANY, KEY_ANY, &insert_mode_insert_text}
};