keymap files: unification of Fxx keys: move to lower case.
[midnight-commander.git] / src / editor / edit.c
blobe5831f1054a00132e6f8a3a0ef369efd086bdc77
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 Authors: Paul Sheer 1996, 1997
7 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
25 /** \file
26 * \brief Source: editor low level data handling and cursor fundamentals
27 * \author Paul Sheer
28 * \date 1996, 1997
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
41 #include <fcntl.h>
43 #include "lib/global.h"
45 #include "lib/tty/color.h"
46 #include "lib/tty/tty.h" /* attrset() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
49 #include "lib/vfs/vfs.h"
50 #include "lib/strutil.h" /* utf string functions */
51 #include "lib/util.h" /* load_file_position(), save_file_position() */
52 #include "lib/timefmt.h" /* time formatting */
53 #include "lib/lock.h"
54 #include "lib/widget.h"
56 #ifdef HAVE_CHARSET
57 #include "lib/charsets.h" /* get_codepage_id */
58 #endif
60 #include "src/filemanager/cmd.h" /* view_other_cmd() */
61 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
63 #include "src/setup.h" /* option_tab_spacing */
64 #include "src/learn.h" /* learn_keys */
65 #include "src/keybind-defaults.h"
67 #include "edit-impl.h"
68 #include "edit-widget.h"
70 /*** global variables ****************************************************************************/
72 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
73 int option_typewriter_wrap = 0;
74 int option_auto_para_formatting = 0;
75 int option_fill_tabs_with_spaces = 0;
76 int option_return_does_auto_indent = 1;
77 int option_backspace_through_tabs = 0;
78 int option_fake_half_tabs = 1;
79 int option_save_mode = EDIT_QUICK_SAVE;
80 int option_save_position = 1;
81 int option_max_undo = 32768;
82 int option_persistent_selections = 1;
83 int option_cursor_beyond_eol = 0;
84 int option_line_state = 0;
85 int option_line_state_width = 0;
87 int option_edit_right_extreme = 0;
88 int option_edit_left_extreme = 0;
89 int option_edit_top_extreme = 0;
90 int option_edit_bottom_extreme = 0;
91 int enable_show_tabs_tws = 1;
92 int option_check_nl_at_eof = 0;
93 int option_group_undo = 0;
94 int show_right_margin = 0;
96 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
97 char *option_backup_ext = NULL;
99 int edit_stack_iterator = 0;
100 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
101 /* magic sequense for say than block is vertical */
102 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
104 /*** file scope macro definitions ****************************************************************/
106 #define TEMP_BUF_LEN 1024
108 #define space_width 1
110 /*** file scope type declarations ****************************************************************/
112 /*** file scope variables ************************************************************************/
114 /* detecting an error on save is easy: just check if every byte has been written. */
115 /* detecting an error on read, is not so easy 'cos there is not way to tell
116 whether you read everything or not. */
117 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
118 static const struct edit_filters
120 const char *read, *write, *extension;
121 } all_filters[] =
123 /* *INDENT-OFF* */
124 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
125 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
126 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
127 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
129 /* *INDENT-ON* */
132 static long last_bracket = -1;
134 /*** file scope functions ************************************************************************/
135 /* --------------------------------------------------------------------------------------------- */
139 * here's a quick sketch of the layout: (don't run this through indent.)
141 * (b1 is buffers1 and b2 is buffers2)
144 * \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
145 * ______________________________________|______________________________________
147 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
148 * |-> |-> |-> |-> |-> |-> |
150 * _<------------------------->|<----------------->_
151 * WEdit->curs2 | WEdit->curs1
152 * ^ | ^
153 * | ^|^ |
154 * cursor ||| cursor
155 * |||
156 * file end|||file beginning
161 * This_is_some_file
162 * fin.
165 /* --------------------------------------------------------------------------------------------- */
167 static int left_of_four_spaces (WEdit * edit);
169 /* --------------------------------------------------------------------------------------------- */
171 static void
172 edit_about (void)
174 const char *header = N_("About");
175 const char *button_name = N_("&OK");
176 const char *const version = "MCEdit " VERSION;
177 char text[BUF_LARGE];
179 int win_len, version_len, button_len;
180 int cols, lines;
182 Dlg_head *about_dlg;
184 #ifdef ENABLE_NLS
185 header = _(header);
186 button_name = _(button_name);
187 #endif
189 button_len = str_term_width1 (button_name) + 5;
190 version_len = str_term_width1 (version);
192 g_snprintf (text, sizeof (text),
193 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
194 " A user friendly text editor\n"
195 " written for the Midnight Commander"));
197 win_len = str_term_width1 (header);
198 win_len = max (win_len, version_len);
199 win_len = max (win_len, button_len);
201 /* count width and height of text */
202 str_msg_term_size (text, &lines, &cols);
203 lines += 9;
204 cols = max (win_len, cols) + 6;
206 /* dialog */
207 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
208 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
210 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
211 add_widget (about_dlg, label_new (5, 3, text));
212 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
213 B_ENTER, NORMAL_BUTTON, button_name, NULL));
215 run_dlg (about_dlg);
216 destroy_dlg (about_dlg);
219 /* --------------------------------------------------------------------------------------------- */
221 * Initialize the buffers for an empty files.
224 static void
225 edit_init_buffers (WEdit * edit)
227 int j;
229 for (j = 0; j <= MAXBUFF; j++)
231 edit->buffers1[j] = NULL;
232 edit->buffers2[j] = NULL;
235 edit->curs1 = 0;
236 edit->curs2 = 0;
237 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
240 /* --------------------------------------------------------------------------------------------- */
242 * Load file OR text into buffers. Set cursor to the beginning of file.
243 * @returns 1 on error.
246 static int
247 edit_load_file_fast (WEdit * edit, const char *filename)
249 long buf, buf2;
250 int file = -1;
251 int ret = 1;
253 edit->curs2 = edit->last_byte;
254 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
256 file = mc_open (filename, O_RDONLY | O_BINARY);
257 if (file == -1)
259 gchar *errmsg;
261 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
262 edit_error_dialog (_("Error"), errmsg);
263 g_free (errmsg);
264 return 1;
267 if (!edit->buffers2[buf2])
268 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
272 if (mc_read (file,
273 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
274 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
275 break;
277 for (buf = buf2 - 1; buf >= 0; buf--)
279 /* edit->buffers2[0] is already allocated */
280 if (!edit->buffers2[buf])
281 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
282 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
283 break;
285 ret = 0;
287 while (0);
288 if (ret)
290 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
291 edit_error_dialog (_("Error"), err_str);
292 g_free (err_str);
294 mc_close (file);
295 return ret;
298 /* --------------------------------------------------------------------------------------------- */
299 /** Return index of the filter or -1 is there is no appropriate filter */
301 static int
302 edit_find_filter (const char *filename)
304 size_t i, l, e;
306 if (filename == NULL)
307 return -1;
309 l = strlen (filename);
310 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
312 e = strlen (all_filters[i].extension);
313 if (l > e)
314 if (!strcmp (all_filters[i].extension, filename + l - e))
315 return i;
317 return -1;
320 /* --------------------------------------------------------------------------------------------- */
322 static char *
323 edit_get_filter (const char *filename)
325 int i;
326 char *p, *quoted_name;
328 i = edit_find_filter (filename);
329 if (i < 0)
330 return NULL;
332 quoted_name = name_quote (filename, 0);
333 p = g_strdup_printf (all_filters[i].read, quoted_name);
334 g_free (quoted_name);
335 return p;
338 /* --------------------------------------------------------------------------------------------- */
340 static long
341 edit_insert_stream (WEdit * edit, FILE * f)
343 int c;
344 long i = 0;
345 while ((c = fgetc (f)) >= 0)
347 edit_insert (edit, c);
348 i++;
350 return i;
353 /* --------------------------------------------------------------------------------------------- */
354 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
356 static int
357 check_file_access (WEdit * edit, const char *filename, struct stat *st)
359 int file;
360 gchar *errmsg = NULL;
362 /* Try opening an existing file */
363 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
365 if (file < 0)
368 * Try creating the file. O_EXCL prevents following broken links
369 * and opening existing files.
371 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
372 if (file < 0)
374 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
375 goto cleanup;
377 else
379 /* New file, delete it if it's not modified or saved */
380 edit->delete_file = 1;
384 /* Check what we have opened */
385 if (mc_fstat (file, st) < 0)
387 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
388 goto cleanup;
391 /* We want to open regular files only */
392 if (!S_ISREG (st->st_mode))
394 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
395 goto cleanup;
399 * Don't delete non-empty files.
400 * O_EXCL should prevent it, but let's be on the safe side.
402 if (st->st_size > 0)
403 edit->delete_file = 0;
405 if (st->st_size >= SIZE_LIMIT)
406 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
408 cleanup:
409 (void) mc_close (file);
411 if (errmsg != NULL)
413 edit_error_dialog (_("Error"), errmsg);
414 g_free (errmsg);
415 return 1;
417 return 0;
420 /* --------------------------------------------------------------------------------------------- */
422 * Open the file and load it into the buffers, either directly or using
423 * a filter. Return 0 on success, 1 on error.
425 * Fast loading (edit_load_file_fast) is used when the file size is
426 * known. In this case the data is read into the buffers by blocks.
427 * If the file size is not known, the data is loaded byte by byte in
428 * edit_insert_file.
431 static int
432 edit_load_file (WEdit * edit)
434 int fast_load = 1;
435 vfs_path_t *vpath = vfs_path_from_str (edit->filename);
437 /* Cannot do fast load if a filter is used */
438 if (edit_find_filter (edit->filename) >= 0)
439 fast_load = 0;
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (vpath))
446 fast_load = 0;
447 vfs_path_free (vpath);
450 * FIXME: line end translation should disable fast loading as well
451 * Consider doing fseek() to the end and ftell() for the real size.
454 if (*edit->filename)
456 /* If we are dealing with a real file, check that it exists */
457 if (check_file_access (edit, edit->filename, &edit->stat1))
458 return 1;
460 else
462 /* nothing to load */
463 fast_load = 0;
466 edit_init_buffers (edit);
468 if (fast_load)
470 edit->last_byte = edit->stat1.st_size;
471 edit_load_file_fast (edit, edit->filename);
472 /* If fast load was used, the number of lines wasn't calculated */
473 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
475 else
477 edit->last_byte = 0;
478 if (*edit->filename)
480 edit->undo_stack_disable = 1;
481 if (edit_insert_file (edit, edit->filename) == 0)
483 edit_clean (edit);
484 return 1;
486 edit->undo_stack_disable = 0;
489 edit->lb = LB_ASIS;
490 return 0;
493 /* --------------------------------------------------------------------------------------------- */
494 /** Restore saved cursor position in the file */
496 static void
497 edit_load_position (WEdit * edit)
499 char *filename;
500 long line, column;
501 off_t offset;
502 vfs_path_t *vpath;
504 if (!edit->filename || !*edit->filename)
505 return;
507 vpath = vfs_path_from_str (edit->filename);
508 filename = vfs_path_to_str (vpath);
509 load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
510 vfs_path_free (vpath);
511 g_free (filename);
513 if (line > 0)
515 edit_move_to_line (edit, line - 1);
516 edit->prev_col = column;
518 else if (offset > 0)
520 edit_cursor_move (edit, offset);
521 line = edit->curs_line;
522 edit->search_start = edit->curs1;
525 book_mark_restore (edit, BOOK_MARK_COLOR);
527 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
528 edit_move_display (edit, line - (edit->widget.lines / 2));
531 /* --------------------------------------------------------------------------------------------- */
532 /** Save cursor position in the file */
534 static void
535 edit_save_position (WEdit * edit)
537 char *filename;
538 vfs_path_t *vpath;
540 if (edit->filename == NULL || *edit->filename == '\0')
541 return;
543 vpath = vfs_path_from_str (edit->filename);
544 filename = vfs_path_to_str (vpath);
546 book_mark_serialize (edit, BOOK_MARK_COLOR);
547 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
548 edit->serialized_bookmarks);
549 edit->serialized_bookmarks = NULL;
551 g_free (filename);
552 vfs_path_free (vpath);
555 /* --------------------------------------------------------------------------------------------- */
556 /** Clean the WEdit stricture except the widget part */
558 static void
559 edit_purge_widget (WEdit * edit)
561 size_t len = sizeof (WEdit) - sizeof (Widget);
562 char *start = (char *) edit + sizeof (Widget);
563 memset (start, 0, len);
566 /* --------------------------------------------------------------------------------------------- */
569 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
570 then the file should be as it was when he loaded up. Then set edit->modified to 0.
573 static long
574 edit_pop_undo_action (WEdit * edit)
576 long c;
577 unsigned long sp = edit->undo_stack_pointer;
579 if (sp == edit->undo_stack_bottom)
580 return STACK_BOTTOM;
582 sp = (sp - 1) & edit->undo_stack_size_mask;
583 c = edit->undo_stack[sp];
584 if (c >= 0)
586 /* edit->undo_stack[sp] = '@'; */
587 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
588 return c;
591 if (sp == edit->undo_stack_bottom)
592 return STACK_BOTTOM;
594 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
595 if (edit->undo_stack[sp] == -2)
597 /* edit->undo_stack[sp] = '@'; */
598 edit->undo_stack_pointer = sp;
600 else
601 edit->undo_stack[sp]++;
603 return c;
606 static long
607 edit_pop_redo_action (WEdit * edit)
609 long c;
610 unsigned long sp = edit->redo_stack_pointer;
612 if (sp == edit->redo_stack_bottom)
613 return STACK_BOTTOM;
615 sp = (sp - 1) & edit->redo_stack_size_mask;
616 c = edit->redo_stack[sp];
617 if (c >= 0)
619 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
620 return c;
623 if (sp == edit->redo_stack_bottom)
624 return STACK_BOTTOM;
626 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
627 if (edit->redo_stack[sp] == -2)
628 edit->redo_stack_pointer = sp;
629 else
630 edit->redo_stack[sp]++;
632 return c;
635 static long
636 get_prev_undo_action (WEdit * edit)
638 long c;
639 unsigned long sp = edit->undo_stack_pointer;
641 if (sp == edit->undo_stack_bottom)
642 return STACK_BOTTOM;
644 sp = (sp - 1) & edit->undo_stack_size_mask;
645 c = edit->undo_stack[sp];
646 if (c >= 0)
647 return c;
649 if (sp == edit->undo_stack_bottom)
650 return STACK_BOTTOM;
652 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
653 return c;
656 /* --------------------------------------------------------------------------------------------- */
657 /** is called whenever a modification is made by one of the four routines below */
659 static void
660 edit_modification (WEdit * edit)
662 edit->caches_valid = 0;
664 /* raise lock when file modified */
665 if (!edit->modified && !edit->delete_file)
666 edit->locked = edit_lock_file (edit);
667 edit->modified = 1;
670 /* --------------------------------------------------------------------------------------------- */
672 static int
673 edit_backspace (WEdit * edit, const int byte_delete)
675 int p = 0;
676 int cw = 1;
677 int i;
679 if (!edit->curs1)
680 return 0;
682 cw = 1;
684 if (edit->mark2 != edit->mark1)
685 edit_push_markers (edit);
687 if (edit->utf8 && byte_delete == 0)
689 edit_get_prev_utf (edit, edit->curs1, &cw);
690 if (cw < 1)
691 cw = 1;
693 for (i = 1; i <= cw; i++)
695 if (edit->mark1 >= edit->curs1)
697 edit->mark1--;
698 edit->end_mark_curs--;
700 if (edit->mark2 >= edit->curs1)
701 edit->mark2--;
702 if (edit->last_get_rule >= edit->curs1)
703 edit->last_get_rule--;
705 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
706 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
707 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
709 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
710 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
712 edit->last_byte--;
713 edit->curs1--;
714 edit_push_undo_action (edit, p);
716 edit_modification (edit);
717 if (p == '\n')
719 if (edit->book_mark)
720 book_mark_dec (edit, edit->curs_line);
721 edit->curs_line--;
722 edit->total_lines--;
723 edit->force |= REDRAW_AFTER_CURSOR;
726 if (edit->curs1 < edit->start_display)
728 edit->start_display--;
729 if (p == '\n')
730 edit->start_line--;
733 return p;
736 /* --------------------------------------------------------------------------------------------- */
737 /* high level cursor movement commands */
738 /* --------------------------------------------------------------------------------------------- */
740 static int
741 is_in_indent (WEdit * edit)
743 long p = edit_bol (edit, edit->curs1);
744 while (p < edit->curs1)
745 if (!strchr (" \t", edit_get_byte (edit, p++)))
746 return 0;
747 return 1;
750 /* --------------------------------------------------------------------------------------------- */
752 static int
753 is_blank (WEdit * edit, long offset)
755 long s, f;
756 int c;
757 s = edit_bol (edit, offset);
758 f = edit_eol (edit, offset) - 1;
759 while (s <= f)
761 c = edit_get_byte (edit, s++);
762 if (!isspace (c))
763 return 0;
765 return 1;
769 /* --------------------------------------------------------------------------------------------- */
770 /** returns the offset of line i */
772 static long
773 edit_find_line (WEdit * edit, int line)
775 int i, j = 0;
776 int m = 2000000000;
777 if (!edit->caches_valid)
779 for (i = 0; i < N_LINE_CACHES; i++)
780 edit->line_numbers[i] = edit->line_offsets[i] = 0;
781 /* three offsets that we *know* are line 0 at 0 and these two: */
782 edit->line_numbers[1] = edit->curs_line;
783 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
784 edit->line_numbers[2] = edit->total_lines;
785 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
786 edit->caches_valid = 1;
788 if (line >= edit->total_lines)
789 return edit->line_offsets[2];
790 if (line <= 0)
791 return 0;
792 /* find the closest known point */
793 for (i = 0; i < N_LINE_CACHES; i++)
795 int n;
796 n = abs (edit->line_numbers[i] - line);
797 if (n < m)
799 m = n;
800 j = i;
803 if (m == 0)
804 return edit->line_offsets[j]; /* know the offset exactly */
805 if (m == 1 && j >= 3)
806 i = j; /* one line different - caller might be looping, so stay in this cache */
807 else
808 i = 3 + (rand () % (N_LINE_CACHES - 3));
809 if (line > edit->line_numbers[j])
810 edit->line_offsets[i] =
811 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
812 else
813 edit->line_offsets[i] =
814 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
815 edit->line_numbers[i] = line;
816 return edit->line_offsets[i];
819 /* --------------------------------------------------------------------------------------------- */
820 /** moves up until a blank line is reached, or until just
821 before a non-blank line is reached */
823 static void
824 edit_move_up_paragraph (WEdit * edit, int do_scroll)
826 int i = 0;
827 if (edit->curs_line > 1)
829 if (line_is_blank (edit, edit->curs_line))
831 if (line_is_blank (edit, edit->curs_line - 1))
833 for (i = edit->curs_line - 1; i; i--)
834 if (!line_is_blank (edit, i))
836 i++;
837 break;
840 else
842 for (i = edit->curs_line - 1; i; i--)
843 if (line_is_blank (edit, i))
844 break;
847 else
849 for (i = edit->curs_line - 1; i; i--)
850 if (line_is_blank (edit, i))
851 break;
854 edit_move_up (edit, edit->curs_line - i, do_scroll);
857 /* --------------------------------------------------------------------------------------------- */
858 /** moves down until a blank line is reached, or until just
859 before a non-blank line is reached */
861 static void
862 edit_move_down_paragraph (WEdit * edit, int do_scroll)
864 int i;
865 if (edit->curs_line >= edit->total_lines - 1)
867 i = edit->total_lines;
869 else
871 if (line_is_blank (edit, edit->curs_line))
873 if (line_is_blank (edit, edit->curs_line + 1))
875 for (i = edit->curs_line + 1; i; i++)
876 if (!line_is_blank (edit, i) || i > edit->total_lines)
878 i--;
879 break;
882 else
884 for (i = edit->curs_line + 1; i; i++)
885 if (line_is_blank (edit, i) || i >= edit->total_lines)
886 break;
889 else
891 for (i = edit->curs_line + 1; i; i++)
892 if (line_is_blank (edit, i) || i >= edit->total_lines)
893 break;
896 edit_move_down (edit, i - edit->curs_line, do_scroll);
899 /* --------------------------------------------------------------------------------------------- */
901 static void
902 edit_begin_page (WEdit * edit)
904 edit_update_curs_row (edit);
905 edit_move_up (edit, edit->curs_row, 0);
908 /* --------------------------------------------------------------------------------------------- */
910 static void
911 edit_end_page (WEdit * edit)
913 edit_update_curs_row (edit);
914 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
918 /* --------------------------------------------------------------------------------------------- */
919 /** goto beginning of text */
921 static void
922 edit_move_to_top (WEdit * edit)
924 if (edit->curs_line)
926 edit_cursor_move (edit, -edit->curs1);
927 edit_move_to_prev_col (edit, 0);
928 edit->force |= REDRAW_PAGE;
929 edit->search_start = 0;
930 edit_update_curs_row (edit);
935 /* --------------------------------------------------------------------------------------------- */
936 /** goto end of text */
938 static void
939 edit_move_to_bottom (WEdit * edit)
941 if (edit->curs_line < edit->total_lines)
943 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
944 edit->start_display = edit->last_byte;
945 edit->start_line = edit->total_lines;
946 edit_scroll_upward (edit, edit->widget.lines - 1);
947 edit->force |= REDRAW_PAGE;
951 /* --------------------------------------------------------------------------------------------- */
952 /** goto beginning of line */
954 static void
955 edit_cursor_to_bol (WEdit * edit)
957 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
958 edit->search_start = edit->curs1;
959 edit->prev_col = edit_get_col (edit);
960 edit->over_col = 0;
963 /* --------------------------------------------------------------------------------------------- */
964 /** goto end of line */
966 static void
967 edit_cursor_to_eol (WEdit * edit)
969 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
970 edit->search_start = edit->curs1;
971 edit->prev_col = edit_get_col (edit);
972 edit->over_col = 0;
975 /* --------------------------------------------------------------------------------------------- */
977 static unsigned long
978 my_type_of (int c)
980 int x, r = 0;
981 const char *p, *q;
982 const char option_chars_move_whole_word[] =
983 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
985 if (!c)
986 return 0;
987 if (c == '!')
989 if (*option_chars_move_whole_word == '!')
990 return 2;
991 return 0x80000000UL;
993 if (g_ascii_isupper ((gchar) c))
994 c = 'A';
995 else if (g_ascii_islower ((gchar) c))
996 c = 'a';
997 else if (g_ascii_isalpha (c))
998 c = 'a';
999 else if (isdigit (c))
1000 c = '0';
1001 else if (isspace (c))
1002 c = ' ';
1003 q = strchr (option_chars_move_whole_word, c);
1004 if (!q)
1005 return 0xFFFFFFFFUL;
1008 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1009 if (*p == '!')
1010 x <<= 1;
1011 r |= x;
1013 while ((q = strchr (q + 1, c)));
1014 return r;
1017 /* --------------------------------------------------------------------------------------------- */
1019 static void
1020 edit_left_word_move (WEdit * edit, int s)
1022 for (;;)
1024 int c1, c2;
1025 if (edit->column_highlight
1026 && edit->mark1 != edit->mark2
1027 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1028 break;
1029 edit_cursor_move (edit, -1);
1030 if (!edit->curs1)
1031 break;
1032 c1 = edit_get_byte (edit, edit->curs1 - 1);
1033 c2 = edit_get_byte (edit, edit->curs1);
1034 if (c1 == '\n' || c2 == '\n')
1035 break;
1036 if (!(my_type_of (c1) & my_type_of (c2)))
1037 break;
1038 if (isspace (c1) && !isspace (c2))
1039 break;
1040 if (s)
1041 if (!isspace (c1) && isspace (c2))
1042 break;
1046 /* --------------------------------------------------------------------------------------------- */
1048 static void
1049 edit_left_word_move_cmd (WEdit * edit)
1051 edit_left_word_move (edit, 0);
1052 edit->force |= REDRAW_PAGE;
1055 /* --------------------------------------------------------------------------------------------- */
1057 static void
1058 edit_right_word_move (WEdit * edit, int s)
1060 for (;;)
1062 int c1, c2;
1063 if (edit->column_highlight
1064 && edit->mark1 != edit->mark2
1065 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1066 break;
1067 edit_cursor_move (edit, 1);
1068 if (edit->curs1 >= edit->last_byte)
1069 break;
1070 c1 = edit_get_byte (edit, edit->curs1 - 1);
1071 c2 = edit_get_byte (edit, edit->curs1);
1072 if (c1 == '\n' || c2 == '\n')
1073 break;
1074 if (!(my_type_of (c1) & my_type_of (c2)))
1075 break;
1076 if (isspace (c1) && !isspace (c2))
1077 break;
1078 if (s)
1079 if (!isspace (c1) && isspace (c2))
1080 break;
1084 /* --------------------------------------------------------------------------------------------- */
1086 static void
1087 edit_right_word_move_cmd (WEdit * edit)
1089 edit_right_word_move (edit, 0);
1090 edit->force |= REDRAW_PAGE;
1093 /* --------------------------------------------------------------------------------------------- */
1095 static void
1096 edit_right_char_move_cmd (WEdit * edit)
1098 int cw = 1;
1099 int c = 0;
1100 if (edit->utf8)
1102 c = edit_get_utf (edit, edit->curs1, &cw);
1103 if (cw < 1)
1104 cw = 1;
1106 else
1108 c = edit_get_byte (edit, edit->curs1);
1110 if (option_cursor_beyond_eol && c == '\n')
1112 edit->over_col++;
1114 else
1116 edit_cursor_move (edit, cw);
1120 /* --------------------------------------------------------------------------------------------- */
1122 static void
1123 edit_left_char_move_cmd (WEdit * edit)
1125 int cw = 1;
1126 if (edit->column_highlight
1127 && option_cursor_beyond_eol
1128 && edit->mark1 != edit->mark2
1129 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1130 return;
1131 if (edit->utf8)
1133 edit_get_prev_utf (edit, edit->curs1, &cw);
1134 if (cw < 1)
1135 cw = 1;
1137 if (option_cursor_beyond_eol && edit->over_col > 0)
1139 edit->over_col--;
1141 else
1143 edit_cursor_move (edit, -cw);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Up or down cursor moving.
1149 direction = TRUE - move up
1150 = FALSE - move down
1153 static void
1154 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1156 unsigned long p;
1157 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1159 if (i > l)
1160 i = l;
1162 if (i == 0)
1163 return;
1165 if (i > 1)
1166 edit->force |= REDRAW_PAGE;
1167 if (do_scroll)
1169 if (direction)
1170 edit_scroll_upward (edit, i);
1171 else
1172 edit_scroll_downward (edit, i);
1174 p = edit_bol (edit, edit->curs1);
1176 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1178 edit_cursor_move (edit, p - edit->curs1);
1180 edit_move_to_prev_col (edit, p);
1182 /* search start of current multibyte char (like CJK) */
1183 if (edit->curs1 + 1 < edit->last_byte)
1185 edit_right_char_move_cmd (edit);
1186 edit_left_char_move_cmd (edit);
1189 edit->search_start = edit->curs1;
1190 edit->found_len = 0;
1193 /* --------------------------------------------------------------------------------------------- */
1195 static void
1196 edit_right_delete_word (WEdit * edit)
1198 int c1, c2;
1199 for (;;)
1201 if (edit->curs1 >= edit->last_byte)
1202 break;
1203 c1 = edit_delete (edit, 1);
1204 c2 = edit_get_byte (edit, edit->curs1);
1205 if (c1 == '\n' || c2 == '\n')
1206 break;
1207 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1208 break;
1209 if (!(my_type_of (c1) & my_type_of (c2)))
1210 break;
1214 /* --------------------------------------------------------------------------------------------- */
1216 static void
1217 edit_left_delete_word (WEdit * edit)
1219 int c1, c2;
1220 for (;;)
1222 if (edit->curs1 <= 0)
1223 break;
1224 c1 = edit_backspace (edit, 1);
1225 c2 = edit_get_byte (edit, edit->curs1 - 1);
1226 if (c1 == '\n' || c2 == '\n')
1227 break;
1228 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1229 break;
1230 if (!(my_type_of (c1) & my_type_of (c2)))
1231 break;
1235 /* --------------------------------------------------------------------------------------------- */
1237 the start column position is not recorded, and hence does not
1238 undo as it happed. But who would notice.
1241 static void
1242 edit_do_undo (WEdit * edit)
1244 long ac;
1245 long count = 0;
1247 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1248 edit->over_col = 0;
1249 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1251 switch ((int) ac)
1253 case STACK_BOTTOM:
1254 goto done_undo;
1255 case CURS_RIGHT:
1256 edit_cursor_move (edit, 1);
1257 break;
1258 case CURS_LEFT:
1259 edit_cursor_move (edit, -1);
1260 break;
1261 case BACKSPACE:
1262 case BACKSPACE_BR:
1263 edit_backspace (edit, 1);
1264 break;
1265 case DELCHAR:
1266 case DELCHAR_BR:
1267 edit_delete (edit, 1);
1268 break;
1269 case COLUMN_ON:
1270 edit->column_highlight = 1;
1271 break;
1272 case COLUMN_OFF:
1273 edit->column_highlight = 0;
1274 break;
1276 if (ac >= 256 && ac < 512)
1277 edit_insert_ahead (edit, ac - 256);
1278 if (ac >= 0 && ac < 256)
1279 edit_insert (edit, ac);
1281 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1283 edit->mark1 = ac - MARK_1;
1284 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1286 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1288 edit->mark2 = ac - MARK_2;
1289 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1291 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1293 edit->end_mark_curs = ac - MARK_CURS;
1295 if (count++)
1296 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1299 if (edit->start_display > ac - KEY_PRESS)
1301 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1302 edit->force |= REDRAW_PAGE;
1304 else if (edit->start_display < ac - KEY_PRESS)
1306 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1307 edit->force |= REDRAW_PAGE;
1309 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1310 edit_update_curs_row (edit);
1312 done_undo:;
1313 edit->undo_stack_disable = 0;
1316 static void
1317 edit_do_redo (WEdit * edit)
1319 long ac;
1320 long count = 0;
1322 if (edit->redo_stack_reset)
1323 return;
1325 edit->over_col = 0;
1326 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1328 switch ((int) ac)
1330 case STACK_BOTTOM:
1331 goto done_redo;
1332 case CURS_RIGHT:
1333 edit_cursor_move (edit, 1);
1334 break;
1335 case CURS_LEFT:
1336 edit_cursor_move (edit, -1);
1337 break;
1338 case BACKSPACE:
1339 edit_backspace (edit, 1);
1340 break;
1341 case DELCHAR:
1342 edit_delete (edit, 1);
1343 break;
1344 case COLUMN_ON:
1345 edit->column_highlight = 1;
1346 break;
1347 case COLUMN_OFF:
1348 edit->column_highlight = 0;
1349 break;
1351 if (ac >= 256 && ac < 512)
1352 edit_insert_ahead (edit, ac - 256);
1353 if (ac >= 0 && ac < 256)
1354 edit_insert (edit, ac);
1356 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1358 edit->mark1 = ac - MARK_1;
1359 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1361 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1363 edit->mark2 = ac - MARK_2;
1364 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1366 /* more than one pop usually means something big */
1367 if (count++)
1368 edit->force |= REDRAW_PAGE;
1371 if (edit->start_display > ac - KEY_PRESS)
1373 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1374 edit->force |= REDRAW_PAGE;
1376 else if (edit->start_display < ac - KEY_PRESS)
1378 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1379 edit->force |= REDRAW_PAGE;
1381 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1382 edit_update_curs_row (edit);
1384 done_redo:;
1387 static void
1388 edit_group_undo (WEdit * edit)
1390 long ac = KEY_PRESS;
1391 long cur_ac = KEY_PRESS;
1392 while (ac != STACK_BOTTOM && ac == cur_ac)
1394 cur_ac = get_prev_undo_action (edit);
1395 edit_do_undo (edit);
1396 ac = get_prev_undo_action (edit);
1397 /* exit from cycle if option_group_undo is not set,
1398 * and make single UNDO operation
1400 if (!option_group_undo)
1401 ac = STACK_BOTTOM;
1405 /* --------------------------------------------------------------------------------------------- */
1407 static void
1408 edit_delete_to_line_end (WEdit * edit)
1410 while (edit_get_byte (edit, edit->curs1) != '\n')
1412 if (!edit->curs2)
1413 break;
1414 edit_delete (edit, 1);
1418 /* --------------------------------------------------------------------------------------------- */
1420 static void
1421 edit_delete_to_line_begin (WEdit * edit)
1423 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1425 if (!edit->curs1)
1426 break;
1427 edit_backspace (edit, 1);
1431 /* --------------------------------------------------------------------------------------------- */
1433 static int
1434 is_aligned_on_a_tab (WEdit * edit)
1436 edit_update_curs_col (edit);
1437 return !((edit->curs_col % (TAB_SIZE * space_width))
1438 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1441 /* --------------------------------------------------------------------------------------------- */
1443 static int
1444 right_of_four_spaces (WEdit * edit)
1446 int i, ch = 0;
1447 for (i = 1; i <= HALF_TAB_SIZE; i++)
1448 ch |= edit_get_byte (edit, edit->curs1 - i);
1449 if (ch == ' ')
1450 return is_aligned_on_a_tab (edit);
1451 return 0;
1454 /* --------------------------------------------------------------------------------------------- */
1456 static int
1457 left_of_four_spaces (WEdit * edit)
1459 int i, ch = 0;
1460 for (i = 0; i < HALF_TAB_SIZE; i++)
1461 ch |= edit_get_byte (edit, edit->curs1 + i);
1462 if (ch == ' ')
1463 return is_aligned_on_a_tab (edit);
1464 return 0;
1467 /* --------------------------------------------------------------------------------------------- */
1469 static void
1470 edit_auto_indent (WEdit * edit)
1472 long p;
1473 char c;
1474 p = edit->curs1;
1475 /* use the previous line as a template */
1476 p = edit_move_backward (edit, p, 1);
1477 /* copy the leading whitespace of the line */
1478 for (;;)
1479 { /* no range check - the line _is_ \n-terminated */
1480 c = edit_get_byte (edit, p++);
1481 if (c != ' ' && c != '\t')
1482 break;
1483 edit_insert (edit, c);
1487 /* --------------------------------------------------------------------------------------------- */
1489 static inline void
1490 edit_double_newline (WEdit * edit)
1492 edit_insert (edit, '\n');
1493 if (edit_get_byte (edit, edit->curs1) == '\n')
1494 return;
1495 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1496 return;
1497 edit->force |= REDRAW_PAGE;
1498 edit_insert (edit, '\n');
1501 /* --------------------------------------------------------------------------------------------- */
1503 static inline void
1504 edit_tab_cmd (WEdit * edit)
1506 int i;
1508 if (option_fake_half_tabs)
1510 if (is_in_indent (edit))
1512 /*insert a half tab (usually four spaces) unless there is a
1513 half tab already behind, then delete it and insert a
1514 full tab. */
1515 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
1517 for (i = 1; i <= HALF_TAB_SIZE; i++)
1518 edit_backspace (edit, 1);
1519 edit_insert (edit, '\t');
1521 else
1523 insert_spaces_tab (edit, 1);
1525 return;
1528 if (option_fill_tabs_with_spaces)
1530 insert_spaces_tab (edit, 0);
1532 else
1534 edit_insert (edit, '\t');
1538 /* --------------------------------------------------------------------------------------------- */
1540 static void
1541 check_and_wrap_line (WEdit * edit)
1543 int curs, c;
1544 if (!option_typewriter_wrap)
1545 return;
1546 edit_update_curs_col (edit);
1547 if (edit->curs_col < option_word_wrap_line_length)
1548 return;
1549 curs = edit->curs1;
1550 for (;;)
1552 curs--;
1553 c = edit_get_byte (edit, curs);
1554 if (c == '\n' || curs <= 0)
1556 edit_insert (edit, '\n');
1557 return;
1559 if (c == ' ' || c == '\t')
1561 int current = edit->curs1;
1562 edit_cursor_move (edit, curs - edit->curs1 + 1);
1563 edit_insert (edit, '\n');
1564 edit_cursor_move (edit, current - edit->curs1 + 1);
1565 return;
1570 /* --------------------------------------------------------------------------------------------- */
1571 /** this find the matching bracket in either direction, and sets edit->bracket */
1573 static long
1574 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1576 const char *const b = "{}{[][()(", *p;
1577 int i = 1, a, inc = -1, c, d, n = 0;
1578 unsigned long j = 0;
1579 long q;
1580 edit_update_curs_row (edit);
1581 c = edit_get_byte (edit, edit->curs1);
1582 p = strchr (b, c);
1583 /* no limit */
1584 if (!furthest_bracket_search)
1585 furthest_bracket_search--;
1586 /* not on a bracket at all */
1587 if (!p)
1588 return -1;
1589 /* the matching bracket */
1590 d = p[1];
1591 /* going left or right? */
1592 if (strchr ("{[(", c))
1593 inc = 1;
1594 for (q = edit->curs1 + inc;; q += inc)
1596 /* out of buffer? */
1597 if (q >= edit->last_byte || q < 0)
1598 break;
1599 a = edit_get_byte (edit, q);
1600 /* don't want to eat CPU */
1601 if (j++ > furthest_bracket_search)
1602 break;
1603 /* out of screen? */
1604 if (in_screen)
1606 if (q < edit->start_display)
1607 break;
1608 /* count lines if searching downward */
1609 if (inc > 0 && a == '\n')
1610 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1611 break;
1613 /* count bracket depth */
1614 i += (a == c) - (a == d);
1615 /* return if bracket depth is zero */
1616 if (!i)
1617 return q;
1619 /* no match */
1620 return -1;
1623 /* --------------------------------------------------------------------------------------------- */
1625 static inline void
1626 edit_goto_matching_bracket (WEdit * edit)
1628 long q;
1630 q = edit_get_bracket (edit, 0, 0);
1631 if (q >= 0)
1633 edit->bracket = edit->curs1;
1634 edit->force |= REDRAW_PAGE;
1635 edit_cursor_move (edit, q - edit->curs1);
1639 /* --------------------------------------------------------------------------------------------- */
1640 /*** public functions ****************************************************************************/
1641 /* --------------------------------------------------------------------------------------------- */
1643 /** User edit menu, like user menu (F2) but only in editor. */
1645 void
1646 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1648 char *block_file;
1649 int nomark;
1650 long curs;
1651 long start_mark, end_mark;
1652 struct stat status;
1654 block_file = concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE);
1655 curs = edit->curs1;
1656 nomark = eval_marks (edit, &start_mark, &end_mark);
1657 if (nomark == 0)
1658 edit_save_block (edit, block_file, start_mark, end_mark);
1660 /* run shell scripts from menu */
1661 if (user_menu_cmd (edit, menu_file, selected_entry)
1662 && (mc_stat (block_file, &status) == 0) && (status.st_size != 0))
1664 int rc = 0;
1665 FILE *fd;
1667 /* i.e. we have marked block */
1668 if (nomark == 0)
1669 rc = edit_block_delete_cmd (edit);
1671 if (rc == 0)
1673 long ins_len;
1675 ins_len = edit_insert_file (edit, block_file);
1676 if (nomark == 0 && ins_len > 0)
1677 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1679 /* truncate block file */
1680 fd = fopen (block_file, "w");
1681 if (fd != NULL)
1682 fclose (fd);
1684 edit_cursor_move (edit, curs - edit->curs1);
1685 edit_refresh_cmd (edit);
1686 edit->force |= REDRAW_COMPLETELY;
1688 g_free (block_file);
1691 /* --------------------------------------------------------------------------------------------- */
1694 edit_get_byte (WEdit * edit, long byte_index)
1696 unsigned long p;
1697 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1698 return '\n';
1700 if (byte_index >= edit->curs1)
1702 p = edit->curs1 + edit->curs2 - byte_index - 1;
1703 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1705 else
1707 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1711 /* --------------------------------------------------------------------------------------------- */
1713 char *
1714 edit_get_byte_ptr (WEdit * edit, long byte_index)
1716 unsigned long p;
1717 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1718 return NULL;
1720 if (byte_index >= edit->curs1)
1722 p = edit->curs1 + edit->curs2 - byte_index - 1;
1723 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1724 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1726 else
1728 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
1729 (byte_index & M_EDIT_BUF_SIZE));
1733 /* --------------------------------------------------------------------------------------------- */
1735 char *
1736 edit_get_buf_ptr (WEdit * edit, long byte_index)
1738 unsigned long p;
1740 if (byte_index >= (edit->curs1 + edit->curs2))
1741 byte_index--;
1743 if (byte_index < 0)
1744 return NULL;
1746 if (byte_index >= edit->curs1)
1748 p = edit->curs1 + edit->curs2 - 1;
1749 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1750 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1752 else
1754 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
1758 /* --------------------------------------------------------------------------------------------- */
1761 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1763 gchar *str = NULL;
1764 int res = -1;
1765 gunichar ch;
1766 gchar *next_ch = NULL;
1767 int width = 0;
1769 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1771 *char_width = 0;
1772 return '\n';
1775 str = edit_get_byte_ptr (edit, byte_index);
1777 if (str == NULL)
1779 *char_width = 0;
1780 return 0;
1783 res = g_utf8_get_char_validated (str, -1);
1785 if (res < 0)
1787 ch = *str;
1788 width = 0;
1790 else
1792 ch = res;
1793 /* Calculate UTF-8 char width */
1794 next_ch = g_utf8_next_char (str);
1795 if (next_ch)
1797 width = next_ch - str;
1799 else
1801 ch = 0;
1802 width = 0;
1805 *char_width = width;
1806 return ch;
1809 /* --------------------------------------------------------------------------------------------- */
1812 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
1814 gchar *str, *buf = NULL;
1815 int res = -1;
1816 gunichar ch;
1817 gchar *next_ch = NULL;
1818 int width = 0;
1820 if (byte_index > 0)
1821 byte_index--;
1823 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1825 *char_width = 0;
1826 return 0;
1829 ch = edit_get_utf (edit, byte_index, &width);
1831 if (width == 1)
1833 *char_width = width;
1834 return ch;
1837 str = edit_get_byte_ptr (edit, byte_index);
1838 buf = edit_get_buf_ptr (edit, byte_index);
1839 if (str == NULL || buf == NULL)
1841 *char_width = 0;
1842 return 0;
1844 /* get prev utf8 char */
1845 if (str != buf)
1846 str = g_utf8_find_prev_char (buf, str);
1848 if (str == NULL)
1850 *char_width = 0;
1851 return 0;
1853 else
1854 res = g_utf8_get_char_validated (str, -1);
1856 if (res < 0)
1858 ch = *str;
1859 width = 0;
1861 else
1863 ch = res;
1864 /* Calculate UTF-8 char width */
1865 next_ch = g_utf8_next_char (str);
1866 if (next_ch)
1868 width = next_ch - str;
1870 else
1872 ch = 0;
1873 width = 0;
1876 *char_width = width;
1877 return ch;
1880 /* --------------------------------------------------------------------------------------------- */
1882 char *
1883 edit_get_write_filter (const char *write_name, const char *filename)
1885 int i;
1886 char *p, *writename;
1888 i = edit_find_filter (filename);
1889 if (i < 0)
1890 return NULL;
1892 writename = name_quote (write_name, 0);
1893 p = g_strdup_printf (all_filters[i].write, writename);
1894 g_free (writename);
1895 return p;
1898 /* --------------------------------------------------------------------------------------------- */
1900 long
1901 edit_write_stream (WEdit * edit, FILE * f)
1903 long i;
1905 if (edit->lb == LB_ASIS)
1907 for (i = 0; i < edit->last_byte; i++)
1908 if (fputc (edit_get_byte (edit, i), f) < 0)
1909 break;
1910 return i;
1913 /* change line breaks */
1914 for (i = 0; i < edit->last_byte; i++)
1916 unsigned char c = edit_get_byte (edit, i);
1918 if (!(c == '\n' || c == '\r'))
1920 /* not line break */
1921 if (fputc (c, f) < 0)
1922 return i;
1924 else
1925 { /* (c == '\n' || c == '\r') */
1926 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1928 switch (edit->lb)
1930 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1931 /* put one line break unconditionally */
1932 if (fputc ('\n', f) < 0)
1933 return i;
1935 i++; /* 2 chars are processed */
1937 if (c == '\r' && c1 == '\n')
1938 /* Windows line break; go to the next char */
1939 break;
1941 if (c == '\r' && c1 == '\r')
1943 /* two Macintosh line breaks; put second line break */
1944 if (fputc ('\n', f) < 0)
1945 return i;
1946 break;
1949 if (fputc (c1, f) < 0)
1950 return i;
1951 break;
1953 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1954 /* put one line break unconditionally */
1955 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1956 return i;
1958 if (c == '\r' && c1 == '\n')
1959 /* Windows line break; go to the next char */
1960 i++;
1961 break;
1963 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1964 /* put one line break unconditionally */
1965 if (fputc ('\r', f) < 0)
1966 return i;
1968 i++; /* 2 chars are processed */
1970 if (c == '\r' && c1 == '\n')
1971 /* Windows line break; go to the next char */
1972 break;
1974 if (c == '\n' && c1 == '\n')
1976 /* two Windows line breaks; put second line break */
1977 if (fputc ('\r', f) < 0)
1978 return i;
1979 break;
1982 if (fputc (c1, f) < 0)
1983 return i;
1984 break;
1985 case LB_ASIS: /* default without changes */
1986 break;
1991 return edit->last_byte;
1994 /* --------------------------------------------------------------------------------------------- */
1995 /** inserts a file at the cursor, returns count of inserted bytes on success */
1996 long
1997 edit_insert_file (WEdit * edit, const char *filename)
1999 char *p;
2000 long ins_len = 0;
2002 p = edit_get_filter (filename);
2003 if (p != NULL)
2005 FILE *f;
2006 long current = edit->curs1;
2007 f = (FILE *) popen (p, "r");
2008 if (f != NULL)
2010 edit_insert_stream (edit, f);
2011 ins_len = edit->curs1 - current;
2012 edit_cursor_move (edit, current - edit->curs1);
2013 if (pclose (f) > 0)
2015 char *errmsg;
2016 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2017 edit_error_dialog (_("Error"), errmsg);
2018 g_free (errmsg);
2019 g_free (p);
2020 return 0;
2023 else
2025 char *errmsg;
2026 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2027 edit_error_dialog (_("Error"), errmsg);
2028 g_free (errmsg);
2029 g_free (p);
2030 return 0;
2032 g_free (p);
2034 else
2036 int i, file, blocklen;
2037 long current = edit->curs1;
2038 int vertical_insertion = 0;
2039 char *buf;
2040 file = mc_open (filename, O_RDONLY | O_BINARY);
2041 if (file == -1)
2042 return 0;
2043 buf = g_malloc0 (TEMP_BUF_LEN);
2044 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2045 if (blocklen > 0)
2047 /* if contain signature VERTICAL_MAGIC then it vertical block */
2048 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2049 vertical_insertion = 1;
2050 else
2051 mc_lseek (file, 0, SEEK_SET);
2053 if (vertical_insertion)
2055 long mark1, mark2;
2056 int c1, c2;
2057 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2058 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2059 /* highlight inserted text then not persistent blocks */
2060 if (!option_persistent_selections)
2062 if (!edit->column_highlight)
2063 edit_push_undo_action (edit, COLUMN_OFF);
2064 edit->column_highlight = 1;
2067 else
2069 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2071 for (i = 0; i < blocklen; i++)
2072 edit_insert (edit, buf[i]);
2074 /* highlight inserted text then not persistent blocks */
2075 if (!option_persistent_selections && edit->modified)
2077 edit_set_markers (edit, edit->curs1, current, 0, 0);
2078 if (edit->column_highlight)
2079 edit_push_undo_action (edit, COLUMN_ON);
2080 edit->column_highlight = 0;
2083 edit->force |= REDRAW_PAGE;
2084 ins_len = edit->curs1 - current;
2085 edit_cursor_move (edit, current - edit->curs1);
2086 g_free (buf);
2087 mc_close (file);
2088 if (blocklen != 0)
2089 return 0;
2091 return ins_len;
2094 /* --------------------------------------------------------------------------------------------- */
2096 * Fill in the edit structure. Return NULL on failure. Pass edit as
2097 * NULL to allocate a new structure.
2099 * If line is 0, try to restore saved position. Otherwise put the
2100 * cursor on that line and show it in the middle of the screen.
2103 WEdit *
2104 edit_init (WEdit * edit, int y, int x, int lines, int cols, const char *filename, long line)
2106 gboolean to_free = FALSE;
2108 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2109 if (option_line_state)
2110 option_line_state_width = LINE_STATE_WIDTH;
2111 else
2112 option_line_state_width = 0;
2114 if (edit == NULL)
2116 #ifdef ENABLE_NLS
2118 * Expand option_whole_chars_search by national letters using
2119 * current locale
2122 static char option_whole_chars_search_buf[256];
2124 if (option_whole_chars_search_buf != option_whole_chars_search)
2126 size_t i;
2127 size_t len = str_term_width1 (option_whole_chars_search);
2129 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2131 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2133 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2135 option_whole_chars_search_buf[len++] = i;
2139 option_whole_chars_search_buf[len] = 0;
2140 option_whole_chars_search = option_whole_chars_search_buf;
2142 #endif /* ENABLE_NLS */
2143 edit = g_malloc0 (sizeof (WEdit));
2144 edit->search = NULL;
2145 to_free = TRUE;
2148 edit_purge_widget (edit);
2149 edit->widget.y = y;
2150 edit->widget.x = x;
2151 edit->widget.lines = lines;
2152 edit->widget.cols = cols;
2154 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2155 edit->stat1.st_uid = getuid ();
2156 edit->stat1.st_gid = getgid ();
2157 edit->stat1.st_mtime = 0;
2159 edit->over_col = 0;
2160 edit->bracket = -1;
2161 edit->force |= REDRAW_PAGE;
2162 edit_set_filename (edit, filename);
2164 edit->undo_stack_size = START_STACK_SIZE;
2165 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2166 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2168 edit->redo_stack_size = START_STACK_SIZE;
2169 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2170 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2172 edit->utf8 = 0;
2173 edit->converter = str_cnv_from_term;
2174 edit_set_codeset (edit);
2176 if (edit_load_file (edit))
2178 /* edit_load_file already gives an error message */
2179 if (to_free)
2180 g_free (edit);
2181 return NULL;
2184 edit->loading_done = 1;
2185 edit->modified = 0;
2186 edit->locked = 0;
2187 edit_load_syntax (edit, NULL, NULL);
2189 int color;
2190 edit_get_syntax_color (edit, -1, &color);
2193 /* load saved cursor position */
2194 if ((line == 0) && option_save_position)
2195 edit_load_position (edit);
2196 else
2198 if (line <= 0)
2199 line = 1;
2200 edit_move_display (edit, line - 1);
2201 edit_move_to_line (edit, line - 1);
2204 edit_load_macro_cmd (edit);
2205 return edit;
2208 /* --------------------------------------------------------------------------------------------- */
2209 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2212 edit_clean (WEdit * edit)
2214 int j = 0;
2216 if (!edit)
2217 return 0;
2219 /* a stale lock, remove it */
2220 if (edit->locked)
2221 edit->locked = edit_unlock_file (edit);
2223 /* save cursor position */
2224 if (option_save_position)
2225 edit_save_position (edit);
2226 else if (edit->serialized_bookmarks != NULL)
2227 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2229 /* File specified on the mcedit command line and never saved */
2230 if (edit->delete_file)
2231 unlink (edit->filename);
2233 edit_free_syntax_rules (edit);
2234 book_mark_flush (edit, -1);
2235 for (; j <= MAXBUFF; j++)
2237 g_free (edit->buffers1[j]);
2238 g_free (edit->buffers2[j]);
2241 g_free (edit->undo_stack);
2242 g_free (edit->redo_stack);
2243 g_free (edit->filename);
2244 g_free (edit->dir);
2245 mc_search_free (edit->search);
2246 edit->search = NULL;
2248 if (edit->converter != str_cnv_from_term)
2249 str_close_conv (edit->converter);
2251 edit_purge_widget (edit);
2253 return 1;
2256 /* --------------------------------------------------------------------------------------------- */
2257 /** returns 1 on success */
2260 edit_renew (WEdit * edit)
2262 int y = edit->widget.y;
2263 int x = edit->widget.x;
2264 int lines = edit->widget.lines;
2265 int columns = edit->widget.cols;
2267 edit_clean (edit);
2268 return (edit_init (edit, y, x, lines, columns, "", 0) != NULL);
2271 /* --------------------------------------------------------------------------------------------- */
2273 * Load a new file into the editor. If it fails, preserve the old file.
2274 * To do it, allocate a new widget, initialize it and, if the new file
2275 * was loaded, copy the data to the old widget.
2276 * Return 1 on success, 0 on failure.
2280 edit_reload (WEdit * edit, const char *filename)
2282 WEdit *e;
2283 int y = edit->widget.y;
2284 int x = edit->widget.x;
2285 int lines = edit->widget.lines;
2286 int columns = edit->widget.cols;
2288 e = g_malloc0 (sizeof (WEdit));
2289 e->widget = edit->widget;
2290 if (edit_init (e, y, x, lines, columns, filename, 0) == NULL)
2292 g_free (e);
2293 return 0;
2295 edit_clean (edit);
2296 memcpy (edit, e, sizeof (WEdit));
2297 g_free (e);
2298 return 1;
2301 /* --------------------------------------------------------------------------------------------- */
2303 * Load a new file into the editor and set line. If it fails, preserve the old file.
2304 * To do it, allocate a new widget, initialize it and, if the new file
2305 * was loaded, copy the data to the old widget.
2306 * Return 1 on success, 0 on failure.
2310 edit_reload_line (WEdit * edit, const char *filename, long line)
2312 WEdit *e;
2313 int y = edit->widget.y;
2314 int x = edit->widget.x;
2315 int lines = edit->widget.lines;
2316 int columns = edit->widget.cols;
2318 e = g_malloc0 (sizeof (WEdit));
2319 e->widget = edit->widget;
2320 if (edit_init (e, y, x, lines, columns, filename, line) == NULL)
2322 g_free (e);
2323 return 0;
2325 edit_clean (edit);
2326 memcpy (edit, e, sizeof (WEdit));
2327 g_free (e);
2328 return 1;
2331 /* --------------------------------------------------------------------------------------------- */
2333 void
2334 edit_set_codeset (WEdit * edit)
2336 #ifdef HAVE_CHARSET
2337 const char *cp_id;
2339 cp_id =
2340 get_codepage_id (mc_global.source_codepage >=
2341 0 ? mc_global.source_codepage : mc_global.display_codepage);
2343 if (cp_id != NULL)
2345 GIConv conv;
2346 conv = str_crt_conv_from (cp_id);
2347 if (conv != INVALID_CONV)
2349 if (edit->converter != str_cnv_from_term)
2350 str_close_conv (edit->converter);
2351 edit->converter = conv;
2355 if (cp_id != NULL)
2356 edit->utf8 = str_isutf8 (cp_id);
2357 #else
2358 (void) edit;
2359 #endif
2363 /* --------------------------------------------------------------------------------------------- */
2365 Recording stack for undo:
2366 The following is an implementation of a compressed stack. Identical
2367 pushes are recorded by a negative prefix indicating the number of times the
2368 same char was pushed. This saves space for repeated curs-left or curs-right
2369 delete etc.
2373 pushed: stored:
2377 b -3
2379 c --> -4
2385 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2386 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2387 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2388 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2389 position.
2391 The only way the cursor moves or the buffer is changed is through the routines:
2392 insert, backspace, insert_ahead, delete, and cursor_move.
2393 These record the reverse undo movements onto the stack each time they are
2394 called.
2396 Each key press results in a set of actions (insert; delete ...). So each time
2397 a key is pressed the current position of start_display is pushed as
2398 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2399 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2400 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2404 void
2405 edit_push_undo_action (WEdit * edit, long c, ...)
2407 unsigned long sp = edit->undo_stack_pointer;
2408 unsigned long spm1;
2409 long *t;
2411 /* first enlarge the stack if necessary */
2412 if (sp > edit->undo_stack_size - 10)
2413 { /* say */
2414 if (option_max_undo < 256)
2415 option_max_undo = 256;
2416 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2418 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2419 if (t)
2421 edit->undo_stack = t;
2422 edit->undo_stack_size <<= 1;
2423 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2427 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2428 if (edit->undo_stack_disable)
2430 edit_push_redo_action (edit, KEY_PRESS);
2431 edit_push_redo_action (edit, c);
2432 return;
2434 else if (edit->redo_stack_reset)
2436 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2439 if (edit->undo_stack_bottom != sp
2440 && spm1 != edit->undo_stack_bottom
2441 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2443 int d;
2444 if (edit->undo_stack[spm1] < 0)
2446 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2447 if (d == c)
2449 if (edit->undo_stack[spm1] > -1000000000)
2451 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2453 edit->undo_stack[spm1]--;
2455 return;
2459 else
2461 d = edit->undo_stack[spm1];
2462 if (d == c)
2464 if (c >= KEY_PRESS)
2465 return; /* --> no need to push multiple do-nothings */
2466 edit->undo_stack[sp] = -2;
2467 goto check_bottom;
2471 edit->undo_stack[sp] = c;
2473 check_bottom:
2474 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2476 /* if the sp wraps round and catches the undo_stack_bottom then erase
2477 * the first set of actions on the stack to make space - by moving
2478 * undo_stack_bottom forward one "key press" */
2479 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2480 if ((unsigned long) c == edit->undo_stack_bottom ||
2481 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2484 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2486 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2487 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2489 /*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: */
2490 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2491 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2493 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2497 void
2498 edit_push_redo_action (WEdit * edit, long c, ...)
2500 unsigned long sp = edit->redo_stack_pointer;
2501 unsigned long spm1;
2502 long *t;
2503 /* first enlarge the stack if necessary */
2504 if (sp > edit->redo_stack_size - 10)
2505 { /* say */
2506 if (option_max_undo < 256)
2507 option_max_undo = 256;
2508 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2510 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2511 if (t)
2513 edit->redo_stack = t;
2514 edit->redo_stack_size <<= 1;
2515 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2519 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2521 if (edit->redo_stack_bottom != sp
2522 && spm1 != edit->redo_stack_bottom
2523 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2525 int d;
2526 if (edit->redo_stack[spm1] < 0)
2528 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2529 if (d == c)
2531 if (edit->redo_stack[spm1] > -1000000000)
2533 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2534 edit->redo_stack[spm1]--;
2535 return;
2539 else
2541 d = edit->redo_stack[spm1];
2542 if (d == c)
2544 if (c >= KEY_PRESS)
2545 return; /* --> no need to push multiple do-nothings */
2546 edit->redo_stack[sp] = -2;
2547 goto redo_check_bottom;
2551 edit->redo_stack[sp] = c;
2553 redo_check_bottom:
2554 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2556 /* if the sp wraps round and catches the redo_stack_bottom then erase
2557 * the first set of actions on the stack to make space - by moving
2558 * redo_stack_bottom forward one "key press" */
2559 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2560 if ((unsigned long) c == edit->redo_stack_bottom ||
2561 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2564 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2566 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2567 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2570 * If a single key produced enough pushes to wrap all the way round then
2571 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2572 * The stack is then initialised:
2575 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2576 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2577 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2581 /* --------------------------------------------------------------------------------------------- */
2583 Basic low level single character buffer alterations and movements at the cursor.
2584 Returns char passed over, inserted or removed.
2587 void
2588 edit_insert (WEdit * edit, int c)
2590 /* check if file has grown to large */
2591 if (edit->last_byte >= SIZE_LIMIT)
2592 return;
2594 /* first we must update the position of the display window */
2595 if (edit->curs1 < edit->start_display)
2597 edit->start_display++;
2598 if (c == '\n')
2599 edit->start_line++;
2602 /* Mark file as modified, unless the file hasn't been fully loaded */
2603 if (edit->loading_done)
2605 edit_modification (edit);
2608 /* now we must update some info on the file and check if a redraw is required */
2609 if (c == '\n')
2611 if (edit->book_mark)
2612 book_mark_inc (edit, edit->curs_line);
2613 edit->curs_line++;
2614 edit->total_lines++;
2615 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2618 /* save the reverse command onto the undo stack */
2619 /* ordinary char and not space */
2620 if (c > 32)
2621 edit_push_undo_action (edit, BACKSPACE);
2622 else
2623 edit_push_undo_action (edit, BACKSPACE_BR);
2624 /* update markers */
2625 edit->mark1 += (edit->mark1 > edit->curs1);
2626 edit->mark2 += (edit->mark2 > edit->curs1);
2627 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2629 /* add a new buffer if we've reached the end of the last one */
2630 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2631 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2633 /* perform the insertion */
2634 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2635 = (unsigned char) c;
2637 /* update file length */
2638 edit->last_byte++;
2640 /* update cursor position */
2641 edit->curs1++;
2644 /* --------------------------------------------------------------------------------------------- */
2645 /** same as edit_insert and move left */
2647 void
2648 edit_insert_ahead (WEdit * edit, int c)
2650 if (edit->last_byte >= SIZE_LIMIT)
2651 return;
2653 if (edit->curs1 < edit->start_display)
2655 edit->start_display++;
2656 if (c == '\n')
2657 edit->start_line++;
2659 edit_modification (edit);
2660 if (c == '\n')
2662 if (edit->book_mark)
2663 book_mark_inc (edit, edit->curs_line);
2664 edit->total_lines++;
2665 edit->force |= REDRAW_AFTER_CURSOR;
2667 /* ordinary char and not space */
2668 if (c > 32)
2669 edit_push_undo_action (edit, DELCHAR);
2670 else
2671 edit_push_undo_action (edit, DELCHAR_BR);
2673 edit->mark1 += (edit->mark1 >= edit->curs1);
2674 edit->mark2 += (edit->mark2 >= edit->curs1);
2675 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2677 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2678 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2679 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2680 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2682 edit->last_byte++;
2683 edit->curs2++;
2687 /* --------------------------------------------------------------------------------------------- */
2690 edit_delete (WEdit * edit, const int byte_delete)
2692 int p = 0;
2693 int cw = 1;
2694 int i;
2696 if (!edit->curs2)
2697 return 0;
2699 cw = 1;
2700 /* if byte_delete = 1 then delete only one byte not multibyte char */
2701 if (edit->utf8 && byte_delete == 0)
2703 edit_get_utf (edit, edit->curs1, &cw);
2704 if (cw < 1)
2705 cw = 1;
2708 if (edit->mark2 != edit->mark1)
2709 edit_push_markers (edit);
2711 for (i = 1; i <= cw; i++)
2713 if (edit->mark1 > edit->curs1)
2715 edit->mark1--;
2716 edit->end_mark_curs--;
2718 if (edit->mark2 > edit->curs1)
2719 edit->mark2--;
2720 if (edit->last_get_rule > edit->curs1)
2721 edit->last_get_rule--;
2723 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2724 ((edit->curs2 -
2725 1) & M_EDIT_BUF_SIZE) - 1];
2727 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2729 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2730 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2732 edit->last_byte--;
2733 edit->curs2--;
2734 edit_push_undo_action (edit, p + 256);
2737 edit_modification (edit);
2738 if (p == '\n')
2740 if (edit->book_mark)
2741 book_mark_dec (edit, edit->curs_line);
2742 edit->total_lines--;
2743 edit->force |= REDRAW_AFTER_CURSOR;
2745 if (edit->curs1 < edit->start_display)
2747 edit->start_display--;
2748 if (p == '\n')
2749 edit->start_line--;
2752 return p;
2755 /* --------------------------------------------------------------------------------------------- */
2756 /** moves the cursor right or left: increment positive or negative respectively */
2758 void
2759 edit_cursor_move (WEdit * edit, long increment)
2761 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2762 int c;
2764 if (increment < 0)
2766 for (; increment < 0; increment++)
2768 if (!edit->curs1)
2769 return;
2771 edit_push_undo_action (edit, CURS_RIGHT);
2773 c = edit_get_byte (edit, edit->curs1 - 1);
2774 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2775 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2776 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2777 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2778 edit->curs2++;
2779 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2780 1) & M_EDIT_BUF_SIZE];
2781 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2783 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2784 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2786 edit->curs1--;
2787 if (c == '\n')
2789 edit->curs_line--;
2790 edit->force |= REDRAW_LINE_BELOW;
2795 else if (increment > 0)
2797 for (; increment > 0; increment--)
2799 if (!edit->curs2)
2800 return;
2802 edit_push_undo_action (edit, CURS_LEFT);
2804 c = edit_get_byte (edit, edit->curs1);
2805 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2806 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2807 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2808 edit->curs1++;
2809 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2810 ((edit->curs2 -
2811 1) & M_EDIT_BUF_SIZE) - 1];
2812 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2814 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2815 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2817 edit->curs2--;
2818 if (c == '\n')
2820 edit->curs_line++;
2821 edit->force |= REDRAW_LINE_ABOVE;
2827 /* These functions return positions relative to lines */
2829 /* --------------------------------------------------------------------------------------------- */
2830 /** returns index of last char on line + 1 */
2832 long
2833 edit_eol (WEdit * edit, long current)
2835 if (current >= edit->last_byte)
2836 return edit->last_byte;
2838 for (;; current++)
2839 if (edit_get_byte (edit, current) == '\n')
2840 break;
2841 return current;
2844 /* --------------------------------------------------------------------------------------------- */
2845 /** returns index of first char on line */
2847 long
2848 edit_bol (WEdit * edit, long current)
2850 if (current <= 0)
2851 return 0;
2853 for (;; current--)
2854 if (edit_get_byte (edit, current - 1) == '\n')
2855 break;
2856 return current;
2859 /* --------------------------------------------------------------------------------------------- */
2861 long
2862 edit_count_lines (WEdit * edit, long current, long upto)
2864 long lines = 0;
2865 if (upto > edit->last_byte)
2866 upto = edit->last_byte;
2867 if (current < 0)
2868 current = 0;
2869 while (current < upto)
2870 if (edit_get_byte (edit, current++) == '\n')
2871 lines++;
2872 return lines;
2875 /* --------------------------------------------------------------------------------------------- */
2876 /* If lines is zero this returns the count of lines from current to upto. */
2877 /* If upto is zero returns index of lines forward current. */
2879 long
2880 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2882 if (upto)
2884 return edit_count_lines (edit, current, upto);
2886 else
2888 long next;
2889 if (lines < 0)
2890 lines = 0;
2891 while (lines--)
2893 next = edit_eol (edit, current) + 1;
2894 if (next > edit->last_byte)
2895 break;
2896 else
2897 current = next;
2899 return current;
2903 /* --------------------------------------------------------------------------------------------- */
2904 /** Returns offset of 'lines' lines up from current */
2906 long
2907 edit_move_backward (WEdit * edit, long current, long lines)
2909 if (lines < 0)
2910 lines = 0;
2911 current = edit_bol (edit, current);
2912 while ((lines--) && current != 0)
2913 current = edit_bol (edit, current - 1);
2914 return current;
2917 /* --------------------------------------------------------------------------------------------- */
2918 /* If cols is zero this returns the count of columns from current to upto. */
2919 /* If upto is zero returns index of cols across from current. */
2921 long
2922 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
2924 long p, q;
2925 int col;
2927 if (upto)
2929 q = upto;
2930 cols = -10;
2932 else
2933 q = edit->last_byte + 2;
2935 for (col = 0, p = current; p < q; p++)
2937 int c, orig_c;
2939 if (cols != -10)
2941 if (col == cols)
2942 return p;
2943 if (col > cols)
2944 return p - 1;
2947 orig_c = c = edit_get_byte (edit, p);
2949 #ifdef HAVE_CHARSET
2950 if (edit->utf8)
2952 int utf_ch;
2953 int cw = 1;
2955 utf_ch = edit_get_utf (edit, p, &cw);
2956 if (mc_global.utf8_display)
2958 if (cw > 1)
2959 col -= cw - 1;
2960 if (g_unichar_iswide (utf_ch))
2961 col++;
2963 else if (cw > 1 && g_unichar_isprint (utf_ch))
2964 col -= cw - 1;
2967 c = convert_to_display_c (c);
2968 #endif
2970 if (c == '\t')
2971 col += TAB_SIZE - col % TAB_SIZE;
2972 else if (c == '\n')
2974 if (upto)
2975 return col;
2976 else
2977 return p;
2979 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
2980 /* '\r' is shown as ^M, so we must advance 2 characters */
2981 /* Caret notation for control characters */
2982 col += 2;
2983 else
2984 col++;
2986 return col;
2989 /* --------------------------------------------------------------------------------------------- */
2990 /** returns the current column position of the cursor */
2993 edit_get_col (WEdit * edit)
2995 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
2998 /* --------------------------------------------------------------------------------------------- */
2999 /* Scrolling functions */
3000 /* --------------------------------------------------------------------------------------------- */
3002 void
3003 edit_update_curs_row (WEdit * edit)
3005 edit->curs_row = edit->curs_line - edit->start_line;
3008 /* --------------------------------------------------------------------------------------------- */
3010 void
3011 edit_update_curs_col (WEdit * edit)
3013 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3016 /* --------------------------------------------------------------------------------------------- */
3019 edit_get_curs_col (const WEdit * edit)
3021 return edit->curs_col;
3024 /* --------------------------------------------------------------------------------------------- */
3025 /** moves the display start position up by i lines */
3027 void
3028 edit_scroll_upward (WEdit * edit, unsigned long i)
3030 unsigned long lines_above = edit->start_line;
3031 if (i > lines_above)
3032 i = lines_above;
3033 if (i)
3035 edit->start_line -= i;
3036 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3037 edit->force |= REDRAW_PAGE;
3038 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3040 edit_update_curs_row (edit);
3044 /* --------------------------------------------------------------------------------------------- */
3045 /** returns 1 if could scroll, 0 otherwise */
3047 void
3048 edit_scroll_downward (WEdit * edit, int i)
3050 int lines_below;
3051 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3052 if (lines_below > 0)
3054 if (i > lines_below)
3055 i = lines_below;
3056 edit->start_line += i;
3057 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3058 edit->force |= REDRAW_PAGE;
3059 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3061 edit_update_curs_row (edit);
3064 /* --------------------------------------------------------------------------------------------- */
3066 void
3067 edit_scroll_right (WEdit * edit, int i)
3069 edit->force |= REDRAW_PAGE;
3070 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3071 edit->start_col -= i;
3074 /* --------------------------------------------------------------------------------------------- */
3076 void
3077 edit_scroll_left (WEdit * edit, int i)
3079 if (edit->start_col)
3081 edit->start_col += i;
3082 if (edit->start_col > 0)
3083 edit->start_col = 0;
3084 edit->force |= REDRAW_PAGE;
3085 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3089 /* --------------------------------------------------------------------------------------------- */
3090 /* high level cursor movement commands */
3091 /* --------------------------------------------------------------------------------------------- */
3093 void
3094 edit_move_to_prev_col (WEdit * edit, long p)
3096 int prev = edit->prev_col;
3097 int over = edit->over_col;
3098 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3100 if (option_cursor_beyond_eol)
3102 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3103 edit_eol (edit, edit->curs1));
3105 if (line_len < prev + edit->over_col)
3107 edit->over_col = prev + over - line_len;
3108 edit->prev_col = line_len;
3109 edit->curs_col = line_len;
3111 else
3113 edit->curs_col = prev + over;
3114 edit->prev_col = edit->curs_col;
3115 edit->over_col = 0;
3118 else
3120 edit->over_col = 0;
3121 if (is_in_indent (edit) && option_fake_half_tabs)
3123 edit_update_curs_col (edit);
3124 if (space_width)
3125 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3127 int q = edit->curs_col;
3128 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3129 p = edit_bol (edit, edit->curs1);
3130 edit_cursor_move (edit,
3131 edit_move_forward3 (edit, p, edit->curs_col,
3132 0) - edit->curs1);
3133 if (!left_of_four_spaces (edit))
3134 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3140 /* --------------------------------------------------------------------------------------------- */
3143 line_is_blank (WEdit * edit, long line)
3145 return is_blank (edit, edit_find_line (edit, line));
3148 /* --------------------------------------------------------------------------------------------- */
3149 /** move cursor to line 'line' */
3151 void
3152 edit_move_to_line (WEdit * e, long line)
3154 if (line < e->curs_line)
3155 edit_move_up (e, e->curs_line - line, 0);
3156 else
3157 edit_move_down (e, line - e->curs_line, 0);
3158 edit_scroll_screen_over_cursor (e);
3161 /* --------------------------------------------------------------------------------------------- */
3162 /** scroll window so that first visible line is 'line' */
3164 void
3165 edit_move_display (WEdit * e, long line)
3167 if (line < e->start_line)
3168 edit_scroll_upward (e, e->start_line - line);
3169 else
3170 edit_scroll_downward (e, line - e->start_line);
3173 /* --------------------------------------------------------------------------------------------- */
3174 /** save markers onto undo stack */
3176 void
3177 edit_push_markers (WEdit * edit)
3179 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3180 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3181 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3184 /* --------------------------------------------------------------------------------------------- */
3186 void
3187 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3189 edit->mark1 = m1;
3190 edit->mark2 = m2;
3191 edit->column1 = c1;
3192 edit->column2 = c2;
3196 /* --------------------------------------------------------------------------------------------- */
3197 /** highlight marker toggle */
3199 void
3200 edit_mark_cmd (WEdit * edit, int unmark)
3202 edit_push_markers (edit);
3203 if (unmark)
3205 edit_set_markers (edit, 0, 0, 0, 0);
3206 edit->force |= REDRAW_PAGE;
3208 else
3210 if (edit->mark2 >= 0)
3212 edit->end_mark_curs = -1;
3213 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3214 edit->curs_col + edit->over_col);
3215 edit->force |= REDRAW_PAGE;
3217 else
3219 edit->end_mark_curs = edit->curs1;
3220 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3221 edit->curs_col + edit->over_col);
3226 /* --------------------------------------------------------------------------------------------- */
3227 /** highlight the word under cursor */
3229 void
3230 edit_mark_current_word_cmd (WEdit * edit)
3232 long pos;
3234 for (pos = edit->curs1; pos != 0; pos--)
3236 int c1, c2;
3238 c1 = edit_get_byte (edit, pos);
3239 c2 = edit_get_byte (edit, pos - 1);
3240 if (!isspace (c1) && isspace (c2))
3241 break;
3242 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3243 break;
3245 edit->mark1 = pos;
3247 for (; pos < edit->last_byte; pos++)
3249 int c1, c2;
3251 c1 = edit_get_byte (edit, pos);
3252 c2 = edit_get_byte (edit, pos + 1);
3253 if (!isspace (c1) && isspace (c2))
3254 break;
3255 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3256 break;
3258 edit->mark2 = min (pos + 1, edit->last_byte);
3260 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3263 /* --------------------------------------------------------------------------------------------- */
3265 void
3266 edit_mark_current_line_cmd (WEdit * edit)
3268 long pos = edit->curs1;
3270 edit->mark1 = edit_bol (edit, pos);
3271 edit->mark2 = edit_eol (edit, pos);
3273 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3276 /* --------------------------------------------------------------------------------------------- */
3278 void
3279 edit_delete_line (WEdit * edit)
3282 * Delete right part of the line.
3283 * Note that edit_get_byte() returns '\n' when byte position is
3284 * beyond EOF.
3286 while (edit_get_byte (edit, edit->curs1) != '\n')
3288 (void) edit_delete (edit, 1);
3292 * Delete '\n' char.
3293 * Note that edit_delete() will not corrupt anything if called while
3294 * cursor position is EOF.
3296 (void) edit_delete (edit, 1);
3299 * Delete left part of the line.
3300 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3302 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3304 (void) edit_backspace (edit, 1);
3308 /* --------------------------------------------------------------------------------------------- */
3310 void
3311 insert_spaces_tab (WEdit * edit, int half)
3313 int i;
3314 edit_update_curs_col (edit);
3315 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
3316 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
3317 while (i > 0)
3319 edit_insert (edit, ' ');
3320 i -= space_width;
3324 /* --------------------------------------------------------------------------------------------- */
3327 edit_indent_width (WEdit * edit, long p)
3329 long q = p;
3330 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3331 q++;
3332 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3335 /* --------------------------------------------------------------------------------------------- */
3337 void
3338 edit_insert_indent (WEdit * edit, int indent)
3340 if (!option_fill_tabs_with_spaces)
3342 while (indent >= TAB_SIZE)
3344 edit_insert (edit, '\t');
3345 indent -= TAB_SIZE;
3348 while (indent-- > 0)
3349 edit_insert (edit, ' ');
3352 /* --------------------------------------------------------------------------------------------- */
3354 void
3355 edit_push_key_press (WEdit * edit)
3357 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3358 if (edit->mark2 == -1)
3360 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3361 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3365 /* --------------------------------------------------------------------------------------------- */
3367 void
3368 edit_find_bracket (WEdit * edit)
3370 edit->bracket = edit_get_bracket (edit, 1, 10000);
3371 if (last_bracket != edit->bracket)
3372 edit->force |= REDRAW_PAGE;
3373 last_bracket = edit->bracket;
3376 /* --------------------------------------------------------------------------------------------- */
3378 * This executes a command as though the user initiated it through a key
3379 * press. Callback with WIDGET_KEY as a message calls this after
3380 * translating the key press. This function can be used to pass any
3381 * command to the editor. Note that the screen wouldn't update
3382 * automatically. Either of command or char_for_insertion must be
3383 * passed as -1. Commands are executed, and char_for_insertion is
3384 * inserted at the cursor.
3387 void
3388 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3390 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3391 || (macro_index < 0
3392 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3394 macro_index = 0;
3395 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3396 return;
3398 if (macro_index != -1)
3400 edit->force |= REDRAW_COMPLETELY;
3401 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3403 edit_store_macro_cmd (edit);
3404 macro_index = -1;
3405 return;
3407 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3409 edit_repeat_macro_cmd (edit);
3410 macro_index = -1;
3411 return;
3415 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3417 record_macro_buf[macro_index].action = command;
3418 record_macro_buf[macro_index++].ch = char_for_insertion;
3420 /* record the beginning of a set of editing actions initiated by a key press */
3421 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3422 edit_push_key_press (edit);
3424 edit_execute_cmd (edit, command, char_for_insertion);
3425 if (edit->column_highlight)
3426 edit->force |= REDRAW_PAGE;
3429 /* --------------------------------------------------------------------------------------------- */
3431 This executes a command at a lower level than macro recording.
3432 It also does not push a key_press onto the undo stack. This means
3433 that if it is called many times, a single undo command will undo
3434 all of them. It also does not check for the Undo command.
3436 void
3437 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3439 edit->force |= REDRAW_LINE;
3441 /* The next key press will unhighlight the found string, so update
3442 * the whole page */
3443 if (edit->found_len || edit->column_highlight)
3444 edit->force |= REDRAW_PAGE;
3446 switch (command)
3448 /* a mark command with shift-arrow */
3449 case CK_MarkLeft:
3450 case CK_MarkRight:
3451 case CK_MarkToWordBegin:
3452 case CK_MarkToWordEnd:
3453 case CK_MarkToHome:
3454 case CK_MarkToEnd:
3455 case CK_MarkUp:
3456 case CK_MarkDown:
3457 case CK_MarkPageUp:
3458 case CK_MarkPageDown:
3459 case CK_MarkToFileBegin:
3460 case CK_MarkToFileEnd:
3461 case CK_MarkToPageBegin:
3462 case CK_MarkToPageEnd:
3463 case CK_MarkScrollUp:
3464 case CK_MarkScrollDown:
3465 case CK_MarkParagraphUp:
3466 case CK_MarkParagraphDown:
3467 /* a mark command with alt-arrow */
3468 case CK_MarkColumnPageUp:
3469 case CK_MarkColumnPageDown:
3470 case CK_MarkColumnLeft:
3471 case CK_MarkColumnRight:
3472 case CK_MarkColumnUp:
3473 case CK_MarkColumnDown:
3474 case CK_MarkColumnScrollUp:
3475 case CK_MarkColumnScrollDown:
3476 case CK_MarkColumnParagraphUp:
3477 case CK_MarkColumnParagraphDown:
3478 edit->column_highlight = 0;
3479 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3481 edit_mark_cmd (edit, 1); /* clear */
3482 edit_mark_cmd (edit, 0); /* marking on */
3484 edit->highlight = 1;
3485 break;
3487 /* any other command */
3488 default:
3489 if (edit->highlight)
3490 edit_mark_cmd (edit, 0); /* clear */
3491 edit->highlight = 0;
3494 /* first check for undo */
3495 if (command == CK_Undo)
3497 edit->redo_stack_reset = 0;
3498 edit_group_undo (edit);
3499 edit->found_len = 0;
3500 edit->prev_col = edit_get_col (edit);
3501 edit->search_start = edit->curs1;
3502 return;
3504 /* check for redo */
3505 if (command == CK_Redo)
3507 edit->redo_stack_reset = 0;
3508 edit_do_redo (edit);
3509 edit->found_len = 0;
3510 edit->prev_col = edit_get_col (edit);
3511 edit->search_start = edit->curs1;
3512 return;
3515 edit->redo_stack_reset = 1;
3517 /* An ordinary key press */
3518 if (char_for_insertion >= 0)
3520 /* if non persistent selection and text selected */
3521 if (!option_persistent_selections)
3523 if (edit->mark1 != edit->mark2)
3524 edit_block_delete_cmd (edit);
3526 if (edit->overwrite)
3528 /* remove char only one time, after input first byte, multibyte chars */
3529 if ((!mc_global.utf8_display || edit->charpoint == 0)
3530 && edit_get_byte (edit, edit->curs1) != '\n')
3531 edit_delete (edit, 0);
3533 if (option_cursor_beyond_eol && edit->over_col > 0)
3534 edit_insert_over (edit);
3535 #ifdef HAVE_CHARSET
3536 if (char_for_insertion > 255 && mc_global.utf8_display == 0)
3538 unsigned char str[6 + 1];
3539 size_t i = 0;
3540 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3541 if (res == 0)
3543 str[0] = '.';
3544 str[1] = '\0';
3546 else
3548 str[res] = '\0';
3550 while (str[i] != 0 && i <= 6)
3552 char_for_insertion = str[i];
3553 edit_insert (edit, char_for_insertion);
3554 i++;
3557 else
3558 #endif
3559 edit_insert (edit, char_for_insertion);
3561 if (option_auto_para_formatting)
3563 format_paragraph (edit, 0);
3564 edit->force |= REDRAW_PAGE;
3566 else
3567 check_and_wrap_line (edit);
3568 edit->found_len = 0;
3569 edit->prev_col = edit_get_col (edit);
3570 edit->search_start = edit->curs1;
3571 edit_find_bracket (edit);
3572 return;
3575 switch (command)
3577 case CK_TopOnScreen:
3578 case CK_BottomOnScreen:
3579 case CK_MarkToPageBegin:
3580 case CK_MarkToPageEnd:
3581 case CK_Up:
3582 case CK_Down:
3583 case CK_Left:
3584 case CK_Right:
3585 case CK_WordLeft:
3586 case CK_WordRight:
3587 if (edit->mark2 >= 0)
3589 if (!option_persistent_selections)
3591 if (edit->column_highlight)
3592 edit_push_undo_action (edit, COLUMN_ON);
3593 edit->column_highlight = 0;
3594 edit_mark_cmd (edit, 1);
3599 switch (command)
3601 case CK_TopOnScreen:
3602 case CK_BottomOnScreen:
3603 case CK_MarkToPageBegin:
3604 case CK_MarkToPageEnd:
3605 case CK_Up:
3606 case CK_Down:
3607 case CK_WordLeft:
3608 case CK_WordRight:
3609 case CK_MarkToWordBegin:
3610 case CK_MarkToWordEnd:
3611 case CK_MarkUp:
3612 case CK_MarkDown:
3613 case CK_MarkColumnUp:
3614 case CK_MarkColumnDown:
3615 if (edit->mark2 == -1)
3616 break; /*marking is following the cursor: may need to highlight a whole line */
3617 case CK_Left:
3618 case CK_Right:
3619 case CK_MarkLeft:
3620 case CK_MarkRight:
3621 edit->force |= REDRAW_CHAR_ONLY;
3624 /* basic cursor key commands */
3625 switch (command)
3627 case CK_BackSpace:
3628 /* if non persistent selection and text selected */
3629 if (!option_persistent_selections)
3631 if (edit->mark1 != edit->mark2)
3633 edit_block_delete_cmd (edit);
3634 break;
3637 if (option_cursor_beyond_eol && edit->over_col > 0)
3639 edit->over_col--;
3640 break;
3642 if (option_backspace_through_tabs && is_in_indent (edit))
3644 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3645 edit_backspace (edit, 1);
3646 break;
3648 else
3650 if (option_fake_half_tabs)
3652 int i;
3653 if (is_in_indent (edit) && right_of_four_spaces (edit))
3655 for (i = 0; i < HALF_TAB_SIZE; i++)
3656 edit_backspace (edit, 1);
3657 break;
3661 edit_backspace (edit, 0);
3662 break;
3663 case CK_Delete:
3664 /* if non persistent selection and text selected */
3665 if (!option_persistent_selections)
3667 if (edit->mark1 != edit->mark2)
3669 edit_block_delete_cmd (edit);
3670 break;
3674 if (option_cursor_beyond_eol && edit->over_col > 0)
3675 edit_insert_over (edit);
3677 if (option_fake_half_tabs)
3679 int i;
3680 if (is_in_indent (edit) && left_of_four_spaces (edit))
3682 for (i = 1; i <= HALF_TAB_SIZE; i++)
3683 edit_delete (edit, 1);
3684 break;
3687 edit_delete (edit, 0);
3688 break;
3689 case CK_DeleteToWordBegin:
3690 edit->over_col = 0;
3691 edit_left_delete_word (edit);
3692 break;
3693 case CK_DeleteToWordEnd:
3694 if (option_cursor_beyond_eol && edit->over_col > 0)
3695 edit_insert_over (edit);
3697 edit_right_delete_word (edit);
3698 break;
3699 case CK_DeleteLine:
3700 edit_delete_line (edit);
3701 break;
3702 case CK_DeleteToHome:
3703 edit_delete_to_line_begin (edit);
3704 break;
3705 case CK_DeleteToEnd:
3706 edit_delete_to_line_end (edit);
3707 break;
3708 case CK_Enter:
3709 edit->over_col = 0;
3710 if (option_auto_para_formatting)
3712 edit_double_newline (edit);
3713 if (option_return_does_auto_indent)
3714 edit_auto_indent (edit);
3715 format_paragraph (edit, 0);
3717 else
3719 edit_insert (edit, '\n');
3720 if (option_return_does_auto_indent)
3722 edit_auto_indent (edit);
3725 break;
3726 case CK_Return:
3727 edit_insert (edit, '\n');
3728 break;
3730 case CK_MarkColumnPageUp:
3731 edit->column_highlight = 1;
3732 case CK_PageUp:
3733 case CK_MarkPageUp:
3734 edit_move_up (edit, edit->widget.lines - 1, 1);
3735 break;
3736 case CK_MarkColumnPageDown:
3737 edit->column_highlight = 1;
3738 case CK_PageDown:
3739 case CK_MarkPageDown:
3740 edit_move_down (edit, edit->widget.lines - 1, 1);
3741 break;
3742 case CK_MarkColumnLeft:
3743 edit->column_highlight = 1;
3744 case CK_Left:
3745 case CK_MarkLeft:
3746 if (option_fake_half_tabs)
3748 if (is_in_indent (edit) && right_of_four_spaces (edit))
3750 if (option_cursor_beyond_eol && edit->over_col > 0)
3751 edit->over_col--;
3752 else
3753 edit_cursor_move (edit, -HALF_TAB_SIZE);
3754 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3755 break;
3758 edit_left_char_move_cmd (edit);
3759 break;
3760 case CK_MarkColumnRight:
3761 edit->column_highlight = 1;
3762 case CK_Right:
3763 case CK_MarkRight:
3764 if (option_fake_half_tabs)
3766 if (is_in_indent (edit) && left_of_four_spaces (edit))
3768 edit_cursor_move (edit, HALF_TAB_SIZE);
3769 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3770 break;
3773 edit_right_char_move_cmd (edit);
3774 break;
3775 case CK_TopOnScreen:
3776 case CK_MarkToPageBegin:
3777 edit_begin_page (edit);
3778 break;
3779 case CK_BottomOnScreen:
3780 case CK_MarkToPageEnd:
3781 edit_end_page (edit);
3782 break;
3783 case CK_WordLeft:
3784 case CK_MarkToWordBegin:
3785 edit->over_col = 0;
3786 edit_left_word_move_cmd (edit);
3787 break;
3788 case CK_WordRight:
3789 case CK_MarkToWordEnd:
3790 edit->over_col = 0;
3791 edit_right_word_move_cmd (edit);
3792 break;
3793 case CK_MarkColumnUp:
3794 edit->column_highlight = 1;
3795 case CK_Up:
3796 case CK_MarkUp:
3797 edit_move_up (edit, 1, 0);
3798 break;
3799 case CK_MarkColumnDown:
3800 edit->column_highlight = 1;
3801 case CK_Down:
3802 case CK_MarkDown:
3803 edit_move_down (edit, 1, 0);
3804 break;
3805 case CK_MarkColumnParagraphUp:
3806 edit->column_highlight = 1;
3807 case CK_ParagraphUp:
3808 case CK_MarkParagraphUp:
3809 edit_move_up_paragraph (edit, 0);
3810 break;
3811 case CK_MarkColumnParagraphDown:
3812 edit->column_highlight = 1;
3813 case CK_ParagraphDown:
3814 case CK_MarkParagraphDown:
3815 edit_move_down_paragraph (edit, 0);
3816 break;
3817 case CK_MarkColumnScrollUp:
3818 edit->column_highlight = 1;
3819 case CK_ScrollUp:
3820 case CK_MarkScrollUp:
3821 edit_move_up (edit, 1, 1);
3822 break;
3823 case CK_MarkColumnScrollDown:
3824 edit->column_highlight = 1;
3825 case CK_ScrollDown:
3826 case CK_MarkScrollDown:
3827 edit_move_down (edit, 1, 1);
3828 break;
3829 case CK_Home:
3830 case CK_MarkToHome:
3831 edit_cursor_to_bol (edit);
3832 break;
3833 case CK_End:
3834 case CK_MarkToEnd:
3835 edit_cursor_to_eol (edit);
3836 break;
3837 case CK_Tab:
3838 /* if text marked shift block */
3839 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3841 if (edit->mark2 < 0)
3842 edit_mark_cmd (edit, 0);
3843 edit_move_block_to_right (edit);
3845 else
3847 if (option_cursor_beyond_eol)
3848 edit_insert_over (edit);
3849 edit_tab_cmd (edit);
3850 if (option_auto_para_formatting)
3852 format_paragraph (edit, 0);
3853 edit->force |= REDRAW_PAGE;
3855 else
3857 check_and_wrap_line (edit);
3860 break;
3862 case CK_InsertOverwrite:
3863 edit->overwrite = !edit->overwrite;
3864 break;
3866 case CK_Mark:
3867 if (edit->mark2 >= 0)
3869 if (edit->column_highlight)
3870 edit_push_undo_action (edit, COLUMN_ON);
3871 edit->column_highlight = 0;
3873 edit_mark_cmd (edit, 0);
3874 break;
3875 case CK_MarkColumn:
3876 if (!edit->column_highlight)
3877 edit_push_undo_action (edit, COLUMN_OFF);
3878 edit->column_highlight = 1;
3879 edit_mark_cmd (edit, 0);
3880 break;
3881 case CK_MarkAll:
3882 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3883 edit->force |= REDRAW_PAGE;
3884 break;
3885 case CK_Unmark:
3886 if (edit->column_highlight)
3887 edit_push_undo_action (edit, COLUMN_ON);
3888 edit->column_highlight = 0;
3889 edit_mark_cmd (edit, 1);
3890 break;
3891 case CK_MarkWord:
3892 if (edit->column_highlight)
3893 edit_push_undo_action (edit, COLUMN_ON);
3894 edit->column_highlight = 0;
3895 edit_mark_current_word_cmd (edit);
3896 break;
3897 case CK_MarkLine:
3898 if (edit->column_highlight)
3899 edit_push_undo_action (edit, COLUMN_ON);
3900 edit->column_highlight = 0;
3901 edit_mark_current_line_cmd (edit);
3902 break;
3904 case CK_ShowNumbers:
3905 option_line_state = !option_line_state;
3906 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
3907 edit->force |= REDRAW_PAGE;
3908 break;
3910 case CK_ShowMargin:
3911 show_right_margin = !show_right_margin;
3912 edit->force |= REDRAW_PAGE;
3913 break;
3915 case CK_Bookmark:
3916 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3917 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3918 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3919 else
3920 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3921 break;
3922 case CK_BookmarkFlush:
3923 book_mark_flush (edit, BOOK_MARK_COLOR);
3924 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3925 edit->force |= REDRAW_PAGE;
3926 break;
3927 case CK_BookmarkNext:
3928 if (edit->book_mark)
3930 struct _book_mark *p;
3931 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3932 if (p->next)
3934 p = p->next;
3935 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
3936 edit_move_display (edit, p->line - edit->widget.lines / 2);
3937 edit_move_to_line (edit, p->line);
3940 break;
3941 case CK_BookmarkPrev:
3942 if (edit->book_mark)
3944 struct _book_mark *p;
3945 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3946 while (p->line == edit->curs_line)
3947 if (p->prev)
3948 p = p->prev;
3949 if (p->line >= 0)
3951 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
3952 edit_move_display (edit, p->line - edit->widget.lines / 2);
3953 edit_move_to_line (edit, p->line);
3956 break;
3958 case CK_Top:
3959 case CK_MarkToFileBegin:
3960 edit_move_to_top (edit);
3961 break;
3962 case CK_Bottom:
3963 case CK_MarkToFileEnd:
3964 edit_move_to_bottom (edit);
3965 break;
3967 case CK_Copy:
3968 if (option_cursor_beyond_eol && edit->over_col > 0)
3969 edit_insert_over (edit);
3970 edit_block_copy_cmd (edit);
3971 break;
3972 case CK_Remove:
3973 edit_block_delete_cmd (edit);
3974 break;
3975 case CK_Move:
3976 edit_block_move_cmd (edit);
3977 break;
3979 case CK_BlockShiftLeft:
3980 if (edit->mark1 != edit->mark2)
3981 edit_move_block_to_left (edit);
3982 break;
3983 case CK_BlockShiftRight:
3984 if (edit->mark1 != edit->mark2)
3985 edit_move_block_to_right (edit);
3986 break;
3987 case CK_Store:
3988 edit_copy_to_X_buf_cmd (edit);
3989 break;
3990 case CK_Cut:
3991 edit_cut_to_X_buf_cmd (edit);
3992 break;
3993 case CK_Paste:
3994 /* if non persistent selection and text selected */
3995 if (!option_persistent_selections)
3997 if (edit->mark1 != edit->mark2)
3998 edit_block_delete_cmd (edit);
4000 if (option_cursor_beyond_eol && edit->over_col > 0)
4001 edit_insert_over (edit);
4002 edit_paste_from_X_buf_cmd (edit);
4003 break;
4004 case CK_History:
4005 edit_paste_from_history (edit);
4006 break;
4008 case CK_SaveAs:
4009 edit_save_as_cmd (edit);
4010 break;
4011 case CK_Save:
4012 edit_save_confirm_cmd (edit);
4013 break;
4014 case CK_EditFile:
4015 edit_load_cmd (edit, EDIT_FILE_COMMON);
4016 break;
4017 case CK_BlockSave:
4018 edit_save_block_cmd (edit);
4019 break;
4020 case CK_InsertFile:
4021 edit_insert_file_cmd (edit);
4022 break;
4024 case CK_FilePrev:
4025 edit_load_back_cmd (edit);
4026 break;
4027 case CK_FileNext:
4028 edit_load_forward_cmd (edit);
4029 break;
4031 case CK_EditSyntaxFile:
4032 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4033 break;
4034 case CK_SyntaxChoose:
4035 edit_syntax_dialog (edit);
4036 break;
4038 case CK_EditUserMenu:
4039 edit_load_cmd (edit, EDIT_FILE_MENU);
4040 break;
4042 case CK_SyntaxOnOff:
4043 option_syntax_highlighting ^= 1;
4044 if (option_syntax_highlighting == 1)
4045 edit_load_syntax (edit, NULL, edit->syntax_type);
4046 edit->force |= REDRAW_PAGE;
4047 break;
4049 case CK_ShowTabTws:
4050 enable_show_tabs_tws ^= 1;
4051 edit->force |= REDRAW_PAGE;
4052 break;
4054 case CK_Search:
4055 edit_search_cmd (edit, FALSE);
4056 break;
4057 case CK_SearchContinue:
4058 edit_search_cmd (edit, TRUE);
4059 break;
4060 case CK_Replace:
4061 edit_replace_cmd (edit, 0);
4062 break;
4063 case CK_ReplaceContinue:
4064 edit_replace_cmd (edit, 1);
4065 break;
4066 case CK_Complete:
4067 /* if text marked shift block */
4068 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4070 edit_move_block_to_left (edit);
4072 else
4074 edit_complete_word_cmd (edit);
4076 break;
4077 case CK_Find:
4078 edit_get_match_keyword_cmd (edit);
4079 break;
4080 case CK_Quit:
4081 dlg_stop (edit->widget.owner);
4082 break;
4083 case CK_EditNew:
4084 edit_new_cmd (edit);
4085 break;
4086 case CK_Help:
4087 edit_help_cmd (edit);
4088 break;
4089 case CK_Refresh:
4090 edit_refresh_cmd (edit);
4091 break;
4092 case CK_SaveSetup:
4093 save_setup_cmd ();
4094 break;
4095 case CK_About:
4096 edit_about ();
4097 break;
4098 case CK_LearnKeys:
4099 learn_keys ();
4100 break;
4101 case CK_Options:
4102 edit_options_dialog (edit);
4103 break;
4104 case CK_OptionsSaveMode:
4105 menu_save_mode_cmd ();
4106 break;
4107 case CK_Date:
4109 char s[BUF_MEDIUM];
4110 /* fool gcc to prevent a Y2K warning */
4111 char time_format[] = "_c";
4112 time_format[0] = '%';
4114 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4115 edit_print_string (edit, s);
4116 edit->force |= REDRAW_PAGE;
4117 break;
4119 break;
4120 case CK_Goto:
4121 edit_goto_cmd (edit);
4122 break;
4123 case CK_ParagraphFormat:
4124 format_paragraph (edit, 1);
4125 edit->force |= REDRAW_PAGE;
4126 break;
4127 case CK_MacroDelete:
4128 edit_delete_macro_cmd (edit);
4129 break;
4130 case CK_MatchBracket:
4131 edit_goto_matching_bracket (edit);
4132 break;
4133 case CK_UserMenu:
4134 user_menu (edit, NULL, -1);
4135 break;
4136 case CK_Sort:
4137 edit_sort_cmd (edit);
4138 break;
4139 case CK_ExternalCommand:
4140 edit_ext_cmd (edit);
4141 break;
4142 case CK_Mail:
4143 edit_mail_dialog (edit);
4144 break;
4145 case CK_Shell:
4146 view_other_cmd ();
4147 break;
4148 #ifdef HAVE_CHARSET
4149 case CK_SelectCodepage:
4150 edit_select_codepage_cmd (edit);
4151 break;
4152 #endif
4153 case CK_InsertLiteral:
4154 edit_insert_literal_cmd (edit);
4155 break;
4156 case CK_MacroStartStopRecord:
4157 edit_begin_end_macro_cmd (edit);
4158 break;
4159 case CK_RepeatStartStopRecord:
4160 edit_begin_end_repeat_cmd (edit);
4161 break;
4162 case CK_ExtendedKeyMap:
4163 edit->extmod = TRUE;
4164 break;
4165 default:
4166 break;
4169 /* CK_PipeBlock */
4170 if ((command / CK_PipeBlock (0)) == 1)
4171 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4173 /* keys which must set the col position, and the search vars */
4174 switch (command)
4176 case CK_Search:
4177 case CK_SearchContinue:
4178 case CK_Replace:
4179 case CK_ReplaceContinue:
4180 case CK_Complete:
4181 edit->prev_col = edit_get_col (edit);
4182 break;
4183 case CK_Up:
4184 case CK_MarkUp:
4185 case CK_MarkColumnUp:
4186 case CK_Down:
4187 case CK_MarkDown:
4188 case CK_MarkColumnDown:
4189 case CK_PageUp:
4190 case CK_MarkPageUp:
4191 case CK_MarkColumnPageUp:
4192 case CK_PageDown:
4193 case CK_MarkPageDown:
4194 case CK_MarkColumnPageDown:
4195 case CK_Top:
4196 case CK_MarkToFileBegin:
4197 case CK_Bottom:
4198 case CK_MarkToFileEnd:
4199 case CK_ParagraphUp:
4200 case CK_MarkParagraphUp:
4201 case CK_MarkColumnParagraphUp:
4202 case CK_ParagraphDown:
4203 case CK_MarkParagraphDown:
4204 case CK_MarkColumnParagraphDown:
4205 case CK_ScrollUp:
4206 case CK_MarkScrollUp:
4207 case CK_MarkColumnScrollUp:
4208 case CK_ScrollDown:
4209 case CK_MarkScrollDown:
4210 case CK_MarkColumnScrollDown:
4211 edit->search_start = edit->curs1;
4212 edit->found_len = 0;
4213 break;
4214 default:
4215 edit->found_len = 0;
4216 edit->prev_col = edit_get_col (edit);
4217 edit->search_start = edit->curs1;
4219 edit_find_bracket (edit);
4221 if (option_auto_para_formatting)
4223 switch (command)
4225 case CK_BackSpace:
4226 case CK_Delete:
4227 case CK_DeleteToWordBegin:
4228 case CK_DeleteToWordEnd:
4229 case CK_DeleteToHome:
4230 case CK_DeleteToEnd:
4231 format_paragraph (edit, 0);
4232 edit->force |= REDRAW_PAGE;
4237 /* --------------------------------------------------------------------------------------------- */
4239 void
4240 edit_stack_init (void)
4242 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4244 edit_history_moveto[edit_stack_iterator].filename = NULL;
4245 edit_history_moveto[edit_stack_iterator].line = -1;
4248 edit_stack_iterator = 0;
4251 /* --------------------------------------------------------------------------------------------- */
4253 void
4254 edit_stack_free (void)
4256 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4257 g_free (edit_history_moveto[edit_stack_iterator].filename);
4260 /* --------------------------------------------------------------------------------------------- */
4261 /** move i lines */
4263 void
4264 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4266 edit_move_updown (edit, i, do_scroll, TRUE);
4269 /* --------------------------------------------------------------------------------------------- */
4270 /** move i lines */
4272 void
4273 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4275 edit_move_updown (edit, i, do_scroll, FALSE);
4278 /* --------------------------------------------------------------------------------------------- */
4280 unsigned int
4281 edit_unlock_file (WEdit * edit)
4283 char *fullpath;
4284 unsigned int ret;
4286 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4287 ret = unlock_file (fullpath);
4288 g_free (fullpath);
4290 return ret;
4293 /* --------------------------------------------------------------------------------------------- */
4295 unsigned int
4296 edit_lock_file (WEdit * edit)
4298 char *fullpath;
4299 unsigned int ret;
4301 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4302 ret = lock_file (fullpath);
4303 g_free (fullpath);
4305 return ret;
4308 /* --------------------------------------------------------------------------------------------- */