mceditor: last_bracket: make member of WEdit.
[midnight-commander.git] / src / editor / edit.c
blob113dcaf2faa194b1ef530de4db5916aea8c17e4a
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 /*** file scope functions ************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
143 * here's a quick sketch of the layout: (don't run this through indent.)
145 * (b1 is buffers1 and b2 is buffers2)
148 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
149 * ______________________________________|______________________________________
151 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
152 * |-> |-> |-> |-> |-> |-> |
154 * _<------------------------->|<----------------->_
155 * WEdit->curs2 | WEdit->curs1
156 * ^ | ^
157 * | ^|^ |
158 * cursor ||| cursor
159 * |||
160 * file end|||file beginning
165 * This_is_some_file
166 * fin.
169 /* --------------------------------------------------------------------------------------------- */
171 * Initialize the buffers for an empty files.
174 static void
175 edit_init_buffers (WEdit * edit)
177 int j;
179 for (j = 0; j <= MAXBUFF; j++)
181 edit->buffers1[j] = NULL;
182 edit->buffers2[j] = NULL;
185 edit->curs1 = 0;
186 edit->curs2 = 0;
187 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
190 /* --------------------------------------------------------------------------------------------- */
193 * Load file OR text into buffers. Set cursor to the beginning of file.
195 * @return FALSE on error.
198 static gboolean
199 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
201 off_t buf, buf2;
202 int file = -1;
203 gboolean ret = FALSE;
205 edit->curs2 = edit->last_byte;
206 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
208 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
209 if (file == -1)
211 gchar *errmsg;
213 errmsg =
214 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
215 edit_error_dialog (_("Error"), errmsg);
216 g_free (errmsg);
217 return FALSE;
220 if (!edit->buffers2[buf2])
221 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
225 if (mc_read (file,
226 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
227 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
228 break;
230 for (buf = buf2 - 1; buf >= 0; buf--)
232 /* edit->buffers2[0] is already allocated */
233 if (!edit->buffers2[buf])
234 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
235 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
236 break;
238 ret = TRUE;
240 while (FALSE);
242 if (!ret)
244 gchar *errmsg;
246 errmsg = g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath));
247 edit_error_dialog (_("Error"), errmsg);
248 g_free (errmsg);
250 mc_close (file);
251 return ret;
254 /* --------------------------------------------------------------------------------------------- */
255 /** Return index of the filter or -1 is there is no appropriate filter */
257 static int
258 edit_find_filter (const vfs_path_t * filename_vpath)
260 size_t i, l, e;
262 if (filename_vpath == NULL)
263 return -1;
265 l = strlen (vfs_path_as_str (filename_vpath));
266 for (i = 0; i < G_N_ELEMENTS (all_filters); i++)
268 e = strlen (all_filters[i].extension);
269 if (l > e)
270 if (!strcmp (all_filters[i].extension, vfs_path_as_str (filename_vpath) + l - e))
271 return i;
273 return -1;
276 /* --------------------------------------------------------------------------------------------- */
278 static char *
279 edit_get_filter (const vfs_path_t * filename_vpath)
281 int i;
282 char *p, *quoted_name;
284 i = edit_find_filter (filename_vpath);
285 if (i < 0)
286 return NULL;
288 quoted_name = name_quote (vfs_path_as_str (filename_vpath), 0);
289 p = g_strdup_printf (all_filters[i].read, quoted_name);
290 g_free (quoted_name);
291 return p;
294 /* --------------------------------------------------------------------------------------------- */
296 static off_t
297 edit_insert_stream (WEdit * edit, FILE * f)
299 int c;
300 off_t i = 0;
301 while ((c = fgetc (f)) >= 0)
303 edit_insert (edit, c);
304 i++;
306 return i;
309 /* --------------------------------------------------------------------------------------------- */
311 * Open file and create it if necessary.
313 * @param edit editor object
314 * @param filename_vpath file name
315 * @param st buffer for store stat info
316 * @return TRUE for success, FALSE for error.
319 static gboolean
320 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
322 int file;
323 gchar *errmsg = NULL;
325 /* Try opening an existing file */
326 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
327 if (file < 0)
330 * Try creating the file. O_EXCL prevents following broken links
331 * and opening existing files.
333 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
334 if (file < 0)
336 errmsg =
337 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
338 goto cleanup;
341 /* New file, delete it if it's not modified or saved */
342 edit->delete_file = 1;
345 /* Check what we have opened */
346 if (mc_fstat (file, st) < 0)
348 errmsg =
349 g_strdup_printf (_("Cannot get size/permissions for %s"),
350 vfs_path_as_str (filename_vpath));
351 goto cleanup;
354 /* We want to open regular files only */
355 if (!S_ISREG (st->st_mode))
357 errmsg =
358 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath));
359 goto cleanup;
363 * Don't delete non-empty files.
364 * O_EXCL should prevent it, but let's be on the safe side.
366 if (st->st_size > 0)
367 edit->delete_file = 0;
369 if (st->st_size >= SIZE_LIMIT)
370 errmsg = g_strdup_printf (_("File \"%s\" is too large"), vfs_path_as_str (filename_vpath));
372 cleanup:
373 (void) mc_close (file);
375 if (errmsg != NULL)
377 edit_error_dialog (_("Error"), errmsg);
378 g_free (errmsg);
379 return FALSE;
381 return TRUE;
384 /* --------------------------------------------------------------------------------------------- */
387 * Open the file and load it into the buffers, either directly or using
388 * a filter. Return TRUE on success, FALSE on error.
390 * Fast loading (edit_load_file_fast) is used when the file size is
391 * known. In this case the data is read into the buffers by blocks.
392 * If the file size is not known, the data is loaded byte by byte in
393 * edit_insert_file.
395 * @param edit editor object
396 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
398 static gboolean
399 edit_load_file (WEdit * edit)
401 gboolean fast_load = TRUE;
403 /* Cannot do fast load if a filter is used */
404 if (edit_find_filter (edit->filename_vpath) >= 0)
405 fast_load = FALSE;
408 * FIXME: line end translation should disable fast loading as well
409 * Consider doing fseek() to the end and ftell() for the real size.
411 if (edit->filename_vpath != NULL)
414 * VFS may report file size incorrectly, and slow load is not a big
415 * deal considering overhead in VFS.
417 if (!vfs_file_is_local (edit->filename_vpath))
418 fast_load = FALSE;
420 /* If we are dealing with a real file, check that it exists */
421 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
423 edit_clean (edit);
424 return FALSE;
427 else
429 /* nothing to load */
430 fast_load = FALSE;
433 edit_init_buffers (edit);
435 if (fast_load)
437 edit->last_byte = edit->stat1.st_size;
438 edit_load_file_fast (edit, edit->filename_vpath);
439 /* If fast load was used, the number of lines wasn't calculated */
440 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
442 else
444 edit->last_byte = 0;
445 if (edit->filename_vpath != NULL
446 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
448 edit->undo_stack_disable = 1;
449 if (edit_insert_file (edit, edit->filename_vpath) < 0)
451 edit_clean (edit);
452 return FALSE;
454 edit->undo_stack_disable = 0;
457 edit->lb = LB_ASIS;
458 return TRUE;
461 /* --------------------------------------------------------------------------------------------- */
462 /** Restore saved cursor position in the file */
464 static void
465 edit_load_position (WEdit * edit)
467 long line, column;
468 off_t offset;
470 if (edit->filename_vpath == NULL
471 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
472 return;
474 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
476 if (line > 0)
478 edit_move_to_line (edit, line - 1);
479 edit->prev_col = column;
481 else if (offset > 0)
483 edit_cursor_move (edit, offset);
484 line = edit->curs_line;
485 edit->search_start = edit->curs1;
488 book_mark_restore (edit, BOOK_MARK_COLOR);
490 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
491 edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
494 /* --------------------------------------------------------------------------------------------- */
495 /** Save cursor position in the file */
497 static void
498 edit_save_position (WEdit * edit)
500 if (edit->filename_vpath == NULL
501 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
502 return;
504 book_mark_serialize (edit, BOOK_MARK_COLOR);
505 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
506 edit->serialized_bookmarks);
507 edit->serialized_bookmarks = NULL;
510 /* --------------------------------------------------------------------------------------------- */
511 /** Clean the WEdit stricture except the widget part */
513 static void
514 edit_purge_widget (WEdit * edit)
516 size_t len = sizeof (WEdit) - sizeof (Widget);
517 char *start = (char *) edit + sizeof (Widget);
518 memset (start, 0, len);
521 /* --------------------------------------------------------------------------------------------- */
524 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
525 then the file should be as it was when he loaded up. Then set edit->modified to 0.
528 static long
529 edit_pop_undo_action (WEdit * edit)
531 long c;
532 unsigned long sp = edit->undo_stack_pointer;
534 if (sp == edit->undo_stack_bottom)
535 return STACK_BOTTOM;
537 sp = (sp - 1) & edit->undo_stack_size_mask;
538 c = edit->undo_stack[sp];
539 if (c >= 0)
541 /* edit->undo_stack[sp] = '@'; */
542 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
543 return c;
546 if (sp == edit->undo_stack_bottom)
547 return STACK_BOTTOM;
549 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
550 if (edit->undo_stack[sp] == -2)
552 /* edit->undo_stack[sp] = '@'; */
553 edit->undo_stack_pointer = sp;
555 else
556 edit->undo_stack[sp]++;
558 return c;
561 static long
562 edit_pop_redo_action (WEdit * edit)
564 long c;
565 unsigned long sp = edit->redo_stack_pointer;
567 if (sp == edit->redo_stack_bottom)
568 return STACK_BOTTOM;
570 sp = (sp - 1) & edit->redo_stack_size_mask;
571 c = edit->redo_stack[sp];
572 if (c >= 0)
574 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
575 return c;
578 if (sp == edit->redo_stack_bottom)
579 return STACK_BOTTOM;
581 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
582 if (edit->redo_stack[sp] == -2)
583 edit->redo_stack_pointer = sp;
584 else
585 edit->redo_stack[sp]++;
587 return c;
590 static long
591 get_prev_undo_action (WEdit * edit)
593 long c;
594 unsigned long sp = edit->undo_stack_pointer;
596 if (sp == edit->undo_stack_bottom)
597 return STACK_BOTTOM;
599 sp = (sp - 1) & edit->undo_stack_size_mask;
600 c = edit->undo_stack[sp];
601 if (c >= 0)
602 return c;
604 if (sp == edit->undo_stack_bottom)
605 return STACK_BOTTOM;
607 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
608 return c;
611 /* --------------------------------------------------------------------------------------------- */
612 /** is called whenever a modification is made by one of the four routines below */
614 static void
615 edit_modification (WEdit * edit)
617 edit->caches_valid = FALSE;
619 /* raise lock when file modified */
620 if (!edit->modified && !edit->delete_file)
621 edit->locked = lock_file (edit->filename_vpath);
622 edit->modified = 1;
625 /* --------------------------------------------------------------------------------------------- */
627 static char *
628 edit_get_byte_ptr (const WEdit * edit, off_t byte_index)
630 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
631 return NULL;
633 if (byte_index >= edit->curs1)
635 off_t p;
637 p = edit->curs1 + edit->curs2 - byte_index - 1;
638 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
639 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
642 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
643 (byte_index & M_EDIT_BUF_SIZE));
646 /* --------------------------------------------------------------------------------------------- */
648 #ifdef HAVE_CHARSET
649 static int
650 edit_get_prev_utf (const WEdit * edit, off_t byte_index, int *char_width)
652 int i, res;
653 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
654 gchar *str;
655 gchar *cursor_buf_ptr;
657 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
659 *char_width = 0;
660 return 0;
663 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
664 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
665 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
667 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
668 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
670 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
672 *char_width = 1;
673 return *(cursor_buf_ptr - 1);
675 else
677 res = g_utf8_get_char_validated (str, -1);
679 if (res < 0)
681 *char_width = 1;
682 return *(cursor_buf_ptr - 1);
684 else
686 *char_width = cursor_buf_ptr - str;
687 return res;
691 #endif
693 /* --------------------------------------------------------------------------------------------- */
694 /* high level cursor movement commands */
695 /* --------------------------------------------------------------------------------------------- */
696 /** check whether cursor is in indent part of line
698 * @param edit editor object
700 * @return TRUE if cursor is in indent, FALSE otherwise
703 static gboolean
704 is_in_indent (const WEdit * edit)
706 off_t p;
708 for (p = edit_bol (edit, edit->curs1); p < edit->curs1; p++)
709 if (strchr (" \t", edit_get_byte (edit, p)) == NULL)
710 return FALSE;
712 return TRUE;
715 /* --------------------------------------------------------------------------------------------- */
716 /** check whether line in editor is blank or not
718 * @param edit editor object
719 * @param offset position in file
721 * @return TRUE if line in blank, FALSE otherwise
724 static gboolean
725 is_blank (const WEdit * edit, off_t offset)
727 off_t s, f;
728 int c;
730 s = edit_bol (edit, offset);
731 f = edit_eol (edit, offset) - 1;
732 while (s <= f)
734 c = edit_get_byte (edit, s++);
735 if (!isspace (c))
736 return FALSE;
738 return TRUE;
741 /* --------------------------------------------------------------------------------------------- */
742 /** returns the offset of line i */
744 static off_t
745 edit_find_line (WEdit * edit, long line)
747 long i, j = 0;
748 long m = 2000000000; /* what is the magic number? */
750 if (!edit->caches_valid)
752 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
753 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
754 /* three offsets that we *know* are line 0 at 0 and these two: */
755 edit->line_numbers[1] = edit->curs_line;
756 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
757 edit->line_numbers[2] = edit->total_lines;
758 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
759 edit->caches_valid = TRUE;
761 if (line >= edit->total_lines)
762 return edit->line_offsets[2];
763 if (line <= 0)
764 return 0;
765 /* find the closest known point */
766 for (i = 0; i < N_LINE_CACHES; i++)
768 long n;
770 n = abs (edit->line_numbers[i] - line);
771 if (n < m)
773 m = n;
774 j = i;
777 if (m == 0)
778 return edit->line_offsets[j]; /* know the offset exactly */
779 if (m == 1 && j >= 3)
780 i = j; /* one line different - caller might be looping, so stay in this cache */
781 else
782 i = 3 + (rand () % (N_LINE_CACHES - 3));
783 if (line > edit->line_numbers[j])
784 edit->line_offsets[i] =
785 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
786 else
787 edit->line_offsets[i] =
788 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
789 edit->line_numbers[i] = line;
790 return edit->line_offsets[i];
793 /* --------------------------------------------------------------------------------------------- */
794 /** moves up until a blank line is reached, or until just
795 before a non-blank line is reached */
797 static void
798 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
800 long i = 0;
802 if (edit->curs_line > 1)
804 if (!edit_line_is_blank (edit, edit->curs_line))
806 for (i = edit->curs_line - 1; i != 0; i--)
807 if (edit_line_is_blank (edit, i))
808 break;
810 else if (edit_line_is_blank (edit, edit->curs_line - 1))
812 for (i = edit->curs_line - 1; i != 0; i--)
813 if (!edit_line_is_blank (edit, i))
815 i++;
816 break;
819 else
821 for (i = edit->curs_line - 1; i != 0; i--)
822 if (edit_line_is_blank (edit, i))
823 break;
827 edit_move_up (edit, edit->curs_line - i, do_scroll);
830 /* --------------------------------------------------------------------------------------------- */
831 /** moves down until a blank line is reached, or until just
832 before a non-blank line is reached */
834 static void
835 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
837 long i;
839 if (edit->curs_line >= edit->total_lines - 1)
840 i = edit->total_lines;
841 else if (!edit_line_is_blank (edit, edit->curs_line))
843 for (i = edit->curs_line + 1; i != 0; i++)
844 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
845 break;
847 else if (edit_line_is_blank (edit, edit->curs_line + 1))
849 for (i = edit->curs_line + 1; i != 0; i++)
850 if (!edit_line_is_blank (edit, i) || i > edit->total_lines)
852 i--;
853 break;
856 else
858 for (i = edit->curs_line + 1; i != 0; i++)
859 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
860 break;
862 edit_move_down (edit, i - edit->curs_line, do_scroll);
865 /* --------------------------------------------------------------------------------------------- */
867 static void
868 edit_begin_page (WEdit * edit)
870 edit_update_curs_row (edit);
871 edit_move_up (edit, edit->curs_row, 0);
874 /* --------------------------------------------------------------------------------------------- */
876 static void
877 edit_end_page (WEdit * edit)
879 edit_update_curs_row (edit);
880 edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, 0);
884 /* --------------------------------------------------------------------------------------------- */
885 /** goto beginning of text */
887 static void
888 edit_move_to_top (WEdit * edit)
890 if (edit->curs_line)
892 edit_cursor_move (edit, -edit->curs1);
893 edit_move_to_prev_col (edit, 0);
894 edit->force |= REDRAW_PAGE;
895 edit->search_start = 0;
896 edit_update_curs_row (edit);
901 /* --------------------------------------------------------------------------------------------- */
902 /** goto end of text */
904 static void
905 edit_move_to_bottom (WEdit * edit)
907 if (edit->curs_line < edit->total_lines)
909 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
910 edit->start_display = edit->last_byte;
911 edit->start_line = edit->total_lines;
912 edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
913 edit->force |= REDRAW_PAGE;
917 /* --------------------------------------------------------------------------------------------- */
918 /** goto beginning of line */
920 static void
921 edit_cursor_to_bol (WEdit * edit)
923 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
924 edit->search_start = edit->curs1;
925 edit->prev_col = edit_get_col (edit);
926 edit->over_col = 0;
929 /* --------------------------------------------------------------------------------------------- */
930 /** goto end of line */
932 static void
933 edit_cursor_to_eol (WEdit * edit)
935 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
936 edit->search_start = edit->curs1;
937 edit->prev_col = edit_get_col (edit);
938 edit->over_col = 0;
941 /* --------------------------------------------------------------------------------------------- */
943 static unsigned long
944 my_type_of (int c)
946 int x, r = 0;
947 const char *p, *q;
948 const char option_chars_move_whole_word[] =
949 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
951 if (c == 0)
952 return 0;
953 if (c == '!')
955 if (*option_chars_move_whole_word == '!')
956 return 2;
957 return 0x80000000UL;
959 if (g_ascii_isupper ((gchar) c))
960 c = 'A';
961 else if (g_ascii_islower ((gchar) c))
962 c = 'a';
963 else if (g_ascii_isalpha (c))
964 c = 'a';
965 else if (isdigit (c))
966 c = '0';
967 else if (isspace (c))
968 c = ' ';
969 q = strchr (option_chars_move_whole_word, c);
970 if (!q)
971 return 0xFFFFFFFFUL;
974 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
975 if (*p == '!')
976 x <<= 1;
977 r |= x;
979 while ((q = strchr (q + 1, c)));
980 return r;
983 /* --------------------------------------------------------------------------------------------- */
985 static void
986 edit_left_word_move (WEdit * edit, int s)
988 while (TRUE)
990 int c1, c2;
992 if (edit->column_highlight
993 && edit->mark1 != edit->mark2
994 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
995 break;
996 edit_cursor_move (edit, -1);
997 if (edit->curs1 == 0)
998 break;
999 c1 = edit_get_byte (edit, edit->curs1 - 1);
1000 c2 = edit_get_byte (edit, edit->curs1);
1001 if (c1 == '\n' || c2 == '\n')
1002 break;
1003 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1004 break;
1005 if (isspace (c1) && !isspace (c2))
1006 break;
1007 if (s != 0 && !isspace (c1) && isspace (c2))
1008 break;
1012 /* --------------------------------------------------------------------------------------------- */
1014 static void
1015 edit_left_word_move_cmd (WEdit * edit)
1017 edit_left_word_move (edit, 0);
1018 edit->force |= REDRAW_PAGE;
1021 /* --------------------------------------------------------------------------------------------- */
1023 static void
1024 edit_right_word_move (WEdit * edit, int s)
1026 while (TRUE)
1028 int c1, c2;
1030 if (edit->column_highlight
1031 && edit->mark1 != edit->mark2
1032 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1033 break;
1034 edit_cursor_move (edit, 1);
1035 if (edit->curs1 >= edit->last_byte)
1036 break;
1037 c1 = edit_get_byte (edit, edit->curs1 - 1);
1038 c2 = edit_get_byte (edit, edit->curs1);
1039 if (c1 == '\n' || c2 == '\n')
1040 break;
1041 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1042 break;
1043 if (isspace (c1) && !isspace (c2))
1044 break;
1045 if (s != 0 && !isspace (c1) && isspace (c2))
1046 break;
1050 /* --------------------------------------------------------------------------------------------- */
1052 static void
1053 edit_right_word_move_cmd (WEdit * edit)
1055 edit_right_word_move (edit, 0);
1056 edit->force |= REDRAW_PAGE;
1059 /* --------------------------------------------------------------------------------------------- */
1061 static void
1062 edit_right_char_move_cmd (WEdit * edit)
1064 int cw = 1;
1065 int c = 0;
1066 #ifdef HAVE_CHARSET
1067 if (edit->utf8)
1069 c = edit_get_utf (edit, edit->curs1, &cw);
1070 if (cw < 1)
1071 cw = 1;
1073 else
1074 #endif
1076 c = edit_get_byte (edit, edit->curs1);
1078 if (option_cursor_beyond_eol && c == '\n')
1080 edit->over_col++;
1082 else
1084 edit_cursor_move (edit, cw);
1088 /* --------------------------------------------------------------------------------------------- */
1090 static void
1091 edit_left_char_move_cmd (WEdit * edit)
1093 int cw = 1;
1094 if (edit->column_highlight
1095 && option_cursor_beyond_eol
1096 && edit->mark1 != edit->mark2
1097 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1098 return;
1099 #ifdef HAVE_CHARSET
1100 if (edit->utf8)
1102 edit_get_prev_utf (edit, edit->curs1, &cw);
1103 if (cw < 1)
1104 cw = 1;
1106 #endif
1107 if (option_cursor_beyond_eol && edit->over_col > 0)
1109 edit->over_col--;
1111 else
1113 edit_cursor_move (edit, -cw);
1117 /* --------------------------------------------------------------------------------------------- */
1118 /** Up or down cursor moving.
1119 direction = TRUE - move up
1120 = FALSE - move down
1123 static void
1124 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1126 long p;
1127 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1129 if (lines > l)
1130 lines = l;
1132 if (lines == 0)
1133 return;
1135 if (lines > 1)
1136 edit->force |= REDRAW_PAGE;
1137 if (do_scroll)
1139 if (direction)
1140 edit_scroll_upward (edit, lines);
1141 else
1142 edit_scroll_downward (edit, lines);
1144 p = edit_bol (edit, edit->curs1);
1146 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1148 edit_cursor_move (edit, p - edit->curs1);
1150 edit_move_to_prev_col (edit, p);
1152 /* search start of current multibyte char (like CJK) */
1153 if (edit->curs1 + 1 < edit->last_byte)
1155 edit_right_char_move_cmd (edit);
1156 edit_left_char_move_cmd (edit);
1159 edit->search_start = edit->curs1;
1160 edit->found_len = 0;
1163 /* --------------------------------------------------------------------------------------------- */
1165 static void
1166 edit_right_delete_word (WEdit * edit)
1168 while (edit->curs1 < edit->last_byte)
1170 int c1, c2;
1172 c1 = edit_delete (edit, TRUE);
1173 c2 = edit_get_byte (edit, edit->curs1);
1174 if (c1 == '\n' || c2 == '\n')
1175 break;
1176 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1177 break;
1178 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1179 break;
1183 /* --------------------------------------------------------------------------------------------- */
1185 static void
1186 edit_left_delete_word (WEdit * edit)
1188 while (edit->curs1 > 0)
1190 int c1, c2;
1192 c1 = edit_backspace (edit, TRUE);
1193 c2 = edit_get_byte (edit, edit->curs1 - 1);
1194 if (c1 == '\n' || c2 == '\n')
1195 break;
1196 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1197 break;
1198 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1199 break;
1203 /* --------------------------------------------------------------------------------------------- */
1205 the start column position is not recorded, and hence does not
1206 undo as it happed. But who would notice.
1209 static void
1210 edit_do_undo (WEdit * edit)
1212 long ac;
1213 long count = 0;
1215 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1216 edit->over_col = 0;
1217 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1219 switch ((int) ac)
1221 case STACK_BOTTOM:
1222 goto done_undo;
1223 case CURS_RIGHT:
1224 edit_cursor_move (edit, 1);
1225 break;
1226 case CURS_LEFT:
1227 edit_cursor_move (edit, -1);
1228 break;
1229 case BACKSPACE:
1230 case BACKSPACE_BR:
1231 edit_backspace (edit, TRUE);
1232 break;
1233 case DELCHAR:
1234 case DELCHAR_BR:
1235 edit_delete (edit, TRUE);
1236 break;
1237 case COLUMN_ON:
1238 edit->column_highlight = 1;
1239 break;
1240 case COLUMN_OFF:
1241 edit->column_highlight = 0;
1242 break;
1244 if (ac >= 256 && ac < 512)
1245 edit_insert_ahead (edit, ac - 256);
1246 if (ac >= 0 && ac < 256)
1247 edit_insert (edit, ac);
1249 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1251 edit->mark1 = ac - MARK_1;
1252 edit->column1 =
1253 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1255 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1257 edit->mark2 = ac - MARK_2;
1258 edit->column2 =
1259 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1261 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1263 edit->end_mark_curs = ac - MARK_CURS;
1265 if (count++)
1266 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1269 if (edit->start_display > ac - KEY_PRESS)
1271 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1272 edit->force |= REDRAW_PAGE;
1274 else if (edit->start_display < ac - KEY_PRESS)
1276 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1277 edit->force |= REDRAW_PAGE;
1279 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1280 edit_update_curs_row (edit);
1282 done_undo:
1283 edit->undo_stack_disable = 0;
1286 /* --------------------------------------------------------------------------------------------- */
1288 static void
1289 edit_do_redo (WEdit * edit)
1291 long ac;
1292 long count = 0;
1294 if (edit->redo_stack_reset)
1295 return;
1297 edit->over_col = 0;
1298 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1300 switch ((int) ac)
1302 case STACK_BOTTOM:
1303 goto done_redo;
1304 case CURS_RIGHT:
1305 edit_cursor_move (edit, 1);
1306 break;
1307 case CURS_LEFT:
1308 edit_cursor_move (edit, -1);
1309 break;
1310 case BACKSPACE:
1311 edit_backspace (edit, TRUE);
1312 break;
1313 case DELCHAR:
1314 edit_delete (edit, TRUE);
1315 break;
1316 case COLUMN_ON:
1317 edit->column_highlight = 1;
1318 break;
1319 case COLUMN_OFF:
1320 edit->column_highlight = 0;
1321 break;
1323 if (ac >= 256 && ac < 512)
1324 edit_insert_ahead (edit, ac - 256);
1325 if (ac >= 0 && ac < 256)
1326 edit_insert (edit, ac);
1328 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1330 edit->mark1 = ac - MARK_1;
1331 edit->column1 =
1332 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1334 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1336 edit->mark2 = ac - MARK_2;
1337 edit->column2 =
1338 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1340 /* more than one pop usually means something big */
1341 if (count++)
1342 edit->force |= REDRAW_PAGE;
1345 if (edit->start_display > ac - KEY_PRESS)
1347 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1348 edit->force |= REDRAW_PAGE;
1350 else if (edit->start_display < ac - KEY_PRESS)
1352 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1353 edit->force |= REDRAW_PAGE;
1355 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1356 edit_update_curs_row (edit);
1358 done_redo:
1362 /* --------------------------------------------------------------------------------------------- */
1364 static void
1365 edit_group_undo (WEdit * edit)
1367 long ac = KEY_PRESS;
1368 long cur_ac = KEY_PRESS;
1369 while (ac != STACK_BOTTOM && ac == cur_ac)
1371 cur_ac = get_prev_undo_action (edit);
1372 edit_do_undo (edit);
1373 ac = get_prev_undo_action (edit);
1374 /* exit from cycle if option_group_undo is not set,
1375 * and make single UNDO operation
1377 if (!option_group_undo)
1378 ac = STACK_BOTTOM;
1382 /* --------------------------------------------------------------------------------------------- */
1384 static void
1385 edit_delete_to_line_end (WEdit * edit)
1387 while (edit_get_byte (edit, edit->curs1) != '\n' && edit->curs2 != 0)
1388 edit_delete (edit, TRUE);
1391 /* --------------------------------------------------------------------------------------------- */
1393 static void
1394 edit_delete_to_line_begin (WEdit * edit)
1396 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 != 0)
1397 edit_backspace (edit, TRUE);
1400 /* --------------------------------------------------------------------------------------------- */
1402 static gboolean
1403 is_aligned_on_a_tab (WEdit * edit)
1405 long curs_col;
1407 edit_update_curs_col (edit);
1408 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1409 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1412 /* --------------------------------------------------------------------------------------------- */
1414 static gboolean
1415 right_of_four_spaces (WEdit * edit)
1417 int i, ch = 0;
1419 for (i = 1; i <= HALF_TAB_SIZE; i++)
1420 ch |= edit_get_byte (edit, edit->curs1 - i);
1422 return (ch == ' ' && is_aligned_on_a_tab (edit));
1425 /* --------------------------------------------------------------------------------------------- */
1427 static gboolean
1428 left_of_four_spaces (WEdit * edit)
1430 int i, ch = 0;
1432 for (i = 0; i < HALF_TAB_SIZE; i++)
1433 ch |= edit_get_byte (edit, edit->curs1 + i);
1435 return (ch == ' ' && is_aligned_on_a_tab (edit));
1438 /* --------------------------------------------------------------------------------------------- */
1440 static void
1441 edit_auto_indent (WEdit * edit)
1443 off_t p;
1444 char c;
1446 p = edit->curs1;
1447 /* use the previous line as a template */
1448 p = edit_move_backward (edit, p, 1);
1449 /* copy the leading whitespace of the line */
1450 while (TRUE)
1451 { /* no range check - the line _is_ \n-terminated */
1452 c = edit_get_byte (edit, p++);
1453 if (c != ' ' && c != '\t')
1454 break;
1455 edit_insert (edit, c);
1459 /* --------------------------------------------------------------------------------------------- */
1461 static inline void
1462 edit_double_newline (WEdit * edit)
1464 edit_insert (edit, '\n');
1465 if (edit_get_byte (edit, edit->curs1) == '\n' || edit_get_byte (edit, edit->curs1 - 2) == '\n')
1466 return;
1467 edit->force |= REDRAW_PAGE;
1468 edit_insert (edit, '\n');
1471 /* --------------------------------------------------------------------------------------------- */
1473 static void
1474 insert_spaces_tab (WEdit * edit, gboolean half)
1476 long i;
1478 edit_update_curs_col (edit);
1479 i = option_tab_spacing * space_width;
1480 if (half)
1481 i /= 2;
1482 if (i != 0)
1484 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1485 while (i > 0)
1487 edit_insert (edit, ' ');
1488 i -= space_width;
1493 /* --------------------------------------------------------------------------------------------- */
1495 static inline void
1496 edit_tab_cmd (WEdit * edit)
1498 if (option_fake_half_tabs && is_in_indent (edit))
1500 /* insert a half tab (usually four spaces) unless there is a
1501 half tab already behind, then delete it and insert a
1502 full tab. */
1503 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1504 insert_spaces_tab (edit, TRUE);
1505 else
1507 int i;
1509 for (i = 1; i <= HALF_TAB_SIZE; i++)
1510 edit_backspace (edit, TRUE);
1511 edit_insert (edit, '\t');
1514 else if (option_fill_tabs_with_spaces)
1515 insert_spaces_tab (edit, FALSE);
1516 else
1517 edit_insert (edit, '\t');
1520 /* --------------------------------------------------------------------------------------------- */
1522 static void
1523 check_and_wrap_line (WEdit * edit)
1525 off_t curs;
1526 int c;
1528 if (!option_typewriter_wrap)
1529 return;
1530 edit_update_curs_col (edit);
1531 if (edit->curs_col < option_word_wrap_line_length)
1532 return;
1533 curs = edit->curs1;
1534 while (TRUE)
1536 curs--;
1537 c = edit_get_byte (edit, curs);
1538 if (c == '\n' || curs <= 0)
1540 edit_insert (edit, '\n');
1541 return;
1543 if (c == ' ' || c == '\t')
1545 off_t current = edit->curs1;
1546 edit_cursor_move (edit, curs - edit->curs1 + 1);
1547 edit_insert (edit, '\n');
1548 edit_cursor_move (edit, current - edit->curs1 + 1);
1549 return;
1554 /* --------------------------------------------------------------------------------------------- */
1555 /** this find the matching bracket in either direction, and sets edit->bracket
1557 * @param edit editor object
1558 * @param in_screen seach only on the current screen
1559 * @param furthest_bracket_search count of the bytes for search
1561 * @return position of the found bracket (-1 if no match)
1564 static off_t
1565 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1567 const char *const b = "{}{[][()(", *p;
1568 int i = 1, a, inc = -1, c, d, n = 0;
1569 unsigned long j = 0;
1570 off_t q;
1571 edit_update_curs_row (edit);
1572 c = edit_get_byte (edit, edit->curs1);
1573 p = strchr (b, c);
1574 /* no limit */
1575 if (!furthest_bracket_search)
1576 furthest_bracket_search--;
1577 /* not on a bracket at all */
1578 if (p == NULL)
1579 return -1;
1580 /* the matching bracket */
1581 d = p[1];
1582 /* going left or right? */
1583 if (strchr ("{[(", c))
1584 inc = 1;
1585 for (q = edit->curs1 + inc;; q += inc)
1587 /* out of buffer? */
1588 if (q >= edit->last_byte || q < 0)
1589 break;
1590 a = edit_get_byte (edit, q);
1591 /* don't want to eat CPU */
1592 if (j++ > furthest_bracket_search)
1593 break;
1594 /* out of screen? */
1595 if (in_screen)
1597 if (q < edit->start_display)
1598 break;
1599 /* count lines if searching downward */
1600 if (inc > 0 && a == '\n')
1601 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1602 break;
1604 /* count bracket depth */
1605 i += (a == c) - (a == d);
1606 /* return if bracket depth is zero */
1607 if (i == 0)
1608 return q;
1610 /* no match */
1611 return -1;
1614 /* --------------------------------------------------------------------------------------------- */
1616 static inline void
1617 edit_goto_matching_bracket (WEdit * edit)
1619 off_t q;
1621 q = edit_get_bracket (edit, 0, 0);
1622 if (q >= 0)
1624 edit->bracket = edit->curs1;
1625 edit->force |= REDRAW_PAGE;
1626 edit_cursor_move (edit, q - edit->curs1);
1630 /* --------------------------------------------------------------------------------------------- */
1632 static void
1633 edit_move_block_to_right (WEdit * edit)
1635 off_t start_mark, end_mark;
1636 long cur_bol, start_bol;
1638 if (eval_marks (edit, &start_mark, &end_mark))
1639 return;
1641 start_bol = edit_bol (edit, start_mark);
1642 cur_bol = edit_bol (edit, end_mark - 1);
1646 edit_cursor_move (edit, cur_bol - edit->curs1);
1647 if (!edit_line_is_blank (edit, edit->curs_line))
1649 if (option_fill_tabs_with_spaces)
1650 insert_spaces_tab (edit, option_fake_half_tabs);
1651 else
1652 edit_insert (edit, '\t');
1653 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1656 if (cur_bol == 0)
1657 break;
1659 cur_bol = edit_bol (edit, cur_bol - 1);
1661 while (cur_bol >= start_bol);
1663 edit->force |= REDRAW_PAGE;
1666 /* --------------------------------------------------------------------------------------------- */
1668 static void
1669 edit_move_block_to_left (WEdit * edit)
1671 off_t start_mark, end_mark;
1672 off_t cur_bol, start_bol;
1673 int i;
1675 if (eval_marks (edit, &start_mark, &end_mark))
1676 return;
1678 start_bol = edit_bol (edit, start_mark);
1679 cur_bol = edit_bol (edit, end_mark - 1);
1683 int del_tab_width;
1684 int next_char;
1686 edit_cursor_move (edit, cur_bol - edit->curs1);
1688 if (option_fake_half_tabs)
1689 del_tab_width = HALF_TAB_SIZE;
1690 else
1691 del_tab_width = option_tab_spacing;
1693 next_char = edit_get_byte (edit, edit->curs1);
1694 if (next_char == '\t')
1695 edit_delete (edit, TRUE);
1696 else if (next_char == ' ')
1697 for (i = 1; i <= del_tab_width; i++)
1699 if (next_char == ' ')
1700 edit_delete (edit, TRUE);
1701 next_char = edit_get_byte (edit, edit->curs1);
1704 if (cur_bol == 0)
1705 break;
1707 cur_bol = edit_bol (edit, cur_bol - 1);
1709 while (cur_bol >= start_bol);
1711 edit->force |= REDRAW_PAGE;
1714 /* --------------------------------------------------------------------------------------------- */
1716 * prints at the cursor
1717 * @return number of chars printed
1720 static size_t
1721 edit_print_string (WEdit * e, const char *s)
1723 size_t i = 0;
1725 while (s[i] != '\0')
1726 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1727 e->force |= REDRAW_COMPLETELY;
1728 edit_update_screen (e);
1729 return i;
1732 /* --------------------------------------------------------------------------------------------- */
1734 static off_t
1735 edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * end_pos,
1736 long *col1, long *col2)
1738 off_t cursor;
1739 int col;
1740 off_t blocklen = -1, width = 0;
1741 unsigned char *data;
1743 cursor = edit->curs1;
1744 col = edit_get_col (edit);
1745 data = g_malloc0 (TEMP_BUF_LEN);
1747 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1749 off_t i;
1750 char *pn;
1752 pn = strchr ((char *) data, '\n');
1753 width = pn == NULL ? blocklen : pn - (char *) data;
1755 for (i = 0; i < blocklen; i++)
1757 if (data[i] != '\n')
1758 edit_insert (edit, data[i]);
1759 else
1760 { /* fill in and move to next line */
1761 long l;
1762 off_t p;
1764 if (edit_get_byte (edit, edit->curs1) != '\n')
1765 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1766 edit_insert (edit, ' ');
1768 for (p = edit->curs1;; p++)
1770 if (p == edit->last_byte)
1772 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1773 edit_insert_ahead (edit, '\n');
1774 p++;
1775 break;
1777 if (edit_get_byte (edit, p) == '\n')
1779 p++;
1780 break;
1784 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1786 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1787 edit_insert (edit, ' ');
1791 *col1 = col;
1792 *col2 = col + width;
1793 *start_pos = cursor;
1794 *end_pos = edit->curs1;
1795 edit_cursor_move (edit, cursor - edit->curs1);
1796 g_free (data);
1798 return blocklen;
1801 /* --------------------------------------------------------------------------------------------- */
1802 /*** public functions ****************************************************************************/
1803 /* --------------------------------------------------------------------------------------------- */
1805 /** User edit menu, like user menu (F2) but only in editor. */
1807 void
1808 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1810 char *block_file;
1811 int nomark;
1812 off_t curs;
1813 off_t start_mark, end_mark;
1814 struct stat status;
1815 vfs_path_t *block_file_vpath;
1817 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1818 block_file_vpath = vfs_path_from_str (block_file);
1819 curs = edit->curs1;
1820 nomark = eval_marks (edit, &start_mark, &end_mark);
1821 if (nomark == 0)
1822 edit_save_block (edit, block_file, start_mark, end_mark);
1824 /* run shell scripts from menu */
1825 if (user_menu_cmd (edit, menu_file, selected_entry)
1826 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1828 int rc = 0;
1829 FILE *fd;
1831 /* i.e. we have marked block */
1832 if (nomark == 0)
1833 rc = edit_block_delete_cmd (edit);
1835 if (rc == 0)
1837 off_t ins_len;
1839 ins_len = edit_insert_file (edit, block_file_vpath);
1840 if (nomark == 0 && ins_len > 0)
1841 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1843 /* truncate block file */
1844 fd = fopen (block_file, "w");
1845 if (fd != NULL)
1846 fclose (fd);
1848 g_free (block_file);
1849 vfs_path_free (block_file_vpath);
1851 edit_cursor_move (edit, curs - edit->curs1);
1852 edit->force |= REDRAW_PAGE;
1853 widget_redraw (WIDGET (edit));
1856 /* --------------------------------------------------------------------------------------------- */
1859 edit_get_byte (const WEdit * edit, off_t byte_index)
1861 char *p;
1863 p = edit_get_byte_ptr (edit, byte_index);
1865 return (p != NULL) ? *(unsigned char *) p : '\n';
1868 /* --------------------------------------------------------------------------------------------- */
1870 #ifdef HAVE_CHARSET
1872 edit_get_utf (const WEdit * edit, off_t byte_index, int *char_width)
1874 gchar *str = NULL;
1875 int res = -1;
1876 gunichar ch;
1877 gchar *next_ch = NULL;
1878 int width = 0;
1879 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1881 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1883 *char_width = 0;
1884 return '\n';
1887 str = edit_get_byte_ptr (edit, byte_index);
1889 if (str == NULL)
1891 *char_width = 0;
1892 return 0;
1895 res = g_utf8_get_char_validated (str, -1);
1897 if (res < 0)
1899 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1900 int i;
1901 for (i = 0; i < UTF8_CHAR_LEN; i++)
1902 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1903 utf8_buf[UTF8_CHAR_LEN] = '\0';
1904 str = utf8_buf;
1905 res = g_utf8_get_char_validated (str, -1);
1908 if (res < 0)
1910 ch = *str;
1911 width = 0;
1913 else
1915 ch = res;
1916 /* Calculate UTF-8 char width */
1917 next_ch = g_utf8_next_char (str);
1918 if (next_ch)
1920 width = next_ch - str;
1922 else
1924 ch = 0;
1925 width = 0;
1928 *char_width = width;
1929 return ch;
1931 #endif
1933 /* --------------------------------------------------------------------------------------------- */
1935 char *
1936 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1938 int i;
1939 char *p, *writename;
1940 const vfs_path_element_t *path_element;
1942 i = edit_find_filter (filename_vpath);
1943 if (i < 0)
1944 return NULL;
1946 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1947 writename = name_quote (path_element->path, 0);
1948 p = g_strdup_printf (all_filters[i].write, writename);
1949 g_free (writename);
1950 return p;
1953 /* --------------------------------------------------------------------------------------------- */
1955 * @param edit editor object
1956 * @param f value of stream file
1957 * @return the length of the file
1960 off_t
1961 edit_write_stream (WEdit * edit, FILE * f)
1963 long i;
1965 if (edit->lb == LB_ASIS)
1967 for (i = 0; i < edit->last_byte; i++)
1968 if (fputc (edit_get_byte (edit, i), f) < 0)
1969 break;
1970 return i;
1973 /* change line breaks */
1974 for (i = 0; i < edit->last_byte; i++)
1976 unsigned char c = edit_get_byte (edit, i);
1978 if (!(c == '\n' || c == '\r'))
1980 /* not line break */
1981 if (fputc (c, f) < 0)
1982 return i;
1984 else
1985 { /* (c == '\n' || c == '\r') */
1986 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1988 switch (edit->lb)
1990 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1991 /* put one line break unconditionally */
1992 if (fputc ('\n', f) < 0)
1993 return i;
1995 i++; /* 2 chars are processed */
1997 if (c == '\r' && c1 == '\n')
1998 /* Windows line break; go to the next char */
1999 break;
2001 if (c == '\r' && c1 == '\r')
2003 /* two Macintosh line breaks; put second line break */
2004 if (fputc ('\n', f) < 0)
2005 return i;
2006 break;
2009 if (fputc (c1, f) < 0)
2010 return i;
2011 break;
2013 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2014 /* put one line break unconditionally */
2015 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2016 return i;
2018 if (c == '\r' && c1 == '\n')
2019 /* Windows line break; go to the next char */
2020 i++;
2021 break;
2023 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2024 /* put one line break unconditionally */
2025 if (fputc ('\r', f) < 0)
2026 return i;
2028 i++; /* 2 chars are processed */
2030 if (c == '\r' && c1 == '\n')
2031 /* Windows line break; go to the next char */
2032 break;
2034 if (c == '\n' && c1 == '\n')
2036 /* two Windows line breaks; put second line break */
2037 if (fputc ('\r', f) < 0)
2038 return i;
2039 break;
2042 if (fputc (c1, f) < 0)
2043 return i;
2044 break;
2045 case LB_ASIS: /* default without changes */
2046 break;
2051 return edit->last_byte;
2054 /* --------------------------------------------------------------------------------------------- */
2056 gboolean
2057 is_break_char (char c)
2059 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2062 /* --------------------------------------------------------------------------------------------- */
2064 char *
2065 edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len,
2066 gsize * cut)
2068 off_t word_start;
2069 long cut_len = 0;
2070 GString *match_expr;
2071 int c1, c2;
2073 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2075 c1 = edit_get_byte (edit, word_start);
2076 c2 = edit_get_byte (edit, word_start - 1);
2078 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2079 break;
2082 match_expr = g_string_sized_new (16);
2086 c1 = edit_get_byte (edit, word_start + match_expr->len);
2087 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2088 g_string_append_c (match_expr, c1);
2090 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2092 *len = match_expr->len;
2093 *start = word_start;
2094 *cut = cut_len;
2096 return g_string_free (match_expr, FALSE);
2099 /* --------------------------------------------------------------------------------------------- */
2100 /** inserts a file at the cursor, returns count of inserted bytes on success */
2102 long
2103 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2105 char *p;
2106 off_t current;
2107 off_t ins_len = 0;
2109 p = edit_get_filter (filename_vpath);
2110 current = edit->curs1;
2112 if (p != NULL)
2114 FILE *f;
2116 f = (FILE *) popen (p, "r");
2117 if (f != NULL)
2119 edit_insert_stream (edit, f);
2121 /* Place cursor at the end of text selection */
2122 if (!option_cursor_after_inserted_block)
2124 ins_len = edit->curs1 - current;
2125 edit_cursor_move (edit, -ins_len);
2127 if (pclose (f) > 0)
2129 char *errmsg;
2131 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2132 edit_error_dialog (_("Error"), errmsg);
2133 g_free (errmsg);
2134 ins_len = -1;
2137 else
2139 char *errmsg;
2141 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2142 edit_error_dialog (_("Error"), errmsg);
2143 g_free (errmsg);
2144 ins_len = -1;
2146 g_free (p);
2148 else
2150 int file;
2151 off_t blocklen;
2152 int vertical_insertion = 0;
2153 char *buf;
2155 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2156 if (file == -1)
2157 return -1;
2159 buf = g_malloc0 (TEMP_BUF_LEN);
2160 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2161 if (blocklen > 0)
2163 /* if contain signature VERTICAL_MAGIC then it vertical block */
2164 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2165 vertical_insertion = 1;
2166 else
2167 mc_lseek (file, 0, SEEK_SET);
2170 if (vertical_insertion)
2172 off_t mark1, mark2;
2173 long c1, c2;
2175 blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2176 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2178 /* highlight inserted text then not persistent blocks */
2179 if (!option_persistent_selections && edit->modified)
2181 if (!edit->column_highlight)
2182 edit_push_undo_action (edit, COLUMN_OFF);
2183 edit->column_highlight = 1;
2186 else
2188 off_t i;
2190 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2192 for (i = 0; i < blocklen; i++)
2193 edit_insert (edit, buf[i]);
2195 /* highlight inserted text then not persistent blocks */
2196 if (!option_persistent_selections && edit->modified)
2198 edit_set_markers (edit, edit->curs1, current, 0, 0);
2199 if (edit->column_highlight)
2200 edit_push_undo_action (edit, COLUMN_ON);
2201 edit->column_highlight = 0;
2204 /* Place cursor at the end of text selection */
2205 if (!option_cursor_after_inserted_block)
2207 ins_len = edit->curs1 - current;
2208 edit_cursor_move (edit, -ins_len);
2212 edit->force |= REDRAW_PAGE;
2213 g_free (buf);
2214 mc_close (file);
2215 if (blocklen != 0)
2216 ins_len = 0;
2219 return ins_len;
2222 /* --------------------------------------------------------------------------------------------- */
2224 * Fill in the edit structure. Return NULL on failure. Pass edit as
2225 * NULL to allocate a new structure.
2227 * If line is 0, try to restore saved position. Otherwise put the
2228 * cursor on that line and show it in the middle of the screen.
2231 WEdit *
2232 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2233 long line)
2235 gboolean to_free = FALSE;
2237 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2238 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2240 if (edit != NULL)
2242 /* save some widget parameters */
2243 gboolean fullscreen = edit->fullscreen;
2244 int y_prev = edit->y_prev;
2245 int x_prev = edit->x_prev;
2246 int lines_prev = edit->lines_prev;
2247 int cols_prev = edit->cols_prev;
2249 edit_purge_widget (edit);
2251 /* restore saved parameters */
2252 edit->fullscreen = fullscreen;
2253 edit->y_prev = y_prev;
2254 edit->x_prev = x_prev;
2255 edit->lines_prev = lines_prev;
2256 edit->cols_prev = cols_prev;
2258 else
2260 edit = g_malloc0 (sizeof (WEdit));
2261 to_free = TRUE;
2263 init_widget (WIDGET (edit), y, x, lines, cols, NULL, NULL);
2264 edit->fullscreen = TRUE;
2265 edit_save_size (edit);
2268 edit->drag_state = MCEDIT_DRAG_NORMAL;
2270 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2271 edit->stat1.st_uid = getuid ();
2272 edit->stat1.st_gid = getgid ();
2273 edit->stat1.st_mtime = 0;
2275 edit->over_col = 0;
2276 edit->bracket = -1;
2277 edit->last_bracket = -1;
2278 edit->force |= REDRAW_PAGE;
2280 /* set file name before load file */
2281 edit_set_filename (edit, filename_vpath);
2283 edit->undo_stack_size = START_STACK_SIZE;
2284 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2285 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2287 edit->redo_stack_size = START_STACK_SIZE;
2288 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2289 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2291 #ifdef HAVE_CHARSET
2292 edit->utf8 = FALSE;
2293 edit->converter = str_cnv_from_term;
2294 edit_set_codeset (edit);
2295 #endif
2297 if (!edit_load_file (edit))
2299 /* edit_load_file already gives an error message */
2300 if (to_free)
2301 g_free (edit);
2302 return NULL;
2305 edit->loading_done = 1;
2306 edit->modified = 0;
2307 edit->locked = 0;
2308 edit_load_syntax (edit, NULL, NULL);
2309 edit_get_syntax_color (edit, -1);
2311 /* load saved cursor position */
2312 if ((line == 0) && option_save_position)
2313 edit_load_position (edit);
2314 else
2316 if (line <= 0)
2317 line = 1;
2318 edit_move_display (edit, line - 1);
2319 edit_move_to_line (edit, line - 1);
2322 edit_load_macro_cmd (edit);
2324 return edit;
2327 /* --------------------------------------------------------------------------------------------- */
2329 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2330 gboolean
2331 edit_clean (WEdit * edit)
2333 int j = 0;
2335 if (edit == NULL)
2336 return FALSE;
2338 /* a stale lock, remove it */
2339 if (edit->locked)
2340 edit->locked = unlock_file (edit->filename_vpath);
2342 /* save cursor position */
2343 if (option_save_position)
2344 edit_save_position (edit);
2345 else if (edit->serialized_bookmarks != NULL)
2346 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2348 /* File specified on the mcedit command line and never saved */
2349 if (edit->delete_file)
2350 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2352 edit_free_syntax_rules (edit);
2353 book_mark_flush (edit, -1);
2354 for (; j <= MAXBUFF; j++)
2356 g_free (edit->buffers1[j]);
2357 g_free (edit->buffers2[j]);
2360 g_free (edit->undo_stack);
2361 g_free (edit->redo_stack);
2362 vfs_path_free (edit->filename_vpath);
2363 vfs_path_free (edit->dir_vpath);
2364 mc_search_free (edit->search);
2365 edit->search = NULL;
2367 #ifdef HAVE_CHARSET
2368 if (edit->converter != str_cnv_from_term)
2369 str_close_conv (edit->converter);
2370 #endif
2372 edit_purge_widget (edit);
2374 return TRUE;
2377 /* --------------------------------------------------------------------------------------------- */
2380 * Load a new file into the editor and set line. If it fails, preserve the old file.
2381 * To do it, allocate a new widget, initialize it and, if the new file
2382 * was loaded, copy the data to the old widget.
2384 * @return TRUE on success, FALSE on failure.
2386 gboolean
2387 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2389 Widget *w = WIDGET (edit);
2390 WEdit *e;
2392 e = g_malloc0 (sizeof (WEdit));
2393 *WIDGET (e) = *w;
2394 /* save some widget parameters */
2395 e->fullscreen = edit->fullscreen;
2396 e->y_prev = edit->y_prev;
2397 e->x_prev = edit->x_prev;
2398 e->lines_prev = edit->lines_prev;
2399 e->cols_prev = edit->cols_prev;
2401 if (edit_init (e, w->y, w->x, w->lines, w->cols, filename_vpath, line) == NULL)
2403 g_free (e);
2404 return FALSE;
2407 edit_clean (edit);
2408 memcpy (edit, e, sizeof (WEdit));
2409 g_free (e);
2411 return TRUE;
2414 /* --------------------------------------------------------------------------------------------- */
2416 #ifdef HAVE_CHARSET
2417 void
2418 edit_set_codeset (WEdit * edit)
2420 const char *cp_id;
2422 cp_id =
2423 get_codepage_id (mc_global.source_codepage >=
2424 0 ? mc_global.source_codepage : mc_global.display_codepage);
2426 if (cp_id != NULL)
2428 GIConv conv;
2429 conv = str_crt_conv_from (cp_id);
2430 if (conv != INVALID_CONV)
2432 if (edit->converter != str_cnv_from_term)
2433 str_close_conv (edit->converter);
2434 edit->converter = conv;
2438 if (cp_id != NULL)
2439 edit->utf8 = str_isutf8 (cp_id);
2441 #endif
2444 /* --------------------------------------------------------------------------------------------- */
2447 * Recording stack for undo:
2448 * The following is an implementation of a compressed stack. Identical
2449 * pushes are recorded by a negative prefix indicating the number of times the
2450 * same char was pushed. This saves space for repeated curs-left or curs-right
2451 * delete etc.
2453 * eg:
2455 * pushed: stored:
2458 * b a
2459 * b -3
2460 * b b
2461 * c --> -4
2462 * c c
2463 * c d
2467 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2468 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2469 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2470 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2471 * position.
2473 * The only way the cursor moves or the buffer is changed is through the routines:
2474 * insert, backspace, insert_ahead, delete, and cursor_move.
2475 * These record the reverse undo movements onto the stack each time they are
2476 * called.
2478 * Each key press results in a set of actions (insert; delete ...). So each time
2479 * a key is pressed the current position of start_display is pushed as
2480 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2481 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2482 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2486 * @param edit editor object
2487 * @param c code of the action
2490 void
2491 edit_push_undo_action (WEdit * edit, long c)
2493 unsigned long sp = edit->undo_stack_pointer;
2494 unsigned long spm1;
2495 long *t;
2497 /* first enlarge the stack if necessary */
2498 if (sp > edit->undo_stack_size - 10)
2499 { /* say */
2500 if (option_max_undo < 256)
2501 option_max_undo = 256;
2502 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2504 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2505 if (t)
2507 edit->undo_stack = t;
2508 edit->undo_stack_size <<= 1;
2509 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2513 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2514 if (edit->undo_stack_disable)
2516 edit_push_redo_action (edit, KEY_PRESS);
2517 edit_push_redo_action (edit, c);
2518 return;
2521 if (edit->redo_stack_reset)
2522 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2524 if (edit->undo_stack_bottom != sp
2525 && spm1 != edit->undo_stack_bottom
2526 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2528 int d;
2529 if (edit->undo_stack[spm1] < 0)
2531 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2532 if (d == c && edit->undo_stack[spm1] > -1000000000)
2534 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2535 edit->undo_stack[spm1]--;
2536 return;
2539 else
2541 d = edit->undo_stack[spm1];
2542 if (d == c)
2544 if (c >= KEY_PRESS)
2545 return; /* --> no need to push multiple do-nothings */
2546 edit->undo_stack[sp] = -2;
2547 goto check_bottom;
2551 edit->undo_stack[sp] = c;
2553 check_bottom:
2554 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2556 /* if the sp wraps round and catches the undo_stack_bottom then erase
2557 * the first set of actions on the stack to make space - by moving
2558 * undo_stack_bottom forward one "key press" */
2559 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2560 if ((unsigned long) c == edit->undo_stack_bottom ||
2561 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2564 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2566 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2567 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2569 /*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: */
2570 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2571 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2573 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2577 void
2578 edit_push_redo_action (WEdit * edit, long c)
2580 unsigned long sp = edit->redo_stack_pointer;
2581 unsigned long spm1;
2582 long *t;
2583 /* first enlarge the stack if necessary */
2584 if (sp > edit->redo_stack_size - 10)
2585 { /* say */
2586 if (option_max_undo < 256)
2587 option_max_undo = 256;
2588 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2590 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2591 if (t)
2593 edit->redo_stack = t;
2594 edit->redo_stack_size <<= 1;
2595 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2599 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2601 if (edit->redo_stack_bottom != sp
2602 && spm1 != edit->redo_stack_bottom
2603 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2605 int d;
2606 if (edit->redo_stack[spm1] < 0)
2608 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2609 if (d == c && edit->redo_stack[spm1] > -1000000000)
2611 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2612 edit->redo_stack[spm1]--;
2613 return;
2616 else
2618 d = edit->redo_stack[spm1];
2619 if (d == c)
2621 if (c >= KEY_PRESS)
2622 return; /* --> no need to push multiple do-nothings */
2623 edit->redo_stack[sp] = -2;
2624 goto redo_check_bottom;
2628 edit->redo_stack[sp] = c;
2630 redo_check_bottom:
2631 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2633 /* if the sp wraps round and catches the redo_stack_bottom then erase
2634 * the first set of actions on the stack to make space - by moving
2635 * redo_stack_bottom forward one "key press" */
2636 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2637 if ((unsigned long) c == edit->redo_stack_bottom ||
2638 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2641 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2643 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2644 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2647 * If a single key produced enough pushes to wrap all the way round then
2648 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2649 * The stack is then initialised:
2652 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2653 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2654 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2657 /* --------------------------------------------------------------------------------------------- */
2659 Basic low level single character buffer alterations and movements at the cursor.
2660 Returns char passed over, inserted or removed.
2663 void
2664 edit_insert (WEdit * edit, int c)
2666 /* check if file has grown to large */
2667 if (edit->last_byte >= SIZE_LIMIT)
2668 return;
2670 /* first we must update the position of the display window */
2671 if (edit->curs1 < edit->start_display)
2673 edit->start_display++;
2674 if (c == '\n')
2675 edit->start_line++;
2678 /* Mark file as modified, unless the file hasn't been fully loaded */
2679 if (edit->loading_done)
2680 edit_modification (edit);
2682 /* now we must update some info on the file and check if a redraw is required */
2683 if (c == '\n')
2685 book_mark_inc (edit, edit->curs_line);
2686 edit->curs_line++;
2687 edit->total_lines++;
2688 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2691 /* save the reverse command onto the undo stack */
2692 /* ordinary char and not space */
2693 if (c > 32)
2694 edit_push_undo_action (edit, BACKSPACE);
2695 else
2696 edit_push_undo_action (edit, BACKSPACE_BR);
2697 /* update markers */
2698 edit->mark1 += (edit->mark1 > edit->curs1);
2699 edit->mark2 += (edit->mark2 > edit->curs1);
2700 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2702 /* add a new buffer if we've reached the end of the last one */
2703 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2704 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2706 /* perform the insertion */
2707 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2708 = (unsigned char) c;
2710 /* update file length */
2711 edit->last_byte++;
2713 /* update cursor position */
2714 edit->curs1++;
2717 /* --------------------------------------------------------------------------------------------- */
2718 /** same as edit_insert and move left */
2720 void
2721 edit_insert_ahead (WEdit * edit, int c)
2723 if (edit->last_byte >= SIZE_LIMIT)
2724 return;
2726 if (edit->curs1 < edit->start_display)
2728 edit->start_display++;
2729 if (c == '\n')
2730 edit->start_line++;
2732 edit_modification (edit);
2733 if (c == '\n')
2735 book_mark_inc (edit, edit->curs_line);
2736 edit->total_lines++;
2737 edit->force |= REDRAW_AFTER_CURSOR;
2739 /* ordinary char and not space */
2740 if (c > 32)
2741 edit_push_undo_action (edit, DELCHAR);
2742 else
2743 edit_push_undo_action (edit, DELCHAR_BR);
2745 edit->mark1 += (edit->mark1 >= edit->curs1);
2746 edit->mark2 += (edit->mark2 >= edit->curs1);
2747 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2749 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2750 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2751 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2752 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2754 edit->last_byte++;
2755 edit->curs2++;
2758 /* --------------------------------------------------------------------------------------------- */
2760 void
2761 edit_insert_over (WEdit * edit)
2763 long i;
2765 for (i = 0; i < edit->over_col; i++)
2766 edit_insert (edit, ' ');
2768 edit->over_col = 0;
2771 /* --------------------------------------------------------------------------------------------- */
2774 edit_delete (WEdit * edit, gboolean byte_delete)
2776 int p = 0;
2777 int cw = 1;
2778 int i;
2780 if (edit->curs2 == 0)
2781 return 0;
2783 #ifdef HAVE_CHARSET
2784 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2785 if (edit->utf8 && !byte_delete)
2787 edit_get_utf (edit, edit->curs1, &cw);
2788 if (cw < 1)
2789 cw = 1;
2791 #else
2792 (void) byte_delete;
2793 #endif
2795 if (edit->mark2 != edit->mark1)
2796 edit_push_markers (edit);
2798 for (i = 1; i <= cw; i++)
2800 if (edit->mark1 > edit->curs1)
2802 edit->mark1--;
2803 edit->end_mark_curs--;
2805 if (edit->mark2 > edit->curs1)
2806 edit->mark2--;
2807 if (edit->last_get_rule > edit->curs1)
2808 edit->last_get_rule--;
2810 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2811 ((edit->curs2 -
2812 1) & M_EDIT_BUF_SIZE) - 1];
2814 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2816 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2817 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2819 edit->last_byte--;
2820 edit->curs2--;
2821 edit_push_undo_action (edit, p + 256);
2824 edit_modification (edit);
2825 if (p == '\n')
2827 book_mark_dec (edit, edit->curs_line);
2828 edit->total_lines--;
2829 edit->force |= REDRAW_AFTER_CURSOR;
2831 if (edit->curs1 < edit->start_display)
2833 edit->start_display--;
2834 if (p == '\n')
2835 edit->start_line--;
2838 return p;
2841 /* --------------------------------------------------------------------------------------------- */
2844 edit_backspace (WEdit * edit, gboolean byte_delete)
2846 int p = 0;
2847 int cw = 1;
2848 int i;
2850 if (edit->curs1 == 0)
2851 return 0;
2853 if (edit->mark2 != edit->mark1)
2854 edit_push_markers (edit);
2856 #ifdef HAVE_CHARSET
2857 if (edit->utf8 && !byte_delete)
2859 edit_get_prev_utf (edit, edit->curs1, &cw);
2860 if (cw < 1)
2861 cw = 1;
2863 #else
2864 (void) byte_delete;
2865 #endif
2867 for (i = 1; i <= cw; i++)
2869 if (edit->mark1 >= edit->curs1)
2871 edit->mark1--;
2872 edit->end_mark_curs--;
2874 if (edit->mark2 >= edit->curs1)
2875 edit->mark2--;
2876 if (edit->last_get_rule >= edit->curs1)
2877 edit->last_get_rule--;
2879 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2880 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2881 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2883 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2884 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2886 edit->last_byte--;
2887 edit->curs1--;
2888 edit_push_undo_action (edit, p);
2890 edit_modification (edit);
2891 if (p == '\n')
2893 book_mark_dec (edit, edit->curs_line);
2894 edit->curs_line--;
2895 edit->total_lines--;
2896 edit->force |= REDRAW_AFTER_CURSOR;
2899 if (edit->curs1 < edit->start_display)
2901 edit->start_display--;
2902 if (p == '\n')
2903 edit->start_line--;
2906 return p;
2909 /* --------------------------------------------------------------------------------------------- */
2910 /** moves the cursor right or left: increment positive or negative respectively */
2912 void
2913 edit_cursor_move (WEdit * edit, off_t increment)
2915 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2916 int c;
2918 if (increment < 0)
2920 for (; increment < 0; increment++)
2922 if (edit->curs1 == 0)
2923 return;
2925 edit_push_undo_action (edit, CURS_RIGHT);
2927 c = edit_get_byte (edit, edit->curs1 - 1);
2928 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2929 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2930 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2931 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2932 edit->curs2++;
2933 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2934 1) & M_EDIT_BUF_SIZE];
2935 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2937 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2938 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2940 edit->curs1--;
2941 if (c == '\n')
2943 edit->curs_line--;
2944 edit->force |= REDRAW_LINE_BELOW;
2949 else
2951 for (; increment > 0; increment--)
2953 if (edit->curs2 == 0)
2954 return;
2956 edit_push_undo_action (edit, CURS_LEFT);
2958 c = edit_get_byte (edit, edit->curs1);
2959 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2960 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2961 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2962 edit->curs1++;
2963 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2964 ((edit->curs2 -
2965 1) & M_EDIT_BUF_SIZE) - 1];
2966 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2968 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2969 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2971 edit->curs2--;
2972 if (c == '\n')
2974 edit->curs_line++;
2975 edit->force |= REDRAW_LINE_ABOVE;
2981 /* These functions return positions relative to lines */
2983 /* --------------------------------------------------------------------------------------------- */
2984 /** returns index of last char on line + 1 */
2986 off_t
2987 edit_eol (const WEdit * edit, off_t current)
2989 if (current >= edit->last_byte)
2990 return edit->last_byte;
2992 for (; edit_get_byte (edit, current) != '\n'; current++)
2995 return current;
2998 /* --------------------------------------------------------------------------------------------- */
2999 /** returns index of first char on line */
3001 off_t
3002 edit_bol (const WEdit * edit, off_t current)
3004 if (current <= 0)
3005 return 0;
3007 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
3010 return current;
3013 /* --------------------------------------------------------------------------------------------- */
3015 long
3016 edit_count_lines (const WEdit * edit, off_t current, off_t upto)
3018 long lines = 0;
3020 if (upto > edit->last_byte)
3021 upto = edit->last_byte;
3022 if (current < 0)
3023 current = 0;
3024 while (current < upto)
3025 if (edit_get_byte (edit, current++) == '\n')
3026 lines++;
3027 return lines;
3030 /* --------------------------------------------------------------------------------------------- */
3031 /* If lines is zero this returns the count of lines from current to upto. */
3032 /* If upto is zero returns index of lines forward current. */
3034 off_t
3035 edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto)
3037 if (upto != 0)
3039 return (off_t) edit_count_lines (edit, current, upto);
3041 else
3043 long next;
3044 if (lines < 0)
3045 lines = 0;
3046 while (lines-- != 0)
3048 next = edit_eol (edit, current) + 1;
3049 if (next > edit->last_byte)
3050 break;
3051 else
3052 current = next;
3054 return current;
3058 /* --------------------------------------------------------------------------------------------- */
3059 /** Returns offset of 'lines' lines up from current */
3061 off_t
3062 edit_move_backward (const WEdit * edit, off_t current, long lines)
3064 if (lines < 0)
3065 lines = 0;
3066 current = edit_bol (edit, current);
3067 while (lines-- != 0 && current != 0)
3068 current = edit_bol (edit, current - 1);
3069 return current;
3072 /* --------------------------------------------------------------------------------------------- */
3073 /* If cols is zero this returns the count of columns from current to upto. */
3074 /* If upto is zero returns index of cols across from current. */
3076 off_t
3077 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
3079 off_t p, q;
3080 long col;
3082 if (upto != 0)
3084 q = upto;
3085 cols = -10;
3087 else
3088 q = edit->last_byte + 2;
3090 for (col = 0, p = current; p < q; p++)
3092 int c, orig_c;
3094 if (cols != -10)
3096 if (col == cols)
3097 return p;
3098 if (col > cols)
3099 return p - 1;
3102 orig_c = c = edit_get_byte (edit, p);
3104 #ifdef HAVE_CHARSET
3105 if (edit->utf8)
3107 int utf_ch;
3108 int cw = 1;
3110 utf_ch = edit_get_utf (edit, p, &cw);
3111 if (mc_global.utf8_display)
3113 if (cw > 1)
3114 col -= cw - 1;
3115 if (g_unichar_iswide (utf_ch))
3116 col++;
3118 else if (cw > 1 && g_unichar_isprint (utf_ch))
3119 col -= cw - 1;
3122 c = convert_to_display_c (c);
3123 #endif
3125 if (c == '\n')
3126 return (upto != 0 ? (off_t) col : p);
3127 if (c == '\t')
3128 col += TAB_SIZE - col % TAB_SIZE;
3129 else if ((c < 32 || c == 127) && (orig_c == c
3130 #ifdef HAVE_CHARSET
3131 || (!mc_global.utf8_display && !edit->utf8)
3132 #endif
3134 /* '\r' is shown as ^M, so we must advance 2 characters */
3135 /* Caret notation for control characters */
3136 col += 2;
3137 else
3138 col++;
3140 return (off_t) col;
3143 /* --------------------------------------------------------------------------------------------- */
3144 /** returns the current column position of the cursor */
3146 long
3147 edit_get_col (const WEdit * edit)
3149 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3152 /* --------------------------------------------------------------------------------------------- */
3153 /* Scrolling functions */
3154 /* --------------------------------------------------------------------------------------------- */
3156 void
3157 edit_update_curs_row (WEdit * edit)
3159 edit->curs_row = edit->curs_line - edit->start_line;
3162 /* --------------------------------------------------------------------------------------------- */
3164 void
3165 edit_update_curs_col (WEdit * edit)
3167 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3170 /* --------------------------------------------------------------------------------------------- */
3172 long
3173 edit_get_curs_col (const WEdit * edit)
3175 return edit->curs_col;
3178 /* --------------------------------------------------------------------------------------------- */
3179 /** moves the display start position up by i lines */
3181 void
3182 edit_scroll_upward (WEdit * edit, long i)
3184 long lines_above = edit->start_line;
3186 if (i > lines_above)
3187 i = lines_above;
3188 if (i != 0)
3190 edit->start_line -= i;
3191 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3192 edit->force |= REDRAW_PAGE;
3193 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3195 edit_update_curs_row (edit);
3199 /* --------------------------------------------------------------------------------------------- */
3201 void
3202 edit_scroll_downward (WEdit * edit, long i)
3204 long lines_below;
3206 lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1);
3207 if (lines_below > 0)
3209 if (i > lines_below)
3210 i = lines_below;
3211 edit->start_line += i;
3212 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3213 edit->force |= REDRAW_PAGE;
3214 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3216 edit_update_curs_row (edit);
3219 /* --------------------------------------------------------------------------------------------- */
3221 void
3222 edit_scroll_right (WEdit * edit, long i)
3224 edit->force |= REDRAW_PAGE;
3225 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3226 edit->start_col -= i;
3229 /* --------------------------------------------------------------------------------------------- */
3231 void
3232 edit_scroll_left (WEdit * edit, long i)
3234 if (edit->start_col)
3236 edit->start_col += i;
3237 if (edit->start_col > 0)
3238 edit->start_col = 0;
3239 edit->force |= REDRAW_PAGE;
3240 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3244 /* --------------------------------------------------------------------------------------------- */
3245 /* high level cursor movement commands */
3246 /* --------------------------------------------------------------------------------------------- */
3248 void
3249 edit_move_to_prev_col (WEdit * edit, off_t p)
3251 long prev = edit->prev_col;
3252 long over = edit->over_col;
3254 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3256 if (option_cursor_beyond_eol)
3258 long line_len;
3260 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3261 edit_eol (edit, edit->curs1));
3262 if (line_len < prev + edit->over_col)
3264 edit->over_col = prev + over - line_len;
3265 edit->prev_col = line_len;
3266 edit->curs_col = line_len;
3268 else
3270 edit->curs_col = prev + over;
3271 edit->prev_col = edit->curs_col;
3272 edit->over_col = 0;
3275 else
3277 edit->over_col = 0;
3278 if (option_fake_half_tabs && is_in_indent (edit))
3280 long fake_half_tabs;
3282 edit_update_curs_col (edit);
3284 fake_half_tabs = HALF_TAB_SIZE * space_width;
3285 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3287 int q;
3289 q = edit->curs_col;
3290 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3291 p = edit_bol (edit, edit->curs1);
3292 edit_cursor_move (edit,
3293 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
3294 if (!left_of_four_spaces (edit))
3295 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3301 /* --------------------------------------------------------------------------------------------- */
3302 /** check whether line in editor is blank or not
3304 * @param edit editor object
3305 * @param line number of line
3307 * @return TRUE if line in blank, FALSE otherwise
3310 gboolean
3311 edit_line_is_blank (WEdit * edit, long line)
3313 return is_blank (edit, edit_find_line (edit, line));
3316 /* --------------------------------------------------------------------------------------------- */
3317 /** move cursor to line 'line' */
3319 void
3320 edit_move_to_line (WEdit * e, long line)
3322 if (line < e->curs_line)
3323 edit_move_up (e, e->curs_line - line, 0);
3324 else
3325 edit_move_down (e, line - e->curs_line, 0);
3326 edit_scroll_screen_over_cursor (e);
3329 /* --------------------------------------------------------------------------------------------- */
3330 /** scroll window so that first visible line is 'line' */
3332 void
3333 edit_move_display (WEdit * e, long line)
3335 if (line < e->start_line)
3336 edit_scroll_upward (e, e->start_line - line);
3337 else
3338 edit_scroll_downward (e, line - e->start_line);
3341 /* --------------------------------------------------------------------------------------------- */
3342 /** save markers onto undo stack */
3344 void
3345 edit_push_markers (WEdit * edit)
3347 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3348 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3349 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3352 /* --------------------------------------------------------------------------------------------- */
3354 void
3355 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3357 edit->mark1 = m1;
3358 edit->mark2 = m2;
3359 edit->column1 = c1;
3360 edit->column2 = c2;
3364 /* --------------------------------------------------------------------------------------------- */
3365 /** highlight marker toggle */
3367 void
3368 edit_mark_cmd (WEdit * edit, gboolean unmark)
3370 edit_push_markers (edit);
3371 if (unmark)
3373 edit_set_markers (edit, 0, 0, 0, 0);
3374 edit->force |= REDRAW_PAGE;
3376 else if (edit->mark2 >= 0)
3378 edit->end_mark_curs = -1;
3379 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3380 edit->curs_col + edit->over_col);
3381 edit->force |= REDRAW_PAGE;
3383 else
3385 edit->end_mark_curs = edit->curs1;
3386 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3387 edit->curs_col + edit->over_col);
3391 /* --------------------------------------------------------------------------------------------- */
3392 /** highlight the word under cursor */
3394 void
3395 edit_mark_current_word_cmd (WEdit * edit)
3397 long pos;
3399 for (pos = edit->curs1; pos != 0; pos--)
3401 int c1, c2;
3403 c1 = edit_get_byte (edit, pos);
3404 c2 = edit_get_byte (edit, pos - 1);
3405 if (!isspace (c1) && isspace (c2))
3406 break;
3407 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3408 break;
3410 edit->mark1 = pos;
3412 for (; pos < edit->last_byte; pos++)
3414 int c1, c2;
3416 c1 = edit_get_byte (edit, pos);
3417 c2 = edit_get_byte (edit, pos + 1);
3418 if (!isspace (c1) && isspace (c2))
3419 break;
3420 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3421 break;
3423 edit->mark2 = min (pos + 1, edit->last_byte);
3425 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3428 /* --------------------------------------------------------------------------------------------- */
3430 void
3431 edit_mark_current_line_cmd (WEdit * edit)
3433 long pos = edit->curs1;
3435 edit->mark1 = edit_bol (edit, pos);
3436 edit->mark2 = edit_eol (edit, pos);
3438 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3441 /* --------------------------------------------------------------------------------------------- */
3443 void
3444 edit_delete_line (WEdit * edit)
3447 * Delete right part of the line.
3448 * Note that edit_get_byte() returns '\n' when byte position is
3449 * beyond EOF.
3451 while (edit_get_byte (edit, edit->curs1) != '\n')
3452 (void) edit_delete (edit, TRUE);
3455 * Delete '\n' char.
3456 * Note that edit_delete() will not corrupt anything if called while
3457 * cursor position is EOF.
3459 (void) edit_delete (edit, TRUE);
3462 * Delete left part of the line.
3463 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3465 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3466 (void) edit_backspace (edit, TRUE);
3469 /* --------------------------------------------------------------------------------------------- */
3471 long
3472 edit_indent_width (const WEdit * edit, off_t p)
3474 off_t q = p;
3476 /* move to the end of the leading whitespace of the line */
3477 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3478 q++;
3479 /* count the number of columns of indentation */
3480 return (long) edit_move_forward3 (edit, p, 0, q);
3483 /* --------------------------------------------------------------------------------------------- */
3485 void
3486 edit_insert_indent (WEdit * edit, int indent)
3488 if (!option_fill_tabs_with_spaces)
3490 while (indent >= TAB_SIZE)
3492 edit_insert (edit, '\t');
3493 indent -= TAB_SIZE;
3496 while (indent-- > 0)
3497 edit_insert (edit, ' ');
3500 /* --------------------------------------------------------------------------------------------- */
3502 void
3503 edit_push_key_press (WEdit * edit)
3505 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3506 if (edit->mark2 == -1)
3508 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3509 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3513 /* --------------------------------------------------------------------------------------------- */
3515 void
3516 edit_find_bracket (WEdit * edit)
3518 edit->bracket = edit_get_bracket (edit, 1, 10000);
3519 if (edit->last_bracket != edit->bracket)
3520 edit->force |= REDRAW_PAGE;
3521 edit->last_bracket = edit->bracket;
3524 /* --------------------------------------------------------------------------------------------- */
3526 * This executes a command as though the user initiated it through a key
3527 * press. Callback with MSG_KEY as a message calls this after
3528 * translating the key press. This function can be used to pass any
3529 * command to the editor. Note that the screen wouldn't update
3530 * automatically. Either of command or char_for_insertion must be
3531 * passed as -1. Commands are executed, and char_for_insertion is
3532 * inserted at the cursor.
3535 void
3536 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3538 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3539 || (macro_index < 0
3540 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3542 macro_index = 0;
3543 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3544 return;
3546 if (macro_index != -1)
3548 edit->force |= REDRAW_COMPLETELY;
3549 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3551 edit_store_macro_cmd (edit);
3552 macro_index = -1;
3553 return;
3555 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3557 edit_repeat_macro_cmd (edit);
3558 macro_index = -1;
3559 return;
3563 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3565 record_macro_buf[macro_index].action = command;
3566 record_macro_buf[macro_index++].ch = char_for_insertion;
3568 /* record the beginning of a set of editing actions initiated by a key press */
3569 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3570 edit_push_key_press (edit);
3572 edit_execute_cmd (edit, command, char_for_insertion);
3573 if (edit->column_highlight)
3574 edit->force |= REDRAW_PAGE;
3577 /* --------------------------------------------------------------------------------------------- */
3579 This executes a command at a lower level than macro recording.
3580 It also does not push a key_press onto the undo stack. This means
3581 that if it is called many times, a single undo command will undo
3582 all of them. It also does not check for the Undo command.
3584 void
3585 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3587 Widget *w = WIDGET (edit);
3589 if (command == CK_WindowFullscreen)
3591 edit_toggle_fullscreen (edit);
3592 return;
3595 /* handle window state */
3596 if (edit_handle_move_resize (edit, command))
3597 return;
3599 edit->force |= REDRAW_LINE;
3601 /* The next key press will unhighlight the found string, so update
3602 * the whole page */
3603 if (edit->found_len || edit->column_highlight)
3604 edit->force |= REDRAW_PAGE;
3606 switch (command)
3608 /* a mark command with shift-arrow */
3609 case CK_MarkLeft:
3610 case CK_MarkRight:
3611 case CK_MarkToWordBegin:
3612 case CK_MarkToWordEnd:
3613 case CK_MarkToHome:
3614 case CK_MarkToEnd:
3615 case CK_MarkUp:
3616 case CK_MarkDown:
3617 case CK_MarkPageUp:
3618 case CK_MarkPageDown:
3619 case CK_MarkToFileBegin:
3620 case CK_MarkToFileEnd:
3621 case CK_MarkToPageBegin:
3622 case CK_MarkToPageEnd:
3623 case CK_MarkScrollUp:
3624 case CK_MarkScrollDown:
3625 case CK_MarkParagraphUp:
3626 case CK_MarkParagraphDown:
3627 /* a mark command with alt-arrow */
3628 case CK_MarkColumnPageUp:
3629 case CK_MarkColumnPageDown:
3630 case CK_MarkColumnLeft:
3631 case CK_MarkColumnRight:
3632 case CK_MarkColumnUp:
3633 case CK_MarkColumnDown:
3634 case CK_MarkColumnScrollUp:
3635 case CK_MarkColumnScrollDown:
3636 case CK_MarkColumnParagraphUp:
3637 case CK_MarkColumnParagraphDown:
3638 edit->column_highlight = 0;
3639 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3641 edit_mark_cmd (edit, TRUE); /* clear */
3642 edit_mark_cmd (edit, FALSE); /* marking on */
3644 edit->highlight = 1;
3645 break;
3647 /* any other command */
3648 default:
3649 if (edit->highlight)
3650 edit_mark_cmd (edit, FALSE); /* clear */
3651 edit->highlight = 0;
3654 /* first check for undo */
3655 if (command == CK_Undo)
3657 edit->redo_stack_reset = 0;
3658 edit_group_undo (edit);
3659 edit->found_len = 0;
3660 edit->prev_col = edit_get_col (edit);
3661 edit->search_start = edit->curs1;
3662 return;
3664 /* check for redo */
3665 if (command == CK_Redo)
3667 edit->redo_stack_reset = 0;
3668 edit_do_redo (edit);
3669 edit->found_len = 0;
3670 edit->prev_col = edit_get_col (edit);
3671 edit->search_start = edit->curs1;
3672 return;
3675 edit->redo_stack_reset = 1;
3677 /* An ordinary key press */
3678 if (char_for_insertion >= 0)
3680 /* if non persistent selection and text selected */
3681 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3682 edit_block_delete_cmd (edit);
3684 if (edit->overwrite)
3686 /* remove char only one time, after input first byte, multibyte chars */
3687 #ifdef HAVE_CHARSET
3688 if (!mc_global.utf8_display || edit->charpoint == 0)
3689 #endif
3690 if (edit_get_byte (edit, edit->curs1) != '\n')
3692 edit_delete (edit, FALSE);
3694 if (option_cursor_beyond_eol && edit->over_col > 0)
3695 edit_insert_over (edit);
3696 #ifdef HAVE_CHARSET
3697 if (char_for_insertion > 255 && !mc_global.utf8_display)
3699 unsigned char str[6 + 1];
3700 size_t i = 0;
3701 int res;
3703 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3704 if (res == 0)
3706 str[0] = '.';
3707 str[1] = '\0';
3709 else
3711 str[res] = '\0';
3713 while (str[i] != 0 && i <= 6)
3715 char_for_insertion = str[i];
3716 edit_insert (edit, char_for_insertion);
3717 i++;
3720 else
3721 #endif
3722 edit_insert (edit, char_for_insertion);
3724 if (option_auto_para_formatting)
3726 format_paragraph (edit, 0);
3727 edit->force |= REDRAW_PAGE;
3729 else
3730 check_and_wrap_line (edit);
3731 edit->found_len = 0;
3732 edit->prev_col = edit_get_col (edit);
3733 edit->search_start = edit->curs1;
3734 edit_find_bracket (edit);
3735 return;
3738 switch (command)
3740 case CK_TopOnScreen:
3741 case CK_BottomOnScreen:
3742 case CK_Top:
3743 case CK_Bottom:
3744 case CK_PageUp:
3745 case CK_PageDown:
3746 case CK_Home:
3747 case CK_End:
3748 case CK_Up:
3749 case CK_Down:
3750 case CK_Left:
3751 case CK_Right:
3752 case CK_WordLeft:
3753 case CK_WordRight:
3754 if (!option_persistent_selections && edit->mark2 >= 0)
3756 if (edit->column_highlight)
3757 edit_push_undo_action (edit, COLUMN_ON);
3758 edit->column_highlight = 0;
3759 edit_mark_cmd (edit, TRUE);
3763 switch (command)
3765 case CK_TopOnScreen:
3766 case CK_BottomOnScreen:
3767 case CK_MarkToPageBegin:
3768 case CK_MarkToPageEnd:
3769 case CK_Up:
3770 case CK_Down:
3771 case CK_WordLeft:
3772 case CK_WordRight:
3773 case CK_MarkToWordBegin:
3774 case CK_MarkToWordEnd:
3775 case CK_MarkUp:
3776 case CK_MarkDown:
3777 case CK_MarkColumnUp:
3778 case CK_MarkColumnDown:
3779 if (edit->mark2 == -1)
3780 break; /*marking is following the cursor: may need to highlight a whole line */
3781 case CK_Left:
3782 case CK_Right:
3783 case CK_MarkLeft:
3784 case CK_MarkRight:
3785 edit->force |= REDRAW_CHAR_ONLY;
3788 /* basic cursor key commands */
3789 switch (command)
3791 case CK_BackSpace:
3792 /* if non persistent selection and text selected */
3793 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3794 edit_block_delete_cmd (edit);
3795 else if (option_cursor_beyond_eol && edit->over_col > 0)
3796 edit->over_col--;
3797 else if (option_backspace_through_tabs && is_in_indent (edit))
3799 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3800 edit_backspace (edit, TRUE);
3802 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3804 int i;
3806 for (i = 0; i < HALF_TAB_SIZE; i++)
3807 edit_backspace (edit, TRUE);
3809 else
3810 edit_backspace (edit, FALSE);
3811 break;
3812 case CK_Delete:
3813 /* if non persistent selection and text selected */
3814 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3815 edit_block_delete_cmd (edit);
3816 else
3818 if (option_cursor_beyond_eol && edit->over_col > 0)
3819 edit_insert_over (edit);
3821 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3823 int i;
3825 for (i = 1; i <= HALF_TAB_SIZE; i++)
3826 edit_delete (edit, TRUE);
3828 else
3829 edit_delete (edit, FALSE);
3831 break;
3832 case CK_DeleteToWordBegin:
3833 edit->over_col = 0;
3834 edit_left_delete_word (edit);
3835 break;
3836 case CK_DeleteToWordEnd:
3837 if (option_cursor_beyond_eol && edit->over_col > 0)
3838 edit_insert_over (edit);
3840 edit_right_delete_word (edit);
3841 break;
3842 case CK_DeleteLine:
3843 edit_delete_line (edit);
3844 break;
3845 case CK_DeleteToHome:
3846 edit_delete_to_line_begin (edit);
3847 break;
3848 case CK_DeleteToEnd:
3849 edit_delete_to_line_end (edit);
3850 break;
3851 case CK_Enter:
3852 edit->over_col = 0;
3853 if (option_auto_para_formatting)
3855 edit_double_newline (edit);
3856 if (option_return_does_auto_indent)
3857 edit_auto_indent (edit);
3858 format_paragraph (edit, 0);
3860 else
3862 edit_insert (edit, '\n');
3863 if (option_return_does_auto_indent)
3864 edit_auto_indent (edit);
3866 break;
3867 case CK_Return:
3868 edit_insert (edit, '\n');
3869 break;
3871 case CK_MarkColumnPageUp:
3872 edit->column_highlight = 1;
3873 case CK_PageUp:
3874 case CK_MarkPageUp:
3875 edit_move_up (edit, w->lines - 1, 1);
3876 break;
3877 case CK_MarkColumnPageDown:
3878 edit->column_highlight = 1;
3879 case CK_PageDown:
3880 case CK_MarkPageDown:
3881 edit_move_down (edit, w->lines - 1, 1);
3882 break;
3883 case CK_MarkColumnLeft:
3884 edit->column_highlight = 1;
3885 case CK_Left:
3886 case CK_MarkLeft:
3887 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3889 if (option_cursor_beyond_eol && edit->over_col > 0)
3890 edit->over_col--;
3891 else
3892 edit_cursor_move (edit, -HALF_TAB_SIZE);
3893 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3895 else
3896 edit_left_char_move_cmd (edit);
3897 break;
3898 case CK_MarkColumnRight:
3899 edit->column_highlight = 1;
3900 case CK_Right:
3901 case CK_MarkRight:
3902 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3904 edit_cursor_move (edit, HALF_TAB_SIZE);
3905 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3907 else
3908 edit_right_char_move_cmd (edit);
3909 break;
3910 case CK_TopOnScreen:
3911 case CK_MarkToPageBegin:
3912 edit_begin_page (edit);
3913 break;
3914 case CK_BottomOnScreen:
3915 case CK_MarkToPageEnd:
3916 edit_end_page (edit);
3917 break;
3918 case CK_WordLeft:
3919 case CK_MarkToWordBegin:
3920 edit->over_col = 0;
3921 edit_left_word_move_cmd (edit);
3922 break;
3923 case CK_WordRight:
3924 case CK_MarkToWordEnd:
3925 edit->over_col = 0;
3926 edit_right_word_move_cmd (edit);
3927 break;
3928 case CK_MarkColumnUp:
3929 edit->column_highlight = 1;
3930 case CK_Up:
3931 case CK_MarkUp:
3932 edit_move_up (edit, 1, 0);
3933 break;
3934 case CK_MarkColumnDown:
3935 edit->column_highlight = 1;
3936 case CK_Down:
3937 case CK_MarkDown:
3938 edit_move_down (edit, 1, 0);
3939 break;
3940 case CK_MarkColumnParagraphUp:
3941 edit->column_highlight = 1;
3942 case CK_ParagraphUp:
3943 case CK_MarkParagraphUp:
3944 edit_move_up_paragraph (edit, 0);
3945 break;
3946 case CK_MarkColumnParagraphDown:
3947 edit->column_highlight = 1;
3948 case CK_ParagraphDown:
3949 case CK_MarkParagraphDown:
3950 edit_move_down_paragraph (edit, 0);
3951 break;
3952 case CK_MarkColumnScrollUp:
3953 edit->column_highlight = 1;
3954 case CK_ScrollUp:
3955 case CK_MarkScrollUp:
3956 edit_move_up (edit, 1, 1);
3957 break;
3958 case CK_MarkColumnScrollDown:
3959 edit->column_highlight = 1;
3960 case CK_ScrollDown:
3961 case CK_MarkScrollDown:
3962 edit_move_down (edit, 1, 1);
3963 break;
3964 case CK_Home:
3965 case CK_MarkToHome:
3966 edit_cursor_to_bol (edit);
3967 break;
3968 case CK_End:
3969 case CK_MarkToEnd:
3970 edit_cursor_to_eol (edit);
3971 break;
3972 case CK_Tab:
3973 /* if text marked shift block */
3974 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3976 if (edit->mark2 < 0)
3977 edit_mark_cmd (edit, FALSE);
3978 edit_move_block_to_right (edit);
3980 else
3982 if (option_cursor_beyond_eol)
3983 edit_insert_over (edit);
3984 edit_tab_cmd (edit);
3985 if (option_auto_para_formatting)
3987 format_paragraph (edit, 0);
3988 edit->force |= REDRAW_PAGE;
3990 else
3991 check_and_wrap_line (edit);
3993 break;
3995 case CK_InsertOverwrite:
3996 edit->overwrite = !edit->overwrite;
3997 break;
3999 case CK_Mark:
4000 if (edit->mark2 >= 0)
4002 if (edit->column_highlight)
4003 edit_push_undo_action (edit, COLUMN_ON);
4004 edit->column_highlight = 0;
4006 edit_mark_cmd (edit, FALSE);
4007 break;
4008 case CK_MarkColumn:
4009 if (!edit->column_highlight)
4010 edit_push_undo_action (edit, COLUMN_OFF);
4011 edit->column_highlight = 1;
4012 edit_mark_cmd (edit, FALSE);
4013 break;
4014 case CK_MarkAll:
4015 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
4016 edit->force |= REDRAW_PAGE;
4017 break;
4018 case CK_Unmark:
4019 if (edit->column_highlight)
4020 edit_push_undo_action (edit, COLUMN_ON);
4021 edit->column_highlight = 0;
4022 edit_mark_cmd (edit, TRUE);
4023 break;
4024 case CK_MarkWord:
4025 if (edit->column_highlight)
4026 edit_push_undo_action (edit, COLUMN_ON);
4027 edit->column_highlight = 0;
4028 edit_mark_current_word_cmd (edit);
4029 break;
4030 case CK_MarkLine:
4031 if (edit->column_highlight)
4032 edit_push_undo_action (edit, COLUMN_ON);
4033 edit->column_highlight = 0;
4034 edit_mark_current_line_cmd (edit);
4035 break;
4037 case CK_Bookmark:
4038 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4039 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4040 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4041 else
4042 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4043 break;
4044 case CK_BookmarkFlush:
4045 book_mark_flush (edit, BOOK_MARK_COLOR);
4046 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4047 edit->force |= REDRAW_PAGE;
4048 break;
4049 case CK_BookmarkNext:
4050 if (edit->book_mark != NULL)
4052 edit_book_mark_t *p;
4054 p = book_mark_find (edit, edit->curs_line);
4055 if (p->next != NULL)
4057 p = p->next;
4058 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4059 edit_move_display (edit, p->line - w->lines / 2);
4060 edit_move_to_line (edit, p->line);
4063 break;
4064 case CK_BookmarkPrev:
4065 if (edit->book_mark != NULL)
4067 edit_book_mark_t *p;
4069 p = book_mark_find (edit, edit->curs_line);
4070 while (p->line == edit->curs_line)
4071 if (p->prev != NULL)
4072 p = p->prev;
4073 if (p->line >= 0)
4075 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4076 edit_move_display (edit, p->line - w->lines / 2);
4077 edit_move_to_line (edit, p->line);
4080 break;
4082 case CK_Top:
4083 case CK_MarkToFileBegin:
4084 edit_move_to_top (edit);
4085 break;
4086 case CK_Bottom:
4087 case CK_MarkToFileEnd:
4088 edit_move_to_bottom (edit);
4089 break;
4091 case CK_Copy:
4092 if (option_cursor_beyond_eol && edit->over_col > 0)
4093 edit_insert_over (edit);
4094 edit_block_copy_cmd (edit);
4095 break;
4096 case CK_Remove:
4097 edit_block_delete_cmd (edit);
4098 break;
4099 case CK_Move:
4100 edit_block_move_cmd (edit);
4101 break;
4103 case CK_BlockShiftLeft:
4104 if (edit->mark1 != edit->mark2)
4105 edit_move_block_to_left (edit);
4106 break;
4107 case CK_BlockShiftRight:
4108 if (edit->mark1 != edit->mark2)
4109 edit_move_block_to_right (edit);
4110 break;
4111 case CK_Store:
4112 edit_copy_to_X_buf_cmd (edit);
4113 break;
4114 case CK_Cut:
4115 edit_cut_to_X_buf_cmd (edit);
4116 break;
4117 case CK_Paste:
4118 /* if non persistent selection and text selected */
4119 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4120 edit_block_delete_cmd (edit);
4121 if (option_cursor_beyond_eol && edit->over_col > 0)
4122 edit_insert_over (edit);
4123 edit_paste_from_X_buf_cmd (edit);
4124 if (!option_persistent_selections && edit->mark2 >= 0)
4126 if (edit->column_highlight)
4127 edit_push_undo_action (edit, COLUMN_ON);
4128 edit->column_highlight = 0;
4129 edit_mark_cmd (edit, TRUE);
4131 break;
4132 case CK_History:
4133 edit_paste_from_history (edit);
4134 break;
4136 case CK_SaveAs:
4137 edit_save_as_cmd (edit);
4138 break;
4139 case CK_Save:
4140 edit_save_confirm_cmd (edit);
4141 break;
4142 case CK_BlockSave:
4143 edit_save_block_cmd (edit);
4144 break;
4145 case CK_InsertFile:
4146 edit_insert_file_cmd (edit);
4147 break;
4149 case CK_FilePrev:
4150 edit_load_back_cmd (edit);
4151 break;
4152 case CK_FileNext:
4153 edit_load_forward_cmd (edit);
4154 break;
4156 case CK_SyntaxChoose:
4157 edit_syntax_dialog (edit);
4158 break;
4160 case CK_Search:
4161 edit_search_cmd (edit, FALSE);
4162 break;
4163 case CK_SearchContinue:
4164 edit_search_cmd (edit, TRUE);
4165 break;
4166 case CK_Replace:
4167 edit_replace_cmd (edit, 0);
4168 break;
4169 case CK_ReplaceContinue:
4170 edit_replace_cmd (edit, 1);
4171 break;
4172 case CK_Complete:
4173 /* if text marked shift block */
4174 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4175 edit_move_block_to_left (edit);
4176 else
4177 edit_complete_word_cmd (edit);
4178 break;
4179 case CK_Find:
4180 edit_get_match_keyword_cmd (edit);
4181 break;
4183 #ifdef HAVE_ASPELL
4184 case CK_SpellCheckCurrentWord:
4185 edit_suggest_current_word (edit);
4186 break;
4187 case CK_SpellCheck:
4188 edit_spellcheck_file (edit);
4189 break;
4190 case CK_SpellCheckSelectLang:
4191 edit_set_spell_lang ();
4192 break;
4193 #endif
4195 case CK_Date:
4197 char s[BUF_MEDIUM];
4198 /* fool gcc to prevent a Y2K warning */
4199 char time_format[] = "_c";
4200 time_format[0] = '%';
4202 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4203 edit_print_string (edit, s);
4204 edit->force |= REDRAW_PAGE;
4206 break;
4207 case CK_Goto:
4208 edit_goto_cmd (edit);
4209 break;
4210 case CK_ParagraphFormat:
4211 format_paragraph (edit, 1);
4212 edit->force |= REDRAW_PAGE;
4213 break;
4214 case CK_MacroDelete:
4215 edit_delete_macro_cmd (edit);
4216 break;
4217 case CK_MatchBracket:
4218 edit_goto_matching_bracket (edit);
4219 break;
4220 case CK_UserMenu:
4221 user_menu (edit, NULL, -1);
4222 break;
4223 case CK_Sort:
4224 edit_sort_cmd (edit);
4225 break;
4226 case CK_ExternalCommand:
4227 edit_ext_cmd (edit);
4228 break;
4229 case CK_Mail:
4230 edit_mail_dialog (edit);
4231 break;
4232 #ifdef HAVE_CHARSET
4233 case CK_SelectCodepage:
4234 edit_select_codepage_cmd (edit);
4235 break;
4236 #endif
4237 case CK_InsertLiteral:
4238 edit_insert_literal_cmd (edit);
4239 break;
4240 case CK_MacroStartStopRecord:
4241 edit_begin_end_macro_cmd (edit);
4242 break;
4243 case CK_RepeatStartStopRecord:
4244 edit_begin_end_repeat_cmd (edit);
4245 break;
4246 case CK_ExtendedKeyMap:
4247 edit->extmod = TRUE;
4248 break;
4249 default:
4250 break;
4253 /* CK_PipeBlock */
4254 if ((command / CK_PipeBlock (0)) == 1)
4255 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4257 /* keys which must set the col position, and the search vars */
4258 switch (command)
4260 case CK_Search:
4261 case CK_SearchContinue:
4262 case CK_Replace:
4263 case CK_ReplaceContinue:
4264 case CK_Complete:
4265 edit->prev_col = edit_get_col (edit);
4266 break;
4267 case CK_Up:
4268 case CK_MarkUp:
4269 case CK_MarkColumnUp:
4270 case CK_Down:
4271 case CK_MarkDown:
4272 case CK_MarkColumnDown:
4273 case CK_PageUp:
4274 case CK_MarkPageUp:
4275 case CK_MarkColumnPageUp:
4276 case CK_PageDown:
4277 case CK_MarkPageDown:
4278 case CK_MarkColumnPageDown:
4279 case CK_Top:
4280 case CK_MarkToFileBegin:
4281 case CK_Bottom:
4282 case CK_MarkToFileEnd:
4283 case CK_ParagraphUp:
4284 case CK_MarkParagraphUp:
4285 case CK_MarkColumnParagraphUp:
4286 case CK_ParagraphDown:
4287 case CK_MarkParagraphDown:
4288 case CK_MarkColumnParagraphDown:
4289 case CK_ScrollUp:
4290 case CK_MarkScrollUp:
4291 case CK_MarkColumnScrollUp:
4292 case CK_ScrollDown:
4293 case CK_MarkScrollDown:
4294 case CK_MarkColumnScrollDown:
4295 edit->search_start = edit->curs1;
4296 edit->found_len = 0;
4297 break;
4298 default:
4299 edit->found_len = 0;
4300 edit->prev_col = edit_get_col (edit);
4301 edit->search_start = edit->curs1;
4303 edit_find_bracket (edit);
4305 if (option_auto_para_formatting)
4307 switch (command)
4309 case CK_BackSpace:
4310 case CK_Delete:
4311 case CK_DeleteToWordBegin:
4312 case CK_DeleteToWordEnd:
4313 case CK_DeleteToHome:
4314 case CK_DeleteToEnd:
4315 format_paragraph (edit, 0);
4316 edit->force |= REDRAW_PAGE;
4321 /* --------------------------------------------------------------------------------------------- */
4323 void
4324 edit_stack_init (void)
4326 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4328 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4329 edit_history_moveto[edit_stack_iterator].line = -1;
4332 edit_stack_iterator = 0;
4335 /* --------------------------------------------------------------------------------------------- */
4337 void
4338 edit_stack_free (void)
4340 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4341 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4344 /* --------------------------------------------------------------------------------------------- */
4345 /** move i lines */
4347 void
4348 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4350 edit_move_updown (edit, i, do_scroll, TRUE);
4353 /* --------------------------------------------------------------------------------------------- */
4354 /** move i lines */
4356 void
4357 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4359 edit_move_updown (edit, i, do_scroll, FALSE);
4362 /* --------------------------------------------------------------------------------------------- */