commit 5c1f64919a8a2974c107614f144fd932aa6db1bf
parent 271d268ba17415be9808bb1da36389036b4eb95d
Author: lumidify <nobody@lumidify.org>
Date: Mon, 1 Nov 2021 16:47:45 +0100
Improve(?) pasting and add backwards pasting (P)
Diffstat:
2 files changed, 67 insertions(+), 3 deletions(-)
diff --git a/keys_basic.c b/keys_basic.c
@@ -1,3 +1,4 @@
+/* FIXME: cursor isn't shown on spaces at end of softlines */
#include <stdio.h>
#include <stdlib.h>
@@ -647,6 +648,10 @@ key_d_cb(ledit_buffer *buffer, int line, int char_pos, enum key_type type) {
/* FIXME: don't use the pango functions directly so normalize_and_set_pango_text is
always called properly */
+/* Note that these paste functions are a bit weird when working with softlines -
+ they always make sure the pasted text is separated from the surrounding text by
+ hard lines, which may be unexpected, but the alternatives I could think of are
+ even weirder */
static struct action
paste_normal(ledit_buffer *buffer, char *text, int len) {
(void)text;
@@ -666,11 +671,18 @@ paste_normal(ledit_buffer *buffer, char *text, int len) {
buffer, buffer->cur_line, sl->start_index + sl->length,
"\n", -1, buffer->cur_line, buffer->cur_index, 1
);
- /* remove trailing newline if it exists - this may be hacky */
int text_len = paste_buffer->len;
- if (paste_buffer->text[paste_buffer->len-1] == '\n') {
+ ll = ledit_buffer_get_line(buffer, buffer->cur_line + 1);
+ if (ll->len == 0 && paste_buffer->text[text_len-1] == '\n') {
+ /* remove trailing newline if it exists and text is already on own line */
text_len--;
- paste_buffer->text[paste_buffer->len-1] = '\0';
+ paste_buffer->text[text_len] = '\0';
+ } else if (ll->len > 0 && paste_buffer->text[text_len-1] != '\n') {
+ /* ensure pasted text is on its own hard line */
+ insert_text(
+ buffer, buffer->cur_line + 1, 0,
+ "\n", -1, buffer->cur_line, buffer->cur_index, 0
+ );
}
insert_text(
buffer, buffer->cur_line + 1, 0,
@@ -690,6 +702,56 @@ paste_normal(ledit_buffer *buffer, char *text, int len) {
}
static struct action
+paste_normal_backwards(ledit_buffer *buffer, char *text, int len) {
+ (void)text;
+ (void)len;
+ if (!paste_buffer) {
+ ledit_window_show_message(buffer->window, "Nothing to paste", -1);
+ discard_repetition_stack();
+ return (struct action){ACTION_NONE, NULL};
+ }
+ if (paste_buffer_line_based) {
+ int x, softline;
+ ledit_buffer_wipe_line_cursor_attrs(buffer, buffer->cur_line);
+ ledit_line *ll = ledit_buffer_get_line(buffer, buffer->cur_line);
+ pango_layout_index_to_line_x(ll->layout, buffer->cur_index, 0, &softline, &x);
+ PangoLayoutLine *sl = pango_layout_get_line_readonly(ll->layout, softline);
+ insert_text(
+ buffer, buffer->cur_line, sl->start_index,
+ "\n", -1, buffer->cur_line, buffer->cur_index, 1
+ );
+ int text_len = paste_buffer->len;
+ ll = ledit_buffer_get_line(buffer, buffer->cur_line);
+ if (paste_buffer->text[text_len-1] == '\n') {
+ /* remove trailing newline if it exists */
+ text_len--;
+ paste_buffer->text[text_len] = '\0';
+ }
+ int new_line = buffer->cur_line;
+ if (ll->len > 0) {
+ /* ensure pasted text is on its own hard line */
+ insert_text(
+ buffer, buffer->cur_line, ll->len,
+ "\n", -1, buffer->cur_line, buffer->cur_index, 0
+ );
+ new_line = buffer->cur_line + 1;
+ }
+ insert_text(
+ buffer, new_line, 0,
+ paste_buffer->text, text_len, new_line, 0, 0
+ );
+ } else {
+ insert_text(
+ buffer, buffer->cur_line, buffer->cur_index,
+ paste_buffer->text, paste_buffer->len, buffer->cur_line, buffer->cur_index, 1
+ );
+ }
+ ledit_buffer_set_line_cursor_attrs(buffer, buffer->cur_line, buffer->cur_index);
+ finalize_repetition_stack();
+ return (struct action){ACTION_NONE, NULL};
+}
+
+static struct action
key_x(ledit_buffer *buffer, char *text, int len) {
(void)buffer;
(void)text;
diff --git a/keys_basic_config.h b/keys_basic_config.h
@@ -65,6 +65,7 @@ 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);
static struct action paste_normal(ledit_buffer *buffer, char *text, int len);
+static struct action paste_normal_backwards(ledit_buffer *buffer, char *text, int len);
/* FIXME: maybe sort these and use binary search
-> but that would mess with the catch-all keys */
@@ -123,6 +124,7 @@ static struct key keys_en[] = {
{"u", ControlMask, 0, NORMAL, KEY_ANY, KEY_NUMBERALLOWED, &scroll_lines_up},
{"G", 0, 0, NORMAL, KEY_ANY, KEY_NUMBERALLOWED, &move_to_line},
{"p", 0, 0, NORMAL, KEY_ANY, KEY_ANY, &paste_normal},
+ {"P", 0, 0, NORMAL, KEY_ANY, KEY_ANY, &paste_normal_backwards},
{"", 0, 0, INSERT, KEY_ANY, KEY_ANY, &insert_mode_insert_text}
};