Applied MC indentation policy.
[midnight-commander.git] / src / editor / edit.c
blobfadbe2623c29c34cae2867144aa1c8814f18109b
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 edit->curs2 = edit->last_byte;
332 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
333 edit->utf8 = 0;
334 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
336 GString *errmsg = g_string_new (NULL);
337 g_string_sprintf (errmsg, _(" Cannot open %s for reading "), filename);
338 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
339 g_string_free (errmsg, TRUE);
340 return 1;
343 if (!edit->buffers2[buf2])
344 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
346 mc_read (file,
347 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
348 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE);
350 for (buf = buf2 - 1; buf >= 0; buf--)
352 /* edit->buffers2[0] is already allocated */
353 if (!edit->buffers2[buf])
354 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
355 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
358 mc_close (file);
359 return 0;
362 /* detecting an error on save is easy: just check if every byte has been written. */
363 /* detecting an error on read, is not so easy 'cos there is not way to tell
364 whether you read everything or not. */
365 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
366 static const struct edit_filters
368 const char *read, *write, *extension;
369 } all_filters[] =
371 /* *INDENT-OFF* */
372 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
373 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
374 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
375 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
376 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
377 /* *INDENT-ON* */
380 /* Return index of the filter or -1 is there is no appropriate filter */
381 static int
382 edit_find_filter (const char *filename)
384 size_t i, l, e;
386 if (filename == NULL)
387 return -1;
389 l = strlen (filename);
390 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
392 e = strlen (all_filters[i].extension);
393 if (l > e)
394 if (!strcmp (all_filters[i].extension, filename + l - e))
395 return i;
397 return -1;
400 static char *
401 edit_get_filter (const char *filename)
403 int i;
404 char *p, *quoted_name;
406 i = edit_find_filter (filename);
407 if (i < 0)
408 return NULL;
410 quoted_name = name_quote (filename, 0);
411 p = g_strdup_printf (all_filters[i].read, quoted_name);
412 g_free (quoted_name);
413 return p;
416 char *
417 edit_get_write_filter (const char *write_name, const char *filename)
419 int i;
420 char *p, *writename;
422 i = edit_find_filter (filename);
423 if (i < 0)
424 return NULL;
426 writename = name_quote (write_name, 0);
427 p = g_strdup_printf (all_filters[i].write, writename);
428 g_free (writename);
429 return p;
432 static long
433 edit_insert_stream (WEdit * edit, FILE * f)
435 int c;
436 long i = 0;
437 while ((c = fgetc (f)) >= 0)
439 edit_insert (edit, c);
440 i++;
442 return i;
445 long
446 edit_write_stream (WEdit * edit, FILE * f)
448 long i;
450 if (edit->lb == LB_ASIS)
452 for (i = 0; i < edit->last_byte; i++)
453 if (fputc (edit_get_byte (edit, i), f) < 0)
454 break;
455 return i;
458 /* change line breaks */
459 for (i = 0; i < edit->last_byte; i++)
461 unsigned char c = edit_get_byte (edit, i);
463 if (!(c == '\n' || c == '\r'))
465 /* not line break */
466 if (fputc (c, f) < 0)
467 return i;
469 else
470 { /* (c == '\n' || c == '\r') */
471 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
473 switch (edit->lb)
475 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
476 /* put one line break unconditionally */
477 if (fputc ('\n', f) < 0)
478 return i;
480 i++; /* 2 chars are processed */
482 if (c == '\r' && c1 == '\n')
483 /* Windows line break; go to the next char */
484 break;
486 if (c == '\r' && c1 == '\r')
488 /* two Macintosh line breaks; put second line break */
489 if (fputc ('\n', f) < 0)
490 return i;
491 break;
494 if (fputc (c1, f) < 0)
495 return i;
496 break;
498 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
499 /* put one line break unconditionally */
500 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
501 return i;
503 if (c == '\r' && c1 == '\n')
504 /* Windows line break; go to the next char */
505 i++;
506 break;
508 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
509 /* put one line break unconditionally */
510 if (fputc ('\r', f) < 0)
511 return i;
513 i++; /* 2 chars are processed */
515 if (c == '\r' && c1 == '\n')
516 /* Windows line break; go to the next char */
517 break;
519 if (c == '\n' && c1 == '\n')
521 /* two Windows line breaks; put second line break */
522 if (fputc ('\r', f) < 0)
523 return i;
524 break;
527 if (fputc (c1, f) < 0)
528 return i;
529 break;
530 case LB_ASIS: /* default without changes */
531 break;
536 return edit->last_byte;
539 #define TEMP_BUF_LEN 1024
541 /* inserts a file at the cursor, returns 1 on success */
543 edit_insert_file (WEdit * edit, const char *filename)
545 char *p;
547 p = edit_get_filter (filename);
548 if (p != NULL)
550 FILE *f;
551 long current = edit->curs1;
552 f = (FILE *) popen (p, "r");
553 if (f != NULL)
555 edit_insert_stream (edit, f);
556 edit_cursor_move (edit, current - edit->curs1);
557 if (pclose (f) > 0)
559 char *errmsg;
560 errmsg = g_strdup_printf (_(" Error reading from pipe: %s "), p);
561 edit_error_dialog (_("Error"), errmsg);
562 g_free (errmsg);
563 g_free (p);
564 return 0;
567 else
569 char *errmsg;
570 errmsg = g_strdup_printf (_(" Cannot open pipe for reading: %s "), p);
571 edit_error_dialog (_("Error"), errmsg);
572 g_free (errmsg);
573 g_free (p);
574 return 0;
576 g_free (p);
578 else
580 int i, file, blocklen;
581 long current = edit->curs1;
582 int vertical_insertion = 0;
583 char *buf;
584 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
585 return 0;
586 buf = g_malloc0 (TEMP_BUF_LEN);
587 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
588 if (blocklen > 0)
590 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
591 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
593 vertical_insertion = 1;
595 else
597 mc_lseek (file, 0, SEEK_SET);
600 if (vertical_insertion)
602 blocklen = edit_insert_column_of_text_from_file (edit, file);
604 else
606 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
608 for (i = 0; i < blocklen; i++)
609 edit_insert (edit, buf[i]);
612 edit_cursor_move (edit, current - edit->curs1);
613 g_free (buf);
614 mc_close (file);
615 if (blocklen)
616 return 0;
618 return 1;
621 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
622 static int
623 check_file_access (WEdit * edit, const char *filename, struct stat *st)
625 int file;
626 GString *errmsg = (GString *) 0;
628 /* Try opening an existing file */
629 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
631 if (file < 0)
634 * Try creating the file. O_EXCL prevents following broken links
635 * and opening existing files.
637 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
638 if (file < 0)
640 g_string_sprintf (errmsg = g_string_new (NULL),
641 _(" Cannot open %s for reading "), filename);
642 goto cleanup;
644 else
646 /* New file, delete it if it's not modified or saved */
647 edit->delete_file = 1;
651 /* Check what we have opened */
652 if (mc_fstat (file, st) < 0)
654 g_string_sprintf (errmsg = g_string_new (NULL),
655 _(" Cannot get size/permissions for %s "), filename);
656 goto cleanup;
659 /* We want to open regular files only */
660 if (!S_ISREG (st->st_mode))
662 g_string_sprintf (errmsg = g_string_new (NULL), _(" %s is not a regular file "), filename);
663 goto cleanup;
667 * Don't delete non-empty files.
668 * O_EXCL should prevent it, but let's be on the safe side.
670 if (st->st_size > 0)
672 edit->delete_file = 0;
675 if (st->st_size >= SIZE_LIMIT)
677 g_string_sprintf (errmsg = g_string_new (NULL), _(" File %s is too large "), filename);
680 cleanup:
681 (void) mc_close (file);
682 if (errmsg)
684 edit_error_dialog (_("Error"), errmsg->str);
685 g_string_free (errmsg, TRUE);
686 return 1;
688 return 0;
692 * Open the file and load it into the buffers, either directly or using
693 * a filter. Return 0 on success, 1 on error.
695 * Fast loading (edit_load_file_fast) is used when the file size is
696 * known. In this case the data is read into the buffers by blocks.
697 * If the file size is not known, the data is loaded byte by byte in
698 * edit_insert_file.
700 static int
701 edit_load_file (WEdit * edit)
703 int fast_load = 1;
705 /* Cannot do fast load if a filter is used */
706 if (edit_find_filter (edit->filename) >= 0)
707 fast_load = 0;
710 * VFS may report file size incorrectly, and slow load is not a big
711 * deal considering overhead in VFS.
713 if (!vfs_file_is_local (edit->filename))
714 fast_load = 0;
717 * FIXME: line end translation should disable fast loading as well
718 * Consider doing fseek() to the end and ftell() for the real size.
721 if (*edit->filename)
723 /* If we are dealing with a real file, check that it exists */
724 if (check_file_access (edit, edit->filename, &edit->stat1))
725 return 1;
727 else
729 /* nothing to load */
730 fast_load = 0;
733 edit_init_buffers (edit);
735 if (fast_load)
737 edit->last_byte = edit->stat1.st_size;
738 edit_load_file_fast (edit, edit->filename);
739 /* If fast load was used, the number of lines wasn't calculated */
740 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
742 else
744 #ifdef HAVE_CHARSET
745 const char *codepage_id;
746 #endif
747 edit->last_byte = 0;
748 if (*edit->filename)
750 edit->stack_disable = 1;
751 if (!edit_insert_file (edit, edit->filename))
753 edit_clean (edit);
754 return 1;
756 edit->stack_disable = 0;
759 #ifdef HAVE_CHARSET
760 codepage_id = get_codepage_id (source_codepage);
761 if (codepage_id)
762 edit->utf8 = str_isutf8 (codepage_id);
763 #endif
765 edit->lb = LB_ASIS;
766 return 0;
769 /* Restore saved cursor position in the file */
770 static void
771 edit_load_position (WEdit * edit)
773 char *filename;
774 long line, column;
775 off_t offset;
777 if (!edit->filename || !*edit->filename)
778 return;
780 filename = vfs_canon (edit->filename);
781 load_file_position (filename, &line, &column, &offset);
782 g_free (filename);
784 if (line > 0)
786 edit_move_to_line (edit, line - 1);
787 edit->prev_col = column;
789 else if (offset > 0)
791 edit_cursor_move (edit, offset);
792 line = edit->curs_line;
794 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
795 edit_move_display (edit, line - (edit->num_widget_lines / 2));
798 /* Save cursor position in the file */
799 static void
800 edit_save_position (WEdit * edit)
802 char *filename;
804 if (!edit->filename || !*edit->filename)
805 return;
807 filename = vfs_canon (edit->filename);
808 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
809 g_free (filename);
812 /* Clean the WEdit stricture except the widget part */
813 static void
814 edit_purge_widget (WEdit * edit)
816 size_t len = sizeof (WEdit) - sizeof (Widget);
817 char *start = (char *) edit + sizeof (Widget);
818 memset (start, 0, len);
819 edit->macro_i = -1; /* not recording a macro */
822 static void
823 edit_set_keymap (void)
825 editor_map = default_editor_keymap;
826 if (editor_keymap && editor_keymap->len > 0)
827 editor_map = (global_keymap_t *) editor_keymap->data;
829 editor_x_map = default_editor_x_keymap;
830 if (editor_x_keymap && editor_x_keymap->len > 0)
831 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
835 #define space_width 1
838 * Fill in the edit structure. Return NULL on failure. Pass edit as
839 * NULL to allocate a new structure.
841 * If line is 0, try to restore saved position. Otherwise put the
842 * cursor on that line and show it in the middle of the screen.
844 WEdit *
845 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
847 int to_free = 0;
848 option_auto_syntax = 1; /* Resetting to auto on every invokation */
849 if (option_line_state)
851 option_line_state_width = LINE_STATE_WIDTH;
853 else
855 option_line_state_width = 0;
857 if (!edit)
859 #ifdef ENABLE_NLS
861 * Expand option_whole_chars_search by national letters using
862 * current locale
865 static char option_whole_chars_search_buf[256];
867 if (option_whole_chars_search_buf != option_whole_chars_search)
869 size_t i;
870 size_t len = str_term_width1 (option_whole_chars_search);
872 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
874 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
876 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
878 option_whole_chars_search_buf[len++] = i;
882 option_whole_chars_search_buf[len] = 0;
883 option_whole_chars_search = option_whole_chars_search_buf;
885 #endif /* ENABLE_NLS */
886 edit = g_malloc0 (sizeof (WEdit));
887 edit->search = NULL;
888 to_free = 1;
890 edit_purge_widget (edit);
891 edit->num_widget_lines = lines;
892 edit->over_col = 0;
893 edit->num_widget_columns = columns;
894 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
895 edit->stat1.st_uid = getuid ();
896 edit->stat1.st_gid = getgid ();
897 edit->stat1.st_mtime = 0;
898 edit->bracket = -1;
899 edit->force |= REDRAW_PAGE;
900 edit_set_filename (edit, filename);
901 edit->stack_size = START_STACK_SIZE;
902 edit->stack_size_mask = START_STACK_SIZE - 1;
903 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
904 if (edit_load_file (edit))
906 /* edit_load_file already gives an error message */
907 if (to_free)
908 g_free (edit);
909 return 0;
911 edit->utf8 = 0;
912 edit->converter = str_cnv_from_term;
913 #ifdef HAVE_CHARSET
915 const char *cp_id = NULL;
916 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
918 if (cp_id != NULL)
920 GIConv conv;
921 conv = str_crt_conv_from (cp_id);
922 if (conv != INVALID_CONV)
924 if (edit->converter != str_cnv_from_term)
925 str_close_conv (edit->converter);
926 edit->converter = conv;
929 if (cp_id != NULL)
930 edit->utf8 = str_isutf8 (cp_id);
932 #endif
934 edit->loading_done = 1;
935 edit->modified = 0;
936 edit->locked = 0;
937 edit_load_syntax (edit, 0, 0);
939 int color;
940 edit_get_syntax_color (edit, -1, &color);
943 /* load saved cursor position */
944 if ((line == 0) && option_save_position)
946 edit_load_position (edit);
948 else
950 if (line <= 0)
951 line = 1;
952 edit_move_display (edit, line - 1);
953 edit_move_to_line (edit, line - 1);
956 edit_set_keymap ();
958 return edit;
961 /* Clear the edit struct, freeing everything in it. Return 1 on success */
963 edit_clean (WEdit * edit)
965 int j = 0;
967 if (!edit)
968 return 0;
970 /* a stale lock, remove it */
971 if (edit->locked)
972 edit->locked = edit_unlock_file (edit->filename);
974 /* save cursor position */
975 if (option_save_position)
976 edit_save_position (edit);
978 /* File specified on the mcedit command line and never saved */
979 if (edit->delete_file)
980 unlink (edit->filename);
982 edit_free_syntax_rules (edit);
983 book_mark_flush (edit, -1);
984 for (; j <= MAXBUFF; j++)
986 g_free (edit->buffers1[j]);
987 g_free (edit->buffers2[j]);
990 g_free (edit->undo_stack);
991 g_free (edit->filename);
992 g_free (edit->dir);
994 mc_search_free (edit->search);
995 edit->search = NULL;
997 if (edit->converter != str_cnv_from_term)
998 str_close_conv (edit->converter);
1000 edit_purge_widget (edit);
1002 return 1;
1005 /* returns 1 on success */
1007 edit_renew (WEdit * edit)
1009 int lines = edit->num_widget_lines;
1010 int columns = edit->num_widget_columns;
1012 edit_clean (edit);
1013 return (edit_init (edit, lines, columns, "", 0) != NULL);
1017 * Load a new file into the editor. If it fails, preserve the old file.
1018 * To do it, allocate a new widget, initialize it and, if the new file
1019 * was loaded, copy the data to the old widget.
1020 * Return 1 on success, 0 on failure.
1023 edit_reload (WEdit * edit, const char *filename)
1025 WEdit *e;
1026 int lines = edit->num_widget_lines;
1027 int columns = edit->num_widget_columns;
1029 e = g_malloc0 (sizeof (WEdit));
1030 e->widget = edit->widget;
1031 if (!edit_init (e, lines, columns, filename, 0))
1033 g_free (e);
1034 return 0;
1036 edit_clean (edit);
1037 memcpy (edit, e, sizeof (WEdit));
1038 g_free (e);
1039 return 1;
1043 * Load a new file into the editor and set line. If it fails, preserve the old file.
1044 * To do it, allocate a new widget, initialize it and, if the new file
1045 * was loaded, copy the data to the old widget.
1046 * Return 1 on success, 0 on failure.
1049 edit_reload_line (WEdit * edit, const char *filename, long line)
1051 WEdit *e;
1052 int lines = edit->num_widget_lines;
1053 int columns = edit->num_widget_columns;
1055 e = g_malloc0 (sizeof (WEdit));
1056 e->widget = edit->widget;
1057 if (!edit_init (e, lines, columns, filename, line))
1059 g_free (e);
1060 return 0;
1062 edit_clean (edit);
1063 memcpy (edit, e, sizeof (WEdit));
1064 g_free (e);
1065 return 1;
1070 Recording stack for undo:
1071 The following is an implementation of a compressed stack. Identical
1072 pushes are recorded by a negative prefix indicating the number of times the
1073 same char was pushed. This saves space for repeated curs-left or curs-right
1074 delete etc.
1078 pushed: stored:
1082 b -3
1084 c --> -4
1090 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1091 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1092 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1093 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1094 position.
1096 The only way the cursor moves or the buffer is changed is through the routines:
1097 insert, backspace, insert_ahead, delete, and cursor_move.
1098 These record the reverse undo movements onto the stack each time they are
1099 called.
1101 Each key press results in a set of actions (insert; delete ...). So each time
1102 a key is pressed the current position of start_display is pushed as
1103 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1104 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1105 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1109 void
1110 edit_push_action (WEdit * edit, long c, ...)
1112 unsigned long sp = edit->stack_pointer;
1113 unsigned long spm1;
1114 long *t;
1116 /* first enlarge the stack if necessary */
1117 if (sp > edit->stack_size - 10)
1118 { /* say */
1119 if (option_max_undo < 256)
1120 option_max_undo = 256;
1121 if (edit->stack_size < (unsigned long) option_max_undo)
1123 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1124 if (t)
1126 edit->undo_stack = t;
1127 edit->stack_size <<= 1;
1128 edit->stack_size_mask = edit->stack_size - 1;
1132 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1133 if (edit->stack_disable)
1134 return;
1136 #ifdef FAST_MOVE_CURSOR
1137 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
1139 va_list ap;
1140 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1141 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1142 va_start (ap, c);
1143 c = -(va_arg (ap, int));
1144 va_end (ap);
1146 else
1147 #endif /* ! FAST_MOVE_CURSOR */
1148 if (edit->stack_bottom != sp
1149 && spm1 != edit->stack_bottom
1150 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
1152 int d;
1153 if (edit->undo_stack[spm1] < 0)
1155 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1156 if (d == c)
1158 if (edit->undo_stack[spm1] > -1000000000)
1160 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1161 edit->undo_stack[spm1]--;
1162 return;
1165 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1166 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1167 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1168 { /* a left then a right anihilate each other */
1169 if (edit->undo_stack[spm1] == -2)
1170 edit->stack_pointer = spm1;
1171 else
1172 edit->undo_stack[spm1]++;
1173 return;
1175 #endif
1177 else
1179 d = edit->undo_stack[spm1];
1180 if (d == c)
1182 if (c >= KEY_PRESS)
1183 return; /* --> no need to push multiple do-nothings */
1184 edit->undo_stack[sp] = -2;
1185 goto check_bottom;
1187 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1188 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1189 { /* a left then a right anihilate each other */
1190 edit->stack_pointer = spm1;
1191 return;
1193 #endif
1196 edit->undo_stack[sp] = c;
1198 check_bottom:
1199 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1201 /* if the sp wraps round and catches the stack_bottom then erase
1202 * the first set of actions on the stack to make space - by moving
1203 * stack_bottom forward one "key press" */
1204 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1205 if ((unsigned long) c == edit->stack_bottom ||
1206 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1209 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1211 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
1212 && edit->stack_bottom != edit->stack_pointer);
1214 /*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: */
1215 if (edit->stack_pointer != edit->stack_bottom
1216 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1217 edit->stack_bottom = edit->stack_pointer = 0;
1221 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1222 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1224 static long
1225 pop_action (WEdit * edit)
1227 long c;
1228 unsigned long sp = edit->stack_pointer;
1229 if (sp == edit->stack_bottom)
1231 return STACK_BOTTOM;
1233 sp = (sp - 1) & edit->stack_size_mask;
1234 if ((c = edit->undo_stack[sp]) >= 0)
1236 /* edit->undo_stack[sp] = '@'; */
1237 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1238 return c;
1240 if (sp == edit->stack_bottom)
1242 return STACK_BOTTOM;
1244 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1245 if (edit->undo_stack[sp] == -2)
1247 /* edit->undo_stack[sp] = '@'; */
1248 edit->stack_pointer = sp;
1250 else
1251 edit->undo_stack[sp]++;
1253 return c;
1256 /* is called whenever a modification is made by one of the four routines below */
1257 static void
1258 edit_modification (WEdit * edit)
1260 edit->caches_valid = 0;
1261 edit->screen_modified = 1;
1263 /* raise lock when file modified */
1264 if (!edit->modified && !edit->delete_file)
1265 edit->locked = edit_lock_file (edit->filename);
1266 edit->modified = 1;
1270 Basic low level single character buffer alterations and movements at the cursor.
1271 Returns char passed over, inserted or removed.
1274 void
1275 edit_insert (WEdit * edit, int c)
1277 /* check if file has grown to large */
1278 if (edit->last_byte >= SIZE_LIMIT)
1279 return;
1281 /* first we must update the position of the display window */
1282 if (edit->curs1 < edit->start_display)
1284 edit->start_display++;
1285 if (c == '\n')
1286 edit->start_line++;
1289 /* Mark file as modified, unless the file hasn't been fully loaded */
1290 if (edit->loading_done)
1292 edit_modification (edit);
1295 /* now we must update some info on the file and check if a redraw is required */
1296 if (c == '\n')
1298 if (edit->book_mark)
1299 book_mark_inc (edit, edit->curs_line);
1300 edit->curs_line++;
1301 edit->total_lines++;
1302 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1305 /* save the reverse command onto the undo stack */
1306 edit_push_action (edit, BACKSPACE);
1308 /* update markers */
1309 edit->mark1 += (edit->mark1 > edit->curs1);
1310 edit->mark2 += (edit->mark2 > edit->curs1);
1311 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1313 /* add a new buffer if we've reached the end of the last one */
1314 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1315 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1317 /* perform the insertion */
1318 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
1319 = (unsigned char) c;
1321 /* update file length */
1322 edit->last_byte++;
1324 /* update cursor position */
1325 edit->curs1++;
1328 static void
1329 edit_insert_over (WEdit * edit)
1331 int i;
1333 for (i = 0; i < edit->over_col; i++)
1335 edit_insert (edit, ' ');
1337 edit->over_col = 0;
1340 /* same as edit_insert and move left */
1341 void
1342 edit_insert_ahead (WEdit * edit, int c)
1344 if (edit->last_byte >= SIZE_LIMIT)
1345 return;
1346 if (edit->curs1 < edit->start_display)
1348 edit->start_display++;
1349 if (c == '\n')
1350 edit->start_line++;
1352 edit_modification (edit);
1353 if (c == '\n')
1355 if (edit->book_mark)
1356 book_mark_inc (edit, edit->curs_line);
1357 edit->total_lines++;
1358 edit->force |= REDRAW_AFTER_CURSOR;
1360 edit_push_action (edit, DELCHAR);
1362 edit->mark1 += (edit->mark1 >= edit->curs1);
1363 edit->mark2 += (edit->mark2 >= edit->curs1);
1364 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1366 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1367 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1368 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) -
1369 1] = c;
1371 edit->last_byte++;
1372 edit->curs2++;
1377 edit_delete (WEdit * edit, const int byte_delete)
1379 int p = 0;
1380 int cw = 1;
1381 int i;
1383 if (!edit->curs2)
1384 return 0;
1386 edit->mark1 -= (edit->mark1 > edit->curs1);
1387 edit->mark2 -= (edit->mark2 > edit->curs1);
1388 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1390 cw = 1;
1391 /* if byte_delete = 1 then delete only one byte not multibyte char */
1392 if (edit->utf8 && byte_delete == 0)
1394 edit_get_utf (edit, edit->curs1, &cw);
1395 if (cw < 1)
1396 cw = 1;
1398 for (i = 1; i <= cw; i++)
1400 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1401 ((edit->curs2 -
1402 1) & M_EDIT_BUF_SIZE) - 1];
1404 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1406 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1407 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1409 edit->last_byte--;
1410 edit->curs2--;
1411 edit_push_action (edit, p + 256);
1414 edit_modification (edit);
1415 if (p == '\n')
1417 if (edit->book_mark)
1418 book_mark_dec (edit, edit->curs_line);
1419 edit->total_lines--;
1420 edit->force |= REDRAW_AFTER_CURSOR;
1422 if (edit->curs1 < edit->start_display)
1424 edit->start_display--;
1425 if (p == '\n')
1426 edit->start_line--;
1429 return p;
1433 static int
1434 edit_backspace (WEdit * edit, const int byte_delete)
1436 int p = 0;
1437 int cw = 1;
1438 int i;
1440 if (!edit->curs1)
1441 return 0;
1443 edit->mark1 -= (edit->mark1 >= edit->curs1);
1444 edit->mark2 -= (edit->mark2 >= edit->curs1);
1445 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1447 cw = 1;
1448 if (edit->utf8 && byte_delete == 0)
1450 edit_get_prev_utf (edit, edit->curs1, &cw);
1451 if (cw < 1)
1452 cw = 1;
1454 for (i = 1; i <= cw; i++)
1456 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
1457 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1458 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1460 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1461 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1463 edit->last_byte--;
1464 edit->curs1--;
1465 edit_push_action (edit, p);
1467 edit_modification (edit);
1468 if (p == '\n')
1470 if (edit->book_mark)
1471 book_mark_dec (edit, edit->curs_line);
1472 edit->curs_line--;
1473 edit->total_lines--;
1474 edit->force |= REDRAW_AFTER_CURSOR;
1477 if (edit->curs1 < edit->start_display)
1479 edit->start_display--;
1480 if (p == '\n')
1481 edit->start_line--;
1484 return p;
1487 #ifdef FAST_MOVE_CURSOR
1489 static void
1490 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1492 unsigned long next;
1493 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
1495 edit->curs_line--;
1496 next -= (unsigned long) dest;
1497 n -= next;
1498 src += next;
1499 dest += next;
1504 edit_move_backward_lots (WEdit * edit, long increment)
1506 int r, s, t;
1507 unsigned char *p = NULL;
1509 if (increment > edit->curs1)
1510 increment = edit->curs1;
1511 if (increment <= 0)
1512 return -1;
1513 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1515 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1516 if (r > increment)
1517 r = increment;
1518 s = edit->curs1 & M_EDIT_BUF_SIZE;
1520 if (s > r)
1522 memqcpy (edit,
1523 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1524 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
1526 else
1528 if (s)
1530 memqcpy (edit,
1531 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1532 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1533 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1534 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1536 memqcpy (edit,
1537 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1538 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1539 EDIT_BUF_SIZE - (r - s), r - s);
1541 increment -= r;
1542 edit->curs1 -= r;
1543 edit->curs2 += r;
1544 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1546 if (p)
1547 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1548 else
1549 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1551 else
1553 g_free (p);
1556 s = edit->curs1 & M_EDIT_BUF_SIZE;
1557 while (increment)
1559 p = 0;
1560 r = EDIT_BUF_SIZE;
1561 if (r > increment)
1562 r = increment;
1563 t = s;
1564 if (r < t)
1565 t = r;
1566 memqcpy (edit,
1567 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1568 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
1569 if (r >= s)
1571 if (t)
1573 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1574 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1576 memqcpy (edit,
1577 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1578 EDIT_BUF_SIZE - r,
1579 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1580 EDIT_BUF_SIZE - (r - s), r - s);
1582 increment -= r;
1583 edit->curs1 -= r;
1584 edit->curs2 += r;
1585 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1587 if (p)
1588 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1589 else
1590 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1592 else
1594 g_free (p);
1597 return edit_get_byte (edit, edit->curs1);
1600 #endif /* ! FAST_MOVE_CURSOR */
1602 /* moves the cursor right or left: increment positive or negative respectively */
1603 void
1604 edit_cursor_move (WEdit * edit, long increment)
1606 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1607 int c;
1608 #ifdef FAST_MOVE_CURSOR
1609 if (increment < -256)
1611 edit->force |= REDRAW_PAGE;
1612 edit_move_backward_lots (edit, -increment);
1613 return;
1615 #endif /* ! FAST_MOVE_CURSOR */
1617 if (increment < 0)
1619 for (; increment < 0; increment++)
1621 if (!edit->curs1)
1622 return;
1624 edit_push_action (edit, CURS_RIGHT);
1626 c = edit_get_byte (edit, edit->curs1 - 1);
1627 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1628 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1629 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1630 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1631 edit->curs2++;
1632 c = edit->
1633 buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1634 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1636 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1637 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1639 edit->curs1--;
1640 if (c == '\n')
1642 edit->curs_line--;
1643 edit->force |= REDRAW_LINE_BELOW;
1648 else if (increment > 0)
1650 for (; increment > 0; increment--)
1652 if (!edit->curs2)
1653 return;
1655 edit_push_action (edit, CURS_LEFT);
1657 c = edit_get_byte (edit, edit->curs1);
1658 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1659 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1660 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1661 edit->curs1++;
1662 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1663 ((edit->curs2 -
1664 1) & M_EDIT_BUF_SIZE) - 1];
1665 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1667 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1668 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1670 edit->curs2--;
1671 if (c == '\n')
1673 edit->curs_line++;
1674 edit->force |= REDRAW_LINE_ABOVE;
1680 /* These functions return positions relative to lines */
1682 /* returns index of last char on line + 1 */
1683 long
1684 edit_eol (WEdit * edit, long current)
1686 if (current < edit->last_byte)
1688 for (;; current++)
1689 if (edit_get_byte (edit, current) == '\n')
1690 break;
1692 else
1693 return edit->last_byte;
1694 return current;
1697 /* returns index of first char on line */
1698 long
1699 edit_bol (WEdit * edit, long current)
1701 if (current > 0)
1703 for (;; current--)
1704 if (edit_get_byte (edit, current - 1) == '\n')
1705 break;
1707 else
1708 return 0;
1709 return current;
1713 long
1714 edit_count_lines (WEdit * edit, long current, long upto)
1716 long lines = 0;
1717 if (upto > edit->last_byte)
1718 upto = edit->last_byte;
1719 if (current < 0)
1720 current = 0;
1721 while (current < upto)
1722 if (edit_get_byte (edit, current++) == '\n')
1723 lines++;
1724 return lines;
1728 /* If lines is zero this returns the count of lines from current to upto. */
1729 /* If upto is zero returns index of lines forward current. */
1730 long
1731 edit_move_forward (WEdit * edit, long current, long lines, long upto)
1733 if (upto)
1735 return edit_count_lines (edit, current, upto);
1737 else
1739 long next;
1740 if (lines < 0)
1741 lines = 0;
1742 while (lines--)
1744 next = edit_eol (edit, current) + 1;
1745 if (next > edit->last_byte)
1746 break;
1747 else
1748 current = next;
1750 return current;
1755 /* Returns offset of 'lines' lines up from current */
1756 long
1757 edit_move_backward (WEdit * edit, long current, long lines)
1759 if (lines < 0)
1760 lines = 0;
1761 current = edit_bol (edit, current);
1762 while ((lines--) && current != 0)
1763 current = edit_bol (edit, current - 1);
1764 return current;
1767 /* If cols is zero this returns the count of columns from current to upto. */
1768 /* If upto is zero returns index of cols across from current. */
1769 long
1770 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1772 long p, q;
1773 int col;
1775 if (upto)
1777 q = upto;
1778 cols = -10;
1780 else
1781 q = edit->last_byte + 2;
1783 for (col = 0, p = current; p < q; p++)
1785 int c, orig_c;
1786 int utf_ch = 0;
1787 #ifdef HAVE_CHARSET
1788 int cw = 1;
1789 #endif
1790 if (cols != -10)
1792 if (col == cols)
1793 return p;
1794 if (col > cols)
1795 return p - 1;
1797 orig_c = c = edit_get_byte (edit, p);
1798 #ifdef HAVE_CHARSET
1799 if (edit->utf8)
1801 utf_ch = edit_get_utf (edit, p, &cw);
1802 if (utf8_display)
1804 if (cw > 1)
1805 col -= cw - 1;
1806 if (g_unichar_iswide (utf_ch))
1807 col++;
1809 else if (cw > 1 && g_unichar_isprint (utf_ch))
1810 col -= cw - 1;
1812 #endif
1813 c = convert_to_display_c (c);
1814 if (c == '\t')
1815 col += TAB_SIZE - col % TAB_SIZE;
1816 else if (c == '\n')
1818 if (upto)
1819 return col;
1820 else
1821 return p;
1823 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1824 /* '\r' is shown as ^M, so we must advance 2 characters */
1825 /* Caret notation for control characters */
1826 col += 2;
1827 else
1828 col++;
1830 return col;
1833 /* returns the current column position of the cursor */
1835 edit_get_col (WEdit * edit)
1837 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1841 /* Scrolling functions */
1843 void
1844 edit_update_curs_row (WEdit * edit)
1846 edit->curs_row = edit->curs_line - edit->start_line;
1849 void
1850 edit_update_curs_col (WEdit * edit)
1852 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1856 edit_get_curs_col (const WEdit * edit)
1858 return edit->curs_col;
1861 /*moves the display start position up by i lines */
1862 void
1863 edit_scroll_upward (WEdit * edit, unsigned long i)
1865 unsigned long lines_above = edit->start_line;
1866 if (i > lines_above)
1867 i = lines_above;
1868 if (i)
1870 edit->start_line -= i;
1871 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1872 edit->force |= REDRAW_PAGE;
1873 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1875 edit_update_curs_row (edit);
1879 /* returns 1 if could scroll, 0 otherwise */
1880 void
1881 edit_scroll_downward (WEdit * edit, int i)
1883 int lines_below;
1884 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1885 if (lines_below > 0)
1887 if (i > lines_below)
1888 i = lines_below;
1889 edit->start_line += i;
1890 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1891 edit->force |= REDRAW_PAGE;
1892 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1894 edit_update_curs_row (edit);
1897 void
1898 edit_scroll_right (WEdit * edit, int i)
1900 edit->force |= REDRAW_PAGE;
1901 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1902 edit->start_col -= i;
1905 void
1906 edit_scroll_left (WEdit * edit, int i)
1908 if (edit->start_col)
1910 edit->start_col += i;
1911 if (edit->start_col > 0)
1912 edit->start_col = 0;
1913 edit->force |= REDRAW_PAGE;
1914 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1918 /* high level cursor movement commands */
1920 static int
1921 is_in_indent (WEdit * edit)
1923 long p = edit_bol (edit, edit->curs1);
1924 while (p < edit->curs1)
1925 if (!strchr (" \t", edit_get_byte (edit, p++)))
1926 return 0;
1927 return 1;
1930 static int left_of_four_spaces (WEdit * edit);
1932 void
1933 edit_move_to_prev_col (WEdit * edit, long p)
1935 int prev = edit->prev_col;
1936 int over = edit->over_col;
1937 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1939 if (option_cursor_beyond_eol)
1941 long line_len =
1942 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 (edit->overwrite)
2949 if (edit_get_byte (edit, edit->curs1) != '\n')
2950 edit_delete (edit, 0);
2952 if (option_cursor_beyond_eol && edit->over_col > 0)
2953 edit_insert_over (edit);
2954 #ifdef HAVE_CHARSET
2955 if (char_for_insertion > 255 && utf8_display == 0)
2957 unsigned char str[6 + 1];
2958 size_t i = 0;
2959 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
2960 if (res == 0)
2962 str[0] = '.';
2963 str[1] = '\0';
2965 else
2967 str[res] = '\0';
2969 while (str[i] != 0 && i <= 6)
2971 char_for_insertion = str[i];
2972 edit_insert (edit, char_for_insertion);
2973 i++;
2976 else
2977 #endif
2978 edit_insert (edit, char_for_insertion);
2980 if (option_auto_para_formatting)
2982 format_paragraph (edit, 0);
2983 edit->force |= REDRAW_PAGE;
2985 else
2986 check_and_wrap_line (edit);
2987 edit->found_len = 0;
2988 edit->prev_col = edit_get_col (edit);
2989 edit->search_start = edit->curs1;
2990 edit_find_bracket (edit);
2991 return;
2994 switch (command)
2996 case CK_Begin_Page:
2997 case CK_End_Page:
2998 case CK_Begin_Page_Highlight:
2999 case CK_End_Page_Highlight:
3000 case CK_Word_Left:
3001 case CK_Word_Right:
3002 case CK_Up:
3003 case CK_Down:
3004 case CK_Left:
3005 case CK_Right:
3006 if (edit->mark2 >= 0)
3008 if (!option_persistent_selections)
3010 if (column_highlighting)
3011 edit_push_action (edit, COLUMN_ON);
3012 column_highlighting = 0;
3013 edit_mark_cmd (edit, 1);
3018 switch (command)
3020 case CK_Begin_Page:
3021 case CK_End_Page:
3022 case CK_Begin_Page_Highlight:
3023 case CK_End_Page_Highlight:
3024 case CK_Word_Left:
3025 case CK_Word_Right:
3026 case CK_Up:
3027 case CK_Down:
3028 case CK_Word_Left_Highlight:
3029 case CK_Word_Right_Highlight:
3030 case CK_Up_Highlight:
3031 case CK_Down_Highlight:
3032 case CK_Up_Alt_Highlight:
3033 case CK_Down_Alt_Highlight:
3034 if (edit->mark2 == -1)
3035 break; /*marking is following the cursor: may need to highlight a whole line */
3036 case CK_Left:
3037 case CK_Right:
3038 case CK_Left_Highlight:
3039 case CK_Right_Highlight:
3040 edit->force |= REDRAW_CHAR_ONLY;
3043 /* basic cursor key commands */
3044 switch (command)
3046 case CK_BackSpace:
3047 /* if non persistent selection and text selected */
3048 if (!option_persistent_selections)
3050 if (edit->mark1 != edit->mark2)
3052 edit_block_delete_cmd (edit);
3053 break;
3056 if (option_cursor_beyond_eol && edit->over_col > 0)
3058 edit->over_col--;
3059 break;
3061 if (option_backspace_through_tabs && is_in_indent (edit))
3063 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3064 edit_backspace (edit, 1);
3065 break;
3067 else
3069 if (option_fake_half_tabs)
3071 int i;
3072 if (is_in_indent (edit) && right_of_four_spaces (edit))
3074 for (i = 0; i < HALF_TAB_SIZE; i++)
3075 edit_backspace (edit, 1);
3076 break;
3080 edit_backspace (edit, 0);
3081 break;
3082 case CK_Delete:
3083 /* if non persistent selection and text selected */
3084 if (!option_persistent_selections)
3086 if (edit->mark1 != edit->mark2)
3088 edit_block_delete_cmd (edit);
3089 break;
3093 if (option_cursor_beyond_eol && edit->over_col > 0)
3094 edit_insert_over (edit);
3096 if (option_fake_half_tabs)
3098 int i;
3099 if (is_in_indent (edit) && left_of_four_spaces (edit))
3101 for (i = 1; i <= HALF_TAB_SIZE; i++)
3102 edit_delete (edit, 1);
3103 break;
3106 edit_delete (edit, 0);
3107 break;
3108 case CK_Delete_Word_Left:
3109 edit->over_col = 0;
3110 edit_left_delete_word (edit);
3111 break;
3112 case CK_Delete_Word_Right:
3113 if (option_cursor_beyond_eol && edit->over_col > 0)
3114 edit_insert_over (edit);
3116 edit_right_delete_word (edit);
3117 break;
3118 case CK_Delete_Line:
3119 edit_delete_line (edit);
3120 break;
3121 case CK_Delete_To_Line_End:
3122 edit_delete_to_line_end (edit);
3123 break;
3124 case CK_Delete_To_Line_Begin:
3125 edit_delete_to_line_begin (edit);
3126 break;
3127 case CK_Enter:
3128 edit->over_col = 0;
3129 if (option_auto_para_formatting)
3131 edit_double_newline (edit);
3132 if (option_return_does_auto_indent)
3133 edit_auto_indent (edit);
3134 format_paragraph (edit, 0);
3136 else
3138 edit_insert (edit, '\n');
3139 if (option_return_does_auto_indent)
3141 edit_auto_indent (edit);
3144 break;
3145 case CK_Return:
3146 edit_insert (edit, '\n');
3147 break;
3149 case CK_Page_Up_Alt_Highlight:
3150 column_highlighting = 1;
3151 case CK_Page_Up:
3152 case CK_Page_Up_Highlight:
3153 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3154 break;
3155 case CK_Page_Down_Alt_Highlight:
3156 column_highlighting = 1;
3157 case CK_Page_Down:
3158 case CK_Page_Down_Highlight:
3159 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3160 break;
3161 case CK_Left_Alt_Highlight:
3162 column_highlighting = 1;
3163 case CK_Left:
3164 case CK_Left_Highlight:
3165 if (option_fake_half_tabs)
3167 if (is_in_indent (edit) && right_of_four_spaces (edit))
3169 if (option_cursor_beyond_eol && edit->over_col > 0)
3170 edit->over_col--;
3171 else
3172 edit_cursor_move (edit, -HALF_TAB_SIZE);
3173 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3174 break;
3177 edit_left_char_move_cmd (edit);
3178 break;
3179 case CK_Right_Alt_Highlight:
3180 column_highlighting = 1;
3181 case CK_Right:
3182 case CK_Right_Highlight:
3183 if (option_fake_half_tabs)
3185 if (is_in_indent (edit) && left_of_four_spaces (edit))
3187 edit_cursor_move (edit, HALF_TAB_SIZE);
3188 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3189 break;
3192 edit_right_char_move_cmd (edit);
3193 break;
3194 case CK_Begin_Page:
3195 case CK_Begin_Page_Highlight:
3196 edit_begin_page (edit);
3197 break;
3198 case CK_End_Page:
3199 case CK_End_Page_Highlight:
3200 edit_end_page (edit);
3201 break;
3202 case CK_Word_Left:
3203 case CK_Word_Left_Highlight:
3204 edit->over_col = 0;
3205 edit_left_word_move_cmd (edit);
3206 break;
3207 case CK_Word_Right:
3208 case CK_Word_Right_Highlight:
3209 edit->over_col = 0;
3210 edit_right_word_move_cmd (edit);
3211 break;
3212 case CK_Up_Alt_Highlight:
3213 column_highlighting = 1;
3214 case CK_Up:
3215 case CK_Up_Highlight:
3216 edit_move_up (edit, 1, 0);
3217 break;
3218 case CK_Down_Alt_Highlight:
3219 column_highlighting = 1;
3220 case CK_Down:
3221 case CK_Down_Highlight:
3222 edit_move_down (edit, 1, 0);
3223 break;
3224 case CK_Paragraph_Up_Alt_Highlight:
3225 column_highlighting = 1;
3226 case CK_Paragraph_Up:
3227 case CK_Paragraph_Up_Highlight:
3228 edit_move_up_paragraph (edit, 0);
3229 break;
3230 case CK_Paragraph_Down_Alt_Highlight:
3231 column_highlighting = 1;
3232 case CK_Paragraph_Down:
3233 case CK_Paragraph_Down_Highlight:
3234 edit_move_down_paragraph (edit, 0);
3235 break;
3236 case CK_Scroll_Up_Alt_Highlight:
3237 column_highlighting = 1;
3238 case CK_Scroll_Up:
3239 case CK_Scroll_Up_Highlight:
3240 edit_move_up (edit, 1, 1);
3241 break;
3242 case CK_Scroll_Down_Alt_Highlight:
3243 column_highlighting = 1;
3244 case CK_Scroll_Down:
3245 case CK_Scroll_Down_Highlight:
3246 edit_move_down (edit, 1, 1);
3247 break;
3248 case CK_Home:
3249 case CK_Home_Highlight:
3250 edit_cursor_to_bol (edit);
3251 break;
3252 case CK_End:
3253 case CK_End_Highlight:
3254 edit_cursor_to_eol (edit);
3255 break;
3256 case CK_Tab:
3257 /* if text marked shift block */
3258 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3260 if (edit->mark2 < 0)
3261 edit_mark_cmd (edit, 0);
3262 edit_move_block_to_right (edit);
3264 else
3266 if (option_cursor_beyond_eol)
3267 edit_insert_over (edit);
3268 edit_tab_cmd (edit);
3269 if (option_auto_para_formatting)
3271 format_paragraph (edit, 0);
3272 edit->force |= REDRAW_PAGE;
3274 else
3276 check_and_wrap_line (edit);
3279 break;
3281 case CK_Toggle_Insert:
3282 edit->overwrite = (edit->overwrite == 0);
3283 break;
3285 case CK_Mark:
3286 if (edit->mark2 >= 0)
3288 if (column_highlighting)
3289 edit_push_action (edit, COLUMN_ON);
3290 column_highlighting = 0;
3292 edit_mark_cmd (edit, 0);
3293 break;
3294 case CK_Column_Mark:
3295 if (!column_highlighting)
3296 edit_push_action (edit, COLUMN_OFF);
3297 column_highlighting = 1;
3298 edit_mark_cmd (edit, 0);
3299 break;
3300 case CK_Mark_All:
3301 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3302 edit->force |= REDRAW_PAGE;
3303 break;
3304 case CK_Unmark:
3305 if (column_highlighting)
3306 edit_push_action (edit, COLUMN_ON);
3307 column_highlighting = 0;
3308 edit_mark_cmd (edit, 1);
3309 break;
3311 case CK_Toggle_Line_State:
3312 option_line_state = !option_line_state;
3313 if (option_line_state)
3315 option_line_state_width = LINE_STATE_WIDTH;
3317 else
3319 option_line_state_width = 0;
3321 edit->force |= REDRAW_PAGE;
3322 break;
3324 case CK_Toggle_Show_Margin:
3325 show_right_margin = !show_right_margin;
3326 edit->force |= REDRAW_PAGE;
3327 break;
3329 case CK_Toggle_Bookmark:
3330 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3331 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3332 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3333 else
3334 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3335 break;
3336 case CK_Flush_Bookmarks:
3337 book_mark_flush (edit, BOOK_MARK_COLOR);
3338 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3339 edit->force |= REDRAW_PAGE;
3340 break;
3341 case CK_Next_Bookmark:
3342 if (edit->book_mark)
3344 struct _book_mark *p;
3345 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3346 if (p->next)
3348 p = p->next;
3349 if (p->line >= edit->start_line + edit->num_widget_lines
3350 || p->line < edit->start_line)
3351 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3352 edit_move_to_line (edit, p->line);
3355 break;
3356 case CK_Prev_Bookmark:
3357 if (edit->book_mark)
3359 struct _book_mark *p;
3360 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3361 while (p->line == edit->curs_line)
3362 if (p->prev)
3363 p = p->prev;
3364 if (p->line >= 0)
3366 if (p->line >= edit->start_line + edit->num_widget_lines
3367 || p->line < edit->start_line)
3368 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3369 edit_move_to_line (edit, p->line);
3372 break;
3374 case CK_Beginning_Of_Text:
3375 case CK_Beginning_Of_Text_Highlight:
3376 edit_move_to_top (edit);
3377 break;
3378 case CK_End_Of_Text:
3379 case CK_End_Of_Text_Highlight:
3380 edit_move_to_bottom (edit);
3381 break;
3383 case CK_Copy:
3384 if (option_cursor_beyond_eol && edit->over_col > 0)
3385 edit_insert_over (edit);
3386 edit_block_copy_cmd (edit);
3387 break;
3388 case CK_Remove:
3389 edit_block_delete_cmd (edit);
3390 break;
3391 case CK_Move:
3392 if (option_cursor_beyond_eol && edit->over_col > 0)
3393 edit_insert_over (edit);
3394 edit_block_move_cmd (edit);
3395 break;
3397 case CK_Shift_Block_Left:
3398 if (edit->mark1 != edit->mark2)
3399 edit_move_block_to_left (edit);
3400 break;
3401 case CK_Shift_Block_Right:
3402 if (edit->mark1 != edit->mark2)
3403 edit_move_block_to_right (edit);
3404 break;
3405 case CK_XStore:
3406 edit_copy_to_X_buf_cmd (edit);
3407 break;
3408 case CK_XCut:
3409 edit_cut_to_X_buf_cmd (edit);
3410 break;
3411 case CK_XPaste:
3412 if (option_cursor_beyond_eol && edit->over_col > 0)
3413 edit_insert_over (edit);
3414 edit_paste_from_X_buf_cmd (edit);
3415 break;
3416 case CK_Selection_History:
3417 edit_paste_from_history (edit);
3418 break;
3420 case CK_Save_As:
3421 edit_save_as_cmd (edit);
3422 break;
3423 case CK_Save:
3424 edit_save_confirm_cmd (edit);
3425 break;
3426 case CK_Load:
3427 edit_load_cmd (edit, EDIT_FILE_COMMON);
3428 break;
3429 case CK_Save_Block:
3430 edit_save_block_cmd (edit);
3431 break;
3432 case CK_Insert_File:
3433 edit_insert_file_cmd (edit);
3434 break;
3436 case CK_Load_Prev_File:
3437 edit_load_back_cmd (edit);
3438 break;
3439 case CK_Load_Next_File:
3440 edit_load_forward_cmd (edit);
3441 break;
3443 case CK_Load_Syntax_File:
3444 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3445 break;
3446 case CK_Choose_Syntax:
3447 edit_syntax_dialog ();
3448 break;
3450 case CK_Load_Menu_File:
3451 edit_load_cmd (edit, EDIT_FILE_MENU);
3452 break;
3454 case CK_Toggle_Syntax:
3455 if ((option_syntax_highlighting ^= 1) == 1)
3456 edit_load_syntax (edit, NULL, option_syntax_type);
3457 edit->force |= REDRAW_PAGE;
3458 break;
3460 case CK_Toggle_Tab_TWS:
3461 enable_show_tabs_tws ^= 1;
3462 edit->force |= REDRAW_PAGE;
3463 break;
3465 case CK_Find:
3466 edit_search_cmd (edit, 0);
3467 break;
3468 case CK_Find_Again:
3469 edit_search_cmd (edit, 1);
3470 break;
3471 case CK_Replace:
3472 edit_replace_cmd (edit, 0);
3473 break;
3474 case CK_Replace_Again:
3475 edit_replace_cmd (edit, 1);
3476 break;
3477 case CK_Complete_Word:
3478 /* if text marked shift block */
3479 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3481 edit_move_block_to_left (edit);
3483 else
3485 edit_complete_word_cmd (edit);
3487 break;
3488 case CK_Find_Definition:
3489 edit_get_match_keyword_cmd (edit);
3490 break;
3491 case CK_Quit:
3492 dlg_stop (edit->widget.parent);
3493 break;
3494 case CK_New:
3495 edit_new_cmd (edit);
3496 break;
3497 case CK_Help:
3498 edit_help_cmd (edit);
3499 break;
3500 case CK_Refresh:
3501 edit_refresh_cmd (edit);
3502 break;
3503 case CK_SaveSetupCmd:
3504 save_setup_cmd ();
3505 break;
3506 case CK_About:
3507 query_dialog (_(" About "),
3508 _("\n Cooledit v3.11.5\n\n"
3509 " Copyright (C) 1996 the Free Software Foundation\n\n"
3510 " A user friendly text editor written\n"
3511 " for the Midnight Commander.\n"), D_NORMAL, 1, _("&OK"));
3512 break;
3513 case CK_LearnKeys:
3514 learn_keys ();
3515 break;
3516 case CK_Edit_Options:
3517 edit_options_dialog ();
3518 break;
3519 case CK_Edit_Save_Mode:
3520 menu_save_mode_cmd ();
3521 break;
3522 case CK_Date:
3524 char s[BUF_MEDIUM];
3525 /* fool gcc to prevent a Y2K warning */
3526 char time_format[] = "_c";
3527 time_format[0] = '%';
3529 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3530 edit_print_string (edit, s);
3531 edit->force |= REDRAW_PAGE;
3532 break;
3534 break;
3535 case CK_Goto:
3536 edit_goto_cmd (edit);
3537 break;
3538 case CK_Paragraph_Format:
3539 format_paragraph (edit, 1);
3540 edit->force |= REDRAW_PAGE;
3541 break;
3542 case CK_Delete_Macro:
3543 edit_delete_macro_cmd (edit);
3544 break;
3545 case CK_Match_Bracket:
3546 edit_goto_matching_bracket (edit);
3547 break;
3548 case CK_User_Menu:
3549 user_menu (edit);
3550 break;
3551 case CK_Sort:
3552 edit_sort_cmd (edit);
3553 break;
3554 case CK_ExtCmd:
3555 edit_ext_cmd (edit);
3556 break;
3557 case CK_Mail:
3558 edit_mail_dialog (edit);
3559 break;
3560 case CK_Shell:
3561 view_other_cmd ();
3562 break;
3563 case CK_SelectCodepage:
3564 edit_select_codepage_cmd (edit);
3565 break;
3566 case CK_Insert_Literal:
3567 edit_insert_literal_cmd (edit);
3568 break;
3569 case CK_Execute_Macro:
3570 edit_execute_macro_cmd (edit);
3571 break;
3572 case CK_Begin_End_Macro:
3573 edit_begin_end_macro_cmd (edit);
3574 break;
3575 case CK_Ext_Mode:
3576 edit->extmod = 1;
3577 break;
3578 default:
3579 break;
3582 /* CK_Pipe_Block */
3583 if ((command / 1000) == 1) /* a shell command */
3584 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3585 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3586 { /* a macro command */
3587 struct macro m[MAX_MACRO_LENGTH];
3588 int nm;
3589 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3590 edit_execute_macro (edit, m, nm);
3593 /* keys which must set the col position, and the search vars */
3594 switch (command)
3596 case CK_Find:
3597 case CK_Find_Again:
3598 case CK_Replace:
3599 case CK_Replace_Again:
3600 case CK_Complete_Word:
3601 edit->prev_col = edit_get_col (edit);
3602 break;
3603 case CK_Up:
3604 case CK_Up_Highlight:
3605 case CK_Up_Alt_Highlight:
3606 case CK_Down:
3607 case CK_Down_Highlight:
3608 case CK_Down_Alt_Highlight:
3609 case CK_Page_Up:
3610 case CK_Page_Up_Highlight:
3611 case CK_Page_Up_Alt_Highlight:
3612 case CK_Page_Down:
3613 case CK_Page_Down_Highlight:
3614 case CK_Page_Down_Alt_Highlight:
3615 case CK_Beginning_Of_Text:
3616 case CK_Beginning_Of_Text_Highlight:
3617 case CK_End_Of_Text:
3618 case CK_End_Of_Text_Highlight:
3619 case CK_Paragraph_Up:
3620 case CK_Paragraph_Up_Highlight:
3621 case CK_Paragraph_Up_Alt_Highlight:
3622 case CK_Paragraph_Down:
3623 case CK_Paragraph_Down_Highlight:
3624 case CK_Paragraph_Down_Alt_Highlight:
3625 case CK_Scroll_Up:
3626 case CK_Scroll_Up_Highlight:
3627 case CK_Scroll_Up_Alt_Highlight:
3628 case CK_Scroll_Down:
3629 case CK_Scroll_Down_Highlight:
3630 case CK_Scroll_Down_Alt_Highlight:
3631 edit->search_start = edit->curs1;
3632 edit->found_len = 0;
3633 break;
3634 default:
3635 edit->found_len = 0;
3636 edit->prev_col = edit_get_col (edit);
3637 edit->search_start = edit->curs1;
3639 edit_find_bracket (edit);
3641 if (option_auto_para_formatting)
3643 switch (command)
3645 case CK_BackSpace:
3646 case CK_Delete:
3647 case CK_Delete_Word_Left:
3648 case CK_Delete_Word_Right:
3649 case CK_Delete_To_Line_End:
3650 case CK_Delete_To_Line_Begin:
3651 format_paragraph (edit, 0);
3652 edit->force |= REDRAW_PAGE;
3658 static void
3659 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
3661 int i = 0;
3663 if (edit->macro_depth++ > 256)
3665 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3666 edit->macro_depth--;
3667 return;
3669 edit->force |= REDRAW_PAGE;
3670 for (; i < n; i++)
3672 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3674 edit_update_screen (edit);
3675 edit->macro_depth--;
3678 /* User edit menu, like user menu (F2) but only in editor. */
3679 static void
3680 user_menu (WEdit * edit)
3682 char *block_file;
3683 int nomark;
3684 long start_mark, end_mark;
3685 struct stat status;
3687 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3689 nomark = eval_marks (edit, &start_mark, &end_mark);
3690 if (nomark == 0)
3691 edit_save_block (edit, block_file, start_mark, end_mark);
3693 /* run shell scripts from menu */
3694 user_menu_cmd (edit);
3696 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
3698 int rc = 0;
3699 FILE *fd;
3701 if (nomark == 0)
3703 /* i.e. we have marked block */
3704 rc = edit_block_delete_cmd (edit);
3707 if (rc == 0)
3708 edit_insert_file (edit, block_file);
3710 /* truncate block file */
3711 fd = fopen (block_file, "w");
3712 if (fd != NULL)
3713 fclose (fd);
3715 edit_refresh_cmd (edit);
3716 edit->force |= REDRAW_COMPLETELY;
3718 g_free (block_file);
3721 void
3722 edit_stack_init (void)
3724 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3726 edit_history_moveto[edit_stack_iterator].filename = NULL;
3727 edit_history_moveto[edit_stack_iterator].line = -1;
3730 edit_stack_iterator = 0;
3733 void
3734 edit_stack_free (void)
3736 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3737 g_free (edit_history_moveto[edit_stack_iterator].filename);
3740 /* move i lines */
3741 void
3742 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3744 edit_move_updown (edit, i, do_scroll, TRUE);
3747 /* move i lines */
3748 void
3749 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3751 edit_move_updown (edit, i, do_scroll, FALSE);