Changed interface of function mc_open() for handle vfs_path_t object as parameter
[midnight-commander.git] / src / editor / edit.c
blob6fe33ce0975d9eabfb2e4ace266b320e9ebe87d7
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 char *write_name, const char *filename)
1971 int i;
1972 char *p, *writename;
1974 i = edit_find_filter (filename);
1975 if (i < 0)
1976 return NULL;
1978 writename = name_quote (write_name, 0);
1979 p = g_strdup_printf (all_filters[i].write, writename);
1980 g_free (writename);
1981 return p;
1984 /* --------------------------------------------------------------------------------------------- */
1986 long
1987 edit_write_stream (WEdit * edit, FILE * f)
1989 long i;
1991 if (edit->lb == LB_ASIS)
1993 for (i = 0; i < edit->last_byte; i++)
1994 if (fputc (edit_get_byte (edit, i), f) < 0)
1995 break;
1996 return i;
1999 /* change line breaks */
2000 for (i = 0; i < edit->last_byte; i++)
2002 unsigned char c = edit_get_byte (edit, i);
2004 if (!(c == '\n' || c == '\r'))
2006 /* not line break */
2007 if (fputc (c, f) < 0)
2008 return i;
2010 else
2011 { /* (c == '\n' || c == '\r') */
2012 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2014 switch (edit->lb)
2016 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2017 /* put one line break unconditionally */
2018 if (fputc ('\n', f) < 0)
2019 return i;
2021 i++; /* 2 chars are processed */
2023 if (c == '\r' && c1 == '\n')
2024 /* Windows line break; go to the next char */
2025 break;
2027 if (c == '\r' && c1 == '\r')
2029 /* two Macintosh line breaks; put second line break */
2030 if (fputc ('\n', f) < 0)
2031 return i;
2032 break;
2035 if (fputc (c1, f) < 0)
2036 return i;
2037 break;
2039 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2040 /* put one line break unconditionally */
2041 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2042 return i;
2044 if (c == '\r' && c1 == '\n')
2045 /* Windows line break; go to the next char */
2046 i++;
2047 break;
2049 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2050 /* put one line break unconditionally */
2051 if (fputc ('\r', f) < 0)
2052 return i;
2054 i++; /* 2 chars are processed */
2056 if (c == '\r' && c1 == '\n')
2057 /* Windows line break; go to the next char */
2058 break;
2060 if (c == '\n' && c1 == '\n')
2062 /* two Windows line breaks; put second line break */
2063 if (fputc ('\r', f) < 0)
2064 return i;
2065 break;
2068 if (fputc (c1, f) < 0)
2069 return i;
2070 break;
2071 case LB_ASIS: /* default without changes */
2072 break;
2077 return edit->last_byte;
2080 /* --------------------------------------------------------------------------------------------- */
2081 /** inserts a file at the cursor, returns count of inserted bytes on success */
2082 long
2083 edit_insert_file (WEdit * edit, const char *filename)
2085 char *p = NULL;
2086 long ins_len = 0;
2087 vfs_path_t *vpath;
2089 vpath = vfs_path_from_str (filename);
2090 p = edit_get_filter (filename);
2091 if (p != NULL)
2093 FILE *f;
2094 long current = edit->curs1;
2096 f = (FILE *) popen (p, "r");
2097 if (f != NULL)
2099 edit_insert_stream (edit, f);
2100 ins_len = edit->curs1 - current;
2101 edit_cursor_move (edit, -ins_len);
2102 if (pclose (f) > 0)
2104 char *errmsg;
2106 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2107 edit_error_dialog (_("Error"), errmsg);
2108 g_free (errmsg);
2109 ins_len = -1;
2110 goto ret;
2113 else
2115 char *errmsg;
2117 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2118 edit_error_dialog (_("Error"), errmsg);
2119 g_free (errmsg);
2120 ins_len = -1;
2121 goto ret;
2124 else
2126 int i, file, blocklen;
2127 long current = edit->curs1;
2128 int vertical_insertion = 0;
2129 char *buf;
2131 file = mc_open (vpath, O_RDONLY | O_BINARY);
2132 if (file == -1)
2134 ins_len = -1;
2135 goto ret;
2137 buf = g_malloc0 (TEMP_BUF_LEN);
2138 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2139 if (blocklen > 0)
2141 /* if contain signature VERTICAL_MAGIC then it vertical block */
2142 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2143 vertical_insertion = 1;
2144 else
2145 mc_lseek (file, 0, SEEK_SET);
2148 if (vertical_insertion)
2150 long mark1, mark2;
2151 int c1, c2;
2153 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2154 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2155 /* highlight inserted text then not persistent blocks */
2156 if (!option_persistent_selections)
2158 if (!edit->column_highlight)
2159 edit_push_undo_action (edit, COLUMN_OFF);
2160 edit->column_highlight = 1;
2163 else
2165 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2167 for (i = 0; i < blocklen; i++)
2168 edit_insert (edit, buf[i]);
2170 /* highlight inserted text then not persistent blocks */
2171 if (!option_persistent_selections && edit->modified)
2173 edit_set_markers (edit, edit->curs1, current, 0, 0);
2174 if (edit->column_highlight)
2175 edit_push_undo_action (edit, COLUMN_ON);
2176 edit->column_highlight = 0;
2180 edit->force |= REDRAW_PAGE;
2181 ins_len = edit->curs1 - current;
2182 edit_cursor_move (edit, -ins_len);
2183 g_free (buf);
2184 mc_close (file);
2185 if (blocklen != 0)
2186 ins_len = 0;
2189 ret:
2190 g_free (p);
2191 vfs_path_free (vpath);
2192 return ins_len;
2195 /* --------------------------------------------------------------------------------------------- */
2197 * Fill in the edit structure. Return NULL on failure. Pass edit as
2198 * NULL to allocate a new structure.
2200 * If line is 0, try to restore saved position. Otherwise put the
2201 * cursor on that line and show it in the middle of the screen.
2204 WEdit *
2205 edit_init (WEdit * edit, int y, int x, int lines, int cols, const char *filename, long line)
2207 gboolean to_free = FALSE;
2209 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2210 if (option_line_state)
2211 option_line_state_width = LINE_STATE_WIDTH;
2212 else
2213 option_line_state_width = 0;
2215 if (edit == NULL)
2217 #ifdef ENABLE_NLS
2219 * Expand option_whole_chars_search by national letters using
2220 * current locale
2223 static char option_whole_chars_search_buf[256];
2225 if (option_whole_chars_search_buf != option_whole_chars_search)
2227 size_t i;
2228 size_t len = str_term_width1 (option_whole_chars_search);
2230 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2232 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2234 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2236 option_whole_chars_search_buf[len++] = i;
2240 option_whole_chars_search_buf[len] = 0;
2241 option_whole_chars_search = option_whole_chars_search_buf;
2243 #endif /* ENABLE_NLS */
2244 edit = g_malloc0 (sizeof (WEdit));
2245 edit->search = NULL;
2246 to_free = TRUE;
2249 edit_purge_widget (edit);
2250 edit->widget.y = y;
2251 edit->widget.x = x;
2252 edit->widget.lines = lines;
2253 edit->widget.cols = cols;
2255 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2256 edit->stat1.st_uid = getuid ();
2257 edit->stat1.st_gid = getgid ();
2258 edit->stat1.st_mtime = 0;
2260 edit->over_col = 0;
2261 edit->bracket = -1;
2262 edit->force |= REDRAW_PAGE;
2263 edit_set_filename (edit, filename);
2265 edit->undo_stack_size = START_STACK_SIZE;
2266 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2267 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2269 edit->redo_stack_size = START_STACK_SIZE;
2270 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2271 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2273 edit->utf8 = 0;
2274 edit->converter = str_cnv_from_term;
2275 edit_set_codeset (edit);
2277 if (edit_load_file (edit))
2279 /* edit_load_file already gives an error message */
2280 if (to_free)
2281 g_free (edit);
2282 return NULL;
2285 edit->loading_done = 1;
2286 edit->modified = 0;
2287 edit->locked = 0;
2288 edit_load_syntax (edit, NULL, NULL);
2290 int color;
2291 edit_get_syntax_color (edit, -1, &color);
2294 /* load saved cursor position */
2295 if ((line == 0) && option_save_position)
2296 edit_load_position (edit);
2297 else
2299 if (line <= 0)
2300 line = 1;
2301 edit_move_display (edit, line - 1);
2302 edit_move_to_line (edit, line - 1);
2305 edit_load_macro_cmd (edit);
2306 return edit;
2309 /* --------------------------------------------------------------------------------------------- */
2310 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2313 edit_clean (WEdit * edit)
2315 int j = 0;
2317 if (!edit)
2318 return 0;
2320 /* a stale lock, remove it */
2321 if (edit->locked)
2322 edit->locked = edit_unlock_file (edit);
2324 /* save cursor position */
2325 if (option_save_position)
2326 edit_save_position (edit);
2327 else if (edit->serialized_bookmarks != NULL)
2328 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2330 /* File specified on the mcedit command line and never saved */
2331 if (edit->delete_file)
2332 unlink (edit->filename);
2334 edit_free_syntax_rules (edit);
2335 book_mark_flush (edit, -1);
2336 for (; j <= MAXBUFF; j++)
2338 g_free (edit->buffers1[j]);
2339 g_free (edit->buffers2[j]);
2342 g_free (edit->undo_stack);
2343 g_free (edit->redo_stack);
2344 g_free (edit->filename);
2345 g_free (edit->dir);
2346 mc_search_free (edit->search);
2347 edit->search = NULL;
2349 if (edit->converter != str_cnv_from_term)
2350 str_close_conv (edit->converter);
2352 edit_purge_widget (edit);
2354 return 1;
2357 /* --------------------------------------------------------------------------------------------- */
2358 /** returns 1 on success */
2361 edit_renew (WEdit * edit)
2363 int y = edit->widget.y;
2364 int x = edit->widget.x;
2365 int lines = edit->widget.lines;
2366 int columns = edit->widget.cols;
2368 edit_clean (edit);
2369 return (edit_init (edit, y, x, lines, columns, "", 0) != NULL);
2372 /* --------------------------------------------------------------------------------------------- */
2374 * Load a new file into the editor. If it fails, preserve the old file.
2375 * To do it, allocate a new widget, initialize it and, if the new file
2376 * was loaded, copy the data to the old widget.
2377 * Return 1 on success, 0 on failure.
2381 edit_reload (WEdit * edit, const char *filename)
2383 WEdit *e;
2384 int y = edit->widget.y;
2385 int x = edit->widget.x;
2386 int lines = edit->widget.lines;
2387 int columns = edit->widget.cols;
2389 e = g_malloc0 (sizeof (WEdit));
2390 e->widget = edit->widget;
2391 if (edit_init (e, y, x, lines, columns, filename, 0) == NULL)
2393 g_free (e);
2394 return 0;
2396 edit_clean (edit);
2397 memcpy (edit, e, sizeof (WEdit));
2398 g_free (e);
2399 return 1;
2402 /* --------------------------------------------------------------------------------------------- */
2404 * Load a new file into the editor and set line. If it fails, preserve the old file.
2405 * To do it, allocate a new widget, initialize it and, if the new file
2406 * was loaded, copy the data to the old widget.
2407 * Return 1 on success, 0 on failure.
2411 edit_reload_line (WEdit * edit, const char *filename, long line)
2413 WEdit *e;
2414 int y = edit->widget.y;
2415 int x = edit->widget.x;
2416 int lines = edit->widget.lines;
2417 int columns = edit->widget.cols;
2419 e = g_malloc0 (sizeof (WEdit));
2420 e->widget = edit->widget;
2421 if (edit_init (e, y, x, lines, columns, filename, line) == NULL)
2423 g_free (e);
2424 return 0;
2426 edit_clean (edit);
2427 memcpy (edit, e, sizeof (WEdit));
2428 g_free (e);
2429 return 1;
2432 /* --------------------------------------------------------------------------------------------- */
2434 void
2435 edit_set_codeset (WEdit * edit)
2437 #ifdef HAVE_CHARSET
2438 const char *cp_id;
2440 cp_id =
2441 get_codepage_id (mc_global.source_codepage >=
2442 0 ? mc_global.source_codepage : mc_global.display_codepage);
2444 if (cp_id != NULL)
2446 GIConv conv;
2447 conv = str_crt_conv_from (cp_id);
2448 if (conv != INVALID_CONV)
2450 if (edit->converter != str_cnv_from_term)
2451 str_close_conv (edit->converter);
2452 edit->converter = conv;
2456 if (cp_id != NULL)
2457 edit->utf8 = str_isutf8 (cp_id);
2458 #else
2459 (void) edit;
2460 #endif
2464 /* --------------------------------------------------------------------------------------------- */
2466 Recording stack for undo:
2467 The following is an implementation of a compressed stack. Identical
2468 pushes are recorded by a negative prefix indicating the number of times the
2469 same char was pushed. This saves space for repeated curs-left or curs-right
2470 delete etc.
2474 pushed: stored:
2478 b -3
2480 c --> -4
2486 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2487 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2488 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2489 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2490 position.
2492 The only way the cursor moves or the buffer is changed is through the routines:
2493 insert, backspace, insert_ahead, delete, and cursor_move.
2494 These record the reverse undo movements onto the stack each time they are
2495 called.
2497 Each key press results in a set of actions (insert; delete ...). So each time
2498 a key is pressed the current position of start_display is pushed as
2499 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2500 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2501 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2505 void
2506 edit_push_undo_action (WEdit * edit, long c, ...)
2508 unsigned long sp = edit->undo_stack_pointer;
2509 unsigned long spm1;
2510 long *t;
2512 /* first enlarge the stack if necessary */
2513 if (sp > edit->undo_stack_size - 10)
2514 { /* say */
2515 if (option_max_undo < 256)
2516 option_max_undo = 256;
2517 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2519 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2520 if (t)
2522 edit->undo_stack = t;
2523 edit->undo_stack_size <<= 1;
2524 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2528 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2529 if (edit->undo_stack_disable)
2531 edit_push_redo_action (edit, KEY_PRESS);
2532 edit_push_redo_action (edit, c);
2533 return;
2535 else if (edit->redo_stack_reset)
2537 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2540 if (edit->undo_stack_bottom != sp
2541 && spm1 != edit->undo_stack_bottom
2542 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2544 int d;
2545 if (edit->undo_stack[spm1] < 0)
2547 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2548 if (d == c)
2550 if (edit->undo_stack[spm1] > -1000000000)
2552 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2554 edit->undo_stack[spm1]--;
2556 return;
2560 else
2562 d = edit->undo_stack[spm1];
2563 if (d == c)
2565 if (c >= KEY_PRESS)
2566 return; /* --> no need to push multiple do-nothings */
2567 edit->undo_stack[sp] = -2;
2568 goto check_bottom;
2572 edit->undo_stack[sp] = c;
2574 check_bottom:
2575 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2577 /* if the sp wraps round and catches the undo_stack_bottom then erase
2578 * the first set of actions on the stack to make space - by moving
2579 * undo_stack_bottom forward one "key press" */
2580 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2581 if ((unsigned long) c == edit->undo_stack_bottom ||
2582 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2585 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2587 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2588 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2590 /*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: */
2591 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2592 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2594 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2598 void
2599 edit_push_redo_action (WEdit * edit, long c, ...)
2601 unsigned long sp = edit->redo_stack_pointer;
2602 unsigned long spm1;
2603 long *t;
2604 /* first enlarge the stack if necessary */
2605 if (sp > edit->redo_stack_size - 10)
2606 { /* say */
2607 if (option_max_undo < 256)
2608 option_max_undo = 256;
2609 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2611 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2612 if (t)
2614 edit->redo_stack = t;
2615 edit->redo_stack_size <<= 1;
2616 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2620 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2622 if (edit->redo_stack_bottom != sp
2623 && spm1 != edit->redo_stack_bottom
2624 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2626 int d;
2627 if (edit->redo_stack[spm1] < 0)
2629 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2630 if (d == c)
2632 if (edit->redo_stack[spm1] > -1000000000)
2634 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2635 edit->redo_stack[spm1]--;
2636 return;
2640 else
2642 d = edit->redo_stack[spm1];
2643 if (d == c)
2645 if (c >= KEY_PRESS)
2646 return; /* --> no need to push multiple do-nothings */
2647 edit->redo_stack[sp] = -2;
2648 goto redo_check_bottom;
2652 edit->redo_stack[sp] = c;
2654 redo_check_bottom:
2655 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2657 /* if the sp wraps round and catches the redo_stack_bottom then erase
2658 * the first set of actions on the stack to make space - by moving
2659 * redo_stack_bottom forward one "key press" */
2660 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2661 if ((unsigned long) c == edit->redo_stack_bottom ||
2662 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2665 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2667 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2668 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2671 * If a single key produced enough pushes to wrap all the way round then
2672 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2673 * The stack is then initialised:
2676 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2677 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2678 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2682 /* --------------------------------------------------------------------------------------------- */
2684 Basic low level single character buffer alterations and movements at the cursor.
2685 Returns char passed over, inserted or removed.
2688 void
2689 edit_insert (WEdit * edit, int c)
2691 /* check if file has grown to large */
2692 if (edit->last_byte >= SIZE_LIMIT)
2693 return;
2695 /* first we must update the position of the display window */
2696 if (edit->curs1 < edit->start_display)
2698 edit->start_display++;
2699 if (c == '\n')
2700 edit->start_line++;
2703 /* Mark file as modified, unless the file hasn't been fully loaded */
2704 if (edit->loading_done)
2706 edit_modification (edit);
2709 /* now we must update some info on the file and check if a redraw is required */
2710 if (c == '\n')
2712 if (edit->book_mark)
2713 book_mark_inc (edit, edit->curs_line);
2714 edit->curs_line++;
2715 edit->total_lines++;
2716 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2719 /* save the reverse command onto the undo stack */
2720 /* ordinary char and not space */
2721 if (c > 32)
2722 edit_push_undo_action (edit, BACKSPACE);
2723 else
2724 edit_push_undo_action (edit, BACKSPACE_BR);
2725 /* update markers */
2726 edit->mark1 += (edit->mark1 > edit->curs1);
2727 edit->mark2 += (edit->mark2 > edit->curs1);
2728 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2730 /* add a new buffer if we've reached the end of the last one */
2731 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2732 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2734 /* perform the insertion */
2735 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2736 = (unsigned char) c;
2738 /* update file length */
2739 edit->last_byte++;
2741 /* update cursor position */
2742 edit->curs1++;
2745 /* --------------------------------------------------------------------------------------------- */
2746 /** same as edit_insert and move left */
2748 void
2749 edit_insert_ahead (WEdit * edit, int c)
2751 if (edit->last_byte >= SIZE_LIMIT)
2752 return;
2754 if (edit->curs1 < edit->start_display)
2756 edit->start_display++;
2757 if (c == '\n')
2758 edit->start_line++;
2760 edit_modification (edit);
2761 if (c == '\n')
2763 if (edit->book_mark)
2764 book_mark_inc (edit, edit->curs_line);
2765 edit->total_lines++;
2766 edit->force |= REDRAW_AFTER_CURSOR;
2768 /* ordinary char and not space */
2769 if (c > 32)
2770 edit_push_undo_action (edit, DELCHAR);
2771 else
2772 edit_push_undo_action (edit, DELCHAR_BR);
2774 edit->mark1 += (edit->mark1 >= edit->curs1);
2775 edit->mark2 += (edit->mark2 >= edit->curs1);
2776 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2778 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2779 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2780 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2781 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2783 edit->last_byte++;
2784 edit->curs2++;
2788 /* --------------------------------------------------------------------------------------------- */
2791 edit_delete (WEdit * edit, const int byte_delete)
2793 int p = 0;
2794 int cw = 1;
2795 int i;
2797 if (!edit->curs2)
2798 return 0;
2800 cw = 1;
2801 /* if byte_delete = 1 then delete only one byte not multibyte char */
2802 if (edit->utf8 && byte_delete == 0)
2804 edit_get_utf (edit, edit->curs1, &cw);
2805 if (cw < 1)
2806 cw = 1;
2809 if (edit->mark2 != edit->mark1)
2810 edit_push_markers (edit);
2812 for (i = 1; i <= cw; i++)
2814 if (edit->mark1 > edit->curs1)
2816 edit->mark1--;
2817 edit->end_mark_curs--;
2819 if (edit->mark2 > edit->curs1)
2820 edit->mark2--;
2821 if (edit->last_get_rule > edit->curs1)
2822 edit->last_get_rule--;
2824 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2825 ((edit->curs2 -
2826 1) & M_EDIT_BUF_SIZE) - 1];
2828 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2830 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2831 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2833 edit->last_byte--;
2834 edit->curs2--;
2835 edit_push_undo_action (edit, p + 256);
2838 edit_modification (edit);
2839 if (p == '\n')
2841 if (edit->book_mark)
2842 book_mark_dec (edit, edit->curs_line);
2843 edit->total_lines--;
2844 edit->force |= REDRAW_AFTER_CURSOR;
2846 if (edit->curs1 < edit->start_display)
2848 edit->start_display--;
2849 if (p == '\n')
2850 edit->start_line--;
2853 return p;
2856 /* --------------------------------------------------------------------------------------------- */
2857 /** moves the cursor right or left: increment positive or negative respectively */
2859 void
2860 edit_cursor_move (WEdit * edit, long increment)
2862 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2863 int c;
2865 if (increment < 0)
2867 for (; increment < 0; increment++)
2869 if (!edit->curs1)
2870 return;
2872 edit_push_undo_action (edit, CURS_RIGHT);
2874 c = edit_get_byte (edit, edit->curs1 - 1);
2875 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2876 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2877 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2878 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2879 edit->curs2++;
2880 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2881 1) & M_EDIT_BUF_SIZE];
2882 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2884 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2885 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2887 edit->curs1--;
2888 if (c == '\n')
2890 edit->curs_line--;
2891 edit->force |= REDRAW_LINE_BELOW;
2896 else if (increment > 0)
2898 for (; increment > 0; increment--)
2900 if (!edit->curs2)
2901 return;
2903 edit_push_undo_action (edit, CURS_LEFT);
2905 c = edit_get_byte (edit, edit->curs1);
2906 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2907 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2908 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2909 edit->curs1++;
2910 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2911 ((edit->curs2 -
2912 1) & M_EDIT_BUF_SIZE) - 1];
2913 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2915 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2916 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2918 edit->curs2--;
2919 if (c == '\n')
2921 edit->curs_line++;
2922 edit->force |= REDRAW_LINE_ABOVE;
2928 /* These functions return positions relative to lines */
2930 /* --------------------------------------------------------------------------------------------- */
2931 /** returns index of last char on line + 1 */
2933 long
2934 edit_eol (WEdit * edit, long current)
2936 if (current >= edit->last_byte)
2937 return edit->last_byte;
2939 for (;; current++)
2940 if (edit_get_byte (edit, current) == '\n')
2941 break;
2942 return current;
2945 /* --------------------------------------------------------------------------------------------- */
2946 /** returns index of first char on line */
2948 long
2949 edit_bol (WEdit * edit, long current)
2951 if (current <= 0)
2952 return 0;
2954 for (;; current--)
2955 if (edit_get_byte (edit, current - 1) == '\n')
2956 break;
2957 return current;
2960 /* --------------------------------------------------------------------------------------------- */
2962 long
2963 edit_count_lines (WEdit * edit, long current, long upto)
2965 long lines = 0;
2966 if (upto > edit->last_byte)
2967 upto = edit->last_byte;
2968 if (current < 0)
2969 current = 0;
2970 while (current < upto)
2971 if (edit_get_byte (edit, current++) == '\n')
2972 lines++;
2973 return lines;
2976 /* --------------------------------------------------------------------------------------------- */
2977 /* If lines is zero this returns the count of lines from current to upto. */
2978 /* If upto is zero returns index of lines forward current. */
2980 long
2981 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2983 if (upto)
2985 return edit_count_lines (edit, current, upto);
2987 else
2989 long next;
2990 if (lines < 0)
2991 lines = 0;
2992 while (lines--)
2994 next = edit_eol (edit, current) + 1;
2995 if (next > edit->last_byte)
2996 break;
2997 else
2998 current = next;
3000 return current;
3004 /* --------------------------------------------------------------------------------------------- */
3005 /** Returns offset of 'lines' lines up from current */
3007 long
3008 edit_move_backward (WEdit * edit, long current, long lines)
3010 if (lines < 0)
3011 lines = 0;
3012 current = edit_bol (edit, current);
3013 while ((lines--) && current != 0)
3014 current = edit_bol (edit, current - 1);
3015 return current;
3018 /* --------------------------------------------------------------------------------------------- */
3019 /* If cols is zero this returns the count of columns from current to upto. */
3020 /* If upto is zero returns index of cols across from current. */
3022 long
3023 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3025 long p, q;
3026 int col;
3028 if (upto)
3030 q = upto;
3031 cols = -10;
3033 else
3034 q = edit->last_byte + 2;
3036 for (col = 0, p = current; p < q; p++)
3038 int c, orig_c;
3040 if (cols != -10)
3042 if (col == cols)
3043 return p;
3044 if (col > cols)
3045 return p - 1;
3048 orig_c = c = edit_get_byte (edit, p);
3050 #ifdef HAVE_CHARSET
3051 if (edit->utf8)
3053 int utf_ch;
3054 int cw = 1;
3056 utf_ch = edit_get_utf (edit, p, &cw);
3057 if (mc_global.utf8_display)
3059 if (cw > 1)
3060 col -= cw - 1;
3061 if (g_unichar_iswide (utf_ch))
3062 col++;
3064 else if (cw > 1 && g_unichar_isprint (utf_ch))
3065 col -= cw - 1;
3068 c = convert_to_display_c (c);
3069 #endif
3071 if (c == '\t')
3072 col += TAB_SIZE - col % TAB_SIZE;
3073 else if (c == '\n')
3075 if (upto)
3076 return col;
3077 else
3078 return p;
3080 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3081 /* '\r' is shown as ^M, so we must advance 2 characters */
3082 /* Caret notation for control characters */
3083 col += 2;
3084 else
3085 col++;
3087 return col;
3090 /* --------------------------------------------------------------------------------------------- */
3091 /** returns the current column position of the cursor */
3094 edit_get_col (WEdit * edit)
3096 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3099 /* --------------------------------------------------------------------------------------------- */
3100 /* Scrolling functions */
3101 /* --------------------------------------------------------------------------------------------- */
3103 void
3104 edit_update_curs_row (WEdit * edit)
3106 edit->curs_row = edit->curs_line - edit->start_line;
3109 /* --------------------------------------------------------------------------------------------- */
3111 void
3112 edit_update_curs_col (WEdit * edit)
3114 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3117 /* --------------------------------------------------------------------------------------------- */
3120 edit_get_curs_col (const WEdit * edit)
3122 return edit->curs_col;
3125 /* --------------------------------------------------------------------------------------------- */
3126 /** moves the display start position up by i lines */
3128 void
3129 edit_scroll_upward (WEdit * edit, unsigned long i)
3131 unsigned long lines_above = edit->start_line;
3132 if (i > lines_above)
3133 i = lines_above;
3134 if (i)
3136 edit->start_line -= i;
3137 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3138 edit->force |= REDRAW_PAGE;
3139 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3141 edit_update_curs_row (edit);
3145 /* --------------------------------------------------------------------------------------------- */
3146 /** returns 1 if could scroll, 0 otherwise */
3148 void
3149 edit_scroll_downward (WEdit * edit, int i)
3151 int lines_below;
3152 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3153 if (lines_below > 0)
3155 if (i > lines_below)
3156 i = lines_below;
3157 edit->start_line += i;
3158 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3159 edit->force |= REDRAW_PAGE;
3160 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3162 edit_update_curs_row (edit);
3165 /* --------------------------------------------------------------------------------------------- */
3167 void
3168 edit_scroll_right (WEdit * edit, int i)
3170 edit->force |= REDRAW_PAGE;
3171 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3172 edit->start_col -= i;
3175 /* --------------------------------------------------------------------------------------------- */
3177 void
3178 edit_scroll_left (WEdit * edit, int i)
3180 if (edit->start_col)
3182 edit->start_col += i;
3183 if (edit->start_col > 0)
3184 edit->start_col = 0;
3185 edit->force |= REDRAW_PAGE;
3186 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3190 /* --------------------------------------------------------------------------------------------- */
3191 /* high level cursor movement commands */
3192 /* --------------------------------------------------------------------------------------------- */
3194 void
3195 edit_move_to_prev_col (WEdit * edit, long p)
3197 int prev = edit->prev_col;
3198 int over = edit->over_col;
3199 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3201 if (option_cursor_beyond_eol)
3203 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3204 edit_eol (edit, edit->curs1));
3206 if (line_len < prev + edit->over_col)
3208 edit->over_col = prev + over - line_len;
3209 edit->prev_col = line_len;
3210 edit->curs_col = line_len;
3212 else
3214 edit->curs_col = prev + over;
3215 edit->prev_col = edit->curs_col;
3216 edit->over_col = 0;
3219 else
3221 edit->over_col = 0;
3222 if (is_in_indent (edit) && option_fake_half_tabs)
3224 edit_update_curs_col (edit);
3225 if (space_width)
3226 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3228 int q = edit->curs_col;
3229 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3230 p = edit_bol (edit, edit->curs1);
3231 edit_cursor_move (edit,
3232 edit_move_forward3 (edit, p, edit->curs_col,
3233 0) - edit->curs1);
3234 if (!left_of_four_spaces (edit))
3235 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3241 /* --------------------------------------------------------------------------------------------- */
3244 line_is_blank (WEdit * edit, long line)
3246 return is_blank (edit, edit_find_line (edit, line));
3249 /* --------------------------------------------------------------------------------------------- */
3250 /** move cursor to line 'line' */
3252 void
3253 edit_move_to_line (WEdit * e, long line)
3255 if (line < e->curs_line)
3256 edit_move_up (e, e->curs_line - line, 0);
3257 else
3258 edit_move_down (e, line - e->curs_line, 0);
3259 edit_scroll_screen_over_cursor (e);
3262 /* --------------------------------------------------------------------------------------------- */
3263 /** scroll window so that first visible line is 'line' */
3265 void
3266 edit_move_display (WEdit * e, long line)
3268 if (line < e->start_line)
3269 edit_scroll_upward (e, e->start_line - line);
3270 else
3271 edit_scroll_downward (e, line - e->start_line);
3274 /* --------------------------------------------------------------------------------------------- */
3275 /** save markers onto undo stack */
3277 void
3278 edit_push_markers (WEdit * edit)
3280 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3281 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3282 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3285 /* --------------------------------------------------------------------------------------------- */
3287 void
3288 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3290 edit->mark1 = m1;
3291 edit->mark2 = m2;
3292 edit->column1 = c1;
3293 edit->column2 = c2;
3297 /* --------------------------------------------------------------------------------------------- */
3298 /** highlight marker toggle */
3300 void
3301 edit_mark_cmd (WEdit * edit, int unmark)
3303 edit_push_markers (edit);
3304 if (unmark)
3306 edit_set_markers (edit, 0, 0, 0, 0);
3307 edit->force |= REDRAW_PAGE;
3309 else
3311 if (edit->mark2 >= 0)
3313 edit->end_mark_curs = -1;
3314 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3315 edit->curs_col + edit->over_col);
3316 edit->force |= REDRAW_PAGE;
3318 else
3320 edit->end_mark_curs = edit->curs1;
3321 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3322 edit->curs_col + edit->over_col);
3327 /* --------------------------------------------------------------------------------------------- */
3328 /** highlight the word under cursor */
3330 void
3331 edit_mark_current_word_cmd (WEdit * edit)
3333 long pos;
3335 for (pos = edit->curs1; pos != 0; pos--)
3337 int c1, c2;
3339 c1 = edit_get_byte (edit, pos);
3340 c2 = edit_get_byte (edit, pos - 1);
3341 if (!isspace (c1) && isspace (c2))
3342 break;
3343 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3344 break;
3346 edit->mark1 = pos;
3348 for (; pos < edit->last_byte; pos++)
3350 int c1, c2;
3352 c1 = edit_get_byte (edit, pos);
3353 c2 = edit_get_byte (edit, pos + 1);
3354 if (!isspace (c1) && isspace (c2))
3355 break;
3356 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3357 break;
3359 edit->mark2 = min (pos + 1, edit->last_byte);
3361 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3364 /* --------------------------------------------------------------------------------------------- */
3366 void
3367 edit_mark_current_line_cmd (WEdit * edit)
3369 long pos = edit->curs1;
3371 edit->mark1 = edit_bol (edit, pos);
3372 edit->mark2 = edit_eol (edit, pos);
3374 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3377 /* --------------------------------------------------------------------------------------------- */
3379 void
3380 edit_delete_line (WEdit * edit)
3383 * Delete right part of the line.
3384 * Note that edit_get_byte() returns '\n' when byte position is
3385 * beyond EOF.
3387 while (edit_get_byte (edit, edit->curs1) != '\n')
3389 (void) edit_delete (edit, 1);
3393 * Delete '\n' char.
3394 * Note that edit_delete() will not corrupt anything if called while
3395 * cursor position is EOF.
3397 (void) edit_delete (edit, 1);
3400 * Delete left part of the line.
3401 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3403 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3405 (void) edit_backspace (edit, 1);
3409 /* --------------------------------------------------------------------------------------------- */
3412 edit_indent_width (WEdit * edit, long p)
3414 long q = p;
3415 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3416 q++;
3417 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3420 /* --------------------------------------------------------------------------------------------- */
3422 void
3423 edit_insert_indent (WEdit * edit, int indent)
3425 if (!option_fill_tabs_with_spaces)
3427 while (indent >= TAB_SIZE)
3429 edit_insert (edit, '\t');
3430 indent -= TAB_SIZE;
3433 while (indent-- > 0)
3434 edit_insert (edit, ' ');
3437 /* --------------------------------------------------------------------------------------------- */
3439 void
3440 edit_push_key_press (WEdit * edit)
3442 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3443 if (edit->mark2 == -1)
3445 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3446 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3450 /* --------------------------------------------------------------------------------------------- */
3452 void
3453 edit_find_bracket (WEdit * edit)
3455 edit->bracket = edit_get_bracket (edit, 1, 10000);
3456 if (last_bracket != edit->bracket)
3457 edit->force |= REDRAW_PAGE;
3458 last_bracket = edit->bracket;
3461 /* --------------------------------------------------------------------------------------------- */
3463 * This executes a command as though the user initiated it through a key
3464 * press. Callback with WIDGET_KEY as a message calls this after
3465 * translating the key press. This function can be used to pass any
3466 * command to the editor. Note that the screen wouldn't update
3467 * automatically. Either of command or char_for_insertion must be
3468 * passed as -1. Commands are executed, and char_for_insertion is
3469 * inserted at the cursor.
3472 void
3473 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3475 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3476 || (macro_index < 0
3477 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3479 macro_index = 0;
3480 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3481 return;
3483 if (macro_index != -1)
3485 edit->force |= REDRAW_COMPLETELY;
3486 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3488 edit_store_macro_cmd (edit);
3489 macro_index = -1;
3490 return;
3492 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3494 edit_repeat_macro_cmd (edit);
3495 macro_index = -1;
3496 return;
3500 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3502 record_macro_buf[macro_index].action = command;
3503 record_macro_buf[macro_index++].ch = char_for_insertion;
3505 /* record the beginning of a set of editing actions initiated by a key press */
3506 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3507 edit_push_key_press (edit);
3509 edit_execute_cmd (edit, command, char_for_insertion);
3510 if (edit->column_highlight)
3511 edit->force |= REDRAW_PAGE;
3514 /* --------------------------------------------------------------------------------------------- */
3516 This executes a command at a lower level than macro recording.
3517 It also does not push a key_press onto the undo stack. This means
3518 that if it is called many times, a single undo command will undo
3519 all of them. It also does not check for the Undo command.
3521 void
3522 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3524 edit->force |= REDRAW_LINE;
3526 /* The next key press will unhighlight the found string, so update
3527 * the whole page */
3528 if (edit->found_len || edit->column_highlight)
3529 edit->force |= REDRAW_PAGE;
3531 switch (command)
3533 /* a mark command with shift-arrow */
3534 case CK_MarkLeft:
3535 case CK_MarkRight:
3536 case CK_MarkToWordBegin:
3537 case CK_MarkToWordEnd:
3538 case CK_MarkToHome:
3539 case CK_MarkToEnd:
3540 case CK_MarkUp:
3541 case CK_MarkDown:
3542 case CK_MarkPageUp:
3543 case CK_MarkPageDown:
3544 case CK_MarkToFileBegin:
3545 case CK_MarkToFileEnd:
3546 case CK_MarkToPageBegin:
3547 case CK_MarkToPageEnd:
3548 case CK_MarkScrollUp:
3549 case CK_MarkScrollDown:
3550 case CK_MarkParagraphUp:
3551 case CK_MarkParagraphDown:
3552 /* a mark command with alt-arrow */
3553 case CK_MarkColumnPageUp:
3554 case CK_MarkColumnPageDown:
3555 case CK_MarkColumnLeft:
3556 case CK_MarkColumnRight:
3557 case CK_MarkColumnUp:
3558 case CK_MarkColumnDown:
3559 case CK_MarkColumnScrollUp:
3560 case CK_MarkColumnScrollDown:
3561 case CK_MarkColumnParagraphUp:
3562 case CK_MarkColumnParagraphDown:
3563 edit->column_highlight = 0;
3564 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3566 edit_mark_cmd (edit, 1); /* clear */
3567 edit_mark_cmd (edit, 0); /* marking on */
3569 edit->highlight = 1;
3570 break;
3572 /* any other command */
3573 default:
3574 if (edit->highlight)
3575 edit_mark_cmd (edit, 0); /* clear */
3576 edit->highlight = 0;
3579 /* first check for undo */
3580 if (command == CK_Undo)
3582 edit->redo_stack_reset = 0;
3583 edit_group_undo (edit);
3584 edit->found_len = 0;
3585 edit->prev_col = edit_get_col (edit);
3586 edit->search_start = edit->curs1;
3587 return;
3589 /* check for redo */
3590 if (command == CK_Redo)
3592 edit->redo_stack_reset = 0;
3593 edit_do_redo (edit);
3594 edit->found_len = 0;
3595 edit->prev_col = edit_get_col (edit);
3596 edit->search_start = edit->curs1;
3597 return;
3600 edit->redo_stack_reset = 1;
3602 /* An ordinary key press */
3603 if (char_for_insertion >= 0)
3605 /* if non persistent selection and text selected */
3606 if (!option_persistent_selections)
3608 if (edit->mark1 != edit->mark2)
3609 edit_block_delete_cmd (edit);
3611 if (edit->overwrite)
3613 /* remove char only one time, after input first byte, multibyte chars */
3614 if ((!mc_global.utf8_display || edit->charpoint == 0)
3615 && edit_get_byte (edit, edit->curs1) != '\n')
3616 edit_delete (edit, 0);
3618 if (option_cursor_beyond_eol && edit->over_col > 0)
3619 edit_insert_over (edit);
3620 #ifdef HAVE_CHARSET
3621 if (char_for_insertion > 255 && !mc_global.utf8_display)
3623 unsigned char str[6 + 1];
3624 size_t i = 0;
3625 int res;
3627 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3628 if (res == 0)
3630 str[0] = '.';
3631 str[1] = '\0';
3633 else
3635 str[res] = '\0';
3637 while (str[i] != 0 && i <= 6)
3639 char_for_insertion = str[i];
3640 edit_insert (edit, char_for_insertion);
3641 i++;
3644 else
3645 #endif
3646 edit_insert (edit, char_for_insertion);
3648 if (option_auto_para_formatting)
3650 format_paragraph (edit, 0);
3651 edit->force |= REDRAW_PAGE;
3653 else
3654 check_and_wrap_line (edit);
3655 edit->found_len = 0;
3656 edit->prev_col = edit_get_col (edit);
3657 edit->search_start = edit->curs1;
3658 edit_find_bracket (edit);
3659 return;
3662 switch (command)
3664 case CK_TopOnScreen:
3665 case CK_BottomOnScreen:
3666 case CK_Top:
3667 case CK_Bottom:
3668 case CK_PageUp:
3669 case CK_PageDown:
3670 case CK_Home:
3671 case CK_End:
3672 case CK_Up:
3673 case CK_Down:
3674 case CK_Left:
3675 case CK_Right:
3676 case CK_WordLeft:
3677 case CK_WordRight:
3678 if (edit->mark2 >= 0)
3680 if (!option_persistent_selections)
3682 if (edit->column_highlight)
3683 edit_push_undo_action (edit, COLUMN_ON);
3684 edit->column_highlight = 0;
3685 edit_mark_cmd (edit, 1);
3690 switch (command)
3692 case CK_TopOnScreen:
3693 case CK_BottomOnScreen:
3694 case CK_MarkToPageBegin:
3695 case CK_MarkToPageEnd:
3696 case CK_Up:
3697 case CK_Down:
3698 case CK_WordLeft:
3699 case CK_WordRight:
3700 case CK_MarkToWordBegin:
3701 case CK_MarkToWordEnd:
3702 case CK_MarkUp:
3703 case CK_MarkDown:
3704 case CK_MarkColumnUp:
3705 case CK_MarkColumnDown:
3706 if (edit->mark2 == -1)
3707 break; /*marking is following the cursor: may need to highlight a whole line */
3708 case CK_Left:
3709 case CK_Right:
3710 case CK_MarkLeft:
3711 case CK_MarkRight:
3712 edit->force |= REDRAW_CHAR_ONLY;
3715 /* basic cursor key commands */
3716 switch (command)
3718 case CK_BackSpace:
3719 /* if non persistent selection and text selected */
3720 if (!option_persistent_selections)
3722 if (edit->mark1 != edit->mark2)
3724 edit_block_delete_cmd (edit);
3725 break;
3728 if (option_cursor_beyond_eol && edit->over_col > 0)
3730 edit->over_col--;
3731 break;
3733 if (option_backspace_through_tabs && is_in_indent (edit))
3735 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3736 edit_backspace (edit, 1);
3737 break;
3739 else
3741 if (option_fake_half_tabs)
3743 int i;
3744 if (is_in_indent (edit) && right_of_four_spaces (edit))
3746 for (i = 0; i < HALF_TAB_SIZE; i++)
3747 edit_backspace (edit, 1);
3748 break;
3752 edit_backspace (edit, 0);
3753 break;
3754 case CK_Delete:
3755 /* if non persistent selection and text selected */
3756 if (!option_persistent_selections)
3758 if (edit->mark1 != edit->mark2)
3760 edit_block_delete_cmd (edit);
3761 break;
3765 if (option_cursor_beyond_eol && edit->over_col > 0)
3766 edit_insert_over (edit);
3768 if (option_fake_half_tabs)
3770 int i;
3771 if (is_in_indent (edit) && left_of_four_spaces (edit))
3773 for (i = 1; i <= HALF_TAB_SIZE; i++)
3774 edit_delete (edit, 1);
3775 break;
3778 edit_delete (edit, 0);
3779 break;
3780 case CK_DeleteToWordBegin:
3781 edit->over_col = 0;
3782 edit_left_delete_word (edit);
3783 break;
3784 case CK_DeleteToWordEnd:
3785 if (option_cursor_beyond_eol && edit->over_col > 0)
3786 edit_insert_over (edit);
3788 edit_right_delete_word (edit);
3789 break;
3790 case CK_DeleteLine:
3791 edit_delete_line (edit);
3792 break;
3793 case CK_DeleteToHome:
3794 edit_delete_to_line_begin (edit);
3795 break;
3796 case CK_DeleteToEnd:
3797 edit_delete_to_line_end (edit);
3798 break;
3799 case CK_Enter:
3800 edit->over_col = 0;
3801 if (option_auto_para_formatting)
3803 edit_double_newline (edit);
3804 if (option_return_does_auto_indent)
3805 edit_auto_indent (edit);
3806 format_paragraph (edit, 0);
3808 else
3810 edit_insert (edit, '\n');
3811 if (option_return_does_auto_indent)
3813 edit_auto_indent (edit);
3816 break;
3817 case CK_Return:
3818 edit_insert (edit, '\n');
3819 break;
3821 case CK_MarkColumnPageUp:
3822 edit->column_highlight = 1;
3823 case CK_PageUp:
3824 case CK_MarkPageUp:
3825 edit_move_up (edit, edit->widget.lines - 1, 1);
3826 break;
3827 case CK_MarkColumnPageDown:
3828 edit->column_highlight = 1;
3829 case CK_PageDown:
3830 case CK_MarkPageDown:
3831 edit_move_down (edit, edit->widget.lines - 1, 1);
3832 break;
3833 case CK_MarkColumnLeft:
3834 edit->column_highlight = 1;
3835 case CK_Left:
3836 case CK_MarkLeft:
3837 if (option_fake_half_tabs)
3839 if (is_in_indent (edit) && right_of_four_spaces (edit))
3841 if (option_cursor_beyond_eol && edit->over_col > 0)
3842 edit->over_col--;
3843 else
3844 edit_cursor_move (edit, -HALF_TAB_SIZE);
3845 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3846 break;
3849 edit_left_char_move_cmd (edit);
3850 break;
3851 case CK_MarkColumnRight:
3852 edit->column_highlight = 1;
3853 case CK_Right:
3854 case CK_MarkRight:
3855 if (option_fake_half_tabs)
3857 if (is_in_indent (edit) && left_of_four_spaces (edit))
3859 edit_cursor_move (edit, HALF_TAB_SIZE);
3860 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3861 break;
3864 edit_right_char_move_cmd (edit);
3865 break;
3866 case CK_TopOnScreen:
3867 case CK_MarkToPageBegin:
3868 edit_begin_page (edit);
3869 break;
3870 case CK_BottomOnScreen:
3871 case CK_MarkToPageEnd:
3872 edit_end_page (edit);
3873 break;
3874 case CK_WordLeft:
3875 case CK_MarkToWordBegin:
3876 edit->over_col = 0;
3877 edit_left_word_move_cmd (edit);
3878 break;
3879 case CK_WordRight:
3880 case CK_MarkToWordEnd:
3881 edit->over_col = 0;
3882 edit_right_word_move_cmd (edit);
3883 break;
3884 case CK_MarkColumnUp:
3885 edit->column_highlight = 1;
3886 case CK_Up:
3887 case CK_MarkUp:
3888 edit_move_up (edit, 1, 0);
3889 break;
3890 case CK_MarkColumnDown:
3891 edit->column_highlight = 1;
3892 case CK_Down:
3893 case CK_MarkDown:
3894 edit_move_down (edit, 1, 0);
3895 break;
3896 case CK_MarkColumnParagraphUp:
3897 edit->column_highlight = 1;
3898 case CK_ParagraphUp:
3899 case CK_MarkParagraphUp:
3900 edit_move_up_paragraph (edit, 0);
3901 break;
3902 case CK_MarkColumnParagraphDown:
3903 edit->column_highlight = 1;
3904 case CK_ParagraphDown:
3905 case CK_MarkParagraphDown:
3906 edit_move_down_paragraph (edit, 0);
3907 break;
3908 case CK_MarkColumnScrollUp:
3909 edit->column_highlight = 1;
3910 case CK_ScrollUp:
3911 case CK_MarkScrollUp:
3912 edit_move_up (edit, 1, 1);
3913 break;
3914 case CK_MarkColumnScrollDown:
3915 edit->column_highlight = 1;
3916 case CK_ScrollDown:
3917 case CK_MarkScrollDown:
3918 edit_move_down (edit, 1, 1);
3919 break;
3920 case CK_Home:
3921 case CK_MarkToHome:
3922 edit_cursor_to_bol (edit);
3923 break;
3924 case CK_End:
3925 case CK_MarkToEnd:
3926 edit_cursor_to_eol (edit);
3927 break;
3928 case CK_Tab:
3929 /* if text marked shift block */
3930 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3932 if (edit->mark2 < 0)
3933 edit_mark_cmd (edit, 0);
3934 edit_move_block_to_right (edit);
3936 else
3938 if (option_cursor_beyond_eol)
3939 edit_insert_over (edit);
3940 edit_tab_cmd (edit);
3941 if (option_auto_para_formatting)
3943 format_paragraph (edit, 0);
3944 edit->force |= REDRAW_PAGE;
3946 else
3948 check_and_wrap_line (edit);
3951 break;
3953 case CK_InsertOverwrite:
3954 edit->overwrite = !edit->overwrite;
3955 break;
3957 case CK_Mark:
3958 if (edit->mark2 >= 0)
3960 if (edit->column_highlight)
3961 edit_push_undo_action (edit, COLUMN_ON);
3962 edit->column_highlight = 0;
3964 edit_mark_cmd (edit, 0);
3965 break;
3966 case CK_MarkColumn:
3967 if (!edit->column_highlight)
3968 edit_push_undo_action (edit, COLUMN_OFF);
3969 edit->column_highlight = 1;
3970 edit_mark_cmd (edit, 0);
3971 break;
3972 case CK_MarkAll:
3973 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3974 edit->force |= REDRAW_PAGE;
3975 break;
3976 case CK_Unmark:
3977 if (edit->column_highlight)
3978 edit_push_undo_action (edit, COLUMN_ON);
3979 edit->column_highlight = 0;
3980 edit_mark_cmd (edit, 1);
3981 break;
3982 case CK_MarkWord:
3983 if (edit->column_highlight)
3984 edit_push_undo_action (edit, COLUMN_ON);
3985 edit->column_highlight = 0;
3986 edit_mark_current_word_cmd (edit);
3987 break;
3988 case CK_MarkLine:
3989 if (edit->column_highlight)
3990 edit_push_undo_action (edit, COLUMN_ON);
3991 edit->column_highlight = 0;
3992 edit_mark_current_line_cmd (edit);
3993 break;
3995 case CK_ShowNumbers:
3996 option_line_state = !option_line_state;
3997 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
3998 edit->force |= REDRAW_PAGE;
3999 break;
4001 case CK_ShowMargin:
4002 show_right_margin = !show_right_margin;
4003 edit->force |= REDRAW_PAGE;
4004 break;
4006 case CK_Bookmark:
4007 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4008 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4009 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4010 else
4011 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4012 break;
4013 case CK_BookmarkFlush:
4014 book_mark_flush (edit, BOOK_MARK_COLOR);
4015 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4016 edit->force |= REDRAW_PAGE;
4017 break;
4018 case CK_BookmarkNext:
4019 if (edit->book_mark)
4021 struct _book_mark *p;
4022 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4023 if (p->next)
4025 p = p->next;
4026 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4027 edit_move_display (edit, p->line - edit->widget.lines / 2);
4028 edit_move_to_line (edit, p->line);
4031 break;
4032 case CK_BookmarkPrev:
4033 if (edit->book_mark)
4035 struct _book_mark *p;
4036 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4037 while (p->line == edit->curs_line)
4038 if (p->prev)
4039 p = p->prev;
4040 if (p->line >= 0)
4042 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4043 edit_move_display (edit, p->line - edit->widget.lines / 2);
4044 edit_move_to_line (edit, p->line);
4047 break;
4049 case CK_Top:
4050 case CK_MarkToFileBegin:
4051 edit_move_to_top (edit);
4052 break;
4053 case CK_Bottom:
4054 case CK_MarkToFileEnd:
4055 edit_move_to_bottom (edit);
4056 break;
4058 case CK_Copy:
4059 if (option_cursor_beyond_eol && edit->over_col > 0)
4060 edit_insert_over (edit);
4061 edit_block_copy_cmd (edit);
4062 break;
4063 case CK_Remove:
4064 edit_block_delete_cmd (edit);
4065 break;
4066 case CK_Move:
4067 edit_block_move_cmd (edit);
4068 break;
4070 case CK_BlockShiftLeft:
4071 if (edit->mark1 != edit->mark2)
4072 edit_move_block_to_left (edit);
4073 break;
4074 case CK_BlockShiftRight:
4075 if (edit->mark1 != edit->mark2)
4076 edit_move_block_to_right (edit);
4077 break;
4078 case CK_Store:
4079 edit_copy_to_X_buf_cmd (edit);
4080 break;
4081 case CK_Cut:
4082 edit_cut_to_X_buf_cmd (edit);
4083 break;
4084 case CK_Paste:
4085 /* if non persistent selection and text selected */
4086 if (!option_persistent_selections)
4088 if (edit->mark1 != edit->mark2)
4089 edit_block_delete_cmd (edit);
4091 if (option_cursor_beyond_eol && edit->over_col > 0)
4092 edit_insert_over (edit);
4093 edit_paste_from_X_buf_cmd (edit);
4094 break;
4095 case CK_History:
4096 edit_paste_from_history (edit);
4097 break;
4099 case CK_SaveAs:
4100 edit_save_as_cmd (edit);
4101 break;
4102 case CK_Save:
4103 edit_save_confirm_cmd (edit);
4104 break;
4105 case CK_EditFile:
4106 edit_load_cmd (edit, EDIT_FILE_COMMON);
4107 break;
4108 case CK_BlockSave:
4109 edit_save_block_cmd (edit);
4110 break;
4111 case CK_InsertFile:
4112 edit_insert_file_cmd (edit);
4113 break;
4115 case CK_FilePrev:
4116 edit_load_back_cmd (edit);
4117 break;
4118 case CK_FileNext:
4119 edit_load_forward_cmd (edit);
4120 break;
4122 case CK_EditSyntaxFile:
4123 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4124 break;
4125 case CK_SyntaxChoose:
4126 edit_syntax_dialog (edit);
4127 break;
4129 case CK_EditUserMenu:
4130 edit_load_cmd (edit, EDIT_FILE_MENU);
4131 break;
4133 case CK_SyntaxOnOff:
4134 option_syntax_highlighting ^= 1;
4135 if (option_syntax_highlighting == 1)
4136 edit_load_syntax (edit, NULL, edit->syntax_type);
4137 edit->force |= REDRAW_PAGE;
4138 break;
4140 case CK_ShowTabTws:
4141 enable_show_tabs_tws ^= 1;
4142 edit->force |= REDRAW_PAGE;
4143 break;
4145 case CK_Search:
4146 edit_search_cmd (edit, FALSE);
4147 break;
4148 case CK_SearchContinue:
4149 edit_search_cmd (edit, TRUE);
4150 break;
4151 case CK_Replace:
4152 edit_replace_cmd (edit, 0);
4153 break;
4154 case CK_ReplaceContinue:
4155 edit_replace_cmd (edit, 1);
4156 break;
4157 case CK_Complete:
4158 /* if text marked shift block */
4159 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4161 edit_move_block_to_left (edit);
4163 else
4165 edit_complete_word_cmd (edit);
4167 break;
4168 case CK_Find:
4169 edit_get_match_keyword_cmd (edit);
4170 break;
4171 case CK_Quit:
4172 dlg_stop (edit->widget.owner);
4173 break;
4174 case CK_EditNew:
4175 edit_new_cmd (edit);
4176 break;
4177 case CK_Help:
4178 edit_help_cmd (edit);
4179 break;
4180 case CK_Refresh:
4181 edit_refresh_cmd (edit);
4182 break;
4183 case CK_SaveSetup:
4184 save_setup_cmd ();
4185 break;
4186 case CK_About:
4187 edit_about ();
4188 break;
4189 case CK_LearnKeys:
4190 learn_keys ();
4191 break;
4192 case CK_Options:
4193 edit_options_dialog (edit);
4194 break;
4195 case CK_OptionsSaveMode:
4196 menu_save_mode_cmd ();
4197 break;
4198 case CK_Date:
4200 char s[BUF_MEDIUM];
4201 /* fool gcc to prevent a Y2K warning */
4202 char time_format[] = "_c";
4203 time_format[0] = '%';
4205 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4206 edit_print_string (edit, s);
4207 edit->force |= REDRAW_PAGE;
4208 break;
4210 break;
4211 case CK_Goto:
4212 edit_goto_cmd (edit);
4213 break;
4214 case CK_ParagraphFormat:
4215 format_paragraph (edit, 1);
4216 edit->force |= REDRAW_PAGE;
4217 break;
4218 case CK_MacroDelete:
4219 edit_delete_macro_cmd (edit);
4220 break;
4221 case CK_MatchBracket:
4222 edit_goto_matching_bracket (edit);
4223 break;
4224 case CK_UserMenu:
4225 user_menu (edit, NULL, -1);
4226 break;
4227 case CK_Sort:
4228 edit_sort_cmd (edit);
4229 break;
4230 case CK_ExternalCommand:
4231 edit_ext_cmd (edit);
4232 break;
4233 case CK_Mail:
4234 edit_mail_dialog (edit);
4235 break;
4236 case CK_Shell:
4237 view_other_cmd ();
4238 break;
4239 #ifdef HAVE_CHARSET
4240 case CK_SelectCodepage:
4241 edit_select_codepage_cmd (edit);
4242 break;
4243 #endif
4244 case CK_InsertLiteral:
4245 edit_insert_literal_cmd (edit);
4246 break;
4247 case CK_MacroStartStopRecord:
4248 edit_begin_end_macro_cmd (edit);
4249 break;
4250 case CK_RepeatStartStopRecord:
4251 edit_begin_end_repeat_cmd (edit);
4252 break;
4253 case CK_ExtendedKeyMap:
4254 edit->extmod = TRUE;
4255 break;
4256 default:
4257 break;
4260 /* CK_PipeBlock */
4261 if ((command / CK_PipeBlock (0)) == 1)
4262 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4264 /* keys which must set the col position, and the search vars */
4265 switch (command)
4267 case CK_Search:
4268 case CK_SearchContinue:
4269 case CK_Replace:
4270 case CK_ReplaceContinue:
4271 case CK_Complete:
4272 edit->prev_col = edit_get_col (edit);
4273 break;
4274 case CK_Up:
4275 case CK_MarkUp:
4276 case CK_MarkColumnUp:
4277 case CK_Down:
4278 case CK_MarkDown:
4279 case CK_MarkColumnDown:
4280 case CK_PageUp:
4281 case CK_MarkPageUp:
4282 case CK_MarkColumnPageUp:
4283 case CK_PageDown:
4284 case CK_MarkPageDown:
4285 case CK_MarkColumnPageDown:
4286 case CK_Top:
4287 case CK_MarkToFileBegin:
4288 case CK_Bottom:
4289 case CK_MarkToFileEnd:
4290 case CK_ParagraphUp:
4291 case CK_MarkParagraphUp:
4292 case CK_MarkColumnParagraphUp:
4293 case CK_ParagraphDown:
4294 case CK_MarkParagraphDown:
4295 case CK_MarkColumnParagraphDown:
4296 case CK_ScrollUp:
4297 case CK_MarkScrollUp:
4298 case CK_MarkColumnScrollUp:
4299 case CK_ScrollDown:
4300 case CK_MarkScrollDown:
4301 case CK_MarkColumnScrollDown:
4302 edit->search_start = edit->curs1;
4303 edit->found_len = 0;
4304 break;
4305 default:
4306 edit->found_len = 0;
4307 edit->prev_col = edit_get_col (edit);
4308 edit->search_start = edit->curs1;
4310 edit_find_bracket (edit);
4312 if (option_auto_para_formatting)
4314 switch (command)
4316 case CK_BackSpace:
4317 case CK_Delete:
4318 case CK_DeleteToWordBegin:
4319 case CK_DeleteToWordEnd:
4320 case CK_DeleteToHome:
4321 case CK_DeleteToEnd:
4322 format_paragraph (edit, 0);
4323 edit->force |= REDRAW_PAGE;
4328 /* --------------------------------------------------------------------------------------------- */
4330 void
4331 edit_stack_init (void)
4333 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4335 edit_history_moveto[edit_stack_iterator].filename = NULL;
4336 edit_history_moveto[edit_stack_iterator].line = -1;
4339 edit_stack_iterator = 0;
4342 /* --------------------------------------------------------------------------------------------- */
4344 void
4345 edit_stack_free (void)
4347 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4348 g_free (edit_history_moveto[edit_stack_iterator].filename);
4351 /* --------------------------------------------------------------------------------------------- */
4352 /** move i lines */
4354 void
4355 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4357 edit_move_updown (edit, i, do_scroll, TRUE);
4360 /* --------------------------------------------------------------------------------------------- */
4361 /** move i lines */
4363 void
4364 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4366 edit_move_updown (edit, i, do_scroll, FALSE);
4369 /* --------------------------------------------------------------------------------------------- */
4371 unsigned int
4372 edit_unlock_file (WEdit * edit)
4374 char *fullpath;
4375 unsigned int ret;
4377 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4378 ret = unlock_file (fullpath);
4379 g_free (fullpath);
4381 return ret;
4384 /* --------------------------------------------------------------------------------------------- */
4386 unsigned int
4387 edit_lock_file (WEdit * edit)
4389 char *fullpath;
4390 unsigned int ret;
4392 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4393 ret = lock_file (fullpath);
4394 g_free (fullpath);
4396 return ret;
4399 /* --------------------------------------------------------------------------------------------- */