cppcheck: reduce variable scope.
[midnight-commander.git] / src / editor / edit.c
blob66a97fe4dc6f2963d512fed11e5f6a459cae88d2
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012, 2013
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdint.h> /* UINTMAX_MAX */
45 #include <stdlib.h>
46 #include <fcntl.h>
48 #include "lib/global.h"
50 #include "lib/tty/color.h"
51 #include "lib/tty/tty.h" /* attrset() */
52 #include "lib/tty/key.h" /* is_idle() */
53 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
54 #include "lib/vfs/vfs.h"
55 #include "lib/strutil.h" /* utf string functions */
56 #include "lib/util.h" /* load_file_position(), save_file_position() */
57 #include "lib/timefmt.h" /* time formatting */
58 #include "lib/lock.h"
59 #include "lib/widget.h"
61 #ifdef HAVE_CHARSET
62 #include "lib/charsets.h" /* get_codepage_id */
63 #endif
65 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
73 #ifdef HAVE_ASPELL
74 #include "spell.h"
75 #endif
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
80 int option_typewriter_wrap = 0;
81 int option_auto_para_formatting = 0;
82 int option_fill_tabs_with_spaces = 0;
83 int option_return_does_auto_indent = 1;
84 int option_backspace_through_tabs = 0;
85 int option_fake_half_tabs = 1;
86 int option_save_mode = EDIT_QUICK_SAVE;
87 int option_save_position = 1;
88 int option_max_undo = 32768;
89 int option_persistent_selections = 1;
90 int option_cursor_beyond_eol = 0;
91 int option_line_state = 0;
92 int option_line_state_width = 0;
93 gboolean option_cursor_after_inserted_block = FALSE;
95 int option_edit_right_extreme = 0;
96 int option_edit_left_extreme = 0;
97 int option_edit_top_extreme = 0;
98 int option_edit_bottom_extreme = 0;
99 int enable_show_tabs_tws = 1;
100 int option_check_nl_at_eof = 0;
101 int option_group_undo = 0;
102 int show_right_margin = 0;
104 char *option_backup_ext = NULL;
105 char *option_filesize_threshold = NULL;
107 unsigned int edit_stack_iterator = 0;
108 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
109 /* magic sequense for say than block is vertical */
110 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
112 /*** file scope macro definitions ****************************************************************/
114 #define TEMP_BUF_LEN 1024
116 #define space_width 1
118 /*** file scope type declarations ****************************************************************/
120 /*** file scope variables ************************************************************************/
122 /* detecting an error on save is easy: just check if every byte has been written. */
123 /* detecting an error on read, is not so easy 'cos there is not way to tell
124 whether you read everything or not. */
125 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
126 static const struct edit_filters
128 const char *read, *write, *extension;
129 } all_filters[] =
131 /* *INDENT-OFF* */
132 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
133 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
134 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
136 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
137 /* *INDENT-ON* */
140 static const off_t option_filesize_default_threshold = 64 * 1024 * 1024; /* 64 MB */
142 /* --------------------------------------------------------------------------------------------- */
143 /*** file scope functions ************************************************************************/
144 /* --------------------------------------------------------------------------------------------- */
146 * Load file OR text into buffers. Set cursor to the beginning of file.
148 * @return FALSE on error.
151 static gboolean
152 edit_load_file_fast (edit_buffer_t * buf, const vfs_path_t * filename_vpath)
154 int file;
155 gboolean ret;
157 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
158 if (file < 0)
160 gchar *errmsg;
162 errmsg =
163 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
164 edit_error_dialog (_("Error"), errmsg);
165 g_free (errmsg);
166 return FALSE;
169 ret = (edit_buffer_read_file (buf, file, buf->size) == buf->size);
170 if (!ret)
172 gchar *errmsg;
174 errmsg = g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath));
175 edit_error_dialog (_("Error"), errmsg);
176 g_free (errmsg);
179 mc_close (file);
180 return ret;
183 /* --------------------------------------------------------------------------------------------- */
184 /** Return index of the filter or -1 is there is no appropriate filter */
186 static int
187 edit_find_filter (const vfs_path_t * filename_vpath)
189 size_t i, l;
191 if (filename_vpath == NULL)
192 return -1;
194 l = strlen (vfs_path_as_str (filename_vpath));
195 for (i = 0; i < G_N_ELEMENTS (all_filters); i++)
197 size_t e;
199 e = strlen (all_filters[i].extension);
200 if (l > e)
201 if (!strcmp (all_filters[i].extension, vfs_path_as_str (filename_vpath) + l - e))
202 return i;
204 return -1;
207 /* --------------------------------------------------------------------------------------------- */
209 static char *
210 edit_get_filter (const vfs_path_t * filename_vpath)
212 int i;
213 char *p, *quoted_name;
215 i = edit_find_filter (filename_vpath);
216 if (i < 0)
217 return NULL;
219 quoted_name = name_quote (vfs_path_as_str (filename_vpath), 0);
220 p = g_strdup_printf (all_filters[i].read, quoted_name);
221 g_free (quoted_name);
222 return p;
225 /* --------------------------------------------------------------------------------------------- */
227 static off_t
228 edit_insert_stream (WEdit * edit, FILE * f)
230 int c;
231 off_t i = 0;
233 while ((c = fgetc (f)) >= 0)
235 edit_insert (edit, c);
236 i++;
238 return i;
241 /* --------------------------------------------------------------------------------------------- */
243 * Open file and create it if necessary.
245 * @param edit editor object
246 * @param filename_vpath file name
247 * @param st buffer for store stat info
248 * @return TRUE for success, FALSE for error.
251 static gboolean
252 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
254 static uintmax_t threshold = UINTMAX_MAX;
255 int file;
256 gchar *errmsg = NULL;
257 gboolean ret = TRUE;
259 /* Try opening an existing file */
260 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
261 if (file < 0)
264 * Try creating the file. O_EXCL prevents following broken links
265 * and opening existing files.
267 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
268 if (file < 0)
270 errmsg =
271 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
272 goto cleanup;
275 /* New file, delete it if it's not modified or saved */
276 edit->delete_file = 1;
279 /* Check what we have opened */
280 if (mc_fstat (file, st) < 0)
282 errmsg =
283 g_strdup_printf (_("Cannot get size/permissions for %s"),
284 vfs_path_as_str (filename_vpath));
285 goto cleanup;
288 /* We want to open regular files only */
289 if (!S_ISREG (st->st_mode))
291 errmsg =
292 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath));
293 goto cleanup;
296 /* get file size threshold for alarm */
297 if (threshold == UINTMAX_MAX)
299 gboolean err = FALSE;
301 threshold = parse_integer (option_filesize_threshold, &err);
302 if (err)
303 threshold = option_filesize_default_threshold;
307 * Don't delete non-empty files.
308 * O_EXCL should prevent it, but let's be on the safe side.
310 if (st->st_size > 0)
311 edit->delete_file = 0;
313 if ((uintmax_t) st->st_size > threshold)
315 int act;
317 errmsg = g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"),
318 vfs_path_as_str (filename_vpath));
319 act = edit_query_dialog2 (_("Warning"), errmsg, _("&Yes"), _("&No"));
320 g_free (errmsg);
321 errmsg = NULL;
323 if (act != 0)
324 ret = FALSE;
327 cleanup:
328 (void) mc_close (file);
330 if (errmsg != NULL)
332 edit_error_dialog (_("Error"), errmsg);
333 g_free (errmsg);
334 ret = FALSE;
337 return ret;
340 /* --------------------------------------------------------------------------------------------- */
343 * Open the file and load it into the buffers, either directly or using
344 * a filter. Return TRUE on success, FALSE on error.
346 * Fast loading (edit_load_file_fast) is used when the file size is
347 * known. In this case the data is read into the buffers by blocks.
348 * If the file size is not known, the data is loaded byte by byte in
349 * edit_insert_file.
351 * @param edit editor object
352 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
354 static gboolean
355 edit_load_file (WEdit * edit)
357 gboolean fast_load = TRUE;
359 /* Cannot do fast load if a filter is used */
360 if (edit_find_filter (edit->filename_vpath) >= 0)
361 fast_load = FALSE;
364 * FIXME: line end translation should disable fast loading as well
365 * Consider doing fseek() to the end and ftell() for the real size.
367 if (edit->filename_vpath != NULL)
370 * VFS may report file size incorrectly, and slow load is not a big
371 * deal considering overhead in VFS.
373 if (!vfs_file_is_local (edit->filename_vpath))
374 fast_load = FALSE;
376 /* If we are dealing with a real file, check that it exists */
377 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
379 edit_clean (edit);
380 return FALSE;
383 else
385 /* nothing to load */
386 fast_load = FALSE;
389 if (fast_load)
391 edit_buffer_init (&edit->buffer, edit->stat1.st_size);
393 if (!edit_load_file_fast (&edit->buffer, edit->filename_vpath))
395 edit_clean (edit);
396 return FALSE;
399 else
401 edit_buffer_init (&edit->buffer, 0);
403 if (edit->filename_vpath != NULL
404 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
406 edit->undo_stack_disable = 1;
407 if (edit_insert_file (edit, edit->filename_vpath) < 0)
409 edit_clean (edit);
410 return FALSE;
412 edit->undo_stack_disable = 0;
415 edit->lb = LB_ASIS;
416 return TRUE;
419 /* --------------------------------------------------------------------------------------------- */
420 /** Restore saved cursor position in the file */
422 static void
423 edit_load_position (WEdit * edit)
425 long line, column;
426 off_t offset;
428 if (edit->filename_vpath == NULL
429 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
430 return;
432 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
434 if (line > 0)
436 edit_move_to_line (edit, line - 1);
437 edit->prev_col = column;
439 else if (offset > 0)
441 edit_cursor_move (edit, offset);
442 line = edit->buffer.curs_line;
443 edit->search_start = edit->buffer.curs1;
446 book_mark_restore (edit, BOOK_MARK_COLOR);
448 edit_move_to_prev_col (edit, edit_buffer_get_current_bol (&edit->buffer));
449 edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
452 /* --------------------------------------------------------------------------------------------- */
453 /** Save cursor position in the file */
455 static void
456 edit_save_position (WEdit * edit)
458 if (edit->filename_vpath == NULL
459 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
460 return;
462 book_mark_serialize (edit, BOOK_MARK_COLOR);
463 save_file_position (edit->filename_vpath, edit->buffer.curs_line + 1, edit->curs_col,
464 edit->buffer.curs1, edit->serialized_bookmarks);
465 edit->serialized_bookmarks = NULL;
468 /* --------------------------------------------------------------------------------------------- */
469 /** Clean the WEdit stricture except the widget part */
471 static void
472 edit_purge_widget (WEdit * edit)
474 size_t len = sizeof (WEdit) - sizeof (Widget);
475 char *start = (char *) edit + sizeof (Widget);
476 memset (start, 0, len);
479 /* --------------------------------------------------------------------------------------------- */
482 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
483 then the file should be as it was when he loaded up. Then set edit->modified to 0.
486 static long
487 edit_pop_undo_action (WEdit * edit)
489 long c;
490 unsigned long sp = edit->undo_stack_pointer;
492 if (sp == edit->undo_stack_bottom)
493 return STACK_BOTTOM;
495 sp = (sp - 1) & edit->undo_stack_size_mask;
496 c = edit->undo_stack[sp];
497 if (c >= 0)
499 /* edit->undo_stack[sp] = '@'; */
500 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
501 return c;
504 if (sp == edit->undo_stack_bottom)
505 return STACK_BOTTOM;
507 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
508 if (edit->undo_stack[sp] == -2)
510 /* edit->undo_stack[sp] = '@'; */
511 edit->undo_stack_pointer = sp;
513 else
514 edit->undo_stack[sp]++;
516 return c;
519 static long
520 edit_pop_redo_action (WEdit * edit)
522 long c;
523 unsigned long sp = edit->redo_stack_pointer;
525 if (sp == edit->redo_stack_bottom)
526 return STACK_BOTTOM;
528 sp = (sp - 1) & edit->redo_stack_size_mask;
529 c = edit->redo_stack[sp];
530 if (c >= 0)
532 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
533 return c;
536 if (sp == edit->redo_stack_bottom)
537 return STACK_BOTTOM;
539 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
540 if (edit->redo_stack[sp] == -2)
541 edit->redo_stack_pointer = sp;
542 else
543 edit->redo_stack[sp]++;
545 return c;
548 static long
549 get_prev_undo_action (WEdit * edit)
551 long c;
552 unsigned long sp = edit->undo_stack_pointer;
554 if (sp == edit->undo_stack_bottom)
555 return STACK_BOTTOM;
557 sp = (sp - 1) & edit->undo_stack_size_mask;
558 c = edit->undo_stack[sp];
559 if (c >= 0)
560 return c;
562 if (sp == edit->undo_stack_bottom)
563 return STACK_BOTTOM;
565 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
566 return c;
569 /* --------------------------------------------------------------------------------------------- */
570 /** is called whenever a modification is made by one of the four routines below */
572 static void
573 edit_modification (WEdit * edit)
575 edit->caches_valid = FALSE;
577 /* raise lock when file modified */
578 if (!edit->modified && !edit->delete_file)
579 edit->locked = lock_file (edit->filename_vpath);
580 edit->modified = 1;
583 /* --------------------------------------------------------------------------------------------- */
584 /* high level cursor movement commands */
585 /* --------------------------------------------------------------------------------------------- */
586 /** check whether cursor is in indent part of line
588 * @param edit editor object
590 * @return TRUE if cursor is in indent, FALSE otherwise
593 static gboolean
594 is_in_indent (const edit_buffer_t * buf)
596 off_t p;
598 for (p = edit_buffer_get_current_bol (buf); p < buf->curs1; p++)
599 if (strchr (" \t", edit_buffer_get_byte (buf, p)) == NULL)
600 return FALSE;
602 return TRUE;
605 /* --------------------------------------------------------------------------------------------- */
606 /** check whether line in editor is blank or not
608 * @param edit editor object
609 * @param offset position in file
611 * @return TRUE if line in blank, FALSE otherwise
614 static gboolean
615 is_blank (const edit_buffer_t * buf, off_t offset)
617 off_t s, f;
619 s = edit_buffer_get_bol (buf, offset);
620 f = edit_buffer_get_eol (buf, offset) - 1;
621 while (s <= f)
623 int c;
625 c = edit_buffer_get_byte (buf, s++);
626 if (!isspace (c))
627 return FALSE;
629 return TRUE;
632 /* --------------------------------------------------------------------------------------------- */
633 /** returns the offset of line i */
635 static off_t
636 edit_find_line (WEdit * edit, long line)
638 long i, j = 0;
639 long m = 2000000000; /* what is the magic number? */
641 if (!edit->caches_valid)
643 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
644 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
645 /* three offsets that we *know* are line 0 at 0 and these two: */
646 edit->line_numbers[1] = edit->buffer.curs_line;
647 edit->line_offsets[1] = edit_buffer_get_current_bol (&edit->buffer);
648 edit->line_numbers[2] = edit->buffer.lines;
649 edit->line_offsets[2] = edit_buffer_get_bol (&edit->buffer, edit->buffer.size);
650 edit->caches_valid = TRUE;
652 if (line >= edit->buffer.lines)
653 return edit->line_offsets[2];
654 if (line <= 0)
655 return 0;
656 /* find the closest known point */
657 for (i = 0; i < N_LINE_CACHES; i++)
659 long n;
661 n = abs (edit->line_numbers[i] - line);
662 if (n < m)
664 m = n;
665 j = i;
668 if (m == 0)
669 return edit->line_offsets[j]; /* know the offset exactly */
670 if (m == 1 && j >= 3)
671 i = j; /* one line different - caller might be looping, so stay in this cache */
672 else
673 i = 3 + (rand () % (N_LINE_CACHES - 3));
674 if (line > edit->line_numbers[j])
675 edit->line_offsets[i] =
676 edit_buffer_move_forward (&edit->buffer, edit->line_offsets[j],
677 line - edit->line_numbers[j], 0);
678 else
679 edit->line_offsets[i] =
680 edit_buffer_move_backward (&edit->buffer, edit->line_offsets[j],
681 edit->line_numbers[j] - line);
682 edit->line_numbers[i] = line;
683 return edit->line_offsets[i];
686 /* --------------------------------------------------------------------------------------------- */
687 /** moves up until a blank line is reached, or until just
688 before a non-blank line is reached */
690 static void
691 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
693 long i = 0;
695 if (edit->buffer.curs_line > 1)
697 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
699 for (i = edit->buffer.curs_line - 1; i != 0; i--)
700 if (edit_line_is_blank (edit, i))
701 break;
703 else if (edit_line_is_blank (edit, edit->buffer.curs_line - 1))
705 for (i = edit->buffer.curs_line - 1; i != 0; i--)
706 if (!edit_line_is_blank (edit, i))
708 i++;
709 break;
712 else
714 for (i = edit->buffer.curs_line - 1; i != 0; i--)
715 if (edit_line_is_blank (edit, i))
716 break;
720 edit_move_up (edit, edit->buffer.curs_line - i, do_scroll);
723 /* --------------------------------------------------------------------------------------------- */
724 /** moves down until a blank line is reached, or until just
725 before a non-blank line is reached */
727 static void
728 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
730 long i;
732 if (edit->buffer.curs_line >= edit->buffer.lines - 1)
733 i = edit->buffer.lines;
734 else if (!edit_line_is_blank (edit, edit->buffer.curs_line))
736 for (i = edit->buffer.curs_line + 1; i != 0; i++)
737 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
738 break;
740 else if (edit_line_is_blank (edit, edit->buffer.curs_line + 1))
742 for (i = edit->buffer.curs_line + 1; i != 0; i++)
743 if (!edit_line_is_blank (edit, i) || i > edit->buffer.lines)
745 i--;
746 break;
749 else
751 for (i = edit->buffer.curs_line + 1; i != 0; i++)
752 if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
753 break;
755 edit_move_down (edit, i - edit->buffer.curs_line, do_scroll);
758 /* --------------------------------------------------------------------------------------------- */
760 static void
761 edit_begin_page (WEdit * edit)
763 edit_update_curs_row (edit);
764 edit_move_up (edit, edit->curs_row, 0);
767 /* --------------------------------------------------------------------------------------------- */
769 static void
770 edit_end_page (WEdit * edit)
772 edit_update_curs_row (edit);
773 edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, 0);
777 /* --------------------------------------------------------------------------------------------- */
778 /** goto beginning of text */
780 static void
781 edit_move_to_top (WEdit * edit)
783 if (edit->buffer.curs_line != 0)
785 edit_cursor_move (edit, -edit->buffer.curs1);
786 edit_move_to_prev_col (edit, 0);
787 edit->force |= REDRAW_PAGE;
788 edit->search_start = 0;
789 edit_update_curs_row (edit);
793 /* --------------------------------------------------------------------------------------------- */
794 /** goto end of text */
796 static void
797 edit_move_to_bottom (WEdit * edit)
799 if (edit->buffer.curs_line < edit->buffer.lines)
801 edit_move_down (edit, edit->buffer.lines - edit->curs_row, 0);
802 edit->start_display = edit->buffer.size;
803 edit->start_line = edit->buffer.lines;
804 edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
805 edit->force |= REDRAW_PAGE;
809 /* --------------------------------------------------------------------------------------------- */
810 /** goto beginning of line */
812 static void
813 edit_cursor_to_bol (WEdit * edit)
815 edit_cursor_move (edit, edit_buffer_get_current_bol (&edit->buffer) - edit->buffer.curs1);
816 edit->search_start = edit->buffer.curs1;
817 edit->prev_col = edit_get_col (edit);
818 edit->over_col = 0;
821 /* --------------------------------------------------------------------------------------------- */
822 /** goto end of line */
824 static void
825 edit_cursor_to_eol (WEdit * edit)
827 edit_cursor_move (edit, edit_buffer_get_current_eol (&edit->buffer) - edit->buffer.curs1);
828 edit->search_start = edit->buffer.curs1;
829 edit->prev_col = edit_get_col (edit);
830 edit->over_col = 0;
833 /* --------------------------------------------------------------------------------------------- */
835 static unsigned long
836 my_type_of (int c)
838 int x, r = 0;
839 const char *p, *q;
840 const char option_chars_move_whole_word[] =
841 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
843 if (c == 0)
844 return 0;
845 if (c == '!')
847 if (*option_chars_move_whole_word == '!')
848 return 2;
849 return 0x80000000UL;
851 if (g_ascii_isupper ((gchar) c))
852 c = 'A';
853 else if (g_ascii_islower ((gchar) c))
854 c = 'a';
855 else if (g_ascii_isalpha (c))
856 c = 'a';
857 else if (isdigit (c))
858 c = '0';
859 else if (isspace (c))
860 c = ' ';
861 q = strchr (option_chars_move_whole_word, c);
862 if (!q)
863 return 0xFFFFFFFFUL;
866 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
867 if (*p == '!')
868 x <<= 1;
869 r |= x;
871 while ((q = strchr (q + 1, c)));
872 return r;
875 /* --------------------------------------------------------------------------------------------- */
877 static void
878 edit_left_word_move (WEdit * edit, int s)
880 while (TRUE)
882 int c1, c2;
884 if (edit->column_highlight
885 && edit->mark1 != edit->mark2
886 && edit->over_col == 0
887 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
888 break;
889 edit_cursor_move (edit, -1);
890 if (edit->buffer.curs1 == 0)
891 break;
892 c1 = edit_buffer_get_previous_byte (&edit->buffer);
893 c2 = edit_buffer_get_current_byte (&edit->buffer);
894 if (c1 == '\n' || c2 == '\n')
895 break;
896 if ((my_type_of (c1) & my_type_of (c2)) == 0)
897 break;
898 if (isspace (c1) && !isspace (c2))
899 break;
900 if (s != 0 && !isspace (c1) && isspace (c2))
901 break;
905 /* --------------------------------------------------------------------------------------------- */
907 static void
908 edit_left_word_move_cmd (WEdit * edit)
910 edit_left_word_move (edit, 0);
911 edit->force |= REDRAW_PAGE;
914 /* --------------------------------------------------------------------------------------------- */
916 static void
917 edit_right_word_move (WEdit * edit, int s)
919 while (TRUE)
921 int c1, c2;
923 if (edit->column_highlight
924 && edit->mark1 != edit->mark2
925 && edit->over_col == 0
926 && edit->buffer.curs1 == edit_buffer_get_current_eol (&edit->buffer))
927 break;
928 edit_cursor_move (edit, 1);
929 if (edit->buffer.curs1 >= edit->buffer.size)
930 break;
931 c1 = edit_buffer_get_previous_byte (&edit->buffer);
932 c2 = edit_buffer_get_current_byte (&edit->buffer);
933 if (c1 == '\n' || c2 == '\n')
934 break;
935 if ((my_type_of (c1) & my_type_of (c2)) == 0)
936 break;
937 if (isspace (c1) && !isspace (c2))
938 break;
939 if (s != 0 && !isspace (c1) && isspace (c2))
940 break;
944 /* --------------------------------------------------------------------------------------------- */
946 static void
947 edit_right_word_move_cmd (WEdit * edit)
949 edit_right_word_move (edit, 0);
950 edit->force |= REDRAW_PAGE;
953 /* --------------------------------------------------------------------------------------------- */
955 static void
956 edit_right_char_move_cmd (WEdit * edit)
958 int cw = 1;
959 int c;
961 #ifdef HAVE_CHARSET
962 if (edit->utf8)
964 c = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &cw);
965 if (cw < 1)
966 cw = 1;
968 else
969 #endif
970 c = edit_buffer_get_current_byte (&edit->buffer);
972 if (option_cursor_beyond_eol && c == '\n')
973 edit->over_col++;
974 else
975 edit_cursor_move (edit, cw);
978 /* --------------------------------------------------------------------------------------------- */
980 static void
981 edit_left_char_move_cmd (WEdit * edit)
983 int cw = 1;
985 if (edit->column_highlight
986 && option_cursor_beyond_eol
987 && edit->mark1 != edit->mark2
988 && edit->over_col == 0 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
989 return;
990 #ifdef HAVE_CHARSET
991 if (edit->utf8)
993 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &cw);
994 if (cw < 1)
995 cw = 1;
997 #endif
999 if (option_cursor_beyond_eol && edit->over_col > 0)
1000 edit->over_col--;
1001 else
1002 edit_cursor_move (edit, -cw);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Up or down cursor moving.
1007 direction = TRUE - move up
1008 = FALSE - move down
1011 static void
1012 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1014 long p;
1015 long l = direction ? edit->buffer.curs_line : edit->buffer.lines - edit->buffer.curs_line;
1017 if (lines > l)
1018 lines = l;
1020 if (lines == 0)
1021 return;
1023 if (lines > 1)
1024 edit->force |= REDRAW_PAGE;
1025 if (do_scroll)
1027 if (direction)
1028 edit_scroll_upward (edit, lines);
1029 else
1030 edit_scroll_downward (edit, lines);
1032 p = edit_buffer_get_current_bol (&edit->buffer);
1033 p = direction ? edit_buffer_move_backward (&edit->buffer, p, lines) :
1034 edit_buffer_move_forward (&edit->buffer, p, lines, 0);
1035 edit_cursor_move (edit, p - edit->buffer.curs1);
1036 edit_move_to_prev_col (edit, p);
1038 #ifdef HAVE_CHARSET
1039 /* search start of current multibyte char (like CJK) */
1040 if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->buffer.size
1041 && edit_buffer_get_current_byte (&edit->buffer) >= 256)
1043 edit_right_char_move_cmd (edit);
1044 edit_left_char_move_cmd (edit);
1046 #endif
1048 edit->search_start = edit->buffer.curs1;
1049 edit->found_len = 0;
1052 /* --------------------------------------------------------------------------------------------- */
1054 static void
1055 edit_right_delete_word (WEdit * edit)
1057 while (edit->buffer.curs1 < edit->buffer.size)
1059 int c1, c2;
1061 c1 = edit_delete (edit, TRUE);
1062 c2 = edit_buffer_get_current_byte (&edit->buffer);
1063 if (c1 == '\n' || c2 == '\n')
1064 break;
1065 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1066 break;
1067 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1068 break;
1072 /* --------------------------------------------------------------------------------------------- */
1074 static void
1075 edit_left_delete_word (WEdit * edit)
1077 while (edit->buffer.curs1 > 0)
1079 int c1, c2;
1081 c1 = edit_backspace (edit, TRUE);
1082 c2 = edit_buffer_get_previous_byte (&edit->buffer);
1083 if (c1 == '\n' || c2 == '\n')
1084 break;
1085 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1086 break;
1087 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1088 break;
1092 /* --------------------------------------------------------------------------------------------- */
1094 the start column position is not recorded, and hence does not
1095 undo as it happed. But who would notice.
1098 static void
1099 edit_do_undo (WEdit * edit)
1101 long ac;
1102 long count = 0;
1104 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1105 edit->over_col = 0;
1106 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1108 switch ((int) ac)
1110 case STACK_BOTTOM:
1111 goto done_undo;
1112 case CURS_RIGHT:
1113 edit_cursor_move (edit, 1);
1114 break;
1115 case CURS_LEFT:
1116 edit_cursor_move (edit, -1);
1117 break;
1118 case BACKSPACE:
1119 case BACKSPACE_BR:
1120 edit_backspace (edit, TRUE);
1121 break;
1122 case DELCHAR:
1123 case DELCHAR_BR:
1124 edit_delete (edit, TRUE);
1125 break;
1126 case COLUMN_ON:
1127 edit->column_highlight = 1;
1128 break;
1129 case COLUMN_OFF:
1130 edit->column_highlight = 0;
1131 break;
1133 if (ac >= 256 && ac < 512)
1134 edit_insert_ahead (edit, ac - 256);
1135 if (ac >= 0 && ac < 256)
1136 edit_insert (edit, ac);
1138 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1140 edit->mark1 = ac - MARK_1;
1141 edit->column1 =
1142 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark1),
1143 0, edit->mark1);
1145 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1147 edit->mark2 = ac - MARK_2;
1148 edit->column2 =
1149 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark2),
1150 0, edit->mark2);
1152 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1154 edit->end_mark_curs = ac - MARK_CURS;
1156 if (count++)
1157 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1160 if (edit->start_display > ac - KEY_PRESS)
1162 edit->start_line -=
1163 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1164 edit->force |= REDRAW_PAGE;
1166 else if (edit->start_display < ac - KEY_PRESS)
1168 edit->start_line +=
1169 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1170 edit->force |= REDRAW_PAGE;
1172 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1173 edit_update_curs_row (edit);
1175 done_undo:
1176 edit->undo_stack_disable = 0;
1179 /* --------------------------------------------------------------------------------------------- */
1181 static void
1182 edit_do_redo (WEdit * edit)
1184 long ac;
1185 long count = 0;
1187 if (edit->redo_stack_reset)
1188 return;
1190 edit->over_col = 0;
1191 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1193 switch ((int) ac)
1195 case STACK_BOTTOM:
1196 goto done_redo;
1197 case CURS_RIGHT:
1198 edit_cursor_move (edit, 1);
1199 break;
1200 case CURS_LEFT:
1201 edit_cursor_move (edit, -1);
1202 break;
1203 case BACKSPACE:
1204 edit_backspace (edit, TRUE);
1205 break;
1206 case DELCHAR:
1207 edit_delete (edit, TRUE);
1208 break;
1209 case COLUMN_ON:
1210 edit->column_highlight = 1;
1211 break;
1212 case COLUMN_OFF:
1213 edit->column_highlight = 0;
1214 break;
1216 if (ac >= 256 && ac < 512)
1217 edit_insert_ahead (edit, ac - 256);
1218 if (ac >= 0 && ac < 256)
1219 edit_insert (edit, ac);
1221 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1223 edit->mark1 = ac - MARK_1;
1224 edit->column1 =
1225 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark1),
1226 0, edit->mark1);
1228 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1230 edit->mark2 = ac - MARK_2;
1231 edit->column2 =
1232 (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, edit->mark2),
1233 0, edit->mark2);
1235 /* more than one pop usually means something big */
1236 if (count++)
1237 edit->force |= REDRAW_PAGE;
1240 if (edit->start_display > ac - KEY_PRESS)
1242 edit->start_line -=
1243 edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1244 edit->force |= REDRAW_PAGE;
1246 else if (edit->start_display < ac - KEY_PRESS)
1248 edit->start_line +=
1249 edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1250 edit->force |= REDRAW_PAGE;
1252 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1253 edit_update_curs_row (edit);
1255 done_redo:
1259 /* --------------------------------------------------------------------------------------------- */
1261 static void
1262 edit_group_undo (WEdit * edit)
1264 long ac = KEY_PRESS;
1265 long cur_ac = KEY_PRESS;
1266 while (ac != STACK_BOTTOM && ac == cur_ac)
1268 cur_ac = get_prev_undo_action (edit);
1269 edit_do_undo (edit);
1270 ac = get_prev_undo_action (edit);
1271 /* exit from cycle if option_group_undo is not set,
1272 * and make single UNDO operation
1274 if (!option_group_undo)
1275 ac = STACK_BOTTOM;
1279 /* --------------------------------------------------------------------------------------------- */
1281 static void
1282 edit_delete_to_line_end (WEdit * edit)
1284 while (edit_buffer_get_current_byte (&edit->buffer) != '\n' && edit->buffer.curs2 != 0)
1285 edit_delete (edit, TRUE);
1288 /* --------------------------------------------------------------------------------------------- */
1290 static void
1291 edit_delete_to_line_begin (WEdit * edit)
1293 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 != 0)
1294 edit_backspace (edit, TRUE);
1297 /* --------------------------------------------------------------------------------------------- */
1299 static gboolean
1300 is_aligned_on_a_tab (WEdit * edit)
1302 long curs_col;
1304 edit_update_curs_col (edit);
1305 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1306 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1309 /* --------------------------------------------------------------------------------------------- */
1311 static gboolean
1312 right_of_four_spaces (WEdit * edit)
1314 int i, ch = 0;
1316 for (i = 1; i <= HALF_TAB_SIZE; i++)
1317 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - i);
1319 return (ch == ' ' && is_aligned_on_a_tab (edit));
1322 /* --------------------------------------------------------------------------------------------- */
1324 static gboolean
1325 left_of_four_spaces (WEdit * edit)
1327 int i, ch = 0;
1329 for (i = 0; i < HALF_TAB_SIZE; i++)
1330 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 + i);
1332 return (ch == ' ' && is_aligned_on_a_tab (edit));
1335 /* --------------------------------------------------------------------------------------------- */
1337 static void
1338 edit_auto_indent (WEdit * edit)
1340 off_t p;
1342 p = edit->buffer.curs1;
1343 /* use the previous line as a template */
1344 p = edit_buffer_move_backward (&edit->buffer, p, 1);
1345 /* copy the leading whitespace of the line */
1346 while (TRUE)
1347 { /* no range check - the line _is_ \n-terminated */
1348 char c;
1350 c = edit_buffer_get_byte (&edit->buffer, p++);
1351 if (c != ' ' && c != '\t')
1352 break;
1353 edit_insert (edit, c);
1357 /* --------------------------------------------------------------------------------------------- */
1359 static inline void
1360 edit_double_newline (WEdit * edit)
1362 edit_insert (edit, '\n');
1363 if (edit_buffer_get_current_byte (&edit->buffer) == '\n'
1364 || edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - 2) == '\n')
1365 return;
1366 edit->force |= REDRAW_PAGE;
1367 edit_insert (edit, '\n');
1370 /* --------------------------------------------------------------------------------------------- */
1372 static void
1373 insert_spaces_tab (WEdit * edit, gboolean half)
1375 long i;
1377 edit_update_curs_col (edit);
1378 i = option_tab_spacing * space_width;
1379 if (half)
1380 i /= 2;
1381 if (i != 0)
1383 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1384 while (i > 0)
1386 edit_insert (edit, ' ');
1387 i -= space_width;
1392 /* --------------------------------------------------------------------------------------------- */
1394 static inline void
1395 edit_tab_cmd (WEdit * edit)
1397 if (option_fake_half_tabs && is_in_indent (&edit->buffer))
1399 /* insert a half tab (usually four spaces) unless there is a
1400 half tab already behind, then delete it and insert a
1401 full tab. */
1402 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1403 insert_spaces_tab (edit, TRUE);
1404 else
1406 int i;
1408 for (i = 1; i <= HALF_TAB_SIZE; i++)
1409 edit_backspace (edit, TRUE);
1410 edit_insert (edit, '\t');
1413 else if (option_fill_tabs_with_spaces)
1414 insert_spaces_tab (edit, FALSE);
1415 else
1416 edit_insert (edit, '\t');
1419 /* --------------------------------------------------------------------------------------------- */
1421 static void
1422 check_and_wrap_line (WEdit * edit)
1424 off_t curs;
1426 if (!option_typewriter_wrap)
1427 return;
1428 edit_update_curs_col (edit);
1429 if (edit->curs_col < option_word_wrap_line_length)
1430 return;
1431 curs = edit->buffer.curs1;
1432 while (TRUE)
1434 int c;
1436 curs--;
1437 c = edit_buffer_get_byte (&edit->buffer, curs);
1438 if (c == '\n' || curs <= 0)
1440 edit_insert (edit, '\n');
1441 return;
1443 if (c == ' ' || c == '\t')
1445 off_t current = edit->buffer.curs1;
1446 edit_cursor_move (edit, curs - edit->buffer.curs1 + 1);
1447 edit_insert (edit, '\n');
1448 edit_cursor_move (edit, current - edit->buffer.curs1 + 1);
1449 return;
1454 /* --------------------------------------------------------------------------------------------- */
1455 /** this find the matching bracket in either direction, and sets edit->bracket
1457 * @param edit editor object
1458 * @param in_screen seach only on the current screen
1459 * @param furthest_bracket_search count of the bytes for search
1461 * @return position of the found bracket (-1 if no match)
1464 static off_t
1465 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1467 const char *const b = "{}{[][()(", *p;
1468 int i = 1, inc = -1, c, d, n = 0;
1469 unsigned long j = 0;
1470 off_t q;
1472 edit_update_curs_row (edit);
1473 c = edit_buffer_get_current_byte (&edit->buffer);
1474 p = strchr (b, c);
1475 /* no limit */
1476 if (!furthest_bracket_search)
1477 furthest_bracket_search--;
1478 /* not on a bracket at all */
1479 if (p == NULL)
1480 return -1;
1481 /* the matching bracket */
1482 d = p[1];
1483 /* going left or right? */
1484 if (strchr ("{[(", c))
1485 inc = 1;
1486 for (q = edit->buffer.curs1 + inc;; q += inc)
1488 int a;
1490 /* out of buffer? */
1491 if (q >= edit->buffer.size || q < 0)
1492 break;
1493 a = edit_buffer_get_byte (&edit->buffer, q);
1494 /* don't want to eat CPU */
1495 if (j++ > furthest_bracket_search)
1496 break;
1497 /* out of screen? */
1498 if (in_screen)
1500 if (q < edit->start_display)
1501 break;
1502 /* count lines if searching downward */
1503 if (inc > 0 && a == '\n')
1504 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1505 break;
1507 /* count bracket depth */
1508 i += (a == c) - (a == d);
1509 /* return if bracket depth is zero */
1510 if (i == 0)
1511 return q;
1513 /* no match */
1514 return -1;
1517 /* --------------------------------------------------------------------------------------------- */
1519 static inline void
1520 edit_goto_matching_bracket (WEdit * edit)
1522 off_t q;
1524 q = edit_get_bracket (edit, 0, 0);
1525 if (q >= 0)
1527 edit->bracket = edit->buffer.curs1;
1528 edit->force |= REDRAW_PAGE;
1529 edit_cursor_move (edit, q - edit->buffer.curs1);
1533 /* --------------------------------------------------------------------------------------------- */
1535 static void
1536 edit_move_block_to_right (WEdit * edit)
1538 off_t start_mark, end_mark;
1539 long cur_bol, start_bol;
1541 if (!eval_marks (edit, &start_mark, &end_mark))
1542 return;
1544 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1545 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1549 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1550 if (!edit_line_is_blank (edit, edit->buffer.curs_line))
1552 if (option_fill_tabs_with_spaces)
1553 insert_spaces_tab (edit, option_fake_half_tabs);
1554 else
1555 edit_insert (edit, '\t');
1556 edit_cursor_move (edit,
1557 edit_buffer_get_bol (&edit->buffer, cur_bol) - edit->buffer.curs1);
1560 if (cur_bol == 0)
1561 break;
1563 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1565 while (cur_bol >= start_bol);
1567 edit->force |= REDRAW_PAGE;
1570 /* --------------------------------------------------------------------------------------------- */
1572 static void
1573 edit_move_block_to_left (WEdit * edit)
1575 off_t start_mark, end_mark;
1576 off_t cur_bol, start_bol;
1577 int i;
1579 if (!eval_marks (edit, &start_mark, &end_mark))
1580 return;
1582 start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1583 cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1587 int del_tab_width;
1588 int next_char;
1590 edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1592 if (option_fake_half_tabs)
1593 del_tab_width = HALF_TAB_SIZE;
1594 else
1595 del_tab_width = option_tab_spacing;
1597 next_char = edit_buffer_get_current_byte (&edit->buffer);
1598 if (next_char == '\t')
1599 edit_delete (edit, TRUE);
1600 else if (next_char == ' ')
1601 for (i = 1; i <= del_tab_width; i++)
1603 if (next_char == ' ')
1604 edit_delete (edit, TRUE);
1605 next_char = edit_buffer_get_current_byte (&edit->buffer);
1608 if (cur_bol == 0)
1609 break;
1611 cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1613 while (cur_bol >= start_bol);
1615 edit->force |= REDRAW_PAGE;
1618 /* --------------------------------------------------------------------------------------------- */
1620 * prints at the cursor
1621 * @return number of chars printed
1624 static size_t
1625 edit_print_string (WEdit * e, const char *s)
1627 size_t i = 0;
1629 while (s[i] != '\0')
1630 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1631 e->force |= REDRAW_COMPLETELY;
1632 edit_update_screen (e);
1633 return i;
1636 /* --------------------------------------------------------------------------------------------- */
1638 static off_t
1639 edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * end_pos,
1640 long *col1, long *col2)
1642 off_t cursor;
1643 int col;
1644 off_t blocklen = -1, width = 0;
1645 unsigned char *data;
1647 cursor = edit->buffer.curs1;
1648 col = edit_get_col (edit);
1649 data = g_malloc0 (TEMP_BUF_LEN);
1651 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1653 off_t i;
1654 char *pn;
1656 pn = strchr ((char *) data, '\n');
1657 width = pn == NULL ? blocklen : pn - (char *) data;
1659 for (i = 0; i < blocklen; i++)
1661 if (data[i] != '\n')
1662 edit_insert (edit, data[i]);
1663 else
1664 { /* fill in and move to next line */
1665 long l;
1666 off_t p;
1668 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1669 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1670 edit_insert (edit, ' ');
1672 for (p = edit->buffer.curs1;; p++)
1674 if (p == edit->buffer.size)
1676 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1677 edit_insert_ahead (edit, '\n');
1678 p++;
1679 break;
1681 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1683 p++;
1684 break;
1688 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1690 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1691 edit_insert (edit, ' ');
1695 *col1 = col;
1696 *col2 = col + width;
1697 *start_pos = cursor;
1698 *end_pos = edit->buffer.curs1;
1699 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1700 g_free (data);
1702 return blocklen;
1705 /* --------------------------------------------------------------------------------------------- */
1706 /*** public functions ****************************************************************************/
1707 /* --------------------------------------------------------------------------------------------- */
1709 /** User edit menu, like user menu (F2) but only in editor. */
1711 void
1712 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1714 char *block_file;
1715 gboolean nomark;
1716 off_t curs;
1717 off_t start_mark, end_mark;
1718 struct stat status;
1719 vfs_path_t *block_file_vpath;
1721 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1722 block_file_vpath = vfs_path_from_str (block_file);
1723 curs = edit->buffer.curs1;
1724 nomark = !eval_marks (edit, &start_mark, &end_mark);
1725 if (!nomark)
1726 edit_save_block (edit, block_file, start_mark, end_mark);
1728 /* run shell scripts from menu */
1729 if (user_menu_cmd (edit, menu_file, selected_entry)
1730 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1732 int rc = 0;
1733 FILE *fd;
1735 /* i.e. we have marked block */
1736 if (!nomark)
1737 rc = edit_block_delete_cmd (edit);
1739 if (rc == 0)
1741 off_t ins_len;
1743 ins_len = edit_insert_file (edit, block_file_vpath);
1744 if (!nomark && ins_len > 0)
1745 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1747 /* truncate block file */
1748 fd = fopen (block_file, "w");
1749 if (fd != NULL)
1750 fclose (fd);
1752 g_free (block_file);
1753 vfs_path_free (block_file_vpath);
1755 edit_cursor_move (edit, curs - edit->buffer.curs1);
1756 edit->force |= REDRAW_PAGE;
1757 widget_redraw (WIDGET (edit));
1760 /* --------------------------------------------------------------------------------------------- */
1762 char *
1763 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1765 int i;
1766 char *p, *writename;
1767 const vfs_path_element_t *path_element;
1769 i = edit_find_filter (filename_vpath);
1770 if (i < 0)
1771 return NULL;
1773 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1774 writename = name_quote (path_element->path, 0);
1775 p = g_strdup_printf (all_filters[i].write, writename);
1776 g_free (writename);
1777 return p;
1780 /* --------------------------------------------------------------------------------------------- */
1782 * @param edit editor object
1783 * @param f value of stream file
1784 * @return the length of the file
1787 off_t
1788 edit_write_stream (WEdit * edit, FILE * f)
1790 long i;
1792 if (edit->lb == LB_ASIS)
1794 for (i = 0; i < edit->buffer.size; i++)
1795 if (fputc (edit_buffer_get_byte (&edit->buffer, i), f) < 0)
1796 break;
1797 return i;
1800 /* change line breaks */
1801 for (i = 0; i < edit->buffer.size; i++)
1803 unsigned char c;
1805 c = edit_buffer_get_byte (&edit->buffer, i);
1806 if (!(c == '\n' || c == '\r'))
1808 /* not line break */
1809 if (fputc (c, f) < 0)
1810 return i;
1812 else
1813 { /* (c == '\n' || c == '\r') */
1814 unsigned char c1;
1816 c1 = edit_buffer_get_byte (&edit->buffer, i + 1); /* next char */
1818 switch (edit->lb)
1820 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1821 /* put one line break unconditionally */
1822 if (fputc ('\n', f) < 0)
1823 return i;
1825 i++; /* 2 chars are processed */
1827 if (c == '\r' && c1 == '\n')
1828 /* Windows line break; go to the next char */
1829 break;
1831 if (c == '\r' && c1 == '\r')
1833 /* two Macintosh line breaks; put second line break */
1834 if (fputc ('\n', f) < 0)
1835 return i;
1836 break;
1839 if (fputc (c1, f) < 0)
1840 return i;
1841 break;
1843 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1844 /* put one line break unconditionally */
1845 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1846 return i;
1848 if (c == '\r' && c1 == '\n')
1849 /* Windows line break; go to the next char */
1850 i++;
1851 break;
1853 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1854 /* put one line break unconditionally */
1855 if (fputc ('\r', f) < 0)
1856 return i;
1858 i++; /* 2 chars are processed */
1860 if (c == '\r' && c1 == '\n')
1861 /* Windows line break; go to the next char */
1862 break;
1864 if (c == '\n' && c1 == '\n')
1866 /* two Windows line breaks; put second line break */
1867 if (fputc ('\r', f) < 0)
1868 return i;
1869 break;
1872 if (fputc (c1, f) < 0)
1873 return i;
1874 break;
1875 case LB_ASIS: /* default without changes */
1876 break;
1881 return edit->buffer.size;
1884 /* --------------------------------------------------------------------------------------------- */
1886 gboolean
1887 is_break_char (char c)
1889 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1892 /* --------------------------------------------------------------------------------------------- */
1893 /** inserts a file at the cursor, returns count of inserted bytes on success */
1895 off_t
1896 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
1898 char *p;
1899 off_t current;
1900 off_t ins_len = 0;
1902 p = edit_get_filter (filename_vpath);
1903 current = edit->buffer.curs1;
1905 if (p != NULL)
1907 FILE *f;
1909 f = (FILE *) popen (p, "r");
1910 if (f != NULL)
1912 edit_insert_stream (edit, f);
1914 /* Place cursor at the end of text selection */
1915 if (!option_cursor_after_inserted_block)
1917 ins_len = edit->buffer.curs1 - current;
1918 edit_cursor_move (edit, -ins_len);
1920 if (pclose (f) > 0)
1922 char *errmsg;
1924 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
1925 edit_error_dialog (_("Error"), errmsg);
1926 g_free (errmsg);
1927 ins_len = -1;
1930 else
1932 char *errmsg;
1934 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
1935 edit_error_dialog (_("Error"), errmsg);
1936 g_free (errmsg);
1937 ins_len = -1;
1939 g_free (p);
1941 else
1943 int file;
1944 off_t blocklen;
1945 int vertical_insertion = 0;
1946 char *buf;
1948 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
1949 if (file == -1)
1950 return -1;
1952 buf = g_malloc0 (TEMP_BUF_LEN);
1953 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
1954 if (blocklen > 0)
1956 /* if contain signature VERTICAL_MAGIC then it vertical block */
1957 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
1958 vertical_insertion = 1;
1959 else
1960 mc_lseek (file, 0, SEEK_SET);
1963 if (vertical_insertion)
1965 off_t mark1, mark2;
1966 long c1, c2;
1968 blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
1969 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
1971 /* highlight inserted text then not persistent blocks */
1972 if (!option_persistent_selections && edit->modified)
1974 if (!edit->column_highlight)
1975 edit_push_undo_action (edit, COLUMN_OFF);
1976 edit->column_highlight = 1;
1979 else
1981 off_t i;
1983 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
1985 for (i = 0; i < blocklen; i++)
1986 edit_insert (edit, buf[i]);
1988 /* highlight inserted text then not persistent blocks */
1989 if (!option_persistent_selections && edit->modified)
1991 edit_set_markers (edit, edit->buffer.curs1, current, 0, 0);
1992 if (edit->column_highlight)
1993 edit_push_undo_action (edit, COLUMN_ON);
1994 edit->column_highlight = 0;
1997 /* Place cursor at the end of text selection */
1998 if (!option_cursor_after_inserted_block)
2000 ins_len = edit->buffer.curs1 - current;
2001 edit_cursor_move (edit, -ins_len);
2005 edit->force |= REDRAW_PAGE;
2006 g_free (buf);
2007 mc_close (file);
2008 if (blocklen != 0)
2009 ins_len = 0;
2012 return ins_len;
2015 /* --------------------------------------------------------------------------------------------- */
2017 * Fill in the edit structure. Return NULL on failure. Pass edit as
2018 * NULL to allocate a new structure.
2020 * If line is 0, try to restore saved position. Otherwise put the
2021 * cursor on that line and show it in the middle of the screen.
2024 WEdit *
2025 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2026 long line)
2028 gboolean to_free = FALSE;
2030 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2031 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2033 if (edit != NULL)
2035 /* save some widget parameters */
2036 gboolean fullscreen = edit->fullscreen;
2037 int y_prev = edit->y_prev;
2038 int x_prev = edit->x_prev;
2039 int lines_prev = edit->lines_prev;
2040 int cols_prev = edit->cols_prev;
2042 edit_purge_widget (edit);
2044 /* restore saved parameters */
2045 edit->fullscreen = fullscreen;
2046 edit->y_prev = y_prev;
2047 edit->x_prev = x_prev;
2048 edit->lines_prev = lines_prev;
2049 edit->cols_prev = cols_prev;
2051 else
2053 edit = g_malloc0 (sizeof (WEdit));
2054 to_free = TRUE;
2056 widget_init (WIDGET (edit), y, x, lines, cols, NULL, NULL);
2057 edit->fullscreen = TRUE;
2058 edit_save_size (edit);
2061 edit->drag_state = MCEDIT_DRAG_NORMAL;
2063 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2064 edit->stat1.st_uid = getuid ();
2065 edit->stat1.st_gid = getgid ();
2066 edit->stat1.st_mtime = 0;
2068 edit->over_col = 0;
2069 edit->bracket = -1;
2070 edit->last_bracket = -1;
2071 edit->force |= REDRAW_PAGE;
2073 /* set file name before load file */
2074 edit_set_filename (edit, filename_vpath);
2076 edit->undo_stack_size = START_STACK_SIZE;
2077 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2078 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2080 edit->redo_stack_size = START_STACK_SIZE;
2081 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2082 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2084 #ifdef HAVE_CHARSET
2085 edit->utf8 = FALSE;
2086 edit->converter = str_cnv_from_term;
2087 edit_set_codeset (edit);
2088 #endif
2090 if (!edit_load_file (edit))
2092 /* edit_load_file already gives an error message */
2093 if (to_free)
2094 g_free (edit);
2095 return NULL;
2098 edit->loading_done = 1;
2099 edit->modified = 0;
2100 edit->locked = 0;
2101 edit_load_syntax (edit, NULL, NULL);
2102 edit_get_syntax_color (edit, -1);
2104 /* load saved cursor position */
2105 if ((line == 0) && option_save_position)
2106 edit_load_position (edit);
2107 else
2109 if (line <= 0)
2110 line = 1;
2111 edit_move_display (edit, line - 1);
2112 edit_move_to_line (edit, line - 1);
2115 edit_load_macro_cmd (edit);
2117 return edit;
2120 /* --------------------------------------------------------------------------------------------- */
2122 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2123 gboolean
2124 edit_clean (WEdit * edit)
2126 if (edit == NULL)
2127 return FALSE;
2129 /* a stale lock, remove it */
2130 if (edit->locked)
2131 edit->locked = unlock_file (edit->filename_vpath);
2133 /* save cursor position */
2134 if (option_save_position)
2135 edit_save_position (edit);
2136 else if (edit->serialized_bookmarks != NULL)
2137 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2139 /* File specified on the mcedit command line and never saved */
2140 if (edit->delete_file)
2141 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2143 edit_free_syntax_rules (edit);
2144 book_mark_flush (edit, -1);
2146 edit_buffer_clean (&edit->buffer);
2148 g_free (edit->undo_stack);
2149 g_free (edit->redo_stack);
2150 vfs_path_free (edit->filename_vpath);
2151 vfs_path_free (edit->dir_vpath);
2152 mc_search_free (edit->search);
2153 edit->search = NULL;
2155 #ifdef HAVE_CHARSET
2156 if (edit->converter != str_cnv_from_term)
2157 str_close_conv (edit->converter);
2158 #endif
2160 edit_purge_widget (edit);
2162 return TRUE;
2165 /* --------------------------------------------------------------------------------------------- */
2168 * Load a new file into the editor and set line. If it fails, preserve the old file.
2169 * To do it, allocate a new widget, initialize it and, if the new file
2170 * was loaded, copy the data to the old widget.
2172 * @return TRUE on success, FALSE on failure.
2174 gboolean
2175 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2177 Widget *w = WIDGET (edit);
2178 WEdit *e;
2180 e = g_malloc0 (sizeof (WEdit));
2181 *WIDGET (e) = *w;
2182 /* save some widget parameters */
2183 e->fullscreen = edit->fullscreen;
2184 e->y_prev = edit->y_prev;
2185 e->x_prev = edit->x_prev;
2186 e->lines_prev = edit->lines_prev;
2187 e->cols_prev = edit->cols_prev;
2189 if (edit_init (e, w->y, w->x, w->lines, w->cols, filename_vpath, line) == NULL)
2191 g_free (e);
2192 return FALSE;
2195 edit_clean (edit);
2196 memcpy (edit, e, sizeof (WEdit));
2197 g_free (e);
2199 return TRUE;
2202 /* --------------------------------------------------------------------------------------------- */
2204 #ifdef HAVE_CHARSET
2205 void
2206 edit_set_codeset (WEdit * edit)
2208 const char *cp_id;
2210 cp_id =
2211 get_codepage_id (mc_global.source_codepage >=
2212 0 ? mc_global.source_codepage : mc_global.display_codepage);
2214 if (cp_id != NULL)
2216 GIConv conv;
2217 conv = str_crt_conv_from (cp_id);
2218 if (conv != INVALID_CONV)
2220 if (edit->converter != str_cnv_from_term)
2221 str_close_conv (edit->converter);
2222 edit->converter = conv;
2226 if (cp_id != NULL)
2227 edit->utf8 = str_isutf8 (cp_id);
2229 #endif
2231 /* --------------------------------------------------------------------------------------------- */
2234 * Recording stack for undo:
2235 * The following is an implementation of a compressed stack. Identical
2236 * pushes are recorded by a negative prefix indicating the number of times the
2237 * same char was pushed. This saves space for repeated curs-left or curs-right
2238 * delete etc.
2240 * eg:
2242 * pushed: stored:
2245 * b a
2246 * b -3
2247 * b b
2248 * c --> -4
2249 * c c
2250 * c d
2254 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2255 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2256 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2257 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2258 * position.
2260 * The only way the cursor moves or the buffer is changed is through the routines:
2261 * insert, backspace, insert_ahead, delete, and cursor_move.
2262 * These record the reverse undo movements onto the stack each time they are
2263 * called.
2265 * Each key press results in a set of actions (insert; delete ...). So each time
2266 * a key is pressed the current position of start_display is pushed as
2267 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2268 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2269 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2273 * @param edit editor object
2274 * @param c code of the action
2277 void
2278 edit_push_undo_action (WEdit * edit, long c)
2280 unsigned long sp = edit->undo_stack_pointer;
2281 unsigned long spm1;
2282 long *t;
2284 /* first enlarge the stack if necessary */
2285 if (sp > edit->undo_stack_size - 10)
2286 { /* say */
2287 if (option_max_undo < 256)
2288 option_max_undo = 256;
2289 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2291 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2292 if (t)
2294 edit->undo_stack = t;
2295 edit->undo_stack_size <<= 1;
2296 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2300 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2301 if (edit->undo_stack_disable)
2303 edit_push_redo_action (edit, KEY_PRESS);
2304 edit_push_redo_action (edit, c);
2305 return;
2308 if (edit->redo_stack_reset)
2309 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2311 if (edit->undo_stack_bottom != sp
2312 && spm1 != edit->undo_stack_bottom
2313 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2315 int d;
2316 if (edit->undo_stack[spm1] < 0)
2318 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2319 if (d == c && edit->undo_stack[spm1] > -1000000000)
2321 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2322 edit->undo_stack[spm1]--;
2323 return;
2326 else
2328 d = edit->undo_stack[spm1];
2329 if (d == c)
2331 if (c >= KEY_PRESS)
2332 return; /* --> no need to push multiple do-nothings */
2333 edit->undo_stack[sp] = -2;
2334 goto check_bottom;
2338 edit->undo_stack[sp] = c;
2340 check_bottom:
2341 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2343 /* if the sp wraps round and catches the undo_stack_bottom then erase
2344 * the first set of actions on the stack to make space - by moving
2345 * undo_stack_bottom forward one "key press" */
2346 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2347 if ((unsigned long) c == edit->undo_stack_bottom ||
2348 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2351 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2353 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2354 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2356 /*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: */
2357 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2358 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2360 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2364 /* --------------------------------------------------------------------------------------------- */
2366 void
2367 edit_push_redo_action (WEdit * edit, long c)
2369 unsigned long sp = edit->redo_stack_pointer;
2370 unsigned long spm1;
2371 long *t;
2372 /* first enlarge the stack if necessary */
2373 if (sp > edit->redo_stack_size - 10)
2374 { /* say */
2375 if (option_max_undo < 256)
2376 option_max_undo = 256;
2377 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2379 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2380 if (t)
2382 edit->redo_stack = t;
2383 edit->redo_stack_size <<= 1;
2384 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2388 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2390 if (edit->redo_stack_bottom != sp
2391 && spm1 != edit->redo_stack_bottom
2392 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2394 int d;
2395 if (edit->redo_stack[spm1] < 0)
2397 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2398 if (d == c && edit->redo_stack[spm1] > -1000000000)
2400 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2401 edit->redo_stack[spm1]--;
2402 return;
2405 else
2407 d = edit->redo_stack[spm1];
2408 if (d == c)
2410 if (c >= KEY_PRESS)
2411 return; /* --> no need to push multiple do-nothings */
2412 edit->redo_stack[sp] = -2;
2413 goto redo_check_bottom;
2417 edit->redo_stack[sp] = c;
2419 redo_check_bottom:
2420 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2422 /* if the sp wraps round and catches the redo_stack_bottom then erase
2423 * the first set of actions on the stack to make space - by moving
2424 * redo_stack_bottom forward one "key press" */
2425 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2426 if ((unsigned long) c == edit->redo_stack_bottom ||
2427 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2430 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2432 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2433 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2436 * If a single key produced enough pushes to wrap all the way round then
2437 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2438 * The stack is then initialised:
2441 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2442 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2443 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2446 /* --------------------------------------------------------------------------------------------- */
2448 Basic low level single character buffer alterations and movements at the cursor.
2451 void
2452 edit_insert (WEdit * edit, int c)
2454 /* first we must update the position of the display window */
2455 if (edit->buffer.curs1 < edit->start_display)
2457 edit->start_display++;
2458 if (c == '\n')
2459 edit->start_line++;
2462 /* Mark file as modified, unless the file hasn't been fully loaded */
2463 if (edit->loading_done)
2464 edit_modification (edit);
2466 /* now we must update some info on the file and check if a redraw is required */
2467 if (c == '\n')
2469 book_mark_inc (edit, edit->buffer.curs_line);
2470 edit->buffer.curs_line++;
2471 edit->buffer.lines++;
2472 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2475 /* save the reverse command onto the undo stack */
2476 /* ordinary char and not space */
2477 if (c > 32)
2478 edit_push_undo_action (edit, BACKSPACE);
2479 else
2480 edit_push_undo_action (edit, BACKSPACE_BR);
2481 /* update markers */
2482 edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2483 edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2484 edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2486 edit_buffer_insert (&edit->buffer, c);
2488 /* update file length */
2489 edit->buffer.size++;
2492 /* --------------------------------------------------------------------------------------------- */
2493 /** same as edit_insert and move left */
2495 void
2496 edit_insert_ahead (WEdit * edit, int c)
2498 if (edit->buffer.curs1 < edit->start_display)
2500 edit->start_display++;
2501 if (c == '\n')
2502 edit->start_line++;
2504 edit_modification (edit);
2505 if (c == '\n')
2507 book_mark_inc (edit, edit->buffer.curs_line);
2508 edit->buffer.lines++;
2509 edit->force |= REDRAW_AFTER_CURSOR;
2511 /* ordinary char and not space */
2512 if (c > 32)
2513 edit_push_undo_action (edit, DELCHAR);
2514 else
2515 edit_push_undo_action (edit, DELCHAR_BR);
2517 edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2518 edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2519 edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2521 edit_buffer_insert_ahead (&edit->buffer, c);
2523 edit->buffer.size++;
2526 /* --------------------------------------------------------------------------------------------- */
2528 void
2529 edit_insert_over (WEdit * edit)
2531 long i;
2533 for (i = 0; i < edit->over_col; i++)
2534 edit_insert (edit, ' ');
2536 edit->over_col = 0;
2539 /* --------------------------------------------------------------------------------------------- */
2542 edit_delete (WEdit * edit, gboolean byte_delete)
2544 int p = 0;
2545 int cw = 1;
2546 int i;
2548 if (edit->buffer.curs2 == 0)
2549 return 0;
2551 #ifdef HAVE_CHARSET
2552 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2553 if (edit->utf8 && !byte_delete)
2555 edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &cw);
2556 if (cw < 1)
2557 cw = 1;
2559 #else
2560 (void) byte_delete;
2561 #endif
2563 if (edit->mark2 != edit->mark1)
2564 edit_push_markers (edit);
2566 for (i = 1; i <= cw; i++)
2568 if (edit->mark1 > edit->buffer.curs1)
2570 edit->mark1--;
2571 edit->end_mark_curs--;
2573 if (edit->mark2 > edit->buffer.curs1)
2574 edit->mark2--;
2575 if (edit->last_get_rule > edit->buffer.curs1)
2576 edit->last_get_rule--;
2578 p = edit_buffer_delete (&edit->buffer);
2580 edit->buffer.size--;
2581 edit_push_undo_action (edit, p + 256);
2584 edit_modification (edit);
2585 if (p == '\n')
2587 book_mark_dec (edit, edit->buffer.curs_line);
2588 edit->buffer.lines--;
2589 edit->force |= REDRAW_AFTER_CURSOR;
2591 if (edit->buffer.curs1 < edit->start_display)
2593 edit->start_display--;
2594 if (p == '\n')
2595 edit->start_line--;
2598 return p;
2601 /* --------------------------------------------------------------------------------------------- */
2604 edit_backspace (WEdit * edit, gboolean byte_delete)
2606 int p = 0;
2607 int cw = 1;
2608 int i;
2610 if (edit->buffer.curs1 == 0)
2611 return 0;
2613 if (edit->mark2 != edit->mark1)
2614 edit_push_markers (edit);
2616 #ifdef HAVE_CHARSET
2617 if (edit->utf8 && !byte_delete)
2619 edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &cw);
2620 if (cw < 1)
2621 cw = 1;
2623 #else
2624 (void) byte_delete;
2625 #endif
2627 for (i = 1; i <= cw; i++)
2629 if (edit->mark1 >= edit->buffer.curs1)
2631 edit->mark1--;
2632 edit->end_mark_curs--;
2634 if (edit->mark2 >= edit->buffer.curs1)
2635 edit->mark2--;
2636 if (edit->last_get_rule >= edit->buffer.curs1)
2637 edit->last_get_rule--;
2639 p = edit_buffer_backspace (&edit->buffer);
2641 edit->buffer.size--;
2642 edit_push_undo_action (edit, p);
2644 edit_modification (edit);
2645 if (p == '\n')
2647 book_mark_dec (edit, edit->buffer.curs_line);
2648 edit->buffer.curs_line--;
2649 edit->buffer.lines--;
2650 edit->force |= REDRAW_AFTER_CURSOR;
2653 if (edit->buffer.curs1 < edit->start_display)
2655 edit->start_display--;
2656 if (p == '\n')
2657 edit->start_line--;
2660 return p;
2663 /* --------------------------------------------------------------------------------------------- */
2664 /** moves the cursor right or left: increment positive or negative respectively */
2666 void
2667 edit_cursor_move (WEdit * edit, off_t increment)
2669 if (increment < 0)
2671 for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2673 int c;
2675 edit_push_undo_action (edit, CURS_RIGHT);
2677 c = edit_buffer_get_previous_byte (&edit->buffer);
2678 edit_buffer_insert_ahead (&edit->buffer, c);
2679 c = edit_buffer_backspace (&edit->buffer);
2680 if (c == '\n')
2682 edit->buffer.curs_line--;
2683 edit->force |= REDRAW_LINE_BELOW;
2687 else
2689 for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2691 int c;
2693 edit_push_undo_action (edit, CURS_LEFT);
2695 c = edit_buffer_get_current_byte (&edit->buffer);
2696 edit_buffer_insert (&edit->buffer, c);
2697 c = edit_buffer_delete (&edit->buffer);
2698 if (c == '\n')
2700 edit->buffer.curs_line++;
2701 edit->force |= REDRAW_LINE_ABOVE;
2707 /* --------------------------------------------------------------------------------------------- */
2708 /* If cols is zero this returns the count of columns from current to upto. */
2709 /* If upto is zero returns index of cols across from current. */
2711 off_t
2712 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
2714 off_t p, q;
2715 long col;
2717 if (upto != 0)
2719 q = upto;
2720 cols = -10;
2722 else
2723 q = edit->buffer.size + 2;
2725 for (col = 0, p = current; p < q; p++)
2727 int c, orig_c;
2729 if (cols != -10)
2731 if (col == cols)
2732 return p;
2733 if (col > cols)
2734 return p - 1;
2737 orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2739 #ifdef HAVE_CHARSET
2740 if (edit->utf8)
2742 int utf_ch;
2743 int cw = 1;
2745 utf_ch = edit_buffer_get_utf (&edit->buffer, p, &cw);
2746 if (mc_global.utf8_display)
2748 if (cw > 1)
2749 col -= cw - 1;
2750 if (g_unichar_iswide (utf_ch))
2751 col++;
2753 else if (cw > 1 && g_unichar_isprint (utf_ch))
2754 col -= cw - 1;
2757 c = convert_to_display_c (c);
2758 #endif
2760 if (c == '\n')
2761 return (upto != 0 ? (off_t) col : p);
2762 if (c == '\t')
2763 col += TAB_SIZE - col % TAB_SIZE;
2764 else if ((c < 32 || c == 127) && (orig_c == c
2765 #ifdef HAVE_CHARSET
2766 || (!mc_global.utf8_display && !edit->utf8)
2767 #endif
2769 /* '\r' is shown as ^M, so we must advance 2 characters */
2770 /* Caret notation for control characters */
2771 col += 2;
2772 else
2773 col++;
2775 return (off_t) col;
2778 /* --------------------------------------------------------------------------------------------- */
2779 /** returns the current column position of the cursor */
2781 long
2782 edit_get_col (const WEdit * edit)
2784 return (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2785 edit->buffer.curs1);
2788 /* --------------------------------------------------------------------------------------------- */
2789 /* Scrolling functions */
2790 /* --------------------------------------------------------------------------------------------- */
2792 void
2793 edit_update_curs_row (WEdit * edit)
2795 edit->curs_row = edit->buffer.curs_line - edit->start_line;
2798 /* --------------------------------------------------------------------------------------------- */
2800 void
2801 edit_update_curs_col (WEdit * edit)
2803 edit->curs_col = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer),
2804 0, edit->buffer.curs1);
2807 /* --------------------------------------------------------------------------------------------- */
2809 long
2810 edit_get_curs_col (const WEdit * edit)
2812 return edit->curs_col;
2815 /* --------------------------------------------------------------------------------------------- */
2816 /** moves the display start position up by i lines */
2818 void
2819 edit_scroll_upward (WEdit * edit, long i)
2821 long lines_above = edit->start_line;
2823 if (i > lines_above)
2824 i = lines_above;
2825 if (i != 0)
2827 edit->start_line -= i;
2828 edit->start_display = edit_buffer_move_backward (&edit->buffer, edit->start_display, i);
2829 edit->force |= REDRAW_PAGE;
2830 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2832 edit_update_curs_row (edit);
2836 /* --------------------------------------------------------------------------------------------- */
2838 void
2839 edit_scroll_downward (WEdit * edit, long i)
2841 long lines_below;
2843 lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->lines - 1);
2844 if (lines_below > 0)
2846 if (i > lines_below)
2847 i = lines_below;
2848 edit->start_line += i;
2849 edit->start_display = edit_buffer_move_forward (&edit->buffer, edit->start_display, i, 0);
2850 edit->force |= REDRAW_PAGE;
2851 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2853 edit_update_curs_row (edit);
2856 /* --------------------------------------------------------------------------------------------- */
2858 void
2859 edit_scroll_right (WEdit * edit, long i)
2861 edit->force |= REDRAW_PAGE;
2862 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2863 edit->start_col -= i;
2866 /* --------------------------------------------------------------------------------------------- */
2868 void
2869 edit_scroll_left (WEdit * edit, long i)
2871 if (edit->start_col)
2873 edit->start_col += i;
2874 if (edit->start_col > 0)
2875 edit->start_col = 0;
2876 edit->force |= REDRAW_PAGE;
2877 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2881 /* --------------------------------------------------------------------------------------------- */
2882 /* high level cursor movement commands */
2883 /* --------------------------------------------------------------------------------------------- */
2885 void
2886 edit_move_to_prev_col (WEdit * edit, off_t p)
2888 long prev = edit->prev_col;
2889 long over = edit->over_col;
2891 edit_cursor_move (edit,
2892 edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
2894 if (option_cursor_beyond_eol)
2896 long line_len;
2898 line_len = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2899 edit_buffer_get_current_eol (&edit->buffer));
2900 if (line_len < prev + edit->over_col)
2902 edit->over_col = prev + over - line_len;
2903 edit->prev_col = line_len;
2904 edit->curs_col = line_len;
2906 else
2908 edit->curs_col = prev + over;
2909 edit->prev_col = edit->curs_col;
2910 edit->over_col = 0;
2913 else
2915 edit->over_col = 0;
2916 if (option_fake_half_tabs && is_in_indent (&edit->buffer))
2918 long fake_half_tabs;
2920 edit_update_curs_col (edit);
2922 fake_half_tabs = HALF_TAB_SIZE * space_width;
2923 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
2925 int q;
2927 q = edit->curs_col;
2928 edit->curs_col -= (edit->curs_col % fake_half_tabs);
2929 p = edit_buffer_get_current_bol (&edit->buffer);
2930 edit_cursor_move (edit,
2931 edit_move_forward3 (edit, p, edit->curs_col,
2932 0) - edit->buffer.curs1);
2933 if (!left_of_four_spaces (edit))
2934 edit_cursor_move (edit,
2935 edit_move_forward3 (edit, p, q, 0) - edit->buffer.curs1);
2941 /* --------------------------------------------------------------------------------------------- */
2942 /** check whether line in editor is blank or not
2944 * @param edit editor object
2945 * @param line number of line
2947 * @return TRUE if line in blank, FALSE otherwise
2950 gboolean
2951 edit_line_is_blank (WEdit * edit, long line)
2953 return is_blank (&edit->buffer, edit_find_line (edit, line));
2956 /* --------------------------------------------------------------------------------------------- */
2957 /** move cursor to line 'line' */
2959 void
2960 edit_move_to_line (WEdit * e, long line)
2962 if (line < e->buffer.curs_line)
2963 edit_move_up (e, e->buffer.curs_line - line, 0);
2964 else
2965 edit_move_down (e, line - e->buffer.curs_line, 0);
2966 edit_scroll_screen_over_cursor (e);
2969 /* --------------------------------------------------------------------------------------------- */
2970 /** scroll window so that first visible line is 'line' */
2972 void
2973 edit_move_display (WEdit * e, long line)
2975 if (line < e->start_line)
2976 edit_scroll_upward (e, e->start_line - line);
2977 else
2978 edit_scroll_downward (e, line - e->start_line);
2981 /* --------------------------------------------------------------------------------------------- */
2982 /** save markers onto undo stack */
2984 void
2985 edit_push_markers (WEdit * edit)
2987 edit_push_undo_action (edit, MARK_1 + edit->mark1);
2988 edit_push_undo_action (edit, MARK_2 + edit->mark2);
2989 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
2992 /* --------------------------------------------------------------------------------------------- */
2994 void
2995 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
2997 edit->mark1 = m1;
2998 edit->mark2 = m2;
2999 edit->column1 = c1;
3000 edit->column2 = c2;
3004 /* --------------------------------------------------------------------------------------------- */
3005 /** highlight marker toggle */
3007 void
3008 edit_mark_cmd (WEdit * edit, gboolean unmark)
3010 edit_push_markers (edit);
3011 if (unmark)
3013 edit_set_markers (edit, 0, 0, 0, 0);
3014 edit->force |= REDRAW_PAGE;
3016 else if (edit->mark2 >= 0)
3018 edit->end_mark_curs = -1;
3019 edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3020 edit->curs_col + edit->over_col);
3021 edit->force |= REDRAW_PAGE;
3023 else
3025 edit->end_mark_curs = edit->buffer.curs1;
3026 edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3027 edit->curs_col + edit->over_col);
3031 /* --------------------------------------------------------------------------------------------- */
3032 /** highlight the word under cursor */
3034 void
3035 edit_mark_current_word_cmd (WEdit * edit)
3037 long pos;
3039 for (pos = edit->buffer.curs1; pos != 0; pos--)
3041 int c1, c2;
3043 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3044 c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3045 if (!isspace (c1) && isspace (c2))
3046 break;
3047 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3048 break;
3050 edit->mark1 = pos;
3052 for (; pos < edit->buffer.size; pos++)
3054 int c1, c2;
3056 c1 = edit_buffer_get_byte (&edit->buffer, pos);
3057 c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3058 if (!isspace (c1) && isspace (c2))
3059 break;
3060 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3061 break;
3063 edit->mark2 = min (pos + 1, edit->buffer.size);
3065 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3068 /* --------------------------------------------------------------------------------------------- */
3070 void
3071 edit_mark_current_line_cmd (WEdit * edit)
3073 edit->mark1 = edit_buffer_get_current_bol (&edit->buffer);
3074 edit->mark2 = edit_buffer_get_current_eol (&edit->buffer);
3076 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3079 /* --------------------------------------------------------------------------------------------- */
3081 void
3082 edit_delete_line (WEdit * edit)
3085 * Delete right part of the line.
3086 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3087 * beyond EOF.
3089 while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3090 (void) edit_delete (edit, TRUE);
3093 * Delete '\n' char.
3094 * Note that edit_delete() will not corrupt anything if called while
3095 * cursor position is EOF.
3097 (void) edit_delete (edit, TRUE);
3100 * Delete left part of the line.
3101 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3103 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3104 (void) edit_backspace (edit, TRUE);
3107 /* --------------------------------------------------------------------------------------------- */
3109 void
3110 edit_push_key_press (WEdit * edit)
3112 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3113 if (edit->mark2 == -1)
3115 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3116 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3120 /* --------------------------------------------------------------------------------------------- */
3122 void
3123 edit_find_bracket (WEdit * edit)
3125 edit->bracket = edit_get_bracket (edit, 1, 10000);
3126 if (edit->last_bracket != edit->bracket)
3127 edit->force |= REDRAW_PAGE;
3128 edit->last_bracket = edit->bracket;
3131 /* --------------------------------------------------------------------------------------------- */
3133 * This executes a command as though the user initiated it through a key
3134 * press. Callback with MSG_KEY as a message calls this after
3135 * translating the key press. This function can be used to pass any
3136 * command to the editor. Note that the screen wouldn't update
3137 * automatically. Either of command or char_for_insertion must be
3138 * passed as -1. Commands are executed, and char_for_insertion is
3139 * inserted at the cursor.
3142 void
3143 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3145 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3146 || (macro_index < 0
3147 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3149 macro_index = 0;
3150 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3151 return;
3153 if (macro_index != -1)
3155 edit->force |= REDRAW_COMPLETELY;
3156 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3158 edit_store_macro_cmd (edit);
3159 macro_index = -1;
3160 return;
3162 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3164 edit_repeat_macro_cmd (edit);
3165 macro_index = -1;
3166 return;
3170 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3172 record_macro_buf[macro_index].action = command;
3173 record_macro_buf[macro_index++].ch = char_for_insertion;
3175 /* record the beginning of a set of editing actions initiated by a key press */
3176 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3177 edit_push_key_press (edit);
3179 edit_execute_cmd (edit, command, char_for_insertion);
3180 if (edit->column_highlight)
3181 edit->force |= REDRAW_PAGE;
3184 /* --------------------------------------------------------------------------------------------- */
3186 This executes a command at a lower level than macro recording.
3187 It also does not push a key_press onto the undo stack. This means
3188 that if it is called many times, a single undo command will undo
3189 all of them. It also does not check for the Undo command.
3191 void
3192 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3194 Widget *w = WIDGET (edit);
3196 if (command == CK_WindowFullscreen)
3198 edit_toggle_fullscreen (edit);
3199 return;
3202 /* handle window state */
3203 if (edit_handle_move_resize (edit, command))
3204 return;
3206 edit->force |= REDRAW_LINE;
3208 /* The next key press will unhighlight the found string, so update
3209 * the whole page */
3210 if (edit->found_len || edit->column_highlight)
3211 edit->force |= REDRAW_PAGE;
3213 switch (command)
3215 /* a mark command with shift-arrow */
3216 case CK_MarkLeft:
3217 case CK_MarkRight:
3218 case CK_MarkToWordBegin:
3219 case CK_MarkToWordEnd:
3220 case CK_MarkToHome:
3221 case CK_MarkToEnd:
3222 case CK_MarkUp:
3223 case CK_MarkDown:
3224 case CK_MarkPageUp:
3225 case CK_MarkPageDown:
3226 case CK_MarkToFileBegin:
3227 case CK_MarkToFileEnd:
3228 case CK_MarkToPageBegin:
3229 case CK_MarkToPageEnd:
3230 case CK_MarkScrollUp:
3231 case CK_MarkScrollDown:
3232 case CK_MarkParagraphUp:
3233 case CK_MarkParagraphDown:
3234 /* a mark command with alt-arrow */
3235 case CK_MarkColumnPageUp:
3236 case CK_MarkColumnPageDown:
3237 case CK_MarkColumnLeft:
3238 case CK_MarkColumnRight:
3239 case CK_MarkColumnUp:
3240 case CK_MarkColumnDown:
3241 case CK_MarkColumnScrollUp:
3242 case CK_MarkColumnScrollDown:
3243 case CK_MarkColumnParagraphUp:
3244 case CK_MarkColumnParagraphDown:
3245 edit->column_highlight = 0;
3246 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3248 edit_mark_cmd (edit, TRUE); /* clear */
3249 edit_mark_cmd (edit, FALSE); /* marking on */
3251 edit->highlight = 1;
3252 break;
3254 /* any other command */
3255 default:
3256 if (edit->highlight)
3257 edit_mark_cmd (edit, FALSE); /* clear */
3258 edit->highlight = 0;
3261 /* first check for undo */
3262 if (command == CK_Undo)
3264 edit->redo_stack_reset = 0;
3265 edit_group_undo (edit);
3266 edit->found_len = 0;
3267 edit->prev_col = edit_get_col (edit);
3268 edit->search_start = edit->buffer.curs1;
3269 return;
3271 /* check for redo */
3272 if (command == CK_Redo)
3274 edit->redo_stack_reset = 0;
3275 edit_do_redo (edit);
3276 edit->found_len = 0;
3277 edit->prev_col = edit_get_col (edit);
3278 edit->search_start = edit->buffer.curs1;
3279 return;
3282 edit->redo_stack_reset = 1;
3284 /* An ordinary key press */
3285 if (char_for_insertion >= 0)
3287 /* if non persistent selection and text selected */
3288 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3289 edit_block_delete_cmd (edit);
3291 if (edit->overwrite)
3293 /* remove char only one time, after input first byte, multibyte chars */
3294 #ifdef HAVE_CHARSET
3295 if (!mc_global.utf8_display || edit->charpoint == 0)
3296 #endif
3297 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3299 edit_delete (edit, FALSE);
3301 if (option_cursor_beyond_eol && edit->over_col > 0)
3302 edit_insert_over (edit);
3303 #ifdef HAVE_CHARSET
3304 if (char_for_insertion > 255 && !mc_global.utf8_display)
3306 unsigned char str[UTF8_CHAR_LEN + 1];
3307 size_t i = 0;
3308 int res;
3310 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3311 if (res == 0)
3313 str[0] = '.';
3314 str[1] = '\0';
3316 else
3318 str[res] = '\0';
3320 while (i <= UTF8_CHAR_LEN && str[i] != '\0')
3322 char_for_insertion = str[i];
3323 edit_insert (edit, char_for_insertion);
3324 i++;
3327 else
3328 #endif
3329 edit_insert (edit, char_for_insertion);
3331 if (option_auto_para_formatting)
3333 format_paragraph (edit, FALSE);
3334 edit->force |= REDRAW_PAGE;
3336 else
3337 check_and_wrap_line (edit);
3338 edit->found_len = 0;
3339 edit->prev_col = edit_get_col (edit);
3340 edit->search_start = edit->buffer.curs1;
3341 edit_find_bracket (edit);
3342 return;
3345 switch (command)
3347 case CK_TopOnScreen:
3348 case CK_BottomOnScreen:
3349 case CK_Top:
3350 case CK_Bottom:
3351 case CK_PageUp:
3352 case CK_PageDown:
3353 case CK_Home:
3354 case CK_End:
3355 case CK_Up:
3356 case CK_Down:
3357 case CK_Left:
3358 case CK_Right:
3359 case CK_WordLeft:
3360 case CK_WordRight:
3361 if (!option_persistent_selections && edit->mark2 >= 0)
3363 if (edit->column_highlight)
3364 edit_push_undo_action (edit, COLUMN_ON);
3365 edit->column_highlight = 0;
3366 edit_mark_cmd (edit, TRUE);
3370 switch (command)
3372 case CK_TopOnScreen:
3373 case CK_BottomOnScreen:
3374 case CK_MarkToPageBegin:
3375 case CK_MarkToPageEnd:
3376 case CK_Up:
3377 case CK_Down:
3378 case CK_WordLeft:
3379 case CK_WordRight:
3380 case CK_MarkToWordBegin:
3381 case CK_MarkToWordEnd:
3382 case CK_MarkUp:
3383 case CK_MarkDown:
3384 case CK_MarkColumnUp:
3385 case CK_MarkColumnDown:
3386 if (edit->mark2 == -1)
3387 break; /*marking is following the cursor: may need to highlight a whole line */
3388 case CK_Left:
3389 case CK_Right:
3390 case CK_MarkLeft:
3391 case CK_MarkRight:
3392 edit->force |= REDRAW_CHAR_ONLY;
3395 /* basic cursor key commands */
3396 switch (command)
3398 case CK_BackSpace:
3399 /* if non persistent selection and text selected */
3400 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3401 edit_block_delete_cmd (edit);
3402 else if (option_cursor_beyond_eol && edit->over_col > 0)
3403 edit->over_col--;
3404 else if (option_backspace_through_tabs && is_in_indent (&edit->buffer))
3406 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
3407 edit_backspace (edit, TRUE);
3409 else if (option_fake_half_tabs && is_in_indent (&edit->buffer)
3410 && right_of_four_spaces (edit))
3412 int i;
3414 for (i = 0; i < HALF_TAB_SIZE; i++)
3415 edit_backspace (edit, TRUE);
3417 else
3418 edit_backspace (edit, FALSE);
3419 break;
3420 case CK_Delete:
3421 /* if non persistent selection and text selected */
3422 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3423 edit_block_delete_cmd (edit);
3424 else
3426 if (option_cursor_beyond_eol && edit->over_col > 0)
3427 edit_insert_over (edit);
3429 if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
3431 int i;
3433 for (i = 1; i <= HALF_TAB_SIZE; i++)
3434 edit_delete (edit, TRUE);
3436 else
3437 edit_delete (edit, FALSE);
3439 break;
3440 case CK_DeleteToWordBegin:
3441 edit->over_col = 0;
3442 edit_left_delete_word (edit);
3443 break;
3444 case CK_DeleteToWordEnd:
3445 if (option_cursor_beyond_eol && edit->over_col > 0)
3446 edit_insert_over (edit);
3448 edit_right_delete_word (edit);
3449 break;
3450 case CK_DeleteLine:
3451 edit_delete_line (edit);
3452 break;
3453 case CK_DeleteToHome:
3454 edit_delete_to_line_begin (edit);
3455 break;
3456 case CK_DeleteToEnd:
3457 edit_delete_to_line_end (edit);
3458 break;
3459 case CK_Enter:
3460 edit->over_col = 0;
3461 if (option_auto_para_formatting)
3463 edit_double_newline (edit);
3464 if (option_return_does_auto_indent && !bracketed_pasting_in_progress)
3465 edit_auto_indent (edit);
3466 format_paragraph (edit, FALSE);
3468 else
3470 edit_insert (edit, '\n');
3471 if (option_return_does_auto_indent && !bracketed_pasting_in_progress)
3472 edit_auto_indent (edit);
3474 break;
3475 case CK_Return:
3476 edit_insert (edit, '\n');
3477 break;
3479 case CK_MarkColumnPageUp:
3480 edit->column_highlight = 1;
3481 case CK_PageUp:
3482 case CK_MarkPageUp:
3483 edit_move_up (edit, w->lines - 1, 1);
3484 break;
3485 case CK_MarkColumnPageDown:
3486 edit->column_highlight = 1;
3487 case CK_PageDown:
3488 case CK_MarkPageDown:
3489 edit_move_down (edit, w->lines - 1, 1);
3490 break;
3491 case CK_MarkColumnLeft:
3492 edit->column_highlight = 1;
3493 case CK_Left:
3494 case CK_MarkLeft:
3495 if (option_fake_half_tabs && is_in_indent (&edit->buffer) && right_of_four_spaces (edit))
3497 if (option_cursor_beyond_eol && edit->over_col > 0)
3498 edit->over_col--;
3499 else
3500 edit_cursor_move (edit, -HALF_TAB_SIZE);
3501 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3503 else
3504 edit_left_char_move_cmd (edit);
3505 break;
3506 case CK_MarkColumnRight:
3507 edit->column_highlight = 1;
3508 case CK_Right:
3509 case CK_MarkRight:
3510 if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
3512 edit_cursor_move (edit, HALF_TAB_SIZE);
3513 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3515 else
3516 edit_right_char_move_cmd (edit);
3517 break;
3518 case CK_TopOnScreen:
3519 case CK_MarkToPageBegin:
3520 edit_begin_page (edit);
3521 break;
3522 case CK_BottomOnScreen:
3523 case CK_MarkToPageEnd:
3524 edit_end_page (edit);
3525 break;
3526 case CK_WordLeft:
3527 case CK_MarkToWordBegin:
3528 edit->over_col = 0;
3529 edit_left_word_move_cmd (edit);
3530 break;
3531 case CK_WordRight:
3532 case CK_MarkToWordEnd:
3533 edit->over_col = 0;
3534 edit_right_word_move_cmd (edit);
3535 break;
3536 case CK_MarkColumnUp:
3537 edit->column_highlight = 1;
3538 case CK_Up:
3539 case CK_MarkUp:
3540 edit_move_up (edit, 1, 0);
3541 break;
3542 case CK_MarkColumnDown:
3543 edit->column_highlight = 1;
3544 case CK_Down:
3545 case CK_MarkDown:
3546 edit_move_down (edit, 1, 0);
3547 break;
3548 case CK_MarkColumnParagraphUp:
3549 edit->column_highlight = 1;
3550 case CK_ParagraphUp:
3551 case CK_MarkParagraphUp:
3552 edit_move_up_paragraph (edit, 0);
3553 break;
3554 case CK_MarkColumnParagraphDown:
3555 edit->column_highlight = 1;
3556 case CK_ParagraphDown:
3557 case CK_MarkParagraphDown:
3558 edit_move_down_paragraph (edit, 0);
3559 break;
3560 case CK_MarkColumnScrollUp:
3561 edit->column_highlight = 1;
3562 case CK_ScrollUp:
3563 case CK_MarkScrollUp:
3564 edit_move_up (edit, 1, 1);
3565 break;
3566 case CK_MarkColumnScrollDown:
3567 edit->column_highlight = 1;
3568 case CK_ScrollDown:
3569 case CK_MarkScrollDown:
3570 edit_move_down (edit, 1, 1);
3571 break;
3572 case CK_Home:
3573 case CK_MarkToHome:
3574 edit_cursor_to_bol (edit);
3575 break;
3576 case CK_End:
3577 case CK_MarkToEnd:
3578 edit_cursor_to_eol (edit);
3579 break;
3580 case CK_Tab:
3581 /* if text marked shift block */
3582 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3584 if (edit->mark2 < 0)
3585 edit_mark_cmd (edit, FALSE);
3586 edit_move_block_to_right (edit);
3588 else
3590 if (option_cursor_beyond_eol)
3591 edit_insert_over (edit);
3592 edit_tab_cmd (edit);
3593 if (option_auto_para_formatting)
3595 format_paragraph (edit, FALSE);
3596 edit->force |= REDRAW_PAGE;
3598 else
3599 check_and_wrap_line (edit);
3601 break;
3603 case CK_InsertOverwrite:
3604 edit->overwrite = !edit->overwrite;
3605 break;
3607 case CK_Mark:
3608 if (edit->mark2 >= 0)
3610 if (edit->column_highlight)
3611 edit_push_undo_action (edit, COLUMN_ON);
3612 edit->column_highlight = 0;
3614 edit_mark_cmd (edit, FALSE);
3615 break;
3616 case CK_MarkColumn:
3617 if (!edit->column_highlight)
3618 edit_push_undo_action (edit, COLUMN_OFF);
3619 edit->column_highlight = 1;
3620 edit_mark_cmd (edit, FALSE);
3621 break;
3622 case CK_MarkAll:
3623 edit_set_markers (edit, 0, edit->buffer.size, 0, 0);
3624 edit->force |= REDRAW_PAGE;
3625 break;
3626 case CK_Unmark:
3627 if (edit->column_highlight)
3628 edit_push_undo_action (edit, COLUMN_ON);
3629 edit->column_highlight = 0;
3630 edit_mark_cmd (edit, TRUE);
3631 break;
3632 case CK_MarkWord:
3633 if (edit->column_highlight)
3634 edit_push_undo_action (edit, COLUMN_ON);
3635 edit->column_highlight = 0;
3636 edit_mark_current_word_cmd (edit);
3637 break;
3638 case CK_MarkLine:
3639 if (edit->column_highlight)
3640 edit_push_undo_action (edit, COLUMN_ON);
3641 edit->column_highlight = 0;
3642 edit_mark_current_line_cmd (edit);
3643 break;
3645 case CK_Bookmark:
3646 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR);
3647 if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR))
3648 book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3649 else
3650 book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3651 break;
3652 case CK_BookmarkFlush:
3653 book_mark_flush (edit, BOOK_MARK_COLOR);
3654 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3655 edit->force |= REDRAW_PAGE;
3656 break;
3657 case CK_BookmarkNext:
3658 if (edit->book_mark != NULL)
3660 edit_book_mark_t *p;
3662 p = book_mark_find (edit, edit->buffer.curs_line);
3663 if (p->next != NULL)
3665 p = p->next;
3666 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3667 edit_move_display (edit, p->line - w->lines / 2);
3668 edit_move_to_line (edit, p->line);
3671 break;
3672 case CK_BookmarkPrev:
3673 if (edit->book_mark != NULL)
3675 edit_book_mark_t *p;
3677 p = book_mark_find (edit, edit->buffer.curs_line);
3678 while (p->line == edit->buffer.curs_line)
3679 if (p->prev != NULL)
3680 p = p->prev;
3681 if (p->line >= 0)
3683 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3684 edit_move_display (edit, p->line - w->lines / 2);
3685 edit_move_to_line (edit, p->line);
3688 break;
3690 case CK_Top:
3691 case CK_MarkToFileBegin:
3692 edit_move_to_top (edit);
3693 break;
3694 case CK_Bottom:
3695 case CK_MarkToFileEnd:
3696 edit_move_to_bottom (edit);
3697 break;
3699 case CK_Copy:
3700 if (option_cursor_beyond_eol && edit->over_col > 0)
3701 edit_insert_over (edit);
3702 edit_block_copy_cmd (edit);
3703 break;
3704 case CK_Remove:
3705 edit_block_delete_cmd (edit);
3706 break;
3707 case CK_Move:
3708 edit_block_move_cmd (edit);
3709 break;
3711 case CK_BlockShiftLeft:
3712 if (edit->mark1 != edit->mark2)
3713 edit_move_block_to_left (edit);
3714 break;
3715 case CK_BlockShiftRight:
3716 if (edit->mark1 != edit->mark2)
3717 edit_move_block_to_right (edit);
3718 break;
3719 case CK_Store:
3720 edit_copy_to_X_buf_cmd (edit);
3721 break;
3722 case CK_Cut:
3723 edit_cut_to_X_buf_cmd (edit);
3724 break;
3725 case CK_Paste:
3726 /* if non persistent selection and text selected */
3727 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3728 edit_block_delete_cmd (edit);
3729 if (option_cursor_beyond_eol && edit->over_col > 0)
3730 edit_insert_over (edit);
3731 edit_paste_from_X_buf_cmd (edit);
3732 if (!option_persistent_selections && edit->mark2 >= 0)
3734 if (edit->column_highlight)
3735 edit_push_undo_action (edit, COLUMN_ON);
3736 edit->column_highlight = 0;
3737 edit_mark_cmd (edit, TRUE);
3739 break;
3740 case CK_History:
3741 edit_paste_from_history (edit);
3742 break;
3744 case CK_SaveAs:
3745 edit_save_as_cmd (edit);
3746 break;
3747 case CK_Save:
3748 edit_save_confirm_cmd (edit);
3749 break;
3750 case CK_BlockSave:
3751 edit_save_block_cmd (edit);
3752 break;
3753 case CK_InsertFile:
3754 edit_insert_file_cmd (edit);
3755 break;
3757 case CK_FilePrev:
3758 edit_load_back_cmd (edit);
3759 break;
3760 case CK_FileNext:
3761 edit_load_forward_cmd (edit);
3762 break;
3764 case CK_SyntaxChoose:
3765 edit_syntax_dialog (edit);
3766 break;
3768 case CK_Search:
3769 edit_search_cmd (edit, FALSE);
3770 break;
3771 case CK_SearchContinue:
3772 edit_search_cmd (edit, TRUE);
3773 break;
3774 case CK_Replace:
3775 edit_replace_cmd (edit, 0);
3776 break;
3777 case CK_ReplaceContinue:
3778 edit_replace_cmd (edit, 1);
3779 break;
3780 case CK_Complete:
3781 /* if text marked shift block */
3782 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3783 edit_move_block_to_left (edit);
3784 else
3785 edit_complete_word_cmd (edit);
3786 break;
3787 case CK_Find:
3788 edit_get_match_keyword_cmd (edit);
3789 break;
3791 #ifdef HAVE_ASPELL
3792 case CK_SpellCheckCurrentWord:
3793 edit_suggest_current_word (edit);
3794 break;
3795 case CK_SpellCheck:
3796 edit_spellcheck_file (edit);
3797 break;
3798 case CK_SpellCheckSelectLang:
3799 edit_set_spell_lang ();
3800 break;
3801 #endif
3803 case CK_Date:
3805 char s[BUF_MEDIUM];
3806 /* fool gcc to prevent a Y2K warning */
3807 char time_format[] = "_c";
3808 time_format[0] = '%';
3810 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3811 edit_print_string (edit, s);
3812 edit->force |= REDRAW_PAGE;
3814 break;
3815 case CK_Goto:
3816 edit_goto_cmd (edit);
3817 break;
3818 case CK_ParagraphFormat:
3819 format_paragraph (edit, TRUE);
3820 edit->force |= REDRAW_PAGE;
3821 break;
3822 case CK_MacroDelete:
3823 edit_delete_macro_cmd (edit);
3824 break;
3825 case CK_MatchBracket:
3826 edit_goto_matching_bracket (edit);
3827 break;
3828 case CK_UserMenu:
3829 user_menu (edit, NULL, -1);
3830 break;
3831 case CK_Sort:
3832 edit_sort_cmd (edit);
3833 break;
3834 case CK_ExternalCommand:
3835 edit_ext_cmd (edit);
3836 break;
3837 case CK_Mail:
3838 edit_mail_dialog (edit);
3839 break;
3840 #ifdef HAVE_CHARSET
3841 case CK_SelectCodepage:
3842 edit_select_codepage_cmd (edit);
3843 break;
3844 #endif
3845 case CK_InsertLiteral:
3846 edit_insert_literal_cmd (edit);
3847 break;
3848 case CK_MacroStartStopRecord:
3849 edit_begin_end_macro_cmd (edit);
3850 break;
3851 case CK_RepeatStartStopRecord:
3852 edit_begin_end_repeat_cmd (edit);
3853 break;
3854 case CK_ExtendedKeyMap:
3855 edit->extmod = TRUE;
3856 break;
3857 default:
3858 break;
3861 /* CK_PipeBlock */
3862 if ((command / CK_PipeBlock (0)) == 1)
3863 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
3865 /* keys which must set the col position, and the search vars */
3866 switch (command)
3868 case CK_Search:
3869 case CK_SearchContinue:
3870 case CK_Replace:
3871 case CK_ReplaceContinue:
3872 case CK_Complete:
3873 edit->prev_col = edit_get_col (edit);
3874 break;
3875 case CK_Up:
3876 case CK_MarkUp:
3877 case CK_MarkColumnUp:
3878 case CK_Down:
3879 case CK_MarkDown:
3880 case CK_MarkColumnDown:
3881 case CK_PageUp:
3882 case CK_MarkPageUp:
3883 case CK_MarkColumnPageUp:
3884 case CK_PageDown:
3885 case CK_MarkPageDown:
3886 case CK_MarkColumnPageDown:
3887 case CK_Top:
3888 case CK_MarkToFileBegin:
3889 case CK_Bottom:
3890 case CK_MarkToFileEnd:
3891 case CK_ParagraphUp:
3892 case CK_MarkParagraphUp:
3893 case CK_MarkColumnParagraphUp:
3894 case CK_ParagraphDown:
3895 case CK_MarkParagraphDown:
3896 case CK_MarkColumnParagraphDown:
3897 case CK_ScrollUp:
3898 case CK_MarkScrollUp:
3899 case CK_MarkColumnScrollUp:
3900 case CK_ScrollDown:
3901 case CK_MarkScrollDown:
3902 case CK_MarkColumnScrollDown:
3903 edit->search_start = edit->buffer.curs1;
3904 edit->found_len = 0;
3905 break;
3906 default:
3907 edit->found_len = 0;
3908 edit->prev_col = edit_get_col (edit);
3909 edit->search_start = edit->buffer.curs1;
3911 edit_find_bracket (edit);
3913 if (option_auto_para_formatting)
3915 switch (command)
3917 case CK_BackSpace:
3918 case CK_Delete:
3919 case CK_DeleteToWordBegin:
3920 case CK_DeleteToWordEnd:
3921 case CK_DeleteToHome:
3922 case CK_DeleteToEnd:
3923 format_paragraph (edit, FALSE);
3924 edit->force |= REDRAW_PAGE;
3929 /* --------------------------------------------------------------------------------------------- */
3931 void
3932 edit_stack_init (void)
3934 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3936 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
3937 edit_history_moveto[edit_stack_iterator].line = -1;
3940 edit_stack_iterator = 0;
3943 /* --------------------------------------------------------------------------------------------- */
3945 void
3946 edit_stack_free (void)
3948 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3949 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
3952 /* --------------------------------------------------------------------------------------------- */
3953 /** move i lines */
3955 void
3956 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
3958 edit_move_updown (edit, i, do_scroll, TRUE);
3961 /* --------------------------------------------------------------------------------------------- */
3962 /** move i lines */
3964 void
3965 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
3967 edit_move_updown (edit, i, do_scroll, FALSE);
3970 /* --------------------------------------------------------------------------------------------- */