Refactored IPV4/IPV6 FTP connection setup code
[midnight-commander.git] / src / editor / edit.c
blobce617c435601f8b401416422661407c5bf91b00c
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 */
50 #include "lib/timefmt.h" /* time formatting */
51 #include "lib/lock.h"
53 #include "src/widget.h"
54 #include "src/cmd.h" /* view_other_cmd() */
55 #include "src/user.h" /* user_menu_cmd() */
56 #include "src/wtools.h" /* query_dialog() */
57 #include "src/charsets.h" /* get_codepage_id */
58 #include "src/main.h" /* source_codepage */
59 #include "src/setup.h" /* option_tab_spacing */
60 #include "src/learn.h" /* learn_keys */
61 #include "src/cmddef.h"
62 #include "src/keybind.h"
64 #include "edit-impl.h"
65 #include "edit-widget.h"
67 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
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);
128 static void
129 edit_about (void)
131 const char *header = N_("About");
132 const char *button_name = N_("&OK");
133 const char * const version = "MCEdit " VERSION;
134 char text[BUF_LARGE];
136 int win_len, version_len, button_len;
137 int cols, lines;
139 Dlg_head *about_dlg;
141 #ifdef ENABLE_NLS
142 header = _(header);
143 button_name = _(button_name);
144 #endif
146 button_len = str_term_width1 (button_name) + 5;
147 version_len = str_term_width1 (version);
149 g_snprintf (text, sizeof (text),
150 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
151 " A user friendly text editor\n"
152 " written for the Midnight Commander"));
154 win_len = str_term_width1 (header);
155 win_len = max (win_len, version_len);
156 win_len = max (win_len, button_len);
158 /* count width and height of text */
159 str_msg_term_size (text, &lines, &cols);
160 lines += 9;
161 cols = max (win_len, cols) + 6;
163 /* dialog */
164 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
165 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
167 add_widget (about_dlg, label_new (3, (cols - version_len)/2, version));
168 add_widget (about_dlg, label_new (5, 3, text));
169 add_widget (about_dlg, button_new (lines - 3, (cols - button_len)/2,
170 B_ENTER, NORMAL_BUTTON, button_name, NULL));
172 run_dlg (about_dlg);
173 destroy_dlg (about_dlg);
177 edit_get_byte (WEdit * edit, long byte_index)
179 unsigned long p;
180 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
181 return '\n';
183 if (byte_index >= edit->curs1)
185 p = edit->curs1 + edit->curs2 - byte_index - 1;
186 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
188 else
190 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
194 char *
195 edit_get_byte_ptr (WEdit * edit, long byte_index)
197 unsigned long p;
198 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
199 return NULL;
201 if (byte_index >= edit->curs1)
203 p = edit->curs1 + edit->curs2 - byte_index - 1;
204 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
205 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
207 else
209 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
210 (byte_index & M_EDIT_BUF_SIZE));
214 char *
215 edit_get_buf_ptr (WEdit * edit, long byte_index)
217 unsigned long p;
219 if (byte_index >= (edit->curs1 + edit->curs2))
220 byte_index--;
222 if (byte_index < 0)
223 return NULL;
225 if (byte_index >= edit->curs1)
227 p = edit->curs1 + edit->curs2 - 1;
228 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
229 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
231 else
233 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
238 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
240 gchar *str = NULL;
241 int res = -1;
242 gunichar ch;
243 gchar *next_ch = NULL;
244 int width = 0;
246 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
248 *char_width = 0;
249 return '\n';
252 str = edit_get_byte_ptr (edit, byte_index);
254 if (str == NULL)
256 *char_width = 0;
257 return 0;
260 res = g_utf8_get_char_validated (str, -1);
262 if (res < 0)
264 ch = *str;
265 width = 0;
267 else
269 ch = res;
270 /* Calculate UTF-8 char width */
271 next_ch = g_utf8_next_char (str);
272 if (next_ch)
274 width = next_ch - str;
276 else
278 ch = 0;
279 width = 0;
282 *char_width = width;
283 return ch;
287 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
289 gchar *str, *buf = NULL;
290 int res = -1;
291 gunichar ch;
292 gchar *next_ch = NULL;
293 int width = 0;
295 if (byte_index > 0)
296 byte_index--;
298 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
300 *char_width = 0;
301 return 0;
304 ch = edit_get_utf (edit, byte_index, &width);
306 if (width == 1)
308 *char_width = width;
309 return ch;
312 str = edit_get_byte_ptr (edit, byte_index);
313 buf = edit_get_buf_ptr (edit, byte_index);
314 if (str == NULL || buf == NULL)
316 *char_width = 0;
317 return 0;
319 /* get prev utf8 char */
320 if (str != buf)
321 str = g_utf8_find_prev_char (buf, str);
323 res = g_utf8_get_char_validated (str, -1);
325 if (res < 0)
327 ch = *str;
328 width = 0;
330 else
332 ch = res;
333 /* Calculate UTF-8 char width */
334 next_ch = g_utf8_next_char (str);
335 if (next_ch)
337 width = next_ch - str;
339 else
341 ch = 0;
342 width = 0;
345 *char_width = width;
346 return ch;
350 * Initialize the buffers for an empty files.
352 static void
353 edit_init_buffers (WEdit * edit)
355 int j;
357 for (j = 0; j <= MAXBUFF; j++)
359 edit->buffers1[j] = NULL;
360 edit->buffers2[j] = NULL;
363 edit->curs1 = 0;
364 edit->curs2 = 0;
365 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
369 * Load file OR text into buffers. Set cursor to the beginning of file.
370 * Return 1 on error.
372 static int
373 edit_load_file_fast (WEdit * edit, const char *filename)
375 long buf, buf2;
376 int file = -1;
377 int ret = 1;
379 edit->curs2 = edit->last_byte;
380 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
382 file = mc_open (filename, O_RDONLY | O_BINARY);
383 if (file == -1)
385 gchar *errmsg;
387 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
388 edit_error_dialog (_("Error"), errmsg);
389 g_free (errmsg);
390 return 1;
393 if (!edit->buffers2[buf2])
394 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
398 if (mc_read (file,
399 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
400 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
401 break;
403 for (buf = buf2 - 1; buf >= 0; buf--)
405 /* edit->buffers2[0] is already allocated */
406 if (!edit->buffers2[buf])
407 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
408 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
409 break;
411 ret = 0;
413 while (0);
414 if (ret)
416 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
417 edit_error_dialog (_("Error"), err_str);
418 g_free (err_str);
420 mc_close (file);
421 return ret;
424 /* detecting an error on save is easy: just check if every byte has been written. */
425 /* detecting an error on read, is not so easy 'cos there is not way to tell
426 whether you read everything or not. */
427 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
428 static const struct edit_filters
430 const char *read, *write, *extension;
431 } all_filters[] =
433 /* *INDENT-OFF* */
434 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
435 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
436 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
437 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
438 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
439 /* *INDENT-ON* */
442 /* Return index of the filter or -1 is there is no appropriate filter */
443 static int
444 edit_find_filter (const char *filename)
446 size_t i, l, e;
448 if (filename == NULL)
449 return -1;
451 l = strlen (filename);
452 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
454 e = strlen (all_filters[i].extension);
455 if (l > e)
456 if (!strcmp (all_filters[i].extension, filename + l - e))
457 return i;
459 return -1;
462 static char *
463 edit_get_filter (const char *filename)
465 int i;
466 char *p, *quoted_name;
468 i = edit_find_filter (filename);
469 if (i < 0)
470 return NULL;
472 quoted_name = name_quote (filename, 0);
473 p = g_strdup_printf (all_filters[i].read, quoted_name);
474 g_free (quoted_name);
475 return p;
478 char *
479 edit_get_write_filter (const char *write_name, const char *filename)
481 int i;
482 char *p, *writename;
484 i = edit_find_filter (filename);
485 if (i < 0)
486 return NULL;
488 writename = name_quote (write_name, 0);
489 p = g_strdup_printf (all_filters[i].write, writename);
490 g_free (writename);
491 return p;
494 static long
495 edit_insert_stream (WEdit * edit, FILE * f)
497 int c;
498 long i = 0;
499 while ((c = fgetc (f)) >= 0)
501 edit_insert (edit, c);
502 i++;
504 return i;
507 long
508 edit_write_stream (WEdit * edit, FILE * f)
510 long i;
512 if (edit->lb == LB_ASIS)
514 for (i = 0; i < edit->last_byte; i++)
515 if (fputc (edit_get_byte (edit, i), f) < 0)
516 break;
517 return i;
520 /* change line breaks */
521 for (i = 0; i < edit->last_byte; i++)
523 unsigned char c = edit_get_byte (edit, i);
525 if (!(c == '\n' || c == '\r'))
527 /* not line break */
528 if (fputc (c, f) < 0)
529 return i;
531 else
532 { /* (c == '\n' || c == '\r') */
533 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
535 switch (edit->lb)
537 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
538 /* put one line break unconditionally */
539 if (fputc ('\n', f) < 0)
540 return i;
542 i++; /* 2 chars are processed */
544 if (c == '\r' && c1 == '\n')
545 /* Windows line break; go to the next char */
546 break;
548 if (c == '\r' && c1 == '\r')
550 /* two Macintosh line breaks; put second line break */
551 if (fputc ('\n', f) < 0)
552 return i;
553 break;
556 if (fputc (c1, f) < 0)
557 return i;
558 break;
560 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
561 /* put one line break unconditionally */
562 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
563 return i;
565 if (c == '\r' && c1 == '\n')
566 /* Windows line break; go to the next char */
567 i++;
568 break;
570 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
571 /* put one line break unconditionally */
572 if (fputc ('\r', f) < 0)
573 return i;
575 i++; /* 2 chars are processed */
577 if (c == '\r' && c1 == '\n')
578 /* Windows line break; go to the next char */
579 break;
581 if (c == '\n' && c1 == '\n')
583 /* two Windows line breaks; put second line break */
584 if (fputc ('\r', f) < 0)
585 return i;
586 break;
589 if (fputc (c1, f) < 0)
590 return i;
591 break;
592 case LB_ASIS: /* default without changes */
593 break;
598 return edit->last_byte;
601 #define TEMP_BUF_LEN 1024
603 /* inserts a file at the cursor, returns 1 on success */
605 edit_insert_file (WEdit * edit, const char *filename)
607 char *p;
609 p = edit_get_filter (filename);
610 if (p != NULL)
612 FILE *f;
613 long current = edit->curs1;
614 f = (FILE *) popen (p, "r");
615 if (f != NULL)
617 edit_insert_stream (edit, f);
618 edit_cursor_move (edit, current - edit->curs1);
619 if (pclose (f) > 0)
621 char *errmsg;
622 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
623 edit_error_dialog (_("Error"), errmsg);
624 g_free (errmsg);
625 g_free (p);
626 return 0;
629 else
631 char *errmsg;
632 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
633 edit_error_dialog (_("Error"), errmsg);
634 g_free (errmsg);
635 g_free (p);
636 return 0;
638 g_free (p);
640 else
642 int i, file, blocklen;
643 long current = edit->curs1;
644 int vertical_insertion = 0;
645 char *buf;
646 file = mc_open (filename, O_RDONLY | O_BINARY);
647 if (file == -1)
648 return 0;
649 buf = g_malloc0 (TEMP_BUF_LEN);
650 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
651 if (blocklen > 0)
653 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
654 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
655 vertical_insertion = 1;
656 else
657 mc_lseek (file, 0, SEEK_SET);
659 if (vertical_insertion)
660 blocklen = edit_insert_column_of_text_from_file (edit, file);
661 else
662 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
663 for (i = 0; i < blocklen; i++)
664 edit_insert (edit, buf[i]);
665 edit_cursor_move (edit, current - edit->curs1);
666 g_free (buf);
667 mc_close (file);
668 if (blocklen != 0)
669 return 0;
671 return 1;
674 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
675 static int
676 check_file_access (WEdit * edit, const char *filename, struct stat *st)
678 int file;
679 gchar *errmsg = NULL;
681 /* Try opening an existing file */
682 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
684 if (file < 0)
687 * Try creating the file. O_EXCL prevents following broken links
688 * and opening existing files.
690 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
691 if (file < 0)
693 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
694 goto cleanup;
696 else
698 /* New file, delete it if it's not modified or saved */
699 edit->delete_file = 1;
703 /* Check what we have opened */
704 if (mc_fstat (file, st) < 0)
706 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
707 goto cleanup;
710 /* We want to open regular files only */
711 if (!S_ISREG (st->st_mode))
713 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
714 goto cleanup;
718 * Don't delete non-empty files.
719 * O_EXCL should prevent it, but let's be on the safe side.
721 if (st->st_size > 0)
722 edit->delete_file = 0;
724 if (st->st_size >= SIZE_LIMIT)
725 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
727 cleanup:
728 (void) mc_close (file);
730 if (errmsg != NULL)
732 edit_error_dialog (_("Error"), errmsg);
733 g_free (errmsg);
734 return 1;
736 return 0;
740 * Open the file and load it into the buffers, either directly or using
741 * a filter. Return 0 on success, 1 on error.
743 * Fast loading (edit_load_file_fast) is used when the file size is
744 * known. In this case the data is read into the buffers by blocks.
745 * If the file size is not known, the data is loaded byte by byte in
746 * edit_insert_file.
748 static int
749 edit_load_file (WEdit * edit)
751 int fast_load = 1;
753 /* Cannot do fast load if a filter is used */
754 if (edit_find_filter (edit->filename) >= 0)
755 fast_load = 0;
758 * VFS may report file size incorrectly, and slow load is not a big
759 * deal considering overhead in VFS.
761 if (!vfs_file_is_local (edit->filename))
762 fast_load = 0;
765 * FIXME: line end translation should disable fast loading as well
766 * Consider doing fseek() to the end and ftell() for the real size.
769 if (*edit->filename)
771 /* If we are dealing with a real file, check that it exists */
772 if (check_file_access (edit, edit->filename, &edit->stat1))
773 return 1;
775 else
777 /* nothing to load */
778 fast_load = 0;
781 edit_init_buffers (edit);
783 if (fast_load)
785 edit->last_byte = edit->stat1.st_size;
786 edit_load_file_fast (edit, edit->filename);
787 /* If fast load was used, the number of lines wasn't calculated */
788 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
790 else
792 edit->last_byte = 0;
793 if (*edit->filename)
795 edit->stack_disable = 1;
796 if (!edit_insert_file (edit, edit->filename))
798 edit_clean (edit);
799 return 1;
801 edit->stack_disable = 0;
804 edit->lb = LB_ASIS;
805 return 0;
808 /* Restore saved cursor position in the file */
809 static void
810 edit_load_position (WEdit * edit)
812 char *filename;
813 long line, column;
814 off_t offset;
816 if (!edit->filename || !*edit->filename)
817 return;
819 filename = vfs_canon (edit->filename);
820 load_file_position (filename, &line, &column, &offset);
821 g_free (filename);
823 if (line > 0)
825 edit_move_to_line (edit, line - 1);
826 edit->prev_col = column;
828 else if (offset > 0)
830 edit_cursor_move (edit, offset);
831 line = edit->curs_line;
833 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
834 edit_move_display (edit, line - (edit->num_widget_lines / 2));
837 /* Save cursor position in the file */
838 static void
839 edit_save_position (WEdit * edit)
841 char *filename;
843 if (!edit->filename || !*edit->filename)
844 return;
846 filename = vfs_canon (edit->filename);
847 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
848 g_free (filename);
851 /* Clean the WEdit stricture except the widget part */
852 static void
853 edit_purge_widget (WEdit * edit)
855 size_t len = sizeof (WEdit) - sizeof (Widget);
856 char *start = (char *) edit + sizeof (Widget);
857 memset (start, 0, len);
858 edit->macro_i = -1; /* not recording a macro */
861 static void
862 edit_set_keymap (void)
864 editor_map = default_editor_keymap;
865 if (editor_keymap && editor_keymap->len > 0)
866 editor_map = (global_keymap_t *) editor_keymap->data;
868 editor_x_map = default_editor_x_keymap;
869 if (editor_x_keymap && editor_x_keymap->len > 0)
870 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
874 #define space_width 1
877 * Fill in the edit structure. Return NULL on failure. Pass edit as
878 * NULL to allocate a new structure.
880 * If line is 0, try to restore saved position. Otherwise put the
881 * cursor on that line and show it in the middle of the screen.
883 WEdit *
884 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
886 int to_free = 0;
887 option_auto_syntax = 1; /* Resetting to auto on every invokation */
888 if (option_line_state)
890 option_line_state_width = LINE_STATE_WIDTH;
892 else
894 option_line_state_width = 0;
896 if (!edit)
898 #ifdef ENABLE_NLS
900 * Expand option_whole_chars_search by national letters using
901 * current locale
904 static char option_whole_chars_search_buf[256];
906 if (option_whole_chars_search_buf != option_whole_chars_search)
908 size_t i;
909 size_t len = str_term_width1 (option_whole_chars_search);
911 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
913 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
915 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
917 option_whole_chars_search_buf[len++] = i;
921 option_whole_chars_search_buf[len] = 0;
922 option_whole_chars_search = option_whole_chars_search_buf;
924 #endif /* ENABLE_NLS */
925 edit = g_malloc0 (sizeof (WEdit));
926 edit->search = NULL;
927 to_free = 1;
929 edit_purge_widget (edit);
930 edit->num_widget_lines = lines;
931 edit->over_col = 0;
932 edit->num_widget_columns = columns;
933 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
934 edit->stat1.st_uid = getuid ();
935 edit->stat1.st_gid = getgid ();
936 edit->stat1.st_mtime = 0;
937 edit->bracket = -1;
938 edit->force |= REDRAW_PAGE;
939 edit_set_filename (edit, filename);
940 edit->stack_size = START_STACK_SIZE;
941 edit->stack_size_mask = START_STACK_SIZE - 1;
942 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
944 edit->utf8 = 0;
945 edit->converter = str_cnv_from_term;
946 edit_set_codeset (edit);
948 if (edit_load_file (edit))
950 /* edit_load_file already gives an error message */
951 if (to_free)
952 g_free (edit);
953 return 0;
956 edit->loading_done = 1;
957 edit->modified = 0;
958 edit->locked = 0;
959 edit_load_syntax (edit, NULL, NULL);
961 int color;
962 edit_get_syntax_color (edit, -1, &color);
965 /* load saved cursor position */
966 if ((line == 0) && option_save_position)
968 edit_load_position (edit);
970 else
972 if (line <= 0)
973 line = 1;
974 edit_move_display (edit, line - 1);
975 edit_move_to_line (edit, line - 1);
978 edit_set_keymap ();
980 return edit;
983 /* Clear the edit struct, freeing everything in it. Return 1 on success */
985 edit_clean (WEdit * edit)
987 int j = 0;
989 if (!edit)
990 return 0;
992 /* a stale lock, remove it */
993 if (edit->locked)
994 edit->locked = edit_unlock_file (edit);
996 /* save cursor position */
997 if (option_save_position)
998 edit_save_position (edit);
1000 /* File specified on the mcedit command line and never saved */
1001 if (edit->delete_file)
1002 unlink (edit->filename);
1004 edit_free_syntax_rules (edit);
1005 book_mark_flush (edit, -1);
1006 for (; j <= MAXBUFF; j++)
1008 g_free (edit->buffers1[j]);
1009 g_free (edit->buffers2[j]);
1012 g_free (edit->undo_stack);
1013 g_free (edit->filename);
1014 g_free (edit->dir);
1016 mc_search_free (edit->search);
1017 edit->search = NULL;
1019 if (edit->converter != str_cnv_from_term)
1020 str_close_conv (edit->converter);
1022 edit_purge_widget (edit);
1024 return 1;
1027 /* returns 1 on success */
1029 edit_renew (WEdit * edit)
1031 int lines = edit->num_widget_lines;
1032 int columns = edit->num_widget_columns;
1034 edit_clean (edit);
1035 return (edit_init (edit, lines, columns, "", 0) != NULL);
1039 * Load a new file into the editor. If it fails, preserve the old file.
1040 * To do it, allocate a new widget, initialize it and, if the new file
1041 * was loaded, copy the data to the old widget.
1042 * Return 1 on success, 0 on failure.
1045 edit_reload (WEdit * edit, const char *filename)
1047 WEdit *e;
1048 int lines = edit->num_widget_lines;
1049 int columns = edit->num_widget_columns;
1051 e = g_malloc0 (sizeof (WEdit));
1052 e->widget = edit->widget;
1053 if (!edit_init (e, lines, columns, filename, 0))
1055 g_free (e);
1056 return 0;
1058 edit_clean (edit);
1059 memcpy (edit, e, sizeof (WEdit));
1060 g_free (e);
1061 return 1;
1065 * Load a new file into the editor and set line. If it fails, preserve the old file.
1066 * To do it, allocate a new widget, initialize it and, if the new file
1067 * was loaded, copy the data to the old widget.
1068 * Return 1 on success, 0 on failure.
1071 edit_reload_line (WEdit * edit, const char *filename, long line)
1073 WEdit *e;
1074 int lines = edit->num_widget_lines;
1075 int columns = edit->num_widget_columns;
1077 e = g_malloc0 (sizeof (WEdit));
1078 e->widget = edit->widget;
1079 if (!edit_init (e, lines, columns, filename, line))
1081 g_free (e);
1082 return 0;
1084 edit_clean (edit);
1085 memcpy (edit, e, sizeof (WEdit));
1086 g_free (e);
1087 return 1;
1090 void
1091 edit_set_codeset (WEdit *edit)
1093 #ifdef HAVE_CHARSET
1094 const char *cp_id;
1096 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
1098 if (cp_id != NULL)
1100 GIConv conv;
1101 conv = str_crt_conv_from (cp_id);
1102 if (conv != INVALID_CONV)
1104 if (edit->converter != str_cnv_from_term)
1105 str_close_conv (edit->converter);
1106 edit->converter = conv;
1110 if (cp_id != NULL)
1111 edit->utf8 = str_isutf8 (cp_id);
1112 #else
1113 (void) edit;
1114 #endif
1119 Recording stack for undo:
1120 The following is an implementation of a compressed stack. Identical
1121 pushes are recorded by a negative prefix indicating the number of times the
1122 same char was pushed. This saves space for repeated curs-left or curs-right
1123 delete etc.
1127 pushed: stored:
1131 b -3
1133 c --> -4
1139 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1140 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1141 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1142 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1143 position.
1145 The only way the cursor moves or the buffer is changed is through the routines:
1146 insert, backspace, insert_ahead, delete, and cursor_move.
1147 These record the reverse undo movements onto the stack each time they are
1148 called.
1150 Each key press results in a set of actions (insert; delete ...). So each time
1151 a key is pressed the current position of start_display is pushed as
1152 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1153 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1154 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1158 void
1159 edit_push_action (WEdit * edit, long c, ...)
1161 unsigned long sp = edit->stack_pointer;
1162 unsigned long spm1;
1163 long *t;
1165 /* first enlarge the stack if necessary */
1166 if (sp > edit->stack_size - 10)
1167 { /* say */
1168 if (option_max_undo < 256)
1169 option_max_undo = 256;
1170 if (edit->stack_size < (unsigned long) option_max_undo)
1172 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1173 if (t)
1175 edit->undo_stack = t;
1176 edit->stack_size <<= 1;
1177 edit->stack_size_mask = edit->stack_size - 1;
1181 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1182 if (edit->stack_disable)
1183 return;
1185 #ifdef FAST_MOVE_CURSOR
1186 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
1188 va_list ap;
1189 edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
1190 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1191 va_start (ap, c);
1192 c = -(va_arg (ap, int));
1193 va_end (ap);
1195 else
1196 #endif /* ! FAST_MOVE_CURSOR */
1197 if (edit->stack_bottom != sp
1198 && spm1 != edit->stack_bottom
1199 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
1201 int d;
1202 if (edit->undo_stack[spm1] < 0)
1204 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1205 if (d == c)
1207 if (edit->undo_stack[spm1] > -1000000000)
1209 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1210 edit->undo_stack[spm1]--;
1211 return;
1214 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1215 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1216 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1217 { /* a left then a right anihilate each other */
1218 if (edit->undo_stack[spm1] == -2)
1219 edit->stack_pointer = spm1;
1220 else
1221 edit->undo_stack[spm1]++;
1222 return;
1224 #endif
1226 else
1228 d = edit->undo_stack[spm1];
1229 if (d == c)
1231 if (c >= KEY_PRESS)
1232 return; /* --> no need to push multiple do-nothings */
1233 edit->undo_stack[sp] = -2;
1234 goto check_bottom;
1236 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1237 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1238 { /* a left then a right anihilate each other */
1239 edit->stack_pointer = spm1;
1240 return;
1242 #endif
1245 edit->undo_stack[sp] = c;
1247 check_bottom:
1248 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1250 /* if the sp wraps round and catches the stack_bottom then erase
1251 * the first set of actions on the stack to make space - by moving
1252 * stack_bottom forward one "key press" */
1253 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1254 if ((unsigned long) c == edit->stack_bottom ||
1255 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1258 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1260 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
1261 && edit->stack_bottom != edit->stack_pointer);
1263 /*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: */
1264 if (edit->stack_pointer != edit->stack_bottom
1265 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1266 edit->stack_bottom = edit->stack_pointer = 0;
1270 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1271 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1273 static long
1274 pop_action (WEdit * edit)
1276 long c;
1277 unsigned long sp = edit->stack_pointer;
1279 if (sp == edit->stack_bottom)
1280 return STACK_BOTTOM;
1282 sp = (sp - 1) & edit->stack_size_mask;
1283 c = edit->undo_stack[sp];
1284 if (c >= 0)
1286 /* edit->undo_stack[sp] = '@'; */
1287 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1288 return c;
1291 if (sp == edit->stack_bottom)
1292 return STACK_BOTTOM;
1294 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1295 if (edit->undo_stack[sp] == -2)
1297 /* edit->undo_stack[sp] = '@'; */
1298 edit->stack_pointer = sp;
1300 else
1301 edit->undo_stack[sp]++;
1303 return c;
1306 /* is called whenever a modification is made by one of the four routines below */
1307 static void
1308 edit_modification (WEdit * edit)
1310 edit->caches_valid = 0;
1311 edit->screen_modified = 1;
1313 /* raise lock when file modified */
1314 if (!edit->modified && !edit->delete_file)
1315 edit->locked = edit_lock_file (edit);
1316 edit->modified = 1;
1320 Basic low level single character buffer alterations and movements at the cursor.
1321 Returns char passed over, inserted or removed.
1324 void
1325 edit_insert (WEdit * edit, int c)
1327 /* check if file has grown to large */
1328 if (edit->last_byte >= SIZE_LIMIT)
1329 return;
1331 /* first we must update the position of the display window */
1332 if (edit->curs1 < edit->start_display)
1334 edit->start_display++;
1335 if (c == '\n')
1336 edit->start_line++;
1339 /* Mark file as modified, unless the file hasn't been fully loaded */
1340 if (edit->loading_done)
1342 edit_modification (edit);
1345 /* now we must update some info on the file and check if a redraw is required */
1346 if (c == '\n')
1348 if (edit->book_mark)
1349 book_mark_inc (edit, edit->curs_line);
1350 edit->curs_line++;
1351 edit->total_lines++;
1352 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1355 /* save the reverse command onto the undo stack */
1356 edit_push_action (edit, BACKSPACE);
1358 /* update markers */
1359 edit->mark1 += (edit->mark1 > edit->curs1);
1360 edit->mark2 += (edit->mark2 > edit->curs1);
1361 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1363 /* add a new buffer if we've reached the end of the last one */
1364 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1365 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1367 /* perform the insertion */
1368 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
1369 = (unsigned char) c;
1371 /* update file length */
1372 edit->last_byte++;
1374 /* update cursor position */
1375 edit->curs1++;
1378 static void
1379 edit_insert_over (WEdit * edit)
1381 int i;
1383 for (i = 0; i < edit->over_col; i++)
1385 edit_insert (edit, ' ');
1387 edit->over_col = 0;
1390 /* same as edit_insert and move left */
1391 void
1392 edit_insert_ahead (WEdit * edit, int c)
1394 if (edit->last_byte >= SIZE_LIMIT)
1395 return;
1397 if (edit->curs1 < edit->start_display)
1399 edit->start_display++;
1400 if (c == '\n')
1401 edit->start_line++;
1403 edit_modification (edit);
1404 if (c == '\n')
1406 if (edit->book_mark)
1407 book_mark_inc (edit, edit->curs_line);
1408 edit->total_lines++;
1409 edit->force |= REDRAW_AFTER_CURSOR;
1411 edit_push_action (edit, DELCHAR);
1413 edit->mark1 += (edit->mark1 >= edit->curs1);
1414 edit->mark2 += (edit->mark2 >= edit->curs1);
1415 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1417 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1418 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1419 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
1420 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1422 edit->last_byte++;
1423 edit->curs2++;
1428 edit_delete (WEdit * edit, const int byte_delete)
1430 int p = 0;
1431 int cw = 1;
1432 int i;
1434 if (!edit->curs2)
1435 return 0;
1437 cw = 1;
1438 /* if byte_delete = 1 then delete only one byte not multibyte char */
1439 if (edit->utf8 && byte_delete == 0)
1441 edit_get_utf (edit, edit->curs1, &cw);
1442 if (cw < 1)
1443 cw = 1;
1445 for (i = 1; i <= cw; i++)
1447 if (edit->mark1 > edit->curs1)
1448 edit->mark1--;
1449 if (edit->mark2 > edit->curs1)
1450 edit->mark2--;
1451 if (edit->last_get_rule > edit->curs1)
1452 edit->last_get_rule--;
1454 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1455 ((edit->curs2 -
1456 1) & M_EDIT_BUF_SIZE) - 1];
1458 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1460 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1461 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1463 edit->last_byte--;
1464 edit->curs2--;
1465 edit_push_action (edit, p + 256);
1468 edit_modification (edit);
1469 if (p == '\n')
1471 if (edit->book_mark)
1472 book_mark_dec (edit, edit->curs_line);
1473 edit->total_lines--;
1474 edit->force |= REDRAW_AFTER_CURSOR;
1476 if (edit->curs1 < edit->start_display)
1478 edit->start_display--;
1479 if (p == '\n')
1480 edit->start_line--;
1483 return p;
1487 static int
1488 edit_backspace (WEdit * edit, const int byte_delete)
1490 int p = 0;
1491 int cw = 1;
1492 int i;
1494 if (!edit->curs1)
1495 return 0;
1497 cw = 1;
1499 if (edit->utf8 && byte_delete == 0)
1501 edit_get_prev_utf (edit, edit->curs1, &cw);
1502 if (cw < 1)
1503 cw = 1;
1505 for (i = 1; i <= cw; i++)
1507 if (edit->mark1 >= edit->curs1)
1508 edit->mark1--;
1509 if (edit->mark1 >= edit->curs1)
1510 edit->mark2--;
1511 if (edit->last_get_rule >= edit->curs1)
1512 edit->last_get_rule--;
1514 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
1515 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1516 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1518 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1519 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1521 edit->last_byte--;
1522 edit->curs1--;
1523 edit_push_action (edit, p);
1525 edit_modification (edit);
1526 if (p == '\n')
1528 if (edit->book_mark)
1529 book_mark_dec (edit, edit->curs_line);
1530 edit->curs_line--;
1531 edit->total_lines--;
1532 edit->force |= REDRAW_AFTER_CURSOR;
1535 if (edit->curs1 < edit->start_display)
1537 edit->start_display--;
1538 if (p == '\n')
1539 edit->start_line--;
1542 return p;
1545 #ifdef FAST_MOVE_CURSOR
1547 static void
1548 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1550 unsigned long next;
1551 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
1553 edit->curs_line--;
1554 next -= (unsigned long) dest;
1555 n -= next;
1556 src += next;
1557 dest += next;
1562 edit_move_backward_lots (WEdit * edit, long increment)
1564 int r, s, t;
1565 unsigned char *p = NULL;
1567 if (increment > edit->curs1)
1568 increment = edit->curs1;
1569 if (increment <= 0)
1570 return -1;
1571 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1573 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1574 if (r > increment)
1575 r = increment;
1576 s = edit->curs1 & M_EDIT_BUF_SIZE;
1578 if (s > r)
1580 memqcpy (edit,
1581 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1582 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
1584 else
1586 if (s != 0)
1588 memqcpy (edit,
1589 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1590 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1591 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1592 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1594 memqcpy (edit,
1595 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1596 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1597 EDIT_BUF_SIZE - (r - s), r - s);
1599 increment -= r;
1600 edit->curs1 -= r;
1601 edit->curs2 += r;
1602 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1604 if (p)
1605 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1606 else
1607 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1609 else
1611 g_free (p);
1614 s = edit->curs1 & M_EDIT_BUF_SIZE;
1615 while (increment)
1617 p = 0;
1618 r = EDIT_BUF_SIZE;
1619 if (r > increment)
1620 r = increment;
1621 t = s;
1622 if (r < t)
1623 t = r;
1624 memqcpy (edit,
1625 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1626 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
1627 if (r >= s)
1629 if (t)
1631 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1632 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1634 memqcpy (edit,
1635 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1636 EDIT_BUF_SIZE - r,
1637 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1638 EDIT_BUF_SIZE - (r - s), r - s);
1640 increment -= r;
1641 edit->curs1 -= r;
1642 edit->curs2 += r;
1643 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1645 if (p)
1646 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1647 else
1648 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1650 else
1651 g_free (p);
1653 return edit_get_byte (edit, edit->curs1);
1656 #endif /* ! FAST_MOVE_CURSOR */
1658 /* moves the cursor right or left: increment positive or negative respectively */
1659 void
1660 edit_cursor_move (WEdit * edit, long increment)
1662 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1663 int c;
1664 #ifdef FAST_MOVE_CURSOR
1665 if (increment < -256)
1667 edit->force |= REDRAW_PAGE;
1668 edit_move_backward_lots (edit, -increment);
1669 return;
1671 #endif /* ! FAST_MOVE_CURSOR */
1673 if (increment < 0)
1675 for (; increment < 0; increment++)
1677 if (!edit->curs1)
1678 return;
1680 edit_push_action (edit, CURS_RIGHT);
1682 c = edit_get_byte (edit, edit->curs1 - 1);
1683 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1684 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1685 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1686 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1687 edit->curs2++;
1688 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
1689 1) & M_EDIT_BUF_SIZE];
1690 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1692 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1693 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1695 edit->curs1--;
1696 if (c == '\n')
1698 edit->curs_line--;
1699 edit->force |= REDRAW_LINE_BELOW;
1704 else if (increment > 0)
1706 for (; increment > 0; increment--)
1708 if (!edit->curs2)
1709 return;
1711 edit_push_action (edit, CURS_LEFT);
1713 c = edit_get_byte (edit, edit->curs1);
1714 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1715 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1716 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1717 edit->curs1++;
1718 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1719 ((edit->curs2 -
1720 1) & M_EDIT_BUF_SIZE) - 1];
1721 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1723 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1724 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1726 edit->curs2--;
1727 if (c == '\n')
1729 edit->curs_line++;
1730 edit->force |= REDRAW_LINE_ABOVE;
1736 /* These functions return positions relative to lines */
1738 /* returns index of last char on line + 1 */
1739 long
1740 edit_eol (WEdit * edit, long current)
1742 if (current >= edit->last_byte)
1743 return edit->last_byte;
1745 for (;; current++)
1746 if (edit_get_byte (edit, current) == '\n')
1747 break;
1748 return current;
1751 /* returns index of first char on line */
1752 long
1753 edit_bol (WEdit * edit, long current)
1755 if (current <= 0)
1756 return 0;
1758 for (;; current--)
1759 if (edit_get_byte (edit, current - 1) == '\n')
1760 break;
1761 return current;
1765 long
1766 edit_count_lines (WEdit * edit, long current, long upto)
1768 long lines = 0;
1769 if (upto > edit->last_byte)
1770 upto = edit->last_byte;
1771 if (current < 0)
1772 current = 0;
1773 while (current < upto)
1774 if (edit_get_byte (edit, current++) == '\n')
1775 lines++;
1776 return lines;
1780 /* If lines is zero this returns the count of lines from current to upto. */
1781 /* If upto is zero returns index of lines forward current. */
1782 long
1783 edit_move_forward (WEdit * edit, long current, long lines, long upto)
1785 if (upto)
1787 return edit_count_lines (edit, current, upto);
1789 else
1791 long next;
1792 if (lines < 0)
1793 lines = 0;
1794 while (lines--)
1796 next = edit_eol (edit, current) + 1;
1797 if (next > edit->last_byte)
1798 break;
1799 else
1800 current = next;
1802 return current;
1807 /* Returns offset of 'lines' lines up from current */
1808 long
1809 edit_move_backward (WEdit * edit, long current, long lines)
1811 if (lines < 0)
1812 lines = 0;
1813 current = edit_bol (edit, current);
1814 while ((lines--) && current != 0)
1815 current = edit_bol (edit, current - 1);
1816 return current;
1819 /* If cols is zero this returns the count of columns from current to upto. */
1820 /* If upto is zero returns index of cols across from current. */
1821 long
1822 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1824 long p, q;
1825 int col;
1827 if (upto)
1829 q = upto;
1830 cols = -10;
1832 else
1833 q = edit->last_byte + 2;
1835 for (col = 0, p = current; p < q; p++)
1837 int c, orig_c;
1838 int utf_ch = 0;
1839 #ifdef HAVE_CHARSET
1840 int cw = 1;
1841 #endif
1842 if (cols != -10)
1844 if (col == cols)
1845 return p;
1846 if (col > cols)
1847 return p - 1;
1849 orig_c = c = edit_get_byte (edit, p);
1850 #ifdef HAVE_CHARSET
1851 if (edit->utf8)
1853 utf_ch = edit_get_utf (edit, p, &cw);
1854 if (utf8_display)
1856 if (cw > 1)
1857 col -= cw - 1;
1858 if (g_unichar_iswide (utf_ch))
1859 col++;
1861 else if (cw > 1 && g_unichar_isprint (utf_ch))
1862 col -= cw - 1;
1864 #endif
1865 c = convert_to_display_c (c);
1866 if (c == '\t')
1867 col += TAB_SIZE - col % TAB_SIZE;
1868 else if (c == '\n')
1870 if (upto)
1871 return col;
1872 else
1873 return p;
1875 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1876 /* '\r' is shown as ^M, so we must advance 2 characters */
1877 /* Caret notation for control characters */
1878 col += 2;
1879 else
1880 col++;
1882 return col;
1885 /* returns the current column position of the cursor */
1887 edit_get_col (WEdit * edit)
1889 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1893 /* Scrolling functions */
1895 void
1896 edit_update_curs_row (WEdit * edit)
1898 edit->curs_row = edit->curs_line - edit->start_line;
1901 void
1902 edit_update_curs_col (WEdit * edit)
1904 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1908 edit_get_curs_col (const WEdit * edit)
1910 return edit->curs_col;
1913 /*moves the display start position up by i lines */
1914 void
1915 edit_scroll_upward (WEdit * edit, unsigned long i)
1917 unsigned long lines_above = edit->start_line;
1918 if (i > lines_above)
1919 i = lines_above;
1920 if (i)
1922 edit->start_line -= i;
1923 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1924 edit->force |= REDRAW_PAGE;
1925 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1927 edit_update_curs_row (edit);
1931 /* returns 1 if could scroll, 0 otherwise */
1932 void
1933 edit_scroll_downward (WEdit * edit, int i)
1935 int lines_below;
1936 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1937 if (lines_below > 0)
1939 if (i > lines_below)
1940 i = lines_below;
1941 edit->start_line += i;
1942 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1943 edit->force |= REDRAW_PAGE;
1944 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1946 edit_update_curs_row (edit);
1949 void
1950 edit_scroll_right (WEdit * edit, int i)
1952 edit->force |= REDRAW_PAGE;
1953 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1954 edit->start_col -= i;
1957 void
1958 edit_scroll_left (WEdit * edit, int i)
1960 if (edit->start_col)
1962 edit->start_col += i;
1963 if (edit->start_col > 0)
1964 edit->start_col = 0;
1965 edit->force |= REDRAW_PAGE;
1966 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1970 /* high level cursor movement commands */
1972 static int
1973 is_in_indent (WEdit * edit)
1975 long p = edit_bol (edit, edit->curs1);
1976 while (p < edit->curs1)
1977 if (!strchr (" \t", edit_get_byte (edit, p++)))
1978 return 0;
1979 return 1;
1982 static int left_of_four_spaces (WEdit * edit);
1984 void
1985 edit_move_to_prev_col (WEdit * edit, long p)
1987 int prev = edit->prev_col;
1988 int over = edit->over_col;
1989 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1991 if (option_cursor_beyond_eol)
1993 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
1994 edit_eol (edit, edit->curs1));
1996 if (line_len < prev + edit->over_col)
1998 edit->over_col = prev + over - line_len;
1999 edit->prev_col = line_len;
2000 edit->curs_col = line_len;
2002 else
2004 edit->curs_col = prev + over;
2005 edit->prev_col = edit->curs_col;
2006 edit->over_col = 0;
2009 else
2011 edit->over_col = 0;
2012 if (is_in_indent (edit) && option_fake_half_tabs)
2014 edit_update_curs_col (edit);
2015 if (space_width)
2016 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
2018 int q = edit->curs_col;
2019 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
2020 p = edit_bol (edit, edit->curs1);
2021 edit_cursor_move (edit,
2022 edit_move_forward3 (edit, p, edit->curs_col,
2023 0) - edit->curs1);
2024 if (!left_of_four_spaces (edit))
2025 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
2031 static int
2032 is_blank (WEdit * edit, long offset)
2034 long s, f;
2035 int c;
2036 s = edit_bol (edit, offset);
2037 f = edit_eol (edit, offset) - 1;
2038 while (s <= f)
2040 c = edit_get_byte (edit, s++);
2041 if (!isspace (c))
2042 return 0;
2044 return 1;
2048 /* returns the offset of line i */
2049 static long
2050 edit_find_line (WEdit * edit, int line)
2052 int i, j = 0;
2053 int m = 2000000000;
2054 if (!edit->caches_valid)
2056 for (i = 0; i < N_LINE_CACHES; i++)
2057 edit->line_numbers[i] = edit->line_offsets[i] = 0;
2058 /* three offsets that we *know* are line 0 at 0 and these two: */
2059 edit->line_numbers[1] = edit->curs_line;
2060 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
2061 edit->line_numbers[2] = edit->total_lines;
2062 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
2063 edit->caches_valid = 1;
2065 if (line >= edit->total_lines)
2066 return edit->line_offsets[2];
2067 if (line <= 0)
2068 return 0;
2069 /* find the closest known point */
2070 for (i = 0; i < N_LINE_CACHES; i++)
2072 int n;
2073 n = abs (edit->line_numbers[i] - line);
2074 if (n < m)
2076 m = n;
2077 j = i;
2080 if (m == 0)
2081 return edit->line_offsets[j]; /* know the offset exactly */
2082 if (m == 1 && j >= 3)
2083 i = j; /* one line different - caller might be looping, so stay in this cache */
2084 else
2085 i = 3 + (rand () % (N_LINE_CACHES - 3));
2086 if (line > edit->line_numbers[j])
2087 edit->line_offsets[i] =
2088 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
2089 else
2090 edit->line_offsets[i] =
2091 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
2092 edit->line_numbers[i] = line;
2093 return edit->line_offsets[i];
2097 line_is_blank (WEdit * edit, long line)
2099 return is_blank (edit, edit_find_line (edit, line));
2102 /* moves up until a blank line is reached, or until just
2103 before a non-blank line is reached */
2104 static void
2105 edit_move_up_paragraph (WEdit * edit, int do_scroll)
2107 int i = 0;
2108 if (edit->curs_line > 1)
2110 if (line_is_blank (edit, edit->curs_line))
2112 if (line_is_blank (edit, edit->curs_line - 1))
2114 for (i = edit->curs_line - 1; i; i--)
2115 if (!line_is_blank (edit, i))
2117 i++;
2118 break;
2121 else
2123 for (i = edit->curs_line - 1; i; i--)
2124 if (line_is_blank (edit, i))
2125 break;
2128 else
2130 for (i = edit->curs_line - 1; i; i--)
2131 if (line_is_blank (edit, i))
2132 break;
2135 edit_move_up (edit, edit->curs_line - i, do_scroll);
2138 /* moves down until a blank line is reached, or until just
2139 before a non-blank line is reached */
2140 static void
2141 edit_move_down_paragraph (WEdit * edit, int do_scroll)
2143 int i;
2144 if (edit->curs_line >= edit->total_lines - 1)
2146 i = edit->total_lines;
2148 else
2150 if (line_is_blank (edit, edit->curs_line))
2152 if (line_is_blank (edit, edit->curs_line + 1))
2154 for (i = edit->curs_line + 1; i; i++)
2155 if (!line_is_blank (edit, i) || i > edit->total_lines)
2157 i--;
2158 break;
2161 else
2163 for (i = edit->curs_line + 1; i; i++)
2164 if (line_is_blank (edit, i) || i >= edit->total_lines)
2165 break;
2168 else
2170 for (i = edit->curs_line + 1; i; i++)
2171 if (line_is_blank (edit, i) || i >= edit->total_lines)
2172 break;
2175 edit_move_down (edit, i - edit->curs_line, do_scroll);
2178 static void
2179 edit_begin_page (WEdit * edit)
2181 edit_update_curs_row (edit);
2182 edit_move_up (edit, edit->curs_row, 0);
2185 static void
2186 edit_end_page (WEdit * edit)
2188 edit_update_curs_row (edit);
2189 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
2193 /* goto beginning of text */
2194 static void
2195 edit_move_to_top (WEdit * edit)
2197 if (edit->curs_line)
2199 edit_cursor_move (edit, -edit->curs1);
2200 edit_move_to_prev_col (edit, 0);
2201 edit->force |= REDRAW_PAGE;
2202 edit->search_start = 0;
2203 edit_update_curs_row (edit);
2208 /* goto end of text */
2209 static void
2210 edit_move_to_bottom (WEdit * edit)
2212 if (edit->curs_line < edit->total_lines)
2214 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
2215 edit->start_display = edit->last_byte;
2216 edit->start_line = edit->total_lines;
2217 edit_scroll_upward (edit, edit->num_widget_lines - 1);
2218 edit->force |= REDRAW_PAGE;
2222 /* goto beginning of line */
2223 static void
2224 edit_cursor_to_bol (WEdit * edit)
2226 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
2227 edit->search_start = edit->curs1;
2228 edit->prev_col = edit_get_col (edit);
2229 edit->over_col = 0;
2232 /* goto end of line */
2233 static void
2234 edit_cursor_to_eol (WEdit * edit)
2236 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
2237 edit->search_start = edit->curs1;
2238 edit->prev_col = edit_get_col (edit);
2239 edit->over_col = 0;
2242 /* move cursor to line 'line' */
2243 void
2244 edit_move_to_line (WEdit * e, long line)
2246 if (line < e->curs_line)
2247 edit_move_up (e, e->curs_line - line, 0);
2248 else
2249 edit_move_down (e, line - e->curs_line, 0);
2250 edit_scroll_screen_over_cursor (e);
2253 /* scroll window so that first visible line is 'line' */
2254 void
2255 edit_move_display (WEdit * e, long line)
2257 if (line < e->start_line)
2258 edit_scroll_upward (e, e->start_line - line);
2259 else
2260 edit_scroll_downward (e, line - e->start_line);
2263 /* save markers onto undo stack */
2264 void
2265 edit_push_markers (WEdit * edit)
2267 edit_push_action (edit, MARK_1 + edit->mark1);
2268 edit_push_action (edit, MARK_2 + edit->mark2);
2271 void
2272 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2274 edit->mark1 = m1;
2275 edit->mark2 = m2;
2276 edit->column1 = c1;
2277 edit->column2 = c2;
2281 /* highlight marker toggle */
2282 void
2283 edit_mark_cmd (WEdit * edit, int unmark)
2285 edit_push_markers (edit);
2286 if (unmark)
2288 edit_set_markers (edit, 0, 0, 0, 0);
2289 edit->force |= REDRAW_PAGE;
2291 else
2293 if (edit->mark2 >= 0)
2295 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
2296 edit->curs_col + edit->over_col);
2297 edit->force |= REDRAW_PAGE;
2299 else
2300 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
2301 edit->curs_col + edit->over_col);
2305 static unsigned long
2306 my_type_of (int c)
2308 int x, r = 0;
2309 const char *p, *q;
2310 const char option_chars_move_whole_word[] =
2311 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2313 if (!c)
2314 return 0;
2315 if (c == '!')
2317 if (*option_chars_move_whole_word == '!')
2318 return 2;
2319 return 0x80000000UL;
2321 if (g_ascii_isupper ((gchar) c))
2322 c = 'A';
2323 else if (g_ascii_islower ((gchar) c))
2324 c = 'a';
2325 else if (g_ascii_isalpha (c))
2326 c = 'a';
2327 else if (isdigit (c))
2328 c = '0';
2329 else if (isspace (c))
2330 c = ' ';
2331 q = strchr (option_chars_move_whole_word, c);
2332 if (!q)
2333 return 0xFFFFFFFFUL;
2336 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2337 if (*p == '!')
2338 x <<= 1;
2339 r |= x;
2341 while ((q = strchr (q + 1, c)));
2342 return r;
2345 static void
2346 edit_left_word_move (WEdit * edit, int s)
2348 for (;;)
2350 int c1, c2;
2351 if (edit->column_highlight
2352 && edit->mark1 != edit->mark2
2353 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2354 break;
2355 edit_cursor_move (edit, -1);
2356 if (!edit->curs1)
2357 break;
2358 c1 = edit_get_byte (edit, edit->curs1 - 1);
2359 c2 = edit_get_byte (edit, edit->curs1);
2360 if (!(my_type_of (c1) & my_type_of (c2)))
2361 break;
2362 if (isspace (c1) && !isspace (c2))
2363 break;
2364 if (s)
2365 if (!isspace (c1) && isspace (c2))
2366 break;
2370 static void
2371 edit_left_word_move_cmd (WEdit * edit)
2373 edit_left_word_move (edit, 0);
2374 edit->force |= REDRAW_PAGE;
2377 static void
2378 edit_right_word_move (WEdit * edit, int s)
2380 for (;;)
2382 int c1, c2;
2383 if (edit->column_highlight
2384 && edit->mark1 != edit->mark2
2385 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
2386 break;
2387 edit_cursor_move (edit, 1);
2388 if (edit->curs1 >= edit->last_byte)
2389 break;
2390 c1 = edit_get_byte (edit, edit->curs1 - 1);
2391 c2 = edit_get_byte (edit, edit->curs1);
2392 if (!(my_type_of (c1) & my_type_of (c2)))
2393 break;
2394 if (isspace (c1) && !isspace (c2))
2395 break;
2396 if (s)
2397 if (!isspace (c1) && isspace (c2))
2398 break;
2402 static void
2403 edit_right_word_move_cmd (WEdit * edit)
2405 edit_right_word_move (edit, 0);
2406 edit->force |= REDRAW_PAGE;
2409 static void
2410 edit_right_char_move_cmd (WEdit * edit)
2412 int cw = 1;
2413 int c = 0;
2414 if (edit->utf8)
2416 c = edit_get_utf (edit, edit->curs1, &cw);
2417 if (cw < 1)
2418 cw = 1;
2420 else
2422 c = edit_get_byte (edit, edit->curs1);
2424 if (option_cursor_beyond_eol && c == '\n')
2426 edit->over_col++;
2428 else
2430 edit_cursor_move (edit, cw);
2434 static void
2435 edit_left_char_move_cmd (WEdit * edit)
2437 int cw = 1;
2438 if (edit->column_highlight
2439 && option_cursor_beyond_eol
2440 && edit->mark1 != edit->mark2
2441 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2442 return;
2443 if (edit->utf8)
2445 edit_get_prev_utf (edit, edit->curs1, &cw);
2446 if (cw < 1)
2447 cw = 1;
2449 if (option_cursor_beyond_eol && edit->over_col > 0)
2451 edit->over_col--;
2453 else
2455 edit_cursor_move (edit, -cw);
2459 /** Up or down cursor moving.
2460 direction = TRUE - move up
2461 = FALSE - move down
2463 static void
2464 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2466 unsigned long p;
2467 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
2469 if (i > l)
2470 i = l;
2472 if (i == 0)
2473 return;
2475 if (i > 1)
2476 edit->force |= REDRAW_PAGE;
2477 if (do_scroll)
2479 if (direction)
2480 edit_scroll_upward (edit, i);
2481 else
2482 edit_scroll_downward (edit, i);
2484 p = edit_bol (edit, edit->curs1);
2486 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
2488 edit_cursor_move (edit, p - edit->curs1);
2490 edit_move_to_prev_col (edit, p);
2492 /* search start of current multibyte char (like CJK) */
2493 if (edit->curs1 + 1 < edit->last_byte)
2495 edit_right_char_move_cmd (edit);
2496 edit_left_char_move_cmd (edit);
2499 edit->search_start = edit->curs1;
2500 edit->found_len = 0;
2503 static void
2504 edit_right_delete_word (WEdit * edit)
2506 int c1, c2;
2507 for (;;)
2509 if (edit->curs1 >= edit->last_byte)
2510 break;
2511 c1 = edit_delete (edit, 1);
2512 c2 = edit_get_byte (edit, edit->curs1);
2513 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2514 break;
2515 if (!(my_type_of (c1) & my_type_of (c2)))
2516 break;
2520 static void
2521 edit_left_delete_word (WEdit * edit)
2523 int c1, c2;
2524 for (;;)
2526 if (edit->curs1 <= 0)
2527 break;
2528 c1 = edit_backspace (edit, 1);
2529 c2 = edit_get_byte (edit, edit->curs1 - 1);
2530 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2531 break;
2532 if (!(my_type_of (c1) & my_type_of (c2)))
2533 break;
2538 the start column position is not recorded, and hence does not
2539 undo as it happed. But who would notice.
2541 static void
2542 edit_do_undo (WEdit * edit)
2544 long ac;
2545 long count = 0;
2547 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2548 edit->over_col = 0;
2549 while ((ac = pop_action (edit)) < KEY_PRESS)
2551 switch ((int) ac)
2553 case STACK_BOTTOM:
2554 goto done_undo;
2555 case CURS_RIGHT:
2556 edit_cursor_move (edit, 1);
2557 break;
2558 case CURS_LEFT:
2559 edit_cursor_move (edit, -1);
2560 break;
2561 case BACKSPACE:
2562 edit_backspace (edit, 1);
2563 break;
2564 case DELCHAR:
2565 edit_delete (edit, 1);
2566 break;
2567 case COLUMN_ON:
2568 edit->column_highlight = 1;
2569 break;
2570 case COLUMN_OFF:
2571 edit->column_highlight = 0;
2572 break;
2574 if (ac >= 256 && ac < 512)
2575 edit_insert_ahead (edit, ac - 256);
2576 if (ac >= 0 && ac < 256)
2577 edit_insert (edit, ac);
2579 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
2581 edit->mark1 = ac - MARK_1;
2582 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2584 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
2586 edit->mark2 = ac - MARK_2;
2587 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2589 if (count++)
2590 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2593 if (edit->start_display > ac - KEY_PRESS)
2595 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2596 edit->force |= REDRAW_PAGE;
2598 else if (edit->start_display < ac - KEY_PRESS)
2600 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2601 edit->force |= REDRAW_PAGE;
2603 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2604 edit_update_curs_row (edit);
2606 done_undo:;
2607 edit->stack_disable = 0;
2610 static void
2611 edit_delete_to_line_end (WEdit * edit)
2613 while (edit_get_byte (edit, edit->curs1) != '\n')
2615 if (!edit->curs2)
2616 break;
2617 edit_delete (edit, 1);
2621 static void
2622 edit_delete_to_line_begin (WEdit * edit)
2624 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2626 if (!edit->curs1)
2627 break;
2628 edit_backspace (edit, 1);
2632 void
2633 edit_delete_line (WEdit * edit)
2636 * Delete right part of the line.
2637 * Note that edit_get_byte() returns '\n' when byte position is
2638 * beyond EOF.
2640 while (edit_get_byte (edit, edit->curs1) != '\n')
2642 (void) edit_delete (edit, 1);
2646 * Delete '\n' char.
2647 * Note that edit_delete() will not corrupt anything if called while
2648 * cursor position is EOF.
2650 (void) edit_delete (edit, 1);
2653 * Delete left part of the line.
2654 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2656 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2658 (void) edit_backspace (edit, 1);
2662 void
2663 insert_spaces_tab (WEdit * edit, int half)
2665 int i;
2666 edit_update_curs_col (edit);
2667 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
2668 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2669 while (i > 0)
2671 edit_insert (edit, ' ');
2672 i -= space_width;
2676 static int
2677 is_aligned_on_a_tab (WEdit * edit)
2679 edit_update_curs_col (edit);
2680 return !((edit->curs_col % (TAB_SIZE * space_width))
2681 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2684 static int
2685 right_of_four_spaces (WEdit * edit)
2687 int i, ch = 0;
2688 for (i = 1; i <= HALF_TAB_SIZE; i++)
2689 ch |= edit_get_byte (edit, edit->curs1 - i);
2690 if (ch == ' ')
2691 return is_aligned_on_a_tab (edit);
2692 return 0;
2695 static int
2696 left_of_four_spaces (WEdit * edit)
2698 int i, ch = 0;
2699 for (i = 0; i < HALF_TAB_SIZE; i++)
2700 ch |= edit_get_byte (edit, edit->curs1 + i);
2701 if (ch == ' ')
2702 return is_aligned_on_a_tab (edit);
2703 return 0;
2707 edit_indent_width (WEdit * edit, long p)
2709 long q = p;
2710 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2711 q++;
2712 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2715 void
2716 edit_insert_indent (WEdit * edit, int indent)
2718 if (!option_fill_tabs_with_spaces)
2720 while (indent >= TAB_SIZE)
2722 edit_insert (edit, '\t');
2723 indent -= TAB_SIZE;
2726 while (indent-- > 0)
2727 edit_insert (edit, ' ');
2730 static void
2731 edit_auto_indent (WEdit * edit)
2733 long p;
2734 char c;
2735 p = edit->curs1;
2736 /* use the previous line as a template */
2737 p = edit_move_backward (edit, p, 1);
2738 /* copy the leading whitespace of the line */
2739 for (;;)
2740 { /* no range check - the line _is_ \n-terminated */
2741 c = edit_get_byte (edit, p++);
2742 if (c != ' ' && c != '\t')
2743 break;
2744 edit_insert (edit, c);
2748 static inline void
2749 edit_double_newline (WEdit * edit)
2751 edit_insert (edit, '\n');
2752 if (edit_get_byte (edit, edit->curs1) == '\n')
2753 return;
2754 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2755 return;
2756 edit->force |= REDRAW_PAGE;
2757 edit_insert (edit, '\n');
2760 static inline void
2761 edit_tab_cmd (WEdit * edit)
2763 int i;
2765 if (option_fake_half_tabs)
2767 if (is_in_indent (edit))
2769 /*insert a half tab (usually four spaces) unless there is a
2770 half tab already behind, then delete it and insert a
2771 full tab. */
2772 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
2774 for (i = 1; i <= HALF_TAB_SIZE; i++)
2775 edit_backspace (edit, 1);
2776 edit_insert (edit, '\t');
2778 else
2780 insert_spaces_tab (edit, 1);
2782 return;
2785 if (option_fill_tabs_with_spaces)
2787 insert_spaces_tab (edit, 0);
2789 else
2791 edit_insert (edit, '\t');
2795 static void
2796 check_and_wrap_line (WEdit * edit)
2798 int curs, c;
2799 if (!option_typewriter_wrap)
2800 return;
2801 edit_update_curs_col (edit);
2802 if (edit->curs_col < option_word_wrap_line_length)
2803 return;
2804 curs = edit->curs1;
2805 for (;;)
2807 curs--;
2808 c = edit_get_byte (edit, curs);
2809 if (c == '\n' || curs <= 0)
2811 edit_insert (edit, '\n');
2812 return;
2814 if (c == ' ' || c == '\t')
2816 int current = edit->curs1;
2817 edit_cursor_move (edit, curs - edit->curs1 + 1);
2818 edit_insert (edit, '\n');
2819 edit_cursor_move (edit, current - edit->curs1 + 1);
2820 return;
2825 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
2827 void
2828 edit_push_key_press (WEdit * edit)
2830 edit_push_action (edit, KEY_PRESS + edit->start_display);
2831 if (edit->mark2 == -1)
2832 edit_push_action (edit, MARK_1 + edit->mark1);
2835 /* this find the matching bracket in either direction, and sets edit->bracket */
2836 static long
2837 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2839 const char *const b = "{}{[][()(", *p;
2840 int i = 1, a, inc = -1, c, d, n = 0;
2841 unsigned long j = 0;
2842 long q;
2843 edit_update_curs_row (edit);
2844 c = edit_get_byte (edit, edit->curs1);
2845 p = strchr (b, c);
2846 /* no limit */
2847 if (!furthest_bracket_search)
2848 furthest_bracket_search--;
2849 /* not on a bracket at all */
2850 if (!p)
2851 return -1;
2852 /* the matching bracket */
2853 d = p[1];
2854 /* going left or right? */
2855 if (strchr ("{[(", c))
2856 inc = 1;
2857 for (q = edit->curs1 + inc;; q += inc)
2859 /* out of buffer? */
2860 if (q >= edit->last_byte || q < 0)
2861 break;
2862 a = edit_get_byte (edit, q);
2863 /* don't want to eat CPU */
2864 if (j++ > furthest_bracket_search)
2865 break;
2866 /* out of screen? */
2867 if (in_screen)
2869 if (q < edit->start_display)
2870 break;
2871 /* count lines if searching downward */
2872 if (inc > 0 && a == '\n')
2873 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2874 break;
2876 /* count bracket depth */
2877 i += (a == c) - (a == d);
2878 /* return if bracket depth is zero */
2879 if (!i)
2880 return q;
2882 /* no match */
2883 return -1;
2886 static long last_bracket = -1;
2888 void
2889 edit_find_bracket (WEdit * edit)
2891 edit->bracket = edit_get_bracket (edit, 1, 10000);
2892 if (last_bracket != edit->bracket)
2893 edit->force |= REDRAW_PAGE;
2894 last_bracket = edit->bracket;
2897 static inline void
2898 edit_goto_matching_bracket (WEdit * edit)
2900 long q;
2902 q = edit_get_bracket (edit, 0, 0);
2903 if (q >= 0)
2905 edit->bracket = edit->curs1;
2906 edit->force |= REDRAW_PAGE;
2907 edit_cursor_move (edit, q - edit->curs1);
2912 * This executes a command as though the user initiated it through a key
2913 * press. Callback with WIDGET_KEY as a message calls this after
2914 * translating the key press. This function can be used to pass any
2915 * command to the editor. Note that the screen wouldn't update
2916 * automatically. Either of command or char_for_insertion must be
2917 * passed as -1. Commands are executed, and char_for_insertion is
2918 * inserted at the cursor.
2920 void
2921 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
2923 if (command == CK_Begin_Record_Macro)
2925 edit->macro_i = 0;
2926 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2927 return;
2929 if (command == CK_End_Record_Macro && edit->macro_i != -1)
2931 edit->force |= REDRAW_COMPLETELY;
2932 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2933 edit->macro_i = -1;
2934 return;
2936 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
2938 edit->macro[edit->macro_i].command = command;
2939 edit->macro[edit->macro_i++].ch = char_for_insertion;
2941 /* record the beginning of a set of editing actions initiated by a key press */
2942 if (command != CK_Undo && command != CK_Ext_Mode)
2943 edit_push_key_press (edit);
2945 edit_execute_cmd (edit, command, char_for_insertion);
2946 if (edit->column_highlight)
2947 edit->force |= REDRAW_PAGE;
2950 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
2953 This executes a command at a lower level than macro recording.
2954 It also does not push a key_press onto the undo stack. This means
2955 that if it is called many times, a single undo command will undo
2956 all of them. It also does not check for the Undo command.
2958 void
2959 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
2961 edit->force |= REDRAW_LINE;
2963 /* The next key press will unhighlight the found string, so update
2964 * the whole page */
2965 if (edit->found_len || edit->column_highlight)
2966 edit->force |= REDRAW_PAGE;
2968 if (command / 100 == 6)
2969 { /* a highlight command like shift-arrow */
2970 edit->column_highlight = 0;
2971 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
2973 edit_mark_cmd (edit, 1); /* clear */
2974 edit_mark_cmd (edit, 0); /* marking on */
2976 edit->highlight = 1;
2978 else
2979 { /* any other command */
2980 if (edit->highlight)
2981 edit_mark_cmd (edit, 0); /* clear */
2982 edit->highlight = 0;
2985 /* first check for undo */
2986 if (command == CK_Undo)
2988 edit_do_undo (edit);
2989 edit->found_len = 0;
2990 edit->prev_col = edit_get_col (edit);
2991 edit->search_start = edit->curs1;
2992 return;
2995 /* An ordinary key press */
2996 if (char_for_insertion >= 0)
2998 /* if non persistent selection and text selected */
2999 if (!option_persistent_selections)
3001 if (edit->mark1 != edit->mark2)
3002 edit_block_delete_cmd (edit);
3004 if (edit->overwrite)
3006 /* remove char only one time, after input first byte, multibyte chars */
3007 if ((!utf8_display || edit->charpoint == 0)
3008 && edit_get_byte (edit, edit->curs1) != '\n')
3009 edit_delete (edit, 0);
3011 if (option_cursor_beyond_eol && edit->over_col > 0)
3012 edit_insert_over (edit);
3013 #ifdef HAVE_CHARSET
3014 if (char_for_insertion > 255 && utf8_display == 0)
3016 unsigned char str[6 + 1];
3017 size_t i = 0;
3018 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3019 if (res == 0)
3021 str[0] = '.';
3022 str[1] = '\0';
3024 else
3026 str[res] = '\0';
3028 while (str[i] != 0 && i <= 6)
3030 char_for_insertion = str[i];
3031 edit_insert (edit, char_for_insertion);
3032 i++;
3035 else
3036 #endif
3037 edit_insert (edit, char_for_insertion);
3039 if (option_auto_para_formatting)
3041 format_paragraph (edit, 0);
3042 edit->force |= REDRAW_PAGE;
3044 else
3045 check_and_wrap_line (edit);
3046 edit->found_len = 0;
3047 edit->prev_col = edit_get_col (edit);
3048 edit->search_start = edit->curs1;
3049 edit_find_bracket (edit);
3050 return;
3053 switch (command)
3055 case CK_Begin_Page:
3056 case CK_End_Page:
3057 case CK_Begin_Page_Highlight:
3058 case CK_End_Page_Highlight:
3059 case CK_Word_Left:
3060 case CK_Word_Right:
3061 case CK_Up:
3062 case CK_Down:
3063 case CK_Left:
3064 case CK_Right:
3065 if (edit->mark2 >= 0)
3067 if (!option_persistent_selections)
3069 if (edit->column_highlight)
3070 edit_push_action (edit, COLUMN_ON);
3071 edit->column_highlight = 0;
3072 edit_mark_cmd (edit, 1);
3077 switch (command)
3079 case CK_Begin_Page:
3080 case CK_End_Page:
3081 case CK_Begin_Page_Highlight:
3082 case CK_End_Page_Highlight:
3083 case CK_Word_Left:
3084 case CK_Word_Right:
3085 case CK_Up:
3086 case CK_Down:
3087 case CK_Word_Left_Highlight:
3088 case CK_Word_Right_Highlight:
3089 case CK_Up_Highlight:
3090 case CK_Down_Highlight:
3091 case CK_Up_Alt_Highlight:
3092 case CK_Down_Alt_Highlight:
3093 if (edit->mark2 == -1)
3094 break; /*marking is following the cursor: may need to highlight a whole line */
3095 case CK_Left:
3096 case CK_Right:
3097 case CK_Left_Highlight:
3098 case CK_Right_Highlight:
3099 edit->force |= REDRAW_CHAR_ONLY;
3102 /* basic cursor key commands */
3103 switch (command)
3105 case CK_BackSpace:
3106 /* if non persistent selection and text selected */
3107 if (!option_persistent_selections)
3109 if (edit->mark1 != edit->mark2)
3111 edit_block_delete_cmd (edit);
3112 break;
3115 if (option_cursor_beyond_eol && edit->over_col > 0)
3117 edit->over_col--;
3118 break;
3120 if (option_backspace_through_tabs && is_in_indent (edit))
3122 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3123 edit_backspace (edit, 1);
3124 break;
3126 else
3128 if (option_fake_half_tabs)
3130 int i;
3131 if (is_in_indent (edit) && right_of_four_spaces (edit))
3133 for (i = 0; i < HALF_TAB_SIZE; i++)
3134 edit_backspace (edit, 1);
3135 break;
3139 edit_backspace (edit, 0);
3140 break;
3141 case CK_Delete:
3142 /* if non persistent selection and text selected */
3143 if (!option_persistent_selections)
3145 if (edit->mark1 != edit->mark2)
3147 edit_block_delete_cmd (edit);
3148 break;
3152 if (option_cursor_beyond_eol && edit->over_col > 0)
3153 edit_insert_over (edit);
3155 if (option_fake_half_tabs)
3157 int i;
3158 if (is_in_indent (edit) && left_of_four_spaces (edit))
3160 for (i = 1; i <= HALF_TAB_SIZE; i++)
3161 edit_delete (edit, 1);
3162 break;
3165 edit_delete (edit, 0);
3166 break;
3167 case CK_Delete_Word_Left:
3168 edit->over_col = 0;
3169 edit_left_delete_word (edit);
3170 break;
3171 case CK_Delete_Word_Right:
3172 if (option_cursor_beyond_eol && edit->over_col > 0)
3173 edit_insert_over (edit);
3175 edit_right_delete_word (edit);
3176 break;
3177 case CK_Delete_Line:
3178 edit_delete_line (edit);
3179 break;
3180 case CK_Delete_To_Line_End:
3181 edit_delete_to_line_end (edit);
3182 break;
3183 case CK_Delete_To_Line_Begin:
3184 edit_delete_to_line_begin (edit);
3185 break;
3186 case CK_Enter:
3187 edit->over_col = 0;
3188 if (option_auto_para_formatting)
3190 edit_double_newline (edit);
3191 if (option_return_does_auto_indent)
3192 edit_auto_indent (edit);
3193 format_paragraph (edit, 0);
3195 else
3197 edit_insert (edit, '\n');
3198 if (option_return_does_auto_indent)
3200 edit_auto_indent (edit);
3203 break;
3204 case CK_Return:
3205 edit_insert (edit, '\n');
3206 break;
3208 case CK_Page_Up_Alt_Highlight:
3209 edit->column_highlight = 1;
3210 case CK_Page_Up:
3211 case CK_Page_Up_Highlight:
3212 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3213 break;
3214 case CK_Page_Down_Alt_Highlight:
3215 edit->column_highlight = 1;
3216 case CK_Page_Down:
3217 case CK_Page_Down_Highlight:
3218 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3219 break;
3220 case CK_Left_Alt_Highlight:
3221 edit->column_highlight = 1;
3222 case CK_Left:
3223 case CK_Left_Highlight:
3224 if (option_fake_half_tabs)
3226 if (is_in_indent (edit) && right_of_four_spaces (edit))
3228 if (option_cursor_beyond_eol && edit->over_col > 0)
3229 edit->over_col--;
3230 else
3231 edit_cursor_move (edit, -HALF_TAB_SIZE);
3232 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3233 break;
3236 edit_left_char_move_cmd (edit);
3237 break;
3238 case CK_Right_Alt_Highlight:
3239 edit->column_highlight = 1;
3240 case CK_Right:
3241 case CK_Right_Highlight:
3242 if (option_fake_half_tabs)
3244 if (is_in_indent (edit) && left_of_four_spaces (edit))
3246 edit_cursor_move (edit, HALF_TAB_SIZE);
3247 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3248 break;
3251 edit_right_char_move_cmd (edit);
3252 break;
3253 case CK_Begin_Page:
3254 case CK_Begin_Page_Highlight:
3255 edit_begin_page (edit);
3256 break;
3257 case CK_End_Page:
3258 case CK_End_Page_Highlight:
3259 edit_end_page (edit);
3260 break;
3261 case CK_Word_Left:
3262 case CK_Word_Left_Highlight:
3263 edit->over_col = 0;
3264 edit_left_word_move_cmd (edit);
3265 break;
3266 case CK_Word_Right:
3267 case CK_Word_Right_Highlight:
3268 edit->over_col = 0;
3269 edit_right_word_move_cmd (edit);
3270 break;
3271 case CK_Up_Alt_Highlight:
3272 edit->column_highlight = 1;
3273 case CK_Up:
3274 case CK_Up_Highlight:
3275 edit_move_up (edit, 1, 0);
3276 break;
3277 case CK_Down_Alt_Highlight:
3278 edit->column_highlight = 1;
3279 case CK_Down:
3280 case CK_Down_Highlight:
3281 edit_move_down (edit, 1, 0);
3282 break;
3283 case CK_Paragraph_Up_Alt_Highlight:
3284 edit->column_highlight = 1;
3285 case CK_Paragraph_Up:
3286 case CK_Paragraph_Up_Highlight:
3287 edit_move_up_paragraph (edit, 0);
3288 break;
3289 case CK_Paragraph_Down_Alt_Highlight:
3290 edit->column_highlight = 1;
3291 case CK_Paragraph_Down:
3292 case CK_Paragraph_Down_Highlight:
3293 edit_move_down_paragraph (edit, 0);
3294 break;
3295 case CK_Scroll_Up_Alt_Highlight:
3296 edit->column_highlight = 1;
3297 case CK_Scroll_Up:
3298 case CK_Scroll_Up_Highlight:
3299 edit_move_up (edit, 1, 1);
3300 break;
3301 case CK_Scroll_Down_Alt_Highlight:
3302 edit->column_highlight = 1;
3303 case CK_Scroll_Down:
3304 case CK_Scroll_Down_Highlight:
3305 edit_move_down (edit, 1, 1);
3306 break;
3307 case CK_Home:
3308 case CK_Home_Highlight:
3309 edit_cursor_to_bol (edit);
3310 break;
3311 case CK_End:
3312 case CK_End_Highlight:
3313 edit_cursor_to_eol (edit);
3314 break;
3315 case CK_Tab:
3316 /* if text marked shift block */
3317 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3319 if (edit->mark2 < 0)
3320 edit_mark_cmd (edit, 0);
3321 edit_move_block_to_right (edit);
3323 else
3325 if (option_cursor_beyond_eol)
3326 edit_insert_over (edit);
3327 edit_tab_cmd (edit);
3328 if (option_auto_para_formatting)
3330 format_paragraph (edit, 0);
3331 edit->force |= REDRAW_PAGE;
3333 else
3335 check_and_wrap_line (edit);
3338 break;
3340 case CK_Toggle_Insert:
3341 edit->overwrite = (edit->overwrite == 0);
3342 break;
3344 case CK_Mark:
3345 if (edit->mark2 >= 0)
3347 if (edit->column_highlight)
3348 edit_push_action (edit, COLUMN_ON);
3349 edit->column_highlight = 0;
3351 edit_mark_cmd (edit, 0);
3352 break;
3353 case CK_Column_Mark:
3354 if (!edit->column_highlight)
3355 edit_push_action (edit, COLUMN_OFF);
3356 edit->column_highlight = 1;
3357 edit_mark_cmd (edit, 0);
3358 break;
3359 case CK_Mark_All:
3360 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3361 edit->force |= REDRAW_PAGE;
3362 break;
3363 case CK_Unmark:
3364 if (edit->column_highlight)
3365 edit_push_action (edit, COLUMN_ON);
3366 edit->column_highlight = 0;
3367 edit_mark_cmd (edit, 1);
3368 break;
3370 case CK_Toggle_Line_State:
3371 option_line_state = !option_line_state;
3372 if (option_line_state)
3374 option_line_state_width = LINE_STATE_WIDTH;
3376 else
3378 option_line_state_width = 0;
3380 edit->force |= REDRAW_PAGE;
3381 break;
3383 case CK_Toggle_Show_Margin:
3384 show_right_margin = !show_right_margin;
3385 edit->force |= REDRAW_PAGE;
3386 break;
3388 case CK_Toggle_Bookmark:
3389 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3390 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3391 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3392 else
3393 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3394 break;
3395 case CK_Flush_Bookmarks:
3396 book_mark_flush (edit, BOOK_MARK_COLOR);
3397 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3398 edit->force |= REDRAW_PAGE;
3399 break;
3400 case CK_Next_Bookmark:
3401 if (edit->book_mark)
3403 struct _book_mark *p;
3404 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3405 if (p->next)
3407 p = p->next;
3408 if (p->line >= edit->start_line + edit->num_widget_lines
3409 || p->line < edit->start_line)
3410 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3411 edit_move_to_line (edit, p->line);
3414 break;
3415 case CK_Prev_Bookmark:
3416 if (edit->book_mark)
3418 struct _book_mark *p;
3419 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3420 while (p->line == edit->curs_line)
3421 if (p->prev)
3422 p = p->prev;
3423 if (p->line >= 0)
3425 if (p->line >= edit->start_line + edit->num_widget_lines
3426 || p->line < edit->start_line)
3427 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3428 edit_move_to_line (edit, p->line);
3431 break;
3433 case CK_Beginning_Of_Text:
3434 case CK_Beginning_Of_Text_Highlight:
3435 edit_move_to_top (edit);
3436 break;
3437 case CK_End_Of_Text:
3438 case CK_End_Of_Text_Highlight:
3439 edit_move_to_bottom (edit);
3440 break;
3442 case CK_Copy:
3443 if (option_cursor_beyond_eol && edit->over_col > 0)
3444 edit_insert_over (edit);
3445 edit_block_copy_cmd (edit);
3446 break;
3447 case CK_Remove:
3448 edit_block_delete_cmd (edit);
3449 break;
3450 case CK_Move:
3451 if (option_cursor_beyond_eol && edit->over_col > 0)
3452 edit_insert_over (edit);
3453 edit_block_move_cmd (edit);
3454 break;
3456 case CK_Shift_Block_Left:
3457 if (edit->mark1 != edit->mark2)
3458 edit_move_block_to_left (edit);
3459 break;
3460 case CK_Shift_Block_Right:
3461 if (edit->mark1 != edit->mark2)
3462 edit_move_block_to_right (edit);
3463 break;
3464 case CK_XStore:
3465 edit_copy_to_X_buf_cmd (edit);
3466 break;
3467 case CK_XCut:
3468 edit_cut_to_X_buf_cmd (edit);
3469 break;
3470 case CK_XPaste:
3471 /* if non persistent selection and text selected */
3472 if (!option_persistent_selections)
3474 if (edit->mark1 != edit->mark2)
3475 edit_block_delete_cmd (edit);
3477 if (option_cursor_beyond_eol && edit->over_col > 0)
3478 edit_insert_over (edit);
3479 edit_paste_from_X_buf_cmd (edit);
3480 break;
3481 case CK_Selection_History:
3482 edit_paste_from_history (edit);
3483 break;
3485 case CK_Save_As:
3486 edit_save_as_cmd (edit);
3487 break;
3488 case CK_Save:
3489 edit_save_confirm_cmd (edit);
3490 break;
3491 case CK_Load:
3492 edit_load_cmd (edit, EDIT_FILE_COMMON);
3493 break;
3494 case CK_Save_Block:
3495 edit_save_block_cmd (edit);
3496 break;
3497 case CK_Insert_File:
3498 edit_insert_file_cmd (edit);
3499 break;
3501 case CK_Load_Prev_File:
3502 edit_load_back_cmd (edit);
3503 break;
3504 case CK_Load_Next_File:
3505 edit_load_forward_cmd (edit);
3506 break;
3508 case CK_Load_Syntax_File:
3509 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3510 break;
3511 case CK_Choose_Syntax:
3512 edit_syntax_dialog (edit, edit->syntax_type);
3513 break;
3515 case CK_Load_Menu_File:
3516 edit_load_cmd (edit, EDIT_FILE_MENU);
3517 break;
3519 case CK_Toggle_Syntax:
3520 option_syntax_highlighting ^= 1;
3521 if (option_syntax_highlighting == 1)
3522 edit_load_syntax (edit, NULL, edit->syntax_type);
3523 edit->force |= REDRAW_PAGE;
3524 break;
3526 case CK_Toggle_Tab_TWS:
3527 enable_show_tabs_tws ^= 1;
3528 edit->force |= REDRAW_PAGE;
3529 break;
3531 case CK_Find:
3532 edit_search_cmd (edit, 0);
3533 break;
3534 case CK_Find_Again:
3535 edit_search_cmd (edit, 1);
3536 break;
3537 case CK_Replace:
3538 edit_replace_cmd (edit, 0);
3539 break;
3540 case CK_Replace_Again:
3541 edit_replace_cmd (edit, 1);
3542 break;
3543 case CK_Complete_Word:
3544 /* if text marked shift block */
3545 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3547 edit_move_block_to_left (edit);
3549 else
3551 edit_complete_word_cmd (edit);
3553 break;
3554 case CK_Find_Definition:
3555 edit_get_match_keyword_cmd (edit);
3556 break;
3557 case CK_Quit:
3558 dlg_stop (edit->widget.owner);
3559 break;
3560 case CK_New:
3561 edit_new_cmd (edit);
3562 break;
3563 case CK_Help:
3564 edit_help_cmd (edit);
3565 break;
3566 case CK_Refresh:
3567 edit_refresh_cmd (edit);
3568 break;
3569 case CK_SaveSetupCmd:
3570 save_setup_cmd ();
3571 break;
3572 case CK_About:
3573 edit_about ();
3574 break;
3575 case CK_LearnKeys:
3576 learn_keys ();
3577 break;
3578 case CK_Edit_Options:
3579 edit_options_dialog (edit);
3580 break;
3581 case CK_Edit_Save_Mode:
3582 menu_save_mode_cmd ();
3583 break;
3584 case CK_Date:
3586 char s[BUF_MEDIUM];
3587 /* fool gcc to prevent a Y2K warning */
3588 char time_format[] = "_c";
3589 time_format[0] = '%';
3591 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3592 edit_print_string (edit, s);
3593 edit->force |= REDRAW_PAGE;
3594 break;
3596 break;
3597 case CK_Goto:
3598 edit_goto_cmd (edit);
3599 break;
3600 case CK_Paragraph_Format:
3601 format_paragraph (edit, 1);
3602 edit->force |= REDRAW_PAGE;
3603 break;
3604 case CK_Delete_Macro:
3605 edit_delete_macro_cmd (edit);
3606 break;
3607 case CK_Match_Bracket:
3608 edit_goto_matching_bracket (edit);
3609 break;
3610 case CK_User_Menu:
3611 user_menu (edit);
3612 break;
3613 case CK_Sort:
3614 edit_sort_cmd (edit);
3615 break;
3616 case CK_ExtCmd:
3617 edit_ext_cmd (edit);
3618 break;
3619 case CK_Mail:
3620 edit_mail_dialog (edit);
3621 break;
3622 case CK_Shell:
3623 view_other_cmd ();
3624 break;
3625 case CK_SelectCodepage:
3626 edit_select_codepage_cmd (edit);
3627 break;
3628 case CK_Insert_Literal:
3629 edit_insert_literal_cmd (edit);
3630 break;
3631 case CK_Execute_Macro:
3632 edit_execute_macro_cmd (edit);
3633 break;
3634 case CK_Begin_End_Macro:
3635 edit_begin_end_macro_cmd (edit);
3636 break;
3637 case CK_Ext_Mode:
3638 edit->extmod = 1;
3639 break;
3640 default:
3641 break;
3644 /* CK_Pipe_Block */
3645 if ((command / 1000) == 1) /* a shell command */
3646 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3647 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3648 { /* a macro command */
3649 struct macro m[MAX_MACRO_LENGTH];
3650 int nm;
3651 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3652 edit_execute_macro (edit, m, nm);
3655 /* keys which must set the col position, and the search vars */
3656 switch (command)
3658 case CK_Find:
3659 case CK_Find_Again:
3660 case CK_Replace:
3661 case CK_Replace_Again:
3662 case CK_Complete_Word:
3663 edit->prev_col = edit_get_col (edit);
3664 break;
3665 case CK_Up:
3666 case CK_Up_Highlight:
3667 case CK_Up_Alt_Highlight:
3668 case CK_Down:
3669 case CK_Down_Highlight:
3670 case CK_Down_Alt_Highlight:
3671 case CK_Page_Up:
3672 case CK_Page_Up_Highlight:
3673 case CK_Page_Up_Alt_Highlight:
3674 case CK_Page_Down:
3675 case CK_Page_Down_Highlight:
3676 case CK_Page_Down_Alt_Highlight:
3677 case CK_Beginning_Of_Text:
3678 case CK_Beginning_Of_Text_Highlight:
3679 case CK_End_Of_Text:
3680 case CK_End_Of_Text_Highlight:
3681 case CK_Paragraph_Up:
3682 case CK_Paragraph_Up_Highlight:
3683 case CK_Paragraph_Up_Alt_Highlight:
3684 case CK_Paragraph_Down:
3685 case CK_Paragraph_Down_Highlight:
3686 case CK_Paragraph_Down_Alt_Highlight:
3687 case CK_Scroll_Up:
3688 case CK_Scroll_Up_Highlight:
3689 case CK_Scroll_Up_Alt_Highlight:
3690 case CK_Scroll_Down:
3691 case CK_Scroll_Down_Highlight:
3692 case CK_Scroll_Down_Alt_Highlight:
3693 edit->search_start = edit->curs1;
3694 edit->found_len = 0;
3695 break;
3696 default:
3697 edit->found_len = 0;
3698 edit->prev_col = edit_get_col (edit);
3699 edit->search_start = edit->curs1;
3701 edit_find_bracket (edit);
3703 if (option_auto_para_formatting)
3705 switch (command)
3707 case CK_BackSpace:
3708 case CK_Delete:
3709 case CK_Delete_Word_Left:
3710 case CK_Delete_Word_Right:
3711 case CK_Delete_To_Line_End:
3712 case CK_Delete_To_Line_Begin:
3713 format_paragraph (edit, 0);
3714 edit->force |= REDRAW_PAGE;
3720 static void
3721 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
3723 int i = 0;
3725 if (edit->macro_depth++ > 256)
3727 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3728 edit->macro_depth--;
3729 return;
3731 edit->force |= REDRAW_PAGE;
3732 for (; i < n; i++)
3734 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3736 edit_update_screen (edit);
3737 edit->macro_depth--;
3740 /* User edit menu, like user menu (F2) but only in editor. */
3741 static void
3742 user_menu (WEdit * edit)
3744 char *block_file;
3745 int nomark;
3746 long start_mark, end_mark;
3747 struct stat status;
3749 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3751 nomark = eval_marks (edit, &start_mark, &end_mark);
3752 if (nomark == 0)
3753 edit_save_block (edit, block_file, start_mark, end_mark);
3755 /* run shell scripts from menu */
3756 user_menu_cmd (edit);
3758 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
3760 int rc = 0;
3761 FILE *fd;
3763 if (nomark == 0)
3765 /* i.e. we have marked block */
3766 rc = edit_block_delete_cmd (edit);
3769 if (rc == 0)
3770 edit_insert_file (edit, block_file);
3772 /* truncate block file */
3773 fd = fopen (block_file, "w");
3774 if (fd != NULL)
3775 fclose (fd);
3777 edit_refresh_cmd (edit);
3778 edit->force |= REDRAW_COMPLETELY;
3780 g_free (block_file);
3783 void
3784 edit_stack_init (void)
3786 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3788 edit_history_moveto[edit_stack_iterator].filename = NULL;
3789 edit_history_moveto[edit_stack_iterator].line = -1;
3792 edit_stack_iterator = 0;
3795 void
3796 edit_stack_free (void)
3798 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3799 g_free (edit_history_moveto[edit_stack_iterator].filename);
3802 /* move i lines */
3803 void
3804 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3806 edit_move_updown (edit, i, do_scroll, TRUE);
3809 /* move i lines */
3810 void
3811 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3813 edit_move_updown (edit, i, do_scroll, FALSE);
3816 unsigned int
3817 edit_unlock_file (WEdit * edit)
3819 char *fullpath;
3820 unsigned int ret;
3822 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
3823 ret = unlock_file (fullpath);
3824 g_free (fullpath);
3826 return ret;
3829 unsigned int
3830 edit_lock_file (WEdit * edit)
3832 char *fullpath;
3833 unsigned int ret;
3835 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
3836 ret = lock_file (fullpath);
3837 g_free (fullpath);
3839 return ret;