(edit_indent_width, edit_insert_indent): move to wordproc.c and make static.
[midnight-commander.git] / src / editor / edit.c
blobc0da1506cb3c353134e1b87b383e7bfa3ca436fe
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012, 2013
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/learn.h" /* learn_keys */
68 #include "src/keybind-defaults.h"
70 #include "edit-impl.h"
71 #include "editwidget.h"
72 #ifdef HAVE_ASPELL
73 #include "spell.h"
74 #endif
76 /*** global variables ****************************************************************************/
78 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
79 int option_typewriter_wrap = 0;
80 int option_auto_para_formatting = 0;
81 int option_fill_tabs_with_spaces = 0;
82 int option_return_does_auto_indent = 1;
83 int option_backspace_through_tabs = 0;
84 int option_fake_half_tabs = 1;
85 int option_save_mode = EDIT_QUICK_SAVE;
86 int option_save_position = 1;
87 int option_max_undo = 32768;
88 int option_persistent_selections = 1;
89 int option_cursor_beyond_eol = 0;
90 int option_line_state = 0;
91 int option_line_state_width = 0;
92 gboolean option_cursor_after_inserted_block = FALSE;
94 int option_edit_right_extreme = 0;
95 int option_edit_left_extreme = 0;
96 int option_edit_top_extreme = 0;
97 int option_edit_bottom_extreme = 0;
98 int enable_show_tabs_tws = 1;
99 int option_check_nl_at_eof = 0;
100 int option_group_undo = 0;
101 int show_right_margin = 0;
103 char *option_backup_ext = NULL;
105 unsigned int edit_stack_iterator = 0;
106 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
107 /* magic sequense for say than block is vertical */
108 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
110 /*** file scope macro definitions ****************************************************************/
112 #define TEMP_BUF_LEN 1024
114 #define space_width 1
116 /*** file scope type declarations ****************************************************************/
118 /*** file scope variables ************************************************************************/
120 /* detecting an error on save is easy: just check if every byte has been written. */
121 /* detecting an error on read, is not so easy 'cos there is not way to tell
122 whether you read everything or not. */
123 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
124 static const struct edit_filters
126 const char *read, *write, *extension;
127 } all_filters[] =
129 /* *INDENT-OFF* */
130 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
131 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
132 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
135 /* *INDENT-ON* */
138 /* --------------------------------------------------------------------------------------------- */
139 /*** file scope functions ************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
142 * Load file OR text into buffers. Set cursor to the beginning of file.
144 * @return FALSE on error.
147 static gboolean
148 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
150 int file;
151 gboolean ret;
153 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
154 if (file < 0)
156 gchar *errmsg;
158 errmsg =
159 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
160 edit_error_dialog (_("Error"), errmsg);
161 g_free (errmsg);
162 return FALSE;
165 ret = (edit_buffer_read_file (&edit->buffer, file, edit->last_byte) == edit->last_byte);
166 if (ret)
167 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
168 else
170 gchar *errmsg;
172 errmsg = g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath));
173 edit_error_dialog (_("Error"), errmsg);
174 g_free (errmsg);
177 mc_close (file);
178 return ret;
181 /* --------------------------------------------------------------------------------------------- */
182 /** Return index of the filter or -1 is there is no appropriate filter */
184 static int
185 edit_find_filter (const vfs_path_t * filename_vpath)
187 size_t i, l, e;
189 if (filename_vpath == NULL)
190 return -1;
192 l = strlen (vfs_path_as_str (filename_vpath));
193 for (i = 0; i < G_N_ELEMENTS (all_filters); i++)
195 e = strlen (all_filters[i].extension);
196 if (l > e)
197 if (!strcmp (all_filters[i].extension, vfs_path_as_str (filename_vpath) + l - e))
198 return i;
200 return -1;
203 /* --------------------------------------------------------------------------------------------- */
205 static char *
206 edit_get_filter (const vfs_path_t * filename_vpath)
208 int i;
209 char *p, *quoted_name;
211 i = edit_find_filter (filename_vpath);
212 if (i < 0)
213 return NULL;
215 quoted_name = name_quote (vfs_path_as_str (filename_vpath), 0);
216 p = g_strdup_printf (all_filters[i].read, quoted_name);
217 g_free (quoted_name);
218 return p;
221 /* --------------------------------------------------------------------------------------------- */
223 static off_t
224 edit_insert_stream (WEdit * edit, FILE * f)
226 int c;
227 off_t i = 0;
228 while ((c = fgetc (f)) >= 0)
230 edit_insert (edit, c);
231 i++;
233 return i;
236 /* --------------------------------------------------------------------------------------------- */
238 * Open file and create it if necessary.
240 * @param edit editor object
241 * @param filename_vpath file name
242 * @param st buffer for store stat info
243 * @return TRUE for success, FALSE for error.
246 static gboolean
247 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
249 int file;
250 gchar *errmsg = NULL;
252 /* Try opening an existing file */
253 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
254 if (file < 0)
257 * Try creating the file. O_EXCL prevents following broken links
258 * and opening existing files.
260 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
261 if (file < 0)
263 errmsg =
264 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
265 goto cleanup;
268 /* New file, delete it if it's not modified or saved */
269 edit->delete_file = 1;
272 /* Check what we have opened */
273 if (mc_fstat (file, st) < 0)
275 errmsg =
276 g_strdup_printf (_("Cannot get size/permissions for %s"),
277 vfs_path_as_str (filename_vpath));
278 goto cleanup;
281 /* We want to open regular files only */
282 if (!S_ISREG (st->st_mode))
284 errmsg =
285 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath));
286 goto cleanup;
290 * Don't delete non-empty files.
291 * O_EXCL should prevent it, but let's be on the safe side.
293 if (st->st_size > 0)
294 edit->delete_file = 0;
296 if (st->st_size >= SIZE_LIMIT)
297 errmsg = g_strdup_printf (_("File \"%s\" is too large"), vfs_path_as_str (filename_vpath));
299 cleanup:
300 (void) mc_close (file);
302 if (errmsg != NULL)
304 edit_error_dialog (_("Error"), errmsg);
305 g_free (errmsg);
306 return FALSE;
308 return TRUE;
311 /* --------------------------------------------------------------------------------------------- */
314 * Open the file and load it into the buffers, either directly or using
315 * a filter. Return TRUE on success, FALSE on error.
317 * Fast loading (edit_load_file_fast) is used when the file size is
318 * known. In this case the data is read into the buffers by blocks.
319 * If the file size is not known, the data is loaded byte by byte in
320 * edit_insert_file.
322 * @param edit editor object
323 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
325 static gboolean
326 edit_load_file (WEdit * edit)
328 gboolean fast_load = TRUE;
330 /* Cannot do fast load if a filter is used */
331 if (edit_find_filter (edit->filename_vpath) >= 0)
332 fast_load = FALSE;
335 * FIXME: line end translation should disable fast loading as well
336 * Consider doing fseek() to the end and ftell() for the real size.
338 if (edit->filename_vpath != NULL)
341 * VFS may report file size incorrectly, and slow load is not a big
342 * deal considering overhead in VFS.
344 if (!vfs_file_is_local (edit->filename_vpath))
345 fast_load = FALSE;
347 /* If we are dealing with a real file, check that it exists */
348 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
350 edit_clean (edit);
351 return FALSE;
354 else
356 /* nothing to load */
357 fast_load = FALSE;
360 edit_buffer_init (&edit->buffer);
362 if (fast_load)
364 edit->last_byte = edit->stat1.st_size;
365 if (!edit_load_file_fast (edit, edit->filename_vpath))
367 edit_clean (edit);
368 return FALSE;
371 else
373 edit->last_byte = 0;
374 if (edit->filename_vpath != NULL
375 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
377 edit->undo_stack_disable = 1;
378 if (edit_insert_file (edit, edit->filename_vpath) < 0)
380 edit_clean (edit);
381 return FALSE;
383 edit->undo_stack_disable = 0;
386 edit->lb = LB_ASIS;
387 return TRUE;
390 /* --------------------------------------------------------------------------------------------- */
391 /** Restore saved cursor position in the file */
393 static void
394 edit_load_position (WEdit * edit)
396 long line, column;
397 off_t offset;
399 if (edit->filename_vpath == NULL
400 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
401 return;
403 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
405 if (line > 0)
407 edit_move_to_line (edit, line - 1);
408 edit->prev_col = column;
410 else if (offset > 0)
412 edit_cursor_move (edit, offset);
413 line = edit->curs_line;
414 edit->search_start = edit->buffer.curs1;
417 book_mark_restore (edit, BOOK_MARK_COLOR);
419 edit_move_to_prev_col (edit, edit_bol (edit, edit->buffer.curs1));
420 edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
423 /* --------------------------------------------------------------------------------------------- */
424 /** Save cursor position in the file */
426 static void
427 edit_save_position (WEdit * edit)
429 if (edit->filename_vpath == NULL
430 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
431 return;
433 book_mark_serialize (edit, BOOK_MARK_COLOR);
434 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->buffer.curs1,
435 edit->serialized_bookmarks);
436 edit->serialized_bookmarks = NULL;
439 /* --------------------------------------------------------------------------------------------- */
440 /** Clean the WEdit stricture except the widget part */
442 static void
443 edit_purge_widget (WEdit * edit)
445 size_t len = sizeof (WEdit) - sizeof (Widget);
446 char *start = (char *) edit + sizeof (Widget);
447 memset (start, 0, len);
450 /* --------------------------------------------------------------------------------------------- */
453 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
454 then the file should be as it was when he loaded up. Then set edit->modified to 0.
457 static long
458 edit_pop_undo_action (WEdit * edit)
460 long c;
461 unsigned long sp = edit->undo_stack_pointer;
463 if (sp == edit->undo_stack_bottom)
464 return STACK_BOTTOM;
466 sp = (sp - 1) & edit->undo_stack_size_mask;
467 c = edit->undo_stack[sp];
468 if (c >= 0)
470 /* edit->undo_stack[sp] = '@'; */
471 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
472 return c;
475 if (sp == edit->undo_stack_bottom)
476 return STACK_BOTTOM;
478 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
479 if (edit->undo_stack[sp] == -2)
481 /* edit->undo_stack[sp] = '@'; */
482 edit->undo_stack_pointer = sp;
484 else
485 edit->undo_stack[sp]++;
487 return c;
490 static long
491 edit_pop_redo_action (WEdit * edit)
493 long c;
494 unsigned long sp = edit->redo_stack_pointer;
496 if (sp == edit->redo_stack_bottom)
497 return STACK_BOTTOM;
499 sp = (sp - 1) & edit->redo_stack_size_mask;
500 c = edit->redo_stack[sp];
501 if (c >= 0)
503 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
504 return c;
507 if (sp == edit->redo_stack_bottom)
508 return STACK_BOTTOM;
510 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
511 if (edit->redo_stack[sp] == -2)
512 edit->redo_stack_pointer = sp;
513 else
514 edit->redo_stack[sp]++;
516 return c;
519 static long
520 get_prev_undo_action (WEdit * edit)
522 long c;
523 unsigned long sp = edit->undo_stack_pointer;
525 if (sp == edit->undo_stack_bottom)
526 return STACK_BOTTOM;
528 sp = (sp - 1) & edit->undo_stack_size_mask;
529 c = edit->undo_stack[sp];
530 if (c >= 0)
531 return c;
533 if (sp == edit->undo_stack_bottom)
534 return STACK_BOTTOM;
536 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
537 return c;
540 /* --------------------------------------------------------------------------------------------- */
541 /** is called whenever a modification is made by one of the four routines below */
543 static void
544 edit_modification (WEdit * edit)
546 edit->caches_valid = FALSE;
548 /* raise lock when file modified */
549 if (!edit->modified && !edit->delete_file)
550 edit->locked = lock_file (edit->filename_vpath);
551 edit->modified = 1;
554 /* --------------------------------------------------------------------------------------------- */
555 /* high level cursor movement commands */
556 /* --------------------------------------------------------------------------------------------- */
557 /** check whether cursor is in indent part of line
559 * @param edit editor object
561 * @return TRUE if cursor is in indent, FALSE otherwise
564 static gboolean
565 is_in_indent (const WEdit * edit)
567 off_t p;
569 for (p = edit_bol (edit, edit->buffer.curs1); p < edit->buffer.curs1; p++)
570 if (strchr (" \t", edit_buffer_get_byte (&edit->buffer, p)) == NULL)
571 return FALSE;
573 return TRUE;
576 /* --------------------------------------------------------------------------------------------- */
577 /** check whether line in editor is blank or not
579 * @param edit editor object
580 * @param offset position in file
582 * @return TRUE if line in blank, FALSE otherwise
585 static gboolean
586 is_blank (const WEdit * edit, off_t offset)
588 off_t s, f;
589 int c;
591 s = edit_bol (edit, offset);
592 f = edit_eol (edit, offset) - 1;
593 while (s <= f)
595 c = edit_buffer_get_byte (&edit->buffer, s++);
596 if (!isspace (c))
597 return FALSE;
599 return TRUE;
602 /* --------------------------------------------------------------------------------------------- */
603 /** returns the offset of line i */
605 static off_t
606 edit_find_line (WEdit * edit, long line)
608 long i, j = 0;
609 long m = 2000000000; /* what is the magic number? */
611 if (!edit->caches_valid)
613 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
614 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
615 /* three offsets that we *know* are line 0 at 0 and these two: */
616 edit->line_numbers[1] = edit->curs_line;
617 edit->line_offsets[1] = edit_bol (edit, edit->buffer.curs1);
618 edit->line_numbers[2] = edit->total_lines;
619 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
620 edit->caches_valid = TRUE;
622 if (line >= edit->total_lines)
623 return edit->line_offsets[2];
624 if (line <= 0)
625 return 0;
626 /* find the closest known point */
627 for (i = 0; i < N_LINE_CACHES; i++)
629 long n;
631 n = abs (edit->line_numbers[i] - line);
632 if (n < m)
634 m = n;
635 j = i;
638 if (m == 0)
639 return edit->line_offsets[j]; /* know the offset exactly */
640 if (m == 1 && j >= 3)
641 i = j; /* one line different - caller might be looping, so stay in this cache */
642 else
643 i = 3 + (rand () % (N_LINE_CACHES - 3));
644 if (line > edit->line_numbers[j])
645 edit->line_offsets[i] =
646 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
647 else
648 edit->line_offsets[i] =
649 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
650 edit->line_numbers[i] = line;
651 return edit->line_offsets[i];
654 /* --------------------------------------------------------------------------------------------- */
655 /** moves up until a blank line is reached, or until just
656 before a non-blank line is reached */
658 static void
659 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
661 long i = 0;
663 if (edit->curs_line > 1)
665 if (!edit_line_is_blank (edit, edit->curs_line))
667 for (i = edit->curs_line - 1; i != 0; i--)
668 if (edit_line_is_blank (edit, i))
669 break;
671 else if (edit_line_is_blank (edit, edit->curs_line - 1))
673 for (i = edit->curs_line - 1; i != 0; i--)
674 if (!edit_line_is_blank (edit, i))
676 i++;
677 break;
680 else
682 for (i = edit->curs_line - 1; i != 0; i--)
683 if (edit_line_is_blank (edit, i))
684 break;
688 edit_move_up (edit, edit->curs_line - i, do_scroll);
691 /* --------------------------------------------------------------------------------------------- */
692 /** moves down until a blank line is reached, or until just
693 before a non-blank line is reached */
695 static void
696 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
698 long i;
700 if (edit->curs_line >= edit->total_lines - 1)
701 i = edit->total_lines;
702 else if (!edit_line_is_blank (edit, edit->curs_line))
704 for (i = edit->curs_line + 1; i != 0; i++)
705 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
706 break;
708 else if (edit_line_is_blank (edit, edit->curs_line + 1))
710 for (i = edit->curs_line + 1; i != 0; i++)
711 if (!edit_line_is_blank (edit, i) || i > edit->total_lines)
713 i--;
714 break;
717 else
719 for (i = edit->curs_line + 1; i != 0; i++)
720 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
721 break;
723 edit_move_down (edit, i - edit->curs_line, do_scroll);
726 /* --------------------------------------------------------------------------------------------- */
728 static void
729 edit_begin_page (WEdit * edit)
731 edit_update_curs_row (edit);
732 edit_move_up (edit, edit->curs_row, 0);
735 /* --------------------------------------------------------------------------------------------- */
737 static void
738 edit_end_page (WEdit * edit)
740 edit_update_curs_row (edit);
741 edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, 0);
745 /* --------------------------------------------------------------------------------------------- */
746 /** goto beginning of text */
748 static void
749 edit_move_to_top (WEdit * edit)
751 if (edit->curs_line)
753 edit_cursor_move (edit, -edit->buffer.curs1);
754 edit_move_to_prev_col (edit, 0);
755 edit->force |= REDRAW_PAGE;
756 edit->search_start = 0;
757 edit_update_curs_row (edit);
762 /* --------------------------------------------------------------------------------------------- */
763 /** goto end of text */
765 static void
766 edit_move_to_bottom (WEdit * edit)
768 if (edit->curs_line < edit->total_lines)
770 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
771 edit->start_display = edit->last_byte;
772 edit->start_line = edit->total_lines;
773 edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
774 edit->force |= REDRAW_PAGE;
778 /* --------------------------------------------------------------------------------------------- */
779 /** goto beginning of line */
781 static void
782 edit_cursor_to_bol (WEdit * edit)
784 edit_cursor_move (edit, edit_bol (edit, edit->buffer.curs1) - edit->buffer.curs1);
785 edit->search_start = edit->buffer.curs1;
786 edit->prev_col = edit_get_col (edit);
787 edit->over_col = 0;
790 /* --------------------------------------------------------------------------------------------- */
791 /** goto end of line */
793 static void
794 edit_cursor_to_eol (WEdit * edit)
796 edit_cursor_move (edit, edit_eol (edit, edit->buffer.curs1) - edit->buffer.curs1);
797 edit->search_start = edit->buffer.curs1;
798 edit->prev_col = edit_get_col (edit);
799 edit->over_col = 0;
802 /* --------------------------------------------------------------------------------------------- */
804 static unsigned long
805 my_type_of (int c)
807 int x, r = 0;
808 const char *p, *q;
809 const char option_chars_move_whole_word[] =
810 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
812 if (c == 0)
813 return 0;
814 if (c == '!')
816 if (*option_chars_move_whole_word == '!')
817 return 2;
818 return 0x80000000UL;
820 if (g_ascii_isupper ((gchar) c))
821 c = 'A';
822 else if (g_ascii_islower ((gchar) c))
823 c = 'a';
824 else if (g_ascii_isalpha (c))
825 c = 'a';
826 else if (isdigit (c))
827 c = '0';
828 else if (isspace (c))
829 c = ' ';
830 q = strchr (option_chars_move_whole_word, c);
831 if (!q)
832 return 0xFFFFFFFFUL;
835 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
836 if (*p == '!')
837 x <<= 1;
838 r |= x;
840 while ((q = strchr (q + 1, c)));
841 return r;
844 /* --------------------------------------------------------------------------------------------- */
846 static void
847 edit_left_word_move (WEdit * edit, int s)
849 while (TRUE)
851 int c1, c2;
853 if (edit->column_highlight
854 && edit->mark1 != edit->mark2
855 && edit->over_col == 0 && edit->buffer.curs1 == edit_bol (edit, edit->buffer.curs1))
856 break;
857 edit_cursor_move (edit, -1);
858 if (edit->buffer.curs1 == 0)
859 break;
860 c1 = edit_buffer_get_previous_byte (&edit->buffer);
861 c2 = edit_buffer_get_current_byte (&edit->buffer);
862 if (c1 == '\n' || c2 == '\n')
863 break;
864 if ((my_type_of (c1) & my_type_of (c2)) == 0)
865 break;
866 if (isspace (c1) && !isspace (c2))
867 break;
868 if (s != 0 && !isspace (c1) && isspace (c2))
869 break;
873 /* --------------------------------------------------------------------------------------------- */
875 static void
876 edit_left_word_move_cmd (WEdit * edit)
878 edit_left_word_move (edit, 0);
879 edit->force |= REDRAW_PAGE;
882 /* --------------------------------------------------------------------------------------------- */
884 static void
885 edit_right_word_move (WEdit * edit, int s)
887 while (TRUE)
889 int c1, c2;
891 if (edit->column_highlight
892 && edit->mark1 != edit->mark2
893 && edit->over_col == 0 && edit->buffer.curs1 == edit_eol (edit, edit->buffer.curs1))
894 break;
895 edit_cursor_move (edit, 1);
896 if (edit->buffer.curs1 >= edit->last_byte)
897 break;
898 c1 = edit_buffer_get_previous_byte (&edit->buffer);
899 c2 = edit_buffer_get_current_byte (&edit->buffer);
900 if (c1 == '\n' || c2 == '\n')
901 break;
902 if ((my_type_of (c1) & my_type_of (c2)) == 0)
903 break;
904 if (isspace (c1) && !isspace (c2))
905 break;
906 if (s != 0 && !isspace (c1) && isspace (c2))
907 break;
911 /* --------------------------------------------------------------------------------------------- */
913 static void
914 edit_right_word_move_cmd (WEdit * edit)
916 edit_right_word_move (edit, 0);
917 edit->force |= REDRAW_PAGE;
920 /* --------------------------------------------------------------------------------------------- */
922 static void
923 edit_right_char_move_cmd (WEdit * edit)
925 int cw = 1;
926 int c;
928 #ifdef HAVE_CHARSET
929 if (edit->utf8)
931 c = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &cw);
932 if (cw < 1)
933 cw = 1;
935 else
936 #endif
937 c = edit_buffer_get_current_byte (&edit->buffer);
939 if (option_cursor_beyond_eol && c == '\n')
940 edit->over_col++;
941 else
942 edit_cursor_move (edit, cw);
945 /* --------------------------------------------------------------------------------------------- */
947 static void
948 edit_left_char_move_cmd (WEdit * edit)
950 int cw = 1;
952 if (edit->column_highlight
953 && option_cursor_beyond_eol
954 && edit->mark1 != edit->mark2
955 && edit->over_col == 0 && edit->buffer.curs1 == edit_bol (edit, edit->buffer.curs1))
956 return;
957 #ifdef HAVE_CHARSET
958 if (edit->utf8)
960 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &cw);
961 if (cw < 1)
962 cw = 1;
964 #endif
965 if (option_cursor_beyond_eol && edit->over_col > 0)
967 edit->over_col--;
969 else
971 edit_cursor_move (edit, -cw);
975 /* --------------------------------------------------------------------------------------------- */
976 /** Up or down cursor moving.
977 direction = TRUE - move up
978 = FALSE - move down
981 static void
982 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
984 long p;
985 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
987 if (lines > l)
988 lines = l;
990 if (lines == 0)
991 return;
993 if (lines > 1)
994 edit->force |= REDRAW_PAGE;
995 if (do_scroll)
997 if (direction)
998 edit_scroll_upward (edit, lines);
999 else
1000 edit_scroll_downward (edit, lines);
1002 p = edit_bol (edit, edit->buffer.curs1);
1004 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1006 edit_cursor_move (edit, p - edit->buffer.curs1);
1008 edit_move_to_prev_col (edit, p);
1010 #ifdef HAVE_CHARSET
1011 /* search start of current multibyte char (like CJK) */
1012 if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->last_byte
1013 && edit_buffer_get_current_byte (&edit->buffer) >= 256)
1015 edit_right_char_move_cmd (edit);
1016 edit_left_char_move_cmd (edit);
1018 #endif
1020 edit->search_start = edit->buffer.curs1;
1021 edit->found_len = 0;
1024 /* --------------------------------------------------------------------------------------------- */
1026 static void
1027 edit_right_delete_word (WEdit * edit)
1029 while (edit->buffer.curs1 < edit->last_byte)
1031 int c1, c2;
1033 c1 = edit_delete (edit, TRUE);
1034 c2 = edit_buffer_get_current_byte (&edit->buffer);
1035 if (c1 == '\n' || c2 == '\n')
1036 break;
1037 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1038 break;
1039 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1040 break;
1044 /* --------------------------------------------------------------------------------------------- */
1046 static void
1047 edit_left_delete_word (WEdit * edit)
1049 while (edit->buffer.curs1 > 0)
1051 int c1, c2;
1053 c1 = edit_backspace (edit, TRUE);
1054 c2 = edit_buffer_get_previous_byte (&edit->buffer);
1055 if (c1 == '\n' || c2 == '\n')
1056 break;
1057 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1058 break;
1059 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1060 break;
1064 /* --------------------------------------------------------------------------------------------- */
1066 the start column position is not recorded, and hence does not
1067 undo as it happed. But who would notice.
1070 static void
1071 edit_do_undo (WEdit * edit)
1073 long ac;
1074 long count = 0;
1076 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1077 edit->over_col = 0;
1078 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1080 switch ((int) ac)
1082 case STACK_BOTTOM:
1083 goto done_undo;
1084 case CURS_RIGHT:
1085 edit_cursor_move (edit, 1);
1086 break;
1087 case CURS_LEFT:
1088 edit_cursor_move (edit, -1);
1089 break;
1090 case BACKSPACE:
1091 case BACKSPACE_BR:
1092 edit_backspace (edit, TRUE);
1093 break;
1094 case DELCHAR:
1095 case DELCHAR_BR:
1096 edit_delete (edit, TRUE);
1097 break;
1098 case COLUMN_ON:
1099 edit->column_highlight = 1;
1100 break;
1101 case COLUMN_OFF:
1102 edit->column_highlight = 0;
1103 break;
1105 if (ac >= 256 && ac < 512)
1106 edit_insert_ahead (edit, ac - 256);
1107 if (ac >= 0 && ac < 256)
1108 edit_insert (edit, ac);
1110 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1112 edit->mark1 = ac - MARK_1;
1113 edit->column1 =
1114 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1116 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1118 edit->mark2 = ac - MARK_2;
1119 edit->column2 =
1120 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1122 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1124 edit->end_mark_curs = ac - MARK_CURS;
1126 if (count++)
1127 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1130 if (edit->start_display > ac - KEY_PRESS)
1132 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1133 edit->force |= REDRAW_PAGE;
1135 else if (edit->start_display < ac - KEY_PRESS)
1137 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1138 edit->force |= REDRAW_PAGE;
1140 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1141 edit_update_curs_row (edit);
1143 done_undo:
1144 edit->undo_stack_disable = 0;
1147 /* --------------------------------------------------------------------------------------------- */
1149 static void
1150 edit_do_redo (WEdit * edit)
1152 long ac;
1153 long count = 0;
1155 if (edit->redo_stack_reset)
1156 return;
1158 edit->over_col = 0;
1159 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1161 switch ((int) ac)
1163 case STACK_BOTTOM:
1164 goto done_redo;
1165 case CURS_RIGHT:
1166 edit_cursor_move (edit, 1);
1167 break;
1168 case CURS_LEFT:
1169 edit_cursor_move (edit, -1);
1170 break;
1171 case BACKSPACE:
1172 edit_backspace (edit, TRUE);
1173 break;
1174 case DELCHAR:
1175 edit_delete (edit, TRUE);
1176 break;
1177 case COLUMN_ON:
1178 edit->column_highlight = 1;
1179 break;
1180 case COLUMN_OFF:
1181 edit->column_highlight = 0;
1182 break;
1184 if (ac >= 256 && ac < 512)
1185 edit_insert_ahead (edit, ac - 256);
1186 if (ac >= 0 && ac < 256)
1187 edit_insert (edit, ac);
1189 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1191 edit->mark1 = ac - MARK_1;
1192 edit->column1 =
1193 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1195 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1197 edit->mark2 = ac - MARK_2;
1198 edit->column2 =
1199 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1201 /* more than one pop usually means something big */
1202 if (count++)
1203 edit->force |= REDRAW_PAGE;
1206 if (edit->start_display > ac - KEY_PRESS)
1208 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1209 edit->force |= REDRAW_PAGE;
1211 else if (edit->start_display < ac - KEY_PRESS)
1213 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1214 edit->force |= REDRAW_PAGE;
1216 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1217 edit_update_curs_row (edit);
1219 done_redo:
1223 /* --------------------------------------------------------------------------------------------- */
1225 static void
1226 edit_group_undo (WEdit * edit)
1228 long ac = KEY_PRESS;
1229 long cur_ac = KEY_PRESS;
1230 while (ac != STACK_BOTTOM && ac == cur_ac)
1232 cur_ac = get_prev_undo_action (edit);
1233 edit_do_undo (edit);
1234 ac = get_prev_undo_action (edit);
1235 /* exit from cycle if option_group_undo is not set,
1236 * and make single UNDO operation
1238 if (!option_group_undo)
1239 ac = STACK_BOTTOM;
1243 /* --------------------------------------------------------------------------------------------- */
1245 static void
1246 edit_delete_to_line_end (WEdit * edit)
1248 while (edit_buffer_get_current_byte (&edit->buffer) != '\n' && edit->buffer.curs2 != 0)
1249 edit_delete (edit, TRUE);
1252 /* --------------------------------------------------------------------------------------------- */
1254 static void
1255 edit_delete_to_line_begin (WEdit * edit)
1257 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 != 0)
1258 edit_backspace (edit, TRUE);
1261 /* --------------------------------------------------------------------------------------------- */
1263 static gboolean
1264 is_aligned_on_a_tab (WEdit * edit)
1266 long curs_col;
1268 edit_update_curs_col (edit);
1269 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1270 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1273 /* --------------------------------------------------------------------------------------------- */
1275 static gboolean
1276 right_of_four_spaces (WEdit * edit)
1278 int i, ch = 0;
1280 for (i = 1; i <= HALF_TAB_SIZE; i++)
1281 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - i);
1283 return (ch == ' ' && is_aligned_on_a_tab (edit));
1286 /* --------------------------------------------------------------------------------------------- */
1288 static gboolean
1289 left_of_four_spaces (WEdit * edit)
1291 int i, ch = 0;
1293 for (i = 0; i < HALF_TAB_SIZE; i++)
1294 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 + i);
1296 return (ch == ' ' && is_aligned_on_a_tab (edit));
1299 /* --------------------------------------------------------------------------------------------- */
1301 static void
1302 edit_auto_indent (WEdit * edit)
1304 off_t p;
1305 char c;
1307 p = edit->buffer.curs1;
1308 /* use the previous line as a template */
1309 p = edit_move_backward (edit, p, 1);
1310 /* copy the leading whitespace of the line */
1311 while (TRUE)
1312 { /* no range check - the line _is_ \n-terminated */
1313 c = edit_buffer_get_byte (&edit->buffer, p++);
1314 if (c != ' ' && c != '\t')
1315 break;
1316 edit_insert (edit, c);
1320 /* --------------------------------------------------------------------------------------------- */
1322 static inline void
1323 edit_double_newline (WEdit * edit)
1325 edit_insert (edit, '\n');
1326 if (edit_buffer_get_current_byte (&edit->buffer) == '\n'
1327 || edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - 2) == '\n')
1328 return;
1329 edit->force |= REDRAW_PAGE;
1330 edit_insert (edit, '\n');
1333 /* --------------------------------------------------------------------------------------------- */
1335 static void
1336 insert_spaces_tab (WEdit * edit, gboolean half)
1338 long i;
1340 edit_update_curs_col (edit);
1341 i = option_tab_spacing * space_width;
1342 if (half)
1343 i /= 2;
1344 if (i != 0)
1346 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1347 while (i > 0)
1349 edit_insert (edit, ' ');
1350 i -= space_width;
1355 /* --------------------------------------------------------------------------------------------- */
1357 static inline void
1358 edit_tab_cmd (WEdit * edit)
1360 if (option_fake_half_tabs && is_in_indent (edit))
1362 /* insert a half tab (usually four spaces) unless there is a
1363 half tab already behind, then delete it and insert a
1364 full tab. */
1365 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1366 insert_spaces_tab (edit, TRUE);
1367 else
1369 int i;
1371 for (i = 1; i <= HALF_TAB_SIZE; i++)
1372 edit_backspace (edit, TRUE);
1373 edit_insert (edit, '\t');
1376 else if (option_fill_tabs_with_spaces)
1377 insert_spaces_tab (edit, FALSE);
1378 else
1379 edit_insert (edit, '\t');
1382 /* --------------------------------------------------------------------------------------------- */
1384 static void
1385 check_and_wrap_line (WEdit * edit)
1387 off_t curs;
1388 int c;
1390 if (!option_typewriter_wrap)
1391 return;
1392 edit_update_curs_col (edit);
1393 if (edit->curs_col < option_word_wrap_line_length)
1394 return;
1395 curs = edit->buffer.curs1;
1396 while (TRUE)
1398 curs--;
1399 c = edit_buffer_get_byte (&edit->buffer, curs);
1400 if (c == '\n' || curs <= 0)
1402 edit_insert (edit, '\n');
1403 return;
1405 if (c == ' ' || c == '\t')
1407 off_t current = edit->buffer.curs1;
1408 edit_cursor_move (edit, curs - edit->buffer.curs1 + 1);
1409 edit_insert (edit, '\n');
1410 edit_cursor_move (edit, current - edit->buffer.curs1 + 1);
1411 return;
1416 /* --------------------------------------------------------------------------------------------- */
1417 /** this find the matching bracket in either direction, and sets edit->bracket
1419 * @param edit editor object
1420 * @param in_screen seach only on the current screen
1421 * @param furthest_bracket_search count of the bytes for search
1423 * @return position of the found bracket (-1 if no match)
1426 static off_t
1427 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1429 const char *const b = "{}{[][()(", *p;
1430 int i = 1, a, inc = -1, c, d, n = 0;
1431 unsigned long j = 0;
1432 off_t q;
1434 edit_update_curs_row (edit);
1435 c = edit_buffer_get_current_byte (&edit->buffer);
1436 p = strchr (b, c);
1437 /* no limit */
1438 if (!furthest_bracket_search)
1439 furthest_bracket_search--;
1440 /* not on a bracket at all */
1441 if (p == NULL)
1442 return -1;
1443 /* the matching bracket */
1444 d = p[1];
1445 /* going left or right? */
1446 if (strchr ("{[(", c))
1447 inc = 1;
1448 for (q = edit->buffer.curs1 + inc;; q += inc)
1450 /* out of buffer? */
1451 if (q >= edit->last_byte || q < 0)
1452 break;
1453 a = edit_buffer_get_byte (&edit->buffer, q);
1454 /* don't want to eat CPU */
1455 if (j++ > furthest_bracket_search)
1456 break;
1457 /* out of screen? */
1458 if (in_screen)
1460 if (q < edit->start_display)
1461 break;
1462 /* count lines if searching downward */
1463 if (inc > 0 && a == '\n')
1464 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1465 break;
1467 /* count bracket depth */
1468 i += (a == c) - (a == d);
1469 /* return if bracket depth is zero */
1470 if (i == 0)
1471 return q;
1473 /* no match */
1474 return -1;
1477 /* --------------------------------------------------------------------------------------------- */
1479 static inline void
1480 edit_goto_matching_bracket (WEdit * edit)
1482 off_t q;
1484 q = edit_get_bracket (edit, 0, 0);
1485 if (q >= 0)
1487 edit->bracket = edit->buffer.curs1;
1488 edit->force |= REDRAW_PAGE;
1489 edit_cursor_move (edit, q - edit->buffer.curs1);
1493 /* --------------------------------------------------------------------------------------------- */
1495 static void
1496 edit_move_block_to_right (WEdit * edit)
1498 off_t start_mark, end_mark;
1499 long cur_bol, start_bol;
1501 if (eval_marks (edit, &start_mark, &end_mark))
1502 return;
1504 start_bol = edit_bol (edit, start_mark);
1505 cur_bol = edit_bol (edit, end_mark - 1);
1509 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1510 if (!edit_line_is_blank (edit, edit->curs_line))
1512 if (option_fill_tabs_with_spaces)
1513 insert_spaces_tab (edit, option_fake_half_tabs);
1514 else
1515 edit_insert (edit, '\t');
1516 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->buffer.curs1);
1519 if (cur_bol == 0)
1520 break;
1522 cur_bol = edit_bol (edit, cur_bol - 1);
1524 while (cur_bol >= start_bol);
1526 edit->force |= REDRAW_PAGE;
1529 /* --------------------------------------------------------------------------------------------- */
1531 static void
1532 edit_move_block_to_left (WEdit * edit)
1534 off_t start_mark, end_mark;
1535 off_t cur_bol, start_bol;
1536 int i;
1538 if (eval_marks (edit, &start_mark, &end_mark))
1539 return;
1541 start_bol = edit_bol (edit, start_mark);
1542 cur_bol = edit_bol (edit, end_mark - 1);
1546 int del_tab_width;
1547 int next_char;
1549 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1551 if (option_fake_half_tabs)
1552 del_tab_width = HALF_TAB_SIZE;
1553 else
1554 del_tab_width = option_tab_spacing;
1556 next_char = edit_buffer_get_current_byte (&edit->buffer);
1557 if (next_char == '\t')
1558 edit_delete (edit, TRUE);
1559 else if (next_char == ' ')
1560 for (i = 1; i <= del_tab_width; i++)
1562 if (next_char == ' ')
1563 edit_delete (edit, TRUE);
1564 next_char = edit_buffer_get_current_byte (&edit->buffer);
1567 if (cur_bol == 0)
1568 break;
1570 cur_bol = edit_bol (edit, cur_bol - 1);
1572 while (cur_bol >= start_bol);
1574 edit->force |= REDRAW_PAGE;
1577 /* --------------------------------------------------------------------------------------------- */
1579 * prints at the cursor
1580 * @return number of chars printed
1583 static size_t
1584 edit_print_string (WEdit * e, const char *s)
1586 size_t i = 0;
1588 while (s[i] != '\0')
1589 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1590 e->force |= REDRAW_COMPLETELY;
1591 edit_update_screen (e);
1592 return i;
1595 /* --------------------------------------------------------------------------------------------- */
1597 static off_t
1598 edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * end_pos,
1599 long *col1, long *col2)
1601 off_t cursor;
1602 int col;
1603 off_t blocklen = -1, width = 0;
1604 unsigned char *data;
1606 cursor = edit->buffer.curs1;
1607 col = edit_get_col (edit);
1608 data = g_malloc0 (TEMP_BUF_LEN);
1610 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1612 off_t i;
1613 char *pn;
1615 pn = strchr ((char *) data, '\n');
1616 width = pn == NULL ? blocklen : pn - (char *) data;
1618 for (i = 0; i < blocklen; i++)
1620 if (data[i] != '\n')
1621 edit_insert (edit, data[i]);
1622 else
1623 { /* fill in and move to next line */
1624 long l;
1625 off_t p;
1627 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1628 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1629 edit_insert (edit, ' ');
1631 for (p = edit->buffer.curs1;; p++)
1633 if (p == edit->last_byte)
1635 edit_cursor_move (edit, edit->last_byte - edit->buffer.curs1);
1636 edit_insert_ahead (edit, '\n');
1637 p++;
1638 break;
1640 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1642 p++;
1643 break;
1647 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1649 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1650 edit_insert (edit, ' ');
1654 *col1 = col;
1655 *col2 = col + width;
1656 *start_pos = cursor;
1657 *end_pos = edit->buffer.curs1;
1658 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1659 g_free (data);
1661 return blocklen;
1664 /* --------------------------------------------------------------------------------------------- */
1665 /*** public functions ****************************************************************************/
1666 /* --------------------------------------------------------------------------------------------- */
1668 /** User edit menu, like user menu (F2) but only in editor. */
1670 void
1671 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1673 char *block_file;
1674 int nomark;
1675 off_t curs;
1676 off_t start_mark, end_mark;
1677 struct stat status;
1678 vfs_path_t *block_file_vpath;
1680 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1681 block_file_vpath = vfs_path_from_str (block_file);
1682 curs = edit->buffer.curs1;
1683 nomark = eval_marks (edit, &start_mark, &end_mark);
1684 if (nomark == 0)
1685 edit_save_block (edit, block_file, start_mark, end_mark);
1687 /* run shell scripts from menu */
1688 if (user_menu_cmd (edit, menu_file, selected_entry)
1689 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1691 int rc = 0;
1692 FILE *fd;
1694 /* i.e. we have marked block */
1695 if (nomark == 0)
1696 rc = edit_block_delete_cmd (edit);
1698 if (rc == 0)
1700 off_t ins_len;
1702 ins_len = edit_insert_file (edit, block_file_vpath);
1703 if (nomark == 0 && ins_len > 0)
1704 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1706 /* truncate block file */
1707 fd = fopen (block_file, "w");
1708 if (fd != NULL)
1709 fclose (fd);
1711 g_free (block_file);
1712 vfs_path_free (block_file_vpath);
1714 edit_cursor_move (edit, curs - edit->buffer.curs1);
1715 edit->force |= REDRAW_PAGE;
1716 widget_redraw (WIDGET (edit));
1719 /* --------------------------------------------------------------------------------------------- */
1721 char *
1722 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1724 int i;
1725 char *p, *writename;
1726 const vfs_path_element_t *path_element;
1728 i = edit_find_filter (filename_vpath);
1729 if (i < 0)
1730 return NULL;
1732 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1733 writename = name_quote (path_element->path, 0);
1734 p = g_strdup_printf (all_filters[i].write, writename);
1735 g_free (writename);
1736 return p;
1739 /* --------------------------------------------------------------------------------------------- */
1741 * @param edit editor object
1742 * @param f value of stream file
1743 * @return the length of the file
1746 off_t
1747 edit_write_stream (WEdit * edit, FILE * f)
1749 long i;
1751 if (edit->lb == LB_ASIS)
1753 for (i = 0; i < edit->last_byte; i++)
1754 if (fputc (edit_buffer_get_byte (&edit->buffer, i), f) < 0)
1755 break;
1756 return i;
1759 /* change line breaks */
1760 for (i = 0; i < edit->last_byte; i++)
1762 unsigned char c;
1764 c = edit_buffer_get_byte (&edit->buffer, i);
1765 if (!(c == '\n' || c == '\r'))
1767 /* not line break */
1768 if (fputc (c, f) < 0)
1769 return i;
1771 else
1772 { /* (c == '\n' || c == '\r') */
1773 unsigned char c1;
1775 c1 = edit_buffer_get_byte (&edit->buffer, i + 1); /* next char */
1777 switch (edit->lb)
1779 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1780 /* put one line break unconditionally */
1781 if (fputc ('\n', f) < 0)
1782 return i;
1784 i++; /* 2 chars are processed */
1786 if (c == '\r' && c1 == '\n')
1787 /* Windows line break; go to the next char */
1788 break;
1790 if (c == '\r' && c1 == '\r')
1792 /* two Macintosh line breaks; put second line break */
1793 if (fputc ('\n', f) < 0)
1794 return i;
1795 break;
1798 if (fputc (c1, f) < 0)
1799 return i;
1800 break;
1802 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1803 /* put one line break unconditionally */
1804 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1805 return i;
1807 if (c == '\r' && c1 == '\n')
1808 /* Windows line break; go to the next char */
1809 i++;
1810 break;
1812 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1813 /* put one line break unconditionally */
1814 if (fputc ('\r', f) < 0)
1815 return i;
1817 i++; /* 2 chars are processed */
1819 if (c == '\r' && c1 == '\n')
1820 /* Windows line break; go to the next char */
1821 break;
1823 if (c == '\n' && c1 == '\n')
1825 /* two Windows line breaks; put second line break */
1826 if (fputc ('\r', f) < 0)
1827 return i;
1828 break;
1831 if (fputc (c1, f) < 0)
1832 return i;
1833 break;
1834 case LB_ASIS: /* default without changes */
1835 break;
1840 return edit->last_byte;
1843 /* --------------------------------------------------------------------------------------------- */
1845 gboolean
1846 is_break_char (char c)
1848 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1851 /* --------------------------------------------------------------------------------------------- */
1853 char *
1854 edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len,
1855 gsize * cut)
1857 off_t word_start;
1858 long cut_len = 0;
1859 GString *match_expr;
1860 int c1, c2;
1862 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
1864 c1 = edit_buffer_get_byte (&edit->buffer, word_start);
1865 c2 = edit_buffer_get_byte (&edit->buffer, word_start - 1);
1867 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
1868 break;
1871 match_expr = g_string_sized_new (16);
1875 c1 = edit_buffer_get_byte (&edit->buffer, word_start + match_expr->len);
1876 c2 = edit_buffer_get_byte (&edit->buffer, word_start + match_expr->len + 1);
1877 g_string_append_c (match_expr, c1);
1879 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
1881 *len = match_expr->len;
1882 *start = word_start;
1883 *cut = cut_len;
1885 return g_string_free (match_expr, FALSE);
1888 /* --------------------------------------------------------------------------------------------- */
1889 /** inserts a file at the cursor, returns count of inserted bytes on success */
1891 long
1892 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
1894 char *p;
1895 off_t current;
1896 off_t ins_len = 0;
1898 p = edit_get_filter (filename_vpath);
1899 current = edit->buffer.curs1;
1901 if (p != NULL)
1903 FILE *f;
1905 f = (FILE *) popen (p, "r");
1906 if (f != NULL)
1908 edit_insert_stream (edit, f);
1910 /* Place cursor at the end of text selection */
1911 if (!option_cursor_after_inserted_block)
1913 ins_len = edit->buffer.curs1 - current;
1914 edit_cursor_move (edit, -ins_len);
1916 if (pclose (f) > 0)
1918 char *errmsg;
1920 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
1921 edit_error_dialog (_("Error"), errmsg);
1922 g_free (errmsg);
1923 ins_len = -1;
1926 else
1928 char *errmsg;
1930 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
1931 edit_error_dialog (_("Error"), errmsg);
1932 g_free (errmsg);
1933 ins_len = -1;
1935 g_free (p);
1937 else
1939 int file;
1940 off_t blocklen;
1941 int vertical_insertion = 0;
1942 char *buf;
1944 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
1945 if (file == -1)
1946 return -1;
1948 buf = g_malloc0 (TEMP_BUF_LEN);
1949 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
1950 if (blocklen > 0)
1952 /* if contain signature VERTICAL_MAGIC then it vertical block */
1953 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
1954 vertical_insertion = 1;
1955 else
1956 mc_lseek (file, 0, SEEK_SET);
1959 if (vertical_insertion)
1961 off_t mark1, mark2;
1962 long c1, c2;
1964 blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
1965 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
1967 /* highlight inserted text then not persistent blocks */
1968 if (!option_persistent_selections && edit->modified)
1970 if (!edit->column_highlight)
1971 edit_push_undo_action (edit, COLUMN_OFF);
1972 edit->column_highlight = 1;
1975 else
1977 off_t i;
1979 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
1981 for (i = 0; i < blocklen; i++)
1982 edit_insert (edit, buf[i]);
1984 /* highlight inserted text then not persistent blocks */
1985 if (!option_persistent_selections && edit->modified)
1987 edit_set_markers (edit, edit->buffer.curs1, current, 0, 0);
1988 if (edit->column_highlight)
1989 edit_push_undo_action (edit, COLUMN_ON);
1990 edit->column_highlight = 0;
1993 /* Place cursor at the end of text selection */
1994 if (!option_cursor_after_inserted_block)
1996 ins_len = edit->buffer.curs1 - current;
1997 edit_cursor_move (edit, -ins_len);
2001 edit->force |= REDRAW_PAGE;
2002 g_free (buf);
2003 mc_close (file);
2004 if (blocklen != 0)
2005 ins_len = 0;
2008 return ins_len;
2011 /* --------------------------------------------------------------------------------------------- */
2013 * Fill in the edit structure. Return NULL on failure. Pass edit as
2014 * NULL to allocate a new structure.
2016 * If line is 0, try to restore saved position. Otherwise put the
2017 * cursor on that line and show it in the middle of the screen.
2020 WEdit *
2021 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2022 long line)
2024 gboolean to_free = FALSE;
2026 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2027 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2029 if (edit != NULL)
2031 /* save some widget parameters */
2032 gboolean fullscreen = edit->fullscreen;
2033 int y_prev = edit->y_prev;
2034 int x_prev = edit->x_prev;
2035 int lines_prev = edit->lines_prev;
2036 int cols_prev = edit->cols_prev;
2038 edit_purge_widget (edit);
2040 /* restore saved parameters */
2041 edit->fullscreen = fullscreen;
2042 edit->y_prev = y_prev;
2043 edit->x_prev = x_prev;
2044 edit->lines_prev = lines_prev;
2045 edit->cols_prev = cols_prev;
2047 else
2049 edit = g_malloc0 (sizeof (WEdit));
2050 to_free = TRUE;
2052 widget_init (WIDGET (edit), y, x, lines, cols, NULL, NULL);
2053 edit->fullscreen = TRUE;
2054 edit_save_size (edit);
2057 edit->drag_state = MCEDIT_DRAG_NORMAL;
2059 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2060 edit->stat1.st_uid = getuid ();
2061 edit->stat1.st_gid = getgid ();
2062 edit->stat1.st_mtime = 0;
2064 edit->over_col = 0;
2065 edit->bracket = -1;
2066 edit->last_bracket = -1;
2067 edit->force |= REDRAW_PAGE;
2069 /* set file name before load file */
2070 edit_set_filename (edit, filename_vpath);
2072 edit->undo_stack_size = START_STACK_SIZE;
2073 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2074 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2076 edit->redo_stack_size = START_STACK_SIZE;
2077 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2078 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2080 #ifdef HAVE_CHARSET
2081 edit->utf8 = FALSE;
2082 edit->converter = str_cnv_from_term;
2083 edit_set_codeset (edit);
2084 #endif
2086 if (!edit_load_file (edit))
2088 /* edit_load_file already gives an error message */
2089 if (to_free)
2090 g_free (edit);
2091 return NULL;
2094 edit->loading_done = 1;
2095 edit->modified = 0;
2096 edit->locked = 0;
2097 edit_load_syntax (edit, NULL, NULL);
2098 edit_get_syntax_color (edit, -1);
2100 /* load saved cursor position */
2101 if ((line == 0) && option_save_position)
2102 edit_load_position (edit);
2103 else
2105 if (line <= 0)
2106 line = 1;
2107 edit_move_display (edit, line - 1);
2108 edit_move_to_line (edit, line - 1);
2111 edit_load_macro_cmd (edit);
2113 return edit;
2116 /* --------------------------------------------------------------------------------------------- */
2118 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2119 gboolean
2120 edit_clean (WEdit * edit)
2122 if (edit == NULL)
2123 return FALSE;
2125 /* a stale lock, remove it */
2126 if (edit->locked)
2127 edit->locked = unlock_file (edit->filename_vpath);
2129 /* save cursor position */
2130 if (option_save_position)
2131 edit_save_position (edit);
2132 else if (edit->serialized_bookmarks != NULL)
2133 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2135 /* File specified on the mcedit command line and never saved */
2136 if (edit->delete_file)
2137 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2139 edit_free_syntax_rules (edit);
2140 book_mark_flush (edit, -1);
2142 edit_buffer_clean (&edit->buffer);
2144 g_free (edit->undo_stack);
2145 g_free (edit->redo_stack);
2146 vfs_path_free (edit->filename_vpath);
2147 vfs_path_free (edit->dir_vpath);
2148 mc_search_free (edit->search);
2149 edit->search = NULL;
2151 #ifdef HAVE_CHARSET
2152 if (edit->converter != str_cnv_from_term)
2153 str_close_conv (edit->converter);
2154 #endif
2156 edit_purge_widget (edit);
2158 return TRUE;
2161 /* --------------------------------------------------------------------------------------------- */
2164 * Load a new file into the editor and set line. If it fails, preserve the old file.
2165 * To do it, allocate a new widget, initialize it and, if the new file
2166 * was loaded, copy the data to the old widget.
2168 * @return TRUE on success, FALSE on failure.
2170 gboolean
2171 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2173 Widget *w = WIDGET (edit);
2174 WEdit *e;
2176 e = g_malloc0 (sizeof (WEdit));
2177 *WIDGET (e) = *w;
2178 /* save some widget parameters */
2179 e->fullscreen = edit->fullscreen;
2180 e->y_prev = edit->y_prev;
2181 e->x_prev = edit->x_prev;
2182 e->lines_prev = edit->lines_prev;
2183 e->cols_prev = edit->cols_prev;
2185 if (edit_init (e, w->y, w->x, w->lines, w->cols, filename_vpath, line) == NULL)
2187 g_free (e);
2188 return FALSE;
2191 edit_clean (edit);
2192 memcpy (edit, e, sizeof (WEdit));
2193 g_free (e);
2195 return TRUE;
2198 /* --------------------------------------------------------------------------------------------- */
2200 #ifdef HAVE_CHARSET
2201 void
2202 edit_set_codeset (WEdit * edit)
2204 const char *cp_id;
2206 cp_id =
2207 get_codepage_id (mc_global.source_codepage >=
2208 0 ? mc_global.source_codepage : mc_global.display_codepage);
2210 if (cp_id != NULL)
2212 GIConv conv;
2213 conv = str_crt_conv_from (cp_id);
2214 if (conv != INVALID_CONV)
2216 if (edit->converter != str_cnv_from_term)
2217 str_close_conv (edit->converter);
2218 edit->converter = conv;
2222 if (cp_id != NULL)
2223 edit->utf8 = str_isutf8 (cp_id);
2225 #endif
2228 /* --------------------------------------------------------------------------------------------- */
2231 * Recording stack for undo:
2232 * The following is an implementation of a compressed stack. Identical
2233 * pushes are recorded by a negative prefix indicating the number of times the
2234 * same char was pushed. This saves space for repeated curs-left or curs-right
2235 * delete etc.
2237 * eg:
2239 * pushed: stored:
2242 * b a
2243 * b -3
2244 * b b
2245 * c --> -4
2246 * c c
2247 * c d
2251 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2252 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2253 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2254 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2255 * position.
2257 * The only way the cursor moves or the buffer is changed is through the routines:
2258 * insert, backspace, insert_ahead, delete, and cursor_move.
2259 * These record the reverse undo movements onto the stack each time they are
2260 * called.
2262 * Each key press results in a set of actions (insert; delete ...). So each time
2263 * a key is pressed the current position of start_display is pushed as
2264 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2265 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2266 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2270 * @param edit editor object
2271 * @param c code of the action
2274 void
2275 edit_push_undo_action (WEdit * edit, long c)
2277 unsigned long sp = edit->undo_stack_pointer;
2278 unsigned long spm1;
2279 long *t;
2281 /* first enlarge the stack if necessary */
2282 if (sp > edit->undo_stack_size - 10)
2283 { /* say */
2284 if (option_max_undo < 256)
2285 option_max_undo = 256;
2286 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2288 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2289 if (t)
2291 edit->undo_stack = t;
2292 edit->undo_stack_size <<= 1;
2293 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2297 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2298 if (edit->undo_stack_disable)
2300 edit_push_redo_action (edit, KEY_PRESS);
2301 edit_push_redo_action (edit, c);
2302 return;
2305 if (edit->redo_stack_reset)
2306 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2308 if (edit->undo_stack_bottom != sp
2309 && spm1 != edit->undo_stack_bottom
2310 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2312 int d;
2313 if (edit->undo_stack[spm1] < 0)
2315 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2316 if (d == c && edit->undo_stack[spm1] > -1000000000)
2318 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2319 edit->undo_stack[spm1]--;
2320 return;
2323 else
2325 d = edit->undo_stack[spm1];
2326 if (d == c)
2328 if (c >= KEY_PRESS)
2329 return; /* --> no need to push multiple do-nothings */
2330 edit->undo_stack[sp] = -2;
2331 goto check_bottom;
2335 edit->undo_stack[sp] = c;
2337 check_bottom:
2338 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2340 /* if the sp wraps round and catches the undo_stack_bottom then erase
2341 * the first set of actions on the stack to make space - by moving
2342 * undo_stack_bottom forward one "key press" */
2343 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2344 if ((unsigned long) c == edit->undo_stack_bottom ||
2345 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2348 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2350 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2351 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2353 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2354 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2355 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2357 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2361 void
2362 edit_push_redo_action (WEdit * edit, long c)
2364 unsigned long sp = edit->redo_stack_pointer;
2365 unsigned long spm1;
2366 long *t;
2367 /* first enlarge the stack if necessary */
2368 if (sp > edit->redo_stack_size - 10)
2369 { /* say */
2370 if (option_max_undo < 256)
2371 option_max_undo = 256;
2372 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2374 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2375 if (t)
2377 edit->redo_stack = t;
2378 edit->redo_stack_size <<= 1;
2379 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2383 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2385 if (edit->redo_stack_bottom != sp
2386 && spm1 != edit->redo_stack_bottom
2387 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2389 int d;
2390 if (edit->redo_stack[spm1] < 0)
2392 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2393 if (d == c && edit->redo_stack[spm1] > -1000000000)
2395 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2396 edit->redo_stack[spm1]--;
2397 return;
2400 else
2402 d = edit->redo_stack[spm1];
2403 if (d == c)
2405 if (c >= KEY_PRESS)
2406 return; /* --> no need to push multiple do-nothings */
2407 edit->redo_stack[sp] = -2;
2408 goto redo_check_bottom;
2412 edit->redo_stack[sp] = c;
2414 redo_check_bottom:
2415 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2417 /* if the sp wraps round and catches the redo_stack_bottom then erase
2418 * the first set of actions on the stack to make space - by moving
2419 * redo_stack_bottom forward one "key press" */
2420 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2421 if ((unsigned long) c == edit->redo_stack_bottom ||
2422 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2425 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2427 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2428 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2431 * If a single key produced enough pushes to wrap all the way round then
2432 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2433 * The stack is then initialised:
2436 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2437 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2438 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2441 /* --------------------------------------------------------------------------------------------- */
2443 Basic low level single character buffer alterations and movements at the cursor.
2446 void
2447 edit_insert (WEdit * edit, int c)
2449 /* check if file has grown to large */
2450 if (edit->last_byte >= SIZE_LIMIT)
2451 return;
2453 /* first we must update the position of the display window */
2454 if (edit->buffer.curs1 < edit->start_display)
2456 edit->start_display++;
2457 if (c == '\n')
2458 edit->start_line++;
2461 /* Mark file as modified, unless the file hasn't been fully loaded */
2462 if (edit->loading_done)
2463 edit_modification (edit);
2465 /* now we must update some info on the file and check if a redraw is required */
2466 if (c == '\n')
2468 book_mark_inc (edit, edit->curs_line);
2469 edit->curs_line++;
2470 edit->total_lines++;
2471 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2474 /* save the reverse command onto the undo stack */
2475 /* ordinary char and not space */
2476 if (c > 32)
2477 edit_push_undo_action (edit, BACKSPACE);
2478 else
2479 edit_push_undo_action (edit, BACKSPACE_BR);
2480 /* update markers */
2481 edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2482 edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2483 edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2485 edit_buffer_insert (&edit->buffer, c);
2487 /* update file length */
2488 edit->last_byte++;
2491 /* --------------------------------------------------------------------------------------------- */
2492 /** same as edit_insert and move left */
2494 void
2495 edit_insert_ahead (WEdit * edit, int c)
2497 if (edit->last_byte >= SIZE_LIMIT)
2498 return;
2500 if (edit->buffer.curs1 < edit->start_display)
2502 edit->start_display++;
2503 if (c == '\n')
2504 edit->start_line++;
2506 edit_modification (edit);
2507 if (c == '\n')
2509 book_mark_inc (edit, edit->curs_line);
2510 edit->total_lines++;
2511 edit->force |= REDRAW_AFTER_CURSOR;
2513 /* ordinary char and not space */
2514 if (c > 32)
2515 edit_push_undo_action (edit, DELCHAR);
2516 else
2517 edit_push_undo_action (edit, DELCHAR_BR);
2519 edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2520 edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2521 edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2523 edit_buffer_insert_ahead (&edit->buffer, c);
2525 edit->last_byte++;
2528 /* --------------------------------------------------------------------------------------------- */
2530 void
2531 edit_insert_over (WEdit * edit)
2533 long i;
2535 for (i = 0; i < edit->over_col; i++)
2536 edit_insert (edit, ' ');
2538 edit->over_col = 0;
2541 /* --------------------------------------------------------------------------------------------- */
2544 edit_delete (WEdit * edit, gboolean byte_delete)
2546 int p = 0;
2547 int cw = 1;
2548 int i;
2550 if (edit->buffer.curs2 == 0)
2551 return 0;
2553 #ifdef HAVE_CHARSET
2554 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2555 if (edit->utf8 && !byte_delete)
2557 edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &cw);
2558 if (cw < 1)
2559 cw = 1;
2561 #else
2562 (void) byte_delete;
2563 #endif
2565 if (edit->mark2 != edit->mark1)
2566 edit_push_markers (edit);
2568 for (i = 1; i <= cw; i++)
2570 if (edit->mark1 > edit->buffer.curs1)
2572 edit->mark1--;
2573 edit->end_mark_curs--;
2575 if (edit->mark2 > edit->buffer.curs1)
2576 edit->mark2--;
2577 if (edit->last_get_rule > edit->buffer.curs1)
2578 edit->last_get_rule--;
2580 p = edit_buffer_delete (&edit->buffer);
2582 edit->last_byte--;
2583 edit_push_undo_action (edit, p + 256);
2586 edit_modification (edit);
2587 if (p == '\n')
2589 book_mark_dec (edit, edit->curs_line);
2590 edit->total_lines--;
2591 edit->force |= REDRAW_AFTER_CURSOR;
2593 if (edit->buffer.curs1 < edit->start_display)
2595 edit->start_display--;
2596 if (p == '\n')
2597 edit->start_line--;
2600 return p;
2603 /* --------------------------------------------------------------------------------------------- */
2606 edit_backspace (WEdit * edit, gboolean byte_delete)
2608 int p = 0;
2609 int cw = 1;
2610 int i;
2612 if (edit->buffer.curs1 == 0)
2613 return 0;
2615 if (edit->mark2 != edit->mark1)
2616 edit_push_markers (edit);
2618 #ifdef HAVE_CHARSET
2619 if (edit->utf8 && !byte_delete)
2621 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &cw);
2622 if (cw < 1)
2623 cw = 1;
2625 #else
2626 (void) byte_delete;
2627 #endif
2629 for (i = 1; i <= cw; i++)
2631 if (edit->mark1 >= edit->buffer.curs1)
2633 edit->mark1--;
2634 edit->end_mark_curs--;
2636 if (edit->mark2 >= edit->buffer.curs1)
2637 edit->mark2--;
2638 if (edit->last_get_rule >= edit->buffer.curs1)
2639 edit->last_get_rule--;
2641 p = edit_buffer_backspace (&edit->buffer);
2643 edit->last_byte--;
2644 edit_push_undo_action (edit, p);
2646 edit_modification (edit);
2647 if (p == '\n')
2649 book_mark_dec (edit, edit->curs_line);
2650 edit->curs_line--;
2651 edit->total_lines--;
2652 edit->force |= REDRAW_AFTER_CURSOR;
2655 if (edit->buffer.curs1 < edit->start_display)
2657 edit->start_display--;
2658 if (p == '\n')
2659 edit->start_line--;
2662 return p;
2665 /* --------------------------------------------------------------------------------------------- */
2666 /** moves the cursor right or left: increment positive or negative respectively */
2668 void
2669 edit_cursor_move (WEdit * edit, off_t increment)
2671 if (increment < 0)
2673 for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2675 int c;
2677 edit_push_undo_action (edit, CURS_RIGHT);
2679 c = edit_buffer_get_previous_byte (&edit->buffer);
2680 edit_buffer_insert_ahead (&edit->buffer, c);
2681 c = edit_buffer_backspace (&edit->buffer);
2682 if (c == '\n')
2684 edit->curs_line--;
2685 edit->force |= REDRAW_LINE_BELOW;
2689 else
2691 for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2693 int c;
2695 edit_push_undo_action (edit, CURS_LEFT);
2697 c = edit_buffer_get_current_byte (&edit->buffer);
2698 edit_buffer_insert (&edit->buffer, c);
2699 c = edit_buffer_delete (&edit->buffer);
2700 if (c == '\n')
2702 edit->curs_line++;
2703 edit->force |= REDRAW_LINE_ABOVE;
2709 /* These functions return positions relative to lines */
2711 /* --------------------------------------------------------------------------------------------- */
2712 /** returns index of last char on line + 1 */
2714 off_t
2715 edit_eol (const WEdit * edit, off_t current)
2717 if (current >= edit->last_byte)
2718 return edit->last_byte;
2720 for (; edit_buffer_get_byte (&edit->buffer, current) != '\n'; current++)
2723 return current;
2726 /* --------------------------------------------------------------------------------------------- */
2727 /** returns index of first char on line */
2729 off_t
2730 edit_bol (const WEdit * edit, off_t current)
2732 if (current <= 0)
2733 return 0;
2735 for (; edit_buffer_get_byte (&edit->buffer, current - 1) != '\n'; current--)
2738 return current;
2741 /* --------------------------------------------------------------------------------------------- */
2743 long
2744 edit_count_lines (const WEdit * edit, off_t current, off_t upto)
2746 long lines = 0;
2748 if (upto > edit->last_byte)
2749 upto = edit->last_byte;
2750 if (current < 0)
2751 current = 0;
2752 while (current < upto)
2753 if (edit_buffer_get_byte (&edit->buffer, current++) == '\n')
2754 lines++;
2755 return lines;
2758 /* --------------------------------------------------------------------------------------------- */
2759 /* If lines is zero this returns the count of lines from current to upto. */
2760 /* If upto is zero returns index of lines forward current. */
2762 off_t
2763 edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto)
2765 if (upto != 0)
2767 return (off_t) edit_count_lines (edit, current, upto);
2769 else
2771 long next;
2772 if (lines < 0)
2773 lines = 0;
2774 while (lines-- != 0)
2776 next = edit_eol (edit, current) + 1;
2777 if (next > edit->last_byte)
2778 break;
2779 else
2780 current = next;
2782 return current;
2786 /* --------------------------------------------------------------------------------------------- */
2787 /** Returns offset of 'lines' lines up from current */
2789 off_t
2790 edit_move_backward (const WEdit * edit, off_t current, long lines)
2792 if (lines < 0)
2793 lines = 0;
2794 current = edit_bol (edit, current);
2795 while (lines-- != 0 && current != 0)
2796 current = edit_bol (edit, current - 1);
2797 return current;
2800 /* --------------------------------------------------------------------------------------------- */
2801 /* If cols is zero this returns the count of columns from current to upto. */
2802 /* If upto is zero returns index of cols across from current. */
2804 off_t
2805 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
2807 off_t p, q;
2808 long col;
2810 if (upto != 0)
2812 q = upto;
2813 cols = -10;
2815 else
2816 q = edit->last_byte + 2;
2818 for (col = 0, p = current; p < q; p++)
2820 int c, orig_c;
2822 if (cols != -10)
2824 if (col == cols)
2825 return p;
2826 if (col > cols)
2827 return p - 1;
2830 orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2832 #ifdef HAVE_CHARSET
2833 if (edit->utf8)
2835 int utf_ch;
2836 int cw = 1;
2838 utf_ch = edit_buffer_get_utf (&edit->buffer, p, &cw);
2839 if (mc_global.utf8_display)
2841 if (cw > 1)
2842 col -= cw - 1;
2843 if (g_unichar_iswide (utf_ch))
2844 col++;
2846 else if (cw > 1 && g_unichar_isprint (utf_ch))
2847 col -= cw - 1;
2850 c = convert_to_display_c (c);
2851 #endif
2853 if (c == '\n')
2854 return (upto != 0 ? (off_t) col : p);
2855 if (c == '\t')
2856 col += TAB_SIZE - col % TAB_SIZE;
2857 else if ((c < 32 || c == 127) && (orig_c == c
2858 #ifdef HAVE_CHARSET
2859 || (!mc_global.utf8_display && !edit->utf8)
2860 #endif
2862 /* '\r' is shown as ^M, so we must advance 2 characters */
2863 /* Caret notation for control characters */
2864 col += 2;
2865 else
2866 col++;
2868 return (off_t) col;
2871 /* --------------------------------------------------------------------------------------------- */
2872 /** returns the current column position of the cursor */
2874 long
2875 edit_get_col (const WEdit * edit)
2877 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->buffer.curs1), 0, edit->buffer.curs1);
2880 /* --------------------------------------------------------------------------------------------- */
2881 /* Scrolling functions */
2882 /* --------------------------------------------------------------------------------------------- */
2884 void
2885 edit_update_curs_row (WEdit * edit)
2887 edit->curs_row = edit->curs_line - edit->start_line;
2890 /* --------------------------------------------------------------------------------------------- */
2892 void
2893 edit_update_curs_col (WEdit * edit)
2895 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->buffer.curs1), 0, edit->buffer.curs1);
2898 /* --------------------------------------------------------------------------------------------- */
2900 long
2901 edit_get_curs_col (const WEdit * edit)
2903 return edit->curs_col;
2906 /* --------------------------------------------------------------------------------------------- */
2907 /** moves the display start position up by i lines */
2909 void
2910 edit_scroll_upward (WEdit * edit, long i)
2912 long lines_above = edit->start_line;
2914 if (i > lines_above)
2915 i = lines_above;
2916 if (i != 0)
2918 edit->start_line -= i;
2919 edit->start_display = edit_move_backward (edit, edit->start_display, i);
2920 edit->force |= REDRAW_PAGE;
2921 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2923 edit_update_curs_row (edit);
2927 /* --------------------------------------------------------------------------------------------- */
2929 void
2930 edit_scroll_downward (WEdit * edit, long i)
2932 long lines_below;
2934 lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1);
2935 if (lines_below > 0)
2937 if (i > lines_below)
2938 i = lines_below;
2939 edit->start_line += i;
2940 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
2941 edit->force |= REDRAW_PAGE;
2942 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2944 edit_update_curs_row (edit);
2947 /* --------------------------------------------------------------------------------------------- */
2949 void
2950 edit_scroll_right (WEdit * edit, long i)
2952 edit->force |= REDRAW_PAGE;
2953 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2954 edit->start_col -= i;
2957 /* --------------------------------------------------------------------------------------------- */
2959 void
2960 edit_scroll_left (WEdit * edit, long i)
2962 if (edit->start_col)
2964 edit->start_col += i;
2965 if (edit->start_col > 0)
2966 edit->start_col = 0;
2967 edit->force |= REDRAW_PAGE;
2968 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2972 /* --------------------------------------------------------------------------------------------- */
2973 /* high level cursor movement commands */
2974 /* --------------------------------------------------------------------------------------------- */
2976 void
2977 edit_move_to_prev_col (WEdit * edit, off_t p)
2979 long prev = edit->prev_col;
2980 long over = edit->over_col;
2982 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
2984 if (option_cursor_beyond_eol)
2986 long line_len;
2988 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->buffer.curs1), 0,
2989 edit_eol (edit, edit->buffer.curs1));
2990 if (line_len < prev + edit->over_col)
2992 edit->over_col = prev + over - line_len;
2993 edit->prev_col = line_len;
2994 edit->curs_col = line_len;
2996 else
2998 edit->curs_col = prev + over;
2999 edit->prev_col = edit->curs_col;
3000 edit->over_col = 0;
3003 else
3005 edit->over_col = 0;
3006 if (option_fake_half_tabs && is_in_indent (edit))
3008 long fake_half_tabs;
3010 edit_update_curs_col (edit);
3012 fake_half_tabs = HALF_TAB_SIZE * space_width;
3013 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3015 int q;
3017 q = edit->curs_col;
3018 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3019 p = edit_bol (edit, edit->buffer.curs1);
3020 edit_cursor_move (edit,
3021 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->buffer.curs1);
3022 if (!left_of_four_spaces (edit))
3023 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->buffer.curs1);
3029 /* --------------------------------------------------------------------------------------------- */
3030 /** check whether line in editor is blank or not
3032 * @param edit editor object
3033 * @param line number of line
3035 * @return TRUE if line in blank, FALSE otherwise
3038 gboolean
3039 edit_line_is_blank (WEdit * edit, long line)
3041 return is_blank (edit, edit_find_line (edit, line));
3044 /* --------------------------------------------------------------------------------------------- */
3045 /** move cursor to line 'line' */
3047 void
3048 edit_move_to_line (WEdit * e, long line)
3050 if (line < e->curs_line)
3051 edit_move_up (e, e->curs_line - line, 0);
3052 else
3053 edit_move_down (e, line - e->curs_line, 0);
3054 edit_scroll_screen_over_cursor (e);
3057 /* --------------------------------------------------------------------------------------------- */
3058 /** scroll window so that first visible line is 'line' */
3060 void
3061 edit_move_display (WEdit * e, long line)
3063 if (line < e->start_line)
3064 edit_scroll_upward (e, e->start_line - line);
3065 else
3066 edit_scroll_downward (e, line - e->start_line);
3069 /* --------------------------------------------------------------------------------------------- */
3070 /** save markers onto undo stack */
3072 void
3073 edit_push_markers (WEdit * edit)
3075 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3076 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3077 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3080 /* --------------------------------------------------------------------------------------------- */
3082 void
3083 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3085 edit->mark1 = m1;
3086 edit->mark2 = m2;
3087 edit->column1 = c1;
3088 edit->column2 = c2;
3092 /* --------------------------------------------------------------------------------------------- */
3093 /** highlight marker toggle */
3095 void
3096 edit_mark_cmd (WEdit * edit, gboolean unmark)
3098 edit_push_markers (edit);
3099 if (unmark)
3101 edit_set_markers (edit, 0, 0, 0, 0);
3102 edit->force |= REDRAW_PAGE;
3104 else if (edit->mark2 >= 0)
3106 edit->end_mark_curs = -1;
3107 edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3108 edit->curs_col + edit->over_col);
3109 edit->force |= REDRAW_PAGE;
3111 else
3113 edit->end_mark_curs = edit->buffer.curs1;
3114 edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3115 edit->curs_col + edit->over_col);
3119 /* --------------------------------------------------------------------------------------------- */
3120 /** highlight the word under cursor */
3122 void
3123 edit_mark_current_word_cmd (WEdit * edit)
3125 long pos;
3127 for (pos = edit->buffer.curs1; pos != 0; pos--)
3129 int c1, c2;
3131 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3132 c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3133 if (!isspace (c1) && isspace (c2))
3134 break;
3135 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3136 break;
3138 edit->mark1 = pos;
3140 for (; pos < edit->last_byte; pos++)
3142 int c1, c2;
3144 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3145 c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3146 if (!isspace (c1) && isspace (c2))
3147 break;
3148 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3149 break;
3151 edit->mark2 = min (pos + 1, edit->last_byte);
3153 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3156 /* --------------------------------------------------------------------------------------------- */
3158 void
3159 edit_mark_current_line_cmd (WEdit * edit)
3161 long pos = edit->buffer.curs1;
3163 edit->mark1 = edit_bol (edit, pos);
3164 edit->mark2 = edit_eol (edit, pos);
3166 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3169 /* --------------------------------------------------------------------------------------------- */
3171 void
3172 edit_delete_line (WEdit * edit)
3175 * Delete right part of the line.
3176 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3177 * beyond EOF.
3179 while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3180 (void) edit_delete (edit, TRUE);
3183 * Delete '\n' char.
3184 * Note that edit_delete() will not corrupt anything if called while
3185 * cursor position is EOF.
3187 (void) edit_delete (edit, TRUE);
3190 * Delete left part of the line.
3191 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3193 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3194 (void) edit_backspace (edit, TRUE);
3197 /* --------------------------------------------------------------------------------------------- */
3199 void
3200 edit_push_key_press (WEdit * edit)
3202 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3203 if (edit->mark2 == -1)
3205 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3206 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3210 /* --------------------------------------------------------------------------------------------- */
3212 void
3213 edit_find_bracket (WEdit * edit)
3215 edit->bracket = edit_get_bracket (edit, 1, 10000);
3216 if (edit->last_bracket != edit->bracket)
3217 edit->force |= REDRAW_PAGE;
3218 edit->last_bracket = edit->bracket;
3221 /* --------------------------------------------------------------------------------------------- */
3223 * This executes a command as though the user initiated it through a key
3224 * press. Callback with MSG_KEY as a message calls this after
3225 * translating the key press. This function can be used to pass any
3226 * command to the editor. Note that the screen wouldn't update
3227 * automatically. Either of command or char_for_insertion must be
3228 * passed as -1. Commands are executed, and char_for_insertion is
3229 * inserted at the cursor.
3232 void
3233 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3235 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3236 || (macro_index < 0
3237 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3239 macro_index = 0;
3240 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3241 return;
3243 if (macro_index != -1)
3245 edit->force |= REDRAW_COMPLETELY;
3246 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3248 edit_store_macro_cmd (edit);
3249 macro_index = -1;
3250 return;
3252 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3254 edit_repeat_macro_cmd (edit);
3255 macro_index = -1;
3256 return;
3260 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3262 record_macro_buf[macro_index].action = command;
3263 record_macro_buf[macro_index++].ch = char_for_insertion;
3265 /* record the beginning of a set of editing actions initiated by a key press */
3266 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3267 edit_push_key_press (edit);
3269 edit_execute_cmd (edit, command, char_for_insertion);
3270 if (edit->column_highlight)
3271 edit->force |= REDRAW_PAGE;
3274 /* --------------------------------------------------------------------------------------------- */
3276 This executes a command at a lower level than macro recording.
3277 It also does not push a key_press onto the undo stack. This means
3278 that if it is called many times, a single undo command will undo
3279 all of them. It also does not check for the Undo command.
3281 void
3282 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3284 Widget *w = WIDGET (edit);
3286 if (command == CK_WindowFullscreen)
3288 edit_toggle_fullscreen (edit);
3289 return;
3292 /* handle window state */
3293 if (edit_handle_move_resize (edit, command))
3294 return;
3296 edit->force |= REDRAW_LINE;
3298 /* The next key press will unhighlight the found string, so update
3299 * the whole page */
3300 if (edit->found_len || edit->column_highlight)
3301 edit->force |= REDRAW_PAGE;
3303 switch (command)
3305 /* a mark command with shift-arrow */
3306 case CK_MarkLeft:
3307 case CK_MarkRight:
3308 case CK_MarkToWordBegin:
3309 case CK_MarkToWordEnd:
3310 case CK_MarkToHome:
3311 case CK_MarkToEnd:
3312 case CK_MarkUp:
3313 case CK_MarkDown:
3314 case CK_MarkPageUp:
3315 case CK_MarkPageDown:
3316 case CK_MarkToFileBegin:
3317 case CK_MarkToFileEnd:
3318 case CK_MarkToPageBegin:
3319 case CK_MarkToPageEnd:
3320 case CK_MarkScrollUp:
3321 case CK_MarkScrollDown:
3322 case CK_MarkParagraphUp:
3323 case CK_MarkParagraphDown:
3324 /* a mark command with alt-arrow */
3325 case CK_MarkColumnPageUp:
3326 case CK_MarkColumnPageDown:
3327 case CK_MarkColumnLeft:
3328 case CK_MarkColumnRight:
3329 case CK_MarkColumnUp:
3330 case CK_MarkColumnDown:
3331 case CK_MarkColumnScrollUp:
3332 case CK_MarkColumnScrollDown:
3333 case CK_MarkColumnParagraphUp:
3334 case CK_MarkColumnParagraphDown:
3335 edit->column_highlight = 0;
3336 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3338 edit_mark_cmd (edit, TRUE); /* clear */
3339 edit_mark_cmd (edit, FALSE); /* marking on */
3341 edit->highlight = 1;
3342 break;
3344 /* any other command */
3345 default:
3346 if (edit->highlight)
3347 edit_mark_cmd (edit, FALSE); /* clear */
3348 edit->highlight = 0;
3351 /* first check for undo */
3352 if (command == CK_Undo)
3354 edit->redo_stack_reset = 0;
3355 edit_group_undo (edit);
3356 edit->found_len = 0;
3357 edit->prev_col = edit_get_col (edit);
3358 edit->search_start = edit->buffer.curs1;
3359 return;
3361 /* check for redo */
3362 if (command == CK_Redo)
3364 edit->redo_stack_reset = 0;
3365 edit_do_redo (edit);
3366 edit->found_len = 0;
3367 edit->prev_col = edit_get_col (edit);
3368 edit->search_start = edit->buffer.curs1;
3369 return;
3372 edit->redo_stack_reset = 1;
3374 /* An ordinary key press */
3375 if (char_for_insertion >= 0)
3377 /* if non persistent selection and text selected */
3378 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3379 edit_block_delete_cmd (edit);
3381 if (edit->overwrite)
3383 /* remove char only one time, after input first byte, multibyte chars */
3384 #ifdef HAVE_CHARSET
3385 if (!mc_global.utf8_display || edit->charpoint == 0)
3386 #endif
3387 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3389 edit_delete (edit, FALSE);
3391 if (option_cursor_beyond_eol && edit->over_col > 0)
3392 edit_insert_over (edit);
3393 #ifdef HAVE_CHARSET
3394 if (char_for_insertion > 255 && !mc_global.utf8_display)
3396 unsigned char str[6 + 1];
3397 size_t i = 0;
3398 int res;
3400 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3401 if (res == 0)
3403 str[0] = '.';
3404 str[1] = '\0';
3406 else
3408 str[res] = '\0';
3410 while (str[i] != 0 && i <= 6)
3412 char_for_insertion = str[i];
3413 edit_insert (edit, char_for_insertion);
3414 i++;
3417 else
3418 #endif
3419 edit_insert (edit, char_for_insertion);
3421 if (option_auto_para_formatting)
3423 format_paragraph (edit, 0);
3424 edit->force |= REDRAW_PAGE;
3426 else
3427 check_and_wrap_line (edit);
3428 edit->found_len = 0;
3429 edit->prev_col = edit_get_col (edit);
3430 edit->search_start = edit->buffer.curs1;
3431 edit_find_bracket (edit);
3432 return;
3435 switch (command)
3437 case CK_TopOnScreen:
3438 case CK_BottomOnScreen:
3439 case CK_Top:
3440 case CK_Bottom:
3441 case CK_PageUp:
3442 case CK_PageDown:
3443 case CK_Home:
3444 case CK_End:
3445 case CK_Up:
3446 case CK_Down:
3447 case CK_Left:
3448 case CK_Right:
3449 case CK_WordLeft:
3450 case CK_WordRight:
3451 if (!option_persistent_selections && edit->mark2 >= 0)
3453 if (edit->column_highlight)
3454 edit_push_undo_action (edit, COLUMN_ON);
3455 edit->column_highlight = 0;
3456 edit_mark_cmd (edit, TRUE);
3460 switch (command)
3462 case CK_TopOnScreen:
3463 case CK_BottomOnScreen:
3464 case CK_MarkToPageBegin:
3465 case CK_MarkToPageEnd:
3466 case CK_Up:
3467 case CK_Down:
3468 case CK_WordLeft:
3469 case CK_WordRight:
3470 case CK_MarkToWordBegin:
3471 case CK_MarkToWordEnd:
3472 case CK_MarkUp:
3473 case CK_MarkDown:
3474 case CK_MarkColumnUp:
3475 case CK_MarkColumnDown:
3476 if (edit->mark2 == -1)
3477 break; /*marking is following the cursor: may need to highlight a whole line */
3478 case CK_Left:
3479 case CK_Right:
3480 case CK_MarkLeft:
3481 case CK_MarkRight:
3482 edit->force |= REDRAW_CHAR_ONLY;
3485 /* basic cursor key commands */
3486 switch (command)
3488 case CK_BackSpace:
3489 /* if non persistent selection and text selected */
3490 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3491 edit_block_delete_cmd (edit);
3492 else if (option_cursor_beyond_eol && edit->over_col > 0)
3493 edit->over_col--;
3494 else if (option_backspace_through_tabs && is_in_indent (edit))
3496 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n'
3497 && edit->buffer.curs1 > 0)
3498 edit_backspace (edit, TRUE);
3500 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3502 int i;
3504 for (i = 0; i < HALF_TAB_SIZE; i++)
3505 edit_backspace (edit, TRUE);
3507 else
3508 edit_backspace (edit, FALSE);
3509 break;
3510 case CK_Delete:
3511 /* if non persistent selection and text selected */
3512 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3513 edit_block_delete_cmd (edit);
3514 else
3516 if (option_cursor_beyond_eol && edit->over_col > 0)
3517 edit_insert_over (edit);
3519 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3521 int i;
3523 for (i = 1; i <= HALF_TAB_SIZE; i++)
3524 edit_delete (edit, TRUE);
3526 else
3527 edit_delete (edit, FALSE);
3529 break;
3530 case CK_DeleteToWordBegin:
3531 edit->over_col = 0;
3532 edit_left_delete_word (edit);
3533 break;
3534 case CK_DeleteToWordEnd:
3535 if (option_cursor_beyond_eol && edit->over_col > 0)
3536 edit_insert_over (edit);
3538 edit_right_delete_word (edit);
3539 break;
3540 case CK_DeleteLine:
3541 edit_delete_line (edit);
3542 break;
3543 case CK_DeleteToHome:
3544 edit_delete_to_line_begin (edit);
3545 break;
3546 case CK_DeleteToEnd:
3547 edit_delete_to_line_end (edit);
3548 break;
3549 case CK_Enter:
3550 edit->over_col = 0;
3551 if (option_auto_para_formatting)
3553 edit_double_newline (edit);
3554 if (option_return_does_auto_indent)
3555 edit_auto_indent (edit);
3556 format_paragraph (edit, 0);
3558 else
3560 edit_insert (edit, '\n');
3561 if (option_return_does_auto_indent)
3562 edit_auto_indent (edit);
3564 break;
3565 case CK_Return:
3566 edit_insert (edit, '\n');
3567 break;
3569 case CK_MarkColumnPageUp:
3570 edit->column_highlight = 1;
3571 case CK_PageUp:
3572 case CK_MarkPageUp:
3573 edit_move_up (edit, w->lines - 1, 1);
3574 break;
3575 case CK_MarkColumnPageDown:
3576 edit->column_highlight = 1;
3577 case CK_PageDown:
3578 case CK_MarkPageDown:
3579 edit_move_down (edit, w->lines - 1, 1);
3580 break;
3581 case CK_MarkColumnLeft:
3582 edit->column_highlight = 1;
3583 case CK_Left:
3584 case CK_MarkLeft:
3585 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3587 if (option_cursor_beyond_eol && edit->over_col > 0)
3588 edit->over_col--;
3589 else
3590 edit_cursor_move (edit, -HALF_TAB_SIZE);
3591 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3593 else
3594 edit_left_char_move_cmd (edit);
3595 break;
3596 case CK_MarkColumnRight:
3597 edit->column_highlight = 1;
3598 case CK_Right:
3599 case CK_MarkRight:
3600 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3602 edit_cursor_move (edit, HALF_TAB_SIZE);
3603 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3605 else
3606 edit_right_char_move_cmd (edit);
3607 break;
3608 case CK_TopOnScreen:
3609 case CK_MarkToPageBegin:
3610 edit_begin_page (edit);
3611 break;
3612 case CK_BottomOnScreen:
3613 case CK_MarkToPageEnd:
3614 edit_end_page (edit);
3615 break;
3616 case CK_WordLeft:
3617 case CK_MarkToWordBegin:
3618 edit->over_col = 0;
3619 edit_left_word_move_cmd (edit);
3620 break;
3621 case CK_WordRight:
3622 case CK_MarkToWordEnd:
3623 edit->over_col = 0;
3624 edit_right_word_move_cmd (edit);
3625 break;
3626 case CK_MarkColumnUp:
3627 edit->column_highlight = 1;
3628 case CK_Up:
3629 case CK_MarkUp:
3630 edit_move_up (edit, 1, 0);
3631 break;
3632 case CK_MarkColumnDown:
3633 edit->column_highlight = 1;
3634 case CK_Down:
3635 case CK_MarkDown:
3636 edit_move_down (edit, 1, 0);
3637 break;
3638 case CK_MarkColumnParagraphUp:
3639 edit->column_highlight = 1;
3640 case CK_ParagraphUp:
3641 case CK_MarkParagraphUp:
3642 edit_move_up_paragraph (edit, 0);
3643 break;
3644 case CK_MarkColumnParagraphDown:
3645 edit->column_highlight = 1;
3646 case CK_ParagraphDown:
3647 case CK_MarkParagraphDown:
3648 edit_move_down_paragraph (edit, 0);
3649 break;
3650 case CK_MarkColumnScrollUp:
3651 edit->column_highlight = 1;
3652 case CK_ScrollUp:
3653 case CK_MarkScrollUp:
3654 edit_move_up (edit, 1, 1);
3655 break;
3656 case CK_MarkColumnScrollDown:
3657 edit->column_highlight = 1;
3658 case CK_ScrollDown:
3659 case CK_MarkScrollDown:
3660 edit_move_down (edit, 1, 1);
3661 break;
3662 case CK_Home:
3663 case CK_MarkToHome:
3664 edit_cursor_to_bol (edit);
3665 break;
3666 case CK_End:
3667 case CK_MarkToEnd:
3668 edit_cursor_to_eol (edit);
3669 break;
3670 case CK_Tab:
3671 /* if text marked shift block */
3672 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3674 if (edit->mark2 < 0)
3675 edit_mark_cmd (edit, FALSE);
3676 edit_move_block_to_right (edit);
3678 else
3680 if (option_cursor_beyond_eol)
3681 edit_insert_over (edit);
3682 edit_tab_cmd (edit);
3683 if (option_auto_para_formatting)
3685 format_paragraph (edit, 0);
3686 edit->force |= REDRAW_PAGE;
3688 else
3689 check_and_wrap_line (edit);
3691 break;
3693 case CK_InsertOverwrite:
3694 edit->overwrite = !edit->overwrite;
3695 break;
3697 case CK_Mark:
3698 if (edit->mark2 >= 0)
3700 if (edit->column_highlight)
3701 edit_push_undo_action (edit, COLUMN_ON);
3702 edit->column_highlight = 0;
3704 edit_mark_cmd (edit, FALSE);
3705 break;
3706 case CK_MarkColumn:
3707 if (!edit->column_highlight)
3708 edit_push_undo_action (edit, COLUMN_OFF);
3709 edit->column_highlight = 1;
3710 edit_mark_cmd (edit, FALSE);
3711 break;
3712 case CK_MarkAll:
3713 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3714 edit->force |= REDRAW_PAGE;
3715 break;
3716 case CK_Unmark:
3717 if (edit->column_highlight)
3718 edit_push_undo_action (edit, COLUMN_ON);
3719 edit->column_highlight = 0;
3720 edit_mark_cmd (edit, TRUE);
3721 break;
3722 case CK_MarkWord:
3723 if (edit->column_highlight)
3724 edit_push_undo_action (edit, COLUMN_ON);
3725 edit->column_highlight = 0;
3726 edit_mark_current_word_cmd (edit);
3727 break;
3728 case CK_MarkLine:
3729 if (edit->column_highlight)
3730 edit_push_undo_action (edit, COLUMN_ON);
3731 edit->column_highlight = 0;
3732 edit_mark_current_line_cmd (edit);
3733 break;
3735 case CK_Bookmark:
3736 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3737 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3738 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3739 else
3740 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3741 break;
3742 case CK_BookmarkFlush:
3743 book_mark_flush (edit, BOOK_MARK_COLOR);
3744 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3745 edit->force |= REDRAW_PAGE;
3746 break;
3747 case CK_BookmarkNext:
3748 if (edit->book_mark != NULL)
3750 edit_book_mark_t *p;
3752 p = book_mark_find (edit, edit->curs_line);
3753 if (p->next != NULL)
3755 p = p->next;
3756 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3757 edit_move_display (edit, p->line - w->lines / 2);
3758 edit_move_to_line (edit, p->line);
3761 break;
3762 case CK_BookmarkPrev:
3763 if (edit->book_mark != NULL)
3765 edit_book_mark_t *p;
3767 p = book_mark_find (edit, edit->curs_line);
3768 while (p->line == edit->curs_line)
3769 if (p->prev != NULL)
3770 p = p->prev;
3771 if (p->line >= 0)
3773 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3774 edit_move_display (edit, p->line - w->lines / 2);
3775 edit_move_to_line (edit, p->line);
3778 break;
3780 case CK_Top:
3781 case CK_MarkToFileBegin:
3782 edit_move_to_top (edit);
3783 break;
3784 case CK_Bottom:
3785 case CK_MarkToFileEnd:
3786 edit_move_to_bottom (edit);
3787 break;
3789 case CK_Copy:
3790 if (option_cursor_beyond_eol && edit->over_col > 0)
3791 edit_insert_over (edit);
3792 edit_block_copy_cmd (edit);
3793 break;
3794 case CK_Remove:
3795 edit_block_delete_cmd (edit);
3796 break;
3797 case CK_Move:
3798 edit_block_move_cmd (edit);
3799 break;
3801 case CK_BlockShiftLeft:
3802 if (edit->mark1 != edit->mark2)
3803 edit_move_block_to_left (edit);
3804 break;
3805 case CK_BlockShiftRight:
3806 if (edit->mark1 != edit->mark2)
3807 edit_move_block_to_right (edit);
3808 break;
3809 case CK_Store:
3810 edit_copy_to_X_buf_cmd (edit);
3811 break;
3812 case CK_Cut:
3813 edit_cut_to_X_buf_cmd (edit);
3814 break;
3815 case CK_Paste:
3816 /* if non persistent selection and text selected */
3817 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3818 edit_block_delete_cmd (edit);
3819 if (option_cursor_beyond_eol && edit->over_col > 0)
3820 edit_insert_over (edit);
3821 edit_paste_from_X_buf_cmd (edit);
3822 if (!option_persistent_selections && edit->mark2 >= 0)
3824 if (edit->column_highlight)
3825 edit_push_undo_action (edit, COLUMN_ON);
3826 edit->column_highlight = 0;
3827 edit_mark_cmd (edit, TRUE);
3829 break;
3830 case CK_History:
3831 edit_paste_from_history (edit);
3832 break;
3834 case CK_SaveAs:
3835 edit_save_as_cmd (edit);
3836 break;
3837 case CK_Save:
3838 edit_save_confirm_cmd (edit);
3839 break;
3840 case CK_BlockSave:
3841 edit_save_block_cmd (edit);
3842 break;
3843 case CK_InsertFile:
3844 edit_insert_file_cmd (edit);
3845 break;
3847 case CK_FilePrev:
3848 edit_load_back_cmd (edit);
3849 break;
3850 case CK_FileNext:
3851 edit_load_forward_cmd (edit);
3852 break;
3854 case CK_SyntaxChoose:
3855 edit_syntax_dialog (edit);
3856 break;
3858 case CK_Search:
3859 edit_search_cmd (edit, FALSE);
3860 break;
3861 case CK_SearchContinue:
3862 edit_search_cmd (edit, TRUE);
3863 break;
3864 case CK_Replace:
3865 edit_replace_cmd (edit, 0);
3866 break;
3867 case CK_ReplaceContinue:
3868 edit_replace_cmd (edit, 1);
3869 break;
3870 case CK_Complete:
3871 /* if text marked shift block */
3872 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3873 edit_move_block_to_left (edit);
3874 else
3875 edit_complete_word_cmd (edit);
3876 break;
3877 case CK_Find:
3878 edit_get_match_keyword_cmd (edit);
3879 break;
3881 #ifdef HAVE_ASPELL
3882 case CK_SpellCheckCurrentWord:
3883 edit_suggest_current_word (edit);
3884 break;
3885 case CK_SpellCheck:
3886 edit_spellcheck_file (edit);
3887 break;
3888 case CK_SpellCheckSelectLang:
3889 edit_set_spell_lang ();
3890 break;
3891 #endif
3893 case CK_Date:
3895 char s[BUF_MEDIUM];
3896 /* fool gcc to prevent a Y2K warning */
3897 char time_format[] = "_c";
3898 time_format[0] = '%';
3900 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3901 edit_print_string (edit, s);
3902 edit->force |= REDRAW_PAGE;
3904 break;
3905 case CK_Goto:
3906 edit_goto_cmd (edit);
3907 break;
3908 case CK_ParagraphFormat:
3909 format_paragraph (edit, 1);
3910 edit->force |= REDRAW_PAGE;
3911 break;
3912 case CK_MacroDelete:
3913 edit_delete_macro_cmd (edit);
3914 break;
3915 case CK_MatchBracket:
3916 edit_goto_matching_bracket (edit);
3917 break;
3918 case CK_UserMenu:
3919 user_menu (edit, NULL, -1);
3920 break;
3921 case CK_Sort:
3922 edit_sort_cmd (edit);
3923 break;
3924 case CK_ExternalCommand:
3925 edit_ext_cmd (edit);
3926 break;
3927 case CK_Mail:
3928 edit_mail_dialog (edit);
3929 break;
3930 #ifdef HAVE_CHARSET
3931 case CK_SelectCodepage:
3932 edit_select_codepage_cmd (edit);
3933 break;
3934 #endif
3935 case CK_InsertLiteral:
3936 edit_insert_literal_cmd (edit);
3937 break;
3938 case CK_MacroStartStopRecord:
3939 edit_begin_end_macro_cmd (edit);
3940 break;
3941 case CK_RepeatStartStopRecord:
3942 edit_begin_end_repeat_cmd (edit);
3943 break;
3944 case CK_ExtendedKeyMap:
3945 edit->extmod = TRUE;
3946 break;
3947 default:
3948 break;
3951 /* CK_PipeBlock */
3952 if ((command / CK_PipeBlock (0)) == 1)
3953 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
3955 /* keys which must set the col position, and the search vars */
3956 switch (command)
3958 case CK_Search:
3959 case CK_SearchContinue:
3960 case CK_Replace:
3961 case CK_ReplaceContinue:
3962 case CK_Complete:
3963 edit->prev_col = edit_get_col (edit);
3964 break;
3965 case CK_Up:
3966 case CK_MarkUp:
3967 case CK_MarkColumnUp:
3968 case CK_Down:
3969 case CK_MarkDown:
3970 case CK_MarkColumnDown:
3971 case CK_PageUp:
3972 case CK_MarkPageUp:
3973 case CK_MarkColumnPageUp:
3974 case CK_PageDown:
3975 case CK_MarkPageDown:
3976 case CK_MarkColumnPageDown:
3977 case CK_Top:
3978 case CK_MarkToFileBegin:
3979 case CK_Bottom:
3980 case CK_MarkToFileEnd:
3981 case CK_ParagraphUp:
3982 case CK_MarkParagraphUp:
3983 case CK_MarkColumnParagraphUp:
3984 case CK_ParagraphDown:
3985 case CK_MarkParagraphDown:
3986 case CK_MarkColumnParagraphDown:
3987 case CK_ScrollUp:
3988 case CK_MarkScrollUp:
3989 case CK_MarkColumnScrollUp:
3990 case CK_ScrollDown:
3991 case CK_MarkScrollDown:
3992 case CK_MarkColumnScrollDown:
3993 edit->search_start = edit->buffer.curs1;
3994 edit->found_len = 0;
3995 break;
3996 default:
3997 edit->found_len = 0;
3998 edit->prev_col = edit_get_col (edit);
3999 edit->search_start = edit->buffer.curs1;
4001 edit_find_bracket (edit);
4003 if (option_auto_para_formatting)
4005 switch (command)
4007 case CK_BackSpace:
4008 case CK_Delete:
4009 case CK_DeleteToWordBegin:
4010 case CK_DeleteToWordEnd:
4011 case CK_DeleteToHome:
4012 case CK_DeleteToEnd:
4013 format_paragraph (edit, 0);
4014 edit->force |= REDRAW_PAGE;
4019 /* --------------------------------------------------------------------------------------------- */
4021 void
4022 edit_stack_init (void)
4024 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4026 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4027 edit_history_moveto[edit_stack_iterator].line = -1;
4030 edit_stack_iterator = 0;
4033 /* --------------------------------------------------------------------------------------------- */
4035 void
4036 edit_stack_free (void)
4038 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4039 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4042 /* --------------------------------------------------------------------------------------------- */
4043 /** move i lines */
4045 void
4046 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4048 edit_move_updown (edit, i, do_scroll, TRUE);
4051 /* --------------------------------------------------------------------------------------------- */
4052 /** move i lines */
4054 void
4055 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4057 edit_move_updown (edit, i, do_scroll, FALSE);
4060 /* --------------------------------------------------------------------------------------------- */