(insert_spaces_tab): fix floating point exception.
[midnight-commander.git] / src / editor / edit.c
blobb2ef4950fe2b34d89e2a06667cf5f5339cff3188
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 if (i != 0)
1514 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1515 while (i > 0)
1517 edit_insert (edit, ' ');
1518 i -= space_width;
1523 /* --------------------------------------------------------------------------------------------- */
1525 static inline void
1526 edit_tab_cmd (WEdit * edit)
1528 if (option_fake_half_tabs && is_in_indent (edit))
1530 /* insert a half tab (usually four spaces) unless there is a
1531 half tab already behind, then delete it and insert a
1532 full tab. */
1533 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1534 insert_spaces_tab (edit, TRUE);
1535 else
1537 int i;
1539 for (i = 1; i <= HALF_TAB_SIZE; i++)
1540 edit_backspace (edit, TRUE);
1541 edit_insert (edit, '\t');
1544 else if (option_fill_tabs_with_spaces)
1545 insert_spaces_tab (edit, FALSE);
1546 else
1547 edit_insert (edit, '\t');
1550 /* --------------------------------------------------------------------------------------------- */
1552 static void
1553 check_and_wrap_line (WEdit * edit)
1555 off_t curs;
1556 int c;
1558 if (!option_typewriter_wrap)
1559 return;
1560 edit_update_curs_col (edit);
1561 if (edit->curs_col < option_word_wrap_line_length)
1562 return;
1563 curs = edit->curs1;
1564 while (TRUE)
1566 curs--;
1567 c = edit_get_byte (edit, curs);
1568 if (c == '\n' || curs <= 0)
1570 edit_insert (edit, '\n');
1571 return;
1573 if (c == ' ' || c == '\t')
1575 off_t current = edit->curs1;
1576 edit_cursor_move (edit, curs - edit->curs1 + 1);
1577 edit_insert (edit, '\n');
1578 edit_cursor_move (edit, current - edit->curs1 + 1);
1579 return;
1584 /* --------------------------------------------------------------------------------------------- */
1585 /** this find the matching bracket in either direction, and sets edit->bracket
1587 * @param edit editor object
1588 * @param in_screen seach only on the current screen
1589 * @param furthest_bracket_search count of the bytes for search
1591 * @return position of the found bracket (-1 if no match)
1594 static off_t
1595 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1597 const char *const b = "{}{[][()(", *p;
1598 int i = 1, a, inc = -1, c, d, n = 0;
1599 unsigned long j = 0;
1600 off_t q;
1601 edit_update_curs_row (edit);
1602 c = edit_get_byte (edit, edit->curs1);
1603 p = strchr (b, c);
1604 /* no limit */
1605 if (!furthest_bracket_search)
1606 furthest_bracket_search--;
1607 /* not on a bracket at all */
1608 if (p == NULL)
1609 return -1;
1610 /* the matching bracket */
1611 d = p[1];
1612 /* going left or right? */
1613 if (strchr ("{[(", c))
1614 inc = 1;
1615 for (q = edit->curs1 + inc;; q += inc)
1617 /* out of buffer? */
1618 if (q >= edit->last_byte || q < 0)
1619 break;
1620 a = edit_get_byte (edit, q);
1621 /* don't want to eat CPU */
1622 if (j++ > furthest_bracket_search)
1623 break;
1624 /* out of screen? */
1625 if (in_screen)
1627 if (q < edit->start_display)
1628 break;
1629 /* count lines if searching downward */
1630 if (inc > 0 && a == '\n')
1631 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1632 break;
1634 /* count bracket depth */
1635 i += (a == c) - (a == d);
1636 /* return if bracket depth is zero */
1637 if (i == 0)
1638 return q;
1640 /* no match */
1641 return -1;
1644 /* --------------------------------------------------------------------------------------------- */
1646 static inline void
1647 edit_goto_matching_bracket (WEdit * edit)
1649 off_t q;
1651 q = edit_get_bracket (edit, 0, 0);
1652 if (q >= 0)
1654 edit->bracket = edit->curs1;
1655 edit->force |= REDRAW_PAGE;
1656 edit_cursor_move (edit, q - edit->curs1);
1660 /* --------------------------------------------------------------------------------------------- */
1662 static void
1663 edit_move_block_to_right (WEdit * edit)
1665 off_t start_mark, end_mark;
1666 long cur_bol, start_bol;
1668 if (eval_marks (edit, &start_mark, &end_mark))
1669 return;
1671 start_bol = edit_bol (edit, start_mark);
1672 cur_bol = edit_bol (edit, end_mark - 1);
1676 edit_cursor_move (edit, cur_bol - edit->curs1);
1677 if (!edit_line_is_blank (edit, edit->curs_line))
1679 if (option_fill_tabs_with_spaces)
1680 insert_spaces_tab (edit, option_fake_half_tabs);
1681 else
1682 edit_insert (edit, '\t');
1683 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1686 if (cur_bol == 0)
1687 break;
1689 cur_bol = edit_bol (edit, cur_bol - 1);
1691 while (cur_bol >= start_bol);
1693 edit->force |= REDRAW_PAGE;
1696 /* --------------------------------------------------------------------------------------------- */
1698 static void
1699 edit_move_block_to_left (WEdit * edit)
1701 off_t start_mark, end_mark;
1702 off_t cur_bol, start_bol;
1703 int i;
1705 if (eval_marks (edit, &start_mark, &end_mark))
1706 return;
1708 start_bol = edit_bol (edit, start_mark);
1709 cur_bol = edit_bol (edit, end_mark - 1);
1713 int del_tab_width;
1714 int next_char;
1716 edit_cursor_move (edit, cur_bol - edit->curs1);
1718 if (option_fake_half_tabs)
1719 del_tab_width = HALF_TAB_SIZE;
1720 else
1721 del_tab_width = option_tab_spacing;
1723 next_char = edit_get_byte (edit, edit->curs1);
1724 if (next_char == '\t')
1725 edit_delete (edit, TRUE);
1726 else if (next_char == ' ')
1727 for (i = 1; i <= del_tab_width; i++)
1729 if (next_char == ' ')
1730 edit_delete (edit, TRUE);
1731 next_char = edit_get_byte (edit, edit->curs1);
1734 if (cur_bol == 0)
1735 break;
1737 cur_bol = edit_bol (edit, cur_bol - 1);
1739 while (cur_bol >= start_bol);
1741 edit->force |= REDRAW_PAGE;
1744 /* --------------------------------------------------------------------------------------------- */
1746 * prints at the cursor
1747 * @return number of chars printed
1750 static size_t
1751 edit_print_string (WEdit * e, const char *s)
1753 size_t i = 0;
1755 while (s[i] != '\0')
1756 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1757 e->force |= REDRAW_COMPLETELY;
1758 edit_update_screen (e);
1759 return i;
1762 /* --------------------------------------------------------------------------------------------- */
1763 /*** public functions ****************************************************************************/
1764 /* --------------------------------------------------------------------------------------------- */
1766 /** User edit menu, like user menu (F2) but only in editor. */
1768 void
1769 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1771 char *block_file;
1772 int nomark;
1773 off_t curs;
1774 off_t start_mark, end_mark;
1775 struct stat status;
1776 vfs_path_t *block_file_vpath;
1778 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1779 block_file_vpath = vfs_path_from_str (block_file);
1780 curs = edit->curs1;
1781 nomark = eval_marks (edit, &start_mark, &end_mark);
1782 if (nomark == 0)
1783 edit_save_block (edit, block_file, start_mark, end_mark);
1785 /* run shell scripts from menu */
1786 if (user_menu_cmd (edit, menu_file, selected_entry)
1787 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1789 int rc = 0;
1790 FILE *fd;
1792 /* i.e. we have marked block */
1793 if (nomark == 0)
1794 rc = edit_block_delete_cmd (edit);
1796 if (rc == 0)
1798 off_t ins_len;
1800 ins_len = edit_insert_file (edit, block_file_vpath);
1801 if (nomark == 0 && ins_len > 0)
1802 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1804 /* truncate block file */
1805 fd = fopen (block_file, "w");
1806 if (fd != NULL)
1807 fclose (fd);
1809 g_free (block_file);
1810 vfs_path_free (block_file_vpath);
1812 edit_cursor_move (edit, curs - edit->curs1);
1813 edit->force |= REDRAW_PAGE;
1814 send_message (edit, NULL, MSG_DRAW, 0, NULL);
1817 /* --------------------------------------------------------------------------------------------- */
1820 edit_get_byte (const WEdit * edit, off_t byte_index)
1822 off_t p;
1824 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1825 return '\n';
1827 if (byte_index >= edit->curs1)
1829 p = edit->curs1 + edit->curs2 - byte_index - 1;
1830 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1833 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1836 /* --------------------------------------------------------------------------------------------- */
1838 #ifdef HAVE_CHARSET
1840 edit_get_utf (const WEdit * edit, off_t byte_index, int *char_width)
1842 gchar *str = NULL;
1843 int res = -1;
1844 gunichar ch;
1845 gchar *next_ch = NULL;
1846 int width = 0;
1847 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1849 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1851 *char_width = 0;
1852 return '\n';
1855 str = edit_get_byte_ptr (edit, byte_index);
1857 if (str == NULL)
1859 *char_width = 0;
1860 return 0;
1863 res = g_utf8_get_char_validated (str, -1);
1865 if (res < 0)
1867 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1868 int i;
1869 for (i = 0; i < UTF8_CHAR_LEN; i++)
1870 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1871 utf8_buf[UTF8_CHAR_LEN] = '\0';
1872 str = utf8_buf;
1873 res = g_utf8_get_char_validated (str, -1);
1876 if (res < 0)
1878 ch = *str;
1879 width = 0;
1881 else
1883 ch = res;
1884 /* Calculate UTF-8 char width */
1885 next_ch = g_utf8_next_char (str);
1886 if (next_ch)
1888 width = next_ch - str;
1890 else
1892 ch = 0;
1893 width = 0;
1896 *char_width = width;
1897 return ch;
1899 #endif
1901 /* --------------------------------------------------------------------------------------------- */
1903 char *
1904 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1906 int i;
1907 char *p, *writename;
1908 const vfs_path_element_t *path_element;
1910 i = edit_find_filter (filename_vpath);
1911 if (i < 0)
1912 return NULL;
1914 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1915 writename = name_quote (path_element->path, 0);
1916 p = g_strdup_printf (all_filters[i].write, writename);
1917 g_free (writename);
1918 return p;
1921 /* --------------------------------------------------------------------------------------------- */
1923 * @param edit editor object
1924 * @param f value of stream file
1925 * @return the length of the file
1928 off_t
1929 edit_write_stream (WEdit * edit, FILE * f)
1931 long i;
1933 if (edit->lb == LB_ASIS)
1935 for (i = 0; i < edit->last_byte; i++)
1936 if (fputc (edit_get_byte (edit, i), f) < 0)
1937 break;
1938 return i;
1941 /* change line breaks */
1942 for (i = 0; i < edit->last_byte; i++)
1944 unsigned char c = edit_get_byte (edit, i);
1946 if (!(c == '\n' || c == '\r'))
1948 /* not line break */
1949 if (fputc (c, f) < 0)
1950 return i;
1952 else
1953 { /* (c == '\n' || c == '\r') */
1954 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1956 switch (edit->lb)
1958 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1959 /* put one line break unconditionally */
1960 if (fputc ('\n', f) < 0)
1961 return i;
1963 i++; /* 2 chars are processed */
1965 if (c == '\r' && c1 == '\n')
1966 /* Windows line break; go to the next char */
1967 break;
1969 if (c == '\r' && c1 == '\r')
1971 /* two Macintosh line breaks; put second line break */
1972 if (fputc ('\n', f) < 0)
1973 return i;
1974 break;
1977 if (fputc (c1, f) < 0)
1978 return i;
1979 break;
1981 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1982 /* put one line break unconditionally */
1983 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1984 return i;
1986 if (c == '\r' && c1 == '\n')
1987 /* Windows line break; go to the next char */
1988 i++;
1989 break;
1991 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1992 /* put one line break unconditionally */
1993 if (fputc ('\r', f) < 0)
1994 return i;
1996 i++; /* 2 chars are processed */
1998 if (c == '\r' && c1 == '\n')
1999 /* Windows line break; go to the next char */
2000 break;
2002 if (c == '\n' && c1 == '\n')
2004 /* two Windows line breaks; put second line break */
2005 if (fputc ('\r', f) < 0)
2006 return i;
2007 break;
2010 if (fputc (c1, f) < 0)
2011 return i;
2012 break;
2013 case LB_ASIS: /* default without changes */
2014 break;
2019 return edit->last_byte;
2022 /* --------------------------------------------------------------------------------------------- */
2024 gboolean
2025 is_break_char (char c)
2027 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2030 /* --------------------------------------------------------------------------------------------- */
2032 char *
2033 edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len,
2034 gsize * cut)
2036 off_t word_start;
2037 long cut_len = 0;
2038 GString *match_expr;
2039 int c1, c2;
2041 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2043 c1 = edit_get_byte (edit, word_start);
2044 c2 = edit_get_byte (edit, word_start - 1);
2046 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2047 break;
2050 match_expr = g_string_sized_new (16);
2054 c1 = edit_get_byte (edit, word_start + match_expr->len);
2055 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2056 g_string_append_c (match_expr, c1);
2058 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2060 *len = match_expr->len;
2061 *start = word_start;
2062 *cut = cut_len;
2064 return g_string_free (match_expr, FALSE);
2067 /* --------------------------------------------------------------------------------------------- */
2068 /** inserts a file at the cursor, returns count of inserted bytes on success */
2070 long
2071 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2073 char *p;
2074 off_t current;
2075 off_t ins_len = 0;
2077 p = edit_get_filter (filename_vpath);
2078 current = edit->curs1;
2080 if (p != NULL)
2082 FILE *f;
2084 f = (FILE *) popen (p, "r");
2085 if (f != NULL)
2087 edit_insert_stream (edit, f);
2089 /* Place cursor at the end of text selection */
2090 if (!option_cursor_after_inserted_block)
2092 ins_len = edit->curs1 - current;
2093 edit_cursor_move (edit, -ins_len);
2095 if (pclose (f) > 0)
2097 char *errmsg;
2099 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2100 edit_error_dialog (_("Error"), errmsg);
2101 g_free (errmsg);
2102 ins_len = -1;
2105 else
2107 char *errmsg;
2109 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2110 edit_error_dialog (_("Error"), errmsg);
2111 g_free (errmsg);
2112 ins_len = -1;
2114 g_free (p);
2116 else
2118 int file;
2119 off_t blocklen;
2120 int vertical_insertion = 0;
2121 char *buf;
2123 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2124 if (file == -1)
2125 return -1;
2127 buf = g_malloc0 (TEMP_BUF_LEN);
2128 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2129 if (blocklen > 0)
2131 /* if contain signature VERTICAL_MAGIC then it vertical block */
2132 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2133 vertical_insertion = 1;
2134 else
2135 mc_lseek (file, 0, SEEK_SET);
2138 if (vertical_insertion)
2140 off_t mark1, mark2;
2141 long c1, c2;
2143 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2144 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2146 /* highlight inserted text then not persistent blocks */
2147 if (!option_persistent_selections && edit->modified)
2149 if (!edit->column_highlight)
2150 edit_push_undo_action (edit, COLUMN_OFF);
2151 edit->column_highlight = 1;
2154 else
2156 off_t i;
2158 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2160 for (i = 0; i < blocklen; i++)
2161 edit_insert (edit, buf[i]);
2163 /* highlight inserted text then not persistent blocks */
2164 if (!option_persistent_selections && edit->modified)
2166 edit_set_markers (edit, edit->curs1, current, 0, 0);
2167 if (edit->column_highlight)
2168 edit_push_undo_action (edit, COLUMN_ON);
2169 edit->column_highlight = 0;
2172 /* Place cursor at the end of text selection */
2173 if (!option_cursor_after_inserted_block)
2175 ins_len = edit->curs1 - current;
2176 edit_cursor_move (edit, -ins_len);
2180 edit->force |= REDRAW_PAGE;
2181 g_free (buf);
2182 mc_close (file);
2183 if (blocklen != 0)
2184 ins_len = 0;
2187 return ins_len;
2190 /* --------------------------------------------------------------------------------------------- */
2192 * Fill in the edit structure. Return NULL on failure. Pass edit as
2193 * NULL to allocate a new structure.
2195 * If line is 0, try to restore saved position. Otherwise put the
2196 * cursor on that line and show it in the middle of the screen.
2199 WEdit *
2200 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2201 long line)
2203 gboolean to_free = FALSE;
2205 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2206 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2208 if (edit != NULL)
2210 /* save some widget parameters */
2211 gboolean fullscreen = edit->fullscreen;
2212 int y_prev = edit->y_prev;
2213 int x_prev = edit->x_prev;
2214 int lines_prev = edit->lines_prev;
2215 int cols_prev = edit->cols_prev;
2217 edit_purge_widget (edit);
2219 /* restore saved parameters */
2220 edit->fullscreen = fullscreen;
2221 edit->y_prev = y_prev;
2222 edit->x_prev = x_prev;
2223 edit->lines_prev = lines_prev;
2224 edit->cols_prev = cols_prev;
2226 else
2228 #ifdef ENABLE_NLS
2230 * Expand option_whole_chars_search by national letters using
2231 * current locale
2234 static char option_whole_chars_search_buf[256];
2236 if (option_whole_chars_search_buf != option_whole_chars_search)
2238 size_t i;
2239 size_t len = str_term_width1 (option_whole_chars_search);
2241 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2243 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2245 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2247 option_whole_chars_search_buf[len++] = i;
2251 option_whole_chars_search_buf[len] = 0;
2252 option_whole_chars_search = option_whole_chars_search_buf;
2254 #endif /* ENABLE_NLS */
2255 edit = g_malloc0 (sizeof (WEdit));
2256 to_free = TRUE;
2258 init_widget (WIDGET (edit), y, x, lines, cols, NULL, NULL);
2259 edit->fullscreen = TRUE;
2260 edit_save_size (edit);
2263 edit->drag_state = MCEDIT_DRAG_NORMAL;
2265 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2266 edit->stat1.st_uid = getuid ();
2267 edit->stat1.st_gid = getgid ();
2268 edit->stat1.st_mtime = 0;
2270 edit->over_col = 0;
2271 edit->bracket = -1;
2272 edit->force |= REDRAW_PAGE;
2274 /* set file name before load file */
2275 edit_set_filename (edit, filename_vpath);
2277 edit->undo_stack_size = START_STACK_SIZE;
2278 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2279 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2281 edit->redo_stack_size = START_STACK_SIZE;
2282 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2283 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2285 #ifdef HAVE_CHARSET
2286 edit->utf8 = FALSE;
2287 edit->converter = str_cnv_from_term;
2288 edit_set_codeset (edit);
2289 #endif
2291 if (!edit_load_file (edit))
2293 /* edit_load_file already gives an error message */
2294 if (to_free)
2295 g_free (edit);
2296 return NULL;
2299 edit->loading_done = 1;
2300 edit->modified = 0;
2301 edit->locked = 0;
2302 edit_load_syntax (edit, NULL, NULL);
2303 edit_get_syntax_color (edit, -1);
2305 /* load saved cursor position */
2306 if ((line == 0) && option_save_position)
2307 edit_load_position (edit);
2308 else
2310 if (line <= 0)
2311 line = 1;
2312 edit_move_display (edit, line - 1);
2313 edit_move_to_line (edit, line - 1);
2316 edit_load_macro_cmd (edit);
2318 return edit;
2321 /* --------------------------------------------------------------------------------------------- */
2323 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2324 gboolean
2325 edit_clean (WEdit * edit)
2327 int j = 0;
2329 if (edit == NULL)
2330 return FALSE;
2332 /* a stale lock, remove it */
2333 if (edit->locked)
2334 edit->locked = unlock_file (edit->filename_vpath);
2336 /* save cursor position */
2337 if (option_save_position)
2338 edit_save_position (edit);
2339 else if (edit->serialized_bookmarks != NULL)
2340 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2342 /* File specified on the mcedit command line and never saved */
2343 if (edit->delete_file)
2344 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2346 edit_free_syntax_rules (edit);
2347 book_mark_flush (edit, -1);
2348 for (; j <= MAXBUFF; j++)
2350 g_free (edit->buffers1[j]);
2351 g_free (edit->buffers2[j]);
2354 g_free (edit->undo_stack);
2355 g_free (edit->redo_stack);
2356 vfs_path_free (edit->filename_vpath);
2357 vfs_path_free (edit->dir_vpath);
2358 mc_search_free (edit->search);
2359 edit->search = NULL;
2361 #ifdef HAVE_CHARSET
2362 if (edit->converter != str_cnv_from_term)
2363 str_close_conv (edit->converter);
2364 #endif
2366 edit_purge_widget (edit);
2368 return TRUE;
2371 /* --------------------------------------------------------------------------------------------- */
2374 * Load a new file into the editor and set line. If it fails, preserve the old file.
2375 * To do it, allocate a new widget, initialize it and, if the new file
2376 * was loaded, copy the data to the old widget.
2378 * @return TRUE on success, FALSE on failure.
2380 gboolean
2381 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2383 Widget *w = WIDGET (edit);
2384 WEdit *e;
2386 e = g_malloc0 (sizeof (WEdit));
2387 *WIDGET (e) = *w;
2388 /* save some widget parameters */
2389 e->fullscreen = edit->fullscreen;
2390 e->y_prev = edit->y_prev;
2391 e->x_prev = edit->x_prev;
2392 e->lines_prev = edit->lines_prev;
2393 e->cols_prev = edit->cols_prev;
2395 if (edit_init (e, w->y, w->x, w->lines, w->cols, filename_vpath, line) == NULL)
2397 g_free (e);
2398 return FALSE;
2401 edit_clean (edit);
2402 memcpy (edit, e, sizeof (WEdit));
2403 g_free (e);
2405 return TRUE;
2408 /* --------------------------------------------------------------------------------------------- */
2410 #ifdef HAVE_CHARSET
2411 void
2412 edit_set_codeset (WEdit * edit)
2414 const char *cp_id;
2416 cp_id =
2417 get_codepage_id (mc_global.source_codepage >=
2418 0 ? mc_global.source_codepage : mc_global.display_codepage);
2420 if (cp_id != NULL)
2422 GIConv conv;
2423 conv = str_crt_conv_from (cp_id);
2424 if (conv != INVALID_CONV)
2426 if (edit->converter != str_cnv_from_term)
2427 str_close_conv (edit->converter);
2428 edit->converter = conv;
2432 if (cp_id != NULL)
2433 edit->utf8 = str_isutf8 (cp_id);
2435 #endif
2438 /* --------------------------------------------------------------------------------------------- */
2441 * Recording stack for undo:
2442 * The following is an implementation of a compressed stack. Identical
2443 * pushes are recorded by a negative prefix indicating the number of times the
2444 * same char was pushed. This saves space for repeated curs-left or curs-right
2445 * delete etc.
2447 * eg:
2449 * pushed: stored:
2452 * b a
2453 * b -3
2454 * b b
2455 * c --> -4
2456 * c c
2457 * c d
2461 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2462 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2463 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2464 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2465 * position.
2467 * The only way the cursor moves or the buffer is changed is through the routines:
2468 * insert, backspace, insert_ahead, delete, and cursor_move.
2469 * These record the reverse undo movements onto the stack each time they are
2470 * called.
2472 * Each key press results in a set of actions (insert; delete ...). So each time
2473 * a key is pressed the current position of start_display is pushed as
2474 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2475 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2476 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2480 * @param edit editor object
2481 * @param c code of the action
2484 void
2485 edit_push_undo_action (WEdit * edit, long c)
2487 unsigned long sp = edit->undo_stack_pointer;
2488 unsigned long spm1;
2489 long *t;
2491 /* first enlarge the stack if necessary */
2492 if (sp > edit->undo_stack_size - 10)
2493 { /* say */
2494 if (option_max_undo < 256)
2495 option_max_undo = 256;
2496 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2498 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2499 if (t)
2501 edit->undo_stack = t;
2502 edit->undo_stack_size <<= 1;
2503 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2507 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2508 if (edit->undo_stack_disable)
2510 edit_push_redo_action (edit, KEY_PRESS);
2511 edit_push_redo_action (edit, c);
2512 return;
2515 if (edit->redo_stack_reset)
2516 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2518 if (edit->undo_stack_bottom != sp
2519 && spm1 != edit->undo_stack_bottom
2520 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2522 int d;
2523 if (edit->undo_stack[spm1] < 0)
2525 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2526 if (d == c && edit->undo_stack[spm1] > -1000000000)
2528 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2529 edit->undo_stack[spm1]--;
2530 return;
2533 else
2535 d = edit->undo_stack[spm1];
2536 if (d == c)
2538 if (c >= KEY_PRESS)
2539 return; /* --> no need to push multiple do-nothings */
2540 edit->undo_stack[sp] = -2;
2541 goto check_bottom;
2545 edit->undo_stack[sp] = c;
2547 check_bottom:
2548 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2550 /* if the sp wraps round and catches the undo_stack_bottom then erase
2551 * the first set of actions on the stack to make space - by moving
2552 * undo_stack_bottom forward one "key press" */
2553 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2554 if ((unsigned long) c == edit->undo_stack_bottom ||
2555 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2558 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2560 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2561 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2563 /*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: */
2564 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2565 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2567 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2571 void
2572 edit_push_redo_action (WEdit * edit, long c)
2574 unsigned long sp = edit->redo_stack_pointer;
2575 unsigned long spm1;
2576 long *t;
2577 /* first enlarge the stack if necessary */
2578 if (sp > edit->redo_stack_size - 10)
2579 { /* say */
2580 if (option_max_undo < 256)
2581 option_max_undo = 256;
2582 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2584 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2585 if (t)
2587 edit->redo_stack = t;
2588 edit->redo_stack_size <<= 1;
2589 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2593 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2595 if (edit->redo_stack_bottom != sp
2596 && spm1 != edit->redo_stack_bottom
2597 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2599 int d;
2600 if (edit->redo_stack[spm1] < 0)
2602 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2603 if (d == c && edit->redo_stack[spm1] > -1000000000)
2605 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2606 edit->redo_stack[spm1]--;
2607 return;
2610 else
2612 d = edit->redo_stack[spm1];
2613 if (d == c)
2615 if (c >= KEY_PRESS)
2616 return; /* --> no need to push multiple do-nothings */
2617 edit->redo_stack[sp] = -2;
2618 goto redo_check_bottom;
2622 edit->redo_stack[sp] = c;
2624 redo_check_bottom:
2625 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2627 /* if the sp wraps round and catches the redo_stack_bottom then erase
2628 * the first set of actions on the stack to make space - by moving
2629 * redo_stack_bottom forward one "key press" */
2630 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2631 if ((unsigned long) c == edit->redo_stack_bottom ||
2632 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2635 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2637 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2638 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2641 * If a single key produced enough pushes to wrap all the way round then
2642 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2643 * The stack is then initialised:
2646 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2647 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2648 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2651 /* --------------------------------------------------------------------------------------------- */
2653 Basic low level single character buffer alterations and movements at the cursor.
2654 Returns char passed over, inserted or removed.
2657 void
2658 edit_insert (WEdit * edit, int c)
2660 /* check if file has grown to large */
2661 if (edit->last_byte >= SIZE_LIMIT)
2662 return;
2664 /* first we must update the position of the display window */
2665 if (edit->curs1 < edit->start_display)
2667 edit->start_display++;
2668 if (c == '\n')
2669 edit->start_line++;
2672 /* Mark file as modified, unless the file hasn't been fully loaded */
2673 if (edit->loading_done)
2674 edit_modification (edit);
2676 /* now we must update some info on the file and check if a redraw is required */
2677 if (c == '\n')
2679 book_mark_inc (edit, edit->curs_line);
2680 edit->curs_line++;
2681 edit->total_lines++;
2682 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2685 /* save the reverse command onto the undo stack */
2686 /* ordinary char and not space */
2687 if (c > 32)
2688 edit_push_undo_action (edit, BACKSPACE);
2689 else
2690 edit_push_undo_action (edit, BACKSPACE_BR);
2691 /* update markers */
2692 edit->mark1 += (edit->mark1 > edit->curs1);
2693 edit->mark2 += (edit->mark2 > edit->curs1);
2694 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2696 /* add a new buffer if we've reached the end of the last one */
2697 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2698 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2700 /* perform the insertion */
2701 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2702 = (unsigned char) c;
2704 /* update file length */
2705 edit->last_byte++;
2707 /* update cursor position */
2708 edit->curs1++;
2711 /* --------------------------------------------------------------------------------------------- */
2712 /** same as edit_insert and move left */
2714 void
2715 edit_insert_ahead (WEdit * edit, int c)
2717 if (edit->last_byte >= SIZE_LIMIT)
2718 return;
2720 if (edit->curs1 < edit->start_display)
2722 edit->start_display++;
2723 if (c == '\n')
2724 edit->start_line++;
2726 edit_modification (edit);
2727 if (c == '\n')
2729 book_mark_inc (edit, edit->curs_line);
2730 edit->total_lines++;
2731 edit->force |= REDRAW_AFTER_CURSOR;
2733 /* ordinary char and not space */
2734 if (c > 32)
2735 edit_push_undo_action (edit, DELCHAR);
2736 else
2737 edit_push_undo_action (edit, DELCHAR_BR);
2739 edit->mark1 += (edit->mark1 >= edit->curs1);
2740 edit->mark2 += (edit->mark2 >= edit->curs1);
2741 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2743 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2744 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2745 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2746 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2748 edit->last_byte++;
2749 edit->curs2++;
2753 /* --------------------------------------------------------------------------------------------- */
2756 edit_delete (WEdit * edit, gboolean byte_delete)
2758 int p = 0;
2759 int cw = 1;
2760 int i;
2762 if (edit->curs2 == 0)
2763 return 0;
2765 #ifdef HAVE_CHARSET
2766 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2767 if (edit->utf8 && !byte_delete)
2769 edit_get_utf (edit, edit->curs1, &cw);
2770 if (cw < 1)
2771 cw = 1;
2773 #else
2774 (void) byte_delete;
2775 #endif
2777 if (edit->mark2 != edit->mark1)
2778 edit_push_markers (edit);
2780 for (i = 1; i <= cw; i++)
2782 if (edit->mark1 > edit->curs1)
2784 edit->mark1--;
2785 edit->end_mark_curs--;
2787 if (edit->mark2 > edit->curs1)
2788 edit->mark2--;
2789 if (edit->last_get_rule > edit->curs1)
2790 edit->last_get_rule--;
2792 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2793 ((edit->curs2 -
2794 1) & M_EDIT_BUF_SIZE) - 1];
2796 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2798 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2799 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2801 edit->last_byte--;
2802 edit->curs2--;
2803 edit_push_undo_action (edit, p + 256);
2806 edit_modification (edit);
2807 if (p == '\n')
2809 book_mark_dec (edit, edit->curs_line);
2810 edit->total_lines--;
2811 edit->force |= REDRAW_AFTER_CURSOR;
2813 if (edit->curs1 < edit->start_display)
2815 edit->start_display--;
2816 if (p == '\n')
2817 edit->start_line--;
2820 return p;
2823 /* --------------------------------------------------------------------------------------------- */
2826 edit_backspace (WEdit * edit, gboolean byte_delete)
2828 int p = 0;
2829 int cw = 1;
2830 int i;
2832 if (edit->curs1 == 0)
2833 return 0;
2835 if (edit->mark2 != edit->mark1)
2836 edit_push_markers (edit);
2838 #ifdef HAVE_CHARSET
2839 if (edit->utf8 && !byte_delete)
2841 edit_get_prev_utf (edit, edit->curs1, &cw);
2842 if (cw < 1)
2843 cw = 1;
2845 #else
2846 (void) byte_delete;
2847 #endif
2849 for (i = 1; i <= cw; i++)
2851 if (edit->mark1 >= edit->curs1)
2853 edit->mark1--;
2854 edit->end_mark_curs--;
2856 if (edit->mark2 >= edit->curs1)
2857 edit->mark2--;
2858 if (edit->last_get_rule >= edit->curs1)
2859 edit->last_get_rule--;
2861 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2862 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2863 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2865 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2866 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2868 edit->last_byte--;
2869 edit->curs1--;
2870 edit_push_undo_action (edit, p);
2872 edit_modification (edit);
2873 if (p == '\n')
2875 book_mark_dec (edit, edit->curs_line);
2876 edit->curs_line--;
2877 edit->total_lines--;
2878 edit->force |= REDRAW_AFTER_CURSOR;
2881 if (edit->curs1 < edit->start_display)
2883 edit->start_display--;
2884 if (p == '\n')
2885 edit->start_line--;
2888 return p;
2891 /* --------------------------------------------------------------------------------------------- */
2892 /** moves the cursor right or left: increment positive or negative respectively */
2894 void
2895 edit_cursor_move (WEdit * edit, off_t increment)
2897 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2898 int c;
2900 if (increment < 0)
2902 for (; increment < 0; increment++)
2904 if (edit->curs1 == 0)
2905 return;
2907 edit_push_undo_action (edit, CURS_RIGHT);
2909 c = edit_get_byte (edit, edit->curs1 - 1);
2910 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2911 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2912 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2913 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2914 edit->curs2++;
2915 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2916 1) & M_EDIT_BUF_SIZE];
2917 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2919 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2920 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2922 edit->curs1--;
2923 if (c == '\n')
2925 edit->curs_line--;
2926 edit->force |= REDRAW_LINE_BELOW;
2931 else
2933 for (; increment > 0; increment--)
2935 if (edit->curs2 == 0)
2936 return;
2938 edit_push_undo_action (edit, CURS_LEFT);
2940 c = edit_get_byte (edit, edit->curs1);
2941 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2942 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2943 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2944 edit->curs1++;
2945 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2946 ((edit->curs2 -
2947 1) & M_EDIT_BUF_SIZE) - 1];
2948 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2950 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2951 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2953 edit->curs2--;
2954 if (c == '\n')
2956 edit->curs_line++;
2957 edit->force |= REDRAW_LINE_ABOVE;
2963 /* These functions return positions relative to lines */
2965 /* --------------------------------------------------------------------------------------------- */
2966 /** returns index of last char on line + 1 */
2968 off_t
2969 edit_eol (const WEdit * edit, off_t current)
2971 if (current >= edit->last_byte)
2972 return edit->last_byte;
2974 for (; edit_get_byte (edit, current) != '\n'; current++)
2977 return current;
2980 /* --------------------------------------------------------------------------------------------- */
2981 /** returns index of first char on line */
2983 off_t
2984 edit_bol (const WEdit * edit, off_t current)
2986 if (current <= 0)
2987 return 0;
2989 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
2992 return current;
2995 /* --------------------------------------------------------------------------------------------- */
2997 long
2998 edit_count_lines (const WEdit * edit, off_t current, off_t upto)
3000 long lines = 0;
3002 if (upto > edit->last_byte)
3003 upto = edit->last_byte;
3004 if (current < 0)
3005 current = 0;
3006 while (current < upto)
3007 if (edit_get_byte (edit, current++) == '\n')
3008 lines++;
3009 return lines;
3012 /* --------------------------------------------------------------------------------------------- */
3013 /* If lines is zero this returns the count of lines from current to upto. */
3014 /* If upto is zero returns index of lines forward current. */
3016 off_t
3017 edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto)
3019 if (upto != 0)
3021 return (off_t) edit_count_lines (edit, current, upto);
3023 else
3025 long next;
3026 if (lines < 0)
3027 lines = 0;
3028 while (lines-- != 0)
3030 next = edit_eol (edit, current) + 1;
3031 if (next > edit->last_byte)
3032 break;
3033 else
3034 current = next;
3036 return current;
3040 /* --------------------------------------------------------------------------------------------- */
3041 /** Returns offset of 'lines' lines up from current */
3043 off_t
3044 edit_move_backward (const WEdit * edit, off_t current, long lines)
3046 if (lines < 0)
3047 lines = 0;
3048 current = edit_bol (edit, current);
3049 while (lines-- != 0 && current != 0)
3050 current = edit_bol (edit, current - 1);
3051 return current;
3054 /* --------------------------------------------------------------------------------------------- */
3055 /* If cols is zero this returns the count of columns from current to upto. */
3056 /* If upto is zero returns index of cols across from current. */
3058 off_t
3059 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
3061 off_t p, q;
3062 long col;
3064 if (upto != 0)
3066 q = upto;
3067 cols = -10;
3069 else
3070 q = edit->last_byte + 2;
3072 for (col = 0, p = current; p < q; p++)
3074 int c, orig_c;
3076 if (cols != -10)
3078 if (col == cols)
3079 return p;
3080 if (col > cols)
3081 return p - 1;
3084 orig_c = c = edit_get_byte (edit, p);
3086 #ifdef HAVE_CHARSET
3087 if (edit->utf8)
3089 int utf_ch;
3090 int cw = 1;
3092 utf_ch = edit_get_utf (edit, p, &cw);
3093 if (mc_global.utf8_display)
3095 if (cw > 1)
3096 col -= cw - 1;
3097 if (g_unichar_iswide (utf_ch))
3098 col++;
3100 else if (cw > 1 && g_unichar_isprint (utf_ch))
3101 col -= cw - 1;
3104 c = convert_to_display_c (c);
3105 #endif
3107 if (c == '\n')
3108 return (upto != 0 ? (off_t) col : p);
3109 if (c == '\t')
3110 col += TAB_SIZE - col % TAB_SIZE;
3111 else if ((c < 32 || c == 127) && (orig_c == c
3112 #ifdef HAVE_CHARSET
3113 || (!mc_global.utf8_display && !edit->utf8)
3114 #endif
3116 /* '\r' is shown as ^M, so we must advance 2 characters */
3117 /* Caret notation for control characters */
3118 col += 2;
3119 else
3120 col++;
3122 return (off_t) col;
3125 /* --------------------------------------------------------------------------------------------- */
3126 /** returns the current column position of the cursor */
3128 long
3129 edit_get_col (const WEdit * edit)
3131 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3134 /* --------------------------------------------------------------------------------------------- */
3135 /* Scrolling functions */
3136 /* --------------------------------------------------------------------------------------------- */
3138 void
3139 edit_update_curs_row (WEdit * edit)
3141 edit->curs_row = edit->curs_line - edit->start_line;
3144 /* --------------------------------------------------------------------------------------------- */
3146 void
3147 edit_update_curs_col (WEdit * edit)
3149 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3152 /* --------------------------------------------------------------------------------------------- */
3154 long
3155 edit_get_curs_col (const WEdit * edit)
3157 return edit->curs_col;
3160 /* --------------------------------------------------------------------------------------------- */
3161 /** moves the display start position up by i lines */
3163 void
3164 edit_scroll_upward (WEdit * edit, long i)
3166 long lines_above = edit->start_line;
3168 if (i > lines_above)
3169 i = lines_above;
3170 if (i != 0)
3172 edit->start_line -= i;
3173 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3174 edit->force |= REDRAW_PAGE;
3175 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3177 edit_update_curs_row (edit);
3181 /* --------------------------------------------------------------------------------------------- */
3183 void
3184 edit_scroll_downward (WEdit * edit, long i)
3186 long lines_below;
3188 lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1);
3189 if (lines_below > 0)
3191 if (i > lines_below)
3192 i = lines_below;
3193 edit->start_line += i;
3194 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3195 edit->force |= REDRAW_PAGE;
3196 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3198 edit_update_curs_row (edit);
3201 /* --------------------------------------------------------------------------------------------- */
3203 void
3204 edit_scroll_right (WEdit * edit, long i)
3206 edit->force |= REDRAW_PAGE;
3207 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3208 edit->start_col -= i;
3211 /* --------------------------------------------------------------------------------------------- */
3213 void
3214 edit_scroll_left (WEdit * edit, long i)
3216 if (edit->start_col)
3218 edit->start_col += i;
3219 if (edit->start_col > 0)
3220 edit->start_col = 0;
3221 edit->force |= REDRAW_PAGE;
3222 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3226 /* --------------------------------------------------------------------------------------------- */
3227 /* high level cursor movement commands */
3228 /* --------------------------------------------------------------------------------------------- */
3230 void
3231 edit_move_to_prev_col (WEdit * edit, off_t p)
3233 long prev = edit->prev_col;
3234 long over = edit->over_col;
3236 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3238 if (option_cursor_beyond_eol)
3240 long line_len;
3242 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3243 edit_eol (edit, edit->curs1));
3244 if (line_len < prev + edit->over_col)
3246 edit->over_col = prev + over - line_len;
3247 edit->prev_col = line_len;
3248 edit->curs_col = line_len;
3250 else
3252 edit->curs_col = prev + over;
3253 edit->prev_col = edit->curs_col;
3254 edit->over_col = 0;
3257 else
3259 edit->over_col = 0;
3260 if (option_fake_half_tabs && is_in_indent (edit))
3262 long fake_half_tabs;
3264 edit_update_curs_col (edit);
3266 fake_half_tabs = HALF_TAB_SIZE * space_width;
3267 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3269 int q;
3271 q = edit->curs_col;
3272 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3273 p = edit_bol (edit, edit->curs1);
3274 edit_cursor_move (edit,
3275 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
3276 if (!left_of_four_spaces (edit))
3277 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3283 /* --------------------------------------------------------------------------------------------- */
3284 /** check whether line in editor is blank or not
3286 * @param edit editor object
3287 * @param line number of line
3289 * @return TRUE if line in blank, FALSE otherwise
3292 gboolean
3293 edit_line_is_blank (WEdit * edit, long line)
3295 return is_blank (edit, edit_find_line (edit, line));
3298 /* --------------------------------------------------------------------------------------------- */
3299 /** move cursor to line 'line' */
3301 void
3302 edit_move_to_line (WEdit * e, long line)
3304 if (line < e->curs_line)
3305 edit_move_up (e, e->curs_line - line, 0);
3306 else
3307 edit_move_down (e, line - e->curs_line, 0);
3308 edit_scroll_screen_over_cursor (e);
3311 /* --------------------------------------------------------------------------------------------- */
3312 /** scroll window so that first visible line is 'line' */
3314 void
3315 edit_move_display (WEdit * e, long line)
3317 if (line < e->start_line)
3318 edit_scroll_upward (e, e->start_line - line);
3319 else
3320 edit_scroll_downward (e, line - e->start_line);
3323 /* --------------------------------------------------------------------------------------------- */
3324 /** save markers onto undo stack */
3326 void
3327 edit_push_markers (WEdit * edit)
3329 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3330 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3331 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3334 /* --------------------------------------------------------------------------------------------- */
3336 void
3337 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3339 edit->mark1 = m1;
3340 edit->mark2 = m2;
3341 edit->column1 = c1;
3342 edit->column2 = c2;
3346 /* --------------------------------------------------------------------------------------------- */
3347 /** highlight marker toggle */
3349 void
3350 edit_mark_cmd (WEdit * edit, gboolean unmark)
3352 edit_push_markers (edit);
3353 if (unmark)
3355 edit_set_markers (edit, 0, 0, 0, 0);
3356 edit->force |= REDRAW_PAGE;
3358 else if (edit->mark2 >= 0)
3360 edit->end_mark_curs = -1;
3361 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3362 edit->curs_col + edit->over_col);
3363 edit->force |= REDRAW_PAGE;
3365 else
3367 edit->end_mark_curs = edit->curs1;
3368 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3369 edit->curs_col + edit->over_col);
3373 /* --------------------------------------------------------------------------------------------- */
3374 /** highlight the word under cursor */
3376 void
3377 edit_mark_current_word_cmd (WEdit * edit)
3379 long pos;
3381 for (pos = edit->curs1; pos != 0; pos--)
3383 int c1, c2;
3385 c1 = edit_get_byte (edit, pos);
3386 c2 = edit_get_byte (edit, pos - 1);
3387 if (!isspace (c1) && isspace (c2))
3388 break;
3389 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3390 break;
3392 edit->mark1 = pos;
3394 for (; pos < edit->last_byte; pos++)
3396 int c1, c2;
3398 c1 = edit_get_byte (edit, pos);
3399 c2 = edit_get_byte (edit, pos + 1);
3400 if (!isspace (c1) && isspace (c2))
3401 break;
3402 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3403 break;
3405 edit->mark2 = min (pos + 1, edit->last_byte);
3407 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3410 /* --------------------------------------------------------------------------------------------- */
3412 void
3413 edit_mark_current_line_cmd (WEdit * edit)
3415 long pos = edit->curs1;
3417 edit->mark1 = edit_bol (edit, pos);
3418 edit->mark2 = edit_eol (edit, pos);
3420 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3423 /* --------------------------------------------------------------------------------------------- */
3425 void
3426 edit_delete_line (WEdit * edit)
3429 * Delete right part of the line.
3430 * Note that edit_get_byte() returns '\n' when byte position is
3431 * beyond EOF.
3433 while (edit_get_byte (edit, edit->curs1) != '\n')
3434 (void) edit_delete (edit, TRUE);
3437 * Delete '\n' char.
3438 * Note that edit_delete() will not corrupt anything if called while
3439 * cursor position is EOF.
3441 (void) edit_delete (edit, TRUE);
3444 * Delete left part of the line.
3445 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3447 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3448 (void) edit_backspace (edit, TRUE);
3451 /* --------------------------------------------------------------------------------------------- */
3453 long
3454 edit_indent_width (const WEdit * edit, off_t p)
3456 off_t q = p;
3458 /* move to the end of the leading whitespace of the line */
3459 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3460 q++;
3461 /* count the number of columns of indentation */
3462 return (long) edit_move_forward3 (edit, p, 0, q);
3465 /* --------------------------------------------------------------------------------------------- */
3467 void
3468 edit_insert_indent (WEdit * edit, int indent)
3470 if (!option_fill_tabs_with_spaces)
3472 while (indent >= TAB_SIZE)
3474 edit_insert (edit, '\t');
3475 indent -= TAB_SIZE;
3478 while (indent-- > 0)
3479 edit_insert (edit, ' ');
3482 /* --------------------------------------------------------------------------------------------- */
3484 void
3485 edit_push_key_press (WEdit * edit)
3487 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3488 if (edit->mark2 == -1)
3490 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3491 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3495 /* --------------------------------------------------------------------------------------------- */
3497 void
3498 edit_find_bracket (WEdit * edit)
3500 edit->bracket = edit_get_bracket (edit, 1, 10000);
3501 if (last_bracket != edit->bracket)
3502 edit->force |= REDRAW_PAGE;
3503 last_bracket = edit->bracket;
3506 /* --------------------------------------------------------------------------------------------- */
3508 * This executes a command as though the user initiated it through a key
3509 * press. Callback with MSG_KEY as a message calls this after
3510 * translating the key press. This function can be used to pass any
3511 * command to the editor. Note that the screen wouldn't update
3512 * automatically. Either of command or char_for_insertion must be
3513 * passed as -1. Commands are executed, and char_for_insertion is
3514 * inserted at the cursor.
3517 void
3518 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3520 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3521 || (macro_index < 0
3522 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3524 macro_index = 0;
3525 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3526 return;
3528 if (macro_index != -1)
3530 edit->force |= REDRAW_COMPLETELY;
3531 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3533 edit_store_macro_cmd (edit);
3534 macro_index = -1;
3535 return;
3537 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3539 edit_repeat_macro_cmd (edit);
3540 macro_index = -1;
3541 return;
3545 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3547 record_macro_buf[macro_index].action = command;
3548 record_macro_buf[macro_index++].ch = char_for_insertion;
3550 /* record the beginning of a set of editing actions initiated by a key press */
3551 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3552 edit_push_key_press (edit);
3554 edit_execute_cmd (edit, command, char_for_insertion);
3555 if (edit->column_highlight)
3556 edit->force |= REDRAW_PAGE;
3559 /* --------------------------------------------------------------------------------------------- */
3561 This executes a command at a lower level than macro recording.
3562 It also does not push a key_press onto the undo stack. This means
3563 that if it is called many times, a single undo command will undo
3564 all of them. It also does not check for the Undo command.
3566 void
3567 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3569 Widget *w = WIDGET (edit);
3571 if (command == CK_WindowFullscreen)
3573 edit_toggle_fullscreen (edit);
3574 return;
3577 /* handle window state */
3578 if (edit_handle_move_resize (edit, command))
3579 return;
3581 edit->force |= REDRAW_LINE;
3583 /* The next key press will unhighlight the found string, so update
3584 * the whole page */
3585 if (edit->found_len || edit->column_highlight)
3586 edit->force |= REDRAW_PAGE;
3588 switch (command)
3590 /* a mark command with shift-arrow */
3591 case CK_MarkLeft:
3592 case CK_MarkRight:
3593 case CK_MarkToWordBegin:
3594 case CK_MarkToWordEnd:
3595 case CK_MarkToHome:
3596 case CK_MarkToEnd:
3597 case CK_MarkUp:
3598 case CK_MarkDown:
3599 case CK_MarkPageUp:
3600 case CK_MarkPageDown:
3601 case CK_MarkToFileBegin:
3602 case CK_MarkToFileEnd:
3603 case CK_MarkToPageBegin:
3604 case CK_MarkToPageEnd:
3605 case CK_MarkScrollUp:
3606 case CK_MarkScrollDown:
3607 case CK_MarkParagraphUp:
3608 case CK_MarkParagraphDown:
3609 /* a mark command with alt-arrow */
3610 case CK_MarkColumnPageUp:
3611 case CK_MarkColumnPageDown:
3612 case CK_MarkColumnLeft:
3613 case CK_MarkColumnRight:
3614 case CK_MarkColumnUp:
3615 case CK_MarkColumnDown:
3616 case CK_MarkColumnScrollUp:
3617 case CK_MarkColumnScrollDown:
3618 case CK_MarkColumnParagraphUp:
3619 case CK_MarkColumnParagraphDown:
3620 edit->column_highlight = 0;
3621 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3623 edit_mark_cmd (edit, TRUE); /* clear */
3624 edit_mark_cmd (edit, FALSE); /* marking on */
3626 edit->highlight = 1;
3627 break;
3629 /* any other command */
3630 default:
3631 if (edit->highlight)
3632 edit_mark_cmd (edit, FALSE); /* clear */
3633 edit->highlight = 0;
3636 /* first check for undo */
3637 if (command == CK_Undo)
3639 edit->redo_stack_reset = 0;
3640 edit_group_undo (edit);
3641 edit->found_len = 0;
3642 edit->prev_col = edit_get_col (edit);
3643 edit->search_start = edit->curs1;
3644 return;
3646 /* check for redo */
3647 if (command == CK_Redo)
3649 edit->redo_stack_reset = 0;
3650 edit_do_redo (edit);
3651 edit->found_len = 0;
3652 edit->prev_col = edit_get_col (edit);
3653 edit->search_start = edit->curs1;
3654 return;
3657 edit->redo_stack_reset = 1;
3659 /* An ordinary key press */
3660 if (char_for_insertion >= 0)
3662 /* if non persistent selection and text selected */
3663 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3664 edit_block_delete_cmd (edit);
3666 if (edit->overwrite)
3668 /* remove char only one time, after input first byte, multibyte chars */
3669 #ifdef HAVE_CHARSET
3670 if (!mc_global.utf8_display || edit->charpoint == 0)
3671 #endif
3672 if (edit_get_byte (edit, edit->curs1) != '\n')
3674 edit_delete (edit, FALSE);
3676 if (option_cursor_beyond_eol && edit->over_col > 0)
3677 edit_insert_over (edit);
3678 #ifdef HAVE_CHARSET
3679 if (char_for_insertion > 255 && !mc_global.utf8_display)
3681 unsigned char str[6 + 1];
3682 size_t i = 0;
3683 int res;
3685 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3686 if (res == 0)
3688 str[0] = '.';
3689 str[1] = '\0';
3691 else
3693 str[res] = '\0';
3695 while (str[i] != 0 && i <= 6)
3697 char_for_insertion = str[i];
3698 edit_insert (edit, char_for_insertion);
3699 i++;
3702 else
3703 #endif
3704 edit_insert (edit, char_for_insertion);
3706 if (option_auto_para_formatting)
3708 format_paragraph (edit, 0);
3709 edit->force |= REDRAW_PAGE;
3711 else
3712 check_and_wrap_line (edit);
3713 edit->found_len = 0;
3714 edit->prev_col = edit_get_col (edit);
3715 edit->search_start = edit->curs1;
3716 edit_find_bracket (edit);
3717 return;
3720 switch (command)
3722 case CK_TopOnScreen:
3723 case CK_BottomOnScreen:
3724 case CK_Top:
3725 case CK_Bottom:
3726 case CK_PageUp:
3727 case CK_PageDown:
3728 case CK_Home:
3729 case CK_End:
3730 case CK_Up:
3731 case CK_Down:
3732 case CK_Left:
3733 case CK_Right:
3734 case CK_WordLeft:
3735 case CK_WordRight:
3736 if (!option_persistent_selections && edit->mark2 >= 0)
3738 if (edit->column_highlight)
3739 edit_push_undo_action (edit, COLUMN_ON);
3740 edit->column_highlight = 0;
3741 edit_mark_cmd (edit, TRUE);
3745 switch (command)
3747 case CK_TopOnScreen:
3748 case CK_BottomOnScreen:
3749 case CK_MarkToPageBegin:
3750 case CK_MarkToPageEnd:
3751 case CK_Up:
3752 case CK_Down:
3753 case CK_WordLeft:
3754 case CK_WordRight:
3755 case CK_MarkToWordBegin:
3756 case CK_MarkToWordEnd:
3757 case CK_MarkUp:
3758 case CK_MarkDown:
3759 case CK_MarkColumnUp:
3760 case CK_MarkColumnDown:
3761 if (edit->mark2 == -1)
3762 break; /*marking is following the cursor: may need to highlight a whole line */
3763 case CK_Left:
3764 case CK_Right:
3765 case CK_MarkLeft:
3766 case CK_MarkRight:
3767 edit->force |= REDRAW_CHAR_ONLY;
3770 /* basic cursor key commands */
3771 switch (command)
3773 case CK_BackSpace:
3774 /* if non persistent selection and text selected */
3775 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3776 edit_block_delete_cmd (edit);
3777 else if (option_cursor_beyond_eol && edit->over_col > 0)
3778 edit->over_col--;
3779 else if (option_backspace_through_tabs && is_in_indent (edit))
3781 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3782 edit_backspace (edit, TRUE);
3784 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3786 int i;
3788 for (i = 0; i < HALF_TAB_SIZE; i++)
3789 edit_backspace (edit, TRUE);
3791 else
3792 edit_backspace (edit, FALSE);
3793 break;
3794 case CK_Delete:
3795 /* if non persistent selection and text selected */
3796 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3797 edit_block_delete_cmd (edit);
3798 else
3800 if (option_cursor_beyond_eol && edit->over_col > 0)
3801 edit_insert_over (edit);
3803 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3805 int i;
3807 for (i = 1; i <= HALF_TAB_SIZE; i++)
3808 edit_delete (edit, TRUE);
3810 else
3811 edit_delete (edit, FALSE);
3813 break;
3814 case CK_DeleteToWordBegin:
3815 edit->over_col = 0;
3816 edit_left_delete_word (edit);
3817 break;
3818 case CK_DeleteToWordEnd:
3819 if (option_cursor_beyond_eol && edit->over_col > 0)
3820 edit_insert_over (edit);
3822 edit_right_delete_word (edit);
3823 break;
3824 case CK_DeleteLine:
3825 edit_delete_line (edit);
3826 break;
3827 case CK_DeleteToHome:
3828 edit_delete_to_line_begin (edit);
3829 break;
3830 case CK_DeleteToEnd:
3831 edit_delete_to_line_end (edit);
3832 break;
3833 case CK_Enter:
3834 edit->over_col = 0;
3835 if (option_auto_para_formatting)
3837 edit_double_newline (edit);
3838 if (option_return_does_auto_indent)
3839 edit_auto_indent (edit);
3840 format_paragraph (edit, 0);
3842 else
3844 edit_insert (edit, '\n');
3845 if (option_return_does_auto_indent)
3846 edit_auto_indent (edit);
3848 break;
3849 case CK_Return:
3850 edit_insert (edit, '\n');
3851 break;
3853 case CK_MarkColumnPageUp:
3854 edit->column_highlight = 1;
3855 case CK_PageUp:
3856 case CK_MarkPageUp:
3857 edit_move_up (edit, w->lines - 1, 1);
3858 break;
3859 case CK_MarkColumnPageDown:
3860 edit->column_highlight = 1;
3861 case CK_PageDown:
3862 case CK_MarkPageDown:
3863 edit_move_down (edit, w->lines - 1, 1);
3864 break;
3865 case CK_MarkColumnLeft:
3866 edit->column_highlight = 1;
3867 case CK_Left:
3868 case CK_MarkLeft:
3869 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3871 if (option_cursor_beyond_eol && edit->over_col > 0)
3872 edit->over_col--;
3873 else
3874 edit_cursor_move (edit, -HALF_TAB_SIZE);
3875 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3877 else
3878 edit_left_char_move_cmd (edit);
3879 break;
3880 case CK_MarkColumnRight:
3881 edit->column_highlight = 1;
3882 case CK_Right:
3883 case CK_MarkRight:
3884 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3886 edit_cursor_move (edit, HALF_TAB_SIZE);
3887 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3889 else
3890 edit_right_char_move_cmd (edit);
3891 break;
3892 case CK_TopOnScreen:
3893 case CK_MarkToPageBegin:
3894 edit_begin_page (edit);
3895 break;
3896 case CK_BottomOnScreen:
3897 case CK_MarkToPageEnd:
3898 edit_end_page (edit);
3899 break;
3900 case CK_WordLeft:
3901 case CK_MarkToWordBegin:
3902 edit->over_col = 0;
3903 edit_left_word_move_cmd (edit);
3904 break;
3905 case CK_WordRight:
3906 case CK_MarkToWordEnd:
3907 edit->over_col = 0;
3908 edit_right_word_move_cmd (edit);
3909 break;
3910 case CK_MarkColumnUp:
3911 edit->column_highlight = 1;
3912 case CK_Up:
3913 case CK_MarkUp:
3914 edit_move_up (edit, 1, 0);
3915 break;
3916 case CK_MarkColumnDown:
3917 edit->column_highlight = 1;
3918 case CK_Down:
3919 case CK_MarkDown:
3920 edit_move_down (edit, 1, 0);
3921 break;
3922 case CK_MarkColumnParagraphUp:
3923 edit->column_highlight = 1;
3924 case CK_ParagraphUp:
3925 case CK_MarkParagraphUp:
3926 edit_move_up_paragraph (edit, 0);
3927 break;
3928 case CK_MarkColumnParagraphDown:
3929 edit->column_highlight = 1;
3930 case CK_ParagraphDown:
3931 case CK_MarkParagraphDown:
3932 edit_move_down_paragraph (edit, 0);
3933 break;
3934 case CK_MarkColumnScrollUp:
3935 edit->column_highlight = 1;
3936 case CK_ScrollUp:
3937 case CK_MarkScrollUp:
3938 edit_move_up (edit, 1, 1);
3939 break;
3940 case CK_MarkColumnScrollDown:
3941 edit->column_highlight = 1;
3942 case CK_ScrollDown:
3943 case CK_MarkScrollDown:
3944 edit_move_down (edit, 1, 1);
3945 break;
3946 case CK_Home:
3947 case CK_MarkToHome:
3948 edit_cursor_to_bol (edit);
3949 break;
3950 case CK_End:
3951 case CK_MarkToEnd:
3952 edit_cursor_to_eol (edit);
3953 break;
3954 case CK_Tab:
3955 /* if text marked shift block */
3956 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3958 if (edit->mark2 < 0)
3959 edit_mark_cmd (edit, FALSE);
3960 edit_move_block_to_right (edit);
3962 else
3964 if (option_cursor_beyond_eol)
3965 edit_insert_over (edit);
3966 edit_tab_cmd (edit);
3967 if (option_auto_para_formatting)
3969 format_paragraph (edit, 0);
3970 edit->force |= REDRAW_PAGE;
3972 else
3973 check_and_wrap_line (edit);
3975 break;
3977 case CK_InsertOverwrite:
3978 edit->overwrite = !edit->overwrite;
3979 break;
3981 case CK_Mark:
3982 if (edit->mark2 >= 0)
3984 if (edit->column_highlight)
3985 edit_push_undo_action (edit, COLUMN_ON);
3986 edit->column_highlight = 0;
3988 edit_mark_cmd (edit, FALSE);
3989 break;
3990 case CK_MarkColumn:
3991 if (!edit->column_highlight)
3992 edit_push_undo_action (edit, COLUMN_OFF);
3993 edit->column_highlight = 1;
3994 edit_mark_cmd (edit, FALSE);
3995 break;
3996 case CK_MarkAll:
3997 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3998 edit->force |= REDRAW_PAGE;
3999 break;
4000 case CK_Unmark:
4001 if (edit->column_highlight)
4002 edit_push_undo_action (edit, COLUMN_ON);
4003 edit->column_highlight = 0;
4004 edit_mark_cmd (edit, TRUE);
4005 break;
4006 case CK_MarkWord:
4007 if (edit->column_highlight)
4008 edit_push_undo_action (edit, COLUMN_ON);
4009 edit->column_highlight = 0;
4010 edit_mark_current_word_cmd (edit);
4011 break;
4012 case CK_MarkLine:
4013 if (edit->column_highlight)
4014 edit_push_undo_action (edit, COLUMN_ON);
4015 edit->column_highlight = 0;
4016 edit_mark_current_line_cmd (edit);
4017 break;
4019 case CK_Bookmark:
4020 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4021 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4022 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4023 else
4024 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4025 break;
4026 case CK_BookmarkFlush:
4027 book_mark_flush (edit, BOOK_MARK_COLOR);
4028 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4029 edit->force |= REDRAW_PAGE;
4030 break;
4031 case CK_BookmarkNext:
4032 if (edit->book_mark != NULL)
4034 edit_book_mark_t *p;
4036 p = book_mark_find (edit, edit->curs_line);
4037 if (p->next != NULL)
4039 p = p->next;
4040 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4041 edit_move_display (edit, p->line - w->lines / 2);
4042 edit_move_to_line (edit, p->line);
4045 break;
4046 case CK_BookmarkPrev:
4047 if (edit->book_mark != NULL)
4049 edit_book_mark_t *p;
4051 p = book_mark_find (edit, edit->curs_line);
4052 while (p->line == edit->curs_line)
4053 if (p->prev != NULL)
4054 p = p->prev;
4055 if (p->line >= 0)
4057 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4058 edit_move_display (edit, p->line - w->lines / 2);
4059 edit_move_to_line (edit, p->line);
4062 break;
4064 case CK_Top:
4065 case CK_MarkToFileBegin:
4066 edit_move_to_top (edit);
4067 break;
4068 case CK_Bottom:
4069 case CK_MarkToFileEnd:
4070 edit_move_to_bottom (edit);
4071 break;
4073 case CK_Copy:
4074 if (option_cursor_beyond_eol && edit->over_col > 0)
4075 edit_insert_over (edit);
4076 edit_block_copy_cmd (edit);
4077 break;
4078 case CK_Remove:
4079 edit_block_delete_cmd (edit);
4080 break;
4081 case CK_Move:
4082 edit_block_move_cmd (edit);
4083 break;
4085 case CK_BlockShiftLeft:
4086 if (edit->mark1 != edit->mark2)
4087 edit_move_block_to_left (edit);
4088 break;
4089 case CK_BlockShiftRight:
4090 if (edit->mark1 != edit->mark2)
4091 edit_move_block_to_right (edit);
4092 break;
4093 case CK_Store:
4094 edit_copy_to_X_buf_cmd (edit);
4095 break;
4096 case CK_Cut:
4097 edit_cut_to_X_buf_cmd (edit);
4098 break;
4099 case CK_Paste:
4100 /* if non persistent selection and text selected */
4101 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4102 edit_block_delete_cmd (edit);
4103 if (option_cursor_beyond_eol && edit->over_col > 0)
4104 edit_insert_over (edit);
4105 edit_paste_from_X_buf_cmd (edit);
4106 if (!option_persistent_selections && edit->mark2 >= 0)
4108 if (edit->column_highlight)
4109 edit_push_undo_action (edit, COLUMN_ON);
4110 edit->column_highlight = 0;
4111 edit_mark_cmd (edit, TRUE);
4113 break;
4114 case CK_History:
4115 edit_paste_from_history (edit);
4116 break;
4118 case CK_SaveAs:
4119 edit_save_as_cmd (edit);
4120 break;
4121 case CK_Save:
4122 edit_save_confirm_cmd (edit);
4123 break;
4124 case CK_BlockSave:
4125 edit_save_block_cmd (edit);
4126 break;
4127 case CK_InsertFile:
4128 edit_insert_file_cmd (edit);
4129 break;
4131 case CK_FilePrev:
4132 edit_load_back_cmd (edit);
4133 break;
4134 case CK_FileNext:
4135 edit_load_forward_cmd (edit);
4136 break;
4138 case CK_SyntaxChoose:
4139 edit_syntax_dialog (edit);
4140 break;
4142 case CK_Search:
4143 edit_search_cmd (edit, FALSE);
4144 break;
4145 case CK_SearchContinue:
4146 edit_search_cmd (edit, TRUE);
4147 break;
4148 case CK_Replace:
4149 edit_replace_cmd (edit, 0);
4150 break;
4151 case CK_ReplaceContinue:
4152 edit_replace_cmd (edit, 1);
4153 break;
4154 case CK_Complete:
4155 /* if text marked shift block */
4156 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4157 edit_move_block_to_left (edit);
4158 else
4159 edit_complete_word_cmd (edit);
4160 break;
4161 case CK_Find:
4162 edit_get_match_keyword_cmd (edit);
4163 break;
4165 #ifdef HAVE_ASPELL
4166 case CK_SpellCheckCurrentWord:
4167 edit_suggest_current_word (edit);
4168 break;
4169 case CK_SpellCheck:
4170 edit_spellcheck_file (edit);
4171 break;
4172 case CK_SpellCheckSelectLang:
4173 edit_set_spell_lang ();
4174 break;
4175 #endif
4177 case CK_Date:
4179 char s[BUF_MEDIUM];
4180 /* fool gcc to prevent a Y2K warning */
4181 char time_format[] = "_c";
4182 time_format[0] = '%';
4184 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4185 edit_print_string (edit, s);
4186 edit->force |= REDRAW_PAGE;
4188 break;
4189 case CK_Goto:
4190 edit_goto_cmd (edit);
4191 break;
4192 case CK_ParagraphFormat:
4193 format_paragraph (edit, 1);
4194 edit->force |= REDRAW_PAGE;
4195 break;
4196 case CK_MacroDelete:
4197 edit_delete_macro_cmd (edit);
4198 break;
4199 case CK_MatchBracket:
4200 edit_goto_matching_bracket (edit);
4201 break;
4202 case CK_UserMenu:
4203 user_menu (edit, NULL, -1);
4204 break;
4205 case CK_Sort:
4206 edit_sort_cmd (edit);
4207 break;
4208 case CK_ExternalCommand:
4209 edit_ext_cmd (edit);
4210 break;
4211 case CK_Mail:
4212 edit_mail_dialog (edit);
4213 break;
4214 #ifdef HAVE_CHARSET
4215 case CK_SelectCodepage:
4216 edit_select_codepage_cmd (edit);
4217 break;
4218 #endif
4219 case CK_InsertLiteral:
4220 edit_insert_literal_cmd (edit);
4221 break;
4222 case CK_MacroStartStopRecord:
4223 edit_begin_end_macro_cmd (edit);
4224 break;
4225 case CK_RepeatStartStopRecord:
4226 edit_begin_end_repeat_cmd (edit);
4227 break;
4228 case CK_ExtendedKeyMap:
4229 edit->extmod = TRUE;
4230 break;
4231 default:
4232 break;
4235 /* CK_PipeBlock */
4236 if ((command / CK_PipeBlock (0)) == 1)
4237 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4239 /* keys which must set the col position, and the search vars */
4240 switch (command)
4242 case CK_Search:
4243 case CK_SearchContinue:
4244 case CK_Replace:
4245 case CK_ReplaceContinue:
4246 case CK_Complete:
4247 edit->prev_col = edit_get_col (edit);
4248 break;
4249 case CK_Up:
4250 case CK_MarkUp:
4251 case CK_MarkColumnUp:
4252 case CK_Down:
4253 case CK_MarkDown:
4254 case CK_MarkColumnDown:
4255 case CK_PageUp:
4256 case CK_MarkPageUp:
4257 case CK_MarkColumnPageUp:
4258 case CK_PageDown:
4259 case CK_MarkPageDown:
4260 case CK_MarkColumnPageDown:
4261 case CK_Top:
4262 case CK_MarkToFileBegin:
4263 case CK_Bottom:
4264 case CK_MarkToFileEnd:
4265 case CK_ParagraphUp:
4266 case CK_MarkParagraphUp:
4267 case CK_MarkColumnParagraphUp:
4268 case CK_ParagraphDown:
4269 case CK_MarkParagraphDown:
4270 case CK_MarkColumnParagraphDown:
4271 case CK_ScrollUp:
4272 case CK_MarkScrollUp:
4273 case CK_MarkColumnScrollUp:
4274 case CK_ScrollDown:
4275 case CK_MarkScrollDown:
4276 case CK_MarkColumnScrollDown:
4277 edit->search_start = edit->curs1;
4278 edit->found_len = 0;
4279 break;
4280 default:
4281 edit->found_len = 0;
4282 edit->prev_col = edit_get_col (edit);
4283 edit->search_start = edit->curs1;
4285 edit_find_bracket (edit);
4287 if (option_auto_para_formatting)
4289 switch (command)
4291 case CK_BackSpace:
4292 case CK_Delete:
4293 case CK_DeleteToWordBegin:
4294 case CK_DeleteToWordEnd:
4295 case CK_DeleteToHome:
4296 case CK_DeleteToEnd:
4297 format_paragraph (edit, 0);
4298 edit->force |= REDRAW_PAGE;
4303 /* --------------------------------------------------------------------------------------------- */
4305 void
4306 edit_stack_init (void)
4308 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4310 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4311 edit_history_moveto[edit_stack_iterator].line = -1;
4314 edit_stack_iterator = 0;
4317 /* --------------------------------------------------------------------------------------------- */
4319 void
4320 edit_stack_free (void)
4322 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4323 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4326 /* --------------------------------------------------------------------------------------------- */
4327 /** move i lines */
4329 void
4330 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4332 edit_move_updown (edit, i, do_scroll, TRUE);
4335 /* --------------------------------------------------------------------------------------------- */
4336 /** move i lines */
4338 void
4339 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4341 edit_move_updown (edit, i, do_scroll, FALSE);
4344 /* --------------------------------------------------------------------------------------------- */