From: Francois Fleuret Date: Tue, 28 Jul 2009 09:39:21 +0000 (+0200) Subject: Cosmetices. Added comments. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=27783ac87a15916381959b883e28c97959700970;p=selector.git Cosmetices. Added comments. --- diff --git a/selector.c b/selector.c index 7dc1848..0bdcd1e 100644 --- a/selector.c +++ b/selector.c @@ -163,8 +163,7 @@ void free_hash_table(hash_table_t *hash_table) { the previous index it had. */ int add_and_get_previous_index(hash_table_t *hash_table, - const char *new_string, int new_index, - char **strings) { + const char *new_string, int new_index, char **strings) { unsigned int code = 0; int k; @@ -356,15 +355,14 @@ int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matche /*********************************************************************/ -/* The value passed to this routine in current_focus_line is the index - of the line we should have highlighted if there was no motion and if - it matched the matcher. So, the line actually highlighted is the - first one matching the matcher in that order: (1) - current_focus_line after motion, (2) the first with a greater - index, (3) the first with a lesser index. +/* The line highlighted is the first one matching the matcher in that + order: (1) current_focus_line after motion, if it does not match, + then (2) the first with a greater index, if none matches, then (3) + the first with a lesser index. The index of the line actually shown highlighted is written in - displayed_focus_line (it can be -1) + displayed_focus_line (it can be -1 if no line at all matches the + matcher) If there is a motion and a line is actually shown highlighted, its value is written in current_focus_line. */ @@ -389,15 +387,21 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, use_default_colors(); + /* Add an empty line where we will print the modeline at the end */ + addstr("\n"); - /* First, we find a visible line. */ + /* If the regexp is erroneous, print a message saying so */ if(matcher.regexp_error) { attron(attr_error); addnstr("Regexp syntax error", console_width); attroff(attr_error); - } else if(nb_lines > 0) { + } + + /* Else, and we do have lines to select from, find a visible line. */ + + else if(nb_lines > 0) { int new_focus_line; if(match(lines[*current_focus_line], &matcher)) { new_focus_line = *current_focus_line; @@ -519,7 +523,11 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, addnstr("No selection", console_width); attroff(attr_error); } - } else { + } + + /* Else, print a message saying that there are no lines to select from */ + + else { attron(attr_error); addnstr("Empty choice", console_width); attroff(attr_error); @@ -563,6 +571,9 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, addstr(" "); } + /* Add a few info about the mode we are in (regexp and/or case + sensitive) */ + if(use_regexp || case_sensitive) { addstr(" ["); if(use_regexp) { @@ -591,37 +602,38 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, /*********************************************************************/ void store_line(hash_table_t *hash_table, - const char *t, - int nb_lines_max, int *nb_lines, char **lines) { + const char *new_line, + int *nb_lines, char **lines) { int dup; /* Remove the zsh history prefix */ - if(zsh_history && *t == ':') { - while(*t && *t != ';') t++; - if(*t == ';') t++; + if(zsh_history && *new_line == ':') { + while(*new_line && *new_line != ';') new_line++; + if(*new_line == ';') new_line++; } /* Remove the bash history prefix */ if(bash_history) { - while(*t == ' ') t++; - while(*t >= '0' && *t <= '9') t++; - while(*t == ' ') t++; + while(*new_line == ' ') new_line++; + while(*new_line >= '0' && *new_line <= '9') new_line++; + while(*new_line == ' ') new_line++; } /* Check for duplicates with the hash table and insert the line in the list if necessary */ if(hash_table) { - dup = add_and_get_previous_index(hash_table, t, *nb_lines, lines); + dup = add_and_get_previous_index(hash_table, + new_line, *nb_lines, lines); } else { dup = -1; } if(dup < 0) { - lines[*nb_lines] = (char *) malloc((strlen(t) + 1) * sizeof(char)); - strcpy(lines[*nb_lines], t); + lines[*nb_lines] = (char *) malloc((strlen(new_line) + 1) * sizeof(char)); + strcpy(lines[*nb_lines], new_line); } else { /* The string was already in there, so we do not allocate a new string but use the pointer to the first occurence of it */ @@ -637,7 +649,7 @@ void read_file(hash_table_t *hash_table, int nb_lines_max, int *nb_lines, char **lines) { char raw_line[BUFFER_SIZE]; - int start, end, k; + int start, end, eol, k; FILE *file; file = fopen(input_filename, "r"); @@ -651,9 +663,15 @@ void read_file(hash_table_t *hash_table, end = 0; while(*nb_lines < nb_lines_max && (end > start || !feof(file))) { - int eol = start; + eol = start; + + /* Look for the end of a line in what is already in the buffer */ while(eol < end && raw_line[eol] != '\n') eol++; + /* if we did not find the of a line, move what has not been + processed and is in the buffer to the beginning of the buffer, + fill the buffer with new data from the file, and look for the + end of a line */ if(eol == end) { for(k = 0; k < end - start; k++) { raw_line[k] = raw_line[k + start]; @@ -665,6 +683,9 @@ void read_file(hash_table_t *hash_table, while(eol < end && raw_line[eol] != '\n') eol++; } + /* The end of the line is the buffer size, which means the line is + too long */ + if(eol == BUFFER_SIZE) { raw_line[BUFFER_SIZE - 1] = '\0'; fprintf(stderr, "Selector: Line too long (max is %d characters):\n", BUFFER_SIZE); @@ -673,10 +694,13 @@ void read_file(hash_table_t *hash_table, exit(1); } + /* If we got a line, we replace the carriage return by a \0 to + finish the string */ + raw_line[eol] = '\0'; store_line(hash_table, raw_line + start, - nb_lines_max, nb_lines, lines); + nb_lines, lines); start = eol + 1; } @@ -933,10 +957,12 @@ int main(int argc, char **argv) { int e = 0; const char *u; t = lines[l]; + while(*t && *t != label_separator) { u = unctrl(*t++); e += strlen(u); } + labels[l] = (char *) malloc((e + 1) * sizeof(char)); t = lines[l]; s = labels[l];