Ticket #2097: clean up before 4.7.2 release.
[midnight-commander.git] / src / editor / edit.c
blobd7fa462b8540241279eb7cd279199d4d9d1ae139
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor low level data handling and cursor fundamentals
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "src/charsets.h" /* get_codepage_id */
57 #include "src/main.h" /* source_codepage */
58 #include "src/learn.h" /* learn_keys */
59 #include "src/cmddef.h"
61 #include "edit-impl.h"
62 #include "editlock.h"
63 #include "edit-widget.h"
66 int option_word_wrap_line_length = 72;
67 int option_typewriter_wrap = 0;
68 int option_auto_para_formatting = 0;
69 int option_fill_tabs_with_spaces = 0;
70 int option_return_does_auto_indent = 1;
71 int option_backspace_through_tabs = 0;
72 int option_fake_half_tabs = 1;
73 int option_save_mode = EDIT_QUICK_SAVE;
74 int option_save_position = 1;
75 int option_max_undo = 32768;
76 int option_persistent_selections = 1;
77 int option_cursor_beyond_eol = 0;
78 int option_line_state = 0;
79 int option_line_state_width = 0;
81 int option_edit_right_extreme = 0;
82 int option_edit_left_extreme = 0;
83 int option_edit_top_extreme = 0;
84 int option_edit_bottom_extreme = 0;
85 int enable_show_tabs_tws = 1;
86 int option_check_nl_at_eof = 0;
87 int show_right_margin = 0;
89 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext = NULL;
92 int edit_stack_iterator = 0;
93 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
97 /*-
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \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
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
112 * ^ | ^
113 * | ^|^ |
114 * cursor ||| cursor
115 * |||
116 * file end|||file beginning
121 * This_is_some_file
122 * fin.
125 const global_keymap_t *editor_map;
126 const global_keymap_t *editor_x_map;
128 static void user_menu (WEdit * edit);
131 edit_get_byte (WEdit * edit, long byte_index)
133 unsigned long p;
134 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
135 return '\n';
137 if (byte_index >= edit->curs1)
139 p = edit->curs1 + edit->curs2 - byte_index - 1;
140 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
142 else
144 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
148 char *
149 edit_get_byte_ptr (WEdit * edit, long byte_index)
151 unsigned long p;
152 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
153 return NULL;
155 if (byte_index >= edit->curs1)
157 p = edit->curs1 + edit->curs2 - byte_index - 1;
158 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
159 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
161 else
163 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
164 (byte_index & M_EDIT_BUF_SIZE));
168 char *
169 edit_get_buf_ptr (WEdit * edit, long byte_index)
171 unsigned long p;
173 if (byte_index >= (edit->curs1 + edit->curs2))
174 byte_index--;
176 if (byte_index < 0)
177 return NULL;
179 if (byte_index >= edit->curs1)
181 p = edit->curs1 + edit->curs2 - 1;
182 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
183 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
185 else
187 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
192 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
194 gchar *str = NULL;
195 int res = -1;
196 gunichar ch;
197 gchar *next_ch = NULL;
198 int width = 0;
200 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
202 *char_width = 0;
203 return '\n';
206 str = edit_get_byte_ptr (edit, byte_index);
208 if (str == NULL)
210 *char_width = 0;
211 return 0;
214 res = g_utf8_get_char_validated (str, -1);
216 if (res < 0)
218 ch = *str;
219 width = 0;
221 else
223 ch = res;
224 /* Calculate UTF-8 char width */
225 next_ch = g_utf8_next_char (str);
226 if (next_ch)
228 width = next_ch - str;
230 else
232 ch = 0;
233 width = 0;
236 *char_width = width;
237 return ch;
241 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
243 gchar *str, *buf = NULL;
244 int res = -1;
245 gunichar ch;
246 gchar *next_ch = NULL;
247 int width = 0;
249 if (byte_index > 0)
250 byte_index--;
252 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
254 *char_width = 0;
255 return 0;
258 ch = edit_get_utf (edit, byte_index, &width);
260 if (width == 1)
262 *char_width = width;
263 return ch;
266 str = edit_get_byte_ptr (edit, byte_index);
267 buf = edit_get_buf_ptr (edit, byte_index);
268 if (str == NULL || buf == NULL)
270 *char_width = 0;
271 return 0;
273 /* get prev utf8 char */
274 if (str != buf)
275 str = g_utf8_find_prev_char (buf, str);
277 res = g_utf8_get_char_validated (str, -1);
279 if (res < 0)
281 ch = *str;
282 width = 0;
284 else
286 ch = res;
287 /* Calculate UTF-8 char width */
288 next_ch = g_utf8_next_char (str);
289 if (next_ch)
291 width = next_ch - str;
293 else
295 ch = 0;
296 width = 0;
299 *char_width = width;
300 return ch;
304 * Initialize the buffers for an empty files.
306 static void
307 edit_init_buffers (WEdit * edit)
309 int j;
311 for (j = 0; j <= MAXBUFF; j++)
313 edit->buffers1[j] = NULL;
314 edit->buffers2[j] = NULL;
317 edit->curs1 = 0;
318 edit->curs2 = 0;
319 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
323 * Load file OR text into buffers. Set cursor to the beginning of file.
324 * Return 1 on error.
326 static int
327 edit_load_file_fast (WEdit * edit, const char *filename)
329 long buf, buf2;
330 int file = -1;
331 int ret = 1;
332 edit->curs2 = edit->last_byte;
333 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
334 edit->utf8 = 0;
335 file = mc_open (filename, O_RDONLY | O_BINARY);
336 if (file == -1)
338 GString *errmsg = g_string_new (NULL);
339 g_string_sprintf (errmsg, _(" Cannot open %s for reading "), filename);
340 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
341 g_string_free (errmsg, TRUE);
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 GString *errmsg = (GString *) 0;
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 g_string_sprintf (errmsg = g_string_new (NULL),
646 _(" Cannot open %s for reading "), filename);
647 goto cleanup;
649 else
651 /* New file, delete it if it's not modified or saved */
652 edit->delete_file = 1;
656 /* Check what we have opened */
657 if (mc_fstat (file, st) < 0)
659 g_string_sprintf (errmsg = g_string_new (NULL),
660 _(" Cannot get size/permissions for %s "), filename);
661 goto cleanup;
664 /* We want to open regular files only */
665 if (!S_ISREG (st->st_mode))
667 g_string_sprintf (errmsg = g_string_new (NULL), _(" %s is not a regular file "), filename);
668 goto cleanup;
672 * Don't delete non-empty files.
673 * O_EXCL should prevent it, but let's be on the safe side.
675 if (st->st_size > 0)
677 edit->delete_file = 0;
680 if (st->st_size >= SIZE_LIMIT)
682 g_string_sprintf (errmsg = g_string_new (NULL), _(" File %s is too large "), filename);
685 cleanup:
686 (void) mc_close (file);
687 if (errmsg)
689 edit_error_dialog (_("Error"), errmsg->str);
690 g_string_free (errmsg, TRUE);
691 return 1;
693 return 0;
697 * Open the file and load it into the buffers, either directly or using
698 * a filter. Return 0 on success, 1 on error.
700 * Fast loading (edit_load_file_fast) is used when the file size is
701 * known. In this case the data is read into the buffers by blocks.
702 * If the file size is not known, the data is loaded byte by byte in
703 * edit_insert_file.
705 static int
706 edit_load_file (WEdit * edit)
708 int fast_load = 1;
710 /* Cannot do fast load if a filter is used */
711 if (edit_find_filter (edit->filename) >= 0)
712 fast_load = 0;
715 * VFS may report file size incorrectly, and slow load is not a big
716 * deal considering overhead in VFS.
718 if (!vfs_file_is_local (edit->filename))
719 fast_load = 0;
722 * FIXME: line end translation should disable fast loading as well
723 * Consider doing fseek() to the end and ftell() for the real size.
726 if (*edit->filename)
728 /* If we are dealing with a real file, check that it exists */
729 if (check_file_access (edit, edit->filename, &edit->stat1))
730 return 1;
732 else
734 /* nothing to load */
735 fast_load = 0;
738 edit_init_buffers (edit);
740 if (fast_load)
742 edit->last_byte = edit->stat1.st_size;
743 edit_load_file_fast (edit, edit->filename);
744 /* If fast load was used, the number of lines wasn't calculated */
745 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
747 else
749 #ifdef HAVE_CHARSET
750 const char *codepage_id;
751 #endif
752 edit->last_byte = 0;
753 if (*edit->filename)
755 edit->stack_disable = 1;
756 if (!edit_insert_file (edit, edit->filename))
758 edit_clean (edit);
759 return 1;
761 edit->stack_disable = 0;
764 #ifdef HAVE_CHARSET
765 codepage_id = get_codepage_id (source_codepage);
766 if (codepage_id)
767 edit->utf8 = str_isutf8 (codepage_id);
768 #endif
770 edit->lb = LB_ASIS;
771 return 0;
774 /* Restore saved cursor position in the file */
775 static void
776 edit_load_position (WEdit * edit)
778 char *filename;
779 long line, column;
780 off_t offset;
782 if (!edit->filename || !*edit->filename)
783 return;
785 filename = vfs_canon (edit->filename);
786 load_file_position (filename, &line, &column, &offset);
787 g_free (filename);
789 if (line > 0)
791 edit_move_to_line (edit, line - 1);
792 edit->prev_col = column;
794 else if (offset > 0)
796 edit_cursor_move (edit, offset);
797 line = edit->curs_line;
799 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
800 edit_move_display (edit, line - (edit->num_widget_lines / 2));
803 /* Save cursor position in the file */
804 static void
805 edit_save_position (WEdit * edit)
807 char *filename;
809 if (!edit->filename || !*edit->filename)
810 return;
812 filename = vfs_canon (edit->filename);
813 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
814 g_free (filename);
817 /* Clean the WEdit stricture except the widget part */
818 static void
819 edit_purge_widget (WEdit * edit)
821 size_t len = sizeof (WEdit) - sizeof (Widget);
822 char *start = (char *) edit + sizeof (Widget);
823 memset (start, 0, len);
824 edit->macro_i = -1; /* not recording a macro */
827 static void
828 edit_set_keymap (void)
830 editor_map = default_editor_keymap;
831 if (editor_keymap && editor_keymap->len > 0)
832 editor_map = (global_keymap_t *) editor_keymap->data;
834 editor_x_map = default_editor_x_keymap;
835 if (editor_x_keymap && editor_x_keymap->len > 0)
836 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
840 #define space_width 1
843 * Fill in the edit structure. Return NULL on failure. Pass edit as
844 * NULL to allocate a new structure.
846 * If line is 0, try to restore saved position. Otherwise put the
847 * cursor on that line and show it in the middle of the screen.
849 WEdit *
850 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
852 int to_free = 0;
853 option_auto_syntax = 1; /* Resetting to auto on every invokation */
854 if (option_line_state)
856 option_line_state_width = LINE_STATE_WIDTH;
858 else
860 option_line_state_width = 0;
862 if (!edit)
864 #ifdef ENABLE_NLS
866 * Expand option_whole_chars_search by national letters using
867 * current locale
870 static char option_whole_chars_search_buf[256];
872 if (option_whole_chars_search_buf != option_whole_chars_search)
874 size_t i;
875 size_t len = str_term_width1 (option_whole_chars_search);
877 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
879 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
881 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
883 option_whole_chars_search_buf[len++] = i;
887 option_whole_chars_search_buf[len] = 0;
888 option_whole_chars_search = option_whole_chars_search_buf;
890 #endif /* ENABLE_NLS */
891 edit = g_malloc0 (sizeof (WEdit));
892 edit->search = NULL;
893 to_free = 1;
895 edit_purge_widget (edit);
896 edit->num_widget_lines = lines;
897 edit->over_col = 0;
898 edit->num_widget_columns = columns;
899 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
900 edit->stat1.st_uid = getuid ();
901 edit->stat1.st_gid = getgid ();
902 edit->stat1.st_mtime = 0;
903 edit->bracket = -1;
904 edit->force |= REDRAW_PAGE;
905 edit_set_filename (edit, filename);
906 edit->stack_size = START_STACK_SIZE;
907 edit->stack_size_mask = START_STACK_SIZE - 1;
908 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
909 if (edit_load_file (edit))
911 /* edit_load_file already gives an error message */
912 if (to_free)
913 g_free (edit);
914 return 0;
916 edit->utf8 = 0;
917 edit->converter = str_cnv_from_term;
918 #ifdef HAVE_CHARSET
920 const char *cp_id = NULL;
921 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
923 if (cp_id != NULL)
925 GIConv conv;
926 conv = str_crt_conv_from (cp_id);
927 if (conv != INVALID_CONV)
929 if (edit->converter != str_cnv_from_term)
930 str_close_conv (edit->converter);
931 edit->converter = conv;
934 if (cp_id != NULL)
935 edit->utf8 = str_isutf8 (cp_id);
937 #endif
939 edit->loading_done = 1;
940 edit->modified = 0;
941 edit->locked = 0;
942 edit_load_syntax (edit, NULL, NULL);
944 int color;
945 edit_get_syntax_color (edit, -1, &color);
948 /* load saved cursor position */
949 if ((line == 0) && option_save_position)
951 edit_load_position (edit);
953 else
955 if (line <= 0)
956 line = 1;
957 edit_move_display (edit, line - 1);
958 edit_move_to_line (edit, line - 1);
961 edit_set_keymap ();
963 return edit;
966 /* Clear the edit struct, freeing everything in it. Return 1 on success */
968 edit_clean (WEdit * edit)
970 int j = 0;
972 if (!edit)
973 return 0;
975 /* a stale lock, remove it */
976 if (edit->locked)
977 edit->locked = edit_unlock_file (edit->filename);
979 /* save cursor position */
980 if (option_save_position)
981 edit_save_position (edit);
983 /* File specified on the mcedit command line and never saved */
984 if (edit->delete_file)
985 unlink (edit->filename);
987 edit_free_syntax_rules (edit);
988 book_mark_flush (edit, -1);
989 for (; j <= MAXBUFF; j++)
991 g_free (edit->buffers1[j]);
992 g_free (edit->buffers2[j]);
995 g_free (edit->undo_stack);
996 g_free (edit->filename);
997 g_free (edit->dir);
999 mc_search_free (edit->search);
1000 edit->search = NULL;
1002 if (edit->converter != str_cnv_from_term)
1003 str_close_conv (edit->converter);
1005 edit_purge_widget (edit);
1007 return 1;
1010 /* returns 1 on success */
1012 edit_renew (WEdit * edit)
1014 int lines = edit->num_widget_lines;
1015 int columns = edit->num_widget_columns;
1017 edit_clean (edit);
1018 return (edit_init (edit, lines, columns, "", 0) != NULL);
1022 * Load a new file into the editor. If it fails, preserve the old file.
1023 * To do it, allocate a new widget, initialize it and, if the new file
1024 * was loaded, copy the data to the old widget.
1025 * Return 1 on success, 0 on failure.
1028 edit_reload (WEdit * edit, const char *filename)
1030 WEdit *e;
1031 int lines = edit->num_widget_lines;
1032 int columns = edit->num_widget_columns;
1034 e = g_malloc0 (sizeof (WEdit));
1035 e->widget = edit->widget;
1036 if (!edit_init (e, lines, columns, filename, 0))
1038 g_free (e);
1039 return 0;
1041 edit_clean (edit);
1042 memcpy (edit, e, sizeof (WEdit));
1043 g_free (e);
1044 return 1;
1048 * Load a new file into the editor and set line. If it fails, preserve the old file.
1049 * To do it, allocate a new widget, initialize it and, if the new file
1050 * was loaded, copy the data to the old widget.
1051 * Return 1 on success, 0 on failure.
1054 edit_reload_line (WEdit * edit, const char *filename, long line)
1056 WEdit *e;
1057 int lines = edit->num_widget_lines;
1058 int columns = edit->num_widget_columns;
1060 e = g_malloc0 (sizeof (WEdit));
1061 e->widget = edit->widget;
1062 if (!edit_init (e, lines, columns, filename, line))
1064 g_free (e);
1065 return 0;
1067 edit_clean (edit);
1068 memcpy (edit, e, sizeof (WEdit));
1069 g_free (e);
1070 return 1;
1075 Recording stack for undo:
1076 The following is an implementation of a compressed stack. Identical
1077 pushes are recorded by a negative prefix indicating the number of times the
1078 same char was pushed. This saves space for repeated curs-left or curs-right
1079 delete etc.
1083 pushed: stored:
1087 b -3
1089 c --> -4
1095 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1096 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1097 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1098 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1099 position.
1101 The only way the cursor moves or the buffer is changed is through the routines:
1102 insert, backspace, insert_ahead, delete, and cursor_move.
1103 These record the reverse undo movements onto the stack each time they are
1104 called.
1106 Each key press results in a set of actions (insert; delete ...). So each time
1107 a key is pressed the current position of start_display is pushed as
1108 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1109 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1110 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1114 void
1115 edit_push_action (WEdit * edit, long c, ...)
1117 unsigned long sp = edit->stack_pointer;
1118 unsigned long spm1;
1119 long *t;
1121 /* first enlarge the stack if necessary */
1122 if (sp > edit->stack_size - 10)
1123 { /* say */
1124 if (option_max_undo < 256)
1125 option_max_undo = 256;
1126 if (edit->stack_size < (unsigned long) option_max_undo)
1128 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1129 if (t)
1131 edit->undo_stack = t;
1132 edit->stack_size <<= 1;
1133 edit->stack_size_mask = edit->stack_size - 1;
1137 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1138 if (edit->stack_disable)
1139 return;
1141 #ifdef FAST_MOVE_CURSOR
1142 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
1144 va_list ap;
1145 edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
1146 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1147 va_start (ap, c);
1148 c = -(va_arg (ap, int));
1149 va_end (ap);
1151 else
1152 #endif /* ! FAST_MOVE_CURSOR */
1153 if (edit->stack_bottom != sp
1154 && spm1 != edit->stack_bottom
1155 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
1157 int d;
1158 if (edit->undo_stack[spm1] < 0)
1160 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1161 if (d == c)
1163 if (edit->undo_stack[spm1] > -1000000000)
1165 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1166 edit->undo_stack[spm1]--;
1167 return;
1170 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1171 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1172 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1173 { /* a left then a right anihilate each other */
1174 if (edit->undo_stack[spm1] == -2)
1175 edit->stack_pointer = spm1;
1176 else
1177 edit->undo_stack[spm1]++;
1178 return;
1180 #endif
1182 else
1184 d = edit->undo_stack[spm1];
1185 if (d == c)
1187 if (c >= KEY_PRESS)
1188 return; /* --> no need to push multiple do-nothings */
1189 edit->undo_stack[sp] = -2;
1190 goto check_bottom;
1192 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1193 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1194 { /* a left then a right anihilate each other */
1195 edit->stack_pointer = spm1;
1196 return;
1198 #endif
1201 edit->undo_stack[sp] = c;
1203 check_bottom:
1204 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1206 /* if the sp wraps round and catches the stack_bottom then erase
1207 * the first set of actions on the stack to make space - by moving
1208 * stack_bottom forward one "key press" */
1209 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1210 if ((unsigned long) c == edit->stack_bottom ||
1211 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1214 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1216 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
1217 && edit->stack_bottom != edit->stack_pointer);
1219 /*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: */
1220 if (edit->stack_pointer != edit->stack_bottom
1221 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1222 edit->stack_bottom = edit->stack_pointer = 0;
1226 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1227 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1229 static long
1230 pop_action (WEdit * edit)
1232 long c;
1233 unsigned long sp = edit->stack_pointer;
1235 if (sp == edit->stack_bottom)
1236 return STACK_BOTTOM;
1238 sp = (sp - 1) & edit->stack_size_mask;
1239 c = edit->undo_stack[sp];
1240 if (c >= 0)
1242 /* edit->undo_stack[sp] = '@'; */
1243 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1244 return c;
1247 if (sp == edit->stack_bottom)
1248 return STACK_BOTTOM;
1250 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1251 if (edit->undo_stack[sp] == -2)
1253 /* edit->undo_stack[sp] = '@'; */
1254 edit->stack_pointer = sp;
1256 else
1257 edit->undo_stack[sp]++;
1259 return c;
1262 /* is called whenever a modification is made by one of the four routines below */
1263 static void
1264 edit_modification (WEdit * edit)
1266 edit->caches_valid = 0;
1267 edit->screen_modified = 1;
1269 /* raise lock when file modified */
1270 if (!edit->modified && !edit->delete_file)
1271 edit->locked = edit_lock_file (edit->filename);
1272 edit->modified = 1;
1276 Basic low level single character buffer alterations and movements at the cursor.
1277 Returns char passed over, inserted or removed.
1280 void
1281 edit_insert (WEdit * edit, int c)
1283 /* check if file has grown to large */
1284 if (edit->last_byte >= SIZE_LIMIT)
1285 return;
1287 /* first we must update the position of the display window */
1288 if (edit->curs1 < edit->start_display)
1290 edit->start_display++;
1291 if (c == '\n')
1292 edit->start_line++;
1295 /* Mark file as modified, unless the file hasn't been fully loaded */
1296 if (edit->loading_done)
1298 edit_modification (edit);
1301 /* now we must update some info on the file and check if a redraw is required */
1302 if (c == '\n')
1304 if (edit->book_mark)
1305 book_mark_inc (edit, edit->curs_line);
1306 edit->curs_line++;
1307 edit->total_lines++;
1308 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1311 /* save the reverse command onto the undo stack */
1312 edit_push_action (edit, BACKSPACE);
1314 /* update markers */
1315 edit->mark1 += (edit->mark1 > edit->curs1);
1316 edit->mark2 += (edit->mark2 > edit->curs1);
1317 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1319 /* add a new buffer if we've reached the end of the last one */
1320 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1321 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1323 /* perform the insertion */
1324 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
1325 = (unsigned char) c;
1327 /* update file length */
1328 edit->last_byte++;
1330 /* update cursor position */
1331 edit->curs1++;
1334 static void
1335 edit_insert_over (WEdit * edit)
1337 int i;
1339 for (i = 0; i < edit->over_col; i++)
1341 edit_insert (edit, ' ');
1343 edit->over_col = 0;
1346 /* same as edit_insert and move left */
1347 void
1348 edit_insert_ahead (WEdit * edit, int c)
1350 if (edit->last_byte >= SIZE_LIMIT)
1351 return;
1353 if (edit->curs1 < edit->start_display)
1355 edit->start_display++;
1356 if (c == '\n')
1357 edit->start_line++;
1359 edit_modification (edit);
1360 if (c == '\n')
1362 if (edit->book_mark)
1363 book_mark_inc (edit, edit->curs_line);
1364 edit->total_lines++;
1365 edit->force |= REDRAW_AFTER_CURSOR;
1367 edit_push_action (edit, DELCHAR);
1369 edit->mark1 += (edit->mark1 >= edit->curs1);
1370 edit->mark2 += (edit->mark2 >= edit->curs1);
1371 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1373 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1374 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1375 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
1376 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1378 edit->last_byte++;
1379 edit->curs2++;
1384 edit_delete (WEdit * edit, const int byte_delete)
1386 int p = 0;
1387 int cw = 1;
1388 int i;
1390 if (!edit->curs2)
1391 return 0;
1393 edit->mark1 -= (edit->mark1 > edit->curs1);
1394 edit->mark2 -= (edit->mark2 > edit->curs1);
1395 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1397 cw = 1;
1398 /* if byte_delete = 1 then delete only one byte not multibyte char */
1399 if (edit->utf8 && byte_delete == 0)
1401 edit_get_utf (edit, edit->curs1, &cw);
1402 if (cw < 1)
1403 cw = 1;
1405 for (i = 1; i <= cw; i++)
1407 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1408 ((edit->curs2 -
1409 1) & M_EDIT_BUF_SIZE) - 1];
1411 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1413 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1414 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1416 edit->last_byte--;
1417 edit->curs2--;
1418 edit_push_action (edit, p + 256);
1421 edit_modification (edit);
1422 if (p == '\n')
1424 if (edit->book_mark)
1425 book_mark_dec (edit, edit->curs_line);
1426 edit->total_lines--;
1427 edit->force |= REDRAW_AFTER_CURSOR;
1429 if (edit->curs1 < edit->start_display)
1431 edit->start_display--;
1432 if (p == '\n')
1433 edit->start_line--;
1436 return p;
1440 static int
1441 edit_backspace (WEdit * edit, const int byte_delete)
1443 int p = 0;
1444 int cw = 1;
1445 int i;
1447 if (!edit->curs1)
1448 return 0;
1450 edit->mark1 -= (edit->mark1 >= edit->curs1);
1451 edit->mark2 -= (edit->mark2 >= edit->curs1);
1452 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1454 cw = 1;
1455 if (edit->utf8 && byte_delete == 0)
1457 edit_get_prev_utf (edit, edit->curs1, &cw);
1458 if (cw < 1)
1459 cw = 1;
1461 for (i = 1; i <= cw; i++)
1463 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
1464 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1465 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1467 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1468 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1470 edit->last_byte--;
1471 edit->curs1--;
1472 edit_push_action (edit, p);
1474 edit_modification (edit);
1475 if (p == '\n')
1477 if (edit->book_mark)
1478 book_mark_dec (edit, edit->curs_line);
1479 edit->curs_line--;
1480 edit->total_lines--;
1481 edit->force |= REDRAW_AFTER_CURSOR;
1484 if (edit->curs1 < edit->start_display)
1486 edit->start_display--;
1487 if (p == '\n')
1488 edit->start_line--;
1491 return p;
1494 #ifdef FAST_MOVE_CURSOR
1496 static void
1497 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1499 unsigned long next;
1500 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
1502 edit->curs_line--;
1503 next -= (unsigned long) dest;
1504 n -= next;
1505 src += next;
1506 dest += next;
1511 edit_move_backward_lots (WEdit * edit, long increment)
1513 int r, s, t;
1514 unsigned char *p = NULL;
1516 if (increment > edit->curs1)
1517 increment = edit->curs1;
1518 if (increment <= 0)
1519 return -1;
1520 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1522 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1523 if (r > increment)
1524 r = increment;
1525 s = edit->curs1 & M_EDIT_BUF_SIZE;
1527 if (s > r)
1529 memqcpy (edit,
1530 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1531 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
1533 else
1535 if (s != 0)
1537 memqcpy (edit,
1538 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1539 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1540 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1541 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1543 memqcpy (edit,
1544 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1545 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1546 EDIT_BUF_SIZE - (r - s), r - s);
1548 increment -= r;
1549 edit->curs1 -= r;
1550 edit->curs2 += r;
1551 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1553 if (p)
1554 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1555 else
1556 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1558 else
1560 g_free (p);
1563 s = edit->curs1 & M_EDIT_BUF_SIZE;
1564 while (increment)
1566 p = 0;
1567 r = EDIT_BUF_SIZE;
1568 if (r > increment)
1569 r = increment;
1570 t = s;
1571 if (r < t)
1572 t = r;
1573 memqcpy (edit,
1574 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1575 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
1576 if (r >= s)
1578 if (t)
1580 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1581 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1583 memqcpy (edit,
1584 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1585 EDIT_BUF_SIZE - r,
1586 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1587 EDIT_BUF_SIZE - (r - s), r - s);
1589 increment -= r;
1590 edit->curs1 -= r;
1591 edit->curs2 += r;
1592 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1594 if (p)
1595 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1596 else
1597 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1599 else
1600 g_free (p);
1602 return edit_get_byte (edit, edit->curs1);
1605 #endif /* ! FAST_MOVE_CURSOR */
1607 /* moves the cursor right or left: increment positive or negative respectively */
1608 void
1609 edit_cursor_move (WEdit * edit, long increment)
1611 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1612 int c;
1613 #ifdef FAST_MOVE_CURSOR
1614 if (increment < -256)
1616 edit->force |= REDRAW_PAGE;
1617 edit_move_backward_lots (edit, -increment);
1618 return;
1620 #endif /* ! FAST_MOVE_CURSOR */
1622 if (increment < 0)
1624 for (; increment < 0; increment++)
1626 if (!edit->curs1)
1627 return;
1629 edit_push_action (edit, CURS_RIGHT);
1631 c = edit_get_byte (edit, edit->curs1 - 1);
1632 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1633 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1634 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1635 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1636 edit->curs2++;
1637 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
1638 1) & M_EDIT_BUF_SIZE];
1639 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1641 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1642 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1644 edit->curs1--;
1645 if (c == '\n')
1647 edit->curs_line--;
1648 edit->force |= REDRAW_LINE_BELOW;
1653 else if (increment > 0)
1655 for (; increment > 0; increment--)
1657 if (!edit->curs2)
1658 return;
1660 edit_push_action (edit, CURS_LEFT);
1662 c = edit_get_byte (edit, edit->curs1);
1663 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1664 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1665 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1666 edit->curs1++;
1667 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1668 ((edit->curs2 -
1669 1) & M_EDIT_BUF_SIZE) - 1];
1670 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1672 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1673 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1675 edit->curs2--;
1676 if (c == '\n')
1678 edit->curs_line++;
1679 edit->force |= REDRAW_LINE_ABOVE;
1685 /* These functions return positions relative to lines */
1687 /* returns index of last char on line + 1 */
1688 long
1689 edit_eol (WEdit * edit, long current)
1691 if (current >= edit->last_byte)
1692 return edit->last_byte;
1694 for (;; current++)
1695 if (edit_get_byte (edit, current) == '\n')
1696 break;
1697 return current;
1700 /* returns index of first char on line */
1701 long
1702 edit_bol (WEdit * edit, long current)
1704 if (current <= 0)
1705 return 0;
1707 for (;; current--)
1708 if (edit_get_byte (edit, current - 1) == '\n')
1709 break;
1710 return current;
1714 long
1715 edit_count_lines (WEdit * edit, long current, long upto)
1717 long lines = 0;
1718 if (upto > edit->last_byte)
1719 upto = edit->last_byte;
1720 if (current < 0)
1721 current = 0;
1722 while (current < upto)
1723 if (edit_get_byte (edit, current++) == '\n')
1724 lines++;
1725 return lines;
1729 /* If lines is zero this returns the count of lines from current to upto. */
1730 /* If upto is zero returns index of lines forward current. */
1731 long
1732 edit_move_forward (WEdit * edit, long current, long lines, long upto)
1734 if (upto)
1736 return edit_count_lines (edit, current, upto);
1738 else
1740 long next;
1741 if (lines < 0)
1742 lines = 0;
1743 while (lines--)
1745 next = edit_eol (edit, current) + 1;
1746 if (next > edit->last_byte)
1747 break;
1748 else
1749 current = next;
1751 return current;
1756 /* Returns offset of 'lines' lines up from current */
1757 long
1758 edit_move_backward (WEdit * edit, long current, long lines)
1760 if (lines < 0)
1761 lines = 0;
1762 current = edit_bol (edit, current);
1763 while ((lines--) && current != 0)
1764 current = edit_bol (edit, current - 1);
1765 return current;
1768 /* If cols is zero this returns the count of columns from current to upto. */
1769 /* If upto is zero returns index of cols across from current. */
1770 long
1771 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1773 long p, q;
1774 int col;
1776 if (upto)
1778 q = upto;
1779 cols = -10;
1781 else
1782 q = edit->last_byte + 2;
1784 for (col = 0, p = current; p < q; p++)
1786 int c, orig_c;
1787 int utf_ch = 0;
1788 #ifdef HAVE_CHARSET
1789 int cw = 1;
1790 #endif
1791 if (cols != -10)
1793 if (col == cols)
1794 return p;
1795 if (col > cols)
1796 return p - 1;
1798 orig_c = c = edit_get_byte (edit, p);
1799 #ifdef HAVE_CHARSET
1800 if (edit->utf8)
1802 utf_ch = edit_get_utf (edit, p, &cw);
1803 if (utf8_display)
1805 if (cw > 1)
1806 col -= cw - 1;
1807 if (g_unichar_iswide (utf_ch))
1808 col++;
1810 else if (cw > 1 && g_unichar_isprint (utf_ch))
1811 col -= cw - 1;
1813 #endif
1814 c = convert_to_display_c (c);
1815 if (c == '\t')
1816 col += TAB_SIZE - col % TAB_SIZE;
1817 else if (c == '\n')
1819 if (upto)
1820 return col;
1821 else
1822 return p;
1824 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1825 /* '\r' is shown as ^M, so we must advance 2 characters */
1826 /* Caret notation for control characters */
1827 col += 2;
1828 else
1829 col++;
1831 return col;
1834 /* returns the current column position of the cursor */
1836 edit_get_col (WEdit * edit)
1838 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1842 /* Scrolling functions */
1844 void
1845 edit_update_curs_row (WEdit * edit)
1847 edit->curs_row = edit->curs_line - edit->start_line;
1850 void
1851 edit_update_curs_col (WEdit * edit)
1853 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1857 edit_get_curs_col (const WEdit * edit)
1859 return edit->curs_col;
1862 /*moves the display start position up by i lines */
1863 void
1864 edit_scroll_upward (WEdit * edit, unsigned long i)
1866 unsigned long lines_above = edit->start_line;
1867 if (i > lines_above)
1868 i = lines_above;
1869 if (i)
1871 edit->start_line -= i;
1872 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1873 edit->force |= REDRAW_PAGE;
1874 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1876 edit_update_curs_row (edit);
1880 /* returns 1 if could scroll, 0 otherwise */
1881 void
1882 edit_scroll_downward (WEdit * edit, int i)
1884 int lines_below;
1885 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1886 if (lines_below > 0)
1888 if (i > lines_below)
1889 i = lines_below;
1890 edit->start_line += i;
1891 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1892 edit->force |= REDRAW_PAGE;
1893 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1895 edit_update_curs_row (edit);
1898 void
1899 edit_scroll_right (WEdit * edit, int i)
1901 edit->force |= REDRAW_PAGE;
1902 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1903 edit->start_col -= i;
1906 void
1907 edit_scroll_left (WEdit * edit, int i)
1909 if (edit->start_col)
1911 edit->start_col += i;
1912 if (edit->start_col > 0)
1913 edit->start_col = 0;
1914 edit->force |= REDRAW_PAGE;
1915 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1919 /* high level cursor movement commands */
1921 static int
1922 is_in_indent (WEdit * edit)
1924 long p = edit_bol (edit, edit->curs1);
1925 while (p < edit->curs1)
1926 if (!strchr (" \t", edit_get_byte (edit, p++)))
1927 return 0;
1928 return 1;
1931 static int left_of_four_spaces (WEdit * edit);
1933 void
1934 edit_move_to_prev_col (WEdit * edit, long p)
1936 int prev = edit->prev_col;
1937 int over = edit->over_col;
1938 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1940 if (option_cursor_beyond_eol)
1942 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
1943 edit_eol (edit, edit->curs1));
1945 if (line_len < prev + edit->over_col)
1947 edit->over_col = prev + over - line_len;
1948 edit->prev_col = line_len;
1949 edit->curs_col = line_len;
1951 else
1953 edit->curs_col = prev + over;
1954 edit->prev_col = edit->curs_col;
1955 edit->over_col = 0;
1958 else
1960 edit->over_col = 0;
1961 if (is_in_indent (edit) && option_fake_half_tabs)
1963 edit_update_curs_col (edit);
1964 if (space_width)
1965 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
1967 int q = edit->curs_col;
1968 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1969 p = edit_bol (edit, edit->curs1);
1970 edit_cursor_move (edit,
1971 edit_move_forward3 (edit, p, edit->curs_col,
1972 0) - edit->curs1);
1973 if (!left_of_four_spaces (edit))
1974 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1980 static int
1981 is_blank (WEdit * edit, long offset)
1983 long s, f;
1984 int c;
1985 s = edit_bol (edit, offset);
1986 f = edit_eol (edit, offset) - 1;
1987 while (s <= f)
1989 c = edit_get_byte (edit, s++);
1990 if (!isspace (c))
1991 return 0;
1993 return 1;
1997 /* returns the offset of line i */
1998 static long
1999 edit_find_line (WEdit * edit, int line)
2001 int i, j = 0;
2002 int m = 2000000000;
2003 if (!edit->caches_valid)
2005 for (i = 0; i < N_LINE_CACHES; i++)
2006 edit->line_numbers[i] = edit->line_offsets[i] = 0;
2007 /* three offsets that we *know* are line 0 at 0 and these two: */
2008 edit->line_numbers[1] = edit->curs_line;
2009 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
2010 edit->line_numbers[2] = edit->total_lines;
2011 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
2012 edit->caches_valid = 1;
2014 if (line >= edit->total_lines)
2015 return edit->line_offsets[2];
2016 if (line <= 0)
2017 return 0;
2018 /* find the closest known point */
2019 for (i = 0; i < N_LINE_CACHES; i++)
2021 int n;
2022 n = abs (edit->line_numbers[i] - line);
2023 if (n < m)
2025 m = n;
2026 j = i;
2029 if (m == 0)
2030 return edit->line_offsets[j]; /* know the offset exactly */
2031 if (m == 1 && j >= 3)
2032 i = j; /* one line different - caller might be looping, so stay in this cache */
2033 else
2034 i = 3 + (rand () % (N_LINE_CACHES - 3));
2035 if (line > edit->line_numbers[j])
2036 edit->line_offsets[i] =
2037 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
2038 else
2039 edit->line_offsets[i] =
2040 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
2041 edit->line_numbers[i] = line;
2042 return edit->line_offsets[i];
2046 line_is_blank (WEdit * edit, long line)
2048 return is_blank (edit, edit_find_line (edit, line));
2051 /* moves up until a blank line is reached, or until just
2052 before a non-blank line is reached */
2053 static void
2054 edit_move_up_paragraph (WEdit * edit, int do_scroll)
2056 int i = 0;
2057 if (edit->curs_line > 1)
2059 if (line_is_blank (edit, edit->curs_line))
2061 if (line_is_blank (edit, edit->curs_line - 1))
2063 for (i = edit->curs_line - 1; i; i--)
2064 if (!line_is_blank (edit, i))
2066 i++;
2067 break;
2070 else
2072 for (i = edit->curs_line - 1; i; i--)
2073 if (line_is_blank (edit, i))
2074 break;
2077 else
2079 for (i = edit->curs_line - 1; i; i--)
2080 if (line_is_blank (edit, i))
2081 break;
2084 edit_move_up (edit, edit->curs_line - i, do_scroll);
2087 /* moves down until a blank line is reached, or until just
2088 before a non-blank line is reached */
2089 static void
2090 edit_move_down_paragraph (WEdit * edit, int do_scroll)
2092 int i;
2093 if (edit->curs_line >= edit->total_lines - 1)
2095 i = edit->total_lines;
2097 else
2099 if (line_is_blank (edit, edit->curs_line))
2101 if (line_is_blank (edit, edit->curs_line + 1))
2103 for (i = edit->curs_line + 1; i; i++)
2104 if (!line_is_blank (edit, i) || i > edit->total_lines)
2106 i--;
2107 break;
2110 else
2112 for (i = edit->curs_line + 1; i; i++)
2113 if (line_is_blank (edit, i) || i >= edit->total_lines)
2114 break;
2117 else
2119 for (i = edit->curs_line + 1; i; i++)
2120 if (line_is_blank (edit, i) || i >= edit->total_lines)
2121 break;
2124 edit_move_down (edit, i - edit->curs_line, do_scroll);
2127 static void
2128 edit_begin_page (WEdit * edit)
2130 edit_update_curs_row (edit);
2131 edit_move_up (edit, edit->curs_row, 0);
2134 static void
2135 edit_end_page (WEdit * edit)
2137 edit_update_curs_row (edit);
2138 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
2142 /* goto beginning of text */
2143 static void
2144 edit_move_to_top (WEdit * edit)
2146 if (edit->curs_line)
2148 edit_cursor_move (edit, -edit->curs1);
2149 edit_move_to_prev_col (edit, 0);
2150 edit->force |= REDRAW_PAGE;
2151 edit->search_start = 0;
2152 edit_update_curs_row (edit);
2157 /* goto end of text */
2158 static void
2159 edit_move_to_bottom (WEdit * edit)
2161 if (edit->curs_line < edit->total_lines)
2163 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
2164 edit->start_display = edit->last_byte;
2165 edit->start_line = edit->total_lines;
2166 edit_scroll_upward (edit, edit->num_widget_lines - 1);
2167 edit->force |= REDRAW_PAGE;
2171 /* goto beginning of line */
2172 static void
2173 edit_cursor_to_bol (WEdit * edit)
2175 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
2176 edit->search_start = edit->curs1;
2177 edit->prev_col = edit_get_col (edit);
2178 edit->over_col = 0;
2181 /* goto end of line */
2182 static void
2183 edit_cursor_to_eol (WEdit * edit)
2185 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
2186 edit->search_start = edit->curs1;
2187 edit->prev_col = edit_get_col (edit);
2188 edit->over_col = 0;
2191 /* move cursor to line 'line' */
2192 void
2193 edit_move_to_line (WEdit * e, long line)
2195 if (line < e->curs_line)
2196 edit_move_up (e, e->curs_line - line, 0);
2197 else
2198 edit_move_down (e, line - e->curs_line, 0);
2199 edit_scroll_screen_over_cursor (e);
2202 /* scroll window so that first visible line is 'line' */
2203 void
2204 edit_move_display (WEdit * e, long line)
2206 if (line < e->start_line)
2207 edit_scroll_upward (e, e->start_line - line);
2208 else
2209 edit_scroll_downward (e, line - e->start_line);
2212 /* save markers onto undo stack */
2213 void
2214 edit_push_markers (WEdit * edit)
2216 edit_push_action (edit, MARK_1 + edit->mark1);
2217 edit_push_action (edit, MARK_2 + edit->mark2);
2220 void
2221 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2223 edit->mark1 = m1;
2224 edit->mark2 = m2;
2225 edit->column1 = c1;
2226 edit->column2 = c2;
2230 /* highlight marker toggle */
2231 void
2232 edit_mark_cmd (WEdit * edit, int unmark)
2234 edit_push_markers (edit);
2235 if (unmark)
2237 edit_set_markers (edit, 0, 0, 0, 0);
2238 edit->force |= REDRAW_PAGE;
2240 else
2242 if (edit->mark2 >= 0)
2244 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
2245 edit->curs_col + edit->over_col);
2246 edit->force |= REDRAW_PAGE;
2248 else
2249 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
2250 edit->curs_col + edit->over_col);
2254 static unsigned long
2255 my_type_of (int c)
2257 int x, r = 0;
2258 const char *p, *q;
2259 const char option_chars_move_whole_word[] =
2260 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2262 if (!c)
2263 return 0;
2264 if (c == '!')
2266 if (*option_chars_move_whole_word == '!')
2267 return 2;
2268 return 0x80000000UL;
2270 if (g_ascii_isupper ((gchar) c))
2271 c = 'A';
2272 else if (g_ascii_islower ((gchar) c))
2273 c = 'a';
2274 else if (g_ascii_isalpha (c))
2275 c = 'a';
2276 else if (isdigit (c))
2277 c = '0';
2278 else if (isspace (c))
2279 c = ' ';
2280 q = strchr (option_chars_move_whole_word, c);
2281 if (!q)
2282 return 0xFFFFFFFFUL;
2285 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2286 if (*p == '!')
2287 x <<= 1;
2288 r |= x;
2290 while ((q = strchr (q + 1, c)));
2291 return r;
2294 static void
2295 edit_left_word_move (WEdit * edit, int s)
2297 for (;;)
2299 int c1, c2;
2300 if (column_highlighting
2301 && edit->mark1 != edit->mark2
2302 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2303 break;
2304 edit_cursor_move (edit, -1);
2305 if (!edit->curs1)
2306 break;
2307 c1 = edit_get_byte (edit, edit->curs1 - 1);
2308 c2 = edit_get_byte (edit, edit->curs1);
2309 if (!(my_type_of (c1) & my_type_of (c2)))
2310 break;
2311 if (isspace (c1) && !isspace (c2))
2312 break;
2313 if (s)
2314 if (!isspace (c1) && isspace (c2))
2315 break;
2319 static void
2320 edit_left_word_move_cmd (WEdit * edit)
2322 edit_left_word_move (edit, 0);
2323 edit->force |= REDRAW_PAGE;
2326 static void
2327 edit_right_word_move (WEdit * edit, int s)
2329 for (;;)
2331 int c1, c2;
2332 if (column_highlighting
2333 && edit->mark1 != edit->mark2
2334 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
2335 break;
2336 edit_cursor_move (edit, 1);
2337 if (edit->curs1 >= edit->last_byte)
2338 break;
2339 c1 = edit_get_byte (edit, edit->curs1 - 1);
2340 c2 = edit_get_byte (edit, edit->curs1);
2341 if (!(my_type_of (c1) & my_type_of (c2)))
2342 break;
2343 if (isspace (c1) && !isspace (c2))
2344 break;
2345 if (s)
2346 if (!isspace (c1) && isspace (c2))
2347 break;
2351 static void
2352 edit_right_word_move_cmd (WEdit * edit)
2354 edit_right_word_move (edit, 0);
2355 edit->force |= REDRAW_PAGE;
2358 static void
2359 edit_right_char_move_cmd (WEdit * edit)
2361 int cw = 1;
2362 int c = 0;
2363 if (edit->utf8)
2365 c = edit_get_utf (edit, edit->curs1, &cw);
2366 if (cw < 1)
2367 cw = 1;
2369 else
2371 c = edit_get_byte (edit, edit->curs1);
2373 if (option_cursor_beyond_eol && c == '\n')
2375 edit->over_col++;
2377 else
2379 edit_cursor_move (edit, cw);
2383 static void
2384 edit_left_char_move_cmd (WEdit * edit)
2386 int cw = 1;
2387 if (column_highlighting
2388 && option_cursor_beyond_eol
2389 && edit->mark1 != edit->mark2
2390 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2391 return;
2392 if (edit->utf8)
2394 edit_get_prev_utf (edit, edit->curs1, &cw);
2395 if (cw < 1)
2396 cw = 1;
2398 if (option_cursor_beyond_eol && edit->over_col > 0)
2400 edit->over_col--;
2402 else
2404 edit_cursor_move (edit, -cw);
2408 /** Up or down cursor moving.
2409 direction = TRUE - move up
2410 = FALSE - move down
2412 static void
2413 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2415 unsigned long p;
2416 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
2418 if (i > l)
2419 i = l;
2421 if (i == 0)
2422 return;
2424 if (i > 1)
2425 edit->force |= REDRAW_PAGE;
2426 if (do_scroll)
2428 if (direction)
2429 edit_scroll_upward (edit, i);
2430 else
2431 edit_scroll_downward (edit, i);
2433 p = edit_bol (edit, edit->curs1);
2435 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
2437 edit_cursor_move (edit, p - edit->curs1);
2439 edit_move_to_prev_col (edit, p);
2441 /* search start of current multibyte char (like CJK) */
2442 if (edit->curs1 + 1 < edit->last_byte)
2444 edit_right_char_move_cmd (edit);
2445 edit_left_char_move_cmd (edit);
2448 edit->search_start = edit->curs1;
2449 edit->found_len = 0;
2452 static void
2453 edit_right_delete_word (WEdit * edit)
2455 int c1, c2;
2456 for (;;)
2458 if (edit->curs1 >= edit->last_byte)
2459 break;
2460 c1 = edit_delete (edit, 1);
2461 c2 = edit_get_byte (edit, edit->curs1);
2462 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2463 break;
2464 if (!(my_type_of (c1) & my_type_of (c2)))
2465 break;
2469 static void
2470 edit_left_delete_word (WEdit * edit)
2472 int c1, c2;
2473 for (;;)
2475 if (edit->curs1 <= 0)
2476 break;
2477 c1 = edit_backspace (edit, 1);
2478 c2 = edit_get_byte (edit, edit->curs1 - 1);
2479 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2480 break;
2481 if (!(my_type_of (c1) & my_type_of (c2)))
2482 break;
2487 the start column position is not recorded, and hence does not
2488 undo as it happed. But who would notice.
2490 static void
2491 edit_do_undo (WEdit * edit)
2493 long ac;
2494 long count = 0;
2496 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2497 edit->over_col = 0;
2498 while ((ac = pop_action (edit)) < KEY_PRESS)
2500 switch ((int) ac)
2502 case STACK_BOTTOM:
2503 goto done_undo;
2504 case CURS_RIGHT:
2505 edit_cursor_move (edit, 1);
2506 break;
2507 case CURS_LEFT:
2508 edit_cursor_move (edit, -1);
2509 break;
2510 case BACKSPACE:
2511 edit_backspace (edit, 1);
2512 break;
2513 case DELCHAR:
2514 edit_delete (edit, 1);
2515 break;
2516 case COLUMN_ON:
2517 column_highlighting = 1;
2518 break;
2519 case COLUMN_OFF:
2520 column_highlighting = 0;
2521 break;
2523 if (ac >= 256 && ac < 512)
2524 edit_insert_ahead (edit, ac - 256);
2525 if (ac >= 0 && ac < 256)
2526 edit_insert (edit, ac);
2528 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
2530 edit->mark1 = ac - MARK_1;
2531 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2533 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
2535 edit->mark2 = ac - MARK_2;
2536 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2538 if (count++)
2539 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2542 if (edit->start_display > ac - KEY_PRESS)
2544 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2545 edit->force |= REDRAW_PAGE;
2547 else if (edit->start_display < ac - KEY_PRESS)
2549 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2550 edit->force |= REDRAW_PAGE;
2552 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2553 edit_update_curs_row (edit);
2555 done_undo:;
2556 edit->stack_disable = 0;
2559 static void
2560 edit_delete_to_line_end (WEdit * edit)
2562 while (edit_get_byte (edit, edit->curs1) != '\n')
2564 if (!edit->curs2)
2565 break;
2566 edit_delete (edit, 1);
2570 static void
2571 edit_delete_to_line_begin (WEdit * edit)
2573 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2575 if (!edit->curs1)
2576 break;
2577 edit_backspace (edit, 1);
2581 void
2582 edit_delete_line (WEdit * edit)
2585 * Delete right part of the line.
2586 * Note that edit_get_byte() returns '\n' when byte position is
2587 * beyond EOF.
2589 while (edit_get_byte (edit, edit->curs1) != '\n')
2591 (void) edit_delete (edit, 1);
2595 * Delete '\n' char.
2596 * Note that edit_delete() will not corrupt anything if called while
2597 * cursor position is EOF.
2599 (void) edit_delete (edit, 1);
2602 * Delete left part of the line.
2603 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2605 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2607 (void) edit_backspace (edit, 1);
2611 void
2612 insert_spaces_tab (WEdit * edit, int half)
2614 int i;
2615 edit_update_curs_col (edit);
2616 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
2617 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2618 while (i > 0)
2620 edit_insert (edit, ' ');
2621 i -= space_width;
2625 static int
2626 is_aligned_on_a_tab (WEdit * edit)
2628 edit_update_curs_col (edit);
2629 return !((edit->curs_col % (TAB_SIZE * space_width))
2630 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2633 static int
2634 right_of_four_spaces (WEdit * edit)
2636 int i, ch = 0;
2637 for (i = 1; i <= HALF_TAB_SIZE; i++)
2638 ch |= edit_get_byte (edit, edit->curs1 - i);
2639 if (ch == ' ')
2640 return is_aligned_on_a_tab (edit);
2641 return 0;
2644 static int
2645 left_of_four_spaces (WEdit * edit)
2647 int i, ch = 0;
2648 for (i = 0; i < HALF_TAB_SIZE; i++)
2649 ch |= edit_get_byte (edit, edit->curs1 + i);
2650 if (ch == ' ')
2651 return is_aligned_on_a_tab (edit);
2652 return 0;
2656 edit_indent_width (WEdit * edit, long p)
2658 long q = p;
2659 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2660 q++;
2661 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2664 void
2665 edit_insert_indent (WEdit * edit, int indent)
2667 if (!option_fill_tabs_with_spaces)
2669 while (indent >= TAB_SIZE)
2671 edit_insert (edit, '\t');
2672 indent -= TAB_SIZE;
2675 while (indent-- > 0)
2676 edit_insert (edit, ' ');
2679 static void
2680 edit_auto_indent (WEdit * edit)
2682 long p;
2683 char c;
2684 p = edit->curs1;
2685 /* use the previous line as a template */
2686 p = edit_move_backward (edit, p, 1);
2687 /* copy the leading whitespace of the line */
2688 for (;;)
2689 { /* no range check - the line _is_ \n-terminated */
2690 c = edit_get_byte (edit, p++);
2691 if (c != ' ' && c != '\t')
2692 break;
2693 edit_insert (edit, c);
2697 static inline void
2698 edit_double_newline (WEdit * edit)
2700 edit_insert (edit, '\n');
2701 if (edit_get_byte (edit, edit->curs1) == '\n')
2702 return;
2703 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2704 return;
2705 edit->force |= REDRAW_PAGE;
2706 edit_insert (edit, '\n');
2709 static inline void
2710 edit_tab_cmd (WEdit * edit)
2712 int i;
2714 if (option_fake_half_tabs)
2716 if (is_in_indent (edit))
2718 /*insert a half tab (usually four spaces) unless there is a
2719 half tab already behind, then delete it and insert a
2720 full tab. */
2721 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
2723 for (i = 1; i <= HALF_TAB_SIZE; i++)
2724 edit_backspace (edit, 1);
2725 edit_insert (edit, '\t');
2727 else
2729 insert_spaces_tab (edit, 1);
2731 return;
2734 if (option_fill_tabs_with_spaces)
2736 insert_spaces_tab (edit, 0);
2738 else
2740 edit_insert (edit, '\t');
2744 static void
2745 check_and_wrap_line (WEdit * edit)
2747 int curs, c;
2748 if (!option_typewriter_wrap)
2749 return;
2750 edit_update_curs_col (edit);
2751 if (edit->curs_col < option_word_wrap_line_length)
2752 return;
2753 curs = edit->curs1;
2754 for (;;)
2756 curs--;
2757 c = edit_get_byte (edit, curs);
2758 if (c == '\n' || curs <= 0)
2760 edit_insert (edit, '\n');
2761 return;
2763 if (c == ' ' || c == '\t')
2765 int current = edit->curs1;
2766 edit_cursor_move (edit, curs - edit->curs1 + 1);
2767 edit_insert (edit, '\n');
2768 edit_cursor_move (edit, current - edit->curs1 + 1);
2769 return;
2774 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
2776 void
2777 edit_push_key_press (WEdit * edit)
2779 edit_push_action (edit, KEY_PRESS + edit->start_display);
2780 if (edit->mark2 == -1)
2781 edit_push_action (edit, MARK_1 + edit->mark1);
2784 /* this find the matching bracket in either direction, and sets edit->bracket */
2785 static long
2786 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2788 const char *const b = "{}{[][()(", *p;
2789 int i = 1, a, inc = -1, c, d, n = 0;
2790 unsigned long j = 0;
2791 long q;
2792 edit_update_curs_row (edit);
2793 c = edit_get_byte (edit, edit->curs1);
2794 p = strchr (b, c);
2795 /* no limit */
2796 if (!furthest_bracket_search)
2797 furthest_bracket_search--;
2798 /* not on a bracket at all */
2799 if (!p)
2800 return -1;
2801 /* the matching bracket */
2802 d = p[1];
2803 /* going left or right? */
2804 if (strchr ("{[(", c))
2805 inc = 1;
2806 for (q = edit->curs1 + inc;; q += inc)
2808 /* out of buffer? */
2809 if (q >= edit->last_byte || q < 0)
2810 break;
2811 a = edit_get_byte (edit, q);
2812 /* don't want to eat CPU */
2813 if (j++ > furthest_bracket_search)
2814 break;
2815 /* out of screen? */
2816 if (in_screen)
2818 if (q < edit->start_display)
2819 break;
2820 /* count lines if searching downward */
2821 if (inc > 0 && a == '\n')
2822 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2823 break;
2825 /* count bracket depth */
2826 i += (a == c) - (a == d);
2827 /* return if bracket depth is zero */
2828 if (!i)
2829 return q;
2831 /* no match */
2832 return -1;
2835 static long last_bracket = -1;
2837 void
2838 edit_find_bracket (WEdit * edit)
2840 edit->bracket = edit_get_bracket (edit, 1, 10000);
2841 if (last_bracket != edit->bracket)
2842 edit->force |= REDRAW_PAGE;
2843 last_bracket = edit->bracket;
2846 static inline void
2847 edit_goto_matching_bracket (WEdit * edit)
2849 long q;
2851 q = edit_get_bracket (edit, 0, 0);
2852 if (q >= 0)
2854 edit->bracket = edit->curs1;
2855 edit->force |= REDRAW_PAGE;
2856 edit_cursor_move (edit, q - edit->curs1);
2861 * This executes a command as though the user initiated it through a key
2862 * press. Callback with WIDGET_KEY as a message calls this after
2863 * translating the key press. This function can be used to pass any
2864 * command to the editor. Note that the screen wouldn't update
2865 * automatically. Either of command or char_for_insertion must be
2866 * passed as -1. Commands are executed, and char_for_insertion is
2867 * inserted at the cursor.
2869 void
2870 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
2872 if (command == CK_Begin_Record_Macro)
2874 edit->macro_i = 0;
2875 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2876 return;
2878 if (command == CK_End_Record_Macro && edit->macro_i != -1)
2880 edit->force |= REDRAW_COMPLETELY;
2881 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2882 edit->macro_i = -1;
2883 return;
2885 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
2887 edit->macro[edit->macro_i].command = command;
2888 edit->macro[edit->macro_i++].ch = char_for_insertion;
2890 /* record the beginning of a set of editing actions initiated by a key press */
2891 if (command != CK_Undo && command != CK_Ext_Mode)
2892 edit_push_key_press (edit);
2894 edit_execute_cmd (edit, command, char_for_insertion);
2895 if (column_highlighting)
2896 edit->force |= REDRAW_PAGE;
2899 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
2902 This executes a command at a lower level than macro recording.
2903 It also does not push a key_press onto the undo stack. This means
2904 that if it is called many times, a single undo command will undo
2905 all of them. It also does not check for the Undo command.
2907 void
2908 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
2910 edit->force |= REDRAW_LINE;
2912 /* The next key press will unhighlight the found string, so update
2913 * the whole page */
2914 if (edit->found_len || column_highlighting)
2915 edit->force |= REDRAW_PAGE;
2917 if (command / 100 == 6)
2918 { /* a highlight command like shift-arrow */
2919 column_highlighting = 0;
2920 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
2922 edit_mark_cmd (edit, 1); /* clear */
2923 edit_mark_cmd (edit, 0); /* marking on */
2925 edit->highlight = 1;
2927 else
2928 { /* any other command */
2929 if (edit->highlight)
2930 edit_mark_cmd (edit, 0); /* clear */
2931 edit->highlight = 0;
2934 /* first check for undo */
2935 if (command == CK_Undo)
2937 edit_do_undo (edit);
2938 edit->found_len = 0;
2939 edit->prev_col = edit_get_col (edit);
2940 edit->search_start = edit->curs1;
2941 return;
2944 /* An ordinary key press */
2945 if (char_for_insertion >= 0)
2947 /* if non persistent selection and text selected */
2948 if (!option_persistent_selections)
2950 if (edit->mark1 != edit->mark2)
2951 edit_block_delete_cmd (edit);
2953 if (edit->overwrite)
2955 /* remove char only one time, after input first byte, multibyte chars */
2956 if ((!utf8_display || edit->charpoint == 0)
2957 && edit_get_byte (edit, edit->curs1) != '\n')
2958 edit_delete (edit, 0);
2960 if (option_cursor_beyond_eol && edit->over_col > 0)
2961 edit_insert_over (edit);
2962 #ifdef HAVE_CHARSET
2963 if (char_for_insertion > 255 && utf8_display == 0)
2965 unsigned char str[6 + 1];
2966 size_t i = 0;
2967 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
2968 if (res == 0)
2970 str[0] = '.';
2971 str[1] = '\0';
2973 else
2975 str[res] = '\0';
2977 while (str[i] != 0 && i <= 6)
2979 char_for_insertion = str[i];
2980 edit_insert (edit, char_for_insertion);
2981 i++;
2984 else
2985 #endif
2986 edit_insert (edit, char_for_insertion);
2988 if (option_auto_para_formatting)
2990 format_paragraph (edit, 0);
2991 edit->force |= REDRAW_PAGE;
2993 else
2994 check_and_wrap_line (edit);
2995 edit->found_len = 0;
2996 edit->prev_col = edit_get_col (edit);
2997 edit->search_start = edit->curs1;
2998 edit_find_bracket (edit);
2999 return;
3002 switch (command)
3004 case CK_Begin_Page:
3005 case CK_End_Page:
3006 case CK_Begin_Page_Highlight:
3007 case CK_End_Page_Highlight:
3008 case CK_Word_Left:
3009 case CK_Word_Right:
3010 case CK_Up:
3011 case CK_Down:
3012 case CK_Left:
3013 case CK_Right:
3014 if (edit->mark2 >= 0)
3016 if (!option_persistent_selections)
3018 if (column_highlighting)
3019 edit_push_action (edit, COLUMN_ON);
3020 column_highlighting = 0;
3021 edit_mark_cmd (edit, 1);
3026 switch (command)
3028 case CK_Begin_Page:
3029 case CK_End_Page:
3030 case CK_Begin_Page_Highlight:
3031 case CK_End_Page_Highlight:
3032 case CK_Word_Left:
3033 case CK_Word_Right:
3034 case CK_Up:
3035 case CK_Down:
3036 case CK_Word_Left_Highlight:
3037 case CK_Word_Right_Highlight:
3038 case CK_Up_Highlight:
3039 case CK_Down_Highlight:
3040 case CK_Up_Alt_Highlight:
3041 case CK_Down_Alt_Highlight:
3042 if (edit->mark2 == -1)
3043 break; /*marking is following the cursor: may need to highlight a whole line */
3044 case CK_Left:
3045 case CK_Right:
3046 case CK_Left_Highlight:
3047 case CK_Right_Highlight:
3048 edit->force |= REDRAW_CHAR_ONLY;
3051 /* basic cursor key commands */
3052 switch (command)
3054 case CK_BackSpace:
3055 /* if non persistent selection and text selected */
3056 if (!option_persistent_selections)
3058 if (edit->mark1 != edit->mark2)
3060 edit_block_delete_cmd (edit);
3061 break;
3064 if (option_cursor_beyond_eol && edit->over_col > 0)
3066 edit->over_col--;
3067 break;
3069 if (option_backspace_through_tabs && is_in_indent (edit))
3071 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3072 edit_backspace (edit, 1);
3073 break;
3075 else
3077 if (option_fake_half_tabs)
3079 int i;
3080 if (is_in_indent (edit) && right_of_four_spaces (edit))
3082 for (i = 0; i < HALF_TAB_SIZE; i++)
3083 edit_backspace (edit, 1);
3084 break;
3088 edit_backspace (edit, 0);
3089 break;
3090 case CK_Delete:
3091 /* if non persistent selection and text selected */
3092 if (!option_persistent_selections)
3094 if (edit->mark1 != edit->mark2)
3096 edit_block_delete_cmd (edit);
3097 break;
3101 if (option_cursor_beyond_eol && edit->over_col > 0)
3102 edit_insert_over (edit);
3104 if (option_fake_half_tabs)
3106 int i;
3107 if (is_in_indent (edit) && left_of_four_spaces (edit))
3109 for (i = 1; i <= HALF_TAB_SIZE; i++)
3110 edit_delete (edit, 1);
3111 break;
3114 edit_delete (edit, 0);
3115 break;
3116 case CK_Delete_Word_Left:
3117 edit->over_col = 0;
3118 edit_left_delete_word (edit);
3119 break;
3120 case CK_Delete_Word_Right:
3121 if (option_cursor_beyond_eol && edit->over_col > 0)
3122 edit_insert_over (edit);
3124 edit_right_delete_word (edit);
3125 break;
3126 case CK_Delete_Line:
3127 edit_delete_line (edit);
3128 break;
3129 case CK_Delete_To_Line_End:
3130 edit_delete_to_line_end (edit);
3131 break;
3132 case CK_Delete_To_Line_Begin:
3133 edit_delete_to_line_begin (edit);
3134 break;
3135 case CK_Enter:
3136 edit->over_col = 0;
3137 if (option_auto_para_formatting)
3139 edit_double_newline (edit);
3140 if (option_return_does_auto_indent)
3141 edit_auto_indent (edit);
3142 format_paragraph (edit, 0);
3144 else
3146 edit_insert (edit, '\n');
3147 if (option_return_does_auto_indent)
3149 edit_auto_indent (edit);
3152 break;
3153 case CK_Return:
3154 edit_insert (edit, '\n');
3155 break;
3157 case CK_Page_Up_Alt_Highlight:
3158 column_highlighting = 1;
3159 case CK_Page_Up:
3160 case CK_Page_Up_Highlight:
3161 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3162 break;
3163 case CK_Page_Down_Alt_Highlight:
3164 column_highlighting = 1;
3165 case CK_Page_Down:
3166 case CK_Page_Down_Highlight:
3167 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3168 break;
3169 case CK_Left_Alt_Highlight:
3170 column_highlighting = 1;
3171 case CK_Left:
3172 case CK_Left_Highlight:
3173 if (option_fake_half_tabs)
3175 if (is_in_indent (edit) && right_of_four_spaces (edit))
3177 if (option_cursor_beyond_eol && edit->over_col > 0)
3178 edit->over_col--;
3179 else
3180 edit_cursor_move (edit, -HALF_TAB_SIZE);
3181 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3182 break;
3185 edit_left_char_move_cmd (edit);
3186 break;
3187 case CK_Right_Alt_Highlight:
3188 column_highlighting = 1;
3189 case CK_Right:
3190 case CK_Right_Highlight:
3191 if (option_fake_half_tabs)
3193 if (is_in_indent (edit) && left_of_four_spaces (edit))
3195 edit_cursor_move (edit, HALF_TAB_SIZE);
3196 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3197 break;
3200 edit_right_char_move_cmd (edit);
3201 break;
3202 case CK_Begin_Page:
3203 case CK_Begin_Page_Highlight:
3204 edit_begin_page (edit);
3205 break;
3206 case CK_End_Page:
3207 case CK_End_Page_Highlight:
3208 edit_end_page (edit);
3209 break;
3210 case CK_Word_Left:
3211 case CK_Word_Left_Highlight:
3212 edit->over_col = 0;
3213 edit_left_word_move_cmd (edit);
3214 break;
3215 case CK_Word_Right:
3216 case CK_Word_Right_Highlight:
3217 edit->over_col = 0;
3218 edit_right_word_move_cmd (edit);
3219 break;
3220 case CK_Up_Alt_Highlight:
3221 column_highlighting = 1;
3222 case CK_Up:
3223 case CK_Up_Highlight:
3224 edit_move_up (edit, 1, 0);
3225 break;
3226 case CK_Down_Alt_Highlight:
3227 column_highlighting = 1;
3228 case CK_Down:
3229 case CK_Down_Highlight:
3230 edit_move_down (edit, 1, 0);
3231 break;
3232 case CK_Paragraph_Up_Alt_Highlight:
3233 column_highlighting = 1;
3234 case CK_Paragraph_Up:
3235 case CK_Paragraph_Up_Highlight:
3236 edit_move_up_paragraph (edit, 0);
3237 break;
3238 case CK_Paragraph_Down_Alt_Highlight:
3239 column_highlighting = 1;
3240 case CK_Paragraph_Down:
3241 case CK_Paragraph_Down_Highlight:
3242 edit_move_down_paragraph (edit, 0);
3243 break;
3244 case CK_Scroll_Up_Alt_Highlight:
3245 column_highlighting = 1;
3246 case CK_Scroll_Up:
3247 case CK_Scroll_Up_Highlight:
3248 edit_move_up (edit, 1, 1);
3249 break;
3250 case CK_Scroll_Down_Alt_Highlight:
3251 column_highlighting = 1;
3252 case CK_Scroll_Down:
3253 case CK_Scroll_Down_Highlight:
3254 edit_move_down (edit, 1, 1);
3255 break;
3256 case CK_Home:
3257 case CK_Home_Highlight:
3258 edit_cursor_to_bol (edit);
3259 break;
3260 case CK_End:
3261 case CK_End_Highlight:
3262 edit_cursor_to_eol (edit);
3263 break;
3264 case CK_Tab:
3265 /* if text marked shift block */
3266 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3268 if (edit->mark2 < 0)
3269 edit_mark_cmd (edit, 0);
3270 edit_move_block_to_right (edit);
3272 else
3274 if (option_cursor_beyond_eol)
3275 edit_insert_over (edit);
3276 edit_tab_cmd (edit);
3277 if (option_auto_para_formatting)
3279 format_paragraph (edit, 0);
3280 edit->force |= REDRAW_PAGE;
3282 else
3284 check_and_wrap_line (edit);
3287 break;
3289 case CK_Toggle_Insert:
3290 edit->overwrite = (edit->overwrite == 0);
3291 break;
3293 case CK_Mark:
3294 if (edit->mark2 >= 0)
3296 if (column_highlighting)
3297 edit_push_action (edit, COLUMN_ON);
3298 column_highlighting = 0;
3300 edit_mark_cmd (edit, 0);
3301 break;
3302 case CK_Column_Mark:
3303 if (!column_highlighting)
3304 edit_push_action (edit, COLUMN_OFF);
3305 column_highlighting = 1;
3306 edit_mark_cmd (edit, 0);
3307 break;
3308 case CK_Mark_All:
3309 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3310 edit->force |= REDRAW_PAGE;
3311 break;
3312 case CK_Unmark:
3313 if (column_highlighting)
3314 edit_push_action (edit, COLUMN_ON);
3315 column_highlighting = 0;
3316 edit_mark_cmd (edit, 1);
3317 break;
3319 case CK_Toggle_Line_State:
3320 option_line_state = !option_line_state;
3321 if (option_line_state)
3323 option_line_state_width = LINE_STATE_WIDTH;
3325 else
3327 option_line_state_width = 0;
3329 edit->force |= REDRAW_PAGE;
3330 break;
3332 case CK_Toggle_Show_Margin:
3333 show_right_margin = !show_right_margin;
3334 edit->force |= REDRAW_PAGE;
3335 break;
3337 case CK_Toggle_Bookmark:
3338 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3339 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3340 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3341 else
3342 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3343 break;
3344 case CK_Flush_Bookmarks:
3345 book_mark_flush (edit, BOOK_MARK_COLOR);
3346 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3347 edit->force |= REDRAW_PAGE;
3348 break;
3349 case CK_Next_Bookmark:
3350 if (edit->book_mark)
3352 struct _book_mark *p;
3353 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3354 if (p->next)
3356 p = p->next;
3357 if (p->line >= edit->start_line + edit->num_widget_lines
3358 || p->line < edit->start_line)
3359 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3360 edit_move_to_line (edit, p->line);
3363 break;
3364 case CK_Prev_Bookmark:
3365 if (edit->book_mark)
3367 struct _book_mark *p;
3368 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3369 while (p->line == edit->curs_line)
3370 if (p->prev)
3371 p = p->prev;
3372 if (p->line >= 0)
3374 if (p->line >= edit->start_line + edit->num_widget_lines
3375 || p->line < edit->start_line)
3376 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3377 edit_move_to_line (edit, p->line);
3380 break;
3382 case CK_Beginning_Of_Text:
3383 case CK_Beginning_Of_Text_Highlight:
3384 edit_move_to_top (edit);
3385 break;
3386 case CK_End_Of_Text:
3387 case CK_End_Of_Text_Highlight:
3388 edit_move_to_bottom (edit);
3389 break;
3391 case CK_Copy:
3392 if (option_cursor_beyond_eol && edit->over_col > 0)
3393 edit_insert_over (edit);
3394 edit_block_copy_cmd (edit);
3395 break;
3396 case CK_Remove:
3397 edit_block_delete_cmd (edit);
3398 break;
3399 case CK_Move:
3400 if (option_cursor_beyond_eol && edit->over_col > 0)
3401 edit_insert_over (edit);
3402 edit_block_move_cmd (edit);
3403 break;
3405 case CK_Shift_Block_Left:
3406 if (edit->mark1 != edit->mark2)
3407 edit_move_block_to_left (edit);
3408 break;
3409 case CK_Shift_Block_Right:
3410 if (edit->mark1 != edit->mark2)
3411 edit_move_block_to_right (edit);
3412 break;
3413 case CK_XStore:
3414 edit_copy_to_X_buf_cmd (edit);
3415 break;
3416 case CK_XCut:
3417 edit_cut_to_X_buf_cmd (edit);
3418 break;
3419 case CK_XPaste:
3420 /* if non persistent selection and text selected */
3421 if (!option_persistent_selections)
3423 if (edit->mark1 != edit->mark2)
3424 edit_block_delete_cmd (edit);
3426 if (option_cursor_beyond_eol && edit->over_col > 0)
3427 edit_insert_over (edit);
3428 edit_paste_from_X_buf_cmd (edit);
3429 break;
3430 case CK_Selection_History:
3431 edit_paste_from_history (edit);
3432 break;
3434 case CK_Save_As:
3435 edit_save_as_cmd (edit);
3436 break;
3437 case CK_Save:
3438 edit_save_confirm_cmd (edit);
3439 break;
3440 case CK_Load:
3441 edit_load_cmd (edit, EDIT_FILE_COMMON);
3442 break;
3443 case CK_Save_Block:
3444 edit_save_block_cmd (edit);
3445 break;
3446 case CK_Insert_File:
3447 edit_insert_file_cmd (edit);
3448 break;
3450 case CK_Load_Prev_File:
3451 edit_load_back_cmd (edit);
3452 break;
3453 case CK_Load_Next_File:
3454 edit_load_forward_cmd (edit);
3455 break;
3457 case CK_Load_Syntax_File:
3458 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3459 break;
3460 case CK_Choose_Syntax:
3461 edit_syntax_dialog (edit, edit->syntax_type);
3462 break;
3464 case CK_Load_Menu_File:
3465 edit_load_cmd (edit, EDIT_FILE_MENU);
3466 break;
3468 case CK_Toggle_Syntax:
3469 if ((option_syntax_highlighting ^= 1) == 1)
3470 edit_load_syntax (edit, NULL, edit->syntax_type);
3471 edit->force |= REDRAW_PAGE;
3472 break;
3474 case CK_Toggle_Tab_TWS:
3475 enable_show_tabs_tws ^= 1;
3476 edit->force |= REDRAW_PAGE;
3477 break;
3479 case CK_Find:
3480 edit_search_cmd (edit, 0);
3481 break;
3482 case CK_Find_Again:
3483 edit_search_cmd (edit, 1);
3484 break;
3485 case CK_Replace:
3486 edit_replace_cmd (edit, 0);
3487 break;
3488 case CK_Replace_Again:
3489 edit_replace_cmd (edit, 1);
3490 break;
3491 case CK_Complete_Word:
3492 /* if text marked shift block */
3493 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3495 edit_move_block_to_left (edit);
3497 else
3499 edit_complete_word_cmd (edit);
3501 break;
3502 case CK_Find_Definition:
3503 edit_get_match_keyword_cmd (edit);
3504 break;
3505 case CK_Quit:
3506 dlg_stop (edit->widget.parent);
3507 break;
3508 case CK_New:
3509 edit_new_cmd (edit);
3510 break;
3511 case CK_Help:
3512 edit_help_cmd (edit);
3513 break;
3514 case CK_Refresh:
3515 edit_refresh_cmd (edit);
3516 break;
3517 case CK_SaveSetupCmd:
3518 save_setup_cmd ();
3519 break;
3520 case CK_About:
3521 query_dialog (_(" About "),
3522 _("\n Cooledit v3.11.5\n\n"
3523 " Copyright (C) 1996 the Free Software Foundation\n\n"
3524 " A user friendly text editor written\n"
3525 " for the Midnight Commander.\n"), D_NORMAL, 1, _("&OK"));
3526 break;
3527 case CK_LearnKeys:
3528 learn_keys ();
3529 break;
3530 case CK_Edit_Options:
3531 edit_options_dialog (edit);
3532 break;
3533 case CK_Edit_Save_Mode:
3534 menu_save_mode_cmd ();
3535 break;
3536 case CK_Date:
3538 char s[BUF_MEDIUM];
3539 /* fool gcc to prevent a Y2K warning */
3540 char time_format[] = "_c";
3541 time_format[0] = '%';
3543 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3544 edit_print_string (edit, s);
3545 edit->force |= REDRAW_PAGE;
3546 break;
3548 break;
3549 case CK_Goto:
3550 edit_goto_cmd (edit);
3551 break;
3552 case CK_Paragraph_Format:
3553 format_paragraph (edit, 1);
3554 edit->force |= REDRAW_PAGE;
3555 break;
3556 case CK_Delete_Macro:
3557 edit_delete_macro_cmd (edit);
3558 break;
3559 case CK_Match_Bracket:
3560 edit_goto_matching_bracket (edit);
3561 break;
3562 case CK_User_Menu:
3563 user_menu (edit);
3564 break;
3565 case CK_Sort:
3566 edit_sort_cmd (edit);
3567 break;
3568 case CK_ExtCmd:
3569 edit_ext_cmd (edit);
3570 break;
3571 case CK_Mail:
3572 edit_mail_dialog (edit);
3573 break;
3574 case CK_Shell:
3575 view_other_cmd ();
3576 break;
3577 case CK_SelectCodepage:
3578 edit_select_codepage_cmd (edit);
3579 break;
3580 case CK_Insert_Literal:
3581 edit_insert_literal_cmd (edit);
3582 break;
3583 case CK_Execute_Macro:
3584 edit_execute_macro_cmd (edit);
3585 break;
3586 case CK_Begin_End_Macro:
3587 edit_begin_end_macro_cmd (edit);
3588 break;
3589 case CK_Ext_Mode:
3590 edit->extmod = 1;
3591 break;
3592 default:
3593 break;
3596 /* CK_Pipe_Block */
3597 if ((command / 1000) == 1) /* a shell command */
3598 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3599 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3600 { /* a macro command */
3601 struct macro m[MAX_MACRO_LENGTH];
3602 int nm;
3603 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3604 edit_execute_macro (edit, m, nm);
3607 /* keys which must set the col position, and the search vars */
3608 switch (command)
3610 case CK_Find:
3611 case CK_Find_Again:
3612 case CK_Replace:
3613 case CK_Replace_Again:
3614 case CK_Complete_Word:
3615 edit->prev_col = edit_get_col (edit);
3616 break;
3617 case CK_Up:
3618 case CK_Up_Highlight:
3619 case CK_Up_Alt_Highlight:
3620 case CK_Down:
3621 case CK_Down_Highlight:
3622 case CK_Down_Alt_Highlight:
3623 case CK_Page_Up:
3624 case CK_Page_Up_Highlight:
3625 case CK_Page_Up_Alt_Highlight:
3626 case CK_Page_Down:
3627 case CK_Page_Down_Highlight:
3628 case CK_Page_Down_Alt_Highlight:
3629 case CK_Beginning_Of_Text:
3630 case CK_Beginning_Of_Text_Highlight:
3631 case CK_End_Of_Text:
3632 case CK_End_Of_Text_Highlight:
3633 case CK_Paragraph_Up:
3634 case CK_Paragraph_Up_Highlight:
3635 case CK_Paragraph_Up_Alt_Highlight:
3636 case CK_Paragraph_Down:
3637 case CK_Paragraph_Down_Highlight:
3638 case CK_Paragraph_Down_Alt_Highlight:
3639 case CK_Scroll_Up:
3640 case CK_Scroll_Up_Highlight:
3641 case CK_Scroll_Up_Alt_Highlight:
3642 case CK_Scroll_Down:
3643 case CK_Scroll_Down_Highlight:
3644 case CK_Scroll_Down_Alt_Highlight:
3645 edit->search_start = edit->curs1;
3646 edit->found_len = 0;
3647 break;
3648 default:
3649 edit->found_len = 0;
3650 edit->prev_col = edit_get_col (edit);
3651 edit->search_start = edit->curs1;
3653 edit_find_bracket (edit);
3655 if (option_auto_para_formatting)
3657 switch (command)
3659 case CK_BackSpace:
3660 case CK_Delete:
3661 case CK_Delete_Word_Left:
3662 case CK_Delete_Word_Right:
3663 case CK_Delete_To_Line_End:
3664 case CK_Delete_To_Line_Begin:
3665 format_paragraph (edit, 0);
3666 edit->force |= REDRAW_PAGE;
3672 static void
3673 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
3675 int i = 0;
3677 if (edit->macro_depth++ > 256)
3679 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3680 edit->macro_depth--;
3681 return;
3683 edit->force |= REDRAW_PAGE;
3684 for (; i < n; i++)
3686 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3688 edit_update_screen (edit);
3689 edit->macro_depth--;
3692 /* User edit menu, like user menu (F2) but only in editor. */
3693 static void
3694 user_menu (WEdit * edit)
3696 char *block_file;
3697 int nomark;
3698 long start_mark, end_mark;
3699 struct stat status;
3701 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3703 nomark = eval_marks (edit, &start_mark, &end_mark);
3704 if (nomark == 0)
3705 edit_save_block (edit, block_file, start_mark, end_mark);
3707 /* run shell scripts from menu */
3708 user_menu_cmd (edit);
3710 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
3712 int rc = 0;
3713 FILE *fd;
3715 if (nomark == 0)
3717 /* i.e. we have marked block */
3718 rc = edit_block_delete_cmd (edit);
3721 if (rc == 0)
3722 edit_insert_file (edit, block_file);
3724 /* truncate block file */
3725 fd = fopen (block_file, "w");
3726 if (fd != NULL)
3727 fclose (fd);
3729 edit_refresh_cmd (edit);
3730 edit->force |= REDRAW_COMPLETELY;
3732 g_free (block_file);
3735 void
3736 edit_stack_init (void)
3738 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3740 edit_history_moveto[edit_stack_iterator].filename = NULL;
3741 edit_history_moveto[edit_stack_iterator].line = -1;
3744 edit_stack_iterator = 0;
3747 void
3748 edit_stack_free (void)
3750 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3751 g_free (edit_history_moveto[edit_stack_iterator].filename);
3754 /* move i lines */
3755 void
3756 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3758 edit_move_updown (edit, i, do_scroll, TRUE);
3761 /* move i lines */
3762 void
3763 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3765 edit_move_updown (edit, i, do_scroll, FALSE);