Code indentation.
[midnight-commander.git] / src / editor / edit.c
blob2d86e05101d5cd4abcfa6c6c1a9e47916bfecb69
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
73 #ifdef HAVE_ASPELL
74 #include "spell.h"
75 #endif
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
80 int option_typewriter_wrap = 0;
81 int option_auto_para_formatting = 0;
82 int option_fill_tabs_with_spaces = 0;
83 int option_return_does_auto_indent = 1;
84 int option_backspace_through_tabs = 0;
85 int option_fake_half_tabs = 1;
86 int option_save_mode = EDIT_QUICK_SAVE;
87 int option_save_position = 1;
88 int option_max_undo = 32768;
89 int option_persistent_selections = 1;
90 int option_cursor_beyond_eol = 0;
91 int option_line_state = 0;
92 int option_line_state_width = 0;
94 int option_edit_right_extreme = 0;
95 int option_edit_left_extreme = 0;
96 int option_edit_top_extreme = 0;
97 int option_edit_bottom_extreme = 0;
98 int enable_show_tabs_tws = 1;
99 int option_check_nl_at_eof = 0;
100 int option_group_undo = 0;
101 int show_right_margin = 0;
103 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
104 char *option_backup_ext = NULL;
106 int edit_stack_iterator = 0;
107 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
111 /*** file scope macro definitions ****************************************************************/
113 #define TEMP_BUF_LEN 1024
115 #define space_width 1
117 /*** file scope type declarations ****************************************************************/
119 /*** file scope variables ************************************************************************/
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
127 const char *read, *write, *extension;
128 } all_filters[] =
130 /* *INDENT-OFF* */
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
133 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 /* *INDENT-ON* */
139 static long last_bracket = -1;
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
146 * here's a quick sketch of the layout: (don't run this through indent.)
148 * (b1 is buffers1 and b2 is buffers2)
151 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
152 * ______________________________________|______________________________________
154 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
155 * |-> |-> |-> |-> |-> |-> |
157 * _<------------------------->|<----------------->_
158 * WEdit->curs2 | WEdit->curs1
159 * ^ | ^
160 * | ^|^ |
161 * cursor ||| cursor
162 * |||
163 * file end|||file beginning
168 * This_is_some_file
169 * fin.
172 /* --------------------------------------------------------------------------------------------- */
174 static int left_of_four_spaces (WEdit * edit);
176 /* --------------------------------------------------------------------------------------------- */
178 * Initialize the buffers for an empty files.
181 static void
182 edit_init_buffers (WEdit * edit)
184 int j;
186 for (j = 0; j <= MAXBUFF; j++)
188 edit->buffers1[j] = NULL;
189 edit->buffers2[j] = NULL;
192 edit->curs1 = 0;
193 edit->curs2 = 0;
194 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
197 /* --------------------------------------------------------------------------------------------- */
200 * Load file OR text into buffers. Set cursor to the beginning of file.
202 * @returns FALSE on error.
205 static gboolean
206 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
208 long buf, buf2;
209 int file = -1;
210 gboolean ret = FALSE;
212 edit->curs2 = edit->last_byte;
213 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
215 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
216 if (file == -1)
218 gchar *errmsg, *filename;
220 filename = vfs_path_to_str (filename_vpath);
221 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
222 g_free (filename);
223 edit_error_dialog (_("Error"), errmsg);
224 g_free (errmsg);
225 return FALSE;
228 if (!edit->buffers2[buf2])
229 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
233 if (mc_read (file,
234 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
235 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
236 break;
238 for (buf = buf2 - 1; buf >= 0; buf--)
240 /* edit->buffers2[0] is already allocated */
241 if (!edit->buffers2[buf])
242 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
243 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
244 break;
246 ret = TRUE;
248 while (FALSE);
250 if (!ret)
252 gchar *errmsg, *filename;
254 filename = vfs_path_to_str (filename_vpath);
255 errmsg = g_strdup_printf (_("Error reading %s"), filename);
256 g_free (filename);
257 edit_error_dialog (_("Error"), errmsg);
258 g_free (errmsg);
260 mc_close (file);
261 return ret;
264 /* --------------------------------------------------------------------------------------------- */
265 /** Return index of the filter or -1 is there is no appropriate filter */
267 static int
268 edit_find_filter (const vfs_path_t * filename_vpath)
270 size_t i, l, e;
271 char *filename;
273 if (filename_vpath == NULL)
274 return -1;
276 filename = vfs_path_to_str (filename_vpath);
277 l = strlen (filename);
278 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
280 e = strlen (all_filters[i].extension);
281 if (l > e)
282 if (!strcmp (all_filters[i].extension, filename + l - e))
284 g_free (filename);
285 return i;
288 g_free (filename);
289 return -1;
292 /* --------------------------------------------------------------------------------------------- */
294 static char *
295 edit_get_filter (const vfs_path_t * filename_vpath)
297 int i;
298 char *p, *quoted_name, *filename;
300 i = edit_find_filter (filename_vpath);
301 if (i < 0)
302 return NULL;
304 filename = vfs_path_to_str (filename_vpath);
305 quoted_name = name_quote (filename, 0);
306 g_free (filename);
307 p = g_strdup_printf (all_filters[i].read, quoted_name);
308 g_free (quoted_name);
309 return p;
312 /* --------------------------------------------------------------------------------------------- */
314 static long
315 edit_insert_stream (WEdit * edit, FILE * f)
317 int c;
318 long i = 0;
319 while ((c = fgetc (f)) >= 0)
321 edit_insert (edit, c);
322 i++;
324 return i;
327 /* --------------------------------------------------------------------------------------------- */
329 * Open file and create it if necessary.
331 * @param edit editor object
332 * @param filename_vpath file name
333 * @param st buffer for store stat info
334 * @returns TRUE for success, FALSE for error.
337 static gboolean
338 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
340 int file;
341 gchar *errmsg = NULL;
343 /* Try opening an existing file */
344 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
345 if (file < 0)
348 * Try creating the file. O_EXCL prevents following broken links
349 * and opening existing files.
351 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
352 if (file < 0)
354 char *filename;
356 filename = vfs_path_to_str (filename_vpath);
357 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
358 g_free (filename);
359 goto cleanup;
362 /* New file, delete it if it's not modified or saved */
363 edit->delete_file = 1;
366 /* Check what we have opened */
367 if (mc_fstat (file, st) < 0)
369 char *filename;
371 filename = vfs_path_to_str (filename_vpath);
372 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
373 g_free (filename);
374 goto cleanup;
377 /* We want to open regular files only */
378 if (!S_ISREG (st->st_mode))
380 char *filename;
382 filename = vfs_path_to_str (filename_vpath);
383 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
384 g_free (filename);
385 goto cleanup;
389 * Don't delete non-empty files.
390 * O_EXCL should prevent it, but let's be on the safe side.
392 if (st->st_size > 0)
393 edit->delete_file = 0;
395 if (st->st_size >= SIZE_LIMIT)
397 char *filename;
399 filename = vfs_path_to_str (filename_vpath);
400 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
401 g_free (filename);
404 cleanup:
405 (void) mc_close (file);
407 if (errmsg != NULL)
409 edit_error_dialog (_("Error"), errmsg);
410 g_free (errmsg);
411 return FALSE;
413 return TRUE;
416 /* --------------------------------------------------------------------------------------------- */
419 * Open the file and load it into the buffers, either directly or using
420 * a filter. Return TRUE on success, FALSE on error.
422 * Fast loading (edit_load_file_fast) is used when the file size is
423 * known. In this case the data is read into the buffers by blocks.
424 * If the file size is not known, the data is loaded byte by byte in
425 * edit_insert_file.
427 * @param edit editor object
428 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
430 static gboolean
431 edit_load_file (WEdit * edit)
433 gboolean fast_load = TRUE;
435 /* Cannot do fast load if a filter is used */
436 if (edit_find_filter (edit->filename_vpath) >= 0)
437 fast_load = FALSE;
440 * FIXME: line end translation should disable fast loading as well
441 * Consider doing fseek() to the end and ftell() for the real size.
443 if (edit->filename_vpath != NULL)
446 * VFS may report file size incorrectly, and slow load is not a big
447 * deal considering overhead in VFS.
449 if (!vfs_file_is_local (edit->filename_vpath))
450 fast_load = FALSE;
452 /* If we are dealing with a real file, check that it exists */
453 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
455 edit_clean (edit);
456 return FALSE;
459 else
461 /* nothing to load */
462 fast_load = FALSE;
465 edit_init_buffers (edit);
467 if (fast_load)
469 edit->last_byte = edit->stat1.st_size;
470 edit_load_file_fast (edit, edit->filename_vpath);
471 /* If fast load was used, the number of lines wasn't calculated */
472 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
474 else
476 edit->last_byte = 0;
477 if (edit->filename_vpath != NULL
478 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
480 edit->undo_stack_disable = 1;
481 if (edit_insert_file (edit, edit->filename_vpath) < 0)
483 edit_clean (edit);
484 return FALSE;
486 edit->undo_stack_disable = 0;
489 edit->lb = LB_ASIS;
490 return TRUE;
493 /* --------------------------------------------------------------------------------------------- */
494 /** Restore saved cursor position in the file */
496 static void
497 edit_load_position (WEdit * edit)
499 long line, column;
500 off_t offset;
502 if (edit->filename_vpath == NULL
503 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
504 return;
506 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
508 if (line > 0)
510 edit_move_to_line (edit, line - 1);
511 edit->prev_col = column;
513 else if (offset > 0)
515 edit_cursor_move (edit, offset);
516 line = edit->curs_line;
517 edit->search_start = edit->curs1;
520 book_mark_restore (edit, BOOK_MARK_COLOR);
522 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
523 edit_move_display (edit, line - (edit->widget.lines / 2));
526 /* --------------------------------------------------------------------------------------------- */
527 /** Save cursor position in the file */
529 static void
530 edit_save_position (WEdit * edit)
532 if (edit->filename_vpath == NULL
533 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
534 return;
536 book_mark_serialize (edit, BOOK_MARK_COLOR);
537 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
538 edit->serialized_bookmarks);
539 edit->serialized_bookmarks = NULL;
542 /* --------------------------------------------------------------------------------------------- */
543 /** Clean the WEdit stricture except the widget part */
545 static void
546 edit_purge_widget (WEdit * edit)
548 size_t len = sizeof (WEdit) - sizeof (Widget);
549 char *start = (char *) edit + sizeof (Widget);
550 memset (start, 0, len);
553 /* --------------------------------------------------------------------------------------------- */
556 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
557 then the file should be as it was when he loaded up. Then set edit->modified to 0.
560 static long
561 edit_pop_undo_action (WEdit * edit)
563 long c;
564 unsigned long sp = edit->undo_stack_pointer;
566 if (sp == edit->undo_stack_bottom)
567 return STACK_BOTTOM;
569 sp = (sp - 1) & edit->undo_stack_size_mask;
570 c = edit->undo_stack[sp];
571 if (c >= 0)
573 /* edit->undo_stack[sp] = '@'; */
574 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
575 return c;
578 if (sp == edit->undo_stack_bottom)
579 return STACK_BOTTOM;
581 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
582 if (edit->undo_stack[sp] == -2)
584 /* edit->undo_stack[sp] = '@'; */
585 edit->undo_stack_pointer = sp;
587 else
588 edit->undo_stack[sp]++;
590 return c;
593 static long
594 edit_pop_redo_action (WEdit * edit)
596 long c;
597 unsigned long sp = edit->redo_stack_pointer;
599 if (sp == edit->redo_stack_bottom)
600 return STACK_BOTTOM;
602 sp = (sp - 1) & edit->redo_stack_size_mask;
603 c = edit->redo_stack[sp];
604 if (c >= 0)
606 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
607 return c;
610 if (sp == edit->redo_stack_bottom)
611 return STACK_BOTTOM;
613 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
614 if (edit->redo_stack[sp] == -2)
615 edit->redo_stack_pointer = sp;
616 else
617 edit->redo_stack[sp]++;
619 return c;
622 static long
623 get_prev_undo_action (WEdit * edit)
625 long c;
626 unsigned long sp = edit->undo_stack_pointer;
628 if (sp == edit->undo_stack_bottom)
629 return STACK_BOTTOM;
631 sp = (sp - 1) & edit->undo_stack_size_mask;
632 c = edit->undo_stack[sp];
633 if (c >= 0)
634 return c;
636 if (sp == edit->undo_stack_bottom)
637 return STACK_BOTTOM;
639 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
640 return c;
643 /* --------------------------------------------------------------------------------------------- */
644 /** is called whenever a modification is made by one of the four routines below */
646 static void
647 edit_modification (WEdit * edit)
649 edit->caches_valid = 0;
651 /* raise lock when file modified */
652 if (!edit->modified && !edit->delete_file)
653 edit->locked = lock_file (edit->filename_vpath);
654 edit->modified = 1;
657 /* --------------------------------------------------------------------------------------------- */
659 static char *
660 edit_get_byte_ptr (WEdit * edit, long byte_index)
662 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
663 return NULL;
665 if (byte_index >= edit->curs1)
667 unsigned long p;
669 p = edit->curs1 + edit->curs2 - byte_index - 1;
670 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
671 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
674 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
675 (byte_index & M_EDIT_BUF_SIZE));
678 /* --------------------------------------------------------------------------------------------- */
680 static int
681 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
683 int i, res;
684 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
685 gchar *str;
686 gchar *cursor_buf_ptr;
688 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
690 *char_width = 0;
691 return 0;
694 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
695 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
696 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
698 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
699 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
701 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
703 *char_width = 1;
704 return *(cursor_buf_ptr - 1);
706 else
708 res = g_utf8_get_char_validated (str, -1);
710 if (res < 0)
712 *char_width = 1;
713 return *(cursor_buf_ptr - 1);
715 else
717 *char_width = cursor_buf_ptr - str;
718 return res;
723 /* --------------------------------------------------------------------------------------------- */
724 /* high level cursor movement commands */
725 /* --------------------------------------------------------------------------------------------- */
727 static int
728 is_in_indent (WEdit * edit)
730 long p = edit_bol (edit, edit->curs1);
731 while (p < edit->curs1)
732 if (!strchr (" \t", edit_get_byte (edit, p++)))
733 return 0;
734 return 1;
737 /* --------------------------------------------------------------------------------------------- */
739 static int
740 is_blank (WEdit * edit, long offset)
742 long s, f;
743 int c;
744 s = edit_bol (edit, offset);
745 f = edit_eol (edit, offset) - 1;
746 while (s <= f)
748 c = edit_get_byte (edit, s++);
749 if (!isspace (c))
750 return 0;
752 return 1;
756 /* --------------------------------------------------------------------------------------------- */
757 /** returns the offset of line i */
759 static long
760 edit_find_line (WEdit * edit, int line)
762 int i, j = 0;
763 int m = 2000000000;
764 if (!edit->caches_valid)
766 for (i = 0; i < N_LINE_CACHES; i++)
767 edit->line_numbers[i] = edit->line_offsets[i] = 0;
768 /* three offsets that we *know* are line 0 at 0 and these two: */
769 edit->line_numbers[1] = edit->curs_line;
770 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
771 edit->line_numbers[2] = edit->total_lines;
772 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
773 edit->caches_valid = 1;
775 if (line >= edit->total_lines)
776 return edit->line_offsets[2];
777 if (line <= 0)
778 return 0;
779 /* find the closest known point */
780 for (i = 0; i < N_LINE_CACHES; i++)
782 int n;
783 n = abs (edit->line_numbers[i] - line);
784 if (n < m)
786 m = n;
787 j = i;
790 if (m == 0)
791 return edit->line_offsets[j]; /* know the offset exactly */
792 if (m == 1 && j >= 3)
793 i = j; /* one line different - caller might be looping, so stay in this cache */
794 else
795 i = 3 + (rand () % (N_LINE_CACHES - 3));
796 if (line > edit->line_numbers[j])
797 edit->line_offsets[i] =
798 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
799 else
800 edit->line_offsets[i] =
801 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
802 edit->line_numbers[i] = line;
803 return edit->line_offsets[i];
806 /* --------------------------------------------------------------------------------------------- */
807 /** moves up until a blank line is reached, or until just
808 before a non-blank line is reached */
810 static void
811 edit_move_up_paragraph (WEdit * edit, int do_scroll)
813 int i = 0;
814 if (edit->curs_line > 1)
816 if (line_is_blank (edit, edit->curs_line))
818 if (line_is_blank (edit, edit->curs_line - 1))
820 for (i = edit->curs_line - 1; i; i--)
821 if (!line_is_blank (edit, i))
823 i++;
824 break;
827 else
829 for (i = edit->curs_line - 1; i; i--)
830 if (line_is_blank (edit, i))
831 break;
834 else
836 for (i = edit->curs_line - 1; i; i--)
837 if (line_is_blank (edit, i))
838 break;
841 edit_move_up (edit, edit->curs_line - i, do_scroll);
844 /* --------------------------------------------------------------------------------------------- */
845 /** moves down until a blank line is reached, or until just
846 before a non-blank line is reached */
848 static void
849 edit_move_down_paragraph (WEdit * edit, int do_scroll)
851 int i;
852 if (edit->curs_line >= edit->total_lines - 1)
854 i = edit->total_lines;
856 else
858 if (line_is_blank (edit, edit->curs_line))
860 if (line_is_blank (edit, edit->curs_line + 1))
862 for (i = edit->curs_line + 1; i; i++)
863 if (!line_is_blank (edit, i) || i > edit->total_lines)
865 i--;
866 break;
869 else
871 for (i = edit->curs_line + 1; i; i++)
872 if (line_is_blank (edit, i) || i >= edit->total_lines)
873 break;
876 else
878 for (i = edit->curs_line + 1; i; i++)
879 if (line_is_blank (edit, i) || i >= edit->total_lines)
880 break;
883 edit_move_down (edit, i - edit->curs_line, do_scroll);
886 /* --------------------------------------------------------------------------------------------- */
888 static void
889 edit_begin_page (WEdit * edit)
891 edit_update_curs_row (edit);
892 edit_move_up (edit, edit->curs_row, 0);
895 /* --------------------------------------------------------------------------------------------- */
897 static void
898 edit_end_page (WEdit * edit)
900 edit_update_curs_row (edit);
901 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
905 /* --------------------------------------------------------------------------------------------- */
906 /** goto beginning of text */
908 static void
909 edit_move_to_top (WEdit * edit)
911 if (edit->curs_line)
913 edit_cursor_move (edit, -edit->curs1);
914 edit_move_to_prev_col (edit, 0);
915 edit->force |= REDRAW_PAGE;
916 edit->search_start = 0;
917 edit_update_curs_row (edit);
922 /* --------------------------------------------------------------------------------------------- */
923 /** goto end of text */
925 static void
926 edit_move_to_bottom (WEdit * edit)
928 if (edit->curs_line < edit->total_lines)
930 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
931 edit->start_display = edit->last_byte;
932 edit->start_line = edit->total_lines;
933 edit_scroll_upward (edit, edit->widget.lines - 1);
934 edit->force |= REDRAW_PAGE;
938 /* --------------------------------------------------------------------------------------------- */
939 /** goto beginning of line */
941 static void
942 edit_cursor_to_bol (WEdit * edit)
944 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
945 edit->search_start = edit->curs1;
946 edit->prev_col = edit_get_col (edit);
947 edit->over_col = 0;
950 /* --------------------------------------------------------------------------------------------- */
951 /** goto end of line */
953 static void
954 edit_cursor_to_eol (WEdit * edit)
956 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
957 edit->search_start = edit->curs1;
958 edit->prev_col = edit_get_col (edit);
959 edit->over_col = 0;
962 /* --------------------------------------------------------------------------------------------- */
964 static unsigned long
965 my_type_of (int c)
967 int x, r = 0;
968 const char *p, *q;
969 const char option_chars_move_whole_word[] =
970 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
972 if (!c)
973 return 0;
974 if (c == '!')
976 if (*option_chars_move_whole_word == '!')
977 return 2;
978 return 0x80000000UL;
980 if (g_ascii_isupper ((gchar) c))
981 c = 'A';
982 else if (g_ascii_islower ((gchar) c))
983 c = 'a';
984 else if (g_ascii_isalpha (c))
985 c = 'a';
986 else if (isdigit (c))
987 c = '0';
988 else if (isspace (c))
989 c = ' ';
990 q = strchr (option_chars_move_whole_word, c);
991 if (!q)
992 return 0xFFFFFFFFUL;
995 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
996 if (*p == '!')
997 x <<= 1;
998 r |= x;
1000 while ((q = strchr (q + 1, c)));
1001 return r;
1004 /* --------------------------------------------------------------------------------------------- */
1006 static void
1007 edit_left_word_move (WEdit * edit, int s)
1009 for (;;)
1011 int c1, c2;
1012 if (edit->column_highlight
1013 && edit->mark1 != edit->mark2
1014 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1015 break;
1016 edit_cursor_move (edit, -1);
1017 if (!edit->curs1)
1018 break;
1019 c1 = edit_get_byte (edit, edit->curs1 - 1);
1020 c2 = edit_get_byte (edit, edit->curs1);
1021 if (c1 == '\n' || c2 == '\n')
1022 break;
1023 if (!(my_type_of (c1) & my_type_of (c2)))
1024 break;
1025 if (isspace (c1) && !isspace (c2))
1026 break;
1027 if (s)
1028 if (!isspace (c1) && isspace (c2))
1029 break;
1033 /* --------------------------------------------------------------------------------------------- */
1035 static void
1036 edit_left_word_move_cmd (WEdit * edit)
1038 edit_left_word_move (edit, 0);
1039 edit->force |= REDRAW_PAGE;
1042 /* --------------------------------------------------------------------------------------------- */
1044 static void
1045 edit_right_word_move (WEdit * edit, int s)
1047 for (;;)
1049 int c1, c2;
1050 if (edit->column_highlight
1051 && edit->mark1 != edit->mark2
1052 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1053 break;
1054 edit_cursor_move (edit, 1);
1055 if (edit->curs1 >= edit->last_byte)
1056 break;
1057 c1 = edit_get_byte (edit, edit->curs1 - 1);
1058 c2 = edit_get_byte (edit, edit->curs1);
1059 if (c1 == '\n' || c2 == '\n')
1060 break;
1061 if (!(my_type_of (c1) & my_type_of (c2)))
1062 break;
1063 if (isspace (c1) && !isspace (c2))
1064 break;
1065 if (s)
1066 if (!isspace (c1) && isspace (c2))
1067 break;
1071 /* --------------------------------------------------------------------------------------------- */
1073 static void
1074 edit_right_word_move_cmd (WEdit * edit)
1076 edit_right_word_move (edit, 0);
1077 edit->force |= REDRAW_PAGE;
1080 /* --------------------------------------------------------------------------------------------- */
1082 static void
1083 edit_right_char_move_cmd (WEdit * edit)
1085 int cw = 1;
1086 int c = 0;
1087 if (edit->utf8)
1089 c = edit_get_utf (edit, edit->curs1, &cw);
1090 if (cw < 1)
1091 cw = 1;
1093 else
1095 c = edit_get_byte (edit, edit->curs1);
1097 if (option_cursor_beyond_eol && c == '\n')
1099 edit->over_col++;
1101 else
1103 edit_cursor_move (edit, cw);
1107 /* --------------------------------------------------------------------------------------------- */
1109 static void
1110 edit_left_char_move_cmd (WEdit * edit)
1112 int cw = 1;
1113 if (edit->column_highlight
1114 && option_cursor_beyond_eol
1115 && edit->mark1 != edit->mark2
1116 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1117 return;
1118 if (edit->utf8)
1120 edit_get_prev_utf (edit, edit->curs1, &cw);
1121 if (cw < 1)
1122 cw = 1;
1124 if (option_cursor_beyond_eol && edit->over_col > 0)
1126 edit->over_col--;
1128 else
1130 edit_cursor_move (edit, -cw);
1134 /* --------------------------------------------------------------------------------------------- */
1135 /** Up or down cursor moving.
1136 direction = TRUE - move up
1137 = FALSE - move down
1140 static void
1141 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1143 unsigned long p;
1144 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1146 if (i > l)
1147 i = l;
1149 if (i == 0)
1150 return;
1152 if (i > 1)
1153 edit->force |= REDRAW_PAGE;
1154 if (do_scroll)
1156 if (direction)
1157 edit_scroll_upward (edit, i);
1158 else
1159 edit_scroll_downward (edit, i);
1161 p = edit_bol (edit, edit->curs1);
1163 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1165 edit_cursor_move (edit, p - edit->curs1);
1167 edit_move_to_prev_col (edit, p);
1169 /* search start of current multibyte char (like CJK) */
1170 if (edit->curs1 + 1 < edit->last_byte)
1172 edit_right_char_move_cmd (edit);
1173 edit_left_char_move_cmd (edit);
1176 edit->search_start = edit->curs1;
1177 edit->found_len = 0;
1180 /* --------------------------------------------------------------------------------------------- */
1182 static void
1183 edit_right_delete_word (WEdit * edit)
1185 int c1, c2;
1186 for (;;)
1188 if (edit->curs1 >= edit->last_byte)
1189 break;
1190 c1 = edit_delete (edit, 1);
1191 c2 = edit_get_byte (edit, edit->curs1);
1192 if (c1 == '\n' || c2 == '\n')
1193 break;
1194 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1195 break;
1196 if (!(my_type_of (c1) & my_type_of (c2)))
1197 break;
1201 /* --------------------------------------------------------------------------------------------- */
1203 static void
1204 edit_left_delete_word (WEdit * edit)
1206 int c1, c2;
1207 for (;;)
1209 if (edit->curs1 <= 0)
1210 break;
1211 c1 = edit_backspace (edit, 1);
1212 c2 = edit_get_byte (edit, edit->curs1 - 1);
1213 if (c1 == '\n' || c2 == '\n')
1214 break;
1215 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1216 break;
1217 if (!(my_type_of (c1) & my_type_of (c2)))
1218 break;
1222 /* --------------------------------------------------------------------------------------------- */
1224 the start column position is not recorded, and hence does not
1225 undo as it happed. But who would notice.
1228 static void
1229 edit_do_undo (WEdit * edit)
1231 long ac;
1232 long count = 0;
1234 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1235 edit->over_col = 0;
1236 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1238 switch ((int) ac)
1240 case STACK_BOTTOM:
1241 goto done_undo;
1242 case CURS_RIGHT:
1243 edit_cursor_move (edit, 1);
1244 break;
1245 case CURS_LEFT:
1246 edit_cursor_move (edit, -1);
1247 break;
1248 case BACKSPACE:
1249 case BACKSPACE_BR:
1250 edit_backspace (edit, 1);
1251 break;
1252 case DELCHAR:
1253 case DELCHAR_BR:
1254 edit_delete (edit, 1);
1255 break;
1256 case COLUMN_ON:
1257 edit->column_highlight = 1;
1258 break;
1259 case COLUMN_OFF:
1260 edit->column_highlight = 0;
1261 break;
1263 if (ac >= 256 && ac < 512)
1264 edit_insert_ahead (edit, ac - 256);
1265 if (ac >= 0 && ac < 256)
1266 edit_insert (edit, ac);
1268 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1270 edit->mark1 = ac - MARK_1;
1271 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1273 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1275 edit->mark2 = ac - MARK_2;
1276 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1278 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1280 edit->end_mark_curs = ac - MARK_CURS;
1282 if (count++)
1283 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1286 if (edit->start_display > ac - KEY_PRESS)
1288 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1289 edit->force |= REDRAW_PAGE;
1291 else if (edit->start_display < ac - KEY_PRESS)
1293 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1294 edit->force |= REDRAW_PAGE;
1296 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1297 edit_update_curs_row (edit);
1299 done_undo:;
1300 edit->undo_stack_disable = 0;
1303 static void
1304 edit_do_redo (WEdit * edit)
1306 long ac;
1307 long count = 0;
1309 if (edit->redo_stack_reset)
1310 return;
1312 edit->over_col = 0;
1313 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1315 switch ((int) ac)
1317 case STACK_BOTTOM:
1318 goto done_redo;
1319 case CURS_RIGHT:
1320 edit_cursor_move (edit, 1);
1321 break;
1322 case CURS_LEFT:
1323 edit_cursor_move (edit, -1);
1324 break;
1325 case BACKSPACE:
1326 edit_backspace (edit, 1);
1327 break;
1328 case DELCHAR:
1329 edit_delete (edit, 1);
1330 break;
1331 case COLUMN_ON:
1332 edit->column_highlight = 1;
1333 break;
1334 case COLUMN_OFF:
1335 edit->column_highlight = 0;
1336 break;
1338 if (ac >= 256 && ac < 512)
1339 edit_insert_ahead (edit, ac - 256);
1340 if (ac >= 0 && ac < 256)
1341 edit_insert (edit, ac);
1343 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1345 edit->mark1 = ac - MARK_1;
1346 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1348 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1350 edit->mark2 = ac - MARK_2;
1351 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1353 /* more than one pop usually means something big */
1354 if (count++)
1355 edit->force |= REDRAW_PAGE;
1358 if (edit->start_display > ac - KEY_PRESS)
1360 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1361 edit->force |= REDRAW_PAGE;
1363 else if (edit->start_display < ac - KEY_PRESS)
1365 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1366 edit->force |= REDRAW_PAGE;
1368 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1369 edit_update_curs_row (edit);
1371 done_redo:;
1374 static void
1375 edit_group_undo (WEdit * edit)
1377 long ac = KEY_PRESS;
1378 long cur_ac = KEY_PRESS;
1379 while (ac != STACK_BOTTOM && ac == cur_ac)
1381 cur_ac = get_prev_undo_action (edit);
1382 edit_do_undo (edit);
1383 ac = get_prev_undo_action (edit);
1384 /* exit from cycle if option_group_undo is not set,
1385 * and make single UNDO operation
1387 if (!option_group_undo)
1388 ac = STACK_BOTTOM;
1392 /* --------------------------------------------------------------------------------------------- */
1394 static void
1395 edit_delete_to_line_end (WEdit * edit)
1397 while (edit_get_byte (edit, edit->curs1) != '\n')
1399 if (!edit->curs2)
1400 break;
1401 edit_delete (edit, 1);
1405 /* --------------------------------------------------------------------------------------------- */
1407 static void
1408 edit_delete_to_line_begin (WEdit * edit)
1410 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1412 if (!edit->curs1)
1413 break;
1414 edit_backspace (edit, 1);
1418 /* --------------------------------------------------------------------------------------------- */
1420 static int
1421 is_aligned_on_a_tab (WEdit * edit)
1423 edit_update_curs_col (edit);
1424 return !((edit->curs_col % (TAB_SIZE * space_width))
1425 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1428 /* --------------------------------------------------------------------------------------------- */
1430 static int
1431 right_of_four_spaces (WEdit * edit)
1433 int i, ch = 0;
1434 for (i = 1; i <= HALF_TAB_SIZE; i++)
1435 ch |= edit_get_byte (edit, edit->curs1 - i);
1436 if (ch == ' ')
1437 return is_aligned_on_a_tab (edit);
1438 return 0;
1441 /* --------------------------------------------------------------------------------------------- */
1443 static int
1444 left_of_four_spaces (WEdit * edit)
1446 int i, ch = 0;
1447 for (i = 0; i < HALF_TAB_SIZE; i++)
1448 ch |= edit_get_byte (edit, edit->curs1 + i);
1449 if (ch == ' ')
1450 return is_aligned_on_a_tab (edit);
1451 return 0;
1454 /* --------------------------------------------------------------------------------------------- */
1456 static void
1457 edit_auto_indent (WEdit * edit)
1459 long p;
1460 char c;
1461 p = edit->curs1;
1462 /* use the previous line as a template */
1463 p = edit_move_backward (edit, p, 1);
1464 /* copy the leading whitespace of the line */
1465 for (;;)
1466 { /* no range check - the line _is_ \n-terminated */
1467 c = edit_get_byte (edit, p++);
1468 if (c != ' ' && c != '\t')
1469 break;
1470 edit_insert (edit, c);
1474 /* --------------------------------------------------------------------------------------------- */
1476 static inline void
1477 edit_double_newline (WEdit * edit)
1479 edit_insert (edit, '\n');
1480 if (edit_get_byte (edit, edit->curs1) == '\n')
1481 return;
1482 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1483 return;
1484 edit->force |= REDRAW_PAGE;
1485 edit_insert (edit, '\n');
1489 /* --------------------------------------------------------------------------------------------- */
1491 static void
1492 insert_spaces_tab (WEdit * edit, gboolean half)
1494 int i;
1496 edit_update_curs_col (edit);
1497 i = option_tab_spacing * space_width;
1498 if (half)
1499 i /= 2;
1500 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1501 while (i > 0)
1503 edit_insert (edit, ' ');
1504 i -= space_width;
1508 /* --------------------------------------------------------------------------------------------- */
1510 static inline void
1511 edit_tab_cmd (WEdit * edit)
1513 int i;
1515 if (option_fake_half_tabs)
1517 if (is_in_indent (edit))
1519 /*insert a half tab (usually four spaces) unless there is a
1520 half tab already behind, then delete it and insert a
1521 full tab. */
1522 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1523 insert_spaces_tab (edit, TRUE);
1524 else
1526 for (i = 1; i <= HALF_TAB_SIZE; i++)
1527 edit_backspace (edit, 1);
1528 edit_insert (edit, '\t');
1530 return;
1533 if (option_fill_tabs_with_spaces)
1534 insert_spaces_tab (edit, FALSE);
1535 else
1536 edit_insert (edit, '\t');
1539 /* --------------------------------------------------------------------------------------------- */
1541 static void
1542 check_and_wrap_line (WEdit * edit)
1544 int curs, c;
1545 if (!option_typewriter_wrap)
1546 return;
1547 edit_update_curs_col (edit);
1548 if (edit->curs_col < option_word_wrap_line_length)
1549 return;
1550 curs = edit->curs1;
1551 for (;;)
1553 curs--;
1554 c = edit_get_byte (edit, curs);
1555 if (c == '\n' || curs <= 0)
1557 edit_insert (edit, '\n');
1558 return;
1560 if (c == ' ' || c == '\t')
1562 int current = edit->curs1;
1563 edit_cursor_move (edit, curs - edit->curs1 + 1);
1564 edit_insert (edit, '\n');
1565 edit_cursor_move (edit, current - edit->curs1 + 1);
1566 return;
1571 /* --------------------------------------------------------------------------------------------- */
1572 /** this find the matching bracket in either direction, and sets edit->bracket */
1574 static long
1575 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1577 const char *const b = "{}{[][()(", *p;
1578 int i = 1, a, inc = -1, c, d, n = 0;
1579 unsigned long j = 0;
1580 long q;
1581 edit_update_curs_row (edit);
1582 c = edit_get_byte (edit, edit->curs1);
1583 p = strchr (b, c);
1584 /* no limit */
1585 if (!furthest_bracket_search)
1586 furthest_bracket_search--;
1587 /* not on a bracket at all */
1588 if (!p)
1589 return -1;
1590 /* the matching bracket */
1591 d = p[1];
1592 /* going left or right? */
1593 if (strchr ("{[(", c))
1594 inc = 1;
1595 for (q = edit->curs1 + inc;; q += inc)
1597 /* out of buffer? */
1598 if (q >= edit->last_byte || q < 0)
1599 break;
1600 a = edit_get_byte (edit, q);
1601 /* don't want to eat CPU */
1602 if (j++ > furthest_bracket_search)
1603 break;
1604 /* out of screen? */
1605 if (in_screen)
1607 if (q < edit->start_display)
1608 break;
1609 /* count lines if searching downward */
1610 if (inc > 0 && a == '\n')
1611 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1612 break;
1614 /* count bracket depth */
1615 i += (a == c) - (a == d);
1616 /* return if bracket depth is zero */
1617 if (!i)
1618 return q;
1620 /* no match */
1621 return -1;
1624 /* --------------------------------------------------------------------------------------------- */
1626 static inline void
1627 edit_goto_matching_bracket (WEdit * edit)
1629 long q;
1631 q = edit_get_bracket (edit, 0, 0);
1632 if (q >= 0)
1634 edit->bracket = edit->curs1;
1635 edit->force |= REDRAW_PAGE;
1636 edit_cursor_move (edit, q - edit->curs1);
1640 /* --------------------------------------------------------------------------------------------- */
1642 static void
1643 edit_move_block_to_right (WEdit * edit)
1645 long start_mark, end_mark;
1646 long cur_bol, start_bol;
1648 if (eval_marks (edit, &start_mark, &end_mark))
1649 return;
1651 start_bol = edit_bol (edit, start_mark);
1652 cur_bol = edit_bol (edit, end_mark - 1);
1656 edit_cursor_move (edit, cur_bol - edit->curs1);
1657 if (option_fill_tabs_with_spaces)
1658 insert_spaces_tab (edit, option_fake_half_tabs);
1659 else
1660 edit_insert (edit, '\t');
1661 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1663 if (cur_bol == 0)
1664 break;
1666 cur_bol = edit_bol (edit, cur_bol - 1);
1668 while (cur_bol >= start_bol);
1670 edit->force |= REDRAW_PAGE;
1673 /* --------------------------------------------------------------------------------------------- */
1675 static void
1676 edit_move_block_to_left (WEdit * edit)
1678 long start_mark, end_mark;
1679 long cur_bol, start_bol;
1680 int i;
1682 if (eval_marks (edit, &start_mark, &end_mark))
1683 return;
1685 start_bol = edit_bol (edit, start_mark);
1686 cur_bol = edit_bol (edit, end_mark - 1);
1690 int del_tab_width;
1691 int next_char;
1693 edit_cursor_move (edit, cur_bol - edit->curs1);
1695 if (option_fake_half_tabs)
1696 del_tab_width = HALF_TAB_SIZE;
1697 else
1698 del_tab_width = option_tab_spacing;
1700 next_char = edit_get_byte (edit, edit->curs1);
1701 if (next_char == '\t')
1702 edit_delete (edit, 1);
1703 else if (next_char == ' ')
1704 for (i = 1; i <= del_tab_width; i++)
1706 if (next_char == ' ')
1707 edit_delete (edit, 1);
1708 next_char = edit_get_byte (edit, edit->curs1);
1711 if (cur_bol == 0)
1712 break;
1714 cur_bol = edit_bol (edit, cur_bol - 1);
1716 while (cur_bol >= start_bol);
1718 edit->force |= REDRAW_PAGE;
1721 /* --------------------------------------------------------------------------------------------- */
1723 * prints at the cursor
1724 * @returns the number of chars printed
1727 static size_t
1728 edit_print_string (WEdit * e, const char *s)
1730 size_t i = 0;
1732 while (s[i] != '\0')
1733 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1734 e->force |= REDRAW_COMPLETELY;
1735 edit_update_screen (e);
1736 return i;
1739 /* --------------------------------------------------------------------------------------------- */
1740 /*** public functions ****************************************************************************/
1741 /* --------------------------------------------------------------------------------------------- */
1743 /** User edit menu, like user menu (F2) but only in editor. */
1745 void
1746 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1748 char *block_file;
1749 int nomark;
1750 long curs;
1751 long start_mark, end_mark;
1752 struct stat status;
1753 vfs_path_t *block_file_vpath;
1755 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1756 block_file_vpath = vfs_path_from_str (block_file);
1757 curs = edit->curs1;
1758 nomark = eval_marks (edit, &start_mark, &end_mark);
1759 if (nomark == 0)
1760 edit_save_block (edit, block_file, start_mark, end_mark);
1762 /* run shell scripts from menu */
1763 if (user_menu_cmd (edit, menu_file, selected_entry)
1764 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1766 int rc = 0;
1767 FILE *fd;
1769 /* i.e. we have marked block */
1770 if (nomark == 0)
1771 rc = edit_block_delete_cmd (edit);
1773 if (rc == 0)
1775 long ins_len;
1777 ins_len = edit_insert_file (edit, block_file_vpath);
1778 if (nomark == 0 && ins_len > 0)
1779 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1781 /* truncate block file */
1782 fd = fopen (block_file, "w");
1783 if (fd != NULL)
1784 fclose (fd);
1786 g_free (block_file);
1787 vfs_path_free (block_file_vpath);
1789 edit_cursor_move (edit, curs - edit->curs1);
1790 edit->force |= REDRAW_PAGE;
1791 send_message ((Widget *) edit, WIDGET_DRAW, 0);
1794 /* --------------------------------------------------------------------------------------------- */
1797 edit_get_byte (WEdit * edit, long byte_index)
1799 unsigned long p;
1800 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1801 return '\n';
1803 if (byte_index >= edit->curs1)
1805 p = edit->curs1 + edit->curs2 - byte_index - 1;
1806 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1808 else
1810 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1814 /* --------------------------------------------------------------------------------------------- */
1817 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1819 gchar *str = NULL;
1820 int res = -1;
1821 gunichar ch;
1822 gchar *next_ch = NULL;
1823 int width = 0;
1824 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1826 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1828 *char_width = 0;
1829 return '\n';
1832 str = edit_get_byte_ptr (edit, byte_index);
1834 if (str == NULL)
1836 *char_width = 0;
1837 return 0;
1840 res = g_utf8_get_char_validated (str, -1);
1842 if (res < 0)
1844 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1845 int i;
1846 for (i = 0; i < UTF8_CHAR_LEN; i++)
1847 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1848 utf8_buf[UTF8_CHAR_LEN] = '\0';
1849 str = utf8_buf;
1850 res = g_utf8_get_char_validated (str, -1);
1853 if (res < 0)
1855 ch = *str;
1856 width = 0;
1858 else
1860 ch = res;
1861 /* Calculate UTF-8 char width */
1862 next_ch = g_utf8_next_char (str);
1863 if (next_ch)
1865 width = next_ch - str;
1867 else
1869 ch = 0;
1870 width = 0;
1873 *char_width = width;
1874 return ch;
1877 /* --------------------------------------------------------------------------------------------- */
1879 char *
1880 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1882 int i;
1883 char *p, *writename;
1884 const vfs_path_element_t *path_element;
1886 i = edit_find_filter (filename_vpath);
1887 if (i < 0)
1888 return NULL;
1890 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1891 writename = name_quote (path_element->path, 0);
1892 p = g_strdup_printf (all_filters[i].write, writename);
1893 g_free (writename);
1894 return p;
1897 /* --------------------------------------------------------------------------------------------- */
1899 long
1900 edit_write_stream (WEdit * edit, FILE * f)
1902 long i;
1904 if (edit->lb == LB_ASIS)
1906 for (i = 0; i < edit->last_byte; i++)
1907 if (fputc (edit_get_byte (edit, i), f) < 0)
1908 break;
1909 return i;
1912 /* change line breaks */
1913 for (i = 0; i < edit->last_byte; i++)
1915 unsigned char c = edit_get_byte (edit, i);
1917 if (!(c == '\n' || c == '\r'))
1919 /* not line break */
1920 if (fputc (c, f) < 0)
1921 return i;
1923 else
1924 { /* (c == '\n' || c == '\r') */
1925 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1927 switch (edit->lb)
1929 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1930 /* put one line break unconditionally */
1931 if (fputc ('\n', f) < 0)
1932 return i;
1934 i++; /* 2 chars are processed */
1936 if (c == '\r' && c1 == '\n')
1937 /* Windows line break; go to the next char */
1938 break;
1940 if (c == '\r' && c1 == '\r')
1942 /* two Macintosh line breaks; put second line break */
1943 if (fputc ('\n', f) < 0)
1944 return i;
1945 break;
1948 if (fputc (c1, f) < 0)
1949 return i;
1950 break;
1952 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1953 /* put one line break unconditionally */
1954 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1955 return i;
1957 if (c == '\r' && c1 == '\n')
1958 /* Windows line break; go to the next char */
1959 i++;
1960 break;
1962 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1963 /* put one line break unconditionally */
1964 if (fputc ('\r', f) < 0)
1965 return i;
1967 i++; /* 2 chars are processed */
1969 if (c == '\r' && c1 == '\n')
1970 /* Windows line break; go to the next char */
1971 break;
1973 if (c == '\n' && c1 == '\n')
1975 /* two Windows line breaks; put second line break */
1976 if (fputc ('\r', f) < 0)
1977 return i;
1978 break;
1981 if (fputc (c1, f) < 0)
1982 return i;
1983 break;
1984 case LB_ASIS: /* default without changes */
1985 break;
1990 return edit->last_byte;
1993 /* --------------------------------------------------------------------------------------------- */
1995 gboolean
1996 is_break_char (char c)
1998 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2001 /* --------------------------------------------------------------------------------------------- */
2003 char *
2004 edit_get_word_from_pos (WEdit * edit, long start_pos, long *start, gsize * len, gsize * cut)
2006 long word_start;
2007 long cut_len = 0;
2008 GString *match_expr;
2009 unsigned char *bufpos;
2010 int c1, c2;
2012 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2014 c1 = edit_get_byte (edit, word_start);
2015 c2 = edit_get_byte (edit, word_start - 1);
2017 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2018 break;
2021 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2022 match_expr = g_string_sized_new (16);
2026 c1 = edit_get_byte (edit, word_start + match_expr->len);
2027 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2028 g_string_append_c (match_expr, c1);
2030 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2032 *len = match_expr->len;
2033 *start = word_start;
2034 *cut = cut_len;
2036 return g_string_free (match_expr, FALSE);
2039 /* --------------------------------------------------------------------------------------------- */
2040 /** inserts a file at the cursor, returns count of inserted bytes on success */
2042 long
2043 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2045 char *p;
2046 long ins_len = 0;
2048 p = edit_get_filter (filename_vpath);
2049 if (p != NULL)
2051 FILE *f;
2052 long current = edit->curs1;
2054 f = (FILE *) popen (p, "r");
2055 if (f != NULL)
2057 edit_insert_stream (edit, f);
2058 ins_len = edit->curs1 - current;
2059 edit_cursor_move (edit, -ins_len);
2060 if (pclose (f) > 0)
2062 char *errmsg;
2064 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2065 edit_error_dialog (_("Error"), errmsg);
2066 g_free (errmsg);
2067 ins_len = -1;
2070 else
2072 char *errmsg;
2074 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2075 edit_error_dialog (_("Error"), errmsg);
2076 g_free (errmsg);
2077 ins_len = -1;
2079 g_free (p);
2081 else
2083 int i, file, blocklen;
2084 long current = edit->curs1;
2085 int vertical_insertion = 0;
2086 char *buf;
2088 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2089 if (file == -1)
2090 return -1;
2092 buf = g_malloc0 (TEMP_BUF_LEN);
2093 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2094 if (blocklen > 0)
2096 /* if contain signature VERTICAL_MAGIC then it vertical block */
2097 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2098 vertical_insertion = 1;
2099 else
2100 mc_lseek (file, 0, SEEK_SET);
2103 if (vertical_insertion)
2105 long mark1, mark2;
2106 int c1, c2;
2108 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2109 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2110 /* highlight inserted text then not persistent blocks */
2111 if (!option_persistent_selections)
2113 if (!edit->column_highlight)
2114 edit_push_undo_action (edit, COLUMN_OFF);
2115 edit->column_highlight = 1;
2118 else
2120 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2122 for (i = 0; i < blocklen; i++)
2123 edit_insert (edit, buf[i]);
2125 /* highlight inserted text then not persistent blocks */
2126 if (!option_persistent_selections && edit->modified)
2128 edit_set_markers (edit, edit->curs1, current, 0, 0);
2129 if (edit->column_highlight)
2130 edit_push_undo_action (edit, COLUMN_ON);
2131 edit->column_highlight = 0;
2135 edit->force |= REDRAW_PAGE;
2136 ins_len = edit->curs1 - current;
2137 edit_cursor_move (edit, -ins_len);
2138 g_free (buf);
2139 mc_close (file);
2140 if (blocklen != 0)
2141 ins_len = 0;
2144 return ins_len;
2147 /* --------------------------------------------------------------------------------------------- */
2149 * Fill in the edit structure. Return NULL on failure. Pass edit as
2150 * NULL to allocate a new structure.
2152 * If line is 0, try to restore saved position. Otherwise put the
2153 * cursor on that line and show it in the middle of the screen.
2156 WEdit *
2157 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2158 long line)
2160 gboolean to_free = FALSE;
2162 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2163 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2165 if (edit != NULL)
2166 edit_purge_widget (edit);
2167 else
2169 #ifdef ENABLE_NLS
2171 * Expand option_whole_chars_search by national letters using
2172 * current locale
2175 static char option_whole_chars_search_buf[256];
2177 if (option_whole_chars_search_buf != option_whole_chars_search)
2179 size_t i;
2180 size_t len = str_term_width1 (option_whole_chars_search);
2182 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2184 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2186 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2188 option_whole_chars_search_buf[len++] = i;
2192 option_whole_chars_search_buf[len] = 0;
2193 option_whole_chars_search = option_whole_chars_search_buf;
2195 #endif /* ENABLE_NLS */
2196 edit = g_malloc0 (sizeof (WEdit));
2197 to_free = TRUE;
2200 edit->drag_state = MCEDIT_DRAG_NORMAL;
2201 edit->widget.y = y;
2202 edit->widget.x = x;
2203 edit->widget.lines = lines;
2204 edit->widget.cols = cols;
2205 edit_save_size (edit);
2206 edit->fullscreen = TRUE;
2208 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2209 edit->stat1.st_uid = getuid ();
2210 edit->stat1.st_gid = getgid ();
2211 edit->stat1.st_mtime = 0;
2213 edit->over_col = 0;
2214 edit->bracket = -1;
2215 edit->force |= REDRAW_PAGE;
2217 /* set file name before load file */
2218 edit_set_filename (edit, filename_vpath);
2220 edit->undo_stack_size = START_STACK_SIZE;
2221 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2222 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2224 edit->redo_stack_size = START_STACK_SIZE;
2225 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2226 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2228 edit->utf8 = 0;
2229 edit->converter = str_cnv_from_term;
2230 edit_set_codeset (edit);
2232 if (!edit_load_file (edit))
2234 /* edit_load_file already gives an error message */
2235 if (to_free)
2236 g_free (edit);
2237 return NULL;
2240 edit->loading_done = 1;
2241 edit->modified = 0;
2242 edit->locked = 0;
2243 edit_load_syntax (edit, NULL, NULL);
2245 int color;
2246 edit_get_syntax_color (edit, -1, &color);
2249 /* load saved cursor position */
2250 if ((line == 0) && option_save_position)
2251 edit_load_position (edit);
2252 else
2254 if (line <= 0)
2255 line = 1;
2256 edit_move_display (edit, line - 1);
2257 edit_move_to_line (edit, line - 1);
2260 edit_load_macro_cmd (edit);
2262 return edit;
2265 /* --------------------------------------------------------------------------------------------- */
2267 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2268 gboolean
2269 edit_clean (WEdit * edit)
2271 int j = 0;
2273 if (edit == NULL)
2274 return FALSE;
2276 /* a stale lock, remove it */
2277 if (edit->locked)
2278 edit->locked = unlock_file (edit->filename_vpath);
2280 /* save cursor position */
2281 if (option_save_position)
2282 edit_save_position (edit);
2283 else if (edit->serialized_bookmarks != NULL)
2284 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2286 /* File specified on the mcedit command line and never saved */
2287 if (edit->delete_file)
2288 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2290 edit_free_syntax_rules (edit);
2291 book_mark_flush (edit, -1);
2292 for (; j <= MAXBUFF; j++)
2294 g_free (edit->buffers1[j]);
2295 g_free (edit->buffers2[j]);
2298 g_free (edit->undo_stack);
2299 g_free (edit->redo_stack);
2300 vfs_path_free (edit->filename_vpath);
2301 vfs_path_free (edit->dir_vpath);
2302 mc_search_free (edit->search);
2303 edit->search = NULL;
2305 if (edit->converter != str_cnv_from_term)
2306 str_close_conv (edit->converter);
2308 edit_purge_widget (edit);
2310 return TRUE;
2313 /* --------------------------------------------------------------------------------------------- */
2316 * Load a new file into the editor and set line. If it fails, preserve the old file.
2317 * To do it, allocate a new widget, initialize it and, if the new file
2318 * was loaded, copy the data to the old widget.
2320 * @returns TRUE on success, FALSE on failure.
2322 gboolean
2323 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2325 WEdit *e;
2326 int y = edit->widget.y;
2327 int x = edit->widget.x;
2328 int lines = edit->widget.lines;
2329 int columns = edit->widget.cols;
2331 e = g_malloc0 (sizeof (WEdit));
2332 e->widget = edit->widget;
2334 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2336 g_free (e);
2337 return FALSE;
2340 edit_clean (edit);
2341 memcpy (edit, e, sizeof (WEdit));
2342 g_free (e);
2344 return TRUE;
2347 /* --------------------------------------------------------------------------------------------- */
2349 void
2350 edit_set_codeset (WEdit * edit)
2352 #ifdef HAVE_CHARSET
2353 const char *cp_id;
2355 cp_id =
2356 get_codepage_id (mc_global.source_codepage >=
2357 0 ? mc_global.source_codepage : mc_global.display_codepage);
2359 if (cp_id != NULL)
2361 GIConv conv;
2362 conv = str_crt_conv_from (cp_id);
2363 if (conv != INVALID_CONV)
2365 if (edit->converter != str_cnv_from_term)
2366 str_close_conv (edit->converter);
2367 edit->converter = conv;
2371 if (cp_id != NULL)
2372 edit->utf8 = str_isutf8 (cp_id);
2373 #else
2374 (void) edit;
2375 #endif
2379 /* --------------------------------------------------------------------------------------------- */
2381 Recording stack for undo:
2382 The following is an implementation of a compressed stack. Identical
2383 pushes are recorded by a negative prefix indicating the number of times the
2384 same char was pushed. This saves space for repeated curs-left or curs-right
2385 delete etc.
2389 pushed: stored:
2393 b -3
2395 c --> -4
2401 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2402 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2403 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2404 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2405 position.
2407 The only way the cursor moves or the buffer is changed is through the routines:
2408 insert, backspace, insert_ahead, delete, and cursor_move.
2409 These record the reverse undo movements onto the stack each time they are
2410 called.
2412 Each key press results in a set of actions (insert; delete ...). So each time
2413 a key is pressed the current position of start_display is pushed as
2414 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2415 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2416 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2420 void
2421 edit_push_undo_action (WEdit * edit, long c, ...)
2423 unsigned long sp = edit->undo_stack_pointer;
2424 unsigned long spm1;
2425 long *t;
2427 /* first enlarge the stack if necessary */
2428 if (sp > edit->undo_stack_size - 10)
2429 { /* say */
2430 if (option_max_undo < 256)
2431 option_max_undo = 256;
2432 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2434 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2435 if (t)
2437 edit->undo_stack = t;
2438 edit->undo_stack_size <<= 1;
2439 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2443 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2444 if (edit->undo_stack_disable)
2446 edit_push_redo_action (edit, KEY_PRESS);
2447 edit_push_redo_action (edit, c);
2448 return;
2450 else if (edit->redo_stack_reset)
2452 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2455 if (edit->undo_stack_bottom != sp
2456 && spm1 != edit->undo_stack_bottom
2457 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2459 int d;
2460 if (edit->undo_stack[spm1] < 0)
2462 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2463 if (d == c)
2465 if (edit->undo_stack[spm1] > -1000000000)
2467 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2469 edit->undo_stack[spm1]--;
2471 return;
2475 else
2477 d = edit->undo_stack[spm1];
2478 if (d == c)
2480 if (c >= KEY_PRESS)
2481 return; /* --> no need to push multiple do-nothings */
2482 edit->undo_stack[sp] = -2;
2483 goto check_bottom;
2487 edit->undo_stack[sp] = c;
2489 check_bottom:
2490 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2492 /* if the sp wraps round and catches the undo_stack_bottom then erase
2493 * the first set of actions on the stack to make space - by moving
2494 * undo_stack_bottom forward one "key press" */
2495 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2496 if ((unsigned long) c == edit->undo_stack_bottom ||
2497 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2500 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2502 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2503 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2505 /*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: */
2506 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2507 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2509 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2513 void
2514 edit_push_redo_action (WEdit * edit, long c, ...)
2516 unsigned long sp = edit->redo_stack_pointer;
2517 unsigned long spm1;
2518 long *t;
2519 /* first enlarge the stack if necessary */
2520 if (sp > edit->redo_stack_size - 10)
2521 { /* say */
2522 if (option_max_undo < 256)
2523 option_max_undo = 256;
2524 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2526 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2527 if (t)
2529 edit->redo_stack = t;
2530 edit->redo_stack_size <<= 1;
2531 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2535 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2537 if (edit->redo_stack_bottom != sp
2538 && spm1 != edit->redo_stack_bottom
2539 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2541 int d;
2542 if (edit->redo_stack[spm1] < 0)
2544 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2545 if (d == c)
2547 if (edit->redo_stack[spm1] > -1000000000)
2549 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2550 edit->redo_stack[spm1]--;
2551 return;
2555 else
2557 d = edit->redo_stack[spm1];
2558 if (d == c)
2560 if (c >= KEY_PRESS)
2561 return; /* --> no need to push multiple do-nothings */
2562 edit->redo_stack[sp] = -2;
2563 goto redo_check_bottom;
2567 edit->redo_stack[sp] = c;
2569 redo_check_bottom:
2570 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2572 /* if the sp wraps round and catches the redo_stack_bottom then erase
2573 * the first set of actions on the stack to make space - by moving
2574 * redo_stack_bottom forward one "key press" */
2575 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2576 if ((unsigned long) c == edit->redo_stack_bottom ||
2577 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2580 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2582 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2583 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2586 * If a single key produced enough pushes to wrap all the way round then
2587 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2588 * The stack is then initialised:
2591 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2592 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2593 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2597 /* --------------------------------------------------------------------------------------------- */
2599 Basic low level single character buffer alterations and movements at the cursor.
2600 Returns char passed over, inserted or removed.
2603 void
2604 edit_insert (WEdit * edit, int c)
2606 /* check if file has grown to large */
2607 if (edit->last_byte >= SIZE_LIMIT)
2608 return;
2610 /* first we must update the position of the display window */
2611 if (edit->curs1 < edit->start_display)
2613 edit->start_display++;
2614 if (c == '\n')
2615 edit->start_line++;
2618 /* Mark file as modified, unless the file hasn't been fully loaded */
2619 if (edit->loading_done)
2621 edit_modification (edit);
2624 /* now we must update some info on the file and check if a redraw is required */
2625 if (c == '\n')
2627 if (edit->book_mark)
2628 book_mark_inc (edit, edit->curs_line);
2629 edit->curs_line++;
2630 edit->total_lines++;
2631 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2634 /* save the reverse command onto the undo stack */
2635 /* ordinary char and not space */
2636 if (c > 32)
2637 edit_push_undo_action (edit, BACKSPACE);
2638 else
2639 edit_push_undo_action (edit, BACKSPACE_BR);
2640 /* update markers */
2641 edit->mark1 += (edit->mark1 > edit->curs1);
2642 edit->mark2 += (edit->mark2 > edit->curs1);
2643 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2645 /* add a new buffer if we've reached the end of the last one */
2646 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2647 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2649 /* perform the insertion */
2650 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2651 = (unsigned char) c;
2653 /* update file length */
2654 edit->last_byte++;
2656 /* update cursor position */
2657 edit->curs1++;
2660 /* --------------------------------------------------------------------------------------------- */
2661 /** same as edit_insert and move left */
2663 void
2664 edit_insert_ahead (WEdit * edit, int c)
2666 if (edit->last_byte >= SIZE_LIMIT)
2667 return;
2669 if (edit->curs1 < edit->start_display)
2671 edit->start_display++;
2672 if (c == '\n')
2673 edit->start_line++;
2675 edit_modification (edit);
2676 if (c == '\n')
2678 if (edit->book_mark)
2679 book_mark_inc (edit, edit->curs_line);
2680 edit->total_lines++;
2681 edit->force |= REDRAW_AFTER_CURSOR;
2683 /* ordinary char and not space */
2684 if (c > 32)
2685 edit_push_undo_action (edit, DELCHAR);
2686 else
2687 edit_push_undo_action (edit, DELCHAR_BR);
2689 edit->mark1 += (edit->mark1 >= edit->curs1);
2690 edit->mark2 += (edit->mark2 >= edit->curs1);
2691 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2693 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2694 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2695 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2696 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2698 edit->last_byte++;
2699 edit->curs2++;
2703 /* --------------------------------------------------------------------------------------------- */
2706 edit_delete (WEdit * edit, const int byte_delete)
2708 int p = 0;
2709 int cw = 1;
2710 int i;
2712 if (!edit->curs2)
2713 return 0;
2715 cw = 1;
2716 /* if byte_delete = 1 then delete only one byte not multibyte char */
2717 if (edit->utf8 && byte_delete == 0)
2719 edit_get_utf (edit, edit->curs1, &cw);
2720 if (cw < 1)
2721 cw = 1;
2724 if (edit->mark2 != edit->mark1)
2725 edit_push_markers (edit);
2727 for (i = 1; i <= cw; i++)
2729 if (edit->mark1 > edit->curs1)
2731 edit->mark1--;
2732 edit->end_mark_curs--;
2734 if (edit->mark2 > edit->curs1)
2735 edit->mark2--;
2736 if (edit->last_get_rule > edit->curs1)
2737 edit->last_get_rule--;
2739 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2740 ((edit->curs2 -
2741 1) & M_EDIT_BUF_SIZE) - 1];
2743 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2745 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2746 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2748 edit->last_byte--;
2749 edit->curs2--;
2750 edit_push_undo_action (edit, p + 256);
2753 edit_modification (edit);
2754 if (p == '\n')
2756 if (edit->book_mark)
2757 book_mark_dec (edit, edit->curs_line);
2758 edit->total_lines--;
2759 edit->force |= REDRAW_AFTER_CURSOR;
2761 if (edit->curs1 < edit->start_display)
2763 edit->start_display--;
2764 if (p == '\n')
2765 edit->start_line--;
2768 return p;
2771 /* --------------------------------------------------------------------------------------------- */
2774 edit_backspace (WEdit * edit, const int byte_delete)
2776 int p = 0;
2777 int cw = 1;
2778 int i;
2780 if (edit->curs1 == 0)
2781 return 0;
2783 cw = 1;
2785 if (edit->mark2 != edit->mark1)
2786 edit_push_markers (edit);
2788 if (edit->utf8 && byte_delete == 0)
2790 edit_get_prev_utf (edit, edit->curs1, &cw);
2791 if (cw < 1)
2792 cw = 1;
2794 for (i = 1; i <= cw; i++)
2796 if (edit->mark1 >= edit->curs1)
2798 edit->mark1--;
2799 edit->end_mark_curs--;
2801 if (edit->mark2 >= edit->curs1)
2802 edit->mark2--;
2803 if (edit->last_get_rule >= edit->curs1)
2804 edit->last_get_rule--;
2806 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2807 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2808 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2810 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2811 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2813 edit->last_byte--;
2814 edit->curs1--;
2815 edit_push_undo_action (edit, p);
2817 edit_modification (edit);
2818 if (p == '\n')
2820 if (edit->book_mark)
2821 book_mark_dec (edit, edit->curs_line);
2822 edit->curs_line--;
2823 edit->total_lines--;
2824 edit->force |= REDRAW_AFTER_CURSOR;
2827 if (edit->curs1 < edit->start_display)
2829 edit->start_display--;
2830 if (p == '\n')
2831 edit->start_line--;
2834 return p;
2837 /* --------------------------------------------------------------------------------------------- */
2838 /** moves the cursor right or left: increment positive or negative respectively */
2840 void
2841 edit_cursor_move (WEdit * edit, long increment)
2843 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2844 int c;
2846 if (increment < 0)
2848 for (; increment < 0; increment++)
2850 if (!edit->curs1)
2851 return;
2853 edit_push_undo_action (edit, CURS_RIGHT);
2855 c = edit_get_byte (edit, edit->curs1 - 1);
2856 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2857 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2858 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2859 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2860 edit->curs2++;
2861 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2862 1) & M_EDIT_BUF_SIZE];
2863 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2865 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2866 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2868 edit->curs1--;
2869 if (c == '\n')
2871 edit->curs_line--;
2872 edit->force |= REDRAW_LINE_BELOW;
2877 else if (increment > 0)
2879 for (; increment > 0; increment--)
2881 if (!edit->curs2)
2882 return;
2884 edit_push_undo_action (edit, CURS_LEFT);
2886 c = edit_get_byte (edit, edit->curs1);
2887 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2888 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2889 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2890 edit->curs1++;
2891 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2892 ((edit->curs2 -
2893 1) & M_EDIT_BUF_SIZE) - 1];
2894 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2896 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2897 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2899 edit->curs2--;
2900 if (c == '\n')
2902 edit->curs_line++;
2903 edit->force |= REDRAW_LINE_ABOVE;
2909 /* These functions return positions relative to lines */
2911 /* --------------------------------------------------------------------------------------------- */
2912 /** returns index of last char on line + 1 */
2914 long
2915 edit_eol (WEdit * edit, long current)
2917 if (current >= edit->last_byte)
2918 return edit->last_byte;
2920 for (;; current++)
2921 if (edit_get_byte (edit, current) == '\n')
2922 break;
2923 return current;
2926 /* --------------------------------------------------------------------------------------------- */
2927 /** returns index of first char on line */
2929 long
2930 edit_bol (WEdit * edit, long current)
2932 if (current <= 0)
2933 return 0;
2935 for (;; current--)
2936 if (edit_get_byte (edit, current - 1) == '\n')
2937 break;
2938 return current;
2941 /* --------------------------------------------------------------------------------------------- */
2943 long
2944 edit_count_lines (WEdit * edit, long current, long upto)
2946 long lines = 0;
2947 if (upto > edit->last_byte)
2948 upto = edit->last_byte;
2949 if (current < 0)
2950 current = 0;
2951 while (current < upto)
2952 if (edit_get_byte (edit, current++) == '\n')
2953 lines++;
2954 return lines;
2957 /* --------------------------------------------------------------------------------------------- */
2958 /* If lines is zero this returns the count of lines from current to upto. */
2959 /* If upto is zero returns index of lines forward current. */
2961 long
2962 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2964 if (upto)
2966 return edit_count_lines (edit, current, upto);
2968 else
2970 long next;
2971 if (lines < 0)
2972 lines = 0;
2973 while (lines--)
2975 next = edit_eol (edit, current) + 1;
2976 if (next > edit->last_byte)
2977 break;
2978 else
2979 current = next;
2981 return current;
2985 /* --------------------------------------------------------------------------------------------- */
2986 /** Returns offset of 'lines' lines up from current */
2988 long
2989 edit_move_backward (WEdit * edit, long current, long lines)
2991 if (lines < 0)
2992 lines = 0;
2993 current = edit_bol (edit, current);
2994 while ((lines--) && current != 0)
2995 current = edit_bol (edit, current - 1);
2996 return current;
2999 /* --------------------------------------------------------------------------------------------- */
3000 /* If cols is zero this returns the count of columns from current to upto. */
3001 /* If upto is zero returns index of cols across from current. */
3003 long
3004 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3006 long p, q;
3007 int col;
3009 if (upto)
3011 q = upto;
3012 cols = -10;
3014 else
3015 q = edit->last_byte + 2;
3017 for (col = 0, p = current; p < q; p++)
3019 int c, orig_c;
3021 if (cols != -10)
3023 if (col == cols)
3024 return p;
3025 if (col > cols)
3026 return p - 1;
3029 orig_c = c = edit_get_byte (edit, p);
3031 #ifdef HAVE_CHARSET
3032 if (edit->utf8)
3034 int utf_ch;
3035 int cw = 1;
3037 utf_ch = edit_get_utf (edit, p, &cw);
3038 if (mc_global.utf8_display)
3040 if (cw > 1)
3041 col -= cw - 1;
3042 if (g_unichar_iswide (utf_ch))
3043 col++;
3045 else if (cw > 1 && g_unichar_isprint (utf_ch))
3046 col -= cw - 1;
3049 c = convert_to_display_c (c);
3050 #endif
3052 if (c == '\t')
3053 col += TAB_SIZE - col % TAB_SIZE;
3054 else if (c == '\n')
3056 if (upto)
3057 return col;
3058 else
3059 return p;
3061 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3062 /* '\r' is shown as ^M, so we must advance 2 characters */
3063 /* Caret notation for control characters */
3064 col += 2;
3065 else
3066 col++;
3068 return col;
3071 /* --------------------------------------------------------------------------------------------- */
3072 /** returns the current column position of the cursor */
3075 edit_get_col (WEdit * edit)
3077 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3080 /* --------------------------------------------------------------------------------------------- */
3081 /* Scrolling functions */
3082 /* --------------------------------------------------------------------------------------------- */
3084 void
3085 edit_update_curs_row (WEdit * edit)
3087 edit->curs_row = edit->curs_line - edit->start_line;
3090 /* --------------------------------------------------------------------------------------------- */
3092 void
3093 edit_update_curs_col (WEdit * edit)
3095 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3098 /* --------------------------------------------------------------------------------------------- */
3101 edit_get_curs_col (const WEdit * edit)
3103 return edit->curs_col;
3106 /* --------------------------------------------------------------------------------------------- */
3107 /** moves the display start position up by i lines */
3109 void
3110 edit_scroll_upward (WEdit * edit, unsigned long i)
3112 unsigned long lines_above = edit->start_line;
3113 if (i > lines_above)
3114 i = lines_above;
3115 if (i)
3117 edit->start_line -= i;
3118 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3119 edit->force |= REDRAW_PAGE;
3120 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3122 edit_update_curs_row (edit);
3126 /* --------------------------------------------------------------------------------------------- */
3127 /** returns 1 if could scroll, 0 otherwise */
3129 void
3130 edit_scroll_downward (WEdit * edit, int i)
3132 int lines_below;
3133 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3134 if (lines_below > 0)
3136 if (i > lines_below)
3137 i = lines_below;
3138 edit->start_line += i;
3139 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3140 edit->force |= REDRAW_PAGE;
3141 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3143 edit_update_curs_row (edit);
3146 /* --------------------------------------------------------------------------------------------- */
3148 void
3149 edit_scroll_right (WEdit * edit, int i)
3151 edit->force |= REDRAW_PAGE;
3152 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3153 edit->start_col -= i;
3156 /* --------------------------------------------------------------------------------------------- */
3158 void
3159 edit_scroll_left (WEdit * edit, int i)
3161 if (edit->start_col)
3163 edit->start_col += i;
3164 if (edit->start_col > 0)
3165 edit->start_col = 0;
3166 edit->force |= REDRAW_PAGE;
3167 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3171 /* --------------------------------------------------------------------------------------------- */
3172 /* high level cursor movement commands */
3173 /* --------------------------------------------------------------------------------------------- */
3175 void
3176 edit_move_to_prev_col (WEdit * edit, long p)
3178 int prev = edit->prev_col;
3179 int over = edit->over_col;
3180 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3182 if (option_cursor_beyond_eol)
3184 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3185 edit_eol (edit, edit->curs1));
3187 if (line_len < prev + edit->over_col)
3189 edit->over_col = prev + over - line_len;
3190 edit->prev_col = line_len;
3191 edit->curs_col = line_len;
3193 else
3195 edit->curs_col = prev + over;
3196 edit->prev_col = edit->curs_col;
3197 edit->over_col = 0;
3200 else
3202 edit->over_col = 0;
3203 if (is_in_indent (edit) && option_fake_half_tabs)
3205 edit_update_curs_col (edit);
3206 if (space_width)
3207 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3209 int q = edit->curs_col;
3210 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3211 p = edit_bol (edit, edit->curs1);
3212 edit_cursor_move (edit,
3213 edit_move_forward3 (edit, p, edit->curs_col,
3214 0) - edit->curs1);
3215 if (!left_of_four_spaces (edit))
3216 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3222 /* --------------------------------------------------------------------------------------------- */
3225 line_is_blank (WEdit * edit, long line)
3227 return is_blank (edit, edit_find_line (edit, line));
3230 /* --------------------------------------------------------------------------------------------- */
3231 /** move cursor to line 'line' */
3233 void
3234 edit_move_to_line (WEdit * e, long line)
3236 if (line < e->curs_line)
3237 edit_move_up (e, e->curs_line - line, 0);
3238 else
3239 edit_move_down (e, line - e->curs_line, 0);
3240 edit_scroll_screen_over_cursor (e);
3243 /* --------------------------------------------------------------------------------------------- */
3244 /** scroll window so that first visible line is 'line' */
3246 void
3247 edit_move_display (WEdit * e, long line)
3249 if (line < e->start_line)
3250 edit_scroll_upward (e, e->start_line - line);
3251 else
3252 edit_scroll_downward (e, line - e->start_line);
3255 /* --------------------------------------------------------------------------------------------- */
3256 /** save markers onto undo stack */
3258 void
3259 edit_push_markers (WEdit * edit)
3261 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3262 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3263 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3266 /* --------------------------------------------------------------------------------------------- */
3268 void
3269 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3271 edit->mark1 = m1;
3272 edit->mark2 = m2;
3273 edit->column1 = c1;
3274 edit->column2 = c2;
3278 /* --------------------------------------------------------------------------------------------- */
3279 /** highlight marker toggle */
3281 void
3282 edit_mark_cmd (WEdit * edit, int unmark)
3284 edit_push_markers (edit);
3285 if (unmark)
3287 edit_set_markers (edit, 0, 0, 0, 0);
3288 edit->force |= REDRAW_PAGE;
3290 else
3292 if (edit->mark2 >= 0)
3294 edit->end_mark_curs = -1;
3295 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3296 edit->curs_col + edit->over_col);
3297 edit->force |= REDRAW_PAGE;
3299 else
3301 edit->end_mark_curs = edit->curs1;
3302 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3303 edit->curs_col + edit->over_col);
3308 /* --------------------------------------------------------------------------------------------- */
3309 /** highlight the word under cursor */
3311 void
3312 edit_mark_current_word_cmd (WEdit * edit)
3314 long pos;
3316 for (pos = edit->curs1; pos != 0; pos--)
3318 int c1, c2;
3320 c1 = edit_get_byte (edit, pos);
3321 c2 = edit_get_byte (edit, pos - 1);
3322 if (!isspace (c1) && isspace (c2))
3323 break;
3324 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3325 break;
3327 edit->mark1 = pos;
3329 for (; pos < edit->last_byte; pos++)
3331 int c1, c2;
3333 c1 = edit_get_byte (edit, pos);
3334 c2 = edit_get_byte (edit, pos + 1);
3335 if (!isspace (c1) && isspace (c2))
3336 break;
3337 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3338 break;
3340 edit->mark2 = min (pos + 1, edit->last_byte);
3342 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3345 /* --------------------------------------------------------------------------------------------- */
3347 void
3348 edit_mark_current_line_cmd (WEdit * edit)
3350 long pos = edit->curs1;
3352 edit->mark1 = edit_bol (edit, pos);
3353 edit->mark2 = edit_eol (edit, pos);
3355 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3358 /* --------------------------------------------------------------------------------------------- */
3360 void
3361 edit_delete_line (WEdit * edit)
3364 * Delete right part of the line.
3365 * Note that edit_get_byte() returns '\n' when byte position is
3366 * beyond EOF.
3368 while (edit_get_byte (edit, edit->curs1) != '\n')
3370 (void) edit_delete (edit, 1);
3374 * Delete '\n' char.
3375 * Note that edit_delete() will not corrupt anything if called while
3376 * cursor position is EOF.
3378 (void) edit_delete (edit, 1);
3381 * Delete left part of the line.
3382 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3384 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3386 (void) edit_backspace (edit, 1);
3390 /* --------------------------------------------------------------------------------------------- */
3393 edit_indent_width (WEdit * edit, long p)
3395 long q = p;
3396 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3397 q++;
3398 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3401 /* --------------------------------------------------------------------------------------------- */
3403 void
3404 edit_insert_indent (WEdit * edit, int indent)
3406 if (!option_fill_tabs_with_spaces)
3408 while (indent >= TAB_SIZE)
3410 edit_insert (edit, '\t');
3411 indent -= TAB_SIZE;
3414 while (indent-- > 0)
3415 edit_insert (edit, ' ');
3418 /* --------------------------------------------------------------------------------------------- */
3420 void
3421 edit_push_key_press (WEdit * edit)
3423 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3424 if (edit->mark2 == -1)
3426 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3427 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3431 /* --------------------------------------------------------------------------------------------- */
3433 void
3434 edit_find_bracket (WEdit * edit)
3436 edit->bracket = edit_get_bracket (edit, 1, 10000);
3437 if (last_bracket != edit->bracket)
3438 edit->force |= REDRAW_PAGE;
3439 last_bracket = edit->bracket;
3442 /* --------------------------------------------------------------------------------------------- */
3444 * This executes a command as though the user initiated it through a key
3445 * press. Callback with WIDGET_KEY as a message calls this after
3446 * translating the key press. This function can be used to pass any
3447 * command to the editor. Note that the screen wouldn't update
3448 * automatically. Either of command or char_for_insertion must be
3449 * passed as -1. Commands are executed, and char_for_insertion is
3450 * inserted at the cursor.
3453 void
3454 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3456 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3457 || (macro_index < 0
3458 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3460 macro_index = 0;
3461 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3462 return;
3464 if (macro_index != -1)
3466 edit->force |= REDRAW_COMPLETELY;
3467 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3469 edit_store_macro_cmd (edit);
3470 macro_index = -1;
3471 return;
3473 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3475 edit_repeat_macro_cmd (edit);
3476 macro_index = -1;
3477 return;
3481 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3483 record_macro_buf[macro_index].action = command;
3484 record_macro_buf[macro_index++].ch = char_for_insertion;
3486 /* record the beginning of a set of editing actions initiated by a key press */
3487 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3488 edit_push_key_press (edit);
3490 edit_execute_cmd (edit, command, char_for_insertion);
3491 if (edit->column_highlight)
3492 edit->force |= REDRAW_PAGE;
3495 /* --------------------------------------------------------------------------------------------- */
3497 This executes a command at a lower level than macro recording.
3498 It also does not push a key_press onto the undo stack. This means
3499 that if it is called many times, a single undo command will undo
3500 all of them. It also does not check for the Undo command.
3502 void
3503 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3505 if (command == CK_WindowFullscreen)
3507 edit_toggle_fullscreen (edit);
3508 return;
3511 /* handle window state */
3512 if (edit_handle_move_resize (edit, command))
3513 return;
3515 edit->force |= REDRAW_LINE;
3517 /* The next key press will unhighlight the found string, so update
3518 * the whole page */
3519 if (edit->found_len || edit->column_highlight)
3520 edit->force |= REDRAW_PAGE;
3522 switch (command)
3524 /* a mark command with shift-arrow */
3525 case CK_MarkLeft:
3526 case CK_MarkRight:
3527 case CK_MarkToWordBegin:
3528 case CK_MarkToWordEnd:
3529 case CK_MarkToHome:
3530 case CK_MarkToEnd:
3531 case CK_MarkUp:
3532 case CK_MarkDown:
3533 case CK_MarkPageUp:
3534 case CK_MarkPageDown:
3535 case CK_MarkToFileBegin:
3536 case CK_MarkToFileEnd:
3537 case CK_MarkToPageBegin:
3538 case CK_MarkToPageEnd:
3539 case CK_MarkScrollUp:
3540 case CK_MarkScrollDown:
3541 case CK_MarkParagraphUp:
3542 case CK_MarkParagraphDown:
3543 /* a mark command with alt-arrow */
3544 case CK_MarkColumnPageUp:
3545 case CK_MarkColumnPageDown:
3546 case CK_MarkColumnLeft:
3547 case CK_MarkColumnRight:
3548 case CK_MarkColumnUp:
3549 case CK_MarkColumnDown:
3550 case CK_MarkColumnScrollUp:
3551 case CK_MarkColumnScrollDown:
3552 case CK_MarkColumnParagraphUp:
3553 case CK_MarkColumnParagraphDown:
3554 edit->column_highlight = 0;
3555 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3557 edit_mark_cmd (edit, 1); /* clear */
3558 edit_mark_cmd (edit, 0); /* marking on */
3560 edit->highlight = 1;
3561 break;
3563 /* any other command */
3564 default:
3565 if (edit->highlight)
3566 edit_mark_cmd (edit, 0); /* clear */
3567 edit->highlight = 0;
3570 /* first check for undo */
3571 if (command == CK_Undo)
3573 edit->redo_stack_reset = 0;
3574 edit_group_undo (edit);
3575 edit->found_len = 0;
3576 edit->prev_col = edit_get_col (edit);
3577 edit->search_start = edit->curs1;
3578 return;
3580 /* check for redo */
3581 if (command == CK_Redo)
3583 edit->redo_stack_reset = 0;
3584 edit_do_redo (edit);
3585 edit->found_len = 0;
3586 edit->prev_col = edit_get_col (edit);
3587 edit->search_start = edit->curs1;
3588 return;
3591 edit->redo_stack_reset = 1;
3593 /* An ordinary key press */
3594 if (char_for_insertion >= 0)
3596 /* if non persistent selection and text selected */
3597 if (!option_persistent_selections)
3599 if (edit->mark1 != edit->mark2)
3600 edit_block_delete_cmd (edit);
3602 if (edit->overwrite)
3604 /* remove char only one time, after input first byte, multibyte chars */
3605 if ((!mc_global.utf8_display || edit->charpoint == 0)
3606 && edit_get_byte (edit, edit->curs1) != '\n')
3607 edit_delete (edit, 0);
3609 if (option_cursor_beyond_eol && edit->over_col > 0)
3610 edit_insert_over (edit);
3611 #ifdef HAVE_CHARSET
3612 if (char_for_insertion > 255 && !mc_global.utf8_display)
3614 unsigned char str[6 + 1];
3615 size_t i = 0;
3616 int res;
3618 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3619 if (res == 0)
3621 str[0] = '.';
3622 str[1] = '\0';
3624 else
3626 str[res] = '\0';
3628 while (str[i] != 0 && i <= 6)
3630 char_for_insertion = str[i];
3631 edit_insert (edit, char_for_insertion);
3632 i++;
3635 else
3636 #endif
3637 edit_insert (edit, char_for_insertion);
3639 if (option_auto_para_formatting)
3641 format_paragraph (edit, 0);
3642 edit->force |= REDRAW_PAGE;
3644 else
3645 check_and_wrap_line (edit);
3646 edit->found_len = 0;
3647 edit->prev_col = edit_get_col (edit);
3648 edit->search_start = edit->curs1;
3649 edit_find_bracket (edit);
3650 return;
3653 switch (command)
3655 case CK_TopOnScreen:
3656 case CK_BottomOnScreen:
3657 case CK_Top:
3658 case CK_Bottom:
3659 case CK_PageUp:
3660 case CK_PageDown:
3661 case CK_Home:
3662 case CK_End:
3663 case CK_Up:
3664 case CK_Down:
3665 case CK_Left:
3666 case CK_Right:
3667 case CK_WordLeft:
3668 case CK_WordRight:
3669 if (edit->mark2 >= 0)
3671 if (!option_persistent_selections)
3673 if (edit->column_highlight)
3674 edit_push_undo_action (edit, COLUMN_ON);
3675 edit->column_highlight = 0;
3676 edit_mark_cmd (edit, 1);
3681 switch (command)
3683 case CK_TopOnScreen:
3684 case CK_BottomOnScreen:
3685 case CK_MarkToPageBegin:
3686 case CK_MarkToPageEnd:
3687 case CK_Up:
3688 case CK_Down:
3689 case CK_WordLeft:
3690 case CK_WordRight:
3691 case CK_MarkToWordBegin:
3692 case CK_MarkToWordEnd:
3693 case CK_MarkUp:
3694 case CK_MarkDown:
3695 case CK_MarkColumnUp:
3696 case CK_MarkColumnDown:
3697 if (edit->mark2 == -1)
3698 break; /*marking is following the cursor: may need to highlight a whole line */
3699 case CK_Left:
3700 case CK_Right:
3701 case CK_MarkLeft:
3702 case CK_MarkRight:
3703 edit->force |= REDRAW_CHAR_ONLY;
3706 /* basic cursor key commands */
3707 switch (command)
3709 case CK_BackSpace:
3710 /* if non persistent selection and text selected */
3711 if (!option_persistent_selections)
3713 if (edit->mark1 != edit->mark2)
3715 edit_block_delete_cmd (edit);
3716 break;
3719 if (option_cursor_beyond_eol && edit->over_col > 0)
3721 edit->over_col--;
3722 break;
3724 if (option_backspace_through_tabs && is_in_indent (edit))
3726 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3727 edit_backspace (edit, 1);
3728 break;
3730 else
3732 if (option_fake_half_tabs)
3734 int i;
3735 if (is_in_indent (edit) && right_of_four_spaces (edit))
3737 for (i = 0; i < HALF_TAB_SIZE; i++)
3738 edit_backspace (edit, 1);
3739 break;
3743 edit_backspace (edit, 0);
3744 break;
3745 case CK_Delete:
3746 /* if non persistent selection and text selected */
3747 if (!option_persistent_selections)
3749 if (edit->mark1 != edit->mark2)
3751 edit_block_delete_cmd (edit);
3752 break;
3756 if (option_cursor_beyond_eol && edit->over_col > 0)
3757 edit_insert_over (edit);
3759 if (option_fake_half_tabs)
3761 int i;
3762 if (is_in_indent (edit) && left_of_four_spaces (edit))
3764 for (i = 1; i <= HALF_TAB_SIZE; i++)
3765 edit_delete (edit, 1);
3766 break;
3769 edit_delete (edit, 0);
3770 break;
3771 case CK_DeleteToWordBegin:
3772 edit->over_col = 0;
3773 edit_left_delete_word (edit);
3774 break;
3775 case CK_DeleteToWordEnd:
3776 if (option_cursor_beyond_eol && edit->over_col > 0)
3777 edit_insert_over (edit);
3779 edit_right_delete_word (edit);
3780 break;
3781 case CK_DeleteLine:
3782 edit_delete_line (edit);
3783 break;
3784 case CK_DeleteToHome:
3785 edit_delete_to_line_begin (edit);
3786 break;
3787 case CK_DeleteToEnd:
3788 edit_delete_to_line_end (edit);
3789 break;
3790 case CK_Enter:
3791 edit->over_col = 0;
3792 if (option_auto_para_formatting)
3794 edit_double_newline (edit);
3795 if (option_return_does_auto_indent)
3796 edit_auto_indent (edit);
3797 format_paragraph (edit, 0);
3799 else
3801 edit_insert (edit, '\n');
3802 if (option_return_does_auto_indent)
3804 edit_auto_indent (edit);
3807 break;
3808 case CK_Return:
3809 edit_insert (edit, '\n');
3810 break;
3812 case CK_MarkColumnPageUp:
3813 edit->column_highlight = 1;
3814 case CK_PageUp:
3815 case CK_MarkPageUp:
3816 edit_move_up (edit, edit->widget.lines - 1, 1);
3817 break;
3818 case CK_MarkColumnPageDown:
3819 edit->column_highlight = 1;
3820 case CK_PageDown:
3821 case CK_MarkPageDown:
3822 edit_move_down (edit, edit->widget.lines - 1, 1);
3823 break;
3824 case CK_MarkColumnLeft:
3825 edit->column_highlight = 1;
3826 case CK_Left:
3827 case CK_MarkLeft:
3828 if (option_fake_half_tabs)
3830 if (is_in_indent (edit) && right_of_four_spaces (edit))
3832 if (option_cursor_beyond_eol && edit->over_col > 0)
3833 edit->over_col--;
3834 else
3835 edit_cursor_move (edit, -HALF_TAB_SIZE);
3836 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3837 break;
3840 edit_left_char_move_cmd (edit);
3841 break;
3842 case CK_MarkColumnRight:
3843 edit->column_highlight = 1;
3844 case CK_Right:
3845 case CK_MarkRight:
3846 if (option_fake_half_tabs)
3848 if (is_in_indent (edit) && left_of_four_spaces (edit))
3850 edit_cursor_move (edit, HALF_TAB_SIZE);
3851 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3852 break;
3855 edit_right_char_move_cmd (edit);
3856 break;
3857 case CK_TopOnScreen:
3858 case CK_MarkToPageBegin:
3859 edit_begin_page (edit);
3860 break;
3861 case CK_BottomOnScreen:
3862 case CK_MarkToPageEnd:
3863 edit_end_page (edit);
3864 break;
3865 case CK_WordLeft:
3866 case CK_MarkToWordBegin:
3867 edit->over_col = 0;
3868 edit_left_word_move_cmd (edit);
3869 break;
3870 case CK_WordRight:
3871 case CK_MarkToWordEnd:
3872 edit->over_col = 0;
3873 edit_right_word_move_cmd (edit);
3874 break;
3875 case CK_MarkColumnUp:
3876 edit->column_highlight = 1;
3877 case CK_Up:
3878 case CK_MarkUp:
3879 edit_move_up (edit, 1, 0);
3880 break;
3881 case CK_MarkColumnDown:
3882 edit->column_highlight = 1;
3883 case CK_Down:
3884 case CK_MarkDown:
3885 edit_move_down (edit, 1, 0);
3886 break;
3887 case CK_MarkColumnParagraphUp:
3888 edit->column_highlight = 1;
3889 case CK_ParagraphUp:
3890 case CK_MarkParagraphUp:
3891 edit_move_up_paragraph (edit, 0);
3892 break;
3893 case CK_MarkColumnParagraphDown:
3894 edit->column_highlight = 1;
3895 case CK_ParagraphDown:
3896 case CK_MarkParagraphDown:
3897 edit_move_down_paragraph (edit, 0);
3898 break;
3899 case CK_MarkColumnScrollUp:
3900 edit->column_highlight = 1;
3901 case CK_ScrollUp:
3902 case CK_MarkScrollUp:
3903 edit_move_up (edit, 1, 1);
3904 break;
3905 case CK_MarkColumnScrollDown:
3906 edit->column_highlight = 1;
3907 case CK_ScrollDown:
3908 case CK_MarkScrollDown:
3909 edit_move_down (edit, 1, 1);
3910 break;
3911 case CK_Home:
3912 case CK_MarkToHome:
3913 edit_cursor_to_bol (edit);
3914 break;
3915 case CK_End:
3916 case CK_MarkToEnd:
3917 edit_cursor_to_eol (edit);
3918 break;
3919 case CK_Tab:
3920 /* if text marked shift block */
3921 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3923 if (edit->mark2 < 0)
3924 edit_mark_cmd (edit, 0);
3925 edit_move_block_to_right (edit);
3927 else
3929 if (option_cursor_beyond_eol)
3930 edit_insert_over (edit);
3931 edit_tab_cmd (edit);
3932 if (option_auto_para_formatting)
3934 format_paragraph (edit, 0);
3935 edit->force |= REDRAW_PAGE;
3937 else
3939 check_and_wrap_line (edit);
3942 break;
3944 case CK_InsertOverwrite:
3945 edit->overwrite = !edit->overwrite;
3946 break;
3948 case CK_Mark:
3949 if (edit->mark2 >= 0)
3951 if (edit->column_highlight)
3952 edit_push_undo_action (edit, COLUMN_ON);
3953 edit->column_highlight = 0;
3955 edit_mark_cmd (edit, 0);
3956 break;
3957 case CK_MarkColumn:
3958 if (!edit->column_highlight)
3959 edit_push_undo_action (edit, COLUMN_OFF);
3960 edit->column_highlight = 1;
3961 edit_mark_cmd (edit, 0);
3962 break;
3963 case CK_MarkAll:
3964 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3965 edit->force |= REDRAW_PAGE;
3966 break;
3967 case CK_Unmark:
3968 if (edit->column_highlight)
3969 edit_push_undo_action (edit, COLUMN_ON);
3970 edit->column_highlight = 0;
3971 edit_mark_cmd (edit, 1);
3972 break;
3973 case CK_MarkWord:
3974 if (edit->column_highlight)
3975 edit_push_undo_action (edit, COLUMN_ON);
3976 edit->column_highlight = 0;
3977 edit_mark_current_word_cmd (edit);
3978 break;
3979 case CK_MarkLine:
3980 if (edit->column_highlight)
3981 edit_push_undo_action (edit, COLUMN_ON);
3982 edit->column_highlight = 0;
3983 edit_mark_current_line_cmd (edit);
3984 break;
3986 case CK_Bookmark:
3987 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3988 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3989 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3990 else
3991 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3992 break;
3993 case CK_BookmarkFlush:
3994 book_mark_flush (edit, BOOK_MARK_COLOR);
3995 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3996 edit->force |= REDRAW_PAGE;
3997 break;
3998 case CK_BookmarkNext:
3999 if (edit->book_mark)
4001 struct _book_mark *p;
4002 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4003 if (p->next)
4005 p = p->next;
4006 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4007 edit_move_display (edit, p->line - edit->widget.lines / 2);
4008 edit_move_to_line (edit, p->line);
4011 break;
4012 case CK_BookmarkPrev:
4013 if (edit->book_mark)
4015 struct _book_mark *p;
4016 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4017 while (p->line == edit->curs_line)
4018 if (p->prev)
4019 p = p->prev;
4020 if (p->line >= 0)
4022 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4023 edit_move_display (edit, p->line - edit->widget.lines / 2);
4024 edit_move_to_line (edit, p->line);
4027 break;
4029 case CK_Top:
4030 case CK_MarkToFileBegin:
4031 edit_move_to_top (edit);
4032 break;
4033 case CK_Bottom:
4034 case CK_MarkToFileEnd:
4035 edit_move_to_bottom (edit);
4036 break;
4038 case CK_Copy:
4039 if (option_cursor_beyond_eol && edit->over_col > 0)
4040 edit_insert_over (edit);
4041 edit_block_copy_cmd (edit);
4042 break;
4043 case CK_Remove:
4044 edit_block_delete_cmd (edit);
4045 break;
4046 case CK_Move:
4047 edit_block_move_cmd (edit);
4048 break;
4050 case CK_BlockShiftLeft:
4051 if (edit->mark1 != edit->mark2)
4052 edit_move_block_to_left (edit);
4053 break;
4054 case CK_BlockShiftRight:
4055 if (edit->mark1 != edit->mark2)
4056 edit_move_block_to_right (edit);
4057 break;
4058 case CK_Store:
4059 edit_copy_to_X_buf_cmd (edit);
4060 break;
4061 case CK_Cut:
4062 edit_cut_to_X_buf_cmd (edit);
4063 break;
4064 case CK_Paste:
4065 /* if non persistent selection and text selected */
4066 if (!option_persistent_selections)
4068 if (edit->mark1 != edit->mark2)
4069 edit_block_delete_cmd (edit);
4071 if (option_cursor_beyond_eol && edit->over_col > 0)
4072 edit_insert_over (edit);
4073 edit_paste_from_X_buf_cmd (edit);
4074 break;
4075 case CK_History:
4076 edit_paste_from_history (edit);
4077 break;
4079 case CK_SaveAs:
4080 edit_save_as_cmd (edit);
4081 break;
4082 case CK_Save:
4083 edit_save_confirm_cmd (edit);
4084 break;
4085 case CK_BlockSave:
4086 edit_save_block_cmd (edit);
4087 break;
4088 case CK_InsertFile:
4089 edit_insert_file_cmd (edit);
4090 break;
4092 case CK_FilePrev:
4093 edit_load_back_cmd (edit);
4094 break;
4095 case CK_FileNext:
4096 edit_load_forward_cmd (edit);
4097 break;
4099 case CK_SyntaxChoose:
4100 edit_syntax_dialog (edit);
4101 break;
4103 case CK_Search:
4104 edit_search_cmd (edit, FALSE);
4105 break;
4106 case CK_SearchContinue:
4107 edit_search_cmd (edit, TRUE);
4108 break;
4109 case CK_Replace:
4110 edit_replace_cmd (edit, 0);
4111 break;
4112 case CK_ReplaceContinue:
4113 edit_replace_cmd (edit, 1);
4114 break;
4115 case CK_Complete:
4116 /* if text marked shift block */
4117 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4119 edit_move_block_to_left (edit);
4121 else
4123 edit_complete_word_cmd (edit);
4125 break;
4126 case CK_Find:
4127 edit_get_match_keyword_cmd (edit);
4128 break;
4129 #ifdef HAVE_ASPELL
4130 case CK_SpellCheckCurrentWord:
4131 edit_suggest_current_word (edit);
4132 break;
4133 case CK_SpellCheck:
4134 edit_spellcheck_file (edit);
4135 break;
4136 case CK_SpellCheckSelectLang:
4137 edit_set_spell_lang ();
4138 break;
4139 #endif
4140 case CK_Date:
4142 char s[BUF_MEDIUM];
4143 /* fool gcc to prevent a Y2K warning */
4144 char time_format[] = "_c";
4145 time_format[0] = '%';
4147 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4148 edit_print_string (edit, s);
4149 edit->force |= REDRAW_PAGE;
4150 break;
4152 break;
4153 case CK_Goto:
4154 edit_goto_cmd (edit);
4155 break;
4156 case CK_ParagraphFormat:
4157 format_paragraph (edit, 1);
4158 edit->force |= REDRAW_PAGE;
4159 break;
4160 case CK_MacroDelete:
4161 edit_delete_macro_cmd (edit);
4162 break;
4163 case CK_MatchBracket:
4164 edit_goto_matching_bracket (edit);
4165 break;
4166 case CK_UserMenu:
4167 user_menu (edit, NULL, -1);
4168 break;
4169 case CK_Sort:
4170 edit_sort_cmd (edit);
4171 break;
4172 case CK_ExternalCommand:
4173 edit_ext_cmd (edit);
4174 break;
4175 case CK_Mail:
4176 edit_mail_dialog (edit);
4177 break;
4178 #ifdef HAVE_CHARSET
4179 case CK_SelectCodepage:
4180 edit_select_codepage_cmd (edit);
4181 break;
4182 #endif
4183 case CK_InsertLiteral:
4184 edit_insert_literal_cmd (edit);
4185 break;
4186 case CK_MacroStartStopRecord:
4187 edit_begin_end_macro_cmd (edit);
4188 break;
4189 case CK_RepeatStartStopRecord:
4190 edit_begin_end_repeat_cmd (edit);
4191 break;
4192 case CK_ExtendedKeyMap:
4193 edit->extmod = TRUE;
4194 break;
4195 default:
4196 break;
4199 /* CK_PipeBlock */
4200 if ((command / CK_PipeBlock (0)) == 1)
4201 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4203 /* keys which must set the col position, and the search vars */
4204 switch (command)
4206 case CK_Search:
4207 case CK_SearchContinue:
4208 case CK_Replace:
4209 case CK_ReplaceContinue:
4210 case CK_Complete:
4211 edit->prev_col = edit_get_col (edit);
4212 break;
4213 case CK_Up:
4214 case CK_MarkUp:
4215 case CK_MarkColumnUp:
4216 case CK_Down:
4217 case CK_MarkDown:
4218 case CK_MarkColumnDown:
4219 case CK_PageUp:
4220 case CK_MarkPageUp:
4221 case CK_MarkColumnPageUp:
4222 case CK_PageDown:
4223 case CK_MarkPageDown:
4224 case CK_MarkColumnPageDown:
4225 case CK_Top:
4226 case CK_MarkToFileBegin:
4227 case CK_Bottom:
4228 case CK_MarkToFileEnd:
4229 case CK_ParagraphUp:
4230 case CK_MarkParagraphUp:
4231 case CK_MarkColumnParagraphUp:
4232 case CK_ParagraphDown:
4233 case CK_MarkParagraphDown:
4234 case CK_MarkColumnParagraphDown:
4235 case CK_ScrollUp:
4236 case CK_MarkScrollUp:
4237 case CK_MarkColumnScrollUp:
4238 case CK_ScrollDown:
4239 case CK_MarkScrollDown:
4240 case CK_MarkColumnScrollDown:
4241 edit->search_start = edit->curs1;
4242 edit->found_len = 0;
4243 break;
4244 default:
4245 edit->found_len = 0;
4246 edit->prev_col = edit_get_col (edit);
4247 edit->search_start = edit->curs1;
4249 edit_find_bracket (edit);
4251 if (option_auto_para_formatting)
4253 switch (command)
4255 case CK_BackSpace:
4256 case CK_Delete:
4257 case CK_DeleteToWordBegin:
4258 case CK_DeleteToWordEnd:
4259 case CK_DeleteToHome:
4260 case CK_DeleteToEnd:
4261 format_paragraph (edit, 0);
4262 edit->force |= REDRAW_PAGE;
4267 /* --------------------------------------------------------------------------------------------- */
4269 void
4270 edit_stack_init (void)
4272 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4274 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4275 edit_history_moveto[edit_stack_iterator].line = -1;
4278 edit_stack_iterator = 0;
4281 /* --------------------------------------------------------------------------------------------- */
4283 void
4284 edit_stack_free (void)
4286 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4287 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4290 /* --------------------------------------------------------------------------------------------- */
4291 /** move i lines */
4293 void
4294 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4296 edit_move_updown (edit, i, do_scroll, TRUE);
4299 /* --------------------------------------------------------------------------------------------- */
4300 /** move i lines */
4302 void
4303 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4305 edit_move_updown (edit, i, do_scroll, FALSE);
4308 /* --------------------------------------------------------------------------------------------- */