(filevercmp): make inline.
[midnight-commander.git] / src / editor / edit.c
blob99b131513a1937119515ea1bd369e15ee59653a6
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996-2024
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_arg_t 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[] = {
138 /* *INDENT-OFF* */
139 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
140 { "zstd -cd %s 2>&1", "zstd > %s", ".zst"},
141 { "lz4 -cd %s 2>&1", "lz4 > %s", ".lz4" },
142 { "lzip -cd %s 2>&1", "lzip > %s", ".lz"},
143 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
144 { "lzop -cd %s 2>&1", "lzop > %s", ".lzo"},
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;
487 off_t b;
489 if (edit->filename_vpath == NULL
490 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
491 return;
493 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
494 /* apply bookmarks in any case */
495 book_mark_restore (edit, BOOK_MARK_COLOR);
497 if (!load_position)
498 return;
500 if (line > 0)
502 edit_move_to_line (edit, line - 1);
503 edit->prev_col = column;
505 else if (offset > 0)
507 edit_cursor_move (edit, offset);
508 line = edit->buffer.curs_line;
509 edit->search_start = edit->buffer.curs1;
512 b = edit_buffer_get_current_bol (&edit->buffer);
513 edit_move_to_prev_col (edit, b);
514 edit_move_display (edit, line - (WIDGET (edit)->rect.lines / 2));
517 /* --------------------------------------------------------------------------------------------- */
518 /** Save cursor position in the file */
520 static void
521 edit_save_position (WEdit *edit)
523 if (edit->filename_vpath == NULL
524 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
525 return;
527 book_mark_serialize (edit, BOOK_MARK_COLOR);
528 save_file_position (edit->filename_vpath, edit->buffer.curs_line + 1, edit->curs_col,
529 edit->buffer.curs1, edit->serialized_bookmarks);
530 edit->serialized_bookmarks = NULL;
533 /* --------------------------------------------------------------------------------------------- */
534 /** Clean the WEdit stricture except the widget part */
536 static void
537 edit_purge_widget (WEdit *edit)
539 size_t len = sizeof (WEdit) - sizeof (Widget);
540 char *start = (char *) edit + sizeof (Widget);
541 memset (start, 0, len);
544 /* --------------------------------------------------------------------------------------------- */
547 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
548 then the file should be as it was when he loaded up. Then set edit->modified to 0.
551 static long
552 edit_pop_undo_action (WEdit *edit)
554 long c;
555 unsigned long sp = edit->undo_stack_pointer;
557 if (sp == edit->undo_stack_bottom)
558 return STACK_BOTTOM;
560 sp = (sp - 1) & edit->undo_stack_size_mask;
561 c = edit->undo_stack[sp];
562 if (c >= 0)
564 /* edit->undo_stack[sp] = '@'; */
565 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
566 return c;
569 if (sp == edit->undo_stack_bottom)
570 return STACK_BOTTOM;
572 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
573 if (edit->undo_stack[sp] == -2)
575 /* edit->undo_stack[sp] = '@'; */
576 edit->undo_stack_pointer = sp;
578 else
579 edit->undo_stack[sp]++;
581 return c;
584 /* --------------------------------------------------------------------------------------------- */
586 static long
587 edit_pop_redo_action (WEdit *edit)
589 long c;
590 unsigned long sp = edit->redo_stack_pointer;
592 if (sp == edit->redo_stack_bottom)
593 return STACK_BOTTOM;
595 sp = (sp - 1) & edit->redo_stack_size_mask;
596 c = edit->redo_stack[sp];
597 if (c >= 0)
599 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
600 return c;
603 if (sp == edit->redo_stack_bottom)
604 return STACK_BOTTOM;
606 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
607 if (edit->redo_stack[sp] == -2)
608 edit->redo_stack_pointer = sp;
609 else
610 edit->redo_stack[sp]++;
612 return c;
615 /* --------------------------------------------------------------------------------------------- */
617 static long
618 get_prev_undo_action (WEdit *edit)
620 long c;
621 unsigned long sp = edit->undo_stack_pointer;
623 if (sp == edit->undo_stack_bottom)
624 return STACK_BOTTOM;
626 sp = (sp - 1) & edit->undo_stack_size_mask;
627 c = edit->undo_stack[sp];
628 if (c >= 0)
629 return c;
631 if (sp == edit->undo_stack_bottom)
632 return STACK_BOTTOM;
634 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
635 return c;
638 /* --------------------------------------------------------------------------------------------- */
639 /** is called whenever a modification is made by one of the four routines below */
641 static void
642 edit_modification (WEdit *edit)
644 edit->caches_valid = FALSE;
646 /* raise lock when file modified */
647 if (edit->modified == 0 && edit->delete_file == 0)
648 edit->locked = lock_file (edit->filename_vpath);
649 edit->modified = 1;
652 /* --------------------------------------------------------------------------------------------- */
653 /* high level cursor movement commands */
654 /* --------------------------------------------------------------------------------------------- */
655 /** check whether cursor is in indent part of line
657 * @param edit editor object
659 * @return TRUE if cursor is in indent, FALSE otherwise
662 static gboolean
663 is_in_indent (const edit_buffer_t *buf)
665 off_t p;
667 for (p = edit_buffer_get_current_bol (buf); p < buf->curs1; p++)
668 if (strchr (" \t", edit_buffer_get_byte (buf, p)) == NULL)
669 return FALSE;
671 return TRUE;
674 /* --------------------------------------------------------------------------------------------- */
675 /** check whether line in editor is blank or not
677 * @param edit editor object
678 * @param offset position in file
680 * @return TRUE if line in blank, FALSE otherwise
683 static gboolean
684 is_blank (const edit_buffer_t *buf, off_t offset)
686 off_t s, f;
688 s = edit_buffer_get_bol (buf, offset);
689 f = edit_buffer_get_eol (buf, offset);
690 for (; s < f; s++)
692 int c;
694 c = edit_buffer_get_byte (buf, s);
695 if (!isspace (c))
696 return FALSE;
698 return TRUE;
701 /* --------------------------------------------------------------------------------------------- */
702 /** returns the offset of line i */
704 static off_t
705 edit_find_line (WEdit *edit, long line)
707 long i;
708 long j = 0;
709 long m = 2000000000; /* what is the magic number? */
711 if (!edit->caches_valid)
713 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
714 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
715 /* three offsets that we *know* are line 0 at 0 and these two: */
716 edit->line_numbers[1] = edit->buffer.curs_line;
717 edit->line_offsets[1] = edit_buffer_get_current_bol (&edit->buffer);
718 edit->line_numbers[2] = edit->buffer.lines;
719 edit->line_offsets[2] = edit_buffer_get_bol (&edit->buffer, edit->buffer.size);
720 edit->caches_valid = TRUE;
722 if (line >= edit->buffer.lines)
723 return edit->line_offsets[2];
724 if (line <= 0)
725 return 0;
726 /* find the closest known point */
727 for (i = 0; i < N_LINE_CACHES; i++)
729 long n;
731 n = labs (edit->line_numbers[i] - line);
732 if (n < m)
734 m = n;
735 j = i;
738 if (m == 0)
739 return edit->line_offsets[j]; /* know the offset exactly */
740 if (m == 1 && j >= 3)
741 i = j; /* one line different - caller might be looping, so stay in this cache */
742 else
743 i = 3 + (rand () % (N_LINE_CACHES - 3));
744 if (line > edit->line_numbers[j])
745 edit->line_offsets[i] =
746 edit_buffer_get_forward_offset (&edit->buffer, edit->line_offsets[j],
747 line - edit->line_numbers[j], 0);
748 else
749 edit->line_offsets[i] =
750 edit_buffer_get_backward_offset (&edit->buffer, edit->line_offsets[j],
751 edit->line_numbers[j] - line);
752 edit->line_numbers[i] = line;
753 return edit->line_offsets[i];
756 /* --------------------------------------------------------------------------------------------- */
757 /** moves up until a blank line is reached, or until just
758 before a non-blank line is reached */
760 static void
761 edit_move_up_paragraph (WEdit *edit, gboolean do_scroll)
763 long i = 0;
765 if (edit->buffer.curs_line > 1)
767 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
769 for (i = edit->buffer.curs_line - 1; i != 0; i--)
770 if (edit_line_is_blank (edit, i))
771 break;
773 else if (edit_line_is_blank (edit, edit->buffer.curs_line - 1))
775 for (i = edit->buffer.curs_line - 1; i != 0; i--)
776 if (!edit_line_is_blank (edit, i))
778 i++;
779 break;
782 else
784 for (i = edit->buffer.curs_line - 1; i != 0; i--)
785 if (edit_line_is_blank (edit, i))
786 break;
790 edit_move_up (edit, edit->buffer.curs_line - i, do_scroll);
793 /* --------------------------------------------------------------------------------------------- */
794 /** moves down until a blank line is reached, or until just
795 before a non-blank line is reached */
797 static void
798 edit_move_down_paragraph (WEdit *edit, gboolean do_scroll)
800 long i;
802 if (edit->buffer.curs_line >= edit->buffer.lines - 1)
803 i = edit->buffer.lines;
804 else if (!edit_line_is_blank (edit, edit->buffer.curs_line))
806 for (i = edit->buffer.curs_line + 1; i != 0; i++)
807 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
808 break;
810 else if (edit_line_is_blank (edit, edit->buffer.curs_line + 1))
812 for (i = edit->buffer.curs_line + 1; i != 0; i++)
813 if (!edit_line_is_blank (edit, i) || i > edit->buffer.lines)
815 i--;
816 break;
819 else
821 for (i = edit->buffer.curs_line + 1; i != 0; i++)
822 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
823 break;
825 edit_move_down (edit, i - edit->buffer.curs_line, do_scroll);
828 /* --------------------------------------------------------------------------------------------- */
830 static void
831 edit_begin_page (WEdit *edit)
833 edit_update_curs_row (edit);
834 edit_move_up (edit, edit->curs_row, FALSE);
837 /* --------------------------------------------------------------------------------------------- */
839 static void
840 edit_end_page (WEdit *edit)
842 edit_update_curs_row (edit);
843 edit_move_down (edit, WIDGET (edit)->rect.lines - edit->curs_row - 1, FALSE);
847 /* --------------------------------------------------------------------------------------------- */
848 /** goto beginning of text */
850 static void
851 edit_move_to_top (WEdit *edit)
853 if (edit->buffer.curs_line != 0)
855 edit_cursor_move (edit, -edit->buffer.curs1);
856 edit_move_to_prev_col (edit, 0);
857 edit->force |= REDRAW_PAGE;
858 edit->search_start = 0;
859 edit_update_curs_row (edit);
863 /* --------------------------------------------------------------------------------------------- */
864 /** goto end of text */
866 static void
867 edit_move_to_bottom (WEdit *edit)
869 if (edit->buffer.curs_line < edit->buffer.lines)
871 edit_move_down (edit, edit->buffer.lines - edit->curs_row, FALSE);
872 edit->start_display = edit->buffer.size;
873 edit->start_line = edit->buffer.lines;
874 edit_scroll_upward (edit, WIDGET (edit)->rect.lines - 1);
875 edit->force |= REDRAW_PAGE;
879 /* --------------------------------------------------------------------------------------------- */
880 /** goto beginning of line */
882 static void
883 edit_cursor_to_bol (WEdit *edit)
885 off_t b;
887 b = edit_buffer_get_current_bol (&edit->buffer);
888 edit_cursor_move (edit, b - edit->buffer.curs1);
889 edit->search_start = edit->buffer.curs1;
890 edit->prev_col = edit_get_col (edit);
891 edit->over_col = 0;
894 /* --------------------------------------------------------------------------------------------- */
895 /** goto end of line */
897 static void
898 edit_cursor_to_eol (WEdit *edit)
900 off_t b;
902 b = edit_buffer_get_current_eol (&edit->buffer);
903 edit_cursor_move (edit, b - edit->buffer.curs1);
904 edit->search_start = edit->buffer.curs1;
905 edit->prev_col = edit_get_col (edit);
906 edit->over_col = 0;
909 /* --------------------------------------------------------------------------------------------- */
911 static unsigned long
912 my_type_of (int c)
914 unsigned long r = 0;
915 const char *q;
916 const char chars_move_whole_word[] =
917 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
919 if (c == 0)
920 return 0;
921 if (c == '!')
922 return 2;
924 if (g_ascii_isupper ((gchar) c))
925 c = 'A';
926 else if (g_ascii_islower ((gchar) c))
927 c = 'a';
928 else if (g_ascii_isalpha (c))
929 c = 'a';
930 else if (isdigit (c))
931 c = '0';
932 else if (isspace (c))
933 c = ' ';
934 q = strchr (chars_move_whole_word, c);
935 if (q == NULL)
936 return 0xFFFFFFFFUL;
940 unsigned long x;
941 const char *p;
943 for (x = 1, p = chars_move_whole_word; p < q; p++)
944 if (*p == '!')
945 x <<= 1;
946 r |= x;
948 while ((q = strchr (q + 1, c)) != NULL);
950 return r;
953 /* --------------------------------------------------------------------------------------------- */
955 static void
956 edit_left_word_move (WEdit *edit, int s)
958 while (TRUE)
960 int c1, c2;
962 if (edit->column_highlight
963 && edit->mark1 != edit->mark2
964 && edit->over_col == 0
965 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
966 break;
967 edit_cursor_move (edit, -1);
968 if (edit->buffer.curs1 == 0)
969 break;
970 c1 = edit_buffer_get_previous_byte (&edit->buffer);
971 if (c1 == '\n')
972 break;
973 c2 = edit_buffer_get_current_byte (&edit->buffer);
974 if (c2 == '\n')
975 break;
976 if ((my_type_of (c1) & my_type_of (c2)) == 0)
977 break;
978 if (isspace (c1) && !isspace (c2))
979 break;
980 if (s != 0 && !isspace (c1) && isspace (c2))
981 break;
985 /* --------------------------------------------------------------------------------------------- */
987 static void
988 edit_left_word_move_cmd (WEdit *edit)
990 edit_left_word_move (edit, 0);
991 edit->force |= REDRAW_PAGE;
994 /* --------------------------------------------------------------------------------------------- */
996 static void
997 edit_right_word_move (WEdit *edit, int s)
999 while (TRUE)
1001 int c1, c2;
1003 if (edit->column_highlight
1004 && edit->mark1 != edit->mark2
1005 && edit->over_col == 0
1006 && edit->buffer.curs1 == edit_buffer_get_current_eol (&edit->buffer))
1007 break;
1008 edit_cursor_move (edit, 1);
1009 if (edit->buffer.curs1 >= edit->buffer.size)
1010 break;
1011 c1 = edit_buffer_get_previous_byte (&edit->buffer);
1012 if (c1 == '\n')
1013 break;
1014 c2 = edit_buffer_get_current_byte (&edit->buffer);
1015 if (c2 == '\n')
1016 break;
1017 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1018 break;
1019 if (isspace (c1) && !isspace (c2))
1020 break;
1021 if (s != 0 && !isspace (c1) && isspace (c2))
1022 break;
1026 /* --------------------------------------------------------------------------------------------- */
1028 static void
1029 edit_right_word_move_cmd (WEdit *edit)
1031 edit_right_word_move (edit, 0);
1032 edit->force |= REDRAW_PAGE;
1035 /* --------------------------------------------------------------------------------------------- */
1037 static void
1038 edit_right_char_move_cmd (WEdit *edit)
1040 int char_length = 1;
1041 int c;
1043 #ifdef HAVE_CHARSET
1044 if (edit->utf8)
1046 c = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1047 if (char_length < 1)
1048 char_length = 1;
1050 else
1051 #endif
1052 c = edit_buffer_get_current_byte (&edit->buffer);
1054 if (edit_options.cursor_beyond_eol && c == '\n')
1055 edit->over_col++;
1056 else
1057 edit_cursor_move (edit, char_length);
1060 /* --------------------------------------------------------------------------------------------- */
1062 static void
1063 edit_left_char_move_cmd (WEdit *edit)
1065 int char_length = 1;
1067 if (edit->column_highlight
1068 && edit_options.cursor_beyond_eol
1069 && edit->mark1 != edit->mark2
1070 && edit->over_col == 0 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
1071 return;
1072 #ifdef HAVE_CHARSET
1073 if (edit->utf8)
1075 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1076 if (char_length < 1)
1077 char_length = 1;
1079 #endif
1081 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
1082 edit->over_col--;
1083 else
1084 edit_cursor_move (edit, -char_length);
1087 /* --------------------------------------------------------------------------------------------- */
1088 /** Up or down cursor moving.
1089 direction = TRUE - move up
1090 = FALSE - move down
1093 static void
1094 edit_move_updown (WEdit *edit, long lines, gboolean do_scroll, gboolean direction)
1096 long p;
1097 long l = direction ? edit->buffer.curs_line : edit->buffer.lines - edit->buffer.curs_line;
1099 if (lines > l)
1100 lines = l;
1102 if (lines == 0)
1103 return;
1105 if (lines > 1)
1106 edit->force |= REDRAW_PAGE;
1107 if (do_scroll)
1109 if (direction)
1110 edit_scroll_upward (edit, lines);
1111 else
1112 edit_scroll_downward (edit, lines);
1114 p = edit_buffer_get_current_bol (&edit->buffer);
1115 p = direction ? edit_buffer_get_backward_offset (&edit->buffer, p, lines) :
1116 edit_buffer_get_forward_offset (&edit->buffer, p, lines, 0);
1117 edit_cursor_move (edit, p - edit->buffer.curs1);
1118 edit_move_to_prev_col (edit, p);
1120 #ifdef HAVE_CHARSET
1121 /* search start of current multibyte char (like CJK) */
1122 if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->buffer.size
1123 && edit_buffer_get_current_byte (&edit->buffer) >= 256)
1125 edit_right_char_move_cmd (edit);
1126 edit_left_char_move_cmd (edit);
1128 #endif
1130 edit->search_start = edit->buffer.curs1;
1131 edit->found_len = 0;
1134 /* --------------------------------------------------------------------------------------------- */
1136 static void
1137 edit_right_delete_word (WEdit *edit)
1139 while (edit->buffer.curs1 < edit->buffer.size)
1141 int c1, c2;
1143 c1 = edit_delete (edit, TRUE);
1144 if (c1 == '\n')
1145 break;
1146 c2 = edit_buffer_get_current_byte (&edit->buffer);
1147 if (c2 == '\n')
1148 break;
1149 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1150 break;
1151 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1152 break;
1156 /* --------------------------------------------------------------------------------------------- */
1158 static void
1159 edit_left_delete_word (WEdit *edit)
1161 while (edit->buffer.curs1 > 0)
1163 int c1, c2;
1165 c1 = edit_backspace (edit, TRUE);
1166 if (c1 == '\n')
1167 break;
1168 c2 = edit_buffer_get_previous_byte (&edit->buffer);
1169 if (c2 == '\n')
1170 break;
1171 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1172 break;
1173 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1174 break;
1178 /* --------------------------------------------------------------------------------------------- */
1180 the start column position is not recorded, and hence does not
1181 undo as it happed. But who would notice.
1184 static void
1185 edit_do_undo (WEdit *edit)
1187 long ac;
1188 long count = 0;
1190 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1191 edit->over_col = 0;
1193 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1195 off_t b;
1197 switch ((int) ac)
1199 case STACK_BOTTOM:
1200 goto done_undo;
1201 case CURS_RIGHT:
1202 edit_cursor_move (edit, 1);
1203 break;
1204 case CURS_LEFT:
1205 edit_cursor_move (edit, -1);
1206 break;
1207 case BACKSPACE:
1208 case BACKSPACE_BR:
1209 edit_backspace (edit, TRUE);
1210 break;
1211 case DELCHAR:
1212 case DELCHAR_BR:
1213 edit_delete (edit, TRUE);
1214 break;
1215 case COLUMN_ON:
1216 edit->column_highlight = 1;
1217 break;
1218 case COLUMN_OFF:
1219 edit->column_highlight = 0;
1220 break;
1221 default:
1222 break;
1224 if (ac >= 256 && ac < 512)
1225 edit_insert_ahead (edit, ac - 256);
1226 if (ac >= 0 && ac < 256)
1227 edit_insert (edit, ac);
1229 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1231 edit->mark1 = ac - MARK_1;
1232 b = edit_buffer_get_bol (&edit->buffer, edit->mark1);
1233 edit->column1 = (long) edit_move_forward3 (edit, b, 0, edit->mark1);
1235 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1237 edit->mark2 = ac - MARK_2;
1238 b = edit_buffer_get_bol (&edit->buffer, edit->mark2);
1239 edit->column2 = (long) edit_move_forward3 (edit, b, 0, edit->mark2);
1241 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1243 edit->end_mark_curs = ac - MARK_CURS;
1245 if (count++)
1246 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1249 if (edit->start_display > ac - KEY_PRESS)
1251 edit->start_line -=
1252 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1253 edit->force |= REDRAW_PAGE;
1255 else if (edit->start_display < ac - KEY_PRESS)
1257 edit->start_line +=
1258 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1259 edit->force |= REDRAW_PAGE;
1261 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1262 edit_update_curs_row (edit);
1264 done_undo:
1265 edit->undo_stack_disable = 0;
1268 /* --------------------------------------------------------------------------------------------- */
1270 static void
1271 edit_do_redo (WEdit *edit)
1273 long ac;
1274 long count = 0;
1276 if (edit->redo_stack_reset)
1277 return;
1279 edit->over_col = 0;
1281 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1283 off_t b;
1285 switch ((int) ac)
1287 case STACK_BOTTOM:
1288 goto done_redo;
1289 case CURS_RIGHT:
1290 edit_cursor_move (edit, 1);
1291 break;
1292 case CURS_LEFT:
1293 edit_cursor_move (edit, -1);
1294 break;
1295 case BACKSPACE:
1296 edit_backspace (edit, TRUE);
1297 break;
1298 case DELCHAR:
1299 edit_delete (edit, TRUE);
1300 break;
1301 case COLUMN_ON:
1302 edit->column_highlight = 1;
1303 break;
1304 case COLUMN_OFF:
1305 edit->column_highlight = 0;
1306 break;
1307 default:
1308 break;
1310 if (ac >= 256 && ac < 512)
1311 edit_insert_ahead (edit, ac - 256);
1312 if (ac >= 0 && ac < 256)
1313 edit_insert (edit, ac);
1315 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1317 edit->mark1 = ac - MARK_1;
1318 b = edit_buffer_get_bol (&edit->buffer, edit->mark1);
1319 edit->column1 = (long) edit_move_forward3 (edit, b, 0, edit->mark1);
1321 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1323 edit->mark2 = ac - MARK_2;
1324 b = edit_buffer_get_bol (&edit->buffer, edit->mark2);
1325 edit->column2 = (long) edit_move_forward3 (edit, b, 0, edit->mark2);
1327 /* more than one pop usually means something big */
1328 if (count++ != 0)
1329 edit->force |= REDRAW_PAGE;
1332 if (edit->start_display > ac - KEY_PRESS)
1334 edit->start_line -=
1335 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1336 edit->force |= REDRAW_PAGE;
1338 else if (edit->start_display < ac - KEY_PRESS)
1340 edit->start_line +=
1341 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1342 edit->force |= REDRAW_PAGE;
1344 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1345 edit_update_curs_row (edit);
1347 done_redo:
1351 /* --------------------------------------------------------------------------------------------- */
1353 static void
1354 edit_group_undo (WEdit *edit)
1356 long ac = KEY_PRESS;
1357 long cur_ac = KEY_PRESS;
1359 while (ac != STACK_BOTTOM && ac == cur_ac)
1361 cur_ac = get_prev_undo_action (edit);
1362 edit_do_undo (edit);
1363 ac = get_prev_undo_action (edit);
1364 /* exit from cycle if edit_options.group_undo is not set,
1365 * and make single UNDO operation
1367 if (!edit_options.group_undo)
1368 ac = STACK_BOTTOM;
1372 /* --------------------------------------------------------------------------------------------- */
1374 static void
1375 edit_delete_to_line_end (WEdit *edit)
1377 while (edit_buffer_get_current_byte (&edit->buffer) != '\n' && edit->buffer.curs2 != 0)
1378 edit_delete (edit, TRUE);
1381 /* --------------------------------------------------------------------------------------------- */
1383 static void
1384 edit_delete_to_line_begin (WEdit *edit)
1386 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 != 0)
1387 edit_backspace (edit, TRUE);
1390 /* --------------------------------------------------------------------------------------------- */
1392 static gboolean
1393 is_aligned_on_a_tab (WEdit *edit)
1395 long curs_col;
1397 edit_update_curs_col (edit);
1398 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1399 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1402 /* --------------------------------------------------------------------------------------------- */
1404 static gboolean
1405 right_of_four_spaces (WEdit *edit)
1407 int i;
1408 int ch = 0;
1410 for (i = 1; i <= HALF_TAB_SIZE; i++)
1411 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - i);
1413 return (ch == ' ' && is_aligned_on_a_tab (edit));
1416 /* --------------------------------------------------------------------------------------------- */
1418 static gboolean
1419 left_of_four_spaces (WEdit *edit)
1421 int i, ch = 0;
1423 for (i = 0; i < HALF_TAB_SIZE; i++)
1424 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 + i);
1426 return (ch == ' ' && is_aligned_on_a_tab (edit));
1429 /* --------------------------------------------------------------------------------------------- */
1431 static void
1432 edit_auto_indent (WEdit *edit)
1434 off_t p;
1436 p = edit->buffer.curs1;
1437 /* use the previous line as a template */
1438 p = edit_buffer_get_backward_offset (&edit->buffer, p, 1);
1439 /* copy the leading whitespace of the line */
1440 while (TRUE)
1441 { /* no range check - the line _is_ \n-terminated */
1442 char c;
1444 c = edit_buffer_get_byte (&edit->buffer, p++);
1445 if (!whitespace (c))
1446 break;
1447 edit_insert (edit, c);
1451 /* --------------------------------------------------------------------------------------------- */
1453 static inline void
1454 edit_double_newline (WEdit *edit)
1456 edit_insert (edit, '\n');
1457 if (edit_buffer_get_current_byte (&edit->buffer) == '\n'
1458 || edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - 2) == '\n')
1459 return;
1460 edit->force |= REDRAW_PAGE;
1461 edit_insert (edit, '\n');
1464 /* --------------------------------------------------------------------------------------------- */
1466 static void
1467 insert_spaces_tab (WEdit *edit, gboolean half)
1469 long i;
1471 edit_update_curs_col (edit);
1472 i = TAB_SIZE * space_width;
1473 if (half)
1474 i /= 2;
1475 if (i != 0)
1476 for (i = ((edit->curs_col / i) + 1) * i - edit->curs_col; i > 0; i -= space_width)
1477 edit_insert (edit, ' ');
1480 /* --------------------------------------------------------------------------------------------- */
1482 static inline void
1483 edit_tab_cmd (WEdit *edit)
1485 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer))
1487 /* insert a half tab (usually four spaces) unless there is a
1488 half tab already behind, then delete it and insert a
1489 full tab. */
1490 if (edit_options.fill_tabs_with_spaces || !right_of_four_spaces (edit))
1491 insert_spaces_tab (edit, TRUE);
1492 else
1494 int i;
1496 for (i = 1; i <= HALF_TAB_SIZE; i++)
1497 edit_backspace (edit, TRUE);
1498 edit_insert (edit, '\t');
1501 else if (edit_options.fill_tabs_with_spaces)
1502 insert_spaces_tab (edit, FALSE);
1503 else
1504 edit_insert (edit, '\t');
1507 /* --------------------------------------------------------------------------------------------- */
1509 static void
1510 check_and_wrap_line (WEdit *edit)
1512 off_t curs;
1514 if (!edit_options.typewriter_wrap)
1515 return;
1516 edit_update_curs_col (edit);
1517 if (edit->curs_col < edit_options.word_wrap_line_length)
1518 return;
1519 curs = edit->buffer.curs1;
1520 while (TRUE)
1522 int c;
1524 curs--;
1525 c = edit_buffer_get_byte (&edit->buffer, curs);
1526 if (c == '\n' || curs <= 0)
1528 edit_insert (edit, '\n');
1529 return;
1531 if (whitespace (c))
1533 off_t current = edit->buffer.curs1;
1534 edit_cursor_move (edit, curs - edit->buffer.curs1 + 1);
1535 edit_insert (edit, '\n');
1536 edit_cursor_move (edit, current - edit->buffer.curs1 + 1);
1537 return;
1542 /* --------------------------------------------------------------------------------------------- */
1543 /** this find the matching bracket in either direction, and sets edit->bracket
1545 * @param edit editor object
1546 * @param in_screen search only on the current screen
1547 * @param furthest_bracket_search count of the bytes for search
1549 * @return position of the found bracket (-1 if no match)
1552 static off_t
1553 edit_get_bracket (WEdit *edit, gboolean in_screen, unsigned long furthest_bracket_search)
1555 const char *const b = "{}{[][()(", *p;
1556 int i = 1, inc = -1, c, d, n = 0;
1557 unsigned long j = 0;
1558 off_t q;
1560 edit_update_curs_row (edit);
1561 c = edit_buffer_get_current_byte (&edit->buffer);
1562 p = strchr (b, c);
1563 /* not on a bracket at all */
1564 if (p == NULL || *p == '\0')
1565 return -1;
1566 /* the matching bracket */
1567 d = p[1];
1568 /* going left or right? */
1569 if (strchr ("{[(", c) != NULL)
1570 inc = 1;
1571 /* no limit */
1572 if (furthest_bracket_search == 0)
1573 furthest_bracket_search--; /* ULONG_MAX */
1574 for (q = edit->buffer.curs1 + inc;; q += inc)
1576 int a;
1578 /* out of buffer? */
1579 if (q >= edit->buffer.size || q < 0)
1580 break;
1581 a = edit_buffer_get_byte (&edit->buffer, q);
1582 /* don't want to eat CPU */
1583 if (j++ > furthest_bracket_search)
1584 break;
1585 /* out of screen? */
1586 if (in_screen)
1588 if (q < edit->start_display)
1589 break;
1590 /* count lines if searching downward */
1591 if (inc > 0 && a == '\n')
1592 if (n++ >= WIDGET (edit)->rect.lines - edit->curs_row) /* out of screen */
1593 break;
1595 /* count bracket depth */
1596 i += (a == c) - (a == d);
1597 /* return if bracket depth is zero */
1598 if (i == 0)
1599 return q;
1601 /* no match */
1602 return -1;
1605 /* --------------------------------------------------------------------------------------------- */
1607 static inline void
1608 edit_goto_matching_bracket (WEdit *edit)
1610 off_t q;
1612 q = edit_get_bracket (edit, 0, 0);
1613 if (q >= 0)
1615 edit->bracket = edit->buffer.curs1;
1616 edit->force |= REDRAW_PAGE;
1617 edit_cursor_move (edit, q - edit->buffer.curs1);
1621 /* --------------------------------------------------------------------------------------------- */
1623 static void
1624 edit_move_block_to_right (WEdit *edit)
1626 off_t start_mark, end_mark;
1627 long cur_bol, start_bol;
1629 if (!eval_marks (edit, &start_mark, &end_mark))
1630 return;
1632 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1633 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1637 off_t b;
1639 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1640 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
1642 if (edit_options.fill_tabs_with_spaces)
1643 insert_spaces_tab (edit, edit_options.fake_half_tabs);
1644 else
1645 edit_insert (edit, '\t');
1647 b = edit_buffer_get_bol (&edit->buffer, cur_bol);
1648 edit_cursor_move (edit, b - edit->buffer.curs1);
1651 if (cur_bol == 0)
1652 break;
1654 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1656 while (cur_bol >= start_bol);
1658 edit->force |= REDRAW_PAGE;
1661 /* --------------------------------------------------------------------------------------------- */
1663 static void
1664 edit_move_block_to_left (WEdit *edit)
1666 off_t start_mark, end_mark;
1667 off_t cur_bol, start_bol;
1669 if (!eval_marks (edit, &start_mark, &end_mark))
1670 return;
1672 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1673 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1677 int del_tab_width;
1678 int next_char;
1680 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1682 del_tab_width = edit_options.fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE;
1684 next_char = edit_buffer_get_current_byte (&edit->buffer);
1685 if (next_char == '\t')
1686 edit_delete (edit, TRUE);
1687 else if (next_char == ' ')
1689 int i;
1691 for (i = 0; i < del_tab_width; i++)
1693 if (next_char == ' ')
1694 edit_delete (edit, TRUE);
1695 next_char = edit_buffer_get_current_byte (&edit->buffer);
1699 if (cur_bol == 0)
1700 break;
1702 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1704 while (cur_bol >= start_bol);
1706 edit->force |= REDRAW_PAGE;
1709 /* --------------------------------------------------------------------------------------------- */
1711 * prints at the cursor
1712 * @return number of chars printed
1715 static size_t
1716 edit_print_string (WEdit *e, const char *s)
1718 size_t i;
1720 for (i = 0; s[i] != '\0'; i++)
1721 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i]);
1722 e->force |= REDRAW_COMPLETELY;
1723 edit_update_screen (e);
1724 return i;
1727 /* --------------------------------------------------------------------------------------------- */
1729 static off_t
1730 edit_insert_column_from_file (WEdit *edit, int file, off_t *start_pos, off_t *end_pos,
1731 long *col1, long *col2)
1733 off_t cursor;
1734 long col;
1735 off_t blocklen = -1, width = 0;
1736 unsigned char *data;
1738 cursor = edit->buffer.curs1;
1739 col = edit_get_col (edit);
1740 data = g_malloc0 (TEMP_BUF_LEN);
1742 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1744 off_t i;
1745 char *pn;
1747 pn = strchr ((char *) data, '\n');
1748 width = pn == NULL ? blocklen : pn - (char *) data;
1750 for (i = 0; i < blocklen; i++)
1752 if (data[i] != '\n')
1753 edit_insert (edit, data[i]);
1754 else
1755 { /* fill in and move to next line */
1756 long l;
1757 off_t p;
1759 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1760 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1761 edit_insert (edit, ' ');
1763 for (p = edit->buffer.curs1;; p++)
1765 if (p == edit->buffer.size)
1767 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1768 edit_insert_ahead (edit, '\n');
1769 p++;
1770 break;
1772 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1774 p++;
1775 break;
1779 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1781 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1782 edit_insert (edit, ' ');
1786 *col1 = col;
1787 *col2 = col + width;
1788 *start_pos = cursor;
1789 *end_pos = edit->buffer.curs1;
1790 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1791 g_free (data);
1793 return blocklen;
1796 /* --------------------------------------------------------------------------------------------- */
1797 /*** public functions ****************************************************************************/
1798 /* --------------------------------------------------------------------------------------------- */
1800 /** User edit menu, like user menu (F2) but only in editor. */
1802 void
1803 edit_user_menu (WEdit *edit, const char *menu_file, int selected_entry)
1805 char *block_file;
1806 gboolean mark;
1807 off_t curs;
1808 off_t start_mark, end_mark;
1809 struct stat status;
1810 vfs_path_t *block_file_vpath;
1812 block_file = mc_config_get_full_path (EDIT_HOME_BLOCK_FILE);
1813 block_file_vpath = vfs_path_from_str (block_file);
1814 curs = edit->buffer.curs1;
1815 mark = eval_marks (edit, &start_mark, &end_mark);
1816 if (mark)
1817 edit_save_block (edit, block_file, start_mark, end_mark);
1819 /* run shell scripts from menu */
1820 if (user_menu_cmd (CONST_WIDGET (edit), menu_file, selected_entry)
1821 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1823 gboolean rc = TRUE;
1824 FILE *fd;
1826 /* i.e. we have marked block */
1827 if (mark)
1828 rc = edit_block_delete_cmd (edit);
1830 if (rc)
1832 off_t ins_len;
1834 ins_len = edit_insert_file (edit, block_file_vpath);
1835 if (mark && ins_len > 0)
1836 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1838 /* truncate block file */
1839 fd = fopen (block_file, "w");
1840 if (fd != NULL)
1841 fclose (fd);
1843 g_free (block_file);
1844 vfs_path_free (block_file_vpath, TRUE);
1846 edit_cursor_move (edit, curs - edit->buffer.curs1);
1847 edit->force |= REDRAW_PAGE;
1848 widget_draw (WIDGET (edit));
1851 /* --------------------------------------------------------------------------------------------- */
1853 char *
1854 edit_get_write_filter (const vfs_path_t *write_name_vpath, const vfs_path_t *filename_vpath)
1856 int i;
1857 const char *write_name;
1858 char *write_name_quoted;
1859 char *p = NULL;
1861 i = edit_find_filter (filename_vpath);
1862 if (i < 0)
1863 return NULL;
1865 write_name = vfs_path_get_last_path_str (write_name_vpath);
1866 write_name_quoted = name_quote (write_name, FALSE);
1867 if (write_name_quoted != NULL)
1869 p = g_strdup_printf (all_filters[i].write, write_name_quoted);
1870 g_free (write_name_quoted);
1872 return p;
1875 /* --------------------------------------------------------------------------------------------- */
1877 * @param edit editor object
1878 * @param f value of stream file
1879 * @return the length of the file
1882 off_t
1883 edit_write_stream (WEdit *edit, FILE *f)
1885 long i;
1887 if (edit->lb == LB_ASIS)
1889 for (i = 0; i < edit->buffer.size; i++)
1890 if (fputc (edit_buffer_get_byte (&edit->buffer, i), f) < 0)
1891 break;
1892 return i;
1895 /* change line breaks */
1896 for (i = 0; i < edit->buffer.size; i++)
1898 unsigned char c;
1900 c = edit_buffer_get_byte (&edit->buffer, i);
1901 if (!(c == '\n' || c == '\r'))
1903 /* not line break */
1904 if (fputc (c, f) < 0)
1905 return i;
1907 else
1908 { /* (c == '\n' || c == '\r') */
1909 unsigned char c1;
1911 c1 = edit_buffer_get_byte (&edit->buffer, i + 1); /* next char */
1913 switch (edit->lb)
1915 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1916 /* put one line break unconditionally */
1917 if (fputc ('\n', f) < 0)
1918 return i;
1920 i++; /* 2 chars are processed */
1922 if (c == '\r' && c1 == '\n')
1923 /* Windows line break; go to the next char */
1924 break;
1926 if (c == '\r' && c1 == '\r')
1928 /* two Macintosh line breaks; put second line break */
1929 if (fputc ('\n', f) < 0)
1930 return i;
1931 break;
1934 if (fputc (c1, f) < 0)
1935 return i;
1936 break;
1938 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1939 /* put one line break unconditionally */
1940 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1941 return i;
1943 if (c == '\r' && c1 == '\n')
1944 /* Windows line break; go to the next char */
1945 i++;
1946 break;
1948 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1949 /* put one line break unconditionally */
1950 if (fputc ('\r', f) < 0)
1951 return i;
1953 i++; /* 2 chars are processed */
1955 if (c == '\r' && c1 == '\n')
1956 /* Windows line break; go to the next char */
1957 break;
1959 if (c == '\n' && c1 == '\n')
1961 /* two Windows line breaks; put second line break */
1962 if (fputc ('\r', f) < 0)
1963 return i;
1964 break;
1967 if (fputc (c1, f) < 0)
1968 return i;
1969 break;
1970 case LB_ASIS: /* default without changes */
1971 default:
1972 break;
1977 return edit->buffer.size;
1980 /* --------------------------------------------------------------------------------------------- */
1982 gboolean
1983 is_break_char (char c)
1985 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c) != NULL);
1988 /* --------------------------------------------------------------------------------------------- */
1989 /** inserts a file at the cursor, returns count of inserted bytes on success */
1991 off_t
1992 edit_insert_file (WEdit *edit, const vfs_path_t *filename_vpath)
1994 char *p;
1995 off_t current;
1996 off_t ins_len = 0;
1998 p = edit_get_filter (filename_vpath);
1999 current = edit->buffer.curs1;
2001 if (p != NULL)
2003 FILE *f;
2005 f = (FILE *) popen (p, "r");
2006 if (f != NULL)
2008 edit_insert_stream (edit, f);
2010 /* Place cursor at the end of text selection */
2011 if (!edit_options.cursor_after_inserted_block)
2013 ins_len = edit->buffer.curs1 - current;
2014 edit_cursor_move (edit, -ins_len);
2016 if (pclose (f) > 0)
2018 char *errmsg;
2020 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2021 edit_error_dialog (_("Error"), errmsg);
2022 g_free (errmsg);
2023 ins_len = -1;
2026 else
2028 char *errmsg;
2030 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2031 edit_error_dialog (_("Error"), errmsg);
2032 g_free (errmsg);
2033 ins_len = -1;
2035 g_free (p);
2037 else
2039 int file;
2040 off_t blocklen;
2041 gboolean vertical_insertion = FALSE;
2042 char *buf;
2044 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2045 if (file == -1)
2046 return -1;
2048 buf = g_malloc0 (TEMP_BUF_LEN);
2049 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2050 if (blocklen > 0)
2052 /* if contain signature VERTICAL_MAGIC then it vertical block */
2053 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2054 vertical_insertion = TRUE;
2055 else
2056 mc_lseek (file, 0, SEEK_SET);
2059 if (vertical_insertion)
2061 off_t mark1, mark2;
2062 long c1, c2;
2064 blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2065 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2067 /* highlight inserted text then not persistent blocks */
2068 if (!edit_options.persistent_selections && edit->modified)
2070 if (!edit->column_highlight)
2071 edit_push_undo_action (edit, COLUMN_OFF);
2072 edit->column_highlight = 1;
2075 else
2077 off_t i;
2079 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2081 for (i = 0; i < blocklen; i++)
2082 edit_insert (edit, buf[i]);
2084 /* highlight inserted text then not persistent blocks */
2085 if (!edit_options.persistent_selections && edit->modified)
2087 edit_set_markers (edit, edit->buffer.curs1, current, 0, 0);
2088 if (edit->column_highlight)
2089 edit_push_undo_action (edit, COLUMN_ON);
2090 edit->column_highlight = 0;
2093 /* Place cursor at the end of text selection */
2094 if (!edit_options.cursor_after_inserted_block)
2096 ins_len = edit->buffer.curs1 - current;
2097 edit_cursor_move (edit, -ins_len);
2101 edit->force |= REDRAW_PAGE;
2102 g_free (buf);
2103 mc_close (file);
2104 if (blocklen != 0)
2105 ins_len = 0;
2108 return ins_len;
2111 /* --------------------------------------------------------------------------------------------- */
2113 * Fill in the edit structure. Return NULL on failure. Pass edit as
2114 * NULL to allocate a new structure.
2116 * If arg is NULL or arg->line_number is 0, try to restore saved position. Otherwise put the
2117 * cursor on that line and show it in the middle of the screen.
2120 WEdit *
2121 edit_init (WEdit *edit, const WRect *r, const edit_arg_t *arg)
2123 gboolean to_free = FALSE;
2124 long line;
2126 auto_syntax = TRUE; /* Resetting to auto on every invocation */
2127 edit_options.line_state_width = edit_options.line_state ? LINE_STATE_WIDTH : 0;
2129 if (edit != NULL)
2131 gboolean fullscreen;
2132 WRect loc_prev;
2134 /* save some widget parameters */
2135 fullscreen = edit->fullscreen;
2136 loc_prev = edit->loc_prev;
2138 edit_purge_widget (edit);
2140 /* restore saved parameters */
2141 edit->fullscreen = fullscreen;
2142 edit->loc_prev = loc_prev;
2144 else
2146 Widget *w;
2148 edit = g_malloc0 (sizeof (WEdit));
2149 to_free = TRUE;
2151 w = WIDGET (edit);
2152 widget_init (w, r, NULL, NULL);
2153 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT | WOP_WANT_CURSOR;
2154 w->keymap = editor_map;
2155 w->ext_keymap = editor_x_map;
2156 edit->fullscreen = TRUE;
2157 edit_save_size (edit);
2160 edit->drag_state = MCEDIT_DRAG_NONE;
2162 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2163 edit->stat1.st_uid = getuid ();
2164 edit->stat1.st_gid = getgid ();
2165 edit->stat1.st_mtime = 0;
2167 if (arg != NULL)
2168 edit->attrs_ok = (mc_fgetflags (arg->file_vpath, &edit->attrs) == 0);
2169 else
2170 edit->attrs_ok = FALSE;
2172 edit->over_col = 0;
2173 edit->bracket = -1;
2174 edit->last_bracket = -1;
2175 edit->force |= REDRAW_PAGE;
2177 /* set file name before load file */
2178 if (arg != NULL)
2180 edit_set_filename (edit, arg->file_vpath);
2181 line = arg->line_number;
2183 else
2185 edit_set_filename (edit, NULL);
2186 line = 0;
2189 edit->undo_stack_size = START_STACK_SIZE;
2190 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2191 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2193 edit->redo_stack_size = START_STACK_SIZE;
2194 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2195 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2197 #ifdef HAVE_CHARSET
2198 edit->utf8 = FALSE;
2199 edit->converter = str_cnv_from_term;
2200 edit_set_codeset (edit);
2201 #endif
2203 if (!edit_load_file (edit))
2205 /* edit_load_file already gives an error message */
2206 if (to_free)
2207 g_free (edit);
2208 return NULL;
2211 edit->loading_done = 1;
2212 edit->modified = 0;
2213 edit->locked = 0;
2214 edit_load_syntax (edit, NULL, NULL);
2215 edit_get_syntax_color (edit, -1);
2217 /* load saved cursor position and/or boolmarks */
2218 if ((line == 0) && edit_options.save_position)
2219 edit_load_position (edit, TRUE);
2220 else
2222 edit_load_position (edit, FALSE);
2223 if (line <= 0)
2224 line = 1;
2225 edit_move_display (edit, line - 1);
2226 edit_move_to_line (edit, line - 1);
2229 edit_load_macro_cmd (edit);
2231 return edit;
2234 /* --------------------------------------------------------------------------------------------- */
2236 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2237 gboolean
2238 edit_clean (WEdit *edit)
2240 if (edit == NULL)
2241 return FALSE;
2243 /* a stale lock, remove it */
2244 if (edit->locked)
2245 (void) unlock_file (edit->filename_vpath);
2247 /* save cursor position */
2248 if (edit_options.save_position)
2249 edit_save_position (edit);
2250 else if (edit->serialized_bookmarks != NULL)
2251 g_array_free (edit->serialized_bookmarks, TRUE);
2253 /* File specified on the mcedit command line and never saved */
2254 if (edit->delete_file)
2255 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2257 edit_free_syntax_rules (edit);
2258 book_mark_flush (edit, -1);
2260 edit_buffer_clean (&edit->buffer);
2262 g_free (edit->undo_stack);
2263 g_free (edit->redo_stack);
2264 vfs_path_free (edit->filename_vpath, TRUE);
2265 vfs_path_free (edit->dir_vpath, TRUE);
2266 edit_search_deinit (edit);
2268 #ifdef HAVE_CHARSET
2269 if (edit->converter != str_cnv_from_term)
2270 str_close_conv (edit->converter);
2271 #endif
2273 edit_purge_widget (edit);
2275 return TRUE;
2278 /* --------------------------------------------------------------------------------------------- */
2281 * Load a new file into the editor and set line. If it fails, preserve the old file.
2282 * To do it, allocate a new widget, initialize it and, if the new file
2283 * was loaded, copy the data to the old widget.
2285 * @return TRUE on success, FALSE on failure.
2287 gboolean
2288 edit_reload_line (WEdit *edit, const edit_arg_t *arg)
2290 Widget *w = WIDGET (edit);
2291 WEdit *e;
2293 e = g_malloc0 (sizeof (WEdit));
2294 *WIDGET (e) = *w;
2295 /* save some widget parameters */
2296 e->fullscreen = edit->fullscreen;
2297 e->loc_prev = edit->loc_prev;
2299 if (edit_init (e, &w->rect, arg) == NULL)
2301 g_free (e);
2302 return FALSE;
2305 edit_clean (edit);
2306 memcpy (edit, e, sizeof (*edit));
2307 g_free (e);
2309 return TRUE;
2312 /* --------------------------------------------------------------------------------------------- */
2314 #ifdef HAVE_CHARSET
2315 void
2316 edit_set_codeset (WEdit *edit)
2318 const char *cp_id;
2320 cp_id =
2321 get_codepage_id (mc_global.source_codepage >=
2322 0 ? mc_global.source_codepage : mc_global.display_codepage);
2324 if (cp_id != NULL)
2326 GIConv conv;
2327 conv = str_crt_conv_from (cp_id);
2328 if (conv != INVALID_CONV)
2330 if (edit->converter != str_cnv_from_term)
2331 str_close_conv (edit->converter);
2332 edit->converter = conv;
2336 if (cp_id != NULL)
2337 edit->utf8 = str_isutf8 (cp_id);
2339 #endif
2341 /* --------------------------------------------------------------------------------------------- */
2344 * Recording stack for undo:
2345 * The following is an implementation of a compressed stack. Identical
2346 * pushes are recorded by a negative prefix indicating the number of times the
2347 * same char was pushed. This saves space for repeated curs-left or curs-right
2348 * delete etc.
2350 * eg:
2352 * pushed: stored:
2355 * b a
2356 * b -3
2357 * b b
2358 * c --> -4
2359 * c c
2360 * c d
2364 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2365 * 256-512 is an insert ahead (from a delete), If it is between 600 and 700 it is one
2366 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2367 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2368 * position.
2370 * The only way the cursor moves or the buffer is changed is through the routines:
2371 * insert, backspace, insert_ahead, delete, and cursor_move.
2372 * These record the reverse undo movements onto the stack each time they are
2373 * called.
2375 * Each key press results in a set of actions (insert; delete ...). So each time
2376 * a key is pressed the current position of start_display is pushed as
2377 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2378 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2379 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2383 * @param edit editor object
2384 * @param c code of the action
2387 void
2388 edit_push_undo_action (WEdit *edit, long c)
2390 unsigned long sp = edit->undo_stack_pointer;
2391 unsigned long spm1;
2393 /* first enlarge the stack if necessary */
2394 if (sp > edit->undo_stack_size - 10)
2395 { /* say */
2396 if (max_undo < 256)
2397 max_undo = 256;
2398 if (edit->undo_stack_size < (unsigned long) max_undo)
2400 long *t;
2402 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2403 if (t != NULL)
2405 edit->undo_stack = t;
2406 edit->undo_stack_size <<= 1;
2407 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2411 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2412 if (edit->undo_stack_disable)
2414 edit_push_redo_action (edit, KEY_PRESS);
2415 edit_push_redo_action (edit, c);
2416 return;
2419 if (edit->redo_stack_reset)
2420 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2422 if (edit->undo_stack_bottom != sp
2423 && spm1 != edit->undo_stack_bottom
2424 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2426 long d;
2428 if (edit->undo_stack[spm1] < 0)
2430 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2431 if (d == c && edit->undo_stack[spm1] > -1000000000)
2433 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2434 edit->undo_stack[spm1]--;
2435 return;
2438 else
2440 d = edit->undo_stack[spm1];
2441 if (d == c)
2443 if (c >= KEY_PRESS)
2444 return; /* --> no need to push multiple do-nothings */
2445 edit->undo_stack[sp] = -2;
2446 goto check_bottom;
2450 edit->undo_stack[sp] = c;
2452 check_bottom:
2453 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2455 /* if the sp wraps round and catches the undo_stack_bottom then erase
2456 * the first set of actions on the stack to make space - by moving
2457 * undo_stack_bottom forward one "key press" */
2458 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2459 if ((unsigned long) c == edit->undo_stack_bottom ||
2460 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2463 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2465 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2466 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2468 /*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: */
2469 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2470 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2472 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2476 /* --------------------------------------------------------------------------------------------- */
2478 void
2479 edit_push_redo_action (WEdit *edit, long c)
2481 unsigned long sp = edit->redo_stack_pointer;
2482 unsigned long spm1;
2483 /* first enlarge the stack if necessary */
2484 if (sp > edit->redo_stack_size - 10)
2485 { /* say */
2486 if (max_undo < 256)
2487 max_undo = 256;
2488 if (edit->redo_stack_size < (unsigned long) max_undo)
2490 long *t;
2492 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2493 if (t != NULL)
2495 edit->redo_stack = t;
2496 edit->redo_stack_size <<= 1;
2497 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2501 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2503 if (edit->redo_stack_bottom != sp
2504 && spm1 != edit->redo_stack_bottom
2505 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2507 long d;
2509 if (edit->redo_stack[spm1] < 0)
2511 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2512 if (d == c && edit->redo_stack[spm1] > -1000000000)
2514 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2515 edit->redo_stack[spm1]--;
2516 return;
2519 else
2521 d = edit->redo_stack[spm1];
2522 if (d == c)
2524 if (c >= KEY_PRESS)
2525 return; /* --> no need to push multiple do-nothings */
2526 edit->redo_stack[sp] = -2;
2527 goto redo_check_bottom;
2531 edit->redo_stack[sp] = c;
2533 redo_check_bottom:
2534 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2536 /* if the sp wraps round and catches the redo_stack_bottom then erase
2537 * the first set of actions on the stack to make space - by moving
2538 * redo_stack_bottom forward one "key press" */
2539 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2540 if ((unsigned long) c == edit->redo_stack_bottom ||
2541 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2544 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2546 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2547 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2550 * If a single key produced enough pushes to wrap all the way round then
2551 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2552 * The stack is then initialised:
2555 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2556 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2557 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2560 /* --------------------------------------------------------------------------------------------- */
2562 Basic low level single character buffer alterations and movements at the cursor.
2565 void
2566 edit_insert (WEdit *edit, int c)
2568 /* first we must update the position of the display window */
2569 if (edit->buffer.curs1 < edit->start_display)
2571 edit->start_display++;
2572 if (c == '\n')
2573 edit->start_line++;
2576 /* Mark file as modified, unless the file hasn't been fully loaded */
2577 if (edit->loading_done)
2578 edit_modification (edit);
2580 /* now we must update some info on the file and check if a redraw is required */
2581 if (c == '\n')
2583 book_mark_inc (edit, edit->buffer.curs_line);
2584 edit->buffer.curs_line++;
2585 edit->buffer.lines++;
2586 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2589 /* save the reverse command onto the undo stack */
2590 /* ordinary char and not space */
2591 if (c > 32)
2592 edit_push_undo_action (edit, BACKSPACE);
2593 else
2594 edit_push_undo_action (edit, BACKSPACE_BR);
2595 /* update markers */
2596 edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2597 edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2598 edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2600 edit_buffer_insert (&edit->buffer, c);
2603 /* --------------------------------------------------------------------------------------------- */
2604 /** same as edit_insert and move left */
2606 void
2607 edit_insert_ahead (WEdit *edit, int c)
2609 if (edit->buffer.curs1 < edit->start_display)
2611 edit->start_display++;
2612 if (c == '\n')
2613 edit->start_line++;
2615 edit_modification (edit);
2616 if (c == '\n')
2618 book_mark_inc (edit, edit->buffer.curs_line);
2619 edit->buffer.lines++;
2620 edit->force |= REDRAW_AFTER_CURSOR;
2622 /* ordinary char and not space */
2623 if (c > 32)
2624 edit_push_undo_action (edit, DELCHAR);
2625 else
2626 edit_push_undo_action (edit, DELCHAR_BR);
2628 edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2629 edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2630 edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2632 edit_buffer_insert_ahead (&edit->buffer, c);
2635 /* --------------------------------------------------------------------------------------------- */
2637 void
2638 edit_insert_over (WEdit *edit)
2640 long i;
2642 for (i = 0; i < edit->over_col; i++)
2643 edit_insert (edit, ' ');
2645 edit->over_col = 0;
2648 /* --------------------------------------------------------------------------------------------- */
2651 edit_delete (WEdit *edit, gboolean byte_delete)
2653 int p = 0;
2654 int char_length = 1;
2655 int i;
2657 if (edit->buffer.curs2 == 0)
2658 return 0;
2660 #ifdef HAVE_CHARSET
2661 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2662 if (edit->utf8 && !byte_delete)
2664 edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2665 if (char_length < 1)
2666 char_length = 1;
2668 #else
2669 (void) byte_delete;
2670 #endif
2672 if (edit->mark2 != edit->mark1)
2673 edit_push_markers (edit);
2675 for (i = 1; i <= char_length; i++)
2677 if (edit->mark1 > edit->buffer.curs1)
2679 edit->mark1--;
2680 edit->end_mark_curs--;
2682 if (edit->mark2 > edit->buffer.curs1)
2683 edit->mark2--;
2684 if (edit->last_get_rule > edit->buffer.curs1)
2685 edit->last_get_rule--;
2687 p = edit_buffer_delete (&edit->buffer);
2689 edit_push_undo_action (edit, p + 256);
2692 edit_modification (edit);
2693 if (p == '\n')
2695 book_mark_dec (edit, edit->buffer.curs_line);
2696 edit->buffer.lines--;
2697 edit->force |= REDRAW_AFTER_CURSOR;
2699 if (edit->buffer.curs1 < edit->start_display)
2701 edit->start_display--;
2702 if (p == '\n')
2703 edit->start_line--;
2706 return p;
2709 /* --------------------------------------------------------------------------------------------- */
2712 edit_backspace (WEdit *edit, gboolean byte_delete)
2714 int p = 0;
2715 int char_length = 1;
2716 int i;
2718 if (edit->buffer.curs1 == 0)
2719 return 0;
2721 if (edit->mark2 != edit->mark1)
2722 edit_push_markers (edit);
2724 #ifdef HAVE_CHARSET
2725 if (edit->utf8 && !byte_delete)
2727 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2728 if (char_length < 1)
2729 char_length = 1;
2731 #else
2732 (void) byte_delete;
2733 #endif
2735 for (i = 1; i <= char_length; i++)
2737 if (edit->mark1 >= edit->buffer.curs1)
2739 edit->mark1--;
2740 edit->end_mark_curs--;
2742 if (edit->mark2 >= edit->buffer.curs1)
2743 edit->mark2--;
2744 if (edit->last_get_rule >= edit->buffer.curs1)
2745 edit->last_get_rule--;
2747 p = edit_buffer_backspace (&edit->buffer);
2749 edit_push_undo_action (edit, p);
2751 edit_modification (edit);
2752 if (p == '\n')
2754 book_mark_dec (edit, edit->buffer.curs_line);
2755 edit->buffer.curs_line--;
2756 edit->buffer.lines--;
2757 edit->force |= REDRAW_AFTER_CURSOR;
2760 if (edit->buffer.curs1 < edit->start_display)
2762 edit->start_display--;
2763 if (p == '\n')
2764 edit->start_line--;
2767 return p;
2770 /* --------------------------------------------------------------------------------------------- */
2771 /** moves the cursor right or left: increment positive or negative respectively */
2773 void
2774 edit_cursor_move (WEdit *edit, off_t increment)
2776 if (increment < 0)
2778 for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2780 int c;
2782 edit_push_undo_action (edit, CURS_RIGHT);
2784 c = edit_buffer_get_previous_byte (&edit->buffer);
2785 edit_buffer_insert_ahead (&edit->buffer, c);
2786 c = edit_buffer_backspace (&edit->buffer);
2787 if (c == '\n')
2789 edit->buffer.curs_line--;
2790 edit->force |= REDRAW_LINE_BELOW;
2794 else
2796 for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2798 int c;
2800 edit_push_undo_action (edit, CURS_LEFT);
2802 c = edit_buffer_get_current_byte (&edit->buffer);
2803 edit_buffer_insert (&edit->buffer, c);
2804 c = edit_buffer_delete (&edit->buffer);
2805 if (c == '\n')
2807 edit->buffer.curs_line++;
2808 edit->force |= REDRAW_LINE_ABOVE;
2814 /* --------------------------------------------------------------------------------------------- */
2815 /* If cols is zero this returns the count of columns from current to upto. */
2816 /* If upto is zero returns index of cols across from current. */
2818 off_t
2819 edit_move_forward3 (const WEdit *edit, off_t current, long cols, off_t upto)
2821 off_t p, q;
2822 long col;
2824 if (upto != 0)
2826 q = upto;
2827 cols = -10;
2829 else
2830 q = edit->buffer.size + 2;
2832 for (col = 0, p = current; p < q; p++)
2834 int c, orig_c;
2836 if (cols != -10)
2838 if (col == cols)
2839 return p;
2840 if (col > cols)
2841 return p - 1;
2844 orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2846 #ifdef HAVE_CHARSET
2847 if (edit->utf8)
2849 int utf_ch;
2850 int char_length = 1;
2852 utf_ch = edit_buffer_get_utf (&edit->buffer, p, &char_length);
2853 if (mc_global.utf8_display)
2855 if (char_length > 1)
2856 col -= char_length - 1;
2857 if (g_unichar_iswide (utf_ch))
2858 col++;
2860 else if (char_length > 1 && g_unichar_isprint (utf_ch))
2861 col -= char_length - 1;
2864 c = convert_to_display_c (c);
2865 #endif
2867 if (c == '\n')
2868 return (upto != 0 ? (off_t) col : p);
2869 if (c == '\t')
2870 col += TAB_SIZE - col % TAB_SIZE;
2871 else if ((c < 32 || c == 127) && (orig_c == c
2872 #ifdef HAVE_CHARSET
2873 || (!mc_global.utf8_display && !edit->utf8)
2874 #endif
2876 /* '\r' is shown as ^M, so we must advance 2 characters */
2877 /* Caret notation for control characters */
2878 col += 2;
2879 else
2880 col++;
2882 return (off_t) col;
2885 /* --------------------------------------------------------------------------------------------- */
2886 /** returns the current offset of the cursor from the beginning of a file */
2888 off_t
2889 edit_get_cursor_offset (const WEdit *edit)
2891 return edit->buffer.curs1;
2894 /* --------------------------------------------------------------------------------------------- */
2895 /** returns the current column position of the cursor */
2897 long
2898 edit_get_col (const WEdit *edit)
2900 off_t b;
2902 b = edit_buffer_get_current_bol (&edit->buffer);
2903 return (long) edit_move_forward3 (edit, b, 0, edit->buffer.curs1);
2906 /* --------------------------------------------------------------------------------------------- */
2907 /* Scrolling functions */
2908 /* --------------------------------------------------------------------------------------------- */
2910 void
2911 edit_update_curs_row (WEdit *edit)
2913 edit->curs_row = edit->buffer.curs_line - edit->start_line;
2916 /* --------------------------------------------------------------------------------------------- */
2918 void
2919 edit_update_curs_col (WEdit *edit)
2921 off_t b;
2923 b = edit_buffer_get_current_bol (&edit->buffer);
2924 edit->curs_col = (long) edit_move_forward3 (edit, b, 0, edit->buffer.curs1);
2927 /* --------------------------------------------------------------------------------------------- */
2929 long
2930 edit_get_curs_col (const WEdit *edit)
2932 return edit->curs_col;
2935 /* --------------------------------------------------------------------------------------------- */
2936 /** moves the display start position up by i lines */
2938 void
2939 edit_scroll_upward (WEdit *edit, long i)
2941 long lines_above = edit->start_line;
2943 if (i > lines_above)
2944 i = lines_above;
2945 if (i != 0)
2947 edit->start_line -= i;
2948 edit->start_display =
2949 edit_buffer_get_backward_offset (&edit->buffer, edit->start_display, i);
2950 edit->force |= REDRAW_PAGE;
2951 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2953 edit_update_curs_row (edit);
2956 /* --------------------------------------------------------------------------------------------- */
2958 void
2959 edit_scroll_downward (WEdit *edit, long i)
2961 long lines_below;
2963 lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->rect.lines - 1);
2964 if (lines_below > 0)
2966 if (i > lines_below)
2967 i = lines_below;
2968 edit->start_line += i;
2969 edit->start_display =
2970 edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, i, 0);
2971 edit->force |= REDRAW_PAGE;
2972 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2974 edit_update_curs_row (edit);
2977 /* --------------------------------------------------------------------------------------------- */
2979 void
2980 edit_scroll_right (WEdit *edit, long i)
2982 edit->force |= REDRAW_PAGE;
2983 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2984 edit->start_col -= i;
2987 /* --------------------------------------------------------------------------------------------- */
2989 void
2990 edit_scroll_left (WEdit *edit, long i)
2992 if (edit->start_col)
2994 edit->start_col += i;
2995 if (edit->start_col > 0)
2996 edit->start_col = 0;
2997 edit->force |= REDRAW_PAGE;
2998 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3002 /* --------------------------------------------------------------------------------------------- */
3003 /* high level cursor movement commands */
3004 /* --------------------------------------------------------------------------------------------- */
3006 void
3007 edit_move_to_prev_col (WEdit *edit, off_t p)
3009 long prev = edit->prev_col;
3010 long over = edit->over_col;
3011 off_t b;
3013 edit_cursor_move (edit,
3014 edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
3016 if (edit_options.cursor_beyond_eol)
3018 off_t e;
3019 long line_len;
3021 b = edit_buffer_get_current_bol (&edit->buffer);
3022 e = edit_buffer_get_current_eol (&edit->buffer);
3023 line_len = (long) edit_move_forward3 (edit, b, 0, e);
3024 if (line_len < prev + edit->over_col)
3026 edit->over_col = prev + over - line_len;
3027 edit->prev_col = line_len;
3028 edit->curs_col = line_len;
3030 else
3032 edit->curs_col = prev + over;
3033 edit->prev_col = edit->curs_col;
3034 edit->over_col = 0;
3037 else
3039 edit->over_col = 0;
3040 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer))
3042 long fake_half_tabs;
3044 edit_update_curs_col (edit);
3046 fake_half_tabs = HALF_TAB_SIZE * space_width;
3047 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3049 long q;
3051 q = edit->curs_col;
3052 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3053 p = edit_buffer_get_current_bol (&edit->buffer);
3054 b = edit_move_forward3 (edit, p, edit->curs_col, 0);
3055 edit_cursor_move (edit, b - edit->buffer.curs1);
3056 if (!left_of_four_spaces (edit))
3058 b = edit_move_forward3 (edit, p, q, 0);
3059 edit_cursor_move (edit, b - edit->buffer.curs1);
3066 /* --------------------------------------------------------------------------------------------- */
3067 /** check whether line in editor is blank or not
3069 * @param edit editor object
3070 * @param line number of line
3072 * @return TRUE if line in blank, FALSE otherwise
3075 gboolean
3076 edit_line_is_blank (WEdit *edit, long line)
3078 return is_blank (&edit->buffer, edit_find_line (edit, line));
3081 /* --------------------------------------------------------------------------------------------- */
3082 /** move cursor to line 'line' */
3084 void
3085 edit_move_to_line (WEdit *e, long line)
3087 if (line < e->buffer.curs_line)
3088 edit_move_up (e, e->buffer.curs_line - line, FALSE);
3089 else
3090 edit_move_down (e, line - e->buffer.curs_line, FALSE);
3091 edit_scroll_screen_over_cursor (e);
3094 /* --------------------------------------------------------------------------------------------- */
3095 /** scroll window so that first visible line is 'line' */
3097 void
3098 edit_move_display (WEdit *e, long line)
3100 if (line < e->start_line)
3101 edit_scroll_upward (e, e->start_line - line);
3102 else
3103 edit_scroll_downward (e, line - e->start_line);
3106 /* --------------------------------------------------------------------------------------------- */
3107 /** save markers onto undo stack */
3109 void
3110 edit_push_markers (WEdit *edit)
3112 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3113 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3114 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3117 /* --------------------------------------------------------------------------------------------- */
3119 void
3120 edit_set_markers (WEdit *edit, off_t m1, off_t m2, long c1, long c2)
3122 edit->mark1 = m1;
3123 edit->mark2 = m2;
3124 edit->column1 = c1;
3125 edit->column2 = c2;
3128 /* --------------------------------------------------------------------------------------------- */
3130 if mark2 is -1 then marking is from mark1 to the cursor.
3131 Otherwise its between the markers. This handles this.
3132 Returns FALSE if no text is marked.
3135 gboolean
3136 eval_marks (WEdit *edit, off_t *start_mark, off_t *end_mark)
3138 long end_mark_curs;
3140 if (edit->mark1 == edit->mark2)
3142 *start_mark = *end_mark = 0;
3143 edit->column2 = edit->column1 = 0;
3144 return FALSE;
3147 if (edit->end_mark_curs < 0)
3148 end_mark_curs = edit->buffer.curs1;
3149 else
3150 end_mark_curs = edit->end_mark_curs;
3152 if (edit->mark2 >= 0)
3154 *start_mark = MIN (edit->mark1, edit->mark2);
3155 *end_mark = MAX (edit->mark1, edit->mark2);
3157 else
3159 *start_mark = MIN (edit->mark1, end_mark_curs);
3160 *end_mark = MAX (edit->mark1, end_mark_curs);
3161 edit->column2 = edit->curs_col + edit->over_col;
3164 if (edit->column_highlight
3165 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
3166 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
3168 off_t start_bol, start_eol;
3169 off_t end_bol, end_eol;
3170 long col1, col2;
3171 off_t diff1, diff2;
3173 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
3174 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
3175 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
3176 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
3177 col1 = MIN (edit->column1, edit->column2);
3178 col2 = MAX (edit->column1, edit->column2);
3180 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
3181 edit_move_forward3 (edit, start_bol, col1, 0);
3182 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
3183 edit_move_forward3 (edit, end_bol, col1, 0);
3185 *start_mark -= diff1;
3186 *end_mark += diff2;
3187 *start_mark = MAX (*start_mark, start_eol);
3188 *end_mark = MIN (*end_mark, end_eol);
3191 return TRUE;
3194 /* --------------------------------------------------------------------------------------------- */
3195 /** highlight marker toggle */
3197 void
3198 edit_mark_cmd (WEdit *edit, gboolean unmark)
3200 edit_push_markers (edit);
3201 if (unmark)
3203 edit_set_markers (edit, 0, 0, 0, 0);
3204 edit->force |= REDRAW_PAGE;
3206 else if (edit->mark2 >= 0)
3208 edit->end_mark_curs = -1;
3209 edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3210 edit->curs_col + edit->over_col);
3211 edit->force |= REDRAW_PAGE;
3213 else
3215 edit->end_mark_curs = edit->buffer.curs1;
3216 edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3217 edit->curs_col + edit->over_col);
3221 /* --------------------------------------------------------------------------------------------- */
3222 /** highlight the word under cursor */
3224 void
3225 edit_mark_current_word_cmd (WEdit *edit)
3227 long pos;
3229 for (pos = edit->buffer.curs1; pos != 0; pos--)
3231 int c1, c2;
3233 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3234 c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3235 if (!isspace (c1) && isspace (c2))
3236 break;
3237 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3238 break;
3240 edit->mark1 = pos;
3242 for (; pos < edit->buffer.size; pos++)
3244 int c1, c2;
3246 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3247 c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3248 if (!isspace (c1) && isspace (c2))
3249 break;
3250 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3251 break;
3253 edit->mark2 = MIN (pos + 1, edit->buffer.size);
3255 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3258 /* --------------------------------------------------------------------------------------------- */
3260 void
3261 edit_mark_current_line_cmd (WEdit *edit)
3263 edit->mark1 = edit_buffer_get_current_bol (&edit->buffer);
3264 edit->mark2 = edit_buffer_get_current_eol (&edit->buffer);
3266 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3269 /* --------------------------------------------------------------------------------------------- */
3271 void
3272 edit_delete_line (WEdit *edit)
3275 * Delete right part of the line.
3276 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3277 * beyond EOF.
3279 while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3280 (void) edit_delete (edit, TRUE);
3283 * Delete '\n' char.
3284 * Note that edit_delete() will not corrupt anything if called while
3285 * cursor position is EOF.
3287 (void) edit_delete (edit, TRUE);
3290 * Delete left part of the line.
3291 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3293 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3294 (void) edit_backspace (edit, TRUE);
3297 /* --------------------------------------------------------------------------------------------- */
3299 void
3300 edit_push_key_press (WEdit *edit)
3302 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3303 if (edit->mark2 == -1)
3305 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3306 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3310 /* --------------------------------------------------------------------------------------------- */
3312 void
3313 edit_find_bracket (WEdit *edit)
3315 edit->bracket = edit_get_bracket (edit, 1, 10000);
3316 if (edit->last_bracket != edit->bracket)
3317 edit->force |= REDRAW_PAGE;
3318 edit->last_bracket = edit->bracket;
3321 /* --------------------------------------------------------------------------------------------- */
3323 * This executes a command as though the user initiated it through a key
3324 * press. Callback with MSG_KEY as a message calls this after
3325 * translating the key press. This function can be used to pass any
3326 * command to the editor. Note that the screen wouldn't update
3327 * automatically. Either of command or char_for_insertion must be
3328 * passed as -1. Commands are executed, and char_for_insertion is
3329 * inserted at the cursor.
3332 void
3333 edit_execute_key_command (WEdit *edit, long command, int char_for_insertion)
3335 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3336 || (macro_index < 0
3337 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3339 macro_index = 0;
3340 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3341 return;
3343 if (macro_index != -1)
3345 edit->force |= REDRAW_COMPLETELY;
3346 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3348 edit_store_macro_cmd (edit);
3349 macro_index = -1;
3350 return;
3352 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3354 edit_repeat_macro_cmd (edit);
3355 macro_index = -1;
3356 return;
3360 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3362 record_macro_buf[macro_index].action = command;
3363 record_macro_buf[macro_index++].ch = char_for_insertion;
3365 /* record the beginning of a set of editing actions initiated by a key press */
3366 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3367 edit_push_key_press (edit);
3369 edit_execute_cmd (edit, command, char_for_insertion);
3370 if (edit->column_highlight)
3371 edit->force |= REDRAW_PAGE;
3374 /* --------------------------------------------------------------------------------------------- */
3376 This executes a command at a lower level than macro recording.
3377 It also does not push a key_press onto the undo stack. This means
3378 that if it is called many times, a single undo command will undo
3379 all of them. It also does not check for the Undo command.
3381 void
3382 edit_execute_cmd (WEdit *edit, long command, int char_for_insertion)
3384 WRect *w = &WIDGET (edit)->rect;
3386 if (command == CK_WindowFullscreen)
3388 edit_toggle_fullscreen (edit);
3389 return;
3392 /* handle window state */
3393 if (edit_handle_move_resize (edit, command))
3394 return;
3396 edit->force |= REDRAW_LINE;
3398 /* The next key press will unhighlight the found string, so update
3399 * the whole page */
3400 if (edit->found_len || edit->column_highlight)
3401 edit->force |= REDRAW_PAGE;
3403 switch (command)
3405 /* a mark command with shift-arrow */
3406 case CK_MarkLeft:
3407 case CK_MarkRight:
3408 case CK_MarkToWordBegin:
3409 case CK_MarkToWordEnd:
3410 case CK_MarkToHome:
3411 case CK_MarkToEnd:
3412 case CK_MarkUp:
3413 case CK_MarkDown:
3414 case CK_MarkPageUp:
3415 case CK_MarkPageDown:
3416 case CK_MarkToFileBegin:
3417 case CK_MarkToFileEnd:
3418 case CK_MarkToPageBegin:
3419 case CK_MarkToPageEnd:
3420 case CK_MarkScrollUp:
3421 case CK_MarkScrollDown:
3422 case CK_MarkParagraphUp:
3423 case CK_MarkParagraphDown:
3424 /* a mark command with alt-arrow */
3425 case CK_MarkColumnPageUp:
3426 case CK_MarkColumnPageDown:
3427 case CK_MarkColumnLeft:
3428 case CK_MarkColumnRight:
3429 case CK_MarkColumnUp:
3430 case CK_MarkColumnDown:
3431 case CK_MarkColumnScrollUp:
3432 case CK_MarkColumnScrollDown:
3433 case CK_MarkColumnParagraphUp:
3434 case CK_MarkColumnParagraphDown:
3435 edit->column_highlight = 0;
3436 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3438 edit_mark_cmd (edit, TRUE); /* clear */
3439 edit_mark_cmd (edit, FALSE); /* marking on */
3441 edit->highlight = 1;
3442 break;
3444 /* any other command */
3445 default:
3446 if (edit->highlight)
3447 edit_mark_cmd (edit, FALSE); /* clear */
3448 edit->highlight = 0;
3451 /* first check for undo */
3452 if (command == CK_Undo)
3454 edit->redo_stack_reset = 0;
3455 edit_group_undo (edit);
3456 edit->found_len = 0;
3457 edit->prev_col = edit_get_col (edit);
3458 edit->search_start = edit->buffer.curs1;
3459 return;
3461 /* check for redo */
3462 if (command == CK_Redo)
3464 edit->redo_stack_reset = 0;
3465 edit_do_redo (edit);
3466 edit->found_len = 0;
3467 edit->prev_col = edit_get_col (edit);
3468 edit->search_start = edit->buffer.curs1;
3469 return;
3472 edit->redo_stack_reset = 1;
3474 /* An ordinary key press */
3475 if (char_for_insertion >= 0)
3477 /* if non persistent selection and text selected */
3478 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3479 edit_block_delete_cmd (edit);
3481 if (edit->overwrite)
3483 /* remove char only one time, after input first byte, multibyte chars */
3484 #ifdef HAVE_CHARSET
3485 if (!mc_global.utf8_display || edit->charpoint == 0)
3486 #endif
3487 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3488 edit_delete (edit, FALSE);
3490 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3491 edit_insert_over (edit);
3492 #ifdef HAVE_CHARSET
3494 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3495 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3497 if (char_for_insertion > 127 && str_isutf8 (get_codepage_id (mc_global.source_codepage))
3498 && !mc_global.utf8_display)
3500 unsigned char str[UTF8_CHAR_LEN + 1];
3501 size_t i;
3502 int res;
3504 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3505 if (res == 0)
3507 str[0] = '.';
3508 str[1] = '\0';
3510 else
3511 str[res] = '\0';
3513 for (i = 0; i <= UTF8_CHAR_LEN && str[i] != '\0'; i++)
3515 char_for_insertion = str[i];
3516 edit_insert (edit, char_for_insertion);
3519 else
3520 #endif
3521 edit_insert (edit, char_for_insertion);
3523 if (edit_options.auto_para_formatting)
3525 format_paragraph (edit, FALSE);
3526 edit->force |= REDRAW_PAGE;
3528 else
3529 check_and_wrap_line (edit);
3530 edit->found_len = 0;
3531 edit->prev_col = edit_get_col (edit);
3532 edit->search_start = edit->buffer.curs1;
3533 edit_find_bracket (edit);
3534 return;
3537 switch (command)
3539 case CK_TopOnScreen:
3540 case CK_BottomOnScreen:
3541 case CK_Top:
3542 case CK_Bottom:
3543 case CK_PageUp:
3544 case CK_PageDown:
3545 case CK_Home:
3546 case CK_End:
3547 case CK_Up:
3548 case CK_Down:
3549 case CK_Left:
3550 case CK_Right:
3551 case CK_WordLeft:
3552 case CK_WordRight:
3553 if (!edit_options.persistent_selections && edit->mark2 >= 0)
3555 if (edit->column_highlight)
3556 edit_push_undo_action (edit, COLUMN_ON);
3557 edit->column_highlight = 0;
3558 edit_mark_cmd (edit, TRUE);
3560 break;
3561 default:
3562 break;
3565 switch (command)
3567 case CK_TopOnScreen:
3568 case CK_BottomOnScreen:
3569 case CK_MarkToPageBegin:
3570 case CK_MarkToPageEnd:
3571 case CK_Up:
3572 case CK_Down:
3573 case CK_WordLeft:
3574 case CK_WordRight:
3575 case CK_MarkToWordBegin:
3576 case CK_MarkToWordEnd:
3577 case CK_MarkUp:
3578 case CK_MarkDown:
3579 case CK_MarkColumnUp:
3580 case CK_MarkColumnDown:
3581 if (edit->mark2 == -1)
3582 break; /*marking is following the cursor: may need to highlight a whole line */
3583 MC_FALLTHROUGH;
3584 case CK_Left:
3585 case CK_Right:
3586 case CK_MarkLeft:
3587 case CK_MarkRight:
3588 edit->force |= REDRAW_CHAR_ONLY;
3589 break;
3590 default:
3591 break;
3594 /* basic cursor key commands */
3595 switch (command)
3597 case CK_BackSpace:
3598 /* if non persistent selection and text selected */
3599 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3600 edit_block_delete_cmd (edit);
3601 else if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3602 edit->over_col--;
3603 else if (edit_options.backspace_through_tabs && is_in_indent (&edit->buffer))
3605 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
3606 edit_backspace (edit, TRUE);
3608 else if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3609 && right_of_four_spaces (edit))
3611 int i;
3613 for (i = 0; i < HALF_TAB_SIZE; i++)
3614 edit_backspace (edit, TRUE);
3616 else
3617 edit_backspace (edit, FALSE);
3618 break;
3619 case CK_Delete:
3620 /* if non persistent selection and text selected */
3621 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3622 edit_block_delete_cmd (edit);
3623 else
3625 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3626 edit_insert_over (edit);
3628 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3629 && left_of_four_spaces (edit))
3631 int i;
3633 for (i = 1; i <= HALF_TAB_SIZE; i++)
3634 edit_delete (edit, TRUE);
3636 else
3637 edit_delete (edit, FALSE);
3639 break;
3640 case CK_DeleteToWordBegin:
3641 edit->over_col = 0;
3642 edit_left_delete_word (edit);
3643 break;
3644 case CK_DeleteToWordEnd:
3645 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3646 edit_insert_over (edit);
3648 edit_right_delete_word (edit);
3649 break;
3650 case CK_DeleteLine:
3651 edit_delete_line (edit);
3652 break;
3653 case CK_DeleteToHome:
3654 edit_delete_to_line_begin (edit);
3655 break;
3656 case CK_DeleteToEnd:
3657 edit_delete_to_line_end (edit);
3658 break;
3659 case CK_Enter:
3660 edit->over_col = 0;
3661 if (edit_options.auto_para_formatting)
3663 edit_double_newline (edit);
3664 if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3665 edit_auto_indent (edit);
3666 format_paragraph (edit, FALSE);
3668 else
3670 edit_insert (edit, '\n');
3671 if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3672 edit_auto_indent (edit);
3674 break;
3675 case CK_Return:
3676 edit_insert (edit, '\n');
3677 break;
3679 case CK_MarkColumnPageUp:
3680 edit->column_highlight = 1;
3681 MC_FALLTHROUGH;
3682 case CK_PageUp:
3683 case CK_MarkPageUp:
3684 edit_move_up (edit, w->lines - 1, TRUE);
3685 break;
3686 case CK_MarkColumnPageDown:
3687 edit->column_highlight = 1;
3688 MC_FALLTHROUGH;
3689 case CK_PageDown:
3690 case CK_MarkPageDown:
3691 edit_move_down (edit, w->lines - 1, TRUE);
3692 break;
3693 case CK_MarkColumnLeft:
3694 edit->column_highlight = 1;
3695 MC_FALLTHROUGH;
3696 case CK_Left:
3697 case CK_MarkLeft:
3698 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3699 && right_of_four_spaces (edit))
3701 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3702 edit->over_col--;
3703 else
3704 edit_cursor_move (edit, -HALF_TAB_SIZE);
3705 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3707 else
3708 edit_left_char_move_cmd (edit);
3709 break;
3710 case CK_MarkColumnRight:
3711 edit->column_highlight = 1;
3712 MC_FALLTHROUGH;
3713 case CK_Right:
3714 case CK_MarkRight:
3715 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3716 && left_of_four_spaces (edit))
3718 edit_cursor_move (edit, HALF_TAB_SIZE);
3719 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3721 else
3722 edit_right_char_move_cmd (edit);
3723 break;
3724 case CK_TopOnScreen:
3725 case CK_MarkToPageBegin:
3726 edit_begin_page (edit);
3727 break;
3728 case CK_BottomOnScreen:
3729 case CK_MarkToPageEnd:
3730 edit_end_page (edit);
3731 break;
3732 case CK_WordLeft:
3733 case CK_MarkToWordBegin:
3734 edit->over_col = 0;
3735 edit_left_word_move_cmd (edit);
3736 break;
3737 case CK_WordRight:
3738 case CK_MarkToWordEnd:
3739 edit->over_col = 0;
3740 edit_right_word_move_cmd (edit);
3741 break;
3742 case CK_MarkColumnUp:
3743 edit->column_highlight = 1;
3744 MC_FALLTHROUGH;
3745 case CK_Up:
3746 case CK_MarkUp:
3747 edit_move_up (edit, 1, FALSE);
3748 break;
3749 case CK_MarkColumnDown:
3750 edit->column_highlight = 1;
3751 MC_FALLTHROUGH;
3752 case CK_Down:
3753 case CK_MarkDown:
3754 edit_move_down (edit, 1, FALSE);
3755 break;
3756 case CK_MarkColumnParagraphUp:
3757 edit->column_highlight = 1;
3758 MC_FALLTHROUGH;
3759 case CK_ParagraphUp:
3760 case CK_MarkParagraphUp:
3761 edit_move_up_paragraph (edit, FALSE);
3762 break;
3763 case CK_MarkColumnParagraphDown:
3764 edit->column_highlight = 1;
3765 MC_FALLTHROUGH;
3766 case CK_ParagraphDown:
3767 case CK_MarkParagraphDown:
3768 edit_move_down_paragraph (edit, FALSE);
3769 break;
3770 case CK_MarkColumnScrollUp:
3771 edit->column_highlight = 1;
3772 MC_FALLTHROUGH;
3773 case CK_ScrollUp:
3774 case CK_MarkScrollUp:
3775 edit_move_up (edit, 1, TRUE);
3776 break;
3777 case CK_MarkColumnScrollDown:
3778 edit->column_highlight = 1;
3779 MC_FALLTHROUGH;
3780 case CK_ScrollDown:
3781 case CK_MarkScrollDown:
3782 edit_move_down (edit, 1, TRUE);
3783 break;
3784 case CK_Home:
3785 case CK_MarkToHome:
3786 edit_cursor_to_bol (edit);
3787 break;
3788 case CK_End:
3789 case CK_MarkToEnd:
3790 edit_cursor_to_eol (edit);
3791 break;
3792 case CK_Tab:
3793 /* if text marked shift block */
3794 if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3796 if (edit->mark2 < 0)
3797 edit_mark_cmd (edit, FALSE);
3798 edit_move_block_to_right (edit);
3800 else
3802 if (edit_options.cursor_beyond_eol)
3803 edit_insert_over (edit);
3804 edit_tab_cmd (edit);
3805 if (edit_options.auto_para_formatting)
3807 format_paragraph (edit, FALSE);
3808 edit->force |= REDRAW_PAGE;
3810 else
3811 check_and_wrap_line (edit);
3813 break;
3815 case CK_InsertOverwrite:
3816 edit->overwrite = !edit->overwrite;
3817 break;
3819 case CK_Mark:
3820 if (edit->mark2 >= 0)
3822 if (edit->column_highlight)
3823 edit_push_undo_action (edit, COLUMN_ON);
3824 edit->column_highlight = 0;
3826 edit_mark_cmd (edit, FALSE);
3827 break;
3828 case CK_MarkColumn:
3829 if (!edit->column_highlight)
3830 edit_push_undo_action (edit, COLUMN_OFF);
3831 edit->column_highlight = 1;
3832 edit_mark_cmd (edit, FALSE);
3833 break;
3834 case CK_MarkAll:
3835 edit_set_markers (edit, 0, edit->buffer.size, 0, 0);
3836 edit->force |= REDRAW_PAGE;
3837 break;
3838 case CK_Unmark:
3839 if (edit->column_highlight)
3840 edit_push_undo_action (edit, COLUMN_ON);
3841 edit->column_highlight = 0;
3842 edit_mark_cmd (edit, TRUE);
3843 break;
3844 case CK_MarkWord:
3845 if (edit->column_highlight)
3846 edit_push_undo_action (edit, COLUMN_ON);
3847 edit->column_highlight = 0;
3848 edit_mark_current_word_cmd (edit);
3849 break;
3850 case CK_MarkLine:
3851 if (edit->column_highlight)
3852 edit_push_undo_action (edit, COLUMN_ON);
3853 edit->column_highlight = 0;
3854 edit_mark_current_line_cmd (edit);
3855 break;
3857 case CK_Bookmark:
3858 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR);
3859 if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR))
3860 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3861 else
3862 book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3863 break;
3864 case CK_BookmarkFlush:
3865 book_mark_flush (edit, BOOK_MARK_COLOR);
3866 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3867 edit->force |= REDRAW_PAGE;
3868 break;
3869 case CK_BookmarkNext:
3870 if (edit->book_mark != NULL)
3872 edit_book_mark_t *p;
3874 p = book_mark_find (edit, edit->buffer.curs_line);
3875 if (p->next != NULL)
3877 p = p->next;
3878 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3879 edit_move_display (edit, p->line - w->lines / 2);
3880 edit_move_to_line (edit, p->line);
3883 break;
3884 case CK_BookmarkPrev:
3885 if (edit->book_mark != NULL)
3887 edit_book_mark_t *p;
3889 p = book_mark_find (edit, edit->buffer.curs_line);
3890 while (p->line == edit->buffer.curs_line)
3891 if (p->prev != NULL)
3892 p = p->prev;
3893 if (p->line >= 0)
3895 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3896 edit_move_display (edit, p->line - w->lines / 2);
3897 edit_move_to_line (edit, p->line);
3900 break;
3902 case CK_Top:
3903 case CK_MarkToFileBegin:
3904 edit_move_to_top (edit);
3905 break;
3906 case CK_Bottom:
3907 case CK_MarkToFileEnd:
3908 edit_move_to_bottom (edit);
3909 break;
3911 case CK_Copy:
3912 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3913 edit_insert_over (edit);
3914 edit_block_copy_cmd (edit);
3915 break;
3916 case CK_Remove:
3917 edit_block_delete_cmd (edit);
3918 break;
3919 case CK_Move:
3920 edit_block_move_cmd (edit);
3921 break;
3923 case CK_BlockShiftLeft:
3924 if (edit->mark1 != edit->mark2)
3925 edit_move_block_to_left (edit);
3926 break;
3927 case CK_BlockShiftRight:
3928 if (edit->mark1 != edit->mark2)
3929 edit_move_block_to_right (edit);
3930 break;
3931 case CK_Store:
3932 edit_copy_to_X_buf_cmd (edit);
3933 break;
3934 case CK_Cut:
3935 edit_cut_to_X_buf_cmd (edit);
3936 break;
3937 case CK_Paste:
3938 /* if non persistent selection and text selected */
3939 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3940 edit_block_delete_cmd (edit);
3941 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3942 edit_insert_over (edit);
3943 edit_paste_from_X_buf_cmd (edit);
3944 if (!edit_options.persistent_selections && edit->mark2 >= 0)
3946 if (edit->column_highlight)
3947 edit_push_undo_action (edit, COLUMN_ON);
3948 edit->column_highlight = 0;
3949 edit_mark_cmd (edit, TRUE);
3951 break;
3952 case CK_History:
3953 edit_paste_from_history (edit);
3954 break;
3956 case CK_SaveAs:
3957 edit_save_as_cmd (edit);
3958 break;
3959 case CK_Save:
3960 edit_save_confirm_cmd (edit);
3961 break;
3962 case CK_BlockSave:
3963 edit_save_block_cmd (edit);
3964 break;
3965 case CK_InsertFile:
3966 edit_insert_file_cmd (edit);
3967 break;
3969 case CK_FilePrev:
3970 edit_load_back_cmd (edit);
3971 break;
3972 case CK_FileNext:
3973 edit_load_forward_cmd (edit);
3974 break;
3976 case CK_SyntaxChoose:
3977 edit_syntax_dialog (edit);
3978 break;
3980 case CK_Search:
3981 edit_search_cmd (edit, FALSE);
3982 break;
3983 case CK_SearchContinue:
3984 edit_search_cmd (edit, TRUE);
3985 break;
3986 case CK_Replace:
3987 edit_replace_cmd (edit, FALSE);
3988 break;
3989 case CK_ReplaceContinue:
3990 edit_replace_cmd (edit, TRUE);
3991 break;
3992 case CK_Complete:
3993 /* if text marked shift block */
3994 if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3995 edit_move_block_to_left (edit);
3996 else
3997 edit_complete_word_cmd (edit);
3998 break;
3999 case CK_Find:
4000 edit_get_match_keyword_cmd (edit);
4001 break;
4003 #ifdef HAVE_ASPELL
4004 case CK_SpellCheckCurrentWord:
4005 edit_suggest_current_word (edit);
4006 break;
4007 case CK_SpellCheck:
4008 edit_spellcheck_file (edit);
4009 break;
4010 case CK_SpellCheckSelectLang:
4011 edit_set_spell_lang ();
4012 break;
4013 #endif
4015 case CK_Date:
4017 char s[BUF_MEDIUM];
4018 /* fool gcc to prevent a Y2K warning */
4019 char time_format[] = "_c";
4020 time_format[0] = '%';
4022 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4023 edit_print_string (edit, s);
4024 edit->force |= REDRAW_PAGE;
4026 break;
4027 case CK_Goto:
4028 edit_goto_cmd (edit);
4029 break;
4030 case CK_ParagraphFormat:
4031 format_paragraph (edit, TRUE);
4032 edit->force |= REDRAW_PAGE;
4033 break;
4034 case CK_MacroDelete:
4035 edit_delete_macro_cmd (edit);
4036 break;
4037 case CK_MatchBracket:
4038 edit_goto_matching_bracket (edit);
4039 break;
4040 case CK_UserMenu:
4041 edit_user_menu (edit, NULL, -1);
4042 break;
4043 case CK_Sort:
4044 edit_sort_cmd (edit);
4045 break;
4046 case CK_ExternalCommand:
4047 edit_ext_cmd (edit);
4048 break;
4049 case CK_EditMail:
4050 edit_mail_dialog (edit);
4051 break;
4052 #ifdef HAVE_CHARSET
4053 case CK_SelectCodepage:
4054 edit_select_codepage_cmd (edit);
4055 break;
4056 #endif
4057 case CK_InsertLiteral:
4058 edit_insert_literal_cmd (edit);
4059 break;
4060 case CK_MacroStartStopRecord:
4061 edit_begin_end_macro_cmd (edit);
4062 break;
4063 case CK_RepeatStartStopRecord:
4064 edit_begin_end_repeat_cmd (edit);
4065 break;
4066 case CK_ExtendedKeyMap:
4067 WIDGET (edit)->ext_mode = TRUE;
4068 break;
4069 default:
4070 break;
4073 /* CK_PipeBlock */
4074 if ((command / CK_PipeBlock (0)) == 1)
4075 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4077 /* keys which must set the col position, and the search vars */
4078 switch (command)
4080 case CK_Search:
4081 case CK_SearchContinue:
4082 case CK_Replace:
4083 case CK_ReplaceContinue:
4084 case CK_Complete:
4085 edit->prev_col = edit_get_col (edit);
4086 break;
4087 case CK_Up:
4088 case CK_MarkUp:
4089 case CK_MarkColumnUp:
4090 case CK_Down:
4091 case CK_MarkDown:
4092 case CK_MarkColumnDown:
4093 case CK_PageUp:
4094 case CK_MarkPageUp:
4095 case CK_MarkColumnPageUp:
4096 case CK_PageDown:
4097 case CK_MarkPageDown:
4098 case CK_MarkColumnPageDown:
4099 case CK_Top:
4100 case CK_MarkToFileBegin:
4101 case CK_Bottom:
4102 case CK_MarkToFileEnd:
4103 case CK_ParagraphUp:
4104 case CK_MarkParagraphUp:
4105 case CK_MarkColumnParagraphUp:
4106 case CK_ParagraphDown:
4107 case CK_MarkParagraphDown:
4108 case CK_MarkColumnParagraphDown:
4109 case CK_ScrollUp:
4110 case CK_MarkScrollUp:
4111 case CK_MarkColumnScrollUp:
4112 case CK_ScrollDown:
4113 case CK_MarkScrollDown:
4114 case CK_MarkColumnScrollDown:
4115 edit->search_start = edit->buffer.curs1;
4116 edit->found_len = 0;
4117 break;
4118 default:
4119 edit->found_len = 0;
4120 edit->prev_col = edit_get_col (edit);
4121 edit->search_start = edit->buffer.curs1;
4123 edit_find_bracket (edit);
4125 if (edit_options.auto_para_formatting)
4127 switch (command)
4129 case CK_BackSpace:
4130 case CK_Delete:
4131 case CK_DeleteToWordBegin:
4132 case CK_DeleteToWordEnd:
4133 case CK_DeleteToHome:
4134 case CK_DeleteToEnd:
4135 format_paragraph (edit, FALSE);
4136 edit->force |= REDRAW_PAGE;
4137 break;
4138 default:
4139 break;
4144 /* --------------------------------------------------------------------------------------------- */
4146 void
4147 edit_stack_init (void)
4149 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4150 edit_arg_init (&edit_history_moveto[edit_stack_iterator], NULL, -1);
4152 edit_stack_iterator = 0;
4155 /* --------------------------------------------------------------------------------------------- */
4157 void
4158 edit_stack_free (void)
4160 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4161 vfs_path_free (edit_history_moveto[edit_stack_iterator].file_vpath, TRUE);
4164 /* --------------------------------------------------------------------------------------------- */
4165 /** move i lines */
4167 void
4168 edit_move_up (WEdit *edit, long i, gboolean do_scroll)
4170 edit_move_updown (edit, i, do_scroll, TRUE);
4173 /* --------------------------------------------------------------------------------------------- */
4174 /** move i lines */
4176 void
4177 edit_move_down (WEdit *edit, long i, gboolean do_scroll)
4179 edit_move_updown (edit, i, do_scroll, FALSE);
4182 /* --------------------------------------------------------------------------------------------- */
4184 * Create edit_arg_t object from vfs_path_t object and the line number.
4186 * @param file_vpath file path object
4187 * @param line_number line number. If value is 0, try to restore saved position.
4188 * @return edit_arg_t object
4191 edit_arg_t *
4192 edit_arg_vpath_new (vfs_path_t *file_vpath, long line_number)
4194 edit_arg_t *arg;
4196 arg = g_new (edit_arg_t, 1);
4197 arg->file_vpath = file_vpath;
4198 arg->line_number = line_number;
4200 return arg;
4203 /* --------------------------------------------------------------------------------------------- */
4205 * Create edit_arg_t object from file name and the line number.
4207 * @param file_name file name
4208 * @param line_number line number. If value is 0, try to restore saved position.
4209 * @return edit_arg_t object
4212 edit_arg_t *
4213 edit_arg_new (const char *file_name, long line_number)
4215 return edit_arg_vpath_new (vfs_path_from_str (file_name), line_number);
4218 /* --------------------------------------------------------------------------------------------- */
4220 * Initialize edit_arg_t object.
4222 * @param arg edit_arg_t object
4223 * @param vpath vfs_path_t object
4224 * @param line line number
4227 void
4228 edit_arg_init (edit_arg_t *arg, vfs_path_t *vpath, long line)
4230 arg->file_vpath = (vfs_path_t *) vpath;
4231 arg->line_number = line;
4234 /* --------------------------------------------------------------------------------------------- */
4236 * Apply new values to edit_arg_t object members.
4238 * @param arg edit_arg_t object
4239 * @param vpath vfs_path_t object
4240 * @param line line number
4243 void
4244 edit_arg_assign (edit_arg_t *arg, vfs_path_t *vpath, long line)
4246 vfs_path_free (arg->file_vpath, TRUE);
4247 edit_arg_init (arg, vpath, line);
4250 /* --------------------------------------------------------------------------------------------- */
4252 * Free the edit_arg_t object.
4254 * @param arg edit_arg_t object
4257 void
4258 edit_arg_free (edit_arg_t *arg)
4260 vfs_path_free (arg->file_vpath, TRUE);
4261 g_free (arg);
4264 /* --------------------------------------------------------------------------------------------- */
4266 const char *
4267 edit_get_file_name (const WEdit *edit)
4269 return vfs_path_as_str (edit->filename_vpath);
4272 /* --------------------------------------------------------------------------------------------- */