Added new fnction for manipulate vpath objects:
[midnight-commander.git] / src / editor / edit.c
blob6768b604e307bb9c2e23fd6c274dd04d449d86cf
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
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor low level data handling and cursor fundamentals
30 * \author Paul Sheer
31 * \date 1996, 1997
34 #include <config.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
44 #include <fcntl.h>
46 #include "lib/global.h"
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "lib/lock.h"
57 #include "lib/widget.h"
59 #ifdef HAVE_CHARSET
60 #include "lib/charsets.h" /* get_codepage_id */
61 #endif
63 #include "src/filemanager/cmd.h" /* view_other_cmd() */
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "edit-widget.h"
74 /*** global variables ****************************************************************************/
76 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
77 int option_typewriter_wrap = 0;
78 int option_auto_para_formatting = 0;
79 int option_fill_tabs_with_spaces = 0;
80 int option_return_does_auto_indent = 1;
81 int option_backspace_through_tabs = 0;
82 int option_fake_half_tabs = 1;
83 int option_save_mode = EDIT_QUICK_SAVE;
84 int option_save_position = 1;
85 int option_max_undo = 32768;
86 int option_persistent_selections = 1;
87 int option_cursor_beyond_eol = 0;
88 int option_line_state = 0;
89 int option_line_state_width = 0;
91 int option_edit_right_extreme = 0;
92 int option_edit_left_extreme = 0;
93 int option_edit_top_extreme = 0;
94 int option_edit_bottom_extreme = 0;
95 int enable_show_tabs_tws = 1;
96 int option_check_nl_at_eof = 0;
97 int option_group_undo = 0;
98 int show_right_margin = 0;
100 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
101 char *option_backup_ext = NULL;
103 int edit_stack_iterator = 0;
104 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
105 /* magic sequense for say than block is vertical */
106 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
108 /*** file scope macro definitions ****************************************************************/
110 #define TEMP_BUF_LEN 1024
112 #define space_width 1
114 /*** file scope type declarations ****************************************************************/
116 /*** file scope variables ************************************************************************/
118 /* detecting an error on save is easy: just check if every byte has been written. */
119 /* detecting an error on read, is not so easy 'cos there is not way to tell
120 whether you read everything or not. */
121 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
122 static const struct edit_filters
124 const char *read, *write, *extension;
125 } all_filters[] =
127 /* *INDENT-OFF* */
128 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
129 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
130 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
133 /* *INDENT-ON* */
136 static long last_bracket = -1;
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 static int left_of_four_spaces (WEdit * edit);
173 /* --------------------------------------------------------------------------------------------- */
175 static void
176 edit_about (void)
178 const char *header = N_("About");
179 const char *button_name = N_("&OK");
180 const char *const version = "MCEdit " VERSION;
181 char text[BUF_LARGE];
183 int win_len, version_len, button_len;
184 int cols, lines;
186 Dlg_head *about_dlg;
188 #ifdef ENABLE_NLS
189 header = _(header);
190 button_name = _(button_name);
191 #endif
193 button_len = str_term_width1 (button_name) + 5;
194 version_len = str_term_width1 (version);
196 g_snprintf (text, sizeof (text),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len = str_term_width1 (header);
202 win_len = max (win_len, version_len);
203 win_len = max (win_len, button_len);
205 /* count width and height of text */
206 str_msg_term_size (text, &lines, &cols);
207 lines += 9;
208 cols = max (win_len, cols) + 6;
210 /* dialog */
211 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
212 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
214 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
215 add_widget (about_dlg, label_new (5, 3, text));
216 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
217 B_ENTER, NORMAL_BUTTON, button_name, NULL));
219 run_dlg (about_dlg);
220 destroy_dlg (about_dlg);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
228 static void
229 edit_init_buffers (WEdit * edit)
231 int j;
233 for (j = 0; j <= MAXBUFF; j++)
235 edit->buffers1[j] = NULL;
236 edit->buffers2[j] = NULL;
239 edit->curs1 = 0;
240 edit->curs2 = 0;
241 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
250 static int
251 edit_load_file_fast (WEdit * edit, const char *filename)
253 long buf, buf2;
254 int file = -1;
255 int ret = 1;
256 vfs_path_t *vpath;
258 edit->curs2 = edit->last_byte;
259 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
261 vpath = vfs_path_from_str (filename);
262 file = mc_open (vpath, O_RDONLY | O_BINARY);
263 vfs_path_free (vpath);
264 if (file == -1)
266 gchar *errmsg;
268 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
269 edit_error_dialog (_("Error"), errmsg);
270 g_free (errmsg);
271 return 1;
274 if (!edit->buffers2[buf2])
275 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
279 if (mc_read (file,
280 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
281 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
282 break;
284 for (buf = buf2 - 1; buf >= 0; buf--)
286 /* edit->buffers2[0] is already allocated */
287 if (!edit->buffers2[buf])
288 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
289 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
290 break;
292 ret = 0;
294 while (0);
295 if (ret)
297 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
298 edit_error_dialog (_("Error"), err_str);
299 g_free (err_str);
301 mc_close (file);
302 return ret;
305 /* --------------------------------------------------------------------------------------------- */
306 /** Return index of the filter or -1 is there is no appropriate filter */
308 static int
309 edit_find_filter (const char *filename)
311 size_t i, l, e;
313 if (filename == NULL)
314 return -1;
316 l = strlen (filename);
317 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
319 e = strlen (all_filters[i].extension);
320 if (l > e)
321 if (!strcmp (all_filters[i].extension, filename + l - e))
322 return i;
324 return -1;
327 /* --------------------------------------------------------------------------------------------- */
329 static char *
330 edit_get_filter (const char *filename)
332 int i;
333 char *p, *quoted_name;
335 i = edit_find_filter (filename);
336 if (i < 0)
337 return NULL;
339 quoted_name = name_quote (filename, 0);
340 p = g_strdup_printf (all_filters[i].read, quoted_name);
341 g_free (quoted_name);
342 return p;
345 /* --------------------------------------------------------------------------------------------- */
347 static long
348 edit_insert_stream (WEdit * edit, FILE * f)
350 int c;
351 long i = 0;
352 while ((c = fgetc (f)) >= 0)
354 edit_insert (edit, c);
355 i++;
357 return i;
360 /* --------------------------------------------------------------------------------------------- */
361 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
363 static int
364 check_file_access (WEdit * edit, const char *filename, struct stat *st)
366 int file;
367 gchar *errmsg = NULL;
368 vfs_path_t *vpath;
370 /* Try opening an existing file */
371 vpath = vfs_path_from_str (filename);
372 file = mc_open (vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
374 if (file < 0)
377 * Try creating the file. O_EXCL prevents following broken links
378 * and opening existing files.
380 file = mc_open (vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
381 if (file < 0)
383 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
384 goto cleanup;
386 else
388 /* New file, delete it if it's not modified or saved */
389 edit->delete_file = 1;
393 /* Check what we have opened */
394 if (mc_fstat (file, st) < 0)
396 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
397 goto cleanup;
400 /* We want to open regular files only */
401 if (!S_ISREG (st->st_mode))
403 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
404 goto cleanup;
408 * Don't delete non-empty files.
409 * O_EXCL should prevent it, but let's be on the safe side.
411 if (st->st_size > 0)
412 edit->delete_file = 0;
414 if (st->st_size >= SIZE_LIMIT)
415 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
417 cleanup:
418 (void) mc_close (file);
419 vfs_path_free (vpath);
421 if (errmsg != NULL)
423 edit_error_dialog (_("Error"), errmsg);
424 g_free (errmsg);
425 return 1;
427 return 0;
430 /* --------------------------------------------------------------------------------------------- */
432 * Open the file and load it into the buffers, either directly or using
433 * a filter. Return 0 on success, 1 on error.
435 * Fast loading (edit_load_file_fast) is used when the file size is
436 * known. In this case the data is read into the buffers by blocks.
437 * If the file size is not known, the data is loaded byte by byte in
438 * edit_insert_file.
441 static int
442 edit_load_file (WEdit * edit)
444 int fast_load = 1;
445 vfs_path_t *vpath = vfs_path_from_str (edit->filename);
447 /* Cannot do fast load if a filter is used */
448 if (edit_find_filter (edit->filename) >= 0)
449 fast_load = 0;
452 * VFS may report file size incorrectly, and slow load is not a big
453 * deal considering overhead in VFS.
455 if (!vfs_file_is_local (vpath))
456 fast_load = 0;
457 vfs_path_free (vpath);
460 * FIXME: line end translation should disable fast loading as well
461 * Consider doing fseek() to the end and ftell() for the real size.
464 if (*edit->filename)
466 /* If we are dealing with a real file, check that it exists */
467 if (check_file_access (edit, edit->filename, &edit->stat1))
468 return 1;
470 else
472 /* nothing to load */
473 fast_load = 0;
476 edit_init_buffers (edit);
478 if (fast_load)
480 edit->last_byte = edit->stat1.st_size;
481 edit_load_file_fast (edit, edit->filename);
482 /* If fast load was used, the number of lines wasn't calculated */
483 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
485 else
487 edit->last_byte = 0;
488 if (*edit->filename)
490 edit->undo_stack_disable = 1;
491 if (edit_insert_file (edit, edit->filename) < 0)
493 edit_clean (edit);
494 return 1;
496 edit->undo_stack_disable = 0;
499 edit->lb = LB_ASIS;
500 return 0;
503 /* --------------------------------------------------------------------------------------------- */
504 /** Restore saved cursor position in the file */
506 static void
507 edit_load_position (WEdit * edit)
509 char *filename;
510 long line, column;
511 off_t offset;
512 vfs_path_t *vpath;
514 if (!edit->filename || !*edit->filename)
515 return;
517 vpath = vfs_path_from_str (edit->filename);
518 filename = vfs_path_to_str (vpath);
519 load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
520 vfs_path_free (vpath);
521 g_free (filename);
523 if (line > 0)
525 edit_move_to_line (edit, line - 1);
526 edit->prev_col = column;
528 else if (offset > 0)
530 edit_cursor_move (edit, offset);
531 line = edit->curs_line;
532 edit->search_start = edit->curs1;
535 book_mark_restore (edit, BOOK_MARK_COLOR);
537 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
538 edit_move_display (edit, line - (edit->widget.lines / 2));
541 /* --------------------------------------------------------------------------------------------- */
542 /** Save cursor position in the file */
544 static void
545 edit_save_position (WEdit * edit)
547 char *filename;
548 vfs_path_t *vpath;
550 if (edit->filename == NULL || *edit->filename == '\0')
551 return;
553 vpath = vfs_path_from_str (edit->filename);
554 filename = vfs_path_to_str (vpath);
556 book_mark_serialize (edit, BOOK_MARK_COLOR);
557 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
558 edit->serialized_bookmarks);
559 edit->serialized_bookmarks = NULL;
561 g_free (filename);
562 vfs_path_free (vpath);
565 /* --------------------------------------------------------------------------------------------- */
566 /** Clean the WEdit stricture except the widget part */
568 static void
569 edit_purge_widget (WEdit * edit)
571 size_t len = sizeof (WEdit) - sizeof (Widget);
572 char *start = (char *) edit + sizeof (Widget);
573 memset (start, 0, len);
576 /* --------------------------------------------------------------------------------------------- */
579 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
580 then the file should be as it was when he loaded up. Then set edit->modified to 0.
583 static long
584 edit_pop_undo_action (WEdit * edit)
586 long c;
587 unsigned long sp = edit->undo_stack_pointer;
589 if (sp == edit->undo_stack_bottom)
590 return STACK_BOTTOM;
592 sp = (sp - 1) & edit->undo_stack_size_mask;
593 c = edit->undo_stack[sp];
594 if (c >= 0)
596 /* edit->undo_stack[sp] = '@'; */
597 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
598 return c;
601 if (sp == edit->undo_stack_bottom)
602 return STACK_BOTTOM;
604 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
605 if (edit->undo_stack[sp] == -2)
607 /* edit->undo_stack[sp] = '@'; */
608 edit->undo_stack_pointer = sp;
610 else
611 edit->undo_stack[sp]++;
613 return c;
616 static long
617 edit_pop_redo_action (WEdit * edit)
619 long c;
620 unsigned long sp = edit->redo_stack_pointer;
622 if (sp == edit->redo_stack_bottom)
623 return STACK_BOTTOM;
625 sp = (sp - 1) & edit->redo_stack_size_mask;
626 c = edit->redo_stack[sp];
627 if (c >= 0)
629 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
630 return c;
633 if (sp == edit->redo_stack_bottom)
634 return STACK_BOTTOM;
636 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
637 if (edit->redo_stack[sp] == -2)
638 edit->redo_stack_pointer = sp;
639 else
640 edit->redo_stack[sp]++;
642 return c;
645 static long
646 get_prev_undo_action (WEdit * edit)
648 long c;
649 unsigned long sp = edit->undo_stack_pointer;
651 if (sp == edit->undo_stack_bottom)
652 return STACK_BOTTOM;
654 sp = (sp - 1) & edit->undo_stack_size_mask;
655 c = edit->undo_stack[sp];
656 if (c >= 0)
657 return c;
659 if (sp == edit->undo_stack_bottom)
660 return STACK_BOTTOM;
662 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
663 return c;
666 /* --------------------------------------------------------------------------------------------- */
667 /** is called whenever a modification is made by one of the four routines below */
669 static void
670 edit_modification (WEdit * edit)
672 edit->caches_valid = 0;
674 /* raise lock when file modified */
675 if (!edit->modified && !edit->delete_file)
676 edit->locked = edit_lock_file (edit);
677 edit->modified = 1;
680 /* --------------------------------------------------------------------------------------------- */
682 static char *
683 edit_get_byte_ptr (WEdit * edit, long byte_index)
685 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
686 return NULL;
688 if (byte_index >= edit->curs1)
690 unsigned long p;
692 p = edit->curs1 + edit->curs2 - byte_index - 1;
693 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
694 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
697 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
698 (byte_index & M_EDIT_BUF_SIZE));
701 /* --------------------------------------------------------------------------------------------- */
703 static int
704 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
706 int i, res;
707 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
708 gchar *str;
709 gchar *cursor_buf_ptr;
711 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
713 *char_width = 0;
714 return 0;
717 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
718 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
719 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
721 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
722 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
724 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
726 *char_width = 1;
727 return *(cursor_buf_ptr - 1);
729 else
731 res = g_utf8_get_char_validated (str, -1);
733 if (res < 0)
735 *char_width = 1;
736 return *(cursor_buf_ptr - 1);
738 else
740 *char_width = cursor_buf_ptr - str;
741 return res;
746 /* --------------------------------------------------------------------------------------------- */
748 static int
749 edit_backspace (WEdit * edit, const int byte_delete)
751 int p = 0;
752 int cw = 1;
753 int i;
755 if (!edit->curs1)
756 return 0;
758 cw = 1;
760 if (edit->mark2 != edit->mark1)
761 edit_push_markers (edit);
763 if (edit->utf8 && byte_delete == 0)
765 edit_get_prev_utf (edit, edit->curs1, &cw);
766 if (cw < 1)
767 cw = 1;
769 for (i = 1; i <= cw; i++)
771 if (edit->mark1 >= edit->curs1)
773 edit->mark1--;
774 edit->end_mark_curs--;
776 if (edit->mark2 >= edit->curs1)
777 edit->mark2--;
778 if (edit->last_get_rule >= edit->curs1)
779 edit->last_get_rule--;
781 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
782 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
783 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
785 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
786 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
788 edit->last_byte--;
789 edit->curs1--;
790 edit_push_undo_action (edit, p);
792 edit_modification (edit);
793 if (p == '\n')
795 if (edit->book_mark)
796 book_mark_dec (edit, edit->curs_line);
797 edit->curs_line--;
798 edit->total_lines--;
799 edit->force |= REDRAW_AFTER_CURSOR;
802 if (edit->curs1 < edit->start_display)
804 edit->start_display--;
805 if (p == '\n')
806 edit->start_line--;
809 return p;
812 /* --------------------------------------------------------------------------------------------- */
813 /* high level cursor movement commands */
814 /* --------------------------------------------------------------------------------------------- */
816 static int
817 is_in_indent (WEdit * edit)
819 long p = edit_bol (edit, edit->curs1);
820 while (p < edit->curs1)
821 if (!strchr (" \t", edit_get_byte (edit, p++)))
822 return 0;
823 return 1;
826 /* --------------------------------------------------------------------------------------------- */
828 static int
829 is_blank (WEdit * edit, long offset)
831 long s, f;
832 int c;
833 s = edit_bol (edit, offset);
834 f = edit_eol (edit, offset) - 1;
835 while (s <= f)
837 c = edit_get_byte (edit, s++);
838 if (!isspace (c))
839 return 0;
841 return 1;
845 /* --------------------------------------------------------------------------------------------- */
846 /** returns the offset of line i */
848 static long
849 edit_find_line (WEdit * edit, int line)
851 int i, j = 0;
852 int m = 2000000000;
853 if (!edit->caches_valid)
855 for (i = 0; i < N_LINE_CACHES; i++)
856 edit->line_numbers[i] = edit->line_offsets[i] = 0;
857 /* three offsets that we *know* are line 0 at 0 and these two: */
858 edit->line_numbers[1] = edit->curs_line;
859 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
860 edit->line_numbers[2] = edit->total_lines;
861 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
862 edit->caches_valid = 1;
864 if (line >= edit->total_lines)
865 return edit->line_offsets[2];
866 if (line <= 0)
867 return 0;
868 /* find the closest known point */
869 for (i = 0; i < N_LINE_CACHES; i++)
871 int n;
872 n = abs (edit->line_numbers[i] - line);
873 if (n < m)
875 m = n;
876 j = i;
879 if (m == 0)
880 return edit->line_offsets[j]; /* know the offset exactly */
881 if (m == 1 && j >= 3)
882 i = j; /* one line different - caller might be looping, so stay in this cache */
883 else
884 i = 3 + (rand () % (N_LINE_CACHES - 3));
885 if (line > edit->line_numbers[j])
886 edit->line_offsets[i] =
887 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
888 else
889 edit->line_offsets[i] =
890 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
891 edit->line_numbers[i] = line;
892 return edit->line_offsets[i];
895 /* --------------------------------------------------------------------------------------------- */
896 /** moves up until a blank line is reached, or until just
897 before a non-blank line is reached */
899 static void
900 edit_move_up_paragraph (WEdit * edit, int do_scroll)
902 int i = 0;
903 if (edit->curs_line > 1)
905 if (line_is_blank (edit, edit->curs_line))
907 if (line_is_blank (edit, edit->curs_line - 1))
909 for (i = edit->curs_line - 1; i; i--)
910 if (!line_is_blank (edit, i))
912 i++;
913 break;
916 else
918 for (i = edit->curs_line - 1; i; i--)
919 if (line_is_blank (edit, i))
920 break;
923 else
925 for (i = edit->curs_line - 1; i; i--)
926 if (line_is_blank (edit, i))
927 break;
930 edit_move_up (edit, edit->curs_line - i, do_scroll);
933 /* --------------------------------------------------------------------------------------------- */
934 /** moves down until a blank line is reached, or until just
935 before a non-blank line is reached */
937 static void
938 edit_move_down_paragraph (WEdit * edit, int do_scroll)
940 int i;
941 if (edit->curs_line >= edit->total_lines - 1)
943 i = edit->total_lines;
945 else
947 if (line_is_blank (edit, edit->curs_line))
949 if (line_is_blank (edit, edit->curs_line + 1))
951 for (i = edit->curs_line + 1; i; i++)
952 if (!line_is_blank (edit, i) || i > edit->total_lines)
954 i--;
955 break;
958 else
960 for (i = edit->curs_line + 1; i; i++)
961 if (line_is_blank (edit, i) || i >= edit->total_lines)
962 break;
965 else
967 for (i = edit->curs_line + 1; i; i++)
968 if (line_is_blank (edit, i) || i >= edit->total_lines)
969 break;
972 edit_move_down (edit, i - edit->curs_line, do_scroll);
975 /* --------------------------------------------------------------------------------------------- */
977 static void
978 edit_begin_page (WEdit * edit)
980 edit_update_curs_row (edit);
981 edit_move_up (edit, edit->curs_row, 0);
984 /* --------------------------------------------------------------------------------------------- */
986 static void
987 edit_end_page (WEdit * edit)
989 edit_update_curs_row (edit);
990 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
994 /* --------------------------------------------------------------------------------------------- */
995 /** goto beginning of text */
997 static void
998 edit_move_to_top (WEdit * edit)
1000 if (edit->curs_line)
1002 edit_cursor_move (edit, -edit->curs1);
1003 edit_move_to_prev_col (edit, 0);
1004 edit->force |= REDRAW_PAGE;
1005 edit->search_start = 0;
1006 edit_update_curs_row (edit);
1011 /* --------------------------------------------------------------------------------------------- */
1012 /** goto end of text */
1014 static void
1015 edit_move_to_bottom (WEdit * edit)
1017 if (edit->curs_line < edit->total_lines)
1019 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1020 edit->start_display = edit->last_byte;
1021 edit->start_line = edit->total_lines;
1022 edit_scroll_upward (edit, edit->widget.lines - 1);
1023 edit->force |= REDRAW_PAGE;
1027 /* --------------------------------------------------------------------------------------------- */
1028 /** goto beginning of line */
1030 static void
1031 edit_cursor_to_bol (WEdit * edit)
1033 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1034 edit->search_start = edit->curs1;
1035 edit->prev_col = edit_get_col (edit);
1036 edit->over_col = 0;
1039 /* --------------------------------------------------------------------------------------------- */
1040 /** goto end of line */
1042 static void
1043 edit_cursor_to_eol (WEdit * edit)
1045 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1046 edit->search_start = edit->curs1;
1047 edit->prev_col = edit_get_col (edit);
1048 edit->over_col = 0;
1051 /* --------------------------------------------------------------------------------------------- */
1053 static unsigned long
1054 my_type_of (int c)
1056 int x, r = 0;
1057 const char *p, *q;
1058 const char option_chars_move_whole_word[] =
1059 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1061 if (!c)
1062 return 0;
1063 if (c == '!')
1065 if (*option_chars_move_whole_word == '!')
1066 return 2;
1067 return 0x80000000UL;
1069 if (g_ascii_isupper ((gchar) c))
1070 c = 'A';
1071 else if (g_ascii_islower ((gchar) c))
1072 c = 'a';
1073 else if (g_ascii_isalpha (c))
1074 c = 'a';
1075 else if (isdigit (c))
1076 c = '0';
1077 else if (isspace (c))
1078 c = ' ';
1079 q = strchr (option_chars_move_whole_word, c);
1080 if (!q)
1081 return 0xFFFFFFFFUL;
1084 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1085 if (*p == '!')
1086 x <<= 1;
1087 r |= x;
1089 while ((q = strchr (q + 1, c)));
1090 return r;
1093 /* --------------------------------------------------------------------------------------------- */
1095 static void
1096 edit_left_word_move (WEdit * edit, int s)
1098 for (;;)
1100 int c1, c2;
1101 if (edit->column_highlight
1102 && edit->mark1 != edit->mark2
1103 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1104 break;
1105 edit_cursor_move (edit, -1);
1106 if (!edit->curs1)
1107 break;
1108 c1 = edit_get_byte (edit, edit->curs1 - 1);
1109 c2 = edit_get_byte (edit, edit->curs1);
1110 if (c1 == '\n' || c2 == '\n')
1111 break;
1112 if (!(my_type_of (c1) & my_type_of (c2)))
1113 break;
1114 if (isspace (c1) && !isspace (c2))
1115 break;
1116 if (s)
1117 if (!isspace (c1) && isspace (c2))
1118 break;
1122 /* --------------------------------------------------------------------------------------------- */
1124 static void
1125 edit_left_word_move_cmd (WEdit * edit)
1127 edit_left_word_move (edit, 0);
1128 edit->force |= REDRAW_PAGE;
1131 /* --------------------------------------------------------------------------------------------- */
1133 static void
1134 edit_right_word_move (WEdit * edit, int s)
1136 for (;;)
1138 int c1, c2;
1139 if (edit->column_highlight
1140 && edit->mark1 != edit->mark2
1141 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1142 break;
1143 edit_cursor_move (edit, 1);
1144 if (edit->curs1 >= edit->last_byte)
1145 break;
1146 c1 = edit_get_byte (edit, edit->curs1 - 1);
1147 c2 = edit_get_byte (edit, edit->curs1);
1148 if (c1 == '\n' || c2 == '\n')
1149 break;
1150 if (!(my_type_of (c1) & my_type_of (c2)))
1151 break;
1152 if (isspace (c1) && !isspace (c2))
1153 break;
1154 if (s)
1155 if (!isspace (c1) && isspace (c2))
1156 break;
1160 /* --------------------------------------------------------------------------------------------- */
1162 static void
1163 edit_right_word_move_cmd (WEdit * edit)
1165 edit_right_word_move (edit, 0);
1166 edit->force |= REDRAW_PAGE;
1169 /* --------------------------------------------------------------------------------------------- */
1171 static void
1172 edit_right_char_move_cmd (WEdit * edit)
1174 int cw = 1;
1175 int c = 0;
1176 if (edit->utf8)
1178 c = edit_get_utf (edit, edit->curs1, &cw);
1179 if (cw < 1)
1180 cw = 1;
1182 else
1184 c = edit_get_byte (edit, edit->curs1);
1186 if (option_cursor_beyond_eol && c == '\n')
1188 edit->over_col++;
1190 else
1192 edit_cursor_move (edit, cw);
1196 /* --------------------------------------------------------------------------------------------- */
1198 static void
1199 edit_left_char_move_cmd (WEdit * edit)
1201 int cw = 1;
1202 if (edit->column_highlight
1203 && option_cursor_beyond_eol
1204 && edit->mark1 != edit->mark2
1205 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1206 return;
1207 if (edit->utf8)
1209 edit_get_prev_utf (edit, edit->curs1, &cw);
1210 if (cw < 1)
1211 cw = 1;
1213 if (option_cursor_beyond_eol && edit->over_col > 0)
1215 edit->over_col--;
1217 else
1219 edit_cursor_move (edit, -cw);
1223 /* --------------------------------------------------------------------------------------------- */
1224 /** Up or down cursor moving.
1225 direction = TRUE - move up
1226 = FALSE - move down
1229 static void
1230 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1232 unsigned long p;
1233 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1235 if (i > l)
1236 i = l;
1238 if (i == 0)
1239 return;
1241 if (i > 1)
1242 edit->force |= REDRAW_PAGE;
1243 if (do_scroll)
1245 if (direction)
1246 edit_scroll_upward (edit, i);
1247 else
1248 edit_scroll_downward (edit, i);
1250 p = edit_bol (edit, edit->curs1);
1252 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1254 edit_cursor_move (edit, p - edit->curs1);
1256 edit_move_to_prev_col (edit, p);
1258 /* search start of current multibyte char (like CJK) */
1259 if (edit->curs1 + 1 < edit->last_byte)
1261 edit_right_char_move_cmd (edit);
1262 edit_left_char_move_cmd (edit);
1265 edit->search_start = edit->curs1;
1266 edit->found_len = 0;
1269 /* --------------------------------------------------------------------------------------------- */
1271 static void
1272 edit_right_delete_word (WEdit * edit)
1274 int c1, c2;
1275 for (;;)
1277 if (edit->curs1 >= edit->last_byte)
1278 break;
1279 c1 = edit_delete (edit, 1);
1280 c2 = edit_get_byte (edit, edit->curs1);
1281 if (c1 == '\n' || c2 == '\n')
1282 break;
1283 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1284 break;
1285 if (!(my_type_of (c1) & my_type_of (c2)))
1286 break;
1290 /* --------------------------------------------------------------------------------------------- */
1292 static void
1293 edit_left_delete_word (WEdit * edit)
1295 int c1, c2;
1296 for (;;)
1298 if (edit->curs1 <= 0)
1299 break;
1300 c1 = edit_backspace (edit, 1);
1301 c2 = edit_get_byte (edit, edit->curs1 - 1);
1302 if (c1 == '\n' || c2 == '\n')
1303 break;
1304 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1305 break;
1306 if (!(my_type_of (c1) & my_type_of (c2)))
1307 break;
1311 /* --------------------------------------------------------------------------------------------- */
1313 the start column position is not recorded, and hence does not
1314 undo as it happed. But who would notice.
1317 static void
1318 edit_do_undo (WEdit * edit)
1320 long ac;
1321 long count = 0;
1323 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1324 edit->over_col = 0;
1325 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1327 switch ((int) ac)
1329 case STACK_BOTTOM:
1330 goto done_undo;
1331 case CURS_RIGHT:
1332 edit_cursor_move (edit, 1);
1333 break;
1334 case CURS_LEFT:
1335 edit_cursor_move (edit, -1);
1336 break;
1337 case BACKSPACE:
1338 case BACKSPACE_BR:
1339 edit_backspace (edit, 1);
1340 break;
1341 case DELCHAR:
1342 case DELCHAR_BR:
1343 edit_delete (edit, 1);
1344 break;
1345 case COLUMN_ON:
1346 edit->column_highlight = 1;
1347 break;
1348 case COLUMN_OFF:
1349 edit->column_highlight = 0;
1350 break;
1352 if (ac >= 256 && ac < 512)
1353 edit_insert_ahead (edit, ac - 256);
1354 if (ac >= 0 && ac < 256)
1355 edit_insert (edit, ac);
1357 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1359 edit->mark1 = ac - MARK_1;
1360 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1362 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1364 edit->mark2 = ac - MARK_2;
1365 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1367 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1369 edit->end_mark_curs = ac - MARK_CURS;
1371 if (count++)
1372 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1375 if (edit->start_display > ac - KEY_PRESS)
1377 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1378 edit->force |= REDRAW_PAGE;
1380 else if (edit->start_display < ac - KEY_PRESS)
1382 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1383 edit->force |= REDRAW_PAGE;
1385 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1386 edit_update_curs_row (edit);
1388 done_undo:;
1389 edit->undo_stack_disable = 0;
1392 static void
1393 edit_do_redo (WEdit * edit)
1395 long ac;
1396 long count = 0;
1398 if (edit->redo_stack_reset)
1399 return;
1401 edit->over_col = 0;
1402 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1404 switch ((int) ac)
1406 case STACK_BOTTOM:
1407 goto done_redo;
1408 case CURS_RIGHT:
1409 edit_cursor_move (edit, 1);
1410 break;
1411 case CURS_LEFT:
1412 edit_cursor_move (edit, -1);
1413 break;
1414 case BACKSPACE:
1415 edit_backspace (edit, 1);
1416 break;
1417 case DELCHAR:
1418 edit_delete (edit, 1);
1419 break;
1420 case COLUMN_ON:
1421 edit->column_highlight = 1;
1422 break;
1423 case COLUMN_OFF:
1424 edit->column_highlight = 0;
1425 break;
1427 if (ac >= 256 && ac < 512)
1428 edit_insert_ahead (edit, ac - 256);
1429 if (ac >= 0 && ac < 256)
1430 edit_insert (edit, ac);
1432 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1434 edit->mark1 = ac - MARK_1;
1435 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1437 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1439 edit->mark2 = ac - MARK_2;
1440 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1442 /* more than one pop usually means something big */
1443 if (count++)
1444 edit->force |= REDRAW_PAGE;
1447 if (edit->start_display > ac - KEY_PRESS)
1449 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1450 edit->force |= REDRAW_PAGE;
1452 else if (edit->start_display < ac - KEY_PRESS)
1454 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1455 edit->force |= REDRAW_PAGE;
1457 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1458 edit_update_curs_row (edit);
1460 done_redo:;
1463 static void
1464 edit_group_undo (WEdit * edit)
1466 long ac = KEY_PRESS;
1467 long cur_ac = KEY_PRESS;
1468 while (ac != STACK_BOTTOM && ac == cur_ac)
1470 cur_ac = get_prev_undo_action (edit);
1471 edit_do_undo (edit);
1472 ac = get_prev_undo_action (edit);
1473 /* exit from cycle if option_group_undo is not set,
1474 * and make single UNDO operation
1476 if (!option_group_undo)
1477 ac = STACK_BOTTOM;
1481 /* --------------------------------------------------------------------------------------------- */
1483 static void
1484 edit_delete_to_line_end (WEdit * edit)
1486 while (edit_get_byte (edit, edit->curs1) != '\n')
1488 if (!edit->curs2)
1489 break;
1490 edit_delete (edit, 1);
1494 /* --------------------------------------------------------------------------------------------- */
1496 static void
1497 edit_delete_to_line_begin (WEdit * edit)
1499 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1501 if (!edit->curs1)
1502 break;
1503 edit_backspace (edit, 1);
1507 /* --------------------------------------------------------------------------------------------- */
1509 static int
1510 is_aligned_on_a_tab (WEdit * edit)
1512 edit_update_curs_col (edit);
1513 return !((edit->curs_col % (TAB_SIZE * space_width))
1514 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1517 /* --------------------------------------------------------------------------------------------- */
1519 static int
1520 right_of_four_spaces (WEdit * edit)
1522 int i, ch = 0;
1523 for (i = 1; i <= HALF_TAB_SIZE; i++)
1524 ch |= edit_get_byte (edit, edit->curs1 - i);
1525 if (ch == ' ')
1526 return is_aligned_on_a_tab (edit);
1527 return 0;
1530 /* --------------------------------------------------------------------------------------------- */
1532 static int
1533 left_of_four_spaces (WEdit * edit)
1535 int i, ch = 0;
1536 for (i = 0; i < HALF_TAB_SIZE; i++)
1537 ch |= edit_get_byte (edit, edit->curs1 + i);
1538 if (ch == ' ')
1539 return is_aligned_on_a_tab (edit);
1540 return 0;
1543 /* --------------------------------------------------------------------------------------------- */
1545 static void
1546 edit_auto_indent (WEdit * edit)
1548 long p;
1549 char c;
1550 p = edit->curs1;
1551 /* use the previous line as a template */
1552 p = edit_move_backward (edit, p, 1);
1553 /* copy the leading whitespace of the line */
1554 for (;;)
1555 { /* no range check - the line _is_ \n-terminated */
1556 c = edit_get_byte (edit, p++);
1557 if (c != ' ' && c != '\t')
1558 break;
1559 edit_insert (edit, c);
1563 /* --------------------------------------------------------------------------------------------- */
1565 static inline void
1566 edit_double_newline (WEdit * edit)
1568 edit_insert (edit, '\n');
1569 if (edit_get_byte (edit, edit->curs1) == '\n')
1570 return;
1571 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1572 return;
1573 edit->force |= REDRAW_PAGE;
1574 edit_insert (edit, '\n');
1578 /* --------------------------------------------------------------------------------------------- */
1580 static void
1581 insert_spaces_tab (WEdit * edit, gboolean half)
1583 int i;
1585 edit_update_curs_col (edit);
1586 i = option_tab_spacing * space_width;
1587 if (half)
1588 i /= 2;
1589 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1590 while (i > 0)
1592 edit_insert (edit, ' ');
1593 i -= space_width;
1597 /* --------------------------------------------------------------------------------------------- */
1599 static inline void
1600 edit_tab_cmd (WEdit * edit)
1602 int i;
1604 if (option_fake_half_tabs)
1606 if (is_in_indent (edit))
1608 /*insert a half tab (usually four spaces) unless there is a
1609 half tab already behind, then delete it and insert a
1610 full tab. */
1611 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1612 insert_spaces_tab (edit, TRUE);
1613 else
1615 for (i = 1; i <= HALF_TAB_SIZE; i++)
1616 edit_backspace (edit, 1);
1617 edit_insert (edit, '\t');
1619 return;
1622 if (option_fill_tabs_with_spaces)
1623 insert_spaces_tab (edit, FALSE);
1624 else
1625 edit_insert (edit, '\t');
1628 /* --------------------------------------------------------------------------------------------- */
1630 static void
1631 check_and_wrap_line (WEdit * edit)
1633 int curs, c;
1634 if (!option_typewriter_wrap)
1635 return;
1636 edit_update_curs_col (edit);
1637 if (edit->curs_col < option_word_wrap_line_length)
1638 return;
1639 curs = edit->curs1;
1640 for (;;)
1642 curs--;
1643 c = edit_get_byte (edit, curs);
1644 if (c == '\n' || curs <= 0)
1646 edit_insert (edit, '\n');
1647 return;
1649 if (c == ' ' || c == '\t')
1651 int current = edit->curs1;
1652 edit_cursor_move (edit, curs - edit->curs1 + 1);
1653 edit_insert (edit, '\n');
1654 edit_cursor_move (edit, current - edit->curs1 + 1);
1655 return;
1660 /* --------------------------------------------------------------------------------------------- */
1661 /** this find the matching bracket in either direction, and sets edit->bracket */
1663 static long
1664 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1666 const char *const b = "{}{[][()(", *p;
1667 int i = 1, a, inc = -1, c, d, n = 0;
1668 unsigned long j = 0;
1669 long q;
1670 edit_update_curs_row (edit);
1671 c = edit_get_byte (edit, edit->curs1);
1672 p = strchr (b, c);
1673 /* no limit */
1674 if (!furthest_bracket_search)
1675 furthest_bracket_search--;
1676 /* not on a bracket at all */
1677 if (!p)
1678 return -1;
1679 /* the matching bracket */
1680 d = p[1];
1681 /* going left or right? */
1682 if (strchr ("{[(", c))
1683 inc = 1;
1684 for (q = edit->curs1 + inc;; q += inc)
1686 /* out of buffer? */
1687 if (q >= edit->last_byte || q < 0)
1688 break;
1689 a = edit_get_byte (edit, q);
1690 /* don't want to eat CPU */
1691 if (j++ > furthest_bracket_search)
1692 break;
1693 /* out of screen? */
1694 if (in_screen)
1696 if (q < edit->start_display)
1697 break;
1698 /* count lines if searching downward */
1699 if (inc > 0 && a == '\n')
1700 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1701 break;
1703 /* count bracket depth */
1704 i += (a == c) - (a == d);
1705 /* return if bracket depth is zero */
1706 if (!i)
1707 return q;
1709 /* no match */
1710 return -1;
1713 /* --------------------------------------------------------------------------------------------- */
1715 static inline void
1716 edit_goto_matching_bracket (WEdit * edit)
1718 long q;
1720 q = edit_get_bracket (edit, 0, 0);
1721 if (q >= 0)
1723 edit->bracket = edit->curs1;
1724 edit->force |= REDRAW_PAGE;
1725 edit_cursor_move (edit, q - edit->curs1);
1729 /* --------------------------------------------------------------------------------------------- */
1731 static void
1732 edit_move_block_to_right (WEdit * edit)
1734 long start_mark, end_mark;
1735 long cur_bol, start_bol;
1737 if (eval_marks (edit, &start_mark, &end_mark))
1738 return;
1740 start_bol = edit_bol (edit, start_mark);
1741 cur_bol = edit_bol (edit, end_mark - 1);
1745 edit_cursor_move (edit, cur_bol - edit->curs1);
1746 if (option_fill_tabs_with_spaces)
1747 insert_spaces_tab (edit, option_fake_half_tabs);
1748 else
1749 edit_insert (edit, '\t');
1750 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1752 if (cur_bol == 0)
1753 break;
1755 cur_bol = edit_bol (edit, cur_bol - 1);
1757 while (cur_bol >= start_bol);
1759 edit->force |= REDRAW_PAGE;
1762 /* --------------------------------------------------------------------------------------------- */
1764 static void
1765 edit_move_block_to_left (WEdit * edit)
1767 long start_mark, end_mark;
1768 long cur_bol, start_bol;
1769 int i;
1771 if (eval_marks (edit, &start_mark, &end_mark))
1772 return;
1774 start_bol = edit_bol (edit, start_mark);
1775 cur_bol = edit_bol (edit, end_mark - 1);
1779 int del_tab_width;
1780 int next_char;
1782 edit_cursor_move (edit, cur_bol - edit->curs1);
1784 if (option_fake_half_tabs)
1785 del_tab_width = HALF_TAB_SIZE;
1786 else
1787 del_tab_width = option_tab_spacing;
1789 next_char = edit_get_byte (edit, edit->curs1);
1790 if (next_char == '\t')
1791 edit_delete (edit, 1);
1792 else if (next_char == ' ')
1793 for (i = 1; i <= del_tab_width; i++)
1795 if (next_char == ' ')
1796 edit_delete (edit, 1);
1797 next_char = edit_get_byte (edit, edit->curs1);
1800 if (cur_bol == 0)
1801 break;
1803 cur_bol = edit_bol (edit, cur_bol - 1);
1805 while (cur_bol >= start_bol);
1807 edit->force |= REDRAW_PAGE;
1810 /* --------------------------------------------------------------------------------------------- */
1812 * prints at the cursor
1813 * @returns the number of chars printed
1816 static size_t
1817 edit_print_string (WEdit * e, const char *s)
1819 size_t i = 0;
1821 while (s[i] != '\0')
1822 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1823 e->force |= REDRAW_COMPLETELY;
1824 edit_update_screen (e);
1825 return i;
1828 /* --------------------------------------------------------------------------------------------- */
1829 /*** public functions ****************************************************************************/
1830 /* --------------------------------------------------------------------------------------------- */
1832 /** User edit menu, like user menu (F2) but only in editor. */
1834 void
1835 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1837 char *block_file;
1838 int nomark;
1839 long curs;
1840 long start_mark, end_mark;
1841 struct stat status;
1842 vfs_path_t *block_file_vpath;
1844 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1845 block_file_vpath = vfs_path_from_str (block_file);
1846 curs = edit->curs1;
1847 nomark = eval_marks (edit, &start_mark, &end_mark);
1848 if (nomark == 0)
1849 edit_save_block (edit, block_file, start_mark, end_mark);
1851 /* run shell scripts from menu */
1852 if (user_menu_cmd (edit, menu_file, selected_entry)
1853 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1855 int rc = 0;
1856 FILE *fd;
1858 /* i.e. we have marked block */
1859 if (nomark == 0)
1860 rc = edit_block_delete_cmd (edit);
1862 if (rc == 0)
1864 long ins_len;
1866 ins_len = edit_insert_file (edit, block_file);
1867 if (nomark == 0 && ins_len > 0)
1868 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1870 /* truncate block file */
1871 fd = fopen (block_file, "w");
1872 if (fd != NULL)
1873 fclose (fd);
1875 edit_cursor_move (edit, curs - edit->curs1);
1876 edit_refresh_cmd (edit);
1877 edit->force |= REDRAW_COMPLETELY;
1879 g_free (block_file);
1880 vfs_path_free (block_file_vpath);
1883 /* --------------------------------------------------------------------------------------------- */
1886 edit_get_byte (WEdit * edit, long byte_index)
1888 unsigned long p;
1889 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1890 return '\n';
1892 if (byte_index >= edit->curs1)
1894 p = edit->curs1 + edit->curs2 - byte_index - 1;
1895 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1897 else
1899 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1903 /* --------------------------------------------------------------------------------------------- */
1906 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1908 gchar *str = NULL;
1909 int res = -1;
1910 gunichar ch;
1911 gchar *next_ch = NULL;
1912 int width = 0;
1913 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1915 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1917 *char_width = 0;
1918 return '\n';
1921 str = edit_get_byte_ptr (edit, byte_index);
1923 if (str == NULL)
1925 *char_width = 0;
1926 return 0;
1929 res = g_utf8_get_char_validated (str, -1);
1931 if (res < 0)
1933 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1934 int i;
1935 for (i = 0; i < UTF8_CHAR_LEN; i++)
1936 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1937 utf8_buf[UTF8_CHAR_LEN] = '\0';
1938 str = utf8_buf;
1939 res = g_utf8_get_char_validated (str, -1);
1942 if (res < 0)
1944 ch = *str;
1945 width = 0;
1947 else
1949 ch = res;
1950 /* Calculate UTF-8 char width */
1951 next_ch = g_utf8_next_char (str);
1952 if (next_ch)
1954 width = next_ch - str;
1956 else
1958 ch = 0;
1959 width = 0;
1962 *char_width = width;
1963 return ch;
1966 /* --------------------------------------------------------------------------------------------- */
1968 char *
1969 edit_get_write_filter (const vfs_path_t * write_name_vpath, const char *filename)
1971 int i;
1972 char *p, *writename;
1973 vfs_path_element_t *path_element;
1975 i = edit_find_filter (filename);
1976 if (i < 0)
1977 return NULL;
1979 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1980 writename = name_quote (path_element->path, 0);
1981 p = g_strdup_printf (all_filters[i].write, writename);
1982 g_free (writename);
1983 return p;
1986 /* --------------------------------------------------------------------------------------------- */
1988 long
1989 edit_write_stream (WEdit * edit, FILE * f)
1991 long i;
1993 if (edit->lb == LB_ASIS)
1995 for (i = 0; i < edit->last_byte; i++)
1996 if (fputc (edit_get_byte (edit, i), f) < 0)
1997 break;
1998 return i;
2001 /* change line breaks */
2002 for (i = 0; i < edit->last_byte; i++)
2004 unsigned char c = edit_get_byte (edit, i);
2006 if (!(c == '\n' || c == '\r'))
2008 /* not line break */
2009 if (fputc (c, f) < 0)
2010 return i;
2012 else
2013 { /* (c == '\n' || c == '\r') */
2014 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2016 switch (edit->lb)
2018 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2019 /* put one line break unconditionally */
2020 if (fputc ('\n', f) < 0)
2021 return i;
2023 i++; /* 2 chars are processed */
2025 if (c == '\r' && c1 == '\n')
2026 /* Windows line break; go to the next char */
2027 break;
2029 if (c == '\r' && c1 == '\r')
2031 /* two Macintosh line breaks; put second line break */
2032 if (fputc ('\n', f) < 0)
2033 return i;
2034 break;
2037 if (fputc (c1, f) < 0)
2038 return i;
2039 break;
2041 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2042 /* put one line break unconditionally */
2043 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2044 return i;
2046 if (c == '\r' && c1 == '\n')
2047 /* Windows line break; go to the next char */
2048 i++;
2049 break;
2051 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2052 /* put one line break unconditionally */
2053 if (fputc ('\r', f) < 0)
2054 return i;
2056 i++; /* 2 chars are processed */
2058 if (c == '\r' && c1 == '\n')
2059 /* Windows line break; go to the next char */
2060 break;
2062 if (c == '\n' && c1 == '\n')
2064 /* two Windows line breaks; put second line break */
2065 if (fputc ('\r', f) < 0)
2066 return i;
2067 break;
2070 if (fputc (c1, f) < 0)
2071 return i;
2072 break;
2073 case LB_ASIS: /* default without changes */
2074 break;
2079 return edit->last_byte;
2082 /* --------------------------------------------------------------------------------------------- */
2083 /** inserts a file at the cursor, returns count of inserted bytes on success */
2084 long
2085 edit_insert_file (WEdit * edit, const char *filename)
2087 char *p = NULL;
2088 long ins_len = 0;
2089 vfs_path_t *vpath;
2091 vpath = vfs_path_from_str (filename);
2092 p = edit_get_filter (filename);
2093 if (p != NULL)
2095 FILE *f;
2096 long current = edit->curs1;
2098 f = (FILE *) popen (p, "r");
2099 if (f != NULL)
2101 edit_insert_stream (edit, f);
2102 ins_len = edit->curs1 - current;
2103 edit_cursor_move (edit, -ins_len);
2104 if (pclose (f) > 0)
2106 char *errmsg;
2108 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2109 edit_error_dialog (_("Error"), errmsg);
2110 g_free (errmsg);
2111 ins_len = -1;
2112 goto ret;
2115 else
2117 char *errmsg;
2119 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2120 edit_error_dialog (_("Error"), errmsg);
2121 g_free (errmsg);
2122 ins_len = -1;
2123 goto ret;
2126 else
2128 int i, file, blocklen;
2129 long current = edit->curs1;
2130 int vertical_insertion = 0;
2131 char *buf;
2133 file = mc_open (vpath, O_RDONLY | O_BINARY);
2134 if (file == -1)
2136 ins_len = -1;
2137 goto ret;
2139 buf = g_malloc0 (TEMP_BUF_LEN);
2140 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2141 if (blocklen > 0)
2143 /* if contain signature VERTICAL_MAGIC then it vertical block */
2144 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2145 vertical_insertion = 1;
2146 else
2147 mc_lseek (file, 0, SEEK_SET);
2150 if (vertical_insertion)
2152 long mark1, mark2;
2153 int c1, c2;
2155 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2156 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2157 /* highlight inserted text then not persistent blocks */
2158 if (!option_persistent_selections)
2160 if (!edit->column_highlight)
2161 edit_push_undo_action (edit, COLUMN_OFF);
2162 edit->column_highlight = 1;
2165 else
2167 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2169 for (i = 0; i < blocklen; i++)
2170 edit_insert (edit, buf[i]);
2172 /* highlight inserted text then not persistent blocks */
2173 if (!option_persistent_selections && edit->modified)
2175 edit_set_markers (edit, edit->curs1, current, 0, 0);
2176 if (edit->column_highlight)
2177 edit_push_undo_action (edit, COLUMN_ON);
2178 edit->column_highlight = 0;
2182 edit->force |= REDRAW_PAGE;
2183 ins_len = edit->curs1 - current;
2184 edit_cursor_move (edit, -ins_len);
2185 g_free (buf);
2186 mc_close (file);
2187 if (blocklen != 0)
2188 ins_len = 0;
2191 ret:
2192 g_free (p);
2193 vfs_path_free (vpath);
2194 return ins_len;
2197 /* --------------------------------------------------------------------------------------------- */
2199 * Fill in the edit structure. Return NULL on failure. Pass edit as
2200 * NULL to allocate a new structure.
2202 * If line is 0, try to restore saved position. Otherwise put the
2203 * cursor on that line and show it in the middle of the screen.
2206 WEdit *
2207 edit_init (WEdit * edit, int y, int x, int lines, int cols, const char *filename, long line)
2209 gboolean to_free = FALSE;
2211 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2212 if (option_line_state)
2213 option_line_state_width = LINE_STATE_WIDTH;
2214 else
2215 option_line_state_width = 0;
2217 if (edit == NULL)
2219 #ifdef ENABLE_NLS
2221 * Expand option_whole_chars_search by national letters using
2222 * current locale
2225 static char option_whole_chars_search_buf[256];
2227 if (option_whole_chars_search_buf != option_whole_chars_search)
2229 size_t i;
2230 size_t len = str_term_width1 (option_whole_chars_search);
2232 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2234 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2236 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2238 option_whole_chars_search_buf[len++] = i;
2242 option_whole_chars_search_buf[len] = 0;
2243 option_whole_chars_search = option_whole_chars_search_buf;
2245 #endif /* ENABLE_NLS */
2246 edit = g_malloc0 (sizeof (WEdit));
2247 edit->search = NULL;
2248 to_free = TRUE;
2251 edit_purge_widget (edit);
2252 edit->widget.y = y;
2253 edit->widget.x = x;
2254 edit->widget.lines = lines;
2255 edit->widget.cols = cols;
2257 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2258 edit->stat1.st_uid = getuid ();
2259 edit->stat1.st_gid = getgid ();
2260 edit->stat1.st_mtime = 0;
2262 edit->over_col = 0;
2263 edit->bracket = -1;
2264 edit->force |= REDRAW_PAGE;
2265 edit_set_filename (edit, filename);
2267 edit->undo_stack_size = START_STACK_SIZE;
2268 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2269 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2271 edit->redo_stack_size = START_STACK_SIZE;
2272 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2273 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2275 edit->utf8 = 0;
2276 edit->converter = str_cnv_from_term;
2277 edit_set_codeset (edit);
2279 if (edit_load_file (edit))
2281 /* edit_load_file already gives an error message */
2282 if (to_free)
2283 g_free (edit);
2284 return NULL;
2287 edit->loading_done = 1;
2288 edit->modified = 0;
2289 edit->locked = 0;
2290 edit_load_syntax (edit, NULL, NULL);
2292 int color;
2293 edit_get_syntax_color (edit, -1, &color);
2296 /* load saved cursor position */
2297 if ((line == 0) && option_save_position)
2298 edit_load_position (edit);
2299 else
2301 if (line <= 0)
2302 line = 1;
2303 edit_move_display (edit, line - 1);
2304 edit_move_to_line (edit, line - 1);
2307 edit_load_macro_cmd (edit);
2308 return edit;
2311 /* --------------------------------------------------------------------------------------------- */
2312 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2315 edit_clean (WEdit * edit)
2317 int j = 0;
2319 if (!edit)
2320 return 0;
2322 /* a stale lock, remove it */
2323 if (edit->locked)
2324 edit->locked = edit_unlock_file (edit);
2326 /* save cursor position */
2327 if (option_save_position)
2328 edit_save_position (edit);
2329 else if (edit->serialized_bookmarks != NULL)
2330 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2332 /* File specified on the mcedit command line and never saved */
2333 if (edit->delete_file)
2334 unlink (edit->filename);
2336 edit_free_syntax_rules (edit);
2337 book_mark_flush (edit, -1);
2338 for (; j <= MAXBUFF; j++)
2340 g_free (edit->buffers1[j]);
2341 g_free (edit->buffers2[j]);
2344 g_free (edit->undo_stack);
2345 g_free (edit->redo_stack);
2346 g_free (edit->filename);
2347 g_free (edit->dir);
2348 mc_search_free (edit->search);
2349 edit->search = NULL;
2351 if (edit->converter != str_cnv_from_term)
2352 str_close_conv (edit->converter);
2354 edit_purge_widget (edit);
2356 return 1;
2359 /* --------------------------------------------------------------------------------------------- */
2360 /** returns 1 on success */
2363 edit_renew (WEdit * edit)
2365 int y = edit->widget.y;
2366 int x = edit->widget.x;
2367 int lines = edit->widget.lines;
2368 int columns = edit->widget.cols;
2370 edit_clean (edit);
2371 return (edit_init (edit, y, x, lines, columns, "", 0) != NULL);
2374 /* --------------------------------------------------------------------------------------------- */
2376 * Load a new file into the editor. If it fails, preserve the old file.
2377 * To do it, allocate a new widget, initialize it and, if the new file
2378 * was loaded, copy the data to the old widget.
2379 * Return 1 on success, 0 on failure.
2383 edit_reload (WEdit * edit, const char *filename)
2385 WEdit *e;
2386 int y = edit->widget.y;
2387 int x = edit->widget.x;
2388 int lines = edit->widget.lines;
2389 int columns = edit->widget.cols;
2391 e = g_malloc0 (sizeof (WEdit));
2392 e->widget = edit->widget;
2393 if (edit_init (e, y, x, lines, columns, filename, 0) == NULL)
2395 g_free (e);
2396 return 0;
2398 edit_clean (edit);
2399 memcpy (edit, e, sizeof (WEdit));
2400 g_free (e);
2401 return 1;
2404 /* --------------------------------------------------------------------------------------------- */
2406 * Load a new file into the editor and set line. If it fails, preserve the old file.
2407 * To do it, allocate a new widget, initialize it and, if the new file
2408 * was loaded, copy the data to the old widget.
2409 * Return 1 on success, 0 on failure.
2413 edit_reload_line (WEdit * edit, const char *filename, long line)
2415 WEdit *e;
2416 int y = edit->widget.y;
2417 int x = edit->widget.x;
2418 int lines = edit->widget.lines;
2419 int columns = edit->widget.cols;
2421 e = g_malloc0 (sizeof (WEdit));
2422 e->widget = edit->widget;
2423 if (edit_init (e, y, x, lines, columns, filename, line) == NULL)
2425 g_free (e);
2426 return 0;
2428 edit_clean (edit);
2429 memcpy (edit, e, sizeof (WEdit));
2430 g_free (e);
2431 return 1;
2434 /* --------------------------------------------------------------------------------------------- */
2436 void
2437 edit_set_codeset (WEdit * edit)
2439 #ifdef HAVE_CHARSET
2440 const char *cp_id;
2442 cp_id =
2443 get_codepage_id (mc_global.source_codepage >=
2444 0 ? mc_global.source_codepage : mc_global.display_codepage);
2446 if (cp_id != NULL)
2448 GIConv conv;
2449 conv = str_crt_conv_from (cp_id);
2450 if (conv != INVALID_CONV)
2452 if (edit->converter != str_cnv_from_term)
2453 str_close_conv (edit->converter);
2454 edit->converter = conv;
2458 if (cp_id != NULL)
2459 edit->utf8 = str_isutf8 (cp_id);
2460 #else
2461 (void) edit;
2462 #endif
2466 /* --------------------------------------------------------------------------------------------- */
2468 Recording stack for undo:
2469 The following is an implementation of a compressed stack. Identical
2470 pushes are recorded by a negative prefix indicating the number of times the
2471 same char was pushed. This saves space for repeated curs-left or curs-right
2472 delete etc.
2476 pushed: stored:
2480 b -3
2482 c --> -4
2488 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2489 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2490 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2491 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2492 position.
2494 The only way the cursor moves or the buffer is changed is through the routines:
2495 insert, backspace, insert_ahead, delete, and cursor_move.
2496 These record the reverse undo movements onto the stack each time they are
2497 called.
2499 Each key press results in a set of actions (insert; delete ...). So each time
2500 a key is pressed the current position of start_display is pushed as
2501 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2502 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2503 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2507 void
2508 edit_push_undo_action (WEdit * edit, long c, ...)
2510 unsigned long sp = edit->undo_stack_pointer;
2511 unsigned long spm1;
2512 long *t;
2514 /* first enlarge the stack if necessary */
2515 if (sp > edit->undo_stack_size - 10)
2516 { /* say */
2517 if (option_max_undo < 256)
2518 option_max_undo = 256;
2519 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2521 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2522 if (t)
2524 edit->undo_stack = t;
2525 edit->undo_stack_size <<= 1;
2526 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2530 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2531 if (edit->undo_stack_disable)
2533 edit_push_redo_action (edit, KEY_PRESS);
2534 edit_push_redo_action (edit, c);
2535 return;
2537 else if (edit->redo_stack_reset)
2539 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2542 if (edit->undo_stack_bottom != sp
2543 && spm1 != edit->undo_stack_bottom
2544 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2546 int d;
2547 if (edit->undo_stack[spm1] < 0)
2549 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2550 if (d == c)
2552 if (edit->undo_stack[spm1] > -1000000000)
2554 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2556 edit->undo_stack[spm1]--;
2558 return;
2562 else
2564 d = edit->undo_stack[spm1];
2565 if (d == c)
2567 if (c >= KEY_PRESS)
2568 return; /* --> no need to push multiple do-nothings */
2569 edit->undo_stack[sp] = -2;
2570 goto check_bottom;
2574 edit->undo_stack[sp] = c;
2576 check_bottom:
2577 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2579 /* if the sp wraps round and catches the undo_stack_bottom then erase
2580 * the first set of actions on the stack to make space - by moving
2581 * undo_stack_bottom forward one "key press" */
2582 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2583 if ((unsigned long) c == edit->undo_stack_bottom ||
2584 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2587 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2589 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2590 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2592 /*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: */
2593 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2594 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2596 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2600 void
2601 edit_push_redo_action (WEdit * edit, long c, ...)
2603 unsigned long sp = edit->redo_stack_pointer;
2604 unsigned long spm1;
2605 long *t;
2606 /* first enlarge the stack if necessary */
2607 if (sp > edit->redo_stack_size - 10)
2608 { /* say */
2609 if (option_max_undo < 256)
2610 option_max_undo = 256;
2611 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2613 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2614 if (t)
2616 edit->redo_stack = t;
2617 edit->redo_stack_size <<= 1;
2618 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2622 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2624 if (edit->redo_stack_bottom != sp
2625 && spm1 != edit->redo_stack_bottom
2626 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2628 int d;
2629 if (edit->redo_stack[spm1] < 0)
2631 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2632 if (d == c)
2634 if (edit->redo_stack[spm1] > -1000000000)
2636 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2637 edit->redo_stack[spm1]--;
2638 return;
2642 else
2644 d = edit->redo_stack[spm1];
2645 if (d == c)
2647 if (c >= KEY_PRESS)
2648 return; /* --> no need to push multiple do-nothings */
2649 edit->redo_stack[sp] = -2;
2650 goto redo_check_bottom;
2654 edit->redo_stack[sp] = c;
2656 redo_check_bottom:
2657 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2659 /* if the sp wraps round and catches the redo_stack_bottom then erase
2660 * the first set of actions on the stack to make space - by moving
2661 * redo_stack_bottom forward one "key press" */
2662 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2663 if ((unsigned long) c == edit->redo_stack_bottom ||
2664 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2667 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2669 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2670 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2673 * If a single key produced enough pushes to wrap all the way round then
2674 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2675 * The stack is then initialised:
2678 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2679 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2680 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2684 /* --------------------------------------------------------------------------------------------- */
2686 Basic low level single character buffer alterations and movements at the cursor.
2687 Returns char passed over, inserted or removed.
2690 void
2691 edit_insert (WEdit * edit, int c)
2693 /* check if file has grown to large */
2694 if (edit->last_byte >= SIZE_LIMIT)
2695 return;
2697 /* first we must update the position of the display window */
2698 if (edit->curs1 < edit->start_display)
2700 edit->start_display++;
2701 if (c == '\n')
2702 edit->start_line++;
2705 /* Mark file as modified, unless the file hasn't been fully loaded */
2706 if (edit->loading_done)
2708 edit_modification (edit);
2711 /* now we must update some info on the file and check if a redraw is required */
2712 if (c == '\n')
2714 if (edit->book_mark)
2715 book_mark_inc (edit, edit->curs_line);
2716 edit->curs_line++;
2717 edit->total_lines++;
2718 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2721 /* save the reverse command onto the undo stack */
2722 /* ordinary char and not space */
2723 if (c > 32)
2724 edit_push_undo_action (edit, BACKSPACE);
2725 else
2726 edit_push_undo_action (edit, BACKSPACE_BR);
2727 /* update markers */
2728 edit->mark1 += (edit->mark1 > edit->curs1);
2729 edit->mark2 += (edit->mark2 > edit->curs1);
2730 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2732 /* add a new buffer if we've reached the end of the last one */
2733 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2734 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2736 /* perform the insertion */
2737 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2738 = (unsigned char) c;
2740 /* update file length */
2741 edit->last_byte++;
2743 /* update cursor position */
2744 edit->curs1++;
2747 /* --------------------------------------------------------------------------------------------- */
2748 /** same as edit_insert and move left */
2750 void
2751 edit_insert_ahead (WEdit * edit, int c)
2753 if (edit->last_byte >= SIZE_LIMIT)
2754 return;
2756 if (edit->curs1 < edit->start_display)
2758 edit->start_display++;
2759 if (c == '\n')
2760 edit->start_line++;
2762 edit_modification (edit);
2763 if (c == '\n')
2765 if (edit->book_mark)
2766 book_mark_inc (edit, edit->curs_line);
2767 edit->total_lines++;
2768 edit->force |= REDRAW_AFTER_CURSOR;
2770 /* ordinary char and not space */
2771 if (c > 32)
2772 edit_push_undo_action (edit, DELCHAR);
2773 else
2774 edit_push_undo_action (edit, DELCHAR_BR);
2776 edit->mark1 += (edit->mark1 >= edit->curs1);
2777 edit->mark2 += (edit->mark2 >= edit->curs1);
2778 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2780 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2781 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2782 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2783 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2785 edit->last_byte++;
2786 edit->curs2++;
2790 /* --------------------------------------------------------------------------------------------- */
2793 edit_delete (WEdit * edit, const int byte_delete)
2795 int p = 0;
2796 int cw = 1;
2797 int i;
2799 if (!edit->curs2)
2800 return 0;
2802 cw = 1;
2803 /* if byte_delete = 1 then delete only one byte not multibyte char */
2804 if (edit->utf8 && byte_delete == 0)
2806 edit_get_utf (edit, edit->curs1, &cw);
2807 if (cw < 1)
2808 cw = 1;
2811 if (edit->mark2 != edit->mark1)
2812 edit_push_markers (edit);
2814 for (i = 1; i <= cw; i++)
2816 if (edit->mark1 > edit->curs1)
2818 edit->mark1--;
2819 edit->end_mark_curs--;
2821 if (edit->mark2 > edit->curs1)
2822 edit->mark2--;
2823 if (edit->last_get_rule > edit->curs1)
2824 edit->last_get_rule--;
2826 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2827 ((edit->curs2 -
2828 1) & M_EDIT_BUF_SIZE) - 1];
2830 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2832 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2833 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2835 edit->last_byte--;
2836 edit->curs2--;
2837 edit_push_undo_action (edit, p + 256);
2840 edit_modification (edit);
2841 if (p == '\n')
2843 if (edit->book_mark)
2844 book_mark_dec (edit, edit->curs_line);
2845 edit->total_lines--;
2846 edit->force |= REDRAW_AFTER_CURSOR;
2848 if (edit->curs1 < edit->start_display)
2850 edit->start_display--;
2851 if (p == '\n')
2852 edit->start_line--;
2855 return p;
2858 /* --------------------------------------------------------------------------------------------- */
2859 /** moves the cursor right or left: increment positive or negative respectively */
2861 void
2862 edit_cursor_move (WEdit * edit, long increment)
2864 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2865 int c;
2867 if (increment < 0)
2869 for (; increment < 0; increment++)
2871 if (!edit->curs1)
2872 return;
2874 edit_push_undo_action (edit, CURS_RIGHT);
2876 c = edit_get_byte (edit, edit->curs1 - 1);
2877 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2878 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2879 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2880 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2881 edit->curs2++;
2882 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2883 1) & M_EDIT_BUF_SIZE];
2884 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2886 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2887 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2889 edit->curs1--;
2890 if (c == '\n')
2892 edit->curs_line--;
2893 edit->force |= REDRAW_LINE_BELOW;
2898 else if (increment > 0)
2900 for (; increment > 0; increment--)
2902 if (!edit->curs2)
2903 return;
2905 edit_push_undo_action (edit, CURS_LEFT);
2907 c = edit_get_byte (edit, edit->curs1);
2908 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2909 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2910 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2911 edit->curs1++;
2912 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2913 ((edit->curs2 -
2914 1) & M_EDIT_BUF_SIZE) - 1];
2915 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2917 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2918 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2920 edit->curs2--;
2921 if (c == '\n')
2923 edit->curs_line++;
2924 edit->force |= REDRAW_LINE_ABOVE;
2930 /* These functions return positions relative to lines */
2932 /* --------------------------------------------------------------------------------------------- */
2933 /** returns index of last char on line + 1 */
2935 long
2936 edit_eol (WEdit * edit, long current)
2938 if (current >= edit->last_byte)
2939 return edit->last_byte;
2941 for (;; current++)
2942 if (edit_get_byte (edit, current) == '\n')
2943 break;
2944 return current;
2947 /* --------------------------------------------------------------------------------------------- */
2948 /** returns index of first char on line */
2950 long
2951 edit_bol (WEdit * edit, long current)
2953 if (current <= 0)
2954 return 0;
2956 for (;; current--)
2957 if (edit_get_byte (edit, current - 1) == '\n')
2958 break;
2959 return current;
2962 /* --------------------------------------------------------------------------------------------- */
2964 long
2965 edit_count_lines (WEdit * edit, long current, long upto)
2967 long lines = 0;
2968 if (upto > edit->last_byte)
2969 upto = edit->last_byte;
2970 if (current < 0)
2971 current = 0;
2972 while (current < upto)
2973 if (edit_get_byte (edit, current++) == '\n')
2974 lines++;
2975 return lines;
2978 /* --------------------------------------------------------------------------------------------- */
2979 /* If lines is zero this returns the count of lines from current to upto. */
2980 /* If upto is zero returns index of lines forward current. */
2982 long
2983 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2985 if (upto)
2987 return edit_count_lines (edit, current, upto);
2989 else
2991 long next;
2992 if (lines < 0)
2993 lines = 0;
2994 while (lines--)
2996 next = edit_eol (edit, current) + 1;
2997 if (next > edit->last_byte)
2998 break;
2999 else
3000 current = next;
3002 return current;
3006 /* --------------------------------------------------------------------------------------------- */
3007 /** Returns offset of 'lines' lines up from current */
3009 long
3010 edit_move_backward (WEdit * edit, long current, long lines)
3012 if (lines < 0)
3013 lines = 0;
3014 current = edit_bol (edit, current);
3015 while ((lines--) && current != 0)
3016 current = edit_bol (edit, current - 1);
3017 return current;
3020 /* --------------------------------------------------------------------------------------------- */
3021 /* If cols is zero this returns the count of columns from current to upto. */
3022 /* If upto is zero returns index of cols across from current. */
3024 long
3025 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3027 long p, q;
3028 int col;
3030 if (upto)
3032 q = upto;
3033 cols = -10;
3035 else
3036 q = edit->last_byte + 2;
3038 for (col = 0, p = current; p < q; p++)
3040 int c, orig_c;
3042 if (cols != -10)
3044 if (col == cols)
3045 return p;
3046 if (col > cols)
3047 return p - 1;
3050 orig_c = c = edit_get_byte (edit, p);
3052 #ifdef HAVE_CHARSET
3053 if (edit->utf8)
3055 int utf_ch;
3056 int cw = 1;
3058 utf_ch = edit_get_utf (edit, p, &cw);
3059 if (mc_global.utf8_display)
3061 if (cw > 1)
3062 col -= cw - 1;
3063 if (g_unichar_iswide (utf_ch))
3064 col++;
3066 else if (cw > 1 && g_unichar_isprint (utf_ch))
3067 col -= cw - 1;
3070 c = convert_to_display_c (c);
3071 #endif
3073 if (c == '\t')
3074 col += TAB_SIZE - col % TAB_SIZE;
3075 else if (c == '\n')
3077 if (upto)
3078 return col;
3079 else
3080 return p;
3082 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3083 /* '\r' is shown as ^M, so we must advance 2 characters */
3084 /* Caret notation for control characters */
3085 col += 2;
3086 else
3087 col++;
3089 return col;
3092 /* --------------------------------------------------------------------------------------------- */
3093 /** returns the current column position of the cursor */
3096 edit_get_col (WEdit * edit)
3098 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3101 /* --------------------------------------------------------------------------------------------- */
3102 /* Scrolling functions */
3103 /* --------------------------------------------------------------------------------------------- */
3105 void
3106 edit_update_curs_row (WEdit * edit)
3108 edit->curs_row = edit->curs_line - edit->start_line;
3111 /* --------------------------------------------------------------------------------------------- */
3113 void
3114 edit_update_curs_col (WEdit * edit)
3116 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3119 /* --------------------------------------------------------------------------------------------- */
3122 edit_get_curs_col (const WEdit * edit)
3124 return edit->curs_col;
3127 /* --------------------------------------------------------------------------------------------- */
3128 /** moves the display start position up by i lines */
3130 void
3131 edit_scroll_upward (WEdit * edit, unsigned long i)
3133 unsigned long lines_above = edit->start_line;
3134 if (i > lines_above)
3135 i = lines_above;
3136 if (i)
3138 edit->start_line -= i;
3139 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3140 edit->force |= REDRAW_PAGE;
3141 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3143 edit_update_curs_row (edit);
3147 /* --------------------------------------------------------------------------------------------- */
3148 /** returns 1 if could scroll, 0 otherwise */
3150 void
3151 edit_scroll_downward (WEdit * edit, int i)
3153 int lines_below;
3154 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3155 if (lines_below > 0)
3157 if (i > lines_below)
3158 i = lines_below;
3159 edit->start_line += i;
3160 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3161 edit->force |= REDRAW_PAGE;
3162 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3164 edit_update_curs_row (edit);
3167 /* --------------------------------------------------------------------------------------------- */
3169 void
3170 edit_scroll_right (WEdit * edit, int i)
3172 edit->force |= REDRAW_PAGE;
3173 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3174 edit->start_col -= i;
3177 /* --------------------------------------------------------------------------------------------- */
3179 void
3180 edit_scroll_left (WEdit * edit, int i)
3182 if (edit->start_col)
3184 edit->start_col += i;
3185 if (edit->start_col > 0)
3186 edit->start_col = 0;
3187 edit->force |= REDRAW_PAGE;
3188 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3192 /* --------------------------------------------------------------------------------------------- */
3193 /* high level cursor movement commands */
3194 /* --------------------------------------------------------------------------------------------- */
3196 void
3197 edit_move_to_prev_col (WEdit * edit, long p)
3199 int prev = edit->prev_col;
3200 int over = edit->over_col;
3201 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3203 if (option_cursor_beyond_eol)
3205 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3206 edit_eol (edit, edit->curs1));
3208 if (line_len < prev + edit->over_col)
3210 edit->over_col = prev + over - line_len;
3211 edit->prev_col = line_len;
3212 edit->curs_col = line_len;
3214 else
3216 edit->curs_col = prev + over;
3217 edit->prev_col = edit->curs_col;
3218 edit->over_col = 0;
3221 else
3223 edit->over_col = 0;
3224 if (is_in_indent (edit) && option_fake_half_tabs)
3226 edit_update_curs_col (edit);
3227 if (space_width)
3228 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3230 int q = edit->curs_col;
3231 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3232 p = edit_bol (edit, edit->curs1);
3233 edit_cursor_move (edit,
3234 edit_move_forward3 (edit, p, edit->curs_col,
3235 0) - edit->curs1);
3236 if (!left_of_four_spaces (edit))
3237 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3243 /* --------------------------------------------------------------------------------------------- */
3246 line_is_blank (WEdit * edit, long line)
3248 return is_blank (edit, edit_find_line (edit, line));
3251 /* --------------------------------------------------------------------------------------------- */
3252 /** move cursor to line 'line' */
3254 void
3255 edit_move_to_line (WEdit * e, long line)
3257 if (line < e->curs_line)
3258 edit_move_up (e, e->curs_line - line, 0);
3259 else
3260 edit_move_down (e, line - e->curs_line, 0);
3261 edit_scroll_screen_over_cursor (e);
3264 /* --------------------------------------------------------------------------------------------- */
3265 /** scroll window so that first visible line is 'line' */
3267 void
3268 edit_move_display (WEdit * e, long line)
3270 if (line < e->start_line)
3271 edit_scroll_upward (e, e->start_line - line);
3272 else
3273 edit_scroll_downward (e, line - e->start_line);
3276 /* --------------------------------------------------------------------------------------------- */
3277 /** save markers onto undo stack */
3279 void
3280 edit_push_markers (WEdit * edit)
3282 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3283 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3284 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3287 /* --------------------------------------------------------------------------------------------- */
3289 void
3290 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3292 edit->mark1 = m1;
3293 edit->mark2 = m2;
3294 edit->column1 = c1;
3295 edit->column2 = c2;
3299 /* --------------------------------------------------------------------------------------------- */
3300 /** highlight marker toggle */
3302 void
3303 edit_mark_cmd (WEdit * edit, int unmark)
3305 edit_push_markers (edit);
3306 if (unmark)
3308 edit_set_markers (edit, 0, 0, 0, 0);
3309 edit->force |= REDRAW_PAGE;
3311 else
3313 if (edit->mark2 >= 0)
3315 edit->end_mark_curs = -1;
3316 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3317 edit->curs_col + edit->over_col);
3318 edit->force |= REDRAW_PAGE;
3320 else
3322 edit->end_mark_curs = edit->curs1;
3323 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3324 edit->curs_col + edit->over_col);
3329 /* --------------------------------------------------------------------------------------------- */
3330 /** highlight the word under cursor */
3332 void
3333 edit_mark_current_word_cmd (WEdit * edit)
3335 long pos;
3337 for (pos = edit->curs1; pos != 0; pos--)
3339 int c1, c2;
3341 c1 = edit_get_byte (edit, pos);
3342 c2 = edit_get_byte (edit, pos - 1);
3343 if (!isspace (c1) && isspace (c2))
3344 break;
3345 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3346 break;
3348 edit->mark1 = pos;
3350 for (; pos < edit->last_byte; pos++)
3352 int c1, c2;
3354 c1 = edit_get_byte (edit, pos);
3355 c2 = edit_get_byte (edit, pos + 1);
3356 if (!isspace (c1) && isspace (c2))
3357 break;
3358 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3359 break;
3361 edit->mark2 = min (pos + 1, edit->last_byte);
3363 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3366 /* --------------------------------------------------------------------------------------------- */
3368 void
3369 edit_mark_current_line_cmd (WEdit * edit)
3371 long pos = edit->curs1;
3373 edit->mark1 = edit_bol (edit, pos);
3374 edit->mark2 = edit_eol (edit, pos);
3376 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3379 /* --------------------------------------------------------------------------------------------- */
3381 void
3382 edit_delete_line (WEdit * edit)
3385 * Delete right part of the line.
3386 * Note that edit_get_byte() returns '\n' when byte position is
3387 * beyond EOF.
3389 while (edit_get_byte (edit, edit->curs1) != '\n')
3391 (void) edit_delete (edit, 1);
3395 * Delete '\n' char.
3396 * Note that edit_delete() will not corrupt anything if called while
3397 * cursor position is EOF.
3399 (void) edit_delete (edit, 1);
3402 * Delete left part of the line.
3403 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3405 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3407 (void) edit_backspace (edit, 1);
3411 /* --------------------------------------------------------------------------------------------- */
3414 edit_indent_width (WEdit * edit, long p)
3416 long q = p;
3417 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3418 q++;
3419 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3422 /* --------------------------------------------------------------------------------------------- */
3424 void
3425 edit_insert_indent (WEdit * edit, int indent)
3427 if (!option_fill_tabs_with_spaces)
3429 while (indent >= TAB_SIZE)
3431 edit_insert (edit, '\t');
3432 indent -= TAB_SIZE;
3435 while (indent-- > 0)
3436 edit_insert (edit, ' ');
3439 /* --------------------------------------------------------------------------------------------- */
3441 void
3442 edit_push_key_press (WEdit * edit)
3444 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3445 if (edit->mark2 == -1)
3447 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3448 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3452 /* --------------------------------------------------------------------------------------------- */
3454 void
3455 edit_find_bracket (WEdit * edit)
3457 edit->bracket = edit_get_bracket (edit, 1, 10000);
3458 if (last_bracket != edit->bracket)
3459 edit->force |= REDRAW_PAGE;
3460 last_bracket = edit->bracket;
3463 /* --------------------------------------------------------------------------------------------- */
3465 * This executes a command as though the user initiated it through a key
3466 * press. Callback with WIDGET_KEY as a message calls this after
3467 * translating the key press. This function can be used to pass any
3468 * command to the editor. Note that the screen wouldn't update
3469 * automatically. Either of command or char_for_insertion must be
3470 * passed as -1. Commands are executed, and char_for_insertion is
3471 * inserted at the cursor.
3474 void
3475 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3477 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3478 || (macro_index < 0
3479 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3481 macro_index = 0;
3482 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3483 return;
3485 if (macro_index != -1)
3487 edit->force |= REDRAW_COMPLETELY;
3488 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3490 edit_store_macro_cmd (edit);
3491 macro_index = -1;
3492 return;
3494 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3496 edit_repeat_macro_cmd (edit);
3497 macro_index = -1;
3498 return;
3502 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3504 record_macro_buf[macro_index].action = command;
3505 record_macro_buf[macro_index++].ch = char_for_insertion;
3507 /* record the beginning of a set of editing actions initiated by a key press */
3508 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3509 edit_push_key_press (edit);
3511 edit_execute_cmd (edit, command, char_for_insertion);
3512 if (edit->column_highlight)
3513 edit->force |= REDRAW_PAGE;
3516 /* --------------------------------------------------------------------------------------------- */
3518 This executes a command at a lower level than macro recording.
3519 It also does not push a key_press onto the undo stack. This means
3520 that if it is called many times, a single undo command will undo
3521 all of them. It also does not check for the Undo command.
3523 void
3524 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3526 edit->force |= REDRAW_LINE;
3528 /* The next key press will unhighlight the found string, so update
3529 * the whole page */
3530 if (edit->found_len || edit->column_highlight)
3531 edit->force |= REDRAW_PAGE;
3533 switch (command)
3535 /* a mark command with shift-arrow */
3536 case CK_MarkLeft:
3537 case CK_MarkRight:
3538 case CK_MarkToWordBegin:
3539 case CK_MarkToWordEnd:
3540 case CK_MarkToHome:
3541 case CK_MarkToEnd:
3542 case CK_MarkUp:
3543 case CK_MarkDown:
3544 case CK_MarkPageUp:
3545 case CK_MarkPageDown:
3546 case CK_MarkToFileBegin:
3547 case CK_MarkToFileEnd:
3548 case CK_MarkToPageBegin:
3549 case CK_MarkToPageEnd:
3550 case CK_MarkScrollUp:
3551 case CK_MarkScrollDown:
3552 case CK_MarkParagraphUp:
3553 case CK_MarkParagraphDown:
3554 /* a mark command with alt-arrow */
3555 case CK_MarkColumnPageUp:
3556 case CK_MarkColumnPageDown:
3557 case CK_MarkColumnLeft:
3558 case CK_MarkColumnRight:
3559 case CK_MarkColumnUp:
3560 case CK_MarkColumnDown:
3561 case CK_MarkColumnScrollUp:
3562 case CK_MarkColumnScrollDown:
3563 case CK_MarkColumnParagraphUp:
3564 case CK_MarkColumnParagraphDown:
3565 edit->column_highlight = 0;
3566 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3568 edit_mark_cmd (edit, 1); /* clear */
3569 edit_mark_cmd (edit, 0); /* marking on */
3571 edit->highlight = 1;
3572 break;
3574 /* any other command */
3575 default:
3576 if (edit->highlight)
3577 edit_mark_cmd (edit, 0); /* clear */
3578 edit->highlight = 0;
3581 /* first check for undo */
3582 if (command == CK_Undo)
3584 edit->redo_stack_reset = 0;
3585 edit_group_undo (edit);
3586 edit->found_len = 0;
3587 edit->prev_col = edit_get_col (edit);
3588 edit->search_start = edit->curs1;
3589 return;
3591 /* check for redo */
3592 if (command == CK_Redo)
3594 edit->redo_stack_reset = 0;
3595 edit_do_redo (edit);
3596 edit->found_len = 0;
3597 edit->prev_col = edit_get_col (edit);
3598 edit->search_start = edit->curs1;
3599 return;
3602 edit->redo_stack_reset = 1;
3604 /* An ordinary key press */
3605 if (char_for_insertion >= 0)
3607 /* if non persistent selection and text selected */
3608 if (!option_persistent_selections)
3610 if (edit->mark1 != edit->mark2)
3611 edit_block_delete_cmd (edit);
3613 if (edit->overwrite)
3615 /* remove char only one time, after input first byte, multibyte chars */
3616 if ((!mc_global.utf8_display || edit->charpoint == 0)
3617 && edit_get_byte (edit, edit->curs1) != '\n')
3618 edit_delete (edit, 0);
3620 if (option_cursor_beyond_eol && edit->over_col > 0)
3621 edit_insert_over (edit);
3622 #ifdef HAVE_CHARSET
3623 if (char_for_insertion > 255 && !mc_global.utf8_display)
3625 unsigned char str[6 + 1];
3626 size_t i = 0;
3627 int res;
3629 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3630 if (res == 0)
3632 str[0] = '.';
3633 str[1] = '\0';
3635 else
3637 str[res] = '\0';
3639 while (str[i] != 0 && i <= 6)
3641 char_for_insertion = str[i];
3642 edit_insert (edit, char_for_insertion);
3643 i++;
3646 else
3647 #endif
3648 edit_insert (edit, char_for_insertion);
3650 if (option_auto_para_formatting)
3652 format_paragraph (edit, 0);
3653 edit->force |= REDRAW_PAGE;
3655 else
3656 check_and_wrap_line (edit);
3657 edit->found_len = 0;
3658 edit->prev_col = edit_get_col (edit);
3659 edit->search_start = edit->curs1;
3660 edit_find_bracket (edit);
3661 return;
3664 switch (command)
3666 case CK_TopOnScreen:
3667 case CK_BottomOnScreen:
3668 case CK_Top:
3669 case CK_Bottom:
3670 case CK_PageUp:
3671 case CK_PageDown:
3672 case CK_Home:
3673 case CK_End:
3674 case CK_Up:
3675 case CK_Down:
3676 case CK_Left:
3677 case CK_Right:
3678 case CK_WordLeft:
3679 case CK_WordRight:
3680 if (edit->mark2 >= 0)
3682 if (!option_persistent_selections)
3684 if (edit->column_highlight)
3685 edit_push_undo_action (edit, COLUMN_ON);
3686 edit->column_highlight = 0;
3687 edit_mark_cmd (edit, 1);
3692 switch (command)
3694 case CK_TopOnScreen:
3695 case CK_BottomOnScreen:
3696 case CK_MarkToPageBegin:
3697 case CK_MarkToPageEnd:
3698 case CK_Up:
3699 case CK_Down:
3700 case CK_WordLeft:
3701 case CK_WordRight:
3702 case CK_MarkToWordBegin:
3703 case CK_MarkToWordEnd:
3704 case CK_MarkUp:
3705 case CK_MarkDown:
3706 case CK_MarkColumnUp:
3707 case CK_MarkColumnDown:
3708 if (edit->mark2 == -1)
3709 break; /*marking is following the cursor: may need to highlight a whole line */
3710 case CK_Left:
3711 case CK_Right:
3712 case CK_MarkLeft:
3713 case CK_MarkRight:
3714 edit->force |= REDRAW_CHAR_ONLY;
3717 /* basic cursor key commands */
3718 switch (command)
3720 case CK_BackSpace:
3721 /* if non persistent selection and text selected */
3722 if (!option_persistent_selections)
3724 if (edit->mark1 != edit->mark2)
3726 edit_block_delete_cmd (edit);
3727 break;
3730 if (option_cursor_beyond_eol && edit->over_col > 0)
3732 edit->over_col--;
3733 break;
3735 if (option_backspace_through_tabs && is_in_indent (edit))
3737 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3738 edit_backspace (edit, 1);
3739 break;
3741 else
3743 if (option_fake_half_tabs)
3745 int i;
3746 if (is_in_indent (edit) && right_of_four_spaces (edit))
3748 for (i = 0; i < HALF_TAB_SIZE; i++)
3749 edit_backspace (edit, 1);
3750 break;
3754 edit_backspace (edit, 0);
3755 break;
3756 case CK_Delete:
3757 /* if non persistent selection and text selected */
3758 if (!option_persistent_selections)
3760 if (edit->mark1 != edit->mark2)
3762 edit_block_delete_cmd (edit);
3763 break;
3767 if (option_cursor_beyond_eol && edit->over_col > 0)
3768 edit_insert_over (edit);
3770 if (option_fake_half_tabs)
3772 int i;
3773 if (is_in_indent (edit) && left_of_four_spaces (edit))
3775 for (i = 1; i <= HALF_TAB_SIZE; i++)
3776 edit_delete (edit, 1);
3777 break;
3780 edit_delete (edit, 0);
3781 break;
3782 case CK_DeleteToWordBegin:
3783 edit->over_col = 0;
3784 edit_left_delete_word (edit);
3785 break;
3786 case CK_DeleteToWordEnd:
3787 if (option_cursor_beyond_eol && edit->over_col > 0)
3788 edit_insert_over (edit);
3790 edit_right_delete_word (edit);
3791 break;
3792 case CK_DeleteLine:
3793 edit_delete_line (edit);
3794 break;
3795 case CK_DeleteToHome:
3796 edit_delete_to_line_begin (edit);
3797 break;
3798 case CK_DeleteToEnd:
3799 edit_delete_to_line_end (edit);
3800 break;
3801 case CK_Enter:
3802 edit->over_col = 0;
3803 if (option_auto_para_formatting)
3805 edit_double_newline (edit);
3806 if (option_return_does_auto_indent)
3807 edit_auto_indent (edit);
3808 format_paragraph (edit, 0);
3810 else
3812 edit_insert (edit, '\n');
3813 if (option_return_does_auto_indent)
3815 edit_auto_indent (edit);
3818 break;
3819 case CK_Return:
3820 edit_insert (edit, '\n');
3821 break;
3823 case CK_MarkColumnPageUp:
3824 edit->column_highlight = 1;
3825 case CK_PageUp:
3826 case CK_MarkPageUp:
3827 edit_move_up (edit, edit->widget.lines - 1, 1);
3828 break;
3829 case CK_MarkColumnPageDown:
3830 edit->column_highlight = 1;
3831 case CK_PageDown:
3832 case CK_MarkPageDown:
3833 edit_move_down (edit, edit->widget.lines - 1, 1);
3834 break;
3835 case CK_MarkColumnLeft:
3836 edit->column_highlight = 1;
3837 case CK_Left:
3838 case CK_MarkLeft:
3839 if (option_fake_half_tabs)
3841 if (is_in_indent (edit) && right_of_four_spaces (edit))
3843 if (option_cursor_beyond_eol && edit->over_col > 0)
3844 edit->over_col--;
3845 else
3846 edit_cursor_move (edit, -HALF_TAB_SIZE);
3847 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3848 break;
3851 edit_left_char_move_cmd (edit);
3852 break;
3853 case CK_MarkColumnRight:
3854 edit->column_highlight = 1;
3855 case CK_Right:
3856 case CK_MarkRight:
3857 if (option_fake_half_tabs)
3859 if (is_in_indent (edit) && left_of_four_spaces (edit))
3861 edit_cursor_move (edit, HALF_TAB_SIZE);
3862 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3863 break;
3866 edit_right_char_move_cmd (edit);
3867 break;
3868 case CK_TopOnScreen:
3869 case CK_MarkToPageBegin:
3870 edit_begin_page (edit);
3871 break;
3872 case CK_BottomOnScreen:
3873 case CK_MarkToPageEnd:
3874 edit_end_page (edit);
3875 break;
3876 case CK_WordLeft:
3877 case CK_MarkToWordBegin:
3878 edit->over_col = 0;
3879 edit_left_word_move_cmd (edit);
3880 break;
3881 case CK_WordRight:
3882 case CK_MarkToWordEnd:
3883 edit->over_col = 0;
3884 edit_right_word_move_cmd (edit);
3885 break;
3886 case CK_MarkColumnUp:
3887 edit->column_highlight = 1;
3888 case CK_Up:
3889 case CK_MarkUp:
3890 edit_move_up (edit, 1, 0);
3891 break;
3892 case CK_MarkColumnDown:
3893 edit->column_highlight = 1;
3894 case CK_Down:
3895 case CK_MarkDown:
3896 edit_move_down (edit, 1, 0);
3897 break;
3898 case CK_MarkColumnParagraphUp:
3899 edit->column_highlight = 1;
3900 case CK_ParagraphUp:
3901 case CK_MarkParagraphUp:
3902 edit_move_up_paragraph (edit, 0);
3903 break;
3904 case CK_MarkColumnParagraphDown:
3905 edit->column_highlight = 1;
3906 case CK_ParagraphDown:
3907 case CK_MarkParagraphDown:
3908 edit_move_down_paragraph (edit, 0);
3909 break;
3910 case CK_MarkColumnScrollUp:
3911 edit->column_highlight = 1;
3912 case CK_ScrollUp:
3913 case CK_MarkScrollUp:
3914 edit_move_up (edit, 1, 1);
3915 break;
3916 case CK_MarkColumnScrollDown:
3917 edit->column_highlight = 1;
3918 case CK_ScrollDown:
3919 case CK_MarkScrollDown:
3920 edit_move_down (edit, 1, 1);
3921 break;
3922 case CK_Home:
3923 case CK_MarkToHome:
3924 edit_cursor_to_bol (edit);
3925 break;
3926 case CK_End:
3927 case CK_MarkToEnd:
3928 edit_cursor_to_eol (edit);
3929 break;
3930 case CK_Tab:
3931 /* if text marked shift block */
3932 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3934 if (edit->mark2 < 0)
3935 edit_mark_cmd (edit, 0);
3936 edit_move_block_to_right (edit);
3938 else
3940 if (option_cursor_beyond_eol)
3941 edit_insert_over (edit);
3942 edit_tab_cmd (edit);
3943 if (option_auto_para_formatting)
3945 format_paragraph (edit, 0);
3946 edit->force |= REDRAW_PAGE;
3948 else
3950 check_and_wrap_line (edit);
3953 break;
3955 case CK_InsertOverwrite:
3956 edit->overwrite = !edit->overwrite;
3957 break;
3959 case CK_Mark:
3960 if (edit->mark2 >= 0)
3962 if (edit->column_highlight)
3963 edit_push_undo_action (edit, COLUMN_ON);
3964 edit->column_highlight = 0;
3966 edit_mark_cmd (edit, 0);
3967 break;
3968 case CK_MarkColumn:
3969 if (!edit->column_highlight)
3970 edit_push_undo_action (edit, COLUMN_OFF);
3971 edit->column_highlight = 1;
3972 edit_mark_cmd (edit, 0);
3973 break;
3974 case CK_MarkAll:
3975 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3976 edit->force |= REDRAW_PAGE;
3977 break;
3978 case CK_Unmark:
3979 if (edit->column_highlight)
3980 edit_push_undo_action (edit, COLUMN_ON);
3981 edit->column_highlight = 0;
3982 edit_mark_cmd (edit, 1);
3983 break;
3984 case CK_MarkWord:
3985 if (edit->column_highlight)
3986 edit_push_undo_action (edit, COLUMN_ON);
3987 edit->column_highlight = 0;
3988 edit_mark_current_word_cmd (edit);
3989 break;
3990 case CK_MarkLine:
3991 if (edit->column_highlight)
3992 edit_push_undo_action (edit, COLUMN_ON);
3993 edit->column_highlight = 0;
3994 edit_mark_current_line_cmd (edit);
3995 break;
3997 case CK_ShowNumbers:
3998 option_line_state = !option_line_state;
3999 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
4000 edit->force |= REDRAW_PAGE;
4001 break;
4003 case CK_ShowMargin:
4004 show_right_margin = !show_right_margin;
4005 edit->force |= REDRAW_PAGE;
4006 break;
4008 case CK_Bookmark:
4009 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4010 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4011 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4012 else
4013 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4014 break;
4015 case CK_BookmarkFlush:
4016 book_mark_flush (edit, BOOK_MARK_COLOR);
4017 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4018 edit->force |= REDRAW_PAGE;
4019 break;
4020 case CK_BookmarkNext:
4021 if (edit->book_mark)
4023 struct _book_mark *p;
4024 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4025 if (p->next)
4027 p = p->next;
4028 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4029 edit_move_display (edit, p->line - edit->widget.lines / 2);
4030 edit_move_to_line (edit, p->line);
4033 break;
4034 case CK_BookmarkPrev:
4035 if (edit->book_mark)
4037 struct _book_mark *p;
4038 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4039 while (p->line == edit->curs_line)
4040 if (p->prev)
4041 p = p->prev;
4042 if (p->line >= 0)
4044 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4045 edit_move_display (edit, p->line - edit->widget.lines / 2);
4046 edit_move_to_line (edit, p->line);
4049 break;
4051 case CK_Top:
4052 case CK_MarkToFileBegin:
4053 edit_move_to_top (edit);
4054 break;
4055 case CK_Bottom:
4056 case CK_MarkToFileEnd:
4057 edit_move_to_bottom (edit);
4058 break;
4060 case CK_Copy:
4061 if (option_cursor_beyond_eol && edit->over_col > 0)
4062 edit_insert_over (edit);
4063 edit_block_copy_cmd (edit);
4064 break;
4065 case CK_Remove:
4066 edit_block_delete_cmd (edit);
4067 break;
4068 case CK_Move:
4069 edit_block_move_cmd (edit);
4070 break;
4072 case CK_BlockShiftLeft:
4073 if (edit->mark1 != edit->mark2)
4074 edit_move_block_to_left (edit);
4075 break;
4076 case CK_BlockShiftRight:
4077 if (edit->mark1 != edit->mark2)
4078 edit_move_block_to_right (edit);
4079 break;
4080 case CK_Store:
4081 edit_copy_to_X_buf_cmd (edit);
4082 break;
4083 case CK_Cut:
4084 edit_cut_to_X_buf_cmd (edit);
4085 break;
4086 case CK_Paste:
4087 /* if non persistent selection and text selected */
4088 if (!option_persistent_selections)
4090 if (edit->mark1 != edit->mark2)
4091 edit_block_delete_cmd (edit);
4093 if (option_cursor_beyond_eol && edit->over_col > 0)
4094 edit_insert_over (edit);
4095 edit_paste_from_X_buf_cmd (edit);
4096 break;
4097 case CK_History:
4098 edit_paste_from_history (edit);
4099 break;
4101 case CK_SaveAs:
4102 edit_save_as_cmd (edit);
4103 break;
4104 case CK_Save:
4105 edit_save_confirm_cmd (edit);
4106 break;
4107 case CK_EditFile:
4108 edit_load_cmd (edit, EDIT_FILE_COMMON);
4109 break;
4110 case CK_BlockSave:
4111 edit_save_block_cmd (edit);
4112 break;
4113 case CK_InsertFile:
4114 edit_insert_file_cmd (edit);
4115 break;
4117 case CK_FilePrev:
4118 edit_load_back_cmd (edit);
4119 break;
4120 case CK_FileNext:
4121 edit_load_forward_cmd (edit);
4122 break;
4124 case CK_EditSyntaxFile:
4125 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4126 break;
4127 case CK_SyntaxChoose:
4128 edit_syntax_dialog (edit);
4129 break;
4131 case CK_EditUserMenu:
4132 edit_load_cmd (edit, EDIT_FILE_MENU);
4133 break;
4135 case CK_SyntaxOnOff:
4136 option_syntax_highlighting ^= 1;
4137 if (option_syntax_highlighting == 1)
4138 edit_load_syntax (edit, NULL, edit->syntax_type);
4139 edit->force |= REDRAW_PAGE;
4140 break;
4142 case CK_ShowTabTws:
4143 enable_show_tabs_tws ^= 1;
4144 edit->force |= REDRAW_PAGE;
4145 break;
4147 case CK_Search:
4148 edit_search_cmd (edit, FALSE);
4149 break;
4150 case CK_SearchContinue:
4151 edit_search_cmd (edit, TRUE);
4152 break;
4153 case CK_Replace:
4154 edit_replace_cmd (edit, 0);
4155 break;
4156 case CK_ReplaceContinue:
4157 edit_replace_cmd (edit, 1);
4158 break;
4159 case CK_Complete:
4160 /* if text marked shift block */
4161 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4163 edit_move_block_to_left (edit);
4165 else
4167 edit_complete_word_cmd (edit);
4169 break;
4170 case CK_Find:
4171 edit_get_match_keyword_cmd (edit);
4172 break;
4173 case CK_Quit:
4174 dlg_stop (edit->widget.owner);
4175 break;
4176 case CK_EditNew:
4177 edit_new_cmd (edit);
4178 break;
4179 case CK_Help:
4180 edit_help_cmd (edit);
4181 break;
4182 case CK_Refresh:
4183 edit_refresh_cmd (edit);
4184 break;
4185 case CK_SaveSetup:
4186 save_setup_cmd ();
4187 break;
4188 case CK_About:
4189 edit_about ();
4190 break;
4191 case CK_LearnKeys:
4192 learn_keys ();
4193 break;
4194 case CK_Options:
4195 edit_options_dialog (edit);
4196 break;
4197 case CK_OptionsSaveMode:
4198 menu_save_mode_cmd ();
4199 break;
4200 case CK_Date:
4202 char s[BUF_MEDIUM];
4203 /* fool gcc to prevent a Y2K warning */
4204 char time_format[] = "_c";
4205 time_format[0] = '%';
4207 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4208 edit_print_string (edit, s);
4209 edit->force |= REDRAW_PAGE;
4210 break;
4212 break;
4213 case CK_Goto:
4214 edit_goto_cmd (edit);
4215 break;
4216 case CK_ParagraphFormat:
4217 format_paragraph (edit, 1);
4218 edit->force |= REDRAW_PAGE;
4219 break;
4220 case CK_MacroDelete:
4221 edit_delete_macro_cmd (edit);
4222 break;
4223 case CK_MatchBracket:
4224 edit_goto_matching_bracket (edit);
4225 break;
4226 case CK_UserMenu:
4227 user_menu (edit, NULL, -1);
4228 break;
4229 case CK_Sort:
4230 edit_sort_cmd (edit);
4231 break;
4232 case CK_ExternalCommand:
4233 edit_ext_cmd (edit);
4234 break;
4235 case CK_Mail:
4236 edit_mail_dialog (edit);
4237 break;
4238 case CK_Shell:
4239 view_other_cmd ();
4240 break;
4241 #ifdef HAVE_CHARSET
4242 case CK_SelectCodepage:
4243 edit_select_codepage_cmd (edit);
4244 break;
4245 #endif
4246 case CK_InsertLiteral:
4247 edit_insert_literal_cmd (edit);
4248 break;
4249 case CK_MacroStartStopRecord:
4250 edit_begin_end_macro_cmd (edit);
4251 break;
4252 case CK_RepeatStartStopRecord:
4253 edit_begin_end_repeat_cmd (edit);
4254 break;
4255 case CK_ExtendedKeyMap:
4256 edit->extmod = TRUE;
4257 break;
4258 default:
4259 break;
4262 /* CK_PipeBlock */
4263 if ((command / CK_PipeBlock (0)) == 1)
4264 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4266 /* keys which must set the col position, and the search vars */
4267 switch (command)
4269 case CK_Search:
4270 case CK_SearchContinue:
4271 case CK_Replace:
4272 case CK_ReplaceContinue:
4273 case CK_Complete:
4274 edit->prev_col = edit_get_col (edit);
4275 break;
4276 case CK_Up:
4277 case CK_MarkUp:
4278 case CK_MarkColumnUp:
4279 case CK_Down:
4280 case CK_MarkDown:
4281 case CK_MarkColumnDown:
4282 case CK_PageUp:
4283 case CK_MarkPageUp:
4284 case CK_MarkColumnPageUp:
4285 case CK_PageDown:
4286 case CK_MarkPageDown:
4287 case CK_MarkColumnPageDown:
4288 case CK_Top:
4289 case CK_MarkToFileBegin:
4290 case CK_Bottom:
4291 case CK_MarkToFileEnd:
4292 case CK_ParagraphUp:
4293 case CK_MarkParagraphUp:
4294 case CK_MarkColumnParagraphUp:
4295 case CK_ParagraphDown:
4296 case CK_MarkParagraphDown:
4297 case CK_MarkColumnParagraphDown:
4298 case CK_ScrollUp:
4299 case CK_MarkScrollUp:
4300 case CK_MarkColumnScrollUp:
4301 case CK_ScrollDown:
4302 case CK_MarkScrollDown:
4303 case CK_MarkColumnScrollDown:
4304 edit->search_start = edit->curs1;
4305 edit->found_len = 0;
4306 break;
4307 default:
4308 edit->found_len = 0;
4309 edit->prev_col = edit_get_col (edit);
4310 edit->search_start = edit->curs1;
4312 edit_find_bracket (edit);
4314 if (option_auto_para_formatting)
4316 switch (command)
4318 case CK_BackSpace:
4319 case CK_Delete:
4320 case CK_DeleteToWordBegin:
4321 case CK_DeleteToWordEnd:
4322 case CK_DeleteToHome:
4323 case CK_DeleteToEnd:
4324 format_paragraph (edit, 0);
4325 edit->force |= REDRAW_PAGE;
4330 /* --------------------------------------------------------------------------------------------- */
4332 void
4333 edit_stack_init (void)
4335 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4337 edit_history_moveto[edit_stack_iterator].filename = NULL;
4338 edit_history_moveto[edit_stack_iterator].line = -1;
4341 edit_stack_iterator = 0;
4344 /* --------------------------------------------------------------------------------------------- */
4346 void
4347 edit_stack_free (void)
4349 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4350 g_free (edit_history_moveto[edit_stack_iterator].filename);
4353 /* --------------------------------------------------------------------------------------------- */
4354 /** move i lines */
4356 void
4357 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4359 edit_move_updown (edit, i, do_scroll, TRUE);
4362 /* --------------------------------------------------------------------------------------------- */
4363 /** move i lines */
4365 void
4366 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4368 edit_move_updown (edit, i, do_scroll, FALSE);
4371 /* --------------------------------------------------------------------------------------------- */
4373 unsigned int
4374 edit_unlock_file (WEdit * edit)
4376 char *fullpath;
4377 unsigned int ret;
4379 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4380 ret = unlock_file (fullpath);
4381 g_free (fullpath);
4383 return ret;
4386 /* --------------------------------------------------------------------------------------------- */
4388 unsigned int
4389 edit_lock_file (WEdit * edit)
4391 char *fullpath;
4392 unsigned int ret;
4394 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4395 ret = lock_file (fullpath);
4396 g_free (fullpath);
4398 return ret;
4401 /* --------------------------------------------------------------------------------------------- */