Commit 02517e0a authored by David Lawrence Ramsey's avatar David Lawrence Ramsey
Browse files

add DB's overhaul of the file loading code to increase efficiency,

remove ugly workarounds for most cases of edittop's or current's being
NULL (as those cases no longer occur due to the overhaul), and remove
detection of binary files (since it wasn't always accurate and will only
cause problems when UTF-8 support is added); also add a few minor fixes
of mine


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1928 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
parent 8bf08097
Showing with 431 additions and 431 deletions
+431 -431
......@@ -15,6 +15,29 @@ CVS code -
- Remove reference to @includedir@ in src/Makefile.am, as it's
unneeded and can break cross-compilation. (DLR, found by Mike
Frysinger)
- Overhaul the file opening, reading, and loading operations to
increase efficiency, avoid problems on invalid filenames
specified on the command line, and eliminate corner cases that
erroneously leave edittop or current NULL when they shouldn't
be. Also split out the code to execute a command into a
separate function, eliminate a workaround for one of the
aforementioned corner cases, handle files with a mix of DOS
and Mac format lines, and remove the code to turn on the
NO_CONVERT flag when opening a binary file, as it's not always
reliable and will cause problems with UTF-8 text files. New
functions open_file(), execute_command(), and mallocstrassn();
changes to read_line(), load_file(), read_file(), open_file(),
get_next_filename(), do_insertfile(), do_insertfile_void(),
do_alt_speller(), and edit_refresh(). (David Benbennick) DLR:
Add a few minor fixes to make sure that current is set
properly in all cases, indicate on the statusbar when the file
has a mix of DOS and Mac format lines, move the test for DOS
line endings from read_line() to read_file() to avoid
inaccurate statusbar messages and to reduce fileformat to a
local variable in read_file(), eliminate another workaround in
edit_update(), rename open_the_file() to open_file() since the
latter has been removed, and rename load_a_file() to
load_buffer().
- files.c:
do_insertfile()
- Readd the NANO_SMALL #ifdef around the start_again: label to
......@@ -23,14 +46,23 @@ CVS code -
shortcut_init()
- Remove redundant NANO_SMALL #ifdef. (DLR)
- nano.c:
die_save_file()
- Clarify the error message when there are too many backup files
and the current one can't be written. (DLR)
do_para_begin(), do_para_end()
- Maintain current_y's value when moving up or down lines so
that smooth scrolling works correctly. (DLR)
- nano.h:
- Add WIDTH_OF_TAB #define, containing the default width of a
tab. (DLR)
- rcfile.c:
parse_rcfile()
- Add missing brackets around an if statement block so that
parsing the numeric argument after "tabsize" works properly
again. (DLR, found by Mike Frysinger)
- Since flag values are longs, use "%ld" instead of "%d" in the
debugging messages indicating when a flag is set or unset.
(DLR)
- search.c:
findnextstr()
- Take the no_sameline parameter after can_display_wrap and
......@@ -56,6 +88,10 @@ CVS code -
- If there are more than MAIN_VISIBLE shortcuts available, only
register clicks on the first MAIN_VISIBLE shortcuts, since
bottombars() only shows that many shortcuts. (DLR)
reset_cursor()
- If this is called before any files have been opened, as it can
be by statusbar(), put the cursor at the top left corner of
the edit window before getting out. (DLR)
edit_refresh()
- Call edit_update() with NONE instead of CENTER when smooth
scrolling is on, for consistency with the movement routines.
......
This diff is collapsed.
......@@ -367,7 +367,8 @@ void shortcut_init(int unjustify)
);
#ifdef ENABLE_MULTIBUFFER
if (open_files != NULL && (open_files->prev != NULL || open_files->next != NULL))
if (open_files != NULL && (open_files->prev != NULL ||
open_files->next != NULL))
/* Translators: try to keep this string under 10 characters long */
sc_init_one(&main_list, NANO_EXIT_KEY, N_("Close"),
IFHELP(nano_exit_msg, NANO_NO_KEY), NANO_EXIT_FKEY,
......
......@@ -179,7 +179,7 @@ void die_save_file(const char *die_filename)
if (!failed)
fprintf(stderr, _("\nBuffer written to %s\n"), ret);
else
fprintf(stderr, _("\nNo %s written (too many backup files?)\n"), ret);
fprintf(stderr, _("\nBuffer not written to %s (too many backup files?)\n"), ret);
free(ret);
}
......@@ -856,7 +856,7 @@ bool open_pipe(const char *command)
if (f == NULL)
nperror("fdopen");
read_file(f, "stdin", FALSE);
read_file(f, "stdin");
/* If multibuffer mode is on, we could be here in view mode. If so,
* don't set the modification flag. */
if (!ISSET(VIEW_MODE))
......@@ -1406,7 +1406,7 @@ bool do_int_spell_fix(const char *word)
bool reverse_search_set = ISSET(REVERSE_SEARCH);
#ifndef NANO_SMALL
bool case_sens_set = ISSET(CASE_SENSITIVE);
bool mark_set = ISSET(MARK_ISSET);
bool old_mark_set = ISSET(MARK_ISSET);
SET(CASE_SENSITIVE);
/* Make sure the marking highlight is off during spell-check. */
......@@ -1475,7 +1475,7 @@ bool do_int_spell_fix(const char *word)
UNSET(CASE_SENSITIVE);
/* Restore marking highlight. */
if (mark_set)
if (old_mark_set)
SET(MARK_ISSET);
#endif
......@@ -1678,16 +1678,17 @@ const char *do_alt_speller(char *tempfile_name)
pid_t pid_spell;
char *ptr;
static int arglen = 3;
static char **spellargs = (char **)NULL;
static char **spellargs = NULL;
FILE *f;
#ifndef NANO_SMALL
bool mark_set = ISSET(MARK_ISSET);
bool old_mark_set = ISSET(MARK_ISSET);
int mbb_lineno_cur = 0;
/* We're going to close the current file, and open the output of
* the alternate spell command. The line that mark_beginbuf
* points to will be freed, so we save the line number and
* restore afterwards. */
if (mark_set) {
if (old_mark_set) {
mbb_lineno_cur = mark_beginbuf->lineno;
UNSET(MARK_ISSET);
}
......@@ -1738,7 +1739,7 @@ const char *do_alt_speller(char *tempfile_name)
refresh();
#ifndef NANO_SMALL
if (mark_set) {
if (old_mark_set) {
do_gotopos(mbb_lineno_cur, mark_beginx, y_cur, 0);
mark_beginbuf = current;
/* In case the line got shorter, assign mark_beginx. */
......@@ -1750,7 +1751,13 @@ const char *do_alt_speller(char *tempfile_name)
free_filestruct(fileage);
terminal_init();
global_init(TRUE);
open_file(tempfile_name, FALSE, TRUE);
/* Do what load_buffer() would do, except for making a new
* buffer for the temp file if multibuffer support is
* available. */
open_file(tempfile_name, FALSE, &f);
read_file(f, tempfile_name);
current = fileage;
#ifndef NANO_SMALL
}
#endif
......@@ -2564,6 +2571,7 @@ void do_justify(bool full_justify)
edit_refresh();
statusbar(_("Can now UnJustify!"));
/* Display the shortcut list with UnJustify. */
shortcut_init(TRUE);
display_main_list();
......@@ -2618,6 +2626,7 @@ void do_justify(bool full_justify)
cutbuffer = cutbuffer_save;
/* Note that now cutbottom is invalid, but that's okay. */
blank_statusbar();
/* Display the shortcut list with UnCut. */
shortcut_init(FALSE);
display_main_list();
......@@ -2940,11 +2949,19 @@ void terminal_init(void)
int main(int argc, char **argv)
{
int optchr;
int startline = 0; /* Line to try and start at */
int startline = 0;
/* Line to try and start at. */
#ifndef DISABLE_WRAPJUSTIFY
bool fill_flag_used = FALSE; /* Was the fill option used? */
bool fill_flag_used = FALSE;
/* Was the fill option used? */
#endif
int kbinput; /* Input from keyboard */
#ifdef ENABLE_MULTIBUFFER
bool old_multibuffer;
/* The old value of the multibuffer option, restored after we
* load all files on the command line. */
#endif
int kbinput;
/* Input from keyboard. */
bool meta_key;
#ifdef HAVE_GETOPT_LONG
const struct option long_options[] = {
......@@ -3015,8 +3032,8 @@ int main(int argc, char **argv)
#endif
#if !defined(ENABLE_NANORC) && defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
/* if we don't have rcfile support, we're root, and
--disable-wrapping-as-root is used, turn wrapping off */
/* If we don't have rcfile support, we're root, and
* --disable-wrapping-as-root is used, turn wrapping off. */
if (geteuid() == NANO_ROOT_UID)
SET(NO_WRAP);
#endif
......@@ -3190,8 +3207,8 @@ int main(int argc, char **argv)
}
/* We've read through the command line options. Now back up the flags
and values that are set, and read the rcfile(s). If the values
haven't changed afterward, restore the backed-up values. */
* and values that are set, and read the rcfile(s). If the values
* haven't changed afterward, restore the backed-up values. */
#ifdef ENABLE_NANORC
if (!ISSET(NO_RCFILE)) {
#ifndef DISABLE_OPERATINGDIR
......@@ -3336,43 +3353,26 @@ int main(int argc, char **argv)
#endif
#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
/* If whitespace wasn't specified, set its default value. */
if (whitespace == NULL)
whitespace = mallocstrcpy(NULL, " ");
#endif
/* If tabsize wasn't specified, set its default value. */
if (tabsize == -1)
tabsize = 8;
/* Clear the filename we'll be using */
filename = charalloc(1);
filename[0] = '\0';
tabsize = WIDTH_OF_TAB;
/* If there's a +LINE flag, it is the first non-option argument. */
if (0 < optind && optind < argc && argv[optind][0] == '+') {
startline = atoi(&argv[optind][1]);
optind++;
}
if (0 < optind && optind < argc)
filename = mallocstrcpy(filename, argv[optind]);
/* See if there's a non-option in argv (first non-option is the
filename, if +LINE is not given) */
if (argc > 1 && argc > optind) {
/* Look for the +line flag... */
if (argv[optind][0] == '+') {
startline = atoi(&argv[optind][1]);
optind++;
if (argc > optind)
filename = mallocstrcpy(filename, argv[optind]);
} else
filename = mallocstrcpy(filename, argv[optind]);
}
/* Back up the old terminal settings so that they can be restored. */
tcgetattr(0, &oldterm);
/* Curses initialization stuff: Start curses and set up the
* terminal state. */
/* Curses initialization stuff: Start curses and set up the
* terminal state. */
initscr();
terminal_init();
......@@ -3392,44 +3392,55 @@ int main(int argc, char **argv)
mouse_init();
#endif
#ifdef DEBUG
fprintf(stderr, "Main: top and bottom win\n");
#endif
titlebar(NULL);
display_main_list();
#ifdef DEBUG
fprintf(stderr, "Main: open file\n");
#endif
open_file(filename, FALSE, FALSE);
#ifdef ENABLE_MULTIBUFFER
/* If we're using multibuffers and more than one file is specified
on the command line, load them all and switch to the first one
afterward. */
if (optind + 1 < argc) {
bool old_multibuffer = ISSET(MULTIBUFFER), list = FALSE;
SET(MULTIBUFFER);
for (optind++; optind < argc; optind++) {
add_open_file(TRUE);
new_file();
filename = mallocstrcpy(filename, argv[optind]);
titlebar(NULL);
open_file(filename, FALSE, FALSE);
load_file(FALSE);
/* Display the main list with "Close" if we haven't
* already. */
if (!list) {
shortcut_init(FALSE);
list = TRUE;
display_main_list();
}
}
open_nextfile_void();
if (!old_multibuffer)
UNSET(MULTIBUFFER);
old_multibuffer = ISSET(MULTIBUFFER);
SET(MULTIBUFFER);
/* Read all the files after the first one on the command line into
* new buffers. */
{
int i;
for (i = optind + 1; i < argc; i++)
load_buffer(argv[i]);
}
#endif
/* Read the first file on the command line into either the current
* buffer or a new buffer, depending on whether multibuffer mode is
* enabled. */
if (optind < argc)
load_buffer(argv[optind]);
/* We didn't open any files if all the command line arguments were
* invalid files like directories or if there were no command line
* arguments given. In this case, we have to load a blank buffer.
* Also, we unset view mode to allow editing. */
if (filename == NULL) {
filename = mallocstrcpy(NULL, "");
new_file();
UNSET(VIEW_MODE);
/* Add this new entry to the open_files structure if we have
* multibuffer support, or to the main filestruct if we don't. */
load_file();
}
#ifdef ENABLE_MULTIBUFFER
if (!old_multibuffer)
UNSET(MULTIBUFFER);
#endif
#ifdef DEBUG
fprintf(stderr, "Main: top and bottom win\n");
#endif
titlebar(NULL);
display_main_list();
if (startline > 0)
do_gotoline(startline, FALSE);
......
......@@ -491,6 +491,9 @@ typedef enum {
* occurs. */
#define CHARS_FROM_EOL 8
/* Default width of a tab. */
#define WIDTH_OF_TAB 8
/* Maximum number of search history strings saved, same value used for
* replace history. */
#define MAX_SEARCH_HISTORY 100
......
......@@ -164,14 +164,18 @@ void do_cut_text(void);
void do_uncut_text(void);
/* Public functions in files.c */
void load_file(int update);
void new_file(void);
filestruct *read_line(char *buf, filestruct *prev, int *line1ins, size_t
len);
void read_file(FILE *f, const char *filename, int quiet);
bool open_file(const char *filename, int insert, int quiet);
filestruct *read_line(char *buf, filestruct *prev, bool *first_line_ins,
size_t len);
void load_file(void);
void read_file(FILE *f, const char *filename);
int open_file(const char *filename, bool newfie, FILE **f);
char *get_next_filename(const char *name);
void do_insertfile(int loading_file);
#ifndef NANO_SMALL
void execute_command(const char *command);
#endif
void load_buffer(const char *name);
void do_insertfile(void);
void do_insertfile_void(void);
#ifdef ENABLE_MULTIBUFFER
openfilestruct *make_new_opennode(openfilestruct *prevnode);
......@@ -411,7 +415,7 @@ int do_replace_loop(const char *needle, const filestruct *real_current,
void do_replace(void);
void do_gotoline(int line, bool save_pos);
void do_gotoline_void(void);
#if defined (ENABLE_MULTIBUFFER) || !defined (DISABLE_SPELLER)
#if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER)
void do_gotopos(int line, int pos_x, int pos_y, size_t pos_pww);
#endif
void do_find_bracket(void);
......@@ -479,6 +483,7 @@ void nperror(const char *s);
void *nmalloc(size_t howmuch);
void *nrealloc(void *ptr, size_t howmuch);
char *mallocstrcpy(char *dest, const char *src);
char *mallocstrassn(char *dest, char *src);
void new_magicline(void);
#ifndef NANO_SMALL
void mark_order(const filestruct **top, size_t *top_x, const filestruct
......
......@@ -619,13 +619,13 @@ void parse_rcfile(FILE *rcstream)
} else
SET(rcopts[i].flag);
#ifdef DEBUG
fprintf(stderr, "set flag %d!\n",
fprintf(stderr, "set flag %ld!\n",
rcopts[i].flag);
#endif
} else {
UNSET(rcopts[i].flag);
#ifdef DEBUG
fprintf(stderr, "unset flag %d!\n",
fprintf(stderr, "unset flag %ld!\n",
rcopts[i].flag);
#endif
}
......
......@@ -616,7 +616,7 @@ int do_replace_loop(const char *needle, const filestruct *real_current,
bool begin_line = FALSE, bol_or_eol = FALSE;
#endif
#ifndef NANO_SMALL
bool old_mark_isset = ISSET(MARK_ISSET);
bool old_mark_set = ISSET(MARK_ISSET);
UNSET(MARK_ISSET);
edit_refresh();
......@@ -758,7 +758,7 @@ int do_replace_loop(const char *needle, const filestruct *real_current,
new_magicline();
#ifndef NANO_SMALL
if (old_mark_isset)
if (old_mark_set)
SET(MARK_ISSET);
#endif
......
......@@ -396,7 +396,7 @@ void *nrealloc(void *ptr, size_t howmuch)
}
/* Copy one malloc()ed string to another pointer. Should be used as:
* dest = mallocstrcpy(dest, src); */
* "dest = mallocstrcpy(dest, src);". */
char *mallocstrcpy(char *dest, const char *src)
{
if (src == NULL)
......@@ -411,7 +411,16 @@ char *mallocstrcpy(char *dest, const char *src)
return dest;
}
/* Append a new magic-line to filebot. */
/* Free the malloc()ed string at dest and return the malloc()ed string
* at src. Should be used as: "answer = mallocstrassn(answer,
* real_dir_from_tilde(answer));". */
char *mallocstrassn(char *dest, char *src)
{
free(dest);
return src;
}
/* Append a new magicline to filebot. */
void new_magicline(void)
{
filebot->next = (filestruct *)nmalloc(sizeof(filestruct));
......
......@@ -2385,13 +2385,15 @@ size_t get_page_start(size_t column)
}
/* Resets current_y, based on the position of current, and puts the
* cursor at (current_y, current_x). */
* cursor in the edit window at (current_y, current_x). */
void reset_cursor(void)
{
/* Yuck. This condition can be true after open_file() when opening
* the first file. */
if (edittop == NULL)
/* If we haven't opened any files yet, put the cursor in the top
* left corner of the edit window and get out. */
if (edittop == NULL || current == NULL) {
wmove(edit, 0, 0);
return;
}
current_y = current->lineno - edittop->lineno;
if (current_y < editwinrows) {
......@@ -2884,14 +2886,6 @@ void edit_redraw(const filestruct *old_current, size_t old_pww)
/* Refresh the screen without changing the position of lines. */
void edit_refresh(void)
{
/* Neither of these conditions should occur, but they do. edittop
* is NULL when you open an existing file on the command line, and
* ENABLE_COLOR is defined. Yuck. */
if (current == NULL)
return;
if (edittop == NULL)
edittop = current;
if (current->lineno < edittop->lineno ||
current->lineno >= edittop->lineno + editwinrows)
/* Note that edit_update() changes edittop so that it's in range
......@@ -2932,10 +2926,6 @@ void edit_update(topmidnone location)
{
filestruct *foo = current;
/* We shouldn't need this check. Yuck. */
if (current == NULL)
return;
if (location != TOP) {
/* If location is CENTER, we move edittop up (editwinrows / 2)
* lines. This puts current at the center of the screen. If
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment