From 6018c523948e50f645af10f5338a56cda1eccf0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Techet?= Date: Fri, 24 Feb 2023 23:17:01 +0100 Subject: [PATCH] Improve code commenting-out global config files Right now, when a config file from Tools->Configuration Files is edited for the first time, the global config file is taken and all lines in it are commented-out. When modified, such a file is copied into the user's config directory. The logic behind this is that the user typically wants to modify just one or few config options while leaving the rest in the default state. However, commenting-out all lines is problemmatic. Consider for instance the beginning of filetypes.common when all lines are commented-out by the current code: ``` #~ # For complete documentation of this file, please see Geany's main documentation #~ [styling] #~ # use foreground;background;bold;italic or named_style,bold,italic #~ #~ # used for filetype All/None #~ #default=default ``` 1. Already commented-out lines are commented out once more which is ugly 2. Sections are commented out. This is problematic because users don't have to just comment-out the variable they want to modify, they must also comment-out the section in which the variable is which is easy to forget. 3. Empty lines are commented out which is unnecessary This patch goes through the config file line by line and keeps sections, comments and empty lines unmodified so the result of the above example is this: ``` # For complete documentation of this file, please see Geany's main documentation [styling] # use foreground;background;bold;italic or named_style,bold,italic # used for filetype All/None #default=default ``` The patch does this only for .conf filetype and uses the original code for other file types. --- src/ui_utils.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/ui_utils.c b/src/ui_utils.c index 6222b2920c..708bc5b4c0 100644 --- a/src/ui_utils.c +++ b/src/ui_utils.c @@ -2096,6 +2096,33 @@ void ui_table_add_row(GtkTable *table, gint row, ...) } +/* comment-out all lines that are not already commented out except sections */ +static void comment_conf_files(ScintillaObject *sci) +{ + gint line, line_count; + + line_count = sci_get_line_count(sci); + for (line = 0; line < line_count; line++) + { + gint pos_start = sci_get_position_from_line(sci, line); + gint pos_end = sci_get_line_end_position(sci, line); + gint pos; + + for (pos = pos_start; pos < pos_end; pos++) + { + gchar c = sci_get_char_at(sci, pos); + if (c == '[' || c == '#') + break; + if (!isspace(c)) + { + sci_insert_text(sci, pos_start, "#"); + break; + } + } + } +} + + static void on_config_file_clicked(GtkWidget *widget, gpointer user_data) { const gchar *file_name = user_data; @@ -2135,9 +2162,14 @@ static void on_config_file_clicked(GtkWidget *widget, gpointer user_data) doc = document_new_file(utf8_filename, ft, global_content); if (global_content) { - sci_select_all(doc->editor->sci); - keybindings_send_command(GEANY_KEY_GROUP_FORMAT, - GEANY_KEYS_FORMAT_COMMENTLINETOGGLE); + if (doc->file_type->id == GEANY_FILETYPES_CONF) + comment_conf_files(doc->editor->sci); + else + { + sci_select_all(doc->editor->sci); + keybindings_send_command(GEANY_KEY_GROUP_FORMAT, + GEANY_KEYS_FORMAT_COMMENTLINETOGGLE); + } sci_set_current_line(doc->editor->sci, 0); document_set_text_changed(doc, FALSE); sci_empty_undo_buffer(doc->editor->sci);