NEED SQUASH
[midnight-commander.git] / src / editor / edit.c
blob0fa52d09e53797bd4e28ebc06bdbd6b3f290ff17
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 vfs_path_t * filename_vpath)
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_vpath, O_RDONLY | O_BINARY);
261 if (file == -1)
263 gchar *errmsg, *filename;
265 filename = vfs_path_to_str (filename_vpath);
266 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
267 g_free (filename);
268 edit_error_dialog (_("Error"), errmsg);
269 g_free (errmsg);
270 return 1;
273 if (!edit->buffers2[buf2])
274 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
278 if (mc_read (file,
279 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
280 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
281 break;
283 for (buf = buf2 - 1; buf >= 0; buf--)
285 /* edit->buffers2[0] is already allocated */
286 if (!edit->buffers2[buf])
287 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
288 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
289 break;
291 ret = 0;
293 while (0);
295 if (ret != 0)
297 gchar *errmsg, *filename;
299 filename = vfs_path_to_str (filename_vpath);
300 errmsg = g_strdup_printf (_("Error reading %s"), filename);
301 g_free (filename);
302 edit_error_dialog (_("Error"), errmsg);
303 g_free (errmsg);
305 mc_close (file);
306 return ret;
309 /* --------------------------------------------------------------------------------------------- */
310 /** Return index of the filter or -1 is there is no appropriate filter */
312 static int
313 edit_find_filter (const vfs_path_t * filename_vpath)
315 size_t i, l, e;
316 char *filename;
318 if (filename_vpath == NULL)
319 return -1;
321 filename = vfs_path_to_str (filename_vpath);
322 l = strlen (filename);
323 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
325 e = strlen (all_filters[i].extension);
326 if (l > e)
327 if (!strcmp (all_filters[i].extension, filename + l - e))
329 g_free (filename);
330 return i;
333 g_free (filename);
334 return -1;
337 /* --------------------------------------------------------------------------------------------- */
339 static char *
340 edit_get_filter (const vfs_path_t * filename_vpath)
342 int i;
343 char *p, *quoted_name, *filename;
345 i = edit_find_filter (filename_vpath);
346 if (i < 0)
347 return NULL;
349 filename = vfs_path_to_str (filename_vpath);
350 quoted_name = name_quote (filename, 0);
351 g_free (filename);
352 p = g_strdup_printf (all_filters[i].read, quoted_name);
353 g_free (quoted_name);
354 return p;
357 /* --------------------------------------------------------------------------------------------- */
359 static long
360 edit_insert_stream (WEdit * edit, FILE * f)
362 int c;
363 long i = 0;
364 while ((c = fgetc (f)) >= 0)
366 edit_insert (edit, c);
367 i++;
369 return i;
372 /* --------------------------------------------------------------------------------------------- */
373 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
375 static int
376 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
378 int file;
379 gchar *errmsg = NULL;
381 /* Try opening an existing file */
382 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
383 if (file < 0)
386 * Try creating the file. O_EXCL prevents following broken links
387 * and opening existing files.
389 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
390 if (file < 0)
392 char *filename;
394 filename = vfs_path_to_str (filename_vpath);
395 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
396 g_free (filename);
397 goto cleanup;
399 else
401 /* New file, delete it if it's not modified or saved */
402 edit->delete_file = 1;
406 /* Check what we have opened */
407 if (mc_fstat (file, st) < 0)
409 char *filename;
411 filename = vfs_path_to_str (filename_vpath);
412 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
413 g_free (filename);
414 goto cleanup;
417 /* We want to open regular files only */
418 if (!S_ISREG (st->st_mode))
420 char *filename;
422 filename = vfs_path_to_str (filename_vpath);
423 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
424 g_free (filename);
425 goto cleanup;
429 * Don't delete non-empty files.
430 * O_EXCL should prevent it, but let's be on the safe side.
432 if (st->st_size > 0)
433 edit->delete_file = 0;
435 if (st->st_size >= SIZE_LIMIT)
437 char *filename;
439 filename = vfs_path_to_str (filename_vpath);
440 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
441 g_free (filename);
444 cleanup:
445 (void) mc_close (file);
447 if (errmsg != NULL)
449 edit_error_dialog (_("Error"), errmsg);
450 g_free (errmsg);
451 return 1;
453 return 0;
456 /* --------------------------------------------------------------------------------------------- */
458 * Open the file and load it into the buffers, either directly or using
459 * a filter. Return 0 on success, 1 on error.
461 * Fast loading (edit_load_file_fast) is used when the file size is
462 * known. In this case the data is read into the buffers by blocks.
463 * If the file size is not known, the data is loaded byte by byte in
464 * edit_insert_file.
467 static int
468 edit_load_file (WEdit * edit)
470 int fast_load = 1;
472 /* Cannot do fast load if a filter is used */
473 if (edit_find_filter (edit->filename_vpath) >= 0)
474 fast_load = 0;
478 * FIXME: line end translation should disable fast loading as well
479 * Consider doing fseek() to the end and ftell() for the real size.
482 if (edit->filename_vpath != NULL)
486 * VFS may report file size incorrectly, and slow load is not a big
487 * deal considering overhead in VFS.
489 if (!vfs_file_is_local (edit->filename_vpath))
490 fast_load = 0;
492 /* If we are dealing with a real file, check that it exists */
493 if (check_file_access (edit, edit->filename_vpath, &edit->stat1))
494 return 1;
496 else
498 /* nothing to load */
499 fast_load = 0;
502 edit_init_buffers (edit);
504 if (fast_load)
506 edit->last_byte = edit->stat1.st_size;
507 edit_load_file_fast (edit, edit->filename_vpath);
508 /* If fast load was used, the number of lines wasn't calculated */
509 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
511 else
513 edit->last_byte = 0;
514 if (edit->filename_vpath != NULL
515 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
517 edit->undo_stack_disable = 1;
518 if (edit_insert_file (edit, edit->filename_vpath) < 0)
520 edit_clean (edit);
521 return 1;
523 edit->undo_stack_disable = 0;
526 edit->lb = LB_ASIS;
527 return 0;
530 /* --------------------------------------------------------------------------------------------- */
531 /** Restore saved cursor position in the file */
533 static void
534 edit_load_position (WEdit * edit)
536 long line, column;
537 off_t offset;
539 if (edit->filename_vpath == NULL
540 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
541 return;
543 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
545 if (line > 0)
547 edit_move_to_line (edit, line - 1);
548 edit->prev_col = column;
550 else if (offset > 0)
552 edit_cursor_move (edit, offset);
553 line = edit->curs_line;
554 edit->search_start = edit->curs1;
557 book_mark_restore (edit, BOOK_MARK_COLOR);
559 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
560 edit_move_display (edit, line - (edit->widget.lines / 2));
563 /* --------------------------------------------------------------------------------------------- */
564 /** Save cursor position in the file */
566 static void
567 edit_save_position (WEdit * edit)
569 if (edit->filename_vpath == NULL
570 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
571 return;
573 book_mark_serialize (edit, BOOK_MARK_COLOR);
574 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
575 edit->serialized_bookmarks);
576 edit->serialized_bookmarks = NULL;
579 /* --------------------------------------------------------------------------------------------- */
580 /** Clean the WEdit stricture except the widget part */
582 static void
583 edit_purge_widget (WEdit * edit)
585 size_t len = sizeof (WEdit) - sizeof (Widget);
586 char *start = (char *) edit + sizeof (Widget);
587 memset (start, 0, len);
590 /* --------------------------------------------------------------------------------------------- */
593 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
594 then the file should be as it was when he loaded up. Then set edit->modified to 0.
597 static long
598 edit_pop_undo_action (WEdit * edit)
600 long c;
601 unsigned long sp = edit->undo_stack_pointer;
603 if (sp == edit->undo_stack_bottom)
604 return STACK_BOTTOM;
606 sp = (sp - 1) & edit->undo_stack_size_mask;
607 c = edit->undo_stack[sp];
608 if (c >= 0)
610 /* edit->undo_stack[sp] = '@'; */
611 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
612 return c;
615 if (sp == edit->undo_stack_bottom)
616 return STACK_BOTTOM;
618 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
619 if (edit->undo_stack[sp] == -2)
621 /* edit->undo_stack[sp] = '@'; */
622 edit->undo_stack_pointer = sp;
624 else
625 edit->undo_stack[sp]++;
627 return c;
630 static long
631 edit_pop_redo_action (WEdit * edit)
633 long c;
634 unsigned long sp = edit->redo_stack_pointer;
636 if (sp == edit->redo_stack_bottom)
637 return STACK_BOTTOM;
639 sp = (sp - 1) & edit->redo_stack_size_mask;
640 c = edit->redo_stack[sp];
641 if (c >= 0)
643 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
644 return c;
647 if (sp == edit->redo_stack_bottom)
648 return STACK_BOTTOM;
650 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
651 if (edit->redo_stack[sp] == -2)
652 edit->redo_stack_pointer = sp;
653 else
654 edit->redo_stack[sp]++;
656 return c;
659 static long
660 get_prev_undo_action (WEdit * edit)
662 long c;
663 unsigned long sp = edit->undo_stack_pointer;
665 if (sp == edit->undo_stack_bottom)
666 return STACK_BOTTOM;
668 sp = (sp - 1) & edit->undo_stack_size_mask;
669 c = edit->undo_stack[sp];
670 if (c >= 0)
671 return c;
673 if (sp == edit->undo_stack_bottom)
674 return STACK_BOTTOM;
676 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
677 return c;
680 /* --------------------------------------------------------------------------------------------- */
681 /** is called whenever a modification is made by one of the four routines below */
683 static void
684 edit_modification (WEdit * edit)
686 edit->caches_valid = 0;
688 /* raise lock when file modified */
689 if (!edit->modified && !edit->delete_file)
690 edit->locked = lock_file (edit->filename_vpath);
691 edit->modified = 1;
694 /* --------------------------------------------------------------------------------------------- */
696 static char *
697 edit_get_byte_ptr (WEdit * edit, long byte_index)
699 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
700 return NULL;
702 if (byte_index >= edit->curs1)
704 unsigned long p;
706 p = edit->curs1 + edit->curs2 - byte_index - 1;
707 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
708 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
711 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
712 (byte_index & M_EDIT_BUF_SIZE));
715 /* --------------------------------------------------------------------------------------------- */
717 static int
718 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
720 int i, res;
721 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
722 gchar *str;
723 gchar *cursor_buf_ptr;
725 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
727 *char_width = 0;
728 return 0;
731 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
732 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
733 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
735 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
736 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
738 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
740 *char_width = 1;
741 return *(cursor_buf_ptr - 1);
743 else
745 res = g_utf8_get_char_validated (str, -1);
747 if (res < 0)
749 *char_width = 1;
750 return *(cursor_buf_ptr - 1);
752 else
754 *char_width = cursor_buf_ptr - str;
755 return res;
760 /* --------------------------------------------------------------------------------------------- */
762 static int
763 edit_backspace (WEdit * edit, const int byte_delete)
765 int p = 0;
766 int cw = 1;
767 int i;
769 if (!edit->curs1)
770 return 0;
772 cw = 1;
774 if (edit->mark2 != edit->mark1)
775 edit_push_markers (edit);
777 if (edit->utf8 && byte_delete == 0)
779 edit_get_prev_utf (edit, edit->curs1, &cw);
780 if (cw < 1)
781 cw = 1;
783 for (i = 1; i <= cw; i++)
785 if (edit->mark1 >= edit->curs1)
787 edit->mark1--;
788 edit->end_mark_curs--;
790 if (edit->mark2 >= edit->curs1)
791 edit->mark2--;
792 if (edit->last_get_rule >= edit->curs1)
793 edit->last_get_rule--;
795 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
796 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
797 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
799 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
800 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
802 edit->last_byte--;
803 edit->curs1--;
804 edit_push_undo_action (edit, p);
806 edit_modification (edit);
807 if (p == '\n')
809 if (edit->book_mark)
810 book_mark_dec (edit, edit->curs_line);
811 edit->curs_line--;
812 edit->total_lines--;
813 edit->force |= REDRAW_AFTER_CURSOR;
816 if (edit->curs1 < edit->start_display)
818 edit->start_display--;
819 if (p == '\n')
820 edit->start_line--;
823 return p;
826 /* --------------------------------------------------------------------------------------------- */
827 /* high level cursor movement commands */
828 /* --------------------------------------------------------------------------------------------- */
830 static int
831 is_in_indent (WEdit * edit)
833 long p = edit_bol (edit, edit->curs1);
834 while (p < edit->curs1)
835 if (!strchr (" \t", edit_get_byte (edit, p++)))
836 return 0;
837 return 1;
840 /* --------------------------------------------------------------------------------------------- */
842 static int
843 is_blank (WEdit * edit, long offset)
845 long s, f;
846 int c;
847 s = edit_bol (edit, offset);
848 f = edit_eol (edit, offset) - 1;
849 while (s <= f)
851 c = edit_get_byte (edit, s++);
852 if (!isspace (c))
853 return 0;
855 return 1;
859 /* --------------------------------------------------------------------------------------------- */
860 /** returns the offset of line i */
862 static long
863 edit_find_line (WEdit * edit, int line)
865 int i, j = 0;
866 int m = 2000000000;
867 if (!edit->caches_valid)
869 for (i = 0; i < N_LINE_CACHES; i++)
870 edit->line_numbers[i] = edit->line_offsets[i] = 0;
871 /* three offsets that we *know* are line 0 at 0 and these two: */
872 edit->line_numbers[1] = edit->curs_line;
873 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
874 edit->line_numbers[2] = edit->total_lines;
875 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
876 edit->caches_valid = 1;
878 if (line >= edit->total_lines)
879 return edit->line_offsets[2];
880 if (line <= 0)
881 return 0;
882 /* find the closest known point */
883 for (i = 0; i < N_LINE_CACHES; i++)
885 int n;
886 n = abs (edit->line_numbers[i] - line);
887 if (n < m)
889 m = n;
890 j = i;
893 if (m == 0)
894 return edit->line_offsets[j]; /* know the offset exactly */
895 if (m == 1 && j >= 3)
896 i = j; /* one line different - caller might be looping, so stay in this cache */
897 else
898 i = 3 + (rand () % (N_LINE_CACHES - 3));
899 if (line > edit->line_numbers[j])
900 edit->line_offsets[i] =
901 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
902 else
903 edit->line_offsets[i] =
904 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
905 edit->line_numbers[i] = line;
906 return edit->line_offsets[i];
909 /* --------------------------------------------------------------------------------------------- */
910 /** moves up until a blank line is reached, or until just
911 before a non-blank line is reached */
913 static void
914 edit_move_up_paragraph (WEdit * edit, int do_scroll)
916 int i = 0;
917 if (edit->curs_line > 1)
919 if (line_is_blank (edit, edit->curs_line))
921 if (line_is_blank (edit, edit->curs_line - 1))
923 for (i = edit->curs_line - 1; i; i--)
924 if (!line_is_blank (edit, i))
926 i++;
927 break;
930 else
932 for (i = edit->curs_line - 1; i; i--)
933 if (line_is_blank (edit, i))
934 break;
937 else
939 for (i = edit->curs_line - 1; i; i--)
940 if (line_is_blank (edit, i))
941 break;
944 edit_move_up (edit, edit->curs_line - i, do_scroll);
947 /* --------------------------------------------------------------------------------------------- */
948 /** moves down until a blank line is reached, or until just
949 before a non-blank line is reached */
951 static void
952 edit_move_down_paragraph (WEdit * edit, int do_scroll)
954 int i;
955 if (edit->curs_line >= edit->total_lines - 1)
957 i = edit->total_lines;
959 else
961 if (line_is_blank (edit, edit->curs_line))
963 if (line_is_blank (edit, edit->curs_line + 1))
965 for (i = edit->curs_line + 1; i; i++)
966 if (!line_is_blank (edit, i) || i > edit->total_lines)
968 i--;
969 break;
972 else
974 for (i = edit->curs_line + 1; i; i++)
975 if (line_is_blank (edit, i) || i >= edit->total_lines)
976 break;
979 else
981 for (i = edit->curs_line + 1; i; i++)
982 if (line_is_blank (edit, i) || i >= edit->total_lines)
983 break;
986 edit_move_down (edit, i - edit->curs_line, do_scroll);
989 /* --------------------------------------------------------------------------------------------- */
991 static void
992 edit_begin_page (WEdit * edit)
994 edit_update_curs_row (edit);
995 edit_move_up (edit, edit->curs_row, 0);
998 /* --------------------------------------------------------------------------------------------- */
1000 static void
1001 edit_end_page (WEdit * edit)
1003 edit_update_curs_row (edit);
1004 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
1008 /* --------------------------------------------------------------------------------------------- */
1009 /** goto beginning of text */
1011 static void
1012 edit_move_to_top (WEdit * edit)
1014 if (edit->curs_line)
1016 edit_cursor_move (edit, -edit->curs1);
1017 edit_move_to_prev_col (edit, 0);
1018 edit->force |= REDRAW_PAGE;
1019 edit->search_start = 0;
1020 edit_update_curs_row (edit);
1025 /* --------------------------------------------------------------------------------------------- */
1026 /** goto end of text */
1028 static void
1029 edit_move_to_bottom (WEdit * edit)
1031 if (edit->curs_line < edit->total_lines)
1033 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1034 edit->start_display = edit->last_byte;
1035 edit->start_line = edit->total_lines;
1036 edit_scroll_upward (edit, edit->widget.lines - 1);
1037 edit->force |= REDRAW_PAGE;
1041 /* --------------------------------------------------------------------------------------------- */
1042 /** returns index of first char on line or the first nonspace char */
1044 static long
1045 edit_bol_var (WEdit * edit, long current)
1047 long tmp;
1048 int b;
1050 if (current < 0)
1051 current = 0;
1053 if (edit_get_byte (edit, current - 1) != '\n') /* not at BOL */
1054 return edit_bol (edit, current);
1056 if (edit_get_byte (edit, current) == '\n') /* on an empty line */
1057 return current;
1059 for (tmp = current; (b = edit_get_byte (edit, tmp)) != '\n'; tmp++)
1061 if (b != ' ' && b != '\t')
1062 return tmp;
1064 return current;
1067 /* --------------------------------------------------------------------------------------------- */
1068 /** returns index of last char on line or the last nonspace char */
1069 static long
1070 edit_eol_var (WEdit * edit, long current)
1072 long tmp;
1073 int b;
1075 if (current >= edit->last_byte)
1076 current = edit->last_byte;
1078 if (edit_get_byte (edit, current) != '\n') /* not at BOL */
1079 return edit_eol (edit, current);
1081 if (edit_get_byte (edit, current - 1) == '\n') /* on an empty line */
1082 return current;
1084 for (tmp = current - 1; (b = edit_get_byte (edit, tmp)) != '\n'; tmp--)
1086 if (b != ' ' && b != '\t')
1087 return tmp + 1;
1089 return current;
1092 /* --------------------------------------------------------------------------------------------- */
1093 /** goto beginning of line */
1095 static void
1096 edit_cursor_to_bol (WEdit * edit)
1098 edit_cursor_move (edit, edit_bol_var (edit, edit->curs1) - edit->curs1);
1099 edit->search_start = edit->curs1;
1100 edit->prev_col = edit_get_col (edit);
1101 edit->over_col = 0;
1104 /* --------------------------------------------------------------------------------------------- */
1105 /** goto end of line */
1107 static void
1108 edit_cursor_to_eol (WEdit * edit)
1110 edit_cursor_move (edit, edit_eol_var (edit, edit->curs1) - edit->curs1);
1111 edit->search_start = edit->curs1;
1112 edit->prev_col = edit_get_col (edit);
1113 edit->over_col = 0;
1116 /* --------------------------------------------------------------------------------------------- */
1118 static unsigned long
1119 my_type_of (int c)
1121 int x, r = 0;
1122 const char *p, *q;
1123 const char option_chars_move_whole_word[] =
1124 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1126 if (!c)
1127 return 0;
1128 if (c == '!')
1130 if (*option_chars_move_whole_word == '!')
1131 return 2;
1132 return 0x80000000UL;
1134 if (g_ascii_isupper ((gchar) c))
1135 c = 'A';
1136 else if (g_ascii_islower ((gchar) c))
1137 c = 'a';
1138 else if (g_ascii_isalpha (c))
1139 c = 'a';
1140 else if (isdigit (c))
1141 c = '0';
1142 else if (isspace (c))
1143 c = ' ';
1144 q = strchr (option_chars_move_whole_word, c);
1145 if (!q)
1146 return 0xFFFFFFFFUL;
1149 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1150 if (*p == '!')
1151 x <<= 1;
1152 r |= x;
1154 while ((q = strchr (q + 1, c)));
1155 return r;
1158 /* --------------------------------------------------------------------------------------------- */
1160 static void
1161 edit_left_word_move (WEdit * edit, int s)
1163 for (;;)
1165 int c1, c2;
1166 if (edit->column_highlight
1167 && edit->mark1 != edit->mark2
1168 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1169 break;
1170 edit_cursor_move (edit, -1);
1171 if (!edit->curs1)
1172 break;
1173 c1 = edit_get_byte (edit, edit->curs1 - 1);
1174 c2 = edit_get_byte (edit, edit->curs1);
1175 if (c1 == '\n' || c2 == '\n')
1176 break;
1177 if (!(my_type_of (c1) & my_type_of (c2)))
1178 break;
1179 if (isspace (c1) && !isspace (c2))
1180 break;
1181 if (s)
1182 if (!isspace (c1) && isspace (c2))
1183 break;
1187 /* --------------------------------------------------------------------------------------------- */
1189 static void
1190 edit_left_word_move_cmd (WEdit * edit)
1192 edit_left_word_move (edit, 0);
1193 edit->force |= REDRAW_PAGE;
1196 /* --------------------------------------------------------------------------------------------- */
1198 static void
1199 edit_right_word_move (WEdit * edit, int s)
1201 for (;;)
1203 int c1, c2;
1204 if (edit->column_highlight
1205 && edit->mark1 != edit->mark2
1206 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1207 break;
1208 edit_cursor_move (edit, 1);
1209 if (edit->curs1 >= edit->last_byte)
1210 break;
1211 c1 = edit_get_byte (edit, edit->curs1 - 1);
1212 c2 = edit_get_byte (edit, edit->curs1);
1213 if (c1 == '\n' || c2 == '\n')
1214 break;
1215 if (!(my_type_of (c1) & my_type_of (c2)))
1216 break;
1217 if (isspace (c1) && !isspace (c2))
1218 break;
1219 if (s)
1220 if (!isspace (c1) && isspace (c2))
1221 break;
1225 /* --------------------------------------------------------------------------------------------- */
1227 static void
1228 edit_right_word_move_cmd (WEdit * edit)
1230 edit_right_word_move (edit, 0);
1231 edit->force |= REDRAW_PAGE;
1234 /* --------------------------------------------------------------------------------------------- */
1236 static void
1237 edit_right_char_move_cmd (WEdit * edit)
1239 int cw = 1;
1240 int c = 0;
1241 if (edit->utf8)
1243 c = edit_get_utf (edit, edit->curs1, &cw);
1244 if (cw < 1)
1245 cw = 1;
1247 else
1249 c = edit_get_byte (edit, edit->curs1);
1251 if (option_cursor_beyond_eol && c == '\n')
1253 edit->over_col++;
1255 else
1257 edit_cursor_move (edit, cw);
1261 /* --------------------------------------------------------------------------------------------- */
1263 static void
1264 edit_left_char_move_cmd (WEdit * edit)
1266 int cw = 1;
1267 if (edit->column_highlight
1268 && option_cursor_beyond_eol
1269 && edit->mark1 != edit->mark2
1270 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1271 return;
1272 if (edit->utf8)
1274 edit_get_prev_utf (edit, edit->curs1, &cw);
1275 if (cw < 1)
1276 cw = 1;
1278 if (option_cursor_beyond_eol && edit->over_col > 0)
1280 edit->over_col--;
1282 else
1284 edit_cursor_move (edit, -cw);
1288 /* --------------------------------------------------------------------------------------------- */
1289 /** Up or down cursor moving.
1290 direction = TRUE - move up
1291 = FALSE - move down
1294 static void
1295 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1297 unsigned long p;
1298 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1300 if (i > l)
1301 i = l;
1303 if (i == 0)
1304 return;
1306 if (i > 1)
1307 edit->force |= REDRAW_PAGE;
1308 if (do_scroll)
1310 if (direction)
1311 edit_scroll_upward (edit, i);
1312 else
1313 edit_scroll_downward (edit, i);
1315 p = edit_bol (edit, edit->curs1);
1317 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1319 edit_cursor_move (edit, p - edit->curs1);
1321 edit_move_to_prev_col (edit, p);
1323 /* search start of current multibyte char (like CJK) */
1324 if (edit->curs1 + 1 < edit->last_byte)
1326 edit_right_char_move_cmd (edit);
1327 edit_left_char_move_cmd (edit);
1330 edit->search_start = edit->curs1;
1331 edit->found_len = 0;
1334 /* --------------------------------------------------------------------------------------------- */
1336 static void
1337 edit_right_delete_word (WEdit * edit)
1339 int c1, c2;
1340 for (;;)
1342 if (edit->curs1 >= edit->last_byte)
1343 break;
1344 c1 = edit_delete (edit, 1);
1345 c2 = edit_get_byte (edit, edit->curs1);
1346 if (c1 == '\n' || c2 == '\n')
1347 break;
1348 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1349 break;
1350 if (!(my_type_of (c1) & my_type_of (c2)))
1351 break;
1355 /* --------------------------------------------------------------------------------------------- */
1357 static void
1358 edit_left_delete_word (WEdit * edit)
1360 int c1, c2;
1361 for (;;)
1363 if (edit->curs1 <= 0)
1364 break;
1365 c1 = edit_backspace (edit, 1);
1366 c2 = edit_get_byte (edit, edit->curs1 - 1);
1367 if (c1 == '\n' || c2 == '\n')
1368 break;
1369 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1370 break;
1371 if (!(my_type_of (c1) & my_type_of (c2)))
1372 break;
1376 /* --------------------------------------------------------------------------------------------- */
1378 the start column position is not recorded, and hence does not
1379 undo as it happed. But who would notice.
1382 static void
1383 edit_do_undo (WEdit * edit)
1385 long ac;
1386 long count = 0;
1388 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1389 edit->over_col = 0;
1390 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1392 switch ((int) ac)
1394 case STACK_BOTTOM:
1395 goto done_undo;
1396 case CURS_RIGHT:
1397 edit_cursor_move (edit, 1);
1398 break;
1399 case CURS_LEFT:
1400 edit_cursor_move (edit, -1);
1401 break;
1402 case BACKSPACE:
1403 case BACKSPACE_BR:
1404 edit_backspace (edit, 1);
1405 break;
1406 case DELCHAR:
1407 case DELCHAR_BR:
1408 edit_delete (edit, 1);
1409 break;
1410 case COLUMN_ON:
1411 edit->column_highlight = 1;
1412 break;
1413 case COLUMN_OFF:
1414 edit->column_highlight = 0;
1415 break;
1417 if (ac >= 256 && ac < 512)
1418 edit_insert_ahead (edit, ac - 256);
1419 if (ac >= 0 && ac < 256)
1420 edit_insert (edit, ac);
1422 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1424 edit->mark1 = ac - MARK_1;
1425 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1427 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1429 edit->mark2 = ac - MARK_2;
1430 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1432 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1434 edit->end_mark_curs = ac - MARK_CURS;
1436 if (count++)
1437 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1440 if (edit->start_display > ac - KEY_PRESS)
1442 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1443 edit->force |= REDRAW_PAGE;
1445 else if (edit->start_display < ac - KEY_PRESS)
1447 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1448 edit->force |= REDRAW_PAGE;
1450 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1451 edit_update_curs_row (edit);
1453 done_undo:;
1454 edit->undo_stack_disable = 0;
1457 static void
1458 edit_do_redo (WEdit * edit)
1460 long ac;
1461 long count = 0;
1463 if (edit->redo_stack_reset)
1464 return;
1466 edit->over_col = 0;
1467 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1469 switch ((int) ac)
1471 case STACK_BOTTOM:
1472 goto done_redo;
1473 case CURS_RIGHT:
1474 edit_cursor_move (edit, 1);
1475 break;
1476 case CURS_LEFT:
1477 edit_cursor_move (edit, -1);
1478 break;
1479 case BACKSPACE:
1480 edit_backspace (edit, 1);
1481 break;
1482 case DELCHAR:
1483 edit_delete (edit, 1);
1484 break;
1485 case COLUMN_ON:
1486 edit->column_highlight = 1;
1487 break;
1488 case COLUMN_OFF:
1489 edit->column_highlight = 0;
1490 break;
1492 if (ac >= 256 && ac < 512)
1493 edit_insert_ahead (edit, ac - 256);
1494 if (ac >= 0 && ac < 256)
1495 edit_insert (edit, ac);
1497 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1499 edit->mark1 = ac - MARK_1;
1500 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1502 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1504 edit->mark2 = ac - MARK_2;
1505 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1507 /* more than one pop usually means something big */
1508 if (count++)
1509 edit->force |= REDRAW_PAGE;
1512 if (edit->start_display > ac - KEY_PRESS)
1514 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1515 edit->force |= REDRAW_PAGE;
1517 else if (edit->start_display < ac - KEY_PRESS)
1519 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1520 edit->force |= REDRAW_PAGE;
1522 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1523 edit_update_curs_row (edit);
1525 done_redo:;
1528 static void
1529 edit_group_undo (WEdit * edit)
1531 long ac = KEY_PRESS;
1532 long cur_ac = KEY_PRESS;
1533 while (ac != STACK_BOTTOM && ac == cur_ac)
1535 cur_ac = get_prev_undo_action (edit);
1536 edit_do_undo (edit);
1537 ac = get_prev_undo_action (edit);
1538 /* exit from cycle if option_group_undo is not set,
1539 * and make single UNDO operation
1541 if (!option_group_undo)
1542 ac = STACK_BOTTOM;
1546 /* --------------------------------------------------------------------------------------------- */
1548 static void
1549 edit_delete_to_line_end (WEdit * edit)
1551 while (edit_get_byte (edit, edit->curs1) != '\n')
1553 if (!edit->curs2)
1554 break;
1555 edit_delete (edit, 1);
1559 /* --------------------------------------------------------------------------------------------- */
1561 static void
1562 edit_delete_to_line_begin (WEdit * edit)
1564 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1566 if (!edit->curs1)
1567 break;
1568 edit_backspace (edit, 1);
1572 /* --------------------------------------------------------------------------------------------- */
1574 static int
1575 is_aligned_on_a_tab (WEdit * edit)
1577 edit_update_curs_col (edit);
1578 return !((edit->curs_col % (TAB_SIZE * space_width))
1579 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1582 /* --------------------------------------------------------------------------------------------- */
1584 static int
1585 right_of_four_spaces (WEdit * edit)
1587 int i, ch = 0;
1588 for (i = 1; i <= HALF_TAB_SIZE; i++)
1589 ch |= edit_get_byte (edit, edit->curs1 - i);
1590 if (ch == ' ')
1591 return is_aligned_on_a_tab (edit);
1592 return 0;
1595 /* --------------------------------------------------------------------------------------------- */
1597 static int
1598 left_of_four_spaces (WEdit * edit)
1600 int i, ch = 0;
1601 for (i = 0; i < HALF_TAB_SIZE; i++)
1602 ch |= edit_get_byte (edit, edit->curs1 + i);
1603 if (ch == ' ')
1604 return is_aligned_on_a_tab (edit);
1605 return 0;
1608 /* --------------------------------------------------------------------------------------------- */
1610 static void
1611 edit_auto_indent (WEdit * edit)
1613 long p;
1614 char c;
1615 p = edit->curs1;
1616 /* use the previous line as a template */
1617 p = edit_move_backward (edit, p, 1);
1618 /* copy the leading whitespace of the line */
1619 for (;;)
1620 { /* no range check - the line _is_ \n-terminated */
1621 c = edit_get_byte (edit, p++);
1622 if (c != ' ' && c != '\t')
1623 break;
1624 edit_insert (edit, c);
1628 /* --------------------------------------------------------------------------------------------- */
1630 static inline void
1631 edit_double_newline (WEdit * edit)
1633 edit_insert (edit, '\n');
1634 if (edit_get_byte (edit, edit->curs1) == '\n')
1635 return;
1636 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1637 return;
1638 edit->force |= REDRAW_PAGE;
1639 edit_insert (edit, '\n');
1643 /* --------------------------------------------------------------------------------------------- */
1645 static void
1646 insert_spaces_tab (WEdit * edit, gboolean half)
1648 int i;
1650 edit_update_curs_col (edit);
1651 i = option_tab_spacing * space_width;
1652 if (half)
1653 i /= 2;
1654 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1655 while (i > 0)
1657 edit_insert (edit, ' ');
1658 i -= space_width;
1662 /* --------------------------------------------------------------------------------------------- */
1664 static inline void
1665 edit_tab_cmd (WEdit * edit)
1667 int i;
1669 if (option_fake_half_tabs)
1671 if (is_in_indent (edit))
1673 /*insert a half tab (usually four spaces) unless there is a
1674 half tab already behind, then delete it and insert a
1675 full tab. */
1676 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1677 insert_spaces_tab (edit, TRUE);
1678 else
1680 for (i = 1; i <= HALF_TAB_SIZE; i++)
1681 edit_backspace (edit, 1);
1682 edit_insert (edit, '\t');
1684 return;
1687 if (option_fill_tabs_with_spaces)
1688 insert_spaces_tab (edit, FALSE);
1689 else
1690 edit_insert (edit, '\t');
1693 /* --------------------------------------------------------------------------------------------- */
1695 static void
1696 check_and_wrap_line (WEdit * edit)
1698 int curs, c;
1699 if (!option_typewriter_wrap)
1700 return;
1701 edit_update_curs_col (edit);
1702 if (edit->curs_col < option_word_wrap_line_length)
1703 return;
1704 curs = edit->curs1;
1705 for (;;)
1707 curs--;
1708 c = edit_get_byte (edit, curs);
1709 if (c == '\n' || curs <= 0)
1711 edit_insert (edit, '\n');
1712 return;
1714 if (c == ' ' || c == '\t')
1716 int current = edit->curs1;
1717 edit_cursor_move (edit, curs - edit->curs1 + 1);
1718 edit_insert (edit, '\n');
1719 edit_cursor_move (edit, current - edit->curs1 + 1);
1720 return;
1725 /* --------------------------------------------------------------------------------------------- */
1726 /** this find the matching bracket in either direction, and sets edit->bracket */
1728 static long
1729 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1731 const char *const b = "{}{[][()(", *p;
1732 int i = 1, a, inc = -1, c, d, n = 0;
1733 unsigned long j = 0;
1734 long q;
1735 edit_update_curs_row (edit);
1736 c = edit_get_byte (edit, edit->curs1);
1737 p = strchr (b, c);
1738 /* no limit */
1739 if (!furthest_bracket_search)
1740 furthest_bracket_search--;
1741 /* not on a bracket at all */
1742 if (!p)
1743 return -1;
1744 /* the matching bracket */
1745 d = p[1];
1746 /* going left or right? */
1747 if (strchr ("{[(", c))
1748 inc = 1;
1749 for (q = edit->curs1 + inc;; q += inc)
1751 /* out of buffer? */
1752 if (q >= edit->last_byte || q < 0)
1753 break;
1754 a = edit_get_byte (edit, q);
1755 /* don't want to eat CPU */
1756 if (j++ > furthest_bracket_search)
1757 break;
1758 /* out of screen? */
1759 if (in_screen)
1761 if (q < edit->start_display)
1762 break;
1763 /* count lines if searching downward */
1764 if (inc > 0 && a == '\n')
1765 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1766 break;
1768 /* count bracket depth */
1769 i += (a == c) - (a == d);
1770 /* return if bracket depth is zero */
1771 if (!i)
1772 return q;
1774 /* no match */
1775 return -1;
1778 /* --------------------------------------------------------------------------------------------- */
1780 static inline void
1781 edit_goto_matching_bracket (WEdit * edit)
1783 long q;
1785 q = edit_get_bracket (edit, 0, 0);
1786 if (q >= 0)
1788 edit->bracket = edit->curs1;
1789 edit->force |= REDRAW_PAGE;
1790 edit_cursor_move (edit, q - edit->curs1);
1794 /* --------------------------------------------------------------------------------------------- */
1796 static void
1797 edit_move_block_to_right (WEdit * edit)
1799 long start_mark, end_mark;
1800 long cur_bol, start_bol;
1802 if (eval_marks (edit, &start_mark, &end_mark))
1803 return;
1805 start_bol = edit_bol (edit, start_mark);
1806 cur_bol = edit_bol (edit, end_mark - 1);
1810 edit_cursor_move (edit, cur_bol - edit->curs1);
1811 if (option_fill_tabs_with_spaces)
1812 insert_spaces_tab (edit, option_fake_half_tabs);
1813 else
1814 edit_insert (edit, '\t');
1815 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1817 if (cur_bol == 0)
1818 break;
1820 cur_bol = edit_bol (edit, cur_bol - 1);
1822 while (cur_bol >= start_bol);
1824 edit->force |= REDRAW_PAGE;
1827 /* --------------------------------------------------------------------------------------------- */
1829 static void
1830 edit_move_block_to_left (WEdit * edit)
1832 long start_mark, end_mark;
1833 long cur_bol, start_bol;
1834 int i;
1836 if (eval_marks (edit, &start_mark, &end_mark))
1837 return;
1839 start_bol = edit_bol (edit, start_mark);
1840 cur_bol = edit_bol (edit, end_mark - 1);
1844 int del_tab_width;
1845 int next_char;
1847 edit_cursor_move (edit, cur_bol - edit->curs1);
1849 if (option_fake_half_tabs)
1850 del_tab_width = HALF_TAB_SIZE;
1851 else
1852 del_tab_width = option_tab_spacing;
1854 next_char = edit_get_byte (edit, edit->curs1);
1855 if (next_char == '\t')
1856 edit_delete (edit, 1);
1857 else if (next_char == ' ')
1858 for (i = 1; i <= del_tab_width; i++)
1860 if (next_char == ' ')
1861 edit_delete (edit, 1);
1862 next_char = edit_get_byte (edit, edit->curs1);
1865 if (cur_bol == 0)
1866 break;
1868 cur_bol = edit_bol (edit, cur_bol - 1);
1870 while (cur_bol >= start_bol);
1872 edit->force |= REDRAW_PAGE;
1875 /* --------------------------------------------------------------------------------------------- */
1877 * prints at the cursor
1878 * @returns the number of chars printed
1881 static size_t
1882 edit_print_string (WEdit * e, const char *s)
1884 size_t i = 0;
1886 while (s[i] != '\0')
1887 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1888 e->force |= REDRAW_COMPLETELY;
1889 edit_update_screen (e);
1890 return i;
1893 /* --------------------------------------------------------------------------------------------- */
1894 /*** public functions ****************************************************************************/
1895 /* --------------------------------------------------------------------------------------------- */
1897 /** User edit menu, like user menu (F2) but only in editor. */
1899 void
1900 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1902 char *block_file;
1903 int nomark;
1904 long curs;
1905 long start_mark, end_mark;
1906 struct stat status;
1907 vfs_path_t *block_file_vpath;
1909 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1910 block_file_vpath = vfs_path_from_str (block_file);
1911 curs = edit->curs1;
1912 nomark = eval_marks (edit, &start_mark, &end_mark);
1913 if (nomark == 0)
1914 edit_save_block (edit, block_file, start_mark, end_mark);
1916 /* run shell scripts from menu */
1917 if (user_menu_cmd (edit, menu_file, selected_entry)
1918 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1920 int rc = 0;
1921 FILE *fd;
1923 /* i.e. we have marked block */
1924 if (nomark == 0)
1925 rc = edit_block_delete_cmd (edit);
1927 if (rc == 0)
1929 long ins_len;
1931 ins_len = edit_insert_file (edit, block_file_vpath);
1932 if (nomark == 0 && ins_len > 0)
1933 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1935 /* truncate block file */
1936 fd = fopen (block_file, "w");
1937 if (fd != NULL)
1938 fclose (fd);
1940 edit_cursor_move (edit, curs - edit->curs1);
1941 edit_refresh_cmd (edit);
1942 edit->force |= REDRAW_COMPLETELY;
1944 g_free (block_file);
1945 vfs_path_free (block_file_vpath);
1948 /* --------------------------------------------------------------------------------------------- */
1951 edit_get_byte (WEdit * edit, long byte_index)
1953 unsigned long p;
1954 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1955 return '\n';
1957 if (byte_index >= edit->curs1)
1959 p = edit->curs1 + edit->curs2 - byte_index - 1;
1960 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1962 else
1964 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1968 /* --------------------------------------------------------------------------------------------- */
1971 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1973 gchar *str = NULL;
1974 int res = -1;
1975 gunichar ch;
1976 gchar *next_ch = NULL;
1977 int width = 0;
1978 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1980 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1982 *char_width = 0;
1983 return '\n';
1986 str = edit_get_byte_ptr (edit, byte_index);
1988 if (str == NULL)
1990 *char_width = 0;
1991 return 0;
1994 res = g_utf8_get_char_validated (str, -1);
1996 if (res < 0)
1998 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1999 int i;
2000 for (i = 0; i < UTF8_CHAR_LEN; i++)
2001 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
2002 utf8_buf[UTF8_CHAR_LEN] = '\0';
2003 str = utf8_buf;
2004 res = g_utf8_get_char_validated (str, -1);
2007 if (res < 0)
2009 ch = *str;
2010 width = 0;
2012 else
2014 ch = res;
2015 /* Calculate UTF-8 char width */
2016 next_ch = g_utf8_next_char (str);
2017 if (next_ch)
2019 width = next_ch - str;
2021 else
2023 ch = 0;
2024 width = 0;
2027 *char_width = width;
2028 return ch;
2031 /* --------------------------------------------------------------------------------------------- */
2033 char *
2034 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
2036 int i;
2037 char *p, *writename;
2038 const vfs_path_element_t *path_element;
2040 i = edit_find_filter (filename_vpath);
2041 if (i < 0)
2042 return NULL;
2044 path_element = vfs_path_get_by_index (write_name_vpath, -1);
2045 writename = name_quote (path_element->path, 0);
2046 p = g_strdup_printf (all_filters[i].write, writename);
2047 g_free (writename);
2048 return p;
2051 /* --------------------------------------------------------------------------------------------- */
2053 long
2054 edit_write_stream (WEdit * edit, FILE * f)
2056 long i;
2058 if (edit->lb == LB_ASIS)
2060 for (i = 0; i < edit->last_byte; i++)
2061 if (fputc (edit_get_byte (edit, i), f) < 0)
2062 break;
2063 return i;
2066 /* change line breaks */
2067 for (i = 0; i < edit->last_byte; i++)
2069 unsigned char c = edit_get_byte (edit, i);
2071 if (!(c == '\n' || c == '\r'))
2073 /* not line break */
2074 if (fputc (c, f) < 0)
2075 return i;
2077 else
2078 { /* (c == '\n' || c == '\r') */
2079 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2081 switch (edit->lb)
2083 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2084 /* put one line break unconditionally */
2085 if (fputc ('\n', f) < 0)
2086 return i;
2088 i++; /* 2 chars are processed */
2090 if (c == '\r' && c1 == '\n')
2091 /* Windows line break; go to the next char */
2092 break;
2094 if (c == '\r' && c1 == '\r')
2096 /* two Macintosh line breaks; put second line break */
2097 if (fputc ('\n', f) < 0)
2098 return i;
2099 break;
2102 if (fputc (c1, f) < 0)
2103 return i;
2104 break;
2106 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2107 /* put one line break unconditionally */
2108 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2109 return i;
2111 if (c == '\r' && c1 == '\n')
2112 /* Windows line break; go to the next char */
2113 i++;
2114 break;
2116 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2117 /* put one line break unconditionally */
2118 if (fputc ('\r', f) < 0)
2119 return i;
2121 i++; /* 2 chars are processed */
2123 if (c == '\r' && c1 == '\n')
2124 /* Windows line break; go to the next char */
2125 break;
2127 if (c == '\n' && c1 == '\n')
2129 /* two Windows line breaks; put second line break */
2130 if (fputc ('\r', f) < 0)
2131 return i;
2132 break;
2135 if (fputc (c1, f) < 0)
2136 return i;
2137 break;
2138 case LB_ASIS: /* default without changes */
2139 break;
2144 return edit->last_byte;
2147 /* --------------------------------------------------------------------------------------------- */
2148 /** inserts a file at the cursor, returns count of inserted bytes on success */
2149 long
2150 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2152 char *p;
2153 long ins_len = 0;
2155 p = edit_get_filter (filename_vpath);
2156 if (p != NULL)
2158 FILE *f;
2159 long current = edit->curs1;
2161 f = (FILE *) popen (p, "r");
2162 if (f != NULL)
2164 edit_insert_stream (edit, f);
2165 ins_len = edit->curs1 - current;
2166 edit_cursor_move (edit, -ins_len);
2167 if (pclose (f) > 0)
2169 char *errmsg;
2171 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2172 edit_error_dialog (_("Error"), errmsg);
2173 g_free (errmsg);
2174 ins_len = -1;
2177 else
2179 char *errmsg;
2181 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2182 edit_error_dialog (_("Error"), errmsg);
2183 g_free (errmsg);
2184 ins_len = -1;
2186 g_free (p);
2188 else
2190 int i, file, blocklen;
2191 long current = edit->curs1;
2192 int vertical_insertion = 0;
2193 char *buf;
2195 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2196 if (file == -1)
2197 return -1;
2199 buf = g_malloc0 (TEMP_BUF_LEN);
2200 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2201 if (blocklen > 0)
2203 /* if contain signature VERTICAL_MAGIC then it vertical block */
2204 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2205 vertical_insertion = 1;
2206 else
2207 mc_lseek (file, 0, SEEK_SET);
2210 if (vertical_insertion)
2212 long mark1, mark2;
2213 int c1, c2;
2215 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2216 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2217 /* highlight inserted text then not persistent blocks */
2218 if (!option_persistent_selections)
2220 if (!edit->column_highlight)
2221 edit_push_undo_action (edit, COLUMN_OFF);
2222 edit->column_highlight = 1;
2225 else
2227 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2229 for (i = 0; i < blocklen; i++)
2230 edit_insert (edit, buf[i]);
2232 /* highlight inserted text then not persistent blocks */
2233 if (!option_persistent_selections && edit->modified)
2235 edit_set_markers (edit, edit->curs1, current, 0, 0);
2236 if (edit->column_highlight)
2237 edit_push_undo_action (edit, COLUMN_ON);
2238 edit->column_highlight = 0;
2242 edit->force |= REDRAW_PAGE;
2243 ins_len = edit->curs1 - current;
2244 edit_cursor_move (edit, -ins_len);
2245 g_free (buf);
2246 mc_close (file);
2247 if (blocklen != 0)
2248 ins_len = 0;
2251 return ins_len;
2254 /* --------------------------------------------------------------------------------------------- */
2256 * Fill in the edit structure. Return NULL on failure. Pass edit as
2257 * NULL to allocate a new structure.
2259 * If line is 0, try to restore saved position. Otherwise put the
2260 * cursor on that line and show it in the middle of the screen.
2263 WEdit *
2264 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2265 long line)
2267 gboolean to_free = FALSE;
2269 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2270 if (option_line_state)
2271 option_line_state_width = LINE_STATE_WIDTH;
2272 else
2273 option_line_state_width = 0;
2275 if (edit == NULL)
2277 #ifdef ENABLE_NLS
2279 * Expand option_whole_chars_search by national letters using
2280 * current locale
2283 static char option_whole_chars_search_buf[256];
2285 if (option_whole_chars_search_buf != option_whole_chars_search)
2287 size_t i;
2288 size_t len = str_term_width1 (option_whole_chars_search);
2290 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2292 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2294 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2296 option_whole_chars_search_buf[len++] = i;
2300 option_whole_chars_search_buf[len] = 0;
2301 option_whole_chars_search = option_whole_chars_search_buf;
2303 #endif /* ENABLE_NLS */
2304 edit = g_malloc0 (sizeof (WEdit));
2305 edit->search = NULL;
2306 to_free = TRUE;
2309 edit_purge_widget (edit);
2310 edit->widget.y = y;
2311 edit->widget.x = x;
2312 edit->widget.lines = lines;
2313 edit->widget.cols = cols;
2315 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2316 edit->stat1.st_uid = getuid ();
2317 edit->stat1.st_gid = getgid ();
2318 edit->stat1.st_mtime = 0;
2320 edit->over_col = 0;
2321 edit->bracket = -1;
2322 edit->force |= REDRAW_PAGE;
2323 edit_set_filename (edit, filename_vpath);
2325 edit->undo_stack_size = START_STACK_SIZE;
2326 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2327 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2329 edit->redo_stack_size = START_STACK_SIZE;
2330 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2331 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2333 edit->utf8 = 0;
2334 edit->converter = str_cnv_from_term;
2335 edit_set_codeset (edit);
2337 if (edit_load_file (edit))
2339 /* edit_load_file already gives an error message */
2340 if (to_free)
2341 g_free (edit);
2342 return NULL;
2345 edit->loading_done = 1;
2346 edit->modified = 0;
2347 edit->locked = 0;
2348 edit_load_syntax (edit, NULL, NULL);
2350 int color;
2351 edit_get_syntax_color (edit, -1, &color);
2354 /* load saved cursor position */
2355 if ((line == 0) && option_save_position)
2356 edit_load_position (edit);
2357 else
2359 if (line <= 0)
2360 line = 1;
2361 edit_move_display (edit, line - 1);
2362 edit_move_to_line (edit, line - 1);
2365 edit_load_macro_cmd (edit);
2366 return edit;
2369 /* --------------------------------------------------------------------------------------------- */
2370 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2373 edit_clean (WEdit * edit)
2375 int j = 0;
2377 if (!edit)
2378 return 0;
2380 /* a stale lock, remove it */
2381 if (edit->locked)
2382 edit->locked = unlock_file (edit->filename_vpath);
2384 /* save cursor position */
2385 if (option_save_position)
2386 edit_save_position (edit);
2387 else if (edit->serialized_bookmarks != NULL)
2388 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2390 /* File specified on the mcedit command line and never saved */
2391 if (edit->delete_file)
2392 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2394 edit_free_syntax_rules (edit);
2395 book_mark_flush (edit, -1);
2396 for (; j <= MAXBUFF; j++)
2398 g_free (edit->buffers1[j]);
2399 g_free (edit->buffers2[j]);
2402 g_free (edit->undo_stack);
2403 g_free (edit->redo_stack);
2404 vfs_path_free (edit->filename_vpath);
2405 vfs_path_free (edit->dir_vpath);
2406 mc_search_free (edit->search);
2407 edit->search = NULL;
2409 if (edit->converter != str_cnv_from_term)
2410 str_close_conv (edit->converter);
2412 edit_purge_widget (edit);
2414 return 1;
2417 /* --------------------------------------------------------------------------------------------- */
2418 /** returns 1 on success */
2421 edit_renew (WEdit * edit)
2423 int y = edit->widget.y;
2424 int x = edit->widget.x;
2425 int lines = edit->widget.lines;
2426 int columns = edit->widget.cols;
2428 edit_clean (edit);
2429 return (edit_init (edit, y, x, lines, columns, NULL, 0) != NULL);
2432 /* --------------------------------------------------------------------------------------------- */
2434 * Load a new file into the editor. If it fails, preserve the old file.
2435 * To do it, allocate a new widget, initialize it and, if the new file
2436 * was loaded, copy the data to the old widget.
2437 * Return 1 on success, 0 on failure.
2441 edit_reload (WEdit * edit, const vfs_path_t * filename_vpath)
2443 WEdit *e;
2444 int y = edit->widget.y;
2445 int x = edit->widget.x;
2446 int lines = edit->widget.lines;
2447 int columns = edit->widget.cols;
2449 e = g_malloc0 (sizeof (WEdit));
2450 e->widget = edit->widget;
2451 if (edit_init (e, y, x, lines, columns, filename_vpath, 0) == NULL)
2453 g_free (e);
2454 return 0;
2456 edit_clean (edit);
2457 memcpy (edit, e, sizeof (WEdit));
2458 g_free (e);
2459 return 1;
2462 /* --------------------------------------------------------------------------------------------- */
2464 * Load a new file into the editor and set line. If it fails, preserve the old file.
2465 * To do it, allocate a new widget, initialize it and, if the new file
2466 * was loaded, copy the data to the old widget.
2467 * Return 1 on success, 0 on failure.
2471 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2473 WEdit *e;
2474 int y = edit->widget.y;
2475 int x = edit->widget.x;
2476 int lines = edit->widget.lines;
2477 int columns = edit->widget.cols;
2479 e = g_malloc0 (sizeof (WEdit));
2480 e->widget = edit->widget;
2481 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2483 g_free (e);
2484 return 0;
2486 edit_clean (edit);
2487 memcpy (edit, e, sizeof (WEdit));
2488 g_free (e);
2489 return 1;
2492 /* --------------------------------------------------------------------------------------------- */
2494 void
2495 edit_set_codeset (WEdit * edit)
2497 #ifdef HAVE_CHARSET
2498 const char *cp_id;
2500 cp_id =
2501 get_codepage_id (mc_global.source_codepage >=
2502 0 ? mc_global.source_codepage : mc_global.display_codepage);
2504 if (cp_id != NULL)
2506 GIConv conv;
2507 conv = str_crt_conv_from (cp_id);
2508 if (conv != INVALID_CONV)
2510 if (edit->converter != str_cnv_from_term)
2511 str_close_conv (edit->converter);
2512 edit->converter = conv;
2516 if (cp_id != NULL)
2517 edit->utf8 = str_isutf8 (cp_id);
2518 #else
2519 (void) edit;
2520 #endif
2523 /* --------------------------------------------------------------------------------------------- */
2525 Recording stack for undo:
2526 The following is an implementation of a compressed stack. Identical
2527 pushes are recorded by a negative prefix indicating the number of times the
2528 same char was pushed. This saves space for repeated curs-left or curs-right
2529 delete etc.
2533 pushed: stored:
2537 b -3
2539 c --> -4
2545 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2546 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2547 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2548 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2549 position.
2551 The only way the cursor moves or the buffer is changed is through the routines:
2552 insert, backspace, insert_ahead, delete, and cursor_move.
2553 These record the reverse undo movements onto the stack each time they are
2554 called.
2556 Each key press results in a set of actions (insert; delete ...). So each time
2557 a key is pressed the current position of start_display is pushed as
2558 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2559 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2560 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2564 void
2565 edit_push_undo_action (WEdit * edit, long c, ...)
2567 unsigned long sp = edit->undo_stack_pointer;
2568 unsigned long spm1;
2569 long *t;
2571 /* first enlarge the stack if necessary */
2572 if (sp > edit->undo_stack_size - 10)
2573 { /* say */
2574 if (option_max_undo < 256)
2575 option_max_undo = 256;
2576 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2578 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2579 if (t)
2581 edit->undo_stack = t;
2582 edit->undo_stack_size <<= 1;
2583 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2587 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2588 if (edit->undo_stack_disable)
2590 edit_push_redo_action (edit, KEY_PRESS);
2591 edit_push_redo_action (edit, c);
2592 return;
2594 else if (edit->redo_stack_reset)
2596 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2599 if (edit->undo_stack_bottom != sp
2600 && spm1 != edit->undo_stack_bottom
2601 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2603 int d;
2604 if (edit->undo_stack[spm1] < 0)
2606 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2607 if (d == c)
2609 if (edit->undo_stack[spm1] > -1000000000)
2611 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2613 edit->undo_stack[spm1]--;
2615 return;
2619 else
2621 d = edit->undo_stack[spm1];
2622 if (d == c)
2624 if (c >= KEY_PRESS)
2625 return; /* --> no need to push multiple do-nothings */
2626 edit->undo_stack[sp] = -2;
2627 goto check_bottom;
2631 edit->undo_stack[sp] = c;
2633 check_bottom:
2634 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2636 /* if the sp wraps round and catches the undo_stack_bottom then erase
2637 * the first set of actions on the stack to make space - by moving
2638 * undo_stack_bottom forward one "key press" */
2639 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2640 if ((unsigned long) c == edit->undo_stack_bottom ||
2641 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2644 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2646 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2647 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2649 /*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: */
2650 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2651 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2653 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2657 void
2658 edit_push_redo_action (WEdit * edit, long c, ...)
2660 unsigned long sp = edit->redo_stack_pointer;
2661 unsigned long spm1;
2662 long *t;
2663 /* first enlarge the stack if necessary */
2664 if (sp > edit->redo_stack_size - 10)
2665 { /* say */
2666 if (option_max_undo < 256)
2667 option_max_undo = 256;
2668 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2670 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2671 if (t)
2673 edit->redo_stack = t;
2674 edit->redo_stack_size <<= 1;
2675 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2679 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2681 if (edit->redo_stack_bottom != sp
2682 && spm1 != edit->redo_stack_bottom
2683 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2685 int d;
2686 if (edit->redo_stack[spm1] < 0)
2688 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2689 if (d == c)
2691 if (edit->redo_stack[spm1] > -1000000000)
2693 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2694 edit->redo_stack[spm1]--;
2695 return;
2699 else
2701 d = edit->redo_stack[spm1];
2702 if (d == c)
2704 if (c >= KEY_PRESS)
2705 return; /* --> no need to push multiple do-nothings */
2706 edit->redo_stack[sp] = -2;
2707 goto redo_check_bottom;
2711 edit->redo_stack[sp] = c;
2713 redo_check_bottom:
2714 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2716 /* if the sp wraps round and catches the redo_stack_bottom then erase
2717 * the first set of actions on the stack to make space - by moving
2718 * redo_stack_bottom forward one "key press" */
2719 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2720 if ((unsigned long) c == edit->redo_stack_bottom ||
2721 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2724 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2726 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2727 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2730 * If a single key produced enough pushes to wrap all the way round then
2731 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2732 * The stack is then initialised:
2735 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2736 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2737 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2741 /* --------------------------------------------------------------------------------------------- */
2743 Basic low level single character buffer alterations and movements at the cursor.
2744 Returns char passed over, inserted or removed.
2747 void
2748 edit_insert (WEdit * edit, int c)
2750 /* check if file has grown to large */
2751 if (edit->last_byte >= SIZE_LIMIT)
2752 return;
2754 /* first we must update the position of the display window */
2755 if (edit->curs1 < edit->start_display)
2757 edit->start_display++;
2758 if (c == '\n')
2759 edit->start_line++;
2762 /* Mark file as modified, unless the file hasn't been fully loaded */
2763 if (edit->loading_done)
2765 edit_modification (edit);
2768 /* now we must update some info on the file and check if a redraw is required */
2769 if (c == '\n')
2771 if (edit->book_mark)
2772 book_mark_inc (edit, edit->curs_line);
2773 edit->curs_line++;
2774 edit->total_lines++;
2775 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2778 /* save the reverse command onto the undo stack */
2779 /* ordinary char and not space */
2780 if (c > 32)
2781 edit_push_undo_action (edit, BACKSPACE);
2782 else
2783 edit_push_undo_action (edit, BACKSPACE_BR);
2784 /* update markers */
2785 edit->mark1 += (edit->mark1 > edit->curs1);
2786 edit->mark2 += (edit->mark2 > edit->curs1);
2787 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2789 /* add a new buffer if we've reached the end of the last one */
2790 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2791 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2793 /* perform the insertion */
2794 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2795 = (unsigned char) c;
2797 /* update file length */
2798 edit->last_byte++;
2800 /* update cursor position */
2801 edit->curs1++;
2804 /* --------------------------------------------------------------------------------------------- */
2805 /** same as edit_insert and move left */
2807 void
2808 edit_insert_ahead (WEdit * edit, int c)
2810 if (edit->last_byte >= SIZE_LIMIT)
2811 return;
2813 if (edit->curs1 < edit->start_display)
2815 edit->start_display++;
2816 if (c == '\n')
2817 edit->start_line++;
2819 edit_modification (edit);
2820 if (c == '\n')
2822 if (edit->book_mark)
2823 book_mark_inc (edit, edit->curs_line);
2824 edit->total_lines++;
2825 edit->force |= REDRAW_AFTER_CURSOR;
2827 /* ordinary char and not space */
2828 if (c > 32)
2829 edit_push_undo_action (edit, DELCHAR);
2830 else
2831 edit_push_undo_action (edit, DELCHAR_BR);
2833 edit->mark1 += (edit->mark1 >= edit->curs1);
2834 edit->mark2 += (edit->mark2 >= edit->curs1);
2835 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2837 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2838 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2839 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2840 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2842 edit->last_byte++;
2843 edit->curs2++;
2847 /* --------------------------------------------------------------------------------------------- */
2850 edit_delete (WEdit * edit, const int byte_delete)
2852 int p = 0;
2853 int cw = 1;
2854 int i;
2856 if (!edit->curs2)
2857 return 0;
2859 cw = 1;
2860 /* if byte_delete = 1 then delete only one byte not multibyte char */
2861 if (edit->utf8 && byte_delete == 0)
2863 edit_get_utf (edit, edit->curs1, &cw);
2864 if (cw < 1)
2865 cw = 1;
2868 if (edit->mark2 != edit->mark1)
2869 edit_push_markers (edit);
2871 for (i = 1; i <= cw; i++)
2873 if (edit->mark1 > edit->curs1)
2875 edit->mark1--;
2876 edit->end_mark_curs--;
2878 if (edit->mark2 > edit->curs1)
2879 edit->mark2--;
2880 if (edit->last_get_rule > edit->curs1)
2881 edit->last_get_rule--;
2883 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2884 ((edit->curs2 -
2885 1) & M_EDIT_BUF_SIZE) - 1];
2887 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2889 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2890 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2892 edit->last_byte--;
2893 edit->curs2--;
2894 edit_push_undo_action (edit, p + 256);
2897 edit_modification (edit);
2898 if (p == '\n')
2900 if (edit->book_mark)
2901 book_mark_dec (edit, edit->curs_line);
2902 edit->total_lines--;
2903 edit->force |= REDRAW_AFTER_CURSOR;
2905 if (edit->curs1 < edit->start_display)
2907 edit->start_display--;
2908 if (p == '\n')
2909 edit->start_line--;
2912 return p;
2915 /* --------------------------------------------------------------------------------------------- */
2916 /** moves the cursor right or left: increment positive or negative respectively */
2918 void
2919 edit_cursor_move (WEdit * edit, long increment)
2921 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2922 int c;
2924 if (increment < 0)
2926 for (; increment < 0; increment++)
2928 if (!edit->curs1)
2929 return;
2931 edit_push_undo_action (edit, CURS_RIGHT);
2933 c = edit_get_byte (edit, edit->curs1 - 1);
2934 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2935 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2936 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2937 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2938 edit->curs2++;
2939 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2940 1) & M_EDIT_BUF_SIZE];
2941 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2943 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2944 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2946 edit->curs1--;
2947 if (c == '\n')
2949 edit->curs_line--;
2950 edit->force |= REDRAW_LINE_BELOW;
2955 else if (increment > 0)
2957 for (; increment > 0; increment--)
2959 if (!edit->curs2)
2960 return;
2962 edit_push_undo_action (edit, CURS_LEFT);
2964 c = edit_get_byte (edit, edit->curs1);
2965 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2966 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2967 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2968 edit->curs1++;
2969 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2970 ((edit->curs2 -
2971 1) & M_EDIT_BUF_SIZE) - 1];
2972 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2974 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2975 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2977 edit->curs2--;
2978 if (c == '\n')
2980 edit->curs_line++;
2981 edit->force |= REDRAW_LINE_ABOVE;
2987 /* These functions return positions relative to lines */
2989 /* --------------------------------------------------------------------------------------------- */
2990 /** returns index of last char on line + 1 */
2992 long
2993 edit_eol (WEdit * edit, long current)
2995 if (current >= edit->last_byte)
2996 return edit->last_byte;
2998 for (;; current++)
2999 if (edit_get_byte (edit, current) == '\n')
3000 break;
3001 return current;
3004 /* --------------------------------------------------------------------------------------------- */
3005 /** returns index of first char on line */
3007 long
3008 edit_bol (WEdit * edit, long current)
3010 if (current <= 0)
3011 return 0;
3013 for (;; current--)
3014 if (edit_get_byte (edit, current - 1) == '\n')
3015 break;
3016 return current;
3019 /* --------------------------------------------------------------------------------------------- */
3021 long
3022 edit_count_lines (WEdit * edit, long current, long upto)
3024 long lines = 0;
3025 if (upto > edit->last_byte)
3026 upto = edit->last_byte;
3027 if (current < 0)
3028 current = 0;
3029 while (current < upto)
3030 if (edit_get_byte (edit, current++) == '\n')
3031 lines++;
3032 return lines;
3035 /* --------------------------------------------------------------------------------------------- */
3036 /* If lines is zero this returns the count of lines from current to upto. */
3037 /* If upto is zero returns index of lines forward current. */
3039 long
3040 edit_move_forward (WEdit * edit, long current, long lines, long upto)
3042 if (upto)
3044 return edit_count_lines (edit, current, upto);
3046 else
3048 long next;
3049 if (lines < 0)
3050 lines = 0;
3051 while (lines--)
3053 next = edit_eol (edit, current) + 1;
3054 if (next > edit->last_byte)
3055 break;
3056 else
3057 current = next;
3059 return current;
3063 /* --------------------------------------------------------------------------------------------- */
3064 /** Returns offset of 'lines' lines up from current */
3066 long
3067 edit_move_backward (WEdit * edit, long current, long lines)
3069 if (lines < 0)
3070 lines = 0;
3071 current = edit_bol (edit, current);
3072 while ((lines--) && current != 0)
3073 current = edit_bol (edit, current - 1);
3074 return current;
3077 /* --------------------------------------------------------------------------------------------- */
3078 /* If cols is zero this returns the count of columns from current to upto. */
3079 /* If upto is zero returns index of cols across from current. */
3081 long
3082 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3084 long p, q;
3085 int col;
3087 if (upto)
3089 q = upto;
3090 cols = -10;
3092 else
3093 q = edit->last_byte + 2;
3095 for (col = 0, p = current; p < q; p++)
3097 int c, orig_c;
3099 if (cols != -10)
3101 if (col == cols)
3102 return p;
3103 if (col > cols)
3104 return p - 1;
3107 orig_c = c = edit_get_byte (edit, p);
3109 #ifdef HAVE_CHARSET
3110 if (edit->utf8)
3112 int utf_ch;
3113 int cw = 1;
3115 utf_ch = edit_get_utf (edit, p, &cw);
3116 if (mc_global.utf8_display)
3118 if (cw > 1)
3119 col -= cw - 1;
3120 if (g_unichar_iswide (utf_ch))
3121 col++;
3123 else if (cw > 1 && g_unichar_isprint (utf_ch))
3124 col -= cw - 1;
3127 c = convert_to_display_c (c);
3128 #endif
3130 if (c == '\t')
3131 col += TAB_SIZE - col % TAB_SIZE;
3132 else if (c == '\n')
3134 if (upto)
3135 return col;
3136 else
3137 return p;
3139 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3140 /* '\r' is shown as ^M, so we must advance 2 characters */
3141 /* Caret notation for control characters */
3142 col += 2;
3143 else
3144 col++;
3146 return col;
3149 /* --------------------------------------------------------------------------------------------- */
3150 /** returns the current column position of the cursor */
3153 edit_get_col (WEdit * edit)
3155 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3158 /* --------------------------------------------------------------------------------------------- */
3159 /* Scrolling functions */
3160 /* --------------------------------------------------------------------------------------------- */
3162 void
3163 edit_update_curs_row (WEdit * edit)
3165 edit->curs_row = edit->curs_line - edit->start_line;
3168 /* --------------------------------------------------------------------------------------------- */
3170 void
3171 edit_update_curs_col (WEdit * edit)
3173 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3176 /* --------------------------------------------------------------------------------------------- */
3179 edit_get_curs_col (const WEdit * edit)
3181 return edit->curs_col;
3184 /* --------------------------------------------------------------------------------------------- */
3185 /** moves the display start position up by i lines */
3187 void
3188 edit_scroll_upward (WEdit * edit, unsigned long i)
3190 unsigned long lines_above = edit->start_line;
3191 if (i > lines_above)
3192 i = lines_above;
3193 if (i)
3195 edit->start_line -= i;
3196 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3197 edit->force |= REDRAW_PAGE;
3198 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3200 edit_update_curs_row (edit);
3204 /* --------------------------------------------------------------------------------------------- */
3205 /** returns 1 if could scroll, 0 otherwise */
3207 void
3208 edit_scroll_downward (WEdit * edit, int i)
3210 int lines_below;
3211 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3212 if (lines_below > 0)
3214 if (i > lines_below)
3215 i = lines_below;
3216 edit->start_line += i;
3217 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3218 edit->force |= REDRAW_PAGE;
3219 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3221 edit_update_curs_row (edit);
3224 /* --------------------------------------------------------------------------------------------- */
3226 void
3227 edit_scroll_right (WEdit * edit, int i)
3229 edit->force |= REDRAW_PAGE;
3230 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3231 edit->start_col -= i;
3234 /* --------------------------------------------------------------------------------------------- */
3236 void
3237 edit_scroll_left (WEdit * edit, int i)
3239 if (edit->start_col)
3241 edit->start_col += i;
3242 if (edit->start_col > 0)
3243 edit->start_col = 0;
3244 edit->force |= REDRAW_PAGE;
3245 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3249 /* --------------------------------------------------------------------------------------------- */
3250 /* high level cursor movement commands */
3251 /* --------------------------------------------------------------------------------------------- */
3253 void
3254 edit_move_to_prev_col (WEdit * edit, long p)
3256 int prev = edit->prev_col;
3257 int over = edit->over_col;
3258 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3260 if (option_cursor_beyond_eol)
3262 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3263 edit_eol (edit, edit->curs1));
3265 if (line_len < prev + edit->over_col)
3267 edit->over_col = prev + over - line_len;
3268 edit->prev_col = line_len;
3269 edit->curs_col = line_len;
3271 else
3273 edit->curs_col = prev + over;
3274 edit->prev_col = edit->curs_col;
3275 edit->over_col = 0;
3278 else
3280 edit->over_col = 0;
3281 if (is_in_indent (edit) && option_fake_half_tabs)
3283 edit_update_curs_col (edit);
3284 if (space_width)
3285 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3287 int q = edit->curs_col;
3288 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3289 p = edit_bol (edit, edit->curs1);
3290 edit_cursor_move (edit,
3291 edit_move_forward3 (edit, p, edit->curs_col,
3292 0) - edit->curs1);
3293 if (!left_of_four_spaces (edit))
3294 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3300 /* --------------------------------------------------------------------------------------------- */
3303 line_is_blank (WEdit * edit, long line)
3305 return is_blank (edit, edit_find_line (edit, line));
3308 /* --------------------------------------------------------------------------------------------- */
3309 /** move cursor to line 'line' */
3311 void
3312 edit_move_to_line (WEdit * e, long line)
3314 if (line < e->curs_line)
3315 edit_move_up (e, e->curs_line - line, 0);
3316 else
3317 edit_move_down (e, line - e->curs_line, 0);
3318 edit_scroll_screen_over_cursor (e);
3321 /* --------------------------------------------------------------------------------------------- */
3322 /** scroll window so that first visible line is 'line' */
3324 void
3325 edit_move_display (WEdit * e, long line)
3327 if (line < e->start_line)
3328 edit_scroll_upward (e, e->start_line - line);
3329 else
3330 edit_scroll_downward (e, line - e->start_line);
3333 /* --------------------------------------------------------------------------------------------- */
3334 /** save markers onto undo stack */
3336 void
3337 edit_push_markers (WEdit * edit)
3339 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3340 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3341 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3344 /* --------------------------------------------------------------------------------------------- */
3346 void
3347 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3349 edit->mark1 = m1;
3350 edit->mark2 = m2;
3351 edit->column1 = c1;
3352 edit->column2 = c2;
3356 /* --------------------------------------------------------------------------------------------- */
3357 /** highlight marker toggle */
3359 void
3360 edit_mark_cmd (WEdit * edit, int unmark)
3362 edit_push_markers (edit);
3363 if (unmark)
3365 edit_set_markers (edit, 0, 0, 0, 0);
3366 edit->force |= REDRAW_PAGE;
3368 else
3370 if (edit->mark2 >= 0)
3372 edit->end_mark_curs = -1;
3373 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3374 edit->curs_col + edit->over_col);
3375 edit->force |= REDRAW_PAGE;
3377 else
3379 edit->end_mark_curs = edit->curs1;
3380 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3381 edit->curs_col + edit->over_col);
3386 /* --------------------------------------------------------------------------------------------- */
3387 /** highlight the word under cursor */
3389 void
3390 edit_mark_current_word_cmd (WEdit * edit)
3392 long pos;
3394 for (pos = edit->curs1; pos != 0; pos--)
3396 int c1, c2;
3398 c1 = edit_get_byte (edit, pos);
3399 c2 = edit_get_byte (edit, pos - 1);
3400 if (!isspace (c1) && isspace (c2))
3401 break;
3402 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3403 break;
3405 edit->mark1 = pos;
3407 for (; pos < edit->last_byte; pos++)
3409 int c1, c2;
3411 c1 = edit_get_byte (edit, pos);
3412 c2 = edit_get_byte (edit, pos + 1);
3413 if (!isspace (c1) && isspace (c2))
3414 break;
3415 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3416 break;
3418 edit->mark2 = min (pos + 1, edit->last_byte);
3420 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3423 /* --------------------------------------------------------------------------------------------- */
3425 void
3426 edit_mark_current_line_cmd (WEdit * edit)
3428 long pos = edit->curs1;
3430 edit->mark1 = edit_bol (edit, pos);
3431 edit->mark2 = edit_eol (edit, pos);
3433 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3436 /* --------------------------------------------------------------------------------------------- */
3438 void
3439 edit_delete_line (WEdit * edit)
3442 * Delete right part of the line.
3443 * Note that edit_get_byte() returns '\n' when byte position is
3444 * beyond EOF.
3446 while (edit_get_byte (edit, edit->curs1) != '\n')
3448 (void) edit_delete (edit, 1);
3452 * Delete '\n' char.
3453 * Note that edit_delete() will not corrupt anything if called while
3454 * cursor position is EOF.
3456 (void) edit_delete (edit, 1);
3459 * Delete left part of the line.
3460 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3462 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3464 (void) edit_backspace (edit, 1);
3468 /* --------------------------------------------------------------------------------------------- */
3471 edit_indent_width (WEdit * edit, long p)
3473 long q = p;
3474 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3475 q++;
3476 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3479 /* --------------------------------------------------------------------------------------------- */
3481 void
3482 edit_insert_indent (WEdit * edit, int indent)
3484 if (!option_fill_tabs_with_spaces)
3486 while (indent >= TAB_SIZE)
3488 edit_insert (edit, '\t');
3489 indent -= TAB_SIZE;
3492 while (indent-- > 0)
3493 edit_insert (edit, ' ');
3496 /* --------------------------------------------------------------------------------------------- */
3498 void
3499 edit_push_key_press (WEdit * edit)
3501 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3502 if (edit->mark2 == -1)
3504 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3505 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3509 /* --------------------------------------------------------------------------------------------- */
3511 void
3512 edit_find_bracket (WEdit * edit)
3514 edit->bracket = edit_get_bracket (edit, 1, 10000);
3515 if (last_bracket != edit->bracket)
3516 edit->force |= REDRAW_PAGE;
3517 last_bracket = edit->bracket;
3520 /* --------------------------------------------------------------------------------------------- */
3522 * This executes a command as though the user initiated it through a key
3523 * press. Callback with WIDGET_KEY as a message calls this after
3524 * translating the key press. This function can be used to pass any
3525 * command to the editor. Note that the screen wouldn't update
3526 * automatically. Either of command or char_for_insertion must be
3527 * passed as -1. Commands are executed, and char_for_insertion is
3528 * inserted at the cursor.
3531 void
3532 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3534 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3535 || (macro_index < 0
3536 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3538 macro_index = 0;
3539 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3540 return;
3542 if (macro_index != -1)
3544 edit->force |= REDRAW_COMPLETELY;
3545 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3547 edit_store_macro_cmd (edit);
3548 macro_index = -1;
3549 return;
3551 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3553 edit_repeat_macro_cmd (edit);
3554 macro_index = -1;
3555 return;
3559 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3561 record_macro_buf[macro_index].action = command;
3562 record_macro_buf[macro_index++].ch = char_for_insertion;
3564 /* record the beginning of a set of editing actions initiated by a key press */
3565 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3566 edit_push_key_press (edit);
3568 edit_execute_cmd (edit, command, char_for_insertion);
3569 if (edit->column_highlight)
3570 edit->force |= REDRAW_PAGE;
3573 /* --------------------------------------------------------------------------------------------- */
3575 This executes a command at a lower level than macro recording.
3576 It also does not push a key_press onto the undo stack. This means
3577 that if it is called many times, a single undo command will undo
3578 all of them. It also does not check for the Undo command.
3580 void
3581 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3583 edit->force |= REDRAW_LINE;
3585 /* The next key press will unhighlight the found string, so update
3586 * the whole page */
3587 if (edit->found_len || edit->column_highlight)
3588 edit->force |= REDRAW_PAGE;
3590 switch (command)
3592 /* a mark command with shift-arrow */
3593 case CK_MarkLeft:
3594 case CK_MarkRight:
3595 case CK_MarkToWordBegin:
3596 case CK_MarkToWordEnd:
3597 case CK_MarkToHome:
3598 case CK_MarkToEnd:
3599 case CK_MarkUp:
3600 case CK_MarkDown:
3601 case CK_MarkPageUp:
3602 case CK_MarkPageDown:
3603 case CK_MarkToFileBegin:
3604 case CK_MarkToFileEnd:
3605 case CK_MarkToPageBegin:
3606 case CK_MarkToPageEnd:
3607 case CK_MarkScrollUp:
3608 case CK_MarkScrollDown:
3609 case CK_MarkParagraphUp:
3610 case CK_MarkParagraphDown:
3611 /* a mark command with alt-arrow */
3612 case CK_MarkColumnPageUp:
3613 case CK_MarkColumnPageDown:
3614 case CK_MarkColumnLeft:
3615 case CK_MarkColumnRight:
3616 case CK_MarkColumnUp:
3617 case CK_MarkColumnDown:
3618 case CK_MarkColumnScrollUp:
3619 case CK_MarkColumnScrollDown:
3620 case CK_MarkColumnParagraphUp:
3621 case CK_MarkColumnParagraphDown:
3622 edit->column_highlight = 0;
3623 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3625 edit_mark_cmd (edit, 1); /* clear */
3626 edit_mark_cmd (edit, 0); /* marking on */
3628 edit->highlight = 1;
3629 break;
3631 /* any other command */
3632 default:
3633 if (edit->highlight)
3634 edit_mark_cmd (edit, 0); /* clear */
3635 edit->highlight = 0;
3638 /* first check for undo */
3639 if (command == CK_Undo)
3641 edit->redo_stack_reset = 0;
3642 edit_group_undo (edit);
3643 edit->found_len = 0;
3644 edit->prev_col = edit_get_col (edit);
3645 edit->search_start = edit->curs1;
3646 return;
3648 /* check for redo */
3649 if (command == CK_Redo)
3651 edit->redo_stack_reset = 0;
3652 edit_do_redo (edit);
3653 edit->found_len = 0;
3654 edit->prev_col = edit_get_col (edit);
3655 edit->search_start = edit->curs1;
3656 return;
3659 edit->redo_stack_reset = 1;
3661 /* An ordinary key press */
3662 if (char_for_insertion >= 0)
3664 /* if non persistent selection and text selected */
3665 if (!option_persistent_selections)
3667 if (edit->mark1 != edit->mark2)
3668 edit_block_delete_cmd (edit);
3670 if (edit->overwrite)
3672 /* remove char only one time, after input first byte, multibyte chars */
3673 if ((!mc_global.utf8_display || edit->charpoint == 0)
3674 && edit_get_byte (edit, edit->curs1) != '\n')
3675 edit_delete (edit, 0);
3677 if (option_cursor_beyond_eol && edit->over_col > 0)
3678 edit_insert_over (edit);
3679 #ifdef HAVE_CHARSET
3680 if (char_for_insertion > 255 && !mc_global.utf8_display)
3682 unsigned char str[6 + 1];
3683 size_t i = 0;
3684 int res;
3686 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3687 if (res == 0)
3689 str[0] = '.';
3690 str[1] = '\0';
3692 else
3694 str[res] = '\0';
3696 while (str[i] != 0 && i <= 6)
3698 char_for_insertion = str[i];
3699 edit_insert (edit, char_for_insertion);
3700 i++;
3703 else
3704 #endif
3705 edit_insert (edit, char_for_insertion);
3707 if (option_auto_para_formatting)
3709 format_paragraph (edit, 0);
3710 edit->force |= REDRAW_PAGE;
3712 else
3713 check_and_wrap_line (edit);
3714 edit->found_len = 0;
3715 edit->prev_col = edit_get_col (edit);
3716 edit->search_start = edit->curs1;
3717 edit_find_bracket (edit);
3718 return;
3721 switch (command)
3723 case CK_TopOnScreen:
3724 case CK_BottomOnScreen:
3725 case CK_Top:
3726 case CK_Bottom:
3727 case CK_PageUp:
3728 case CK_PageDown:
3729 case CK_Home:
3730 case CK_End:
3731 case CK_Up:
3732 case CK_Down:
3733 case CK_Left:
3734 case CK_Right:
3735 case CK_WordLeft:
3736 case CK_WordRight:
3737 if (edit->mark2 >= 0)
3739 if (!option_persistent_selections)
3741 if (edit->column_highlight)
3742 edit_push_undo_action (edit, COLUMN_ON);
3743 edit->column_highlight = 0;
3744 edit_mark_cmd (edit, 1);
3749 switch (command)
3751 case CK_TopOnScreen:
3752 case CK_BottomOnScreen:
3753 case CK_MarkToPageBegin:
3754 case CK_MarkToPageEnd:
3755 case CK_Up:
3756 case CK_Down:
3757 case CK_WordLeft:
3758 case CK_WordRight:
3759 case CK_MarkToWordBegin:
3760 case CK_MarkToWordEnd:
3761 case CK_MarkUp:
3762 case CK_MarkDown:
3763 case CK_MarkColumnUp:
3764 case CK_MarkColumnDown:
3765 if (edit->mark2 == -1)
3766 break; /*marking is following the cursor: may need to highlight a whole line */
3767 case CK_Left:
3768 case CK_Right:
3769 case CK_MarkLeft:
3770 case CK_MarkRight:
3771 edit->force |= REDRAW_CHAR_ONLY;
3774 /* basic cursor key commands */
3775 switch (command)
3777 case CK_BackSpace:
3778 /* if non persistent selection and text selected */
3779 if (!option_persistent_selections)
3781 if (edit->mark1 != edit->mark2)
3783 edit_block_delete_cmd (edit);
3784 break;
3787 if (option_cursor_beyond_eol && edit->over_col > 0)
3789 edit->over_col--;
3790 break;
3792 if (option_backspace_through_tabs && is_in_indent (edit))
3794 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3795 edit_backspace (edit, 1);
3796 break;
3798 else
3800 if (option_fake_half_tabs)
3802 int i;
3803 if (is_in_indent (edit) && right_of_four_spaces (edit))
3805 for (i = 0; i < HALF_TAB_SIZE; i++)
3806 edit_backspace (edit, 1);
3807 break;
3811 edit_backspace (edit, 0);
3812 break;
3813 case CK_Delete:
3814 /* if non persistent selection and text selected */
3815 if (!option_persistent_selections)
3817 if (edit->mark1 != edit->mark2)
3819 edit_block_delete_cmd (edit);
3820 break;
3824 if (option_cursor_beyond_eol && edit->over_col > 0)
3825 edit_insert_over (edit);
3827 if (option_fake_half_tabs)
3829 int i;
3830 if (is_in_indent (edit) && left_of_four_spaces (edit))
3832 for (i = 1; i <= HALF_TAB_SIZE; i++)
3833 edit_delete (edit, 1);
3834 break;
3837 edit_delete (edit, 0);
3838 break;
3839 case CK_DeleteToWordBegin:
3840 edit->over_col = 0;
3841 edit_left_delete_word (edit);
3842 break;
3843 case CK_DeleteToWordEnd:
3844 if (option_cursor_beyond_eol && edit->over_col > 0)
3845 edit_insert_over (edit);
3847 edit_right_delete_word (edit);
3848 break;
3849 case CK_DeleteLine:
3850 edit_delete_line (edit);
3851 break;
3852 case CK_DeleteToHome:
3853 edit_delete_to_line_begin (edit);
3854 break;
3855 case CK_DeleteToEnd:
3856 edit_delete_to_line_end (edit);
3857 break;
3858 case CK_Enter:
3859 edit->over_col = 0;
3860 if (option_auto_para_formatting)
3862 edit_double_newline (edit);
3863 if (option_return_does_auto_indent)
3864 edit_auto_indent (edit);
3865 format_paragraph (edit, 0);
3867 else
3869 edit_insert (edit, '\n');
3870 if (option_return_does_auto_indent)
3872 edit_auto_indent (edit);
3875 break;
3876 case CK_Return:
3877 edit_insert (edit, '\n');
3878 break;
3880 case CK_MarkColumnPageUp:
3881 edit->column_highlight = 1;
3882 case CK_PageUp:
3883 case CK_MarkPageUp:
3884 edit_move_up (edit, edit->widget.lines - 1, 1);
3885 break;
3886 case CK_MarkColumnPageDown:
3887 edit->column_highlight = 1;
3888 case CK_PageDown:
3889 case CK_MarkPageDown:
3890 edit_move_down (edit, edit->widget.lines - 1, 1);
3891 break;
3892 case CK_MarkColumnLeft:
3893 edit->column_highlight = 1;
3894 case CK_Left:
3895 case CK_MarkLeft:
3896 if (option_fake_half_tabs)
3898 if (is_in_indent (edit) && right_of_four_spaces (edit))
3900 if (option_cursor_beyond_eol && edit->over_col > 0)
3901 edit->over_col--;
3902 else
3903 edit_cursor_move (edit, -HALF_TAB_SIZE);
3904 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3905 break;
3908 edit_left_char_move_cmd (edit);
3909 break;
3910 case CK_MarkColumnRight:
3911 edit->column_highlight = 1;
3912 case CK_Right:
3913 case CK_MarkRight:
3914 if (option_fake_half_tabs)
3916 if (is_in_indent (edit) && left_of_four_spaces (edit))
3918 edit_cursor_move (edit, HALF_TAB_SIZE);
3919 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3920 break;
3923 edit_right_char_move_cmd (edit);
3924 break;
3925 case CK_TopOnScreen:
3926 case CK_MarkToPageBegin:
3927 edit_begin_page (edit);
3928 break;
3929 case CK_BottomOnScreen:
3930 case CK_MarkToPageEnd:
3931 edit_end_page (edit);
3932 break;
3933 case CK_WordLeft:
3934 case CK_MarkToWordBegin:
3935 edit->over_col = 0;
3936 edit_left_word_move_cmd (edit);
3937 break;
3938 case CK_WordRight:
3939 case CK_MarkToWordEnd:
3940 edit->over_col = 0;
3941 edit_right_word_move_cmd (edit);
3942 break;
3943 case CK_MarkColumnUp:
3944 edit->column_highlight = 1;
3945 case CK_Up:
3946 case CK_MarkUp:
3947 edit_move_up (edit, 1, 0);
3948 break;
3949 case CK_MarkColumnDown:
3950 edit->column_highlight = 1;
3951 case CK_Down:
3952 case CK_MarkDown:
3953 edit_move_down (edit, 1, 0);
3954 break;
3955 case CK_MarkColumnParagraphUp:
3956 edit->column_highlight = 1;
3957 case CK_ParagraphUp:
3958 case CK_MarkParagraphUp:
3959 edit_move_up_paragraph (edit, 0);
3960 break;
3961 case CK_MarkColumnParagraphDown:
3962 edit->column_highlight = 1;
3963 case CK_ParagraphDown:
3964 case CK_MarkParagraphDown:
3965 edit_move_down_paragraph (edit, 0);
3966 break;
3967 case CK_MarkColumnScrollUp:
3968 edit->column_highlight = 1;
3969 case CK_ScrollUp:
3970 case CK_MarkScrollUp:
3971 edit_move_up (edit, 1, 1);
3972 break;
3973 case CK_MarkColumnScrollDown:
3974 edit->column_highlight = 1;
3975 case CK_ScrollDown:
3976 case CK_MarkScrollDown:
3977 edit_move_down (edit, 1, 1);
3978 break;
3979 case CK_Home:
3980 case CK_MarkToHome:
3981 edit_cursor_to_bol (edit);
3982 break;
3983 case CK_End:
3984 case CK_MarkToEnd:
3985 edit_cursor_to_eol (edit);
3986 break;
3987 case CK_Tab:
3988 /* if text marked shift block */
3989 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3991 if (edit->mark2 < 0)
3992 edit_mark_cmd (edit, 0);
3993 edit_move_block_to_right (edit);
3995 else
3997 if (option_cursor_beyond_eol)
3998 edit_insert_over (edit);
3999 edit_tab_cmd (edit);
4000 if (option_auto_para_formatting)
4002 format_paragraph (edit, 0);
4003 edit->force |= REDRAW_PAGE;
4005 else
4007 check_and_wrap_line (edit);
4010 break;
4012 case CK_InsertOverwrite:
4013 edit->overwrite = !edit->overwrite;
4014 break;
4016 case CK_Mark:
4017 if (edit->mark2 >= 0)
4019 if (edit->column_highlight)
4020 edit_push_undo_action (edit, COLUMN_ON);
4021 edit->column_highlight = 0;
4023 edit_mark_cmd (edit, 0);
4024 break;
4025 case CK_MarkColumn:
4026 if (!edit->column_highlight)
4027 edit_push_undo_action (edit, COLUMN_OFF);
4028 edit->column_highlight = 1;
4029 edit_mark_cmd (edit, 0);
4030 break;
4031 case CK_MarkAll:
4032 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
4033 edit->force |= REDRAW_PAGE;
4034 break;
4035 case CK_Unmark:
4036 if (edit->column_highlight)
4037 edit_push_undo_action (edit, COLUMN_ON);
4038 edit->column_highlight = 0;
4039 edit_mark_cmd (edit, 1);
4040 break;
4041 case CK_MarkWord:
4042 if (edit->column_highlight)
4043 edit_push_undo_action (edit, COLUMN_ON);
4044 edit->column_highlight = 0;
4045 edit_mark_current_word_cmd (edit);
4046 break;
4047 case CK_MarkLine:
4048 if (edit->column_highlight)
4049 edit_push_undo_action (edit, COLUMN_ON);
4050 edit->column_highlight = 0;
4051 edit_mark_current_line_cmd (edit);
4052 break;
4054 case CK_ShowNumbers:
4055 option_line_state = !option_line_state;
4056 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
4057 edit->force |= REDRAW_PAGE;
4058 break;
4060 case CK_ShowMargin:
4061 show_right_margin = !show_right_margin;
4062 edit->force |= REDRAW_PAGE;
4063 break;
4065 case CK_Bookmark:
4066 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4067 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4068 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4069 else
4070 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4071 break;
4072 case CK_BookmarkFlush:
4073 book_mark_flush (edit, BOOK_MARK_COLOR);
4074 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4075 edit->force |= REDRAW_PAGE;
4076 break;
4077 case CK_BookmarkNext:
4078 if (edit->book_mark)
4080 struct _book_mark *p;
4081 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4082 if (p->next)
4084 p = p->next;
4085 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4086 edit_move_display (edit, p->line - edit->widget.lines / 2);
4087 edit_move_to_line (edit, p->line);
4090 break;
4091 case CK_BookmarkPrev:
4092 if (edit->book_mark)
4094 struct _book_mark *p;
4095 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4096 while (p->line == edit->curs_line)
4097 if (p->prev)
4098 p = p->prev;
4099 if (p->line >= 0)
4101 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4102 edit_move_display (edit, p->line - edit->widget.lines / 2);
4103 edit_move_to_line (edit, p->line);
4106 break;
4108 case CK_Top:
4109 case CK_MarkToFileBegin:
4110 edit_move_to_top (edit);
4111 break;
4112 case CK_Bottom:
4113 case CK_MarkToFileEnd:
4114 edit_move_to_bottom (edit);
4115 break;
4117 case CK_Copy:
4118 if (option_cursor_beyond_eol && edit->over_col > 0)
4119 edit_insert_over (edit);
4120 edit_block_copy_cmd (edit);
4121 break;
4122 case CK_Remove:
4123 edit_block_delete_cmd (edit);
4124 break;
4125 case CK_Move:
4126 edit_block_move_cmd (edit);
4127 break;
4129 case CK_BlockShiftLeft:
4130 if (edit->mark1 != edit->mark2)
4131 edit_move_block_to_left (edit);
4132 break;
4133 case CK_BlockShiftRight:
4134 if (edit->mark1 != edit->mark2)
4135 edit_move_block_to_right (edit);
4136 break;
4137 case CK_Store:
4138 edit_copy_to_X_buf_cmd (edit);
4139 break;
4140 case CK_Cut:
4141 edit_cut_to_X_buf_cmd (edit);
4142 break;
4143 case CK_Paste:
4144 /* if non persistent selection and text selected */
4145 if (!option_persistent_selections)
4147 if (edit->mark1 != edit->mark2)
4148 edit_block_delete_cmd (edit);
4150 if (option_cursor_beyond_eol && edit->over_col > 0)
4151 edit_insert_over (edit);
4152 edit_paste_from_X_buf_cmd (edit);
4153 break;
4154 case CK_History:
4155 edit_paste_from_history (edit);
4156 break;
4158 case CK_SaveAs:
4159 edit_save_as_cmd (edit);
4160 break;
4161 case CK_Save:
4162 edit_save_confirm_cmd (edit);
4163 break;
4164 case CK_EditFile:
4165 edit_load_cmd (edit, EDIT_FILE_COMMON);
4166 break;
4167 case CK_BlockSave:
4168 edit_save_block_cmd (edit);
4169 break;
4170 case CK_InsertFile:
4171 edit_insert_file_cmd (edit);
4172 break;
4174 case CK_FilePrev:
4175 edit_load_back_cmd (edit);
4176 break;
4177 case CK_FileNext:
4178 edit_load_forward_cmd (edit);
4179 break;
4181 case CK_EditSyntaxFile:
4182 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4183 break;
4184 case CK_SyntaxChoose:
4185 edit_syntax_dialog (edit);
4186 break;
4188 case CK_EditUserMenu:
4189 edit_load_cmd (edit, EDIT_FILE_MENU);
4190 break;
4192 case CK_SyntaxOnOff:
4193 option_syntax_highlighting ^= 1;
4194 if (option_syntax_highlighting == 1)
4195 edit_load_syntax (edit, NULL, edit->syntax_type);
4196 edit->force |= REDRAW_PAGE;
4197 break;
4199 case CK_ShowTabTws:
4200 enable_show_tabs_tws ^= 1;
4201 edit->force |= REDRAW_PAGE;
4202 break;
4204 case CK_Search:
4205 edit_search_cmd (edit, FALSE);
4206 break;
4207 case CK_SearchContinue:
4208 edit_search_cmd (edit, TRUE);
4209 break;
4210 case CK_Replace:
4211 edit_replace_cmd (edit, 0);
4212 break;
4213 case CK_ReplaceContinue:
4214 edit_replace_cmd (edit, 1);
4215 break;
4216 case CK_Complete:
4217 /* if text marked shift block */
4218 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4220 edit_move_block_to_left (edit);
4222 else
4224 edit_complete_word_cmd (edit);
4226 break;
4227 case CK_Find:
4228 edit_get_match_keyword_cmd (edit);
4229 break;
4230 case CK_Quit:
4231 dlg_stop (edit->widget.owner);
4232 break;
4233 case CK_EditNew:
4234 edit_new_cmd (edit);
4235 break;
4236 case CK_Help:
4237 edit_help_cmd (edit);
4238 break;
4239 case CK_Refresh:
4240 edit_refresh_cmd (edit);
4241 break;
4242 case CK_SaveSetup:
4243 save_setup_cmd ();
4244 break;
4245 case CK_About:
4246 edit_about ();
4247 break;
4248 case CK_LearnKeys:
4249 learn_keys ();
4250 break;
4251 case CK_Options:
4252 edit_options_dialog (edit);
4253 break;
4254 case CK_OptionsSaveMode:
4255 menu_save_mode_cmd ();
4256 break;
4257 case CK_Date:
4259 char s[BUF_MEDIUM];
4260 /* fool gcc to prevent a Y2K warning */
4261 char time_format[] = "_c";
4262 time_format[0] = '%';
4264 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4265 edit_print_string (edit, s);
4266 edit->force |= REDRAW_PAGE;
4267 break;
4269 break;
4270 case CK_Goto:
4271 edit_goto_cmd (edit);
4272 break;
4273 case CK_ParagraphFormat:
4274 format_paragraph (edit, 1);
4275 edit->force |= REDRAW_PAGE;
4276 break;
4277 case CK_MacroDelete:
4278 edit_delete_macro_cmd (edit);
4279 break;
4280 case CK_MatchBracket:
4281 edit_goto_matching_bracket (edit);
4282 break;
4283 case CK_UserMenu:
4284 user_menu (edit, NULL, -1);
4285 break;
4286 case CK_Sort:
4287 edit_sort_cmd (edit);
4288 break;
4289 case CK_ExternalCommand:
4290 edit_ext_cmd (edit);
4291 break;
4292 case CK_Mail:
4293 edit_mail_dialog (edit);
4294 break;
4295 case CK_Shell:
4296 view_other_cmd ();
4297 break;
4298 #ifdef HAVE_CHARSET
4299 case CK_SelectCodepage:
4300 edit_select_codepage_cmd (edit);
4301 break;
4302 #endif
4303 case CK_InsertLiteral:
4304 edit_insert_literal_cmd (edit);
4305 break;
4306 case CK_MacroStartStopRecord:
4307 edit_begin_end_macro_cmd (edit);
4308 break;
4309 case CK_RepeatStartStopRecord:
4310 edit_begin_end_repeat_cmd (edit);
4311 break;
4312 case CK_ExtendedKeyMap:
4313 edit->extmod = TRUE;
4314 break;
4315 default:
4316 break;
4319 /* CK_PipeBlock */
4320 if ((command / CK_PipeBlock (0)) == 1)
4321 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4323 /* keys which must set the col position, and the search vars */
4324 switch (command)
4326 case CK_Search:
4327 case CK_SearchContinue:
4328 case CK_Replace:
4329 case CK_ReplaceContinue:
4330 case CK_Complete:
4331 edit->prev_col = edit_get_col (edit);
4332 break;
4333 case CK_Up:
4334 case CK_MarkUp:
4335 case CK_MarkColumnUp:
4336 case CK_Down:
4337 case CK_MarkDown:
4338 case CK_MarkColumnDown:
4339 case CK_PageUp:
4340 case CK_MarkPageUp:
4341 case CK_MarkColumnPageUp:
4342 case CK_PageDown:
4343 case CK_MarkPageDown:
4344 case CK_MarkColumnPageDown:
4345 case CK_Top:
4346 case CK_MarkToFileBegin:
4347 case CK_Bottom:
4348 case CK_MarkToFileEnd:
4349 case CK_ParagraphUp:
4350 case CK_MarkParagraphUp:
4351 case CK_MarkColumnParagraphUp:
4352 case CK_ParagraphDown:
4353 case CK_MarkParagraphDown:
4354 case CK_MarkColumnParagraphDown:
4355 case CK_ScrollUp:
4356 case CK_MarkScrollUp:
4357 case CK_MarkColumnScrollUp:
4358 case CK_ScrollDown:
4359 case CK_MarkScrollDown:
4360 case CK_MarkColumnScrollDown:
4361 edit->search_start = edit->curs1;
4362 edit->found_len = 0;
4363 break;
4364 default:
4365 edit->found_len = 0;
4366 edit->prev_col = edit_get_col (edit);
4367 edit->search_start = edit->curs1;
4369 edit_find_bracket (edit);
4371 if (option_auto_para_formatting)
4373 switch (command)
4375 case CK_BackSpace:
4376 case CK_Delete:
4377 case CK_DeleteToWordBegin:
4378 case CK_DeleteToWordEnd:
4379 case CK_DeleteToHome:
4380 case CK_DeleteToEnd:
4381 format_paragraph (edit, 0);
4382 edit->force |= REDRAW_PAGE;
4387 /* --------------------------------------------------------------------------------------------- */
4389 void
4390 edit_stack_init (void)
4392 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4394 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4395 edit_history_moveto[edit_stack_iterator].line = -1;
4398 edit_stack_iterator = 0;
4401 /* --------------------------------------------------------------------------------------------- */
4403 void
4404 edit_stack_free (void)
4406 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4407 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4410 /* --------------------------------------------------------------------------------------------- */
4411 /** move i lines */
4413 void
4414 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4416 edit_move_updown (edit, i, do_scroll, TRUE);
4419 /* --------------------------------------------------------------------------------------------- */
4420 /** move i lines */
4422 void
4423 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4425 edit_move_updown (edit, i, do_scroll, FALSE);
4428 /* --------------------------------------------------------------------------------------------- */