Use off_t instead of long for all variables related to position in file:
[midnight-commander.git] / src / editor / edit.c
blob0807972f347b188ebd28f0b32ce61d359acd935b
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 static int left_of_four_spaces (WEdit * edit);
176 /* --------------------------------------------------------------------------------------------- */
178 * Initialize the buffers for an empty files.
181 static void
182 edit_init_buffers (WEdit * edit)
184 int j;
186 for (j = 0; j <= MAXBUFF; j++)
188 edit->buffers1[j] = NULL;
189 edit->buffers2[j] = NULL;
192 edit->curs1 = 0;
193 edit->curs2 = 0;
194 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
197 /* --------------------------------------------------------------------------------------------- */
200 * Load file OR text into buffers. Set cursor to the beginning of file.
202 * @returns FALSE on error.
205 static gboolean
206 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
208 off_t buf, buf2;
209 int file = -1;
210 gboolean ret = FALSE;
212 edit->curs2 = edit->last_byte;
213 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
215 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
216 if (file == -1)
218 gchar *errmsg, *filename;
220 filename = vfs_path_to_str (filename_vpath);
221 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
222 g_free (filename);
223 edit_error_dialog (_("Error"), errmsg);
224 g_free (errmsg);
225 return FALSE;
228 if (!edit->buffers2[buf2])
229 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
233 if (mc_read (file,
234 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
235 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
236 break;
238 for (buf = buf2 - 1; buf >= 0; buf--)
240 /* edit->buffers2[0] is already allocated */
241 if (!edit->buffers2[buf])
242 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
243 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
244 break;
246 ret = TRUE;
248 while (FALSE);
250 if (!ret)
252 gchar *errmsg, *filename;
254 filename = vfs_path_to_str (filename_vpath);
255 errmsg = g_strdup_printf (_("Error reading %s"), filename);
256 g_free (filename);
257 edit_error_dialog (_("Error"), errmsg);
258 g_free (errmsg);
260 mc_close (file);
261 return ret;
264 /* --------------------------------------------------------------------------------------------- */
265 /** Return index of the filter or -1 is there is no appropriate filter */
267 static int
268 edit_find_filter (const vfs_path_t * filename_vpath)
270 size_t i, l, e;
271 char *filename;
273 if (filename_vpath == NULL)
274 return -1;
276 filename = vfs_path_to_str (filename_vpath);
277 l = strlen (filename);
278 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
280 e = strlen (all_filters[i].extension);
281 if (l > e)
282 if (!strcmp (all_filters[i].extension, filename + l - e))
284 g_free (filename);
285 return i;
288 g_free (filename);
289 return -1;
292 /* --------------------------------------------------------------------------------------------- */
294 static char *
295 edit_get_filter (const vfs_path_t * filename_vpath)
297 int i;
298 char *p, *quoted_name, *filename;
300 i = edit_find_filter (filename_vpath);
301 if (i < 0)
302 return NULL;
304 filename = vfs_path_to_str (filename_vpath);
305 quoted_name = name_quote (filename, 0);
306 g_free (filename);
307 p = g_strdup_printf (all_filters[i].read, quoted_name);
308 g_free (quoted_name);
309 return p;
312 /* --------------------------------------------------------------------------------------------- */
314 static off_t
315 edit_insert_stream (WEdit * edit, FILE * f)
317 int c;
318 off_t i = 0;
319 while ((c = fgetc (f)) >= 0)
321 edit_insert (edit, c);
322 i++;
324 return i;
327 /* --------------------------------------------------------------------------------------------- */
329 * Open file and create it if necessary.
331 * @param edit editor object
332 * @param filename_vpath file name
333 * @param st buffer for store stat info
334 * @returns TRUE for success, FALSE for error.
337 static gboolean
338 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
340 int file;
341 gchar *errmsg = NULL;
343 /* Try opening an existing file */
344 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
345 if (file < 0)
348 * Try creating the file. O_EXCL prevents following broken links
349 * and opening existing files.
351 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
352 if (file < 0)
354 char *filename;
356 filename = vfs_path_to_str (filename_vpath);
357 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
358 g_free (filename);
359 goto cleanup;
362 /* New file, delete it if it's not modified or saved */
363 edit->delete_file = 1;
366 /* Check what we have opened */
367 if (mc_fstat (file, st) < 0)
369 char *filename;
371 filename = vfs_path_to_str (filename_vpath);
372 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
373 g_free (filename);
374 goto cleanup;
377 /* We want to open regular files only */
378 if (!S_ISREG (st->st_mode))
380 char *filename;
382 filename = vfs_path_to_str (filename_vpath);
383 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
384 g_free (filename);
385 goto cleanup;
389 * Don't delete non-empty files.
390 * O_EXCL should prevent it, but let's be on the safe side.
392 if (st->st_size > 0)
393 edit->delete_file = 0;
395 if (st->st_size >= SIZE_LIMIT)
397 char *filename;
399 filename = vfs_path_to_str (filename_vpath);
400 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
401 g_free (filename);
404 cleanup:
405 (void) mc_close (file);
407 if (errmsg != NULL)
409 edit_error_dialog (_("Error"), errmsg);
410 g_free (errmsg);
411 return FALSE;
413 return TRUE;
416 /* --------------------------------------------------------------------------------------------- */
419 * Open the file and load it into the buffers, either directly or using
420 * a filter. Return TRUE on success, FALSE on error.
422 * Fast loading (edit_load_file_fast) is used when the file size is
423 * known. In this case the data is read into the buffers by blocks.
424 * If the file size is not known, the data is loaded byte by byte in
425 * edit_insert_file.
427 * @param edit editor object
428 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
430 static gboolean
431 edit_load_file (WEdit * edit)
433 gboolean fast_load = TRUE;
435 /* Cannot do fast load if a filter is used */
436 if (edit_find_filter (edit->filename_vpath) >= 0)
437 fast_load = FALSE;
440 * FIXME: line end translation should disable fast loading as well
441 * Consider doing fseek() to the end and ftell() for the real size.
443 if (edit->filename_vpath != NULL)
446 * VFS may report file size incorrectly, and slow load is not a big
447 * deal considering overhead in VFS.
449 if (!vfs_file_is_local (edit->filename_vpath))
450 fast_load = FALSE;
452 /* If we are dealing with a real file, check that it exists */
453 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
455 edit_clean (edit);
456 return FALSE;
459 else
461 /* nothing to load */
462 fast_load = FALSE;
465 edit_init_buffers (edit);
467 if (fast_load)
469 edit->last_byte = edit->stat1.st_size;
470 edit_load_file_fast (edit, edit->filename_vpath);
471 /* If fast load was used, the number of lines wasn't calculated */
472 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
474 else
476 edit->last_byte = 0;
477 if (edit->filename_vpath != NULL
478 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
480 edit->undo_stack_disable = 1;
481 if (edit_insert_file (edit, edit->filename_vpath) < 0)
483 edit_clean (edit);
484 return FALSE;
486 edit->undo_stack_disable = 0;
489 edit->lb = LB_ASIS;
490 return TRUE;
493 /* --------------------------------------------------------------------------------------------- */
494 /** Restore saved cursor position in the file */
496 static void
497 edit_load_position (WEdit * edit)
499 long line, column;
500 off_t offset;
502 if (edit->filename_vpath == NULL
503 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
504 return;
506 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
508 if (line > 0)
510 edit_move_to_line (edit, line - 1);
511 edit->prev_col = column;
513 else if (offset > 0)
515 edit_cursor_move (edit, offset);
516 line = edit->curs_line;
517 edit->search_start = edit->curs1;
520 book_mark_restore (edit, BOOK_MARK_COLOR);
522 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
523 edit_move_display (edit, line - (edit->widget.lines / 2));
526 /* --------------------------------------------------------------------------------------------- */
527 /** Save cursor position in the file */
529 static void
530 edit_save_position (WEdit * edit)
532 if (edit->filename_vpath == NULL
533 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
534 return;
536 book_mark_serialize (edit, BOOK_MARK_COLOR);
537 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
538 edit->serialized_bookmarks);
539 edit->serialized_bookmarks = NULL;
542 /* --------------------------------------------------------------------------------------------- */
543 /** Clean the WEdit stricture except the widget part */
545 static void
546 edit_purge_widget (WEdit * edit)
548 size_t len = sizeof (WEdit) - sizeof (Widget);
549 char *start = (char *) edit + sizeof (Widget);
550 memset (start, 0, len);
553 /* --------------------------------------------------------------------------------------------- */
556 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
557 then the file should be as it was when he loaded up. Then set edit->modified to 0.
560 static long
561 edit_pop_undo_action (WEdit * edit)
563 long c;
564 unsigned long sp = edit->undo_stack_pointer;
566 if (sp == edit->undo_stack_bottom)
567 return STACK_BOTTOM;
569 sp = (sp - 1) & edit->undo_stack_size_mask;
570 c = edit->undo_stack[sp];
571 if (c >= 0)
573 /* edit->undo_stack[sp] = '@'; */
574 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
575 return c;
578 if (sp == edit->undo_stack_bottom)
579 return STACK_BOTTOM;
581 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
582 if (edit->undo_stack[sp] == -2)
584 /* edit->undo_stack[sp] = '@'; */
585 edit->undo_stack_pointer = sp;
587 else
588 edit->undo_stack[sp]++;
590 return c;
593 static long
594 edit_pop_redo_action (WEdit * edit)
596 long c;
597 unsigned long sp = edit->redo_stack_pointer;
599 if (sp == edit->redo_stack_bottom)
600 return STACK_BOTTOM;
602 sp = (sp - 1) & edit->redo_stack_size_mask;
603 c = edit->redo_stack[sp];
604 if (c >= 0)
606 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
607 return c;
610 if (sp == edit->redo_stack_bottom)
611 return STACK_BOTTOM;
613 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
614 if (edit->redo_stack[sp] == -2)
615 edit->redo_stack_pointer = sp;
616 else
617 edit->redo_stack[sp]++;
619 return c;
622 static long
623 get_prev_undo_action (WEdit * edit)
625 long c;
626 unsigned long sp = edit->undo_stack_pointer;
628 if (sp == edit->undo_stack_bottom)
629 return STACK_BOTTOM;
631 sp = (sp - 1) & edit->undo_stack_size_mask;
632 c = edit->undo_stack[sp];
633 if (c >= 0)
634 return c;
636 if (sp == edit->undo_stack_bottom)
637 return STACK_BOTTOM;
639 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
640 return c;
643 /* --------------------------------------------------------------------------------------------- */
644 /** is called whenever a modification is made by one of the four routines below */
646 static void
647 edit_modification (WEdit * edit)
649 edit->caches_valid = FALSE;
651 /* raise lock when file modified */
652 if (!edit->modified && !edit->delete_file)
653 edit->locked = lock_file (edit->filename_vpath);
654 edit->modified = 1;
657 /* --------------------------------------------------------------------------------------------- */
659 static char *
660 edit_get_byte_ptr (WEdit * edit, off_t byte_index)
662 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
663 return NULL;
665 if (byte_index >= edit->curs1)
667 off_t p;
669 p = edit->curs1 + edit->curs2 - byte_index - 1;
670 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
671 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
674 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
675 (byte_index & M_EDIT_BUF_SIZE));
678 /* --------------------------------------------------------------------------------------------- */
680 static int
681 edit_get_prev_utf (WEdit * edit, off_t byte_index, int *char_width)
683 int i, res;
684 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
685 gchar *str;
686 gchar *cursor_buf_ptr;
688 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
690 *char_width = 0;
691 return 0;
694 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
695 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
696 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
698 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
699 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
701 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
703 *char_width = 1;
704 return *(cursor_buf_ptr - 1);
706 else
708 res = g_utf8_get_char_validated (str, -1);
710 if (res < 0)
712 *char_width = 1;
713 return *(cursor_buf_ptr - 1);
715 else
717 *char_width = cursor_buf_ptr - str;
718 return res;
723 /* --------------------------------------------------------------------------------------------- */
724 /* high level cursor movement commands */
725 /* --------------------------------------------------------------------------------------------- */
726 /** check whether cursor is in indent part of line
728 * @param edit editor object
730 * @return TRUE if cursor is in indent, FALSE otherwise
733 static gboolean
734 is_in_indent (WEdit * edit)
736 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 if (line_is_blank (edit, edit->curs_line - 1))
838 for (i = edit->curs_line - 1; i; i--)
839 if (!line_is_blank (edit, i))
841 i++;
842 break;
845 else
847 for (i = edit->curs_line - 1; i; i--)
848 if (line_is_blank (edit, i))
849 break;
852 else
854 for (i = edit->curs_line - 1; i; i--)
855 if (line_is_blank (edit, i))
856 break;
859 edit_move_up (edit, edit->curs_line - i, do_scroll);
862 /* --------------------------------------------------------------------------------------------- */
863 /** moves down until a blank line is reached, or until just
864 before a non-blank line is reached */
866 static void
867 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
869 long i;
871 if (edit->curs_line >= edit->total_lines - 1)
873 i = edit->total_lines;
875 else
877 if (line_is_blank (edit, edit->curs_line))
879 if (line_is_blank (edit, edit->curs_line + 1))
881 for (i = edit->curs_line + 1; i; i++)
882 if (!line_is_blank (edit, i) || i > edit->total_lines)
884 i--;
885 break;
888 else
890 for (i = edit->curs_line + 1; i; i++)
891 if (line_is_blank (edit, i) || i >= edit->total_lines)
892 break;
895 else
897 for (i = edit->curs_line + 1; i; i++)
898 if (line_is_blank (edit, i) || i >= edit->total_lines)
899 break;
902 edit_move_down (edit, i - edit->curs_line, do_scroll);
905 /* --------------------------------------------------------------------------------------------- */
907 static void
908 edit_begin_page (WEdit * edit)
910 edit_update_curs_row (edit);
911 edit_move_up (edit, edit->curs_row, 0);
914 /* --------------------------------------------------------------------------------------------- */
916 static void
917 edit_end_page (WEdit * edit)
919 edit_update_curs_row (edit);
920 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
924 /* --------------------------------------------------------------------------------------------- */
925 /** goto beginning of text */
927 static void
928 edit_move_to_top (WEdit * edit)
930 if (edit->curs_line)
932 edit_cursor_move (edit, -edit->curs1);
933 edit_move_to_prev_col (edit, 0);
934 edit->force |= REDRAW_PAGE;
935 edit->search_start = 0;
936 edit_update_curs_row (edit);
941 /* --------------------------------------------------------------------------------------------- */
942 /** goto end of text */
944 static void
945 edit_move_to_bottom (WEdit * edit)
947 if (edit->curs_line < edit->total_lines)
949 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
950 edit->start_display = edit->last_byte;
951 edit->start_line = edit->total_lines;
952 edit_scroll_upward (edit, edit->widget.lines - 1);
953 edit->force |= REDRAW_PAGE;
957 /* --------------------------------------------------------------------------------------------- */
958 /** goto beginning of line */
960 static void
961 edit_cursor_to_bol (WEdit * edit)
963 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
964 edit->search_start = edit->curs1;
965 edit->prev_col = edit_get_col (edit);
966 edit->over_col = 0;
969 /* --------------------------------------------------------------------------------------------- */
970 /** goto end of line */
972 static void
973 edit_cursor_to_eol (WEdit * edit)
975 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
976 edit->search_start = edit->curs1;
977 edit->prev_col = edit_get_col (edit);
978 edit->over_col = 0;
981 /* --------------------------------------------------------------------------------------------- */
983 static unsigned long
984 my_type_of (int c)
986 int x, r = 0;
987 const char *p, *q;
988 const char option_chars_move_whole_word[] =
989 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
991 if (!c)
992 return 0;
993 if (c == '!')
995 if (*option_chars_move_whole_word == '!')
996 return 2;
997 return 0x80000000UL;
999 if (g_ascii_isupper ((gchar) c))
1000 c = 'A';
1001 else if (g_ascii_islower ((gchar) c))
1002 c = 'a';
1003 else if (g_ascii_isalpha (c))
1004 c = 'a';
1005 else if (isdigit (c))
1006 c = '0';
1007 else if (isspace (c))
1008 c = ' ';
1009 q = strchr (option_chars_move_whole_word, c);
1010 if (!q)
1011 return 0xFFFFFFFFUL;
1014 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1015 if (*p == '!')
1016 x <<= 1;
1017 r |= x;
1019 while ((q = strchr (q + 1, c)));
1020 return r;
1023 /* --------------------------------------------------------------------------------------------- */
1025 static void
1026 edit_left_word_move (WEdit * edit, int s)
1028 for (;;)
1030 int c1, c2;
1031 if (edit->column_highlight
1032 && edit->mark1 != edit->mark2
1033 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1034 break;
1035 edit_cursor_move (edit, -1);
1036 if (!edit->curs1)
1037 break;
1038 c1 = edit_get_byte (edit, edit->curs1 - 1);
1039 c2 = edit_get_byte (edit, edit->curs1);
1040 if (c1 == '\n' || c2 == '\n')
1041 break;
1042 if (!(my_type_of (c1) & my_type_of (c2)))
1043 break;
1044 if (isspace (c1) && !isspace (c2))
1045 break;
1046 if (s)
1047 if (!isspace (c1) && isspace (c2))
1048 break;
1052 /* --------------------------------------------------------------------------------------------- */
1054 static void
1055 edit_left_word_move_cmd (WEdit * edit)
1057 edit_left_word_move (edit, 0);
1058 edit->force |= REDRAW_PAGE;
1061 /* --------------------------------------------------------------------------------------------- */
1063 static void
1064 edit_right_word_move (WEdit * edit, int s)
1066 for (;;)
1068 int c1, c2;
1069 if (edit->column_highlight
1070 && edit->mark1 != edit->mark2
1071 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1072 break;
1073 edit_cursor_move (edit, 1);
1074 if (edit->curs1 >= edit->last_byte)
1075 break;
1076 c1 = edit_get_byte (edit, edit->curs1 - 1);
1077 c2 = edit_get_byte (edit, edit->curs1);
1078 if (c1 == '\n' || c2 == '\n')
1079 break;
1080 if (!(my_type_of (c1) & my_type_of (c2)))
1081 break;
1082 if (isspace (c1) && !isspace (c2))
1083 break;
1084 if (s)
1085 if (!isspace (c1) && isspace (c2))
1086 break;
1090 /* --------------------------------------------------------------------------------------------- */
1092 static void
1093 edit_right_word_move_cmd (WEdit * edit)
1095 edit_right_word_move (edit, 0);
1096 edit->force |= REDRAW_PAGE;
1099 /* --------------------------------------------------------------------------------------------- */
1101 static void
1102 edit_right_char_move_cmd (WEdit * edit)
1104 int cw = 1;
1105 int c = 0;
1106 if (edit->utf8)
1108 c = edit_get_utf (edit, edit->curs1, &cw);
1109 if (cw < 1)
1110 cw = 1;
1112 else
1114 c = edit_get_byte (edit, edit->curs1);
1116 if (option_cursor_beyond_eol && c == '\n')
1118 edit->over_col++;
1120 else
1122 edit_cursor_move (edit, cw);
1126 /* --------------------------------------------------------------------------------------------- */
1128 static void
1129 edit_left_char_move_cmd (WEdit * edit)
1131 int cw = 1;
1132 if (edit->column_highlight
1133 && option_cursor_beyond_eol
1134 && edit->mark1 != edit->mark2
1135 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1136 return;
1137 if (edit->utf8)
1139 edit_get_prev_utf (edit, edit->curs1, &cw);
1140 if (cw < 1)
1141 cw = 1;
1143 if (option_cursor_beyond_eol && edit->over_col > 0)
1145 edit->over_col--;
1147 else
1149 edit_cursor_move (edit, -cw);
1153 /* --------------------------------------------------------------------------------------------- */
1154 /** Up or down cursor moving.
1155 direction = TRUE - move up
1156 = FALSE - move down
1159 static void
1160 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1162 long p;
1163 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1165 if (lines > l)
1166 lines = l;
1168 if (lines == 0)
1169 return;
1171 if (lines > 1)
1172 edit->force |= REDRAW_PAGE;
1173 if (do_scroll)
1175 if (direction)
1176 edit_scroll_upward (edit, lines);
1177 else
1178 edit_scroll_downward (edit, lines);
1180 p = edit_bol (edit, edit->curs1);
1182 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1184 edit_cursor_move (edit, p - edit->curs1);
1186 edit_move_to_prev_col (edit, p);
1188 /* search start of current multibyte char (like CJK) */
1189 if (edit->curs1 + 1 < edit->last_byte)
1191 edit_right_char_move_cmd (edit);
1192 edit_left_char_move_cmd (edit);
1195 edit->search_start = edit->curs1;
1196 edit->found_len = 0;
1199 /* --------------------------------------------------------------------------------------------- */
1201 static void
1202 edit_right_delete_word (WEdit * edit)
1204 int c1, c2;
1205 for (;;)
1207 if (edit->curs1 >= edit->last_byte)
1208 break;
1209 c1 = edit_delete (edit, 1);
1210 c2 = edit_get_byte (edit, edit->curs1);
1211 if (c1 == '\n' || c2 == '\n')
1212 break;
1213 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1214 break;
1215 if (!(my_type_of (c1) & my_type_of (c2)))
1216 break;
1220 /* --------------------------------------------------------------------------------------------- */
1222 static void
1223 edit_left_delete_word (WEdit * edit)
1225 int c1, c2;
1226 for (;;)
1228 if (edit->curs1 <= 0)
1229 break;
1230 c1 = edit_backspace (edit, 1);
1231 c2 = edit_get_byte (edit, edit->curs1 - 1);
1232 if (c1 == '\n' || c2 == '\n')
1233 break;
1234 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1235 break;
1236 if (!(my_type_of (c1) & my_type_of (c2)))
1237 break;
1241 /* --------------------------------------------------------------------------------------------- */
1243 the start column position is not recorded, and hence does not
1244 undo as it happed. But who would notice.
1247 static void
1248 edit_do_undo (WEdit * edit)
1250 long ac;
1251 long count = 0;
1253 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1254 edit->over_col = 0;
1255 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1257 switch ((int) ac)
1259 case STACK_BOTTOM:
1260 goto done_undo;
1261 case CURS_RIGHT:
1262 edit_cursor_move (edit, 1);
1263 break;
1264 case CURS_LEFT:
1265 edit_cursor_move (edit, -1);
1266 break;
1267 case BACKSPACE:
1268 case BACKSPACE_BR:
1269 edit_backspace (edit, 1);
1270 break;
1271 case DELCHAR:
1272 case DELCHAR_BR:
1273 edit_delete (edit, 1);
1274 break;
1275 case COLUMN_ON:
1276 edit->column_highlight = 1;
1277 break;
1278 case COLUMN_OFF:
1279 edit->column_highlight = 0;
1280 break;
1282 if (ac >= 256 && ac < 512)
1283 edit_insert_ahead (edit, ac - 256);
1284 if (ac >= 0 && ac < 256)
1285 edit_insert (edit, ac);
1287 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1289 edit->mark1 = ac - MARK_1;
1290 edit->column1 =
1291 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1293 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1295 edit->mark2 = ac - MARK_2;
1296 edit->column2 =
1297 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1299 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1301 edit->end_mark_curs = ac - MARK_CURS;
1303 if (count++)
1304 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1307 if (edit->start_display > ac - KEY_PRESS)
1309 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1310 edit->force |= REDRAW_PAGE;
1312 else if (edit->start_display < ac - KEY_PRESS)
1314 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1315 edit->force |= REDRAW_PAGE;
1317 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1318 edit_update_curs_row (edit);
1320 done_undo:;
1321 edit->undo_stack_disable = 0;
1324 static void
1325 edit_do_redo (WEdit * edit)
1327 long ac;
1328 long count = 0;
1330 if (edit->redo_stack_reset)
1331 return;
1333 edit->over_col = 0;
1334 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1336 switch ((int) ac)
1338 case STACK_BOTTOM:
1339 goto done_redo;
1340 case CURS_RIGHT:
1341 edit_cursor_move (edit, 1);
1342 break;
1343 case CURS_LEFT:
1344 edit_cursor_move (edit, -1);
1345 break;
1346 case BACKSPACE:
1347 edit_backspace (edit, 1);
1348 break;
1349 case DELCHAR:
1350 edit_delete (edit, 1);
1351 break;
1352 case COLUMN_ON:
1353 edit->column_highlight = 1;
1354 break;
1355 case COLUMN_OFF:
1356 edit->column_highlight = 0;
1357 break;
1359 if (ac >= 256 && ac < 512)
1360 edit_insert_ahead (edit, ac - 256);
1361 if (ac >= 0 && ac < 256)
1362 edit_insert (edit, ac);
1364 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1366 edit->mark1 = ac - MARK_1;
1367 edit->column1 =
1368 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1370 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1372 edit->mark2 = ac - MARK_2;
1373 edit->column2 =
1374 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1376 /* more than one pop usually means something big */
1377 if (count++)
1378 edit->force |= REDRAW_PAGE;
1381 if (edit->start_display > ac - KEY_PRESS)
1383 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1384 edit->force |= REDRAW_PAGE;
1386 else if (edit->start_display < ac - KEY_PRESS)
1388 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1389 edit->force |= REDRAW_PAGE;
1391 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1392 edit_update_curs_row (edit);
1394 done_redo:;
1397 static void
1398 edit_group_undo (WEdit * edit)
1400 long ac = KEY_PRESS;
1401 long cur_ac = KEY_PRESS;
1402 while (ac != STACK_BOTTOM && ac == cur_ac)
1404 cur_ac = get_prev_undo_action (edit);
1405 edit_do_undo (edit);
1406 ac = get_prev_undo_action (edit);
1407 /* exit from cycle if option_group_undo is not set,
1408 * and make single UNDO operation
1410 if (!option_group_undo)
1411 ac = STACK_BOTTOM;
1415 /* --------------------------------------------------------------------------------------------- */
1417 static void
1418 edit_delete_to_line_end (WEdit * edit)
1420 while (edit_get_byte (edit, edit->curs1) != '\n')
1422 if (!edit->curs2)
1423 break;
1424 edit_delete (edit, 1);
1428 /* --------------------------------------------------------------------------------------------- */
1430 static void
1431 edit_delete_to_line_begin (WEdit * edit)
1433 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1435 if (!edit->curs1)
1436 break;
1437 edit_backspace (edit, 1);
1441 /* --------------------------------------------------------------------------------------------- */
1443 static int
1444 is_aligned_on_a_tab (WEdit * edit)
1446 edit_update_curs_col (edit);
1447 return !((edit->curs_col % (TAB_SIZE * space_width))
1448 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1451 /* --------------------------------------------------------------------------------------------- */
1453 static int
1454 right_of_four_spaces (WEdit * edit)
1456 int i, ch = 0;
1457 for (i = 1; i <= HALF_TAB_SIZE; i++)
1458 ch |= edit_get_byte (edit, edit->curs1 - i);
1459 if (ch == ' ')
1460 return is_aligned_on_a_tab (edit);
1461 return 0;
1464 /* --------------------------------------------------------------------------------------------- */
1466 static int
1467 left_of_four_spaces (WEdit * edit)
1469 int i, ch = 0;
1470 for (i = 0; i < HALF_TAB_SIZE; i++)
1471 ch |= edit_get_byte (edit, edit->curs1 + i);
1472 if (ch == ' ')
1473 return is_aligned_on_a_tab (edit);
1474 return 0;
1477 /* --------------------------------------------------------------------------------------------- */
1479 static void
1480 edit_auto_indent (WEdit * edit)
1482 off_t p;
1483 char c;
1484 p = edit->curs1;
1485 /* use the previous line as a template */
1486 p = edit_move_backward (edit, p, 1);
1487 /* copy the leading whitespace of the line */
1488 for (;;)
1489 { /* no range check - the line _is_ \n-terminated */
1490 c = edit_get_byte (edit, p++);
1491 if (c != ' ' && c != '\t')
1492 break;
1493 edit_insert (edit, c);
1497 /* --------------------------------------------------------------------------------------------- */
1499 static inline void
1500 edit_double_newline (WEdit * edit)
1502 edit_insert (edit, '\n');
1503 if (edit_get_byte (edit, edit->curs1) == '\n')
1504 return;
1505 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1506 return;
1507 edit->force |= REDRAW_PAGE;
1508 edit_insert (edit, '\n');
1512 /* --------------------------------------------------------------------------------------------- */
1514 static void
1515 insert_spaces_tab (WEdit * edit, gboolean half)
1517 long i;
1519 edit_update_curs_col (edit);
1520 i = option_tab_spacing * space_width;
1521 if (half)
1522 i /= 2;
1523 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1524 while (i > 0)
1526 edit_insert (edit, ' ');
1527 i -= space_width;
1531 /* --------------------------------------------------------------------------------------------- */
1533 static inline void
1534 edit_tab_cmd (WEdit * edit)
1536 int i;
1538 if (option_fake_half_tabs)
1540 if (is_in_indent (edit))
1542 /*insert a half tab (usually four spaces) unless there is a
1543 half tab already behind, then delete it and insert a
1544 full tab. */
1545 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1546 insert_spaces_tab (edit, TRUE);
1547 else
1549 for (i = 1; i <= HALF_TAB_SIZE; i++)
1550 edit_backspace (edit, 1);
1551 edit_insert (edit, '\t');
1553 return;
1556 if (option_fill_tabs_with_spaces)
1557 insert_spaces_tab (edit, FALSE);
1558 else
1559 edit_insert (edit, '\t');
1562 /* --------------------------------------------------------------------------------------------- */
1564 static void
1565 check_and_wrap_line (WEdit * edit)
1567 off_t curs;
1568 int c;
1570 if (!option_typewriter_wrap)
1571 return;
1572 edit_update_curs_col (edit);
1573 if (edit->curs_col < option_word_wrap_line_length)
1574 return;
1575 curs = edit->curs1;
1576 for (;;)
1578 curs--;
1579 c = edit_get_byte (edit, curs);
1580 if (c == '\n' || curs <= 0)
1582 edit_insert (edit, '\n');
1583 return;
1585 if (c == ' ' || c == '\t')
1587 off_t current = edit->curs1;
1588 edit_cursor_move (edit, curs - edit->curs1 + 1);
1589 edit_insert (edit, '\n');
1590 edit_cursor_move (edit, current - edit->curs1 + 1);
1591 return;
1596 /* --------------------------------------------------------------------------------------------- */
1597 /** this find the matching bracket in either direction, and sets edit->bracket
1599 * @param edit editor object
1600 * @param in_screen seach only on the current screen
1601 * @param furthest_bracket_search count of the bytes for search
1603 * @return position of the found bracket (-1 if no match)
1606 static off_t
1607 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1609 const char *const b = "{}{[][()(", *p;
1610 int i = 1, a, inc = -1, c, d, n = 0;
1611 unsigned long j = 0;
1612 off_t q;
1613 edit_update_curs_row (edit);
1614 c = edit_get_byte (edit, edit->curs1);
1615 p = strchr (b, c);
1616 /* no limit */
1617 if (!furthest_bracket_search)
1618 furthest_bracket_search--;
1619 /* not on a bracket at all */
1620 if (p == NULL)
1621 return -1;
1622 /* the matching bracket */
1623 d = p[1];
1624 /* going left or right? */
1625 if (strchr ("{[(", c))
1626 inc = 1;
1627 for (q = edit->curs1 + inc;; q += inc)
1629 /* out of buffer? */
1630 if (q >= edit->last_byte || q < 0)
1631 break;
1632 a = edit_get_byte (edit, q);
1633 /* don't want to eat CPU */
1634 if (j++ > furthest_bracket_search)
1635 break;
1636 /* out of screen? */
1637 if (in_screen)
1639 if (q < edit->start_display)
1640 break;
1641 /* count lines if searching downward */
1642 if (inc > 0 && a == '\n')
1643 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1644 break;
1646 /* count bracket depth */
1647 i += (a == c) - (a == d);
1648 /* return if bracket depth is zero */
1649 if (i == 0)
1650 return q;
1652 /* no match */
1653 return -1;
1656 /* --------------------------------------------------------------------------------------------- */
1658 static inline void
1659 edit_goto_matching_bracket (WEdit * edit)
1661 off_t q;
1663 q = edit_get_bracket (edit, 0, 0);
1664 if (q >= 0)
1666 edit->bracket = edit->curs1;
1667 edit->force |= REDRAW_PAGE;
1668 edit_cursor_move (edit, q - edit->curs1);
1672 /* --------------------------------------------------------------------------------------------- */
1674 static void
1675 edit_move_block_to_right (WEdit * edit)
1677 off_t start_mark, end_mark;
1678 long cur_bol, start_bol;
1680 if (eval_marks (edit, &start_mark, &end_mark))
1681 return;
1683 start_bol = edit_bol (edit, start_mark);
1684 cur_bol = edit_bol (edit, end_mark - 1);
1688 edit_cursor_move (edit, cur_bol - edit->curs1);
1689 if (option_fill_tabs_with_spaces)
1690 insert_spaces_tab (edit, option_fake_half_tabs);
1691 else
1692 edit_insert (edit, '\t');
1693 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1695 if (cur_bol == 0)
1696 break;
1698 cur_bol = edit_bol (edit, cur_bol - 1);
1700 while (cur_bol >= start_bol);
1702 edit->force |= REDRAW_PAGE;
1705 /* --------------------------------------------------------------------------------------------- */
1707 static void
1708 edit_move_block_to_left (WEdit * edit)
1710 off_t start_mark, end_mark;
1711 off_t cur_bol, start_bol;
1712 int i;
1714 if (eval_marks (edit, &start_mark, &end_mark))
1715 return;
1717 start_bol = edit_bol (edit, start_mark);
1718 cur_bol = edit_bol (edit, end_mark - 1);
1722 int del_tab_width;
1723 int next_char;
1725 edit_cursor_move (edit, cur_bol - edit->curs1);
1727 if (option_fake_half_tabs)
1728 del_tab_width = HALF_TAB_SIZE;
1729 else
1730 del_tab_width = option_tab_spacing;
1732 next_char = edit_get_byte (edit, edit->curs1);
1733 if (next_char == '\t')
1734 edit_delete (edit, 1);
1735 else if (next_char == ' ')
1736 for (i = 1; i <= del_tab_width; i++)
1738 if (next_char == ' ')
1739 edit_delete (edit, 1);
1740 next_char = edit_get_byte (edit, edit->curs1);
1743 if (cur_bol == 0)
1744 break;
1746 cur_bol = edit_bol (edit, cur_bol - 1);
1748 while (cur_bol >= start_bol);
1750 edit->force |= REDRAW_PAGE;
1753 /* --------------------------------------------------------------------------------------------- */
1755 * prints at the cursor
1756 * @returns the number of chars printed
1759 static size_t
1760 edit_print_string (WEdit * e, const char *s)
1762 size_t i = 0;
1764 while (s[i] != '\0')
1765 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1766 e->force |= REDRAW_COMPLETELY;
1767 edit_update_screen (e);
1768 return i;
1771 /* --------------------------------------------------------------------------------------------- */
1772 /*** public functions ****************************************************************************/
1773 /* --------------------------------------------------------------------------------------------- */
1775 /** User edit menu, like user menu (F2) but only in editor. */
1777 void
1778 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1780 char *block_file;
1781 int nomark;
1782 off_t curs;
1783 off_t start_mark, end_mark;
1784 struct stat status;
1785 vfs_path_t *block_file_vpath;
1787 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1788 block_file_vpath = vfs_path_from_str (block_file);
1789 curs = edit->curs1;
1790 nomark = eval_marks (edit, &start_mark, &end_mark);
1791 if (nomark == 0)
1792 edit_save_block (edit, block_file, start_mark, end_mark);
1794 /* run shell scripts from menu */
1795 if (user_menu_cmd (edit, menu_file, selected_entry)
1796 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1798 int rc = 0;
1799 FILE *fd;
1801 /* i.e. we have marked block */
1802 if (nomark == 0)
1803 rc = edit_block_delete_cmd (edit);
1805 if (rc == 0)
1807 off_t ins_len;
1809 ins_len = edit_insert_file (edit, block_file_vpath);
1810 if (nomark == 0 && ins_len > 0)
1811 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1813 /* truncate block file */
1814 fd = fopen (block_file, "w");
1815 if (fd != NULL)
1816 fclose (fd);
1818 g_free (block_file);
1819 vfs_path_free (block_file_vpath);
1821 edit_cursor_move (edit, curs - edit->curs1);
1822 edit->force |= REDRAW_PAGE;
1823 send_message ((Widget *) edit, WIDGET_DRAW, 0);
1826 /* --------------------------------------------------------------------------------------------- */
1829 edit_get_byte (WEdit * edit, off_t byte_index)
1831 off_t p;
1832 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1833 return '\n';
1835 if (byte_index >= edit->curs1)
1837 p = edit->curs1 + edit->curs2 - byte_index - 1;
1838 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1840 else
1842 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1846 /* --------------------------------------------------------------------------------------------- */
1849 edit_get_utf (WEdit * edit, off_t byte_index, int *char_width)
1851 gchar *str = NULL;
1852 int res = -1;
1853 gunichar ch;
1854 gchar *next_ch = NULL;
1855 int width = 0;
1856 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1858 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1860 *char_width = 0;
1861 return '\n';
1864 str = edit_get_byte_ptr (edit, byte_index);
1866 if (str == NULL)
1868 *char_width = 0;
1869 return 0;
1872 res = g_utf8_get_char_validated (str, -1);
1874 if (res < 0)
1876 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1877 int i;
1878 for (i = 0; i < UTF8_CHAR_LEN; i++)
1879 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1880 utf8_buf[UTF8_CHAR_LEN] = '\0';
1881 str = utf8_buf;
1882 res = g_utf8_get_char_validated (str, -1);
1885 if (res < 0)
1887 ch = *str;
1888 width = 0;
1890 else
1892 ch = res;
1893 /* Calculate UTF-8 char width */
1894 next_ch = g_utf8_next_char (str);
1895 if (next_ch)
1897 width = next_ch - str;
1899 else
1901 ch = 0;
1902 width = 0;
1905 *char_width = width;
1906 return ch;
1909 /* --------------------------------------------------------------------------------------------- */
1911 char *
1912 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1914 int i;
1915 char *p, *writename;
1916 const vfs_path_element_t *path_element;
1918 i = edit_find_filter (filename_vpath);
1919 if (i < 0)
1920 return NULL;
1922 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1923 writename = name_quote (path_element->path, 0);
1924 p = g_strdup_printf (all_filters[i].write, writename);
1925 g_free (writename);
1926 return p;
1929 /* --------------------------------------------------------------------------------------------- */
1931 * @param edit editor object
1932 * @param f value of stream file
1933 * @returns the length of the file
1936 off_t
1937 edit_write_stream (WEdit * edit, FILE * f)
1939 long i;
1941 if (edit->lb == LB_ASIS)
1943 for (i = 0; i < edit->last_byte; i++)
1944 if (fputc (edit_get_byte (edit, i), f) < 0)
1945 break;
1946 return i;
1949 /* change line breaks */
1950 for (i = 0; i < edit->last_byte; i++)
1952 unsigned char c = edit_get_byte (edit, i);
1954 if (!(c == '\n' || c == '\r'))
1956 /* not line break */
1957 if (fputc (c, f) < 0)
1958 return i;
1960 else
1961 { /* (c == '\n' || c == '\r') */
1962 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1964 switch (edit->lb)
1966 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1967 /* put one line break unconditionally */
1968 if (fputc ('\n', f) < 0)
1969 return i;
1971 i++; /* 2 chars are processed */
1973 if (c == '\r' && c1 == '\n')
1974 /* Windows line break; go to the next char */
1975 break;
1977 if (c == '\r' && c1 == '\r')
1979 /* two Macintosh line breaks; put second line break */
1980 if (fputc ('\n', f) < 0)
1981 return i;
1982 break;
1985 if (fputc (c1, f) < 0)
1986 return i;
1987 break;
1989 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1990 /* put one line break unconditionally */
1991 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1992 return i;
1994 if (c == '\r' && c1 == '\n')
1995 /* Windows line break; go to the next char */
1996 i++;
1997 break;
1999 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2000 /* put one line break unconditionally */
2001 if (fputc ('\r', f) < 0)
2002 return i;
2004 i++; /* 2 chars are processed */
2006 if (c == '\r' && c1 == '\n')
2007 /* Windows line break; go to the next char */
2008 break;
2010 if (c == '\n' && c1 == '\n')
2012 /* two Windows line breaks; put second line break */
2013 if (fputc ('\r', f) < 0)
2014 return i;
2015 break;
2018 if (fputc (c1, f) < 0)
2019 return i;
2020 break;
2021 case LB_ASIS: /* default without changes */
2022 break;
2027 return edit->last_byte;
2030 /* --------------------------------------------------------------------------------------------- */
2032 gboolean
2033 is_break_char (char c)
2035 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
2038 /* --------------------------------------------------------------------------------------------- */
2040 char *
2041 edit_get_word_from_pos (WEdit * edit, off_t start_pos, off_t * start, gsize * len, gsize * cut)
2043 off_t word_start;
2044 long cut_len = 0;
2045 GString *match_expr;
2046 unsigned char *bufpos;
2047 int c1, c2;
2049 for (word_start = start_pos; word_start != 0; word_start--, cut_len++)
2051 c1 = edit_get_byte (edit, word_start);
2052 c2 = edit_get_byte (edit, word_start - 1);
2054 if (is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n')
2055 break;
2058 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
2059 match_expr = g_string_sized_new (16);
2063 c1 = edit_get_byte (edit, word_start + match_expr->len);
2064 c2 = edit_get_byte (edit, word_start + match_expr->len + 1);
2065 g_string_append_c (match_expr, c1);
2067 while (!(is_break_char (c1) != is_break_char (c2) || c1 == '\n' || c2 == '\n'));
2069 *len = match_expr->len;
2070 *start = word_start;
2071 *cut = cut_len;
2073 return g_string_free (match_expr, FALSE);
2076 /* --------------------------------------------------------------------------------------------- */
2077 /** inserts a file at the cursor, returns count of inserted bytes on success */
2079 long
2080 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2082 char *p;
2083 off_t ins_len = 0;
2085 p = edit_get_filter (filename_vpath);
2086 if (p != NULL)
2088 FILE *f;
2089 off_t current = edit->curs1;
2091 f = (FILE *) popen (p, "r");
2092 if (f != NULL)
2094 edit_insert_stream (edit, f);
2095 ins_len = edit->curs1 - current;
2096 edit_cursor_move (edit, -ins_len);
2097 if (pclose (f) > 0)
2099 char *errmsg;
2101 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2102 edit_error_dialog (_("Error"), errmsg);
2103 g_free (errmsg);
2104 ins_len = -1;
2107 else
2109 char *errmsg;
2111 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2112 edit_error_dialog (_("Error"), errmsg);
2113 g_free (errmsg);
2114 ins_len = -1;
2116 g_free (p);
2118 else
2120 int file;
2121 off_t blocklen;
2122 off_t current = edit->curs1;
2123 int vertical_insertion = 0;
2124 char *buf;
2126 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2127 if (file == -1)
2128 return -1;
2130 buf = g_malloc0 (TEMP_BUF_LEN);
2131 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2132 if (blocklen > 0)
2134 /* if contain signature VERTICAL_MAGIC then it vertical block */
2135 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2136 vertical_insertion = 1;
2137 else
2138 mc_lseek (file, 0, SEEK_SET);
2141 if (vertical_insertion)
2143 off_t mark1, mark2;
2144 long c1, c2;
2146 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2147 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2148 /* highlight inserted text then not persistent blocks */
2149 if (!option_persistent_selections)
2151 if (!edit->column_highlight)
2152 edit_push_undo_action (edit, COLUMN_OFF);
2153 edit->column_highlight = 1;
2156 else
2158 off_t i;
2160 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2162 for (i = 0; i < blocklen; i++)
2163 edit_insert (edit, buf[i]);
2165 /* highlight inserted text then not persistent blocks */
2166 if (!option_persistent_selections && edit->modified)
2168 edit_set_markers (edit, edit->curs1, current, 0, 0);
2169 if (edit->column_highlight)
2170 edit_push_undo_action (edit, COLUMN_ON);
2171 edit->column_highlight = 0;
2175 edit->force |= REDRAW_PAGE;
2176 ins_len = edit->curs1 - current;
2177 edit_cursor_move (edit, -ins_len);
2178 g_free (buf);
2179 mc_close (file);
2180 if (blocklen != 0)
2181 ins_len = 0;
2184 return ins_len;
2187 /* --------------------------------------------------------------------------------------------- */
2189 * Fill in the edit structure. Return NULL on failure. Pass edit as
2190 * NULL to allocate a new structure.
2192 * If line is 0, try to restore saved position. Otherwise put the
2193 * cursor on that line and show it in the middle of the screen.
2196 WEdit *
2197 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2198 long line)
2200 gboolean to_free = FALSE;
2202 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2203 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2205 if (edit != NULL)
2206 edit_purge_widget (edit);
2207 else
2209 #ifdef ENABLE_NLS
2211 * Expand option_whole_chars_search by national letters using
2212 * current locale
2215 static char option_whole_chars_search_buf[256];
2217 if (option_whole_chars_search_buf != option_whole_chars_search)
2219 size_t i;
2220 size_t len = str_term_width1 (option_whole_chars_search);
2222 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2224 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2226 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2228 option_whole_chars_search_buf[len++] = i;
2232 option_whole_chars_search_buf[len] = 0;
2233 option_whole_chars_search = option_whole_chars_search_buf;
2235 #endif /* ENABLE_NLS */
2236 edit = g_malloc0 (sizeof (WEdit));
2237 to_free = TRUE;
2240 edit->drag_state = MCEDIT_DRAG_NORMAL;
2241 edit->widget.y = y;
2242 edit->widget.x = x;
2243 edit->widget.lines = lines;
2244 edit->widget.cols = cols;
2245 edit_save_size (edit);
2246 edit->fullscreen = TRUE;
2248 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2249 edit->stat1.st_uid = getuid ();
2250 edit->stat1.st_gid = getgid ();
2251 edit->stat1.st_mtime = 0;
2253 edit->over_col = 0;
2254 edit->bracket = -1;
2255 edit->force |= REDRAW_PAGE;
2257 /* set file name before load file */
2258 edit_set_filename (edit, filename_vpath);
2260 edit->undo_stack_size = START_STACK_SIZE;
2261 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2262 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2264 edit->redo_stack_size = START_STACK_SIZE;
2265 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2266 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2268 edit->utf8 = 0;
2269 edit->converter = str_cnv_from_term;
2270 edit_set_codeset (edit);
2272 if (!edit_load_file (edit))
2274 /* edit_load_file already gives an error message */
2275 if (to_free)
2276 g_free (edit);
2277 return NULL;
2280 edit->loading_done = 1;
2281 edit->modified = 0;
2282 edit->locked = 0;
2283 edit_load_syntax (edit, NULL, NULL);
2285 int color;
2286 edit_get_syntax_color (edit, -1, &color);
2289 /* load saved cursor position */
2290 if ((line == 0) && option_save_position)
2291 edit_load_position (edit);
2292 else
2294 if (line <= 0)
2295 line = 1;
2296 edit_move_display (edit, line - 1);
2297 edit_move_to_line (edit, line - 1);
2300 edit_load_macro_cmd (edit);
2302 return edit;
2305 /* --------------------------------------------------------------------------------------------- */
2307 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2308 gboolean
2309 edit_clean (WEdit * edit)
2311 int j = 0;
2313 if (edit == NULL)
2314 return FALSE;
2316 /* a stale lock, remove it */
2317 if (edit->locked)
2318 edit->locked = unlock_file (edit->filename_vpath);
2320 /* save cursor position */
2321 if (option_save_position)
2322 edit_save_position (edit);
2323 else if (edit->serialized_bookmarks != NULL)
2324 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2326 /* File specified on the mcedit command line and never saved */
2327 if (edit->delete_file)
2328 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2330 edit_free_syntax_rules (edit);
2331 book_mark_flush (edit, -1);
2332 for (; j <= MAXBUFF; j++)
2334 g_free (edit->buffers1[j]);
2335 g_free (edit->buffers2[j]);
2338 g_free (edit->undo_stack);
2339 g_free (edit->redo_stack);
2340 vfs_path_free (edit->filename_vpath);
2341 vfs_path_free (edit->dir_vpath);
2342 mc_search_free (edit->search);
2343 edit->search = NULL;
2345 if (edit->converter != str_cnv_from_term)
2346 str_close_conv (edit->converter);
2348 edit_purge_widget (edit);
2350 return TRUE;
2353 /* --------------------------------------------------------------------------------------------- */
2356 * Load a new file into the editor and set line. If it fails, preserve the old file.
2357 * To do it, allocate a new widget, initialize it and, if the new file
2358 * was loaded, copy the data to the old widget.
2360 * @returns TRUE on success, FALSE on failure.
2362 gboolean
2363 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2365 WEdit *e;
2366 int y = edit->widget.y;
2367 int x = edit->widget.x;
2368 int lines = edit->widget.lines;
2369 int columns = edit->widget.cols;
2371 e = g_malloc0 (sizeof (WEdit));
2372 e->widget = edit->widget;
2374 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2376 g_free (e);
2377 return FALSE;
2380 edit_clean (edit);
2381 memcpy (edit, e, sizeof (WEdit));
2382 g_free (e);
2384 return TRUE;
2387 /* --------------------------------------------------------------------------------------------- */
2389 void
2390 edit_set_codeset (WEdit * edit)
2392 #ifdef HAVE_CHARSET
2393 const char *cp_id;
2395 cp_id =
2396 get_codepage_id (mc_global.source_codepage >=
2397 0 ? mc_global.source_codepage : mc_global.display_codepage);
2399 if (cp_id != NULL)
2401 GIConv conv;
2402 conv = str_crt_conv_from (cp_id);
2403 if (conv != INVALID_CONV)
2405 if (edit->converter != str_cnv_from_term)
2406 str_close_conv (edit->converter);
2407 edit->converter = conv;
2411 if (cp_id != NULL)
2412 edit->utf8 = str_isutf8 (cp_id);
2413 #else
2414 (void) edit;
2415 #endif
2419 /* --------------------------------------------------------------------------------------------- */
2421 Recording stack for undo:
2422 The following is an implementation of a compressed stack. Identical
2423 pushes are recorded by a negative prefix indicating the number of times the
2424 same char was pushed. This saves space for repeated curs-left or curs-right
2425 delete etc.
2429 pushed: stored:
2433 b -3
2435 c --> -4
2441 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2442 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2443 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2444 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2445 position.
2447 The only way the cursor moves or the buffer is changed is through the routines:
2448 insert, backspace, insert_ahead, delete, and cursor_move.
2449 These record the reverse undo movements onto the stack each time they are
2450 called.
2452 Each key press results in a set of actions (insert; delete ...). So each time
2453 a key is pressed the current position of start_display is pushed as
2454 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2455 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2456 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2460 void
2461 edit_push_undo_action (WEdit * edit, long c, ...)
2463 unsigned long sp = edit->undo_stack_pointer;
2464 unsigned long spm1;
2465 long *t;
2467 /* first enlarge the stack if necessary */
2468 if (sp > edit->undo_stack_size - 10)
2469 { /* say */
2470 if (option_max_undo < 256)
2471 option_max_undo = 256;
2472 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2474 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2475 if (t)
2477 edit->undo_stack = t;
2478 edit->undo_stack_size <<= 1;
2479 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2483 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2484 if (edit->undo_stack_disable)
2486 edit_push_redo_action (edit, KEY_PRESS);
2487 edit_push_redo_action (edit, c);
2488 return;
2490 else if (edit->redo_stack_reset)
2492 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2495 if (edit->undo_stack_bottom != sp
2496 && spm1 != edit->undo_stack_bottom
2497 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2499 int d;
2500 if (edit->undo_stack[spm1] < 0)
2502 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2503 if (d == c)
2505 if (edit->undo_stack[spm1] > -1000000000)
2507 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2509 edit->undo_stack[spm1]--;
2511 return;
2515 else
2517 d = edit->undo_stack[spm1];
2518 if (d == c)
2520 if (c >= KEY_PRESS)
2521 return; /* --> no need to push multiple do-nothings */
2522 edit->undo_stack[sp] = -2;
2523 goto check_bottom;
2527 edit->undo_stack[sp] = c;
2529 check_bottom:
2530 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2532 /* if the sp wraps round and catches the undo_stack_bottom then erase
2533 * the first set of actions on the stack to make space - by moving
2534 * undo_stack_bottom forward one "key press" */
2535 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2536 if ((unsigned long) c == edit->undo_stack_bottom ||
2537 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2540 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2542 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2543 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2545 /*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: */
2546 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2547 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2549 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2553 void
2554 edit_push_redo_action (WEdit * edit, long c, ...)
2556 unsigned long sp = edit->redo_stack_pointer;
2557 unsigned long spm1;
2558 long *t;
2559 /* first enlarge the stack if necessary */
2560 if (sp > edit->redo_stack_size - 10)
2561 { /* say */
2562 if (option_max_undo < 256)
2563 option_max_undo = 256;
2564 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2566 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2567 if (t)
2569 edit->redo_stack = t;
2570 edit->redo_stack_size <<= 1;
2571 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2575 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2577 if (edit->redo_stack_bottom != sp
2578 && spm1 != edit->redo_stack_bottom
2579 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2581 int d;
2582 if (edit->redo_stack[spm1] < 0)
2584 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2585 if (d == c)
2587 if (edit->redo_stack[spm1] > -1000000000)
2589 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2590 edit->redo_stack[spm1]--;
2591 return;
2595 else
2597 d = edit->redo_stack[spm1];
2598 if (d == c)
2600 if (c >= KEY_PRESS)
2601 return; /* --> no need to push multiple do-nothings */
2602 edit->redo_stack[sp] = -2;
2603 goto redo_check_bottom;
2607 edit->redo_stack[sp] = c;
2609 redo_check_bottom:
2610 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2612 /* if the sp wraps round and catches the redo_stack_bottom then erase
2613 * the first set of actions on the stack to make space - by moving
2614 * redo_stack_bottom forward one "key press" */
2615 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2616 if ((unsigned long) c == edit->redo_stack_bottom ||
2617 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2620 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2622 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2623 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2626 * If a single key produced enough pushes to wrap all the way round then
2627 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2628 * The stack is then initialised:
2631 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2632 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2633 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2637 /* --------------------------------------------------------------------------------------------- */
2639 Basic low level single character buffer alterations and movements at the cursor.
2640 Returns char passed over, inserted or removed.
2643 void
2644 edit_insert (WEdit * edit, int c)
2646 /* check if file has grown to large */
2647 if (edit->last_byte >= SIZE_LIMIT)
2648 return;
2650 /* first we must update the position of the display window */
2651 if (edit->curs1 < edit->start_display)
2653 edit->start_display++;
2654 if (c == '\n')
2655 edit->start_line++;
2658 /* Mark file as modified, unless the file hasn't been fully loaded */
2659 if (edit->loading_done)
2661 edit_modification (edit);
2664 /* now we must update some info on the file and check if a redraw is required */
2665 if (c == '\n')
2667 if (edit->book_mark)
2668 book_mark_inc (edit, edit->curs_line);
2669 edit->curs_line++;
2670 edit->total_lines++;
2671 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2674 /* save the reverse command onto the undo stack */
2675 /* ordinary char and not space */
2676 if (c > 32)
2677 edit_push_undo_action (edit, BACKSPACE);
2678 else
2679 edit_push_undo_action (edit, BACKSPACE_BR);
2680 /* update markers */
2681 edit->mark1 += (edit->mark1 > edit->curs1);
2682 edit->mark2 += (edit->mark2 > edit->curs1);
2683 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2685 /* add a new buffer if we've reached the end of the last one */
2686 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2687 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2689 /* perform the insertion */
2690 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2691 = (unsigned char) c;
2693 /* update file length */
2694 edit->last_byte++;
2696 /* update cursor position */
2697 edit->curs1++;
2700 /* --------------------------------------------------------------------------------------------- */
2701 /** same as edit_insert and move left */
2703 void
2704 edit_insert_ahead (WEdit * edit, int c)
2706 if (edit->last_byte >= SIZE_LIMIT)
2707 return;
2709 if (edit->curs1 < edit->start_display)
2711 edit->start_display++;
2712 if (c == '\n')
2713 edit->start_line++;
2715 edit_modification (edit);
2716 if (c == '\n')
2718 if (edit->book_mark)
2719 book_mark_inc (edit, edit->curs_line);
2720 edit->total_lines++;
2721 edit->force |= REDRAW_AFTER_CURSOR;
2723 /* ordinary char and not space */
2724 if (c > 32)
2725 edit_push_undo_action (edit, DELCHAR);
2726 else
2727 edit_push_undo_action (edit, DELCHAR_BR);
2729 edit->mark1 += (edit->mark1 >= edit->curs1);
2730 edit->mark2 += (edit->mark2 >= edit->curs1);
2731 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2733 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2734 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2735 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2736 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2738 edit->last_byte++;
2739 edit->curs2++;
2743 /* --------------------------------------------------------------------------------------------- */
2746 edit_delete (WEdit * edit, const int byte_delete)
2748 int p = 0;
2749 int cw = 1;
2750 int i;
2752 if (!edit->curs2)
2753 return 0;
2755 cw = 1;
2756 /* if byte_delete = 1 then delete only one byte not multibyte char */
2757 if (edit->utf8 && byte_delete == 0)
2759 edit_get_utf (edit, edit->curs1, &cw);
2760 if (cw < 1)
2761 cw = 1;
2764 if (edit->mark2 != edit->mark1)
2765 edit_push_markers (edit);
2767 for (i = 1; i <= cw; i++)
2769 if (edit->mark1 > edit->curs1)
2771 edit->mark1--;
2772 edit->end_mark_curs--;
2774 if (edit->mark2 > edit->curs1)
2775 edit->mark2--;
2776 if (edit->last_get_rule > edit->curs1)
2777 edit->last_get_rule--;
2779 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2780 ((edit->curs2 -
2781 1) & M_EDIT_BUF_SIZE) - 1];
2783 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2785 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2786 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2788 edit->last_byte--;
2789 edit->curs2--;
2790 edit_push_undo_action (edit, p + 256);
2793 edit_modification (edit);
2794 if (p == '\n')
2796 if (edit->book_mark)
2797 book_mark_dec (edit, edit->curs_line);
2798 edit->total_lines--;
2799 edit->force |= REDRAW_AFTER_CURSOR;
2801 if (edit->curs1 < edit->start_display)
2803 edit->start_display--;
2804 if (p == '\n')
2805 edit->start_line--;
2808 return p;
2811 /* --------------------------------------------------------------------------------------------- */
2814 edit_backspace (WEdit * edit, const int byte_delete)
2816 int p = 0;
2817 int cw = 1;
2818 int i;
2820 if (edit->curs1 == 0)
2821 return 0;
2823 cw = 1;
2825 if (edit->mark2 != edit->mark1)
2826 edit_push_markers (edit);
2828 if (edit->utf8 && byte_delete == 0)
2830 edit_get_prev_utf (edit, edit->curs1, &cw);
2831 if (cw < 1)
2832 cw = 1;
2834 for (i = 1; i <= cw; i++)
2836 if (edit->mark1 >= edit->curs1)
2838 edit->mark1--;
2839 edit->end_mark_curs--;
2841 if (edit->mark2 >= edit->curs1)
2842 edit->mark2--;
2843 if (edit->last_get_rule >= edit->curs1)
2844 edit->last_get_rule--;
2846 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
2847 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
2848 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2850 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2851 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2853 edit->last_byte--;
2854 edit->curs1--;
2855 edit_push_undo_action (edit, p);
2857 edit_modification (edit);
2858 if (p == '\n')
2860 if (edit->book_mark)
2861 book_mark_dec (edit, edit->curs_line);
2862 edit->curs_line--;
2863 edit->total_lines--;
2864 edit->force |= REDRAW_AFTER_CURSOR;
2867 if (edit->curs1 < edit->start_display)
2869 edit->start_display--;
2870 if (p == '\n')
2871 edit->start_line--;
2874 return p;
2877 /* --------------------------------------------------------------------------------------------- */
2878 /** moves the cursor right or left: increment positive or negative respectively */
2880 void
2881 edit_cursor_move (WEdit * edit, off_t increment)
2883 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2884 int c;
2886 if (increment < 0)
2888 for (; increment < 0; increment++)
2890 if (edit->curs1 == 0)
2891 return;
2893 edit_push_undo_action (edit, CURS_RIGHT);
2895 c = edit_get_byte (edit, edit->curs1 - 1);
2896 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2897 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2898 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2899 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2900 edit->curs2++;
2901 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2902 1) & M_EDIT_BUF_SIZE];
2903 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2905 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2906 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2908 edit->curs1--;
2909 if (c == '\n')
2911 edit->curs_line--;
2912 edit->force |= REDRAW_LINE_BELOW;
2917 else if (increment > 0)
2919 for (; increment > 0; increment--)
2921 if (edit->curs2 == 0)
2922 return;
2924 edit_push_undo_action (edit, CURS_LEFT);
2926 c = edit_get_byte (edit, edit->curs1);
2927 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2928 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2929 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2930 edit->curs1++;
2931 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2932 ((edit->curs2 -
2933 1) & M_EDIT_BUF_SIZE) - 1];
2934 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2936 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2937 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2939 edit->curs2--;
2940 if (c == '\n')
2942 edit->curs_line++;
2943 edit->force |= REDRAW_LINE_ABOVE;
2949 /* These functions return positions relative to lines */
2951 /* --------------------------------------------------------------------------------------------- */
2952 /** returns index of last char on line + 1 */
2954 off_t
2955 edit_eol (WEdit * edit, off_t current)
2957 if (current >= edit->last_byte)
2958 return edit->last_byte;
2960 for (;; current++)
2961 if (edit_get_byte (edit, current) == '\n')
2962 break;
2963 return current;
2966 /* --------------------------------------------------------------------------------------------- */
2967 /** returns index of first char on line */
2969 off_t
2970 edit_bol (WEdit * edit, off_t current)
2972 if (current <= 0)
2973 return 0;
2975 for (;; current--)
2976 if (edit_get_byte (edit, current - 1) == '\n')
2977 break;
2978 return current;
2981 /* --------------------------------------------------------------------------------------------- */
2983 long
2984 edit_count_lines (WEdit * edit, off_t current, off_t upto)
2986 long lines = 0;
2987 if (upto > edit->last_byte)
2988 upto = edit->last_byte;
2989 if (current < 0)
2990 current = 0;
2991 while (current < upto)
2992 if (edit_get_byte (edit, current++) == '\n')
2993 lines++;
2994 return lines;
2997 /* --------------------------------------------------------------------------------------------- */
2998 /* If lines is zero this returns the count of lines from current to upto. */
2999 /* If upto is zero returns index of lines forward current. */
3001 off_t
3002 edit_move_forward (WEdit * edit, off_t current, long lines, off_t upto)
3004 if (upto != 0)
3006 return (off_t) edit_count_lines (edit, current, upto);
3008 else
3010 long next;
3011 if (lines < 0)
3012 lines = 0;
3013 while (lines--)
3015 next = edit_eol (edit, current) + 1;
3016 if (next > edit->last_byte)
3017 break;
3018 else
3019 current = next;
3021 return current;
3025 /* --------------------------------------------------------------------------------------------- */
3026 /** Returns offset of 'lines' lines up from current */
3028 off_t
3029 edit_move_backward (WEdit * edit, off_t current, long lines)
3031 if (lines < 0)
3032 lines = 0;
3033 current = edit_bol (edit, current);
3034 while ((lines--) && current != 0)
3035 current = edit_bol (edit, current - 1);
3036 return current;
3039 /* --------------------------------------------------------------------------------------------- */
3040 /* If cols is zero this returns the count of columns from current to upto. */
3041 /* If upto is zero returns index of cols across from current. */
3043 off_t
3044 edit_move_forward3 (WEdit * edit, off_t current, long cols, off_t upto)
3046 off_t p, q;
3047 long col;
3049 if (upto != 0)
3051 q = upto;
3052 cols = -10;
3054 else
3055 q = edit->last_byte + 2;
3057 for (col = 0, p = current; p < q; p++)
3059 int c, orig_c;
3061 if (cols != -10)
3063 if (col == cols)
3064 return p;
3065 if (col > cols)
3066 return p - 1;
3069 orig_c = c = edit_get_byte (edit, p);
3071 #ifdef HAVE_CHARSET
3072 if (edit->utf8)
3074 int utf_ch;
3075 int cw = 1;
3077 utf_ch = edit_get_utf (edit, p, &cw);
3078 if (mc_global.utf8_display)
3080 if (cw > 1)
3081 col -= cw - 1;
3082 if (g_unichar_iswide (utf_ch))
3083 col++;
3085 else if (cw > 1 && g_unichar_isprint (utf_ch))
3086 col -= cw - 1;
3089 c = convert_to_display_c (c);
3090 #endif
3092 if (c == '\t')
3093 col += TAB_SIZE - col % TAB_SIZE;
3094 else if (c == '\n')
3096 if (upto != 0)
3097 return (off_t) col;
3098 else
3099 return p;
3101 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3102 /* '\r' is shown as ^M, so we must advance 2 characters */
3103 /* Caret notation for control characters */
3104 col += 2;
3105 else
3106 col++;
3108 return (off_t) col;
3111 /* --------------------------------------------------------------------------------------------- */
3112 /** returns the current column position of the cursor */
3114 long
3115 edit_get_col (WEdit * edit)
3117 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3120 /* --------------------------------------------------------------------------------------------- */
3121 /* Scrolling functions */
3122 /* --------------------------------------------------------------------------------------------- */
3124 void
3125 edit_update_curs_row (WEdit * edit)
3127 edit->curs_row = edit->curs_line - edit->start_line;
3130 /* --------------------------------------------------------------------------------------------- */
3132 void
3133 edit_update_curs_col (WEdit * edit)
3135 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3138 /* --------------------------------------------------------------------------------------------- */
3140 long
3141 edit_get_curs_col (const WEdit * edit)
3143 return edit->curs_col;
3146 /* --------------------------------------------------------------------------------------------- */
3147 /** moves the display start position up by i lines */
3149 void
3150 edit_scroll_upward (WEdit * edit, long i)
3152 long lines_above = edit->start_line;
3154 if (i > lines_above)
3155 i = lines_above;
3156 if (i != 0)
3158 edit->start_line -= i;
3159 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3160 edit->force |= REDRAW_PAGE;
3161 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3163 edit_update_curs_row (edit);
3167 /* --------------------------------------------------------------------------------------------- */
3169 void
3170 edit_scroll_downward (WEdit * edit, long i)
3172 long lines_below;
3174 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3175 if (lines_below > 0)
3177 if (i > lines_below)
3178 i = lines_below;
3179 edit->start_line += i;
3180 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3181 edit->force |= REDRAW_PAGE;
3182 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3184 edit_update_curs_row (edit);
3187 /* --------------------------------------------------------------------------------------------- */
3189 void
3190 edit_scroll_right (WEdit * edit, long i)
3192 edit->force |= REDRAW_PAGE;
3193 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3194 edit->start_col -= i;
3197 /* --------------------------------------------------------------------------------------------- */
3199 void
3200 edit_scroll_left (WEdit * edit, long i)
3202 if (edit->start_col)
3204 edit->start_col += i;
3205 if (edit->start_col > 0)
3206 edit->start_col = 0;
3207 edit->force |= REDRAW_PAGE;
3208 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3212 /* --------------------------------------------------------------------------------------------- */
3213 /* high level cursor movement commands */
3214 /* --------------------------------------------------------------------------------------------- */
3216 void
3217 edit_move_to_prev_col (WEdit * edit, off_t p)
3219 long prev = edit->prev_col;
3220 long over = edit->over_col;
3222 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3224 if (option_cursor_beyond_eol)
3226 long line_len;
3228 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3229 edit_eol (edit, edit->curs1));
3230 if (line_len < prev + edit->over_col)
3232 edit->over_col = prev + over - line_len;
3233 edit->prev_col = line_len;
3234 edit->curs_col = line_len;
3236 else
3238 edit->curs_col = prev + over;
3239 edit->prev_col = edit->curs_col;
3240 edit->over_col = 0;
3243 else
3245 edit->over_col = 0;
3246 if (is_in_indent (edit) && option_fake_half_tabs)
3248 edit_update_curs_col (edit);
3249 if (space_width)
3251 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3253 int q;
3255 q = edit->curs_col;
3256 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3257 p = edit_bol (edit, edit->curs1);
3259 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0)
3260 - edit->curs1);
3262 if (!left_of_four_spaces (edit))
3263 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3270 /* --------------------------------------------------------------------------------------------- */
3271 /** check whether line in editor is blank or not
3273 * @param edit editor object
3274 * @param line number of line
3276 * @return TRUE if line in blank, FALSE otherwise
3279 gboolean
3280 line_is_blank (WEdit * edit, long line)
3282 return is_blank (edit, edit_find_line (edit, line));
3285 /* --------------------------------------------------------------------------------------------- */
3286 /** move cursor to line 'line' */
3288 void
3289 edit_move_to_line (WEdit * e, long line)
3291 if (line < e->curs_line)
3292 edit_move_up (e, e->curs_line - line, 0);
3293 else
3294 edit_move_down (e, line - e->curs_line, 0);
3295 edit_scroll_screen_over_cursor (e);
3298 /* --------------------------------------------------------------------------------------------- */
3299 /** scroll window so that first visible line is 'line' */
3301 void
3302 edit_move_display (WEdit * e, long line)
3304 if (line < e->start_line)
3305 edit_scroll_upward (e, e->start_line - line);
3306 else
3307 edit_scroll_downward (e, line - e->start_line);
3310 /* --------------------------------------------------------------------------------------------- */
3311 /** save markers onto undo stack */
3313 void
3314 edit_push_markers (WEdit * edit)
3316 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3317 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3318 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3321 /* --------------------------------------------------------------------------------------------- */
3323 void
3324 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3326 edit->mark1 = m1;
3327 edit->mark2 = m2;
3328 edit->column1 = c1;
3329 edit->column2 = c2;
3333 /* --------------------------------------------------------------------------------------------- */
3334 /** highlight marker toggle */
3336 void
3337 edit_mark_cmd (WEdit * edit, gboolean unmark)
3339 edit_push_markers (edit);
3340 if (unmark)
3342 edit_set_markers (edit, 0, 0, 0, 0);
3343 edit->force |= REDRAW_PAGE;
3345 else
3347 if (edit->mark2 >= 0)
3349 edit->end_mark_curs = -1;
3350 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3351 edit->curs_col + edit->over_col);
3352 edit->force |= REDRAW_PAGE;
3354 else
3356 edit->end_mark_curs = edit->curs1;
3357 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3358 edit->curs_col + edit->over_col);
3363 /* --------------------------------------------------------------------------------------------- */
3364 /** highlight the word under cursor */
3366 void
3367 edit_mark_current_word_cmd (WEdit * edit)
3369 long pos;
3371 for (pos = edit->curs1; pos != 0; pos--)
3373 int c1, c2;
3375 c1 = edit_get_byte (edit, pos);
3376 c2 = edit_get_byte (edit, pos - 1);
3377 if (!isspace (c1) && isspace (c2))
3378 break;
3379 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3380 break;
3382 edit->mark1 = pos;
3384 for (; pos < edit->last_byte; pos++)
3386 int c1, c2;
3388 c1 = edit_get_byte (edit, pos);
3389 c2 = edit_get_byte (edit, pos + 1);
3390 if (!isspace (c1) && isspace (c2))
3391 break;
3392 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3393 break;
3395 edit->mark2 = min (pos + 1, edit->last_byte);
3397 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3400 /* --------------------------------------------------------------------------------------------- */
3402 void
3403 edit_mark_current_line_cmd (WEdit * edit)
3405 long pos = edit->curs1;
3407 edit->mark1 = edit_bol (edit, pos);
3408 edit->mark2 = edit_eol (edit, pos);
3410 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3413 /* --------------------------------------------------------------------------------------------- */
3415 void
3416 edit_delete_line (WEdit * edit)
3419 * Delete right part of the line.
3420 * Note that edit_get_byte() returns '\n' when byte position is
3421 * beyond EOF.
3423 while (edit_get_byte (edit, edit->curs1) != '\n')
3425 (void) edit_delete (edit, 1);
3429 * Delete '\n' char.
3430 * Note that edit_delete() will not corrupt anything if called while
3431 * cursor position is EOF.
3433 (void) edit_delete (edit, 1);
3436 * Delete left part of the line.
3437 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3439 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3441 (void) edit_backspace (edit, 1);
3445 /* --------------------------------------------------------------------------------------------- */
3447 long
3448 edit_indent_width (WEdit * edit, off_t p)
3450 off_t q = p;
3452 /* move to the end of the leading whitespace of the line */
3453 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3454 q++;
3455 /* count the number of columns of indentation */
3456 return (long) edit_move_forward3 (edit, p, 0, q);
3459 /* --------------------------------------------------------------------------------------------- */
3461 void
3462 edit_insert_indent (WEdit * edit, int indent)
3464 if (!option_fill_tabs_with_spaces)
3466 while (indent >= TAB_SIZE)
3468 edit_insert (edit, '\t');
3469 indent -= TAB_SIZE;
3472 while (indent-- > 0)
3473 edit_insert (edit, ' ');
3476 /* --------------------------------------------------------------------------------------------- */
3478 void
3479 edit_push_key_press (WEdit * edit)
3481 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3482 if (edit->mark2 == -1)
3484 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3485 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3489 /* --------------------------------------------------------------------------------------------- */
3491 void
3492 edit_find_bracket (WEdit * edit)
3494 edit->bracket = edit_get_bracket (edit, 1, 10000);
3495 if (last_bracket != edit->bracket)
3496 edit->force |= REDRAW_PAGE;
3497 last_bracket = edit->bracket;
3500 /* --------------------------------------------------------------------------------------------- */
3502 * This executes a command as though the user initiated it through a key
3503 * press. Callback with WIDGET_KEY as a message calls this after
3504 * translating the key press. This function can be used to pass any
3505 * command to the editor. Note that the screen wouldn't update
3506 * automatically. Either of command or char_for_insertion must be
3507 * passed as -1. Commands are executed, and char_for_insertion is
3508 * inserted at the cursor.
3511 void
3512 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3514 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3515 || (macro_index < 0
3516 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3518 macro_index = 0;
3519 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3520 return;
3522 if (macro_index != -1)
3524 edit->force |= REDRAW_COMPLETELY;
3525 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3527 edit_store_macro_cmd (edit);
3528 macro_index = -1;
3529 return;
3531 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3533 edit_repeat_macro_cmd (edit);
3534 macro_index = -1;
3535 return;
3539 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3541 record_macro_buf[macro_index].action = command;
3542 record_macro_buf[macro_index++].ch = char_for_insertion;
3544 /* record the beginning of a set of editing actions initiated by a key press */
3545 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3546 edit_push_key_press (edit);
3548 edit_execute_cmd (edit, command, char_for_insertion);
3549 if (edit->column_highlight)
3550 edit->force |= REDRAW_PAGE;
3553 /* --------------------------------------------------------------------------------------------- */
3555 This executes a command at a lower level than macro recording.
3556 It also does not push a key_press onto the undo stack. This means
3557 that if it is called many times, a single undo command will undo
3558 all of them. It also does not check for the Undo command.
3560 void
3561 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3563 if (command == CK_WindowFullscreen)
3565 edit_toggle_fullscreen (edit);
3566 return;
3569 /* handle window state */
3570 if (edit_handle_move_resize (edit, command))
3571 return;
3573 edit->force |= REDRAW_LINE;
3575 /* The next key press will unhighlight the found string, so update
3576 * the whole page */
3577 if (edit->found_len || edit->column_highlight)
3578 edit->force |= REDRAW_PAGE;
3580 switch (command)
3582 /* a mark command with shift-arrow */
3583 case CK_MarkLeft:
3584 case CK_MarkRight:
3585 case CK_MarkToWordBegin:
3586 case CK_MarkToWordEnd:
3587 case CK_MarkToHome:
3588 case CK_MarkToEnd:
3589 case CK_MarkUp:
3590 case CK_MarkDown:
3591 case CK_MarkPageUp:
3592 case CK_MarkPageDown:
3593 case CK_MarkToFileBegin:
3594 case CK_MarkToFileEnd:
3595 case CK_MarkToPageBegin:
3596 case CK_MarkToPageEnd:
3597 case CK_MarkScrollUp:
3598 case CK_MarkScrollDown:
3599 case CK_MarkParagraphUp:
3600 case CK_MarkParagraphDown:
3601 /* a mark command with alt-arrow */
3602 case CK_MarkColumnPageUp:
3603 case CK_MarkColumnPageDown:
3604 case CK_MarkColumnLeft:
3605 case CK_MarkColumnRight:
3606 case CK_MarkColumnUp:
3607 case CK_MarkColumnDown:
3608 case CK_MarkColumnScrollUp:
3609 case CK_MarkColumnScrollDown:
3610 case CK_MarkColumnParagraphUp:
3611 case CK_MarkColumnParagraphDown:
3612 edit->column_highlight = 0;
3613 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3615 edit_mark_cmd (edit, TRUE); /* clear */
3616 edit_mark_cmd (edit, FALSE); /* marking on */
3618 edit->highlight = 1;
3619 break;
3621 /* any other command */
3622 default:
3623 if (edit->highlight)
3624 edit_mark_cmd (edit, FALSE); /* clear */
3625 edit->highlight = 0;
3628 /* first check for undo */
3629 if (command == CK_Undo)
3631 edit->redo_stack_reset = 0;
3632 edit_group_undo (edit);
3633 edit->found_len = 0;
3634 edit->prev_col = edit_get_col (edit);
3635 edit->search_start = edit->curs1;
3636 return;
3638 /* check for redo */
3639 if (command == CK_Redo)
3641 edit->redo_stack_reset = 0;
3642 edit_do_redo (edit);
3643 edit->found_len = 0;
3644 edit->prev_col = edit_get_col (edit);
3645 edit->search_start = edit->curs1;
3646 return;
3649 edit->redo_stack_reset = 1;
3651 /* An ordinary key press */
3652 if (char_for_insertion >= 0)
3654 /* if non persistent selection and text selected */
3655 if (!option_persistent_selections)
3657 if (edit->mark1 != edit->mark2)
3658 edit_block_delete_cmd (edit);
3660 if (edit->overwrite)
3662 /* remove char only one time, after input first byte, multibyte chars */
3663 if ((!mc_global.utf8_display || edit->charpoint == 0)
3664 && edit_get_byte (edit, edit->curs1) != '\n')
3665 edit_delete (edit, 0);
3667 if (option_cursor_beyond_eol && edit->over_col > 0)
3668 edit_insert_over (edit);
3669 #ifdef HAVE_CHARSET
3670 if (char_for_insertion > 255 && !mc_global.utf8_display)
3672 unsigned char str[6 + 1];
3673 size_t i = 0;
3674 int res;
3676 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3677 if (res == 0)
3679 str[0] = '.';
3680 str[1] = '\0';
3682 else
3684 str[res] = '\0';
3686 while (str[i] != 0 && i <= 6)
3688 char_for_insertion = str[i];
3689 edit_insert (edit, char_for_insertion);
3690 i++;
3693 else
3694 #endif
3695 edit_insert (edit, char_for_insertion);
3697 if (option_auto_para_formatting)
3699 format_paragraph (edit, 0);
3700 edit->force |= REDRAW_PAGE;
3702 else
3703 check_and_wrap_line (edit);
3704 edit->found_len = 0;
3705 edit->prev_col = edit_get_col (edit);
3706 edit->search_start = edit->curs1;
3707 edit_find_bracket (edit);
3708 return;
3711 switch (command)
3713 case CK_TopOnScreen:
3714 case CK_BottomOnScreen:
3715 case CK_Top:
3716 case CK_Bottom:
3717 case CK_PageUp:
3718 case CK_PageDown:
3719 case CK_Home:
3720 case CK_End:
3721 case CK_Up:
3722 case CK_Down:
3723 case CK_Left:
3724 case CK_Right:
3725 case CK_WordLeft:
3726 case CK_WordRight:
3727 if (edit->mark2 >= 0)
3729 if (!option_persistent_selections)
3731 if (edit->column_highlight)
3732 edit_push_undo_action (edit, COLUMN_ON);
3733 edit->column_highlight = 0;
3734 edit_mark_cmd (edit, TRUE);
3739 switch (command)
3741 case CK_TopOnScreen:
3742 case CK_BottomOnScreen:
3743 case CK_MarkToPageBegin:
3744 case CK_MarkToPageEnd:
3745 case CK_Up:
3746 case CK_Down:
3747 case CK_WordLeft:
3748 case CK_WordRight:
3749 case CK_MarkToWordBegin:
3750 case CK_MarkToWordEnd:
3751 case CK_MarkUp:
3752 case CK_MarkDown:
3753 case CK_MarkColumnUp:
3754 case CK_MarkColumnDown:
3755 if (edit->mark2 == -1)
3756 break; /*marking is following the cursor: may need to highlight a whole line */
3757 case CK_Left:
3758 case CK_Right:
3759 case CK_MarkLeft:
3760 case CK_MarkRight:
3761 edit->force |= REDRAW_CHAR_ONLY;
3764 /* basic cursor key commands */
3765 switch (command)
3767 case CK_BackSpace:
3768 /* if non persistent selection and text selected */
3769 if (!option_persistent_selections)
3771 if (edit->mark1 != edit->mark2)
3773 edit_block_delete_cmd (edit);
3774 break;
3777 if (option_cursor_beyond_eol && edit->over_col > 0)
3779 edit->over_col--;
3780 break;
3782 if (option_backspace_through_tabs && is_in_indent (edit))
3784 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3785 edit_backspace (edit, 1);
3786 break;
3788 else
3790 if (option_fake_half_tabs)
3792 int i;
3793 if (is_in_indent (edit) && right_of_four_spaces (edit))
3795 for (i = 0; i < HALF_TAB_SIZE; i++)
3796 edit_backspace (edit, 1);
3797 break;
3801 edit_backspace (edit, 0);
3802 break;
3803 case CK_Delete:
3804 /* if non persistent selection and text selected */
3805 if (!option_persistent_selections)
3807 if (edit->mark1 != edit->mark2)
3809 edit_block_delete_cmd (edit);
3810 break;
3814 if (option_cursor_beyond_eol && edit->over_col > 0)
3815 edit_insert_over (edit);
3817 if (option_fake_half_tabs)
3819 int i;
3820 if (is_in_indent (edit) && left_of_four_spaces (edit))
3822 for (i = 1; i <= HALF_TAB_SIZE; i++)
3823 edit_delete (edit, 1);
3824 break;
3827 edit_delete (edit, 0);
3828 break;
3829 case CK_DeleteToWordBegin:
3830 edit->over_col = 0;
3831 edit_left_delete_word (edit);
3832 break;
3833 case CK_DeleteToWordEnd:
3834 if (option_cursor_beyond_eol && edit->over_col > 0)
3835 edit_insert_over (edit);
3837 edit_right_delete_word (edit);
3838 break;
3839 case CK_DeleteLine:
3840 edit_delete_line (edit);
3841 break;
3842 case CK_DeleteToHome:
3843 edit_delete_to_line_begin (edit);
3844 break;
3845 case CK_DeleteToEnd:
3846 edit_delete_to_line_end (edit);
3847 break;
3848 case CK_Enter:
3849 edit->over_col = 0;
3850 if (option_auto_para_formatting)
3852 edit_double_newline (edit);
3853 if (option_return_does_auto_indent)
3854 edit_auto_indent (edit);
3855 format_paragraph (edit, 0);
3857 else
3859 edit_insert (edit, '\n');
3860 if (option_return_does_auto_indent)
3862 edit_auto_indent (edit);
3865 break;
3866 case CK_Return:
3867 edit_insert (edit, '\n');
3868 break;
3870 case CK_MarkColumnPageUp:
3871 edit->column_highlight = 1;
3872 case CK_PageUp:
3873 case CK_MarkPageUp:
3874 edit_move_up (edit, edit->widget.lines - 1, 1);
3875 break;
3876 case CK_MarkColumnPageDown:
3877 edit->column_highlight = 1;
3878 case CK_PageDown:
3879 case CK_MarkPageDown:
3880 edit_move_down (edit, edit->widget.lines - 1, 1);
3881 break;
3882 case CK_MarkColumnLeft:
3883 edit->column_highlight = 1;
3884 case CK_Left:
3885 case CK_MarkLeft:
3886 if (option_fake_half_tabs)
3888 if (is_in_indent (edit) && right_of_four_spaces (edit))
3890 if (option_cursor_beyond_eol && edit->over_col > 0)
3891 edit->over_col--;
3892 else
3893 edit_cursor_move (edit, -HALF_TAB_SIZE);
3894 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3895 break;
3898 edit_left_char_move_cmd (edit);
3899 break;
3900 case CK_MarkColumnRight:
3901 edit->column_highlight = 1;
3902 case CK_Right:
3903 case CK_MarkRight:
3904 if (option_fake_half_tabs)
3906 if (is_in_indent (edit) && left_of_four_spaces (edit))
3908 edit_cursor_move (edit, HALF_TAB_SIZE);
3909 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3910 break;
3913 edit_right_char_move_cmd (edit);
3914 break;
3915 case CK_TopOnScreen:
3916 case CK_MarkToPageBegin:
3917 edit_begin_page (edit);
3918 break;
3919 case CK_BottomOnScreen:
3920 case CK_MarkToPageEnd:
3921 edit_end_page (edit);
3922 break;
3923 case CK_WordLeft:
3924 case CK_MarkToWordBegin:
3925 edit->over_col = 0;
3926 edit_left_word_move_cmd (edit);
3927 break;
3928 case CK_WordRight:
3929 case CK_MarkToWordEnd:
3930 edit->over_col = 0;
3931 edit_right_word_move_cmd (edit);
3932 break;
3933 case CK_MarkColumnUp:
3934 edit->column_highlight = 1;
3935 case CK_Up:
3936 case CK_MarkUp:
3937 edit_move_up (edit, 1, 0);
3938 break;
3939 case CK_MarkColumnDown:
3940 edit->column_highlight = 1;
3941 case CK_Down:
3942 case CK_MarkDown:
3943 edit_move_down (edit, 1, 0);
3944 break;
3945 case CK_MarkColumnParagraphUp:
3946 edit->column_highlight = 1;
3947 case CK_ParagraphUp:
3948 case CK_MarkParagraphUp:
3949 edit_move_up_paragraph (edit, 0);
3950 break;
3951 case CK_MarkColumnParagraphDown:
3952 edit->column_highlight = 1;
3953 case CK_ParagraphDown:
3954 case CK_MarkParagraphDown:
3955 edit_move_down_paragraph (edit, 0);
3956 break;
3957 case CK_MarkColumnScrollUp:
3958 edit->column_highlight = 1;
3959 case CK_ScrollUp:
3960 case CK_MarkScrollUp:
3961 edit_move_up (edit, 1, 1);
3962 break;
3963 case CK_MarkColumnScrollDown:
3964 edit->column_highlight = 1;
3965 case CK_ScrollDown:
3966 case CK_MarkScrollDown:
3967 edit_move_down (edit, 1, 1);
3968 break;
3969 case CK_Home:
3970 case CK_MarkToHome:
3971 edit_cursor_to_bol (edit);
3972 break;
3973 case CK_End:
3974 case CK_MarkToEnd:
3975 edit_cursor_to_eol (edit);
3976 break;
3977 case CK_Tab:
3978 /* if text marked shift block */
3979 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3981 if (edit->mark2 < 0)
3982 edit_mark_cmd (edit, FALSE);
3983 edit_move_block_to_right (edit);
3985 else
3987 if (option_cursor_beyond_eol)
3988 edit_insert_over (edit);
3989 edit_tab_cmd (edit);
3990 if (option_auto_para_formatting)
3992 format_paragraph (edit, 0);
3993 edit->force |= REDRAW_PAGE;
3995 else
3997 check_and_wrap_line (edit);
4000 break;
4002 case CK_InsertOverwrite:
4003 edit->overwrite = !edit->overwrite;
4004 break;
4006 case CK_Mark:
4007 if (edit->mark2 >= 0)
4009 if (edit->column_highlight)
4010 edit_push_undo_action (edit, COLUMN_ON);
4011 edit->column_highlight = 0;
4013 edit_mark_cmd (edit, FALSE);
4014 break;
4015 case CK_MarkColumn:
4016 if (!edit->column_highlight)
4017 edit_push_undo_action (edit, COLUMN_OFF);
4018 edit->column_highlight = 1;
4019 edit_mark_cmd (edit, FALSE);
4020 break;
4021 case CK_MarkAll:
4022 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
4023 edit->force |= REDRAW_PAGE;
4024 break;
4025 case CK_Unmark:
4026 if (edit->column_highlight)
4027 edit_push_undo_action (edit, COLUMN_ON);
4028 edit->column_highlight = 0;
4029 edit_mark_cmd (edit, TRUE);
4030 break;
4031 case CK_MarkWord:
4032 if (edit->column_highlight)
4033 edit_push_undo_action (edit, COLUMN_ON);
4034 edit->column_highlight = 0;
4035 edit_mark_current_word_cmd (edit);
4036 break;
4037 case CK_MarkLine:
4038 if (edit->column_highlight)
4039 edit_push_undo_action (edit, COLUMN_ON);
4040 edit->column_highlight = 0;
4041 edit_mark_current_line_cmd (edit);
4042 break;
4044 case CK_Bookmark:
4045 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
4046 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
4047 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
4048 else
4049 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
4050 break;
4051 case CK_BookmarkFlush:
4052 book_mark_flush (edit, BOOK_MARK_COLOR);
4053 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4054 edit->force |= REDRAW_PAGE;
4055 break;
4056 case CK_BookmarkNext:
4057 if (edit->book_mark)
4059 struct _book_mark *p;
4060 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4061 if (p->next)
4063 p = p->next;
4064 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4065 edit_move_display (edit, p->line - edit->widget.lines / 2);
4066 edit_move_to_line (edit, p->line);
4069 break;
4070 case CK_BookmarkPrev:
4071 if (edit->book_mark)
4073 struct _book_mark *p;
4074 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4075 while (p->line == edit->curs_line)
4076 if (p->prev)
4077 p = p->prev;
4078 if (p->line >= 0)
4080 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4081 edit_move_display (edit, p->line - edit->widget.lines / 2);
4082 edit_move_to_line (edit, p->line);
4085 break;
4087 case CK_Top:
4088 case CK_MarkToFileBegin:
4089 edit_move_to_top (edit);
4090 break;
4091 case CK_Bottom:
4092 case CK_MarkToFileEnd:
4093 edit_move_to_bottom (edit);
4094 break;
4096 case CK_Copy:
4097 if (option_cursor_beyond_eol && edit->over_col > 0)
4098 edit_insert_over (edit);
4099 edit_block_copy_cmd (edit);
4100 break;
4101 case CK_Remove:
4102 edit_block_delete_cmd (edit);
4103 break;
4104 case CK_Move:
4105 edit_block_move_cmd (edit);
4106 break;
4108 case CK_BlockShiftLeft:
4109 if (edit->mark1 != edit->mark2)
4110 edit_move_block_to_left (edit);
4111 break;
4112 case CK_BlockShiftRight:
4113 if (edit->mark1 != edit->mark2)
4114 edit_move_block_to_right (edit);
4115 break;
4116 case CK_Store:
4117 edit_copy_to_X_buf_cmd (edit);
4118 break;
4119 case CK_Cut:
4120 edit_cut_to_X_buf_cmd (edit);
4121 break;
4122 case CK_Paste:
4123 /* if non persistent selection and text selected */
4124 if (!option_persistent_selections)
4126 if (edit->mark1 != edit->mark2)
4127 edit_block_delete_cmd (edit);
4129 if (option_cursor_beyond_eol && edit->over_col > 0)
4130 edit_insert_over (edit);
4131 edit_paste_from_X_buf_cmd (edit);
4132 break;
4133 case CK_History:
4134 edit_paste_from_history (edit);
4135 break;
4137 case CK_SaveAs:
4138 edit_save_as_cmd (edit);
4139 break;
4140 case CK_Save:
4141 edit_save_confirm_cmd (edit);
4142 break;
4143 case CK_BlockSave:
4144 edit_save_block_cmd (edit);
4145 break;
4146 case CK_InsertFile:
4147 edit_insert_file_cmd (edit);
4148 break;
4150 case CK_FilePrev:
4151 edit_load_back_cmd (edit);
4152 break;
4153 case CK_FileNext:
4154 edit_load_forward_cmd (edit);
4155 break;
4157 case CK_SyntaxChoose:
4158 edit_syntax_dialog (edit);
4159 break;
4161 case CK_Search:
4162 edit_search_cmd (edit, FALSE);
4163 break;
4164 case CK_SearchContinue:
4165 edit_search_cmd (edit, TRUE);
4166 break;
4167 case CK_Replace:
4168 edit_replace_cmd (edit, 0);
4169 break;
4170 case CK_ReplaceContinue:
4171 edit_replace_cmd (edit, 1);
4172 break;
4173 case CK_Complete:
4174 /* if text marked shift block */
4175 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4177 edit_move_block_to_left (edit);
4179 else
4181 edit_complete_word_cmd (edit);
4183 break;
4184 case CK_Find:
4185 edit_get_match_keyword_cmd (edit);
4186 break;
4187 #ifdef HAVE_ASPELL
4188 case CK_SpellCheckCurrentWord:
4189 edit_suggest_current_word (edit);
4190 break;
4191 case CK_SpellCheck:
4192 edit_spellcheck_file (edit);
4193 break;
4194 case CK_SpellCheckSelectLang:
4195 edit_set_spell_lang ();
4196 break;
4197 #endif
4198 case CK_Date:
4200 char s[BUF_MEDIUM];
4201 /* fool gcc to prevent a Y2K warning */
4202 char time_format[] = "_c";
4203 time_format[0] = '%';
4205 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4206 edit_print_string (edit, s);
4207 edit->force |= REDRAW_PAGE;
4208 break;
4210 break;
4211 case CK_Goto:
4212 edit_goto_cmd (edit);
4213 break;
4214 case CK_ParagraphFormat:
4215 format_paragraph (edit, 1);
4216 edit->force |= REDRAW_PAGE;
4217 break;
4218 case CK_MacroDelete:
4219 edit_delete_macro_cmd (edit);
4220 break;
4221 case CK_MatchBracket:
4222 edit_goto_matching_bracket (edit);
4223 break;
4224 case CK_UserMenu:
4225 user_menu (edit, NULL, -1);
4226 break;
4227 case CK_Sort:
4228 edit_sort_cmd (edit);
4229 break;
4230 case CK_ExternalCommand:
4231 edit_ext_cmd (edit);
4232 break;
4233 case CK_Mail:
4234 edit_mail_dialog (edit);
4235 break;
4236 #ifdef HAVE_CHARSET
4237 case CK_SelectCodepage:
4238 edit_select_codepage_cmd (edit);
4239 break;
4240 #endif
4241 case CK_InsertLiteral:
4242 edit_insert_literal_cmd (edit);
4243 break;
4244 case CK_MacroStartStopRecord:
4245 edit_begin_end_macro_cmd (edit);
4246 break;
4247 case CK_RepeatStartStopRecord:
4248 edit_begin_end_repeat_cmd (edit);
4249 break;
4250 case CK_ExtendedKeyMap:
4251 edit->extmod = TRUE;
4252 break;
4253 default:
4254 break;
4257 /* CK_PipeBlock */
4258 if ((command / CK_PipeBlock (0)) == 1)
4259 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4261 /* keys which must set the col position, and the search vars */
4262 switch (command)
4264 case CK_Search:
4265 case CK_SearchContinue:
4266 case CK_Replace:
4267 case CK_ReplaceContinue:
4268 case CK_Complete:
4269 edit->prev_col = edit_get_col (edit);
4270 break;
4271 case CK_Up:
4272 case CK_MarkUp:
4273 case CK_MarkColumnUp:
4274 case CK_Down:
4275 case CK_MarkDown:
4276 case CK_MarkColumnDown:
4277 case CK_PageUp:
4278 case CK_MarkPageUp:
4279 case CK_MarkColumnPageUp:
4280 case CK_PageDown:
4281 case CK_MarkPageDown:
4282 case CK_MarkColumnPageDown:
4283 case CK_Top:
4284 case CK_MarkToFileBegin:
4285 case CK_Bottom:
4286 case CK_MarkToFileEnd:
4287 case CK_ParagraphUp:
4288 case CK_MarkParagraphUp:
4289 case CK_MarkColumnParagraphUp:
4290 case CK_ParagraphDown:
4291 case CK_MarkParagraphDown:
4292 case CK_MarkColumnParagraphDown:
4293 case CK_ScrollUp:
4294 case CK_MarkScrollUp:
4295 case CK_MarkColumnScrollUp:
4296 case CK_ScrollDown:
4297 case CK_MarkScrollDown:
4298 case CK_MarkColumnScrollDown:
4299 edit->search_start = edit->curs1;
4300 edit->found_len = 0;
4301 break;
4302 default:
4303 edit->found_len = 0;
4304 edit->prev_col = edit_get_col (edit);
4305 edit->search_start = edit->curs1;
4307 edit_find_bracket (edit);
4309 if (option_auto_para_formatting)
4311 switch (command)
4313 case CK_BackSpace:
4314 case CK_Delete:
4315 case CK_DeleteToWordBegin:
4316 case CK_DeleteToWordEnd:
4317 case CK_DeleteToHome:
4318 case CK_DeleteToEnd:
4319 format_paragraph (edit, 0);
4320 edit->force |= REDRAW_PAGE;
4325 /* --------------------------------------------------------------------------------------------- */
4327 void
4328 edit_stack_init (void)
4330 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4332 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4333 edit_history_moveto[edit_stack_iterator].line = -1;
4336 edit_stack_iterator = 0;
4339 /* --------------------------------------------------------------------------------------------- */
4341 void
4342 edit_stack_free (void)
4344 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4345 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4348 /* --------------------------------------------------------------------------------------------- */
4349 /** move i lines */
4351 void
4352 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4354 edit_move_updown (edit, i, do_scroll, TRUE);
4357 /* --------------------------------------------------------------------------------------------- */
4358 /** move i lines */
4360 void
4361 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4363 edit_move_updown (edit, i, do_scroll, FALSE);
4366 /* --------------------------------------------------------------------------------------------- */