чување 39043fce0da5bbba1116512aa979238a6f959335
родитељ e42175108d47152866cfc7183c148d9bf49248d6
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Thu, 8 Aug 2024 11:51:54 +0200
patch/syntax.patch: Improve accuracy at expense of speed; multiline
comments corner case fix
Diffstat:
| M | patch/syntax.patch | | | 224 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
измењених датотека: 1, додавања: 118(+), брисања: 106(-)
diff --git a/patch/syntax.patch b/patch/syntax.patch
@@ -1,22 +1,20 @@
-From 04d6bfdfa410e4089dafde7626020ddff7722c64 Mon Sep 17 00:00:00 2001
+From 7986fc8772e59f32706c3ba8289550657b8f9df6 Mon Sep 17 00:00:00 2001
From: Страхиња Радић <sr@strahinja.org>
-Date: Thu Aug 8 07:38:16 2024 UTC
+Date: Thu Aug 8 09:49:22 2024 UTC
Subject: [PATCH] Add syntax highlighting
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
-Add syntax highlighting. This is a rudimentary, flawed version,
-but gets the job done. TODO: Optimization.
---
config.def.h | 7+ 0-
- draw.c | 485+ 29-
+ draw.c | 481+ 29-
draw.h | 62+ 6-
- sled.c | 83+ 3-
- 4 files changed, 637 insertions(+), 38 deletions(-)
+ sled.c | 82+ 16-
+ 4 files changed, 632 insertions(+), 51 deletions(-)
diff --git a/config.def.h b/config.def.h
-index 7c142b66a..04d6bfdfa 100644
+index e42175108..7986fc877 100644
--- a/config.def.h
+++ b/config.def.h
@@ -239,6 +239,13 @@ static const struct Key command_keys[] = {
@@ -34,18 +32,18 @@ index 7c142b66a..04d6bfdfa 100644
{.format = "%s%s",
.callback = format_filename,
diff --git a/draw.c b/draw.c
-index 7c142b66a..04d6bfdfa 100644
+index e42175108..7986fc877 100644
--- a/draw.c
+++ b/draw.c
-@@ -2,6 +2,7 @@
- * any later version. Copyright (C) 2022-2024 Страхиња Радић.
+@@ -3,6 +3,7 @@
* See the file LICENSE for exact copyright and license details. */
+
#include <assert.h>
+#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
-@@ -13,6 +14,28 @@
+@@ -14,6 +15,28 @@
#include "util.h"
#include "version.h"
@@ -74,7 +72,7 @@ index 7c142b66a..04d6bfdfa 100644
enum {
UNKNOWN_MODE,
COMMAND_MODE,
-@@ -87,6 +110,95 @@ static const struct {
+@@ -88,6 +111,95 @@ static const struct {
{ .boundary = 99, .width = 3 },
{ .boundary = 9, .width = 2 }
};
@@ -170,7 +168,7 @@ index 7c142b66a..04d6bfdfa 100644
const char* go_to_dialog_title = "Go to line";
const char* prompt_dirty = "File changed, save [y/n/ESC]?";
const char* prompt_overwrite = "Not empty, overwrite? [y/n/ESC]?";
-@@ -95,6 +207,46 @@ const char* replace_dialog_title = "Replace with";
+@@ -96,6 +208,46 @@ const char* replace_dialog_title = "Replace with";
const char* search_dialog_title = "Search text";
/* clang-format on */
@@ -217,7 +215,7 @@ index 7c142b66a..04d6bfdfa 100644
void get_selection(int* ssr, int* ssc, int* ser, int* sec,
const struct DrawState* state);
-@@ -264,15 +416,15 @@ draw_help(const struct DrawState* state)
+@@ -265,15 +417,15 @@ draw_help(const struct DrawState* state)
endy = h > maxy - 1 ? maxy - 2 : starty + h;
draw_box(startx, endx, starty, endy, DLG_FG, DLG_BG);
@@ -236,7 +234,7 @@ index 7c142b66a..04d6bfdfa 100644
return 1;
row++;
}
-@@ -307,15 +459,16 @@ draw_input_dialog(const struct DrawState* state)
+@@ -308,15 +460,16 @@ draw_input_dialog(const struct DrawState* state)
INPUT_EDIT_BG);
if (state->input_title)
if (draw_string(startx, starty, TITLE_FG, TITLE_BG,
@@ -258,7 +256,7 @@ index 7c142b66a..04d6bfdfa 100644
tb_set_cursor(startx + 2 + state->input_display_column
- state->input_first_shown_column,
starty + 1);
-@@ -336,7 +489,7 @@ draw_line_number(const struct DrawState* state, const
+@@ -337,7 +490,7 @@ draw_line_number(const struct DrawState* state, const
pnumbuf = numbuf_end - numbuf > state->line_numbers_width
? numbuf_end - state->line_numbers_width
: numbuf;
@@ -267,7 +265,7 @@ index 7c142b66a..04d6bfdfa 100644
numbuf_end - numbuf + 1, state->line_numbers_width, 0,
0, RIGHT, 0, 1, NULL, 0, 0, 0, 0, 0, 0);
}
-@@ -416,9 +569,10 @@ draw_screen(const struct DrawState* state)
+@@ -417,9 +570,10 @@ draw_screen(const struct DrawState* state)
u32_draw_string(state->line_numbers
? state->line_numbers_width + 2
: 0,
@@ -281,7 +279,7 @@ index 7c142b66a..04d6bfdfa 100644
i++;
cy++;
}
-@@ -448,22 +602,22 @@ draw_status(const struct DrawState* state)
+@@ -449,22 +603,22 @@ draw_status(const struct DrawState* state)
if (IN(state->mode, M_PROMPT) && *state->prompt)
{
if (draw_string(0, state->maxy - 1, PROMPT_FG, PROMPT_BG,
@@ -310,7 +308,7 @@ index 7c142b66a..04d6bfdfa 100644
return 1;
}
else
-@@ -486,7 +640,7 @@ draw_status(const struct DrawState* state)
+@@ -487,7 +641,7 @@ draw_status(const struct DrawState* state)
if (pseg == status_segments + LEN(status_segments) - 1)
seg_size = maxx - 1 - current_start;
if (draw_string(current_start, state->maxy - 1,
@@ -319,7 +317,7 @@ index 7c142b66a..04d6bfdfa 100644
MIN(seg_size, (ssize_t)(strlen(buf) + 2)),
last ? seg_size + 1 : seg_size, 1, 1,
pseg->alignment, 0, 1, NULL, 0, 0, 0, 0, 0,
-@@ -521,8 +675,9 @@ draw_status(const struct DrawState* state)
+@@ -522,8 +676,9 @@ draw_status(const struct DrawState* state)
* */
int
draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
@@ -331,7 +329,7 @@ index 7c142b66a..04d6bfdfa 100644
const uint32_t* srch, const uint16_t sfg, const uint16_t sbg,
const int ssi, const int sei, const uint16_t slfg, const uint16_t slbg)
{
-@@ -532,8 +687,8 @@ draw_string(const int x, const int y, const uint16_t f
+@@ -533,8 +688,8 @@ draw_string(const int x, const int y, const uint16_t f
if (!us)
return 1;
u8_string_to_unicode(us, s, mc);
@@ -342,7 +340,7 @@ index 7c142b66a..04d6bfdfa 100644
free(us);
return 0;
}
-@@ -638,6 +793,8 @@ free_bufferline(struct BufferLine* bl)
+@@ -639,6 +794,8 @@ free_bufferline(struct BufferLine* bl)
bl->length = 0;
bl->fg = 0;
bl->bg = 0;
@@ -351,7 +349,7 @@ index 7c142b66a..04d6bfdfa 100644
}
void
-@@ -676,6 +833,7 @@ init_bufferline(struct BufferLine* bl)
+@@ -677,6 +834,7 @@ init_bufferline(struct BufferLine* bl)
bl->size = 0;
bl->fg = 0;
bl->bg = 0;
@@ -359,7 +357,7 @@ index 7c142b66a..04d6bfdfa 100644
}
void
-@@ -701,6 +859,11 @@ init_drawstate(struct DrawState* state, char* error, c
+@@ -702,6 +860,11 @@ init_drawstate(struct DrawState* state, char* error, c
state->orig_real_filename = NULL;
state->backup_filename = NULL;
state->rfn_len = 0;
@@ -371,7 +369,7 @@ index 7c142b66a..04d6bfdfa 100644
}
state->input = NULL;
state->search = NULL;
-@@ -759,7 +922,272 @@ recalculate_size(struct DrawState* state)
+@@ -760,7 +923,273 @@ recalculate_size(struct DrawState* state)
recalculate_line_numbers(state);
}
@@ -424,7 +422,7 @@ index 7c142b66a..04d6bfdfa 100644
+ klist = state->keywords;
+ clist = state->comments;
+ olist = state->operators;
-+ li = state->first_shown.row;
++ li = 0;
+ quote = 0;
+ comment_index = -1;
+recalc_syn_next_line:
@@ -433,7 +431,7 @@ index 7c142b66a..04d6bfdfa 100644
+ curline->syntax = NULL;
+ curline->syntax_length = 0;
+ curline->syntax_size = 0;
-+ if (li == state->first_shown.row + state->maxy - 1)
++ if (li == state->rows_count)
+ return;
+
+ ti = 0;
@@ -452,6 +450,7 @@ index 7c142b66a..04d6bfdfa 100644
+ /* Check for comments */
+ if (comment_index >= 0)
+ {
++ recalc_syn_comment_rest_of_line:
+ seen_end_comment = 0;
+ while (ti < curline->length)
+ {
@@ -512,7 +511,7 @@ index 7c142b66a..04d6bfdfa 100644
+ comment_start = ti;
+ comment_index = i;
+ ti += comment->start_size;
-+ goto recalc_syn_next_token;
++ goto recalc_syn_comment_rest_of_line;
+ }
+ i++;
+ comment = &clist[i];
@@ -644,19 +643,7 @@ index 7c142b66a..04d6bfdfa 100644
redraw_status(struct DrawState* state)
{
assert(state != NULL);
-@@ -768,6 +1196,11 @@ redraw_status(struct DrawState* state)
- recalculate_size(state);
- state->recalculate_size = 0;
- }
-+ if (state->syntax_enabled && state->recalculate_syntax)
-+ {
-+ recalculate_syntax(state);
-+ state->recalculate_syntax = 0;
-+ }
- (void)draw_status(state);
- tb_present();
- }
-@@ -790,7 +1223,8 @@ redraw_status(struct DrawState* state)
+@@ -791,7 +1220,8 @@ redraw_status(struct DrawState* state)
* */
void
u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
@@ -666,7 +653,7 @@ index 7c142b66a..04d6bfdfa 100644
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 int ssi, const int sei, const uint16_t slfg, const uint16_t slbg)
-@@ -828,6 +1262,8 @@ u32_draw_string(const int x, const int y, const uint16
+@@ -829,6 +1259,8 @@ u32_draw_string(const int x, const int y, const uint16
int w = 0; /* actual on-screen rune width */
@@ -675,7 +662,7 @@ index 7c142b66a..04d6bfdfa 100644
const int ts = TAB_SIZE;
const uint32_t* ps = s; /* pointer to current character in
-@@ -836,11 +1272,23 @@ u32_draw_string(const int x, const int y, const uint16
+@@ -837,11 +1269,23 @@ u32_draw_string(const int x, const int y, const uint16
uint16_t cfg = fg; /* current fg */
uint16_t cbg = bg; /* current bg */
@@ -699,7 +686,7 @@ index 7c142b66a..04d6bfdfa 100644
c = mc - p_l - p_r;
if (align == LEFT)
-@@ -872,7 +1320,12 @@ u32_draw_string(const int x, const int y, const uint16
+@@ -873,7 +1317,12 @@ u32_draw_string(const int x, const int y, const uint16
*/
while (*ps && sc < c)
{
@@ -713,7 +700,7 @@ index 7c142b66a..04d6bfdfa 100644
{
cfg = slfg;
cbg = slbg;
-@@ -889,6 +1342,9 @@ u32_draw_string(const int x, const int y, const uint16
+@@ -890,6 +1339,9 @@ u32_draw_string(const int x, const int y, const uint16
}
}
@@ -723,7 +710,7 @@ index 7c142b66a..04d6bfdfa 100644
if (*ps == '\t')
{
if (ssc < ds)
-@@ -1004,11 +1460,11 @@ u32_draw_string(const int x, const int y, const uint16
+@@ -1005,11 +1457,11 @@ u32_draw_string(const int x, const int y, const uint16
}
ssc += w;
}
@@ -740,7 +727,7 @@ index 7c142b66a..04d6bfdfa 100644
}
diff --git a/draw.h b/draw.h
-index 7c142b66a..04d6bfdfa 100644
+index e42175108..7986fc877 100644
--- a/draw.h
+++ b/draw.h
@@ -37,17 +37,58 @@ enum {
@@ -863,7 +850,7 @@ index 7c142b66a..04d6bfdfa 100644
+ const uint16_t sbg, const int ssi, const int sei, const uint16_t slfg,
+ const uint16_t slbg);
diff --git a/sled.c b/sled.c
-index 7c142b66a..04d6bfdfa 100644
+index e42175108..7986fc877 100644
--- a/sled.c
+++ b/sled.c
@@ -67,6 +67,18 @@ extern const char* prompt_reread_dirty;
@@ -885,118 +872,143 @@ index 7c142b66a..04d6bfdfa 100644
static int
cancel_close(struct DrawState* state)
{
-@@ -195,6 +207,8 @@ create_file(struct DrawState* state)
+@@ -193,9 +205,9 @@ create_file(struct DrawState* state)
+ state->buffer_size = ALLOC_DELTA;
+ state->rows_count = 1;
recalculate_line_numbers(state);
- state->coord.row = 0;
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
-
+- state->coord.row = 0;
+- state->dirty = 1;
+-
++ state->coord.row = 0;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
return LOAD_ERR_NONE;
}
-@@ -325,6 +339,8 @@ do_search_replace(struct DrawState* state)
+
+@@ -325,6 +337,7 @@ do_search_replace(struct DrawState* state)
state->buffer[row].length = buflen;
}
free(buf);
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
++ state->recalculate_syntax = 1;
return 0;
do_search_replace_alloc_error:
-@@ -378,6 +394,8 @@ erase_backwards(struct DrawState* state)
+@@ -377,7 +390,8 @@ erase_backwards(struct DrawState* state)
+ buffer_line->text[i - 1] = 0;
buffer_line->length--;
position_to(state, state->coord.row, state->coord.col - 1);
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
}
else if (state->coord.row > 0)
{
-@@ -415,6 +433,8 @@ erase_forward(struct DrawState* state)
+@@ -414,7 +428,8 @@ erase_forward(struct DrawState* state)
+ }
buffer_line->text[i] = 0;
buffer_line->length--;
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
}
else if ((state->coord.row + 1) < state->rows_count)
simple_join_lines(state);
-@@ -450,6 +470,8 @@ erase_line(struct DrawState* state)
+@@ -449,7 +464,8 @@ erase_line(struct DrawState* state)
+ state->coord.row--;
position_to(state, state->coord.row, 0);
state->rows_count--;
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
recalculate_line_numbers(state);
return 0;
-@@ -489,6 +511,8 @@ erase_prev_word(struct DrawState* state)
+@@ -489,6 +505,7 @@ erase_prev_word(struct DrawState* state)
state->coord.col = i;
state->display_column = display_length(current->text, state->coord.col);
state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
++ state->recalculate_syntax = 1;
return 0;
}
-@@ -545,6 +569,8 @@ erase_selection(struct DrawState* state)
+@@ -544,7 +561,8 @@ erase_selection(struct DrawState* state)
+ recalculate_line_numbers(state);
}
free(buf);
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
}
return 0;
}
-@@ -570,6 +596,8 @@ erase_to_end(struct DrawState* state)
+@@ -569,7 +587,8 @@ erase_to_end(struct DrawState* state)
+ buffer_line->text[state->coord.col] = 0;
buffer_line->length = state->coord.col;
move_end(state);
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
return 0;
}
-@@ -604,6 +632,8 @@ erase_to_start(struct DrawState* state)
+@@ -603,7 +622,8 @@ erase_to_start(struct DrawState* state)
+ buffer_line->text[len - state->coord.col] = 0;
buffer_line->length = len - state->coord.col;
move_start(state);
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
return 0;
}
-@@ -1160,6 +1190,8 @@ insert_char(struct DrawState* state, uint32_t ch)
+@@ -1159,8 +1179,8 @@ insert_char(struct DrawState* state, uint32_t ch)
+ }
insert_skip_wrap:
- state->dirty = 1;
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
+- state->dirty = 1;
+-
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
+ return 0;
+ insert_char_error:
+@@ -1212,7 +1232,8 @@ insert_skip_wrap:
+ simple_insert_line(state);
+ state->saved_column = -1;
+ position_to(state, state->coord.row + 1, 0);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
return 0;
+ }
-@@ -1865,7 +1897,11 @@ position_to(struct DrawState* state, const int row, co
- state->first_shown.col = fsc;
+@@ -1747,7 +1768,8 @@ paste_from_paste(struct DrawState* state)
}
- if (old_first_shown_row != state->first_shown.row)
-+ {
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
- recalculate_line_numbers(state);
-+ }
- if (IN(state->mode, M_SELECTION))
- assign_coord(&state->sel_end, state->coord);
-@@ -2025,6 +2061,8 @@ reread_file(struct DrawState* state)
+ position_to(state, newpos.row, newpos.col);
+- state->dirty = 1;
++ state->dirty = 1;
++ state->recalculate_syntax = 1;
+
+ paste_from_paste_cleanup:
+ free(rest);
+@@ -2025,6 +2047,7 @@ reread_file(struct DrawState* state)
break;
default:
recalculate_line_numbers(state);
-+ if (state->syntax_enabled)
-+ recalculate_syntax(state);
++ state->recalculate_syntax = 1;
}
return result;
}
-@@ -2905,6 +2943,8 @@ main(int argc, char** argv)
+@@ -2438,6 +2461,7 @@ simple_join_lines(struct DrawState* state)
+ state->rows_count--;
+ state->dirty = 1;
+ recalculate_line_numbers(state);
++ state->recalculate_syntax = 1;
+ return;
+
+ simple_join_lines_error:
+@@ -2905,6 +2929,8 @@ main(int argc, char** argv)
int result = 0;
int dir_access;
int dir_access_errno;
@@ -1005,7 +1017,7 @@ index 7c142b66a..04d6bfdfa 100644
*error = 0;
*action = 0;
-@@ -2988,6 +3028,40 @@ main(int argc, char** argv)
+@@ -2988,6 +3014,40 @@ main(int argc, char** argv)
init_drawstate(&state, error, action, filename, prompt, 0, 0);
@@ -1046,7 +1058,7 @@ index 7c142b66a..04d6bfdfa 100644
#ifdef __OpenBSD__
if (unveil(state.filename, "rwc") < 0)
{
-@@ -3210,9 +3284,10 @@ main_skip_backups:
+@@ -3210,9 +3270,10 @@ main_skip_backups:
init_termbox:
tb_init();
@@ -1060,7 +1072,7 @@ index 7c142b66a..04d6bfdfa 100644
while (state.running)
{
-@@ -3224,6 +3299,11 @@ init_termbox:
+@@ -3224,6 +3285,11 @@ init_termbox:
recalculate_size(&state);
state.recalculate_size = 0;
}