(edit_insert_over): move from src/editor/editcmd.c into src/editor/edit.c.
[midnight-commander.git] / src / editor / edit.c
blobb33b4ae4202bca0a9a5addfc8591589fc20f9664
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, 2013
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, 2013
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/learn.h" /* learn_keys */
68 #include "src/keybind-defaults.h"
70 #include "edit-impl.h"
71 #include "editwidget.h"
72 #ifdef HAVE_ASPELL
73 #include "spell.h"
74 #endif
76 /*** global variables ****************************************************************************/
78 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
79 int option_typewriter_wrap = 0;
80 int option_auto_para_formatting = 0;
81 int option_fill_tabs_with_spaces = 0;
82 int option_return_does_auto_indent = 1;
83 int option_backspace_through_tabs = 0;
84 int option_fake_half_tabs = 1;
85 int option_save_mode = EDIT_QUICK_SAVE;
86 int option_save_position = 1;
87 int option_max_undo = 32768;
88 int option_persistent_selections = 1;
89 int option_cursor_beyond_eol = 0;
90 int option_line_state = 0;
91 int option_line_state_width = 0;
92 gboolean option_cursor_after_inserted_block = FALSE;
94 int option_edit_right_extreme = 0;
95 int option_edit_left_extreme = 0;
96 int option_edit_top_extreme = 0;
97 int option_edit_bottom_extreme = 0;
98 int enable_show_tabs_tws = 1;
99 int option_check_nl_at_eof = 0;
100 int option_group_undo = 0;
101 int show_right_margin = 0;
103 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
104 char *option_backup_ext = NULL;
106 unsigned int edit_stack_iterator = 0;
107 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
111 /*** file scope macro definitions ****************************************************************/
113 #define TEMP_BUF_LEN 1024
115 #define space_width 1
117 /*** file scope type declarations ****************************************************************/
119 /*** file scope variables ************************************************************************/
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
127 const char *read, *write, *extension;
128 } all_filters[] =
130 /* *INDENT-OFF* */
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
133 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 /* *INDENT-ON* */
139 static off_t last_bracket = -1;
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
146 * here's a quick sketch of the layout: (don't run this through indent.)
148 * (b1 is buffers1 and b2 is buffers2)
151 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
152 * ______________________________________|______________________________________
154 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
155 * |-> |-> |-> |-> |-> |-> |
157 * _<------------------------->|<----------------->_
158 * WEdit->curs2 | WEdit->curs1
159 * ^ | ^
160 * | ^|^ |
161 * cursor ||| cursor
162 * |||
163 * file end|||file beginning
168 * This_is_some_file
169 * fin.
172 /* --------------------------------------------------------------------------------------------- */
174 * Initialize the buffers for an empty files.
177 static void
178 edit_init_buffers (WEdit * edit)
180 int j;
182 for (j = 0; j <= MAXBUFF; j++)
184 edit->buffers1[j] = NULL;
185 edit->buffers2[j] = NULL;
188 edit->curs1 = 0;
189 edit->curs2 = 0;
190 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
193 /* --------------------------------------------------------------------------------------------- */
196 * Load file OR text into buffers. Set cursor to the beginning of file.
198 * @return FALSE on error.
201 static gboolean
202 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
204 off_t buf, buf2;
205 int file = -1;
206 gboolean ret = FALSE;
208 edit->curs2 = edit->last_byte;
209 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
211 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
212 if (file == -1)
214 gchar *errmsg, *filename;
216 filename = vfs_path_to_str (filename_vpath);
217 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
218 g_free (filename);
219 edit_error_dialog (_("Error"), errmsg);
220 g_free (errmsg);
221 return FALSE;
224 if (!edit->buffers2[buf2])
225 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
229 if (mc_read (file,
230 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
231 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
232 break;
234 for (buf = buf2 - 1; buf >= 0; buf--)
236 /* edit->buffers2[0] is already allocated */
237 if (!edit->buffers2[buf])
238 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
239 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
240 break;
242 ret = TRUE;
244 while (FALSE);
246 if (!ret)
248 gchar *errmsg, *filename;
250 filename = vfs_path_to_str (filename_vpath);
251 errmsg = g_strdup_printf (_("Error reading %s"), filename);
252 g_free (filename);
253 edit_error_dialog (_("Error"), errmsg);
254 g_free (errmsg);
256 mc_close (file);
257 return ret;
260 /* --------------------------------------------------------------------------------------------- */
261 /** Return index of the filter or -1 is there is no appropriate filter */
263 static int
264 edit_find_filter (const vfs_path_t * filename_vpath)
266 size_t i, l, e;
267 char *filename;
269 if (filename_vpath == NULL)
270 return -1;
272 filename = vfs_path_to_str (filename_vpath);
273 l = strlen (filename);
274 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
276 e = strlen (all_filters[i].extension);
277 if (l > e)
278 if (!strcmp (all_filters[i].extension, filename + l - e))
280 g_free (filename);
281 return i;
284 g_free (filename);
285 return -1;
288 /* --------------------------------------------------------------------------------------------- */
290 static char *
291 edit_get_filter (const vfs_path_t * filename_vpath)
293 int i;
294 char *p, *quoted_name, *filename;
296 i = edit_find_filter (filename_vpath);
297 if (i < 0)
298 return NULL;
300 filename = vfs_path_to_str (filename_vpath);
301 quoted_name = name_quote (filename, 0);
302 g_free (filename);
303 p = g_strdup_printf (all_filters[i].read, quoted_name);
304 g_free (quoted_name);
305 return p;
308 /* --------------------------------------------------------------------------------------------- */
310 static off_t
311 edit_insert_stream (WEdit * edit, FILE * f)
313 int c;
314 off_t i = 0;
315 while ((c = fgetc (f)) >= 0)
317 edit_insert (edit, c);
318 i++;
320 return i;
323 /* --------------------------------------------------------------------------------------------- */
325 * Open file and create it if necessary.
327 * @param edit editor object
328 * @param filename_vpath file name
329 * @param st buffer for store stat info
330 * @return TRUE for success, FALSE for error.
333 static gboolean
334 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
336 int file;
337 gchar *errmsg = NULL;
339 /* Try opening an existing file */
340 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
341 if (file < 0)
344 * Try creating the file. O_EXCL prevents following broken links
345 * and opening existing files.
347 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
348 if (file < 0)
350 char *filename;
352 filename = vfs_path_to_str (filename_vpath);
353 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
354 g_free (filename);
355 goto cleanup;
358 /* New file, delete it if it's not modified or saved */
359 edit->delete_file = 1;
362 /* Check what we have opened */
363 if (mc_fstat (file, st) < 0)
365 char *filename;
367 filename = vfs_path_to_str (filename_vpath);
368 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
369 g_free (filename);
370 goto cleanup;
373 /* We want to open regular files only */
374 if (!S_ISREG (st->st_mode))
376 char *filename;
378 filename = vfs_path_to_str (filename_vpath);
379 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
380 g_free (filename);
381 goto cleanup;
385 * Don't delete non-empty files.
386 * O_EXCL should prevent it, but let's be on the safe side.
388 if (st->st_size > 0)
389 edit->delete_file = 0;
391 if (st->st_size >= SIZE_LIMIT)
393 char *filename;
395 filename = vfs_path_to_str (filename_vpath);
396 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
397 g_free (filename);
400 cleanup:
401 (void) mc_close (file);
403 if (errmsg != NULL)
405 edit_error_dialog (_("Error"), errmsg);
406 g_free (errmsg);
407 return FALSE;
409 return TRUE;
412 /* --------------------------------------------------------------------------------------------- */
415 * Open the file and load it into the buffers, either directly or using
416 * a filter. Return TRUE on success, FALSE on error.
418 * Fast loading (edit_load_file_fast) is used when the file size is
419 * known. In this case the data is read into the buffers by blocks.
420 * If the file size is not known, the data is loaded byte by byte in
421 * edit_insert_file.
423 * @param edit editor object
424 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
426 static gboolean
427 edit_load_file (WEdit * edit)
429 gboolean fast_load = TRUE;
431 /* Cannot do fast load if a filter is used */
432 if (edit_find_filter (edit->filename_vpath) >= 0)
433 fast_load = FALSE;
436 * FIXME: line end translation should disable fast loading as well
437 * Consider doing fseek() to the end and ftell() for the real size.
439 if (edit->filename_vpath != NULL)
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (edit->filename_vpath))
446 fast_load = FALSE;
448 /* If we are dealing with a real file, check that it exists */
449 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
451 edit_clean (edit);
452 return FALSE;
455 else
457 /* nothing to load */
458 fast_load = FALSE;
461 edit_init_buffers (edit);
463 if (fast_load)
465 edit->last_byte = edit->stat1.st_size;
466 edit_load_file_fast (edit, edit->filename_vpath);
467 /* If fast load was used, the number of lines wasn't calculated */
468 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
470 else
472 edit->last_byte = 0;
473 if (edit->filename_vpath != NULL
474 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
476 edit->undo_stack_disable = 1;
477 if (edit_insert_file (edit, edit->filename_vpath) < 0)
479 edit_clean (edit);
480 return FALSE;
482 edit->undo_stack_disable = 0;
485 edit->lb = LB_ASIS;
486 return TRUE;
489 /* --------------------------------------------------------------------------------------------- */
490 /** Restore saved cursor position in the file */
492 static void
493 edit_load_position (WEdit * edit)
495 long line, column;
496 off_t offset;
498 if (edit->filename_vpath == NULL
499 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
500 return;
502 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
504 if (line > 0)
506 edit_move_to_line (edit, line - 1);
507 edit->prev_col = column;
509 else if (offset > 0)
511 edit_cursor_move (edit, offset);
512 line = edit->curs_line;
513 edit->search_start = edit->curs1;
516 book_mark_restore (edit, BOOK_MARK_COLOR);
518 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
519 edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
522 /* --------------------------------------------------------------------------------------------- */
523 /** Save cursor position in the file */
525 static void
526 edit_save_position (WEdit * edit)
528 if (edit->filename_vpath == NULL
529 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
530 return;
532 book_mark_serialize (edit, BOOK_MARK_COLOR);
533 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
534 edit->serialized_bookmarks);
535 edit->serialized_bookmarks = NULL;
538 /* --------------------------------------------------------------------------------------------- */
539 /** Clean the WEdit stricture except the widget part */
541 static void
542 edit_purge_widget (WEdit * edit)
544 size_t len = sizeof (WEdit) - sizeof (Widget);
545 char *start = (char *) edit + sizeof (Widget);
546 memset (start, 0, len);
549 /* --------------------------------------------------------------------------------------------- */
552 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
553 then the file should be as it was when he loaded up. Then set edit->modified to 0.
556 static long
557 edit_pop_undo_action (WEdit * edit)
559 long c;
560 unsigned long sp = edit->undo_stack_pointer;
562 if (sp == edit->undo_stack_bottom)
563 return STACK_BOTTOM;
565 sp = (sp - 1) & edit->undo_stack_size_mask;
566 c = edit->undo_stack[sp];
567 if (c >= 0)
569 /* edit->undo_stack[sp] = '@'; */
570 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
571 return c;
574 if (sp == edit->undo_stack_bottom)
575 return STACK_BOTTOM;
577 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
578 if (edit->undo_stack[sp] == -2)
580 /* edit->undo_stack[sp] = '@'; */
581 edit->undo_stack_pointer = sp;
583 else
584 edit->undo_stack[sp]++;
586 return c;
589 static long
590 edit_pop_redo_action (WEdit * edit)
592 long c;
593 unsigned long sp = edit->redo_stack_pointer;
595 if (sp == edit->redo_stack_bottom)
596 return STACK_BOTTOM;
598 sp = (sp - 1) & edit->redo_stack_size_mask;
599 c = edit->redo_stack[sp];
600 if (c >= 0)
602 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
603 return c;
606 if (sp == edit->redo_stack_bottom)
607 return STACK_BOTTOM;
609 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
610 if (edit->redo_stack[sp] == -2)
611 edit->redo_stack_pointer = sp;
612 else
613 edit->redo_stack[sp]++;
615 return c;
618 static long
619 get_prev_undo_action (WEdit * edit)
621 long c;
622 unsigned long sp = edit->undo_stack_pointer;
624 if (sp == edit->undo_stack_bottom)
625 return STACK_BOTTOM;
627 sp = (sp - 1) & edit->undo_stack_size_mask;
628 c = edit->undo_stack[sp];
629 if (c >= 0)
630 return c;
632 if (sp == edit->undo_stack_bottom)
633 return STACK_BOTTOM;
635 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
636 return c;
639 /* --------------------------------------------------------------------------------------------- */
640 /** is called whenever a modification is made by one of the four routines below */
642 static void
643 edit_modification (WEdit * edit)
645 edit->caches_valid = FALSE;
647 /* raise lock when file modified */
648 if (!edit->modified && !edit->delete_file)
649 edit->locked = lock_file (edit->filename_vpath);
650 edit->modified = 1;
653 /* --------------------------------------------------------------------------------------------- */
655 static char *
656 edit_get_byte_ptr (const WEdit * edit, off_t byte_index)
658 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
659 return NULL;
661 if (byte_index >= edit->curs1)
663 off_t p;
665 p = edit->curs1 + edit->curs2 - byte_index - 1;
666 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
667 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
670 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
671 (byte_index & M_EDIT_BUF_SIZE));
674 /* --------------------------------------------------------------------------------------------- */
676 #ifdef HAVE_CHARSET
677 static int
678 edit_get_prev_utf (const WEdit * edit, off_t byte_index, int *char_width)
680 int i, res;
681 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
682 gchar *str;
683 gchar *cursor_buf_ptr;
685 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
687 *char_width = 0;
688 return 0;
691 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
692 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
693 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
695 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
696 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
698 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
700 *char_width = 1;
701 return *(cursor_buf_ptr - 1);
703 else
705 res = g_utf8_get_char_validated (str, -1);
707 if (res < 0)
709 *char_width = 1;
710 return *(cursor_buf_ptr - 1);
712 else
714 *char_width = cursor_buf_ptr - str;
715 return res;
719 #endif
721 /* --------------------------------------------------------------------------------------------- */
722 /* high level cursor movement commands */
723 /* --------------------------------------------------------------------------------------------- */
724 /** check whether cursor is in indent part of line
726 * @param edit editor object
728 * @return TRUE if cursor is in indent, FALSE otherwise
731 static gboolean
732 is_in_indent (const WEdit * edit)
734 off_t p;
736 for (p = edit_bol (edit, edit->curs1); p < edit->curs1; p++)
737 if (strchr (" \t", edit_get_byte (edit, p)) == NULL)
738 return FALSE;
740 return TRUE;
743 /* --------------------------------------------------------------------------------------------- */
744 /** check whether line in editor is blank or not
746 * @param edit editor object
747 * @param offset position in file
749 * @return TRUE if line in blank, FALSE otherwise
752 static gboolean
753 is_blank (const WEdit * edit, off_t offset)
755 off_t s, f;
756 int c;
758 s = edit_bol (edit, offset);
759 f = edit_eol (edit, offset) - 1;
760 while (s <= f)
762 c = edit_get_byte (edit, s++);
763 if (!isspace (c))
764 return FALSE;
766 return TRUE;
769 /* --------------------------------------------------------------------------------------------- */
770 /** returns the offset of line i */
772 static off_t
773 edit_find_line (WEdit * edit, long line)
775 long i, j = 0;
776 long m = 2000000000; /* what is the magic number? */
778 if (!edit->caches_valid)
780 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
781 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
782 /* three offsets that we *know* are line 0 at 0 and these two: */
783 edit->line_numbers[1] = edit->curs_line;
784 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
785 edit->line_numbers[2] = edit->total_lines;
786 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
787 edit->caches_valid = TRUE;
789 if (line >= edit->total_lines)
790 return edit->line_offsets[2];
791 if (line <= 0)
792 return 0;
793 /* find the closest known point */
794 for (i = 0; i < N_LINE_CACHES; i++)
796 long 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, gboolean do_scroll)
828 long i = 0;
830 if (edit->curs_line > 1)
832 if (!edit_line_is_blank (edit, edit->curs_line))
834 for (i = edit->curs_line - 1; i != 0; i--)
835 if (edit_line_is_blank (edit, i))
836 break;
838 else if (edit_line_is_blank (edit, edit->curs_line - 1))
840 for (i = edit->curs_line - 1; i != 0; i--)
841 if (!edit_line_is_blank (edit, i))
843 i++;
844 break;
847 else
849 for (i = edit->curs_line - 1; i != 0; i--)
850 if (edit_line_is_blank (edit, i))
851 break;
855 edit_move_up (edit, edit->curs_line - i, do_scroll);
858 /* --------------------------------------------------------------------------------------------- */
859 /** moves down until a blank line is reached, or until just
860 before a non-blank line is reached */
862 static void
863 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
865 long i;
867 if (edit->curs_line >= edit->total_lines - 1)
868 i = edit->total_lines;
869 else if (!edit_line_is_blank (edit, edit->curs_line))
871 for (i = edit->curs_line + 1; i != 0; i++)
872 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
873 break;
875 else if (edit_line_is_blank (edit, edit->curs_line + 1))
877 for (i = edit->curs_line + 1; i != 0; i++)
878 if (!edit_line_is_blank (edit, i) || i > edit->total_lines)
880 i--;
881 break;
884 else
886 for (i = edit->curs_line + 1; i != 0; i++)
887 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
888 break;
890 edit_move_down (edit, i - edit->curs_line, do_scroll);
893 /* --------------------------------------------------------------------------------------------- */
895 static void
896 edit_begin_page (WEdit * edit)
898 edit_update_curs_row (edit);
899 edit_move_up (edit, edit->curs_row, 0);
902 /* --------------------------------------------------------------------------------------------- */
904 static void
905 edit_end_page (WEdit * edit)
907 edit_update_curs_row (edit);
908 edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, 0);
912 /* --------------------------------------------------------------------------------------------- */
913 /** goto beginning of text */
915 static void
916 edit_move_to_top (WEdit * edit)
918 if (edit->curs_line)
920 edit_cursor_move (edit, -edit->curs1);
921 edit_move_to_prev_col (edit, 0);
922 edit->force |= REDRAW_PAGE;
923 edit->search_start = 0;
924 edit_update_curs_row (edit);
929 /* --------------------------------------------------------------------------------------------- */
930 /** goto end of text */
932 static void
933 edit_move_to_bottom (WEdit * edit)
935 if (edit->curs_line < edit->total_lines)
937 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
938 edit->start_display = edit->last_byte;
939 edit->start_line = edit->total_lines;
940 edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
941 edit->force |= REDRAW_PAGE;
945 /* --------------------------------------------------------------------------------------------- */
946 /** goto beginning of line */
948 static void
949 edit_cursor_to_bol (WEdit * edit)
951 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
952 edit->search_start = edit->curs1;
953 edit->prev_col = edit_get_col (edit);
954 edit->over_col = 0;
957 /* --------------------------------------------------------------------------------------------- */
958 /** goto end of line */
960 static void
961 edit_cursor_to_eol (WEdit * edit)
963 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
964 edit->search_start = edit->curs1;
965 edit->prev_col = edit_get_col (edit);
966 edit->over_col = 0;
969 /* --------------------------------------------------------------------------------------------- */
971 static unsigned long
972 my_type_of (int c)
974 int x, r = 0;
975 const char *p, *q;
976 const char option_chars_move_whole_word[] =
977 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
979 if (c == 0)
980 return 0;
981 if (c == '!')
983 if (*option_chars_move_whole_word == '!')
984 return 2;
985 return 0x80000000UL;
987 if (g_ascii_isupper ((gchar) c))
988 c = 'A';
989 else if (g_ascii_islower ((gchar) c))
990 c = 'a';
991 else if (g_ascii_isalpha (c))
992 c = 'a';
993 else if (isdigit (c))
994 c = '0';
995 else if (isspace (c))
996 c = ' ';
997 q = strchr (option_chars_move_whole_word, c);
998 if (!q)
999 return 0xFFFFFFFFUL;
1002 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1003 if (*p == '!')
1004 x <<= 1;
1005 r |= x;
1007 while ((q = strchr (q + 1, c)));
1008 return r;
1011 /* --------------------------------------------------------------------------------------------- */
1013 static void
1014 edit_left_word_move (WEdit * edit, int s)
1016 while (TRUE)
1018 int c1, c2;
1020 if (edit->column_highlight
1021 && edit->mark1 != edit->mark2
1022 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1023 break;
1024 edit_cursor_move (edit, -1);
1025 if (edit->curs1 == 0)
1026 break;
1027 c1 = edit_get_byte (edit, edit->curs1 - 1);
1028 c2 = edit_get_byte (edit, edit->curs1);
1029 if (c1 == '\n' || c2 == '\n')
1030 break;
1031 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1032 break;
1033 if (isspace (c1) && !isspace (c2))
1034 break;
1035 if (s != 0 && !isspace (c1) && isspace (c2))
1036 break;
1040 /* --------------------------------------------------------------------------------------------- */
1042 static void
1043 edit_left_word_move_cmd (WEdit * edit)
1045 edit_left_word_move (edit, 0);
1046 edit->force |= REDRAW_PAGE;
1049 /* --------------------------------------------------------------------------------------------- */
1051 static void
1052 edit_right_word_move (WEdit * edit, int s)
1054 while (TRUE)
1056 int c1, c2;
1058 if (edit->column_highlight
1059 && edit->mark1 != edit->mark2
1060 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1061 break;
1062 edit_cursor_move (edit, 1);
1063 if (edit->curs1 >= edit->last_byte)
1064 break;
1065 c1 = edit_get_byte (edit, edit->curs1 - 1);
1066 c2 = edit_get_byte (edit, edit->curs1);
1067 if (c1 == '\n' || c2 == '\n')
1068 break;
1069 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1070 break;
1071 if (isspace (c1) && !isspace (c2))
1072 break;
1073 if (s != 0 && !isspace (c1) && isspace (c2))
1074 break;
1078 /* --------------------------------------------------------------------------------------------- */
1080 static void
1081 edit_right_word_move_cmd (WEdit * edit)
1083 edit_right_word_move (edit, 0);
1084 edit->force |= REDRAW_PAGE;
1087 /* --------------------------------------------------------------------------------------------- */
1089 static void
1090 edit_right_char_move_cmd (WEdit * edit)
1092 int cw = 1;
1093 int c = 0;
1094 #ifdef HAVE_CHARSET
1095 if (edit->utf8)
1097 c = edit_get_utf (edit, edit->curs1, &cw);
1098 if (cw < 1)
1099 cw = 1;
1101 else
1102 #endif
1104 c = edit_get_byte (edit, edit->curs1);
1106 if (option_cursor_beyond_eol && c == '\n')
1108 edit->over_col++;
1110 else
1112 edit_cursor_move (edit, cw);
1116 /* --------------------------------------------------------------------------------------------- */
1118 static void
1119 edit_left_char_move_cmd (WEdit * edit)
1121 int cw = 1;
1122 if (edit->column_highlight
1123 && option_cursor_beyond_eol
1124 && edit->mark1 != edit->mark2
1125 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1126 return;
1127 #ifdef HAVE_CHARSET
1128 if (edit->utf8)
1130 edit_get_prev_utf (edit, edit->curs1, &cw);
1131 if (cw < 1)
1132 cw = 1;
1134 #endif
1135 if (option_cursor_beyond_eol && edit->over_col > 0)
1137 edit->over_col--;
1139 else
1141 edit_cursor_move (edit, -cw);
1145 /* --------------------------------------------------------------------------------------------- */
1146 /** Up or down cursor moving.
1147 direction = TRUE - move up
1148 = FALSE - move down
1151 static void
1152 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1154 long p;
1155 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1157 if (lines > l)
1158 lines = l;
1160 if (lines == 0)
1161 return;
1163 if (lines > 1)
1164 edit->force |= REDRAW_PAGE;
1165 if (do_scroll)
1167 if (direction)
1168 edit_scroll_upward (edit, lines);
1169 else
1170 edit_scroll_downward (edit, lines);
1172 p = edit_bol (edit, edit->curs1);
1174 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1176 edit_cursor_move (edit, p - edit->curs1);
1178 edit_move_to_prev_col (edit, p);
1180 /* search start of current multibyte char (like CJK) */
1181 if (edit->curs1 + 1 < edit->last_byte)
1183 edit_right_char_move_cmd (edit);
1184 edit_left_char_move_cmd (edit);
1187 edit->search_start = edit->curs1;
1188 edit->found_len = 0;
1191 /* --------------------------------------------------------------------------------------------- */
1193 static void
1194 edit_right_delete_word (WEdit * edit)
1196 while (edit->curs1 < edit->last_byte)
1198 int c1, c2;
1200 c1 = edit_delete (edit, TRUE);
1201 c2 = edit_get_byte (edit, edit->curs1);
1202 if (c1 == '\n' || c2 == '\n')
1203 break;
1204 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1205 break;
1206 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1207 break;
1211 /* --------------------------------------------------------------------------------------------- */
1213 static void
1214 edit_left_delete_word (WEdit * edit)
1216 while (edit->curs1 > 0)
1218 int c1, c2;
1220 c1 = edit_backspace (edit, TRUE);
1221 c2 = edit_get_byte (edit, edit->curs1 - 1);
1222 if (c1 == '\n' || c2 == '\n')
1223 break;
1224 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1225 break;
1226 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1227 break;
1231 /* --------------------------------------------------------------------------------------------- */
1233 the start column position is not recorded, and hence does not
1234 undo as it happed. But who would notice.
1237 static void
1238 edit_do_undo (WEdit * edit)
1240 long ac;
1241 long count = 0;
1243 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1244 edit->over_col = 0;
1245 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1247 switch ((int) ac)
1249 case STACK_BOTTOM:
1250 goto done_undo;
1251 case CURS_RIGHT:
1252 edit_cursor_move (edit, 1);
1253 break;
1254 case CURS_LEFT:
1255 edit_cursor_move (edit, -1);
1256 break;
1257 case BACKSPACE:
1258 case BACKSPACE_BR:
1259 edit_backspace (edit, TRUE);
1260 break;
1261 case DELCHAR:
1262 case DELCHAR_BR:
1263 edit_delete (edit, TRUE);
1264 break;
1265 case COLUMN_ON:
1266 edit->column_highlight = 1;
1267 break;
1268 case COLUMN_OFF:
1269 edit->column_highlight = 0;
1270 break;
1272 if (ac >= 256 && ac < 512)
1273 edit_insert_ahead (edit, ac - 256);
1274 if (ac >= 0 && ac < 256)
1275 edit_insert (edit, ac);
1277 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1279 edit->mark1 = ac - MARK_1;
1280 edit->column1 =
1281 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1283 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1285 edit->mark2 = ac - MARK_2;
1286 edit->column2 =
1287 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1289 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1291 edit->end_mark_curs = ac - MARK_CURS;
1293 if (count++)
1294 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1297 if (edit->start_display > ac - KEY_PRESS)
1299 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1300 edit->force |= REDRAW_PAGE;
1302 else if (edit->start_display < ac - KEY_PRESS)
1304 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1305 edit->force |= REDRAW_PAGE;
1307 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1308 edit_update_curs_row (edit);
1310 done_undo:
1311 edit->undo_stack_disable = 0;
1314 /* --------------------------------------------------------------------------------------------- */
1316 static void
1317 edit_do_redo (WEdit * edit)
1319 long ac;
1320 long count = 0;
1322 if (edit->redo_stack_reset)
1323 return;
1325 edit->over_col = 0;
1326 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1328 switch ((int) ac)
1330 case STACK_BOTTOM:
1331 goto done_redo;
1332 case CURS_RIGHT:
1333 edit_cursor_move (edit, 1);
1334 break;
1335 case CURS_LEFT:
1336 edit_cursor_move (edit, -1);
1337 break;
1338 case BACKSPACE:
1339 edit_backspace (edit, TRUE);
1340 break;
1341 case DELCHAR:
1342 edit_delete (edit, TRUE);
1343 break;
1344 case COLUMN_ON:
1345 edit->column_highlight = 1;
1346 break;
1347 case COLUMN_OFF:
1348 edit->column_highlight = 0;
1349 break;
1351 if (ac >= 256 && ac < 512)
1352 edit_insert_ahead (edit, ac - 256);
1353 if (ac >= 0 && ac < 256)
1354 edit_insert (edit, ac);
1356 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1358 edit->mark1 = ac - MARK_1;
1359 edit->column1 =
1360 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1362 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1364 edit->mark2 = ac - MARK_2;
1365 edit->column2 =
1366 (long) 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:
1390 /* --------------------------------------------------------------------------------------------- */
1392 static void
1393 edit_group_undo (WEdit * edit)
1395 long ac = KEY_PRESS;
1396 long cur_ac = KEY_PRESS;
1397 while (ac != STACK_BOTTOM && ac == cur_ac)
1399 cur_ac = get_prev_undo_action (edit);
1400 edit_do_undo (edit);
1401 ac = get_prev_undo_action (edit);
1402 /* exit from cycle if option_group_undo is not set,
1403 * and make single UNDO operation
1405 if (!option_group_undo)
1406 ac = STACK_BOTTOM;
1410 /* --------------------------------------------------------------------------------------------- */
1412 static void
1413 edit_delete_to_line_end (WEdit * edit)
1415 while (edit_get_byte (edit, edit->curs1) != '\n' && edit->curs2 != 0)
1416 edit_delete (edit, TRUE);
1419 /* --------------------------------------------------------------------------------------------- */
1421 static void
1422 edit_delete_to_line_begin (WEdit * edit)
1424 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 != 0)
1425 edit_backspace (edit, TRUE);
1428 /* --------------------------------------------------------------------------------------------- */
1430 static gboolean
1431 is_aligned_on_a_tab (WEdit * edit)
1433 long curs_col;
1435 edit_update_curs_col (edit);
1436 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1437 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1440 /* --------------------------------------------------------------------------------------------- */
1442 static gboolean
1443 right_of_four_spaces (WEdit * edit)
1445 int i, ch = 0;
1447 for (i = 1; i <= HALF_TAB_SIZE; i++)
1448 ch |= edit_get_byte (edit, edit->curs1 - i);
1450 return (ch == ' ' && is_aligned_on_a_tab (edit));
1453 /* --------------------------------------------------------------------------------------------- */
1455 static gboolean
1456 left_of_four_spaces (WEdit * edit)
1458 int i, ch = 0;
1460 for (i = 0; i < HALF_TAB_SIZE; i++)
1461 ch |= edit_get_byte (edit, edit->curs1 + i);
1463 return (ch == ' ' && is_aligned_on_a_tab (edit));
1466 /* --------------------------------------------------------------------------------------------- */
1468 static void
1469 edit_auto_indent (WEdit * edit)
1471 off_t p;
1472 char c;
1474 p = edit->curs1;
1475 /* use the previous line as a template */
1476 p = edit_move_backward (edit, p, 1);
1477 /* copy the leading whitespace of the line */
1478 while (TRUE)
1479 { /* no range check - the line _is_ \n-terminated */
1480 c = edit_get_byte (edit, p++);
1481 if (c != ' ' && c != '\t')
1482 break;
1483 edit_insert (edit, c);
1487 /* --------------------------------------------------------------------------------------------- */
1489 static inline void
1490 edit_double_newline (WEdit * edit)
1492 edit_insert (edit, '\n');
1493 if (edit_get_byte (edit, edit->curs1) == '\n' || edit_get_byte (edit, edit->curs1 - 2) == '\n')
1494 return;
1495 edit->force |= REDRAW_PAGE;
1496 edit_insert (edit, '\n');
1499 /* --------------------------------------------------------------------------------------------- */
1501 static void
1502 insert_spaces_tab (WEdit * edit, gboolean half)
1504 long i;
1506 edit_update_curs_col (edit);
1507 i = option_tab_spacing * space_width;
1508 if (half)
1509 i /= 2;
1510 if (i != 0)
1512 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1513 while (i > 0)
1515 edit_insert (edit, ' ');
1516 i -= space_width;
1521 /* --------------------------------------------------------------------------------------------- */
1523 static inline void
1524 edit_tab_cmd (WEdit * edit)
1526 if (option_fake_half_tabs && is_in_indent (edit))
1528 /* insert a half tab (usually four spaces) unless there is a
1529 half tab already behind, then delete it and insert a
1530 full tab. */
1531 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1532 insert_spaces_tab (edit, TRUE);
1533 else
1535 int i;
1537 for (i = 1; i <= HALF_TAB_SIZE; i++)
1538 edit_backspace (edit, TRUE);
1539 edit_insert (edit, '\t');
1542 else if (option_fill_tabs_with_spaces)
1543 insert_spaces_tab (edit, FALSE);
1544 else
1545 edit_insert (edit, '\t');
1548 /* --------------------------------------------------------------------------------------------- */
1550 static void
1551 check_and_wrap_line (WEdit * edit)
1553 off_t curs;
1554 int c;
1556 if (!option_typewriter_wrap)
1557 return;
1558 edit_update_curs_col (edit);
1559 if (edit->curs_col < option_word_wrap_line_length)
1560 return;
1561 curs = edit->curs1;
1562 while (TRUE)
1564 curs--;
1565 c = edit_get_byte (edit, curs);
1566 if (c == '\n' || curs <= 0)
1568 edit_insert (edit, '\n');
1569 return;
1571 if (c == ' ' || c == '\t')
1573 off_t current = edit->curs1;
1574 edit_cursor_move (edit, curs - edit->curs1 + 1);
1575 edit_insert (edit, '\n');
1576 edit_cursor_move (edit, current - edit->curs1 + 1);
1577 return;
1582 /* --------------------------------------------------------------------------------------------- */
1583 /** this find the matching bracket in either direction, and sets edit->bracket
1585 * @param edit editor object
1586 * @param in_screen seach only on the current screen
1587 * @param furthest_bracket_search count of the bytes for search
1589 * @return position of the found bracket (-1 if no match)
1592 static off_t
1593 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1595 const char *const b = "{}{[][()(", *p;
1596 int i = 1, a, inc = -1, c, d, n = 0;
1597 unsigned long j = 0;
1598 off_t q;
1599 edit_update_curs_row (edit);
1600 c = edit_get_byte (edit, edit->curs1);
1601 p = strchr (b, c);
1602 /* no limit */
1603 if (!furthest_bracket_search)
1604 furthest_bracket_search--;
1605 /* not on a bracket at all */
1606 if (p == NULL)
1607 return -1;
1608 /* the matching bracket */
1609 d = p[1];
1610 /* going left or right? */
1611 if (strchr ("{[(", c))
1612 inc = 1;
1613 for (q = edit->curs1 + inc;; q += inc)
1615 /* out of buffer? */
1616 if (q >= edit->last_byte || q < 0)
1617 break;
1618 a = edit_get_byte (edit, q);
1619 /* don't want to eat CPU */
1620 if (j++ > furthest_bracket_search)
1621 break;
1622 /* out of screen? */
1623 if (in_screen)
1625 if (q < edit->start_display)
1626 break;
1627 /* count lines if searching downward */
1628 if (inc > 0 && a == '\n')
1629 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1630 break;
1632 /* count bracket depth */
1633 i += (a == c) - (a == d);
1634 /* return if bracket depth is zero */
1635 if (i == 0)
1636 return q;
1638 /* no match */
1639 return -1;
1642 /* --------------------------------------------------------------------------------------------- */
1644 static inline void
1645 edit_goto_matching_bracket (WEdit * edit)
1647 off_t q;
1649 q = edit_get_bracket (edit, 0, 0);
1650 if (q >= 0)
1652 edit->bracket = edit->curs1;
1653 edit->force |= REDRAW_PAGE;
1654 edit_cursor_move (edit, q - edit->curs1);
1658 /* --------------------------------------------------------------------------------------------- */
1660 static void
1661 edit_move_block_to_right (WEdit * edit)
1663 off_t start_mark, end_mark;
1664 long cur_bol, start_bol;
1666 if (eval_marks (edit, &start_mark, &end_mark))
1667 return;
1669 start_bol = edit_bol (edit, start_mark);
1670 cur_bol = edit_bol (edit, end_mark - 1);
1674 edit_cursor_move (edit, cur_bol - edit->curs1);
1675 if (!edit_line_is_blank (edit, edit->curs_line))
1677 if (option_fill_tabs_with_spaces)
1678 insert_spaces_tab (edit, option_fake_half_tabs);
1679 else
1680 edit_insert (edit, '\t');
1681 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1684 if (cur_bol == 0)
1685 break;
1687 cur_bol = edit_bol (edit, cur_bol - 1);
1689 while (cur_bol >= start_bol);
1691 edit->force |= REDRAW_PAGE;
1694 /* --------------------------------------------------------------------------------------------- */
1696 static void
1697 edit_move_block_to_left (WEdit * edit)
1699 off_t start_mark, end_mark;
1700 off_t cur_bol, start_bol;
1701 int i;
1703 if (eval_marks (edit, &start_mark, &end_mark))
1704 return;
1706 start_bol = edit_bol (edit, start_mark);
1707 cur_bol = edit_bol (edit, end_mark - 1);
1711 int del_tab_width;
1712 int next_char;
1714 edit_cursor_move (edit, cur_bol - edit->curs1);
1716 if (option_fake_half_tabs)
1717 del_tab_width = HALF_TAB_SIZE;
1718 else
1719 del_tab_width = option_tab_spacing;
1721 next_char = edit_get_byte (edit, edit->curs1);
1722 if (next_char == '\t')
1723 edit_delete (edit, TRUE);
1724 else if (next_char == ' ')
1725 for (i = 1; i <= del_tab_width; i++)
1727 if (next_char == ' ')
1728 edit_delete (edit, TRUE);
1729 next_char = edit_get_byte (edit, edit->curs1);
1732 if (cur_bol == 0)
1733 break;
1735 cur_bol = edit_bol (edit, cur_bol - 1);
1737 while (cur_bol >= start_bol);
1739 edit->force |= REDRAW_PAGE;
1742 /* --------------------------------------------------------------------------------------------- */
1744 * prints at the cursor
1745 * @return number of chars printed
1748 static size_t
1749 edit_print_string (WEdit * e, const char *s)
1751 size_t i = 0;
1753 while (s[i] != '\0')
1754 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1755 e->force |= REDRAW_COMPLETELY;
1756 edit_update_screen (e);
1757 return i;
1760 /* --------------------------------------------------------------------------------------------- */
1762 static off_t
1763 edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * end_pos,
1764 long *col1, long *col2)
1766 off_t cursor;
1767 int col;
1768 off_t blocklen = -1, width = 0;
1769 unsigned char *data;
1771 cursor = edit->curs1;
1772 col = edit_get_col (edit);
1773 data = g_malloc0 (TEMP_BUF_LEN);
1775 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1777 off_t i;
1778 char *pn;
1780 pn = strchr ((char *) data, '\n');
1781 width = pn == NULL ? blocklen : pn - (char *) data;
1783 for (i = 0; i < blocklen; i++)
1785 if (data[i] != '\n')
1786 edit_insert (edit, data[i]);
1787 else
1788 { /* fill in and move to next line */
1789 long l;
1790 off_t p;
1792 if (edit_get_byte (edit, edit->curs1) != '\n')
1793 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1794 edit_insert (edit, ' ');
1796 for (p = edit->curs1; ; p++)
1798 if (p == edit->last_byte)
1800 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1801 edit_insert_ahead (edit, '\n');
1802 p++;
1803 break;
1805 if (edit_get_byte (edit, p) == '\n')
1807 p++;
1808 break;
1812 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1814 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1815 edit_insert (edit, ' ');
1819 *col1 = col;
1820 *col2 = col + width;
1821 *start_pos = cursor;
1822 *end_pos = edit->curs1;
1823 edit_cursor_move (edit, cursor - edit->curs1);
1824 g_free (data);
1826 return blocklen;
1829 /* --------------------------------------------------------------------------------------------- */
1830 /*** public functions ****************************************************************************/
1831 /* --------------------------------------------------------------------------------------------- */
1833 /** User edit menu, like user menu (F2) but only in editor. */
1835 void
1836 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1838 char *block_file;
1839 int nomark;
1840 off_t curs;
1841 off_t start_mark, end_mark;
1842 struct stat status;
1843 vfs_path_t *block_file_vpath;
1845 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1846 block_file_vpath = vfs_path_from_str (block_file);
1847 curs = edit->curs1;
1848 nomark = eval_marks (edit, &start_mark, &end_mark);
1849 if (nomark == 0)
1850 edit_save_block (edit, block_file, start_mark, end_mark);
1852 /* run shell scripts from menu */
1853 if (user_menu_cmd (edit, menu_file, selected_entry)
1854 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1856 int rc = 0;
1857 FILE *fd;
1859 /* i.e. we have marked block */
1860 if (nomark == 0)
1861 rc = edit_block_delete_cmd (edit);
1863 if (rc == 0)
1865 off_t ins_len;
1867 ins_len = edit_insert_file (edit, block_file_vpath);
1868 if (nomark == 0 && ins_len > 0)
1869 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1871 /* truncate block file */
1872 fd = fopen (block_file, "w");
1873 if (fd != NULL)
1874 fclose (fd);
1876 g_free (block_file);
1877 vfs_path_free (block_file_vpath);
1879 edit_cursor_move (edit, curs - edit->curs1);
1880 edit->force |= REDRAW_PAGE;
1881 send_message (edit, NULL, MSG_DRAW, 0, NULL);
1884 /* --------------------------------------------------------------------------------------------- */
1887 edit_get_byte (const WEdit * edit, off_t byte_index)
1889 char *p;
1891 p = edit_get_byte_ptr (edit, byte_index);
1893 return (p != NULL) ? *(unsigned char *) p : '\n';
1896 /* --------------------------------------------------------------------------------------------- */
1898 #ifdef HAVE_CHARSET
1900 edit_get_utf (const WEdit * edit, off_t byte_index, int *char_width)
1902 gchar *str = NULL;
1903 int res = -1;
1904 gunichar ch;
1905 gchar *next_ch = NULL;
1906 int width = 0;
1907 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1909 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1911 *char_width = 0;
1912 return '\n';
1915 str = edit_get_byte_ptr (edit, byte_index);
1917 if (str == NULL)
1919 *char_width = 0;
1920 return 0;
1923 res = g_utf8_get_char_validated (str, -1);
1925 if (res < 0)
1927 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1928 int i;
1929 for (i = 0; i < UTF8_CHAR_LEN; i++)
1930 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1931 utf8_buf[UTF8_CHAR_LEN] = '\0';
1932 str = utf8_buf;
1933 res = g_utf8_get_char_validated (str, -1);
1936 if (res < 0)
1938 ch = *str;
1939 width = 0;
1941 else
1943 ch = res;
1944 /* Calculate UTF-8 char width */
1945 next_ch = g_utf8_next_char (str);
1946 if (next_ch)
1948 width = next_ch - str;
1950 else
1952 ch = 0;
1953 width = 0;
1956 *char_width = width;
1957 return ch;
1959 #endif
1961 /* --------------------------------------------------------------------------------------------- */
1963 char *
1964 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1966 int i;
1967 char *p, *writename;
1968 const vfs_path_element_t *path_element;
1970 i = edit_find_filter (filename_vpath);
1971 if (i < 0)
1972 return NULL;
1974 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1975 writename = name_quote (path_element->path, 0);
1976 p = g_strdup_printf (all_filters[i].write, writename);
1977 g_free (writename);
1978 return p;
1981 /* --------------------------------------------------------------------------------------------- */
1983 * @param edit editor object
1984 * @param f value of stream file
1985 * @return the length of the file
1988 off_t
1989 edit_write_stream (WEdit * edit, FILE * f)
1991 long i;
1993 if (edit->lb == LB_ASIS)
1995 for (i = 0; i < edit->last_byte; i++)
1996 if (fputc (edit_get_byte (edit, i), f) < 0)
1997 break;
1998 return i;
2001 /* change line breaks */
2002 for (i = 0; i < edit->last_byte; i++)
2004 unsigned char c = edit_get_byte (edit, i);
2006 if (!(c == '\n' || c == '\r'))
2008 /* not line break */
2009 if (fputc (c, f) < 0)
2010 return i;
2012 else
2013 { /* (c == '\n' || c == '\r') */
2014 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2016 switch (edit->lb)
2018 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2019 /* put one line break unconditionally */
2020 if (fputc ('\n', f) < 0)
2021 return i;
2023 i++; /* 2 chars are processed */
2025 if (c == '\r' && c1 == '\n')
2026 /* Windows line break; go to the next char */
2027 break;
2029 if (c == '\r' && c1 == '\r')
2031 /* two Macintosh line breaks; put second line break */
2032 if (fputc ('\n', f) < 0)
2033 return i;
2034 break;
2037 if (fputc (c1, f) < 0)
2038 return i;
2039 break;
2041 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2042 /* put one line break unconditionally */
2043 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2044 return i;
2046 if (c == '\r' && c1 == '\n')
2047 /* Windows line break; go to the next char */
2048 i++;
2049 break;
2051 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2052 /* put one line break unconditionally */
2053 if (fputc ('\r', f) < 0)
2054 return i;
2056 i++; /* 2 chars are processed */
2058 if (c == '\r' && c1 == '\n')
2059 /* Windows line break; go to the next char */
2060 break;
2062 if (c == '\n' && c1 == '\n')
2064 /* two Windows line breaks; put second line break */
2065 if (fputc ('\r', f) < 0)
2066 return i;
2067 break;
2070 if (fputc (c1, f) < 0)
2071 return i;
2072 break;
2073 case LB_ASIS: /* default without changes */
2074 break;
2079 return edit->last_byte;
2082 /* --------------------------------------------------------------------------------------------- */
2084 gboolean
2085 is_break_char (char c)
2087 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2090 /* --------------------------------------------------------------------------------------------- */
2092 char *
2093 edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len,
2094 gsize * cut)
2096 off_t word_start;
2097 long cut_len = 0;
2098 GString *match_expr;
2099 int c1, c2;
2101 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2103 c1 = edit_get_byte (edit, word_start);
2104 c2 = edit_get_byte (edit, word_start - 1);
2106 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2107 break;
2110 match_expr = g_string_sized_new (16);
2114 c1 = edit_get_byte (edit, word_start + match_expr->len);
2115 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2116 g_string_append_c (match_expr, c1);
2118 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2120 *len = match_expr->len;
2121 *start = word_start;
2122 *cut = cut_len;
2124 return g_string_free (match_expr, FALSE);
2127 /* --------------------------------------------------------------------------------------------- */
2128 /** inserts a file at the cursor, returns count of inserted bytes on success */
2130 long
2131 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2133 char *p;
2134 off_t current;
2135 off_t ins_len = 0;
2137 p = edit_get_filter (filename_vpath);
2138 current = edit->curs1;
2140 if (p != NULL)
2142 FILE *f;
2144 f = (FILE *) popen (p, "r");
2145 if (f != NULL)
2147 edit_insert_stream (edit, f);
2149 /* Place cursor at the end of text selection */
2150 if (!option_cursor_after_inserted_block)
2152 ins_len = edit->curs1 - current;
2153 edit_cursor_move (edit, -ins_len);
2155 if (pclose (f) > 0)
2157 char *errmsg;
2159 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2160 edit_error_dialog (_("Error"), errmsg);
2161 g_free (errmsg);
2162 ins_len = -1;
2165 else
2167 char *errmsg;
2169 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2170 edit_error_dialog (_("Error"), errmsg);
2171 g_free (errmsg);
2172 ins_len = -1;
2174 g_free (p);
2176 else
2178 int file;
2179 off_t blocklen;
2180 int vertical_insertion = 0;
2181 char *buf;
2183 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2184 if (file == -1)
2185 return -1;
2187 buf = g_malloc0 (TEMP_BUF_LEN);
2188 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2189 if (blocklen > 0)
2191 /* if contain signature VERTICAL_MAGIC then it vertical block */
2192 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2193 vertical_insertion = 1;
2194 else
2195 mc_lseek (file, 0, SEEK_SET);
2198 if (vertical_insertion)
2200 off_t mark1, mark2;
2201 long c1, c2;
2203 blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2204 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2206 /* highlight inserted text then not persistent blocks */
2207 if (!option_persistent_selections && edit->modified)
2209 if (!edit->column_highlight)
2210 edit_push_undo_action (edit, COLUMN_OFF);
2211 edit->column_highlight = 1;
2214 else
2216 off_t i;
2218 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2220 for (i = 0; i < blocklen; i++)
2221 edit_insert (edit, buf[i]);
2223 /* highlight inserted text then not persistent blocks */
2224 if (!option_persistent_selections && edit->modified)
2226 edit_set_markers (edit, edit->curs1, current, 0, 0);
2227 if (edit->column_highlight)
2228 edit_push_undo_action (edit, COLUMN_ON);
2229 edit->column_highlight = 0;
2232 /* Place cursor at the end of text selection */
2233 if (!option_cursor_after_inserted_block)
2235 ins_len = edit->curs1 - current;
2236 edit_cursor_move (edit, -ins_len);
2240 edit->force |= REDRAW_PAGE;
2241 g_free (buf);
2242 mc_close (file);
2243 if (blocklen != 0)
2244 ins_len = 0;
2247 return ins_len;
2250 /* --------------------------------------------------------------------------------------------- */
2252 * Fill in the edit structure. Return NULL on failure. Pass edit as
2253 * NULL to allocate a new structure.
2255 * If line is 0, try to restore saved position. Otherwise put the
2256 * cursor on that line and show it in the middle of the screen.
2259 WEdit *
2260 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2261 long line)
2263 gboolean to_free = FALSE;
2265 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2266 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2268 if (edit != NULL)
2270 /* save some widget parameters */
2271 gboolean fullscreen = edit->fullscreen;
2272 int y_prev = edit->y_prev;
2273 int x_prev = edit->x_prev;
2274 int lines_prev = edit->lines_prev;
2275 int cols_prev = edit->cols_prev;
2277 edit_purge_widget (edit);
2279 /* restore saved parameters */
2280 edit->fullscreen = fullscreen;
2281 edit->y_prev = y_prev;
2282 edit->x_prev = x_prev;
2283 edit->lines_prev = lines_prev;
2284 edit->cols_prev = cols_prev;
2286 else
2288 #ifdef ENABLE_NLS
2290 * Expand option_whole_chars_search by national letters using
2291 * current locale
2294 static char option_whole_chars_search_buf[256];
2296 if (option_whole_chars_search_buf != option_whole_chars_search)
2298 size_t i;
2299 size_t len = str_term_width1 (option_whole_chars_search);
2301 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2303 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2305 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2307 option_whole_chars_search_buf[len++] = i;
2311 option_whole_chars_search_buf[len] = 0;
2312 option_whole_chars_search = option_whole_chars_search_buf;
2314 #endif /* ENABLE_NLS */
2315 edit = g_malloc0 (sizeof (WEdit));
2316 to_free = TRUE;
2318 init_widget (WIDGET (edit), y, x, lines, cols, NULL, NULL);
2319 edit->fullscreen = TRUE;
2320 edit_save_size (edit);
2323 edit->drag_state = MCEDIT_DRAG_NORMAL;
2325 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2326 edit->stat1.st_uid = getuid ();
2327 edit->stat1.st_gid = getgid ();
2328 edit->stat1.st_mtime = 0;
2330 edit->over_col = 0;
2331 edit->bracket = -1;
2332 edit->force |= REDRAW_PAGE;
2334 /* set file name before load file */
2335 edit_set_filename (edit, filename_vpath);
2337 edit->undo_stack_size = START_STACK_SIZE;
2338 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2339 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2341 edit->redo_stack_size = START_STACK_SIZE;
2342 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2343 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2345 #ifdef HAVE_CHARSET
2346 edit->utf8 = FALSE;
2347 edit->converter = str_cnv_from_term;
2348 edit_set_codeset (edit);
2349 #endif
2351 if (!edit_load_file (edit))
2353 /* edit_load_file already gives an error message */
2354 if (to_free)
2355 g_free (edit);
2356 return NULL;
2359 edit->loading_done = 1;
2360 edit->modified = 0;
2361 edit->locked = 0;
2362 edit_load_syntax (edit, NULL, NULL);
2363 edit_get_syntax_color (edit, -1);
2365 /* load saved cursor position */
2366 if ((line == 0) && option_save_position)
2367 edit_load_position (edit);
2368 else
2370 if (line <= 0)
2371 line = 1;
2372 edit_move_display (edit, line - 1);
2373 edit_move_to_line (edit, line - 1);
2376 edit_load_macro_cmd (edit);
2378 return edit;
2381 /* --------------------------------------------------------------------------------------------- */
2383 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2384 gboolean
2385 edit_clean (WEdit * edit)
2387 int j = 0;
2389 if (edit == NULL)
2390 return FALSE;
2392 /* a stale lock, remove it */
2393 if (edit->locked)
2394 edit->locked = unlock_file (edit->filename_vpath);
2396 /* save cursor position */
2397 if (option_save_position)
2398 edit_save_position (edit);
2399 else if (edit->serialized_bookmarks != NULL)
2400 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2402 /* File specified on the mcedit command line and never saved */
2403 if (edit->delete_file)
2404 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2406 edit_free_syntax_rules (edit);
2407 book_mark_flush (edit, -1);
2408 for (; j <= MAXBUFF; j++)
2410 g_free (edit->buffers1[j]);
2411 g_free (edit->buffers2[j]);
2414 g_free (edit->undo_stack);
2415 g_free (edit->redo_stack);
2416 vfs_path_free (edit->filename_vpath);
2417 vfs_path_free (edit->dir_vpath);
2418 mc_search_free (edit->search);
2419 edit->search = NULL;
2421 #ifdef HAVE_CHARSET
2422 if (edit->converter != str_cnv_from_term)
2423 str_close_conv (edit->converter);
2424 #endif
2426 edit_purge_widget (edit);
2428 return TRUE;
2431 /* --------------------------------------------------------------------------------------------- */
2434 * Load a new file into the editor and set line. If it fails, preserve the old file.
2435 * To do it, allocate a new widget, initialize it and, if the new file
2436 * was loaded, copy the data to the old widget.
2438 * @return TRUE on success, FALSE on failure.
2440 gboolean
2441 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2443 Widget *w = WIDGET (edit);
2444 WEdit *e;
2446 e = g_malloc0 (sizeof (WEdit));
2447 *WIDGET (e) = *w;
2448 /* save some widget parameters */
2449 e->fullscreen = edit->fullscreen;
2450 e->y_prev = edit->y_prev;
2451 e->x_prev = edit->x_prev;
2452 e->lines_prev = edit->lines_prev;
2453 e->cols_prev = edit->cols_prev;
2455 if (edit_init (e, w->y, w->x, w->lines, w->cols, filename_vpath, line) == NULL)
2457 g_free (e);
2458 return FALSE;
2461 edit_clean (edit);
2462 memcpy (edit, e, sizeof (WEdit));
2463 g_free (e);
2465 return TRUE;
2468 /* --------------------------------------------------------------------------------------------- */
2470 #ifdef HAVE_CHARSET
2471 void
2472 edit_set_codeset (WEdit * edit)
2474 const char *cp_id;
2476 cp_id =
2477 get_codepage_id (mc_global.source_codepage >=
2478 0 ? mc_global.source_codepage : mc_global.display_codepage);
2480 if (cp_id != NULL)
2482 GIConv conv;
2483 conv = str_crt_conv_from (cp_id);
2484 if (conv != INVALID_CONV)
2486 if (edit->converter != str_cnv_from_term)
2487 str_close_conv (edit->converter);
2488 edit->converter = conv;
2492 if (cp_id != NULL)
2493 edit->utf8 = str_isutf8 (cp_id);
2495 #endif
2498 /* --------------------------------------------------------------------------------------------- */
2501 * Recording stack for undo:
2502 * The following is an implementation of a compressed stack. Identical
2503 * pushes are recorded by a negative prefix indicating the number of times the
2504 * same char was pushed. This saves space for repeated curs-left or curs-right
2505 * delete etc.
2507 * eg:
2509 * pushed: stored:
2512 * b a
2513 * b -3
2514 * b b
2515 * c --> -4
2516 * c c
2517 * c d
2521 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2522 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2523 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2524 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2525 * position.
2527 * The only way the cursor moves or the buffer is changed is through the routines:
2528 * insert, backspace, insert_ahead, delete, and cursor_move.
2529 * These record the reverse undo movements onto the stack each time they are
2530 * called.
2532 * Each key press results in a set of actions (insert; delete ...). So each time
2533 * a key is pressed the current position of start_display is pushed as
2534 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2535 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2536 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2540 * @param edit editor object
2541 * @param c code of the action
2544 void
2545 edit_push_undo_action (WEdit * edit, long c)
2547 unsigned long sp = edit->undo_stack_pointer;
2548 unsigned long spm1;
2549 long *t;
2551 /* first enlarge the stack if necessary */
2552 if (sp > edit->undo_stack_size - 10)
2553 { /* say */
2554 if (option_max_undo < 256)
2555 option_max_undo = 256;
2556 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2558 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2559 if (t)
2561 edit->undo_stack = t;
2562 edit->undo_stack_size <<= 1;
2563 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2567 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2568 if (edit->undo_stack_disable)
2570 edit_push_redo_action (edit, KEY_PRESS);
2571 edit_push_redo_action (edit, c);
2572 return;
2575 if (edit->redo_stack_reset)
2576 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2578 if (edit->undo_stack_bottom != sp
2579 && spm1 != edit->undo_stack_bottom
2580 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2582 int d;
2583 if (edit->undo_stack[spm1] < 0)
2585 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2586 if (d == c && edit->undo_stack[spm1] > -1000000000)
2588 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2589 edit->undo_stack[spm1]--;
2590 return;
2593 else
2595 d = edit->undo_stack[spm1];
2596 if (d == c)
2598 if (c >= KEY_PRESS)
2599 return; /* --> no need to push multiple do-nothings */
2600 edit->undo_stack[sp] = -2;
2601 goto check_bottom;
2605 edit->undo_stack[sp] = c;
2607 check_bottom:
2608 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2610 /* if the sp wraps round and catches the undo_stack_bottom then erase
2611 * the first set of actions on the stack to make space - by moving
2612 * undo_stack_bottom forward one "key press" */
2613 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2614 if ((unsigned long) c == edit->undo_stack_bottom ||
2615 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2618 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2620 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2621 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2623 /*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: */
2624 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2625 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2627 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2631 void
2632 edit_push_redo_action (WEdit * edit, long c)
2634 unsigned long sp = edit->redo_stack_pointer;
2635 unsigned long spm1;
2636 long *t;
2637 /* first enlarge the stack if necessary */
2638 if (sp > edit->redo_stack_size - 10)
2639 { /* say */
2640 if (option_max_undo < 256)
2641 option_max_undo = 256;
2642 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2644 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2645 if (t)
2647 edit->redo_stack = t;
2648 edit->redo_stack_size <<= 1;
2649 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2653 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2655 if (edit->redo_stack_bottom != sp
2656 && spm1 != edit->redo_stack_bottom
2657 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2659 int d;
2660 if (edit->redo_stack[spm1] < 0)
2662 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2663 if (d == c && edit->redo_stack[spm1] > -1000000000)
2665 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2666 edit->redo_stack[spm1]--;
2667 return;
2670 else
2672 d = edit->redo_stack[spm1];
2673 if (d == c)
2675 if (c >= KEY_PRESS)
2676 return; /* --> no need to push multiple do-nothings */
2677 edit->redo_stack[sp] = -2;
2678 goto redo_check_bottom;
2682 edit->redo_stack[sp] = c;
2684 redo_check_bottom:
2685 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2687 /* if the sp wraps round and catches the redo_stack_bottom then erase
2688 * the first set of actions on the stack to make space - by moving
2689 * redo_stack_bottom forward one "key press" */
2690 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2691 if ((unsigned long) c == edit->redo_stack_bottom ||
2692 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2695 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2697 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2698 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2701 * If a single key produced enough pushes to wrap all the way round then
2702 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2703 * The stack is then initialised:
2706 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2707 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2708 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2711 /* --------------------------------------------------------------------------------------------- */
2713 Basic low level single character buffer alterations and movements at the cursor.
2714 Returns char passed over, inserted or removed.
2717 void
2718 edit_insert (WEdit * edit, int c)
2720 /* check if file has grown to large */
2721 if (edit->last_byte >= SIZE_LIMIT)
2722 return;
2724 /* first we must update the position of the display window */
2725 if (edit->curs1 < edit->start_display)
2727 edit->start_display++;
2728 if (c == '\n')
2729 edit->start_line++;
2732 /* Mark file as modified, unless the file hasn't been fully loaded */
2733 if (edit->loading_done)
2734 edit_modification (edit);
2736 /* now we must update some info on the file and check if a redraw is required */
2737 if (c == '\n')
2739 book_mark_inc (edit, edit->curs_line);
2740 edit->curs_line++;
2741 edit->total_lines++;
2742 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2745 /* save the reverse command onto the undo stack */
2746 /* ordinary char and not space */
2747 if (c > 32)
2748 edit_push_undo_action (edit, BACKSPACE);
2749 else
2750 edit_push_undo_action (edit, BACKSPACE_BR);
2751 /* update markers */
2752 edit->mark1 += (edit->mark1 > edit->curs1);
2753 edit->mark2 += (edit->mark2 > edit->curs1);
2754 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2756 /* add a new buffer if we've reached the end of the last one */
2757 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2758 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2760 /* perform the insertion */
2761 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2762 = (unsigned char) c;
2764 /* update file length */
2765 edit->last_byte++;
2767 /* update cursor position */
2768 edit->curs1++;
2771 /* --------------------------------------------------------------------------------------------- */
2772 /** same as edit_insert and move left */
2774 void
2775 edit_insert_ahead (WEdit * edit, int c)
2777 if (edit->last_byte >= SIZE_LIMIT)
2778 return;
2780 if (edit->curs1 < edit->start_display)
2782 edit->start_display++;
2783 if (c == '\n')
2784 edit->start_line++;
2786 edit_modification (edit);
2787 if (c == '\n')
2789 book_mark_inc (edit, edit->curs_line);
2790 edit->total_lines++;
2791 edit->force |= REDRAW_AFTER_CURSOR;
2793 /* ordinary char and not space */
2794 if (c > 32)
2795 edit_push_undo_action (edit, DELCHAR);
2796 else
2797 edit_push_undo_action (edit, DELCHAR_BR);
2799 edit->mark1 += (edit->mark1 >= edit->curs1);
2800 edit->mark2 += (edit->mark2 >= edit->curs1);
2801 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2803 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2804 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2805 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2806 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2808 edit->last_byte++;
2809 edit->curs2++;
2812 /* --------------------------------------------------------------------------------------------- */
2814 void
2815 edit_insert_over (WEdit * edit)
2817 long i;
2819 for (i = 0; i < edit->over_col; i++)
2820 edit_insert (edit, ' ');
2822 edit->over_col = 0;
2825 /* --------------------------------------------------------------------------------------------- */
2828 edit_delete (WEdit * edit, gboolean byte_delete)
2830 int p = 0;
2831 int cw = 1;
2832 int i;
2834 if (edit->curs2 == 0)
2835 return 0;
2837 #ifdef HAVE_CHARSET
2838 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2839 if (edit->utf8 && !byte_delete)
2841 edit_get_utf (edit, edit->curs1, &cw);
2842 if (cw < 1)
2843 cw = 1;
2845 #else
2846 (void) byte_delete;
2847 #endif
2849 if (edit->mark2 != edit->mark1)
2850 edit_push_markers (edit);
2852 for (i = 1; i <= cw; i++)
2854 if (edit->mark1 > edit->curs1)
2856 edit->mark1--;
2857 edit->end_mark_curs--;
2859 if (edit->mark2 > edit->curs1)
2860 edit->mark2--;
2861 if (edit->last_get_rule > edit->curs1)
2862 edit->last_get_rule--;
2864 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2865 ((edit->curs2 -
2866 1) & M_EDIT_BUF_SIZE) - 1];
2868 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2870 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2871 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2873 edit->last_byte--;
2874 edit->curs2--;
2875 edit_push_undo_action (edit, p + 256);
2878 edit_modification (edit);
2879 if (p == '\n')
2881 book_mark_dec (edit, edit->curs_line);
2882 edit->total_lines--;
2883 edit->force |= REDRAW_AFTER_CURSOR;
2885 if (edit->curs1 < edit->start_display)
2887 edit->start_display--;
2888 if (p == '\n')
2889 edit->start_line--;
2892 return p;
2895 /* --------------------------------------------------------------------------------------------- */
2898 edit_backspace (WEdit * edit, gboolean byte_delete)
2900 int p = 0;
2901 int cw = 1;
2902 int i;
2904 if (edit->curs1 == 0)
2905 return 0;
2907 if (edit->mark2 != edit->mark1)
2908 edit_push_markers (edit);
2910 #ifdef HAVE_CHARSET
2911 if (edit->utf8 && !byte_delete)
2913 edit_get_prev_utf (edit, edit->curs1, &cw);
2914 if (cw < 1)
2915 cw = 1;
2917 #else
2918 (void) byte_delete;
2919 #endif
2921 for (i = 1; i <= cw; i++)
2923 if (edit->mark1 >= edit->curs1)
2925 edit->mark1--;
2926 edit->end_mark_curs--;
2928 if (edit->mark2 >= edit->curs1)
2929 edit->mark2--;
2930 if (edit->last_get_rule >= edit->curs1)
2931 edit->last_get_rule--;
2933 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2934 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2935 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2937 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2938 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2940 edit->last_byte--;
2941 edit->curs1--;
2942 edit_push_undo_action (edit, p);
2944 edit_modification (edit);
2945 if (p == '\n')
2947 book_mark_dec (edit, edit->curs_line);
2948 edit->curs_line--;
2949 edit->total_lines--;
2950 edit->force |= REDRAW_AFTER_CURSOR;
2953 if (edit->curs1 < edit->start_display)
2955 edit->start_display--;
2956 if (p == '\n')
2957 edit->start_line--;
2960 return p;
2963 /* --------------------------------------------------------------------------------------------- */
2964 /** moves the cursor right or left: increment positive or negative respectively */
2966 void
2967 edit_cursor_move (WEdit * edit, off_t increment)
2969 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2970 int c;
2972 if (increment < 0)
2974 for (; increment < 0; increment++)
2976 if (edit->curs1 == 0)
2977 return;
2979 edit_push_undo_action (edit, CURS_RIGHT);
2981 c = edit_get_byte (edit, edit->curs1 - 1);
2982 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2983 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2984 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2985 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2986 edit->curs2++;
2987 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2988 1) & M_EDIT_BUF_SIZE];
2989 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2991 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2992 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2994 edit->curs1--;
2995 if (c == '\n')
2997 edit->curs_line--;
2998 edit->force |= REDRAW_LINE_BELOW;
3003 else
3005 for (; increment > 0; increment--)
3007 if (edit->curs2 == 0)
3008 return;
3010 edit_push_undo_action (edit, CURS_LEFT);
3012 c = edit_get_byte (edit, edit->curs1);
3013 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
3014 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
3015 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
3016 edit->curs1++;
3017 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
3018 ((edit->curs2 -
3019 1) & M_EDIT_BUF_SIZE) - 1];
3020 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
3022 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
3023 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
3025 edit->curs2--;
3026 if (c == '\n')
3028 edit->curs_line++;
3029 edit->force |= REDRAW_LINE_ABOVE;
3035 /* These functions return positions relative to lines */
3037 /* --------------------------------------------------------------------------------------------- */
3038 /** returns index of last char on line + 1 */
3040 off_t
3041 edit_eol (const WEdit * edit, off_t current)
3043 if (current >= edit->last_byte)
3044 return edit->last_byte;
3046 for (; edit_get_byte (edit, current) != '\n'; current++)
3049 return current;
3052 /* --------------------------------------------------------------------------------------------- */
3053 /** returns index of first char on line */
3055 off_t
3056 edit_bol (const WEdit * edit, off_t current)
3058 if (current <= 0)
3059 return 0;
3061 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
3064 return current;
3067 /* --------------------------------------------------------------------------------------------- */
3069 long
3070 edit_count_lines (const WEdit * edit, off_t current, off_t upto)
3072 long lines = 0;
3074 if (upto > edit->last_byte)
3075 upto = edit->last_byte;
3076 if (current < 0)
3077 current = 0;
3078 while (current < upto)
3079 if (edit_get_byte (edit, current++) == '\n')
3080 lines++;
3081 return lines;
3084 /* --------------------------------------------------------------------------------------------- */
3085 /* If lines is zero this returns the count of lines from current to upto. */
3086 /* If upto is zero returns index of lines forward current. */
3088 off_t
3089 edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto)
3091 if (upto != 0)
3093 return (off_t) edit_count_lines (edit, current, upto);
3095 else
3097 long next;
3098 if (lines < 0)
3099 lines = 0;
3100 while (lines-- != 0)
3102 next = edit_eol (edit, current) + 1;
3103 if (next > edit->last_byte)
3104 break;
3105 else
3106 current = next;
3108 return current;
3112 /* --------------------------------------------------------------------------------------------- */
3113 /** Returns offset of 'lines' lines up from current */
3115 off_t
3116 edit_move_backward (const WEdit * edit, off_t current, long lines)
3118 if (lines < 0)
3119 lines = 0;
3120 current = edit_bol (edit, current);
3121 while (lines-- != 0 && current != 0)
3122 current = edit_bol (edit, current - 1);
3123 return current;
3126 /* --------------------------------------------------------------------------------------------- */
3127 /* If cols is zero this returns the count of columns from current to upto. */
3128 /* If upto is zero returns index of cols across from current. */
3130 off_t
3131 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
3133 off_t p, q;
3134 long col;
3136 if (upto != 0)
3138 q = upto;
3139 cols = -10;
3141 else
3142 q = edit->last_byte + 2;
3144 for (col = 0, p = current; p < q; p++)
3146 int c, orig_c;
3148 if (cols != -10)
3150 if (col == cols)
3151 return p;
3152 if (col > cols)
3153 return p - 1;
3156 orig_c = c = edit_get_byte (edit, p);
3158 #ifdef HAVE_CHARSET
3159 if (edit->utf8)
3161 int utf_ch;
3162 int cw = 1;
3164 utf_ch = edit_get_utf (edit, p, &cw);
3165 if (mc_global.utf8_display)
3167 if (cw > 1)
3168 col -= cw - 1;
3169 if (g_unichar_iswide (utf_ch))
3170 col++;
3172 else if (cw > 1 && g_unichar_isprint (utf_ch))
3173 col -= cw - 1;
3176 c = convert_to_display_c (c);
3177 #endif
3179 if (c == '\n')
3180 return (upto != 0 ? (off_t) col : p);
3181 if (c == '\t')
3182 col += TAB_SIZE - col % TAB_SIZE;
3183 else if ((c < 32 || c == 127) && (orig_c == c
3184 #ifdef HAVE_CHARSET
3185 || (!mc_global.utf8_display && !edit->utf8)
3186 #endif
3188 /* '\r' is shown as ^M, so we must advance 2 characters */
3189 /* Caret notation for control characters */
3190 col += 2;
3191 else
3192 col++;
3194 return (off_t) col;
3197 /* --------------------------------------------------------------------------------------------- */
3198 /** returns the current column position of the cursor */
3200 long
3201 edit_get_col (const WEdit * edit)
3203 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3206 /* --------------------------------------------------------------------------------------------- */
3207 /* Scrolling functions */
3208 /* --------------------------------------------------------------------------------------------- */
3210 void
3211 edit_update_curs_row (WEdit * edit)
3213 edit->curs_row = edit->curs_line - edit->start_line;
3216 /* --------------------------------------------------------------------------------------------- */
3218 void
3219 edit_update_curs_col (WEdit * edit)
3221 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3224 /* --------------------------------------------------------------------------------------------- */
3226 long
3227 edit_get_curs_col (const WEdit * edit)
3229 return edit->curs_col;
3232 /* --------------------------------------------------------------------------------------------- */
3233 /** moves the display start position up by i lines */
3235 void
3236 edit_scroll_upward (WEdit * edit, long i)
3238 long lines_above = edit->start_line;
3240 if (i > lines_above)
3241 i = lines_above;
3242 if (i != 0)
3244 edit->start_line -= i;
3245 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3246 edit->force |= REDRAW_PAGE;
3247 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3249 edit_update_curs_row (edit);
3253 /* --------------------------------------------------------------------------------------------- */
3255 void
3256 edit_scroll_downward (WEdit * edit, long i)
3258 long lines_below;
3260 lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1);
3261 if (lines_below > 0)
3263 if (i > lines_below)
3264 i = lines_below;
3265 edit->start_line += i;
3266 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3267 edit->force |= REDRAW_PAGE;
3268 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3270 edit_update_curs_row (edit);
3273 /* --------------------------------------------------------------------------------------------- */
3275 void
3276 edit_scroll_right (WEdit * edit, long i)
3278 edit->force |= REDRAW_PAGE;
3279 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3280 edit->start_col -= i;
3283 /* --------------------------------------------------------------------------------------------- */
3285 void
3286 edit_scroll_left (WEdit * edit, long i)
3288 if (edit->start_col)
3290 edit->start_col += i;
3291 if (edit->start_col > 0)
3292 edit->start_col = 0;
3293 edit->force |= REDRAW_PAGE;
3294 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3298 /* --------------------------------------------------------------------------------------------- */
3299 /* high level cursor movement commands */
3300 /* --------------------------------------------------------------------------------------------- */
3302 void
3303 edit_move_to_prev_col (WEdit * edit, off_t p)
3305 long prev = edit->prev_col;
3306 long over = edit->over_col;
3308 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3310 if (option_cursor_beyond_eol)
3312 long line_len;
3314 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3315 edit_eol (edit, edit->curs1));
3316 if (line_len < prev + edit->over_col)
3318 edit->over_col = prev + over - line_len;
3319 edit->prev_col = line_len;
3320 edit->curs_col = line_len;
3322 else
3324 edit->curs_col = prev + over;
3325 edit->prev_col = edit->curs_col;
3326 edit->over_col = 0;
3329 else
3331 edit->over_col = 0;
3332 if (option_fake_half_tabs && is_in_indent (edit))
3334 long fake_half_tabs;
3336 edit_update_curs_col (edit);
3338 fake_half_tabs = HALF_TAB_SIZE * space_width;
3339 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3341 int q;
3343 q = edit->curs_col;
3344 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3345 p = edit_bol (edit, edit->curs1);
3346 edit_cursor_move (edit,
3347 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
3348 if (!left_of_four_spaces (edit))
3349 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3355 /* --------------------------------------------------------------------------------------------- */
3356 /** check whether line in editor is blank or not
3358 * @param edit editor object
3359 * @param line number of line
3361 * @return TRUE if line in blank, FALSE otherwise
3364 gboolean
3365 edit_line_is_blank (WEdit * edit, long line)
3367 return is_blank (edit, edit_find_line (edit, line));
3370 /* --------------------------------------------------------------------------------------------- */
3371 /** move cursor to line 'line' */
3373 void
3374 edit_move_to_line (WEdit * e, long line)
3376 if (line < e->curs_line)
3377 edit_move_up (e, e->curs_line - line, 0);
3378 else
3379 edit_move_down (e, line - e->curs_line, 0);
3380 edit_scroll_screen_over_cursor (e);
3383 /* --------------------------------------------------------------------------------------------- */
3384 /** scroll window so that first visible line is 'line' */
3386 void
3387 edit_move_display (WEdit * e, long line)
3389 if (line < e->start_line)
3390 edit_scroll_upward (e, e->start_line - line);
3391 else
3392 edit_scroll_downward (e, line - e->start_line);
3395 /* --------------------------------------------------------------------------------------------- */
3396 /** save markers onto undo stack */
3398 void
3399 edit_push_markers (WEdit * edit)
3401 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3402 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3403 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3406 /* --------------------------------------------------------------------------------------------- */
3408 void
3409 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3411 edit->mark1 = m1;
3412 edit->mark2 = m2;
3413 edit->column1 = c1;
3414 edit->column2 = c2;
3418 /* --------------------------------------------------------------------------------------------- */
3419 /** highlight marker toggle */
3421 void
3422 edit_mark_cmd (WEdit * edit, gboolean unmark)
3424 edit_push_markers (edit);
3425 if (unmark)
3427 edit_set_markers (edit, 0, 0, 0, 0);
3428 edit->force |= REDRAW_PAGE;
3430 else if (edit->mark2 >= 0)
3432 edit->end_mark_curs = -1;
3433 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3434 edit->curs_col + edit->over_col);
3435 edit->force |= REDRAW_PAGE;
3437 else
3439 edit->end_mark_curs = edit->curs1;
3440 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3441 edit->curs_col + edit->over_col);
3445 /* --------------------------------------------------------------------------------------------- */
3446 /** highlight the word under cursor */
3448 void
3449 edit_mark_current_word_cmd (WEdit * edit)
3451 long pos;
3453 for (pos = edit->curs1; pos != 0; pos--)
3455 int c1, c2;
3457 c1 = edit_get_byte (edit, pos);
3458 c2 = edit_get_byte (edit, pos - 1);
3459 if (!isspace (c1) && isspace (c2))
3460 break;
3461 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3462 break;
3464 edit->mark1 = pos;
3466 for (; pos < edit->last_byte; pos++)
3468 int c1, c2;
3470 c1 = edit_get_byte (edit, pos);
3471 c2 = edit_get_byte (edit, pos + 1);
3472 if (!isspace (c1) && isspace (c2))
3473 break;
3474 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3475 break;
3477 edit->mark2 = min (pos + 1, edit->last_byte);
3479 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3482 /* --------------------------------------------------------------------------------------------- */
3484 void
3485 edit_mark_current_line_cmd (WEdit * edit)
3487 long pos = edit->curs1;
3489 edit->mark1 = edit_bol (edit, pos);
3490 edit->mark2 = edit_eol (edit, pos);
3492 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3495 /* --------------------------------------------------------------------------------------------- */
3497 void
3498 edit_delete_line (WEdit * edit)
3501 * Delete right part of the line.
3502 * Note that edit_get_byte() returns '\n' when byte position is
3503 * beyond EOF.
3505 while (edit_get_byte (edit, edit->curs1) != '\n')
3506 (void) edit_delete (edit, TRUE);
3509 * Delete '\n' char.
3510 * Note that edit_delete() will not corrupt anything if called while
3511 * cursor position is EOF.
3513 (void) edit_delete (edit, TRUE);
3516 * Delete left part of the line.
3517 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3519 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3520 (void) edit_backspace (edit, TRUE);
3523 /* --------------------------------------------------------------------------------------------- */
3525 long
3526 edit_indent_width (const WEdit * edit, off_t p)
3528 off_t q = p;
3530 /* move to the end of the leading whitespace of the line */
3531 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3532 q++;
3533 /* count the number of columns of indentation */
3534 return (long) edit_move_forward3 (edit, p, 0, q);
3537 /* --------------------------------------------------------------------------------------------- */
3539 void
3540 edit_insert_indent (WEdit * edit, int indent)
3542 if (!option_fill_tabs_with_spaces)
3544 while (indent >= TAB_SIZE)
3546 edit_insert (edit, '\t');
3547 indent -= TAB_SIZE;
3550 while (indent-- > 0)
3551 edit_insert (edit, ' ');
3554 /* --------------------------------------------------------------------------------------------- */
3556 void
3557 edit_push_key_press (WEdit * edit)
3559 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3560 if (edit->mark2 == -1)
3562 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3563 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3567 /* --------------------------------------------------------------------------------------------- */
3569 void
3570 edit_find_bracket (WEdit * edit)
3572 edit->bracket = edit_get_bracket (edit, 1, 10000);
3573 if (last_bracket != edit->bracket)
3574 edit->force |= REDRAW_PAGE;
3575 last_bracket = edit->bracket;
3578 /* --------------------------------------------------------------------------------------------- */
3580 * This executes a command as though the user initiated it through a key
3581 * press. Callback with MSG_KEY as a message calls this after
3582 * translating the key press. This function can be used to pass any
3583 * command to the editor. Note that the screen wouldn't update
3584 * automatically. Either of command or char_for_insertion must be
3585 * passed as -1. Commands are executed, and char_for_insertion is
3586 * inserted at the cursor.
3589 void
3590 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3592 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3593 || (macro_index < 0
3594 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3596 macro_index = 0;
3597 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3598 return;
3600 if (macro_index != -1)
3602 edit->force |= REDRAW_COMPLETELY;
3603 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3605 edit_store_macro_cmd (edit);
3606 macro_index = -1;
3607 return;
3609 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3611 edit_repeat_macro_cmd (edit);
3612 macro_index = -1;
3613 return;
3617 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3619 record_macro_buf[macro_index].action = command;
3620 record_macro_buf[macro_index++].ch = char_for_insertion;
3622 /* record the beginning of a set of editing actions initiated by a key press */
3623 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3624 edit_push_key_press (edit);
3626 edit_execute_cmd (edit, command, char_for_insertion);
3627 if (edit->column_highlight)
3628 edit->force |= REDRAW_PAGE;
3631 /* --------------------------------------------------------------------------------------------- */
3633 This executes a command at a lower level than macro recording.
3634 It also does not push a key_press onto the undo stack. This means
3635 that if it is called many times, a single undo command will undo
3636 all of them. It also does not check for the Undo command.
3638 void
3639 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3641 Widget *w = WIDGET (edit);
3643 if (command == CK_WindowFullscreen)
3645 edit_toggle_fullscreen (edit);
3646 return;
3649 /* handle window state */
3650 if (edit_handle_move_resize (edit, command))
3651 return;
3653 edit->force |= REDRAW_LINE;
3655 /* The next key press will unhighlight the found string, so update
3656 * the whole page */
3657 if (edit->found_len || edit->column_highlight)
3658 edit->force |= REDRAW_PAGE;
3660 switch (command)
3662 /* a mark command with shift-arrow */
3663 case CK_MarkLeft:
3664 case CK_MarkRight:
3665 case CK_MarkToWordBegin:
3666 case CK_MarkToWordEnd:
3667 case CK_MarkToHome:
3668 case CK_MarkToEnd:
3669 case CK_MarkUp:
3670 case CK_MarkDown:
3671 case CK_MarkPageUp:
3672 case CK_MarkPageDown:
3673 case CK_MarkToFileBegin:
3674 case CK_MarkToFileEnd:
3675 case CK_MarkToPageBegin:
3676 case CK_MarkToPageEnd:
3677 case CK_MarkScrollUp:
3678 case CK_MarkScrollDown:
3679 case CK_MarkParagraphUp:
3680 case CK_MarkParagraphDown:
3681 /* a mark command with alt-arrow */
3682 case CK_MarkColumnPageUp:
3683 case CK_MarkColumnPageDown:
3684 case CK_MarkColumnLeft:
3685 case CK_MarkColumnRight:
3686 case CK_MarkColumnUp:
3687 case CK_MarkColumnDown:
3688 case CK_MarkColumnScrollUp:
3689 case CK_MarkColumnScrollDown:
3690 case CK_MarkColumnParagraphUp:
3691 case CK_MarkColumnParagraphDown:
3692 edit->column_highlight = 0;
3693 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3695 edit_mark_cmd (edit, TRUE); /* clear */
3696 edit_mark_cmd (edit, FALSE); /* marking on */
3698 edit->highlight = 1;
3699 break;
3701 /* any other command */
3702 default:
3703 if (edit->highlight)
3704 edit_mark_cmd (edit, FALSE); /* clear */
3705 edit->highlight = 0;
3708 /* first check for undo */
3709 if (command == CK_Undo)
3711 edit->redo_stack_reset = 0;
3712 edit_group_undo (edit);
3713 edit->found_len = 0;
3714 edit->prev_col = edit_get_col (edit);
3715 edit->search_start = edit->curs1;
3716 return;
3718 /* check for redo */
3719 if (command == CK_Redo)
3721 edit->redo_stack_reset = 0;
3722 edit_do_redo (edit);
3723 edit->found_len = 0;
3724 edit->prev_col = edit_get_col (edit);
3725 edit->search_start = edit->curs1;
3726 return;
3729 edit->redo_stack_reset = 1;
3731 /* An ordinary key press */
3732 if (char_for_insertion >= 0)
3734 /* if non persistent selection and text selected */
3735 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3736 edit_block_delete_cmd (edit);
3738 if (edit->overwrite)
3740 /* remove char only one time, after input first byte, multibyte chars */
3741 #ifdef HAVE_CHARSET
3742 if (!mc_global.utf8_display || edit->charpoint == 0)
3743 #endif
3744 if (edit_get_byte (edit, edit->curs1) != '\n')
3746 edit_delete (edit, FALSE);
3748 if (option_cursor_beyond_eol && edit->over_col > 0)
3749 edit_insert_over (edit);
3750 #ifdef HAVE_CHARSET
3751 if (char_for_insertion > 255 && !mc_global.utf8_display)
3753 unsigned char str[6 + 1];
3754 size_t i = 0;
3755 int res;
3757 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3758 if (res == 0)
3760 str[0] = '.';
3761 str[1] = '\0';
3763 else
3765 str[res] = '\0';
3767 while (str[i] != 0 && i <= 6)
3769 char_for_insertion = str[i];
3770 edit_insert (edit, char_for_insertion);
3771 i++;
3774 else
3775 #endif
3776 edit_insert (edit, char_for_insertion);
3778 if (option_auto_para_formatting)
3780 format_paragraph (edit, 0);
3781 edit->force |= REDRAW_PAGE;
3783 else
3784 check_and_wrap_line (edit);
3785 edit->found_len = 0;
3786 edit->prev_col = edit_get_col (edit);
3787 edit->search_start = edit->curs1;
3788 edit_find_bracket (edit);
3789 return;
3792 switch (command)
3794 case CK_TopOnScreen:
3795 case CK_BottomOnScreen:
3796 case CK_Top:
3797 case CK_Bottom:
3798 case CK_PageUp:
3799 case CK_PageDown:
3800 case CK_Home:
3801 case CK_End:
3802 case CK_Up:
3803 case CK_Down:
3804 case CK_Left:
3805 case CK_Right:
3806 case CK_WordLeft:
3807 case CK_WordRight:
3808 if (!option_persistent_selections && edit->mark2 >= 0)
3810 if (edit->column_highlight)
3811 edit_push_undo_action (edit, COLUMN_ON);
3812 edit->column_highlight = 0;
3813 edit_mark_cmd (edit, TRUE);
3817 switch (command)
3819 case CK_TopOnScreen:
3820 case CK_BottomOnScreen:
3821 case CK_MarkToPageBegin:
3822 case CK_MarkToPageEnd:
3823 case CK_Up:
3824 case CK_Down:
3825 case CK_WordLeft:
3826 case CK_WordRight:
3827 case CK_MarkToWordBegin:
3828 case CK_MarkToWordEnd:
3829 case CK_MarkUp:
3830 case CK_MarkDown:
3831 case CK_MarkColumnUp:
3832 case CK_MarkColumnDown:
3833 if (edit->mark2 == -1)
3834 break; /*marking is following the cursor: may need to highlight a whole line */
3835 case CK_Left:
3836 case CK_Right:
3837 case CK_MarkLeft:
3838 case CK_MarkRight:
3839 edit->force |= REDRAW_CHAR_ONLY;
3842 /* basic cursor key commands */
3843 switch (command)
3845 case CK_BackSpace:
3846 /* if non persistent selection and text selected */
3847 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3848 edit_block_delete_cmd (edit);
3849 else if (option_cursor_beyond_eol && edit->over_col > 0)
3850 edit->over_col--;
3851 else if (option_backspace_through_tabs && is_in_indent (edit))
3853 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3854 edit_backspace (edit, TRUE);
3856 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3858 int i;
3860 for (i = 0; i < HALF_TAB_SIZE; i++)
3861 edit_backspace (edit, TRUE);
3863 else
3864 edit_backspace (edit, FALSE);
3865 break;
3866 case CK_Delete:
3867 /* if non persistent selection and text selected */
3868 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3869 edit_block_delete_cmd (edit);
3870 else
3872 if (option_cursor_beyond_eol && edit->over_col > 0)
3873 edit_insert_over (edit);
3875 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3877 int i;
3879 for (i = 1; i <= HALF_TAB_SIZE; i++)
3880 edit_delete (edit, TRUE);
3882 else
3883 edit_delete (edit, FALSE);
3885 break;
3886 case CK_DeleteToWordBegin:
3887 edit->over_col = 0;
3888 edit_left_delete_word (edit);
3889 break;
3890 case CK_DeleteToWordEnd:
3891 if (option_cursor_beyond_eol && edit->over_col > 0)
3892 edit_insert_over (edit);
3894 edit_right_delete_word (edit);
3895 break;
3896 case CK_DeleteLine:
3897 edit_delete_line (edit);
3898 break;
3899 case CK_DeleteToHome:
3900 edit_delete_to_line_begin (edit);
3901 break;
3902 case CK_DeleteToEnd:
3903 edit_delete_to_line_end (edit);
3904 break;
3905 case CK_Enter:
3906 edit->over_col = 0;
3907 if (option_auto_para_formatting)
3909 edit_double_newline (edit);
3910 if (option_return_does_auto_indent)
3911 edit_auto_indent (edit);
3912 format_paragraph (edit, 0);
3914 else
3916 edit_insert (edit, '\n');
3917 if (option_return_does_auto_indent)
3918 edit_auto_indent (edit);
3920 break;
3921 case CK_Return:
3922 edit_insert (edit, '\n');
3923 break;
3925 case CK_MarkColumnPageUp:
3926 edit->column_highlight = 1;
3927 case CK_PageUp:
3928 case CK_MarkPageUp:
3929 edit_move_up (edit, w->lines - 1, 1);
3930 break;
3931 case CK_MarkColumnPageDown:
3932 edit->column_highlight = 1;
3933 case CK_PageDown:
3934 case CK_MarkPageDown:
3935 edit_move_down (edit, w->lines - 1, 1);
3936 break;
3937 case CK_MarkColumnLeft:
3938 edit->column_highlight = 1;
3939 case CK_Left:
3940 case CK_MarkLeft:
3941 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3943 if (option_cursor_beyond_eol && edit->over_col > 0)
3944 edit->over_col--;
3945 else
3946 edit_cursor_move (edit, -HALF_TAB_SIZE);
3947 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3949 else
3950 edit_left_char_move_cmd (edit);
3951 break;
3952 case CK_MarkColumnRight:
3953 edit->column_highlight = 1;
3954 case CK_Right:
3955 case CK_MarkRight:
3956 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3958 edit_cursor_move (edit, HALF_TAB_SIZE);
3959 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3961 else
3962 edit_right_char_move_cmd (edit);
3963 break;
3964 case CK_TopOnScreen:
3965 case CK_MarkToPageBegin:
3966 edit_begin_page (edit);
3967 break;
3968 case CK_BottomOnScreen:
3969 case CK_MarkToPageEnd:
3970 edit_end_page (edit);
3971 break;
3972 case CK_WordLeft:
3973 case CK_MarkToWordBegin:
3974 edit->over_col = 0;
3975 edit_left_word_move_cmd (edit);
3976 break;
3977 case CK_WordRight:
3978 case CK_MarkToWordEnd:
3979 edit->over_col = 0;
3980 edit_right_word_move_cmd (edit);
3981 break;
3982 case CK_MarkColumnUp:
3983 edit->column_highlight = 1;
3984 case CK_Up:
3985 case CK_MarkUp:
3986 edit_move_up (edit, 1, 0);
3987 break;
3988 case CK_MarkColumnDown:
3989 edit->column_highlight = 1;
3990 case CK_Down:
3991 case CK_MarkDown:
3992 edit_move_down (edit, 1, 0);
3993 break;
3994 case CK_MarkColumnParagraphUp:
3995 edit->column_highlight = 1;
3996 case CK_ParagraphUp:
3997 case CK_MarkParagraphUp:
3998 edit_move_up_paragraph (edit, 0);
3999 break;
4000 case CK_MarkColumnParagraphDown:
4001 edit->column_highlight = 1;
4002 case CK_ParagraphDown:
4003 case CK_MarkParagraphDown:
4004 edit_move_down_paragraph (edit, 0);
4005 break;
4006 case CK_MarkColumnScrollUp:
4007 edit->column_highlight = 1;
4008 case CK_ScrollUp:
4009 case CK_MarkScrollUp:
4010 edit_move_up (edit, 1, 1);
4011 break;
4012 case CK_MarkColumnScrollDown:
4013 edit->column_highlight = 1;
4014 case CK_ScrollDown:
4015 case CK_MarkScrollDown:
4016 edit_move_down (edit, 1, 1);
4017 break;
4018 case CK_Home:
4019 case CK_MarkToHome:
4020 edit_cursor_to_bol (edit);
4021 break;
4022 case CK_End:
4023 case CK_MarkToEnd:
4024 edit_cursor_to_eol (edit);
4025 break;
4026 case CK_Tab:
4027 /* if text marked shift block */
4028 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4030 if (edit->mark2 < 0)
4031 edit_mark_cmd (edit, FALSE);
4032 edit_move_block_to_right (edit);
4034 else
4036 if (option_cursor_beyond_eol)
4037 edit_insert_over (edit);
4038 edit_tab_cmd (edit);
4039 if (option_auto_para_formatting)
4041 format_paragraph (edit, 0);
4042 edit->force |= REDRAW_PAGE;
4044 else
4045 check_and_wrap_line (edit);
4047 break;
4049 case CK_InsertOverwrite:
4050 edit->overwrite = !edit->overwrite;
4051 break;
4053 case CK_Mark:
4054 if (edit->mark2 >= 0)
4056 if (edit->column_highlight)
4057 edit_push_undo_action (edit, COLUMN_ON);
4058 edit->column_highlight = 0;
4060 edit_mark_cmd (edit, FALSE);
4061 break;
4062 case CK_MarkColumn:
4063 if (!edit->column_highlight)
4064 edit_push_undo_action (edit, COLUMN_OFF);
4065 edit->column_highlight = 1;
4066 edit_mark_cmd (edit, FALSE);
4067 break;
4068 case CK_MarkAll:
4069 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
4070 edit->force |= REDRAW_PAGE;
4071 break;
4072 case CK_Unmark:
4073 if (edit->column_highlight)
4074 edit_push_undo_action (edit, COLUMN_ON);
4075 edit->column_highlight = 0;
4076 edit_mark_cmd (edit, TRUE);
4077 break;
4078 case CK_MarkWord:
4079 if (edit->column_highlight)
4080 edit_push_undo_action (edit, COLUMN_ON);
4081 edit->column_highlight = 0;
4082 edit_mark_current_word_cmd (edit);
4083 break;
4084 case CK_MarkLine:
4085 if (edit->column_highlight)
4086 edit_push_undo_action (edit, COLUMN_ON);
4087 edit->column_highlight = 0;
4088 edit_mark_current_line_cmd (edit);
4089 break;
4091 case CK_Bookmark:
4092 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4093 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4094 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4095 else
4096 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4097 break;
4098 case CK_BookmarkFlush:
4099 book_mark_flush (edit, BOOK_MARK_COLOR);
4100 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4101 edit->force |= REDRAW_PAGE;
4102 break;
4103 case CK_BookmarkNext:
4104 if (edit->book_mark != NULL)
4106 edit_book_mark_t *p;
4108 p = book_mark_find (edit, edit->curs_line);
4109 if (p->next != NULL)
4111 p = p->next;
4112 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4113 edit_move_display (edit, p->line - w->lines / 2);
4114 edit_move_to_line (edit, p->line);
4117 break;
4118 case CK_BookmarkPrev:
4119 if (edit->book_mark != NULL)
4121 edit_book_mark_t *p;
4123 p = book_mark_find (edit, edit->curs_line);
4124 while (p->line == edit->curs_line)
4125 if (p->prev != NULL)
4126 p = p->prev;
4127 if (p->line >= 0)
4129 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4130 edit_move_display (edit, p->line - w->lines / 2);
4131 edit_move_to_line (edit, p->line);
4134 break;
4136 case CK_Top:
4137 case CK_MarkToFileBegin:
4138 edit_move_to_top (edit);
4139 break;
4140 case CK_Bottom:
4141 case CK_MarkToFileEnd:
4142 edit_move_to_bottom (edit);
4143 break;
4145 case CK_Copy:
4146 if (option_cursor_beyond_eol && edit->over_col > 0)
4147 edit_insert_over (edit);
4148 edit_block_copy_cmd (edit);
4149 break;
4150 case CK_Remove:
4151 edit_block_delete_cmd (edit);
4152 break;
4153 case CK_Move:
4154 edit_block_move_cmd (edit);
4155 break;
4157 case CK_BlockShiftLeft:
4158 if (edit->mark1 != edit->mark2)
4159 edit_move_block_to_left (edit);
4160 break;
4161 case CK_BlockShiftRight:
4162 if (edit->mark1 != edit->mark2)
4163 edit_move_block_to_right (edit);
4164 break;
4165 case CK_Store:
4166 edit_copy_to_X_buf_cmd (edit);
4167 break;
4168 case CK_Cut:
4169 edit_cut_to_X_buf_cmd (edit);
4170 break;
4171 case CK_Paste:
4172 /* if non persistent selection and text selected */
4173 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4174 edit_block_delete_cmd (edit);
4175 if (option_cursor_beyond_eol && edit->over_col > 0)
4176 edit_insert_over (edit);
4177 edit_paste_from_X_buf_cmd (edit);
4178 if (!option_persistent_selections && edit->mark2 >= 0)
4180 if (edit->column_highlight)
4181 edit_push_undo_action (edit, COLUMN_ON);
4182 edit->column_highlight = 0;
4183 edit_mark_cmd (edit, TRUE);
4185 break;
4186 case CK_History:
4187 edit_paste_from_history (edit);
4188 break;
4190 case CK_SaveAs:
4191 edit_save_as_cmd (edit);
4192 break;
4193 case CK_Save:
4194 edit_save_confirm_cmd (edit);
4195 break;
4196 case CK_BlockSave:
4197 edit_save_block_cmd (edit);
4198 break;
4199 case CK_InsertFile:
4200 edit_insert_file_cmd (edit);
4201 break;
4203 case CK_FilePrev:
4204 edit_load_back_cmd (edit);
4205 break;
4206 case CK_FileNext:
4207 edit_load_forward_cmd (edit);
4208 break;
4210 case CK_SyntaxChoose:
4211 edit_syntax_dialog (edit);
4212 break;
4214 case CK_Search:
4215 edit_search_cmd (edit, FALSE);
4216 break;
4217 case CK_SearchContinue:
4218 edit_search_cmd (edit, TRUE);
4219 break;
4220 case CK_Replace:
4221 edit_replace_cmd (edit, 0);
4222 break;
4223 case CK_ReplaceContinue:
4224 edit_replace_cmd (edit, 1);
4225 break;
4226 case CK_Complete:
4227 /* if text marked shift block */
4228 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4229 edit_move_block_to_left (edit);
4230 else
4231 edit_complete_word_cmd (edit);
4232 break;
4233 case CK_Find:
4234 edit_get_match_keyword_cmd (edit);
4235 break;
4237 #ifdef HAVE_ASPELL
4238 case CK_SpellCheckCurrentWord:
4239 edit_suggest_current_word (edit);
4240 break;
4241 case CK_SpellCheck:
4242 edit_spellcheck_file (edit);
4243 break;
4244 case CK_SpellCheckSelectLang:
4245 edit_set_spell_lang ();
4246 break;
4247 #endif
4249 case CK_Date:
4251 char s[BUF_MEDIUM];
4252 /* fool gcc to prevent a Y2K warning */
4253 char time_format[] = "_c";
4254 time_format[0] = '%';
4256 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4257 edit_print_string (edit, s);
4258 edit->force |= REDRAW_PAGE;
4260 break;
4261 case CK_Goto:
4262 edit_goto_cmd (edit);
4263 break;
4264 case CK_ParagraphFormat:
4265 format_paragraph (edit, 1);
4266 edit->force |= REDRAW_PAGE;
4267 break;
4268 case CK_MacroDelete:
4269 edit_delete_macro_cmd (edit);
4270 break;
4271 case CK_MatchBracket:
4272 edit_goto_matching_bracket (edit);
4273 break;
4274 case CK_UserMenu:
4275 user_menu (edit, NULL, -1);
4276 break;
4277 case CK_Sort:
4278 edit_sort_cmd (edit);
4279 break;
4280 case CK_ExternalCommand:
4281 edit_ext_cmd (edit);
4282 break;
4283 case CK_Mail:
4284 edit_mail_dialog (edit);
4285 break;
4286 #ifdef HAVE_CHARSET
4287 case CK_SelectCodepage:
4288 edit_select_codepage_cmd (edit);
4289 break;
4290 #endif
4291 case CK_InsertLiteral:
4292 edit_insert_literal_cmd (edit);
4293 break;
4294 case CK_MacroStartStopRecord:
4295 edit_begin_end_macro_cmd (edit);
4296 break;
4297 case CK_RepeatStartStopRecord:
4298 edit_begin_end_repeat_cmd (edit);
4299 break;
4300 case CK_ExtendedKeyMap:
4301 edit->extmod = TRUE;
4302 break;
4303 default:
4304 break;
4307 /* CK_PipeBlock */
4308 if ((command / CK_PipeBlock (0)) == 1)
4309 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4311 /* keys which must set the col position, and the search vars */
4312 switch (command)
4314 case CK_Search:
4315 case CK_SearchContinue:
4316 case CK_Replace:
4317 case CK_ReplaceContinue:
4318 case CK_Complete:
4319 edit->prev_col = edit_get_col (edit);
4320 break;
4321 case CK_Up:
4322 case CK_MarkUp:
4323 case CK_MarkColumnUp:
4324 case CK_Down:
4325 case CK_MarkDown:
4326 case CK_MarkColumnDown:
4327 case CK_PageUp:
4328 case CK_MarkPageUp:
4329 case CK_MarkColumnPageUp:
4330 case CK_PageDown:
4331 case CK_MarkPageDown:
4332 case CK_MarkColumnPageDown:
4333 case CK_Top:
4334 case CK_MarkToFileBegin:
4335 case CK_Bottom:
4336 case CK_MarkToFileEnd:
4337 case CK_ParagraphUp:
4338 case CK_MarkParagraphUp:
4339 case CK_MarkColumnParagraphUp:
4340 case CK_ParagraphDown:
4341 case CK_MarkParagraphDown:
4342 case CK_MarkColumnParagraphDown:
4343 case CK_ScrollUp:
4344 case CK_MarkScrollUp:
4345 case CK_MarkColumnScrollUp:
4346 case CK_ScrollDown:
4347 case CK_MarkScrollDown:
4348 case CK_MarkColumnScrollDown:
4349 edit->search_start = edit->curs1;
4350 edit->found_len = 0;
4351 break;
4352 default:
4353 edit->found_len = 0;
4354 edit->prev_col = edit_get_col (edit);
4355 edit->search_start = edit->curs1;
4357 edit_find_bracket (edit);
4359 if (option_auto_para_formatting)
4361 switch (command)
4363 case CK_BackSpace:
4364 case CK_Delete:
4365 case CK_DeleteToWordBegin:
4366 case CK_DeleteToWordEnd:
4367 case CK_DeleteToHome:
4368 case CK_DeleteToEnd:
4369 format_paragraph (edit, 0);
4370 edit->force |= REDRAW_PAGE;
4375 /* --------------------------------------------------------------------------------------------- */
4377 void
4378 edit_stack_init (void)
4380 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4382 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4383 edit_history_moveto[edit_stack_iterator].line = -1;
4386 edit_stack_iterator = 0;
4389 /* --------------------------------------------------------------------------------------------- */
4391 void
4392 edit_stack_free (void)
4394 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4395 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4398 /* --------------------------------------------------------------------------------------------- */
4399 /** move i lines */
4401 void
4402 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4404 edit_move_updown (edit, i, do_scroll, TRUE);
4407 /* --------------------------------------------------------------------------------------------- */
4408 /** move i lines */
4410 void
4411 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4413 edit_move_updown (edit, i, do_scroll, FALSE);
4416 /* --------------------------------------------------------------------------------------------- */