Ticket #2372 (Editor sometimes shows multibyte UTF-8 chars as two dots)
[midnight-commander.git] / src / editor / edit.c
blobeffca4a97b688e9a73ebfd75d1f22e594e3851b6
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor low level data handling and cursor fundamentals
30 * \author Paul Sheer
31 * \date 1996, 1997
34 #include <config.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
44 #include <fcntl.h>
46 #include "lib/global.h"
48 #include "lib/tty/color.h"
49 #include "lib/tty/tty.h" /* attrset() */
50 #include "lib/tty/key.h" /* is_idle() */
51 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "lib/lock.h"
57 #include "lib/widget.h"
59 #ifdef HAVE_CHARSET
60 #include "lib/charsets.h" /* get_codepage_id */
61 #endif
63 #include "src/filemanager/cmd.h" /* view_other_cmd() */
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/learn.h" /* learn_keys */
68 #include "src/keybind-defaults.h"
70 #include "edit-impl.h"
71 #include "edit-widget.h"
73 /*** global variables ****************************************************************************/
75 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
76 int option_typewriter_wrap = 0;
77 int option_auto_para_formatting = 0;
78 int option_fill_tabs_with_spaces = 0;
79 int option_return_does_auto_indent = 1;
80 int option_backspace_through_tabs = 0;
81 int option_fake_half_tabs = 1;
82 int option_save_mode = EDIT_QUICK_SAVE;
83 int option_save_position = 1;
84 int option_max_undo = 32768;
85 int option_persistent_selections = 1;
86 int option_cursor_beyond_eol = 0;
87 int option_line_state = 0;
88 int option_line_state_width = 0;
90 int option_edit_right_extreme = 0;
91 int option_edit_left_extreme = 0;
92 int option_edit_top_extreme = 0;
93 int option_edit_bottom_extreme = 0;
94 int enable_show_tabs_tws = 1;
95 int option_check_nl_at_eof = 0;
96 int option_group_undo = 0;
97 int show_right_margin = 0;
99 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
100 char *option_backup_ext = NULL;
102 int edit_stack_iterator = 0;
103 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
104 /* magic sequense for say than block is vertical */
105 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
107 /*** file scope macro definitions ****************************************************************/
109 #define TEMP_BUF_LEN 1024
111 #define space_width 1
113 /*** file scope type declarations ****************************************************************/
115 /*** file scope variables ************************************************************************/
117 /* detecting an error on save is easy: just check if every byte has been written. */
118 /* detecting an error on read, is not so easy 'cos there is not way to tell
119 whether you read everything or not. */
120 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
121 static const struct edit_filters
123 const char *read, *write, *extension;
124 } all_filters[] =
126 /* *INDENT-OFF* */
127 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
128 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
129 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
130 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
132 /* *INDENT-ON* */
135 static long last_bracket = -1;
137 /*** file scope functions ************************************************************************/
138 /* --------------------------------------------------------------------------------------------- */
142 * here's a quick sketch of the layout: (don't run this through indent.)
144 * (b1 is buffers1 and b2 is buffers2)
147 * \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
148 * ______________________________________|______________________________________
150 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
151 * |-> |-> |-> |-> |-> |-> |
153 * _<------------------------->|<----------------->_
154 * WEdit->curs2 | WEdit->curs1
155 * ^ | ^
156 * | ^|^ |
157 * cursor ||| cursor
158 * |||
159 * file end|||file beginning
164 * This_is_some_file
165 * fin.
168 /* --------------------------------------------------------------------------------------------- */
170 static int left_of_four_spaces (WEdit * edit);
172 /* --------------------------------------------------------------------------------------------- */
174 static void
175 edit_about (void)
177 const char *header = N_("About");
178 const char *button_name = N_("&OK");
179 const char *const version = "MCEdit " VERSION;
180 char text[BUF_LARGE];
182 int win_len, version_len, button_len;
183 int cols, lines;
185 Dlg_head *about_dlg;
187 #ifdef ENABLE_NLS
188 header = _(header);
189 button_name = _(button_name);
190 #endif
192 button_len = str_term_width1 (button_name) + 5;
193 version_len = str_term_width1 (version);
195 g_snprintf (text, sizeof (text),
196 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
197 " A user friendly text editor\n"
198 " written for the Midnight Commander"));
200 win_len = str_term_width1 (header);
201 win_len = max (win_len, version_len);
202 win_len = max (win_len, button_len);
204 /* count width and height of text */
205 str_msg_term_size (text, &lines, &cols);
206 lines += 9;
207 cols = max (win_len, cols) + 6;
209 /* dialog */
210 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
211 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
213 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
214 add_widget (about_dlg, label_new (5, 3, text));
215 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
216 B_ENTER, NORMAL_BUTTON, button_name, NULL));
218 run_dlg (about_dlg);
219 destroy_dlg (about_dlg);
222 /* --------------------------------------------------------------------------------------------- */
224 * Initialize the buffers for an empty files.
227 static void
228 edit_init_buffers (WEdit * edit)
230 int j;
232 for (j = 0; j <= MAXBUFF; j++)
234 edit->buffers1[j] = NULL;
235 edit->buffers2[j] = NULL;
238 edit->curs1 = 0;
239 edit->curs2 = 0;
240 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
243 /* --------------------------------------------------------------------------------------------- */
245 * Load file OR text into buffers. Set cursor to the beginning of file.
246 * @returns 1 on error.
249 static int
250 edit_load_file_fast (WEdit * edit, const char *filename)
252 long buf, buf2;
253 int file = -1;
254 int ret = 1;
256 edit->curs2 = edit->last_byte;
257 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
259 file = mc_open (filename, O_RDONLY | O_BINARY);
260 if (file == -1)
262 gchar *errmsg;
264 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
265 edit_error_dialog (_("Error"), errmsg);
266 g_free (errmsg);
267 return 1;
270 if (!edit->buffers2[buf2])
271 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
275 if (mc_read (file,
276 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
277 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
278 break;
280 for (buf = buf2 - 1; buf >= 0; buf--)
282 /* edit->buffers2[0] is already allocated */
283 if (!edit->buffers2[buf])
284 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
285 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
286 break;
288 ret = 0;
290 while (0);
291 if (ret)
293 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
294 edit_error_dialog (_("Error"), err_str);
295 g_free (err_str);
297 mc_close (file);
298 return ret;
301 /* --------------------------------------------------------------------------------------------- */
302 /** Return index of the filter or -1 is there is no appropriate filter */
304 static int
305 edit_find_filter (const char *filename)
307 size_t i, l, e;
309 if (filename == NULL)
310 return -1;
312 l = strlen (filename);
313 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
315 e = strlen (all_filters[i].extension);
316 if (l > e)
317 if (!strcmp (all_filters[i].extension, filename + l - e))
318 return i;
320 return -1;
323 /* --------------------------------------------------------------------------------------------- */
325 static char *
326 edit_get_filter (const char *filename)
328 int i;
329 char *p, *quoted_name;
331 i = edit_find_filter (filename);
332 if (i < 0)
333 return NULL;
335 quoted_name = name_quote (filename, 0);
336 p = g_strdup_printf (all_filters[i].read, quoted_name);
337 g_free (quoted_name);
338 return p;
341 /* --------------------------------------------------------------------------------------------- */
343 static long
344 edit_insert_stream (WEdit * edit, FILE * f)
346 int c;
347 long i = 0;
348 while ((c = fgetc (f)) >= 0)
350 edit_insert (edit, c);
351 i++;
353 return i;
356 /* --------------------------------------------------------------------------------------------- */
357 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
359 static int
360 check_file_access (WEdit * edit, const char *filename, struct stat *st)
362 int file;
363 gchar *errmsg = NULL;
365 /* Try opening an existing file */
366 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
368 if (file < 0)
371 * Try creating the file. O_EXCL prevents following broken links
372 * and opening existing files.
374 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
375 if (file < 0)
377 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
378 goto cleanup;
380 else
382 /* New file, delete it if it's not modified or saved */
383 edit->delete_file = 1;
387 /* Check what we have opened */
388 if (mc_fstat (file, st) < 0)
390 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
391 goto cleanup;
394 /* We want to open regular files only */
395 if (!S_ISREG (st->st_mode))
397 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
398 goto cleanup;
402 * Don't delete non-empty files.
403 * O_EXCL should prevent it, but let's be on the safe side.
405 if (st->st_size > 0)
406 edit->delete_file = 0;
408 if (st->st_size >= SIZE_LIMIT)
409 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
411 cleanup:
412 (void) mc_close (file);
414 if (errmsg != NULL)
416 edit_error_dialog (_("Error"), errmsg);
417 g_free (errmsg);
418 return 1;
420 return 0;
423 /* --------------------------------------------------------------------------------------------- */
425 * Open the file and load it into the buffers, either directly or using
426 * a filter. Return 0 on success, 1 on error.
428 * Fast loading (edit_load_file_fast) is used when the file size is
429 * known. In this case the data is read into the buffers by blocks.
430 * If the file size is not known, the data is loaded byte by byte in
431 * edit_insert_file.
434 static int
435 edit_load_file (WEdit * edit)
437 int fast_load = 1;
438 vfs_path_t *vpath = vfs_path_from_str (edit->filename);
440 /* Cannot do fast load if a filter is used */
441 if (edit_find_filter (edit->filename) >= 0)
442 fast_load = 0;
445 * VFS may report file size incorrectly, and slow load is not a big
446 * deal considering overhead in VFS.
448 if (!vfs_file_is_local (vpath))
449 fast_load = 0;
450 vfs_path_free (vpath);
453 * FIXME: line end translation should disable fast loading as well
454 * Consider doing fseek() to the end and ftell() for the real size.
457 if (*edit->filename)
459 /* If we are dealing with a real file, check that it exists */
460 if (check_file_access (edit, edit->filename, &edit->stat1))
461 return 1;
463 else
465 /* nothing to load */
466 fast_load = 0;
469 edit_init_buffers (edit);
471 if (fast_load)
473 edit->last_byte = edit->stat1.st_size;
474 edit_load_file_fast (edit, edit->filename);
475 /* If fast load was used, the number of lines wasn't calculated */
476 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
478 else
480 edit->last_byte = 0;
481 if (*edit->filename)
483 edit->undo_stack_disable = 1;
484 if (edit_insert_file (edit, edit->filename) == 0)
486 edit_clean (edit);
487 return 1;
489 edit->undo_stack_disable = 0;
492 edit->lb = LB_ASIS;
493 return 0;
496 /* --------------------------------------------------------------------------------------------- */
497 /** Restore saved cursor position in the file */
499 static void
500 edit_load_position (WEdit * edit)
502 char *filename;
503 long line, column;
504 off_t offset;
505 vfs_path_t *vpath;
507 if (!edit->filename || !*edit->filename)
508 return;
510 vpath = vfs_path_from_str (edit->filename);
511 filename = vfs_path_to_str (vpath);
512 load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
513 vfs_path_free (vpath);
514 g_free (filename);
516 if (line > 0)
518 edit_move_to_line (edit, line - 1);
519 edit->prev_col = column;
521 else if (offset > 0)
523 edit_cursor_move (edit, offset);
524 line = edit->curs_line;
525 edit->search_start = edit->curs1;
528 book_mark_restore (edit, BOOK_MARK_COLOR);
530 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
531 edit_move_display (edit, line - (edit->widget.lines / 2));
534 /* --------------------------------------------------------------------------------------------- */
535 /** Save cursor position in the file */
537 static void
538 edit_save_position (WEdit * edit)
540 char *filename;
541 vfs_path_t *vpath;
543 if (edit->filename == NULL || *edit->filename == '\0')
544 return;
546 vpath = vfs_path_from_str (edit->filename);
547 filename = vfs_path_to_str (vpath);
549 book_mark_serialize (edit, BOOK_MARK_COLOR);
550 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
551 edit->serialized_bookmarks);
552 edit->serialized_bookmarks = NULL;
554 g_free (filename);
555 vfs_path_free (vpath);
558 /* --------------------------------------------------------------------------------------------- */
559 /** Clean the WEdit stricture except the widget part */
561 static void
562 edit_purge_widget (WEdit * edit)
564 size_t len = sizeof (WEdit) - sizeof (Widget);
565 char *start = (char *) edit + sizeof (Widget);
566 memset (start, 0, len);
569 /* --------------------------------------------------------------------------------------------- */
572 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
573 then the file should be as it was when he loaded up. Then set edit->modified to 0.
576 static long
577 edit_pop_undo_action (WEdit * edit)
579 long c;
580 unsigned long sp = edit->undo_stack_pointer;
582 if (sp == edit->undo_stack_bottom)
583 return STACK_BOTTOM;
585 sp = (sp - 1) & edit->undo_stack_size_mask;
586 c = edit->undo_stack[sp];
587 if (c >= 0)
589 /* edit->undo_stack[sp] = '@'; */
590 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
591 return c;
594 if (sp == edit->undo_stack_bottom)
595 return STACK_BOTTOM;
597 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
598 if (edit->undo_stack[sp] == -2)
600 /* edit->undo_stack[sp] = '@'; */
601 edit->undo_stack_pointer = sp;
603 else
604 edit->undo_stack[sp]++;
606 return c;
609 static long
610 edit_pop_redo_action (WEdit * edit)
612 long c;
613 unsigned long sp = edit->redo_stack_pointer;
615 if (sp == edit->redo_stack_bottom)
616 return STACK_BOTTOM;
618 sp = (sp - 1) & edit->redo_stack_size_mask;
619 c = edit->redo_stack[sp];
620 if (c >= 0)
622 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
623 return c;
626 if (sp == edit->redo_stack_bottom)
627 return STACK_BOTTOM;
629 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
630 if (edit->redo_stack[sp] == -2)
631 edit->redo_stack_pointer = sp;
632 else
633 edit->redo_stack[sp]++;
635 return c;
638 static long
639 get_prev_undo_action (WEdit * edit)
641 long c;
642 unsigned long sp = edit->undo_stack_pointer;
644 if (sp == edit->undo_stack_bottom)
645 return STACK_BOTTOM;
647 sp = (sp - 1) & edit->undo_stack_size_mask;
648 c = edit->undo_stack[sp];
649 if (c >= 0)
650 return c;
652 if (sp == edit->undo_stack_bottom)
653 return STACK_BOTTOM;
655 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
656 return c;
659 /* --------------------------------------------------------------------------------------------- */
660 /** is called whenever a modification is made by one of the four routines below */
662 static void
663 edit_modification (WEdit * edit)
665 edit->caches_valid = 0;
667 /* raise lock when file modified */
668 if (!edit->modified && !edit->delete_file)
669 edit->locked = edit_lock_file (edit);
670 edit->modified = 1;
673 /* --------------------------------------------------------------------------------------------- */
675 static char *
676 edit_get_byte_ptr (WEdit * edit, long byte_index)
678 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
679 return NULL;
681 if (byte_index >= edit->curs1)
683 unsigned long p;
685 p = edit->curs1 + edit->curs2 - byte_index - 1;
686 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
687 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
690 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
691 (byte_index & M_EDIT_BUF_SIZE));
694 /* --------------------------------------------------------------------------------------------- */
696 static char *
697 edit_get_buf_ptr (WEdit * edit, long byte_index)
699 if (byte_index >= (edit->curs1 + edit->curs2))
700 byte_index--;
702 if (byte_index < 0)
703 return NULL;
705 if (byte_index >= edit->curs1)
707 unsigned long p;
709 p = edit->curs1 + edit->curs2 - 1;
710 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
711 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
714 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
717 /* --------------------------------------------------------------------------------------------- */
719 static int
720 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
722 gchar *str, *buf = NULL;
723 int res = -1;
724 gunichar ch;
725 gchar *next_ch = NULL;
726 int width = 0;
728 if (byte_index > 0)
729 byte_index--;
731 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
733 *char_width = 0;
734 return 0;
737 ch = edit_get_utf (edit, byte_index, &width);
739 if (width == 1)
741 *char_width = width;
742 return ch;
745 str = edit_get_byte_ptr (edit, byte_index);
746 buf = edit_get_buf_ptr (edit, byte_index);
747 if (str == NULL || buf == NULL)
749 *char_width = 0;
750 return 0;
752 /* get prev utf8 char */
753 if (str != buf)
754 str = g_utf8_find_prev_char (buf, str);
756 if (str == NULL)
758 *char_width = 0;
759 return 0;
761 else
762 res = g_utf8_get_char_validated (str, -1);
764 if (res < 0)
766 ch = *str;
767 width = 0;
769 else
771 ch = res;
772 /* Calculate UTF-8 char width */
773 next_ch = g_utf8_next_char (str);
774 if (next_ch)
776 width = next_ch - str;
778 else
780 ch = 0;
781 width = 0;
784 *char_width = width;
785 return ch;
788 /* --------------------------------------------------------------------------------------------- */
790 static int
791 edit_backspace (WEdit * edit, const int byte_delete)
793 int p = 0;
794 int cw = 1;
795 int i;
797 if (!edit->curs1)
798 return 0;
800 cw = 1;
802 if (edit->mark2 != edit->mark1)
803 edit_push_markers (edit);
805 if (edit->utf8 && byte_delete == 0)
807 edit_get_prev_utf (edit, edit->curs1, &cw);
808 if (cw < 1)
809 cw = 1;
811 for (i = 1; i <= cw; i++)
813 if (edit->mark1 >= edit->curs1)
815 edit->mark1--;
816 edit->end_mark_curs--;
818 if (edit->mark2 >= edit->curs1)
819 edit->mark2--;
820 if (edit->last_get_rule >= edit->curs1)
821 edit->last_get_rule--;
823 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
824 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
825 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
827 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
828 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
830 edit->last_byte--;
831 edit->curs1--;
832 edit_push_undo_action (edit, p);
834 edit_modification (edit);
835 if (p == '\n')
837 if (edit->book_mark)
838 book_mark_dec (edit, edit->curs_line);
839 edit->curs_line--;
840 edit->total_lines--;
841 edit->force |= REDRAW_AFTER_CURSOR;
844 if (edit->curs1 < edit->start_display)
846 edit->start_display--;
847 if (p == '\n')
848 edit->start_line--;
851 return p;
854 /* --------------------------------------------------------------------------------------------- */
855 /* high level cursor movement commands */
856 /* --------------------------------------------------------------------------------------------- */
858 static int
859 is_in_indent (WEdit * edit)
861 long p = edit_bol (edit, edit->curs1);
862 while (p < edit->curs1)
863 if (!strchr (" \t", edit_get_byte (edit, p++)))
864 return 0;
865 return 1;
868 /* --------------------------------------------------------------------------------------------- */
870 static int
871 is_blank (WEdit * edit, long offset)
873 long s, f;
874 int c;
875 s = edit_bol (edit, offset);
876 f = edit_eol (edit, offset) - 1;
877 while (s <= f)
879 c = edit_get_byte (edit, s++);
880 if (!isspace (c))
881 return 0;
883 return 1;
887 /* --------------------------------------------------------------------------------------------- */
888 /** returns the offset of line i */
890 static long
891 edit_find_line (WEdit * edit, int line)
893 int i, j = 0;
894 int m = 2000000000;
895 if (!edit->caches_valid)
897 for (i = 0; i < N_LINE_CACHES; i++)
898 edit->line_numbers[i] = edit->line_offsets[i] = 0;
899 /* three offsets that we *know* are line 0 at 0 and these two: */
900 edit->line_numbers[1] = edit->curs_line;
901 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
902 edit->line_numbers[2] = edit->total_lines;
903 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
904 edit->caches_valid = 1;
906 if (line >= edit->total_lines)
907 return edit->line_offsets[2];
908 if (line <= 0)
909 return 0;
910 /* find the closest known point */
911 for (i = 0; i < N_LINE_CACHES; i++)
913 int n;
914 n = abs (edit->line_numbers[i] - line);
915 if (n < m)
917 m = n;
918 j = i;
921 if (m == 0)
922 return edit->line_offsets[j]; /* know the offset exactly */
923 if (m == 1 && j >= 3)
924 i = j; /* one line different - caller might be looping, so stay in this cache */
925 else
926 i = 3 + (rand () % (N_LINE_CACHES - 3));
927 if (line > edit->line_numbers[j])
928 edit->line_offsets[i] =
929 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
930 else
931 edit->line_offsets[i] =
932 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
933 edit->line_numbers[i] = line;
934 return edit->line_offsets[i];
937 /* --------------------------------------------------------------------------------------------- */
938 /** moves up until a blank line is reached, or until just
939 before a non-blank line is reached */
941 static void
942 edit_move_up_paragraph (WEdit * edit, int do_scroll)
944 int i = 0;
945 if (edit->curs_line > 1)
947 if (line_is_blank (edit, edit->curs_line))
949 if (line_is_blank (edit, edit->curs_line - 1))
951 for (i = edit->curs_line - 1; i; i--)
952 if (!line_is_blank (edit, i))
954 i++;
955 break;
958 else
960 for (i = edit->curs_line - 1; i; i--)
961 if (line_is_blank (edit, i))
962 break;
965 else
967 for (i = edit->curs_line - 1; i; i--)
968 if (line_is_blank (edit, i))
969 break;
972 edit_move_up (edit, edit->curs_line - i, do_scroll);
975 /* --------------------------------------------------------------------------------------------- */
976 /** moves down until a blank line is reached, or until just
977 before a non-blank line is reached */
979 static void
980 edit_move_down_paragraph (WEdit * edit, int do_scroll)
982 int i;
983 if (edit->curs_line >= edit->total_lines - 1)
985 i = edit->total_lines;
987 else
989 if (line_is_blank (edit, edit->curs_line))
991 if (line_is_blank (edit, edit->curs_line + 1))
993 for (i = edit->curs_line + 1; i; i++)
994 if (!line_is_blank (edit, i) || i > edit->total_lines)
996 i--;
997 break;
1000 else
1002 for (i = edit->curs_line + 1; i; i++)
1003 if (line_is_blank (edit, i) || i >= edit->total_lines)
1004 break;
1007 else
1009 for (i = edit->curs_line + 1; i; i++)
1010 if (line_is_blank (edit, i) || i >= edit->total_lines)
1011 break;
1014 edit_move_down (edit, i - edit->curs_line, do_scroll);
1017 /* --------------------------------------------------------------------------------------------- */
1019 static void
1020 edit_begin_page (WEdit * edit)
1022 edit_update_curs_row (edit);
1023 edit_move_up (edit, edit->curs_row, 0);
1026 /* --------------------------------------------------------------------------------------------- */
1028 static void
1029 edit_end_page (WEdit * edit)
1031 edit_update_curs_row (edit);
1032 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
1036 /* --------------------------------------------------------------------------------------------- */
1037 /** goto beginning of text */
1039 static void
1040 edit_move_to_top (WEdit * edit)
1042 if (edit->curs_line)
1044 edit_cursor_move (edit, -edit->curs1);
1045 edit_move_to_prev_col (edit, 0);
1046 edit->force |= REDRAW_PAGE;
1047 edit->search_start = 0;
1048 edit_update_curs_row (edit);
1053 /* --------------------------------------------------------------------------------------------- */
1054 /** goto end of text */
1056 static void
1057 edit_move_to_bottom (WEdit * edit)
1059 if (edit->curs_line < edit->total_lines)
1061 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1062 edit->start_display = edit->last_byte;
1063 edit->start_line = edit->total_lines;
1064 edit_scroll_upward (edit, edit->widget.lines - 1);
1065 edit->force |= REDRAW_PAGE;
1069 /* --------------------------------------------------------------------------------------------- */
1070 /** goto beginning of line */
1072 static void
1073 edit_cursor_to_bol (WEdit * edit)
1075 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1076 edit->search_start = edit->curs1;
1077 edit->prev_col = edit_get_col (edit);
1078 edit->over_col = 0;
1081 /* --------------------------------------------------------------------------------------------- */
1082 /** goto end of line */
1084 static void
1085 edit_cursor_to_eol (WEdit * edit)
1087 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1088 edit->search_start = edit->curs1;
1089 edit->prev_col = edit_get_col (edit);
1090 edit->over_col = 0;
1093 /* --------------------------------------------------------------------------------------------- */
1095 static unsigned long
1096 my_type_of (int c)
1098 int x, r = 0;
1099 const char *p, *q;
1100 const char option_chars_move_whole_word[] =
1101 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1103 if (!c)
1104 return 0;
1105 if (c == '!')
1107 if (*option_chars_move_whole_word == '!')
1108 return 2;
1109 return 0x80000000UL;
1111 if (g_ascii_isupper ((gchar) c))
1112 c = 'A';
1113 else if (g_ascii_islower ((gchar) c))
1114 c = 'a';
1115 else if (g_ascii_isalpha (c))
1116 c = 'a';
1117 else if (isdigit (c))
1118 c = '0';
1119 else if (isspace (c))
1120 c = ' ';
1121 q = strchr (option_chars_move_whole_word, c);
1122 if (!q)
1123 return 0xFFFFFFFFUL;
1126 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1127 if (*p == '!')
1128 x <<= 1;
1129 r |= x;
1131 while ((q = strchr (q + 1, c)));
1132 return r;
1135 /* --------------------------------------------------------------------------------------------- */
1137 static void
1138 edit_left_word_move (WEdit * edit, int s)
1140 for (;;)
1142 int c1, c2;
1143 if (edit->column_highlight
1144 && edit->mark1 != edit->mark2
1145 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1146 break;
1147 edit_cursor_move (edit, -1);
1148 if (!edit->curs1)
1149 break;
1150 c1 = edit_get_byte (edit, edit->curs1 - 1);
1151 c2 = edit_get_byte (edit, edit->curs1);
1152 if (c1 == '\n' || c2 == '\n')
1153 break;
1154 if (!(my_type_of (c1) & my_type_of (c2)))
1155 break;
1156 if (isspace (c1) && !isspace (c2))
1157 break;
1158 if (s)
1159 if (!isspace (c1) && isspace (c2))
1160 break;
1164 /* --------------------------------------------------------------------------------------------- */
1166 static void
1167 edit_left_word_move_cmd (WEdit * edit)
1169 edit_left_word_move (edit, 0);
1170 edit->force |= REDRAW_PAGE;
1173 /* --------------------------------------------------------------------------------------------- */
1175 static void
1176 edit_right_word_move (WEdit * edit, int s)
1178 for (;;)
1180 int c1, c2;
1181 if (edit->column_highlight
1182 && edit->mark1 != edit->mark2
1183 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1184 break;
1185 edit_cursor_move (edit, 1);
1186 if (edit->curs1 >= edit->last_byte)
1187 break;
1188 c1 = edit_get_byte (edit, edit->curs1 - 1);
1189 c2 = edit_get_byte (edit, edit->curs1);
1190 if (c1 == '\n' || c2 == '\n')
1191 break;
1192 if (!(my_type_of (c1) & my_type_of (c2)))
1193 break;
1194 if (isspace (c1) && !isspace (c2))
1195 break;
1196 if (s)
1197 if (!isspace (c1) && isspace (c2))
1198 break;
1202 /* --------------------------------------------------------------------------------------------- */
1204 static void
1205 edit_right_word_move_cmd (WEdit * edit)
1207 edit_right_word_move (edit, 0);
1208 edit->force |= REDRAW_PAGE;
1211 /* --------------------------------------------------------------------------------------------- */
1213 static void
1214 edit_right_char_move_cmd (WEdit * edit)
1216 int cw = 1;
1217 int c = 0;
1218 if (edit->utf8)
1220 c = edit_get_utf (edit, edit->curs1, &cw);
1221 if (cw < 1)
1222 cw = 1;
1224 else
1226 c = edit_get_byte (edit, edit->curs1);
1228 if (option_cursor_beyond_eol && c == '\n')
1230 edit->over_col++;
1232 else
1234 edit_cursor_move (edit, cw);
1238 /* --------------------------------------------------------------------------------------------- */
1240 static void
1241 edit_left_char_move_cmd (WEdit * edit)
1243 int cw = 1;
1244 if (edit->column_highlight
1245 && option_cursor_beyond_eol
1246 && edit->mark1 != edit->mark2
1247 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1248 return;
1249 if (edit->utf8)
1251 edit_get_prev_utf (edit, edit->curs1, &cw);
1252 if (cw < 1)
1253 cw = 1;
1255 if (option_cursor_beyond_eol && edit->over_col > 0)
1257 edit->over_col--;
1259 else
1261 edit_cursor_move (edit, -cw);
1265 /* --------------------------------------------------------------------------------------------- */
1266 /** Up or down cursor moving.
1267 direction = TRUE - move up
1268 = FALSE - move down
1271 static void
1272 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1274 unsigned long p;
1275 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1277 if (i > l)
1278 i = l;
1280 if (i == 0)
1281 return;
1283 if (i > 1)
1284 edit->force |= REDRAW_PAGE;
1285 if (do_scroll)
1287 if (direction)
1288 edit_scroll_upward (edit, i);
1289 else
1290 edit_scroll_downward (edit, i);
1292 p = edit_bol (edit, edit->curs1);
1294 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1296 edit_cursor_move (edit, p - edit->curs1);
1298 edit_move_to_prev_col (edit, p);
1300 /* search start of current multibyte char (like CJK) */
1301 if (edit->curs1 + 1 < edit->last_byte)
1303 edit_right_char_move_cmd (edit);
1304 edit_left_char_move_cmd (edit);
1307 edit->search_start = edit->curs1;
1308 edit->found_len = 0;
1311 /* --------------------------------------------------------------------------------------------- */
1313 static void
1314 edit_right_delete_word (WEdit * edit)
1316 int c1, c2;
1317 for (;;)
1319 if (edit->curs1 >= edit->last_byte)
1320 break;
1321 c1 = edit_delete (edit, 1);
1322 c2 = edit_get_byte (edit, edit->curs1);
1323 if (c1 == '\n' || c2 == '\n')
1324 break;
1325 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1326 break;
1327 if (!(my_type_of (c1) & my_type_of (c2)))
1328 break;
1332 /* --------------------------------------------------------------------------------------------- */
1334 static void
1335 edit_left_delete_word (WEdit * edit)
1337 int c1, c2;
1338 for (;;)
1340 if (edit->curs1 <= 0)
1341 break;
1342 c1 = edit_backspace (edit, 1);
1343 c2 = edit_get_byte (edit, edit->curs1 - 1);
1344 if (c1 == '\n' || c2 == '\n')
1345 break;
1346 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1347 break;
1348 if (!(my_type_of (c1) & my_type_of (c2)))
1349 break;
1353 /* --------------------------------------------------------------------------------------------- */
1355 the start column position is not recorded, and hence does not
1356 undo as it happed. But who would notice.
1359 static void
1360 edit_do_undo (WEdit * edit)
1362 long ac;
1363 long count = 0;
1365 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1366 edit->over_col = 0;
1367 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1369 switch ((int) ac)
1371 case STACK_BOTTOM:
1372 goto done_undo;
1373 case CURS_RIGHT:
1374 edit_cursor_move (edit, 1);
1375 break;
1376 case CURS_LEFT:
1377 edit_cursor_move (edit, -1);
1378 break;
1379 case BACKSPACE:
1380 case BACKSPACE_BR:
1381 edit_backspace (edit, 1);
1382 break;
1383 case DELCHAR:
1384 case DELCHAR_BR:
1385 edit_delete (edit, 1);
1386 break;
1387 case COLUMN_ON:
1388 edit->column_highlight = 1;
1389 break;
1390 case COLUMN_OFF:
1391 edit->column_highlight = 0;
1392 break;
1394 if (ac >= 256 && ac < 512)
1395 edit_insert_ahead (edit, ac - 256);
1396 if (ac >= 0 && ac < 256)
1397 edit_insert (edit, ac);
1399 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1401 edit->mark1 = ac - MARK_1;
1402 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1404 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1406 edit->mark2 = ac - MARK_2;
1407 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1409 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1411 edit->end_mark_curs = ac - MARK_CURS;
1413 if (count++)
1414 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1417 if (edit->start_display > ac - KEY_PRESS)
1419 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1420 edit->force |= REDRAW_PAGE;
1422 else if (edit->start_display < ac - KEY_PRESS)
1424 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1425 edit->force |= REDRAW_PAGE;
1427 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1428 edit_update_curs_row (edit);
1430 done_undo:;
1431 edit->undo_stack_disable = 0;
1434 static void
1435 edit_do_redo (WEdit * edit)
1437 long ac;
1438 long count = 0;
1440 if (edit->redo_stack_reset)
1441 return;
1443 edit->over_col = 0;
1444 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1446 switch ((int) ac)
1448 case STACK_BOTTOM:
1449 goto done_redo;
1450 case CURS_RIGHT:
1451 edit_cursor_move (edit, 1);
1452 break;
1453 case CURS_LEFT:
1454 edit_cursor_move (edit, -1);
1455 break;
1456 case BACKSPACE:
1457 edit_backspace (edit, 1);
1458 break;
1459 case DELCHAR:
1460 edit_delete (edit, 1);
1461 break;
1462 case COLUMN_ON:
1463 edit->column_highlight = 1;
1464 break;
1465 case COLUMN_OFF:
1466 edit->column_highlight = 0;
1467 break;
1469 if (ac >= 256 && ac < 512)
1470 edit_insert_ahead (edit, ac - 256);
1471 if (ac >= 0 && ac < 256)
1472 edit_insert (edit, ac);
1474 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1476 edit->mark1 = ac - MARK_1;
1477 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1479 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1481 edit->mark2 = ac - MARK_2;
1482 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1484 /* more than one pop usually means something big */
1485 if (count++)
1486 edit->force |= REDRAW_PAGE;
1489 if (edit->start_display > ac - KEY_PRESS)
1491 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1492 edit->force |= REDRAW_PAGE;
1494 else if (edit->start_display < ac - KEY_PRESS)
1496 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1497 edit->force |= REDRAW_PAGE;
1499 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1500 edit_update_curs_row (edit);
1502 done_redo:;
1505 static void
1506 edit_group_undo (WEdit * edit)
1508 long ac = KEY_PRESS;
1509 long cur_ac = KEY_PRESS;
1510 while (ac != STACK_BOTTOM && ac == cur_ac)
1512 cur_ac = get_prev_undo_action (edit);
1513 edit_do_undo (edit);
1514 ac = get_prev_undo_action (edit);
1515 /* exit from cycle if option_group_undo is not set,
1516 * and make single UNDO operation
1518 if (!option_group_undo)
1519 ac = STACK_BOTTOM;
1523 /* --------------------------------------------------------------------------------------------- */
1525 static void
1526 edit_delete_to_line_end (WEdit * edit)
1528 while (edit_get_byte (edit, edit->curs1) != '\n')
1530 if (!edit->curs2)
1531 break;
1532 edit_delete (edit, 1);
1536 /* --------------------------------------------------------------------------------------------- */
1538 static void
1539 edit_delete_to_line_begin (WEdit * edit)
1541 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1543 if (!edit->curs1)
1544 break;
1545 edit_backspace (edit, 1);
1549 /* --------------------------------------------------------------------------------------------- */
1551 static int
1552 is_aligned_on_a_tab (WEdit * edit)
1554 edit_update_curs_col (edit);
1555 return !((edit->curs_col % (TAB_SIZE * space_width))
1556 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1559 /* --------------------------------------------------------------------------------------------- */
1561 static int
1562 right_of_four_spaces (WEdit * edit)
1564 int i, ch = 0;
1565 for (i = 1; i <= HALF_TAB_SIZE; i++)
1566 ch |= edit_get_byte (edit, edit->curs1 - i);
1567 if (ch == ' ')
1568 return is_aligned_on_a_tab (edit);
1569 return 0;
1572 /* --------------------------------------------------------------------------------------------- */
1574 static int
1575 left_of_four_spaces (WEdit * edit)
1577 int i, ch = 0;
1578 for (i = 0; i < HALF_TAB_SIZE; i++)
1579 ch |= edit_get_byte (edit, edit->curs1 + i);
1580 if (ch == ' ')
1581 return is_aligned_on_a_tab (edit);
1582 return 0;
1585 /* --------------------------------------------------------------------------------------------- */
1587 static void
1588 edit_auto_indent (WEdit * edit)
1590 long p;
1591 char c;
1592 p = edit->curs1;
1593 /* use the previous line as a template */
1594 p = edit_move_backward (edit, p, 1);
1595 /* copy the leading whitespace of the line */
1596 for (;;)
1597 { /* no range check - the line _is_ \n-terminated */
1598 c = edit_get_byte (edit, p++);
1599 if (c != ' ' && c != '\t')
1600 break;
1601 edit_insert (edit, c);
1605 /* --------------------------------------------------------------------------------------------- */
1607 static inline void
1608 edit_double_newline (WEdit * edit)
1610 edit_insert (edit, '\n');
1611 if (edit_get_byte (edit, edit->curs1) == '\n')
1612 return;
1613 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1614 return;
1615 edit->force |= REDRAW_PAGE;
1616 edit_insert (edit, '\n');
1620 /* --------------------------------------------------------------------------------------------- */
1622 static void
1623 insert_spaces_tab (WEdit * edit, gboolean half)
1625 int i;
1627 edit_update_curs_col (edit);
1628 i = option_tab_spacing * space_width;
1629 if (half)
1630 i /= 2;
1631 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1632 while (i > 0)
1634 edit_insert (edit, ' ');
1635 i -= space_width;
1639 /* --------------------------------------------------------------------------------------------- */
1641 static inline void
1642 edit_tab_cmd (WEdit * edit)
1644 int i;
1646 if (option_fake_half_tabs)
1648 if (is_in_indent (edit))
1650 /*insert a half tab (usually four spaces) unless there is a
1651 half tab already behind, then delete it and insert a
1652 full tab. */
1653 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1654 insert_spaces_tab (edit, TRUE);
1655 else
1657 for (i = 1; i <= HALF_TAB_SIZE; i++)
1658 edit_backspace (edit, 1);
1659 edit_insert (edit, '\t');
1661 return;
1664 if (option_fill_tabs_with_spaces)
1665 insert_spaces_tab (edit, FALSE);
1666 else
1667 edit_insert (edit, '\t');
1670 /* --------------------------------------------------------------------------------------------- */
1672 static void
1673 check_and_wrap_line (WEdit * edit)
1675 int curs, c;
1676 if (!option_typewriter_wrap)
1677 return;
1678 edit_update_curs_col (edit);
1679 if (edit->curs_col < option_word_wrap_line_length)
1680 return;
1681 curs = edit->curs1;
1682 for (;;)
1684 curs--;
1685 c = edit_get_byte (edit, curs);
1686 if (c == '\n' || curs <= 0)
1688 edit_insert (edit, '\n');
1689 return;
1691 if (c == ' ' || c == '\t')
1693 int current = edit->curs1;
1694 edit_cursor_move (edit, curs - edit->curs1 + 1);
1695 edit_insert (edit, '\n');
1696 edit_cursor_move (edit, current - edit->curs1 + 1);
1697 return;
1702 /* --------------------------------------------------------------------------------------------- */
1703 /** this find the matching bracket in either direction, and sets edit->bracket */
1705 static long
1706 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1708 const char *const b = "{}{[][()(", *p;
1709 int i = 1, a, inc = -1, c, d, n = 0;
1710 unsigned long j = 0;
1711 long q;
1712 edit_update_curs_row (edit);
1713 c = edit_get_byte (edit, edit->curs1);
1714 p = strchr (b, c);
1715 /* no limit */
1716 if (!furthest_bracket_search)
1717 furthest_bracket_search--;
1718 /* not on a bracket at all */
1719 if (!p)
1720 return -1;
1721 /* the matching bracket */
1722 d = p[1];
1723 /* going left or right? */
1724 if (strchr ("{[(", c))
1725 inc = 1;
1726 for (q = edit->curs1 + inc;; q += inc)
1728 /* out of buffer? */
1729 if (q >= edit->last_byte || q < 0)
1730 break;
1731 a = edit_get_byte (edit, q);
1732 /* don't want to eat CPU */
1733 if (j++ > furthest_bracket_search)
1734 break;
1735 /* out of screen? */
1736 if (in_screen)
1738 if (q < edit->start_display)
1739 break;
1740 /* count lines if searching downward */
1741 if (inc > 0 && a == '\n')
1742 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1743 break;
1745 /* count bracket depth */
1746 i += (a == c) - (a == d);
1747 /* return if bracket depth is zero */
1748 if (!i)
1749 return q;
1751 /* no match */
1752 return -1;
1755 /* --------------------------------------------------------------------------------------------- */
1757 static inline void
1758 edit_goto_matching_bracket (WEdit * edit)
1760 long q;
1762 q = edit_get_bracket (edit, 0, 0);
1763 if (q >= 0)
1765 edit->bracket = edit->curs1;
1766 edit->force |= REDRAW_PAGE;
1767 edit_cursor_move (edit, q - edit->curs1);
1771 /* --------------------------------------------------------------------------------------------- */
1773 static void
1774 edit_move_block_to_right (WEdit * edit)
1776 long start_mark, end_mark;
1777 long cur_bol, start_bol;
1779 if (eval_marks (edit, &start_mark, &end_mark))
1780 return;
1782 start_bol = edit_bol (edit, start_mark);
1783 cur_bol = edit_bol (edit, end_mark - 1);
1787 edit_cursor_move (edit, cur_bol - edit->curs1);
1788 if (option_fill_tabs_with_spaces)
1789 insert_spaces_tab (edit, option_fake_half_tabs);
1790 else
1791 edit_insert (edit, '\t');
1792 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1794 if (cur_bol == 0)
1795 break;
1797 cur_bol = edit_bol (edit, cur_bol - 1);
1799 while (cur_bol >= start_bol);
1801 edit->force |= REDRAW_PAGE;
1804 /* --------------------------------------------------------------------------------------------- */
1806 static void
1807 edit_move_block_to_left (WEdit * edit)
1809 long start_mark, end_mark;
1810 long cur_bol, start_bol;
1811 int i;
1813 if (eval_marks (edit, &start_mark, &end_mark))
1814 return;
1816 start_bol = edit_bol (edit, start_mark);
1817 cur_bol = edit_bol (edit, end_mark - 1);
1821 int del_tab_width;
1822 int next_char;
1824 edit_cursor_move (edit, cur_bol - edit->curs1);
1826 if (option_fake_half_tabs)
1827 del_tab_width = HALF_TAB_SIZE;
1828 else
1829 del_tab_width = option_tab_spacing;
1831 next_char = edit_get_byte (edit, edit->curs1);
1832 if (next_char == '\t')
1833 edit_delete (edit, 1);
1834 else if (next_char == ' ')
1835 for (i = 1; i <= del_tab_width; i++)
1837 if (next_char == ' ')
1838 edit_delete (edit, 1);
1839 next_char = edit_get_byte (edit, edit->curs1);
1842 if (cur_bol == 0)
1843 break;
1845 cur_bol = edit_bol (edit, cur_bol - 1);
1847 while (cur_bol >= start_bol);
1849 edit->force |= REDRAW_PAGE;
1852 /* --------------------------------------------------------------------------------------------- */
1854 * prints at the cursor
1855 * @returns the number of chars printed
1858 static size_t
1859 edit_print_string (WEdit * e, const char *s)
1861 size_t i = 0;
1863 while (s[i] != '\0')
1864 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1865 e->force |= REDRAW_COMPLETELY;
1866 edit_update_screen (e);
1867 return i;
1870 /* --------------------------------------------------------------------------------------------- */
1871 /*** public functions ****************************************************************************/
1872 /* --------------------------------------------------------------------------------------------- */
1874 /** User edit menu, like user menu (F2) but only in editor. */
1876 void
1877 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1879 char *block_file;
1880 int nomark;
1881 long curs;
1882 long start_mark, end_mark;
1883 struct stat status;
1885 block_file = concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE);
1886 curs = edit->curs1;
1887 nomark = eval_marks (edit, &start_mark, &end_mark);
1888 if (nomark == 0)
1889 edit_save_block (edit, block_file, start_mark, end_mark);
1891 /* run shell scripts from menu */
1892 if (user_menu_cmd (edit, menu_file, selected_entry)
1893 && (mc_stat (block_file, &status) == 0) && (status.st_size != 0))
1895 int rc = 0;
1896 FILE *fd;
1898 /* i.e. we have marked block */
1899 if (nomark == 0)
1900 rc = edit_block_delete_cmd (edit);
1902 if (rc == 0)
1904 long ins_len;
1906 ins_len = edit_insert_file (edit, block_file);
1907 if (nomark == 0 && ins_len > 0)
1908 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1910 /* truncate block file */
1911 fd = fopen (block_file, "w");
1912 if (fd != NULL)
1913 fclose (fd);
1915 edit_cursor_move (edit, curs - edit->curs1);
1916 edit_refresh_cmd (edit);
1917 edit->force |= REDRAW_COMPLETELY;
1919 g_free (block_file);
1922 /* --------------------------------------------------------------------------------------------- */
1925 edit_get_byte (WEdit * edit, long byte_index)
1927 unsigned long p;
1928 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1929 return '\n';
1931 if (byte_index >= edit->curs1)
1933 p = edit->curs1 + edit->curs2 - byte_index - 1;
1934 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1936 else
1938 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1942 /* --------------------------------------------------------------------------------------------- */
1945 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1947 gchar *str = NULL;
1948 int res = -1;
1949 gunichar ch;
1950 gchar *next_ch = NULL;
1951 int width = 0;
1952 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1954 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1956 *char_width = 0;
1957 return '\n';
1960 str = edit_get_byte_ptr (edit, byte_index);
1962 if (str == NULL)
1964 *char_width = 0;
1965 return 0;
1968 res = g_utf8_get_char_validated (str, -1);
1970 if (res < 0)
1972 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1973 int i;
1974 for (i = 0; i < UTF8_CHAR_LEN; i++)
1975 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1976 utf8_buf[UTF8_CHAR_LEN] = '\0';
1977 str = utf8_buf;
1978 res = g_utf8_get_char_validated (str, -1);
1981 if (res < 0)
1983 ch = *str;
1984 width = 0;
1986 else
1988 ch = res;
1989 /* Calculate UTF-8 char width */
1990 next_ch = g_utf8_next_char (str);
1991 if (next_ch)
1993 width = next_ch - str;
1995 else
1997 ch = 0;
1998 width = 0;
2001 *char_width = width;
2002 return ch;
2005 /* --------------------------------------------------------------------------------------------- */
2007 char *
2008 edit_get_write_filter (const char *write_name, const char *filename)
2010 int i;
2011 char *p, *writename;
2013 i = edit_find_filter (filename);
2014 if (i < 0)
2015 return NULL;
2017 writename = name_quote (write_name, 0);
2018 p = g_strdup_printf (all_filters[i].write, writename);
2019 g_free (writename);
2020 return p;
2023 /* --------------------------------------------------------------------------------------------- */
2025 long
2026 edit_write_stream (WEdit * edit, FILE * f)
2028 long i;
2030 if (edit->lb == LB_ASIS)
2032 for (i = 0; i < edit->last_byte; i++)
2033 if (fputc (edit_get_byte (edit, i), f) < 0)
2034 break;
2035 return i;
2038 /* change line breaks */
2039 for (i = 0; i < edit->last_byte; i++)
2041 unsigned char c = edit_get_byte (edit, i);
2043 if (!(c == '\n' || c == '\r'))
2045 /* not line break */
2046 if (fputc (c, f) < 0)
2047 return i;
2049 else
2050 { /* (c == '\n' || c == '\r') */
2051 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2053 switch (edit->lb)
2055 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2056 /* put one line break unconditionally */
2057 if (fputc ('\n', f) < 0)
2058 return i;
2060 i++; /* 2 chars are processed */
2062 if (c == '\r' && c1 == '\n')
2063 /* Windows line break; go to the next char */
2064 break;
2066 if (c == '\r' && c1 == '\r')
2068 /* two Macintosh line breaks; put second line break */
2069 if (fputc ('\n', f) < 0)
2070 return i;
2071 break;
2074 if (fputc (c1, f) < 0)
2075 return i;
2076 break;
2078 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2079 /* put one line break unconditionally */
2080 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2081 return i;
2083 if (c == '\r' && c1 == '\n')
2084 /* Windows line break; go to the next char */
2085 i++;
2086 break;
2088 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2089 /* put one line break unconditionally */
2090 if (fputc ('\r', f) < 0)
2091 return i;
2093 i++; /* 2 chars are processed */
2095 if (c == '\r' && c1 == '\n')
2096 /* Windows line break; go to the next char */
2097 break;
2099 if (c == '\n' && c1 == '\n')
2101 /* two Windows line breaks; put second line break */
2102 if (fputc ('\r', f) < 0)
2103 return i;
2104 break;
2107 if (fputc (c1, f) < 0)
2108 return i;
2109 break;
2110 case LB_ASIS: /* default without changes */
2111 break;
2116 return edit->last_byte;
2119 /* --------------------------------------------------------------------------------------------- */
2120 /** inserts a file at the cursor, returns count of inserted bytes on success */
2121 long
2122 edit_insert_file (WEdit * edit, const char *filename)
2124 char *p;
2125 long ins_len = 0;
2127 p = edit_get_filter (filename);
2128 if (p != NULL)
2130 FILE *f;
2131 long current = edit->curs1;
2132 f = (FILE *) popen (p, "r");
2133 if (f != NULL)
2135 edit_insert_stream (edit, f);
2136 ins_len = edit->curs1 - current;
2137 edit_cursor_move (edit, current - edit->curs1);
2138 if (pclose (f) > 0)
2140 char *errmsg;
2141 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2142 edit_error_dialog (_("Error"), errmsg);
2143 g_free (errmsg);
2144 g_free (p);
2145 return 0;
2148 else
2150 char *errmsg;
2151 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2152 edit_error_dialog (_("Error"), errmsg);
2153 g_free (errmsg);
2154 g_free (p);
2155 return 0;
2157 g_free (p);
2159 else
2161 int i, file, blocklen;
2162 long current = edit->curs1;
2163 int vertical_insertion = 0;
2164 char *buf;
2165 file = mc_open (filename, O_RDONLY | O_BINARY);
2166 if (file == -1)
2167 return 0;
2168 buf = g_malloc0 (TEMP_BUF_LEN);
2169 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2170 if (blocklen > 0)
2172 /* if contain signature VERTICAL_MAGIC then it vertical block */
2173 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2174 vertical_insertion = 1;
2175 else
2176 mc_lseek (file, 0, SEEK_SET);
2178 if (vertical_insertion)
2180 long mark1, mark2;
2181 int c1, c2;
2182 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2183 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2184 /* highlight inserted text then not persistent blocks */
2185 if (!option_persistent_selections)
2187 if (!edit->column_highlight)
2188 edit_push_undo_action (edit, COLUMN_OFF);
2189 edit->column_highlight = 1;
2192 else
2194 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2196 for (i = 0; i < blocklen; i++)
2197 edit_insert (edit, buf[i]);
2199 /* highlight inserted text then not persistent blocks */
2200 if (!option_persistent_selections && edit->modified)
2202 edit_set_markers (edit, edit->curs1, current, 0, 0);
2203 if (edit->column_highlight)
2204 edit_push_undo_action (edit, COLUMN_ON);
2205 edit->column_highlight = 0;
2208 edit->force |= REDRAW_PAGE;
2209 ins_len = edit->curs1 - current;
2210 edit_cursor_move (edit, current - edit->curs1);
2211 g_free (buf);
2212 mc_close (file);
2213 if (blocklen != 0)
2214 return 0;
2216 return ins_len;
2219 /* --------------------------------------------------------------------------------------------- */
2221 * Fill in the edit structure. Return NULL on failure. Pass edit as
2222 * NULL to allocate a new structure.
2224 * If line is 0, try to restore saved position. Otherwise put the
2225 * cursor on that line and show it in the middle of the screen.
2228 WEdit *
2229 edit_init (WEdit * edit, int y, int x, int lines, int cols, const char *filename, long line)
2231 gboolean to_free = FALSE;
2233 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2234 if (option_line_state)
2235 option_line_state_width = LINE_STATE_WIDTH;
2236 else
2237 option_line_state_width = 0;
2239 if (edit == NULL)
2241 #ifdef ENABLE_NLS
2243 * Expand option_whole_chars_search by national letters using
2244 * current locale
2247 static char option_whole_chars_search_buf[256];
2249 if (option_whole_chars_search_buf != option_whole_chars_search)
2251 size_t i;
2252 size_t len = str_term_width1 (option_whole_chars_search);
2254 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2256 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2258 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2260 option_whole_chars_search_buf[len++] = i;
2264 option_whole_chars_search_buf[len] = 0;
2265 option_whole_chars_search = option_whole_chars_search_buf;
2267 #endif /* ENABLE_NLS */
2268 edit = g_malloc0 (sizeof (WEdit));
2269 edit->search = NULL;
2270 to_free = TRUE;
2273 edit_purge_widget (edit);
2274 edit->widget.y = y;
2275 edit->widget.x = x;
2276 edit->widget.lines = lines;
2277 edit->widget.cols = cols;
2279 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2280 edit->stat1.st_uid = getuid ();
2281 edit->stat1.st_gid = getgid ();
2282 edit->stat1.st_mtime = 0;
2284 edit->over_col = 0;
2285 edit->bracket = -1;
2286 edit->force |= REDRAW_PAGE;
2287 edit_set_filename (edit, filename);
2289 edit->undo_stack_size = START_STACK_SIZE;
2290 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2291 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2293 edit->redo_stack_size = START_STACK_SIZE;
2294 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2295 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2297 edit->utf8 = 0;
2298 edit->converter = str_cnv_from_term;
2299 edit_set_codeset (edit);
2301 if (edit_load_file (edit))
2303 /* edit_load_file already gives an error message */
2304 if (to_free)
2305 g_free (edit);
2306 return NULL;
2309 edit->loading_done = 1;
2310 edit->modified = 0;
2311 edit->locked = 0;
2312 edit_load_syntax (edit, NULL, NULL);
2314 int color;
2315 edit_get_syntax_color (edit, -1, &color);
2318 /* load saved cursor position */
2319 if ((line == 0) && option_save_position)
2320 edit_load_position (edit);
2321 else
2323 if (line <= 0)
2324 line = 1;
2325 edit_move_display (edit, line - 1);
2326 edit_move_to_line (edit, line - 1);
2329 edit_load_macro_cmd (edit);
2330 return edit;
2333 /* --------------------------------------------------------------------------------------------- */
2334 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2337 edit_clean (WEdit * edit)
2339 int j = 0;
2341 if (!edit)
2342 return 0;
2344 /* a stale lock, remove it */
2345 if (edit->locked)
2346 edit->locked = edit_unlock_file (edit);
2348 /* save cursor position */
2349 if (option_save_position)
2350 edit_save_position (edit);
2351 else if (edit->serialized_bookmarks != NULL)
2352 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2354 /* File specified on the mcedit command line and never saved */
2355 if (edit->delete_file)
2356 unlink (edit->filename);
2358 edit_free_syntax_rules (edit);
2359 book_mark_flush (edit, -1);
2360 for (; j <= MAXBUFF; j++)
2362 g_free (edit->buffers1[j]);
2363 g_free (edit->buffers2[j]);
2366 g_free (edit->undo_stack);
2367 g_free (edit->redo_stack);
2368 g_free (edit->filename);
2369 g_free (edit->dir);
2370 mc_search_free (edit->search);
2371 edit->search = NULL;
2373 if (edit->converter != str_cnv_from_term)
2374 str_close_conv (edit->converter);
2376 edit_purge_widget (edit);
2378 return 1;
2381 /* --------------------------------------------------------------------------------------------- */
2382 /** returns 1 on success */
2385 edit_renew (WEdit * edit)
2387 int y = edit->widget.y;
2388 int x = edit->widget.x;
2389 int lines = edit->widget.lines;
2390 int columns = edit->widget.cols;
2392 edit_clean (edit);
2393 return (edit_init (edit, y, x, lines, columns, "", 0) != NULL);
2396 /* --------------------------------------------------------------------------------------------- */
2398 * Load a new file into the editor. If it fails, preserve the old file.
2399 * To do it, allocate a new widget, initialize it and, if the new file
2400 * was loaded, copy the data to the old widget.
2401 * Return 1 on success, 0 on failure.
2405 edit_reload (WEdit * edit, const char *filename)
2407 WEdit *e;
2408 int y = edit->widget.y;
2409 int x = edit->widget.x;
2410 int lines = edit->widget.lines;
2411 int columns = edit->widget.cols;
2413 e = g_malloc0 (sizeof (WEdit));
2414 e->widget = edit->widget;
2415 if (edit_init (e, y, x, lines, columns, filename, 0) == NULL)
2417 g_free (e);
2418 return 0;
2420 edit_clean (edit);
2421 memcpy (edit, e, sizeof (WEdit));
2422 g_free (e);
2423 return 1;
2426 /* --------------------------------------------------------------------------------------------- */
2428 * Load a new file into the editor and set line. If it fails, preserve the old file.
2429 * To do it, allocate a new widget, initialize it and, if the new file
2430 * was loaded, copy the data to the old widget.
2431 * Return 1 on success, 0 on failure.
2435 edit_reload_line (WEdit * edit, const char *filename, long line)
2437 WEdit *e;
2438 int y = edit->widget.y;
2439 int x = edit->widget.x;
2440 int lines = edit->widget.lines;
2441 int columns = edit->widget.cols;
2443 e = g_malloc0 (sizeof (WEdit));
2444 e->widget = edit->widget;
2445 if (edit_init (e, y, x, lines, columns, filename, line) == NULL)
2447 g_free (e);
2448 return 0;
2450 edit_clean (edit);
2451 memcpy (edit, e, sizeof (WEdit));
2452 g_free (e);
2453 return 1;
2456 /* --------------------------------------------------------------------------------------------- */
2458 void
2459 edit_set_codeset (WEdit * edit)
2461 #ifdef HAVE_CHARSET
2462 const char *cp_id;
2464 cp_id =
2465 get_codepage_id (mc_global.source_codepage >=
2466 0 ? mc_global.source_codepage : mc_global.display_codepage);
2468 if (cp_id != NULL)
2470 GIConv conv;
2471 conv = str_crt_conv_from (cp_id);
2472 if (conv != INVALID_CONV)
2474 if (edit->converter != str_cnv_from_term)
2475 str_close_conv (edit->converter);
2476 edit->converter = conv;
2480 if (cp_id != NULL)
2481 edit->utf8 = str_isutf8 (cp_id);
2482 #else
2483 (void) edit;
2484 #endif
2488 /* --------------------------------------------------------------------------------------------- */
2490 Recording stack for undo:
2491 The following is an implementation of a compressed stack. Identical
2492 pushes are recorded by a negative prefix indicating the number of times the
2493 same char was pushed. This saves space for repeated curs-left or curs-right
2494 delete etc.
2498 pushed: stored:
2502 b -3
2504 c --> -4
2510 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2511 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2512 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2513 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2514 position.
2516 The only way the cursor moves or the buffer is changed is through the routines:
2517 insert, backspace, insert_ahead, delete, and cursor_move.
2518 These record the reverse undo movements onto the stack each time they are
2519 called.
2521 Each key press results in a set of actions (insert; delete ...). So each time
2522 a key is pressed the current position of start_display is pushed as
2523 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2524 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2525 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2529 void
2530 edit_push_undo_action (WEdit * edit, long c, ...)
2532 unsigned long sp = edit->undo_stack_pointer;
2533 unsigned long spm1;
2534 long *t;
2536 /* first enlarge the stack if necessary */
2537 if (sp > edit->undo_stack_size - 10)
2538 { /* say */
2539 if (option_max_undo < 256)
2540 option_max_undo = 256;
2541 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2543 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2544 if (t)
2546 edit->undo_stack = t;
2547 edit->undo_stack_size <<= 1;
2548 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2552 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2553 if (edit->undo_stack_disable)
2555 edit_push_redo_action (edit, KEY_PRESS);
2556 edit_push_redo_action (edit, c);
2557 return;
2559 else if (edit->redo_stack_reset)
2561 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2564 if (edit->undo_stack_bottom != sp
2565 && spm1 != edit->undo_stack_bottom
2566 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2568 int d;
2569 if (edit->undo_stack[spm1] < 0)
2571 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2572 if (d == c)
2574 if (edit->undo_stack[spm1] > -1000000000)
2576 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2578 edit->undo_stack[spm1]--;
2580 return;
2584 else
2586 d = edit->undo_stack[spm1];
2587 if (d == c)
2589 if (c >= KEY_PRESS)
2590 return; /* --> no need to push multiple do-nothings */
2591 edit->undo_stack[sp] = -2;
2592 goto check_bottom;
2596 edit->undo_stack[sp] = c;
2598 check_bottom:
2599 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2601 /* if the sp wraps round and catches the undo_stack_bottom then erase
2602 * the first set of actions on the stack to make space - by moving
2603 * undo_stack_bottom forward one "key press" */
2604 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2605 if ((unsigned long) c == edit->undo_stack_bottom ||
2606 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2609 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2611 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2612 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2614 /*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: */
2615 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2616 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2618 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2622 void
2623 edit_push_redo_action (WEdit * edit, long c, ...)
2625 unsigned long sp = edit->redo_stack_pointer;
2626 unsigned long spm1;
2627 long *t;
2628 /* first enlarge the stack if necessary */
2629 if (sp > edit->redo_stack_size - 10)
2630 { /* say */
2631 if (option_max_undo < 256)
2632 option_max_undo = 256;
2633 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2635 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2636 if (t)
2638 edit->redo_stack = t;
2639 edit->redo_stack_size <<= 1;
2640 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2644 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2646 if (edit->redo_stack_bottom != sp
2647 && spm1 != edit->redo_stack_bottom
2648 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2650 int d;
2651 if (edit->redo_stack[spm1] < 0)
2653 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2654 if (d == c)
2656 if (edit->redo_stack[spm1] > -1000000000)
2658 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2659 edit->redo_stack[spm1]--;
2660 return;
2664 else
2666 d = edit->redo_stack[spm1];
2667 if (d == c)
2669 if (c >= KEY_PRESS)
2670 return; /* --> no need to push multiple do-nothings */
2671 edit->redo_stack[sp] = -2;
2672 goto redo_check_bottom;
2676 edit->redo_stack[sp] = c;
2678 redo_check_bottom:
2679 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2681 /* if the sp wraps round and catches the redo_stack_bottom then erase
2682 * the first set of actions on the stack to make space - by moving
2683 * redo_stack_bottom forward one "key press" */
2684 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2685 if ((unsigned long) c == edit->redo_stack_bottom ||
2686 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2689 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2691 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2692 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2695 * If a single key produced enough pushes to wrap all the way round then
2696 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2697 * The stack is then initialised:
2700 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2701 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2702 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2706 /* --------------------------------------------------------------------------------------------- */
2708 Basic low level single character buffer alterations and movements at the cursor.
2709 Returns char passed over, inserted or removed.
2712 void
2713 edit_insert (WEdit * edit, int c)
2715 /* check if file has grown to large */
2716 if (edit->last_byte >= SIZE_LIMIT)
2717 return;
2719 /* first we must update the position of the display window */
2720 if (edit->curs1 < edit->start_display)
2722 edit->start_display++;
2723 if (c == '\n')
2724 edit->start_line++;
2727 /* Mark file as modified, unless the file hasn't been fully loaded */
2728 if (edit->loading_done)
2730 edit_modification (edit);
2733 /* now we must update some info on the file and check if a redraw is required */
2734 if (c == '\n')
2736 if (edit->book_mark)
2737 book_mark_inc (edit, edit->curs_line);
2738 edit->curs_line++;
2739 edit->total_lines++;
2740 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2743 /* save the reverse command onto the undo stack */
2744 /* ordinary char and not space */
2745 if (c > 32)
2746 edit_push_undo_action (edit, BACKSPACE);
2747 else
2748 edit_push_undo_action (edit, BACKSPACE_BR);
2749 /* update markers */
2750 edit->mark1 += (edit->mark1 > edit->curs1);
2751 edit->mark2 += (edit->mark2 > edit->curs1);
2752 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2754 /* add a new buffer if we've reached the end of the last one */
2755 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2756 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2758 /* perform the insertion */
2759 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2760 = (unsigned char) c;
2762 /* update file length */
2763 edit->last_byte++;
2765 /* update cursor position */
2766 edit->curs1++;
2769 /* --------------------------------------------------------------------------------------------- */
2770 /** same as edit_insert and move left */
2772 void
2773 edit_insert_ahead (WEdit * edit, int c)
2775 if (edit->last_byte >= SIZE_LIMIT)
2776 return;
2778 if (edit->curs1 < edit->start_display)
2780 edit->start_display++;
2781 if (c == '\n')
2782 edit->start_line++;
2784 edit_modification (edit);
2785 if (c == '\n')
2787 if (edit->book_mark)
2788 book_mark_inc (edit, edit->curs_line);
2789 edit->total_lines++;
2790 edit->force |= REDRAW_AFTER_CURSOR;
2792 /* ordinary char and not space */
2793 if (c > 32)
2794 edit_push_undo_action (edit, DELCHAR);
2795 else
2796 edit_push_undo_action (edit, DELCHAR_BR);
2798 edit->mark1 += (edit->mark1 >= edit->curs1);
2799 edit->mark2 += (edit->mark2 >= edit->curs1);
2800 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2802 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2803 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2804 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2805 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2807 edit->last_byte++;
2808 edit->curs2++;
2812 /* --------------------------------------------------------------------------------------------- */
2815 edit_delete (WEdit * edit, const int byte_delete)
2817 int p = 0;
2818 int cw = 1;
2819 int i;
2821 if (!edit->curs2)
2822 return 0;
2824 cw = 1;
2825 /* if byte_delete = 1 then delete only one byte not multibyte char */
2826 if (edit->utf8 && byte_delete == 0)
2828 edit_get_utf (edit, edit->curs1, &cw);
2829 if (cw < 1)
2830 cw = 1;
2833 if (edit->mark2 != edit->mark1)
2834 edit_push_markers (edit);
2836 for (i = 1; i <= cw; i++)
2838 if (edit->mark1 > edit->curs1)
2840 edit->mark1--;
2841 edit->end_mark_curs--;
2843 if (edit->mark2 > edit->curs1)
2844 edit->mark2--;
2845 if (edit->last_get_rule > edit->curs1)
2846 edit->last_get_rule--;
2848 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2849 ((edit->curs2 -
2850 1) & M_EDIT_BUF_SIZE) - 1];
2852 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2854 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2855 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2857 edit->last_byte--;
2858 edit->curs2--;
2859 edit_push_undo_action (edit, p + 256);
2862 edit_modification (edit);
2863 if (p == '\n')
2865 if (edit->book_mark)
2866 book_mark_dec (edit, edit->curs_line);
2867 edit->total_lines--;
2868 edit->force |= REDRAW_AFTER_CURSOR;
2870 if (edit->curs1 < edit->start_display)
2872 edit->start_display--;
2873 if (p == '\n')
2874 edit->start_line--;
2877 return p;
2880 /* --------------------------------------------------------------------------------------------- */
2881 /** moves the cursor right or left: increment positive or negative respectively */
2883 void
2884 edit_cursor_move (WEdit * edit, long increment)
2886 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2887 int c;
2889 if (increment < 0)
2891 for (; increment < 0; increment++)
2893 if (!edit->curs1)
2894 return;
2896 edit_push_undo_action (edit, CURS_RIGHT);
2898 c = edit_get_byte (edit, edit->curs1 - 1);
2899 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2900 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2901 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2902 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2903 edit->curs2++;
2904 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2905 1) & M_EDIT_BUF_SIZE];
2906 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2908 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2909 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2911 edit->curs1--;
2912 if (c == '\n')
2914 edit->curs_line--;
2915 edit->force |= REDRAW_LINE_BELOW;
2920 else if (increment > 0)
2922 for (; increment > 0; increment--)
2924 if (!edit->curs2)
2925 return;
2927 edit_push_undo_action (edit, CURS_LEFT);
2929 c = edit_get_byte (edit, edit->curs1);
2930 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2931 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2932 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2933 edit->curs1++;
2934 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2935 ((edit->curs2 -
2936 1) & M_EDIT_BUF_SIZE) - 1];
2937 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2939 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2940 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2942 edit->curs2--;
2943 if (c == '\n')
2945 edit->curs_line++;
2946 edit->force |= REDRAW_LINE_ABOVE;
2952 /* These functions return positions relative to lines */
2954 /* --------------------------------------------------------------------------------------------- */
2955 /** returns index of last char on line + 1 */
2957 long
2958 edit_eol (WEdit * edit, long current)
2960 if (current >= edit->last_byte)
2961 return edit->last_byte;
2963 for (;; current++)
2964 if (edit_get_byte (edit, current) == '\n')
2965 break;
2966 return current;
2969 /* --------------------------------------------------------------------------------------------- */
2970 /** returns index of first char on line */
2972 long
2973 edit_bol (WEdit * edit, long current)
2975 if (current <= 0)
2976 return 0;
2978 for (;; current--)
2979 if (edit_get_byte (edit, current - 1) == '\n')
2980 break;
2981 return current;
2984 /* --------------------------------------------------------------------------------------------- */
2986 long
2987 edit_count_lines (WEdit * edit, long current, long upto)
2989 long lines = 0;
2990 if (upto > edit->last_byte)
2991 upto = edit->last_byte;
2992 if (current < 0)
2993 current = 0;
2994 while (current < upto)
2995 if (edit_get_byte (edit, current++) == '\n')
2996 lines++;
2997 return lines;
3000 /* --------------------------------------------------------------------------------------------- */
3001 /* If lines is zero this returns the count of lines from current to upto. */
3002 /* If upto is zero returns index of lines forward current. */
3004 long
3005 edit_move_forward (WEdit * edit, long current, long lines, long upto)
3007 if (upto)
3009 return edit_count_lines (edit, current, upto);
3011 else
3013 long next;
3014 if (lines < 0)
3015 lines = 0;
3016 while (lines--)
3018 next = edit_eol (edit, current) + 1;
3019 if (next > edit->last_byte)
3020 break;
3021 else
3022 current = next;
3024 return current;
3028 /* --------------------------------------------------------------------------------------------- */
3029 /** Returns offset of 'lines' lines up from current */
3031 long
3032 edit_move_backward (WEdit * edit, long current, long lines)
3034 if (lines < 0)
3035 lines = 0;
3036 current = edit_bol (edit, current);
3037 while ((lines--) && current != 0)
3038 current = edit_bol (edit, current - 1);
3039 return current;
3042 /* --------------------------------------------------------------------------------------------- */
3043 /* If cols is zero this returns the count of columns from current to upto. */
3044 /* If upto is zero returns index of cols across from current. */
3046 long
3047 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3049 long p, q;
3050 int col;
3052 if (upto)
3054 q = upto;
3055 cols = -10;
3057 else
3058 q = edit->last_byte + 2;
3060 for (col = 0, p = current; p < q; p++)
3062 int c, orig_c;
3064 if (cols != -10)
3066 if (col == cols)
3067 return p;
3068 if (col > cols)
3069 return p - 1;
3072 orig_c = c = edit_get_byte (edit, p);
3074 #ifdef HAVE_CHARSET
3075 if (edit->utf8)
3077 int utf_ch;
3078 int cw = 1;
3080 utf_ch = edit_get_utf (edit, p, &cw);
3081 if (mc_global.utf8_display)
3083 if (cw > 1)
3084 col -= cw - 1;
3085 if (g_unichar_iswide (utf_ch))
3086 col++;
3088 else if (cw > 1 && g_unichar_isprint (utf_ch))
3089 col -= cw - 1;
3092 c = convert_to_display_c (c);
3093 #endif
3095 if (c == '\t')
3096 col += TAB_SIZE - col % TAB_SIZE;
3097 else if (c == '\n')
3099 if (upto)
3100 return col;
3101 else
3102 return p;
3104 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3105 /* '\r' is shown as ^M, so we must advance 2 characters */
3106 /* Caret notation for control characters */
3107 col += 2;
3108 else
3109 col++;
3111 return col;
3114 /* --------------------------------------------------------------------------------------------- */
3115 /** returns the current column position of the cursor */
3118 edit_get_col (WEdit * edit)
3120 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3123 /* --------------------------------------------------------------------------------------------- */
3124 /* Scrolling functions */
3125 /* --------------------------------------------------------------------------------------------- */
3127 void
3128 edit_update_curs_row (WEdit * edit)
3130 edit->curs_row = edit->curs_line - edit->start_line;
3133 /* --------------------------------------------------------------------------------------------- */
3135 void
3136 edit_update_curs_col (WEdit * edit)
3138 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3141 /* --------------------------------------------------------------------------------------------- */
3144 edit_get_curs_col (const WEdit * edit)
3146 return edit->curs_col;
3149 /* --------------------------------------------------------------------------------------------- */
3150 /** moves the display start position up by i lines */
3152 void
3153 edit_scroll_upward (WEdit * edit, unsigned long i)
3155 unsigned long lines_above = edit->start_line;
3156 if (i > lines_above)
3157 i = lines_above;
3158 if (i)
3160 edit->start_line -= i;
3161 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3162 edit->force |= REDRAW_PAGE;
3163 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3165 edit_update_curs_row (edit);
3169 /* --------------------------------------------------------------------------------------------- */
3170 /** returns 1 if could scroll, 0 otherwise */
3172 void
3173 edit_scroll_downward (WEdit * edit, int i)
3175 int lines_below;
3176 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3177 if (lines_below > 0)
3179 if (i > lines_below)
3180 i = lines_below;
3181 edit->start_line += i;
3182 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3183 edit->force |= REDRAW_PAGE;
3184 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3186 edit_update_curs_row (edit);
3189 /* --------------------------------------------------------------------------------------------- */
3191 void
3192 edit_scroll_right (WEdit * edit, int i)
3194 edit->force |= REDRAW_PAGE;
3195 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3196 edit->start_col -= i;
3199 /* --------------------------------------------------------------------------------------------- */
3201 void
3202 edit_scroll_left (WEdit * edit, int i)
3204 if (edit->start_col)
3206 edit->start_col += i;
3207 if (edit->start_col > 0)
3208 edit->start_col = 0;
3209 edit->force |= REDRAW_PAGE;
3210 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3214 /* --------------------------------------------------------------------------------------------- */
3215 /* high level cursor movement commands */
3216 /* --------------------------------------------------------------------------------------------- */
3218 void
3219 edit_move_to_prev_col (WEdit * edit, long p)
3221 int prev = edit->prev_col;
3222 int over = edit->over_col;
3223 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3225 if (option_cursor_beyond_eol)
3227 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3228 edit_eol (edit, edit->curs1));
3230 if (line_len < prev + edit->over_col)
3232 edit->over_col = prev + over - line_len;
3233 edit->prev_col = line_len;
3234 edit->curs_col = line_len;
3236 else
3238 edit->curs_col = prev + over;
3239 edit->prev_col = edit->curs_col;
3240 edit->over_col = 0;
3243 else
3245 edit->over_col = 0;
3246 if (is_in_indent (edit) && option_fake_half_tabs)
3248 edit_update_curs_col (edit);
3249 if (space_width)
3250 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3252 int q = edit->curs_col;
3253 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3254 p = edit_bol (edit, edit->curs1);
3255 edit_cursor_move (edit,
3256 edit_move_forward3 (edit, p, edit->curs_col,
3257 0) - edit->curs1);
3258 if (!left_of_four_spaces (edit))
3259 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3265 /* --------------------------------------------------------------------------------------------- */
3268 line_is_blank (WEdit * edit, long line)
3270 return is_blank (edit, edit_find_line (edit, line));
3273 /* --------------------------------------------------------------------------------------------- */
3274 /** move cursor to line 'line' */
3276 void
3277 edit_move_to_line (WEdit * e, long line)
3279 if (line < e->curs_line)
3280 edit_move_up (e, e->curs_line - line, 0);
3281 else
3282 edit_move_down (e, line - e->curs_line, 0);
3283 edit_scroll_screen_over_cursor (e);
3286 /* --------------------------------------------------------------------------------------------- */
3287 /** scroll window so that first visible line is 'line' */
3289 void
3290 edit_move_display (WEdit * e, long line)
3292 if (line < e->start_line)
3293 edit_scroll_upward (e, e->start_line - line);
3294 else
3295 edit_scroll_downward (e, line - e->start_line);
3298 /* --------------------------------------------------------------------------------------------- */
3299 /** save markers onto undo stack */
3301 void
3302 edit_push_markers (WEdit * edit)
3304 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3305 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3306 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3309 /* --------------------------------------------------------------------------------------------- */
3311 void
3312 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3314 edit->mark1 = m1;
3315 edit->mark2 = m2;
3316 edit->column1 = c1;
3317 edit->column2 = c2;
3321 /* --------------------------------------------------------------------------------------------- */
3322 /** highlight marker toggle */
3324 void
3325 edit_mark_cmd (WEdit * edit, int unmark)
3327 edit_push_markers (edit);
3328 if (unmark)
3330 edit_set_markers (edit, 0, 0, 0, 0);
3331 edit->force |= REDRAW_PAGE;
3333 else
3335 if (edit->mark2 >= 0)
3337 edit->end_mark_curs = -1;
3338 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3339 edit->curs_col + edit->over_col);
3340 edit->force |= REDRAW_PAGE;
3342 else
3344 edit->end_mark_curs = edit->curs1;
3345 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3346 edit->curs_col + edit->over_col);
3351 /* --------------------------------------------------------------------------------------------- */
3352 /** highlight the word under cursor */
3354 void
3355 edit_mark_current_word_cmd (WEdit * edit)
3357 long pos;
3359 for (pos = edit->curs1; pos != 0; pos--)
3361 int c1, c2;
3363 c1 = edit_get_byte (edit, pos);
3364 c2 = edit_get_byte (edit, pos - 1);
3365 if (!isspace (c1) && isspace (c2))
3366 break;
3367 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3368 break;
3370 edit->mark1 = pos;
3372 for (; pos < edit->last_byte; pos++)
3374 int c1, c2;
3376 c1 = edit_get_byte (edit, pos);
3377 c2 = edit_get_byte (edit, pos + 1);
3378 if (!isspace (c1) && isspace (c2))
3379 break;
3380 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3381 break;
3383 edit->mark2 = min (pos + 1, edit->last_byte);
3385 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3388 /* --------------------------------------------------------------------------------------------- */
3390 void
3391 edit_mark_current_line_cmd (WEdit * edit)
3393 long pos = edit->curs1;
3395 edit->mark1 = edit_bol (edit, pos);
3396 edit->mark2 = edit_eol (edit, pos);
3398 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3401 /* --------------------------------------------------------------------------------------------- */
3403 void
3404 edit_delete_line (WEdit * edit)
3407 * Delete right part of the line.
3408 * Note that edit_get_byte() returns '\n' when byte position is
3409 * beyond EOF.
3411 while (edit_get_byte (edit, edit->curs1) != '\n')
3413 (void) edit_delete (edit, 1);
3417 * Delete '\n' char.
3418 * Note that edit_delete() will not corrupt anything if called while
3419 * cursor position is EOF.
3421 (void) edit_delete (edit, 1);
3424 * Delete left part of the line.
3425 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3427 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3429 (void) edit_backspace (edit, 1);
3433 /* --------------------------------------------------------------------------------------------- */
3436 edit_indent_width (WEdit * edit, long p)
3438 long q = p;
3439 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3440 q++;
3441 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3444 /* --------------------------------------------------------------------------------------------- */
3446 void
3447 edit_insert_indent (WEdit * edit, int indent)
3449 if (!option_fill_tabs_with_spaces)
3451 while (indent >= TAB_SIZE)
3453 edit_insert (edit, '\t');
3454 indent -= TAB_SIZE;
3457 while (indent-- > 0)
3458 edit_insert (edit, ' ');
3461 /* --------------------------------------------------------------------------------------------- */
3463 void
3464 edit_push_key_press (WEdit * edit)
3466 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3467 if (edit->mark2 == -1)
3469 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3470 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3474 /* --------------------------------------------------------------------------------------------- */
3476 void
3477 edit_find_bracket (WEdit * edit)
3479 edit->bracket = edit_get_bracket (edit, 1, 10000);
3480 if (last_bracket != edit->bracket)
3481 edit->force |= REDRAW_PAGE;
3482 last_bracket = edit->bracket;
3485 /* --------------------------------------------------------------------------------------------- */
3487 * This executes a command as though the user initiated it through a key
3488 * press. Callback with WIDGET_KEY as a message calls this after
3489 * translating the key press. This function can be used to pass any
3490 * command to the editor. Note that the screen wouldn't update
3491 * automatically. Either of command or char_for_insertion must be
3492 * passed as -1. Commands are executed, and char_for_insertion is
3493 * inserted at the cursor.
3496 void
3497 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3499 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3500 || (macro_index < 0
3501 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3503 macro_index = 0;
3504 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3505 return;
3507 if (macro_index != -1)
3509 edit->force |= REDRAW_COMPLETELY;
3510 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3512 edit_store_macro_cmd (edit);
3513 macro_index = -1;
3514 return;
3516 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3518 edit_repeat_macro_cmd (edit);
3519 macro_index = -1;
3520 return;
3524 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3526 record_macro_buf[macro_index].action = command;
3527 record_macro_buf[macro_index++].ch = char_for_insertion;
3529 /* record the beginning of a set of editing actions initiated by a key press */
3530 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3531 edit_push_key_press (edit);
3533 edit_execute_cmd (edit, command, char_for_insertion);
3534 if (edit->column_highlight)
3535 edit->force |= REDRAW_PAGE;
3538 /* --------------------------------------------------------------------------------------------- */
3540 This executes a command at a lower level than macro recording.
3541 It also does not push a key_press onto the undo stack. This means
3542 that if it is called many times, a single undo command will undo
3543 all of them. It also does not check for the Undo command.
3545 void
3546 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3548 edit->force |= REDRAW_LINE;
3550 /* The next key press will unhighlight the found string, so update
3551 * the whole page */
3552 if (edit->found_len || edit->column_highlight)
3553 edit->force |= REDRAW_PAGE;
3555 switch (command)
3557 /* a mark command with shift-arrow */
3558 case CK_MarkLeft:
3559 case CK_MarkRight:
3560 case CK_MarkToWordBegin:
3561 case CK_MarkToWordEnd:
3562 case CK_MarkToHome:
3563 case CK_MarkToEnd:
3564 case CK_MarkUp:
3565 case CK_MarkDown:
3566 case CK_MarkPageUp:
3567 case CK_MarkPageDown:
3568 case CK_MarkToFileBegin:
3569 case CK_MarkToFileEnd:
3570 case CK_MarkToPageBegin:
3571 case CK_MarkToPageEnd:
3572 case CK_MarkScrollUp:
3573 case CK_MarkScrollDown:
3574 case CK_MarkParagraphUp:
3575 case CK_MarkParagraphDown:
3576 /* a mark command with alt-arrow */
3577 case CK_MarkColumnPageUp:
3578 case CK_MarkColumnPageDown:
3579 case CK_MarkColumnLeft:
3580 case CK_MarkColumnRight:
3581 case CK_MarkColumnUp:
3582 case CK_MarkColumnDown:
3583 case CK_MarkColumnScrollUp:
3584 case CK_MarkColumnScrollDown:
3585 case CK_MarkColumnParagraphUp:
3586 case CK_MarkColumnParagraphDown:
3587 edit->column_highlight = 0;
3588 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3590 edit_mark_cmd (edit, 1); /* clear */
3591 edit_mark_cmd (edit, 0); /* marking on */
3593 edit->highlight = 1;
3594 break;
3596 /* any other command */
3597 default:
3598 if (edit->highlight)
3599 edit_mark_cmd (edit, 0); /* clear */
3600 edit->highlight = 0;
3603 /* first check for undo */
3604 if (command == CK_Undo)
3606 edit->redo_stack_reset = 0;
3607 edit_group_undo (edit);
3608 edit->found_len = 0;
3609 edit->prev_col = edit_get_col (edit);
3610 edit->search_start = edit->curs1;
3611 return;
3613 /* check for redo */
3614 if (command == CK_Redo)
3616 edit->redo_stack_reset = 0;
3617 edit_do_redo (edit);
3618 edit->found_len = 0;
3619 edit->prev_col = edit_get_col (edit);
3620 edit->search_start = edit->curs1;
3621 return;
3624 edit->redo_stack_reset = 1;
3626 /* An ordinary key press */
3627 if (char_for_insertion >= 0)
3629 /* if non persistent selection and text selected */
3630 if (!option_persistent_selections)
3632 if (edit->mark1 != edit->mark2)
3633 edit_block_delete_cmd (edit);
3635 if (edit->overwrite)
3637 /* remove char only one time, after input first byte, multibyte chars */
3638 if ((!mc_global.utf8_display || edit->charpoint == 0)
3639 && edit_get_byte (edit, edit->curs1) != '\n')
3640 edit_delete (edit, 0);
3642 if (option_cursor_beyond_eol && edit->over_col > 0)
3643 edit_insert_over (edit);
3644 #ifdef HAVE_CHARSET
3645 if (char_for_insertion > 255 && mc_global.utf8_display == 0)
3647 unsigned char str[6 + 1];
3648 size_t i = 0;
3649 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3650 if (res == 0)
3652 str[0] = '.';
3653 str[1] = '\0';
3655 else
3657 str[res] = '\0';
3659 while (str[i] != 0 && i <= 6)
3661 char_for_insertion = str[i];
3662 edit_insert (edit, char_for_insertion);
3663 i++;
3666 else
3667 #endif
3668 edit_insert (edit, char_for_insertion);
3670 if (option_auto_para_formatting)
3672 format_paragraph (edit, 0);
3673 edit->force |= REDRAW_PAGE;
3675 else
3676 check_and_wrap_line (edit);
3677 edit->found_len = 0;
3678 edit->prev_col = edit_get_col (edit);
3679 edit->search_start = edit->curs1;
3680 edit_find_bracket (edit);
3681 return;
3684 switch (command)
3686 case CK_TopOnScreen:
3687 case CK_BottomOnScreen:
3688 case CK_MarkToPageBegin:
3689 case CK_MarkToPageEnd:
3690 case CK_Up:
3691 case CK_Down:
3692 case CK_Left:
3693 case CK_Right:
3694 case CK_WordLeft:
3695 case CK_WordRight:
3696 if (edit->mark2 >= 0)
3698 if (!option_persistent_selections)
3700 if (edit->column_highlight)
3701 edit_push_undo_action (edit, COLUMN_ON);
3702 edit->column_highlight = 0;
3703 edit_mark_cmd (edit, 1);
3708 switch (command)
3710 case CK_TopOnScreen:
3711 case CK_BottomOnScreen:
3712 case CK_MarkToPageBegin:
3713 case CK_MarkToPageEnd:
3714 case CK_Up:
3715 case CK_Down:
3716 case CK_WordLeft:
3717 case CK_WordRight:
3718 case CK_MarkToWordBegin:
3719 case CK_MarkToWordEnd:
3720 case CK_MarkUp:
3721 case CK_MarkDown:
3722 case CK_MarkColumnUp:
3723 case CK_MarkColumnDown:
3724 if (edit->mark2 == -1)
3725 break; /*marking is following the cursor: may need to highlight a whole line */
3726 case CK_Left:
3727 case CK_Right:
3728 case CK_MarkLeft:
3729 case CK_MarkRight:
3730 edit->force |= REDRAW_CHAR_ONLY;
3733 /* basic cursor key commands */
3734 switch (command)
3736 case CK_BackSpace:
3737 /* if non persistent selection and text selected */
3738 if (!option_persistent_selections)
3740 if (edit->mark1 != edit->mark2)
3742 edit_block_delete_cmd (edit);
3743 break;
3746 if (option_cursor_beyond_eol && edit->over_col > 0)
3748 edit->over_col--;
3749 break;
3751 if (option_backspace_through_tabs && is_in_indent (edit))
3753 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3754 edit_backspace (edit, 1);
3755 break;
3757 else
3759 if (option_fake_half_tabs)
3761 int i;
3762 if (is_in_indent (edit) && right_of_four_spaces (edit))
3764 for (i = 0; i < HALF_TAB_SIZE; i++)
3765 edit_backspace (edit, 1);
3766 break;
3770 edit_backspace (edit, 0);
3771 break;
3772 case CK_Delete:
3773 /* if non persistent selection and text selected */
3774 if (!option_persistent_selections)
3776 if (edit->mark1 != edit->mark2)
3778 edit_block_delete_cmd (edit);
3779 break;
3783 if (option_cursor_beyond_eol && edit->over_col > 0)
3784 edit_insert_over (edit);
3786 if (option_fake_half_tabs)
3788 int i;
3789 if (is_in_indent (edit) && left_of_four_spaces (edit))
3791 for (i = 1; i <= HALF_TAB_SIZE; i++)
3792 edit_delete (edit, 1);
3793 break;
3796 edit_delete (edit, 0);
3797 break;
3798 case CK_DeleteToWordBegin:
3799 edit->over_col = 0;
3800 edit_left_delete_word (edit);
3801 break;
3802 case CK_DeleteToWordEnd:
3803 if (option_cursor_beyond_eol && edit->over_col > 0)
3804 edit_insert_over (edit);
3806 edit_right_delete_word (edit);
3807 break;
3808 case CK_DeleteLine:
3809 edit_delete_line (edit);
3810 break;
3811 case CK_DeleteToHome:
3812 edit_delete_to_line_begin (edit);
3813 break;
3814 case CK_DeleteToEnd:
3815 edit_delete_to_line_end (edit);
3816 break;
3817 case CK_Enter:
3818 edit->over_col = 0;
3819 if (option_auto_para_formatting)
3821 edit_double_newline (edit);
3822 if (option_return_does_auto_indent)
3823 edit_auto_indent (edit);
3824 format_paragraph (edit, 0);
3826 else
3828 edit_insert (edit, '\n');
3829 if (option_return_does_auto_indent)
3831 edit_auto_indent (edit);
3834 break;
3835 case CK_Return:
3836 edit_insert (edit, '\n');
3837 break;
3839 case CK_MarkColumnPageUp:
3840 edit->column_highlight = 1;
3841 case CK_PageUp:
3842 case CK_MarkPageUp:
3843 edit_move_up (edit, edit->widget.lines - 1, 1);
3844 break;
3845 case CK_MarkColumnPageDown:
3846 edit->column_highlight = 1;
3847 case CK_PageDown:
3848 case CK_MarkPageDown:
3849 edit_move_down (edit, edit->widget.lines - 1, 1);
3850 break;
3851 case CK_MarkColumnLeft:
3852 edit->column_highlight = 1;
3853 case CK_Left:
3854 case CK_MarkLeft:
3855 if (option_fake_half_tabs)
3857 if (is_in_indent (edit) && right_of_four_spaces (edit))
3859 if (option_cursor_beyond_eol && edit->over_col > 0)
3860 edit->over_col--;
3861 else
3862 edit_cursor_move (edit, -HALF_TAB_SIZE);
3863 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3864 break;
3867 edit_left_char_move_cmd (edit);
3868 break;
3869 case CK_MarkColumnRight:
3870 edit->column_highlight = 1;
3871 case CK_Right:
3872 case CK_MarkRight:
3873 if (option_fake_half_tabs)
3875 if (is_in_indent (edit) && left_of_four_spaces (edit))
3877 edit_cursor_move (edit, HALF_TAB_SIZE);
3878 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3879 break;
3882 edit_right_char_move_cmd (edit);
3883 break;
3884 case CK_TopOnScreen:
3885 case CK_MarkToPageBegin:
3886 edit_begin_page (edit);
3887 break;
3888 case CK_BottomOnScreen:
3889 case CK_MarkToPageEnd:
3890 edit_end_page (edit);
3891 break;
3892 case CK_WordLeft:
3893 case CK_MarkToWordBegin:
3894 edit->over_col = 0;
3895 edit_left_word_move_cmd (edit);
3896 break;
3897 case CK_WordRight:
3898 case CK_MarkToWordEnd:
3899 edit->over_col = 0;
3900 edit_right_word_move_cmd (edit);
3901 break;
3902 case CK_MarkColumnUp:
3903 edit->column_highlight = 1;
3904 case CK_Up:
3905 case CK_MarkUp:
3906 edit_move_up (edit, 1, 0);
3907 break;
3908 case CK_MarkColumnDown:
3909 edit->column_highlight = 1;
3910 case CK_Down:
3911 case CK_MarkDown:
3912 edit_move_down (edit, 1, 0);
3913 break;
3914 case CK_MarkColumnParagraphUp:
3915 edit->column_highlight = 1;
3916 case CK_ParagraphUp:
3917 case CK_MarkParagraphUp:
3918 edit_move_up_paragraph (edit, 0);
3919 break;
3920 case CK_MarkColumnParagraphDown:
3921 edit->column_highlight = 1;
3922 case CK_ParagraphDown:
3923 case CK_MarkParagraphDown:
3924 edit_move_down_paragraph (edit, 0);
3925 break;
3926 case CK_MarkColumnScrollUp:
3927 edit->column_highlight = 1;
3928 case CK_ScrollUp:
3929 case CK_MarkScrollUp:
3930 edit_move_up (edit, 1, 1);
3931 break;
3932 case CK_MarkColumnScrollDown:
3933 edit->column_highlight = 1;
3934 case CK_ScrollDown:
3935 case CK_MarkScrollDown:
3936 edit_move_down (edit, 1, 1);
3937 break;
3938 case CK_Home:
3939 case CK_MarkToHome:
3940 edit_cursor_to_bol (edit);
3941 break;
3942 case CK_End:
3943 case CK_MarkToEnd:
3944 edit_cursor_to_eol (edit);
3945 break;
3946 case CK_Tab:
3947 /* if text marked shift block */
3948 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3950 if (edit->mark2 < 0)
3951 edit_mark_cmd (edit, 0);
3952 edit_move_block_to_right (edit);
3954 else
3956 if (option_cursor_beyond_eol)
3957 edit_insert_over (edit);
3958 edit_tab_cmd (edit);
3959 if (option_auto_para_formatting)
3961 format_paragraph (edit, 0);
3962 edit->force |= REDRAW_PAGE;
3964 else
3966 check_and_wrap_line (edit);
3969 break;
3971 case CK_InsertOverwrite:
3972 edit->overwrite = !edit->overwrite;
3973 break;
3975 case CK_Mark:
3976 if (edit->mark2 >= 0)
3978 if (edit->column_highlight)
3979 edit_push_undo_action (edit, COLUMN_ON);
3980 edit->column_highlight = 0;
3982 edit_mark_cmd (edit, 0);
3983 break;
3984 case CK_MarkColumn:
3985 if (!edit->column_highlight)
3986 edit_push_undo_action (edit, COLUMN_OFF);
3987 edit->column_highlight = 1;
3988 edit_mark_cmd (edit, 0);
3989 break;
3990 case CK_MarkAll:
3991 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3992 edit->force |= REDRAW_PAGE;
3993 break;
3994 case CK_Unmark:
3995 if (edit->column_highlight)
3996 edit_push_undo_action (edit, COLUMN_ON);
3997 edit->column_highlight = 0;
3998 edit_mark_cmd (edit, 1);
3999 break;
4000 case CK_MarkWord:
4001 if (edit->column_highlight)
4002 edit_push_undo_action (edit, COLUMN_ON);
4003 edit->column_highlight = 0;
4004 edit_mark_current_word_cmd (edit);
4005 break;
4006 case CK_MarkLine:
4007 if (edit->column_highlight)
4008 edit_push_undo_action (edit, COLUMN_ON);
4009 edit->column_highlight = 0;
4010 edit_mark_current_line_cmd (edit);
4011 break;
4013 case CK_ShowNumbers:
4014 option_line_state = !option_line_state;
4015 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
4016 edit->force |= REDRAW_PAGE;
4017 break;
4019 case CK_ShowMargin:
4020 show_right_margin = !show_right_margin;
4021 edit->force |= REDRAW_PAGE;
4022 break;
4024 case CK_Bookmark:
4025 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4026 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4027 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4028 else
4029 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4030 break;
4031 case CK_BookmarkFlush:
4032 book_mark_flush (edit, BOOK_MARK_COLOR);
4033 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4034 edit->force |= REDRAW_PAGE;
4035 break;
4036 case CK_BookmarkNext:
4037 if (edit->book_mark)
4039 struct _book_mark *p;
4040 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4041 if (p->next)
4043 p = p->next;
4044 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4045 edit_move_display (edit, p->line - edit->widget.lines / 2);
4046 edit_move_to_line (edit, p->line);
4049 break;
4050 case CK_BookmarkPrev:
4051 if (edit->book_mark)
4053 struct _book_mark *p;
4054 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4055 while (p->line == edit->curs_line)
4056 if (p->prev)
4057 p = p->prev;
4058 if (p->line >= 0)
4060 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4061 edit_move_display (edit, p->line - edit->widget.lines / 2);
4062 edit_move_to_line (edit, p->line);
4065 break;
4067 case CK_Top:
4068 case CK_MarkToFileBegin:
4069 edit_move_to_top (edit);
4070 break;
4071 case CK_Bottom:
4072 case CK_MarkToFileEnd:
4073 edit_move_to_bottom (edit);
4074 break;
4076 case CK_Copy:
4077 if (option_cursor_beyond_eol && edit->over_col > 0)
4078 edit_insert_over (edit);
4079 edit_block_copy_cmd (edit);
4080 break;
4081 case CK_Remove:
4082 edit_block_delete_cmd (edit);
4083 break;
4084 case CK_Move:
4085 edit_block_move_cmd (edit);
4086 break;
4088 case CK_BlockShiftLeft:
4089 if (edit->mark1 != edit->mark2)
4090 edit_move_block_to_left (edit);
4091 break;
4092 case CK_BlockShiftRight:
4093 if (edit->mark1 != edit->mark2)
4094 edit_move_block_to_right (edit);
4095 break;
4096 case CK_Store:
4097 edit_copy_to_X_buf_cmd (edit);
4098 break;
4099 case CK_Cut:
4100 edit_cut_to_X_buf_cmd (edit);
4101 break;
4102 case CK_Paste:
4103 /* if non persistent selection and text selected */
4104 if (!option_persistent_selections)
4106 if (edit->mark1 != edit->mark2)
4107 edit_block_delete_cmd (edit);
4109 if (option_cursor_beyond_eol && edit->over_col > 0)
4110 edit_insert_over (edit);
4111 edit_paste_from_X_buf_cmd (edit);
4112 break;
4113 case CK_History:
4114 edit_paste_from_history (edit);
4115 break;
4117 case CK_SaveAs:
4118 edit_save_as_cmd (edit);
4119 break;
4120 case CK_Save:
4121 edit_save_confirm_cmd (edit);
4122 break;
4123 case CK_EditFile:
4124 edit_load_cmd (edit, EDIT_FILE_COMMON);
4125 break;
4126 case CK_BlockSave:
4127 edit_save_block_cmd (edit);
4128 break;
4129 case CK_InsertFile:
4130 edit_insert_file_cmd (edit);
4131 break;
4133 case CK_FilePrev:
4134 edit_load_back_cmd (edit);
4135 break;
4136 case CK_FileNext:
4137 edit_load_forward_cmd (edit);
4138 break;
4140 case CK_EditSyntaxFile:
4141 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4142 break;
4143 case CK_SyntaxChoose:
4144 edit_syntax_dialog (edit);
4145 break;
4147 case CK_EditUserMenu:
4148 edit_load_cmd (edit, EDIT_FILE_MENU);
4149 break;
4151 case CK_SyntaxOnOff:
4152 option_syntax_highlighting ^= 1;
4153 if (option_syntax_highlighting == 1)
4154 edit_load_syntax (edit, NULL, edit->syntax_type);
4155 edit->force |= REDRAW_PAGE;
4156 break;
4158 case CK_ShowTabTws:
4159 enable_show_tabs_tws ^= 1;
4160 edit->force |= REDRAW_PAGE;
4161 break;
4163 case CK_Search:
4164 edit_search_cmd (edit, FALSE);
4165 break;
4166 case CK_SearchContinue:
4167 edit_search_cmd (edit, TRUE);
4168 break;
4169 case CK_Replace:
4170 edit_replace_cmd (edit, 0);
4171 break;
4172 case CK_ReplaceContinue:
4173 edit_replace_cmd (edit, 1);
4174 break;
4175 case CK_Complete:
4176 /* if text marked shift block */
4177 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4179 edit_move_block_to_left (edit);
4181 else
4183 edit_complete_word_cmd (edit);
4185 break;
4186 case CK_Find:
4187 edit_get_match_keyword_cmd (edit);
4188 break;
4189 case CK_Quit:
4190 dlg_stop (edit->widget.owner);
4191 break;
4192 case CK_EditNew:
4193 edit_new_cmd (edit);
4194 break;
4195 case CK_Help:
4196 edit_help_cmd (edit);
4197 break;
4198 case CK_Refresh:
4199 edit_refresh_cmd (edit);
4200 break;
4201 case CK_SaveSetup:
4202 save_setup_cmd ();
4203 break;
4204 case CK_About:
4205 edit_about ();
4206 break;
4207 case CK_LearnKeys:
4208 learn_keys ();
4209 break;
4210 case CK_Options:
4211 edit_options_dialog (edit);
4212 break;
4213 case CK_OptionsSaveMode:
4214 menu_save_mode_cmd ();
4215 break;
4216 case CK_Date:
4218 char s[BUF_MEDIUM];
4219 /* fool gcc to prevent a Y2K warning */
4220 char time_format[] = "_c";
4221 time_format[0] = '%';
4223 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4224 edit_print_string (edit, s);
4225 edit->force |= REDRAW_PAGE;
4226 break;
4228 break;
4229 case CK_Goto:
4230 edit_goto_cmd (edit);
4231 break;
4232 case CK_ParagraphFormat:
4233 format_paragraph (edit, 1);
4234 edit->force |= REDRAW_PAGE;
4235 break;
4236 case CK_MacroDelete:
4237 edit_delete_macro_cmd (edit);
4238 break;
4239 case CK_MatchBracket:
4240 edit_goto_matching_bracket (edit);
4241 break;
4242 case CK_UserMenu:
4243 user_menu (edit, NULL, -1);
4244 break;
4245 case CK_Sort:
4246 edit_sort_cmd (edit);
4247 break;
4248 case CK_ExternalCommand:
4249 edit_ext_cmd (edit);
4250 break;
4251 case CK_Mail:
4252 edit_mail_dialog (edit);
4253 break;
4254 case CK_Shell:
4255 view_other_cmd ();
4256 break;
4257 #ifdef HAVE_CHARSET
4258 case CK_SelectCodepage:
4259 edit_select_codepage_cmd (edit);
4260 break;
4261 #endif
4262 case CK_InsertLiteral:
4263 edit_insert_literal_cmd (edit);
4264 break;
4265 case CK_MacroStartStopRecord:
4266 edit_begin_end_macro_cmd (edit);
4267 break;
4268 case CK_RepeatStartStopRecord:
4269 edit_begin_end_repeat_cmd (edit);
4270 break;
4271 case CK_ExtendedKeyMap:
4272 edit->extmod = TRUE;
4273 break;
4274 default:
4275 break;
4278 /* CK_PipeBlock */
4279 if ((command / CK_PipeBlock (0)) == 1)
4280 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4282 /* keys which must set the col position, and the search vars */
4283 switch (command)
4285 case CK_Search:
4286 case CK_SearchContinue:
4287 case CK_Replace:
4288 case CK_ReplaceContinue:
4289 case CK_Complete:
4290 edit->prev_col = edit_get_col (edit);
4291 break;
4292 case CK_Up:
4293 case CK_MarkUp:
4294 case CK_MarkColumnUp:
4295 case CK_Down:
4296 case CK_MarkDown:
4297 case CK_MarkColumnDown:
4298 case CK_PageUp:
4299 case CK_MarkPageUp:
4300 case CK_MarkColumnPageUp:
4301 case CK_PageDown:
4302 case CK_MarkPageDown:
4303 case CK_MarkColumnPageDown:
4304 case CK_Top:
4305 case CK_MarkToFileBegin:
4306 case CK_Bottom:
4307 case CK_MarkToFileEnd:
4308 case CK_ParagraphUp:
4309 case CK_MarkParagraphUp:
4310 case CK_MarkColumnParagraphUp:
4311 case CK_ParagraphDown:
4312 case CK_MarkParagraphDown:
4313 case CK_MarkColumnParagraphDown:
4314 case CK_ScrollUp:
4315 case CK_MarkScrollUp:
4316 case CK_MarkColumnScrollUp:
4317 case CK_ScrollDown:
4318 case CK_MarkScrollDown:
4319 case CK_MarkColumnScrollDown:
4320 edit->search_start = edit->curs1;
4321 edit->found_len = 0;
4322 break;
4323 default:
4324 edit->found_len = 0;
4325 edit->prev_col = edit_get_col (edit);
4326 edit->search_start = edit->curs1;
4328 edit_find_bracket (edit);
4330 if (option_auto_para_formatting)
4332 switch (command)
4334 case CK_BackSpace:
4335 case CK_Delete:
4336 case CK_DeleteToWordBegin:
4337 case CK_DeleteToWordEnd:
4338 case CK_DeleteToHome:
4339 case CK_DeleteToEnd:
4340 format_paragraph (edit, 0);
4341 edit->force |= REDRAW_PAGE;
4346 /* --------------------------------------------------------------------------------------------- */
4348 void
4349 edit_stack_init (void)
4351 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4353 edit_history_moveto[edit_stack_iterator].filename = NULL;
4354 edit_history_moveto[edit_stack_iterator].line = -1;
4357 edit_stack_iterator = 0;
4360 /* --------------------------------------------------------------------------------------------- */
4362 void
4363 edit_stack_free (void)
4365 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4366 g_free (edit_history_moveto[edit_stack_iterator].filename);
4369 /* --------------------------------------------------------------------------------------------- */
4370 /** move i lines */
4372 void
4373 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4375 edit_move_updown (edit, i, do_scroll, TRUE);
4378 /* --------------------------------------------------------------------------------------------- */
4379 /** move i lines */
4381 void
4382 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4384 edit_move_updown (edit, i, do_scroll, FALSE);
4387 /* --------------------------------------------------------------------------------------------- */
4389 unsigned int
4390 edit_unlock_file (WEdit * edit)
4392 char *fullpath;
4393 unsigned int ret;
4395 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4396 ret = unlock_file (fullpath);
4397 g_free (fullpath);
4399 return ret;
4402 /* --------------------------------------------------------------------------------------------- */
4404 unsigned int
4405 edit_lock_file (WEdit * edit)
4407 char *fullpath;
4408 unsigned int ret;
4410 fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL);
4411 ret = lock_file (fullpath);
4412 g_free (fullpath);
4414 return ret;
4417 /* --------------------------------------------------------------------------------------------- */