1
1
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Этот коммит содержится в:
Andrew Borodin 2013-02-24 16:15:11 +04:00
родитель c466b18f67
Коммит c5d35ac93b
3 изменённых файлов: 98 добавлений и 107 удалений

Просмотреть файл

@ -226,7 +226,7 @@ gboolean edit_insert_file_cmd (WEdit * edit);
char *edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len, char *edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len,
gsize * cut); gsize * cut);
long edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath); off_t edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath);
gboolean edit_load_back_cmd (WEdit * edit); gboolean edit_load_back_cmd (WEdit * edit);
gboolean edit_load_forward_cmd (WEdit * edit); gboolean edit_load_forward_cmd (WEdit * edit);
void edit_block_process_cmd (WEdit * edit, int macro_number); void edit_block_process_cmd (WEdit * edit, int macro_number);
@ -288,7 +288,7 @@ gboolean is_break_char (char c);
void edit_options_dialog (WDialog * h); void edit_options_dialog (WDialog * h);
void edit_syntax_dialog (WEdit * edit); void edit_syntax_dialog (WEdit * edit);
void edit_mail_dialog (WEdit * edit); void edit_mail_dialog (WEdit * edit);
void format_paragraph (WEdit * edit, int force); void format_paragraph (WEdit * edit, gboolean force);
/* either command or char_for_insertion must be passed as -1 */ /* either command or char_for_insertion must be passed as -1 */
void edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion); void edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion);

Просмотреть файл

@ -225,6 +225,7 @@ edit_insert_stream (WEdit * edit, FILE * f)
{ {
int c; int c;
off_t i = 0; off_t i = 0;
while ((c = fgetc (f)) >= 0) while ((c = fgetc (f)) >= 0)
{ {
edit_insert (edit, c); edit_insert (edit, c);
@ -1888,7 +1889,7 @@ edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsiz
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/** inserts a file at the cursor, returns count of inserted bytes on success */ /** inserts a file at the cursor, returns count of inserted bytes on success */
long off_t
edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath) edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
{ {
char *p; char *p;
@ -3420,7 +3421,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
if (option_auto_para_formatting) if (option_auto_para_formatting)
{ {
format_paragraph (edit, 0); format_paragraph (edit, FALSE);
edit->force |= REDRAW_PAGE; edit->force |= REDRAW_PAGE;
} }
else else
@ -3553,7 +3554,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
edit_double_newline (edit); edit_double_newline (edit);
if (option_return_does_auto_indent) if (option_return_does_auto_indent)
edit_auto_indent (edit); edit_auto_indent (edit);
format_paragraph (edit, 0); format_paragraph (edit, FALSE);
} }
else else
{ {
@ -3682,7 +3683,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
edit_tab_cmd (edit); edit_tab_cmd (edit);
if (option_auto_para_formatting) if (option_auto_para_formatting)
{ {
format_paragraph (edit, 0); format_paragraph (edit, FALSE);
edit->force |= REDRAW_PAGE; edit->force |= REDRAW_PAGE;
} }
else else
@ -3906,7 +3907,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
edit_goto_cmd (edit); edit_goto_cmd (edit);
break; break;
case CK_ParagraphFormat: case CK_ParagraphFormat:
format_paragraph (edit, 1); format_paragraph (edit, TRUE);
edit->force |= REDRAW_PAGE; edit->force |= REDRAW_PAGE;
break; break;
case CK_MacroDelete: case CK_MacroDelete:
@ -4010,7 +4011,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
case CK_DeleteToWordEnd: case CK_DeleteToWordEnd:
case CK_DeleteToHome: case CK_DeleteToHome:
case CK_DeleteToEnd: case CK_DeleteToEnd:
format_paragraph (edit, 0); format_paragraph (edit, FALSE);
edit->force |= REDRAW_PAGE; edit->force |= REDRAW_PAGE;
} }
} }

Просмотреть файл

@ -71,7 +71,7 @@
/*** file scope functions ************************************************************************/ /*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static long static off_t
line_start (WEdit * edit, long line) line_start (WEdit * edit, long line)
{ {
off_t p; off_t p;
@ -86,36 +86,33 @@ line_start (WEdit * edit, long line)
p = edit_move_forward (edit, p, line - l, 0); p = edit_move_forward (edit, p, line - l, 0);
p = edit_bol (edit, p); p = edit_bol (edit, p);
while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p))) while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
p++; p++;
return p; return p;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static int static gboolean
bad_line_start (WEdit * edit, off_t p) bad_line_start (WEdit * edit, off_t p)
{ {
int c; int c;
c = edit_buffer_get_byte (&edit->buffer, p); c = edit_buffer_get_byte (&edit->buffer, p);
if (c == '.') if (c == '.')
{ /* `...' is acceptable */ {
if (edit_buffer_get_byte (&edit->buffer, p + 1) == '.' /* `...' is acceptable */
&& edit_buffer_get_byte (&edit->buffer, p + 2) == '.') return !(edit_buffer_get_byte (&edit->buffer, p + 1) == '.'
return 0; && edit_buffer_get_byte (&edit->buffer, p + 2) == '.');
return 1;
} }
if (c == '-') if (c == '-')
{ {
if (edit_buffer_get_byte (&edit->buffer, p + 1) == '-' /* `---' is acceptable */
&& edit_buffer_get_byte (&edit->buffer, p + 2) == '-') return !(edit_buffer_get_byte (&edit->buffer, p + 1) == '-'
return 0; /* `---' is acceptable */ && edit_buffer_get_byte (&edit->buffer, p + 2) == '-');
return 1;
} }
if (strchr (NO_FORMAT_CHARS_START, c))
return 1; return (strchr (NO_FORMAT_CHARS_START, c) != NULL);
return 0;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -124,26 +121,19 @@ bad_line_start (WEdit * edit, off_t p)
* Return position in the file. * Return position in the file.
*/ */
static long static off_t
begin_paragraph (WEdit * edit, int force) begin_paragraph (WEdit * edit, gboolean force)
{ {
long i; long i;
for (i = edit->curs_line - 1; i >= 0; i--) for (i = edit->curs_line - 1; i >= 0; i--)
{ if (edit_line_is_blank (edit, i) ||
if (edit_line_is_blank (edit, i)) (force && bad_line_start (edit, line_start (edit, i))))
{ {
i++; i++;
break; break;
} }
if (force)
{
if (bad_line_start (edit, line_start (edit, i)))
{
i++;
break;
}
}
}
return edit_move_backward (edit, edit_bol (edit, edit->buffer.curs1), edit->curs_line - i); return edit_move_backward (edit, edit_bol (edit, edit->buffer.curs1), edit->curs_line - i);
} }
@ -153,24 +143,19 @@ begin_paragraph (WEdit * edit, int force)
* Return position in the file. * Return position in the file.
*/ */
static long static off_t
end_paragraph (WEdit * edit, int force) end_paragraph (WEdit * edit, gboolean force)
{ {
long i; long i;
for (i = edit->curs_line + 1; i <= edit->total_lines; i++) for (i = edit->curs_line + 1; i <= edit->total_lines; i++)
{ if (edit_line_is_blank (edit, i) ||
if (edit_line_is_blank (edit, i)) (force && bad_line_start (edit, line_start (edit, i))))
{ {
i--; i--;
break; break;
} }
if (force)
if (bad_line_start (edit, line_start (edit, i)))
{
i--;
break;
}
}
return edit_eol (edit, return edit_eol (edit,
edit_move_forward (edit, edit_bol (edit, edit->buffer.curs1), edit_move_forward (edit, edit_bol (edit, edit->buffer.curs1),
i - edit->curs_line, 0)); i - edit->curs_line, 0));
@ -179,9 +164,10 @@ end_paragraph (WEdit * edit, int force)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static unsigned char * static unsigned char *
get_paragraph (WEdit * edit, off_t p, off_t q, int indent, int *size) get_paragraph (WEdit * edit, off_t p, off_t q, gboolean indent, off_t * size)
{ {
unsigned char *s, *t; unsigned char *s, *t;
#if 0 #if 0
t = g_try_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length + 10); t = g_try_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length + 10);
#else #else
@ -192,11 +178,11 @@ get_paragraph (WEdit * edit, off_t p, off_t q, int indent, int *size)
for (s = t; p < q; p++, s++) for (s = t; p < q; p++, s++)
{ {
if (indent && edit_buffer_get_byte (&edit->buffer, p - 1) == '\n') if (indent && edit_buffer_get_byte (&edit->buffer, p - 1) == '\n')
while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p))) while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
p++; p++;
*s = edit_buffer_get_byte (&edit->buffer, p); *s = edit_buffer_get_byte (&edit->buffer, p);
} }
*size = (unsigned long) (s - t); *size = (off_t) (s - t);
/* FIXME: all variables related to 'size' should be fixed */ /* FIXME: all variables related to 'size' should be fixed */
t[*size] = '\n'; t[*size] = '\n';
return t; return t;
@ -205,15 +191,13 @@ get_paragraph (WEdit * edit, off_t p, off_t q, int indent, int *size)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static inline void static inline void
strip_newlines (unsigned char *t, int size) strip_newlines (unsigned char *t, off_t size)
{ {
unsigned char *p = t; unsigned char *p;
while (size-- != 0)
{ for (p = t; size-- != 0; p++)
if (*p == '\n') if (*p == '\n')
*p = ' '; *p = ' ';
p++;
}
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -221,23 +205,23 @@ strip_newlines (unsigned char *t, int size)
This function calculates the number of chars in a line specified to length l in pixels This function calculates the number of chars in a line specified to length l in pixels
*/ */
static inline int static inline off_t
next_tab_pos (int x) next_tab_pos (off_t x)
{ {
return x += tab_width - x % tab_width; x += tab_width - x % tab_width;
return x;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static inline int static inline off_t
line_pixel_length (unsigned char *t, long b, int l) line_pixel_length (unsigned char *t, off_t b, off_t l)
{ {
int x = 0, c, xn = 0; off_t x = 0, xn = 0;
while (TRUE) while (TRUE)
{ {
c = t[b]; switch (t[b])
switch (c)
{ {
case '\n': case '\n':
return b; return b;
@ -258,11 +242,11 @@ line_pixel_length (unsigned char *t, long b, int l)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static int static off_t
next_word_start (unsigned char *t, int q, int size) next_word_start (unsigned char *t, off_t q, off_t size)
{ {
int i; off_t i;
int saw_ws = 0; gboolean saw_ws = FALSE;
for (i = q; i < size; i++) for (i = q; i < size; i++)
{ {
@ -272,37 +256,37 @@ next_word_start (unsigned char *t, int q, int size)
return -1; return -1;
case '\t': case '\t':
case ' ': case ' ':
saw_ws = 1; saw_ws = TRUE;
break; break;
default: default:
if (saw_ws != 0) if (saw_ws)
return i; return i;
break; break;
} }
} }
return -1; return (-1);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/** find the start of a word */ /** find the start of a word */
static inline int static inline int
word_start (unsigned char *t, int q, int size) word_start (unsigned char *t, off_t q, off_t size)
{ {
int i = q; off_t i;
if (t[q] == ' ' || t[q] == '\t') if (t[q] == ' ' || t[q] == '\t')
return next_word_start (t, q, size); return next_word_start (t, q, size);
while (TRUE) for (i = q;; i--)
{ {
int c; unsigned char c;
if (i == 0) if (i == 0)
return -1; return (-1);
c = t[i - 1]; c = t[i - 1];
if (c == '\n') if (c == '\n')
return -1; return (-1);
if (c == ' ' || c == '\t') if (c == ' ' || c == '\t')
return i; return i;
i--; i--;
@ -313,17 +297,18 @@ word_start (unsigned char *t, int q, int size)
/** replaces ' ' with '\n' to properly format a paragraph */ /** replaces ' ' with '\n' to properly format a paragraph */
static inline void static inline void
format_this (unsigned char *t, int size, int indent) format_this (unsigned char *t, off_t size, long indent)
{ {
int q = 0, ww; off_t q = 0, ww;
strip_newlines (t, size); strip_newlines (t, size);
ww = option_word_wrap_line_length * FONT_MEAN_WIDTH - indent; ww = option_word_wrap_line_length * FONT_MEAN_WIDTH - indent;
if (ww < FONT_MEAN_WIDTH * 2) if (ww < FONT_MEAN_WIDTH * 2)
ww = FONT_MEAN_WIDTH * 2; ww = FONT_MEAN_WIDTH * 2;
while (TRUE) while (TRUE)
{ {
int p; off_t p;
q = line_pixel_length (t, q, ww); q = line_pixel_length (t, q, ww);
if (q > size) if (q > size)
@ -347,7 +332,7 @@ format_this (unsigned char *t, int size, int indent)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static inline void static inline void
replace_at (WEdit * edit, long q, int c) replace_at (WEdit * edit, off_t q, int c)
{ {
edit_cursor_move (edit, q - edit->buffer.curs1); edit_cursor_move (edit, q - edit->buffer.curs1);
edit_delete (edit, TRUE); edit_delete (edit, TRUE);
@ -389,27 +374,29 @@ edit_insert_indent (WEdit * edit, long indent)
/** replaces a block of text */ /** replaces a block of text */
static inline void static inline void
put_paragraph (WEdit * edit, unsigned char *t, off_t p, int indent, int size) put_paragraph (WEdit * edit, unsigned char *t, off_t p, long indent, off_t size)
{ {
long cursor; off_t cursor;
int i, c = 0; off_t i;
int c = '\0';
cursor = edit->buffer.curs1; cursor = edit->buffer.curs1;
if (indent) if (indent != 0)
while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p))) while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
p++; p++;
for (i = 0; i < size; i++, p++) for (i = 0; i < size; i++, p++)
{ {
if (i && indent) if (i != 0 && indent != 0)
{ {
if (t[i - 1] == '\n' && c == '\n') if (t[i - 1] == '\n' && c == '\n')
{ {
while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p))) while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
p++; p++;
} }
else if (t[i - 1] == '\n') else if (t[i - 1] == '\n')
{ {
off_t curs; off_t curs;
edit_cursor_move (edit, p - edit->buffer.curs1); edit_cursor_move (edit, p - edit->buffer.curs1);
curs = edit->buffer.curs1; curs = edit->buffer.curs1;
edit_insert_indent (edit, indent); edit_insert_indent (edit, indent);
@ -420,7 +407,7 @@ put_paragraph (WEdit * edit, unsigned char *t, off_t p, int indent, int size)
else if (c == '\n') else if (c == '\n')
{ {
edit_cursor_move (edit, p - edit->buffer.curs1); edit_cursor_move (edit, p - edit->buffer.curs1);
while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p))) while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
{ {
edit_delete (edit, TRUE); edit_delete (edit, TRUE);
if (cursor > edit->buffer.curs1) if (cursor > edit->buffer.curs1)
@ -429,6 +416,7 @@ put_paragraph (WEdit * edit, unsigned char *t, off_t p, int indent, int size)
p = edit->buffer.curs1; p = edit->buffer.curs1;
} }
} }
c = edit_buffer_get_byte (&edit->buffer, p); c = edit_buffer_get_byte (&edit->buffer, p);
if (c != t[i]) if (c != t[i])
replace_at (edit, p, t[i]); replace_at (edit, p, t[i]);
@ -438,10 +426,10 @@ put_paragraph (WEdit * edit, unsigned char *t, off_t p, int indent, int size)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static inline int static inline long
test_indent (const WEdit * edit, off_t p, off_t q) test_indent (const WEdit * edit, off_t p, off_t q)
{ {
int indent; long indent;
indent = edit_indent_width (edit, p++); indent = edit_indent_width (edit, p++);
if (indent == 0) if (indent == 0)
@ -459,42 +447,44 @@ test_indent (const WEdit * edit, off_t p, off_t q)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
void void
format_paragraph (WEdit * edit, int force) format_paragraph (WEdit * edit, gboolean force)
{ {
long p, q; off_t p, q;
int size; off_t size;
unsigned char *t; unsigned char *t;
int indent = 0; long indent;
if (option_word_wrap_line_length < 2) if (option_word_wrap_line_length < 2)
return; return;
if (edit_line_is_blank (edit, edit->curs_line)) if (edit_line_is_blank (edit, edit->curs_line))
return; return;
p = begin_paragraph (edit, force); p = begin_paragraph (edit, force);
q = end_paragraph (edit, force); q = end_paragraph (edit, force);
indent = test_indent (edit, p, q); indent = test_indent (edit, p, q);
t = get_paragraph (edit, p, q, indent, &size);
if (!t) t = get_paragraph (edit, p, q, indent != 0, &size);
if (t == NULL)
return; return;
if (!force) if (!force)
{ {
int i; off_t i;
if (strchr (NO_FORMAT_CHARS_START, *t))
if (strchr (NO_FORMAT_CHARS_START, *t) != NULL)
{ {
g_free (t); g_free (t);
return; return;
} }
for (i = 0; i < size - 1; i++) for (i = 0; i < size - 1; i++)
{ if (t[i] == '\n' && strchr (NO_FORMAT_CHARS_START "\t ", t[i + 1]) != NULL)
if (t[i] == '\n')
{ {
if (strchr (NO_FORMAT_CHARS_START "\t ", t[i + 1])) g_free (t);
{ return;
g_free (t);
return;
}
} }
}
} }
format_this (t, q - p, indent); format_this (t, q - p, indent);
put_paragraph (edit, t, p, indent, size); put_paragraph (edit, t, p, indent, size);
g_free (t); g_free (t);