Fix cursor position after execution of external script.
[midnight-commander/osp/mc.git] / src / editor / edit.c
blob66b2dd5e8f7b2287f5e822cda533eb9f016cbd94
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 Authors: Paul Sheer 1996, 1997
7 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
25 /** \file
26 * \brief Source: editor low level data handling and cursor fundamentals
27 * \author Paul Sheer
28 * \date 1996, 1997
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
41 #include <fcntl.h>
43 #include "lib/global.h"
45 #include "lib/tty/color.h"
46 #include "lib/tty/tty.h" /* attrset() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
49 #include "lib/vfs/mc-vfs/vfs.h"
50 #include "lib/strutil.h" /* utf string functions */
51 #include "lib/util.h" /* load_file_position(), save_file_position() */
52 #include "lib/timefmt.h" /* time formatting */
53 #include "lib/lock.h"
54 #include "lib/widget.h"
56 #ifdef HAVE_CHARSET
57 #include "lib/charsets.h" /* get_codepage_id */
58 #endif
60 #include "src/filemanager/cmd.h" /* view_other_cmd() */
61 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
63 #include "src/main.h" /* source_codepage */
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/learn.h" /* learn_keys */
66 #include "src/keybind-defaults.h"
68 #include "edit-impl.h"
69 #include "edit-widget.h"
71 /*** global variables ****************************************************************************/
73 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
74 int option_typewriter_wrap = 0;
75 int option_auto_para_formatting = 0;
76 int option_fill_tabs_with_spaces = 0;
77 int option_return_does_auto_indent = 1;
78 int option_backspace_through_tabs = 0;
79 int option_fake_half_tabs = 1;
80 int option_save_mode = EDIT_QUICK_SAVE;
81 int option_save_position = 1;
82 int option_max_undo = 32768;
83 int option_persistent_selections = 1;
84 int option_cursor_beyond_eol = 0;
85 int option_line_state = 0;
86 int option_line_state_width = 0;
88 int option_edit_right_extreme = 0;
89 int option_edit_left_extreme = 0;
90 int option_edit_top_extreme = 0;
91 int option_edit_bottom_extreme = 0;
92 int enable_show_tabs_tws = 1;
93 int option_check_nl_at_eof = 0;
94 int option_group_undo = 0;
95 int show_right_margin = 0;
97 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
98 char *option_backup_ext = NULL;
100 int edit_stack_iterator = 0;
101 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
102 /* magic sequense for say than block is vertical */
103 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
105 /*** file scope macro definitions ****************************************************************/
107 #define TEMP_BUF_LEN 1024
109 #define space_width 1
111 /*** file scope type declarations ****************************************************************/
113 /*** file scope variables ************************************************************************/
115 /* detecting an error on save is easy: just check if every byte has been written. */
116 /* detecting an error on read, is not so easy 'cos there is not way to tell
117 whether you read everything or not. */
118 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
119 static const struct edit_filters
121 const char *read, *write, *extension;
122 } all_filters[] =
124 /* *INDENT-OFF* */
125 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
126 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
127 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
129 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
130 /* *INDENT-ON* */
133 static long last_bracket = -1;
135 /*** file scope functions ************************************************************************/
136 /* --------------------------------------------------------------------------------------------- */
140 * here's a quick sketch of the layout: (don't run this through indent.)
142 * (b1 is buffers1 and b2 is buffers2)
145 * \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
146 * ______________________________________|______________________________________
148 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
149 * |-> |-> |-> |-> |-> |-> |
151 * _<------------------------->|<----------------->_
152 * WEdit->curs2 | WEdit->curs1
153 * ^ | ^
154 * | ^|^ |
155 * cursor ||| cursor
156 * |||
157 * file end|||file beginning
162 * This_is_some_file
163 * fin.
166 /* --------------------------------------------------------------------------------------------- */
168 static int left_of_four_spaces (WEdit * edit);
170 /* --------------------------------------------------------------------------------------------- */
172 static void
173 edit_about (void)
175 const char *header = N_("About");
176 const char *button_name = N_("&OK");
177 const char *const version = "MCEdit " VERSION;
178 char text[BUF_LARGE];
180 int win_len, version_len, button_len;
181 int cols, lines;
183 Dlg_head *about_dlg;
185 #ifdef ENABLE_NLS
186 header = _(header);
187 button_name = _(button_name);
188 #endif
190 button_len = str_term_width1 (button_name) + 5;
191 version_len = str_term_width1 (version);
193 g_snprintf (text, sizeof (text),
194 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
195 " A user friendly text editor\n"
196 " written for the Midnight Commander"));
198 win_len = str_term_width1 (header);
199 win_len = max (win_len, version_len);
200 win_len = max (win_len, button_len);
202 /* count width and height of text */
203 str_msg_term_size (text, &lines, &cols);
204 lines += 9;
205 cols = max (win_len, cols) + 6;
207 /* dialog */
208 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
209 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
211 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
212 add_widget (about_dlg, label_new (5, 3, text));
213 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
214 B_ENTER, NORMAL_BUTTON, button_name, NULL));
216 run_dlg (about_dlg);
217 destroy_dlg (about_dlg);
220 /* --------------------------------------------------------------------------------------------- */
222 * Initialize the buffers for an empty files.
225 static void
226 edit_init_buffers (WEdit * edit)
228 int j;
230 for (j = 0; j <= MAXBUFF; j++)
232 edit->buffers1[j] = NULL;
233 edit->buffers2[j] = NULL;
236 edit->curs1 = 0;
237 edit->curs2 = 0;
238 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
241 /* --------------------------------------------------------------------------------------------- */
243 * Load file OR text into buffers. Set cursor to the beginning of file.
244 * @returns 1 on error.
247 static int
248 edit_load_file_fast (WEdit * edit, const char *filename)
250 long buf, buf2;
251 int file = -1;
252 int ret = 1;
254 edit->curs2 = edit->last_byte;
255 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
257 file = mc_open (filename, O_RDONLY | O_BINARY);
258 if (file == -1)
260 gchar *errmsg;
262 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
263 edit_error_dialog (_("Error"), errmsg);
264 g_free (errmsg);
265 return 1;
268 if (!edit->buffers2[buf2])
269 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
273 if (mc_read (file,
274 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
275 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
276 break;
278 for (buf = buf2 - 1; buf >= 0; buf--)
280 /* edit->buffers2[0] is already allocated */
281 if (!edit->buffers2[buf])
282 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
283 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
284 break;
286 ret = 0;
288 while (0);
289 if (ret)
291 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
292 edit_error_dialog (_("Error"), err_str);
293 g_free (err_str);
295 mc_close (file);
296 return ret;
299 /* --------------------------------------------------------------------------------------------- */
300 /** Return index of the filter or -1 is there is no appropriate filter */
302 static int
303 edit_find_filter (const char *filename)
305 size_t i, l, e;
307 if (filename == NULL)
308 return -1;
310 l = strlen (filename);
311 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
313 e = strlen (all_filters[i].extension);
314 if (l > e)
315 if (!strcmp (all_filters[i].extension, filename + l - e))
316 return i;
318 return -1;
321 /* --------------------------------------------------------------------------------------------- */
323 static char *
324 edit_get_filter (const char *filename)
326 int i;
327 char *p, *quoted_name;
329 i = edit_find_filter (filename);
330 if (i < 0)
331 return NULL;
333 quoted_name = name_quote (filename, 0);
334 p = g_strdup_printf (all_filters[i].read, quoted_name);
335 g_free (quoted_name);
336 return p;
339 /* --------------------------------------------------------------------------------------------- */
341 static long
342 edit_insert_stream (WEdit * edit, FILE * f)
344 int c;
345 long i = 0;
346 while ((c = fgetc (f)) >= 0)
348 edit_insert (edit, c);
349 i++;
351 return i;
354 /* --------------------------------------------------------------------------------------------- */
355 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
357 static int
358 check_file_access (WEdit * edit, const char *filename, struct stat *st)
360 int file;
361 gchar *errmsg = NULL;
363 /* Try opening an existing file */
364 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
366 if (file < 0)
369 * Try creating the file. O_EXCL prevents following broken links
370 * and opening existing files.
372 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
373 if (file < 0)
375 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
376 goto cleanup;
378 else
380 /* New file, delete it if it's not modified or saved */
381 edit->delete_file = 1;
385 /* Check what we have opened */
386 if (mc_fstat (file, st) < 0)
388 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
389 goto cleanup;
392 /* We want to open regular files only */
393 if (!S_ISREG (st->st_mode))
395 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
396 goto cleanup;
400 * Don't delete non-empty files.
401 * O_EXCL should prevent it, but let's be on the safe side.
403 if (st->st_size > 0)
404 edit->delete_file = 0;
406 if (st->st_size >= SIZE_LIMIT)
407 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
409 cleanup:
410 (void) mc_close (file);
412 if (errmsg != NULL)
414 edit_error_dialog (_("Error"), errmsg);
415 g_free (errmsg);
416 return 1;
418 return 0;
421 /* --------------------------------------------------------------------------------------------- */
423 * Open the file and load it into the buffers, either directly or using
424 * a filter. Return 0 on success, 1 on error.
426 * Fast loading (edit_load_file_fast) is used when the file size is
427 * known. In this case the data is read into the buffers by blocks.
428 * If the file size is not known, the data is loaded byte by byte in
429 * edit_insert_file.
432 static int
433 edit_load_file (WEdit * edit)
435 int fast_load = 1;
437 /* Cannot do fast load if a filter is used */
438 if (edit_find_filter (edit->filename) >= 0)
439 fast_load = 0;
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (edit->filename))
446 fast_load = 0;
449 * FIXME: line end translation should disable fast loading as well
450 * Consider doing fseek() to the end and ftell() for the real size.
453 if (*edit->filename)
455 /* If we are dealing with a real file, check that it exists */
456 if (check_file_access (edit, edit->filename, &edit->stat1))
457 return 1;
459 else
461 /* nothing to load */
462 fast_load = 0;
465 edit_init_buffers (edit);
467 if (fast_load)
469 edit->last_byte = edit->stat1.st_size;
470 edit_load_file_fast (edit, edit->filename);
471 /* If fast load was used, the number of lines wasn't calculated */
472 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
474 else
476 edit->last_byte = 0;
477 if (*edit->filename)
479 edit->undo_stack_disable = 1;
480 if (edit_insert_file (edit, edit->filename) == 0)
482 edit_clean (edit);
483 return 1;
485 edit->undo_stack_disable = 0;
488 edit->lb = LB_ASIS;
489 return 0;
492 /* --------------------------------------------------------------------------------------------- */
493 /** Restore saved cursor position in the file */
495 static void
496 edit_load_position (WEdit * edit)
498 char *filename;
499 long line, column;
500 off_t offset;
502 if (!edit->filename || !*edit->filename)
503 return;
505 filename = vfs_canon (edit->filename);
506 load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
507 g_free (filename);
509 if (line > 0)
511 edit_move_to_line (edit, line - 1);
512 edit->prev_col = column;
514 else if (offset > 0)
516 edit_cursor_move (edit, offset);
517 line = edit->curs_line;
518 edit->search_start = edit->curs1;
521 book_mark_restore (edit, BOOK_MARK_COLOR);
523 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
524 edit_move_display (edit, line - (edit->num_widget_lines / 2));
527 /* --------------------------------------------------------------------------------------------- */
528 /** Save cursor position in the file */
530 static void
531 edit_save_position (WEdit * edit)
533 char *filename;
535 if (edit->filename == NULL || *edit->filename == '\0')
536 return;
538 filename = vfs_canon (edit->filename);
540 book_mark_serialize (edit, BOOK_MARK_COLOR);
541 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
542 edit->serialized_bookmarks);
543 edit->serialized_bookmarks = NULL;
545 g_free (filename);
548 /* --------------------------------------------------------------------------------------------- */
549 /** Clean the WEdit stricture except the widget part */
551 static void
552 edit_purge_widget (WEdit * edit)
554 size_t len = sizeof (WEdit) - sizeof (Widget);
555 char *start = (char *) edit + sizeof (Widget);
556 memset (start, 0, len);
559 /* --------------------------------------------------------------------------------------------- */
561 static void
562 edit_set_keymap (void)
564 editor_map = default_editor_keymap;
565 if (editor_keymap && editor_keymap->len > 0)
566 editor_map = (global_keymap_t *) editor_keymap->data;
568 editor_x_map = default_editor_x_keymap;
569 if (editor_x_keymap && editor_x_keymap->len > 0)
570 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
573 /* --------------------------------------------------------------------------------------------- */
575 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
576 then the file should be as it was when he loaded up. Then set edit->modified to 0.
579 static long
580 edit_pop_undo_action (WEdit * edit)
582 long c;
583 unsigned long sp = edit->undo_stack_pointer;
585 if (sp == edit->undo_stack_bottom)
586 return STACK_BOTTOM;
588 sp = (sp - 1) & edit->undo_stack_size_mask;
589 c = edit->undo_stack[sp];
590 if (c >= 0)
592 /* edit->undo_stack[sp] = '@'; */
593 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
594 return c;
597 if (sp == edit->undo_stack_bottom)
598 return STACK_BOTTOM;
600 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
601 if (edit->undo_stack[sp] == -2)
603 /* edit->undo_stack[sp] = '@'; */
604 edit->undo_stack_pointer = sp;
606 else
607 edit->undo_stack[sp]++;
609 return c;
612 static long
613 edit_pop_redo_action (WEdit * edit)
615 long c;
616 unsigned long sp = edit->redo_stack_pointer;
618 if (sp == edit->redo_stack_bottom)
619 return STACK_BOTTOM;
621 sp = (sp - 1) & edit->redo_stack_size_mask;
622 c = edit->redo_stack[sp];
623 if (c >= 0)
625 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
626 return c;
629 if (sp == edit->redo_stack_bottom)
630 return STACK_BOTTOM;
632 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
633 if (edit->redo_stack[sp] == -2)
634 edit->redo_stack_pointer = sp;
635 else
636 edit->redo_stack[sp]++;
638 return c;
641 static long
642 get_prev_undo_action (WEdit * edit)
644 long c;
645 unsigned long sp = edit->undo_stack_pointer;
647 if (sp == edit->undo_stack_bottom)
648 return STACK_BOTTOM;
650 sp = (sp - 1) & edit->undo_stack_size_mask;
651 c = edit->undo_stack[sp];
652 if (c >= 0)
653 return c;
655 if (sp == edit->undo_stack_bottom)
656 return STACK_BOTTOM;
658 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
659 return c;
662 /* --------------------------------------------------------------------------------------------- */
663 /** is called whenever a modification is made by one of the four routines below */
665 static void
666 edit_modification (WEdit * edit)
668 edit->caches_valid = 0;
669 edit->screen_modified = 1;
671 /* raise lock when file modified */
672 if (!edit->modified && !edit->delete_file)
673 edit->locked = edit_lock_file (edit);
674 edit->modified = 1;
677 /* --------------------------------------------------------------------------------------------- */
679 static void
680 edit_insert_over (WEdit * edit)
682 int i;
684 for (i = 0; i < edit->over_col; i++)
686 edit_insert (edit, ' ');
688 edit->over_col = 0;
691 /* --------------------------------------------------------------------------------------------- */
693 static int
694 edit_backspace (WEdit * edit, const int byte_delete)
696 int p = 0;
697 int cw = 1;
698 int i;
700 if (!edit->curs1)
701 return 0;
703 cw = 1;
705 if (edit->mark2 != edit->mark1)
706 edit_push_markers (edit);
708 if (edit->utf8 && byte_delete == 0)
710 edit_get_prev_utf (edit, edit->curs1, &cw);
711 if (cw < 1)
712 cw = 1;
714 for (i = 1; i <= cw; i++)
716 if (edit->mark1 >= edit->curs1)
718 edit->mark1--;
719 edit->end_mark_curs--;
721 if (edit->mark2 >= edit->curs1)
722 edit->mark2--;
723 if (edit->last_get_rule >= edit->curs1)
724 edit->last_get_rule--;
726 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
727 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
728 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
730 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
731 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
733 edit->last_byte--;
734 edit->curs1--;
735 edit_push_undo_action (edit, p);
737 edit_modification (edit);
738 if (p == '\n')
740 if (edit->book_mark)
741 book_mark_dec (edit, edit->curs_line);
742 edit->curs_line--;
743 edit->total_lines--;
744 edit->force |= REDRAW_AFTER_CURSOR;
747 if (edit->curs1 < edit->start_display)
749 edit->start_display--;
750 if (p == '\n')
751 edit->start_line--;
754 return p;
757 /* --------------------------------------------------------------------------------------------- */
758 /* high level cursor movement commands */
759 /* --------------------------------------------------------------------------------------------- */
761 static int
762 is_in_indent (WEdit * edit)
764 long p = edit_bol (edit, edit->curs1);
765 while (p < edit->curs1)
766 if (!strchr (" \t", edit_get_byte (edit, p++)))
767 return 0;
768 return 1;
771 /* --------------------------------------------------------------------------------------------- */
773 static int
774 is_blank (WEdit * edit, long offset)
776 long s, f;
777 int c;
778 s = edit_bol (edit, offset);
779 f = edit_eol (edit, offset) - 1;
780 while (s <= f)
782 c = edit_get_byte (edit, s++);
783 if (!isspace (c))
784 return 0;
786 return 1;
790 /* --------------------------------------------------------------------------------------------- */
791 /** returns the offset of line i */
793 static long
794 edit_find_line (WEdit * edit, int line)
796 int i, j = 0;
797 int m = 2000000000;
798 if (!edit->caches_valid)
800 for (i = 0; i < N_LINE_CACHES; i++)
801 edit->line_numbers[i] = edit->line_offsets[i] = 0;
802 /* three offsets that we *know* are line 0 at 0 and these two: */
803 edit->line_numbers[1] = edit->curs_line;
804 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
805 edit->line_numbers[2] = edit->total_lines;
806 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
807 edit->caches_valid = 1;
809 if (line >= edit->total_lines)
810 return edit->line_offsets[2];
811 if (line <= 0)
812 return 0;
813 /* find the closest known point */
814 for (i = 0; i < N_LINE_CACHES; i++)
816 int n;
817 n = abs (edit->line_numbers[i] - line);
818 if (n < m)
820 m = n;
821 j = i;
824 if (m == 0)
825 return edit->line_offsets[j]; /* know the offset exactly */
826 if (m == 1 && j >= 3)
827 i = j; /* one line different - caller might be looping, so stay in this cache */
828 else
829 i = 3 + (rand () % (N_LINE_CACHES - 3));
830 if (line > edit->line_numbers[j])
831 edit->line_offsets[i] =
832 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
833 else
834 edit->line_offsets[i] =
835 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
836 edit->line_numbers[i] = line;
837 return edit->line_offsets[i];
840 /* --------------------------------------------------------------------------------------------- */
841 /** moves up until a blank line is reached, or until just
842 before a non-blank line is reached */
844 static void
845 edit_move_up_paragraph (WEdit * edit, int do_scroll)
847 int i = 0;
848 if (edit->curs_line > 1)
850 if (line_is_blank (edit, edit->curs_line))
852 if (line_is_blank (edit, edit->curs_line - 1))
854 for (i = edit->curs_line - 1; i; i--)
855 if (!line_is_blank (edit, i))
857 i++;
858 break;
861 else
863 for (i = edit->curs_line - 1; i; i--)
864 if (line_is_blank (edit, i))
865 break;
868 else
870 for (i = edit->curs_line - 1; i; i--)
871 if (line_is_blank (edit, i))
872 break;
875 edit_move_up (edit, edit->curs_line - i, do_scroll);
878 /* --------------------------------------------------------------------------------------------- */
879 /** moves down until a blank line is reached, or until just
880 before a non-blank line is reached */
882 static void
883 edit_move_down_paragraph (WEdit * edit, int do_scroll)
885 int i;
886 if (edit->curs_line >= edit->total_lines - 1)
888 i = edit->total_lines;
890 else
892 if (line_is_blank (edit, edit->curs_line))
894 if (line_is_blank (edit, edit->curs_line + 1))
896 for (i = edit->curs_line + 1; i; i++)
897 if (!line_is_blank (edit, i) || i > edit->total_lines)
899 i--;
900 break;
903 else
905 for (i = edit->curs_line + 1; i; i++)
906 if (line_is_blank (edit, i) || i >= edit->total_lines)
907 break;
910 else
912 for (i = edit->curs_line + 1; i; i++)
913 if (line_is_blank (edit, i) || i >= edit->total_lines)
914 break;
917 edit_move_down (edit, i - edit->curs_line, do_scroll);
920 /* --------------------------------------------------------------------------------------------- */
922 static void
923 edit_begin_page (WEdit * edit)
925 edit_update_curs_row (edit);
926 edit_move_up (edit, edit->curs_row, 0);
929 /* --------------------------------------------------------------------------------------------- */
931 static void
932 edit_end_page (WEdit * edit)
934 edit_update_curs_row (edit);
935 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
939 /* --------------------------------------------------------------------------------------------- */
940 /** goto beginning of text */
942 static void
943 edit_move_to_top (WEdit * edit)
945 if (edit->curs_line)
947 edit_cursor_move (edit, -edit->curs1);
948 edit_move_to_prev_col (edit, 0);
949 edit->force |= REDRAW_PAGE;
950 edit->search_start = 0;
951 edit_update_curs_row (edit);
956 /* --------------------------------------------------------------------------------------------- */
957 /** goto end of text */
959 static void
960 edit_move_to_bottom (WEdit * edit)
962 if (edit->curs_line < edit->total_lines)
964 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
965 edit->start_display = edit->last_byte;
966 edit->start_line = edit->total_lines;
967 edit_scroll_upward (edit, edit->num_widget_lines - 1);
968 edit->force |= REDRAW_PAGE;
972 /* --------------------------------------------------------------------------------------------- */
973 /** goto beginning of line */
975 static void
976 edit_cursor_to_bol (WEdit * edit)
978 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
979 edit->search_start = edit->curs1;
980 edit->prev_col = edit_get_col (edit);
981 edit->over_col = 0;
984 /* --------------------------------------------------------------------------------------------- */
985 /** goto end of line */
987 static void
988 edit_cursor_to_eol (WEdit * edit)
990 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
991 edit->search_start = edit->curs1;
992 edit->prev_col = edit_get_col (edit);
993 edit->over_col = 0;
996 /* --------------------------------------------------------------------------------------------- */
998 static unsigned long
999 my_type_of (int c)
1001 int x, r = 0;
1002 const char *p, *q;
1003 const char option_chars_move_whole_word[] =
1004 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1006 if (!c)
1007 return 0;
1008 if (c == '!')
1010 if (*option_chars_move_whole_word == '!')
1011 return 2;
1012 return 0x80000000UL;
1014 if (g_ascii_isupper ((gchar) c))
1015 c = 'A';
1016 else if (g_ascii_islower ((gchar) c))
1017 c = 'a';
1018 else if (g_ascii_isalpha (c))
1019 c = 'a';
1020 else if (isdigit (c))
1021 c = '0';
1022 else if (isspace (c))
1023 c = ' ';
1024 q = strchr (option_chars_move_whole_word, c);
1025 if (!q)
1026 return 0xFFFFFFFFUL;
1029 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1030 if (*p == '!')
1031 x <<= 1;
1032 r |= x;
1034 while ((q = strchr (q + 1, c)));
1035 return r;
1038 /* --------------------------------------------------------------------------------------------- */
1040 static void
1041 edit_left_word_move (WEdit * edit, int s)
1043 for (;;)
1045 int c1, c2;
1046 if (edit->column_highlight
1047 && edit->mark1 != edit->mark2
1048 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1049 break;
1050 edit_cursor_move (edit, -1);
1051 if (!edit->curs1)
1052 break;
1053 c1 = edit_get_byte (edit, edit->curs1 - 1);
1054 c2 = edit_get_byte (edit, edit->curs1);
1055 if (c1 == '\n' || c2 == '\n')
1056 break;
1057 if (!(my_type_of (c1) & my_type_of (c2)))
1058 break;
1059 if (isspace (c1) && !isspace (c2))
1060 break;
1061 if (s)
1062 if (!isspace (c1) && isspace (c2))
1063 break;
1067 /* --------------------------------------------------------------------------------------------- */
1069 static void
1070 edit_left_word_move_cmd (WEdit * edit)
1072 edit_left_word_move (edit, 0);
1073 edit->force |= REDRAW_PAGE;
1076 /* --------------------------------------------------------------------------------------------- */
1078 static void
1079 edit_right_word_move (WEdit * edit, int s)
1081 for (;;)
1083 int c1, c2;
1084 if (edit->column_highlight
1085 && edit->mark1 != edit->mark2
1086 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1087 break;
1088 edit_cursor_move (edit, 1);
1089 if (edit->curs1 >= edit->last_byte)
1090 break;
1091 c1 = edit_get_byte (edit, edit->curs1 - 1);
1092 c2 = edit_get_byte (edit, edit->curs1);
1093 if (c1 == '\n' || c2 == '\n')
1094 break;
1095 if (!(my_type_of (c1) & my_type_of (c2)))
1096 break;
1097 if (isspace (c1) && !isspace (c2))
1098 break;
1099 if (s)
1100 if (!isspace (c1) && isspace (c2))
1101 break;
1105 /* --------------------------------------------------------------------------------------------- */
1107 static void
1108 edit_right_word_move_cmd (WEdit * edit)
1110 edit_right_word_move (edit, 0);
1111 edit->force |= REDRAW_PAGE;
1114 /* --------------------------------------------------------------------------------------------- */
1116 static void
1117 edit_right_char_move_cmd (WEdit * edit)
1119 int cw = 1;
1120 int c = 0;
1121 if (edit->utf8)
1123 c = edit_get_utf (edit, edit->curs1, &cw);
1124 if (cw < 1)
1125 cw = 1;
1127 else
1129 c = edit_get_byte (edit, edit->curs1);
1131 if (option_cursor_beyond_eol && c == '\n')
1133 edit->over_col++;
1135 else
1137 edit_cursor_move (edit, cw);
1141 /* --------------------------------------------------------------------------------------------- */
1143 static void
1144 edit_left_char_move_cmd (WEdit * edit)
1146 int cw = 1;
1147 if (edit->column_highlight
1148 && option_cursor_beyond_eol
1149 && edit->mark1 != edit->mark2
1150 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1151 return;
1152 if (edit->utf8)
1154 edit_get_prev_utf (edit, edit->curs1, &cw);
1155 if (cw < 1)
1156 cw = 1;
1158 if (option_cursor_beyond_eol && edit->over_col > 0)
1160 edit->over_col--;
1162 else
1164 edit_cursor_move (edit, -cw);
1168 /* --------------------------------------------------------------------------------------------- */
1169 /** Up or down cursor moving.
1170 direction = TRUE - move up
1171 = FALSE - move down
1174 static void
1175 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1177 unsigned long p;
1178 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1180 if (i > l)
1181 i = l;
1183 if (i == 0)
1184 return;
1186 if (i > 1)
1187 edit->force |= REDRAW_PAGE;
1188 if (do_scroll)
1190 if (direction)
1191 edit_scroll_upward (edit, i);
1192 else
1193 edit_scroll_downward (edit, i);
1195 p = edit_bol (edit, edit->curs1);
1197 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1199 edit_cursor_move (edit, p - edit->curs1);
1201 edit_move_to_prev_col (edit, p);
1203 /* search start of current multibyte char (like CJK) */
1204 if (edit->curs1 + 1 < edit->last_byte)
1206 edit_right_char_move_cmd (edit);
1207 edit_left_char_move_cmd (edit);
1210 edit->search_start = edit->curs1;
1211 edit->found_len = 0;
1214 /* --------------------------------------------------------------------------------------------- */
1216 static void
1217 edit_right_delete_word (WEdit * edit)
1219 int c1, c2;
1220 for (;;)
1222 if (edit->curs1 >= edit->last_byte)
1223 break;
1224 c1 = edit_delete (edit, 1);
1225 c2 = edit_get_byte (edit, edit->curs1);
1226 if (c1 == '\n' || c2 == '\n')
1227 break;
1228 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1229 break;
1230 if (!(my_type_of (c1) & my_type_of (c2)))
1231 break;
1235 /* --------------------------------------------------------------------------------------------- */
1237 static void
1238 edit_left_delete_word (WEdit * edit)
1240 int c1, c2;
1241 for (;;)
1243 if (edit->curs1 <= 0)
1244 break;
1245 c1 = edit_backspace (edit, 1);
1246 c2 = edit_get_byte (edit, edit->curs1 - 1);
1247 if (c1 == '\n' || c2 == '\n')
1248 break;
1249 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1250 break;
1251 if (!(my_type_of (c1) & my_type_of (c2)))
1252 break;
1256 /* --------------------------------------------------------------------------------------------- */
1258 the start column position is not recorded, and hence does not
1259 undo as it happed. But who would notice.
1262 static void
1263 edit_do_undo (WEdit * edit)
1265 long ac;
1266 long count = 0;
1268 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1269 edit->over_col = 0;
1270 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1272 switch ((int) ac)
1274 case STACK_BOTTOM:
1275 goto done_undo;
1276 case CURS_RIGHT:
1277 edit_cursor_move (edit, 1);
1278 break;
1279 case CURS_LEFT:
1280 edit_cursor_move (edit, -1);
1281 break;
1282 case BACKSPACE:
1283 case BACKSPACE_BR:
1284 edit_backspace (edit, 1);
1285 break;
1286 case DELCHAR:
1287 case DELCHAR_BR:
1288 edit_delete (edit, 1);
1289 break;
1290 case COLUMN_ON:
1291 edit->column_highlight = 1;
1292 break;
1293 case COLUMN_OFF:
1294 edit->column_highlight = 0;
1295 break;
1297 if (ac >= 256 && ac < 512)
1298 edit_insert_ahead (edit, ac - 256);
1299 if (ac >= 0 && ac < 256)
1300 edit_insert (edit, ac);
1302 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1304 edit->mark1 = ac - MARK_1;
1305 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1307 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1309 edit->mark2 = ac - MARK_2;
1310 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1312 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1314 edit->end_mark_curs = ac - MARK_CURS;
1316 if (count++)
1317 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1320 if (edit->start_display > ac - KEY_PRESS)
1322 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1323 edit->force |= REDRAW_PAGE;
1325 else if (edit->start_display < ac - KEY_PRESS)
1327 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1328 edit->force |= REDRAW_PAGE;
1330 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1331 edit_update_curs_row (edit);
1333 done_undo:;
1334 edit->undo_stack_disable = 0;
1337 static void
1338 edit_do_redo (WEdit * edit)
1340 long ac;
1341 long count = 0;
1343 if (edit->redo_stack_reset)
1344 return;
1346 edit->over_col = 0;
1347 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1349 switch ((int) ac)
1351 case STACK_BOTTOM:
1352 goto done_redo;
1353 case CURS_RIGHT:
1354 edit_cursor_move (edit, 1);
1355 break;
1356 case CURS_LEFT:
1357 edit_cursor_move (edit, -1);
1358 break;
1359 case BACKSPACE:
1360 edit_backspace (edit, 1);
1361 break;
1362 case DELCHAR:
1363 edit_delete (edit, 1);
1364 break;
1365 case COLUMN_ON:
1366 edit->column_highlight = 1;
1367 break;
1368 case COLUMN_OFF:
1369 edit->column_highlight = 0;
1370 break;
1372 if (ac >= 256 && ac < 512)
1373 edit_insert_ahead (edit, ac - 256);
1374 if (ac >= 0 && ac < 256)
1375 edit_insert (edit, ac);
1377 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1379 edit->mark1 = ac - MARK_1;
1380 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1382 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1384 edit->mark2 = ac - MARK_2;
1385 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1387 /* more than one pop usually means something big */
1388 if (count++)
1389 edit->force |= REDRAW_PAGE;
1392 if (edit->start_display > ac - KEY_PRESS)
1394 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1395 edit->force |= REDRAW_PAGE;
1397 else if (edit->start_display < ac - KEY_PRESS)
1399 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1400 edit->force |= REDRAW_PAGE;
1402 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1403 edit_update_curs_row (edit);
1405 done_redo:;
1408 static void
1409 edit_group_undo (WEdit * edit)
1411 long ac = KEY_PRESS;
1412 long cur_ac = KEY_PRESS;
1413 while (ac != STACK_BOTTOM && ac == cur_ac)
1415 cur_ac = get_prev_undo_action (edit);
1416 edit_do_undo (edit);
1417 ac = get_prev_undo_action (edit);
1418 /* exit from cycle if option_group_undo is not set,
1419 * and make single UNDO operation
1421 if (!option_group_undo)
1422 ac = STACK_BOTTOM;
1426 /* --------------------------------------------------------------------------------------------- */
1428 static void
1429 edit_delete_to_line_end (WEdit * edit)
1431 while (edit_get_byte (edit, edit->curs1) != '\n')
1433 if (!edit->curs2)
1434 break;
1435 edit_delete (edit, 1);
1439 /* --------------------------------------------------------------------------------------------- */
1441 static void
1442 edit_delete_to_line_begin (WEdit * edit)
1444 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1446 if (!edit->curs1)
1447 break;
1448 edit_backspace (edit, 1);
1452 /* --------------------------------------------------------------------------------------------- */
1454 static int
1455 is_aligned_on_a_tab (WEdit * edit)
1457 edit_update_curs_col (edit);
1458 return !((edit->curs_col % (TAB_SIZE * space_width))
1459 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1462 /* --------------------------------------------------------------------------------------------- */
1464 static int
1465 right_of_four_spaces (WEdit * edit)
1467 int i, ch = 0;
1468 for (i = 1; i <= HALF_TAB_SIZE; i++)
1469 ch |= edit_get_byte (edit, edit->curs1 - i);
1470 if (ch == ' ')
1471 return is_aligned_on_a_tab (edit);
1472 return 0;
1475 /* --------------------------------------------------------------------------------------------- */
1477 static int
1478 left_of_four_spaces (WEdit * edit)
1480 int i, ch = 0;
1481 for (i = 0; i < HALF_TAB_SIZE; i++)
1482 ch |= edit_get_byte (edit, edit->curs1 + i);
1483 if (ch == ' ')
1484 return is_aligned_on_a_tab (edit);
1485 return 0;
1488 /* --------------------------------------------------------------------------------------------- */
1490 static void
1491 edit_auto_indent (WEdit * edit)
1493 long p;
1494 char c;
1495 p = edit->curs1;
1496 /* use the previous line as a template */
1497 p = edit_move_backward (edit, p, 1);
1498 /* copy the leading whitespace of the line */
1499 for (;;)
1500 { /* no range check - the line _is_ \n-terminated */
1501 c = edit_get_byte (edit, p++);
1502 if (c != ' ' && c != '\t')
1503 break;
1504 edit_insert (edit, c);
1508 /* --------------------------------------------------------------------------------------------- */
1510 static inline void
1511 edit_double_newline (WEdit * edit)
1513 edit_insert (edit, '\n');
1514 if (edit_get_byte (edit, edit->curs1) == '\n')
1515 return;
1516 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1517 return;
1518 edit->force |= REDRAW_PAGE;
1519 edit_insert (edit, '\n');
1522 /* --------------------------------------------------------------------------------------------- */
1524 static inline void
1525 edit_tab_cmd (WEdit * edit)
1527 int i;
1529 if (option_fake_half_tabs)
1531 if (is_in_indent (edit))
1533 /*insert a half tab (usually four spaces) unless there is a
1534 half tab already behind, then delete it and insert a
1535 full tab. */
1536 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
1538 for (i = 1; i <= HALF_TAB_SIZE; i++)
1539 edit_backspace (edit, 1);
1540 edit_insert (edit, '\t');
1542 else
1544 insert_spaces_tab (edit, 1);
1546 return;
1549 if (option_fill_tabs_with_spaces)
1551 insert_spaces_tab (edit, 0);
1553 else
1555 edit_insert (edit, '\t');
1559 /* --------------------------------------------------------------------------------------------- */
1561 static void
1562 check_and_wrap_line (WEdit * edit)
1564 int curs, c;
1565 if (!option_typewriter_wrap)
1566 return;
1567 edit_update_curs_col (edit);
1568 if (edit->curs_col < option_word_wrap_line_length)
1569 return;
1570 curs = edit->curs1;
1571 for (;;)
1573 curs--;
1574 c = edit_get_byte (edit, curs);
1575 if (c == '\n' || curs <= 0)
1577 edit_insert (edit, '\n');
1578 return;
1580 if (c == ' ' || c == '\t')
1582 int current = edit->curs1;
1583 edit_cursor_move (edit, curs - edit->curs1 + 1);
1584 edit_insert (edit, '\n');
1585 edit_cursor_move (edit, current - edit->curs1 + 1);
1586 return;
1591 /* --------------------------------------------------------------------------------------------- */
1592 /** this find the matching bracket in either direction, and sets edit->bracket */
1594 static long
1595 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1597 const char *const b = "{}{[][()(", *p;
1598 int i = 1, a, inc = -1, c, d, n = 0;
1599 unsigned long j = 0;
1600 long q;
1601 edit_update_curs_row (edit);
1602 c = edit_get_byte (edit, edit->curs1);
1603 p = strchr (b, c);
1604 /* no limit */
1605 if (!furthest_bracket_search)
1606 furthest_bracket_search--;
1607 /* not on a bracket at all */
1608 if (!p)
1609 return -1;
1610 /* the matching bracket */
1611 d = p[1];
1612 /* going left or right? */
1613 if (strchr ("{[(", c))
1614 inc = 1;
1615 for (q = edit->curs1 + inc;; q += inc)
1617 /* out of buffer? */
1618 if (q >= edit->last_byte || q < 0)
1619 break;
1620 a = edit_get_byte (edit, q);
1621 /* don't want to eat CPU */
1622 if (j++ > furthest_bracket_search)
1623 break;
1624 /* out of screen? */
1625 if (in_screen)
1627 if (q < edit->start_display)
1628 break;
1629 /* count lines if searching downward */
1630 if (inc > 0 && a == '\n')
1631 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
1632 break;
1634 /* count bracket depth */
1635 i += (a == c) - (a == d);
1636 /* return if bracket depth is zero */
1637 if (!i)
1638 return q;
1640 /* no match */
1641 return -1;
1644 /* --------------------------------------------------------------------------------------------- */
1646 static inline void
1647 edit_goto_matching_bracket (WEdit * edit)
1649 long q;
1651 q = edit_get_bracket (edit, 0, 0);
1652 if (q >= 0)
1654 edit->bracket = edit->curs1;
1655 edit->force |= REDRAW_PAGE;
1656 edit_cursor_move (edit, q - edit->curs1);
1660 /* --------------------------------------------------------------------------------------------- */
1661 /*** public functions ****************************************************************************/
1662 /* --------------------------------------------------------------------------------------------- */
1664 /** User edit menu, like user menu (F2) but only in editor. */
1666 void
1667 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1669 char *block_file;
1670 int nomark;
1671 long curs;
1672 long start_mark, end_mark;
1673 struct stat status;
1675 block_file = concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE);
1676 curs = edit->curs1;
1677 nomark = eval_marks (edit, &start_mark, &end_mark);
1678 if (nomark == 0)
1679 edit_save_block (edit, block_file, start_mark, end_mark);
1681 /* run shell scripts from menu */
1682 if (user_menu_cmd (edit, menu_file, selected_entry)
1683 && (mc_stat (block_file, &status) == 0)
1684 && (status.st_size != 0))
1686 int rc = 0;
1687 FILE *fd;
1689 /* i.e. we have marked block */
1690 if (nomark == 0)
1691 rc = edit_block_delete_cmd (edit);
1693 if (rc == 0)
1695 long ins_len;
1697 ins_len = edit_insert_file (edit, block_file);
1698 if (nomark == 0 && ins_len > 0)
1699 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1701 /* truncate block file */
1702 fd = fopen (block_file, "w");
1703 if (fd != NULL)
1704 fclose (fd);
1706 edit_cursor_move (edit, curs - edit->curs1);
1707 edit_refresh_cmd (edit);
1708 edit->force |= REDRAW_COMPLETELY;
1710 g_free (block_file);
1713 /* --------------------------------------------------------------------------------------------- */
1716 edit_get_byte (WEdit * edit, long byte_index)
1718 unsigned long p;
1719 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1720 return '\n';
1722 if (byte_index >= edit->curs1)
1724 p = edit->curs1 + edit->curs2 - byte_index - 1;
1725 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1727 else
1729 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1733 /* --------------------------------------------------------------------------------------------- */
1735 char *
1736 edit_get_byte_ptr (WEdit * edit, long byte_index)
1738 unsigned long p;
1739 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1740 return NULL;
1742 if (byte_index >= edit->curs1)
1744 p = edit->curs1 + edit->curs2 - byte_index - 1;
1745 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1746 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1748 else
1750 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
1751 (byte_index & M_EDIT_BUF_SIZE));
1755 /* --------------------------------------------------------------------------------------------- */
1757 char *
1758 edit_get_buf_ptr (WEdit * edit, long byte_index)
1760 unsigned long p;
1762 if (byte_index >= (edit->curs1 + edit->curs2))
1763 byte_index--;
1765 if (byte_index < 0)
1766 return NULL;
1768 if (byte_index >= edit->curs1)
1770 p = edit->curs1 + edit->curs2 - 1;
1771 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1772 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1774 else
1776 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
1780 /* --------------------------------------------------------------------------------------------- */
1783 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1785 gchar *str = NULL;
1786 int res = -1;
1787 gunichar ch;
1788 gchar *next_ch = NULL;
1789 int width = 0;
1791 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1793 *char_width = 0;
1794 return '\n';
1797 str = edit_get_byte_ptr (edit, byte_index);
1799 if (str == NULL)
1801 *char_width = 0;
1802 return 0;
1805 res = g_utf8_get_char_validated (str, -1);
1807 if (res < 0)
1809 ch = *str;
1810 width = 0;
1812 else
1814 ch = res;
1815 /* Calculate UTF-8 char width */
1816 next_ch = g_utf8_next_char (str);
1817 if (next_ch)
1819 width = next_ch - str;
1821 else
1823 ch = 0;
1824 width = 0;
1827 *char_width = width;
1828 return ch;
1831 /* --------------------------------------------------------------------------------------------- */
1834 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
1836 gchar *str, *buf = NULL;
1837 int res = -1;
1838 gunichar ch;
1839 gchar *next_ch = NULL;
1840 int width = 0;
1842 if (byte_index > 0)
1843 byte_index--;
1845 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1847 *char_width = 0;
1848 return 0;
1851 ch = edit_get_utf (edit, byte_index, &width);
1853 if (width == 1)
1855 *char_width = width;
1856 return ch;
1859 str = edit_get_byte_ptr (edit, byte_index);
1860 buf = edit_get_buf_ptr (edit, byte_index);
1861 if (str == NULL || buf == NULL)
1863 *char_width = 0;
1864 return 0;
1866 /* get prev utf8 char */
1867 if (str != buf)
1868 str = g_utf8_find_prev_char (buf, str);
1870 if (str == NULL)
1872 *char_width = 0;
1873 return 0;
1875 else
1876 res = g_utf8_get_char_validated (str, -1);
1878 if (res < 0)
1880 ch = *str;
1881 width = 0;
1883 else
1885 ch = res;
1886 /* Calculate UTF-8 char width */
1887 next_ch = g_utf8_next_char (str);
1888 if (next_ch)
1890 width = next_ch - str;
1892 else
1894 ch = 0;
1895 width = 0;
1898 *char_width = width;
1899 return ch;
1902 /* --------------------------------------------------------------------------------------------- */
1904 char *
1905 edit_get_write_filter (const char *write_name, const char *filename)
1907 int i;
1908 char *p, *writename;
1910 i = edit_find_filter (filename);
1911 if (i < 0)
1912 return NULL;
1914 writename = name_quote (write_name, 0);
1915 p = g_strdup_printf (all_filters[i].write, writename);
1916 g_free (writename);
1917 return p;
1920 /* --------------------------------------------------------------------------------------------- */
1922 long
1923 edit_write_stream (WEdit * edit, FILE * f)
1925 long i;
1927 if (edit->lb == LB_ASIS)
1929 for (i = 0; i < edit->last_byte; i++)
1930 if (fputc (edit_get_byte (edit, i), f) < 0)
1931 break;
1932 return i;
1935 /* change line breaks */
1936 for (i = 0; i < edit->last_byte; i++)
1938 unsigned char c = edit_get_byte (edit, i);
1940 if (!(c == '\n' || c == '\r'))
1942 /* not line break */
1943 if (fputc (c, f) < 0)
1944 return i;
1946 else
1947 { /* (c == '\n' || c == '\r') */
1948 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1950 switch (edit->lb)
1952 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1953 /* put one line break unconditionally */
1954 if (fputc ('\n', f) < 0)
1955 return i;
1957 i++; /* 2 chars are processed */
1959 if (c == '\r' && c1 == '\n')
1960 /* Windows line break; go to the next char */
1961 break;
1963 if (c == '\r' && c1 == '\r')
1965 /* two Macintosh line breaks; put second line break */
1966 if (fputc ('\n', f) < 0)
1967 return i;
1968 break;
1971 if (fputc (c1, f) < 0)
1972 return i;
1973 break;
1975 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1976 /* put one line break unconditionally */
1977 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1978 return i;
1980 if (c == '\r' && c1 == '\n')
1981 /* Windows line break; go to the next char */
1982 i++;
1983 break;
1985 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1986 /* put one line break unconditionally */
1987 if (fputc ('\r', f) < 0)
1988 return i;
1990 i++; /* 2 chars are processed */
1992 if (c == '\r' && c1 == '\n')
1993 /* Windows line break; go to the next char */
1994 break;
1996 if (c == '\n' && c1 == '\n')
1998 /* two Windows line breaks; put second line break */
1999 if (fputc ('\r', f) < 0)
2000 return i;
2001 break;
2004 if (fputc (c1, f) < 0)
2005 return i;
2006 break;
2007 case LB_ASIS: /* default without changes */
2008 break;
2013 return edit->last_byte;
2016 /* --------------------------------------------------------------------------------------------- */
2017 /** inserts a file at the cursor, returns count of inserted bytes on success */
2018 long
2019 edit_insert_file (WEdit * edit, const char *filename)
2021 char *p;
2022 long ins_len = 0;
2024 p = edit_get_filter (filename);
2025 if (p != NULL)
2027 FILE *f;
2028 long current = edit->curs1;
2029 f = (FILE *) popen (p, "r");
2030 if (f != NULL)
2032 edit_insert_stream (edit, f);
2033 ins_len = edit->curs1 - current;
2034 edit_cursor_move (edit, current - edit->curs1);
2035 if (pclose (f) > 0)
2037 char *errmsg;
2038 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2039 edit_error_dialog (_("Error"), errmsg);
2040 g_free (errmsg);
2041 g_free (p);
2042 return 0;
2045 else
2047 char *errmsg;
2048 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2049 edit_error_dialog (_("Error"), errmsg);
2050 g_free (errmsg);
2051 g_free (p);
2052 return 0;
2054 g_free (p);
2056 else
2058 int i, file, blocklen;
2059 long current = edit->curs1;
2060 int vertical_insertion = 0;
2061 char *buf;
2062 file = mc_open (filename, O_RDONLY | O_BINARY);
2063 if (file == -1)
2064 return 0;
2065 buf = g_malloc0 (TEMP_BUF_LEN);
2066 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2067 if (blocklen > 0)
2069 /* if contain signature VERTICAL_MAGIC then it vertical block */
2070 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2071 vertical_insertion = 1;
2072 else
2073 mc_lseek (file, 0, SEEK_SET);
2075 if (vertical_insertion)
2077 blocklen = edit_insert_column_of_text_from_file (edit, file);
2079 else
2081 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2083 for (i = 0; i < blocklen; i++)
2084 edit_insert (edit, buf[i]);
2087 ins_len = edit->curs1 - current;
2088 edit_cursor_move (edit, current - edit->curs1);
2089 g_free (buf);
2090 mc_close (file);
2091 if (blocklen != 0)
2092 return 0;
2094 return ins_len;
2097 /* --------------------------------------------------------------------------------------------- */
2099 * Fill in the edit structure. Return NULL on failure. Pass edit as
2100 * NULL to allocate a new structure.
2102 * If line is 0, try to restore saved position. Otherwise put the
2103 * cursor on that line and show it in the middle of the screen.
2106 WEdit *
2107 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
2109 int to_free = 0;
2110 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2111 if (option_line_state)
2113 option_line_state_width = LINE_STATE_WIDTH;
2115 else
2117 option_line_state_width = 0;
2119 if (!edit)
2121 #ifdef ENABLE_NLS
2123 * Expand option_whole_chars_search by national letters using
2124 * current locale
2127 static char option_whole_chars_search_buf[256];
2129 if (option_whole_chars_search_buf != option_whole_chars_search)
2131 size_t i;
2132 size_t len = str_term_width1 (option_whole_chars_search);
2134 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2136 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2138 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2140 option_whole_chars_search_buf[len++] = i;
2144 option_whole_chars_search_buf[len] = 0;
2145 option_whole_chars_search = option_whole_chars_search_buf;
2147 #endif /* ENABLE_NLS */
2148 edit = g_malloc0 (sizeof (WEdit));
2149 edit->search = NULL;
2150 to_free = 1;
2152 edit_purge_widget (edit);
2153 edit->num_widget_lines = lines;
2154 edit->over_col = 0;
2155 edit->num_widget_columns = columns;
2156 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2157 edit->stat1.st_uid = getuid ();
2158 edit->stat1.st_gid = getgid ();
2159 edit->stat1.st_mtime = 0;
2160 edit->bracket = -1;
2161 edit->force |= REDRAW_PAGE;
2162 edit_set_filename (edit, filename);
2163 edit->undo_stack_size = START_STACK_SIZE;
2164 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2165 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2167 edit->redo_stack_size = START_STACK_SIZE;
2168 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2169 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2171 edit->utf8 = 0;
2172 edit->converter = str_cnv_from_term;
2173 edit_set_codeset (edit);
2175 if (edit_load_file (edit))
2177 /* edit_load_file already gives an error message */
2178 if (to_free)
2179 g_free (edit);
2180 return 0;
2183 edit->loading_done = 1;
2184 edit->modified = 0;
2185 edit->locked = 0;
2186 edit_load_syntax (edit, NULL, NULL);
2188 int color;
2189 edit_get_syntax_color (edit, -1, &color);
2192 /* load saved cursor position */
2193 if ((line == 0) && option_save_position)
2195 edit_load_position (edit);
2197 else
2199 if (line <= 0)
2200 line = 1;
2201 edit_move_display (edit, line - 1);
2202 edit_move_to_line (edit, line - 1);
2205 edit_set_keymap ();
2206 edit_load_macro_cmd (edit);
2207 return edit;
2210 /* --------------------------------------------------------------------------------------------- */
2211 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2214 edit_clean (WEdit * edit)
2216 int j = 0;
2218 if (!edit)
2219 return 0;
2221 /* a stale lock, remove it */
2222 if (edit->locked)
2223 edit->locked = edit_unlock_file (edit);
2225 /* save cursor position */
2226 if (option_save_position)
2227 edit_save_position (edit);
2228 else if (edit->serialized_bookmarks != NULL)
2229 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2231 /* File specified on the mcedit command line and never saved */
2232 if (edit->delete_file)
2233 unlink (edit->filename);
2235 edit_free_syntax_rules (edit);
2236 book_mark_flush (edit, -1);
2237 for (; j <= MAXBUFF; j++)
2239 g_free (edit->buffers1[j]);
2240 g_free (edit->buffers2[j]);
2243 g_free (edit->undo_stack);
2244 g_free (edit->redo_stack);
2245 g_free (edit->filename);
2246 g_free (edit->dir);
2247 mc_search_free (edit->search);
2248 edit->search = NULL;
2250 if (edit->converter != str_cnv_from_term)
2251 str_close_conv (edit->converter);
2253 edit_purge_widget (edit);
2255 return 1;
2258 /* --------------------------------------------------------------------------------------------- */
2259 /** returns 1 on success */
2262 edit_renew (WEdit * edit)
2264 int lines = edit->num_widget_lines;
2265 int columns = edit->num_widget_columns;
2267 edit_clean (edit);
2268 return (edit_init (edit, lines, columns, "", 0) != NULL);
2271 /* --------------------------------------------------------------------------------------------- */
2273 * Load a new file into the editor. If it fails, preserve the old file.
2274 * To do it, allocate a new widget, initialize it and, if the new file
2275 * was loaded, copy the data to the old widget.
2276 * Return 1 on success, 0 on failure.
2280 edit_reload (WEdit * edit, const char *filename)
2282 WEdit *e;
2283 int lines = edit->num_widget_lines;
2284 int columns = edit->num_widget_columns;
2286 e = g_malloc0 (sizeof (WEdit));
2287 e->widget = edit->widget;
2288 if (!edit_init (e, lines, columns, filename, 0))
2290 g_free (e);
2291 return 0;
2293 edit_clean (edit);
2294 memcpy (edit, e, sizeof (WEdit));
2295 g_free (e);
2296 return 1;
2299 /* --------------------------------------------------------------------------------------------- */
2301 * Load a new file into the editor and set line. If it fails, preserve the old file.
2302 * To do it, allocate a new widget, initialize it and, if the new file
2303 * was loaded, copy the data to the old widget.
2304 * Return 1 on success, 0 on failure.
2308 edit_reload_line (WEdit * edit, const char *filename, long line)
2310 WEdit *e;
2311 int lines = edit->num_widget_lines;
2312 int columns = edit->num_widget_columns;
2314 e = g_malloc0 (sizeof (WEdit));
2315 e->widget = edit->widget;
2316 if (!edit_init (e, lines, columns, filename, line))
2318 g_free (e);
2319 return 0;
2321 edit_clean (edit);
2322 memcpy (edit, e, sizeof (WEdit));
2323 g_free (e);
2324 return 1;
2327 /* --------------------------------------------------------------------------------------------- */
2329 void
2330 edit_set_codeset (WEdit * edit)
2332 #ifdef HAVE_CHARSET
2333 const char *cp_id;
2335 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
2337 if (cp_id != NULL)
2339 GIConv conv;
2340 conv = str_crt_conv_from (cp_id);
2341 if (conv != INVALID_CONV)
2343 if (edit->converter != str_cnv_from_term)
2344 str_close_conv (edit->converter);
2345 edit->converter = conv;
2349 if (cp_id != NULL)
2350 edit->utf8 = str_isutf8 (cp_id);
2351 #else
2352 (void) edit;
2353 #endif
2357 /* --------------------------------------------------------------------------------------------- */
2359 Recording stack for undo:
2360 The following is an implementation of a compressed stack. Identical
2361 pushes are recorded by a negative prefix indicating the number of times the
2362 same char was pushed. This saves space for repeated curs-left or curs-right
2363 delete etc.
2367 pushed: stored:
2371 b -3
2373 c --> -4
2379 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2380 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2381 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2382 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2383 position.
2385 The only way the cursor moves or the buffer is changed is through the routines:
2386 insert, backspace, insert_ahead, delete, and cursor_move.
2387 These record the reverse undo movements onto the stack each time they are
2388 called.
2390 Each key press results in a set of actions (insert; delete ...). So each time
2391 a key is pressed the current position of start_display is pushed as
2392 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2393 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2394 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2398 void
2399 edit_push_undo_action (WEdit * edit, long c, ...)
2401 unsigned long sp = edit->undo_stack_pointer;
2402 unsigned long spm1;
2403 long *t;
2405 /* first enlarge the stack if necessary */
2406 if (sp > edit->undo_stack_size - 10)
2407 { /* say */
2408 if (option_max_undo < 256)
2409 option_max_undo = 256;
2410 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2412 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2413 if (t)
2415 edit->undo_stack = t;
2416 edit->undo_stack_size <<= 1;
2417 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2421 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2422 if (edit->undo_stack_disable)
2424 edit_push_redo_action (edit, KEY_PRESS);
2425 edit_push_redo_action (edit, c);
2426 return;
2428 else if (edit->redo_stack_reset)
2430 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2433 if (edit->undo_stack_bottom != sp
2434 && spm1 != edit->undo_stack_bottom
2435 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2437 int d;
2438 if (edit->undo_stack[spm1] < 0)
2440 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2441 if (d == c)
2443 if (edit->undo_stack[spm1] > -1000000000)
2445 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2447 edit->undo_stack[spm1]--;
2449 return;
2453 else
2455 d = edit->undo_stack[spm1];
2456 if (d == c)
2458 if (c >= KEY_PRESS)
2459 return; /* --> no need to push multiple do-nothings */
2460 edit->undo_stack[sp] = -2;
2461 goto check_bottom;
2465 edit->undo_stack[sp] = c;
2467 check_bottom:
2468 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2470 /* if the sp wraps round and catches the undo_stack_bottom then erase
2471 * the first set of actions on the stack to make space - by moving
2472 * undo_stack_bottom forward one "key press" */
2473 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2474 if ((unsigned long) c == edit->undo_stack_bottom ||
2475 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2478 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2480 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2481 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2483 /*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: */
2484 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2485 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2487 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2491 void
2492 edit_push_redo_action (WEdit * edit, long c, ...)
2494 unsigned long sp = edit->redo_stack_pointer;
2495 unsigned long spm1;
2496 long *t;
2497 /* first enlarge the stack if necessary */
2498 if (sp > edit->redo_stack_size - 10)
2499 { /* say */
2500 if (option_max_undo < 256)
2501 option_max_undo = 256;
2502 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2504 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2505 if (t)
2507 edit->redo_stack = t;
2508 edit->redo_stack_size <<= 1;
2509 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2513 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2515 if (edit->redo_stack_bottom != sp
2516 && spm1 != edit->redo_stack_bottom
2517 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2519 int d;
2520 if (edit->redo_stack[spm1] < 0)
2522 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2523 if (d == c)
2525 if (edit->redo_stack[spm1] > -1000000000)
2527 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2528 edit->redo_stack[spm1]--;
2529 return;
2533 else
2535 d = edit->redo_stack[spm1];
2536 if (d == c)
2538 if (c >= KEY_PRESS)
2539 return; /* --> no need to push multiple do-nothings */
2540 edit->redo_stack[sp] = -2;
2541 goto redo_check_bottom;
2545 edit->redo_stack[sp] = c;
2547 redo_check_bottom:
2548 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2550 /* if the sp wraps round and catches the redo_stack_bottom then erase
2551 * the first set of actions on the stack to make space - by moving
2552 * redo_stack_bottom forward one "key press" */
2553 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2554 if ((unsigned long) c == edit->redo_stack_bottom ||
2555 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2558 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2560 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2561 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2564 * If a single key produced enough pushes to wrap all the way round then
2565 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2566 * The stack is then initialised:
2569 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2570 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2571 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2575 /* --------------------------------------------------------------------------------------------- */
2577 Basic low level single character buffer alterations and movements at the cursor.
2578 Returns char passed over, inserted or removed.
2581 void
2582 edit_insert (WEdit * edit, int c)
2584 /* check if file has grown to large */
2585 if (edit->last_byte >= SIZE_LIMIT)
2586 return;
2588 /* first we must update the position of the display window */
2589 if (edit->curs1 < edit->start_display)
2591 edit->start_display++;
2592 if (c == '\n')
2593 edit->start_line++;
2596 /* Mark file as modified, unless the file hasn't been fully loaded */
2597 if (edit->loading_done)
2599 edit_modification (edit);
2602 /* now we must update some info on the file and check if a redraw is required */
2603 if (c == '\n')
2605 if (edit->book_mark)
2606 book_mark_inc (edit, edit->curs_line);
2607 edit->curs_line++;
2608 edit->total_lines++;
2609 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2612 /* save the reverse command onto the undo stack */
2613 /* ordinary char and not space */
2614 if (c > 32)
2615 edit_push_undo_action (edit, BACKSPACE);
2616 else
2617 edit_push_undo_action (edit, BACKSPACE_BR);
2618 /* update markers */
2619 edit->mark1 += (edit->mark1 > edit->curs1);
2620 edit->mark2 += (edit->mark2 > edit->curs1);
2621 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2623 /* add a new buffer if we've reached the end of the last one */
2624 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2625 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2627 /* perform the insertion */
2628 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2629 = (unsigned char) c;
2631 /* update file length */
2632 edit->last_byte++;
2634 /* update cursor position */
2635 edit->curs1++;
2638 /* --------------------------------------------------------------------------------------------- */
2639 /** same as edit_insert and move left */
2641 void
2642 edit_insert_ahead (WEdit * edit, int c)
2644 if (edit->last_byte >= SIZE_LIMIT)
2645 return;
2647 if (edit->curs1 < edit->start_display)
2649 edit->start_display++;
2650 if (c == '\n')
2651 edit->start_line++;
2653 edit_modification (edit);
2654 if (c == '\n')
2656 if (edit->book_mark)
2657 book_mark_inc (edit, edit->curs_line);
2658 edit->total_lines++;
2659 edit->force |= REDRAW_AFTER_CURSOR;
2661 /* ordinary char and not space */
2662 if (c > 32)
2663 edit_push_undo_action (edit, DELCHAR);
2664 else
2665 edit_push_undo_action (edit, DELCHAR_BR);
2667 edit->mark1 += (edit->mark1 >= edit->curs1);
2668 edit->mark2 += (edit->mark2 >= edit->curs1);
2669 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2671 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2672 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2673 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2674 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2676 edit->last_byte++;
2677 edit->curs2++;
2681 /* --------------------------------------------------------------------------------------------- */
2684 edit_delete (WEdit * edit, const int byte_delete)
2686 int p = 0;
2687 int cw = 1;
2688 int i;
2690 if (!edit->curs2)
2691 return 0;
2693 cw = 1;
2694 /* if byte_delete = 1 then delete only one byte not multibyte char */
2695 if (edit->utf8 && byte_delete == 0)
2697 edit_get_utf (edit, edit->curs1, &cw);
2698 if (cw < 1)
2699 cw = 1;
2702 if (edit->mark2 != edit->mark1)
2703 edit_push_markers (edit);
2705 for (i = 1; i <= cw; i++)
2707 if (edit->mark1 > edit->curs1)
2709 edit->mark1--;
2710 edit->end_mark_curs--;
2712 if (edit->mark2 > edit->curs1)
2713 edit->mark2--;
2714 if (edit->last_get_rule > edit->curs1)
2715 edit->last_get_rule--;
2717 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2718 ((edit->curs2 -
2719 1) & M_EDIT_BUF_SIZE) - 1];
2721 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2723 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2724 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2726 edit->last_byte--;
2727 edit->curs2--;
2728 edit_push_undo_action (edit, p + 256);
2731 edit_modification (edit);
2732 if (p == '\n')
2734 if (edit->book_mark)
2735 book_mark_dec (edit, edit->curs_line);
2736 edit->total_lines--;
2737 edit->force |= REDRAW_AFTER_CURSOR;
2739 if (edit->curs1 < edit->start_display)
2741 edit->start_display--;
2742 if (p == '\n')
2743 edit->start_line--;
2746 return p;
2749 /* --------------------------------------------------------------------------------------------- */
2750 /** moves the cursor right or left: increment positive or negative respectively */
2752 void
2753 edit_cursor_move (WEdit * edit, long increment)
2755 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2756 int c;
2758 if (increment < 0)
2760 for (; increment < 0; increment++)
2762 if (!edit->curs1)
2763 return;
2765 edit_push_undo_action (edit, CURS_RIGHT);
2767 c = edit_get_byte (edit, edit->curs1 - 1);
2768 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2769 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2770 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2771 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2772 edit->curs2++;
2773 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2774 1) & M_EDIT_BUF_SIZE];
2775 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2777 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2778 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2780 edit->curs1--;
2781 if (c == '\n')
2783 edit->curs_line--;
2784 edit->force |= REDRAW_LINE_BELOW;
2789 else if (increment > 0)
2791 for (; increment > 0; increment--)
2793 if (!edit->curs2)
2794 return;
2796 edit_push_undo_action (edit, CURS_LEFT);
2798 c = edit_get_byte (edit, edit->curs1);
2799 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2800 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2801 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2802 edit->curs1++;
2803 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2804 ((edit->curs2 -
2805 1) & M_EDIT_BUF_SIZE) - 1];
2806 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2808 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2809 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2811 edit->curs2--;
2812 if (c == '\n')
2814 edit->curs_line++;
2815 edit->force |= REDRAW_LINE_ABOVE;
2821 /* These functions return positions relative to lines */
2823 /* --------------------------------------------------------------------------------------------- */
2824 /** returns index of last char on line + 1 */
2826 long
2827 edit_eol (WEdit * edit, long current)
2829 if (current >= edit->last_byte)
2830 return edit->last_byte;
2832 for (;; current++)
2833 if (edit_get_byte (edit, current) == '\n')
2834 break;
2835 return current;
2838 /* --------------------------------------------------------------------------------------------- */
2839 /** returns index of first char on line */
2841 long
2842 edit_bol (WEdit * edit, long current)
2844 if (current <= 0)
2845 return 0;
2847 for (;; current--)
2848 if (edit_get_byte (edit, current - 1) == '\n')
2849 break;
2850 return current;
2853 /* --------------------------------------------------------------------------------------------- */
2855 long
2856 edit_count_lines (WEdit * edit, long current, long upto)
2858 long lines = 0;
2859 if (upto > edit->last_byte)
2860 upto = edit->last_byte;
2861 if (current < 0)
2862 current = 0;
2863 while (current < upto)
2864 if (edit_get_byte (edit, current++) == '\n')
2865 lines++;
2866 return lines;
2869 /* --------------------------------------------------------------------------------------------- */
2870 /* If lines is zero this returns the count of lines from current to upto. */
2871 /* If upto is zero returns index of lines forward current. */
2873 long
2874 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2876 if (upto)
2878 return edit_count_lines (edit, current, upto);
2880 else
2882 long next;
2883 if (lines < 0)
2884 lines = 0;
2885 while (lines--)
2887 next = edit_eol (edit, current) + 1;
2888 if (next > edit->last_byte)
2889 break;
2890 else
2891 current = next;
2893 return current;
2897 /* --------------------------------------------------------------------------------------------- */
2898 /** Returns offset of 'lines' lines up from current */
2900 long
2901 edit_move_backward (WEdit * edit, long current, long lines)
2903 if (lines < 0)
2904 lines = 0;
2905 current = edit_bol (edit, current);
2906 while ((lines--) && current != 0)
2907 current = edit_bol (edit, current - 1);
2908 return current;
2911 /* --------------------------------------------------------------------------------------------- */
2912 /* If cols is zero this returns the count of columns from current to upto. */
2913 /* If upto is zero returns index of cols across from current. */
2915 long
2916 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
2918 long p, q;
2919 int col;
2921 if (upto)
2923 q = upto;
2924 cols = -10;
2926 else
2927 q = edit->last_byte + 2;
2929 for (col = 0, p = current; p < q; p++)
2931 int c, orig_c;
2933 if (cols != -10)
2935 if (col == cols)
2936 return p;
2937 if (col > cols)
2938 return p - 1;
2941 orig_c = c = edit_get_byte (edit, p);
2943 #ifdef HAVE_CHARSET
2944 if (edit->utf8)
2946 int utf_ch;
2947 int cw = 1;
2949 utf_ch = edit_get_utf (edit, p, &cw);
2950 if (utf8_display)
2952 if (cw > 1)
2953 col -= cw - 1;
2954 if (g_unichar_iswide (utf_ch))
2955 col++;
2957 else if (cw > 1 && g_unichar_isprint (utf_ch))
2958 col -= cw - 1;
2961 c = convert_to_display_c (c);
2962 #endif
2964 if (c == '\t')
2965 col += TAB_SIZE - col % TAB_SIZE;
2966 else if (c == '\n')
2968 if (upto)
2969 return col;
2970 else
2971 return p;
2973 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
2974 /* '\r' is shown as ^M, so we must advance 2 characters */
2975 /* Caret notation for control characters */
2976 col += 2;
2977 else
2978 col++;
2980 return col;
2983 /* --------------------------------------------------------------------------------------------- */
2984 /** returns the current column position of the cursor */
2987 edit_get_col (WEdit * edit)
2989 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
2992 /* --------------------------------------------------------------------------------------------- */
2993 /* Scrolling functions */
2994 /* --------------------------------------------------------------------------------------------- */
2996 void
2997 edit_update_curs_row (WEdit * edit)
2999 edit->curs_row = edit->curs_line - edit->start_line;
3002 /* --------------------------------------------------------------------------------------------- */
3004 void
3005 edit_update_curs_col (WEdit * edit)
3007 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3010 /* --------------------------------------------------------------------------------------------- */
3013 edit_get_curs_col (const WEdit * edit)
3015 return edit->curs_col;
3018 /* --------------------------------------------------------------------------------------------- */
3019 /** moves the display start position up by i lines */
3021 void
3022 edit_scroll_upward (WEdit * edit, unsigned long i)
3024 unsigned long lines_above = edit->start_line;
3025 if (i > lines_above)
3026 i = lines_above;
3027 if (i)
3029 edit->start_line -= i;
3030 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3031 edit->force |= REDRAW_PAGE;
3032 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3034 edit_update_curs_row (edit);
3038 /* --------------------------------------------------------------------------------------------- */
3039 /** returns 1 if could scroll, 0 otherwise */
3041 void
3042 edit_scroll_downward (WEdit * edit, int i)
3044 int lines_below;
3045 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
3046 if (lines_below > 0)
3048 if (i > lines_below)
3049 i = lines_below;
3050 edit->start_line += i;
3051 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3052 edit->force |= REDRAW_PAGE;
3053 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3055 edit_update_curs_row (edit);
3058 /* --------------------------------------------------------------------------------------------- */
3060 void
3061 edit_scroll_right (WEdit * edit, int i)
3063 edit->force |= REDRAW_PAGE;
3064 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3065 edit->start_col -= i;
3068 /* --------------------------------------------------------------------------------------------- */
3070 void
3071 edit_scroll_left (WEdit * edit, int i)
3073 if (edit->start_col)
3075 edit->start_col += i;
3076 if (edit->start_col > 0)
3077 edit->start_col = 0;
3078 edit->force |= REDRAW_PAGE;
3079 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3083 /* --------------------------------------------------------------------------------------------- */
3084 /* high level cursor movement commands */
3085 /* --------------------------------------------------------------------------------------------- */
3087 void
3088 edit_move_to_prev_col (WEdit * edit, long p)
3090 int prev = edit->prev_col;
3091 int over = edit->over_col;
3092 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3094 if (option_cursor_beyond_eol)
3096 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3097 edit_eol (edit, edit->curs1));
3099 if (line_len < prev + edit->over_col)
3101 edit->over_col = prev + over - line_len;
3102 edit->prev_col = line_len;
3103 edit->curs_col = line_len;
3105 else
3107 edit->curs_col = prev + over;
3108 edit->prev_col = edit->curs_col;
3109 edit->over_col = 0;
3112 else
3114 edit->over_col = 0;
3115 if (is_in_indent (edit) && option_fake_half_tabs)
3117 edit_update_curs_col (edit);
3118 if (space_width)
3119 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3121 int q = edit->curs_col;
3122 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3123 p = edit_bol (edit, edit->curs1);
3124 edit_cursor_move (edit,
3125 edit_move_forward3 (edit, p, edit->curs_col,
3126 0) - edit->curs1);
3127 if (!left_of_four_spaces (edit))
3128 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3134 /* --------------------------------------------------------------------------------------------- */
3137 line_is_blank (WEdit * edit, long line)
3139 return is_blank (edit, edit_find_line (edit, line));
3142 /* --------------------------------------------------------------------------------------------- */
3143 /** move cursor to line 'line' */
3145 void
3146 edit_move_to_line (WEdit * e, long line)
3148 if (line < e->curs_line)
3149 edit_move_up (e, e->curs_line - line, 0);
3150 else
3151 edit_move_down (e, line - e->curs_line, 0);
3152 edit_scroll_screen_over_cursor (e);
3155 /* --------------------------------------------------------------------------------------------- */
3156 /** scroll window so that first visible line is 'line' */
3158 void
3159 edit_move_display (WEdit * e, long line)
3161 if (line < e->start_line)
3162 edit_scroll_upward (e, e->start_line - line);
3163 else
3164 edit_scroll_downward (e, line - e->start_line);
3167 /* --------------------------------------------------------------------------------------------- */
3168 /** save markers onto undo stack */
3170 void
3171 edit_push_markers (WEdit * edit)
3173 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3174 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3175 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3178 /* --------------------------------------------------------------------------------------------- */
3180 void
3181 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3183 edit->mark1 = m1;
3184 edit->mark2 = m2;
3185 edit->column1 = c1;
3186 edit->column2 = c2;
3190 /* --------------------------------------------------------------------------------------------- */
3191 /** highlight marker toggle */
3193 void
3194 edit_mark_cmd (WEdit * edit, int unmark)
3196 edit_push_markers (edit);
3197 if (unmark)
3199 edit_set_markers (edit, 0, 0, 0, 0);
3200 edit->force |= REDRAW_PAGE;
3202 else
3204 if (edit->mark2 >= 0)
3206 edit->end_mark_curs = -1;
3207 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3208 edit->curs_col + edit->over_col);
3209 edit->force |= REDRAW_PAGE;
3211 else
3213 edit->end_mark_curs = edit->curs1;
3214 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3215 edit->curs_col + edit->over_col);
3220 /* --------------------------------------------------------------------------------------------- */
3221 /** highlight the word under cursor */
3223 void
3224 edit_mark_current_word_cmd (WEdit * edit)
3226 long pos;
3228 for (pos = edit->curs1; pos != 0; pos--)
3230 int c1, c2;
3232 c1 = edit_get_byte (edit, pos);
3233 c2 = edit_get_byte (edit, pos - 1);
3234 if (!isspace (c1) && isspace (c2))
3235 break;
3236 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3237 break;
3239 edit->mark1 = pos;
3241 for (; pos < edit->last_byte; pos++)
3243 int c1, c2;
3245 c1 = edit_get_byte (edit, pos);
3246 c2 = edit_get_byte (edit, pos + 1);
3247 if (!isspace (c1) && isspace (c2))
3248 break;
3249 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3250 break;
3252 edit->mark2 = min (pos + 1, edit->last_byte);
3254 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3257 /* --------------------------------------------------------------------------------------------- */
3259 void
3260 edit_mark_current_line_cmd (WEdit * edit)
3262 long pos = edit->curs1;
3264 edit->mark1 = edit_bol (edit, pos);
3265 edit->mark2 = edit_eol (edit, pos);
3267 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3270 /* --------------------------------------------------------------------------------------------- */
3272 void
3273 edit_delete_line (WEdit * edit)
3276 * Delete right part of the line.
3277 * Note that edit_get_byte() returns '\n' when byte position is
3278 * beyond EOF.
3280 while (edit_get_byte (edit, edit->curs1) != '\n')
3282 (void) edit_delete (edit, 1);
3286 * Delete '\n' char.
3287 * Note that edit_delete() will not corrupt anything if called while
3288 * cursor position is EOF.
3290 (void) edit_delete (edit, 1);
3293 * Delete left part of the line.
3294 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3296 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3298 (void) edit_backspace (edit, 1);
3302 /* --------------------------------------------------------------------------------------------- */
3304 void
3305 insert_spaces_tab (WEdit * edit, int half)
3307 int i;
3308 edit_update_curs_col (edit);
3309 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
3310 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
3311 while (i > 0)
3313 edit_insert (edit, ' ');
3314 i -= space_width;
3318 /* --------------------------------------------------------------------------------------------- */
3321 edit_indent_width (WEdit * edit, long p)
3323 long q = p;
3324 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3325 q++;
3326 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3329 /* --------------------------------------------------------------------------------------------- */
3331 void
3332 edit_insert_indent (WEdit * edit, int indent)
3334 if (!option_fill_tabs_with_spaces)
3336 while (indent >= TAB_SIZE)
3338 edit_insert (edit, '\t');
3339 indent -= TAB_SIZE;
3342 while (indent-- > 0)
3343 edit_insert (edit, ' ');
3346 /* --------------------------------------------------------------------------------------------- */
3348 void
3349 edit_push_key_press (WEdit * edit)
3351 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3352 if (edit->mark2 == -1)
3354 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3355 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3359 /* --------------------------------------------------------------------------------------------- */
3361 void
3362 edit_find_bracket (WEdit * edit)
3364 edit->bracket = edit_get_bracket (edit, 1, 10000);
3365 if (last_bracket != edit->bracket)
3366 edit->force |= REDRAW_PAGE;
3367 last_bracket = edit->bracket;
3370 /* --------------------------------------------------------------------------------------------- */
3372 * This executes a command as though the user initiated it through a key
3373 * press. Callback with WIDGET_KEY as a message calls this after
3374 * translating the key press. This function can be used to pass any
3375 * command to the editor. Note that the screen wouldn't update
3376 * automatically. Either of command or char_for_insertion must be
3377 * passed as -1. Commands are executed, and char_for_insertion is
3378 * inserted at the cursor.
3381 void
3382 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3384 if (command == CK_Begin_Record_Macro || command == CK_Begin_Record_Repeat
3385 || (macro_index < 0 && (command == CK_Begin_End_Macro || command == CK_Begin_End_Repeat)))
3387 macro_index = 0;
3388 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3389 return;
3391 if (macro_index != -1)
3393 edit->force |= REDRAW_COMPLETELY;
3394 if (command == CK_End_Record_Macro || command == CK_Begin_End_Macro)
3396 edit_store_macro_cmd (edit);
3397 macro_index = -1;
3398 return;
3400 else if (command == CK_End_Record_Repeat || command == CK_Begin_End_Repeat)
3402 edit_repeat_macro_cmd (edit);
3403 macro_index = -1;
3404 return;
3409 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3411 record_macro_buf[macro_index].action = command;
3412 record_macro_buf[macro_index++].ch = char_for_insertion;
3414 /* record the beginning of a set of editing actions initiated by a key press */
3415 if (command != CK_Undo && command != CK_Ext_Mode)
3416 edit_push_key_press (edit);
3418 edit_execute_cmd (edit, command, char_for_insertion);
3419 if (edit->column_highlight)
3420 edit->force |= REDRAW_PAGE;
3423 /* --------------------------------------------------------------------------------------------- */
3425 This executes a command at a lower level than macro recording.
3426 It also does not push a key_press onto the undo stack. This means
3427 that if it is called many times, a single undo command will undo
3428 all of them. It also does not check for the Undo command.
3430 void
3431 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3433 edit->force |= REDRAW_LINE;
3435 /* The next key press will unhighlight the found string, so update
3436 * the whole page */
3437 if (edit->found_len || edit->column_highlight)
3438 edit->force |= REDRAW_PAGE;
3440 if (command / 100 == 6)
3441 { /* a highlight command like shift-arrow */
3442 edit->column_highlight = 0;
3443 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3445 edit_mark_cmd (edit, 1); /* clear */
3446 edit_mark_cmd (edit, 0); /* marking on */
3448 edit->highlight = 1;
3450 else
3451 { /* any other command */
3452 if (edit->highlight)
3453 edit_mark_cmd (edit, 0); /* clear */
3454 edit->highlight = 0;
3457 /* first check for undo */
3458 if (command == CK_Undo)
3460 edit->redo_stack_reset = 0;
3461 edit_group_undo (edit);
3462 edit->found_len = 0;
3463 edit->prev_col = edit_get_col (edit);
3464 edit->search_start = edit->curs1;
3465 return;
3467 /* check for redo */
3468 if (command == CK_Redo)
3470 edit->redo_stack_reset = 0;
3471 edit_do_redo (edit);
3472 edit->found_len = 0;
3473 edit->prev_col = edit_get_col (edit);
3474 edit->search_start = edit->curs1;
3475 return;
3478 edit->redo_stack_reset = 1;
3480 /* An ordinary key press */
3481 if (char_for_insertion >= 0)
3483 /* if non persistent selection and text selected */
3484 if (!option_persistent_selections)
3486 if (edit->mark1 != edit->mark2)
3487 edit_block_delete_cmd (edit);
3489 if (edit->overwrite)
3491 /* remove char only one time, after input first byte, multibyte chars */
3492 if ((!utf8_display || edit->charpoint == 0)
3493 && edit_get_byte (edit, edit->curs1) != '\n')
3494 edit_delete (edit, 0);
3496 if (option_cursor_beyond_eol && edit->over_col > 0)
3497 edit_insert_over (edit);
3498 #ifdef HAVE_CHARSET
3499 if (char_for_insertion > 255 && utf8_display == 0)
3501 unsigned char str[6 + 1];
3502 size_t i = 0;
3503 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3504 if (res == 0)
3506 str[0] = '.';
3507 str[1] = '\0';
3509 else
3511 str[res] = '\0';
3513 while (str[i] != 0 && i <= 6)
3515 char_for_insertion = str[i];
3516 edit_insert (edit, char_for_insertion);
3517 i++;
3520 else
3521 #endif
3522 edit_insert (edit, char_for_insertion);
3524 if (option_auto_para_formatting)
3526 format_paragraph (edit, 0);
3527 edit->force |= REDRAW_PAGE;
3529 else
3530 check_and_wrap_line (edit);
3531 edit->found_len = 0;
3532 edit->prev_col = edit_get_col (edit);
3533 edit->search_start = edit->curs1;
3534 edit_find_bracket (edit);
3535 return;
3538 switch (command)
3540 case CK_Begin_Page:
3541 case CK_End_Page:
3542 case CK_Begin_Page_Highlight:
3543 case CK_End_Page_Highlight:
3544 case CK_Word_Left:
3545 case CK_Word_Right:
3546 case CK_Up:
3547 case CK_Down:
3548 case CK_Left:
3549 case CK_Right:
3550 if (edit->mark2 >= 0)
3552 if (!option_persistent_selections)
3554 if (edit->column_highlight)
3555 edit_push_undo_action (edit, COLUMN_ON);
3556 edit->column_highlight = 0;
3557 edit_mark_cmd (edit, 1);
3562 switch (command)
3564 case CK_Begin_Page:
3565 case CK_End_Page:
3566 case CK_Begin_Page_Highlight:
3567 case CK_End_Page_Highlight:
3568 case CK_Word_Left:
3569 case CK_Word_Right:
3570 case CK_Up:
3571 case CK_Down:
3572 case CK_Word_Left_Highlight:
3573 case CK_Word_Right_Highlight:
3574 case CK_Up_Highlight:
3575 case CK_Down_Highlight:
3576 case CK_Up_Alt_Highlight:
3577 case CK_Down_Alt_Highlight:
3578 if (edit->mark2 == -1)
3579 break; /*marking is following the cursor: may need to highlight a whole line */
3580 case CK_Left:
3581 case CK_Right:
3582 case CK_Left_Highlight:
3583 case CK_Right_Highlight:
3584 edit->force |= REDRAW_CHAR_ONLY;
3587 /* basic cursor key commands */
3588 switch (command)
3590 case CK_BackSpace:
3591 /* if non persistent selection and text selected */
3592 if (!option_persistent_selections)
3594 if (edit->mark1 != edit->mark2)
3596 edit_block_delete_cmd (edit);
3597 break;
3600 if (option_cursor_beyond_eol && edit->over_col > 0)
3602 edit->over_col--;
3603 break;
3605 if (option_backspace_through_tabs && is_in_indent (edit))
3607 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3608 edit_backspace (edit, 1);
3609 break;
3611 else
3613 if (option_fake_half_tabs)
3615 int i;
3616 if (is_in_indent (edit) && right_of_four_spaces (edit))
3618 for (i = 0; i < HALF_TAB_SIZE; i++)
3619 edit_backspace (edit, 1);
3620 break;
3624 edit_backspace (edit, 0);
3625 break;
3626 case CK_Delete:
3627 /* if non persistent selection and text selected */
3628 if (!option_persistent_selections)
3630 if (edit->mark1 != edit->mark2)
3632 edit_block_delete_cmd (edit);
3633 break;
3637 if (option_cursor_beyond_eol && edit->over_col > 0)
3638 edit_insert_over (edit);
3640 if (option_fake_half_tabs)
3642 int i;
3643 if (is_in_indent (edit) && left_of_four_spaces (edit))
3645 for (i = 1; i <= HALF_TAB_SIZE; i++)
3646 edit_delete (edit, 1);
3647 break;
3650 edit_delete (edit, 0);
3651 break;
3652 case CK_Delete_Word_Left:
3653 edit->over_col = 0;
3654 edit_left_delete_word (edit);
3655 break;
3656 case CK_Delete_Word_Right:
3657 if (option_cursor_beyond_eol && edit->over_col > 0)
3658 edit_insert_over (edit);
3660 edit_right_delete_word (edit);
3661 break;
3662 case CK_Delete_Line:
3663 edit_delete_line (edit);
3664 break;
3665 case CK_Delete_To_Line_End:
3666 edit_delete_to_line_end (edit);
3667 break;
3668 case CK_Delete_To_Line_Begin:
3669 edit_delete_to_line_begin (edit);
3670 break;
3671 case CK_Enter:
3672 edit->over_col = 0;
3673 if (option_auto_para_formatting)
3675 edit_double_newline (edit);
3676 if (option_return_does_auto_indent)
3677 edit_auto_indent (edit);
3678 format_paragraph (edit, 0);
3680 else
3682 edit_insert (edit, '\n');
3683 if (option_return_does_auto_indent)
3685 edit_auto_indent (edit);
3688 break;
3689 case CK_Return:
3690 edit_insert (edit, '\n');
3691 break;
3693 case CK_Page_Up_Alt_Highlight:
3694 edit->column_highlight = 1;
3695 case CK_Page_Up:
3696 case CK_Page_Up_Highlight:
3697 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3698 break;
3699 case CK_Page_Down_Alt_Highlight:
3700 edit->column_highlight = 1;
3701 case CK_Page_Down:
3702 case CK_Page_Down_Highlight:
3703 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3704 break;
3705 case CK_Left_Alt_Highlight:
3706 edit->column_highlight = 1;
3707 case CK_Left:
3708 case CK_Left_Highlight:
3709 if (option_fake_half_tabs)
3711 if (is_in_indent (edit) && right_of_four_spaces (edit))
3713 if (option_cursor_beyond_eol && edit->over_col > 0)
3714 edit->over_col--;
3715 else
3716 edit_cursor_move (edit, -HALF_TAB_SIZE);
3717 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3718 break;
3721 edit_left_char_move_cmd (edit);
3722 break;
3723 case CK_Right_Alt_Highlight:
3724 edit->column_highlight = 1;
3725 case CK_Right:
3726 case CK_Right_Highlight:
3727 if (option_fake_half_tabs)
3729 if (is_in_indent (edit) && left_of_four_spaces (edit))
3731 edit_cursor_move (edit, HALF_TAB_SIZE);
3732 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3733 break;
3736 edit_right_char_move_cmd (edit);
3737 break;
3738 case CK_Begin_Page:
3739 case CK_Begin_Page_Highlight:
3740 edit_begin_page (edit);
3741 break;
3742 case CK_End_Page:
3743 case CK_End_Page_Highlight:
3744 edit_end_page (edit);
3745 break;
3746 case CK_Word_Left:
3747 case CK_Word_Left_Highlight:
3748 edit->over_col = 0;
3749 edit_left_word_move_cmd (edit);
3750 break;
3751 case CK_Word_Right:
3752 case CK_Word_Right_Highlight:
3753 edit->over_col = 0;
3754 edit_right_word_move_cmd (edit);
3755 break;
3756 case CK_Up_Alt_Highlight:
3757 edit->column_highlight = 1;
3758 case CK_Up:
3759 case CK_Up_Highlight:
3760 edit_move_up (edit, 1, 0);
3761 break;
3762 case CK_Down_Alt_Highlight:
3763 edit->column_highlight = 1;
3764 case CK_Down:
3765 case CK_Down_Highlight:
3766 edit_move_down (edit, 1, 0);
3767 break;
3768 case CK_Paragraph_Up_Alt_Highlight:
3769 edit->column_highlight = 1;
3770 case CK_Paragraph_Up:
3771 case CK_Paragraph_Up_Highlight:
3772 edit_move_up_paragraph (edit, 0);
3773 break;
3774 case CK_Paragraph_Down_Alt_Highlight:
3775 edit->column_highlight = 1;
3776 case CK_Paragraph_Down:
3777 case CK_Paragraph_Down_Highlight:
3778 edit_move_down_paragraph (edit, 0);
3779 break;
3780 case CK_Scroll_Up_Alt_Highlight:
3781 edit->column_highlight = 1;
3782 case CK_Scroll_Up:
3783 case CK_Scroll_Up_Highlight:
3784 edit_move_up (edit, 1, 1);
3785 break;
3786 case CK_Scroll_Down_Alt_Highlight:
3787 edit->column_highlight = 1;
3788 case CK_Scroll_Down:
3789 case CK_Scroll_Down_Highlight:
3790 edit_move_down (edit, 1, 1);
3791 break;
3792 case CK_Home:
3793 case CK_Home_Highlight:
3794 edit_cursor_to_bol (edit);
3795 break;
3796 case CK_End:
3797 case CK_End_Highlight:
3798 edit_cursor_to_eol (edit);
3799 break;
3800 case CK_Tab:
3801 /* if text marked shift block */
3802 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3804 if (edit->mark2 < 0)
3805 edit_mark_cmd (edit, 0);
3806 edit_move_block_to_right (edit);
3808 else
3810 if (option_cursor_beyond_eol)
3811 edit_insert_over (edit);
3812 edit_tab_cmd (edit);
3813 if (option_auto_para_formatting)
3815 format_paragraph (edit, 0);
3816 edit->force |= REDRAW_PAGE;
3818 else
3820 check_and_wrap_line (edit);
3823 break;
3825 case CK_Toggle_Insert:
3826 edit->overwrite = (edit->overwrite == 0);
3827 break;
3829 case CK_Mark:
3830 if (edit->mark2 >= 0)
3832 if (edit->column_highlight)
3833 edit_push_undo_action (edit, COLUMN_ON);
3834 edit->column_highlight = 0;
3836 edit_mark_cmd (edit, 0);
3837 break;
3838 case CK_Column_Mark:
3839 if (!edit->column_highlight)
3840 edit_push_undo_action (edit, COLUMN_OFF);
3841 edit->column_highlight = 1;
3842 edit_mark_cmd (edit, 0);
3843 break;
3844 case CK_Mark_All:
3845 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3846 edit->force |= REDRAW_PAGE;
3847 break;
3848 case CK_Unmark:
3849 if (edit->column_highlight)
3850 edit_push_undo_action (edit, COLUMN_ON);
3851 edit->column_highlight = 0;
3852 edit_mark_cmd (edit, 1);
3853 break;
3854 case CK_Mark_Word:
3855 if (edit->column_highlight)
3856 edit_push_undo_action (edit, COLUMN_ON);
3857 edit->column_highlight = 0;
3858 edit_mark_current_word_cmd (edit);
3859 break;
3860 case CK_Mark_Line:
3861 if (edit->column_highlight)
3862 edit_push_undo_action (edit, COLUMN_ON);
3863 edit->column_highlight = 0;
3864 edit_mark_current_line_cmd (edit);
3865 break;
3866 case CK_Toggle_Line_State:
3867 option_line_state = !option_line_state;
3868 if (option_line_state)
3870 option_line_state_width = LINE_STATE_WIDTH;
3872 else
3874 option_line_state_width = 0;
3876 edit->force |= REDRAW_PAGE;
3877 break;
3879 case CK_Toggle_Show_Margin:
3880 show_right_margin = !show_right_margin;
3881 edit->force |= REDRAW_PAGE;
3882 break;
3884 case CK_Toggle_Bookmark:
3885 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3886 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3887 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3888 else
3889 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3890 break;
3891 case CK_Flush_Bookmarks:
3892 book_mark_flush (edit, BOOK_MARK_COLOR);
3893 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3894 edit->force |= REDRAW_PAGE;
3895 break;
3896 case CK_Next_Bookmark:
3897 if (edit->book_mark)
3899 struct _book_mark *p;
3900 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3901 if (p->next)
3903 p = p->next;
3904 if (p->line >= edit->start_line + edit->num_widget_lines
3905 || p->line < edit->start_line)
3906 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3907 edit_move_to_line (edit, p->line);
3910 break;
3911 case CK_Prev_Bookmark:
3912 if (edit->book_mark)
3914 struct _book_mark *p;
3915 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3916 while (p->line == edit->curs_line)
3917 if (p->prev)
3918 p = p->prev;
3919 if (p->line >= 0)
3921 if (p->line >= edit->start_line + edit->num_widget_lines
3922 || p->line < edit->start_line)
3923 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3924 edit_move_to_line (edit, p->line);
3927 break;
3929 case CK_Beginning_Of_Text:
3930 case CK_Beginning_Of_Text_Highlight:
3931 edit_move_to_top (edit);
3932 break;
3933 case CK_End_Of_Text:
3934 case CK_End_Of_Text_Highlight:
3935 edit_move_to_bottom (edit);
3936 break;
3938 case CK_Copy:
3939 if (option_cursor_beyond_eol && edit->over_col > 0)
3940 edit_insert_over (edit);
3941 edit_block_copy_cmd (edit);
3942 break;
3943 case CK_Remove:
3944 edit_block_delete_cmd (edit);
3945 break;
3946 case CK_Move:
3947 if (option_cursor_beyond_eol && edit->over_col > 0)
3948 edit_insert_over (edit);
3949 edit_block_move_cmd (edit);
3950 break;
3952 case CK_Shift_Block_Left:
3953 if (edit->mark1 != edit->mark2)
3954 edit_move_block_to_left (edit);
3955 break;
3956 case CK_Shift_Block_Right:
3957 if (edit->mark1 != edit->mark2)
3958 edit_move_block_to_right (edit);
3959 break;
3960 case CK_XStore:
3961 edit_copy_to_X_buf_cmd (edit);
3962 break;
3963 case CK_XCut:
3964 edit_cut_to_X_buf_cmd (edit);
3965 break;
3966 case CK_XPaste:
3967 /* if non persistent selection and text selected */
3968 if (!option_persistent_selections)
3970 if (edit->mark1 != edit->mark2)
3971 edit_block_delete_cmd (edit);
3973 if (option_cursor_beyond_eol && edit->over_col > 0)
3974 edit_insert_over (edit);
3975 edit_paste_from_X_buf_cmd (edit);
3976 break;
3977 case CK_Selection_History:
3978 edit_paste_from_history (edit);
3979 break;
3981 case CK_Save_As:
3982 edit_save_as_cmd (edit);
3983 break;
3984 case CK_Save:
3985 edit_save_confirm_cmd (edit);
3986 break;
3987 case CK_Load:
3988 edit_load_cmd (edit, EDIT_FILE_COMMON);
3989 break;
3990 case CK_Save_Block:
3991 edit_save_block_cmd (edit);
3992 break;
3993 case CK_Insert_File:
3994 edit_insert_file_cmd (edit);
3995 break;
3997 case CK_Load_Prev_File:
3998 edit_load_back_cmd (edit);
3999 break;
4000 case CK_Load_Next_File:
4001 edit_load_forward_cmd (edit);
4002 break;
4004 case CK_Load_Syntax_File:
4005 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4006 break;
4007 case CK_Choose_Syntax:
4008 edit_syntax_dialog (edit);
4009 break;
4011 case CK_Load_Menu_File:
4012 edit_load_cmd (edit, EDIT_FILE_MENU);
4013 break;
4015 case CK_Toggle_Syntax:
4016 option_syntax_highlighting ^= 1;
4017 if (option_syntax_highlighting == 1)
4018 edit_load_syntax (edit, NULL, edit->syntax_type);
4019 edit->force |= REDRAW_PAGE;
4020 break;
4022 case CK_Toggle_Tab_TWS:
4023 enable_show_tabs_tws ^= 1;
4024 edit->force |= REDRAW_PAGE;
4025 break;
4027 case CK_Find:
4028 edit_search_cmd (edit, FALSE);
4029 break;
4030 case CK_Find_Again:
4031 edit_search_cmd (edit, TRUE);
4032 break;
4033 case CK_Replace:
4034 edit_replace_cmd (edit, 0);
4035 break;
4036 case CK_Replace_Again:
4037 edit_replace_cmd (edit, 1);
4038 break;
4039 case CK_Complete_Word:
4040 /* if text marked shift block */
4041 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4043 edit_move_block_to_left (edit);
4045 else
4047 edit_complete_word_cmd (edit);
4049 break;
4050 case CK_Find_Definition:
4051 edit_get_match_keyword_cmd (edit);
4052 break;
4053 case CK_Quit:
4054 dlg_stop (edit->widget.owner);
4055 break;
4056 case CK_New:
4057 edit_new_cmd (edit);
4058 break;
4059 case CK_Help:
4060 edit_help_cmd (edit);
4061 break;
4062 case CK_Refresh:
4063 edit_refresh_cmd (edit);
4064 break;
4065 case CK_SaveSetupCmd:
4066 save_setup_cmd ();
4067 break;
4068 case CK_About:
4069 edit_about ();
4070 break;
4071 case CK_LearnKeys:
4072 learn_keys ();
4073 break;
4074 case CK_Edit_Options:
4075 edit_options_dialog (edit);
4076 break;
4077 case CK_Edit_Save_Mode:
4078 menu_save_mode_cmd ();
4079 break;
4080 case CK_Date:
4082 char s[BUF_MEDIUM];
4083 /* fool gcc to prevent a Y2K warning */
4084 char time_format[] = "_c";
4085 time_format[0] = '%';
4087 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4088 edit_print_string (edit, s);
4089 edit->force |= REDRAW_PAGE;
4090 break;
4092 break;
4093 case CK_Goto:
4094 edit_goto_cmd (edit);
4095 break;
4096 case CK_Paragraph_Format:
4097 format_paragraph (edit, 1);
4098 edit->force |= REDRAW_PAGE;
4099 break;
4100 case CK_Delete_Macro:
4101 edit_delete_macro_cmd (edit);
4102 break;
4103 case CK_Match_Bracket:
4104 edit_goto_matching_bracket (edit);
4105 break;
4106 case CK_User_Menu:
4107 user_menu (edit, NULL, -1);
4108 break;
4109 case CK_Sort:
4110 edit_sort_cmd (edit);
4111 break;
4112 case CK_ExtCmd:
4113 edit_ext_cmd (edit);
4114 break;
4115 case CK_Mail:
4116 edit_mail_dialog (edit);
4117 break;
4118 case CK_Shell:
4119 view_other_cmd ();
4120 break;
4121 case CK_SelectCodepage:
4122 edit_select_codepage_cmd (edit);
4123 break;
4124 case CK_Insert_Literal:
4125 edit_insert_literal_cmd (edit);
4126 break;
4127 case CK_Begin_End_Macro:
4128 edit_begin_end_macro_cmd (edit);
4129 break;
4130 case CK_Begin_End_Repeat:
4131 edit_begin_end_repeat_cmd (edit);
4132 break;
4133 case CK_Ext_Mode:
4134 edit->extmod = 1;
4135 break;
4136 default:
4137 break;
4140 /* CK_Pipe_Block */
4141 if ((command / CK_Pipe_Block (0)) == 1)
4142 edit_block_process_cmd (edit, command - CK_Pipe_Block (0));
4144 /* keys which must set the col position, and the search vars */
4145 switch (command)
4147 case CK_Find:
4148 case CK_Find_Again:
4149 case CK_Replace:
4150 case CK_Replace_Again:
4151 case CK_Complete_Word:
4152 edit->prev_col = edit_get_col (edit);
4153 break;
4154 case CK_Up:
4155 case CK_Up_Highlight:
4156 case CK_Up_Alt_Highlight:
4157 case CK_Down:
4158 case CK_Down_Highlight:
4159 case CK_Down_Alt_Highlight:
4160 case CK_Page_Up:
4161 case CK_Page_Up_Highlight:
4162 case CK_Page_Up_Alt_Highlight:
4163 case CK_Page_Down:
4164 case CK_Page_Down_Highlight:
4165 case CK_Page_Down_Alt_Highlight:
4166 case CK_Beginning_Of_Text:
4167 case CK_Beginning_Of_Text_Highlight:
4168 case CK_End_Of_Text:
4169 case CK_End_Of_Text_Highlight:
4170 case CK_Paragraph_Up:
4171 case CK_Paragraph_Up_Highlight:
4172 case CK_Paragraph_Up_Alt_Highlight:
4173 case CK_Paragraph_Down:
4174 case CK_Paragraph_Down_Highlight:
4175 case CK_Paragraph_Down_Alt_Highlight:
4176 case CK_Scroll_Up:
4177 case CK_Scroll_Up_Highlight:
4178 case CK_Scroll_Up_Alt_Highlight:
4179 case CK_Scroll_Down:
4180 case CK_Scroll_Down_Highlight:
4181 case CK_Scroll_Down_Alt_Highlight:
4182 edit->search_start = edit->curs1;
4183 edit->found_len = 0;
4184 break;
4185 default:
4186 edit->found_len = 0;
4187 edit->prev_col = edit_get_col (edit);
4188 edit->search_start = edit->curs1;
4190 edit_find_bracket (edit);
4192 if (option_auto_para_formatting)
4194 switch (command)
4196 case CK_BackSpace:
4197 case CK_Delete:
4198 case CK_Delete_Word_Left:
4199 case CK_Delete_Word_Right:
4200 case CK_Delete_To_Line_End:
4201 case CK_Delete_To_Line_Begin:
4202 format_paragraph (edit, 0);
4203 edit->force |= REDRAW_PAGE;
4208 /* --------------------------------------------------------------------------------------------- */
4210 void
4211 edit_stack_init (void)
4213 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4215 edit_history_moveto[edit_stack_iterator].filename = NULL;
4216 edit_history_moveto[edit_stack_iterator].line = -1;
4219 edit_stack_iterator = 0;
4222 /* --------------------------------------------------------------------------------------------- */
4224 void
4225 edit_stack_free (void)
4227 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4228 g_free (edit_history_moveto[edit_stack_iterator].filename);
4231 /* --------------------------------------------------------------------------------------------- */
4232 /** move i lines */
4234 void
4235 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4237 edit_move_updown (edit, i, do_scroll, TRUE);
4240 /* --------------------------------------------------------------------------------------------- */
4241 /** move i lines */
4243 void
4244 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4246 edit_move_updown (edit, i, do_scroll, FALSE);
4249 /* --------------------------------------------------------------------------------------------- */
4251 unsigned int
4252 edit_unlock_file (WEdit * edit)
4254 char *fullpath;
4255 unsigned int ret;
4257 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4258 ret = unlock_file (fullpath);
4259 g_free (fullpath);
4261 return ret;
4264 /* --------------------------------------------------------------------------------------------- */
4266 unsigned int
4267 edit_lock_file (WEdit * edit)
4269 char *fullpath;
4270 unsigned int ret;
4272 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4273 ret = lock_file (fullpath);
4274 g_free (fullpath);
4276 return ret;
4279 /* --------------------------------------------------------------------------------------------- */