Ticket #2876: code cleanup before 4.8.6 release.
[midnight-commander.git] / src / editor / edit.c
blob5a22b51fa050ccbed298b213484ce8821282dead
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/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
73 #ifdef HAVE_ASPELL
74 #include "spell.h"
75 #endif
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
80 int option_typewriter_wrap = 0;
81 int option_auto_para_formatting = 0;
82 int option_fill_tabs_with_spaces = 0;
83 int option_return_does_auto_indent = 1;
84 int option_backspace_through_tabs = 0;
85 int option_fake_half_tabs = 1;
86 int option_save_mode = EDIT_QUICK_SAVE;
87 int option_save_position = 1;
88 int option_max_undo = 32768;
89 int option_persistent_selections = 1;
90 int option_cursor_beyond_eol = 0;
91 int option_line_state = 0;
92 int option_line_state_width = 0;
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 * @returns 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 * @returns 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 - (edit->widget.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 (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 (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 (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 (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 (line_is_blank (edit, edit->curs_line))
836 if (line_is_blank (edit, edit->curs_line - 1))
838 for (i = edit->curs_line - 1; i; i--)
839 if (!line_is_blank (edit, i))
841 i++;
842 break;
845 else
847 for (i = edit->curs_line - 1; i; i--)
848 if (line_is_blank (edit, i))
849 break;
852 else
854 for (i = edit->curs_line - 1; i; i--)
855 if (line_is_blank (edit, i))
856 break;
859 edit_move_up (edit, edit->curs_line - i, do_scroll);
862 /* --------------------------------------------------------------------------------------------- */
863 /** moves down until a blank line is reached, or until just
864 before a non-blank line is reached */
866 static void
867 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
869 long i;
871 if (edit->curs_line >= edit->total_lines - 1)
873 i = edit->total_lines;
875 else
877 if (line_is_blank (edit, edit->curs_line))
879 if (line_is_blank (edit, edit->curs_line + 1))
881 for (i = edit->curs_line + 1; i; i++)
882 if (!line_is_blank (edit, i) || i > edit->total_lines)
884 i--;
885 break;
888 else
890 for (i = edit->curs_line + 1; i; i++)
891 if (line_is_blank (edit, i) || i >= edit->total_lines)
892 break;
895 else
897 for (i = edit->curs_line + 1; i; i++)
898 if (line_is_blank (edit, i) || i >= edit->total_lines)
899 break;
902 edit_move_down (edit, i - edit->curs_line, do_scroll);
905 /* --------------------------------------------------------------------------------------------- */
907 static void
908 edit_begin_page (WEdit * edit)
910 edit_update_curs_row (edit);
911 edit_move_up (edit, edit->curs_row, 0);
914 /* --------------------------------------------------------------------------------------------- */
916 static void
917 edit_end_page (WEdit * edit)
919 edit_update_curs_row (edit);
920 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
924 /* --------------------------------------------------------------------------------------------- */
925 /** goto beginning of text */
927 static void
928 edit_move_to_top (WEdit * edit)
930 if (edit->curs_line)
932 edit_cursor_move (edit, -edit->curs1);
933 edit_move_to_prev_col (edit, 0);
934 edit->force |= REDRAW_PAGE;
935 edit->search_start = 0;
936 edit_update_curs_row (edit);
941 /* --------------------------------------------------------------------------------------------- */
942 /** goto end of text */
944 static void
945 edit_move_to_bottom (WEdit * edit)
947 if (edit->curs_line < edit->total_lines)
949 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
950 edit->start_display = edit->last_byte;
951 edit->start_line = edit->total_lines;
952 edit_scroll_upward (edit, edit->widget.lines - 1);
953 edit->force |= REDRAW_PAGE;
957 /* --------------------------------------------------------------------------------------------- */
958 /** goto beginning of line */
960 static void
961 edit_cursor_to_bol (WEdit * edit)
963 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
964 edit->search_start = edit->curs1;
965 edit->prev_col = edit_get_col (edit);
966 edit->over_col = 0;
969 /* --------------------------------------------------------------------------------------------- */
970 /** goto end of line */
972 static void
973 edit_cursor_to_eol (WEdit * edit)
975 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
976 edit->search_start = edit->curs1;
977 edit->prev_col = edit_get_col (edit);
978 edit->over_col = 0;
981 /* --------------------------------------------------------------------------------------------- */
983 static unsigned long
984 my_type_of (int c)
986 int x, r = 0;
987 const char *p, *q;
988 const char option_chars_move_whole_word[] =
989 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
991 if (!c)
992 return 0;
993 if (c == '!')
995 if (*option_chars_move_whole_word == '!')
996 return 2;
997 return 0x80000000UL;
999 if (g_ascii_isupper ((gchar) c))
1000 c = 'A';
1001 else if (g_ascii_islower ((gchar) c))
1002 c = 'a';
1003 else if (g_ascii_isalpha (c))
1004 c = 'a';
1005 else if (isdigit (c))
1006 c = '0';
1007 else if (isspace (c))
1008 c = ' ';
1009 q = strchr (option_chars_move_whole_word, c);
1010 if (!q)
1011 return 0xFFFFFFFFUL;
1014 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1015 if (*p == '!')
1016 x <<= 1;
1017 r |= x;
1019 while ((q = strchr (q + 1, c)));
1020 return r;
1023 /* --------------------------------------------------------------------------------------------- */
1025 static void
1026 edit_left_word_move (WEdit * edit, int s)
1028 for (;;)
1030 int c1, c2;
1031 if (edit->column_highlight
1032 && edit->mark1 != edit->mark2
1033 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1034 break;
1035 edit_cursor_move (edit, -1);
1036 if (!edit->curs1)
1037 break;
1038 c1 = edit_get_byte (edit, edit->curs1 - 1);
1039 c2 = edit_get_byte (edit, edit->curs1);
1040 if (c1 == '\n' || c2 == '\n')
1041 break;
1042 if (!(my_type_of (c1) & my_type_of (c2)))
1043 break;
1044 if (isspace (c1) && !isspace (c2))
1045 break;
1046 if (s)
1047 if (!isspace (c1) && isspace (c2))
1048 break;
1052 /* --------------------------------------------------------------------------------------------- */
1054 static void
1055 edit_left_word_move_cmd (WEdit * edit)
1057 edit_left_word_move (edit, 0);
1058 edit->force |= REDRAW_PAGE;
1061 /* --------------------------------------------------------------------------------------------- */
1063 static void
1064 edit_right_word_move (WEdit * edit, int s)
1066 for (;;)
1068 int c1, c2;
1069 if (edit->column_highlight
1070 && edit->mark1 != edit->mark2
1071 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1072 break;
1073 edit_cursor_move (edit, 1);
1074 if (edit->curs1 >= edit->last_byte)
1075 break;
1076 c1 = edit_get_byte (edit, edit->curs1 - 1);
1077 c2 = edit_get_byte (edit, edit->curs1);
1078 if (c1 == '\n' || c2 == '\n')
1079 break;
1080 if (!(my_type_of (c1) & my_type_of (c2)))
1081 break;
1082 if (isspace (c1) && !isspace (c2))
1083 break;
1084 if (s)
1085 if (!isspace (c1) && isspace (c2))
1086 break;
1090 /* --------------------------------------------------------------------------------------------- */
1092 static void
1093 edit_right_word_move_cmd (WEdit * edit)
1095 edit_right_word_move (edit, 0);
1096 edit->force |= REDRAW_PAGE;
1099 /* --------------------------------------------------------------------------------------------- */
1101 static void
1102 edit_right_char_move_cmd (WEdit * edit)
1104 int cw = 1;
1105 int c = 0;
1106 #ifdef HAVE_CHARSET
1107 if (edit->utf8)
1109 c = edit_get_utf (edit, edit->curs1, &cw);
1110 if (cw < 1)
1111 cw = 1;
1113 else
1114 #endif
1116 c = edit_get_byte (edit, edit->curs1);
1118 if (option_cursor_beyond_eol && c == '\n')
1120 edit->over_col++;
1122 else
1124 edit_cursor_move (edit, cw);
1128 /* --------------------------------------------------------------------------------------------- */
1130 static void
1131 edit_left_char_move_cmd (WEdit * edit)
1133 int cw = 1;
1134 if (edit->column_highlight
1135 && option_cursor_beyond_eol
1136 && edit->mark1 != edit->mark2
1137 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1138 return;
1139 #ifdef HAVE_CHARSET
1140 if (edit->utf8)
1142 edit_get_prev_utf (edit, edit->curs1, &cw);
1143 if (cw < 1)
1144 cw = 1;
1146 #endif
1147 if (option_cursor_beyond_eol && edit->over_col > 0)
1149 edit->over_col--;
1151 else
1153 edit_cursor_move (edit, -cw);
1157 /* --------------------------------------------------------------------------------------------- */
1158 /** Up or down cursor moving.
1159 direction = TRUE - move up
1160 = FALSE - move down
1163 static void
1164 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1166 long p;
1167 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1169 if (lines > l)
1170 lines = l;
1172 if (lines == 0)
1173 return;
1175 if (lines > 1)
1176 edit->force |= REDRAW_PAGE;
1177 if (do_scroll)
1179 if (direction)
1180 edit_scroll_upward (edit, lines);
1181 else
1182 edit_scroll_downward (edit, lines);
1184 p = edit_bol (edit, edit->curs1);
1186 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1188 edit_cursor_move (edit, p - edit->curs1);
1190 edit_move_to_prev_col (edit, p);
1192 /* search start of current multibyte char (like CJK) */
1193 if (edit->curs1 + 1 < edit->last_byte)
1195 edit_right_char_move_cmd (edit);
1196 edit_left_char_move_cmd (edit);
1199 edit->search_start = edit->curs1;
1200 edit->found_len = 0;
1203 /* --------------------------------------------------------------------------------------------- */
1205 static void
1206 edit_right_delete_word (WEdit * edit)
1208 int c1, c2;
1209 for (;;)
1211 if (edit->curs1 >= edit->last_byte)
1212 break;
1213 c1 = edit_delete (edit, 1);
1214 c2 = edit_get_byte (edit, edit->curs1);
1215 if (c1 == '\n' || c2 == '\n')
1216 break;
1217 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1218 break;
1219 if (!(my_type_of (c1) & my_type_of (c2)))
1220 break;
1224 /* --------------------------------------------------------------------------------------------- */
1226 static void
1227 edit_left_delete_word (WEdit * edit)
1229 int c1, c2;
1230 for (;;)
1232 if (edit->curs1 <= 0)
1233 break;
1234 c1 = edit_backspace (edit, 1);
1235 c2 = edit_get_byte (edit, edit->curs1 - 1);
1236 if (c1 == '\n' || c2 == '\n')
1237 break;
1238 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1239 break;
1240 if (!(my_type_of (c1) & my_type_of (c2)))
1241 break;
1245 /* --------------------------------------------------------------------------------------------- */
1247 the start column position is not recorded, and hence does not
1248 undo as it happed. But who would notice.
1251 static void
1252 edit_do_undo (WEdit * edit)
1254 long ac;
1255 long count = 0;
1257 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1258 edit->over_col = 0;
1259 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1261 switch ((int) ac)
1263 case STACK_BOTTOM:
1264 goto done_undo;
1265 case CURS_RIGHT:
1266 edit_cursor_move (edit, 1);
1267 break;
1268 case CURS_LEFT:
1269 edit_cursor_move (edit, -1);
1270 break;
1271 case BACKSPACE:
1272 case BACKSPACE_BR:
1273 edit_backspace (edit, 1);
1274 break;
1275 case DELCHAR:
1276 case DELCHAR_BR:
1277 edit_delete (edit, 1);
1278 break;
1279 case COLUMN_ON:
1280 edit->column_highlight = 1;
1281 break;
1282 case COLUMN_OFF:
1283 edit->column_highlight = 0;
1284 break;
1286 if (ac >= 256 && ac < 512)
1287 edit_insert_ahead (edit, ac - 256);
1288 if (ac >= 0 && ac < 256)
1289 edit_insert (edit, ac);
1291 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1293 edit->mark1 = ac - MARK_1;
1294 edit->column1 =
1295 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1297 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1299 edit->mark2 = ac - MARK_2;
1300 edit->column2 =
1301 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1303 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1305 edit->end_mark_curs = ac - MARK_CURS;
1307 if (count++)
1308 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1311 if (edit->start_display > ac - KEY_PRESS)
1313 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1314 edit->force |= REDRAW_PAGE;
1316 else if (edit->start_display < ac - KEY_PRESS)
1318 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1319 edit->force |= REDRAW_PAGE;
1321 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1322 edit_update_curs_row (edit);
1324 done_undo:;
1325 edit->undo_stack_disable = 0;
1328 static void
1329 edit_do_redo (WEdit * edit)
1331 long ac;
1332 long count = 0;
1334 if (edit->redo_stack_reset)
1335 return;
1337 edit->over_col = 0;
1338 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1340 switch ((int) ac)
1342 case STACK_BOTTOM:
1343 goto done_redo;
1344 case CURS_RIGHT:
1345 edit_cursor_move (edit, 1);
1346 break;
1347 case CURS_LEFT:
1348 edit_cursor_move (edit, -1);
1349 break;
1350 case BACKSPACE:
1351 edit_backspace (edit, 1);
1352 break;
1353 case DELCHAR:
1354 edit_delete (edit, 1);
1355 break;
1356 case COLUMN_ON:
1357 edit->column_highlight = 1;
1358 break;
1359 case COLUMN_OFF:
1360 edit->column_highlight = 0;
1361 break;
1363 if (ac >= 256 && ac < 512)
1364 edit_insert_ahead (edit, ac - 256);
1365 if (ac >= 0 && ac < 256)
1366 edit_insert (edit, ac);
1368 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1370 edit->mark1 = ac - MARK_1;
1371 edit->column1 =
1372 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1374 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1376 edit->mark2 = ac - MARK_2;
1377 edit->column2 =
1378 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1380 /* more than one pop usually means something big */
1381 if (count++)
1382 edit->force |= REDRAW_PAGE;
1385 if (edit->start_display > ac - KEY_PRESS)
1387 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1388 edit->force |= REDRAW_PAGE;
1390 else if (edit->start_display < ac - KEY_PRESS)
1392 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1393 edit->force |= REDRAW_PAGE;
1395 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1396 edit_update_curs_row (edit);
1398 done_redo:;
1401 static void
1402 edit_group_undo (WEdit * edit)
1404 long ac = KEY_PRESS;
1405 long cur_ac = KEY_PRESS;
1406 while (ac != STACK_BOTTOM && ac == cur_ac)
1408 cur_ac = get_prev_undo_action (edit);
1409 edit_do_undo (edit);
1410 ac = get_prev_undo_action (edit);
1411 /* exit from cycle if option_group_undo is not set,
1412 * and make single UNDO operation
1414 if (!option_group_undo)
1415 ac = STACK_BOTTOM;
1419 /* --------------------------------------------------------------------------------------------- */
1421 static void
1422 edit_delete_to_line_end (WEdit * edit)
1424 while (edit_get_byte (edit, edit->curs1) != '\n')
1426 if (!edit->curs2)
1427 break;
1428 edit_delete (edit, 1);
1432 /* --------------------------------------------------------------------------------------------- */
1434 static void
1435 edit_delete_to_line_begin (WEdit * edit)
1437 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1439 if (!edit->curs1)
1440 break;
1441 edit_backspace (edit, 1);
1445 /* --------------------------------------------------------------------------------------------- */
1447 static gboolean
1448 is_aligned_on_a_tab (WEdit * edit)
1450 long curs_col;
1452 edit_update_curs_col (edit);
1453 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1454 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1457 /* --------------------------------------------------------------------------------------------- */
1459 static gboolean
1460 right_of_four_spaces (WEdit * edit)
1462 int i, ch = 0;
1464 for (i = 1; i <= HALF_TAB_SIZE; i++)
1465 ch |= edit_get_byte (edit, edit->curs1 - i);
1467 return (ch == ' ' && is_aligned_on_a_tab (edit));
1470 /* --------------------------------------------------------------------------------------------- */
1472 static gboolean
1473 left_of_four_spaces (WEdit * edit)
1475 int i, ch = 0;
1477 for (i = 0; i < HALF_TAB_SIZE; i++)
1478 ch |= edit_get_byte (edit, edit->curs1 + i);
1480 return (ch == ' ' && is_aligned_on_a_tab (edit));
1483 /* --------------------------------------------------------------------------------------------- */
1485 static void
1486 edit_auto_indent (WEdit * edit)
1488 off_t p;
1489 char c;
1490 p = edit->curs1;
1491 /* use the previous line as a template */
1492 p = edit_move_backward (edit, p, 1);
1493 /* copy the leading whitespace of the line */
1494 for (;;)
1495 { /* no range check - the line _is_ \n-terminated */
1496 c = edit_get_byte (edit, p++);
1497 if (c != ' ' && c != '\t')
1498 break;
1499 edit_insert (edit, c);
1503 /* --------------------------------------------------------------------------------------------- */
1505 static inline void
1506 edit_double_newline (WEdit * edit)
1508 edit_insert (edit, '\n');
1509 if (edit_get_byte (edit, edit->curs1) == '\n')
1510 return;
1511 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1512 return;
1513 edit->force |= REDRAW_PAGE;
1514 edit_insert (edit, '\n');
1518 /* --------------------------------------------------------------------------------------------- */
1520 static void
1521 insert_spaces_tab (WEdit * edit, gboolean half)
1523 long i;
1525 edit_update_curs_col (edit);
1526 i = option_tab_spacing * space_width;
1527 if (half)
1528 i /= 2;
1529 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1530 while (i > 0)
1532 edit_insert (edit, ' ');
1533 i -= space_width;
1537 /* --------------------------------------------------------------------------------------------- */
1539 static inline void
1540 edit_tab_cmd (WEdit * edit)
1542 int i;
1544 if (option_fake_half_tabs)
1546 if (is_in_indent (edit))
1548 /*insert a half tab (usually four spaces) unless there is a
1549 half tab already behind, then delete it and insert a
1550 full tab. */
1551 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1552 insert_spaces_tab (edit, TRUE);
1553 else
1555 for (i = 1; i <= HALF_TAB_SIZE; i++)
1556 edit_backspace (edit, 1);
1557 edit_insert (edit, '\t');
1559 return;
1562 if (option_fill_tabs_with_spaces)
1563 insert_spaces_tab (edit, FALSE);
1564 else
1565 edit_insert (edit, '\t');
1568 /* --------------------------------------------------------------------------------------------- */
1570 static void
1571 check_and_wrap_line (WEdit * edit)
1573 off_t curs;
1574 int c;
1576 if (!option_typewriter_wrap)
1577 return;
1578 edit_update_curs_col (edit);
1579 if (edit->curs_col < option_word_wrap_line_length)
1580 return;
1581 curs = edit->curs1;
1582 for (;;)
1584 curs--;
1585 c = edit_get_byte (edit, curs);
1586 if (c == '\n' || curs <= 0)
1588 edit_insert (edit, '\n');
1589 return;
1591 if (c == ' ' || c == '\t')
1593 off_t current = edit->curs1;
1594 edit_cursor_move (edit, curs - edit->curs1 + 1);
1595 edit_insert (edit, '\n');
1596 edit_cursor_move (edit, current - edit->curs1 + 1);
1597 return;
1602 /* --------------------------------------------------------------------------------------------- */
1603 /** this find the matching bracket in either direction, and sets edit->bracket
1605 * @param edit editor object
1606 * @param in_screen seach only on the current screen
1607 * @param furthest_bracket_search count of the bytes for search
1609 * @return position of the found bracket (-1 if no match)
1612 static off_t
1613 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1615 const char *const b = "{}{[][()(", *p;
1616 int i = 1, a, inc = -1, c, d, n = 0;
1617 unsigned long j = 0;
1618 off_t q;
1619 edit_update_curs_row (edit);
1620 c = edit_get_byte (edit, edit->curs1);
1621 p = strchr (b, c);
1622 /* no limit */
1623 if (!furthest_bracket_search)
1624 furthest_bracket_search--;
1625 /* not on a bracket at all */
1626 if (p == NULL)
1627 return -1;
1628 /* the matching bracket */
1629 d = p[1];
1630 /* going left or right? */
1631 if (strchr ("{[(", c))
1632 inc = 1;
1633 for (q = edit->curs1 + inc;; q += inc)
1635 /* out of buffer? */
1636 if (q >= edit->last_byte || q < 0)
1637 break;
1638 a = edit_get_byte (edit, q);
1639 /* don't want to eat CPU */
1640 if (j++ > furthest_bracket_search)
1641 break;
1642 /* out of screen? */
1643 if (in_screen)
1645 if (q < edit->start_display)
1646 break;
1647 /* count lines if searching downward */
1648 if (inc > 0 && a == '\n')
1649 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1650 break;
1652 /* count bracket depth */
1653 i += (a == c) - (a == d);
1654 /* return if bracket depth is zero */
1655 if (i == 0)
1656 return q;
1658 /* no match */
1659 return -1;
1662 /* --------------------------------------------------------------------------------------------- */
1664 static inline void
1665 edit_goto_matching_bracket (WEdit * edit)
1667 off_t q;
1669 q = edit_get_bracket (edit, 0, 0);
1670 if (q >= 0)
1672 edit->bracket = edit->curs1;
1673 edit->force |= REDRAW_PAGE;
1674 edit_cursor_move (edit, q - edit->curs1);
1678 /* --------------------------------------------------------------------------------------------- */
1680 static void
1681 edit_move_block_to_right (WEdit * edit)
1683 off_t start_mark, end_mark;
1684 long cur_bol, start_bol;
1686 if (eval_marks (edit, &start_mark, &end_mark))
1687 return;
1689 start_bol = edit_bol (edit, start_mark);
1690 cur_bol = edit_bol (edit, end_mark - 1);
1694 edit_cursor_move (edit, cur_bol - edit->curs1);
1695 if (option_fill_tabs_with_spaces)
1696 insert_spaces_tab (edit, option_fake_half_tabs);
1697 else
1698 edit_insert (edit, '\t');
1699 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1701 if (cur_bol == 0)
1702 break;
1704 cur_bol = edit_bol (edit, cur_bol - 1);
1706 while (cur_bol >= start_bol);
1708 edit->force |= REDRAW_PAGE;
1711 /* --------------------------------------------------------------------------------------------- */
1713 static void
1714 edit_move_block_to_left (WEdit * edit)
1716 off_t start_mark, end_mark;
1717 off_t cur_bol, start_bol;
1718 int i;
1720 if (eval_marks (edit, &start_mark, &end_mark))
1721 return;
1723 start_bol = edit_bol (edit, start_mark);
1724 cur_bol = edit_bol (edit, end_mark - 1);
1728 int del_tab_width;
1729 int next_char;
1731 edit_cursor_move (edit, cur_bol - edit->curs1);
1733 if (option_fake_half_tabs)
1734 del_tab_width = HALF_TAB_SIZE;
1735 else
1736 del_tab_width = option_tab_spacing;
1738 next_char = edit_get_byte (edit, edit->curs1);
1739 if (next_char == '\t')
1740 edit_delete (edit, 1);
1741 else if (next_char == ' ')
1742 for (i = 1; i <= del_tab_width; i++)
1744 if (next_char == ' ')
1745 edit_delete (edit, 1);
1746 next_char = edit_get_byte (edit, edit->curs1);
1749 if (cur_bol == 0)
1750 break;
1752 cur_bol = edit_bol (edit, cur_bol - 1);
1754 while (cur_bol >= start_bol);
1756 edit->force |= REDRAW_PAGE;
1759 /* --------------------------------------------------------------------------------------------- */
1761 * prints at the cursor
1762 * @returns the number of chars printed
1765 static size_t
1766 edit_print_string (WEdit * e, const char *s)
1768 size_t i = 0;
1770 while (s[i] != '\0')
1771 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1772 e->force |= REDRAW_COMPLETELY;
1773 edit_update_screen (e);
1774 return i;
1777 /* --------------------------------------------------------------------------------------------- */
1778 /*** public functions ****************************************************************************/
1779 /* --------------------------------------------------------------------------------------------- */
1781 /** User edit menu, like user menu (F2) but only in editor. */
1783 void
1784 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1786 char *block_file;
1787 int nomark;
1788 off_t curs;
1789 off_t start_mark, end_mark;
1790 struct stat status;
1791 vfs_path_t *block_file_vpath;
1793 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1794 block_file_vpath = vfs_path_from_str (block_file);
1795 curs = edit->curs1;
1796 nomark = eval_marks (edit, &start_mark, &end_mark);
1797 if (nomark == 0)
1798 edit_save_block (edit, block_file, start_mark, end_mark);
1800 /* run shell scripts from menu */
1801 if (user_menu_cmd (edit, menu_file, selected_entry)
1802 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1804 int rc = 0;
1805 FILE *fd;
1807 /* i.e. we have marked block */
1808 if (nomark == 0)
1809 rc = edit_block_delete_cmd (edit);
1811 if (rc == 0)
1813 off_t ins_len;
1815 ins_len = edit_insert_file (edit, block_file_vpath);
1816 if (nomark == 0 && ins_len > 0)
1817 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1819 /* truncate block file */
1820 fd = fopen (block_file, "w");
1821 if (fd != NULL)
1822 fclose (fd);
1824 g_free (block_file);
1825 vfs_path_free (block_file_vpath);
1827 edit_cursor_move (edit, curs - edit->curs1);
1828 edit->force |= REDRAW_PAGE;
1829 send_message ((Widget *) edit, WIDGET_DRAW, 0);
1832 /* --------------------------------------------------------------------------------------------- */
1835 edit_get_byte (WEdit * edit, off_t byte_index)
1837 off_t p;
1838 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1839 return '\n';
1841 if (byte_index >= edit->curs1)
1843 p = edit->curs1 + edit->curs2 - byte_index - 1;
1844 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1846 else
1848 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1852 /* --------------------------------------------------------------------------------------------- */
1854 #ifdef HAVE_CHARSET
1856 edit_get_utf (WEdit * edit, off_t byte_index, int *char_width)
1858 gchar *str = NULL;
1859 int res = -1;
1860 gunichar ch;
1861 gchar *next_ch = NULL;
1862 int width = 0;
1863 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1865 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1867 *char_width = 0;
1868 return '\n';
1871 str = edit_get_byte_ptr (edit, byte_index);
1873 if (str == NULL)
1875 *char_width = 0;
1876 return 0;
1879 res = g_utf8_get_char_validated (str, -1);
1881 if (res < 0)
1883 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1884 int i;
1885 for (i = 0; i < UTF8_CHAR_LEN; i++)
1886 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1887 utf8_buf[UTF8_CHAR_LEN] = '\0';
1888 str = utf8_buf;
1889 res = g_utf8_get_char_validated (str, -1);
1892 if (res < 0)
1894 ch = *str;
1895 width = 0;
1897 else
1899 ch = res;
1900 /* Calculate UTF-8 char width */
1901 next_ch = g_utf8_next_char (str);
1902 if (next_ch)
1904 width = next_ch - str;
1906 else
1908 ch = 0;
1909 width = 0;
1912 *char_width = width;
1913 return ch;
1915 #endif
1917 /* --------------------------------------------------------------------------------------------- */
1919 char *
1920 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1922 int i;
1923 char *p, *writename;
1924 const vfs_path_element_t *path_element;
1926 i = edit_find_filter (filename_vpath);
1927 if (i < 0)
1928 return NULL;
1930 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1931 writename = name_quote (path_element->path, 0);
1932 p = g_strdup_printf (all_filters[i].write, writename);
1933 g_free (writename);
1934 return p;
1937 /* --------------------------------------------------------------------------------------------- */
1939 * @param edit editor object
1940 * @param f value of stream file
1941 * @returns the length of the file
1944 off_t
1945 edit_write_stream (WEdit * edit, FILE * f)
1947 long i;
1949 if (edit->lb == LB_ASIS)
1951 for (i = 0; i < edit->last_byte; i++)
1952 if (fputc (edit_get_byte (edit, i), f) < 0)
1953 break;
1954 return i;
1957 /* change line breaks */
1958 for (i = 0; i < edit->last_byte; i++)
1960 unsigned char c = edit_get_byte (edit, i);
1962 if (!(c == '\n' || c == '\r'))
1964 /* not line break */
1965 if (fputc (c, f) < 0)
1966 return i;
1968 else
1969 { /* (c == '\n' || c == '\r') */
1970 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1972 switch (edit->lb)
1974 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1975 /* put one line break unconditionally */
1976 if (fputc ('\n', f) < 0)
1977 return i;
1979 i++; /* 2 chars are processed */
1981 if (c == '\r' && c1 == '\n')
1982 /* Windows line break; go to the next char */
1983 break;
1985 if (c == '\r' && c1 == '\r')
1987 /* two Macintosh line breaks; put second line break */
1988 if (fputc ('\n', f) < 0)
1989 return i;
1990 break;
1993 if (fputc (c1, f) < 0)
1994 return i;
1995 break;
1997 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1998 /* put one line break unconditionally */
1999 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2000 return i;
2002 if (c == '\r' && c1 == '\n')
2003 /* Windows line break; go to the next char */
2004 i++;
2005 break;
2007 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2008 /* put one line break unconditionally */
2009 if (fputc ('\r', f) < 0)
2010 return i;
2012 i++; /* 2 chars are processed */
2014 if (c == '\r' && c1 == '\n')
2015 /* Windows line break; go to the next char */
2016 break;
2018 if (c == '\n' && c1 == '\n')
2020 /* two Windows line breaks; put second line break */
2021 if (fputc ('\r', f) < 0)
2022 return i;
2023 break;
2026 if (fputc (c1, f) < 0)
2027 return i;
2028 break;
2029 case LB_ASIS: /* default without changes */
2030 break;
2035 return edit->last_byte;
2038 /* --------------------------------------------------------------------------------------------- */
2040 gboolean
2041 is_break_char (char c)
2043 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2046 /* --------------------------------------------------------------------------------------------- */
2048 char *
2049 edit_get_word_from_pos (WEdit * edit, off_t start_pos, off_t * start, gsize * len, gsize * cut)
2051 off_t word_start;
2052 long cut_len = 0;
2053 GString *match_expr;
2054 unsigned char *bufpos;
2055 int c1, c2;
2057 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2059 c1 = edit_get_byte (edit, word_start);
2060 c2 = edit_get_byte (edit, word_start - 1);
2062 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2063 break;
2066 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2067 match_expr = g_string_sized_new (16);
2071 c1 = edit_get_byte (edit, word_start + match_expr->len);
2072 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2073 g_string_append_c (match_expr, c1);
2075 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2077 *len = match_expr->len;
2078 *start = word_start;
2079 *cut = cut_len;
2081 return g_string_free (match_expr, FALSE);
2084 /* --------------------------------------------------------------------------------------------- */
2085 /** inserts a file at the cursor, returns count of inserted bytes on success */
2087 long
2088 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2090 char *p;
2091 off_t ins_len = 0;
2093 p = edit_get_filter (filename_vpath);
2094 if (p != NULL)
2096 FILE *f;
2097 off_t current = edit->curs1;
2099 f = (FILE *) popen (p, "r");
2100 if (f != NULL)
2102 edit_insert_stream (edit, f);
2103 ins_len = edit->curs1 - current;
2104 edit_cursor_move (edit, -ins_len);
2105 if (pclose (f) > 0)
2107 char *errmsg;
2109 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2110 edit_error_dialog (_("Error"), errmsg);
2111 g_free (errmsg);
2112 ins_len = -1;
2115 else
2117 char *errmsg;
2119 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2120 edit_error_dialog (_("Error"), errmsg);
2121 g_free (errmsg);
2122 ins_len = -1;
2124 g_free (p);
2126 else
2128 int file;
2129 off_t blocklen;
2130 off_t current = edit->curs1;
2131 int vertical_insertion = 0;
2132 char *buf;
2134 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2135 if (file == -1)
2136 return -1;
2138 buf = g_malloc0 (TEMP_BUF_LEN);
2139 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2140 if (blocklen > 0)
2142 /* if contain signature VERTICAL_MAGIC then it vertical block */
2143 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2144 vertical_insertion = 1;
2145 else
2146 mc_lseek (file, 0, SEEK_SET);
2149 if (vertical_insertion)
2151 off_t mark1, mark2;
2152 long c1, c2;
2154 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2155 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2156 /* highlight inserted text then not persistent blocks */
2157 if (!option_persistent_selections)
2159 if (!edit->column_highlight)
2160 edit_push_undo_action (edit, COLUMN_OFF);
2161 edit->column_highlight = 1;
2164 else
2166 off_t i;
2168 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2170 for (i = 0; i < blocklen; i++)
2171 edit_insert (edit, buf[i]);
2173 /* highlight inserted text then not persistent blocks */
2174 if (!option_persistent_selections && edit->modified)
2176 edit_set_markers (edit, edit->curs1, current, 0, 0);
2177 if (edit->column_highlight)
2178 edit_push_undo_action (edit, COLUMN_ON);
2179 edit->column_highlight = 0;
2183 edit->force |= REDRAW_PAGE;
2184 ins_len = edit->curs1 - current;
2185 edit_cursor_move (edit, -ins_len);
2186 g_free (buf);
2187 mc_close (file);
2188 if (blocklen != 0)
2189 ins_len = 0;
2192 return ins_len;
2195 /* --------------------------------------------------------------------------------------------- */
2197 * Fill in the edit structure. Return NULL on failure. Pass edit as
2198 * NULL to allocate a new structure.
2200 * If line is 0, try to restore saved position. Otherwise put the
2201 * cursor on that line and show it in the middle of the screen.
2204 WEdit *
2205 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2206 long line)
2208 gboolean to_free = FALSE;
2210 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2211 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2213 if (edit != NULL)
2214 edit_purge_widget (edit);
2215 else
2217 #ifdef ENABLE_NLS
2219 * Expand option_whole_chars_search by national letters using
2220 * current locale
2223 static char option_whole_chars_search_buf[256];
2225 if (option_whole_chars_search_buf != option_whole_chars_search)
2227 size_t i;
2228 size_t len = str_term_width1 (option_whole_chars_search);
2230 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2232 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2234 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2236 option_whole_chars_search_buf[len++] = i;
2240 option_whole_chars_search_buf[len] = 0;
2241 option_whole_chars_search = option_whole_chars_search_buf;
2243 #endif /* ENABLE_NLS */
2244 edit = g_malloc0 (sizeof (WEdit));
2245 to_free = TRUE;
2248 edit->drag_state = MCEDIT_DRAG_NORMAL;
2249 edit->widget.y = y;
2250 edit->widget.x = x;
2251 edit->widget.lines = lines;
2252 edit->widget.cols = cols;
2253 edit_save_size (edit);
2254 edit->fullscreen = TRUE;
2256 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2257 edit->stat1.st_uid = getuid ();
2258 edit->stat1.st_gid = getgid ();
2259 edit->stat1.st_mtime = 0;
2261 edit->over_col = 0;
2262 edit->bracket = -1;
2263 edit->force |= REDRAW_PAGE;
2265 /* set file name before load file */
2266 edit_set_filename (edit, filename_vpath);
2268 edit->undo_stack_size = START_STACK_SIZE;
2269 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2270 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2272 edit->redo_stack_size = START_STACK_SIZE;
2273 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2274 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2276 #ifdef HAVE_CHARSET
2277 edit->utf8 = FALSE;
2278 edit->converter = str_cnv_from_term;
2279 edit_set_codeset (edit);
2280 #endif
2282 if (!edit_load_file (edit))
2284 /* edit_load_file already gives an error message */
2285 if (to_free)
2286 g_free (edit);
2287 return NULL;
2290 edit->loading_done = 1;
2291 edit->modified = 0;
2292 edit->locked = 0;
2293 edit_load_syntax (edit, NULL, NULL);
2295 int color;
2296 edit_get_syntax_color (edit, -1, &color);
2299 /* load saved cursor position */
2300 if ((line == 0) && option_save_position)
2301 edit_load_position (edit);
2302 else
2304 if (line <= 0)
2305 line = 1;
2306 edit_move_display (edit, line - 1);
2307 edit_move_to_line (edit, line - 1);
2310 edit_load_macro_cmd (edit);
2312 return edit;
2315 /* --------------------------------------------------------------------------------------------- */
2317 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2318 gboolean
2319 edit_clean (WEdit * edit)
2321 int j = 0;
2323 if (edit == NULL)
2324 return FALSE;
2326 /* a stale lock, remove it */
2327 if (edit->locked)
2328 edit->locked = unlock_file (edit->filename_vpath);
2330 /* save cursor position */
2331 if (option_save_position)
2332 edit_save_position (edit);
2333 else if (edit->serialized_bookmarks != NULL)
2334 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2336 /* File specified on the mcedit command line and never saved */
2337 if (edit->delete_file)
2338 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2340 edit_free_syntax_rules (edit);
2341 book_mark_flush (edit, -1);
2342 for (; j <= MAXBUFF; j++)
2344 g_free (edit->buffers1[j]);
2345 g_free (edit->buffers2[j]);
2348 g_free (edit->undo_stack);
2349 g_free (edit->redo_stack);
2350 vfs_path_free (edit->filename_vpath);
2351 vfs_path_free (edit->dir_vpath);
2352 mc_search_free (edit->search);
2353 edit->search = NULL;
2355 #ifdef HAVE_CHARSET
2356 if (edit->converter != str_cnv_from_term)
2357 str_close_conv (edit->converter);
2358 #endif
2360 edit_purge_widget (edit);
2362 return TRUE;
2365 /* --------------------------------------------------------------------------------------------- */
2368 * Load a new file into the editor and set line. If it fails, preserve the old file.
2369 * To do it, allocate a new widget, initialize it and, if the new file
2370 * was loaded, copy the data to the old widget.
2372 * @returns TRUE on success, FALSE on failure.
2374 gboolean
2375 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2377 WEdit *e;
2378 int y = edit->widget.y;
2379 int x = edit->widget.x;
2380 int lines = edit->widget.lines;
2381 int columns = edit->widget.cols;
2383 e = g_malloc0 (sizeof (WEdit));
2384 e->widget = edit->widget;
2386 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2388 g_free (e);
2389 return FALSE;
2392 edit_clean (edit);
2393 memcpy (edit, e, sizeof (WEdit));
2394 g_free (e);
2396 return TRUE;
2399 /* --------------------------------------------------------------------------------------------- */
2401 #ifdef HAVE_CHARSET
2402 void
2403 edit_set_codeset (WEdit * edit)
2405 const char *cp_id;
2407 cp_id =
2408 get_codepage_id (mc_global.source_codepage >=
2409 0 ? mc_global.source_codepage : mc_global.display_codepage);
2411 if (cp_id != NULL)
2413 GIConv conv;
2414 conv = str_crt_conv_from (cp_id);
2415 if (conv != INVALID_CONV)
2417 if (edit->converter != str_cnv_from_term)
2418 str_close_conv (edit->converter);
2419 edit->converter = conv;
2423 if (cp_id != NULL)
2424 edit->utf8 = str_isutf8 (cp_id);
2426 #endif
2429 /* --------------------------------------------------------------------------------------------- */
2431 Recording stack for undo:
2432 The following is an implementation of a compressed stack. Identical
2433 pushes are recorded by a negative prefix indicating the number of times the
2434 same char was pushed. This saves space for repeated curs-left or curs-right
2435 delete etc.
2439 pushed: stored:
2443 b -3
2445 c --> -4
2451 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2452 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2453 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2454 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2455 position.
2457 The only way the cursor moves or the buffer is changed is through the routines:
2458 insert, backspace, insert_ahead, delete, and cursor_move.
2459 These record the reverse undo movements onto the stack each time they are
2460 called.
2462 Each key press results in a set of actions (insert; delete ...). So each time
2463 a key is pressed the current position of start_display is pushed as
2464 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2465 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2466 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2470 void
2471 edit_push_undo_action (WEdit * edit, long c, ...)
2473 unsigned long sp = edit->undo_stack_pointer;
2474 unsigned long spm1;
2475 long *t;
2477 /* first enlarge the stack if necessary */
2478 if (sp > edit->undo_stack_size - 10)
2479 { /* say */
2480 if (option_max_undo < 256)
2481 option_max_undo = 256;
2482 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2484 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2485 if (t)
2487 edit->undo_stack = t;
2488 edit->undo_stack_size <<= 1;
2489 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2493 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2494 if (edit->undo_stack_disable)
2496 edit_push_redo_action (edit, KEY_PRESS);
2497 edit_push_redo_action (edit, c);
2498 return;
2500 else if (edit->redo_stack_reset)
2502 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2505 if (edit->undo_stack_bottom != sp
2506 && spm1 != edit->undo_stack_bottom
2507 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2509 int d;
2510 if (edit->undo_stack[spm1] < 0)
2512 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2513 if (d == c)
2515 if (edit->undo_stack[spm1] > -1000000000)
2517 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2519 edit->undo_stack[spm1]--;
2521 return;
2525 else
2527 d = edit->undo_stack[spm1];
2528 if (d == c)
2530 if (c >= KEY_PRESS)
2531 return; /* --> no need to push multiple do-nothings */
2532 edit->undo_stack[sp] = -2;
2533 goto check_bottom;
2537 edit->undo_stack[sp] = c;
2539 check_bottom:
2540 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2542 /* if the sp wraps round and catches the undo_stack_bottom then erase
2543 * the first set of actions on the stack to make space - by moving
2544 * undo_stack_bottom forward one "key press" */
2545 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2546 if ((unsigned long) c == edit->undo_stack_bottom ||
2547 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2550 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2552 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2553 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2555 /*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: */
2556 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2557 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2559 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2563 void
2564 edit_push_redo_action (WEdit * edit, long c, ...)
2566 unsigned long sp = edit->redo_stack_pointer;
2567 unsigned long spm1;
2568 long *t;
2569 /* first enlarge the stack if necessary */
2570 if (sp > edit->redo_stack_size - 10)
2571 { /* say */
2572 if (option_max_undo < 256)
2573 option_max_undo = 256;
2574 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2576 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2577 if (t)
2579 edit->redo_stack = t;
2580 edit->redo_stack_size <<= 1;
2581 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2585 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2587 if (edit->redo_stack_bottom != sp
2588 && spm1 != edit->redo_stack_bottom
2589 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2591 int d;
2592 if (edit->redo_stack[spm1] < 0)
2594 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2595 if (d == c)
2597 if (edit->redo_stack[spm1] > -1000000000)
2599 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2600 edit->redo_stack[spm1]--;
2601 return;
2605 else
2607 d = edit->redo_stack[spm1];
2608 if (d == c)
2610 if (c >= KEY_PRESS)
2611 return; /* --> no need to push multiple do-nothings */
2612 edit->redo_stack[sp] = -2;
2613 goto redo_check_bottom;
2617 edit->redo_stack[sp] = c;
2619 redo_check_bottom:
2620 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2622 /* if the sp wraps round and catches the redo_stack_bottom then erase
2623 * the first set of actions on the stack to make space - by moving
2624 * redo_stack_bottom forward one "key press" */
2625 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2626 if ((unsigned long) c == edit->redo_stack_bottom ||
2627 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2630 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2632 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2633 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2636 * If a single key produced enough pushes to wrap all the way round then
2637 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2638 * The stack is then initialised:
2641 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2642 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2643 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2647 /* --------------------------------------------------------------------------------------------- */
2649 Basic low level single character buffer alterations and movements at the cursor.
2650 Returns char passed over, inserted or removed.
2653 void
2654 edit_insert (WEdit * edit, int c)
2656 /* check if file has grown to large */
2657 if (edit->last_byte >= SIZE_LIMIT)
2658 return;
2660 /* first we must update the position of the display window */
2661 if (edit->curs1 < edit->start_display)
2663 edit->start_display++;
2664 if (c == '\n')
2665 edit->start_line++;
2668 /* Mark file as modified, unless the file hasn't been fully loaded */
2669 if (edit->loading_done)
2671 edit_modification (edit);
2674 /* now we must update some info on the file and check if a redraw is required */
2675 if (c == '\n')
2677 if (edit->book_mark)
2678 book_mark_inc (edit, edit->curs_line);
2679 edit->curs_line++;
2680 edit->total_lines++;
2681 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2684 /* save the reverse command onto the undo stack */
2685 /* ordinary char and not space */
2686 if (c > 32)
2687 edit_push_undo_action (edit, BACKSPACE);
2688 else
2689 edit_push_undo_action (edit, BACKSPACE_BR);
2690 /* update markers */
2691 edit->mark1 += (edit->mark1 > edit->curs1);
2692 edit->mark2 += (edit->mark2 > edit->curs1);
2693 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2695 /* add a new buffer if we've reached the end of the last one */
2696 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2697 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2699 /* perform the insertion */
2700 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2701 = (unsigned char) c;
2703 /* update file length */
2704 edit->last_byte++;
2706 /* update cursor position */
2707 edit->curs1++;
2710 /* --------------------------------------------------------------------------------------------- */
2711 /** same as edit_insert and move left */
2713 void
2714 edit_insert_ahead (WEdit * edit, int c)
2716 if (edit->last_byte >= SIZE_LIMIT)
2717 return;
2719 if (edit->curs1 < edit->start_display)
2721 edit->start_display++;
2722 if (c == '\n')
2723 edit->start_line++;
2725 edit_modification (edit);
2726 if (c == '\n')
2728 if (edit->book_mark)
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, const int byte_delete)
2758 int p = 0;
2759 int cw = 1;
2760 int i;
2762 if (!edit->curs2)
2763 return 0;
2765 #ifdef HAVE_CHARSET
2766 /* if byte_delete = 1 then delete only one byte not multibyte char */
2767 if (edit->utf8 && byte_delete == 0)
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 if (edit->book_mark)
2810 book_mark_dec (edit, edit->curs_line);
2811 edit->total_lines--;
2812 edit->force |= REDRAW_AFTER_CURSOR;
2814 if (edit->curs1 < edit->start_display)
2816 edit->start_display--;
2817 if (p == '\n')
2818 edit->start_line--;
2821 return p;
2824 /* --------------------------------------------------------------------------------------------- */
2827 edit_backspace (WEdit * edit, const int byte_delete)
2829 int p = 0;
2830 int cw = 1;
2831 int i;
2833 if (edit->curs1 == 0)
2834 return 0;
2836 if (edit->mark2 != edit->mark1)
2837 edit_push_markers (edit);
2839 #ifdef HAVE_CHARSET
2840 if (edit->utf8 && byte_delete == 0)
2842 edit_get_prev_utf (edit, edit->curs1, &cw);
2843 if (cw < 1)
2844 cw = 1;
2846 #else
2847 (void) byte_delete;
2848 #endif
2850 for (i = 1; i <= cw; i++)
2852 if (edit->mark1 >= edit->curs1)
2854 edit->mark1--;
2855 edit->end_mark_curs--;
2857 if (edit->mark2 >= edit->curs1)
2858 edit->mark2--;
2859 if (edit->last_get_rule >= edit->curs1)
2860 edit->last_get_rule--;
2862 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2863 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2864 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2866 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2867 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2869 edit->last_byte--;
2870 edit->curs1--;
2871 edit_push_undo_action (edit, p);
2873 edit_modification (edit);
2874 if (p == '\n')
2876 if (edit->book_mark)
2877 book_mark_dec (edit, edit->curs_line);
2878 edit->curs_line--;
2879 edit->total_lines--;
2880 edit->force |= REDRAW_AFTER_CURSOR;
2883 if (edit->curs1 < edit->start_display)
2885 edit->start_display--;
2886 if (p == '\n')
2887 edit->start_line--;
2890 return p;
2893 /* --------------------------------------------------------------------------------------------- */
2894 /** moves the cursor right or left: increment positive or negative respectively */
2896 void
2897 edit_cursor_move (WEdit * edit, off_t increment)
2899 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2900 int c;
2902 if (increment < 0)
2904 for (; increment < 0; increment++)
2906 if (edit->curs1 == 0)
2907 return;
2909 edit_push_undo_action (edit, CURS_RIGHT);
2911 c = edit_get_byte (edit, edit->curs1 - 1);
2912 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2913 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2914 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2915 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2916 edit->curs2++;
2917 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2918 1) & M_EDIT_BUF_SIZE];
2919 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2921 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2922 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2924 edit->curs1--;
2925 if (c == '\n')
2927 edit->curs_line--;
2928 edit->force |= REDRAW_LINE_BELOW;
2933 else if (increment > 0)
2935 for (; increment > 0; increment--)
2937 if (edit->curs2 == 0)
2938 return;
2940 edit_push_undo_action (edit, CURS_LEFT);
2942 c = edit_get_byte (edit, edit->curs1);
2943 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2944 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2945 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2946 edit->curs1++;
2947 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2948 ((edit->curs2 -
2949 1) & M_EDIT_BUF_SIZE) - 1];
2950 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2952 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2953 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2955 edit->curs2--;
2956 if (c == '\n')
2958 edit->curs_line++;
2959 edit->force |= REDRAW_LINE_ABOVE;
2965 /* These functions return positions relative to lines */
2967 /* --------------------------------------------------------------------------------------------- */
2968 /** returns index of last char on line + 1 */
2970 off_t
2971 edit_eol (WEdit * edit, off_t current)
2973 if (current >= edit->last_byte)
2974 return edit->last_byte;
2976 for (;; current++)
2977 if (edit_get_byte (edit, current) == '\n')
2978 break;
2979 return current;
2982 /* --------------------------------------------------------------------------------------------- */
2983 /** returns index of first char on line */
2985 off_t
2986 edit_bol (WEdit * edit, off_t current)
2988 if (current <= 0)
2989 return 0;
2991 for (;; current--)
2992 if (edit_get_byte (edit, current - 1) == '\n')
2993 break;
2994 return current;
2997 /* --------------------------------------------------------------------------------------------- */
2999 long
3000 edit_count_lines (WEdit * edit, off_t current, off_t upto)
3002 long lines = 0;
3003 if (upto > edit->last_byte)
3004 upto = edit->last_byte;
3005 if (current < 0)
3006 current = 0;
3007 while (current < upto)
3008 if (edit_get_byte (edit, current++) == '\n')
3009 lines++;
3010 return lines;
3013 /* --------------------------------------------------------------------------------------------- */
3014 /* If lines is zero this returns the count of lines from current to upto. */
3015 /* If upto is zero returns index of lines forward current. */
3017 off_t
3018 edit_move_forward (WEdit * edit, off_t current, long lines, off_t upto)
3020 if (upto != 0)
3022 return (off_t) edit_count_lines (edit, current, upto);
3024 else
3026 long next;
3027 if (lines < 0)
3028 lines = 0;
3029 while (lines--)
3031 next = edit_eol (edit, current) + 1;
3032 if (next > edit->last_byte)
3033 break;
3034 else
3035 current = next;
3037 return current;
3041 /* --------------------------------------------------------------------------------------------- */
3042 /** Returns offset of 'lines' lines up from current */
3044 off_t
3045 edit_move_backward (WEdit * edit, off_t current, long lines)
3047 if (lines < 0)
3048 lines = 0;
3049 current = edit_bol (edit, current);
3050 while ((lines--) && current != 0)
3051 current = edit_bol (edit, current - 1);
3052 return current;
3055 /* --------------------------------------------------------------------------------------------- */
3056 /* If cols is zero this returns the count of columns from current to upto. */
3057 /* If upto is zero returns index of cols across from current. */
3059 off_t
3060 edit_move_forward3 (WEdit * edit, off_t current, long cols, off_t upto)
3062 off_t p, q;
3063 long col;
3065 if (upto != 0)
3067 q = upto;
3068 cols = -10;
3070 else
3071 q = edit->last_byte + 2;
3073 for (col = 0, p = current; p < q; p++)
3075 int c, orig_c;
3077 if (cols != -10)
3079 if (col == cols)
3080 return p;
3081 if (col > cols)
3082 return p - 1;
3085 orig_c = c = edit_get_byte (edit, p);
3087 #ifdef HAVE_CHARSET
3088 if (edit->utf8)
3090 int utf_ch;
3091 int cw = 1;
3093 utf_ch = edit_get_utf (edit, p, &cw);
3094 if (mc_global.utf8_display)
3096 if (cw > 1)
3097 col -= cw - 1;
3098 if (g_unichar_iswide (utf_ch))
3099 col++;
3101 else if (cw > 1 && g_unichar_isprint (utf_ch))
3102 col -= cw - 1;
3105 c = convert_to_display_c (c);
3106 #endif
3108 if (c == '\t')
3109 col += TAB_SIZE - col % TAB_SIZE;
3110 else if (c == '\n')
3112 if (upto != 0)
3113 return (off_t) col;
3114 else
3115 return p;
3117 else if ((c < 32 || c == 127) && (orig_c == c
3118 #ifdef HAVE_CHARSET
3119 || (!mc_global.utf8_display && !edit->utf8)
3120 #endif
3122 /* '\r' is shown as ^M, so we must advance 2 characters */
3123 /* Caret notation for control characters */
3124 col += 2;
3125 else
3126 col++;
3128 return (off_t) col;
3131 /* --------------------------------------------------------------------------------------------- */
3132 /** returns the current column position of the cursor */
3134 long
3135 edit_get_col (WEdit * edit)
3137 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3140 /* --------------------------------------------------------------------------------------------- */
3141 /* Scrolling functions */
3142 /* --------------------------------------------------------------------------------------------- */
3144 void
3145 edit_update_curs_row (WEdit * edit)
3147 edit->curs_row = edit->curs_line - edit->start_line;
3150 /* --------------------------------------------------------------------------------------------- */
3152 void
3153 edit_update_curs_col (WEdit * edit)
3155 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3158 /* --------------------------------------------------------------------------------------------- */
3160 long
3161 edit_get_curs_col (const WEdit * edit)
3163 return edit->curs_col;
3166 /* --------------------------------------------------------------------------------------------- */
3167 /** moves the display start position up by i lines */
3169 void
3170 edit_scroll_upward (WEdit * edit, long i)
3172 long lines_above = edit->start_line;
3174 if (i > lines_above)
3175 i = lines_above;
3176 if (i != 0)
3178 edit->start_line -= i;
3179 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3180 edit->force |= REDRAW_PAGE;
3181 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3183 edit_update_curs_row (edit);
3187 /* --------------------------------------------------------------------------------------------- */
3189 void
3190 edit_scroll_downward (WEdit * edit, long i)
3192 long lines_below;
3194 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3195 if (lines_below > 0)
3197 if (i > lines_below)
3198 i = lines_below;
3199 edit->start_line += i;
3200 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3201 edit->force |= REDRAW_PAGE;
3202 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3204 edit_update_curs_row (edit);
3207 /* --------------------------------------------------------------------------------------------- */
3209 void
3210 edit_scroll_right (WEdit * edit, long i)
3212 edit->force |= REDRAW_PAGE;
3213 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3214 edit->start_col -= i;
3217 /* --------------------------------------------------------------------------------------------- */
3219 void
3220 edit_scroll_left (WEdit * edit, long i)
3222 if (edit->start_col)
3224 edit->start_col += i;
3225 if (edit->start_col > 0)
3226 edit->start_col = 0;
3227 edit->force |= REDRAW_PAGE;
3228 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3232 /* --------------------------------------------------------------------------------------------- */
3233 /* high level cursor movement commands */
3234 /* --------------------------------------------------------------------------------------------- */
3236 void
3237 edit_move_to_prev_col (WEdit * edit, off_t p)
3239 long prev = edit->prev_col;
3240 long over = edit->over_col;
3242 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3244 if (option_cursor_beyond_eol)
3246 long line_len;
3248 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3249 edit_eol (edit, edit->curs1));
3250 if (line_len < prev + edit->over_col)
3252 edit->over_col = prev + over - line_len;
3253 edit->prev_col = line_len;
3254 edit->curs_col = line_len;
3256 else
3258 edit->curs_col = prev + over;
3259 edit->prev_col = edit->curs_col;
3260 edit->over_col = 0;
3263 else
3265 edit->over_col = 0;
3266 if (is_in_indent (edit) && option_fake_half_tabs)
3268 edit_update_curs_col (edit);
3269 if (space_width)
3271 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3273 int q;
3275 q = edit->curs_col;
3276 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3277 p = edit_bol (edit, edit->curs1);
3279 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0)
3280 - edit->curs1);
3282 if (!left_of_four_spaces (edit))
3283 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3290 /* --------------------------------------------------------------------------------------------- */
3291 /** check whether line in editor is blank or not
3293 * @param edit editor object
3294 * @param line number of line
3296 * @return TRUE if line in blank, FALSE otherwise
3299 gboolean
3300 line_is_blank (WEdit * edit, long line)
3302 return is_blank (edit, edit_find_line (edit, line));
3305 /* --------------------------------------------------------------------------------------------- */
3306 /** move cursor to line 'line' */
3308 void
3309 edit_move_to_line (WEdit * e, long line)
3311 if (line < e->curs_line)
3312 edit_move_up (e, e->curs_line - line, 0);
3313 else
3314 edit_move_down (e, line - e->curs_line, 0);
3315 edit_scroll_screen_over_cursor (e);
3318 /* --------------------------------------------------------------------------------------------- */
3319 /** scroll window so that first visible line is 'line' */
3321 void
3322 edit_move_display (WEdit * e, long line)
3324 if (line < e->start_line)
3325 edit_scroll_upward (e, e->start_line - line);
3326 else
3327 edit_scroll_downward (e, line - e->start_line);
3330 /* --------------------------------------------------------------------------------------------- */
3331 /** save markers onto undo stack */
3333 void
3334 edit_push_markers (WEdit * edit)
3336 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3337 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3338 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3341 /* --------------------------------------------------------------------------------------------- */
3343 void
3344 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3346 edit->mark1 = m1;
3347 edit->mark2 = m2;
3348 edit->column1 = c1;
3349 edit->column2 = c2;
3353 /* --------------------------------------------------------------------------------------------- */
3354 /** highlight marker toggle */
3356 void
3357 edit_mark_cmd (WEdit * edit, gboolean unmark)
3359 edit_push_markers (edit);
3360 if (unmark)
3362 edit_set_markers (edit, 0, 0, 0, 0);
3363 edit->force |= REDRAW_PAGE;
3365 else
3367 if (edit->mark2 >= 0)
3369 edit->end_mark_curs = -1;
3370 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3371 edit->curs_col + edit->over_col);
3372 edit->force |= REDRAW_PAGE;
3374 else
3376 edit->end_mark_curs = edit->curs1;
3377 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3378 edit->curs_col + edit->over_col);
3383 /* --------------------------------------------------------------------------------------------- */
3384 /** highlight the word under cursor */
3386 void
3387 edit_mark_current_word_cmd (WEdit * edit)
3389 long pos;
3391 for (pos = edit->curs1; pos != 0; pos--)
3393 int c1, c2;
3395 c1 = edit_get_byte (edit, pos);
3396 c2 = edit_get_byte (edit, pos - 1);
3397 if (!isspace (c1) && isspace (c2))
3398 break;
3399 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3400 break;
3402 edit->mark1 = pos;
3404 for (; pos < edit->last_byte; pos++)
3406 int c1, c2;
3408 c1 = edit_get_byte (edit, pos);
3409 c2 = edit_get_byte (edit, pos + 1);
3410 if (!isspace (c1) && isspace (c2))
3411 break;
3412 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3413 break;
3415 edit->mark2 = min (pos + 1, edit->last_byte);
3417 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3420 /* --------------------------------------------------------------------------------------------- */
3422 void
3423 edit_mark_current_line_cmd (WEdit * edit)
3425 long pos = edit->curs1;
3427 edit->mark1 = edit_bol (edit, pos);
3428 edit->mark2 = edit_eol (edit, pos);
3430 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3433 /* --------------------------------------------------------------------------------------------- */
3435 void
3436 edit_delete_line (WEdit * edit)
3439 * Delete right part of the line.
3440 * Note that edit_get_byte() returns '\n' when byte position is
3441 * beyond EOF.
3443 while (edit_get_byte (edit, edit->curs1) != '\n')
3445 (void) edit_delete (edit, 1);
3449 * Delete '\n' char.
3450 * Note that edit_delete() will not corrupt anything if called while
3451 * cursor position is EOF.
3453 (void) edit_delete (edit, 1);
3456 * Delete left part of the line.
3457 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3459 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3461 (void) edit_backspace (edit, 1);
3465 /* --------------------------------------------------------------------------------------------- */
3467 long
3468 edit_indent_width (WEdit * edit, off_t p)
3470 off_t q = p;
3472 /* move to the end of the leading whitespace of the line */
3473 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3474 q++;
3475 /* count the number of columns of indentation */
3476 return (long) edit_move_forward3 (edit, p, 0, q);
3479 /* --------------------------------------------------------------------------------------------- */
3481 void
3482 edit_insert_indent (WEdit * edit, int indent)
3484 if (!option_fill_tabs_with_spaces)
3486 while (indent >= TAB_SIZE)
3488 edit_insert (edit, '\t');
3489 indent -= TAB_SIZE;
3492 while (indent-- > 0)
3493 edit_insert (edit, ' ');
3496 /* --------------------------------------------------------------------------------------------- */
3498 void
3499 edit_push_key_press (WEdit * edit)
3501 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3502 if (edit->mark2 == -1)
3504 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3505 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3509 /* --------------------------------------------------------------------------------------------- */
3511 void
3512 edit_find_bracket (WEdit * edit)
3514 edit->bracket = edit_get_bracket (edit, 1, 10000);
3515 if (last_bracket != edit->bracket)
3516 edit->force |= REDRAW_PAGE;
3517 last_bracket = edit->bracket;
3520 /* --------------------------------------------------------------------------------------------- */
3522 * This executes a command as though the user initiated it through a key
3523 * press. Callback with WIDGET_KEY as a message calls this after
3524 * translating the key press. This function can be used to pass any
3525 * command to the editor. Note that the screen wouldn't update
3526 * automatically. Either of command or char_for_insertion must be
3527 * passed as -1. Commands are executed, and char_for_insertion is
3528 * inserted at the cursor.
3531 void
3532 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3534 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3535 || (macro_index < 0
3536 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3538 macro_index = 0;
3539 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3540 return;
3542 if (macro_index != -1)
3544 edit->force |= REDRAW_COMPLETELY;
3545 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3547 edit_store_macro_cmd (edit);
3548 macro_index = -1;
3549 return;
3551 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3553 edit_repeat_macro_cmd (edit);
3554 macro_index = -1;
3555 return;
3559 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3561 record_macro_buf[macro_index].action = command;
3562 record_macro_buf[macro_index++].ch = char_for_insertion;
3564 /* record the beginning of a set of editing actions initiated by a key press */
3565 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3566 edit_push_key_press (edit);
3568 edit_execute_cmd (edit, command, char_for_insertion);
3569 if (edit->column_highlight)
3570 edit->force |= REDRAW_PAGE;
3573 /* --------------------------------------------------------------------------------------------- */
3575 This executes a command at a lower level than macro recording.
3576 It also does not push a key_press onto the undo stack. This means
3577 that if it is called many times, a single undo command will undo
3578 all of them. It also does not check for the Undo command.
3580 void
3581 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3583 if (command == CK_WindowFullscreen)
3585 edit_toggle_fullscreen (edit);
3586 return;
3589 /* handle window state */
3590 if (edit_handle_move_resize (edit, command))
3591 return;
3593 edit->force |= REDRAW_LINE;
3595 /* The next key press will unhighlight the found string, so update
3596 * the whole page */
3597 if (edit->found_len || edit->column_highlight)
3598 edit->force |= REDRAW_PAGE;
3600 switch (command)
3602 /* a mark command with shift-arrow */
3603 case CK_MarkLeft:
3604 case CK_MarkRight:
3605 case CK_MarkToWordBegin:
3606 case CK_MarkToWordEnd:
3607 case CK_MarkToHome:
3608 case CK_MarkToEnd:
3609 case CK_MarkUp:
3610 case CK_MarkDown:
3611 case CK_MarkPageUp:
3612 case CK_MarkPageDown:
3613 case CK_MarkToFileBegin:
3614 case CK_MarkToFileEnd:
3615 case CK_MarkToPageBegin:
3616 case CK_MarkToPageEnd:
3617 case CK_MarkScrollUp:
3618 case CK_MarkScrollDown:
3619 case CK_MarkParagraphUp:
3620 case CK_MarkParagraphDown:
3621 /* a mark command with alt-arrow */
3622 case CK_MarkColumnPageUp:
3623 case CK_MarkColumnPageDown:
3624 case CK_MarkColumnLeft:
3625 case CK_MarkColumnRight:
3626 case CK_MarkColumnUp:
3627 case CK_MarkColumnDown:
3628 case CK_MarkColumnScrollUp:
3629 case CK_MarkColumnScrollDown:
3630 case CK_MarkColumnParagraphUp:
3631 case CK_MarkColumnParagraphDown:
3632 edit->column_highlight = 0;
3633 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3635 edit_mark_cmd (edit, TRUE); /* clear */
3636 edit_mark_cmd (edit, FALSE); /* marking on */
3638 edit->highlight = 1;
3639 break;
3641 /* any other command */
3642 default:
3643 if (edit->highlight)
3644 edit_mark_cmd (edit, FALSE); /* clear */
3645 edit->highlight = 0;
3648 /* first check for undo */
3649 if (command == CK_Undo)
3651 edit->redo_stack_reset = 0;
3652 edit_group_undo (edit);
3653 edit->found_len = 0;
3654 edit->prev_col = edit_get_col (edit);
3655 edit->search_start = edit->curs1;
3656 return;
3658 /* check for redo */
3659 if (command == CK_Redo)
3661 edit->redo_stack_reset = 0;
3662 edit_do_redo (edit);
3663 edit->found_len = 0;
3664 edit->prev_col = edit_get_col (edit);
3665 edit->search_start = edit->curs1;
3666 return;
3669 edit->redo_stack_reset = 1;
3671 /* An ordinary key press */
3672 if (char_for_insertion >= 0)
3674 /* if non persistent selection and text selected */
3675 if (!option_persistent_selections)
3677 if (edit->mark1 != edit->mark2)
3678 edit_block_delete_cmd (edit);
3680 if (edit->overwrite)
3682 /* remove char only one time, after input first byte, multibyte chars */
3683 #ifdef HAVE_CHARSET
3684 if (!mc_global.utf8_display || edit->charpoint == 0)
3685 #endif
3686 if (edit_get_byte (edit, edit->curs1) != '\n')
3688 edit_delete (edit, 0);
3690 if (option_cursor_beyond_eol && edit->over_col > 0)
3691 edit_insert_over (edit);
3692 #ifdef HAVE_CHARSET
3693 if (char_for_insertion > 255 && !mc_global.utf8_display)
3695 unsigned char str[6 + 1];
3696 size_t i = 0;
3697 int res;
3699 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3700 if (res == 0)
3702 str[0] = '.';
3703 str[1] = '\0';
3705 else
3707 str[res] = '\0';
3709 while (str[i] != 0 && i <= 6)
3711 char_for_insertion = str[i];
3712 edit_insert (edit, char_for_insertion);
3713 i++;
3716 else
3717 #endif
3718 edit_insert (edit, char_for_insertion);
3720 if (option_auto_para_formatting)
3722 format_paragraph (edit, 0);
3723 edit->force |= REDRAW_PAGE;
3725 else
3726 check_and_wrap_line (edit);
3727 edit->found_len = 0;
3728 edit->prev_col = edit_get_col (edit);
3729 edit->search_start = edit->curs1;
3730 edit_find_bracket (edit);
3731 return;
3734 switch (command)
3736 case CK_TopOnScreen:
3737 case CK_BottomOnScreen:
3738 case CK_Top:
3739 case CK_Bottom:
3740 case CK_PageUp:
3741 case CK_PageDown:
3742 case CK_Home:
3743 case CK_End:
3744 case CK_Up:
3745 case CK_Down:
3746 case CK_Left:
3747 case CK_Right:
3748 case CK_WordLeft:
3749 case CK_WordRight:
3750 if (edit->mark2 >= 0)
3752 if (!option_persistent_selections)
3754 if (edit->column_highlight)
3755 edit_push_undo_action (edit, COLUMN_ON);
3756 edit->column_highlight = 0;
3757 edit_mark_cmd (edit, TRUE);
3762 switch (command)
3764 case CK_TopOnScreen:
3765 case CK_BottomOnScreen:
3766 case CK_MarkToPageBegin:
3767 case CK_MarkToPageEnd:
3768 case CK_Up:
3769 case CK_Down:
3770 case CK_WordLeft:
3771 case CK_WordRight:
3772 case CK_MarkToWordBegin:
3773 case CK_MarkToWordEnd:
3774 case CK_MarkUp:
3775 case CK_MarkDown:
3776 case CK_MarkColumnUp:
3777 case CK_MarkColumnDown:
3778 if (edit->mark2 == -1)
3779 break; /*marking is following the cursor: may need to highlight a whole line */
3780 case CK_Left:
3781 case CK_Right:
3782 case CK_MarkLeft:
3783 case CK_MarkRight:
3784 edit->force |= REDRAW_CHAR_ONLY;
3787 /* basic cursor key commands */
3788 switch (command)
3790 case CK_BackSpace:
3791 /* if non persistent selection and text selected */
3792 if (!option_persistent_selections)
3794 if (edit->mark1 != edit->mark2)
3796 edit_block_delete_cmd (edit);
3797 break;
3800 if (option_cursor_beyond_eol && edit->over_col > 0)
3802 edit->over_col--;
3803 break;
3805 if (option_backspace_through_tabs && is_in_indent (edit))
3807 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3808 edit_backspace (edit, 1);
3809 break;
3811 else
3813 if (option_fake_half_tabs)
3815 int i;
3816 if (is_in_indent (edit) && right_of_four_spaces (edit))
3818 for (i = 0; i < HALF_TAB_SIZE; i++)
3819 edit_backspace (edit, 1);
3820 break;
3824 edit_backspace (edit, 0);
3825 break;
3826 case CK_Delete:
3827 /* if non persistent selection and text selected */
3828 if (!option_persistent_selections)
3830 if (edit->mark1 != edit->mark2)
3832 edit_block_delete_cmd (edit);
3833 break;
3837 if (option_cursor_beyond_eol && edit->over_col > 0)
3838 edit_insert_over (edit);
3840 if (option_fake_half_tabs)
3842 int i;
3843 if (is_in_indent (edit) && left_of_four_spaces (edit))
3845 for (i = 1; i <= HALF_TAB_SIZE; i++)
3846 edit_delete (edit, 1);
3847 break;
3850 edit_delete (edit, 0);
3851 break;
3852 case CK_DeleteToWordBegin:
3853 edit->over_col = 0;
3854 edit_left_delete_word (edit);
3855 break;
3856 case CK_DeleteToWordEnd:
3857 if (option_cursor_beyond_eol && edit->over_col > 0)
3858 edit_insert_over (edit);
3860 edit_right_delete_word (edit);
3861 break;
3862 case CK_DeleteLine:
3863 edit_delete_line (edit);
3864 break;
3865 case CK_DeleteToHome:
3866 edit_delete_to_line_begin (edit);
3867 break;
3868 case CK_DeleteToEnd:
3869 edit_delete_to_line_end (edit);
3870 break;
3871 case CK_Enter:
3872 edit->over_col = 0;
3873 if (option_auto_para_formatting)
3875 edit_double_newline (edit);
3876 if (option_return_does_auto_indent)
3877 edit_auto_indent (edit);
3878 format_paragraph (edit, 0);
3880 else
3882 edit_insert (edit, '\n');
3883 if (option_return_does_auto_indent)
3885 edit_auto_indent (edit);
3888 break;
3889 case CK_Return:
3890 edit_insert (edit, '\n');
3891 break;
3893 case CK_MarkColumnPageUp:
3894 edit->column_highlight = 1;
3895 case CK_PageUp:
3896 case CK_MarkPageUp:
3897 edit_move_up (edit, edit->widget.lines - 1, 1);
3898 break;
3899 case CK_MarkColumnPageDown:
3900 edit->column_highlight = 1;
3901 case CK_PageDown:
3902 case CK_MarkPageDown:
3903 edit_move_down (edit, edit->widget.lines - 1, 1);
3904 break;
3905 case CK_MarkColumnLeft:
3906 edit->column_highlight = 1;
3907 case CK_Left:
3908 case CK_MarkLeft:
3909 if (option_fake_half_tabs)
3911 if (is_in_indent (edit) && right_of_four_spaces (edit))
3913 if (option_cursor_beyond_eol && edit->over_col > 0)
3914 edit->over_col--;
3915 else
3916 edit_cursor_move (edit, -HALF_TAB_SIZE);
3917 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3918 break;
3921 edit_left_char_move_cmd (edit);
3922 break;
3923 case CK_MarkColumnRight:
3924 edit->column_highlight = 1;
3925 case CK_Right:
3926 case CK_MarkRight:
3927 if (option_fake_half_tabs)
3929 if (is_in_indent (edit) && left_of_four_spaces (edit))
3931 edit_cursor_move (edit, HALF_TAB_SIZE);
3932 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3933 break;
3936 edit_right_char_move_cmd (edit);
3937 break;
3938 case CK_TopOnScreen:
3939 case CK_MarkToPageBegin:
3940 edit_begin_page (edit);
3941 break;
3942 case CK_BottomOnScreen:
3943 case CK_MarkToPageEnd:
3944 edit_end_page (edit);
3945 break;
3946 case CK_WordLeft:
3947 case CK_MarkToWordBegin:
3948 edit->over_col = 0;
3949 edit_left_word_move_cmd (edit);
3950 break;
3951 case CK_WordRight:
3952 case CK_MarkToWordEnd:
3953 edit->over_col = 0;
3954 edit_right_word_move_cmd (edit);
3955 break;
3956 case CK_MarkColumnUp:
3957 edit->column_highlight = 1;
3958 case CK_Up:
3959 case CK_MarkUp:
3960 edit_move_up (edit, 1, 0);
3961 break;
3962 case CK_MarkColumnDown:
3963 edit->column_highlight = 1;
3964 case CK_Down:
3965 case CK_MarkDown:
3966 edit_move_down (edit, 1, 0);
3967 break;
3968 case CK_MarkColumnParagraphUp:
3969 edit->column_highlight = 1;
3970 case CK_ParagraphUp:
3971 case CK_MarkParagraphUp:
3972 edit_move_up_paragraph (edit, 0);
3973 break;
3974 case CK_MarkColumnParagraphDown:
3975 edit->column_highlight = 1;
3976 case CK_ParagraphDown:
3977 case CK_MarkParagraphDown:
3978 edit_move_down_paragraph (edit, 0);
3979 break;
3980 case CK_MarkColumnScrollUp:
3981 edit->column_highlight = 1;
3982 case CK_ScrollUp:
3983 case CK_MarkScrollUp:
3984 edit_move_up (edit, 1, 1);
3985 break;
3986 case CK_MarkColumnScrollDown:
3987 edit->column_highlight = 1;
3988 case CK_ScrollDown:
3989 case CK_MarkScrollDown:
3990 edit_move_down (edit, 1, 1);
3991 break;
3992 case CK_Home:
3993 case CK_MarkToHome:
3994 edit_cursor_to_bol (edit);
3995 break;
3996 case CK_End:
3997 case CK_MarkToEnd:
3998 edit_cursor_to_eol (edit);
3999 break;
4000 case CK_Tab:
4001 /* if text marked shift block */
4002 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4004 if (edit->mark2 < 0)
4005 edit_mark_cmd (edit, FALSE);
4006 edit_move_block_to_right (edit);
4008 else
4010 if (option_cursor_beyond_eol)
4011 edit_insert_over (edit);
4012 edit_tab_cmd (edit);
4013 if (option_auto_para_formatting)
4015 format_paragraph (edit, 0);
4016 edit->force |= REDRAW_PAGE;
4018 else
4020 check_and_wrap_line (edit);
4023 break;
4025 case CK_InsertOverwrite:
4026 edit->overwrite = !edit->overwrite;
4027 break;
4029 case CK_Mark:
4030 if (edit->mark2 >= 0)
4032 if (edit->column_highlight)
4033 edit_push_undo_action (edit, COLUMN_ON);
4034 edit->column_highlight = 0;
4036 edit_mark_cmd (edit, FALSE);
4037 break;
4038 case CK_MarkColumn:
4039 if (!edit->column_highlight)
4040 edit_push_undo_action (edit, COLUMN_OFF);
4041 edit->column_highlight = 1;
4042 edit_mark_cmd (edit, FALSE);
4043 break;
4044 case CK_MarkAll:
4045 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
4046 edit->force |= REDRAW_PAGE;
4047 break;
4048 case CK_Unmark:
4049 if (edit->column_highlight)
4050 edit_push_undo_action (edit, COLUMN_ON);
4051 edit->column_highlight = 0;
4052 edit_mark_cmd (edit, TRUE);
4053 break;
4054 case CK_MarkWord:
4055 if (edit->column_highlight)
4056 edit_push_undo_action (edit, COLUMN_ON);
4057 edit->column_highlight = 0;
4058 edit_mark_current_word_cmd (edit);
4059 break;
4060 case CK_MarkLine:
4061 if (edit->column_highlight)
4062 edit_push_undo_action (edit, COLUMN_ON);
4063 edit->column_highlight = 0;
4064 edit_mark_current_line_cmd (edit);
4065 break;
4067 case CK_Bookmark:
4068 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4069 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4070 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4071 else
4072 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4073 break;
4074 case CK_BookmarkFlush:
4075 book_mark_flush (edit, BOOK_MARK_COLOR);
4076 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4077 edit->force |= REDRAW_PAGE;
4078 break;
4079 case CK_BookmarkNext:
4080 if (edit->book_mark)
4082 struct _book_mark *p;
4083 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4084 if (p->next)
4086 p = p->next;
4087 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4088 edit_move_display (edit, p->line - edit->widget.lines / 2);
4089 edit_move_to_line (edit, p->line);
4092 break;
4093 case CK_BookmarkPrev:
4094 if (edit->book_mark)
4096 struct _book_mark *p;
4097 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4098 while (p->line == edit->curs_line)
4099 if (p->prev)
4100 p = p->prev;
4101 if (p->line >= 0)
4103 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4104 edit_move_display (edit, p->line - edit->widget.lines / 2);
4105 edit_move_to_line (edit, p->line);
4108 break;
4110 case CK_Top:
4111 case CK_MarkToFileBegin:
4112 edit_move_to_top (edit);
4113 break;
4114 case CK_Bottom:
4115 case CK_MarkToFileEnd:
4116 edit_move_to_bottom (edit);
4117 break;
4119 case CK_Copy:
4120 if (option_cursor_beyond_eol && edit->over_col > 0)
4121 edit_insert_over (edit);
4122 edit_block_copy_cmd (edit);
4123 break;
4124 case CK_Remove:
4125 edit_block_delete_cmd (edit);
4126 break;
4127 case CK_Move:
4128 edit_block_move_cmd (edit);
4129 break;
4131 case CK_BlockShiftLeft:
4132 if (edit->mark1 != edit->mark2)
4133 edit_move_block_to_left (edit);
4134 break;
4135 case CK_BlockShiftRight:
4136 if (edit->mark1 != edit->mark2)
4137 edit_move_block_to_right (edit);
4138 break;
4139 case CK_Store:
4140 edit_copy_to_X_buf_cmd (edit);
4141 break;
4142 case CK_Cut:
4143 edit_cut_to_X_buf_cmd (edit);
4144 break;
4145 case CK_Paste:
4146 /* if non persistent selection and text selected */
4147 if (!option_persistent_selections)
4149 if (edit->mark1 != edit->mark2)
4150 edit_block_delete_cmd (edit);
4152 if (option_cursor_beyond_eol && edit->over_col > 0)
4153 edit_insert_over (edit);
4154 edit_paste_from_X_buf_cmd (edit);
4155 break;
4156 case CK_History:
4157 edit_paste_from_history (edit);
4158 break;
4160 case CK_SaveAs:
4161 edit_save_as_cmd (edit);
4162 break;
4163 case CK_Save:
4164 edit_save_confirm_cmd (edit);
4165 break;
4166 case CK_BlockSave:
4167 edit_save_block_cmd (edit);
4168 break;
4169 case CK_InsertFile:
4170 edit_insert_file_cmd (edit);
4171 break;
4173 case CK_FilePrev:
4174 edit_load_back_cmd (edit);
4175 break;
4176 case CK_FileNext:
4177 edit_load_forward_cmd (edit);
4178 break;
4180 case CK_SyntaxChoose:
4181 edit_syntax_dialog (edit);
4182 break;
4184 case CK_Search:
4185 edit_search_cmd (edit, FALSE);
4186 break;
4187 case CK_SearchContinue:
4188 edit_search_cmd (edit, TRUE);
4189 break;
4190 case CK_Replace:
4191 edit_replace_cmd (edit, 0);
4192 break;
4193 case CK_ReplaceContinue:
4194 edit_replace_cmd (edit, 1);
4195 break;
4196 case CK_Complete:
4197 /* if text marked shift block */
4198 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4200 edit_move_block_to_left (edit);
4202 else
4204 edit_complete_word_cmd (edit);
4206 break;
4207 case CK_Find:
4208 edit_get_match_keyword_cmd (edit);
4209 break;
4210 #ifdef HAVE_ASPELL
4211 case CK_SpellCheckCurrentWord:
4212 edit_suggest_current_word (edit);
4213 break;
4214 case CK_SpellCheck:
4215 edit_spellcheck_file (edit);
4216 break;
4217 case CK_SpellCheckSelectLang:
4218 edit_set_spell_lang ();
4219 break;
4220 #endif
4221 case CK_Date:
4223 char s[BUF_MEDIUM];
4224 /* fool gcc to prevent a Y2K warning */
4225 char time_format[] = "_c";
4226 time_format[0] = '%';
4228 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4229 edit_print_string (edit, s);
4230 edit->force |= REDRAW_PAGE;
4231 break;
4233 break;
4234 case CK_Goto:
4235 edit_goto_cmd (edit);
4236 break;
4237 case CK_ParagraphFormat:
4238 format_paragraph (edit, 1);
4239 edit->force |= REDRAW_PAGE;
4240 break;
4241 case CK_MacroDelete:
4242 edit_delete_macro_cmd (edit);
4243 break;
4244 case CK_MatchBracket:
4245 edit_goto_matching_bracket (edit);
4246 break;
4247 case CK_UserMenu:
4248 user_menu (edit, NULL, -1);
4249 break;
4250 case CK_Sort:
4251 edit_sort_cmd (edit);
4252 break;
4253 case CK_ExternalCommand:
4254 edit_ext_cmd (edit);
4255 break;
4256 case CK_Mail:
4257 edit_mail_dialog (edit);
4258 break;
4259 #ifdef HAVE_CHARSET
4260 case CK_SelectCodepage:
4261 edit_select_codepage_cmd (edit);
4262 break;
4263 #endif
4264 case CK_InsertLiteral:
4265 edit_insert_literal_cmd (edit);
4266 break;
4267 case CK_MacroStartStopRecord:
4268 edit_begin_end_macro_cmd (edit);
4269 break;
4270 case CK_RepeatStartStopRecord:
4271 edit_begin_end_repeat_cmd (edit);
4272 break;
4273 case CK_ExtendedKeyMap:
4274 edit->extmod = TRUE;
4275 break;
4276 default:
4277 break;
4280 /* CK_PipeBlock */
4281 if ((command / CK_PipeBlock (0)) == 1)
4282 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4284 /* keys which must set the col position, and the search vars */
4285 switch (command)
4287 case CK_Search:
4288 case CK_SearchContinue:
4289 case CK_Replace:
4290 case CK_ReplaceContinue:
4291 case CK_Complete:
4292 edit->prev_col = edit_get_col (edit);
4293 break;
4294 case CK_Up:
4295 case CK_MarkUp:
4296 case CK_MarkColumnUp:
4297 case CK_Down:
4298 case CK_MarkDown:
4299 case CK_MarkColumnDown:
4300 case CK_PageUp:
4301 case CK_MarkPageUp:
4302 case CK_MarkColumnPageUp:
4303 case CK_PageDown:
4304 case CK_MarkPageDown:
4305 case CK_MarkColumnPageDown:
4306 case CK_Top:
4307 case CK_MarkToFileBegin:
4308 case CK_Bottom:
4309 case CK_MarkToFileEnd:
4310 case CK_ParagraphUp:
4311 case CK_MarkParagraphUp:
4312 case CK_MarkColumnParagraphUp:
4313 case CK_ParagraphDown:
4314 case CK_MarkParagraphDown:
4315 case CK_MarkColumnParagraphDown:
4316 case CK_ScrollUp:
4317 case CK_MarkScrollUp:
4318 case CK_MarkColumnScrollUp:
4319 case CK_ScrollDown:
4320 case CK_MarkScrollDown:
4321 case CK_MarkColumnScrollDown:
4322 edit->search_start = edit->curs1;
4323 edit->found_len = 0;
4324 break;
4325 default:
4326 edit->found_len = 0;
4327 edit->prev_col = edit_get_col (edit);
4328 edit->search_start = edit->curs1;
4330 edit_find_bracket (edit);
4332 if (option_auto_para_formatting)
4334 switch (command)
4336 case CK_BackSpace:
4337 case CK_Delete:
4338 case CK_DeleteToWordBegin:
4339 case CK_DeleteToWordEnd:
4340 case CK_DeleteToHome:
4341 case CK_DeleteToEnd:
4342 format_paragraph (edit, 0);
4343 edit->force |= REDRAW_PAGE;
4348 /* --------------------------------------------------------------------------------------------- */
4350 void
4351 edit_stack_init (void)
4353 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4355 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4356 edit_history_moveto[edit_stack_iterator].line = -1;
4359 edit_stack_iterator = 0;
4362 /* --------------------------------------------------------------------------------------------- */
4364 void
4365 edit_stack_free (void)
4367 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4368 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4371 /* --------------------------------------------------------------------------------------------- */
4372 /** move i lines */
4374 void
4375 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4377 edit_move_updown (edit, i, do_scroll, TRUE);
4380 /* --------------------------------------------------------------------------------------------- */
4381 /** move i lines */
4383 void
4384 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4386 edit_move_updown (edit, i, do_scroll, FALSE);
4389 /* --------------------------------------------------------------------------------------------- */