From 6671e01295cd583d51b4a88805067a06a4cb8f3b Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 3 Jun 2016 10:56:19 +0200 Subject: [PATCH 2/2] text: use a per-file variable to store the comment sequence --- src/color.c | 7 +++++++ src/files.c | 3 +++ src/nano.c | 3 +++ src/nano.h | 4 ++++ src/text.c | 19 +++++-------------- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/color.c b/src/color.c index 75e9243..3cf12e3 100644 --- a/src/color.c +++ b/src/color.c @@ -290,6 +290,13 @@ void color_update(void) regcomp(ink->end, fixbounds(ink->end_regex), ink->rex_flags); } } + +#ifdef ENABLE_COMMENT + /* If the syntax defines a comment string, override the default. */ + if (sint != NULL && sint->comment != NULL) + openfile->comment_string = mallocstrcpy(openfile->comment_string, + sint->comment); +#endif } /* Reset the multiline coloring cache for one specific regex (given by diff --git a/src/files.c b/src/files.c index 946c737..7be9ab9 100644 --- a/src/files.c +++ b/src/files.c @@ -108,6 +108,9 @@ void make_new_buffer(void) openfile->current_stat = NULL; openfile->lock_filename = NULL; #endif +#ifdef ENABLE_COMMENT + openfile->comment_string = mallocstrcpy(NULL, "#"); +#endif #ifndef DISABLE_COLOR openfile->syntax = NULL; openfile->colorstrings = NULL; diff --git a/src/nano.c b/src/nano.c index 5c1b78a..3fcb726 100644 --- a/src/nano.c +++ b/src/nano.c @@ -551,6 +551,9 @@ void delete_opennode(openfilestruct *fileptr) /* Free the undo stack. */ discard_until(NULL, fileptr); #endif +#ifdef ENABLE_COMMENT + free(fileptr->comment_string); +#endif free(fileptr); } diff --git a/src/nano.h b/src/nano.h index 7ef66d5..b65b0ad 100644 --- a/src/nano.h +++ b/src/nano.h @@ -420,6 +420,10 @@ typedef struct openfilestruct { char *lock_filename; /* The path of the lockfile, if we created one. */ #endif +#ifdef ENABLE_COMMENT + char *comment_string; + /* The prefix (and postfix) for commenting a line in this file. */ +#endif #ifndef DISABLE_COLOR syntaxtype *syntax; /* The syntax struct for this file, if any. */ diff --git a/src/text.c b/src/text.c index 40712cf..3cf6d4e 100644 --- a/src/text.c +++ b/src/text.c @@ -43,10 +43,6 @@ static bool prepend_wrap = FALSE; static filestruct *jusbottom = NULL; /* Pointer to the end of the justify buffer. */ #endif -#ifdef ENABLE_COMMENT -static char *comment_seq; - /* The string to be used for commenting a line. */ -#endif #ifndef NANO_TINY /* Toggle the mark. */ @@ -451,14 +447,9 @@ void do_comment() assert(openfile->current != NULL && openfile->current->data != NULL); - comment_seq = "#"; - #ifndef DISABLE_COLOR - if (openfile->syntax && openfile->syntax->comment) - comment_seq = openfile->syntax->comment; - - /* Does the syntax not allow comments? */ - if (strlen(comment_seq) == 0) { + /* Does the current syntax not allow comments? */ + if (strlen(openfile->comment_string) == 0) { statusbar(_("Commenting is not supported for this file type")); return; } @@ -483,7 +474,7 @@ void do_comment() empty = white_string(f->data); /* If this line is not blank and not commented, we comment all. */ - if (!empty && !comment_line(PREFLIGHT, f, comment_seq)) { + if (!empty && !comment_line(PREFLIGHT, f, openfile->comment_string)) { action = COMMENT; break; } @@ -498,7 +489,7 @@ void do_comment() /* Process the selected line or lines. */ for (f = top; f != bot->next; f = f->next) { /* Comment/uncomment a line, and add undo data when line changed. */ - if (comment_line(action, f, comment_seq)) + if (comment_line(action, f, openfile->comment_string)) update_comment_undo(f->lineno); } @@ -1255,7 +1246,7 @@ void add_undo(undo_type action) case UNCOMMENT: /* Store the comment sequence used for the operation, because it could * change when the file name changes; we need to know what it was. */ - u->strdata = mallocstrcpy(NULL, comment_seq); + u->strdata = mallocstrcpy(NULL, openfile->comment_string); break; #endif default: -- 2.8.1