New editor buffer API to delete character at cursor position.
[midnight-commander.git] / src / editor / edit.c
blobc4d231d331d075bb2c03a6eeaa01abe9e0494120
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 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2672 int c;
2674 if (increment < 0)
2676 for (; increment < 0; increment++)
2678 if (edit->buffer.curs1 == 0)
2679 return;
2681 edit_push_undo_action (edit, CURS_RIGHT);
2683 c = edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - 1);
2684 if (!((edit->buffer.curs2 + 1) & M_EDIT_BUF_SIZE))
2685 edit->buffer.buffers2[(edit->buffer.curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2686 edit->buffer.buffers2[edit->buffer.curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2687 (edit->buffer.curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2688 edit->buffer.curs2++;
2689 c = edit->buffer.buffers1[(edit->buffer.curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->buffer.curs1 -
2690 1) & M_EDIT_BUF_SIZE];
2691 if (!((edit->buffer.curs1 - 1) & M_EDIT_BUF_SIZE))
2693 g_free (edit->buffer.buffers1[edit->buffer.curs1 >> S_EDIT_BUF_SIZE]);
2694 edit->buffer.buffers1[edit->buffer.curs1 >> S_EDIT_BUF_SIZE] = NULL;
2696 edit->buffer.curs1--;
2697 if (c == '\n')
2699 edit->curs_line--;
2700 edit->force |= REDRAW_LINE_BELOW;
2705 else
2707 for (; increment > 0; increment--)
2709 if (edit->buffer.curs2 == 0)
2710 return;
2712 edit_push_undo_action (edit, CURS_LEFT);
2714 c = edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1);
2715 if (!(edit->buffer.curs1 & M_EDIT_BUF_SIZE))
2716 edit->buffer.buffers1[edit->buffer.curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2717 edit->buffer.buffers1[edit->buffer.curs1 >> S_EDIT_BUF_SIZE][edit->buffer.curs1 & M_EDIT_BUF_SIZE] = c;
2718 edit->buffer.curs1++;
2719 c = edit->buffer.buffers2[(edit->buffer.curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2720 ((edit->buffer.curs2 -
2721 1) & M_EDIT_BUF_SIZE) - 1];
2722 if (!(edit->buffer.curs2 & M_EDIT_BUF_SIZE))
2724 g_free (edit->buffer.buffers2[edit->buffer.curs2 >> S_EDIT_BUF_SIZE]);
2725 edit->buffer.buffers2[edit->buffer.curs2 >> S_EDIT_BUF_SIZE] = 0;
2727 edit->buffer.curs2--;
2728 if (c == '\n')
2730 edit->curs_line++;
2731 edit->force |= REDRAW_LINE_ABOVE;
2737 /* These functions return positions relative to lines */
2739 /* --------------------------------------------------------------------------------------------- */
2740 /** returns index of last char on line + 1 */
2742 off_t
2743 edit_eol (const WEdit * edit, off_t current)
2745 if (current >= edit->last_byte)
2746 return edit->last_byte;
2748 for (; edit_buffer_get_byte (&edit->buffer, current) != '\n'; current++)
2751 return current;
2754 /* --------------------------------------------------------------------------------------------- */
2755 /** returns index of first char on line */
2757 off_t
2758 edit_bol (const WEdit * edit, off_t current)
2760 if (current <= 0)
2761 return 0;
2763 for (; edit_buffer_get_byte (&edit->buffer, current - 1) != '\n'; current--)
2766 return current;
2769 /* --------------------------------------------------------------------------------------------- */
2771 long
2772 edit_count_lines (const WEdit * edit, off_t current, off_t upto)
2774 long lines = 0;
2776 if (upto > edit->last_byte)
2777 upto = edit->last_byte;
2778 if (current < 0)
2779 current = 0;
2780 while (current < upto)
2781 if (edit_buffer_get_byte (&edit->buffer, current++) == '\n')
2782 lines++;
2783 return lines;
2786 /* --------------------------------------------------------------------------------------------- */
2787 /* If lines is zero this returns the count of lines from current to upto. */
2788 /* If upto is zero returns index of lines forward current. */
2790 off_t
2791 edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto)
2793 if (upto != 0)
2795 return (off_t) edit_count_lines (edit, current, upto);
2797 else
2799 long next;
2800 if (lines < 0)
2801 lines = 0;
2802 while (lines-- != 0)
2804 next = edit_eol (edit, current) + 1;
2805 if (next > edit->last_byte)
2806 break;
2807 else
2808 current = next;
2810 return current;
2814 /* --------------------------------------------------------------------------------------------- */
2815 /** Returns offset of 'lines' lines up from current */
2817 off_t
2818 edit_move_backward (const WEdit * edit, off_t current, long lines)
2820 if (lines < 0)
2821 lines = 0;
2822 current = edit_bol (edit, current);
2823 while (lines-- != 0 && current != 0)
2824 current = edit_bol (edit, current - 1);
2825 return current;
2828 /* --------------------------------------------------------------------------------------------- */
2829 /* If cols is zero this returns the count of columns from current to upto. */
2830 /* If upto is zero returns index of cols across from current. */
2832 off_t
2833 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
2835 off_t p, q;
2836 long col;
2838 if (upto != 0)
2840 q = upto;
2841 cols = -10;
2843 else
2844 q = edit->last_byte + 2;
2846 for (col = 0, p = current; p < q; p++)
2848 int c, orig_c;
2850 if (cols != -10)
2852 if (col == cols)
2853 return p;
2854 if (col > cols)
2855 return p - 1;
2858 orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2860 #ifdef HAVE_CHARSET
2861 if (edit->utf8)
2863 int utf_ch;
2864 int cw = 1;
2866 utf_ch = edit_buffer_get_utf (&edit->buffer, p, &cw);
2867 if (mc_global.utf8_display)
2869 if (cw > 1)
2870 col -= cw - 1;
2871 if (g_unichar_iswide (utf_ch))
2872 col++;
2874 else if (cw > 1 && g_unichar_isprint (utf_ch))
2875 col -= cw - 1;
2878 c = convert_to_display_c (c);
2879 #endif
2881 if (c == '\n')
2882 return (upto != 0 ? (off_t) col : p);
2883 if (c == '\t')
2884 col += TAB_SIZE - col % TAB_SIZE;
2885 else if ((c < 32 || c == 127) && (orig_c == c
2886 #ifdef HAVE_CHARSET
2887 || (!mc_global.utf8_display && !edit->utf8)
2888 #endif
2890 /* '\r' is shown as ^M, so we must advance 2 characters */
2891 /* Caret notation for control characters */
2892 col += 2;
2893 else
2894 col++;
2896 return (off_t) col;
2899 /* --------------------------------------------------------------------------------------------- */
2900 /** returns the current column position of the cursor */
2902 long
2903 edit_get_col (const WEdit * edit)
2905 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->buffer.curs1), 0, edit->buffer.curs1);
2908 /* --------------------------------------------------------------------------------------------- */
2909 /* Scrolling functions */
2910 /* --------------------------------------------------------------------------------------------- */
2912 void
2913 edit_update_curs_row (WEdit * edit)
2915 edit->curs_row = edit->curs_line - edit->start_line;
2918 /* --------------------------------------------------------------------------------------------- */
2920 void
2921 edit_update_curs_col (WEdit * edit)
2923 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->buffer.curs1), 0, edit->buffer.curs1);
2926 /* --------------------------------------------------------------------------------------------- */
2928 long
2929 edit_get_curs_col (const WEdit * edit)
2931 return edit->curs_col;
2934 /* --------------------------------------------------------------------------------------------- */
2935 /** moves the display start position up by i lines */
2937 void
2938 edit_scroll_upward (WEdit * edit, long i)
2940 long lines_above = edit->start_line;
2942 if (i > lines_above)
2943 i = lines_above;
2944 if (i != 0)
2946 edit->start_line -= i;
2947 edit->start_display = edit_move_backward (edit, edit->start_display, i);
2948 edit->force |= REDRAW_PAGE;
2949 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2951 edit_update_curs_row (edit);
2955 /* --------------------------------------------------------------------------------------------- */
2957 void
2958 edit_scroll_downward (WEdit * edit, long i)
2960 long lines_below;
2962 lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1);
2963 if (lines_below > 0)
2965 if (i > lines_below)
2966 i = lines_below;
2967 edit->start_line += i;
2968 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
2969 edit->force |= REDRAW_PAGE;
2970 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2972 edit_update_curs_row (edit);
2975 /* --------------------------------------------------------------------------------------------- */
2977 void
2978 edit_scroll_right (WEdit * edit, long i)
2980 edit->force |= REDRAW_PAGE;
2981 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2982 edit->start_col -= i;
2985 /* --------------------------------------------------------------------------------------------- */
2987 void
2988 edit_scroll_left (WEdit * edit, long i)
2990 if (edit->start_col)
2992 edit->start_col += i;
2993 if (edit->start_col > 0)
2994 edit->start_col = 0;
2995 edit->force |= REDRAW_PAGE;
2996 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3000 /* --------------------------------------------------------------------------------------------- */
3001 /* high level cursor movement commands */
3002 /* --------------------------------------------------------------------------------------------- */
3004 void
3005 edit_move_to_prev_col (WEdit * edit, off_t p)
3007 long prev = edit->prev_col;
3008 long over = edit->over_col;
3010 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
3012 if (option_cursor_beyond_eol)
3014 long line_len;
3016 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->buffer.curs1), 0,
3017 edit_eol (edit, edit->buffer.curs1));
3018 if (line_len < prev + edit->over_col)
3020 edit->over_col = prev + over - line_len;
3021 edit->prev_col = line_len;
3022 edit->curs_col = line_len;
3024 else
3026 edit->curs_col = prev + over;
3027 edit->prev_col = edit->curs_col;
3028 edit->over_col = 0;
3031 else
3033 edit->over_col = 0;
3034 if (option_fake_half_tabs && is_in_indent (edit))
3036 long fake_half_tabs;
3038 edit_update_curs_col (edit);
3040 fake_half_tabs = HALF_TAB_SIZE * space_width;
3041 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3043 int q;
3045 q = edit->curs_col;
3046 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3047 p = edit_bol (edit, edit->buffer.curs1);
3048 edit_cursor_move (edit,
3049 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->buffer.curs1);
3050 if (!left_of_four_spaces (edit))
3051 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->buffer.curs1);
3057 /* --------------------------------------------------------------------------------------------- */
3058 /** check whether line in editor is blank or not
3060 * @param edit editor object
3061 * @param line number of line
3063 * @return TRUE if line in blank, FALSE otherwise
3066 gboolean
3067 edit_line_is_blank (WEdit * edit, long line)
3069 return is_blank (edit, edit_find_line (edit, line));
3072 /* --------------------------------------------------------------------------------------------- */
3073 /** move cursor to line 'line' */
3075 void
3076 edit_move_to_line (WEdit * e, long line)
3078 if (line < e->curs_line)
3079 edit_move_up (e, e->curs_line - line, 0);
3080 else
3081 edit_move_down (e, line - e->curs_line, 0);
3082 edit_scroll_screen_over_cursor (e);
3085 /* --------------------------------------------------------------------------------------------- */
3086 /** scroll window so that first visible line is 'line' */
3088 void
3089 edit_move_display (WEdit * e, long line)
3091 if (line < e->start_line)
3092 edit_scroll_upward (e, e->start_line - line);
3093 else
3094 edit_scroll_downward (e, line - e->start_line);
3097 /* --------------------------------------------------------------------------------------------- */
3098 /** save markers onto undo stack */
3100 void
3101 edit_push_markers (WEdit * edit)
3103 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3104 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3105 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3108 /* --------------------------------------------------------------------------------------------- */
3110 void
3111 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3113 edit->mark1 = m1;
3114 edit->mark2 = m2;
3115 edit->column1 = c1;
3116 edit->column2 = c2;
3120 /* --------------------------------------------------------------------------------------------- */
3121 /** highlight marker toggle */
3123 void
3124 edit_mark_cmd (WEdit * edit, gboolean unmark)
3126 edit_push_markers (edit);
3127 if (unmark)
3129 edit_set_markers (edit, 0, 0, 0, 0);
3130 edit->force |= REDRAW_PAGE;
3132 else if (edit->mark2 >= 0)
3134 edit->end_mark_curs = -1;
3135 edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3136 edit->curs_col + edit->over_col);
3137 edit->force |= REDRAW_PAGE;
3139 else
3141 edit->end_mark_curs = edit->buffer.curs1;
3142 edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3143 edit->curs_col + edit->over_col);
3147 /* --------------------------------------------------------------------------------------------- */
3148 /** highlight the word under cursor */
3150 void
3151 edit_mark_current_word_cmd (WEdit * edit)
3153 long pos;
3155 for (pos = edit->buffer.curs1; pos != 0; pos--)
3157 int c1, c2;
3159 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3160 c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3161 if (!isspace (c1) && isspace (c2))
3162 break;
3163 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3164 break;
3166 edit->mark1 = pos;
3168 for (; pos < edit->last_byte; pos++)
3170 int c1, c2;
3172 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3173 c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3174 if (!isspace (c1) && isspace (c2))
3175 break;
3176 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3177 break;
3179 edit->mark2 = min (pos + 1, edit->last_byte);
3181 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3184 /* --------------------------------------------------------------------------------------------- */
3186 void
3187 edit_mark_current_line_cmd (WEdit * edit)
3189 long pos = edit->buffer.curs1;
3191 edit->mark1 = edit_bol (edit, pos);
3192 edit->mark2 = edit_eol (edit, pos);
3194 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3197 /* --------------------------------------------------------------------------------------------- */
3199 void
3200 edit_delete_line (WEdit * edit)
3203 * Delete right part of the line.
3204 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3205 * beyond EOF.
3207 while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3208 (void) edit_delete (edit, TRUE);
3211 * Delete '\n' char.
3212 * Note that edit_delete() will not corrupt anything if called while
3213 * cursor position is EOF.
3215 (void) edit_delete (edit, TRUE);
3218 * Delete left part of the line.
3219 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3221 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3222 (void) edit_backspace (edit, TRUE);
3225 /* --------------------------------------------------------------------------------------------- */
3227 long
3228 edit_indent_width (const WEdit * edit, off_t p)
3230 off_t q = p;
3232 /* move to the end of the leading whitespace of the line */
3233 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, q)) && q < edit->last_byte - 1)
3234 q++;
3235 /* count the number of columns of indentation */
3236 return (long) edit_move_forward3 (edit, p, 0, q);
3239 /* --------------------------------------------------------------------------------------------- */
3241 void
3242 edit_insert_indent (WEdit * edit, int indent)
3244 if (!option_fill_tabs_with_spaces)
3246 while (indent >= TAB_SIZE)
3248 edit_insert (edit, '\t');
3249 indent -= TAB_SIZE;
3252 while (indent-- > 0)
3253 edit_insert (edit, ' ');
3256 /* --------------------------------------------------------------------------------------------- */
3258 void
3259 edit_push_key_press (WEdit * edit)
3261 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3262 if (edit->mark2 == -1)
3264 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3265 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3269 /* --------------------------------------------------------------------------------------------- */
3271 void
3272 edit_find_bracket (WEdit * edit)
3274 edit->bracket = edit_get_bracket (edit, 1, 10000);
3275 if (edit->last_bracket != edit->bracket)
3276 edit->force |= REDRAW_PAGE;
3277 edit->last_bracket = edit->bracket;
3280 /* --------------------------------------------------------------------------------------------- */
3282 * This executes a command as though the user initiated it through a key
3283 * press. Callback with MSG_KEY as a message calls this after
3284 * translating the key press. This function can be used to pass any
3285 * command to the editor. Note that the screen wouldn't update
3286 * automatically. Either of command or char_for_insertion must be
3287 * passed as -1. Commands are executed, and char_for_insertion is
3288 * inserted at the cursor.
3291 void
3292 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3294 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3295 || (macro_index < 0
3296 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3298 macro_index = 0;
3299 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3300 return;
3302 if (macro_index != -1)
3304 edit->force |= REDRAW_COMPLETELY;
3305 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3307 edit_store_macro_cmd (edit);
3308 macro_index = -1;
3309 return;
3311 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3313 edit_repeat_macro_cmd (edit);
3314 macro_index = -1;
3315 return;
3319 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3321 record_macro_buf[macro_index].action = command;
3322 record_macro_buf[macro_index++].ch = char_for_insertion;
3324 /* record the beginning of a set of editing actions initiated by a key press */
3325 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3326 edit_push_key_press (edit);
3328 edit_execute_cmd (edit, command, char_for_insertion);
3329 if (edit->column_highlight)
3330 edit->force |= REDRAW_PAGE;
3333 /* --------------------------------------------------------------------------------------------- */
3335 This executes a command at a lower level than macro recording.
3336 It also does not push a key_press onto the undo stack. This means
3337 that if it is called many times, a single undo command will undo
3338 all of them. It also does not check for the Undo command.
3340 void
3341 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3343 Widget *w = WIDGET (edit);
3345 if (command == CK_WindowFullscreen)
3347 edit_toggle_fullscreen (edit);
3348 return;
3351 /* handle window state */
3352 if (edit_handle_move_resize (edit, command))
3353 return;
3355 edit->force |= REDRAW_LINE;
3357 /* The next key press will unhighlight the found string, so update
3358 * the whole page */
3359 if (edit->found_len || edit->column_highlight)
3360 edit->force |= REDRAW_PAGE;
3362 switch (command)
3364 /* a mark command with shift-arrow */
3365 case CK_MarkLeft:
3366 case CK_MarkRight:
3367 case CK_MarkToWordBegin:
3368 case CK_MarkToWordEnd:
3369 case CK_MarkToHome:
3370 case CK_MarkToEnd:
3371 case CK_MarkUp:
3372 case CK_MarkDown:
3373 case CK_MarkPageUp:
3374 case CK_MarkPageDown:
3375 case CK_MarkToFileBegin:
3376 case CK_MarkToFileEnd:
3377 case CK_MarkToPageBegin:
3378 case CK_MarkToPageEnd:
3379 case CK_MarkScrollUp:
3380 case CK_MarkScrollDown:
3381 case CK_MarkParagraphUp:
3382 case CK_MarkParagraphDown:
3383 /* a mark command with alt-arrow */
3384 case CK_MarkColumnPageUp:
3385 case CK_MarkColumnPageDown:
3386 case CK_MarkColumnLeft:
3387 case CK_MarkColumnRight:
3388 case CK_MarkColumnUp:
3389 case CK_MarkColumnDown:
3390 case CK_MarkColumnScrollUp:
3391 case CK_MarkColumnScrollDown:
3392 case CK_MarkColumnParagraphUp:
3393 case CK_MarkColumnParagraphDown:
3394 edit->column_highlight = 0;
3395 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3397 edit_mark_cmd (edit, TRUE); /* clear */
3398 edit_mark_cmd (edit, FALSE); /* marking on */
3400 edit->highlight = 1;
3401 break;
3403 /* any other command */
3404 default:
3405 if (edit->highlight)
3406 edit_mark_cmd (edit, FALSE); /* clear */
3407 edit->highlight = 0;
3410 /* first check for undo */
3411 if (command == CK_Undo)
3413 edit->redo_stack_reset = 0;
3414 edit_group_undo (edit);
3415 edit->found_len = 0;
3416 edit->prev_col = edit_get_col (edit);
3417 edit->search_start = edit->buffer.curs1;
3418 return;
3420 /* check for redo */
3421 if (command == CK_Redo)
3423 edit->redo_stack_reset = 0;
3424 edit_do_redo (edit);
3425 edit->found_len = 0;
3426 edit->prev_col = edit_get_col (edit);
3427 edit->search_start = edit->buffer.curs1;
3428 return;
3431 edit->redo_stack_reset = 1;
3433 /* An ordinary key press */
3434 if (char_for_insertion >= 0)
3436 /* if non persistent selection and text selected */
3437 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3438 edit_block_delete_cmd (edit);
3440 if (edit->overwrite)
3442 /* remove char only one time, after input first byte, multibyte chars */
3443 #ifdef HAVE_CHARSET
3444 if (!mc_global.utf8_display || edit->charpoint == 0)
3445 #endif
3446 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3448 edit_delete (edit, FALSE);
3450 if (option_cursor_beyond_eol && edit->over_col > 0)
3451 edit_insert_over (edit);
3452 #ifdef HAVE_CHARSET
3453 if (char_for_insertion > 255 && !mc_global.utf8_display)
3455 unsigned char str[6 + 1];
3456 size_t i = 0;
3457 int res;
3459 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3460 if (res == 0)
3462 str[0] = '.';
3463 str[1] = '\0';
3465 else
3467 str[res] = '\0';
3469 while (str[i] != 0 && i <= 6)
3471 char_for_insertion = str[i];
3472 edit_insert (edit, char_for_insertion);
3473 i++;
3476 else
3477 #endif
3478 edit_insert (edit, char_for_insertion);
3480 if (option_auto_para_formatting)
3482 format_paragraph (edit, 0);
3483 edit->force |= REDRAW_PAGE;
3485 else
3486 check_and_wrap_line (edit);
3487 edit->found_len = 0;
3488 edit->prev_col = edit_get_col (edit);
3489 edit->search_start = edit->buffer.curs1;
3490 edit_find_bracket (edit);
3491 return;
3494 switch (command)
3496 case CK_TopOnScreen:
3497 case CK_BottomOnScreen:
3498 case CK_Top:
3499 case CK_Bottom:
3500 case CK_PageUp:
3501 case CK_PageDown:
3502 case CK_Home:
3503 case CK_End:
3504 case CK_Up:
3505 case CK_Down:
3506 case CK_Left:
3507 case CK_Right:
3508 case CK_WordLeft:
3509 case CK_WordRight:
3510 if (!option_persistent_selections && edit->mark2 >= 0)
3512 if (edit->column_highlight)
3513 edit_push_undo_action (edit, COLUMN_ON);
3514 edit->column_highlight = 0;
3515 edit_mark_cmd (edit, TRUE);
3519 switch (command)
3521 case CK_TopOnScreen:
3522 case CK_BottomOnScreen:
3523 case CK_MarkToPageBegin:
3524 case CK_MarkToPageEnd:
3525 case CK_Up:
3526 case CK_Down:
3527 case CK_WordLeft:
3528 case CK_WordRight:
3529 case CK_MarkToWordBegin:
3530 case CK_MarkToWordEnd:
3531 case CK_MarkUp:
3532 case CK_MarkDown:
3533 case CK_MarkColumnUp:
3534 case CK_MarkColumnDown:
3535 if (edit->mark2 == -1)
3536 break; /*marking is following the cursor: may need to highlight a whole line */
3537 case CK_Left:
3538 case CK_Right:
3539 case CK_MarkLeft:
3540 case CK_MarkRight:
3541 edit->force |= REDRAW_CHAR_ONLY;
3544 /* basic cursor key commands */
3545 switch (command)
3547 case CK_BackSpace:
3548 /* if non persistent selection and text selected */
3549 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3550 edit_block_delete_cmd (edit);
3551 else if (option_cursor_beyond_eol && edit->over_col > 0)
3552 edit->over_col--;
3553 else if (option_backspace_through_tabs && is_in_indent (edit))
3555 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n'
3556 && edit->buffer.curs1 > 0)
3557 edit_backspace (edit, TRUE);
3559 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3561 int i;
3563 for (i = 0; i < HALF_TAB_SIZE; i++)
3564 edit_backspace (edit, TRUE);
3566 else
3567 edit_backspace (edit, FALSE);
3568 break;
3569 case CK_Delete:
3570 /* if non persistent selection and text selected */
3571 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3572 edit_block_delete_cmd (edit);
3573 else
3575 if (option_cursor_beyond_eol && edit->over_col > 0)
3576 edit_insert_over (edit);
3578 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3580 int i;
3582 for (i = 1; i <= HALF_TAB_SIZE; i++)
3583 edit_delete (edit, TRUE);
3585 else
3586 edit_delete (edit, FALSE);
3588 break;
3589 case CK_DeleteToWordBegin:
3590 edit->over_col = 0;
3591 edit_left_delete_word (edit);
3592 break;
3593 case CK_DeleteToWordEnd:
3594 if (option_cursor_beyond_eol && edit->over_col > 0)
3595 edit_insert_over (edit);
3597 edit_right_delete_word (edit);
3598 break;
3599 case CK_DeleteLine:
3600 edit_delete_line (edit);
3601 break;
3602 case CK_DeleteToHome:
3603 edit_delete_to_line_begin (edit);
3604 break;
3605 case CK_DeleteToEnd:
3606 edit_delete_to_line_end (edit);
3607 break;
3608 case CK_Enter:
3609 edit->over_col = 0;
3610 if (option_auto_para_formatting)
3612 edit_double_newline (edit);
3613 if (option_return_does_auto_indent)
3614 edit_auto_indent (edit);
3615 format_paragraph (edit, 0);
3617 else
3619 edit_insert (edit, '\n');
3620 if (option_return_does_auto_indent)
3621 edit_auto_indent (edit);
3623 break;
3624 case CK_Return:
3625 edit_insert (edit, '\n');
3626 break;
3628 case CK_MarkColumnPageUp:
3629 edit->column_highlight = 1;
3630 case CK_PageUp:
3631 case CK_MarkPageUp:
3632 edit_move_up (edit, w->lines - 1, 1);
3633 break;
3634 case CK_MarkColumnPageDown:
3635 edit->column_highlight = 1;
3636 case CK_PageDown:
3637 case CK_MarkPageDown:
3638 edit_move_down (edit, w->lines - 1, 1);
3639 break;
3640 case CK_MarkColumnLeft:
3641 edit->column_highlight = 1;
3642 case CK_Left:
3643 case CK_MarkLeft:
3644 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3646 if (option_cursor_beyond_eol && edit->over_col > 0)
3647 edit->over_col--;
3648 else
3649 edit_cursor_move (edit, -HALF_TAB_SIZE);
3650 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3652 else
3653 edit_left_char_move_cmd (edit);
3654 break;
3655 case CK_MarkColumnRight:
3656 edit->column_highlight = 1;
3657 case CK_Right:
3658 case CK_MarkRight:
3659 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3661 edit_cursor_move (edit, HALF_TAB_SIZE);
3662 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3664 else
3665 edit_right_char_move_cmd (edit);
3666 break;
3667 case CK_TopOnScreen:
3668 case CK_MarkToPageBegin:
3669 edit_begin_page (edit);
3670 break;
3671 case CK_BottomOnScreen:
3672 case CK_MarkToPageEnd:
3673 edit_end_page (edit);
3674 break;
3675 case CK_WordLeft:
3676 case CK_MarkToWordBegin:
3677 edit->over_col = 0;
3678 edit_left_word_move_cmd (edit);
3679 break;
3680 case CK_WordRight:
3681 case CK_MarkToWordEnd:
3682 edit->over_col = 0;
3683 edit_right_word_move_cmd (edit);
3684 break;
3685 case CK_MarkColumnUp:
3686 edit->column_highlight = 1;
3687 case CK_Up:
3688 case CK_MarkUp:
3689 edit_move_up (edit, 1, 0);
3690 break;
3691 case CK_MarkColumnDown:
3692 edit->column_highlight = 1;
3693 case CK_Down:
3694 case CK_MarkDown:
3695 edit_move_down (edit, 1, 0);
3696 break;
3697 case CK_MarkColumnParagraphUp:
3698 edit->column_highlight = 1;
3699 case CK_ParagraphUp:
3700 case CK_MarkParagraphUp:
3701 edit_move_up_paragraph (edit, 0);
3702 break;
3703 case CK_MarkColumnParagraphDown:
3704 edit->column_highlight = 1;
3705 case CK_ParagraphDown:
3706 case CK_MarkParagraphDown:
3707 edit_move_down_paragraph (edit, 0);
3708 break;
3709 case CK_MarkColumnScrollUp:
3710 edit->column_highlight = 1;
3711 case CK_ScrollUp:
3712 case CK_MarkScrollUp:
3713 edit_move_up (edit, 1, 1);
3714 break;
3715 case CK_MarkColumnScrollDown:
3716 edit->column_highlight = 1;
3717 case CK_ScrollDown:
3718 case CK_MarkScrollDown:
3719 edit_move_down (edit, 1, 1);
3720 break;
3721 case CK_Home:
3722 case CK_MarkToHome:
3723 edit_cursor_to_bol (edit);
3724 break;
3725 case CK_End:
3726 case CK_MarkToEnd:
3727 edit_cursor_to_eol (edit);
3728 break;
3729 case CK_Tab:
3730 /* if text marked shift block */
3731 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3733 if (edit->mark2 < 0)
3734 edit_mark_cmd (edit, FALSE);
3735 edit_move_block_to_right (edit);
3737 else
3739 if (option_cursor_beyond_eol)
3740 edit_insert_over (edit);
3741 edit_tab_cmd (edit);
3742 if (option_auto_para_formatting)
3744 format_paragraph (edit, 0);
3745 edit->force |= REDRAW_PAGE;
3747 else
3748 check_and_wrap_line (edit);
3750 break;
3752 case CK_InsertOverwrite:
3753 edit->overwrite = !edit->overwrite;
3754 break;
3756 case CK_Mark:
3757 if (edit->mark2 >= 0)
3759 if (edit->column_highlight)
3760 edit_push_undo_action (edit, COLUMN_ON);
3761 edit->column_highlight = 0;
3763 edit_mark_cmd (edit, FALSE);
3764 break;
3765 case CK_MarkColumn:
3766 if (!edit->column_highlight)
3767 edit_push_undo_action (edit, COLUMN_OFF);
3768 edit->column_highlight = 1;
3769 edit_mark_cmd (edit, FALSE);
3770 break;
3771 case CK_MarkAll:
3772 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3773 edit->force |= REDRAW_PAGE;
3774 break;
3775 case CK_Unmark:
3776 if (edit->column_highlight)
3777 edit_push_undo_action (edit, COLUMN_ON);
3778 edit->column_highlight = 0;
3779 edit_mark_cmd (edit, TRUE);
3780 break;
3781 case CK_MarkWord:
3782 if (edit->column_highlight)
3783 edit_push_undo_action (edit, COLUMN_ON);
3784 edit->column_highlight = 0;
3785 edit_mark_current_word_cmd (edit);
3786 break;
3787 case CK_MarkLine:
3788 if (edit->column_highlight)
3789 edit_push_undo_action (edit, COLUMN_ON);
3790 edit->column_highlight = 0;
3791 edit_mark_current_line_cmd (edit);
3792 break;
3794 case CK_Bookmark:
3795 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3796 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3797 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3798 else
3799 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3800 break;
3801 case CK_BookmarkFlush:
3802 book_mark_flush (edit, BOOK_MARK_COLOR);
3803 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3804 edit->force |= REDRAW_PAGE;
3805 break;
3806 case CK_BookmarkNext:
3807 if (edit->book_mark != NULL)
3809 edit_book_mark_t *p;
3811 p = book_mark_find (edit, edit->curs_line);
3812 if (p->next != NULL)
3814 p = p->next;
3815 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3816 edit_move_display (edit, p->line - w->lines / 2);
3817 edit_move_to_line (edit, p->line);
3820 break;
3821 case CK_BookmarkPrev:
3822 if (edit->book_mark != NULL)
3824 edit_book_mark_t *p;
3826 p = book_mark_find (edit, edit->curs_line);
3827 while (p->line == edit->curs_line)
3828 if (p->prev != NULL)
3829 p = p->prev;
3830 if (p->line >= 0)
3832 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3833 edit_move_display (edit, p->line - w->lines / 2);
3834 edit_move_to_line (edit, p->line);
3837 break;
3839 case CK_Top:
3840 case CK_MarkToFileBegin:
3841 edit_move_to_top (edit);
3842 break;
3843 case CK_Bottom:
3844 case CK_MarkToFileEnd:
3845 edit_move_to_bottom (edit);
3846 break;
3848 case CK_Copy:
3849 if (option_cursor_beyond_eol && edit->over_col > 0)
3850 edit_insert_over (edit);
3851 edit_block_copy_cmd (edit);
3852 break;
3853 case CK_Remove:
3854 edit_block_delete_cmd (edit);
3855 break;
3856 case CK_Move:
3857 edit_block_move_cmd (edit);
3858 break;
3860 case CK_BlockShiftLeft:
3861 if (edit->mark1 != edit->mark2)
3862 edit_move_block_to_left (edit);
3863 break;
3864 case CK_BlockShiftRight:
3865 if (edit->mark1 != edit->mark2)
3866 edit_move_block_to_right (edit);
3867 break;
3868 case CK_Store:
3869 edit_copy_to_X_buf_cmd (edit);
3870 break;
3871 case CK_Cut:
3872 edit_cut_to_X_buf_cmd (edit);
3873 break;
3874 case CK_Paste:
3875 /* if non persistent selection and text selected */
3876 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3877 edit_block_delete_cmd (edit);
3878 if (option_cursor_beyond_eol && edit->over_col > 0)
3879 edit_insert_over (edit);
3880 edit_paste_from_X_buf_cmd (edit);
3881 if (!option_persistent_selections && edit->mark2 >= 0)
3883 if (edit->column_highlight)
3884 edit_push_undo_action (edit, COLUMN_ON);
3885 edit->column_highlight = 0;
3886 edit_mark_cmd (edit, TRUE);
3888 break;
3889 case CK_History:
3890 edit_paste_from_history (edit);
3891 break;
3893 case CK_SaveAs:
3894 edit_save_as_cmd (edit);
3895 break;
3896 case CK_Save:
3897 edit_save_confirm_cmd (edit);
3898 break;
3899 case CK_BlockSave:
3900 edit_save_block_cmd (edit);
3901 break;
3902 case CK_InsertFile:
3903 edit_insert_file_cmd (edit);
3904 break;
3906 case CK_FilePrev:
3907 edit_load_back_cmd (edit);
3908 break;
3909 case CK_FileNext:
3910 edit_load_forward_cmd (edit);
3911 break;
3913 case CK_SyntaxChoose:
3914 edit_syntax_dialog (edit);
3915 break;
3917 case CK_Search:
3918 edit_search_cmd (edit, FALSE);
3919 break;
3920 case CK_SearchContinue:
3921 edit_search_cmd (edit, TRUE);
3922 break;
3923 case CK_Replace:
3924 edit_replace_cmd (edit, 0);
3925 break;
3926 case CK_ReplaceContinue:
3927 edit_replace_cmd (edit, 1);
3928 break;
3929 case CK_Complete:
3930 /* if text marked shift block */
3931 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3932 edit_move_block_to_left (edit);
3933 else
3934 edit_complete_word_cmd (edit);
3935 break;
3936 case CK_Find:
3937 edit_get_match_keyword_cmd (edit);
3938 break;
3940 #ifdef HAVE_ASPELL
3941 case CK_SpellCheckCurrentWord:
3942 edit_suggest_current_word (edit);
3943 break;
3944 case CK_SpellCheck:
3945 edit_spellcheck_file (edit);
3946 break;
3947 case CK_SpellCheckSelectLang:
3948 edit_set_spell_lang ();
3949 break;
3950 #endif
3952 case CK_Date:
3954 char s[BUF_MEDIUM];
3955 /* fool gcc to prevent a Y2K warning */
3956 char time_format[] = "_c";
3957 time_format[0] = '%';
3959 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3960 edit_print_string (edit, s);
3961 edit->force |= REDRAW_PAGE;
3963 break;
3964 case CK_Goto:
3965 edit_goto_cmd (edit);
3966 break;
3967 case CK_ParagraphFormat:
3968 format_paragraph (edit, 1);
3969 edit->force |= REDRAW_PAGE;
3970 break;
3971 case CK_MacroDelete:
3972 edit_delete_macro_cmd (edit);
3973 break;
3974 case CK_MatchBracket:
3975 edit_goto_matching_bracket (edit);
3976 break;
3977 case CK_UserMenu:
3978 user_menu (edit, NULL, -1);
3979 break;
3980 case CK_Sort:
3981 edit_sort_cmd (edit);
3982 break;
3983 case CK_ExternalCommand:
3984 edit_ext_cmd (edit);
3985 break;
3986 case CK_Mail:
3987 edit_mail_dialog (edit);
3988 break;
3989 #ifdef HAVE_CHARSET
3990 case CK_SelectCodepage:
3991 edit_select_codepage_cmd (edit);
3992 break;
3993 #endif
3994 case CK_InsertLiteral:
3995 edit_insert_literal_cmd (edit);
3996 break;
3997 case CK_MacroStartStopRecord:
3998 edit_begin_end_macro_cmd (edit);
3999 break;
4000 case CK_RepeatStartStopRecord:
4001 edit_begin_end_repeat_cmd (edit);
4002 break;
4003 case CK_ExtendedKeyMap:
4004 edit->extmod = TRUE;
4005 break;
4006 default:
4007 break;
4010 /* CK_PipeBlock */
4011 if ((command / CK_PipeBlock (0)) == 1)
4012 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4014 /* keys which must set the col position, and the search vars */
4015 switch (command)
4017 case CK_Search:
4018 case CK_SearchContinue:
4019 case CK_Replace:
4020 case CK_ReplaceContinue:
4021 case CK_Complete:
4022 edit->prev_col = edit_get_col (edit);
4023 break;
4024 case CK_Up:
4025 case CK_MarkUp:
4026 case CK_MarkColumnUp:
4027 case CK_Down:
4028 case CK_MarkDown:
4029 case CK_MarkColumnDown:
4030 case CK_PageUp:
4031 case CK_MarkPageUp:
4032 case CK_MarkColumnPageUp:
4033 case CK_PageDown:
4034 case CK_MarkPageDown:
4035 case CK_MarkColumnPageDown:
4036 case CK_Top:
4037 case CK_MarkToFileBegin:
4038 case CK_Bottom:
4039 case CK_MarkToFileEnd:
4040 case CK_ParagraphUp:
4041 case CK_MarkParagraphUp:
4042 case CK_MarkColumnParagraphUp:
4043 case CK_ParagraphDown:
4044 case CK_MarkParagraphDown:
4045 case CK_MarkColumnParagraphDown:
4046 case CK_ScrollUp:
4047 case CK_MarkScrollUp:
4048 case CK_MarkColumnScrollUp:
4049 case CK_ScrollDown:
4050 case CK_MarkScrollDown:
4051 case CK_MarkColumnScrollDown:
4052 edit->search_start = edit->buffer.curs1;
4053 edit->found_len = 0;
4054 break;
4055 default:
4056 edit->found_len = 0;
4057 edit->prev_col = edit_get_col (edit);
4058 edit->search_start = edit->buffer.curs1;
4060 edit_find_bracket (edit);
4062 if (option_auto_para_formatting)
4064 switch (command)
4066 case CK_BackSpace:
4067 case CK_Delete:
4068 case CK_DeleteToWordBegin:
4069 case CK_DeleteToWordEnd:
4070 case CK_DeleteToHome:
4071 case CK_DeleteToEnd:
4072 format_paragraph (edit, 0);
4073 edit->force |= REDRAW_PAGE;
4078 /* --------------------------------------------------------------------------------------------- */
4080 void
4081 edit_stack_init (void)
4083 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4085 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4086 edit_history_moveto[edit_stack_iterator].line = -1;
4089 edit_stack_iterator = 0;
4092 /* --------------------------------------------------------------------------------------------- */
4094 void
4095 edit_stack_free (void)
4097 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4098 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4101 /* --------------------------------------------------------------------------------------------- */
4102 /** move i lines */
4104 void
4105 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4107 edit_move_updown (edit, i, do_scroll, TRUE);
4110 /* --------------------------------------------------------------------------------------------- */
4111 /** move i lines */
4113 void
4114 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4116 edit_move_updown (edit, i, do_scroll, FALSE);
4119 /* --------------------------------------------------------------------------------------------- */