fixup
[midnight-commander.git] / src / editor / edit.c
blobb5298cd28cb5ed3477f571c80295bfd4c9ce0976
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor low level data handling and cursor fundamentals
30 * \author Paul Sheer
31 * \date 1996, 1997
34 #include <config.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
44 #include <fcntl.h>
46 #include "lib/global.h"
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "lib/lock.h"
57 #include "lib/widget.h"
59 #ifdef HAVE_CHARSET
60 #include "lib/charsets.h" /* get_codepage_id */
61 #endif
63 #include "src/filemanager/cmd.h" /* view_other_cmd() */
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "edit-widget.h"
74 /*** global variables ****************************************************************************/
76 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
77 int option_typewriter_wrap = 0;
78 int option_auto_para_formatting = 0;
79 int option_fill_tabs_with_spaces = 0;
80 int option_return_does_auto_indent = 1;
81 int option_backspace_through_tabs = 0;
82 int option_fake_half_tabs = 1;
83 int option_save_mode = EDIT_QUICK_SAVE;
84 int option_save_position = 1;
85 int option_max_undo = 32768;
86 int option_persistent_selections = 1;
87 int option_cursor_beyond_eol = 0;
88 int option_line_state = 0;
89 int option_line_state_width = 0;
91 int option_edit_right_extreme = 0;
92 int option_edit_left_extreme = 0;
93 int option_edit_top_extreme = 0;
94 int option_edit_bottom_extreme = 0;
95 int enable_show_tabs_tws = 1;
96 int option_check_nl_at_eof = 0;
97 int option_group_undo = 0;
98 int show_right_margin = 0;
100 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
101 char *option_backup_ext = NULL;
103 int edit_stack_iterator = 0;
104 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
105 /* magic sequense for say than block is vertical */
106 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
108 /*** file scope macro definitions ****************************************************************/
110 #define TEMP_BUF_LEN 1024
112 #define space_width 1
114 /*** file scope type declarations ****************************************************************/
116 /*** file scope variables ************************************************************************/
118 /* detecting an error on save is easy: just check if every byte has been written. */
119 /* detecting an error on read, is not so easy 'cos there is not way to tell
120 whether you read everything or not. */
121 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
122 static const struct edit_filters
124 const char *read, *write, *extension;
125 } all_filters[] =
127 /* *INDENT-OFF* */
128 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
129 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
130 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
133 /* *INDENT-ON* */
136 static long last_bracket = -1;
138 /*** file scope functions ************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
143 * here's a quick sketch of the layout: (don't run this through indent.)
145 * (b1 is buffers1 and b2 is buffers2)
148 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
149 * ______________________________________|______________________________________
151 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
152 * |-> |-> |-> |-> |-> |-> |
154 * _<------------------------->|<----------------->_
155 * WEdit->curs2 | WEdit->curs1
156 * ^ | ^
157 * | ^|^ |
158 * cursor ||| cursor
159 * |||
160 * file end|||file beginning
165 * This_is_some_file
166 * fin.
169 /* --------------------------------------------------------------------------------------------- */
171 static int left_of_four_spaces (WEdit * edit);
173 /* --------------------------------------------------------------------------------------------- */
175 static void
176 edit_about (void)
178 const char *header = N_("About");
179 const char *button_name = N_("&OK");
180 const char *const version = "MCEdit " VERSION;
181 char text[BUF_LARGE];
183 int win_len, version_len, button_len;
184 int cols, lines;
186 Dlg_head *about_dlg;
188 #ifdef ENABLE_NLS
189 header = _(header);
190 button_name = _(button_name);
191 #endif
193 button_len = str_term_width1 (button_name) + 5;
194 version_len = str_term_width1 (version);
196 g_snprintf (text, sizeof (text),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len = str_term_width1 (header);
202 win_len = max (win_len, version_len);
203 win_len = max (win_len, button_len);
205 /* count width and height of text */
206 str_msg_term_size (text, &lines, &cols);
207 lines += 9;
208 cols = max (win_len, cols) + 6;
210 /* dialog */
211 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
212 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
214 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
215 add_widget (about_dlg, label_new (5, 3, text));
216 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
217 B_ENTER, NORMAL_BUTTON, button_name, NULL));
219 run_dlg (about_dlg);
220 destroy_dlg (about_dlg);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
228 static void
229 edit_init_buffers (WEdit * edit)
231 int j;
233 for (j = 0; j <= MAXBUFF; j++)
235 edit->buffers1[j] = NULL;
236 edit->buffers2[j] = NULL;
239 edit->curs1 = 0;
240 edit->curs2 = 0;
241 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
250 static int
251 edit_load_file_fast (WEdit * edit, const char *filename)
253 long buf, buf2;
254 int file = -1;
255 int ret = 1;
257 edit->curs2 = edit->last_byte;
258 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
260 file = mc_open (filename, O_RDONLY | O_BINARY);
261 if (file == -1)
263 gchar *errmsg;
265 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
266 edit_error_dialog (_("Error"), errmsg);
267 g_free (errmsg);
268 return 1;
271 if (!edit->buffers2[buf2])
272 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
276 if (mc_read (file,
277 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
278 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
279 break;
281 for (buf = buf2 - 1; buf >= 0; buf--)
283 /* edit->buffers2[0] is already allocated */
284 if (!edit->buffers2[buf])
285 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
286 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
287 break;
289 ret = 0;
291 while (0);
292 if (ret)
294 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
295 edit_error_dialog (_("Error"), err_str);
296 g_free (err_str);
298 mc_close (file);
299 return ret;
302 /* --------------------------------------------------------------------------------------------- */
303 /** Return index of the filter or -1 is there is no appropriate filter */
305 static int
306 edit_find_filter (const char *filename)
308 size_t i, l, e;
310 if (filename == NULL)
311 return -1;
313 l = strlen (filename);
314 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
316 e = strlen (all_filters[i].extension);
317 if (l > e)
318 if (!strcmp (all_filters[i].extension, filename + l - e))
319 return i;
321 return -1;
324 /* --------------------------------------------------------------------------------------------- */
326 static char *
327 edit_get_filter (const char *filename)
329 int i;
330 char *p, *quoted_name;
332 i = edit_find_filter (filename);
333 if (i < 0)
334 return NULL;
336 quoted_name = name_quote (filename, 0);
337 p = g_strdup_printf (all_filters[i].read, quoted_name);
338 g_free (quoted_name);
339 return p;
342 /* --------------------------------------------------------------------------------------------- */
344 static long
345 edit_insert_stream (WEdit * edit, FILE * f)
347 int c;
348 long i = 0;
349 while ((c = fgetc (f)) >= 0)
351 edit_insert (edit, c);
352 i++;
354 return i;
357 /* --------------------------------------------------------------------------------------------- */
358 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
360 static int
361 check_file_access (WEdit * edit, const char *filename, struct stat *st)
363 int file;
364 gchar *errmsg = NULL;
366 /* Try opening an existing file */
367 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
369 if (file < 0)
372 * Try creating the file. O_EXCL prevents following broken links
373 * and opening existing files.
375 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
376 if (file < 0)
378 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
379 goto cleanup;
381 else
383 /* New file, delete it if it's not modified or saved */
384 edit->delete_file = 1;
388 /* Check what we have opened */
389 if (mc_fstat (file, st) < 0)
391 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
392 goto cleanup;
395 /* We want to open regular files only */
396 if (!S_ISREG (st->st_mode))
398 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
399 goto cleanup;
403 * Don't delete non-empty files.
404 * O_EXCL should prevent it, but let's be on the safe side.
406 if (st->st_size > 0)
407 edit->delete_file = 0;
409 if (st->st_size >= SIZE_LIMIT)
410 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
412 cleanup:
413 (void) mc_close (file);
415 if (errmsg != NULL)
417 edit_error_dialog (_("Error"), errmsg);
418 g_free (errmsg);
419 return 1;
421 return 0;
424 /* --------------------------------------------------------------------------------------------- */
426 * Open the file and load it into the buffers, either directly or using
427 * a filter. Return 0 on success, 1 on error.
429 * Fast loading (edit_load_file_fast) is used when the file size is
430 * known. In this case the data is read into the buffers by blocks.
431 * If the file size is not known, the data is loaded byte by byte in
432 * edit_insert_file.
435 static int
436 edit_load_file (WEdit * edit)
438 int fast_load = 1;
439 vfs_path_t *vpath = vfs_path_from_str (edit->filename);
441 /* Cannot do fast load if a filter is used */
442 if (edit_find_filter (edit->filename) >= 0)
443 fast_load = 0;
446 * VFS may report file size incorrectly, and slow load is not a big
447 * deal considering overhead in VFS.
449 if (!vfs_file_is_local (vpath))
450 fast_load = 0;
451 vfs_path_free (vpath);
454 * FIXME: line end translation should disable fast loading as well
455 * Consider doing fseek() to the end and ftell() for the real size.
458 if (*edit->filename)
460 /* If we are dealing with a real file, check that it exists */
461 if (check_file_access (edit, edit->filename, &edit->stat1))
462 return 1;
464 else
466 /* nothing to load */
467 fast_load = 0;
470 edit_init_buffers (edit);
472 if (fast_load)
474 edit->last_byte = edit->stat1.st_size;
475 edit_load_file_fast (edit, edit->filename);
476 /* If fast load was used, the number of lines wasn't calculated */
477 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
479 else
481 edit->last_byte = 0;
482 if (*edit->filename)
484 edit->undo_stack_disable = 1;
485 if (edit_insert_file (edit, edit->filename) < 0)
487 edit_clean (edit);
488 return 1;
490 edit->undo_stack_disable = 0;
493 edit->lb = LB_ASIS;
494 return 0;
497 /* --------------------------------------------------------------------------------------------- */
498 /** Restore saved cursor position in the file */
500 static void
501 edit_load_position (WEdit * edit)
503 char *filename;
504 long line, column;
505 off_t offset;
506 vfs_path_t *vpath;
508 if (!edit->filename || !*edit->filename)
509 return;
511 vpath = vfs_path_from_str (edit->filename);
512 filename = vfs_path_to_str (vpath);
513 load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
514 vfs_path_free (vpath);
515 g_free (filename);
517 if (line > 0)
519 edit_move_to_line (edit, line - 1);
520 edit->prev_col = column;
522 else if (offset > 0)
524 edit_cursor_move (edit, offset);
525 line = edit->curs_line;
526 edit->search_start = edit->curs1;
529 book_mark_restore (edit, BOOK_MARK_COLOR);
531 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
532 edit_move_display (edit, line - (edit->widget.lines / 2));
535 /* --------------------------------------------------------------------------------------------- */
536 /** Save cursor position in the file */
538 static void
539 edit_save_position (WEdit * edit)
541 char *filename;
542 vfs_path_t *vpath;
544 if (edit->filename == NULL || *edit->filename == '\0')
545 return;
547 vpath = vfs_path_from_str (edit->filename);
548 filename = vfs_path_to_str (vpath);
550 book_mark_serialize (edit, BOOK_MARK_COLOR);
551 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
552 edit->serialized_bookmarks);
553 edit->serialized_bookmarks = NULL;
555 g_free (filename);
556 vfs_path_free (vpath);
559 /* --------------------------------------------------------------------------------------------- */
560 /** Clean the WEdit stricture except the widget part */
562 static void
563 edit_purge_widget (WEdit * edit)
565 size_t len = sizeof (WEdit) - sizeof (Widget);
566 char *start = (char *) edit + sizeof (Widget);
567 memset (start, 0, len);
570 /* --------------------------------------------------------------------------------------------- */
573 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
574 then the file should be as it was when he loaded up. Then set edit->modified to 0.
577 static long
578 edit_pop_undo_action (WEdit * edit)
580 long c;
581 unsigned long sp = edit->undo_stack_pointer;
583 if (sp == edit->undo_stack_bottom)
584 return STACK_BOTTOM;
586 sp = (sp - 1) & edit->undo_stack_size_mask;
587 c = edit->undo_stack[sp];
588 if (c >= 0)
590 /* edit->undo_stack[sp] = '@'; */
591 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
592 return c;
595 if (sp == edit->undo_stack_bottom)
596 return STACK_BOTTOM;
598 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
599 if (edit->undo_stack[sp] == -2)
601 /* edit->undo_stack[sp] = '@'; */
602 edit->undo_stack_pointer = sp;
604 else
605 edit->undo_stack[sp]++;
607 return c;
610 static long
611 edit_pop_redo_action (WEdit * edit)
613 long c;
614 unsigned long sp = edit->redo_stack_pointer;
616 if (sp == edit->redo_stack_bottom)
617 return STACK_BOTTOM;
619 sp = (sp - 1) & edit->redo_stack_size_mask;
620 c = edit->redo_stack[sp];
621 if (c >= 0)
623 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
624 return c;
627 if (sp == edit->redo_stack_bottom)
628 return STACK_BOTTOM;
630 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
631 if (edit->redo_stack[sp] == -2)
632 edit->redo_stack_pointer = sp;
633 else
634 edit->redo_stack[sp]++;
636 return c;
639 static long
640 get_prev_undo_action (WEdit * edit)
642 long c;
643 unsigned long sp = edit->undo_stack_pointer;
645 if (sp == edit->undo_stack_bottom)
646 return STACK_BOTTOM;
648 sp = (sp - 1) & edit->undo_stack_size_mask;
649 c = edit->undo_stack[sp];
650 if (c >= 0)
651 return c;
653 if (sp == edit->undo_stack_bottom)
654 return STACK_BOTTOM;
656 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
657 return c;
660 /* --------------------------------------------------------------------------------------------- */
661 /** is called whenever a modification is made by one of the four routines below */
663 static void
664 edit_modification (WEdit * edit)
666 edit->caches_valid = 0;
668 /* raise lock when file modified */
669 if (!edit->modified && !edit->delete_file)
670 edit->locked = edit_lock_file (edit);
671 edit->modified = 1;
674 /* --------------------------------------------------------------------------------------------- */
676 static char *
677 edit_get_byte_ptr (WEdit * edit, long byte_index)
679 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
680 return NULL;
682 if (byte_index >= edit->curs1)
684 unsigned long p;
686 p = edit->curs1 + edit->curs2 - byte_index - 1;
687 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
688 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
691 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
692 (byte_index & M_EDIT_BUF_SIZE));
695 /* --------------------------------------------------------------------------------------------- */
697 static int
698 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
700 int i, res;
701 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
702 gchar *str;
703 gchar *cursor_buf_ptr;
705 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
707 *char_width = 0;
708 return 0;
711 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
712 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
713 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
715 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
716 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
718 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
720 *char_width = 1;
721 return *(cursor_buf_ptr - 1);
723 else
725 res = g_utf8_get_char_validated (str, -1);
727 if (res < 0)
729 *char_width = 1;
730 return *(cursor_buf_ptr - 1);
732 else
734 *char_width = cursor_buf_ptr - str;
735 return res;
740 /* --------------------------------------------------------------------------------------------- */
742 static int
743 edit_backspace (WEdit * edit, const int byte_delete)
745 int p = 0;
746 int cw = 1;
747 int i;
749 if (!edit->curs1)
750 return 0;
752 cw = 1;
754 if (edit->mark2 != edit->mark1)
755 edit_push_markers (edit);
757 if (edit->utf8 && byte_delete == 0)
759 edit_get_prev_utf (edit, edit->curs1, &cw);
760 if (cw < 1)
761 cw = 1;
763 for (i = 1; i <= cw; i++)
765 if (edit->mark1 >= edit->curs1)
767 edit->mark1--;
768 edit->end_mark_curs--;
770 if (edit->mark2 >= edit->curs1)
771 edit->mark2--;
772 if (edit->last_get_rule >= edit->curs1)
773 edit->last_get_rule--;
775 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
776 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
777 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
779 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
780 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
782 edit->last_byte--;
783 edit->curs1--;
784 edit_push_undo_action (edit, p);
786 edit_modification (edit);
787 if (p == '\n')
789 if (edit->book_mark)
790 book_mark_dec (edit, edit->curs_line);
791 edit->curs_line--;
792 edit->total_lines--;
793 edit->force |= REDRAW_AFTER_CURSOR;
796 if (edit->curs1 < edit->start_display)
798 edit->start_display--;
799 if (p == '\n')
800 edit->start_line--;
803 return p;
806 /* --------------------------------------------------------------------------------------------- */
807 /* high level cursor movement commands */
808 /* --------------------------------------------------------------------------------------------- */
810 static int
811 is_in_indent (WEdit * edit)
813 long p = edit_bol (edit, edit->curs1);
814 while (p < edit->curs1)
815 if (!strchr (" \t", edit_get_byte (edit, p++)))
816 return 0;
817 return 1;
820 /* --------------------------------------------------------------------------------------------- */
822 static int
823 is_blank (WEdit * edit, long offset)
825 long s, f;
826 int c;
827 s = edit_bol (edit, offset);
828 f = edit_eol (edit, offset) - 1;
829 while (s <= f)
831 c = edit_get_byte (edit, s++);
832 if (!isspace (c))
833 return 0;
835 return 1;
839 /* --------------------------------------------------------------------------------------------- */
840 /** returns the offset of line i */
842 static long
843 edit_find_line (WEdit * edit, int line)
845 int i, j = 0;
846 int m = 2000000000;
847 if (!edit->caches_valid)
849 for (i = 0; i < N_LINE_CACHES; i++)
850 edit->line_numbers[i] = edit->line_offsets[i] = 0;
851 /* three offsets that we *know* are line 0 at 0 and these two: */
852 edit->line_numbers[1] = edit->curs_line;
853 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
854 edit->line_numbers[2] = edit->total_lines;
855 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
856 edit->caches_valid = 1;
858 if (line >= edit->total_lines)
859 return edit->line_offsets[2];
860 if (line <= 0)
861 return 0;
862 /* find the closest known point */
863 for (i = 0; i < N_LINE_CACHES; i++)
865 int n;
866 n = abs (edit->line_numbers[i] - line);
867 if (n < m)
869 m = n;
870 j = i;
873 if (m == 0)
874 return edit->line_offsets[j]; /* know the offset exactly */
875 if (m == 1 && j >= 3)
876 i = j; /* one line different - caller might be looping, so stay in this cache */
877 else
878 i = 3 + (rand () % (N_LINE_CACHES - 3));
879 if (line > edit->line_numbers[j])
880 edit->line_offsets[i] =
881 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
882 else
883 edit->line_offsets[i] =
884 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
885 edit->line_numbers[i] = line;
886 return edit->line_offsets[i];
889 /* --------------------------------------------------------------------------------------------- */
890 /** moves up until a blank line is reached, or until just
891 before a non-blank line is reached */
893 static void
894 edit_move_up_paragraph (WEdit * edit, int do_scroll)
896 int i = 0;
897 if (edit->curs_line > 1)
899 if (line_is_blank (edit, edit->curs_line))
901 if (line_is_blank (edit, edit->curs_line - 1))
903 for (i = edit->curs_line - 1; i; i--)
904 if (!line_is_blank (edit, i))
906 i++;
907 break;
910 else
912 for (i = edit->curs_line - 1; i; i--)
913 if (line_is_blank (edit, i))
914 break;
917 else
919 for (i = edit->curs_line - 1; i; i--)
920 if (line_is_blank (edit, i))
921 break;
924 edit_move_up (edit, edit->curs_line - i, do_scroll);
927 /* --------------------------------------------------------------------------------------------- */
928 /** moves down until a blank line is reached, or until just
929 before a non-blank line is reached */
931 static void
932 edit_move_down_paragraph (WEdit * edit, int do_scroll)
934 int i;
935 if (edit->curs_line >= edit->total_lines - 1)
937 i = edit->total_lines;
939 else
941 if (line_is_blank (edit, edit->curs_line))
943 if (line_is_blank (edit, edit->curs_line + 1))
945 for (i = edit->curs_line + 1; i; i++)
946 if (!line_is_blank (edit, i) || i > edit->total_lines)
948 i--;
949 break;
952 else
954 for (i = edit->curs_line + 1; i; i++)
955 if (line_is_blank (edit, i) || i >= edit->total_lines)
956 break;
959 else
961 for (i = edit->curs_line + 1; i; i++)
962 if (line_is_blank (edit, i) || i >= edit->total_lines)
963 break;
966 edit_move_down (edit, i - edit->curs_line, do_scroll);
969 /* --------------------------------------------------------------------------------------------- */
971 static void
972 edit_begin_page (WEdit * edit)
974 edit_update_curs_row (edit);
975 edit_move_up (edit, edit->curs_row, 0);
978 /* --------------------------------------------------------------------------------------------- */
980 static void
981 edit_end_page (WEdit * edit)
983 edit_update_curs_row (edit);
984 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
988 /* --------------------------------------------------------------------------------------------- */
989 /** goto beginning of text */
991 static void
992 edit_move_to_top (WEdit * edit)
994 if (edit->curs_line)
996 edit_cursor_move (edit, -edit->curs1);
997 edit_move_to_prev_col (edit, 0);
998 edit->force |= REDRAW_PAGE;
999 edit->search_start = 0;
1000 edit_update_curs_row (edit);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** goto end of text */
1008 static void
1009 edit_move_to_bottom (WEdit * edit)
1011 if (edit->curs_line < edit->total_lines)
1013 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1014 edit->start_display = edit->last_byte;
1015 edit->start_line = edit->total_lines;
1016 edit_scroll_upward (edit, edit->widget.lines - 1);
1017 edit->force |= REDRAW_PAGE;
1021 /* --------------------------------------------------------------------------------------------- */
1022 /** goto beginning of line */
1024 static void
1025 edit_cursor_to_bol (WEdit * edit)
1027 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1028 edit->search_start = edit->curs1;
1029 edit->prev_col = edit_get_col (edit);
1030 edit->over_col = 0;
1033 /* --------------------------------------------------------------------------------------------- */
1034 /** goto end of line */
1036 static void
1037 edit_cursor_to_eol (WEdit * edit)
1039 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1040 edit->search_start = edit->curs1;
1041 edit->prev_col = edit_get_col (edit);
1042 edit->over_col = 0;
1045 /* --------------------------------------------------------------------------------------------- */
1047 static unsigned long
1048 my_type_of (int c)
1050 int x, r = 0;
1051 const char *p, *q;
1052 const char option_chars_move_whole_word[] =
1053 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1055 if (!c)
1056 return 0;
1057 if (c == '!')
1059 if (*option_chars_move_whole_word == '!')
1060 return 2;
1061 return 0x80000000UL;
1063 if (g_ascii_isupper ((gchar) c))
1064 c = 'A';
1065 else if (g_ascii_islower ((gchar) c))
1066 c = 'a';
1067 else if (g_ascii_isalpha (c))
1068 c = 'a';
1069 else if (isdigit (c))
1070 c = '0';
1071 else if (isspace (c))
1072 c = ' ';
1073 q = strchr (option_chars_move_whole_word, c);
1074 if (!q)
1075 return 0xFFFFFFFFUL;
1078 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1079 if (*p == '!')
1080 x <<= 1;
1081 r |= x;
1083 while ((q = strchr (q + 1, c)));
1084 return r;
1087 /* --------------------------------------------------------------------------------------------- */
1089 static void
1090 edit_left_word_move (WEdit * edit, int s)
1092 for (;;)
1094 int c1, c2;
1095 if (edit->column_highlight
1096 && edit->mark1 != edit->mark2
1097 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1098 break;
1099 edit_cursor_move (edit, -1);
1100 if (!edit->curs1)
1101 break;
1102 c1 = edit_get_byte (edit, edit->curs1 - 1);
1103 c2 = edit_get_byte (edit, edit->curs1);
1104 if (c1 == '\n' || c2 == '\n')
1105 break;
1106 if (!(my_type_of (c1) & my_type_of (c2)))
1107 break;
1108 if (isspace (c1) && !isspace (c2))
1109 break;
1110 if (s)
1111 if (!isspace (c1) && isspace (c2))
1112 break;
1116 /* --------------------------------------------------------------------------------------------- */
1118 static void
1119 edit_left_word_move_cmd (WEdit * edit)
1121 edit_left_word_move (edit, 0);
1122 edit->force |= REDRAW_PAGE;
1125 /* --------------------------------------------------------------------------------------------- */
1127 static void
1128 edit_right_word_move (WEdit * edit, int s)
1130 for (;;)
1132 int c1, c2;
1133 if (edit->column_highlight
1134 && edit->mark1 != edit->mark2
1135 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1136 break;
1137 edit_cursor_move (edit, 1);
1138 if (edit->curs1 >= edit->last_byte)
1139 break;
1140 c1 = edit_get_byte (edit, edit->curs1 - 1);
1141 c2 = edit_get_byte (edit, edit->curs1);
1142 if (c1 == '\n' || c2 == '\n')
1143 break;
1144 if (!(my_type_of (c1) & my_type_of (c2)))
1145 break;
1146 if (isspace (c1) && !isspace (c2))
1147 break;
1148 if (s)
1149 if (!isspace (c1) && isspace (c2))
1150 break;
1154 /* --------------------------------------------------------------------------------------------- */
1156 static void
1157 edit_right_word_move_cmd (WEdit * edit)
1159 edit_right_word_move (edit, 0);
1160 edit->force |= REDRAW_PAGE;
1163 /* --------------------------------------------------------------------------------------------- */
1165 static void
1166 edit_right_char_move_cmd (WEdit * edit)
1168 int cw = 1;
1169 int c = 0;
1170 if (edit->utf8)
1172 c = edit_get_utf (edit, edit->curs1, &cw);
1173 if (cw < 1)
1174 cw = 1;
1176 else
1178 c = edit_get_byte (edit, edit->curs1);
1180 if (option_cursor_beyond_eol && c == '\n')
1182 edit->over_col++;
1184 else
1186 edit_cursor_move (edit, cw);
1190 /* --------------------------------------------------------------------------------------------- */
1192 static void
1193 edit_left_char_move_cmd (WEdit * edit)
1195 int cw = 1;
1196 if (edit->column_highlight
1197 && option_cursor_beyond_eol
1198 && edit->mark1 != edit->mark2
1199 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1200 return;
1201 if (edit->utf8)
1203 edit_get_prev_utf (edit, edit->curs1, &cw);
1204 if (cw < 1)
1205 cw = 1;
1207 if (option_cursor_beyond_eol && edit->over_col > 0)
1209 edit->over_col--;
1211 else
1213 edit_cursor_move (edit, -cw);
1217 /* --------------------------------------------------------------------------------------------- */
1218 /** Up or down cursor moving.
1219 direction = TRUE - move up
1220 = FALSE - move down
1223 static void
1224 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1226 unsigned long p;
1227 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1229 if (i > l)
1230 i = l;
1232 if (i == 0)
1233 return;
1235 if (i > 1)
1236 edit->force |= REDRAW_PAGE;
1237 if (do_scroll)
1239 if (direction)
1240 edit_scroll_upward (edit, i);
1241 else
1242 edit_scroll_downward (edit, i);
1244 p = edit_bol (edit, edit->curs1);
1246 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1248 edit_cursor_move (edit, p - edit->curs1);
1250 edit_move_to_prev_col (edit, p);
1252 /* search start of current multibyte char (like CJK) */
1253 if (edit->curs1 + 1 < edit->last_byte)
1255 edit_right_char_move_cmd (edit);
1256 edit_left_char_move_cmd (edit);
1259 edit->search_start = edit->curs1;
1260 edit->found_len = 0;
1263 /* --------------------------------------------------------------------------------------------- */
1265 static void
1266 edit_right_delete_word (WEdit * edit)
1268 int c1, c2;
1269 for (;;)
1271 if (edit->curs1 >= edit->last_byte)
1272 break;
1273 c1 = edit_delete (edit, 1);
1274 c2 = edit_get_byte (edit, edit->curs1);
1275 if (c1 == '\n' || c2 == '\n')
1276 break;
1277 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1278 break;
1279 if (!(my_type_of (c1) & my_type_of (c2)))
1280 break;
1284 /* --------------------------------------------------------------------------------------------- */
1286 static void
1287 edit_left_delete_word (WEdit * edit)
1289 int c1, c2;
1290 for (;;)
1292 if (edit->curs1 <= 0)
1293 break;
1294 c1 = edit_backspace (edit, 1);
1295 c2 = edit_get_byte (edit, edit->curs1 - 1);
1296 if (c1 == '\n' || c2 == '\n')
1297 break;
1298 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1299 break;
1300 if (!(my_type_of (c1) & my_type_of (c2)))
1301 break;
1305 /* --------------------------------------------------------------------------------------------- */
1307 the start column position is not recorded, and hence does not
1308 undo as it happed. But who would notice.
1311 static void
1312 edit_do_undo (WEdit * edit)
1314 long ac;
1315 long count = 0;
1317 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1318 edit->over_col = 0;
1319 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1321 switch ((int) ac)
1323 case STACK_BOTTOM:
1324 goto done_undo;
1325 case CURS_RIGHT:
1326 edit_cursor_move (edit, 1);
1327 break;
1328 case CURS_LEFT:
1329 edit_cursor_move (edit, -1);
1330 break;
1331 case BACKSPACE:
1332 case BACKSPACE_BR:
1333 edit_backspace (edit, 1);
1334 break;
1335 case DELCHAR:
1336 case DELCHAR_BR:
1337 edit_delete (edit, 1);
1338 break;
1339 case COLUMN_ON:
1340 edit->column_highlight = 1;
1341 break;
1342 case COLUMN_OFF:
1343 edit->column_highlight = 0;
1344 break;
1346 if (ac >= 256 && ac < 512)
1347 edit_insert_ahead (edit, ac - 256);
1348 if (ac >= 0 && ac < 256)
1349 edit_insert (edit, ac);
1351 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1353 edit->mark1 = ac - MARK_1;
1354 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1356 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1358 edit->mark2 = ac - MARK_2;
1359 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1361 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1363 edit->end_mark_curs = ac - MARK_CURS;
1365 if (count++)
1366 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1369 if (edit->start_display > ac - KEY_PRESS)
1371 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1372 edit->force |= REDRAW_PAGE;
1374 else if (edit->start_display < ac - KEY_PRESS)
1376 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1377 edit->force |= REDRAW_PAGE;
1379 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1380 edit_update_curs_row (edit);
1382 done_undo:;
1383 edit->undo_stack_disable = 0;
1386 static void
1387 edit_do_redo (WEdit * edit)
1389 long ac;
1390 long count = 0;
1392 if (edit->redo_stack_reset)
1393 return;
1395 edit->over_col = 0;
1396 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1398 switch ((int) ac)
1400 case STACK_BOTTOM:
1401 goto done_redo;
1402 case CURS_RIGHT:
1403 edit_cursor_move (edit, 1);
1404 break;
1405 case CURS_LEFT:
1406 edit_cursor_move (edit, -1);
1407 break;
1408 case BACKSPACE:
1409 edit_backspace (edit, 1);
1410 break;
1411 case DELCHAR:
1412 edit_delete (edit, 1);
1413 break;
1414 case COLUMN_ON:
1415 edit->column_highlight = 1;
1416 break;
1417 case COLUMN_OFF:
1418 edit->column_highlight = 0;
1419 break;
1421 if (ac >= 256 && ac < 512)
1422 edit_insert_ahead (edit, ac - 256);
1423 if (ac >= 0 && ac < 256)
1424 edit_insert (edit, ac);
1426 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1428 edit->mark1 = ac - MARK_1;
1429 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1431 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1433 edit->mark2 = ac - MARK_2;
1434 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1436 /* more than one pop usually means something big */
1437 if (count++)
1438 edit->force |= REDRAW_PAGE;
1441 if (edit->start_display > ac - KEY_PRESS)
1443 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1444 edit->force |= REDRAW_PAGE;
1446 else if (edit->start_display < ac - KEY_PRESS)
1448 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1449 edit->force |= REDRAW_PAGE;
1451 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1452 edit_update_curs_row (edit);
1454 done_redo:;
1457 static void
1458 edit_group_undo (WEdit * edit)
1460 long ac = KEY_PRESS;
1461 long cur_ac = KEY_PRESS;
1462 while (ac != STACK_BOTTOM && ac == cur_ac)
1464 cur_ac = get_prev_undo_action (edit);
1465 edit_do_undo (edit);
1466 ac = get_prev_undo_action (edit);
1467 /* exit from cycle if option_group_undo is not set,
1468 * and make single UNDO operation
1470 if (!option_group_undo)
1471 ac = STACK_BOTTOM;
1475 /* --------------------------------------------------------------------------------------------- */
1477 static void
1478 edit_delete_to_line_end (WEdit * edit)
1480 while (edit_get_byte (edit, edit->curs1) != '\n')
1482 if (!edit->curs2)
1483 break;
1484 edit_delete (edit, 1);
1488 /* --------------------------------------------------------------------------------------------- */
1490 static void
1491 edit_delete_to_line_begin (WEdit * edit)
1493 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1495 if (!edit->curs1)
1496 break;
1497 edit_backspace (edit, 1);
1501 /* --------------------------------------------------------------------------------------------- */
1503 static int
1504 is_aligned_on_a_tab (WEdit * edit)
1506 edit_update_curs_col (edit);
1507 return !((edit->curs_col % (TAB_SIZE * space_width))
1508 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1511 /* --------------------------------------------------------------------------------------------- */
1513 static int
1514 right_of_four_spaces (WEdit * edit)
1516 int i, ch = 0;
1517 for (i = 1; i <= HALF_TAB_SIZE; i++)
1518 ch |= edit_get_byte (edit, edit->curs1 - i);
1519 if (ch == ' ')
1520 return is_aligned_on_a_tab (edit);
1521 return 0;
1524 /* --------------------------------------------------------------------------------------------- */
1526 static int
1527 left_of_four_spaces (WEdit * edit)
1529 int i, ch = 0;
1530 for (i = 0; i < HALF_TAB_SIZE; i++)
1531 ch |= edit_get_byte (edit, edit->curs1 + i);
1532 if (ch == ' ')
1533 return is_aligned_on_a_tab (edit);
1534 return 0;
1537 /* --------------------------------------------------------------------------------------------- */
1539 static void
1540 edit_auto_indent (WEdit * edit)
1542 long p;
1543 char c;
1544 p = edit->curs1;
1545 /* use the previous line as a template */
1546 p = edit_move_backward (edit, p, 1);
1547 /* copy the leading whitespace of the line */
1548 for (;;)
1549 { /* no range check - the line _is_ \n-terminated */
1550 c = edit_get_byte (edit, p++);
1551 if (c != ' ' && c != '\t')
1552 break;
1553 edit_insert (edit, c);
1557 /* --------------------------------------------------------------------------------------------- */
1559 static inline void
1560 edit_double_newline (WEdit * edit)
1562 edit_insert (edit, '\n');
1563 if (edit_get_byte (edit, edit->curs1) == '\n')
1564 return;
1565 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1566 return;
1567 edit->force |= REDRAW_PAGE;
1568 edit_insert (edit, '\n');
1572 /* --------------------------------------------------------------------------------------------- */
1574 static void
1575 insert_spaces_tab (WEdit * edit, gboolean half)
1577 int i;
1579 edit_update_curs_col (edit);
1580 i = option_tab_spacing * space_width;
1581 if (half)
1582 i /= 2;
1583 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1584 while (i > 0)
1586 edit_insert (edit, ' ');
1587 i -= space_width;
1591 /* --------------------------------------------------------------------------------------------- */
1593 static inline void
1594 edit_tab_cmd (WEdit * edit)
1596 int i;
1598 if (option_fake_half_tabs)
1600 if (is_in_indent (edit))
1602 /*insert a half tab (usually four spaces) unless there is a
1603 half tab already behind, then delete it and insert a
1604 full tab. */
1605 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1606 insert_spaces_tab (edit, TRUE);
1607 else
1609 for (i = 1; i <= HALF_TAB_SIZE; i++)
1610 edit_backspace (edit, 1);
1611 edit_insert (edit, '\t');
1613 return;
1616 if (option_fill_tabs_with_spaces)
1617 insert_spaces_tab (edit, FALSE);
1618 else
1619 edit_insert (edit, '\t');
1622 /* --------------------------------------------------------------------------------------------- */
1624 static void
1625 check_and_wrap_line (WEdit * edit)
1627 int curs, c;
1628 if (!option_typewriter_wrap)
1629 return;
1630 edit_update_curs_col (edit);
1631 if (edit->curs_col < option_word_wrap_line_length)
1632 return;
1633 curs = edit->curs1;
1634 for (;;)
1636 curs--;
1637 c = edit_get_byte (edit, curs);
1638 if (c == '\n' || curs <= 0)
1640 edit_insert (edit, '\n');
1641 return;
1643 if (c == ' ' || c == '\t')
1645 int current = edit->curs1;
1646 edit_cursor_move (edit, curs - edit->curs1 + 1);
1647 edit_insert (edit, '\n');
1648 edit_cursor_move (edit, current - edit->curs1 + 1);
1649 return;
1654 /* --------------------------------------------------------------------------------------------- */
1655 /** this find the matching bracket in either direction, and sets edit->bracket */
1657 static long
1658 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1660 const char *const b = "{}{[][()(", *p;
1661 int i = 1, a, inc = -1, c, d, n = 0;
1662 unsigned long j = 0;
1663 long q;
1664 edit_update_curs_row (edit);
1665 c = edit_get_byte (edit, edit->curs1);
1666 p = strchr (b, c);
1667 /* no limit */
1668 if (!furthest_bracket_search)
1669 furthest_bracket_search--;
1670 /* not on a bracket at all */
1671 if (!p)
1672 return -1;
1673 /* the matching bracket */
1674 d = p[1];
1675 /* going left or right? */
1676 if (strchr ("{[(", c))
1677 inc = 1;
1678 for (q = edit->curs1 + inc;; q += inc)
1680 /* out of buffer? */
1681 if (q >= edit->last_byte || q < 0)
1682 break;
1683 a = edit_get_byte (edit, q);
1684 /* don't want to eat CPU */
1685 if (j++ > furthest_bracket_search)
1686 break;
1687 /* out of screen? */
1688 if (in_screen)
1690 if (q < edit->start_display)
1691 break;
1692 /* count lines if searching downward */
1693 if (inc > 0 && a == '\n')
1694 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1695 break;
1697 /* count bracket depth */
1698 i += (a == c) - (a == d);
1699 /* return if bracket depth is zero */
1700 if (!i)
1701 return q;
1703 /* no match */
1704 return -1;
1707 /* --------------------------------------------------------------------------------------------- */
1709 static inline void
1710 edit_goto_matching_bracket (WEdit * edit)
1712 long q;
1714 q = edit_get_bracket (edit, 0, 0);
1715 if (q >= 0)
1717 edit->bracket = edit->curs1;
1718 edit->force |= REDRAW_PAGE;
1719 edit_cursor_move (edit, q - edit->curs1);
1723 /* --------------------------------------------------------------------------------------------- */
1725 static void
1726 edit_move_block_to_right (WEdit * edit)
1728 long start_mark, end_mark;
1729 long cur_bol, start_bol;
1731 if (eval_marks (edit, &start_mark, &end_mark))
1732 return;
1734 start_bol = edit_bol (edit, start_mark);
1735 cur_bol = edit_bol (edit, end_mark - 1);
1739 edit_cursor_move (edit, cur_bol - edit->curs1);
1740 if (option_fill_tabs_with_spaces)
1741 insert_spaces_tab (edit, option_fake_half_tabs);
1742 else
1743 edit_insert (edit, '\t');
1744 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1746 if (cur_bol == 0)
1747 break;
1749 cur_bol = edit_bol (edit, cur_bol - 1);
1751 while (cur_bol >= start_bol);
1753 edit->force |= REDRAW_PAGE;
1756 /* --------------------------------------------------------------------------------------------- */
1758 static void
1759 edit_move_block_to_left (WEdit * edit)
1761 long start_mark, end_mark;
1762 long cur_bol, start_bol;
1763 int i;
1765 if (eval_marks (edit, &start_mark, &end_mark))
1766 return;
1768 start_bol = edit_bol (edit, start_mark);
1769 cur_bol = edit_bol (edit, end_mark - 1);
1773 int del_tab_width;
1774 int next_char;
1776 edit_cursor_move (edit, cur_bol - edit->curs1);
1778 if (option_fake_half_tabs)
1779 del_tab_width = HALF_TAB_SIZE;
1780 else
1781 del_tab_width = option_tab_spacing;
1783 next_char = edit_get_byte (edit, edit->curs1);
1784 if (next_char == '\t')
1785 edit_delete (edit, 1);
1786 else if (next_char == ' ')
1787 for (i = 1; i <= del_tab_width; i++)
1789 if (next_char == ' ')
1790 edit_delete (edit, 1);
1791 next_char = edit_get_byte (edit, edit->curs1);
1794 if (cur_bol == 0)
1795 break;
1797 cur_bol = edit_bol (edit, cur_bol - 1);
1799 while (cur_bol >= start_bol);
1801 edit->force |= REDRAW_PAGE;
1804 /* --------------------------------------------------------------------------------------------- */
1806 * prints at the cursor
1807 * @returns the number of chars printed
1810 static size_t
1811 edit_print_string (WEdit * e, const char *s)
1813 size_t i = 0;
1815 while (s[i] != '\0')
1816 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1817 e->force |= REDRAW_COMPLETELY;
1818 edit_update_screen (e);
1819 return i;
1822 /* --------------------------------------------------------------------------------------------- */
1823 /*** public functions ****************************************************************************/
1824 /* --------------------------------------------------------------------------------------------- */
1826 /** User edit menu, like user menu (F2) but only in editor. */
1828 void
1829 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1831 char *block_file;
1832 int nomark;
1833 long curs;
1834 long start_mark, end_mark;
1835 struct stat status;
1837 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1838 curs = edit->curs1;
1839 nomark = eval_marks (edit, &start_mark, &end_mark);
1840 if (nomark == 0)
1841 edit_save_block (edit, block_file, start_mark, end_mark);
1843 /* run shell scripts from menu */
1844 if (user_menu_cmd (edit, menu_file, selected_entry)
1845 && (mc_stat (block_file, &status) == 0) && (status.st_size != 0))
1847 int rc = 0;
1848 FILE *fd;
1850 /* i.e. we have marked block */
1851 if (nomark == 0)
1852 rc = edit_block_delete_cmd (edit);
1854 if (rc == 0)
1856 long ins_len;
1858 ins_len = edit_insert_file (edit, block_file);
1859 if (nomark == 0 && ins_len > 0)
1860 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1862 /* truncate block file */
1863 fd = fopen (block_file, "w");
1864 if (fd != NULL)
1865 fclose (fd);
1867 edit_cursor_move (edit, curs - edit->curs1);
1868 edit_refresh_cmd (edit);
1869 edit->force |= REDRAW_COMPLETELY;
1871 g_free (block_file);
1874 /* --------------------------------------------------------------------------------------------- */
1877 edit_get_byte (WEdit * edit, long byte_index)
1879 unsigned long p;
1880 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1881 return '\n';
1883 if (byte_index >= edit->curs1)
1885 p = edit->curs1 + edit->curs2 - byte_index - 1;
1886 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1888 else
1890 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1894 /* --------------------------------------------------------------------------------------------- */
1897 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1899 gchar *str = NULL;
1900 int res = -1;
1901 gunichar ch;
1902 gchar *next_ch = NULL;
1903 int width = 0;
1904 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1906 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1908 *char_width = 0;
1909 return '\n';
1912 str = edit_get_byte_ptr (edit, byte_index);
1914 if (str == NULL)
1916 *char_width = 0;
1917 return 0;
1920 res = g_utf8_get_char_validated (str, -1);
1922 if (res < 0)
1924 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1925 int i;
1926 for (i = 0; i < UTF8_CHAR_LEN; i++)
1927 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1928 utf8_buf[UTF8_CHAR_LEN] = '\0';
1929 str = utf8_buf;
1930 res = g_utf8_get_char_validated (str, -1);
1933 if (res < 0)
1935 ch = *str;
1936 width = 0;
1938 else
1940 ch = res;
1941 /* Calculate UTF-8 char width */
1942 next_ch = g_utf8_next_char (str);
1943 if (next_ch)
1945 width = next_ch - str;
1947 else
1949 ch = 0;
1950 width = 0;
1953 *char_width = width;
1954 return ch;
1957 /* --------------------------------------------------------------------------------------------- */
1959 char *
1960 edit_get_write_filter (const char *write_name, const char *filename)
1962 int i;
1963 char *p, *writename;
1965 i = edit_find_filter (filename);
1966 if (i < 0)
1967 return NULL;
1969 writename = name_quote (write_name, 0);
1970 p = g_strdup_printf (all_filters[i].write, writename);
1971 g_free (writename);
1972 return p;
1975 /* --------------------------------------------------------------------------------------------- */
1977 long
1978 edit_write_stream (WEdit * edit, FILE * f)
1980 long i;
1982 if (edit->lb == LB_ASIS)
1984 for (i = 0; i < edit->last_byte; i++)
1985 if (fputc (edit_get_byte (edit, i), f) < 0)
1986 break;
1987 return i;
1990 /* change line breaks */
1991 for (i = 0; i < edit->last_byte; i++)
1993 unsigned char c = edit_get_byte (edit, i);
1995 if (!(c == '\n' || c == '\r'))
1997 /* not line break */
1998 if (fputc (c, f) < 0)
1999 return i;
2001 else
2002 { /* (c == '\n' || c == '\r') */
2003 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2005 switch (edit->lb)
2007 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2008 /* put one line break unconditionally */
2009 if (fputc ('\n', f) < 0)
2010 return i;
2012 i++; /* 2 chars are processed */
2014 if (c == '\r' && c1 == '\n')
2015 /* Windows line break; go to the next char */
2016 break;
2018 if (c == '\r' && c1 == '\r')
2020 /* two Macintosh line breaks; put second line break */
2021 if (fputc ('\n', f) < 0)
2022 return i;
2023 break;
2026 if (fputc (c1, f) < 0)
2027 return i;
2028 break;
2030 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2031 /* put one line break unconditionally */
2032 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2033 return i;
2035 if (c == '\r' && c1 == '\n')
2036 /* Windows line break; go to the next char */
2037 i++;
2038 break;
2040 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2041 /* put one line break unconditionally */
2042 if (fputc ('\r', f) < 0)
2043 return i;
2045 i++; /* 2 chars are processed */
2047 if (c == '\r' && c1 == '\n')
2048 /* Windows line break; go to the next char */
2049 break;
2051 if (c == '\n' && c1 == '\n')
2053 /* two Windows line breaks; put second line break */
2054 if (fputc ('\r', f) < 0)
2055 return i;
2056 break;
2059 if (fputc (c1, f) < 0)
2060 return i;
2061 break;
2062 case LB_ASIS: /* default without changes */
2063 break;
2068 return edit->last_byte;
2071 /* --------------------------------------------------------------------------------------------- */
2072 /** inserts a file at the cursor, returns count of inserted bytes on success */
2073 long
2074 edit_insert_file (WEdit * edit, const char *filename)
2076 char *p;
2077 long ins_len = 0;
2079 p = edit_get_filter (filename);
2080 if (p != NULL)
2082 FILE *f;
2083 long current = edit->curs1;
2084 f = (FILE *) popen (p, "r");
2085 if (f != NULL)
2087 edit_insert_stream (edit, f);
2088 ins_len = edit->curs1 - current;
2089 edit_cursor_move (edit, current - edit->curs1);
2090 if (pclose (f) > 0)
2092 char *errmsg;
2093 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2094 edit_error_dialog (_("Error"), errmsg);
2095 g_free (errmsg);
2096 g_free (p);
2097 return -1;
2100 else
2102 char *errmsg;
2103 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2104 edit_error_dialog (_("Error"), errmsg);
2105 g_free (errmsg);
2106 g_free (p);
2107 return -1;
2109 g_free (p);
2111 else
2113 int i, file, blocklen;
2114 long current = edit->curs1;
2115 int vertical_insertion = 0;
2116 char *buf;
2117 file = mc_open (filename, O_RDONLY | O_BINARY);
2118 if (file == -1)
2119 return -1;
2120 buf = g_malloc0 (TEMP_BUF_LEN);
2121 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2122 if (blocklen > 0)
2124 /* if contain signature VERTICAL_MAGIC then it vertical block */
2125 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2126 vertical_insertion = 1;
2127 else
2128 mc_lseek (file, 0, SEEK_SET);
2130 if (vertical_insertion)
2132 long mark1, mark2;
2133 int c1, c2;
2134 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2135 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2136 /* highlight inserted text then not persistent blocks */
2137 if (!option_persistent_selections)
2139 if (!edit->column_highlight)
2140 edit_push_undo_action (edit, COLUMN_OFF);
2141 edit->column_highlight = 1;
2144 else
2146 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2148 for (i = 0; i < blocklen; i++)
2149 edit_insert (edit, buf[i]);
2151 /* highlight inserted text then not persistent blocks */
2152 if (!option_persistent_selections && edit->modified)
2154 edit_set_markers (edit, edit->curs1, current, 0, 0);
2155 if (edit->column_highlight)
2156 edit_push_undo_action (edit, COLUMN_ON);
2157 edit->column_highlight = 0;
2160 edit->force |= REDRAW_PAGE;
2161 ins_len = edit->curs1 - current;
2162 edit_cursor_move (edit, current - edit->curs1);
2163 g_free (buf);
2164 mc_close (file);
2165 if (blocklen != 0)
2166 return 0;
2168 return ins_len;
2171 /* --------------------------------------------------------------------------------------------- */
2173 * Fill in the edit structure. Return NULL on failure. Pass edit as
2174 * NULL to allocate a new structure.
2176 * If line is 0, try to restore saved position. Otherwise put the
2177 * cursor on that line and show it in the middle of the screen.
2180 WEdit *
2181 edit_init (WEdit * edit, int y, int x, int lines, int cols, const char *filename, long line)
2183 gboolean to_free = FALSE;
2185 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2186 if (option_line_state)
2187 option_line_state_width = LINE_STATE_WIDTH;
2188 else
2189 option_line_state_width = 0;
2191 if (edit == NULL)
2193 #ifdef ENABLE_NLS
2195 * Expand option_whole_chars_search by national letters using
2196 * current locale
2199 static char option_whole_chars_search_buf[256];
2201 if (option_whole_chars_search_buf != option_whole_chars_search)
2203 size_t i;
2204 size_t len = str_term_width1 (option_whole_chars_search);
2206 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2208 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2210 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2212 option_whole_chars_search_buf[len++] = i;
2216 option_whole_chars_search_buf[len] = 0;
2217 option_whole_chars_search = option_whole_chars_search_buf;
2219 #endif /* ENABLE_NLS */
2220 edit = g_malloc0 (sizeof (WEdit));
2221 edit->search = NULL;
2222 to_free = TRUE;
2225 edit_purge_widget (edit);
2226 edit->widget.y = y;
2227 edit->widget.x = x;
2228 edit->widget.lines = lines;
2229 edit->widget.cols = cols;
2231 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2232 edit->stat1.st_uid = getuid ();
2233 edit->stat1.st_gid = getgid ();
2234 edit->stat1.st_mtime = 0;
2236 edit->over_col = 0;
2237 edit->bracket = -1;
2238 edit->force |= REDRAW_PAGE;
2239 edit_set_filename (edit, filename);
2241 edit->undo_stack_size = START_STACK_SIZE;
2242 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2243 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2245 edit->redo_stack_size = START_STACK_SIZE;
2246 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2247 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2249 edit->utf8 = 0;
2250 edit->converter = str_cnv_from_term;
2251 edit_set_codeset (edit);
2253 if (edit_load_file (edit))
2255 /* edit_load_file already gives an error message */
2256 if (to_free)
2257 g_free (edit);
2258 return NULL;
2261 edit->loading_done = 1;
2262 edit->modified = 0;
2263 edit->locked = 0;
2264 edit_load_syntax (edit, NULL, NULL);
2266 int color;
2267 edit_get_syntax_color (edit, -1, &color);
2270 /* load saved cursor position */
2271 if ((line == 0) && option_save_position)
2272 edit_load_position (edit);
2273 else
2275 if (line <= 0)
2276 line = 1;
2277 edit_move_display (edit, line - 1);
2278 edit_move_to_line (edit, line - 1);
2281 edit_load_macro_cmd (edit);
2282 return edit;
2285 /* --------------------------------------------------------------------------------------------- */
2286 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2289 edit_clean (WEdit * edit)
2291 int j = 0;
2293 if (!edit)
2294 return 0;
2296 /* a stale lock, remove it */
2297 if (edit->locked)
2298 edit->locked = edit_unlock_file (edit);
2300 /* save cursor position */
2301 if (option_save_position)
2302 edit_save_position (edit);
2303 else if (edit->serialized_bookmarks != NULL)
2304 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2306 /* File specified on the mcedit command line and never saved */
2307 if (edit->delete_file)
2308 unlink (edit->filename);
2310 edit_free_syntax_rules (edit);
2311 book_mark_flush (edit, -1);
2312 for (; j <= MAXBUFF; j++)
2314 g_free (edit->buffers1[j]);
2315 g_free (edit->buffers2[j]);
2318 g_free (edit->undo_stack);
2319 g_free (edit->redo_stack);
2320 g_free (edit->filename);
2321 g_free (edit->dir);
2322 mc_search_free (edit->search);
2323 edit->search = NULL;
2324 edit_highlight_deinit ();
2326 if (edit->converter != str_cnv_from_term)
2327 str_close_conv (edit->converter);
2329 edit_purge_widget (edit);
2331 return 1;
2334 /* --------------------------------------------------------------------------------------------- */
2335 /** returns 1 on success */
2338 edit_renew (WEdit * edit)
2340 int y = edit->widget.y;
2341 int x = edit->widget.x;
2342 int lines = edit->widget.lines;
2343 int columns = edit->widget.cols;
2345 edit_clean (edit);
2346 return (edit_init (edit, y, x, lines, columns, "", 0) != NULL);
2349 /* --------------------------------------------------------------------------------------------- */
2351 * Load a new file into the editor. If it fails, preserve the old file.
2352 * To do it, allocate a new widget, initialize it and, if the new file
2353 * was loaded, copy the data to the old widget.
2354 * Return 1 on success, 0 on failure.
2358 edit_reload (WEdit * edit, const char *filename)
2360 WEdit *e;
2361 int y = edit->widget.y;
2362 int x = edit->widget.x;
2363 int lines = edit->widget.lines;
2364 int columns = edit->widget.cols;
2366 e = g_malloc0 (sizeof (WEdit));
2367 e->widget = edit->widget;
2368 if (edit_init (e, y, x, lines, columns, filename, 0) == NULL)
2370 g_free (e);
2371 return 0;
2373 edit_clean (edit);
2374 memcpy (edit, e, sizeof (WEdit));
2375 g_free (e);
2376 return 1;
2379 /* --------------------------------------------------------------------------------------------- */
2381 * Load a new file into the editor and set line. If it fails, preserve the old file.
2382 * To do it, allocate a new widget, initialize it and, if the new file
2383 * was loaded, copy the data to the old widget.
2384 * Return 1 on success, 0 on failure.
2388 edit_reload_line (WEdit * edit, const char *filename, long line)
2390 WEdit *e;
2391 int y = edit->widget.y;
2392 int x = edit->widget.x;
2393 int lines = edit->widget.lines;
2394 int columns = edit->widget.cols;
2396 e = g_malloc0 (sizeof (WEdit));
2397 e->widget = edit->widget;
2398 if (edit_init (e, y, x, lines, columns, filename, line) == NULL)
2400 g_free (e);
2401 return 0;
2403 edit_clean (edit);
2404 memcpy (edit, e, sizeof (WEdit));
2405 g_free (e);
2406 return 1;
2409 /* --------------------------------------------------------------------------------------------- */
2411 void
2412 edit_set_codeset (WEdit * edit)
2414 #ifdef HAVE_CHARSET
2415 const char *cp_id;
2417 cp_id =
2418 get_codepage_id (mc_global.source_codepage >=
2419 0 ? mc_global.source_codepage : mc_global.display_codepage);
2421 if (cp_id != NULL)
2423 GIConv conv;
2424 conv = str_crt_conv_from (cp_id);
2425 if (conv != INVALID_CONV)
2427 if (edit->converter != str_cnv_from_term)
2428 str_close_conv (edit->converter);
2429 edit->converter = conv;
2433 if (cp_id != NULL)
2434 edit->utf8 = str_isutf8 (cp_id);
2435 #else
2436 (void) edit;
2437 #endif
2441 /* --------------------------------------------------------------------------------------------- */
2443 Recording stack for undo:
2444 The following is an implementation of a compressed stack. Identical
2445 pushes are recorded by a negative prefix indicating the number of times the
2446 same char was pushed. This saves space for repeated curs-left or curs-right
2447 delete etc.
2451 pushed: stored:
2455 b -3
2457 c --> -4
2463 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2464 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2465 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2466 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2467 position.
2469 The only way the cursor moves or the buffer is changed is through the routines:
2470 insert, backspace, insert_ahead, delete, and cursor_move.
2471 These record the reverse undo movements onto the stack each time they are
2472 called.
2474 Each key press results in a set of actions (insert; delete ...). So each time
2475 a key is pressed the current position of start_display is pushed as
2476 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2477 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2478 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2482 void
2483 edit_push_undo_action (WEdit * edit, long c, ...)
2485 unsigned long sp = edit->undo_stack_pointer;
2486 unsigned long spm1;
2487 long *t;
2489 /* first enlarge the stack if necessary */
2490 if (sp > edit->undo_stack_size - 10)
2491 { /* say */
2492 if (option_max_undo < 256)
2493 option_max_undo = 256;
2494 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2496 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2497 if (t)
2499 edit->undo_stack = t;
2500 edit->undo_stack_size <<= 1;
2501 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2505 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2506 if (edit->undo_stack_disable)
2508 edit_push_redo_action (edit, KEY_PRESS);
2509 edit_push_redo_action (edit, c);
2510 return;
2512 else if (edit->redo_stack_reset)
2514 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2517 if (edit->undo_stack_bottom != sp
2518 && spm1 != edit->undo_stack_bottom
2519 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2521 int d;
2522 if (edit->undo_stack[spm1] < 0)
2524 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2525 if (d == c)
2527 if (edit->undo_stack[spm1] > -1000000000)
2529 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2531 edit->undo_stack[spm1]--;
2533 return;
2537 else
2539 d = edit->undo_stack[spm1];
2540 if (d == c)
2542 if (c >= KEY_PRESS)
2543 return; /* --> no need to push multiple do-nothings */
2544 edit->undo_stack[sp] = -2;
2545 goto check_bottom;
2549 edit->undo_stack[sp] = c;
2551 check_bottom:
2552 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2554 /* if the sp wraps round and catches the undo_stack_bottom then erase
2555 * the first set of actions on the stack to make space - by moving
2556 * undo_stack_bottom forward one "key press" */
2557 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2558 if ((unsigned long) c == edit->undo_stack_bottom ||
2559 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2562 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2564 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2565 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2567 /*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: */
2568 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2569 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2571 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2575 void
2576 edit_push_redo_action (WEdit * edit, long c, ...)
2578 unsigned long sp = edit->redo_stack_pointer;
2579 unsigned long spm1;
2580 long *t;
2581 /* first enlarge the stack if necessary */
2582 if (sp > edit->redo_stack_size - 10)
2583 { /* say */
2584 if (option_max_undo < 256)
2585 option_max_undo = 256;
2586 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2588 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2589 if (t)
2591 edit->redo_stack = t;
2592 edit->redo_stack_size <<= 1;
2593 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2597 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2599 if (edit->redo_stack_bottom != sp
2600 && spm1 != edit->redo_stack_bottom
2601 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2603 int d;
2604 if (edit->redo_stack[spm1] < 0)
2606 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2607 if (d == c)
2609 if (edit->redo_stack[spm1] > -1000000000)
2611 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2612 edit->redo_stack[spm1]--;
2613 return;
2617 else
2619 d = edit->redo_stack[spm1];
2620 if (d == c)
2622 if (c >= KEY_PRESS)
2623 return; /* --> no need to push multiple do-nothings */
2624 edit->redo_stack[sp] = -2;
2625 goto redo_check_bottom;
2629 edit->redo_stack[sp] = c;
2631 redo_check_bottom:
2632 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2634 /* if the sp wraps round and catches the redo_stack_bottom then erase
2635 * the first set of actions on the stack to make space - by moving
2636 * redo_stack_bottom forward one "key press" */
2637 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2638 if ((unsigned long) c == edit->redo_stack_bottom ||
2639 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2642 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2644 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2645 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2648 * If a single key produced enough pushes to wrap all the way round then
2649 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2650 * The stack is then initialised:
2653 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2654 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2655 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2659 /* --------------------------------------------------------------------------------------------- */
2661 Basic low level single character buffer alterations and movements at the cursor.
2662 Returns char passed over, inserted or removed.
2665 void
2666 edit_insert (WEdit * edit, int c)
2668 /* check if file has grown to large */
2669 if (edit->last_byte >= SIZE_LIMIT)
2670 return;
2672 /* first we must update the position of the display window */
2673 if (edit->curs1 < edit->start_display)
2675 edit->start_display++;
2676 if (c == '\n')
2677 edit->start_line++;
2680 /* Mark file as modified, unless the file hasn't been fully loaded */
2681 if (edit->loading_done)
2683 edit_modification (edit);
2686 /* now we must update some info on the file and check if a redraw is required */
2687 if (c == '\n')
2689 if (edit->book_mark)
2690 book_mark_inc (edit, edit->curs_line);
2691 edit->curs_line++;
2692 edit->total_lines++;
2693 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2696 /* save the reverse command onto the undo stack */
2697 /* ordinary char and not space */
2698 if (c > 32)
2699 edit_push_undo_action (edit, BACKSPACE);
2700 else
2701 edit_push_undo_action (edit, BACKSPACE_BR);
2702 /* update markers */
2703 edit->mark1 += (edit->mark1 > edit->curs1);
2704 edit->mark2 += (edit->mark2 > edit->curs1);
2705 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2707 /* add a new buffer if we've reached the end of the last one */
2708 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2709 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2711 /* perform the insertion */
2712 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2713 = (unsigned char) c;
2715 /* update file length */
2716 edit->last_byte++;
2718 /* update cursor position */
2719 edit->curs1++;
2722 /* --------------------------------------------------------------------------------------------- */
2723 /** same as edit_insert and move left */
2725 void
2726 edit_insert_ahead (WEdit * edit, int c)
2728 if (edit->last_byte >= SIZE_LIMIT)
2729 return;
2731 if (edit->curs1 < edit->start_display)
2733 edit->start_display++;
2734 if (c == '\n')
2735 edit->start_line++;
2737 edit_modification (edit);
2738 if (c == '\n')
2740 if (edit->book_mark)
2741 book_mark_inc (edit, edit->curs_line);
2742 edit->total_lines++;
2743 edit->force |= REDRAW_AFTER_CURSOR;
2745 /* ordinary char and not space */
2746 if (c > 32)
2747 edit_push_undo_action (edit, DELCHAR);
2748 else
2749 edit_push_undo_action (edit, DELCHAR_BR);
2751 edit->mark1 += (edit->mark1 >= edit->curs1);
2752 edit->mark2 += (edit->mark2 >= edit->curs1);
2753 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2755 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2756 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2757 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2758 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2760 edit->last_byte++;
2761 edit->curs2++;
2765 /* --------------------------------------------------------------------------------------------- */
2768 edit_delete (WEdit * edit, const int byte_delete)
2770 int p = 0;
2771 int cw = 1;
2772 int i;
2774 if (!edit->curs2)
2775 return 0;
2777 cw = 1;
2778 /* if byte_delete = 1 then delete only one byte not multibyte char */
2779 if (edit->utf8 && byte_delete == 0)
2781 edit_get_utf (edit, edit->curs1, &cw);
2782 if (cw < 1)
2783 cw = 1;
2786 if (edit->mark2 != edit->mark1)
2787 edit_push_markers (edit);
2789 for (i = 1; i <= cw; i++)
2791 if (edit->mark1 > edit->curs1)
2793 edit->mark1--;
2794 edit->end_mark_curs--;
2796 if (edit->mark2 > edit->curs1)
2797 edit->mark2--;
2798 if (edit->last_get_rule > edit->curs1)
2799 edit->last_get_rule--;
2801 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2802 ((edit->curs2 -
2803 1) & M_EDIT_BUF_SIZE) - 1];
2805 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2807 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2808 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2810 edit->last_byte--;
2811 edit->curs2--;
2812 edit_push_undo_action (edit, p + 256);
2815 edit_modification (edit);
2816 if (p == '\n')
2818 if (edit->book_mark)
2819 book_mark_dec (edit, edit->curs_line);
2820 edit->total_lines--;
2821 edit->force |= REDRAW_AFTER_CURSOR;
2823 if (edit->curs1 < edit->start_display)
2825 edit->start_display--;
2826 if (p == '\n')
2827 edit->start_line--;
2830 return p;
2833 /* --------------------------------------------------------------------------------------------- */
2834 /** moves the cursor right or left: increment positive or negative respectively */
2836 void
2837 edit_cursor_move (WEdit * edit, long increment)
2839 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2840 int c;
2842 if (increment < 0)
2844 for (; increment < 0; increment++)
2846 if (!edit->curs1)
2847 return;
2849 edit_push_undo_action (edit, CURS_RIGHT);
2851 c = edit_get_byte (edit, edit->curs1 - 1);
2852 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2853 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2854 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2855 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2856 edit->curs2++;
2857 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2858 1) & M_EDIT_BUF_SIZE];
2859 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2861 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2862 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2864 edit->curs1--;
2865 if (c == '\n')
2867 edit->curs_line--;
2868 edit->force |= REDRAW_LINE_BELOW;
2873 else if (increment > 0)
2875 for (; increment > 0; increment--)
2877 if (!edit->curs2)
2878 return;
2880 edit_push_undo_action (edit, CURS_LEFT);
2882 c = edit_get_byte (edit, edit->curs1);
2883 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2884 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2885 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2886 edit->curs1++;
2887 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2888 ((edit->curs2 -
2889 1) & M_EDIT_BUF_SIZE) - 1];
2890 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2892 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2893 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2895 edit->curs2--;
2896 if (c == '\n')
2898 edit->curs_line++;
2899 edit->force |= REDRAW_LINE_ABOVE;
2905 /* These functions return positions relative to lines */
2907 /* --------------------------------------------------------------------------------------------- */
2908 /** returns index of last char on line + 1 */
2910 long
2911 edit_eol (WEdit * edit, long current)
2913 if (current >= edit->last_byte)
2914 return edit->last_byte;
2916 for (;; current++)
2917 if (edit_get_byte (edit, current) == '\n')
2918 break;
2919 return current;
2922 /* --------------------------------------------------------------------------------------------- */
2923 /** returns index of first char on line */
2925 long
2926 edit_bol (WEdit * edit, long current)
2928 if (current <= 0)
2929 return 0;
2931 for (;; current--)
2932 if (edit_get_byte (edit, current - 1) == '\n')
2933 break;
2934 return current;
2937 /* --------------------------------------------------------------------------------------------- */
2939 long
2940 edit_count_lines (WEdit * edit, long current, long upto)
2942 long lines = 0;
2943 if (upto > edit->last_byte)
2944 upto = edit->last_byte;
2945 if (current < 0)
2946 current = 0;
2947 while (current < upto)
2948 if (edit_get_byte (edit, current++) == '\n')
2949 lines++;
2950 return lines;
2953 /* --------------------------------------------------------------------------------------------- */
2954 /* If lines is zero this returns the count of lines from current to upto. */
2955 /* If upto is zero returns index of lines forward current. */
2957 long
2958 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2960 if (upto)
2962 return edit_count_lines (edit, current, upto);
2964 else
2966 long next;
2967 if (lines < 0)
2968 lines = 0;
2969 while (lines--)
2971 next = edit_eol (edit, current) + 1;
2972 if (next > edit->last_byte)
2973 break;
2974 else
2975 current = next;
2977 return current;
2981 /* --------------------------------------------------------------------------------------------- */
2982 /** Returns offset of 'lines' lines up from current */
2984 long
2985 edit_move_backward (WEdit * edit, long current, long lines)
2987 if (lines < 0)
2988 lines = 0;
2989 current = edit_bol (edit, current);
2990 while ((lines--) && current != 0)
2991 current = edit_bol (edit, current - 1);
2992 return current;
2995 /* --------------------------------------------------------------------------------------------- */
2996 /* If cols is zero this returns the count of columns from current to upto. */
2997 /* If upto is zero returns index of cols across from current. */
2999 long
3000 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3002 long p, q;
3003 int col;
3005 if (upto)
3007 q = upto;
3008 cols = -10;
3010 else
3011 q = edit->last_byte + 2;
3013 for (col = 0, p = current; p < q; p++)
3015 int c, orig_c;
3017 if (cols != -10)
3019 if (col == cols)
3020 return p;
3021 if (col > cols)
3022 return p - 1;
3025 orig_c = c = edit_get_byte (edit, p);
3027 #ifdef HAVE_CHARSET
3028 if (edit->utf8)
3030 int utf_ch;
3031 int cw = 1;
3033 utf_ch = edit_get_utf (edit, p, &cw);
3034 if (mc_global.utf8_display)
3036 if (cw > 1)
3037 col -= cw - 1;
3038 if (g_unichar_iswide (utf_ch))
3039 col++;
3041 else if (cw > 1 && g_unichar_isprint (utf_ch))
3042 col -= cw - 1;
3045 c = convert_to_display_c (c);
3046 #endif
3048 if (c == '\t')
3049 col += TAB_SIZE - col % TAB_SIZE;
3050 else if (c == '\n')
3052 if (upto)
3053 return col;
3054 else
3055 return p;
3057 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3058 /* '\r' is shown as ^M, so we must advance 2 characters */
3059 /* Caret notation for control characters */
3060 col += 2;
3061 else
3062 col++;
3064 return col;
3067 /* --------------------------------------------------------------------------------------------- */
3068 /** returns the current column position of the cursor */
3071 edit_get_col (WEdit * edit)
3073 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3076 /* --------------------------------------------------------------------------------------------- */
3077 /* Scrolling functions */
3078 /* --------------------------------------------------------------------------------------------- */
3080 void
3081 edit_update_curs_row (WEdit * edit)
3083 edit->curs_row = edit->curs_line - edit->start_line;
3086 /* --------------------------------------------------------------------------------------------- */
3088 void
3089 edit_update_curs_col (WEdit * edit)
3091 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3094 /* --------------------------------------------------------------------------------------------- */
3097 edit_get_curs_col (const WEdit * edit)
3099 return edit->curs_col;
3102 /* --------------------------------------------------------------------------------------------- */
3103 /** moves the display start position up by i lines */
3105 void
3106 edit_scroll_upward (WEdit * edit, unsigned long i)
3108 unsigned long lines_above = edit->start_line;
3109 if (i > lines_above)
3110 i = lines_above;
3111 if (i)
3113 edit->start_line -= i;
3114 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3115 edit->force |= REDRAW_PAGE;
3116 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3118 edit_update_curs_row (edit);
3122 /* --------------------------------------------------------------------------------------------- */
3123 /** returns 1 if could scroll, 0 otherwise */
3125 void
3126 edit_scroll_downward (WEdit * edit, int i)
3128 int lines_below;
3129 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3130 if (lines_below > 0)
3132 if (i > lines_below)
3133 i = lines_below;
3134 edit->start_line += i;
3135 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3136 edit->force |= REDRAW_PAGE;
3137 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3139 edit_update_curs_row (edit);
3142 /* --------------------------------------------------------------------------------------------- */
3144 void
3145 edit_scroll_right (WEdit * edit, int i)
3147 edit->force |= REDRAW_PAGE;
3148 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3149 edit->start_col -= i;
3152 /* --------------------------------------------------------------------------------------------- */
3154 void
3155 edit_scroll_left (WEdit * edit, int i)
3157 if (edit->start_col)
3159 edit->start_col += i;
3160 if (edit->start_col > 0)
3161 edit->start_col = 0;
3162 edit->force |= REDRAW_PAGE;
3163 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3167 /* --------------------------------------------------------------------------------------------- */
3168 /* high level cursor movement commands */
3169 /* --------------------------------------------------------------------------------------------- */
3171 void
3172 edit_move_to_prev_col (WEdit * edit, long p)
3174 int prev = edit->prev_col;
3175 int over = edit->over_col;
3176 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3178 if (option_cursor_beyond_eol)
3180 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3181 edit_eol (edit, edit->curs1));
3183 if (line_len < prev + edit->over_col)
3185 edit->over_col = prev + over - line_len;
3186 edit->prev_col = line_len;
3187 edit->curs_col = line_len;
3189 else
3191 edit->curs_col = prev + over;
3192 edit->prev_col = edit->curs_col;
3193 edit->over_col = 0;
3196 else
3198 edit->over_col = 0;
3199 if (is_in_indent (edit) && option_fake_half_tabs)
3201 edit_update_curs_col (edit);
3202 if (space_width)
3203 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3205 int q = edit->curs_col;
3206 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3207 p = edit_bol (edit, edit->curs1);
3208 edit_cursor_move (edit,
3209 edit_move_forward3 (edit, p, edit->curs_col,
3210 0) - edit->curs1);
3211 if (!left_of_four_spaces (edit))
3212 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3218 /* --------------------------------------------------------------------------------------------- */
3221 line_is_blank (WEdit * edit, long line)
3223 return is_blank (edit, edit_find_line (edit, line));
3226 /* --------------------------------------------------------------------------------------------- */
3227 /** move cursor to line 'line' */
3229 void
3230 edit_move_to_line (WEdit * e, long line)
3232 if (line < e->curs_line)
3233 edit_move_up (e, e->curs_line - line, 0);
3234 else
3235 edit_move_down (e, line - e->curs_line, 0);
3236 edit_scroll_screen_over_cursor (e);
3239 /* --------------------------------------------------------------------------------------------- */
3240 /** scroll window so that first visible line is 'line' */
3242 void
3243 edit_move_display (WEdit * e, long line)
3245 if (line < e->start_line)
3246 edit_scroll_upward (e, e->start_line - line);
3247 else
3248 edit_scroll_downward (e, line - e->start_line);
3251 /* --------------------------------------------------------------------------------------------- */
3252 /** save markers onto undo stack */
3254 void
3255 edit_push_markers (WEdit * edit)
3257 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3258 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3259 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3262 /* --------------------------------------------------------------------------------------------- */
3264 void
3265 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3267 edit->mark1 = m1;
3268 edit->mark2 = m2;
3269 edit->column1 = c1;
3270 edit->column2 = c2;
3274 /* --------------------------------------------------------------------------------------------- */
3275 /** highlight marker toggle */
3277 void
3278 edit_mark_cmd (WEdit * edit, int unmark)
3280 edit_push_markers (edit);
3281 if (unmark)
3283 edit_set_markers (edit, 0, 0, 0, 0);
3284 edit->force |= REDRAW_PAGE;
3286 else
3288 if (edit->mark2 >= 0)
3290 edit->end_mark_curs = -1;
3291 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3292 edit->curs_col + edit->over_col);
3293 edit->force |= REDRAW_PAGE;
3295 else
3297 edit->end_mark_curs = edit->curs1;
3298 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3299 edit->curs_col + edit->over_col);
3304 /* --------------------------------------------------------------------------------------------- */
3305 /** highlight the word under cursor */
3307 void
3308 edit_mark_current_word_cmd (WEdit * edit)
3310 long pos;
3312 for (pos = edit->curs1; pos != 0; pos--)
3314 int c1, c2;
3316 c1 = edit_get_byte (edit, pos);
3317 c2 = edit_get_byte (edit, pos - 1);
3318 if (!isspace (c1) && isspace (c2))
3319 break;
3320 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3321 break;
3323 edit->mark1 = pos;
3325 for (; pos < edit->last_byte; pos++)
3327 int c1, c2;
3329 c1 = edit_get_byte (edit, pos);
3330 c2 = edit_get_byte (edit, pos + 1);
3331 if (!isspace (c1) && isspace (c2))
3332 break;
3333 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3334 break;
3336 edit->mark2 = min (pos + 1, edit->last_byte);
3338 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3341 /* --------------------------------------------------------------------------------------------- */
3343 void
3344 edit_mark_current_line_cmd (WEdit * edit)
3346 long pos = edit->curs1;
3348 edit->mark1 = edit_bol (edit, pos);
3349 edit->mark2 = edit_eol (edit, pos);
3351 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3354 /* --------------------------------------------------------------------------------------------- */
3356 void
3357 edit_delete_line (WEdit * edit)
3360 * Delete right part of the line.
3361 * Note that edit_get_byte() returns '\n' when byte position is
3362 * beyond EOF.
3364 while (edit_get_byte (edit, edit->curs1) != '\n')
3366 (void) edit_delete (edit, 1);
3370 * Delete '\n' char.
3371 * Note that edit_delete() will not corrupt anything if called while
3372 * cursor position is EOF.
3374 (void) edit_delete (edit, 1);
3377 * Delete left part of the line.
3378 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3380 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3382 (void) edit_backspace (edit, 1);
3386 /* --------------------------------------------------------------------------------------------- */
3389 edit_indent_width (WEdit * edit, long p)
3391 long q = p;
3392 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3393 q++;
3394 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3397 /* --------------------------------------------------------------------------------------------- */
3399 void
3400 edit_insert_indent (WEdit * edit, int indent)
3402 if (!option_fill_tabs_with_spaces)
3404 while (indent >= TAB_SIZE)
3406 edit_insert (edit, '\t');
3407 indent -= TAB_SIZE;
3410 while (indent-- > 0)
3411 edit_insert (edit, ' ');
3414 /* --------------------------------------------------------------------------------------------- */
3416 void
3417 edit_push_key_press (WEdit * edit)
3419 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3420 if (edit->mark2 == -1)
3422 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3423 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3427 /* --------------------------------------------------------------------------------------------- */
3429 void
3430 edit_find_bracket (WEdit * edit)
3432 edit->bracket = edit_get_bracket (edit, 1, 10000);
3433 if (last_bracket != edit->bracket)
3434 edit->force |= REDRAW_PAGE;
3435 last_bracket = edit->bracket;
3438 /* --------------------------------------------------------------------------------------------- */
3440 * This executes a command as though the user initiated it through a key
3441 * press. Callback with WIDGET_KEY as a message calls this after
3442 * translating the key press. This function can be used to pass any
3443 * command to the editor. Note that the screen wouldn't update
3444 * automatically. Either of command or char_for_insertion must be
3445 * passed as -1. Commands are executed, and char_for_insertion is
3446 * inserted at the cursor.
3449 void
3450 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3452 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3453 || (macro_index < 0
3454 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3456 macro_index = 0;
3457 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3458 return;
3460 if (macro_index != -1)
3462 edit->force |= REDRAW_COMPLETELY;
3463 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3465 edit_store_macro_cmd (edit);
3466 macro_index = -1;
3467 return;
3469 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3471 edit_repeat_macro_cmd (edit);
3472 macro_index = -1;
3473 return;
3477 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3479 record_macro_buf[macro_index].action = command;
3480 record_macro_buf[macro_index++].ch = char_for_insertion;
3482 /* record the beginning of a set of editing actions initiated by a key press */
3483 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3484 edit_push_key_press (edit);
3486 edit_execute_cmd (edit, command, char_for_insertion);
3487 if (edit->column_highlight)
3488 edit->force |= REDRAW_PAGE;
3491 /* --------------------------------------------------------------------------------------------- */
3493 This executes a command at a lower level than macro recording.
3494 It also does not push a key_press onto the undo stack. This means
3495 that if it is called many times, a single undo command will undo
3496 all of them. It also does not check for the Undo command.
3498 void
3499 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3501 edit->force |= REDRAW_LINE;
3503 /* The next key press will unhighlight the found string, so update
3504 * the whole page */
3505 if (edit->found_len || edit->column_highlight)
3506 edit->force |= REDRAW_PAGE;
3508 switch (command)
3510 /* a mark command with shift-arrow */
3511 case CK_MarkLeft:
3512 case CK_MarkRight:
3513 case CK_MarkToWordBegin:
3514 case CK_MarkToWordEnd:
3515 case CK_MarkToHome:
3516 case CK_MarkToEnd:
3517 case CK_MarkUp:
3518 case CK_MarkDown:
3519 case CK_MarkPageUp:
3520 case CK_MarkPageDown:
3521 case CK_MarkToFileBegin:
3522 case CK_MarkToFileEnd:
3523 case CK_MarkToPageBegin:
3524 case CK_MarkToPageEnd:
3525 case CK_MarkScrollUp:
3526 case CK_MarkScrollDown:
3527 case CK_MarkParagraphUp:
3528 case CK_MarkParagraphDown:
3529 /* a mark command with alt-arrow */
3530 case CK_MarkColumnPageUp:
3531 case CK_MarkColumnPageDown:
3532 case CK_MarkColumnLeft:
3533 case CK_MarkColumnRight:
3534 case CK_MarkColumnUp:
3535 case CK_MarkColumnDown:
3536 case CK_MarkColumnScrollUp:
3537 case CK_MarkColumnScrollDown:
3538 case CK_MarkColumnParagraphUp:
3539 case CK_MarkColumnParagraphDown:
3540 edit->column_highlight = 0;
3541 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3543 edit_mark_cmd (edit, 1); /* clear */
3544 edit_mark_cmd (edit, 0); /* marking on */
3546 edit->highlight = 1;
3547 break;
3549 /* any other command */
3550 default:
3551 if (edit->highlight)
3552 edit_mark_cmd (edit, 0); /* clear */
3553 edit->highlight = 0;
3556 /* first check for undo */
3557 if (command == CK_Undo)
3559 edit->redo_stack_reset = 0;
3560 edit_group_undo (edit);
3561 edit->found_len = 0;
3562 edit->prev_col = edit_get_col (edit);
3563 edit->search_start = edit->curs1;
3564 return;
3566 /* check for redo */
3567 if (command == CK_Redo)
3569 edit->redo_stack_reset = 0;
3570 edit_do_redo (edit);
3571 edit->found_len = 0;
3572 edit->prev_col = edit_get_col (edit);
3573 edit->search_start = edit->curs1;
3574 return;
3577 edit->redo_stack_reset = 1;
3579 /* An ordinary key press */
3580 if (char_for_insertion >= 0)
3582 /* if non persistent selection and text selected */
3583 if (!option_persistent_selections)
3585 if (edit->mark1 != edit->mark2)
3586 edit_block_delete_cmd (edit);
3588 if (edit->overwrite)
3590 /* remove char only one time, after input first byte, multibyte chars */
3591 if ((!mc_global.utf8_display || edit->charpoint == 0)
3592 && edit_get_byte (edit, edit->curs1) != '\n')
3593 edit_delete (edit, 0);
3595 if (option_cursor_beyond_eol && edit->over_col > 0)
3596 edit_insert_over (edit);
3597 #ifdef HAVE_CHARSET
3598 if (char_for_insertion > 255 && mc_global.utf8_display == 0)
3600 unsigned char str[6 + 1];
3601 size_t i = 0;
3602 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3603 if (res == 0)
3605 str[0] = '.';
3606 str[1] = '\0';
3608 else
3610 str[res] = '\0';
3612 while (str[i] != 0 && i <= 6)
3614 char_for_insertion = str[i];
3615 edit_insert (edit, char_for_insertion);
3616 i++;
3619 else
3620 #endif
3621 edit_insert (edit, char_for_insertion);
3623 if (option_auto_para_formatting)
3625 format_paragraph (edit, 0);
3626 edit->force |= REDRAW_PAGE;
3628 else
3629 check_and_wrap_line (edit);
3630 edit->found_len = 0;
3631 edit->prev_col = edit_get_col (edit);
3632 edit->search_start = edit->curs1;
3633 edit_find_bracket (edit);
3634 return;
3637 switch (command)
3639 case CK_TopOnScreen:
3640 case CK_BottomOnScreen:
3641 case CK_Top:
3642 case CK_Bottom:
3643 case CK_PageUp:
3644 case CK_PageDown:
3645 case CK_Home:
3646 case CK_End:
3647 case CK_Up:
3648 case CK_Down:
3649 case CK_Left:
3650 case CK_Right:
3651 case CK_WordLeft:
3652 case CK_WordRight:
3653 if (edit->mark2 >= 0)
3655 if (!option_persistent_selections)
3657 if (edit->column_highlight)
3658 edit_push_undo_action (edit, COLUMN_ON);
3659 edit->column_highlight = 0;
3660 edit_mark_cmd (edit, 1);
3665 switch (command)
3667 case CK_TopOnScreen:
3668 case CK_BottomOnScreen:
3669 case CK_MarkToPageBegin:
3670 case CK_MarkToPageEnd:
3671 case CK_Up:
3672 case CK_Down:
3673 case CK_WordLeft:
3674 case CK_WordRight:
3675 case CK_MarkToWordBegin:
3676 case CK_MarkToWordEnd:
3677 case CK_MarkUp:
3678 case CK_MarkDown:
3679 case CK_MarkColumnUp:
3680 case CK_MarkColumnDown:
3681 if (edit->mark2 == -1)
3682 break; /*marking is following the cursor: may need to highlight a whole line */
3683 case CK_Left:
3684 case CK_Right:
3685 case CK_MarkLeft:
3686 case CK_MarkRight:
3687 edit->force |= REDRAW_CHAR_ONLY;
3690 /* basic cursor key commands */
3691 switch (command)
3693 case CK_BackSpace:
3694 /* if non persistent selection and text selected */
3695 if (!option_persistent_selections)
3697 if (edit->mark1 != edit->mark2)
3699 edit_block_delete_cmd (edit);
3700 break;
3703 if (option_cursor_beyond_eol && edit->over_col > 0)
3705 edit->over_col--;
3706 break;
3708 if (option_backspace_through_tabs && is_in_indent (edit))
3710 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3711 edit_backspace (edit, 1);
3712 break;
3714 else
3716 if (option_fake_half_tabs)
3718 int i;
3719 if (is_in_indent (edit) && right_of_four_spaces (edit))
3721 for (i = 0; i < HALF_TAB_SIZE; i++)
3722 edit_backspace (edit, 1);
3723 break;
3727 edit_backspace (edit, 0);
3728 break;
3729 case CK_Delete:
3730 /* if non persistent selection and text selected */
3731 if (!option_persistent_selections)
3733 if (edit->mark1 != edit->mark2)
3735 edit_block_delete_cmd (edit);
3736 break;
3740 if (option_cursor_beyond_eol && edit->over_col > 0)
3741 edit_insert_over (edit);
3743 if (option_fake_half_tabs)
3745 int i;
3746 if (is_in_indent (edit) && left_of_four_spaces (edit))
3748 for (i = 1; i <= HALF_TAB_SIZE; i++)
3749 edit_delete (edit, 1);
3750 break;
3753 edit_delete (edit, 0);
3754 break;
3755 case CK_DeleteToWordBegin:
3756 edit->over_col = 0;
3757 edit_left_delete_word (edit);
3758 break;
3759 case CK_DeleteToWordEnd:
3760 if (option_cursor_beyond_eol && edit->over_col > 0)
3761 edit_insert_over (edit);
3763 edit_right_delete_word (edit);
3764 break;
3765 case CK_DeleteLine:
3766 edit_delete_line (edit);
3767 break;
3768 case CK_DeleteToHome:
3769 edit_delete_to_line_begin (edit);
3770 break;
3771 case CK_DeleteToEnd:
3772 edit_delete_to_line_end (edit);
3773 break;
3774 case CK_Enter:
3775 edit->over_col = 0;
3776 if (option_auto_para_formatting)
3778 edit_double_newline (edit);
3779 if (option_return_does_auto_indent)
3780 edit_auto_indent (edit);
3781 format_paragraph (edit, 0);
3783 else
3785 edit_insert (edit, '\n');
3786 if (option_return_does_auto_indent)
3788 edit_auto_indent (edit);
3791 break;
3792 case CK_Return:
3793 edit_insert (edit, '\n');
3794 break;
3796 case CK_MarkColumnPageUp:
3797 edit->column_highlight = 1;
3798 case CK_PageUp:
3799 case CK_MarkPageUp:
3800 edit_move_up (edit, edit->widget.lines - 1, 1);
3801 break;
3802 case CK_MarkColumnPageDown:
3803 edit->column_highlight = 1;
3804 case CK_PageDown:
3805 case CK_MarkPageDown:
3806 edit_move_down (edit, edit->widget.lines - 1, 1);
3807 break;
3808 case CK_MarkColumnLeft:
3809 edit->column_highlight = 1;
3810 case CK_Left:
3811 case CK_MarkLeft:
3812 if (option_fake_half_tabs)
3814 if (is_in_indent (edit) && right_of_four_spaces (edit))
3816 if (option_cursor_beyond_eol && edit->over_col > 0)
3817 edit->over_col--;
3818 else
3819 edit_cursor_move (edit, -HALF_TAB_SIZE);
3820 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3821 break;
3824 edit_left_char_move_cmd (edit);
3825 break;
3826 case CK_MarkColumnRight:
3827 edit->column_highlight = 1;
3828 case CK_Right:
3829 case CK_MarkRight:
3830 if (option_fake_half_tabs)
3832 if (is_in_indent (edit) && left_of_four_spaces (edit))
3834 edit_cursor_move (edit, HALF_TAB_SIZE);
3835 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3836 break;
3839 edit_right_char_move_cmd (edit);
3840 break;
3841 case CK_TopOnScreen:
3842 case CK_MarkToPageBegin:
3843 edit_begin_page (edit);
3844 break;
3845 case CK_BottomOnScreen:
3846 case CK_MarkToPageEnd:
3847 edit_end_page (edit);
3848 break;
3849 case CK_WordLeft:
3850 case CK_MarkToWordBegin:
3851 edit->over_col = 0;
3852 edit_left_word_move_cmd (edit);
3853 break;
3854 case CK_WordRight:
3855 case CK_MarkToWordEnd:
3856 edit->over_col = 0;
3857 edit_right_word_move_cmd (edit);
3858 break;
3859 case CK_MarkColumnUp:
3860 edit->column_highlight = 1;
3861 case CK_Up:
3862 case CK_MarkUp:
3863 edit_move_up (edit, 1, 0);
3864 break;
3865 case CK_MarkColumnDown:
3866 edit->column_highlight = 1;
3867 case CK_Down:
3868 case CK_MarkDown:
3869 edit_move_down (edit, 1, 0);
3870 break;
3871 case CK_MarkColumnParagraphUp:
3872 edit->column_highlight = 1;
3873 case CK_ParagraphUp:
3874 case CK_MarkParagraphUp:
3875 edit_move_up_paragraph (edit, 0);
3876 break;
3877 case CK_MarkColumnParagraphDown:
3878 edit->column_highlight = 1;
3879 case CK_ParagraphDown:
3880 case CK_MarkParagraphDown:
3881 edit_move_down_paragraph (edit, 0);
3882 break;
3883 case CK_MarkColumnScrollUp:
3884 edit->column_highlight = 1;
3885 case CK_ScrollUp:
3886 case CK_MarkScrollUp:
3887 edit_move_up (edit, 1, 1);
3888 break;
3889 case CK_MarkColumnScrollDown:
3890 edit->column_highlight = 1;
3891 case CK_ScrollDown:
3892 case CK_MarkScrollDown:
3893 edit_move_down (edit, 1, 1);
3894 break;
3895 case CK_Home:
3896 case CK_MarkToHome:
3897 edit_cursor_to_bol (edit);
3898 break;
3899 case CK_End:
3900 case CK_MarkToEnd:
3901 edit_cursor_to_eol (edit);
3902 break;
3903 case CK_Tab:
3904 /* if text marked shift block */
3905 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3907 if (edit->mark2 < 0)
3908 edit_mark_cmd (edit, 0);
3909 edit_move_block_to_right (edit);
3911 else
3913 if (option_cursor_beyond_eol)
3914 edit_insert_over (edit);
3915 edit_tab_cmd (edit);
3916 if (option_auto_para_formatting)
3918 format_paragraph (edit, 0);
3919 edit->force |= REDRAW_PAGE;
3921 else
3923 check_and_wrap_line (edit);
3926 break;
3928 case CK_InsertOverwrite:
3929 edit->overwrite = !edit->overwrite;
3930 break;
3932 case CK_Mark:
3933 if (edit->mark2 >= 0)
3935 if (edit->column_highlight)
3936 edit_push_undo_action (edit, COLUMN_ON);
3937 edit->column_highlight = 0;
3939 edit_mark_cmd (edit, 0);
3940 break;
3941 case CK_MarkColumn:
3942 if (!edit->column_highlight)
3943 edit_push_undo_action (edit, COLUMN_OFF);
3944 edit->column_highlight = 1;
3945 edit_mark_cmd (edit, 0);
3946 break;
3947 case CK_MarkAll:
3948 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3949 edit->force |= REDRAW_PAGE;
3950 break;
3951 case CK_Unmark:
3952 if (edit->column_highlight)
3953 edit_push_undo_action (edit, COLUMN_ON);
3954 edit->column_highlight = 0;
3955 edit_mark_cmd (edit, 1);
3956 break;
3957 case CK_MarkWord:
3958 if (edit->column_highlight)
3959 edit_push_undo_action (edit, COLUMN_ON);
3960 edit->column_highlight = 0;
3961 edit_mark_current_word_cmd (edit);
3962 break;
3963 case CK_MarkLine:
3964 if (edit->column_highlight)
3965 edit_push_undo_action (edit, COLUMN_ON);
3966 edit->column_highlight = 0;
3967 edit_mark_current_line_cmd (edit);
3968 break;
3970 case CK_ShowNumbers:
3971 option_line_state = !option_line_state;
3972 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
3973 edit->force |= REDRAW_PAGE;
3974 break;
3976 case CK_ShowMargin:
3977 show_right_margin = !show_right_margin;
3978 edit->force |= REDRAW_PAGE;
3979 break;
3981 case CK_HighlightOccurences:
3982 edit_highlight_occurences (edit);
3983 edit->force |= REDRAW_PAGE;
3984 break;
3986 case CK_UnhighlightOccurences:
3987 edit_unhighlight_occurences (edit);
3988 edit->force |= REDRAW_PAGE;
3989 break;
3991 case CK_Bookmark:
3992 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3993 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3994 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3995 else
3996 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3997 break;
3998 case CK_BookmarkFlush:
3999 book_mark_flush (edit, BOOK_MARK_COLOR);
4000 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4001 edit->force |= REDRAW_PAGE;
4002 break;
4003 case CK_BookmarkNext:
4004 if (edit->book_mark)
4006 struct _book_mark *p;
4007 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4008 if (p->next)
4010 p = p->next;
4011 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4012 edit_move_display (edit, p->line - edit->widget.lines / 2);
4013 edit_move_to_line (edit, p->line);
4016 break;
4017 case CK_BookmarkPrev:
4018 if (edit->book_mark)
4020 struct _book_mark *p;
4021 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4022 while (p->line == edit->curs_line)
4023 if (p->prev)
4024 p = p->prev;
4025 if (p->line >= 0)
4027 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4028 edit_move_display (edit, p->line - edit->widget.lines / 2);
4029 edit_move_to_line (edit, p->line);
4032 break;
4034 case CK_Top:
4035 case CK_MarkToFileBegin:
4036 edit_move_to_top (edit);
4037 break;
4038 case CK_Bottom:
4039 case CK_MarkToFileEnd:
4040 edit_move_to_bottom (edit);
4041 break;
4043 case CK_Copy:
4044 if (option_cursor_beyond_eol && edit->over_col > 0)
4045 edit_insert_over (edit);
4046 edit_block_copy_cmd (edit);
4047 break;
4048 case CK_Remove:
4049 edit_block_delete_cmd (edit);
4050 break;
4051 case CK_Move:
4052 edit_block_move_cmd (edit);
4053 break;
4055 case CK_BlockShiftLeft:
4056 if (edit->mark1 != edit->mark2)
4057 edit_move_block_to_left (edit);
4058 break;
4059 case CK_BlockShiftRight:
4060 if (edit->mark1 != edit->mark2)
4061 edit_move_block_to_right (edit);
4062 break;
4063 case CK_Store:
4064 edit_copy_to_X_buf_cmd (edit);
4065 break;
4066 case CK_Cut:
4067 edit_cut_to_X_buf_cmd (edit);
4068 break;
4069 case CK_Paste:
4070 /* if non persistent selection and text selected */
4071 if (!option_persistent_selections)
4073 if (edit->mark1 != edit->mark2)
4074 edit_block_delete_cmd (edit);
4076 if (option_cursor_beyond_eol && edit->over_col > 0)
4077 edit_insert_over (edit);
4078 edit_paste_from_X_buf_cmd (edit);
4079 break;
4080 case CK_History:
4081 edit_paste_from_history (edit);
4082 break;
4084 case CK_SaveAs:
4085 edit_save_as_cmd (edit);
4086 break;
4087 case CK_Save:
4088 edit_save_confirm_cmd (edit);
4089 break;
4090 case CK_EditFile:
4091 edit_load_cmd (edit, EDIT_FILE_COMMON);
4092 break;
4093 case CK_BlockSave:
4094 edit_save_block_cmd (edit);
4095 break;
4096 case CK_InsertFile:
4097 edit_insert_file_cmd (edit);
4098 break;
4100 case CK_FilePrev:
4101 edit_load_back_cmd (edit);
4102 break;
4103 case CK_FileNext:
4104 edit_load_forward_cmd (edit);
4105 break;
4107 case CK_EditSyntaxFile:
4108 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4109 break;
4110 case CK_SyntaxChoose:
4111 edit_syntax_dialog (edit);
4112 break;
4114 case CK_EditUserMenu:
4115 edit_load_cmd (edit, EDIT_FILE_MENU);
4116 break;
4118 case CK_SyntaxOnOff:
4119 option_syntax_highlighting ^= 1;
4120 if (option_syntax_highlighting == 1)
4121 edit_load_syntax (edit, NULL, edit->syntax_type);
4122 edit->force |= REDRAW_PAGE;
4123 break;
4125 case CK_ShowTabTws:
4126 enable_show_tabs_tws ^= 1;
4127 edit->force |= REDRAW_PAGE;
4128 break;
4130 case CK_Search:
4131 edit_search_cmd (edit, FALSE);
4132 break;
4133 case CK_SearchContinue:
4134 edit_search_cmd (edit, TRUE);
4135 break;
4136 case CK_Replace:
4137 edit_replace_cmd (edit, 0);
4138 break;
4139 case CK_ReplaceContinue:
4140 edit_replace_cmd (edit, 1);
4141 break;
4142 case CK_Complete:
4143 /* if text marked shift block */
4144 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4146 edit_move_block_to_left (edit);
4148 else
4150 edit_complete_word_cmd (edit);
4152 break;
4153 case CK_Find:
4154 edit_get_match_keyword_cmd (edit);
4155 break;
4156 case CK_Quit:
4157 dlg_stop (edit->widget.owner);
4158 break;
4159 case CK_EditNew:
4160 edit_new_cmd (edit);
4161 break;
4162 case CK_Help:
4163 edit_help_cmd (edit);
4164 break;
4165 case CK_Refresh:
4166 edit_refresh_cmd (edit);
4167 break;
4168 case CK_SaveSetup:
4169 save_setup_cmd ();
4170 break;
4171 case CK_About:
4172 edit_about ();
4173 break;
4174 case CK_LearnKeys:
4175 learn_keys ();
4176 break;
4177 case CK_Options:
4178 edit_options_dialog (edit);
4179 break;
4180 case CK_OptionsSaveMode:
4181 menu_save_mode_cmd ();
4182 break;
4183 case CK_Date:
4185 char s[BUF_MEDIUM];
4186 /* fool gcc to prevent a Y2K warning */
4187 char time_format[] = "_c";
4188 time_format[0] = '%';
4190 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4191 edit_print_string (edit, s);
4192 edit->force |= REDRAW_PAGE;
4193 break;
4195 break;
4196 case CK_Goto:
4197 edit_goto_cmd (edit);
4198 break;
4199 case CK_ParagraphFormat:
4200 format_paragraph (edit, 1);
4201 edit->force |= REDRAW_PAGE;
4202 break;
4203 case CK_MacroDelete:
4204 edit_delete_macro_cmd (edit);
4205 break;
4206 case CK_MatchBracket:
4207 edit_goto_matching_bracket (edit);
4208 break;
4209 case CK_UserMenu:
4210 user_menu (edit, NULL, -1);
4211 break;
4212 case CK_Sort:
4213 edit_sort_cmd (edit);
4214 break;
4215 case CK_ExternalCommand:
4216 edit_ext_cmd (edit);
4217 break;
4218 case CK_Mail:
4219 edit_mail_dialog (edit);
4220 break;
4221 case CK_Shell:
4222 view_other_cmd ();
4223 break;
4224 #ifdef HAVE_CHARSET
4225 case CK_SelectCodepage:
4226 edit_select_codepage_cmd (edit);
4227 break;
4228 #endif
4229 case CK_InsertLiteral:
4230 edit_insert_literal_cmd (edit);
4231 break;
4232 case CK_MacroStartStopRecord:
4233 edit_begin_end_macro_cmd (edit);
4234 break;
4235 case CK_RepeatStartStopRecord:
4236 edit_begin_end_repeat_cmd (edit);
4237 break;
4238 case CK_ExtendedKeyMap:
4239 edit->extmod = TRUE;
4240 break;
4241 default:
4242 break;
4245 /* CK_PipeBlock */
4246 if ((command / CK_PipeBlock (0)) == 1)
4247 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4249 /* keys which must set the col position, and the search vars */
4250 switch (command)
4252 case CK_Search:
4253 case CK_SearchContinue:
4254 case CK_Replace:
4255 case CK_ReplaceContinue:
4256 case CK_Complete:
4257 edit->prev_col = edit_get_col (edit);
4258 break;
4259 case CK_Up:
4260 case CK_MarkUp:
4261 case CK_MarkColumnUp:
4262 case CK_Down:
4263 case CK_MarkDown:
4264 case CK_MarkColumnDown:
4265 case CK_PageUp:
4266 case CK_MarkPageUp:
4267 case CK_MarkColumnPageUp:
4268 case CK_PageDown:
4269 case CK_MarkPageDown:
4270 case CK_MarkColumnPageDown:
4271 case CK_Top:
4272 case CK_MarkToFileBegin:
4273 case CK_Bottom:
4274 case CK_MarkToFileEnd:
4275 case CK_ParagraphUp:
4276 case CK_MarkParagraphUp:
4277 case CK_MarkColumnParagraphUp:
4278 case CK_ParagraphDown:
4279 case CK_MarkParagraphDown:
4280 case CK_MarkColumnParagraphDown:
4281 case CK_ScrollUp:
4282 case CK_MarkScrollUp:
4283 case CK_MarkColumnScrollUp:
4284 case CK_ScrollDown:
4285 case CK_MarkScrollDown:
4286 case CK_MarkColumnScrollDown:
4287 edit->search_start = edit->curs1;
4288 edit->found_len = 0;
4289 break;
4290 default:
4291 edit->found_len = 0;
4292 edit->prev_col = edit_get_col (edit);
4293 edit->search_start = edit->curs1;
4295 edit_find_bracket (edit);
4297 if (option_auto_para_formatting)
4299 switch (command)
4301 case CK_BackSpace:
4302 case CK_Delete:
4303 case CK_DeleteToWordBegin:
4304 case CK_DeleteToWordEnd:
4305 case CK_DeleteToHome:
4306 case CK_DeleteToEnd:
4307 format_paragraph (edit, 0);
4308 edit->force |= REDRAW_PAGE;
4313 /* --------------------------------------------------------------------------------------------- */
4315 void
4316 edit_stack_init (void)
4318 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4320 edit_history_moveto[edit_stack_iterator].filename = NULL;
4321 edit_history_moveto[edit_stack_iterator].line = -1;
4324 edit_stack_iterator = 0;
4327 /* --------------------------------------------------------------------------------------------- */
4329 void
4330 edit_stack_free (void)
4332 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4333 g_free (edit_history_moveto[edit_stack_iterator].filename);
4336 /* --------------------------------------------------------------------------------------------- */
4337 /** move i lines */
4339 void
4340 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4342 edit_move_updown (edit, i, do_scroll, TRUE);
4345 /* --------------------------------------------------------------------------------------------- */
4346 /** move i lines */
4348 void
4349 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4351 edit_move_updown (edit, i, do_scroll, FALSE);
4354 /* --------------------------------------------------------------------------------------------- */
4356 unsigned int
4357 edit_unlock_file (WEdit * edit)
4359 char *fullpath;
4360 unsigned int ret;
4362 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4363 ret = unlock_file (fullpath);
4364 g_free (fullpath);
4366 return ret;
4369 /* --------------------------------------------------------------------------------------------- */
4371 unsigned int
4372 edit_lock_file (WEdit * edit)
4374 char *fullpath;
4375 unsigned int ret;
4377 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4378 ret = lock_file (fullpath);
4379 g_free (fullpath);
4381 return ret;
4384 /* --------------------------------------------------------------------------------------------- */