From 17ceb233b0a42d28a9765a6184a417dd05b5490b Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 6 Feb 2013 10:51:36 +0400 Subject: [PATCH] (edit_insert_column_of_text_from_file): refactoring: * move from src/editor/editcmd.c into src/editor/edit.c and make static; * rename to edit_insert_column_from_file; * make some refactoring. Signed-off-by: Andrew Borodin --- src/editor/edit-impl.h | 3 -- src/editor/edit.c | 75 +++++++++++++++++++++++++++++++++++++++++++++-- src/editor/editcmd.c | 79 ++------------------------------------------------ 3 files changed, 74 insertions(+), 83 deletions(-) diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index 16c1b276d..3c8cf84df 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -254,9 +254,6 @@ gboolean edit_save_block (WEdit * edit, const char *filename, off_t start, off_t gboolean edit_save_block_cmd (WEdit * edit); gboolean edit_insert_file_cmd (WEdit * edit); void edit_insert_over (WEdit * edit); -off_t edit_insert_column_of_text_from_file (WEdit * edit, int file, - off_t * start_pos, off_t * end_pos, long *col1, - long *col2); char *edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len, gsize * cut); diff --git a/src/editor/edit.c b/src/editor/edit.c index dc1e485bc..e2a482fc2 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -2,13 +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, 2012 + 2007, 2008, 2009, 2010, 2011, 2012, 2013 The Free Software Foundation, Inc. Written by: Paul Sheer 1996, 1997 Ilia Maslakov 2009, 2010, 2011 - Andrew Borodin 2012. + Andrew Borodin 2012, 2013 This file is part of the Midnight Commander. @@ -1758,6 +1758,75 @@ edit_print_string (WEdit * e, const char *s) } /* --------------------------------------------------------------------------------------------- */ + +static off_t +edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * end_pos, + long *col1, long *col2) +{ + off_t cursor; + int col; + off_t blocklen = -1, width = 0; + unsigned char *data; + + cursor = edit->curs1; + col = edit_get_col (edit); + data = g_malloc0 (TEMP_BUF_LEN); + + while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0) + { + off_t i; + char *pn; + + pn = strchr ((char *) data, '\n'); + width = pn == NULL ? blocklen : pn - (char *) data; + + for (i = 0; i < blocklen; i++) + { + if (data[i] != '\n') + edit_insert (edit, data[i]); + else + { /* fill in and move to next line */ + long l; + off_t p; + + if (edit_get_byte (edit, edit->curs1) != '\n') + for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width) + edit_insert (edit, ' '); + + for (p = edit->curs1; ; p++) + { + if (p == edit->last_byte) + { + edit_cursor_move (edit, edit->last_byte - edit->curs1); + edit_insert_ahead (edit, '\n'); + p++; + break; + } + if (edit_get_byte (edit, p) == '\n') + { + p++; + break; + } + } + + edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1); + + for (l = col - edit_get_col (edit); l >= space_width; l -= space_width) + edit_insert (edit, ' '); + } + } + } + *col1 = col; + *col2 = col + width; + *start_pos = cursor; + *end_pos = edit->curs1; + edit_cursor_move (edit, cursor - edit->curs1); + g_free (data); + + return blocklen; +} + +/* --------------------------------------------------------------------------------------------- */ /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ @@ -2131,7 +2200,7 @@ edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath) off_t mark1, mark2; long c1, c2; - blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2); + blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2); edit_set_markers (edit, edit->curs1, mark2, c1, c2); /* highlight inserted text then not persistent blocks */ diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 70a308d83..668dd7e9d 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -2,12 +2,12 @@ Editor high level editing commands Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006, - 2007, 2011, 2012 + 2007, 2011, 2012, 2013 The Free Software Foundation, Inc. Written by: Paul Sheer, 1996, 1997 - Andrew Borodin , 2012 + Andrew Borodin , 2012, 2013 Ilia Maslakov , 2012 This file is part of the Midnight Commander. @@ -2309,81 +2309,6 @@ edit_insert_over (WEdit * edit) /* --------------------------------------------------------------------------------------------- */ -off_t -edit_insert_column_of_text_from_file (WEdit * edit, int file, - off_t * start_pos, off_t * end_pos, long *col1, long *col2) -{ - off_t cursor; - int col; - off_t blocklen = -1, width = 0; - unsigned char *data; - - cursor = edit->curs1; - col = edit_get_col (edit); - data = g_malloc0 (TEMP_BUF_LEN); - - while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0) - { - off_t i; - for (width = 0; width < blocklen; width++) - { - if (data[width] == '\n') - break; - } - for (i = 0; i < blocklen; i++) - { - if (data[i] == '\n') - { /* fill in and move to next line */ - long l; - off_t p; - if (edit_get_byte (edit, edit->curs1) != '\n') - { - l = width - (edit_get_col (edit) - col); - while (l > 0) - { - edit_insert (edit, ' '); - l -= space_width; - } - } - for (p = edit->curs1;; p++) - { - if (p == edit->last_byte) - { - edit_cursor_move (edit, edit->last_byte - edit->curs1); - edit_insert_ahead (edit, '\n'); - p++; - break; - } - if (edit_get_byte (edit, p) == '\n') - { - p++; - break; - } - } - edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1); - l = col - edit_get_col (edit); - while (l >= space_width) - { - edit_insert (edit, ' '); - l -= space_width; - } - continue; - } - edit_insert (edit, data[i]); - } - } - *col1 = col; - *col2 = col + width; - *start_pos = cursor; - *end_pos = edit->curs1; - edit_cursor_move (edit, cursor - edit->curs1); - g_free (data); - - return blocklen; -} - -/* --------------------------------------------------------------------------------------------- */ - void edit_block_copy_cmd (WEdit * edit) { -- 2.11.4.GIT