This patch makes it so that the syntax coloring updates when you give
a document a name for the first time.
editor.c | 19 +++++++++++++------
editor.h | 3 +++
vis.c | 6 ++++++
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/editor.c b/editor.c
index 668062c..0e3e603 100644
--- a/editor.c
+++ b/editor.c
@@ -257,6 +257,18 @@ static EditorWin *editor_window_new_text(Editor *ed, Text *text) {
return win;
}
+Syntax *determine_syntax_from_filename(Syntax *const syntaxes, const char *const filename) {
+ if (!filename)
+ return NULL;
+
+ for (Syntax *syn = syntaxes; syn && syn->name; syn++) {
+ if (!regexec(&syn->file_regex, filename, 0, NULL, 0))
+ return syn;
+ }
+
+ return NULL;
+}
+
bool editor_window_new(Editor *ed, const char *filename) {
Text *text = NULL;
/* try to detect whether the same file is already open in another window
@@ -291,12 +303,7 @@ bool editor_window_new(Editor *ed, const char *filename) {
window_cursor_to(win->win, window_cursor_get(original->win));
} else if (filename) {
text_filename_set(text, filename);
- for (Syntax *syn = ed->syntaxes; syn && syn->name; syn++) {
- if (!regexec(&syn->file_regex, filename, 0, NULL, 0)) {
- window_syntax_set(win->win, syn);
- break;
- }
- }
+ window_syntax_set(win->win, determine_syntax_from_filename(ed->syntaxes, filename));
}
editor_draw(ed);
diff --git a/editor.h b/editor.h
index ba7d3aa..ccb723d 100644
--- a/editor.h
+++ b/editor.h
@@ -139,6 +139,9 @@ int editor_tabwidth_get(Editor*);
bool editor_syntax_load(Editor*, Syntax *syntaxes, Color *colors);
void editor_syntax_unload(Editor*);
+/* Look at the filename to determine what syntax rules we should be using */
+Syntax *determine_syntax_from_filename(Syntax *const syntaxes, const char *const filename);
+
/* creates a new window, and loads the given file. if filename is NULL
* an unamed / empty buffer is created. If the given file is already opened
* in another window, share the underlying text that is changes will be
diff --git a/vis.c b/vis.c
index dc1ac6e..3716871 100644
--- a/vis.c
+++ b/vis.c
@@ -1660,6 +1660,7 @@ static bool cmd_wq(Filerange *range, enum CmdOpt opt, const char *argv[]) {
static bool cmd_write(Filerange *range, enum CmdOpt opt, const char *argv[]) {
Text *text = vis->win->text;
+ bool no_name = !text_filename_get(text);
if (!argv[1])
argv[1] = text_filename_get(text);
if (!argv[1]) {
@@ -1678,6 +1679,11 @@ static bool cmd_write(Filerange *range, enum CmdOpt opt, const char *argv[]) {
return false;
}
}
+
+ /* File has name for the first time? If so update syntax coloring */
+ if (no_name && text_filename_get(text))
+ window_syntax_set(vis->win->win, determine_syntax_from_filename(vis->syntaxes, text_filename_get(text)));
+
return true;
}