Minor optimization and type accuracy of some editor functions.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2011-03-23 14:08:17 +03:00
parent ef54468d68
commit b092b10606
5 changed files with 157 additions and 179 deletions

View File

@ -204,7 +204,7 @@ long edit_eol (WEdit * edit, long current);
void edit_update_curs_row (WEdit * edit);
void edit_update_curs_col (WEdit * edit);
void edit_find_bracket (WEdit * edit);
int edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line);
gboolean edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line);
void edit_set_codeset (WEdit * edit);
void edit_block_copy_cmd (WEdit * edit);
@ -226,12 +226,11 @@ int edit_save_confirm_cmd (WEdit * edit);
int edit_save_as_cmd (WEdit * edit);
WEdit *edit_init (WEdit * edit, int y, int x, int lines, int cols,
const vfs_path_t * filename_vpath, long line);
int edit_clean (WEdit * edit);
gboolean edit_clean (WEdit * edit);
gboolean edit_ok_to_exit (WEdit * edit);
int edit_renew (WEdit * edit);
int edit_new_cmd (WEdit * edit);
int edit_reload (WEdit * edit, const vfs_path_t * filename_vpath);
int edit_load_cmd (WEdit * edit, edit_current_file_t what);
gboolean edit_renew (WEdit * edit);
gboolean edit_new_cmd (WEdit * edit);
gboolean edit_load_cmd (WEdit * edit, edit_current_file_t what);
void edit_mark_cmd (WEdit * edit, int unmark);
void edit_mark_current_word_cmd (WEdit * edit);
void edit_mark_current_line_cmd (WEdit * edit);
@ -250,8 +249,8 @@ void edit_insert_over (WEdit * edit);
int edit_insert_column_of_text_from_file (WEdit * edit, int file,
long *start_pos, long *end_pos, int *col1, int *col2);
long edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath);
int edit_load_back_cmd (WEdit * edit);
int edit_load_forward_cmd (WEdit * edit);
gboolean edit_load_back_cmd (WEdit * edit);
gboolean edit_load_forward_cmd (WEdit * edit);
void edit_block_process_cmd (WEdit * edit, int macro_number);
void edit_refresh_cmd (WEdit * edit);
void edit_date_cmd (WEdit * edit);
@ -314,4 +313,17 @@ void edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_inserti
/*** inline functions ****************************************************************************/
/**
* Load a new file into the editor. If it fails, preserve the old file.
* To do it, allocate a new widget, initialize it and, if the new file
* was loaded, copy the data to the old widget.
*
* @returns TRUE on success, FALSE on failure.
*/
static inline gboolean
edit_reload (WEdit * edit, const vfs_path_t * filename_vpath)
{
return edit_reload_line (edit, filename_vpath, 0);
}
#endif /* MC__EDIT_IMPL_H */

View File

@ -2,12 +2,13 @@
Editor low level data handling and cursor fundamentals.
Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011
2007, 2008, 2009, 2010, 2011, 2012
The Free Software Foundation, Inc.
Written by:
Paul Sheer 1996, 1997
Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
Andrew Borodin <aborodin@vmail.ru> 2012.
This file is part of the Midnight Commander.
@ -242,17 +243,19 @@ edit_init_buffers (WEdit * edit)
}
/* --------------------------------------------------------------------------------------------- */
/**
* Load file OR text into buffers. Set cursor to the beginning of file.
* @returns 1 on error.
*
* @returns FALSE on error.
*/
static int
static gboolean
edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
{
long buf, buf2;
int file = -1;
int ret = 1;
gboolean ret = FALSE;
edit->curs2 = edit->last_byte;
buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
@ -267,7 +270,7 @@ edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
g_free (filename);
edit_error_dialog (_("Error"), errmsg);
g_free (errmsg);
return 1;
return FALSE;
}
if (!edit->buffers2[buf2])
@ -288,11 +291,11 @@ edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
break;
}
ret = 0;
ret = TRUE;
}
while (0);
while (FALSE);
if (ret != 0)
if (!ret)
{
gchar *errmsg, *filename;
@ -370,9 +373,16 @@ edit_insert_stream (WEdit * edit, FILE * f)
}
/* --------------------------------------------------------------------------------------------- */
/** Open file and create it if necessary. Return 0 for success, 1 for error. */
/**
* Open file and create it if necessary.
*
* @param edit editor object
* @param filename_vpath file name
* @param st buffer for store stat info
* @returns TRUE for success, FALSE for error.
*/
static int
static gboolean
check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
{
int file;
@ -396,11 +406,9 @@ check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat
g_free (filename);
goto cleanup;
}
else
{
/* New file, delete it if it's not modified or saved */
edit->delete_file = 1;
}
/* New file, delete it if it's not modified or saved */
edit->delete_file = 1;
}
/* Check what we have opened */
@ -448,55 +456,55 @@ check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat
{
edit_error_dialog (_("Error"), errmsg);
g_free (errmsg);
return 1;
return FALSE;
}
return 0;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Open the file and load it into the buffers, either directly or using
* a filter. Return 0 on success, 1 on error.
* a filter. Return TRUE on success, FALSE on error.
*
* Fast loading (edit_load_file_fast) is used when the file size is
* known. In this case the data is read into the buffers by blocks.
* If the file size is not known, the data is loaded byte by byte in
* edit_insert_file.
*
* @param edit editor object
* @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
*/
static int
static gboolean
edit_load_file (WEdit * edit)
{
int fast_load = 1;
gboolean fast_load = TRUE;
/* Cannot do fast load if a filter is used */
if (edit_find_filter (edit->filename_vpath) >= 0)
fast_load = 0;
fast_load = FALSE;
/*
* FIXME: line end translation should disable fast loading as well
* Consider doing fseek() to the end and ftell() for the real size.
*/
if (edit->filename_vpath != NULL)
{
/*
* VFS may report file size incorrectly, and slow load is not a big
* deal considering overhead in VFS.
*/
if (!vfs_file_is_local (edit->filename_vpath))
fast_load = 0;
fast_load = FALSE;
/* If we are dealing with a real file, check that it exists */
if (check_file_access (edit, edit->filename_vpath, &edit->stat1))
return 1;
if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
return FALSE;
}
else
{
/* nothing to load */
fast_load = 0;
fast_load = FALSE;
}
edit_init_buffers (edit);
@ -518,13 +526,13 @@ edit_load_file (WEdit * edit)
if (edit_insert_file (edit, edit->filename_vpath) < 0)
{
edit_clean (edit);
return 1;
return FALSE;
}
edit->undo_stack_disable = 0;
}
}
edit->lb = LB_ASIS;
return 0;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
@ -2283,7 +2291,7 @@ edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * f
edit->converter = str_cnv_from_term;
edit_set_codeset (edit);
if (edit_load_file (edit))
if (!edit_load_file (edit))
{
/* edit_load_file already gives an error message */
if (to_free)
@ -2316,15 +2324,15 @@ edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * f
}
/* --------------------------------------------------------------------------------------------- */
/** Clear the edit struct, freeing everything in it. Return 1 on success */
int
/** Clear the edit struct, freeing everything in it. Return TRUE on success */
gboolean
edit_clean (WEdit * edit)
{
int j = 0;
if (!edit)
return 0;
if (edit == NULL)
return FALSE;
/* a stale lock, remove it */
if (edit->locked)
@ -2360,13 +2368,13 @@ edit_clean (WEdit * edit)
edit_purge_widget (edit);
return 1;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/** returns 1 on success */
int
/** returns TRUE on success */
gboolean
edit_renew (WEdit * edit)
{
int y = edit->widget.y;
@ -2379,44 +2387,15 @@ edit_renew (WEdit * edit)
}
/* --------------------------------------------------------------------------------------------- */
/**
* Load a new file into the editor. If it fails, preserve the old file.
* To do it, allocate a new widget, initialize it and, if the new file
* was loaded, copy the data to the old widget.
* Return 1 on success, 0 on failure.
*/
int
edit_reload (WEdit * edit, const vfs_path_t * filename_vpath)
{
WEdit *e;
int y = edit->widget.y;
int x = edit->widget.x;
int lines = edit->widget.lines;
int columns = edit->widget.cols;
e = g_malloc0 (sizeof (WEdit));
e->widget = edit->widget;
if (edit_init (e, y, x, lines, columns, filename_vpath, 0) == NULL)
{
g_free (e);
return 0;
}
edit_clean (edit);
memcpy (edit, e, sizeof (WEdit));
g_free (e);
return 1;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Load a new file into the editor and set line. If it fails, preserve the old file.
* To do it, allocate a new widget, initialize it and, if the new file
* was loaded, copy the data to the old widget.
* Return 1 on success, 0 on failure.
*
* @returns TRUE on success, FALSE on failure.
*/
int
gboolean
edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
{
WEdit *e;
@ -2427,15 +2406,18 @@ edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
e = g_malloc0 (sizeof (WEdit));
e->widget = edit->widget;
if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
{
g_free (e);
return 0;
return FALSE;
}
edit_clean (edit);
memcpy (edit, e, sizeof (WEdit));
g_free (e);
return 1;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -61,7 +61,7 @@ extern int show_right_margin;
void edit_stack_init (void);
void edit_stack_free (void);
int edit_file (const vfs_path_t * _file_vpath, int line);
gboolean edit_file (const vfs_path_t * _file_vpath, int line);
char *edit_get_file_name (const WEdit * edit);
int edit_get_curs_col (const WEdit * edit);

View File

@ -2,11 +2,12 @@
Editor high level editing commands
Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2011
2007, 2011, 2012
The Free Software Foundation, Inc.
Written by:
Paul Sheer, 1996, 1997
Andrew Borodin <aborodin@vmail.ru> 2012
This file is part of the Midnight Commander.
@ -489,18 +490,24 @@ edit_save_cmd (WEdit * edit)
}
/* --------------------------------------------------------------------------------------------- */
/** returns 1 on error */
/**
* Load file content
*
* @param edit widget object
* @param exp_vpath vfs file path
* @return TRUE if file content was successfully loaded, FALSE otherwise
*/
static int
static gboolean
edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
{
int prev_locked = edit->locked;
vfs_path_t *prev_filename;
int ret = 0;
gboolean ret = TRUE;
prev_filename = vfs_path_clone (edit->filename_vpath);
if (!edit_reload (edit, exp_vpath))
ret = 1;
ret = FALSE;
else if (prev_locked)
unlock_file (prev_filename);
@ -2069,23 +2076,21 @@ edit_save_confirm_cmd (WEdit * edit)
}
/* --------------------------------------------------------------------------------------------- */
/** returns 1 on success */
int
/* returns TRUE on success */
gboolean
edit_new_cmd (WEdit * edit)
{
if (edit->modified)
if (edit->modified
&& edit_query_dialog2 (_("Warning"),
_("Current text was modified without a file save.\n"
"Continue discards these changes"),
_("C&ontinue"), _("&Cancel")) == 1)
{
if (edit_query_dialog2
(_("Warning"),
_
("Current text was modified without a file save.\nContinue discards these changes"),
_("C&ontinue"), _("&Cancel")))
{
edit->force |= REDRAW_COMPLETELY;
return 0;
}
edit->force |= REDRAW_COMPLETELY;
return TRUE;
}
edit->force |= REDRAW_COMPLETELY;
return edit_renew (edit); /* if this gives an error, something has really screwed up */
@ -2093,17 +2098,19 @@ edit_new_cmd (WEdit * edit)
/* --------------------------------------------------------------------------------------------- */
int
gboolean
edit_load_cmd (WEdit * edit, edit_current_file_t what)
{
gboolean ret = TRUE;
if (edit->modified
&& (edit_query_dialog2
(_("Warning"),
_("Current text was modified without a file save.\n"
"Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
&& edit_query_dialog2 (_("Warning"),
_("Current text was modified without a file save.\n"
"Continue discards these changes"), _("C&ontinue"),
_("&Cancel")) == 1)
{
edit->force |= REDRAW_COMPLETELY;
return 0;
return TRUE;
}
switch (what)
@ -2117,18 +2124,16 @@ edit_load_cmd (WEdit * edit, edit_current_file_t what)
MC_HISTORY_EDIT_LOAD, filename);
g_free (filename);
if (exp != NULL)
if (exp != NULL && *exp != '\0')
{
if (*exp != '\0')
{
vfs_path_t *exp_vpath;
vfs_path_t *exp_vpath;
exp_vpath = vfs_path_from_str (exp);
edit_load_file_from_filename (edit, exp_vpath);
vfs_path_free (exp_vpath);
}
g_free (exp);
exp_vpath = vfs_path_from_str (exp);
ret = edit_load_file_from_filename (edit, exp_vpath);
vfs_path_free (exp_vpath);
}
g_free (exp);
}
break;
@ -2145,7 +2150,7 @@ edit_load_cmd (WEdit * edit, edit_current_file_t what)
}
edit->force |= REDRAW_COMPLETELY;
return 0;
return ret;
}
/* --------------------------------------------------------------------------------------------- */
@ -3021,8 +3026,8 @@ edit_save_block_cmd (WEdit * edit)
/* --------------------------------------------------------------------------------------------- */
/** returns TRUE on success */
/** returns TRUE on success */
gboolean
edit_insert_file_cmd (WEdit * edit)
{
@ -3358,78 +3363,57 @@ edit_begin_end_repeat_cmd (WEdit * edit)
/* --------------------------------------------------------------------------------------------- */
int
gboolean
edit_load_forward_cmd (WEdit * edit)
{
if (edit->modified)
if (edit->modified
&& edit_query_dialog2 (_("Warning"),
_("Current text was modified without a file save.\n"
"Continue discards these changes"), _("C&ontinue"),
_("&Cancel")) == 1)
{
if (edit_query_dialog2
(_("Warning"),
_("Current text was modified without a file save\n"
"Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
{
edit->force |= REDRAW_COMPLETELY;
return 0;
}
}
if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
{
if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
{
return 1;
}
edit_stack_iterator++;
if (edit_history_moveto[edit_stack_iterator].filename_vpath)
{
edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
edit_history_moveto[edit_stack_iterator].line);
return 0;
}
else
{
return 1;
}
}
else
{
return 1;
edit->force |= REDRAW_COMPLETELY;
return TRUE;
}
if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
return FALSE;
if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
return FALSE;
edit_stack_iterator++;
if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
edit_history_moveto[edit_stack_iterator].line);
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
int
gboolean
edit_load_back_cmd (WEdit * edit)
{
if (edit->modified)
if (edit->modified
&& edit_query_dialog2 (_("Warning"),
_("Current text was modified without a file save.\n"
"Continue discards these changes"), _("C&ontinue"),
_("&Cancel")) == 1)
{
if (edit_query_dialog2
(_("Warning"),
_("Current text was modified without a file save\n"
"Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
{
edit->force |= REDRAW_COMPLETELY;
return 0;
}
}
if (edit_stack_iterator > 0)
{
edit_stack_iterator--;
if (edit_history_moveto[edit_stack_iterator].filename_vpath)
{
edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
edit_history_moveto[edit_stack_iterator].line);
return 0;
}
else
{
return 1;
}
}
else
{
return 1;
edit->force |= REDRAW_COMPLETELY;
return TRUE;
}
if (edit_stack_iterator < 0)
return FALSE;
edit_stack_iterator--;
if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
edit_history_moveto[edit_stack_iterator].line);
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -359,7 +359,7 @@ edit_callback (Widget * w, widget_msg_t msg, int parm)
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
int
gboolean
edit_file (const vfs_path_t * _file_vpath, int line)
{
static gboolean made_directory = FALSE;
@ -387,7 +387,7 @@ edit_file (const vfs_path_t * _file_vpath, int line)
wedit = edit_init (NULL, 1, 0, LINES - 2, COLS, _file_vpath, line);
if (wedit == NULL)
return 0;
return FALSE;
/* Create a new dialog and add it widgets to it */
edit_dlg =
@ -414,7 +414,7 @@ edit_file (const vfs_path_t * _file_vpath, int line)
if (edit_dlg->state == DLG_CLOSED)
destroy_dlg (edit_dlg);
return 1;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */