Fix editor crash in "Save as..." command.
[midnight-commander.git] / src / editor / edit.c
blob49d9f7755277a080a846cf3c1629359d1552322b
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
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.
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 <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/learn.h" /* learn_keys */
68 #include "src/keybind-defaults.h"
70 #include "edit-impl.h"
71 #include "editwidget.h"
72 #ifdef HAVE_ASPELL
73 #include "spell.h"
74 #endif
76 /*** global variables ****************************************************************************/
78 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
79 int option_typewriter_wrap = 0;
80 int option_auto_para_formatting = 0;
81 int option_fill_tabs_with_spaces = 0;
82 int option_return_does_auto_indent = 1;
83 int option_backspace_through_tabs = 0;
84 int option_fake_half_tabs = 1;
85 int option_save_mode = EDIT_QUICK_SAVE;
86 int option_save_position = 1;
87 int option_max_undo = 32768;
88 int option_persistent_selections = 1;
89 int option_cursor_beyond_eol = 0;
90 int option_line_state = 0;
91 int option_line_state_width = 0;
92 gboolean option_cursor_after_inserted_block = FALSE;
94 int option_edit_right_extreme = 0;
95 int option_edit_left_extreme = 0;
96 int option_edit_top_extreme = 0;
97 int option_edit_bottom_extreme = 0;
98 int enable_show_tabs_tws = 1;
99 int option_check_nl_at_eof = 0;
100 int option_group_undo = 0;
101 int show_right_margin = 0;
103 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
104 char *option_backup_ext = NULL;
106 unsigned int edit_stack_iterator = 0;
107 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
111 /*** file scope macro definitions ****************************************************************/
113 #define TEMP_BUF_LEN 1024
115 #define space_width 1
117 /*** file scope type declarations ****************************************************************/
119 /*** file scope variables ************************************************************************/
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
127 const char *read, *write, *extension;
128 } all_filters[] =
130 /* *INDENT-OFF* */
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
133 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 /* *INDENT-ON* */
139 static off_t last_bracket = -1;
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
146 * here's a quick sketch of the layout: (don't run this through indent.)
148 * (b1 is buffers1 and b2 is buffers2)
151 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
152 * ______________________________________|______________________________________
154 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
155 * |-> |-> |-> |-> |-> |-> |
157 * _<------------------------->|<----------------->_
158 * WEdit->curs2 | WEdit->curs1
159 * ^ | ^
160 * | ^|^ |
161 * cursor ||| cursor
162 * |||
163 * file end|||file beginning
168 * This_is_some_file
169 * fin.
172 /* --------------------------------------------------------------------------------------------- */
174 * Initialize the buffers for an empty files.
177 static void
178 edit_init_buffers (WEdit * edit)
180 int j;
182 for (j = 0; j <= MAXBUFF; j++)
184 edit->buffers1[j] = NULL;
185 edit->buffers2[j] = NULL;
188 edit->curs1 = 0;
189 edit->curs2 = 0;
190 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
193 /* --------------------------------------------------------------------------------------------- */
196 * Load file OR text into buffers. Set cursor to the beginning of file.
198 * @return FALSE on error.
201 static gboolean
202 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
204 off_t buf, buf2;
205 int file = -1;
206 gboolean ret = FALSE;
208 edit->curs2 = edit->last_byte;
209 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
211 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
212 if (file == -1)
214 gchar *errmsg, *filename;
216 filename = vfs_path_to_str (filename_vpath);
217 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
218 g_free (filename);
219 edit_error_dialog (_("Error"), errmsg);
220 g_free (errmsg);
221 return FALSE;
224 if (!edit->buffers2[buf2])
225 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
229 if (mc_read (file,
230 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
231 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
232 break;
234 for (buf = buf2 - 1; buf >= 0; buf--)
236 /* edit->buffers2[0] is already allocated */
237 if (!edit->buffers2[buf])
238 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
239 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
240 break;
242 ret = TRUE;
244 while (FALSE);
246 if (!ret)
248 gchar *errmsg, *filename;
250 filename = vfs_path_to_str (filename_vpath);
251 errmsg = g_strdup_printf (_("Error reading %s"), filename);
252 g_free (filename);
253 edit_error_dialog (_("Error"), errmsg);
254 g_free (errmsg);
256 mc_close (file);
257 return ret;
260 /* --------------------------------------------------------------------------------------------- */
261 /** Return index of the filter or -1 is there is no appropriate filter */
263 static int
264 edit_find_filter (const vfs_path_t * filename_vpath)
266 size_t i, l, e;
267 char *filename;
269 if (filename_vpath == NULL)
270 return -1;
272 filename = vfs_path_to_str (filename_vpath);
273 l = strlen (filename);
274 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
276 e = strlen (all_filters[i].extension);
277 if (l > e)
278 if (!strcmp (all_filters[i].extension, filename + l - e))
280 g_free (filename);
281 return i;
284 g_free (filename);
285 return -1;
288 /* --------------------------------------------------------------------------------------------- */
290 static char *
291 edit_get_filter (const vfs_path_t * filename_vpath)
293 int i;
294 char *p, *quoted_name, *filename;
296 i = edit_find_filter (filename_vpath);
297 if (i < 0)
298 return NULL;
300 filename = vfs_path_to_str (filename_vpath);
301 quoted_name = name_quote (filename, 0);
302 g_free (filename);
303 p = g_strdup_printf (all_filters[i].read, quoted_name);
304 g_free (quoted_name);
305 return p;
308 /* --------------------------------------------------------------------------------------------- */
310 static off_t
311 edit_insert_stream (WEdit * edit, FILE * f)
313 int c;
314 off_t i = 0;
315 while ((c = fgetc (f)) >= 0)
317 edit_insert (edit, c);
318 i++;
320 return i;
323 /* --------------------------------------------------------------------------------------------- */
325 * Open file and create it if necessary.
327 * @param edit editor object
328 * @param filename_vpath file name
329 * @param st buffer for store stat info
330 * @return TRUE for success, FALSE for error.
333 static gboolean
334 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
336 int file;
337 gchar *errmsg = NULL;
339 /* Try opening an existing file */
340 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
341 if (file < 0)
344 * Try creating the file. O_EXCL prevents following broken links
345 * and opening existing files.
347 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
348 if (file < 0)
350 char *filename;
352 filename = vfs_path_to_str (filename_vpath);
353 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
354 g_free (filename);
355 goto cleanup;
358 /* New file, delete it if it's not modified or saved */
359 edit->delete_file = 1;
362 /* Check what we have opened */
363 if (mc_fstat (file, st) < 0)
365 char *filename;
367 filename = vfs_path_to_str (filename_vpath);
368 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
369 g_free (filename);
370 goto cleanup;
373 /* We want to open regular files only */
374 if (!S_ISREG (st->st_mode))
376 char *filename;
378 filename = vfs_path_to_str (filename_vpath);
379 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
380 g_free (filename);
381 goto cleanup;
385 * Don't delete non-empty files.
386 * O_EXCL should prevent it, but let's be on the safe side.
388 if (st->st_size > 0)
389 edit->delete_file = 0;
391 if (st->st_size >= SIZE_LIMIT)
393 char *filename;
395 filename = vfs_path_to_str (filename_vpath);
396 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
397 g_free (filename);
400 cleanup:
401 (void) mc_close (file);
403 if (errmsg != NULL)
405 edit_error_dialog (_("Error"), errmsg);
406 g_free (errmsg);
407 return FALSE;
409 return TRUE;
412 /* --------------------------------------------------------------------------------------------- */
415 * Open the file and load it into the buffers, either directly or using
416 * a filter. Return TRUE on success, FALSE on error.
418 * Fast loading (edit_load_file_fast) is used when the file size is
419 * known. In this case the data is read into the buffers by blocks.
420 * If the file size is not known, the data is loaded byte by byte in
421 * edit_insert_file.
423 * @param edit editor object
424 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
426 static gboolean
427 edit_load_file (WEdit * edit)
429 gboolean fast_load = TRUE;
431 /* Cannot do fast load if a filter is used */
432 if (edit_find_filter (edit->filename_vpath) >= 0)
433 fast_load = FALSE;
436 * FIXME: line end translation should disable fast loading as well
437 * Consider doing fseek() to the end and ftell() for the real size.
439 if (edit->filename_vpath != NULL)
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (edit->filename_vpath))
446 fast_load = FALSE;
448 /* If we are dealing with a real file, check that it exists */
449 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
451 edit_clean (edit);
452 return FALSE;
455 else
457 /* nothing to load */
458 fast_load = FALSE;
461 edit_init_buffers (edit);
463 if (fast_load)
465 edit->last_byte = edit->stat1.st_size;
466 edit_load_file_fast (edit, edit->filename_vpath);
467 /* If fast load was used, the number of lines wasn't calculated */
468 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
470 else
472 edit->last_byte = 0;
473 if (edit->filename_vpath != NULL
474 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
476 edit->undo_stack_disable = 1;
477 if (edit_insert_file (edit, edit->filename_vpath) < 0)
479 edit_clean (edit);
480 return FALSE;
482 edit->undo_stack_disable = 0;
485 edit->lb = LB_ASIS;
486 return TRUE;
489 /* --------------------------------------------------------------------------------------------- */
490 /** Restore saved cursor position in the file */
492 static void
493 edit_load_position (WEdit * edit)
495 long line, column;
496 off_t offset;
498 if (edit->filename_vpath == NULL
499 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
500 return;
502 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
504 if (line > 0)
506 edit_move_to_line (edit, line - 1);
507 edit->prev_col = column;
509 else if (offset > 0)
511 edit_cursor_move (edit, offset);
512 line = edit->curs_line;
513 edit->search_start = edit->curs1;
516 book_mark_restore (edit, BOOK_MARK_COLOR);
518 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
519 edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
522 /* --------------------------------------------------------------------------------------------- */
523 /** Save cursor position in the file */
525 static void
526 edit_save_position (WEdit * edit)
528 if (edit->filename_vpath == NULL
529 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
530 return;
532 book_mark_serialize (edit, BOOK_MARK_COLOR);
533 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
534 edit->serialized_bookmarks);
535 edit->serialized_bookmarks = NULL;
538 /* --------------------------------------------------------------------------------------------- */
539 /** Clean the WEdit stricture except the widget part */
541 static void
542 edit_purge_widget (WEdit * edit)
544 size_t len = sizeof (WEdit) - sizeof (Widget);
545 char *start = (char *) edit + sizeof (Widget);
546 memset (start, 0, len);
549 /* --------------------------------------------------------------------------------------------- */
552 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
553 then the file should be as it was when he loaded up. Then set edit->modified to 0.
556 static long
557 edit_pop_undo_action (WEdit * edit)
559 long c;
560 unsigned long sp = edit->undo_stack_pointer;
562 if (sp == edit->undo_stack_bottom)
563 return STACK_BOTTOM;
565 sp = (sp - 1) & edit->undo_stack_size_mask;
566 c = edit->undo_stack[sp];
567 if (c >= 0)
569 /* edit->undo_stack[sp] = '@'; */
570 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
571 return c;
574 if (sp == edit->undo_stack_bottom)
575 return STACK_BOTTOM;
577 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
578 if (edit->undo_stack[sp] == -2)
580 /* edit->undo_stack[sp] = '@'; */
581 edit->undo_stack_pointer = sp;
583 else
584 edit->undo_stack[sp]++;
586 return c;
589 static long
590 edit_pop_redo_action (WEdit * edit)
592 long c;
593 unsigned long sp = edit->redo_stack_pointer;
595 if (sp == edit->redo_stack_bottom)
596 return STACK_BOTTOM;
598 sp = (sp - 1) & edit->redo_stack_size_mask;
599 c = edit->redo_stack[sp];
600 if (c >= 0)
602 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
603 return c;
606 if (sp == edit->redo_stack_bottom)
607 return STACK_BOTTOM;
609 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
610 if (edit->redo_stack[sp] == -2)
611 edit->redo_stack_pointer = sp;
612 else
613 edit->redo_stack[sp]++;
615 return c;
618 static long
619 get_prev_undo_action (WEdit * edit)
621 long c;
622 unsigned long sp = edit->undo_stack_pointer;
624 if (sp == edit->undo_stack_bottom)
625 return STACK_BOTTOM;
627 sp = (sp - 1) & edit->undo_stack_size_mask;
628 c = edit->undo_stack[sp];
629 if (c >= 0)
630 return c;
632 if (sp == edit->undo_stack_bottom)
633 return STACK_BOTTOM;
635 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
636 return c;
639 /* --------------------------------------------------------------------------------------------- */
640 /** is called whenever a modification is made by one of the four routines below */
642 static void
643 edit_modification (WEdit * edit)
645 edit->caches_valid = FALSE;
647 /* raise lock when file modified */
648 if (!edit->modified && !edit->delete_file)
649 edit->locked = lock_file (edit->filename_vpath);
650 edit->modified = 1;
653 /* --------------------------------------------------------------------------------------------- */
655 #ifdef HAVE_CHARSET
656 static char *
657 edit_get_byte_ptr (const WEdit * edit, off_t byte_index)
659 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
660 return NULL;
662 if (byte_index >= edit->curs1)
664 off_t p;
666 p = edit->curs1 + edit->curs2 - byte_index - 1;
667 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
668 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
671 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
672 (byte_index & M_EDIT_BUF_SIZE));
674 #endif
676 /* --------------------------------------------------------------------------------------------- */
678 #ifdef HAVE_CHARSET
679 static int
680 edit_get_prev_utf (const WEdit * edit, off_t byte_index, int *char_width)
682 int i, res;
683 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
684 gchar *str;
685 gchar *cursor_buf_ptr;
687 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
689 *char_width = 0;
690 return 0;
693 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
694 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
695 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
697 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
698 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
700 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
702 *char_width = 1;
703 return *(cursor_buf_ptr - 1);
705 else
707 res = g_utf8_get_char_validated (str, -1);
709 if (res < 0)
711 *char_width = 1;
712 return *(cursor_buf_ptr - 1);
714 else
716 *char_width = cursor_buf_ptr - str;
717 return res;
721 #endif
723 /* --------------------------------------------------------------------------------------------- */
724 /* high level cursor movement commands */
725 /* --------------------------------------------------------------------------------------------- */
726 /** check whether cursor is in indent part of line
728 * @param edit editor object
730 * @return TRUE if cursor is in indent, FALSE otherwise
733 static gboolean
734 is_in_indent (const WEdit * edit)
736 off_t p;
738 for (p = edit_bol (edit, edit->curs1); p < edit->curs1; p++)
739 if (strchr (" \t", edit_get_byte (edit, p)) == NULL)
740 return FALSE;
742 return TRUE;
745 /* --------------------------------------------------------------------------------------------- */
746 /** check whether line in editor is blank or not
748 * @param edit editor object
749 * @param offset position in file
751 * @return TRUE if line in blank, FALSE otherwise
754 static gboolean
755 is_blank (const WEdit * edit, off_t offset)
757 off_t s, f;
758 int c;
760 s = edit_bol (edit, offset);
761 f = edit_eol (edit, offset) - 1;
762 while (s <= f)
764 c = edit_get_byte (edit, s++);
765 if (!isspace (c))
766 return FALSE;
768 return TRUE;
771 /* --------------------------------------------------------------------------------------------- */
772 /** returns the offset of line i */
774 static off_t
775 edit_find_line (WEdit * edit, long line)
777 long i, j = 0;
778 long m = 2000000000; /* what is the magic number? */
780 if (!edit->caches_valid)
782 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
783 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
784 /* three offsets that we *know* are line 0 at 0 and these two: */
785 edit->line_numbers[1] = edit->curs_line;
786 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
787 edit->line_numbers[2] = edit->total_lines;
788 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
789 edit->caches_valid = TRUE;
791 if (line >= edit->total_lines)
792 return edit->line_offsets[2];
793 if (line <= 0)
794 return 0;
795 /* find the closest known point */
796 for (i = 0; i < N_LINE_CACHES; i++)
798 long n;
800 n = abs (edit->line_numbers[i] - line);
801 if (n < m)
803 m = n;
804 j = i;
807 if (m == 0)
808 return edit->line_offsets[j]; /* know the offset exactly */
809 if (m == 1 && j >= 3)
810 i = j; /* one line different - caller might be looping, so stay in this cache */
811 else
812 i = 3 + (rand () % (N_LINE_CACHES - 3));
813 if (line > edit->line_numbers[j])
814 edit->line_offsets[i] =
815 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
816 else
817 edit->line_offsets[i] =
818 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
819 edit->line_numbers[i] = line;
820 return edit->line_offsets[i];
823 /* --------------------------------------------------------------------------------------------- */
824 /** moves up until a blank line is reached, or until just
825 before a non-blank line is reached */
827 static void
828 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
830 long i = 0;
832 if (edit->curs_line > 1)
834 if (!edit_line_is_blank (edit, edit->curs_line))
836 for (i = edit->curs_line - 1; i != 0; i--)
837 if (edit_line_is_blank (edit, i))
838 break;
840 else if (edit_line_is_blank (edit, edit->curs_line - 1))
842 for (i = edit->curs_line - 1; i != 0; i--)
843 if (!edit_line_is_blank (edit, i))
845 i++;
846 break;
849 else
851 for (i = edit->curs_line - 1; i != 0; i--)
852 if (edit_line_is_blank (edit, i))
853 break;
857 edit_move_up (edit, edit->curs_line - i, do_scroll);
860 /* --------------------------------------------------------------------------------------------- */
861 /** moves down until a blank line is reached, or until just
862 before a non-blank line is reached */
864 static void
865 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
867 long i;
869 if (edit->curs_line >= edit->total_lines - 1)
870 i = edit->total_lines;
871 else if (!edit_line_is_blank (edit, edit->curs_line))
873 for (i = edit->curs_line + 1; i != 0; i++)
874 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
875 break;
877 else if (edit_line_is_blank (edit, edit->curs_line + 1))
879 for (i = edit->curs_line + 1; i != 0; i++)
880 if (!edit_line_is_blank (edit, i) || i > edit->total_lines)
882 i--;
883 break;
886 else
888 for (i = edit->curs_line + 1; i != 0; i++)
889 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
890 break;
892 edit_move_down (edit, i - edit->curs_line, do_scroll);
895 /* --------------------------------------------------------------------------------------------- */
897 static void
898 edit_begin_page (WEdit * edit)
900 edit_update_curs_row (edit);
901 edit_move_up (edit, edit->curs_row, 0);
904 /* --------------------------------------------------------------------------------------------- */
906 static void
907 edit_end_page (WEdit * edit)
909 edit_update_curs_row (edit);
910 edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, 0);
914 /* --------------------------------------------------------------------------------------------- */
915 /** goto beginning of text */
917 static void
918 edit_move_to_top (WEdit * edit)
920 if (edit->curs_line)
922 edit_cursor_move (edit, -edit->curs1);
923 edit_move_to_prev_col (edit, 0);
924 edit->force |= REDRAW_PAGE;
925 edit->search_start = 0;
926 edit_update_curs_row (edit);
931 /* --------------------------------------------------------------------------------------------- */
932 /** goto end of text */
934 static void
935 edit_move_to_bottom (WEdit * edit)
937 if (edit->curs_line < edit->total_lines)
939 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
940 edit->start_display = edit->last_byte;
941 edit->start_line = edit->total_lines;
942 edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
943 edit->force |= REDRAW_PAGE;
947 /* --------------------------------------------------------------------------------------------- */
948 /** goto beginning of line */
950 static void
951 edit_cursor_to_bol (WEdit * edit)
953 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
954 edit->search_start = edit->curs1;
955 edit->prev_col = edit_get_col (edit);
956 edit->over_col = 0;
959 /* --------------------------------------------------------------------------------------------- */
960 /** goto end of line */
962 static void
963 edit_cursor_to_eol (WEdit * edit)
965 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
966 edit->search_start = edit->curs1;
967 edit->prev_col = edit_get_col (edit);
968 edit->over_col = 0;
971 /* --------------------------------------------------------------------------------------------- */
973 static unsigned long
974 my_type_of (int c)
976 int x, r = 0;
977 const char *p, *q;
978 const char option_chars_move_whole_word[] =
979 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
981 if (c == 0)
982 return 0;
983 if (c == '!')
985 if (*option_chars_move_whole_word == '!')
986 return 2;
987 return 0x80000000UL;
989 if (g_ascii_isupper ((gchar) c))
990 c = 'A';
991 else if (g_ascii_islower ((gchar) c))
992 c = 'a';
993 else if (g_ascii_isalpha (c))
994 c = 'a';
995 else if (isdigit (c))
996 c = '0';
997 else if (isspace (c))
998 c = ' ';
999 q = strchr (option_chars_move_whole_word, c);
1000 if (!q)
1001 return 0xFFFFFFFFUL;
1004 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1005 if (*p == '!')
1006 x <<= 1;
1007 r |= x;
1009 while ((q = strchr (q + 1, c)));
1010 return r;
1013 /* --------------------------------------------------------------------------------------------- */
1015 static void
1016 edit_left_word_move (WEdit * edit, int s)
1018 while (TRUE)
1020 int c1, c2;
1022 if (edit->column_highlight
1023 && edit->mark1 != edit->mark2
1024 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1025 break;
1026 edit_cursor_move (edit, -1);
1027 if (edit->curs1 == 0)
1028 break;
1029 c1 = edit_get_byte (edit, edit->curs1 - 1);
1030 c2 = edit_get_byte (edit, edit->curs1);
1031 if (c1 == '\n' || c2 == '\n')
1032 break;
1033 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1034 break;
1035 if (isspace (c1) && !isspace (c2))
1036 break;
1037 if (s != 0 && !isspace (c1) && isspace (c2))
1038 break;
1042 /* --------------------------------------------------------------------------------------------- */
1044 static void
1045 edit_left_word_move_cmd (WEdit * edit)
1047 edit_left_word_move (edit, 0);
1048 edit->force |= REDRAW_PAGE;
1051 /* --------------------------------------------------------------------------------------------- */
1053 static void
1054 edit_right_word_move (WEdit * edit, int s)
1056 while (TRUE)
1058 int c1, c2;
1060 if (edit->column_highlight
1061 && edit->mark1 != edit->mark2
1062 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1063 break;
1064 edit_cursor_move (edit, 1);
1065 if (edit->curs1 >= edit->last_byte)
1066 break;
1067 c1 = edit_get_byte (edit, edit->curs1 - 1);
1068 c2 = edit_get_byte (edit, edit->curs1);
1069 if (c1 == '\n' || c2 == '\n')
1070 break;
1071 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1072 break;
1073 if (isspace (c1) && !isspace (c2))
1074 break;
1075 if (s != 0 && !isspace (c1) && isspace (c2))
1076 break;
1080 /* --------------------------------------------------------------------------------------------- */
1082 static void
1083 edit_right_word_move_cmd (WEdit * edit)
1085 edit_right_word_move (edit, 0);
1086 edit->force |= REDRAW_PAGE;
1089 /* --------------------------------------------------------------------------------------------- */
1091 static void
1092 edit_right_char_move_cmd (WEdit * edit)
1094 int cw = 1;
1095 int c = 0;
1096 #ifdef HAVE_CHARSET
1097 if (edit->utf8)
1099 c = edit_get_utf (edit, edit->curs1, &cw);
1100 if (cw < 1)
1101 cw = 1;
1103 else
1104 #endif
1106 c = edit_get_byte (edit, edit->curs1);
1108 if (option_cursor_beyond_eol && c == '\n')
1110 edit->over_col++;
1112 else
1114 edit_cursor_move (edit, cw);
1118 /* --------------------------------------------------------------------------------------------- */
1120 static void
1121 edit_left_char_move_cmd (WEdit * edit)
1123 int cw = 1;
1124 if (edit->column_highlight
1125 && option_cursor_beyond_eol
1126 && edit->mark1 != edit->mark2
1127 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1128 return;
1129 #ifdef HAVE_CHARSET
1130 if (edit->utf8)
1132 edit_get_prev_utf (edit, edit->curs1, &cw);
1133 if (cw < 1)
1134 cw = 1;
1136 #endif
1137 if (option_cursor_beyond_eol && edit->over_col > 0)
1139 edit->over_col--;
1141 else
1143 edit_cursor_move (edit, -cw);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Up or down cursor moving.
1149 direction = TRUE - move up
1150 = FALSE - move down
1153 static void
1154 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1156 long p;
1157 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1159 if (lines > l)
1160 lines = l;
1162 if (lines == 0)
1163 return;
1165 if (lines > 1)
1166 edit->force |= REDRAW_PAGE;
1167 if (do_scroll)
1169 if (direction)
1170 edit_scroll_upward (edit, lines);
1171 else
1172 edit_scroll_downward (edit, lines);
1174 p = edit_bol (edit, edit->curs1);
1176 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1178 edit_cursor_move (edit, p - edit->curs1);
1180 edit_move_to_prev_col (edit, p);
1182 /* search start of current multibyte char (like CJK) */
1183 if (edit->curs1 + 1 < edit->last_byte)
1185 edit_right_char_move_cmd (edit);
1186 edit_left_char_move_cmd (edit);
1189 edit->search_start = edit->curs1;
1190 edit->found_len = 0;
1193 /* --------------------------------------------------------------------------------------------- */
1195 static void
1196 edit_right_delete_word (WEdit * edit)
1198 while (edit->curs1 < edit->last_byte)
1200 int c1, c2;
1202 c1 = edit_delete (edit, TRUE);
1203 c2 = edit_get_byte (edit, edit->curs1);
1204 if (c1 == '\n' || c2 == '\n')
1205 break;
1206 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1207 break;
1208 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1209 break;
1213 /* --------------------------------------------------------------------------------------------- */
1215 static void
1216 edit_left_delete_word (WEdit * edit)
1218 while (edit->curs1 > 0)
1220 int c1, c2;
1222 c1 = edit_backspace (edit, TRUE);
1223 c2 = edit_get_byte (edit, edit->curs1 - 1);
1224 if (c1 == '\n' || c2 == '\n')
1225 break;
1226 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1227 break;
1228 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1229 break;
1233 /* --------------------------------------------------------------------------------------------- */
1235 the start column position is not recorded, and hence does not
1236 undo as it happed. But who would notice.
1239 static void
1240 edit_do_undo (WEdit * edit)
1242 long ac;
1243 long count = 0;
1245 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1246 edit->over_col = 0;
1247 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1249 switch ((int) ac)
1251 case STACK_BOTTOM:
1252 goto done_undo;
1253 case CURS_RIGHT:
1254 edit_cursor_move (edit, 1);
1255 break;
1256 case CURS_LEFT:
1257 edit_cursor_move (edit, -1);
1258 break;
1259 case BACKSPACE:
1260 case BACKSPACE_BR:
1261 edit_backspace (edit, TRUE);
1262 break;
1263 case DELCHAR:
1264 case DELCHAR_BR:
1265 edit_delete (edit, TRUE);
1266 break;
1267 case COLUMN_ON:
1268 edit->column_highlight = 1;
1269 break;
1270 case COLUMN_OFF:
1271 edit->column_highlight = 0;
1272 break;
1274 if (ac >= 256 && ac < 512)
1275 edit_insert_ahead (edit, ac - 256);
1276 if (ac >= 0 && ac < 256)
1277 edit_insert (edit, ac);
1279 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1281 edit->mark1 = ac - MARK_1;
1282 edit->column1 =
1283 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1285 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1287 edit->mark2 = ac - MARK_2;
1288 edit->column2 =
1289 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1291 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1293 edit->end_mark_curs = ac - MARK_CURS;
1295 if (count++)
1296 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1299 if (edit->start_display > ac - KEY_PRESS)
1301 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1302 edit->force |= REDRAW_PAGE;
1304 else if (edit->start_display < ac - KEY_PRESS)
1306 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1307 edit->force |= REDRAW_PAGE;
1309 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1310 edit_update_curs_row (edit);
1312 done_undo:
1313 edit->undo_stack_disable = 0;
1316 /* --------------------------------------------------------------------------------------------- */
1318 static void
1319 edit_do_redo (WEdit * edit)
1321 long ac;
1322 long count = 0;
1324 if (edit->redo_stack_reset)
1325 return;
1327 edit->over_col = 0;
1328 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1330 switch ((int) ac)
1332 case STACK_BOTTOM:
1333 goto done_redo;
1334 case CURS_RIGHT:
1335 edit_cursor_move (edit, 1);
1336 break;
1337 case CURS_LEFT:
1338 edit_cursor_move (edit, -1);
1339 break;
1340 case BACKSPACE:
1341 edit_backspace (edit, TRUE);
1342 break;
1343 case DELCHAR:
1344 edit_delete (edit, TRUE);
1345 break;
1346 case COLUMN_ON:
1347 edit->column_highlight = 1;
1348 break;
1349 case COLUMN_OFF:
1350 edit->column_highlight = 0;
1351 break;
1353 if (ac >= 256 && ac < 512)
1354 edit_insert_ahead (edit, ac - 256);
1355 if (ac >= 0 && ac < 256)
1356 edit_insert (edit, ac);
1358 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1360 edit->mark1 = ac - MARK_1;
1361 edit->column1 =
1362 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1364 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1366 edit->mark2 = ac - MARK_2;
1367 edit->column2 =
1368 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1370 /* more than one pop usually means something big */
1371 if (count++)
1372 edit->force |= REDRAW_PAGE;
1375 if (edit->start_display > ac - KEY_PRESS)
1377 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1378 edit->force |= REDRAW_PAGE;
1380 else if (edit->start_display < ac - KEY_PRESS)
1382 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1383 edit->force |= REDRAW_PAGE;
1385 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1386 edit_update_curs_row (edit);
1388 done_redo:
1392 /* --------------------------------------------------------------------------------------------- */
1394 static void
1395 edit_group_undo (WEdit * edit)
1397 long ac = KEY_PRESS;
1398 long cur_ac = KEY_PRESS;
1399 while (ac != STACK_BOTTOM && ac == cur_ac)
1401 cur_ac = get_prev_undo_action (edit);
1402 edit_do_undo (edit);
1403 ac = get_prev_undo_action (edit);
1404 /* exit from cycle if option_group_undo is not set,
1405 * and make single UNDO operation
1407 if (!option_group_undo)
1408 ac = STACK_BOTTOM;
1412 /* --------------------------------------------------------------------------------------------- */
1414 static void
1415 edit_delete_to_line_end (WEdit * edit)
1417 while (edit_get_byte (edit, edit->curs1) != '\n' && edit->curs2 != 0)
1418 edit_delete (edit, TRUE);
1421 /* --------------------------------------------------------------------------------------------- */
1423 static void
1424 edit_delete_to_line_begin (WEdit * edit)
1426 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 != 0)
1427 edit_backspace (edit, TRUE);
1430 /* --------------------------------------------------------------------------------------------- */
1432 static gboolean
1433 is_aligned_on_a_tab (WEdit * edit)
1435 long curs_col;
1437 edit_update_curs_col (edit);
1438 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1439 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1442 /* --------------------------------------------------------------------------------------------- */
1444 static gboolean
1445 right_of_four_spaces (WEdit * edit)
1447 int i, ch = 0;
1449 for (i = 1; i <= HALF_TAB_SIZE; i++)
1450 ch |= edit_get_byte (edit, edit->curs1 - i);
1452 return (ch == ' ' && is_aligned_on_a_tab (edit));
1455 /* --------------------------------------------------------------------------------------------- */
1457 static gboolean
1458 left_of_four_spaces (WEdit * edit)
1460 int i, ch = 0;
1462 for (i = 0; i < HALF_TAB_SIZE; i++)
1463 ch |= edit_get_byte (edit, edit->curs1 + i);
1465 return (ch == ' ' && is_aligned_on_a_tab (edit));
1468 /* --------------------------------------------------------------------------------------------- */
1470 static void
1471 edit_auto_indent (WEdit * edit)
1473 off_t p;
1474 char c;
1476 p = edit->curs1;
1477 /* use the previous line as a template */
1478 p = edit_move_backward (edit, p, 1);
1479 /* copy the leading whitespace of the line */
1480 while (TRUE)
1481 { /* no range check - the line _is_ \n-terminated */
1482 c = edit_get_byte (edit, p++);
1483 if (c != ' ' && c != '\t')
1484 break;
1485 edit_insert (edit, c);
1489 /* --------------------------------------------------------------------------------------------- */
1491 static inline void
1492 edit_double_newline (WEdit * edit)
1494 edit_insert (edit, '\n');
1495 if (edit_get_byte (edit, edit->curs1) == '\n' || edit_get_byte (edit, edit->curs1 - 2) == '\n')
1496 return;
1497 edit->force |= REDRAW_PAGE;
1498 edit_insert (edit, '\n');
1501 /* --------------------------------------------------------------------------------------------- */
1503 static void
1504 insert_spaces_tab (WEdit * edit, gboolean half)
1506 long i;
1508 edit_update_curs_col (edit);
1509 i = option_tab_spacing * space_width;
1510 if (half)
1511 i /= 2;
1512 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1513 while (i > 0)
1515 edit_insert (edit, ' ');
1516 i -= space_width;
1520 /* --------------------------------------------------------------------------------------------- */
1522 static inline void
1523 edit_tab_cmd (WEdit * edit)
1525 if (option_fake_half_tabs && is_in_indent (edit))
1527 /* insert a half tab (usually four spaces) unless there is a
1528 half tab already behind, then delete it and insert a
1529 full tab. */
1530 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1531 insert_spaces_tab (edit, TRUE);
1532 else
1534 int i;
1536 for (i = 1; i <= HALF_TAB_SIZE; i++)
1537 edit_backspace (edit, TRUE);
1538 edit_insert (edit, '\t');
1541 else if (option_fill_tabs_with_spaces)
1542 insert_spaces_tab (edit, FALSE);
1543 else
1544 edit_insert (edit, '\t');
1547 /* --------------------------------------------------------------------------------------------- */
1549 static void
1550 check_and_wrap_line (WEdit * edit)
1552 off_t curs;
1553 int c;
1555 if (!option_typewriter_wrap)
1556 return;
1557 edit_update_curs_col (edit);
1558 if (edit->curs_col < option_word_wrap_line_length)
1559 return;
1560 curs = edit->curs1;
1561 while (TRUE)
1563 curs--;
1564 c = edit_get_byte (edit, curs);
1565 if (c == '\n' || curs <= 0)
1567 edit_insert (edit, '\n');
1568 return;
1570 if (c == ' ' || c == '\t')
1572 off_t current = edit->curs1;
1573 edit_cursor_move (edit, curs - edit->curs1 + 1);
1574 edit_insert (edit, '\n');
1575 edit_cursor_move (edit, current - edit->curs1 + 1);
1576 return;
1581 /* --------------------------------------------------------------------------------------------- */
1582 /** this find the matching bracket in either direction, and sets edit->bracket
1584 * @param edit editor object
1585 * @param in_screen seach only on the current screen
1586 * @param furthest_bracket_search count of the bytes for search
1588 * @return position of the found bracket (-1 if no match)
1591 static off_t
1592 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1594 const char *const b = "{}{[][()(", *p;
1595 int i = 1, a, inc = -1, c, d, n = 0;
1596 unsigned long j = 0;
1597 off_t q;
1598 edit_update_curs_row (edit);
1599 c = edit_get_byte (edit, edit->curs1);
1600 p = strchr (b, c);
1601 /* no limit */
1602 if (!furthest_bracket_search)
1603 furthest_bracket_search--;
1604 /* not on a bracket at all */
1605 if (p == NULL)
1606 return -1;
1607 /* the matching bracket */
1608 d = p[1];
1609 /* going left or right? */
1610 if (strchr ("{[(", c))
1611 inc = 1;
1612 for (q = edit->curs1 + inc;; q += inc)
1614 /* out of buffer? */
1615 if (q >= edit->last_byte || q < 0)
1616 break;
1617 a = edit_get_byte (edit, q);
1618 /* don't want to eat CPU */
1619 if (j++ > furthest_bracket_search)
1620 break;
1621 /* out of screen? */
1622 if (in_screen)
1624 if (q < edit->start_display)
1625 break;
1626 /* count lines if searching downward */
1627 if (inc > 0 && a == '\n')
1628 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1629 break;
1631 /* count bracket depth */
1632 i += (a == c) - (a == d);
1633 /* return if bracket depth is zero */
1634 if (i == 0)
1635 return q;
1637 /* no match */
1638 return -1;
1641 /* --------------------------------------------------------------------------------------------- */
1643 static inline void
1644 edit_goto_matching_bracket (WEdit * edit)
1646 off_t q;
1648 q = edit_get_bracket (edit, 0, 0);
1649 if (q >= 0)
1651 edit->bracket = edit->curs1;
1652 edit->force |= REDRAW_PAGE;
1653 edit_cursor_move (edit, q - edit->curs1);
1657 /* --------------------------------------------------------------------------------------------- */
1659 static void
1660 edit_move_block_to_right (WEdit * edit)
1662 off_t start_mark, end_mark;
1663 long cur_bol, start_bol;
1665 if (eval_marks (edit, &start_mark, &end_mark))
1666 return;
1668 start_bol = edit_bol (edit, start_mark);
1669 cur_bol = edit_bol (edit, end_mark - 1);
1673 edit_cursor_move (edit, cur_bol - edit->curs1);
1674 if (!edit_line_is_blank (edit, edit->curs_line))
1676 if (option_fill_tabs_with_spaces)
1677 insert_spaces_tab (edit, option_fake_half_tabs);
1678 else
1679 edit_insert (edit, '\t');
1680 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1683 if (cur_bol == 0)
1684 break;
1686 cur_bol = edit_bol (edit, cur_bol - 1);
1688 while (cur_bol >= start_bol);
1690 edit->force |= REDRAW_PAGE;
1693 /* --------------------------------------------------------------------------------------------- */
1695 static void
1696 edit_move_block_to_left (WEdit * edit)
1698 off_t start_mark, end_mark;
1699 off_t cur_bol, start_bol;
1700 int i;
1702 if (eval_marks (edit, &start_mark, &end_mark))
1703 return;
1705 start_bol = edit_bol (edit, start_mark);
1706 cur_bol = edit_bol (edit, end_mark - 1);
1710 int del_tab_width;
1711 int next_char;
1713 edit_cursor_move (edit, cur_bol - edit->curs1);
1715 if (option_fake_half_tabs)
1716 del_tab_width = HALF_TAB_SIZE;
1717 else
1718 del_tab_width = option_tab_spacing;
1720 next_char = edit_get_byte (edit, edit->curs1);
1721 if (next_char == '\t')
1722 edit_delete (edit, TRUE);
1723 else if (next_char == ' ')
1724 for (i = 1; i <= del_tab_width; i++)
1726 if (next_char == ' ')
1727 edit_delete (edit, TRUE);
1728 next_char = edit_get_byte (edit, edit->curs1);
1731 if (cur_bol == 0)
1732 break;
1734 cur_bol = edit_bol (edit, cur_bol - 1);
1736 while (cur_bol >= start_bol);
1738 edit->force |= REDRAW_PAGE;
1741 /* --------------------------------------------------------------------------------------------- */
1743 * prints at the cursor
1744 * @return number of chars printed
1747 static size_t
1748 edit_print_string (WEdit * e, const char *s)
1750 size_t i = 0;
1752 while (s[i] != '\0')
1753 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1754 e->force |= REDRAW_COMPLETELY;
1755 edit_update_screen (e);
1756 return i;
1759 /* --------------------------------------------------------------------------------------------- */
1760 /*** public functions ****************************************************************************/
1761 /* --------------------------------------------------------------------------------------------- */
1763 /** User edit menu, like user menu (F2) but only in editor. */
1765 void
1766 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1768 char *block_file;
1769 int nomark;
1770 off_t curs;
1771 off_t start_mark, end_mark;
1772 struct stat status;
1773 vfs_path_t *block_file_vpath;
1775 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1776 block_file_vpath = vfs_path_from_str (block_file);
1777 curs = edit->curs1;
1778 nomark = eval_marks (edit, &start_mark, &end_mark);
1779 if (nomark == 0)
1780 edit_save_block (edit, block_file, start_mark, end_mark);
1782 /* run shell scripts from menu */
1783 if (user_menu_cmd (edit, menu_file, selected_entry)
1784 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1786 int rc = 0;
1787 FILE *fd;
1789 /* i.e. we have marked block */
1790 if (nomark == 0)
1791 rc = edit_block_delete_cmd (edit);
1793 if (rc == 0)
1795 off_t ins_len;
1797 ins_len = edit_insert_file (edit, block_file_vpath);
1798 if (nomark == 0 && ins_len > 0)
1799 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1801 /* truncate block file */
1802 fd = fopen (block_file, "w");
1803 if (fd != NULL)
1804 fclose (fd);
1806 g_free (block_file);
1807 vfs_path_free (block_file_vpath);
1809 edit_cursor_move (edit, curs - edit->curs1);
1810 edit->force |= REDRAW_PAGE;
1811 send_message (edit, NULL, MSG_DRAW, 0, NULL);
1814 /* --------------------------------------------------------------------------------------------- */
1817 edit_get_byte (const WEdit * edit, off_t byte_index)
1819 off_t p;
1821 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1822 return '\n';
1824 if (byte_index >= edit->curs1)
1826 p = edit->curs1 + edit->curs2 - byte_index - 1;
1827 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1830 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1833 /* --------------------------------------------------------------------------------------------- */
1835 #ifdef HAVE_CHARSET
1837 edit_get_utf (const WEdit * edit, off_t byte_index, int *char_width)
1839 gchar *str = NULL;
1840 int res = -1;
1841 gunichar ch;
1842 gchar *next_ch = NULL;
1843 int width = 0;
1844 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1846 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1848 *char_width = 0;
1849 return '\n';
1852 str = edit_get_byte_ptr (edit, byte_index);
1854 if (str == NULL)
1856 *char_width = 0;
1857 return 0;
1860 res = g_utf8_get_char_validated (str, -1);
1862 if (res < 0)
1864 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1865 int i;
1866 for (i = 0; i < UTF8_CHAR_LEN; i++)
1867 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1868 utf8_buf[UTF8_CHAR_LEN] = '\0';
1869 str = utf8_buf;
1870 res = g_utf8_get_char_validated (str, -1);
1873 if (res < 0)
1875 ch = *str;
1876 width = 0;
1878 else
1880 ch = res;
1881 /* Calculate UTF-8 char width */
1882 next_ch = g_utf8_next_char (str);
1883 if (next_ch)
1885 width = next_ch - str;
1887 else
1889 ch = 0;
1890 width = 0;
1893 *char_width = width;
1894 return ch;
1896 #endif
1898 /* --------------------------------------------------------------------------------------------- */
1900 char *
1901 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1903 int i;
1904 char *p, *writename;
1905 const vfs_path_element_t *path_element;
1907 i = edit_find_filter (filename_vpath);
1908 if (i < 0)
1909 return NULL;
1911 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1912 writename = name_quote (path_element->path, 0);
1913 p = g_strdup_printf (all_filters[i].write, writename);
1914 g_free (writename);
1915 return p;
1918 /* --------------------------------------------------------------------------------------------- */
1920 * @param edit editor object
1921 * @param f value of stream file
1922 * @return the length of the file
1925 off_t
1926 edit_write_stream (WEdit * edit, FILE * f)
1928 long i;
1930 if (edit->lb == LB_ASIS)
1932 for (i = 0; i < edit->last_byte; i++)
1933 if (fputc (edit_get_byte (edit, i), f) < 0)
1934 break;
1935 return i;
1938 /* change line breaks */
1939 for (i = 0; i < edit->last_byte; i++)
1941 unsigned char c = edit_get_byte (edit, i);
1943 if (!(c == '\n' || c == '\r'))
1945 /* not line break */
1946 if (fputc (c, f) < 0)
1947 return i;
1949 else
1950 { /* (c == '\n' || c == '\r') */
1951 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1953 switch (edit->lb)
1955 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1956 /* put one line break unconditionally */
1957 if (fputc ('\n', f) < 0)
1958 return i;
1960 i++; /* 2 chars are processed */
1962 if (c == '\r' && c1 == '\n')
1963 /* Windows line break; go to the next char */
1964 break;
1966 if (c == '\r' && c1 == '\r')
1968 /* two Macintosh line breaks; put second line break */
1969 if (fputc ('\n', f) < 0)
1970 return i;
1971 break;
1974 if (fputc (c1, f) < 0)
1975 return i;
1976 break;
1978 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1979 /* put one line break unconditionally */
1980 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1981 return i;
1983 if (c == '\r' && c1 == '\n')
1984 /* Windows line break; go to the next char */
1985 i++;
1986 break;
1988 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1989 /* put one line break unconditionally */
1990 if (fputc ('\r', f) < 0)
1991 return i;
1993 i++; /* 2 chars are processed */
1995 if (c == '\r' && c1 == '\n')
1996 /* Windows line break; go to the next char */
1997 break;
1999 if (c == '\n' && c1 == '\n')
2001 /* two Windows line breaks; put second line break */
2002 if (fputc ('\r', f) < 0)
2003 return i;
2004 break;
2007 if (fputc (c1, f) < 0)
2008 return i;
2009 break;
2010 case LB_ASIS: /* default without changes */
2011 break;
2016 return edit->last_byte;
2019 /* --------------------------------------------------------------------------------------------- */
2021 gboolean
2022 is_break_char (char c)
2024 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2027 /* --------------------------------------------------------------------------------------------- */
2029 char *
2030 edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len,
2031 gsize * cut)
2033 off_t word_start;
2034 long cut_len = 0;
2035 GString *match_expr;
2036 unsigned char *bufpos;
2037 int c1, c2;
2039 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2041 c1 = edit_get_byte (edit, word_start);
2042 c2 = edit_get_byte (edit, word_start - 1);
2044 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2045 break;
2048 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2049 match_expr = g_string_sized_new (16);
2053 c1 = edit_get_byte (edit, word_start + match_expr->len);
2054 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2055 g_string_append_c (match_expr, c1);
2057 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2059 *len = match_expr->len;
2060 *start = word_start;
2061 *cut = cut_len;
2063 return g_string_free (match_expr, FALSE);
2066 /* --------------------------------------------------------------------------------------------- */
2067 /** inserts a file at the cursor, returns count of inserted bytes on success */
2069 long
2070 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2072 char *p;
2073 off_t current;
2074 off_t ins_len = 0;
2076 p = edit_get_filter (filename_vpath);
2077 current = edit->curs1;
2079 if (p != NULL)
2081 FILE *f;
2083 f = (FILE *) popen (p, "r");
2084 if (f != NULL)
2086 edit_insert_stream (edit, f);
2088 /* Place cursor at the end of text selection */
2089 if (!option_cursor_after_inserted_block)
2091 ins_len = edit->curs1 - current;
2092 edit_cursor_move (edit, -ins_len);
2094 if (pclose (f) > 0)
2096 char *errmsg;
2098 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2099 edit_error_dialog (_("Error"), errmsg);
2100 g_free (errmsg);
2101 ins_len = -1;
2104 else
2106 char *errmsg;
2108 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2109 edit_error_dialog (_("Error"), errmsg);
2110 g_free (errmsg);
2111 ins_len = -1;
2113 g_free (p);
2115 else
2117 int file;
2118 off_t blocklen;
2119 int vertical_insertion = 0;
2120 char *buf;
2122 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2123 if (file == -1)
2124 return -1;
2126 buf = g_malloc0 (TEMP_BUF_LEN);
2127 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2128 if (blocklen > 0)
2130 /* if contain signature VERTICAL_MAGIC then it vertical block */
2131 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2132 vertical_insertion = 1;
2133 else
2134 mc_lseek (file, 0, SEEK_SET);
2137 if (vertical_insertion)
2139 off_t mark1, mark2;
2140 long c1, c2;
2142 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2143 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2145 /* highlight inserted text then not persistent blocks */
2146 if (!option_persistent_selections && edit->modified)
2148 if (!edit->column_highlight)
2149 edit_push_undo_action (edit, COLUMN_OFF);
2150 edit->column_highlight = 1;
2153 else
2155 off_t i;
2157 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2159 for (i = 0; i < blocklen; i++)
2160 edit_insert (edit, buf[i]);
2162 /* highlight inserted text then not persistent blocks */
2163 if (!option_persistent_selections && edit->modified)
2165 edit_set_markers (edit, edit->curs1, current, 0, 0);
2166 if (edit->column_highlight)
2167 edit_push_undo_action (edit, COLUMN_ON);
2168 edit->column_highlight = 0;
2171 /* Place cursor at the end of text selection */
2172 if (!option_cursor_after_inserted_block)
2174 ins_len = edit->curs1 - current;
2175 edit_cursor_move (edit, -ins_len);
2179 edit->force |= REDRAW_PAGE;
2180 g_free (buf);
2181 mc_close (file);
2182 if (blocklen != 0)
2183 ins_len = 0;
2186 return ins_len;
2189 /* --------------------------------------------------------------------------------------------- */
2191 * Fill in the edit structure. Return NULL on failure. Pass edit as
2192 * NULL to allocate a new structure.
2194 * If line is 0, try to restore saved position. Otherwise put the
2195 * cursor on that line and show it in the middle of the screen.
2198 WEdit *
2199 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2200 long line)
2202 gboolean to_free = FALSE;
2204 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2205 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2207 if (edit != NULL)
2208 edit_purge_widget (edit);
2209 else
2211 #ifdef ENABLE_NLS
2213 * Expand option_whole_chars_search by national letters using
2214 * current locale
2217 static char option_whole_chars_search_buf[256];
2219 if (option_whole_chars_search_buf != option_whole_chars_search)
2221 size_t i;
2222 size_t len = str_term_width1 (option_whole_chars_search);
2224 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2226 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2228 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2230 option_whole_chars_search_buf[len++] = i;
2234 option_whole_chars_search_buf[len] = 0;
2235 option_whole_chars_search = option_whole_chars_search_buf;
2237 #endif /* ENABLE_NLS */
2238 edit = g_malloc0 (sizeof (WEdit));
2239 to_free = TRUE;
2241 init_widget (WIDGET (edit), y, x, lines, cols, NULL, NULL);
2242 edit->fullscreen = TRUE;
2243 edit_save_size (edit);
2246 edit->drag_state = MCEDIT_DRAG_NORMAL;
2248 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2249 edit->stat1.st_uid = getuid ();
2250 edit->stat1.st_gid = getgid ();
2251 edit->stat1.st_mtime = 0;
2253 edit->over_col = 0;
2254 edit->bracket = -1;
2255 edit->force |= REDRAW_PAGE;
2257 /* set file name before load file */
2258 edit_set_filename (edit, filename_vpath);
2260 edit->undo_stack_size = START_STACK_SIZE;
2261 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2262 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2264 edit->redo_stack_size = START_STACK_SIZE;
2265 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2266 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2268 #ifdef HAVE_CHARSET
2269 edit->utf8 = FALSE;
2270 edit->converter = str_cnv_from_term;
2271 edit_set_codeset (edit);
2272 #endif
2274 if (!edit_load_file (edit))
2276 /* edit_load_file already gives an error message */
2277 if (to_free)
2278 g_free (edit);
2279 return NULL;
2282 edit->loading_done = 1;
2283 edit->modified = 0;
2284 edit->locked = 0;
2285 edit_load_syntax (edit, NULL, NULL);
2286 edit_get_syntax_color (edit, -1);
2288 /* load saved cursor position */
2289 if ((line == 0) && option_save_position)
2290 edit_load_position (edit);
2291 else
2293 if (line <= 0)
2294 line = 1;
2295 edit_move_display (edit, line - 1);
2296 edit_move_to_line (edit, line - 1);
2299 edit_load_macro_cmd (edit);
2301 return edit;
2304 /* --------------------------------------------------------------------------------------------- */
2306 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2307 gboolean
2308 edit_clean (WEdit * edit)
2310 int j = 0;
2312 if (edit == NULL)
2313 return FALSE;
2315 /* a stale lock, remove it */
2316 if (edit->locked)
2317 edit->locked = unlock_file (edit->filename_vpath);
2319 /* save cursor position */
2320 if (option_save_position)
2321 edit_save_position (edit);
2322 else if (edit->serialized_bookmarks != NULL)
2323 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2325 /* File specified on the mcedit command line and never saved */
2326 if (edit->delete_file)
2327 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2329 edit_free_syntax_rules (edit);
2330 book_mark_flush (edit, -1);
2331 for (; j <= MAXBUFF; j++)
2333 g_free (edit->buffers1[j]);
2334 g_free (edit->buffers2[j]);
2337 g_free (edit->undo_stack);
2338 g_free (edit->redo_stack);
2339 vfs_path_free (edit->filename_vpath);
2340 vfs_path_free (edit->dir_vpath);
2341 mc_search_free (edit->search);
2342 edit->search = NULL;
2344 #ifdef HAVE_CHARSET
2345 if (edit->converter != str_cnv_from_term)
2346 str_close_conv (edit->converter);
2347 #endif
2349 edit_purge_widget (edit);
2351 return TRUE;
2354 /* --------------------------------------------------------------------------------------------- */
2357 * Load a new file into the editor and set line. If it fails, preserve the old file.
2358 * To do it, allocate a new widget, initialize it and, if the new file
2359 * was loaded, copy the data to the old widget.
2361 * @return TRUE on success, FALSE on failure.
2363 gboolean
2364 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2366 Widget *w = WIDGET (edit);
2367 WEdit *e;
2369 int y = w->y;
2370 int x = w->x;
2371 int lines = w->lines;
2372 int columns = w->cols;
2374 e = g_malloc0 (sizeof (WEdit));
2375 *WIDGET (e) = *w;
2377 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2379 g_free (e);
2380 return FALSE;
2383 edit_clean (edit);
2384 memcpy (edit, e, sizeof (WEdit));
2385 g_free (e);
2387 return TRUE;
2390 /* --------------------------------------------------------------------------------------------- */
2392 #ifdef HAVE_CHARSET
2393 void
2394 edit_set_codeset (WEdit * edit)
2396 const char *cp_id;
2398 cp_id =
2399 get_codepage_id (mc_global.source_codepage >=
2400 0 ? mc_global.source_codepage : mc_global.display_codepage);
2402 if (cp_id != NULL)
2404 GIConv conv;
2405 conv = str_crt_conv_from (cp_id);
2406 if (conv != INVALID_CONV)
2408 if (edit->converter != str_cnv_from_term)
2409 str_close_conv (edit->converter);
2410 edit->converter = conv;
2414 if (cp_id != NULL)
2415 edit->utf8 = str_isutf8 (cp_id);
2417 #endif
2420 /* --------------------------------------------------------------------------------------------- */
2423 * Recording stack for undo:
2424 * The following is an implementation of a compressed stack. Identical
2425 * pushes are recorded by a negative prefix indicating the number of times the
2426 * same char was pushed. This saves space for repeated curs-left or curs-right
2427 * delete etc.
2429 * eg:
2431 * pushed: stored:
2434 * b a
2435 * b -3
2436 * b b
2437 * c --> -4
2438 * c c
2439 * c d
2443 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2444 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2445 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2446 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2447 * position.
2449 * The only way the cursor moves or the buffer is changed is through the routines:
2450 * insert, backspace, insert_ahead, delete, and cursor_move.
2451 * These record the reverse undo movements onto the stack each time they are
2452 * called.
2454 * Each key press results in a set of actions (insert; delete ...). So each time
2455 * a key is pressed the current position of start_display is pushed as
2456 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2457 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2458 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2462 * @param edit editor object
2463 * @param c code of the action
2466 void
2467 edit_push_undo_action (WEdit * edit, long c)
2469 unsigned long sp = edit->undo_stack_pointer;
2470 unsigned long spm1;
2471 long *t;
2473 /* first enlarge the stack if necessary */
2474 if (sp > edit->undo_stack_size - 10)
2475 { /* say */
2476 if (option_max_undo < 256)
2477 option_max_undo = 256;
2478 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2480 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2481 if (t)
2483 edit->undo_stack = t;
2484 edit->undo_stack_size <<= 1;
2485 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2489 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2490 if (edit->undo_stack_disable)
2492 edit_push_redo_action (edit, KEY_PRESS);
2493 edit_push_redo_action (edit, c);
2494 return;
2497 if (edit->redo_stack_reset)
2498 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2500 if (edit->undo_stack_bottom != sp
2501 && spm1 != edit->undo_stack_bottom
2502 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2504 int d;
2505 if (edit->undo_stack[spm1] < 0)
2507 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2508 if (d == c && edit->undo_stack[spm1] > -1000000000)
2510 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2511 edit->undo_stack[spm1]--;
2512 return;
2515 else
2517 d = edit->undo_stack[spm1];
2518 if (d == c)
2520 if (c >= KEY_PRESS)
2521 return; /* --> no need to push multiple do-nothings */
2522 edit->undo_stack[sp] = -2;
2523 goto check_bottom;
2527 edit->undo_stack[sp] = c;
2529 check_bottom:
2530 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2532 /* if the sp wraps round and catches the undo_stack_bottom then erase
2533 * the first set of actions on the stack to make space - by moving
2534 * undo_stack_bottom forward one "key press" */
2535 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2536 if ((unsigned long) c == edit->undo_stack_bottom ||
2537 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2540 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2542 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2543 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2545 /*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: */
2546 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2547 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2549 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2553 void
2554 edit_push_redo_action (WEdit * edit, long c)
2556 unsigned long sp = edit->redo_stack_pointer;
2557 unsigned long spm1;
2558 long *t;
2559 /* first enlarge the stack if necessary */
2560 if (sp > edit->redo_stack_size - 10)
2561 { /* say */
2562 if (option_max_undo < 256)
2563 option_max_undo = 256;
2564 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2566 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2567 if (t)
2569 edit->redo_stack = t;
2570 edit->redo_stack_size <<= 1;
2571 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2575 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2577 if (edit->redo_stack_bottom != sp
2578 && spm1 != edit->redo_stack_bottom
2579 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2581 int d;
2582 if (edit->redo_stack[spm1] < 0)
2584 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2585 if (d == c && edit->redo_stack[spm1] > -1000000000)
2587 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2588 edit->redo_stack[spm1]--;
2589 return;
2592 else
2594 d = edit->redo_stack[spm1];
2595 if (d == c)
2597 if (c >= KEY_PRESS)
2598 return; /* --> no need to push multiple do-nothings */
2599 edit->redo_stack[sp] = -2;
2600 goto redo_check_bottom;
2604 edit->redo_stack[sp] = c;
2606 redo_check_bottom:
2607 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2609 /* if the sp wraps round and catches the redo_stack_bottom then erase
2610 * the first set of actions on the stack to make space - by moving
2611 * redo_stack_bottom forward one "key press" */
2612 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2613 if ((unsigned long) c == edit->redo_stack_bottom ||
2614 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2617 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2619 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2620 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2623 * If a single key produced enough pushes to wrap all the way round then
2624 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2625 * The stack is then initialised:
2628 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2629 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2630 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2633 /* --------------------------------------------------------------------------------------------- */
2635 Basic low level single character buffer alterations and movements at the cursor.
2636 Returns char passed over, inserted or removed.
2639 void
2640 edit_insert (WEdit * edit, int c)
2642 /* check if file has grown to large */
2643 if (edit->last_byte >= SIZE_LIMIT)
2644 return;
2646 /* first we must update the position of the display window */
2647 if (edit->curs1 < edit->start_display)
2649 edit->start_display++;
2650 if (c == '\n')
2651 edit->start_line++;
2654 /* Mark file as modified, unless the file hasn't been fully loaded */
2655 if (edit->loading_done)
2656 edit_modification (edit);
2658 /* now we must update some info on the file and check if a redraw is required */
2659 if (c == '\n')
2661 book_mark_inc (edit, edit->curs_line);
2662 edit->curs_line++;
2663 edit->total_lines++;
2664 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2667 /* save the reverse command onto the undo stack */
2668 /* ordinary char and not space */
2669 if (c > 32)
2670 edit_push_undo_action (edit, BACKSPACE);
2671 else
2672 edit_push_undo_action (edit, BACKSPACE_BR);
2673 /* update markers */
2674 edit->mark1 += (edit->mark1 > edit->curs1);
2675 edit->mark2 += (edit->mark2 > edit->curs1);
2676 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2678 /* add a new buffer if we've reached the end of the last one */
2679 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2680 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2682 /* perform the insertion */
2683 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2684 = (unsigned char) c;
2686 /* update file length */
2687 edit->last_byte++;
2689 /* update cursor position */
2690 edit->curs1++;
2693 /* --------------------------------------------------------------------------------------------- */
2694 /** same as edit_insert and move left */
2696 void
2697 edit_insert_ahead (WEdit * edit, int c)
2699 if (edit->last_byte >= SIZE_LIMIT)
2700 return;
2702 if (edit->curs1 < edit->start_display)
2704 edit->start_display++;
2705 if (c == '\n')
2706 edit->start_line++;
2708 edit_modification (edit);
2709 if (c == '\n')
2711 book_mark_inc (edit, edit->curs_line);
2712 edit->total_lines++;
2713 edit->force |= REDRAW_AFTER_CURSOR;
2715 /* ordinary char and not space */
2716 if (c > 32)
2717 edit_push_undo_action (edit, DELCHAR);
2718 else
2719 edit_push_undo_action (edit, DELCHAR_BR);
2721 edit->mark1 += (edit->mark1 >= edit->curs1);
2722 edit->mark2 += (edit->mark2 >= edit->curs1);
2723 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2725 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2726 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2727 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2728 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2730 edit->last_byte++;
2731 edit->curs2++;
2735 /* --------------------------------------------------------------------------------------------- */
2738 edit_delete (WEdit * edit, gboolean byte_delete)
2740 int p = 0;
2741 int cw = 1;
2742 int i;
2744 if (edit->curs2 == 0)
2745 return 0;
2747 #ifdef HAVE_CHARSET
2748 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2749 if (edit->utf8 && !byte_delete)
2751 edit_get_utf (edit, edit->curs1, &cw);
2752 if (cw < 1)
2753 cw = 1;
2755 #else
2756 (void) byte_delete;
2757 #endif
2759 if (edit->mark2 != edit->mark1)
2760 edit_push_markers (edit);
2762 for (i = 1; i <= cw; i++)
2764 if (edit->mark1 > edit->curs1)
2766 edit->mark1--;
2767 edit->end_mark_curs--;
2769 if (edit->mark2 > edit->curs1)
2770 edit->mark2--;
2771 if (edit->last_get_rule > edit->curs1)
2772 edit->last_get_rule--;
2774 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2775 ((edit->curs2 -
2776 1) & M_EDIT_BUF_SIZE) - 1];
2778 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2780 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2781 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2783 edit->last_byte--;
2784 edit->curs2--;
2785 edit_push_undo_action (edit, p + 256);
2788 edit_modification (edit);
2789 if (p == '\n')
2791 book_mark_dec (edit, edit->curs_line);
2792 edit->total_lines--;
2793 edit->force |= REDRAW_AFTER_CURSOR;
2795 if (edit->curs1 < edit->start_display)
2797 edit->start_display--;
2798 if (p == '\n')
2799 edit->start_line--;
2802 return p;
2805 /* --------------------------------------------------------------------------------------------- */
2808 edit_backspace (WEdit * edit, gboolean byte_delete)
2810 int p = 0;
2811 int cw = 1;
2812 int i;
2814 if (edit->curs1 == 0)
2815 return 0;
2817 if (edit->mark2 != edit->mark1)
2818 edit_push_markers (edit);
2820 #ifdef HAVE_CHARSET
2821 if (edit->utf8 && !byte_delete)
2823 edit_get_prev_utf (edit, edit->curs1, &cw);
2824 if (cw < 1)
2825 cw = 1;
2827 #else
2828 (void) byte_delete;
2829 #endif
2831 for (i = 1; i <= cw; i++)
2833 if (edit->mark1 >= edit->curs1)
2835 edit->mark1--;
2836 edit->end_mark_curs--;
2838 if (edit->mark2 >= edit->curs1)
2839 edit->mark2--;
2840 if (edit->last_get_rule >= edit->curs1)
2841 edit->last_get_rule--;
2843 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2844 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2845 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2847 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2848 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2850 edit->last_byte--;
2851 edit->curs1--;
2852 edit_push_undo_action (edit, p);
2854 edit_modification (edit);
2855 if (p == '\n')
2857 book_mark_dec (edit, edit->curs_line);
2858 edit->curs_line--;
2859 edit->total_lines--;
2860 edit->force |= REDRAW_AFTER_CURSOR;
2863 if (edit->curs1 < edit->start_display)
2865 edit->start_display--;
2866 if (p == '\n')
2867 edit->start_line--;
2870 return p;
2873 /* --------------------------------------------------------------------------------------------- */
2874 /** moves the cursor right or left: increment positive or negative respectively */
2876 void
2877 edit_cursor_move (WEdit * edit, off_t increment)
2879 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2880 int c;
2882 if (increment < 0)
2884 for (; increment < 0; increment++)
2886 if (edit->curs1 == 0)
2887 return;
2889 edit_push_undo_action (edit, CURS_RIGHT);
2891 c = edit_get_byte (edit, edit->curs1 - 1);
2892 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2893 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2894 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2895 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2896 edit->curs2++;
2897 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2898 1) & M_EDIT_BUF_SIZE];
2899 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2901 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2902 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2904 edit->curs1--;
2905 if (c == '\n')
2907 edit->curs_line--;
2908 edit->force |= REDRAW_LINE_BELOW;
2913 else
2915 for (; increment > 0; increment--)
2917 if (edit->curs2 == 0)
2918 return;
2920 edit_push_undo_action (edit, CURS_LEFT);
2922 c = edit_get_byte (edit, edit->curs1);
2923 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2924 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2925 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2926 edit->curs1++;
2927 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2928 ((edit->curs2 -
2929 1) & M_EDIT_BUF_SIZE) - 1];
2930 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2932 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2933 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2935 edit->curs2--;
2936 if (c == '\n')
2938 edit->curs_line++;
2939 edit->force |= REDRAW_LINE_ABOVE;
2945 /* These functions return positions relative to lines */
2947 /* --------------------------------------------------------------------------------------------- */
2948 /** returns index of last char on line + 1 */
2950 off_t
2951 edit_eol (const WEdit * edit, off_t current)
2953 if (current >= edit->last_byte)
2954 return edit->last_byte;
2956 for (; edit_get_byte (edit, current) != '\n'; current++)
2959 return current;
2962 /* --------------------------------------------------------------------------------------------- */
2963 /** returns index of first char on line */
2965 off_t
2966 edit_bol (const WEdit * edit, off_t current)
2968 if (current <= 0)
2969 return 0;
2971 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
2974 return current;
2977 /* --------------------------------------------------------------------------------------------- */
2979 long
2980 edit_count_lines (const WEdit * edit, off_t current, off_t upto)
2982 long lines = 0;
2984 if (upto > edit->last_byte)
2985 upto = edit->last_byte;
2986 if (current < 0)
2987 current = 0;
2988 while (current < upto)
2989 if (edit_get_byte (edit, current++) == '\n')
2990 lines++;
2991 return lines;
2994 /* --------------------------------------------------------------------------------------------- */
2995 /* If lines is zero this returns the count of lines from current to upto. */
2996 /* If upto is zero returns index of lines forward current. */
2998 off_t
2999 edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto)
3001 if (upto != 0)
3003 return (off_t) edit_count_lines (edit, current, upto);
3005 else
3007 long next;
3008 if (lines < 0)
3009 lines = 0;
3010 while (lines-- != 0)
3012 next = edit_eol (edit, current) + 1;
3013 if (next > edit->last_byte)
3014 break;
3015 else
3016 current = next;
3018 return current;
3022 /* --------------------------------------------------------------------------------------------- */
3023 /** Returns offset of 'lines' lines up from current */
3025 off_t
3026 edit_move_backward (const WEdit * edit, off_t current, long lines)
3028 if (lines < 0)
3029 lines = 0;
3030 current = edit_bol (edit, current);
3031 while (lines-- != 0 && current != 0)
3032 current = edit_bol (edit, current - 1);
3033 return current;
3036 /* --------------------------------------------------------------------------------------------- */
3037 /* If cols is zero this returns the count of columns from current to upto. */
3038 /* If upto is zero returns index of cols across from current. */
3040 off_t
3041 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
3043 off_t p, q;
3044 long col;
3046 if (upto != 0)
3048 q = upto;
3049 cols = -10;
3051 else
3052 q = edit->last_byte + 2;
3054 for (col = 0, p = current; p < q; p++)
3056 int c, orig_c;
3058 if (cols != -10)
3060 if (col == cols)
3061 return p;
3062 if (col > cols)
3063 return p - 1;
3066 orig_c = c = edit_get_byte (edit, p);
3068 #ifdef HAVE_CHARSET
3069 if (edit->utf8)
3071 int utf_ch;
3072 int cw = 1;
3074 utf_ch = edit_get_utf (edit, p, &cw);
3075 if (mc_global.utf8_display)
3077 if (cw > 1)
3078 col -= cw - 1;
3079 if (g_unichar_iswide (utf_ch))
3080 col++;
3082 else if (cw > 1 && g_unichar_isprint (utf_ch))
3083 col -= cw - 1;
3086 c = convert_to_display_c (c);
3087 #endif
3089 if (c == '\n')
3090 return (upto != 0 ? (off_t) col : p);
3091 if (c == '\t')
3092 col += TAB_SIZE - col % TAB_SIZE;
3093 else if ((c < 32 || c == 127) && (orig_c == c
3094 #ifdef HAVE_CHARSET
3095 || (!mc_global.utf8_display && !edit->utf8)
3096 #endif
3098 /* '\r' is shown as ^M, so we must advance 2 characters */
3099 /* Caret notation for control characters */
3100 col += 2;
3101 else
3102 col++;
3104 return (off_t) col;
3107 /* --------------------------------------------------------------------------------------------- */
3108 /** returns the current column position of the cursor */
3110 long
3111 edit_get_col (const WEdit * edit)
3113 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3116 /* --------------------------------------------------------------------------------------------- */
3117 /* Scrolling functions */
3118 /* --------------------------------------------------------------------------------------------- */
3120 void
3121 edit_update_curs_row (WEdit * edit)
3123 edit->curs_row = edit->curs_line - edit->start_line;
3126 /* --------------------------------------------------------------------------------------------- */
3128 void
3129 edit_update_curs_col (WEdit * edit)
3131 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3134 /* --------------------------------------------------------------------------------------------- */
3136 long
3137 edit_get_curs_col (const WEdit * edit)
3139 return edit->curs_col;
3142 /* --------------------------------------------------------------------------------------------- */
3143 /** moves the display start position up by i lines */
3145 void
3146 edit_scroll_upward (WEdit * edit, long i)
3148 long lines_above = edit->start_line;
3150 if (i > lines_above)
3151 i = lines_above;
3152 if (i != 0)
3154 edit->start_line -= i;
3155 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3156 edit->force |= REDRAW_PAGE;
3157 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3159 edit_update_curs_row (edit);
3163 /* --------------------------------------------------------------------------------------------- */
3165 void
3166 edit_scroll_downward (WEdit * edit, long i)
3168 long lines_below;
3170 lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1);
3171 if (lines_below > 0)
3173 if (i > lines_below)
3174 i = lines_below;
3175 edit->start_line += i;
3176 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3177 edit->force |= REDRAW_PAGE;
3178 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3180 edit_update_curs_row (edit);
3183 /* --------------------------------------------------------------------------------------------- */
3185 void
3186 edit_scroll_right (WEdit * edit, long i)
3188 edit->force |= REDRAW_PAGE;
3189 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3190 edit->start_col -= i;
3193 /* --------------------------------------------------------------------------------------------- */
3195 void
3196 edit_scroll_left (WEdit * edit, long i)
3198 if (edit->start_col)
3200 edit->start_col += i;
3201 if (edit->start_col > 0)
3202 edit->start_col = 0;
3203 edit->force |= REDRAW_PAGE;
3204 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3208 /* --------------------------------------------------------------------------------------------- */
3209 /* high level cursor movement commands */
3210 /* --------------------------------------------------------------------------------------------- */
3212 void
3213 edit_move_to_prev_col (WEdit * edit, off_t p)
3215 long prev = edit->prev_col;
3216 long over = edit->over_col;
3218 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3220 if (option_cursor_beyond_eol)
3222 long line_len;
3224 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3225 edit_eol (edit, edit->curs1));
3226 if (line_len < prev + edit->over_col)
3228 edit->over_col = prev + over - line_len;
3229 edit->prev_col = line_len;
3230 edit->curs_col = line_len;
3232 else
3234 edit->curs_col = prev + over;
3235 edit->prev_col = edit->curs_col;
3236 edit->over_col = 0;
3239 else
3241 edit->over_col = 0;
3242 if (option_fake_half_tabs && is_in_indent (edit))
3244 edit_update_curs_col (edit);
3245 if (space_width != 0 && edit->curs_col % (HALF_TAB_SIZE * space_width) != 0)
3247 int q;
3249 q = edit->curs_col;
3250 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3251 p = edit_bol (edit, edit->curs1);
3252 edit_cursor_move (edit,
3253 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
3254 if (!left_of_four_spaces (edit))
3255 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3261 /* --------------------------------------------------------------------------------------------- */
3262 /** check whether line in editor is blank or not
3264 * @param edit editor object
3265 * @param line number of line
3267 * @return TRUE if line in blank, FALSE otherwise
3270 gboolean
3271 edit_line_is_blank (WEdit * edit, long line)
3273 return is_blank (edit, edit_find_line (edit, line));
3276 /* --------------------------------------------------------------------------------------------- */
3277 /** move cursor to line 'line' */
3279 void
3280 edit_move_to_line (WEdit * e, long line)
3282 if (line < e->curs_line)
3283 edit_move_up (e, e->curs_line - line, 0);
3284 else
3285 edit_move_down (e, line - e->curs_line, 0);
3286 edit_scroll_screen_over_cursor (e);
3289 /* --------------------------------------------------------------------------------------------- */
3290 /** scroll window so that first visible line is 'line' */
3292 void
3293 edit_move_display (WEdit * e, long line)
3295 if (line < e->start_line)
3296 edit_scroll_upward (e, e->start_line - line);
3297 else
3298 edit_scroll_downward (e, line - e->start_line);
3301 /* --------------------------------------------------------------------------------------------- */
3302 /** save markers onto undo stack */
3304 void
3305 edit_push_markers (WEdit * edit)
3307 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3308 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3309 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3312 /* --------------------------------------------------------------------------------------------- */
3314 void
3315 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3317 edit->mark1 = m1;
3318 edit->mark2 = m2;
3319 edit->column1 = c1;
3320 edit->column2 = c2;
3324 /* --------------------------------------------------------------------------------------------- */
3325 /** highlight marker toggle */
3327 void
3328 edit_mark_cmd (WEdit * edit, gboolean unmark)
3330 edit_push_markers (edit);
3331 if (unmark)
3333 edit_set_markers (edit, 0, 0, 0, 0);
3334 edit->force |= REDRAW_PAGE;
3336 else if (edit->mark2 >= 0)
3338 edit->end_mark_curs = -1;
3339 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3340 edit->curs_col + edit->over_col);
3341 edit->force |= REDRAW_PAGE;
3343 else
3345 edit->end_mark_curs = edit->curs1;
3346 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3347 edit->curs_col + edit->over_col);
3351 /* --------------------------------------------------------------------------------------------- */
3352 /** highlight the word under cursor */
3354 void
3355 edit_mark_current_word_cmd (WEdit * edit)
3357 long pos;
3359 for (pos = edit->curs1; pos != 0; pos--)
3361 int c1, c2;
3363 c1 = edit_get_byte (edit, pos);
3364 c2 = edit_get_byte (edit, pos - 1);
3365 if (!isspace (c1) && isspace (c2))
3366 break;
3367 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3368 break;
3370 edit->mark1 = pos;
3372 for (; pos < edit->last_byte; pos++)
3374 int c1, c2;
3376 c1 = edit_get_byte (edit, pos);
3377 c2 = edit_get_byte (edit, pos + 1);
3378 if (!isspace (c1) && isspace (c2))
3379 break;
3380 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3381 break;
3383 edit->mark2 = min (pos + 1, edit->last_byte);
3385 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3388 /* --------------------------------------------------------------------------------------------- */
3390 void
3391 edit_mark_current_line_cmd (WEdit * edit)
3393 long pos = edit->curs1;
3395 edit->mark1 = edit_bol (edit, pos);
3396 edit->mark2 = edit_eol (edit, pos);
3398 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3401 /* --------------------------------------------------------------------------------------------- */
3403 void
3404 edit_delete_line (WEdit * edit)
3407 * Delete right part of the line.
3408 * Note that edit_get_byte() returns '\n' when byte position is
3409 * beyond EOF.
3411 while (edit_get_byte (edit, edit->curs1) != '\n')
3412 (void) edit_delete (edit, TRUE);
3415 * Delete '\n' char.
3416 * Note that edit_delete() will not corrupt anything if called while
3417 * cursor position is EOF.
3419 (void) edit_delete (edit, TRUE);
3422 * Delete left part of the line.
3423 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3425 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3426 (void) edit_backspace (edit, TRUE);
3429 /* --------------------------------------------------------------------------------------------- */
3431 long
3432 edit_indent_width (const WEdit * edit, off_t p)
3434 off_t q = p;
3436 /* move to the end of the leading whitespace of the line */
3437 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3438 q++;
3439 /* count the number of columns of indentation */
3440 return (long) edit_move_forward3 (edit, p, 0, q);
3443 /* --------------------------------------------------------------------------------------------- */
3445 void
3446 edit_insert_indent (WEdit * edit, int indent)
3448 if (!option_fill_tabs_with_spaces)
3450 while (indent >= TAB_SIZE)
3452 edit_insert (edit, '\t');
3453 indent -= TAB_SIZE;
3456 while (indent-- > 0)
3457 edit_insert (edit, ' ');
3460 /* --------------------------------------------------------------------------------------------- */
3462 void
3463 edit_push_key_press (WEdit * edit)
3465 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3466 if (edit->mark2 == -1)
3468 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3469 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3473 /* --------------------------------------------------------------------------------------------- */
3475 void
3476 edit_find_bracket (WEdit * edit)
3478 edit->bracket = edit_get_bracket (edit, 1, 10000);
3479 if (last_bracket != edit->bracket)
3480 edit->force |= REDRAW_PAGE;
3481 last_bracket = edit->bracket;
3484 /* --------------------------------------------------------------------------------------------- */
3486 * This executes a command as though the user initiated it through a key
3487 * press. Callback with MSG_KEY as a message calls this after
3488 * translating the key press. This function can be used to pass any
3489 * command to the editor. Note that the screen wouldn't update
3490 * automatically. Either of command or char_for_insertion must be
3491 * passed as -1. Commands are executed, and char_for_insertion is
3492 * inserted at the cursor.
3495 void
3496 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3498 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3499 || (macro_index < 0
3500 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3502 macro_index = 0;
3503 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3504 return;
3506 if (macro_index != -1)
3508 edit->force |= REDRAW_COMPLETELY;
3509 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3511 edit_store_macro_cmd (edit);
3512 macro_index = -1;
3513 return;
3515 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3517 edit_repeat_macro_cmd (edit);
3518 macro_index = -1;
3519 return;
3523 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3525 record_macro_buf[macro_index].action = command;
3526 record_macro_buf[macro_index++].ch = char_for_insertion;
3528 /* record the beginning of a set of editing actions initiated by a key press */
3529 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3530 edit_push_key_press (edit);
3532 edit_execute_cmd (edit, command, char_for_insertion);
3533 if (edit->column_highlight)
3534 edit->force |= REDRAW_PAGE;
3537 /* --------------------------------------------------------------------------------------------- */
3539 This executes a command at a lower level than macro recording.
3540 It also does not push a key_press onto the undo stack. This means
3541 that if it is called many times, a single undo command will undo
3542 all of them. It also does not check for the Undo command.
3544 void
3545 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3547 Widget *w = WIDGET (edit);
3549 if (command == CK_WindowFullscreen)
3551 edit_toggle_fullscreen (edit);
3552 return;
3555 /* handle window state */
3556 if (edit_handle_move_resize (edit, command))
3557 return;
3559 edit->force |= REDRAW_LINE;
3561 /* The next key press will unhighlight the found string, so update
3562 * the whole page */
3563 if (edit->found_len || edit->column_highlight)
3564 edit->force |= REDRAW_PAGE;
3566 switch (command)
3568 /* a mark command with shift-arrow */
3569 case CK_MarkLeft:
3570 case CK_MarkRight:
3571 case CK_MarkToWordBegin:
3572 case CK_MarkToWordEnd:
3573 case CK_MarkToHome:
3574 case CK_MarkToEnd:
3575 case CK_MarkUp:
3576 case CK_MarkDown:
3577 case CK_MarkPageUp:
3578 case CK_MarkPageDown:
3579 case CK_MarkToFileBegin:
3580 case CK_MarkToFileEnd:
3581 case CK_MarkToPageBegin:
3582 case CK_MarkToPageEnd:
3583 case CK_MarkScrollUp:
3584 case CK_MarkScrollDown:
3585 case CK_MarkParagraphUp:
3586 case CK_MarkParagraphDown:
3587 /* a mark command with alt-arrow */
3588 case CK_MarkColumnPageUp:
3589 case CK_MarkColumnPageDown:
3590 case CK_MarkColumnLeft:
3591 case CK_MarkColumnRight:
3592 case CK_MarkColumnUp:
3593 case CK_MarkColumnDown:
3594 case CK_MarkColumnScrollUp:
3595 case CK_MarkColumnScrollDown:
3596 case CK_MarkColumnParagraphUp:
3597 case CK_MarkColumnParagraphDown:
3598 edit->column_highlight = 0;
3599 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3601 edit_mark_cmd (edit, TRUE); /* clear */
3602 edit_mark_cmd (edit, FALSE); /* marking on */
3604 edit->highlight = 1;
3605 break;
3607 /* any other command */
3608 default:
3609 if (edit->highlight)
3610 edit_mark_cmd (edit, FALSE); /* clear */
3611 edit->highlight = 0;
3614 /* first check for undo */
3615 if (command == CK_Undo)
3617 edit->redo_stack_reset = 0;
3618 edit_group_undo (edit);
3619 edit->found_len = 0;
3620 edit->prev_col = edit_get_col (edit);
3621 edit->search_start = edit->curs1;
3622 return;
3624 /* check for redo */
3625 if (command == CK_Redo)
3627 edit->redo_stack_reset = 0;
3628 edit_do_redo (edit);
3629 edit->found_len = 0;
3630 edit->prev_col = edit_get_col (edit);
3631 edit->search_start = edit->curs1;
3632 return;
3635 edit->redo_stack_reset = 1;
3637 /* An ordinary key press */
3638 if (char_for_insertion >= 0)
3640 /* if non persistent selection and text selected */
3641 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3642 edit_block_delete_cmd (edit);
3644 if (edit->overwrite)
3646 /* remove char only one time, after input first byte, multibyte chars */
3647 #ifdef HAVE_CHARSET
3648 if (!mc_global.utf8_display || edit->charpoint == 0)
3649 #endif
3650 if (edit_get_byte (edit, edit->curs1) != '\n')
3652 edit_delete (edit, FALSE);
3654 if (option_cursor_beyond_eol && edit->over_col > 0)
3655 edit_insert_over (edit);
3656 #ifdef HAVE_CHARSET
3657 if (char_for_insertion > 255 && !mc_global.utf8_display)
3659 unsigned char str[6 + 1];
3660 size_t i = 0;
3661 int res;
3663 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3664 if (res == 0)
3666 str[0] = '.';
3667 str[1] = '\0';
3669 else
3671 str[res] = '\0';
3673 while (str[i] != 0 && i <= 6)
3675 char_for_insertion = str[i];
3676 edit_insert (edit, char_for_insertion);
3677 i++;
3680 else
3681 #endif
3682 edit_insert (edit, char_for_insertion);
3684 if (option_auto_para_formatting)
3686 format_paragraph (edit, 0);
3687 edit->force |= REDRAW_PAGE;
3689 else
3690 check_and_wrap_line (edit);
3691 edit->found_len = 0;
3692 edit->prev_col = edit_get_col (edit);
3693 edit->search_start = edit->curs1;
3694 edit_find_bracket (edit);
3695 return;
3698 switch (command)
3700 case CK_TopOnScreen:
3701 case CK_BottomOnScreen:
3702 case CK_Top:
3703 case CK_Bottom:
3704 case CK_PageUp:
3705 case CK_PageDown:
3706 case CK_Home:
3707 case CK_End:
3708 case CK_Up:
3709 case CK_Down:
3710 case CK_Left:
3711 case CK_Right:
3712 case CK_WordLeft:
3713 case CK_WordRight:
3714 if (!option_persistent_selections && edit->mark2 >= 0)
3716 if (edit->column_highlight)
3717 edit_push_undo_action (edit, COLUMN_ON);
3718 edit->column_highlight = 0;
3719 edit_mark_cmd (edit, TRUE);
3723 switch (command)
3725 case CK_TopOnScreen:
3726 case CK_BottomOnScreen:
3727 case CK_MarkToPageBegin:
3728 case CK_MarkToPageEnd:
3729 case CK_Up:
3730 case CK_Down:
3731 case CK_WordLeft:
3732 case CK_WordRight:
3733 case CK_MarkToWordBegin:
3734 case CK_MarkToWordEnd:
3735 case CK_MarkUp:
3736 case CK_MarkDown:
3737 case CK_MarkColumnUp:
3738 case CK_MarkColumnDown:
3739 if (edit->mark2 == -1)
3740 break; /*marking is following the cursor: may need to highlight a whole line */
3741 case CK_Left:
3742 case CK_Right:
3743 case CK_MarkLeft:
3744 case CK_MarkRight:
3745 edit->force |= REDRAW_CHAR_ONLY;
3748 /* basic cursor key commands */
3749 switch (command)
3751 case CK_BackSpace:
3752 /* if non persistent selection and text selected */
3753 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3754 edit_block_delete_cmd (edit);
3755 else if (option_cursor_beyond_eol && edit->over_col > 0)
3756 edit->over_col--;
3757 else if (option_backspace_through_tabs && is_in_indent (edit))
3759 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3760 edit_backspace (edit, TRUE);
3762 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3764 int i;
3766 for (i = 0; i < HALF_TAB_SIZE; i++)
3767 edit_backspace (edit, TRUE);
3769 else
3770 edit_backspace (edit, FALSE);
3771 break;
3772 case CK_Delete:
3773 /* if non persistent selection and text selected */
3774 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3775 edit_block_delete_cmd (edit);
3776 else
3778 if (option_cursor_beyond_eol && edit->over_col > 0)
3779 edit_insert_over (edit);
3781 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3783 int i;
3785 for (i = 1; i <= HALF_TAB_SIZE; i++)
3786 edit_delete (edit, TRUE);
3788 else
3789 edit_delete (edit, FALSE);
3791 break;
3792 case CK_DeleteToWordBegin:
3793 edit->over_col = 0;
3794 edit_left_delete_word (edit);
3795 break;
3796 case CK_DeleteToWordEnd:
3797 if (option_cursor_beyond_eol && edit->over_col > 0)
3798 edit_insert_over (edit);
3800 edit_right_delete_word (edit);
3801 break;
3802 case CK_DeleteLine:
3803 edit_delete_line (edit);
3804 break;
3805 case CK_DeleteToHome:
3806 edit_delete_to_line_begin (edit);
3807 break;
3808 case CK_DeleteToEnd:
3809 edit_delete_to_line_end (edit);
3810 break;
3811 case CK_Enter:
3812 edit->over_col = 0;
3813 if (option_auto_para_formatting)
3815 edit_double_newline (edit);
3816 if (option_return_does_auto_indent)
3817 edit_auto_indent (edit);
3818 format_paragraph (edit, 0);
3820 else
3822 edit_insert (edit, '\n');
3823 if (option_return_does_auto_indent)
3824 edit_auto_indent (edit);
3826 break;
3827 case CK_Return:
3828 edit_insert (edit, '\n');
3829 break;
3831 case CK_MarkColumnPageUp:
3832 edit->column_highlight = 1;
3833 case CK_PageUp:
3834 case CK_MarkPageUp:
3835 edit_move_up (edit, w->lines - 1, 1);
3836 break;
3837 case CK_MarkColumnPageDown:
3838 edit->column_highlight = 1;
3839 case CK_PageDown:
3840 case CK_MarkPageDown:
3841 edit_move_down (edit, w->lines - 1, 1);
3842 break;
3843 case CK_MarkColumnLeft:
3844 edit->column_highlight = 1;
3845 case CK_Left:
3846 case CK_MarkLeft:
3847 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3849 if (option_cursor_beyond_eol && edit->over_col > 0)
3850 edit->over_col--;
3851 else
3852 edit_cursor_move (edit, -HALF_TAB_SIZE);
3853 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3855 else
3856 edit_left_char_move_cmd (edit);
3857 break;
3858 case CK_MarkColumnRight:
3859 edit->column_highlight = 1;
3860 case CK_Right:
3861 case CK_MarkRight:
3862 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3864 edit_cursor_move (edit, HALF_TAB_SIZE);
3865 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3867 else
3868 edit_right_char_move_cmd (edit);
3869 break;
3870 case CK_TopOnScreen:
3871 case CK_MarkToPageBegin:
3872 edit_begin_page (edit);
3873 break;
3874 case CK_BottomOnScreen:
3875 case CK_MarkToPageEnd:
3876 edit_end_page (edit);
3877 break;
3878 case CK_WordLeft:
3879 case CK_MarkToWordBegin:
3880 edit->over_col = 0;
3881 edit_left_word_move_cmd (edit);
3882 break;
3883 case CK_WordRight:
3884 case CK_MarkToWordEnd:
3885 edit->over_col = 0;
3886 edit_right_word_move_cmd (edit);
3887 break;
3888 case CK_MarkColumnUp:
3889 edit->column_highlight = 1;
3890 case CK_Up:
3891 case CK_MarkUp:
3892 edit_move_up (edit, 1, 0);
3893 break;
3894 case CK_MarkColumnDown:
3895 edit->column_highlight = 1;
3896 case CK_Down:
3897 case CK_MarkDown:
3898 edit_move_down (edit, 1, 0);
3899 break;
3900 case CK_MarkColumnParagraphUp:
3901 edit->column_highlight = 1;
3902 case CK_ParagraphUp:
3903 case CK_MarkParagraphUp:
3904 edit_move_up_paragraph (edit, 0);
3905 break;
3906 case CK_MarkColumnParagraphDown:
3907 edit->column_highlight = 1;
3908 case CK_ParagraphDown:
3909 case CK_MarkParagraphDown:
3910 edit_move_down_paragraph (edit, 0);
3911 break;
3912 case CK_MarkColumnScrollUp:
3913 edit->column_highlight = 1;
3914 case CK_ScrollUp:
3915 case CK_MarkScrollUp:
3916 edit_move_up (edit, 1, 1);
3917 break;
3918 case CK_MarkColumnScrollDown:
3919 edit->column_highlight = 1;
3920 case CK_ScrollDown:
3921 case CK_MarkScrollDown:
3922 edit_move_down (edit, 1, 1);
3923 break;
3924 case CK_Home:
3925 case CK_MarkToHome:
3926 edit_cursor_to_bol (edit);
3927 break;
3928 case CK_End:
3929 case CK_MarkToEnd:
3930 edit_cursor_to_eol (edit);
3931 break;
3932 case CK_Tab:
3933 /* if text marked shift block */
3934 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3936 if (edit->mark2 < 0)
3937 edit_mark_cmd (edit, FALSE);
3938 edit_move_block_to_right (edit);
3940 else
3942 if (option_cursor_beyond_eol)
3943 edit_insert_over (edit);
3944 edit_tab_cmd (edit);
3945 if (option_auto_para_formatting)
3947 format_paragraph (edit, 0);
3948 edit->force |= REDRAW_PAGE;
3950 else
3951 check_and_wrap_line (edit);
3953 break;
3955 case CK_InsertOverwrite:
3956 edit->overwrite = !edit->overwrite;
3957 break;
3959 case CK_Mark:
3960 if (edit->mark2 >= 0)
3962 if (edit->column_highlight)
3963 edit_push_undo_action (edit, COLUMN_ON);
3964 edit->column_highlight = 0;
3966 edit_mark_cmd (edit, FALSE);
3967 break;
3968 case CK_MarkColumn:
3969 if (!edit->column_highlight)
3970 edit_push_undo_action (edit, COLUMN_OFF);
3971 edit->column_highlight = 1;
3972 edit_mark_cmd (edit, FALSE);
3973 break;
3974 case CK_MarkAll:
3975 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3976 edit->force |= REDRAW_PAGE;
3977 break;
3978 case CK_Unmark:
3979 if (edit->column_highlight)
3980 edit_push_undo_action (edit, COLUMN_ON);
3981 edit->column_highlight = 0;
3982 edit_mark_cmd (edit, TRUE);
3983 break;
3984 case CK_MarkWord:
3985 if (edit->column_highlight)
3986 edit_push_undo_action (edit, COLUMN_ON);
3987 edit->column_highlight = 0;
3988 edit_mark_current_word_cmd (edit);
3989 break;
3990 case CK_MarkLine:
3991 if (edit->column_highlight)
3992 edit_push_undo_action (edit, COLUMN_ON);
3993 edit->column_highlight = 0;
3994 edit_mark_current_line_cmd (edit);
3995 break;
3997 case CK_Bookmark:
3998 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3999 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4000 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4001 else
4002 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4003 break;
4004 case CK_BookmarkFlush:
4005 book_mark_flush (edit, BOOK_MARK_COLOR);
4006 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4007 edit->force |= REDRAW_PAGE;
4008 break;
4009 case CK_BookmarkNext:
4010 if (edit->book_mark != NULL)
4012 edit_book_mark_t *p;
4014 p = book_mark_find (edit, edit->curs_line);
4015 if (p->next != NULL)
4017 p = p->next;
4018 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4019 edit_move_display (edit, p->line - w->lines / 2);
4020 edit_move_to_line (edit, p->line);
4023 break;
4024 case CK_BookmarkPrev:
4025 if (edit->book_mark != NULL)
4027 edit_book_mark_t *p;
4029 p = book_mark_find (edit, edit->curs_line);
4030 while (p->line == edit->curs_line)
4031 if (p->prev != NULL)
4032 p = p->prev;
4033 if (p->line >= 0)
4035 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4036 edit_move_display (edit, p->line - w->lines / 2);
4037 edit_move_to_line (edit, p->line);
4040 break;
4042 case CK_Top:
4043 case CK_MarkToFileBegin:
4044 edit_move_to_top (edit);
4045 break;
4046 case CK_Bottom:
4047 case CK_MarkToFileEnd:
4048 edit_move_to_bottom (edit);
4049 break;
4051 case CK_Copy:
4052 if (option_cursor_beyond_eol && edit->over_col > 0)
4053 edit_insert_over (edit);
4054 edit_block_copy_cmd (edit);
4055 break;
4056 case CK_Remove:
4057 edit_block_delete_cmd (edit);
4058 break;
4059 case CK_Move:
4060 edit_block_move_cmd (edit);
4061 break;
4063 case CK_BlockShiftLeft:
4064 if (edit->mark1 != edit->mark2)
4065 edit_move_block_to_left (edit);
4066 break;
4067 case CK_BlockShiftRight:
4068 if (edit->mark1 != edit->mark2)
4069 edit_move_block_to_right (edit);
4070 break;
4071 case CK_Store:
4072 edit_copy_to_X_buf_cmd (edit);
4073 break;
4074 case CK_Cut:
4075 edit_cut_to_X_buf_cmd (edit);
4076 break;
4077 case CK_Paste:
4078 /* if non persistent selection and text selected */
4079 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4080 edit_block_delete_cmd (edit);
4081 if (option_cursor_beyond_eol && edit->over_col > 0)
4082 edit_insert_over (edit);
4083 edit_paste_from_X_buf_cmd (edit);
4084 if (!option_persistent_selections && edit->mark2 >= 0)
4086 if (edit->column_highlight)
4087 edit_push_undo_action (edit, COLUMN_ON);
4088 edit->column_highlight = 0;
4089 edit_mark_cmd (edit, TRUE);
4091 break;
4092 case CK_History:
4093 edit_paste_from_history (edit);
4094 break;
4096 case CK_SaveAs:
4097 edit_save_as_cmd (edit);
4098 break;
4099 case CK_Save:
4100 edit_save_confirm_cmd (edit);
4101 break;
4102 case CK_BlockSave:
4103 edit_save_block_cmd (edit);
4104 break;
4105 case CK_InsertFile:
4106 edit_insert_file_cmd (edit);
4107 break;
4109 case CK_FilePrev:
4110 edit_load_back_cmd (edit);
4111 break;
4112 case CK_FileNext:
4113 edit_load_forward_cmd (edit);
4114 break;
4116 case CK_SyntaxChoose:
4117 edit_syntax_dialog (edit);
4118 break;
4120 case CK_Search:
4121 edit_search_cmd (edit, FALSE);
4122 break;
4123 case CK_SearchContinue:
4124 edit_search_cmd (edit, TRUE);
4125 break;
4126 case CK_Replace:
4127 edit_replace_cmd (edit, 0);
4128 break;
4129 case CK_ReplaceContinue:
4130 edit_replace_cmd (edit, 1);
4131 break;
4132 case CK_Complete:
4133 /* if text marked shift block */
4134 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4135 edit_move_block_to_left (edit);
4136 else
4137 edit_complete_word_cmd (edit);
4138 break;
4139 case CK_Find:
4140 edit_get_match_keyword_cmd (edit);
4141 break;
4143 #ifdef HAVE_ASPELL
4144 case CK_SpellCheckCurrentWord:
4145 edit_suggest_current_word (edit);
4146 break;
4147 case CK_SpellCheck:
4148 edit_spellcheck_file (edit);
4149 break;
4150 case CK_SpellCheckSelectLang:
4151 edit_set_spell_lang ();
4152 break;
4153 #endif
4155 case CK_Date:
4157 char s[BUF_MEDIUM];
4158 /* fool gcc to prevent a Y2K warning */
4159 char time_format[] = "_c";
4160 time_format[0] = '%';
4162 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4163 edit_print_string (edit, s);
4164 edit->force |= REDRAW_PAGE;
4166 break;
4167 case CK_Goto:
4168 edit_goto_cmd (edit);
4169 break;
4170 case CK_ParagraphFormat:
4171 format_paragraph (edit, 1);
4172 edit->force |= REDRAW_PAGE;
4173 break;
4174 case CK_MacroDelete:
4175 edit_delete_macro_cmd (edit);
4176 break;
4177 case CK_MatchBracket:
4178 edit_goto_matching_bracket (edit);
4179 break;
4180 case CK_UserMenu:
4181 user_menu (edit, NULL, -1);
4182 break;
4183 case CK_Sort:
4184 edit_sort_cmd (edit);
4185 break;
4186 case CK_ExternalCommand:
4187 edit_ext_cmd (edit);
4188 break;
4189 case CK_Mail:
4190 edit_mail_dialog (edit);
4191 break;
4192 #ifdef HAVE_CHARSET
4193 case CK_SelectCodepage:
4194 edit_select_codepage_cmd (edit);
4195 break;
4196 #endif
4197 case CK_InsertLiteral:
4198 edit_insert_literal_cmd (edit);
4199 break;
4200 case CK_MacroStartStopRecord:
4201 edit_begin_end_macro_cmd (edit);
4202 break;
4203 case CK_RepeatStartStopRecord:
4204 edit_begin_end_repeat_cmd (edit);
4205 break;
4206 case CK_ExtendedKeyMap:
4207 edit->extmod = TRUE;
4208 break;
4209 default:
4210 break;
4213 /* CK_PipeBlock */
4214 if ((command / CK_PipeBlock (0)) == 1)
4215 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4217 /* keys which must set the col position, and the search vars */
4218 switch (command)
4220 case CK_Search:
4221 case CK_SearchContinue:
4222 case CK_Replace:
4223 case CK_ReplaceContinue:
4224 case CK_Complete:
4225 edit->prev_col = edit_get_col (edit);
4226 break;
4227 case CK_Up:
4228 case CK_MarkUp:
4229 case CK_MarkColumnUp:
4230 case CK_Down:
4231 case CK_MarkDown:
4232 case CK_MarkColumnDown:
4233 case CK_PageUp:
4234 case CK_MarkPageUp:
4235 case CK_MarkColumnPageUp:
4236 case CK_PageDown:
4237 case CK_MarkPageDown:
4238 case CK_MarkColumnPageDown:
4239 case CK_Top:
4240 case CK_MarkToFileBegin:
4241 case CK_Bottom:
4242 case CK_MarkToFileEnd:
4243 case CK_ParagraphUp:
4244 case CK_MarkParagraphUp:
4245 case CK_MarkColumnParagraphUp:
4246 case CK_ParagraphDown:
4247 case CK_MarkParagraphDown:
4248 case CK_MarkColumnParagraphDown:
4249 case CK_ScrollUp:
4250 case CK_MarkScrollUp:
4251 case CK_MarkColumnScrollUp:
4252 case CK_ScrollDown:
4253 case CK_MarkScrollDown:
4254 case CK_MarkColumnScrollDown:
4255 edit->search_start = edit->curs1;
4256 edit->found_len = 0;
4257 break;
4258 default:
4259 edit->found_len = 0;
4260 edit->prev_col = edit_get_col (edit);
4261 edit->search_start = edit->curs1;
4263 edit_find_bracket (edit);
4265 if (option_auto_para_formatting)
4267 switch (command)
4269 case CK_BackSpace:
4270 case CK_Delete:
4271 case CK_DeleteToWordBegin:
4272 case CK_DeleteToWordEnd:
4273 case CK_DeleteToHome:
4274 case CK_DeleteToEnd:
4275 format_paragraph (edit, 0);
4276 edit->force |= REDRAW_PAGE;
4281 /* --------------------------------------------------------------------------------------------- */
4283 void
4284 edit_stack_init (void)
4286 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4288 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4289 edit_history_moveto[edit_stack_iterator].line = -1;
4292 edit_stack_iterator = 0;
4295 /* --------------------------------------------------------------------------------------------- */
4297 void
4298 edit_stack_free (void)
4300 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4301 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4304 /* --------------------------------------------------------------------------------------------- */
4305 /** move i lines */
4307 void
4308 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4310 edit_move_updown (edit, i, do_scroll, TRUE);
4313 /* --------------------------------------------------------------------------------------------- */
4314 /** move i lines */
4316 void
4317 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4319 edit_move_updown (edit, i, do_scroll, FALSE);
4322 /* --------------------------------------------------------------------------------------------- */