Ticket #2196: editor hangup if editor_tab_spacing is equalt to 0.
[free-mc.git] / src / editor / edit.c
blobd875a282e22156d3f25da6787e1174c21bbb3b4c
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"
60 #include "src/keybind.h"
62 #include "edit-impl.h"
63 #include "editlock.h"
64 #include "edit-widget.h"
66 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
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 static void user_menu (WEdit * edit);
128 edit_get_byte (WEdit * edit, long byte_index)
130 unsigned long p;
131 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
132 return '\n';
134 if (byte_index >= edit->curs1)
136 p = edit->curs1 + edit->curs2 - byte_index - 1;
137 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
139 else
141 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
145 char *
146 edit_get_byte_ptr (WEdit * edit, long byte_index)
148 unsigned long p;
149 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
150 return NULL;
152 if (byte_index >= edit->curs1)
154 p = edit->curs1 + edit->curs2 - byte_index - 1;
155 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
156 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
158 else
160 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
161 (byte_index & M_EDIT_BUF_SIZE));
165 char *
166 edit_get_buf_ptr (WEdit * edit, long byte_index)
168 unsigned long p;
170 if (byte_index >= (edit->curs1 + edit->curs2))
171 byte_index--;
173 if (byte_index < 0)
174 return NULL;
176 if (byte_index >= edit->curs1)
178 p = edit->curs1 + edit->curs2 - 1;
179 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
180 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
182 else
184 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
189 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
191 gchar *str = NULL;
192 int res = -1;
193 gunichar ch;
194 gchar *next_ch = NULL;
195 int width = 0;
197 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
199 *char_width = 0;
200 return '\n';
203 str = edit_get_byte_ptr (edit, byte_index);
205 if (str == NULL)
207 *char_width = 0;
208 return 0;
211 res = g_utf8_get_char_validated (str, -1);
213 if (res < 0)
215 ch = *str;
216 width = 0;
218 else
220 ch = res;
221 /* Calculate UTF-8 char width */
222 next_ch = g_utf8_next_char (str);
223 if (next_ch)
225 width = next_ch - str;
227 else
229 ch = 0;
230 width = 0;
233 *char_width = width;
234 return ch;
238 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
240 gchar *str, *buf = NULL;
241 int res = -1;
242 gunichar ch;
243 gchar *next_ch = NULL;
244 int width = 0;
246 if (byte_index > 0)
247 byte_index--;
249 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
251 *char_width = 0;
252 return 0;
255 ch = edit_get_utf (edit, byte_index, &width);
257 if (width == 1)
259 *char_width = width;
260 return ch;
263 str = edit_get_byte_ptr (edit, byte_index);
264 buf = edit_get_buf_ptr (edit, byte_index);
265 if (str == NULL || buf == NULL)
267 *char_width = 0;
268 return 0;
270 /* get prev utf8 char */
271 if (str != buf)
272 str = g_utf8_find_prev_char (buf, str);
274 res = g_utf8_get_char_validated (str, -1);
276 if (res < 0)
278 ch = *str;
279 width = 0;
281 else
283 ch = res;
284 /* Calculate UTF-8 char width */
285 next_ch = g_utf8_next_char (str);
286 if (next_ch)
288 width = next_ch - str;
290 else
292 ch = 0;
293 width = 0;
296 *char_width = width;
297 return ch;
301 * Initialize the buffers for an empty files.
303 static void
304 edit_init_buffers (WEdit * edit)
306 int j;
308 for (j = 0; j <= MAXBUFF; j++)
310 edit->buffers1[j] = NULL;
311 edit->buffers2[j] = NULL;
314 edit->curs1 = 0;
315 edit->curs2 = 0;
316 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
320 * Load file OR text into buffers. Set cursor to the beginning of file.
321 * Return 1 on error.
323 static int
324 edit_load_file_fast (WEdit * edit, const char *filename)
326 long buf, buf2;
327 int file = -1;
328 int ret = 1;
330 edit->curs2 = edit->last_byte;
331 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
333 file = mc_open (filename, O_RDONLY | O_BINARY);
334 if (file == -1)
336 gchar *errmsg;
338 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
339 edit_error_dialog (_("Error"), errmsg);
340 g_free (errmsg);
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)
367 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
368 edit_error_dialog (_("Error"), err_str);
369 g_free (err_str);
371 mc_close (file);
372 return ret;
375 /* detecting an error on save is easy: just check if every byte has been written. */
376 /* detecting an error on read, is not so easy 'cos there is not way to tell
377 whether you read everything or not. */
378 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
379 static const struct edit_filters
381 const char *read, *write, *extension;
382 } all_filters[] =
384 /* *INDENT-OFF* */
385 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
386 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
387 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
388 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
389 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
390 /* *INDENT-ON* */
393 /* Return index of the filter or -1 is there is no appropriate filter */
394 static int
395 edit_find_filter (const char *filename)
397 size_t i, l, e;
399 if (filename == NULL)
400 return -1;
402 l = strlen (filename);
403 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
405 e = strlen (all_filters[i].extension);
406 if (l > e)
407 if (!strcmp (all_filters[i].extension, filename + l - e))
408 return i;
410 return -1;
413 static char *
414 edit_get_filter (const char *filename)
416 int i;
417 char *p, *quoted_name;
419 i = edit_find_filter (filename);
420 if (i < 0)
421 return NULL;
423 quoted_name = name_quote (filename, 0);
424 p = g_strdup_printf (all_filters[i].read, quoted_name);
425 g_free (quoted_name);
426 return p;
429 char *
430 edit_get_write_filter (const char *write_name, const char *filename)
432 int i;
433 char *p, *writename;
435 i = edit_find_filter (filename);
436 if (i < 0)
437 return NULL;
439 writename = name_quote (write_name, 0);
440 p = g_strdup_printf (all_filters[i].write, writename);
441 g_free (writename);
442 return p;
445 static long
446 edit_insert_stream (WEdit * edit, FILE * f)
448 int c;
449 long i = 0;
450 while ((c = fgetc (f)) >= 0)
452 edit_insert (edit, c);
453 i++;
455 return i;
458 long
459 edit_write_stream (WEdit * edit, FILE * f)
461 long i;
463 if (edit->lb == LB_ASIS)
465 for (i = 0; i < edit->last_byte; i++)
466 if (fputc (edit_get_byte (edit, i), f) < 0)
467 break;
468 return i;
471 /* change line breaks */
472 for (i = 0; i < edit->last_byte; i++)
474 unsigned char c = edit_get_byte (edit, i);
476 if (!(c == '\n' || c == '\r'))
478 /* not line break */
479 if (fputc (c, f) < 0)
480 return i;
482 else
483 { /* (c == '\n' || c == '\r') */
484 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
486 switch (edit->lb)
488 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
489 /* put one line break unconditionally */
490 if (fputc ('\n', f) < 0)
491 return i;
493 i++; /* 2 chars are processed */
495 if (c == '\r' && c1 == '\n')
496 /* Windows line break; go to the next char */
497 break;
499 if (c == '\r' && c1 == '\r')
501 /* two Macintosh line breaks; put second line break */
502 if (fputc ('\n', f) < 0)
503 return i;
504 break;
507 if (fputc (c1, f) < 0)
508 return i;
509 break;
511 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
512 /* put one line break unconditionally */
513 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
514 return i;
516 if (c == '\r' && c1 == '\n')
517 /* Windows line break; go to the next char */
518 i++;
519 break;
521 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
522 /* put one line break unconditionally */
523 if (fputc ('\r', f) < 0)
524 return i;
526 i++; /* 2 chars are processed */
528 if (c == '\r' && c1 == '\n')
529 /* Windows line break; go to the next char */
530 break;
532 if (c == '\n' && c1 == '\n')
534 /* two Windows line breaks; put second line break */
535 if (fputc ('\r', f) < 0)
536 return i;
537 break;
540 if (fputc (c1, f) < 0)
541 return i;
542 break;
543 case LB_ASIS: /* default without changes */
544 break;
549 return edit->last_byte;
552 #define TEMP_BUF_LEN 1024
554 /* inserts a file at the cursor, returns 1 on success */
556 edit_insert_file (WEdit * edit, const char *filename)
558 char *p;
560 p = edit_get_filter (filename);
561 if (p != NULL)
563 FILE *f;
564 long current = edit->curs1;
565 f = (FILE *) popen (p, "r");
566 if (f != NULL)
568 edit_insert_stream (edit, f);
569 edit_cursor_move (edit, current - edit->curs1);
570 if (pclose (f) > 0)
572 char *errmsg;
573 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
574 edit_error_dialog (_("Error"), errmsg);
575 g_free (errmsg);
576 g_free (p);
577 return 0;
580 else
582 char *errmsg;
583 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
584 edit_error_dialog (_("Error"), errmsg);
585 g_free (errmsg);
586 g_free (p);
587 return 0;
589 g_free (p);
591 else
593 int i, file, blocklen;
594 long current = edit->curs1;
595 int vertical_insertion = 0;
596 char *buf;
597 file = mc_open (filename, O_RDONLY | O_BINARY);
598 if (file == -1)
599 return 0;
600 buf = g_malloc0 (TEMP_BUF_LEN);
601 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
602 if (blocklen > 0)
604 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
605 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
606 vertical_insertion = 1;
607 else
608 mc_lseek (file, 0, SEEK_SET);
610 if (vertical_insertion)
611 blocklen = edit_insert_column_of_text_from_file (edit, file);
612 else
613 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
614 for (i = 0; i < blocklen; i++)
615 edit_insert (edit, buf[i]);
616 edit_cursor_move (edit, current - edit->curs1);
617 g_free (buf);
618 mc_close (file);
619 if (blocklen != 0)
620 return 0;
622 return 1;
625 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
626 static int
627 check_file_access (WEdit * edit, const char *filename, struct stat *st)
629 int file;
630 gchar *errmsg = NULL;
632 /* Try opening an existing file */
633 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
635 if (file < 0)
638 * Try creating the file. O_EXCL prevents following broken links
639 * and opening existing files.
641 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
642 if (file < 0)
644 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
645 goto cleanup;
647 else
649 /* New file, delete it if it's not modified or saved */
650 edit->delete_file = 1;
654 /* Check what we have opened */
655 if (mc_fstat (file, st) < 0)
657 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
658 goto cleanup;
661 /* We want to open regular files only */
662 if (!S_ISREG (st->st_mode))
664 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
665 goto cleanup;
669 * Don't delete non-empty files.
670 * O_EXCL should prevent it, but let's be on the safe side.
672 if (st->st_size > 0)
673 edit->delete_file = 0;
675 if (st->st_size >= SIZE_LIMIT)
676 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
678 cleanup:
679 (void) mc_close (file);
681 if (errmsg != NULL)
683 edit_error_dialog (_("Error"), errmsg);
684 g_free (errmsg);
685 return 1;
687 return 0;
691 * Open the file and load it into the buffers, either directly or using
692 * a filter. Return 0 on success, 1 on error.
694 * Fast loading (edit_load_file_fast) is used when the file size is
695 * known. In this case the data is read into the buffers by blocks.
696 * If the file size is not known, the data is loaded byte by byte in
697 * edit_insert_file.
699 static int
700 edit_load_file (WEdit * edit)
702 int fast_load = 1;
704 /* Cannot do fast load if a filter is used */
705 if (edit_find_filter (edit->filename) >= 0)
706 fast_load = 0;
709 * VFS may report file size incorrectly, and slow load is not a big
710 * deal considering overhead in VFS.
712 if (!vfs_file_is_local (edit->filename))
713 fast_load = 0;
716 * FIXME: line end translation should disable fast loading as well
717 * Consider doing fseek() to the end and ftell() for the real size.
720 if (*edit->filename)
722 /* If we are dealing with a real file, check that it exists */
723 if (check_file_access (edit, edit->filename, &edit->stat1))
724 return 1;
726 else
728 /* nothing to load */
729 fast_load = 0;
732 edit_init_buffers (edit);
734 if (fast_load)
736 edit->last_byte = edit->stat1.st_size;
737 edit_load_file_fast (edit, edit->filename);
738 /* If fast load was used, the number of lines wasn't calculated */
739 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
741 else
743 #ifdef HAVE_CHARSET
744 const char *codepage_id;
745 #endif
746 edit->last_byte = 0;
747 if (*edit->filename)
749 edit->stack_disable = 1;
750 if (!edit_insert_file (edit, edit->filename))
752 edit_clean (edit);
753 return 1;
755 edit->stack_disable = 0;
758 #ifdef HAVE_CHARSET
759 codepage_id = get_codepage_id (source_codepage);
760 if (codepage_id)
761 edit->utf8 = str_isutf8 (codepage_id);
762 #endif
764 edit->lb = LB_ASIS;
765 return 0;
768 /* Restore saved cursor position in the file */
769 static void
770 edit_load_position (WEdit * edit)
772 char *filename;
773 long line, column;
774 off_t offset;
776 if (!edit->filename || !*edit->filename)
777 return;
779 filename = vfs_canon (edit->filename);
780 load_file_position (filename, &line, &column, &offset);
781 g_free (filename);
783 if (line > 0)
785 edit_move_to_line (edit, line - 1);
786 edit->prev_col = column;
788 else if (offset > 0)
790 edit_cursor_move (edit, offset);
791 line = edit->curs_line;
793 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
794 edit_move_display (edit, line - (edit->num_widget_lines / 2));
797 /* Save cursor position in the file */
798 static void
799 edit_save_position (WEdit * edit)
801 char *filename;
803 if (!edit->filename || !*edit->filename)
804 return;
806 filename = vfs_canon (edit->filename);
807 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
808 g_free (filename);
811 /* Clean the WEdit stricture except the widget part */
812 static void
813 edit_purge_widget (WEdit * edit)
815 size_t len = sizeof (WEdit) - sizeof (Widget);
816 char *start = (char *) edit + sizeof (Widget);
817 memset (start, 0, len);
818 edit->macro_i = -1; /* not recording a macro */
821 static void
822 edit_set_keymap (void)
824 editor_map = default_editor_keymap;
825 if (editor_keymap && editor_keymap->len > 0)
826 editor_map = (global_keymap_t *) editor_keymap->data;
828 editor_x_map = default_editor_x_keymap;
829 if (editor_x_keymap && editor_x_keymap->len > 0)
830 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
834 #define space_width 1
837 * Fill in the edit structure. Return NULL on failure. Pass edit as
838 * NULL to allocate a new structure.
840 * If line is 0, try to restore saved position. Otherwise put the
841 * cursor on that line and show it in the middle of the screen.
843 WEdit *
844 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
846 int to_free = 0;
847 option_auto_syntax = 1; /* Resetting to auto on every invokation */
848 if (option_line_state)
850 option_line_state_width = LINE_STATE_WIDTH;
852 else
854 option_line_state_width = 0;
856 if (!edit)
858 #ifdef ENABLE_NLS
860 * Expand option_whole_chars_search by national letters using
861 * current locale
864 static char option_whole_chars_search_buf[256];
866 if (option_whole_chars_search_buf != option_whole_chars_search)
868 size_t i;
869 size_t len = str_term_width1 (option_whole_chars_search);
871 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
873 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
875 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
877 option_whole_chars_search_buf[len++] = i;
881 option_whole_chars_search_buf[len] = 0;
882 option_whole_chars_search = option_whole_chars_search_buf;
884 #endif /* ENABLE_NLS */
885 edit = g_malloc0 (sizeof (WEdit));
886 edit->search = NULL;
887 to_free = 1;
889 edit_purge_widget (edit);
890 edit->num_widget_lines = lines;
891 edit->over_col = 0;
892 edit->num_widget_columns = columns;
893 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
894 edit->stat1.st_uid = getuid ();
895 edit->stat1.st_gid = getgid ();
896 edit->stat1.st_mtime = 0;
897 edit->bracket = -1;
898 edit->force |= REDRAW_PAGE;
899 edit_set_filename (edit, filename);
900 edit->stack_size = START_STACK_SIZE;
901 edit->stack_size_mask = START_STACK_SIZE - 1;
902 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
904 edit->utf8 = 0;
905 edit->converter = str_cnv_from_term;
906 edit_set_codeset (edit);
908 if (edit_load_file (edit))
910 /* edit_load_file already gives an error message */
911 if (to_free)
912 g_free (edit);
913 return 0;
916 edit->loading_done = 1;
917 edit->modified = 0;
918 edit->locked = 0;
919 edit_load_syntax (edit, NULL, NULL);
921 int color;
922 edit_get_syntax_color (edit, -1, &color);
925 /* load saved cursor position */
926 if ((line == 0) && option_save_position)
928 edit_load_position (edit);
930 else
932 if (line <= 0)
933 line = 1;
934 edit_move_display (edit, line - 1);
935 edit_move_to_line (edit, line - 1);
938 edit_set_keymap ();
940 return edit;
943 /* Clear the edit struct, freeing everything in it. Return 1 on success */
945 edit_clean (WEdit * edit)
947 int j = 0;
949 if (!edit)
950 return 0;
952 /* a stale lock, remove it */
953 if (edit->locked)
954 edit->locked = edit_unlock_file (edit->filename);
956 /* save cursor position */
957 if (option_save_position)
958 edit_save_position (edit);
960 /* File specified on the mcedit command line and never saved */
961 if (edit->delete_file)
962 unlink (edit->filename);
964 edit_free_syntax_rules (edit);
965 book_mark_flush (edit, -1);
966 for (; j <= MAXBUFF; j++)
968 g_free (edit->buffers1[j]);
969 g_free (edit->buffers2[j]);
972 g_free (edit->undo_stack);
973 g_free (edit->filename);
974 g_free (edit->dir);
976 mc_search_free (edit->search);
977 edit->search = NULL;
979 if (edit->converter != str_cnv_from_term)
980 str_close_conv (edit->converter);
982 edit_purge_widget (edit);
984 return 1;
987 /* returns 1 on success */
989 edit_renew (WEdit * edit)
991 int lines = edit->num_widget_lines;
992 int columns = edit->num_widget_columns;
994 edit_clean (edit);
995 return (edit_init (edit, lines, columns, "", 0) != NULL);
999 * Load a new file into the editor. If it fails, preserve the old file.
1000 * To do it, allocate a new widget, initialize it and, if the new file
1001 * was loaded, copy the data to the old widget.
1002 * Return 1 on success, 0 on failure.
1005 edit_reload (WEdit * edit, const char *filename)
1007 WEdit *e;
1008 int lines = edit->num_widget_lines;
1009 int columns = edit->num_widget_columns;
1011 e = g_malloc0 (sizeof (WEdit));
1012 e->widget = edit->widget;
1013 if (!edit_init (e, lines, columns, filename, 0))
1015 g_free (e);
1016 return 0;
1018 edit_clean (edit);
1019 memcpy (edit, e, sizeof (WEdit));
1020 g_free (e);
1021 return 1;
1025 * Load a new file into the editor and set line. If it fails, preserve the old file.
1026 * To do it, allocate a new widget, initialize it and, if the new file
1027 * was loaded, copy the data to the old widget.
1028 * Return 1 on success, 0 on failure.
1031 edit_reload_line (WEdit * edit, const char *filename, long line)
1033 WEdit *e;
1034 int lines = edit->num_widget_lines;
1035 int columns = edit->num_widget_columns;
1037 e = g_malloc0 (sizeof (WEdit));
1038 e->widget = edit->widget;
1039 if (!edit_init (e, lines, columns, filename, line))
1041 g_free (e);
1042 return 0;
1044 edit_clean (edit);
1045 memcpy (edit, e, sizeof (WEdit));
1046 g_free (e);
1047 return 1;
1050 void
1051 edit_set_codeset (WEdit *edit)
1053 #ifdef HAVE_CHARSET
1054 const char *cp_id;
1056 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
1058 if (cp_id != NULL)
1060 GIConv conv;
1061 conv = str_crt_conv_from (cp_id);
1062 if (conv != INVALID_CONV)
1064 if (edit->converter != str_cnv_from_term)
1065 str_close_conv (edit->converter);
1066 edit->converter = conv;
1070 if (cp_id != NULL)
1071 edit->utf8 = str_isutf8 (cp_id);
1072 #else
1073 (void) edit;
1074 #endif
1079 Recording stack for undo:
1080 The following is an implementation of a compressed stack. Identical
1081 pushes are recorded by a negative prefix indicating the number of times the
1082 same char was pushed. This saves space for repeated curs-left or curs-right
1083 delete etc.
1087 pushed: stored:
1091 b -3
1093 c --> -4
1099 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1100 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1101 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1102 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1103 position.
1105 The only way the cursor moves or the buffer is changed is through the routines:
1106 insert, backspace, insert_ahead, delete, and cursor_move.
1107 These record the reverse undo movements onto the stack each time they are
1108 called.
1110 Each key press results in a set of actions (insert; delete ...). So each time
1111 a key is pressed the current position of start_display is pushed as
1112 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1113 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1114 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1118 void
1119 edit_push_action (WEdit * edit, long c, ...)
1121 unsigned long sp = edit->stack_pointer;
1122 unsigned long spm1;
1123 long *t;
1125 /* first enlarge the stack if necessary */
1126 if (sp > edit->stack_size - 10)
1127 { /* say */
1128 if (option_max_undo < 256)
1129 option_max_undo = 256;
1130 if (edit->stack_size < (unsigned long) option_max_undo)
1132 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1133 if (t)
1135 edit->undo_stack = t;
1136 edit->stack_size <<= 1;
1137 edit->stack_size_mask = edit->stack_size - 1;
1141 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1142 if (edit->stack_disable)
1143 return;
1145 #ifdef FAST_MOVE_CURSOR
1146 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
1148 va_list ap;
1149 edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
1150 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1151 va_start (ap, c);
1152 c = -(va_arg (ap, int));
1153 va_end (ap);
1155 else
1156 #endif /* ! FAST_MOVE_CURSOR */
1157 if (edit->stack_bottom != sp
1158 && spm1 != edit->stack_bottom
1159 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
1161 int d;
1162 if (edit->undo_stack[spm1] < 0)
1164 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1165 if (d == c)
1167 if (edit->undo_stack[spm1] > -1000000000)
1169 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1170 edit->undo_stack[spm1]--;
1171 return;
1174 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1175 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1176 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1177 { /* a left then a right anihilate each other */
1178 if (edit->undo_stack[spm1] == -2)
1179 edit->stack_pointer = spm1;
1180 else
1181 edit->undo_stack[spm1]++;
1182 return;
1184 #endif
1186 else
1188 d = edit->undo_stack[spm1];
1189 if (d == c)
1191 if (c >= KEY_PRESS)
1192 return; /* --> no need to push multiple do-nothings */
1193 edit->undo_stack[sp] = -2;
1194 goto check_bottom;
1196 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1197 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
1198 { /* a left then a right anihilate each other */
1199 edit->stack_pointer = spm1;
1200 return;
1202 #endif
1205 edit->undo_stack[sp] = c;
1207 check_bottom:
1208 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1210 /* if the sp wraps round and catches the stack_bottom then erase
1211 * the first set of actions on the stack to make space - by moving
1212 * stack_bottom forward one "key press" */
1213 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1214 if ((unsigned long) c == edit->stack_bottom ||
1215 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1218 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1220 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
1221 && edit->stack_bottom != edit->stack_pointer);
1223 /*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: */
1224 if (edit->stack_pointer != edit->stack_bottom
1225 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1226 edit->stack_bottom = edit->stack_pointer = 0;
1230 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1231 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1233 static long
1234 pop_action (WEdit * edit)
1236 long c;
1237 unsigned long sp = edit->stack_pointer;
1239 if (sp == edit->stack_bottom)
1240 return STACK_BOTTOM;
1242 sp = (sp - 1) & edit->stack_size_mask;
1243 c = edit->undo_stack[sp];
1244 if (c >= 0)
1246 /* edit->undo_stack[sp] = '@'; */
1247 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1248 return c;
1251 if (sp == edit->stack_bottom)
1252 return STACK_BOTTOM;
1254 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1255 if (edit->undo_stack[sp] == -2)
1257 /* edit->undo_stack[sp] = '@'; */
1258 edit->stack_pointer = sp;
1260 else
1261 edit->undo_stack[sp]++;
1263 return c;
1266 /* is called whenever a modification is made by one of the four routines below */
1267 static void
1268 edit_modification (WEdit * edit)
1270 edit->caches_valid = 0;
1271 edit->screen_modified = 1;
1273 /* raise lock when file modified */
1274 if (!edit->modified && !edit->delete_file)
1275 edit->locked = edit_lock_file (edit->filename);
1276 edit->modified = 1;
1280 Basic low level single character buffer alterations and movements at the cursor.
1281 Returns char passed over, inserted or removed.
1284 void
1285 edit_insert (WEdit * edit, int c)
1287 /* check if file has grown to large */
1288 if (edit->last_byte >= SIZE_LIMIT)
1289 return;
1291 /* first we must update the position of the display window */
1292 if (edit->curs1 < edit->start_display)
1294 edit->start_display++;
1295 if (c == '\n')
1296 edit->start_line++;
1299 /* Mark file as modified, unless the file hasn't been fully loaded */
1300 if (edit->loading_done)
1302 edit_modification (edit);
1305 /* now we must update some info on the file and check if a redraw is required */
1306 if (c == '\n')
1308 if (edit->book_mark)
1309 book_mark_inc (edit, edit->curs_line);
1310 edit->curs_line++;
1311 edit->total_lines++;
1312 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1315 /* save the reverse command onto the undo stack */
1316 edit_push_action (edit, BACKSPACE);
1318 /* update markers */
1319 edit->mark1 += (edit->mark1 > edit->curs1);
1320 edit->mark2 += (edit->mark2 > edit->curs1);
1321 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1323 /* add a new buffer if we've reached the end of the last one */
1324 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1325 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1327 /* perform the insertion */
1328 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
1329 = (unsigned char) c;
1331 /* update file length */
1332 edit->last_byte++;
1334 /* update cursor position */
1335 edit->curs1++;
1338 static void
1339 edit_insert_over (WEdit * edit)
1341 int i;
1343 for (i = 0; i < edit->over_col; i++)
1345 edit_insert (edit, ' ');
1347 edit->over_col = 0;
1350 /* same as edit_insert and move left */
1351 void
1352 edit_insert_ahead (WEdit * edit, int c)
1354 if (edit->last_byte >= SIZE_LIMIT)
1355 return;
1357 if (edit->curs1 < edit->start_display)
1359 edit->start_display++;
1360 if (c == '\n')
1361 edit->start_line++;
1363 edit_modification (edit);
1364 if (c == '\n')
1366 if (edit->book_mark)
1367 book_mark_inc (edit, edit->curs_line);
1368 edit->total_lines++;
1369 edit->force |= REDRAW_AFTER_CURSOR;
1371 edit_push_action (edit, DELCHAR);
1373 edit->mark1 += (edit->mark1 >= edit->curs1);
1374 edit->mark2 += (edit->mark2 >= edit->curs1);
1375 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1377 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1378 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1379 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
1380 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1382 edit->last_byte++;
1383 edit->curs2++;
1388 edit_delete (WEdit * edit, const int byte_delete)
1390 int p = 0;
1391 int cw = 1;
1392 int i;
1394 if (!edit->curs2)
1395 return 0;
1397 edit->mark1 -= (edit->mark1 > edit->curs1);
1398 edit->mark2 -= (edit->mark2 > edit->curs1);
1399 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1401 cw = 1;
1402 /* if byte_delete = 1 then delete only one byte not multibyte char */
1403 if (edit->utf8 && byte_delete == 0)
1405 edit_get_utf (edit, edit->curs1, &cw);
1406 if (cw < 1)
1407 cw = 1;
1409 for (i = 1; i <= cw; i++)
1411 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1412 ((edit->curs2 -
1413 1) & M_EDIT_BUF_SIZE) - 1];
1415 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1417 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1418 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1420 edit->last_byte--;
1421 edit->curs2--;
1422 edit_push_action (edit, p + 256);
1425 edit_modification (edit);
1426 if (p == '\n')
1428 if (edit->book_mark)
1429 book_mark_dec (edit, edit->curs_line);
1430 edit->total_lines--;
1431 edit->force |= REDRAW_AFTER_CURSOR;
1433 if (edit->curs1 < edit->start_display)
1435 edit->start_display--;
1436 if (p == '\n')
1437 edit->start_line--;
1440 return p;
1444 static int
1445 edit_backspace (WEdit * edit, const int byte_delete)
1447 int p = 0;
1448 int cw = 1;
1449 int i;
1451 if (!edit->curs1)
1452 return 0;
1454 edit->mark1 -= (edit->mark1 >= edit->curs1);
1455 edit->mark2 -= (edit->mark2 >= edit->curs1);
1456 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1458 cw = 1;
1459 if (edit->utf8 && byte_delete == 0)
1461 edit_get_prev_utf (edit, edit->curs1, &cw);
1462 if (cw < 1)
1463 cw = 1;
1465 for (i = 1; i <= cw; i++)
1467 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
1468 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1469 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1471 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1472 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1474 edit->last_byte--;
1475 edit->curs1--;
1476 edit_push_action (edit, p);
1478 edit_modification (edit);
1479 if (p == '\n')
1481 if (edit->book_mark)
1482 book_mark_dec (edit, edit->curs_line);
1483 edit->curs_line--;
1484 edit->total_lines--;
1485 edit->force |= REDRAW_AFTER_CURSOR;
1488 if (edit->curs1 < edit->start_display)
1490 edit->start_display--;
1491 if (p == '\n')
1492 edit->start_line--;
1495 return p;
1498 #ifdef FAST_MOVE_CURSOR
1500 static void
1501 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1503 unsigned long next;
1504 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
1506 edit->curs_line--;
1507 next -= (unsigned long) dest;
1508 n -= next;
1509 src += next;
1510 dest += next;
1515 edit_move_backward_lots (WEdit * edit, long increment)
1517 int r, s, t;
1518 unsigned char *p = NULL;
1520 if (increment > edit->curs1)
1521 increment = edit->curs1;
1522 if (increment <= 0)
1523 return -1;
1524 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1526 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1527 if (r > increment)
1528 r = increment;
1529 s = edit->curs1 & M_EDIT_BUF_SIZE;
1531 if (s > r)
1533 memqcpy (edit,
1534 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1535 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
1537 else
1539 if (s != 0)
1541 memqcpy (edit,
1542 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1543 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1544 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1545 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1547 memqcpy (edit,
1548 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1549 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1550 EDIT_BUF_SIZE - (r - s), r - s);
1552 increment -= r;
1553 edit->curs1 -= r;
1554 edit->curs2 += r;
1555 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1557 if (p)
1558 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1559 else
1560 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1562 else
1564 g_free (p);
1567 s = edit->curs1 & M_EDIT_BUF_SIZE;
1568 while (increment)
1570 p = 0;
1571 r = EDIT_BUF_SIZE;
1572 if (r > increment)
1573 r = increment;
1574 t = s;
1575 if (r < t)
1576 t = r;
1577 memqcpy (edit,
1578 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1579 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
1580 if (r >= s)
1582 if (t)
1584 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1585 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1587 memqcpy (edit,
1588 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1589 EDIT_BUF_SIZE - r,
1590 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1591 EDIT_BUF_SIZE - (r - s), r - s);
1593 increment -= r;
1594 edit->curs1 -= r;
1595 edit->curs2 += r;
1596 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1598 if (p)
1599 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1600 else
1601 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1603 else
1604 g_free (p);
1606 return edit_get_byte (edit, edit->curs1);
1609 #endif /* ! FAST_MOVE_CURSOR */
1611 /* moves the cursor right or left: increment positive or negative respectively */
1612 void
1613 edit_cursor_move (WEdit * edit, long increment)
1615 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1616 int c;
1617 #ifdef FAST_MOVE_CURSOR
1618 if (increment < -256)
1620 edit->force |= REDRAW_PAGE;
1621 edit_move_backward_lots (edit, -increment);
1622 return;
1624 #endif /* ! FAST_MOVE_CURSOR */
1626 if (increment < 0)
1628 for (; increment < 0; increment++)
1630 if (!edit->curs1)
1631 return;
1633 edit_push_action (edit, CURS_RIGHT);
1635 c = edit_get_byte (edit, edit->curs1 - 1);
1636 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1637 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1638 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1639 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1640 edit->curs2++;
1641 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
1642 1) & M_EDIT_BUF_SIZE];
1643 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
1645 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1646 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1648 edit->curs1--;
1649 if (c == '\n')
1651 edit->curs_line--;
1652 edit->force |= REDRAW_LINE_BELOW;
1657 else if (increment > 0)
1659 for (; increment > 0; increment--)
1661 if (!edit->curs2)
1662 return;
1664 edit_push_action (edit, CURS_LEFT);
1666 c = edit_get_byte (edit, edit->curs1);
1667 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1668 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1669 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1670 edit->curs1++;
1671 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
1672 ((edit->curs2 -
1673 1) & M_EDIT_BUF_SIZE) - 1];
1674 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
1676 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1677 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1679 edit->curs2--;
1680 if (c == '\n')
1682 edit->curs_line++;
1683 edit->force |= REDRAW_LINE_ABOVE;
1689 /* These functions return positions relative to lines */
1691 /* returns index of last char on line + 1 */
1692 long
1693 edit_eol (WEdit * edit, long current)
1695 if (current >= edit->last_byte)
1696 return edit->last_byte;
1698 for (;; current++)
1699 if (edit_get_byte (edit, current) == '\n')
1700 break;
1701 return current;
1704 /* returns index of first char on line */
1705 long
1706 edit_bol (WEdit * edit, long current)
1708 if (current <= 0)
1709 return 0;
1711 for (;; current--)
1712 if (edit_get_byte (edit, current - 1) == '\n')
1713 break;
1714 return current;
1718 long
1719 edit_count_lines (WEdit * edit, long current, long upto)
1721 long lines = 0;
1722 if (upto > edit->last_byte)
1723 upto = edit->last_byte;
1724 if (current < 0)
1725 current = 0;
1726 while (current < upto)
1727 if (edit_get_byte (edit, current++) == '\n')
1728 lines++;
1729 return lines;
1733 /* If lines is zero this returns the count of lines from current to upto. */
1734 /* If upto is zero returns index of lines forward current. */
1735 long
1736 edit_move_forward (WEdit * edit, long current, long lines, long upto)
1738 if (upto)
1740 return edit_count_lines (edit, current, upto);
1742 else
1744 long next;
1745 if (lines < 0)
1746 lines = 0;
1747 while (lines--)
1749 next = edit_eol (edit, current) + 1;
1750 if (next > edit->last_byte)
1751 break;
1752 else
1753 current = next;
1755 return current;
1760 /* Returns offset of 'lines' lines up from current */
1761 long
1762 edit_move_backward (WEdit * edit, long current, long lines)
1764 if (lines < 0)
1765 lines = 0;
1766 current = edit_bol (edit, current);
1767 while ((lines--) && current != 0)
1768 current = edit_bol (edit, current - 1);
1769 return current;
1772 /* If cols is zero this returns the count of columns from current to upto. */
1773 /* If upto is zero returns index of cols across from current. */
1774 long
1775 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1777 long p, q;
1778 int col;
1780 if (upto)
1782 q = upto;
1783 cols = -10;
1785 else
1786 q = edit->last_byte + 2;
1788 for (col = 0, p = current; p < q; p++)
1790 int c, orig_c;
1791 int utf_ch = 0;
1792 #ifdef HAVE_CHARSET
1793 int cw = 1;
1794 #endif
1795 if (cols != -10)
1797 if (col == cols)
1798 return p;
1799 if (col > cols)
1800 return p - 1;
1802 orig_c = c = edit_get_byte (edit, p);
1803 #ifdef HAVE_CHARSET
1804 if (edit->utf8)
1806 utf_ch = edit_get_utf (edit, p, &cw);
1807 if (utf8_display)
1809 if (cw > 1)
1810 col -= cw - 1;
1811 if (g_unichar_iswide (utf_ch))
1812 col++;
1814 else if (cw > 1 && g_unichar_isprint (utf_ch))
1815 col -= cw - 1;
1817 #endif
1818 c = convert_to_display_c (c);
1819 if (c == '\t')
1820 col += TAB_SIZE - col % TAB_SIZE;
1821 else if (c == '\n')
1823 if (upto)
1824 return col;
1825 else
1826 return p;
1828 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1829 /* '\r' is shown as ^M, so we must advance 2 characters */
1830 /* Caret notation for control characters */
1831 col += 2;
1832 else
1833 col++;
1835 return col;
1838 /* returns the current column position of the cursor */
1840 edit_get_col (WEdit * edit)
1842 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1846 /* Scrolling functions */
1848 void
1849 edit_update_curs_row (WEdit * edit)
1851 edit->curs_row = edit->curs_line - edit->start_line;
1854 void
1855 edit_update_curs_col (WEdit * edit)
1857 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1861 edit_get_curs_col (const WEdit * edit)
1863 return edit->curs_col;
1866 /*moves the display start position up by i lines */
1867 void
1868 edit_scroll_upward (WEdit * edit, unsigned long i)
1870 unsigned long lines_above = edit->start_line;
1871 if (i > lines_above)
1872 i = lines_above;
1873 if (i)
1875 edit->start_line -= i;
1876 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1877 edit->force |= REDRAW_PAGE;
1878 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1880 edit_update_curs_row (edit);
1884 /* returns 1 if could scroll, 0 otherwise */
1885 void
1886 edit_scroll_downward (WEdit * edit, int i)
1888 int lines_below;
1889 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1890 if (lines_below > 0)
1892 if (i > lines_below)
1893 i = lines_below;
1894 edit->start_line += i;
1895 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1896 edit->force |= REDRAW_PAGE;
1897 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1899 edit_update_curs_row (edit);
1902 void
1903 edit_scroll_right (WEdit * edit, int i)
1905 edit->force |= REDRAW_PAGE;
1906 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1907 edit->start_col -= i;
1910 void
1911 edit_scroll_left (WEdit * edit, int i)
1913 if (edit->start_col)
1915 edit->start_col += i;
1916 if (edit->start_col > 0)
1917 edit->start_col = 0;
1918 edit->force |= REDRAW_PAGE;
1919 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1923 /* high level cursor movement commands */
1925 static int
1926 is_in_indent (WEdit * edit)
1928 long p = edit_bol (edit, edit->curs1);
1929 while (p < edit->curs1)
1930 if (!strchr (" \t", edit_get_byte (edit, p++)))
1931 return 0;
1932 return 1;
1935 static int left_of_four_spaces (WEdit * edit);
1937 void
1938 edit_move_to_prev_col (WEdit * edit, long p)
1940 int prev = edit->prev_col;
1941 int over = edit->over_col;
1942 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1944 if (option_cursor_beyond_eol)
1946 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
1947 edit_eol (edit, edit->curs1));
1949 if (line_len < prev + edit->over_col)
1951 edit->over_col = prev + over - line_len;
1952 edit->prev_col = line_len;
1953 edit->curs_col = line_len;
1955 else
1957 edit->curs_col = prev + over;
1958 edit->prev_col = edit->curs_col;
1959 edit->over_col = 0;
1962 else
1964 edit->over_col = 0;
1965 if (is_in_indent (edit) && option_fake_half_tabs)
1967 edit_update_curs_col (edit);
1968 if (space_width)
1969 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
1971 int q = edit->curs_col;
1972 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1973 p = edit_bol (edit, edit->curs1);
1974 edit_cursor_move (edit,
1975 edit_move_forward3 (edit, p, edit->curs_col,
1976 0) - edit->curs1);
1977 if (!left_of_four_spaces (edit))
1978 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1984 static int
1985 is_blank (WEdit * edit, long offset)
1987 long s, f;
1988 int c;
1989 s = edit_bol (edit, offset);
1990 f = edit_eol (edit, offset) - 1;
1991 while (s <= f)
1993 c = edit_get_byte (edit, s++);
1994 if (!isspace (c))
1995 return 0;
1997 return 1;
2001 /* returns the offset of line i */
2002 static long
2003 edit_find_line (WEdit * edit, int line)
2005 int i, j = 0;
2006 int m = 2000000000;
2007 if (!edit->caches_valid)
2009 for (i = 0; i < N_LINE_CACHES; i++)
2010 edit->line_numbers[i] = edit->line_offsets[i] = 0;
2011 /* three offsets that we *know* are line 0 at 0 and these two: */
2012 edit->line_numbers[1] = edit->curs_line;
2013 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
2014 edit->line_numbers[2] = edit->total_lines;
2015 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
2016 edit->caches_valid = 1;
2018 if (line >= edit->total_lines)
2019 return edit->line_offsets[2];
2020 if (line <= 0)
2021 return 0;
2022 /* find the closest known point */
2023 for (i = 0; i < N_LINE_CACHES; i++)
2025 int n;
2026 n = abs (edit->line_numbers[i] - line);
2027 if (n < m)
2029 m = n;
2030 j = i;
2033 if (m == 0)
2034 return edit->line_offsets[j]; /* know the offset exactly */
2035 if (m == 1 && j >= 3)
2036 i = j; /* one line different - caller might be looping, so stay in this cache */
2037 else
2038 i = 3 + (rand () % (N_LINE_CACHES - 3));
2039 if (line > edit->line_numbers[j])
2040 edit->line_offsets[i] =
2041 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
2042 else
2043 edit->line_offsets[i] =
2044 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
2045 edit->line_numbers[i] = line;
2046 return edit->line_offsets[i];
2050 line_is_blank (WEdit * edit, long line)
2052 return is_blank (edit, edit_find_line (edit, line));
2055 /* moves up until a blank line is reached, or until just
2056 before a non-blank line is reached */
2057 static void
2058 edit_move_up_paragraph (WEdit * edit, int do_scroll)
2060 int i = 0;
2061 if (edit->curs_line > 1)
2063 if (line_is_blank (edit, edit->curs_line))
2065 if (line_is_blank (edit, edit->curs_line - 1))
2067 for (i = edit->curs_line - 1; i; i--)
2068 if (!line_is_blank (edit, i))
2070 i++;
2071 break;
2074 else
2076 for (i = edit->curs_line - 1; i; i--)
2077 if (line_is_blank (edit, i))
2078 break;
2081 else
2083 for (i = edit->curs_line - 1; i; i--)
2084 if (line_is_blank (edit, i))
2085 break;
2088 edit_move_up (edit, edit->curs_line - i, do_scroll);
2091 /* moves down until a blank line is reached, or until just
2092 before a non-blank line is reached */
2093 static void
2094 edit_move_down_paragraph (WEdit * edit, int do_scroll)
2096 int i;
2097 if (edit->curs_line >= edit->total_lines - 1)
2099 i = edit->total_lines;
2101 else
2103 if (line_is_blank (edit, edit->curs_line))
2105 if (line_is_blank (edit, edit->curs_line + 1))
2107 for (i = edit->curs_line + 1; i; i++)
2108 if (!line_is_blank (edit, i) || i > edit->total_lines)
2110 i--;
2111 break;
2114 else
2116 for (i = edit->curs_line + 1; i; i++)
2117 if (line_is_blank (edit, i) || i >= edit->total_lines)
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 edit_move_down (edit, i - edit->curs_line, do_scroll);
2131 static void
2132 edit_begin_page (WEdit * edit)
2134 edit_update_curs_row (edit);
2135 edit_move_up (edit, edit->curs_row, 0);
2138 static void
2139 edit_end_page (WEdit * edit)
2141 edit_update_curs_row (edit);
2142 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
2146 /* goto beginning of text */
2147 static void
2148 edit_move_to_top (WEdit * edit)
2150 if (edit->curs_line)
2152 edit_cursor_move (edit, -edit->curs1);
2153 edit_move_to_prev_col (edit, 0);
2154 edit->force |= REDRAW_PAGE;
2155 edit->search_start = 0;
2156 edit_update_curs_row (edit);
2161 /* goto end of text */
2162 static void
2163 edit_move_to_bottom (WEdit * edit)
2165 if (edit->curs_line < edit->total_lines)
2167 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
2168 edit->start_display = edit->last_byte;
2169 edit->start_line = edit->total_lines;
2170 edit_scroll_upward (edit, edit->num_widget_lines - 1);
2171 edit->force |= REDRAW_PAGE;
2175 /* goto beginning of line */
2176 static void
2177 edit_cursor_to_bol (WEdit * edit)
2179 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
2180 edit->search_start = edit->curs1;
2181 edit->prev_col = edit_get_col (edit);
2182 edit->over_col = 0;
2185 /* goto end of line */
2186 static void
2187 edit_cursor_to_eol (WEdit * edit)
2189 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
2190 edit->search_start = edit->curs1;
2191 edit->prev_col = edit_get_col (edit);
2192 edit->over_col = 0;
2195 /* move cursor to line 'line' */
2196 void
2197 edit_move_to_line (WEdit * e, long line)
2199 if (line < e->curs_line)
2200 edit_move_up (e, e->curs_line - line, 0);
2201 else
2202 edit_move_down (e, line - e->curs_line, 0);
2203 edit_scroll_screen_over_cursor (e);
2206 /* scroll window so that first visible line is 'line' */
2207 void
2208 edit_move_display (WEdit * e, long line)
2210 if (line < e->start_line)
2211 edit_scroll_upward (e, e->start_line - line);
2212 else
2213 edit_scroll_downward (e, line - e->start_line);
2216 /* save markers onto undo stack */
2217 void
2218 edit_push_markers (WEdit * edit)
2220 edit_push_action (edit, MARK_1 + edit->mark1);
2221 edit_push_action (edit, MARK_2 + edit->mark2);
2224 void
2225 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2227 edit->mark1 = m1;
2228 edit->mark2 = m2;
2229 edit->column1 = c1;
2230 edit->column2 = c2;
2234 /* highlight marker toggle */
2235 void
2236 edit_mark_cmd (WEdit * edit, int unmark)
2238 edit_push_markers (edit);
2239 if (unmark)
2241 edit_set_markers (edit, 0, 0, 0, 0);
2242 edit->force |= REDRAW_PAGE;
2244 else
2246 if (edit->mark2 >= 0)
2248 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
2249 edit->curs_col + edit->over_col);
2250 edit->force |= REDRAW_PAGE;
2252 else
2253 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
2254 edit->curs_col + edit->over_col);
2258 static unsigned long
2259 my_type_of (int c)
2261 int x, r = 0;
2262 const char *p, *q;
2263 const char option_chars_move_whole_word[] =
2264 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2266 if (!c)
2267 return 0;
2268 if (c == '!')
2270 if (*option_chars_move_whole_word == '!')
2271 return 2;
2272 return 0x80000000UL;
2274 if (g_ascii_isupper ((gchar) c))
2275 c = 'A';
2276 else if (g_ascii_islower ((gchar) c))
2277 c = 'a';
2278 else if (g_ascii_isalpha (c))
2279 c = 'a';
2280 else if (isdigit (c))
2281 c = '0';
2282 else if (isspace (c))
2283 c = ' ';
2284 q = strchr (option_chars_move_whole_word, c);
2285 if (!q)
2286 return 0xFFFFFFFFUL;
2289 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2290 if (*p == '!')
2291 x <<= 1;
2292 r |= x;
2294 while ((q = strchr (q + 1, c)));
2295 return r;
2298 static void
2299 edit_left_word_move (WEdit * edit, int s)
2301 for (;;)
2303 int c1, c2;
2304 if (column_highlighting
2305 && edit->mark1 != edit->mark2
2306 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2307 break;
2308 edit_cursor_move (edit, -1);
2309 if (!edit->curs1)
2310 break;
2311 c1 = edit_get_byte (edit, edit->curs1 - 1);
2312 c2 = edit_get_byte (edit, edit->curs1);
2313 if (!(my_type_of (c1) & my_type_of (c2)))
2314 break;
2315 if (isspace (c1) && !isspace (c2))
2316 break;
2317 if (s)
2318 if (!isspace (c1) && isspace (c2))
2319 break;
2323 static void
2324 edit_left_word_move_cmd (WEdit * edit)
2326 edit_left_word_move (edit, 0);
2327 edit->force |= REDRAW_PAGE;
2330 static void
2331 edit_right_word_move (WEdit * edit, int s)
2333 for (;;)
2335 int c1, c2;
2336 if (column_highlighting
2337 && edit->mark1 != edit->mark2
2338 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
2339 break;
2340 edit_cursor_move (edit, 1);
2341 if (edit->curs1 >= edit->last_byte)
2342 break;
2343 c1 = edit_get_byte (edit, edit->curs1 - 1);
2344 c2 = edit_get_byte (edit, edit->curs1);
2345 if (!(my_type_of (c1) & my_type_of (c2)))
2346 break;
2347 if (isspace (c1) && !isspace (c2))
2348 break;
2349 if (s)
2350 if (!isspace (c1) && isspace (c2))
2351 break;
2355 static void
2356 edit_right_word_move_cmd (WEdit * edit)
2358 edit_right_word_move (edit, 0);
2359 edit->force |= REDRAW_PAGE;
2362 static void
2363 edit_right_char_move_cmd (WEdit * edit)
2365 int cw = 1;
2366 int c = 0;
2367 if (edit->utf8)
2369 c = edit_get_utf (edit, edit->curs1, &cw);
2370 if (cw < 1)
2371 cw = 1;
2373 else
2375 c = edit_get_byte (edit, edit->curs1);
2377 if (option_cursor_beyond_eol && c == '\n')
2379 edit->over_col++;
2381 else
2383 edit_cursor_move (edit, cw);
2387 static void
2388 edit_left_char_move_cmd (WEdit * edit)
2390 int cw = 1;
2391 if (column_highlighting
2392 && option_cursor_beyond_eol
2393 && edit->mark1 != edit->mark2
2394 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
2395 return;
2396 if (edit->utf8)
2398 edit_get_prev_utf (edit, edit->curs1, &cw);
2399 if (cw < 1)
2400 cw = 1;
2402 if (option_cursor_beyond_eol && edit->over_col > 0)
2404 edit->over_col--;
2406 else
2408 edit_cursor_move (edit, -cw);
2412 /** Up or down cursor moving.
2413 direction = TRUE - move up
2414 = FALSE - move down
2416 static void
2417 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2419 unsigned long p;
2420 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
2422 if (i > l)
2423 i = l;
2425 if (i == 0)
2426 return;
2428 if (i > 1)
2429 edit->force |= REDRAW_PAGE;
2430 if (do_scroll)
2432 if (direction)
2433 edit_scroll_upward (edit, i);
2434 else
2435 edit_scroll_downward (edit, i);
2437 p = edit_bol (edit, edit->curs1);
2439 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
2441 edit_cursor_move (edit, p - edit->curs1);
2443 edit_move_to_prev_col (edit, p);
2445 /* search start of current multibyte char (like CJK) */
2446 if (edit->curs1 + 1 < edit->last_byte)
2448 edit_right_char_move_cmd (edit);
2449 edit_left_char_move_cmd (edit);
2452 edit->search_start = edit->curs1;
2453 edit->found_len = 0;
2456 static void
2457 edit_right_delete_word (WEdit * edit)
2459 int c1, c2;
2460 for (;;)
2462 if (edit->curs1 >= edit->last_byte)
2463 break;
2464 c1 = edit_delete (edit, 1);
2465 c2 = edit_get_byte (edit, edit->curs1);
2466 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2467 break;
2468 if (!(my_type_of (c1) & my_type_of (c2)))
2469 break;
2473 static void
2474 edit_left_delete_word (WEdit * edit)
2476 int c1, c2;
2477 for (;;)
2479 if (edit->curs1 <= 0)
2480 break;
2481 c1 = edit_backspace (edit, 1);
2482 c2 = edit_get_byte (edit, edit->curs1 - 1);
2483 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2484 break;
2485 if (!(my_type_of (c1) & my_type_of (c2)))
2486 break;
2491 the start column position is not recorded, and hence does not
2492 undo as it happed. But who would notice.
2494 static void
2495 edit_do_undo (WEdit * edit)
2497 long ac;
2498 long count = 0;
2500 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2501 edit->over_col = 0;
2502 while ((ac = pop_action (edit)) < KEY_PRESS)
2504 switch ((int) ac)
2506 case STACK_BOTTOM:
2507 goto done_undo;
2508 case CURS_RIGHT:
2509 edit_cursor_move (edit, 1);
2510 break;
2511 case CURS_LEFT:
2512 edit_cursor_move (edit, -1);
2513 break;
2514 case BACKSPACE:
2515 edit_backspace (edit, 1);
2516 break;
2517 case DELCHAR:
2518 edit_delete (edit, 1);
2519 break;
2520 case COLUMN_ON:
2521 column_highlighting = 1;
2522 break;
2523 case COLUMN_OFF:
2524 column_highlighting = 0;
2525 break;
2527 if (ac >= 256 && ac < 512)
2528 edit_insert_ahead (edit, ac - 256);
2529 if (ac >= 0 && ac < 256)
2530 edit_insert (edit, ac);
2532 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
2534 edit->mark1 = ac - MARK_1;
2535 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2537 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
2539 edit->mark2 = ac - MARK_2;
2540 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2542 if (count++)
2543 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2546 if (edit->start_display > ac - KEY_PRESS)
2548 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2549 edit->force |= REDRAW_PAGE;
2551 else if (edit->start_display < ac - KEY_PRESS)
2553 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2554 edit->force |= REDRAW_PAGE;
2556 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2557 edit_update_curs_row (edit);
2559 done_undo:;
2560 edit->stack_disable = 0;
2563 static void
2564 edit_delete_to_line_end (WEdit * edit)
2566 while (edit_get_byte (edit, edit->curs1) != '\n')
2568 if (!edit->curs2)
2569 break;
2570 edit_delete (edit, 1);
2574 static void
2575 edit_delete_to_line_begin (WEdit * edit)
2577 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2579 if (!edit->curs1)
2580 break;
2581 edit_backspace (edit, 1);
2585 void
2586 edit_delete_line (WEdit * edit)
2589 * Delete right part of the line.
2590 * Note that edit_get_byte() returns '\n' when byte position is
2591 * beyond EOF.
2593 while (edit_get_byte (edit, edit->curs1) != '\n')
2595 (void) edit_delete (edit, 1);
2599 * Delete '\n' char.
2600 * Note that edit_delete() will not corrupt anything if called while
2601 * cursor position is EOF.
2603 (void) edit_delete (edit, 1);
2606 * Delete left part of the line.
2607 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2609 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
2611 (void) edit_backspace (edit, 1);
2615 void
2616 insert_spaces_tab (WEdit * edit, int half)
2618 int i;
2619 edit_update_curs_col (edit);
2620 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
2621 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2622 while (i > 0)
2624 edit_insert (edit, ' ');
2625 i -= space_width;
2629 static int
2630 is_aligned_on_a_tab (WEdit * edit)
2632 edit_update_curs_col (edit);
2633 return !((edit->curs_col % (TAB_SIZE * space_width))
2634 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2637 static int
2638 right_of_four_spaces (WEdit * edit)
2640 int i, ch = 0;
2641 for (i = 1; i <= HALF_TAB_SIZE; i++)
2642 ch |= edit_get_byte (edit, edit->curs1 - i);
2643 if (ch == ' ')
2644 return is_aligned_on_a_tab (edit);
2645 return 0;
2648 static int
2649 left_of_four_spaces (WEdit * edit)
2651 int i, ch = 0;
2652 for (i = 0; i < HALF_TAB_SIZE; i++)
2653 ch |= edit_get_byte (edit, edit->curs1 + i);
2654 if (ch == ' ')
2655 return is_aligned_on_a_tab (edit);
2656 return 0;
2660 edit_indent_width (WEdit * edit, long p)
2662 long q = p;
2663 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2664 q++;
2665 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2668 void
2669 edit_insert_indent (WEdit * edit, int indent)
2671 if (!option_fill_tabs_with_spaces)
2673 while (indent >= TAB_SIZE)
2675 edit_insert (edit, '\t');
2676 indent -= TAB_SIZE;
2679 while (indent-- > 0)
2680 edit_insert (edit, ' ');
2683 static void
2684 edit_auto_indent (WEdit * edit)
2686 long p;
2687 char c;
2688 p = edit->curs1;
2689 /* use the previous line as a template */
2690 p = edit_move_backward (edit, p, 1);
2691 /* copy the leading whitespace of the line */
2692 for (;;)
2693 { /* no range check - the line _is_ \n-terminated */
2694 c = edit_get_byte (edit, p++);
2695 if (c != ' ' && c != '\t')
2696 break;
2697 edit_insert (edit, c);
2701 static inline void
2702 edit_double_newline (WEdit * edit)
2704 edit_insert (edit, '\n');
2705 if (edit_get_byte (edit, edit->curs1) == '\n')
2706 return;
2707 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2708 return;
2709 edit->force |= REDRAW_PAGE;
2710 edit_insert (edit, '\n');
2713 static inline void
2714 edit_tab_cmd (WEdit * edit)
2716 int i;
2718 if (option_fake_half_tabs)
2720 if (is_in_indent (edit))
2722 /*insert a half tab (usually four spaces) unless there is a
2723 half tab already behind, then delete it and insert a
2724 full tab. */
2725 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
2727 for (i = 1; i <= HALF_TAB_SIZE; i++)
2728 edit_backspace (edit, 1);
2729 edit_insert (edit, '\t');
2731 else
2733 insert_spaces_tab (edit, 1);
2735 return;
2738 if (option_fill_tabs_with_spaces)
2740 insert_spaces_tab (edit, 0);
2742 else
2744 edit_insert (edit, '\t');
2748 static void
2749 check_and_wrap_line (WEdit * edit)
2751 int curs, c;
2752 if (!option_typewriter_wrap)
2753 return;
2754 edit_update_curs_col (edit);
2755 if (edit->curs_col < option_word_wrap_line_length)
2756 return;
2757 curs = edit->curs1;
2758 for (;;)
2760 curs--;
2761 c = edit_get_byte (edit, curs);
2762 if (c == '\n' || curs <= 0)
2764 edit_insert (edit, '\n');
2765 return;
2767 if (c == ' ' || c == '\t')
2769 int current = edit->curs1;
2770 edit_cursor_move (edit, curs - edit->curs1 + 1);
2771 edit_insert (edit, '\n');
2772 edit_cursor_move (edit, current - edit->curs1 + 1);
2773 return;
2778 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
2780 void
2781 edit_push_key_press (WEdit * edit)
2783 edit_push_action (edit, KEY_PRESS + edit->start_display);
2784 if (edit->mark2 == -1)
2785 edit_push_action (edit, MARK_1 + edit->mark1);
2788 /* this find the matching bracket in either direction, and sets edit->bracket */
2789 static long
2790 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2792 const char *const b = "{}{[][()(", *p;
2793 int i = 1, a, inc = -1, c, d, n = 0;
2794 unsigned long j = 0;
2795 long q;
2796 edit_update_curs_row (edit);
2797 c = edit_get_byte (edit, edit->curs1);
2798 p = strchr (b, c);
2799 /* no limit */
2800 if (!furthest_bracket_search)
2801 furthest_bracket_search--;
2802 /* not on a bracket at all */
2803 if (!p)
2804 return -1;
2805 /* the matching bracket */
2806 d = p[1];
2807 /* going left or right? */
2808 if (strchr ("{[(", c))
2809 inc = 1;
2810 for (q = edit->curs1 + inc;; q += inc)
2812 /* out of buffer? */
2813 if (q >= edit->last_byte || q < 0)
2814 break;
2815 a = edit_get_byte (edit, q);
2816 /* don't want to eat CPU */
2817 if (j++ > furthest_bracket_search)
2818 break;
2819 /* out of screen? */
2820 if (in_screen)
2822 if (q < edit->start_display)
2823 break;
2824 /* count lines if searching downward */
2825 if (inc > 0 && a == '\n')
2826 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2827 break;
2829 /* count bracket depth */
2830 i += (a == c) - (a == d);
2831 /* return if bracket depth is zero */
2832 if (!i)
2833 return q;
2835 /* no match */
2836 return -1;
2839 static long last_bracket = -1;
2841 void
2842 edit_find_bracket (WEdit * edit)
2844 edit->bracket = edit_get_bracket (edit, 1, 10000);
2845 if (last_bracket != edit->bracket)
2846 edit->force |= REDRAW_PAGE;
2847 last_bracket = edit->bracket;
2850 static inline void
2851 edit_goto_matching_bracket (WEdit * edit)
2853 long q;
2855 q = edit_get_bracket (edit, 0, 0);
2856 if (q >= 0)
2858 edit->bracket = edit->curs1;
2859 edit->force |= REDRAW_PAGE;
2860 edit_cursor_move (edit, q - edit->curs1);
2865 * This executes a command as though the user initiated it through a key
2866 * press. Callback with WIDGET_KEY as a message calls this after
2867 * translating the key press. This function can be used to pass any
2868 * command to the editor. Note that the screen wouldn't update
2869 * automatically. Either of command or char_for_insertion must be
2870 * passed as -1. Commands are executed, and char_for_insertion is
2871 * inserted at the cursor.
2873 void
2874 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
2876 if (command == CK_Begin_Record_Macro)
2878 edit->macro_i = 0;
2879 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2880 return;
2882 if (command == CK_End_Record_Macro && edit->macro_i != -1)
2884 edit->force |= REDRAW_COMPLETELY;
2885 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2886 edit->macro_i = -1;
2887 return;
2889 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
2891 edit->macro[edit->macro_i].command = command;
2892 edit->macro[edit->macro_i++].ch = char_for_insertion;
2894 /* record the beginning of a set of editing actions initiated by a key press */
2895 if (command != CK_Undo && command != CK_Ext_Mode)
2896 edit_push_key_press (edit);
2898 edit_execute_cmd (edit, command, char_for_insertion);
2899 if (column_highlighting)
2900 edit->force |= REDRAW_PAGE;
2903 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
2906 This executes a command at a lower level than macro recording.
2907 It also does not push a key_press onto the undo stack. This means
2908 that if it is called many times, a single undo command will undo
2909 all of them. It also does not check for the Undo command.
2911 void
2912 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
2914 edit->force |= REDRAW_LINE;
2916 /* The next key press will unhighlight the found string, so update
2917 * the whole page */
2918 if (edit->found_len || column_highlighting)
2919 edit->force |= REDRAW_PAGE;
2921 if (command / 100 == 6)
2922 { /* a highlight command like shift-arrow */
2923 column_highlighting = 0;
2924 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
2926 edit_mark_cmd (edit, 1); /* clear */
2927 edit_mark_cmd (edit, 0); /* marking on */
2929 edit->highlight = 1;
2931 else
2932 { /* any other command */
2933 if (edit->highlight)
2934 edit_mark_cmd (edit, 0); /* clear */
2935 edit->highlight = 0;
2938 /* first check for undo */
2939 if (command == CK_Undo)
2941 edit_do_undo (edit);
2942 edit->found_len = 0;
2943 edit->prev_col = edit_get_col (edit);
2944 edit->search_start = edit->curs1;
2945 return;
2948 /* An ordinary key press */
2949 if (char_for_insertion >= 0)
2951 /* if non persistent selection and text selected */
2952 if (!option_persistent_selections)
2954 if (edit->mark1 != edit->mark2)
2955 edit_block_delete_cmd (edit);
2957 if (edit->overwrite)
2959 /* remove char only one time, after input first byte, multibyte chars */
2960 if ((!utf8_display || edit->charpoint == 0)
2961 && edit_get_byte (edit, edit->curs1) != '\n')
2962 edit_delete (edit, 0);
2964 if (option_cursor_beyond_eol && edit->over_col > 0)
2965 edit_insert_over (edit);
2966 #ifdef HAVE_CHARSET
2967 if (char_for_insertion > 255 && utf8_display == 0)
2969 unsigned char str[6 + 1];
2970 size_t i = 0;
2971 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
2972 if (res == 0)
2974 str[0] = '.';
2975 str[1] = '\0';
2977 else
2979 str[res] = '\0';
2981 while (str[i] != 0 && i <= 6)
2983 char_for_insertion = str[i];
2984 edit_insert (edit, char_for_insertion);
2985 i++;
2988 else
2989 #endif
2990 edit_insert (edit, char_for_insertion);
2992 if (option_auto_para_formatting)
2994 format_paragraph (edit, 0);
2995 edit->force |= REDRAW_PAGE;
2997 else
2998 check_and_wrap_line (edit);
2999 edit->found_len = 0;
3000 edit->prev_col = edit_get_col (edit);
3001 edit->search_start = edit->curs1;
3002 edit_find_bracket (edit);
3003 return;
3006 switch (command)
3008 case CK_Begin_Page:
3009 case CK_End_Page:
3010 case CK_Begin_Page_Highlight:
3011 case CK_End_Page_Highlight:
3012 case CK_Word_Left:
3013 case CK_Word_Right:
3014 case CK_Up:
3015 case CK_Down:
3016 case CK_Left:
3017 case CK_Right:
3018 if (edit->mark2 >= 0)
3020 if (!option_persistent_selections)
3022 if (column_highlighting)
3023 edit_push_action (edit, COLUMN_ON);
3024 column_highlighting = 0;
3025 edit_mark_cmd (edit, 1);
3030 switch (command)
3032 case CK_Begin_Page:
3033 case CK_End_Page:
3034 case CK_Begin_Page_Highlight:
3035 case CK_End_Page_Highlight:
3036 case CK_Word_Left:
3037 case CK_Word_Right:
3038 case CK_Up:
3039 case CK_Down:
3040 case CK_Word_Left_Highlight:
3041 case CK_Word_Right_Highlight:
3042 case CK_Up_Highlight:
3043 case CK_Down_Highlight:
3044 case CK_Up_Alt_Highlight:
3045 case CK_Down_Alt_Highlight:
3046 if (edit->mark2 == -1)
3047 break; /*marking is following the cursor: may need to highlight a whole line */
3048 case CK_Left:
3049 case CK_Right:
3050 case CK_Left_Highlight:
3051 case CK_Right_Highlight:
3052 edit->force |= REDRAW_CHAR_ONLY;
3055 /* basic cursor key commands */
3056 switch (command)
3058 case CK_BackSpace:
3059 /* if non persistent selection and text selected */
3060 if (!option_persistent_selections)
3062 if (edit->mark1 != edit->mark2)
3064 edit_block_delete_cmd (edit);
3065 break;
3068 if (option_cursor_beyond_eol && edit->over_col > 0)
3070 edit->over_col--;
3071 break;
3073 if (option_backspace_through_tabs && is_in_indent (edit))
3075 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3076 edit_backspace (edit, 1);
3077 break;
3079 else
3081 if (option_fake_half_tabs)
3083 int i;
3084 if (is_in_indent (edit) && right_of_four_spaces (edit))
3086 for (i = 0; i < HALF_TAB_SIZE; i++)
3087 edit_backspace (edit, 1);
3088 break;
3092 edit_backspace (edit, 0);
3093 break;
3094 case CK_Delete:
3095 /* if non persistent selection and text selected */
3096 if (!option_persistent_selections)
3098 if (edit->mark1 != edit->mark2)
3100 edit_block_delete_cmd (edit);
3101 break;
3105 if (option_cursor_beyond_eol && edit->over_col > 0)
3106 edit_insert_over (edit);
3108 if (option_fake_half_tabs)
3110 int i;
3111 if (is_in_indent (edit) && left_of_four_spaces (edit))
3113 for (i = 1; i <= HALF_TAB_SIZE; i++)
3114 edit_delete (edit, 1);
3115 break;
3118 edit_delete (edit, 0);
3119 break;
3120 case CK_Delete_Word_Left:
3121 edit->over_col = 0;
3122 edit_left_delete_word (edit);
3123 break;
3124 case CK_Delete_Word_Right:
3125 if (option_cursor_beyond_eol && edit->over_col > 0)
3126 edit_insert_over (edit);
3128 edit_right_delete_word (edit);
3129 break;
3130 case CK_Delete_Line:
3131 edit_delete_line (edit);
3132 break;
3133 case CK_Delete_To_Line_End:
3134 edit_delete_to_line_end (edit);
3135 break;
3136 case CK_Delete_To_Line_Begin:
3137 edit_delete_to_line_begin (edit);
3138 break;
3139 case CK_Enter:
3140 edit->over_col = 0;
3141 if (option_auto_para_formatting)
3143 edit_double_newline (edit);
3144 if (option_return_does_auto_indent)
3145 edit_auto_indent (edit);
3146 format_paragraph (edit, 0);
3148 else
3150 edit_insert (edit, '\n');
3151 if (option_return_does_auto_indent)
3153 edit_auto_indent (edit);
3156 break;
3157 case CK_Return:
3158 edit_insert (edit, '\n');
3159 break;
3161 case CK_Page_Up_Alt_Highlight:
3162 column_highlighting = 1;
3163 case CK_Page_Up:
3164 case CK_Page_Up_Highlight:
3165 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3166 break;
3167 case CK_Page_Down_Alt_Highlight:
3168 column_highlighting = 1;
3169 case CK_Page_Down:
3170 case CK_Page_Down_Highlight:
3171 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3172 break;
3173 case CK_Left_Alt_Highlight:
3174 column_highlighting = 1;
3175 case CK_Left:
3176 case CK_Left_Highlight:
3177 if (option_fake_half_tabs)
3179 if (is_in_indent (edit) && right_of_four_spaces (edit))
3181 if (option_cursor_beyond_eol && edit->over_col > 0)
3182 edit->over_col--;
3183 else
3184 edit_cursor_move (edit, -HALF_TAB_SIZE);
3185 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3186 break;
3189 edit_left_char_move_cmd (edit);
3190 break;
3191 case CK_Right_Alt_Highlight:
3192 column_highlighting = 1;
3193 case CK_Right:
3194 case CK_Right_Highlight:
3195 if (option_fake_half_tabs)
3197 if (is_in_indent (edit) && left_of_four_spaces (edit))
3199 edit_cursor_move (edit, HALF_TAB_SIZE);
3200 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3201 break;
3204 edit_right_char_move_cmd (edit);
3205 break;
3206 case CK_Begin_Page:
3207 case CK_Begin_Page_Highlight:
3208 edit_begin_page (edit);
3209 break;
3210 case CK_End_Page:
3211 case CK_End_Page_Highlight:
3212 edit_end_page (edit);
3213 break;
3214 case CK_Word_Left:
3215 case CK_Word_Left_Highlight:
3216 edit->over_col = 0;
3217 edit_left_word_move_cmd (edit);
3218 break;
3219 case CK_Word_Right:
3220 case CK_Word_Right_Highlight:
3221 edit->over_col = 0;
3222 edit_right_word_move_cmd (edit);
3223 break;
3224 case CK_Up_Alt_Highlight:
3225 column_highlighting = 1;
3226 case CK_Up:
3227 case CK_Up_Highlight:
3228 edit_move_up (edit, 1, 0);
3229 break;
3230 case CK_Down_Alt_Highlight:
3231 column_highlighting = 1;
3232 case CK_Down:
3233 case CK_Down_Highlight:
3234 edit_move_down (edit, 1, 0);
3235 break;
3236 case CK_Paragraph_Up_Alt_Highlight:
3237 column_highlighting = 1;
3238 case CK_Paragraph_Up:
3239 case CK_Paragraph_Up_Highlight:
3240 edit_move_up_paragraph (edit, 0);
3241 break;
3242 case CK_Paragraph_Down_Alt_Highlight:
3243 column_highlighting = 1;
3244 case CK_Paragraph_Down:
3245 case CK_Paragraph_Down_Highlight:
3246 edit_move_down_paragraph (edit, 0);
3247 break;
3248 case CK_Scroll_Up_Alt_Highlight:
3249 column_highlighting = 1;
3250 case CK_Scroll_Up:
3251 case CK_Scroll_Up_Highlight:
3252 edit_move_up (edit, 1, 1);
3253 break;
3254 case CK_Scroll_Down_Alt_Highlight:
3255 column_highlighting = 1;
3256 case CK_Scroll_Down:
3257 case CK_Scroll_Down_Highlight:
3258 edit_move_down (edit, 1, 1);
3259 break;
3260 case CK_Home:
3261 case CK_Home_Highlight:
3262 edit_cursor_to_bol (edit);
3263 break;
3264 case CK_End:
3265 case CK_End_Highlight:
3266 edit_cursor_to_eol (edit);
3267 break;
3268 case CK_Tab:
3269 /* if text marked shift block */
3270 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3272 if (edit->mark2 < 0)
3273 edit_mark_cmd (edit, 0);
3274 edit_move_block_to_right (edit);
3276 else
3278 if (option_cursor_beyond_eol)
3279 edit_insert_over (edit);
3280 edit_tab_cmd (edit);
3281 if (option_auto_para_formatting)
3283 format_paragraph (edit, 0);
3284 edit->force |= REDRAW_PAGE;
3286 else
3288 check_and_wrap_line (edit);
3291 break;
3293 case CK_Toggle_Insert:
3294 edit->overwrite = (edit->overwrite == 0);
3295 break;
3297 case CK_Mark:
3298 if (edit->mark2 >= 0)
3300 if (column_highlighting)
3301 edit_push_action (edit, COLUMN_ON);
3302 column_highlighting = 0;
3304 edit_mark_cmd (edit, 0);
3305 break;
3306 case CK_Column_Mark:
3307 if (!column_highlighting)
3308 edit_push_action (edit, COLUMN_OFF);
3309 column_highlighting = 1;
3310 edit_mark_cmd (edit, 0);
3311 break;
3312 case CK_Mark_All:
3313 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3314 edit->force |= REDRAW_PAGE;
3315 break;
3316 case CK_Unmark:
3317 if (column_highlighting)
3318 edit_push_action (edit, COLUMN_ON);
3319 column_highlighting = 0;
3320 edit_mark_cmd (edit, 1);
3321 break;
3323 case CK_Toggle_Line_State:
3324 option_line_state = !option_line_state;
3325 if (option_line_state)
3327 option_line_state_width = LINE_STATE_WIDTH;
3329 else
3331 option_line_state_width = 0;
3333 edit->force |= REDRAW_PAGE;
3334 break;
3336 case CK_Toggle_Show_Margin:
3337 show_right_margin = !show_right_margin;
3338 edit->force |= REDRAW_PAGE;
3339 break;
3341 case CK_Toggle_Bookmark:
3342 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3343 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3344 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3345 else
3346 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3347 break;
3348 case CK_Flush_Bookmarks:
3349 book_mark_flush (edit, BOOK_MARK_COLOR);
3350 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3351 edit->force |= REDRAW_PAGE;
3352 break;
3353 case CK_Next_Bookmark:
3354 if (edit->book_mark)
3356 struct _book_mark *p;
3357 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3358 if (p->next)
3360 p = p->next;
3361 if (p->line >= edit->start_line + edit->num_widget_lines
3362 || p->line < edit->start_line)
3363 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3364 edit_move_to_line (edit, p->line);
3367 break;
3368 case CK_Prev_Bookmark:
3369 if (edit->book_mark)
3371 struct _book_mark *p;
3372 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3373 while (p->line == edit->curs_line)
3374 if (p->prev)
3375 p = p->prev;
3376 if (p->line >= 0)
3378 if (p->line >= edit->start_line + edit->num_widget_lines
3379 || p->line < edit->start_line)
3380 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3381 edit_move_to_line (edit, p->line);
3384 break;
3386 case CK_Beginning_Of_Text:
3387 case CK_Beginning_Of_Text_Highlight:
3388 edit_move_to_top (edit);
3389 break;
3390 case CK_End_Of_Text:
3391 case CK_End_Of_Text_Highlight:
3392 edit_move_to_bottom (edit);
3393 break;
3395 case CK_Copy:
3396 if (option_cursor_beyond_eol && edit->over_col > 0)
3397 edit_insert_over (edit);
3398 edit_block_copy_cmd (edit);
3399 break;
3400 case CK_Remove:
3401 edit_block_delete_cmd (edit);
3402 break;
3403 case CK_Move:
3404 if (option_cursor_beyond_eol && edit->over_col > 0)
3405 edit_insert_over (edit);
3406 edit_block_move_cmd (edit);
3407 break;
3409 case CK_Shift_Block_Left:
3410 if (edit->mark1 != edit->mark2)
3411 edit_move_block_to_left (edit);
3412 break;
3413 case CK_Shift_Block_Right:
3414 if (edit->mark1 != edit->mark2)
3415 edit_move_block_to_right (edit);
3416 break;
3417 case CK_XStore:
3418 edit_copy_to_X_buf_cmd (edit);
3419 break;
3420 case CK_XCut:
3421 edit_cut_to_X_buf_cmd (edit);
3422 break;
3423 case CK_XPaste:
3424 /* if non persistent selection and text selected */
3425 if (!option_persistent_selections)
3427 if (edit->mark1 != edit->mark2)
3428 edit_block_delete_cmd (edit);
3430 if (option_cursor_beyond_eol && edit->over_col > 0)
3431 edit_insert_over (edit);
3432 edit_paste_from_X_buf_cmd (edit);
3433 break;
3434 case CK_Selection_History:
3435 edit_paste_from_history (edit);
3436 break;
3438 case CK_Save_As:
3439 edit_save_as_cmd (edit);
3440 break;
3441 case CK_Save:
3442 edit_save_confirm_cmd (edit);
3443 break;
3444 case CK_Load:
3445 edit_load_cmd (edit, EDIT_FILE_COMMON);
3446 break;
3447 case CK_Save_Block:
3448 edit_save_block_cmd (edit);
3449 break;
3450 case CK_Insert_File:
3451 edit_insert_file_cmd (edit);
3452 break;
3454 case CK_Load_Prev_File:
3455 edit_load_back_cmd (edit);
3456 break;
3457 case CK_Load_Next_File:
3458 edit_load_forward_cmd (edit);
3459 break;
3461 case CK_Load_Syntax_File:
3462 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3463 break;
3464 case CK_Choose_Syntax:
3465 edit_syntax_dialog (edit, edit->syntax_type);
3466 break;
3468 case CK_Load_Menu_File:
3469 edit_load_cmd (edit, EDIT_FILE_MENU);
3470 break;
3472 case CK_Toggle_Syntax:
3473 if ((option_syntax_highlighting ^= 1) == 1)
3474 edit_load_syntax (edit, NULL, edit->syntax_type);
3475 edit->force |= REDRAW_PAGE;
3476 break;
3478 case CK_Toggle_Tab_TWS:
3479 enable_show_tabs_tws ^= 1;
3480 edit->force |= REDRAW_PAGE;
3481 break;
3483 case CK_Find:
3484 edit_search_cmd (edit, 0);
3485 break;
3486 case CK_Find_Again:
3487 edit_search_cmd (edit, 1);
3488 break;
3489 case CK_Replace:
3490 edit_replace_cmd (edit, 0);
3491 break;
3492 case CK_Replace_Again:
3493 edit_replace_cmd (edit, 1);
3494 break;
3495 case CK_Complete_Word:
3496 /* if text marked shift block */
3497 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3499 edit_move_block_to_left (edit);
3501 else
3503 edit_complete_word_cmd (edit);
3505 break;
3506 case CK_Find_Definition:
3507 edit_get_match_keyword_cmd (edit);
3508 break;
3509 case CK_Quit:
3510 dlg_stop (edit->widget.parent);
3511 break;
3512 case CK_New:
3513 edit_new_cmd (edit);
3514 break;
3515 case CK_Help:
3516 edit_help_cmd (edit);
3517 break;
3518 case CK_Refresh:
3519 edit_refresh_cmd (edit);
3520 break;
3521 case CK_SaveSetupCmd:
3522 save_setup_cmd ();
3523 break;
3524 case CK_About:
3525 query_dialog (_("About"),
3526 _("\n Cooledit v3.11.5\n\n"
3527 " Copyright (C) 1996 the Free Software Foundation\n\n"
3528 " A user friendly text editor written\n"
3529 " for the Midnight Commander.\n"), D_NORMAL, 1, _("&OK"));
3530 break;
3531 case CK_LearnKeys:
3532 learn_keys ();
3533 break;
3534 case CK_Edit_Options:
3535 edit_options_dialog (edit);
3536 break;
3537 case CK_Edit_Save_Mode:
3538 menu_save_mode_cmd ();
3539 break;
3540 case CK_Date:
3542 char s[BUF_MEDIUM];
3543 /* fool gcc to prevent a Y2K warning */
3544 char time_format[] = "_c";
3545 time_format[0] = '%';
3547 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3548 edit_print_string (edit, s);
3549 edit->force |= REDRAW_PAGE;
3550 break;
3552 break;
3553 case CK_Goto:
3554 edit_goto_cmd (edit);
3555 break;
3556 case CK_Paragraph_Format:
3557 format_paragraph (edit, 1);
3558 edit->force |= REDRAW_PAGE;
3559 break;
3560 case CK_Delete_Macro:
3561 edit_delete_macro_cmd (edit);
3562 break;
3563 case CK_Match_Bracket:
3564 edit_goto_matching_bracket (edit);
3565 break;
3566 case CK_User_Menu:
3567 user_menu (edit);
3568 break;
3569 case CK_Sort:
3570 edit_sort_cmd (edit);
3571 break;
3572 case CK_ExtCmd:
3573 edit_ext_cmd (edit);
3574 break;
3575 case CK_Mail:
3576 edit_mail_dialog (edit);
3577 break;
3578 case CK_Shell:
3579 view_other_cmd ();
3580 break;
3581 case CK_SelectCodepage:
3582 edit_select_codepage_cmd (edit);
3583 break;
3584 case CK_Insert_Literal:
3585 edit_insert_literal_cmd (edit);
3586 break;
3587 case CK_Execute_Macro:
3588 edit_execute_macro_cmd (edit);
3589 break;
3590 case CK_Begin_End_Macro:
3591 edit_begin_end_macro_cmd (edit);
3592 break;
3593 case CK_Ext_Mode:
3594 edit->extmod = 1;
3595 break;
3596 default:
3597 break;
3600 /* CK_Pipe_Block */
3601 if ((command / 1000) == 1) /* a shell command */
3602 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3603 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3604 { /* a macro command */
3605 struct macro m[MAX_MACRO_LENGTH];
3606 int nm;
3607 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3608 edit_execute_macro (edit, m, nm);
3611 /* keys which must set the col position, and the search vars */
3612 switch (command)
3614 case CK_Find:
3615 case CK_Find_Again:
3616 case CK_Replace:
3617 case CK_Replace_Again:
3618 case CK_Complete_Word:
3619 edit->prev_col = edit_get_col (edit);
3620 break;
3621 case CK_Up:
3622 case CK_Up_Highlight:
3623 case CK_Up_Alt_Highlight:
3624 case CK_Down:
3625 case CK_Down_Highlight:
3626 case CK_Down_Alt_Highlight:
3627 case CK_Page_Up:
3628 case CK_Page_Up_Highlight:
3629 case CK_Page_Up_Alt_Highlight:
3630 case CK_Page_Down:
3631 case CK_Page_Down_Highlight:
3632 case CK_Page_Down_Alt_Highlight:
3633 case CK_Beginning_Of_Text:
3634 case CK_Beginning_Of_Text_Highlight:
3635 case CK_End_Of_Text:
3636 case CK_End_Of_Text_Highlight:
3637 case CK_Paragraph_Up:
3638 case CK_Paragraph_Up_Highlight:
3639 case CK_Paragraph_Up_Alt_Highlight:
3640 case CK_Paragraph_Down:
3641 case CK_Paragraph_Down_Highlight:
3642 case CK_Paragraph_Down_Alt_Highlight:
3643 case CK_Scroll_Up:
3644 case CK_Scroll_Up_Highlight:
3645 case CK_Scroll_Up_Alt_Highlight:
3646 case CK_Scroll_Down:
3647 case CK_Scroll_Down_Highlight:
3648 case CK_Scroll_Down_Alt_Highlight:
3649 edit->search_start = edit->curs1;
3650 edit->found_len = 0;
3651 break;
3652 default:
3653 edit->found_len = 0;
3654 edit->prev_col = edit_get_col (edit);
3655 edit->search_start = edit->curs1;
3657 edit_find_bracket (edit);
3659 if (option_auto_para_formatting)
3661 switch (command)
3663 case CK_BackSpace:
3664 case CK_Delete:
3665 case CK_Delete_Word_Left:
3666 case CK_Delete_Word_Right:
3667 case CK_Delete_To_Line_End:
3668 case CK_Delete_To_Line_Begin:
3669 format_paragraph (edit, 0);
3670 edit->force |= REDRAW_PAGE;
3676 static void
3677 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
3679 int i = 0;
3681 if (edit->macro_depth++ > 256)
3683 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3684 edit->macro_depth--;
3685 return;
3687 edit->force |= REDRAW_PAGE;
3688 for (; i < n; i++)
3690 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3692 edit_update_screen (edit);
3693 edit->macro_depth--;
3696 /* User edit menu, like user menu (F2) but only in editor. */
3697 static void
3698 user_menu (WEdit * edit)
3700 char *block_file;
3701 int nomark;
3702 long start_mark, end_mark;
3703 struct stat status;
3705 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3707 nomark = eval_marks (edit, &start_mark, &end_mark);
3708 if (nomark == 0)
3709 edit_save_block (edit, block_file, start_mark, end_mark);
3711 /* run shell scripts from menu */
3712 user_menu_cmd (edit);
3714 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
3716 int rc = 0;
3717 FILE *fd;
3719 if (nomark == 0)
3721 /* i.e. we have marked block */
3722 rc = edit_block_delete_cmd (edit);
3725 if (rc == 0)
3726 edit_insert_file (edit, block_file);
3728 /* truncate block file */
3729 fd = fopen (block_file, "w");
3730 if (fd != NULL)
3731 fclose (fd);
3733 edit_refresh_cmd (edit);
3734 edit->force |= REDRAW_COMPLETELY;
3736 g_free (block_file);
3739 void
3740 edit_stack_init (void)
3742 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3744 edit_history_moveto[edit_stack_iterator].filename = NULL;
3745 edit_history_moveto[edit_stack_iterator].line = -1;
3748 edit_stack_iterator = 0;
3751 void
3752 edit_stack_free (void)
3754 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
3755 g_free (edit_history_moveto[edit_stack_iterator].filename);
3758 /* move i lines */
3759 void
3760 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3762 edit_move_updown (edit, i, do_scroll, TRUE);
3765 /* move i lines */
3766 void
3767 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3769 edit_move_updown (edit, i, do_scroll, FALSE);