edit_load_file_fast(): don't reset utf8 flag that was set early.
[midnight-commander.git] / src / editor / edit.c
blob6f8ca07dde1dcde319374592106020ec228cd5af
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor low level data handling and cursor fundamentals
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "src/charsets.h" /* get_codepage_id */
57 #include "src/main.h" /* source_codepage */
58 #include "src/learn.h" /* learn_keys */
59 #include "src/cmddef.h"
60 #include "src/keybind.h"
62 #include "edit-impl.h"
63 #include "editlock.h"
64 #include "edit-widget.h"
67 int option_word_wrap_line_length = 72;
68 int option_typewriter_wrap = 0;
69 int option_auto_para_formatting = 0;
70 int option_fill_tabs_with_spaces = 0;
71 int option_return_does_auto_indent = 1;
72 int option_backspace_through_tabs = 0;
73 int option_fake_half_tabs = 1;
74 int option_save_mode = EDIT_QUICK_SAVE;
75 int option_save_position = 1;
76 int option_max_undo = 32768;
77 int option_persistent_selections = 1;
78 int option_cursor_beyond_eol = 0;
79 int option_line_state = 0;
80 int option_line_state_width = 0;
82 int option_edit_right_extreme = 0;
83 int option_edit_left_extreme = 0;
84 int option_edit_top_extreme = 0;
85 int option_edit_bottom_extreme = 0;
86 int enable_show_tabs_tws = 1;
87 int option_check_nl_at_eof = 0;
88 int show_right_margin = 0;
90 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
91 char *option_backup_ext = NULL;
93 int edit_stack_iterator = 0;
94 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
95 /* magic sequense for say than block is vertical */
96 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
98 /*-
100 * here's a quick sketch of the layout: (don't run this through indent.)
102 * (b1 is buffers1 and b2 is buffers2)
105 * \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
106 * ______________________________________|______________________________________
108 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
109 * |-> |-> |-> |-> |-> |-> |
111 * _<------------------------->|<----------------->_
112 * WEdit->curs2 | WEdit->curs1
113 * ^ | ^
114 * | ^|^ |
115 * cursor ||| cursor
116 * |||
117 * file end|||file beginning
122 * This_is_some_file
123 * fin.
126 static void user_menu (WEdit * edit);
129 edit_get_byte (WEdit * edit, long byte_index)
131 unsigned long p;
132 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
133 return '\n';
135 if (byte_index >= edit->curs1)
137 p = edit->curs1 + edit->curs2 - byte_index - 1;
138 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
140 else
142 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
146 char *
147 edit_get_byte_ptr (WEdit * edit, long byte_index)
149 unsigned long p;
150 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
151 return NULL;
153 if (byte_index >= edit->curs1)
155 p = edit->curs1 + edit->curs2 - byte_index - 1;
156 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
157 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
159 else
161 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
162 (byte_index & M_EDIT_BUF_SIZE));
166 char *
167 edit_get_buf_ptr (WEdit * edit, long byte_index)
169 unsigned long p;
171 if (byte_index >= (edit->curs1 + edit->curs2))
172 byte_index--;
174 if (byte_index < 0)
175 return NULL;
177 if (byte_index >= edit->curs1)
179 p = edit->curs1 + edit->curs2 - 1;
180 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
181 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
183 else
185 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
190 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
192 gchar *str = NULL;
193 int res = -1;
194 gunichar ch;
195 gchar *next_ch = NULL;
196 int width = 0;
198 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
200 *char_width = 0;
201 return '\n';
204 str = edit_get_byte_ptr (edit, byte_index);
206 if (str == NULL)
208 *char_width = 0;
209 return 0;
212 res = g_utf8_get_char_validated (str, -1);
214 if (res < 0)
216 ch = *str;
217 width = 0;
219 else
221 ch = res;
222 /* Calculate UTF-8 char width */
223 next_ch = g_utf8_next_char (str);
224 if (next_ch)
226 width = next_ch - str;
228 else
230 ch = 0;
231 width = 0;
234 *char_width = width;
235 return ch;
239 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
241 gchar *str, *buf = NULL;
242 int res = -1;
243 gunichar ch;
244 gchar *next_ch = NULL;
245 int width = 0;
247 if (byte_index > 0)
248 byte_index--;
250 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
252 *char_width = 0;
253 return 0;
256 ch = edit_get_utf (edit, byte_index, &width);
258 if (width == 1)
260 *char_width = width;
261 return ch;
264 str = edit_get_byte_ptr (edit, byte_index);
265 buf = edit_get_buf_ptr (edit, byte_index);
266 if (str == NULL || buf == NULL)
268 *char_width = 0;
269 return 0;
271 /* get prev utf8 char */
272 if (str != buf)
273 str = g_utf8_find_prev_char (buf, str);
275 res = g_utf8_get_char_validated (str, -1);
277 if (res < 0)
279 ch = *str;
280 width = 0;
282 else
284 ch = res;
285 /* Calculate UTF-8 char width */
286 next_ch = g_utf8_next_char (str);
287 if (next_ch)
289 width = next_ch - str;
291 else
293 ch = 0;
294 width = 0;
297 *char_width = width;
298 return ch;
302 * Initialize the buffers for an empty files.
304 static void
305 edit_init_buffers (WEdit * edit)
307 int j;
309 for (j = 0; j <= MAXBUFF; j++)
311 edit->buffers1[j] = NULL;
312 edit->buffers2[j] = NULL;
315 edit->curs1 = 0;
316 edit->curs2 = 0;
317 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
321 * Load file OR text into buffers. Set cursor to the beginning of file.
322 * Return 1 on error.
324 static int
325 edit_load_file_fast (WEdit * edit, const char *filename)
327 long buf, buf2;
328 int file = -1;
329 int ret = 1;
331 edit->curs2 = edit->last_byte;
332 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
334 file = mc_open (filename, O_RDONLY | O_BINARY);
335 if (file == -1)
337 GString *errmsg = g_string_new (NULL);
338 g_string_sprintf (errmsg, _(" Cannot open %s for reading "), filename);
339 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
340 g_string_free (errmsg, TRUE);
341 return 1;
344 if (!edit->buffers2[buf2])
345 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
349 if (mc_read (file,
350 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
351 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
352 break;
354 for (buf = buf2 - 1; buf >= 0; buf--)
356 /* edit->buffers2[0] is already allocated */
357 if (!edit->buffers2[buf])
358 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
359 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
360 break;
362 ret = 0;
364 while (0);
365 if (ret)
367 char *err_str = g_strdup_printf (_(" Error reading %s "), filename);
368 edit_error_dialog (_("Error"), err_str);
369 g_free (err_str);
371 mc_close (file);
372 return ret;
375 /* detecting an error on save is easy: just check if every byte has been written. */
376 /* detecting an error on read, is not so easy 'cos there is not way to tell
377 whether you read everything or not. */
378 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
379 static const struct edit_filters
381 const char *read, *write, *extension;
382 } all_filters[] =
384 /* *INDENT-OFF* */
385 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
386 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
387 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
388 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
389 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
390 /* *INDENT-ON* */
393 /* Return index of the filter or -1 is there is no appropriate filter */
394 static int
395 edit_find_filter (const char *filename)
397 size_t i, l, e;
399 if (filename == NULL)
400 return -1;
402 l = strlen (filename);
403 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
405 e = strlen (all_filters[i].extension);
406 if (l > e)
407 if (!strcmp (all_filters[i].extension, filename + l - e))
408 return i;
410 return -1;
413 static char *
414 edit_get_filter (const char *filename)
416 int i;
417 char *p, *quoted_name;
419 i = edit_find_filter (filename);
420 if (i < 0)
421 return NULL;
423 quoted_name = name_quote (filename, 0);
424 p = g_strdup_printf (all_filters[i].read, quoted_name);
425 g_free (quoted_name);
426 return p;
429 char *
430 edit_get_write_filter (const char *write_name, const char *filename)
432 int i;
433 char *p, *writename;
435 i = edit_find_filter (filename);
436 if (i < 0)
437 return NULL;
439 writename = name_quote (write_name, 0);
440 p = g_strdup_printf (all_filters[i].write, writename);
441 g_free (writename);
442 return p;
445 static long
446 edit_insert_stream (WEdit * edit, FILE * f)
448 int c;
449 long i = 0;
450 while ((c = fgetc (f)) >= 0)
452 edit_insert (edit, c);
453 i++;
455 return i;
458 long
459 edit_write_stream (WEdit * edit, FILE * f)
461 long i;
463 if (edit->lb == LB_ASIS)
465 for (i = 0; i < edit->last_byte; i++)
466 if (fputc (edit_get_byte (edit, i), f) < 0)
467 break;
468 return i;
471 /* change line breaks */
472 for (i = 0; i < edit->last_byte; i++)
474 unsigned char c = edit_get_byte (edit, i);
476 if (!(c == '\n' || c == '\r'))
478 /* not line break */
479 if (fputc (c, f) < 0)
480 return i;
482 else
483 { /* (c == '\n' || c == '\r') */
484 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
486 switch (edit->lb)
488 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
489 /* put one line break unconditionally */
490 if (fputc ('\n', f) < 0)
491 return i;
493 i++; /* 2 chars are processed */
495 if (c == '\r' && c1 == '\n')
496 /* Windows line break; go to the next char */
497 break;
499 if (c == '\r' && c1 == '\r')
501 /* two Macintosh line breaks; put second line break */
502 if (fputc ('\n', f) < 0)
503 return i;
504 break;
507 if (fputc (c1, f) < 0)
508 return i;
509 break;
511 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
512 /* put one line break unconditionally */
513 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
514 return i;
516 if (c == '\r' && c1 == '\n')
517 /* Windows line break; go to the next char */
518 i++;
519 break;
521 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
522 /* put one line break unconditionally */
523 if (fputc ('\r', f) < 0)
524 return i;
526 i++; /* 2 chars are processed */
528 if (c == '\r' && c1 == '\n')
529 /* Windows line break; go to the next char */
530 break;
532 if (c == '\n' && c1 == '\n')
534 /* two Windows line breaks; put second line break */
535 if (fputc ('\r', f) < 0)
536 return i;
537 break;
540 if (fputc (c1, f) < 0)
541 return i;
542 break;
543 case LB_ASIS: /* default without changes */
544 break;
549 return edit->last_byte;
552 #define TEMP_BUF_LEN 1024
554 /* inserts a file at the cursor, returns 1 on success */
556 edit_insert_file (WEdit * edit, const char *filename)
558 char *p;
560 p = edit_get_filter (filename);
561 if (p != NULL)
563 FILE *f;
564 long current = edit->curs1;
565 f = (FILE *) popen (p, "r");
566 if (f != NULL)
568 edit_insert_stream (edit, f);
569 edit_cursor_move (edit, current - edit->curs1);
570 if (pclose (f) > 0)
572 char *errmsg;
573 errmsg = g_strdup_printf (_(" Error reading from pipe: %s "), p);
574 edit_error_dialog (_("Error"), errmsg);
575 g_free (errmsg);
576 g_free (p);
577 return 0;
580 else
582 char *errmsg;
583 errmsg = g_strdup_printf (_(" Cannot open pipe for reading: %s "), p);
584 edit_error_dialog (_("Error"), errmsg);
585 g_free (errmsg);
586 g_free (p);
587 return 0;
589 g_free (p);
591 else
593 int i, file, blocklen;
594 long current = edit->curs1;
595 int vertical_insertion = 0;
596 char *buf;
597 file = mc_open (filename, O_RDONLY | O_BINARY);
598 if (file == -1)
599 return 0;
600 buf = g_malloc0 (TEMP_BUF_LEN);
601 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
602 if (blocklen > 0)
604 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
605 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
606 vertical_insertion = 1;
607 else
608 mc_lseek (file, 0, SEEK_SET);
610 if (vertical_insertion)
611 blocklen = edit_insert_column_of_text_from_file (edit, file);
612 else
613 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
614 for (i = 0; i < blocklen; i++)
615 edit_insert (edit, buf[i]);
616 edit_cursor_move (edit, current - edit->curs1);
617 g_free (buf);
618 mc_close (file);
619 if (blocklen != 0)
620 return 0;
622 return 1;
625 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
626 static int
627 check_file_access (WEdit * edit, const char *filename, struct stat *st)
629 int file;
630 GString *errmsg = (GString *) 0;
632 /* Try opening an existing file */
633 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
635 if (file < 0)
638 * Try creating the file. O_EXCL prevents following broken links
639 * and opening existing files.
641 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
642 if (file < 0)
644 g_string_sprintf (errmsg = g_string_new (NULL),
645 _(" Cannot open %s for reading "), filename);
646 goto cleanup;
648 else
650 /* New file, delete it if it's not modified or saved */
651 edit->delete_file = 1;
655 /* Check what we have opened */
656 if (mc_fstat (file, st) < 0)
658 g_string_sprintf (errmsg = g_string_new (NULL),
659 _(" Cannot get size/permissions for %s "), filename);
660 goto cleanup;
663 /* We want to open regular files only */
664 if (!S_ISREG (st->st_mode))
666 g_string_sprintf (errmsg = g_string_new (NULL), _(" %s is not a regular file "), filename);
667 goto cleanup;
671 * Don't delete non-empty files.
672 * O_EXCL should prevent it, but let's be on the safe side.
674 if (st->st_size > 0)
676 edit->delete_file = 0;
679 if (st->st_size >= SIZE_LIMIT)
681 g_string_sprintf (errmsg = g_string_new (NULL), _(" File %s is too large "), filename);
684 cleanup:
685 (void) mc_close (file);
686 if (errmsg)
688 edit_error_dialog (_("Error"), errmsg->str);
689 g_string_free (errmsg, TRUE);
690 return 1;
692 return 0;
696 * Open the file and load it into the buffers, either directly or using
697 * a filter. Return 0 on success, 1 on error.
699 * Fast loading (edit_load_file_fast) is used when the file size is
700 * known. In this case the data is read into the buffers by blocks.
701 * If the file size is not known, the data is loaded byte by byte in
702 * edit_insert_file.
704 static int
705 edit_load_file (WEdit * edit)
707 int fast_load = 1;
709 /* Cannot do fast load if a filter is used */
710 if (edit_find_filter (edit->filename) >= 0)
711 fast_load = 0;
714 * VFS may report file size incorrectly, and slow load is not a big
715 * deal considering overhead in VFS.
717 if (!vfs_file_is_local (edit->filename))
718 fast_load = 0;
721 * FIXME: line end translation should disable fast loading as well
722 * Consider doing fseek() to the end and ftell() for the real size.
725 if (*edit->filename)
727 /* If we are dealing with a real file, check that it exists */
728 if (check_file_access (edit, edit->filename, &edit->stat1))
729 return 1;
731 else
733 /* nothing to load */
734 fast_load = 0;
737 edit_init_buffers (edit);
739 if (fast_load)
741 edit->last_byte = edit->stat1.st_size;
742 edit_load_file_fast (edit, edit->filename);
743 /* If fast load was used, the number of lines wasn't calculated */
744 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
746 else
748 #ifdef HAVE_CHARSET
749 const char *codepage_id;
750 #endif
751 edit->last_byte = 0;
752 if (*edit->filename)
754 edit->stack_disable = 1;
755 if (!edit_insert_file (edit, edit->filename))
757 edit_clean (edit);
758 return 1;
760 edit->stack_disable = 0;
763 #ifdef HAVE_CHARSET
764 codepage_id = get_codepage_id (source_codepage);
765 if (codepage_id)
766 edit->utf8 = str_isutf8 (codepage_id);
767 #endif
769 edit->lb = LB_ASIS;
770 return 0;
773 /* Restore saved cursor position in the file */
774 static void
775 edit_load_position (WEdit * edit)
777 char *filename;
778 long line, column;
779 off_t offset;
781 if (!edit->filename || !*edit->filename)
782 return;
784 filename = vfs_canon (edit->filename);
785 load_file_position (filename, &line, &column, &offset);
786 g_free (filename);
788 if (line > 0)
790 edit_move_to_line (edit, line - 1);
791 edit->prev_col = column;
793 else if (offset > 0)
795 edit_cursor_move (edit, offset);
796 line = edit->curs_line;
798 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
799 edit_move_display (edit, line - (edit->num_widget_lines / 2));
802 /* Save cursor position in the file */
803 static void
804 edit_save_position (WEdit * edit)
806 char *filename;
808 if (!edit->filename || !*edit->filename)
809 return;
811 filename = vfs_canon (edit->filename);
812 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
813 g_free (filename);
816 /* Clean the WEdit stricture except the widget part */
817 static void
818 edit_purge_widget (WEdit * edit)
820 size_t len = sizeof (WEdit) - sizeof (Widget);
821 char *start = (char *) edit + sizeof (Widget);
822 memset (start, 0, len);
823 edit->macro_i = -1; /* not recording a macro */
826 static void
827 edit_set_keymap (void)
829 editor_map = default_editor_keymap;
830 if (editor_keymap && editor_keymap->len > 0)
831 editor_map = (global_keymap_t *) editor_keymap->data;
833 editor_x_map = default_editor_x_keymap;
834 if (editor_x_keymap && editor_x_keymap->len > 0)
835 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
839 #define space_width 1
842 * Fill in the edit structure. Return NULL on failure. Pass edit as
843 * NULL to allocate a new structure.
845 * If line is 0, try to restore saved position. Otherwise put the
846 * cursor on that line and show it in the middle of the screen.
848 WEdit *
849 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
851 int to_free = 0;
852 option_auto_syntax = 1; /* Resetting to auto on every invokation */
853 if (option_line_state)
855 option_line_state_width = LINE_STATE_WIDTH;
857 else
859 option_line_state_width = 0;
861 if (!edit)
863 #ifdef ENABLE_NLS
865 * Expand option_whole_chars_search by national letters using
866 * current locale
869 static char option_whole_chars_search_buf[256];
871 if (option_whole_chars_search_buf != option_whole_chars_search)
873 size_t i;
874 size_t len = str_term_width1 (option_whole_chars_search);
876 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
878 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
880 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
882 option_whole_chars_search_buf[len++] = i;
886 option_whole_chars_search_buf[len] = 0;
887 option_whole_chars_search = option_whole_chars_search_buf;
889 #endif /* ENABLE_NLS */
890 edit = g_malloc0 (sizeof (WEdit));
891 edit->search = NULL;
892 to_free = 1;
894 edit_purge_widget (edit);
895 edit->num_widget_lines = lines;
896 edit->over_col = 0;
897 edit->num_widget_columns = columns;
898 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
899 edit->stat1.st_uid = getuid ();
900 edit->stat1.st_gid = getgid ();
901 edit->stat1.st_mtime = 0;
902 edit->bracket = -1;
903 edit->force |= REDRAW_PAGE;
904 edit_set_filename (edit, filename);
905 edit->stack_size = START_STACK_SIZE;
906 edit->stack_size_mask = START_STACK_SIZE - 1;
907 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
909 edit->utf8 = 0;
910 edit->converter = str_cnv_from_term;
911 edit_set_codeset (edit);
913 if (edit_load_file (edit))
915 /* edit_load_file already gives an error message */
916 if (to_free)
917 g_free (edit);
918 return 0;
921 edit->loading_done = 1;
922 edit->modified = 0;
923 edit->locked = 0;
924 edit_load_syntax (edit, NULL, NULL);
926 int color;
927 edit_get_syntax_color (edit, -1, &color);
930 /* load saved cursor position */
931 if ((line == 0) && option_save_position)
933 edit_load_position (edit);
935 else
937 if (line <= 0)
938 line = 1;
939 edit_move_display (edit, line - 1);
940 edit_move_to_line (edit, line - 1);
943 edit_set_keymap ();
945 return edit;
948 /* Clear the edit struct, freeing everything in it. Return 1 on success */
950 edit_clean (WEdit * edit)
952 int j = 0;
954 if (!edit)
955 return 0;
957 /* a stale lock, remove it */
958 if (edit->locked)
959 edit->locked = edit_unlock_file (edit->filename);
961 /* save cursor position */
962 if (option_save_position)
963 edit_save_position (edit);
965 /* File specified on the mcedit command line and never saved */
966 if (edit->delete_file)
967 unlink (edit->filename);
969 edit_free_syntax_rules (edit);
970 book_mark_flush (edit, -1);
971 for (; j <= MAXBUFF; j++)
973 g_free (edit->buffers1[j]);
974 g_free (edit->buffers2[j]);
977 g_free (edit->undo_stack);
978 g_free (edit->filename);
979 g_free (edit->dir);
981 mc_search_free (edit->search);
982 edit->search = NULL;
984 if (edit->converter != str_cnv_from_term)
985 str_close_conv (edit->converter);
987 edit_purge_widget (edit);
989 return 1;
992 /* returns 1 on success */
994 edit_renew (WEdit * edit)
996 int lines = edit->num_widget_lines;
997 int columns = edit->num_widget_columns;
999 edit_clean (edit);
1000 return (edit_init (edit, lines, columns, "", 0) != NULL);
1004 * Load a new file into the editor. If it fails, preserve the old file.
1005 * To do it, allocate a new widget, initialize it and, if the new file
1006 * was loaded, copy the data to the old widget.
1007 * Return 1 on success, 0 on failure.
1010 edit_reload (WEdit * edit, const char *filename)
1012 WEdit *e;
1013 int lines = edit->num_widget_lines;
1014 int columns = edit->num_widget_columns;
1016 e = g_malloc0 (sizeof (WEdit));
1017 e->widget = edit->widget;
1018 if (!edit_init (e, lines, columns, filename, 0))
1020 g_free (e);
1021 return 0;
1023 edit_clean (edit);
1024 memcpy (edit, e, sizeof (WEdit));
1025 g_free (e);
1026 return 1;
1030 * Load a new file into the editor and set line. If it fails, preserve the old file.
1031 * To do it, allocate a new widget, initialize it and, if the new file
1032 * was loaded, copy the data to the old widget.
1033 * Return 1 on success, 0 on failure.
1036 edit_reload_line (WEdit * edit, const char *filename, long line)
1038 WEdit *e;
1039 int lines = edit->num_widget_lines;
1040 int columns = edit->num_widget_columns;
1042 e = g_malloc0 (sizeof (WEdit));
1043 e->widget = edit->widget;
1044 if (!edit_init (e, lines, columns, filename, line))
1046 g_free (e);
1047 return 0;
1049 edit_clean (edit);
1050 memcpy (edit, e, sizeof (WEdit));
1051 g_free (e);
1052 return 1;
1055 void
1056 edit_set_codeset (WEdit *edit)
1058 #ifdef HAVE_CHARSET
1059 const char *cp_id;
1061 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
1063 if (cp_id != NULL)
1065 GIConv conv;
1066 conv = str_crt_conv_from (cp_id);
1067 if (conv != INVALID_CONV)
1069 if (edit->converter != str_cnv_from_term)
1070 str_close_conv (edit->converter);
1071 edit->converter = conv;
1075 if (cp_id != NULL)
1076 edit->utf8 = str_isutf8 (cp_id);
1077 #else
1078 (void) edit;
1079 #endif
1084 Recording stack for undo:
1085 The following is an implementation of a compressed stack. Identical
1086 pushes are recorded by a negative prefix indicating the number of times the
1087 same char was pushed. This saves space for repeated curs-left or curs-right
1088 delete etc.
1092 pushed: stored:
1096 b -3
1098 c --> -4
1104 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1105 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1106 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1107 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1108 position.
1110 The only way the cursor moves or the buffer is changed is through the routines:
1111 insert, backspace, insert_ahead, delete, and cursor_move.
1112 These record the reverse undo movements onto the stack each time they are
1113 called.
1115 Each key press results in a set of actions (insert; delete ...). So each time
1116 a key is pressed the current position of start_display is pushed as
1117 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1118 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1119 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1123 void
1124 edit_push_action (WEdit * edit, long c, ...)
1126 unsigned long sp = edit->stack_pointer;
1127 unsigned long spm1;
1128 long *t;
1130 /* first enlarge the stack if necessary */
1131 if (sp > edit->stack_size - 10)
1132 { /* say */
1133 if (option_max_undo < 256)
1134 option_max_undo = 256;
1135 if (edit->stack_size < (unsigned long) option_max_undo)
1137 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1138 if (t)
1140 edit->undo_stack = t;
1141 edit->stack_size <<= 1;
1142 edit->stack_size_mask = edit->stack_size - 1;
1146 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1147 if (edit->stack_disable)
1148 return;
1150 #ifdef FAST_MOVE_CURSOR
1151 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
1153 va_list ap;
1154 edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
1155 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1156 va_start (ap, c);
1157 c = -(va_arg (ap, int));
1158 va_end (ap);
1160 else
1161 #endif /* ! FAST_MOVE_CURSOR */
1162 if (edit->stack_bottom != sp
1163 && spm1 != edit->stack_bottom
1164 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
1166 int d;
1167 if (edit->undo_stack[spm1] < 0)
1169 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1170 if (d == c)
1172 if (edit->undo_stack[spm1] > -1000000000)
1174 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1175 edit->undo_stack[spm1]--;
1176 return;
1179 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1180 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1181 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1182 { /* a left then a right anihilate each other */
1183 if (edit->undo_stack[spm1] == -2)
1184 edit->stack_pointer = spm1;
1185 else
1186 edit->undo_stack[spm1]++;
1187 return;
1189 #endif
1191 else
1193 d = edit->undo_stack[spm1];
1194 if (d == c)
1196 if (c >= KEY_PRESS)
1197 return; /* --> no need to push multiple do-nothings */
1198 edit->undo_stack[sp] = -2;
1199 goto check_bottom;
1201 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1202 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1203 { /* a left then a right anihilate each other */
1204 edit->stack_pointer = spm1;
1205 return;
1207 #endif
1210 edit->undo_stack[sp] = c;
1212 check_bottom:
1213 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1215 /* if the sp wraps round and catches the stack_bottom then erase
1216 * the first set of actions on the stack to make space - by moving
1217 * stack_bottom forward one "key press" */
1218 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1219 if ((unsigned long) c == edit->stack_bottom ||
1220 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1223 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1225 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
1226 && edit->stack_bottom != edit->stack_pointer);
1228 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
1229 if (edit->stack_pointer != edit->stack_bottom
1230 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1231 edit->stack_bottom = edit->stack_pointer = 0;
1235 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1236 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1238 static long
1239 pop_action (WEdit * edit)
1241 long c;
1242 unsigned long sp = edit->stack_pointer;
1244 if (sp == edit->stack_bottom)
1245 return STACK_BOTTOM;
1247 sp = (sp - 1) & edit->stack_size_mask;
1248 c = edit->undo_stack[sp];
1249 if (c >= 0)
1251 /* edit->undo_stack[sp] = '@'; */
1252 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1253 return c;
1256 if (sp == edit->stack_bottom)
1257 return STACK_BOTTOM;
1259 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1260 if (edit->undo_stack[sp] == -2)
1262 /* edit->undo_stack[sp] = '@'; */
1263 edit->stack_pointer = sp;
1265 else
1266 edit->undo_stack[sp]++;
1268 return c;
1271 /* is called whenever a modification is made by one of the four routines below */
1272 static void
1273 edit_modification (WEdit * edit)
1275 edit->caches_valid = 0;
1276 edit->screen_modified = 1;
1278 /* raise lock when file modified */
1279 if (!edit->modified && !edit->delete_file)
1280 edit->locked = edit_lock_file (edit->filename);
1281 edit->modified = 1;
1285 Basic low level single character buffer alterations and movements at the cursor.
1286 Returns char passed over, inserted or removed.
1289 void
1290 edit_insert (WEdit * edit, int c)
1292 /* check if file has grown to large */
1293 if (edit->last_byte >= SIZE_LIMIT)
1294 return;
1296 /* first we must update the position of the display window */
1297 if (edit->curs1 < edit->start_display)
1299 edit->start_display++;
1300 if (c == '\n')
1301 edit->start_line++;
1304 /* Mark file as modified, unless the file hasn't been fully loaded */
1305 if (edit->loading_done)
1307 edit_modification (edit);
1310 /* now we must update some info on the file and check if a redraw is required */
1311 if (c == '\n')
1313 if (edit->book_mark)
1314 book_mark_inc (edit, edit->curs_line);
1315 edit->curs_line++;
1316 edit->total_lines++;
1317 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1320 /* save the reverse command onto the undo stack */
1321 edit_push_action (edit, BACKSPACE);
1323 /* update markers */
1324 edit->mark1 += (edit->mark1 > edit->curs1);
1325 edit->mark2 += (edit->mark2 > edit->curs1);
1326 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1328 /* add a new buffer if we've reached the end of the last one */
1329 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1330 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1332 /* perform the insertion */
1333 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
1334 = (unsigned char) c;
1336 /* update file length */
1337 edit->last_byte++;
1339 /* update cursor position */
1340 edit->curs1++;
1343 static void
1344 edit_insert_over (WEdit * edit)
1346 int i;
1348 for (i = 0; i < edit->over_col; i++)
1350 edit_insert (edit, ' ');
1352 edit->over_col = 0;
1355 /* same as edit_insert and move left */
1356 void
1357 edit_insert_ahead (WEdit * edit, int c)
1359 if (edit->last_byte >= SIZE_LIMIT)
1360 return;
1362 if (edit->curs1 < edit->start_display)
1364 edit->start_display++;
1365 if (c == '\n')
1366 edit->start_line++;
1368 edit_modification (edit);
1369 if (c == '\n')
1371 if (edit->book_mark)
1372 book_mark_inc (edit, edit->curs_line);
1373 edit->total_lines++;
1374 edit->force |= REDRAW_AFTER_CURSOR;
1376 edit_push_action (edit, DELCHAR);
1378 edit->mark1 += (edit->mark1 >= edit->curs1);
1379 edit->mark2 += (edit->mark2 >= edit->curs1);
1380 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1382 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1383 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1384 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
1385 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1387 edit->last_byte++;
1388 edit->curs2++;
1393 edit_delete (WEdit * edit, const int byte_delete)
1395 int p = 0;
1396 int cw = 1;
1397 int i;
1399 if (!edit->curs2)
1400 return 0;
1402 edit->mark1 -= (edit->mark1 > edit->curs1);
1403 edit->mark2 -= (edit->mark2 > edit->curs1);
1404 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1406 cw = 1;
1407 /* if byte_delete = 1 then delete only one byte not multibyte char */
1408 if (edit->utf8 && byte_delete == 0)
1410 edit_get_utf (edit, edit->curs1, &cw);
1411 if (cw < 1)
1412 cw = 1;
1414 for (i = 1; i <= cw; i++)
1416 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1417 ((edit->curs2 -
1418 1) & M_EDIT_BUF_SIZE) - 1];
1420 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1422 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1423 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1425 edit->last_byte--;
1426 edit->curs2--;
1427 edit_push_action (edit, p + 256);
1430 edit_modification (edit);
1431 if (p == '\n')
1433 if (edit->book_mark)
1434 book_mark_dec (edit, edit->curs_line);
1435 edit->total_lines--;
1436 edit->force |= REDRAW_AFTER_CURSOR;
1438 if (edit->curs1 < edit->start_display)
1440 edit->start_display--;
1441 if (p == '\n')
1442 edit->start_line--;
1445 return p;
1449 static int
1450 edit_backspace (WEdit * edit, const int byte_delete)
1452 int p = 0;
1453 int cw = 1;
1454 int i;
1456 if (!edit->curs1)
1457 return 0;
1459 edit->mark1 -= (edit->mark1 >= edit->curs1);
1460 edit->mark2 -= (edit->mark2 >= edit->curs1);
1461 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1463 cw = 1;
1464 if (edit->utf8 && byte_delete == 0)
1466 edit_get_prev_utf (edit, edit->curs1, &cw);
1467 if (cw < 1)
1468 cw = 1;
1470 for (i = 1; i <= cw; i++)
1472 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
1473 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1474 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1476 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1477 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1479 edit->last_byte--;
1480 edit->curs1--;
1481 edit_push_action (edit, p);
1483 edit_modification (edit);
1484 if (p == '\n')
1486 if (edit->book_mark)
1487 book_mark_dec (edit, edit->curs_line);
1488 edit->curs_line--;
1489 edit->total_lines--;
1490 edit->force |= REDRAW_AFTER_CURSOR;
1493 if (edit->curs1 < edit->start_display)
1495 edit->start_display--;
1496 if (p == '\n')
1497 edit->start_line--;
1500 return p;
1503 #ifdef FAST_MOVE_CURSOR
1505 static void
1506 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1508 unsigned long next;
1509 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
1511 edit->curs_line--;
1512 next -= (unsigned long) dest;
1513 n -= next;
1514 src += next;
1515 dest += next;
1520 edit_move_backward_lots (WEdit * edit, long increment)
1522 int r, s, t;
1523 unsigned char *p = NULL;
1525 if (increment > edit->curs1)
1526 increment = edit->curs1;
1527 if (increment <= 0)
1528 return -1;
1529 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1531 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1532 if (r > increment)
1533 r = increment;
1534 s = edit->curs1 & M_EDIT_BUF_SIZE;
1536 if (s > r)
1538 memqcpy (edit,
1539 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1540 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
1542 else
1544 if (s != 0)
1546 memqcpy (edit,
1547 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1548 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1549 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1550 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1552 memqcpy (edit,
1553 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1554 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1555 EDIT_BUF_SIZE - (r - s), r - s);
1557 increment -= r;
1558 edit->curs1 -= r;
1559 edit->curs2 += r;
1560 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1562 if (p)
1563 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1564 else
1565 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1567 else
1569 g_free (p);
1572 s = edit->curs1 & M_EDIT_BUF_SIZE;
1573 while (increment)
1575 p = 0;
1576 r = EDIT_BUF_SIZE;
1577 if (r > increment)
1578 r = increment;
1579 t = s;
1580 if (r < t)
1581 t = r;
1582 memqcpy (edit,
1583 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1584 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
1585 if (r >= s)
1587 if (t)
1589 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1590 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1592 memqcpy (edit,
1593 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1594 EDIT_BUF_SIZE - r,
1595 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1596 EDIT_BUF_SIZE - (r - s), r - s);
1598 increment -= r;
1599 edit->curs1 -= r;
1600 edit->curs2 += r;
1601 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1603 if (p)
1604 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1605 else
1606 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1608 else
1609 g_free (p);
1611 return edit_get_byte (edit, edit->curs1);
1614 #endif /* ! FAST_MOVE_CURSOR */
1616 /* moves the cursor right or left: increment positive or negative respectively */
1617 void
1618 edit_cursor_move (WEdit * edit, long increment)
1620 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1621 int c;
1622 #ifdef FAST_MOVE_CURSOR
1623 if (increment < -256)
1625 edit->force |= REDRAW_PAGE;
1626 edit_move_backward_lots (edit, -increment);
1627 return;
1629 #endif /* ! FAST_MOVE_CURSOR */
1631 if (increment < 0)
1633 for (; increment < 0; increment++)
1635 if (!edit->curs1)
1636 return;
1638 edit_push_action (edit, CURS_RIGHT);
1640 c = edit_get_byte (edit, edit->curs1 - 1);
1641 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1642 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1643 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1644 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1645 edit->curs2++;
1646 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
1647 1) & M_EDIT_BUF_SIZE];
1648 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1650 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1651 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1653 edit->curs1--;
1654 if (c == '\n')
1656 edit->curs_line--;
1657 edit->force |= REDRAW_LINE_BELOW;
1662 else if (increment > 0)
1664 for (; increment > 0; increment--)
1666 if (!edit->curs2)
1667 return;
1669 edit_push_action (edit, CURS_LEFT);
1671 c = edit_get_byte (edit, edit->curs1);
1672 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1673 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1674 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1675 edit->curs1++;
1676 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1677 ((edit->curs2 -
1678 1) & M_EDIT_BUF_SIZE) - 1];
1679 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1681 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1682 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1684 edit->curs2--;
1685 if (c == '\n')
1687 edit->curs_line++;
1688 edit->force |= REDRAW_LINE_ABOVE;
1694 /* These functions return positions relative to lines */
1696 /* returns index of last char on line + 1 */
1697 long
1698 edit_eol (WEdit * edit, long current)
1700 if (current >= edit->last_byte)
1701 return edit->last_byte;
1703 for (;; current++)
1704 if (edit_get_byte (edit, current) == '\n')
1705 break;
1706 return current;
1709 /* returns index of first char on line */
1710 long
1711 edit_bol (WEdit * edit, long current)
1713 if (current <= 0)
1714 return 0;
1716 for (;; current--)
1717 if (edit_get_byte (edit, current - 1) == '\n')
1718 break;
1719 return current;
1723 long
1724 edit_count_lines (WEdit * edit, long current, long upto)
1726 long lines = 0;
1727 if (upto > edit->last_byte)
1728 upto = edit->last_byte;
1729 if (current < 0)
1730 current = 0;
1731 while (current < upto)
1732 if (edit_get_byte (edit, current++) == '\n')
1733 lines++;
1734 return lines;
1738 /* If lines is zero this returns the count of lines from current to upto. */
1739 /* If upto is zero returns index of lines forward current. */
1740 long
1741 edit_move_forward (WEdit * edit, long current, long lines, long upto)
1743 if (upto)
1745 return edit_count_lines (edit, current, upto);
1747 else
1749 long next;
1750 if (lines < 0)
1751 lines = 0;
1752 while (lines--)
1754 next = edit_eol (edit, current) + 1;
1755 if (next > edit->last_byte)
1756 break;
1757 else
1758 current = next;
1760 return current;
1765 /* Returns offset of 'lines' lines up from current */
1766 long
1767 edit_move_backward (WEdit * edit, long current, long lines)
1769 if (lines < 0)
1770 lines = 0;
1771 current = edit_bol (edit, current);
1772 while ((lines--) && current != 0)
1773 current = edit_bol (edit, current - 1);
1774 return current;
1777 /* If cols is zero this returns the count of columns from current to upto. */
1778 /* If upto is zero returns index of cols across from current. */
1779 long
1780 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1782 long p, q;
1783 int col;
1785 if (upto)
1787 q = upto;
1788 cols = -10;
1790 else
1791 q = edit->last_byte + 2;
1793 for (col = 0, p = current; p < q; p++)
1795 int c, orig_c;
1796 int utf_ch = 0;
1797 #ifdef HAVE_CHARSET
1798 int cw = 1;
1799 #endif
1800 if (cols != -10)
1802 if (col == cols)
1803 return p;
1804 if (col > cols)
1805 return p - 1;
1807 orig_c = c = edit_get_byte (edit, p);
1808 #ifdef HAVE_CHARSET
1809 if (edit->utf8)
1811 utf_ch = edit_get_utf (edit, p, &cw);
1812 if (utf8_display)
1814 if (cw > 1)
1815 col -= cw - 1;
1816 if (g_unichar_iswide (utf_ch))
1817 col++;
1819 else if (cw > 1 && g_unichar_isprint (utf_ch))
1820 col -= cw - 1;
1822 #endif
1823 c = convert_to_display_c (c);
1824 if (c == '\t')
1825 col += TAB_SIZE - col % TAB_SIZE;
1826 else if (c == '\n')
1828 if (upto)
1829 return col;
1830 else
1831 return p;
1833 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1834 /* '\r' is shown as ^M, so we must advance 2 characters */
1835 /* Caret notation for control characters */
1836 col += 2;
1837 else
1838 col++;
1840 return col;
1843 /* returns the current column position of the cursor */
1845 edit_get_col (WEdit * edit)
1847 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1851 /* Scrolling functions */
1853 void
1854 edit_update_curs_row (WEdit * edit)
1856 edit->curs_row = edit->curs_line - edit->start_line;
1859 void
1860 edit_update_curs_col (WEdit * edit)
1862 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1866 edit_get_curs_col (const WEdit * edit)
1868 return edit->curs_col;
1871 /*moves the display start position up by i lines */
1872 void
1873 edit_scroll_upward (WEdit * edit, unsigned long i)
1875 unsigned long lines_above = edit->start_line;
1876 if (i > lines_above)
1877 i = lines_above;
1878 if (i)
1880 edit->start_line -= i;
1881 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1882 edit->force |= REDRAW_PAGE;
1883 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1885 edit_update_curs_row (edit);
1889 /* returns 1 if could scroll, 0 otherwise */
1890 void
1891 edit_scroll_downward (WEdit * edit, int i)
1893 int lines_below;
1894 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1895 if (lines_below > 0)
1897 if (i > lines_below)
1898 i = lines_below;
1899 edit->start_line += i;
1900 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1901 edit->force |= REDRAW_PAGE;
1902 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1904 edit_update_curs_row (edit);
1907 void
1908 edit_scroll_right (WEdit * edit, int i)
1910 edit->force |= REDRAW_PAGE;
1911 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1912 edit->start_col -= i;
1915 void
1916 edit_scroll_left (WEdit * edit, int i)
1918 if (edit->start_col)
1920 edit->start_col += i;
1921 if (edit->start_col > 0)
1922 edit->start_col = 0;
1923 edit->force |= REDRAW_PAGE;
1924 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1928 /* high level cursor movement commands */
1930 static int
1931 is_in_indent (WEdit * edit)
1933 long p = edit_bol (edit, edit->curs1);
1934 while (p < edit->curs1)
1935 if (!strchr (" \t", edit_get_byte (edit, p++)))
1936 return 0;
1937 return 1;
1940 static int left_of_four_spaces (WEdit * edit);
1942 void
1943 edit_move_to_prev_col (WEdit * edit, long p)
1945 int prev = edit->prev_col;
1946 int over = edit->over_col;
1947 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1949 if (option_cursor_beyond_eol)
1951 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
1952 edit_eol (edit, edit->curs1));
1954 if (line_len < prev + edit->over_col)
1956 edit->over_col = prev + over - line_len;
1957 edit->prev_col = line_len;
1958 edit->curs_col = line_len;
1960 else
1962 edit->curs_col = prev + over;
1963 edit->prev_col = edit->curs_col;
1964 edit->over_col = 0;
1967 else
1969 edit->over_col = 0;
1970 if (is_in_indent (edit) && option_fake_half_tabs)
1972 edit_update_curs_col (edit);
1973 if (space_width)
1974 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
1976 int q = edit->curs_col;
1977 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1978 p = edit_bol (edit, edit->curs1);
1979 edit_cursor_move (edit,
1980 edit_move_forward3 (edit, p, edit->curs_col,
1981 0) - edit->curs1);
1982 if (!left_of_four_spaces (edit))
1983 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1989 static int
1990 is_blank (WEdit * edit, long offset)
1992 long s, f;
1993 int c;
1994 s = edit_bol (edit, offset);
1995 f = edit_eol (edit, offset) - 1;
1996 while (s <= f)
1998 c = edit_get_byte (edit, s++);
1999 if (!isspace (c))
2000 return 0;
2002 return 1;
2006 /* returns the offset of line i */
2007 static long
2008 edit_find_line (WEdit * edit, int line)
2010 int i, j = 0;
2011 int m = 2000000000;
2012 if (!edit->caches_valid)
2014 for (i = 0; i < N_LINE_CACHES; i++)
2015 edit->line_numbers[i] = edit->line_offsets[i] = 0;
2016 /* three offsets that we *know* are line 0 at 0 and these two: */
2017 edit->line_numbers[1] = edit->curs_line;
2018 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
2019 edit->line_numbers[2] = edit->total_lines;
2020 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
2021 edit->caches_valid = 1;
2023 if (line >= edit->total_lines)
2024 return edit->line_offsets[2];
2025 if (line <= 0)
2026 return 0;
2027 /* find the closest known point */
2028 for (i = 0; i < N_LINE_CACHES; i++)
2030 int n;
2031 n = abs (edit->line_numbers[i] - line);
2032 if (n < m)
2034 m = n;
2035 j = i;
2038 if (m == 0)
2039 return edit->line_offsets[j]; /* know the offset exactly */
2040 if (m == 1 && j >= 3)
2041 i = j; /* one line different - caller might be looping, so stay in this cache */
2042 else
2043 i = 3 + (rand () % (N_LINE_CACHES - 3));
2044 if (line > edit->line_numbers[j])
2045 edit->line_offsets[i] =
2046 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
2047 else
2048 edit->line_offsets[i] =
2049 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
2050 edit->line_numbers[i] = line;
2051 return edit->line_offsets[i];
2055 line_is_blank (WEdit * edit, long line)
2057 return is_blank (edit, edit_find_line (edit, line));
2060 /* moves up until a blank line is reached, or until just
2061 before a non-blank line is reached */
2062 static void
2063 edit_move_up_paragraph (WEdit * edit, int do_scroll)
2065 int i = 0;
2066 if (edit->curs_line > 1)
2068 if (line_is_blank (edit, edit->curs_line))
2070 if (line_is_blank (edit, edit->curs_line - 1))
2072 for (i = edit->curs_line - 1; i; i--)
2073 if (!line_is_blank (edit, i))
2075 i++;
2076 break;
2079 else
2081 for (i = edit->curs_line - 1; i; i--)
2082 if (line_is_blank (edit, i))
2083 break;
2086 else
2088 for (i = edit->curs_line - 1; i; i--)
2089 if (line_is_blank (edit, i))
2090 break;
2093 edit_move_up (edit, edit->curs_line - i, do_scroll);
2096 /* moves down until a blank line is reached, or until just
2097 before a non-blank line is reached */
2098 static void
2099 edit_move_down_paragraph (WEdit * edit, int do_scroll)
2101 int i;
2102 if (edit->curs_line >= edit->total_lines - 1)
2104 i = edit->total_lines;
2106 else
2108 if (line_is_blank (edit, edit->curs_line))
2110 if (line_is_blank (edit, edit->curs_line + 1))
2112 for (i = edit->curs_line + 1; i; i++)
2113 if (!line_is_blank (edit, i) || i > edit->total_lines)
2115 i--;
2116 break;
2119 else
2121 for (i = edit->curs_line + 1; i; i++)
2122 if (line_is_blank (edit, i) || i >= edit->total_lines)
2123 break;
2126 else
2128 for (i = edit->curs_line + 1; i; i++)
2129 if (line_is_blank (edit, i) || i >= edit->total_lines)
2130 break;
2133 edit_move_down (edit, i - edit->curs_line, do_scroll);
2136 static void
2137 edit_begin_page (WEdit * edit)
2139 edit_update_curs_row (edit);
2140 edit_move_up (edit, edit->curs_row, 0);
2143 static void
2144 edit_end_page (WEdit * edit)
2146 edit_update_curs_row (edit);
2147 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
2151 /* goto beginning of text */
2152 static void
2153 edit_move_to_top (WEdit * edit)
2155 if (edit->curs_line)
2157 edit_cursor_move (edit, -edit->curs1);
2158 edit_move_to_prev_col (edit, 0);
2159 edit->force |= REDRAW_PAGE;
2160 edit->search_start = 0;
2161 edit_update_curs_row (edit);
2166 /* goto end of text */
2167 static void
2168 edit_move_to_bottom (WEdit * edit)
2170 if (edit->curs_line < edit->total_lines)
2172 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
2173 edit->start_display = edit->last_byte;
2174 edit->start_line = edit->total_lines;
2175 edit_scroll_upward (edit, edit->num_widget_lines - 1);
2176 edit->force |= REDRAW_PAGE;
2180 /* goto beginning of line */
2181 static void
2182 edit_cursor_to_bol (WEdit * edit)
2184 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
2185 edit->search_start = edit->curs1;
2186 edit->prev_col = edit_get_col (edit);
2187 edit->over_col = 0;
2190 /* goto end of line */
2191 static void
2192 edit_cursor_to_eol (WEdit * edit)
2194 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
2195 edit->search_start = edit->curs1;
2196 edit->prev_col = edit_get_col (edit);
2197 edit->over_col = 0;
2200 /* move cursor to line 'line' */
2201 void
2202 edit_move_to_line (WEdit * e, long line)
2204 if (line < e->curs_line)
2205 edit_move_up (e, e->curs_line - line, 0);
2206 else
2207 edit_move_down (e, line - e->curs_line, 0);
2208 edit_scroll_screen_over_cursor (e);
2211 /* scroll window so that first visible line is 'line' */
2212 void
2213 edit_move_display (WEdit * e, long line)
2215 if (line < e->start_line)
2216 edit_scroll_upward (e, e->start_line - line);
2217 else
2218 edit_scroll_downward (e, line - e->start_line);
2221 /* save markers onto undo stack */
2222 void
2223 edit_push_markers (WEdit * edit)
2225 edit_push_action (edit, MARK_1 + edit->mark1);
2226 edit_push_action (edit, MARK_2 + edit->mark2);
2229 void
2230 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2232 edit->mark1 = m1;
2233 edit->mark2 = m2;
2234 edit->column1 = c1;
2235 edit->column2 = c2;
2239 /* highlight marker toggle */
2240 void
2241 edit_mark_cmd (WEdit * edit, int unmark)
2243 edit_push_markers (edit);
2244 if (unmark)
2246 edit_set_markers (edit, 0, 0, 0, 0);
2247 edit->force |= REDRAW_PAGE;
2249 else
2251 if (edit->mark2 >= 0)
2253 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
2254 edit->curs_col + edit->over_col);
2255 edit->force |= REDRAW_PAGE;
2257 else
2258 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
2259 edit->curs_col + edit->over_col);
2263 static unsigned long
2264 my_type_of (int c)
2266 int x, r = 0;
2267 const char *p, *q;
2268 const char option_chars_move_whole_word[] =
2269 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2271 if (!c)
2272 return 0;
2273 if (c == '!')
2275 if (*option_chars_move_whole_word == '!')
2276 return 2;
2277 return 0x80000000UL;
2279 if (g_ascii_isupper ((gchar) c))
2280 c = 'A';
2281 else if (g_ascii_islower ((gchar) c))
2282 c = 'a';
2283 else if (g_ascii_isalpha (c))
2284 c = 'a';
2285 else if (isdigit (c))
2286 c = '0';
2287 else if (isspace (c))
2288 c = ' ';
2289 q = strchr (option_chars_move_whole_word, c);
2290 if (!q)
2291 return 0xFFFFFFFFUL;
2294 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2295 if (*p == '!')
2296 x <<= 1;
2297 r |= x;
2299 while ((q = strchr (q + 1, c)));
2300 return r;
2303 static void
2304 edit_left_word_move (WEdit * edit, int s)
2306 for (;;)
2308 int c1, c2;
2309 if (column_highlighting
2310 && edit->mark1 != edit->mark2
2311 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2312 break;
2313 edit_cursor_move (edit, -1);
2314 if (!edit->curs1)
2315 break;
2316 c1 = edit_get_byte (edit, edit->curs1 - 1);
2317 c2 = edit_get_byte (edit, edit->curs1);
2318 if (!(my_type_of (c1) & my_type_of (c2)))
2319 break;
2320 if (isspace (c1) && !isspace (c2))
2321 break;
2322 if (s)
2323 if (!isspace (c1) && isspace (c2))
2324 break;
2328 static void
2329 edit_left_word_move_cmd (WEdit * edit)
2331 edit_left_word_move (edit, 0);
2332 edit->force |= REDRAW_PAGE;
2335 static void
2336 edit_right_word_move (WEdit * edit, int s)
2338 for (;;)
2340 int c1, c2;
2341 if (column_highlighting
2342 && edit->mark1 != edit->mark2
2343 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
2344 break;
2345 edit_cursor_move (edit, 1);
2346 if (edit->curs1 >= edit->last_byte)
2347 break;
2348 c1 = edit_get_byte (edit, edit->curs1 - 1);
2349 c2 = edit_get_byte (edit, edit->curs1);
2350 if (!(my_type_of (c1) & my_type_of (c2)))
2351 break;
2352 if (isspace (c1) && !isspace (c2))
2353 break;
2354 if (s)
2355 if (!isspace (c1) && isspace (c2))
2356 break;
2360 static void
2361 edit_right_word_move_cmd (WEdit * edit)
2363 edit_right_word_move (edit, 0);
2364 edit->force |= REDRAW_PAGE;
2367 static void
2368 edit_right_char_move_cmd (WEdit * edit)
2370 int cw = 1;
2371 int c = 0;
2372 if (edit->utf8)
2374 c = edit_get_utf (edit, edit->curs1, &cw);
2375 if (cw < 1)
2376 cw = 1;
2378 else
2380 c = edit_get_byte (edit, edit->curs1);
2382 if (option_cursor_beyond_eol && c == '\n')
2384 edit->over_col++;
2386 else
2388 edit_cursor_move (edit, cw);
2392 static void
2393 edit_left_char_move_cmd (WEdit * edit)
2395 int cw = 1;
2396 if (column_highlighting
2397 && option_cursor_beyond_eol
2398 && edit->mark1 != edit->mark2
2399 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2400 return;
2401 if (edit->utf8)
2403 edit_get_prev_utf (edit, edit->curs1, &cw);
2404 if (cw < 1)
2405 cw = 1;
2407 if (option_cursor_beyond_eol && edit->over_col > 0)
2409 edit->over_col--;
2411 else
2413 edit_cursor_move (edit, -cw);
2417 /** Up or down cursor moving.
2418 direction = TRUE - move up
2419 = FALSE - move down
2421 static void
2422 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2424 unsigned long p;
2425 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
2427 if (i > l)
2428 i = l;
2430 if (i == 0)
2431 return;
2433 if (i > 1)
2434 edit->force |= REDRAW_PAGE;
2435 if (do_scroll)
2437 if (direction)
2438 edit_scroll_upward (edit, i);
2439 else
2440 edit_scroll_downward (edit, i);
2442 p = edit_bol (edit, edit->curs1);
2444 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
2446 edit_cursor_move (edit, p - edit->curs1);
2448 edit_move_to_prev_col (edit, p);
2450 /* search start of current multibyte char (like CJK) */
2451 if (edit->curs1 + 1 < edit->last_byte)
2453 edit_right_char_move_cmd (edit);
2454 edit_left_char_move_cmd (edit);
2457 edit->search_start = edit->curs1;
2458 edit->found_len = 0;
2461 static void
2462 edit_right_delete_word (WEdit * edit)
2464 int c1, c2;
2465 for (;;)
2467 if (edit->curs1 >= edit->last_byte)
2468 break;
2469 c1 = edit_delete (edit, 1);
2470 c2 = edit_get_byte (edit, edit->curs1);
2471 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2472 break;
2473 if (!(my_type_of (c1) & my_type_of (c2)))
2474 break;
2478 static void
2479 edit_left_delete_word (WEdit * edit)
2481 int c1, c2;
2482 for (;;)
2484 if (edit->curs1 <= 0)
2485 break;
2486 c1 = edit_backspace (edit, 1);
2487 c2 = edit_get_byte (edit, edit->curs1 - 1);
2488 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2489 break;
2490 if (!(my_type_of (c1) & my_type_of (c2)))
2491 break;
2496 the start column position is not recorded, and hence does not
2497 undo as it happed. But who would notice.
2499 static void
2500 edit_do_undo (WEdit * edit)
2502 long ac;
2503 long count = 0;
2505 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2506 edit->over_col = 0;
2507 while ((ac = pop_action (edit)) < KEY_PRESS)
2509 switch ((int) ac)
2511 case STACK_BOTTOM:
2512 goto done_undo;
2513 case CURS_RIGHT:
2514 edit_cursor_move (edit, 1);
2515 break;
2516 case CURS_LEFT:
2517 edit_cursor_move (edit, -1);
2518 break;
2519 case BACKSPACE:
2520 edit_backspace (edit, 1);
2521 break;
2522 case DELCHAR:
2523 edit_delete (edit, 1);
2524 break;
2525 case COLUMN_ON:
2526 column_highlighting = 1;
2527 break;
2528 case COLUMN_OFF:
2529 column_highlighting = 0;
2530 break;
2532 if (ac >= 256 && ac < 512)
2533 edit_insert_ahead (edit, ac - 256);
2534 if (ac >= 0 && ac < 256)
2535 edit_insert (edit, ac);
2537 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
2539 edit->mark1 = ac - MARK_1;
2540 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2542 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
2544 edit->mark2 = ac - MARK_2;
2545 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2547 if (count++)
2548 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2551 if (edit->start_display > ac - KEY_PRESS)
2553 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2554 edit->force |= REDRAW_PAGE;
2556 else if (edit->start_display < ac - KEY_PRESS)
2558 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2559 edit->force |= REDRAW_PAGE;
2561 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2562 edit_update_curs_row (edit);
2564 done_undo:;
2565 edit->stack_disable = 0;
2568 static void
2569 edit_delete_to_line_end (WEdit * edit)
2571 while (edit_get_byte (edit, edit->curs1) != '\n')
2573 if (!edit->curs2)
2574 break;
2575 edit_delete (edit, 1);
2579 static void
2580 edit_delete_to_line_begin (WEdit * edit)
2582 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2584 if (!edit->curs1)
2585 break;
2586 edit_backspace (edit, 1);
2590 void
2591 edit_delete_line (WEdit * edit)
2594 * Delete right part of the line.
2595 * Note that edit_get_byte() returns '\n' when byte position is
2596 * beyond EOF.
2598 while (edit_get_byte (edit, edit->curs1) != '\n')
2600 (void) edit_delete (edit, 1);
2604 * Delete '\n' char.
2605 * Note that edit_delete() will not corrupt anything if called while
2606 * cursor position is EOF.
2608 (void) edit_delete (edit, 1);
2611 * Delete left part of the line.
2612 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2614 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2616 (void) edit_backspace (edit, 1);
2620 void
2621 insert_spaces_tab (WEdit * edit, int half)
2623 int i;
2624 edit_update_curs_col (edit);
2625 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
2626 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2627 while (i > 0)
2629 edit_insert (edit, ' ');
2630 i -= space_width;
2634 static int
2635 is_aligned_on_a_tab (WEdit * edit)
2637 edit_update_curs_col (edit);
2638 return !((edit->curs_col % (TAB_SIZE * space_width))
2639 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2642 static int
2643 right_of_four_spaces (WEdit * edit)
2645 int i, ch = 0;
2646 for (i = 1; i <= HALF_TAB_SIZE; i++)
2647 ch |= edit_get_byte (edit, edit->curs1 - i);
2648 if (ch == ' ')
2649 return is_aligned_on_a_tab (edit);
2650 return 0;
2653 static int
2654 left_of_four_spaces (WEdit * edit)
2656 int i, ch = 0;
2657 for (i = 0; i < HALF_TAB_SIZE; i++)
2658 ch |= edit_get_byte (edit, edit->curs1 + i);
2659 if (ch == ' ')
2660 return is_aligned_on_a_tab (edit);
2661 return 0;
2665 edit_indent_width (WEdit * edit, long p)
2667 long q = p;
2668 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2669 q++;
2670 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2673 void
2674 edit_insert_indent (WEdit * edit, int indent)
2676 if (!option_fill_tabs_with_spaces)
2678 while (indent >= TAB_SIZE)
2680 edit_insert (edit, '\t');
2681 indent -= TAB_SIZE;
2684 while (indent-- > 0)
2685 edit_insert (edit, ' ');
2688 static void
2689 edit_auto_indent (WEdit * edit)
2691 long p;
2692 char c;
2693 p = edit->curs1;
2694 /* use the previous line as a template */
2695 p = edit_move_backward (edit, p, 1);
2696 /* copy the leading whitespace of the line */
2697 for (;;)
2698 { /* no range check - the line _is_ \n-terminated */
2699 c = edit_get_byte (edit, p++);
2700 if (c != ' ' && c != '\t')
2701 break;
2702 edit_insert (edit, c);
2706 static inline void
2707 edit_double_newline (WEdit * edit)
2709 edit_insert (edit, '\n');
2710 if (edit_get_byte (edit, edit->curs1) == '\n')
2711 return;
2712 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2713 return;
2714 edit->force |= REDRAW_PAGE;
2715 edit_insert (edit, '\n');
2718 static inline void
2719 edit_tab_cmd (WEdit * edit)
2721 int i;
2723 if (option_fake_half_tabs)
2725 if (is_in_indent (edit))
2727 /*insert a half tab (usually four spaces) unless there is a
2728 half tab already behind, then delete it and insert a
2729 full tab. */
2730 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
2732 for (i = 1; i <= HALF_TAB_SIZE; i++)
2733 edit_backspace (edit, 1);
2734 edit_insert (edit, '\t');
2736 else
2738 insert_spaces_tab (edit, 1);
2740 return;
2743 if (option_fill_tabs_with_spaces)
2745 insert_spaces_tab (edit, 0);
2747 else
2749 edit_insert (edit, '\t');
2753 static void
2754 check_and_wrap_line (WEdit * edit)
2756 int curs, c;
2757 if (!option_typewriter_wrap)
2758 return;
2759 edit_update_curs_col (edit);
2760 if (edit->curs_col < option_word_wrap_line_length)
2761 return;
2762 curs = edit->curs1;
2763 for (;;)
2765 curs--;
2766 c = edit_get_byte (edit, curs);
2767 if (c == '\n' || curs <= 0)
2769 edit_insert (edit, '\n');
2770 return;
2772 if (c == ' ' || c == '\t')
2774 int current = edit->curs1;
2775 edit_cursor_move (edit, curs - edit->curs1 + 1);
2776 edit_insert (edit, '\n');
2777 edit_cursor_move (edit, current - edit->curs1 + 1);
2778 return;
2783 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
2785 void
2786 edit_push_key_press (WEdit * edit)
2788 edit_push_action (edit, KEY_PRESS + edit->start_display);
2789 if (edit->mark2 == -1)
2790 edit_push_action (edit, MARK_1 + edit->mark1);
2793 /* this find the matching bracket in either direction, and sets edit->bracket */
2794 static long
2795 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2797 const char *const b = "{}{[][()(", *p;
2798 int i = 1, a, inc = -1, c, d, n = 0;
2799 unsigned long j = 0;
2800 long q;
2801 edit_update_curs_row (edit);
2802 c = edit_get_byte (edit, edit->curs1);
2803 p = strchr (b, c);
2804 /* no limit */
2805 if (!furthest_bracket_search)
2806 furthest_bracket_search--;
2807 /* not on a bracket at all */
2808 if (!p)
2809 return -1;
2810 /* the matching bracket */
2811 d = p[1];
2812 /* going left or right? */
2813 if (strchr ("{[(", c))
2814 inc = 1;
2815 for (q = edit->curs1 + inc;; q += inc)
2817 /* out of buffer? */
2818 if (q >= edit->last_byte || q < 0)
2819 break;
2820 a = edit_get_byte (edit, q);
2821 /* don't want to eat CPU */
2822 if (j++ > furthest_bracket_search)
2823 break;
2824 /* out of screen? */
2825 if (in_screen)
2827 if (q < edit->start_display)
2828 break;
2829 /* count lines if searching downward */
2830 if (inc > 0 && a == '\n')
2831 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2832 break;
2834 /* count bracket depth */
2835 i += (a == c) - (a == d);
2836 /* return if bracket depth is zero */
2837 if (!i)
2838 return q;
2840 /* no match */
2841 return -1;
2844 static long last_bracket = -1;
2846 void
2847 edit_find_bracket (WEdit * edit)
2849 edit->bracket = edit_get_bracket (edit, 1, 10000);
2850 if (last_bracket != edit->bracket)
2851 edit->force |= REDRAW_PAGE;
2852 last_bracket = edit->bracket;
2855 static inline void
2856 edit_goto_matching_bracket (WEdit * edit)
2858 long q;
2860 q = edit_get_bracket (edit, 0, 0);
2861 if (q >= 0)
2863 edit->bracket = edit->curs1;
2864 edit->force |= REDRAW_PAGE;
2865 edit_cursor_move (edit, q - edit->curs1);
2870 * This executes a command as though the user initiated it through a key
2871 * press. Callback with WIDGET_KEY as a message calls this after
2872 * translating the key press. This function can be used to pass any
2873 * command to the editor. Note that the screen wouldn't update
2874 * automatically. Either of command or char_for_insertion must be
2875 * passed as -1. Commands are executed, and char_for_insertion is
2876 * inserted at the cursor.
2878 void
2879 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
2881 if (command == CK_Begin_Record_Macro)
2883 edit->macro_i = 0;
2884 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2885 return;
2887 if (command == CK_End_Record_Macro && edit->macro_i != -1)
2889 edit->force |= REDRAW_COMPLETELY;
2890 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2891 edit->macro_i = -1;
2892 return;
2894 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
2896 edit->macro[edit->macro_i].command = command;
2897 edit->macro[edit->macro_i++].ch = char_for_insertion;
2899 /* record the beginning of a set of editing actions initiated by a key press */
2900 if (command != CK_Undo && command != CK_Ext_Mode)
2901 edit_push_key_press (edit);
2903 edit_execute_cmd (edit, command, char_for_insertion);
2904 if (column_highlighting)
2905 edit->force |= REDRAW_PAGE;
2908 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
2911 This executes a command at a lower level than macro recording.
2912 It also does not push a key_press onto the undo stack. This means
2913 that if it is called many times, a single undo command will undo
2914 all of them. It also does not check for the Undo command.
2916 void
2917 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
2919 edit->force |= REDRAW_LINE;
2921 /* The next key press will unhighlight the found string, so update
2922 * the whole page */
2923 if (edit->found_len || column_highlighting)
2924 edit->force |= REDRAW_PAGE;
2926 if (command / 100 == 6)
2927 { /* a highlight command like shift-arrow */
2928 column_highlighting = 0;
2929 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
2931 edit_mark_cmd (edit, 1); /* clear */
2932 edit_mark_cmd (edit, 0); /* marking on */
2934 edit->highlight = 1;
2936 else
2937 { /* any other command */
2938 if (edit->highlight)
2939 edit_mark_cmd (edit, 0); /* clear */
2940 edit->highlight = 0;
2943 /* first check for undo */
2944 if (command == CK_Undo)
2946 edit_do_undo (edit);
2947 edit->found_len = 0;
2948 edit->prev_col = edit_get_col (edit);
2949 edit->search_start = edit->curs1;
2950 return;
2953 /* An ordinary key press */
2954 if (char_for_insertion >= 0)
2956 /* if non persistent selection and text selected */
2957 if (!option_persistent_selections)
2959 if (edit->mark1 != edit->mark2)
2960 edit_block_delete_cmd (edit);
2962 if (edit->overwrite)
2964 /* remove char only one time, after input first byte, multibyte chars */
2965 if ((!utf8_display || edit->charpoint == 0)
2966 && edit_get_byte (edit, edit->curs1) != '\n')
2967 edit_delete (edit, 0);
2969 if (option_cursor_beyond_eol && edit->over_col > 0)
2970 edit_insert_over (edit);
2971 #ifdef HAVE_CHARSET
2972 if (char_for_insertion > 255 && utf8_display == 0)
2974 unsigned char str[6 + 1];
2975 size_t i = 0;
2976 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
2977 if (res == 0)
2979 str[0] = '.';
2980 str[1] = '\0';
2982 else
2984 str[res] = '\0';
2986 while (str[i] != 0 && i <= 6)
2988 char_for_insertion = str[i];
2989 edit_insert (edit, char_for_insertion);
2990 i++;
2993 else
2994 #endif
2995 edit_insert (edit, char_for_insertion);
2997 if (option_auto_para_formatting)
2999 format_paragraph (edit, 0);
3000 edit->force |= REDRAW_PAGE;
3002 else
3003 check_and_wrap_line (edit);
3004 edit->found_len = 0;
3005 edit->prev_col = edit_get_col (edit);
3006 edit->search_start = edit->curs1;
3007 edit_find_bracket (edit);
3008 return;
3011 switch (command)
3013 case CK_Begin_Page:
3014 case CK_End_Page:
3015 case CK_Begin_Page_Highlight:
3016 case CK_End_Page_Highlight:
3017 case CK_Word_Left:
3018 case CK_Word_Right:
3019 case CK_Up:
3020 case CK_Down:
3021 case CK_Left:
3022 case CK_Right:
3023 if (edit->mark2 >= 0)
3025 if (!option_persistent_selections)
3027 if (column_highlighting)
3028 edit_push_action (edit, COLUMN_ON);
3029 column_highlighting = 0;
3030 edit_mark_cmd (edit, 1);
3035 switch (command)
3037 case CK_Begin_Page:
3038 case CK_End_Page:
3039 case CK_Begin_Page_Highlight:
3040 case CK_End_Page_Highlight:
3041 case CK_Word_Left:
3042 case CK_Word_Right:
3043 case CK_Up:
3044 case CK_Down:
3045 case CK_Word_Left_Highlight:
3046 case CK_Word_Right_Highlight:
3047 case CK_Up_Highlight:
3048 case CK_Down_Highlight:
3049 case CK_Up_Alt_Highlight:
3050 case CK_Down_Alt_Highlight:
3051 if (edit->mark2 == -1)
3052 break; /*marking is following the cursor: may need to highlight a whole line */
3053 case CK_Left:
3054 case CK_Right:
3055 case CK_Left_Highlight:
3056 case CK_Right_Highlight:
3057 edit->force |= REDRAW_CHAR_ONLY;
3060 /* basic cursor key commands */
3061 switch (command)
3063 case CK_BackSpace:
3064 /* if non persistent selection and text selected */
3065 if (!option_persistent_selections)
3067 if (edit->mark1 != edit->mark2)
3069 edit_block_delete_cmd (edit);
3070 break;
3073 if (option_cursor_beyond_eol && edit->over_col > 0)
3075 edit->over_col--;
3076 break;
3078 if (option_backspace_through_tabs && is_in_indent (edit))
3080 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3081 edit_backspace (edit, 1);
3082 break;
3084 else
3086 if (option_fake_half_tabs)
3088 int i;
3089 if (is_in_indent (edit) && right_of_four_spaces (edit))
3091 for (i = 0; i < HALF_TAB_SIZE; i++)
3092 edit_backspace (edit, 1);
3093 break;
3097 edit_backspace (edit, 0);
3098 break;
3099 case CK_Delete:
3100 /* if non persistent selection and text selected */
3101 if (!option_persistent_selections)
3103 if (edit->mark1 != edit->mark2)
3105 edit_block_delete_cmd (edit);
3106 break;
3110 if (option_cursor_beyond_eol && edit->over_col > 0)
3111 edit_insert_over (edit);
3113 if (option_fake_half_tabs)
3115 int i;
3116 if (is_in_indent (edit) && left_of_four_spaces (edit))
3118 for (i = 1; i <= HALF_TAB_SIZE; i++)
3119 edit_delete (edit, 1);
3120 break;
3123 edit_delete (edit, 0);
3124 break;
3125 case CK_Delete_Word_Left:
3126 edit->over_col = 0;
3127 edit_left_delete_word (edit);
3128 break;
3129 case CK_Delete_Word_Right:
3130 if (option_cursor_beyond_eol && edit->over_col > 0)
3131 edit_insert_over (edit);
3133 edit_right_delete_word (edit);
3134 break;
3135 case CK_Delete_Line:
3136 edit_delete_line (edit);
3137 break;
3138 case CK_Delete_To_Line_End:
3139 edit_delete_to_line_end (edit);
3140 break;
3141 case CK_Delete_To_Line_Begin:
3142 edit_delete_to_line_begin (edit);
3143 break;
3144 case CK_Enter:
3145 edit->over_col = 0;
3146 if (option_auto_para_formatting)
3148 edit_double_newline (edit);
3149 if (option_return_does_auto_indent)
3150 edit_auto_indent (edit);
3151 format_paragraph (edit, 0);
3153 else
3155 edit_insert (edit, '\n');
3156 if (option_return_does_auto_indent)
3158 edit_auto_indent (edit);
3161 break;
3162 case CK_Return:
3163 edit_insert (edit, '\n');
3164 break;
3166 case CK_Page_Up_Alt_Highlight:
3167 column_highlighting = 1;
3168 case CK_Page_Up:
3169 case CK_Page_Up_Highlight:
3170 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3171 break;
3172 case CK_Page_Down_Alt_Highlight:
3173 column_highlighting = 1;
3174 case CK_Page_Down:
3175 case CK_Page_Down_Highlight:
3176 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3177 break;
3178 case CK_Left_Alt_Highlight:
3179 column_highlighting = 1;
3180 case CK_Left:
3181 case CK_Left_Highlight:
3182 if (option_fake_half_tabs)
3184 if (is_in_indent (edit) && right_of_four_spaces (edit))
3186 if (option_cursor_beyond_eol && edit->over_col > 0)
3187 edit->over_col--;
3188 else
3189 edit_cursor_move (edit, -HALF_TAB_SIZE);
3190 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3191 break;
3194 edit_left_char_move_cmd (edit);
3195 break;
3196 case CK_Right_Alt_Highlight:
3197 column_highlighting = 1;
3198 case CK_Right:
3199 case CK_Right_Highlight:
3200 if (option_fake_half_tabs)
3202 if (is_in_indent (edit) && left_of_four_spaces (edit))
3204 edit_cursor_move (edit, HALF_TAB_SIZE);
3205 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3206 break;
3209 edit_right_char_move_cmd (edit);
3210 break;
3211 case CK_Begin_Page:
3212 case CK_Begin_Page_Highlight:
3213 edit_begin_page (edit);
3214 break;
3215 case CK_End_Page:
3216 case CK_End_Page_Highlight:
3217 edit_end_page (edit);
3218 break;
3219 case CK_Word_Left:
3220 case CK_Word_Left_Highlight:
3221 edit->over_col = 0;
3222 edit_left_word_move_cmd (edit);
3223 break;
3224 case CK_Word_Right:
3225 case CK_Word_Right_Highlight:
3226 edit->over_col = 0;
3227 edit_right_word_move_cmd (edit);
3228 break;
3229 case CK_Up_Alt_Highlight:
3230 column_highlighting = 1;
3231 case CK_Up:
3232 case CK_Up_Highlight:
3233 edit_move_up (edit, 1, 0);
3234 break;
3235 case CK_Down_Alt_Highlight:
3236 column_highlighting = 1;
3237 case CK_Down:
3238 case CK_Down_Highlight:
3239 edit_move_down (edit, 1, 0);
3240 break;
3241 case CK_Paragraph_Up_Alt_Highlight:
3242 column_highlighting = 1;
3243 case CK_Paragraph_Up:
3244 case CK_Paragraph_Up_Highlight:
3245 edit_move_up_paragraph (edit, 0);
3246 break;
3247 case CK_Paragraph_Down_Alt_Highlight:
3248 column_highlighting = 1;
3249 case CK_Paragraph_Down:
3250 case CK_Paragraph_Down_Highlight:
3251 edit_move_down_paragraph (edit, 0);
3252 break;
3253 case CK_Scroll_Up_Alt_Highlight:
3254 column_highlighting = 1;
3255 case CK_Scroll_Up:
3256 case CK_Scroll_Up_Highlight:
3257 edit_move_up (edit, 1, 1);
3258 break;
3259 case CK_Scroll_Down_Alt_Highlight:
3260 column_highlighting = 1;
3261 case CK_Scroll_Down:
3262 case CK_Scroll_Down_Highlight:
3263 edit_move_down (edit, 1, 1);
3264 break;
3265 case CK_Home:
3266 case CK_Home_Highlight:
3267 edit_cursor_to_bol (edit);
3268 break;
3269 case CK_End:
3270 case CK_End_Highlight:
3271 edit_cursor_to_eol (edit);
3272 break;
3273 case CK_Tab:
3274 /* if text marked shift block */
3275 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3277 if (edit->mark2 < 0)
3278 edit_mark_cmd (edit, 0);
3279 edit_move_block_to_right (edit);
3281 else
3283 if (option_cursor_beyond_eol)
3284 edit_insert_over (edit);
3285 edit_tab_cmd (edit);
3286 if (option_auto_para_formatting)
3288 format_paragraph (edit, 0);
3289 edit->force |= REDRAW_PAGE;
3291 else
3293 check_and_wrap_line (edit);
3296 break;
3298 case CK_Toggle_Insert:
3299 edit->overwrite = (edit->overwrite == 0);
3300 break;
3302 case CK_Mark:
3303 if (edit->mark2 >= 0)
3305 if (column_highlighting)
3306 edit_push_action (edit, COLUMN_ON);
3307 column_highlighting = 0;
3309 edit_mark_cmd (edit, 0);
3310 break;
3311 case CK_Column_Mark:
3312 if (!column_highlighting)
3313 edit_push_action (edit, COLUMN_OFF);
3314 column_highlighting = 1;
3315 edit_mark_cmd (edit, 0);
3316 break;
3317 case CK_Mark_All:
3318 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3319 edit->force |= REDRAW_PAGE;
3320 break;
3321 case CK_Unmark:
3322 if (column_highlighting)
3323 edit_push_action (edit, COLUMN_ON);
3324 column_highlighting = 0;
3325 edit_mark_cmd (edit, 1);
3326 break;
3328 case CK_Toggle_Line_State:
3329 option_line_state = !option_line_state;
3330 if (option_line_state)
3332 option_line_state_width = LINE_STATE_WIDTH;
3334 else
3336 option_line_state_width = 0;
3338 edit->force |= REDRAW_PAGE;
3339 break;
3341 case CK_Toggle_Show_Margin:
3342 show_right_margin = !show_right_margin;
3343 edit->force |= REDRAW_PAGE;
3344 break;
3346 case CK_Toggle_Bookmark:
3347 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3348 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3349 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3350 else
3351 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3352 break;
3353 case CK_Flush_Bookmarks:
3354 book_mark_flush (edit, BOOK_MARK_COLOR);
3355 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3356 edit->force |= REDRAW_PAGE;
3357 break;
3358 case CK_Next_Bookmark:
3359 if (edit->book_mark)
3361 struct _book_mark *p;
3362 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3363 if (p->next)
3365 p = p->next;
3366 if (p->line >= edit->start_line + edit->num_widget_lines
3367 || p->line < edit->start_line)
3368 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3369 edit_move_to_line (edit, p->line);
3372 break;
3373 case CK_Prev_Bookmark:
3374 if (edit->book_mark)
3376 struct _book_mark *p;
3377 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3378 while (p->line == edit->curs_line)
3379 if (p->prev)
3380 p = p->prev;
3381 if (p->line >= 0)
3383 if (p->line >= edit->start_line + edit->num_widget_lines
3384 || p->line < edit->start_line)
3385 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3386 edit_move_to_line (edit, p->line);
3389 break;
3391 case CK_Beginning_Of_Text:
3392 case CK_Beginning_Of_Text_Highlight:
3393 edit_move_to_top (edit);
3394 break;
3395 case CK_End_Of_Text:
3396 case CK_End_Of_Text_Highlight:
3397 edit_move_to_bottom (edit);
3398 break;
3400 case CK_Copy:
3401 if (option_cursor_beyond_eol && edit->over_col > 0)
3402 edit_insert_over (edit);
3403 edit_block_copy_cmd (edit);
3404 break;
3405 case CK_Remove:
3406 edit_block_delete_cmd (edit);
3407 break;
3408 case CK_Move:
3409 if (option_cursor_beyond_eol && edit->over_col > 0)
3410 edit_insert_over (edit);
3411 edit_block_move_cmd (edit);
3412 break;
3414 case CK_Shift_Block_Left:
3415 if (edit->mark1 != edit->mark2)
3416 edit_move_block_to_left (edit);
3417 break;
3418 case CK_Shift_Block_Right:
3419 if (edit->mark1 != edit->mark2)
3420 edit_move_block_to_right (edit);
3421 break;
3422 case CK_XStore:
3423 edit_copy_to_X_buf_cmd (edit);
3424 break;
3425 case CK_XCut:
3426 edit_cut_to_X_buf_cmd (edit);
3427 break;
3428 case CK_XPaste:
3429 /* if non persistent selection and text selected */
3430 if (!option_persistent_selections)
3432 if (edit->mark1 != edit->mark2)
3433 edit_block_delete_cmd (edit);
3435 if (option_cursor_beyond_eol && edit->over_col > 0)
3436 edit_insert_over (edit);
3437 edit_paste_from_X_buf_cmd (edit);
3438 break;
3439 case CK_Selection_History:
3440 edit_paste_from_history (edit);
3441 break;
3443 case CK_Save_As:
3444 edit_save_as_cmd (edit);
3445 break;
3446 case CK_Save:
3447 edit_save_confirm_cmd (edit);
3448 break;
3449 case CK_Load:
3450 edit_load_cmd (edit, EDIT_FILE_COMMON);
3451 break;
3452 case CK_Save_Block:
3453 edit_save_block_cmd (edit);
3454 break;
3455 case CK_Insert_File:
3456 edit_insert_file_cmd (edit);
3457 break;
3459 case CK_Load_Prev_File:
3460 edit_load_back_cmd (edit);
3461 break;
3462 case CK_Load_Next_File:
3463 edit_load_forward_cmd (edit);
3464 break;
3466 case CK_Load_Syntax_File:
3467 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3468 break;
3469 case CK_Choose_Syntax:
3470 edit_syntax_dialog (edit, edit->syntax_type);
3471 break;
3473 case CK_Load_Menu_File:
3474 edit_load_cmd (edit, EDIT_FILE_MENU);
3475 break;
3477 case CK_Toggle_Syntax:
3478 if ((option_syntax_highlighting ^= 1) == 1)
3479 edit_load_syntax (edit, NULL, edit->syntax_type);
3480 edit->force |= REDRAW_PAGE;
3481 break;
3483 case CK_Toggle_Tab_TWS:
3484 enable_show_tabs_tws ^= 1;
3485 edit->force |= REDRAW_PAGE;
3486 break;
3488 case CK_Find:
3489 edit_search_cmd (edit, 0);
3490 break;
3491 case CK_Find_Again:
3492 edit_search_cmd (edit, 1);
3493 break;
3494 case CK_Replace:
3495 edit_replace_cmd (edit, 0);
3496 break;
3497 case CK_Replace_Again:
3498 edit_replace_cmd (edit, 1);
3499 break;
3500 case CK_Complete_Word:
3501 /* if text marked shift block */
3502 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3504 edit_move_block_to_left (edit);
3506 else
3508 edit_complete_word_cmd (edit);
3510 break;
3511 case CK_Find_Definition:
3512 edit_get_match_keyword_cmd (edit);
3513 break;
3514 case CK_Quit:
3515 dlg_stop (edit->widget.parent);
3516 break;
3517 case CK_New:
3518 edit_new_cmd (edit);
3519 break;
3520 case CK_Help:
3521 edit_help_cmd (edit);
3522 break;
3523 case CK_Refresh:
3524 edit_refresh_cmd (edit);
3525 break;
3526 case CK_SaveSetupCmd:
3527 save_setup_cmd ();
3528 break;
3529 case CK_About:
3530 query_dialog (_(" About "),
3531 _("\n Cooledit v3.11.5\n\n"
3532 " Copyright (C) 1996 the Free Software Foundation\n\n"
3533 " A user friendly text editor written\n"
3534 " for the Midnight Commander.\n"), D_NORMAL, 1, _("&OK"));
3535 break;
3536 case CK_LearnKeys:
3537 learn_keys ();
3538 break;
3539 case CK_Edit_Options:
3540 edit_options_dialog (edit);
3541 break;
3542 case CK_Edit_Save_Mode:
3543 menu_save_mode_cmd ();
3544 break;
3545 case CK_Date:
3547 char s[BUF_MEDIUM];
3548 /* fool gcc to prevent a Y2K warning */
3549 char time_format[] = "_c";
3550 time_format[0] = '%';
3552 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3553 edit_print_string (edit, s);
3554 edit->force |= REDRAW_PAGE;
3555 break;
3557 break;
3558 case CK_Goto:
3559 edit_goto_cmd (edit);
3560 break;
3561 case CK_Paragraph_Format:
3562 format_paragraph (edit, 1);
3563 edit->force |= REDRAW_PAGE;
3564 break;
3565 case CK_Delete_Macro:
3566 edit_delete_macro_cmd (edit);
3567 break;
3568 case CK_Match_Bracket:
3569 edit_goto_matching_bracket (edit);
3570 break;
3571 case CK_User_Menu:
3572 user_menu (edit);
3573 break;
3574 case CK_Sort:
3575 edit_sort_cmd (edit);
3576 break;
3577 case CK_ExtCmd:
3578 edit_ext_cmd (edit);
3579 break;
3580 case CK_Mail:
3581 edit_mail_dialog (edit);
3582 break;
3583 case CK_Shell:
3584 view_other_cmd ();
3585 break;
3586 case CK_SelectCodepage:
3587 edit_select_codepage_cmd (edit);
3588 break;
3589 case CK_Insert_Literal:
3590 edit_insert_literal_cmd (edit);
3591 break;
3592 case CK_Execute_Macro:
3593 edit_execute_macro_cmd (edit);
3594 break;
3595 case CK_Begin_End_Macro:
3596 edit_begin_end_macro_cmd (edit);
3597 break;
3598 case CK_Ext_Mode:
3599 edit->extmod = 1;
3600 break;
3601 default:
3602 break;
3605 /* CK_Pipe_Block */
3606 if ((command / 1000) == 1) /* a shell command */
3607 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3608 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3609 { /* a macro command */
3610 struct macro m[MAX_MACRO_LENGTH];
3611 int nm;
3612 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3613 edit_execute_macro (edit, m, nm);
3616 /* keys which must set the col position, and the search vars */
3617 switch (command)
3619 case CK_Find:
3620 case CK_Find_Again:
3621 case CK_Replace:
3622 case CK_Replace_Again:
3623 case CK_Complete_Word:
3624 edit->prev_col = edit_get_col (edit);
3625 break;
3626 case CK_Up:
3627 case CK_Up_Highlight:
3628 case CK_Up_Alt_Highlight:
3629 case CK_Down:
3630 case CK_Down_Highlight:
3631 case CK_Down_Alt_Highlight:
3632 case CK_Page_Up:
3633 case CK_Page_Up_Highlight:
3634 case CK_Page_Up_Alt_Highlight:
3635 case CK_Page_Down:
3636 case CK_Page_Down_Highlight:
3637 case CK_Page_Down_Alt_Highlight:
3638 case CK_Beginning_Of_Text:
3639 case CK_Beginning_Of_Text_Highlight:
3640 case CK_End_Of_Text:
3641 case CK_End_Of_Text_Highlight:
3642 case CK_Paragraph_Up:
3643 case CK_Paragraph_Up_Highlight:
3644 case CK_Paragraph_Up_Alt_Highlight:
3645 case CK_Paragraph_Down:
3646 case CK_Paragraph_Down_Highlight:
3647 case CK_Paragraph_Down_Alt_Highlight:
3648 case CK_Scroll_Up:
3649 case CK_Scroll_Up_Highlight:
3650 case CK_Scroll_Up_Alt_Highlight:
3651 case CK_Scroll_Down:
3652 case CK_Scroll_Down_Highlight:
3653 case CK_Scroll_Down_Alt_Highlight:
3654 edit->search_start = edit->curs1;
3655 edit->found_len = 0;
3656 break;
3657 default:
3658 edit->found_len = 0;
3659 edit->prev_col = edit_get_col (edit);
3660 edit->search_start = edit->curs1;
3662 edit_find_bracket (edit);
3664 if (option_auto_para_formatting)
3666 switch (command)
3668 case CK_BackSpace:
3669 case CK_Delete:
3670 case CK_Delete_Word_Left:
3671 case CK_Delete_Word_Right:
3672 case CK_Delete_To_Line_End:
3673 case CK_Delete_To_Line_Begin:
3674 format_paragraph (edit, 0);
3675 edit->force |= REDRAW_PAGE;
3681 static void
3682 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
3684 int i = 0;
3686 if (edit->macro_depth++ > 256)
3688 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3689 edit->macro_depth--;
3690 return;
3692 edit->force |= REDRAW_PAGE;
3693 for (; i < n; i++)
3695 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3697 edit_update_screen (edit);
3698 edit->macro_depth--;
3701 /* User edit menu, like user menu (F2) but only in editor. */
3702 static void
3703 user_menu (WEdit * edit)
3705 char *block_file;
3706 int nomark;
3707 long start_mark, end_mark;
3708 struct stat status;
3710 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3712 nomark = eval_marks (edit, &start_mark, &end_mark);
3713 if (nomark == 0)
3714 edit_save_block (edit, block_file, start_mark, end_mark);
3716 /* run shell scripts from menu */
3717 user_menu_cmd (edit);
3719 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
3721 int rc = 0;
3722 FILE *fd;
3724 if (nomark == 0)
3726 /* i.e. we have marked block */
3727 rc = edit_block_delete_cmd (edit);
3730 if (rc == 0)
3731 edit_insert_file (edit, block_file);
3733 /* truncate block file */
3734 fd = fopen (block_file, "w");
3735 if (fd != NULL)
3736 fclose (fd);
3738 edit_refresh_cmd (edit);
3739 edit->force |= REDRAW_COMPLETELY;
3741 g_free (block_file);
3744 void
3745 edit_stack_init (void)
3747 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3749 edit_history_moveto[edit_stack_iterator].filename = NULL;
3750 edit_history_moveto[edit_stack_iterator].line = -1;
3753 edit_stack_iterator = 0;
3756 void
3757 edit_stack_free (void)
3759 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3760 g_free (edit_history_moveto[edit_stack_iterator].filename);
3763 /* move i lines */
3764 void
3765 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3767 edit_move_updown (edit, i, do_scroll, TRUE);
3770 /* move i lines */
3771 void
3772 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3774 edit_move_updown (edit, i, do_scroll, FALSE);