Ticket #2825: obtain FS name from stat info: sync with coreutils.
[midnight-commander.git] / src / editor / edit.c
blob961fdd27491bbadaf47cb15290bb89add2cd96c9
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"
74 /*** global variables ****************************************************************************/
76 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
77 int option_typewriter_wrap = 0;
78 int option_auto_para_formatting = 0;
79 int option_fill_tabs_with_spaces = 0;
80 int option_return_does_auto_indent = 1;
81 int option_backspace_through_tabs = 0;
82 int option_fake_half_tabs = 1;
83 int option_save_mode = EDIT_QUICK_SAVE;
84 int option_save_position = 1;
85 int option_max_undo = 32768;
86 int option_persistent_selections = 1;
87 int option_cursor_beyond_eol = 0;
88 int option_line_state = 0;
89 int option_line_state_width = 0;
91 int option_edit_right_extreme = 0;
92 int option_edit_left_extreme = 0;
93 int option_edit_top_extreme = 0;
94 int option_edit_bottom_extreme = 0;
95 int enable_show_tabs_tws = 1;
96 int option_check_nl_at_eof = 0;
97 int option_group_undo = 0;
98 int show_right_margin = 0;
100 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
101 char *option_backup_ext = NULL;
103 int edit_stack_iterator = 0;
104 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
105 /* magic sequense for say than block is vertical */
106 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
108 /*** file scope macro definitions ****************************************************************/
110 #define TEMP_BUF_LEN 1024
112 #define space_width 1
114 /*** file scope type declarations ****************************************************************/
116 /*** file scope variables ************************************************************************/
118 /* detecting an error on save is easy: just check if every byte has been written. */
119 /* detecting an error on read, is not so easy 'cos there is not way to tell
120 whether you read everything or not. */
121 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
122 static const struct edit_filters
124 const char *read, *write, *extension;
125 } all_filters[] =
127 /* *INDENT-OFF* */
128 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
129 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
130 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
131 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
133 /* *INDENT-ON* */
136 static long last_bracket = -1;
138 /*** file scope functions ************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
143 * here's a quick sketch of the layout: (don't run this through indent.)
145 * (b1 is buffers1 and b2 is buffers2)
148 * \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
149 * ______________________________________|______________________________________
151 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
152 * |-> |-> |-> |-> |-> |-> |
154 * _<------------------------->|<----------------->_
155 * WEdit->curs2 | WEdit->curs1
156 * ^ | ^
157 * | ^|^ |
158 * cursor ||| cursor
159 * |||
160 * file end|||file beginning
165 * This_is_some_file
166 * fin.
169 /* --------------------------------------------------------------------------------------------- */
171 static int left_of_four_spaces (WEdit * edit);
173 /* --------------------------------------------------------------------------------------------- */
175 * Initialize the buffers for an empty files.
178 static void
179 edit_init_buffers (WEdit * edit)
181 int j;
183 for (j = 0; j <= MAXBUFF; j++)
185 edit->buffers1[j] = NULL;
186 edit->buffers2[j] = NULL;
189 edit->curs1 = 0;
190 edit->curs2 = 0;
191 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
194 /* --------------------------------------------------------------------------------------------- */
197 * Load file OR text into buffers. Set cursor to the beginning of file.
199 * @returns FALSE on error.
202 static gboolean
203 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
205 long buf, buf2;
206 int file = -1;
207 gboolean ret = FALSE;
209 edit->curs2 = edit->last_byte;
210 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
212 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
213 if (file == -1)
215 gchar *errmsg, *filename;
217 filename = vfs_path_to_str (filename_vpath);
218 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
219 g_free (filename);
220 edit_error_dialog (_("Error"), errmsg);
221 g_free (errmsg);
222 return FALSE;
225 if (!edit->buffers2[buf2])
226 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
230 if (mc_read (file,
231 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
232 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
233 break;
235 for (buf = buf2 - 1; buf >= 0; buf--)
237 /* edit->buffers2[0] is already allocated */
238 if (!edit->buffers2[buf])
239 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
240 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
241 break;
243 ret = TRUE;
245 while (FALSE);
247 if (!ret)
249 gchar *errmsg, *filename;
251 filename = vfs_path_to_str (filename_vpath);
252 errmsg = g_strdup_printf (_("Error reading %s"), filename);
253 g_free (filename);
254 edit_error_dialog (_("Error"), errmsg);
255 g_free (errmsg);
257 mc_close (file);
258 return ret;
261 /* --------------------------------------------------------------------------------------------- */
262 /** Return index of the filter or -1 is there is no appropriate filter */
264 static int
265 edit_find_filter (const vfs_path_t * filename_vpath)
267 size_t i, l, e;
268 char *filename;
270 if (filename_vpath == NULL)
271 return -1;
273 filename = vfs_path_to_str (filename_vpath);
274 l = strlen (filename);
275 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
277 e = strlen (all_filters[i].extension);
278 if (l > e)
279 if (!strcmp (all_filters[i].extension, filename + l - e))
281 g_free (filename);
282 return i;
285 g_free (filename);
286 return -1;
289 /* --------------------------------------------------------------------------------------------- */
291 static char *
292 edit_get_filter (const vfs_path_t * filename_vpath)
294 int i;
295 char *p, *quoted_name, *filename;
297 i = edit_find_filter (filename_vpath);
298 if (i < 0)
299 return NULL;
301 filename = vfs_path_to_str (filename_vpath);
302 quoted_name = name_quote (filename, 0);
303 g_free (filename);
304 p = g_strdup_printf (all_filters[i].read, quoted_name);
305 g_free (quoted_name);
306 return p;
309 /* --------------------------------------------------------------------------------------------- */
311 static long
312 edit_insert_stream (WEdit * edit, FILE * f)
314 int c;
315 long i = 0;
316 while ((c = fgetc (f)) >= 0)
318 edit_insert (edit, c);
319 i++;
321 return i;
324 /* --------------------------------------------------------------------------------------------- */
326 * Open file and create it if necessary.
328 * @param edit editor object
329 * @param filename_vpath file name
330 * @param st buffer for store stat info
331 * @returns TRUE for success, FALSE for error.
334 static gboolean
335 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
337 int file;
338 gchar *errmsg = NULL;
340 /* Try opening an existing file */
341 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
342 if (file < 0)
345 * Try creating the file. O_EXCL prevents following broken links
346 * and opening existing files.
348 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
349 if (file < 0)
351 char *filename;
353 filename = vfs_path_to_str (filename_vpath);
354 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
355 g_free (filename);
356 goto cleanup;
359 /* New file, delete it if it's not modified or saved */
360 edit->delete_file = 1;
363 /* Check what we have opened */
364 if (mc_fstat (file, st) < 0)
366 char *filename;
368 filename = vfs_path_to_str (filename_vpath);
369 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
370 g_free (filename);
371 goto cleanup;
374 /* We want to open regular files only */
375 if (!S_ISREG (st->st_mode))
377 char *filename;
379 filename = vfs_path_to_str (filename_vpath);
380 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
381 g_free (filename);
382 goto cleanup;
386 * Don't delete non-empty files.
387 * O_EXCL should prevent it, but let's be on the safe side.
389 if (st->st_size > 0)
390 edit->delete_file = 0;
392 if (st->st_size >= SIZE_LIMIT)
394 char *filename;
396 filename = vfs_path_to_str (filename_vpath);
397 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
398 g_free (filename);
401 cleanup:
402 (void) mc_close (file);
404 if (errmsg != NULL)
406 edit_error_dialog (_("Error"), errmsg);
407 g_free (errmsg);
408 return FALSE;
410 return TRUE;
413 /* --------------------------------------------------------------------------------------------- */
416 * Open the file and load it into the buffers, either directly or using
417 * a filter. Return TRUE on success, FALSE on error.
419 * Fast loading (edit_load_file_fast) is used when the file size is
420 * known. In this case the data is read into the buffers by blocks.
421 * If the file size is not known, the data is loaded byte by byte in
422 * edit_insert_file.
424 * @param edit editor object
425 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
427 static gboolean
428 edit_load_file (WEdit * edit)
430 gboolean fast_load = TRUE;
432 /* Cannot do fast load if a filter is used */
433 if (edit_find_filter (edit->filename_vpath) >= 0)
434 fast_load = FALSE;
437 * FIXME: line end translation should disable fast loading as well
438 * Consider doing fseek() to the end and ftell() for the real size.
440 if (edit->filename_vpath != NULL)
443 * VFS may report file size incorrectly, and slow load is not a big
444 * deal considering overhead in VFS.
446 if (!vfs_file_is_local (edit->filename_vpath))
447 fast_load = FALSE;
449 /* If we are dealing with a real file, check that it exists */
450 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
452 edit_clean (edit);
453 return FALSE;
456 else
458 /* nothing to load */
459 fast_load = FALSE;
462 edit_init_buffers (edit);
464 if (fast_load)
466 edit->last_byte = edit->stat1.st_size;
467 edit_load_file_fast (edit, edit->filename_vpath);
468 /* If fast load was used, the number of lines wasn't calculated */
469 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
471 else
473 edit->last_byte = 0;
474 if (edit->filename_vpath != NULL
475 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
477 edit->undo_stack_disable = 1;
478 if (edit_insert_file (edit, edit->filename_vpath) < 0)
480 edit_clean (edit);
481 return FALSE;
483 edit->undo_stack_disable = 0;
486 edit->lb = LB_ASIS;
487 return TRUE;
490 /* --------------------------------------------------------------------------------------------- */
491 /** Restore saved cursor position in the file */
493 static void
494 edit_load_position (WEdit * edit)
496 long line, column;
497 off_t offset;
499 if (edit->filename_vpath == NULL
500 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
501 return;
503 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
505 if (line > 0)
507 edit_move_to_line (edit, line - 1);
508 edit->prev_col = column;
510 else if (offset > 0)
512 edit_cursor_move (edit, offset);
513 line = edit->curs_line;
514 edit->search_start = edit->curs1;
517 book_mark_restore (edit, BOOK_MARK_COLOR);
519 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
520 edit_move_display (edit, line - (edit->widget.lines / 2));
523 /* --------------------------------------------------------------------------------------------- */
524 /** Save cursor position in the file */
526 static void
527 edit_save_position (WEdit * edit)
529 if (edit->filename_vpath == NULL
530 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
531 return;
533 book_mark_serialize (edit, BOOK_MARK_COLOR);
534 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
535 edit->serialized_bookmarks);
536 edit->serialized_bookmarks = NULL;
539 /* --------------------------------------------------------------------------------------------- */
540 /** Clean the WEdit stricture except the widget part */
542 static void
543 edit_purge_widget (WEdit * edit)
545 size_t len = sizeof (WEdit) - sizeof (Widget);
546 char *start = (char *) edit + sizeof (Widget);
547 memset (start, 0, len);
550 /* --------------------------------------------------------------------------------------------- */
553 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
554 then the file should be as it was when he loaded up. Then set edit->modified to 0.
557 static long
558 edit_pop_undo_action (WEdit * edit)
560 long c;
561 unsigned long sp = edit->undo_stack_pointer;
563 if (sp == edit->undo_stack_bottom)
564 return STACK_BOTTOM;
566 sp = (sp - 1) & edit->undo_stack_size_mask;
567 c = edit->undo_stack[sp];
568 if (c >= 0)
570 /* edit->undo_stack[sp] = '@'; */
571 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
572 return c;
575 if (sp == edit->undo_stack_bottom)
576 return STACK_BOTTOM;
578 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
579 if (edit->undo_stack[sp] == -2)
581 /* edit->undo_stack[sp] = '@'; */
582 edit->undo_stack_pointer = sp;
584 else
585 edit->undo_stack[sp]++;
587 return c;
590 static long
591 edit_pop_redo_action (WEdit * edit)
593 long c;
594 unsigned long sp = edit->redo_stack_pointer;
596 if (sp == edit->redo_stack_bottom)
597 return STACK_BOTTOM;
599 sp = (sp - 1) & edit->redo_stack_size_mask;
600 c = edit->redo_stack[sp];
601 if (c >= 0)
603 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
604 return c;
607 if (sp == edit->redo_stack_bottom)
608 return STACK_BOTTOM;
610 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
611 if (edit->redo_stack[sp] == -2)
612 edit->redo_stack_pointer = sp;
613 else
614 edit->redo_stack[sp]++;
616 return c;
619 static long
620 get_prev_undo_action (WEdit * edit)
622 long c;
623 unsigned long sp = edit->undo_stack_pointer;
625 if (sp == edit->undo_stack_bottom)
626 return STACK_BOTTOM;
628 sp = (sp - 1) & edit->undo_stack_size_mask;
629 c = edit->undo_stack[sp];
630 if (c >= 0)
631 return c;
633 if (sp == edit->undo_stack_bottom)
634 return STACK_BOTTOM;
636 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
637 return c;
640 /* --------------------------------------------------------------------------------------------- */
641 /** is called whenever a modification is made by one of the four routines below */
643 static void
644 edit_modification (WEdit * edit)
646 edit->caches_valid = 0;
648 /* raise lock when file modified */
649 if (!edit->modified && !edit->delete_file)
650 edit->locked = lock_file (edit->filename_vpath);
651 edit->modified = 1;
654 /* --------------------------------------------------------------------------------------------- */
656 static char *
657 edit_get_byte_ptr (WEdit * edit, long byte_index)
659 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
660 return NULL;
662 if (byte_index >= edit->curs1)
664 unsigned long p;
666 p = edit->curs1 + edit->curs2 - byte_index - 1;
667 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
668 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
671 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
672 (byte_index & M_EDIT_BUF_SIZE));
675 /* --------------------------------------------------------------------------------------------- */
677 static int
678 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
680 int i, res;
681 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
682 gchar *str;
683 gchar *cursor_buf_ptr;
685 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
687 *char_width = 0;
688 return 0;
691 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
692 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
693 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
695 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
696 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
698 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
700 *char_width = 1;
701 return *(cursor_buf_ptr - 1);
703 else
705 res = g_utf8_get_char_validated (str, -1);
707 if (res < 0)
709 *char_width = 1;
710 return *(cursor_buf_ptr - 1);
712 else
714 *char_width = cursor_buf_ptr - str;
715 return res;
720 /* --------------------------------------------------------------------------------------------- */
722 static int
723 edit_backspace (WEdit * edit, const int byte_delete)
725 int p = 0;
726 int cw = 1;
727 int i;
729 if (!edit->curs1)
730 return 0;
732 cw = 1;
734 if (edit->mark2 != edit->mark1)
735 edit_push_markers (edit);
737 if (edit->utf8 && byte_delete == 0)
739 edit_get_prev_utf (edit, edit->curs1, &cw);
740 if (cw < 1)
741 cw = 1;
743 for (i = 1; i <= cw; i++)
745 if (edit->mark1 >= edit->curs1)
747 edit->mark1--;
748 edit->end_mark_curs--;
750 if (edit->mark2 >= edit->curs1)
751 edit->mark2--;
752 if (edit->last_get_rule >= edit->curs1)
753 edit->last_get_rule--;
755 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
756 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
757 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
759 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
760 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
762 edit->last_byte--;
763 edit->curs1--;
764 edit_push_undo_action (edit, p);
766 edit_modification (edit);
767 if (p == '\n')
769 if (edit->book_mark)
770 book_mark_dec (edit, edit->curs_line);
771 edit->curs_line--;
772 edit->total_lines--;
773 edit->force |= REDRAW_AFTER_CURSOR;
776 if (edit->curs1 < edit->start_display)
778 edit->start_display--;
779 if (p == '\n')
780 edit->start_line--;
783 return p;
786 /* --------------------------------------------------------------------------------------------- */
787 /* high level cursor movement commands */
788 /* --------------------------------------------------------------------------------------------- */
790 static int
791 is_in_indent (WEdit * edit)
793 long p = edit_bol (edit, edit->curs1);
794 while (p < edit->curs1)
795 if (!strchr (" \t", edit_get_byte (edit, p++)))
796 return 0;
797 return 1;
800 /* --------------------------------------------------------------------------------------------- */
802 static int
803 is_blank (WEdit * edit, long offset)
805 long s, f;
806 int c;
807 s = edit_bol (edit, offset);
808 f = edit_eol (edit, offset) - 1;
809 while (s <= f)
811 c = edit_get_byte (edit, s++);
812 if (!isspace (c))
813 return 0;
815 return 1;
819 /* --------------------------------------------------------------------------------------------- */
820 /** returns the offset of line i */
822 static long
823 edit_find_line (WEdit * edit, int line)
825 int i, j = 0;
826 int m = 2000000000;
827 if (!edit->caches_valid)
829 for (i = 0; i < N_LINE_CACHES; i++)
830 edit->line_numbers[i] = edit->line_offsets[i] = 0;
831 /* three offsets that we *know* are line 0 at 0 and these two: */
832 edit->line_numbers[1] = edit->curs_line;
833 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
834 edit->line_numbers[2] = edit->total_lines;
835 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
836 edit->caches_valid = 1;
838 if (line >= edit->total_lines)
839 return edit->line_offsets[2];
840 if (line <= 0)
841 return 0;
842 /* find the closest known point */
843 for (i = 0; i < N_LINE_CACHES; i++)
845 int n;
846 n = abs (edit->line_numbers[i] - line);
847 if (n < m)
849 m = n;
850 j = i;
853 if (m == 0)
854 return edit->line_offsets[j]; /* know the offset exactly */
855 if (m == 1 && j >= 3)
856 i = j; /* one line different - caller might be looping, so stay in this cache */
857 else
858 i = 3 + (rand () % (N_LINE_CACHES - 3));
859 if (line > edit->line_numbers[j])
860 edit->line_offsets[i] =
861 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
862 else
863 edit->line_offsets[i] =
864 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
865 edit->line_numbers[i] = line;
866 return edit->line_offsets[i];
869 /* --------------------------------------------------------------------------------------------- */
870 /** moves up until a blank line is reached, or until just
871 before a non-blank line is reached */
873 static void
874 edit_move_up_paragraph (WEdit * edit, int do_scroll)
876 int i = 0;
877 if (edit->curs_line > 1)
879 if (line_is_blank (edit, edit->curs_line))
881 if (line_is_blank (edit, edit->curs_line - 1))
883 for (i = edit->curs_line - 1; i; i--)
884 if (!line_is_blank (edit, i))
886 i++;
887 break;
890 else
892 for (i = edit->curs_line - 1; i; i--)
893 if (line_is_blank (edit, i))
894 break;
897 else
899 for (i = edit->curs_line - 1; i; i--)
900 if (line_is_blank (edit, i))
901 break;
904 edit_move_up (edit, edit->curs_line - i, do_scroll);
907 /* --------------------------------------------------------------------------------------------- */
908 /** moves down until a blank line is reached, or until just
909 before a non-blank line is reached */
911 static void
912 edit_move_down_paragraph (WEdit * edit, int do_scroll)
914 int i;
915 if (edit->curs_line >= edit->total_lines - 1)
917 i = edit->total_lines;
919 else
921 if (line_is_blank (edit, edit->curs_line))
923 if (line_is_blank (edit, edit->curs_line + 1))
925 for (i = edit->curs_line + 1; i; i++)
926 if (!line_is_blank (edit, i) || i > edit->total_lines)
928 i--;
929 break;
932 else
934 for (i = edit->curs_line + 1; i; i++)
935 if (line_is_blank (edit, i) || i >= edit->total_lines)
936 break;
939 else
941 for (i = edit->curs_line + 1; i; i++)
942 if (line_is_blank (edit, i) || i >= edit->total_lines)
943 break;
946 edit_move_down (edit, i - edit->curs_line, do_scroll);
949 /* --------------------------------------------------------------------------------------------- */
951 static void
952 edit_begin_page (WEdit * edit)
954 edit_update_curs_row (edit);
955 edit_move_up (edit, edit->curs_row, 0);
958 /* --------------------------------------------------------------------------------------------- */
960 static void
961 edit_end_page (WEdit * edit)
963 edit_update_curs_row (edit);
964 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
968 /* --------------------------------------------------------------------------------------------- */
969 /** goto beginning of text */
971 static void
972 edit_move_to_top (WEdit * edit)
974 if (edit->curs_line)
976 edit_cursor_move (edit, -edit->curs1);
977 edit_move_to_prev_col (edit, 0);
978 edit->force |= REDRAW_PAGE;
979 edit->search_start = 0;
980 edit_update_curs_row (edit);
985 /* --------------------------------------------------------------------------------------------- */
986 /** goto end of text */
988 static void
989 edit_move_to_bottom (WEdit * edit)
991 if (edit->curs_line < edit->total_lines)
993 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
994 edit->start_display = edit->last_byte;
995 edit->start_line = edit->total_lines;
996 edit_scroll_upward (edit, edit->widget.lines - 1);
997 edit->force |= REDRAW_PAGE;
1001 /* --------------------------------------------------------------------------------------------- */
1002 /** goto beginning of line */
1004 static void
1005 edit_cursor_to_bol (WEdit * edit)
1007 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1008 edit->search_start = edit->curs1;
1009 edit->prev_col = edit_get_col (edit);
1010 edit->over_col = 0;
1013 /* --------------------------------------------------------------------------------------------- */
1014 /** goto end of line */
1016 static void
1017 edit_cursor_to_eol (WEdit * edit)
1019 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1020 edit->search_start = edit->curs1;
1021 edit->prev_col = edit_get_col (edit);
1022 edit->over_col = 0;
1025 /* --------------------------------------------------------------------------------------------- */
1027 static unsigned long
1028 my_type_of (int c)
1030 int x, r = 0;
1031 const char *p, *q;
1032 const char option_chars_move_whole_word[] =
1033 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1035 if (!c)
1036 return 0;
1037 if (c == '!')
1039 if (*option_chars_move_whole_word == '!')
1040 return 2;
1041 return 0x80000000UL;
1043 if (g_ascii_isupper ((gchar) c))
1044 c = 'A';
1045 else if (g_ascii_islower ((gchar) c))
1046 c = 'a';
1047 else if (g_ascii_isalpha (c))
1048 c = 'a';
1049 else if (isdigit (c))
1050 c = '0';
1051 else if (isspace (c))
1052 c = ' ';
1053 q = strchr (option_chars_move_whole_word, c);
1054 if (!q)
1055 return 0xFFFFFFFFUL;
1058 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1059 if (*p == '!')
1060 x <<= 1;
1061 r |= x;
1063 while ((q = strchr (q + 1, c)));
1064 return r;
1067 /* --------------------------------------------------------------------------------------------- */
1069 static void
1070 edit_left_word_move (WEdit * edit, int s)
1072 for (;;)
1074 int c1, c2;
1075 if (edit->column_highlight
1076 && edit->mark1 != edit->mark2
1077 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1078 break;
1079 edit_cursor_move (edit, -1);
1080 if (!edit->curs1)
1081 break;
1082 c1 = edit_get_byte (edit, edit->curs1 - 1);
1083 c2 = edit_get_byte (edit, edit->curs1);
1084 if (c1 == '\n' || c2 == '\n')
1085 break;
1086 if (!(my_type_of (c1) & my_type_of (c2)))
1087 break;
1088 if (isspace (c1) && !isspace (c2))
1089 break;
1090 if (s)
1091 if (!isspace (c1) && isspace (c2))
1092 break;
1096 /* --------------------------------------------------------------------------------------------- */
1098 static void
1099 edit_left_word_move_cmd (WEdit * edit)
1101 edit_left_word_move (edit, 0);
1102 edit->force |= REDRAW_PAGE;
1105 /* --------------------------------------------------------------------------------------------- */
1107 static void
1108 edit_right_word_move (WEdit * edit, int s)
1110 for (;;)
1112 int c1, c2;
1113 if (edit->column_highlight
1114 && edit->mark1 != edit->mark2
1115 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1116 break;
1117 edit_cursor_move (edit, 1);
1118 if (edit->curs1 >= edit->last_byte)
1119 break;
1120 c1 = edit_get_byte (edit, edit->curs1 - 1);
1121 c2 = edit_get_byte (edit, edit->curs1);
1122 if (c1 == '\n' || c2 == '\n')
1123 break;
1124 if (!(my_type_of (c1) & my_type_of (c2)))
1125 break;
1126 if (isspace (c1) && !isspace (c2))
1127 break;
1128 if (s)
1129 if (!isspace (c1) && isspace (c2))
1130 break;
1134 /* --------------------------------------------------------------------------------------------- */
1136 static void
1137 edit_right_word_move_cmd (WEdit * edit)
1139 edit_right_word_move (edit, 0);
1140 edit->force |= REDRAW_PAGE;
1143 /* --------------------------------------------------------------------------------------------- */
1145 static void
1146 edit_right_char_move_cmd (WEdit * edit)
1148 int cw = 1;
1149 int c = 0;
1150 if (edit->utf8)
1152 c = edit_get_utf (edit, edit->curs1, &cw);
1153 if (cw < 1)
1154 cw = 1;
1156 else
1158 c = edit_get_byte (edit, edit->curs1);
1160 if (option_cursor_beyond_eol && c == '\n')
1162 edit->over_col++;
1164 else
1166 edit_cursor_move (edit, cw);
1170 /* --------------------------------------------------------------------------------------------- */
1172 static void
1173 edit_left_char_move_cmd (WEdit * edit)
1175 int cw = 1;
1176 if (edit->column_highlight
1177 && option_cursor_beyond_eol
1178 && edit->mark1 != edit->mark2
1179 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1180 return;
1181 if (edit->utf8)
1183 edit_get_prev_utf (edit, edit->curs1, &cw);
1184 if (cw < 1)
1185 cw = 1;
1187 if (option_cursor_beyond_eol && edit->over_col > 0)
1189 edit->over_col--;
1191 else
1193 edit_cursor_move (edit, -cw);
1197 /* --------------------------------------------------------------------------------------------- */
1198 /** Up or down cursor moving.
1199 direction = TRUE - move up
1200 = FALSE - move down
1203 static void
1204 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1206 unsigned long p;
1207 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1209 if (i > l)
1210 i = l;
1212 if (i == 0)
1213 return;
1215 if (i > 1)
1216 edit->force |= REDRAW_PAGE;
1217 if (do_scroll)
1219 if (direction)
1220 edit_scroll_upward (edit, i);
1221 else
1222 edit_scroll_downward (edit, i);
1224 p = edit_bol (edit, edit->curs1);
1226 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1228 edit_cursor_move (edit, p - edit->curs1);
1230 edit_move_to_prev_col (edit, p);
1232 /* search start of current multibyte char (like CJK) */
1233 if (edit->curs1 + 1 < edit->last_byte)
1235 edit_right_char_move_cmd (edit);
1236 edit_left_char_move_cmd (edit);
1239 edit->search_start = edit->curs1;
1240 edit->found_len = 0;
1243 /* --------------------------------------------------------------------------------------------- */
1245 static void
1246 edit_right_delete_word (WEdit * edit)
1248 int c1, c2;
1249 for (;;)
1251 if (edit->curs1 >= edit->last_byte)
1252 break;
1253 c1 = edit_delete (edit, 1);
1254 c2 = edit_get_byte (edit, edit->curs1);
1255 if (c1 == '\n' || c2 == '\n')
1256 break;
1257 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1258 break;
1259 if (!(my_type_of (c1) & my_type_of (c2)))
1260 break;
1264 /* --------------------------------------------------------------------------------------------- */
1266 static void
1267 edit_left_delete_word (WEdit * edit)
1269 int c1, c2;
1270 for (;;)
1272 if (edit->curs1 <= 0)
1273 break;
1274 c1 = edit_backspace (edit, 1);
1275 c2 = edit_get_byte (edit, edit->curs1 - 1);
1276 if (c1 == '\n' || c2 == '\n')
1277 break;
1278 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1279 break;
1280 if (!(my_type_of (c1) & my_type_of (c2)))
1281 break;
1285 /* --------------------------------------------------------------------------------------------- */
1287 the start column position is not recorded, and hence does not
1288 undo as it happed. But who would notice.
1291 static void
1292 edit_do_undo (WEdit * edit)
1294 long ac;
1295 long count = 0;
1297 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1298 edit->over_col = 0;
1299 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1301 switch ((int) ac)
1303 case STACK_BOTTOM:
1304 goto done_undo;
1305 case CURS_RIGHT:
1306 edit_cursor_move (edit, 1);
1307 break;
1308 case CURS_LEFT:
1309 edit_cursor_move (edit, -1);
1310 break;
1311 case BACKSPACE:
1312 case BACKSPACE_BR:
1313 edit_backspace (edit, 1);
1314 break;
1315 case DELCHAR:
1316 case DELCHAR_BR:
1317 edit_delete (edit, 1);
1318 break;
1319 case COLUMN_ON:
1320 edit->column_highlight = 1;
1321 break;
1322 case COLUMN_OFF:
1323 edit->column_highlight = 0;
1324 break;
1326 if (ac >= 256 && ac < 512)
1327 edit_insert_ahead (edit, ac - 256);
1328 if (ac >= 0 && ac < 256)
1329 edit_insert (edit, ac);
1331 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1333 edit->mark1 = ac - MARK_1;
1334 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1336 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1338 edit->mark2 = ac - MARK_2;
1339 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1341 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1343 edit->end_mark_curs = ac - MARK_CURS;
1345 if (count++)
1346 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1349 if (edit->start_display > ac - KEY_PRESS)
1351 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1352 edit->force |= REDRAW_PAGE;
1354 else if (edit->start_display < ac - KEY_PRESS)
1356 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1357 edit->force |= REDRAW_PAGE;
1359 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1360 edit_update_curs_row (edit);
1362 done_undo:;
1363 edit->undo_stack_disable = 0;
1366 static void
1367 edit_do_redo (WEdit * edit)
1369 long ac;
1370 long count = 0;
1372 if (edit->redo_stack_reset)
1373 return;
1375 edit->over_col = 0;
1376 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1378 switch ((int) ac)
1380 case STACK_BOTTOM:
1381 goto done_redo;
1382 case CURS_RIGHT:
1383 edit_cursor_move (edit, 1);
1384 break;
1385 case CURS_LEFT:
1386 edit_cursor_move (edit, -1);
1387 break;
1388 case BACKSPACE:
1389 edit_backspace (edit, 1);
1390 break;
1391 case DELCHAR:
1392 edit_delete (edit, 1);
1393 break;
1394 case COLUMN_ON:
1395 edit->column_highlight = 1;
1396 break;
1397 case COLUMN_OFF:
1398 edit->column_highlight = 0;
1399 break;
1401 if (ac >= 256 && ac < 512)
1402 edit_insert_ahead (edit, ac - 256);
1403 if (ac >= 0 && ac < 256)
1404 edit_insert (edit, ac);
1406 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1408 edit->mark1 = ac - MARK_1;
1409 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1411 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1413 edit->mark2 = ac - MARK_2;
1414 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1416 /* more than one pop usually means something big */
1417 if (count++)
1418 edit->force |= REDRAW_PAGE;
1421 if (edit->start_display > ac - KEY_PRESS)
1423 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1424 edit->force |= REDRAW_PAGE;
1426 else if (edit->start_display < ac - KEY_PRESS)
1428 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1429 edit->force |= REDRAW_PAGE;
1431 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1432 edit_update_curs_row (edit);
1434 done_redo:;
1437 static void
1438 edit_group_undo (WEdit * edit)
1440 long ac = KEY_PRESS;
1441 long cur_ac = KEY_PRESS;
1442 while (ac != STACK_BOTTOM && ac == cur_ac)
1444 cur_ac = get_prev_undo_action (edit);
1445 edit_do_undo (edit);
1446 ac = get_prev_undo_action (edit);
1447 /* exit from cycle if option_group_undo is not set,
1448 * and make single UNDO operation
1450 if (!option_group_undo)
1451 ac = STACK_BOTTOM;
1455 /* --------------------------------------------------------------------------------------------- */
1457 static void
1458 edit_delete_to_line_end (WEdit * edit)
1460 while (edit_get_byte (edit, edit->curs1) != '\n')
1462 if (!edit->curs2)
1463 break;
1464 edit_delete (edit, 1);
1468 /* --------------------------------------------------------------------------------------------- */
1470 static void
1471 edit_delete_to_line_begin (WEdit * edit)
1473 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1475 if (!edit->curs1)
1476 break;
1477 edit_backspace (edit, 1);
1481 /* --------------------------------------------------------------------------------------------- */
1483 static int
1484 is_aligned_on_a_tab (WEdit * edit)
1486 edit_update_curs_col (edit);
1487 return !((edit->curs_col % (TAB_SIZE * space_width))
1488 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1491 /* --------------------------------------------------------------------------------------------- */
1493 static int
1494 right_of_four_spaces (WEdit * edit)
1496 int i, ch = 0;
1497 for (i = 1; i <= HALF_TAB_SIZE; i++)
1498 ch |= edit_get_byte (edit, edit->curs1 - i);
1499 if (ch == ' ')
1500 return is_aligned_on_a_tab (edit);
1501 return 0;
1504 /* --------------------------------------------------------------------------------------------- */
1506 static int
1507 left_of_four_spaces (WEdit * edit)
1509 int i, ch = 0;
1510 for (i = 0; i < HALF_TAB_SIZE; i++)
1511 ch |= edit_get_byte (edit, edit->curs1 + i);
1512 if (ch == ' ')
1513 return is_aligned_on_a_tab (edit);
1514 return 0;
1517 /* --------------------------------------------------------------------------------------------- */
1519 static void
1520 edit_auto_indent (WEdit * edit)
1522 long p;
1523 char c;
1524 p = edit->curs1;
1525 /* use the previous line as a template */
1526 p = edit_move_backward (edit, p, 1);
1527 /* copy the leading whitespace of the line */
1528 for (;;)
1529 { /* no range check - the line _is_ \n-terminated */
1530 c = edit_get_byte (edit, p++);
1531 if (c != ' ' && c != '\t')
1532 break;
1533 edit_insert (edit, c);
1537 /* --------------------------------------------------------------------------------------------- */
1539 static inline void
1540 edit_double_newline (WEdit * edit)
1542 edit_insert (edit, '\n');
1543 if (edit_get_byte (edit, edit->curs1) == '\n')
1544 return;
1545 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1546 return;
1547 edit->force |= REDRAW_PAGE;
1548 edit_insert (edit, '\n');
1552 /* --------------------------------------------------------------------------------------------- */
1554 static void
1555 insert_spaces_tab (WEdit * edit, gboolean half)
1557 int i;
1559 edit_update_curs_col (edit);
1560 i = option_tab_spacing * space_width;
1561 if (half)
1562 i /= 2;
1563 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1564 while (i > 0)
1566 edit_insert (edit, ' ');
1567 i -= space_width;
1571 /* --------------------------------------------------------------------------------------------- */
1573 static inline void
1574 edit_tab_cmd (WEdit * edit)
1576 int i;
1578 if (option_fake_half_tabs)
1580 if (is_in_indent (edit))
1582 /*insert a half tab (usually four spaces) unless there is a
1583 half tab already behind, then delete it and insert a
1584 full tab. */
1585 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1586 insert_spaces_tab (edit, TRUE);
1587 else
1589 for (i = 1; i <= HALF_TAB_SIZE; i++)
1590 edit_backspace (edit, 1);
1591 edit_insert (edit, '\t');
1593 return;
1596 if (option_fill_tabs_with_spaces)
1597 insert_spaces_tab (edit, FALSE);
1598 else
1599 edit_insert (edit, '\t');
1602 /* --------------------------------------------------------------------------------------------- */
1604 static void
1605 check_and_wrap_line (WEdit * edit)
1607 int curs, c;
1608 if (!option_typewriter_wrap)
1609 return;
1610 edit_update_curs_col (edit);
1611 if (edit->curs_col < option_word_wrap_line_length)
1612 return;
1613 curs = edit->curs1;
1614 for (;;)
1616 curs--;
1617 c = edit_get_byte (edit, curs);
1618 if (c == '\n' || curs <= 0)
1620 edit_insert (edit, '\n');
1621 return;
1623 if (c == ' ' || c == '\t')
1625 int current = edit->curs1;
1626 edit_cursor_move (edit, curs - edit->curs1 + 1);
1627 edit_insert (edit, '\n');
1628 edit_cursor_move (edit, current - edit->curs1 + 1);
1629 return;
1634 /* --------------------------------------------------------------------------------------------- */
1635 /** this find the matching bracket in either direction, and sets edit->bracket */
1637 static long
1638 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1640 const char *const b = "{}{[][()(", *p;
1641 int i = 1, a, inc = -1, c, d, n = 0;
1642 unsigned long j = 0;
1643 long q;
1644 edit_update_curs_row (edit);
1645 c = edit_get_byte (edit, edit->curs1);
1646 p = strchr (b, c);
1647 /* no limit */
1648 if (!furthest_bracket_search)
1649 furthest_bracket_search--;
1650 /* not on a bracket at all */
1651 if (!p)
1652 return -1;
1653 /* the matching bracket */
1654 d = p[1];
1655 /* going left or right? */
1656 if (strchr ("{[(", c))
1657 inc = 1;
1658 for (q = edit->curs1 + inc;; q += inc)
1660 /* out of buffer? */
1661 if (q >= edit->last_byte || q < 0)
1662 break;
1663 a = edit_get_byte (edit, q);
1664 /* don't want to eat CPU */
1665 if (j++ > furthest_bracket_search)
1666 break;
1667 /* out of screen? */
1668 if (in_screen)
1670 if (q < edit->start_display)
1671 break;
1672 /* count lines if searching downward */
1673 if (inc > 0 && a == '\n')
1674 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1675 break;
1677 /* count bracket depth */
1678 i += (a == c) - (a == d);
1679 /* return if bracket depth is zero */
1680 if (!i)
1681 return q;
1683 /* no match */
1684 return -1;
1687 /* --------------------------------------------------------------------------------------------- */
1689 static inline void
1690 edit_goto_matching_bracket (WEdit * edit)
1692 long q;
1694 q = edit_get_bracket (edit, 0, 0);
1695 if (q >= 0)
1697 edit->bracket = edit->curs1;
1698 edit->force |= REDRAW_PAGE;
1699 edit_cursor_move (edit, q - edit->curs1);
1703 /* --------------------------------------------------------------------------------------------- */
1705 static void
1706 edit_move_block_to_right (WEdit * edit)
1708 long start_mark, end_mark;
1709 long cur_bol, start_bol;
1711 if (eval_marks (edit, &start_mark, &end_mark))
1712 return;
1714 start_bol = edit_bol (edit, start_mark);
1715 cur_bol = edit_bol (edit, end_mark - 1);
1719 edit_cursor_move (edit, cur_bol - edit->curs1);
1720 if (option_fill_tabs_with_spaces)
1721 insert_spaces_tab (edit, option_fake_half_tabs);
1722 else
1723 edit_insert (edit, '\t');
1724 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1726 if (cur_bol == 0)
1727 break;
1729 cur_bol = edit_bol (edit, cur_bol - 1);
1731 while (cur_bol >= start_bol);
1733 edit->force |= REDRAW_PAGE;
1736 /* --------------------------------------------------------------------------------------------- */
1738 static void
1739 edit_move_block_to_left (WEdit * edit)
1741 long start_mark, end_mark;
1742 long cur_bol, start_bol;
1743 int i;
1745 if (eval_marks (edit, &start_mark, &end_mark))
1746 return;
1748 start_bol = edit_bol (edit, start_mark);
1749 cur_bol = edit_bol (edit, end_mark - 1);
1753 int del_tab_width;
1754 int next_char;
1756 edit_cursor_move (edit, cur_bol - edit->curs1);
1758 if (option_fake_half_tabs)
1759 del_tab_width = HALF_TAB_SIZE;
1760 else
1761 del_tab_width = option_tab_spacing;
1763 next_char = edit_get_byte (edit, edit->curs1);
1764 if (next_char == '\t')
1765 edit_delete (edit, 1);
1766 else if (next_char == ' ')
1767 for (i = 1; i <= del_tab_width; i++)
1769 if (next_char == ' ')
1770 edit_delete (edit, 1);
1771 next_char = edit_get_byte (edit, edit->curs1);
1774 if (cur_bol == 0)
1775 break;
1777 cur_bol = edit_bol (edit, cur_bol - 1);
1779 while (cur_bol >= start_bol);
1781 edit->force |= REDRAW_PAGE;
1784 /* --------------------------------------------------------------------------------------------- */
1786 * prints at the cursor
1787 * @returns the number of chars printed
1790 static size_t
1791 edit_print_string (WEdit * e, const char *s)
1793 size_t i = 0;
1795 while (s[i] != '\0')
1796 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1797 e->force |= REDRAW_COMPLETELY;
1798 edit_update_screen (e);
1799 return i;
1802 /* --------------------------------------------------------------------------------------------- */
1803 /*** public functions ****************************************************************************/
1804 /* --------------------------------------------------------------------------------------------- */
1806 /** User edit menu, like user menu (F2) but only in editor. */
1808 void
1809 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1811 char *block_file;
1812 int nomark;
1813 long curs;
1814 long start_mark, end_mark;
1815 struct stat status;
1816 vfs_path_t *block_file_vpath;
1818 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1819 block_file_vpath = vfs_path_from_str (block_file);
1820 curs = edit->curs1;
1821 nomark = eval_marks (edit, &start_mark, &end_mark);
1822 if (nomark == 0)
1823 edit_save_block (edit, block_file, start_mark, end_mark);
1825 /* run shell scripts from menu */
1826 if (user_menu_cmd (edit, menu_file, selected_entry)
1827 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1829 int rc = 0;
1830 FILE *fd;
1832 /* i.e. we have marked block */
1833 if (nomark == 0)
1834 rc = edit_block_delete_cmd (edit);
1836 if (rc == 0)
1838 long ins_len;
1840 ins_len = edit_insert_file (edit, block_file_vpath);
1841 if (nomark == 0 && ins_len > 0)
1842 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1844 /* truncate block file */
1845 fd = fopen (block_file, "w");
1846 if (fd != NULL)
1847 fclose (fd);
1849 g_free (block_file);
1850 vfs_path_free (block_file_vpath);
1852 edit_cursor_move (edit, curs - edit->curs1);
1853 edit->force |= REDRAW_PAGE;
1854 send_message ((Widget *) edit, WIDGET_DRAW, 0);
1857 /* --------------------------------------------------------------------------------------------- */
1860 edit_get_byte (WEdit * edit, long byte_index)
1862 unsigned long p;
1863 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1864 return '\n';
1866 if (byte_index >= edit->curs1)
1868 p = edit->curs1 + edit->curs2 - byte_index - 1;
1869 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1871 else
1873 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1877 /* --------------------------------------------------------------------------------------------- */
1880 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1882 gchar *str = NULL;
1883 int res = -1;
1884 gunichar ch;
1885 gchar *next_ch = NULL;
1886 int width = 0;
1887 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1889 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1891 *char_width = 0;
1892 return '\n';
1895 str = edit_get_byte_ptr (edit, byte_index);
1897 if (str == NULL)
1899 *char_width = 0;
1900 return 0;
1903 res = g_utf8_get_char_validated (str, -1);
1905 if (res < 0)
1907 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1908 int i;
1909 for (i = 0; i < UTF8_CHAR_LEN; i++)
1910 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1911 utf8_buf[UTF8_CHAR_LEN] = '\0';
1912 str = utf8_buf;
1913 res = g_utf8_get_char_validated (str, -1);
1916 if (res < 0)
1918 ch = *str;
1919 width = 0;
1921 else
1923 ch = res;
1924 /* Calculate UTF-8 char width */
1925 next_ch = g_utf8_next_char (str);
1926 if (next_ch)
1928 width = next_ch - str;
1930 else
1932 ch = 0;
1933 width = 0;
1936 *char_width = width;
1937 return ch;
1940 /* --------------------------------------------------------------------------------------------- */
1942 char *
1943 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
1945 int i;
1946 char *p, *writename;
1947 const vfs_path_element_t *path_element;
1949 i = edit_find_filter (filename_vpath);
1950 if (i < 0)
1951 return NULL;
1953 path_element = vfs_path_get_by_index (write_name_vpath, -1);
1954 writename = name_quote (path_element->path, 0);
1955 p = g_strdup_printf (all_filters[i].write, writename);
1956 g_free (writename);
1957 return p;
1960 /* --------------------------------------------------------------------------------------------- */
1962 long
1963 edit_write_stream (WEdit * edit, FILE * f)
1965 long i;
1967 if (edit->lb == LB_ASIS)
1969 for (i = 0; i < edit->last_byte; i++)
1970 if (fputc (edit_get_byte (edit, i), f) < 0)
1971 break;
1972 return i;
1975 /* change line breaks */
1976 for (i = 0; i < edit->last_byte; i++)
1978 unsigned char c = edit_get_byte (edit, i);
1980 if (!(c == '\n' || c == '\r'))
1982 /* not line break */
1983 if (fputc (c, f) < 0)
1984 return i;
1986 else
1987 { /* (c == '\n' || c == '\r') */
1988 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1990 switch (edit->lb)
1992 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1993 /* put one line break unconditionally */
1994 if (fputc ('\n', f) < 0)
1995 return i;
1997 i++; /* 2 chars are processed */
1999 if (c == '\r' && c1 == '\n')
2000 /* Windows line break; go to the next char */
2001 break;
2003 if (c == '\r' && c1 == '\r')
2005 /* two Macintosh line breaks; put second line break */
2006 if (fputc ('\n', f) < 0)
2007 return i;
2008 break;
2011 if (fputc (c1, f) < 0)
2012 return i;
2013 break;
2015 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2016 /* put one line break unconditionally */
2017 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2018 return i;
2020 if (c == '\r' && c1 == '\n')
2021 /* Windows line break; go to the next char */
2022 i++;
2023 break;
2025 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2026 /* put one line break unconditionally */
2027 if (fputc ('\r', f) < 0)
2028 return i;
2030 i++; /* 2 chars are processed */
2032 if (c == '\r' && c1 == '\n')
2033 /* Windows line break; go to the next char */
2034 break;
2036 if (c == '\n' && c1 == '\n')
2038 /* two Windows line breaks; put second line break */
2039 if (fputc ('\r', f) < 0)
2040 return i;
2041 break;
2044 if (fputc (c1, f) < 0)
2045 return i;
2046 break;
2047 case LB_ASIS: /* default without changes */
2048 break;
2053 return edit->last_byte;
2056 /* --------------------------------------------------------------------------------------------- */
2057 /** inserts a file at the cursor, returns count of inserted bytes on success */
2058 long
2059 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2061 char *p;
2062 long ins_len = 0;
2064 p = edit_get_filter (filename_vpath);
2065 if (p != NULL)
2067 FILE *f;
2068 long current = edit->curs1;
2070 f = (FILE *) popen (p, "r");
2071 if (f != NULL)
2073 edit_insert_stream (edit, f);
2074 ins_len = edit->curs1 - current;
2075 edit_cursor_move (edit, -ins_len);
2076 if (pclose (f) > 0)
2078 char *errmsg;
2080 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2081 edit_error_dialog (_("Error"), errmsg);
2082 g_free (errmsg);
2083 ins_len = -1;
2086 else
2088 char *errmsg;
2090 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2091 edit_error_dialog (_("Error"), errmsg);
2092 g_free (errmsg);
2093 ins_len = -1;
2095 g_free (p);
2097 else
2099 int i, file, blocklen;
2100 long current = edit->curs1;
2101 int vertical_insertion = 0;
2102 char *buf;
2104 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2105 if (file == -1)
2106 return -1;
2108 buf = g_malloc0 (TEMP_BUF_LEN);
2109 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2110 if (blocklen > 0)
2112 /* if contain signature VERTICAL_MAGIC then it vertical block */
2113 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2114 vertical_insertion = 1;
2115 else
2116 mc_lseek (file, 0, SEEK_SET);
2119 if (vertical_insertion)
2121 long mark1, mark2;
2122 int c1, c2;
2124 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2125 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2126 /* highlight inserted text then not persistent blocks */
2127 if (!option_persistent_selections)
2129 if (!edit->column_highlight)
2130 edit_push_undo_action (edit, COLUMN_OFF);
2131 edit->column_highlight = 1;
2134 else
2136 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2138 for (i = 0; i < blocklen; i++)
2139 edit_insert (edit, buf[i]);
2141 /* highlight inserted text then not persistent blocks */
2142 if (!option_persistent_selections && edit->modified)
2144 edit_set_markers (edit, edit->curs1, current, 0, 0);
2145 if (edit->column_highlight)
2146 edit_push_undo_action (edit, COLUMN_ON);
2147 edit->column_highlight = 0;
2151 edit->force |= REDRAW_PAGE;
2152 ins_len = edit->curs1 - current;
2153 edit_cursor_move (edit, -ins_len);
2154 g_free (buf);
2155 mc_close (file);
2156 if (blocklen != 0)
2157 ins_len = 0;
2160 return ins_len;
2163 /* --------------------------------------------------------------------------------------------- */
2165 * Fill in the edit structure. Return NULL on failure. Pass edit as
2166 * NULL to allocate a new structure.
2168 * If line is 0, try to restore saved position. Otherwise put the
2169 * cursor on that line and show it in the middle of the screen.
2172 WEdit *
2173 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2174 long line)
2176 gboolean to_free = FALSE;
2178 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2179 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
2181 if (edit != NULL)
2182 edit_purge_widget (edit);
2183 else
2185 #ifdef ENABLE_NLS
2187 * Expand option_whole_chars_search by national letters using
2188 * current locale
2191 static char option_whole_chars_search_buf[256];
2193 if (option_whole_chars_search_buf != option_whole_chars_search)
2195 size_t i;
2196 size_t len = str_term_width1 (option_whole_chars_search);
2198 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2200 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2202 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2204 option_whole_chars_search_buf[len++] = i;
2208 option_whole_chars_search_buf[len] = 0;
2209 option_whole_chars_search = option_whole_chars_search_buf;
2211 #endif /* ENABLE_NLS */
2212 edit = g_malloc0 (sizeof (WEdit));
2213 to_free = TRUE;
2216 edit->drag_state = MCEDIT_DRAG_NORMAL;
2217 edit->widget.y = y;
2218 edit->widget.x = x;
2219 edit->widget.lines = lines;
2220 edit->widget.cols = cols;
2221 edit_save_size (edit);
2222 edit->fullscreen = TRUE;
2224 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2225 edit->stat1.st_uid = getuid ();
2226 edit->stat1.st_gid = getgid ();
2227 edit->stat1.st_mtime = 0;
2229 edit->over_col = 0;
2230 edit->bracket = -1;
2231 edit->force |= REDRAW_PAGE;
2233 /* set file name before load file */
2234 edit_set_filename (edit, filename_vpath);
2236 edit->undo_stack_size = START_STACK_SIZE;
2237 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2238 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2240 edit->redo_stack_size = START_STACK_SIZE;
2241 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2242 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2244 edit->utf8 = 0;
2245 edit->converter = str_cnv_from_term;
2246 edit_set_codeset (edit);
2248 if (!edit_load_file (edit))
2250 /* edit_load_file already gives an error message */
2251 if (to_free)
2252 g_free (edit);
2253 return NULL;
2256 edit->loading_done = 1;
2257 edit->modified = 0;
2258 edit->locked = 0;
2259 edit_load_syntax (edit, NULL, NULL);
2261 int color;
2262 edit_get_syntax_color (edit, -1, &color);
2265 /* load saved cursor position */
2266 if ((line == 0) && option_save_position)
2267 edit_load_position (edit);
2268 else
2270 if (line <= 0)
2271 line = 1;
2272 edit_move_display (edit, line - 1);
2273 edit_move_to_line (edit, line - 1);
2276 edit_load_macro_cmd (edit);
2277 return edit;
2280 /* --------------------------------------------------------------------------------------------- */
2282 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2283 gboolean
2284 edit_clean (WEdit * edit)
2286 int j = 0;
2288 if (edit == NULL)
2289 return FALSE;
2291 /* a stale lock, remove it */
2292 if (edit->locked)
2293 edit->locked = unlock_file (edit->filename_vpath);
2295 /* save cursor position */
2296 if (option_save_position)
2297 edit_save_position (edit);
2298 else if (edit->serialized_bookmarks != NULL)
2299 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2301 /* File specified on the mcedit command line and never saved */
2302 if (edit->delete_file)
2303 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2305 edit_free_syntax_rules (edit);
2306 book_mark_flush (edit, -1);
2307 for (; j <= MAXBUFF; j++)
2309 g_free (edit->buffers1[j]);
2310 g_free (edit->buffers2[j]);
2313 g_free (edit->undo_stack);
2314 g_free (edit->redo_stack);
2315 vfs_path_free (edit->filename_vpath);
2316 vfs_path_free (edit->dir_vpath);
2317 mc_search_free (edit->search);
2318 edit->search = NULL;
2320 if (edit->converter != str_cnv_from_term)
2321 str_close_conv (edit->converter);
2323 edit_purge_widget (edit);
2325 return TRUE;
2328 /* --------------------------------------------------------------------------------------------- */
2331 * Load a new file into the editor and set line. If it fails, preserve the old file.
2332 * To do it, allocate a new widget, initialize it and, if the new file
2333 * was loaded, copy the data to the old widget.
2335 * @returns TRUE on success, FALSE on failure.
2337 gboolean
2338 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2340 WEdit *e;
2341 int y = edit->widget.y;
2342 int x = edit->widget.x;
2343 int lines = edit->widget.lines;
2344 int columns = edit->widget.cols;
2346 e = g_malloc0 (sizeof (WEdit));
2347 e->widget = edit->widget;
2349 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2351 g_free (e);
2352 return FALSE;
2355 edit_clean (edit);
2356 memcpy (edit, e, sizeof (WEdit));
2357 g_free (e);
2359 return TRUE;
2362 /* --------------------------------------------------------------------------------------------- */
2364 void
2365 edit_set_codeset (WEdit * edit)
2367 #ifdef HAVE_CHARSET
2368 const char *cp_id;
2370 cp_id =
2371 get_codepage_id (mc_global.source_codepage >=
2372 0 ? mc_global.source_codepage : mc_global.display_codepage);
2374 if (cp_id != NULL)
2376 GIConv conv;
2377 conv = str_crt_conv_from (cp_id);
2378 if (conv != INVALID_CONV)
2380 if (edit->converter != str_cnv_from_term)
2381 str_close_conv (edit->converter);
2382 edit->converter = conv;
2386 if (cp_id != NULL)
2387 edit->utf8 = str_isutf8 (cp_id);
2388 #else
2389 (void) edit;
2390 #endif
2394 /* --------------------------------------------------------------------------------------------- */
2396 Recording stack for undo:
2397 The following is an implementation of a compressed stack. Identical
2398 pushes are recorded by a negative prefix indicating the number of times the
2399 same char was pushed. This saves space for repeated curs-left or curs-right
2400 delete etc.
2404 pushed: stored:
2408 b -3
2410 c --> -4
2416 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2417 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2418 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2419 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2420 position.
2422 The only way the cursor moves or the buffer is changed is through the routines:
2423 insert, backspace, insert_ahead, delete, and cursor_move.
2424 These record the reverse undo movements onto the stack each time they are
2425 called.
2427 Each key press results in a set of actions (insert; delete ...). So each time
2428 a key is pressed the current position of start_display is pushed as
2429 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2430 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2431 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2435 void
2436 edit_push_undo_action (WEdit * edit, long c, ...)
2438 unsigned long sp = edit->undo_stack_pointer;
2439 unsigned long spm1;
2440 long *t;
2442 /* first enlarge the stack if necessary */
2443 if (sp > edit->undo_stack_size - 10)
2444 { /* say */
2445 if (option_max_undo < 256)
2446 option_max_undo = 256;
2447 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2449 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2450 if (t)
2452 edit->undo_stack = t;
2453 edit->undo_stack_size <<= 1;
2454 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2458 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2459 if (edit->undo_stack_disable)
2461 edit_push_redo_action (edit, KEY_PRESS);
2462 edit_push_redo_action (edit, c);
2463 return;
2465 else if (edit->redo_stack_reset)
2467 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2470 if (edit->undo_stack_bottom != sp
2471 && spm1 != edit->undo_stack_bottom
2472 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2474 int d;
2475 if (edit->undo_stack[spm1] < 0)
2477 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2478 if (d == c)
2480 if (edit->undo_stack[spm1] > -1000000000)
2482 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2484 edit->undo_stack[spm1]--;
2486 return;
2490 else
2492 d = edit->undo_stack[spm1];
2493 if (d == c)
2495 if (c >= KEY_PRESS)
2496 return; /* --> no need to push multiple do-nothings */
2497 edit->undo_stack[sp] = -2;
2498 goto check_bottom;
2502 edit->undo_stack[sp] = c;
2504 check_bottom:
2505 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2507 /* if the sp wraps round and catches the undo_stack_bottom then erase
2508 * the first set of actions on the stack to make space - by moving
2509 * undo_stack_bottom forward one "key press" */
2510 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2511 if ((unsigned long) c == edit->undo_stack_bottom ||
2512 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2515 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2517 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2518 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2520 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2521 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2522 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2524 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2528 void
2529 edit_push_redo_action (WEdit * edit, long c, ...)
2531 unsigned long sp = edit->redo_stack_pointer;
2532 unsigned long spm1;
2533 long *t;
2534 /* first enlarge the stack if necessary */
2535 if (sp > edit->redo_stack_size - 10)
2536 { /* say */
2537 if (option_max_undo < 256)
2538 option_max_undo = 256;
2539 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2541 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2542 if (t)
2544 edit->redo_stack = t;
2545 edit->redo_stack_size <<= 1;
2546 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2550 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2552 if (edit->redo_stack_bottom != sp
2553 && spm1 != edit->redo_stack_bottom
2554 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2556 int d;
2557 if (edit->redo_stack[spm1] < 0)
2559 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2560 if (d == c)
2562 if (edit->redo_stack[spm1] > -1000000000)
2564 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2565 edit->redo_stack[spm1]--;
2566 return;
2570 else
2572 d = edit->redo_stack[spm1];
2573 if (d == c)
2575 if (c >= KEY_PRESS)
2576 return; /* --> no need to push multiple do-nothings */
2577 edit->redo_stack[sp] = -2;
2578 goto redo_check_bottom;
2582 edit->redo_stack[sp] = c;
2584 redo_check_bottom:
2585 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2587 /* if the sp wraps round and catches the redo_stack_bottom then erase
2588 * the first set of actions on the stack to make space - by moving
2589 * redo_stack_bottom forward one "key press" */
2590 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2591 if ((unsigned long) c == edit->redo_stack_bottom ||
2592 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2595 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2597 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2598 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2601 * If a single key produced enough pushes to wrap all the way round then
2602 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2603 * The stack is then initialised:
2606 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2607 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2608 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2612 /* --------------------------------------------------------------------------------------------- */
2614 Basic low level single character buffer alterations and movements at the cursor.
2615 Returns char passed over, inserted or removed.
2618 void
2619 edit_insert (WEdit * edit, int c)
2621 /* check if file has grown to large */
2622 if (edit->last_byte >= SIZE_LIMIT)
2623 return;
2625 /* first we must update the position of the display window */
2626 if (edit->curs1 < edit->start_display)
2628 edit->start_display++;
2629 if (c == '\n')
2630 edit->start_line++;
2633 /* Mark file as modified, unless the file hasn't been fully loaded */
2634 if (edit->loading_done)
2636 edit_modification (edit);
2639 /* now we must update some info on the file and check if a redraw is required */
2640 if (c == '\n')
2642 if (edit->book_mark)
2643 book_mark_inc (edit, edit->curs_line);
2644 edit->curs_line++;
2645 edit->total_lines++;
2646 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2649 /* save the reverse command onto the undo stack */
2650 /* ordinary char and not space */
2651 if (c > 32)
2652 edit_push_undo_action (edit, BACKSPACE);
2653 else
2654 edit_push_undo_action (edit, BACKSPACE_BR);
2655 /* update markers */
2656 edit->mark1 += (edit->mark1 > edit->curs1);
2657 edit->mark2 += (edit->mark2 > edit->curs1);
2658 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2660 /* add a new buffer if we've reached the end of the last one */
2661 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2662 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2664 /* perform the insertion */
2665 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2666 = (unsigned char) c;
2668 /* update file length */
2669 edit->last_byte++;
2671 /* update cursor position */
2672 edit->curs1++;
2675 /* --------------------------------------------------------------------------------------------- */
2676 /** same as edit_insert and move left */
2678 void
2679 edit_insert_ahead (WEdit * edit, int c)
2681 if (edit->last_byte >= SIZE_LIMIT)
2682 return;
2684 if (edit->curs1 < edit->start_display)
2686 edit->start_display++;
2687 if (c == '\n')
2688 edit->start_line++;
2690 edit_modification (edit);
2691 if (c == '\n')
2693 if (edit->book_mark)
2694 book_mark_inc (edit, edit->curs_line);
2695 edit->total_lines++;
2696 edit->force |= REDRAW_AFTER_CURSOR;
2698 /* ordinary char and not space */
2699 if (c > 32)
2700 edit_push_undo_action (edit, DELCHAR);
2701 else
2702 edit_push_undo_action (edit, DELCHAR_BR);
2704 edit->mark1 += (edit->mark1 >= edit->curs1);
2705 edit->mark2 += (edit->mark2 >= edit->curs1);
2706 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2708 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2709 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2710 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2711 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2713 edit->last_byte++;
2714 edit->curs2++;
2718 /* --------------------------------------------------------------------------------------------- */
2721 edit_delete (WEdit * edit, const int byte_delete)
2723 int p = 0;
2724 int cw = 1;
2725 int i;
2727 if (!edit->curs2)
2728 return 0;
2730 cw = 1;
2731 /* if byte_delete = 1 then delete only one byte not multibyte char */
2732 if (edit->utf8 && byte_delete == 0)
2734 edit_get_utf (edit, edit->curs1, &cw);
2735 if (cw < 1)
2736 cw = 1;
2739 if (edit->mark2 != edit->mark1)
2740 edit_push_markers (edit);
2742 for (i = 1; i <= cw; i++)
2744 if (edit->mark1 > edit->curs1)
2746 edit->mark1--;
2747 edit->end_mark_curs--;
2749 if (edit->mark2 > edit->curs1)
2750 edit->mark2--;
2751 if (edit->last_get_rule > edit->curs1)
2752 edit->last_get_rule--;
2754 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2755 ((edit->curs2 -
2756 1) & M_EDIT_BUF_SIZE) - 1];
2758 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2760 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2761 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2763 edit->last_byte--;
2764 edit->curs2--;
2765 edit_push_undo_action (edit, p + 256);
2768 edit_modification (edit);
2769 if (p == '\n')
2771 if (edit->book_mark)
2772 book_mark_dec (edit, edit->curs_line);
2773 edit->total_lines--;
2774 edit->force |= REDRAW_AFTER_CURSOR;
2776 if (edit->curs1 < edit->start_display)
2778 edit->start_display--;
2779 if (p == '\n')
2780 edit->start_line--;
2783 return p;
2786 /* --------------------------------------------------------------------------------------------- */
2787 /** moves the cursor right or left: increment positive or negative respectively */
2789 void
2790 edit_cursor_move (WEdit * edit, long increment)
2792 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2793 int c;
2795 if (increment < 0)
2797 for (; increment < 0; increment++)
2799 if (!edit->curs1)
2800 return;
2802 edit_push_undo_action (edit, CURS_RIGHT);
2804 c = edit_get_byte (edit, edit->curs1 - 1);
2805 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2806 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2807 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2808 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2809 edit->curs2++;
2810 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2811 1) & M_EDIT_BUF_SIZE];
2812 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2814 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2815 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2817 edit->curs1--;
2818 if (c == '\n')
2820 edit->curs_line--;
2821 edit->force |= REDRAW_LINE_BELOW;
2826 else if (increment > 0)
2828 for (; increment > 0; increment--)
2830 if (!edit->curs2)
2831 return;
2833 edit_push_undo_action (edit, CURS_LEFT);
2835 c = edit_get_byte (edit, edit->curs1);
2836 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2837 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2838 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2839 edit->curs1++;
2840 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2841 ((edit->curs2 -
2842 1) & M_EDIT_BUF_SIZE) - 1];
2843 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2845 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2846 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2848 edit->curs2--;
2849 if (c == '\n')
2851 edit->curs_line++;
2852 edit->force |= REDRAW_LINE_ABOVE;
2858 /* These functions return positions relative to lines */
2860 /* --------------------------------------------------------------------------------------------- */
2861 /** returns index of last char on line + 1 */
2863 long
2864 edit_eol (WEdit * edit, long current)
2866 if (current >= edit->last_byte)
2867 return edit->last_byte;
2869 for (;; current++)
2870 if (edit_get_byte (edit, current) == '\n')
2871 break;
2872 return current;
2875 /* --------------------------------------------------------------------------------------------- */
2876 /** returns index of first char on line */
2878 long
2879 edit_bol (WEdit * edit, long current)
2881 if (current <= 0)
2882 return 0;
2884 for (;; current--)
2885 if (edit_get_byte (edit, current - 1) == '\n')
2886 break;
2887 return current;
2890 /* --------------------------------------------------------------------------------------------- */
2892 long
2893 edit_count_lines (WEdit * edit, long current, long upto)
2895 long lines = 0;
2896 if (upto > edit->last_byte)
2897 upto = edit->last_byte;
2898 if (current < 0)
2899 current = 0;
2900 while (current < upto)
2901 if (edit_get_byte (edit, current++) == '\n')
2902 lines++;
2903 return lines;
2906 /* --------------------------------------------------------------------------------------------- */
2907 /* If lines is zero this returns the count of lines from current to upto. */
2908 /* If upto is zero returns index of lines forward current. */
2910 long
2911 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2913 if (upto)
2915 return edit_count_lines (edit, current, upto);
2917 else
2919 long next;
2920 if (lines < 0)
2921 lines = 0;
2922 while (lines--)
2924 next = edit_eol (edit, current) + 1;
2925 if (next > edit->last_byte)
2926 break;
2927 else
2928 current = next;
2930 return current;
2934 /* --------------------------------------------------------------------------------------------- */
2935 /** Returns offset of 'lines' lines up from current */
2937 long
2938 edit_move_backward (WEdit * edit, long current, long lines)
2940 if (lines < 0)
2941 lines = 0;
2942 current = edit_bol (edit, current);
2943 while ((lines--) && current != 0)
2944 current = edit_bol (edit, current - 1);
2945 return current;
2948 /* --------------------------------------------------------------------------------------------- */
2949 /* If cols is zero this returns the count of columns from current to upto. */
2950 /* If upto is zero returns index of cols across from current. */
2952 long
2953 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
2955 long p, q;
2956 int col;
2958 if (upto)
2960 q = upto;
2961 cols = -10;
2963 else
2964 q = edit->last_byte + 2;
2966 for (col = 0, p = current; p < q; p++)
2968 int c, orig_c;
2970 if (cols != -10)
2972 if (col == cols)
2973 return p;
2974 if (col > cols)
2975 return p - 1;
2978 orig_c = c = edit_get_byte (edit, p);
2980 #ifdef HAVE_CHARSET
2981 if (edit->utf8)
2983 int utf_ch;
2984 int cw = 1;
2986 utf_ch = edit_get_utf (edit, p, &cw);
2987 if (mc_global.utf8_display)
2989 if (cw > 1)
2990 col -= cw - 1;
2991 if (g_unichar_iswide (utf_ch))
2992 col++;
2994 else if (cw > 1 && g_unichar_isprint (utf_ch))
2995 col -= cw - 1;
2998 c = convert_to_display_c (c);
2999 #endif
3001 if (c == '\t')
3002 col += TAB_SIZE - col % TAB_SIZE;
3003 else if (c == '\n')
3005 if (upto)
3006 return col;
3007 else
3008 return p;
3010 else if ((c < 32 || c == 127) && (orig_c == c || (!mc_global.utf8_display && !edit->utf8)))
3011 /* '\r' is shown as ^M, so we must advance 2 characters */
3012 /* Caret notation for control characters */
3013 col += 2;
3014 else
3015 col++;
3017 return col;
3020 /* --------------------------------------------------------------------------------------------- */
3021 /** returns the current column position of the cursor */
3024 edit_get_col (WEdit * edit)
3026 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3029 /* --------------------------------------------------------------------------------------------- */
3030 /* Scrolling functions */
3031 /* --------------------------------------------------------------------------------------------- */
3033 void
3034 edit_update_curs_row (WEdit * edit)
3036 edit->curs_row = edit->curs_line - edit->start_line;
3039 /* --------------------------------------------------------------------------------------------- */
3041 void
3042 edit_update_curs_col (WEdit * edit)
3044 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3047 /* --------------------------------------------------------------------------------------------- */
3050 edit_get_curs_col (const WEdit * edit)
3052 return edit->curs_col;
3055 /* --------------------------------------------------------------------------------------------- */
3056 /** moves the display start position up by i lines */
3058 void
3059 edit_scroll_upward (WEdit * edit, unsigned long i)
3061 unsigned long lines_above = edit->start_line;
3062 if (i > lines_above)
3063 i = lines_above;
3064 if (i)
3066 edit->start_line -= i;
3067 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3068 edit->force |= REDRAW_PAGE;
3069 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3071 edit_update_curs_row (edit);
3075 /* --------------------------------------------------------------------------------------------- */
3076 /** returns 1 if could scroll, 0 otherwise */
3078 void
3079 edit_scroll_downward (WEdit * edit, int i)
3081 int lines_below;
3082 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3083 if (lines_below > 0)
3085 if (i > lines_below)
3086 i = lines_below;
3087 edit->start_line += i;
3088 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3089 edit->force |= REDRAW_PAGE;
3090 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3092 edit_update_curs_row (edit);
3095 /* --------------------------------------------------------------------------------------------- */
3097 void
3098 edit_scroll_right (WEdit * edit, int i)
3100 edit->force |= REDRAW_PAGE;
3101 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3102 edit->start_col -= i;
3105 /* --------------------------------------------------------------------------------------------- */
3107 void
3108 edit_scroll_left (WEdit * edit, int i)
3110 if (edit->start_col)
3112 edit->start_col += i;
3113 if (edit->start_col > 0)
3114 edit->start_col = 0;
3115 edit->force |= REDRAW_PAGE;
3116 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3120 /* --------------------------------------------------------------------------------------------- */
3121 /* high level cursor movement commands */
3122 /* --------------------------------------------------------------------------------------------- */
3124 void
3125 edit_move_to_prev_col (WEdit * edit, long p)
3127 int prev = edit->prev_col;
3128 int over = edit->over_col;
3129 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3131 if (option_cursor_beyond_eol)
3133 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3134 edit_eol (edit, edit->curs1));
3136 if (line_len < prev + edit->over_col)
3138 edit->over_col = prev + over - line_len;
3139 edit->prev_col = line_len;
3140 edit->curs_col = line_len;
3142 else
3144 edit->curs_col = prev + over;
3145 edit->prev_col = edit->curs_col;
3146 edit->over_col = 0;
3149 else
3151 edit->over_col = 0;
3152 if (is_in_indent (edit) && option_fake_half_tabs)
3154 edit_update_curs_col (edit);
3155 if (space_width)
3156 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3158 int q = edit->curs_col;
3159 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3160 p = edit_bol (edit, edit->curs1);
3161 edit_cursor_move (edit,
3162 edit_move_forward3 (edit, p, edit->curs_col,
3163 0) - edit->curs1);
3164 if (!left_of_four_spaces (edit))
3165 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3171 /* --------------------------------------------------------------------------------------------- */
3174 line_is_blank (WEdit * edit, long line)
3176 return is_blank (edit, edit_find_line (edit, line));
3179 /* --------------------------------------------------------------------------------------------- */
3180 /** move cursor to line 'line' */
3182 void
3183 edit_move_to_line (WEdit * e, long line)
3185 if (line < e->curs_line)
3186 edit_move_up (e, e->curs_line - line, 0);
3187 else
3188 edit_move_down (e, line - e->curs_line, 0);
3189 edit_scroll_screen_over_cursor (e);
3192 /* --------------------------------------------------------------------------------------------- */
3193 /** scroll window so that first visible line is 'line' */
3195 void
3196 edit_move_display (WEdit * e, long line)
3198 if (line < e->start_line)
3199 edit_scroll_upward (e, e->start_line - line);
3200 else
3201 edit_scroll_downward (e, line - e->start_line);
3204 /* --------------------------------------------------------------------------------------------- */
3205 /** save markers onto undo stack */
3207 void
3208 edit_push_markers (WEdit * edit)
3210 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3211 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3212 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3215 /* --------------------------------------------------------------------------------------------- */
3217 void
3218 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3220 edit->mark1 = m1;
3221 edit->mark2 = m2;
3222 edit->column1 = c1;
3223 edit->column2 = c2;
3227 /* --------------------------------------------------------------------------------------------- */
3228 /** highlight marker toggle */
3230 void
3231 edit_mark_cmd (WEdit * edit, int unmark)
3233 edit_push_markers (edit);
3234 if (unmark)
3236 edit_set_markers (edit, 0, 0, 0, 0);
3237 edit->force |= REDRAW_PAGE;
3239 else
3241 if (edit->mark2 >= 0)
3243 edit->end_mark_curs = -1;
3244 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3245 edit->curs_col + edit->over_col);
3246 edit->force |= REDRAW_PAGE;
3248 else
3250 edit->end_mark_curs = edit->curs1;
3251 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3252 edit->curs_col + edit->over_col);
3257 /* --------------------------------------------------------------------------------------------- */
3258 /** highlight the word under cursor */
3260 void
3261 edit_mark_current_word_cmd (WEdit * edit)
3263 long pos;
3265 for (pos = edit->curs1; pos != 0; pos--)
3267 int c1, c2;
3269 c1 = edit_get_byte (edit, pos);
3270 c2 = edit_get_byte (edit, pos - 1);
3271 if (!isspace (c1) && isspace (c2))
3272 break;
3273 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3274 break;
3276 edit->mark1 = pos;
3278 for (; pos < edit->last_byte; pos++)
3280 int c1, c2;
3282 c1 = edit_get_byte (edit, pos);
3283 c2 = edit_get_byte (edit, pos + 1);
3284 if (!isspace (c1) && isspace (c2))
3285 break;
3286 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3287 break;
3289 edit->mark2 = min (pos + 1, edit->last_byte);
3291 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3294 /* --------------------------------------------------------------------------------------------- */
3296 void
3297 edit_mark_current_line_cmd (WEdit * edit)
3299 long pos = edit->curs1;
3301 edit->mark1 = edit_bol (edit, pos);
3302 edit->mark2 = edit_eol (edit, pos);
3304 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3307 /* --------------------------------------------------------------------------------------------- */
3309 void
3310 edit_delete_line (WEdit * edit)
3313 * Delete right part of the line.
3314 * Note that edit_get_byte() returns '\n' when byte position is
3315 * beyond EOF.
3317 while (edit_get_byte (edit, edit->curs1) != '\n')
3319 (void) edit_delete (edit, 1);
3323 * Delete '\n' char.
3324 * Note that edit_delete() will not corrupt anything if called while
3325 * cursor position is EOF.
3327 (void) edit_delete (edit, 1);
3330 * Delete left part of the line.
3331 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3333 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3335 (void) edit_backspace (edit, 1);
3339 /* --------------------------------------------------------------------------------------------- */
3342 edit_indent_width (WEdit * edit, long p)
3344 long q = p;
3345 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3346 q++;
3347 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3350 /* --------------------------------------------------------------------------------------------- */
3352 void
3353 edit_insert_indent (WEdit * edit, int indent)
3355 if (!option_fill_tabs_with_spaces)
3357 while (indent >= TAB_SIZE)
3359 edit_insert (edit, '\t');
3360 indent -= TAB_SIZE;
3363 while (indent-- > 0)
3364 edit_insert (edit, ' ');
3367 /* --------------------------------------------------------------------------------------------- */
3369 void
3370 edit_push_key_press (WEdit * edit)
3372 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3373 if (edit->mark2 == -1)
3375 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3376 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3380 /* --------------------------------------------------------------------------------------------- */
3382 void
3383 edit_find_bracket (WEdit * edit)
3385 edit->bracket = edit_get_bracket (edit, 1, 10000);
3386 if (last_bracket != edit->bracket)
3387 edit->force |= REDRAW_PAGE;
3388 last_bracket = edit->bracket;
3391 /* --------------------------------------------------------------------------------------------- */
3393 * This executes a command as though the user initiated it through a key
3394 * press. Callback with WIDGET_KEY as a message calls this after
3395 * translating the key press. This function can be used to pass any
3396 * command to the editor. Note that the screen wouldn't update
3397 * automatically. Either of command or char_for_insertion must be
3398 * passed as -1. Commands are executed, and char_for_insertion is
3399 * inserted at the cursor.
3402 void
3403 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3405 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3406 || (macro_index < 0
3407 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3409 macro_index = 0;
3410 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3411 return;
3413 if (macro_index != -1)
3415 edit->force |= REDRAW_COMPLETELY;
3416 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3418 edit_store_macro_cmd (edit);
3419 macro_index = -1;
3420 return;
3422 else if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3424 edit_repeat_macro_cmd (edit);
3425 macro_index = -1;
3426 return;
3430 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3432 record_macro_buf[macro_index].action = command;
3433 record_macro_buf[macro_index++].ch = char_for_insertion;
3435 /* record the beginning of a set of editing actions initiated by a key press */
3436 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3437 edit_push_key_press (edit);
3439 edit_execute_cmd (edit, command, char_for_insertion);
3440 if (edit->column_highlight)
3441 edit->force |= REDRAW_PAGE;
3444 /* --------------------------------------------------------------------------------------------- */
3446 This executes a command at a lower level than macro recording.
3447 It also does not push a key_press onto the undo stack. This means
3448 that if it is called many times, a single undo command will undo
3449 all of them. It also does not check for the Undo command.
3451 void
3452 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3454 if (command == CK_WindowFullscreen)
3456 edit_toggle_fullscreen (edit);
3457 return;
3460 /* handle window state */
3461 if (edit_handle_move_resize (edit, command))
3462 return;
3464 edit->force |= REDRAW_LINE;
3466 /* The next key press will unhighlight the found string, so update
3467 * the whole page */
3468 if (edit->found_len || edit->column_highlight)
3469 edit->force |= REDRAW_PAGE;
3471 switch (command)
3473 /* a mark command with shift-arrow */
3474 case CK_MarkLeft:
3475 case CK_MarkRight:
3476 case CK_MarkToWordBegin:
3477 case CK_MarkToWordEnd:
3478 case CK_MarkToHome:
3479 case CK_MarkToEnd:
3480 case CK_MarkUp:
3481 case CK_MarkDown:
3482 case CK_MarkPageUp:
3483 case CK_MarkPageDown:
3484 case CK_MarkToFileBegin:
3485 case CK_MarkToFileEnd:
3486 case CK_MarkToPageBegin:
3487 case CK_MarkToPageEnd:
3488 case CK_MarkScrollUp:
3489 case CK_MarkScrollDown:
3490 case CK_MarkParagraphUp:
3491 case CK_MarkParagraphDown:
3492 /* a mark command with alt-arrow */
3493 case CK_MarkColumnPageUp:
3494 case CK_MarkColumnPageDown:
3495 case CK_MarkColumnLeft:
3496 case CK_MarkColumnRight:
3497 case CK_MarkColumnUp:
3498 case CK_MarkColumnDown:
3499 case CK_MarkColumnScrollUp:
3500 case CK_MarkColumnScrollDown:
3501 case CK_MarkColumnParagraphUp:
3502 case CK_MarkColumnParagraphDown:
3503 edit->column_highlight = 0;
3504 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3506 edit_mark_cmd (edit, 1); /* clear */
3507 edit_mark_cmd (edit, 0); /* marking on */
3509 edit->highlight = 1;
3510 break;
3512 /* any other command */
3513 default:
3514 if (edit->highlight)
3515 edit_mark_cmd (edit, 0); /* clear */
3516 edit->highlight = 0;
3519 /* first check for undo */
3520 if (command == CK_Undo)
3522 edit->redo_stack_reset = 0;
3523 edit_group_undo (edit);
3524 edit->found_len = 0;
3525 edit->prev_col = edit_get_col (edit);
3526 edit->search_start = edit->curs1;
3527 return;
3529 /* check for redo */
3530 if (command == CK_Redo)
3532 edit->redo_stack_reset = 0;
3533 edit_do_redo (edit);
3534 edit->found_len = 0;
3535 edit->prev_col = edit_get_col (edit);
3536 edit->search_start = edit->curs1;
3537 return;
3540 edit->redo_stack_reset = 1;
3542 /* An ordinary key press */
3543 if (char_for_insertion >= 0)
3545 /* if non persistent selection and text selected */
3546 if (!option_persistent_selections)
3548 if (edit->mark1 != edit->mark2)
3549 edit_block_delete_cmd (edit);
3551 if (edit->overwrite)
3553 /* remove char only one time, after input first byte, multibyte chars */
3554 if ((!mc_global.utf8_display || edit->charpoint == 0)
3555 && edit_get_byte (edit, edit->curs1) != '\n')
3556 edit_delete (edit, 0);
3558 if (option_cursor_beyond_eol && edit->over_col > 0)
3559 edit_insert_over (edit);
3560 #ifdef HAVE_CHARSET
3561 if (char_for_insertion > 255 && !mc_global.utf8_display)
3563 unsigned char str[6 + 1];
3564 size_t i = 0;
3565 int res;
3567 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3568 if (res == 0)
3570 str[0] = '.';
3571 str[1] = '\0';
3573 else
3575 str[res] = '\0';
3577 while (str[i] != 0 && i <= 6)
3579 char_for_insertion = str[i];
3580 edit_insert (edit, char_for_insertion);
3581 i++;
3584 else
3585 #endif
3586 edit_insert (edit, char_for_insertion);
3588 if (option_auto_para_formatting)
3590 format_paragraph (edit, 0);
3591 edit->force |= REDRAW_PAGE;
3593 else
3594 check_and_wrap_line (edit);
3595 edit->found_len = 0;
3596 edit->prev_col = edit_get_col (edit);
3597 edit->search_start = edit->curs1;
3598 edit_find_bracket (edit);
3599 return;
3602 switch (command)
3604 case CK_TopOnScreen:
3605 case CK_BottomOnScreen:
3606 case CK_Top:
3607 case CK_Bottom:
3608 case CK_PageUp:
3609 case CK_PageDown:
3610 case CK_Home:
3611 case CK_End:
3612 case CK_Up:
3613 case CK_Down:
3614 case CK_Left:
3615 case CK_Right:
3616 case CK_WordLeft:
3617 case CK_WordRight:
3618 if (edit->mark2 >= 0)
3620 if (!option_persistent_selections)
3622 if (edit->column_highlight)
3623 edit_push_undo_action (edit, COLUMN_ON);
3624 edit->column_highlight = 0;
3625 edit_mark_cmd (edit, 1);
3630 switch (command)
3632 case CK_TopOnScreen:
3633 case CK_BottomOnScreen:
3634 case CK_MarkToPageBegin:
3635 case CK_MarkToPageEnd:
3636 case CK_Up:
3637 case CK_Down:
3638 case CK_WordLeft:
3639 case CK_WordRight:
3640 case CK_MarkToWordBegin:
3641 case CK_MarkToWordEnd:
3642 case CK_MarkUp:
3643 case CK_MarkDown:
3644 case CK_MarkColumnUp:
3645 case CK_MarkColumnDown:
3646 if (edit->mark2 == -1)
3647 break; /*marking is following the cursor: may need to highlight a whole line */
3648 case CK_Left:
3649 case CK_Right:
3650 case CK_MarkLeft:
3651 case CK_MarkRight:
3652 edit->force |= REDRAW_CHAR_ONLY;
3655 /* basic cursor key commands */
3656 switch (command)
3658 case CK_BackSpace:
3659 /* if non persistent selection and text selected */
3660 if (!option_persistent_selections)
3662 if (edit->mark1 != edit->mark2)
3664 edit_block_delete_cmd (edit);
3665 break;
3668 if (option_cursor_beyond_eol && edit->over_col > 0)
3670 edit->over_col--;
3671 break;
3673 if (option_backspace_through_tabs && is_in_indent (edit))
3675 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3676 edit_backspace (edit, 1);
3677 break;
3679 else
3681 if (option_fake_half_tabs)
3683 int i;
3684 if (is_in_indent (edit) && right_of_four_spaces (edit))
3686 for (i = 0; i < HALF_TAB_SIZE; i++)
3687 edit_backspace (edit, 1);
3688 break;
3692 edit_backspace (edit, 0);
3693 break;
3694 case CK_Delete:
3695 /* if non persistent selection and text selected */
3696 if (!option_persistent_selections)
3698 if (edit->mark1 != edit->mark2)
3700 edit_block_delete_cmd (edit);
3701 break;
3705 if (option_cursor_beyond_eol && edit->over_col > 0)
3706 edit_insert_over (edit);
3708 if (option_fake_half_tabs)
3710 int i;
3711 if (is_in_indent (edit) && left_of_four_spaces (edit))
3713 for (i = 1; i <= HALF_TAB_SIZE; i++)
3714 edit_delete (edit, 1);
3715 break;
3718 edit_delete (edit, 0);
3719 break;
3720 case CK_DeleteToWordBegin:
3721 edit->over_col = 0;
3722 edit_left_delete_word (edit);
3723 break;
3724 case CK_DeleteToWordEnd:
3725 if (option_cursor_beyond_eol && edit->over_col > 0)
3726 edit_insert_over (edit);
3728 edit_right_delete_word (edit);
3729 break;
3730 case CK_DeleteLine:
3731 edit_delete_line (edit);
3732 break;
3733 case CK_DeleteToHome:
3734 edit_delete_to_line_begin (edit);
3735 break;
3736 case CK_DeleteToEnd:
3737 edit_delete_to_line_end (edit);
3738 break;
3739 case CK_Enter:
3740 edit->over_col = 0;
3741 if (option_auto_para_formatting)
3743 edit_double_newline (edit);
3744 if (option_return_does_auto_indent)
3745 edit_auto_indent (edit);
3746 format_paragraph (edit, 0);
3748 else
3750 edit_insert (edit, '\n');
3751 if (option_return_does_auto_indent)
3753 edit_auto_indent (edit);
3756 break;
3757 case CK_Return:
3758 edit_insert (edit, '\n');
3759 break;
3761 case CK_MarkColumnPageUp:
3762 edit->column_highlight = 1;
3763 case CK_PageUp:
3764 case CK_MarkPageUp:
3765 edit_move_up (edit, edit->widget.lines - 1, 1);
3766 break;
3767 case CK_MarkColumnPageDown:
3768 edit->column_highlight = 1;
3769 case CK_PageDown:
3770 case CK_MarkPageDown:
3771 edit_move_down (edit, edit->widget.lines - 1, 1);
3772 break;
3773 case CK_MarkColumnLeft:
3774 edit->column_highlight = 1;
3775 case CK_Left:
3776 case CK_MarkLeft:
3777 if (option_fake_half_tabs)
3779 if (is_in_indent (edit) && right_of_four_spaces (edit))
3781 if (option_cursor_beyond_eol && edit->over_col > 0)
3782 edit->over_col--;
3783 else
3784 edit_cursor_move (edit, -HALF_TAB_SIZE);
3785 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3786 break;
3789 edit_left_char_move_cmd (edit);
3790 break;
3791 case CK_MarkColumnRight:
3792 edit->column_highlight = 1;
3793 case CK_Right:
3794 case CK_MarkRight:
3795 if (option_fake_half_tabs)
3797 if (is_in_indent (edit) && left_of_four_spaces (edit))
3799 edit_cursor_move (edit, HALF_TAB_SIZE);
3800 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3801 break;
3804 edit_right_char_move_cmd (edit);
3805 break;
3806 case CK_TopOnScreen:
3807 case CK_MarkToPageBegin:
3808 edit_begin_page (edit);
3809 break;
3810 case CK_BottomOnScreen:
3811 case CK_MarkToPageEnd:
3812 edit_end_page (edit);
3813 break;
3814 case CK_WordLeft:
3815 case CK_MarkToWordBegin:
3816 edit->over_col = 0;
3817 edit_left_word_move_cmd (edit);
3818 break;
3819 case CK_WordRight:
3820 case CK_MarkToWordEnd:
3821 edit->over_col = 0;
3822 edit_right_word_move_cmd (edit);
3823 break;
3824 case CK_MarkColumnUp:
3825 edit->column_highlight = 1;
3826 case CK_Up:
3827 case CK_MarkUp:
3828 edit_move_up (edit, 1, 0);
3829 break;
3830 case CK_MarkColumnDown:
3831 edit->column_highlight = 1;
3832 case CK_Down:
3833 case CK_MarkDown:
3834 edit_move_down (edit, 1, 0);
3835 break;
3836 case CK_MarkColumnParagraphUp:
3837 edit->column_highlight = 1;
3838 case CK_ParagraphUp:
3839 case CK_MarkParagraphUp:
3840 edit_move_up_paragraph (edit, 0);
3841 break;
3842 case CK_MarkColumnParagraphDown:
3843 edit->column_highlight = 1;
3844 case CK_ParagraphDown:
3845 case CK_MarkParagraphDown:
3846 edit_move_down_paragraph (edit, 0);
3847 break;
3848 case CK_MarkColumnScrollUp:
3849 edit->column_highlight = 1;
3850 case CK_ScrollUp:
3851 case CK_MarkScrollUp:
3852 edit_move_up (edit, 1, 1);
3853 break;
3854 case CK_MarkColumnScrollDown:
3855 edit->column_highlight = 1;
3856 case CK_ScrollDown:
3857 case CK_MarkScrollDown:
3858 edit_move_down (edit, 1, 1);
3859 break;
3860 case CK_Home:
3861 case CK_MarkToHome:
3862 edit_cursor_to_bol (edit);
3863 break;
3864 case CK_End:
3865 case CK_MarkToEnd:
3866 edit_cursor_to_eol (edit);
3867 break;
3868 case CK_Tab:
3869 /* if text marked shift block */
3870 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3872 if (edit->mark2 < 0)
3873 edit_mark_cmd (edit, 0);
3874 edit_move_block_to_right (edit);
3876 else
3878 if (option_cursor_beyond_eol)
3879 edit_insert_over (edit);
3880 edit_tab_cmd (edit);
3881 if (option_auto_para_formatting)
3883 format_paragraph (edit, 0);
3884 edit->force |= REDRAW_PAGE;
3886 else
3888 check_and_wrap_line (edit);
3891 break;
3893 case CK_InsertOverwrite:
3894 edit->overwrite = !edit->overwrite;
3895 break;
3897 case CK_Mark:
3898 if (edit->mark2 >= 0)
3900 if (edit->column_highlight)
3901 edit_push_undo_action (edit, COLUMN_ON);
3902 edit->column_highlight = 0;
3904 edit_mark_cmd (edit, 0);
3905 break;
3906 case CK_MarkColumn:
3907 if (!edit->column_highlight)
3908 edit_push_undo_action (edit, COLUMN_OFF);
3909 edit->column_highlight = 1;
3910 edit_mark_cmd (edit, 0);
3911 break;
3912 case CK_MarkAll:
3913 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3914 edit->force |= REDRAW_PAGE;
3915 break;
3916 case CK_Unmark:
3917 if (edit->column_highlight)
3918 edit_push_undo_action (edit, COLUMN_ON);
3919 edit->column_highlight = 0;
3920 edit_mark_cmd (edit, 1);
3921 break;
3922 case CK_MarkWord:
3923 if (edit->column_highlight)
3924 edit_push_undo_action (edit, COLUMN_ON);
3925 edit->column_highlight = 0;
3926 edit_mark_current_word_cmd (edit);
3927 break;
3928 case CK_MarkLine:
3929 if (edit->column_highlight)
3930 edit_push_undo_action (edit, COLUMN_ON);
3931 edit->column_highlight = 0;
3932 edit_mark_current_line_cmd (edit);
3933 break;
3935 case CK_Bookmark:
3936 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3937 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3938 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3939 else
3940 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3941 break;
3942 case CK_BookmarkFlush:
3943 book_mark_flush (edit, BOOK_MARK_COLOR);
3944 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3945 edit->force |= REDRAW_PAGE;
3946 break;
3947 case CK_BookmarkNext:
3948 if (edit->book_mark)
3950 struct _book_mark *p;
3951 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3952 if (p->next)
3954 p = p->next;
3955 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
3956 edit_move_display (edit, p->line - edit->widget.lines / 2);
3957 edit_move_to_line (edit, p->line);
3960 break;
3961 case CK_BookmarkPrev:
3962 if (edit->book_mark)
3964 struct _book_mark *p;
3965 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3966 while (p->line == edit->curs_line)
3967 if (p->prev)
3968 p = p->prev;
3969 if (p->line >= 0)
3971 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
3972 edit_move_display (edit, p->line - edit->widget.lines / 2);
3973 edit_move_to_line (edit, p->line);
3976 break;
3978 case CK_Top:
3979 case CK_MarkToFileBegin:
3980 edit_move_to_top (edit);
3981 break;
3982 case CK_Bottom:
3983 case CK_MarkToFileEnd:
3984 edit_move_to_bottom (edit);
3985 break;
3987 case CK_Copy:
3988 if (option_cursor_beyond_eol && edit->over_col > 0)
3989 edit_insert_over (edit);
3990 edit_block_copy_cmd (edit);
3991 break;
3992 case CK_Remove:
3993 edit_block_delete_cmd (edit);
3994 break;
3995 case CK_Move:
3996 edit_block_move_cmd (edit);
3997 break;
3999 case CK_BlockShiftLeft:
4000 if (edit->mark1 != edit->mark2)
4001 edit_move_block_to_left (edit);
4002 break;
4003 case CK_BlockShiftRight:
4004 if (edit->mark1 != edit->mark2)
4005 edit_move_block_to_right (edit);
4006 break;
4007 case CK_Store:
4008 edit_copy_to_X_buf_cmd (edit);
4009 break;
4010 case CK_Cut:
4011 edit_cut_to_X_buf_cmd (edit);
4012 break;
4013 case CK_Paste:
4014 /* if non persistent selection and text selected */
4015 if (!option_persistent_selections)
4017 if (edit->mark1 != edit->mark2)
4018 edit_block_delete_cmd (edit);
4020 if (option_cursor_beyond_eol && edit->over_col > 0)
4021 edit_insert_over (edit);
4022 edit_paste_from_X_buf_cmd (edit);
4023 break;
4024 case CK_History:
4025 edit_paste_from_history (edit);
4026 break;
4028 case CK_SaveAs:
4029 edit_save_as_cmd (edit);
4030 break;
4031 case CK_Save:
4032 edit_save_confirm_cmd (edit);
4033 break;
4034 case CK_BlockSave:
4035 edit_save_block_cmd (edit);
4036 break;
4037 case CK_InsertFile:
4038 edit_insert_file_cmd (edit);
4039 break;
4041 case CK_FilePrev:
4042 edit_load_back_cmd (edit);
4043 break;
4044 case CK_FileNext:
4045 edit_load_forward_cmd (edit);
4046 break;
4048 case CK_SyntaxChoose:
4049 edit_syntax_dialog (edit);
4050 break;
4052 case CK_Search:
4053 edit_search_cmd (edit, FALSE);
4054 break;
4055 case CK_SearchContinue:
4056 edit_search_cmd (edit, TRUE);
4057 break;
4058 case CK_Replace:
4059 edit_replace_cmd (edit, 0);
4060 break;
4061 case CK_ReplaceContinue:
4062 edit_replace_cmd (edit, 1);
4063 break;
4064 case CK_Complete:
4065 /* if text marked shift block */
4066 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4068 edit_move_block_to_left (edit);
4070 else
4072 edit_complete_word_cmd (edit);
4074 break;
4075 case CK_Find:
4076 edit_get_match_keyword_cmd (edit);
4077 break;
4078 case CK_Date:
4080 char s[BUF_MEDIUM];
4081 /* fool gcc to prevent a Y2K warning */
4082 char time_format[] = "_c";
4083 time_format[0] = '%';
4085 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4086 edit_print_string (edit, s);
4087 edit->force |= REDRAW_PAGE;
4088 break;
4090 break;
4091 case CK_Goto:
4092 edit_goto_cmd (edit);
4093 break;
4094 case CK_ParagraphFormat:
4095 format_paragraph (edit, 1);
4096 edit->force |= REDRAW_PAGE;
4097 break;
4098 case CK_MacroDelete:
4099 edit_delete_macro_cmd (edit);
4100 break;
4101 case CK_MatchBracket:
4102 edit_goto_matching_bracket (edit);
4103 break;
4104 case CK_UserMenu:
4105 user_menu (edit, NULL, -1);
4106 break;
4107 case CK_Sort:
4108 edit_sort_cmd (edit);
4109 break;
4110 case CK_ExternalCommand:
4111 edit_ext_cmd (edit);
4112 break;
4113 case CK_Mail:
4114 edit_mail_dialog (edit);
4115 break;
4116 #ifdef HAVE_CHARSET
4117 case CK_SelectCodepage:
4118 edit_select_codepage_cmd (edit);
4119 break;
4120 #endif
4121 case CK_InsertLiteral:
4122 edit_insert_literal_cmd (edit);
4123 break;
4124 case CK_MacroStartStopRecord:
4125 edit_begin_end_macro_cmd (edit);
4126 break;
4127 case CK_RepeatStartStopRecord:
4128 edit_begin_end_repeat_cmd (edit);
4129 break;
4130 case CK_ExtendedKeyMap:
4131 edit->extmod = TRUE;
4132 break;
4133 default:
4134 break;
4137 /* CK_PipeBlock */
4138 if ((command / CK_PipeBlock (0)) == 1)
4139 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4141 /* keys which must set the col position, and the search vars */
4142 switch (command)
4144 case CK_Search:
4145 case CK_SearchContinue:
4146 case CK_Replace:
4147 case CK_ReplaceContinue:
4148 case CK_Complete:
4149 edit->prev_col = edit_get_col (edit);
4150 break;
4151 case CK_Up:
4152 case CK_MarkUp:
4153 case CK_MarkColumnUp:
4154 case CK_Down:
4155 case CK_MarkDown:
4156 case CK_MarkColumnDown:
4157 case CK_PageUp:
4158 case CK_MarkPageUp:
4159 case CK_MarkColumnPageUp:
4160 case CK_PageDown:
4161 case CK_MarkPageDown:
4162 case CK_MarkColumnPageDown:
4163 case CK_Top:
4164 case CK_MarkToFileBegin:
4165 case CK_Bottom:
4166 case CK_MarkToFileEnd:
4167 case CK_ParagraphUp:
4168 case CK_MarkParagraphUp:
4169 case CK_MarkColumnParagraphUp:
4170 case CK_ParagraphDown:
4171 case CK_MarkParagraphDown:
4172 case CK_MarkColumnParagraphDown:
4173 case CK_ScrollUp:
4174 case CK_MarkScrollUp:
4175 case CK_MarkColumnScrollUp:
4176 case CK_ScrollDown:
4177 case CK_MarkScrollDown:
4178 case CK_MarkColumnScrollDown:
4179 edit->search_start = edit->curs1;
4180 edit->found_len = 0;
4181 break;
4182 default:
4183 edit->found_len = 0;
4184 edit->prev_col = edit_get_col (edit);
4185 edit->search_start = edit->curs1;
4187 edit_find_bracket (edit);
4189 if (option_auto_para_formatting)
4191 switch (command)
4193 case CK_BackSpace:
4194 case CK_Delete:
4195 case CK_DeleteToWordBegin:
4196 case CK_DeleteToWordEnd:
4197 case CK_DeleteToHome:
4198 case CK_DeleteToEnd:
4199 format_paragraph (edit, 0);
4200 edit->force |= REDRAW_PAGE;
4205 /* --------------------------------------------------------------------------------------------- */
4207 void
4208 edit_stack_init (void)
4210 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4212 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4213 edit_history_moveto[edit_stack_iterator].line = -1;
4216 edit_stack_iterator = 0;
4219 /* --------------------------------------------------------------------------------------------- */
4221 void
4222 edit_stack_free (void)
4224 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4225 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4228 /* --------------------------------------------------------------------------------------------- */
4229 /** move i lines */
4231 void
4232 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4234 edit_move_updown (edit, i, do_scroll, TRUE);
4237 /* --------------------------------------------------------------------------------------------- */
4238 /** move i lines */
4240 void
4241 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4243 edit_move_updown (edit, i, do_scroll, FALSE);
4246 /* --------------------------------------------------------------------------------------------- */