(edit_mark_cmd): take gboolean instead of int.
[midnight-commander.git] / src / editor / edit.c
blob54b627acaf2d6468f147526d2b3db10e297e6cf4
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
73 #ifdef HAVE_ASPELL
74 #include "spell.h"
75 #endif
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
80 int option_typewriter_wrap = 0;
81 int option_auto_para_formatting = 0;
82 int option_fill_tabs_with_spaces = 0;
83 int option_return_does_auto_indent = 1;
84 int option_backspace_through_tabs = 0;
85 int option_fake_half_tabs = 1;
86 int option_save_mode = EDIT_QUICK_SAVE;
87 int option_save_position = 1;
88 int option_max_undo = 32768;
89 int option_persistent_selections = 1;
90 int option_cursor_beyond_eol = 0;
91 int option_line_state = 0;
92 int option_line_state_width = 0;
94 int option_edit_right_extreme = 0;
95 int option_edit_left_extreme = 0;
96 int option_edit_top_extreme = 0;
97 int option_edit_bottom_extreme = 0;
98 int enable_show_tabs_tws = 1;
99 int option_check_nl_at_eof = 0;
100 int option_group_undo = 0;
101 int show_right_margin = 0;
103 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
104 char *option_backup_ext = NULL;
106 unsigned int edit_stack_iterator = 0;
107 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
111 /*** file scope macro definitions ****************************************************************/
113 #define TEMP_BUF_LEN 1024
115 #define space_width 1
117 /*** file scope type declarations ****************************************************************/
119 /*** file scope variables ************************************************************************/
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
127 const char *read, *write, *extension;
128 } all_filters[] =
130 /* *INDENT-OFF* */
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
133 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 /* *INDENT-ON* */
139 static 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 = FALSE;
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 /* --------------------------------------------------------------------------------------------- */
726 /** check whether cursor is in indent part of line
728 * @param edit editor object
730 * @return TRUE if cursor is in indent, FALSE otherwise
733 static gboolean
734 is_in_indent (WEdit * edit)
736 long p;
738 for (p = edit_bol (edit, edit->curs1); p < edit->curs1; p++)
739 if (strchr (" \t", edit_get_byte (edit, p)) == NULL)
740 return FALSE;
742 return TRUE;
745 /* --------------------------------------------------------------------------------------------- */
746 /** check whether line in editor is blank or not
748 * @param edit editor object
749 * @param offset position in file
751 * @return TRUE if line in blank, FALSE otherwise
754 static gboolean
755 is_blank (WEdit * edit, long offset)
757 long s, f;
758 int c;
760 s = edit_bol (edit, offset);
761 f = edit_eol (edit, offset) - 1;
762 while (s <= f)
764 c = edit_get_byte (edit, s++);
765 if (!isspace (c))
766 return FALSE;
768 return TRUE;
771 /* --------------------------------------------------------------------------------------------- */
772 /** returns the offset of line i */
774 static long
775 edit_find_line (WEdit * edit, int line)
777 int i, j = 0;
778 int m = 2000000000;
779 if (!edit->caches_valid)
781 for (i = 0; i < N_LINE_CACHES; i++)
782 edit->line_numbers[i] = edit->line_offsets[i] = 0;
783 /* three offsets that we *know* are line 0 at 0 and these two: */
784 edit->line_numbers[1] = edit->curs_line;
785 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
786 edit->line_numbers[2] = edit->total_lines;
787 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
788 edit->caches_valid = TRUE;
790 if (line >= edit->total_lines)
791 return edit->line_offsets[2];
792 if (line <= 0)
793 return 0;
794 /* find the closest known point */
795 for (i = 0; i < N_LINE_CACHES; i++)
797 int n;
798 n = abs (edit->line_numbers[i] - line);
799 if (n < m)
801 m = n;
802 j = i;
805 if (m == 0)
806 return edit->line_offsets[j]; /* know the offset exactly */
807 if (m == 1 && j >= 3)
808 i = j; /* one line different - caller might be looping, so stay in this cache */
809 else
810 i = 3 + (rand () % (N_LINE_CACHES - 3));
811 if (line > edit->line_numbers[j])
812 edit->line_offsets[i] =
813 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
814 else
815 edit->line_offsets[i] =
816 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
817 edit->line_numbers[i] = line;
818 return edit->line_offsets[i];
821 /* --------------------------------------------------------------------------------------------- */
822 /** moves up until a blank line is reached, or until just
823 before a non-blank line is reached */
825 static void
826 edit_move_up_paragraph (WEdit * edit, int do_scroll)
828 int i = 0;
829 if (edit->curs_line > 1)
831 if (line_is_blank (edit, edit->curs_line))
833 if (line_is_blank (edit, edit->curs_line - 1))
835 for (i = edit->curs_line - 1; i; i--)
836 if (!line_is_blank (edit, i))
838 i++;
839 break;
842 else
844 for (i = edit->curs_line - 1; i; i--)
845 if (line_is_blank (edit, i))
846 break;
849 else
851 for (i = edit->curs_line - 1; i; i--)
852 if (line_is_blank (edit, i))
853 break;
856 edit_move_up (edit, edit->curs_line - i, do_scroll);
859 /* --------------------------------------------------------------------------------------------- */
860 /** moves down until a blank line is reached, or until just
861 before a non-blank line is reached */
863 static void
864 edit_move_down_paragraph (WEdit * edit, int do_scroll)
866 int i;
867 if (edit->curs_line >= edit->total_lines - 1)
869 i = edit->total_lines;
871 else
873 if (line_is_blank (edit, edit->curs_line))
875 if (line_is_blank (edit, edit->curs_line + 1))
877 for (i = edit->curs_line + 1; i; i++)
878 if (!line_is_blank (edit, i) || i > edit->total_lines)
880 i--;
881 break;
884 else
886 for (i = edit->curs_line + 1; i; i++)
887 if (line_is_blank (edit, i) || i >= edit->total_lines)
888 break;
891 else
893 for (i = edit->curs_line + 1; i; i++)
894 if (line_is_blank (edit, i) || i >= edit->total_lines)
895 break;
898 edit_move_down (edit, i - edit->curs_line, do_scroll);
901 /* --------------------------------------------------------------------------------------------- */
903 static void
904 edit_begin_page (WEdit * edit)
906 edit_update_curs_row (edit);
907 edit_move_up (edit, edit->curs_row, 0);
910 /* --------------------------------------------------------------------------------------------- */
912 static void
913 edit_end_page (WEdit * edit)
915 edit_update_curs_row (edit);
916 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
920 /* --------------------------------------------------------------------------------------------- */
921 /** goto beginning of text */
923 static void
924 edit_move_to_top (WEdit * edit)
926 if (edit->curs_line)
928 edit_cursor_move (edit, -edit->curs1);
929 edit_move_to_prev_col (edit, 0);
930 edit->force |= REDRAW_PAGE;
931 edit->search_start = 0;
932 edit_update_curs_row (edit);
937 /* --------------------------------------------------------------------------------------------- */
938 /** goto end of text */
940 static void
941 edit_move_to_bottom (WEdit * edit)
943 if (edit->curs_line < edit->total_lines)
945 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
946 edit->start_display = edit->last_byte;
947 edit->start_line = edit->total_lines;
948 edit_scroll_upward (edit, edit->widget.lines - 1);
949 edit->force |= REDRAW_PAGE;
953 /* --------------------------------------------------------------------------------------------- */
954 /** goto beginning of line */
956 static void
957 edit_cursor_to_bol (WEdit * edit)
959 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
960 edit->search_start = edit->curs1;
961 edit->prev_col = edit_get_col (edit);
962 edit->over_col = 0;
965 /* --------------------------------------------------------------------------------------------- */
966 /** goto end of line */
968 static void
969 edit_cursor_to_eol (WEdit * edit)
971 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
972 edit->search_start = edit->curs1;
973 edit->prev_col = edit_get_col (edit);
974 edit->over_col = 0;
977 /* --------------------------------------------------------------------------------------------- */
979 static unsigned long
980 my_type_of (int c)
982 int x, r = 0;
983 const char *p, *q;
984 const char option_chars_move_whole_word[] =
985 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
987 if (!c)
988 return 0;
989 if (c == '!')
991 if (*option_chars_move_whole_word == '!')
992 return 2;
993 return 0x80000000UL;
995 if (g_ascii_isupper ((gchar) c))
996 c = 'A';
997 else if (g_ascii_islower ((gchar) c))
998 c = 'a';
999 else if (g_ascii_isalpha (c))
1000 c = 'a';
1001 else if (isdigit (c))
1002 c = '0';
1003 else if (isspace (c))
1004 c = ' ';
1005 q = strchr (option_chars_move_whole_word, c);
1006 if (!q)
1007 return 0xFFFFFFFFUL;
1010 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1011 if (*p == '!')
1012 x <<= 1;
1013 r |= x;
1015 while ((q = strchr (q + 1, c)));
1016 return r;
1019 /* --------------------------------------------------------------------------------------------- */
1021 static void
1022 edit_left_word_move (WEdit * edit, int s)
1024 for (;;)
1026 int c1, c2;
1027 if (edit->column_highlight
1028 && edit->mark1 != edit->mark2
1029 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1030 break;
1031 edit_cursor_move (edit, -1);
1032 if (!edit->curs1)
1033 break;
1034 c1 = edit_get_byte (edit, edit->curs1 - 1);
1035 c2 = edit_get_byte (edit, edit->curs1);
1036 if (c1 == '\n' || c2 == '\n')
1037 break;
1038 if (!(my_type_of (c1) & my_type_of (c2)))
1039 break;
1040 if (isspace (c1) && !isspace (c2))
1041 break;
1042 if (s)
1043 if (!isspace (c1) && isspace (c2))
1044 break;
1048 /* --------------------------------------------------------------------------------------------- */
1050 static void
1051 edit_left_word_move_cmd (WEdit * edit)
1053 edit_left_word_move (edit, 0);
1054 edit->force |= REDRAW_PAGE;
1057 /* --------------------------------------------------------------------------------------------- */
1059 static void
1060 edit_right_word_move (WEdit * edit, int s)
1062 for (;;)
1064 int c1, c2;
1065 if (edit->column_highlight
1066 && edit->mark1 != edit->mark2
1067 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1068 break;
1069 edit_cursor_move (edit, 1);
1070 if (edit->curs1 >= edit->last_byte)
1071 break;
1072 c1 = edit_get_byte (edit, edit->curs1 - 1);
1073 c2 = edit_get_byte (edit, edit->curs1);
1074 if (c1 == '\n' || c2 == '\n')
1075 break;
1076 if (!(my_type_of (c1) & my_type_of (c2)))
1077 break;
1078 if (isspace (c1) && !isspace (c2))
1079 break;
1080 if (s)
1081 if (!isspace (c1) && isspace (c2))
1082 break;
1086 /* --------------------------------------------------------------------------------------------- */
1088 static void
1089 edit_right_word_move_cmd (WEdit * edit)
1091 edit_right_word_move (edit, 0);
1092 edit->force |= REDRAW_PAGE;
1095 /* --------------------------------------------------------------------------------------------- */
1097 static void
1098 edit_right_char_move_cmd (WEdit * edit)
1100 int cw = 1;
1101 int c = 0;
1102 if (edit->utf8)
1104 c = edit_get_utf (edit, edit->curs1, &cw);
1105 if (cw < 1)
1106 cw = 1;
1108 else
1110 c = edit_get_byte (edit, edit->curs1);
1112 if (option_cursor_beyond_eol && c == '\n')
1114 edit->over_col++;
1116 else
1118 edit_cursor_move (edit, cw);
1122 /* --------------------------------------------------------------------------------------------- */
1124 static void
1125 edit_left_char_move_cmd (WEdit * edit)
1127 int cw = 1;
1128 if (edit->column_highlight
1129 && option_cursor_beyond_eol
1130 && edit->mark1 != edit->mark2
1131 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1132 return;
1133 if (edit->utf8)
1135 edit_get_prev_utf (edit, edit->curs1, &cw);
1136 if (cw < 1)
1137 cw = 1;
1139 if (option_cursor_beyond_eol && edit->over_col > 0)
1141 edit->over_col--;
1143 else
1145 edit_cursor_move (edit, -cw);
1149 /* --------------------------------------------------------------------------------------------- */
1150 /** Up or down cursor moving.
1151 direction = TRUE - move up
1152 = FALSE - move down
1155 static void
1156 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1158 unsigned long p;
1159 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1161 if (i > l)
1162 i = l;
1164 if (i == 0)
1165 return;
1167 if (i > 1)
1168 edit->force |= REDRAW_PAGE;
1169 if (do_scroll)
1171 if (direction)
1172 edit_scroll_upward (edit, i);
1173 else
1174 edit_scroll_downward (edit, i);
1176 p = edit_bol (edit, edit->curs1);
1178 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1180 edit_cursor_move (edit, p - edit->curs1);
1182 edit_move_to_prev_col (edit, p);
1184 /* search start of current multibyte char (like CJK) */
1185 if (edit->curs1 + 1 < edit->last_byte)
1187 edit_right_char_move_cmd (edit);
1188 edit_left_char_move_cmd (edit);
1191 edit->search_start = edit->curs1;
1192 edit->found_len = 0;
1195 /* --------------------------------------------------------------------------------------------- */
1197 static void
1198 edit_right_delete_word (WEdit * edit)
1200 int c1, c2;
1201 for (;;)
1203 if (edit->curs1 >= edit->last_byte)
1204 break;
1205 c1 = edit_delete (edit, 1);
1206 c2 = edit_get_byte (edit, edit->curs1);
1207 if (c1 == '\n' || c2 == '\n')
1208 break;
1209 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1210 break;
1211 if (!(my_type_of (c1) & my_type_of (c2)))
1212 break;
1216 /* --------------------------------------------------------------------------------------------- */
1218 static void
1219 edit_left_delete_word (WEdit * edit)
1221 int c1, c2;
1222 for (;;)
1224 if (edit->curs1 <= 0)
1225 break;
1226 c1 = edit_backspace (edit, 1);
1227 c2 = edit_get_byte (edit, edit->curs1 - 1);
1228 if (c1 == '\n' || c2 == '\n')
1229 break;
1230 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1231 break;
1232 if (!(my_type_of (c1) & my_type_of (c2)))
1233 break;
1237 /* --------------------------------------------------------------------------------------------- */
1239 the start column position is not recorded, and hence does not
1240 undo as it happed. But who would notice.
1243 static void
1244 edit_do_undo (WEdit * edit)
1246 long ac;
1247 long count = 0;
1249 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1250 edit->over_col = 0;
1251 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1253 switch ((int) ac)
1255 case STACK_BOTTOM:
1256 goto done_undo;
1257 case CURS_RIGHT:
1258 edit_cursor_move (edit, 1);
1259 break;
1260 case CURS_LEFT:
1261 edit_cursor_move (edit, -1);
1262 break;
1263 case BACKSPACE:
1264 case BACKSPACE_BR:
1265 edit_backspace (edit, 1);
1266 break;
1267 case DELCHAR:
1268 case DELCHAR_BR:
1269 edit_delete (edit, 1);
1270 break;
1271 case COLUMN_ON:
1272 edit->column_highlight = 1;
1273 break;
1274 case COLUMN_OFF:
1275 edit->column_highlight = 0;
1276 break;
1278 if (ac >= 256 && ac < 512)
1279 edit_insert_ahead (edit, ac - 256);
1280 if (ac >= 0 && ac < 256)
1281 edit_insert (edit, ac);
1283 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1285 edit->mark1 = ac - MARK_1;
1286 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1288 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1290 edit->mark2 = ac - MARK_2;
1291 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1293 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1295 edit->end_mark_curs = ac - MARK_CURS;
1297 if (count++)
1298 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1301 if (edit->start_display > ac - KEY_PRESS)
1303 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1304 edit->force |= REDRAW_PAGE;
1306 else if (edit->start_display < ac - KEY_PRESS)
1308 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1309 edit->force |= REDRAW_PAGE;
1311 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1312 edit_update_curs_row (edit);
1314 done_undo:;
1315 edit->undo_stack_disable = 0;
1318 static void
1319 edit_do_redo (WEdit * edit)
1321 long ac;
1322 long count = 0;
1324 if (edit->redo_stack_reset)
1325 return;
1327 edit->over_col = 0;
1328 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1330 switch ((int) ac)
1332 case STACK_BOTTOM:
1333 goto done_redo;
1334 case CURS_RIGHT:
1335 edit_cursor_move (edit, 1);
1336 break;
1337 case CURS_LEFT:
1338 edit_cursor_move (edit, -1);
1339 break;
1340 case BACKSPACE:
1341 edit_backspace (edit, 1);
1342 break;
1343 case DELCHAR:
1344 edit_delete (edit, 1);
1345 break;
1346 case COLUMN_ON:
1347 edit->column_highlight = 1;
1348 break;
1349 case COLUMN_OFF:
1350 edit->column_highlight = 0;
1351 break;
1353 if (ac >= 256 && ac < 512)
1354 edit_insert_ahead (edit, ac - 256);
1355 if (ac >= 0 && ac < 256)
1356 edit_insert (edit, ac);
1358 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1360 edit->mark1 = ac - MARK_1;
1361 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1363 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1365 edit->mark2 = ac - MARK_2;
1366 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1368 /* more than one pop usually means something big */
1369 if (count++)
1370 edit->force |= REDRAW_PAGE;
1373 if (edit->start_display > ac - KEY_PRESS)
1375 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1376 edit->force |= REDRAW_PAGE;
1378 else if (edit->start_display < ac - KEY_PRESS)
1380 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1381 edit->force |= REDRAW_PAGE;
1383 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1384 edit_update_curs_row (edit);
1386 done_redo:;
1389 static void
1390 edit_group_undo (WEdit * edit)
1392 long ac = KEY_PRESS;
1393 long cur_ac = KEY_PRESS;
1394 while (ac != STACK_BOTTOM && ac == cur_ac)
1396 cur_ac = get_prev_undo_action (edit);
1397 edit_do_undo (edit);
1398 ac = get_prev_undo_action (edit);
1399 /* exit from cycle if option_group_undo is not set,
1400 * and make single UNDO operation
1402 if (!option_group_undo)
1403 ac = STACK_BOTTOM;
1407 /* --------------------------------------------------------------------------------------------- */
1409 static void
1410 edit_delete_to_line_end (WEdit * edit)
1412 while (edit_get_byte (edit, edit->curs1) != '\n')
1414 if (!edit->curs2)
1415 break;
1416 edit_delete (edit, 1);
1420 /* --------------------------------------------------------------------------------------------- */
1422 static void
1423 edit_delete_to_line_begin (WEdit * edit)
1425 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1427 if (!edit->curs1)
1428 break;
1429 edit_backspace (edit, 1);
1433 /* --------------------------------------------------------------------------------------------- */
1435 static int
1436 is_aligned_on_a_tab (WEdit * edit)
1438 edit_update_curs_col (edit);
1439 return !((edit->curs_col % (TAB_SIZE * space_width))
1440 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1443 /* --------------------------------------------------------------------------------------------- */
1445 static int
1446 right_of_four_spaces (WEdit * edit)
1448 int i, ch = 0;
1449 for (i = 1; i <= HALF_TAB_SIZE; i++)
1450 ch |= edit_get_byte (edit, edit->curs1 - i);
1451 if (ch == ' ')
1452 return is_aligned_on_a_tab (edit);
1453 return 0;
1456 /* --------------------------------------------------------------------------------------------- */
1458 static int
1459 left_of_four_spaces (WEdit * edit)
1461 int i, ch = 0;
1462 for (i = 0; i < HALF_TAB_SIZE; i++)
1463 ch |= edit_get_byte (edit, edit->curs1 + i);
1464 if (ch == ' ')
1465 return is_aligned_on_a_tab (edit);
1466 return 0;
1469 /* --------------------------------------------------------------------------------------------- */
1471 static void
1472 edit_auto_indent (WEdit * edit)
1474 long p;
1475 char c;
1476 p = edit->curs1;
1477 /* use the previous line as a template */
1478 p = edit_move_backward (edit, p, 1);
1479 /* copy the leading whitespace of the line */
1480 for (;;)
1481 { /* no range check - the line _is_ \n-terminated */
1482 c = edit_get_byte (edit, p++);
1483 if (c != ' ' && c != '\t')
1484 break;
1485 edit_insert (edit, c);
1489 /* --------------------------------------------------------------------------------------------- */
1491 static inline void
1492 edit_double_newline (WEdit * edit)
1494 edit_insert (edit, '\n');
1495 if (edit_get_byte (edit, edit->curs1) == '\n')
1496 return;
1497 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1498 return;
1499 edit->force |= REDRAW_PAGE;
1500 edit_insert (edit, '\n');
1504 /* --------------------------------------------------------------------------------------------- */
1506 static void
1507 insert_spaces_tab (WEdit * edit, gboolean half)
1509 int i;
1511 edit_update_curs_col (edit);
1512 i = option_tab_spacing * space_width;
1513 if (half)
1514 i /= 2;
1515 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1516 while (i > 0)
1518 edit_insert (edit, ' ');
1519 i -= space_width;
1523 /* --------------------------------------------------------------------------------------------- */
1525 static inline void
1526 edit_tab_cmd (WEdit * edit)
1528 int i;
1530 if (option_fake_half_tabs)
1532 if (is_in_indent (edit))
1534 /*insert a half tab (usually four spaces) unless there is a
1535 half tab already behind, then delete it and insert a
1536 full tab. */
1537 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1538 insert_spaces_tab (edit, TRUE);
1539 else
1541 for (i = 1; i <= HALF_TAB_SIZE; i++)
1542 edit_backspace (edit, 1);
1543 edit_insert (edit, '\t');
1545 return;
1548 if (option_fill_tabs_with_spaces)
1549 insert_spaces_tab (edit, FALSE);
1550 else
1551 edit_insert (edit, '\t');
1554 /* --------------------------------------------------------------------------------------------- */
1556 static void
1557 check_and_wrap_line (WEdit * edit)
1559 int curs, c;
1560 if (!option_typewriter_wrap)
1561 return;
1562 edit_update_curs_col (edit);
1563 if (edit->curs_col < option_word_wrap_line_length)
1564 return;
1565 curs = edit->curs1;
1566 for (;;)
1568 curs--;
1569 c = edit_get_byte (edit, curs);
1570 if (c == '\n' || curs <= 0)
1572 edit_insert (edit, '\n');
1573 return;
1575 if (c == ' ' || c == '\t')
1577 int current = edit->curs1;
1578 edit_cursor_move (edit, curs - edit->curs1 + 1);
1579 edit_insert (edit, '\n');
1580 edit_cursor_move (edit, current - edit->curs1 + 1);
1581 return;
1586 /* --------------------------------------------------------------------------------------------- */
1587 /** this find the matching bracket in either direction, and sets edit->bracket */
1589 static long
1590 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1592 const char *const b = "{}{[][()(", *p;
1593 int i = 1, a, inc = -1, c, d, n = 0;
1594 unsigned long j = 0;
1595 long q;
1596 edit_update_curs_row (edit);
1597 c = edit_get_byte (edit, edit->curs1);
1598 p = strchr (b, c);
1599 /* no limit */
1600 if (!furthest_bracket_search)
1601 furthest_bracket_search--;
1602 /* not on a bracket at all */
1603 if (!p)
1604 return -1;
1605 /* the matching bracket */
1606 d = p[1];
1607 /* going left or right? */
1608 if (strchr ("{[(", c))
1609 inc = 1;
1610 for (q = edit->curs1 + inc;; q += inc)
1612 /* out of buffer? */
1613 if (q >= edit->last_byte || q < 0)
1614 break;
1615 a = edit_get_byte (edit, q);
1616 /* don't want to eat CPU */
1617 if (j++ > furthest_bracket_search)
1618 break;
1619 /* out of screen? */
1620 if (in_screen)
1622 if (q < edit->start_display)
1623 break;
1624 /* count lines if searching downward */
1625 if (inc > 0 && a == '\n')
1626 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1627 break;
1629 /* count bracket depth */
1630 i += (a == c) - (a == d);
1631 /* return if bracket depth is zero */
1632 if (!i)
1633 return q;
1635 /* no match */
1636 return -1;
1639 /* --------------------------------------------------------------------------------------------- */
1641 static inline void
1642 edit_goto_matching_bracket (WEdit * edit)
1644 long q;
1646 q = edit_get_bracket (edit, 0, 0);
1647 if (q >= 0)
1649 edit->bracket = edit->curs1;
1650 edit->force |= REDRAW_PAGE;
1651 edit_cursor_move (edit, q - edit->curs1);
1655 /* --------------------------------------------------------------------------------------------- */
1657 static void
1658 edit_move_block_to_right (WEdit * edit)
1660 long start_mark, end_mark;
1661 long cur_bol, start_bol;
1663 if (eval_marks (edit, &start_mark, &end_mark))
1664 return;
1666 start_bol = edit_bol (edit, start_mark);
1667 cur_bol = edit_bol (edit, end_mark - 1);
1671 edit_cursor_move (edit, cur_bol - edit->curs1);
1672 if (option_fill_tabs_with_spaces)
1673 insert_spaces_tab (edit, option_fake_half_tabs);
1674 else
1675 edit_insert (edit, '\t');
1676 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1678 if (cur_bol == 0)
1679 break;
1681 cur_bol = edit_bol (edit, cur_bol - 1);
1683 while (cur_bol >= start_bol);
1685 edit->force |= REDRAW_PAGE;
1688 /* --------------------------------------------------------------------------------------------- */
1690 static void
1691 edit_move_block_to_left (WEdit * edit)
1693 long start_mark, end_mark;
1694 long cur_bol, start_bol;
1695 int i;
1697 if (eval_marks (edit, &start_mark, &end_mark))
1698 return;
1700 start_bol = edit_bol (edit, start_mark);
1701 cur_bol = edit_bol (edit, end_mark - 1);
1705 int del_tab_width;
1706 int next_char;
1708 edit_cursor_move (edit, cur_bol - edit->curs1);
1710 if (option_fake_half_tabs)
1711 del_tab_width = HALF_TAB_SIZE;
1712 else
1713 del_tab_width = option_tab_spacing;
1715 next_char = edit_get_byte (edit, edit->curs1);
1716 if (next_char == '\t')
1717 edit_delete (edit, 1);
1718 else if (next_char == ' ')
1719 for (i = 1; i <= del_tab_width; i++)
1721 if (next_char == ' ')
1722 edit_delete (edit, 1);
1723 next_char = edit_get_byte (edit, edit->curs1);
1726 if (cur_bol == 0)
1727 break;
1729 cur_bol = edit_bol (edit, cur_bol - 1);
1731 while (cur_bol >= start_bol);
1733 edit->force |= REDRAW_PAGE;
1736 /* --------------------------------------------------------------------------------------------- */
1738 * prints at the cursor
1739 * @returns the number of chars printed
1742 static size_t
1743 edit_print_string (WEdit * e, const char *s)
1745 size_t i = 0;
1747 while (s[i] != '\0')
1748 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1749 e->force |= REDRAW_COMPLETELY;
1750 edit_update_screen (e);
1751 return i;
1754 /* --------------------------------------------------------------------------------------------- */
1755 /*** public functions ****************************************************************************/
1756 /* --------------------------------------------------------------------------------------------- */
1758 /** User edit menu, like user menu (F2) but only in editor. */
1760 void
1761 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1763 char *block_file;
1764 int nomark;
1765 long curs;
1766 long start_mark, end_mark;
1767 struct stat status;
1768 vfs_path_t *block_file_vpath;
1770 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1771 block_file_vpath = vfs_path_from_str (block_file);
1772 curs = edit->curs1;
1773 nomark = eval_marks (edit, &start_mark, &end_mark);
1774 if (nomark == 0)
1775 edit_save_block (edit, block_file, start_mark, end_mark);
1777 /* run shell scripts from menu */
1778 if (user_menu_cmd (edit, menu_file, selected_entry)
1779 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1781 int rc = 0;
1782 FILE *fd;
1784 /* i.e. we have marked block */
1785 if (nomark == 0)
1786 rc = edit_block_delete_cmd (edit);
1788 if (rc == 0)
1790 long ins_len;
1792 ins_len = edit_insert_file (edit, block_file_vpath);
1793 if (nomark == 0 && ins_len > 0)
1794 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1796 /* truncate block file */
1797 fd = fopen (block_file, "w");
1798 if (fd != NULL)
1799 fclose (fd);
1801 g_free (block_file);
1802 vfs_path_free (block_file_vpath);
1804 edit_cursor_move (edit, curs - edit->curs1);
1805 edit->force |= REDRAW_PAGE;
1806 send_message ((Widget *) edit, WIDGET_DRAW, 0);
1809 /* --------------------------------------------------------------------------------------------- */
1812 edit_get_byte (WEdit * edit, long byte_index)
1814 unsigned long p;
1815 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1816 return '\n';
1818 if (byte_index >= edit->curs1)
1820 p = edit->curs1 + edit->curs2 - byte_index - 1;
1821 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1823 else
1825 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1829 /* --------------------------------------------------------------------------------------------- */
1832 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1834 gchar *str = NULL;
1835 int res = -1;
1836 gunichar ch;
1837 gchar *next_ch = NULL;
1838 int width = 0;
1839 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1841 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1843 *char_width = 0;
1844 return '\n';
1847 str = edit_get_byte_ptr (edit, byte_index);
1849 if (str == NULL)
1851 *char_width = 0;
1852 return 0;
1855 res = g_utf8_get_char_validated (str, -1);
1857 if (res < 0)
1859 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1860 int i;
1861 for (i = 0; i < UTF8_CHAR_LEN; i++)
1862 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1863 utf8_buf[UTF8_CHAR_LEN] = '\0';
1864 str = utf8_buf;
1865 res = g_utf8_get_char_validated (str, -1);
1868 if (res < 0)
1870 ch = *str;
1871 width = 0;
1873 else
1875 ch = res;
1876 /* Calculate UTF-8 char width */
1877 next_ch = g_utf8_next_char (str);
1878 if (next_ch)
1880 width = next_ch - str;
1882 else
1884 ch = 0;
1885 width = 0;
1888 *char_width = width;
1889 return ch;
1892 /* --------------------------------------------------------------------------------------------- */
1894 char *
1895 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1897 int i;
1898 char *p, *writename;
1899 const vfs_path_element_t *path_element;
1901 i = edit_find_filter (filename_vpath);
1902 if (i < 0)
1903 return NULL;
1905 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1906 writename = name_quote (path_element->path, 0);
1907 p = g_strdup_printf (all_filters[i].write, writename);
1908 g_free (writename);
1909 return p;
1912 /* --------------------------------------------------------------------------------------------- */
1914 long
1915 edit_write_stream (WEdit * edit, FILE * f)
1917 long i;
1919 if (edit->lb == LB_ASIS)
1921 for (i = 0; i < edit->last_byte; i++)
1922 if (fputc (edit_get_byte (edit, i), f) < 0)
1923 break;
1924 return i;
1927 /* change line breaks */
1928 for (i = 0; i < edit->last_byte; i++)
1930 unsigned char c = edit_get_byte (edit, i);
1932 if (!(c == '\n' || c == '\r'))
1934 /* not line break */
1935 if (fputc (c, f) < 0)
1936 return i;
1938 else
1939 { /* (c == '\n' || c == '\r') */
1940 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1942 switch (edit->lb)
1944 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1945 /* put one line break unconditionally */
1946 if (fputc ('\n', f) < 0)
1947 return i;
1949 i++; /* 2 chars are processed */
1951 if (c == '\r' && c1 == '\n')
1952 /* Windows line break; go to the next char */
1953 break;
1955 if (c == '\r' && c1 == '\r')
1957 /* two Macintosh line breaks; put second line break */
1958 if (fputc ('\n', f) < 0)
1959 return i;
1960 break;
1963 if (fputc (c1, f) < 0)
1964 return i;
1965 break;
1967 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1968 /* put one line break unconditionally */
1969 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1970 return i;
1972 if (c == '\r' && c1 == '\n')
1973 /* Windows line break; go to the next char */
1974 i++;
1975 break;
1977 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1978 /* put one line break unconditionally */
1979 if (fputc ('\r', f) < 0)
1980 return i;
1982 i++; /* 2 chars are processed */
1984 if (c == '\r' && c1 == '\n')
1985 /* Windows line break; go to the next char */
1986 break;
1988 if (c == '\n' && c1 == '\n')
1990 /* two Windows line breaks; put second line break */
1991 if (fputc ('\r', f) < 0)
1992 return i;
1993 break;
1996 if (fputc (c1, f) < 0)
1997 return i;
1998 break;
1999 case LB_ASIS: /* default without changes */
2000 break;
2005 return edit->last_byte;
2008 /* --------------------------------------------------------------------------------------------- */
2010 gboolean
2011 is_break_char (char c)
2013 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2016 /* --------------------------------------------------------------------------------------------- */
2018 char *
2019 edit_get_word_from_pos (WEdit * edit, long start_pos, long *start, gsize * len, gsize * cut)
2021 long word_start;
2022 long cut_len = 0;
2023 GString *match_expr;
2024 unsigned char *bufpos;
2025 int c1, c2;
2027 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2029 c1 = edit_get_byte (edit, word_start);
2030 c2 = edit_get_byte (edit, word_start - 1);
2032 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2033 break;
2036 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2037 match_expr = g_string_sized_new (16);
2041 c1 = edit_get_byte (edit, word_start + match_expr->len);
2042 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2043 g_string_append_c (match_expr, c1);
2045 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2047 *len = match_expr->len;
2048 *start = word_start;
2049 *cut = cut_len;
2051 return g_string_free (match_expr, FALSE);
2054 /* --------------------------------------------------------------------------------------------- */
2055 /** inserts a file at the cursor, returns count of inserted bytes on success */
2057 long
2058 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2060 char *p;
2061 long ins_len = 0;
2063 p = edit_get_filter (filename_vpath);
2064 if (p != NULL)
2066 FILE *f;
2067 long current = edit->curs1;
2069 f = (FILE *) popen (p, "r");
2070 if (f != NULL)
2072 edit_insert_stream (edit, f);
2073 ins_len = edit->curs1 - current;
2074 edit_cursor_move (edit, -ins_len);
2075 if (pclose (f) > 0)
2077 char *errmsg;
2079 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2080 edit_error_dialog (_("Error"), errmsg);
2081 g_free (errmsg);
2082 ins_len = -1;
2085 else
2087 char *errmsg;
2089 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2090 edit_error_dialog (_("Error"), errmsg);
2091 g_free (errmsg);
2092 ins_len = -1;
2094 g_free (p);
2096 else
2098 int i, file, blocklen;
2099 long current = edit->curs1;
2100 int vertical_insertion = 0;
2101 char *buf;
2103 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2104 if (file == -1)
2105 return -1;
2107 buf = g_malloc0 (TEMP_BUF_LEN);
2108 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2109 if (blocklen > 0)
2111 /* if contain signature VERTICAL_MAGIC then it vertical block */
2112 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2113 vertical_insertion = 1;
2114 else
2115 mc_lseek (file, 0, SEEK_SET);
2118 if (vertical_insertion)
2120 long mark1, mark2;
2121 int c1, c2;
2123 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2124 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2125 /* highlight inserted text then not persistent blocks */
2126 if (!option_persistent_selections)
2128 if (!edit->column_highlight)
2129 edit_push_undo_action (edit, COLUMN_OFF);
2130 edit->column_highlight = 1;
2133 else
2135 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2137 for (i = 0; i < blocklen; i++)
2138 edit_insert (edit, buf[i]);
2140 /* highlight inserted text then not persistent blocks */
2141 if (!option_persistent_selections && edit->modified)
2143 edit_set_markers (edit, edit->curs1, current, 0, 0);
2144 if (edit->column_highlight)
2145 edit_push_undo_action (edit, COLUMN_ON);
2146 edit->column_highlight = 0;
2150 edit->force |= REDRAW_PAGE;
2151 ins_len = edit->curs1 - current;
2152 edit_cursor_move (edit, -ins_len);
2153 g_free (buf);
2154 mc_close (file);
2155 if (blocklen != 0)
2156 ins_len = 0;
2159 return ins_len;
2162 /* --------------------------------------------------------------------------------------------- */
2164 * Fill in the edit structure. Return NULL on failure. Pass edit as
2165 * NULL to allocate a new structure.
2167 * If line is 0, try to restore saved position. Otherwise put the
2168 * cursor on that line and show it in the middle of the screen.
2171 WEdit *
2172 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2173 long line)
2175 gboolean to_free = FALSE;
2177 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2178 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2180 if (edit != NULL)
2181 edit_purge_widget (edit);
2182 else
2184 #ifdef ENABLE_NLS
2186 * Expand option_whole_chars_search by national letters using
2187 * current locale
2190 static char option_whole_chars_search_buf[256];
2192 if (option_whole_chars_search_buf != option_whole_chars_search)
2194 size_t i;
2195 size_t len = str_term_width1 (option_whole_chars_search);
2197 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2199 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2201 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2203 option_whole_chars_search_buf[len++] = i;
2207 option_whole_chars_search_buf[len] = 0;
2208 option_whole_chars_search = option_whole_chars_search_buf;
2210 #endif /* ENABLE_NLS */
2211 edit = g_malloc0 (sizeof (WEdit));
2212 to_free = TRUE;
2215 edit->drag_state = MCEDIT_DRAG_NORMAL;
2216 edit->widget.y = y;
2217 edit->widget.x = x;
2218 edit->widget.lines = lines;
2219 edit->widget.cols = cols;
2220 edit_save_size (edit);
2221 edit->fullscreen = TRUE;
2223 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2224 edit->stat1.st_uid = getuid ();
2225 edit->stat1.st_gid = getgid ();
2226 edit->stat1.st_mtime = 0;
2228 edit->over_col = 0;
2229 edit->bracket = -1;
2230 edit->force |= REDRAW_PAGE;
2232 /* set file name before load file */
2233 edit_set_filename (edit, filename_vpath);
2235 edit->undo_stack_size = START_STACK_SIZE;
2236 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2237 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2239 edit->redo_stack_size = START_STACK_SIZE;
2240 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2241 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2243 edit->utf8 = 0;
2244 edit->converter = str_cnv_from_term;
2245 edit_set_codeset (edit);
2247 if (!edit_load_file (edit))
2249 /* edit_load_file already gives an error message */
2250 if (to_free)
2251 g_free (edit);
2252 return NULL;
2255 edit->loading_done = 1;
2256 edit->modified = 0;
2257 edit->locked = 0;
2258 edit_load_syntax (edit, NULL, NULL);
2260 int color;
2261 edit_get_syntax_color (edit, -1, &color);
2264 /* load saved cursor position */
2265 if ((line == 0) && option_save_position)
2266 edit_load_position (edit);
2267 else
2269 if (line <= 0)
2270 line = 1;
2271 edit_move_display (edit, line - 1);
2272 edit_move_to_line (edit, line - 1);
2275 edit_load_macro_cmd (edit);
2277 return edit;
2280 /* --------------------------------------------------------------------------------------------- */
2282 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2283 gboolean
2284 edit_clean (WEdit * edit)
2286 int j = 0;
2288 if (edit == NULL)
2289 return FALSE;
2291 /* a stale lock, remove it */
2292 if (edit->locked)
2293 edit->locked = unlock_file (edit->filename_vpath);
2295 /* save cursor position */
2296 if (option_save_position)
2297 edit_save_position (edit);
2298 else if (edit->serialized_bookmarks != NULL)
2299 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2301 /* File specified on the mcedit command line and never saved */
2302 if (edit->delete_file)
2303 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2305 edit_free_syntax_rules (edit);
2306 book_mark_flush (edit, -1);
2307 for (; j <= MAXBUFF; j++)
2309 g_free (edit->buffers1[j]);
2310 g_free (edit->buffers2[j]);
2313 g_free (edit->undo_stack);
2314 g_free (edit->redo_stack);
2315 vfs_path_free (edit->filename_vpath);
2316 vfs_path_free (edit->dir_vpath);
2317 mc_search_free (edit->search);
2318 edit->search = NULL;
2320 if (edit->converter != str_cnv_from_term)
2321 str_close_conv (edit->converter);
2323 edit_purge_widget (edit);
2325 return TRUE;
2328 /* --------------------------------------------------------------------------------------------- */
2331 * Load a new file into the editor and set line. If it fails, preserve the old file.
2332 * To do it, allocate a new widget, initialize it and, if the new file
2333 * was loaded, copy the data to the old widget.
2335 * @returns TRUE on success, FALSE on failure.
2337 gboolean
2338 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2340 WEdit *e;
2341 int y = edit->widget.y;
2342 int x = edit->widget.x;
2343 int lines = edit->widget.lines;
2344 int columns = edit->widget.cols;
2346 e = g_malloc0 (sizeof (WEdit));
2347 e->widget = edit->widget;
2349 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2351 g_free (e);
2352 return FALSE;
2355 edit_clean (edit);
2356 memcpy (edit, e, sizeof (WEdit));
2357 g_free (e);
2359 return TRUE;
2362 /* --------------------------------------------------------------------------------------------- */
2364 void
2365 edit_set_codeset (WEdit * edit)
2367 #ifdef HAVE_CHARSET
2368 const char *cp_id;
2370 cp_id =
2371 get_codepage_id (mc_global.source_codepage >=
2372 0 ? mc_global.source_codepage : mc_global.display_codepage);
2374 if (cp_id != NULL)
2376 GIConv conv;
2377 conv = str_crt_conv_from (cp_id);
2378 if (conv != INVALID_CONV)
2380 if (edit->converter != str_cnv_from_term)
2381 str_close_conv (edit->converter);
2382 edit->converter = conv;
2386 if (cp_id != NULL)
2387 edit->utf8 = str_isutf8 (cp_id);
2388 #else
2389 (void) edit;
2390 #endif
2394 /* --------------------------------------------------------------------------------------------- */
2396 Recording stack for undo:
2397 The following is an implementation of a compressed stack. Identical
2398 pushes are recorded by a negative prefix indicating the number of times the
2399 same char was pushed. This saves space for repeated curs-left or curs-right
2400 delete etc.
2404 pushed: stored:
2408 b -3
2410 c --> -4
2416 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2417 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2418 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2419 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2420 position.
2422 The only way the cursor moves or the buffer is changed is through the routines:
2423 insert, backspace, insert_ahead, delete, and cursor_move.
2424 These record the reverse undo movements onto the stack each time they are
2425 called.
2427 Each key press results in a set of actions (insert; delete ...). So each time
2428 a key is pressed the current position of start_display is pushed as
2429 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2430 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2431 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2435 void
2436 edit_push_undo_action (WEdit * edit, long c, ...)
2438 unsigned long sp = edit->undo_stack_pointer;
2439 unsigned long spm1;
2440 long *t;
2442 /* first enlarge the stack if necessary */
2443 if (sp > edit->undo_stack_size - 10)
2444 { /* say */
2445 if (option_max_undo < 256)
2446 option_max_undo = 256;
2447 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2449 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2450 if (t)
2452 edit->undo_stack = t;
2453 edit->undo_stack_size <<= 1;
2454 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2458 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2459 if (edit->undo_stack_disable)
2461 edit_push_redo_action (edit, KEY_PRESS);
2462 edit_push_redo_action (edit, c);
2463 return;
2465 else if (edit->redo_stack_reset)
2467 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2470 if (edit->undo_stack_bottom != sp
2471 && spm1 != edit->undo_stack_bottom
2472 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2474 int d;
2475 if (edit->undo_stack[spm1] < 0)
2477 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2478 if (d == c)
2480 if (edit->undo_stack[spm1] > -1000000000)
2482 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2484 edit->undo_stack[spm1]--;
2486 return;
2490 else
2492 d = edit->undo_stack[spm1];
2493 if (d == c)
2495 if (c >= KEY_PRESS)
2496 return; /* --> no need to push multiple do-nothings */
2497 edit->undo_stack[sp] = -2;
2498 goto check_bottom;
2502 edit->undo_stack[sp] = c;
2504 check_bottom:
2505 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2507 /* if the sp wraps round and catches the undo_stack_bottom then erase
2508 * the first set of actions on the stack to make space - by moving
2509 * undo_stack_bottom forward one "key press" */
2510 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2511 if ((unsigned long) c == edit->undo_stack_bottom ||
2512 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2515 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2517 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2518 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2520 /*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: */
2521 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2522 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2524 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2528 void
2529 edit_push_redo_action (WEdit * edit, long c, ...)
2531 unsigned long sp = edit->redo_stack_pointer;
2532 unsigned long spm1;
2533 long *t;
2534 /* first enlarge the stack if necessary */
2535 if (sp > edit->redo_stack_size - 10)
2536 { /* say */
2537 if (option_max_undo < 256)
2538 option_max_undo = 256;
2539 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2541 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2542 if (t)
2544 edit->redo_stack = t;
2545 edit->redo_stack_size <<= 1;
2546 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2550 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2552 if (edit->redo_stack_bottom != sp
2553 && spm1 != edit->redo_stack_bottom
2554 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2556 int d;
2557 if (edit->redo_stack[spm1] < 0)
2559 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2560 if (d == c)
2562 if (edit->redo_stack[spm1] > -1000000000)
2564 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2565 edit->redo_stack[spm1]--;
2566 return;
2570 else
2572 d = edit->redo_stack[spm1];
2573 if (d == c)
2575 if (c >= KEY_PRESS)
2576 return; /* --> no need to push multiple do-nothings */
2577 edit->redo_stack[sp] = -2;
2578 goto redo_check_bottom;
2582 edit->redo_stack[sp] = c;
2584 redo_check_bottom:
2585 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2587 /* if the sp wraps round and catches the redo_stack_bottom then erase
2588 * the first set of actions on the stack to make space - by moving
2589 * redo_stack_bottom forward one "key press" */
2590 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2591 if ((unsigned long) c == edit->redo_stack_bottom ||
2592 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2595 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2597 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2598 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2601 * If a single key produced enough pushes to wrap all the way round then
2602 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2603 * The stack is then initialised:
2606 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2607 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2608 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2612 /* --------------------------------------------------------------------------------------------- */
2614 Basic low level single character buffer alterations and movements at the cursor.
2615 Returns char passed over, inserted or removed.
2618 void
2619 edit_insert (WEdit * edit, int c)
2621 /* check if file has grown to large */
2622 if (edit->last_byte >= SIZE_LIMIT)
2623 return;
2625 /* first we must update the position of the display window */
2626 if (edit->curs1 < edit->start_display)
2628 edit->start_display++;
2629 if (c == '\n')
2630 edit->start_line++;
2633 /* Mark file as modified, unless the file hasn't been fully loaded */
2634 if (edit->loading_done)
2636 edit_modification (edit);
2639 /* now we must update some info on the file and check if a redraw is required */
2640 if (c == '\n')
2642 if (edit->book_mark)
2643 book_mark_inc (edit, edit->curs_line);
2644 edit->curs_line++;
2645 edit->total_lines++;
2646 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2649 /* save the reverse command onto the undo stack */
2650 /* ordinary char and not space */
2651 if (c > 32)
2652 edit_push_undo_action (edit, BACKSPACE);
2653 else
2654 edit_push_undo_action (edit, BACKSPACE_BR);
2655 /* update markers */
2656 edit->mark1 += (edit->mark1 > edit->curs1);
2657 edit->mark2 += (edit->mark2 > edit->curs1);
2658 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2660 /* add a new buffer if we've reached the end of the last one */
2661 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2662 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2664 /* perform the insertion */
2665 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2666 = (unsigned char) c;
2668 /* update file length */
2669 edit->last_byte++;
2671 /* update cursor position */
2672 edit->curs1++;
2675 /* --------------------------------------------------------------------------------------------- */
2676 /** same as edit_insert and move left */
2678 void
2679 edit_insert_ahead (WEdit * edit, int c)
2681 if (edit->last_byte >= SIZE_LIMIT)
2682 return;
2684 if (edit->curs1 < edit->start_display)
2686 edit->start_display++;
2687 if (c == '\n')
2688 edit->start_line++;
2690 edit_modification (edit);
2691 if (c == '\n')
2693 if (edit->book_mark)
2694 book_mark_inc (edit, edit->curs_line);
2695 edit->total_lines++;
2696 edit->force |= REDRAW_AFTER_CURSOR;
2698 /* ordinary char and not space */
2699 if (c > 32)
2700 edit_push_undo_action (edit, DELCHAR);
2701 else
2702 edit_push_undo_action (edit, DELCHAR_BR);
2704 edit->mark1 += (edit->mark1 >= edit->curs1);
2705 edit->mark2 += (edit->mark2 >= edit->curs1);
2706 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2708 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2709 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2710 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2711 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2713 edit->last_byte++;
2714 edit->curs2++;
2718 /* --------------------------------------------------------------------------------------------- */
2721 edit_delete (WEdit * edit, const int byte_delete)
2723 int p = 0;
2724 int cw = 1;
2725 int i;
2727 if (!edit->curs2)
2728 return 0;
2730 cw = 1;
2731 /* if byte_delete = 1 then delete only one byte not multibyte char */
2732 if (edit->utf8 && byte_delete == 0)
2734 edit_get_utf (edit, edit->curs1, &cw);
2735 if (cw < 1)
2736 cw = 1;
2739 if (edit->mark2 != edit->mark1)
2740 edit_push_markers (edit);
2742 for (i = 1; i <= cw; i++)
2744 if (edit->mark1 > edit->curs1)
2746 edit->mark1--;
2747 edit->end_mark_curs--;
2749 if (edit->mark2 > edit->curs1)
2750 edit->mark2--;
2751 if (edit->last_get_rule > edit->curs1)
2752 edit->last_get_rule--;
2754 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2755 ((edit->curs2 -
2756 1) & M_EDIT_BUF_SIZE) - 1];
2758 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2760 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2761 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2763 edit->last_byte--;
2764 edit->curs2--;
2765 edit_push_undo_action (edit, p + 256);
2768 edit_modification (edit);
2769 if (p == '\n')
2771 if (edit->book_mark)
2772 book_mark_dec (edit, edit->curs_line);
2773 edit->total_lines--;
2774 edit->force |= REDRAW_AFTER_CURSOR;
2776 if (edit->curs1 < edit->start_display)
2778 edit->start_display--;
2779 if (p == '\n')
2780 edit->start_line--;
2783 return p;
2786 /* --------------------------------------------------------------------------------------------- */
2789 edit_backspace (WEdit * edit, const int byte_delete)
2791 int p = 0;
2792 int cw = 1;
2793 int i;
2795 if (edit->curs1 == 0)
2796 return 0;
2798 cw = 1;
2800 if (edit->mark2 != edit->mark1)
2801 edit_push_markers (edit);
2803 if (edit->utf8 && byte_delete == 0)
2805 edit_get_prev_utf (edit, edit->curs1, &cw);
2806 if (cw < 1)
2807 cw = 1;
2809 for (i = 1; i <= cw; i++)
2811 if (edit->mark1 >= edit->curs1)
2813 edit->mark1--;
2814 edit->end_mark_curs--;
2816 if (edit->mark2 >= edit->curs1)
2817 edit->mark2--;
2818 if (edit->last_get_rule >= edit->curs1)
2819 edit->last_get_rule--;
2821 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2822 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2823 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2825 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2826 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2828 edit->last_byte--;
2829 edit->curs1--;
2830 edit_push_undo_action (edit, p);
2832 edit_modification (edit);
2833 if (p == '\n')
2835 if (edit->book_mark)
2836 book_mark_dec (edit, edit->curs_line);
2837 edit->curs_line--;
2838 edit->total_lines--;
2839 edit->force |= REDRAW_AFTER_CURSOR;
2842 if (edit->curs1 < edit->start_display)
2844 edit->start_display--;
2845 if (p == '\n')
2846 edit->start_line--;
2849 return p;
2852 /* --------------------------------------------------------------------------------------------- */
2853 /** moves the cursor right or left: increment positive or negative respectively */
2855 void
2856 edit_cursor_move (WEdit * edit, long increment)
2858 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2859 int c;
2861 if (increment < 0)
2863 for (; increment < 0; increment++)
2865 if (!edit->curs1)
2866 return;
2868 edit_push_undo_action (edit, CURS_RIGHT);
2870 c = edit_get_byte (edit, edit->curs1 - 1);
2871 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2872 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2873 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2874 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2875 edit->curs2++;
2876 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2877 1) & M_EDIT_BUF_SIZE];
2878 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2880 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2881 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2883 edit->curs1--;
2884 if (c == '\n')
2886 edit->curs_line--;
2887 edit->force |= REDRAW_LINE_BELOW;
2892 else if (increment > 0)
2894 for (; increment > 0; increment--)
2896 if (!edit->curs2)
2897 return;
2899 edit_push_undo_action (edit, CURS_LEFT);
2901 c = edit_get_byte (edit, edit->curs1);
2902 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2903 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2904 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2905 edit->curs1++;
2906 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2907 ((edit->curs2 -
2908 1) & M_EDIT_BUF_SIZE) - 1];
2909 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2911 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2912 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2914 edit->curs2--;
2915 if (c == '\n')
2917 edit->curs_line++;
2918 edit->force |= REDRAW_LINE_ABOVE;
2924 /* These functions return positions relative to lines */
2926 /* --------------------------------------------------------------------------------------------- */
2927 /** returns index of last char on line + 1 */
2929 long
2930 edit_eol (WEdit * edit, long current)
2932 if (current >= edit->last_byte)
2933 return edit->last_byte;
2935 for (;; current++)
2936 if (edit_get_byte (edit, current) == '\n')
2937 break;
2938 return current;
2941 /* --------------------------------------------------------------------------------------------- */
2942 /** returns index of first char on line */
2944 long
2945 edit_bol (WEdit * edit, long current)
2947 if (current <= 0)
2948 return 0;
2950 for (;; current--)
2951 if (edit_get_byte (edit, current - 1) == '\n')
2952 break;
2953 return current;
2956 /* --------------------------------------------------------------------------------------------- */
2958 long
2959 edit_count_lines (WEdit * edit, long current, long upto)
2961 long lines = 0;
2962 if (upto > edit->last_byte)
2963 upto = edit->last_byte;
2964 if (current < 0)
2965 current = 0;
2966 while (current < upto)
2967 if (edit_get_byte (edit, current++) == '\n')
2968 lines++;
2969 return lines;
2972 /* --------------------------------------------------------------------------------------------- */
2973 /* If lines is zero this returns the count of lines from current to upto. */
2974 /* If upto is zero returns index of lines forward current. */
2976 long
2977 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2979 if (upto)
2981 return edit_count_lines (edit, current, upto);
2983 else
2985 long next;
2986 if (lines < 0)
2987 lines = 0;
2988 while (lines--)
2990 next = edit_eol (edit, current) + 1;
2991 if (next > edit->last_byte)
2992 break;
2993 else
2994 current = next;
2996 return current;
3000 /* --------------------------------------------------------------------------------------------- */
3001 /** Returns offset of 'lines' lines up from current */
3003 long
3004 edit_move_backward (WEdit * edit, long current, long lines)
3006 if (lines < 0)
3007 lines = 0;
3008 current = edit_bol (edit, current);
3009 while ((lines--) && current != 0)
3010 current = edit_bol (edit, current - 1);
3011 return current;
3014 /* --------------------------------------------------------------------------------------------- */
3015 /* If cols is zero this returns the count of columns from current to upto. */
3016 /* If upto is zero returns index of cols across from current. */
3018 long
3019 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
3021 long p, q;
3022 int col;
3024 if (upto)
3026 q = upto;
3027 cols = -10;
3029 else
3030 q = edit->last_byte + 2;
3032 for (col = 0, p = current; p < q; p++)
3034 int c, orig_c;
3036 if (cols != -10)
3038 if (col == cols)
3039 return p;
3040 if (col > cols)
3041 return p - 1;
3044 orig_c = c = edit_get_byte (edit, p);
3046 #ifdef HAVE_CHARSET
3047 if (edit->utf8)
3049 int utf_ch;
3050 int cw = 1;
3052 utf_ch = edit_get_utf (edit, p, &cw);
3053 if (mc_global.utf8_display)
3055 if (cw > 1)
3056 col -= cw - 1;
3057 if (g_unichar_iswide (utf_ch))
3058 col++;
3060 else if (cw > 1 && g_unichar_isprint (utf_ch))
3061 col -= cw - 1;
3064 c = convert_to_display_c (c);
3065 #endif
3067 if (c == '\t')
3068 col += TAB_SIZE - col % TAB_SIZE;
3069 else if (c == '\n')
3071 if (upto)
3072 return col;
3073 else
3074 return p;
3076 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3077 /* '\r' is shown as ^M, so we must advance 2 characters */
3078 /* Caret notation for control characters */
3079 col += 2;
3080 else
3081 col++;
3083 return col;
3086 /* --------------------------------------------------------------------------------------------- */
3087 /** returns the current column position of the cursor */
3090 edit_get_col (WEdit * edit)
3092 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3095 /* --------------------------------------------------------------------------------------------- */
3096 /* Scrolling functions */
3097 /* --------------------------------------------------------------------------------------------- */
3099 void
3100 edit_update_curs_row (WEdit * edit)
3102 edit->curs_row = edit->curs_line - edit->start_line;
3105 /* --------------------------------------------------------------------------------------------- */
3107 void
3108 edit_update_curs_col (WEdit * edit)
3110 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3113 /* --------------------------------------------------------------------------------------------- */
3116 edit_get_curs_col (const WEdit * edit)
3118 return edit->curs_col;
3121 /* --------------------------------------------------------------------------------------------- */
3122 /** moves the display start position up by i lines */
3124 void
3125 edit_scroll_upward (WEdit * edit, unsigned long i)
3127 unsigned long lines_above = edit->start_line;
3128 if (i > lines_above)
3129 i = lines_above;
3130 if (i)
3132 edit->start_line -= i;
3133 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3134 edit->force |= REDRAW_PAGE;
3135 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3137 edit_update_curs_row (edit);
3141 /* --------------------------------------------------------------------------------------------- */
3142 /** returns 1 if could scroll, 0 otherwise */
3144 void
3145 edit_scroll_downward (WEdit * edit, int i)
3147 int lines_below;
3148 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3149 if (lines_below > 0)
3151 if (i > lines_below)
3152 i = lines_below;
3153 edit->start_line += i;
3154 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3155 edit->force |= REDRAW_PAGE;
3156 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3158 edit_update_curs_row (edit);
3161 /* --------------------------------------------------------------------------------------------- */
3163 void
3164 edit_scroll_right (WEdit * edit, int i)
3166 edit->force |= REDRAW_PAGE;
3167 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3168 edit->start_col -= i;
3171 /* --------------------------------------------------------------------------------------------- */
3173 void
3174 edit_scroll_left (WEdit * edit, int i)
3176 if (edit->start_col)
3178 edit->start_col += i;
3179 if (edit->start_col > 0)
3180 edit->start_col = 0;
3181 edit->force |= REDRAW_PAGE;
3182 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3186 /* --------------------------------------------------------------------------------------------- */
3187 /* high level cursor movement commands */
3188 /* --------------------------------------------------------------------------------------------- */
3190 void
3191 edit_move_to_prev_col (WEdit * edit, long p)
3193 int prev = edit->prev_col;
3194 int over = edit->over_col;
3195 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3197 if (option_cursor_beyond_eol)
3199 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3200 edit_eol (edit, edit->curs1));
3202 if (line_len < prev + edit->over_col)
3204 edit->over_col = prev + over - line_len;
3205 edit->prev_col = line_len;
3206 edit->curs_col = line_len;
3208 else
3210 edit->curs_col = prev + over;
3211 edit->prev_col = edit->curs_col;
3212 edit->over_col = 0;
3215 else
3217 edit->over_col = 0;
3218 if (is_in_indent (edit) && option_fake_half_tabs)
3220 edit_update_curs_col (edit);
3221 if (space_width)
3222 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3224 int q = edit->curs_col;
3225 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3226 p = edit_bol (edit, edit->curs1);
3227 edit_cursor_move (edit,
3228 edit_move_forward3 (edit, p, edit->curs_col,
3229 0) - edit->curs1);
3230 if (!left_of_four_spaces (edit))
3231 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3237 /* --------------------------------------------------------------------------------------------- */
3238 /** check whether line in editor is blank or not
3240 * @param edit editor object
3241 * @param line number of line
3243 * @return TRUE if line in blank, FALSE otherwise
3246 gboolean
3247 line_is_blank (WEdit * edit, long line)
3249 return is_blank (edit, edit_find_line (edit, line));
3252 /* --------------------------------------------------------------------------------------------- */
3253 /** move cursor to line 'line' */
3255 void
3256 edit_move_to_line (WEdit * e, long line)
3258 if (line < e->curs_line)
3259 edit_move_up (e, e->curs_line - line, 0);
3260 else
3261 edit_move_down (e, line - e->curs_line, 0);
3262 edit_scroll_screen_over_cursor (e);
3265 /* --------------------------------------------------------------------------------------------- */
3266 /** scroll window so that first visible line is 'line' */
3268 void
3269 edit_move_display (WEdit * e, long line)
3271 if (line < e->start_line)
3272 edit_scroll_upward (e, e->start_line - line);
3273 else
3274 edit_scroll_downward (e, line - e->start_line);
3277 /* --------------------------------------------------------------------------------------------- */
3278 /** save markers onto undo stack */
3280 void
3281 edit_push_markers (WEdit * edit)
3283 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3284 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3285 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3288 /* --------------------------------------------------------------------------------------------- */
3290 void
3291 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3293 edit->mark1 = m1;
3294 edit->mark2 = m2;
3295 edit->column1 = c1;
3296 edit->column2 = c2;
3300 /* --------------------------------------------------------------------------------------------- */
3301 /** highlight marker toggle */
3303 void
3304 edit_mark_cmd (WEdit * edit, gboolean unmark)
3306 edit_push_markers (edit);
3307 if (unmark)
3309 edit_set_markers (edit, 0, 0, 0, 0);
3310 edit->force |= REDRAW_PAGE;
3312 else
3314 if (edit->mark2 >= 0)
3316 edit->end_mark_curs = -1;
3317 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3318 edit->curs_col + edit->over_col);
3319 edit->force |= REDRAW_PAGE;
3321 else
3323 edit->end_mark_curs = edit->curs1;
3324 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3325 edit->curs_col + edit->over_col);
3330 /* --------------------------------------------------------------------------------------------- */
3331 /** highlight the word under cursor */
3333 void
3334 edit_mark_current_word_cmd (WEdit * edit)
3336 long pos;
3338 for (pos = edit->curs1; pos != 0; pos--)
3340 int c1, c2;
3342 c1 = edit_get_byte (edit, pos);
3343 c2 = edit_get_byte (edit, pos - 1);
3344 if (!isspace (c1) && isspace (c2))
3345 break;
3346 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3347 break;
3349 edit->mark1 = pos;
3351 for (; pos < edit->last_byte; pos++)
3353 int c1, c2;
3355 c1 = edit_get_byte (edit, pos);
3356 c2 = edit_get_byte (edit, pos + 1);
3357 if (!isspace (c1) && isspace (c2))
3358 break;
3359 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3360 break;
3362 edit->mark2 = min (pos + 1, edit->last_byte);
3364 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3367 /* --------------------------------------------------------------------------------------------- */
3369 void
3370 edit_mark_current_line_cmd (WEdit * edit)
3372 long pos = edit->curs1;
3374 edit->mark1 = edit_bol (edit, pos);
3375 edit->mark2 = edit_eol (edit, pos);
3377 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3380 /* --------------------------------------------------------------------------------------------- */
3382 void
3383 edit_delete_line (WEdit * edit)
3386 * Delete right part of the line.
3387 * Note that edit_get_byte() returns '\n' when byte position is
3388 * beyond EOF.
3390 while (edit_get_byte (edit, edit->curs1) != '\n')
3392 (void) edit_delete (edit, 1);
3396 * Delete '\n' char.
3397 * Note that edit_delete() will not corrupt anything if called while
3398 * cursor position is EOF.
3400 (void) edit_delete (edit, 1);
3403 * Delete left part of the line.
3404 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3406 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3408 (void) edit_backspace (edit, 1);
3412 /* --------------------------------------------------------------------------------------------- */
3415 edit_indent_width (WEdit * edit, long p)
3417 long q = p;
3418 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3419 q++;
3420 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3423 /* --------------------------------------------------------------------------------------------- */
3425 void
3426 edit_insert_indent (WEdit * edit, int indent)
3428 if (!option_fill_tabs_with_spaces)
3430 while (indent >= TAB_SIZE)
3432 edit_insert (edit, '\t');
3433 indent -= TAB_SIZE;
3436 while (indent-- > 0)
3437 edit_insert (edit, ' ');
3440 /* --------------------------------------------------------------------------------------------- */
3442 void
3443 edit_push_key_press (WEdit * edit)
3445 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3446 if (edit->mark2 == -1)
3448 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3449 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3453 /* --------------------------------------------------------------------------------------------- */
3455 void
3456 edit_find_bracket (WEdit * edit)
3458 edit->bracket = edit_get_bracket (edit, 1, 10000);
3459 if (last_bracket != edit->bracket)
3460 edit->force |= REDRAW_PAGE;
3461 last_bracket = edit->bracket;
3464 /* --------------------------------------------------------------------------------------------- */
3466 * This executes a command as though the user initiated it through a key
3467 * press. Callback with WIDGET_KEY as a message calls this after
3468 * translating the key press. This function can be used to pass any
3469 * command to the editor. Note that the screen wouldn't update
3470 * automatically. Either of command or char_for_insertion must be
3471 * passed as -1. Commands are executed, and char_for_insertion is
3472 * inserted at the cursor.
3475 void
3476 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3478 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3479 || (macro_index < 0
3480 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3482 macro_index = 0;
3483 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3484 return;
3486 if (macro_index != -1)
3488 edit->force |= REDRAW_COMPLETELY;
3489 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3491 edit_store_macro_cmd (edit);
3492 macro_index = -1;
3493 return;
3495 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3497 edit_repeat_macro_cmd (edit);
3498 macro_index = -1;
3499 return;
3503 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3505 record_macro_buf[macro_index].action = command;
3506 record_macro_buf[macro_index++].ch = char_for_insertion;
3508 /* record the beginning of a set of editing actions initiated by a key press */
3509 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3510 edit_push_key_press (edit);
3512 edit_execute_cmd (edit, command, char_for_insertion);
3513 if (edit->column_highlight)
3514 edit->force |= REDRAW_PAGE;
3517 /* --------------------------------------------------------------------------------------------- */
3519 This executes a command at a lower level than macro recording.
3520 It also does not push a key_press onto the undo stack. This means
3521 that if it is called many times, a single undo command will undo
3522 all of them. It also does not check for the Undo command.
3524 void
3525 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3527 if (command == CK_WindowFullscreen)
3529 edit_toggle_fullscreen (edit);
3530 return;
3533 /* handle window state */
3534 if (edit_handle_move_resize (edit, command))
3535 return;
3537 edit->force |= REDRAW_LINE;
3539 /* The next key press will unhighlight the found string, so update
3540 * the whole page */
3541 if (edit->found_len || edit->column_highlight)
3542 edit->force |= REDRAW_PAGE;
3544 switch (command)
3546 /* a mark command with shift-arrow */
3547 case CK_MarkLeft:
3548 case CK_MarkRight:
3549 case CK_MarkToWordBegin:
3550 case CK_MarkToWordEnd:
3551 case CK_MarkToHome:
3552 case CK_MarkToEnd:
3553 case CK_MarkUp:
3554 case CK_MarkDown:
3555 case CK_MarkPageUp:
3556 case CK_MarkPageDown:
3557 case CK_MarkToFileBegin:
3558 case CK_MarkToFileEnd:
3559 case CK_MarkToPageBegin:
3560 case CK_MarkToPageEnd:
3561 case CK_MarkScrollUp:
3562 case CK_MarkScrollDown:
3563 case CK_MarkParagraphUp:
3564 case CK_MarkParagraphDown:
3565 /* a mark command with alt-arrow */
3566 case CK_MarkColumnPageUp:
3567 case CK_MarkColumnPageDown:
3568 case CK_MarkColumnLeft:
3569 case CK_MarkColumnRight:
3570 case CK_MarkColumnUp:
3571 case CK_MarkColumnDown:
3572 case CK_MarkColumnScrollUp:
3573 case CK_MarkColumnScrollDown:
3574 case CK_MarkColumnParagraphUp:
3575 case CK_MarkColumnParagraphDown:
3576 edit->column_highlight = 0;
3577 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3579 edit_mark_cmd (edit, TRUE); /* clear */
3580 edit_mark_cmd (edit, FALSE); /* marking on */
3582 edit->highlight = 1;
3583 break;
3585 /* any other command */
3586 default:
3587 if (edit->highlight)
3588 edit_mark_cmd (edit, FALSE); /* clear */
3589 edit->highlight = 0;
3592 /* first check for undo */
3593 if (command == CK_Undo)
3595 edit->redo_stack_reset = 0;
3596 edit_group_undo (edit);
3597 edit->found_len = 0;
3598 edit->prev_col = edit_get_col (edit);
3599 edit->search_start = edit->curs1;
3600 return;
3602 /* check for redo */
3603 if (command == CK_Redo)
3605 edit->redo_stack_reset = 0;
3606 edit_do_redo (edit);
3607 edit->found_len = 0;
3608 edit->prev_col = edit_get_col (edit);
3609 edit->search_start = edit->curs1;
3610 return;
3613 edit->redo_stack_reset = 1;
3615 /* An ordinary key press */
3616 if (char_for_insertion >= 0)
3618 /* if non persistent selection and text selected */
3619 if (!option_persistent_selections)
3621 if (edit->mark1 != edit->mark2)
3622 edit_block_delete_cmd (edit);
3624 if (edit->overwrite)
3626 /* remove char only one time, after input first byte, multibyte chars */
3627 if ((!mc_global.utf8_display || edit->charpoint == 0)
3628 && edit_get_byte (edit, edit->curs1) != '\n')
3629 edit_delete (edit, 0);
3631 if (option_cursor_beyond_eol && edit->over_col > 0)
3632 edit_insert_over (edit);
3633 #ifdef HAVE_CHARSET
3634 if (char_for_insertion > 255 && !mc_global.utf8_display)
3636 unsigned char str[6 + 1];
3637 size_t i = 0;
3638 int res;
3640 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3641 if (res == 0)
3643 str[0] = '.';
3644 str[1] = '\0';
3646 else
3648 str[res] = '\0';
3650 while (str[i] != 0 && i <= 6)
3652 char_for_insertion = str[i];
3653 edit_insert (edit, char_for_insertion);
3654 i++;
3657 else
3658 #endif
3659 edit_insert (edit, char_for_insertion);
3661 if (option_auto_para_formatting)
3663 format_paragraph (edit, 0);
3664 edit->force |= REDRAW_PAGE;
3666 else
3667 check_and_wrap_line (edit);
3668 edit->found_len = 0;
3669 edit->prev_col = edit_get_col (edit);
3670 edit->search_start = edit->curs1;
3671 edit_find_bracket (edit);
3672 return;
3675 switch (command)
3677 case CK_TopOnScreen:
3678 case CK_BottomOnScreen:
3679 case CK_Top:
3680 case CK_Bottom:
3681 case CK_PageUp:
3682 case CK_PageDown:
3683 case CK_Home:
3684 case CK_End:
3685 case CK_Up:
3686 case CK_Down:
3687 case CK_Left:
3688 case CK_Right:
3689 case CK_WordLeft:
3690 case CK_WordRight:
3691 if (edit->mark2 >= 0)
3693 if (!option_persistent_selections)
3695 if (edit->column_highlight)
3696 edit_push_undo_action (edit, COLUMN_ON);
3697 edit->column_highlight = 0;
3698 edit_mark_cmd (edit, TRUE);
3703 switch (command)
3705 case CK_TopOnScreen:
3706 case CK_BottomOnScreen:
3707 case CK_MarkToPageBegin:
3708 case CK_MarkToPageEnd:
3709 case CK_Up:
3710 case CK_Down:
3711 case CK_WordLeft:
3712 case CK_WordRight:
3713 case CK_MarkToWordBegin:
3714 case CK_MarkToWordEnd:
3715 case CK_MarkUp:
3716 case CK_MarkDown:
3717 case CK_MarkColumnUp:
3718 case CK_MarkColumnDown:
3719 if (edit->mark2 == -1)
3720 break; /*marking is following the cursor: may need to highlight a whole line */
3721 case CK_Left:
3722 case CK_Right:
3723 case CK_MarkLeft:
3724 case CK_MarkRight:
3725 edit->force |= REDRAW_CHAR_ONLY;
3728 /* basic cursor key commands */
3729 switch (command)
3731 case CK_BackSpace:
3732 /* if non persistent selection and text selected */
3733 if (!option_persistent_selections)
3735 if (edit->mark1 != edit->mark2)
3737 edit_block_delete_cmd (edit);
3738 break;
3741 if (option_cursor_beyond_eol && edit->over_col > 0)
3743 edit->over_col--;
3744 break;
3746 if (option_backspace_through_tabs && is_in_indent (edit))
3748 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3749 edit_backspace (edit, 1);
3750 break;
3752 else
3754 if (option_fake_half_tabs)
3756 int i;
3757 if (is_in_indent (edit) && right_of_four_spaces (edit))
3759 for (i = 0; i < HALF_TAB_SIZE; i++)
3760 edit_backspace (edit, 1);
3761 break;
3765 edit_backspace (edit, 0);
3766 break;
3767 case CK_Delete:
3768 /* if non persistent selection and text selected */
3769 if (!option_persistent_selections)
3771 if (edit->mark1 != edit->mark2)
3773 edit_block_delete_cmd (edit);
3774 break;
3778 if (option_cursor_beyond_eol && edit->over_col > 0)
3779 edit_insert_over (edit);
3781 if (option_fake_half_tabs)
3783 int i;
3784 if (is_in_indent (edit) && left_of_four_spaces (edit))
3786 for (i = 1; i <= HALF_TAB_SIZE; i++)
3787 edit_delete (edit, 1);
3788 break;
3791 edit_delete (edit, 0);
3792 break;
3793 case CK_DeleteToWordBegin:
3794 edit->over_col = 0;
3795 edit_left_delete_word (edit);
3796 break;
3797 case CK_DeleteToWordEnd:
3798 if (option_cursor_beyond_eol && edit->over_col > 0)
3799 edit_insert_over (edit);
3801 edit_right_delete_word (edit);
3802 break;
3803 case CK_DeleteLine:
3804 edit_delete_line (edit);
3805 break;
3806 case CK_DeleteToHome:
3807 edit_delete_to_line_begin (edit);
3808 break;
3809 case CK_DeleteToEnd:
3810 edit_delete_to_line_end (edit);
3811 break;
3812 case CK_Enter:
3813 edit->over_col = 0;
3814 if (option_auto_para_formatting)
3816 edit_double_newline (edit);
3817 if (option_return_does_auto_indent)
3818 edit_auto_indent (edit);
3819 format_paragraph (edit, 0);
3821 else
3823 edit_insert (edit, '\n');
3824 if (option_return_does_auto_indent)
3826 edit_auto_indent (edit);
3829 break;
3830 case CK_Return:
3831 edit_insert (edit, '\n');
3832 break;
3834 case CK_MarkColumnPageUp:
3835 edit->column_highlight = 1;
3836 case CK_PageUp:
3837 case CK_MarkPageUp:
3838 edit_move_up (edit, edit->widget.lines - 1, 1);
3839 break;
3840 case CK_MarkColumnPageDown:
3841 edit->column_highlight = 1;
3842 case CK_PageDown:
3843 case CK_MarkPageDown:
3844 edit_move_down (edit, edit->widget.lines - 1, 1);
3845 break;
3846 case CK_MarkColumnLeft:
3847 edit->column_highlight = 1;
3848 case CK_Left:
3849 case CK_MarkLeft:
3850 if (option_fake_half_tabs)
3852 if (is_in_indent (edit) && right_of_four_spaces (edit))
3854 if (option_cursor_beyond_eol && edit->over_col > 0)
3855 edit->over_col--;
3856 else
3857 edit_cursor_move (edit, -HALF_TAB_SIZE);
3858 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3859 break;
3862 edit_left_char_move_cmd (edit);
3863 break;
3864 case CK_MarkColumnRight:
3865 edit->column_highlight = 1;
3866 case CK_Right:
3867 case CK_MarkRight:
3868 if (option_fake_half_tabs)
3870 if (is_in_indent (edit) && left_of_four_spaces (edit))
3872 edit_cursor_move (edit, HALF_TAB_SIZE);
3873 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3874 break;
3877 edit_right_char_move_cmd (edit);
3878 break;
3879 case CK_TopOnScreen:
3880 case CK_MarkToPageBegin:
3881 edit_begin_page (edit);
3882 break;
3883 case CK_BottomOnScreen:
3884 case CK_MarkToPageEnd:
3885 edit_end_page (edit);
3886 break;
3887 case CK_WordLeft:
3888 case CK_MarkToWordBegin:
3889 edit->over_col = 0;
3890 edit_left_word_move_cmd (edit);
3891 break;
3892 case CK_WordRight:
3893 case CK_MarkToWordEnd:
3894 edit->over_col = 0;
3895 edit_right_word_move_cmd (edit);
3896 break;
3897 case CK_MarkColumnUp:
3898 edit->column_highlight = 1;
3899 case CK_Up:
3900 case CK_MarkUp:
3901 edit_move_up (edit, 1, 0);
3902 break;
3903 case CK_MarkColumnDown:
3904 edit->column_highlight = 1;
3905 case CK_Down:
3906 case CK_MarkDown:
3907 edit_move_down (edit, 1, 0);
3908 break;
3909 case CK_MarkColumnParagraphUp:
3910 edit->column_highlight = 1;
3911 case CK_ParagraphUp:
3912 case CK_MarkParagraphUp:
3913 edit_move_up_paragraph (edit, 0);
3914 break;
3915 case CK_MarkColumnParagraphDown:
3916 edit->column_highlight = 1;
3917 case CK_ParagraphDown:
3918 case CK_MarkParagraphDown:
3919 edit_move_down_paragraph (edit, 0);
3920 break;
3921 case CK_MarkColumnScrollUp:
3922 edit->column_highlight = 1;
3923 case CK_ScrollUp:
3924 case CK_MarkScrollUp:
3925 edit_move_up (edit, 1, 1);
3926 break;
3927 case CK_MarkColumnScrollDown:
3928 edit->column_highlight = 1;
3929 case CK_ScrollDown:
3930 case CK_MarkScrollDown:
3931 edit_move_down (edit, 1, 1);
3932 break;
3933 case CK_Home:
3934 case CK_MarkToHome:
3935 edit_cursor_to_bol (edit);
3936 break;
3937 case CK_End:
3938 case CK_MarkToEnd:
3939 edit_cursor_to_eol (edit);
3940 break;
3941 case CK_Tab:
3942 /* if text marked shift block */
3943 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3945 if (edit->mark2 < 0)
3946 edit_mark_cmd (edit, FALSE);
3947 edit_move_block_to_right (edit);
3949 else
3951 if (option_cursor_beyond_eol)
3952 edit_insert_over (edit);
3953 edit_tab_cmd (edit);
3954 if (option_auto_para_formatting)
3956 format_paragraph (edit, 0);
3957 edit->force |= REDRAW_PAGE;
3959 else
3961 check_and_wrap_line (edit);
3964 break;
3966 case CK_InsertOverwrite:
3967 edit->overwrite = !edit->overwrite;
3968 break;
3970 case CK_Mark:
3971 if (edit->mark2 >= 0)
3973 if (edit->column_highlight)
3974 edit_push_undo_action (edit, COLUMN_ON);
3975 edit->column_highlight = 0;
3977 edit_mark_cmd (edit, FALSE);
3978 break;
3979 case CK_MarkColumn:
3980 if (!edit->column_highlight)
3981 edit_push_undo_action (edit, COLUMN_OFF);
3982 edit->column_highlight = 1;
3983 edit_mark_cmd (edit, FALSE);
3984 break;
3985 case CK_MarkAll:
3986 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3987 edit->force |= REDRAW_PAGE;
3988 break;
3989 case CK_Unmark:
3990 if (edit->column_highlight)
3991 edit_push_undo_action (edit, COLUMN_ON);
3992 edit->column_highlight = 0;
3993 edit_mark_cmd (edit, TRUE);
3994 break;
3995 case CK_MarkWord:
3996 if (edit->column_highlight)
3997 edit_push_undo_action (edit, COLUMN_ON);
3998 edit->column_highlight = 0;
3999 edit_mark_current_word_cmd (edit);
4000 break;
4001 case CK_MarkLine:
4002 if (edit->column_highlight)
4003 edit_push_undo_action (edit, COLUMN_ON);
4004 edit->column_highlight = 0;
4005 edit_mark_current_line_cmd (edit);
4006 break;
4008 case CK_Bookmark:
4009 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4010 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4011 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4012 else
4013 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4014 break;
4015 case CK_BookmarkFlush:
4016 book_mark_flush (edit, BOOK_MARK_COLOR);
4017 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4018 edit->force |= REDRAW_PAGE;
4019 break;
4020 case CK_BookmarkNext:
4021 if (edit->book_mark)
4023 struct _book_mark *p;
4024 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4025 if (p->next)
4027 p = p->next;
4028 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4029 edit_move_display (edit, p->line - edit->widget.lines / 2);
4030 edit_move_to_line (edit, p->line);
4033 break;
4034 case CK_BookmarkPrev:
4035 if (edit->book_mark)
4037 struct _book_mark *p;
4038 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4039 while (p->line == edit->curs_line)
4040 if (p->prev)
4041 p = p->prev;
4042 if (p->line >= 0)
4044 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4045 edit_move_display (edit, p->line - edit->widget.lines / 2);
4046 edit_move_to_line (edit, p->line);
4049 break;
4051 case CK_Top:
4052 case CK_MarkToFileBegin:
4053 edit_move_to_top (edit);
4054 break;
4055 case CK_Bottom:
4056 case CK_MarkToFileEnd:
4057 edit_move_to_bottom (edit);
4058 break;
4060 case CK_Copy:
4061 if (option_cursor_beyond_eol && edit->over_col > 0)
4062 edit_insert_over (edit);
4063 edit_block_copy_cmd (edit);
4064 break;
4065 case CK_Remove:
4066 edit_block_delete_cmd (edit);
4067 break;
4068 case CK_Move:
4069 edit_block_move_cmd (edit);
4070 break;
4072 case CK_BlockShiftLeft:
4073 if (edit->mark1 != edit->mark2)
4074 edit_move_block_to_left (edit);
4075 break;
4076 case CK_BlockShiftRight:
4077 if (edit->mark1 != edit->mark2)
4078 edit_move_block_to_right (edit);
4079 break;
4080 case CK_Store:
4081 edit_copy_to_X_buf_cmd (edit);
4082 break;
4083 case CK_Cut:
4084 edit_cut_to_X_buf_cmd (edit);
4085 break;
4086 case CK_Paste:
4087 /* if non persistent selection and text selected */
4088 if (!option_persistent_selections)
4090 if (edit->mark1 != edit->mark2)
4091 edit_block_delete_cmd (edit);
4093 if (option_cursor_beyond_eol && edit->over_col > 0)
4094 edit_insert_over (edit);
4095 edit_paste_from_X_buf_cmd (edit);
4096 break;
4097 case CK_History:
4098 edit_paste_from_history (edit);
4099 break;
4101 case CK_SaveAs:
4102 edit_save_as_cmd (edit);
4103 break;
4104 case CK_Save:
4105 edit_save_confirm_cmd (edit);
4106 break;
4107 case CK_BlockSave:
4108 edit_save_block_cmd (edit);
4109 break;
4110 case CK_InsertFile:
4111 edit_insert_file_cmd (edit);
4112 break;
4114 case CK_FilePrev:
4115 edit_load_back_cmd (edit);
4116 break;
4117 case CK_FileNext:
4118 edit_load_forward_cmd (edit);
4119 break;
4121 case CK_SyntaxChoose:
4122 edit_syntax_dialog (edit);
4123 break;
4125 case CK_Search:
4126 edit_search_cmd (edit, FALSE);
4127 break;
4128 case CK_SearchContinue:
4129 edit_search_cmd (edit, TRUE);
4130 break;
4131 case CK_Replace:
4132 edit_replace_cmd (edit, 0);
4133 break;
4134 case CK_ReplaceContinue:
4135 edit_replace_cmd (edit, 1);
4136 break;
4137 case CK_Complete:
4138 /* if text marked shift block */
4139 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4141 edit_move_block_to_left (edit);
4143 else
4145 edit_complete_word_cmd (edit);
4147 break;
4148 case CK_Find:
4149 edit_get_match_keyword_cmd (edit);
4150 break;
4151 #ifdef HAVE_ASPELL
4152 case CK_SpellCheckCurrentWord:
4153 edit_suggest_current_word (edit);
4154 break;
4155 case CK_SpellCheck:
4156 edit_spellcheck_file (edit);
4157 break;
4158 case CK_SpellCheckSelectLang:
4159 edit_set_spell_lang ();
4160 break;
4161 #endif
4162 case CK_Date:
4164 char s[BUF_MEDIUM];
4165 /* fool gcc to prevent a Y2K warning */
4166 char time_format[] = "_c";
4167 time_format[0] = '%';
4169 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4170 edit_print_string (edit, s);
4171 edit->force |= REDRAW_PAGE;
4172 break;
4174 break;
4175 case CK_Goto:
4176 edit_goto_cmd (edit);
4177 break;
4178 case CK_ParagraphFormat:
4179 format_paragraph (edit, 1);
4180 edit->force |= REDRAW_PAGE;
4181 break;
4182 case CK_MacroDelete:
4183 edit_delete_macro_cmd (edit);
4184 break;
4185 case CK_MatchBracket:
4186 edit_goto_matching_bracket (edit);
4187 break;
4188 case CK_UserMenu:
4189 user_menu (edit, NULL, -1);
4190 break;
4191 case CK_Sort:
4192 edit_sort_cmd (edit);
4193 break;
4194 case CK_ExternalCommand:
4195 edit_ext_cmd (edit);
4196 break;
4197 case CK_Mail:
4198 edit_mail_dialog (edit);
4199 break;
4200 #ifdef HAVE_CHARSET
4201 case CK_SelectCodepage:
4202 edit_select_codepage_cmd (edit);
4203 break;
4204 #endif
4205 case CK_InsertLiteral:
4206 edit_insert_literal_cmd (edit);
4207 break;
4208 case CK_MacroStartStopRecord:
4209 edit_begin_end_macro_cmd (edit);
4210 break;
4211 case CK_RepeatStartStopRecord:
4212 edit_begin_end_repeat_cmd (edit);
4213 break;
4214 case CK_ExtendedKeyMap:
4215 edit->extmod = TRUE;
4216 break;
4217 default:
4218 break;
4221 /* CK_PipeBlock */
4222 if ((command / CK_PipeBlock (0)) == 1)
4223 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4225 /* keys which must set the col position, and the search vars */
4226 switch (command)
4228 case CK_Search:
4229 case CK_SearchContinue:
4230 case CK_Replace:
4231 case CK_ReplaceContinue:
4232 case CK_Complete:
4233 edit->prev_col = edit_get_col (edit);
4234 break;
4235 case CK_Up:
4236 case CK_MarkUp:
4237 case CK_MarkColumnUp:
4238 case CK_Down:
4239 case CK_MarkDown:
4240 case CK_MarkColumnDown:
4241 case CK_PageUp:
4242 case CK_MarkPageUp:
4243 case CK_MarkColumnPageUp:
4244 case CK_PageDown:
4245 case CK_MarkPageDown:
4246 case CK_MarkColumnPageDown:
4247 case CK_Top:
4248 case CK_MarkToFileBegin:
4249 case CK_Bottom:
4250 case CK_MarkToFileEnd:
4251 case CK_ParagraphUp:
4252 case CK_MarkParagraphUp:
4253 case CK_MarkColumnParagraphUp:
4254 case CK_ParagraphDown:
4255 case CK_MarkParagraphDown:
4256 case CK_MarkColumnParagraphDown:
4257 case CK_ScrollUp:
4258 case CK_MarkScrollUp:
4259 case CK_MarkColumnScrollUp:
4260 case CK_ScrollDown:
4261 case CK_MarkScrollDown:
4262 case CK_MarkColumnScrollDown:
4263 edit->search_start = edit->curs1;
4264 edit->found_len = 0;
4265 break;
4266 default:
4267 edit->found_len = 0;
4268 edit->prev_col = edit_get_col (edit);
4269 edit->search_start = edit->curs1;
4271 edit_find_bracket (edit);
4273 if (option_auto_para_formatting)
4275 switch (command)
4277 case CK_BackSpace:
4278 case CK_Delete:
4279 case CK_DeleteToWordBegin:
4280 case CK_DeleteToWordEnd:
4281 case CK_DeleteToHome:
4282 case CK_DeleteToEnd:
4283 format_paragraph (edit, 0);
4284 edit->force |= REDRAW_PAGE;
4289 /* --------------------------------------------------------------------------------------------- */
4291 void
4292 edit_stack_init (void)
4294 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4296 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4297 edit_history_moveto[edit_stack_iterator].line = -1;
4300 edit_stack_iterator = 0;
4303 /* --------------------------------------------------------------------------------------------- */
4305 void
4306 edit_stack_free (void)
4308 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4309 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4312 /* --------------------------------------------------------------------------------------------- */
4313 /** move i lines */
4315 void
4316 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4318 edit_move_updown (edit, i, do_scroll, TRUE);
4321 /* --------------------------------------------------------------------------------------------- */
4322 /** move i lines */
4324 void
4325 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4327 edit_move_updown (edit, i, do_scroll, FALSE);
4330 /* --------------------------------------------------------------------------------------------- */