Added unit tests for checking "detect type of line breaks" functionality
[midnight-commander.git] / src / editor / edit.c
blob3b041905689a72178a854e16deb9772e2c86c73d
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;
93 int option_autodetect_lb = 0;
95 int option_edit_right_extreme = 0;
96 int option_edit_left_extreme = 0;
97 int option_edit_top_extreme = 0;
98 int option_edit_bottom_extreme = 0;
99 int enable_show_tabs_tws = 1;
100 int option_check_nl_at_eof = 0;
101 int option_group_undo = 0;
102 int show_right_margin = 0;
104 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
105 char *option_backup_ext = NULL;
107 int edit_stack_iterator = 0;
108 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
109 /* magic sequense for say than block is vertical */
110 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
112 /*** file scope macro definitions ****************************************************************/
114 #define TEMP_BUF_LEN 1024
116 #define space_width 1
118 #define DETECT_LB_TYPE_BUFLEN BUF_MEDIUM
120 /*** file scope type declarations ****************************************************************/
122 /*** file scope variables ************************************************************************/
124 /* detecting an error on save is easy: just check if every byte has been written. */
125 /* detecting an error on read, is not so easy 'cos there is not way to tell
126 whether you read everything or not. */
127 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
128 static const struct edit_filters
130 const char *read, *write, *extension;
131 } all_filters[] =
133 /* *INDENT-OFF* */
134 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
135 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
136 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
137 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
138 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
139 /* *INDENT-ON* */
142 static long last_bracket = -1;
144 /*** file scope functions ************************************************************************/
145 /* --------------------------------------------------------------------------------------------- */
149 * here's a quick sketch of the layout: (don't run this through indent.)
151 * (b1 is buffers1 and b2 is buffers2)
154 * \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
155 * ______________________________________|______________________________________
157 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
158 * |-> |-> |-> |-> |-> |-> |
160 * _<------------------------->|<----------------->_
161 * WEdit->curs2 | WEdit->curs1
162 * ^ | ^
163 * | ^|^ |
164 * cursor ||| cursor
165 * |||
166 * file end|||file beginning
171 * This_is_some_file
172 * fin.
175 /* --------------------------------------------------------------------------------------------- */
177 static int left_of_four_spaces (WEdit * edit);
179 /* --------------------------------------------------------------------------------------------- */
181 * Initialize the buffers for an empty files.
184 static void
185 edit_init_buffers (WEdit * edit)
187 int j;
189 for (j = 0; j <= MAXBUFF; j++)
191 edit->buffers1[j] = NULL;
192 edit->buffers2[j] = NULL;
195 edit->curs1 = 0;
196 edit->curs2 = 0;
197 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
200 /* --------------------------------------------------------------------------------------------- */
203 * Load file OR text into buffers. Set cursor to the beginning of file.
205 * @returns FALSE on error.
208 static gboolean
209 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
211 long buf, buf2;
212 int file = -1;
213 gboolean ret = FALSE;
215 edit->curs2 = edit->last_byte;
216 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
218 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
219 if (file == -1)
221 gchar *errmsg, *filename;
223 filename = vfs_path_to_str (filename_vpath);
224 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
225 g_free (filename);
226 edit_error_dialog (_("Error"), errmsg);
227 g_free (errmsg);
228 return FALSE;
231 if (!edit->buffers2[buf2])
232 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
236 if (mc_read (file,
237 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
238 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
239 break;
241 for (buf = buf2 - 1; buf >= 0; buf--)
243 /* edit->buffers2[0] is already allocated */
244 if (!edit->buffers2[buf])
245 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
246 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
247 break;
249 ret = TRUE;
251 while (FALSE);
253 if (!ret)
255 gchar *errmsg, *filename;
257 filename = vfs_path_to_str (filename_vpath);
258 errmsg = g_strdup_printf (_("Error reading %s"), filename);
259 g_free (filename);
260 edit_error_dialog (_("Error"), errmsg);
261 g_free (errmsg);
263 mc_close (file);
264 return ret;
267 /* --------------------------------------------------------------------------------------------- */
268 /** Return index of the filter or -1 is there is no appropriate filter */
270 static int
271 edit_find_filter (const vfs_path_t * filename_vpath)
273 size_t i, l, e;
274 char *filename;
276 if (filename_vpath == NULL)
277 return -1;
279 filename = vfs_path_to_str (filename_vpath);
280 l = strlen (filename);
281 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
283 e = strlen (all_filters[i].extension);
284 if (l > e)
285 if (!strcmp (all_filters[i].extension, filename + l - e))
287 g_free (filename);
288 return i;
291 g_free (filename);
292 return -1;
295 /* --------------------------------------------------------------------------------------------- */
297 static char *
298 edit_get_filter (const vfs_path_t * filename_vpath)
300 int i;
301 char *p, *quoted_name, *filename;
303 i = edit_find_filter (filename_vpath);
304 if (i < 0)
305 return NULL;
307 filename = vfs_path_to_str (filename_vpath);
308 quoted_name = name_quote (filename, 0);
309 g_free (filename);
310 p = g_strdup_printf (all_filters[i].read, quoted_name);
311 g_free (quoted_name);
312 return p;
315 /* --------------------------------------------------------------------------------------------- */
317 static long
318 edit_insert_stream (WEdit * edit, FILE * f)
320 int c;
321 long i = 0;
322 while ((c = fgetc (f)) >= 0)
324 edit_insert (edit, c);
325 i++;
327 return i;
330 /* --------------------------------------------------------------------------------------------- */
332 * Open file and create it if necessary.
334 * @param edit editor object
335 * @param filename_vpath file name
336 * @param st buffer for store stat info
337 * @returns TRUE for success, FALSE for error.
340 static gboolean
341 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
343 int file;
344 gchar *errmsg = NULL;
346 /* Try opening an existing file */
347 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
348 if (file < 0)
351 * Try creating the file. O_EXCL prevents following broken links
352 * and opening existing files.
354 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
355 if (file < 0)
357 char *filename;
359 filename = vfs_path_to_str (filename_vpath);
360 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
361 g_free (filename);
362 goto cleanup;
365 /* New file, delete it if it's not modified or saved */
366 edit->delete_file = 1;
369 /* Check what we have opened */
370 if (mc_fstat (file, st) < 0)
372 char *filename;
374 filename = vfs_path_to_str (filename_vpath);
375 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
376 g_free (filename);
377 goto cleanup;
380 /* We want to open regular files only */
381 if (!S_ISREG (st->st_mode))
383 char *filename;
385 filename = vfs_path_to_str (filename_vpath);
386 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
387 g_free (filename);
388 goto cleanup;
392 * Don't delete non-empty files.
393 * O_EXCL should prevent it, but let's be on the safe side.
395 if (st->st_size > 0)
396 edit->delete_file = 0;
398 if (st->st_size >= SIZE_LIMIT)
400 char *filename;
402 filename = vfs_path_to_str (filename_vpath);
403 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
404 g_free (filename);
407 cleanup:
408 (void) mc_close (file);
410 if (errmsg != NULL)
412 edit_error_dialog (_("Error"), errmsg);
413 g_free (errmsg);
414 return FALSE;
416 return TRUE;
419 /* --------------------------------------------------------------------------------------------- */
422 * detect type of line breaks
425 /* --------------------------------------------------------------------------------------------- */
427 static LineBreaks
428 detect_lb_type (const vfs_path_t *filename_vpath)
430 char buf[BUF_MEDIUM];
431 ssize_t file, sz;
433 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
434 if (file == -1)
435 return LB_ASIS;
437 sz = mc_read (file, buf, sizeof (buf) - 1);
438 mc_close (file);
440 if (sz <= 0)
441 return LB_ASIS;
443 buf[(size_t) sz] = '\0';
444 if (strstr (buf, "\r\n") != NULL)
445 return LB_WIN;
446 if (strchr (buf, '\r') != NULL)
447 return LB_MAC;
448 return LB_ASIS;
451 /* --------------------------------------------------------------------------------------------- */
453 * Open the file and load it into the buffers, either directly or using
454 * a filter. Return TRUE on success, FALSE on error.
456 * Fast loading (edit_load_file_fast) is used when the file size is
457 * known. In this case the data is read into the buffers by blocks.
458 * If the file size is not known, the data is loaded byte by byte in
459 * edit_insert_file.
461 * @param edit editor object
462 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
464 static gboolean
465 edit_load_file (WEdit * edit)
467 gboolean fast_load = TRUE;
468 LineBreaks lb_type = LB_ASIS;
470 /* Cannot do fast load if a filter is used */
471 if (edit_find_filter (edit->filename_vpath) >= 0)
472 fast_load = FALSE;
475 * FIXME: line end translation should disable fast loading as well
476 * Consider doing fseek() to the end and ftell() for the real size.
478 if (edit->filename_vpath != NULL)
481 * VFS may report file size incorrectly, and slow load is not a big
482 * deal considering overhead in VFS.
484 if (!vfs_file_is_local (edit->filename_vpath))
485 fast_load = FALSE;
487 /* If we are dealing with a real file, check that it exists */
488 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
490 edit_clean (edit);
491 return FALSE;
493 if (option_autodetect_lb)
494 lb_type = detect_lb_type (edit->filename_vpath);
496 if (lb_type != LB_ASIS && lb_type != LB_UNIX)
497 fast_load = FALSE;
499 else
501 /* nothing to load */
502 fast_load = FALSE;
505 edit_init_buffers (edit);
507 if (fast_load)
509 edit->last_byte = edit->stat1.st_size;
510 edit_load_file_fast (edit, edit->filename_vpath);
511 /* If fast load was used, the number of lines wasn't calculated */
512 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
514 else
516 edit->last_byte = 0;
517 if (edit->filename_vpath != NULL
518 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
520 edit->undo_stack_disable = 1;
521 if (edit_insert_file (edit, edit->filename_vpath, lb_type) < 0)
523 edit_clean (edit);
524 return FALSE;
526 edit_set_markers (edit, 0, 0, 0, 0);
527 edit->undo_stack_disable = 0;
530 edit->lb = lb_type;
531 return TRUE;
534 /* --------------------------------------------------------------------------------------------- */
535 /** Restore saved cursor position in the file */
537 static void
538 edit_load_position (WEdit * edit)
540 long line, column;
541 off_t offset;
543 if (edit->filename_vpath == NULL
544 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
545 return;
547 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
549 if (line > 0)
551 edit_move_to_line (edit, line - 1);
552 edit->prev_col = column;
554 else if (offset > 0)
556 edit_cursor_move (edit, offset);
557 line = edit->curs_line;
558 edit->search_start = edit->curs1;
561 book_mark_restore (edit, BOOK_MARK_COLOR);
563 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
564 edit_move_display (edit, line - (edit->widget.lines / 2));
567 /* --------------------------------------------------------------------------------------------- */
568 /** Save cursor position in the file */
570 static void
571 edit_save_position (WEdit * edit)
573 if (edit->filename_vpath == NULL
574 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
575 return;
577 book_mark_serialize (edit, BOOK_MARK_COLOR);
578 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
579 edit->serialized_bookmarks);
580 edit->serialized_bookmarks = NULL;
583 /* --------------------------------------------------------------------------------------------- */
584 /** Clean the WEdit stricture except the widget part */
586 static void
587 edit_purge_widget (WEdit * edit)
589 size_t len = sizeof (WEdit) - sizeof (Widget);
590 char *start = (char *) edit + sizeof (Widget);
591 memset (start, 0, len);
594 /* --------------------------------------------------------------------------------------------- */
597 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
598 then the file should be as it was when he loaded up. Then set edit->modified to 0.
601 static long
602 edit_pop_undo_action (WEdit * edit)
604 long c;
605 unsigned long sp = edit->undo_stack_pointer;
607 if (sp == edit->undo_stack_bottom)
608 return STACK_BOTTOM;
610 sp = (sp - 1) & edit->undo_stack_size_mask;
611 c = edit->undo_stack[sp];
612 if (c >= 0)
614 /* edit->undo_stack[sp] = '@'; */
615 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
616 return c;
619 if (sp == edit->undo_stack_bottom)
620 return STACK_BOTTOM;
622 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
623 if (edit->undo_stack[sp] == -2)
625 /* edit->undo_stack[sp] = '@'; */
626 edit->undo_stack_pointer = sp;
628 else
629 edit->undo_stack[sp]++;
631 return c;
634 static long
635 edit_pop_redo_action (WEdit * edit)
637 long c;
638 unsigned long sp = edit->redo_stack_pointer;
640 if (sp == edit->redo_stack_bottom)
641 return STACK_BOTTOM;
643 sp = (sp - 1) & edit->redo_stack_size_mask;
644 c = edit->redo_stack[sp];
645 if (c >= 0)
647 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
648 return c;
651 if (sp == edit->redo_stack_bottom)
652 return STACK_BOTTOM;
654 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
655 if (edit->redo_stack[sp] == -2)
656 edit->redo_stack_pointer = sp;
657 else
658 edit->redo_stack[sp]++;
660 return c;
663 static long
664 get_prev_undo_action (WEdit * edit)
666 long c;
667 unsigned long sp = edit->undo_stack_pointer;
669 if (sp == edit->undo_stack_bottom)
670 return STACK_BOTTOM;
672 sp = (sp - 1) & edit->undo_stack_size_mask;
673 c = edit->undo_stack[sp];
674 if (c >= 0)
675 return c;
677 if (sp == edit->undo_stack_bottom)
678 return STACK_BOTTOM;
680 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
681 return c;
684 /* --------------------------------------------------------------------------------------------- */
685 /** is called whenever a modification is made by one of the four routines below */
687 static void
688 edit_modification (WEdit * edit)
690 edit->caches_valid = 0;
692 /* raise lock when file modified */
693 if (!edit->modified && !edit->delete_file)
694 edit->locked = lock_file (edit->filename_vpath);
695 edit->modified = 1;
698 /* --------------------------------------------------------------------------------------------- */
700 static char *
701 edit_get_byte_ptr (WEdit * edit, long byte_index)
703 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
704 return NULL;
706 if (byte_index >= edit->curs1)
708 unsigned long p;
710 p = edit->curs1 + edit->curs2 - byte_index - 1;
711 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
712 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
715 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
716 (byte_index & M_EDIT_BUF_SIZE));
719 /* --------------------------------------------------------------------------------------------- */
721 static int
722 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
724 int i, res;
725 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
726 gchar *str;
727 gchar *cursor_buf_ptr;
729 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
731 *char_width = 0;
732 return 0;
735 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
736 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
737 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
739 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
740 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
742 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
744 *char_width = 1;
745 return *(cursor_buf_ptr - 1);
747 else
749 res = g_utf8_get_char_validated (str, -1);
751 if (res < 0)
753 *char_width = 1;
754 return *(cursor_buf_ptr - 1);
756 else
758 *char_width = cursor_buf_ptr - str;
759 return res;
764 /* --------------------------------------------------------------------------------------------- */
765 /* high level cursor movement commands */
766 /* --------------------------------------------------------------------------------------------- */
768 static int
769 is_in_indent (WEdit * edit)
771 long p = edit_bol (edit, edit->curs1);
772 while (p < edit->curs1)
773 if (!strchr (" \t", edit_get_byte (edit, p++)))
774 return 0;
775 return 1;
778 /* --------------------------------------------------------------------------------------------- */
780 static int
781 is_blank (WEdit * edit, long offset)
783 long s, f;
784 int c;
785 s = edit_bol (edit, offset);
786 f = edit_eol (edit, offset) - 1;
787 while (s <= f)
789 c = edit_get_byte (edit, s++);
790 if (!isspace (c))
791 return 0;
793 return 1;
797 /* --------------------------------------------------------------------------------------------- */
798 /** returns the offset of line i */
800 static long
801 edit_find_line (WEdit * edit, int line)
803 int i, j = 0;
804 int m = 2000000000;
805 if (!edit->caches_valid)
807 for (i = 0; i < N_LINE_CACHES; i++)
808 edit->line_numbers[i] = edit->line_offsets[i] = 0;
809 /* three offsets that we *know* are line 0 at 0 and these two: */
810 edit->line_numbers[1] = edit->curs_line;
811 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
812 edit->line_numbers[2] = edit->total_lines;
813 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
814 edit->caches_valid = 1;
816 if (line >= edit->total_lines)
817 return edit->line_offsets[2];
818 if (line <= 0)
819 return 0;
820 /* find the closest known point */
821 for (i = 0; i < N_LINE_CACHES; i++)
823 int n;
824 n = abs (edit->line_numbers[i] - line);
825 if (n < m)
827 m = n;
828 j = i;
831 if (m == 0)
832 return edit->line_offsets[j]; /* know the offset exactly */
833 if (m == 1 && j >= 3)
834 i = j; /* one line different - caller might be looping, so stay in this cache */
835 else
836 i = 3 + (rand () % (N_LINE_CACHES - 3));
837 if (line > edit->line_numbers[j])
838 edit->line_offsets[i] =
839 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
840 else
841 edit->line_offsets[i] =
842 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
843 edit->line_numbers[i] = line;
844 return edit->line_offsets[i];
847 /* --------------------------------------------------------------------------------------------- */
848 /** moves up until a blank line is reached, or until just
849 before a non-blank line is reached */
851 static void
852 edit_move_up_paragraph (WEdit * edit, int do_scroll)
854 int i = 0;
855 if (edit->curs_line > 1)
857 if (line_is_blank (edit, edit->curs_line))
859 if (line_is_blank (edit, edit->curs_line - 1))
861 for (i = edit->curs_line - 1; i; i--)
862 if (!line_is_blank (edit, i))
864 i++;
865 break;
868 else
870 for (i = edit->curs_line - 1; i; i--)
871 if (line_is_blank (edit, i))
872 break;
875 else
877 for (i = edit->curs_line - 1; i; i--)
878 if (line_is_blank (edit, i))
879 break;
882 edit_move_up (edit, edit->curs_line - i, do_scroll);
885 /* --------------------------------------------------------------------------------------------- */
886 /** moves down until a blank line is reached, or until just
887 before a non-blank line is reached */
889 static void
890 edit_move_down_paragraph (WEdit * edit, int do_scroll)
892 int i;
893 if (edit->curs_line >= edit->total_lines - 1)
895 i = edit->total_lines;
897 else
899 if (line_is_blank (edit, edit->curs_line))
901 if (line_is_blank (edit, edit->curs_line + 1))
903 for (i = edit->curs_line + 1; i; i++)
904 if (!line_is_blank (edit, i) || i > edit->total_lines)
906 i--;
907 break;
910 else
912 for (i = edit->curs_line + 1; i; i++)
913 if (line_is_blank (edit, i) || i >= edit->total_lines)
914 break;
917 else
919 for (i = edit->curs_line + 1; i; i++)
920 if (line_is_blank (edit, i) || i >= edit->total_lines)
921 break;
924 edit_move_down (edit, i - edit->curs_line, do_scroll);
927 /* --------------------------------------------------------------------------------------------- */
929 static void
930 edit_begin_page (WEdit * edit)
932 edit_update_curs_row (edit);
933 edit_move_up (edit, edit->curs_row, 0);
936 /* --------------------------------------------------------------------------------------------- */
938 static void
939 edit_end_page (WEdit * edit)
941 edit_update_curs_row (edit);
942 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
946 /* --------------------------------------------------------------------------------------------- */
947 /** goto beginning of text */
949 static void
950 edit_move_to_top (WEdit * edit)
952 if (edit->curs_line)
954 edit_cursor_move (edit, -edit->curs1);
955 edit_move_to_prev_col (edit, 0);
956 edit->force |= REDRAW_PAGE;
957 edit->search_start = 0;
958 edit_update_curs_row (edit);
963 /* --------------------------------------------------------------------------------------------- */
964 /** goto end of text */
966 static void
967 edit_move_to_bottom (WEdit * edit)
969 if (edit->curs_line < edit->total_lines)
971 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
972 edit->start_display = edit->last_byte;
973 edit->start_line = edit->total_lines;
974 edit_scroll_upward (edit, edit->widget.lines - 1);
975 edit->force |= REDRAW_PAGE;
979 /* --------------------------------------------------------------------------------------------- */
980 /** goto beginning of line */
982 static void
983 edit_cursor_to_bol (WEdit * edit)
985 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
986 edit->search_start = edit->curs1;
987 edit->prev_col = edit_get_col (edit);
988 edit->over_col = 0;
991 /* --------------------------------------------------------------------------------------------- */
992 /** goto end of line */
994 static void
995 edit_cursor_to_eol (WEdit * edit)
997 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
998 edit->search_start = edit->curs1;
999 edit->prev_col = edit_get_col (edit);
1000 edit->over_col = 0;
1003 /* --------------------------------------------------------------------------------------------- */
1005 static unsigned long
1006 my_type_of (int c)
1008 int x, r = 0;
1009 const char *p, *q;
1010 const char option_chars_move_whole_word[] =
1011 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1013 if (!c)
1014 return 0;
1015 if (c == '!')
1017 if (*option_chars_move_whole_word == '!')
1018 return 2;
1019 return 0x80000000UL;
1021 if (g_ascii_isupper ((gchar) c))
1022 c = 'A';
1023 else if (g_ascii_islower ((gchar) c))
1024 c = 'a';
1025 else if (g_ascii_isalpha (c))
1026 c = 'a';
1027 else if (isdigit (c))
1028 c = '0';
1029 else if (isspace (c))
1030 c = ' ';
1031 q = strchr (option_chars_move_whole_word, c);
1032 if (!q)
1033 return 0xFFFFFFFFUL;
1036 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1037 if (*p == '!')
1038 x <<= 1;
1039 r |= x;
1041 while ((q = strchr (q + 1, c)));
1042 return r;
1045 /* --------------------------------------------------------------------------------------------- */
1047 static void
1048 edit_left_word_move (WEdit * edit, int s)
1050 for (;;)
1052 int c1, c2;
1053 if (edit->column_highlight
1054 && edit->mark1 != edit->mark2
1055 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1056 break;
1057 edit_cursor_move (edit, -1);
1058 if (!edit->curs1)
1059 break;
1060 c1 = edit_get_byte (edit, edit->curs1 - 1);
1061 c2 = edit_get_byte (edit, edit->curs1);
1062 if (c1 == '\n' || c2 == '\n')
1063 break;
1064 if (!(my_type_of (c1) & my_type_of (c2)))
1065 break;
1066 if (isspace (c1) && !isspace (c2))
1067 break;
1068 if (s)
1069 if (!isspace (c1) && isspace (c2))
1070 break;
1074 /* --------------------------------------------------------------------------------------------- */
1076 static void
1077 edit_left_word_move_cmd (WEdit * edit)
1079 edit_left_word_move (edit, 0);
1080 edit->force |= REDRAW_PAGE;
1083 /* --------------------------------------------------------------------------------------------- */
1085 static void
1086 edit_right_word_move (WEdit * edit, int s)
1088 for (;;)
1090 int c1, c2;
1091 if (edit->column_highlight
1092 && edit->mark1 != edit->mark2
1093 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1094 break;
1095 edit_cursor_move (edit, 1);
1096 if (edit->curs1 >= edit->last_byte)
1097 break;
1098 c1 = edit_get_byte (edit, edit->curs1 - 1);
1099 c2 = edit_get_byte (edit, edit->curs1);
1100 if (c1 == '\n' || c2 == '\n')
1101 break;
1102 if (!(my_type_of (c1) & my_type_of (c2)))
1103 break;
1104 if (isspace (c1) && !isspace (c2))
1105 break;
1106 if (s)
1107 if (!isspace (c1) && isspace (c2))
1108 break;
1112 /* --------------------------------------------------------------------------------------------- */
1114 static void
1115 edit_right_word_move_cmd (WEdit * edit)
1117 edit_right_word_move (edit, 0);
1118 edit->force |= REDRAW_PAGE;
1121 /* --------------------------------------------------------------------------------------------- */
1123 static void
1124 edit_right_char_move_cmd (WEdit * edit)
1126 int cw = 1;
1127 int c = 0;
1128 if (edit->utf8)
1130 c = edit_get_utf (edit, edit->curs1, &cw);
1131 if (cw < 1)
1132 cw = 1;
1134 else
1136 c = edit_get_byte (edit, edit->curs1);
1138 if (option_cursor_beyond_eol && c == '\n')
1140 edit->over_col++;
1142 else
1144 edit_cursor_move (edit, cw);
1148 /* --------------------------------------------------------------------------------------------- */
1150 static void
1151 edit_left_char_move_cmd (WEdit * edit)
1153 int cw = 1;
1154 if (edit->column_highlight
1155 && option_cursor_beyond_eol
1156 && edit->mark1 != edit->mark2
1157 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1158 return;
1159 if (edit->utf8)
1161 edit_get_prev_utf (edit, edit->curs1, &cw);
1162 if (cw < 1)
1163 cw = 1;
1165 if (option_cursor_beyond_eol && edit->over_col > 0)
1167 edit->over_col--;
1169 else
1171 edit_cursor_move (edit, -cw);
1175 /* --------------------------------------------------------------------------------------------- */
1176 /** Up or down cursor moving.
1177 direction = TRUE - move up
1178 = FALSE - move down
1181 static void
1182 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1184 unsigned long p;
1185 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1187 if (i > l)
1188 i = l;
1190 if (i == 0)
1191 return;
1193 if (i > 1)
1194 edit->force |= REDRAW_PAGE;
1195 if (do_scroll)
1197 if (direction)
1198 edit_scroll_upward (edit, i);
1199 else
1200 edit_scroll_downward (edit, i);
1202 p = edit_bol (edit, edit->curs1);
1204 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1206 edit_cursor_move (edit, p - edit->curs1);
1208 edit_move_to_prev_col (edit, p);
1210 /* search start of current multibyte char (like CJK) */
1211 if (edit->curs1 + 1 < edit->last_byte)
1213 edit_right_char_move_cmd (edit);
1214 edit_left_char_move_cmd (edit);
1217 edit->search_start = edit->curs1;
1218 edit->found_len = 0;
1221 /* --------------------------------------------------------------------------------------------- */
1223 static void
1224 edit_right_delete_word (WEdit * edit)
1226 int c1, c2;
1227 for (;;)
1229 if (edit->curs1 >= edit->last_byte)
1230 break;
1231 c1 = edit_delete (edit, 1);
1232 c2 = edit_get_byte (edit, edit->curs1);
1233 if (c1 == '\n' || c2 == '\n')
1234 break;
1235 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1236 break;
1237 if (!(my_type_of (c1) & my_type_of (c2)))
1238 break;
1242 /* --------------------------------------------------------------------------------------------- */
1244 static void
1245 edit_left_delete_word (WEdit * edit)
1247 int c1, c2;
1248 for (;;)
1250 if (edit->curs1 <= 0)
1251 break;
1252 c1 = edit_backspace (edit, 1);
1253 c2 = edit_get_byte (edit, edit->curs1 - 1);
1254 if (c1 == '\n' || c2 == '\n')
1255 break;
1256 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1257 break;
1258 if (!(my_type_of (c1) & my_type_of (c2)))
1259 break;
1263 /* --------------------------------------------------------------------------------------------- */
1265 the start column position is not recorded, and hence does not
1266 undo as it happed. But who would notice.
1269 static void
1270 edit_do_undo (WEdit * edit)
1272 long ac;
1273 long count = 0;
1275 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1276 edit->over_col = 0;
1277 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1279 switch ((int) ac)
1281 case STACK_BOTTOM:
1282 goto done_undo;
1283 case CURS_RIGHT:
1284 edit_cursor_move (edit, 1);
1285 break;
1286 case CURS_LEFT:
1287 edit_cursor_move (edit, -1);
1288 break;
1289 case BACKSPACE:
1290 case BACKSPACE_BR:
1291 edit_backspace (edit, 1);
1292 break;
1293 case DELCHAR:
1294 case DELCHAR_BR:
1295 edit_delete (edit, 1);
1296 break;
1297 case COLUMN_ON:
1298 edit->column_highlight = 1;
1299 break;
1300 case COLUMN_OFF:
1301 edit->column_highlight = 0;
1302 break;
1304 if (ac >= 256 && ac < 512)
1305 edit_insert_ahead (edit, ac - 256);
1306 if (ac >= 0 && ac < 256)
1307 edit_insert (edit, ac);
1309 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1311 edit->mark1 = ac - MARK_1;
1312 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1314 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1316 edit->mark2 = ac - MARK_2;
1317 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1319 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1321 edit->end_mark_curs = ac - MARK_CURS;
1323 if (count++)
1324 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1327 if (edit->start_display > ac - KEY_PRESS)
1329 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1330 edit->force |= REDRAW_PAGE;
1332 else if (edit->start_display < ac - KEY_PRESS)
1334 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1335 edit->force |= REDRAW_PAGE;
1337 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1338 edit_update_curs_row (edit);
1340 done_undo:;
1341 edit->undo_stack_disable = 0;
1344 static void
1345 edit_do_redo (WEdit * edit)
1347 long ac;
1348 long count = 0;
1350 if (edit->redo_stack_reset)
1351 return;
1353 edit->over_col = 0;
1354 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1356 switch ((int) ac)
1358 case STACK_BOTTOM:
1359 goto done_redo;
1360 case CURS_RIGHT:
1361 edit_cursor_move (edit, 1);
1362 break;
1363 case CURS_LEFT:
1364 edit_cursor_move (edit, -1);
1365 break;
1366 case BACKSPACE:
1367 edit_backspace (edit, 1);
1368 break;
1369 case DELCHAR:
1370 edit_delete (edit, 1);
1371 break;
1372 case COLUMN_ON:
1373 edit->column_highlight = 1;
1374 break;
1375 case COLUMN_OFF:
1376 edit->column_highlight = 0;
1377 break;
1379 if (ac >= 256 && ac < 512)
1380 edit_insert_ahead (edit, ac - 256);
1381 if (ac >= 0 && ac < 256)
1382 edit_insert (edit, ac);
1384 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1386 edit->mark1 = ac - MARK_1;
1387 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1389 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1391 edit->mark2 = ac - MARK_2;
1392 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1394 /* more than one pop usually means something big */
1395 if (count++)
1396 edit->force |= REDRAW_PAGE;
1399 if (edit->start_display > ac - KEY_PRESS)
1401 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1402 edit->force |= REDRAW_PAGE;
1404 else if (edit->start_display < ac - KEY_PRESS)
1406 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1407 edit->force |= REDRAW_PAGE;
1409 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1410 edit_update_curs_row (edit);
1412 done_redo:;
1415 static void
1416 edit_group_undo (WEdit * edit)
1418 long ac = KEY_PRESS;
1419 long cur_ac = KEY_PRESS;
1420 while (ac != STACK_BOTTOM && ac == cur_ac)
1422 cur_ac = get_prev_undo_action (edit);
1423 edit_do_undo (edit);
1424 ac = get_prev_undo_action (edit);
1425 /* exit from cycle if option_group_undo is not set,
1426 * and make single UNDO operation
1428 if (!option_group_undo)
1429 ac = STACK_BOTTOM;
1433 /* --------------------------------------------------------------------------------------------- */
1435 static void
1436 edit_delete_to_line_end (WEdit * edit)
1438 while (edit_get_byte (edit, edit->curs1) != '\n')
1440 if (!edit->curs2)
1441 break;
1442 edit_delete (edit, 1);
1446 /* --------------------------------------------------------------------------------------------- */
1448 static void
1449 edit_delete_to_line_begin (WEdit * edit)
1451 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1453 if (!edit->curs1)
1454 break;
1455 edit_backspace (edit, 1);
1459 /* --------------------------------------------------------------------------------------------- */
1461 static int
1462 is_aligned_on_a_tab (WEdit * edit)
1464 edit_update_curs_col (edit);
1465 return !((edit->curs_col % (TAB_SIZE * space_width))
1466 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1469 /* --------------------------------------------------------------------------------------------- */
1471 static int
1472 right_of_four_spaces (WEdit * edit)
1474 int i, ch = 0;
1475 for (i = 1; i <= HALF_TAB_SIZE; i++)
1476 ch |= edit_get_byte (edit, edit->curs1 - i);
1477 if (ch == ' ')
1478 return is_aligned_on_a_tab (edit);
1479 return 0;
1482 /* --------------------------------------------------------------------------------------------- */
1484 static int
1485 left_of_four_spaces (WEdit * edit)
1487 int i, ch = 0;
1488 for (i = 0; i < HALF_TAB_SIZE; i++)
1489 ch |= edit_get_byte (edit, edit->curs1 + i);
1490 if (ch == ' ')
1491 return is_aligned_on_a_tab (edit);
1492 return 0;
1495 /* --------------------------------------------------------------------------------------------- */
1497 static void
1498 edit_auto_indent (WEdit * edit)
1500 long p;
1501 char c;
1502 p = edit->curs1;
1503 /* use the previous line as a template */
1504 p = edit_move_backward (edit, p, 1);
1505 /* copy the leading whitespace of the line */
1506 for (;;)
1507 { /* no range check - the line _is_ \n-terminated */
1508 c = edit_get_byte (edit, p++);
1509 if (c != ' ' && c != '\t')
1510 break;
1511 edit_insert (edit, c);
1515 /* --------------------------------------------------------------------------------------------- */
1517 static inline void
1518 edit_double_newline (WEdit * edit)
1520 edit_insert (edit, '\n');
1521 if (edit_get_byte (edit, edit->curs1) == '\n')
1522 return;
1523 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1524 return;
1525 edit->force |= REDRAW_PAGE;
1526 edit_insert (edit, '\n');
1530 /* --------------------------------------------------------------------------------------------- */
1532 static void
1533 insert_spaces_tab (WEdit * edit, gboolean half)
1535 int i;
1537 edit_update_curs_col (edit);
1538 i = option_tab_spacing * space_width;
1539 if (half)
1540 i /= 2;
1541 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1542 while (i > 0)
1544 edit_insert (edit, ' ');
1545 i -= space_width;
1549 /* --------------------------------------------------------------------------------------------- */
1551 static inline void
1552 edit_tab_cmd (WEdit * edit)
1554 int i;
1556 if (option_fake_half_tabs)
1558 if (is_in_indent (edit))
1560 /*insert a half tab (usually four spaces) unless there is a
1561 half tab already behind, then delete it and insert a
1562 full tab. */
1563 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1564 insert_spaces_tab (edit, TRUE);
1565 else
1567 for (i = 1; i <= HALF_TAB_SIZE; i++)
1568 edit_backspace (edit, 1);
1569 edit_insert (edit, '\t');
1571 return;
1574 if (option_fill_tabs_with_spaces)
1575 insert_spaces_tab (edit, FALSE);
1576 else
1577 edit_insert (edit, '\t');
1580 /* --------------------------------------------------------------------------------------------- */
1582 static void
1583 check_and_wrap_line (WEdit * edit)
1585 int curs, c;
1586 if (!option_typewriter_wrap)
1587 return;
1588 edit_update_curs_col (edit);
1589 if (edit->curs_col < option_word_wrap_line_length)
1590 return;
1591 curs = edit->curs1;
1592 for (;;)
1594 curs--;
1595 c = edit_get_byte (edit, curs);
1596 if (c == '\n' || curs <= 0)
1598 edit_insert (edit, '\n');
1599 return;
1601 if (c == ' ' || c == '\t')
1603 int current = edit->curs1;
1604 edit_cursor_move (edit, curs - edit->curs1 + 1);
1605 edit_insert (edit, '\n');
1606 edit_cursor_move (edit, current - edit->curs1 + 1);
1607 return;
1612 /* --------------------------------------------------------------------------------------------- */
1613 /** this find the matching bracket in either direction, and sets edit->bracket */
1615 static long
1616 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1618 const char *const b = "{}{[][()(", *p;
1619 int i = 1, a, inc = -1, c, d, n = 0;
1620 unsigned long j = 0;
1621 long q;
1622 edit_update_curs_row (edit);
1623 c = edit_get_byte (edit, edit->curs1);
1624 p = strchr (b, c);
1625 /* no limit */
1626 if (!furthest_bracket_search)
1627 furthest_bracket_search--;
1628 /* not on a bracket at all */
1629 if (!p)
1630 return -1;
1631 /* the matching bracket */
1632 d = p[1];
1633 /* going left or right? */
1634 if (strchr ("{[(", c))
1635 inc = 1;
1636 for (q = edit->curs1 + inc;; q += inc)
1638 /* out of buffer? */
1639 if (q >= edit->last_byte || q < 0)
1640 break;
1641 a = edit_get_byte (edit, q);
1642 /* don't want to eat CPU */
1643 if (j++ > furthest_bracket_search)
1644 break;
1645 /* out of screen? */
1646 if (in_screen)
1648 if (q < edit->start_display)
1649 break;
1650 /* count lines if searching downward */
1651 if (inc > 0 && a == '\n')
1652 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1653 break;
1655 /* count bracket depth */
1656 i += (a == c) - (a == d);
1657 /* return if bracket depth is zero */
1658 if (!i)
1659 return q;
1661 /* no match */
1662 return -1;
1665 /* --------------------------------------------------------------------------------------------- */
1667 static inline void
1668 edit_goto_matching_bracket (WEdit * edit)
1670 long q;
1672 q = edit_get_bracket (edit, 0, 0);
1673 if (q >= 0)
1675 edit->bracket = edit->curs1;
1676 edit->force |= REDRAW_PAGE;
1677 edit_cursor_move (edit, q - edit->curs1);
1681 /* --------------------------------------------------------------------------------------------- */
1683 static void
1684 edit_move_block_to_right (WEdit * edit)
1686 long start_mark, end_mark;
1687 long cur_bol, start_bol;
1689 if (eval_marks (edit, &start_mark, &end_mark))
1690 return;
1692 start_bol = edit_bol (edit, start_mark);
1693 cur_bol = edit_bol (edit, end_mark - 1);
1697 edit_cursor_move (edit, cur_bol - edit->curs1);
1698 if (option_fill_tabs_with_spaces)
1699 insert_spaces_tab (edit, option_fake_half_tabs);
1700 else
1701 edit_insert (edit, '\t');
1702 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1704 if (cur_bol == 0)
1705 break;
1707 cur_bol = edit_bol (edit, cur_bol - 1);
1709 while (cur_bol >= start_bol);
1711 edit->force |= REDRAW_PAGE;
1714 /* --------------------------------------------------------------------------------------------- */
1716 static void
1717 edit_move_block_to_left (WEdit * edit)
1719 long start_mark, end_mark;
1720 long cur_bol, start_bol;
1721 int i;
1723 if (eval_marks (edit, &start_mark, &end_mark))
1724 return;
1726 start_bol = edit_bol (edit, start_mark);
1727 cur_bol = edit_bol (edit, end_mark - 1);
1731 int del_tab_width;
1732 int next_char;
1734 edit_cursor_move (edit, cur_bol - edit->curs1);
1736 if (option_fake_half_tabs)
1737 del_tab_width = HALF_TAB_SIZE;
1738 else
1739 del_tab_width = option_tab_spacing;
1741 next_char = edit_get_byte (edit, edit->curs1);
1742 if (next_char == '\t')
1743 edit_delete (edit, 1);
1744 else if (next_char == ' ')
1745 for (i = 1; i <= del_tab_width; i++)
1747 if (next_char == ' ')
1748 edit_delete (edit, 1);
1749 next_char = edit_get_byte (edit, edit->curs1);
1752 if (cur_bol == 0)
1753 break;
1755 cur_bol = edit_bol (edit, cur_bol - 1);
1757 while (cur_bol >= start_bol);
1759 edit->force |= REDRAW_PAGE;
1762 /* --------------------------------------------------------------------------------------------- */
1764 * prints at the cursor
1765 * @returns the number of chars printed
1768 static size_t
1769 edit_print_string (WEdit * e, const char *s)
1771 size_t i = 0;
1773 while (s[i] != '\0')
1774 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1775 e->force |= REDRAW_COMPLETELY;
1776 edit_update_screen (e);
1777 return i;
1780 /* --------------------------------------------------------------------------------------------- */
1781 /*** public functions ****************************************************************************/
1782 /* --------------------------------------------------------------------------------------------- */
1784 /** User edit menu, like user menu (F2) but only in editor. */
1786 void
1787 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1789 char *block_file;
1790 int nomark;
1791 long curs;
1792 long start_mark, end_mark;
1793 struct stat status;
1794 vfs_path_t *block_file_vpath;
1796 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1797 block_file_vpath = vfs_path_from_str (block_file);
1798 curs = edit->curs1;
1799 nomark = eval_marks (edit, &start_mark, &end_mark);
1800 if (nomark == 0)
1801 edit_save_block (edit, block_file, start_mark, end_mark);
1803 /* run shell scripts from menu */
1804 if (user_menu_cmd (edit, menu_file, selected_entry)
1805 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1807 int rc = 0;
1808 FILE *fd;
1810 /* i.e. we have marked block */
1811 if (nomark == 0)
1812 rc = edit_block_delete_cmd (edit);
1814 if (rc == 0)
1816 long ins_len;
1818 ins_len = edit_insert_file (edit, block_file_vpath, LB_ASIS);
1819 if (nomark == 0 && ins_len > 0)
1820 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1822 /* truncate block file */
1823 fd = fopen (block_file, "w");
1824 if (fd != NULL)
1825 fclose (fd);
1827 g_free (block_file);
1828 vfs_path_free (block_file_vpath);
1830 edit_cursor_move (edit, curs - edit->curs1);
1831 edit->force |= REDRAW_PAGE;
1832 send_message ((Widget *) edit, WIDGET_DRAW, 0);
1835 /* --------------------------------------------------------------------------------------------- */
1838 edit_get_byte (WEdit * edit, long byte_index)
1840 unsigned long p;
1841 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1842 return '\n';
1844 if (byte_index >= edit->curs1)
1846 p = edit->curs1 + edit->curs2 - byte_index - 1;
1847 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1849 else
1851 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1855 /* --------------------------------------------------------------------------------------------- */
1858 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1860 gchar *str = NULL;
1861 int res = -1;
1862 gunichar ch;
1863 gchar *next_ch = NULL;
1864 int width = 0;
1865 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1867 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1869 *char_width = 0;
1870 return '\n';
1873 str = edit_get_byte_ptr (edit, byte_index);
1875 if (str == NULL)
1877 *char_width = 0;
1878 return 0;
1881 res = g_utf8_get_char_validated (str, -1);
1883 if (res < 0)
1885 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1886 int i;
1887 for (i = 0; i < UTF8_CHAR_LEN; i++)
1888 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1889 utf8_buf[UTF8_CHAR_LEN] = '\0';
1890 str = utf8_buf;
1891 res = g_utf8_get_char_validated (str, -1);
1894 if (res < 0)
1896 ch = *str;
1897 width = 0;
1899 else
1901 ch = res;
1902 /* Calculate UTF-8 char width */
1903 next_ch = g_utf8_next_char (str);
1904 if (next_ch)
1906 width = next_ch - str;
1908 else
1910 ch = 0;
1911 width = 0;
1914 *char_width = width;
1915 return ch;
1918 /* --------------------------------------------------------------------------------------------- */
1920 char *
1921 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1923 int i;
1924 char *p, *writename;
1925 const vfs_path_element_t *path_element;
1927 i = edit_find_filter (filename_vpath);
1928 if (i < 0)
1929 return NULL;
1931 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1932 writename = name_quote (path_element->path, 0);
1933 p = g_strdup_printf (all_filters[i].write, writename);
1934 g_free (writename);
1935 return p;
1938 /* --------------------------------------------------------------------------------------------- */
1940 long
1941 edit_write_stream (WEdit * edit, FILE * f)
1943 long i;
1945 if (edit->lb == LB_ASIS)
1947 for (i = 0; i < edit->last_byte; i++)
1948 if (fputc (edit_get_byte (edit, i), f) < 0)
1949 break;
1950 return i;
1953 /* change line breaks */
1954 for (i = 0; i < edit->last_byte; i++)
1956 unsigned char c = edit_get_byte (edit, i);
1958 if (!(c == '\n' || c == '\r'))
1960 /* not line break */
1961 if (fputc (c, f) < 0)
1962 return i;
1964 else
1965 { /* (c == '\n' || c == '\r') */
1966 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1968 switch (edit->lb)
1970 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1971 /* put one line break unconditionally */
1972 if (fputc ('\n', f) < 0)
1973 return i;
1975 i++; /* 2 chars are processed */
1977 if (c == '\r' && c1 == '\n')
1978 /* Windows line break; go to the next char */
1979 break;
1981 if (c == '\r' && c1 == '\r')
1983 /* two Macintosh line breaks; put second line break */
1984 if (fputc ('\n', f) < 0)
1985 return i;
1986 break;
1989 if (fputc (c1, f) < 0)
1990 return i;
1991 break;
1993 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1994 /* put one line break unconditionally */
1995 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1996 return i;
1998 if (c == '\r' && c1 == '\n')
1999 /* Windows line break; go to the next char */
2000 i++;
2001 break;
2003 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2004 /* put one line break unconditionally */
2005 if (fputc ('\r', f) < 0)
2006 return i;
2008 i++; /* 2 chars are processed */
2010 if (c == '\r' && c1 == '\n')
2011 /* Windows line break; go to the next char */
2012 break;
2014 if (c == '\n' && c1 == '\n')
2016 /* two Windows line breaks; put second line break */
2017 if (fputc ('\r', f) < 0)
2018 return i;
2019 break;
2022 if (fputc (c1, f) < 0)
2023 return i;
2024 break;
2025 case LB_ASIS: /* default without changes */
2026 break;
2031 return edit->last_byte;
2034 /* --------------------------------------------------------------------------------------------- */
2036 gboolean
2037 is_break_char (char c)
2039 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2042 /* --------------------------------------------------------------------------------------------- */
2044 char *
2045 edit_get_word_from_pos (WEdit * edit, long start_pos, long *start, gsize * len, gsize * cut)
2047 long word_start;
2048 long cut_len = 0;
2049 GString *match_expr;
2050 unsigned char *bufpos;
2051 int c1, c2;
2053 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2055 c1 = edit_get_byte (edit, word_start);
2056 c2 = edit_get_byte (edit, word_start - 1);
2058 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2059 break;
2062 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2063 match_expr = g_string_sized_new (16);
2067 c1 = edit_get_byte (edit, word_start + match_expr->len);
2068 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2069 g_string_append_c (match_expr, c1);
2071 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2073 *len = match_expr->len;
2074 *start = word_start;
2075 *cut = cut_len;
2077 return g_string_free (match_expr, FALSE);
2080 /* --------------------------------------------------------------------------------------------- */
2081 /** inserts a file at the cursor, returns count of inserted bytes on success */
2083 long
2084 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath, LineBreaks lb_type)
2086 char *p;
2087 long ins_len = 0;
2089 p = edit_get_filter (filename_vpath);
2090 if (p != NULL)
2092 FILE *f;
2093 long current = edit->curs1;
2095 f = (FILE *) popen (p, "r");
2096 if (f != NULL)
2098 edit_insert_stream (edit, f);
2099 ins_len = edit->curs1 - current;
2100 edit_cursor_move (edit, -ins_len);
2101 if (pclose (f) > 0)
2103 char *errmsg;
2105 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2106 edit_error_dialog (_("Error"), errmsg);
2107 g_free (errmsg);
2108 ins_len = -1;
2111 else
2113 char *errmsg;
2115 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2116 edit_error_dialog (_("Error"), errmsg);
2117 g_free (errmsg);
2118 ins_len = -1;
2120 g_free (p);
2122 else
2124 int i, file, blocklen;
2125 long current = edit->curs1;
2126 int vertical_insertion = 0;
2127 char *buf;
2129 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2130 if (file == -1)
2131 return -1;
2133 buf = g_malloc0 (TEMP_BUF_LEN);
2134 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2135 if (blocklen > 0)
2137 /* if contain signature VERTICAL_MAGIC then it vertical block */
2138 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2139 vertical_insertion = 1;
2140 else
2141 mc_lseek (file, 0, SEEK_SET);
2144 if (vertical_insertion)
2146 long mark1, mark2;
2147 int c1, c2;
2149 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2150 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2151 /* highlight inserted text then not persistent blocks */
2152 if (!option_persistent_selections)
2154 if (!edit->column_highlight)
2155 edit_push_undo_action (edit, COLUMN_OFF);
2156 edit->column_highlight = 1;
2159 else
2161 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2163 for (i = 0; i < blocklen; i++)
2165 if (buf[i] == '\r')
2167 if (lb_type == LB_MAC)
2168 edit_insert (edit, '\n');
2169 else if (lb_type == LB_WIN)
2170 /* just skip */ ;
2171 else
2172 edit_insert (edit, '\r');
2174 else
2175 edit_insert (edit, buf[i]);
2178 /* highlight inserted text then not persistent blocks */
2179 if (!option_persistent_selections && edit->modified)
2181 edit_set_markers (edit, edit->curs1, current, 0, 0);
2182 if (edit->column_highlight)
2183 edit_push_undo_action (edit, COLUMN_ON);
2184 edit->column_highlight = 0;
2188 edit->force |= REDRAW_PAGE;
2189 ins_len = edit->curs1 - current;
2190 edit_cursor_move (edit, -ins_len);
2191 g_free (buf);
2192 mc_close (file);
2193 if (blocklen != 0)
2194 ins_len = 0;
2197 return ins_len;
2200 /* --------------------------------------------------------------------------------------------- */
2202 * Fill in the edit structure. Return NULL on failure. Pass edit as
2203 * NULL to allocate a new structure.
2205 * If line is 0, try to restore saved position. Otherwise put the
2206 * cursor on that line and show it in the middle of the screen.
2209 WEdit *
2210 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2211 long line)
2213 gboolean to_free = FALSE;
2215 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2216 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2218 if (edit != NULL)
2219 edit_purge_widget (edit);
2220 else
2222 #ifdef ENABLE_NLS
2224 * Expand option_whole_chars_search by national letters using
2225 * current locale
2228 static char option_whole_chars_search_buf[256];
2230 if (option_whole_chars_search_buf != option_whole_chars_search)
2232 size_t i;
2233 size_t len = str_term_width1 (option_whole_chars_search);
2235 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2237 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2239 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2241 option_whole_chars_search_buf[len++] = i;
2245 option_whole_chars_search_buf[len] = 0;
2246 option_whole_chars_search = option_whole_chars_search_buf;
2248 #endif /* ENABLE_NLS */
2249 edit = g_malloc0 (sizeof (WEdit));
2250 to_free = TRUE;
2253 edit->drag_state = MCEDIT_DRAG_NORMAL;
2254 edit->widget.y = y;
2255 edit->widget.x = x;
2256 edit->widget.lines = lines;
2257 edit->widget.cols = cols;
2258 edit_save_size (edit);
2259 edit->fullscreen = TRUE;
2261 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2262 edit->stat1.st_uid = getuid ();
2263 edit->stat1.st_gid = getgid ();
2264 edit->stat1.st_mtime = 0;
2266 edit->over_col = 0;
2267 edit->bracket = -1;
2268 edit->force |= REDRAW_PAGE;
2270 /* set file name before load file */
2271 edit_set_filename (edit, filename_vpath);
2273 edit->undo_stack_size = START_STACK_SIZE;
2274 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2275 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2277 edit->redo_stack_size = START_STACK_SIZE;
2278 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2279 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2281 edit->highlight = 0;
2282 edit->utf8 = 0;
2283 edit->converter = str_cnv_from_term;
2284 edit_set_codeset (edit);
2286 if (!edit_load_file (edit))
2288 /* edit_load_file already gives an error message */
2289 if (to_free)
2290 g_free (edit);
2291 return NULL;
2294 edit->loading_done = 1;
2295 edit->modified = 0;
2296 edit->locked = 0;
2297 edit_load_syntax (edit, NULL, NULL);
2299 int color;
2300 edit_get_syntax_color (edit, -1, &color);
2303 /* load saved cursor position */
2304 if ((line == 0) && option_save_position)
2305 edit_load_position (edit);
2306 else
2308 if (line <= 0)
2309 line = 1;
2310 edit_move_display (edit, line - 1);
2311 edit_move_to_line (edit, line - 1);
2314 edit_load_macro_cmd (edit);
2316 return edit;
2319 /* --------------------------------------------------------------------------------------------- */
2321 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2322 gboolean
2323 edit_clean (WEdit * edit)
2325 int j = 0;
2327 if (edit == NULL)
2328 return FALSE;
2330 /* a stale lock, remove it */
2331 if (edit->locked)
2332 edit->locked = unlock_file (edit->filename_vpath);
2334 /* save cursor position */
2335 if (option_save_position)
2336 edit_save_position (edit);
2337 else if (edit->serialized_bookmarks != NULL)
2338 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2340 /* File specified on the mcedit command line and never saved */
2341 if (edit->delete_file)
2342 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2344 edit_free_syntax_rules (edit);
2345 book_mark_flush (edit, -1);
2346 for (; j <= MAXBUFF; j++)
2348 g_free (edit->buffers1[j]);
2349 g_free (edit->buffers2[j]);
2352 g_free (edit->undo_stack);
2353 g_free (edit->redo_stack);
2354 vfs_path_free (edit->filename_vpath);
2355 vfs_path_free (edit->dir_vpath);
2356 mc_search_free (edit->search);
2357 edit->search = NULL;
2359 if (edit->converter != str_cnv_from_term)
2360 str_close_conv (edit->converter);
2362 edit_purge_widget (edit);
2364 return TRUE;
2367 /* --------------------------------------------------------------------------------------------- */
2370 * Load a new file into the editor and set line. If it fails, preserve the old file.
2371 * To do it, allocate a new widget, initialize it and, if the new file
2372 * was loaded, copy the data to the old widget.
2374 * @returns TRUE on success, FALSE on failure.
2376 gboolean
2377 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2379 WEdit *e;
2380 int y = edit->widget.y;
2381 int x = edit->widget.x;
2382 int lines = edit->widget.lines;
2383 int columns = edit->widget.cols;
2385 e = g_malloc0 (sizeof (WEdit));
2386 e->widget = edit->widget;
2388 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2390 g_free (e);
2391 return FALSE;
2394 edit_clean (edit);
2395 memcpy (edit, e, sizeof (WEdit));
2396 g_free (e);
2398 return TRUE;
2401 /* --------------------------------------------------------------------------------------------- */
2403 void
2404 edit_set_codeset (WEdit * edit)
2406 #ifdef HAVE_CHARSET
2407 const char *cp_id;
2409 cp_id =
2410 get_codepage_id (mc_global.source_codepage >=
2411 0 ? mc_global.source_codepage : mc_global.display_codepage);
2413 if (cp_id != NULL)
2415 GIConv conv;
2416 conv = str_crt_conv_from (cp_id);
2417 if (conv != INVALID_CONV)
2419 if (edit->converter != str_cnv_from_term)
2420 str_close_conv (edit->converter);
2421 edit->converter = conv;
2425 if (cp_id != NULL)
2426 edit->utf8 = str_isutf8 (cp_id);
2427 #else
2428 (void) edit;
2429 #endif
2433 /* --------------------------------------------------------------------------------------------- */
2435 Recording stack for undo:
2436 The following is an implementation of a compressed stack. Identical
2437 pushes are recorded by a negative prefix indicating the number of times the
2438 same char was pushed. This saves space for repeated curs-left or curs-right
2439 delete etc.
2443 pushed: stored:
2447 b -3
2449 c --> -4
2455 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2456 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2457 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2458 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2459 position.
2461 The only way the cursor moves or the buffer is changed is through the routines:
2462 insert, backspace, insert_ahead, delete, and cursor_move.
2463 These record the reverse undo movements onto the stack each time they are
2464 called.
2466 Each key press results in a set of actions (insert; delete ...). So each time
2467 a key is pressed the current position of start_display is pushed as
2468 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2469 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2470 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2474 void
2475 edit_push_undo_action (WEdit * edit, long c, ...)
2477 unsigned long sp = edit->undo_stack_pointer;
2478 unsigned long spm1;
2479 long *t;
2481 /* first enlarge the stack if necessary */
2482 if (sp > edit->undo_stack_size - 10)
2483 { /* say */
2484 if (option_max_undo < 256)
2485 option_max_undo = 256;
2486 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2488 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2489 if (t)
2491 edit->undo_stack = t;
2492 edit->undo_stack_size <<= 1;
2493 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2497 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2498 if (edit->undo_stack_disable)
2500 edit_push_redo_action (edit, KEY_PRESS);
2501 edit_push_redo_action (edit, c);
2502 return;
2504 else if (edit->redo_stack_reset)
2506 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2509 if (edit->undo_stack_bottom != sp
2510 && spm1 != edit->undo_stack_bottom
2511 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2513 int d;
2514 if (edit->undo_stack[spm1] < 0)
2516 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2517 if (d == c)
2519 if (edit->undo_stack[spm1] > -1000000000)
2521 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2523 edit->undo_stack[spm1]--;
2525 return;
2529 else
2531 d = edit->undo_stack[spm1];
2532 if (d == c)
2534 if (c >= KEY_PRESS)
2535 return; /* --> no need to push multiple do-nothings */
2536 edit->undo_stack[sp] = -2;
2537 goto check_bottom;
2541 edit->undo_stack[sp] = c;
2543 check_bottom:
2544 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2546 /* if the sp wraps round and catches the undo_stack_bottom then erase
2547 * the first set of actions on the stack to make space - by moving
2548 * undo_stack_bottom forward one "key press" */
2549 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2550 if ((unsigned long) c == edit->undo_stack_bottom ||
2551 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2554 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2556 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2557 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2559 /*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: */
2560 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2561 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2563 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2567 void
2568 edit_push_redo_action (WEdit * edit, long c, ...)
2570 unsigned long sp = edit->redo_stack_pointer;
2571 unsigned long spm1;
2572 long *t;
2573 /* first enlarge the stack if necessary */
2574 if (sp > edit->redo_stack_size - 10)
2575 { /* say */
2576 if (option_max_undo < 256)
2577 option_max_undo = 256;
2578 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2580 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2581 if (t)
2583 edit->redo_stack = t;
2584 edit->redo_stack_size <<= 1;
2585 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2589 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2591 if (edit->redo_stack_bottom != sp
2592 && spm1 != edit->redo_stack_bottom
2593 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2595 int d;
2596 if (edit->redo_stack[spm1] < 0)
2598 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2599 if (d == c)
2601 if (edit->redo_stack[spm1] > -1000000000)
2603 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2604 edit->redo_stack[spm1]--;
2605 return;
2609 else
2611 d = edit->redo_stack[spm1];
2612 if (d == c)
2614 if (c >= KEY_PRESS)
2615 return; /* --> no need to push multiple do-nothings */
2616 edit->redo_stack[sp] = -2;
2617 goto redo_check_bottom;
2621 edit->redo_stack[sp] = c;
2623 redo_check_bottom:
2624 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2626 /* if the sp wraps round and catches the redo_stack_bottom then erase
2627 * the first set of actions on the stack to make space - by moving
2628 * redo_stack_bottom forward one "key press" */
2629 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2630 if ((unsigned long) c == edit->redo_stack_bottom ||
2631 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2634 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2636 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2637 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2640 * If a single key produced enough pushes to wrap all the way round then
2641 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2642 * The stack is then initialised:
2645 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2646 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2647 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2651 /* --------------------------------------------------------------------------------------------- */
2653 Basic low level single character buffer alterations and movements at the cursor.
2654 Returns char passed over, inserted or removed.
2657 void
2658 edit_insert (WEdit * edit, int c)
2660 /* check if file has grown to large */
2661 if (edit->last_byte >= SIZE_LIMIT)
2662 return;
2664 /* first we must update the position of the display window */
2665 if (edit->curs1 < edit->start_display)
2667 edit->start_display++;
2668 if (c == '\n')
2669 edit->start_line++;
2672 /* Mark file as modified, unless the file hasn't been fully loaded */
2673 if (edit->loading_done)
2675 edit_modification (edit);
2678 /* now we must update some info on the file and check if a redraw is required */
2679 if (c == '\n')
2681 if (edit->book_mark)
2682 book_mark_inc (edit, edit->curs_line);
2683 edit->curs_line++;
2684 edit->total_lines++;
2685 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2688 /* save the reverse command onto the undo stack */
2689 /* ordinary char and not space */
2690 if (c > 32)
2691 edit_push_undo_action (edit, BACKSPACE);
2692 else
2693 edit_push_undo_action (edit, BACKSPACE_BR);
2694 /* update markers */
2695 edit->mark1 += (edit->mark1 > edit->curs1);
2696 edit->mark2 += (edit->mark2 > edit->curs1);
2697 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2699 /* add a new buffer if we've reached the end of the last one */
2700 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2701 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2703 /* perform the insertion */
2704 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2705 = (unsigned char) c;
2707 /* update file length */
2708 edit->last_byte++;
2710 /* update cursor position */
2711 edit->curs1++;
2714 /* --------------------------------------------------------------------------------------------- */
2715 /** same as edit_insert and move left */
2717 void
2718 edit_insert_ahead (WEdit * edit, int c)
2720 if (edit->last_byte >= SIZE_LIMIT)
2721 return;
2723 if (edit->curs1 < edit->start_display)
2725 edit->start_display++;
2726 if (c == '\n')
2727 edit->start_line++;
2729 edit_modification (edit);
2730 if (c == '\n')
2732 if (edit->book_mark)
2733 book_mark_inc (edit, edit->curs_line);
2734 edit->total_lines++;
2735 edit->force |= REDRAW_AFTER_CURSOR;
2737 /* ordinary char and not space */
2738 if (c > 32)
2739 edit_push_undo_action (edit, DELCHAR);
2740 else
2741 edit_push_undo_action (edit, DELCHAR_BR);
2743 edit->mark1 += (edit->mark1 >= edit->curs1);
2744 edit->mark2 += (edit->mark2 >= edit->curs1);
2745 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2747 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2748 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2749 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2750 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2752 edit->last_byte++;
2753 edit->curs2++;
2757 /* --------------------------------------------------------------------------------------------- */
2760 edit_delete (WEdit * edit, const int byte_delete)
2762 int p = 0;
2763 int cw = 1;
2764 int i;
2766 if (!edit->curs2)
2767 return 0;
2769 cw = 1;
2770 /* if byte_delete = 1 then delete only one byte not multibyte char */
2771 if (edit->utf8 && byte_delete == 0)
2773 edit_get_utf (edit, edit->curs1, &cw);
2774 if (cw < 1)
2775 cw = 1;
2778 if (edit->mark2 != edit->mark1)
2779 edit_push_markers (edit);
2781 for (i = 1; i <= cw; i++)
2783 if (edit->mark1 > edit->curs1)
2785 edit->mark1--;
2786 edit->end_mark_curs--;
2788 if (edit->mark2 > edit->curs1)
2789 edit->mark2--;
2790 if (edit->last_get_rule > edit->curs1)
2791 edit->last_get_rule--;
2793 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2794 ((edit->curs2 -
2795 1) & M_EDIT_BUF_SIZE) - 1];
2797 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2799 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2800 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2802 edit->last_byte--;
2803 edit->curs2--;
2804 edit_push_undo_action (edit, p + 256);
2807 edit_modification (edit);
2808 if (p == '\n')
2810 if (edit->book_mark)
2811 book_mark_dec (edit, edit->curs_line);
2812 edit->total_lines--;
2813 edit->force |= REDRAW_AFTER_CURSOR;
2815 if (edit->curs1 < edit->start_display)
2817 edit->start_display--;
2818 if (p == '\n')
2819 edit->start_line--;
2822 return p;
2825 /* --------------------------------------------------------------------------------------------- */
2828 edit_backspace (WEdit * edit, const int byte_delete)
2830 int p = 0;
2831 int cw = 1;
2832 int i;
2834 if (edit->curs1 == 0)
2835 return 0;
2837 cw = 1;
2839 if (edit->mark2 != edit->mark1)
2840 edit_push_markers (edit);
2842 if (edit->utf8 && byte_delete == 0)
2844 edit_get_prev_utf (edit, edit->curs1, &cw);
2845 if (cw < 1)
2846 cw = 1;
2848 for (i = 1; i <= cw; i++)
2850 if (edit->mark1 >= edit->curs1)
2852 edit->mark1--;
2853 edit->end_mark_curs--;
2855 if (edit->mark2 >= edit->curs1)
2856 edit->mark2--;
2857 if (edit->last_get_rule >= edit->curs1)
2858 edit->last_get_rule--;
2860 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2861 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2862 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2864 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2865 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2867 edit->last_byte--;
2868 edit->curs1--;
2869 edit_push_undo_action (edit, p);
2871 edit_modification (edit);
2872 if (p == '\n')
2874 if (edit->book_mark)
2875 book_mark_dec (edit, edit->curs_line);
2876 edit->curs_line--;
2877 edit->total_lines--;
2878 edit->force |= REDRAW_AFTER_CURSOR;
2881 if (edit->curs1 < edit->start_display)
2883 edit->start_display--;
2884 if (p == '\n')
2885 edit->start_line--;
2888 return p;
2891 /* --------------------------------------------------------------------------------------------- */
2892 /** moves the cursor right or left: increment positive or negative respectively */
2894 void
2895 edit_cursor_move (WEdit * edit, long increment)
2897 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2898 int c;
2900 if (increment < 0)
2902 for (; increment < 0; increment++)
2904 if (!edit->curs1)
2905 return;
2907 edit_push_undo_action (edit, CURS_RIGHT);
2909 c = edit_get_byte (edit, edit->curs1 - 1);
2910 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2911 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2912 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2913 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2914 edit->curs2++;
2915 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2916 1) & M_EDIT_BUF_SIZE];
2917 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2919 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2920 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2922 edit->curs1--;
2923 if (c == '\n')
2925 edit->curs_line--;
2926 edit->force |= REDRAW_LINE_BELOW;
2931 else if (increment > 0)
2933 for (; increment > 0; increment--)
2935 if (!edit->curs2)
2936 return;
2938 edit_push_undo_action (edit, CURS_LEFT);
2940 c = edit_get_byte (edit, edit->curs1);
2941 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2942 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2943 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2944 edit->curs1++;
2945 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2946 ((edit->curs2 -
2947 1) & M_EDIT_BUF_SIZE) - 1];
2948 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2950 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2951 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2953 edit->curs2--;
2954 if (c == '\n')
2956 edit->curs_line++;
2957 edit->force |= REDRAW_LINE_ABOVE;
2963 /* These functions return positions relative to lines */
2965 /* --------------------------------------------------------------------------------------------- */
2966 /** returns index of last char on line + 1 */
2968 long
2969 edit_eol (WEdit * edit, long current)
2971 if (current >= edit->last_byte)
2972 return edit->last_byte;
2974 for (;; current++)
2975 if (edit_get_byte (edit, current) == '\n')
2976 break;
2977 return current;
2980 /* --------------------------------------------------------------------------------------------- */
2981 /** returns index of first char on line */
2983 long
2984 edit_bol (WEdit * edit, long current)
2986 if (current <= 0)
2987 return 0;
2989 for (;; current--)
2990 if (edit_get_byte (edit, current - 1) == '\n')
2991 break;
2992 return current;
2995 /* --------------------------------------------------------------------------------------------- */
2997 long
2998 edit_count_lines (WEdit * edit, long current, long upto)
3000 long lines = 0;
3001 if (upto > edit->last_byte)
3002 upto = edit->last_byte;
3003 if (current < 0)
3004 current = 0;
3005 while (current < upto)
3006 if (edit_get_byte (edit, current++) == '\n')
3007 lines++;
3008 return lines;
3011 /* --------------------------------------------------------------------------------------------- */
3012 /* If lines is zero this returns the count of lines from current to upto. */
3013 /* If upto is zero returns index of lines forward current. */
3015 long
3016 edit_move_forward (WEdit * edit, long current, long lines, long upto)
3018 if (upto)
3020 return edit_count_lines (edit, current, upto);
3022 else
3024 long next;
3025 if (lines < 0)
3026 lines = 0;
3027 while (lines--)
3029 next = edit_eol (edit, current) + 1;
3030 if (next > edit->last_byte)
3031 break;
3032 else
3033 current = next;
3035 return current;
3039 /* --------------------------------------------------------------------------------------------- */
3040 /** Returns offset of 'lines' lines up from current */
3042 long
3043 edit_move_backward (WEdit * edit, long current, long lines)
3045 if (lines < 0)
3046 lines = 0;
3047 current = edit_bol (edit, current);
3048 while ((lines--) && current != 0)
3049 current = edit_bol (edit, current - 1);
3050 return current;
3053 /* --------------------------------------------------------------------------------------------- */
3054 /* If cols is zero this returns the count of columns from current to upto. */
3055 /* If upto is zero returns index of cols across from current. */
3057 long
3058 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3060 long p, q;
3061 int col;
3063 if (upto)
3065 q = upto;
3066 cols = -10;
3068 else
3069 q = edit->last_byte + 2;
3071 for (col = 0, p = current; p < q; p++)
3073 int c, orig_c;
3075 if (cols != -10)
3077 if (col == cols)
3078 return p;
3079 if (col > cols)
3080 return p - 1;
3083 orig_c = c = edit_get_byte (edit, p);
3085 #ifdef HAVE_CHARSET
3086 if (edit->utf8)
3088 int utf_ch;
3089 int cw = 1;
3091 utf_ch = edit_get_utf (edit, p, &cw);
3092 if (mc_global.utf8_display)
3094 if (cw > 1)
3095 col -= cw - 1;
3096 if (g_unichar_iswide (utf_ch))
3097 col++;
3099 else if (cw > 1 && g_unichar_isprint (utf_ch))
3100 col -= cw - 1;
3103 c = convert_to_display_c (c);
3104 #endif
3106 if (c == '\t')
3107 col += TAB_SIZE - col % TAB_SIZE;
3108 else if (c == '\n')
3110 if (upto)
3111 return col;
3112 else
3113 return p;
3115 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3116 /* '\r' is shown as ^M, so we must advance 2 characters */
3117 /* Caret notation for control characters */
3118 col += 2;
3119 else
3120 col++;
3122 return col;
3125 /* --------------------------------------------------------------------------------------------- */
3126 /** returns the current column position of the cursor */
3129 edit_get_col (WEdit * edit)
3131 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3134 /* --------------------------------------------------------------------------------------------- */
3135 /* Scrolling functions */
3136 /* --------------------------------------------------------------------------------------------- */
3138 void
3139 edit_update_curs_row (WEdit * edit)
3141 edit->curs_row = edit->curs_line - edit->start_line;
3144 /* --------------------------------------------------------------------------------------------- */
3146 void
3147 edit_update_curs_col (WEdit * edit)
3149 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3152 /* --------------------------------------------------------------------------------------------- */
3155 edit_get_curs_col (const WEdit * edit)
3157 return edit->curs_col;
3160 /* --------------------------------------------------------------------------------------------- */
3161 /** moves the display start position up by i lines */
3163 void
3164 edit_scroll_upward (WEdit * edit, unsigned long i)
3166 unsigned long lines_above = edit->start_line;
3167 if (i > lines_above)
3168 i = lines_above;
3169 if (i)
3171 edit->start_line -= i;
3172 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3173 edit->force |= REDRAW_PAGE;
3174 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3176 edit_update_curs_row (edit);
3180 /* --------------------------------------------------------------------------------------------- */
3181 /** returns 1 if could scroll, 0 otherwise */
3183 void
3184 edit_scroll_downward (WEdit * edit, int i)
3186 int lines_below;
3187 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3188 if (lines_below > 0)
3190 if (i > lines_below)
3191 i = lines_below;
3192 edit->start_line += i;
3193 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3194 edit->force |= REDRAW_PAGE;
3195 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3197 edit_update_curs_row (edit);
3200 /* --------------------------------------------------------------------------------------------- */
3202 void
3203 edit_scroll_right (WEdit * edit, int i)
3205 edit->force |= REDRAW_PAGE;
3206 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3207 edit->start_col -= i;
3210 /* --------------------------------------------------------------------------------------------- */
3212 void
3213 edit_scroll_left (WEdit * edit, int i)
3215 if (edit->start_col)
3217 edit->start_col += i;
3218 if (edit->start_col > 0)
3219 edit->start_col = 0;
3220 edit->force |= REDRAW_PAGE;
3221 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3225 /* --------------------------------------------------------------------------------------------- */
3226 /* high level cursor movement commands */
3227 /* --------------------------------------------------------------------------------------------- */
3229 void
3230 edit_move_to_prev_col (WEdit * edit, long p)
3232 int prev = edit->prev_col;
3233 int over = edit->over_col;
3234 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3236 if (option_cursor_beyond_eol)
3238 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3239 edit_eol (edit, edit->curs1));
3241 if (line_len < prev + edit->over_col)
3243 edit->over_col = prev + over - line_len;
3244 edit->prev_col = line_len;
3245 edit->curs_col = line_len;
3247 else
3249 edit->curs_col = prev + over;
3250 edit->prev_col = edit->curs_col;
3251 edit->over_col = 0;
3254 else
3256 edit->over_col = 0;
3257 if (is_in_indent (edit) && option_fake_half_tabs)
3259 edit_update_curs_col (edit);
3260 if (space_width)
3261 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3263 int q = edit->curs_col;
3264 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3265 p = edit_bol (edit, edit->curs1);
3266 edit_cursor_move (edit,
3267 edit_move_forward3 (edit, p, edit->curs_col,
3268 0) - edit->curs1);
3269 if (!left_of_four_spaces (edit))
3270 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3276 /* --------------------------------------------------------------------------------------------- */
3279 line_is_blank (WEdit * edit, long line)
3281 return is_blank (edit, edit_find_line (edit, line));
3284 /* --------------------------------------------------------------------------------------------- */
3285 /** move cursor to line 'line' */
3287 void
3288 edit_move_to_line (WEdit * e, long line)
3290 if (line < e->curs_line)
3291 edit_move_up (e, e->curs_line - line, 0);
3292 else
3293 edit_move_down (e, line - e->curs_line, 0);
3294 edit_scroll_screen_over_cursor (e);
3297 /* --------------------------------------------------------------------------------------------- */
3298 /** scroll window so that first visible line is 'line' */
3300 void
3301 edit_move_display (WEdit * e, long line)
3303 if (line < e->start_line)
3304 edit_scroll_upward (e, e->start_line - line);
3305 else
3306 edit_scroll_downward (e, line - e->start_line);
3309 /* --------------------------------------------------------------------------------------------- */
3310 /** save markers onto undo stack */
3312 void
3313 edit_push_markers (WEdit * edit)
3315 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3316 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3317 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3320 /* --------------------------------------------------------------------------------------------- */
3322 void
3323 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3325 edit->mark1 = m1;
3326 edit->mark2 = m2;
3327 edit->column1 = c1;
3328 edit->column2 = c2;
3332 /* --------------------------------------------------------------------------------------------- */
3333 /** highlight marker toggle */
3335 void
3336 edit_mark_cmd (WEdit * edit, int unmark)
3338 edit_push_markers (edit);
3339 if (unmark)
3341 edit_set_markers (edit, 0, 0, 0, 0);
3342 edit->force |= REDRAW_PAGE;
3344 else
3346 if (edit->mark2 >= 0)
3348 edit->end_mark_curs = -1;
3349 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3350 edit->curs_col + edit->over_col);
3351 edit->force |= REDRAW_PAGE;
3353 else
3355 edit->end_mark_curs = edit->curs1;
3356 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3357 edit->curs_col + edit->over_col);
3362 /* --------------------------------------------------------------------------------------------- */
3363 /** highlight the word under cursor */
3365 void
3366 edit_mark_current_word_cmd (WEdit * edit)
3368 long pos;
3370 for (pos = edit->curs1; pos != 0; pos--)
3372 int c1, c2;
3374 c1 = edit_get_byte (edit, pos);
3375 c2 = edit_get_byte (edit, pos - 1);
3376 if (!isspace (c1) && isspace (c2))
3377 break;
3378 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3379 break;
3381 edit->mark1 = pos;
3383 for (; pos < edit->last_byte; pos++)
3385 int c1, c2;
3387 c1 = edit_get_byte (edit, pos);
3388 c2 = edit_get_byte (edit, pos + 1);
3389 if (!isspace (c1) && isspace (c2))
3390 break;
3391 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3392 break;
3394 edit->mark2 = min (pos + 1, edit->last_byte);
3396 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3399 /* --------------------------------------------------------------------------------------------- */
3401 void
3402 edit_mark_current_line_cmd (WEdit * edit)
3404 long pos = edit->curs1;
3406 edit->mark1 = edit_bol (edit, pos);
3407 edit->mark2 = edit_eol (edit, pos);
3409 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3412 /* --------------------------------------------------------------------------------------------- */
3414 void
3415 edit_delete_line (WEdit * edit)
3418 * Delete right part of the line.
3419 * Note that edit_get_byte() returns '\n' when byte position is
3420 * beyond EOF.
3422 while (edit_get_byte (edit, edit->curs1) != '\n')
3424 (void) edit_delete (edit, 1);
3428 * Delete '\n' char.
3429 * Note that edit_delete() will not corrupt anything if called while
3430 * cursor position is EOF.
3432 (void) edit_delete (edit, 1);
3435 * Delete left part of the line.
3436 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3438 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3440 (void) edit_backspace (edit, 1);
3444 /* --------------------------------------------------------------------------------------------- */
3447 edit_indent_width (WEdit * edit, long p)
3449 long q = p;
3450 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3451 q++;
3452 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3455 /* --------------------------------------------------------------------------------------------- */
3457 void
3458 edit_insert_indent (WEdit * edit, int indent)
3460 if (!option_fill_tabs_with_spaces)
3462 while (indent >= TAB_SIZE)
3464 edit_insert (edit, '\t');
3465 indent -= TAB_SIZE;
3468 while (indent-- > 0)
3469 edit_insert (edit, ' ');
3472 /* --------------------------------------------------------------------------------------------- */
3474 void
3475 edit_push_key_press (WEdit * edit)
3477 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3478 if (edit->mark2 == -1)
3480 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3481 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3485 /* --------------------------------------------------------------------------------------------- */
3487 void
3488 edit_find_bracket (WEdit * edit)
3490 edit->bracket = edit_get_bracket (edit, 1, 10000);
3491 if (last_bracket != edit->bracket)
3492 edit->force |= REDRAW_PAGE;
3493 last_bracket = edit->bracket;
3496 /* --------------------------------------------------------------------------------------------- */
3498 * This executes a command as though the user initiated it through a key
3499 * press. Callback with WIDGET_KEY as a message calls this after
3500 * translating the key press. This function can be used to pass any
3501 * command to the editor. Note that the screen wouldn't update
3502 * automatically. Either of command or char_for_insertion must be
3503 * passed as -1. Commands are executed, and char_for_insertion is
3504 * inserted at the cursor.
3507 void
3508 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3510 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3511 || (macro_index < 0
3512 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3514 macro_index = 0;
3515 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3516 return;
3518 if (macro_index != -1)
3520 edit->force |= REDRAW_COMPLETELY;
3521 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3523 edit_store_macro_cmd (edit);
3524 macro_index = -1;
3525 return;
3527 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3529 edit_repeat_macro_cmd (edit);
3530 macro_index = -1;
3531 return;
3535 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3537 record_macro_buf[macro_index].action = command;
3538 record_macro_buf[macro_index++].ch = char_for_insertion;
3540 /* record the beginning of a set of editing actions initiated by a key press */
3541 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3542 edit_push_key_press (edit);
3544 edit_execute_cmd (edit, command, char_for_insertion);
3545 if (edit->column_highlight)
3546 edit->force |= REDRAW_PAGE;
3549 /* --------------------------------------------------------------------------------------------- */
3551 This executes a command at a lower level than macro recording.
3552 It also does not push a key_press onto the undo stack. This means
3553 that if it is called many times, a single undo command will undo
3554 all of them. It also does not check for the Undo command.
3556 void
3557 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3559 if (command == CK_WindowFullscreen)
3561 edit_toggle_fullscreen (edit);
3562 return;
3565 /* handle window state */
3566 if (edit_handle_move_resize (edit, command))
3567 return;
3569 edit->force |= REDRAW_LINE;
3571 /* The next key press will unhighlight the found string, so update
3572 * the whole page */
3573 if (edit->found_len || edit->column_highlight)
3574 edit->force |= REDRAW_PAGE;
3576 switch (command)
3578 /* a mark command with shift-arrow */
3579 case CK_MarkLeft:
3580 case CK_MarkRight:
3581 case CK_MarkToWordBegin:
3582 case CK_MarkToWordEnd:
3583 case CK_MarkToHome:
3584 case CK_MarkToEnd:
3585 case CK_MarkUp:
3586 case CK_MarkDown:
3587 case CK_MarkPageUp:
3588 case CK_MarkPageDown:
3589 case CK_MarkToFileBegin:
3590 case CK_MarkToFileEnd:
3591 case CK_MarkToPageBegin:
3592 case CK_MarkToPageEnd:
3593 case CK_MarkScrollUp:
3594 case CK_MarkScrollDown:
3595 case CK_MarkParagraphUp:
3596 case CK_MarkParagraphDown:
3597 /* a mark command with alt-arrow */
3598 case CK_MarkColumnPageUp:
3599 case CK_MarkColumnPageDown:
3600 case CK_MarkColumnLeft:
3601 case CK_MarkColumnRight:
3602 case CK_MarkColumnUp:
3603 case CK_MarkColumnDown:
3604 case CK_MarkColumnScrollUp:
3605 case CK_MarkColumnScrollDown:
3606 case CK_MarkColumnParagraphUp:
3607 case CK_MarkColumnParagraphDown:
3608 edit->column_highlight = 0;
3609 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3611 edit_mark_cmd (edit, 1); /* clear */
3612 edit_mark_cmd (edit, 0); /* marking on */
3614 edit->highlight = 1;
3615 break;
3617 /* any other command */
3618 default:
3619 if (edit->highlight)
3620 edit_mark_cmd (edit, 0); /* clear */
3621 edit->highlight = 0;
3624 /* first check for undo */
3625 if (command == CK_Undo)
3627 edit->redo_stack_reset = 0;
3628 edit_group_undo (edit);
3629 edit->found_len = 0;
3630 edit->prev_col = edit_get_col (edit);
3631 edit->search_start = edit->curs1;
3632 return;
3634 /* check for redo */
3635 if (command == CK_Redo)
3637 edit->redo_stack_reset = 0;
3638 edit_do_redo (edit);
3639 edit->found_len = 0;
3640 edit->prev_col = edit_get_col (edit);
3641 edit->search_start = edit->curs1;
3642 return;
3645 edit->redo_stack_reset = 1;
3647 /* An ordinary key press */
3648 if (char_for_insertion >= 0)
3650 /* if non persistent selection and text selected */
3651 if (!option_persistent_selections)
3653 if (edit->mark1 != edit->mark2)
3654 edit_block_delete_cmd (edit);
3656 if (edit->overwrite)
3658 /* remove char only one time, after input first byte, multibyte chars */
3659 if ((!mc_global.utf8_display || edit->charpoint == 0)
3660 && edit_get_byte (edit, edit->curs1) != '\n')
3661 edit_delete (edit, 0);
3663 if (option_cursor_beyond_eol && edit->over_col > 0)
3664 edit_insert_over (edit);
3665 #ifdef HAVE_CHARSET
3666 if (char_for_insertion > 255 && !mc_global.utf8_display)
3668 unsigned char str[6 + 1];
3669 size_t i = 0;
3670 int res;
3672 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3673 if (res == 0)
3675 str[0] = '.';
3676 str[1] = '\0';
3678 else
3680 str[res] = '\0';
3682 while (str[i] != 0 && i <= 6)
3684 char_for_insertion = str[i];
3685 edit_insert (edit, char_for_insertion);
3686 i++;
3689 else
3690 #endif
3691 edit_insert (edit, char_for_insertion);
3693 if (option_auto_para_formatting)
3695 format_paragraph (edit, 0);
3696 edit->force |= REDRAW_PAGE;
3698 else
3699 check_and_wrap_line (edit);
3700 edit->found_len = 0;
3701 edit->prev_col = edit_get_col (edit);
3702 edit->search_start = edit->curs1;
3703 edit_find_bracket (edit);
3704 return;
3707 switch (command)
3709 case CK_TopOnScreen:
3710 case CK_BottomOnScreen:
3711 case CK_Top:
3712 case CK_Bottom:
3713 case CK_PageUp:
3714 case CK_PageDown:
3715 case CK_Home:
3716 case CK_End:
3717 case CK_Up:
3718 case CK_Down:
3719 case CK_Left:
3720 case CK_Right:
3721 case CK_WordLeft:
3722 case CK_WordRight:
3723 if (edit->mark2 >= 0)
3725 if (!option_persistent_selections)
3727 if (edit->column_highlight)
3728 edit_push_undo_action (edit, COLUMN_ON);
3729 edit->column_highlight = 0;
3730 edit_mark_cmd (edit, 1);
3735 switch (command)
3737 case CK_TopOnScreen:
3738 case CK_BottomOnScreen:
3739 case CK_MarkToPageBegin:
3740 case CK_MarkToPageEnd:
3741 case CK_Up:
3742 case CK_Down:
3743 case CK_WordLeft:
3744 case CK_WordRight:
3745 case CK_MarkToWordBegin:
3746 case CK_MarkToWordEnd:
3747 case CK_MarkUp:
3748 case CK_MarkDown:
3749 case CK_MarkColumnUp:
3750 case CK_MarkColumnDown:
3751 if (edit->mark2 == -1)
3752 break; /*marking is following the cursor: may need to highlight a whole line */
3753 case CK_Left:
3754 case CK_Right:
3755 case CK_MarkLeft:
3756 case CK_MarkRight:
3757 edit->force |= REDRAW_CHAR_ONLY;
3760 /* basic cursor key commands */
3761 switch (command)
3763 case CK_BackSpace:
3764 /* if non persistent selection and text selected */
3765 if (!option_persistent_selections)
3767 if (edit->mark1 != edit->mark2)
3769 edit_block_delete_cmd (edit);
3770 break;
3773 if (option_cursor_beyond_eol && edit->over_col > 0)
3775 edit->over_col--;
3776 break;
3778 if (option_backspace_through_tabs && is_in_indent (edit))
3780 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3781 edit_backspace (edit, 1);
3782 break;
3784 else
3786 if (option_fake_half_tabs)
3788 int i;
3789 if (is_in_indent (edit) && right_of_four_spaces (edit))
3791 for (i = 0; i < HALF_TAB_SIZE; i++)
3792 edit_backspace (edit, 1);
3793 break;
3797 edit_backspace (edit, 0);
3798 break;
3799 case CK_Delete:
3800 /* if non persistent selection and text selected */
3801 if (!option_persistent_selections)
3803 if (edit->mark1 != edit->mark2)
3805 edit_block_delete_cmd (edit);
3806 break;
3810 if (option_cursor_beyond_eol && edit->over_col > 0)
3811 edit_insert_over (edit);
3813 if (option_fake_half_tabs)
3815 int i;
3816 if (is_in_indent (edit) && left_of_four_spaces (edit))
3818 for (i = 1; i <= HALF_TAB_SIZE; i++)
3819 edit_delete (edit, 1);
3820 break;
3823 edit_delete (edit, 0);
3824 break;
3825 case CK_DeleteToWordBegin:
3826 edit->over_col = 0;
3827 edit_left_delete_word (edit);
3828 break;
3829 case CK_DeleteToWordEnd:
3830 if (option_cursor_beyond_eol && edit->over_col > 0)
3831 edit_insert_over (edit);
3833 edit_right_delete_word (edit);
3834 break;
3835 case CK_DeleteLine:
3836 edit_delete_line (edit);
3837 break;
3838 case CK_DeleteToHome:
3839 edit_delete_to_line_begin (edit);
3840 break;
3841 case CK_DeleteToEnd:
3842 edit_delete_to_line_end (edit);
3843 break;
3844 case CK_Enter:
3845 edit->over_col = 0;
3846 if (option_auto_para_formatting)
3848 edit_double_newline (edit);
3849 if (option_return_does_auto_indent)
3850 edit_auto_indent (edit);
3851 format_paragraph (edit, 0);
3853 else
3855 edit_insert (edit, '\n');
3856 if (option_return_does_auto_indent)
3858 edit_auto_indent (edit);
3861 break;
3862 case CK_Return:
3863 edit_insert (edit, '\n');
3864 break;
3866 case CK_MarkColumnPageUp:
3867 edit->column_highlight = 1;
3868 case CK_PageUp:
3869 case CK_MarkPageUp:
3870 edit_move_up (edit, edit->widget.lines - 1, 1);
3871 break;
3872 case CK_MarkColumnPageDown:
3873 edit->column_highlight = 1;
3874 case CK_PageDown:
3875 case CK_MarkPageDown:
3876 edit_move_down (edit, edit->widget.lines - 1, 1);
3877 break;
3878 case CK_MarkColumnLeft:
3879 edit->column_highlight = 1;
3880 case CK_Left:
3881 case CK_MarkLeft:
3882 if (option_fake_half_tabs)
3884 if (is_in_indent (edit) && right_of_four_spaces (edit))
3886 if (option_cursor_beyond_eol && edit->over_col > 0)
3887 edit->over_col--;
3888 else
3889 edit_cursor_move (edit, -HALF_TAB_SIZE);
3890 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3891 break;
3894 edit_left_char_move_cmd (edit);
3895 break;
3896 case CK_MarkColumnRight:
3897 edit->column_highlight = 1;
3898 case CK_Right:
3899 case CK_MarkRight:
3900 if (option_fake_half_tabs)
3902 if (is_in_indent (edit) && left_of_four_spaces (edit))
3904 edit_cursor_move (edit, HALF_TAB_SIZE);
3905 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3906 break;
3909 edit_right_char_move_cmd (edit);
3910 break;
3911 case CK_TopOnScreen:
3912 case CK_MarkToPageBegin:
3913 edit_begin_page (edit);
3914 break;
3915 case CK_BottomOnScreen:
3916 case CK_MarkToPageEnd:
3917 edit_end_page (edit);
3918 break;
3919 case CK_WordLeft:
3920 case CK_MarkToWordBegin:
3921 edit->over_col = 0;
3922 edit_left_word_move_cmd (edit);
3923 break;
3924 case CK_WordRight:
3925 case CK_MarkToWordEnd:
3926 edit->over_col = 0;
3927 edit_right_word_move_cmd (edit);
3928 break;
3929 case CK_MarkColumnUp:
3930 edit->column_highlight = 1;
3931 case CK_Up:
3932 case CK_MarkUp:
3933 edit_move_up (edit, 1, 0);
3934 break;
3935 case CK_MarkColumnDown:
3936 edit->column_highlight = 1;
3937 case CK_Down:
3938 case CK_MarkDown:
3939 edit_move_down (edit, 1, 0);
3940 break;
3941 case CK_MarkColumnParagraphUp:
3942 edit->column_highlight = 1;
3943 case CK_ParagraphUp:
3944 case CK_MarkParagraphUp:
3945 edit_move_up_paragraph (edit, 0);
3946 break;
3947 case CK_MarkColumnParagraphDown:
3948 edit->column_highlight = 1;
3949 case CK_ParagraphDown:
3950 case CK_MarkParagraphDown:
3951 edit_move_down_paragraph (edit, 0);
3952 break;
3953 case CK_MarkColumnScrollUp:
3954 edit->column_highlight = 1;
3955 case CK_ScrollUp:
3956 case CK_MarkScrollUp:
3957 edit_move_up (edit, 1, 1);
3958 break;
3959 case CK_MarkColumnScrollDown:
3960 edit->column_highlight = 1;
3961 case CK_ScrollDown:
3962 case CK_MarkScrollDown:
3963 edit_move_down (edit, 1, 1);
3964 break;
3965 case CK_Home:
3966 case CK_MarkToHome:
3967 edit_cursor_to_bol (edit);
3968 break;
3969 case CK_End:
3970 case CK_MarkToEnd:
3971 edit_cursor_to_eol (edit);
3972 break;
3973 case CK_Tab:
3974 /* if text marked shift block */
3975 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3977 if (edit->mark2 < 0)
3978 edit_mark_cmd (edit, 0);
3979 edit_move_block_to_right (edit);
3981 else
3983 if (option_cursor_beyond_eol)
3984 edit_insert_over (edit);
3985 edit_tab_cmd (edit);
3986 if (option_auto_para_formatting)
3988 format_paragraph (edit, 0);
3989 edit->force |= REDRAW_PAGE;
3991 else
3993 check_and_wrap_line (edit);
3996 break;
3998 case CK_InsertOverwrite:
3999 edit->overwrite = !edit->overwrite;
4000 break;
4002 case CK_Mark:
4003 if (edit->mark2 >= 0)
4005 if (edit->column_highlight)
4006 edit_push_undo_action (edit, COLUMN_ON);
4007 edit->column_highlight = 0;
4009 edit_mark_cmd (edit, 0);
4010 break;
4011 case CK_MarkColumn:
4012 if (!edit->column_highlight)
4013 edit_push_undo_action (edit, COLUMN_OFF);
4014 edit->column_highlight = 1;
4015 edit_mark_cmd (edit, 0);
4016 break;
4017 case CK_MarkAll:
4018 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
4019 edit->force |= REDRAW_PAGE;
4020 break;
4021 case CK_Unmark:
4022 if (edit->column_highlight)
4023 edit_push_undo_action (edit, COLUMN_ON);
4024 edit->column_highlight = 0;
4025 edit_mark_cmd (edit, 1);
4026 break;
4027 case CK_MarkWord:
4028 if (edit->column_highlight)
4029 edit_push_undo_action (edit, COLUMN_ON);
4030 edit->column_highlight = 0;
4031 edit_mark_current_word_cmd (edit);
4032 break;
4033 case CK_MarkLine:
4034 if (edit->column_highlight)
4035 edit_push_undo_action (edit, COLUMN_ON);
4036 edit->column_highlight = 0;
4037 edit_mark_current_line_cmd (edit);
4038 break;
4040 case CK_Bookmark:
4041 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4042 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4043 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4044 else
4045 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4046 break;
4047 case CK_BookmarkFlush:
4048 book_mark_flush (edit, BOOK_MARK_COLOR);
4049 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4050 edit->force |= REDRAW_PAGE;
4051 break;
4052 case CK_BookmarkNext:
4053 if (edit->book_mark)
4055 struct _book_mark *p;
4056 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4057 if (p->next)
4059 p = p->next;
4060 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4061 edit_move_display (edit, p->line - edit->widget.lines / 2);
4062 edit_move_to_line (edit, p->line);
4065 break;
4066 case CK_BookmarkPrev:
4067 if (edit->book_mark)
4069 struct _book_mark *p;
4070 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4071 while (p->line == edit->curs_line)
4072 if (p->prev)
4073 p = p->prev;
4074 if (p->line >= 0)
4076 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4077 edit_move_display (edit, p->line - edit->widget.lines / 2);
4078 edit_move_to_line (edit, p->line);
4081 break;
4083 case CK_Top:
4084 case CK_MarkToFileBegin:
4085 edit_move_to_top (edit);
4086 break;
4087 case CK_Bottom:
4088 case CK_MarkToFileEnd:
4089 edit_move_to_bottom (edit);
4090 break;
4092 case CK_Copy:
4093 if (option_cursor_beyond_eol && edit->over_col > 0)
4094 edit_insert_over (edit);
4095 edit_block_copy_cmd (edit);
4096 break;
4097 case CK_Remove:
4098 edit_block_delete_cmd (edit);
4099 break;
4100 case CK_Move:
4101 edit_block_move_cmd (edit);
4102 break;
4104 case CK_BlockShiftLeft:
4105 if (edit->mark1 != edit->mark2)
4106 edit_move_block_to_left (edit);
4107 break;
4108 case CK_BlockShiftRight:
4109 if (edit->mark1 != edit->mark2)
4110 edit_move_block_to_right (edit);
4111 break;
4112 case CK_Store:
4113 edit_copy_to_X_buf_cmd (edit);
4114 break;
4115 case CK_Cut:
4116 edit_cut_to_X_buf_cmd (edit);
4117 break;
4118 case CK_Paste:
4119 /* if non persistent selection and text selected */
4120 if (!option_persistent_selections)
4122 if (edit->mark1 != edit->mark2)
4123 edit_block_delete_cmd (edit);
4125 if (option_cursor_beyond_eol && edit->over_col > 0)
4126 edit_insert_over (edit);
4127 edit_paste_from_X_buf_cmd (edit);
4128 break;
4129 case CK_History:
4130 edit_paste_from_history (edit);
4131 break;
4133 case CK_SaveAs:
4134 edit_save_as_cmd (edit);
4135 break;
4136 case CK_Save:
4137 edit_save_confirm_cmd (edit);
4138 break;
4139 case CK_BlockSave:
4140 edit_save_block_cmd (edit);
4141 break;
4142 case CK_InsertFile:
4143 edit_insert_file_cmd (edit);
4144 break;
4146 case CK_FilePrev:
4147 edit_load_back_cmd (edit);
4148 break;
4149 case CK_FileNext:
4150 edit_load_forward_cmd (edit);
4151 break;
4153 case CK_SyntaxChoose:
4154 edit_syntax_dialog (edit);
4155 break;
4157 case CK_Search:
4158 edit_search_cmd (edit, FALSE);
4159 break;
4160 case CK_SearchContinue:
4161 edit_search_cmd (edit, TRUE);
4162 break;
4163 case CK_Replace:
4164 edit_replace_cmd (edit, 0);
4165 break;
4166 case CK_ReplaceContinue:
4167 edit_replace_cmd (edit, 1);
4168 break;
4169 case CK_Complete:
4170 /* if text marked shift block */
4171 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4173 edit_move_block_to_left (edit);
4175 else
4177 edit_complete_word_cmd (edit);
4179 break;
4180 case CK_Find:
4181 edit_get_match_keyword_cmd (edit);
4182 break;
4183 #ifdef HAVE_ASPELL
4184 case CK_SpellCheckCurrentWord:
4185 edit_suggest_current_word (edit);
4186 break;
4187 case CK_SpellCheck:
4188 edit_spellcheck_file (edit);
4189 break;
4190 case CK_SpellCheckSelectLang:
4191 edit_set_spell_lang ();
4192 break;
4193 #endif
4194 case CK_Date:
4196 char s[BUF_MEDIUM];
4197 /* fool gcc to prevent a Y2K warning */
4198 char time_format[] = "_c";
4199 time_format[0] = '%';
4201 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4202 edit_print_string (edit, s);
4203 edit->force |= REDRAW_PAGE;
4204 break;
4206 break;
4207 case CK_Goto:
4208 edit_goto_cmd (edit);
4209 break;
4210 case CK_ParagraphFormat:
4211 format_paragraph (edit, 1);
4212 edit->force |= REDRAW_PAGE;
4213 break;
4214 case CK_MacroDelete:
4215 edit_delete_macro_cmd (edit);
4216 break;
4217 case CK_MatchBracket:
4218 edit_goto_matching_bracket (edit);
4219 break;
4220 case CK_UserMenu:
4221 user_menu (edit, NULL, -1);
4222 break;
4223 case CK_Sort:
4224 edit_sort_cmd (edit);
4225 break;
4226 case CK_ExternalCommand:
4227 edit_ext_cmd (edit);
4228 break;
4229 case CK_Mail:
4230 edit_mail_dialog (edit);
4231 break;
4232 #ifdef HAVE_CHARSET
4233 case CK_SelectCodepage:
4234 edit_select_codepage_cmd (edit);
4235 break;
4236 #endif
4237 case CK_InsertLiteral:
4238 edit_insert_literal_cmd (edit);
4239 break;
4240 case CK_MacroStartStopRecord:
4241 edit_begin_end_macro_cmd (edit);
4242 break;
4243 case CK_RepeatStartStopRecord:
4244 edit_begin_end_repeat_cmd (edit);
4245 break;
4246 case CK_ExtendedKeyMap:
4247 edit->extmod = TRUE;
4248 break;
4249 default:
4250 break;
4253 /* CK_PipeBlock */
4254 if ((command / CK_PipeBlock (0)) == 1)
4255 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4257 /* keys which must set the col position, and the search vars */
4258 switch (command)
4260 case CK_Search:
4261 case CK_SearchContinue:
4262 case CK_Replace:
4263 case CK_ReplaceContinue:
4264 case CK_Complete:
4265 edit->prev_col = edit_get_col (edit);
4266 break;
4267 case CK_Up:
4268 case CK_MarkUp:
4269 case CK_MarkColumnUp:
4270 case CK_Down:
4271 case CK_MarkDown:
4272 case CK_MarkColumnDown:
4273 case CK_PageUp:
4274 case CK_MarkPageUp:
4275 case CK_MarkColumnPageUp:
4276 case CK_PageDown:
4277 case CK_MarkPageDown:
4278 case CK_MarkColumnPageDown:
4279 case CK_Top:
4280 case CK_MarkToFileBegin:
4281 case CK_Bottom:
4282 case CK_MarkToFileEnd:
4283 case CK_ParagraphUp:
4284 case CK_MarkParagraphUp:
4285 case CK_MarkColumnParagraphUp:
4286 case CK_ParagraphDown:
4287 case CK_MarkParagraphDown:
4288 case CK_MarkColumnParagraphDown:
4289 case CK_ScrollUp:
4290 case CK_MarkScrollUp:
4291 case CK_MarkColumnScrollUp:
4292 case CK_ScrollDown:
4293 case CK_MarkScrollDown:
4294 case CK_MarkColumnScrollDown:
4295 edit->search_start = edit->curs1;
4296 edit->found_len = 0;
4297 break;
4298 default:
4299 edit->found_len = 0;
4300 edit->prev_col = edit_get_col (edit);
4301 edit->search_start = edit->curs1;
4303 edit_find_bracket (edit);
4305 if (option_auto_para_formatting)
4307 switch (command)
4309 case CK_BackSpace:
4310 case CK_Delete:
4311 case CK_DeleteToWordBegin:
4312 case CK_DeleteToWordEnd:
4313 case CK_DeleteToHome:
4314 case CK_DeleteToEnd:
4315 format_paragraph (edit, 0);
4316 edit->force |= REDRAW_PAGE;
4321 /* --------------------------------------------------------------------------------------------- */
4323 void
4324 edit_stack_init (void)
4326 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4328 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4329 edit_history_moveto[edit_stack_iterator].line = -1;
4332 edit_stack_iterator = 0;
4335 /* --------------------------------------------------------------------------------------------- */
4337 void
4338 edit_stack_free (void)
4340 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4341 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4344 /* --------------------------------------------------------------------------------------------- */
4345 /** move i lines */
4347 void
4348 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4350 edit_move_updown (edit, i, do_scroll, TRUE);
4353 /* --------------------------------------------------------------------------------------------- */
4354 /** move i lines */
4356 void
4357 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4359 edit_move_updown (edit, i, do_scroll, FALSE);
4362 /* --------------------------------------------------------------------------------------------- */