Changes into src/editor directory:
[midnight-commander.git] / src / editor / edit.c
blob5d956ef0f0e0f934c14b2f20742da456abf7c4fd
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 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
337 GString *errmsg = g_string_new (NULL);
338 g_string_sprintf (errmsg, _(" Cannot open %s for reading "), filename);
339 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
340 g_string_free (errmsg, TRUE);
341 return 1;
344 if (!edit->buffers2[buf2])
345 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
349 if (mc_read (file,
350 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
351 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
352 break;
354 for (buf = buf2 - 1; buf >= 0; buf--)
356 /* edit->buffers2[0] is already allocated */
357 if (!edit->buffers2[buf])
358 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
359 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
360 break;
362 ret = 0;
364 while (0);
365 if (ret) {
366 char *err_str = g_strdup_printf(_(" Error reading %s "), filename);
367 edit_error_dialog (_("Error"), err_str);
368 g_free(err_str);
370 mc_close (file);
371 return ret;
374 /* detecting an error on save is easy: just check if every byte has been written. */
375 /* detecting an error on read, is not so easy 'cos there is not way to tell
376 whether you read everything or not. */
377 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
378 static const struct edit_filters
380 const char *read, *write, *extension;
381 } all_filters[] =
383 /* *INDENT-OFF* */
384 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
385 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
386 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
387 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
388 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
389 /* *INDENT-ON* */
392 /* Return index of the filter or -1 is there is no appropriate filter */
393 static int
394 edit_find_filter (const char *filename)
396 size_t i, l, e;
398 if (filename == NULL)
399 return -1;
401 l = strlen (filename);
402 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
404 e = strlen (all_filters[i].extension);
405 if (l > e)
406 if (!strcmp (all_filters[i].extension, filename + l - e))
407 return i;
409 return -1;
412 static char *
413 edit_get_filter (const char *filename)
415 int i;
416 char *p, *quoted_name;
418 i = edit_find_filter (filename);
419 if (i < 0)
420 return NULL;
422 quoted_name = name_quote (filename, 0);
423 p = g_strdup_printf (all_filters[i].read, quoted_name);
424 g_free (quoted_name);
425 return p;
428 char *
429 edit_get_write_filter (const char *write_name, const char *filename)
431 int i;
432 char *p, *writename;
434 i = edit_find_filter (filename);
435 if (i < 0)
436 return NULL;
438 writename = name_quote (write_name, 0);
439 p = g_strdup_printf (all_filters[i].write, writename);
440 g_free (writename);
441 return p;
444 static long
445 edit_insert_stream (WEdit * edit, FILE * f)
447 int c;
448 long i = 0;
449 while ((c = fgetc (f)) >= 0)
451 edit_insert (edit, c);
452 i++;
454 return i;
457 long
458 edit_write_stream (WEdit * edit, FILE * f)
460 long i;
462 if (edit->lb == LB_ASIS)
464 for (i = 0; i < edit->last_byte; i++)
465 if (fputc (edit_get_byte (edit, i), f) < 0)
466 break;
467 return i;
470 /* change line breaks */
471 for (i = 0; i < edit->last_byte; i++)
473 unsigned char c = edit_get_byte (edit, i);
475 if (!(c == '\n' || c == '\r'))
477 /* not line break */
478 if (fputc (c, f) < 0)
479 return i;
481 else
482 { /* (c == '\n' || c == '\r') */
483 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
485 switch (edit->lb)
487 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
488 /* put one line break unconditionally */
489 if (fputc ('\n', f) < 0)
490 return i;
492 i++; /* 2 chars are processed */
494 if (c == '\r' && c1 == '\n')
495 /* Windows line break; go to the next char */
496 break;
498 if (c == '\r' && c1 == '\r')
500 /* two Macintosh line breaks; put second line break */
501 if (fputc ('\n', f) < 0)
502 return i;
503 break;
506 if (fputc (c1, f) < 0)
507 return i;
508 break;
510 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
511 /* put one line break unconditionally */
512 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
513 return i;
515 if (c == '\r' && c1 == '\n')
516 /* Windows line break; go to the next char */
517 i++;
518 break;
520 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
521 /* put one line break unconditionally */
522 if (fputc ('\r', f) < 0)
523 return i;
525 i++; /* 2 chars are processed */
527 if (c == '\r' && c1 == '\n')
528 /* Windows line break; go to the next char */
529 break;
531 if (c == '\n' && c1 == '\n')
533 /* two Windows line breaks; put second line break */
534 if (fputc ('\r', f) < 0)
535 return i;
536 break;
539 if (fputc (c1, f) < 0)
540 return i;
541 break;
542 case LB_ASIS: /* default without changes */
543 break;
548 return edit->last_byte;
551 #define TEMP_BUF_LEN 1024
553 /* inserts a file at the cursor, returns 1 on success */
555 edit_insert_file (WEdit * edit, const char *filename)
557 char *p;
559 p = edit_get_filter (filename);
560 if (p != NULL)
562 FILE *f;
563 long current = edit->curs1;
564 f = (FILE *) popen (p, "r");
565 if (f != NULL)
567 edit_insert_stream (edit, f);
568 edit_cursor_move (edit, current - edit->curs1);
569 if (pclose (f) > 0)
571 char *errmsg;
572 errmsg = g_strdup_printf (_(" Error reading from pipe: %s "), p);
573 edit_error_dialog (_("Error"), errmsg);
574 g_free (errmsg);
575 g_free (p);
576 return 0;
579 else
581 char *errmsg;
582 errmsg = g_strdup_printf (_(" Cannot open pipe for reading: %s "), p);
583 edit_error_dialog (_("Error"), errmsg);
584 g_free (errmsg);
585 g_free (p);
586 return 0;
588 g_free (p);
590 else
592 int i, file, blocklen;
593 long current = edit->curs1;
594 int vertical_insertion = 0;
595 char *buf;
596 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
597 return 0;
598 buf = g_malloc0 (TEMP_BUF_LEN);
599 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
600 if (blocklen > 0)
602 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
603 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
605 vertical_insertion = 1;
607 else
609 mc_lseek (file, 0, SEEK_SET);
612 if (vertical_insertion)
614 blocklen = edit_insert_column_of_text_from_file (edit, file);
616 else
618 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
620 for (i = 0; i < blocklen; i++)
621 edit_insert (edit, buf[i]);
624 edit_cursor_move (edit, current - edit->curs1);
625 g_free (buf);
626 mc_close (file);
627 if (blocklen)
628 return 0;
630 return 1;
633 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
634 static int
635 check_file_access (WEdit * edit, const char *filename, struct stat *st)
637 int file;
638 GString *errmsg = (GString *) 0;
640 /* Try opening an existing file */
641 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
643 if (file < 0)
646 * Try creating the file. O_EXCL prevents following broken links
647 * and opening existing files.
649 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
650 if (file < 0)
652 g_string_sprintf (errmsg = g_string_new (NULL),
653 _(" Cannot open %s for reading "), filename);
654 goto cleanup;
656 else
658 /* New file, delete it if it's not modified or saved */
659 edit->delete_file = 1;
663 /* Check what we have opened */
664 if (mc_fstat (file, st) < 0)
666 g_string_sprintf (errmsg = g_string_new (NULL),
667 _(" Cannot get size/permissions for %s "), filename);
668 goto cleanup;
671 /* We want to open regular files only */
672 if (!S_ISREG (st->st_mode))
674 g_string_sprintf (errmsg = g_string_new (NULL), _(" %s is not a regular file "), filename);
675 goto cleanup;
679 * Don't delete non-empty files.
680 * O_EXCL should prevent it, but let's be on the safe side.
682 if (st->st_size > 0)
684 edit->delete_file = 0;
687 if (st->st_size >= SIZE_LIMIT)
689 g_string_sprintf (errmsg = g_string_new (NULL), _(" File %s is too large "), filename);
692 cleanup:
693 (void) mc_close (file);
694 if (errmsg)
696 edit_error_dialog (_("Error"), errmsg->str);
697 g_string_free (errmsg, TRUE);
698 return 1;
700 return 0;
704 * Open the file and load it into the buffers, either directly or using
705 * a filter. Return 0 on success, 1 on error.
707 * Fast loading (edit_load_file_fast) is used when the file size is
708 * known. In this case the data is read into the buffers by blocks.
709 * If the file size is not known, the data is loaded byte by byte in
710 * edit_insert_file.
712 static int
713 edit_load_file (WEdit * edit)
715 int fast_load = 1;
717 /* Cannot do fast load if a filter is used */
718 if (edit_find_filter (edit->filename) >= 0)
719 fast_load = 0;
722 * VFS may report file size incorrectly, and slow load is not a big
723 * deal considering overhead in VFS.
725 if (!vfs_file_is_local (edit->filename))
726 fast_load = 0;
729 * FIXME: line end translation should disable fast loading as well
730 * Consider doing fseek() to the end and ftell() for the real size.
733 if (*edit->filename)
735 /* If we are dealing with a real file, check that it exists */
736 if (check_file_access (edit, edit->filename, &edit->stat1))
737 return 1;
739 else
741 /* nothing to load */
742 fast_load = 0;
745 edit_init_buffers (edit);
747 if (fast_load)
749 edit->last_byte = edit->stat1.st_size;
750 edit_load_file_fast (edit, edit->filename);
751 /* If fast load was used, the number of lines wasn't calculated */
752 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
754 else
756 #ifdef HAVE_CHARSET
757 const char *codepage_id;
758 #endif
759 edit->last_byte = 0;
760 if (*edit->filename)
762 edit->stack_disable = 1;
763 if (!edit_insert_file (edit, edit->filename))
765 edit_clean (edit);
766 return 1;
768 edit->stack_disable = 0;
771 #ifdef HAVE_CHARSET
772 codepage_id = get_codepage_id (source_codepage);
773 if (codepage_id)
774 edit->utf8 = str_isutf8 (codepage_id);
775 #endif
777 edit->lb = LB_ASIS;
778 return 0;
781 /* Restore saved cursor position in the file */
782 static void
783 edit_load_position (WEdit * edit)
785 char *filename;
786 long line, column;
787 off_t offset;
789 if (!edit->filename || !*edit->filename)
790 return;
792 filename = vfs_canon (edit->filename);
793 load_file_position (filename, &line, &column, &offset);
794 g_free (filename);
796 if (line > 0)
798 edit_move_to_line (edit, line - 1);
799 edit->prev_col = column;
801 else if (offset > 0)
803 edit_cursor_move (edit, offset);
804 line = edit->curs_line;
806 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
807 edit_move_display (edit, line - (edit->num_widget_lines / 2));
810 /* Save cursor position in the file */
811 static void
812 edit_save_position (WEdit * edit)
814 char *filename;
816 if (!edit->filename || !*edit->filename)
817 return;
819 filename = vfs_canon (edit->filename);
820 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
821 g_free (filename);
824 /* Clean the WEdit stricture except the widget part */
825 static void
826 edit_purge_widget (WEdit * edit)
828 size_t len = sizeof (WEdit) - sizeof (Widget);
829 char *start = (char *) edit + sizeof (Widget);
830 memset (start, 0, len);
831 edit->macro_i = -1; /* not recording a macro */
834 static void
835 edit_set_keymap (void)
837 editor_map = default_editor_keymap;
838 if (editor_keymap && editor_keymap->len > 0)
839 editor_map = (global_keymap_t *) editor_keymap->data;
841 editor_x_map = default_editor_x_keymap;
842 if (editor_x_keymap && editor_x_keymap->len > 0)
843 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
847 #define space_width 1
850 * Fill in the edit structure. Return NULL on failure. Pass edit as
851 * NULL to allocate a new structure.
853 * If line is 0, try to restore saved position. Otherwise put the
854 * cursor on that line and show it in the middle of the screen.
856 WEdit *
857 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
859 int to_free = 0;
860 option_auto_syntax = 1; /* Resetting to auto on every invokation */
861 if (option_line_state)
863 option_line_state_width = LINE_STATE_WIDTH;
865 else
867 option_line_state_width = 0;
869 if (!edit)
871 #ifdef ENABLE_NLS
873 * Expand option_whole_chars_search by national letters using
874 * current locale
877 static char option_whole_chars_search_buf[256];
879 if (option_whole_chars_search_buf != option_whole_chars_search)
881 size_t i;
882 size_t len = str_term_width1 (option_whole_chars_search);
884 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
886 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
888 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
890 option_whole_chars_search_buf[len++] = i;
894 option_whole_chars_search_buf[len] = 0;
895 option_whole_chars_search = option_whole_chars_search_buf;
897 #endif /* ENABLE_NLS */
898 edit = g_malloc0 (sizeof (WEdit));
899 edit->search = NULL;
900 to_free = 1;
902 edit_purge_widget (edit);
903 edit->num_widget_lines = lines;
904 edit->over_col = 0;
905 edit->num_widget_columns = columns;
906 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
907 edit->stat1.st_uid = getuid ();
908 edit->stat1.st_gid = getgid ();
909 edit->stat1.st_mtime = 0;
910 edit->bracket = -1;
911 edit->force |= REDRAW_PAGE;
912 edit_set_filename (edit, filename);
913 edit->stack_size = START_STACK_SIZE;
914 edit->stack_size_mask = START_STACK_SIZE - 1;
915 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
916 if (edit_load_file (edit))
918 /* edit_load_file already gives an error message */
919 if (to_free)
920 g_free (edit);
921 return 0;
923 edit->utf8 = 0;
924 edit->converter = str_cnv_from_term;
925 #ifdef HAVE_CHARSET
927 const char *cp_id = NULL;
928 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
930 if (cp_id != NULL)
932 GIConv conv;
933 conv = str_crt_conv_from (cp_id);
934 if (conv != INVALID_CONV)
936 if (edit->converter != str_cnv_from_term)
937 str_close_conv (edit->converter);
938 edit->converter = conv;
941 if (cp_id != NULL)
942 edit->utf8 = str_isutf8 (cp_id);
944 #endif
946 edit->loading_done = 1;
947 edit->modified = 0;
948 edit->locked = 0;
949 edit_load_syntax (edit, NULL, NULL);
951 int color;
952 edit_get_syntax_color (edit, -1, &color);
955 /* load saved cursor position */
956 if ((line == 0) && option_save_position)
958 edit_load_position (edit);
960 else
962 if (line <= 0)
963 line = 1;
964 edit_move_display (edit, line - 1);
965 edit_move_to_line (edit, line - 1);
968 edit_set_keymap ();
970 return edit;
973 /* Clear the edit struct, freeing everything in it. Return 1 on success */
975 edit_clean (WEdit * edit)
977 int j = 0;
979 if (!edit)
980 return 0;
982 /* a stale lock, remove it */
983 if (edit->locked)
984 edit->locked = edit_unlock_file (edit->filename);
986 /* save cursor position */
987 if (option_save_position)
988 edit_save_position (edit);
990 /* File specified on the mcedit command line and never saved */
991 if (edit->delete_file)
992 unlink (edit->filename);
994 edit_free_syntax_rules (edit);
995 book_mark_flush (edit, -1);
996 for (; j <= MAXBUFF; j++)
998 g_free (edit->buffers1[j]);
999 g_free (edit->buffers2[j]);
1002 g_free (edit->undo_stack);
1003 g_free (edit->filename);
1004 g_free (edit->dir);
1006 mc_search_free (edit->search);
1007 edit->search = NULL;
1009 if (edit->converter != str_cnv_from_term)
1010 str_close_conv (edit->converter);
1012 edit_purge_widget (edit);
1014 return 1;
1017 /* returns 1 on success */
1019 edit_renew (WEdit * edit)
1021 int lines = edit->num_widget_lines;
1022 int columns = edit->num_widget_columns;
1024 edit_clean (edit);
1025 return (edit_init (edit, lines, columns, "", 0) != NULL);
1029 * Load a new file into the editor. If it fails, preserve the old file.
1030 * To do it, allocate a new widget, initialize it and, if the new file
1031 * was loaded, copy the data to the old widget.
1032 * Return 1 on success, 0 on failure.
1035 edit_reload (WEdit * edit, const char *filename)
1037 WEdit *e;
1038 int lines = edit->num_widget_lines;
1039 int columns = edit->num_widget_columns;
1041 e = g_malloc0 (sizeof (WEdit));
1042 e->widget = edit->widget;
1043 if (!edit_init (e, lines, columns, filename, 0))
1045 g_free (e);
1046 return 0;
1048 edit_clean (edit);
1049 memcpy (edit, e, sizeof (WEdit));
1050 g_free (e);
1051 return 1;
1055 * Load a new file into the editor and set line. If it fails, preserve the old file.
1056 * To do it, allocate a new widget, initialize it and, if the new file
1057 * was loaded, copy the data to the old widget.
1058 * Return 1 on success, 0 on failure.
1061 edit_reload_line (WEdit * edit, const char *filename, long line)
1063 WEdit *e;
1064 int lines = edit->num_widget_lines;
1065 int columns = edit->num_widget_columns;
1067 e = g_malloc0 (sizeof (WEdit));
1068 e->widget = edit->widget;
1069 if (!edit_init (e, lines, columns, filename, line))
1071 g_free (e);
1072 return 0;
1074 edit_clean (edit);
1075 memcpy (edit, e, sizeof (WEdit));
1076 g_free (e);
1077 return 1;
1082 Recording stack for undo:
1083 The following is an implementation of a compressed stack. Identical
1084 pushes are recorded by a negative prefix indicating the number of times the
1085 same char was pushed. This saves space for repeated curs-left or curs-right
1086 delete etc.
1090 pushed: stored:
1094 b -3
1096 c --> -4
1102 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1103 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1104 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1105 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1106 position.
1108 The only way the cursor moves or the buffer is changed is through the routines:
1109 insert, backspace, insert_ahead, delete, and cursor_move.
1110 These record the reverse undo movements onto the stack each time they are
1111 called.
1113 Each key press results in a set of actions (insert; delete ...). So each time
1114 a key is pressed the current position of start_display is pushed as
1115 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1116 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1117 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1121 void
1122 edit_push_action (WEdit * edit, long c, ...)
1124 unsigned long sp = edit->stack_pointer;
1125 unsigned long spm1;
1126 long *t;
1128 /* first enlarge the stack if necessary */
1129 if (sp > edit->stack_size - 10)
1130 { /* say */
1131 if (option_max_undo < 256)
1132 option_max_undo = 256;
1133 if (edit->stack_size < (unsigned long) option_max_undo)
1135 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1136 if (t)
1138 edit->undo_stack = t;
1139 edit->stack_size <<= 1;
1140 edit->stack_size_mask = edit->stack_size - 1;
1144 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1145 if (edit->stack_disable)
1146 return;
1148 #ifdef FAST_MOVE_CURSOR
1149 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
1151 va_list ap;
1152 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1153 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1154 va_start (ap, c);
1155 c = -(va_arg (ap, int));
1156 va_end (ap);
1158 else
1159 #endif /* ! FAST_MOVE_CURSOR */
1160 if (edit->stack_bottom != sp
1161 && spm1 != edit->stack_bottom
1162 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
1164 int d;
1165 if (edit->undo_stack[spm1] < 0)
1167 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1168 if (d == c)
1170 if (edit->undo_stack[spm1] > -1000000000)
1172 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1173 edit->undo_stack[spm1]--;
1174 return;
1177 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1178 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1179 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1180 { /* a left then a right anihilate each other */
1181 if (edit->undo_stack[spm1] == -2)
1182 edit->stack_pointer = spm1;
1183 else
1184 edit->undo_stack[spm1]++;
1185 return;
1187 #endif
1189 else
1191 d = edit->undo_stack[spm1];
1192 if (d == c)
1194 if (c >= KEY_PRESS)
1195 return; /* --> no need to push multiple do-nothings */
1196 edit->undo_stack[sp] = -2;
1197 goto check_bottom;
1199 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1200 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1201 { /* a left then a right anihilate each other */
1202 edit->stack_pointer = spm1;
1203 return;
1205 #endif
1208 edit->undo_stack[sp] = c;
1210 check_bottom:
1211 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1213 /* if the sp wraps round and catches the stack_bottom then erase
1214 * the first set of actions on the stack to make space - by moving
1215 * stack_bottom forward one "key press" */
1216 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1217 if ((unsigned long) c == edit->stack_bottom ||
1218 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1221 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1223 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
1224 && edit->stack_bottom != edit->stack_pointer);
1226 /*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: */
1227 if (edit->stack_pointer != edit->stack_bottom
1228 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1229 edit->stack_bottom = edit->stack_pointer = 0;
1233 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1234 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1236 static long
1237 pop_action (WEdit * edit)
1239 long c;
1240 unsigned long sp = edit->stack_pointer;
1241 if (sp == edit->stack_bottom)
1243 return STACK_BOTTOM;
1245 sp = (sp - 1) & edit->stack_size_mask;
1246 if ((c = edit->undo_stack[sp]) >= 0)
1248 /* edit->undo_stack[sp] = '@'; */
1249 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1250 return c;
1252 if (sp == edit->stack_bottom)
1254 return STACK_BOTTOM;
1256 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1257 if (edit->undo_stack[sp] == -2)
1259 /* edit->undo_stack[sp] = '@'; */
1260 edit->stack_pointer = sp;
1262 else
1263 edit->undo_stack[sp]++;
1265 return c;
1268 /* is called whenever a modification is made by one of the four routines below */
1269 static void
1270 edit_modification (WEdit * edit)
1272 edit->caches_valid = 0;
1273 edit->screen_modified = 1;
1275 /* raise lock when file modified */
1276 if (!edit->modified && !edit->delete_file)
1277 edit->locked = edit_lock_file (edit->filename);
1278 edit->modified = 1;
1282 Basic low level single character buffer alterations and movements at the cursor.
1283 Returns char passed over, inserted or removed.
1286 void
1287 edit_insert (WEdit * edit, int c)
1289 /* check if file has grown to large */
1290 if (edit->last_byte >= SIZE_LIMIT)
1291 return;
1293 /* first we must update the position of the display window */
1294 if (edit->curs1 < edit->start_display)
1296 edit->start_display++;
1297 if (c == '\n')
1298 edit->start_line++;
1301 /* Mark file as modified, unless the file hasn't been fully loaded */
1302 if (edit->loading_done)
1304 edit_modification (edit);
1307 /* now we must update some info on the file and check if a redraw is required */
1308 if (c == '\n')
1310 if (edit->book_mark)
1311 book_mark_inc (edit, edit->curs_line);
1312 edit->curs_line++;
1313 edit->total_lines++;
1314 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1317 /* save the reverse command onto the undo stack */
1318 edit_push_action (edit, BACKSPACE);
1320 /* update markers */
1321 edit->mark1 += (edit->mark1 > edit->curs1);
1322 edit->mark2 += (edit->mark2 > edit->curs1);
1323 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1325 /* add a new buffer if we've reached the end of the last one */
1326 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1327 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1329 /* perform the insertion */
1330 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
1331 = (unsigned char) c;
1333 /* update file length */
1334 edit->last_byte++;
1336 /* update cursor position */
1337 edit->curs1++;
1340 static void
1341 edit_insert_over (WEdit * edit)
1343 int i;
1345 for (i = 0; i < edit->over_col; i++)
1347 edit_insert (edit, ' ');
1349 edit->over_col = 0;
1352 /* same as edit_insert and move left */
1353 void
1354 edit_insert_ahead (WEdit * edit, int c)
1356 if (edit->last_byte >= SIZE_LIMIT)
1357 return;
1358 if (edit->curs1 < edit->start_display)
1360 edit->start_display++;
1361 if (c == '\n')
1362 edit->start_line++;
1364 edit_modification (edit);
1365 if (c == '\n')
1367 if (edit->book_mark)
1368 book_mark_inc (edit, edit->curs_line);
1369 edit->total_lines++;
1370 edit->force |= REDRAW_AFTER_CURSOR;
1372 edit_push_action (edit, DELCHAR);
1374 edit->mark1 += (edit->mark1 >= edit->curs1);
1375 edit->mark2 += (edit->mark2 >= edit->curs1);
1376 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1378 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1379 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1380 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) -
1381 1] = c;
1383 edit->last_byte++;
1384 edit->curs2++;
1389 edit_delete (WEdit * edit, const int byte_delete)
1391 int p = 0;
1392 int cw = 1;
1393 int i;
1395 if (!edit->curs2)
1396 return 0;
1398 edit->mark1 -= (edit->mark1 > edit->curs1);
1399 edit->mark2 -= (edit->mark2 > edit->curs1);
1400 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1402 cw = 1;
1403 /* if byte_delete = 1 then delete only one byte not multibyte char */
1404 if (edit->utf8 && byte_delete == 0)
1406 edit_get_utf (edit, edit->curs1, &cw);
1407 if (cw < 1)
1408 cw = 1;
1410 for (i = 1; i <= cw; i++)
1412 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1413 ((edit->curs2 -
1414 1) & M_EDIT_BUF_SIZE) - 1];
1416 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1418 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1419 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1421 edit->last_byte--;
1422 edit->curs2--;
1423 edit_push_action (edit, p + 256);
1426 edit_modification (edit);
1427 if (p == '\n')
1429 if (edit->book_mark)
1430 book_mark_dec (edit, edit->curs_line);
1431 edit->total_lines--;
1432 edit->force |= REDRAW_AFTER_CURSOR;
1434 if (edit->curs1 < edit->start_display)
1436 edit->start_display--;
1437 if (p == '\n')
1438 edit->start_line--;
1441 return p;
1445 static int
1446 edit_backspace (WEdit * edit, const int byte_delete)
1448 int p = 0;
1449 int cw = 1;
1450 int i;
1452 if (!edit->curs1)
1453 return 0;
1455 edit->mark1 -= (edit->mark1 >= edit->curs1);
1456 edit->mark2 -= (edit->mark2 >= edit->curs1);
1457 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1459 cw = 1;
1460 if (edit->utf8 && byte_delete == 0)
1462 edit_get_prev_utf (edit, edit->curs1, &cw);
1463 if (cw < 1)
1464 cw = 1;
1466 for (i = 1; i <= cw; i++)
1468 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
1469 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1470 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1472 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1473 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1475 edit->last_byte--;
1476 edit->curs1--;
1477 edit_push_action (edit, p);
1479 edit_modification (edit);
1480 if (p == '\n')
1482 if (edit->book_mark)
1483 book_mark_dec (edit, edit->curs_line);
1484 edit->curs_line--;
1485 edit->total_lines--;
1486 edit->force |= REDRAW_AFTER_CURSOR;
1489 if (edit->curs1 < edit->start_display)
1491 edit->start_display--;
1492 if (p == '\n')
1493 edit->start_line--;
1496 return p;
1499 #ifdef FAST_MOVE_CURSOR
1501 static void
1502 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1504 unsigned long next;
1505 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
1507 edit->curs_line--;
1508 next -= (unsigned long) dest;
1509 n -= next;
1510 src += next;
1511 dest += next;
1516 edit_move_backward_lots (WEdit * edit, long increment)
1518 int r, s, t;
1519 unsigned char *p = NULL;
1521 if (increment > edit->curs1)
1522 increment = edit->curs1;
1523 if (increment <= 0)
1524 return -1;
1525 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1527 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1528 if (r > increment)
1529 r = increment;
1530 s = edit->curs1 & M_EDIT_BUF_SIZE;
1532 if (s > r)
1534 memqcpy (edit,
1535 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1536 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
1538 else
1540 if (s)
1542 memqcpy (edit,
1543 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1544 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1545 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1546 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1548 memqcpy (edit,
1549 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1550 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1551 EDIT_BUF_SIZE - (r - s), r - s);
1553 increment -= r;
1554 edit->curs1 -= r;
1555 edit->curs2 += r;
1556 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1558 if (p)
1559 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1560 else
1561 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1563 else
1565 g_free (p);
1568 s = edit->curs1 & M_EDIT_BUF_SIZE;
1569 while (increment)
1571 p = 0;
1572 r = EDIT_BUF_SIZE;
1573 if (r > increment)
1574 r = increment;
1575 t = s;
1576 if (r < t)
1577 t = r;
1578 memqcpy (edit,
1579 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1580 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
1581 if (r >= s)
1583 if (t)
1585 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1586 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1588 memqcpy (edit,
1589 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1590 EDIT_BUF_SIZE - r,
1591 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1592 EDIT_BUF_SIZE - (r - s), r - s);
1594 increment -= r;
1595 edit->curs1 -= r;
1596 edit->curs2 += r;
1597 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1599 if (p)
1600 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1601 else
1602 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1604 else
1606 g_free (p);
1609 return edit_get_byte (edit, edit->curs1);
1612 #endif /* ! FAST_MOVE_CURSOR */
1614 /* moves the cursor right or left: increment positive or negative respectively */
1615 void
1616 edit_cursor_move (WEdit * edit, long increment)
1618 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1619 int c;
1620 #ifdef FAST_MOVE_CURSOR
1621 if (increment < -256)
1623 edit->force |= REDRAW_PAGE;
1624 edit_move_backward_lots (edit, -increment);
1625 return;
1627 #endif /* ! FAST_MOVE_CURSOR */
1629 if (increment < 0)
1631 for (; increment < 0; increment++)
1633 if (!edit->curs1)
1634 return;
1636 edit_push_action (edit, CURS_RIGHT);
1638 c = edit_get_byte (edit, edit->curs1 - 1);
1639 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1640 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1641 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1642 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1643 edit->curs2++;
1644 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
1645 1) & M_EDIT_BUF_SIZE];
1646 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1648 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1649 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1651 edit->curs1--;
1652 if (c == '\n')
1654 edit->curs_line--;
1655 edit->force |= REDRAW_LINE_BELOW;
1660 else if (increment > 0)
1662 for (; increment > 0; increment--)
1664 if (!edit->curs2)
1665 return;
1667 edit_push_action (edit, CURS_LEFT);
1669 c = edit_get_byte (edit, edit->curs1);
1670 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1671 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1672 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1673 edit->curs1++;
1674 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1675 ((edit->curs2 -
1676 1) & M_EDIT_BUF_SIZE) - 1];
1677 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1679 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1680 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1682 edit->curs2--;
1683 if (c == '\n')
1685 edit->curs_line++;
1686 edit->force |= REDRAW_LINE_ABOVE;
1692 /* These functions return positions relative to lines */
1694 /* returns index of last char on line + 1 */
1695 long
1696 edit_eol (WEdit * edit, long current)
1698 if (current < edit->last_byte)
1700 for (;; current++)
1701 if (edit_get_byte (edit, current) == '\n')
1702 break;
1704 else
1705 return edit->last_byte;
1706 return current;
1709 /* returns index of first char on line */
1710 long
1711 edit_bol (WEdit * edit, long current)
1713 if (current > 0)
1715 for (;; current--)
1716 if (edit_get_byte (edit, current - 1) == '\n')
1717 break;
1719 else
1720 return 0;
1721 return current;
1725 long
1726 edit_count_lines (WEdit * edit, long current, long upto)
1728 long lines = 0;
1729 if (upto > edit->last_byte)
1730 upto = edit->last_byte;
1731 if (current < 0)
1732 current = 0;
1733 while (current < upto)
1734 if (edit_get_byte (edit, current++) == '\n')
1735 lines++;
1736 return lines;
1740 /* If lines is zero this returns the count of lines from current to upto. */
1741 /* If upto is zero returns index of lines forward current. */
1742 long
1743 edit_move_forward (WEdit * edit, long current, long lines, long upto)
1745 if (upto)
1747 return edit_count_lines (edit, current, upto);
1749 else
1751 long next;
1752 if (lines < 0)
1753 lines = 0;
1754 while (lines--)
1756 next = edit_eol (edit, current) + 1;
1757 if (next > edit->last_byte)
1758 break;
1759 else
1760 current = next;
1762 return current;
1767 /* Returns offset of 'lines' lines up from current */
1768 long
1769 edit_move_backward (WEdit * edit, long current, long lines)
1771 if (lines < 0)
1772 lines = 0;
1773 current = edit_bol (edit, current);
1774 while ((lines--) && current != 0)
1775 current = edit_bol (edit, current - 1);
1776 return current;
1779 /* If cols is zero this returns the count of columns from current to upto. */
1780 /* If upto is zero returns index of cols across from current. */
1781 long
1782 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1784 long p, q;
1785 int col;
1787 if (upto)
1789 q = upto;
1790 cols = -10;
1792 else
1793 q = edit->last_byte + 2;
1795 for (col = 0, p = current; p < q; p++)
1797 int c, orig_c;
1798 int utf_ch = 0;
1799 #ifdef HAVE_CHARSET
1800 int cw = 1;
1801 #endif
1802 if (cols != -10)
1804 if (col == cols)
1805 return p;
1806 if (col > cols)
1807 return p - 1;
1809 orig_c = c = edit_get_byte (edit, p);
1810 #ifdef HAVE_CHARSET
1811 if (edit->utf8)
1813 utf_ch = edit_get_utf (edit, p, &cw);
1814 if (utf8_display)
1816 if (cw > 1)
1817 col -= cw - 1;
1818 if (g_unichar_iswide (utf_ch))
1819 col++;
1821 else if (cw > 1 && g_unichar_isprint (utf_ch))
1822 col -= cw - 1;
1824 #endif
1825 c = convert_to_display_c (c);
1826 if (c == '\t')
1827 col += TAB_SIZE - col % TAB_SIZE;
1828 else if (c == '\n')
1830 if (upto)
1831 return col;
1832 else
1833 return p;
1835 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1836 /* '\r' is shown as ^M, so we must advance 2 characters */
1837 /* Caret notation for control characters */
1838 col += 2;
1839 else
1840 col++;
1842 return col;
1845 /* returns the current column position of the cursor */
1847 edit_get_col (WEdit * edit)
1849 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1853 /* Scrolling functions */
1855 void
1856 edit_update_curs_row (WEdit * edit)
1858 edit->curs_row = edit->curs_line - edit->start_line;
1861 void
1862 edit_update_curs_col (WEdit * edit)
1864 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1868 edit_get_curs_col (const WEdit * edit)
1870 return edit->curs_col;
1873 /*moves the display start position up by i lines */
1874 void
1875 edit_scroll_upward (WEdit * edit, unsigned long i)
1877 unsigned long lines_above = edit->start_line;
1878 if (i > lines_above)
1879 i = lines_above;
1880 if (i)
1882 edit->start_line -= i;
1883 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1884 edit->force |= REDRAW_PAGE;
1885 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1887 edit_update_curs_row (edit);
1891 /* returns 1 if could scroll, 0 otherwise */
1892 void
1893 edit_scroll_downward (WEdit * edit, int i)
1895 int lines_below;
1896 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1897 if (lines_below > 0)
1899 if (i > lines_below)
1900 i = lines_below;
1901 edit->start_line += i;
1902 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1903 edit->force |= REDRAW_PAGE;
1904 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1906 edit_update_curs_row (edit);
1909 void
1910 edit_scroll_right (WEdit * edit, int i)
1912 edit->force |= REDRAW_PAGE;
1913 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1914 edit->start_col -= i;
1917 void
1918 edit_scroll_left (WEdit * edit, int i)
1920 if (edit->start_col)
1922 edit->start_col += i;
1923 if (edit->start_col > 0)
1924 edit->start_col = 0;
1925 edit->force |= REDRAW_PAGE;
1926 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1930 /* high level cursor movement commands */
1932 static int
1933 is_in_indent (WEdit * edit)
1935 long p = edit_bol (edit, edit->curs1);
1936 while (p < edit->curs1)
1937 if (!strchr (" \t", edit_get_byte (edit, p++)))
1938 return 0;
1939 return 1;
1942 static int left_of_four_spaces (WEdit * edit);
1944 void
1945 edit_move_to_prev_col (WEdit * edit, long p)
1947 int prev = edit->prev_col;
1948 int over = edit->over_col;
1949 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1951 if (option_cursor_beyond_eol)
1953 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
1954 edit_eol (edit, edit->curs1));
1956 if (line_len < prev + edit->over_col)
1958 edit->over_col = prev + over - line_len;
1959 edit->prev_col = line_len;
1960 edit->curs_col = line_len;
1962 else
1964 edit->curs_col = prev + over;
1965 edit->prev_col = edit->curs_col;
1966 edit->over_col = 0;
1969 else
1971 edit->over_col = 0;
1972 if (is_in_indent (edit) && option_fake_half_tabs)
1974 edit_update_curs_col (edit);
1975 if (space_width)
1976 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
1978 int q = edit->curs_col;
1979 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1980 p = edit_bol (edit, edit->curs1);
1981 edit_cursor_move (edit,
1982 edit_move_forward3 (edit, p, edit->curs_col,
1983 0) - edit->curs1);
1984 if (!left_of_four_spaces (edit))
1985 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1991 static int
1992 is_blank (WEdit * edit, long offset)
1994 long s, f;
1995 int c;
1996 s = edit_bol (edit, offset);
1997 f = edit_eol (edit, offset) - 1;
1998 while (s <= f)
2000 c = edit_get_byte (edit, s++);
2001 if (!isspace (c))
2002 return 0;
2004 return 1;
2008 /* returns the offset of line i */
2009 static long
2010 edit_find_line (WEdit * edit, int line)
2012 int i, j = 0;
2013 int m = 2000000000;
2014 if (!edit->caches_valid)
2016 for (i = 0; i < N_LINE_CACHES; i++)
2017 edit->line_numbers[i] = edit->line_offsets[i] = 0;
2018 /* three offsets that we *know* are line 0 at 0 and these two: */
2019 edit->line_numbers[1] = edit->curs_line;
2020 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
2021 edit->line_numbers[2] = edit->total_lines;
2022 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
2023 edit->caches_valid = 1;
2025 if (line >= edit->total_lines)
2026 return edit->line_offsets[2];
2027 if (line <= 0)
2028 return 0;
2029 /* find the closest known point */
2030 for (i = 0; i < N_LINE_CACHES; i++)
2032 int n;
2033 n = abs (edit->line_numbers[i] - line);
2034 if (n < m)
2036 m = n;
2037 j = i;
2040 if (m == 0)
2041 return edit->line_offsets[j]; /* know the offset exactly */
2042 if (m == 1 && j >= 3)
2043 i = j; /* one line different - caller might be looping, so stay in this cache */
2044 else
2045 i = 3 + (rand () % (N_LINE_CACHES - 3));
2046 if (line > edit->line_numbers[j])
2047 edit->line_offsets[i] =
2048 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
2049 else
2050 edit->line_offsets[i] =
2051 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
2052 edit->line_numbers[i] = line;
2053 return edit->line_offsets[i];
2057 line_is_blank (WEdit * edit, long line)
2059 return is_blank (edit, edit_find_line (edit, line));
2062 /* moves up until a blank line is reached, or until just
2063 before a non-blank line is reached */
2064 static void
2065 edit_move_up_paragraph (WEdit * edit, int do_scroll)
2067 int i = 0;
2068 if (edit->curs_line > 1)
2070 if (line_is_blank (edit, edit->curs_line))
2072 if (line_is_blank (edit, edit->curs_line - 1))
2074 for (i = edit->curs_line - 1; i; i--)
2075 if (!line_is_blank (edit, i))
2077 i++;
2078 break;
2081 else
2083 for (i = edit->curs_line - 1; i; i--)
2084 if (line_is_blank (edit, i))
2085 break;
2088 else
2090 for (i = edit->curs_line - 1; i; i--)
2091 if (line_is_blank (edit, i))
2092 break;
2095 edit_move_up (edit, edit->curs_line - i, do_scroll);
2098 /* moves down until a blank line is reached, or until just
2099 before a non-blank line is reached */
2100 static void
2101 edit_move_down_paragraph (WEdit * edit, int do_scroll)
2103 int i;
2104 if (edit->curs_line >= edit->total_lines - 1)
2106 i = edit->total_lines;
2108 else
2110 if (line_is_blank (edit, edit->curs_line))
2112 if (line_is_blank (edit, edit->curs_line + 1))
2114 for (i = edit->curs_line + 1; i; i++)
2115 if (!line_is_blank (edit, i) || i > edit->total_lines)
2117 i--;
2118 break;
2121 else
2123 for (i = edit->curs_line + 1; i; i++)
2124 if (line_is_blank (edit, i) || i >= edit->total_lines)
2125 break;
2128 else
2130 for (i = edit->curs_line + 1; i; i++)
2131 if (line_is_blank (edit, i) || i >= edit->total_lines)
2132 break;
2135 edit_move_down (edit, i - edit->curs_line, do_scroll);
2138 static void
2139 edit_begin_page (WEdit * edit)
2141 edit_update_curs_row (edit);
2142 edit_move_up (edit, edit->curs_row, 0);
2145 static void
2146 edit_end_page (WEdit * edit)
2148 edit_update_curs_row (edit);
2149 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
2153 /* goto beginning of text */
2154 static void
2155 edit_move_to_top (WEdit * edit)
2157 if (edit->curs_line)
2159 edit_cursor_move (edit, -edit->curs1);
2160 edit_move_to_prev_col (edit, 0);
2161 edit->force |= REDRAW_PAGE;
2162 edit->search_start = 0;
2163 edit_update_curs_row (edit);
2168 /* goto end of text */
2169 static void
2170 edit_move_to_bottom (WEdit * edit)
2172 if (edit->curs_line < edit->total_lines)
2174 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
2175 edit->start_display = edit->last_byte;
2176 edit->start_line = edit->total_lines;
2177 edit_scroll_upward (edit, edit->num_widget_lines - 1);
2178 edit->force |= REDRAW_PAGE;
2182 /* goto beginning of line */
2183 static void
2184 edit_cursor_to_bol (WEdit * edit)
2186 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
2187 edit->search_start = edit->curs1;
2188 edit->prev_col = edit_get_col (edit);
2189 edit->over_col = 0;
2192 /* goto end of line */
2193 static void
2194 edit_cursor_to_eol (WEdit * edit)
2196 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
2197 edit->search_start = edit->curs1;
2198 edit->prev_col = edit_get_col (edit);
2199 edit->over_col = 0;
2202 /* move cursor to line 'line' */
2203 void
2204 edit_move_to_line (WEdit * e, long line)
2206 if (line < e->curs_line)
2207 edit_move_up (e, e->curs_line - line, 0);
2208 else
2209 edit_move_down (e, line - e->curs_line, 0);
2210 edit_scroll_screen_over_cursor (e);
2213 /* scroll window so that first visible line is 'line' */
2214 void
2215 edit_move_display (WEdit * e, long line)
2217 if (line < e->start_line)
2218 edit_scroll_upward (e, e->start_line - line);
2219 else
2220 edit_scroll_downward (e, line - e->start_line);
2223 /* save markers onto undo stack */
2224 void
2225 edit_push_markers (WEdit * edit)
2227 edit_push_action (edit, MARK_1 + edit->mark1);
2228 edit_push_action (edit, MARK_2 + edit->mark2);
2231 void
2232 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2234 edit->mark1 = m1;
2235 edit->mark2 = m2;
2236 edit->column1 = c1;
2237 edit->column2 = c2;
2241 /* highlight marker toggle */
2242 void
2243 edit_mark_cmd (WEdit * edit, int unmark)
2245 edit_push_markers (edit);
2246 if (unmark)
2248 edit_set_markers (edit, 0, 0, 0, 0);
2249 edit->force |= REDRAW_PAGE;
2251 else
2253 if (edit->mark2 >= 0)
2255 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
2256 edit->curs_col + edit->over_col);
2257 edit->force |= REDRAW_PAGE;
2259 else
2260 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
2261 edit->curs_col + edit->over_col);
2265 static unsigned long
2266 my_type_of (int c)
2268 int x, r = 0;
2269 const char *p, *q;
2270 const char option_chars_move_whole_word[] =
2271 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2273 if (!c)
2274 return 0;
2275 if (c == '!')
2277 if (*option_chars_move_whole_word == '!')
2278 return 2;
2279 return 0x80000000UL;
2281 if (g_ascii_isupper ((gchar) c))
2282 c = 'A';
2283 else if (g_ascii_islower ((gchar) c))
2284 c = 'a';
2285 else if (g_ascii_isalpha (c))
2286 c = 'a';
2287 else if (isdigit (c))
2288 c = '0';
2289 else if (isspace (c))
2290 c = ' ';
2291 q = strchr (option_chars_move_whole_word, c);
2292 if (!q)
2293 return 0xFFFFFFFFUL;
2296 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2297 if (*p == '!')
2298 x <<= 1;
2299 r |= x;
2301 while ((q = strchr (q + 1, c)));
2302 return r;
2305 static void
2306 edit_left_word_move (WEdit * edit, int s)
2308 for (;;)
2310 int c1, c2;
2311 if (column_highlighting
2312 && edit->mark1 != edit->mark2
2313 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2314 break;
2315 edit_cursor_move (edit, -1);
2316 if (!edit->curs1)
2317 break;
2318 c1 = edit_get_byte (edit, edit->curs1 - 1);
2319 c2 = edit_get_byte (edit, edit->curs1);
2320 if (!(my_type_of (c1) & my_type_of (c2)))
2321 break;
2322 if (isspace (c1) && !isspace (c2))
2323 break;
2324 if (s)
2325 if (!isspace (c1) && isspace (c2))
2326 break;
2330 static void
2331 edit_left_word_move_cmd (WEdit * edit)
2333 edit_left_word_move (edit, 0);
2334 edit->force |= REDRAW_PAGE;
2337 static void
2338 edit_right_word_move (WEdit * edit, int s)
2340 for (;;)
2342 int c1, c2;
2343 if (column_highlighting
2344 && edit->mark1 != edit->mark2
2345 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
2346 break;
2347 edit_cursor_move (edit, 1);
2348 if (edit->curs1 >= edit->last_byte)
2349 break;
2350 c1 = edit_get_byte (edit, edit->curs1 - 1);
2351 c2 = edit_get_byte (edit, edit->curs1);
2352 if (!(my_type_of (c1) & my_type_of (c2)))
2353 break;
2354 if (isspace (c1) && !isspace (c2))
2355 break;
2356 if (s)
2357 if (!isspace (c1) && isspace (c2))
2358 break;
2362 static void
2363 edit_right_word_move_cmd (WEdit * edit)
2365 edit_right_word_move (edit, 0);
2366 edit->force |= REDRAW_PAGE;
2369 static void
2370 edit_right_char_move_cmd (WEdit * edit)
2372 int cw = 1;
2373 int c = 0;
2374 if (edit->utf8)
2376 c = edit_get_utf (edit, edit->curs1, &cw);
2377 if (cw < 1)
2378 cw = 1;
2380 else
2382 c = edit_get_byte (edit, edit->curs1);
2384 if (option_cursor_beyond_eol && c == '\n')
2386 edit->over_col++;
2388 else
2390 edit_cursor_move (edit, cw);
2394 static void
2395 edit_left_char_move_cmd (WEdit * edit)
2397 int cw = 1;
2398 if (column_highlighting
2399 && option_cursor_beyond_eol
2400 && edit->mark1 != edit->mark2
2401 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2402 return;
2403 if (edit->utf8)
2405 edit_get_prev_utf (edit, edit->curs1, &cw);
2406 if (cw < 1)
2407 cw = 1;
2409 if (option_cursor_beyond_eol && edit->over_col > 0)
2411 edit->over_col--;
2413 else
2415 edit_cursor_move (edit, -cw);
2419 /** Up or down cursor moving.
2420 direction = TRUE - move up
2421 = FALSE - move down
2423 static void
2424 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2426 unsigned long p;
2427 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
2429 if (i > l)
2430 i = l;
2432 if (i == 0)
2433 return;
2435 if (i > 1)
2436 edit->force |= REDRAW_PAGE;
2437 if (do_scroll)
2439 if (direction)
2440 edit_scroll_upward (edit, i);
2441 else
2442 edit_scroll_downward (edit, i);
2444 p = edit_bol (edit, edit->curs1);
2446 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
2448 edit_cursor_move (edit, p - edit->curs1);
2450 edit_move_to_prev_col (edit, p);
2452 /* search start of current multibyte char (like CJK) */
2453 if (edit->curs1 + 1 < edit->last_byte)
2455 edit_right_char_move_cmd (edit);
2456 edit_left_char_move_cmd (edit);
2459 edit->search_start = edit->curs1;
2460 edit->found_len = 0;
2463 static void
2464 edit_right_delete_word (WEdit * edit)
2466 int c1, c2;
2467 for (;;)
2469 if (edit->curs1 >= edit->last_byte)
2470 break;
2471 c1 = edit_delete (edit, 1);
2472 c2 = edit_get_byte (edit, edit->curs1);
2473 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2474 break;
2475 if (!(my_type_of (c1) & my_type_of (c2)))
2476 break;
2480 static void
2481 edit_left_delete_word (WEdit * edit)
2483 int c1, c2;
2484 for (;;)
2486 if (edit->curs1 <= 0)
2487 break;
2488 c1 = edit_backspace (edit, 1);
2489 c2 = edit_get_byte (edit, edit->curs1 - 1);
2490 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2491 break;
2492 if (!(my_type_of (c1) & my_type_of (c2)))
2493 break;
2498 the start column position is not recorded, and hence does not
2499 undo as it happed. But who would notice.
2501 static void
2502 edit_do_undo (WEdit * edit)
2504 long ac;
2505 long count = 0;
2507 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2508 edit->over_col = 0;
2509 while ((ac = pop_action (edit)) < KEY_PRESS)
2511 switch ((int) ac)
2513 case STACK_BOTTOM:
2514 goto done_undo;
2515 case CURS_RIGHT:
2516 edit_cursor_move (edit, 1);
2517 break;
2518 case CURS_LEFT:
2519 edit_cursor_move (edit, -1);
2520 break;
2521 case BACKSPACE:
2522 edit_backspace (edit, 1);
2523 break;
2524 case DELCHAR:
2525 edit_delete (edit, 1);
2526 break;
2527 case COLUMN_ON:
2528 column_highlighting = 1;
2529 break;
2530 case COLUMN_OFF:
2531 column_highlighting = 0;
2532 break;
2534 if (ac >= 256 && ac < 512)
2535 edit_insert_ahead (edit, ac - 256);
2536 if (ac >= 0 && ac < 256)
2537 edit_insert (edit, ac);
2539 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
2541 edit->mark1 = ac - MARK_1;
2542 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2544 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
2546 edit->mark2 = ac - MARK_2;
2547 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2549 if (count++)
2550 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2553 if (edit->start_display > ac - KEY_PRESS)
2555 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2556 edit->force |= REDRAW_PAGE;
2558 else if (edit->start_display < ac - KEY_PRESS)
2560 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2561 edit->force |= REDRAW_PAGE;
2563 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2564 edit_update_curs_row (edit);
2566 done_undo:;
2567 edit->stack_disable = 0;
2570 static void
2571 edit_delete_to_line_end (WEdit * edit)
2573 while (edit_get_byte (edit, edit->curs1) != '\n')
2575 if (!edit->curs2)
2576 break;
2577 edit_delete (edit, 1);
2581 static void
2582 edit_delete_to_line_begin (WEdit * edit)
2584 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2586 if (!edit->curs1)
2587 break;
2588 edit_backspace (edit, 1);
2592 void
2593 edit_delete_line (WEdit * edit)
2596 * Delete right part of the line.
2597 * Note that edit_get_byte() returns '\n' when byte position is
2598 * beyond EOF.
2600 while (edit_get_byte (edit, edit->curs1) != '\n')
2602 (void) edit_delete (edit, 1);
2606 * Delete '\n' char.
2607 * Note that edit_delete() will not corrupt anything if called while
2608 * cursor position is EOF.
2610 (void) edit_delete (edit, 1);
2613 * Delete left part of the line.
2614 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2616 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2618 (void) edit_backspace (edit, 1);
2622 void
2623 insert_spaces_tab (WEdit * edit, int half)
2625 int i;
2626 edit_update_curs_col (edit);
2627 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
2628 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2629 while (i > 0)
2631 edit_insert (edit, ' ');
2632 i -= space_width;
2636 static int
2637 is_aligned_on_a_tab (WEdit * edit)
2639 edit_update_curs_col (edit);
2640 return !((edit->curs_col % (TAB_SIZE * space_width))
2641 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2644 static int
2645 right_of_four_spaces (WEdit * edit)
2647 int i, ch = 0;
2648 for (i = 1; 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;
2655 static int
2656 left_of_four_spaces (WEdit * edit)
2658 int i, ch = 0;
2659 for (i = 0; i < HALF_TAB_SIZE; i++)
2660 ch |= edit_get_byte (edit, edit->curs1 + i);
2661 if (ch == ' ')
2662 return is_aligned_on_a_tab (edit);
2663 return 0;
2667 edit_indent_width (WEdit * edit, long p)
2669 long q = p;
2670 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2671 q++;
2672 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2675 void
2676 edit_insert_indent (WEdit * edit, int indent)
2678 if (!option_fill_tabs_with_spaces)
2680 while (indent >= TAB_SIZE)
2682 edit_insert (edit, '\t');
2683 indent -= TAB_SIZE;
2686 while (indent-- > 0)
2687 edit_insert (edit, ' ');
2690 static void
2691 edit_auto_indent (WEdit * edit)
2693 long p;
2694 char c;
2695 p = edit->curs1;
2696 /* use the previous line as a template */
2697 p = edit_move_backward (edit, p, 1);
2698 /* copy the leading whitespace of the line */
2699 for (;;)
2700 { /* no range check - the line _is_ \n-terminated */
2701 c = edit_get_byte (edit, p++);
2702 if (c != ' ' && c != '\t')
2703 break;
2704 edit_insert (edit, c);
2708 static inline void
2709 edit_double_newline (WEdit * edit)
2711 edit_insert (edit, '\n');
2712 if (edit_get_byte (edit, edit->curs1) == '\n')
2713 return;
2714 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2715 return;
2716 edit->force |= REDRAW_PAGE;
2717 edit_insert (edit, '\n');
2720 static inline void
2721 edit_tab_cmd (WEdit * edit)
2723 int i;
2725 if (option_fake_half_tabs)
2727 if (is_in_indent (edit))
2729 /*insert a half tab (usually four spaces) unless there is a
2730 half tab already behind, then delete it and insert a
2731 full tab. */
2732 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
2734 for (i = 1; i <= HALF_TAB_SIZE; i++)
2735 edit_backspace (edit, 1);
2736 edit_insert (edit, '\t');
2738 else
2740 insert_spaces_tab (edit, 1);
2742 return;
2745 if (option_fill_tabs_with_spaces)
2747 insert_spaces_tab (edit, 0);
2749 else
2751 edit_insert (edit, '\t');
2755 static void
2756 check_and_wrap_line (WEdit * edit)
2758 int curs, c;
2759 if (!option_typewriter_wrap)
2760 return;
2761 edit_update_curs_col (edit);
2762 if (edit->curs_col < option_word_wrap_line_length)
2763 return;
2764 curs = edit->curs1;
2765 for (;;)
2767 curs--;
2768 c = edit_get_byte (edit, curs);
2769 if (c == '\n' || curs <= 0)
2771 edit_insert (edit, '\n');
2772 return;
2774 if (c == ' ' || c == '\t')
2776 int current = edit->curs1;
2777 edit_cursor_move (edit, curs - edit->curs1 + 1);
2778 edit_insert (edit, '\n');
2779 edit_cursor_move (edit, current - edit->curs1 + 1);
2780 return;
2785 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
2787 void
2788 edit_push_key_press (WEdit * edit)
2790 edit_push_action (edit, KEY_PRESS + edit->start_display);
2791 if (edit->mark2 == -1)
2792 edit_push_action (edit, MARK_1 + edit->mark1);
2795 /* this find the matching bracket in either direction, and sets edit->bracket */
2796 static long
2797 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2799 const char *const b = "{}{[][()(", *p;
2800 int i = 1, a, inc = -1, c, d, n = 0;
2801 unsigned long j = 0;
2802 long q;
2803 edit_update_curs_row (edit);
2804 c = edit_get_byte (edit, edit->curs1);
2805 p = strchr (b, c);
2806 /* no limit */
2807 if (!furthest_bracket_search)
2808 furthest_bracket_search--;
2809 /* not on a bracket at all */
2810 if (!p)
2811 return -1;
2812 /* the matching bracket */
2813 d = p[1];
2814 /* going left or right? */
2815 if (strchr ("{[(", c))
2816 inc = 1;
2817 for (q = edit->curs1 + inc;; q += inc)
2819 /* out of buffer? */
2820 if (q >= edit->last_byte || q < 0)
2821 break;
2822 a = edit_get_byte (edit, q);
2823 /* don't want to eat CPU */
2824 if (j++ > furthest_bracket_search)
2825 break;
2826 /* out of screen? */
2827 if (in_screen)
2829 if (q < edit->start_display)
2830 break;
2831 /* count lines if searching downward */
2832 if (inc > 0 && a == '\n')
2833 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2834 break;
2836 /* count bracket depth */
2837 i += (a == c) - (a == d);
2838 /* return if bracket depth is zero */
2839 if (!i)
2840 return q;
2842 /* no match */
2843 return -1;
2846 static long last_bracket = -1;
2848 void
2849 edit_find_bracket (WEdit * edit)
2851 edit->bracket = edit_get_bracket (edit, 1, 10000);
2852 if (last_bracket != edit->bracket)
2853 edit->force |= REDRAW_PAGE;
2854 last_bracket = edit->bracket;
2857 static inline void
2858 edit_goto_matching_bracket (WEdit * edit)
2860 long q;
2862 q = edit_get_bracket (edit, 0, 0);
2863 if (q >= 0)
2865 edit->bracket = edit->curs1;
2866 edit->force |= REDRAW_PAGE;
2867 edit_cursor_move (edit, q - edit->curs1);
2872 * This executes a command as though the user initiated it through a key
2873 * press. Callback with WIDGET_KEY as a message calls this after
2874 * translating the key press. This function can be used to pass any
2875 * command to the editor. Note that the screen wouldn't update
2876 * automatically. Either of command or char_for_insertion must be
2877 * passed as -1. Commands are executed, and char_for_insertion is
2878 * inserted at the cursor.
2880 void
2881 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
2883 if (command == CK_Begin_Record_Macro)
2885 edit->macro_i = 0;
2886 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2887 return;
2889 if (command == CK_End_Record_Macro && edit->macro_i != -1)
2891 edit->force |= REDRAW_COMPLETELY;
2892 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2893 edit->macro_i = -1;
2894 return;
2896 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
2898 edit->macro[edit->macro_i].command = command;
2899 edit->macro[edit->macro_i++].ch = char_for_insertion;
2901 /* record the beginning of a set of editing actions initiated by a key press */
2902 if (command != CK_Undo && command != CK_Ext_Mode)
2903 edit_push_key_press (edit);
2905 edit_execute_cmd (edit, command, char_for_insertion);
2906 if (column_highlighting)
2907 edit->force |= REDRAW_PAGE;
2910 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
2913 This executes a command at a lower level than macro recording.
2914 It also does not push a key_press onto the undo stack. This means
2915 that if it is called many times, a single undo command will undo
2916 all of them. It also does not check for the Undo command.
2918 void
2919 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
2921 edit->force |= REDRAW_LINE;
2923 /* The next key press will unhighlight the found string, so update
2924 * the whole page */
2925 if (edit->found_len || column_highlighting)
2926 edit->force |= REDRAW_PAGE;
2928 if (command / 100 == 6)
2929 { /* a highlight command like shift-arrow */
2930 column_highlighting = 0;
2931 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
2933 edit_mark_cmd (edit, 1); /* clear */
2934 edit_mark_cmd (edit, 0); /* marking on */
2936 edit->highlight = 1;
2938 else
2939 { /* any other command */
2940 if (edit->highlight)
2941 edit_mark_cmd (edit, 0); /* clear */
2942 edit->highlight = 0;
2945 /* first check for undo */
2946 if (command == CK_Undo)
2948 edit_do_undo (edit);
2949 edit->found_len = 0;
2950 edit->prev_col = edit_get_col (edit);
2951 edit->search_start = edit->curs1;
2952 return;
2955 /* An ordinary key press */
2956 if (char_for_insertion >= 0)
2958 /* if non persistent selection and text selected */
2959 if (!option_persistent_selections)
2961 if (edit->mark1 != edit->mark2)
2962 edit_block_delete_cmd (edit);
2964 if (edit->overwrite)
2966 /* remove char only one time, after input first byte, multibyte chars */
2967 if ((!utf8_display || edit->charpoint == 0)
2968 && edit_get_byte (edit, edit->curs1) != '\n')
2969 edit_delete (edit, 0);
2971 if (option_cursor_beyond_eol && edit->over_col > 0)
2972 edit_insert_over (edit);
2973 #ifdef HAVE_CHARSET
2974 if (char_for_insertion > 255 && utf8_display == 0)
2976 unsigned char str[6 + 1];
2977 size_t i = 0;
2978 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
2979 if (res == 0)
2981 str[0] = '.';
2982 str[1] = '\0';
2984 else
2986 str[res] = '\0';
2988 while (str[i] != 0 && i <= 6)
2990 char_for_insertion = str[i];
2991 edit_insert (edit, char_for_insertion);
2992 i++;
2995 else
2996 #endif
2997 edit_insert (edit, char_for_insertion);
2999 if (option_auto_para_formatting)
3001 format_paragraph (edit, 0);
3002 edit->force |= REDRAW_PAGE;
3004 else
3005 check_and_wrap_line (edit);
3006 edit->found_len = 0;
3007 edit->prev_col = edit_get_col (edit);
3008 edit->search_start = edit->curs1;
3009 edit_find_bracket (edit);
3010 return;
3013 switch (command)
3015 case CK_Begin_Page:
3016 case CK_End_Page:
3017 case CK_Begin_Page_Highlight:
3018 case CK_End_Page_Highlight:
3019 case CK_Word_Left:
3020 case CK_Word_Right:
3021 case CK_Up:
3022 case CK_Down:
3023 case CK_Left:
3024 case CK_Right:
3025 if (edit->mark2 >= 0)
3027 if (!option_persistent_selections)
3029 if (column_highlighting)
3030 edit_push_action (edit, COLUMN_ON);
3031 column_highlighting = 0;
3032 edit_mark_cmd (edit, 1);
3037 switch (command)
3039 case CK_Begin_Page:
3040 case CK_End_Page:
3041 case CK_Begin_Page_Highlight:
3042 case CK_End_Page_Highlight:
3043 case CK_Word_Left:
3044 case CK_Word_Right:
3045 case CK_Up:
3046 case CK_Down:
3047 case CK_Word_Left_Highlight:
3048 case CK_Word_Right_Highlight:
3049 case CK_Up_Highlight:
3050 case CK_Down_Highlight:
3051 case CK_Up_Alt_Highlight:
3052 case CK_Down_Alt_Highlight:
3053 if (edit->mark2 == -1)
3054 break; /*marking is following the cursor: may need to highlight a whole line */
3055 case CK_Left:
3056 case CK_Right:
3057 case CK_Left_Highlight:
3058 case CK_Right_Highlight:
3059 edit->force |= REDRAW_CHAR_ONLY;
3062 /* basic cursor key commands */
3063 switch (command)
3065 case CK_BackSpace:
3066 /* if non persistent selection and text selected */
3067 if (!option_persistent_selections)
3069 if (edit->mark1 != edit->mark2)
3071 edit_block_delete_cmd (edit);
3072 break;
3075 if (option_cursor_beyond_eol && edit->over_col > 0)
3077 edit->over_col--;
3078 break;
3080 if (option_backspace_through_tabs && is_in_indent (edit))
3082 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3083 edit_backspace (edit, 1);
3084 break;
3086 else
3088 if (option_fake_half_tabs)
3090 int i;
3091 if (is_in_indent (edit) && right_of_four_spaces (edit))
3093 for (i = 0; i < HALF_TAB_SIZE; i++)
3094 edit_backspace (edit, 1);
3095 break;
3099 edit_backspace (edit, 0);
3100 break;
3101 case CK_Delete:
3102 /* if non persistent selection and text selected */
3103 if (!option_persistent_selections)
3105 if (edit->mark1 != edit->mark2)
3107 edit_block_delete_cmd (edit);
3108 break;
3112 if (option_cursor_beyond_eol && edit->over_col > 0)
3113 edit_insert_over (edit);
3115 if (option_fake_half_tabs)
3117 int i;
3118 if (is_in_indent (edit) && left_of_four_spaces (edit))
3120 for (i = 1; i <= HALF_TAB_SIZE; i++)
3121 edit_delete (edit, 1);
3122 break;
3125 edit_delete (edit, 0);
3126 break;
3127 case CK_Delete_Word_Left:
3128 edit->over_col = 0;
3129 edit_left_delete_word (edit);
3130 break;
3131 case CK_Delete_Word_Right:
3132 if (option_cursor_beyond_eol && edit->over_col > 0)
3133 edit_insert_over (edit);
3135 edit_right_delete_word (edit);
3136 break;
3137 case CK_Delete_Line:
3138 edit_delete_line (edit);
3139 break;
3140 case CK_Delete_To_Line_End:
3141 edit_delete_to_line_end (edit);
3142 break;
3143 case CK_Delete_To_Line_Begin:
3144 edit_delete_to_line_begin (edit);
3145 break;
3146 case CK_Enter:
3147 edit->over_col = 0;
3148 if (option_auto_para_formatting)
3150 edit_double_newline (edit);
3151 if (option_return_does_auto_indent)
3152 edit_auto_indent (edit);
3153 format_paragraph (edit, 0);
3155 else
3157 edit_insert (edit, '\n');
3158 if (option_return_does_auto_indent)
3160 edit_auto_indent (edit);
3163 break;
3164 case CK_Return:
3165 edit_insert (edit, '\n');
3166 break;
3168 case CK_Page_Up_Alt_Highlight:
3169 column_highlighting = 1;
3170 case CK_Page_Up:
3171 case CK_Page_Up_Highlight:
3172 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3173 break;
3174 case CK_Page_Down_Alt_Highlight:
3175 column_highlighting = 1;
3176 case CK_Page_Down:
3177 case CK_Page_Down_Highlight:
3178 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3179 break;
3180 case CK_Left_Alt_Highlight:
3181 column_highlighting = 1;
3182 case CK_Left:
3183 case CK_Left_Highlight:
3184 if (option_fake_half_tabs)
3186 if (is_in_indent (edit) && right_of_four_spaces (edit))
3188 if (option_cursor_beyond_eol && edit->over_col > 0)
3189 edit->over_col--;
3190 else
3191 edit_cursor_move (edit, -HALF_TAB_SIZE);
3192 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3193 break;
3196 edit_left_char_move_cmd (edit);
3197 break;
3198 case CK_Right_Alt_Highlight:
3199 column_highlighting = 1;
3200 case CK_Right:
3201 case CK_Right_Highlight:
3202 if (option_fake_half_tabs)
3204 if (is_in_indent (edit) && left_of_four_spaces (edit))
3206 edit_cursor_move (edit, HALF_TAB_SIZE);
3207 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3208 break;
3211 edit_right_char_move_cmd (edit);
3212 break;
3213 case CK_Begin_Page:
3214 case CK_Begin_Page_Highlight:
3215 edit_begin_page (edit);
3216 break;
3217 case CK_End_Page:
3218 case CK_End_Page_Highlight:
3219 edit_end_page (edit);
3220 break;
3221 case CK_Word_Left:
3222 case CK_Word_Left_Highlight:
3223 edit->over_col = 0;
3224 edit_left_word_move_cmd (edit);
3225 break;
3226 case CK_Word_Right:
3227 case CK_Word_Right_Highlight:
3228 edit->over_col = 0;
3229 edit_right_word_move_cmd (edit);
3230 break;
3231 case CK_Up_Alt_Highlight:
3232 column_highlighting = 1;
3233 case CK_Up:
3234 case CK_Up_Highlight:
3235 edit_move_up (edit, 1, 0);
3236 break;
3237 case CK_Down_Alt_Highlight:
3238 column_highlighting = 1;
3239 case CK_Down:
3240 case CK_Down_Highlight:
3241 edit_move_down (edit, 1, 0);
3242 break;
3243 case CK_Paragraph_Up_Alt_Highlight:
3244 column_highlighting = 1;
3245 case CK_Paragraph_Up:
3246 case CK_Paragraph_Up_Highlight:
3247 edit_move_up_paragraph (edit, 0);
3248 break;
3249 case CK_Paragraph_Down_Alt_Highlight:
3250 column_highlighting = 1;
3251 case CK_Paragraph_Down:
3252 case CK_Paragraph_Down_Highlight:
3253 edit_move_down_paragraph (edit, 0);
3254 break;
3255 case CK_Scroll_Up_Alt_Highlight:
3256 column_highlighting = 1;
3257 case CK_Scroll_Up:
3258 case CK_Scroll_Up_Highlight:
3259 edit_move_up (edit, 1, 1);
3260 break;
3261 case CK_Scroll_Down_Alt_Highlight:
3262 column_highlighting = 1;
3263 case CK_Scroll_Down:
3264 case CK_Scroll_Down_Highlight:
3265 edit_move_down (edit, 1, 1);
3266 break;
3267 case CK_Home:
3268 case CK_Home_Highlight:
3269 edit_cursor_to_bol (edit);
3270 break;
3271 case CK_End:
3272 case CK_End_Highlight:
3273 edit_cursor_to_eol (edit);
3274 break;
3275 case CK_Tab:
3276 /* if text marked shift block */
3277 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3279 if (edit->mark2 < 0)
3280 edit_mark_cmd (edit, 0);
3281 edit_move_block_to_right (edit);
3283 else
3285 if (option_cursor_beyond_eol)
3286 edit_insert_over (edit);
3287 edit_tab_cmd (edit);
3288 if (option_auto_para_formatting)
3290 format_paragraph (edit, 0);
3291 edit->force |= REDRAW_PAGE;
3293 else
3295 check_and_wrap_line (edit);
3298 break;
3300 case CK_Toggle_Insert:
3301 edit->overwrite = (edit->overwrite == 0);
3302 break;
3304 case CK_Mark:
3305 if (edit->mark2 >= 0)
3307 if (column_highlighting)
3308 edit_push_action (edit, COLUMN_ON);
3309 column_highlighting = 0;
3311 edit_mark_cmd (edit, 0);
3312 break;
3313 case CK_Column_Mark:
3314 if (!column_highlighting)
3315 edit_push_action (edit, COLUMN_OFF);
3316 column_highlighting = 1;
3317 edit_mark_cmd (edit, 0);
3318 break;
3319 case CK_Mark_All:
3320 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3321 edit->force |= REDRAW_PAGE;
3322 break;
3323 case CK_Unmark:
3324 if (column_highlighting)
3325 edit_push_action (edit, COLUMN_ON);
3326 column_highlighting = 0;
3327 edit_mark_cmd (edit, 1);
3328 break;
3330 case CK_Toggle_Line_State:
3331 option_line_state = !option_line_state;
3332 if (option_line_state)
3334 option_line_state_width = LINE_STATE_WIDTH;
3336 else
3338 option_line_state_width = 0;
3340 edit->force |= REDRAW_PAGE;
3341 break;
3343 case CK_Toggle_Show_Margin:
3344 show_right_margin = !show_right_margin;
3345 edit->force |= REDRAW_PAGE;
3346 break;
3348 case CK_Toggle_Bookmark:
3349 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3350 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3351 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3352 else
3353 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3354 break;
3355 case CK_Flush_Bookmarks:
3356 book_mark_flush (edit, BOOK_MARK_COLOR);
3357 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3358 edit->force |= REDRAW_PAGE;
3359 break;
3360 case CK_Next_Bookmark:
3361 if (edit->book_mark)
3363 struct _book_mark *p;
3364 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3365 if (p->next)
3367 p = p->next;
3368 if (p->line >= edit->start_line + edit->num_widget_lines
3369 || p->line < edit->start_line)
3370 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3371 edit_move_to_line (edit, p->line);
3374 break;
3375 case CK_Prev_Bookmark:
3376 if (edit->book_mark)
3378 struct _book_mark *p;
3379 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3380 while (p->line == edit->curs_line)
3381 if (p->prev)
3382 p = p->prev;
3383 if (p->line >= 0)
3385 if (p->line >= edit->start_line + edit->num_widget_lines
3386 || p->line < edit->start_line)
3387 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3388 edit_move_to_line (edit, p->line);
3391 break;
3393 case CK_Beginning_Of_Text:
3394 case CK_Beginning_Of_Text_Highlight:
3395 edit_move_to_top (edit);
3396 break;
3397 case CK_End_Of_Text:
3398 case CK_End_Of_Text_Highlight:
3399 edit_move_to_bottom (edit);
3400 break;
3402 case CK_Copy:
3403 if (option_cursor_beyond_eol && edit->over_col > 0)
3404 edit_insert_over (edit);
3405 edit_block_copy_cmd (edit);
3406 break;
3407 case CK_Remove:
3408 edit_block_delete_cmd (edit);
3409 break;
3410 case CK_Move:
3411 if (option_cursor_beyond_eol && edit->over_col > 0)
3412 edit_insert_over (edit);
3413 edit_block_move_cmd (edit);
3414 break;
3416 case CK_Shift_Block_Left:
3417 if (edit->mark1 != edit->mark2)
3418 edit_move_block_to_left (edit);
3419 break;
3420 case CK_Shift_Block_Right:
3421 if (edit->mark1 != edit->mark2)
3422 edit_move_block_to_right (edit);
3423 break;
3424 case CK_XStore:
3425 edit_copy_to_X_buf_cmd (edit);
3426 break;
3427 case CK_XCut:
3428 edit_cut_to_X_buf_cmd (edit);
3429 break;
3430 case CK_XPaste:
3431 /* if non persistent selection and text selected */
3432 if (!option_persistent_selections)
3434 if (edit->mark1 != edit->mark2)
3435 edit_block_delete_cmd (edit);
3437 if (option_cursor_beyond_eol && edit->over_col > 0)
3438 edit_insert_over (edit);
3439 edit_paste_from_X_buf_cmd (edit);
3440 break;
3441 case CK_Selection_History:
3442 edit_paste_from_history (edit);
3443 break;
3445 case CK_Save_As:
3446 edit_save_as_cmd (edit);
3447 break;
3448 case CK_Save:
3449 edit_save_confirm_cmd (edit);
3450 break;
3451 case CK_Load:
3452 edit_load_cmd (edit, EDIT_FILE_COMMON);
3453 break;
3454 case CK_Save_Block:
3455 edit_save_block_cmd (edit);
3456 break;
3457 case CK_Insert_File:
3458 edit_insert_file_cmd (edit);
3459 break;
3461 case CK_Load_Prev_File:
3462 edit_load_back_cmd (edit);
3463 break;
3464 case CK_Load_Next_File:
3465 edit_load_forward_cmd (edit);
3466 break;
3468 case CK_Load_Syntax_File:
3469 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3470 break;
3471 case CK_Choose_Syntax:
3472 edit_syntax_dialog (edit, edit->syntax_type);
3473 break;
3475 case CK_Load_Menu_File:
3476 edit_load_cmd (edit, EDIT_FILE_MENU);
3477 break;
3479 case CK_Toggle_Syntax:
3480 if ((option_syntax_highlighting ^= 1) == 1)
3481 edit_load_syntax (edit, NULL, edit->syntax_type);
3482 edit->force |= REDRAW_PAGE;
3483 break;
3485 case CK_Toggle_Tab_TWS:
3486 enable_show_tabs_tws ^= 1;
3487 edit->force |= REDRAW_PAGE;
3488 break;
3490 case CK_Find:
3491 edit_search_cmd (edit, 0);
3492 break;
3493 case CK_Find_Again:
3494 edit_search_cmd (edit, 1);
3495 break;
3496 case CK_Replace:
3497 edit_replace_cmd (edit, 0);
3498 break;
3499 case CK_Replace_Again:
3500 edit_replace_cmd (edit, 1);
3501 break;
3502 case CK_Complete_Word:
3503 /* if text marked shift block */
3504 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3506 edit_move_block_to_left (edit);
3508 else
3510 edit_complete_word_cmd (edit);
3512 break;
3513 case CK_Find_Definition:
3514 edit_get_match_keyword_cmd (edit);
3515 break;
3516 case CK_Quit:
3517 dlg_stop (edit->widget.parent);
3518 break;
3519 case CK_New:
3520 edit_new_cmd (edit);
3521 break;
3522 case CK_Help:
3523 edit_help_cmd (edit);
3524 break;
3525 case CK_Refresh:
3526 edit_refresh_cmd (edit);
3527 break;
3528 case CK_SaveSetupCmd:
3529 save_setup_cmd ();
3530 break;
3531 case CK_About:
3532 query_dialog (_(" About "),
3533 _("\n Cooledit v3.11.5\n\n"
3534 " Copyright (C) 1996 the Free Software Foundation\n\n"
3535 " A user friendly text editor written\n"
3536 " for the Midnight Commander.\n"), D_NORMAL, 1, _("&OK"));
3537 break;
3538 case CK_LearnKeys:
3539 learn_keys ();
3540 break;
3541 case CK_Edit_Options:
3542 edit_options_dialog (edit);
3543 break;
3544 case CK_Edit_Save_Mode:
3545 menu_save_mode_cmd ();
3546 break;
3547 case CK_Date:
3549 char s[BUF_MEDIUM];
3550 /* fool gcc to prevent a Y2K warning */
3551 char time_format[] = "_c";
3552 time_format[0] = '%';
3554 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3555 edit_print_string (edit, s);
3556 edit->force |= REDRAW_PAGE;
3557 break;
3559 break;
3560 case CK_Goto:
3561 edit_goto_cmd (edit);
3562 break;
3563 case CK_Paragraph_Format:
3564 format_paragraph (edit, 1);
3565 edit->force |= REDRAW_PAGE;
3566 break;
3567 case CK_Delete_Macro:
3568 edit_delete_macro_cmd (edit);
3569 break;
3570 case CK_Match_Bracket:
3571 edit_goto_matching_bracket (edit);
3572 break;
3573 case CK_User_Menu:
3574 user_menu (edit);
3575 break;
3576 case CK_Sort:
3577 edit_sort_cmd (edit);
3578 break;
3579 case CK_ExtCmd:
3580 edit_ext_cmd (edit);
3581 break;
3582 case CK_Mail:
3583 edit_mail_dialog (edit);
3584 break;
3585 case CK_Shell:
3586 view_other_cmd ();
3587 break;
3588 case CK_SelectCodepage:
3589 edit_select_codepage_cmd (edit);
3590 break;
3591 case CK_Insert_Literal:
3592 edit_insert_literal_cmd (edit);
3593 break;
3594 case CK_Execute_Macro:
3595 edit_execute_macro_cmd (edit);
3596 break;
3597 case CK_Begin_End_Macro:
3598 edit_begin_end_macro_cmd (edit);
3599 break;
3600 case CK_Ext_Mode:
3601 edit->extmod = 1;
3602 break;
3603 default:
3604 break;
3607 /* CK_Pipe_Block */
3608 if ((command / 1000) == 1) /* a shell command */
3609 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3610 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3611 { /* a macro command */
3612 struct macro m[MAX_MACRO_LENGTH];
3613 int nm;
3614 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3615 edit_execute_macro (edit, m, nm);
3618 /* keys which must set the col position, and the search vars */
3619 switch (command)
3621 case CK_Find:
3622 case CK_Find_Again:
3623 case CK_Replace:
3624 case CK_Replace_Again:
3625 case CK_Complete_Word:
3626 edit->prev_col = edit_get_col (edit);
3627 break;
3628 case CK_Up:
3629 case CK_Up_Highlight:
3630 case CK_Up_Alt_Highlight:
3631 case CK_Down:
3632 case CK_Down_Highlight:
3633 case CK_Down_Alt_Highlight:
3634 case CK_Page_Up:
3635 case CK_Page_Up_Highlight:
3636 case CK_Page_Up_Alt_Highlight:
3637 case CK_Page_Down:
3638 case CK_Page_Down_Highlight:
3639 case CK_Page_Down_Alt_Highlight:
3640 case CK_Beginning_Of_Text:
3641 case CK_Beginning_Of_Text_Highlight:
3642 case CK_End_Of_Text:
3643 case CK_End_Of_Text_Highlight:
3644 case CK_Paragraph_Up:
3645 case CK_Paragraph_Up_Highlight:
3646 case CK_Paragraph_Up_Alt_Highlight:
3647 case CK_Paragraph_Down:
3648 case CK_Paragraph_Down_Highlight:
3649 case CK_Paragraph_Down_Alt_Highlight:
3650 case CK_Scroll_Up:
3651 case CK_Scroll_Up_Highlight:
3652 case CK_Scroll_Up_Alt_Highlight:
3653 case CK_Scroll_Down:
3654 case CK_Scroll_Down_Highlight:
3655 case CK_Scroll_Down_Alt_Highlight:
3656 edit->search_start = edit->curs1;
3657 edit->found_len = 0;
3658 break;
3659 default:
3660 edit->found_len = 0;
3661 edit->prev_col = edit_get_col (edit);
3662 edit->search_start = edit->curs1;
3664 edit_find_bracket (edit);
3666 if (option_auto_para_formatting)
3668 switch (command)
3670 case CK_BackSpace:
3671 case CK_Delete:
3672 case CK_Delete_Word_Left:
3673 case CK_Delete_Word_Right:
3674 case CK_Delete_To_Line_End:
3675 case CK_Delete_To_Line_Begin:
3676 format_paragraph (edit, 0);
3677 edit->force |= REDRAW_PAGE;
3683 static void
3684 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
3686 int i = 0;
3688 if (edit->macro_depth++ > 256)
3690 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3691 edit->macro_depth--;
3692 return;
3694 edit->force |= REDRAW_PAGE;
3695 for (; i < n; i++)
3697 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3699 edit_update_screen (edit);
3700 edit->macro_depth--;
3703 /* User edit menu, like user menu (F2) but only in editor. */
3704 static void
3705 user_menu (WEdit * edit)
3707 char *block_file;
3708 int nomark;
3709 long start_mark, end_mark;
3710 struct stat status;
3712 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3714 nomark = eval_marks (edit, &start_mark, &end_mark);
3715 if (nomark == 0)
3716 edit_save_block (edit, block_file, start_mark, end_mark);
3718 /* run shell scripts from menu */
3719 user_menu_cmd (edit);
3721 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
3723 int rc = 0;
3724 FILE *fd;
3726 if (nomark == 0)
3728 /* i.e. we have marked block */
3729 rc = edit_block_delete_cmd (edit);
3732 if (rc == 0)
3733 edit_insert_file (edit, block_file);
3735 /* truncate block file */
3736 fd = fopen (block_file, "w");
3737 if (fd != NULL)
3738 fclose (fd);
3740 edit_refresh_cmd (edit);
3741 edit->force |= REDRAW_COMPLETELY;
3743 g_free (block_file);
3746 void
3747 edit_stack_init (void)
3749 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3751 edit_history_moveto[edit_stack_iterator].filename = NULL;
3752 edit_history_moveto[edit_stack_iterator].line = -1;
3755 edit_stack_iterator = 0;
3758 void
3759 edit_stack_free (void)
3761 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3762 g_free (edit_history_moveto[edit_stack_iterator].filename);
3765 /* move i lines */
3766 void
3767 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3769 edit_move_updown (edit, i, do_scroll, TRUE);
3772 /* move i lines */
3773 void
3774 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3776 edit_move_updown (edit, i, do_scroll, FALSE);