Minor optimization and type accuracy of some editor functions.
[midnight-commander.git] / src / editor / edit.c
blob2d5ed34306fdc348274a746540f65de64e30a249
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 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 /* --------------------------------------------------------------------------------------------- */
850 static int
851 is_blank (WEdit * edit, long offset)
853 long s, f;
854 int c;
855 s = edit_bol (edit, offset);
856 f = edit_eol (edit, offset) - 1;
857 while (s <= f)
859 c = edit_get_byte (edit, s++);
860 if (!isspace (c))
861 return 0;
863 return 1;
867 /* --------------------------------------------------------------------------------------------- */
868 /** returns the offset of line i */
870 static long
871 edit_find_line (WEdit * edit, int line)
873 int i, j = 0;
874 int m = 2000000000;
875 if (!edit->caches_valid)
877 for (i = 0; i < N_LINE_CACHES; i++)
878 edit->line_numbers[i] = edit->line_offsets[i] = 0;
879 /* three offsets that we *know* are line 0 at 0 and these two: */
880 edit->line_numbers[1] = edit->curs_line;
881 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
882 edit->line_numbers[2] = edit->total_lines;
883 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
884 edit->caches_valid = 1;
886 if (line >= edit->total_lines)
887 return edit->line_offsets[2];
888 if (line <= 0)
889 return 0;
890 /* find the closest known point */
891 for (i = 0; i < N_LINE_CACHES; i++)
893 int n;
894 n = abs (edit->line_numbers[i] - line);
895 if (n < m)
897 m = n;
898 j = i;
901 if (m == 0)
902 return edit->line_offsets[j]; /* know the offset exactly */
903 if (m == 1 && j >= 3)
904 i = j; /* one line different - caller might be looping, so stay in this cache */
905 else
906 i = 3 + (rand () % (N_LINE_CACHES - 3));
907 if (line > edit->line_numbers[j])
908 edit->line_offsets[i] =
909 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
910 else
911 edit->line_offsets[i] =
912 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
913 edit->line_numbers[i] = line;
914 return edit->line_offsets[i];
917 /* --------------------------------------------------------------------------------------------- */
918 /** moves up until a blank line is reached, or until just
919 before a non-blank line is reached */
921 static void
922 edit_move_up_paragraph (WEdit * edit, int do_scroll)
924 int i = 0;
925 if (edit->curs_line > 1)
927 if (line_is_blank (edit, edit->curs_line))
929 if (line_is_blank (edit, edit->curs_line - 1))
931 for (i = edit->curs_line - 1; i; i--)
932 if (!line_is_blank (edit, i))
934 i++;
935 break;
938 else
940 for (i = edit->curs_line - 1; i; i--)
941 if (line_is_blank (edit, i))
942 break;
945 else
947 for (i = edit->curs_line - 1; i; i--)
948 if (line_is_blank (edit, i))
949 break;
952 edit_move_up (edit, edit->curs_line - i, do_scroll);
955 /* --------------------------------------------------------------------------------------------- */
956 /** moves down until a blank line is reached, or until just
957 before a non-blank line is reached */
959 static void
960 edit_move_down_paragraph (WEdit * edit, int do_scroll)
962 int i;
963 if (edit->curs_line >= edit->total_lines - 1)
965 i = edit->total_lines;
967 else
969 if (line_is_blank (edit, edit->curs_line))
971 if (line_is_blank (edit, edit->curs_line + 1))
973 for (i = edit->curs_line + 1; i; i++)
974 if (!line_is_blank (edit, i) || i > edit->total_lines)
976 i--;
977 break;
980 else
982 for (i = edit->curs_line + 1; i; i++)
983 if (line_is_blank (edit, i) || i >= edit->total_lines)
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 edit_move_down (edit, i - edit->curs_line, do_scroll);
997 /* --------------------------------------------------------------------------------------------- */
999 static void
1000 edit_begin_page (WEdit * edit)
1002 edit_update_curs_row (edit);
1003 edit_move_up (edit, edit->curs_row, 0);
1006 /* --------------------------------------------------------------------------------------------- */
1008 static void
1009 edit_end_page (WEdit * edit)
1011 edit_update_curs_row (edit);
1012 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
1016 /* --------------------------------------------------------------------------------------------- */
1017 /** goto beginning of text */
1019 static void
1020 edit_move_to_top (WEdit * edit)
1022 if (edit->curs_line)
1024 edit_cursor_move (edit, -edit->curs1);
1025 edit_move_to_prev_col (edit, 0);
1026 edit->force |= REDRAW_PAGE;
1027 edit->search_start = 0;
1028 edit_update_curs_row (edit);
1033 /* --------------------------------------------------------------------------------------------- */
1034 /** goto end of text */
1036 static void
1037 edit_move_to_bottom (WEdit * edit)
1039 if (edit->curs_line < edit->total_lines)
1041 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1042 edit->start_display = edit->last_byte;
1043 edit->start_line = edit->total_lines;
1044 edit_scroll_upward (edit, edit->widget.lines - 1);
1045 edit->force |= REDRAW_PAGE;
1049 /* --------------------------------------------------------------------------------------------- */
1050 /** goto beginning of line */
1052 static void
1053 edit_cursor_to_bol (WEdit * edit)
1055 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1056 edit->search_start = edit->curs1;
1057 edit->prev_col = edit_get_col (edit);
1058 edit->over_col = 0;
1061 /* --------------------------------------------------------------------------------------------- */
1062 /** goto end of line */
1064 static void
1065 edit_cursor_to_eol (WEdit * edit)
1067 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1068 edit->search_start = edit->curs1;
1069 edit->prev_col = edit_get_col (edit);
1070 edit->over_col = 0;
1073 /* --------------------------------------------------------------------------------------------- */
1075 static unsigned long
1076 my_type_of (int c)
1078 int x, r = 0;
1079 const char *p, *q;
1080 const char option_chars_move_whole_word[] =
1081 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1083 if (!c)
1084 return 0;
1085 if (c == '!')
1087 if (*option_chars_move_whole_word == '!')
1088 return 2;
1089 return 0x80000000UL;
1091 if (g_ascii_isupper ((gchar) c))
1092 c = 'A';
1093 else if (g_ascii_islower ((gchar) c))
1094 c = 'a';
1095 else if (g_ascii_isalpha (c))
1096 c = 'a';
1097 else if (isdigit (c))
1098 c = '0';
1099 else if (isspace (c))
1100 c = ' ';
1101 q = strchr (option_chars_move_whole_word, c);
1102 if (!q)
1103 return 0xFFFFFFFFUL;
1106 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1107 if (*p == '!')
1108 x <<= 1;
1109 r |= x;
1111 while ((q = strchr (q + 1, c)));
1112 return r;
1115 /* --------------------------------------------------------------------------------------------- */
1117 static void
1118 edit_left_word_move (WEdit * edit, int s)
1120 for (;;)
1122 int c1, c2;
1123 if (edit->column_highlight
1124 && edit->mark1 != edit->mark2
1125 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1126 break;
1127 edit_cursor_move (edit, -1);
1128 if (!edit->curs1)
1129 break;
1130 c1 = edit_get_byte (edit, edit->curs1 - 1);
1131 c2 = edit_get_byte (edit, edit->curs1);
1132 if (c1 == '\n' || c2 == '\n')
1133 break;
1134 if (!(my_type_of (c1) & my_type_of (c2)))
1135 break;
1136 if (isspace (c1) && !isspace (c2))
1137 break;
1138 if (s)
1139 if (!isspace (c1) && isspace (c2))
1140 break;
1144 /* --------------------------------------------------------------------------------------------- */
1146 static void
1147 edit_left_word_move_cmd (WEdit * edit)
1149 edit_left_word_move (edit, 0);
1150 edit->force |= REDRAW_PAGE;
1153 /* --------------------------------------------------------------------------------------------- */
1155 static void
1156 edit_right_word_move (WEdit * edit, int s)
1158 for (;;)
1160 int c1, c2;
1161 if (edit->column_highlight
1162 && edit->mark1 != edit->mark2
1163 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1164 break;
1165 edit_cursor_move (edit, 1);
1166 if (edit->curs1 >= edit->last_byte)
1167 break;
1168 c1 = edit_get_byte (edit, edit->curs1 - 1);
1169 c2 = edit_get_byte (edit, edit->curs1);
1170 if (c1 == '\n' || c2 == '\n')
1171 break;
1172 if (!(my_type_of (c1) & my_type_of (c2)))
1173 break;
1174 if (isspace (c1) && !isspace (c2))
1175 break;
1176 if (s)
1177 if (!isspace (c1) && isspace (c2))
1178 break;
1182 /* --------------------------------------------------------------------------------------------- */
1184 static void
1185 edit_right_word_move_cmd (WEdit * edit)
1187 edit_right_word_move (edit, 0);
1188 edit->force |= REDRAW_PAGE;
1191 /* --------------------------------------------------------------------------------------------- */
1193 static void
1194 edit_right_char_move_cmd (WEdit * edit)
1196 int cw = 1;
1197 int c = 0;
1198 if (edit->utf8)
1200 c = edit_get_utf (edit, edit->curs1, &cw);
1201 if (cw < 1)
1202 cw = 1;
1204 else
1206 c = edit_get_byte (edit, edit->curs1);
1208 if (option_cursor_beyond_eol && c == '\n')
1210 edit->over_col++;
1212 else
1214 edit_cursor_move (edit, cw);
1218 /* --------------------------------------------------------------------------------------------- */
1220 static void
1221 edit_left_char_move_cmd (WEdit * edit)
1223 int cw = 1;
1224 if (edit->column_highlight
1225 && option_cursor_beyond_eol
1226 && edit->mark1 != edit->mark2
1227 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1228 return;
1229 if (edit->utf8)
1231 edit_get_prev_utf (edit, edit->curs1, &cw);
1232 if (cw < 1)
1233 cw = 1;
1235 if (option_cursor_beyond_eol && edit->over_col > 0)
1237 edit->over_col--;
1239 else
1241 edit_cursor_move (edit, -cw);
1245 /* --------------------------------------------------------------------------------------------- */
1246 /** Up or down cursor moving.
1247 direction = TRUE - move up
1248 = FALSE - move down
1251 static void
1252 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1254 unsigned long p;
1255 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1257 if (i > l)
1258 i = l;
1260 if (i == 0)
1261 return;
1263 if (i > 1)
1264 edit->force |= REDRAW_PAGE;
1265 if (do_scroll)
1267 if (direction)
1268 edit_scroll_upward (edit, i);
1269 else
1270 edit_scroll_downward (edit, i);
1272 p = edit_bol (edit, edit->curs1);
1274 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1276 edit_cursor_move (edit, p - edit->curs1);
1278 edit_move_to_prev_col (edit, p);
1280 /* search start of current multibyte char (like CJK) */
1281 if (edit->curs1 + 1 < edit->last_byte)
1283 edit_right_char_move_cmd (edit);
1284 edit_left_char_move_cmd (edit);
1287 edit->search_start = edit->curs1;
1288 edit->found_len = 0;
1291 /* --------------------------------------------------------------------------------------------- */
1293 static void
1294 edit_right_delete_word (WEdit * edit)
1296 int c1, c2;
1297 for (;;)
1299 if (edit->curs1 >= edit->last_byte)
1300 break;
1301 c1 = edit_delete (edit, 1);
1302 c2 = edit_get_byte (edit, edit->curs1);
1303 if (c1 == '\n' || c2 == '\n')
1304 break;
1305 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1306 break;
1307 if (!(my_type_of (c1) & my_type_of (c2)))
1308 break;
1312 /* --------------------------------------------------------------------------------------------- */
1314 static void
1315 edit_left_delete_word (WEdit * edit)
1317 int c1, c2;
1318 for (;;)
1320 if (edit->curs1 <= 0)
1321 break;
1322 c1 = edit_backspace (edit, 1);
1323 c2 = edit_get_byte (edit, edit->curs1 - 1);
1324 if (c1 == '\n' || c2 == '\n')
1325 break;
1326 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1327 break;
1328 if (!(my_type_of (c1) & my_type_of (c2)))
1329 break;
1333 /* --------------------------------------------------------------------------------------------- */
1335 the start column position is not recorded, and hence does not
1336 undo as it happed. But who would notice.
1339 static void
1340 edit_do_undo (WEdit * edit)
1342 long ac;
1343 long count = 0;
1345 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1346 edit->over_col = 0;
1347 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1349 switch ((int) ac)
1351 case STACK_BOTTOM:
1352 goto done_undo;
1353 case CURS_RIGHT:
1354 edit_cursor_move (edit, 1);
1355 break;
1356 case CURS_LEFT:
1357 edit_cursor_move (edit, -1);
1358 break;
1359 case BACKSPACE:
1360 case BACKSPACE_BR:
1361 edit_backspace (edit, 1);
1362 break;
1363 case DELCHAR:
1364 case DELCHAR_BR:
1365 edit_delete (edit, 1);
1366 break;
1367 case COLUMN_ON:
1368 edit->column_highlight = 1;
1369 break;
1370 case COLUMN_OFF:
1371 edit->column_highlight = 0;
1372 break;
1374 if (ac >= 256 && ac < 512)
1375 edit_insert_ahead (edit, ac - 256);
1376 if (ac >= 0 && ac < 256)
1377 edit_insert (edit, ac);
1379 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1381 edit->mark1 = ac - MARK_1;
1382 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1384 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1386 edit->mark2 = ac - MARK_2;
1387 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1389 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1391 edit->end_mark_curs = ac - MARK_CURS;
1393 if (count++)
1394 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1397 if (edit->start_display > ac - KEY_PRESS)
1399 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1400 edit->force |= REDRAW_PAGE;
1402 else if (edit->start_display < ac - KEY_PRESS)
1404 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1405 edit->force |= REDRAW_PAGE;
1407 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1408 edit_update_curs_row (edit);
1410 done_undo:;
1411 edit->undo_stack_disable = 0;
1414 static void
1415 edit_do_redo (WEdit * edit)
1417 long ac;
1418 long count = 0;
1420 if (edit->redo_stack_reset)
1421 return;
1423 edit->over_col = 0;
1424 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1426 switch ((int) ac)
1428 case STACK_BOTTOM:
1429 goto done_redo;
1430 case CURS_RIGHT:
1431 edit_cursor_move (edit, 1);
1432 break;
1433 case CURS_LEFT:
1434 edit_cursor_move (edit, -1);
1435 break;
1436 case BACKSPACE:
1437 edit_backspace (edit, 1);
1438 break;
1439 case DELCHAR:
1440 edit_delete (edit, 1);
1441 break;
1442 case COLUMN_ON:
1443 edit->column_highlight = 1;
1444 break;
1445 case COLUMN_OFF:
1446 edit->column_highlight = 0;
1447 break;
1449 if (ac >= 256 && ac < 512)
1450 edit_insert_ahead (edit, ac - 256);
1451 if (ac >= 0 && ac < 256)
1452 edit_insert (edit, ac);
1454 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1456 edit->mark1 = ac - MARK_1;
1457 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1459 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1461 edit->mark2 = ac - MARK_2;
1462 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1464 /* more than one pop usually means something big */
1465 if (count++)
1466 edit->force |= REDRAW_PAGE;
1469 if (edit->start_display > ac - KEY_PRESS)
1471 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1472 edit->force |= REDRAW_PAGE;
1474 else if (edit->start_display < ac - KEY_PRESS)
1476 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1477 edit->force |= REDRAW_PAGE;
1479 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1480 edit_update_curs_row (edit);
1482 done_redo:;
1485 static void
1486 edit_group_undo (WEdit * edit)
1488 long ac = KEY_PRESS;
1489 long cur_ac = KEY_PRESS;
1490 while (ac != STACK_BOTTOM && ac == cur_ac)
1492 cur_ac = get_prev_undo_action (edit);
1493 edit_do_undo (edit);
1494 ac = get_prev_undo_action (edit);
1495 /* exit from cycle if option_group_undo is not set,
1496 * and make single UNDO operation
1498 if (!option_group_undo)
1499 ac = STACK_BOTTOM;
1503 /* --------------------------------------------------------------------------------------------- */
1505 static void
1506 edit_delete_to_line_end (WEdit * edit)
1508 while (edit_get_byte (edit, edit->curs1) != '\n')
1510 if (!edit->curs2)
1511 break;
1512 edit_delete (edit, 1);
1516 /* --------------------------------------------------------------------------------------------- */
1518 static void
1519 edit_delete_to_line_begin (WEdit * edit)
1521 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1523 if (!edit->curs1)
1524 break;
1525 edit_backspace (edit, 1);
1529 /* --------------------------------------------------------------------------------------------- */
1531 static int
1532 is_aligned_on_a_tab (WEdit * edit)
1534 edit_update_curs_col (edit);
1535 return !((edit->curs_col % (TAB_SIZE * space_width))
1536 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1539 /* --------------------------------------------------------------------------------------------- */
1541 static int
1542 right_of_four_spaces (WEdit * edit)
1544 int i, ch = 0;
1545 for (i = 1; i <= HALF_TAB_SIZE; i++)
1546 ch |= edit_get_byte (edit, edit->curs1 - i);
1547 if (ch == ' ')
1548 return is_aligned_on_a_tab (edit);
1549 return 0;
1552 /* --------------------------------------------------------------------------------------------- */
1554 static int
1555 left_of_four_spaces (WEdit * edit)
1557 int i, ch = 0;
1558 for (i = 0; i < HALF_TAB_SIZE; i++)
1559 ch |= edit_get_byte (edit, edit->curs1 + i);
1560 if (ch == ' ')
1561 return is_aligned_on_a_tab (edit);
1562 return 0;
1565 /* --------------------------------------------------------------------------------------------- */
1567 static void
1568 edit_auto_indent (WEdit * edit)
1570 long p;
1571 char c;
1572 p = edit->curs1;
1573 /* use the previous line as a template */
1574 p = edit_move_backward (edit, p, 1);
1575 /* copy the leading whitespace of the line */
1576 for (;;)
1577 { /* no range check - the line _is_ \n-terminated */
1578 c = edit_get_byte (edit, p++);
1579 if (c != ' ' && c != '\t')
1580 break;
1581 edit_insert (edit, c);
1585 /* --------------------------------------------------------------------------------------------- */
1587 static inline void
1588 edit_double_newline (WEdit * edit)
1590 edit_insert (edit, '\n');
1591 if (edit_get_byte (edit, edit->curs1) == '\n')
1592 return;
1593 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1594 return;
1595 edit->force |= REDRAW_PAGE;
1596 edit_insert (edit, '\n');
1600 /* --------------------------------------------------------------------------------------------- */
1602 static void
1603 insert_spaces_tab (WEdit * edit, gboolean half)
1605 int i;
1607 edit_update_curs_col (edit);
1608 i = option_tab_spacing * space_width;
1609 if (half)
1610 i /= 2;
1611 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1612 while (i > 0)
1614 edit_insert (edit, ' ');
1615 i -= space_width;
1619 /* --------------------------------------------------------------------------------------------- */
1621 static inline void
1622 edit_tab_cmd (WEdit * edit)
1624 int i;
1626 if (option_fake_half_tabs)
1628 if (is_in_indent (edit))
1630 /*insert a half tab (usually four spaces) unless there is a
1631 half tab already behind, then delete it and insert a
1632 full tab. */
1633 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1634 insert_spaces_tab (edit, TRUE);
1635 else
1637 for (i = 1; i <= HALF_TAB_SIZE; i++)
1638 edit_backspace (edit, 1);
1639 edit_insert (edit, '\t');
1641 return;
1644 if (option_fill_tabs_with_spaces)
1645 insert_spaces_tab (edit, FALSE);
1646 else
1647 edit_insert (edit, '\t');
1650 /* --------------------------------------------------------------------------------------------- */
1652 static void
1653 check_and_wrap_line (WEdit * edit)
1655 int curs, c;
1656 if (!option_typewriter_wrap)
1657 return;
1658 edit_update_curs_col (edit);
1659 if (edit->curs_col < option_word_wrap_line_length)
1660 return;
1661 curs = edit->curs1;
1662 for (;;)
1664 curs--;
1665 c = edit_get_byte (edit, curs);
1666 if (c == '\n' || curs <= 0)
1668 edit_insert (edit, '\n');
1669 return;
1671 if (c == ' ' || c == '\t')
1673 int current = edit->curs1;
1674 edit_cursor_move (edit, curs - edit->curs1 + 1);
1675 edit_insert (edit, '\n');
1676 edit_cursor_move (edit, current - edit->curs1 + 1);
1677 return;
1682 /* --------------------------------------------------------------------------------------------- */
1683 /** this find the matching bracket in either direction, and sets edit->bracket */
1685 static long
1686 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1688 const char *const b = "{}{[][()(", *p;
1689 int i = 1, a, inc = -1, c, d, n = 0;
1690 unsigned long j = 0;
1691 long q;
1692 edit_update_curs_row (edit);
1693 c = edit_get_byte (edit, edit->curs1);
1694 p = strchr (b, c);
1695 /* no limit */
1696 if (!furthest_bracket_search)
1697 furthest_bracket_search--;
1698 /* not on a bracket at all */
1699 if (!p)
1700 return -1;
1701 /* the matching bracket */
1702 d = p[1];
1703 /* going left or right? */
1704 if (strchr ("{[(", c))
1705 inc = 1;
1706 for (q = edit->curs1 + inc;; q += inc)
1708 /* out of buffer? */
1709 if (q >= edit->last_byte || q < 0)
1710 break;
1711 a = edit_get_byte (edit, q);
1712 /* don't want to eat CPU */
1713 if (j++ > furthest_bracket_search)
1714 break;
1715 /* out of screen? */
1716 if (in_screen)
1718 if (q < edit->start_display)
1719 break;
1720 /* count lines if searching downward */
1721 if (inc > 0 && a == '\n')
1722 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1723 break;
1725 /* count bracket depth */
1726 i += (a == c) - (a == d);
1727 /* return if bracket depth is zero */
1728 if (!i)
1729 return q;
1731 /* no match */
1732 return -1;
1735 /* --------------------------------------------------------------------------------------------- */
1737 static inline void
1738 edit_goto_matching_bracket (WEdit * edit)
1740 long q;
1742 q = edit_get_bracket (edit, 0, 0);
1743 if (q >= 0)
1745 edit->bracket = edit->curs1;
1746 edit->force |= REDRAW_PAGE;
1747 edit_cursor_move (edit, q - edit->curs1);
1751 /* --------------------------------------------------------------------------------------------- */
1753 static void
1754 edit_move_block_to_right (WEdit * edit)
1756 long start_mark, end_mark;
1757 long cur_bol, start_bol;
1759 if (eval_marks (edit, &start_mark, &end_mark))
1760 return;
1762 start_bol = edit_bol (edit, start_mark);
1763 cur_bol = edit_bol (edit, end_mark - 1);
1767 edit_cursor_move (edit, cur_bol - edit->curs1);
1768 if (option_fill_tabs_with_spaces)
1769 insert_spaces_tab (edit, option_fake_half_tabs);
1770 else
1771 edit_insert (edit, '\t');
1772 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1774 if (cur_bol == 0)
1775 break;
1777 cur_bol = edit_bol (edit, cur_bol - 1);
1779 while (cur_bol >= start_bol);
1781 edit->force |= REDRAW_PAGE;
1784 /* --------------------------------------------------------------------------------------------- */
1786 static void
1787 edit_move_block_to_left (WEdit * edit)
1789 long start_mark, end_mark;
1790 long cur_bol, start_bol;
1791 int i;
1793 if (eval_marks (edit, &start_mark, &end_mark))
1794 return;
1796 start_bol = edit_bol (edit, start_mark);
1797 cur_bol = edit_bol (edit, end_mark - 1);
1801 int del_tab_width;
1802 int next_char;
1804 edit_cursor_move (edit, cur_bol - edit->curs1);
1806 if (option_fake_half_tabs)
1807 del_tab_width = HALF_TAB_SIZE;
1808 else
1809 del_tab_width = option_tab_spacing;
1811 next_char = edit_get_byte (edit, edit->curs1);
1812 if (next_char == '\t')
1813 edit_delete (edit, 1);
1814 else if (next_char == ' ')
1815 for (i = 1; i <= del_tab_width; i++)
1817 if (next_char == ' ')
1818 edit_delete (edit, 1);
1819 next_char = edit_get_byte (edit, edit->curs1);
1822 if (cur_bol == 0)
1823 break;
1825 cur_bol = edit_bol (edit, cur_bol - 1);
1827 while (cur_bol >= start_bol);
1829 edit->force |= REDRAW_PAGE;
1832 /* --------------------------------------------------------------------------------------------- */
1834 * prints at the cursor
1835 * @returns the number of chars printed
1838 static size_t
1839 edit_print_string (WEdit * e, const char *s)
1841 size_t i = 0;
1843 while (s[i] != '\0')
1844 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1845 e->force |= REDRAW_COMPLETELY;
1846 edit_update_screen (e);
1847 return i;
1850 /* --------------------------------------------------------------------------------------------- */
1851 /*** public functions ****************************************************************************/
1852 /* --------------------------------------------------------------------------------------------- */
1854 /** User edit menu, like user menu (F2) but only in editor. */
1856 void
1857 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1859 char *block_file;
1860 int nomark;
1861 long curs;
1862 long start_mark, end_mark;
1863 struct stat status;
1864 vfs_path_t *block_file_vpath;
1866 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1867 block_file_vpath = vfs_path_from_str (block_file);
1868 curs = edit->curs1;
1869 nomark = eval_marks (edit, &start_mark, &end_mark);
1870 if (nomark == 0)
1871 edit_save_block (edit, block_file, start_mark, end_mark);
1873 /* run shell scripts from menu */
1874 if (user_menu_cmd (edit, menu_file, selected_entry)
1875 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1877 int rc = 0;
1878 FILE *fd;
1880 /* i.e. we have marked block */
1881 if (nomark == 0)
1882 rc = edit_block_delete_cmd (edit);
1884 if (rc == 0)
1886 long ins_len;
1888 ins_len = edit_insert_file (edit, block_file_vpath);
1889 if (nomark == 0 && ins_len > 0)
1890 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1892 /* truncate block file */
1893 fd = fopen (block_file, "w");
1894 if (fd != NULL)
1895 fclose (fd);
1897 edit_cursor_move (edit, curs - edit->curs1);
1898 edit_refresh_cmd (edit);
1899 edit->force |= REDRAW_COMPLETELY;
1901 g_free (block_file);
1902 vfs_path_free (block_file_vpath);
1905 /* --------------------------------------------------------------------------------------------- */
1908 edit_get_byte (WEdit * edit, long byte_index)
1910 unsigned long p;
1911 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1912 return '\n';
1914 if (byte_index >= edit->curs1)
1916 p = edit->curs1 + edit->curs2 - byte_index - 1;
1917 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1919 else
1921 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1925 /* --------------------------------------------------------------------------------------------- */
1928 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1930 gchar *str = NULL;
1931 int res = -1;
1932 gunichar ch;
1933 gchar *next_ch = NULL;
1934 int width = 0;
1935 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1937 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1939 *char_width = 0;
1940 return '\n';
1943 str = edit_get_byte_ptr (edit, byte_index);
1945 if (str == NULL)
1947 *char_width = 0;
1948 return 0;
1951 res = g_utf8_get_char_validated (str, -1);
1953 if (res < 0)
1955 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1956 int i;
1957 for (i = 0; i < UTF8_CHAR_LEN; i++)
1958 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1959 utf8_buf[UTF8_CHAR_LEN] = '\0';
1960 str = utf8_buf;
1961 res = g_utf8_get_char_validated (str, -1);
1964 if (res < 0)
1966 ch = *str;
1967 width = 0;
1969 else
1971 ch = res;
1972 /* Calculate UTF-8 char width */
1973 next_ch = g_utf8_next_char (str);
1974 if (next_ch)
1976 width = next_ch - str;
1978 else
1980 ch = 0;
1981 width = 0;
1984 *char_width = width;
1985 return ch;
1988 /* --------------------------------------------------------------------------------------------- */
1990 char *
1991 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1993 int i;
1994 char *p, *writename;
1995 const vfs_path_element_t *path_element;
1997 i = edit_find_filter (filename_vpath);
1998 if (i < 0)
1999 return NULL;
2001 path_element = vfs_path_get_by_index (write_name_vpath, -1);
2002 writename = name_quote (path_element->path, 0);
2003 p = g_strdup_printf (all_filters[i].write, writename);
2004 g_free (writename);
2005 return p;
2008 /* --------------------------------------------------------------------------------------------- */
2010 long
2011 edit_write_stream (WEdit * edit, FILE * f)
2013 long i;
2015 if (edit->lb == LB_ASIS)
2017 for (i = 0; i < edit->last_byte; i++)
2018 if (fputc (edit_get_byte (edit, i), f) < 0)
2019 break;
2020 return i;
2023 /* change line breaks */
2024 for (i = 0; i < edit->last_byte; i++)
2026 unsigned char c = edit_get_byte (edit, i);
2028 if (!(c == '\n' || c == '\r'))
2030 /* not line break */
2031 if (fputc (c, f) < 0)
2032 return i;
2034 else
2035 { /* (c == '\n' || c == '\r') */
2036 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2038 switch (edit->lb)
2040 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2041 /* put one line break unconditionally */
2042 if (fputc ('\n', f) < 0)
2043 return i;
2045 i++; /* 2 chars are processed */
2047 if (c == '\r' && c1 == '\n')
2048 /* Windows line break; go to the next char */
2049 break;
2051 if (c == '\r' && c1 == '\r')
2053 /* two Macintosh line breaks; put second line break */
2054 if (fputc ('\n', f) < 0)
2055 return i;
2056 break;
2059 if (fputc (c1, f) < 0)
2060 return i;
2061 break;
2063 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2064 /* put one line break unconditionally */
2065 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2066 return i;
2068 if (c == '\r' && c1 == '\n')
2069 /* Windows line break; go to the next char */
2070 i++;
2071 break;
2073 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2074 /* put one line break unconditionally */
2075 if (fputc ('\r', f) < 0)
2076 return i;
2078 i++; /* 2 chars are processed */
2080 if (c == '\r' && c1 == '\n')
2081 /* Windows line break; go to the next char */
2082 break;
2084 if (c == '\n' && c1 == '\n')
2086 /* two Windows line breaks; put second line break */
2087 if (fputc ('\r', f) < 0)
2088 return i;
2089 break;
2092 if (fputc (c1, f) < 0)
2093 return i;
2094 break;
2095 case LB_ASIS: /* default without changes */
2096 break;
2101 return edit->last_byte;
2104 /* --------------------------------------------------------------------------------------------- */
2105 /** inserts a file at the cursor, returns count of inserted bytes on success */
2106 long
2107 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2109 char *p;
2110 long ins_len = 0;
2112 p = edit_get_filter (filename_vpath);
2113 if (p != NULL)
2115 FILE *f;
2116 long current = edit->curs1;
2118 f = (FILE *) popen (p, "r");
2119 if (f != NULL)
2121 edit_insert_stream (edit, f);
2122 ins_len = edit->curs1 - current;
2123 edit_cursor_move (edit, -ins_len);
2124 if (pclose (f) > 0)
2126 char *errmsg;
2128 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2129 edit_error_dialog (_("Error"), errmsg);
2130 g_free (errmsg);
2131 ins_len = -1;
2134 else
2136 char *errmsg;
2138 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2139 edit_error_dialog (_("Error"), errmsg);
2140 g_free (errmsg);
2141 ins_len = -1;
2143 g_free (p);
2145 else
2147 int i, file, blocklen;
2148 long current = edit->curs1;
2149 int vertical_insertion = 0;
2150 char *buf;
2152 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2153 if (file == -1)
2154 return -1;
2156 buf = g_malloc0 (TEMP_BUF_LEN);
2157 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2158 if (blocklen > 0)
2160 /* if contain signature VERTICAL_MAGIC then it vertical block */
2161 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2162 vertical_insertion = 1;
2163 else
2164 mc_lseek (file, 0, SEEK_SET);
2167 if (vertical_insertion)
2169 long mark1, mark2;
2170 int c1, c2;
2172 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2173 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2174 /* highlight inserted text then not persistent blocks */
2175 if (!option_persistent_selections)
2177 if (!edit->column_highlight)
2178 edit_push_undo_action (edit, COLUMN_OFF);
2179 edit->column_highlight = 1;
2182 else
2184 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2186 for (i = 0; i < blocklen; i++)
2187 edit_insert (edit, buf[i]);
2189 /* highlight inserted text then not persistent blocks */
2190 if (!option_persistent_selections && edit->modified)
2192 edit_set_markers (edit, edit->curs1, current, 0, 0);
2193 if (edit->column_highlight)
2194 edit_push_undo_action (edit, COLUMN_ON);
2195 edit->column_highlight = 0;
2199 edit->force |= REDRAW_PAGE;
2200 ins_len = edit->curs1 - current;
2201 edit_cursor_move (edit, -ins_len);
2202 g_free (buf);
2203 mc_close (file);
2204 if (blocklen != 0)
2205 ins_len = 0;
2208 return ins_len;
2211 /* --------------------------------------------------------------------------------------------- */
2213 * Fill in the edit structure. Return NULL on failure. Pass edit as
2214 * NULL to allocate a new structure.
2216 * If line is 0, try to restore saved position. Otherwise put the
2217 * cursor on that line and show it in the middle of the screen.
2220 WEdit *
2221 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2222 long line)
2224 gboolean to_free = FALSE;
2226 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2227 if (option_line_state)
2228 option_line_state_width = LINE_STATE_WIDTH;
2229 else
2230 option_line_state_width = 0;
2232 if (edit == NULL)
2234 #ifdef ENABLE_NLS
2236 * Expand option_whole_chars_search by national letters using
2237 * current locale
2240 static char option_whole_chars_search_buf[256];
2242 if (option_whole_chars_search_buf != option_whole_chars_search)
2244 size_t i;
2245 size_t len = str_term_width1 (option_whole_chars_search);
2247 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2249 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2251 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2253 option_whole_chars_search_buf[len++] = i;
2257 option_whole_chars_search_buf[len] = 0;
2258 option_whole_chars_search = option_whole_chars_search_buf;
2260 #endif /* ENABLE_NLS */
2261 edit = g_malloc0 (sizeof (WEdit));
2262 edit->search = NULL;
2263 to_free = TRUE;
2266 edit_purge_widget (edit);
2267 edit->widget.y = y;
2268 edit->widget.x = x;
2269 edit->widget.lines = lines;
2270 edit->widget.cols = cols;
2272 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2273 edit->stat1.st_uid = getuid ();
2274 edit->stat1.st_gid = getgid ();
2275 edit->stat1.st_mtime = 0;
2277 edit->over_col = 0;
2278 edit->bracket = -1;
2279 edit->force |= REDRAW_PAGE;
2280 edit_set_filename (edit, filename_vpath);
2282 edit->undo_stack_size = START_STACK_SIZE;
2283 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2284 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2286 edit->redo_stack_size = START_STACK_SIZE;
2287 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2288 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2290 edit->utf8 = 0;
2291 edit->converter = str_cnv_from_term;
2292 edit_set_codeset (edit);
2294 if (!edit_load_file (edit))
2296 /* edit_load_file already gives an error message */
2297 if (to_free)
2298 g_free (edit);
2299 return NULL;
2302 edit->loading_done = 1;
2303 edit->modified = 0;
2304 edit->locked = 0;
2305 edit_load_syntax (edit, NULL, NULL);
2307 int color;
2308 edit_get_syntax_color (edit, -1, &color);
2311 /* load saved cursor position */
2312 if ((line == 0) && option_save_position)
2313 edit_load_position (edit);
2314 else
2316 if (line <= 0)
2317 line = 1;
2318 edit_move_display (edit, line - 1);
2319 edit_move_to_line (edit, line - 1);
2322 edit_load_macro_cmd (edit);
2323 return edit;
2326 /* --------------------------------------------------------------------------------------------- */
2328 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2329 gboolean
2330 edit_clean (WEdit * edit)
2332 int j = 0;
2334 if (edit == NULL)
2335 return FALSE;
2337 /* a stale lock, remove it */
2338 if (edit->locked)
2339 edit->locked = unlock_file (edit->filename_vpath);
2341 /* save cursor position */
2342 if (option_save_position)
2343 edit_save_position (edit);
2344 else if (edit->serialized_bookmarks != NULL)
2345 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2347 /* File specified on the mcedit command line and never saved */
2348 if (edit->delete_file)
2349 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2351 edit_free_syntax_rules (edit);
2352 book_mark_flush (edit, -1);
2353 for (; j <= MAXBUFF; j++)
2355 g_free (edit->buffers1[j]);
2356 g_free (edit->buffers2[j]);
2359 g_free (edit->undo_stack);
2360 g_free (edit->redo_stack);
2361 vfs_path_free (edit->filename_vpath);
2362 vfs_path_free (edit->dir_vpath);
2363 mc_search_free (edit->search);
2364 edit->search = NULL;
2366 if (edit->converter != str_cnv_from_term)
2367 str_close_conv (edit->converter);
2369 edit_purge_widget (edit);
2371 return TRUE;
2374 /* --------------------------------------------------------------------------------------------- */
2376 /** returns TRUE on success */
2377 gboolean
2378 edit_renew (WEdit * edit)
2380 int y = edit->widget.y;
2381 int x = edit->widget.x;
2382 int lines = edit->widget.lines;
2383 int columns = edit->widget.cols;
2385 edit_clean (edit);
2386 return (edit_init (edit, y, x, lines, columns, NULL, 0) != NULL);
2389 /* --------------------------------------------------------------------------------------------- */
2392 * Load a new file into the editor and set line. If it fails, preserve the old file.
2393 * To do it, allocate a new widget, initialize it and, if the new file
2394 * was loaded, copy the data to the old widget.
2396 * @returns TRUE on success, FALSE on failure.
2398 gboolean
2399 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2401 WEdit *e;
2402 int y = edit->widget.y;
2403 int x = edit->widget.x;
2404 int lines = edit->widget.lines;
2405 int columns = edit->widget.cols;
2407 e = g_malloc0 (sizeof (WEdit));
2408 e->widget = edit->widget;
2410 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2412 g_free (e);
2413 return FALSE;
2416 edit_clean (edit);
2417 memcpy (edit, e, sizeof (WEdit));
2418 g_free (e);
2420 return TRUE;
2423 /* --------------------------------------------------------------------------------------------- */
2425 void
2426 edit_set_codeset (WEdit * edit)
2428 #ifdef HAVE_CHARSET
2429 const char *cp_id;
2431 cp_id =
2432 get_codepage_id (mc_global.source_codepage >=
2433 0 ? mc_global.source_codepage : mc_global.display_codepage);
2435 if (cp_id != NULL)
2437 GIConv conv;
2438 conv = str_crt_conv_from (cp_id);
2439 if (conv != INVALID_CONV)
2441 if (edit->converter != str_cnv_from_term)
2442 str_close_conv (edit->converter);
2443 edit->converter = conv;
2447 if (cp_id != NULL)
2448 edit->utf8 = str_isutf8 (cp_id);
2449 #else
2450 (void) edit;
2451 #endif
2455 /* --------------------------------------------------------------------------------------------- */
2457 Recording stack for undo:
2458 The following is an implementation of a compressed stack. Identical
2459 pushes are recorded by a negative prefix indicating the number of times the
2460 same char was pushed. This saves space for repeated curs-left or curs-right
2461 delete etc.
2465 pushed: stored:
2469 b -3
2471 c --> -4
2477 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2478 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2479 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2480 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2481 position.
2483 The only way the cursor moves or the buffer is changed is through the routines:
2484 insert, backspace, insert_ahead, delete, and cursor_move.
2485 These record the reverse undo movements onto the stack each time they are
2486 called.
2488 Each key press results in a set of actions (insert; delete ...). So each time
2489 a key is pressed the current position of start_display is pushed as
2490 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2491 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2492 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2496 void
2497 edit_push_undo_action (WEdit * edit, long c, ...)
2499 unsigned long sp = edit->undo_stack_pointer;
2500 unsigned long spm1;
2501 long *t;
2503 /* first enlarge the stack if necessary */
2504 if (sp > edit->undo_stack_size - 10)
2505 { /* say */
2506 if (option_max_undo < 256)
2507 option_max_undo = 256;
2508 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2510 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2511 if (t)
2513 edit->undo_stack = t;
2514 edit->undo_stack_size <<= 1;
2515 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2519 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2520 if (edit->undo_stack_disable)
2522 edit_push_redo_action (edit, KEY_PRESS);
2523 edit_push_redo_action (edit, c);
2524 return;
2526 else if (edit->redo_stack_reset)
2528 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2531 if (edit->undo_stack_bottom != sp
2532 && spm1 != edit->undo_stack_bottom
2533 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2535 int d;
2536 if (edit->undo_stack[spm1] < 0)
2538 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2539 if (d == c)
2541 if (edit->undo_stack[spm1] > -1000000000)
2543 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2545 edit->undo_stack[spm1]--;
2547 return;
2551 else
2553 d = edit->undo_stack[spm1];
2554 if (d == c)
2556 if (c >= KEY_PRESS)
2557 return; /* --> no need to push multiple do-nothings */
2558 edit->undo_stack[sp] = -2;
2559 goto check_bottom;
2563 edit->undo_stack[sp] = c;
2565 check_bottom:
2566 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2568 /* if the sp wraps round and catches the undo_stack_bottom then erase
2569 * the first set of actions on the stack to make space - by moving
2570 * undo_stack_bottom forward one "key press" */
2571 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2572 if ((unsigned long) c == edit->undo_stack_bottom ||
2573 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2576 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2578 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2579 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2581 /*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: */
2582 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2583 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2585 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2589 void
2590 edit_push_redo_action (WEdit * edit, long c, ...)
2592 unsigned long sp = edit->redo_stack_pointer;
2593 unsigned long spm1;
2594 long *t;
2595 /* first enlarge the stack if necessary */
2596 if (sp > edit->redo_stack_size - 10)
2597 { /* say */
2598 if (option_max_undo < 256)
2599 option_max_undo = 256;
2600 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2602 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2603 if (t)
2605 edit->redo_stack = t;
2606 edit->redo_stack_size <<= 1;
2607 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2611 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2613 if (edit->redo_stack_bottom != sp
2614 && spm1 != edit->redo_stack_bottom
2615 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2617 int d;
2618 if (edit->redo_stack[spm1] < 0)
2620 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2621 if (d == c)
2623 if (edit->redo_stack[spm1] > -1000000000)
2625 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2626 edit->redo_stack[spm1]--;
2627 return;
2631 else
2633 d = edit->redo_stack[spm1];
2634 if (d == c)
2636 if (c >= KEY_PRESS)
2637 return; /* --> no need to push multiple do-nothings */
2638 edit->redo_stack[sp] = -2;
2639 goto redo_check_bottom;
2643 edit->redo_stack[sp] = c;
2645 redo_check_bottom:
2646 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2648 /* if the sp wraps round and catches the redo_stack_bottom then erase
2649 * the first set of actions on the stack to make space - by moving
2650 * redo_stack_bottom forward one "key press" */
2651 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2652 if ((unsigned long) c == edit->redo_stack_bottom ||
2653 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2656 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2658 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2659 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2662 * If a single key produced enough pushes to wrap all the way round then
2663 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2664 * The stack is then initialised:
2667 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2668 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2669 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2673 /* --------------------------------------------------------------------------------------------- */
2675 Basic low level single character buffer alterations and movements at the cursor.
2676 Returns char passed over, inserted or removed.
2679 void
2680 edit_insert (WEdit * edit, int c)
2682 /* check if file has grown to large */
2683 if (edit->last_byte >= SIZE_LIMIT)
2684 return;
2686 /* first we must update the position of the display window */
2687 if (edit->curs1 < edit->start_display)
2689 edit->start_display++;
2690 if (c == '\n')
2691 edit->start_line++;
2694 /* Mark file as modified, unless the file hasn't been fully loaded */
2695 if (edit->loading_done)
2697 edit_modification (edit);
2700 /* now we must update some info on the file and check if a redraw is required */
2701 if (c == '\n')
2703 if (edit->book_mark)
2704 book_mark_inc (edit, edit->curs_line);
2705 edit->curs_line++;
2706 edit->total_lines++;
2707 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2710 /* save the reverse command onto the undo stack */
2711 /* ordinary char and not space */
2712 if (c > 32)
2713 edit_push_undo_action (edit, BACKSPACE);
2714 else
2715 edit_push_undo_action (edit, BACKSPACE_BR);
2716 /* update markers */
2717 edit->mark1 += (edit->mark1 > edit->curs1);
2718 edit->mark2 += (edit->mark2 > edit->curs1);
2719 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2721 /* add a new buffer if we've reached the end of the last one */
2722 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2723 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2725 /* perform the insertion */
2726 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2727 = (unsigned char) c;
2729 /* update file length */
2730 edit->last_byte++;
2732 /* update cursor position */
2733 edit->curs1++;
2736 /* --------------------------------------------------------------------------------------------- */
2737 /** same as edit_insert and move left */
2739 void
2740 edit_insert_ahead (WEdit * edit, int c)
2742 if (edit->last_byte >= SIZE_LIMIT)
2743 return;
2745 if (edit->curs1 < edit->start_display)
2747 edit->start_display++;
2748 if (c == '\n')
2749 edit->start_line++;
2751 edit_modification (edit);
2752 if (c == '\n')
2754 if (edit->book_mark)
2755 book_mark_inc (edit, edit->curs_line);
2756 edit->total_lines++;
2757 edit->force |= REDRAW_AFTER_CURSOR;
2759 /* ordinary char and not space */
2760 if (c > 32)
2761 edit_push_undo_action (edit, DELCHAR);
2762 else
2763 edit_push_undo_action (edit, DELCHAR_BR);
2765 edit->mark1 += (edit->mark1 >= edit->curs1);
2766 edit->mark2 += (edit->mark2 >= edit->curs1);
2767 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2769 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2770 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2771 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2772 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2774 edit->last_byte++;
2775 edit->curs2++;
2779 /* --------------------------------------------------------------------------------------------- */
2782 edit_delete (WEdit * edit, const int byte_delete)
2784 int p = 0;
2785 int cw = 1;
2786 int i;
2788 if (!edit->curs2)
2789 return 0;
2791 cw = 1;
2792 /* if byte_delete = 1 then delete only one byte not multibyte char */
2793 if (edit->utf8 && byte_delete == 0)
2795 edit_get_utf (edit, edit->curs1, &cw);
2796 if (cw < 1)
2797 cw = 1;
2800 if (edit->mark2 != edit->mark1)
2801 edit_push_markers (edit);
2803 for (i = 1; i <= cw; i++)
2805 if (edit->mark1 > edit->curs1)
2807 edit->mark1--;
2808 edit->end_mark_curs--;
2810 if (edit->mark2 > edit->curs1)
2811 edit->mark2--;
2812 if (edit->last_get_rule > edit->curs1)
2813 edit->last_get_rule--;
2815 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2816 ((edit->curs2 -
2817 1) & M_EDIT_BUF_SIZE) - 1];
2819 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2821 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2822 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2824 edit->last_byte--;
2825 edit->curs2--;
2826 edit_push_undo_action (edit, p + 256);
2829 edit_modification (edit);
2830 if (p == '\n')
2832 if (edit->book_mark)
2833 book_mark_dec (edit, edit->curs_line);
2834 edit->total_lines--;
2835 edit->force |= REDRAW_AFTER_CURSOR;
2837 if (edit->curs1 < edit->start_display)
2839 edit->start_display--;
2840 if (p == '\n')
2841 edit->start_line--;
2844 return p;
2847 /* --------------------------------------------------------------------------------------------- */
2848 /** moves the cursor right or left: increment positive or negative respectively */
2850 void
2851 edit_cursor_move (WEdit * edit, long increment)
2853 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2854 int c;
2856 if (increment < 0)
2858 for (; increment < 0; increment++)
2860 if (!edit->curs1)
2861 return;
2863 edit_push_undo_action (edit, CURS_RIGHT);
2865 c = edit_get_byte (edit, edit->curs1 - 1);
2866 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2867 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2868 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2869 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2870 edit->curs2++;
2871 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2872 1) & M_EDIT_BUF_SIZE];
2873 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2875 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2876 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2878 edit->curs1--;
2879 if (c == '\n')
2881 edit->curs_line--;
2882 edit->force |= REDRAW_LINE_BELOW;
2887 else if (increment > 0)
2889 for (; increment > 0; increment--)
2891 if (!edit->curs2)
2892 return;
2894 edit_push_undo_action (edit, CURS_LEFT);
2896 c = edit_get_byte (edit, edit->curs1);
2897 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2898 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2899 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2900 edit->curs1++;
2901 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2902 ((edit->curs2 -
2903 1) & M_EDIT_BUF_SIZE) - 1];
2904 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2906 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2907 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2909 edit->curs2--;
2910 if (c == '\n')
2912 edit->curs_line++;
2913 edit->force |= REDRAW_LINE_ABOVE;
2919 /* These functions return positions relative to lines */
2921 /* --------------------------------------------------------------------------------------------- */
2922 /** returns index of last char on line + 1 */
2924 long
2925 edit_eol (WEdit * edit, long current)
2927 if (current >= edit->last_byte)
2928 return edit->last_byte;
2930 for (;; current++)
2931 if (edit_get_byte (edit, current) == '\n')
2932 break;
2933 return current;
2936 /* --------------------------------------------------------------------------------------------- */
2937 /** returns index of first char on line */
2939 long
2940 edit_bol (WEdit * edit, long current)
2942 if (current <= 0)
2943 return 0;
2945 for (;; current--)
2946 if (edit_get_byte (edit, current - 1) == '\n')
2947 break;
2948 return current;
2951 /* --------------------------------------------------------------------------------------------- */
2953 long
2954 edit_count_lines (WEdit * edit, long current, long upto)
2956 long lines = 0;
2957 if (upto > edit->last_byte)
2958 upto = edit->last_byte;
2959 if (current < 0)
2960 current = 0;
2961 while (current < upto)
2962 if (edit_get_byte (edit, current++) == '\n')
2963 lines++;
2964 return lines;
2967 /* --------------------------------------------------------------------------------------------- */
2968 /* If lines is zero this returns the count of lines from current to upto. */
2969 /* If upto is zero returns index of lines forward current. */
2971 long
2972 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2974 if (upto)
2976 return edit_count_lines (edit, current, upto);
2978 else
2980 long next;
2981 if (lines < 0)
2982 lines = 0;
2983 while (lines--)
2985 next = edit_eol (edit, current) + 1;
2986 if (next > edit->last_byte)
2987 break;
2988 else
2989 current = next;
2991 return current;
2995 /* --------------------------------------------------------------------------------------------- */
2996 /** Returns offset of 'lines' lines up from current */
2998 long
2999 edit_move_backward (WEdit * edit, long current, long lines)
3001 if (lines < 0)
3002 lines = 0;
3003 current = edit_bol (edit, current);
3004 while ((lines--) && current != 0)
3005 current = edit_bol (edit, current - 1);
3006 return current;
3009 /* --------------------------------------------------------------------------------------------- */
3010 /* If cols is zero this returns the count of columns from current to upto. */
3011 /* If upto is zero returns index of cols across from current. */
3013 long
3014 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3016 long p, q;
3017 int col;
3019 if (upto)
3021 q = upto;
3022 cols = -10;
3024 else
3025 q = edit->last_byte + 2;
3027 for (col = 0, p = current; p < q; p++)
3029 int c, orig_c;
3031 if (cols != -10)
3033 if (col == cols)
3034 return p;
3035 if (col > cols)
3036 return p - 1;
3039 orig_c = c = edit_get_byte (edit, p);
3041 #ifdef HAVE_CHARSET
3042 if (edit->utf8)
3044 int utf_ch;
3045 int cw = 1;
3047 utf_ch = edit_get_utf (edit, p, &cw);
3048 if (mc_global.utf8_display)
3050 if (cw > 1)
3051 col -= cw - 1;
3052 if (g_unichar_iswide (utf_ch))
3053 col++;
3055 else if (cw > 1 && g_unichar_isprint (utf_ch))
3056 col -= cw - 1;
3059 c = convert_to_display_c (c);
3060 #endif
3062 if (c == '\t')
3063 col += TAB_SIZE - col % TAB_SIZE;
3064 else if (c == '\n')
3066 if (upto)
3067 return col;
3068 else
3069 return p;
3071 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3072 /* '\r' is shown as ^M, so we must advance 2 characters */
3073 /* Caret notation for control characters */
3074 col += 2;
3075 else
3076 col++;
3078 return col;
3081 /* --------------------------------------------------------------------------------------------- */
3082 /** returns the current column position of the cursor */
3085 edit_get_col (WEdit * edit)
3087 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3090 /* --------------------------------------------------------------------------------------------- */
3091 /* Scrolling functions */
3092 /* --------------------------------------------------------------------------------------------- */
3094 void
3095 edit_update_curs_row (WEdit * edit)
3097 edit->curs_row = edit->curs_line - edit->start_line;
3100 /* --------------------------------------------------------------------------------------------- */
3102 void
3103 edit_update_curs_col (WEdit * edit)
3105 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3108 /* --------------------------------------------------------------------------------------------- */
3111 edit_get_curs_col (const WEdit * edit)
3113 return edit->curs_col;
3116 /* --------------------------------------------------------------------------------------------- */
3117 /** moves the display start position up by i lines */
3119 void
3120 edit_scroll_upward (WEdit * edit, unsigned long i)
3122 unsigned long lines_above = edit->start_line;
3123 if (i > lines_above)
3124 i = lines_above;
3125 if (i)
3127 edit->start_line -= i;
3128 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3129 edit->force |= REDRAW_PAGE;
3130 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3132 edit_update_curs_row (edit);
3136 /* --------------------------------------------------------------------------------------------- */
3137 /** returns 1 if could scroll, 0 otherwise */
3139 void
3140 edit_scroll_downward (WEdit * edit, int i)
3142 int lines_below;
3143 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3144 if (lines_below > 0)
3146 if (i > lines_below)
3147 i = lines_below;
3148 edit->start_line += i;
3149 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3150 edit->force |= REDRAW_PAGE;
3151 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3153 edit_update_curs_row (edit);
3156 /* --------------------------------------------------------------------------------------------- */
3158 void
3159 edit_scroll_right (WEdit * edit, int i)
3161 edit->force |= REDRAW_PAGE;
3162 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3163 edit->start_col -= i;
3166 /* --------------------------------------------------------------------------------------------- */
3168 void
3169 edit_scroll_left (WEdit * edit, int i)
3171 if (edit->start_col)
3173 edit->start_col += i;
3174 if (edit->start_col > 0)
3175 edit->start_col = 0;
3176 edit->force |= REDRAW_PAGE;
3177 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3181 /* --------------------------------------------------------------------------------------------- */
3182 /* high level cursor movement commands */
3183 /* --------------------------------------------------------------------------------------------- */
3185 void
3186 edit_move_to_prev_col (WEdit * edit, long p)
3188 int prev = edit->prev_col;
3189 int over = edit->over_col;
3190 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3192 if (option_cursor_beyond_eol)
3194 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3195 edit_eol (edit, edit->curs1));
3197 if (line_len < prev + edit->over_col)
3199 edit->over_col = prev + over - line_len;
3200 edit->prev_col = line_len;
3201 edit->curs_col = line_len;
3203 else
3205 edit->curs_col = prev + over;
3206 edit->prev_col = edit->curs_col;
3207 edit->over_col = 0;
3210 else
3212 edit->over_col = 0;
3213 if (is_in_indent (edit) && option_fake_half_tabs)
3215 edit_update_curs_col (edit);
3216 if (space_width)
3217 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3219 int q = edit->curs_col;
3220 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3221 p = edit_bol (edit, edit->curs1);
3222 edit_cursor_move (edit,
3223 edit_move_forward3 (edit, p, edit->curs_col,
3224 0) - edit->curs1);
3225 if (!left_of_four_spaces (edit))
3226 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3232 /* --------------------------------------------------------------------------------------------- */
3235 line_is_blank (WEdit * edit, long line)
3237 return is_blank (edit, edit_find_line (edit, line));
3240 /* --------------------------------------------------------------------------------------------- */
3241 /** move cursor to line 'line' */
3243 void
3244 edit_move_to_line (WEdit * e, long line)
3246 if (line < e->curs_line)
3247 edit_move_up (e, e->curs_line - line, 0);
3248 else
3249 edit_move_down (e, line - e->curs_line, 0);
3250 edit_scroll_screen_over_cursor (e);
3253 /* --------------------------------------------------------------------------------------------- */
3254 /** scroll window so that first visible line is 'line' */
3256 void
3257 edit_move_display (WEdit * e, long line)
3259 if (line < e->start_line)
3260 edit_scroll_upward (e, e->start_line - line);
3261 else
3262 edit_scroll_downward (e, line - e->start_line);
3265 /* --------------------------------------------------------------------------------------------- */
3266 /** save markers onto undo stack */
3268 void
3269 edit_push_markers (WEdit * edit)
3271 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3272 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3273 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3276 /* --------------------------------------------------------------------------------------------- */
3278 void
3279 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3281 edit->mark1 = m1;
3282 edit->mark2 = m2;
3283 edit->column1 = c1;
3284 edit->column2 = c2;
3288 /* --------------------------------------------------------------------------------------------- */
3289 /** highlight marker toggle */
3291 void
3292 edit_mark_cmd (WEdit * edit, int unmark)
3294 edit_push_markers (edit);
3295 if (unmark)
3297 edit_set_markers (edit, 0, 0, 0, 0);
3298 edit->force |= REDRAW_PAGE;
3300 else
3302 if (edit->mark2 >= 0)
3304 edit->end_mark_curs = -1;
3305 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3306 edit->curs_col + edit->over_col);
3307 edit->force |= REDRAW_PAGE;
3309 else
3311 edit->end_mark_curs = edit->curs1;
3312 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3313 edit->curs_col + edit->over_col);
3318 /* --------------------------------------------------------------------------------------------- */
3319 /** highlight the word under cursor */
3321 void
3322 edit_mark_current_word_cmd (WEdit * edit)
3324 long pos;
3326 for (pos = edit->curs1; pos != 0; pos--)
3328 int c1, c2;
3330 c1 = edit_get_byte (edit, pos);
3331 c2 = edit_get_byte (edit, pos - 1);
3332 if (!isspace (c1) && isspace (c2))
3333 break;
3334 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3335 break;
3337 edit->mark1 = pos;
3339 for (; pos < edit->last_byte; pos++)
3341 int c1, c2;
3343 c1 = edit_get_byte (edit, pos);
3344 c2 = edit_get_byte (edit, pos + 1);
3345 if (!isspace (c1) && isspace (c2))
3346 break;
3347 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3348 break;
3350 edit->mark2 = min (pos + 1, edit->last_byte);
3352 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3355 /* --------------------------------------------------------------------------------------------- */
3357 void
3358 edit_mark_current_line_cmd (WEdit * edit)
3360 long pos = edit->curs1;
3362 edit->mark1 = edit_bol (edit, pos);
3363 edit->mark2 = edit_eol (edit, pos);
3365 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3368 /* --------------------------------------------------------------------------------------------- */
3370 void
3371 edit_delete_line (WEdit * edit)
3374 * Delete right part of the line.
3375 * Note that edit_get_byte() returns '\n' when byte position is
3376 * beyond EOF.
3378 while (edit_get_byte (edit, edit->curs1) != '\n')
3380 (void) edit_delete (edit, 1);
3384 * Delete '\n' char.
3385 * Note that edit_delete() will not corrupt anything if called while
3386 * cursor position is EOF.
3388 (void) edit_delete (edit, 1);
3391 * Delete left part of the line.
3392 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3394 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3396 (void) edit_backspace (edit, 1);
3400 /* --------------------------------------------------------------------------------------------- */
3403 edit_indent_width (WEdit * edit, long p)
3405 long q = p;
3406 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3407 q++;
3408 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3411 /* --------------------------------------------------------------------------------------------- */
3413 void
3414 edit_insert_indent (WEdit * edit, int indent)
3416 if (!option_fill_tabs_with_spaces)
3418 while (indent >= TAB_SIZE)
3420 edit_insert (edit, '\t');
3421 indent -= TAB_SIZE;
3424 while (indent-- > 0)
3425 edit_insert (edit, ' ');
3428 /* --------------------------------------------------------------------------------------------- */
3430 void
3431 edit_push_key_press (WEdit * edit)
3433 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3434 if (edit->mark2 == -1)
3436 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3437 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3441 /* --------------------------------------------------------------------------------------------- */
3443 void
3444 edit_find_bracket (WEdit * edit)
3446 edit->bracket = edit_get_bracket (edit, 1, 10000);
3447 if (last_bracket != edit->bracket)
3448 edit->force |= REDRAW_PAGE;
3449 last_bracket = edit->bracket;
3452 /* --------------------------------------------------------------------------------------------- */
3454 * This executes a command as though the user initiated it through a key
3455 * press. Callback with WIDGET_KEY as a message calls this after
3456 * translating the key press. This function can be used to pass any
3457 * command to the editor. Note that the screen wouldn't update
3458 * automatically. Either of command or char_for_insertion must be
3459 * passed as -1. Commands are executed, and char_for_insertion is
3460 * inserted at the cursor.
3463 void
3464 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3466 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3467 || (macro_index < 0
3468 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3470 macro_index = 0;
3471 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3472 return;
3474 if (macro_index != -1)
3476 edit->force |= REDRAW_COMPLETELY;
3477 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3479 edit_store_macro_cmd (edit);
3480 macro_index = -1;
3481 return;
3483 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3485 edit_repeat_macro_cmd (edit);
3486 macro_index = -1;
3487 return;
3491 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3493 record_macro_buf[macro_index].action = command;
3494 record_macro_buf[macro_index++].ch = char_for_insertion;
3496 /* record the beginning of a set of editing actions initiated by a key press */
3497 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3498 edit_push_key_press (edit);
3500 edit_execute_cmd (edit, command, char_for_insertion);
3501 if (edit->column_highlight)
3502 edit->force |= REDRAW_PAGE;
3505 /* --------------------------------------------------------------------------------------------- */
3507 This executes a command at a lower level than macro recording.
3508 It also does not push a key_press onto the undo stack. This means
3509 that if it is called many times, a single undo command will undo
3510 all of them. It also does not check for the Undo command.
3512 void
3513 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3515 edit->force |= REDRAW_LINE;
3517 /* The next key press will unhighlight the found string, so update
3518 * the whole page */
3519 if (edit->found_len || edit->column_highlight)
3520 edit->force |= REDRAW_PAGE;
3522 switch (command)
3524 /* a mark command with shift-arrow */
3525 case CK_MarkLeft:
3526 case CK_MarkRight:
3527 case CK_MarkToWordBegin:
3528 case CK_MarkToWordEnd:
3529 case CK_MarkToHome:
3530 case CK_MarkToEnd:
3531 case CK_MarkUp:
3532 case CK_MarkDown:
3533 case CK_MarkPageUp:
3534 case CK_MarkPageDown:
3535 case CK_MarkToFileBegin:
3536 case CK_MarkToFileEnd:
3537 case CK_MarkToPageBegin:
3538 case CK_MarkToPageEnd:
3539 case CK_MarkScrollUp:
3540 case CK_MarkScrollDown:
3541 case CK_MarkParagraphUp:
3542 case CK_MarkParagraphDown:
3543 /* a mark command with alt-arrow */
3544 case CK_MarkColumnPageUp:
3545 case CK_MarkColumnPageDown:
3546 case CK_MarkColumnLeft:
3547 case CK_MarkColumnRight:
3548 case CK_MarkColumnUp:
3549 case CK_MarkColumnDown:
3550 case CK_MarkColumnScrollUp:
3551 case CK_MarkColumnScrollDown:
3552 case CK_MarkColumnParagraphUp:
3553 case CK_MarkColumnParagraphDown:
3554 edit->column_highlight = 0;
3555 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3557 edit_mark_cmd (edit, 1); /* clear */
3558 edit_mark_cmd (edit, 0); /* marking on */
3560 edit->highlight = 1;
3561 break;
3563 /* any other command */
3564 default:
3565 if (edit->highlight)
3566 edit_mark_cmd (edit, 0); /* clear */
3567 edit->highlight = 0;
3570 /* first check for undo */
3571 if (command == CK_Undo)
3573 edit->redo_stack_reset = 0;
3574 edit_group_undo (edit);
3575 edit->found_len = 0;
3576 edit->prev_col = edit_get_col (edit);
3577 edit->search_start = edit->curs1;
3578 return;
3580 /* check for redo */
3581 if (command == CK_Redo)
3583 edit->redo_stack_reset = 0;
3584 edit_do_redo (edit);
3585 edit->found_len = 0;
3586 edit->prev_col = edit_get_col (edit);
3587 edit->search_start = edit->curs1;
3588 return;
3591 edit->redo_stack_reset = 1;
3593 /* An ordinary key press */
3594 if (char_for_insertion >= 0)
3596 /* if non persistent selection and text selected */
3597 if (!option_persistent_selections)
3599 if (edit->mark1 != edit->mark2)
3600 edit_block_delete_cmd (edit);
3602 if (edit->overwrite)
3604 /* remove char only one time, after input first byte, multibyte chars */
3605 if ((!mc_global.utf8_display || edit->charpoint == 0)
3606 && edit_get_byte (edit, edit->curs1) != '\n')
3607 edit_delete (edit, 0);
3609 if (option_cursor_beyond_eol && edit->over_col > 0)
3610 edit_insert_over (edit);
3611 #ifdef HAVE_CHARSET
3612 if (char_for_insertion > 255 && !mc_global.utf8_display)
3614 unsigned char str[6 + 1];
3615 size_t i = 0;
3616 int res;
3618 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3619 if (res == 0)
3621 str[0] = '.';
3622 str[1] = '\0';
3624 else
3626 str[res] = '\0';
3628 while (str[i] != 0 && i <= 6)
3630 char_for_insertion = str[i];
3631 edit_insert (edit, char_for_insertion);
3632 i++;
3635 else
3636 #endif
3637 edit_insert (edit, char_for_insertion);
3639 if (option_auto_para_formatting)
3641 format_paragraph (edit, 0);
3642 edit->force |= REDRAW_PAGE;
3644 else
3645 check_and_wrap_line (edit);
3646 edit->found_len = 0;
3647 edit->prev_col = edit_get_col (edit);
3648 edit->search_start = edit->curs1;
3649 edit_find_bracket (edit);
3650 return;
3653 switch (command)
3655 case CK_TopOnScreen:
3656 case CK_BottomOnScreen:
3657 case CK_Top:
3658 case CK_Bottom:
3659 case CK_PageUp:
3660 case CK_PageDown:
3661 case CK_Home:
3662 case CK_End:
3663 case CK_Up:
3664 case CK_Down:
3665 case CK_Left:
3666 case CK_Right:
3667 case CK_WordLeft:
3668 case CK_WordRight:
3669 if (edit->mark2 >= 0)
3671 if (!option_persistent_selections)
3673 if (edit->column_highlight)
3674 edit_push_undo_action (edit, COLUMN_ON);
3675 edit->column_highlight = 0;
3676 edit_mark_cmd (edit, 1);
3681 switch (command)
3683 case CK_TopOnScreen:
3684 case CK_BottomOnScreen:
3685 case CK_MarkToPageBegin:
3686 case CK_MarkToPageEnd:
3687 case CK_Up:
3688 case CK_Down:
3689 case CK_WordLeft:
3690 case CK_WordRight:
3691 case CK_MarkToWordBegin:
3692 case CK_MarkToWordEnd:
3693 case CK_MarkUp:
3694 case CK_MarkDown:
3695 case CK_MarkColumnUp:
3696 case CK_MarkColumnDown:
3697 if (edit->mark2 == -1)
3698 break; /*marking is following the cursor: may need to highlight a whole line */
3699 case CK_Left:
3700 case CK_Right:
3701 case CK_MarkLeft:
3702 case CK_MarkRight:
3703 edit->force |= REDRAW_CHAR_ONLY;
3706 /* basic cursor key commands */
3707 switch (command)
3709 case CK_BackSpace:
3710 /* if non persistent selection and text selected */
3711 if (!option_persistent_selections)
3713 if (edit->mark1 != edit->mark2)
3715 edit_block_delete_cmd (edit);
3716 break;
3719 if (option_cursor_beyond_eol && edit->over_col > 0)
3721 edit->over_col--;
3722 break;
3724 if (option_backspace_through_tabs && is_in_indent (edit))
3726 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3727 edit_backspace (edit, 1);
3728 break;
3730 else
3732 if (option_fake_half_tabs)
3734 int i;
3735 if (is_in_indent (edit) && right_of_four_spaces (edit))
3737 for (i = 0; i < HALF_TAB_SIZE; i++)
3738 edit_backspace (edit, 1);
3739 break;
3743 edit_backspace (edit, 0);
3744 break;
3745 case CK_Delete:
3746 /* if non persistent selection and text selected */
3747 if (!option_persistent_selections)
3749 if (edit->mark1 != edit->mark2)
3751 edit_block_delete_cmd (edit);
3752 break;
3756 if (option_cursor_beyond_eol && edit->over_col > 0)
3757 edit_insert_over (edit);
3759 if (option_fake_half_tabs)
3761 int i;
3762 if (is_in_indent (edit) && left_of_four_spaces (edit))
3764 for (i = 1; i <= HALF_TAB_SIZE; i++)
3765 edit_delete (edit, 1);
3766 break;
3769 edit_delete (edit, 0);
3770 break;
3771 case CK_DeleteToWordBegin:
3772 edit->over_col = 0;
3773 edit_left_delete_word (edit);
3774 break;
3775 case CK_DeleteToWordEnd:
3776 if (option_cursor_beyond_eol && edit->over_col > 0)
3777 edit_insert_over (edit);
3779 edit_right_delete_word (edit);
3780 break;
3781 case CK_DeleteLine:
3782 edit_delete_line (edit);
3783 break;
3784 case CK_DeleteToHome:
3785 edit_delete_to_line_begin (edit);
3786 break;
3787 case CK_DeleteToEnd:
3788 edit_delete_to_line_end (edit);
3789 break;
3790 case CK_Enter:
3791 edit->over_col = 0;
3792 if (option_auto_para_formatting)
3794 edit_double_newline (edit);
3795 if (option_return_does_auto_indent)
3796 edit_auto_indent (edit);
3797 format_paragraph (edit, 0);
3799 else
3801 edit_insert (edit, '\n');
3802 if (option_return_does_auto_indent)
3804 edit_auto_indent (edit);
3807 break;
3808 case CK_Return:
3809 edit_insert (edit, '\n');
3810 break;
3812 case CK_MarkColumnPageUp:
3813 edit->column_highlight = 1;
3814 case CK_PageUp:
3815 case CK_MarkPageUp:
3816 edit_move_up (edit, edit->widget.lines - 1, 1);
3817 break;
3818 case CK_MarkColumnPageDown:
3819 edit->column_highlight = 1;
3820 case CK_PageDown:
3821 case CK_MarkPageDown:
3822 edit_move_down (edit, edit->widget.lines - 1, 1);
3823 break;
3824 case CK_MarkColumnLeft:
3825 edit->column_highlight = 1;
3826 case CK_Left:
3827 case CK_MarkLeft:
3828 if (option_fake_half_tabs)
3830 if (is_in_indent (edit) && right_of_four_spaces (edit))
3832 if (option_cursor_beyond_eol && edit->over_col > 0)
3833 edit->over_col--;
3834 else
3835 edit_cursor_move (edit, -HALF_TAB_SIZE);
3836 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3837 break;
3840 edit_left_char_move_cmd (edit);
3841 break;
3842 case CK_MarkColumnRight:
3843 edit->column_highlight = 1;
3844 case CK_Right:
3845 case CK_MarkRight:
3846 if (option_fake_half_tabs)
3848 if (is_in_indent (edit) && left_of_four_spaces (edit))
3850 edit_cursor_move (edit, HALF_TAB_SIZE);
3851 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3852 break;
3855 edit_right_char_move_cmd (edit);
3856 break;
3857 case CK_TopOnScreen:
3858 case CK_MarkToPageBegin:
3859 edit_begin_page (edit);
3860 break;
3861 case CK_BottomOnScreen:
3862 case CK_MarkToPageEnd:
3863 edit_end_page (edit);
3864 break;
3865 case CK_WordLeft:
3866 case CK_MarkToWordBegin:
3867 edit->over_col = 0;
3868 edit_left_word_move_cmd (edit);
3869 break;
3870 case CK_WordRight:
3871 case CK_MarkToWordEnd:
3872 edit->over_col = 0;
3873 edit_right_word_move_cmd (edit);
3874 break;
3875 case CK_MarkColumnUp:
3876 edit->column_highlight = 1;
3877 case CK_Up:
3878 case CK_MarkUp:
3879 edit_move_up (edit, 1, 0);
3880 break;
3881 case CK_MarkColumnDown:
3882 edit->column_highlight = 1;
3883 case CK_Down:
3884 case CK_MarkDown:
3885 edit_move_down (edit, 1, 0);
3886 break;
3887 case CK_MarkColumnParagraphUp:
3888 edit->column_highlight = 1;
3889 case CK_ParagraphUp:
3890 case CK_MarkParagraphUp:
3891 edit_move_up_paragraph (edit, 0);
3892 break;
3893 case CK_MarkColumnParagraphDown:
3894 edit->column_highlight = 1;
3895 case CK_ParagraphDown:
3896 case CK_MarkParagraphDown:
3897 edit_move_down_paragraph (edit, 0);
3898 break;
3899 case CK_MarkColumnScrollUp:
3900 edit->column_highlight = 1;
3901 case CK_ScrollUp:
3902 case CK_MarkScrollUp:
3903 edit_move_up (edit, 1, 1);
3904 break;
3905 case CK_MarkColumnScrollDown:
3906 edit->column_highlight = 1;
3907 case CK_ScrollDown:
3908 case CK_MarkScrollDown:
3909 edit_move_down (edit, 1, 1);
3910 break;
3911 case CK_Home:
3912 case CK_MarkToHome:
3913 edit_cursor_to_bol (edit);
3914 break;
3915 case CK_End:
3916 case CK_MarkToEnd:
3917 edit_cursor_to_eol (edit);
3918 break;
3919 case CK_Tab:
3920 /* if text marked shift block */
3921 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3923 if (edit->mark2 < 0)
3924 edit_mark_cmd (edit, 0);
3925 edit_move_block_to_right (edit);
3927 else
3929 if (option_cursor_beyond_eol)
3930 edit_insert_over (edit);
3931 edit_tab_cmd (edit);
3932 if (option_auto_para_formatting)
3934 format_paragraph (edit, 0);
3935 edit->force |= REDRAW_PAGE;
3937 else
3939 check_and_wrap_line (edit);
3942 break;
3944 case CK_InsertOverwrite:
3945 edit->overwrite = !edit->overwrite;
3946 break;
3948 case CK_Mark:
3949 if (edit->mark2 >= 0)
3951 if (edit->column_highlight)
3952 edit_push_undo_action (edit, COLUMN_ON);
3953 edit->column_highlight = 0;
3955 edit_mark_cmd (edit, 0);
3956 break;
3957 case CK_MarkColumn:
3958 if (!edit->column_highlight)
3959 edit_push_undo_action (edit, COLUMN_OFF);
3960 edit->column_highlight = 1;
3961 edit_mark_cmd (edit, 0);
3962 break;
3963 case CK_MarkAll:
3964 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3965 edit->force |= REDRAW_PAGE;
3966 break;
3967 case CK_Unmark:
3968 if (edit->column_highlight)
3969 edit_push_undo_action (edit, COLUMN_ON);
3970 edit->column_highlight = 0;
3971 edit_mark_cmd (edit, 1);
3972 break;
3973 case CK_MarkWord:
3974 if (edit->column_highlight)
3975 edit_push_undo_action (edit, COLUMN_ON);
3976 edit->column_highlight = 0;
3977 edit_mark_current_word_cmd (edit);
3978 break;
3979 case CK_MarkLine:
3980 if (edit->column_highlight)
3981 edit_push_undo_action (edit, COLUMN_ON);
3982 edit->column_highlight = 0;
3983 edit_mark_current_line_cmd (edit);
3984 break;
3986 case CK_ShowNumbers:
3987 option_line_state = !option_line_state;
3988 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
3989 edit->force |= REDRAW_PAGE;
3990 break;
3992 case CK_ShowMargin:
3993 show_right_margin = !show_right_margin;
3994 edit->force |= REDRAW_PAGE;
3995 break;
3997 case CK_Bookmark:
3998 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3999 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4000 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4001 else
4002 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4003 break;
4004 case CK_BookmarkFlush:
4005 book_mark_flush (edit, BOOK_MARK_COLOR);
4006 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4007 edit->force |= REDRAW_PAGE;
4008 break;
4009 case CK_BookmarkNext:
4010 if (edit->book_mark)
4012 struct _book_mark *p;
4013 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4014 if (p->next)
4016 p = p->next;
4017 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4018 edit_move_display (edit, p->line - edit->widget.lines / 2);
4019 edit_move_to_line (edit, p->line);
4022 break;
4023 case CK_BookmarkPrev:
4024 if (edit->book_mark)
4026 struct _book_mark *p;
4027 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4028 while (p->line == edit->curs_line)
4029 if (p->prev)
4030 p = p->prev;
4031 if (p->line >= 0)
4033 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4034 edit_move_display (edit, p->line - edit->widget.lines / 2);
4035 edit_move_to_line (edit, p->line);
4038 break;
4040 case CK_Top:
4041 case CK_MarkToFileBegin:
4042 edit_move_to_top (edit);
4043 break;
4044 case CK_Bottom:
4045 case CK_MarkToFileEnd:
4046 edit_move_to_bottom (edit);
4047 break;
4049 case CK_Copy:
4050 if (option_cursor_beyond_eol && edit->over_col > 0)
4051 edit_insert_over (edit);
4052 edit_block_copy_cmd (edit);
4053 break;
4054 case CK_Remove:
4055 edit_block_delete_cmd (edit);
4056 break;
4057 case CK_Move:
4058 edit_block_move_cmd (edit);
4059 break;
4061 case CK_BlockShiftLeft:
4062 if (edit->mark1 != edit->mark2)
4063 edit_move_block_to_left (edit);
4064 break;
4065 case CK_BlockShiftRight:
4066 if (edit->mark1 != edit->mark2)
4067 edit_move_block_to_right (edit);
4068 break;
4069 case CK_Store:
4070 edit_copy_to_X_buf_cmd (edit);
4071 break;
4072 case CK_Cut:
4073 edit_cut_to_X_buf_cmd (edit);
4074 break;
4075 case CK_Paste:
4076 /* if non persistent selection and text selected */
4077 if (!option_persistent_selections)
4079 if (edit->mark1 != edit->mark2)
4080 edit_block_delete_cmd (edit);
4082 if (option_cursor_beyond_eol && edit->over_col > 0)
4083 edit_insert_over (edit);
4084 edit_paste_from_X_buf_cmd (edit);
4085 break;
4086 case CK_History:
4087 edit_paste_from_history (edit);
4088 break;
4090 case CK_SaveAs:
4091 edit_save_as_cmd (edit);
4092 break;
4093 case CK_Save:
4094 edit_save_confirm_cmd (edit);
4095 break;
4096 case CK_EditFile:
4097 edit_load_cmd (edit, EDIT_FILE_COMMON);
4098 break;
4099 case CK_BlockSave:
4100 edit_save_block_cmd (edit);
4101 break;
4102 case CK_InsertFile:
4103 edit_insert_file_cmd (edit);
4104 break;
4106 case CK_FilePrev:
4107 edit_load_back_cmd (edit);
4108 break;
4109 case CK_FileNext:
4110 edit_load_forward_cmd (edit);
4111 break;
4113 case CK_EditSyntaxFile:
4114 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4115 break;
4116 case CK_SyntaxChoose:
4117 edit_syntax_dialog (edit);
4118 break;
4120 case CK_EditUserMenu:
4121 edit_load_cmd (edit, EDIT_FILE_MENU);
4122 break;
4124 case CK_SyntaxOnOff:
4125 option_syntax_highlighting ^= 1;
4126 if (option_syntax_highlighting == 1)
4127 edit_load_syntax (edit, NULL, edit->syntax_type);
4128 edit->force |= REDRAW_PAGE;
4129 break;
4131 case CK_ShowTabTws:
4132 enable_show_tabs_tws ^= 1;
4133 edit->force |= REDRAW_PAGE;
4134 break;
4136 case CK_Search:
4137 edit_search_cmd (edit, FALSE);
4138 break;
4139 case CK_SearchContinue:
4140 edit_search_cmd (edit, TRUE);
4141 break;
4142 case CK_Replace:
4143 edit_replace_cmd (edit, 0);
4144 break;
4145 case CK_ReplaceContinue:
4146 edit_replace_cmd (edit, 1);
4147 break;
4148 case CK_Complete:
4149 /* if text marked shift block */
4150 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4152 edit_move_block_to_left (edit);
4154 else
4156 edit_complete_word_cmd (edit);
4158 break;
4159 case CK_Find:
4160 edit_get_match_keyword_cmd (edit);
4161 break;
4162 case CK_Quit:
4163 dlg_stop (edit->widget.owner);
4164 break;
4165 case CK_EditNew:
4166 edit_new_cmd (edit);
4167 break;
4168 case CK_Help:
4169 edit_help_cmd (edit);
4170 break;
4171 case CK_Refresh:
4172 edit_refresh_cmd (edit);
4173 break;
4174 case CK_SaveSetup:
4175 save_setup_cmd ();
4176 break;
4177 case CK_About:
4178 edit_about ();
4179 break;
4180 case CK_LearnKeys:
4181 learn_keys ();
4182 break;
4183 case CK_Options:
4184 edit_options_dialog (edit);
4185 break;
4186 case CK_OptionsSaveMode:
4187 menu_save_mode_cmd ();
4188 break;
4189 case CK_Date:
4191 char s[BUF_MEDIUM];
4192 /* fool gcc to prevent a Y2K warning */
4193 char time_format[] = "_c";
4194 time_format[0] = '%';
4196 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4197 edit_print_string (edit, s);
4198 edit->force |= REDRAW_PAGE;
4199 break;
4201 break;
4202 case CK_Goto:
4203 edit_goto_cmd (edit);
4204 break;
4205 case CK_ParagraphFormat:
4206 format_paragraph (edit, 1);
4207 edit->force |= REDRAW_PAGE;
4208 break;
4209 case CK_MacroDelete:
4210 edit_delete_macro_cmd (edit);
4211 break;
4212 case CK_MatchBracket:
4213 edit_goto_matching_bracket (edit);
4214 break;
4215 case CK_UserMenu:
4216 user_menu (edit, NULL, -1);
4217 break;
4218 case CK_Sort:
4219 edit_sort_cmd (edit);
4220 break;
4221 case CK_ExternalCommand:
4222 edit_ext_cmd (edit);
4223 break;
4224 case CK_Mail:
4225 edit_mail_dialog (edit);
4226 break;
4227 case CK_Shell:
4228 view_other_cmd ();
4229 break;
4230 #ifdef HAVE_CHARSET
4231 case CK_SelectCodepage:
4232 edit_select_codepage_cmd (edit);
4233 break;
4234 #endif
4235 case CK_InsertLiteral:
4236 edit_insert_literal_cmd (edit);
4237 break;
4238 case CK_MacroStartStopRecord:
4239 edit_begin_end_macro_cmd (edit);
4240 break;
4241 case CK_RepeatStartStopRecord:
4242 edit_begin_end_repeat_cmd (edit);
4243 break;
4244 case CK_ExtendedKeyMap:
4245 edit->extmod = TRUE;
4246 break;
4247 default:
4248 break;
4251 /* CK_PipeBlock */
4252 if ((command / CK_PipeBlock (0)) == 1)
4253 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4255 /* keys which must set the col position, and the search vars */
4256 switch (command)
4258 case CK_Search:
4259 case CK_SearchContinue:
4260 case CK_Replace:
4261 case CK_ReplaceContinue:
4262 case CK_Complete:
4263 edit->prev_col = edit_get_col (edit);
4264 break;
4265 case CK_Up:
4266 case CK_MarkUp:
4267 case CK_MarkColumnUp:
4268 case CK_Down:
4269 case CK_MarkDown:
4270 case CK_MarkColumnDown:
4271 case CK_PageUp:
4272 case CK_MarkPageUp:
4273 case CK_MarkColumnPageUp:
4274 case CK_PageDown:
4275 case CK_MarkPageDown:
4276 case CK_MarkColumnPageDown:
4277 case CK_Top:
4278 case CK_MarkToFileBegin:
4279 case CK_Bottom:
4280 case CK_MarkToFileEnd:
4281 case CK_ParagraphUp:
4282 case CK_MarkParagraphUp:
4283 case CK_MarkColumnParagraphUp:
4284 case CK_ParagraphDown:
4285 case CK_MarkParagraphDown:
4286 case CK_MarkColumnParagraphDown:
4287 case CK_ScrollUp:
4288 case CK_MarkScrollUp:
4289 case CK_MarkColumnScrollUp:
4290 case CK_ScrollDown:
4291 case CK_MarkScrollDown:
4292 case CK_MarkColumnScrollDown:
4293 edit->search_start = edit->curs1;
4294 edit->found_len = 0;
4295 break;
4296 default:
4297 edit->found_len = 0;
4298 edit->prev_col = edit_get_col (edit);
4299 edit->search_start = edit->curs1;
4301 edit_find_bracket (edit);
4303 if (option_auto_para_formatting)
4305 switch (command)
4307 case CK_BackSpace:
4308 case CK_Delete:
4309 case CK_DeleteToWordBegin:
4310 case CK_DeleteToWordEnd:
4311 case CK_DeleteToHome:
4312 case CK_DeleteToEnd:
4313 format_paragraph (edit, 0);
4314 edit->force |= REDRAW_PAGE;
4319 /* --------------------------------------------------------------------------------------------- */
4321 void
4322 edit_stack_init (void)
4324 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4326 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4327 edit_history_moveto[edit_stack_iterator].line = -1;
4330 edit_stack_iterator = 0;
4333 /* --------------------------------------------------------------------------------------------- */
4335 void
4336 edit_stack_free (void)
4338 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4339 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4342 /* --------------------------------------------------------------------------------------------- */
4343 /** move i lines */
4345 void
4346 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4348 edit_move_updown (edit, i, do_scroll, TRUE);
4351 /* --------------------------------------------------------------------------------------------- */
4352 /** move i lines */
4354 void
4355 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4357 edit_move_updown (edit, i, do_scroll, FALSE);
4360 /* --------------------------------------------------------------------------------------------- */