Ticket #1977: type accuracy of WEdit members and mceditor functions.
[midnight-commander.git] / src / editor / edit.c
blobd957339fe5a656b28462350fe0ce810f37d2e712
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, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/cmd.h" /* view_other_cmd() */
65 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* macro_index */
69 #include "src/learn.h" /* learn_keys */
70 #include "src/keybind-defaults.h"
72 #include "edit-impl.h"
73 #include "editwidget.h"
75 /*** global variables ****************************************************************************/
77 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
78 int option_typewriter_wrap = 0;
79 int option_auto_para_formatting = 0;
80 int option_fill_tabs_with_spaces = 0;
81 int option_return_does_auto_indent = 1;
82 int option_backspace_through_tabs = 0;
83 int option_fake_half_tabs = 1;
84 int option_save_mode = EDIT_QUICK_SAVE;
85 int option_save_position = 1;
86 int option_max_undo = 32768;
87 int option_persistent_selections = 1;
88 int option_cursor_beyond_eol = 0;
89 int option_line_state = 0;
90 int option_line_state_width = 0;
92 int option_edit_right_extreme = 0;
93 int option_edit_left_extreme = 0;
94 int option_edit_top_extreme = 0;
95 int option_edit_bottom_extreme = 0;
96 int enable_show_tabs_tws = 1;
97 int option_check_nl_at_eof = 0;
98 int option_group_undo = 0;
99 int show_right_margin = 0;
101 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
102 char *option_backup_ext = NULL;
104 unsigned int edit_stack_iterator = 0;
105 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
106 /* magic sequense for say than block is vertical */
107 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
109 /*** file scope macro definitions ****************************************************************/
111 #define TEMP_BUF_LEN 1024
113 #define space_width 1
115 /*** file scope type declarations ****************************************************************/
117 /*** file scope variables ************************************************************************/
119 /* detecting an error on save is easy: just check if every byte has been written. */
120 /* detecting an error on read, is not so easy 'cos there is not way to tell
121 whether you read everything or not. */
122 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
123 static const struct edit_filters
125 const char *read, *write, *extension;
126 } all_filters[] =
128 /* *INDENT-OFF* */
129 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
130 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
131 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
134 /* *INDENT-ON* */
137 static long last_bracket = -1;
139 /*** file scope functions ************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
144 * here's a quick sketch of the layout: (don't run this through indent.)
146 * (b1 is buffers1 and b2 is buffers2)
149 * \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
150 * ______________________________________|______________________________________
152 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
153 * |-> |-> |-> |-> |-> |-> |
155 * _<------------------------->|<----------------->_
156 * WEdit->curs2 | WEdit->curs1
157 * ^ | ^
158 * | ^|^ |
159 * cursor ||| cursor
160 * |||
161 * file end|||file beginning
166 * This_is_some_file
167 * fin.
170 /* --------------------------------------------------------------------------------------------- */
172 static int left_of_four_spaces (WEdit * edit);
174 /* --------------------------------------------------------------------------------------------- */
176 static void
177 edit_about (void)
179 const char *header = N_("About");
180 const char *button_name = N_("&OK");
181 const char *const version = "MCEdit " VERSION;
182 char text[BUF_LARGE];
184 int win_len, version_len, button_len;
185 int cols, lines;
187 Dlg_head *about_dlg;
189 #ifdef ENABLE_NLS
190 header = _(header);
191 button_name = _(button_name);
192 #endif
194 button_len = str_term_width1 (button_name) + 5;
195 version_len = str_term_width1 (version);
197 g_snprintf (text, sizeof (text),
198 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
199 " A user friendly text editor\n"
200 " written for the Midnight Commander"));
202 win_len = str_term_width1 (header);
203 win_len = max (win_len, version_len);
204 win_len = max (win_len, button_len);
206 /* count width and height of text */
207 str_msg_term_size (text, &lines, &cols);
208 lines += 9;
209 cols = max (win_len, cols) + 6;
211 /* dialog */
212 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
213 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
215 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
216 add_widget (about_dlg, label_new (5, 3, text));
217 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
218 B_ENTER, NORMAL_BUTTON, button_name, NULL));
220 run_dlg (about_dlg);
221 destroy_dlg (about_dlg);
224 /* --------------------------------------------------------------------------------------------- */
226 * Initialize the buffers for an empty files.
229 static void
230 edit_init_buffers (WEdit * edit)
232 int j;
234 for (j = 0; j <= MAXBUFF; j++)
236 edit->buffers1[j] = NULL;
237 edit->buffers2[j] = NULL;
240 edit->curs1 = 0;
241 edit->curs2 = 0;
242 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
245 /* --------------------------------------------------------------------------------------------- */
248 * Load file OR text into buffers. Set cursor to the beginning of file.
250 * @returns FALSE on error.
253 static gboolean
254 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
256 long buf, buf2;
257 int file = -1;
258 gboolean ret = FALSE;
260 edit->curs2 = edit->last_byte;
261 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
263 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
264 if (file == -1)
266 gchar *errmsg, *filename;
268 filename = vfs_path_to_str (filename_vpath);
269 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
270 g_free (filename);
271 edit_error_dialog (_("Error"), errmsg);
272 g_free (errmsg);
273 return FALSE;
276 if (!edit->buffers2[buf2])
277 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
281 if (mc_read (file,
282 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
283 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
284 break;
286 for (buf = buf2 - 1; buf >= 0; buf--)
288 /* edit->buffers2[0] is already allocated */
289 if (!edit->buffers2[buf])
290 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
291 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
292 break;
294 ret = TRUE;
296 while (FALSE);
298 if (!ret)
300 gchar *errmsg, *filename;
302 filename = vfs_path_to_str (filename_vpath);
303 errmsg = g_strdup_printf (_("Error reading %s"), filename);
304 g_free (filename);
305 edit_error_dialog (_("Error"), errmsg);
306 g_free (errmsg);
308 mc_close (file);
309 return ret;
312 /* --------------------------------------------------------------------------------------------- */
313 /** Return index of the filter or -1 is there is no appropriate filter */
315 static int
316 edit_find_filter (const vfs_path_t * filename_vpath)
318 size_t i, l, e;
319 char *filename;
321 if (filename_vpath == NULL)
322 return -1;
324 filename = vfs_path_to_str (filename_vpath);
325 l = strlen (filename);
326 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
328 e = strlen (all_filters[i].extension);
329 if (l > e)
330 if (!strcmp (all_filters[i].extension, filename + l - e))
332 g_free (filename);
333 return i;
336 g_free (filename);
337 return -1;
340 /* --------------------------------------------------------------------------------------------- */
342 static char *
343 edit_get_filter (const vfs_path_t * filename_vpath)
345 int i;
346 char *p, *quoted_name, *filename;
348 i = edit_find_filter (filename_vpath);
349 if (i < 0)
350 return NULL;
352 filename = vfs_path_to_str (filename_vpath);
353 quoted_name = name_quote (filename, 0);
354 g_free (filename);
355 p = g_strdup_printf (all_filters[i].read, quoted_name);
356 g_free (quoted_name);
357 return p;
360 /* --------------------------------------------------------------------------------------------- */
362 static long
363 edit_insert_stream (WEdit * edit, FILE * f)
365 int c;
366 long i = 0;
367 while ((c = fgetc (f)) >= 0)
369 edit_insert (edit, c);
370 i++;
372 return i;
375 /* --------------------------------------------------------------------------------------------- */
377 * Open file and create it if necessary.
379 * @param edit editor object
380 * @param filename_vpath file name
381 * @param st buffer for store stat info
382 * @returns TRUE for success, FALSE for error.
385 static gboolean
386 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
388 int file;
389 gchar *errmsg = NULL;
391 /* Try opening an existing file */
392 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
393 if (file < 0)
396 * Try creating the file. O_EXCL prevents following broken links
397 * and opening existing files.
399 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
400 if (file < 0)
402 char *filename;
404 filename = vfs_path_to_str (filename_vpath);
405 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
406 g_free (filename);
407 goto cleanup;
410 /* New file, delete it if it's not modified or saved */
411 edit->delete_file = 1;
414 /* Check what we have opened */
415 if (mc_fstat (file, st) < 0)
417 char *filename;
419 filename = vfs_path_to_str (filename_vpath);
420 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
421 g_free (filename);
422 goto cleanup;
425 /* We want to open regular files only */
426 if (!S_ISREG (st->st_mode))
428 char *filename;
430 filename = vfs_path_to_str (filename_vpath);
431 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
432 g_free (filename);
433 goto cleanup;
437 * Don't delete non-empty files.
438 * O_EXCL should prevent it, but let's be on the safe side.
440 if (st->st_size > 0)
441 edit->delete_file = 0;
443 if (st->st_size >= SIZE_LIMIT)
445 char *filename;
447 filename = vfs_path_to_str (filename_vpath);
448 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
449 g_free (filename);
452 cleanup:
453 (void) mc_close (file);
455 if (errmsg != NULL)
457 edit_error_dialog (_("Error"), errmsg);
458 g_free (errmsg);
459 return FALSE;
461 return TRUE;
464 /* --------------------------------------------------------------------------------------------- */
467 * Open the file and load it into the buffers, either directly or using
468 * a filter. Return TRUE on success, FALSE on error.
470 * Fast loading (edit_load_file_fast) is used when the file size is
471 * known. In this case the data is read into the buffers by blocks.
472 * If the file size is not known, the data is loaded byte by byte in
473 * edit_insert_file.
475 * @param edit editor object
476 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
478 static gboolean
479 edit_load_file (WEdit * edit)
481 gboolean fast_load = TRUE;
483 /* Cannot do fast load if a filter is used */
484 if (edit_find_filter (edit->filename_vpath) >= 0)
485 fast_load = FALSE;
488 * FIXME: line end translation should disable fast loading as well
489 * Consider doing fseek() to the end and ftell() for the real size.
491 if (edit->filename_vpath != NULL)
494 * VFS may report file size incorrectly, and slow load is not a big
495 * deal considering overhead in VFS.
497 if (!vfs_file_is_local (edit->filename_vpath))
498 fast_load = FALSE;
500 /* If we are dealing with a real file, check that it exists */
501 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
502 return FALSE;
504 else
506 /* nothing to load */
507 fast_load = FALSE;
510 edit_init_buffers (edit);
512 if (fast_load)
514 edit->last_byte = edit->stat1.st_size;
515 edit_load_file_fast (edit, edit->filename_vpath);
516 /* If fast load was used, the number of lines wasn't calculated */
517 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
519 else
521 edit->last_byte = 0;
522 if (edit->filename_vpath != NULL
523 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
525 edit->undo_stack_disable = 1;
526 if (edit_insert_file (edit, edit->filename_vpath) < 0)
528 edit_clean (edit);
529 return FALSE;
531 edit->undo_stack_disable = 0;
534 edit->lb = LB_ASIS;
535 return TRUE;
538 /* --------------------------------------------------------------------------------------------- */
539 /** Restore saved cursor position in the file */
541 static void
542 edit_load_position (WEdit * edit)
544 long line, column;
545 off_t offset;
547 if (edit->filename_vpath == NULL
548 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
549 return;
551 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
553 if (line > 0)
555 edit_move_to_line (edit, line - 1);
556 edit->prev_col = column;
558 else if (offset > 0)
560 edit_cursor_move (edit, offset);
561 line = edit->curs_line;
562 edit->search_start = edit->curs1;
565 book_mark_restore (edit, BOOK_MARK_COLOR);
567 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
568 edit_move_display (edit, line - (edit->widget.lines / 2));
571 /* --------------------------------------------------------------------------------------------- */
572 /** Save cursor position in the file */
574 static void
575 edit_save_position (WEdit * edit)
577 if (edit->filename_vpath == NULL
578 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
579 return;
581 book_mark_serialize (edit, BOOK_MARK_COLOR);
582 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
583 edit->serialized_bookmarks);
584 edit->serialized_bookmarks = NULL;
587 /* --------------------------------------------------------------------------------------------- */
588 /** Clean the WEdit stricture except the widget part */
590 static void
591 edit_purge_widget (WEdit * edit)
593 size_t len = sizeof (WEdit) - sizeof (Widget);
594 char *start = (char *) edit + sizeof (Widget);
595 memset (start, 0, len);
598 /* --------------------------------------------------------------------------------------------- */
601 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
602 then the file should be as it was when he loaded up. Then set edit->modified to 0.
605 static long
606 edit_pop_undo_action (WEdit * edit)
608 long c;
609 unsigned long sp = edit->undo_stack_pointer;
611 if (sp == edit->undo_stack_bottom)
612 return STACK_BOTTOM;
614 sp = (sp - 1) & edit->undo_stack_size_mask;
615 c = edit->undo_stack[sp];
616 if (c >= 0)
618 /* edit->undo_stack[sp] = '@'; */
619 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
620 return c;
623 if (sp == edit->undo_stack_bottom)
624 return STACK_BOTTOM;
626 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
627 if (edit->undo_stack[sp] == -2)
629 /* edit->undo_stack[sp] = '@'; */
630 edit->undo_stack_pointer = sp;
632 else
633 edit->undo_stack[sp]++;
635 return c;
638 static long
639 edit_pop_redo_action (WEdit * edit)
641 long c;
642 unsigned long sp = edit->redo_stack_pointer;
644 if (sp == edit->redo_stack_bottom)
645 return STACK_BOTTOM;
647 sp = (sp - 1) & edit->redo_stack_size_mask;
648 c = edit->redo_stack[sp];
649 if (c >= 0)
651 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
652 return c;
655 if (sp == edit->redo_stack_bottom)
656 return STACK_BOTTOM;
658 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
659 if (edit->redo_stack[sp] == -2)
660 edit->redo_stack_pointer = sp;
661 else
662 edit->redo_stack[sp]++;
664 return c;
667 static long
668 get_prev_undo_action (WEdit * edit)
670 long c;
671 unsigned long sp = edit->undo_stack_pointer;
673 if (sp == edit->undo_stack_bottom)
674 return STACK_BOTTOM;
676 sp = (sp - 1) & edit->undo_stack_size_mask;
677 c = edit->undo_stack[sp];
678 if (c >= 0)
679 return c;
681 if (sp == edit->undo_stack_bottom)
682 return STACK_BOTTOM;
684 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
685 return c;
688 /* --------------------------------------------------------------------------------------------- */
689 /** is called whenever a modification is made by one of the four routines below */
691 static void
692 edit_modification (WEdit * edit)
694 edit->caches_valid = 0;
696 /* raise lock when file modified */
697 if (!edit->modified && !edit->delete_file)
698 edit->locked = lock_file (edit->filename_vpath);
699 edit->modified = 1;
702 /* --------------------------------------------------------------------------------------------- */
704 static char *
705 edit_get_byte_ptr (WEdit * edit, long byte_index)
707 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
708 return NULL;
710 if (byte_index >= edit->curs1)
712 unsigned long p;
714 p = edit->curs1 + edit->curs2 - byte_index - 1;
715 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
716 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
719 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
720 (byte_index & M_EDIT_BUF_SIZE));
723 /* --------------------------------------------------------------------------------------------- */
725 static int
726 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
728 int i, res;
729 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
730 gchar *str;
731 gchar *cursor_buf_ptr;
733 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
735 *char_width = 0;
736 return 0;
739 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
740 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
741 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
743 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
744 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
746 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
748 *char_width = 1;
749 return *(cursor_buf_ptr - 1);
751 else
753 res = g_utf8_get_char_validated (str, -1);
755 if (res < 0)
757 *char_width = 1;
758 return *(cursor_buf_ptr - 1);
760 else
762 *char_width = cursor_buf_ptr - str;
763 return res;
768 /* --------------------------------------------------------------------------------------------- */
770 static int
771 edit_backspace (WEdit * edit, const int byte_delete)
773 int p = 0;
774 int cw = 1;
775 int i;
777 if (!edit->curs1)
778 return 0;
780 cw = 1;
782 if (edit->mark2 != edit->mark1)
783 edit_push_markers (edit);
785 if (edit->utf8 && byte_delete == 0)
787 edit_get_prev_utf (edit, edit->curs1, &cw);
788 if (cw < 1)
789 cw = 1;
791 for (i = 1; i <= cw; i++)
793 if (edit->mark1 >= edit->curs1)
795 edit->mark1--;
796 edit->end_mark_curs--;
798 if (edit->mark2 >= edit->curs1)
799 edit->mark2--;
800 if (edit->last_get_rule >= edit->curs1)
801 edit->last_get_rule--;
803 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
804 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
805 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
807 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
808 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
810 edit->last_byte--;
811 edit->curs1--;
812 edit_push_undo_action (edit, p);
814 edit_modification (edit);
815 if (p == '\n')
817 if (edit->book_mark)
818 book_mark_dec (edit, edit->curs_line);
819 edit->curs_line--;
820 edit->total_lines--;
821 edit->force |= REDRAW_AFTER_CURSOR;
824 if (edit->curs1 < edit->start_display)
826 edit->start_display--;
827 if (p == '\n')
828 edit->start_line--;
831 return p;
834 /* --------------------------------------------------------------------------------------------- */
835 /* high level cursor movement commands */
836 /* --------------------------------------------------------------------------------------------- */
838 static int
839 is_in_indent (WEdit * edit)
841 long p = edit_bol (edit, edit->curs1);
842 while (p < edit->curs1)
843 if (!strchr (" \t", edit_get_byte (edit, p++)))
844 return 0;
845 return 1;
848 /* --------------------------------------------------------------------------------------------- */
849 /** check whether line in editor is blank or not
851 * @param edit editor object
852 * @param offset position in file
854 * @return TRUE if line in blank, FALSE otherwise
857 static gboolean
858 is_blank (WEdit * edit, long offset)
860 long s, f;
861 int c;
863 s = edit_bol (edit, offset);
864 f = edit_eol (edit, offset) - 1;
865 while (s <= f)
867 c = edit_get_byte (edit, s++);
868 if (!isspace (c))
869 return FALSE;
871 return TRUE;
874 /* --------------------------------------------------------------------------------------------- */
875 /** returns the offset of line i */
877 static long
878 edit_find_line (WEdit * edit, int line)
880 int i, j = 0;
881 int m = 2000000000;
882 if (!edit->caches_valid)
884 for (i = 0; i < N_LINE_CACHES; i++)
885 edit->line_numbers[i] = edit->line_offsets[i] = 0;
886 /* three offsets that we *know* are line 0 at 0 and these two: */
887 edit->line_numbers[1] = edit->curs_line;
888 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
889 edit->line_numbers[2] = edit->total_lines;
890 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
891 edit->caches_valid = 1;
893 if (line >= edit->total_lines)
894 return edit->line_offsets[2];
895 if (line <= 0)
896 return 0;
897 /* find the closest known point */
898 for (i = 0; i < N_LINE_CACHES; i++)
900 int n;
901 n = abs (edit->line_numbers[i] - line);
902 if (n < m)
904 m = n;
905 j = i;
908 if (m == 0)
909 return edit->line_offsets[j]; /* know the offset exactly */
910 if (m == 1 && j >= 3)
911 i = j; /* one line different - caller might be looping, so stay in this cache */
912 else
913 i = 3 + (rand () % (N_LINE_CACHES - 3));
914 if (line > edit->line_numbers[j])
915 edit->line_offsets[i] =
916 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
917 else
918 edit->line_offsets[i] =
919 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
920 edit->line_numbers[i] = line;
921 return edit->line_offsets[i];
924 /* --------------------------------------------------------------------------------------------- */
925 /** moves up until a blank line is reached, or until just
926 before a non-blank line is reached */
928 static void
929 edit_move_up_paragraph (WEdit * edit, int do_scroll)
931 int i = 0;
932 if (edit->curs_line > 1)
934 if (line_is_blank (edit, edit->curs_line))
936 if (line_is_blank (edit, edit->curs_line - 1))
938 for (i = edit->curs_line - 1; i; i--)
939 if (!line_is_blank (edit, i))
941 i++;
942 break;
945 else
947 for (i = edit->curs_line - 1; i; i--)
948 if (line_is_blank (edit, i))
949 break;
952 else
954 for (i = edit->curs_line - 1; i; i--)
955 if (line_is_blank (edit, i))
956 break;
959 edit_move_up (edit, edit->curs_line - i, do_scroll);
962 /* --------------------------------------------------------------------------------------------- */
963 /** moves down until a blank line is reached, or until just
964 before a non-blank line is reached */
966 static void
967 edit_move_down_paragraph (WEdit * edit, int do_scroll)
969 int i;
970 if (edit->curs_line >= edit->total_lines - 1)
972 i = edit->total_lines;
974 else
976 if (line_is_blank (edit, edit->curs_line))
978 if (line_is_blank (edit, edit->curs_line + 1))
980 for (i = edit->curs_line + 1; i; i++)
981 if (!line_is_blank (edit, i) || i > edit->total_lines)
983 i--;
984 break;
987 else
989 for (i = edit->curs_line + 1; i; i++)
990 if (line_is_blank (edit, i) || i >= edit->total_lines)
991 break;
994 else
996 for (i = edit->curs_line + 1; i; i++)
997 if (line_is_blank (edit, i) || i >= edit->total_lines)
998 break;
1001 edit_move_down (edit, i - edit->curs_line, do_scroll);
1004 /* --------------------------------------------------------------------------------------------- */
1006 static void
1007 edit_begin_page (WEdit * edit)
1009 edit_update_curs_row (edit);
1010 edit_move_up (edit, edit->curs_row, 0);
1013 /* --------------------------------------------------------------------------------------------- */
1015 static void
1016 edit_end_page (WEdit * edit)
1018 edit_update_curs_row (edit);
1019 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
1023 /* --------------------------------------------------------------------------------------------- */
1024 /** goto beginning of text */
1026 static void
1027 edit_move_to_top (WEdit * edit)
1029 if (edit->curs_line)
1031 edit_cursor_move (edit, -edit->curs1);
1032 edit_move_to_prev_col (edit, 0);
1033 edit->force |= REDRAW_PAGE;
1034 edit->search_start = 0;
1035 edit_update_curs_row (edit);
1040 /* --------------------------------------------------------------------------------------------- */
1041 /** goto end of text */
1043 static void
1044 edit_move_to_bottom (WEdit * edit)
1046 if (edit->curs_line < edit->total_lines)
1048 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1049 edit->start_display = edit->last_byte;
1050 edit->start_line = edit->total_lines;
1051 edit_scroll_upward (edit, edit->widget.lines - 1);
1052 edit->force |= REDRAW_PAGE;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** goto beginning of line */
1059 static void
1060 edit_cursor_to_bol (WEdit * edit)
1062 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1063 edit->search_start = edit->curs1;
1064 edit->prev_col = edit_get_col (edit);
1065 edit->over_col = 0;
1068 /* --------------------------------------------------------------------------------------------- */
1069 /** goto end of line */
1071 static void
1072 edit_cursor_to_eol (WEdit * edit)
1074 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1075 edit->search_start = edit->curs1;
1076 edit->prev_col = edit_get_col (edit);
1077 edit->over_col = 0;
1080 /* --------------------------------------------------------------------------------------------- */
1082 static unsigned long
1083 my_type_of (int c)
1085 int x, r = 0;
1086 const char *p, *q;
1087 const char option_chars_move_whole_word[] =
1088 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1090 if (!c)
1091 return 0;
1092 if (c == '!')
1094 if (*option_chars_move_whole_word == '!')
1095 return 2;
1096 return 0x80000000UL;
1098 if (g_ascii_isupper ((gchar) c))
1099 c = 'A';
1100 else if (g_ascii_islower ((gchar) c))
1101 c = 'a';
1102 else if (g_ascii_isalpha (c))
1103 c = 'a';
1104 else if (isdigit (c))
1105 c = '0';
1106 else if (isspace (c))
1107 c = ' ';
1108 q = strchr (option_chars_move_whole_word, c);
1109 if (!q)
1110 return 0xFFFFFFFFUL;
1113 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1114 if (*p == '!')
1115 x <<= 1;
1116 r |= x;
1118 while ((q = strchr (q + 1, c)));
1119 return r;
1122 /* --------------------------------------------------------------------------------------------- */
1124 static void
1125 edit_left_word_move (WEdit * edit, int s)
1127 for (;;)
1129 int c1, c2;
1130 if (edit->column_highlight
1131 && edit->mark1 != edit->mark2
1132 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1133 break;
1134 edit_cursor_move (edit, -1);
1135 if (!edit->curs1)
1136 break;
1137 c1 = edit_get_byte (edit, edit->curs1 - 1);
1138 c2 = edit_get_byte (edit, edit->curs1);
1139 if (c1 == '\n' || c2 == '\n')
1140 break;
1141 if (!(my_type_of (c1) & my_type_of (c2)))
1142 break;
1143 if (isspace (c1) && !isspace (c2))
1144 break;
1145 if (s)
1146 if (!isspace (c1) && isspace (c2))
1147 break;
1151 /* --------------------------------------------------------------------------------------------- */
1153 static void
1154 edit_left_word_move_cmd (WEdit * edit)
1156 edit_left_word_move (edit, 0);
1157 edit->force |= REDRAW_PAGE;
1160 /* --------------------------------------------------------------------------------------------- */
1162 static void
1163 edit_right_word_move (WEdit * edit, int s)
1165 for (;;)
1167 int c1, c2;
1168 if (edit->column_highlight
1169 && edit->mark1 != edit->mark2
1170 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1171 break;
1172 edit_cursor_move (edit, 1);
1173 if (edit->curs1 >= edit->last_byte)
1174 break;
1175 c1 = edit_get_byte (edit, edit->curs1 - 1);
1176 c2 = edit_get_byte (edit, edit->curs1);
1177 if (c1 == '\n' || c2 == '\n')
1178 break;
1179 if (!(my_type_of (c1) & my_type_of (c2)))
1180 break;
1181 if (isspace (c1) && !isspace (c2))
1182 break;
1183 if (s)
1184 if (!isspace (c1) && isspace (c2))
1185 break;
1189 /* --------------------------------------------------------------------------------------------- */
1191 static void
1192 edit_right_word_move_cmd (WEdit * edit)
1194 edit_right_word_move (edit, 0);
1195 edit->force |= REDRAW_PAGE;
1198 /* --------------------------------------------------------------------------------------------- */
1200 static void
1201 edit_right_char_move_cmd (WEdit * edit)
1203 int cw = 1;
1204 int c = 0;
1205 if (edit->utf8)
1207 c = edit_get_utf (edit, edit->curs1, &cw);
1208 if (cw < 1)
1209 cw = 1;
1211 else
1213 c = edit_get_byte (edit, edit->curs1);
1215 if (option_cursor_beyond_eol && c == '\n')
1217 edit->over_col++;
1219 else
1221 edit_cursor_move (edit, cw);
1225 /* --------------------------------------------------------------------------------------------- */
1227 static void
1228 edit_left_char_move_cmd (WEdit * edit)
1230 int cw = 1;
1231 if (edit->column_highlight
1232 && option_cursor_beyond_eol
1233 && edit->mark1 != edit->mark2
1234 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1235 return;
1236 if (edit->utf8)
1238 edit_get_prev_utf (edit, edit->curs1, &cw);
1239 if (cw < 1)
1240 cw = 1;
1242 if (option_cursor_beyond_eol && edit->over_col > 0)
1244 edit->over_col--;
1246 else
1248 edit_cursor_move (edit, -cw);
1252 /* --------------------------------------------------------------------------------------------- */
1253 /** Up or down cursor moving.
1254 direction = TRUE - move up
1255 = FALSE - move down
1258 static void
1259 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1261 unsigned long p;
1262 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1264 if (i > l)
1265 i = l;
1267 if (i == 0)
1268 return;
1270 if (i > 1)
1271 edit->force |= REDRAW_PAGE;
1272 if (do_scroll)
1274 if (direction)
1275 edit_scroll_upward (edit, i);
1276 else
1277 edit_scroll_downward (edit, i);
1279 p = edit_bol (edit, edit->curs1);
1281 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1283 edit_cursor_move (edit, p - edit->curs1);
1285 edit_move_to_prev_col (edit, p);
1287 /* search start of current multibyte char (like CJK) */
1288 if (edit->curs1 + 1 < edit->last_byte)
1290 edit_right_char_move_cmd (edit);
1291 edit_left_char_move_cmd (edit);
1294 edit->search_start = edit->curs1;
1295 edit->found_len = 0;
1298 /* --------------------------------------------------------------------------------------------- */
1300 static void
1301 edit_right_delete_word (WEdit * edit)
1303 int c1, c2;
1304 for (;;)
1306 if (edit->curs1 >= edit->last_byte)
1307 break;
1308 c1 = edit_delete (edit, 1);
1309 c2 = edit_get_byte (edit, edit->curs1);
1310 if (c1 == '\n' || c2 == '\n')
1311 break;
1312 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1313 break;
1314 if (!(my_type_of (c1) & my_type_of (c2)))
1315 break;
1319 /* --------------------------------------------------------------------------------------------- */
1321 static void
1322 edit_left_delete_word (WEdit * edit)
1324 int c1, c2;
1325 for (;;)
1327 if (edit->curs1 <= 0)
1328 break;
1329 c1 = edit_backspace (edit, 1);
1330 c2 = edit_get_byte (edit, edit->curs1 - 1);
1331 if (c1 == '\n' || c2 == '\n')
1332 break;
1333 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1334 break;
1335 if (!(my_type_of (c1) & my_type_of (c2)))
1336 break;
1340 /* --------------------------------------------------------------------------------------------- */
1342 the start column position is not recorded, and hence does not
1343 undo as it happed. But who would notice.
1346 static void
1347 edit_do_undo (WEdit * edit)
1349 long ac;
1350 long count = 0;
1352 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1353 edit->over_col = 0;
1354 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1356 switch ((int) ac)
1358 case STACK_BOTTOM:
1359 goto done_undo;
1360 case CURS_RIGHT:
1361 edit_cursor_move (edit, 1);
1362 break;
1363 case CURS_LEFT:
1364 edit_cursor_move (edit, -1);
1365 break;
1366 case BACKSPACE:
1367 case BACKSPACE_BR:
1368 edit_backspace (edit, 1);
1369 break;
1370 case DELCHAR:
1371 case DELCHAR_BR:
1372 edit_delete (edit, 1);
1373 break;
1374 case COLUMN_ON:
1375 edit->column_highlight = 1;
1376 break;
1377 case COLUMN_OFF:
1378 edit->column_highlight = 0;
1379 break;
1381 if (ac >= 256 && ac < 512)
1382 edit_insert_ahead (edit, ac - 256);
1383 if (ac >= 0 && ac < 256)
1384 edit_insert (edit, ac);
1386 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1388 edit->mark1 = ac - MARK_1;
1389 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1391 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1393 edit->mark2 = ac - MARK_2;
1394 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1396 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1398 edit->end_mark_curs = ac - MARK_CURS;
1400 if (count++)
1401 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1404 if (edit->start_display > ac - KEY_PRESS)
1406 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1407 edit->force |= REDRAW_PAGE;
1409 else if (edit->start_display < ac - KEY_PRESS)
1411 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1412 edit->force |= REDRAW_PAGE;
1414 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1415 edit_update_curs_row (edit);
1417 done_undo:;
1418 edit->undo_stack_disable = 0;
1421 static void
1422 edit_do_redo (WEdit * edit)
1424 long ac;
1425 long count = 0;
1427 if (edit->redo_stack_reset)
1428 return;
1430 edit->over_col = 0;
1431 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1433 switch ((int) ac)
1435 case STACK_BOTTOM:
1436 goto done_redo;
1437 case CURS_RIGHT:
1438 edit_cursor_move (edit, 1);
1439 break;
1440 case CURS_LEFT:
1441 edit_cursor_move (edit, -1);
1442 break;
1443 case BACKSPACE:
1444 edit_backspace (edit, 1);
1445 break;
1446 case DELCHAR:
1447 edit_delete (edit, 1);
1448 break;
1449 case COLUMN_ON:
1450 edit->column_highlight = 1;
1451 break;
1452 case COLUMN_OFF:
1453 edit->column_highlight = 0;
1454 break;
1456 if (ac >= 256 && ac < 512)
1457 edit_insert_ahead (edit, ac - 256);
1458 if (ac >= 0 && ac < 256)
1459 edit_insert (edit, ac);
1461 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1463 edit->mark1 = ac - MARK_1;
1464 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1466 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1468 edit->mark2 = ac - MARK_2;
1469 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1471 /* more than one pop usually means something big */
1472 if (count++)
1473 edit->force |= REDRAW_PAGE;
1476 if (edit->start_display > ac - KEY_PRESS)
1478 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1479 edit->force |= REDRAW_PAGE;
1481 else if (edit->start_display < ac - KEY_PRESS)
1483 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1484 edit->force |= REDRAW_PAGE;
1486 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1487 edit_update_curs_row (edit);
1489 done_redo:;
1492 static void
1493 edit_group_undo (WEdit * edit)
1495 long ac = KEY_PRESS;
1496 long cur_ac = KEY_PRESS;
1497 while (ac != STACK_BOTTOM && ac == cur_ac)
1499 cur_ac = get_prev_undo_action (edit);
1500 edit_do_undo (edit);
1501 ac = get_prev_undo_action (edit);
1502 /* exit from cycle if option_group_undo is not set,
1503 * and make single UNDO operation
1505 if (!option_group_undo)
1506 ac = STACK_BOTTOM;
1510 /* --------------------------------------------------------------------------------------------- */
1512 static void
1513 edit_delete_to_line_end (WEdit * edit)
1515 while (edit_get_byte (edit, edit->curs1) != '\n')
1517 if (!edit->curs2)
1518 break;
1519 edit_delete (edit, 1);
1523 /* --------------------------------------------------------------------------------------------- */
1525 static void
1526 edit_delete_to_line_begin (WEdit * edit)
1528 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1530 if (!edit->curs1)
1531 break;
1532 edit_backspace (edit, 1);
1536 /* --------------------------------------------------------------------------------------------- */
1538 static int
1539 is_aligned_on_a_tab (WEdit * edit)
1541 edit_update_curs_col (edit);
1542 return !((edit->curs_col % (TAB_SIZE * space_width))
1543 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1546 /* --------------------------------------------------------------------------------------------- */
1548 static int
1549 right_of_four_spaces (WEdit * edit)
1551 int i, ch = 0;
1552 for (i = 1; i <= HALF_TAB_SIZE; i++)
1553 ch |= edit_get_byte (edit, edit->curs1 - i);
1554 if (ch == ' ')
1555 return is_aligned_on_a_tab (edit);
1556 return 0;
1559 /* --------------------------------------------------------------------------------------------- */
1561 static int
1562 left_of_four_spaces (WEdit * edit)
1564 int i, ch = 0;
1565 for (i = 0; i < HALF_TAB_SIZE; i++)
1566 ch |= edit_get_byte (edit, edit->curs1 + i);
1567 if (ch == ' ')
1568 return is_aligned_on_a_tab (edit);
1569 return 0;
1572 /* --------------------------------------------------------------------------------------------- */
1574 static void
1575 edit_auto_indent (WEdit * edit)
1577 long p;
1578 char c;
1579 p = edit->curs1;
1580 /* use the previous line as a template */
1581 p = edit_move_backward (edit, p, 1);
1582 /* copy the leading whitespace of the line */
1583 for (;;)
1584 { /* no range check - the line _is_ \n-terminated */
1585 c = edit_get_byte (edit, p++);
1586 if (c != ' ' && c != '\t')
1587 break;
1588 edit_insert (edit, c);
1592 /* --------------------------------------------------------------------------------------------- */
1594 static inline void
1595 edit_double_newline (WEdit * edit)
1597 edit_insert (edit, '\n');
1598 if (edit_get_byte (edit, edit->curs1) == '\n')
1599 return;
1600 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1601 return;
1602 edit->force |= REDRAW_PAGE;
1603 edit_insert (edit, '\n');
1607 /* --------------------------------------------------------------------------------------------- */
1609 static void
1610 insert_spaces_tab (WEdit * edit, gboolean half)
1612 int i;
1614 edit_update_curs_col (edit);
1615 i = option_tab_spacing * space_width;
1616 if (half)
1617 i /= 2;
1618 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1619 while (i > 0)
1621 edit_insert (edit, ' ');
1622 i -= space_width;
1626 /* --------------------------------------------------------------------------------------------- */
1628 static inline void
1629 edit_tab_cmd (WEdit * edit)
1631 int i;
1633 if (option_fake_half_tabs)
1635 if (is_in_indent (edit))
1637 /*insert a half tab (usually four spaces) unless there is a
1638 half tab already behind, then delete it and insert a
1639 full tab. */
1640 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1641 insert_spaces_tab (edit, TRUE);
1642 else
1644 for (i = 1; i <= HALF_TAB_SIZE; i++)
1645 edit_backspace (edit, 1);
1646 edit_insert (edit, '\t');
1648 return;
1651 if (option_fill_tabs_with_spaces)
1652 insert_spaces_tab (edit, FALSE);
1653 else
1654 edit_insert (edit, '\t');
1657 /* --------------------------------------------------------------------------------------------- */
1659 static void
1660 check_and_wrap_line (WEdit * edit)
1662 int curs, c;
1663 if (!option_typewriter_wrap)
1664 return;
1665 edit_update_curs_col (edit);
1666 if (edit->curs_col < option_word_wrap_line_length)
1667 return;
1668 curs = edit->curs1;
1669 for (;;)
1671 curs--;
1672 c = edit_get_byte (edit, curs);
1673 if (c == '\n' || curs <= 0)
1675 edit_insert (edit, '\n');
1676 return;
1678 if (c == ' ' || c == '\t')
1680 int current = edit->curs1;
1681 edit_cursor_move (edit, curs - edit->curs1 + 1);
1682 edit_insert (edit, '\n');
1683 edit_cursor_move (edit, current - edit->curs1 + 1);
1684 return;
1689 /* --------------------------------------------------------------------------------------------- */
1690 /** this find the matching bracket in either direction, and sets edit->bracket */
1692 static long
1693 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1695 const char *const b = "{}{[][()(", *p;
1696 int i = 1, a, inc = -1, c, d, n = 0;
1697 unsigned long j = 0;
1698 long q;
1699 edit_update_curs_row (edit);
1700 c = edit_get_byte (edit, edit->curs1);
1701 p = strchr (b, c);
1702 /* no limit */
1703 if (!furthest_bracket_search)
1704 furthest_bracket_search--;
1705 /* not on a bracket at all */
1706 if (!p)
1707 return -1;
1708 /* the matching bracket */
1709 d = p[1];
1710 /* going left or right? */
1711 if (strchr ("{[(", c))
1712 inc = 1;
1713 for (q = edit->curs1 + inc;; q += inc)
1715 /* out of buffer? */
1716 if (q >= edit->last_byte || q < 0)
1717 break;
1718 a = edit_get_byte (edit, q);
1719 /* don't want to eat CPU */
1720 if (j++ > furthest_bracket_search)
1721 break;
1722 /* out of screen? */
1723 if (in_screen)
1725 if (q < edit->start_display)
1726 break;
1727 /* count lines if searching downward */
1728 if (inc > 0 && a == '\n')
1729 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1730 break;
1732 /* count bracket depth */
1733 i += (a == c) - (a == d);
1734 /* return if bracket depth is zero */
1735 if (!i)
1736 return q;
1738 /* no match */
1739 return -1;
1742 /* --------------------------------------------------------------------------------------------- */
1744 static inline void
1745 edit_goto_matching_bracket (WEdit * edit)
1747 long q;
1749 q = edit_get_bracket (edit, 0, 0);
1750 if (q >= 0)
1752 edit->bracket = edit->curs1;
1753 edit->force |= REDRAW_PAGE;
1754 edit_cursor_move (edit, q - edit->curs1);
1758 /* --------------------------------------------------------------------------------------------- */
1760 static void
1761 edit_move_block_to_right (WEdit * edit)
1763 long start_mark, end_mark;
1764 long cur_bol, start_bol;
1766 if (eval_marks (edit, &start_mark, &end_mark))
1767 return;
1769 start_bol = edit_bol (edit, start_mark);
1770 cur_bol = edit_bol (edit, end_mark - 1);
1774 edit_cursor_move (edit, cur_bol - edit->curs1);
1775 if (option_fill_tabs_with_spaces)
1776 insert_spaces_tab (edit, option_fake_half_tabs);
1777 else
1778 edit_insert (edit, '\t');
1779 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1781 if (cur_bol == 0)
1782 break;
1784 cur_bol = edit_bol (edit, cur_bol - 1);
1786 while (cur_bol >= start_bol);
1788 edit->force |= REDRAW_PAGE;
1791 /* --------------------------------------------------------------------------------------------- */
1793 static void
1794 edit_move_block_to_left (WEdit * edit)
1796 long start_mark, end_mark;
1797 long cur_bol, start_bol;
1798 int i;
1800 if (eval_marks (edit, &start_mark, &end_mark))
1801 return;
1803 start_bol = edit_bol (edit, start_mark);
1804 cur_bol = edit_bol (edit, end_mark - 1);
1808 int del_tab_width;
1809 int next_char;
1811 edit_cursor_move (edit, cur_bol - edit->curs1);
1813 if (option_fake_half_tabs)
1814 del_tab_width = HALF_TAB_SIZE;
1815 else
1816 del_tab_width = option_tab_spacing;
1818 next_char = edit_get_byte (edit, edit->curs1);
1819 if (next_char == '\t')
1820 edit_delete (edit, 1);
1821 else if (next_char == ' ')
1822 for (i = 1; i <= del_tab_width; i++)
1824 if (next_char == ' ')
1825 edit_delete (edit, 1);
1826 next_char = edit_get_byte (edit, edit->curs1);
1829 if (cur_bol == 0)
1830 break;
1832 cur_bol = edit_bol (edit, cur_bol - 1);
1834 while (cur_bol >= start_bol);
1836 edit->force |= REDRAW_PAGE;
1839 /* --------------------------------------------------------------------------------------------- */
1841 * prints at the cursor
1842 * @returns the number of chars printed
1845 static size_t
1846 edit_print_string (WEdit * e, const char *s)
1848 size_t i = 0;
1850 while (s[i] != '\0')
1851 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1852 e->force |= REDRAW_COMPLETELY;
1853 edit_update_screen (e);
1854 return i;
1857 /* --------------------------------------------------------------------------------------------- */
1858 /*** public functions ****************************************************************************/
1859 /* --------------------------------------------------------------------------------------------- */
1861 /** User edit menu, like user menu (F2) but only in editor. */
1863 void
1864 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1866 char *block_file;
1867 int nomark;
1868 long curs;
1869 long start_mark, end_mark;
1870 struct stat status;
1871 vfs_path_t *block_file_vpath;
1873 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1874 block_file_vpath = vfs_path_from_str (block_file);
1875 curs = edit->curs1;
1876 nomark = eval_marks (edit, &start_mark, &end_mark);
1877 if (nomark == 0)
1878 edit_save_block (edit, block_file, start_mark, end_mark);
1880 /* run shell scripts from menu */
1881 if (user_menu_cmd (edit, menu_file, selected_entry)
1882 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1884 int rc = 0;
1885 FILE *fd;
1887 /* i.e. we have marked block */
1888 if (nomark == 0)
1889 rc = edit_block_delete_cmd (edit);
1891 if (rc == 0)
1893 long ins_len;
1895 ins_len = edit_insert_file (edit, block_file_vpath);
1896 if (nomark == 0 && ins_len > 0)
1897 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1899 /* truncate block file */
1900 fd = fopen (block_file, "w");
1901 if (fd != NULL)
1902 fclose (fd);
1904 edit_cursor_move (edit, curs - edit->curs1);
1905 edit_refresh_cmd (edit);
1906 edit->force |= REDRAW_COMPLETELY;
1908 g_free (block_file);
1909 vfs_path_free (block_file_vpath);
1912 /* --------------------------------------------------------------------------------------------- */
1915 edit_get_byte (WEdit * edit, long byte_index)
1917 unsigned long p;
1918 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1919 return '\n';
1921 if (byte_index >= edit->curs1)
1923 p = edit->curs1 + edit->curs2 - byte_index - 1;
1924 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1926 else
1928 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1932 /* --------------------------------------------------------------------------------------------- */
1935 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1937 gchar *str = NULL;
1938 int res = -1;
1939 gunichar ch;
1940 gchar *next_ch = NULL;
1941 int width = 0;
1942 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1944 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1946 *char_width = 0;
1947 return '\n';
1950 str = edit_get_byte_ptr (edit, byte_index);
1952 if (str == NULL)
1954 *char_width = 0;
1955 return 0;
1958 res = g_utf8_get_char_validated (str, -1);
1960 if (res < 0)
1962 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1963 int i;
1964 for (i = 0; i < UTF8_CHAR_LEN; i++)
1965 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1966 utf8_buf[UTF8_CHAR_LEN] = '\0';
1967 str = utf8_buf;
1968 res = g_utf8_get_char_validated (str, -1);
1971 if (res < 0)
1973 ch = *str;
1974 width = 0;
1976 else
1978 ch = res;
1979 /* Calculate UTF-8 char width */
1980 next_ch = g_utf8_next_char (str);
1981 if (next_ch)
1983 width = next_ch - str;
1985 else
1987 ch = 0;
1988 width = 0;
1991 *char_width = width;
1992 return ch;
1995 /* --------------------------------------------------------------------------------------------- */
1997 char *
1998 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
2000 int i;
2001 char *p, *writename;
2002 const vfs_path_element_t *path_element;
2004 i = edit_find_filter (filename_vpath);
2005 if (i < 0)
2006 return NULL;
2008 path_element = vfs_path_get_by_index (write_name_vpath, -1);
2009 writename = name_quote (path_element->path, 0);
2010 p = g_strdup_printf (all_filters[i].write, writename);
2011 g_free (writename);
2012 return p;
2015 /* --------------------------------------------------------------------------------------------- */
2017 long
2018 edit_write_stream (WEdit * edit, FILE * f)
2020 long i;
2022 if (edit->lb == LB_ASIS)
2024 for (i = 0; i < edit->last_byte; i++)
2025 if (fputc (edit_get_byte (edit, i), f) < 0)
2026 break;
2027 return i;
2030 /* change line breaks */
2031 for (i = 0; i < edit->last_byte; i++)
2033 unsigned char c = edit_get_byte (edit, i);
2035 if (!(c == '\n' || c == '\r'))
2037 /* not line break */
2038 if (fputc (c, f) < 0)
2039 return i;
2041 else
2042 { /* (c == '\n' || c == '\r') */
2043 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2045 switch (edit->lb)
2047 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2048 /* put one line break unconditionally */
2049 if (fputc ('\n', f) < 0)
2050 return i;
2052 i++; /* 2 chars are processed */
2054 if (c == '\r' && c1 == '\n')
2055 /* Windows line break; go to the next char */
2056 break;
2058 if (c == '\r' && c1 == '\r')
2060 /* two Macintosh line breaks; put second line break */
2061 if (fputc ('\n', f) < 0)
2062 return i;
2063 break;
2066 if (fputc (c1, f) < 0)
2067 return i;
2068 break;
2070 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2071 /* put one line break unconditionally */
2072 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2073 return i;
2075 if (c == '\r' && c1 == '\n')
2076 /* Windows line break; go to the next char */
2077 i++;
2078 break;
2080 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2081 /* put one line break unconditionally */
2082 if (fputc ('\r', f) < 0)
2083 return i;
2085 i++; /* 2 chars are processed */
2087 if (c == '\r' && c1 == '\n')
2088 /* Windows line break; go to the next char */
2089 break;
2091 if (c == '\n' && c1 == '\n')
2093 /* two Windows line breaks; put second line break */
2094 if (fputc ('\r', f) < 0)
2095 return i;
2096 break;
2099 if (fputc (c1, f) < 0)
2100 return i;
2101 break;
2102 case LB_ASIS: /* default without changes */
2103 break;
2108 return edit->last_byte;
2111 /* --------------------------------------------------------------------------------------------- */
2112 /** inserts a file at the cursor, returns count of inserted bytes on success */
2113 long
2114 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2116 char *p;
2117 long ins_len = 0;
2119 p = edit_get_filter (filename_vpath);
2120 if (p != NULL)
2122 FILE *f;
2123 long current = edit->curs1;
2125 f = (FILE *) popen (p, "r");
2126 if (f != NULL)
2128 edit_insert_stream (edit, f);
2129 ins_len = edit->curs1 - current;
2130 edit_cursor_move (edit, -ins_len);
2131 if (pclose (f) > 0)
2133 char *errmsg;
2135 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2136 edit_error_dialog (_("Error"), errmsg);
2137 g_free (errmsg);
2138 ins_len = -1;
2141 else
2143 char *errmsg;
2145 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2146 edit_error_dialog (_("Error"), errmsg);
2147 g_free (errmsg);
2148 ins_len = -1;
2150 g_free (p);
2152 else
2154 int i, file, blocklen;
2155 long current = edit->curs1;
2156 int vertical_insertion = 0;
2157 char *buf;
2159 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2160 if (file == -1)
2161 return -1;
2163 buf = g_malloc0 (TEMP_BUF_LEN);
2164 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2165 if (blocklen > 0)
2167 /* if contain signature VERTICAL_MAGIC then it vertical block */
2168 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2169 vertical_insertion = 1;
2170 else
2171 mc_lseek (file, 0, SEEK_SET);
2174 if (vertical_insertion)
2176 long mark1, mark2;
2177 int c1, c2;
2179 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2180 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2181 /* highlight inserted text then not persistent blocks */
2182 if (!option_persistent_selections)
2184 if (!edit->column_highlight)
2185 edit_push_undo_action (edit, COLUMN_OFF);
2186 edit->column_highlight = 1;
2189 else
2191 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2193 for (i = 0; i < blocklen; i++)
2194 edit_insert (edit, buf[i]);
2196 /* highlight inserted text then not persistent blocks */
2197 if (!option_persistent_selections && edit->modified)
2199 edit_set_markers (edit, edit->curs1, current, 0, 0);
2200 if (edit->column_highlight)
2201 edit_push_undo_action (edit, COLUMN_ON);
2202 edit->column_highlight = 0;
2206 edit->force |= REDRAW_PAGE;
2207 ins_len = edit->curs1 - current;
2208 edit_cursor_move (edit, -ins_len);
2209 g_free (buf);
2210 mc_close (file);
2211 if (blocklen != 0)
2212 ins_len = 0;
2215 return ins_len;
2218 /* --------------------------------------------------------------------------------------------- */
2220 * Fill in the edit structure. Return NULL on failure. Pass edit as
2221 * NULL to allocate a new structure.
2223 * If line is 0, try to restore saved position. Otherwise put the
2224 * cursor on that line and show it in the middle of the screen.
2227 WEdit *
2228 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2229 long line)
2231 gboolean to_free = FALSE;
2233 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2234 if (option_line_state)
2235 option_line_state_width = LINE_STATE_WIDTH;
2236 else
2237 option_line_state_width = 0;
2239 if (edit == NULL)
2241 #ifdef ENABLE_NLS
2243 * Expand option_whole_chars_search by national letters using
2244 * current locale
2247 static char option_whole_chars_search_buf[256];
2249 if (option_whole_chars_search_buf != option_whole_chars_search)
2251 size_t i;
2252 size_t len = str_term_width1 (option_whole_chars_search);
2254 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2256 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2258 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2260 option_whole_chars_search_buf[len++] = i;
2264 option_whole_chars_search_buf[len] = 0;
2265 option_whole_chars_search = option_whole_chars_search_buf;
2267 #endif /* ENABLE_NLS */
2268 edit = g_malloc0 (sizeof (WEdit));
2269 edit->search = NULL;
2270 to_free = TRUE;
2273 edit_purge_widget (edit);
2274 edit->widget.y = y;
2275 edit->widget.x = x;
2276 edit->widget.lines = lines;
2277 edit->widget.cols = cols;
2279 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2280 edit->stat1.st_uid = getuid ();
2281 edit->stat1.st_gid = getgid ();
2282 edit->stat1.st_mtime = 0;
2284 edit->over_col = 0;
2285 edit->bracket = -1;
2286 edit->force |= REDRAW_PAGE;
2287 edit_set_filename (edit, filename_vpath);
2289 edit->undo_stack_size = START_STACK_SIZE;
2290 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2291 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2293 edit->redo_stack_size = START_STACK_SIZE;
2294 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2295 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2297 edit->utf8 = 0;
2298 edit->converter = str_cnv_from_term;
2299 edit_set_codeset (edit);
2301 if (!edit_load_file (edit))
2303 /* edit_load_file already gives an error message */
2304 if (to_free)
2305 g_free (edit);
2306 return NULL;
2309 edit->loading_done = 1;
2310 edit->modified = 0;
2311 edit->locked = 0;
2312 edit_load_syntax (edit, NULL, NULL);
2314 int color;
2315 edit_get_syntax_color (edit, -1, &color);
2318 /* load saved cursor position */
2319 if ((line == 0) && option_save_position)
2320 edit_load_position (edit);
2321 else
2323 if (line <= 0)
2324 line = 1;
2325 edit_move_display (edit, line - 1);
2326 edit_move_to_line (edit, line - 1);
2329 edit_load_macro_cmd (edit);
2330 return edit;
2333 /* --------------------------------------------------------------------------------------------- */
2335 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2336 gboolean
2337 edit_clean (WEdit * edit)
2339 int j = 0;
2341 if (edit == NULL)
2342 return FALSE;
2344 /* a stale lock, remove it */
2345 if (edit->locked)
2346 edit->locked = unlock_file (edit->filename_vpath);
2348 /* save cursor position */
2349 if (option_save_position)
2350 edit_save_position (edit);
2351 else if (edit->serialized_bookmarks != NULL)
2352 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2354 /* File specified on the mcedit command line and never saved */
2355 if (edit->delete_file)
2356 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2358 edit_free_syntax_rules (edit);
2359 book_mark_flush (edit, -1);
2360 for (; j <= MAXBUFF; j++)
2362 g_free (edit->buffers1[j]);
2363 g_free (edit->buffers2[j]);
2366 g_free (edit->undo_stack);
2367 g_free (edit->redo_stack);
2368 vfs_path_free (edit->filename_vpath);
2369 vfs_path_free (edit->dir_vpath);
2370 mc_search_free (edit->search);
2371 edit->search = NULL;
2373 if (edit->converter != str_cnv_from_term)
2374 str_close_conv (edit->converter);
2376 edit_purge_widget (edit);
2378 return TRUE;
2381 /* --------------------------------------------------------------------------------------------- */
2383 /** returns TRUE on success */
2384 gboolean
2385 edit_renew (WEdit * edit)
2387 int y = edit->widget.y;
2388 int x = edit->widget.x;
2389 int lines = edit->widget.lines;
2390 int columns = edit->widget.cols;
2392 edit_clean (edit);
2393 return (edit_init (edit, y, x, lines, columns, NULL, 0) != NULL);
2396 /* --------------------------------------------------------------------------------------------- */
2399 * Load a new file into the editor and set line. If it fails, preserve the old file.
2400 * To do it, allocate a new widget, initialize it and, if the new file
2401 * was loaded, copy the data to the old widget.
2403 * @returns TRUE on success, FALSE on failure.
2405 gboolean
2406 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2408 WEdit *e;
2409 int y = edit->widget.y;
2410 int x = edit->widget.x;
2411 int lines = edit->widget.lines;
2412 int columns = edit->widget.cols;
2414 e = g_malloc0 (sizeof (WEdit));
2415 e->widget = edit->widget;
2417 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2419 g_free (e);
2420 return FALSE;
2423 edit_clean (edit);
2424 memcpy (edit, e, sizeof (WEdit));
2425 g_free (e);
2427 return TRUE;
2430 /* --------------------------------------------------------------------------------------------- */
2432 void
2433 edit_set_codeset (WEdit * edit)
2435 #ifdef HAVE_CHARSET
2436 const char *cp_id;
2438 cp_id =
2439 get_codepage_id (mc_global.source_codepage >=
2440 0 ? mc_global.source_codepage : mc_global.display_codepage);
2442 if (cp_id != NULL)
2444 GIConv conv;
2445 conv = str_crt_conv_from (cp_id);
2446 if (conv != INVALID_CONV)
2448 if (edit->converter != str_cnv_from_term)
2449 str_close_conv (edit->converter);
2450 edit->converter = conv;
2454 if (cp_id != NULL)
2455 edit->utf8 = str_isutf8 (cp_id);
2456 #else
2457 (void) edit;
2458 #endif
2462 /* --------------------------------------------------------------------------------------------- */
2464 Recording stack for undo:
2465 The following is an implementation of a compressed stack. Identical
2466 pushes are recorded by a negative prefix indicating the number of times the
2467 same char was pushed. This saves space for repeated curs-left or curs-right
2468 delete etc.
2472 pushed: stored:
2476 b -3
2478 c --> -4
2484 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2485 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2486 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2487 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2488 position.
2490 The only way the cursor moves or the buffer is changed is through the routines:
2491 insert, backspace, insert_ahead, delete, and cursor_move.
2492 These record the reverse undo movements onto the stack each time they are
2493 called.
2495 Each key press results in a set of actions (insert; delete ...). So each time
2496 a key is pressed the current position of start_display is pushed as
2497 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2498 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2499 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2503 void
2504 edit_push_undo_action (WEdit * edit, long c, ...)
2506 unsigned long sp = edit->undo_stack_pointer;
2507 unsigned long spm1;
2508 long *t;
2510 /* first enlarge the stack if necessary */
2511 if (sp > edit->undo_stack_size - 10)
2512 { /* say */
2513 if (option_max_undo < 256)
2514 option_max_undo = 256;
2515 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2517 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2518 if (t)
2520 edit->undo_stack = t;
2521 edit->undo_stack_size <<= 1;
2522 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2526 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2527 if (edit->undo_stack_disable)
2529 edit_push_redo_action (edit, KEY_PRESS);
2530 edit_push_redo_action (edit, c);
2531 return;
2533 else if (edit->redo_stack_reset)
2535 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2538 if (edit->undo_stack_bottom != sp
2539 && spm1 != edit->undo_stack_bottom
2540 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2542 int d;
2543 if (edit->undo_stack[spm1] < 0)
2545 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2546 if (d == c)
2548 if (edit->undo_stack[spm1] > -1000000000)
2550 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2552 edit->undo_stack[spm1]--;
2554 return;
2558 else
2560 d = edit->undo_stack[spm1];
2561 if (d == c)
2563 if (c >= KEY_PRESS)
2564 return; /* --> no need to push multiple do-nothings */
2565 edit->undo_stack[sp] = -2;
2566 goto check_bottom;
2570 edit->undo_stack[sp] = c;
2572 check_bottom:
2573 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2575 /* if the sp wraps round and catches the undo_stack_bottom then erase
2576 * the first set of actions on the stack to make space - by moving
2577 * undo_stack_bottom forward one "key press" */
2578 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2579 if ((unsigned long) c == edit->undo_stack_bottom ||
2580 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2583 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2585 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2586 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2588 /*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: */
2589 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2590 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2592 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2596 void
2597 edit_push_redo_action (WEdit * edit, long c, ...)
2599 unsigned long sp = edit->redo_stack_pointer;
2600 unsigned long spm1;
2601 long *t;
2602 /* first enlarge the stack if necessary */
2603 if (sp > edit->redo_stack_size - 10)
2604 { /* say */
2605 if (option_max_undo < 256)
2606 option_max_undo = 256;
2607 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2609 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2610 if (t)
2612 edit->redo_stack = t;
2613 edit->redo_stack_size <<= 1;
2614 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2618 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2620 if (edit->redo_stack_bottom != sp
2621 && spm1 != edit->redo_stack_bottom
2622 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2624 int d;
2625 if (edit->redo_stack[spm1] < 0)
2627 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2628 if (d == c)
2630 if (edit->redo_stack[spm1] > -1000000000)
2632 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2633 edit->redo_stack[spm1]--;
2634 return;
2638 else
2640 d = edit->redo_stack[spm1];
2641 if (d == c)
2643 if (c >= KEY_PRESS)
2644 return; /* --> no need to push multiple do-nothings */
2645 edit->redo_stack[sp] = -2;
2646 goto redo_check_bottom;
2650 edit->redo_stack[sp] = c;
2652 redo_check_bottom:
2653 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2655 /* if the sp wraps round and catches the redo_stack_bottom then erase
2656 * the first set of actions on the stack to make space - by moving
2657 * redo_stack_bottom forward one "key press" */
2658 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2659 if ((unsigned long) c == edit->redo_stack_bottom ||
2660 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2663 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2665 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2666 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2669 * If a single key produced enough pushes to wrap all the way round then
2670 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2671 * The stack is then initialised:
2674 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2675 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2676 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2680 /* --------------------------------------------------------------------------------------------- */
2682 Basic low level single character buffer alterations and movements at the cursor.
2683 Returns char passed over, inserted or removed.
2686 void
2687 edit_insert (WEdit * edit, int c)
2689 /* check if file has grown to large */
2690 if (edit->last_byte >= SIZE_LIMIT)
2691 return;
2693 /* first we must update the position of the display window */
2694 if (edit->curs1 < edit->start_display)
2696 edit->start_display++;
2697 if (c == '\n')
2698 edit->start_line++;
2701 /* Mark file as modified, unless the file hasn't been fully loaded */
2702 if (edit->loading_done)
2704 edit_modification (edit);
2707 /* now we must update some info on the file and check if a redraw is required */
2708 if (c == '\n')
2710 if (edit->book_mark)
2711 book_mark_inc (edit, edit->curs_line);
2712 edit->curs_line++;
2713 edit->total_lines++;
2714 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2717 /* save the reverse command onto the undo stack */
2718 /* ordinary char and not space */
2719 if (c > 32)
2720 edit_push_undo_action (edit, BACKSPACE);
2721 else
2722 edit_push_undo_action (edit, BACKSPACE_BR);
2723 /* update markers */
2724 edit->mark1 += (edit->mark1 > edit->curs1);
2725 edit->mark2 += (edit->mark2 > edit->curs1);
2726 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2728 /* add a new buffer if we've reached the end of the last one */
2729 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2730 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2732 /* perform the insertion */
2733 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2734 = (unsigned char) c;
2736 /* update file length */
2737 edit->last_byte++;
2739 /* update cursor position */
2740 edit->curs1++;
2743 /* --------------------------------------------------------------------------------------------- */
2744 /** same as edit_insert and move left */
2746 void
2747 edit_insert_ahead (WEdit * edit, int c)
2749 if (edit->last_byte >= SIZE_LIMIT)
2750 return;
2752 if (edit->curs1 < edit->start_display)
2754 edit->start_display++;
2755 if (c == '\n')
2756 edit->start_line++;
2758 edit_modification (edit);
2759 if (c == '\n')
2761 if (edit->book_mark)
2762 book_mark_inc (edit, edit->curs_line);
2763 edit->total_lines++;
2764 edit->force |= REDRAW_AFTER_CURSOR;
2766 /* ordinary char and not space */
2767 if (c > 32)
2768 edit_push_undo_action (edit, DELCHAR);
2769 else
2770 edit_push_undo_action (edit, DELCHAR_BR);
2772 edit->mark1 += (edit->mark1 >= edit->curs1);
2773 edit->mark2 += (edit->mark2 >= edit->curs1);
2774 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2776 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2777 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2778 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2779 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2781 edit->last_byte++;
2782 edit->curs2++;
2786 /* --------------------------------------------------------------------------------------------- */
2789 edit_delete (WEdit * edit, const int byte_delete)
2791 int p = 0;
2792 int cw = 1;
2793 int i;
2795 if (!edit->curs2)
2796 return 0;
2798 cw = 1;
2799 /* if byte_delete = 1 then delete only one byte not multibyte char */
2800 if (edit->utf8 && byte_delete == 0)
2802 edit_get_utf (edit, edit->curs1, &cw);
2803 if (cw < 1)
2804 cw = 1;
2807 if (edit->mark2 != edit->mark1)
2808 edit_push_markers (edit);
2810 for (i = 1; i <= cw; i++)
2812 if (edit->mark1 > edit->curs1)
2814 edit->mark1--;
2815 edit->end_mark_curs--;
2817 if (edit->mark2 > edit->curs1)
2818 edit->mark2--;
2819 if (edit->last_get_rule > edit->curs1)
2820 edit->last_get_rule--;
2822 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2823 ((edit->curs2 -
2824 1) & M_EDIT_BUF_SIZE) - 1];
2826 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2828 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2829 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2831 edit->last_byte--;
2832 edit->curs2--;
2833 edit_push_undo_action (edit, p + 256);
2836 edit_modification (edit);
2837 if (p == '\n')
2839 if (edit->book_mark)
2840 book_mark_dec (edit, edit->curs_line);
2841 edit->total_lines--;
2842 edit->force |= REDRAW_AFTER_CURSOR;
2844 if (edit->curs1 < edit->start_display)
2846 edit->start_display--;
2847 if (p == '\n')
2848 edit->start_line--;
2851 return p;
2854 /* --------------------------------------------------------------------------------------------- */
2855 /** moves the cursor right or left: increment positive or negative respectively */
2857 void
2858 edit_cursor_move (WEdit * edit, long increment)
2860 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2861 int c;
2863 if (increment < 0)
2865 for (; increment < 0; increment++)
2867 if (!edit->curs1)
2868 return;
2870 edit_push_undo_action (edit, CURS_RIGHT);
2872 c = edit_get_byte (edit, edit->curs1 - 1);
2873 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2874 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2875 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2876 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2877 edit->curs2++;
2878 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2879 1) & M_EDIT_BUF_SIZE];
2880 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2882 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2883 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2885 edit->curs1--;
2886 if (c == '\n')
2888 edit->curs_line--;
2889 edit->force |= REDRAW_LINE_BELOW;
2894 else if (increment > 0)
2896 for (; increment > 0; increment--)
2898 if (!edit->curs2)
2899 return;
2901 edit_push_undo_action (edit, CURS_LEFT);
2903 c = edit_get_byte (edit, edit->curs1);
2904 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2905 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2906 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2907 edit->curs1++;
2908 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2909 ((edit->curs2 -
2910 1) & M_EDIT_BUF_SIZE) - 1];
2911 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2913 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2914 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2916 edit->curs2--;
2917 if (c == '\n')
2919 edit->curs_line++;
2920 edit->force |= REDRAW_LINE_ABOVE;
2926 /* These functions return positions relative to lines */
2928 /* --------------------------------------------------------------------------------------------- */
2929 /** returns index of last char on line + 1 */
2931 long
2932 edit_eol (WEdit * edit, long current)
2934 if (current >= edit->last_byte)
2935 return edit->last_byte;
2937 for (;; current++)
2938 if (edit_get_byte (edit, current) == '\n')
2939 break;
2940 return current;
2943 /* --------------------------------------------------------------------------------------------- */
2944 /** returns index of first char on line */
2946 long
2947 edit_bol (WEdit * edit, long current)
2949 if (current <= 0)
2950 return 0;
2952 for (;; current--)
2953 if (edit_get_byte (edit, current - 1) == '\n')
2954 break;
2955 return current;
2958 /* --------------------------------------------------------------------------------------------- */
2960 long
2961 edit_count_lines (WEdit * edit, long current, long upto)
2963 long lines = 0;
2964 if (upto > edit->last_byte)
2965 upto = edit->last_byte;
2966 if (current < 0)
2967 current = 0;
2968 while (current < upto)
2969 if (edit_get_byte (edit, current++) == '\n')
2970 lines++;
2971 return lines;
2974 /* --------------------------------------------------------------------------------------------- */
2975 /* If lines is zero this returns the count of lines from current to upto. */
2976 /* If upto is zero returns index of lines forward current. */
2978 long
2979 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2981 if (upto)
2983 return edit_count_lines (edit, current, upto);
2985 else
2987 long next;
2988 if (lines < 0)
2989 lines = 0;
2990 while (lines--)
2992 next = edit_eol (edit, current) + 1;
2993 if (next > edit->last_byte)
2994 break;
2995 else
2996 current = next;
2998 return current;
3002 /* --------------------------------------------------------------------------------------------- */
3003 /** Returns offset of 'lines' lines up from current */
3005 long
3006 edit_move_backward (WEdit * edit, long current, long lines)
3008 if (lines < 0)
3009 lines = 0;
3010 current = edit_bol (edit, current);
3011 while ((lines--) && current != 0)
3012 current = edit_bol (edit, current - 1);
3013 return current;
3016 /* --------------------------------------------------------------------------------------------- */
3017 /* If cols is zero this returns the count of columns from current to upto. */
3018 /* If upto is zero returns index of cols across from current. */
3020 long
3021 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3023 long p, q;
3024 int col;
3026 if (upto)
3028 q = upto;
3029 cols = -10;
3031 else
3032 q = edit->last_byte + 2;
3034 for (col = 0, p = current; p < q; p++)
3036 int c, orig_c;
3038 if (cols != -10)
3040 if (col == cols)
3041 return p;
3042 if (col > cols)
3043 return p - 1;
3046 orig_c = c = edit_get_byte (edit, p);
3048 #ifdef HAVE_CHARSET
3049 if (edit->utf8)
3051 int utf_ch;
3052 int cw = 1;
3054 utf_ch = edit_get_utf (edit, p, &cw);
3055 if (mc_global.utf8_display)
3057 if (cw > 1)
3058 col -= cw - 1;
3059 if (g_unichar_iswide (utf_ch))
3060 col++;
3062 else if (cw > 1 && g_unichar_isprint (utf_ch))
3063 col -= cw - 1;
3066 c = convert_to_display_c (c);
3067 #endif
3069 if (c == '\t')
3070 col += TAB_SIZE - col % TAB_SIZE;
3071 else if (c == '\n')
3073 if (upto)
3074 return col;
3075 else
3076 return p;
3078 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3079 /* '\r' is shown as ^M, so we must advance 2 characters */
3080 /* Caret notation for control characters */
3081 col += 2;
3082 else
3083 col++;
3085 return col;
3088 /* --------------------------------------------------------------------------------------------- */
3089 /** returns the current column position of the cursor */
3092 edit_get_col (WEdit * edit)
3094 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3097 /* --------------------------------------------------------------------------------------------- */
3098 /* Scrolling functions */
3099 /* --------------------------------------------------------------------------------------------- */
3101 void
3102 edit_update_curs_row (WEdit * edit)
3104 edit->curs_row = edit->curs_line - edit->start_line;
3107 /* --------------------------------------------------------------------------------------------- */
3109 void
3110 edit_update_curs_col (WEdit * edit)
3112 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3115 /* --------------------------------------------------------------------------------------------- */
3118 edit_get_curs_col (const WEdit * edit)
3120 return edit->curs_col;
3123 /* --------------------------------------------------------------------------------------------- */
3124 /** moves the display start position up by i lines */
3126 void
3127 edit_scroll_upward (WEdit * edit, unsigned long i)
3129 unsigned long lines_above = edit->start_line;
3130 if (i > lines_above)
3131 i = lines_above;
3132 if (i)
3134 edit->start_line -= i;
3135 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3136 edit->force |= REDRAW_PAGE;
3137 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3139 edit_update_curs_row (edit);
3143 /* --------------------------------------------------------------------------------------------- */
3144 /** returns 1 if could scroll, 0 otherwise */
3146 void
3147 edit_scroll_downward (WEdit * edit, int i)
3149 int lines_below;
3150 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3151 if (lines_below > 0)
3153 if (i > lines_below)
3154 i = lines_below;
3155 edit->start_line += i;
3156 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3157 edit->force |= REDRAW_PAGE;
3158 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3160 edit_update_curs_row (edit);
3163 /* --------------------------------------------------------------------------------------------- */
3165 void
3166 edit_scroll_right (WEdit * edit, int i)
3168 edit->force |= REDRAW_PAGE;
3169 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3170 edit->start_col -= i;
3173 /* --------------------------------------------------------------------------------------------- */
3175 void
3176 edit_scroll_left (WEdit * edit, int i)
3178 if (edit->start_col)
3180 edit->start_col += i;
3181 if (edit->start_col > 0)
3182 edit->start_col = 0;
3183 edit->force |= REDRAW_PAGE;
3184 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3188 /* --------------------------------------------------------------------------------------------- */
3189 /* high level cursor movement commands */
3190 /* --------------------------------------------------------------------------------------------- */
3192 void
3193 edit_move_to_prev_col (WEdit * edit, long p)
3195 int prev = edit->prev_col;
3196 int over = edit->over_col;
3197 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3199 if (option_cursor_beyond_eol)
3201 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3202 edit_eol (edit, edit->curs1));
3204 if (line_len < prev + edit->over_col)
3206 edit->over_col = prev + over - line_len;
3207 edit->prev_col = line_len;
3208 edit->curs_col = line_len;
3210 else
3212 edit->curs_col = prev + over;
3213 edit->prev_col = edit->curs_col;
3214 edit->over_col = 0;
3217 else
3219 edit->over_col = 0;
3220 if (is_in_indent (edit) && option_fake_half_tabs)
3222 edit_update_curs_col (edit);
3223 if (space_width)
3224 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3226 int q = edit->curs_col;
3227 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3228 p = edit_bol (edit, edit->curs1);
3229 edit_cursor_move (edit,
3230 edit_move_forward3 (edit, p, edit->curs_col,
3231 0) - edit->curs1);
3232 if (!left_of_four_spaces (edit))
3233 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3239 /* --------------------------------------------------------------------------------------------- */
3240 /** check whether line in editor is blank or not
3242 * @param edit editor object
3243 * @param line number of line
3245 * @return TRUE if line in blank, FALSE otherwise
3248 gboolean
3249 line_is_blank (WEdit * edit, long line)
3251 return is_blank (edit, edit_find_line (edit, line));
3254 /* --------------------------------------------------------------------------------------------- */
3255 /** move cursor to line 'line' */
3257 void
3258 edit_move_to_line (WEdit * e, long line)
3260 if (line < e->curs_line)
3261 edit_move_up (e, e->curs_line - line, 0);
3262 else
3263 edit_move_down (e, line - e->curs_line, 0);
3264 edit_scroll_screen_over_cursor (e);
3267 /* --------------------------------------------------------------------------------------------- */
3268 /** scroll window so that first visible line is 'line' */
3270 void
3271 edit_move_display (WEdit * e, long line)
3273 if (line < e->start_line)
3274 edit_scroll_upward (e, e->start_line - line);
3275 else
3276 edit_scroll_downward (e, line - e->start_line);
3279 /* --------------------------------------------------------------------------------------------- */
3280 /** save markers onto undo stack */
3282 void
3283 edit_push_markers (WEdit * edit)
3285 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3286 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3287 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3290 /* --------------------------------------------------------------------------------------------- */
3292 void
3293 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3295 edit->mark1 = m1;
3296 edit->mark2 = m2;
3297 edit->column1 = c1;
3298 edit->column2 = c2;
3302 /* --------------------------------------------------------------------------------------------- */
3303 /** highlight marker toggle */
3305 void
3306 edit_mark_cmd (WEdit * edit, int unmark)
3308 edit_push_markers (edit);
3309 if (unmark)
3311 edit_set_markers (edit, 0, 0, 0, 0);
3312 edit->force |= REDRAW_PAGE;
3314 else
3316 if (edit->mark2 >= 0)
3318 edit->end_mark_curs = -1;
3319 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3320 edit->curs_col + edit->over_col);
3321 edit->force |= REDRAW_PAGE;
3323 else
3325 edit->end_mark_curs = edit->curs1;
3326 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3327 edit->curs_col + edit->over_col);
3332 /* --------------------------------------------------------------------------------------------- */
3333 /** highlight the word under cursor */
3335 void
3336 edit_mark_current_word_cmd (WEdit * edit)
3338 long pos;
3340 for (pos = edit->curs1; pos != 0; pos--)
3342 int c1, c2;
3344 c1 = edit_get_byte (edit, pos);
3345 c2 = edit_get_byte (edit, pos - 1);
3346 if (!isspace (c1) && isspace (c2))
3347 break;
3348 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3349 break;
3351 edit->mark1 = pos;
3353 for (; pos < edit->last_byte; pos++)
3355 int c1, c2;
3357 c1 = edit_get_byte (edit, pos);
3358 c2 = edit_get_byte (edit, pos + 1);
3359 if (!isspace (c1) && isspace (c2))
3360 break;
3361 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3362 break;
3364 edit->mark2 = min (pos + 1, edit->last_byte);
3366 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3369 /* --------------------------------------------------------------------------------------------- */
3371 void
3372 edit_mark_current_line_cmd (WEdit * edit)
3374 long pos = edit->curs1;
3376 edit->mark1 = edit_bol (edit, pos);
3377 edit->mark2 = edit_eol (edit, pos);
3379 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3382 /* --------------------------------------------------------------------------------------------- */
3384 void
3385 edit_delete_line (WEdit * edit)
3388 * Delete right part of the line.
3389 * Note that edit_get_byte() returns '\n' when byte position is
3390 * beyond EOF.
3392 while (edit_get_byte (edit, edit->curs1) != '\n')
3394 (void) edit_delete (edit, 1);
3398 * Delete '\n' char.
3399 * Note that edit_delete() will not corrupt anything if called while
3400 * cursor position is EOF.
3402 (void) edit_delete (edit, 1);
3405 * Delete left part of the line.
3406 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3408 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3410 (void) edit_backspace (edit, 1);
3414 /* --------------------------------------------------------------------------------------------- */
3417 edit_indent_width (WEdit * edit, long p)
3419 long q = p;
3420 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3421 q++;
3422 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3425 /* --------------------------------------------------------------------------------------------- */
3427 void
3428 edit_insert_indent (WEdit * edit, int indent)
3430 if (!option_fill_tabs_with_spaces)
3432 while (indent >= TAB_SIZE)
3434 edit_insert (edit, '\t');
3435 indent -= TAB_SIZE;
3438 while (indent-- > 0)
3439 edit_insert (edit, ' ');
3442 /* --------------------------------------------------------------------------------------------- */
3444 void
3445 edit_push_key_press (WEdit * edit)
3447 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3448 if (edit->mark2 == -1)
3450 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3451 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3455 /* --------------------------------------------------------------------------------------------- */
3457 void
3458 edit_find_bracket (WEdit * edit)
3460 edit->bracket = edit_get_bracket (edit, 1, 10000);
3461 if (last_bracket != edit->bracket)
3462 edit->force |= REDRAW_PAGE;
3463 last_bracket = edit->bracket;
3466 /* --------------------------------------------------------------------------------------------- */
3468 * This executes a command as though the user initiated it through a key
3469 * press. Callback with WIDGET_KEY as a message calls this after
3470 * translating the key press. This function can be used to pass any
3471 * command to the editor. Note that the screen wouldn't update
3472 * automatically. Either of command or char_for_insertion must be
3473 * passed as -1. Commands are executed, and char_for_insertion is
3474 * inserted at the cursor.
3477 void
3478 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3480 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3481 || (macro_index < 0
3482 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3484 macro_index = 0;
3485 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3486 return;
3488 if (macro_index != -1)
3490 edit->force |= REDRAW_COMPLETELY;
3491 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3493 edit_store_macro_cmd (edit);
3494 macro_index = -1;
3495 return;
3497 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3499 edit_repeat_macro_cmd (edit);
3500 macro_index = -1;
3501 return;
3505 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3507 record_macro_buf[macro_index].action = command;
3508 record_macro_buf[macro_index++].ch = char_for_insertion;
3510 /* record the beginning of a set of editing actions initiated by a key press */
3511 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3512 edit_push_key_press (edit);
3514 edit_execute_cmd (edit, command, char_for_insertion);
3515 if (edit->column_highlight)
3516 edit->force |= REDRAW_PAGE;
3519 /* --------------------------------------------------------------------------------------------- */
3521 This executes a command at a lower level than macro recording.
3522 It also does not push a key_press onto the undo stack. This means
3523 that if it is called many times, a single undo command will undo
3524 all of them. It also does not check for the Undo command.
3526 void
3527 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3529 edit->force |= REDRAW_LINE;
3531 /* The next key press will unhighlight the found string, so update
3532 * the whole page */
3533 if (edit->found_len || edit->column_highlight)
3534 edit->force |= REDRAW_PAGE;
3536 switch (command)
3538 /* a mark command with shift-arrow */
3539 case CK_MarkLeft:
3540 case CK_MarkRight:
3541 case CK_MarkToWordBegin:
3542 case CK_MarkToWordEnd:
3543 case CK_MarkToHome:
3544 case CK_MarkToEnd:
3545 case CK_MarkUp:
3546 case CK_MarkDown:
3547 case CK_MarkPageUp:
3548 case CK_MarkPageDown:
3549 case CK_MarkToFileBegin:
3550 case CK_MarkToFileEnd:
3551 case CK_MarkToPageBegin:
3552 case CK_MarkToPageEnd:
3553 case CK_MarkScrollUp:
3554 case CK_MarkScrollDown:
3555 case CK_MarkParagraphUp:
3556 case CK_MarkParagraphDown:
3557 /* a mark command with alt-arrow */
3558 case CK_MarkColumnPageUp:
3559 case CK_MarkColumnPageDown:
3560 case CK_MarkColumnLeft:
3561 case CK_MarkColumnRight:
3562 case CK_MarkColumnUp:
3563 case CK_MarkColumnDown:
3564 case CK_MarkColumnScrollUp:
3565 case CK_MarkColumnScrollDown:
3566 case CK_MarkColumnParagraphUp:
3567 case CK_MarkColumnParagraphDown:
3568 edit->column_highlight = 0;
3569 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3571 edit_mark_cmd (edit, 1); /* clear */
3572 edit_mark_cmd (edit, 0); /* marking on */
3574 edit->highlight = 1;
3575 break;
3577 /* any other command */
3578 default:
3579 if (edit->highlight)
3580 edit_mark_cmd (edit, 0); /* clear */
3581 edit->highlight = 0;
3584 /* first check for undo */
3585 if (command == CK_Undo)
3587 edit->redo_stack_reset = 0;
3588 edit_group_undo (edit);
3589 edit->found_len = 0;
3590 edit->prev_col = edit_get_col (edit);
3591 edit->search_start = edit->curs1;
3592 return;
3594 /* check for redo */
3595 if (command == CK_Redo)
3597 edit->redo_stack_reset = 0;
3598 edit_do_redo (edit);
3599 edit->found_len = 0;
3600 edit->prev_col = edit_get_col (edit);
3601 edit->search_start = edit->curs1;
3602 return;
3605 edit->redo_stack_reset = 1;
3607 /* An ordinary key press */
3608 if (char_for_insertion >= 0)
3610 /* if non persistent selection and text selected */
3611 if (!option_persistent_selections)
3613 if (edit->mark1 != edit->mark2)
3614 edit_block_delete_cmd (edit);
3616 if (edit->overwrite)
3618 /* remove char only one time, after input first byte, multibyte chars */
3619 if ((!mc_global.utf8_display || edit->charpoint == 0)
3620 && edit_get_byte (edit, edit->curs1) != '\n')
3621 edit_delete (edit, 0);
3623 if (option_cursor_beyond_eol && edit->over_col > 0)
3624 edit_insert_over (edit);
3625 #ifdef HAVE_CHARSET
3626 if (char_for_insertion > 255 && !mc_global.utf8_display)
3628 unsigned char str[6 + 1];
3629 size_t i = 0;
3630 int res;
3632 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3633 if (res == 0)
3635 str[0] = '.';
3636 str[1] = '\0';
3638 else
3640 str[res] = '\0';
3642 while (str[i] != 0 && i <= 6)
3644 char_for_insertion = str[i];
3645 edit_insert (edit, char_for_insertion);
3646 i++;
3649 else
3650 #endif
3651 edit_insert (edit, char_for_insertion);
3653 if (option_auto_para_formatting)
3655 format_paragraph (edit, 0);
3656 edit->force |= REDRAW_PAGE;
3658 else
3659 check_and_wrap_line (edit);
3660 edit->found_len = 0;
3661 edit->prev_col = edit_get_col (edit);
3662 edit->search_start = edit->curs1;
3663 edit_find_bracket (edit);
3664 return;
3667 switch (command)
3669 case CK_TopOnScreen:
3670 case CK_BottomOnScreen:
3671 case CK_Top:
3672 case CK_Bottom:
3673 case CK_PageUp:
3674 case CK_PageDown:
3675 case CK_Home:
3676 case CK_End:
3677 case CK_Up:
3678 case CK_Down:
3679 case CK_Left:
3680 case CK_Right:
3681 case CK_WordLeft:
3682 case CK_WordRight:
3683 if (edit->mark2 >= 0)
3685 if (!option_persistent_selections)
3687 if (edit->column_highlight)
3688 edit_push_undo_action (edit, COLUMN_ON);
3689 edit->column_highlight = 0;
3690 edit_mark_cmd (edit, 1);
3695 switch (command)
3697 case CK_TopOnScreen:
3698 case CK_BottomOnScreen:
3699 case CK_MarkToPageBegin:
3700 case CK_MarkToPageEnd:
3701 case CK_Up:
3702 case CK_Down:
3703 case CK_WordLeft:
3704 case CK_WordRight:
3705 case CK_MarkToWordBegin:
3706 case CK_MarkToWordEnd:
3707 case CK_MarkUp:
3708 case CK_MarkDown:
3709 case CK_MarkColumnUp:
3710 case CK_MarkColumnDown:
3711 if (edit->mark2 == -1)
3712 break; /*marking is following the cursor: may need to highlight a whole line */
3713 case CK_Left:
3714 case CK_Right:
3715 case CK_MarkLeft:
3716 case CK_MarkRight:
3717 edit->force |= REDRAW_CHAR_ONLY;
3720 /* basic cursor key commands */
3721 switch (command)
3723 case CK_BackSpace:
3724 /* if non persistent selection and text selected */
3725 if (!option_persistent_selections)
3727 if (edit->mark1 != edit->mark2)
3729 edit_block_delete_cmd (edit);
3730 break;
3733 if (option_cursor_beyond_eol && edit->over_col > 0)
3735 edit->over_col--;
3736 break;
3738 if (option_backspace_through_tabs && is_in_indent (edit))
3740 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3741 edit_backspace (edit, 1);
3742 break;
3744 else
3746 if (option_fake_half_tabs)
3748 int i;
3749 if (is_in_indent (edit) && right_of_four_spaces (edit))
3751 for (i = 0; i < HALF_TAB_SIZE; i++)
3752 edit_backspace (edit, 1);
3753 break;
3757 edit_backspace (edit, 0);
3758 break;
3759 case CK_Delete:
3760 /* if non persistent selection and text selected */
3761 if (!option_persistent_selections)
3763 if (edit->mark1 != edit->mark2)
3765 edit_block_delete_cmd (edit);
3766 break;
3770 if (option_cursor_beyond_eol && edit->over_col > 0)
3771 edit_insert_over (edit);
3773 if (option_fake_half_tabs)
3775 int i;
3776 if (is_in_indent (edit) && left_of_four_spaces (edit))
3778 for (i = 1; i <= HALF_TAB_SIZE; i++)
3779 edit_delete (edit, 1);
3780 break;
3783 edit_delete (edit, 0);
3784 break;
3785 case CK_DeleteToWordBegin:
3786 edit->over_col = 0;
3787 edit_left_delete_word (edit);
3788 break;
3789 case CK_DeleteToWordEnd:
3790 if (option_cursor_beyond_eol && edit->over_col > 0)
3791 edit_insert_over (edit);
3793 edit_right_delete_word (edit);
3794 break;
3795 case CK_DeleteLine:
3796 edit_delete_line (edit);
3797 break;
3798 case CK_DeleteToHome:
3799 edit_delete_to_line_begin (edit);
3800 break;
3801 case CK_DeleteToEnd:
3802 edit_delete_to_line_end (edit);
3803 break;
3804 case CK_Enter:
3805 edit->over_col = 0;
3806 if (option_auto_para_formatting)
3808 edit_double_newline (edit);
3809 if (option_return_does_auto_indent)
3810 edit_auto_indent (edit);
3811 format_paragraph (edit, 0);
3813 else
3815 edit_insert (edit, '\n');
3816 if (option_return_does_auto_indent)
3818 edit_auto_indent (edit);
3821 break;
3822 case CK_Return:
3823 edit_insert (edit, '\n');
3824 break;
3826 case CK_MarkColumnPageUp:
3827 edit->column_highlight = 1;
3828 case CK_PageUp:
3829 case CK_MarkPageUp:
3830 edit_move_up (edit, edit->widget.lines - 1, 1);
3831 break;
3832 case CK_MarkColumnPageDown:
3833 edit->column_highlight = 1;
3834 case CK_PageDown:
3835 case CK_MarkPageDown:
3836 edit_move_down (edit, edit->widget.lines - 1, 1);
3837 break;
3838 case CK_MarkColumnLeft:
3839 edit->column_highlight = 1;
3840 case CK_Left:
3841 case CK_MarkLeft:
3842 if (option_fake_half_tabs)
3844 if (is_in_indent (edit) && right_of_four_spaces (edit))
3846 if (option_cursor_beyond_eol && edit->over_col > 0)
3847 edit->over_col--;
3848 else
3849 edit_cursor_move (edit, -HALF_TAB_SIZE);
3850 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3851 break;
3854 edit_left_char_move_cmd (edit);
3855 break;
3856 case CK_MarkColumnRight:
3857 edit->column_highlight = 1;
3858 case CK_Right:
3859 case CK_MarkRight:
3860 if (option_fake_half_tabs)
3862 if (is_in_indent (edit) && left_of_four_spaces (edit))
3864 edit_cursor_move (edit, HALF_TAB_SIZE);
3865 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3866 break;
3869 edit_right_char_move_cmd (edit);
3870 break;
3871 case CK_TopOnScreen:
3872 case CK_MarkToPageBegin:
3873 edit_begin_page (edit);
3874 break;
3875 case CK_BottomOnScreen:
3876 case CK_MarkToPageEnd:
3877 edit_end_page (edit);
3878 break;
3879 case CK_WordLeft:
3880 case CK_MarkToWordBegin:
3881 edit->over_col = 0;
3882 edit_left_word_move_cmd (edit);
3883 break;
3884 case CK_WordRight:
3885 case CK_MarkToWordEnd:
3886 edit->over_col = 0;
3887 edit_right_word_move_cmd (edit);
3888 break;
3889 case CK_MarkColumnUp:
3890 edit->column_highlight = 1;
3891 case CK_Up:
3892 case CK_MarkUp:
3893 edit_move_up (edit, 1, 0);
3894 break;
3895 case CK_MarkColumnDown:
3896 edit->column_highlight = 1;
3897 case CK_Down:
3898 case CK_MarkDown:
3899 edit_move_down (edit, 1, 0);
3900 break;
3901 case CK_MarkColumnParagraphUp:
3902 edit->column_highlight = 1;
3903 case CK_ParagraphUp:
3904 case CK_MarkParagraphUp:
3905 edit_move_up_paragraph (edit, 0);
3906 break;
3907 case CK_MarkColumnParagraphDown:
3908 edit->column_highlight = 1;
3909 case CK_ParagraphDown:
3910 case CK_MarkParagraphDown:
3911 edit_move_down_paragraph (edit, 0);
3912 break;
3913 case CK_MarkColumnScrollUp:
3914 edit->column_highlight = 1;
3915 case CK_ScrollUp:
3916 case CK_MarkScrollUp:
3917 edit_move_up (edit, 1, 1);
3918 break;
3919 case CK_MarkColumnScrollDown:
3920 edit->column_highlight = 1;
3921 case CK_ScrollDown:
3922 case CK_MarkScrollDown:
3923 edit_move_down (edit, 1, 1);
3924 break;
3925 case CK_Home:
3926 case CK_MarkToHome:
3927 edit_cursor_to_bol (edit);
3928 break;
3929 case CK_End:
3930 case CK_MarkToEnd:
3931 edit_cursor_to_eol (edit);
3932 break;
3933 case CK_Tab:
3934 /* if text marked shift block */
3935 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3937 if (edit->mark2 < 0)
3938 edit_mark_cmd (edit, 0);
3939 edit_move_block_to_right (edit);
3941 else
3943 if (option_cursor_beyond_eol)
3944 edit_insert_over (edit);
3945 edit_tab_cmd (edit);
3946 if (option_auto_para_formatting)
3948 format_paragraph (edit, 0);
3949 edit->force |= REDRAW_PAGE;
3951 else
3953 check_and_wrap_line (edit);
3956 break;
3958 case CK_InsertOverwrite:
3959 edit->overwrite = !edit->overwrite;
3960 break;
3962 case CK_Mark:
3963 if (edit->mark2 >= 0)
3965 if (edit->column_highlight)
3966 edit_push_undo_action (edit, COLUMN_ON);
3967 edit->column_highlight = 0;
3969 edit_mark_cmd (edit, 0);
3970 break;
3971 case CK_MarkColumn:
3972 if (!edit->column_highlight)
3973 edit_push_undo_action (edit, COLUMN_OFF);
3974 edit->column_highlight = 1;
3975 edit_mark_cmd (edit, 0);
3976 break;
3977 case CK_MarkAll:
3978 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3979 edit->force |= REDRAW_PAGE;
3980 break;
3981 case CK_Unmark:
3982 if (edit->column_highlight)
3983 edit_push_undo_action (edit, COLUMN_ON);
3984 edit->column_highlight = 0;
3985 edit_mark_cmd (edit, 1);
3986 break;
3987 case CK_MarkWord:
3988 if (edit->column_highlight)
3989 edit_push_undo_action (edit, COLUMN_ON);
3990 edit->column_highlight = 0;
3991 edit_mark_current_word_cmd (edit);
3992 break;
3993 case CK_MarkLine:
3994 if (edit->column_highlight)
3995 edit_push_undo_action (edit, COLUMN_ON);
3996 edit->column_highlight = 0;
3997 edit_mark_current_line_cmd (edit);
3998 break;
4000 case CK_ShowNumbers:
4001 option_line_state = !option_line_state;
4002 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
4003 edit->force |= REDRAW_PAGE;
4004 break;
4006 case CK_ShowMargin:
4007 show_right_margin = !show_right_margin;
4008 edit->force |= REDRAW_PAGE;
4009 break;
4011 case CK_Bookmark:
4012 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4013 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4014 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4015 else
4016 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4017 break;
4018 case CK_BookmarkFlush:
4019 book_mark_flush (edit, BOOK_MARK_COLOR);
4020 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4021 edit->force |= REDRAW_PAGE;
4022 break;
4023 case CK_BookmarkNext:
4024 if (edit->book_mark)
4026 struct _book_mark *p;
4027 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4028 if (p->next)
4030 p = p->next;
4031 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4032 edit_move_display (edit, p->line - edit->widget.lines / 2);
4033 edit_move_to_line (edit, p->line);
4036 break;
4037 case CK_BookmarkPrev:
4038 if (edit->book_mark)
4040 struct _book_mark *p;
4041 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4042 while (p->line == edit->curs_line)
4043 if (p->prev)
4044 p = p->prev;
4045 if (p->line >= 0)
4047 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4048 edit_move_display (edit, p->line - edit->widget.lines / 2);
4049 edit_move_to_line (edit, p->line);
4052 break;
4054 case CK_Top:
4055 case CK_MarkToFileBegin:
4056 edit_move_to_top (edit);
4057 break;
4058 case CK_Bottom:
4059 case CK_MarkToFileEnd:
4060 edit_move_to_bottom (edit);
4061 break;
4063 case CK_Copy:
4064 if (option_cursor_beyond_eol && edit->over_col > 0)
4065 edit_insert_over (edit);
4066 edit_block_copy_cmd (edit);
4067 break;
4068 case CK_Remove:
4069 edit_block_delete_cmd (edit);
4070 break;
4071 case CK_Move:
4072 edit_block_move_cmd (edit);
4073 break;
4075 case CK_BlockShiftLeft:
4076 if (edit->mark1 != edit->mark2)
4077 edit_move_block_to_left (edit);
4078 break;
4079 case CK_BlockShiftRight:
4080 if (edit->mark1 != edit->mark2)
4081 edit_move_block_to_right (edit);
4082 break;
4083 case CK_Store:
4084 edit_copy_to_X_buf_cmd (edit);
4085 break;
4086 case CK_Cut:
4087 edit_cut_to_X_buf_cmd (edit);
4088 break;
4089 case CK_Paste:
4090 /* if non persistent selection and text selected */
4091 if (!option_persistent_selections)
4093 if (edit->mark1 != edit->mark2)
4094 edit_block_delete_cmd (edit);
4096 if (option_cursor_beyond_eol && edit->over_col > 0)
4097 edit_insert_over (edit);
4098 edit_paste_from_X_buf_cmd (edit);
4099 break;
4100 case CK_History:
4101 edit_paste_from_history (edit);
4102 break;
4104 case CK_SaveAs:
4105 edit_save_as_cmd (edit);
4106 break;
4107 case CK_Save:
4108 edit_save_confirm_cmd (edit);
4109 break;
4110 case CK_EditFile:
4111 edit_load_cmd (edit, EDIT_FILE_COMMON);
4112 break;
4113 case CK_BlockSave:
4114 edit_save_block_cmd (edit);
4115 break;
4116 case CK_InsertFile:
4117 edit_insert_file_cmd (edit);
4118 break;
4120 case CK_FilePrev:
4121 edit_load_back_cmd (edit);
4122 break;
4123 case CK_FileNext:
4124 edit_load_forward_cmd (edit);
4125 break;
4127 case CK_EditSyntaxFile:
4128 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4129 break;
4130 case CK_SyntaxChoose:
4131 edit_syntax_dialog (edit);
4132 break;
4134 case CK_EditUserMenu:
4135 edit_load_cmd (edit, EDIT_FILE_MENU);
4136 break;
4138 case CK_SyntaxOnOff:
4139 option_syntax_highlighting ^= 1;
4140 if (option_syntax_highlighting == 1)
4141 edit_load_syntax (edit, NULL, edit->syntax_type);
4142 edit->force |= REDRAW_PAGE;
4143 break;
4145 case CK_ShowTabTws:
4146 enable_show_tabs_tws ^= 1;
4147 edit->force |= REDRAW_PAGE;
4148 break;
4150 case CK_Search:
4151 edit_search_cmd (edit, FALSE);
4152 break;
4153 case CK_SearchContinue:
4154 edit_search_cmd (edit, TRUE);
4155 break;
4156 case CK_Replace:
4157 edit_replace_cmd (edit, 0);
4158 break;
4159 case CK_ReplaceContinue:
4160 edit_replace_cmd (edit, 1);
4161 break;
4162 case CK_Complete:
4163 /* if text marked shift block */
4164 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4166 edit_move_block_to_left (edit);
4168 else
4170 edit_complete_word_cmd (edit);
4172 break;
4173 case CK_Find:
4174 edit_get_match_keyword_cmd (edit);
4175 break;
4176 case CK_Quit:
4177 dlg_stop (edit->widget.owner);
4178 break;
4179 case CK_EditNew:
4180 edit_new_cmd (edit);
4181 break;
4182 case CK_Help:
4183 edit_help_cmd (edit);
4184 break;
4185 case CK_Refresh:
4186 edit_refresh_cmd (edit);
4187 break;
4188 case CK_SaveSetup:
4189 save_setup_cmd ();
4190 break;
4191 case CK_About:
4192 edit_about ();
4193 break;
4194 case CK_LearnKeys:
4195 learn_keys ();
4196 break;
4197 case CK_Options:
4198 edit_options_dialog (edit);
4199 break;
4200 case CK_OptionsSaveMode:
4201 menu_save_mode_cmd ();
4202 break;
4203 case CK_Date:
4205 char s[BUF_MEDIUM];
4206 /* fool gcc to prevent a Y2K warning */
4207 char time_format[] = "_c";
4208 time_format[0] = '%';
4210 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4211 edit_print_string (edit, s);
4212 edit->force |= REDRAW_PAGE;
4213 break;
4215 break;
4216 case CK_Goto:
4217 edit_goto_cmd (edit);
4218 break;
4219 case CK_ParagraphFormat:
4220 format_paragraph (edit, 1);
4221 edit->force |= REDRAW_PAGE;
4222 break;
4223 case CK_MacroDelete:
4224 edit_delete_macro_cmd (edit);
4225 break;
4226 case CK_MatchBracket:
4227 edit_goto_matching_bracket (edit);
4228 break;
4229 case CK_UserMenu:
4230 user_menu (edit, NULL, -1);
4231 break;
4232 case CK_Sort:
4233 edit_sort_cmd (edit);
4234 break;
4235 case CK_ExternalCommand:
4236 edit_ext_cmd (edit);
4237 break;
4238 case CK_Mail:
4239 edit_mail_dialog (edit);
4240 break;
4241 case CK_Shell:
4242 view_other_cmd ();
4243 break;
4244 #ifdef HAVE_CHARSET
4245 case CK_SelectCodepage:
4246 edit_select_codepage_cmd (edit);
4247 break;
4248 #endif
4249 case CK_InsertLiteral:
4250 edit_insert_literal_cmd (edit);
4251 break;
4252 case CK_MacroStartStopRecord:
4253 edit_begin_end_macro_cmd (edit);
4254 break;
4255 case CK_RepeatStartStopRecord:
4256 edit_begin_end_repeat_cmd (edit);
4257 break;
4258 case CK_ExtendedKeyMap:
4259 edit->extmod = TRUE;
4260 break;
4261 default:
4262 break;
4265 /* CK_PipeBlock */
4266 if ((command / CK_PipeBlock (0)) == 1)
4267 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4269 /* keys which must set the col position, and the search vars */
4270 switch (command)
4272 case CK_Search:
4273 case CK_SearchContinue:
4274 case CK_Replace:
4275 case CK_ReplaceContinue:
4276 case CK_Complete:
4277 edit->prev_col = edit_get_col (edit);
4278 break;
4279 case CK_Up:
4280 case CK_MarkUp:
4281 case CK_MarkColumnUp:
4282 case CK_Down:
4283 case CK_MarkDown:
4284 case CK_MarkColumnDown:
4285 case CK_PageUp:
4286 case CK_MarkPageUp:
4287 case CK_MarkColumnPageUp:
4288 case CK_PageDown:
4289 case CK_MarkPageDown:
4290 case CK_MarkColumnPageDown:
4291 case CK_Top:
4292 case CK_MarkToFileBegin:
4293 case CK_Bottom:
4294 case CK_MarkToFileEnd:
4295 case CK_ParagraphUp:
4296 case CK_MarkParagraphUp:
4297 case CK_MarkColumnParagraphUp:
4298 case CK_ParagraphDown:
4299 case CK_MarkParagraphDown:
4300 case CK_MarkColumnParagraphDown:
4301 case CK_ScrollUp:
4302 case CK_MarkScrollUp:
4303 case CK_MarkColumnScrollUp:
4304 case CK_ScrollDown:
4305 case CK_MarkScrollDown:
4306 case CK_MarkColumnScrollDown:
4307 edit->search_start = edit->curs1;
4308 edit->found_len = 0;
4309 break;
4310 default:
4311 edit->found_len = 0;
4312 edit->prev_col = edit_get_col (edit);
4313 edit->search_start = edit->curs1;
4315 edit_find_bracket (edit);
4317 if (option_auto_para_formatting)
4319 switch (command)
4321 case CK_BackSpace:
4322 case CK_Delete:
4323 case CK_DeleteToWordBegin:
4324 case CK_DeleteToWordEnd:
4325 case CK_DeleteToHome:
4326 case CK_DeleteToEnd:
4327 format_paragraph (edit, 0);
4328 edit->force |= REDRAW_PAGE;
4333 /* --------------------------------------------------------------------------------------------- */
4335 void
4336 edit_stack_init (void)
4338 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4340 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4341 edit_history_moveto[edit_stack_iterator].line = -1;
4344 edit_stack_iterator = 0;
4347 /* --------------------------------------------------------------------------------------------- */
4349 void
4350 edit_stack_free (void)
4352 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4353 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4356 /* --------------------------------------------------------------------------------------------- */
4357 /** move i lines */
4359 void
4360 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4362 edit_move_updown (edit, i, do_scroll, TRUE);
4365 /* --------------------------------------------------------------------------------------------- */
4366 /** move i lines */
4368 void
4369 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4371 edit_move_updown (edit, i, do_scroll, FALSE);
4374 /* --------------------------------------------------------------------------------------------- */