(edit_insert_file): change variable type from int to gboolean.
[midnight-commander.git] / src / editor / edit.c
blobde75ac624e23b078f8840770af16fcc63b3d53b2
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996-2023
5 Free Software Foundation, Inc.
7 Written by:
8 Paul Sheer 1996, 1997
9 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
10 Andrew Borodin <aborodin@vmail.ru> 2012-2022
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor low level data handling and cursor fundamentals
30 * \author Paul Sheer
31 * \date 1996, 1997
34 #include <config.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <sys/stat.h>
42 #include <stdint.h> /* UINTMAX_MAX */
43 #include <stdlib.h>
45 #include "lib/global.h"
47 #include "lib/tty/color.h"
48 #include "lib/tty/tty.h" /* attrset() */
49 #include "lib/tty/key.h" /* is_idle() */
50 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
51 #include "lib/fileloc.h" /* EDIT_HOME_BLOCK_FILE */
52 #include "lib/vfs/vfs.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/util.h" /* load_file_position(), save_file_position() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "lib/lock.h"
57 #include "lib/widget.h"
59 #ifdef HAVE_CHARSET
60 #include "lib/charsets.h" /* get_codepage_id */
61 #endif
63 #include "src/usermenu.h" /* user_menu_cmd() */
65 #include "src/keymap.h"
67 #include "edit-impl.h"
68 #include "editwidget.h"
69 #include "editsearch.h"
70 #include "editcomplete.h" /* edit_complete_word_cmd() */
71 #include "editmacros.h"
72 #include "etags.h" /* edit_get_match_keyword_cmd() */
73 #ifdef HAVE_ASPELL
74 #include "spell.h"
75 #endif
77 /*** global variables ****************************************************************************/
79 edit_options_t edit_options = {
80 .word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH,
81 .typewriter_wrap = FALSE,
82 .auto_para_formatting = FALSE,
83 .fill_tabs_with_spaces = FALSE,
84 .return_does_auto_indent = TRUE,
85 .backspace_through_tabs = FALSE,
86 .fake_half_tabs = TRUE,
87 .persistent_selections = TRUE,
88 .drop_selection_on_copy = TRUE,
89 .cursor_beyond_eol = FALSE,
90 .cursor_after_inserted_block = FALSE,
91 .state_full_filename = FALSE,
92 .line_state = FALSE,
93 .line_state_width = 0,
94 .save_mode = EDIT_QUICK_SAVE,
95 .confirm_save = TRUE,
96 .save_position = TRUE,
97 .syntax_highlighting = TRUE,
98 .group_undo = FALSE,
99 .backup_ext = NULL,
100 .filesize_threshold = NULL,
101 .stop_format_chars = NULL,
102 .visible_tabs = TRUE,
103 .visible_tws = TRUE,
104 .show_right_margin = FALSE,
105 .simple_statusbar = FALSE,
106 .check_nl_at_eof = FALSE
109 int max_undo = 32768;
111 gboolean enable_show_tabs_tws = TRUE;
113 unsigned int edit_stack_iterator = 0;
114 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
115 /* magic sequence for say than block is vertical */
116 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
118 /*** file scope macro definitions ****************************************************************/
120 #define TEMP_BUF_LEN 1024
122 #define space_width 1
124 /*** file scope type declarations ****************************************************************/
126 /*** forward declarations (file scope functions) *************************************************/
128 /*** file scope variables ************************************************************************/
130 /* detecting an error on save is easy: just check if every byte has been written. */
131 /* detecting an error on read, is not so easy 'cos there is not way to tell
132 whether you read everything or not. */
133 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
134 static const struct edit_filters
136 const char *read, *write, *extension;
137 } all_filters[] =
139 /* *INDENT-OFF* */
140 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
141 { "zstd -cd %s 2>&1", "zstd > %s", ".zst"},
142 { "lz4 -cd %s 2>&1", "lz4 > %s", ".lz4" },
143 { "lzip -cd %s 2>&1", "lzip > %s", ".lz"},
144 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
145 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
146 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
147 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
148 /* *INDENT-ON* */
151 static const off_t filesize_default_threshold = 64 * 1024 * 1024; /* 64 MB */
153 /* --------------------------------------------------------------------------------------------- */
154 /*** file scope functions ************************************************************************/
155 /* --------------------------------------------------------------------------------------------- */
157 static int
158 edit_load_status_update_cb (status_msg_t * sm)
160 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
161 edit_buffer_read_file_status_msg_t *rsm = (edit_buffer_read_file_status_msg_t *) sm;
162 Widget *wd = WIDGET (sm->dlg);
164 if (verbose)
165 label_set_textv (ssm->label, _("Loading: %3d%%"),
166 edit_buffer_calc_percent (rsm->buf, rsm->loaded));
167 else
168 label_set_text (ssm->label, _("Loading..."));
170 if (rsm->first)
172 Widget *lw = WIDGET (ssm->label);
173 WRect r;
175 r = wd->rect;
176 r.cols = MAX (r.cols, lw->rect.cols + 6);
177 widget_set_size_rect (wd, &r);
178 r = lw->rect;
179 r.x = wd->rect.x + (wd->rect.cols - r.cols) / 2;
180 widget_set_size_rect (lw, &r);
181 rsm->first = FALSE;
184 return status_msg_common_update (sm);
187 /* --------------------------------------------------------------------------------------------- */
189 * Load file OR text into buffers. Set cursor to the beginning of file.
191 * @return FALSE on error.
194 static gboolean
195 edit_load_file_fast (edit_buffer_t * buf, const vfs_path_t * filename_vpath)
197 int file;
198 gboolean ret;
199 edit_buffer_read_file_status_msg_t rsm;
200 gboolean aborted;
202 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
203 if (file < 0)
205 gchar *errmsg;
207 errmsg =
208 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
209 edit_error_dialog (_("Error"), errmsg);
210 g_free (errmsg);
211 return FALSE;
214 rsm.first = TRUE;
215 rsm.buf = buf;
216 rsm.loaded = 0;
218 status_msg_init (STATUS_MSG (&rsm), _("Load file"), 1.0, simple_status_msg_init_cb,
219 edit_load_status_update_cb, NULL);
221 ret = (edit_buffer_read_file (buf, file, buf->size, &rsm, &aborted) == buf->size);
223 status_msg_deinit (STATUS_MSG (&rsm));
225 if (!ret && !aborted)
227 gchar *errmsg;
229 errmsg = g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath));
230 edit_error_dialog (_("Error"), errmsg);
231 g_free (errmsg);
234 mc_close (file);
235 return ret;
238 /* --------------------------------------------------------------------------------------------- */
239 /** Return index of the filter or -1 is there is no appropriate filter */
241 static int
242 edit_find_filter (const vfs_path_t * filename_vpath)
244 if (filename_vpath != NULL)
246 const char *s;
247 size_t i;
249 s = vfs_path_as_str (filename_vpath);
251 for (i = 0; i < G_N_ELEMENTS (all_filters); i++)
252 if (g_str_has_suffix (s, all_filters[i].extension))
253 return i;
256 return -1;
259 /* --------------------------------------------------------------------------------------------- */
261 static char *
262 edit_get_filter (const vfs_path_t * filename_vpath)
264 int i;
265 char *quoted_name;
266 char *p = NULL;
268 i = edit_find_filter (filename_vpath);
269 if (i < 0)
270 return NULL;
272 quoted_name = name_quote (vfs_path_as_str (filename_vpath), FALSE);
273 if (quoted_name != NULL)
275 p = g_strdup_printf (all_filters[i].read, quoted_name);
276 g_free (quoted_name);
279 return p;
282 /* --------------------------------------------------------------------------------------------- */
284 static off_t
285 edit_insert_stream (WEdit * edit, FILE * f)
287 int c;
288 off_t i;
290 for (i = 0; (c = fgetc (f)) >= 0; i++)
291 edit_insert (edit, c);
293 return i;
296 /* --------------------------------------------------------------------------------------------- */
298 * Open file and create it if necessary.
300 * @param edit editor object
301 * @param filename_vpath file name
302 * @param st buffer for store stat info
303 * @return TRUE for success, FALSE for error.
306 static gboolean
307 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
309 static uintmax_t threshold = UINTMAX_MAX;
310 int file;
311 gchar *errmsg = NULL;
312 gboolean ret = TRUE;
314 /* Try opening an existing file */
315 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
316 if (file < 0)
319 * Try creating the file. O_EXCL prevents following broken links
320 * and opening existing files.
322 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
323 if (file < 0)
325 errmsg =
326 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
327 goto cleanup;
330 /* New file, delete it if it's not modified or saved */
331 edit->delete_file = 1;
334 /* Check what we have opened */
335 if (mc_fstat (file, st) < 0)
337 errmsg =
338 g_strdup_printf (_("Cannot get size/permissions for %s"),
339 vfs_path_as_str (filename_vpath));
340 goto cleanup;
343 /* We want to open regular files only */
344 if (!S_ISREG (st->st_mode))
346 errmsg =
347 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath));
348 goto cleanup;
351 /* get file size threshold for alarm */
352 if (threshold == UINTMAX_MAX)
354 gboolean err = FALSE;
356 threshold = parse_integer (edit_options.filesize_threshold, &err);
357 if (err)
358 threshold = filesize_default_threshold;
362 * Don't delete non-empty files.
363 * O_EXCL should prevent it, but let's be on the safe side.
365 if (st->st_size > 0)
366 edit->delete_file = 0;
368 if ((uintmax_t) st->st_size > threshold)
370 int act;
372 errmsg = g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"),
373 vfs_path_as_str (filename_vpath));
374 act = edit_query_dialog2 (_("Warning"), errmsg, _("&Yes"), _("&No"));
375 MC_PTR_FREE (errmsg);
377 if (act != 0)
378 ret = FALSE;
381 cleanup:
382 (void) mc_close (file);
384 if (errmsg != NULL)
386 edit_error_dialog (_("Error"), errmsg);
387 g_free (errmsg);
388 ret = FALSE;
391 return ret;
394 /* --------------------------------------------------------------------------------------------- */
397 * Open the file and load it into the buffers, either directly or using
398 * a filter. Return TRUE on success, FALSE on error.
400 * Fast loading (edit_load_file_fast) is used when the file size is
401 * known. In this case the data is read into the buffers by blocks.
402 * If the file size is not known, the data is loaded byte by byte in
403 * edit_insert_file.
405 * @param edit editor object
406 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
408 static gboolean
409 edit_load_file (WEdit * edit)
411 gboolean fast_load = TRUE;
413 /* Cannot do fast load if a filter is used */
414 if (edit_find_filter (edit->filename_vpath) >= 0)
415 fast_load = FALSE;
418 * FIXME: line end translation should disable fast loading as well
419 * Consider doing fseek() to the end and ftell() for the real size.
421 if (edit->filename_vpath != NULL)
424 * VFS may report file size incorrectly, and slow load is not a big
425 * deal considering overhead in VFS.
427 if (!vfs_file_is_local (edit->filename_vpath))
428 fast_load = FALSE;
430 /* If we are dealing with a real file, check that it exists */
431 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
433 edit_clean (edit);
434 return FALSE;
437 else
439 /* nothing to load */
440 fast_load = FALSE;
443 if (fast_load)
445 edit_buffer_init (&edit->buffer, edit->stat1.st_size);
447 if (!edit_load_file_fast (&edit->buffer, edit->filename_vpath))
449 edit_clean (edit);
450 return FALSE;
453 else
455 edit_buffer_init (&edit->buffer, 0);
457 if (edit->filename_vpath != NULL
458 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
460 edit->undo_stack_disable = 1;
461 if (edit_insert_file (edit, edit->filename_vpath) < 0)
463 edit_clean (edit);
464 return FALSE;
466 edit->undo_stack_disable = 0;
469 edit->lb = LB_ASIS;
470 return TRUE;
473 /* --------------------------------------------------------------------------------------------- */
475 * Restore saved cursor position and/or bookmarks in the file
477 * @param edit editor object
478 * @param load_position If TRUE, load bookmarks and cursor position and apply them.
479 * If FALSE, load bookmarks only.
482 static void
483 edit_load_position (WEdit * edit, gboolean load_position)
485 long line, column;
486 off_t offset;
488 if (edit->filename_vpath == NULL
489 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
490 return;
492 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
493 /* apply bookmarks in any case */
494 book_mark_restore (edit, BOOK_MARK_COLOR);
496 if (!load_position)
497 return;
499 if (line > 0)
501 edit_move_to_line (edit, line - 1);
502 edit->prev_col = column;
504 else if (offset > 0)
506 edit_cursor_move (edit, offset);
507 line = edit->buffer.curs_line;
508 edit->search_start = edit->buffer.curs1;
511 edit_move_to_prev_col (edit, edit_buffer_get_current_bol (&edit->buffer));
512 edit_move_display (edit, line - (WIDGET (edit)->rect.lines / 2));
515 /* --------------------------------------------------------------------------------------------- */
516 /** Save cursor position in the file */
518 static void
519 edit_save_position (WEdit * edit)
521 if (edit->filename_vpath == NULL
522 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
523 return;
525 book_mark_serialize (edit, BOOK_MARK_COLOR);
526 save_file_position (edit->filename_vpath, edit->buffer.curs_line + 1, edit->curs_col,
527 edit->buffer.curs1, edit->serialized_bookmarks);
528 edit->serialized_bookmarks = NULL;
531 /* --------------------------------------------------------------------------------------------- */
532 /** Clean the WEdit stricture except the widget part */
534 static void
535 edit_purge_widget (WEdit * edit)
537 size_t len = sizeof (WEdit) - sizeof (Widget);
538 char *start = (char *) edit + sizeof (Widget);
539 memset (start, 0, len);
542 /* --------------------------------------------------------------------------------------------- */
545 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
546 then the file should be as it was when he loaded up. Then set edit->modified to 0.
549 static long
550 edit_pop_undo_action (WEdit * edit)
552 long c;
553 unsigned long sp = edit->undo_stack_pointer;
555 if (sp == edit->undo_stack_bottom)
556 return STACK_BOTTOM;
558 sp = (sp - 1) & edit->undo_stack_size_mask;
559 c = edit->undo_stack[sp];
560 if (c >= 0)
562 /* edit->undo_stack[sp] = '@'; */
563 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
564 return c;
567 if (sp == edit->undo_stack_bottom)
568 return STACK_BOTTOM;
570 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
571 if (edit->undo_stack[sp] == -2)
573 /* edit->undo_stack[sp] = '@'; */
574 edit->undo_stack_pointer = sp;
576 else
577 edit->undo_stack[sp]++;
579 return c;
582 /* --------------------------------------------------------------------------------------------- */
584 static long
585 edit_pop_redo_action (WEdit * edit)
587 long c;
588 unsigned long sp = edit->redo_stack_pointer;
590 if (sp == edit->redo_stack_bottom)
591 return STACK_BOTTOM;
593 sp = (sp - 1) & edit->redo_stack_size_mask;
594 c = edit->redo_stack[sp];
595 if (c >= 0)
597 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
598 return c;
601 if (sp == edit->redo_stack_bottom)
602 return STACK_BOTTOM;
604 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
605 if (edit->redo_stack[sp] == -2)
606 edit->redo_stack_pointer = sp;
607 else
608 edit->redo_stack[sp]++;
610 return c;
613 /* --------------------------------------------------------------------------------------------- */
615 static long
616 get_prev_undo_action (WEdit * edit)
618 long c;
619 unsigned long sp = edit->undo_stack_pointer;
621 if (sp == edit->undo_stack_bottom)
622 return STACK_BOTTOM;
624 sp = (sp - 1) & edit->undo_stack_size_mask;
625 c = edit->undo_stack[sp];
626 if (c >= 0)
627 return c;
629 if (sp == edit->undo_stack_bottom)
630 return STACK_BOTTOM;
632 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
633 return c;
636 /* --------------------------------------------------------------------------------------------- */
637 /** is called whenever a modification is made by one of the four routines below */
639 static void
640 edit_modification (WEdit * edit)
642 edit->caches_valid = FALSE;
644 /* raise lock when file modified */
645 if (!edit->modified && !edit->delete_file)
646 edit->locked = lock_file (edit->filename_vpath);
647 edit->modified = 1;
650 /* --------------------------------------------------------------------------------------------- */
651 /* high level cursor movement commands */
652 /* --------------------------------------------------------------------------------------------- */
653 /** check whether cursor is in indent part of line
655 * @param edit editor object
657 * @return TRUE if cursor is in indent, FALSE otherwise
660 static gboolean
661 is_in_indent (const edit_buffer_t * buf)
663 off_t p;
665 for (p = edit_buffer_get_current_bol (buf); p < buf->curs1; p++)
666 if (strchr (" \t", edit_buffer_get_byte (buf, p)) == NULL)
667 return FALSE;
669 return TRUE;
672 /* --------------------------------------------------------------------------------------------- */
673 /** check whether line in editor is blank or not
675 * @param edit editor object
676 * @param offset position in file
678 * @return TRUE if line in blank, FALSE otherwise
681 static gboolean
682 is_blank (const edit_buffer_t * buf, off_t offset)
684 off_t s, f;
686 s = edit_buffer_get_bol (buf, offset);
687 f = edit_buffer_get_eol (buf, offset) - 1;
688 while (s <= f)
690 int c;
692 c = edit_buffer_get_byte (buf, s++);
693 if (!isspace (c))
694 return FALSE;
696 return TRUE;
699 /* --------------------------------------------------------------------------------------------- */
700 /** returns the offset of line i */
702 static off_t
703 edit_find_line (WEdit * edit, long line)
705 long i, j = 0;
706 long m = 2000000000; /* what is the magic number? */
708 if (!edit->caches_valid)
710 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
711 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
712 /* three offsets that we *know* are line 0 at 0 and these two: */
713 edit->line_numbers[1] = edit->buffer.curs_line;
714 edit->line_offsets[1] = edit_buffer_get_current_bol (&edit->buffer);
715 edit->line_numbers[2] = edit->buffer.lines;
716 edit->line_offsets[2] = edit_buffer_get_bol (&edit->buffer, edit->buffer.size);
717 edit->caches_valid = TRUE;
719 if (line >= edit->buffer.lines)
720 return edit->line_offsets[2];
721 if (line <= 0)
722 return 0;
723 /* find the closest known point */
724 for (i = 0; i < N_LINE_CACHES; i++)
726 long n;
728 n = labs (edit->line_numbers[i] - line);
729 if (n < m)
731 m = n;
732 j = i;
735 if (m == 0)
736 return edit->line_offsets[j]; /* know the offset exactly */
737 if (m == 1 && j >= 3)
738 i = j; /* one line different - caller might be looping, so stay in this cache */
739 else
740 i = 3 + (rand () % (N_LINE_CACHES - 3));
741 if (line > edit->line_numbers[j])
742 edit->line_offsets[i] =
743 edit_buffer_get_forward_offset (&edit->buffer, edit->line_offsets[j],
744 line - edit->line_numbers[j], 0);
745 else
746 edit->line_offsets[i] =
747 edit_buffer_get_backward_offset (&edit->buffer, edit->line_offsets[j],
748 edit->line_numbers[j] - line);
749 edit->line_numbers[i] = line;
750 return edit->line_offsets[i];
753 /* --------------------------------------------------------------------------------------------- */
754 /** moves up until a blank line is reached, or until just
755 before a non-blank line is reached */
757 static void
758 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
760 long i = 0;
762 if (edit->buffer.curs_line > 1)
764 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
766 for (i = edit->buffer.curs_line - 1; i != 0; i--)
767 if (edit_line_is_blank (edit, i))
768 break;
770 else if (edit_line_is_blank (edit, edit->buffer.curs_line - 1))
772 for (i = edit->buffer.curs_line - 1; i != 0; i--)
773 if (!edit_line_is_blank (edit, i))
775 i++;
776 break;
779 else
781 for (i = edit->buffer.curs_line - 1; i != 0; i--)
782 if (edit_line_is_blank (edit, i))
783 break;
787 edit_move_up (edit, edit->buffer.curs_line - i, do_scroll);
790 /* --------------------------------------------------------------------------------------------- */
791 /** moves down until a blank line is reached, or until just
792 before a non-blank line is reached */
794 static void
795 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
797 long i;
799 if (edit->buffer.curs_line >= edit->buffer.lines - 1)
800 i = edit->buffer.lines;
801 else if (!edit_line_is_blank (edit, edit->buffer.curs_line))
803 for (i = edit->buffer.curs_line + 1; i != 0; i++)
804 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
805 break;
807 else if (edit_line_is_blank (edit, edit->buffer.curs_line + 1))
809 for (i = edit->buffer.curs_line + 1; i != 0; i++)
810 if (!edit_line_is_blank (edit, i) || i > edit->buffer.lines)
812 i--;
813 break;
816 else
818 for (i = edit->buffer.curs_line + 1; i != 0; i++)
819 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
820 break;
822 edit_move_down (edit, i - edit->buffer.curs_line, do_scroll);
825 /* --------------------------------------------------------------------------------------------- */
827 static void
828 edit_begin_page (WEdit * edit)
830 edit_update_curs_row (edit);
831 edit_move_up (edit, edit->curs_row, FALSE);
834 /* --------------------------------------------------------------------------------------------- */
836 static void
837 edit_end_page (WEdit * edit)
839 edit_update_curs_row (edit);
840 edit_move_down (edit, WIDGET (edit)->rect.lines - edit->curs_row - 1, FALSE);
844 /* --------------------------------------------------------------------------------------------- */
845 /** goto beginning of text */
847 static void
848 edit_move_to_top (WEdit * edit)
850 if (edit->buffer.curs_line != 0)
852 edit_cursor_move (edit, -edit->buffer.curs1);
853 edit_move_to_prev_col (edit, 0);
854 edit->force |= REDRAW_PAGE;
855 edit->search_start = 0;
856 edit_update_curs_row (edit);
860 /* --------------------------------------------------------------------------------------------- */
861 /** goto end of text */
863 static void
864 edit_move_to_bottom (WEdit * edit)
866 if (edit->buffer.curs_line < edit->buffer.lines)
868 edit_move_down (edit, edit->buffer.lines - edit->curs_row, FALSE);
869 edit->start_display = edit->buffer.size;
870 edit->start_line = edit->buffer.lines;
871 edit_scroll_upward (edit, WIDGET (edit)->rect.lines - 1);
872 edit->force |= REDRAW_PAGE;
876 /* --------------------------------------------------------------------------------------------- */
877 /** goto beginning of line */
879 static void
880 edit_cursor_to_bol (WEdit * edit)
882 edit_cursor_move (edit, edit_buffer_get_current_bol (&edit->buffer) - edit->buffer.curs1);
883 edit->search_start = edit->buffer.curs1;
884 edit->prev_col = edit_get_col (edit);
885 edit->over_col = 0;
888 /* --------------------------------------------------------------------------------------------- */
889 /** goto end of line */
891 static void
892 edit_cursor_to_eol (WEdit * edit)
894 edit_cursor_move (edit, edit_buffer_get_current_eol (&edit->buffer) - edit->buffer.curs1);
895 edit->search_start = edit->buffer.curs1;
896 edit->prev_col = edit_get_col (edit);
897 edit->over_col = 0;
900 /* --------------------------------------------------------------------------------------------- */
902 static unsigned long
903 my_type_of (int c)
905 unsigned long r = 0;
906 const char *q;
907 const char chars_move_whole_word[] =
908 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
910 if (c == 0)
911 return 0;
912 if (c == '!')
913 return 2;
915 if (g_ascii_isupper ((gchar) c))
916 c = 'A';
917 else if (g_ascii_islower ((gchar) c))
918 c = 'a';
919 else if (g_ascii_isalpha (c))
920 c = 'a';
921 else if (isdigit (c))
922 c = '0';
923 else if (isspace (c))
924 c = ' ';
925 q = strchr (chars_move_whole_word, c);
926 if (!q)
927 return 0xFFFFFFFFUL;
930 unsigned long x;
931 const char *p;
933 for (x = 1, p = chars_move_whole_word; p < q; p++)
934 if (*p == '!')
935 x <<= 1;
936 r |= x;
938 while ((q = strchr (q + 1, c)));
939 return r;
942 /* --------------------------------------------------------------------------------------------- */
944 static void
945 edit_left_word_move (WEdit * edit, int s)
947 while (TRUE)
949 int c1, c2;
951 if (edit->column_highlight
952 && edit->mark1 != edit->mark2
953 && edit->over_col == 0
954 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
955 break;
956 edit_cursor_move (edit, -1);
957 if (edit->buffer.curs1 == 0)
958 break;
959 c1 = edit_buffer_get_previous_byte (&edit->buffer);
960 if (c1 == '\n')
961 break;
962 c2 = edit_buffer_get_current_byte (&edit->buffer);
963 if (c2 == '\n')
964 break;
965 if ((my_type_of (c1) & my_type_of (c2)) == 0)
966 break;
967 if (isspace (c1) && !isspace (c2))
968 break;
969 if (s != 0 && !isspace (c1) && isspace (c2))
970 break;
974 /* --------------------------------------------------------------------------------------------- */
976 static void
977 edit_left_word_move_cmd (WEdit * edit)
979 edit_left_word_move (edit, 0);
980 edit->force |= REDRAW_PAGE;
983 /* --------------------------------------------------------------------------------------------- */
985 static void
986 edit_right_word_move (WEdit * edit, int s)
988 while (TRUE)
990 int c1, c2;
992 if (edit->column_highlight
993 && edit->mark1 != edit->mark2
994 && edit->over_col == 0
995 && edit->buffer.curs1 == edit_buffer_get_current_eol (&edit->buffer))
996 break;
997 edit_cursor_move (edit, 1);
998 if (edit->buffer.curs1 >= edit->buffer.size)
999 break;
1000 c1 = edit_buffer_get_previous_byte (&edit->buffer);
1001 if (c1 == '\n')
1002 break;
1003 c2 = edit_buffer_get_current_byte (&edit->buffer);
1004 if (c2 == '\n')
1005 break;
1006 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1007 break;
1008 if (isspace (c1) && !isspace (c2))
1009 break;
1010 if (s != 0 && !isspace (c1) && isspace (c2))
1011 break;
1015 /* --------------------------------------------------------------------------------------------- */
1017 static void
1018 edit_right_word_move_cmd (WEdit * edit)
1020 edit_right_word_move (edit, 0);
1021 edit->force |= REDRAW_PAGE;
1024 /* --------------------------------------------------------------------------------------------- */
1026 static void
1027 edit_right_char_move_cmd (WEdit * edit)
1029 int char_length = 1;
1030 int c;
1032 #ifdef HAVE_CHARSET
1033 if (edit->utf8)
1035 c = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1036 if (char_length < 1)
1037 char_length = 1;
1039 else
1040 #endif
1041 c = edit_buffer_get_current_byte (&edit->buffer);
1043 if (edit_options.cursor_beyond_eol && c == '\n')
1044 edit->over_col++;
1045 else
1046 edit_cursor_move (edit, char_length);
1049 /* --------------------------------------------------------------------------------------------- */
1051 static void
1052 edit_left_char_move_cmd (WEdit * edit)
1054 int char_length = 1;
1056 if (edit->column_highlight
1057 && edit_options.cursor_beyond_eol
1058 && edit->mark1 != edit->mark2
1059 && edit->over_col == 0 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
1060 return;
1061 #ifdef HAVE_CHARSET
1062 if (edit->utf8)
1064 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1065 if (char_length < 1)
1066 char_length = 1;
1068 #endif
1070 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
1071 edit->over_col--;
1072 else
1073 edit_cursor_move (edit, -char_length);
1076 /* --------------------------------------------------------------------------------------------- */
1077 /** Up or down cursor moving.
1078 direction = TRUE - move up
1079 = FALSE - move down
1082 static void
1083 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1085 long p;
1086 long l = direction ? edit->buffer.curs_line : edit->buffer.lines - edit->buffer.curs_line;
1088 if (lines > l)
1089 lines = l;
1091 if (lines == 0)
1092 return;
1094 if (lines > 1)
1095 edit->force |= REDRAW_PAGE;
1096 if (do_scroll)
1098 if (direction)
1099 edit_scroll_upward (edit, lines);
1100 else
1101 edit_scroll_downward (edit, lines);
1103 p = edit_buffer_get_current_bol (&edit->buffer);
1104 p = direction ? edit_buffer_get_backward_offset (&edit->buffer, p, lines) :
1105 edit_buffer_get_forward_offset (&edit->buffer, p, lines, 0);
1106 edit_cursor_move (edit, p - edit->buffer.curs1);
1107 edit_move_to_prev_col (edit, p);
1109 #ifdef HAVE_CHARSET
1110 /* search start of current multibyte char (like CJK) */
1111 if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->buffer.size
1112 && edit_buffer_get_current_byte (&edit->buffer) >= 256)
1114 edit_right_char_move_cmd (edit);
1115 edit_left_char_move_cmd (edit);
1117 #endif
1119 edit->search_start = edit->buffer.curs1;
1120 edit->found_len = 0;
1123 /* --------------------------------------------------------------------------------------------- */
1125 static void
1126 edit_right_delete_word (WEdit * edit)
1128 while (edit->buffer.curs1 < edit->buffer.size)
1130 int c1, c2;
1132 c1 = edit_delete (edit, TRUE);
1133 if (c1 == '\n')
1134 break;
1135 c2 = edit_buffer_get_current_byte (&edit->buffer);
1136 if (c2 == '\n')
1137 break;
1138 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1139 break;
1140 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1141 break;
1145 /* --------------------------------------------------------------------------------------------- */
1147 static void
1148 edit_left_delete_word (WEdit * edit)
1150 while (edit->buffer.curs1 > 0)
1152 int c1, c2;
1154 c1 = edit_backspace (edit, TRUE);
1155 if (c1 == '\n')
1156 break;
1157 c2 = edit_buffer_get_previous_byte (&edit->buffer);
1158 if (c2 == '\n')
1159 break;
1160 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1161 break;
1162 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1163 break;
1167 /* --------------------------------------------------------------------------------------------- */
1169 the start column position is not recorded, and hence does not
1170 undo as it happed. But who would notice.
1173 static void
1174 edit_do_undo (WEdit * edit)
1176 long ac;
1177 long count = 0;
1179 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1180 edit->over_col = 0;
1181 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1183 switch ((int) ac)
1185 case STACK_BOTTOM:
1186 goto done_undo;
1187 case CURS_RIGHT:
1188 edit_cursor_move (edit, 1);
1189 break;
1190 case CURS_LEFT:
1191 edit_cursor_move (edit, -1);
1192 break;
1193 case BACKSPACE:
1194 case BACKSPACE_BR:
1195 edit_backspace (edit, TRUE);
1196 break;
1197 case DELCHAR:
1198 case DELCHAR_BR:
1199 edit_delete (edit, TRUE);
1200 break;
1201 case COLUMN_ON:
1202 edit->column_highlight = 1;
1203 break;
1204 case COLUMN_OFF:
1205 edit->column_highlight = 0;
1206 break;
1207 default:
1208 break;
1210 if (ac >= 256 && ac < 512)
1211 edit_insert_ahead (edit, ac - 256);
1212 if (ac >= 0 && ac < 256)
1213 edit_insert (edit, ac);
1215 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1217 edit->mark1 = ac - MARK_1;
1218 edit->column1 =
1219 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark1),
1220 0, edit->mark1);
1222 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1224 edit->mark2 = ac - MARK_2;
1225 edit->column2 =
1226 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark2),
1227 0, edit->mark2);
1229 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1231 edit->end_mark_curs = ac - MARK_CURS;
1233 if (count++)
1234 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1237 if (edit->start_display > ac - KEY_PRESS)
1239 edit->start_line -=
1240 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1241 edit->force |= REDRAW_PAGE;
1243 else if (edit->start_display < ac - KEY_PRESS)
1245 edit->start_line +=
1246 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1247 edit->force |= REDRAW_PAGE;
1249 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1250 edit_update_curs_row (edit);
1252 done_undo:
1253 edit->undo_stack_disable = 0;
1256 /* --------------------------------------------------------------------------------------------- */
1258 static void
1259 edit_do_redo (WEdit * edit)
1261 long ac;
1262 long count = 0;
1264 if (edit->redo_stack_reset)
1265 return;
1267 edit->over_col = 0;
1268 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1270 switch ((int) ac)
1272 case STACK_BOTTOM:
1273 goto done_redo;
1274 case CURS_RIGHT:
1275 edit_cursor_move (edit, 1);
1276 break;
1277 case CURS_LEFT:
1278 edit_cursor_move (edit, -1);
1279 break;
1280 case BACKSPACE:
1281 edit_backspace (edit, TRUE);
1282 break;
1283 case DELCHAR:
1284 edit_delete (edit, TRUE);
1285 break;
1286 case COLUMN_ON:
1287 edit->column_highlight = 1;
1288 break;
1289 case COLUMN_OFF:
1290 edit->column_highlight = 0;
1291 break;
1292 default:
1293 break;
1295 if (ac >= 256 && ac < 512)
1296 edit_insert_ahead (edit, ac - 256);
1297 if (ac >= 0 && ac < 256)
1298 edit_insert (edit, ac);
1300 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1302 edit->mark1 = ac - MARK_1;
1303 edit->column1 =
1304 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark1),
1305 0, edit->mark1);
1307 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1309 edit->mark2 = ac - MARK_2;
1310 edit->column2 =
1311 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark2),
1312 0, edit->mark2);
1314 /* more than one pop usually means something big */
1315 if (count++)
1316 edit->force |= REDRAW_PAGE;
1319 if (edit->start_display > ac - KEY_PRESS)
1321 edit->start_line -=
1322 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1323 edit->force |= REDRAW_PAGE;
1325 else if (edit->start_display < ac - KEY_PRESS)
1327 edit->start_line +=
1328 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1329 edit->force |= REDRAW_PAGE;
1331 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1332 edit_update_curs_row (edit);
1334 done_redo:
1338 /* --------------------------------------------------------------------------------------------- */
1340 static void
1341 edit_group_undo (WEdit * edit)
1343 long ac = KEY_PRESS;
1344 long cur_ac = KEY_PRESS;
1345 while (ac != STACK_BOTTOM && ac == cur_ac)
1347 cur_ac = get_prev_undo_action (edit);
1348 edit_do_undo (edit);
1349 ac = get_prev_undo_action (edit);
1350 /* exit from cycle if edit_options.group_undo is not set,
1351 * and make single UNDO operation
1353 if (!edit_options.group_undo)
1354 ac = STACK_BOTTOM;
1358 /* --------------------------------------------------------------------------------------------- */
1360 static void
1361 edit_delete_to_line_end (WEdit * edit)
1363 while (edit_buffer_get_current_byte (&edit->buffer) != '\n' && edit->buffer.curs2 != 0)
1364 edit_delete (edit, TRUE);
1367 /* --------------------------------------------------------------------------------------------- */
1369 static void
1370 edit_delete_to_line_begin (WEdit * edit)
1372 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 != 0)
1373 edit_backspace (edit, TRUE);
1376 /* --------------------------------------------------------------------------------------------- */
1378 static gboolean
1379 is_aligned_on_a_tab (WEdit * edit)
1381 long curs_col;
1383 edit_update_curs_col (edit);
1384 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1385 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1388 /* --------------------------------------------------------------------------------------------- */
1390 static gboolean
1391 right_of_four_spaces (WEdit * edit)
1393 int i, ch = 0;
1395 for (i = 1; i <= HALF_TAB_SIZE; i++)
1396 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - i);
1398 return (ch == ' ' && is_aligned_on_a_tab (edit));
1401 /* --------------------------------------------------------------------------------------------- */
1403 static gboolean
1404 left_of_four_spaces (WEdit * edit)
1406 int i, ch = 0;
1408 for (i = 0; i < HALF_TAB_SIZE; i++)
1409 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 + i);
1411 return (ch == ' ' && is_aligned_on_a_tab (edit));
1414 /* --------------------------------------------------------------------------------------------- */
1416 static void
1417 edit_auto_indent (WEdit * edit)
1419 off_t p;
1421 p = edit->buffer.curs1;
1422 /* use the previous line as a template */
1423 p = edit_buffer_get_backward_offset (&edit->buffer, p, 1);
1424 /* copy the leading whitespace of the line */
1425 while (TRUE)
1426 { /* no range check - the line _is_ \n-terminated */
1427 char c;
1429 c = edit_buffer_get_byte (&edit->buffer, p++);
1430 if (!whitespace (c))
1431 break;
1432 edit_insert (edit, c);
1436 /* --------------------------------------------------------------------------------------------- */
1438 static inline void
1439 edit_double_newline (WEdit * edit)
1441 edit_insert (edit, '\n');
1442 if (edit_buffer_get_current_byte (&edit->buffer) == '\n'
1443 || edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - 2) == '\n')
1444 return;
1445 edit->force |= REDRAW_PAGE;
1446 edit_insert (edit, '\n');
1449 /* --------------------------------------------------------------------------------------------- */
1451 static void
1452 insert_spaces_tab (WEdit * edit, gboolean half)
1454 long i;
1456 edit_update_curs_col (edit);
1457 i = TAB_SIZE * space_width;
1458 if (half)
1459 i /= 2;
1460 if (i != 0)
1461 for (i = ((edit->curs_col / i) + 1) * i - edit->curs_col; i > 0; i -= space_width)
1462 edit_insert (edit, ' ');
1465 /* --------------------------------------------------------------------------------------------- */
1467 static inline void
1468 edit_tab_cmd (WEdit * edit)
1470 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer))
1472 /* insert a half tab (usually four spaces) unless there is a
1473 half tab already behind, then delete it and insert a
1474 full tab. */
1475 if (edit_options.fill_tabs_with_spaces || !right_of_four_spaces (edit))
1476 insert_spaces_tab (edit, TRUE);
1477 else
1479 int i;
1481 for (i = 1; i <= HALF_TAB_SIZE; i++)
1482 edit_backspace (edit, TRUE);
1483 edit_insert (edit, '\t');
1486 else if (edit_options.fill_tabs_with_spaces)
1487 insert_spaces_tab (edit, FALSE);
1488 else
1489 edit_insert (edit, '\t');
1492 /* --------------------------------------------------------------------------------------------- */
1494 static void
1495 check_and_wrap_line (WEdit * edit)
1497 off_t curs;
1499 if (!edit_options.typewriter_wrap)
1500 return;
1501 edit_update_curs_col (edit);
1502 if (edit->curs_col < edit_options.word_wrap_line_length)
1503 return;
1504 curs = edit->buffer.curs1;
1505 while (TRUE)
1507 int c;
1509 curs--;
1510 c = edit_buffer_get_byte (&edit->buffer, curs);
1511 if (c == '\n' || curs <= 0)
1513 edit_insert (edit, '\n');
1514 return;
1516 if (whitespace (c))
1518 off_t current = edit->buffer.curs1;
1519 edit_cursor_move (edit, curs - edit->buffer.curs1 + 1);
1520 edit_insert (edit, '\n');
1521 edit_cursor_move (edit, current - edit->buffer.curs1 + 1);
1522 return;
1527 /* --------------------------------------------------------------------------------------------- */
1528 /** this find the matching bracket in either direction, and sets edit->bracket
1530 * @param edit editor object
1531 * @param in_screen search only on the current screen
1532 * @param furthest_bracket_search count of the bytes for search
1534 * @return position of the found bracket (-1 if no match)
1537 static off_t
1538 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1540 const char *const b = "{}{[][()(", *p;
1541 int i = 1, inc = -1, c, d, n = 0;
1542 unsigned long j = 0;
1543 off_t q;
1545 edit_update_curs_row (edit);
1546 c = edit_buffer_get_current_byte (&edit->buffer);
1547 p = strchr (b, c);
1548 /* not on a bracket at all */
1549 if (p == NULL || *p == '\0')
1550 return -1;
1551 /* the matching bracket */
1552 d = p[1];
1553 /* going left or right? */
1554 if (strchr ("{[(", c) != NULL)
1555 inc = 1;
1556 /* no limit */
1557 if (furthest_bracket_search == 0)
1558 furthest_bracket_search--; /* ULONG_MAX */
1559 for (q = edit->buffer.curs1 + inc;; q += inc)
1561 int a;
1563 /* out of buffer? */
1564 if (q >= edit->buffer.size || q < 0)
1565 break;
1566 a = edit_buffer_get_byte (&edit->buffer, q);
1567 /* don't want to eat CPU */
1568 if (j++ > furthest_bracket_search)
1569 break;
1570 /* out of screen? */
1571 if (in_screen)
1573 if (q < edit->start_display)
1574 break;
1575 /* count lines if searching downward */
1576 if (inc > 0 && a == '\n')
1577 if (n++ >= WIDGET (edit)->rect.lines - edit->curs_row) /* out of screen */
1578 break;
1580 /* count bracket depth */
1581 i += (a == c) - (a == d);
1582 /* return if bracket depth is zero */
1583 if (i == 0)
1584 return q;
1586 /* no match */
1587 return -1;
1590 /* --------------------------------------------------------------------------------------------- */
1592 static inline void
1593 edit_goto_matching_bracket (WEdit * edit)
1595 off_t q;
1597 q = edit_get_bracket (edit, 0, 0);
1598 if (q >= 0)
1600 edit->bracket = edit->buffer.curs1;
1601 edit->force |= REDRAW_PAGE;
1602 edit_cursor_move (edit, q - edit->buffer.curs1);
1606 /* --------------------------------------------------------------------------------------------- */
1608 static void
1609 edit_move_block_to_right (WEdit * edit)
1611 off_t start_mark, end_mark;
1612 long cur_bol, start_bol;
1614 if (!eval_marks (edit, &start_mark, &end_mark))
1615 return;
1617 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1618 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1622 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1623 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
1625 if (edit_options.fill_tabs_with_spaces)
1626 insert_spaces_tab (edit, edit_options.fake_half_tabs);
1627 else
1628 edit_insert (edit, '\t');
1629 edit_cursor_move (edit,
1630 edit_buffer_get_bol (&edit->buffer, cur_bol) - edit->buffer.curs1);
1633 if (cur_bol == 0)
1634 break;
1636 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1638 while (cur_bol >= start_bol);
1640 edit->force |= REDRAW_PAGE;
1643 /* --------------------------------------------------------------------------------------------- */
1645 static void
1646 edit_move_block_to_left (WEdit * edit)
1648 off_t start_mark, end_mark;
1649 off_t cur_bol, start_bol;
1651 if (!eval_marks (edit, &start_mark, &end_mark))
1652 return;
1654 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1655 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1659 int del_tab_width;
1660 int next_char;
1662 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1664 del_tab_width = edit_options.fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE;
1666 next_char = edit_buffer_get_current_byte (&edit->buffer);
1667 if (next_char == '\t')
1668 edit_delete (edit, TRUE);
1669 else if (next_char == ' ')
1671 int i;
1673 for (i = 0; i < del_tab_width; i++)
1675 if (next_char == ' ')
1676 edit_delete (edit, TRUE);
1677 next_char = edit_buffer_get_current_byte (&edit->buffer);
1681 if (cur_bol == 0)
1682 break;
1684 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1686 while (cur_bol >= start_bol);
1688 edit->force |= REDRAW_PAGE;
1691 /* --------------------------------------------------------------------------------------------- */
1693 * prints at the cursor
1694 * @return number of chars printed
1697 static size_t
1698 edit_print_string (WEdit * e, const char *s)
1700 size_t i;
1702 for (i = 0; s[i] != '\0'; i++)
1703 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i]);
1704 e->force |= REDRAW_COMPLETELY;
1705 edit_update_screen (e);
1706 return i;
1709 /* --------------------------------------------------------------------------------------------- */
1711 static off_t
1712 edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * end_pos,
1713 long *col1, long *col2)
1715 off_t cursor;
1716 long col;
1717 off_t blocklen = -1, width = 0;
1718 unsigned char *data;
1720 cursor = edit->buffer.curs1;
1721 col = edit_get_col (edit);
1722 data = g_malloc0 (TEMP_BUF_LEN);
1724 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1726 off_t i;
1727 char *pn;
1729 pn = strchr ((char *) data, '\n');
1730 width = pn == NULL ? blocklen : pn - (char *) data;
1732 for (i = 0; i < blocklen; i++)
1734 if (data[i] != '\n')
1735 edit_insert (edit, data[i]);
1736 else
1737 { /* fill in and move to next line */
1738 long l;
1739 off_t p;
1741 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1742 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1743 edit_insert (edit, ' ');
1745 for (p = edit->buffer.curs1;; p++)
1747 if (p == edit->buffer.size)
1749 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1750 edit_insert_ahead (edit, '\n');
1751 p++;
1752 break;
1754 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1756 p++;
1757 break;
1761 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1763 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1764 edit_insert (edit, ' ');
1768 *col1 = col;
1769 *col2 = col + width;
1770 *start_pos = cursor;
1771 *end_pos = edit->buffer.curs1;
1772 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1773 g_free (data);
1775 return blocklen;
1778 /* --------------------------------------------------------------------------------------------- */
1779 /*** public functions ****************************************************************************/
1780 /* --------------------------------------------------------------------------------------------- */
1782 /** User edit menu, like user menu (F2) but only in editor. */
1784 void
1785 edit_user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1787 char *block_file;
1788 gboolean mark;
1789 off_t curs;
1790 off_t start_mark, end_mark;
1791 struct stat status;
1792 vfs_path_t *block_file_vpath;
1794 block_file = mc_config_get_full_path (EDIT_HOME_BLOCK_FILE);
1795 block_file_vpath = vfs_path_from_str (block_file);
1796 curs = edit->buffer.curs1;
1797 mark = eval_marks (edit, &start_mark, &end_mark);
1798 if (mark)
1799 edit_save_block (edit, block_file, start_mark, end_mark);
1801 /* run shell scripts from menu */
1802 if (user_menu_cmd (CONST_WIDGET (edit), menu_file, selected_entry)
1803 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1805 int rc = 0;
1806 FILE *fd;
1808 /* i.e. we have marked block */
1809 if (mark)
1810 rc = edit_block_delete_cmd (edit);
1812 if (rc == 0)
1814 off_t ins_len;
1816 ins_len = edit_insert_file (edit, block_file_vpath);
1817 if (mark && ins_len > 0)
1818 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1820 /* truncate block file */
1821 fd = fopen (block_file, "w");
1822 if (fd != NULL)
1823 fclose (fd);
1825 g_free (block_file);
1826 vfs_path_free (block_file_vpath, TRUE);
1828 edit_cursor_move (edit, curs - edit->buffer.curs1);
1829 edit->force |= REDRAW_PAGE;
1830 widget_draw (WIDGET (edit));
1833 /* --------------------------------------------------------------------------------------------- */
1835 char *
1836 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1838 int i;
1839 const char *write_name;
1840 char *write_name_quoted;
1841 char *p = NULL;
1843 i = edit_find_filter (filename_vpath);
1844 if (i < 0)
1845 return NULL;
1847 write_name = vfs_path_get_last_path_str (write_name_vpath);
1848 write_name_quoted = name_quote (write_name, FALSE);
1849 if (write_name_quoted != NULL)
1851 p = g_strdup_printf (all_filters[i].write, write_name_quoted);
1852 g_free (write_name_quoted);
1854 return p;
1857 /* --------------------------------------------------------------------------------------------- */
1859 * @param edit editor object
1860 * @param f value of stream file
1861 * @return the length of the file
1864 off_t
1865 edit_write_stream (WEdit * edit, FILE * f)
1867 long i;
1869 if (edit->lb == LB_ASIS)
1871 for (i = 0; i < edit->buffer.size; i++)
1872 if (fputc (edit_buffer_get_byte (&edit->buffer, i), f) < 0)
1873 break;
1874 return i;
1877 /* change line breaks */
1878 for (i = 0; i < edit->buffer.size; i++)
1880 unsigned char c;
1882 c = edit_buffer_get_byte (&edit->buffer, i);
1883 if (!(c == '\n' || c == '\r'))
1885 /* not line break */
1886 if (fputc (c, f) < 0)
1887 return i;
1889 else
1890 { /* (c == '\n' || c == '\r') */
1891 unsigned char c1;
1893 c1 = edit_buffer_get_byte (&edit->buffer, i + 1); /* next char */
1895 switch (edit->lb)
1897 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1898 /* put one line break unconditionally */
1899 if (fputc ('\n', f) < 0)
1900 return i;
1902 i++; /* 2 chars are processed */
1904 if (c == '\r' && c1 == '\n')
1905 /* Windows line break; go to the next char */
1906 break;
1908 if (c == '\r' && c1 == '\r')
1910 /* two Macintosh line breaks; put second line break */
1911 if (fputc ('\n', f) < 0)
1912 return i;
1913 break;
1916 if (fputc (c1, f) < 0)
1917 return i;
1918 break;
1920 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1921 /* put one line break unconditionally */
1922 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1923 return i;
1925 if (c == '\r' && c1 == '\n')
1926 /* Windows line break; go to the next char */
1927 i++;
1928 break;
1930 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1931 /* put one line break unconditionally */
1932 if (fputc ('\r', f) < 0)
1933 return i;
1935 i++; /* 2 chars are processed */
1937 if (c == '\r' && c1 == '\n')
1938 /* Windows line break; go to the next char */
1939 break;
1941 if (c == '\n' && c1 == '\n')
1943 /* two Windows line breaks; put second line break */
1944 if (fputc ('\r', f) < 0)
1945 return i;
1946 break;
1949 if (fputc (c1, f) < 0)
1950 return i;
1951 break;
1952 case LB_ASIS: /* default without changes */
1953 default:
1954 break;
1959 return edit->buffer.size;
1962 /* --------------------------------------------------------------------------------------------- */
1964 gboolean
1965 is_break_char (char c)
1967 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1970 /* --------------------------------------------------------------------------------------------- */
1971 /** inserts a file at the cursor, returns count of inserted bytes on success */
1973 off_t
1974 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
1976 char *p;
1977 off_t current;
1978 off_t ins_len = 0;
1980 p = edit_get_filter (filename_vpath);
1981 current = edit->buffer.curs1;
1983 if (p != NULL)
1985 FILE *f;
1987 f = (FILE *) popen (p, "r");
1988 if (f != NULL)
1990 edit_insert_stream (edit, f);
1992 /* Place cursor at the end of text selection */
1993 if (!edit_options.cursor_after_inserted_block)
1995 ins_len = edit->buffer.curs1 - current;
1996 edit_cursor_move (edit, -ins_len);
1998 if (pclose (f) > 0)
2000 char *errmsg;
2002 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2003 edit_error_dialog (_("Error"), errmsg);
2004 g_free (errmsg);
2005 ins_len = -1;
2008 else
2010 char *errmsg;
2012 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2013 edit_error_dialog (_("Error"), errmsg);
2014 g_free (errmsg);
2015 ins_len = -1;
2017 g_free (p);
2019 else
2021 int file;
2022 off_t blocklen;
2023 gboolean vertical_insertion = FALSE;
2024 char *buf;
2026 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2027 if (file == -1)
2028 return -1;
2030 buf = g_malloc0 (TEMP_BUF_LEN);
2031 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2032 if (blocklen > 0)
2034 /* if contain signature VERTICAL_MAGIC then it vertical block */
2035 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2036 vertical_insertion = TRUE;
2037 else
2038 mc_lseek (file, 0, SEEK_SET);
2041 if (vertical_insertion)
2043 off_t mark1, mark2;
2044 long c1, c2;
2046 blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2047 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2049 /* highlight inserted text then not persistent blocks */
2050 if (!edit_options.persistent_selections && edit->modified)
2052 if (!edit->column_highlight)
2053 edit_push_undo_action (edit, COLUMN_OFF);
2054 edit->column_highlight = 1;
2057 else
2059 off_t i;
2061 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2063 for (i = 0; i < blocklen; i++)
2064 edit_insert (edit, buf[i]);
2066 /* highlight inserted text then not persistent blocks */
2067 if (!edit_options.persistent_selections && edit->modified)
2069 edit_set_markers (edit, edit->buffer.curs1, current, 0, 0);
2070 if (edit->column_highlight)
2071 edit_push_undo_action (edit, COLUMN_ON);
2072 edit->column_highlight = 0;
2075 /* Place cursor at the end of text selection */
2076 if (!edit_options.cursor_after_inserted_block)
2078 ins_len = edit->buffer.curs1 - current;
2079 edit_cursor_move (edit, -ins_len);
2083 edit->force |= REDRAW_PAGE;
2084 g_free (buf);
2085 mc_close (file);
2086 if (blocklen != 0)
2087 ins_len = 0;
2090 return ins_len;
2093 /* --------------------------------------------------------------------------------------------- */
2095 * Fill in the edit structure. Return NULL on failure. Pass edit as
2096 * NULL to allocate a new structure.
2098 * If line is 0, try to restore saved position. Otherwise put the
2099 * cursor on that line and show it in the middle of the screen.
2102 WEdit *
2103 edit_init (WEdit * edit, const WRect * r, const vfs_path_t * filename_vpath, long line)
2105 gboolean to_free = FALSE;
2107 auto_syntax = TRUE; /* Resetting to auto on every invocation */
2108 edit_options.line_state_width = edit_options.line_state ? LINE_STATE_WIDTH : 0;
2110 if (edit != NULL)
2112 gboolean fullscreen;
2113 WRect loc_prev;
2115 /* save some widget parameters */
2116 fullscreen = edit->fullscreen;
2117 loc_prev = edit->loc_prev;
2119 edit_purge_widget (edit);
2121 /* restore saved parameters */
2122 edit->fullscreen = fullscreen;
2123 edit->loc_prev = loc_prev;
2125 else
2127 Widget *w;
2129 edit = g_malloc0 (sizeof (WEdit));
2130 to_free = TRUE;
2132 w = WIDGET (edit);
2133 widget_init (w, r, NULL, NULL);
2134 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT | WOP_WANT_CURSOR;
2135 w->keymap = editor_map;
2136 w->ext_keymap = editor_x_map;
2137 edit->fullscreen = TRUE;
2138 edit_save_size (edit);
2141 edit->drag_state = MCEDIT_DRAG_NONE;
2143 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2144 edit->stat1.st_uid = getuid ();
2145 edit->stat1.st_gid = getgid ();
2146 edit->stat1.st_mtime = 0;
2148 edit->over_col = 0;
2149 edit->bracket = -1;
2150 edit->last_bracket = -1;
2151 edit->force |= REDRAW_PAGE;
2153 /* set file name before load file */
2154 edit_set_filename (edit, filename_vpath);
2156 edit->undo_stack_size = START_STACK_SIZE;
2157 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2158 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2160 edit->redo_stack_size = START_STACK_SIZE;
2161 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2162 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2164 #ifdef HAVE_CHARSET
2165 edit->utf8 = FALSE;
2166 edit->converter = str_cnv_from_term;
2167 edit_set_codeset (edit);
2168 #endif
2170 if (!edit_load_file (edit))
2172 /* edit_load_file already gives an error message */
2173 if (to_free)
2174 g_free (edit);
2175 return NULL;
2178 edit->loading_done = 1;
2179 edit->modified = 0;
2180 edit->locked = 0;
2181 edit_load_syntax (edit, NULL, NULL);
2182 edit_get_syntax_color (edit, -1);
2184 /* load saved cursor position and/or boolmarks */
2185 if ((line == 0) && edit_options.save_position)
2186 edit_load_position (edit, TRUE);
2187 else
2189 edit_load_position (edit, FALSE);
2190 if (line <= 0)
2191 line = 1;
2192 edit_move_display (edit, line - 1);
2193 edit_move_to_line (edit, line - 1);
2196 edit_load_macro_cmd (edit);
2198 return edit;
2201 /* --------------------------------------------------------------------------------------------- */
2203 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2204 gboolean
2205 edit_clean (WEdit * edit)
2207 if (edit == NULL)
2208 return FALSE;
2210 /* a stale lock, remove it */
2211 if (edit->locked)
2212 (void) unlock_file (edit->filename_vpath);
2214 /* save cursor position */
2215 if (edit_options.save_position)
2216 edit_save_position (edit);
2217 else if (edit->serialized_bookmarks != NULL)
2218 g_array_free (edit->serialized_bookmarks, TRUE);
2220 /* File specified on the mcedit command line and never saved */
2221 if (edit->delete_file)
2222 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2224 edit_free_syntax_rules (edit);
2225 book_mark_flush (edit, -1);
2227 edit_buffer_clean (&edit->buffer);
2229 g_free (edit->undo_stack);
2230 g_free (edit->redo_stack);
2231 vfs_path_free (edit->filename_vpath, TRUE);
2232 vfs_path_free (edit->dir_vpath, TRUE);
2233 edit_search_deinit (edit);
2235 #ifdef HAVE_CHARSET
2236 if (edit->converter != str_cnv_from_term)
2237 str_close_conv (edit->converter);
2238 #endif
2240 edit_purge_widget (edit);
2242 return TRUE;
2245 /* --------------------------------------------------------------------------------------------- */
2248 * Load a new file into the editor and set line. If it fails, preserve the old file.
2249 * To do it, allocate a new widget, initialize it and, if the new file
2250 * was loaded, copy the data to the old widget.
2252 * @return TRUE on success, FALSE on failure.
2254 gboolean
2255 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2257 Widget *w = WIDGET (edit);
2258 WEdit *e;
2260 e = g_malloc0 (sizeof (WEdit));
2261 *WIDGET (e) = *w;
2262 /* save some widget parameters */
2263 e->fullscreen = edit->fullscreen;
2264 e->loc_prev = edit->loc_prev;
2266 if (edit_init (e, &w->rect, filename_vpath, line) == NULL)
2268 g_free (e);
2269 return FALSE;
2272 edit_clean (edit);
2273 memcpy (edit, e, sizeof (*edit));
2274 g_free (e);
2276 return TRUE;
2279 /* --------------------------------------------------------------------------------------------- */
2281 #ifdef HAVE_CHARSET
2282 void
2283 edit_set_codeset (WEdit * edit)
2285 const char *cp_id;
2287 cp_id =
2288 get_codepage_id (mc_global.source_codepage >=
2289 0 ? mc_global.source_codepage : mc_global.display_codepage);
2291 if (cp_id != NULL)
2293 GIConv conv;
2294 conv = str_crt_conv_from (cp_id);
2295 if (conv != INVALID_CONV)
2297 if (edit->converter != str_cnv_from_term)
2298 str_close_conv (edit->converter);
2299 edit->converter = conv;
2303 if (cp_id != NULL)
2304 edit->utf8 = str_isutf8 (cp_id);
2306 #endif
2308 /* --------------------------------------------------------------------------------------------- */
2311 * Recording stack for undo:
2312 * The following is an implementation of a compressed stack. Identical
2313 * pushes are recorded by a negative prefix indicating the number of times the
2314 * same char was pushed. This saves space for repeated curs-left or curs-right
2315 * delete etc.
2317 * eg:
2319 * pushed: stored:
2322 * b a
2323 * b -3
2324 * b b
2325 * c --> -4
2326 * c c
2327 * c d
2331 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2332 * 256-512 is an insert ahead (from a delete), If it is between 600 and 700 it is one
2333 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2334 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2335 * position.
2337 * The only way the cursor moves or the buffer is changed is through the routines:
2338 * insert, backspace, insert_ahead, delete, and cursor_move.
2339 * These record the reverse undo movements onto the stack each time they are
2340 * called.
2342 * Each key press results in a set of actions (insert; delete ...). So each time
2343 * a key is pressed the current position of start_display is pushed as
2344 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2345 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2346 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2350 * @param edit editor object
2351 * @param c code of the action
2354 void
2355 edit_push_undo_action (WEdit * edit, long c)
2357 unsigned long sp = edit->undo_stack_pointer;
2358 unsigned long spm1;
2360 /* first enlarge the stack if necessary */
2361 if (sp > edit->undo_stack_size - 10)
2362 { /* say */
2363 if (max_undo < 256)
2364 max_undo = 256;
2365 if (edit->undo_stack_size < (unsigned long) max_undo)
2367 long *t;
2369 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2370 if (t != NULL)
2372 edit->undo_stack = t;
2373 edit->undo_stack_size <<= 1;
2374 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2378 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2379 if (edit->undo_stack_disable)
2381 edit_push_redo_action (edit, KEY_PRESS);
2382 edit_push_redo_action (edit, c);
2383 return;
2386 if (edit->redo_stack_reset)
2387 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2389 if (edit->undo_stack_bottom != sp
2390 && spm1 != edit->undo_stack_bottom
2391 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2393 long d;
2394 if (edit->undo_stack[spm1] < 0)
2396 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2397 if (d == c && edit->undo_stack[spm1] > -1000000000)
2399 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2400 edit->undo_stack[spm1]--;
2401 return;
2404 else
2406 d = edit->undo_stack[spm1];
2407 if (d == c)
2409 if (c >= KEY_PRESS)
2410 return; /* --> no need to push multiple do-nothings */
2411 edit->undo_stack[sp] = -2;
2412 goto check_bottom;
2416 edit->undo_stack[sp] = c;
2418 check_bottom:
2419 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2421 /* if the sp wraps round and catches the undo_stack_bottom then erase
2422 * the first set of actions on the stack to make space - by moving
2423 * undo_stack_bottom forward one "key press" */
2424 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2425 if ((unsigned long) c == edit->undo_stack_bottom ||
2426 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2429 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2431 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2432 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2434 /*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: */
2435 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2436 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2438 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2442 /* --------------------------------------------------------------------------------------------- */
2444 void
2445 edit_push_redo_action (WEdit * edit, long c)
2447 unsigned long sp = edit->redo_stack_pointer;
2448 unsigned long spm1;
2449 /* first enlarge the stack if necessary */
2450 if (sp > edit->redo_stack_size - 10)
2451 { /* say */
2452 if (max_undo < 256)
2453 max_undo = 256;
2454 if (edit->redo_stack_size < (unsigned long) max_undo)
2456 long *t;
2458 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2459 if (t != NULL)
2461 edit->redo_stack = t;
2462 edit->redo_stack_size <<= 1;
2463 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2467 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2469 if (edit->redo_stack_bottom != sp
2470 && spm1 != edit->redo_stack_bottom
2471 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2473 long d;
2474 if (edit->redo_stack[spm1] < 0)
2476 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2477 if (d == c && edit->redo_stack[spm1] > -1000000000)
2479 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2480 edit->redo_stack[spm1]--;
2481 return;
2484 else
2486 d = edit->redo_stack[spm1];
2487 if (d == c)
2489 if (c >= KEY_PRESS)
2490 return; /* --> no need to push multiple do-nothings */
2491 edit->redo_stack[sp] = -2;
2492 goto redo_check_bottom;
2496 edit->redo_stack[sp] = c;
2498 redo_check_bottom:
2499 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2501 /* if the sp wraps round and catches the redo_stack_bottom then erase
2502 * the first set of actions on the stack to make space - by moving
2503 * redo_stack_bottom forward one "key press" */
2504 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2505 if ((unsigned long) c == edit->redo_stack_bottom ||
2506 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2509 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2511 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2512 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2515 * If a single key produced enough pushes to wrap all the way round then
2516 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2517 * The stack is then initialised:
2520 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2521 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2522 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2525 /* --------------------------------------------------------------------------------------------- */
2527 Basic low level single character buffer alterations and movements at the cursor.
2530 void
2531 edit_insert (WEdit * edit, int c)
2533 /* first we must update the position of the display window */
2534 if (edit->buffer.curs1 < edit->start_display)
2536 edit->start_display++;
2537 if (c == '\n')
2538 edit->start_line++;
2541 /* Mark file as modified, unless the file hasn't been fully loaded */
2542 if (edit->loading_done)
2543 edit_modification (edit);
2545 /* now we must update some info on the file and check if a redraw is required */
2546 if (c == '\n')
2548 book_mark_inc (edit, edit->buffer.curs_line);
2549 edit->buffer.curs_line++;
2550 edit->buffer.lines++;
2551 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2554 /* save the reverse command onto the undo stack */
2555 /* ordinary char and not space */
2556 if (c > 32)
2557 edit_push_undo_action (edit, BACKSPACE);
2558 else
2559 edit_push_undo_action (edit, BACKSPACE_BR);
2560 /* update markers */
2561 edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2562 edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2563 edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2565 edit_buffer_insert (&edit->buffer, c);
2568 /* --------------------------------------------------------------------------------------------- */
2569 /** same as edit_insert and move left */
2571 void
2572 edit_insert_ahead (WEdit * edit, int c)
2574 if (edit->buffer.curs1 < edit->start_display)
2576 edit->start_display++;
2577 if (c == '\n')
2578 edit->start_line++;
2580 edit_modification (edit);
2581 if (c == '\n')
2583 book_mark_inc (edit, edit->buffer.curs_line);
2584 edit->buffer.lines++;
2585 edit->force |= REDRAW_AFTER_CURSOR;
2587 /* ordinary char and not space */
2588 if (c > 32)
2589 edit_push_undo_action (edit, DELCHAR);
2590 else
2591 edit_push_undo_action (edit, DELCHAR_BR);
2593 edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2594 edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2595 edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2597 edit_buffer_insert_ahead (&edit->buffer, c);
2600 /* --------------------------------------------------------------------------------------------- */
2602 void
2603 edit_insert_over (WEdit * edit)
2605 long i;
2607 for (i = 0; i < edit->over_col; i++)
2608 edit_insert (edit, ' ');
2610 edit->over_col = 0;
2613 /* --------------------------------------------------------------------------------------------- */
2616 edit_delete (WEdit * edit, gboolean byte_delete)
2618 int p = 0;
2619 int char_length = 1;
2620 int i;
2622 if (edit->buffer.curs2 == 0)
2623 return 0;
2625 #ifdef HAVE_CHARSET
2626 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2627 if (edit->utf8 && !byte_delete)
2629 edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2630 if (char_length < 1)
2631 char_length = 1;
2633 #else
2634 (void) byte_delete;
2635 #endif
2637 if (edit->mark2 != edit->mark1)
2638 edit_push_markers (edit);
2640 for (i = 1; i <= char_length; i++)
2642 if (edit->mark1 > edit->buffer.curs1)
2644 edit->mark1--;
2645 edit->end_mark_curs--;
2647 if (edit->mark2 > edit->buffer.curs1)
2648 edit->mark2--;
2649 if (edit->last_get_rule > edit->buffer.curs1)
2650 edit->last_get_rule--;
2652 p = edit_buffer_delete (&edit->buffer);
2654 edit_push_undo_action (edit, p + 256);
2657 edit_modification (edit);
2658 if (p == '\n')
2660 book_mark_dec (edit, edit->buffer.curs_line);
2661 edit->buffer.lines--;
2662 edit->force |= REDRAW_AFTER_CURSOR;
2664 if (edit->buffer.curs1 < edit->start_display)
2666 edit->start_display--;
2667 if (p == '\n')
2668 edit->start_line--;
2671 return p;
2674 /* --------------------------------------------------------------------------------------------- */
2677 edit_backspace (WEdit * edit, gboolean byte_delete)
2679 int p = 0;
2680 int char_length = 1;
2681 int i;
2683 if (edit->buffer.curs1 == 0)
2684 return 0;
2686 if (edit->mark2 != edit->mark1)
2687 edit_push_markers (edit);
2689 #ifdef HAVE_CHARSET
2690 if (edit->utf8 && !byte_delete)
2692 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2693 if (char_length < 1)
2694 char_length = 1;
2696 #else
2697 (void) byte_delete;
2698 #endif
2700 for (i = 1; i <= char_length; i++)
2702 if (edit->mark1 >= edit->buffer.curs1)
2704 edit->mark1--;
2705 edit->end_mark_curs--;
2707 if (edit->mark2 >= edit->buffer.curs1)
2708 edit->mark2--;
2709 if (edit->last_get_rule >= edit->buffer.curs1)
2710 edit->last_get_rule--;
2712 p = edit_buffer_backspace (&edit->buffer);
2714 edit_push_undo_action (edit, p);
2716 edit_modification (edit);
2717 if (p == '\n')
2719 book_mark_dec (edit, edit->buffer.curs_line);
2720 edit->buffer.curs_line--;
2721 edit->buffer.lines--;
2722 edit->force |= REDRAW_AFTER_CURSOR;
2725 if (edit->buffer.curs1 < edit->start_display)
2727 edit->start_display--;
2728 if (p == '\n')
2729 edit->start_line--;
2732 return p;
2735 /* --------------------------------------------------------------------------------------------- */
2736 /** moves the cursor right or left: increment positive or negative respectively */
2738 void
2739 edit_cursor_move (WEdit * edit, off_t increment)
2741 if (increment < 0)
2743 for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2745 int c;
2747 edit_push_undo_action (edit, CURS_RIGHT);
2749 c = edit_buffer_get_previous_byte (&edit->buffer);
2750 edit_buffer_insert_ahead (&edit->buffer, c);
2751 c = edit_buffer_backspace (&edit->buffer);
2752 if (c == '\n')
2754 edit->buffer.curs_line--;
2755 edit->force |= REDRAW_LINE_BELOW;
2759 else
2761 for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2763 int c;
2765 edit_push_undo_action (edit, CURS_LEFT);
2767 c = edit_buffer_get_current_byte (&edit->buffer);
2768 edit_buffer_insert (&edit->buffer, c);
2769 c = edit_buffer_delete (&edit->buffer);
2770 if (c == '\n')
2772 edit->buffer.curs_line++;
2773 edit->force |= REDRAW_LINE_ABOVE;
2779 /* --------------------------------------------------------------------------------------------- */
2780 /* If cols is zero this returns the count of columns from current to upto. */
2781 /* If upto is zero returns index of cols across from current. */
2783 off_t
2784 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
2786 off_t p, q;
2787 long col;
2789 if (upto != 0)
2791 q = upto;
2792 cols = -10;
2794 else
2795 q = edit->buffer.size + 2;
2797 for (col = 0, p = current; p < q; p++)
2799 int c, orig_c;
2801 if (cols != -10)
2803 if (col == cols)
2804 return p;
2805 if (col > cols)
2806 return p - 1;
2809 orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2811 #ifdef HAVE_CHARSET
2812 if (edit->utf8)
2814 int utf_ch;
2815 int char_length = 1;
2817 utf_ch = edit_buffer_get_utf (&edit->buffer, p, &char_length);
2818 if (mc_global.utf8_display)
2820 if (char_length > 1)
2821 col -= char_length - 1;
2822 if (g_unichar_iswide (utf_ch))
2823 col++;
2825 else if (char_length > 1 && g_unichar_isprint (utf_ch))
2826 col -= char_length - 1;
2829 c = convert_to_display_c (c);
2830 #endif
2832 if (c == '\n')
2833 return (upto != 0 ? (off_t) col : p);
2834 if (c == '\t')
2835 col += TAB_SIZE - col % TAB_SIZE;
2836 else if ((c < 32 || c == 127) && (orig_c == c
2837 #ifdef HAVE_CHARSET
2838 || (!mc_global.utf8_display && !edit->utf8)
2839 #endif
2841 /* '\r' is shown as ^M, so we must advance 2 characters */
2842 /* Caret notation for control characters */
2843 col += 2;
2844 else
2845 col++;
2847 return (off_t) col;
2850 /* --------------------------------------------------------------------------------------------- */
2851 /** returns the current offset of the cursor from the beginning of a file */
2853 off_t
2854 edit_get_cursor_offset (const WEdit * edit)
2856 return edit->buffer.curs1;
2859 /* --------------------------------------------------------------------------------------------- */
2860 /** returns the current column position of the cursor */
2862 long
2863 edit_get_col (const WEdit * edit)
2865 return (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2866 edit->buffer.curs1);
2869 /* --------------------------------------------------------------------------------------------- */
2870 /* Scrolling functions */
2871 /* --------------------------------------------------------------------------------------------- */
2873 void
2874 edit_update_curs_row (WEdit * edit)
2876 edit->curs_row = edit->buffer.curs_line - edit->start_line;
2879 /* --------------------------------------------------------------------------------------------- */
2881 void
2882 edit_update_curs_col (WEdit * edit)
2884 edit->curs_col = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer),
2885 0, edit->buffer.curs1);
2888 /* --------------------------------------------------------------------------------------------- */
2890 long
2891 edit_get_curs_col (const WEdit * edit)
2893 return edit->curs_col;
2896 /* --------------------------------------------------------------------------------------------- */
2897 /** moves the display start position up by i lines */
2899 void
2900 edit_scroll_upward (WEdit * edit, long i)
2902 long lines_above = edit->start_line;
2904 if (i > lines_above)
2905 i = lines_above;
2906 if (i != 0)
2908 edit->start_line -= i;
2909 edit->start_display =
2910 edit_buffer_get_backward_offset (&edit->buffer, edit->start_display, i);
2911 edit->force |= REDRAW_PAGE;
2912 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2914 edit_update_curs_row (edit);
2918 /* --------------------------------------------------------------------------------------------- */
2920 void
2921 edit_scroll_downward (WEdit * edit, long i)
2923 long lines_below;
2925 lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->rect.lines - 1);
2926 if (lines_below > 0)
2928 if (i > lines_below)
2929 i = lines_below;
2930 edit->start_line += i;
2931 edit->start_display =
2932 edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, i, 0);
2933 edit->force |= REDRAW_PAGE;
2934 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2936 edit_update_curs_row (edit);
2939 /* --------------------------------------------------------------------------------------------- */
2941 void
2942 edit_scroll_right (WEdit * edit, long i)
2944 edit->force |= REDRAW_PAGE;
2945 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2946 edit->start_col -= i;
2949 /* --------------------------------------------------------------------------------------------- */
2951 void
2952 edit_scroll_left (WEdit * edit, long i)
2954 if (edit->start_col)
2956 edit->start_col += i;
2957 if (edit->start_col > 0)
2958 edit->start_col = 0;
2959 edit->force |= REDRAW_PAGE;
2960 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2964 /* --------------------------------------------------------------------------------------------- */
2965 /* high level cursor movement commands */
2966 /* --------------------------------------------------------------------------------------------- */
2968 void
2969 edit_move_to_prev_col (WEdit * edit, off_t p)
2971 long prev = edit->prev_col;
2972 long over = edit->over_col;
2974 edit_cursor_move (edit,
2975 edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
2977 if (edit_options.cursor_beyond_eol)
2979 long line_len;
2981 line_len = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2982 edit_buffer_get_current_eol (&edit->buffer));
2983 if (line_len < prev + edit->over_col)
2985 edit->over_col = prev + over - line_len;
2986 edit->prev_col = line_len;
2987 edit->curs_col = line_len;
2989 else
2991 edit->curs_col = prev + over;
2992 edit->prev_col = edit->curs_col;
2993 edit->over_col = 0;
2996 else
2998 edit->over_col = 0;
2999 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer))
3001 long fake_half_tabs;
3003 edit_update_curs_col (edit);
3005 fake_half_tabs = HALF_TAB_SIZE * space_width;
3006 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3008 long q;
3010 q = edit->curs_col;
3011 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3012 p = edit_buffer_get_current_bol (&edit->buffer);
3013 edit_cursor_move (edit,
3014 edit_move_forward3 (edit, p, edit->curs_col,
3015 0) - edit->buffer.curs1);
3016 if (!left_of_four_spaces (edit))
3017 edit_cursor_move (edit,
3018 edit_move_forward3 (edit, p, q, 0) - edit->buffer.curs1);
3024 /* --------------------------------------------------------------------------------------------- */
3025 /** check whether line in editor is blank or not
3027 * @param edit editor object
3028 * @param line number of line
3030 * @return TRUE if line in blank, FALSE otherwise
3033 gboolean
3034 edit_line_is_blank (WEdit * edit, long line)
3036 return is_blank (&edit->buffer, edit_find_line (edit, line));
3039 /* --------------------------------------------------------------------------------------------- */
3040 /** move cursor to line 'line' */
3042 void
3043 edit_move_to_line (WEdit * e, long line)
3045 if (line < e->buffer.curs_line)
3046 edit_move_up (e, e->buffer.curs_line - line, FALSE);
3047 else
3048 edit_move_down (e, line - e->buffer.curs_line, FALSE);
3049 edit_scroll_screen_over_cursor (e);
3052 /* --------------------------------------------------------------------------------------------- */
3053 /** scroll window so that first visible line is 'line' */
3055 void
3056 edit_move_display (WEdit * e, long line)
3058 if (line < e->start_line)
3059 edit_scroll_upward (e, e->start_line - line);
3060 else
3061 edit_scroll_downward (e, line - e->start_line);
3064 /* --------------------------------------------------------------------------------------------- */
3065 /** save markers onto undo stack */
3067 void
3068 edit_push_markers (WEdit * edit)
3070 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3071 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3072 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3075 /* --------------------------------------------------------------------------------------------- */
3077 void
3078 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3080 edit->mark1 = m1;
3081 edit->mark2 = m2;
3082 edit->column1 = c1;
3083 edit->column2 = c2;
3087 /* --------------------------------------------------------------------------------------------- */
3088 /** highlight marker toggle */
3090 void
3091 edit_mark_cmd (WEdit * edit, gboolean unmark)
3093 edit_push_markers (edit);
3094 if (unmark)
3096 edit_set_markers (edit, 0, 0, 0, 0);
3097 edit->force |= REDRAW_PAGE;
3099 else if (edit->mark2 >= 0)
3101 edit->end_mark_curs = -1;
3102 edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3103 edit->curs_col + edit->over_col);
3104 edit->force |= REDRAW_PAGE;
3106 else
3108 edit->end_mark_curs = edit->buffer.curs1;
3109 edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3110 edit->curs_col + edit->over_col);
3114 /* --------------------------------------------------------------------------------------------- */
3115 /** highlight the word under cursor */
3117 void
3118 edit_mark_current_word_cmd (WEdit * edit)
3120 long pos;
3122 for (pos = edit->buffer.curs1; pos != 0; pos--)
3124 int c1, c2;
3126 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3127 c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3128 if (!isspace (c1) && isspace (c2))
3129 break;
3130 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3131 break;
3133 edit->mark1 = pos;
3135 for (; pos < edit->buffer.size; pos++)
3137 int c1, c2;
3139 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3140 c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3141 if (!isspace (c1) && isspace (c2))
3142 break;
3143 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3144 break;
3146 edit->mark2 = MIN (pos + 1, edit->buffer.size);
3148 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3151 /* --------------------------------------------------------------------------------------------- */
3153 void
3154 edit_mark_current_line_cmd (WEdit * edit)
3156 edit->mark1 = edit_buffer_get_current_bol (&edit->buffer);
3157 edit->mark2 = edit_buffer_get_current_eol (&edit->buffer);
3159 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3162 /* --------------------------------------------------------------------------------------------- */
3164 void
3165 edit_delete_line (WEdit * edit)
3168 * Delete right part of the line.
3169 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3170 * beyond EOF.
3172 while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3173 (void) edit_delete (edit, TRUE);
3176 * Delete '\n' char.
3177 * Note that edit_delete() will not corrupt anything if called while
3178 * cursor position is EOF.
3180 (void) edit_delete (edit, TRUE);
3183 * Delete left part of the line.
3184 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3186 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3187 (void) edit_backspace (edit, TRUE);
3190 /* --------------------------------------------------------------------------------------------- */
3192 void
3193 edit_push_key_press (WEdit * edit)
3195 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3196 if (edit->mark2 == -1)
3198 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3199 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3203 /* --------------------------------------------------------------------------------------------- */
3205 void
3206 edit_find_bracket (WEdit * edit)
3208 edit->bracket = edit_get_bracket (edit, 1, 10000);
3209 if (edit->last_bracket != edit->bracket)
3210 edit->force |= REDRAW_PAGE;
3211 edit->last_bracket = edit->bracket;
3214 /* --------------------------------------------------------------------------------------------- */
3216 * This executes a command as though the user initiated it through a key
3217 * press. Callback with MSG_KEY as a message calls this after
3218 * translating the key press. This function can be used to pass any
3219 * command to the editor. Note that the screen wouldn't update
3220 * automatically. Either of command or char_for_insertion must be
3221 * passed as -1. Commands are executed, and char_for_insertion is
3222 * inserted at the cursor.
3225 void
3226 edit_execute_key_command (WEdit * edit, long command, int char_for_insertion)
3228 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3229 || (macro_index < 0
3230 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3232 macro_index = 0;
3233 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3234 return;
3236 if (macro_index != -1)
3238 edit->force |= REDRAW_COMPLETELY;
3239 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3241 edit_store_macro_cmd (edit);
3242 macro_index = -1;
3243 return;
3245 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3247 edit_repeat_macro_cmd (edit);
3248 macro_index = -1;
3249 return;
3253 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3255 record_macro_buf[macro_index].action = command;
3256 record_macro_buf[macro_index++].ch = char_for_insertion;
3258 /* record the beginning of a set of editing actions initiated by a key press */
3259 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3260 edit_push_key_press (edit);
3262 edit_execute_cmd (edit, command, char_for_insertion);
3263 if (edit->column_highlight)
3264 edit->force |= REDRAW_PAGE;
3267 /* --------------------------------------------------------------------------------------------- */
3269 This executes a command at a lower level than macro recording.
3270 It also does not push a key_press onto the undo stack. This means
3271 that if it is called many times, a single undo command will undo
3272 all of them. It also does not check for the Undo command.
3274 void
3275 edit_execute_cmd (WEdit * edit, long command, int char_for_insertion)
3277 WRect *w = &WIDGET (edit)->rect;
3279 if (command == CK_WindowFullscreen)
3281 edit_toggle_fullscreen (edit);
3282 return;
3285 /* handle window state */
3286 if (edit_handle_move_resize (edit, command))
3287 return;
3289 edit->force |= REDRAW_LINE;
3291 /* The next key press will unhighlight the found string, so update
3292 * the whole page */
3293 if (edit->found_len || edit->column_highlight)
3294 edit->force |= REDRAW_PAGE;
3296 switch (command)
3298 /* a mark command with shift-arrow */
3299 case CK_MarkLeft:
3300 case CK_MarkRight:
3301 case CK_MarkToWordBegin:
3302 case CK_MarkToWordEnd:
3303 case CK_MarkToHome:
3304 case CK_MarkToEnd:
3305 case CK_MarkUp:
3306 case CK_MarkDown:
3307 case CK_MarkPageUp:
3308 case CK_MarkPageDown:
3309 case CK_MarkToFileBegin:
3310 case CK_MarkToFileEnd:
3311 case CK_MarkToPageBegin:
3312 case CK_MarkToPageEnd:
3313 case CK_MarkScrollUp:
3314 case CK_MarkScrollDown:
3315 case CK_MarkParagraphUp:
3316 case CK_MarkParagraphDown:
3317 /* a mark command with alt-arrow */
3318 case CK_MarkColumnPageUp:
3319 case CK_MarkColumnPageDown:
3320 case CK_MarkColumnLeft:
3321 case CK_MarkColumnRight:
3322 case CK_MarkColumnUp:
3323 case CK_MarkColumnDown:
3324 case CK_MarkColumnScrollUp:
3325 case CK_MarkColumnScrollDown:
3326 case CK_MarkColumnParagraphUp:
3327 case CK_MarkColumnParagraphDown:
3328 edit->column_highlight = 0;
3329 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3331 edit_mark_cmd (edit, TRUE); /* clear */
3332 edit_mark_cmd (edit, FALSE); /* marking on */
3334 edit->highlight = 1;
3335 break;
3337 /* any other command */
3338 default:
3339 if (edit->highlight)
3340 edit_mark_cmd (edit, FALSE); /* clear */
3341 edit->highlight = 0;
3344 /* first check for undo */
3345 if (command == CK_Undo)
3347 edit->redo_stack_reset = 0;
3348 edit_group_undo (edit);
3349 edit->found_len = 0;
3350 edit->prev_col = edit_get_col (edit);
3351 edit->search_start = edit->buffer.curs1;
3352 return;
3354 /* check for redo */
3355 if (command == CK_Redo)
3357 edit->redo_stack_reset = 0;
3358 edit_do_redo (edit);
3359 edit->found_len = 0;
3360 edit->prev_col = edit_get_col (edit);
3361 edit->search_start = edit->buffer.curs1;
3362 return;
3365 edit->redo_stack_reset = 1;
3367 /* An ordinary key press */
3368 if (char_for_insertion >= 0)
3370 /* if non persistent selection and text selected */
3371 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3372 edit_block_delete_cmd (edit);
3374 if (edit->overwrite)
3376 /* remove char only one time, after input first byte, multibyte chars */
3377 #ifdef HAVE_CHARSET
3378 if (!mc_global.utf8_display || edit->charpoint == 0)
3379 #endif
3380 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3382 edit_delete (edit, FALSE);
3384 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3385 edit_insert_over (edit);
3386 #ifdef HAVE_CHARSET
3388 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3389 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3391 if (char_for_insertion > 127 && str_isutf8 (get_codepage_id (mc_global.source_codepage))
3392 && !mc_global.utf8_display)
3394 unsigned char str[UTF8_CHAR_LEN + 1];
3395 size_t i;
3396 int res;
3398 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3399 if (res == 0)
3401 str[0] = '.';
3402 str[1] = '\0';
3404 else
3406 str[res] = '\0';
3408 for (i = 0; i <= UTF8_CHAR_LEN && str[i] != '\0'; i++)
3410 char_for_insertion = str[i];
3411 edit_insert (edit, char_for_insertion);
3414 else
3415 #endif
3416 edit_insert (edit, char_for_insertion);
3418 if (edit_options.auto_para_formatting)
3420 format_paragraph (edit, FALSE);
3421 edit->force |= REDRAW_PAGE;
3423 else
3424 check_and_wrap_line (edit);
3425 edit->found_len = 0;
3426 edit->prev_col = edit_get_col (edit);
3427 edit->search_start = edit->buffer.curs1;
3428 edit_find_bracket (edit);
3429 return;
3432 switch (command)
3434 case CK_TopOnScreen:
3435 case CK_BottomOnScreen:
3436 case CK_Top:
3437 case CK_Bottom:
3438 case CK_PageUp:
3439 case CK_PageDown:
3440 case CK_Home:
3441 case CK_End:
3442 case CK_Up:
3443 case CK_Down:
3444 case CK_Left:
3445 case CK_Right:
3446 case CK_WordLeft:
3447 case CK_WordRight:
3448 if (!edit_options.persistent_selections && edit->mark2 >= 0)
3450 if (edit->column_highlight)
3451 edit_push_undo_action (edit, COLUMN_ON);
3452 edit->column_highlight = 0;
3453 edit_mark_cmd (edit, TRUE);
3455 break;
3456 default:
3457 break;
3460 switch (command)
3462 case CK_TopOnScreen:
3463 case CK_BottomOnScreen:
3464 case CK_MarkToPageBegin:
3465 case CK_MarkToPageEnd:
3466 case CK_Up:
3467 case CK_Down:
3468 case CK_WordLeft:
3469 case CK_WordRight:
3470 case CK_MarkToWordBegin:
3471 case CK_MarkToWordEnd:
3472 case CK_MarkUp:
3473 case CK_MarkDown:
3474 case CK_MarkColumnUp:
3475 case CK_MarkColumnDown:
3476 if (edit->mark2 == -1)
3477 break; /*marking is following the cursor: may need to highlight a whole line */
3478 MC_FALLTHROUGH;
3479 case CK_Left:
3480 case CK_Right:
3481 case CK_MarkLeft:
3482 case CK_MarkRight:
3483 edit->force |= REDRAW_CHAR_ONLY;
3484 break;
3485 default:
3486 break;
3489 /* basic cursor key commands */
3490 switch (command)
3492 case CK_BackSpace:
3493 /* if non persistent selection and text selected */
3494 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3495 edit_block_delete_cmd (edit);
3496 else if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3497 edit->over_col--;
3498 else if (edit_options.backspace_through_tabs && is_in_indent (&edit->buffer))
3500 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
3501 edit_backspace (edit, TRUE);
3503 else if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3504 && right_of_four_spaces (edit))
3506 int i;
3508 for (i = 0; i < HALF_TAB_SIZE; i++)
3509 edit_backspace (edit, TRUE);
3511 else
3512 edit_backspace (edit, FALSE);
3513 break;
3514 case CK_Delete:
3515 /* if non persistent selection and text selected */
3516 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3517 edit_block_delete_cmd (edit);
3518 else
3520 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3521 edit_insert_over (edit);
3523 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3524 && left_of_four_spaces (edit))
3526 int i;
3528 for (i = 1; i <= HALF_TAB_SIZE; i++)
3529 edit_delete (edit, TRUE);
3531 else
3532 edit_delete (edit, FALSE);
3534 break;
3535 case CK_DeleteToWordBegin:
3536 edit->over_col = 0;
3537 edit_left_delete_word (edit);
3538 break;
3539 case CK_DeleteToWordEnd:
3540 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3541 edit_insert_over (edit);
3543 edit_right_delete_word (edit);
3544 break;
3545 case CK_DeleteLine:
3546 edit_delete_line (edit);
3547 break;
3548 case CK_DeleteToHome:
3549 edit_delete_to_line_begin (edit);
3550 break;
3551 case CK_DeleteToEnd:
3552 edit_delete_to_line_end (edit);
3553 break;
3554 case CK_Enter:
3555 edit->over_col = 0;
3556 if (edit_options.auto_para_formatting)
3558 edit_double_newline (edit);
3559 if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3560 edit_auto_indent (edit);
3561 format_paragraph (edit, FALSE);
3563 else
3565 edit_insert (edit, '\n');
3566 if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3567 edit_auto_indent (edit);
3569 break;
3570 case CK_Return:
3571 edit_insert (edit, '\n');
3572 break;
3574 case CK_MarkColumnPageUp:
3575 edit->column_highlight = 1;
3576 MC_FALLTHROUGH;
3577 case CK_PageUp:
3578 case CK_MarkPageUp:
3579 edit_move_up (edit, w->lines - 1, TRUE);
3580 break;
3581 case CK_MarkColumnPageDown:
3582 edit->column_highlight = 1;
3583 MC_FALLTHROUGH;
3584 case CK_PageDown:
3585 case CK_MarkPageDown:
3586 edit_move_down (edit, w->lines - 1, TRUE);
3587 break;
3588 case CK_MarkColumnLeft:
3589 edit->column_highlight = 1;
3590 MC_FALLTHROUGH;
3591 case CK_Left:
3592 case CK_MarkLeft:
3593 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3594 && right_of_four_spaces (edit))
3596 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3597 edit->over_col--;
3598 else
3599 edit_cursor_move (edit, -HALF_TAB_SIZE);
3600 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3602 else
3603 edit_left_char_move_cmd (edit);
3604 break;
3605 case CK_MarkColumnRight:
3606 edit->column_highlight = 1;
3607 MC_FALLTHROUGH;
3608 case CK_Right:
3609 case CK_MarkRight:
3610 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3611 && left_of_four_spaces (edit))
3613 edit_cursor_move (edit, HALF_TAB_SIZE);
3614 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3616 else
3617 edit_right_char_move_cmd (edit);
3618 break;
3619 case CK_TopOnScreen:
3620 case CK_MarkToPageBegin:
3621 edit_begin_page (edit);
3622 break;
3623 case CK_BottomOnScreen:
3624 case CK_MarkToPageEnd:
3625 edit_end_page (edit);
3626 break;
3627 case CK_WordLeft:
3628 case CK_MarkToWordBegin:
3629 edit->over_col = 0;
3630 edit_left_word_move_cmd (edit);
3631 break;
3632 case CK_WordRight:
3633 case CK_MarkToWordEnd:
3634 edit->over_col = 0;
3635 edit_right_word_move_cmd (edit);
3636 break;
3637 case CK_MarkColumnUp:
3638 edit->column_highlight = 1;
3639 MC_FALLTHROUGH;
3640 case CK_Up:
3641 case CK_MarkUp:
3642 edit_move_up (edit, 1, FALSE);
3643 break;
3644 case CK_MarkColumnDown:
3645 edit->column_highlight = 1;
3646 MC_FALLTHROUGH;
3647 case CK_Down:
3648 case CK_MarkDown:
3649 edit_move_down (edit, 1, FALSE);
3650 break;
3651 case CK_MarkColumnParagraphUp:
3652 edit->column_highlight = 1;
3653 MC_FALLTHROUGH;
3654 case CK_ParagraphUp:
3655 case CK_MarkParagraphUp:
3656 edit_move_up_paragraph (edit, FALSE);
3657 break;
3658 case CK_MarkColumnParagraphDown:
3659 edit->column_highlight = 1;
3660 MC_FALLTHROUGH;
3661 case CK_ParagraphDown:
3662 case CK_MarkParagraphDown:
3663 edit_move_down_paragraph (edit, FALSE);
3664 break;
3665 case CK_MarkColumnScrollUp:
3666 edit->column_highlight = 1;
3667 MC_FALLTHROUGH;
3668 case CK_ScrollUp:
3669 case CK_MarkScrollUp:
3670 edit_move_up (edit, 1, TRUE);
3671 break;
3672 case CK_MarkColumnScrollDown:
3673 edit->column_highlight = 1;
3674 MC_FALLTHROUGH;
3675 case CK_ScrollDown:
3676 case CK_MarkScrollDown:
3677 edit_move_down (edit, 1, TRUE);
3678 break;
3679 case CK_Home:
3680 case CK_MarkToHome:
3681 edit_cursor_to_bol (edit);
3682 break;
3683 case CK_End:
3684 case CK_MarkToEnd:
3685 edit_cursor_to_eol (edit);
3686 break;
3687 case CK_Tab:
3688 /* if text marked shift block */
3689 if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3691 if (edit->mark2 < 0)
3692 edit_mark_cmd (edit, FALSE);
3693 edit_move_block_to_right (edit);
3695 else
3697 if (edit_options.cursor_beyond_eol)
3698 edit_insert_over (edit);
3699 edit_tab_cmd (edit);
3700 if (edit_options.auto_para_formatting)
3702 format_paragraph (edit, FALSE);
3703 edit->force |= REDRAW_PAGE;
3705 else
3706 check_and_wrap_line (edit);
3708 break;
3710 case CK_InsertOverwrite:
3711 edit->overwrite = !edit->overwrite;
3712 break;
3714 case CK_Mark:
3715 if (edit->mark2 >= 0)
3717 if (edit->column_highlight)
3718 edit_push_undo_action (edit, COLUMN_ON);
3719 edit->column_highlight = 0;
3721 edit_mark_cmd (edit, FALSE);
3722 break;
3723 case CK_MarkColumn:
3724 if (!edit->column_highlight)
3725 edit_push_undo_action (edit, COLUMN_OFF);
3726 edit->column_highlight = 1;
3727 edit_mark_cmd (edit, FALSE);
3728 break;
3729 case CK_MarkAll:
3730 edit_set_markers (edit, 0, edit->buffer.size, 0, 0);
3731 edit->force |= REDRAW_PAGE;
3732 break;
3733 case CK_Unmark:
3734 if (edit->column_highlight)
3735 edit_push_undo_action (edit, COLUMN_ON);
3736 edit->column_highlight = 0;
3737 edit_mark_cmd (edit, TRUE);
3738 break;
3739 case CK_MarkWord:
3740 if (edit->column_highlight)
3741 edit_push_undo_action (edit, COLUMN_ON);
3742 edit->column_highlight = 0;
3743 edit_mark_current_word_cmd (edit);
3744 break;
3745 case CK_MarkLine:
3746 if (edit->column_highlight)
3747 edit_push_undo_action (edit, COLUMN_ON);
3748 edit->column_highlight = 0;
3749 edit_mark_current_line_cmd (edit);
3750 break;
3752 case CK_Bookmark:
3753 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR);
3754 if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR))
3755 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3756 else
3757 book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3758 break;
3759 case CK_BookmarkFlush:
3760 book_mark_flush (edit, BOOK_MARK_COLOR);
3761 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3762 edit->force |= REDRAW_PAGE;
3763 break;
3764 case CK_BookmarkNext:
3765 if (edit->book_mark != NULL)
3767 edit_book_mark_t *p;
3769 p = book_mark_find (edit, edit->buffer.curs_line);
3770 if (p->next != NULL)
3772 p = p->next;
3773 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3774 edit_move_display (edit, p->line - w->lines / 2);
3775 edit_move_to_line (edit, p->line);
3778 break;
3779 case CK_BookmarkPrev:
3780 if (edit->book_mark != NULL)
3782 edit_book_mark_t *p;
3784 p = book_mark_find (edit, edit->buffer.curs_line);
3785 while (p->line == edit->buffer.curs_line)
3786 if (p->prev != NULL)
3787 p = p->prev;
3788 if (p->line >= 0)
3790 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3791 edit_move_display (edit, p->line - w->lines / 2);
3792 edit_move_to_line (edit, p->line);
3795 break;
3797 case CK_Top:
3798 case CK_MarkToFileBegin:
3799 edit_move_to_top (edit);
3800 break;
3801 case CK_Bottom:
3802 case CK_MarkToFileEnd:
3803 edit_move_to_bottom (edit);
3804 break;
3806 case CK_Copy:
3807 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3808 edit_insert_over (edit);
3809 edit_block_copy_cmd (edit);
3810 break;
3811 case CK_Remove:
3812 edit_block_delete_cmd (edit);
3813 break;
3814 case CK_Move:
3815 edit_block_move_cmd (edit);
3816 break;
3818 case CK_BlockShiftLeft:
3819 if (edit->mark1 != edit->mark2)
3820 edit_move_block_to_left (edit);
3821 break;
3822 case CK_BlockShiftRight:
3823 if (edit->mark1 != edit->mark2)
3824 edit_move_block_to_right (edit);
3825 break;
3826 case CK_Store:
3827 edit_copy_to_X_buf_cmd (edit);
3828 break;
3829 case CK_Cut:
3830 edit_cut_to_X_buf_cmd (edit);
3831 break;
3832 case CK_Paste:
3833 /* if non persistent selection and text selected */
3834 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3835 edit_block_delete_cmd (edit);
3836 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3837 edit_insert_over (edit);
3838 edit_paste_from_X_buf_cmd (edit);
3839 if (!edit_options.persistent_selections && edit->mark2 >= 0)
3841 if (edit->column_highlight)
3842 edit_push_undo_action (edit, COLUMN_ON);
3843 edit->column_highlight = 0;
3844 edit_mark_cmd (edit, TRUE);
3846 break;
3847 case CK_History:
3848 edit_paste_from_history (edit);
3849 break;
3851 case CK_SaveAs:
3852 edit_save_as_cmd (edit);
3853 break;
3854 case CK_Save:
3855 edit_save_confirm_cmd (edit);
3856 break;
3857 case CK_BlockSave:
3858 edit_save_block_cmd (edit);
3859 break;
3860 case CK_InsertFile:
3861 edit_insert_file_cmd (edit);
3862 break;
3864 case CK_FilePrev:
3865 edit_load_back_cmd (edit);
3866 break;
3867 case CK_FileNext:
3868 edit_load_forward_cmd (edit);
3869 break;
3871 case CK_SyntaxChoose:
3872 edit_syntax_dialog (edit);
3873 break;
3875 case CK_Search:
3876 edit_search_cmd (edit, FALSE);
3877 break;
3878 case CK_SearchContinue:
3879 edit_search_cmd (edit, TRUE);
3880 break;
3881 case CK_Replace:
3882 edit_replace_cmd (edit, FALSE);
3883 break;
3884 case CK_ReplaceContinue:
3885 edit_replace_cmd (edit, TRUE);
3886 break;
3887 case CK_Complete:
3888 /* if text marked shift block */
3889 if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3890 edit_move_block_to_left (edit);
3891 else
3892 edit_complete_word_cmd (edit);
3893 break;
3894 case CK_Find:
3895 edit_get_match_keyword_cmd (edit);
3896 break;
3898 #ifdef HAVE_ASPELL
3899 case CK_SpellCheckCurrentWord:
3900 edit_suggest_current_word (edit);
3901 break;
3902 case CK_SpellCheck:
3903 edit_spellcheck_file (edit);
3904 break;
3905 case CK_SpellCheckSelectLang:
3906 edit_set_spell_lang ();
3907 break;
3908 #endif
3910 case CK_Date:
3912 char s[BUF_MEDIUM];
3913 /* fool gcc to prevent a Y2K warning */
3914 char time_format[] = "_c";
3915 time_format[0] = '%';
3917 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3918 edit_print_string (edit, s);
3919 edit->force |= REDRAW_PAGE;
3921 break;
3922 case CK_Goto:
3923 edit_goto_cmd (edit);
3924 break;
3925 case CK_ParagraphFormat:
3926 format_paragraph (edit, TRUE);
3927 edit->force |= REDRAW_PAGE;
3928 break;
3929 case CK_MacroDelete:
3930 edit_delete_macro_cmd (edit);
3931 break;
3932 case CK_MatchBracket:
3933 edit_goto_matching_bracket (edit);
3934 break;
3935 case CK_UserMenu:
3936 edit_user_menu (edit, NULL, -1);
3937 break;
3938 case CK_Sort:
3939 edit_sort_cmd (edit);
3940 break;
3941 case CK_ExternalCommand:
3942 edit_ext_cmd (edit);
3943 break;
3944 case CK_EditMail:
3945 edit_mail_dialog (edit);
3946 break;
3947 #ifdef HAVE_CHARSET
3948 case CK_SelectCodepage:
3949 edit_select_codepage_cmd (edit);
3950 break;
3951 #endif
3952 case CK_InsertLiteral:
3953 edit_insert_literal_cmd (edit);
3954 break;
3955 case CK_MacroStartStopRecord:
3956 edit_begin_end_macro_cmd (edit);
3957 break;
3958 case CK_RepeatStartStopRecord:
3959 edit_begin_end_repeat_cmd (edit);
3960 break;
3961 case CK_ExtendedKeyMap:
3962 WIDGET (edit)->ext_mode = TRUE;
3963 break;
3964 default:
3965 break;
3968 /* CK_PipeBlock */
3969 if ((command / CK_PipeBlock (0)) == 1)
3970 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
3972 /* keys which must set the col position, and the search vars */
3973 switch (command)
3975 case CK_Search:
3976 case CK_SearchContinue:
3977 case CK_Replace:
3978 case CK_ReplaceContinue:
3979 case CK_Complete:
3980 edit->prev_col = edit_get_col (edit);
3981 break;
3982 case CK_Up:
3983 case CK_MarkUp:
3984 case CK_MarkColumnUp:
3985 case CK_Down:
3986 case CK_MarkDown:
3987 case CK_MarkColumnDown:
3988 case CK_PageUp:
3989 case CK_MarkPageUp:
3990 case CK_MarkColumnPageUp:
3991 case CK_PageDown:
3992 case CK_MarkPageDown:
3993 case CK_MarkColumnPageDown:
3994 case CK_Top:
3995 case CK_MarkToFileBegin:
3996 case CK_Bottom:
3997 case CK_MarkToFileEnd:
3998 case CK_ParagraphUp:
3999 case CK_MarkParagraphUp:
4000 case CK_MarkColumnParagraphUp:
4001 case CK_ParagraphDown:
4002 case CK_MarkParagraphDown:
4003 case CK_MarkColumnParagraphDown:
4004 case CK_ScrollUp:
4005 case CK_MarkScrollUp:
4006 case CK_MarkColumnScrollUp:
4007 case CK_ScrollDown:
4008 case CK_MarkScrollDown:
4009 case CK_MarkColumnScrollDown:
4010 edit->search_start = edit->buffer.curs1;
4011 edit->found_len = 0;
4012 break;
4013 default:
4014 edit->found_len = 0;
4015 edit->prev_col = edit_get_col (edit);
4016 edit->search_start = edit->buffer.curs1;
4018 edit_find_bracket (edit);
4020 if (edit_options.auto_para_formatting)
4022 switch (command)
4024 case CK_BackSpace:
4025 case CK_Delete:
4026 case CK_DeleteToWordBegin:
4027 case CK_DeleteToWordEnd:
4028 case CK_DeleteToHome:
4029 case CK_DeleteToEnd:
4030 format_paragraph (edit, FALSE);
4031 edit->force |= REDRAW_PAGE;
4032 break;
4033 default:
4034 break;
4039 /* --------------------------------------------------------------------------------------------- */
4041 void
4042 edit_stack_init (void)
4044 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4046 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4047 edit_history_moveto[edit_stack_iterator].line = -1;
4050 edit_stack_iterator = 0;
4053 /* --------------------------------------------------------------------------------------------- */
4055 void
4056 edit_stack_free (void)
4058 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4059 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath, TRUE);
4062 /* --------------------------------------------------------------------------------------------- */
4063 /** move i lines */
4065 void
4066 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4068 edit_move_updown (edit, i, do_scroll, TRUE);
4071 /* --------------------------------------------------------------------------------------------- */
4072 /** move i lines */
4074 void
4075 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4077 edit_move_updown (edit, i, do_scroll, FALSE);
4080 /* --------------------------------------------------------------------------------------------- */