From 9245f9796031bed19ea22eb0ab132277ff80a23d Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Tue, 17 May 2005 18:06:26 +0000 Subject: [PATCH] more LINE,COLUMN-related work: fix problems parsing it, and allow it at the (soon-to-be-renamed) "Go to Line" prompt git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2522 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 4 +++- src/global.c | 3 ++- src/nano.c | 20 +++++++------------- src/proto.h | 5 +++-- src/search.c | 39 +++++++++++++++++++++------------------ src/utils.c | 8 +++++--- 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/ChangeLog b/ChangeLog index 22a071ea..c240c1cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,7 +16,9 @@ CVS code - - Add the ability to open a file on a specified column as well as a specified line, by allowing an argument of the form +LINE,COLUMN. New function parse_line_column(); changes to - main(), nano.1, and nano.texi. (DLR, suggested by PFTank) + main(), do_gotoline() (renamed do_gotolinecolumn()), + do_gotoline_void() (renamed do_gotolinecolumn_void()), nano.1, + and nano.texi. (DLR, suggested by PFTank) - cut.c: cut_line() - Set placewewant properly after cutting a line, to avoid a diff --git a/src/global.c b/src/global.c index f2a45761..aeaf7b74 100644 --- a/src/global.c +++ b/src/global.c @@ -480,7 +480,8 @@ void shortcut_init(bool unjustify) sc_init_one(&main_list, NANO_GOTOLINE_KEY, go_to_line_msg, IFHELP(nano_gotoline_msg, NANO_GOTOLINE_ALTKEY), - NANO_GOTOLINE_FKEY, NANO_NO_KEY, VIEW, do_gotoline_void); + NANO_GOTOLINE_FKEY, NANO_NO_KEY, VIEW, + do_gotolinecolumn_void); sc_init_one(&main_list, NANO_REPLACE_KEY, replace_msg, IFHELP(nano_replace_msg, NANO_ALT_REPLACE_KEY), diff --git a/src/nano.c b/src/nano.c index c3869d64..bb4e9ce8 100644 --- a/src/nano.c +++ b/src/nano.c @@ -4446,18 +4446,15 @@ int main(int argc, char **argv) * followed by at least one other argument, the filename it * applies to. */ if (i < argc - 1 && argv[i][0] == '+' && iline == 1 && - icol == 1) { + icol == 1) parse_line_column(&argv[i][1], &iline, &icol); - } else { + else { load_buffer(argv[i]); - if (iline > 1) { - do_gotoline(iline, FALSE); + if (iline > 1 || icol > 1) { + do_gotolinecolumn(iline, icol - 1, FALSE, FALSE, + FALSE); iline = 1; - } - - if (icol > 1) { - current_x = actual_x(current->data, icol - 1); icol = 1; } } @@ -4498,11 +4495,8 @@ int main(int argc, char **argv) titlebar(NULL); display_main_list(); - if (startline > 1) - do_gotoline(startline, FALSE); - - if (startcol > 1) - current_x = actual_x(current->data, startcol - 1); + if (startline > 1 || startcol > 1) + do_gotolinecolumn(startline, startcol, FALSE, FALSE, FALSE); #ifndef NANO_SMALL /* Return here after a SIGWINCH. */ diff --git a/src/proto.h b/src/proto.h index b6430ad7..9cde7e68 100644 --- a/src/proto.h +++ b/src/proto.h @@ -516,8 +516,9 @@ ssize_t do_replace_loop(const char *needle, const filestruct *real_current, size_t *real_current_x, bool wholewords, bool *canceled); void do_replace(void); -void do_gotoline(int line, bool save_pos); -void do_gotoline_void(void); +void do_gotolinecolumn(int line, ssize_t column, bool use_answer, bool + interactive, bool save_pos); +void do_gotolinecolumn_void(void); #if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER) void do_gotopos(int line, size_t pos_x, int pos_y, size_t pos_pww); #endif diff --git a/src/search.c b/src/search.c index 0d2040d6..d52165c3 100644 --- a/src/search.c +++ b/src/search.c @@ -255,9 +255,9 @@ int search_init(bool replacing, bool use_answer) #ifndef NANO_SMALL search_history.current = search_history.next; #endif - do_gotoline(-1, FALSE); /* Put answer up on the - * statusbar and fall - * through. */ + do_gotolinecolumn(1, 1, TRUE, TRUE, FALSE); + /* Put answer up on the statusbar and + * fall through. */ default: return -1; } @@ -969,11 +969,12 @@ void do_replace(void) replace_abort(); } -void do_gotoline(int line, bool save_pos) +void do_gotolinecolumn(int line, ssize_t column, bool use_answer, bool + interactive, bool save_pos) { - if (line <= 0) { /* Ask for it. */ + if (interactive) { /* Ask for it. */ char *ans = mallocstrcpy(NULL, answer); - int i = statusq(FALSE, gotoline_list, line < 0 ? ans : "", + int i = statusq(FALSE, gotoline_list, use_answer ? ans : "", #ifndef NANO_SMALL NULL, #endif @@ -998,12 +999,19 @@ void do_gotoline(int line, bool save_pos) /* Do a bounds check. Display a warning on an out-of-bounds * line number only if we hit Enter at the statusbar prompt. */ - if (!parse_num(answer, &line) || line < 1) { + if (!parse_line_column(answer, &line, &column) || line < 1 || + column < 1) { if (i == 0) statusbar(_("Come on, be reasonable")); display_main_list(); return; } + } else { + if (line < 1) + line = 1; + + if (column < 1) + column = 1; } if (current->lineno > line) { @@ -1016,33 +1024,28 @@ void do_gotoline(int line, bool save_pos) ; } - current_x = 0; + current_x = actual_x(current->data, column - 1); /* If save_pos is TRUE, don't change the cursor position when * updating the edit window. */ edit_update(save_pos ? NONE : CENTER); - placewewant = 0; + placewewant = xplustabs(); display_main_list(); } -void do_gotoline_void(void) +void do_gotolinecolumn_void(void) { - do_gotoline(0, FALSE); + do_gotolinecolumn(1, 1, FALSE, TRUE, FALSE); } #if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER) void do_gotopos(int line, size_t pos_x, int pos_y, size_t pos_pww) { - /* Since do_gotoline() resets the x-coordinate but not the + /* Since do_gotolinecolumn() resets the x-coordinate but not the * y-coordinate, set the coordinates up this way. */ current_y = pos_y; - do_gotoline(line, TRUE); - - /* Make sure that the x-coordinate is sane here. */ - current_x = strlen(current->data); - if (pos_x < current_x) - current_x = pos_x; + do_gotolinecolumn(line, pos_x, FALSE, FALSE, TRUE); /* Set the rest of the coordinates up. */ placewewant = pos_pww; diff --git a/src/utils.c b/src/utils.c index 6a016d22..c2ca4356 100644 --- a/src/utils.c +++ b/src/utils.c @@ -124,9 +124,10 @@ bool parse_line_column(const char *str, int *line, ssize_t *column) if (line != NULL) { if (comma != NULL) { - char *str_line = mallocstrncpy(NULL, str, comma - str); + char *str_line = mallocstrncpy(NULL, str, comma - str + 1); + str_line[comma - str] = '\0'; - if (!parse_num(str_line, line)) + if (str_line[0] != '\0' && !parse_num(str_line, line)) retval = FALSE; free(str_line); @@ -358,7 +359,8 @@ char *mallocstrncpy(char *dest, const char *src, size_t n) * "dest = mallocstrcpy(dest, src);". */ char *mallocstrcpy(char *dest, const char *src) { - return mallocstrncpy(dest, src, src == NULL ? 1 : strlen(src) + 1); + return mallocstrncpy(dest, src, (src == NULL) ? 1 : + strlen(src) + 1); } /* Free the malloc()ed string at dest and return the malloc()ed string -- 2.39.5