Fixed creation of lock files
[midnight-commander.git] / src / editor / edit.c
blob7cec9720bac6efa889c5e5a9c3d246a4802aca61
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 /** goto beginning of line */
1044 static void
1045 edit_cursor_to_bol (WEdit * edit)
1047 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1048 edit->search_start = edit->curs1;
1049 edit->prev_col = edit_get_col (edit);
1050 edit->over_col = 0;
1053 /* --------------------------------------------------------------------------------------------- */
1054 /** goto end of line */
1056 static void
1057 edit_cursor_to_eol (WEdit * edit)
1059 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1060 edit->search_start = edit->curs1;
1061 edit->prev_col = edit_get_col (edit);
1062 edit->over_col = 0;
1065 /* --------------------------------------------------------------------------------------------- */
1067 static unsigned long
1068 my_type_of (int c)
1070 int x, r = 0;
1071 const char *p, *q;
1072 const char option_chars_move_whole_word[] =
1073 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1075 if (!c)
1076 return 0;
1077 if (c == '!')
1079 if (*option_chars_move_whole_word == '!')
1080 return 2;
1081 return 0x80000000UL;
1083 if (g_ascii_isupper ((gchar) c))
1084 c = 'A';
1085 else if (g_ascii_islower ((gchar) c))
1086 c = 'a';
1087 else if (g_ascii_isalpha (c))
1088 c = 'a';
1089 else if (isdigit (c))
1090 c = '0';
1091 else if (isspace (c))
1092 c = ' ';
1093 q = strchr (option_chars_move_whole_word, c);
1094 if (!q)
1095 return 0xFFFFFFFFUL;
1098 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1099 if (*p == '!')
1100 x <<= 1;
1101 r |= x;
1103 while ((q = strchr (q + 1, c)));
1104 return r;
1107 /* --------------------------------------------------------------------------------------------- */
1109 static void
1110 edit_left_word_move (WEdit * edit, int s)
1112 for (;;)
1114 int c1, c2;
1115 if (edit->column_highlight
1116 && edit->mark1 != edit->mark2
1117 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1118 break;
1119 edit_cursor_move (edit, -1);
1120 if (!edit->curs1)
1121 break;
1122 c1 = edit_get_byte (edit, edit->curs1 - 1);
1123 c2 = edit_get_byte (edit, edit->curs1);
1124 if (c1 == '\n' || c2 == '\n')
1125 break;
1126 if (!(my_type_of (c1) & my_type_of (c2)))
1127 break;
1128 if (isspace (c1) && !isspace (c2))
1129 break;
1130 if (s)
1131 if (!isspace (c1) && isspace (c2))
1132 break;
1136 /* --------------------------------------------------------------------------------------------- */
1138 static void
1139 edit_left_word_move_cmd (WEdit * edit)
1141 edit_left_word_move (edit, 0);
1142 edit->force |= REDRAW_PAGE;
1145 /* --------------------------------------------------------------------------------------------- */
1147 static void
1148 edit_right_word_move (WEdit * edit, int s)
1150 for (;;)
1152 int c1, c2;
1153 if (edit->column_highlight
1154 && edit->mark1 != edit->mark2
1155 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1156 break;
1157 edit_cursor_move (edit, 1);
1158 if (edit->curs1 >= edit->last_byte)
1159 break;
1160 c1 = edit_get_byte (edit, edit->curs1 - 1);
1161 c2 = edit_get_byte (edit, edit->curs1);
1162 if (c1 == '\n' || c2 == '\n')
1163 break;
1164 if (!(my_type_of (c1) & my_type_of (c2)))
1165 break;
1166 if (isspace (c1) && !isspace (c2))
1167 break;
1168 if (s)
1169 if (!isspace (c1) && isspace (c2))
1170 break;
1174 /* --------------------------------------------------------------------------------------------- */
1176 static void
1177 edit_right_word_move_cmd (WEdit * edit)
1179 edit_right_word_move (edit, 0);
1180 edit->force |= REDRAW_PAGE;
1183 /* --------------------------------------------------------------------------------------------- */
1185 static void
1186 edit_right_char_move_cmd (WEdit * edit)
1188 int cw = 1;
1189 int c = 0;
1190 if (edit->utf8)
1192 c = edit_get_utf (edit, edit->curs1, &cw);
1193 if (cw < 1)
1194 cw = 1;
1196 else
1198 c = edit_get_byte (edit, edit->curs1);
1200 if (option_cursor_beyond_eol && c == '\n')
1202 edit->over_col++;
1204 else
1206 edit_cursor_move (edit, cw);
1210 /* --------------------------------------------------------------------------------------------- */
1212 static void
1213 edit_left_char_move_cmd (WEdit * edit)
1215 int cw = 1;
1216 if (edit->column_highlight
1217 && option_cursor_beyond_eol
1218 && edit->mark1 != edit->mark2
1219 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1220 return;
1221 if (edit->utf8)
1223 edit_get_prev_utf (edit, edit->curs1, &cw);
1224 if (cw < 1)
1225 cw = 1;
1227 if (option_cursor_beyond_eol && edit->over_col > 0)
1229 edit->over_col--;
1231 else
1233 edit_cursor_move (edit, -cw);
1237 /* --------------------------------------------------------------------------------------------- */
1238 /** Up or down cursor moving.
1239 direction = TRUE - move up
1240 = FALSE - move down
1243 static void
1244 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1246 unsigned long p;
1247 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1249 if (i > l)
1250 i = l;
1252 if (i == 0)
1253 return;
1255 if (i > 1)
1256 edit->force |= REDRAW_PAGE;
1257 if (do_scroll)
1259 if (direction)
1260 edit_scroll_upward (edit, i);
1261 else
1262 edit_scroll_downward (edit, i);
1264 p = edit_bol (edit, edit->curs1);
1266 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1268 edit_cursor_move (edit, p - edit->curs1);
1270 edit_move_to_prev_col (edit, p);
1272 /* search start of current multibyte char (like CJK) */
1273 if (edit->curs1 + 1 < edit->last_byte)
1275 edit_right_char_move_cmd (edit);
1276 edit_left_char_move_cmd (edit);
1279 edit->search_start = edit->curs1;
1280 edit->found_len = 0;
1283 /* --------------------------------------------------------------------------------------------- */
1285 static void
1286 edit_right_delete_word (WEdit * edit)
1288 int c1, c2;
1289 for (;;)
1291 if (edit->curs1 >= edit->last_byte)
1292 break;
1293 c1 = edit_delete (edit, 1);
1294 c2 = edit_get_byte (edit, edit->curs1);
1295 if (c1 == '\n' || c2 == '\n')
1296 break;
1297 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1298 break;
1299 if (!(my_type_of (c1) & my_type_of (c2)))
1300 break;
1304 /* --------------------------------------------------------------------------------------------- */
1306 static void
1307 edit_left_delete_word (WEdit * edit)
1309 int c1, c2;
1310 for (;;)
1312 if (edit->curs1 <= 0)
1313 break;
1314 c1 = edit_backspace (edit, 1);
1315 c2 = edit_get_byte (edit, edit->curs1 - 1);
1316 if (c1 == '\n' || c2 == '\n')
1317 break;
1318 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1319 break;
1320 if (!(my_type_of (c1) & my_type_of (c2)))
1321 break;
1325 /* --------------------------------------------------------------------------------------------- */
1327 the start column position is not recorded, and hence does not
1328 undo as it happed. But who would notice.
1331 static void
1332 edit_do_undo (WEdit * edit)
1334 long ac;
1335 long count = 0;
1337 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1338 edit->over_col = 0;
1339 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1341 switch ((int) ac)
1343 case STACK_BOTTOM:
1344 goto done_undo;
1345 case CURS_RIGHT:
1346 edit_cursor_move (edit, 1);
1347 break;
1348 case CURS_LEFT:
1349 edit_cursor_move (edit, -1);
1350 break;
1351 case BACKSPACE:
1352 case BACKSPACE_BR:
1353 edit_backspace (edit, 1);
1354 break;
1355 case DELCHAR:
1356 case DELCHAR_BR:
1357 edit_delete (edit, 1);
1358 break;
1359 case COLUMN_ON:
1360 edit->column_highlight = 1;
1361 break;
1362 case COLUMN_OFF:
1363 edit->column_highlight = 0;
1364 break;
1366 if (ac >= 256 && ac < 512)
1367 edit_insert_ahead (edit, ac - 256);
1368 if (ac >= 0 && ac < 256)
1369 edit_insert (edit, ac);
1371 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1373 edit->mark1 = ac - MARK_1;
1374 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1376 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1378 edit->mark2 = ac - MARK_2;
1379 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1381 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1383 edit->end_mark_curs = ac - MARK_CURS;
1385 if (count++)
1386 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1389 if (edit->start_display > ac - KEY_PRESS)
1391 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1392 edit->force |= REDRAW_PAGE;
1394 else if (edit->start_display < ac - KEY_PRESS)
1396 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1397 edit->force |= REDRAW_PAGE;
1399 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1400 edit_update_curs_row (edit);
1402 done_undo:;
1403 edit->undo_stack_disable = 0;
1406 static void
1407 edit_do_redo (WEdit * edit)
1409 long ac;
1410 long count = 0;
1412 if (edit->redo_stack_reset)
1413 return;
1415 edit->over_col = 0;
1416 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1418 switch ((int) ac)
1420 case STACK_BOTTOM:
1421 goto done_redo;
1422 case CURS_RIGHT:
1423 edit_cursor_move (edit, 1);
1424 break;
1425 case CURS_LEFT:
1426 edit_cursor_move (edit, -1);
1427 break;
1428 case BACKSPACE:
1429 edit_backspace (edit, 1);
1430 break;
1431 case DELCHAR:
1432 edit_delete (edit, 1);
1433 break;
1434 case COLUMN_ON:
1435 edit->column_highlight = 1;
1436 break;
1437 case COLUMN_OFF:
1438 edit->column_highlight = 0;
1439 break;
1441 if (ac >= 256 && ac < 512)
1442 edit_insert_ahead (edit, ac - 256);
1443 if (ac >= 0 && ac < 256)
1444 edit_insert (edit, ac);
1446 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1448 edit->mark1 = ac - MARK_1;
1449 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1451 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1453 edit->mark2 = ac - MARK_2;
1454 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1456 /* more than one pop usually means something big */
1457 if (count++)
1458 edit->force |= REDRAW_PAGE;
1461 if (edit->start_display > ac - KEY_PRESS)
1463 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1464 edit->force |= REDRAW_PAGE;
1466 else if (edit->start_display < ac - KEY_PRESS)
1468 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1469 edit->force |= REDRAW_PAGE;
1471 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1472 edit_update_curs_row (edit);
1474 done_redo:;
1477 static void
1478 edit_group_undo (WEdit * edit)
1480 long ac = KEY_PRESS;
1481 long cur_ac = KEY_PRESS;
1482 while (ac != STACK_BOTTOM && ac == cur_ac)
1484 cur_ac = get_prev_undo_action (edit);
1485 edit_do_undo (edit);
1486 ac = get_prev_undo_action (edit);
1487 /* exit from cycle if option_group_undo is not set,
1488 * and make single UNDO operation
1490 if (!option_group_undo)
1491 ac = STACK_BOTTOM;
1495 /* --------------------------------------------------------------------------------------------- */
1497 static void
1498 edit_delete_to_line_end (WEdit * edit)
1500 while (edit_get_byte (edit, edit->curs1) != '\n')
1502 if (!edit->curs2)
1503 break;
1504 edit_delete (edit, 1);
1508 /* --------------------------------------------------------------------------------------------- */
1510 static void
1511 edit_delete_to_line_begin (WEdit * edit)
1513 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1515 if (!edit->curs1)
1516 break;
1517 edit_backspace (edit, 1);
1521 /* --------------------------------------------------------------------------------------------- */
1523 static int
1524 is_aligned_on_a_tab (WEdit * edit)
1526 edit_update_curs_col (edit);
1527 return !((edit->curs_col % (TAB_SIZE * space_width))
1528 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1531 /* --------------------------------------------------------------------------------------------- */
1533 static int
1534 right_of_four_spaces (WEdit * edit)
1536 int i, ch = 0;
1537 for (i = 1; i <= HALF_TAB_SIZE; i++)
1538 ch |= edit_get_byte (edit, edit->curs1 - i);
1539 if (ch == ' ')
1540 return is_aligned_on_a_tab (edit);
1541 return 0;
1544 /* --------------------------------------------------------------------------------------------- */
1546 static int
1547 left_of_four_spaces (WEdit * edit)
1549 int i, ch = 0;
1550 for (i = 0; i < HALF_TAB_SIZE; i++)
1551 ch |= edit_get_byte (edit, edit->curs1 + i);
1552 if (ch == ' ')
1553 return is_aligned_on_a_tab (edit);
1554 return 0;
1557 /* --------------------------------------------------------------------------------------------- */
1559 static void
1560 edit_auto_indent (WEdit * edit)
1562 long p;
1563 char c;
1564 p = edit->curs1;
1565 /* use the previous line as a template */
1566 p = edit_move_backward (edit, p, 1);
1567 /* copy the leading whitespace of the line */
1568 for (;;)
1569 { /* no range check - the line _is_ \n-terminated */
1570 c = edit_get_byte (edit, p++);
1571 if (c != ' ' && c != '\t')
1572 break;
1573 edit_insert (edit, c);
1577 /* --------------------------------------------------------------------------------------------- */
1579 static inline void
1580 edit_double_newline (WEdit * edit)
1582 edit_insert (edit, '\n');
1583 if (edit_get_byte (edit, edit->curs1) == '\n')
1584 return;
1585 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1586 return;
1587 edit->force |= REDRAW_PAGE;
1588 edit_insert (edit, '\n');
1592 /* --------------------------------------------------------------------------------------------- */
1594 static void
1595 insert_spaces_tab (WEdit * edit, gboolean half)
1597 int i;
1599 edit_update_curs_col (edit);
1600 i = option_tab_spacing * space_width;
1601 if (half)
1602 i /= 2;
1603 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1604 while (i > 0)
1606 edit_insert (edit, ' ');
1607 i -= space_width;
1611 /* --------------------------------------------------------------------------------------------- */
1613 static inline void
1614 edit_tab_cmd (WEdit * edit)
1616 int i;
1618 if (option_fake_half_tabs)
1620 if (is_in_indent (edit))
1622 /*insert a half tab (usually four spaces) unless there is a
1623 half tab already behind, then delete it and insert a
1624 full tab. */
1625 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1626 insert_spaces_tab (edit, TRUE);
1627 else
1629 for (i = 1; i <= HALF_TAB_SIZE; i++)
1630 edit_backspace (edit, 1);
1631 edit_insert (edit, '\t');
1633 return;
1636 if (option_fill_tabs_with_spaces)
1637 insert_spaces_tab (edit, FALSE);
1638 else
1639 edit_insert (edit, '\t');
1642 /* --------------------------------------------------------------------------------------------- */
1644 static void
1645 check_and_wrap_line (WEdit * edit)
1647 int curs, c;
1648 if (!option_typewriter_wrap)
1649 return;
1650 edit_update_curs_col (edit);
1651 if (edit->curs_col < option_word_wrap_line_length)
1652 return;
1653 curs = edit->curs1;
1654 for (;;)
1656 curs--;
1657 c = edit_get_byte (edit, curs);
1658 if (c == '\n' || curs <= 0)
1660 edit_insert (edit, '\n');
1661 return;
1663 if (c == ' ' || c == '\t')
1665 int current = edit->curs1;
1666 edit_cursor_move (edit, curs - edit->curs1 + 1);
1667 edit_insert (edit, '\n');
1668 edit_cursor_move (edit, current - edit->curs1 + 1);
1669 return;
1674 /* --------------------------------------------------------------------------------------------- */
1675 /** this find the matching bracket in either direction, and sets edit->bracket */
1677 static long
1678 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1680 const char *const b = "{}{[][()(", *p;
1681 int i = 1, a, inc = -1, c, d, n = 0;
1682 unsigned long j = 0;
1683 long q;
1684 edit_update_curs_row (edit);
1685 c = edit_get_byte (edit, edit->curs1);
1686 p = strchr (b, c);
1687 /* no limit */
1688 if (!furthest_bracket_search)
1689 furthest_bracket_search--;
1690 /* not on a bracket at all */
1691 if (!p)
1692 return -1;
1693 /* the matching bracket */
1694 d = p[1];
1695 /* going left or right? */
1696 if (strchr ("{[(", c))
1697 inc = 1;
1698 for (q = edit->curs1 + inc;; q += inc)
1700 /* out of buffer? */
1701 if (q >= edit->last_byte || q < 0)
1702 break;
1703 a = edit_get_byte (edit, q);
1704 /* don't want to eat CPU */
1705 if (j++ > furthest_bracket_search)
1706 break;
1707 /* out of screen? */
1708 if (in_screen)
1710 if (q < edit->start_display)
1711 break;
1712 /* count lines if searching downward */
1713 if (inc > 0 && a == '\n')
1714 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1715 break;
1717 /* count bracket depth */
1718 i += (a == c) - (a == d);
1719 /* return if bracket depth is zero */
1720 if (!i)
1721 return q;
1723 /* no match */
1724 return -1;
1727 /* --------------------------------------------------------------------------------------------- */
1729 static inline void
1730 edit_goto_matching_bracket (WEdit * edit)
1732 long q;
1734 q = edit_get_bracket (edit, 0, 0);
1735 if (q >= 0)
1737 edit->bracket = edit->curs1;
1738 edit->force |= REDRAW_PAGE;
1739 edit_cursor_move (edit, q - edit->curs1);
1743 /* --------------------------------------------------------------------------------------------- */
1745 static void
1746 edit_move_block_to_right (WEdit * edit)
1748 long start_mark, end_mark;
1749 long cur_bol, start_bol;
1751 if (eval_marks (edit, &start_mark, &end_mark))
1752 return;
1754 start_bol = edit_bol (edit, start_mark);
1755 cur_bol = edit_bol (edit, end_mark - 1);
1759 edit_cursor_move (edit, cur_bol - edit->curs1);
1760 if (option_fill_tabs_with_spaces)
1761 insert_spaces_tab (edit, option_fake_half_tabs);
1762 else
1763 edit_insert (edit, '\t');
1764 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1766 if (cur_bol == 0)
1767 break;
1769 cur_bol = edit_bol (edit, cur_bol - 1);
1771 while (cur_bol >= start_bol);
1773 edit->force |= REDRAW_PAGE;
1776 /* --------------------------------------------------------------------------------------------- */
1778 static void
1779 edit_move_block_to_left (WEdit * edit)
1781 long start_mark, end_mark;
1782 long cur_bol, start_bol;
1783 int i;
1785 if (eval_marks (edit, &start_mark, &end_mark))
1786 return;
1788 start_bol = edit_bol (edit, start_mark);
1789 cur_bol = edit_bol (edit, end_mark - 1);
1793 int del_tab_width;
1794 int next_char;
1796 edit_cursor_move (edit, cur_bol - edit->curs1);
1798 if (option_fake_half_tabs)
1799 del_tab_width = HALF_TAB_SIZE;
1800 else
1801 del_tab_width = option_tab_spacing;
1803 next_char = edit_get_byte (edit, edit->curs1);
1804 if (next_char == '\t')
1805 edit_delete (edit, 1);
1806 else if (next_char == ' ')
1807 for (i = 1; i <= del_tab_width; i++)
1809 if (next_char == ' ')
1810 edit_delete (edit, 1);
1811 next_char = edit_get_byte (edit, edit->curs1);
1814 if (cur_bol == 0)
1815 break;
1817 cur_bol = edit_bol (edit, cur_bol - 1);
1819 while (cur_bol >= start_bol);
1821 edit->force |= REDRAW_PAGE;
1824 /* --------------------------------------------------------------------------------------------- */
1826 * prints at the cursor
1827 * @returns the number of chars printed
1830 static size_t
1831 edit_print_string (WEdit * e, const char *s)
1833 size_t i = 0;
1835 while (s[i] != '\0')
1836 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1837 e->force |= REDRAW_COMPLETELY;
1838 edit_update_screen (e);
1839 return i;
1842 /* --------------------------------------------------------------------------------------------- */
1843 /*** public functions ****************************************************************************/
1844 /* --------------------------------------------------------------------------------------------- */
1846 /** User edit menu, like user menu (F2) but only in editor. */
1848 void
1849 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1851 char *block_file;
1852 int nomark;
1853 long curs;
1854 long start_mark, end_mark;
1855 struct stat status;
1856 vfs_path_t *block_file_vpath;
1858 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1859 block_file_vpath = vfs_path_from_str (block_file);
1860 curs = edit->curs1;
1861 nomark = eval_marks (edit, &start_mark, &end_mark);
1862 if (nomark == 0)
1863 edit_save_block (edit, block_file, start_mark, end_mark);
1865 /* run shell scripts from menu */
1866 if (user_menu_cmd (edit, menu_file, selected_entry)
1867 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1869 int rc = 0;
1870 FILE *fd;
1872 /* i.e. we have marked block */
1873 if (nomark == 0)
1874 rc = edit_block_delete_cmd (edit);
1876 if (rc == 0)
1878 long ins_len;
1880 ins_len = edit_insert_file (edit, block_file_vpath);
1881 if (nomark == 0 && ins_len > 0)
1882 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1884 /* truncate block file */
1885 fd = fopen (block_file, "w");
1886 if (fd != NULL)
1887 fclose (fd);
1889 edit_cursor_move (edit, curs - edit->curs1);
1890 edit_refresh_cmd (edit);
1891 edit->force |= REDRAW_COMPLETELY;
1893 g_free (block_file);
1894 vfs_path_free (block_file_vpath);
1897 /* --------------------------------------------------------------------------------------------- */
1900 edit_get_byte (WEdit * edit, long byte_index)
1902 unsigned long p;
1903 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1904 return '\n';
1906 if (byte_index >= edit->curs1)
1908 p = edit->curs1 + edit->curs2 - byte_index - 1;
1909 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1911 else
1913 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1917 /* --------------------------------------------------------------------------------------------- */
1920 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1922 gchar *str = NULL;
1923 int res = -1;
1924 gunichar ch;
1925 gchar *next_ch = NULL;
1926 int width = 0;
1927 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1929 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1931 *char_width = 0;
1932 return '\n';
1935 str = edit_get_byte_ptr (edit, byte_index);
1937 if (str == NULL)
1939 *char_width = 0;
1940 return 0;
1943 res = g_utf8_get_char_validated (str, -1);
1945 if (res < 0)
1947 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1948 int i;
1949 for (i = 0; i < UTF8_CHAR_LEN; i++)
1950 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1951 utf8_buf[UTF8_CHAR_LEN] = '\0';
1952 str = utf8_buf;
1953 res = g_utf8_get_char_validated (str, -1);
1956 if (res < 0)
1958 ch = *str;
1959 width = 0;
1961 else
1963 ch = res;
1964 /* Calculate UTF-8 char width */
1965 next_ch = g_utf8_next_char (str);
1966 if (next_ch)
1968 width = next_ch - str;
1970 else
1972 ch = 0;
1973 width = 0;
1976 *char_width = width;
1977 return ch;
1980 /* --------------------------------------------------------------------------------------------- */
1982 char *
1983 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1985 int i;
1986 char *p, *writename;
1987 const vfs_path_element_t *path_element;
1989 i = edit_find_filter (filename_vpath);
1990 if (i < 0)
1991 return NULL;
1993 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1994 writename = name_quote (path_element->path, 0);
1995 p = g_strdup_printf (all_filters[i].write, writename);
1996 g_free (writename);
1997 return p;
2000 /* --------------------------------------------------------------------------------------------- */
2002 long
2003 edit_write_stream (WEdit * edit, FILE * f)
2005 long i;
2007 if (edit->lb == LB_ASIS)
2009 for (i = 0; i < edit->last_byte; i++)
2010 if (fputc (edit_get_byte (edit, i), f) < 0)
2011 break;
2012 return i;
2015 /* change line breaks */
2016 for (i = 0; i < edit->last_byte; i++)
2018 unsigned char c = edit_get_byte (edit, i);
2020 if (!(c == '\n' || c == '\r'))
2022 /* not line break */
2023 if (fputc (c, f) < 0)
2024 return i;
2026 else
2027 { /* (c == '\n' || c == '\r') */
2028 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2030 switch (edit->lb)
2032 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2033 /* put one line break unconditionally */
2034 if (fputc ('\n', f) < 0)
2035 return i;
2037 i++; /* 2 chars are processed */
2039 if (c == '\r' && c1 == '\n')
2040 /* Windows line break; go to the next char */
2041 break;
2043 if (c == '\r' && c1 == '\r')
2045 /* two Macintosh line breaks; put second line break */
2046 if (fputc ('\n', f) < 0)
2047 return i;
2048 break;
2051 if (fputc (c1, f) < 0)
2052 return i;
2053 break;
2055 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2056 /* put one line break unconditionally */
2057 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2058 return i;
2060 if (c == '\r' && c1 == '\n')
2061 /* Windows line break; go to the next char */
2062 i++;
2063 break;
2065 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2066 /* put one line break unconditionally */
2067 if (fputc ('\r', f) < 0)
2068 return i;
2070 i++; /* 2 chars are processed */
2072 if (c == '\r' && c1 == '\n')
2073 /* Windows line break; go to the next char */
2074 break;
2076 if (c == '\n' && c1 == '\n')
2078 /* two Windows line breaks; put second line break */
2079 if (fputc ('\r', f) < 0)
2080 return i;
2081 break;
2084 if (fputc (c1, f) < 0)
2085 return i;
2086 break;
2087 case LB_ASIS: /* default without changes */
2088 break;
2093 return edit->last_byte;
2096 /* --------------------------------------------------------------------------------------------- */
2097 /** inserts a file at the cursor, returns count of inserted bytes on success */
2098 long
2099 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2101 char *p;
2102 long ins_len = 0;
2104 p = edit_get_filter (filename_vpath);
2105 if (p != NULL)
2107 FILE *f;
2108 long current = edit->curs1;
2110 f = (FILE *) popen (p, "r");
2111 if (f != NULL)
2113 edit_insert_stream (edit, f);
2114 ins_len = edit->curs1 - current;
2115 edit_cursor_move (edit, -ins_len);
2116 if (pclose (f) > 0)
2118 char *errmsg;
2120 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2121 edit_error_dialog (_("Error"), errmsg);
2122 g_free (errmsg);
2123 ins_len = -1;
2124 goto ret;
2127 else
2129 char *errmsg;
2131 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2132 edit_error_dialog (_("Error"), errmsg);
2133 g_free (errmsg);
2134 ins_len = -1;
2135 goto ret;
2137 g_free (p);
2139 else
2141 int i, file, blocklen;
2142 long current = edit->curs1;
2143 int vertical_insertion = 0;
2144 char *buf;
2146 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2147 if (file == -1)
2149 ins_len = -1;
2150 goto ret;
2153 buf = g_malloc0 (TEMP_BUF_LEN);
2154 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2155 if (blocklen > 0)
2157 /* if contain signature VERTICAL_MAGIC then it vertical block */
2158 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2159 vertical_insertion = 1;
2160 else
2161 mc_lseek (file, 0, SEEK_SET);
2164 if (vertical_insertion)
2166 long mark1, mark2;
2167 int c1, c2;
2169 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2170 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2171 /* highlight inserted text then not persistent blocks */
2172 if (!option_persistent_selections)
2174 if (!edit->column_highlight)
2175 edit_push_undo_action (edit, COLUMN_OFF);
2176 edit->column_highlight = 1;
2179 else
2181 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2183 for (i = 0; i < blocklen; i++)
2184 edit_insert (edit, buf[i]);
2186 /* highlight inserted text then not persistent blocks */
2187 if (!option_persistent_selections && edit->modified)
2189 edit_set_markers (edit, edit->curs1, current, 0, 0);
2190 if (edit->column_highlight)
2191 edit_push_undo_action (edit, COLUMN_ON);
2192 edit->column_highlight = 0;
2196 edit->force |= REDRAW_PAGE;
2197 ins_len = edit->curs1 - current;
2198 edit_cursor_move (edit, -ins_len);
2199 g_free (buf);
2200 mc_close (file);
2201 if (blocklen != 0)
2202 ins_len = 0;
2205 ret:
2206 g_free (p);
2207 return ins_len;
2210 /* --------------------------------------------------------------------------------------------- */
2212 * Fill in the edit structure. Return NULL on failure. Pass edit as
2213 * NULL to allocate a new structure.
2215 * If line is 0, try to restore saved position. Otherwise put the
2216 * cursor on that line and show it in the middle of the screen.
2219 WEdit *
2220 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2221 long line)
2223 gboolean to_free = FALSE;
2225 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2226 if (option_line_state)
2227 option_line_state_width = LINE_STATE_WIDTH;
2228 else
2229 option_line_state_width = 0;
2231 if (edit == NULL)
2233 #ifdef ENABLE_NLS
2235 * Expand option_whole_chars_search by national letters using
2236 * current locale
2239 static char option_whole_chars_search_buf[256];
2241 if (option_whole_chars_search_buf != option_whole_chars_search)
2243 size_t i;
2244 size_t len = str_term_width1 (option_whole_chars_search);
2246 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2248 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2250 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2252 option_whole_chars_search_buf[len++] = i;
2256 option_whole_chars_search_buf[len] = 0;
2257 option_whole_chars_search = option_whole_chars_search_buf;
2259 #endif /* ENABLE_NLS */
2260 edit = g_malloc0 (sizeof (WEdit));
2261 edit->search = NULL;
2262 to_free = TRUE;
2265 edit_purge_widget (edit);
2266 edit->widget.y = y;
2267 edit->widget.x = x;
2268 edit->widget.lines = lines;
2269 edit->widget.cols = cols;
2271 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2272 edit->stat1.st_uid = getuid ();
2273 edit->stat1.st_gid = getgid ();
2274 edit->stat1.st_mtime = 0;
2276 edit->over_col = 0;
2277 edit->bracket = -1;
2278 edit->force |= REDRAW_PAGE;
2279 edit_set_filename (edit, filename_vpath);
2281 edit->undo_stack_size = START_STACK_SIZE;
2282 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2283 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2285 edit->redo_stack_size = START_STACK_SIZE;
2286 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2287 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2289 edit->utf8 = 0;
2290 edit->converter = str_cnv_from_term;
2291 edit_set_codeset (edit);
2293 if (edit_load_file (edit))
2295 /* edit_load_file already gives an error message */
2296 if (to_free)
2297 g_free (edit);
2298 return NULL;
2301 edit->loading_done = 1;
2302 edit->modified = 0;
2303 edit->locked = 0;
2304 edit_load_syntax (edit, NULL, NULL);
2306 int color;
2307 edit_get_syntax_color (edit, -1, &color);
2310 /* load saved cursor position */
2311 if ((line == 0) && option_save_position)
2312 edit_load_position (edit);
2313 else
2315 if (line <= 0)
2316 line = 1;
2317 edit_move_display (edit, line - 1);
2318 edit_move_to_line (edit, line - 1);
2321 edit_load_macro_cmd (edit);
2322 return edit;
2325 /* --------------------------------------------------------------------------------------------- */
2326 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2329 edit_clean (WEdit * edit)
2331 int j = 0;
2333 if (!edit)
2334 return 0;
2336 /* a stale lock, remove it */
2337 if (edit->locked)
2338 edit->locked = unlock_file (edit->filename_vpath);
2340 /* save cursor position */
2341 if (option_save_position)
2342 edit_save_position (edit);
2343 else if (edit->serialized_bookmarks != NULL)
2344 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2346 /* File specified on the mcedit command line and never saved */
2347 if (edit->delete_file)
2348 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2350 edit_free_syntax_rules (edit);
2351 book_mark_flush (edit, -1);
2352 for (; j <= MAXBUFF; j++)
2354 g_free (edit->buffers1[j]);
2355 g_free (edit->buffers2[j]);
2358 g_free (edit->undo_stack);
2359 g_free (edit->redo_stack);
2360 vfs_path_free (edit->filename_vpath);
2361 vfs_path_free (edit->dir_vpath);
2362 mc_search_free (edit->search);
2363 edit->search = NULL;
2365 if (edit->converter != str_cnv_from_term)
2366 str_close_conv (edit->converter);
2368 edit_purge_widget (edit);
2370 return 1;
2373 /* --------------------------------------------------------------------------------------------- */
2374 /** returns 1 on success */
2377 edit_renew (WEdit * edit)
2379 int y = edit->widget.y;
2380 int x = edit->widget.x;
2381 int lines = edit->widget.lines;
2382 int columns = edit->widget.cols;
2384 edit_clean (edit);
2385 return (edit_init (edit, y, x, lines, columns, NULL, 0) != NULL);
2388 /* --------------------------------------------------------------------------------------------- */
2390 * Load a new file into the editor. If it fails, preserve the old file.
2391 * To do it, allocate a new widget, initialize it and, if the new file
2392 * was loaded, copy the data to the old widget.
2393 * Return 1 on success, 0 on failure.
2397 edit_reload (WEdit * edit, const vfs_path_t * filename_vpath)
2399 WEdit *e;
2400 int y = edit->widget.y;
2401 int x = edit->widget.x;
2402 int lines = edit->widget.lines;
2403 int columns = edit->widget.cols;
2405 e = g_malloc0 (sizeof (WEdit));
2406 e->widget = edit->widget;
2407 if (edit_init (e, y, x, lines, columns, filename_vpath, 0) == NULL)
2409 g_free (e);
2410 return 0;
2412 edit_clean (edit);
2413 memcpy (edit, e, sizeof (WEdit));
2414 g_free (e);
2415 return 1;
2418 /* --------------------------------------------------------------------------------------------- */
2420 * Load a new file into the editor and set line. If it fails, preserve the old file.
2421 * To do it, allocate a new widget, initialize it and, if the new file
2422 * was loaded, copy the data to the old widget.
2423 * Return 1 on success, 0 on failure.
2427 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2429 WEdit *e;
2430 int y = edit->widget.y;
2431 int x = edit->widget.x;
2432 int lines = edit->widget.lines;
2433 int columns = edit->widget.cols;
2435 e = g_malloc0 (sizeof (WEdit));
2436 e->widget = edit->widget;
2437 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2439 g_free (e);
2440 return 0;
2442 edit_clean (edit);
2443 memcpy (edit, e, sizeof (WEdit));
2444 g_free (e);
2445 return 1;
2448 /* --------------------------------------------------------------------------------------------- */
2450 void
2451 edit_set_codeset (WEdit * edit)
2453 #ifdef HAVE_CHARSET
2454 const char *cp_id;
2456 cp_id =
2457 get_codepage_id (mc_global.source_codepage >=
2458 0 ? mc_global.source_codepage : mc_global.display_codepage);
2460 if (cp_id != NULL)
2462 GIConv conv;
2463 conv = str_crt_conv_from (cp_id);
2464 if (conv != INVALID_CONV)
2466 if (edit->converter != str_cnv_from_term)
2467 str_close_conv (edit->converter);
2468 edit->converter = conv;
2472 if (cp_id != NULL)
2473 edit->utf8 = str_isutf8 (cp_id);
2474 #else
2475 (void) edit;
2476 #endif
2480 /* --------------------------------------------------------------------------------------------- */
2482 Recording stack for undo:
2483 The following is an implementation of a compressed stack. Identical
2484 pushes are recorded by a negative prefix indicating the number of times the
2485 same char was pushed. This saves space for repeated curs-left or curs-right
2486 delete etc.
2490 pushed: stored:
2494 b -3
2496 c --> -4
2502 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2503 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2504 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2505 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2506 position.
2508 The only way the cursor moves or the buffer is changed is through the routines:
2509 insert, backspace, insert_ahead, delete, and cursor_move.
2510 These record the reverse undo movements onto the stack each time they are
2511 called.
2513 Each key press results in a set of actions (insert; delete ...). So each time
2514 a key is pressed the current position of start_display is pushed as
2515 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2516 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2517 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2521 void
2522 edit_push_undo_action (WEdit * edit, long c, ...)
2524 unsigned long sp = edit->undo_stack_pointer;
2525 unsigned long spm1;
2526 long *t;
2528 /* first enlarge the stack if necessary */
2529 if (sp > edit->undo_stack_size - 10)
2530 { /* say */
2531 if (option_max_undo < 256)
2532 option_max_undo = 256;
2533 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2535 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2536 if (t)
2538 edit->undo_stack = t;
2539 edit->undo_stack_size <<= 1;
2540 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2544 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2545 if (edit->undo_stack_disable)
2547 edit_push_redo_action (edit, KEY_PRESS);
2548 edit_push_redo_action (edit, c);
2549 return;
2551 else if (edit->redo_stack_reset)
2553 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2556 if (edit->undo_stack_bottom != sp
2557 && spm1 != edit->undo_stack_bottom
2558 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2560 int d;
2561 if (edit->undo_stack[spm1] < 0)
2563 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2564 if (d == c)
2566 if (edit->undo_stack[spm1] > -1000000000)
2568 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2570 edit->undo_stack[spm1]--;
2572 return;
2576 else
2578 d = edit->undo_stack[spm1];
2579 if (d == c)
2581 if (c >= KEY_PRESS)
2582 return; /* --> no need to push multiple do-nothings */
2583 edit->undo_stack[sp] = -2;
2584 goto check_bottom;
2588 edit->undo_stack[sp] = c;
2590 check_bottom:
2591 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2593 /* if the sp wraps round and catches the undo_stack_bottom then erase
2594 * the first set of actions on the stack to make space - by moving
2595 * undo_stack_bottom forward one "key press" */
2596 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2597 if ((unsigned long) c == edit->undo_stack_bottom ||
2598 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2601 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2603 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2604 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2606 /*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: */
2607 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2608 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2610 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2614 void
2615 edit_push_redo_action (WEdit * edit, long c, ...)
2617 unsigned long sp = edit->redo_stack_pointer;
2618 unsigned long spm1;
2619 long *t;
2620 /* first enlarge the stack if necessary */
2621 if (sp > edit->redo_stack_size - 10)
2622 { /* say */
2623 if (option_max_undo < 256)
2624 option_max_undo = 256;
2625 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2627 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2628 if (t)
2630 edit->redo_stack = t;
2631 edit->redo_stack_size <<= 1;
2632 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2636 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2638 if (edit->redo_stack_bottom != sp
2639 && spm1 != edit->redo_stack_bottom
2640 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2642 int d;
2643 if (edit->redo_stack[spm1] < 0)
2645 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2646 if (d == c)
2648 if (edit->redo_stack[spm1] > -1000000000)
2650 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2651 edit->redo_stack[spm1]--;
2652 return;
2656 else
2658 d = edit->redo_stack[spm1];
2659 if (d == c)
2661 if (c >= KEY_PRESS)
2662 return; /* --> no need to push multiple do-nothings */
2663 edit->redo_stack[sp] = -2;
2664 goto redo_check_bottom;
2668 edit->redo_stack[sp] = c;
2670 redo_check_bottom:
2671 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2673 /* if the sp wraps round and catches the redo_stack_bottom then erase
2674 * the first set of actions on the stack to make space - by moving
2675 * redo_stack_bottom forward one "key press" */
2676 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2677 if ((unsigned long) c == edit->redo_stack_bottom ||
2678 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2681 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2683 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2684 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2687 * If a single key produced enough pushes to wrap all the way round then
2688 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2689 * The stack is then initialised:
2692 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2693 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2694 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2698 /* --------------------------------------------------------------------------------------------- */
2700 Basic low level single character buffer alterations and movements at the cursor.
2701 Returns char passed over, inserted or removed.
2704 void
2705 edit_insert (WEdit * edit, int c)
2707 /* check if file has grown to large */
2708 if (edit->last_byte >= SIZE_LIMIT)
2709 return;
2711 /* first we must update the position of the display window */
2712 if (edit->curs1 < edit->start_display)
2714 edit->start_display++;
2715 if (c == '\n')
2716 edit->start_line++;
2719 /* Mark file as modified, unless the file hasn't been fully loaded */
2720 if (edit->loading_done)
2722 edit_modification (edit);
2725 /* now we must update some info on the file and check if a redraw is required */
2726 if (c == '\n')
2728 if (edit->book_mark)
2729 book_mark_inc (edit, edit->curs_line);
2730 edit->curs_line++;
2731 edit->total_lines++;
2732 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2735 /* save the reverse command onto the undo stack */
2736 /* ordinary char and not space */
2737 if (c > 32)
2738 edit_push_undo_action (edit, BACKSPACE);
2739 else
2740 edit_push_undo_action (edit, BACKSPACE_BR);
2741 /* update markers */
2742 edit->mark1 += (edit->mark1 > edit->curs1);
2743 edit->mark2 += (edit->mark2 > edit->curs1);
2744 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2746 /* add a new buffer if we've reached the end of the last one */
2747 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2748 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2750 /* perform the insertion */
2751 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2752 = (unsigned char) c;
2754 /* update file length */
2755 edit->last_byte++;
2757 /* update cursor position */
2758 edit->curs1++;
2761 /* --------------------------------------------------------------------------------------------- */
2762 /** same as edit_insert and move left */
2764 void
2765 edit_insert_ahead (WEdit * edit, int c)
2767 if (edit->last_byte >= SIZE_LIMIT)
2768 return;
2770 if (edit->curs1 < edit->start_display)
2772 edit->start_display++;
2773 if (c == '\n')
2774 edit->start_line++;
2776 edit_modification (edit);
2777 if (c == '\n')
2779 if (edit->book_mark)
2780 book_mark_inc (edit, edit->curs_line);
2781 edit->total_lines++;
2782 edit->force |= REDRAW_AFTER_CURSOR;
2784 /* ordinary char and not space */
2785 if (c > 32)
2786 edit_push_undo_action (edit, DELCHAR);
2787 else
2788 edit_push_undo_action (edit, DELCHAR_BR);
2790 edit->mark1 += (edit->mark1 >= edit->curs1);
2791 edit->mark2 += (edit->mark2 >= edit->curs1);
2792 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2794 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2795 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2796 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2797 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2799 edit->last_byte++;
2800 edit->curs2++;
2804 /* --------------------------------------------------------------------------------------------- */
2807 edit_delete (WEdit * edit, const int byte_delete)
2809 int p = 0;
2810 int cw = 1;
2811 int i;
2813 if (!edit->curs2)
2814 return 0;
2816 cw = 1;
2817 /* if byte_delete = 1 then delete only one byte not multibyte char */
2818 if (edit->utf8 && byte_delete == 0)
2820 edit_get_utf (edit, edit->curs1, &cw);
2821 if (cw < 1)
2822 cw = 1;
2825 if (edit->mark2 != edit->mark1)
2826 edit_push_markers (edit);
2828 for (i = 1; i <= cw; i++)
2830 if (edit->mark1 > edit->curs1)
2832 edit->mark1--;
2833 edit->end_mark_curs--;
2835 if (edit->mark2 > edit->curs1)
2836 edit->mark2--;
2837 if (edit->last_get_rule > edit->curs1)
2838 edit->last_get_rule--;
2840 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2841 ((edit->curs2 -
2842 1) & M_EDIT_BUF_SIZE) - 1];
2844 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2846 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2847 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2849 edit->last_byte--;
2850 edit->curs2--;
2851 edit_push_undo_action (edit, p + 256);
2854 edit_modification (edit);
2855 if (p == '\n')
2857 if (edit->book_mark)
2858 book_mark_dec (edit, edit->curs_line);
2859 edit->total_lines--;
2860 edit->force |= REDRAW_AFTER_CURSOR;
2862 if (edit->curs1 < edit->start_display)
2864 edit->start_display--;
2865 if (p == '\n')
2866 edit->start_line--;
2869 return p;
2872 /* --------------------------------------------------------------------------------------------- */
2873 /** moves the cursor right or left: increment positive or negative respectively */
2875 void
2876 edit_cursor_move (WEdit * edit, long increment)
2878 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2879 int c;
2881 if (increment < 0)
2883 for (; increment < 0; increment++)
2885 if (!edit->curs1)
2886 return;
2888 edit_push_undo_action (edit, CURS_RIGHT);
2890 c = edit_get_byte (edit, edit->curs1 - 1);
2891 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2892 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2893 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2894 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2895 edit->curs2++;
2896 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2897 1) & M_EDIT_BUF_SIZE];
2898 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2900 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2901 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2903 edit->curs1--;
2904 if (c == '\n')
2906 edit->curs_line--;
2907 edit->force |= REDRAW_LINE_BELOW;
2912 else if (increment > 0)
2914 for (; increment > 0; increment--)
2916 if (!edit->curs2)
2917 return;
2919 edit_push_undo_action (edit, CURS_LEFT);
2921 c = edit_get_byte (edit, edit->curs1);
2922 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2923 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2924 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2925 edit->curs1++;
2926 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2927 ((edit->curs2 -
2928 1) & M_EDIT_BUF_SIZE) - 1];
2929 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2931 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2932 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2934 edit->curs2--;
2935 if (c == '\n')
2937 edit->curs_line++;
2938 edit->force |= REDRAW_LINE_ABOVE;
2944 /* These functions return positions relative to lines */
2946 /* --------------------------------------------------------------------------------------------- */
2947 /** returns index of last char on line + 1 */
2949 long
2950 edit_eol (WEdit * edit, long current)
2952 if (current >= edit->last_byte)
2953 return edit->last_byte;
2955 for (;; current++)
2956 if (edit_get_byte (edit, current) == '\n')
2957 break;
2958 return current;
2961 /* --------------------------------------------------------------------------------------------- */
2962 /** returns index of first char on line */
2964 long
2965 edit_bol (WEdit * edit, long current)
2967 if (current <= 0)
2968 return 0;
2970 for (;; current--)
2971 if (edit_get_byte (edit, current - 1) == '\n')
2972 break;
2973 return current;
2976 /* --------------------------------------------------------------------------------------------- */
2978 long
2979 edit_count_lines (WEdit * edit, long current, long upto)
2981 long lines = 0;
2982 if (upto > edit->last_byte)
2983 upto = edit->last_byte;
2984 if (current < 0)
2985 current = 0;
2986 while (current < upto)
2987 if (edit_get_byte (edit, current++) == '\n')
2988 lines++;
2989 return lines;
2992 /* --------------------------------------------------------------------------------------------- */
2993 /* If lines is zero this returns the count of lines from current to upto. */
2994 /* If upto is zero returns index of lines forward current. */
2996 long
2997 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2999 if (upto)
3001 return edit_count_lines (edit, current, upto);
3003 else
3005 long next;
3006 if (lines < 0)
3007 lines = 0;
3008 while (lines--)
3010 next = edit_eol (edit, current) + 1;
3011 if (next > edit->last_byte)
3012 break;
3013 else
3014 current = next;
3016 return current;
3020 /* --------------------------------------------------------------------------------------------- */
3021 /** Returns offset of 'lines' lines up from current */
3023 long
3024 edit_move_backward (WEdit * edit, long current, long lines)
3026 if (lines < 0)
3027 lines = 0;
3028 current = edit_bol (edit, current);
3029 while ((lines--) && current != 0)
3030 current = edit_bol (edit, current - 1);
3031 return current;
3034 /* --------------------------------------------------------------------------------------------- */
3035 /* If cols is zero this returns the count of columns from current to upto. */
3036 /* If upto is zero returns index of cols across from current. */
3038 long
3039 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3041 long p, q;
3042 int col;
3044 if (upto)
3046 q = upto;
3047 cols = -10;
3049 else
3050 q = edit->last_byte + 2;
3052 for (col = 0, p = current; p < q; p++)
3054 int c, orig_c;
3056 if (cols != -10)
3058 if (col == cols)
3059 return p;
3060 if (col > cols)
3061 return p - 1;
3064 orig_c = c = edit_get_byte (edit, p);
3066 #ifdef HAVE_CHARSET
3067 if (edit->utf8)
3069 int utf_ch;
3070 int cw = 1;
3072 utf_ch = edit_get_utf (edit, p, &cw);
3073 if (mc_global.utf8_display)
3075 if (cw > 1)
3076 col -= cw - 1;
3077 if (g_unichar_iswide (utf_ch))
3078 col++;
3080 else if (cw > 1 && g_unichar_isprint (utf_ch))
3081 col -= cw - 1;
3084 c = convert_to_display_c (c);
3085 #endif
3087 if (c == '\t')
3088 col += TAB_SIZE - col % TAB_SIZE;
3089 else if (c == '\n')
3091 if (upto)
3092 return col;
3093 else
3094 return p;
3096 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3097 /* '\r' is shown as ^M, so we must advance 2 characters */
3098 /* Caret notation for control characters */
3099 col += 2;
3100 else
3101 col++;
3103 return col;
3106 /* --------------------------------------------------------------------------------------------- */
3107 /** returns the current column position of the cursor */
3110 edit_get_col (WEdit * edit)
3112 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3115 /* --------------------------------------------------------------------------------------------- */
3116 /* Scrolling functions */
3117 /* --------------------------------------------------------------------------------------------- */
3119 void
3120 edit_update_curs_row (WEdit * edit)
3122 edit->curs_row = edit->curs_line - edit->start_line;
3125 /* --------------------------------------------------------------------------------------------- */
3127 void
3128 edit_update_curs_col (WEdit * edit)
3130 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3133 /* --------------------------------------------------------------------------------------------- */
3136 edit_get_curs_col (const WEdit * edit)
3138 return edit->curs_col;
3141 /* --------------------------------------------------------------------------------------------- */
3142 /** moves the display start position up by i lines */
3144 void
3145 edit_scroll_upward (WEdit * edit, unsigned long i)
3147 unsigned long lines_above = edit->start_line;
3148 if (i > lines_above)
3149 i = lines_above;
3150 if (i)
3152 edit->start_line -= i;
3153 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3154 edit->force |= REDRAW_PAGE;
3155 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3157 edit_update_curs_row (edit);
3161 /* --------------------------------------------------------------------------------------------- */
3162 /** returns 1 if could scroll, 0 otherwise */
3164 void
3165 edit_scroll_downward (WEdit * edit, int i)
3167 int lines_below;
3168 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3169 if (lines_below > 0)
3171 if (i > lines_below)
3172 i = lines_below;
3173 edit->start_line += i;
3174 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3175 edit->force |= REDRAW_PAGE;
3176 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3178 edit_update_curs_row (edit);
3181 /* --------------------------------------------------------------------------------------------- */
3183 void
3184 edit_scroll_right (WEdit * edit, int i)
3186 edit->force |= REDRAW_PAGE;
3187 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3188 edit->start_col -= i;
3191 /* --------------------------------------------------------------------------------------------- */
3193 void
3194 edit_scroll_left (WEdit * edit, int i)
3196 if (edit->start_col)
3198 edit->start_col += i;
3199 if (edit->start_col > 0)
3200 edit->start_col = 0;
3201 edit->force |= REDRAW_PAGE;
3202 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3206 /* --------------------------------------------------------------------------------------------- */
3207 /* high level cursor movement commands */
3208 /* --------------------------------------------------------------------------------------------- */
3210 void
3211 edit_move_to_prev_col (WEdit * edit, long p)
3213 int prev = edit->prev_col;
3214 int over = edit->over_col;
3215 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3217 if (option_cursor_beyond_eol)
3219 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3220 edit_eol (edit, edit->curs1));
3222 if (line_len < prev + edit->over_col)
3224 edit->over_col = prev + over - line_len;
3225 edit->prev_col = line_len;
3226 edit->curs_col = line_len;
3228 else
3230 edit->curs_col = prev + over;
3231 edit->prev_col = edit->curs_col;
3232 edit->over_col = 0;
3235 else
3237 edit->over_col = 0;
3238 if (is_in_indent (edit) && option_fake_half_tabs)
3240 edit_update_curs_col (edit);
3241 if (space_width)
3242 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3244 int q = edit->curs_col;
3245 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3246 p = edit_bol (edit, edit->curs1);
3247 edit_cursor_move (edit,
3248 edit_move_forward3 (edit, p, edit->curs_col,
3249 0) - edit->curs1);
3250 if (!left_of_four_spaces (edit))
3251 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3257 /* --------------------------------------------------------------------------------------------- */
3260 line_is_blank (WEdit * edit, long line)
3262 return is_blank (edit, edit_find_line (edit, line));
3265 /* --------------------------------------------------------------------------------------------- */
3266 /** move cursor to line 'line' */
3268 void
3269 edit_move_to_line (WEdit * e, long line)
3271 if (line < e->curs_line)
3272 edit_move_up (e, e->curs_line - line, 0);
3273 else
3274 edit_move_down (e, line - e->curs_line, 0);
3275 edit_scroll_screen_over_cursor (e);
3278 /* --------------------------------------------------------------------------------------------- */
3279 /** scroll window so that first visible line is 'line' */
3281 void
3282 edit_move_display (WEdit * e, long line)
3284 if (line < e->start_line)
3285 edit_scroll_upward (e, e->start_line - line);
3286 else
3287 edit_scroll_downward (e, line - e->start_line);
3290 /* --------------------------------------------------------------------------------------------- */
3291 /** save markers onto undo stack */
3293 void
3294 edit_push_markers (WEdit * edit)
3296 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3297 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3298 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3301 /* --------------------------------------------------------------------------------------------- */
3303 void
3304 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3306 edit->mark1 = m1;
3307 edit->mark2 = m2;
3308 edit->column1 = c1;
3309 edit->column2 = c2;
3313 /* --------------------------------------------------------------------------------------------- */
3314 /** highlight marker toggle */
3316 void
3317 edit_mark_cmd (WEdit * edit, int unmark)
3319 edit_push_markers (edit);
3320 if (unmark)
3322 edit_set_markers (edit, 0, 0, 0, 0);
3323 edit->force |= REDRAW_PAGE;
3325 else
3327 if (edit->mark2 >= 0)
3329 edit->end_mark_curs = -1;
3330 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3331 edit->curs_col + edit->over_col);
3332 edit->force |= REDRAW_PAGE;
3334 else
3336 edit->end_mark_curs = edit->curs1;
3337 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3338 edit->curs_col + edit->over_col);
3343 /* --------------------------------------------------------------------------------------------- */
3344 /** highlight the word under cursor */
3346 void
3347 edit_mark_current_word_cmd (WEdit * edit)
3349 long pos;
3351 for (pos = edit->curs1; pos != 0; pos--)
3353 int c1, c2;
3355 c1 = edit_get_byte (edit, pos);
3356 c2 = edit_get_byte (edit, pos - 1);
3357 if (!isspace (c1) && isspace (c2))
3358 break;
3359 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3360 break;
3362 edit->mark1 = pos;
3364 for (; pos < edit->last_byte; pos++)
3366 int c1, c2;
3368 c1 = edit_get_byte (edit, pos);
3369 c2 = edit_get_byte (edit, pos + 1);
3370 if (!isspace (c1) && isspace (c2))
3371 break;
3372 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3373 break;
3375 edit->mark2 = min (pos + 1, edit->last_byte);
3377 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3380 /* --------------------------------------------------------------------------------------------- */
3382 void
3383 edit_mark_current_line_cmd (WEdit * edit)
3385 long pos = edit->curs1;
3387 edit->mark1 = edit_bol (edit, pos);
3388 edit->mark2 = edit_eol (edit, pos);
3390 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3393 /* --------------------------------------------------------------------------------------------- */
3395 void
3396 edit_delete_line (WEdit * edit)
3399 * Delete right part of the line.
3400 * Note that edit_get_byte() returns '\n' when byte position is
3401 * beyond EOF.
3403 while (edit_get_byte (edit, edit->curs1) != '\n')
3405 (void) edit_delete (edit, 1);
3409 * Delete '\n' char.
3410 * Note that edit_delete() will not corrupt anything if called while
3411 * cursor position is EOF.
3413 (void) edit_delete (edit, 1);
3416 * Delete left part of the line.
3417 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3419 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3421 (void) edit_backspace (edit, 1);
3425 /* --------------------------------------------------------------------------------------------- */
3428 edit_indent_width (WEdit * edit, long p)
3430 long q = p;
3431 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3432 q++;
3433 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3436 /* --------------------------------------------------------------------------------------------- */
3438 void
3439 edit_insert_indent (WEdit * edit, int indent)
3441 if (!option_fill_tabs_with_spaces)
3443 while (indent >= TAB_SIZE)
3445 edit_insert (edit, '\t');
3446 indent -= TAB_SIZE;
3449 while (indent-- > 0)
3450 edit_insert (edit, ' ');
3453 /* --------------------------------------------------------------------------------------------- */
3455 void
3456 edit_push_key_press (WEdit * edit)
3458 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3459 if (edit->mark2 == -1)
3461 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3462 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3466 /* --------------------------------------------------------------------------------------------- */
3468 void
3469 edit_find_bracket (WEdit * edit)
3471 edit->bracket = edit_get_bracket (edit, 1, 10000);
3472 if (last_bracket != edit->bracket)
3473 edit->force |= REDRAW_PAGE;
3474 last_bracket = edit->bracket;
3477 /* --------------------------------------------------------------------------------------------- */
3479 * This executes a command as though the user initiated it through a key
3480 * press. Callback with WIDGET_KEY as a message calls this after
3481 * translating the key press. This function can be used to pass any
3482 * command to the editor. Note that the screen wouldn't update
3483 * automatically. Either of command or char_for_insertion must be
3484 * passed as -1. Commands are executed, and char_for_insertion is
3485 * inserted at the cursor.
3488 void
3489 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3491 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3492 || (macro_index < 0
3493 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3495 macro_index = 0;
3496 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3497 return;
3499 if (macro_index != -1)
3501 edit->force |= REDRAW_COMPLETELY;
3502 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3504 edit_store_macro_cmd (edit);
3505 macro_index = -1;
3506 return;
3508 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3510 edit_repeat_macro_cmd (edit);
3511 macro_index = -1;
3512 return;
3516 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3518 record_macro_buf[macro_index].action = command;
3519 record_macro_buf[macro_index++].ch = char_for_insertion;
3521 /* record the beginning of a set of editing actions initiated by a key press */
3522 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3523 edit_push_key_press (edit);
3525 edit_execute_cmd (edit, command, char_for_insertion);
3526 if (edit->column_highlight)
3527 edit->force |= REDRAW_PAGE;
3530 /* --------------------------------------------------------------------------------------------- */
3532 This executes a command at a lower level than macro recording.
3533 It also does not push a key_press onto the undo stack. This means
3534 that if it is called many times, a single undo command will undo
3535 all of them. It also does not check for the Undo command.
3537 void
3538 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3540 edit->force |= REDRAW_LINE;
3542 /* The next key press will unhighlight the found string, so update
3543 * the whole page */
3544 if (edit->found_len || edit->column_highlight)
3545 edit->force |= REDRAW_PAGE;
3547 switch (command)
3549 /* a mark command with shift-arrow */
3550 case CK_MarkLeft:
3551 case CK_MarkRight:
3552 case CK_MarkToWordBegin:
3553 case CK_MarkToWordEnd:
3554 case CK_MarkToHome:
3555 case CK_MarkToEnd:
3556 case CK_MarkUp:
3557 case CK_MarkDown:
3558 case CK_MarkPageUp:
3559 case CK_MarkPageDown:
3560 case CK_MarkToFileBegin:
3561 case CK_MarkToFileEnd:
3562 case CK_MarkToPageBegin:
3563 case CK_MarkToPageEnd:
3564 case CK_MarkScrollUp:
3565 case CK_MarkScrollDown:
3566 case CK_MarkParagraphUp:
3567 case CK_MarkParagraphDown:
3568 /* a mark command with alt-arrow */
3569 case CK_MarkColumnPageUp:
3570 case CK_MarkColumnPageDown:
3571 case CK_MarkColumnLeft:
3572 case CK_MarkColumnRight:
3573 case CK_MarkColumnUp:
3574 case CK_MarkColumnDown:
3575 case CK_MarkColumnScrollUp:
3576 case CK_MarkColumnScrollDown:
3577 case CK_MarkColumnParagraphUp:
3578 case CK_MarkColumnParagraphDown:
3579 edit->column_highlight = 0;
3580 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3582 edit_mark_cmd (edit, 1); /* clear */
3583 edit_mark_cmd (edit, 0); /* marking on */
3585 edit->highlight = 1;
3586 break;
3588 /* any other command */
3589 default:
3590 if (edit->highlight)
3591 edit_mark_cmd (edit, 0); /* clear */
3592 edit->highlight = 0;
3595 /* first check for undo */
3596 if (command == CK_Undo)
3598 edit->redo_stack_reset = 0;
3599 edit_group_undo (edit);
3600 edit->found_len = 0;
3601 edit->prev_col = edit_get_col (edit);
3602 edit->search_start = edit->curs1;
3603 return;
3605 /* check for redo */
3606 if (command == CK_Redo)
3608 edit->redo_stack_reset = 0;
3609 edit_do_redo (edit);
3610 edit->found_len = 0;
3611 edit->prev_col = edit_get_col (edit);
3612 edit->search_start = edit->curs1;
3613 return;
3616 edit->redo_stack_reset = 1;
3618 /* An ordinary key press */
3619 if (char_for_insertion >= 0)
3621 /* if non persistent selection and text selected */
3622 if (!option_persistent_selections)
3624 if (edit->mark1 != edit->mark2)
3625 edit_block_delete_cmd (edit);
3627 if (edit->overwrite)
3629 /* remove char only one time, after input first byte, multibyte chars */
3630 if ((!mc_global.utf8_display || edit->charpoint == 0)
3631 && edit_get_byte (edit, edit->curs1) != '\n')
3632 edit_delete (edit, 0);
3634 if (option_cursor_beyond_eol && edit->over_col > 0)
3635 edit_insert_over (edit);
3636 #ifdef HAVE_CHARSET
3637 if (char_for_insertion > 255 && mc_global.utf8_display == 0)
3639 unsigned char str[6 + 1];
3640 size_t i = 0;
3641 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3642 if (res == 0)
3644 str[0] = '.';
3645 str[1] = '\0';
3647 else
3649 str[res] = '\0';
3651 while (str[i] != 0 && i <= 6)
3653 char_for_insertion = str[i];
3654 edit_insert (edit, char_for_insertion);
3655 i++;
3658 else
3659 #endif
3660 edit_insert (edit, char_for_insertion);
3662 if (option_auto_para_formatting)
3664 format_paragraph (edit, 0);
3665 edit->force |= REDRAW_PAGE;
3667 else
3668 check_and_wrap_line (edit);
3669 edit->found_len = 0;
3670 edit->prev_col = edit_get_col (edit);
3671 edit->search_start = edit->curs1;
3672 edit_find_bracket (edit);
3673 return;
3676 switch (command)
3678 case CK_TopOnScreen:
3679 case CK_BottomOnScreen:
3680 case CK_Top:
3681 case CK_Bottom:
3682 case CK_PageUp:
3683 case CK_PageDown:
3684 case CK_Home:
3685 case CK_End:
3686 case CK_Up:
3687 case CK_Down:
3688 case CK_Left:
3689 case CK_Right:
3690 case CK_WordLeft:
3691 case CK_WordRight:
3692 if (edit->mark2 >= 0)
3694 if (!option_persistent_selections)
3696 if (edit->column_highlight)
3697 edit_push_undo_action (edit, COLUMN_ON);
3698 edit->column_highlight = 0;
3699 edit_mark_cmd (edit, 1);
3704 switch (command)
3706 case CK_TopOnScreen:
3707 case CK_BottomOnScreen:
3708 case CK_MarkToPageBegin:
3709 case CK_MarkToPageEnd:
3710 case CK_Up:
3711 case CK_Down:
3712 case CK_WordLeft:
3713 case CK_WordRight:
3714 case CK_MarkToWordBegin:
3715 case CK_MarkToWordEnd:
3716 case CK_MarkUp:
3717 case CK_MarkDown:
3718 case CK_MarkColumnUp:
3719 case CK_MarkColumnDown:
3720 if (edit->mark2 == -1)
3721 break; /*marking is following the cursor: may need to highlight a whole line */
3722 case CK_Left:
3723 case CK_Right:
3724 case CK_MarkLeft:
3725 case CK_MarkRight:
3726 edit->force |= REDRAW_CHAR_ONLY;
3729 /* basic cursor key commands */
3730 switch (command)
3732 case CK_BackSpace:
3733 /* if non persistent selection and text selected */
3734 if (!option_persistent_selections)
3736 if (edit->mark1 != edit->mark2)
3738 edit_block_delete_cmd (edit);
3739 break;
3742 if (option_cursor_beyond_eol && edit->over_col > 0)
3744 edit->over_col--;
3745 break;
3747 if (option_backspace_through_tabs && is_in_indent (edit))
3749 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3750 edit_backspace (edit, 1);
3751 break;
3753 else
3755 if (option_fake_half_tabs)
3757 int i;
3758 if (is_in_indent (edit) && right_of_four_spaces (edit))
3760 for (i = 0; i < HALF_TAB_SIZE; i++)
3761 edit_backspace (edit, 1);
3762 break;
3766 edit_backspace (edit, 0);
3767 break;
3768 case CK_Delete:
3769 /* if non persistent selection and text selected */
3770 if (!option_persistent_selections)
3772 if (edit->mark1 != edit->mark2)
3774 edit_block_delete_cmd (edit);
3775 break;
3779 if (option_cursor_beyond_eol && edit->over_col > 0)
3780 edit_insert_over (edit);
3782 if (option_fake_half_tabs)
3784 int i;
3785 if (is_in_indent (edit) && left_of_four_spaces (edit))
3787 for (i = 1; i <= HALF_TAB_SIZE; i++)
3788 edit_delete (edit, 1);
3789 break;
3792 edit_delete (edit, 0);
3793 break;
3794 case CK_DeleteToWordBegin:
3795 edit->over_col = 0;
3796 edit_left_delete_word (edit);
3797 break;
3798 case CK_DeleteToWordEnd:
3799 if (option_cursor_beyond_eol && edit->over_col > 0)
3800 edit_insert_over (edit);
3802 edit_right_delete_word (edit);
3803 break;
3804 case CK_DeleteLine:
3805 edit_delete_line (edit);
3806 break;
3807 case CK_DeleteToHome:
3808 edit_delete_to_line_begin (edit);
3809 break;
3810 case CK_DeleteToEnd:
3811 edit_delete_to_line_end (edit);
3812 break;
3813 case CK_Enter:
3814 edit->over_col = 0;
3815 if (option_auto_para_formatting)
3817 edit_double_newline (edit);
3818 if (option_return_does_auto_indent)
3819 edit_auto_indent (edit);
3820 format_paragraph (edit, 0);
3822 else
3824 edit_insert (edit, '\n');
3825 if (option_return_does_auto_indent)
3827 edit_auto_indent (edit);
3830 break;
3831 case CK_Return:
3832 edit_insert (edit, '\n');
3833 break;
3835 case CK_MarkColumnPageUp:
3836 edit->column_highlight = 1;
3837 case CK_PageUp:
3838 case CK_MarkPageUp:
3839 edit_move_up (edit, edit->widget.lines - 1, 1);
3840 break;
3841 case CK_MarkColumnPageDown:
3842 edit->column_highlight = 1;
3843 case CK_PageDown:
3844 case CK_MarkPageDown:
3845 edit_move_down (edit, edit->widget.lines - 1, 1);
3846 break;
3847 case CK_MarkColumnLeft:
3848 edit->column_highlight = 1;
3849 case CK_Left:
3850 case CK_MarkLeft:
3851 if (option_fake_half_tabs)
3853 if (is_in_indent (edit) && right_of_four_spaces (edit))
3855 if (option_cursor_beyond_eol && edit->over_col > 0)
3856 edit->over_col--;
3857 else
3858 edit_cursor_move (edit, -HALF_TAB_SIZE);
3859 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3860 break;
3863 edit_left_char_move_cmd (edit);
3864 break;
3865 case CK_MarkColumnRight:
3866 edit->column_highlight = 1;
3867 case CK_Right:
3868 case CK_MarkRight:
3869 if (option_fake_half_tabs)
3871 if (is_in_indent (edit) && left_of_four_spaces (edit))
3873 edit_cursor_move (edit, HALF_TAB_SIZE);
3874 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3875 break;
3878 edit_right_char_move_cmd (edit);
3879 break;
3880 case CK_TopOnScreen:
3881 case CK_MarkToPageBegin:
3882 edit_begin_page (edit);
3883 break;
3884 case CK_BottomOnScreen:
3885 case CK_MarkToPageEnd:
3886 edit_end_page (edit);
3887 break;
3888 case CK_WordLeft:
3889 case CK_MarkToWordBegin:
3890 edit->over_col = 0;
3891 edit_left_word_move_cmd (edit);
3892 break;
3893 case CK_WordRight:
3894 case CK_MarkToWordEnd:
3895 edit->over_col = 0;
3896 edit_right_word_move_cmd (edit);
3897 break;
3898 case CK_MarkColumnUp:
3899 edit->column_highlight = 1;
3900 case CK_Up:
3901 case CK_MarkUp:
3902 edit_move_up (edit, 1, 0);
3903 break;
3904 case CK_MarkColumnDown:
3905 edit->column_highlight = 1;
3906 case CK_Down:
3907 case CK_MarkDown:
3908 edit_move_down (edit, 1, 0);
3909 break;
3910 case CK_MarkColumnParagraphUp:
3911 edit->column_highlight = 1;
3912 case CK_ParagraphUp:
3913 case CK_MarkParagraphUp:
3914 edit_move_up_paragraph (edit, 0);
3915 break;
3916 case CK_MarkColumnParagraphDown:
3917 edit->column_highlight = 1;
3918 case CK_ParagraphDown:
3919 case CK_MarkParagraphDown:
3920 edit_move_down_paragraph (edit, 0);
3921 break;
3922 case CK_MarkColumnScrollUp:
3923 edit->column_highlight = 1;
3924 case CK_ScrollUp:
3925 case CK_MarkScrollUp:
3926 edit_move_up (edit, 1, 1);
3927 break;
3928 case CK_MarkColumnScrollDown:
3929 edit->column_highlight = 1;
3930 case CK_ScrollDown:
3931 case CK_MarkScrollDown:
3932 edit_move_down (edit, 1, 1);
3933 break;
3934 case CK_Home:
3935 case CK_MarkToHome:
3936 edit_cursor_to_bol (edit);
3937 break;
3938 case CK_End:
3939 case CK_MarkToEnd:
3940 edit_cursor_to_eol (edit);
3941 break;
3942 case CK_Tab:
3943 /* if text marked shift block */
3944 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3946 if (edit->mark2 < 0)
3947 edit_mark_cmd (edit, 0);
3948 edit_move_block_to_right (edit);
3950 else
3952 if (option_cursor_beyond_eol)
3953 edit_insert_over (edit);
3954 edit_tab_cmd (edit);
3955 if (option_auto_para_formatting)
3957 format_paragraph (edit, 0);
3958 edit->force |= REDRAW_PAGE;
3960 else
3962 check_and_wrap_line (edit);
3965 break;
3967 case CK_InsertOverwrite:
3968 edit->overwrite = !edit->overwrite;
3969 break;
3971 case CK_Mark:
3972 if (edit->mark2 >= 0)
3974 if (edit->column_highlight)
3975 edit_push_undo_action (edit, COLUMN_ON);
3976 edit->column_highlight = 0;
3978 edit_mark_cmd (edit, 0);
3979 break;
3980 case CK_MarkColumn:
3981 if (!edit->column_highlight)
3982 edit_push_undo_action (edit, COLUMN_OFF);
3983 edit->column_highlight = 1;
3984 edit_mark_cmd (edit, 0);
3985 break;
3986 case CK_MarkAll:
3987 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3988 edit->force |= REDRAW_PAGE;
3989 break;
3990 case CK_Unmark:
3991 if (edit->column_highlight)
3992 edit_push_undo_action (edit, COLUMN_ON);
3993 edit->column_highlight = 0;
3994 edit_mark_cmd (edit, 1);
3995 break;
3996 case CK_MarkWord:
3997 if (edit->column_highlight)
3998 edit_push_undo_action (edit, COLUMN_ON);
3999 edit->column_highlight = 0;
4000 edit_mark_current_word_cmd (edit);
4001 break;
4002 case CK_MarkLine:
4003 if (edit->column_highlight)
4004 edit_push_undo_action (edit, COLUMN_ON);
4005 edit->column_highlight = 0;
4006 edit_mark_current_line_cmd (edit);
4007 break;
4009 case CK_ShowNumbers:
4010 option_line_state = !option_line_state;
4011 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
4012 edit->force |= REDRAW_PAGE;
4013 break;
4015 case CK_ShowMargin:
4016 show_right_margin = !show_right_margin;
4017 edit->force |= REDRAW_PAGE;
4018 break;
4020 case CK_Bookmark:
4021 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4022 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4023 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4024 else
4025 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4026 break;
4027 case CK_BookmarkFlush:
4028 book_mark_flush (edit, BOOK_MARK_COLOR);
4029 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4030 edit->force |= REDRAW_PAGE;
4031 break;
4032 case CK_BookmarkNext:
4033 if (edit->book_mark)
4035 struct _book_mark *p;
4036 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4037 if (p->next)
4039 p = p->next;
4040 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4041 edit_move_display (edit, p->line - edit->widget.lines / 2);
4042 edit_move_to_line (edit, p->line);
4045 break;
4046 case CK_BookmarkPrev:
4047 if (edit->book_mark)
4049 struct _book_mark *p;
4050 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4051 while (p->line == edit->curs_line)
4052 if (p->prev)
4053 p = p->prev;
4054 if (p->line >= 0)
4056 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4057 edit_move_display (edit, p->line - edit->widget.lines / 2);
4058 edit_move_to_line (edit, p->line);
4061 break;
4063 case CK_Top:
4064 case CK_MarkToFileBegin:
4065 edit_move_to_top (edit);
4066 break;
4067 case CK_Bottom:
4068 case CK_MarkToFileEnd:
4069 edit_move_to_bottom (edit);
4070 break;
4072 case CK_Copy:
4073 if (option_cursor_beyond_eol && edit->over_col > 0)
4074 edit_insert_over (edit);
4075 edit_block_copy_cmd (edit);
4076 break;
4077 case CK_Remove:
4078 edit_block_delete_cmd (edit);
4079 break;
4080 case CK_Move:
4081 edit_block_move_cmd (edit);
4082 break;
4084 case CK_BlockShiftLeft:
4085 if (edit->mark1 != edit->mark2)
4086 edit_move_block_to_left (edit);
4087 break;
4088 case CK_BlockShiftRight:
4089 if (edit->mark1 != edit->mark2)
4090 edit_move_block_to_right (edit);
4091 break;
4092 case CK_Store:
4093 edit_copy_to_X_buf_cmd (edit);
4094 break;
4095 case CK_Cut:
4096 edit_cut_to_X_buf_cmd (edit);
4097 break;
4098 case CK_Paste:
4099 /* if non persistent selection and text selected */
4100 if (!option_persistent_selections)
4102 if (edit->mark1 != edit->mark2)
4103 edit_block_delete_cmd (edit);
4105 if (option_cursor_beyond_eol && edit->over_col > 0)
4106 edit_insert_over (edit);
4107 edit_paste_from_X_buf_cmd (edit);
4108 break;
4109 case CK_History:
4110 edit_paste_from_history (edit);
4111 break;
4113 case CK_SaveAs:
4114 edit_save_as_cmd (edit);
4115 break;
4116 case CK_Save:
4117 edit_save_confirm_cmd (edit);
4118 break;
4119 case CK_EditFile:
4120 edit_load_cmd (edit, EDIT_FILE_COMMON);
4121 break;
4122 case CK_BlockSave:
4123 edit_save_block_cmd (edit);
4124 break;
4125 case CK_InsertFile:
4126 edit_insert_file_cmd (edit);
4127 break;
4129 case CK_FilePrev:
4130 edit_load_back_cmd (edit);
4131 break;
4132 case CK_FileNext:
4133 edit_load_forward_cmd (edit);
4134 break;
4136 case CK_EditSyntaxFile:
4137 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4138 break;
4139 case CK_SyntaxChoose:
4140 edit_syntax_dialog (edit);
4141 break;
4143 case CK_EditUserMenu:
4144 edit_load_cmd (edit, EDIT_FILE_MENU);
4145 break;
4147 case CK_SyntaxOnOff:
4148 option_syntax_highlighting ^= 1;
4149 if (option_syntax_highlighting == 1)
4150 edit_load_syntax (edit, NULL, edit->syntax_type);
4151 edit->force |= REDRAW_PAGE;
4152 break;
4154 case CK_ShowTabTws:
4155 enable_show_tabs_tws ^= 1;
4156 edit->force |= REDRAW_PAGE;
4157 break;
4159 case CK_Search:
4160 edit_search_cmd (edit, FALSE);
4161 break;
4162 case CK_SearchContinue:
4163 edit_search_cmd (edit, TRUE);
4164 break;
4165 case CK_Replace:
4166 edit_replace_cmd (edit, 0);
4167 break;
4168 case CK_ReplaceContinue:
4169 edit_replace_cmd (edit, 1);
4170 break;
4171 case CK_Complete:
4172 /* if text marked shift block */
4173 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4175 edit_move_block_to_left (edit);
4177 else
4179 edit_complete_word_cmd (edit);
4181 break;
4182 case CK_Find:
4183 edit_get_match_keyword_cmd (edit);
4184 break;
4185 case CK_Quit:
4186 dlg_stop (edit->widget.owner);
4187 break;
4188 case CK_EditNew:
4189 edit_new_cmd (edit);
4190 break;
4191 case CK_Help:
4192 edit_help_cmd (edit);
4193 break;
4194 case CK_Refresh:
4195 edit_refresh_cmd (edit);
4196 break;
4197 case CK_SaveSetup:
4198 save_setup_cmd ();
4199 break;
4200 case CK_About:
4201 edit_about ();
4202 break;
4203 case CK_LearnKeys:
4204 learn_keys ();
4205 break;
4206 case CK_Options:
4207 edit_options_dialog (edit);
4208 break;
4209 case CK_OptionsSaveMode:
4210 menu_save_mode_cmd ();
4211 break;
4212 case CK_Date:
4214 char s[BUF_MEDIUM];
4215 /* fool gcc to prevent a Y2K warning */
4216 char time_format[] = "_c";
4217 time_format[0] = '%';
4219 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4220 edit_print_string (edit, s);
4221 edit->force |= REDRAW_PAGE;
4222 break;
4224 break;
4225 case CK_Goto:
4226 edit_goto_cmd (edit);
4227 break;
4228 case CK_ParagraphFormat:
4229 format_paragraph (edit, 1);
4230 edit->force |= REDRAW_PAGE;
4231 break;
4232 case CK_MacroDelete:
4233 edit_delete_macro_cmd (edit);
4234 break;
4235 case CK_MatchBracket:
4236 edit_goto_matching_bracket (edit);
4237 break;
4238 case CK_UserMenu:
4239 user_menu (edit, NULL, -1);
4240 break;
4241 case CK_Sort:
4242 edit_sort_cmd (edit);
4243 break;
4244 case CK_ExternalCommand:
4245 edit_ext_cmd (edit);
4246 break;
4247 case CK_Mail:
4248 edit_mail_dialog (edit);
4249 break;
4250 case CK_Shell:
4251 view_other_cmd ();
4252 break;
4253 #ifdef HAVE_CHARSET
4254 case CK_SelectCodepage:
4255 edit_select_codepage_cmd (edit);
4256 break;
4257 #endif
4258 case CK_InsertLiteral:
4259 edit_insert_literal_cmd (edit);
4260 break;
4261 case CK_MacroStartStopRecord:
4262 edit_begin_end_macro_cmd (edit);
4263 break;
4264 case CK_RepeatStartStopRecord:
4265 edit_begin_end_repeat_cmd (edit);
4266 break;
4267 case CK_ExtendedKeyMap:
4268 edit->extmod = TRUE;
4269 break;
4270 default:
4271 break;
4274 /* CK_PipeBlock */
4275 if ((command / CK_PipeBlock (0)) == 1)
4276 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4278 /* keys which must set the col position, and the search vars */
4279 switch (command)
4281 case CK_Search:
4282 case CK_SearchContinue:
4283 case CK_Replace:
4284 case CK_ReplaceContinue:
4285 case CK_Complete:
4286 edit->prev_col = edit_get_col (edit);
4287 break;
4288 case CK_Up:
4289 case CK_MarkUp:
4290 case CK_MarkColumnUp:
4291 case CK_Down:
4292 case CK_MarkDown:
4293 case CK_MarkColumnDown:
4294 case CK_PageUp:
4295 case CK_MarkPageUp:
4296 case CK_MarkColumnPageUp:
4297 case CK_PageDown:
4298 case CK_MarkPageDown:
4299 case CK_MarkColumnPageDown:
4300 case CK_Top:
4301 case CK_MarkToFileBegin:
4302 case CK_Bottom:
4303 case CK_MarkToFileEnd:
4304 case CK_ParagraphUp:
4305 case CK_MarkParagraphUp:
4306 case CK_MarkColumnParagraphUp:
4307 case CK_ParagraphDown:
4308 case CK_MarkParagraphDown:
4309 case CK_MarkColumnParagraphDown:
4310 case CK_ScrollUp:
4311 case CK_MarkScrollUp:
4312 case CK_MarkColumnScrollUp:
4313 case CK_ScrollDown:
4314 case CK_MarkScrollDown:
4315 case CK_MarkColumnScrollDown:
4316 edit->search_start = edit->curs1;
4317 edit->found_len = 0;
4318 break;
4319 default:
4320 edit->found_len = 0;
4321 edit->prev_col = edit_get_col (edit);
4322 edit->search_start = edit->curs1;
4324 edit_find_bracket (edit);
4326 if (option_auto_para_formatting)
4328 switch (command)
4330 case CK_BackSpace:
4331 case CK_Delete:
4332 case CK_DeleteToWordBegin:
4333 case CK_DeleteToWordEnd:
4334 case CK_DeleteToHome:
4335 case CK_DeleteToEnd:
4336 format_paragraph (edit, 0);
4337 edit->force |= REDRAW_PAGE;
4342 /* --------------------------------------------------------------------------------------------- */
4344 void
4345 edit_stack_init (void)
4347 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4349 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4350 edit_history_moveto[edit_stack_iterator].line = -1;
4353 edit_stack_iterator = 0;
4356 /* --------------------------------------------------------------------------------------------- */
4358 void
4359 edit_stack_free (void)
4361 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4362 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4365 /* --------------------------------------------------------------------------------------------- */
4366 /** move i lines */
4368 void
4369 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4371 edit_move_updown (edit, i, do_scroll, TRUE);
4374 /* --------------------------------------------------------------------------------------------- */
4375 /** move i lines */
4377 void
4378 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4380 edit_move_updown (edit, i, do_scroll, FALSE);
4383 /* --------------------------------------------------------------------------------------------- */