Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / editor / edit.c
blob6030654b4da0c380f016d5454b5c15de086ee86e
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/lock.h"
52 #include "src/widget.h"
53 #include "src/cmd.h" /* view_other_cmd() */
54 #include "src/user.h" /* user_menu_cmd() */
55 #include "src/wtools.h" /* query_dialog() */
56 #include "lib/timefmt.h" /* time formatting */
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);
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 gchar *errmsg;
339 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
340 edit_error_dialog (_("Error"), errmsg);
341 g_free (errmsg);
342 return 1;
345 if (!edit->buffers2[buf2])
346 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
350 if (mc_read (file,
351 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
352 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
353 break;
355 for (buf = buf2 - 1; buf >= 0; buf--)
357 /* edit->buffers2[0] is already allocated */
358 if (!edit->buffers2[buf])
359 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
360 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
361 break;
363 ret = 0;
365 while (0);
366 if (ret)
368 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
369 edit_error_dialog (_("Error"), err_str);
370 g_free (err_str);
372 mc_close (file);
373 return ret;
376 /* detecting an error on save is easy: just check if every byte has been written. */
377 /* detecting an error on read, is not so easy 'cos there is not way to tell
378 whether you read everything or not. */
379 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
380 static const struct edit_filters
382 const char *read, *write, *extension;
383 } all_filters[] =
385 /* *INDENT-OFF* */
386 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
387 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
388 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
389 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
390 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
391 /* *INDENT-ON* */
394 /* Return index of the filter or -1 is there is no appropriate filter */
395 static int
396 edit_find_filter (const char *filename)
398 size_t i, l, e;
400 if (filename == NULL)
401 return -1;
403 l = strlen (filename);
404 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
406 e = strlen (all_filters[i].extension);
407 if (l > e)
408 if (!strcmp (all_filters[i].extension, filename + l - e))
409 return i;
411 return -1;
414 static char *
415 edit_get_filter (const char *filename)
417 int i;
418 char *p, *quoted_name;
420 i = edit_find_filter (filename);
421 if (i < 0)
422 return NULL;
424 quoted_name = name_quote (filename, 0);
425 p = g_strdup_printf (all_filters[i].read, quoted_name);
426 g_free (quoted_name);
427 return p;
430 char *
431 edit_get_write_filter (const char *write_name, const char *filename)
433 int i;
434 char *p, *writename;
436 i = edit_find_filter (filename);
437 if (i < 0)
438 return NULL;
440 writename = name_quote (write_name, 0);
441 p = g_strdup_printf (all_filters[i].write, writename);
442 g_free (writename);
443 return p;
446 static long
447 edit_insert_stream (WEdit * edit, FILE * f)
449 int c;
450 long i = 0;
451 while ((c = fgetc (f)) >= 0)
453 edit_insert (edit, c);
454 i++;
456 return i;
459 long
460 edit_write_stream (WEdit * edit, FILE * f)
462 long i;
464 if (edit->lb == LB_ASIS)
466 for (i = 0; i < edit->last_byte; i++)
467 if (fputc (edit_get_byte (edit, i), f) < 0)
468 break;
469 return i;
472 /* change line breaks */
473 for (i = 0; i < edit->last_byte; i++)
475 unsigned char c = edit_get_byte (edit, i);
477 if (!(c == '\n' || c == '\r'))
479 /* not line break */
480 if (fputc (c, f) < 0)
481 return i;
483 else
484 { /* (c == '\n' || c == '\r') */
485 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
487 switch (edit->lb)
489 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
490 /* put one line break unconditionally */
491 if (fputc ('\n', f) < 0)
492 return i;
494 i++; /* 2 chars are processed */
496 if (c == '\r' && c1 == '\n')
497 /* Windows line break; go to the next char */
498 break;
500 if (c == '\r' && c1 == '\r')
502 /* two Macintosh line breaks; put second line break */
503 if (fputc ('\n', f) < 0)
504 return i;
505 break;
508 if (fputc (c1, f) < 0)
509 return i;
510 break;
512 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
513 /* put one line break unconditionally */
514 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
515 return i;
517 if (c == '\r' && c1 == '\n')
518 /* Windows line break; go to the next char */
519 i++;
520 break;
522 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
523 /* put one line break unconditionally */
524 if (fputc ('\r', f) < 0)
525 return i;
527 i++; /* 2 chars are processed */
529 if (c == '\r' && c1 == '\n')
530 /* Windows line break; go to the next char */
531 break;
533 if (c == '\n' && c1 == '\n')
535 /* two Windows line breaks; put second line break */
536 if (fputc ('\r', f) < 0)
537 return i;
538 break;
541 if (fputc (c1, f) < 0)
542 return i;
543 break;
544 case LB_ASIS: /* default without changes */
545 break;
550 return edit->last_byte;
553 #define TEMP_BUF_LEN 1024
555 /* inserts a file at the cursor, returns 1 on success */
557 edit_insert_file (WEdit * edit, const char *filename)
559 char *p;
561 p = edit_get_filter (filename);
562 if (p != NULL)
564 FILE *f;
565 long current = edit->curs1;
566 f = (FILE *) popen (p, "r");
567 if (f != NULL)
569 edit_insert_stream (edit, f);
570 edit_cursor_move (edit, current - edit->curs1);
571 if (pclose (f) > 0)
573 char *errmsg;
574 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
575 edit_error_dialog (_("Error"), errmsg);
576 g_free (errmsg);
577 g_free (p);
578 return 0;
581 else
583 char *errmsg;
584 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
585 edit_error_dialog (_("Error"), errmsg);
586 g_free (errmsg);
587 g_free (p);
588 return 0;
590 g_free (p);
592 else
594 int i, file, blocklen;
595 long current = edit->curs1;
596 int vertical_insertion = 0;
597 char *buf;
598 file = mc_open (filename, O_RDONLY | O_BINARY);
599 if (file == -1)
600 return 0;
601 buf = g_malloc0 (TEMP_BUF_LEN);
602 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
603 if (blocklen > 0)
605 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
606 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
607 vertical_insertion = 1;
608 else
609 mc_lseek (file, 0, SEEK_SET);
611 if (vertical_insertion)
612 blocklen = edit_insert_column_of_text_from_file (edit, file);
613 else
614 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
615 for (i = 0; i < blocklen; i++)
616 edit_insert (edit, buf[i]);
617 edit_cursor_move (edit, current - edit->curs1);
618 g_free (buf);
619 mc_close (file);
620 if (blocklen != 0)
621 return 0;
623 return 1;
626 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
627 static int
628 check_file_access (WEdit * edit, const char *filename, struct stat *st)
630 int file;
631 gchar *errmsg = NULL;
633 /* Try opening an existing file */
634 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
636 if (file < 0)
639 * Try creating the file. O_EXCL prevents following broken links
640 * and opening existing files.
642 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
643 if (file < 0)
645 errmsg = g_strdup_printf (_("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 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
659 goto cleanup;
662 /* We want to open regular files only */
663 if (!S_ISREG (st->st_mode))
665 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
666 goto cleanup;
670 * Don't delete non-empty files.
671 * O_EXCL should prevent it, but let's be on the safe side.
673 if (st->st_size > 0)
674 edit->delete_file = 0;
676 if (st->st_size >= SIZE_LIMIT)
677 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
679 cleanup:
680 (void) mc_close (file);
682 if (errmsg != NULL)
684 edit_error_dialog (_("Error"), errmsg);
685 g_free (errmsg);
686 return 1;
688 return 0;
692 * Open the file and load it into the buffers, either directly or using
693 * a filter. Return 0 on success, 1 on error.
695 * Fast loading (edit_load_file_fast) is used when the file size is
696 * known. In this case the data is read into the buffers by blocks.
697 * If the file size is not known, the data is loaded byte by byte in
698 * edit_insert_file.
700 static int
701 edit_load_file (WEdit * edit)
703 int fast_load = 1;
705 /* Cannot do fast load if a filter is used */
706 if (edit_find_filter (edit->filename) >= 0)
707 fast_load = 0;
710 * VFS may report file size incorrectly, and slow load is not a big
711 * deal considering overhead in VFS.
713 if (!vfs_file_is_local (edit->filename))
714 fast_load = 0;
717 * FIXME: line end translation should disable fast loading as well
718 * Consider doing fseek() to the end and ftell() for the real size.
721 if (*edit->filename)
723 /* If we are dealing with a real file, check that it exists */
724 if (check_file_access (edit, edit->filename, &edit->stat1))
725 return 1;
727 else
729 /* nothing to load */
730 fast_load = 0;
733 edit_init_buffers (edit);
735 if (fast_load)
737 edit->last_byte = edit->stat1.st_size;
738 edit_load_file_fast (edit, edit->filename);
739 /* If fast load was used, the number of lines wasn't calculated */
740 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
742 else
744 edit->last_byte = 0;
745 if (*edit->filename)
747 edit->stack_disable = 1;
748 if (!edit_insert_file (edit, edit->filename))
750 edit_clean (edit);
751 return 1;
753 edit->stack_disable = 0;
756 edit->lb = LB_ASIS;
757 return 0;
760 /* Restore saved cursor position in the file */
761 static void
762 edit_load_position (WEdit * edit)
764 char *filename;
765 long line, column;
766 off_t offset;
768 if (!edit->filename || !*edit->filename)
769 return;
771 filename = vfs_canon (edit->filename);
772 load_file_position (filename, &line, &column, &offset);
773 g_free (filename);
775 if (line > 0)
777 edit_move_to_line (edit, line - 1);
778 edit->prev_col = column;
780 else if (offset > 0)
782 edit_cursor_move (edit, offset);
783 line = edit->curs_line;
785 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
786 edit_move_display (edit, line - (edit->num_widget_lines / 2));
789 /* Save cursor position in the file */
790 static void
791 edit_save_position (WEdit * edit)
793 char *filename;
795 if (!edit->filename || !*edit->filename)
796 return;
798 filename = vfs_canon (edit->filename);
799 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
800 g_free (filename);
803 /* Clean the WEdit stricture except the widget part */
804 static void
805 edit_purge_widget (WEdit * edit)
807 size_t len = sizeof (WEdit) - sizeof (Widget);
808 char *start = (char *) edit + sizeof (Widget);
809 memset (start, 0, len);
810 edit->macro_i = -1; /* not recording a macro */
813 static void
814 edit_set_keymap (void)
816 editor_map = default_editor_keymap;
817 if (editor_keymap && editor_keymap->len > 0)
818 editor_map = (global_keymap_t *) editor_keymap->data;
820 editor_x_map = default_editor_x_keymap;
821 if (editor_x_keymap && editor_x_keymap->len > 0)
822 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
826 #define space_width 1
829 * Fill in the edit structure. Return NULL on failure. Pass edit as
830 * NULL to allocate a new structure.
832 * If line is 0, try to restore saved position. Otherwise put the
833 * cursor on that line and show it in the middle of the screen.
835 WEdit *
836 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
838 int to_free = 0;
839 option_auto_syntax = 1; /* Resetting to auto on every invokation */
840 if (option_line_state)
842 option_line_state_width = LINE_STATE_WIDTH;
844 else
846 option_line_state_width = 0;
848 if (!edit)
850 #ifdef ENABLE_NLS
852 * Expand option_whole_chars_search by national letters using
853 * current locale
856 static char option_whole_chars_search_buf[256];
858 if (option_whole_chars_search_buf != option_whole_chars_search)
860 size_t i;
861 size_t len = str_term_width1 (option_whole_chars_search);
863 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
865 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
867 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
869 option_whole_chars_search_buf[len++] = i;
873 option_whole_chars_search_buf[len] = 0;
874 option_whole_chars_search = option_whole_chars_search_buf;
876 #endif /* ENABLE_NLS */
877 edit = g_malloc0 (sizeof (WEdit));
878 edit->search = NULL;
879 to_free = 1;
881 edit_purge_widget (edit);
882 edit->num_widget_lines = lines;
883 edit->over_col = 0;
884 edit->num_widget_columns = columns;
885 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
886 edit->stat1.st_uid = getuid ();
887 edit->stat1.st_gid = getgid ();
888 edit->stat1.st_mtime = 0;
889 edit->bracket = -1;
890 edit->force |= REDRAW_PAGE;
891 edit_set_filename (edit, filename);
892 edit->stack_size = START_STACK_SIZE;
893 edit->stack_size_mask = START_STACK_SIZE - 1;
894 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
896 edit->utf8 = 0;
897 edit->converter = str_cnv_from_term;
898 edit_set_codeset (edit);
900 if (edit_load_file (edit))
902 /* edit_load_file already gives an error message */
903 if (to_free)
904 g_free (edit);
905 return 0;
908 edit->loading_done = 1;
909 edit->modified = 0;
910 edit->locked = 0;
911 edit_load_syntax (edit, NULL, NULL);
913 int color;
914 edit_get_syntax_color (edit, -1, &color);
917 /* load saved cursor position */
918 if ((line == 0) && option_save_position)
920 edit_load_position (edit);
922 else
924 if (line <= 0)
925 line = 1;
926 edit_move_display (edit, line - 1);
927 edit_move_to_line (edit, line - 1);
930 edit_set_keymap ();
932 return edit;
935 /* Clear the edit struct, freeing everything in it. Return 1 on success */
937 edit_clean (WEdit * edit)
939 int j = 0;
941 if (!edit)
942 return 0;
944 /* a stale lock, remove it */
945 if (edit->locked)
946 edit->locked = edit_unlock_file (edit);
948 /* save cursor position */
949 if (option_save_position)
950 edit_save_position (edit);
952 /* File specified on the mcedit command line and never saved */
953 if (edit->delete_file)
954 unlink (edit->filename);
956 edit_free_syntax_rules (edit);
957 book_mark_flush (edit, -1);
958 for (; j <= MAXBUFF; j++)
960 g_free (edit->buffers1[j]);
961 g_free (edit->buffers2[j]);
964 g_free (edit->undo_stack);
965 g_free (edit->filename);
966 g_free (edit->dir);
968 mc_search_free (edit->search);
969 edit->search = NULL;
971 if (edit->converter != str_cnv_from_term)
972 str_close_conv (edit->converter);
974 edit_purge_widget (edit);
976 return 1;
979 /* returns 1 on success */
981 edit_renew (WEdit * edit)
983 int lines = edit->num_widget_lines;
984 int columns = edit->num_widget_columns;
986 edit_clean (edit);
987 return (edit_init (edit, lines, columns, "", 0) != NULL);
991 * Load a new file into the editor. If it fails, preserve the old file.
992 * To do it, allocate a new widget, initialize it and, if the new file
993 * was loaded, copy the data to the old widget.
994 * Return 1 on success, 0 on failure.
997 edit_reload (WEdit * edit, const char *filename)
999 WEdit *e;
1000 int lines = edit->num_widget_lines;
1001 int columns = edit->num_widget_columns;
1003 e = g_malloc0 (sizeof (WEdit));
1004 e->widget = edit->widget;
1005 if (!edit_init (e, lines, columns, filename, 0))
1007 g_free (e);
1008 return 0;
1010 edit_clean (edit);
1011 memcpy (edit, e, sizeof (WEdit));
1012 g_free (e);
1013 return 1;
1017 * Load a new file into the editor and set line. If it fails, preserve the old file.
1018 * To do it, allocate a new widget, initialize it and, if the new file
1019 * was loaded, copy the data to the old widget.
1020 * Return 1 on success, 0 on failure.
1023 edit_reload_line (WEdit * edit, const char *filename, long line)
1025 WEdit *e;
1026 int lines = edit->num_widget_lines;
1027 int columns = edit->num_widget_columns;
1029 e = g_malloc0 (sizeof (WEdit));
1030 e->widget = edit->widget;
1031 if (!edit_init (e, lines, columns, filename, line))
1033 g_free (e);
1034 return 0;
1036 edit_clean (edit);
1037 memcpy (edit, e, sizeof (WEdit));
1038 g_free (e);
1039 return 1;
1042 void
1043 edit_set_codeset (WEdit *edit)
1045 #ifdef HAVE_CHARSET
1046 const char *cp_id;
1048 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
1050 if (cp_id != NULL)
1052 GIConv conv;
1053 conv = str_crt_conv_from (cp_id);
1054 if (conv != INVALID_CONV)
1056 if (edit->converter != str_cnv_from_term)
1057 str_close_conv (edit->converter);
1058 edit->converter = conv;
1062 if (cp_id != NULL)
1063 edit->utf8 = str_isutf8 (cp_id);
1064 #else
1065 (void) edit;
1066 #endif
1071 Recording stack for undo:
1072 The following is an implementation of a compressed stack. Identical
1073 pushes are recorded by a negative prefix indicating the number of times the
1074 same char was pushed. This saves space for repeated curs-left or curs-right
1075 delete etc.
1079 pushed: stored:
1083 b -3
1085 c --> -4
1091 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1092 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1093 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1094 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1095 position.
1097 The only way the cursor moves or the buffer is changed is through the routines:
1098 insert, backspace, insert_ahead, delete, and cursor_move.
1099 These record the reverse undo movements onto the stack each time they are
1100 called.
1102 Each key press results in a set of actions (insert; delete ...). So each time
1103 a key is pressed the current position of start_display is pushed as
1104 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1105 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1106 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1110 void
1111 edit_push_action (WEdit * edit, long c, ...)
1113 unsigned long sp = edit->stack_pointer;
1114 unsigned long spm1;
1115 long *t;
1117 /* first enlarge the stack if necessary */
1118 if (sp > edit->stack_size - 10)
1119 { /* say */
1120 if (option_max_undo < 256)
1121 option_max_undo = 256;
1122 if (edit->stack_size < (unsigned long) option_max_undo)
1124 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1125 if (t)
1127 edit->undo_stack = t;
1128 edit->stack_size <<= 1;
1129 edit->stack_size_mask = edit->stack_size - 1;
1133 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1134 if (edit->stack_disable)
1135 return;
1137 #ifdef FAST_MOVE_CURSOR
1138 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
1140 va_list ap;
1141 edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
1142 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1143 va_start (ap, c);
1144 c = -(va_arg (ap, int));
1145 va_end (ap);
1147 else
1148 #endif /* ! FAST_MOVE_CURSOR */
1149 if (edit->stack_bottom != sp
1150 && spm1 != edit->stack_bottom
1151 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
1153 int d;
1154 if (edit->undo_stack[spm1] < 0)
1156 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1157 if (d == c)
1159 if (edit->undo_stack[spm1] > -1000000000)
1161 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1162 edit->undo_stack[spm1]--;
1163 return;
1166 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1167 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1168 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1169 { /* a left then a right anihilate each other */
1170 if (edit->undo_stack[spm1] == -2)
1171 edit->stack_pointer = spm1;
1172 else
1173 edit->undo_stack[spm1]++;
1174 return;
1176 #endif
1178 else
1180 d = edit->undo_stack[spm1];
1181 if (d == c)
1183 if (c >= KEY_PRESS)
1184 return; /* --> no need to push multiple do-nothings */
1185 edit->undo_stack[sp] = -2;
1186 goto check_bottom;
1188 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1189 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1190 { /* a left then a right anihilate each other */
1191 edit->stack_pointer = spm1;
1192 return;
1194 #endif
1197 edit->undo_stack[sp] = c;
1199 check_bottom:
1200 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1202 /* if the sp wraps round and catches the stack_bottom then erase
1203 * the first set of actions on the stack to make space - by moving
1204 * stack_bottom forward one "key press" */
1205 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1206 if ((unsigned long) c == edit->stack_bottom ||
1207 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1210 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1212 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
1213 && edit->stack_bottom != edit->stack_pointer);
1215 /*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: */
1216 if (edit->stack_pointer != edit->stack_bottom
1217 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1218 edit->stack_bottom = edit->stack_pointer = 0;
1222 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1223 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1225 static long
1226 pop_action (WEdit * edit)
1228 long c;
1229 unsigned long sp = edit->stack_pointer;
1231 if (sp == edit->stack_bottom)
1232 return STACK_BOTTOM;
1234 sp = (sp - 1) & edit->stack_size_mask;
1235 c = edit->undo_stack[sp];
1236 if (c >= 0)
1238 /* edit->undo_stack[sp] = '@'; */
1239 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1240 return c;
1243 if (sp == edit->stack_bottom)
1244 return STACK_BOTTOM;
1246 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1247 if (edit->undo_stack[sp] == -2)
1249 /* edit->undo_stack[sp] = '@'; */
1250 edit->stack_pointer = sp;
1252 else
1253 edit->undo_stack[sp]++;
1255 return c;
1258 /* is called whenever a modification is made by one of the four routines below */
1259 static void
1260 edit_modification (WEdit * edit)
1262 edit->caches_valid = 0;
1263 edit->screen_modified = 1;
1265 /* raise lock when file modified */
1266 if (!edit->modified && !edit->delete_file)
1267 edit->locked = edit_lock_file (edit);
1268 edit->modified = 1;
1272 Basic low level single character buffer alterations and movements at the cursor.
1273 Returns char passed over, inserted or removed.
1276 void
1277 edit_insert (WEdit * edit, int c)
1279 /* check if file has grown to large */
1280 if (edit->last_byte >= SIZE_LIMIT)
1281 return;
1283 /* first we must update the position of the display window */
1284 if (edit->curs1 < edit->start_display)
1286 edit->start_display++;
1287 if (c == '\n')
1288 edit->start_line++;
1291 /* Mark file as modified, unless the file hasn't been fully loaded */
1292 if (edit->loading_done)
1294 edit_modification (edit);
1297 /* now we must update some info on the file and check if a redraw is required */
1298 if (c == '\n')
1300 if (edit->book_mark)
1301 book_mark_inc (edit, edit->curs_line);
1302 edit->curs_line++;
1303 edit->total_lines++;
1304 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1307 /* save the reverse command onto the undo stack */
1308 edit_push_action (edit, BACKSPACE);
1310 /* update markers */
1311 edit->mark1 += (edit->mark1 > edit->curs1);
1312 edit->mark2 += (edit->mark2 > edit->curs1);
1313 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1315 /* add a new buffer if we've reached the end of the last one */
1316 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1317 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1319 /* perform the insertion */
1320 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
1321 = (unsigned char) c;
1323 /* update file length */
1324 edit->last_byte++;
1326 /* update cursor position */
1327 edit->curs1++;
1330 static void
1331 edit_insert_over (WEdit * edit)
1333 int i;
1335 for (i = 0; i < edit->over_col; i++)
1337 edit_insert (edit, ' ');
1339 edit->over_col = 0;
1342 /* same as edit_insert and move left */
1343 void
1344 edit_insert_ahead (WEdit * edit, int c)
1346 if (edit->last_byte >= SIZE_LIMIT)
1347 return;
1349 if (edit->curs1 < edit->start_display)
1351 edit->start_display++;
1352 if (c == '\n')
1353 edit->start_line++;
1355 edit_modification (edit);
1356 if (c == '\n')
1358 if (edit->book_mark)
1359 book_mark_inc (edit, edit->curs_line);
1360 edit->total_lines++;
1361 edit->force |= REDRAW_AFTER_CURSOR;
1363 edit_push_action (edit, DELCHAR);
1365 edit->mark1 += (edit->mark1 >= edit->curs1);
1366 edit->mark2 += (edit->mark2 >= edit->curs1);
1367 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1369 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1370 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1371 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
1372 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1374 edit->last_byte++;
1375 edit->curs2++;
1380 edit_delete (WEdit * edit, const int byte_delete)
1382 int p = 0;
1383 int cw = 1;
1384 int i;
1386 if (!edit->curs2)
1387 return 0;
1389 cw = 1;
1390 /* if byte_delete = 1 then delete only one byte not multibyte char */
1391 if (edit->utf8 && byte_delete == 0)
1393 edit_get_utf (edit, edit->curs1, &cw);
1394 if (cw < 1)
1395 cw = 1;
1397 for (i = 1; i <= cw; i++)
1399 if (edit->mark1 > edit->curs1)
1400 edit->mark1--;
1401 if (edit->mark2 > edit->curs1)
1402 edit->mark2--;
1403 if (edit->last_get_rule > edit->curs1)
1404 edit->last_get_rule--;
1406 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1407 ((edit->curs2 -
1408 1) & M_EDIT_BUF_SIZE) - 1];
1410 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1412 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1413 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1415 edit->last_byte--;
1416 edit->curs2--;
1417 edit_push_action (edit, p + 256);
1420 edit_modification (edit);
1421 if (p == '\n')
1423 if (edit->book_mark)
1424 book_mark_dec (edit, edit->curs_line);
1425 edit->total_lines--;
1426 edit->force |= REDRAW_AFTER_CURSOR;
1428 if (edit->curs1 < edit->start_display)
1430 edit->start_display--;
1431 if (p == '\n')
1432 edit->start_line--;
1435 return p;
1439 static int
1440 edit_backspace (WEdit * edit, const int byte_delete)
1442 int p = 0;
1443 int cw = 1;
1444 int i;
1446 if (!edit->curs1)
1447 return 0;
1449 cw = 1;
1451 if (edit->utf8 && byte_delete == 0)
1453 edit_get_prev_utf (edit, edit->curs1, &cw);
1454 if (cw < 1)
1455 cw = 1;
1457 for (i = 1; i <= cw; i++)
1459 if (edit->mark1 >= edit->curs1)
1460 edit->mark1--;
1461 if (edit->mark1 >= edit->curs1)
1462 edit->mark2--;
1463 if (edit->last_get_rule >= edit->curs1)
1464 edit->last_get_rule--;
1466 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
1467 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1468 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1470 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1471 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1473 edit->last_byte--;
1474 edit->curs1--;
1475 edit_push_action (edit, p);
1477 edit_modification (edit);
1478 if (p == '\n')
1480 if (edit->book_mark)
1481 book_mark_dec (edit, edit->curs_line);
1482 edit->curs_line--;
1483 edit->total_lines--;
1484 edit->force |= REDRAW_AFTER_CURSOR;
1487 if (edit->curs1 < edit->start_display)
1489 edit->start_display--;
1490 if (p == '\n')
1491 edit->start_line--;
1494 return p;
1497 #ifdef FAST_MOVE_CURSOR
1499 static void
1500 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1502 unsigned long next;
1503 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
1505 edit->curs_line--;
1506 next -= (unsigned long) dest;
1507 n -= next;
1508 src += next;
1509 dest += next;
1514 edit_move_backward_lots (WEdit * edit, long increment)
1516 int r, s, t;
1517 unsigned char *p = NULL;
1519 if (increment > edit->curs1)
1520 increment = edit->curs1;
1521 if (increment <= 0)
1522 return -1;
1523 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1525 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1526 if (r > increment)
1527 r = increment;
1528 s = edit->curs1 & M_EDIT_BUF_SIZE;
1530 if (s > r)
1532 memqcpy (edit,
1533 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1534 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
1536 else
1538 if (s != 0)
1540 memqcpy (edit,
1541 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1542 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1543 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1544 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1546 memqcpy (edit,
1547 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1548 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1549 EDIT_BUF_SIZE - (r - s), r - s);
1551 increment -= r;
1552 edit->curs1 -= r;
1553 edit->curs2 += r;
1554 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1556 if (p)
1557 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1558 else
1559 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1561 else
1563 g_free (p);
1566 s = edit->curs1 & M_EDIT_BUF_SIZE;
1567 while (increment)
1569 p = 0;
1570 r = EDIT_BUF_SIZE;
1571 if (r > increment)
1572 r = increment;
1573 t = s;
1574 if (r < t)
1575 t = r;
1576 memqcpy (edit,
1577 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1578 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
1579 if (r >= s)
1581 if (t)
1583 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1584 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1586 memqcpy (edit,
1587 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1588 EDIT_BUF_SIZE - r,
1589 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1590 EDIT_BUF_SIZE - (r - s), r - s);
1592 increment -= r;
1593 edit->curs1 -= r;
1594 edit->curs2 += r;
1595 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1597 if (p)
1598 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1599 else
1600 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1602 else
1603 g_free (p);
1605 return edit_get_byte (edit, edit->curs1);
1608 #endif /* ! FAST_MOVE_CURSOR */
1610 /* moves the cursor right or left: increment positive or negative respectively */
1611 void
1612 edit_cursor_move (WEdit * edit, long increment)
1614 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1615 int c;
1616 #ifdef FAST_MOVE_CURSOR
1617 if (increment < -256)
1619 edit->force |= REDRAW_PAGE;
1620 edit_move_backward_lots (edit, -increment);
1621 return;
1623 #endif /* ! FAST_MOVE_CURSOR */
1625 if (increment < 0)
1627 for (; increment < 0; increment++)
1629 if (!edit->curs1)
1630 return;
1632 edit_push_action (edit, CURS_RIGHT);
1634 c = edit_get_byte (edit, edit->curs1 - 1);
1635 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1636 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1637 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1638 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1639 edit->curs2++;
1640 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
1641 1) & M_EDIT_BUF_SIZE];
1642 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1644 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1645 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1647 edit->curs1--;
1648 if (c == '\n')
1650 edit->curs_line--;
1651 edit->force |= REDRAW_LINE_BELOW;
1656 else if (increment > 0)
1658 for (; increment > 0; increment--)
1660 if (!edit->curs2)
1661 return;
1663 edit_push_action (edit, CURS_LEFT);
1665 c = edit_get_byte (edit, edit->curs1);
1666 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1667 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1668 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1669 edit->curs1++;
1670 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1671 ((edit->curs2 -
1672 1) & M_EDIT_BUF_SIZE) - 1];
1673 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1675 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1676 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1678 edit->curs2--;
1679 if (c == '\n')
1681 edit->curs_line++;
1682 edit->force |= REDRAW_LINE_ABOVE;
1688 /* These functions return positions relative to lines */
1690 /* returns index of last char on line + 1 */
1691 long
1692 edit_eol (WEdit * edit, long current)
1694 if (current >= edit->last_byte)
1695 return edit->last_byte;
1697 for (;; current++)
1698 if (edit_get_byte (edit, current) == '\n')
1699 break;
1700 return current;
1703 /* returns index of first char on line */
1704 long
1705 edit_bol (WEdit * edit, long current)
1707 if (current <= 0)
1708 return 0;
1710 for (;; current--)
1711 if (edit_get_byte (edit, current - 1) == '\n')
1712 break;
1713 return current;
1717 long
1718 edit_count_lines (WEdit * edit, long current, long upto)
1720 long lines = 0;
1721 if (upto > edit->last_byte)
1722 upto = edit->last_byte;
1723 if (current < 0)
1724 current = 0;
1725 while (current < upto)
1726 if (edit_get_byte (edit, current++) == '\n')
1727 lines++;
1728 return lines;
1732 /* If lines is zero this returns the count of lines from current to upto. */
1733 /* If upto is zero returns index of lines forward current. */
1734 long
1735 edit_move_forward (WEdit * edit, long current, long lines, long upto)
1737 if (upto)
1739 return edit_count_lines (edit, current, upto);
1741 else
1743 long next;
1744 if (lines < 0)
1745 lines = 0;
1746 while (lines--)
1748 next = edit_eol (edit, current) + 1;
1749 if (next > edit->last_byte)
1750 break;
1751 else
1752 current = next;
1754 return current;
1759 /* Returns offset of 'lines' lines up from current */
1760 long
1761 edit_move_backward (WEdit * edit, long current, long lines)
1763 if (lines < 0)
1764 lines = 0;
1765 current = edit_bol (edit, current);
1766 while ((lines--) && current != 0)
1767 current = edit_bol (edit, current - 1);
1768 return current;
1771 /* If cols is zero this returns the count of columns from current to upto. */
1772 /* If upto is zero returns index of cols across from current. */
1773 long
1774 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1776 long p, q;
1777 int col;
1779 if (upto)
1781 q = upto;
1782 cols = -10;
1784 else
1785 q = edit->last_byte + 2;
1787 for (col = 0, p = current; p < q; p++)
1789 int c, orig_c;
1790 int utf_ch = 0;
1791 #ifdef HAVE_CHARSET
1792 int cw = 1;
1793 #endif
1794 if (cols != -10)
1796 if (col == cols)
1797 return p;
1798 if (col > cols)
1799 return p - 1;
1801 orig_c = c = edit_get_byte (edit, p);
1802 #ifdef HAVE_CHARSET
1803 if (edit->utf8)
1805 utf_ch = edit_get_utf (edit, p, &cw);
1806 if (utf8_display)
1808 if (cw > 1)
1809 col -= cw - 1;
1810 if (g_unichar_iswide (utf_ch))
1811 col++;
1813 else if (cw > 1 && g_unichar_isprint (utf_ch))
1814 col -= cw - 1;
1816 #endif
1817 c = convert_to_display_c (c);
1818 if (c == '\t')
1819 col += TAB_SIZE - col % TAB_SIZE;
1820 else if (c == '\n')
1822 if (upto)
1823 return col;
1824 else
1825 return p;
1827 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1828 /* '\r' is shown as ^M, so we must advance 2 characters */
1829 /* Caret notation for control characters */
1830 col += 2;
1831 else
1832 col++;
1834 return col;
1837 /* returns the current column position of the cursor */
1839 edit_get_col (WEdit * edit)
1841 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1845 /* Scrolling functions */
1847 void
1848 edit_update_curs_row (WEdit * edit)
1850 edit->curs_row = edit->curs_line - edit->start_line;
1853 void
1854 edit_update_curs_col (WEdit * edit)
1856 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1860 edit_get_curs_col (const WEdit * edit)
1862 return edit->curs_col;
1865 /*moves the display start position up by i lines */
1866 void
1867 edit_scroll_upward (WEdit * edit, unsigned long i)
1869 unsigned long lines_above = edit->start_line;
1870 if (i > lines_above)
1871 i = lines_above;
1872 if (i)
1874 edit->start_line -= i;
1875 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1876 edit->force |= REDRAW_PAGE;
1877 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1879 edit_update_curs_row (edit);
1883 /* returns 1 if could scroll, 0 otherwise */
1884 void
1885 edit_scroll_downward (WEdit * edit, int i)
1887 int lines_below;
1888 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1889 if (lines_below > 0)
1891 if (i > lines_below)
1892 i = lines_below;
1893 edit->start_line += i;
1894 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1895 edit->force |= REDRAW_PAGE;
1896 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1898 edit_update_curs_row (edit);
1901 void
1902 edit_scroll_right (WEdit * edit, int i)
1904 edit->force |= REDRAW_PAGE;
1905 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1906 edit->start_col -= i;
1909 void
1910 edit_scroll_left (WEdit * edit, int i)
1912 if (edit->start_col)
1914 edit->start_col += i;
1915 if (edit->start_col > 0)
1916 edit->start_col = 0;
1917 edit->force |= REDRAW_PAGE;
1918 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1922 /* high level cursor movement commands */
1924 static int
1925 is_in_indent (WEdit * edit)
1927 long p = edit_bol (edit, edit->curs1);
1928 while (p < edit->curs1)
1929 if (!strchr (" \t", edit_get_byte (edit, p++)))
1930 return 0;
1931 return 1;
1934 static int left_of_four_spaces (WEdit * edit);
1936 void
1937 edit_move_to_prev_col (WEdit * edit, long p)
1939 int prev = edit->prev_col;
1940 int over = edit->over_col;
1941 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1943 if (option_cursor_beyond_eol)
1945 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
1946 edit_eol (edit, edit->curs1));
1948 if (line_len < prev + edit->over_col)
1950 edit->over_col = prev + over - line_len;
1951 edit->prev_col = line_len;
1952 edit->curs_col = line_len;
1954 else
1956 edit->curs_col = prev + over;
1957 edit->prev_col = edit->curs_col;
1958 edit->over_col = 0;
1961 else
1963 edit->over_col = 0;
1964 if (is_in_indent (edit) && option_fake_half_tabs)
1966 edit_update_curs_col (edit);
1967 if (space_width)
1968 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
1970 int q = edit->curs_col;
1971 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1972 p = edit_bol (edit, edit->curs1);
1973 edit_cursor_move (edit,
1974 edit_move_forward3 (edit, p, edit->curs_col,
1975 0) - edit->curs1);
1976 if (!left_of_four_spaces (edit))
1977 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1983 static int
1984 is_blank (WEdit * edit, long offset)
1986 long s, f;
1987 int c;
1988 s = edit_bol (edit, offset);
1989 f = edit_eol (edit, offset) - 1;
1990 while (s <= f)
1992 c = edit_get_byte (edit, s++);
1993 if (!isspace (c))
1994 return 0;
1996 return 1;
2000 /* returns the offset of line i */
2001 static long
2002 edit_find_line (WEdit * edit, int line)
2004 int i, j = 0;
2005 int m = 2000000000;
2006 if (!edit->caches_valid)
2008 for (i = 0; i < N_LINE_CACHES; i++)
2009 edit->line_numbers[i] = edit->line_offsets[i] = 0;
2010 /* three offsets that we *know* are line 0 at 0 and these two: */
2011 edit->line_numbers[1] = edit->curs_line;
2012 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
2013 edit->line_numbers[2] = edit->total_lines;
2014 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
2015 edit->caches_valid = 1;
2017 if (line >= edit->total_lines)
2018 return edit->line_offsets[2];
2019 if (line <= 0)
2020 return 0;
2021 /* find the closest known point */
2022 for (i = 0; i < N_LINE_CACHES; i++)
2024 int n;
2025 n = abs (edit->line_numbers[i] - line);
2026 if (n < m)
2028 m = n;
2029 j = i;
2032 if (m == 0)
2033 return edit->line_offsets[j]; /* know the offset exactly */
2034 if (m == 1 && j >= 3)
2035 i = j; /* one line different - caller might be looping, so stay in this cache */
2036 else
2037 i = 3 + (rand () % (N_LINE_CACHES - 3));
2038 if (line > edit->line_numbers[j])
2039 edit->line_offsets[i] =
2040 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
2041 else
2042 edit->line_offsets[i] =
2043 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
2044 edit->line_numbers[i] = line;
2045 return edit->line_offsets[i];
2049 line_is_blank (WEdit * edit, long line)
2051 return is_blank (edit, edit_find_line (edit, line));
2054 /* moves up until a blank line is reached, or until just
2055 before a non-blank line is reached */
2056 static void
2057 edit_move_up_paragraph (WEdit * edit, int do_scroll)
2059 int i = 0;
2060 if (edit->curs_line > 1)
2062 if (line_is_blank (edit, edit->curs_line))
2064 if (line_is_blank (edit, edit->curs_line - 1))
2066 for (i = edit->curs_line - 1; i; i--)
2067 if (!line_is_blank (edit, i))
2069 i++;
2070 break;
2073 else
2075 for (i = edit->curs_line - 1; i; i--)
2076 if (line_is_blank (edit, i))
2077 break;
2080 else
2082 for (i = edit->curs_line - 1; i; i--)
2083 if (line_is_blank (edit, i))
2084 break;
2087 edit_move_up (edit, edit->curs_line - i, do_scroll);
2090 /* moves down until a blank line is reached, or until just
2091 before a non-blank line is reached */
2092 static void
2093 edit_move_down_paragraph (WEdit * edit, int do_scroll)
2095 int i;
2096 if (edit->curs_line >= edit->total_lines - 1)
2098 i = edit->total_lines;
2100 else
2102 if (line_is_blank (edit, edit->curs_line))
2104 if (line_is_blank (edit, edit->curs_line + 1))
2106 for (i = edit->curs_line + 1; i; i++)
2107 if (!line_is_blank (edit, i) || i > edit->total_lines)
2109 i--;
2110 break;
2113 else
2115 for (i = edit->curs_line + 1; i; i++)
2116 if (line_is_blank (edit, i) || i >= edit->total_lines)
2117 break;
2120 else
2122 for (i = edit->curs_line + 1; i; i++)
2123 if (line_is_blank (edit, i) || i >= edit->total_lines)
2124 break;
2127 edit_move_down (edit, i - edit->curs_line, do_scroll);
2130 static void
2131 edit_begin_page (WEdit * edit)
2133 edit_update_curs_row (edit);
2134 edit_move_up (edit, edit->curs_row, 0);
2137 static void
2138 edit_end_page (WEdit * edit)
2140 edit_update_curs_row (edit);
2141 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
2145 /* goto beginning of text */
2146 static void
2147 edit_move_to_top (WEdit * edit)
2149 if (edit->curs_line)
2151 edit_cursor_move (edit, -edit->curs1);
2152 edit_move_to_prev_col (edit, 0);
2153 edit->force |= REDRAW_PAGE;
2154 edit->search_start = 0;
2155 edit_update_curs_row (edit);
2160 /* goto end of text */
2161 static void
2162 edit_move_to_bottom (WEdit * edit)
2164 if (edit->curs_line < edit->total_lines)
2166 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
2167 edit->start_display = edit->last_byte;
2168 edit->start_line = edit->total_lines;
2169 edit_scroll_upward (edit, edit->num_widget_lines - 1);
2170 edit->force |= REDRAW_PAGE;
2174 /* goto beginning of line */
2175 static void
2176 edit_cursor_to_bol (WEdit * edit)
2178 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
2179 edit->search_start = edit->curs1;
2180 edit->prev_col = edit_get_col (edit);
2181 edit->over_col = 0;
2184 /* goto end of line */
2185 static void
2186 edit_cursor_to_eol (WEdit * edit)
2188 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
2189 edit->search_start = edit->curs1;
2190 edit->prev_col = edit_get_col (edit);
2191 edit->over_col = 0;
2194 /* move cursor to line 'line' */
2195 void
2196 edit_move_to_line (WEdit * e, long line)
2198 if (line < e->curs_line)
2199 edit_move_up (e, e->curs_line - line, 0);
2200 else
2201 edit_move_down (e, line - e->curs_line, 0);
2202 edit_scroll_screen_over_cursor (e);
2205 /* scroll window so that first visible line is 'line' */
2206 void
2207 edit_move_display (WEdit * e, long line)
2209 if (line < e->start_line)
2210 edit_scroll_upward (e, e->start_line - line);
2211 else
2212 edit_scroll_downward (e, line - e->start_line);
2215 /* save markers onto undo stack */
2216 void
2217 edit_push_markers (WEdit * edit)
2219 edit_push_action (edit, MARK_1 + edit->mark1);
2220 edit_push_action (edit, MARK_2 + edit->mark2);
2223 void
2224 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2226 edit->mark1 = m1;
2227 edit->mark2 = m2;
2228 edit->column1 = c1;
2229 edit->column2 = c2;
2233 /* highlight marker toggle */
2234 void
2235 edit_mark_cmd (WEdit * edit, int unmark)
2237 edit_push_markers (edit);
2238 if (unmark)
2240 edit_set_markers (edit, 0, 0, 0, 0);
2241 edit->force |= REDRAW_PAGE;
2243 else
2245 if (edit->mark2 >= 0)
2247 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
2248 edit->curs_col + edit->over_col);
2249 edit->force |= REDRAW_PAGE;
2251 else
2252 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
2253 edit->curs_col + edit->over_col);
2257 static unsigned long
2258 my_type_of (int c)
2260 int x, r = 0;
2261 const char *p, *q;
2262 const char option_chars_move_whole_word[] =
2263 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2265 if (!c)
2266 return 0;
2267 if (c == '!')
2269 if (*option_chars_move_whole_word == '!')
2270 return 2;
2271 return 0x80000000UL;
2273 if (g_ascii_isupper ((gchar) c))
2274 c = 'A';
2275 else if (g_ascii_islower ((gchar) c))
2276 c = 'a';
2277 else if (g_ascii_isalpha (c))
2278 c = 'a';
2279 else if (isdigit (c))
2280 c = '0';
2281 else if (isspace (c))
2282 c = ' ';
2283 q = strchr (option_chars_move_whole_word, c);
2284 if (!q)
2285 return 0xFFFFFFFFUL;
2288 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2289 if (*p == '!')
2290 x <<= 1;
2291 r |= x;
2293 while ((q = strchr (q + 1, c)));
2294 return r;
2297 static void
2298 edit_left_word_move (WEdit * edit, int s)
2300 for (;;)
2302 int c1, c2;
2303 if (edit->column_highlight
2304 && edit->mark1 != edit->mark2
2305 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2306 break;
2307 edit_cursor_move (edit, -1);
2308 if (!edit->curs1)
2309 break;
2310 c1 = edit_get_byte (edit, edit->curs1 - 1);
2311 c2 = edit_get_byte (edit, edit->curs1);
2312 if (!(my_type_of (c1) & my_type_of (c2)))
2313 break;
2314 if (isspace (c1) && !isspace (c2))
2315 break;
2316 if (s)
2317 if (!isspace (c1) && isspace (c2))
2318 break;
2322 static void
2323 edit_left_word_move_cmd (WEdit * edit)
2325 edit_left_word_move (edit, 0);
2326 edit->force |= REDRAW_PAGE;
2329 static void
2330 edit_right_word_move (WEdit * edit, int s)
2332 for (;;)
2334 int c1, c2;
2335 if (edit->column_highlight
2336 && edit->mark1 != edit->mark2
2337 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
2338 break;
2339 edit_cursor_move (edit, 1);
2340 if (edit->curs1 >= edit->last_byte)
2341 break;
2342 c1 = edit_get_byte (edit, edit->curs1 - 1);
2343 c2 = edit_get_byte (edit, edit->curs1);
2344 if (!(my_type_of (c1) & my_type_of (c2)))
2345 break;
2346 if (isspace (c1) && !isspace (c2))
2347 break;
2348 if (s)
2349 if (!isspace (c1) && isspace (c2))
2350 break;
2354 static void
2355 edit_right_word_move_cmd (WEdit * edit)
2357 edit_right_word_move (edit, 0);
2358 edit->force |= REDRAW_PAGE;
2361 static void
2362 edit_right_char_move_cmd (WEdit * edit)
2364 int cw = 1;
2365 int c = 0;
2366 if (edit->utf8)
2368 c = edit_get_utf (edit, edit->curs1, &cw);
2369 if (cw < 1)
2370 cw = 1;
2372 else
2374 c = edit_get_byte (edit, edit->curs1);
2376 if (option_cursor_beyond_eol && c == '\n')
2378 edit->over_col++;
2380 else
2382 edit_cursor_move (edit, cw);
2386 static void
2387 edit_left_char_move_cmd (WEdit * edit)
2389 int cw = 1;
2390 if (edit->column_highlight
2391 && option_cursor_beyond_eol
2392 && edit->mark1 != edit->mark2
2393 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2394 return;
2395 if (edit->utf8)
2397 edit_get_prev_utf (edit, edit->curs1, &cw);
2398 if (cw < 1)
2399 cw = 1;
2401 if (option_cursor_beyond_eol && edit->over_col > 0)
2403 edit->over_col--;
2405 else
2407 edit_cursor_move (edit, -cw);
2411 /** Up or down cursor moving.
2412 direction = TRUE - move up
2413 = FALSE - move down
2415 static void
2416 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2418 unsigned long p;
2419 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
2421 if (i > l)
2422 i = l;
2424 if (i == 0)
2425 return;
2427 if (i > 1)
2428 edit->force |= REDRAW_PAGE;
2429 if (do_scroll)
2431 if (direction)
2432 edit_scroll_upward (edit, i);
2433 else
2434 edit_scroll_downward (edit, i);
2436 p = edit_bol (edit, edit->curs1);
2438 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
2440 edit_cursor_move (edit, p - edit->curs1);
2442 edit_move_to_prev_col (edit, p);
2444 /* search start of current multibyte char (like CJK) */
2445 if (edit->curs1 + 1 < edit->last_byte)
2447 edit_right_char_move_cmd (edit);
2448 edit_left_char_move_cmd (edit);
2451 edit->search_start = edit->curs1;
2452 edit->found_len = 0;
2455 static void
2456 edit_right_delete_word (WEdit * edit)
2458 int c1, c2;
2459 for (;;)
2461 if (edit->curs1 >= edit->last_byte)
2462 break;
2463 c1 = edit_delete (edit, 1);
2464 c2 = edit_get_byte (edit, edit->curs1);
2465 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2466 break;
2467 if (!(my_type_of (c1) & my_type_of (c2)))
2468 break;
2472 static void
2473 edit_left_delete_word (WEdit * edit)
2475 int c1, c2;
2476 for (;;)
2478 if (edit->curs1 <= 0)
2479 break;
2480 c1 = edit_backspace (edit, 1);
2481 c2 = edit_get_byte (edit, edit->curs1 - 1);
2482 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2483 break;
2484 if (!(my_type_of (c1) & my_type_of (c2)))
2485 break;
2490 the start column position is not recorded, and hence does not
2491 undo as it happed. But who would notice.
2493 static void
2494 edit_do_undo (WEdit * edit)
2496 long ac;
2497 long count = 0;
2499 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2500 edit->over_col = 0;
2501 while ((ac = pop_action (edit)) < KEY_PRESS)
2503 switch ((int) ac)
2505 case STACK_BOTTOM:
2506 goto done_undo;
2507 case CURS_RIGHT:
2508 edit_cursor_move (edit, 1);
2509 break;
2510 case CURS_LEFT:
2511 edit_cursor_move (edit, -1);
2512 break;
2513 case BACKSPACE:
2514 edit_backspace (edit, 1);
2515 break;
2516 case DELCHAR:
2517 edit_delete (edit, 1);
2518 break;
2519 case COLUMN_ON:
2520 edit->column_highlight = 1;
2521 break;
2522 case COLUMN_OFF:
2523 edit->column_highlight = 0;
2524 break;
2526 if (ac >= 256 && ac < 512)
2527 edit_insert_ahead (edit, ac - 256);
2528 if (ac >= 0 && ac < 256)
2529 edit_insert (edit, ac);
2531 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
2533 edit->mark1 = ac - MARK_1;
2534 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2536 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
2538 edit->mark2 = ac - MARK_2;
2539 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2541 if (count++)
2542 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2545 if (edit->start_display > ac - KEY_PRESS)
2547 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2548 edit->force |= REDRAW_PAGE;
2550 else if (edit->start_display < ac - KEY_PRESS)
2552 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2553 edit->force |= REDRAW_PAGE;
2555 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2556 edit_update_curs_row (edit);
2558 done_undo:;
2559 edit->stack_disable = 0;
2562 static void
2563 edit_delete_to_line_end (WEdit * edit)
2565 while (edit_get_byte (edit, edit->curs1) != '\n')
2567 if (!edit->curs2)
2568 break;
2569 edit_delete (edit, 1);
2573 static void
2574 edit_delete_to_line_begin (WEdit * edit)
2576 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2578 if (!edit->curs1)
2579 break;
2580 edit_backspace (edit, 1);
2584 void
2585 edit_delete_line (WEdit * edit)
2588 * Delete right part of the line.
2589 * Note that edit_get_byte() returns '\n' when byte position is
2590 * beyond EOF.
2592 while (edit_get_byte (edit, edit->curs1) != '\n')
2594 (void) edit_delete (edit, 1);
2598 * Delete '\n' char.
2599 * Note that edit_delete() will not corrupt anything if called while
2600 * cursor position is EOF.
2602 (void) edit_delete (edit, 1);
2605 * Delete left part of the line.
2606 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2608 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2610 (void) edit_backspace (edit, 1);
2614 void
2615 insert_spaces_tab (WEdit * edit, int half)
2617 int i;
2618 edit_update_curs_col (edit);
2619 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
2620 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2621 while (i > 0)
2623 edit_insert (edit, ' ');
2624 i -= space_width;
2628 static int
2629 is_aligned_on_a_tab (WEdit * edit)
2631 edit_update_curs_col (edit);
2632 return !((edit->curs_col % (TAB_SIZE * space_width))
2633 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2636 static int
2637 right_of_four_spaces (WEdit * edit)
2639 int i, ch = 0;
2640 for (i = 1; i <= HALF_TAB_SIZE; i++)
2641 ch |= edit_get_byte (edit, edit->curs1 - i);
2642 if (ch == ' ')
2643 return is_aligned_on_a_tab (edit);
2644 return 0;
2647 static int
2648 left_of_four_spaces (WEdit * edit)
2650 int i, ch = 0;
2651 for (i = 0; i < HALF_TAB_SIZE; i++)
2652 ch |= edit_get_byte (edit, edit->curs1 + i);
2653 if (ch == ' ')
2654 return is_aligned_on_a_tab (edit);
2655 return 0;
2659 edit_indent_width (WEdit * edit, long p)
2661 long q = p;
2662 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2663 q++;
2664 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2667 void
2668 edit_insert_indent (WEdit * edit, int indent)
2670 if (!option_fill_tabs_with_spaces)
2672 while (indent >= TAB_SIZE)
2674 edit_insert (edit, '\t');
2675 indent -= TAB_SIZE;
2678 while (indent-- > 0)
2679 edit_insert (edit, ' ');
2682 static void
2683 edit_auto_indent (WEdit * edit)
2685 long p;
2686 char c;
2687 p = edit->curs1;
2688 /* use the previous line as a template */
2689 p = edit_move_backward (edit, p, 1);
2690 /* copy the leading whitespace of the line */
2691 for (;;)
2692 { /* no range check - the line _is_ \n-terminated */
2693 c = edit_get_byte (edit, p++);
2694 if (c != ' ' && c != '\t')
2695 break;
2696 edit_insert (edit, c);
2700 static inline void
2701 edit_double_newline (WEdit * edit)
2703 edit_insert (edit, '\n');
2704 if (edit_get_byte (edit, edit->curs1) == '\n')
2705 return;
2706 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2707 return;
2708 edit->force |= REDRAW_PAGE;
2709 edit_insert (edit, '\n');
2712 static inline void
2713 edit_tab_cmd (WEdit * edit)
2715 int i;
2717 if (option_fake_half_tabs)
2719 if (is_in_indent (edit))
2721 /*insert a half tab (usually four spaces) unless there is a
2722 half tab already behind, then delete it and insert a
2723 full tab. */
2724 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
2726 for (i = 1; i <= HALF_TAB_SIZE; i++)
2727 edit_backspace (edit, 1);
2728 edit_insert (edit, '\t');
2730 else
2732 insert_spaces_tab (edit, 1);
2734 return;
2737 if (option_fill_tabs_with_spaces)
2739 insert_spaces_tab (edit, 0);
2741 else
2743 edit_insert (edit, '\t');
2747 static void
2748 check_and_wrap_line (WEdit * edit)
2750 int curs, c;
2751 if (!option_typewriter_wrap)
2752 return;
2753 edit_update_curs_col (edit);
2754 if (edit->curs_col < option_word_wrap_line_length)
2755 return;
2756 curs = edit->curs1;
2757 for (;;)
2759 curs--;
2760 c = edit_get_byte (edit, curs);
2761 if (c == '\n' || curs <= 0)
2763 edit_insert (edit, '\n');
2764 return;
2766 if (c == ' ' || c == '\t')
2768 int current = edit->curs1;
2769 edit_cursor_move (edit, curs - edit->curs1 + 1);
2770 edit_insert (edit, '\n');
2771 edit_cursor_move (edit, current - edit->curs1 + 1);
2772 return;
2777 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
2779 void
2780 edit_push_key_press (WEdit * edit)
2782 edit_push_action (edit, KEY_PRESS + edit->start_display);
2783 if (edit->mark2 == -1)
2784 edit_push_action (edit, MARK_1 + edit->mark1);
2787 /* this find the matching bracket in either direction, and sets edit->bracket */
2788 static long
2789 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2791 const char *const b = "{}{[][()(", *p;
2792 int i = 1, a, inc = -1, c, d, n = 0;
2793 unsigned long j = 0;
2794 long q;
2795 edit_update_curs_row (edit);
2796 c = edit_get_byte (edit, edit->curs1);
2797 p = strchr (b, c);
2798 /* no limit */
2799 if (!furthest_bracket_search)
2800 furthest_bracket_search--;
2801 /* not on a bracket at all */
2802 if (!p)
2803 return -1;
2804 /* the matching bracket */
2805 d = p[1];
2806 /* going left or right? */
2807 if (strchr ("{[(", c))
2808 inc = 1;
2809 for (q = edit->curs1 + inc;; q += inc)
2811 /* out of buffer? */
2812 if (q >= edit->last_byte || q < 0)
2813 break;
2814 a = edit_get_byte (edit, q);
2815 /* don't want to eat CPU */
2816 if (j++ > furthest_bracket_search)
2817 break;
2818 /* out of screen? */
2819 if (in_screen)
2821 if (q < edit->start_display)
2822 break;
2823 /* count lines if searching downward */
2824 if (inc > 0 && a == '\n')
2825 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2826 break;
2828 /* count bracket depth */
2829 i += (a == c) - (a == d);
2830 /* return if bracket depth is zero */
2831 if (!i)
2832 return q;
2834 /* no match */
2835 return -1;
2838 static long last_bracket = -1;
2840 void
2841 edit_find_bracket (WEdit * edit)
2843 edit->bracket = edit_get_bracket (edit, 1, 10000);
2844 if (last_bracket != edit->bracket)
2845 edit->force |= REDRAW_PAGE;
2846 last_bracket = edit->bracket;
2849 static inline void
2850 edit_goto_matching_bracket (WEdit * edit)
2852 long q;
2854 q = edit_get_bracket (edit, 0, 0);
2855 if (q >= 0)
2857 edit->bracket = edit->curs1;
2858 edit->force |= REDRAW_PAGE;
2859 edit_cursor_move (edit, q - edit->curs1);
2864 * This executes a command as though the user initiated it through a key
2865 * press. Callback with WIDGET_KEY as a message calls this after
2866 * translating the key press. This function can be used to pass any
2867 * command to the editor. Note that the screen wouldn't update
2868 * automatically. Either of command or char_for_insertion must be
2869 * passed as -1. Commands are executed, and char_for_insertion is
2870 * inserted at the cursor.
2872 void
2873 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
2875 if (command == CK_Begin_Record_Macro)
2877 edit->macro_i = 0;
2878 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2879 return;
2881 if (command == CK_End_Record_Macro && edit->macro_i != -1)
2883 edit->force |= REDRAW_COMPLETELY;
2884 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2885 edit->macro_i = -1;
2886 return;
2888 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
2890 edit->macro[edit->macro_i].command = command;
2891 edit->macro[edit->macro_i++].ch = char_for_insertion;
2893 /* record the beginning of a set of editing actions initiated by a key press */
2894 if (command != CK_Undo && command != CK_Ext_Mode)
2895 edit_push_key_press (edit);
2897 edit_execute_cmd (edit, command, char_for_insertion);
2898 if (edit->column_highlight)
2899 edit->force |= REDRAW_PAGE;
2902 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
2905 This executes a command at a lower level than macro recording.
2906 It also does not push a key_press onto the undo stack. This means
2907 that if it is called many times, a single undo command will undo
2908 all of them. It also does not check for the Undo command.
2910 void
2911 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
2913 edit->force |= REDRAW_LINE;
2915 /* The next key press will unhighlight the found string, so update
2916 * the whole page */
2917 if (edit->found_len || edit->column_highlight)
2918 edit->force |= REDRAW_PAGE;
2920 if (command / 100 == 6)
2921 { /* a highlight command like shift-arrow */
2922 edit->column_highlight = 0;
2923 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
2925 edit_mark_cmd (edit, 1); /* clear */
2926 edit_mark_cmd (edit, 0); /* marking on */
2928 edit->highlight = 1;
2930 else
2931 { /* any other command */
2932 if (edit->highlight)
2933 edit_mark_cmd (edit, 0); /* clear */
2934 edit->highlight = 0;
2937 /* first check for undo */
2938 if (command == CK_Undo)
2940 edit_do_undo (edit);
2941 edit->found_len = 0;
2942 edit->prev_col = edit_get_col (edit);
2943 edit->search_start = edit->curs1;
2944 return;
2947 /* An ordinary key press */
2948 if (char_for_insertion >= 0)
2950 /* if non persistent selection and text selected */
2951 if (!option_persistent_selections)
2953 if (edit->mark1 != edit->mark2)
2954 edit_block_delete_cmd (edit);
2956 if (edit->overwrite)
2958 /* remove char only one time, after input first byte, multibyte chars */
2959 if ((!utf8_display || edit->charpoint == 0)
2960 && edit_get_byte (edit, edit->curs1) != '\n')
2961 edit_delete (edit, 0);
2963 if (option_cursor_beyond_eol && edit->over_col > 0)
2964 edit_insert_over (edit);
2965 #ifdef HAVE_CHARSET
2966 if (char_for_insertion > 255 && utf8_display == 0)
2968 unsigned char str[6 + 1];
2969 size_t i = 0;
2970 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
2971 if (res == 0)
2973 str[0] = '.';
2974 str[1] = '\0';
2976 else
2978 str[res] = '\0';
2980 while (str[i] != 0 && i <= 6)
2982 char_for_insertion = str[i];
2983 edit_insert (edit, char_for_insertion);
2984 i++;
2987 else
2988 #endif
2989 edit_insert (edit, char_for_insertion);
2991 if (option_auto_para_formatting)
2993 format_paragraph (edit, 0);
2994 edit->force |= REDRAW_PAGE;
2996 else
2997 check_and_wrap_line (edit);
2998 edit->found_len = 0;
2999 edit->prev_col = edit_get_col (edit);
3000 edit->search_start = edit->curs1;
3001 edit_find_bracket (edit);
3002 return;
3005 switch (command)
3007 case CK_Begin_Page:
3008 case CK_End_Page:
3009 case CK_Begin_Page_Highlight:
3010 case CK_End_Page_Highlight:
3011 case CK_Word_Left:
3012 case CK_Word_Right:
3013 case CK_Up:
3014 case CK_Down:
3015 case CK_Left:
3016 case CK_Right:
3017 if (edit->mark2 >= 0)
3019 if (!option_persistent_selections)
3021 if (edit->column_highlight)
3022 edit_push_action (edit, COLUMN_ON);
3023 edit->column_highlight = 0;
3024 edit_mark_cmd (edit, 1);
3029 switch (command)
3031 case CK_Begin_Page:
3032 case CK_End_Page:
3033 case CK_Begin_Page_Highlight:
3034 case CK_End_Page_Highlight:
3035 case CK_Word_Left:
3036 case CK_Word_Right:
3037 case CK_Up:
3038 case CK_Down:
3039 case CK_Word_Left_Highlight:
3040 case CK_Word_Right_Highlight:
3041 case CK_Up_Highlight:
3042 case CK_Down_Highlight:
3043 case CK_Up_Alt_Highlight:
3044 case CK_Down_Alt_Highlight:
3045 if (edit->mark2 == -1)
3046 break; /*marking is following the cursor: may need to highlight a whole line */
3047 case CK_Left:
3048 case CK_Right:
3049 case CK_Left_Highlight:
3050 case CK_Right_Highlight:
3051 edit->force |= REDRAW_CHAR_ONLY;
3054 /* basic cursor key commands */
3055 switch (command)
3057 case CK_BackSpace:
3058 /* if non persistent selection and text selected */
3059 if (!option_persistent_selections)
3061 if (edit->mark1 != edit->mark2)
3063 edit_block_delete_cmd (edit);
3064 break;
3067 if (option_cursor_beyond_eol && edit->over_col > 0)
3069 edit->over_col--;
3070 break;
3072 if (option_backspace_through_tabs && is_in_indent (edit))
3074 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3075 edit_backspace (edit, 1);
3076 break;
3078 else
3080 if (option_fake_half_tabs)
3082 int i;
3083 if (is_in_indent (edit) && right_of_four_spaces (edit))
3085 for (i = 0; i < HALF_TAB_SIZE; i++)
3086 edit_backspace (edit, 1);
3087 break;
3091 edit_backspace (edit, 0);
3092 break;
3093 case CK_Delete:
3094 /* if non persistent selection and text selected */
3095 if (!option_persistent_selections)
3097 if (edit->mark1 != edit->mark2)
3099 edit_block_delete_cmd (edit);
3100 break;
3104 if (option_cursor_beyond_eol && edit->over_col > 0)
3105 edit_insert_over (edit);
3107 if (option_fake_half_tabs)
3109 int i;
3110 if (is_in_indent (edit) && left_of_four_spaces (edit))
3112 for (i = 1; i <= HALF_TAB_SIZE; i++)
3113 edit_delete (edit, 1);
3114 break;
3117 edit_delete (edit, 0);
3118 break;
3119 case CK_Delete_Word_Left:
3120 edit->over_col = 0;
3121 edit_left_delete_word (edit);
3122 break;
3123 case CK_Delete_Word_Right:
3124 if (option_cursor_beyond_eol && edit->over_col > 0)
3125 edit_insert_over (edit);
3127 edit_right_delete_word (edit);
3128 break;
3129 case CK_Delete_Line:
3130 edit_delete_line (edit);
3131 break;
3132 case CK_Delete_To_Line_End:
3133 edit_delete_to_line_end (edit);
3134 break;
3135 case CK_Delete_To_Line_Begin:
3136 edit_delete_to_line_begin (edit);
3137 break;
3138 case CK_Enter:
3139 edit->over_col = 0;
3140 if (option_auto_para_formatting)
3142 edit_double_newline (edit);
3143 if (option_return_does_auto_indent)
3144 edit_auto_indent (edit);
3145 format_paragraph (edit, 0);
3147 else
3149 edit_insert (edit, '\n');
3150 if (option_return_does_auto_indent)
3152 edit_auto_indent (edit);
3155 break;
3156 case CK_Return:
3157 edit_insert (edit, '\n');
3158 break;
3160 case CK_Page_Up_Alt_Highlight:
3161 edit->column_highlight = 1;
3162 case CK_Page_Up:
3163 case CK_Page_Up_Highlight:
3164 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3165 break;
3166 case CK_Page_Down_Alt_Highlight:
3167 edit->column_highlight = 1;
3168 case CK_Page_Down:
3169 case CK_Page_Down_Highlight:
3170 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3171 break;
3172 case CK_Left_Alt_Highlight:
3173 edit->column_highlight = 1;
3174 case CK_Left:
3175 case CK_Left_Highlight:
3176 if (option_fake_half_tabs)
3178 if (is_in_indent (edit) && right_of_four_spaces (edit))
3180 if (option_cursor_beyond_eol && edit->over_col > 0)
3181 edit->over_col--;
3182 else
3183 edit_cursor_move (edit, -HALF_TAB_SIZE);
3184 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3185 break;
3188 edit_left_char_move_cmd (edit);
3189 break;
3190 case CK_Right_Alt_Highlight:
3191 edit->column_highlight = 1;
3192 case CK_Right:
3193 case CK_Right_Highlight:
3194 if (option_fake_half_tabs)
3196 if (is_in_indent (edit) && left_of_four_spaces (edit))
3198 edit_cursor_move (edit, HALF_TAB_SIZE);
3199 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3200 break;
3203 edit_right_char_move_cmd (edit);
3204 break;
3205 case CK_Begin_Page:
3206 case CK_Begin_Page_Highlight:
3207 edit_begin_page (edit);
3208 break;
3209 case CK_End_Page:
3210 case CK_End_Page_Highlight:
3211 edit_end_page (edit);
3212 break;
3213 case CK_Word_Left:
3214 case CK_Word_Left_Highlight:
3215 edit->over_col = 0;
3216 edit_left_word_move_cmd (edit);
3217 break;
3218 case CK_Word_Right:
3219 case CK_Word_Right_Highlight:
3220 edit->over_col = 0;
3221 edit_right_word_move_cmd (edit);
3222 break;
3223 case CK_Up_Alt_Highlight:
3224 edit->column_highlight = 1;
3225 case CK_Up:
3226 case CK_Up_Highlight:
3227 edit_move_up (edit, 1, 0);
3228 break;
3229 case CK_Down_Alt_Highlight:
3230 edit->column_highlight = 1;
3231 case CK_Down:
3232 case CK_Down_Highlight:
3233 edit_move_down (edit, 1, 0);
3234 break;
3235 case CK_Paragraph_Up_Alt_Highlight:
3236 edit->column_highlight = 1;
3237 case CK_Paragraph_Up:
3238 case CK_Paragraph_Up_Highlight:
3239 edit_move_up_paragraph (edit, 0);
3240 break;
3241 case CK_Paragraph_Down_Alt_Highlight:
3242 edit->column_highlight = 1;
3243 case CK_Paragraph_Down:
3244 case CK_Paragraph_Down_Highlight:
3245 edit_move_down_paragraph (edit, 0);
3246 break;
3247 case CK_Scroll_Up_Alt_Highlight:
3248 edit->column_highlight = 1;
3249 case CK_Scroll_Up:
3250 case CK_Scroll_Up_Highlight:
3251 edit_move_up (edit, 1, 1);
3252 break;
3253 case CK_Scroll_Down_Alt_Highlight:
3254 edit->column_highlight = 1;
3255 case CK_Scroll_Down:
3256 case CK_Scroll_Down_Highlight:
3257 edit_move_down (edit, 1, 1);
3258 break;
3259 case CK_Home:
3260 case CK_Home_Highlight:
3261 edit_cursor_to_bol (edit);
3262 break;
3263 case CK_End:
3264 case CK_End_Highlight:
3265 edit_cursor_to_eol (edit);
3266 break;
3267 case CK_Tab:
3268 /* if text marked shift block */
3269 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3271 if (edit->mark2 < 0)
3272 edit_mark_cmd (edit, 0);
3273 edit_move_block_to_right (edit);
3275 else
3277 if (option_cursor_beyond_eol)
3278 edit_insert_over (edit);
3279 edit_tab_cmd (edit);
3280 if (option_auto_para_formatting)
3282 format_paragraph (edit, 0);
3283 edit->force |= REDRAW_PAGE;
3285 else
3287 check_and_wrap_line (edit);
3290 break;
3292 case CK_Toggle_Insert:
3293 edit->overwrite = (edit->overwrite == 0);
3294 break;
3296 case CK_Mark:
3297 if (edit->mark2 >= 0)
3299 if (edit->column_highlight)
3300 edit_push_action (edit, COLUMN_ON);
3301 edit->column_highlight = 0;
3303 edit_mark_cmd (edit, 0);
3304 break;
3305 case CK_Column_Mark:
3306 if (!edit->column_highlight)
3307 edit_push_action (edit, COLUMN_OFF);
3308 edit->column_highlight = 1;
3309 edit_mark_cmd (edit, 0);
3310 break;
3311 case CK_Mark_All:
3312 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3313 edit->force |= REDRAW_PAGE;
3314 break;
3315 case CK_Unmark:
3316 if (edit->column_highlight)
3317 edit_push_action (edit, COLUMN_ON);
3318 edit->column_highlight = 0;
3319 edit_mark_cmd (edit, 1);
3320 break;
3322 case CK_Toggle_Line_State:
3323 option_line_state = !option_line_state;
3324 if (option_line_state)
3326 option_line_state_width = LINE_STATE_WIDTH;
3328 else
3330 option_line_state_width = 0;
3332 edit->force |= REDRAW_PAGE;
3333 break;
3335 case CK_Toggle_Show_Margin:
3336 show_right_margin = !show_right_margin;
3337 edit->force |= REDRAW_PAGE;
3338 break;
3340 case CK_Toggle_Bookmark:
3341 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3342 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3343 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3344 else
3345 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3346 break;
3347 case CK_Flush_Bookmarks:
3348 book_mark_flush (edit, BOOK_MARK_COLOR);
3349 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3350 edit->force |= REDRAW_PAGE;
3351 break;
3352 case CK_Next_Bookmark:
3353 if (edit->book_mark)
3355 struct _book_mark *p;
3356 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3357 if (p->next)
3359 p = p->next;
3360 if (p->line >= edit->start_line + edit->num_widget_lines
3361 || p->line < edit->start_line)
3362 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3363 edit_move_to_line (edit, p->line);
3366 break;
3367 case CK_Prev_Bookmark:
3368 if (edit->book_mark)
3370 struct _book_mark *p;
3371 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3372 while (p->line == edit->curs_line)
3373 if (p->prev)
3374 p = p->prev;
3375 if (p->line >= 0)
3377 if (p->line >= edit->start_line + edit->num_widget_lines
3378 || p->line < edit->start_line)
3379 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3380 edit_move_to_line (edit, p->line);
3383 break;
3385 case CK_Beginning_Of_Text:
3386 case CK_Beginning_Of_Text_Highlight:
3387 edit_move_to_top (edit);
3388 break;
3389 case CK_End_Of_Text:
3390 case CK_End_Of_Text_Highlight:
3391 edit_move_to_bottom (edit);
3392 break;
3394 case CK_Copy:
3395 if (option_cursor_beyond_eol && edit->over_col > 0)
3396 edit_insert_over (edit);
3397 edit_block_copy_cmd (edit);
3398 break;
3399 case CK_Remove:
3400 edit_block_delete_cmd (edit);
3401 break;
3402 case CK_Move:
3403 if (option_cursor_beyond_eol && edit->over_col > 0)
3404 edit_insert_over (edit);
3405 edit_block_move_cmd (edit);
3406 break;
3408 case CK_Shift_Block_Left:
3409 if (edit->mark1 != edit->mark2)
3410 edit_move_block_to_left (edit);
3411 break;
3412 case CK_Shift_Block_Right:
3413 if (edit->mark1 != edit->mark2)
3414 edit_move_block_to_right (edit);
3415 break;
3416 case CK_XStore:
3417 edit_copy_to_X_buf_cmd (edit);
3418 break;
3419 case CK_XCut:
3420 edit_cut_to_X_buf_cmd (edit);
3421 break;
3422 case CK_XPaste:
3423 /* if non persistent selection and text selected */
3424 if (!option_persistent_selections)
3426 if (edit->mark1 != edit->mark2)
3427 edit_block_delete_cmd (edit);
3429 if (option_cursor_beyond_eol && edit->over_col > 0)
3430 edit_insert_over (edit);
3431 edit_paste_from_X_buf_cmd (edit);
3432 break;
3433 case CK_Selection_History:
3434 edit_paste_from_history (edit);
3435 break;
3437 case CK_Save_As:
3438 edit_save_as_cmd (edit);
3439 break;
3440 case CK_Save:
3441 edit_save_confirm_cmd (edit);
3442 break;
3443 case CK_Load:
3444 edit_load_cmd (edit, EDIT_FILE_COMMON);
3445 break;
3446 case CK_Save_Block:
3447 edit_save_block_cmd (edit);
3448 break;
3449 case CK_Insert_File:
3450 edit_insert_file_cmd (edit);
3451 break;
3453 case CK_Load_Prev_File:
3454 edit_load_back_cmd (edit);
3455 break;
3456 case CK_Load_Next_File:
3457 edit_load_forward_cmd (edit);
3458 break;
3460 case CK_Load_Syntax_File:
3461 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3462 break;
3463 case CK_Choose_Syntax:
3464 edit_syntax_dialog (edit, edit->syntax_type);
3465 break;
3467 case CK_Load_Menu_File:
3468 edit_load_cmd (edit, EDIT_FILE_MENU);
3469 break;
3471 case CK_Toggle_Syntax:
3472 if ((option_syntax_highlighting ^= 1) == 1)
3473 edit_load_syntax (edit, NULL, edit->syntax_type);
3474 edit->force |= REDRAW_PAGE;
3475 break;
3477 case CK_Toggle_Tab_TWS:
3478 enable_show_tabs_tws ^= 1;
3479 edit->force |= REDRAW_PAGE;
3480 break;
3482 case CK_Find:
3483 edit_search_cmd (edit, 0);
3484 break;
3485 case CK_Find_Again:
3486 edit_search_cmd (edit, 1);
3487 break;
3488 case CK_Replace:
3489 edit_replace_cmd (edit, 0);
3490 break;
3491 case CK_Replace_Again:
3492 edit_replace_cmd (edit, 1);
3493 break;
3494 case CK_Complete_Word:
3495 /* if text marked shift block */
3496 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3498 edit_move_block_to_left (edit);
3500 else
3502 edit_complete_word_cmd (edit);
3504 break;
3505 case CK_Find_Definition:
3506 edit_get_match_keyword_cmd (edit);
3507 break;
3508 case CK_Quit:
3509 dlg_stop (edit->widget.owner);
3510 break;
3511 case CK_New:
3512 edit_new_cmd (edit);
3513 break;
3514 case CK_Help:
3515 edit_help_cmd (edit);
3516 break;
3517 case CK_Refresh:
3518 edit_refresh_cmd (edit);
3519 break;
3520 case CK_SaveSetupCmd:
3521 save_setup_cmd ();
3522 break;
3523 case CK_About:
3524 query_dialog (_("About"),
3525 _("\n Cooledit v3.11.5\n\n"
3526 " Copyright (C) 1996 the Free Software Foundation\n\n"
3527 " A user friendly text editor written\n"
3528 " for the Midnight Commander.\n"), D_NORMAL, 1, _("&OK"));
3529 break;
3530 case CK_LearnKeys:
3531 learn_keys ();
3532 break;
3533 case CK_Edit_Options:
3534 edit_options_dialog (edit);
3535 break;
3536 case CK_Edit_Save_Mode:
3537 menu_save_mode_cmd ();
3538 break;
3539 case CK_Date:
3541 char s[BUF_MEDIUM];
3542 /* fool gcc to prevent a Y2K warning */
3543 char time_format[] = "_c";
3544 time_format[0] = '%';
3546 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3547 edit_print_string (edit, s);
3548 edit->force |= REDRAW_PAGE;
3549 break;
3551 break;
3552 case CK_Goto:
3553 edit_goto_cmd (edit);
3554 break;
3555 case CK_Paragraph_Format:
3556 format_paragraph (edit, 1);
3557 edit->force |= REDRAW_PAGE;
3558 break;
3559 case CK_Delete_Macro:
3560 edit_delete_macro_cmd (edit);
3561 break;
3562 case CK_Match_Bracket:
3563 edit_goto_matching_bracket (edit);
3564 break;
3565 case CK_User_Menu:
3566 user_menu (edit);
3567 break;
3568 case CK_Sort:
3569 edit_sort_cmd (edit);
3570 break;
3571 case CK_ExtCmd:
3572 edit_ext_cmd (edit);
3573 break;
3574 case CK_Mail:
3575 edit_mail_dialog (edit);
3576 break;
3577 case CK_Shell:
3578 view_other_cmd ();
3579 break;
3580 case CK_SelectCodepage:
3581 edit_select_codepage_cmd (edit);
3582 break;
3583 case CK_Insert_Literal:
3584 edit_insert_literal_cmd (edit);
3585 break;
3586 case CK_Execute_Macro:
3587 edit_execute_macro_cmd (edit);
3588 break;
3589 case CK_Begin_End_Macro:
3590 edit_begin_end_macro_cmd (edit);
3591 break;
3592 case CK_Ext_Mode:
3593 edit->extmod = 1;
3594 break;
3595 default:
3596 break;
3599 /* CK_Pipe_Block */
3600 if ((command / 1000) == 1) /* a shell command */
3601 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3602 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3603 { /* a macro command */
3604 struct macro m[MAX_MACRO_LENGTH];
3605 int nm;
3606 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3607 edit_execute_macro (edit, m, nm);
3610 /* keys which must set the col position, and the search vars */
3611 switch (command)
3613 case CK_Find:
3614 case CK_Find_Again:
3615 case CK_Replace:
3616 case CK_Replace_Again:
3617 case CK_Complete_Word:
3618 edit->prev_col = edit_get_col (edit);
3619 break;
3620 case CK_Up:
3621 case CK_Up_Highlight:
3622 case CK_Up_Alt_Highlight:
3623 case CK_Down:
3624 case CK_Down_Highlight:
3625 case CK_Down_Alt_Highlight:
3626 case CK_Page_Up:
3627 case CK_Page_Up_Highlight:
3628 case CK_Page_Up_Alt_Highlight:
3629 case CK_Page_Down:
3630 case CK_Page_Down_Highlight:
3631 case CK_Page_Down_Alt_Highlight:
3632 case CK_Beginning_Of_Text:
3633 case CK_Beginning_Of_Text_Highlight:
3634 case CK_End_Of_Text:
3635 case CK_End_Of_Text_Highlight:
3636 case CK_Paragraph_Up:
3637 case CK_Paragraph_Up_Highlight:
3638 case CK_Paragraph_Up_Alt_Highlight:
3639 case CK_Paragraph_Down:
3640 case CK_Paragraph_Down_Highlight:
3641 case CK_Paragraph_Down_Alt_Highlight:
3642 case CK_Scroll_Up:
3643 case CK_Scroll_Up_Highlight:
3644 case CK_Scroll_Up_Alt_Highlight:
3645 case CK_Scroll_Down:
3646 case CK_Scroll_Down_Highlight:
3647 case CK_Scroll_Down_Alt_Highlight:
3648 edit->search_start = edit->curs1;
3649 edit->found_len = 0;
3650 break;
3651 default:
3652 edit->found_len = 0;
3653 edit->prev_col = edit_get_col (edit);
3654 edit->search_start = edit->curs1;
3656 edit_find_bracket (edit);
3658 if (option_auto_para_formatting)
3660 switch (command)
3662 case CK_BackSpace:
3663 case CK_Delete:
3664 case CK_Delete_Word_Left:
3665 case CK_Delete_Word_Right:
3666 case CK_Delete_To_Line_End:
3667 case CK_Delete_To_Line_Begin:
3668 format_paragraph (edit, 0);
3669 edit->force |= REDRAW_PAGE;
3675 static void
3676 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
3678 int i = 0;
3680 if (edit->macro_depth++ > 256)
3682 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3683 edit->macro_depth--;
3684 return;
3686 edit->force |= REDRAW_PAGE;
3687 for (; i < n; i++)
3689 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3691 edit_update_screen (edit);
3692 edit->macro_depth--;
3695 /* User edit menu, like user menu (F2) but only in editor. */
3696 static void
3697 user_menu (WEdit * edit)
3699 char *block_file;
3700 int nomark;
3701 long start_mark, end_mark;
3702 struct stat status;
3704 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3706 nomark = eval_marks (edit, &start_mark, &end_mark);
3707 if (nomark == 0)
3708 edit_save_block (edit, block_file, start_mark, end_mark);
3710 /* run shell scripts from menu */
3711 user_menu_cmd (edit);
3713 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
3715 int rc = 0;
3716 FILE *fd;
3718 if (nomark == 0)
3720 /* i.e. we have marked block */
3721 rc = edit_block_delete_cmd (edit);
3724 if (rc == 0)
3725 edit_insert_file (edit, block_file);
3727 /* truncate block file */
3728 fd = fopen (block_file, "w");
3729 if (fd != NULL)
3730 fclose (fd);
3732 edit_refresh_cmd (edit);
3733 edit->force |= REDRAW_COMPLETELY;
3735 g_free (block_file);
3738 void
3739 edit_stack_init (void)
3741 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3743 edit_history_moveto[edit_stack_iterator].filename = NULL;
3744 edit_history_moveto[edit_stack_iterator].line = -1;
3747 edit_stack_iterator = 0;
3750 void
3751 edit_stack_free (void)
3753 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3754 g_free (edit_history_moveto[edit_stack_iterator].filename);
3757 /* move i lines */
3758 void
3759 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3761 edit_move_updown (edit, i, do_scroll, TRUE);
3764 /* move i lines */
3765 void
3766 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3768 edit_move_updown (edit, i, do_scroll, FALSE);
3771 unsigned int
3772 edit_unlock_file (WEdit * edit)
3774 char *fullpath;
3775 unsigned int ret;
3777 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
3778 ret = unlock_file (fullpath);
3779 g_free (fullpath);
3781 return ret;
3784 unsigned int
3785 edit_lock_file (WEdit * edit)
3787 char *fullpath;
3788 unsigned int ret;
3790 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
3791 ret = lock_file (fullpath);
3792 g_free (fullpath);
3794 return ret;