buildsys: fix broken detection for termcap libraries when using slang
[midnight-commander.git] / src / editor / edit.c
blobaaf79bf8c7725604cb94b43d912b30b30e12a9f9
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 edit->attrs_ok = (mc_fgetflags (arg->file_vpath, &edit->attrs) == 0);
2169 edit->over_col = 0;
2170 edit->bracket = -1;
2171 edit->last_bracket = -1;
2172 edit->force |= REDRAW_PAGE;
2174 /* set file name before load file */
2175 if (arg != NULL)
2177 edit_set_filename (edit, arg->file_vpath);
2178 line = arg->line_number;
2180 else
2182 edit_set_filename (edit, NULL);
2183 line = 0;
2186 edit->undo_stack_size = START_STACK_SIZE;
2187 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2188 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2190 edit->redo_stack_size = START_STACK_SIZE;
2191 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2192 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2194 #ifdef HAVE_CHARSET
2195 edit->utf8 = FALSE;
2196 edit->converter = str_cnv_from_term;
2197 edit_set_codeset (edit);
2198 #endif
2200 if (!edit_load_file (edit))
2202 /* edit_load_file already gives an error message */
2203 if (to_free)
2204 g_free (edit);
2205 return NULL;
2208 edit->loading_done = 1;
2209 edit->modified = 0;
2210 edit->locked = 0;
2211 edit_load_syntax (edit, NULL, NULL);
2212 edit_get_syntax_color (edit, -1);
2214 /* load saved cursor position and/or boolmarks */
2215 if ((line == 0) && edit_options.save_position)
2216 edit_load_position (edit, TRUE);
2217 else
2219 edit_load_position (edit, FALSE);
2220 if (line <= 0)
2221 line = 1;
2222 edit_move_display (edit, line - 1);
2223 edit_move_to_line (edit, line - 1);
2226 edit_load_macro_cmd (edit);
2228 return edit;
2231 /* --------------------------------------------------------------------------------------------- */
2233 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2234 gboolean
2235 edit_clean (WEdit *edit)
2237 if (edit == NULL)
2238 return FALSE;
2240 /* a stale lock, remove it */
2241 if (edit->locked)
2242 (void) unlock_file (edit->filename_vpath);
2244 /* save cursor position */
2245 if (edit_options.save_position)
2246 edit_save_position (edit);
2247 else if (edit->serialized_bookmarks != NULL)
2248 g_array_free (edit->serialized_bookmarks, TRUE);
2250 /* File specified on the mcedit command line and never saved */
2251 if (edit->delete_file)
2252 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2254 edit_free_syntax_rules (edit);
2255 book_mark_flush (edit, -1);
2257 edit_buffer_clean (&edit->buffer);
2259 g_free (edit->undo_stack);
2260 g_free (edit->redo_stack);
2261 vfs_path_free (edit->filename_vpath, TRUE);
2262 vfs_path_free (edit->dir_vpath, TRUE);
2263 edit_search_deinit (edit);
2265 #ifdef HAVE_CHARSET
2266 if (edit->converter != str_cnv_from_term)
2267 str_close_conv (edit->converter);
2268 #endif
2270 edit_purge_widget (edit);
2272 return TRUE;
2275 /* --------------------------------------------------------------------------------------------- */
2278 * Load a new file into the editor and set line. If it fails, preserve the old file.
2279 * To do it, allocate a new widget, initialize it and, if the new file
2280 * was loaded, copy the data to the old widget.
2282 * @return TRUE on success, FALSE on failure.
2284 gboolean
2285 edit_reload_line (WEdit *edit, const edit_arg_t *arg)
2287 Widget *w = WIDGET (edit);
2288 WEdit *e;
2290 e = g_malloc0 (sizeof (WEdit));
2291 *WIDGET (e) = *w;
2292 /* save some widget parameters */
2293 e->fullscreen = edit->fullscreen;
2294 e->loc_prev = edit->loc_prev;
2296 if (edit_init (e, &w->rect, arg) == NULL)
2298 g_free (e);
2299 return FALSE;
2302 edit_clean (edit);
2303 memcpy (edit, e, sizeof (*edit));
2304 g_free (e);
2306 return TRUE;
2309 /* --------------------------------------------------------------------------------------------- */
2311 #ifdef HAVE_CHARSET
2312 void
2313 edit_set_codeset (WEdit *edit)
2315 const char *cp_id;
2317 cp_id =
2318 get_codepage_id (mc_global.source_codepage >=
2319 0 ? mc_global.source_codepage : mc_global.display_codepage);
2321 if (cp_id != NULL)
2323 GIConv conv;
2324 conv = str_crt_conv_from (cp_id);
2325 if (conv != INVALID_CONV)
2327 if (edit->converter != str_cnv_from_term)
2328 str_close_conv (edit->converter);
2329 edit->converter = conv;
2333 if (cp_id != NULL)
2334 edit->utf8 = str_isutf8 (cp_id);
2336 #endif
2338 /* --------------------------------------------------------------------------------------------- */
2341 * Recording stack for undo:
2342 * The following is an implementation of a compressed stack. Identical
2343 * pushes are recorded by a negative prefix indicating the number of times the
2344 * same char was pushed. This saves space for repeated curs-left or curs-right
2345 * delete etc.
2347 * eg:
2349 * pushed: stored:
2352 * b a
2353 * b -3
2354 * b b
2355 * c --> -4
2356 * c c
2357 * c d
2361 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2362 * 256-512 is an insert ahead (from a delete), If it is between 600 and 700 it is one
2363 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2364 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2365 * position.
2367 * The only way the cursor moves or the buffer is changed is through the routines:
2368 * insert, backspace, insert_ahead, delete, and cursor_move.
2369 * These record the reverse undo movements onto the stack each time they are
2370 * called.
2372 * Each key press results in a set of actions (insert; delete ...). So each time
2373 * a key is pressed the current position of start_display is pushed as
2374 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2375 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2376 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2380 * @param edit editor object
2381 * @param c code of the action
2384 void
2385 edit_push_undo_action (WEdit *edit, long c)
2387 unsigned long sp = edit->undo_stack_pointer;
2388 unsigned long spm1;
2390 /* first enlarge the stack if necessary */
2391 if (sp > edit->undo_stack_size - 10)
2392 { /* say */
2393 if (max_undo < 256)
2394 max_undo = 256;
2395 if (edit->undo_stack_size < (unsigned long) max_undo)
2397 long *t;
2399 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2400 if (t != NULL)
2402 edit->undo_stack = t;
2403 edit->undo_stack_size <<= 1;
2404 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2408 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2409 if (edit->undo_stack_disable)
2411 edit_push_redo_action (edit, KEY_PRESS);
2412 edit_push_redo_action (edit, c);
2413 return;
2416 if (edit->redo_stack_reset)
2417 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2419 if (edit->undo_stack_bottom != sp
2420 && spm1 != edit->undo_stack_bottom
2421 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2423 long d;
2425 if (edit->undo_stack[spm1] < 0)
2427 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2428 if (d == c && edit->undo_stack[spm1] > -1000000000)
2430 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2431 edit->undo_stack[spm1]--;
2432 return;
2435 else
2437 d = edit->undo_stack[spm1];
2438 if (d == c)
2440 if (c >= KEY_PRESS)
2441 return; /* --> no need to push multiple do-nothings */
2442 edit->undo_stack[sp] = -2;
2443 goto check_bottom;
2447 edit->undo_stack[sp] = c;
2449 check_bottom:
2450 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2452 /* if the sp wraps round and catches the undo_stack_bottom then erase
2453 * the first set of actions on the stack to make space - by moving
2454 * undo_stack_bottom forward one "key press" */
2455 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2456 if ((unsigned long) c == edit->undo_stack_bottom ||
2457 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2460 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2462 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2463 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2465 /*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: */
2466 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2467 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2469 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2473 /* --------------------------------------------------------------------------------------------- */
2475 void
2476 edit_push_redo_action (WEdit *edit, long c)
2478 unsigned long sp = edit->redo_stack_pointer;
2479 unsigned long spm1;
2480 /* first enlarge the stack if necessary */
2481 if (sp > edit->redo_stack_size - 10)
2482 { /* say */
2483 if (max_undo < 256)
2484 max_undo = 256;
2485 if (edit->redo_stack_size < (unsigned long) max_undo)
2487 long *t;
2489 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2490 if (t != NULL)
2492 edit->redo_stack = t;
2493 edit->redo_stack_size <<= 1;
2494 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2498 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2500 if (edit->redo_stack_bottom != sp
2501 && spm1 != edit->redo_stack_bottom
2502 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2504 long d;
2506 if (edit->redo_stack[spm1] < 0)
2508 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2509 if (d == c && edit->redo_stack[spm1] > -1000000000)
2511 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2512 edit->redo_stack[spm1]--;
2513 return;
2516 else
2518 d = edit->redo_stack[spm1];
2519 if (d == c)
2521 if (c >= KEY_PRESS)
2522 return; /* --> no need to push multiple do-nothings */
2523 edit->redo_stack[sp] = -2;
2524 goto redo_check_bottom;
2528 edit->redo_stack[sp] = c;
2530 redo_check_bottom:
2531 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2533 /* if the sp wraps round and catches the redo_stack_bottom then erase
2534 * the first set of actions on the stack to make space - by moving
2535 * redo_stack_bottom forward one "key press" */
2536 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2537 if ((unsigned long) c == edit->redo_stack_bottom ||
2538 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2541 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2543 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2544 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2547 * If a single key produced enough pushes to wrap all the way round then
2548 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2549 * The stack is then initialised:
2552 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2553 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2554 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2557 /* --------------------------------------------------------------------------------------------- */
2559 Basic low level single character buffer alterations and movements at the cursor.
2562 void
2563 edit_insert (WEdit *edit, int c)
2565 /* first we must update the position of the display window */
2566 if (edit->buffer.curs1 < edit->start_display)
2568 edit->start_display++;
2569 if (c == '\n')
2570 edit->start_line++;
2573 /* Mark file as modified, unless the file hasn't been fully loaded */
2574 if (edit->loading_done)
2575 edit_modification (edit);
2577 /* now we must update some info on the file and check if a redraw is required */
2578 if (c == '\n')
2580 book_mark_inc (edit, edit->buffer.curs_line);
2581 edit->buffer.curs_line++;
2582 edit->buffer.lines++;
2583 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2586 /* save the reverse command onto the undo stack */
2587 /* ordinary char and not space */
2588 if (c > 32)
2589 edit_push_undo_action (edit, BACKSPACE);
2590 else
2591 edit_push_undo_action (edit, BACKSPACE_BR);
2592 /* update markers */
2593 edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2594 edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2595 edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2597 edit_buffer_insert (&edit->buffer, c);
2600 /* --------------------------------------------------------------------------------------------- */
2601 /** same as edit_insert and move left */
2603 void
2604 edit_insert_ahead (WEdit *edit, int c)
2606 if (edit->buffer.curs1 < edit->start_display)
2608 edit->start_display++;
2609 if (c == '\n')
2610 edit->start_line++;
2612 edit_modification (edit);
2613 if (c == '\n')
2615 book_mark_inc (edit, edit->buffer.curs_line);
2616 edit->buffer.lines++;
2617 edit->force |= REDRAW_AFTER_CURSOR;
2619 /* ordinary char and not space */
2620 if (c > 32)
2621 edit_push_undo_action (edit, DELCHAR);
2622 else
2623 edit_push_undo_action (edit, DELCHAR_BR);
2625 edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2626 edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2627 edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2629 edit_buffer_insert_ahead (&edit->buffer, c);
2632 /* --------------------------------------------------------------------------------------------- */
2634 void
2635 edit_insert_over (WEdit *edit)
2637 long i;
2639 for (i = 0; i < edit->over_col; i++)
2640 edit_insert (edit, ' ');
2642 edit->over_col = 0;
2645 /* --------------------------------------------------------------------------------------------- */
2648 edit_delete (WEdit *edit, gboolean byte_delete)
2650 int p = 0;
2651 int char_length = 1;
2652 int i;
2654 if (edit->buffer.curs2 == 0)
2655 return 0;
2657 #ifdef HAVE_CHARSET
2658 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2659 if (edit->utf8 && !byte_delete)
2661 edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2662 if (char_length < 1)
2663 char_length = 1;
2665 #else
2666 (void) byte_delete;
2667 #endif
2669 if (edit->mark2 != edit->mark1)
2670 edit_push_markers (edit);
2672 for (i = 1; i <= char_length; i++)
2674 if (edit->mark1 > edit->buffer.curs1)
2676 edit->mark1--;
2677 edit->end_mark_curs--;
2679 if (edit->mark2 > edit->buffer.curs1)
2680 edit->mark2--;
2681 if (edit->last_get_rule > edit->buffer.curs1)
2682 edit->last_get_rule--;
2684 p = edit_buffer_delete (&edit->buffer);
2686 edit_push_undo_action (edit, p + 256);
2689 edit_modification (edit);
2690 if (p == '\n')
2692 book_mark_dec (edit, edit->buffer.curs_line);
2693 edit->buffer.lines--;
2694 edit->force |= REDRAW_AFTER_CURSOR;
2696 if (edit->buffer.curs1 < edit->start_display)
2698 edit->start_display--;
2699 if (p == '\n')
2700 edit->start_line--;
2703 return p;
2706 /* --------------------------------------------------------------------------------------------- */
2709 edit_backspace (WEdit *edit, gboolean byte_delete)
2711 int p = 0;
2712 int char_length = 1;
2713 int i;
2715 if (edit->buffer.curs1 == 0)
2716 return 0;
2718 if (edit->mark2 != edit->mark1)
2719 edit_push_markers (edit);
2721 #ifdef HAVE_CHARSET
2722 if (edit->utf8 && !byte_delete)
2724 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2725 if (char_length < 1)
2726 char_length = 1;
2728 #else
2729 (void) byte_delete;
2730 #endif
2732 for (i = 1; i <= char_length; i++)
2734 if (edit->mark1 >= edit->buffer.curs1)
2736 edit->mark1--;
2737 edit->end_mark_curs--;
2739 if (edit->mark2 >= edit->buffer.curs1)
2740 edit->mark2--;
2741 if (edit->last_get_rule >= edit->buffer.curs1)
2742 edit->last_get_rule--;
2744 p = edit_buffer_backspace (&edit->buffer);
2746 edit_push_undo_action (edit, p);
2748 edit_modification (edit);
2749 if (p == '\n')
2751 book_mark_dec (edit, edit->buffer.curs_line);
2752 edit->buffer.curs_line--;
2753 edit->buffer.lines--;
2754 edit->force |= REDRAW_AFTER_CURSOR;
2757 if (edit->buffer.curs1 < edit->start_display)
2759 edit->start_display--;
2760 if (p == '\n')
2761 edit->start_line--;
2764 return p;
2767 /* --------------------------------------------------------------------------------------------- */
2768 /** moves the cursor right or left: increment positive or negative respectively */
2770 void
2771 edit_cursor_move (WEdit *edit, off_t increment)
2773 if (increment < 0)
2775 for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2777 int c;
2779 edit_push_undo_action (edit, CURS_RIGHT);
2781 c = edit_buffer_get_previous_byte (&edit->buffer);
2782 edit_buffer_insert_ahead (&edit->buffer, c);
2783 c = edit_buffer_backspace (&edit->buffer);
2784 if (c == '\n')
2786 edit->buffer.curs_line--;
2787 edit->force |= REDRAW_LINE_BELOW;
2791 else
2793 for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2795 int c;
2797 edit_push_undo_action (edit, CURS_LEFT);
2799 c = edit_buffer_get_current_byte (&edit->buffer);
2800 edit_buffer_insert (&edit->buffer, c);
2801 c = edit_buffer_delete (&edit->buffer);
2802 if (c == '\n')
2804 edit->buffer.curs_line++;
2805 edit->force |= REDRAW_LINE_ABOVE;
2811 /* --------------------------------------------------------------------------------------------- */
2812 /* If cols is zero this returns the count of columns from current to upto. */
2813 /* If upto is zero returns index of cols across from current. */
2815 off_t
2816 edit_move_forward3 (const WEdit *edit, off_t current, long cols, off_t upto)
2818 off_t p, q;
2819 long col;
2821 if (upto != 0)
2823 q = upto;
2824 cols = -10;
2826 else
2827 q = edit->buffer.size + 2;
2829 for (col = 0, p = current; p < q; p++)
2831 int c, orig_c;
2833 if (cols != -10)
2835 if (col == cols)
2836 return p;
2837 if (col > cols)
2838 return p - 1;
2841 orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2843 #ifdef HAVE_CHARSET
2844 if (edit->utf8)
2846 int utf_ch;
2847 int char_length = 1;
2849 utf_ch = edit_buffer_get_utf (&edit->buffer, p, &char_length);
2850 if (mc_global.utf8_display)
2852 if (char_length > 1)
2853 col -= char_length - 1;
2854 if (g_unichar_iswide (utf_ch))
2855 col++;
2857 else if (char_length > 1 && g_unichar_isprint (utf_ch))
2858 col -= char_length - 1;
2861 c = convert_to_display_c (c);
2862 #endif
2864 if (c == '\n')
2865 return (upto != 0 ? (off_t) col : p);
2866 if (c == '\t')
2867 col += TAB_SIZE - col % TAB_SIZE;
2868 else if ((c < 32 || c == 127) && (orig_c == c
2869 #ifdef HAVE_CHARSET
2870 || (!mc_global.utf8_display && !edit->utf8)
2871 #endif
2873 /* '\r' is shown as ^M, so we must advance 2 characters */
2874 /* Caret notation for control characters */
2875 col += 2;
2876 else
2877 col++;
2879 return (off_t) col;
2882 /* --------------------------------------------------------------------------------------------- */
2883 /** returns the current offset of the cursor from the beginning of a file */
2885 off_t
2886 edit_get_cursor_offset (const WEdit *edit)
2888 return edit->buffer.curs1;
2891 /* --------------------------------------------------------------------------------------------- */
2892 /** returns the current column position of the cursor */
2894 long
2895 edit_get_col (const WEdit *edit)
2897 off_t b;
2899 b = edit_buffer_get_current_bol (&edit->buffer);
2900 return (long) edit_move_forward3 (edit, b, 0, edit->buffer.curs1);
2903 /* --------------------------------------------------------------------------------------------- */
2904 /* Scrolling functions */
2905 /* --------------------------------------------------------------------------------------------- */
2907 void
2908 edit_update_curs_row (WEdit *edit)
2910 edit->curs_row = edit->buffer.curs_line - edit->start_line;
2913 /* --------------------------------------------------------------------------------------------- */
2915 void
2916 edit_update_curs_col (WEdit *edit)
2918 off_t b;
2920 b = edit_buffer_get_current_bol (&edit->buffer);
2921 edit->curs_col = (long) edit_move_forward3 (edit, b, 0, edit->buffer.curs1);
2924 /* --------------------------------------------------------------------------------------------- */
2926 long
2927 edit_get_curs_col (const WEdit *edit)
2929 return edit->curs_col;
2932 /* --------------------------------------------------------------------------------------------- */
2933 /** moves the display start position up by i lines */
2935 void
2936 edit_scroll_upward (WEdit *edit, long i)
2938 long lines_above = edit->start_line;
2940 if (i > lines_above)
2941 i = lines_above;
2942 if (i != 0)
2944 edit->start_line -= i;
2945 edit->start_display =
2946 edit_buffer_get_backward_offset (&edit->buffer, edit->start_display, i);
2947 edit->force |= REDRAW_PAGE;
2948 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2950 edit_update_curs_row (edit);
2953 /* --------------------------------------------------------------------------------------------- */
2955 void
2956 edit_scroll_downward (WEdit *edit, long i)
2958 long lines_below;
2960 lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->rect.lines - 1);
2961 if (lines_below > 0)
2963 if (i > lines_below)
2964 i = lines_below;
2965 edit->start_line += i;
2966 edit->start_display =
2967 edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, i, 0);
2968 edit->force |= REDRAW_PAGE;
2969 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2971 edit_update_curs_row (edit);
2974 /* --------------------------------------------------------------------------------------------- */
2976 void
2977 edit_scroll_right (WEdit *edit, long i)
2979 edit->force |= REDRAW_PAGE;
2980 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2981 edit->start_col -= i;
2984 /* --------------------------------------------------------------------------------------------- */
2986 void
2987 edit_scroll_left (WEdit *edit, long i)
2989 if (edit->start_col)
2991 edit->start_col += i;
2992 if (edit->start_col > 0)
2993 edit->start_col = 0;
2994 edit->force |= REDRAW_PAGE;
2995 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2999 /* --------------------------------------------------------------------------------------------- */
3000 /* high level cursor movement commands */
3001 /* --------------------------------------------------------------------------------------------- */
3003 void
3004 edit_move_to_prev_col (WEdit *edit, off_t p)
3006 long prev = edit->prev_col;
3007 long over = edit->over_col;
3008 off_t b;
3010 edit_cursor_move (edit,
3011 edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
3013 if (edit_options.cursor_beyond_eol)
3015 off_t e;
3016 long line_len;
3018 b = edit_buffer_get_current_bol (&edit->buffer);
3019 e = edit_buffer_get_current_eol (&edit->buffer);
3020 line_len = (long) edit_move_forward3 (edit, b, 0, e);
3021 if (line_len < prev + edit->over_col)
3023 edit->over_col = prev + over - line_len;
3024 edit->prev_col = line_len;
3025 edit->curs_col = line_len;
3027 else
3029 edit->curs_col = prev + over;
3030 edit->prev_col = edit->curs_col;
3031 edit->over_col = 0;
3034 else
3036 edit->over_col = 0;
3037 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer))
3039 long fake_half_tabs;
3041 edit_update_curs_col (edit);
3043 fake_half_tabs = HALF_TAB_SIZE * space_width;
3044 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3046 long q;
3048 q = edit->curs_col;
3049 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3050 p = edit_buffer_get_current_bol (&edit->buffer);
3051 b = edit_move_forward3 (edit, p, edit->curs_col, 0);
3052 edit_cursor_move (edit, b - edit->buffer.curs1);
3053 if (!left_of_four_spaces (edit))
3055 b = edit_move_forward3 (edit, p, q, 0);
3056 edit_cursor_move (edit, b - edit->buffer.curs1);
3063 /* --------------------------------------------------------------------------------------------- */
3064 /** check whether line in editor is blank or not
3066 * @param edit editor object
3067 * @param line number of line
3069 * @return TRUE if line in blank, FALSE otherwise
3072 gboolean
3073 edit_line_is_blank (WEdit *edit, long line)
3075 return is_blank (&edit->buffer, edit_find_line (edit, line));
3078 /* --------------------------------------------------------------------------------------------- */
3079 /** move cursor to line 'line' */
3081 void
3082 edit_move_to_line (WEdit *e, long line)
3084 if (line < e->buffer.curs_line)
3085 edit_move_up (e, e->buffer.curs_line - line, FALSE);
3086 else
3087 edit_move_down (e, line - e->buffer.curs_line, FALSE);
3088 edit_scroll_screen_over_cursor (e);
3091 /* --------------------------------------------------------------------------------------------- */
3092 /** scroll window so that first visible line is 'line' */
3094 void
3095 edit_move_display (WEdit *e, long line)
3097 if (line < e->start_line)
3098 edit_scroll_upward (e, e->start_line - line);
3099 else
3100 edit_scroll_downward (e, line - e->start_line);
3103 /* --------------------------------------------------------------------------------------------- */
3104 /** save markers onto undo stack */
3106 void
3107 edit_push_markers (WEdit *edit)
3109 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3110 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3111 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3114 /* --------------------------------------------------------------------------------------------- */
3116 void
3117 edit_set_markers (WEdit *edit, off_t m1, off_t m2, long c1, long c2)
3119 edit->mark1 = m1;
3120 edit->mark2 = m2;
3121 edit->column1 = c1;
3122 edit->column2 = c2;
3125 /* --------------------------------------------------------------------------------------------- */
3127 if mark2 is -1 then marking is from mark1 to the cursor.
3128 Otherwise its between the markers. This handles this.
3129 Returns FALSE if no text is marked.
3132 gboolean
3133 eval_marks (WEdit *edit, off_t *start_mark, off_t *end_mark)
3135 long end_mark_curs;
3137 if (edit->mark1 == edit->mark2)
3139 *start_mark = *end_mark = 0;
3140 edit->column2 = edit->column1 = 0;
3141 return FALSE;
3144 if (edit->end_mark_curs < 0)
3145 end_mark_curs = edit->buffer.curs1;
3146 else
3147 end_mark_curs = edit->end_mark_curs;
3149 if (edit->mark2 >= 0)
3151 *start_mark = MIN (edit->mark1, edit->mark2);
3152 *end_mark = MAX (edit->mark1, edit->mark2);
3154 else
3156 *start_mark = MIN (edit->mark1, end_mark_curs);
3157 *end_mark = MAX (edit->mark1, end_mark_curs);
3158 edit->column2 = edit->curs_col + edit->over_col;
3161 if (edit->column_highlight
3162 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
3163 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
3165 off_t start_bol, start_eol;
3166 off_t end_bol, end_eol;
3167 long col1, col2;
3168 off_t diff1, diff2;
3170 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
3171 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
3172 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
3173 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
3174 col1 = MIN (edit->column1, edit->column2);
3175 col2 = MAX (edit->column1, edit->column2);
3177 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
3178 edit_move_forward3 (edit, start_bol, col1, 0);
3179 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
3180 edit_move_forward3 (edit, end_bol, col1, 0);
3182 *start_mark -= diff1;
3183 *end_mark += diff2;
3184 *start_mark = MAX (*start_mark, start_eol);
3185 *end_mark = MIN (*end_mark, end_eol);
3188 return TRUE;
3191 /* --------------------------------------------------------------------------------------------- */
3192 /** highlight marker toggle */
3194 void
3195 edit_mark_cmd (WEdit *edit, gboolean unmark)
3197 edit_push_markers (edit);
3198 if (unmark)
3200 edit_set_markers (edit, 0, 0, 0, 0);
3201 edit->force |= REDRAW_PAGE;
3203 else if (edit->mark2 >= 0)
3205 edit->end_mark_curs = -1;
3206 edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3207 edit->curs_col + edit->over_col);
3208 edit->force |= REDRAW_PAGE;
3210 else
3212 edit->end_mark_curs = edit->buffer.curs1;
3213 edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3214 edit->curs_col + edit->over_col);
3218 /* --------------------------------------------------------------------------------------------- */
3219 /** highlight the word under cursor */
3221 void
3222 edit_mark_current_word_cmd (WEdit *edit)
3224 long pos;
3226 for (pos = edit->buffer.curs1; pos != 0; pos--)
3228 int c1, c2;
3230 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3231 c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3232 if (!isspace (c1) && isspace (c2))
3233 break;
3234 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3235 break;
3237 edit->mark1 = pos;
3239 for (; pos < edit->buffer.size; pos++)
3241 int c1, c2;
3243 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3244 c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3245 if (!isspace (c1) && isspace (c2))
3246 break;
3247 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3248 break;
3250 edit->mark2 = MIN (pos + 1, edit->buffer.size);
3252 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3255 /* --------------------------------------------------------------------------------------------- */
3257 void
3258 edit_mark_current_line_cmd (WEdit *edit)
3260 edit->mark1 = edit_buffer_get_current_bol (&edit->buffer);
3261 edit->mark2 = edit_buffer_get_current_eol (&edit->buffer);
3263 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3266 /* --------------------------------------------------------------------------------------------- */
3268 void
3269 edit_delete_line (WEdit *edit)
3272 * Delete right part of the line.
3273 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3274 * beyond EOF.
3276 while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3277 (void) edit_delete (edit, TRUE);
3280 * Delete '\n' char.
3281 * Note that edit_delete() will not corrupt anything if called while
3282 * cursor position is EOF.
3284 (void) edit_delete (edit, TRUE);
3287 * Delete left part of the line.
3288 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3290 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3291 (void) edit_backspace (edit, TRUE);
3294 /* --------------------------------------------------------------------------------------------- */
3296 void
3297 edit_push_key_press (WEdit *edit)
3299 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3300 if (edit->mark2 == -1)
3302 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3303 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3307 /* --------------------------------------------------------------------------------------------- */
3309 void
3310 edit_find_bracket (WEdit *edit)
3312 edit->bracket = edit_get_bracket (edit, 1, 10000);
3313 if (edit->last_bracket != edit->bracket)
3314 edit->force |= REDRAW_PAGE;
3315 edit->last_bracket = edit->bracket;
3318 /* --------------------------------------------------------------------------------------------- */
3320 * This executes a command as though the user initiated it through a key
3321 * press. Callback with MSG_KEY as a message calls this after
3322 * translating the key press. This function can be used to pass any
3323 * command to the editor. Note that the screen wouldn't update
3324 * automatically. Either of command or char_for_insertion must be
3325 * passed as -1. Commands are executed, and char_for_insertion is
3326 * inserted at the cursor.
3329 void
3330 edit_execute_key_command (WEdit *edit, long command, int char_for_insertion)
3332 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3333 || (macro_index < 0
3334 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3336 macro_index = 0;
3337 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3338 return;
3340 if (macro_index != -1)
3342 edit->force |= REDRAW_COMPLETELY;
3343 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3345 edit_store_macro_cmd (edit);
3346 macro_index = -1;
3347 return;
3349 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3351 edit_repeat_macro_cmd (edit);
3352 macro_index = -1;
3353 return;
3357 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3359 record_macro_buf[macro_index].action = command;
3360 record_macro_buf[macro_index++].ch = char_for_insertion;
3362 /* record the beginning of a set of editing actions initiated by a key press */
3363 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3364 edit_push_key_press (edit);
3366 edit_execute_cmd (edit, command, char_for_insertion);
3367 if (edit->column_highlight)
3368 edit->force |= REDRAW_PAGE;
3371 /* --------------------------------------------------------------------------------------------- */
3373 This executes a command at a lower level than macro recording.
3374 It also does not push a key_press onto the undo stack. This means
3375 that if it is called many times, a single undo command will undo
3376 all of them. It also does not check for the Undo command.
3378 void
3379 edit_execute_cmd (WEdit *edit, long command, int char_for_insertion)
3381 WRect *w = &WIDGET (edit)->rect;
3383 if (command == CK_WindowFullscreen)
3385 edit_toggle_fullscreen (edit);
3386 return;
3389 /* handle window state */
3390 if (edit_handle_move_resize (edit, command))
3391 return;
3393 edit->force |= REDRAW_LINE;
3395 /* The next key press will unhighlight the found string, so update
3396 * the whole page */
3397 if (edit->found_len || edit->column_highlight)
3398 edit->force |= REDRAW_PAGE;
3400 switch (command)
3402 /* a mark command with shift-arrow */
3403 case CK_MarkLeft:
3404 case CK_MarkRight:
3405 case CK_MarkToWordBegin:
3406 case CK_MarkToWordEnd:
3407 case CK_MarkToHome:
3408 case CK_MarkToEnd:
3409 case CK_MarkUp:
3410 case CK_MarkDown:
3411 case CK_MarkPageUp:
3412 case CK_MarkPageDown:
3413 case CK_MarkToFileBegin:
3414 case CK_MarkToFileEnd:
3415 case CK_MarkToPageBegin:
3416 case CK_MarkToPageEnd:
3417 case CK_MarkScrollUp:
3418 case CK_MarkScrollDown:
3419 case CK_MarkParagraphUp:
3420 case CK_MarkParagraphDown:
3421 /* a mark command with alt-arrow */
3422 case CK_MarkColumnPageUp:
3423 case CK_MarkColumnPageDown:
3424 case CK_MarkColumnLeft:
3425 case CK_MarkColumnRight:
3426 case CK_MarkColumnUp:
3427 case CK_MarkColumnDown:
3428 case CK_MarkColumnScrollUp:
3429 case CK_MarkColumnScrollDown:
3430 case CK_MarkColumnParagraphUp:
3431 case CK_MarkColumnParagraphDown:
3432 edit->column_highlight = 0;
3433 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3435 edit_mark_cmd (edit, TRUE); /* clear */
3436 edit_mark_cmd (edit, FALSE); /* marking on */
3438 edit->highlight = 1;
3439 break;
3441 /* any other command */
3442 default:
3443 if (edit->highlight)
3444 edit_mark_cmd (edit, FALSE); /* clear */
3445 edit->highlight = 0;
3448 /* first check for undo */
3449 if (command == CK_Undo)
3451 edit->redo_stack_reset = 0;
3452 edit_group_undo (edit);
3453 edit->found_len = 0;
3454 edit->prev_col = edit_get_col (edit);
3455 edit->search_start = edit->buffer.curs1;
3456 return;
3458 /* check for redo */
3459 if (command == CK_Redo)
3461 edit->redo_stack_reset = 0;
3462 edit_do_redo (edit);
3463 edit->found_len = 0;
3464 edit->prev_col = edit_get_col (edit);
3465 edit->search_start = edit->buffer.curs1;
3466 return;
3469 edit->redo_stack_reset = 1;
3471 /* An ordinary key press */
3472 if (char_for_insertion >= 0)
3474 /* if non persistent selection and text selected */
3475 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3476 edit_block_delete_cmd (edit);
3478 if (edit->overwrite)
3480 /* remove char only one time, after input first byte, multibyte chars */
3481 #ifdef HAVE_CHARSET
3482 if (!mc_global.utf8_display || edit->charpoint == 0)
3483 #endif
3484 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3485 edit_delete (edit, FALSE);
3487 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3488 edit_insert_over (edit);
3489 #ifdef HAVE_CHARSET
3491 Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3492 *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3494 if (char_for_insertion > 127 && str_isutf8 (get_codepage_id (mc_global.source_codepage))
3495 && !mc_global.utf8_display)
3497 unsigned char str[UTF8_CHAR_LEN + 1];
3498 size_t i;
3499 int res;
3501 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3502 if (res == 0)
3504 str[0] = '.';
3505 str[1] = '\0';
3507 else
3508 str[res] = '\0';
3510 for (i = 0; i <= UTF8_CHAR_LEN && str[i] != '\0'; i++)
3512 char_for_insertion = str[i];
3513 edit_insert (edit, char_for_insertion);
3516 else
3517 #endif
3518 edit_insert (edit, char_for_insertion);
3520 if (edit_options.auto_para_formatting)
3522 format_paragraph (edit, FALSE);
3523 edit->force |= REDRAW_PAGE;
3525 else
3526 check_and_wrap_line (edit);
3527 edit->found_len = 0;
3528 edit->prev_col = edit_get_col (edit);
3529 edit->search_start = edit->buffer.curs1;
3530 edit_find_bracket (edit);
3531 return;
3534 switch (command)
3536 case CK_TopOnScreen:
3537 case CK_BottomOnScreen:
3538 case CK_Top:
3539 case CK_Bottom:
3540 case CK_PageUp:
3541 case CK_PageDown:
3542 case CK_Home:
3543 case CK_End:
3544 case CK_Up:
3545 case CK_Down:
3546 case CK_Left:
3547 case CK_Right:
3548 case CK_WordLeft:
3549 case CK_WordRight:
3550 if (!edit_options.persistent_selections && edit->mark2 >= 0)
3552 if (edit->column_highlight)
3553 edit_push_undo_action (edit, COLUMN_ON);
3554 edit->column_highlight = 0;
3555 edit_mark_cmd (edit, TRUE);
3557 break;
3558 default:
3559 break;
3562 switch (command)
3564 case CK_TopOnScreen:
3565 case CK_BottomOnScreen:
3566 case CK_MarkToPageBegin:
3567 case CK_MarkToPageEnd:
3568 case CK_Up:
3569 case CK_Down:
3570 case CK_WordLeft:
3571 case CK_WordRight:
3572 case CK_MarkToWordBegin:
3573 case CK_MarkToWordEnd:
3574 case CK_MarkUp:
3575 case CK_MarkDown:
3576 case CK_MarkColumnUp:
3577 case CK_MarkColumnDown:
3578 if (edit->mark2 == -1)
3579 break; /*marking is following the cursor: may need to highlight a whole line */
3580 MC_FALLTHROUGH;
3581 case CK_Left:
3582 case CK_Right:
3583 case CK_MarkLeft:
3584 case CK_MarkRight:
3585 edit->force |= REDRAW_CHAR_ONLY;
3586 break;
3587 default:
3588 break;
3591 /* basic cursor key commands */
3592 switch (command)
3594 case CK_BackSpace:
3595 /* if non persistent selection and text selected */
3596 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3597 edit_block_delete_cmd (edit);
3598 else if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3599 edit->over_col--;
3600 else if (edit_options.backspace_through_tabs && is_in_indent (&edit->buffer))
3602 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
3603 edit_backspace (edit, TRUE);
3605 else if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3606 && right_of_four_spaces (edit))
3608 int i;
3610 for (i = 0; i < HALF_TAB_SIZE; i++)
3611 edit_backspace (edit, TRUE);
3613 else
3614 edit_backspace (edit, FALSE);
3615 break;
3616 case CK_Delete:
3617 /* if non persistent selection and text selected */
3618 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3619 edit_block_delete_cmd (edit);
3620 else
3622 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3623 edit_insert_over (edit);
3625 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3626 && left_of_four_spaces (edit))
3628 int i;
3630 for (i = 1; i <= HALF_TAB_SIZE; i++)
3631 edit_delete (edit, TRUE);
3633 else
3634 edit_delete (edit, FALSE);
3636 break;
3637 case CK_DeleteToWordBegin:
3638 edit->over_col = 0;
3639 edit_left_delete_word (edit);
3640 break;
3641 case CK_DeleteToWordEnd:
3642 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3643 edit_insert_over (edit);
3645 edit_right_delete_word (edit);
3646 break;
3647 case CK_DeleteLine:
3648 edit_delete_line (edit);
3649 break;
3650 case CK_DeleteToHome:
3651 edit_delete_to_line_begin (edit);
3652 break;
3653 case CK_DeleteToEnd:
3654 edit_delete_to_line_end (edit);
3655 break;
3656 case CK_Enter:
3657 edit->over_col = 0;
3658 if (edit_options.auto_para_formatting)
3660 edit_double_newline (edit);
3661 if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3662 edit_auto_indent (edit);
3663 format_paragraph (edit, FALSE);
3665 else
3667 edit_insert (edit, '\n');
3668 if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3669 edit_auto_indent (edit);
3671 break;
3672 case CK_Return:
3673 edit_insert (edit, '\n');
3674 break;
3676 case CK_MarkColumnPageUp:
3677 edit->column_highlight = 1;
3678 MC_FALLTHROUGH;
3679 case CK_PageUp:
3680 case CK_MarkPageUp:
3681 edit_move_up (edit, w->lines - 1, TRUE);
3682 break;
3683 case CK_MarkColumnPageDown:
3684 edit->column_highlight = 1;
3685 MC_FALLTHROUGH;
3686 case CK_PageDown:
3687 case CK_MarkPageDown:
3688 edit_move_down (edit, w->lines - 1, TRUE);
3689 break;
3690 case CK_MarkColumnLeft:
3691 edit->column_highlight = 1;
3692 MC_FALLTHROUGH;
3693 case CK_Left:
3694 case CK_MarkLeft:
3695 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3696 && right_of_four_spaces (edit))
3698 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3699 edit->over_col--;
3700 else
3701 edit_cursor_move (edit, -HALF_TAB_SIZE);
3702 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3704 else
3705 edit_left_char_move_cmd (edit);
3706 break;
3707 case CK_MarkColumnRight:
3708 edit->column_highlight = 1;
3709 MC_FALLTHROUGH;
3710 case CK_Right:
3711 case CK_MarkRight:
3712 if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3713 && left_of_four_spaces (edit))
3715 edit_cursor_move (edit, HALF_TAB_SIZE);
3716 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3718 else
3719 edit_right_char_move_cmd (edit);
3720 break;
3721 case CK_TopOnScreen:
3722 case CK_MarkToPageBegin:
3723 edit_begin_page (edit);
3724 break;
3725 case CK_BottomOnScreen:
3726 case CK_MarkToPageEnd:
3727 edit_end_page (edit);
3728 break;
3729 case CK_WordLeft:
3730 case CK_MarkToWordBegin:
3731 edit->over_col = 0;
3732 edit_left_word_move_cmd (edit);
3733 break;
3734 case CK_WordRight:
3735 case CK_MarkToWordEnd:
3736 edit->over_col = 0;
3737 edit_right_word_move_cmd (edit);
3738 break;
3739 case CK_MarkColumnUp:
3740 edit->column_highlight = 1;
3741 MC_FALLTHROUGH;
3742 case CK_Up:
3743 case CK_MarkUp:
3744 edit_move_up (edit, 1, FALSE);
3745 break;
3746 case CK_MarkColumnDown:
3747 edit->column_highlight = 1;
3748 MC_FALLTHROUGH;
3749 case CK_Down:
3750 case CK_MarkDown:
3751 edit_move_down (edit, 1, FALSE);
3752 break;
3753 case CK_MarkColumnParagraphUp:
3754 edit->column_highlight = 1;
3755 MC_FALLTHROUGH;
3756 case CK_ParagraphUp:
3757 case CK_MarkParagraphUp:
3758 edit_move_up_paragraph (edit, FALSE);
3759 break;
3760 case CK_MarkColumnParagraphDown:
3761 edit->column_highlight = 1;
3762 MC_FALLTHROUGH;
3763 case CK_ParagraphDown:
3764 case CK_MarkParagraphDown:
3765 edit_move_down_paragraph (edit, FALSE);
3766 break;
3767 case CK_MarkColumnScrollUp:
3768 edit->column_highlight = 1;
3769 MC_FALLTHROUGH;
3770 case CK_ScrollUp:
3771 case CK_MarkScrollUp:
3772 edit_move_up (edit, 1, TRUE);
3773 break;
3774 case CK_MarkColumnScrollDown:
3775 edit->column_highlight = 1;
3776 MC_FALLTHROUGH;
3777 case CK_ScrollDown:
3778 case CK_MarkScrollDown:
3779 edit_move_down (edit, 1, TRUE);
3780 break;
3781 case CK_Home:
3782 case CK_MarkToHome:
3783 edit_cursor_to_bol (edit);
3784 break;
3785 case CK_End:
3786 case CK_MarkToEnd:
3787 edit_cursor_to_eol (edit);
3788 break;
3789 case CK_Tab:
3790 /* if text marked shift block */
3791 if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3793 if (edit->mark2 < 0)
3794 edit_mark_cmd (edit, FALSE);
3795 edit_move_block_to_right (edit);
3797 else
3799 if (edit_options.cursor_beyond_eol)
3800 edit_insert_over (edit);
3801 edit_tab_cmd (edit);
3802 if (edit_options.auto_para_formatting)
3804 format_paragraph (edit, FALSE);
3805 edit->force |= REDRAW_PAGE;
3807 else
3808 check_and_wrap_line (edit);
3810 break;
3812 case CK_InsertOverwrite:
3813 edit->overwrite = !edit->overwrite;
3814 break;
3816 case CK_Mark:
3817 if (edit->mark2 >= 0)
3819 if (edit->column_highlight)
3820 edit_push_undo_action (edit, COLUMN_ON);
3821 edit->column_highlight = 0;
3823 edit_mark_cmd (edit, FALSE);
3824 break;
3825 case CK_MarkColumn:
3826 if (!edit->column_highlight)
3827 edit_push_undo_action (edit, COLUMN_OFF);
3828 edit->column_highlight = 1;
3829 edit_mark_cmd (edit, FALSE);
3830 break;
3831 case CK_MarkAll:
3832 edit_set_markers (edit, 0, edit->buffer.size, 0, 0);
3833 edit->force |= REDRAW_PAGE;
3834 break;
3835 case CK_Unmark:
3836 if (edit->column_highlight)
3837 edit_push_undo_action (edit, COLUMN_ON);
3838 edit->column_highlight = 0;
3839 edit_mark_cmd (edit, TRUE);
3840 break;
3841 case CK_MarkWord:
3842 if (edit->column_highlight)
3843 edit_push_undo_action (edit, COLUMN_ON);
3844 edit->column_highlight = 0;
3845 edit_mark_current_word_cmd (edit);
3846 break;
3847 case CK_MarkLine:
3848 if (edit->column_highlight)
3849 edit_push_undo_action (edit, COLUMN_ON);
3850 edit->column_highlight = 0;
3851 edit_mark_current_line_cmd (edit);
3852 break;
3854 case CK_Bookmark:
3855 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR);
3856 if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR))
3857 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3858 else
3859 book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3860 break;
3861 case CK_BookmarkFlush:
3862 book_mark_flush (edit, BOOK_MARK_COLOR);
3863 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3864 edit->force |= REDRAW_PAGE;
3865 break;
3866 case CK_BookmarkNext:
3867 if (edit->book_mark != NULL)
3869 edit_book_mark_t *p;
3871 p = book_mark_find (edit, edit->buffer.curs_line);
3872 if (p->next != NULL)
3874 p = p->next;
3875 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3876 edit_move_display (edit, p->line - w->lines / 2);
3877 edit_move_to_line (edit, p->line);
3880 break;
3881 case CK_BookmarkPrev:
3882 if (edit->book_mark != NULL)
3884 edit_book_mark_t *p;
3886 p = book_mark_find (edit, edit->buffer.curs_line);
3887 while (p->line == edit->buffer.curs_line)
3888 if (p->prev != NULL)
3889 p = p->prev;
3890 if (p->line >= 0)
3892 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3893 edit_move_display (edit, p->line - w->lines / 2);
3894 edit_move_to_line (edit, p->line);
3897 break;
3899 case CK_Top:
3900 case CK_MarkToFileBegin:
3901 edit_move_to_top (edit);
3902 break;
3903 case CK_Bottom:
3904 case CK_MarkToFileEnd:
3905 edit_move_to_bottom (edit);
3906 break;
3908 case CK_Copy:
3909 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3910 edit_insert_over (edit);
3911 edit_block_copy_cmd (edit);
3912 break;
3913 case CK_Remove:
3914 edit_block_delete_cmd (edit);
3915 break;
3916 case CK_Move:
3917 edit_block_move_cmd (edit);
3918 break;
3920 case CK_BlockShiftLeft:
3921 if (edit->mark1 != edit->mark2)
3922 edit_move_block_to_left (edit);
3923 break;
3924 case CK_BlockShiftRight:
3925 if (edit->mark1 != edit->mark2)
3926 edit_move_block_to_right (edit);
3927 break;
3928 case CK_Store:
3929 edit_copy_to_X_buf_cmd (edit);
3930 break;
3931 case CK_Cut:
3932 edit_cut_to_X_buf_cmd (edit);
3933 break;
3934 case CK_Paste:
3935 /* if non persistent selection and text selected */
3936 if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3937 edit_block_delete_cmd (edit);
3938 if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3939 edit_insert_over (edit);
3940 edit_paste_from_X_buf_cmd (edit);
3941 if (!edit_options.persistent_selections && edit->mark2 >= 0)
3943 if (edit->column_highlight)
3944 edit_push_undo_action (edit, COLUMN_ON);
3945 edit->column_highlight = 0;
3946 edit_mark_cmd (edit, TRUE);
3948 break;
3949 case CK_History:
3950 edit_paste_from_history (edit);
3951 break;
3953 case CK_SaveAs:
3954 edit_save_as_cmd (edit);
3955 break;
3956 case CK_Save:
3957 edit_save_confirm_cmd (edit);
3958 break;
3959 case CK_BlockSave:
3960 edit_save_block_cmd (edit);
3961 break;
3962 case CK_InsertFile:
3963 edit_insert_file_cmd (edit);
3964 break;
3966 case CK_FilePrev:
3967 edit_load_back_cmd (edit);
3968 break;
3969 case CK_FileNext:
3970 edit_load_forward_cmd (edit);
3971 break;
3973 case CK_SyntaxChoose:
3974 edit_syntax_dialog (edit);
3975 break;
3977 case CK_Search:
3978 edit_search_cmd (edit, FALSE);
3979 break;
3980 case CK_SearchContinue:
3981 edit_search_cmd (edit, TRUE);
3982 break;
3983 case CK_Replace:
3984 edit_replace_cmd (edit, FALSE);
3985 break;
3986 case CK_ReplaceContinue:
3987 edit_replace_cmd (edit, TRUE);
3988 break;
3989 case CK_Complete:
3990 /* if text marked shift block */
3991 if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3992 edit_move_block_to_left (edit);
3993 else
3994 edit_complete_word_cmd (edit);
3995 break;
3996 case CK_Find:
3997 edit_get_match_keyword_cmd (edit);
3998 break;
4000 #ifdef HAVE_ASPELL
4001 case CK_SpellCheckCurrentWord:
4002 edit_suggest_current_word (edit);
4003 break;
4004 case CK_SpellCheck:
4005 edit_spellcheck_file (edit);
4006 break;
4007 case CK_SpellCheckSelectLang:
4008 edit_set_spell_lang ();
4009 break;
4010 #endif
4012 case CK_Date:
4014 char s[BUF_MEDIUM];
4015 /* fool gcc to prevent a Y2K warning */
4016 char time_format[] = "_c";
4017 time_format[0] = '%';
4019 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4020 edit_print_string (edit, s);
4021 edit->force |= REDRAW_PAGE;
4023 break;
4024 case CK_Goto:
4025 edit_goto_cmd (edit);
4026 break;
4027 case CK_ParagraphFormat:
4028 format_paragraph (edit, TRUE);
4029 edit->force |= REDRAW_PAGE;
4030 break;
4031 case CK_MacroDelete:
4032 edit_delete_macro_cmd (edit);
4033 break;
4034 case CK_MatchBracket:
4035 edit_goto_matching_bracket (edit);
4036 break;
4037 case CK_UserMenu:
4038 edit_user_menu (edit, NULL, -1);
4039 break;
4040 case CK_Sort:
4041 edit_sort_cmd (edit);
4042 break;
4043 case CK_ExternalCommand:
4044 edit_ext_cmd (edit);
4045 break;
4046 case CK_EditMail:
4047 edit_mail_dialog (edit);
4048 break;
4049 #ifdef HAVE_CHARSET
4050 case CK_SelectCodepage:
4051 edit_select_codepage_cmd (edit);
4052 break;
4053 #endif
4054 case CK_InsertLiteral:
4055 edit_insert_literal_cmd (edit);
4056 break;
4057 case CK_MacroStartStopRecord:
4058 edit_begin_end_macro_cmd (edit);
4059 break;
4060 case CK_RepeatStartStopRecord:
4061 edit_begin_end_repeat_cmd (edit);
4062 break;
4063 case CK_ExtendedKeyMap:
4064 WIDGET (edit)->ext_mode = TRUE;
4065 break;
4066 default:
4067 break;
4070 /* CK_PipeBlock */
4071 if ((command / CK_PipeBlock (0)) == 1)
4072 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4074 /* keys which must set the col position, and the search vars */
4075 switch (command)
4077 case CK_Search:
4078 case CK_SearchContinue:
4079 case CK_Replace:
4080 case CK_ReplaceContinue:
4081 case CK_Complete:
4082 edit->prev_col = edit_get_col (edit);
4083 break;
4084 case CK_Up:
4085 case CK_MarkUp:
4086 case CK_MarkColumnUp:
4087 case CK_Down:
4088 case CK_MarkDown:
4089 case CK_MarkColumnDown:
4090 case CK_PageUp:
4091 case CK_MarkPageUp:
4092 case CK_MarkColumnPageUp:
4093 case CK_PageDown:
4094 case CK_MarkPageDown:
4095 case CK_MarkColumnPageDown:
4096 case CK_Top:
4097 case CK_MarkToFileBegin:
4098 case CK_Bottom:
4099 case CK_MarkToFileEnd:
4100 case CK_ParagraphUp:
4101 case CK_MarkParagraphUp:
4102 case CK_MarkColumnParagraphUp:
4103 case CK_ParagraphDown:
4104 case CK_MarkParagraphDown:
4105 case CK_MarkColumnParagraphDown:
4106 case CK_ScrollUp:
4107 case CK_MarkScrollUp:
4108 case CK_MarkColumnScrollUp:
4109 case CK_ScrollDown:
4110 case CK_MarkScrollDown:
4111 case CK_MarkColumnScrollDown:
4112 edit->search_start = edit->buffer.curs1;
4113 edit->found_len = 0;
4114 break;
4115 default:
4116 edit->found_len = 0;
4117 edit->prev_col = edit_get_col (edit);
4118 edit->search_start = edit->buffer.curs1;
4120 edit_find_bracket (edit);
4122 if (edit_options.auto_para_formatting)
4124 switch (command)
4126 case CK_BackSpace:
4127 case CK_Delete:
4128 case CK_DeleteToWordBegin:
4129 case CK_DeleteToWordEnd:
4130 case CK_DeleteToHome:
4131 case CK_DeleteToEnd:
4132 format_paragraph (edit, FALSE);
4133 edit->force |= REDRAW_PAGE;
4134 break;
4135 default:
4136 break;
4141 /* --------------------------------------------------------------------------------------------- */
4143 void
4144 edit_stack_init (void)
4146 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4147 edit_arg_init (&edit_history_moveto[edit_stack_iterator], NULL, -1);
4149 edit_stack_iterator = 0;
4152 /* --------------------------------------------------------------------------------------------- */
4154 void
4155 edit_stack_free (void)
4157 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4158 vfs_path_free (edit_history_moveto[edit_stack_iterator].file_vpath, TRUE);
4161 /* --------------------------------------------------------------------------------------------- */
4162 /** move i lines */
4164 void
4165 edit_move_up (WEdit *edit, long i, gboolean do_scroll)
4167 edit_move_updown (edit, i, do_scroll, TRUE);
4170 /* --------------------------------------------------------------------------------------------- */
4171 /** move i lines */
4173 void
4174 edit_move_down (WEdit *edit, long i, gboolean do_scroll)
4176 edit_move_updown (edit, i, do_scroll, FALSE);
4179 /* --------------------------------------------------------------------------------------------- */
4181 * Create edit_arg_t object from vfs_path_t object and the line number.
4183 * @param file_vpath file path object
4184 * @param line_number line number. If value is 0, try to restore saved position.
4185 * @return edit_arg_t object
4188 edit_arg_t *
4189 edit_arg_vpath_new (vfs_path_t *file_vpath, long line_number)
4191 edit_arg_t *arg;
4193 arg = g_new (edit_arg_t, 1);
4194 arg->file_vpath = file_vpath;
4195 arg->line_number = line_number;
4197 return arg;
4200 /* --------------------------------------------------------------------------------------------- */
4202 * Create edit_arg_t object from file name and the line number.
4204 * @param file_name file name
4205 * @param line_number line number. If value is 0, try to restore saved position.
4206 * @return edit_arg_t object
4209 edit_arg_t *
4210 edit_arg_new (const char *file_name, long line_number)
4212 return edit_arg_vpath_new (vfs_path_from_str (file_name), line_number);
4215 /* --------------------------------------------------------------------------------------------- */
4217 * Initialize edit_arg_t object.
4219 * @param arg edit_arg_t object
4220 * @param vpath vfs_path_t object
4221 * @param line line number
4224 void
4225 edit_arg_init (edit_arg_t *arg, vfs_path_t *vpath, long line)
4227 arg->file_vpath = (vfs_path_t *) vpath;
4228 arg->line_number = line;
4231 /* --------------------------------------------------------------------------------------------- */
4233 * Apply new values to edit_arg_t object members.
4235 * @param arg edit_arg_t object
4236 * @param vpath vfs_path_t object
4237 * @param line line number
4240 void
4241 edit_arg_assign (edit_arg_t *arg, vfs_path_t *vpath, long line)
4243 vfs_path_free (arg->file_vpath, TRUE);
4244 edit_arg_init (arg, vpath, line);
4247 /* --------------------------------------------------------------------------------------------- */
4249 * Free the edit_arg_t object.
4251 * @param arg edit_arg_t object
4254 void
4255 edit_arg_free (edit_arg_t *arg)
4257 vfs_path_free (arg->file_vpath, TRUE);
4258 g_free (arg);
4261 /* --------------------------------------------------------------------------------------------- */
4263 const char *
4264 edit_get_file_name (const WEdit *edit)
4266 return vfs_path_as_str (edit->filename_vpath);
4269 /* --------------------------------------------------------------------------------------------- */