code indentation
[midnight-commander.git] / src / editor / edit.c
blobbfda1b923d576528de8421efadc355684c1a0495
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* macro_index */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
73 #ifdef HAVE_ASPELL
74 #include "spell.h"
75 #endif
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
80 int option_typewriter_wrap = 0;
81 int option_auto_para_formatting = 0;
82 int option_fill_tabs_with_spaces = 0;
83 int option_return_does_auto_indent = 1;
84 int option_backspace_through_tabs = 0;
85 int option_fake_half_tabs = 1;
86 int option_save_mode = EDIT_QUICK_SAVE;
87 int option_save_position = 1;
88 int option_max_undo = 32768;
89 int option_persistent_selections = 1;
90 int option_cursor_beyond_eol = 0;
91 int option_line_state = 0;
92 int option_line_state_width = 0;
94 int option_edit_right_extreme = 0;
95 int option_edit_left_extreme = 0;
96 int option_edit_top_extreme = 0;
97 int option_edit_bottom_extreme = 0;
98 int enable_show_tabs_tws = 1;
99 int option_check_nl_at_eof = 0;
100 int option_group_undo = 0;
101 int show_right_margin = 0;
103 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
104 char *option_backup_ext = NULL;
106 unsigned int edit_stack_iterator = 0;
107 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
108 /* magic sequense for say than block is vertical */
109 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
111 /*** file scope macro definitions ****************************************************************/
113 #define TEMP_BUF_LEN 1024
115 #define space_width 1
117 /*** file scope type declarations ****************************************************************/
119 /*** file scope variables ************************************************************************/
121 /* detecting an error on save is easy: just check if every byte has been written. */
122 /* detecting an error on read, is not so easy 'cos there is not way to tell
123 whether you read everything or not. */
124 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
125 static const struct edit_filters
127 const char *read, *write, *extension;
128 } all_filters[] =
130 /* *INDENT-OFF* */
131 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
132 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
133 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
134 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
136 /* *INDENT-ON* */
139 static off_t last_bracket = -1;
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
146 * here's a quick sketch of the layout: (don't run this through indent.)
148 * (b1 is buffers1 and b2 is buffers2)
151 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
152 * ______________________________________|______________________________________
154 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
155 * |-> |-> |-> |-> |-> |-> |
157 * _<------------------------->|<----------------->_
158 * WEdit->curs2 | WEdit->curs1
159 * ^ | ^
160 * | ^|^ |
161 * cursor ||| cursor
162 * |||
163 * file end|||file beginning
168 * This_is_some_file
169 * fin.
172 /* --------------------------------------------------------------------------------------------- */
174 * Initialize the buffers for an empty files.
177 static void
178 edit_init_buffers (WEdit * edit)
180 int j;
182 for (j = 0; j <= MAXBUFF; j++)
184 edit->buffers1[j] = NULL;
185 edit->buffers2[j] = NULL;
188 edit->curs1 = 0;
189 edit->curs2 = 0;
190 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
193 /* --------------------------------------------------------------------------------------------- */
196 * Load file OR text into buffers. Set cursor to the beginning of file.
198 * @returns FALSE on error.
201 static gboolean
202 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
204 off_t buf, buf2;
205 int file = -1;
206 gboolean ret = FALSE;
208 edit->curs2 = edit->last_byte;
209 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
211 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
212 if (file == -1)
214 gchar *errmsg, *filename;
216 filename = vfs_path_to_str (filename_vpath);
217 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
218 g_free (filename);
219 edit_error_dialog (_("Error"), errmsg);
220 g_free (errmsg);
221 return FALSE;
224 if (!edit->buffers2[buf2])
225 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
229 if (mc_read (file,
230 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
231 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
232 break;
234 for (buf = buf2 - 1; buf >= 0; buf--)
236 /* edit->buffers2[0] is already allocated */
237 if (!edit->buffers2[buf])
238 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
239 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
240 break;
242 ret = TRUE;
244 while (FALSE);
246 if (!ret)
248 gchar *errmsg, *filename;
250 filename = vfs_path_to_str (filename_vpath);
251 errmsg = g_strdup_printf (_("Error reading %s"), filename);
252 g_free (filename);
253 edit_error_dialog (_("Error"), errmsg);
254 g_free (errmsg);
256 mc_close (file);
257 return ret;
260 /* --------------------------------------------------------------------------------------------- */
261 /** Return index of the filter or -1 is there is no appropriate filter */
263 static int
264 edit_find_filter (const vfs_path_t * filename_vpath)
266 size_t i, l, e;
267 char *filename;
269 if (filename_vpath == NULL)
270 return -1;
272 filename = vfs_path_to_str (filename_vpath);
273 l = strlen (filename);
274 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
276 e = strlen (all_filters[i].extension);
277 if (l > e)
278 if (!strcmp (all_filters[i].extension, filename + l - e))
280 g_free (filename);
281 return i;
284 g_free (filename);
285 return -1;
288 /* --------------------------------------------------------------------------------------------- */
290 static char *
291 edit_get_filter (const vfs_path_t * filename_vpath)
293 int i;
294 char *p, *quoted_name, *filename;
296 i = edit_find_filter (filename_vpath);
297 if (i < 0)
298 return NULL;
300 filename = vfs_path_to_str (filename_vpath);
301 quoted_name = name_quote (filename, 0);
302 g_free (filename);
303 p = g_strdup_printf (all_filters[i].read, quoted_name);
304 g_free (quoted_name);
305 return p;
308 /* --------------------------------------------------------------------------------------------- */
310 static off_t
311 edit_insert_stream (WEdit * edit, FILE * f)
313 int c;
314 off_t i = 0;
315 while ((c = fgetc (f)) >= 0)
317 edit_insert (edit, c);
318 i++;
320 return i;
323 /* --------------------------------------------------------------------------------------------- */
325 * Open file and create it if necessary.
327 * @param edit editor object
328 * @param filename_vpath file name
329 * @param st buffer for store stat info
330 * @returns TRUE for success, FALSE for error.
333 static gboolean
334 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
336 int file;
337 gchar *errmsg = NULL;
339 /* Try opening an existing file */
340 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
341 if (file < 0)
344 * Try creating the file. O_EXCL prevents following broken links
345 * and opening existing files.
347 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
348 if (file < 0)
350 char *filename;
352 filename = vfs_path_to_str (filename_vpath);
353 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
354 g_free (filename);
355 goto cleanup;
358 /* New file, delete it if it's not modified or saved */
359 edit->delete_file = 1;
362 /* Check what we have opened */
363 if (mc_fstat (file, st) < 0)
365 char *filename;
367 filename = vfs_path_to_str (filename_vpath);
368 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
369 g_free (filename);
370 goto cleanup;
373 /* We want to open regular files only */
374 if (!S_ISREG (st->st_mode))
376 char *filename;
378 filename = vfs_path_to_str (filename_vpath);
379 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
380 g_free (filename);
381 goto cleanup;
385 * Don't delete non-empty files.
386 * O_EXCL should prevent it, but let's be on the safe side.
388 if (st->st_size > 0)
389 edit->delete_file = 0;
391 if (st->st_size >= SIZE_LIMIT)
393 char *filename;
395 filename = vfs_path_to_str (filename_vpath);
396 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
397 g_free (filename);
400 cleanup:
401 (void) mc_close (file);
403 if (errmsg != NULL)
405 edit_error_dialog (_("Error"), errmsg);
406 g_free (errmsg);
407 return FALSE;
409 return TRUE;
412 /* --------------------------------------------------------------------------------------------- */
415 * Open the file and load it into the buffers, either directly or using
416 * a filter. Return TRUE on success, FALSE on error.
418 * Fast loading (edit_load_file_fast) is used when the file size is
419 * known. In this case the data is read into the buffers by blocks.
420 * If the file size is not known, the data is loaded byte by byte in
421 * edit_insert_file.
423 * @param edit editor object
424 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
426 static gboolean
427 edit_load_file (WEdit * edit)
429 gboolean fast_load = TRUE;
431 /* Cannot do fast load if a filter is used */
432 if (edit_find_filter (edit->filename_vpath) >= 0)
433 fast_load = FALSE;
436 * FIXME: line end translation should disable fast loading as well
437 * Consider doing fseek() to the end and ftell() for the real size.
439 if (edit->filename_vpath != NULL)
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (edit->filename_vpath))
446 fast_load = FALSE;
448 /* If we are dealing with a real file, check that it exists */
449 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
451 edit_clean (edit);
452 return FALSE;
455 else
457 /* nothing to load */
458 fast_load = FALSE;
461 edit_init_buffers (edit);
463 if (fast_load)
465 edit->last_byte = edit->stat1.st_size;
466 edit_load_file_fast (edit, edit->filename_vpath);
467 /* If fast load was used, the number of lines wasn't calculated */
468 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
470 else
472 edit->last_byte = 0;
473 if (edit->filename_vpath != NULL
474 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
476 edit->undo_stack_disable = 1;
477 if (edit_insert_file (edit, edit->filename_vpath) < 0)
479 edit_clean (edit);
480 return FALSE;
482 edit->undo_stack_disable = 0;
485 edit->lb = LB_ASIS;
486 return TRUE;
489 /* --------------------------------------------------------------------------------------------- */
490 /** Restore saved cursor position in the file */
492 static void
493 edit_load_position (WEdit * edit)
495 long line, column;
496 off_t offset;
498 if (edit->filename_vpath == NULL
499 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
500 return;
502 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
504 if (line > 0)
506 edit_move_to_line (edit, line - 1);
507 edit->prev_col = column;
509 else if (offset > 0)
511 edit_cursor_move (edit, offset);
512 line = edit->curs_line;
513 edit->search_start = edit->curs1;
516 book_mark_restore (edit, BOOK_MARK_COLOR);
518 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
519 edit_move_display (edit, line - (edit->widget.lines / 2));
522 /* --------------------------------------------------------------------------------------------- */
523 /** Save cursor position in the file */
525 static void
526 edit_save_position (WEdit * edit)
528 if (edit->filename_vpath == NULL
529 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
530 return;
532 book_mark_serialize (edit, BOOK_MARK_COLOR);
533 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
534 edit->serialized_bookmarks);
535 edit->serialized_bookmarks = NULL;
538 /* --------------------------------------------------------------------------------------------- */
539 /** Clean the WEdit stricture except the widget part */
541 static void
542 edit_purge_widget (WEdit * edit)
544 size_t len = sizeof (WEdit) - sizeof (Widget);
545 char *start = (char *) edit + sizeof (Widget);
546 memset (start, 0, len);
549 /* --------------------------------------------------------------------------------------------- */
552 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
553 then the file should be as it was when he loaded up. Then set edit->modified to 0.
556 static long
557 edit_pop_undo_action (WEdit * edit)
559 long c;
560 unsigned long sp = edit->undo_stack_pointer;
562 if (sp == edit->undo_stack_bottom)
563 return STACK_BOTTOM;
565 sp = (sp - 1) & edit->undo_stack_size_mask;
566 c = edit->undo_stack[sp];
567 if (c >= 0)
569 /* edit->undo_stack[sp] = '@'; */
570 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
571 return c;
574 if (sp == edit->undo_stack_bottom)
575 return STACK_BOTTOM;
577 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
578 if (edit->undo_stack[sp] == -2)
580 /* edit->undo_stack[sp] = '@'; */
581 edit->undo_stack_pointer = sp;
583 else
584 edit->undo_stack[sp]++;
586 return c;
589 static long
590 edit_pop_redo_action (WEdit * edit)
592 long c;
593 unsigned long sp = edit->redo_stack_pointer;
595 if (sp == edit->redo_stack_bottom)
596 return STACK_BOTTOM;
598 sp = (sp - 1) & edit->redo_stack_size_mask;
599 c = edit->redo_stack[sp];
600 if (c >= 0)
602 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
603 return c;
606 if (sp == edit->redo_stack_bottom)
607 return STACK_BOTTOM;
609 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
610 if (edit->redo_stack[sp] == -2)
611 edit->redo_stack_pointer = sp;
612 else
613 edit->redo_stack[sp]++;
615 return c;
618 static long
619 get_prev_undo_action (WEdit * edit)
621 long c;
622 unsigned long sp = edit->undo_stack_pointer;
624 if (sp == edit->undo_stack_bottom)
625 return STACK_BOTTOM;
627 sp = (sp - 1) & edit->undo_stack_size_mask;
628 c = edit->undo_stack[sp];
629 if (c >= 0)
630 return c;
632 if (sp == edit->undo_stack_bottom)
633 return STACK_BOTTOM;
635 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
636 return c;
639 /* --------------------------------------------------------------------------------------------- */
640 /** is called whenever a modification is made by one of the four routines below */
642 static void
643 edit_modification (WEdit * edit)
645 edit->caches_valid = FALSE;
647 /* raise lock when file modified */
648 if (!edit->modified && !edit->delete_file)
649 edit->locked = lock_file (edit->filename_vpath);
650 edit->modified = 1;
653 /* --------------------------------------------------------------------------------------------- */
655 #ifdef HAVE_CHARSET
656 static char *
657 edit_get_byte_ptr (WEdit * edit, off_t byte_index)
659 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
660 return NULL;
662 if (byte_index >= edit->curs1)
664 off_t p;
666 p = edit->curs1 + edit->curs2 - byte_index - 1;
667 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
668 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
671 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
672 (byte_index & M_EDIT_BUF_SIZE));
674 #endif
676 /* --------------------------------------------------------------------------------------------- */
678 #ifdef HAVE_CHARSET
679 static int
680 edit_get_prev_utf (WEdit * edit, off_t byte_index, int *char_width)
682 int i, res;
683 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
684 gchar *str;
685 gchar *cursor_buf_ptr;
687 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
689 *char_width = 0;
690 return 0;
693 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
694 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
695 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
697 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
698 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
700 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
702 *char_width = 1;
703 return *(cursor_buf_ptr - 1);
705 else
707 res = g_utf8_get_char_validated (str, -1);
709 if (res < 0)
711 *char_width = 1;
712 return *(cursor_buf_ptr - 1);
714 else
716 *char_width = cursor_buf_ptr - str;
717 return res;
721 #endif
723 /* --------------------------------------------------------------------------------------------- */
724 /* high level cursor movement commands */
725 /* --------------------------------------------------------------------------------------------- */
726 /** check whether cursor is in indent part of line
728 * @param edit editor object
730 * @return TRUE if cursor is in indent, FALSE otherwise
733 static gboolean
734 is_in_indent (WEdit * edit)
736 off_t p;
738 for (p = edit_bol (edit, edit->curs1); p < edit->curs1; p++)
739 if (strchr (" \t", edit_get_byte (edit, p)) == NULL)
740 return FALSE;
742 return TRUE;
745 /* --------------------------------------------------------------------------------------------- */
746 /** check whether line in editor is blank or not
748 * @param edit editor object
749 * @param offset position in file
751 * @return TRUE if line in blank, FALSE otherwise
754 static gboolean
755 is_blank (WEdit * edit, off_t offset)
757 off_t s, f;
758 int c;
760 s = edit_bol (edit, offset);
761 f = edit_eol (edit, offset) - 1;
762 while (s <= f)
764 c = edit_get_byte (edit, s++);
765 if (!isspace (c))
766 return FALSE;
768 return TRUE;
771 /* --------------------------------------------------------------------------------------------- */
772 /** returns the offset of line i */
774 static off_t
775 edit_find_line (WEdit * edit, long line)
777 long i, j = 0;
778 long m = 2000000000; /* what is the magic number? */
780 if (!edit->caches_valid)
782 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
783 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
784 /* three offsets that we *know* are line 0 at 0 and these two: */
785 edit->line_numbers[1] = edit->curs_line;
786 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
787 edit->line_numbers[2] = edit->total_lines;
788 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
789 edit->caches_valid = TRUE;
791 if (line >= edit->total_lines)
792 return edit->line_offsets[2];
793 if (line <= 0)
794 return 0;
795 /* find the closest known point */
796 for (i = 0; i < N_LINE_CACHES; i++)
798 long n;
800 n = abs (edit->line_numbers[i] - line);
801 if (n < m)
803 m = n;
804 j = i;
807 if (m == 0)
808 return edit->line_offsets[j]; /* know the offset exactly */
809 if (m == 1 && j >= 3)
810 i = j; /* one line different - caller might be looping, so stay in this cache */
811 else
812 i = 3 + (rand () % (N_LINE_CACHES - 3));
813 if (line > edit->line_numbers[j])
814 edit->line_offsets[i] =
815 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
816 else
817 edit->line_offsets[i] =
818 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
819 edit->line_numbers[i] = line;
820 return edit->line_offsets[i];
823 /* --------------------------------------------------------------------------------------------- */
824 /** moves up until a blank line is reached, or until just
825 before a non-blank line is reached */
827 static void
828 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
830 long i = 0;
832 if (edit->curs_line > 1)
834 if (!line_is_blank (edit, edit->curs_line))
836 for (i = edit->curs_line - 1; i != 0; i--)
837 if (line_is_blank (edit, i))
838 break;
840 else if (line_is_blank (edit, edit->curs_line - 1))
842 for (i = edit->curs_line - 1; i != 0; i--)
843 if (!line_is_blank (edit, i))
845 i++;
846 break;
849 else
851 for (i = edit->curs_line - 1; i != 0; i--)
852 if (line_is_blank (edit, i))
853 break;
857 edit_move_up (edit, edit->curs_line - i, do_scroll);
860 /* --------------------------------------------------------------------------------------------- */
861 /** moves down until a blank line is reached, or until just
862 before a non-blank line is reached */
864 static void
865 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
867 long i;
869 if (edit->curs_line >= edit->total_lines - 1)
870 i = edit->total_lines;
871 else if (!line_is_blank (edit, edit->curs_line))
873 for (i = edit->curs_line + 1; i != 0; i++)
874 if (line_is_blank (edit, i) || i >= edit->total_lines)
875 break;
877 else if (line_is_blank (edit, edit->curs_line + 1))
879 for (i = edit->curs_line + 1; i != 0; i++)
880 if (!line_is_blank (edit, i) || i > edit->total_lines)
882 i--;
883 break;
886 else
888 for (i = edit->curs_line + 1; i != 0; i++)
889 if (line_is_blank (edit, i) || i >= edit->total_lines)
890 break;
892 edit_move_down (edit, i - edit->curs_line, do_scroll);
895 /* --------------------------------------------------------------------------------------------- */
897 static void
898 edit_begin_page (WEdit * edit)
900 edit_update_curs_row (edit);
901 edit_move_up (edit, edit->curs_row, 0);
904 /* --------------------------------------------------------------------------------------------- */
906 static void
907 edit_end_page (WEdit * edit)
909 edit_update_curs_row (edit);
910 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
914 /* --------------------------------------------------------------------------------------------- */
915 /** goto beginning of text */
917 static void
918 edit_move_to_top (WEdit * edit)
920 if (edit->curs_line)
922 edit_cursor_move (edit, -edit->curs1);
923 edit_move_to_prev_col (edit, 0);
924 edit->force |= REDRAW_PAGE;
925 edit->search_start = 0;
926 edit_update_curs_row (edit);
931 /* --------------------------------------------------------------------------------------------- */
932 /** goto end of text */
934 static void
935 edit_move_to_bottom (WEdit * edit)
937 if (edit->curs_line < edit->total_lines)
939 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
940 edit->start_display = edit->last_byte;
941 edit->start_line = edit->total_lines;
942 edit_scroll_upward (edit, edit->widget.lines - 1);
943 edit->force |= REDRAW_PAGE;
947 /* --------------------------------------------------------------------------------------------- */
948 /** goto beginning of line */
950 static void
951 edit_cursor_to_bol (WEdit * edit)
953 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
954 edit->search_start = edit->curs1;
955 edit->prev_col = edit_get_col (edit);
956 edit->over_col = 0;
959 /* --------------------------------------------------------------------------------------------- */
960 /** goto end of line */
962 static void
963 edit_cursor_to_eol (WEdit * edit)
965 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
966 edit->search_start = edit->curs1;
967 edit->prev_col = edit_get_col (edit);
968 edit->over_col = 0;
971 /* --------------------------------------------------------------------------------------------- */
973 static unsigned long
974 my_type_of (int c)
976 int x, r = 0;
977 const char *p, *q;
978 const char option_chars_move_whole_word[] =
979 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
981 if (!c)
982 return 0;
983 if (c == '!')
985 if (*option_chars_move_whole_word == '!')
986 return 2;
987 return 0x80000000UL;
989 if (g_ascii_isupper ((gchar) c))
990 c = 'A';
991 else if (g_ascii_islower ((gchar) c))
992 c = 'a';
993 else if (g_ascii_isalpha (c))
994 c = 'a';
995 else if (isdigit (c))
996 c = '0';
997 else if (isspace (c))
998 c = ' ';
999 q = strchr (option_chars_move_whole_word, c);
1000 if (!q)
1001 return 0xFFFFFFFFUL;
1004 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1005 if (*p == '!')
1006 x <<= 1;
1007 r |= x;
1009 while ((q = strchr (q + 1, c)));
1010 return r;
1013 /* --------------------------------------------------------------------------------------------- */
1015 static void
1016 edit_left_word_move (WEdit * edit, int s)
1018 for (;;)
1020 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)
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)))
1033 break;
1034 if (isspace (c1) && !isspace (c2))
1035 break;
1036 if (s)
1037 if (!isspace (c1) && isspace (c2))
1038 break;
1042 /* --------------------------------------------------------------------------------------------- */
1044 static void
1045 edit_left_word_move_cmd (WEdit * edit)
1047 edit_left_word_move (edit, 0);
1048 edit->force |= REDRAW_PAGE;
1051 /* --------------------------------------------------------------------------------------------- */
1053 static void
1054 edit_right_word_move (WEdit * edit, int s)
1056 for (;;)
1058 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)))
1071 break;
1072 if (isspace (c1) && !isspace (c2))
1073 break;
1074 if (s)
1075 if (!isspace (c1) && isspace (c2))
1076 break;
1080 /* --------------------------------------------------------------------------------------------- */
1082 static void
1083 edit_right_word_move_cmd (WEdit * edit)
1085 edit_right_word_move (edit, 0);
1086 edit->force |= REDRAW_PAGE;
1089 /* --------------------------------------------------------------------------------------------- */
1091 static void
1092 edit_right_char_move_cmd (WEdit * edit)
1094 int cw = 1;
1095 int c = 0;
1096 #ifdef HAVE_CHARSET
1097 if (edit->utf8)
1099 c = edit_get_utf (edit, edit->curs1, &cw);
1100 if (cw < 1)
1101 cw = 1;
1103 else
1104 #endif
1106 c = edit_get_byte (edit, edit->curs1);
1108 if (option_cursor_beyond_eol && c == '\n')
1110 edit->over_col++;
1112 else
1114 edit_cursor_move (edit, cw);
1118 /* --------------------------------------------------------------------------------------------- */
1120 static void
1121 edit_left_char_move_cmd (WEdit * edit)
1123 int cw = 1;
1124 if (edit->column_highlight
1125 && option_cursor_beyond_eol
1126 && edit->mark1 != edit->mark2
1127 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1128 return;
1129 #ifdef HAVE_CHARSET
1130 if (edit->utf8)
1132 edit_get_prev_utf (edit, edit->curs1, &cw);
1133 if (cw < 1)
1134 cw = 1;
1136 #endif
1137 if (option_cursor_beyond_eol && edit->over_col > 0)
1139 edit->over_col--;
1141 else
1143 edit_cursor_move (edit, -cw);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Up or down cursor moving.
1149 direction = TRUE - move up
1150 = FALSE - move down
1153 static void
1154 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1156 long p;
1157 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1159 if (lines > l)
1160 lines = l;
1162 if (lines == 0)
1163 return;
1165 if (lines > 1)
1166 edit->force |= REDRAW_PAGE;
1167 if (do_scroll)
1169 if (direction)
1170 edit_scroll_upward (edit, lines);
1171 else
1172 edit_scroll_downward (edit, lines);
1174 p = edit_bol (edit, edit->curs1);
1176 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1178 edit_cursor_move (edit, p - edit->curs1);
1180 edit_move_to_prev_col (edit, p);
1182 /* search start of current multibyte char (like CJK) */
1183 if (edit->curs1 + 1 < edit->last_byte)
1185 edit_right_char_move_cmd (edit);
1186 edit_left_char_move_cmd (edit);
1189 edit->search_start = edit->curs1;
1190 edit->found_len = 0;
1193 /* --------------------------------------------------------------------------------------------- */
1195 static void
1196 edit_right_delete_word (WEdit * edit)
1198 int c1, c2;
1199 for (;;)
1201 if (edit->curs1 >= edit->last_byte)
1202 break;
1203 c1 = edit_delete (edit, 1);
1204 c2 = edit_get_byte (edit, edit->curs1);
1205 if (c1 == '\n' || c2 == '\n')
1206 break;
1207 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1208 break;
1209 if (!(my_type_of (c1) & my_type_of (c2)))
1210 break;
1214 /* --------------------------------------------------------------------------------------------- */
1216 static void
1217 edit_left_delete_word (WEdit * edit)
1219 int c1, c2;
1220 for (;;)
1222 if (edit->curs1 <= 0)
1223 break;
1224 c1 = edit_backspace (edit, 1);
1225 c2 = edit_get_byte (edit, edit->curs1 - 1);
1226 if (c1 == '\n' || c2 == '\n')
1227 break;
1228 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1229 break;
1230 if (!(my_type_of (c1) & my_type_of (c2)))
1231 break;
1235 /* --------------------------------------------------------------------------------------------- */
1237 the start column position is not recorded, and hence does not
1238 undo as it happed. But who would notice.
1241 static void
1242 edit_do_undo (WEdit * edit)
1244 long ac;
1245 long count = 0;
1247 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1248 edit->over_col = 0;
1249 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1251 switch ((int) ac)
1253 case STACK_BOTTOM:
1254 goto done_undo;
1255 case CURS_RIGHT:
1256 edit_cursor_move (edit, 1);
1257 break;
1258 case CURS_LEFT:
1259 edit_cursor_move (edit, -1);
1260 break;
1261 case BACKSPACE:
1262 case BACKSPACE_BR:
1263 edit_backspace (edit, 1);
1264 break;
1265 case DELCHAR:
1266 case DELCHAR_BR:
1267 edit_delete (edit, 1);
1268 break;
1269 case COLUMN_ON:
1270 edit->column_highlight = 1;
1271 break;
1272 case COLUMN_OFF:
1273 edit->column_highlight = 0;
1274 break;
1276 if (ac >= 256 && ac < 512)
1277 edit_insert_ahead (edit, ac - 256);
1278 if (ac >= 0 && ac < 256)
1279 edit_insert (edit, ac);
1281 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1283 edit->mark1 = ac - MARK_1;
1284 edit->column1 =
1285 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1287 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1289 edit->mark2 = ac - MARK_2;
1290 edit->column2 =
1291 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1293 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1295 edit->end_mark_curs = ac - MARK_CURS;
1297 if (count++)
1298 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1301 if (edit->start_display > ac - KEY_PRESS)
1303 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1304 edit->force |= REDRAW_PAGE;
1306 else if (edit->start_display < ac - KEY_PRESS)
1308 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1309 edit->force |= REDRAW_PAGE;
1311 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1312 edit_update_curs_row (edit);
1314 done_undo:
1315 edit->undo_stack_disable = 0;
1318 /* --------------------------------------------------------------------------------------------- */
1320 static void
1321 edit_do_redo (WEdit * edit)
1323 long ac;
1324 long count = 0;
1326 if (edit->redo_stack_reset)
1327 return;
1329 edit->over_col = 0;
1330 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1332 switch ((int) ac)
1334 case STACK_BOTTOM:
1335 goto done_redo;
1336 case CURS_RIGHT:
1337 edit_cursor_move (edit, 1);
1338 break;
1339 case CURS_LEFT:
1340 edit_cursor_move (edit, -1);
1341 break;
1342 case BACKSPACE:
1343 edit_backspace (edit, 1);
1344 break;
1345 case DELCHAR:
1346 edit_delete (edit, 1);
1347 break;
1348 case COLUMN_ON:
1349 edit->column_highlight = 1;
1350 break;
1351 case COLUMN_OFF:
1352 edit->column_highlight = 0;
1353 break;
1355 if (ac >= 256 && ac < 512)
1356 edit_insert_ahead (edit, ac - 256);
1357 if (ac >= 0 && ac < 256)
1358 edit_insert (edit, ac);
1360 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1362 edit->mark1 = ac - MARK_1;
1363 edit->column1 =
1364 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1366 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1368 edit->mark2 = ac - MARK_2;
1369 edit->column2 =
1370 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1372 /* more than one pop usually means something big */
1373 if (count++)
1374 edit->force |= REDRAW_PAGE;
1377 if (edit->start_display > ac - KEY_PRESS)
1379 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1380 edit->force |= REDRAW_PAGE;
1382 else if (edit->start_display < ac - KEY_PRESS)
1384 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1385 edit->force |= REDRAW_PAGE;
1387 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1388 edit_update_curs_row (edit);
1390 done_redo:
1394 /* --------------------------------------------------------------------------------------------- */
1396 static void
1397 edit_group_undo (WEdit * edit)
1399 long ac = KEY_PRESS;
1400 long cur_ac = KEY_PRESS;
1401 while (ac != STACK_BOTTOM && ac == cur_ac)
1403 cur_ac = get_prev_undo_action (edit);
1404 edit_do_undo (edit);
1405 ac = get_prev_undo_action (edit);
1406 /* exit from cycle if option_group_undo is not set,
1407 * and make single UNDO operation
1409 if (!option_group_undo)
1410 ac = STACK_BOTTOM;
1414 /* --------------------------------------------------------------------------------------------- */
1416 static void
1417 edit_delete_to_line_end (WEdit * edit)
1419 while (edit_get_byte (edit, edit->curs1) != '\n' && edit->curs2 != 0)
1420 edit_delete (edit, 1);
1423 /* --------------------------------------------------------------------------------------------- */
1425 static void
1426 edit_delete_to_line_begin (WEdit * edit)
1428 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 != 0)
1429 edit_backspace (edit, 1);
1432 /* --------------------------------------------------------------------------------------------- */
1434 static gboolean
1435 is_aligned_on_a_tab (WEdit * edit)
1437 long curs_col;
1439 edit_update_curs_col (edit);
1440 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1441 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1444 /* --------------------------------------------------------------------------------------------- */
1446 static gboolean
1447 right_of_four_spaces (WEdit * edit)
1449 int i, ch = 0;
1451 for (i = 1; i <= HALF_TAB_SIZE; i++)
1452 ch |= edit_get_byte (edit, edit->curs1 - i);
1454 return (ch == ' ' && is_aligned_on_a_tab (edit));
1457 /* --------------------------------------------------------------------------------------------- */
1459 static gboolean
1460 left_of_four_spaces (WEdit * edit)
1462 int i, ch = 0;
1464 for (i = 0; i < HALF_TAB_SIZE; i++)
1465 ch |= edit_get_byte (edit, edit->curs1 + i);
1467 return (ch == ' ' && is_aligned_on_a_tab (edit));
1470 /* --------------------------------------------------------------------------------------------- */
1472 static void
1473 edit_auto_indent (WEdit * edit)
1475 off_t p;
1476 char c;
1477 p = edit->curs1;
1478 /* use the previous line as a template */
1479 p = edit_move_backward (edit, p, 1);
1480 /* copy the leading whitespace of the line */
1481 for (;;)
1482 { /* no range check - the line _is_ \n-terminated */
1483 c = edit_get_byte (edit, p++);
1484 if (c != ' ' && c != '\t')
1485 break;
1486 edit_insert (edit, c);
1490 /* --------------------------------------------------------------------------------------------- */
1492 static inline void
1493 edit_double_newline (WEdit * edit)
1495 edit_insert (edit, '\n');
1496 if (edit_get_byte (edit, edit->curs1) == '\n' || edit_get_byte (edit, edit->curs1 - 2) == '\n')
1497 return;
1498 edit->force |= REDRAW_PAGE;
1499 edit_insert (edit, '\n');
1502 /* --------------------------------------------------------------------------------------------- */
1504 static void
1505 insert_spaces_tab (WEdit * edit, gboolean half)
1507 long i;
1509 edit_update_curs_col (edit);
1510 i = option_tab_spacing * space_width;
1511 if (half)
1512 i /= 2;
1513 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1514 while (i > 0)
1516 edit_insert (edit, ' ');
1517 i -= space_width;
1521 /* --------------------------------------------------------------------------------------------- */
1523 static inline void
1524 edit_tab_cmd (WEdit * edit)
1526 if (option_fake_half_tabs && is_in_indent (edit))
1528 /* insert a half tab (usually four spaces) unless there is a
1529 half tab already behind, then delete it and insert a
1530 full tab. */
1531 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1532 insert_spaces_tab (edit, TRUE);
1533 else
1535 int i;
1537 for (i = 1; i <= HALF_TAB_SIZE; i++)
1538 edit_backspace (edit, 1);
1539 edit_insert (edit, '\t');
1542 else if (option_fill_tabs_with_spaces)
1543 insert_spaces_tab (edit, FALSE);
1544 else
1545 edit_insert (edit, '\t');
1548 /* --------------------------------------------------------------------------------------------- */
1550 static void
1551 check_and_wrap_line (WEdit * edit)
1553 off_t curs;
1554 int c;
1556 if (!option_typewriter_wrap)
1557 return;
1558 edit_update_curs_col (edit);
1559 if (edit->curs_col < option_word_wrap_line_length)
1560 return;
1561 curs = edit->curs1;
1562 for (;;)
1564 curs--;
1565 c = edit_get_byte (edit, curs);
1566 if (c == '\n' || curs <= 0)
1568 edit_insert (edit, '\n');
1569 return;
1571 if (c == ' ' || c == '\t')
1573 off_t current = edit->curs1;
1574 edit_cursor_move (edit, curs - edit->curs1 + 1);
1575 edit_insert (edit, '\n');
1576 edit_cursor_move (edit, current - edit->curs1 + 1);
1577 return;
1582 /* --------------------------------------------------------------------------------------------- */
1583 /** this find the matching bracket in either direction, and sets edit->bracket
1585 * @param edit editor object
1586 * @param in_screen seach only on the current screen
1587 * @param furthest_bracket_search count of the bytes for search
1589 * @return position of the found bracket (-1 if no match)
1592 static off_t
1593 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1595 const char *const b = "{}{[][()(", *p;
1596 int i = 1, a, inc = -1, c, d, n = 0;
1597 unsigned long j = 0;
1598 off_t q;
1599 edit_update_curs_row (edit);
1600 c = edit_get_byte (edit, edit->curs1);
1601 p = strchr (b, c);
1602 /* no limit */
1603 if (!furthest_bracket_search)
1604 furthest_bracket_search--;
1605 /* not on a bracket at all */
1606 if (p == NULL)
1607 return -1;
1608 /* the matching bracket */
1609 d = p[1];
1610 /* going left or right? */
1611 if (strchr ("{[(", c))
1612 inc = 1;
1613 for (q = edit->curs1 + inc;; q += inc)
1615 /* out of buffer? */
1616 if (q >= edit->last_byte || q < 0)
1617 break;
1618 a = edit_get_byte (edit, q);
1619 /* don't want to eat CPU */
1620 if (j++ > furthest_bracket_search)
1621 break;
1622 /* out of screen? */
1623 if (in_screen)
1625 if (q < edit->start_display)
1626 break;
1627 /* count lines if searching downward */
1628 if (inc > 0 && a == '\n')
1629 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1630 break;
1632 /* count bracket depth */
1633 i += (a == c) - (a == d);
1634 /* return if bracket depth is zero */
1635 if (i == 0)
1636 return q;
1638 /* no match */
1639 return -1;
1642 /* --------------------------------------------------------------------------------------------- */
1644 static inline void
1645 edit_goto_matching_bracket (WEdit * edit)
1647 off_t q;
1649 q = edit_get_bracket (edit, 0, 0);
1650 if (q >= 0)
1652 edit->bracket = edit->curs1;
1653 edit->force |= REDRAW_PAGE;
1654 edit_cursor_move (edit, q - edit->curs1);
1658 /* --------------------------------------------------------------------------------------------- */
1660 static void
1661 edit_move_block_to_right (WEdit * edit)
1663 off_t start_mark, end_mark;
1664 long cur_bol, start_bol;
1666 if (eval_marks (edit, &start_mark, &end_mark))
1667 return;
1669 start_bol = edit_bol (edit, start_mark);
1670 cur_bol = edit_bol (edit, end_mark - 1);
1674 edit_cursor_move (edit, cur_bol - edit->curs1);
1675 if (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);
1681 if (cur_bol == 0)
1682 break;
1684 cur_bol = edit_bol (edit, cur_bol - 1);
1686 while (cur_bol >= start_bol);
1688 edit->force |= REDRAW_PAGE;
1691 /* --------------------------------------------------------------------------------------------- */
1693 static void
1694 edit_move_block_to_left (WEdit * edit)
1696 off_t start_mark, end_mark;
1697 off_t cur_bol, start_bol;
1698 int i;
1700 if (eval_marks (edit, &start_mark, &end_mark))
1701 return;
1703 start_bol = edit_bol (edit, start_mark);
1704 cur_bol = edit_bol (edit, end_mark - 1);
1708 int del_tab_width;
1709 int next_char;
1711 edit_cursor_move (edit, cur_bol - edit->curs1);
1713 if (option_fake_half_tabs)
1714 del_tab_width = HALF_TAB_SIZE;
1715 else
1716 del_tab_width = option_tab_spacing;
1718 next_char = edit_get_byte (edit, edit->curs1);
1719 if (next_char == '\t')
1720 edit_delete (edit, 1);
1721 else if (next_char == ' ')
1722 for (i = 1; i <= del_tab_width; i++)
1724 if (next_char == ' ')
1725 edit_delete (edit, 1);
1726 next_char = edit_get_byte (edit, edit->curs1);
1729 if (cur_bol == 0)
1730 break;
1732 cur_bol = edit_bol (edit, cur_bol - 1);
1734 while (cur_bol >= start_bol);
1736 edit->force |= REDRAW_PAGE;
1739 /* --------------------------------------------------------------------------------------------- */
1741 * prints at the cursor
1742 * @returns the number of chars printed
1745 static size_t
1746 edit_print_string (WEdit * e, const char *s)
1748 size_t i = 0;
1750 while (s[i] != '\0')
1751 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1752 e->force |= REDRAW_COMPLETELY;
1753 edit_update_screen (e);
1754 return i;
1757 /* --------------------------------------------------------------------------------------------- */
1758 /*** public functions ****************************************************************************/
1759 /* --------------------------------------------------------------------------------------------- */
1761 /** User edit menu, like user menu (F2) but only in editor. */
1763 void
1764 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1766 char *block_file;
1767 int nomark;
1768 off_t curs;
1769 off_t start_mark, end_mark;
1770 struct stat status;
1771 vfs_path_t *block_file_vpath;
1773 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1774 block_file_vpath = vfs_path_from_str (block_file);
1775 curs = edit->curs1;
1776 nomark = eval_marks (edit, &start_mark, &end_mark);
1777 if (nomark == 0)
1778 edit_save_block (edit, block_file, start_mark, end_mark);
1780 /* run shell scripts from menu */
1781 if (user_menu_cmd (edit, menu_file, selected_entry)
1782 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1784 int rc = 0;
1785 FILE *fd;
1787 /* i.e. we have marked block */
1788 if (nomark == 0)
1789 rc = edit_block_delete_cmd (edit);
1791 if (rc == 0)
1793 off_t ins_len;
1795 ins_len = edit_insert_file (edit, block_file_vpath);
1796 if (nomark == 0 && ins_len > 0)
1797 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1799 /* truncate block file */
1800 fd = fopen (block_file, "w");
1801 if (fd != NULL)
1802 fclose (fd);
1804 g_free (block_file);
1805 vfs_path_free (block_file_vpath);
1807 edit_cursor_move (edit, curs - edit->curs1);
1808 edit->force |= REDRAW_PAGE;
1809 send_message ((Widget *) edit, WIDGET_DRAW, 0);
1812 /* --------------------------------------------------------------------------------------------- */
1815 edit_get_byte (WEdit * edit, off_t byte_index)
1817 off_t p;
1819 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1820 return '\n';
1822 if (byte_index >= edit->curs1)
1824 p = edit->curs1 + edit->curs2 - byte_index - 1;
1825 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1828 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1831 /* --------------------------------------------------------------------------------------------- */
1833 #ifdef HAVE_CHARSET
1835 edit_get_utf (WEdit * edit, off_t byte_index, int *char_width)
1837 gchar *str = NULL;
1838 int res = -1;
1839 gunichar ch;
1840 gchar *next_ch = NULL;
1841 int width = 0;
1842 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1844 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1846 *char_width = 0;
1847 return '\n';
1850 str = edit_get_byte_ptr (edit, byte_index);
1852 if (str == NULL)
1854 *char_width = 0;
1855 return 0;
1858 res = g_utf8_get_char_validated (str, -1);
1860 if (res < 0)
1862 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1863 int i;
1864 for (i = 0; i < UTF8_CHAR_LEN; i++)
1865 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1866 utf8_buf[UTF8_CHAR_LEN] = '\0';
1867 str = utf8_buf;
1868 res = g_utf8_get_char_validated (str, -1);
1871 if (res < 0)
1873 ch = *str;
1874 width = 0;
1876 else
1878 ch = res;
1879 /* Calculate UTF-8 char width */
1880 next_ch = g_utf8_next_char (str);
1881 if (next_ch)
1883 width = next_ch - str;
1885 else
1887 ch = 0;
1888 width = 0;
1891 *char_width = width;
1892 return ch;
1894 #endif
1896 /* --------------------------------------------------------------------------------------------- */
1898 char *
1899 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1901 int i;
1902 char *p, *writename;
1903 const vfs_path_element_t *path_element;
1905 i = edit_find_filter (filename_vpath);
1906 if (i < 0)
1907 return NULL;
1909 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1910 writename = name_quote (path_element->path, 0);
1911 p = g_strdup_printf (all_filters[i].write, writename);
1912 g_free (writename);
1913 return p;
1916 /* --------------------------------------------------------------------------------------------- */
1918 * @param edit editor object
1919 * @param f value of stream file
1920 * @returns the length of the file
1923 off_t
1924 edit_write_stream (WEdit * edit, FILE * f)
1926 long i;
1928 if (edit->lb == LB_ASIS)
1930 for (i = 0; i < edit->last_byte; i++)
1931 if (fputc (edit_get_byte (edit, i), f) < 0)
1932 break;
1933 return i;
1936 /* change line breaks */
1937 for (i = 0; i < edit->last_byte; i++)
1939 unsigned char c = edit_get_byte (edit, i);
1941 if (!(c == '\n' || c == '\r'))
1943 /* not line break */
1944 if (fputc (c, f) < 0)
1945 return i;
1947 else
1948 { /* (c == '\n' || c == '\r') */
1949 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1951 switch (edit->lb)
1953 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1954 /* put one line break unconditionally */
1955 if (fputc ('\n', f) < 0)
1956 return i;
1958 i++; /* 2 chars are processed */
1960 if (c == '\r' && c1 == '\n')
1961 /* Windows line break; go to the next char */
1962 break;
1964 if (c == '\r' && c1 == '\r')
1966 /* two Macintosh line breaks; put second line break */
1967 if (fputc ('\n', f) < 0)
1968 return i;
1969 break;
1972 if (fputc (c1, f) < 0)
1973 return i;
1974 break;
1976 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1977 /* put one line break unconditionally */
1978 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1979 return i;
1981 if (c == '\r' && c1 == '\n')
1982 /* Windows line break; go to the next char */
1983 i++;
1984 break;
1986 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1987 /* put one line break unconditionally */
1988 if (fputc ('\r', f) < 0)
1989 return i;
1991 i++; /* 2 chars are processed */
1993 if (c == '\r' && c1 == '\n')
1994 /* Windows line break; go to the next char */
1995 break;
1997 if (c == '\n' && c1 == '\n')
1999 /* two Windows line breaks; put second line break */
2000 if (fputc ('\r', f) < 0)
2001 return i;
2002 break;
2005 if (fputc (c1, f) < 0)
2006 return i;
2007 break;
2008 case LB_ASIS: /* default without changes */
2009 break;
2014 return edit->last_byte;
2017 /* --------------------------------------------------------------------------------------------- */
2019 gboolean
2020 is_break_char (char c)
2022 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2025 /* --------------------------------------------------------------------------------------------- */
2027 char *
2028 edit_get_word_from_pos (WEdit * edit, off_t start_pos, off_t * start, gsize * len, gsize * cut)
2030 off_t word_start;
2031 long cut_len = 0;
2032 GString *match_expr;
2033 unsigned char *bufpos;
2034 int c1, c2;
2036 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2038 c1 = edit_get_byte (edit, word_start);
2039 c2 = edit_get_byte (edit, word_start - 1);
2041 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2042 break;
2045 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2046 match_expr = g_string_sized_new (16);
2050 c1 = edit_get_byte (edit, word_start + match_expr->len);
2051 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2052 g_string_append_c (match_expr, c1);
2054 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2056 *len = match_expr->len;
2057 *start = word_start;
2058 *cut = cut_len;
2060 return g_string_free (match_expr, FALSE);
2063 /* --------------------------------------------------------------------------------------------- */
2064 /** inserts a file at the cursor, returns count of inserted bytes on success */
2066 long
2067 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2069 char *p;
2070 off_t ins_len = 0;
2072 p = edit_get_filter (filename_vpath);
2073 if (p != NULL)
2075 FILE *f;
2076 off_t current = edit->curs1;
2078 f = (FILE *) popen (p, "r");
2079 if (f != NULL)
2081 edit_insert_stream (edit, f);
2082 ins_len = edit->curs1 - current;
2083 edit_cursor_move (edit, -ins_len);
2084 if (pclose (f) > 0)
2086 char *errmsg;
2088 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2089 edit_error_dialog (_("Error"), errmsg);
2090 g_free (errmsg);
2091 ins_len = -1;
2094 else
2096 char *errmsg;
2098 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2099 edit_error_dialog (_("Error"), errmsg);
2100 g_free (errmsg);
2101 ins_len = -1;
2103 g_free (p);
2105 else
2107 int file;
2108 off_t blocklen;
2109 off_t current = edit->curs1;
2110 int vertical_insertion = 0;
2111 char *buf;
2113 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2114 if (file == -1)
2115 return -1;
2117 buf = g_malloc0 (TEMP_BUF_LEN);
2118 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2119 if (blocklen > 0)
2121 /* if contain signature VERTICAL_MAGIC then it vertical block */
2122 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2123 vertical_insertion = 1;
2124 else
2125 mc_lseek (file, 0, SEEK_SET);
2128 if (vertical_insertion)
2130 off_t mark1, mark2;
2131 long c1, c2;
2133 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2134 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2135 /* highlight inserted text then not persistent blocks */
2136 if (!option_persistent_selections)
2138 if (!edit->column_highlight)
2139 edit_push_undo_action (edit, COLUMN_OFF);
2140 edit->column_highlight = 1;
2143 else
2145 off_t i;
2147 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2149 for (i = 0; i < blocklen; i++)
2150 edit_insert (edit, buf[i]);
2152 /* highlight inserted text then not persistent blocks */
2153 if (!option_persistent_selections && edit->modified)
2155 edit_set_markers (edit, edit->curs1, current, 0, 0);
2156 if (edit->column_highlight)
2157 edit_push_undo_action (edit, COLUMN_ON);
2158 edit->column_highlight = 0;
2162 edit->force |= REDRAW_PAGE;
2163 ins_len = edit->curs1 - current;
2164 edit_cursor_move (edit, -ins_len);
2165 g_free (buf);
2166 mc_close (file);
2167 if (blocklen != 0)
2168 ins_len = 0;
2171 return ins_len;
2174 /* --------------------------------------------------------------------------------------------- */
2176 * Fill in the edit structure. Return NULL on failure. Pass edit as
2177 * NULL to allocate a new structure.
2179 * If line is 0, try to restore saved position. Otherwise put the
2180 * cursor on that line and show it in the middle of the screen.
2183 WEdit *
2184 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2185 long line)
2187 gboolean to_free = FALSE;
2189 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2190 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2192 if (edit != NULL)
2193 edit_purge_widget (edit);
2194 else
2196 #ifdef ENABLE_NLS
2198 * Expand option_whole_chars_search by national letters using
2199 * current locale
2202 static char option_whole_chars_search_buf[256];
2204 if (option_whole_chars_search_buf != option_whole_chars_search)
2206 size_t i;
2207 size_t len = str_term_width1 (option_whole_chars_search);
2209 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2211 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2213 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2215 option_whole_chars_search_buf[len++] = i;
2219 option_whole_chars_search_buf[len] = 0;
2220 option_whole_chars_search = option_whole_chars_search_buf;
2222 #endif /* ENABLE_NLS */
2223 edit = g_malloc0 (sizeof (WEdit));
2224 to_free = TRUE;
2227 edit->drag_state = MCEDIT_DRAG_NORMAL;
2228 edit->widget.y = y;
2229 edit->widget.x = x;
2230 edit->widget.lines = lines;
2231 edit->widget.cols = cols;
2232 edit_save_size (edit);
2233 edit->fullscreen = TRUE;
2235 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2236 edit->stat1.st_uid = getuid ();
2237 edit->stat1.st_gid = getgid ();
2238 edit->stat1.st_mtime = 0;
2240 edit->over_col = 0;
2241 edit->bracket = -1;
2242 edit->force |= REDRAW_PAGE;
2244 /* set file name before load file */
2245 edit_set_filename (edit, filename_vpath);
2247 edit->undo_stack_size = START_STACK_SIZE;
2248 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2249 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2251 edit->redo_stack_size = START_STACK_SIZE;
2252 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2253 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2255 #ifdef HAVE_CHARSET
2256 edit->utf8 = FALSE;
2257 edit->converter = str_cnv_from_term;
2258 edit_set_codeset (edit);
2259 #endif
2261 if (!edit_load_file (edit))
2263 /* edit_load_file already gives an error message */
2264 if (to_free)
2265 g_free (edit);
2266 return NULL;
2269 edit->loading_done = 1;
2270 edit->modified = 0;
2271 edit->locked = 0;
2272 edit_load_syntax (edit, NULL, NULL);
2274 int color;
2275 edit_get_syntax_color (edit, -1, &color);
2278 /* load saved cursor position */
2279 if ((line == 0) && option_save_position)
2280 edit_load_position (edit);
2281 else
2283 if (line <= 0)
2284 line = 1;
2285 edit_move_display (edit, line - 1);
2286 edit_move_to_line (edit, line - 1);
2289 edit_load_macro_cmd (edit);
2291 return edit;
2294 /* --------------------------------------------------------------------------------------------- */
2296 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2297 gboolean
2298 edit_clean (WEdit * edit)
2300 int j = 0;
2302 if (edit == NULL)
2303 return FALSE;
2305 /* a stale lock, remove it */
2306 if (edit->locked)
2307 edit->locked = unlock_file (edit->filename_vpath);
2309 /* save cursor position */
2310 if (option_save_position)
2311 edit_save_position (edit);
2312 else if (edit->serialized_bookmarks != NULL)
2313 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2315 /* File specified on the mcedit command line and never saved */
2316 if (edit->delete_file)
2317 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2319 edit_free_syntax_rules (edit);
2320 book_mark_flush (edit, -1);
2321 for (; j <= MAXBUFF; j++)
2323 g_free (edit->buffers1[j]);
2324 g_free (edit->buffers2[j]);
2327 g_free (edit->undo_stack);
2328 g_free (edit->redo_stack);
2329 vfs_path_free (edit->filename_vpath);
2330 vfs_path_free (edit->dir_vpath);
2331 mc_search_free (edit->search);
2332 edit->search = NULL;
2334 #ifdef HAVE_CHARSET
2335 if (edit->converter != str_cnv_from_term)
2336 str_close_conv (edit->converter);
2337 #endif
2339 edit_purge_widget (edit);
2341 return TRUE;
2344 /* --------------------------------------------------------------------------------------------- */
2347 * Load a new file into the editor and set line. If it fails, preserve the old file.
2348 * To do it, allocate a new widget, initialize it and, if the new file
2349 * was loaded, copy the data to the old widget.
2351 * @returns TRUE on success, FALSE on failure.
2353 gboolean
2354 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2356 WEdit *e;
2357 int y = edit->widget.y;
2358 int x = edit->widget.x;
2359 int lines = edit->widget.lines;
2360 int columns = edit->widget.cols;
2362 e = g_malloc0 (sizeof (WEdit));
2363 e->widget = edit->widget;
2365 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2367 g_free (e);
2368 return FALSE;
2371 edit_clean (edit);
2372 memcpy (edit, e, sizeof (WEdit));
2373 g_free (e);
2375 return TRUE;
2378 /* --------------------------------------------------------------------------------------------- */
2380 #ifdef HAVE_CHARSET
2381 void
2382 edit_set_codeset (WEdit * edit)
2384 const char *cp_id;
2386 cp_id =
2387 get_codepage_id (mc_global.source_codepage >=
2388 0 ? mc_global.source_codepage : mc_global.display_codepage);
2390 if (cp_id != NULL)
2392 GIConv conv;
2393 conv = str_crt_conv_from (cp_id);
2394 if (conv != INVALID_CONV)
2396 if (edit->converter != str_cnv_from_term)
2397 str_close_conv (edit->converter);
2398 edit->converter = conv;
2402 if (cp_id != NULL)
2403 edit->utf8 = str_isutf8 (cp_id);
2405 #endif
2408 /* --------------------------------------------------------------------------------------------- */
2411 * Recording stack for undo:
2412 * The following is an implementation of a compressed stack. Identical
2413 * pushes are recorded by a negative prefix indicating the number of times the
2414 * same char was pushed. This saves space for repeated curs-left or curs-right
2415 * delete etc.
2417 * eg:
2419 * pushed: stored:
2422 * b a
2423 * b -3
2424 * b b
2425 * c --> -4
2426 * c c
2427 * c d
2431 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2432 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2433 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2434 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2435 * position.
2437 * The only way the cursor moves or the buffer is changed is through the routines:
2438 * insert, backspace, insert_ahead, delete, and cursor_move.
2439 * These record the reverse undo movements onto the stack each time they are
2440 * called.
2442 * Each key press results in a set of actions (insert; delete ...). So each time
2443 * a key is pressed the current position of start_display is pushed as
2444 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2445 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2446 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2450 * @param edit editor object
2451 * @param c code of the action
2454 void
2455 edit_push_undo_action (WEdit * edit, long c)
2457 unsigned long sp = edit->undo_stack_pointer;
2458 unsigned long spm1;
2459 long *t;
2461 /* first enlarge the stack if necessary */
2462 if (sp > edit->undo_stack_size - 10)
2463 { /* say */
2464 if (option_max_undo < 256)
2465 option_max_undo = 256;
2466 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2468 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2469 if (t)
2471 edit->undo_stack = t;
2472 edit->undo_stack_size <<= 1;
2473 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2477 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2478 if (edit->undo_stack_disable)
2480 edit_push_redo_action (edit, KEY_PRESS);
2481 edit_push_redo_action (edit, c);
2482 return;
2485 if (edit->redo_stack_reset)
2486 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2488 if (edit->undo_stack_bottom != sp
2489 && spm1 != edit->undo_stack_bottom
2490 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2492 int d;
2493 if (edit->undo_stack[spm1] < 0)
2495 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2496 if (d == c && edit->undo_stack[spm1] > -1000000000)
2498 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2499 edit->undo_stack[spm1]--;
2500 return;
2503 else
2505 d = edit->undo_stack[spm1];
2506 if (d == c)
2508 if (c >= KEY_PRESS)
2509 return; /* --> no need to push multiple do-nothings */
2510 edit->undo_stack[sp] = -2;
2511 goto check_bottom;
2515 edit->undo_stack[sp] = c;
2517 check_bottom:
2518 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2520 /* if the sp wraps round and catches the undo_stack_bottom then erase
2521 * the first set of actions on the stack to make space - by moving
2522 * undo_stack_bottom forward one "key press" */
2523 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2524 if ((unsigned long) c == edit->undo_stack_bottom ||
2525 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2528 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2530 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2531 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2533 /*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: */
2534 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2535 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2537 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2541 void
2542 edit_push_redo_action (WEdit * edit, long c)
2544 unsigned long sp = edit->redo_stack_pointer;
2545 unsigned long spm1;
2546 long *t;
2547 /* first enlarge the stack if necessary */
2548 if (sp > edit->redo_stack_size - 10)
2549 { /* say */
2550 if (option_max_undo < 256)
2551 option_max_undo = 256;
2552 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2554 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2555 if (t)
2557 edit->redo_stack = t;
2558 edit->redo_stack_size <<= 1;
2559 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2563 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2565 if (edit->redo_stack_bottom != sp
2566 && spm1 != edit->redo_stack_bottom
2567 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2569 int d;
2570 if (edit->redo_stack[spm1] < 0)
2572 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2573 if (d == c && edit->redo_stack[spm1] > -1000000000)
2575 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2576 edit->redo_stack[spm1]--;
2577 return;
2580 else
2582 d = edit->redo_stack[spm1];
2583 if (d == c)
2585 if (c >= KEY_PRESS)
2586 return; /* --> no need to push multiple do-nothings */
2587 edit->redo_stack[sp] = -2;
2588 goto redo_check_bottom;
2592 edit->redo_stack[sp] = c;
2594 redo_check_bottom:
2595 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2597 /* if the sp wraps round and catches the redo_stack_bottom then erase
2598 * the first set of actions on the stack to make space - by moving
2599 * redo_stack_bottom forward one "key press" */
2600 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2601 if ((unsigned long) c == edit->redo_stack_bottom ||
2602 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2605 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2607 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2608 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2611 * If a single key produced enough pushes to wrap all the way round then
2612 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2613 * The stack is then initialised:
2616 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2617 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2618 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2621 /* --------------------------------------------------------------------------------------------- */
2623 Basic low level single character buffer alterations and movements at the cursor.
2624 Returns char passed over, inserted or removed.
2627 void
2628 edit_insert (WEdit * edit, int c)
2630 /* check if file has grown to large */
2631 if (edit->last_byte >= SIZE_LIMIT)
2632 return;
2634 /* first we must update the position of the display window */
2635 if (edit->curs1 < edit->start_display)
2637 edit->start_display++;
2638 if (c == '\n')
2639 edit->start_line++;
2642 /* Mark file as modified, unless the file hasn't been fully loaded */
2643 if (edit->loading_done)
2644 edit_modification (edit);
2646 /* now we must update some info on the file and check if a redraw is required */
2647 if (c == '\n')
2649 book_mark_inc (edit, edit->curs_line);
2650 edit->curs_line++;
2651 edit->total_lines++;
2652 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2655 /* save the reverse command onto the undo stack */
2656 /* ordinary char and not space */
2657 if (c > 32)
2658 edit_push_undo_action (edit, BACKSPACE);
2659 else
2660 edit_push_undo_action (edit, BACKSPACE_BR);
2661 /* update markers */
2662 edit->mark1 += (edit->mark1 > edit->curs1);
2663 edit->mark2 += (edit->mark2 > edit->curs1);
2664 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2666 /* add a new buffer if we've reached the end of the last one */
2667 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2668 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2670 /* perform the insertion */
2671 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2672 = (unsigned char) c;
2674 /* update file length */
2675 edit->last_byte++;
2677 /* update cursor position */
2678 edit->curs1++;
2681 /* --------------------------------------------------------------------------------------------- */
2682 /** same as edit_insert and move left */
2684 void
2685 edit_insert_ahead (WEdit * edit, int c)
2687 if (edit->last_byte >= SIZE_LIMIT)
2688 return;
2690 if (edit->curs1 < edit->start_display)
2692 edit->start_display++;
2693 if (c == '\n')
2694 edit->start_line++;
2696 edit_modification (edit);
2697 if (c == '\n')
2699 book_mark_inc (edit, edit->curs_line);
2700 edit->total_lines++;
2701 edit->force |= REDRAW_AFTER_CURSOR;
2703 /* ordinary char and not space */
2704 if (c > 32)
2705 edit_push_undo_action (edit, DELCHAR);
2706 else
2707 edit_push_undo_action (edit, DELCHAR_BR);
2709 edit->mark1 += (edit->mark1 >= edit->curs1);
2710 edit->mark2 += (edit->mark2 >= edit->curs1);
2711 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2713 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2714 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2715 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2716 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2718 edit->last_byte++;
2719 edit->curs2++;
2723 /* --------------------------------------------------------------------------------------------- */
2726 edit_delete (WEdit * edit, const int byte_delete)
2728 int p = 0;
2729 int cw = 1;
2730 int i;
2732 if (edit->curs2 == 0)
2733 return 0;
2735 #ifdef HAVE_CHARSET
2736 /* if byte_delete = 1 then delete only one byte not multibyte char */
2737 if (edit->utf8 && byte_delete == 0)
2739 edit_get_utf (edit, edit->curs1, &cw);
2740 if (cw < 1)
2741 cw = 1;
2743 #else
2744 (void) byte_delete;
2745 #endif
2747 if (edit->mark2 != edit->mark1)
2748 edit_push_markers (edit);
2750 for (i = 1; i <= cw; i++)
2752 if (edit->mark1 > edit->curs1)
2754 edit->mark1--;
2755 edit->end_mark_curs--;
2757 if (edit->mark2 > edit->curs1)
2758 edit->mark2--;
2759 if (edit->last_get_rule > edit->curs1)
2760 edit->last_get_rule--;
2762 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2763 ((edit->curs2 -
2764 1) & M_EDIT_BUF_SIZE) - 1];
2766 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2768 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2769 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2771 edit->last_byte--;
2772 edit->curs2--;
2773 edit_push_undo_action (edit, p + 256);
2776 edit_modification (edit);
2777 if (p == '\n')
2779 book_mark_dec (edit, edit->curs_line);
2780 edit->total_lines--;
2781 edit->force |= REDRAW_AFTER_CURSOR;
2783 if (edit->curs1 < edit->start_display)
2785 edit->start_display--;
2786 if (p == '\n')
2787 edit->start_line--;
2790 return p;
2793 /* --------------------------------------------------------------------------------------------- */
2796 edit_backspace (WEdit * edit, const int byte_delete)
2798 int p = 0;
2799 int cw = 1;
2800 int i;
2802 if (edit->curs1 == 0)
2803 return 0;
2805 if (edit->mark2 != edit->mark1)
2806 edit_push_markers (edit);
2808 #ifdef HAVE_CHARSET
2809 if (edit->utf8 && byte_delete == 0)
2811 edit_get_prev_utf (edit, edit->curs1, &cw);
2812 if (cw < 1)
2813 cw = 1;
2815 #else
2816 (void) byte_delete;
2817 #endif
2819 for (i = 1; i <= cw; i++)
2821 if (edit->mark1 >= edit->curs1)
2823 edit->mark1--;
2824 edit->end_mark_curs--;
2826 if (edit->mark2 >= edit->curs1)
2827 edit->mark2--;
2828 if (edit->last_get_rule >= edit->curs1)
2829 edit->last_get_rule--;
2831 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2832 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2833 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2835 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2836 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2838 edit->last_byte--;
2839 edit->curs1--;
2840 edit_push_undo_action (edit, p);
2842 edit_modification (edit);
2843 if (p == '\n')
2845 book_mark_dec (edit, edit->curs_line);
2846 edit->curs_line--;
2847 edit->total_lines--;
2848 edit->force |= REDRAW_AFTER_CURSOR;
2851 if (edit->curs1 < edit->start_display)
2853 edit->start_display--;
2854 if (p == '\n')
2855 edit->start_line--;
2858 return p;
2861 /* --------------------------------------------------------------------------------------------- */
2862 /** moves the cursor right or left: increment positive or negative respectively */
2864 void
2865 edit_cursor_move (WEdit * edit, off_t increment)
2867 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2868 int c;
2870 if (increment < 0)
2872 for (; increment < 0; increment++)
2874 if (edit->curs1 == 0)
2875 return;
2877 edit_push_undo_action (edit, CURS_RIGHT);
2879 c = edit_get_byte (edit, edit->curs1 - 1);
2880 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2881 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2882 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2883 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2884 edit->curs2++;
2885 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2886 1) & M_EDIT_BUF_SIZE];
2887 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2889 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2890 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2892 edit->curs1--;
2893 if (c == '\n')
2895 edit->curs_line--;
2896 edit->force |= REDRAW_LINE_BELOW;
2901 else
2903 for (; increment > 0; increment--)
2905 if (edit->curs2 == 0)
2906 return;
2908 edit_push_undo_action (edit, CURS_LEFT);
2910 c = edit_get_byte (edit, edit->curs1);
2911 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2912 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2913 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2914 edit->curs1++;
2915 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2916 ((edit->curs2 -
2917 1) & M_EDIT_BUF_SIZE) - 1];
2918 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2920 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2921 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2923 edit->curs2--;
2924 if (c == '\n')
2926 edit->curs_line++;
2927 edit->force |= REDRAW_LINE_ABOVE;
2933 /* These functions return positions relative to lines */
2935 /* --------------------------------------------------------------------------------------------- */
2936 /** returns index of last char on line + 1 */
2938 off_t
2939 edit_eol (WEdit * edit, off_t current)
2941 if (current >= edit->last_byte)
2942 return edit->last_byte;
2944 for (; edit_get_byte (edit, current) != '\n'; current++)
2947 return current;
2950 /* --------------------------------------------------------------------------------------------- */
2951 /** returns index of first char on line */
2953 off_t
2954 edit_bol (WEdit * edit, off_t current)
2956 if (current <= 0)
2957 return 0;
2959 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
2962 return current;
2965 /* --------------------------------------------------------------------------------------------- */
2967 long
2968 edit_count_lines (WEdit * edit, off_t current, off_t upto)
2970 long lines = 0;
2971 if (upto > edit->last_byte)
2972 upto = edit->last_byte;
2973 if (current < 0)
2974 current = 0;
2975 while (current < upto)
2976 if (edit_get_byte (edit, current++) == '\n')
2977 lines++;
2978 return lines;
2981 /* --------------------------------------------------------------------------------------------- */
2982 /* If lines is zero this returns the count of lines from current to upto. */
2983 /* If upto is zero returns index of lines forward current. */
2985 off_t
2986 edit_move_forward (WEdit * edit, off_t current, long lines, off_t upto)
2988 if (upto != 0)
2990 return (off_t) edit_count_lines (edit, current, upto);
2992 else
2994 long next;
2995 if (lines < 0)
2996 lines = 0;
2997 while (lines-- != 0)
2999 next = edit_eol (edit, current) + 1;
3000 if (next > edit->last_byte)
3001 break;
3002 else
3003 current = next;
3005 return current;
3009 /* --------------------------------------------------------------------------------------------- */
3010 /** Returns offset of 'lines' lines up from current */
3012 off_t
3013 edit_move_backward (WEdit * edit, off_t current, long lines)
3015 if (lines < 0)
3016 lines = 0;
3017 current = edit_bol (edit, current);
3018 while (lines-- != 0 && current != 0)
3019 current = edit_bol (edit, current - 1);
3020 return current;
3023 /* --------------------------------------------------------------------------------------------- */
3024 /* If cols is zero this returns the count of columns from current to upto. */
3025 /* If upto is zero returns index of cols across from current. */
3027 off_t
3028 edit_move_forward3 (WEdit * edit, off_t current, long cols, off_t upto)
3030 off_t p, q;
3031 long col;
3033 if (upto != 0)
3035 q = upto;
3036 cols = -10;
3038 else
3039 q = edit->last_byte + 2;
3041 for (col = 0, p = current; p < q; p++)
3043 int c, orig_c;
3045 if (cols != -10)
3047 if (col == cols)
3048 return p;
3049 if (col > cols)
3050 return p - 1;
3053 orig_c = c = edit_get_byte (edit, p);
3055 #ifdef HAVE_CHARSET
3056 if (edit->utf8)
3058 int utf_ch;
3059 int cw = 1;
3061 utf_ch = edit_get_utf (edit, p, &cw);
3062 if (mc_global.utf8_display)
3064 if (cw > 1)
3065 col -= cw - 1;
3066 if (g_unichar_iswide (utf_ch))
3067 col++;
3069 else if (cw > 1 && g_unichar_isprint (utf_ch))
3070 col -= cw - 1;
3073 c = convert_to_display_c (c);
3074 #endif
3076 if (c == '\t')
3077 col += TAB_SIZE - col % TAB_SIZE;
3078 else if (c == '\n')
3079 return (upto != 0 ? (off_t) col : p);
3081 if ((c < 32 || c == 127) && (orig_c == c
3082 #ifdef HAVE_CHARSET
3083 || (!mc_global.utf8_display && !edit->utf8)
3084 #endif
3086 /* '\r' is shown as ^M, so we must advance 2 characters */
3087 /* Caret notation for control characters */
3088 col += 2;
3089 else
3090 col++;
3092 return (off_t) col;
3095 /* --------------------------------------------------------------------------------------------- */
3096 /** returns the current column position of the cursor */
3098 long
3099 edit_get_col (WEdit * edit)
3101 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3104 /* --------------------------------------------------------------------------------------------- */
3105 /* Scrolling functions */
3106 /* --------------------------------------------------------------------------------------------- */
3108 void
3109 edit_update_curs_row (WEdit * edit)
3111 edit->curs_row = edit->curs_line - edit->start_line;
3114 /* --------------------------------------------------------------------------------------------- */
3116 void
3117 edit_update_curs_col (WEdit * edit)
3119 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3122 /* --------------------------------------------------------------------------------------------- */
3124 long
3125 edit_get_curs_col (const WEdit * edit)
3127 return edit->curs_col;
3130 /* --------------------------------------------------------------------------------------------- */
3131 /** moves the display start position up by i lines */
3133 void
3134 edit_scroll_upward (WEdit * edit, long i)
3136 long lines_above = edit->start_line;
3138 if (i > lines_above)
3139 i = lines_above;
3140 if (i != 0)
3142 edit->start_line -= i;
3143 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3144 edit->force |= REDRAW_PAGE;
3145 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3147 edit_update_curs_row (edit);
3151 /* --------------------------------------------------------------------------------------------- */
3153 void
3154 edit_scroll_downward (WEdit * edit, long i)
3156 long lines_below;
3158 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3159 if (lines_below > 0)
3161 if (i > lines_below)
3162 i = lines_below;
3163 edit->start_line += i;
3164 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3165 edit->force |= REDRAW_PAGE;
3166 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3168 edit_update_curs_row (edit);
3171 /* --------------------------------------------------------------------------------------------- */
3173 void
3174 edit_scroll_right (WEdit * edit, long i)
3176 edit->force |= REDRAW_PAGE;
3177 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3178 edit->start_col -= i;
3181 /* --------------------------------------------------------------------------------------------- */
3183 void
3184 edit_scroll_left (WEdit * edit, long i)
3186 if (edit->start_col)
3188 edit->start_col += i;
3189 if (edit->start_col > 0)
3190 edit->start_col = 0;
3191 edit->force |= REDRAW_PAGE;
3192 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3196 /* --------------------------------------------------------------------------------------------- */
3197 /* high level cursor movement commands */
3198 /* --------------------------------------------------------------------------------------------- */
3200 void
3201 edit_move_to_prev_col (WEdit * edit, off_t p)
3203 long prev = edit->prev_col;
3204 long over = edit->over_col;
3206 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3208 if (option_cursor_beyond_eol)
3210 long line_len;
3212 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3213 edit_eol (edit, edit->curs1));
3214 if (line_len < prev + edit->over_col)
3216 edit->over_col = prev + over - line_len;
3217 edit->prev_col = line_len;
3218 edit->curs_col = line_len;
3220 else
3222 edit->curs_col = prev + over;
3223 edit->prev_col = edit->curs_col;
3224 edit->over_col = 0;
3227 else
3229 edit->over_col = 0;
3230 if (is_in_indent (edit) && option_fake_half_tabs)
3232 edit_update_curs_col (edit);
3233 if (space_width != 0 && edit->curs_col % (HALF_TAB_SIZE * space_width) != 0)
3235 int q;
3237 q = edit->curs_col;
3238 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3239 p = edit_bol (edit, edit->curs1);
3240 edit_cursor_move (edit,
3241 edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
3242 if (!left_of_four_spaces (edit))
3243 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3249 /* --------------------------------------------------------------------------------------------- */
3250 /** check whether line in editor is blank or not
3252 * @param edit editor object
3253 * @param line number of line
3255 * @return TRUE if line in blank, FALSE otherwise
3258 gboolean
3259 line_is_blank (WEdit * edit, long line)
3261 return is_blank (edit, edit_find_line (edit, line));
3264 /* --------------------------------------------------------------------------------------------- */
3265 /** move cursor to line 'line' */
3267 void
3268 edit_move_to_line (WEdit * e, long line)
3270 if (line < e->curs_line)
3271 edit_move_up (e, e->curs_line - line, 0);
3272 else
3273 edit_move_down (e, line - e->curs_line, 0);
3274 edit_scroll_screen_over_cursor (e);
3277 /* --------------------------------------------------------------------------------------------- */
3278 /** scroll window so that first visible line is 'line' */
3280 void
3281 edit_move_display (WEdit * e, long line)
3283 if (line < e->start_line)
3284 edit_scroll_upward (e, e->start_line - line);
3285 else
3286 edit_scroll_downward (e, line - e->start_line);
3289 /* --------------------------------------------------------------------------------------------- */
3290 /** save markers onto undo stack */
3292 void
3293 edit_push_markers (WEdit * edit)
3295 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3296 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3297 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3300 /* --------------------------------------------------------------------------------------------- */
3302 void
3303 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3305 edit->mark1 = m1;
3306 edit->mark2 = m2;
3307 edit->column1 = c1;
3308 edit->column2 = c2;
3312 /* --------------------------------------------------------------------------------------------- */
3313 /** highlight marker toggle */
3315 void
3316 edit_mark_cmd (WEdit * edit, gboolean unmark)
3318 edit_push_markers (edit);
3319 if (unmark)
3321 edit_set_markers (edit, 0, 0, 0, 0);
3322 edit->force |= REDRAW_PAGE;
3324 else if (edit->mark2 >= 0)
3326 edit->end_mark_curs = -1;
3327 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3328 edit->curs_col + edit->over_col);
3329 edit->force |= REDRAW_PAGE;
3331 else
3333 edit->end_mark_curs = edit->curs1;
3334 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3335 edit->curs_col + edit->over_col);
3339 /* --------------------------------------------------------------------------------------------- */
3340 /** highlight the word under cursor */
3342 void
3343 edit_mark_current_word_cmd (WEdit * edit)
3345 long pos;
3347 for (pos = edit->curs1; pos != 0; pos--)
3349 int c1, c2;
3351 c1 = edit_get_byte (edit, pos);
3352 c2 = edit_get_byte (edit, pos - 1);
3353 if (!isspace (c1) && isspace (c2))
3354 break;
3355 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3356 break;
3358 edit->mark1 = pos;
3360 for (; pos < edit->last_byte; pos++)
3362 int c1, c2;
3364 c1 = edit_get_byte (edit, pos);
3365 c2 = edit_get_byte (edit, pos + 1);
3366 if (!isspace (c1) && isspace (c2))
3367 break;
3368 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3369 break;
3371 edit->mark2 = min (pos + 1, edit->last_byte);
3373 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3376 /* --------------------------------------------------------------------------------------------- */
3378 void
3379 edit_mark_current_line_cmd (WEdit * edit)
3381 long pos = edit->curs1;
3383 edit->mark1 = edit_bol (edit, pos);
3384 edit->mark2 = edit_eol (edit, pos);
3386 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3389 /* --------------------------------------------------------------------------------------------- */
3391 void
3392 edit_delete_line (WEdit * edit)
3395 * Delete right part of the line.
3396 * Note that edit_get_byte() returns '\n' when byte position is
3397 * beyond EOF.
3399 while (edit_get_byte (edit, edit->curs1) != '\n')
3400 (void) edit_delete (edit, 1);
3403 * Delete '\n' char.
3404 * Note that edit_delete() will not corrupt anything if called while
3405 * cursor position is EOF.
3407 (void) edit_delete (edit, 1);
3410 * Delete left part of the line.
3411 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3413 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3414 (void) edit_backspace (edit, 1);
3417 /* --------------------------------------------------------------------------------------------- */
3419 long
3420 edit_indent_width (WEdit * edit, off_t p)
3422 off_t q = p;
3424 /* move to the end of the leading whitespace of the line */
3425 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3426 q++;
3427 /* count the number of columns of indentation */
3428 return (long) edit_move_forward3 (edit, p, 0, q);
3431 /* --------------------------------------------------------------------------------------------- */
3433 void
3434 edit_insert_indent (WEdit * edit, int indent)
3436 if (!option_fill_tabs_with_spaces)
3438 while (indent >= TAB_SIZE)
3440 edit_insert (edit, '\t');
3441 indent -= TAB_SIZE;
3444 while (indent-- > 0)
3445 edit_insert (edit, ' ');
3448 /* --------------------------------------------------------------------------------------------- */
3450 void
3451 edit_push_key_press (WEdit * edit)
3453 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3454 if (edit->mark2 == -1)
3456 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3457 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3461 /* --------------------------------------------------------------------------------------------- */
3463 void
3464 edit_find_bracket (WEdit * edit)
3466 edit->bracket = edit_get_bracket (edit, 1, 10000);
3467 if (last_bracket != edit->bracket)
3468 edit->force |= REDRAW_PAGE;
3469 last_bracket = edit->bracket;
3472 /* --------------------------------------------------------------------------------------------- */
3474 * This executes a command as though the user initiated it through a key
3475 * press. Callback with WIDGET_KEY as a message calls this after
3476 * translating the key press. This function can be used to pass any
3477 * command to the editor. Note that the screen wouldn't update
3478 * automatically. Either of command or char_for_insertion must be
3479 * passed as -1. Commands are executed, and char_for_insertion is
3480 * inserted at the cursor.
3483 void
3484 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3486 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3487 || (macro_index < 0
3488 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3490 macro_index = 0;
3491 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3492 return;
3494 if (macro_index != -1)
3496 edit->force |= REDRAW_COMPLETELY;
3497 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3499 edit_store_macro_cmd (edit);
3500 macro_index = -1;
3501 return;
3503 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3505 edit_repeat_macro_cmd (edit);
3506 macro_index = -1;
3507 return;
3511 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3513 record_macro_buf[macro_index].action = command;
3514 record_macro_buf[macro_index++].ch = char_for_insertion;
3516 /* record the beginning of a set of editing actions initiated by a key press */
3517 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3518 edit_push_key_press (edit);
3520 edit_execute_cmd (edit, command, char_for_insertion);
3521 if (edit->column_highlight)
3522 edit->force |= REDRAW_PAGE;
3525 /* --------------------------------------------------------------------------------------------- */
3527 This executes a command at a lower level than macro recording.
3528 It also does not push a key_press onto the undo stack. This means
3529 that if it is called many times, a single undo command will undo
3530 all of them. It also does not check for the Undo command.
3532 void
3533 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3535 if (command == CK_WindowFullscreen)
3537 edit_toggle_fullscreen (edit);
3538 return;
3541 /* handle window state */
3542 if (edit_handle_move_resize (edit, command))
3543 return;
3545 edit->force |= REDRAW_LINE;
3547 /* The next key press will unhighlight the found string, so update
3548 * the whole page */
3549 if (edit->found_len || edit->column_highlight)
3550 edit->force |= REDRAW_PAGE;
3552 switch (command)
3554 /* a mark command with shift-arrow */
3555 case CK_MarkLeft:
3556 case CK_MarkRight:
3557 case CK_MarkToWordBegin:
3558 case CK_MarkToWordEnd:
3559 case CK_MarkToHome:
3560 case CK_MarkToEnd:
3561 case CK_MarkUp:
3562 case CK_MarkDown:
3563 case CK_MarkPageUp:
3564 case CK_MarkPageDown:
3565 case CK_MarkToFileBegin:
3566 case CK_MarkToFileEnd:
3567 case CK_MarkToPageBegin:
3568 case CK_MarkToPageEnd:
3569 case CK_MarkScrollUp:
3570 case CK_MarkScrollDown:
3571 case CK_MarkParagraphUp:
3572 case CK_MarkParagraphDown:
3573 /* a mark command with alt-arrow */
3574 case CK_MarkColumnPageUp:
3575 case CK_MarkColumnPageDown:
3576 case CK_MarkColumnLeft:
3577 case CK_MarkColumnRight:
3578 case CK_MarkColumnUp:
3579 case CK_MarkColumnDown:
3580 case CK_MarkColumnScrollUp:
3581 case CK_MarkColumnScrollDown:
3582 case CK_MarkColumnParagraphUp:
3583 case CK_MarkColumnParagraphDown:
3584 edit->column_highlight = 0;
3585 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3587 edit_mark_cmd (edit, TRUE); /* clear */
3588 edit_mark_cmd (edit, FALSE); /* marking on */
3590 edit->highlight = 1;
3591 break;
3593 /* any other command */
3594 default:
3595 if (edit->highlight)
3596 edit_mark_cmd (edit, FALSE); /* clear */
3597 edit->highlight = 0;
3600 /* first check for undo */
3601 if (command == CK_Undo)
3603 edit->redo_stack_reset = 0;
3604 edit_group_undo (edit);
3605 edit->found_len = 0;
3606 edit->prev_col = edit_get_col (edit);
3607 edit->search_start = edit->curs1;
3608 return;
3610 /* check for redo */
3611 if (command == CK_Redo)
3613 edit->redo_stack_reset = 0;
3614 edit_do_redo (edit);
3615 edit->found_len = 0;
3616 edit->prev_col = edit_get_col (edit);
3617 edit->search_start = edit->curs1;
3618 return;
3621 edit->redo_stack_reset = 1;
3623 /* An ordinary key press */
3624 if (char_for_insertion >= 0)
3626 /* if non persistent selection and text selected */
3627 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3628 edit_block_delete_cmd (edit);
3630 if (edit->overwrite)
3632 /* remove char only one time, after input first byte, multibyte chars */
3633 #ifdef HAVE_CHARSET
3634 if (!mc_global.utf8_display || edit->charpoint == 0)
3635 #endif
3636 if (edit_get_byte (edit, edit->curs1) != '\n')
3638 edit_delete (edit, 0);
3640 if (option_cursor_beyond_eol && edit->over_col > 0)
3641 edit_insert_over (edit);
3642 #ifdef HAVE_CHARSET
3643 if (char_for_insertion > 255 && !mc_global.utf8_display)
3645 unsigned char str[6 + 1];
3646 size_t i = 0;
3647 int res;
3649 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3650 if (res == 0)
3652 str[0] = '.';
3653 str[1] = '\0';
3655 else
3657 str[res] = '\0';
3659 while (str[i] != 0 && i <= 6)
3661 char_for_insertion = str[i];
3662 edit_insert (edit, char_for_insertion);
3663 i++;
3666 else
3667 #endif
3668 edit_insert (edit, char_for_insertion);
3670 if (option_auto_para_formatting)
3672 format_paragraph (edit, 0);
3673 edit->force |= REDRAW_PAGE;
3675 else
3676 check_and_wrap_line (edit);
3677 edit->found_len = 0;
3678 edit->prev_col = edit_get_col (edit);
3679 edit->search_start = edit->curs1;
3680 edit_find_bracket (edit);
3681 return;
3684 switch (command)
3686 case CK_TopOnScreen:
3687 case CK_BottomOnScreen:
3688 case CK_Top:
3689 case CK_Bottom:
3690 case CK_PageUp:
3691 case CK_PageDown:
3692 case CK_Home:
3693 case CK_End:
3694 case CK_Up:
3695 case CK_Down:
3696 case CK_Left:
3697 case CK_Right:
3698 case CK_WordLeft:
3699 case CK_WordRight:
3700 if (!option_persistent_selections && edit->mark2 >= 0)
3702 if (edit->column_highlight)
3703 edit_push_undo_action (edit, COLUMN_ON);
3704 edit->column_highlight = 0;
3705 edit_mark_cmd (edit, TRUE);
3709 switch (command)
3711 case CK_TopOnScreen:
3712 case CK_BottomOnScreen:
3713 case CK_MarkToPageBegin:
3714 case CK_MarkToPageEnd:
3715 case CK_Up:
3716 case CK_Down:
3717 case CK_WordLeft:
3718 case CK_WordRight:
3719 case CK_MarkToWordBegin:
3720 case CK_MarkToWordEnd:
3721 case CK_MarkUp:
3722 case CK_MarkDown:
3723 case CK_MarkColumnUp:
3724 case CK_MarkColumnDown:
3725 if (edit->mark2 == -1)
3726 break; /*marking is following the cursor: may need to highlight a whole line */
3727 case CK_Left:
3728 case CK_Right:
3729 case CK_MarkLeft:
3730 case CK_MarkRight:
3731 edit->force |= REDRAW_CHAR_ONLY;
3734 /* basic cursor key commands */
3735 switch (command)
3737 case CK_BackSpace:
3738 /* if non persistent selection and text selected */
3739 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3740 edit_block_delete_cmd (edit);
3741 else if (option_cursor_beyond_eol && edit->over_col > 0)
3742 edit->over_col--;
3743 else if (option_backspace_through_tabs && is_in_indent (edit))
3745 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3746 edit_backspace (edit, 1);
3748 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3750 int i;
3752 for (i = 0; i < HALF_TAB_SIZE; i++)
3753 edit_backspace (edit, 1);
3755 else
3756 edit_backspace (edit, 0);
3757 break;
3758 case CK_Delete:
3759 /* if non persistent selection and text selected */
3760 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3761 edit_block_delete_cmd (edit);
3762 else
3764 if (option_cursor_beyond_eol && edit->over_col > 0)
3765 edit_insert_over (edit);
3767 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3769 int i;
3771 for (i = 1; i <= HALF_TAB_SIZE; i++)
3772 edit_delete (edit, 1);
3774 else
3775 edit_delete (edit, 0);
3777 break;
3778 case CK_DeleteToWordBegin:
3779 edit->over_col = 0;
3780 edit_left_delete_word (edit);
3781 break;
3782 case CK_DeleteToWordEnd:
3783 if (option_cursor_beyond_eol && edit->over_col > 0)
3784 edit_insert_over (edit);
3786 edit_right_delete_word (edit);
3787 break;
3788 case CK_DeleteLine:
3789 edit_delete_line (edit);
3790 break;
3791 case CK_DeleteToHome:
3792 edit_delete_to_line_begin (edit);
3793 break;
3794 case CK_DeleteToEnd:
3795 edit_delete_to_line_end (edit);
3796 break;
3797 case CK_Enter:
3798 edit->over_col = 0;
3799 if (option_auto_para_formatting)
3801 edit_double_newline (edit);
3802 if (option_return_does_auto_indent)
3803 edit_auto_indent (edit);
3804 format_paragraph (edit, 0);
3806 else
3808 edit_insert (edit, '\n');
3809 if (option_return_does_auto_indent)
3810 edit_auto_indent (edit);
3812 break;
3813 case CK_Return:
3814 edit_insert (edit, '\n');
3815 break;
3817 case CK_MarkColumnPageUp:
3818 edit->column_highlight = 1;
3819 case CK_PageUp:
3820 case CK_MarkPageUp:
3821 edit_move_up (edit, edit->widget.lines - 1, 1);
3822 break;
3823 case CK_MarkColumnPageDown:
3824 edit->column_highlight = 1;
3825 case CK_PageDown:
3826 case CK_MarkPageDown:
3827 edit_move_down (edit, edit->widget.lines - 1, 1);
3828 break;
3829 case CK_MarkColumnLeft:
3830 edit->column_highlight = 1;
3831 case CK_Left:
3832 case CK_MarkLeft:
3833 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3835 if (option_cursor_beyond_eol && edit->over_col > 0)
3836 edit->over_col--;
3837 else
3838 edit_cursor_move (edit, -HALF_TAB_SIZE);
3839 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3841 else
3842 edit_left_char_move_cmd (edit);
3843 break;
3844 case CK_MarkColumnRight:
3845 edit->column_highlight = 1;
3846 case CK_Right:
3847 case CK_MarkRight:
3848 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3850 edit_cursor_move (edit, HALF_TAB_SIZE);
3851 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3853 else
3854 edit_right_char_move_cmd (edit);
3855 break;
3856 case CK_TopOnScreen:
3857 case CK_MarkToPageBegin:
3858 edit_begin_page (edit);
3859 break;
3860 case CK_BottomOnScreen:
3861 case CK_MarkToPageEnd:
3862 edit_end_page (edit);
3863 break;
3864 case CK_WordLeft:
3865 case CK_MarkToWordBegin:
3866 edit->over_col = 0;
3867 edit_left_word_move_cmd (edit);
3868 break;
3869 case CK_WordRight:
3870 case CK_MarkToWordEnd:
3871 edit->over_col = 0;
3872 edit_right_word_move_cmd (edit);
3873 break;
3874 case CK_MarkColumnUp:
3875 edit->column_highlight = 1;
3876 case CK_Up:
3877 case CK_MarkUp:
3878 edit_move_up (edit, 1, 0);
3879 break;
3880 case CK_MarkColumnDown:
3881 edit->column_highlight = 1;
3882 case CK_Down:
3883 case CK_MarkDown:
3884 edit_move_down (edit, 1, 0);
3885 break;
3886 case CK_MarkColumnParagraphUp:
3887 edit->column_highlight = 1;
3888 case CK_ParagraphUp:
3889 case CK_MarkParagraphUp:
3890 edit_move_up_paragraph (edit, 0);
3891 break;
3892 case CK_MarkColumnParagraphDown:
3893 edit->column_highlight = 1;
3894 case CK_ParagraphDown:
3895 case CK_MarkParagraphDown:
3896 edit_move_down_paragraph (edit, 0);
3897 break;
3898 case CK_MarkColumnScrollUp:
3899 edit->column_highlight = 1;
3900 case CK_ScrollUp:
3901 case CK_MarkScrollUp:
3902 edit_move_up (edit, 1, 1);
3903 break;
3904 case CK_MarkColumnScrollDown:
3905 edit->column_highlight = 1;
3906 case CK_ScrollDown:
3907 case CK_MarkScrollDown:
3908 edit_move_down (edit, 1, 1);
3909 break;
3910 case CK_Home:
3911 case CK_MarkToHome:
3912 edit_cursor_to_bol (edit);
3913 break;
3914 case CK_End:
3915 case CK_MarkToEnd:
3916 edit_cursor_to_eol (edit);
3917 break;
3918 case CK_Tab:
3919 /* if text marked shift block */
3920 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3922 if (edit->mark2 < 0)
3923 edit_mark_cmd (edit, FALSE);
3924 edit_move_block_to_right (edit);
3926 else
3928 if (option_cursor_beyond_eol)
3929 edit_insert_over (edit);
3930 edit_tab_cmd (edit);
3931 if (option_auto_para_formatting)
3933 format_paragraph (edit, 0);
3934 edit->force |= REDRAW_PAGE;
3936 else
3937 check_and_wrap_line (edit);
3939 break;
3941 case CK_InsertOverwrite:
3942 edit->overwrite = !edit->overwrite;
3943 break;
3945 case CK_Mark:
3946 if (edit->mark2 >= 0)
3948 if (edit->column_highlight)
3949 edit_push_undo_action (edit, COLUMN_ON);
3950 edit->column_highlight = 0;
3952 edit_mark_cmd (edit, FALSE);
3953 break;
3954 case CK_MarkColumn:
3955 if (!edit->column_highlight)
3956 edit_push_undo_action (edit, COLUMN_OFF);
3957 edit->column_highlight = 1;
3958 edit_mark_cmd (edit, FALSE);
3959 break;
3960 case CK_MarkAll:
3961 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3962 edit->force |= REDRAW_PAGE;
3963 break;
3964 case CK_Unmark:
3965 if (edit->column_highlight)
3966 edit_push_undo_action (edit, COLUMN_ON);
3967 edit->column_highlight = 0;
3968 edit_mark_cmd (edit, TRUE);
3969 break;
3970 case CK_MarkWord:
3971 if (edit->column_highlight)
3972 edit_push_undo_action (edit, COLUMN_ON);
3973 edit->column_highlight = 0;
3974 edit_mark_current_word_cmd (edit);
3975 break;
3976 case CK_MarkLine:
3977 if (edit->column_highlight)
3978 edit_push_undo_action (edit, COLUMN_ON);
3979 edit->column_highlight = 0;
3980 edit_mark_current_line_cmd (edit);
3981 break;
3983 case CK_Bookmark:
3984 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3985 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3986 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3987 else
3988 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3989 break;
3990 case CK_BookmarkFlush:
3991 book_mark_flush (edit, BOOK_MARK_COLOR);
3992 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3993 edit->force |= REDRAW_PAGE;
3994 break;
3995 case CK_BookmarkNext:
3996 if (edit->book_mark)
3998 struct _book_mark *p;
4000 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4001 if (p->next != NULL)
4003 p = p->next;
4004 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4005 edit_move_display (edit, p->line - edit->widget.lines / 2);
4006 edit_move_to_line (edit, p->line);
4009 break;
4010 case CK_BookmarkPrev:
4011 if (edit->book_mark)
4013 struct _book_mark *p;
4015 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4016 while (p->line == edit->curs_line)
4017 if (p->prev != NULL)
4018 p = p->prev;
4019 if (p->line >= 0)
4021 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4022 edit_move_display (edit, p->line - edit->widget.lines / 2);
4023 edit_move_to_line (edit, p->line);
4026 break;
4028 case CK_Top:
4029 case CK_MarkToFileBegin:
4030 edit_move_to_top (edit);
4031 break;
4032 case CK_Bottom:
4033 case CK_MarkToFileEnd:
4034 edit_move_to_bottom (edit);
4035 break;
4037 case CK_Copy:
4038 if (option_cursor_beyond_eol && edit->over_col > 0)
4039 edit_insert_over (edit);
4040 edit_block_copy_cmd (edit);
4041 break;
4042 case CK_Remove:
4043 edit_block_delete_cmd (edit);
4044 break;
4045 case CK_Move:
4046 edit_block_move_cmd (edit);
4047 break;
4049 case CK_BlockShiftLeft:
4050 if (edit->mark1 != edit->mark2)
4051 edit_move_block_to_left (edit);
4052 break;
4053 case CK_BlockShiftRight:
4054 if (edit->mark1 != edit->mark2)
4055 edit_move_block_to_right (edit);
4056 break;
4057 case CK_Store:
4058 edit_copy_to_X_buf_cmd (edit);
4059 break;
4060 case CK_Cut:
4061 edit_cut_to_X_buf_cmd (edit);
4062 break;
4063 case CK_Paste:
4064 /* if non persistent selection and text selected */
4065 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4066 edit_block_delete_cmd (edit);
4067 if (option_cursor_beyond_eol && edit->over_col > 0)
4068 edit_insert_over (edit);
4069 edit_paste_from_X_buf_cmd (edit);
4070 break;
4071 case CK_History:
4072 edit_paste_from_history (edit);
4073 break;
4075 case CK_SaveAs:
4076 edit_save_as_cmd (edit);
4077 break;
4078 case CK_Save:
4079 edit_save_confirm_cmd (edit);
4080 break;
4081 case CK_BlockSave:
4082 edit_save_block_cmd (edit);
4083 break;
4084 case CK_InsertFile:
4085 edit_insert_file_cmd (edit);
4086 break;
4088 case CK_FilePrev:
4089 edit_load_back_cmd (edit);
4090 break;
4091 case CK_FileNext:
4092 edit_load_forward_cmd (edit);
4093 break;
4095 case CK_SyntaxChoose:
4096 edit_syntax_dialog (edit);
4097 break;
4099 case CK_Search:
4100 edit_search_cmd (edit, FALSE);
4101 break;
4102 case CK_SearchContinue:
4103 edit_search_cmd (edit, TRUE);
4104 break;
4105 case CK_Replace:
4106 edit_replace_cmd (edit, 0);
4107 break;
4108 case CK_ReplaceContinue:
4109 edit_replace_cmd (edit, 1);
4110 break;
4111 case CK_Complete:
4112 /* if text marked shift block */
4113 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4114 edit_move_block_to_left (edit);
4115 else
4116 edit_complete_word_cmd (edit);
4117 break;
4118 case CK_Find:
4119 edit_get_match_keyword_cmd (edit);
4120 break;
4122 #ifdef HAVE_ASPELL
4123 case CK_SpellCheckCurrentWord:
4124 edit_suggest_current_word (edit);
4125 break;
4126 case CK_SpellCheck:
4127 edit_spellcheck_file (edit);
4128 break;
4129 case CK_SpellCheckSelectLang:
4130 edit_set_spell_lang ();
4131 break;
4132 #endif
4134 case CK_Date:
4136 char s[BUF_MEDIUM];
4137 /* fool gcc to prevent a Y2K warning */
4138 char time_format[] = "_c";
4139 time_format[0] = '%';
4141 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4142 edit_print_string (edit, s);
4143 edit->force |= REDRAW_PAGE;
4145 break;
4146 case CK_Goto:
4147 edit_goto_cmd (edit);
4148 break;
4149 case CK_ParagraphFormat:
4150 format_paragraph (edit, 1);
4151 edit->force |= REDRAW_PAGE;
4152 break;
4153 case CK_MacroDelete:
4154 edit_delete_macro_cmd (edit);
4155 break;
4156 case CK_MatchBracket:
4157 edit_goto_matching_bracket (edit);
4158 break;
4159 case CK_UserMenu:
4160 user_menu (edit, NULL, -1);
4161 break;
4162 case CK_Sort:
4163 edit_sort_cmd (edit);
4164 break;
4165 case CK_ExternalCommand:
4166 edit_ext_cmd (edit);
4167 break;
4168 case CK_Mail:
4169 edit_mail_dialog (edit);
4170 break;
4171 #ifdef HAVE_CHARSET
4172 case CK_SelectCodepage:
4173 edit_select_codepage_cmd (edit);
4174 break;
4175 #endif
4176 case CK_InsertLiteral:
4177 edit_insert_literal_cmd (edit);
4178 break;
4179 case CK_MacroStartStopRecord:
4180 edit_begin_end_macro_cmd (edit);
4181 break;
4182 case CK_RepeatStartStopRecord:
4183 edit_begin_end_repeat_cmd (edit);
4184 break;
4185 case CK_ExtendedKeyMap:
4186 edit->extmod = TRUE;
4187 break;
4188 default:
4189 break;
4192 /* CK_PipeBlock */
4193 if ((command / CK_PipeBlock (0)) == 1)
4194 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4196 /* keys which must set the col position, and the search vars */
4197 switch (command)
4199 case CK_Search:
4200 case CK_SearchContinue:
4201 case CK_Replace:
4202 case CK_ReplaceContinue:
4203 case CK_Complete:
4204 edit->prev_col = edit_get_col (edit);
4205 break;
4206 case CK_Up:
4207 case CK_MarkUp:
4208 case CK_MarkColumnUp:
4209 case CK_Down:
4210 case CK_MarkDown:
4211 case CK_MarkColumnDown:
4212 case CK_PageUp:
4213 case CK_MarkPageUp:
4214 case CK_MarkColumnPageUp:
4215 case CK_PageDown:
4216 case CK_MarkPageDown:
4217 case CK_MarkColumnPageDown:
4218 case CK_Top:
4219 case CK_MarkToFileBegin:
4220 case CK_Bottom:
4221 case CK_MarkToFileEnd:
4222 case CK_ParagraphUp:
4223 case CK_MarkParagraphUp:
4224 case CK_MarkColumnParagraphUp:
4225 case CK_ParagraphDown:
4226 case CK_MarkParagraphDown:
4227 case CK_MarkColumnParagraphDown:
4228 case CK_ScrollUp:
4229 case CK_MarkScrollUp:
4230 case CK_MarkColumnScrollUp:
4231 case CK_ScrollDown:
4232 case CK_MarkScrollDown:
4233 case CK_MarkColumnScrollDown:
4234 edit->search_start = edit->curs1;
4235 edit->found_len = 0;
4236 break;
4237 default:
4238 edit->found_len = 0;
4239 edit->prev_col = edit_get_col (edit);
4240 edit->search_start = edit->curs1;
4242 edit_find_bracket (edit);
4244 if (option_auto_para_formatting)
4246 switch (command)
4248 case CK_BackSpace:
4249 case CK_Delete:
4250 case CK_DeleteToWordBegin:
4251 case CK_DeleteToWordEnd:
4252 case CK_DeleteToHome:
4253 case CK_DeleteToEnd:
4254 format_paragraph (edit, 0);
4255 edit->force |= REDRAW_PAGE;
4260 /* --------------------------------------------------------------------------------------------- */
4262 void
4263 edit_stack_init (void)
4265 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4267 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4268 edit_history_moveto[edit_stack_iterator].line = -1;
4271 edit_stack_iterator = 0;
4274 /* --------------------------------------------------------------------------------------------- */
4276 void
4277 edit_stack_free (void)
4279 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4280 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4283 /* --------------------------------------------------------------------------------------------- */
4284 /** move i lines */
4286 void
4287 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4289 edit_move_updown (edit, i, do_scroll, TRUE);
4292 /* --------------------------------------------------------------------------------------------- */
4293 /** move i lines */
4295 void
4296 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4298 edit_move_updown (edit, i, do_scroll, FALSE);
4301 /* --------------------------------------------------------------------------------------------- */