Aggressive use WIDGET macro.
[midnight-commander.git] / src / editor / edit.c
blobb4169e28a42a00cba7b7f191ea0d54e5917a441c
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/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;
93 int option_edit_right_extreme = 0;
94 int option_edit_left_extreme = 0;
95 int option_edit_top_extreme = 0;
96 int option_edit_bottom_extreme = 0;
97 int enable_show_tabs_tws = 1;
98 int option_check_nl_at_eof = 0;
99 int option_group_undo = 0;
100 int show_right_margin = 0;
102 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
103 char *option_backup_ext = NULL;
105 unsigned int edit_stack_iterator = 0;
106 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
107 /* magic sequense for say than block is vertical */
108 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
110 /*** file scope macro definitions ****************************************************************/
112 #define TEMP_BUF_LEN 1024
114 #define space_width 1
116 /*** file scope type declarations ****************************************************************/
118 /*** file scope variables ************************************************************************/
120 /* detecting an error on save is easy: just check if every byte has been written. */
121 /* detecting an error on read, is not so easy 'cos there is not way to tell
122 whether you read everything or not. */
123 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
124 static const struct edit_filters
126 const char *read, *write, *extension;
127 } all_filters[] =
129 /* *INDENT-OFF* */
130 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
131 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
132 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
135 /* *INDENT-ON* */
138 static off_t last_bracket = -1;
140 /*** file scope functions ************************************************************************/
141 /* --------------------------------------------------------------------------------------------- */
145 * here's a quick sketch of the layout: (don't run this through indent.)
147 * (b1 is buffers1 and b2 is buffers2)
150 * \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
151 * ______________________________________|______________________________________
153 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
154 * |-> |-> |-> |-> |-> |-> |
156 * _<------------------------->|<----------------->_
157 * WEdit->curs2 | WEdit->curs1
158 * ^ | ^
159 * | ^|^ |
160 * cursor ||| cursor
161 * |||
162 * file end|||file beginning
167 * This_is_some_file
168 * fin.
171 /* --------------------------------------------------------------------------------------------- */
173 * Initialize the buffers for an empty files.
176 static void
177 edit_init_buffers (WEdit * edit)
179 int j;
181 for (j = 0; j <= MAXBUFF; j++)
183 edit->buffers1[j] = NULL;
184 edit->buffers2[j] = NULL;
187 edit->curs1 = 0;
188 edit->curs2 = 0;
189 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
192 /* --------------------------------------------------------------------------------------------- */
195 * Load file OR text into buffers. Set cursor to the beginning of file.
197 * @return FALSE on error.
200 static gboolean
201 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
203 off_t buf, buf2;
204 int file = -1;
205 gboolean ret = FALSE;
207 edit->curs2 = edit->last_byte;
208 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
210 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
211 if (file == -1)
213 gchar *errmsg, *filename;
215 filename = vfs_path_to_str (filename_vpath);
216 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
217 g_free (filename);
218 edit_error_dialog (_("Error"), errmsg);
219 g_free (errmsg);
220 return FALSE;
223 if (!edit->buffers2[buf2])
224 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
228 if (mc_read (file,
229 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
230 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
231 break;
233 for (buf = buf2 - 1; buf >= 0; buf--)
235 /* edit->buffers2[0] is already allocated */
236 if (!edit->buffers2[buf])
237 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
238 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
239 break;
241 ret = TRUE;
243 while (FALSE);
245 if (!ret)
247 gchar *errmsg, *filename;
249 filename = vfs_path_to_str (filename_vpath);
250 errmsg = g_strdup_printf (_("Error reading %s"), filename);
251 g_free (filename);
252 edit_error_dialog (_("Error"), errmsg);
253 g_free (errmsg);
255 mc_close (file);
256 return ret;
259 /* --------------------------------------------------------------------------------------------- */
260 /** Return index of the filter or -1 is there is no appropriate filter */
262 static int
263 edit_find_filter (const vfs_path_t * filename_vpath)
265 size_t i, l, e;
266 char *filename;
268 if (filename_vpath == NULL)
269 return -1;
271 filename = vfs_path_to_str (filename_vpath);
272 l = strlen (filename);
273 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
275 e = strlen (all_filters[i].extension);
276 if (l > e)
277 if (!strcmp (all_filters[i].extension, filename + l - e))
279 g_free (filename);
280 return i;
283 g_free (filename);
284 return -1;
287 /* --------------------------------------------------------------------------------------------- */
289 static char *
290 edit_get_filter (const vfs_path_t * filename_vpath)
292 int i;
293 char *p, *quoted_name, *filename;
295 i = edit_find_filter (filename_vpath);
296 if (i < 0)
297 return NULL;
299 filename = vfs_path_to_str (filename_vpath);
300 quoted_name = name_quote (filename, 0);
301 g_free (filename);
302 p = g_strdup_printf (all_filters[i].read, quoted_name);
303 g_free (quoted_name);
304 return p;
307 /* --------------------------------------------------------------------------------------------- */
309 static off_t
310 edit_insert_stream (WEdit * edit, FILE * f)
312 int c;
313 off_t i = 0;
314 while ((c = fgetc (f)) >= 0)
316 edit_insert (edit, c);
317 i++;
319 return i;
322 /* --------------------------------------------------------------------------------------------- */
324 * Open file and create it if necessary.
326 * @param edit editor object
327 * @param filename_vpath file name
328 * @param st buffer for store stat info
329 * @return TRUE for success, FALSE for error.
332 static gboolean
333 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
335 int file;
336 gchar *errmsg = NULL;
338 /* Try opening an existing file */
339 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
340 if (file < 0)
343 * Try creating the file. O_EXCL prevents following broken links
344 * and opening existing files.
346 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
347 if (file < 0)
349 char *filename;
351 filename = vfs_path_to_str (filename_vpath);
352 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
353 g_free (filename);
354 goto cleanup;
357 /* New file, delete it if it's not modified or saved */
358 edit->delete_file = 1;
361 /* Check what we have opened */
362 if (mc_fstat (file, st) < 0)
364 char *filename;
366 filename = vfs_path_to_str (filename_vpath);
367 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
368 g_free (filename);
369 goto cleanup;
372 /* We want to open regular files only */
373 if (!S_ISREG (st->st_mode))
375 char *filename;
377 filename = vfs_path_to_str (filename_vpath);
378 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
379 g_free (filename);
380 goto cleanup;
384 * Don't delete non-empty files.
385 * O_EXCL should prevent it, but let's be on the safe side.
387 if (st->st_size > 0)
388 edit->delete_file = 0;
390 if (st->st_size >= SIZE_LIMIT)
392 char *filename;
394 filename = vfs_path_to_str (filename_vpath);
395 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
396 g_free (filename);
399 cleanup:
400 (void) mc_close (file);
402 if (errmsg != NULL)
404 edit_error_dialog (_("Error"), errmsg);
405 g_free (errmsg);
406 return FALSE;
408 return TRUE;
411 /* --------------------------------------------------------------------------------------------- */
414 * Open the file and load it into the buffers, either directly or using
415 * a filter. Return TRUE on success, FALSE on error.
417 * Fast loading (edit_load_file_fast) is used when the file size is
418 * known. In this case the data is read into the buffers by blocks.
419 * If the file size is not known, the data is loaded byte by byte in
420 * edit_insert_file.
422 * @param edit editor object
423 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
425 static gboolean
426 edit_load_file (WEdit * edit)
428 gboolean fast_load = TRUE;
430 /* Cannot do fast load if a filter is used */
431 if (edit_find_filter (edit->filename_vpath) >= 0)
432 fast_load = FALSE;
435 * FIXME: line end translation should disable fast loading as well
436 * Consider doing fseek() to the end and ftell() for the real size.
438 if (edit->filename_vpath != NULL)
441 * VFS may report file size incorrectly, and slow load is not a big
442 * deal considering overhead in VFS.
444 if (!vfs_file_is_local (edit->filename_vpath))
445 fast_load = FALSE;
447 /* If we are dealing with a real file, check that it exists */
448 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
450 edit_clean (edit);
451 return FALSE;
454 else
456 /* nothing to load */
457 fast_load = FALSE;
460 edit_init_buffers (edit);
462 if (fast_load)
464 edit->last_byte = edit->stat1.st_size;
465 edit_load_file_fast (edit, edit->filename_vpath);
466 /* If fast load was used, the number of lines wasn't calculated */
467 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
469 else
471 edit->last_byte = 0;
472 if (edit->filename_vpath != NULL
473 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
475 edit->undo_stack_disable = 1;
476 if (edit_insert_file (edit, edit->filename_vpath) < 0)
478 edit_clean (edit);
479 return FALSE;
481 edit->undo_stack_disable = 0;
484 edit->lb = LB_ASIS;
485 return TRUE;
488 /* --------------------------------------------------------------------------------------------- */
489 /** Restore saved cursor position in the file */
491 static void
492 edit_load_position (WEdit * edit)
494 long line, column;
495 off_t offset;
497 if (edit->filename_vpath == NULL
498 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
499 return;
501 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
503 if (line > 0)
505 edit_move_to_line (edit, line - 1);
506 edit->prev_col = column;
508 else if (offset > 0)
510 edit_cursor_move (edit, offset);
511 line = edit->curs_line;
512 edit->search_start = edit->curs1;
515 book_mark_restore (edit, BOOK_MARK_COLOR);
517 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
518 edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
521 /* --------------------------------------------------------------------------------------------- */
522 /** Save cursor position in the file */
524 static void
525 edit_save_position (WEdit * edit)
527 if (edit->filename_vpath == NULL
528 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
529 return;
531 book_mark_serialize (edit, BOOK_MARK_COLOR);
532 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
533 edit->serialized_bookmarks);
534 edit->serialized_bookmarks = NULL;
537 /* --------------------------------------------------------------------------------------------- */
538 /** Clean the WEdit stricture except the widget part */
540 static void
541 edit_purge_widget (WEdit * edit)
543 size_t len = sizeof (WEdit) - sizeof (Widget);
544 char *start = (char *) edit + sizeof (Widget);
545 memset (start, 0, len);
548 /* --------------------------------------------------------------------------------------------- */
551 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
552 then the file should be as it was when he loaded up. Then set edit->modified to 0.
555 static long
556 edit_pop_undo_action (WEdit * edit)
558 long c;
559 unsigned long sp = edit->undo_stack_pointer;
561 if (sp == edit->undo_stack_bottom)
562 return STACK_BOTTOM;
564 sp = (sp - 1) & edit->undo_stack_size_mask;
565 c = edit->undo_stack[sp];
566 if (c >= 0)
568 /* edit->undo_stack[sp] = '@'; */
569 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
570 return c;
573 if (sp == edit->undo_stack_bottom)
574 return STACK_BOTTOM;
576 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
577 if (edit->undo_stack[sp] == -2)
579 /* edit->undo_stack[sp] = '@'; */
580 edit->undo_stack_pointer = sp;
582 else
583 edit->undo_stack[sp]++;
585 return c;
588 static long
589 edit_pop_redo_action (WEdit * edit)
591 long c;
592 unsigned long sp = edit->redo_stack_pointer;
594 if (sp == edit->redo_stack_bottom)
595 return STACK_BOTTOM;
597 sp = (sp - 1) & edit->redo_stack_size_mask;
598 c = edit->redo_stack[sp];
599 if (c >= 0)
601 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
602 return c;
605 if (sp == edit->redo_stack_bottom)
606 return STACK_BOTTOM;
608 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
609 if (edit->redo_stack[sp] == -2)
610 edit->redo_stack_pointer = sp;
611 else
612 edit->redo_stack[sp]++;
614 return c;
617 static long
618 get_prev_undo_action (WEdit * edit)
620 long c;
621 unsigned long sp = edit->undo_stack_pointer;
623 if (sp == edit->undo_stack_bottom)
624 return STACK_BOTTOM;
626 sp = (sp - 1) & edit->undo_stack_size_mask;
627 c = edit->undo_stack[sp];
628 if (c >= 0)
629 return c;
631 if (sp == edit->undo_stack_bottom)
632 return STACK_BOTTOM;
634 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
635 return c;
638 /* --------------------------------------------------------------------------------------------- */
639 /** is called whenever a modification is made by one of the four routines below */
641 static void
642 edit_modification (WEdit * edit)
644 edit->caches_valid = FALSE;
646 /* raise lock when file modified */
647 if (!edit->modified && !edit->delete_file)
648 edit->locked = lock_file (edit->filename_vpath);
649 edit->modified = 1;
652 /* --------------------------------------------------------------------------------------------- */
654 #ifdef HAVE_CHARSET
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));
673 #endif
675 /* --------------------------------------------------------------------------------------------- */
677 #ifdef HAVE_CHARSET
678 static int
679 edit_get_prev_utf (const WEdit * edit, off_t byte_index, int *char_width)
681 int i, res;
682 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
683 gchar *str;
684 gchar *cursor_buf_ptr;
686 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
688 *char_width = 0;
689 return 0;
692 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
693 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
694 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
696 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
697 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
699 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
701 *char_width = 1;
702 return *(cursor_buf_ptr - 1);
704 else
706 res = g_utf8_get_char_validated (str, -1);
708 if (res < 0)
710 *char_width = 1;
711 return *(cursor_buf_ptr - 1);
713 else
715 *char_width = cursor_buf_ptr - str;
716 return res;
720 #endif
722 /* --------------------------------------------------------------------------------------------- */
723 /* high level cursor movement commands */
724 /* --------------------------------------------------------------------------------------------- */
725 /** check whether cursor is in indent part of line
727 * @param edit editor object
729 * @return TRUE if cursor is in indent, FALSE otherwise
732 static gboolean
733 is_in_indent (const WEdit * edit)
735 off_t p;
737 for (p = edit_bol (edit, edit->curs1); p < edit->curs1; p++)
738 if (strchr (" \t", edit_get_byte (edit, p)) == NULL)
739 return FALSE;
741 return TRUE;
744 /* --------------------------------------------------------------------------------------------- */
745 /** check whether line in editor is blank or not
747 * @param edit editor object
748 * @param offset position in file
750 * @return TRUE if line in blank, FALSE otherwise
753 static gboolean
754 is_blank (const WEdit * edit, off_t offset)
756 off_t s, f;
757 int c;
759 s = edit_bol (edit, offset);
760 f = edit_eol (edit, offset) - 1;
761 while (s <= f)
763 c = edit_get_byte (edit, s++);
764 if (!isspace (c))
765 return FALSE;
767 return TRUE;
770 /* --------------------------------------------------------------------------------------------- */
771 /** returns the offset of line i */
773 static off_t
774 edit_find_line (WEdit * edit, long line)
776 long i, j = 0;
777 long m = 2000000000; /* what is the magic number? */
779 if (!edit->caches_valid)
781 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
782 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
783 /* three offsets that we *know* are line 0 at 0 and these two: */
784 edit->line_numbers[1] = edit->curs_line;
785 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
786 edit->line_numbers[2] = edit->total_lines;
787 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
788 edit->caches_valid = TRUE;
790 if (line >= edit->total_lines)
791 return edit->line_offsets[2];
792 if (line <= 0)
793 return 0;
794 /* find the closest known point */
795 for (i = 0; i < N_LINE_CACHES; i++)
797 long n;
799 n = abs (edit->line_numbers[i] - line);
800 if (n < m)
802 m = n;
803 j = i;
806 if (m == 0)
807 return edit->line_offsets[j]; /* know the offset exactly */
808 if (m == 1 && j >= 3)
809 i = j; /* one line different - caller might be looping, so stay in this cache */
810 else
811 i = 3 + (rand () % (N_LINE_CACHES - 3));
812 if (line > edit->line_numbers[j])
813 edit->line_offsets[i] =
814 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
815 else
816 edit->line_offsets[i] =
817 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
818 edit->line_numbers[i] = line;
819 return edit->line_offsets[i];
822 /* --------------------------------------------------------------------------------------------- */
823 /** moves up until a blank line is reached, or until just
824 before a non-blank line is reached */
826 static void
827 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
829 long i = 0;
831 if (edit->curs_line > 1)
833 if (!edit_line_is_blank (edit, edit->curs_line))
835 for (i = edit->curs_line - 1; i != 0; i--)
836 if (edit_line_is_blank (edit, i))
837 break;
839 else if (edit_line_is_blank (edit, edit->curs_line - 1))
841 for (i = edit->curs_line - 1; i != 0; i--)
842 if (!edit_line_is_blank (edit, i))
844 i++;
845 break;
848 else
850 for (i = edit->curs_line - 1; i != 0; i--)
851 if (edit_line_is_blank (edit, i))
852 break;
856 edit_move_up (edit, edit->curs_line - i, do_scroll);
859 /* --------------------------------------------------------------------------------------------- */
860 /** moves down until a blank line is reached, or until just
861 before a non-blank line is reached */
863 static void
864 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
866 long i;
868 if (edit->curs_line >= edit->total_lines - 1)
869 i = edit->total_lines;
870 else if (!edit_line_is_blank (edit, edit->curs_line))
872 for (i = edit->curs_line + 1; i != 0; i++)
873 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
874 break;
876 else if (edit_line_is_blank (edit, edit->curs_line + 1))
878 for (i = edit->curs_line + 1; i != 0; i++)
879 if (!edit_line_is_blank (edit, i) || i > edit->total_lines)
881 i--;
882 break;
885 else
887 for (i = edit->curs_line + 1; i != 0; i++)
888 if (edit_line_is_blank (edit, i) || i >= edit->total_lines)
889 break;
891 edit_move_down (edit, i - edit->curs_line, do_scroll);
894 /* --------------------------------------------------------------------------------------------- */
896 static void
897 edit_begin_page (WEdit * edit)
899 edit_update_curs_row (edit);
900 edit_move_up (edit, edit->curs_row, 0);
903 /* --------------------------------------------------------------------------------------------- */
905 static void
906 edit_end_page (WEdit * edit)
908 edit_update_curs_row (edit);
909 edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, 0);
913 /* --------------------------------------------------------------------------------------------- */
914 /** goto beginning of text */
916 static void
917 edit_move_to_top (WEdit * edit)
919 if (edit->curs_line)
921 edit_cursor_move (edit, -edit->curs1);
922 edit_move_to_prev_col (edit, 0);
923 edit->force |= REDRAW_PAGE;
924 edit->search_start = 0;
925 edit_update_curs_row (edit);
930 /* --------------------------------------------------------------------------------------------- */
931 /** goto end of text */
933 static void
934 edit_move_to_bottom (WEdit * edit)
936 if (edit->curs_line < edit->total_lines)
938 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
939 edit->start_display = edit->last_byte;
940 edit->start_line = edit->total_lines;
941 edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
942 edit->force |= REDRAW_PAGE;
946 /* --------------------------------------------------------------------------------------------- */
947 /** goto beginning of line */
949 static void
950 edit_cursor_to_bol (WEdit * edit)
952 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
953 edit->search_start = edit->curs1;
954 edit->prev_col = edit_get_col (edit);
955 edit->over_col = 0;
958 /* --------------------------------------------------------------------------------------------- */
959 /** goto end of line */
961 static void
962 edit_cursor_to_eol (WEdit * edit)
964 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
965 edit->search_start = edit->curs1;
966 edit->prev_col = edit_get_col (edit);
967 edit->over_col = 0;
970 /* --------------------------------------------------------------------------------------------- */
972 static unsigned long
973 my_type_of (int c)
975 int x, r = 0;
976 const char *p, *q;
977 const char option_chars_move_whole_word[] =
978 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
980 if (c == 0)
981 return 0;
982 if (c == '!')
984 if (*option_chars_move_whole_word == '!')
985 return 2;
986 return 0x80000000UL;
988 if (g_ascii_isupper ((gchar) c))
989 c = 'A';
990 else if (g_ascii_islower ((gchar) c))
991 c = 'a';
992 else if (g_ascii_isalpha (c))
993 c = 'a';
994 else if (isdigit (c))
995 c = '0';
996 else if (isspace (c))
997 c = ' ';
998 q = strchr (option_chars_move_whole_word, c);
999 if (!q)
1000 return 0xFFFFFFFFUL;
1003 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1004 if (*p == '!')
1005 x <<= 1;
1006 r |= x;
1008 while ((q = strchr (q + 1, c)));
1009 return r;
1012 /* --------------------------------------------------------------------------------------------- */
1014 static void
1015 edit_left_word_move (WEdit * edit, int s)
1017 while (TRUE)
1019 int c1, c2;
1021 if (edit->column_highlight
1022 && edit->mark1 != edit->mark2
1023 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1024 break;
1025 edit_cursor_move (edit, -1);
1026 if (edit->curs1 == 0)
1027 break;
1028 c1 = edit_get_byte (edit, edit->curs1 - 1);
1029 c2 = edit_get_byte (edit, edit->curs1);
1030 if (c1 == '\n' || c2 == '\n')
1031 break;
1032 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1033 break;
1034 if (isspace (c1) && !isspace (c2))
1035 break;
1036 if (s != 0 && !isspace (c1) && isspace (c2))
1037 break;
1041 /* --------------------------------------------------------------------------------------------- */
1043 static void
1044 edit_left_word_move_cmd (WEdit * edit)
1046 edit_left_word_move (edit, 0);
1047 edit->force |= REDRAW_PAGE;
1050 /* --------------------------------------------------------------------------------------------- */
1052 static void
1053 edit_right_word_move (WEdit * edit, int s)
1055 while (TRUE)
1057 int c1, c2;
1059 if (edit->column_highlight
1060 && edit->mark1 != edit->mark2
1061 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1062 break;
1063 edit_cursor_move (edit, 1);
1064 if (edit->curs1 >= edit->last_byte)
1065 break;
1066 c1 = edit_get_byte (edit, edit->curs1 - 1);
1067 c2 = edit_get_byte (edit, edit->curs1);
1068 if (c1 == '\n' || c2 == '\n')
1069 break;
1070 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1071 break;
1072 if (isspace (c1) && !isspace (c2))
1073 break;
1074 if (s != 0 && !isspace (c1) && isspace (c2))
1075 break;
1079 /* --------------------------------------------------------------------------------------------- */
1081 static void
1082 edit_right_word_move_cmd (WEdit * edit)
1084 edit_right_word_move (edit, 0);
1085 edit->force |= REDRAW_PAGE;
1088 /* --------------------------------------------------------------------------------------------- */
1090 static void
1091 edit_right_char_move_cmd (WEdit * edit)
1093 int cw = 1;
1094 int c = 0;
1095 #ifdef HAVE_CHARSET
1096 if (edit->utf8)
1098 c = edit_get_utf (edit, edit->curs1, &cw);
1099 if (cw < 1)
1100 cw = 1;
1102 else
1103 #endif
1105 c = edit_get_byte (edit, edit->curs1);
1107 if (option_cursor_beyond_eol && c == '\n')
1109 edit->over_col++;
1111 else
1113 edit_cursor_move (edit, cw);
1117 /* --------------------------------------------------------------------------------------------- */
1119 static void
1120 edit_left_char_move_cmd (WEdit * edit)
1122 int cw = 1;
1123 if (edit->column_highlight
1124 && option_cursor_beyond_eol
1125 && edit->mark1 != edit->mark2
1126 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1127 return;
1128 #ifdef HAVE_CHARSET
1129 if (edit->utf8)
1131 edit_get_prev_utf (edit, edit->curs1, &cw);
1132 if (cw < 1)
1133 cw = 1;
1135 #endif
1136 if (option_cursor_beyond_eol && edit->over_col > 0)
1138 edit->over_col--;
1140 else
1142 edit_cursor_move (edit, -cw);
1146 /* --------------------------------------------------------------------------------------------- */
1147 /** Up or down cursor moving.
1148 direction = TRUE - move up
1149 = FALSE - move down
1152 static void
1153 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1155 long p;
1156 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1158 if (lines > l)
1159 lines = l;
1161 if (lines == 0)
1162 return;
1164 if (lines > 1)
1165 edit->force |= REDRAW_PAGE;
1166 if (do_scroll)
1168 if (direction)
1169 edit_scroll_upward (edit, lines);
1170 else
1171 edit_scroll_downward (edit, lines);
1173 p = edit_bol (edit, edit->curs1);
1175 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1177 edit_cursor_move (edit, p - edit->curs1);
1179 edit_move_to_prev_col (edit, p);
1181 /* search start of current multibyte char (like CJK) */
1182 if (edit->curs1 + 1 < edit->last_byte)
1184 edit_right_char_move_cmd (edit);
1185 edit_left_char_move_cmd (edit);
1188 edit->search_start = edit->curs1;
1189 edit->found_len = 0;
1192 /* --------------------------------------------------------------------------------------------- */
1194 static void
1195 edit_right_delete_word (WEdit * edit)
1197 while (edit->curs1 < edit->last_byte)
1199 int c1, c2;
1201 c1 = edit_delete (edit, TRUE);
1202 c2 = edit_get_byte (edit, edit->curs1);
1203 if (c1 == '\n' || c2 == '\n')
1204 break;
1205 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1206 break;
1207 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1208 break;
1212 /* --------------------------------------------------------------------------------------------- */
1214 static void
1215 edit_left_delete_word (WEdit * edit)
1217 while (edit->curs1 > 0)
1219 int c1, c2;
1221 c1 = edit_backspace (edit, TRUE);
1222 c2 = edit_get_byte (edit, edit->curs1 - 1);
1223 if (c1 == '\n' || c2 == '\n')
1224 break;
1225 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1226 break;
1227 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1228 break;
1232 /* --------------------------------------------------------------------------------------------- */
1234 the start column position is not recorded, and hence does not
1235 undo as it happed. But who would notice.
1238 static void
1239 edit_do_undo (WEdit * edit)
1241 long ac;
1242 long count = 0;
1244 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1245 edit->over_col = 0;
1246 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1248 switch ((int) ac)
1250 case STACK_BOTTOM:
1251 goto done_undo;
1252 case CURS_RIGHT:
1253 edit_cursor_move (edit, 1);
1254 break;
1255 case CURS_LEFT:
1256 edit_cursor_move (edit, -1);
1257 break;
1258 case BACKSPACE:
1259 case BACKSPACE_BR:
1260 edit_backspace (edit, TRUE);
1261 break;
1262 case DELCHAR:
1263 case DELCHAR_BR:
1264 edit_delete (edit, TRUE);
1265 break;
1266 case COLUMN_ON:
1267 edit->column_highlight = 1;
1268 break;
1269 case COLUMN_OFF:
1270 edit->column_highlight = 0;
1271 break;
1273 if (ac >= 256 && ac < 512)
1274 edit_insert_ahead (edit, ac - 256);
1275 if (ac >= 0 && ac < 256)
1276 edit_insert (edit, ac);
1278 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1280 edit->mark1 = ac - MARK_1;
1281 edit->column1 =
1282 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1284 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1286 edit->mark2 = ac - MARK_2;
1287 edit->column2 =
1288 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1290 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1292 edit->end_mark_curs = ac - MARK_CURS;
1294 if (count++)
1295 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1298 if (edit->start_display > ac - KEY_PRESS)
1300 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1301 edit->force |= REDRAW_PAGE;
1303 else if (edit->start_display < ac - KEY_PRESS)
1305 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1306 edit->force |= REDRAW_PAGE;
1308 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1309 edit_update_curs_row (edit);
1311 done_undo:
1312 edit->undo_stack_disable = 0;
1315 /* --------------------------------------------------------------------------------------------- */
1317 static void
1318 edit_do_redo (WEdit * edit)
1320 long ac;
1321 long count = 0;
1323 if (edit->redo_stack_reset)
1324 return;
1326 edit->over_col = 0;
1327 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1329 switch ((int) ac)
1331 case STACK_BOTTOM:
1332 goto done_redo;
1333 case CURS_RIGHT:
1334 edit_cursor_move (edit, 1);
1335 break;
1336 case CURS_LEFT:
1337 edit_cursor_move (edit, -1);
1338 break;
1339 case BACKSPACE:
1340 edit_backspace (edit, TRUE);
1341 break;
1342 case DELCHAR:
1343 edit_delete (edit, TRUE);
1344 break;
1345 case COLUMN_ON:
1346 edit->column_highlight = 1;
1347 break;
1348 case COLUMN_OFF:
1349 edit->column_highlight = 0;
1350 break;
1352 if (ac >= 256 && ac < 512)
1353 edit_insert_ahead (edit, ac - 256);
1354 if (ac >= 0 && ac < 256)
1355 edit_insert (edit, ac);
1357 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1359 edit->mark1 = ac - MARK_1;
1360 edit->column1 =
1361 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1363 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1365 edit->mark2 = ac - MARK_2;
1366 edit->column2 =
1367 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1369 /* more than one pop usually means something big */
1370 if (count++)
1371 edit->force |= REDRAW_PAGE;
1374 if (edit->start_display > ac - KEY_PRESS)
1376 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1377 edit->force |= REDRAW_PAGE;
1379 else if (edit->start_display < ac - KEY_PRESS)
1381 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1382 edit->force |= REDRAW_PAGE;
1384 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1385 edit_update_curs_row (edit);
1387 done_redo:
1391 /* --------------------------------------------------------------------------------------------- */
1393 static void
1394 edit_group_undo (WEdit * edit)
1396 long ac = KEY_PRESS;
1397 long cur_ac = KEY_PRESS;
1398 while (ac != STACK_BOTTOM && ac == cur_ac)
1400 cur_ac = get_prev_undo_action (edit);
1401 edit_do_undo (edit);
1402 ac = get_prev_undo_action (edit);
1403 /* exit from cycle if option_group_undo is not set,
1404 * and make single UNDO operation
1406 if (!option_group_undo)
1407 ac = STACK_BOTTOM;
1411 /* --------------------------------------------------------------------------------------------- */
1413 static void
1414 edit_delete_to_line_end (WEdit * edit)
1416 while (edit_get_byte (edit, edit->curs1) != '\n' && edit->curs2 != 0)
1417 edit_delete (edit, TRUE);
1420 /* --------------------------------------------------------------------------------------------- */
1422 static void
1423 edit_delete_to_line_begin (WEdit * edit)
1425 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 != 0)
1426 edit_backspace (edit, TRUE);
1429 /* --------------------------------------------------------------------------------------------- */
1431 static gboolean
1432 is_aligned_on_a_tab (WEdit * edit)
1434 long curs_col;
1436 edit_update_curs_col (edit);
1437 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1438 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1441 /* --------------------------------------------------------------------------------------------- */
1443 static gboolean
1444 right_of_four_spaces (WEdit * edit)
1446 int i, ch = 0;
1448 for (i = 1; i <= HALF_TAB_SIZE; i++)
1449 ch |= edit_get_byte (edit, edit->curs1 - i);
1451 return (ch == ' ' && is_aligned_on_a_tab (edit));
1454 /* --------------------------------------------------------------------------------------------- */
1456 static gboolean
1457 left_of_four_spaces (WEdit * edit)
1459 int i, ch = 0;
1461 for (i = 0; i < HALF_TAB_SIZE; i++)
1462 ch |= edit_get_byte (edit, edit->curs1 + i);
1464 return (ch == ' ' && is_aligned_on_a_tab (edit));
1467 /* --------------------------------------------------------------------------------------------- */
1469 static void
1470 edit_auto_indent (WEdit * edit)
1472 off_t p;
1473 char c;
1475 p = edit->curs1;
1476 /* use the previous line as a template */
1477 p = edit_move_backward (edit, p, 1);
1478 /* copy the leading whitespace of the line */
1479 while (TRUE)
1480 { /* no range check - the line _is_ \n-terminated */
1481 c = edit_get_byte (edit, p++);
1482 if (c != ' ' && c != '\t')
1483 break;
1484 edit_insert (edit, c);
1488 /* --------------------------------------------------------------------------------------------- */
1490 static inline void
1491 edit_double_newline (WEdit * edit)
1493 edit_insert (edit, '\n');
1494 if (edit_get_byte (edit, edit->curs1) == '\n' || edit_get_byte (edit, edit->curs1 - 2) == '\n')
1495 return;
1496 edit->force |= REDRAW_PAGE;
1497 edit_insert (edit, '\n');
1500 /* --------------------------------------------------------------------------------------------- */
1502 static void
1503 insert_spaces_tab (WEdit * edit, gboolean half)
1505 long i;
1507 edit_update_curs_col (edit);
1508 i = option_tab_spacing * space_width;
1509 if (half)
1510 i /= 2;
1511 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1512 while (i > 0)
1514 edit_insert (edit, ' ');
1515 i -= space_width;
1519 /* --------------------------------------------------------------------------------------------- */
1521 static inline void
1522 edit_tab_cmd (WEdit * edit)
1524 if (option_fake_half_tabs && is_in_indent (edit))
1526 /* insert a half tab (usually four spaces) unless there is a
1527 half tab already behind, then delete it and insert a
1528 full tab. */
1529 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1530 insert_spaces_tab (edit, TRUE);
1531 else
1533 int i;
1535 for (i = 1; i <= HALF_TAB_SIZE; i++)
1536 edit_backspace (edit, TRUE);
1537 edit_insert (edit, '\t');
1540 else if (option_fill_tabs_with_spaces)
1541 insert_spaces_tab (edit, FALSE);
1542 else
1543 edit_insert (edit, '\t');
1546 /* --------------------------------------------------------------------------------------------- */
1548 static void
1549 check_and_wrap_line (WEdit * edit)
1551 off_t curs;
1552 int c;
1554 if (!option_typewriter_wrap)
1555 return;
1556 edit_update_curs_col (edit);
1557 if (edit->curs_col < option_word_wrap_line_length)
1558 return;
1559 curs = edit->curs1;
1560 while (TRUE)
1562 curs--;
1563 c = edit_get_byte (edit, curs);
1564 if (c == '\n' || curs <= 0)
1566 edit_insert (edit, '\n');
1567 return;
1569 if (c == ' ' || c == '\t')
1571 off_t current = edit->curs1;
1572 edit_cursor_move (edit, curs - edit->curs1 + 1);
1573 edit_insert (edit, '\n');
1574 edit_cursor_move (edit, current - edit->curs1 + 1);
1575 return;
1580 /* --------------------------------------------------------------------------------------------- */
1581 /** this find the matching bracket in either direction, and sets edit->bracket
1583 * @param edit editor object
1584 * @param in_screen seach only on the current screen
1585 * @param furthest_bracket_search count of the bytes for search
1587 * @return position of the found bracket (-1 if no match)
1590 static off_t
1591 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1593 const char *const b = "{}{[][()(", *p;
1594 int i = 1, a, inc = -1, c, d, n = 0;
1595 unsigned long j = 0;
1596 off_t q;
1597 edit_update_curs_row (edit);
1598 c = edit_get_byte (edit, edit->curs1);
1599 p = strchr (b, c);
1600 /* no limit */
1601 if (!furthest_bracket_search)
1602 furthest_bracket_search--;
1603 /* not on a bracket at all */
1604 if (p == NULL)
1605 return -1;
1606 /* the matching bracket */
1607 d = p[1];
1608 /* going left or right? */
1609 if (strchr ("{[(", c))
1610 inc = 1;
1611 for (q = edit->curs1 + inc;; q += inc)
1613 /* out of buffer? */
1614 if (q >= edit->last_byte || q < 0)
1615 break;
1616 a = edit_get_byte (edit, q);
1617 /* don't want to eat CPU */
1618 if (j++ > furthest_bracket_search)
1619 break;
1620 /* out of screen? */
1621 if (in_screen)
1623 if (q < edit->start_display)
1624 break;
1625 /* count lines if searching downward */
1626 if (inc > 0 && a == '\n')
1627 if (n++ >= WIDGET (edit)->lines - edit->curs_row) /* out of screen */
1628 break;
1630 /* count bracket depth */
1631 i += (a == c) - (a == d);
1632 /* return if bracket depth is zero */
1633 if (i == 0)
1634 return q;
1636 /* no match */
1637 return -1;
1640 /* --------------------------------------------------------------------------------------------- */
1642 static inline void
1643 edit_goto_matching_bracket (WEdit * edit)
1645 off_t q;
1647 q = edit_get_bracket (edit, 0, 0);
1648 if (q >= 0)
1650 edit->bracket = edit->curs1;
1651 edit->force |= REDRAW_PAGE;
1652 edit_cursor_move (edit, q - edit->curs1);
1656 /* --------------------------------------------------------------------------------------------- */
1658 static void
1659 edit_move_block_to_right (WEdit * edit)
1661 off_t start_mark, end_mark;
1662 long cur_bol, start_bol;
1664 if (eval_marks (edit, &start_mark, &end_mark))
1665 return;
1667 start_bol = edit_bol (edit, start_mark);
1668 cur_bol = edit_bol (edit, end_mark - 1);
1672 edit_cursor_move (edit, cur_bol - edit->curs1);
1673 if (!edit_line_is_blank (edit, edit->curs_line))
1675 if (option_fill_tabs_with_spaces)
1676 insert_spaces_tab (edit, option_fake_half_tabs);
1677 else
1678 edit_insert (edit, '\t');
1679 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1682 if (cur_bol == 0)
1683 break;
1685 cur_bol = edit_bol (edit, cur_bol - 1);
1687 while (cur_bol >= start_bol);
1689 edit->force |= REDRAW_PAGE;
1692 /* --------------------------------------------------------------------------------------------- */
1694 static void
1695 edit_move_block_to_left (WEdit * edit)
1697 off_t start_mark, end_mark;
1698 off_t cur_bol, start_bol;
1699 int i;
1701 if (eval_marks (edit, &start_mark, &end_mark))
1702 return;
1704 start_bol = edit_bol (edit, start_mark);
1705 cur_bol = edit_bol (edit, end_mark - 1);
1709 int del_tab_width;
1710 int next_char;
1712 edit_cursor_move (edit, cur_bol - edit->curs1);
1714 if (option_fake_half_tabs)
1715 del_tab_width = HALF_TAB_SIZE;
1716 else
1717 del_tab_width = option_tab_spacing;
1719 next_char = edit_get_byte (edit, edit->curs1);
1720 if (next_char == '\t')
1721 edit_delete (edit, TRUE);
1722 else if (next_char == ' ')
1723 for (i = 1; i <= del_tab_width; i++)
1725 if (next_char == ' ')
1726 edit_delete (edit, TRUE);
1727 next_char = edit_get_byte (edit, edit->curs1);
1730 if (cur_bol == 0)
1731 break;
1733 cur_bol = edit_bol (edit, cur_bol - 1);
1735 while (cur_bol >= start_bol);
1737 edit->force |= REDRAW_PAGE;
1740 /* --------------------------------------------------------------------------------------------- */
1742 * prints at the cursor
1743 * @return number of chars printed
1746 static size_t
1747 edit_print_string (WEdit * e, const char *s)
1749 size_t i = 0;
1751 while (s[i] != '\0')
1752 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1753 e->force |= REDRAW_COMPLETELY;
1754 edit_update_screen (e);
1755 return i;
1758 /* --------------------------------------------------------------------------------------------- */
1759 /*** public functions ****************************************************************************/
1760 /* --------------------------------------------------------------------------------------------- */
1762 /** User edit menu, like user menu (F2) but only in editor. */
1764 void
1765 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1767 char *block_file;
1768 int nomark;
1769 off_t curs;
1770 off_t start_mark, end_mark;
1771 struct stat status;
1772 vfs_path_t *block_file_vpath;
1774 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1775 block_file_vpath = vfs_path_from_str (block_file);
1776 curs = edit->curs1;
1777 nomark = eval_marks (edit, &start_mark, &end_mark);
1778 if (nomark == 0)
1779 edit_save_block (edit, block_file, start_mark, end_mark);
1781 /* run shell scripts from menu */
1782 if (user_menu_cmd (edit, menu_file, selected_entry)
1783 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1785 int rc = 0;
1786 FILE *fd;
1788 /* i.e. we have marked block */
1789 if (nomark == 0)
1790 rc = edit_block_delete_cmd (edit);
1792 if (rc == 0)
1794 off_t ins_len;
1796 ins_len = edit_insert_file (edit, block_file_vpath);
1797 if (nomark == 0 && ins_len > 0)
1798 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1800 /* truncate block file */
1801 fd = fopen (block_file, "w");
1802 if (fd != NULL)
1803 fclose (fd);
1805 g_free (block_file);
1806 vfs_path_free (block_file_vpath);
1808 edit_cursor_move (edit, curs - edit->curs1);
1809 edit->force |= REDRAW_PAGE;
1810 send_message (WIDGET (edit), WIDGET_DRAW, 0);
1813 /* --------------------------------------------------------------------------------------------- */
1816 edit_get_byte (const WEdit * edit, off_t byte_index)
1818 off_t p;
1820 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1821 return '\n';
1823 if (byte_index >= edit->curs1)
1825 p = edit->curs1 + edit->curs2 - byte_index - 1;
1826 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1829 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1832 /* --------------------------------------------------------------------------------------------- */
1834 #ifdef HAVE_CHARSET
1836 edit_get_utf (const WEdit * edit, off_t byte_index, int *char_width)
1838 gchar *str = NULL;
1839 int res = -1;
1840 gunichar ch;
1841 gchar *next_ch = NULL;
1842 int width = 0;
1843 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1845 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1847 *char_width = 0;
1848 return '\n';
1851 str = edit_get_byte_ptr (edit, byte_index);
1853 if (str == NULL)
1855 *char_width = 0;
1856 return 0;
1859 res = g_utf8_get_char_validated (str, -1);
1861 if (res < 0)
1863 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1864 int i;
1865 for (i = 0; i < UTF8_CHAR_LEN; i++)
1866 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1867 utf8_buf[UTF8_CHAR_LEN] = '\0';
1868 str = utf8_buf;
1869 res = g_utf8_get_char_validated (str, -1);
1872 if (res < 0)
1874 ch = *str;
1875 width = 0;
1877 else
1879 ch = res;
1880 /* Calculate UTF-8 char width */
1881 next_ch = g_utf8_next_char (str);
1882 if (next_ch)
1884 width = next_ch - str;
1886 else
1888 ch = 0;
1889 width = 0;
1892 *char_width = width;
1893 return ch;
1895 #endif
1897 /* --------------------------------------------------------------------------------------------- */
1899 char *
1900 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1902 int i;
1903 char *p, *writename;
1904 const vfs_path_element_t *path_element;
1906 i = edit_find_filter (filename_vpath);
1907 if (i < 0)
1908 return NULL;
1910 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1911 writename = name_quote (path_element->path, 0);
1912 p = g_strdup_printf (all_filters[i].write, writename);
1913 g_free (writename);
1914 return p;
1917 /* --------------------------------------------------------------------------------------------- */
1919 * @param edit editor object
1920 * @param f value of stream file
1921 * @return the length of the file
1924 off_t
1925 edit_write_stream (WEdit * edit, FILE * f)
1927 long i;
1929 if (edit->lb == LB_ASIS)
1931 for (i = 0; i < edit->last_byte; i++)
1932 if (fputc (edit_get_byte (edit, i), f) < 0)
1933 break;
1934 return i;
1937 /* change line breaks */
1938 for (i = 0; i < edit->last_byte; i++)
1940 unsigned char c = edit_get_byte (edit, i);
1942 if (!(c == '\n' || c == '\r'))
1944 /* not line break */
1945 if (fputc (c, f) < 0)
1946 return i;
1948 else
1949 { /* (c == '\n' || c == '\r') */
1950 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1952 switch (edit->lb)
1954 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1955 /* put one line break unconditionally */
1956 if (fputc ('\n', f) < 0)
1957 return i;
1959 i++; /* 2 chars are processed */
1961 if (c == '\r' && c1 == '\n')
1962 /* Windows line break; go to the next char */
1963 break;
1965 if (c == '\r' && c1 == '\r')
1967 /* two Macintosh line breaks; put second line break */
1968 if (fputc ('\n', f) < 0)
1969 return i;
1970 break;
1973 if (fputc (c1, f) < 0)
1974 return i;
1975 break;
1977 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1978 /* put one line break unconditionally */
1979 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1980 return i;
1982 if (c == '\r' && c1 == '\n')
1983 /* Windows line break; go to the next char */
1984 i++;
1985 break;
1987 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1988 /* put one line break unconditionally */
1989 if (fputc ('\r', f) < 0)
1990 return i;
1992 i++; /* 2 chars are processed */
1994 if (c == '\r' && c1 == '\n')
1995 /* Windows line break; go to the next char */
1996 break;
1998 if (c == '\n' && c1 == '\n')
2000 /* two Windows line breaks; put second line break */
2001 if (fputc ('\r', f) < 0)
2002 return i;
2003 break;
2006 if (fputc (c1, f) < 0)
2007 return i;
2008 break;
2009 case LB_ASIS: /* default without changes */
2010 break;
2015 return edit->last_byte;
2018 /* --------------------------------------------------------------------------------------------- */
2020 gboolean
2021 is_break_char (char c)
2023 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2026 /* --------------------------------------------------------------------------------------------- */
2028 char *
2029 edit_get_word_from_pos (const WEdit * edit, off_t start_pos, off_t * start, gsize * len,
2030 gsize * cut)
2032 off_t word_start;
2033 long cut_len = 0;
2034 GString *match_expr;
2035 unsigned char *bufpos;
2036 int c1, c2;
2038 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2040 c1 = edit_get_byte (edit, word_start);
2041 c2 = edit_get_byte (edit, word_start - 1);
2043 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2044 break;
2047 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2048 match_expr = g_string_sized_new (16);
2052 c1 = edit_get_byte (edit, word_start + match_expr->len);
2053 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2054 g_string_append_c (match_expr, c1);
2056 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2058 *len = match_expr->len;
2059 *start = word_start;
2060 *cut = cut_len;
2062 return g_string_free (match_expr, FALSE);
2065 /* --------------------------------------------------------------------------------------------- */
2066 /** inserts a file at the cursor, returns count of inserted bytes on success */
2068 long
2069 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2071 char *p;
2072 off_t ins_len = 0;
2074 p = edit_get_filter (filename_vpath);
2075 if (p != NULL)
2077 FILE *f;
2078 off_t current = edit->curs1;
2080 f = (FILE *) popen (p, "r");
2081 if (f != NULL)
2083 edit_insert_stream (edit, f);
2084 ins_len = edit->curs1 - current;
2085 edit_cursor_move (edit, -ins_len);
2086 if (pclose (f) > 0)
2088 char *errmsg;
2090 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2091 edit_error_dialog (_("Error"), errmsg);
2092 g_free (errmsg);
2093 ins_len = -1;
2096 else
2098 char *errmsg;
2100 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2101 edit_error_dialog (_("Error"), errmsg);
2102 g_free (errmsg);
2103 ins_len = -1;
2105 g_free (p);
2107 else
2109 int file;
2110 off_t blocklen;
2111 off_t current = edit->curs1;
2112 int vertical_insertion = 0;
2113 char *buf;
2115 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2116 if (file == -1)
2117 return -1;
2119 buf = g_malloc0 (TEMP_BUF_LEN);
2120 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2121 if (blocklen > 0)
2123 /* if contain signature VERTICAL_MAGIC then it vertical block */
2124 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2125 vertical_insertion = 1;
2126 else
2127 mc_lseek (file, 0, SEEK_SET);
2130 if (vertical_insertion)
2132 off_t mark1, mark2;
2133 long c1, c2;
2135 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2136 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2137 /* highlight inserted text then not persistent blocks */
2138 if (!option_persistent_selections)
2140 if (!edit->column_highlight)
2141 edit_push_undo_action (edit, COLUMN_OFF);
2142 edit->column_highlight = 1;
2145 else
2147 off_t i;
2149 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2151 for (i = 0; i < blocklen; i++)
2152 edit_insert (edit, buf[i]);
2154 /* highlight inserted text then not persistent blocks */
2155 if (!option_persistent_selections && edit->modified)
2157 edit_set_markers (edit, edit->curs1, current, 0, 0);
2158 if (edit->column_highlight)
2159 edit_push_undo_action (edit, COLUMN_ON);
2160 edit->column_highlight = 0;
2164 edit->force |= REDRAW_PAGE;
2165 ins_len = edit->curs1 - current;
2166 edit_cursor_move (edit, -ins_len);
2167 g_free (buf);
2168 mc_close (file);
2169 if (blocklen != 0)
2170 ins_len = 0;
2173 return ins_len;
2176 /* --------------------------------------------------------------------------------------------- */
2178 * Fill in the edit structure. Return NULL on failure. Pass edit as
2179 * NULL to allocate a new structure.
2181 * If line is 0, try to restore saved position. Otherwise put the
2182 * cursor on that line and show it in the middle of the screen.
2185 WEdit *
2186 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2187 long line)
2189 gboolean to_free = FALSE;
2190 Widget *w;
2192 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2193 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2195 if (edit != NULL)
2196 edit_purge_widget (edit);
2197 else
2199 #ifdef ENABLE_NLS
2201 * Expand option_whole_chars_search by national letters using
2202 * current locale
2205 static char option_whole_chars_search_buf[256];
2207 if (option_whole_chars_search_buf != option_whole_chars_search)
2209 size_t i;
2210 size_t len = str_term_width1 (option_whole_chars_search);
2212 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2214 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2216 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2218 option_whole_chars_search_buf[len++] = i;
2222 option_whole_chars_search_buf[len] = 0;
2223 option_whole_chars_search = option_whole_chars_search_buf;
2225 #endif /* ENABLE_NLS */
2226 edit = g_malloc0 (sizeof (WEdit));
2227 to_free = TRUE;
2230 w = WIDGET (edit);
2232 w->y = y;
2233 w->x = x;
2234 w->lines = lines;
2235 w->cols = cols;
2236 edit_save_size (edit);
2237 edit->fullscreen = TRUE;
2238 edit->drag_state = MCEDIT_DRAG_NORMAL;
2240 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2241 edit->stat1.st_uid = getuid ();
2242 edit->stat1.st_gid = getgid ();
2243 edit->stat1.st_mtime = 0;
2245 edit->over_col = 0;
2246 edit->bracket = -1;
2247 edit->force |= REDRAW_PAGE;
2249 /* set file name before load file */
2250 edit_set_filename (edit, filename_vpath);
2252 edit->undo_stack_size = START_STACK_SIZE;
2253 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2254 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2256 edit->redo_stack_size = START_STACK_SIZE;
2257 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2258 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2260 #ifdef HAVE_CHARSET
2261 edit->utf8 = FALSE;
2262 edit->converter = str_cnv_from_term;
2263 edit_set_codeset (edit);
2264 #endif
2266 if (!edit_load_file (edit))
2268 /* edit_load_file already gives an error message */
2269 if (to_free)
2270 g_free (edit);
2271 return NULL;
2274 edit->loading_done = 1;
2275 edit->modified = 0;
2276 edit->locked = 0;
2277 edit_load_syntax (edit, NULL, NULL);
2278 edit_get_syntax_color (edit, -1);
2280 /* load saved cursor position */
2281 if ((line == 0) && option_save_position)
2282 edit_load_position (edit);
2283 else
2285 if (line <= 0)
2286 line = 1;
2287 edit_move_display (edit, line - 1);
2288 edit_move_to_line (edit, line - 1);
2291 edit_load_macro_cmd (edit);
2293 return edit;
2296 /* --------------------------------------------------------------------------------------------- */
2298 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2299 gboolean
2300 edit_clean (WEdit * edit)
2302 int j = 0;
2304 if (edit == NULL)
2305 return FALSE;
2307 /* a stale lock, remove it */
2308 if (edit->locked)
2309 edit->locked = unlock_file (edit->filename_vpath);
2311 /* save cursor position */
2312 if (option_save_position)
2313 edit_save_position (edit);
2314 else if (edit->serialized_bookmarks != NULL)
2315 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2317 /* File specified on the mcedit command line and never saved */
2318 if (edit->delete_file)
2319 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2321 edit_free_syntax_rules (edit);
2322 book_mark_flush (edit, -1);
2323 for (; j <= MAXBUFF; j++)
2325 g_free (edit->buffers1[j]);
2326 g_free (edit->buffers2[j]);
2329 g_free (edit->undo_stack);
2330 g_free (edit->redo_stack);
2331 vfs_path_free (edit->filename_vpath);
2332 vfs_path_free (edit->dir_vpath);
2333 mc_search_free (edit->search);
2334 edit->search = NULL;
2336 #ifdef HAVE_CHARSET
2337 if (edit->converter != str_cnv_from_term)
2338 str_close_conv (edit->converter);
2339 #endif
2341 edit_purge_widget (edit);
2343 return TRUE;
2346 /* --------------------------------------------------------------------------------------------- */
2349 * Load a new file into the editor and set line. If it fails, preserve the old file.
2350 * To do it, allocate a new widget, initialize it and, if the new file
2351 * was loaded, copy the data to the old widget.
2353 * @return TRUE on success, FALSE on failure.
2355 gboolean
2356 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2358 Widget *w = WIDGET (edit);
2359 WEdit *e;
2361 int y = w->y;
2362 int x = w->x;
2363 int lines = w->lines;
2364 int columns = w->cols;
2366 e = g_malloc0 (sizeof (WEdit));
2367 *WIDGET (e) = *w;
2369 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2371 g_free (e);
2372 return FALSE;
2375 edit_clean (edit);
2376 memcpy (edit, e, sizeof (WEdit));
2377 g_free (e);
2379 return TRUE;
2382 /* --------------------------------------------------------------------------------------------- */
2384 #ifdef HAVE_CHARSET
2385 void
2386 edit_set_codeset (WEdit * edit)
2388 const char *cp_id;
2390 cp_id =
2391 get_codepage_id (mc_global.source_codepage >=
2392 0 ? mc_global.source_codepage : mc_global.display_codepage);
2394 if (cp_id != NULL)
2396 GIConv conv;
2397 conv = str_crt_conv_from (cp_id);
2398 if (conv != INVALID_CONV)
2400 if (edit->converter != str_cnv_from_term)
2401 str_close_conv (edit->converter);
2402 edit->converter = conv;
2406 if (cp_id != NULL)
2407 edit->utf8 = str_isutf8 (cp_id);
2409 #endif
2412 /* --------------------------------------------------------------------------------------------- */
2415 * Recording stack for undo:
2416 * The following is an implementation of a compressed stack. Identical
2417 * pushes are recorded by a negative prefix indicating the number of times the
2418 * same char was pushed. This saves space for repeated curs-left or curs-right
2419 * delete etc.
2421 * eg:
2423 * pushed: stored:
2426 * b a
2427 * b -3
2428 * b b
2429 * c --> -4
2430 * c c
2431 * c d
2435 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2436 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2437 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2438 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2439 * position.
2441 * The only way the cursor moves or the buffer is changed is through the routines:
2442 * insert, backspace, insert_ahead, delete, and cursor_move.
2443 * These record the reverse undo movements onto the stack each time they are
2444 * called.
2446 * Each key press results in a set of actions (insert; delete ...). So each time
2447 * a key is pressed the current position of start_display is pushed as
2448 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2449 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2450 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2454 * @param edit editor object
2455 * @param c code of the action
2458 void
2459 edit_push_undo_action (WEdit * edit, long c)
2461 unsigned long sp = edit->undo_stack_pointer;
2462 unsigned long spm1;
2463 long *t;
2465 /* first enlarge the stack if necessary */
2466 if (sp > edit->undo_stack_size - 10)
2467 { /* say */
2468 if (option_max_undo < 256)
2469 option_max_undo = 256;
2470 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2472 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2473 if (t)
2475 edit->undo_stack = t;
2476 edit->undo_stack_size <<= 1;
2477 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2481 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2482 if (edit->undo_stack_disable)
2484 edit_push_redo_action (edit, KEY_PRESS);
2485 edit_push_redo_action (edit, c);
2486 return;
2489 if (edit->redo_stack_reset)
2490 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2492 if (edit->undo_stack_bottom != sp
2493 && spm1 != edit->undo_stack_bottom
2494 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2496 int d;
2497 if (edit->undo_stack[spm1] < 0)
2499 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2500 if (d == c && edit->undo_stack[spm1] > -1000000000)
2502 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2503 edit->undo_stack[spm1]--;
2504 return;
2507 else
2509 d = edit->undo_stack[spm1];
2510 if (d == c)
2512 if (c >= KEY_PRESS)
2513 return; /* --> no need to push multiple do-nothings */
2514 edit->undo_stack[sp] = -2;
2515 goto check_bottom;
2519 edit->undo_stack[sp] = c;
2521 check_bottom:
2522 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2524 /* if the sp wraps round and catches the undo_stack_bottom then erase
2525 * the first set of actions on the stack to make space - by moving
2526 * undo_stack_bottom forward one "key press" */
2527 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2528 if ((unsigned long) c == edit->undo_stack_bottom ||
2529 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2532 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2534 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2535 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2537 /*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: */
2538 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2539 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2541 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2545 void
2546 edit_push_redo_action (WEdit * edit, long c)
2548 unsigned long sp = edit->redo_stack_pointer;
2549 unsigned long spm1;
2550 long *t;
2551 /* first enlarge the stack if necessary */
2552 if (sp > edit->redo_stack_size - 10)
2553 { /* say */
2554 if (option_max_undo < 256)
2555 option_max_undo = 256;
2556 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2558 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2559 if (t)
2561 edit->redo_stack = t;
2562 edit->redo_stack_size <<= 1;
2563 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2567 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2569 if (edit->redo_stack_bottom != sp
2570 && spm1 != edit->redo_stack_bottom
2571 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2573 int d;
2574 if (edit->redo_stack[spm1] < 0)
2576 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2577 if (d == c && edit->redo_stack[spm1] > -1000000000)
2579 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2580 edit->redo_stack[spm1]--;
2581 return;
2584 else
2586 d = edit->redo_stack[spm1];
2587 if (d == c)
2589 if (c >= KEY_PRESS)
2590 return; /* --> no need to push multiple do-nothings */
2591 edit->redo_stack[sp] = -2;
2592 goto redo_check_bottom;
2596 edit->redo_stack[sp] = c;
2598 redo_check_bottom:
2599 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2601 /* if the sp wraps round and catches the redo_stack_bottom then erase
2602 * the first set of actions on the stack to make space - by moving
2603 * redo_stack_bottom forward one "key press" */
2604 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2605 if ((unsigned long) c == edit->redo_stack_bottom ||
2606 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2609 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2611 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2612 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2615 * If a single key produced enough pushes to wrap all the way round then
2616 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2617 * The stack is then initialised:
2620 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2621 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2622 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2625 /* --------------------------------------------------------------------------------------------- */
2627 Basic low level single character buffer alterations and movements at the cursor.
2628 Returns char passed over, inserted or removed.
2631 void
2632 edit_insert (WEdit * edit, int c)
2634 /* check if file has grown to large */
2635 if (edit->last_byte >= SIZE_LIMIT)
2636 return;
2638 /* first we must update the position of the display window */
2639 if (edit->curs1 < edit->start_display)
2641 edit->start_display++;
2642 if (c == '\n')
2643 edit->start_line++;
2646 /* Mark file as modified, unless the file hasn't been fully loaded */
2647 if (edit->loading_done)
2648 edit_modification (edit);
2650 /* now we must update some info on the file and check if a redraw is required */
2651 if (c == '\n')
2653 book_mark_inc (edit, edit->curs_line);
2654 edit->curs_line++;
2655 edit->total_lines++;
2656 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2659 /* save the reverse command onto the undo stack */
2660 /* ordinary char and not space */
2661 if (c > 32)
2662 edit_push_undo_action (edit, BACKSPACE);
2663 else
2664 edit_push_undo_action (edit, BACKSPACE_BR);
2665 /* update markers */
2666 edit->mark1 += (edit->mark1 > edit->curs1);
2667 edit->mark2 += (edit->mark2 > edit->curs1);
2668 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2670 /* add a new buffer if we've reached the end of the last one */
2671 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2672 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2674 /* perform the insertion */
2675 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2676 = (unsigned char) c;
2678 /* update file length */
2679 edit->last_byte++;
2681 /* update cursor position */
2682 edit->curs1++;
2685 /* --------------------------------------------------------------------------------------------- */
2686 /** same as edit_insert and move left */
2688 void
2689 edit_insert_ahead (WEdit * edit, int c)
2691 if (edit->last_byte >= SIZE_LIMIT)
2692 return;
2694 if (edit->curs1 < edit->start_display)
2696 edit->start_display++;
2697 if (c == '\n')
2698 edit->start_line++;
2700 edit_modification (edit);
2701 if (c == '\n')
2703 book_mark_inc (edit, edit->curs_line);
2704 edit->total_lines++;
2705 edit->force |= REDRAW_AFTER_CURSOR;
2707 /* ordinary char and not space */
2708 if (c > 32)
2709 edit_push_undo_action (edit, DELCHAR);
2710 else
2711 edit_push_undo_action (edit, DELCHAR_BR);
2713 edit->mark1 += (edit->mark1 >= edit->curs1);
2714 edit->mark2 += (edit->mark2 >= edit->curs1);
2715 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2717 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2718 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2719 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2720 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2722 edit->last_byte++;
2723 edit->curs2++;
2727 /* --------------------------------------------------------------------------------------------- */
2730 edit_delete (WEdit * edit, gboolean byte_delete)
2732 int p = 0;
2733 int cw = 1;
2734 int i;
2736 if (edit->curs2 == 0)
2737 return 0;
2739 #ifdef HAVE_CHARSET
2740 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2741 if (edit->utf8 && !byte_delete)
2743 edit_get_utf (edit, edit->curs1, &cw);
2744 if (cw < 1)
2745 cw = 1;
2747 #else
2748 (void) byte_delete;
2749 #endif
2751 if (edit->mark2 != edit->mark1)
2752 edit_push_markers (edit);
2754 for (i = 1; i <= cw; i++)
2756 if (edit->mark1 > edit->curs1)
2758 edit->mark1--;
2759 edit->end_mark_curs--;
2761 if (edit->mark2 > edit->curs1)
2762 edit->mark2--;
2763 if (edit->last_get_rule > edit->curs1)
2764 edit->last_get_rule--;
2766 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2767 ((edit->curs2 -
2768 1) & M_EDIT_BUF_SIZE) - 1];
2770 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2772 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2773 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2775 edit->last_byte--;
2776 edit->curs2--;
2777 edit_push_undo_action (edit, p + 256);
2780 edit_modification (edit);
2781 if (p == '\n')
2783 book_mark_dec (edit, edit->curs_line);
2784 edit->total_lines--;
2785 edit->force |= REDRAW_AFTER_CURSOR;
2787 if (edit->curs1 < edit->start_display)
2789 edit->start_display--;
2790 if (p == '\n')
2791 edit->start_line--;
2794 return p;
2797 /* --------------------------------------------------------------------------------------------- */
2800 edit_backspace (WEdit * edit, gboolean byte_delete)
2802 int p = 0;
2803 int cw = 1;
2804 int i;
2806 if (edit->curs1 == 0)
2807 return 0;
2809 if (edit->mark2 != edit->mark1)
2810 edit_push_markers (edit);
2812 #ifdef HAVE_CHARSET
2813 if (edit->utf8 && !byte_delete)
2815 edit_get_prev_utf (edit, edit->curs1, &cw);
2816 if (cw < 1)
2817 cw = 1;
2819 #else
2820 (void) byte_delete;
2821 #endif
2823 for (i = 1; i <= cw; i++)
2825 if (edit->mark1 >= edit->curs1)
2827 edit->mark1--;
2828 edit->end_mark_curs--;
2830 if (edit->mark2 >= edit->curs1)
2831 edit->mark2--;
2832 if (edit->last_get_rule >= edit->curs1)
2833 edit->last_get_rule--;
2835 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2836 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2837 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2839 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2840 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2842 edit->last_byte--;
2843 edit->curs1--;
2844 edit_push_undo_action (edit, p);
2846 edit_modification (edit);
2847 if (p == '\n')
2849 book_mark_dec (edit, edit->curs_line);
2850 edit->curs_line--;
2851 edit->total_lines--;
2852 edit->force |= REDRAW_AFTER_CURSOR;
2855 if (edit->curs1 < edit->start_display)
2857 edit->start_display--;
2858 if (p == '\n')
2859 edit->start_line--;
2862 return p;
2865 /* --------------------------------------------------------------------------------------------- */
2866 /** moves the cursor right or left: increment positive or negative respectively */
2868 void
2869 edit_cursor_move (WEdit * edit, off_t increment)
2871 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2872 int c;
2874 if (increment < 0)
2876 for (; increment < 0; increment++)
2878 if (edit->curs1 == 0)
2879 return;
2881 edit_push_undo_action (edit, CURS_RIGHT);
2883 c = edit_get_byte (edit, edit->curs1 - 1);
2884 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2885 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2886 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2887 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2888 edit->curs2++;
2889 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2890 1) & M_EDIT_BUF_SIZE];
2891 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2893 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2894 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2896 edit->curs1--;
2897 if (c == '\n')
2899 edit->curs_line--;
2900 edit->force |= REDRAW_LINE_BELOW;
2905 else
2907 for (; increment > 0; increment--)
2909 if (edit->curs2 == 0)
2910 return;
2912 edit_push_undo_action (edit, CURS_LEFT);
2914 c = edit_get_byte (edit, edit->curs1);
2915 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2916 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2917 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2918 edit->curs1++;
2919 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2920 ((edit->curs2 -
2921 1) & M_EDIT_BUF_SIZE) - 1];
2922 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2924 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2925 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2927 edit->curs2--;
2928 if (c == '\n')
2930 edit->curs_line++;
2931 edit->force |= REDRAW_LINE_ABOVE;
2937 /* These functions return positions relative to lines */
2939 /* --------------------------------------------------------------------------------------------- */
2940 /** returns index of last char on line + 1 */
2942 off_t
2943 edit_eol (const WEdit * edit, off_t current)
2945 if (current >= edit->last_byte)
2946 return edit->last_byte;
2948 for (; edit_get_byte (edit, current) != '\n'; current++)
2951 return current;
2954 /* --------------------------------------------------------------------------------------------- */
2955 /** returns index of first char on line */
2957 off_t
2958 edit_bol (const WEdit * edit, off_t current)
2960 if (current <= 0)
2961 return 0;
2963 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
2966 return current;
2969 /* --------------------------------------------------------------------------------------------- */
2971 long
2972 edit_count_lines (const WEdit * edit, off_t current, off_t upto)
2974 long lines = 0;
2976 if (upto > edit->last_byte)
2977 upto = edit->last_byte;
2978 if (current < 0)
2979 current = 0;
2980 while (current < upto)
2981 if (edit_get_byte (edit, current++) == '\n')
2982 lines++;
2983 return lines;
2986 /* --------------------------------------------------------------------------------------------- */
2987 /* If lines is zero this returns the count of lines from current to upto. */
2988 /* If upto is zero returns index of lines forward current. */
2990 off_t
2991 edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto)
2993 if (upto != 0)
2995 return (off_t) edit_count_lines (edit, current, upto);
2997 else
2999 long next;
3000 if (lines < 0)
3001 lines = 0;
3002 while (lines-- != 0)
3004 next = edit_eol (edit, current) + 1;
3005 if (next > edit->last_byte)
3006 break;
3007 else
3008 current = next;
3010 return current;
3014 /* --------------------------------------------------------------------------------------------- */
3015 /** Returns offset of 'lines' lines up from current */
3017 off_t
3018 edit_move_backward (const WEdit * edit, off_t current, long lines)
3020 if (lines < 0)
3021 lines = 0;
3022 current = edit_bol (edit, current);
3023 while (lines-- != 0 && current != 0)
3024 current = edit_bol (edit, current - 1);
3025 return current;
3028 /* --------------------------------------------------------------------------------------------- */
3029 /* If cols is zero this returns the count of columns from current to upto. */
3030 /* If upto is zero returns index of cols across from current. */
3032 off_t
3033 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
3035 off_t p, q;
3036 long col;
3038 if (upto != 0)
3040 q = upto;
3041 cols = -10;
3043 else
3044 q = edit->last_byte + 2;
3046 for (col = 0, p = current; p < q; p++)
3048 int c, orig_c;
3050 if (cols != -10)
3052 if (col == cols)
3053 return p;
3054 if (col > cols)
3055 return p - 1;
3058 orig_c = c = edit_get_byte (edit, p);
3060 #ifdef HAVE_CHARSET
3061 if (edit->utf8)
3063 int utf_ch;
3064 int cw = 1;
3066 utf_ch = edit_get_utf (edit, p, &cw);
3067 if (mc_global.utf8_display)
3069 if (cw > 1)
3070 col -= cw - 1;
3071 if (g_unichar_iswide (utf_ch))
3072 col++;
3074 else if (cw > 1 && g_unichar_isprint (utf_ch))
3075 col -= cw - 1;
3078 c = convert_to_display_c (c);
3079 #endif
3081 if (c == '\n')
3082 return (upto != 0 ? (off_t) col : p);
3083 if (c == '\t')
3084 col += TAB_SIZE - col % TAB_SIZE;
3085 else if ((c < 32 || c == 127) && (orig_c == c
3086 #ifdef HAVE_CHARSET
3087 || (!mc_global.utf8_display && !edit->utf8)
3088 #endif
3090 /* '\r' is shown as ^M, so we must advance 2 characters */
3091 /* Caret notation for control characters */
3092 col += 2;
3093 else
3094 col++;
3096 return (off_t) col;
3099 /* --------------------------------------------------------------------------------------------- */
3100 /** returns the current column position of the cursor */
3102 long
3103 edit_get_col (const WEdit * edit)
3105 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3108 /* --------------------------------------------------------------------------------------------- */
3109 /* Scrolling functions */
3110 /* --------------------------------------------------------------------------------------------- */
3112 void
3113 edit_update_curs_row (WEdit * edit)
3115 edit->curs_row = edit->curs_line - edit->start_line;
3118 /* --------------------------------------------------------------------------------------------- */
3120 void
3121 edit_update_curs_col (WEdit * edit)
3123 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3126 /* --------------------------------------------------------------------------------------------- */
3128 long
3129 edit_get_curs_col (const WEdit * edit)
3131 return edit->curs_col;
3134 /* --------------------------------------------------------------------------------------------- */
3135 /** moves the display start position up by i lines */
3137 void
3138 edit_scroll_upward (WEdit * edit, long i)
3140 long lines_above = edit->start_line;
3142 if (i > lines_above)
3143 i = lines_above;
3144 if (i != 0)
3146 edit->start_line -= i;
3147 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3148 edit->force |= REDRAW_PAGE;
3149 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3151 edit_update_curs_row (edit);
3155 /* --------------------------------------------------------------------------------------------- */
3157 void
3158 edit_scroll_downward (WEdit * edit, long i)
3160 long lines_below;
3162 lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1);
3163 if (lines_below > 0)
3165 if (i > lines_below)
3166 i = lines_below;
3167 edit->start_line += i;
3168 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3169 edit->force |= REDRAW_PAGE;
3170 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3172 edit_update_curs_row (edit);
3175 /* --------------------------------------------------------------------------------------------- */
3177 void
3178 edit_scroll_right (WEdit * edit, long i)
3180 edit->force |= REDRAW_PAGE;
3181 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3182 edit->start_col -= i;
3185 /* --------------------------------------------------------------------------------------------- */
3187 void
3188 edit_scroll_left (WEdit * edit, long i)
3190 if (edit->start_col)
3192 edit->start_col += i;
3193 if (edit->start_col > 0)
3194 edit->start_col = 0;
3195 edit->force |= REDRAW_PAGE;
3196 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3200 /* --------------------------------------------------------------------------------------------- */
3201 /* high level cursor movement commands */
3202 /* --------------------------------------------------------------------------------------------- */
3204 void
3205 edit_move_to_prev_col (WEdit * edit, off_t p)
3207 long prev = edit->prev_col;
3208 long over = edit->over_col;
3210 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3212 if (option_cursor_beyond_eol)
3214 long line_len;
3216 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3217 edit_eol (edit, edit->curs1));
3218 if (line_len < prev + edit->over_col)
3220 edit->over_col = prev + over - line_len;
3221 edit->prev_col = line_len;
3222 edit->curs_col = line_len;
3224 else
3226 edit->curs_col = prev + over;
3227 edit->prev_col = edit->curs_col;
3228 edit->over_col = 0;
3231 else
3233 edit->over_col = 0;
3234 if (option_fake_half_tabs && is_in_indent (edit))
3236 edit_update_curs_col (edit);
3237 if (space_width != 0 && edit->curs_col % (HALF_TAB_SIZE * space_width) != 0)
3239 int q;
3241 q = edit->curs_col;
3242 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3243 p = edit_bol (edit, edit->curs1);
3244 edit_cursor_move (edit,
3245 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
3246 if (!left_of_four_spaces (edit))
3247 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3253 /* --------------------------------------------------------------------------------------------- */
3254 /** check whether line in editor is blank or not
3256 * @param edit editor object
3257 * @param line number of line
3259 * @return TRUE if line in blank, FALSE otherwise
3262 gboolean
3263 edit_line_is_blank (WEdit * edit, long line)
3265 return is_blank (edit, edit_find_line (edit, line));
3268 /* --------------------------------------------------------------------------------------------- */
3269 /** move cursor to line 'line' */
3271 void
3272 edit_move_to_line (WEdit * e, long line)
3274 if (line < e->curs_line)
3275 edit_move_up (e, e->curs_line - line, 0);
3276 else
3277 edit_move_down (e, line - e->curs_line, 0);
3278 edit_scroll_screen_over_cursor (e);
3281 /* --------------------------------------------------------------------------------------------- */
3282 /** scroll window so that first visible line is 'line' */
3284 void
3285 edit_move_display (WEdit * e, long line)
3287 if (line < e->start_line)
3288 edit_scroll_upward (e, e->start_line - line);
3289 else
3290 edit_scroll_downward (e, line - e->start_line);
3293 /* --------------------------------------------------------------------------------------------- */
3294 /** save markers onto undo stack */
3296 void
3297 edit_push_markers (WEdit * edit)
3299 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3300 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3301 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3304 /* --------------------------------------------------------------------------------------------- */
3306 void
3307 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3309 edit->mark1 = m1;
3310 edit->mark2 = m2;
3311 edit->column1 = c1;
3312 edit->column2 = c2;
3316 /* --------------------------------------------------------------------------------------------- */
3317 /** highlight marker toggle */
3319 void
3320 edit_mark_cmd (WEdit * edit, gboolean unmark)
3322 edit_push_markers (edit);
3323 if (unmark)
3325 edit_set_markers (edit, 0, 0, 0, 0);
3326 edit->force |= REDRAW_PAGE;
3328 else if (edit->mark2 >= 0)
3330 edit->end_mark_curs = -1;
3331 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3332 edit->curs_col + edit->over_col);
3333 edit->force |= REDRAW_PAGE;
3335 else
3337 edit->end_mark_curs = edit->curs1;
3338 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3339 edit->curs_col + edit->over_col);
3343 /* --------------------------------------------------------------------------------------------- */
3344 /** highlight the word under cursor */
3346 void
3347 edit_mark_current_word_cmd (WEdit * edit)
3349 long pos;
3351 for (pos = edit->curs1; pos != 0; pos--)
3353 int c1, c2;
3355 c1 = edit_get_byte (edit, pos);
3356 c2 = edit_get_byte (edit, pos - 1);
3357 if (!isspace (c1) && isspace (c2))
3358 break;
3359 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3360 break;
3362 edit->mark1 = pos;
3364 for (; pos < edit->last_byte; pos++)
3366 int c1, c2;
3368 c1 = edit_get_byte (edit, pos);
3369 c2 = edit_get_byte (edit, pos + 1);
3370 if (!isspace (c1) && isspace (c2))
3371 break;
3372 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3373 break;
3375 edit->mark2 = min (pos + 1, edit->last_byte);
3377 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3380 /* --------------------------------------------------------------------------------------------- */
3382 void
3383 edit_mark_current_line_cmd (WEdit * edit)
3385 long pos = edit->curs1;
3387 edit->mark1 = edit_bol (edit, pos);
3388 edit->mark2 = edit_eol (edit, pos);
3390 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3393 /* --------------------------------------------------------------------------------------------- */
3395 void
3396 edit_delete_line (WEdit * edit)
3399 * Delete right part of the line.
3400 * Note that edit_get_byte() returns '\n' when byte position is
3401 * beyond EOF.
3403 while (edit_get_byte (edit, edit->curs1) != '\n')
3404 (void) edit_delete (edit, TRUE);
3407 * Delete '\n' char.
3408 * Note that edit_delete() will not corrupt anything if called while
3409 * cursor position is EOF.
3411 (void) edit_delete (edit, TRUE);
3414 * Delete left part of the line.
3415 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3417 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3418 (void) edit_backspace (edit, TRUE);
3421 /* --------------------------------------------------------------------------------------------- */
3423 long
3424 edit_indent_width (const WEdit * edit, off_t p)
3426 off_t q = p;
3428 /* move to the end of the leading whitespace of the line */
3429 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3430 q++;
3431 /* count the number of columns of indentation */
3432 return (long) edit_move_forward3 (edit, p, 0, q);
3435 /* --------------------------------------------------------------------------------------------- */
3437 void
3438 edit_insert_indent (WEdit * edit, int indent)
3440 if (!option_fill_tabs_with_spaces)
3442 while (indent >= TAB_SIZE)
3444 edit_insert (edit, '\t');
3445 indent -= TAB_SIZE;
3448 while (indent-- > 0)
3449 edit_insert (edit, ' ');
3452 /* --------------------------------------------------------------------------------------------- */
3454 void
3455 edit_push_key_press (WEdit * edit)
3457 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3458 if (edit->mark2 == -1)
3460 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3461 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3465 /* --------------------------------------------------------------------------------------------- */
3467 void
3468 edit_find_bracket (WEdit * edit)
3470 edit->bracket = edit_get_bracket (edit, 1, 10000);
3471 if (last_bracket != edit->bracket)
3472 edit->force |= REDRAW_PAGE;
3473 last_bracket = edit->bracket;
3476 /* --------------------------------------------------------------------------------------------- */
3478 * This executes a command as though the user initiated it through a key
3479 * press. Callback with WIDGET_KEY as a message calls this after
3480 * translating the key press. This function can be used to pass any
3481 * command to the editor. Note that the screen wouldn't update
3482 * automatically. Either of command or char_for_insertion must be
3483 * passed as -1. Commands are executed, and char_for_insertion is
3484 * inserted at the cursor.
3487 void
3488 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3490 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3491 || (macro_index < 0
3492 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3494 macro_index = 0;
3495 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3496 return;
3498 if (macro_index != -1)
3500 edit->force |= REDRAW_COMPLETELY;
3501 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3503 edit_store_macro_cmd (edit);
3504 macro_index = -1;
3505 return;
3507 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3509 edit_repeat_macro_cmd (edit);
3510 macro_index = -1;
3511 return;
3515 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3517 record_macro_buf[macro_index].action = command;
3518 record_macro_buf[macro_index++].ch = char_for_insertion;
3520 /* record the beginning of a set of editing actions initiated by a key press */
3521 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3522 edit_push_key_press (edit);
3524 edit_execute_cmd (edit, command, char_for_insertion);
3525 if (edit->column_highlight)
3526 edit->force |= REDRAW_PAGE;
3529 /* --------------------------------------------------------------------------------------------- */
3531 This executes a command at a lower level than macro recording.
3532 It also does not push a key_press onto the undo stack. This means
3533 that if it is called many times, a single undo command will undo
3534 all of them. It also does not check for the Undo command.
3536 void
3537 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3539 Widget *w = WIDGET (edit);
3541 if (command == CK_WindowFullscreen)
3543 edit_toggle_fullscreen (edit);
3544 return;
3547 /* handle window state */
3548 if (edit_handle_move_resize (edit, command))
3549 return;
3551 edit->force |= REDRAW_LINE;
3553 /* The next key press will unhighlight the found string, so update
3554 * the whole page */
3555 if (edit->found_len || edit->column_highlight)
3556 edit->force |= REDRAW_PAGE;
3558 switch (command)
3560 /* a mark command with shift-arrow */
3561 case CK_MarkLeft:
3562 case CK_MarkRight:
3563 case CK_MarkToWordBegin:
3564 case CK_MarkToWordEnd:
3565 case CK_MarkToHome:
3566 case CK_MarkToEnd:
3567 case CK_MarkUp:
3568 case CK_MarkDown:
3569 case CK_MarkPageUp:
3570 case CK_MarkPageDown:
3571 case CK_MarkToFileBegin:
3572 case CK_MarkToFileEnd:
3573 case CK_MarkToPageBegin:
3574 case CK_MarkToPageEnd:
3575 case CK_MarkScrollUp:
3576 case CK_MarkScrollDown:
3577 case CK_MarkParagraphUp:
3578 case CK_MarkParagraphDown:
3579 /* a mark command with alt-arrow */
3580 case CK_MarkColumnPageUp:
3581 case CK_MarkColumnPageDown:
3582 case CK_MarkColumnLeft:
3583 case CK_MarkColumnRight:
3584 case CK_MarkColumnUp:
3585 case CK_MarkColumnDown:
3586 case CK_MarkColumnScrollUp:
3587 case CK_MarkColumnScrollDown:
3588 case CK_MarkColumnParagraphUp:
3589 case CK_MarkColumnParagraphDown:
3590 edit->column_highlight = 0;
3591 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3593 edit_mark_cmd (edit, TRUE); /* clear */
3594 edit_mark_cmd (edit, FALSE); /* marking on */
3596 edit->highlight = 1;
3597 break;
3599 /* any other command */
3600 default:
3601 if (edit->highlight)
3602 edit_mark_cmd (edit, FALSE); /* clear */
3603 edit->highlight = 0;
3606 /* first check for undo */
3607 if (command == CK_Undo)
3609 edit->redo_stack_reset = 0;
3610 edit_group_undo (edit);
3611 edit->found_len = 0;
3612 edit->prev_col = edit_get_col (edit);
3613 edit->search_start = edit->curs1;
3614 return;
3616 /* check for redo */
3617 if (command == CK_Redo)
3619 edit->redo_stack_reset = 0;
3620 edit_do_redo (edit);
3621 edit->found_len = 0;
3622 edit->prev_col = edit_get_col (edit);
3623 edit->search_start = edit->curs1;
3624 return;
3627 edit->redo_stack_reset = 1;
3629 /* An ordinary key press */
3630 if (char_for_insertion >= 0)
3632 /* if non persistent selection and text selected */
3633 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3634 edit_block_delete_cmd (edit);
3636 if (edit->overwrite)
3638 /* remove char only one time, after input first byte, multibyte chars */
3639 #ifdef HAVE_CHARSET
3640 if (!mc_global.utf8_display || edit->charpoint == 0)
3641 #endif
3642 if (edit_get_byte (edit, edit->curs1) != '\n')
3644 edit_delete (edit, FALSE);
3646 if (option_cursor_beyond_eol && edit->over_col > 0)
3647 edit_insert_over (edit);
3648 #ifdef HAVE_CHARSET
3649 if (char_for_insertion > 255 && !mc_global.utf8_display)
3651 unsigned char str[6 + 1];
3652 size_t i = 0;
3653 int res;
3655 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3656 if (res == 0)
3658 str[0] = '.';
3659 str[1] = '\0';
3661 else
3663 str[res] = '\0';
3665 while (str[i] != 0 && i <= 6)
3667 char_for_insertion = str[i];
3668 edit_insert (edit, char_for_insertion);
3669 i++;
3672 else
3673 #endif
3674 edit_insert (edit, char_for_insertion);
3676 if (option_auto_para_formatting)
3678 format_paragraph (edit, 0);
3679 edit->force |= REDRAW_PAGE;
3681 else
3682 check_and_wrap_line (edit);
3683 edit->found_len = 0;
3684 edit->prev_col = edit_get_col (edit);
3685 edit->search_start = edit->curs1;
3686 edit_find_bracket (edit);
3687 return;
3690 switch (command)
3692 case CK_TopOnScreen:
3693 case CK_BottomOnScreen:
3694 case CK_Top:
3695 case CK_Bottom:
3696 case CK_PageUp:
3697 case CK_PageDown:
3698 case CK_Home:
3699 case CK_End:
3700 case CK_Up:
3701 case CK_Down:
3702 case CK_Left:
3703 case CK_Right:
3704 case CK_WordLeft:
3705 case CK_WordRight:
3706 if (!option_persistent_selections && edit->mark2 >= 0)
3708 if (edit->column_highlight)
3709 edit_push_undo_action (edit, COLUMN_ON);
3710 edit->column_highlight = 0;
3711 edit_mark_cmd (edit, TRUE);
3715 switch (command)
3717 case CK_TopOnScreen:
3718 case CK_BottomOnScreen:
3719 case CK_MarkToPageBegin:
3720 case CK_MarkToPageEnd:
3721 case CK_Up:
3722 case CK_Down:
3723 case CK_WordLeft:
3724 case CK_WordRight:
3725 case CK_MarkToWordBegin:
3726 case CK_MarkToWordEnd:
3727 case CK_MarkUp:
3728 case CK_MarkDown:
3729 case CK_MarkColumnUp:
3730 case CK_MarkColumnDown:
3731 if (edit->mark2 == -1)
3732 break; /*marking is following the cursor: may need to highlight a whole line */
3733 case CK_Left:
3734 case CK_Right:
3735 case CK_MarkLeft:
3736 case CK_MarkRight:
3737 edit->force |= REDRAW_CHAR_ONLY;
3740 /* basic cursor key commands */
3741 switch (command)
3743 case CK_BackSpace:
3744 /* if non persistent selection and text selected */
3745 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3746 edit_block_delete_cmd (edit);
3747 else if (option_cursor_beyond_eol && edit->over_col > 0)
3748 edit->over_col--;
3749 else if (option_backspace_through_tabs && is_in_indent (edit))
3751 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3752 edit_backspace (edit, TRUE);
3754 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3756 int i;
3758 for (i = 0; i < HALF_TAB_SIZE; i++)
3759 edit_backspace (edit, TRUE);
3761 else
3762 edit_backspace (edit, FALSE);
3763 break;
3764 case CK_Delete:
3765 /* if non persistent selection and text selected */
3766 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3767 edit_block_delete_cmd (edit);
3768 else
3770 if (option_cursor_beyond_eol && edit->over_col > 0)
3771 edit_insert_over (edit);
3773 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3775 int i;
3777 for (i = 1; i <= HALF_TAB_SIZE; i++)
3778 edit_delete (edit, TRUE);
3780 else
3781 edit_delete (edit, FALSE);
3783 break;
3784 case CK_DeleteToWordBegin:
3785 edit->over_col = 0;
3786 edit_left_delete_word (edit);
3787 break;
3788 case CK_DeleteToWordEnd:
3789 if (option_cursor_beyond_eol && edit->over_col > 0)
3790 edit_insert_over (edit);
3792 edit_right_delete_word (edit);
3793 break;
3794 case CK_DeleteLine:
3795 edit_delete_line (edit);
3796 break;
3797 case CK_DeleteToHome:
3798 edit_delete_to_line_begin (edit);
3799 break;
3800 case CK_DeleteToEnd:
3801 edit_delete_to_line_end (edit);
3802 break;
3803 case CK_Enter:
3804 edit->over_col = 0;
3805 if (option_auto_para_formatting)
3807 edit_double_newline (edit);
3808 if (option_return_does_auto_indent)
3809 edit_auto_indent (edit);
3810 format_paragraph (edit, 0);
3812 else
3814 edit_insert (edit, '\n');
3815 if (option_return_does_auto_indent)
3816 edit_auto_indent (edit);
3818 break;
3819 case CK_Return:
3820 edit_insert (edit, '\n');
3821 break;
3823 case CK_MarkColumnPageUp:
3824 edit->column_highlight = 1;
3825 case CK_PageUp:
3826 case CK_MarkPageUp:
3827 edit_move_up (edit, w->lines - 1, 1);
3828 break;
3829 case CK_MarkColumnPageDown:
3830 edit->column_highlight = 1;
3831 case CK_PageDown:
3832 case CK_MarkPageDown:
3833 edit_move_down (edit, w->lines - 1, 1);
3834 break;
3835 case CK_MarkColumnLeft:
3836 edit->column_highlight = 1;
3837 case CK_Left:
3838 case CK_MarkLeft:
3839 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3841 if (option_cursor_beyond_eol && edit->over_col > 0)
3842 edit->over_col--;
3843 else
3844 edit_cursor_move (edit, -HALF_TAB_SIZE);
3845 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3847 else
3848 edit_left_char_move_cmd (edit);
3849 break;
3850 case CK_MarkColumnRight:
3851 edit->column_highlight = 1;
3852 case CK_Right:
3853 case CK_MarkRight:
3854 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3856 edit_cursor_move (edit, HALF_TAB_SIZE);
3857 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3859 else
3860 edit_right_char_move_cmd (edit);
3861 break;
3862 case CK_TopOnScreen:
3863 case CK_MarkToPageBegin:
3864 edit_begin_page (edit);
3865 break;
3866 case CK_BottomOnScreen:
3867 case CK_MarkToPageEnd:
3868 edit_end_page (edit);
3869 break;
3870 case CK_WordLeft:
3871 case CK_MarkToWordBegin:
3872 edit->over_col = 0;
3873 edit_left_word_move_cmd (edit);
3874 break;
3875 case CK_WordRight:
3876 case CK_MarkToWordEnd:
3877 edit->over_col = 0;
3878 edit_right_word_move_cmd (edit);
3879 break;
3880 case CK_MarkColumnUp:
3881 edit->column_highlight = 1;
3882 case CK_Up:
3883 case CK_MarkUp:
3884 edit_move_up (edit, 1, 0);
3885 break;
3886 case CK_MarkColumnDown:
3887 edit->column_highlight = 1;
3888 case CK_Down:
3889 case CK_MarkDown:
3890 edit_move_down (edit, 1, 0);
3891 break;
3892 case CK_MarkColumnParagraphUp:
3893 edit->column_highlight = 1;
3894 case CK_ParagraphUp:
3895 case CK_MarkParagraphUp:
3896 edit_move_up_paragraph (edit, 0);
3897 break;
3898 case CK_MarkColumnParagraphDown:
3899 edit->column_highlight = 1;
3900 case CK_ParagraphDown:
3901 case CK_MarkParagraphDown:
3902 edit_move_down_paragraph (edit, 0);
3903 break;
3904 case CK_MarkColumnScrollUp:
3905 edit->column_highlight = 1;
3906 case CK_ScrollUp:
3907 case CK_MarkScrollUp:
3908 edit_move_up (edit, 1, 1);
3909 break;
3910 case CK_MarkColumnScrollDown:
3911 edit->column_highlight = 1;
3912 case CK_ScrollDown:
3913 case CK_MarkScrollDown:
3914 edit_move_down (edit, 1, 1);
3915 break;
3916 case CK_Home:
3917 case CK_MarkToHome:
3918 edit_cursor_to_bol (edit);
3919 break;
3920 case CK_End:
3921 case CK_MarkToEnd:
3922 edit_cursor_to_eol (edit);
3923 break;
3924 case CK_Tab:
3925 /* if text marked shift block */
3926 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3928 if (edit->mark2 < 0)
3929 edit_mark_cmd (edit, FALSE);
3930 edit_move_block_to_right (edit);
3932 else
3934 if (option_cursor_beyond_eol)
3935 edit_insert_over (edit);
3936 edit_tab_cmd (edit);
3937 if (option_auto_para_formatting)
3939 format_paragraph (edit, 0);
3940 edit->force |= REDRAW_PAGE;
3942 else
3943 check_and_wrap_line (edit);
3945 break;
3947 case CK_InsertOverwrite:
3948 edit->overwrite = !edit->overwrite;
3949 break;
3951 case CK_Mark:
3952 if (edit->mark2 >= 0)
3954 if (edit->column_highlight)
3955 edit_push_undo_action (edit, COLUMN_ON);
3956 edit->column_highlight = 0;
3958 edit_mark_cmd (edit, FALSE);
3959 break;
3960 case CK_MarkColumn:
3961 if (!edit->column_highlight)
3962 edit_push_undo_action (edit, COLUMN_OFF);
3963 edit->column_highlight = 1;
3964 edit_mark_cmd (edit, FALSE);
3965 break;
3966 case CK_MarkAll:
3967 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3968 edit->force |= REDRAW_PAGE;
3969 break;
3970 case CK_Unmark:
3971 if (edit->column_highlight)
3972 edit_push_undo_action (edit, COLUMN_ON);
3973 edit->column_highlight = 0;
3974 edit_mark_cmd (edit, TRUE);
3975 break;
3976 case CK_MarkWord:
3977 if (edit->column_highlight)
3978 edit_push_undo_action (edit, COLUMN_ON);
3979 edit->column_highlight = 0;
3980 edit_mark_current_word_cmd (edit);
3981 break;
3982 case CK_MarkLine:
3983 if (edit->column_highlight)
3984 edit_push_undo_action (edit, COLUMN_ON);
3985 edit->column_highlight = 0;
3986 edit_mark_current_line_cmd (edit);
3987 break;
3989 case CK_Bookmark:
3990 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3991 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3992 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3993 else
3994 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3995 break;
3996 case CK_BookmarkFlush:
3997 book_mark_flush (edit, BOOK_MARK_COLOR);
3998 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3999 edit->force |= REDRAW_PAGE;
4000 break;
4001 case CK_BookmarkNext:
4002 if (edit->book_mark != NULL)
4004 edit_book_mark_t *p;
4006 p = book_mark_find (edit, edit->curs_line);
4007 if (p->next != NULL)
4009 p = p->next;
4010 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4011 edit_move_display (edit, p->line - w->lines / 2);
4012 edit_move_to_line (edit, p->line);
4015 break;
4016 case CK_BookmarkPrev:
4017 if (edit->book_mark != NULL)
4019 edit_book_mark_t *p;
4021 p = book_mark_find (edit, edit->curs_line);
4022 while (p->line == edit->curs_line)
4023 if (p->prev != NULL)
4024 p = p->prev;
4025 if (p->line >= 0)
4027 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
4028 edit_move_display (edit, p->line - w->lines / 2);
4029 edit_move_to_line (edit, p->line);
4032 break;
4034 case CK_Top:
4035 case CK_MarkToFileBegin:
4036 edit_move_to_top (edit);
4037 break;
4038 case CK_Bottom:
4039 case CK_MarkToFileEnd:
4040 edit_move_to_bottom (edit);
4041 break;
4043 case CK_Copy:
4044 if (option_cursor_beyond_eol && edit->over_col > 0)
4045 edit_insert_over (edit);
4046 edit_block_copy_cmd (edit);
4047 break;
4048 case CK_Remove:
4049 edit_block_delete_cmd (edit);
4050 break;
4051 case CK_Move:
4052 edit_block_move_cmd (edit);
4053 break;
4055 case CK_BlockShiftLeft:
4056 if (edit->mark1 != edit->mark2)
4057 edit_move_block_to_left (edit);
4058 break;
4059 case CK_BlockShiftRight:
4060 if (edit->mark1 != edit->mark2)
4061 edit_move_block_to_right (edit);
4062 break;
4063 case CK_Store:
4064 edit_copy_to_X_buf_cmd (edit);
4065 break;
4066 case CK_Cut:
4067 edit_cut_to_X_buf_cmd (edit);
4068 break;
4069 case CK_Paste:
4070 /* if non persistent selection and text selected */
4071 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4072 edit_block_delete_cmd (edit);
4073 if (option_cursor_beyond_eol && edit->over_col > 0)
4074 edit_insert_over (edit);
4075 edit_paste_from_X_buf_cmd (edit);
4076 if (!option_persistent_selections && edit->mark2 >= 0)
4078 if (edit->column_highlight)
4079 edit_push_undo_action (edit, COLUMN_ON);
4080 edit->column_highlight = 0;
4081 edit_mark_cmd (edit, TRUE);
4083 break;
4084 case CK_History:
4085 edit_paste_from_history (edit);
4086 break;
4088 case CK_SaveAs:
4089 edit_save_as_cmd (edit);
4090 break;
4091 case CK_Save:
4092 edit_save_confirm_cmd (edit);
4093 break;
4094 case CK_BlockSave:
4095 edit_save_block_cmd (edit);
4096 break;
4097 case CK_InsertFile:
4098 edit_insert_file_cmd (edit);
4099 break;
4101 case CK_FilePrev:
4102 edit_load_back_cmd (edit);
4103 break;
4104 case CK_FileNext:
4105 edit_load_forward_cmd (edit);
4106 break;
4108 case CK_SyntaxChoose:
4109 edit_syntax_dialog (edit);
4110 break;
4112 case CK_Search:
4113 edit_search_cmd (edit, FALSE);
4114 break;
4115 case CK_SearchContinue:
4116 edit_search_cmd (edit, TRUE);
4117 break;
4118 case CK_Replace:
4119 edit_replace_cmd (edit, 0);
4120 break;
4121 case CK_ReplaceContinue:
4122 edit_replace_cmd (edit, 1);
4123 break;
4124 case CK_Complete:
4125 /* if text marked shift block */
4126 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4127 edit_move_block_to_left (edit);
4128 else
4129 edit_complete_word_cmd (edit);
4130 break;
4131 case CK_Find:
4132 edit_get_match_keyword_cmd (edit);
4133 break;
4135 #ifdef HAVE_ASPELL
4136 case CK_SpellCheckCurrentWord:
4137 edit_suggest_current_word (edit);
4138 break;
4139 case CK_SpellCheck:
4140 edit_spellcheck_file (edit);
4141 break;
4142 case CK_SpellCheckSelectLang:
4143 edit_set_spell_lang ();
4144 break;
4145 #endif
4147 case CK_Date:
4149 char s[BUF_MEDIUM];
4150 /* fool gcc to prevent a Y2K warning */
4151 char time_format[] = "_c";
4152 time_format[0] = '%';
4154 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4155 edit_print_string (edit, s);
4156 edit->force |= REDRAW_PAGE;
4158 break;
4159 case CK_Goto:
4160 edit_goto_cmd (edit);
4161 break;
4162 case CK_ParagraphFormat:
4163 format_paragraph (edit, 1);
4164 edit->force |= REDRAW_PAGE;
4165 break;
4166 case CK_MacroDelete:
4167 edit_delete_macro_cmd (edit);
4168 break;
4169 case CK_MatchBracket:
4170 edit_goto_matching_bracket (edit);
4171 break;
4172 case CK_UserMenu:
4173 user_menu (edit, NULL, -1);
4174 break;
4175 case CK_Sort:
4176 edit_sort_cmd (edit);
4177 break;
4178 case CK_ExternalCommand:
4179 edit_ext_cmd (edit);
4180 break;
4181 case CK_Mail:
4182 edit_mail_dialog (edit);
4183 break;
4184 #ifdef HAVE_CHARSET
4185 case CK_SelectCodepage:
4186 edit_select_codepage_cmd (edit);
4187 break;
4188 #endif
4189 case CK_InsertLiteral:
4190 edit_insert_literal_cmd (edit);
4191 break;
4192 case CK_MacroStartStopRecord:
4193 edit_begin_end_macro_cmd (edit);
4194 break;
4195 case CK_RepeatStartStopRecord:
4196 edit_begin_end_repeat_cmd (edit);
4197 break;
4198 case CK_ExtendedKeyMap:
4199 edit->extmod = TRUE;
4200 break;
4201 default:
4202 break;
4205 /* CK_PipeBlock */
4206 if ((command / CK_PipeBlock (0)) == 1)
4207 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4209 /* keys which must set the col position, and the search vars */
4210 switch (command)
4212 case CK_Search:
4213 case CK_SearchContinue:
4214 case CK_Replace:
4215 case CK_ReplaceContinue:
4216 case CK_Complete:
4217 edit->prev_col = edit_get_col (edit);
4218 break;
4219 case CK_Up:
4220 case CK_MarkUp:
4221 case CK_MarkColumnUp:
4222 case CK_Down:
4223 case CK_MarkDown:
4224 case CK_MarkColumnDown:
4225 case CK_PageUp:
4226 case CK_MarkPageUp:
4227 case CK_MarkColumnPageUp:
4228 case CK_PageDown:
4229 case CK_MarkPageDown:
4230 case CK_MarkColumnPageDown:
4231 case CK_Top:
4232 case CK_MarkToFileBegin:
4233 case CK_Bottom:
4234 case CK_MarkToFileEnd:
4235 case CK_ParagraphUp:
4236 case CK_MarkParagraphUp:
4237 case CK_MarkColumnParagraphUp:
4238 case CK_ParagraphDown:
4239 case CK_MarkParagraphDown:
4240 case CK_MarkColumnParagraphDown:
4241 case CK_ScrollUp:
4242 case CK_MarkScrollUp:
4243 case CK_MarkColumnScrollUp:
4244 case CK_ScrollDown:
4245 case CK_MarkScrollDown:
4246 case CK_MarkColumnScrollDown:
4247 edit->search_start = edit->curs1;
4248 edit->found_len = 0;
4249 break;
4250 default:
4251 edit->found_len = 0;
4252 edit->prev_col = edit_get_col (edit);
4253 edit->search_start = edit->curs1;
4255 edit_find_bracket (edit);
4257 if (option_auto_para_formatting)
4259 switch (command)
4261 case CK_BackSpace:
4262 case CK_Delete:
4263 case CK_DeleteToWordBegin:
4264 case CK_DeleteToWordEnd:
4265 case CK_DeleteToHome:
4266 case CK_DeleteToEnd:
4267 format_paragraph (edit, 0);
4268 edit->force |= REDRAW_PAGE;
4273 /* --------------------------------------------------------------------------------------------- */
4275 void
4276 edit_stack_init (void)
4278 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4280 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4281 edit_history_moveto[edit_stack_iterator].line = -1;
4284 edit_stack_iterator = 0;
4287 /* --------------------------------------------------------------------------------------------- */
4289 void
4290 edit_stack_free (void)
4292 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4293 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4296 /* --------------------------------------------------------------------------------------------- */
4297 /** move i lines */
4299 void
4300 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4302 edit_move_updown (edit, i, do_scroll, TRUE);
4305 /* --------------------------------------------------------------------------------------------- */
4306 /** move i lines */
4308 void
4309 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4311 edit_move_updown (edit, i, do_scroll, FALSE);
4314 /* --------------------------------------------------------------------------------------------- */