чување f7fe0ff854280d9812760e1675e19eb5140ef49b
родитељ ad58df3da37705ec4406eac5b0b17b1364a303a2
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sat, 14 Oct 2023 20:15:54 +0200
Fix "backwards" selection
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | TODO | | | 15 | --------------- |
| M | TODO.done | | | 15 | +++++++++++++++ |
| M | draw.c | | | 38 | +++++++++++++------------------------- |
| M | draw.h | | | 6 | ++---- |
| M | sled.c | | | 105 | ++++++++++++++++++++++++++++++++----------------------------------------------- |
измењених датотека: 5, додавања: 73(+), брисања: 106(-)
diff --git a/TODO b/TODO
@@ -1,21 +1,6 @@
TODO
====
-[x] Migrate from using MANDIR to using MANPREFIX like suckless programs
- ex:
- MANDIR=$PREFIX/share/man/man1
- for OpenBSD:
- MANDIR=$PREFIX/man/man1
- and then:
- install -Dm 0644 myman.1 $MANDIR/myman.1
- vs: ----------------------------------------------------
- MANPREFIX=$PREFIX/share/man
- for OpenBSD:
- MANPREFIX=$PREFIX/man
- and then:
- install -Dm 0644 myman.1 $MANPREFIX/man1/myman.1
- This would need to be done for all my programs.
-
[~] Code optimization (struct coord...)
diff --git a/TODO.done b/TODO.done
@@ -1,6 +1,21 @@
Done or canceled todos
======================
+[x] Migrate from using MANDIR to using MANPREFIX like suckless programs
+ ex:
+ MANDIR=$PREFIX/share/man/man1
+ for OpenBSD:
+ MANDIR=$PREFIX/man/man1
+ and then:
+ install -Dm 0644 myman.1 $MANDIR/myman.1
+ vs: ----------------------------------------------------
+ MANPREFIX=$PREFIX/share/man
+ for OpenBSD:
+ MANPREFIX=$PREFIX/man
+ and then:
+ install -Dm 0644 myman.1 $MANPREFIX/man1/myman.1
+ This would need to be done for all my programs.
+
[x] Pressing h/Left on the start of line should go to the end of previous line,
similar for l/Right and beginning of next line
diff --git a/draw.c b/draw.c
@@ -67,6 +67,9 @@ const char* prompt_overwrite = "Not empty, overwrite? [y/n/ESC]?";
const char* prompt_reread_dirty = "File changed, lose changes [y/n/ESC]?";
/* clang-format on */
+void get_selection(int* ssr, int* ssc, int* ser, int* sec,
+ const struct DrawState* state);
+
void
init_bufferline(struct BufferLine* bl)
{
@@ -329,8 +332,7 @@ draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
const char* s, const int mc, const int fc, const int p_l, const int p_r,
const Alignment align, const int ds, const int fill,
const uint32_t* srch, const uint16_t sfg, const uint16_t sbg,
- const size_t ssi, const size_t sei, const uint16_t slfg,
- const uint16_t slbg)
+ const int ssi, const int sei, const uint16_t slfg, const uint16_t slbg)
{
uint32_t* us = calloc(strlen(s) + 1, sizeof(uint32_t));
if (!us)
@@ -363,8 +365,7 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
const uint32_t* s, const int mc, const int fc, const int p_l,
const int p_r, const Alignment align, const int ds, const int fill,
const uint32_t* srch, const uint16_t sfg, const uint16_t sbg,
- const size_t ssi, const size_t sei, const uint16_t slfg,
- const uint16_t slbg)
+ const int ssi, const int sei, const uint16_t slfg, const uint16_t slbg)
{
/*
* <-------- visible part ---------->
@@ -644,16 +645,16 @@ draw_screen(const struct DrawState* state)
const struct BufferLine* current = NULL;
int max_screen_lines = state->maxy - 1;
int cy = 0;
- size_t i = state->first_shown.row;
+ int i = state->first_shown.row;
/* Selection start/end index - passed to u32_draw_string */
- size_t ssi = 0;
- size_t sei = 0;
+ int ssi = 0;
+ int sei = 0;
/* Selection start row/col */
- size_t ssr = 0;
- size_t ssc = 0;
+ int ssr = 0;
+ int ssc = 0;
/* Selection end row/col */
- size_t ser = 0;
- size_t sec = 0;
+ int ser = 0;
+ int sec = 0;
if (!state)
return NULL;
@@ -669,20 +670,7 @@ draw_screen(const struct DrawState* state)
ssi = 0;
sei = 0;
- if (state->sel_start.row <= state->sel_end.row)
- {
- ssr = state->sel_start.row;
- ssc = state->sel_start.col;
- ser = state->sel_end.row;
- sec = state->sel_end.col;
- }
- else /* Swap */
- {
- ssr = state->sel_end.row;
- ssc = state->sel_end.col;
- ser = state->sel_start.row;
- sec = state->sel_start.col;
- }
+ get_selection(&ssr, &ssc, &ser, &sec, state);
if ((i > ssr) && (i < ser))
{
ssi = 0;
diff --git a/draw.h b/draw.h
@@ -128,15 +128,13 @@ const char* draw_string(const int x, const int y, const uint16_t fg,
const int fill_cols, const int padding_left, const int padding_right,
const Alignment align, const int ds, const int fill,
const uint32_t* srch, const uint16_t sfg, const uint16_t sbg,
- const size_t ssi, const size_t sei, const uint16_t slfg,
- const uint16_t slbg);
+ const int ssi, const int sei, const uint16_t slfg, const uint16_t slbg);
void u32_draw_string(const int x, const int y, const uint16_t fg,
const uint16_t bg, const uint32_t* s, const int max_cols,
const int fill_cols, const int padding_left, const int padding_right,
const Alignment align, const int ds, const int fill,
const uint32_t* srch, const uint16_t sfg, const uint16_t sbg,
- const size_t ssi, const size_t sei, const uint16_t slfg,
- const uint16_t slbg);
+ const int ssi, const int sei, const uint16_t slfg, const uint16_t slbg);
const struct DrawState* draw_help(const struct DrawState* state);
const struct DrawState* draw_input_dialog(const struct DrawState* state);
const struct DrawState* draw_screen(const struct DrawState* state);
diff --git a/sled.c b/sled.c
@@ -62,6 +62,8 @@ int clear_selection(struct DrawState* state);
int create_file(struct DrawState* dstate);
int do_search_replace(struct DrawState* state);
int erase_selection(struct DrawState* state);
+void get_selection(int* ssr, int* ssc, int* ser, int* sec,
+ const struct DrawState* state);
void go_to_line_callback(struct DrawState* state);
void handle_key_event(struct tb_event* ev, struct DrawState* state);
int input_insert_char(struct DrawState* state, uint32_t ch);
@@ -132,18 +134,7 @@ do_search_replace(struct DrawState* state)
int end_row = state->rows_count;
if (SEL_NOT_EMPTY(state))
- {
- if (state->sel_start.row <= state->sel_end.row)
- {
- start_row = state->sel_start.row;
- end_row = state->sel_end.row;
- }
- else /* Swap */
- {
- start_row = state->sel_end.row;
- end_row = state->sel_start.row;
- }
- }
+ get_selection(&start_row, NULL, &end_row, NULL, state);
for (int row = start_row; row < end_row; row++)
{
@@ -192,20 +183,7 @@ erase_selection(struct DrawState* state)
if (!buf)
exit(print_error(ENOMEM, "Memory allocation failed"));
- if (state->sel_start.row <= state->sel_end.row)
- {
- ssr = state->sel_start.row;
- ssc = state->sel_start.col;
- ser = state->sel_end.row;
- sec = state->sel_end.col;
- }
- else /* Swap */
- {
- ssr = state->sel_end.row;
- ssc = state->sel_end.col;
- ser = state->sel_start.row;
- sec = state->sel_start.col;
- }
+ get_selection(&ssr, &ssc, &ser, &sec, state);
u32_strcpy(buf, state->buffer[ser].text + sec);
@@ -238,6 +216,41 @@ erase_selection(struct DrawState* state)
}
void
+get_selection(int* ssr, int* ssc, int* ser, int* sec,
+ const struct DrawState* state)
+{
+ if (state->sel_start.col <= state->sel_end.col)
+ {
+ if (ssc)
+ *ssc = state->sel_start.col;
+ if (sec)
+ *sec = state->sel_end.col;
+ }
+ else /* Swap */
+ {
+ if (ssc)
+ *ssc = state->sel_end.col;
+ if (sec)
+ *sec = state->sel_start.col;
+ }
+
+ if (state->sel_start.row <= state->sel_end.row)
+ {
+ if (ssr)
+ *ssr = state->sel_start.row;
+ if (ser)
+ *ser = state->sel_end.row;
+ }
+ else /* Swap */
+ {
+ if (ssr)
+ *ssr = state->sel_end.row;
+ if (ser)
+ *ser = state->sel_start.row;
+ }
+}
+
+void
go_to_line_callback(struct DrawState* state)
{
size_t lineno = 0;
@@ -517,18 +530,8 @@ join_lines(struct DrawState* state)
if (SEL_NOT_EMPTY(state))
{
- if (state->sel_start.row <= state->sel_end.row)
- {
- ssr = state->sel_start.row;
- ssc = state->sel_start.col;
- ser = state->sel_end.row - 1;
- }
- else /* Swap */
- {
- ssr = state->sel_end.row;
- ssc = state->sel_end.col;
- ser = state->sel_start.row - 1;
- }
+ get_selection(&ssr, &ssc, &ser, NULL, state);
+ ser--;
selection = 1;
clear_selection(state);
}
@@ -750,16 +753,7 @@ position_to_selection_start(struct DrawState* state)
int ssr = 0;
int ssc = 0;
- if (state->sel_start.row <= state->sel_end.row)
- {
- ssr = state->sel_start.row;
- ssc = state->sel_start.col;
- }
- else /* Swap */
- {
- ssr = state->sel_end.row;
- ssc = state->sel_end.col;
- }
+ get_selection(&ssr, &ssc, NULL, NULL, state);
state->saved_column
= display_length(state, state->buffer[ssr].text, ssc);
@@ -2011,20 +2005,7 @@ simple_yank(struct DrawState* state)
return;
}
- if (state->sel_start.row <= state->sel_end.row)
- {
- ssr = state->sel_start.row;
- ssc = state->sel_start.col;
- ser = state->sel_end.row;
- sec = state->sel_end.col;
- }
- else /* Swap */
- {
- ssr = state->sel_end.row;
- ssc = state->sel_end.col;
- ser = state->sel_start.row;
- sec = state->sel_start.col;
- }
+ get_selection(&ssr, &ssc, &ser, &sec, state);
state->paste_count = ser - ssr + 1;
initialize_start = state->paste_size;