Ticket #1898 (mcedit: vertical selection bug)
[pantumic.git] / src / editor / edit.c
blob97d8abe4784e5fabaf18e11a39829a08ddde014e
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor low level data handling and cursor fundamentals
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
50 #include "lib/util.h" /* load_file_position(), save_file_position() */
51 #include "lib/timefmt.h" /* time formatting */
52 #include "lib/lock.h"
53 #include "lib/widget.h"
55 #ifdef HAVE_CHARSET
56 #include "lib/charsets.h" /* get_codepage_id */
57 #endif
59 #include "src/filemanager/cmd.h" /* view_other_cmd() */
60 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
62 #include "src/main.h" /* source_codepage */
63 #include "src/setup.h" /* option_tab_spacing */
64 #include "src/learn.h" /* learn_keys */
65 #include "src/keybind-defaults.h"
67 #include "edit-impl.h"
68 #include "edit-widget.h"
70 /*** global variables ****************************************************************************/
72 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
73 int option_typewriter_wrap = 0;
74 int option_auto_para_formatting = 0;
75 int option_fill_tabs_with_spaces = 0;
76 int option_return_does_auto_indent = 1;
77 int option_backspace_through_tabs = 0;
78 int option_fake_half_tabs = 1;
79 int option_save_mode = EDIT_QUICK_SAVE;
80 int option_save_position = 1;
81 int option_max_undo = 32768;
82 int option_persistent_selections = 1;
83 int option_cursor_beyond_eol = 0;
84 int option_line_state = 0;
85 int option_line_state_width = 0;
87 int option_edit_right_extreme = 0;
88 int option_edit_left_extreme = 0;
89 int option_edit_top_extreme = 0;
90 int option_edit_bottom_extreme = 0;
91 int enable_show_tabs_tws = 1;
92 int option_check_nl_at_eof = 0;
93 int show_right_margin = 0;
95 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
96 char *option_backup_ext = NULL;
98 int edit_stack_iterator = 0;
99 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
100 /* magic sequense for say than block is vertical */
101 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
103 /*** file scope macro definitions ****************************************************************/
105 #define TEMP_BUF_LEN 1024
107 #define space_width 1
109 /*** file scope type declarations ****************************************************************/
111 /*** file scope variables ************************************************************************/
113 /* detecting an error on save is easy: just check if every byte has been written. */
114 /* detecting an error on read, is not so easy 'cos there is not way to tell
115 whether you read everything or not. */
116 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
117 static const struct edit_filters
119 const char *read, *write, *extension;
120 } all_filters[] =
122 /* *INDENT-OFF* */
123 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
124 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
125 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
126 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
127 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
128 /* *INDENT-ON* */
131 static long last_bracket = -1;
133 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
135 /*** file scope functions ************************************************************************/
136 /* --------------------------------------------------------------------------------------------- */
140 * here's a quick sketch of the layout: (don't run this through indent.)
142 * (b1 is buffers1 and b2 is buffers2)
145 * \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
146 * ______________________________________|______________________________________
148 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
149 * |-> |-> |-> |-> |-> |-> |
151 * _<------------------------->|<----------------->_
152 * WEdit->curs2 | WEdit->curs1
153 * ^ | ^
154 * | ^|^ |
155 * cursor ||| cursor
156 * |||
157 * file end|||file beginning
162 * This_is_some_file
163 * fin.
166 /* --------------------------------------------------------------------------------------------- */
168 static void user_menu (WEdit * edit);
169 static int left_of_four_spaces (WEdit * edit);
170 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
172 /* --------------------------------------------------------------------------------------------- */
174 static void
175 edit_about (void)
177 const char *header = N_("About");
178 const char *button_name = N_("&OK");
179 const char *const version = "MCEdit " VERSION;
180 char text[BUF_LARGE];
182 int win_len, version_len, button_len;
183 int cols, lines;
185 Dlg_head *about_dlg;
187 #ifdef ENABLE_NLS
188 header = _(header);
189 button_name = _(button_name);
190 #endif
192 button_len = str_term_width1 (button_name) + 5;
193 version_len = str_term_width1 (version);
195 g_snprintf (text, sizeof (text),
196 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
197 " A user friendly text editor\n"
198 " written for the Midnight Commander"));
200 win_len = str_term_width1 (header);
201 win_len = max (win_len, version_len);
202 win_len = max (win_len, button_len);
204 /* count width and height of text */
205 str_msg_term_size (text, &lines, &cols);
206 lines += 9;
207 cols = max (win_len, cols) + 6;
209 /* dialog */
210 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
211 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
213 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
214 add_widget (about_dlg, label_new (5, 3, text));
215 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
216 B_ENTER, NORMAL_BUTTON, button_name, NULL));
218 run_dlg (about_dlg);
219 destroy_dlg (about_dlg);
222 /* --------------------------------------------------------------------------------------------- */
224 * Initialize the buffers for an empty files.
227 static void
228 edit_init_buffers (WEdit * edit)
230 int j;
232 for (j = 0; j <= MAXBUFF; j++)
234 edit->buffers1[j] = NULL;
235 edit->buffers2[j] = NULL;
238 edit->curs1 = 0;
239 edit->curs2 = 0;
240 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
243 /* --------------------------------------------------------------------------------------------- */
245 * Load file OR text into buffers. Set cursor to the beginning of file.
246 * @returns 1 on error.
249 static int
250 edit_load_file_fast (WEdit * edit, const char *filename)
252 long buf, buf2;
253 int file = -1;
254 int ret = 1;
256 edit->curs2 = edit->last_byte;
257 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
259 file = mc_open (filename, O_RDONLY | O_BINARY);
260 if (file == -1)
262 gchar *errmsg;
264 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
265 edit_error_dialog (_("Error"), errmsg);
266 g_free (errmsg);
267 return 1;
270 if (!edit->buffers2[buf2])
271 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
275 if (mc_read (file,
276 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
277 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
278 break;
280 for (buf = buf2 - 1; buf >= 0; buf--)
282 /* edit->buffers2[0] is already allocated */
283 if (!edit->buffers2[buf])
284 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
285 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
286 break;
288 ret = 0;
290 while (0);
291 if (ret)
293 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
294 edit_error_dialog (_("Error"), err_str);
295 g_free (err_str);
297 mc_close (file);
298 return ret;
301 /* --------------------------------------------------------------------------------------------- */
302 /** Return index of the filter or -1 is there is no appropriate filter */
304 static int
305 edit_find_filter (const char *filename)
307 size_t i, l, e;
309 if (filename == NULL)
310 return -1;
312 l = strlen (filename);
313 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
315 e = strlen (all_filters[i].extension);
316 if (l > e)
317 if (!strcmp (all_filters[i].extension, filename + l - e))
318 return i;
320 return -1;
323 /* --------------------------------------------------------------------------------------------- */
325 static char *
326 edit_get_filter (const char *filename)
328 int i;
329 char *p, *quoted_name;
331 i = edit_find_filter (filename);
332 if (i < 0)
333 return NULL;
335 quoted_name = name_quote (filename, 0);
336 p = g_strdup_printf (all_filters[i].read, quoted_name);
337 g_free (quoted_name);
338 return p;
341 /* --------------------------------------------------------------------------------------------- */
343 static long
344 edit_insert_stream (WEdit * edit, FILE * f)
346 int c;
347 long i = 0;
348 while ((c = fgetc (f)) >= 0)
350 edit_insert (edit, c);
351 i++;
353 return i;
356 /* --------------------------------------------------------------------------------------------- */
357 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
359 static int
360 check_file_access (WEdit * edit, const char *filename, struct stat *st)
362 int file;
363 gchar *errmsg = NULL;
365 /* Try opening an existing file */
366 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
368 if (file < 0)
371 * Try creating the file. O_EXCL prevents following broken links
372 * and opening existing files.
374 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
375 if (file < 0)
377 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
378 goto cleanup;
380 else
382 /* New file, delete it if it's not modified or saved */
383 edit->delete_file = 1;
387 /* Check what we have opened */
388 if (mc_fstat (file, st) < 0)
390 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
391 goto cleanup;
394 /* We want to open regular files only */
395 if (!S_ISREG (st->st_mode))
397 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
398 goto cleanup;
402 * Don't delete non-empty files.
403 * O_EXCL should prevent it, but let's be on the safe side.
405 if (st->st_size > 0)
406 edit->delete_file = 0;
408 if (st->st_size >= SIZE_LIMIT)
409 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
411 cleanup:
412 (void) mc_close (file);
414 if (errmsg != NULL)
416 edit_error_dialog (_("Error"), errmsg);
417 g_free (errmsg);
418 return 1;
420 return 0;
423 /* --------------------------------------------------------------------------------------------- */
425 * Open the file and load it into the buffers, either directly or using
426 * a filter. Return 0 on success, 1 on error.
428 * Fast loading (edit_load_file_fast) is used when the file size is
429 * known. In this case the data is read into the buffers by blocks.
430 * If the file size is not known, the data is loaded byte by byte in
431 * edit_insert_file.
434 static int
435 edit_load_file (WEdit * edit)
437 int fast_load = 1;
439 /* Cannot do fast load if a filter is used */
440 if (edit_find_filter (edit->filename) >= 0)
441 fast_load = 0;
444 * VFS may report file size incorrectly, and slow load is not a big
445 * deal considering overhead in VFS.
447 if (!vfs_file_is_local (edit->filename))
448 fast_load = 0;
451 * FIXME: line end translation should disable fast loading as well
452 * Consider doing fseek() to the end and ftell() for the real size.
455 if (*edit->filename)
457 /* If we are dealing with a real file, check that it exists */
458 if (check_file_access (edit, edit->filename, &edit->stat1))
459 return 1;
461 else
463 /* nothing to load */
464 fast_load = 0;
467 edit_init_buffers (edit);
469 if (fast_load)
471 edit->last_byte = edit->stat1.st_size;
472 edit_load_file_fast (edit, edit->filename);
473 /* If fast load was used, the number of lines wasn't calculated */
474 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
476 else
478 edit->last_byte = 0;
479 if (*edit->filename)
481 edit->stack_disable = 1;
482 if (!edit_insert_file (edit, edit->filename))
484 edit_clean (edit);
485 return 1;
487 edit->stack_disable = 0;
490 edit->lb = LB_ASIS;
491 return 0;
494 /* --------------------------------------------------------------------------------------------- */
495 /** Restore saved cursor position in the file */
497 static void
498 edit_load_position (WEdit * edit)
500 char *filename;
501 long line, column;
502 off_t offset;
504 if (!edit->filename || !*edit->filename)
505 return;
507 filename = vfs_canon (edit->filename);
508 load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
509 g_free (filename);
511 if (line > 0)
513 edit_move_to_line (edit, line - 1);
514 edit->prev_col = column;
516 else if (offset > 0)
518 edit_cursor_move (edit, offset);
519 line = edit->curs_line;
520 edit->search_start = edit->curs1;
523 book_mark_restore (edit, BOOK_MARK_COLOR);
525 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
526 edit_move_display (edit, line - (edit->num_widget_lines / 2));
529 /* --------------------------------------------------------------------------------------------- */
530 /** Save cursor position in the file */
532 static void
533 edit_save_position (WEdit * edit)
535 char *filename;
537 if (edit->filename == NULL || *edit->filename == '\0')
538 return;
540 filename = vfs_canon (edit->filename);
542 book_mark_serialize (edit, BOOK_MARK_COLOR);
543 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
544 edit->serialized_bookmarks);
545 edit->serialized_bookmarks = NULL;
547 g_free (filename);
550 /* --------------------------------------------------------------------------------------------- */
551 /** Clean the WEdit stricture except the widget part */
553 static void
554 edit_purge_widget (WEdit * edit)
556 size_t len = sizeof (WEdit) - sizeof (Widget);
557 char *start = (char *) edit + sizeof (Widget);
558 memset (start, 0, len);
559 edit->macro_i = -1; /* not recording a macro */
562 /* --------------------------------------------------------------------------------------------- */
564 static void
565 edit_set_keymap (void)
567 editor_map = default_editor_keymap;
568 if (editor_keymap && editor_keymap->len > 0)
569 editor_map = (global_keymap_t *) editor_keymap->data;
571 editor_x_map = default_editor_x_keymap;
572 if (editor_x_keymap && editor_x_keymap->len > 0)
573 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
576 /* --------------------------------------------------------------------------------------------- */
578 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
579 then the file should be as it was when he loaded up. Then set edit->modified to 0.
582 static long
583 pop_action (WEdit * edit)
585 long c;
586 unsigned long sp = edit->stack_pointer;
588 if (sp == edit->stack_bottom)
589 return STACK_BOTTOM;
591 sp = (sp - 1) & edit->stack_size_mask;
592 c = edit->undo_stack[sp];
593 if (c >= 0)
595 /* edit->undo_stack[sp] = '@'; */
596 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
597 return c;
600 if (sp == edit->stack_bottom)
601 return STACK_BOTTOM;
603 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
604 if (edit->undo_stack[sp] == -2)
606 /* edit->undo_stack[sp] = '@'; */
607 edit->stack_pointer = sp;
609 else
610 edit->undo_stack[sp]++;
612 return c;
615 /* --------------------------------------------------------------------------------------------- */
616 /** is called whenever a modification is made by one of the four routines below */
618 static void
619 edit_modification (WEdit * edit)
621 edit->caches_valid = 0;
622 edit->screen_modified = 1;
624 /* raise lock when file modified */
625 if (!edit->modified && !edit->delete_file)
626 edit->locked = edit_lock_file (edit);
627 edit->modified = 1;
630 /* --------------------------------------------------------------------------------------------- */
632 static void
633 edit_insert_over (WEdit * edit)
635 int i;
637 for (i = 0; i < edit->over_col; i++)
639 edit_insert (edit, ' ');
641 edit->over_col = 0;
644 /* --------------------------------------------------------------------------------------------- */
646 static int
647 edit_backspace (WEdit * edit, const int byte_delete)
649 int p = 0;
650 int cw = 1;
651 int i;
653 if (!edit->curs1)
654 return 0;
656 cw = 1;
658 if (edit->utf8 && byte_delete == 0)
660 edit_get_prev_utf (edit, edit->curs1, &cw);
661 if (cw < 1)
662 cw = 1;
664 for (i = 1; i <= cw; i++)
666 if (edit->mark1 >= edit->curs1)
668 edit->mark1--;
669 edit->end_mark_curs--;
671 if (edit->mark2 >= edit->curs1)
672 edit->mark2--;
673 if (edit->last_get_rule >= edit->curs1)
674 edit->last_get_rule--;
676 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
677 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
678 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
680 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
681 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
683 edit->last_byte--;
684 edit->curs1--;
685 edit_push_action (edit, p);
687 edit_modification (edit);
688 if (p == '\n')
690 if (edit->book_mark)
691 book_mark_dec (edit, edit->curs_line);
692 edit->curs_line--;
693 edit->total_lines--;
694 edit->force |= REDRAW_AFTER_CURSOR;
697 if (edit->curs1 < edit->start_display)
699 edit->start_display--;
700 if (p == '\n')
701 edit->start_line--;
704 return p;
708 /* --------------------------------------------------------------------------------------------- */
710 #ifdef FAST_MOVE_CURSOR
711 static void
712 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
714 unsigned long next;
715 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
717 edit->curs_line--;
718 next -= (unsigned long) dest;
719 n -= next;
720 src += next;
721 dest += next;
724 #endif /* FAST_MOVE_CURSOR */
726 /* --------------------------------------------------------------------------------------------- */
727 /* high level cursor movement commands */
728 /* --------------------------------------------------------------------------------------------- */
730 static int
731 is_in_indent (WEdit * edit)
733 long p = edit_bol (edit, edit->curs1);
734 while (p < edit->curs1)
735 if (!strchr (" \t", edit_get_byte (edit, p++)))
736 return 0;
737 return 1;
740 /* --------------------------------------------------------------------------------------------- */
742 static int
743 is_blank (WEdit * edit, long offset)
745 long s, f;
746 int c;
747 s = edit_bol (edit, offset);
748 f = edit_eol (edit, offset) - 1;
749 while (s <= f)
751 c = edit_get_byte (edit, s++);
752 if (!isspace (c))
753 return 0;
755 return 1;
759 /* --------------------------------------------------------------------------------------------- */
760 /** returns the offset of line i */
762 static long
763 edit_find_line (WEdit * edit, int line)
765 int i, j = 0;
766 int m = 2000000000;
767 if (!edit->caches_valid)
769 for (i = 0; i < N_LINE_CACHES; i++)
770 edit->line_numbers[i] = edit->line_offsets[i] = 0;
771 /* three offsets that we *know* are line 0 at 0 and these two: */
772 edit->line_numbers[1] = edit->curs_line;
773 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
774 edit->line_numbers[2] = edit->total_lines;
775 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
776 edit->caches_valid = 1;
778 if (line >= edit->total_lines)
779 return edit->line_offsets[2];
780 if (line <= 0)
781 return 0;
782 /* find the closest known point */
783 for (i = 0; i < N_LINE_CACHES; i++)
785 int n;
786 n = abs (edit->line_numbers[i] - line);
787 if (n < m)
789 m = n;
790 j = i;
793 if (m == 0)
794 return edit->line_offsets[j]; /* know the offset exactly */
795 if (m == 1 && j >= 3)
796 i = j; /* one line different - caller might be looping, so stay in this cache */
797 else
798 i = 3 + (rand () % (N_LINE_CACHES - 3));
799 if (line > edit->line_numbers[j])
800 edit->line_offsets[i] =
801 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
802 else
803 edit->line_offsets[i] =
804 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
805 edit->line_numbers[i] = line;
806 return edit->line_offsets[i];
809 /* --------------------------------------------------------------------------------------------- */
810 /** moves up until a blank line is reached, or until just
811 before a non-blank line is reached */
813 static void
814 edit_move_up_paragraph (WEdit * edit, int do_scroll)
816 int i = 0;
817 if (edit->curs_line > 1)
819 if (line_is_blank (edit, edit->curs_line))
821 if (line_is_blank (edit, edit->curs_line - 1))
823 for (i = edit->curs_line - 1; i; i--)
824 if (!line_is_blank (edit, i))
826 i++;
827 break;
830 else
832 for (i = edit->curs_line - 1; i; i--)
833 if (line_is_blank (edit, i))
834 break;
837 else
839 for (i = edit->curs_line - 1; i; i--)
840 if (line_is_blank (edit, i))
841 break;
844 edit_move_up (edit, edit->curs_line - i, do_scroll);
847 /* --------------------------------------------------------------------------------------------- */
848 /** moves down until a blank line is reached, or until just
849 before a non-blank line is reached */
851 static void
852 edit_move_down_paragraph (WEdit * edit, int do_scroll)
854 int i;
855 if (edit->curs_line >= edit->total_lines - 1)
857 i = edit->total_lines;
859 else
861 if (line_is_blank (edit, edit->curs_line))
863 if (line_is_blank (edit, edit->curs_line + 1))
865 for (i = edit->curs_line + 1; i; i++)
866 if (!line_is_blank (edit, i) || i > edit->total_lines)
868 i--;
869 break;
872 else
874 for (i = edit->curs_line + 1; i; i++)
875 if (line_is_blank (edit, i) || i >= edit->total_lines)
876 break;
879 else
881 for (i = edit->curs_line + 1; i; i++)
882 if (line_is_blank (edit, i) || i >= edit->total_lines)
883 break;
886 edit_move_down (edit, i - edit->curs_line, do_scroll);
889 /* --------------------------------------------------------------------------------------------- */
891 static void
892 edit_begin_page (WEdit * edit)
894 edit_update_curs_row (edit);
895 edit_move_up (edit, edit->curs_row, 0);
898 /* --------------------------------------------------------------------------------------------- */
900 static void
901 edit_end_page (WEdit * edit)
903 edit_update_curs_row (edit);
904 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
908 /* --------------------------------------------------------------------------------------------- */
909 /** goto beginning of text */
911 static void
912 edit_move_to_top (WEdit * edit)
914 if (edit->curs_line)
916 edit_cursor_move (edit, -edit->curs1);
917 edit_move_to_prev_col (edit, 0);
918 edit->force |= REDRAW_PAGE;
919 edit->search_start = 0;
920 edit_update_curs_row (edit);
925 /* --------------------------------------------------------------------------------------------- */
926 /** goto end of text */
928 static void
929 edit_move_to_bottom (WEdit * edit)
931 if (edit->curs_line < edit->total_lines)
933 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
934 edit->start_display = edit->last_byte;
935 edit->start_line = edit->total_lines;
936 edit_scroll_upward (edit, edit->num_widget_lines - 1);
937 edit->force |= REDRAW_PAGE;
941 /* --------------------------------------------------------------------------------------------- */
942 /** goto beginning of line */
944 static void
945 edit_cursor_to_bol (WEdit * edit)
947 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
948 edit->search_start = edit->curs1;
949 edit->prev_col = edit_get_col (edit);
950 edit->over_col = 0;
953 /* --------------------------------------------------------------------------------------------- */
954 /** goto end of line */
956 static void
957 edit_cursor_to_eol (WEdit * edit)
959 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
960 edit->search_start = edit->curs1;
961 edit->prev_col = edit_get_col (edit);
962 edit->over_col = 0;
965 /* --------------------------------------------------------------------------------------------- */
967 static unsigned long
968 my_type_of (int c)
970 int x, r = 0;
971 const char *p, *q;
972 const char option_chars_move_whole_word[] =
973 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
975 if (!c)
976 return 0;
977 if (c == '!')
979 if (*option_chars_move_whole_word == '!')
980 return 2;
981 return 0x80000000UL;
983 if (g_ascii_isupper ((gchar) c))
984 c = 'A';
985 else if (g_ascii_islower ((gchar) c))
986 c = 'a';
987 else if (g_ascii_isalpha (c))
988 c = 'a';
989 else if (isdigit (c))
990 c = '0';
991 else if (isspace (c))
992 c = ' ';
993 q = strchr (option_chars_move_whole_word, c);
994 if (!q)
995 return 0xFFFFFFFFUL;
998 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
999 if (*p == '!')
1000 x <<= 1;
1001 r |= x;
1003 while ((q = strchr (q + 1, c)));
1004 return r;
1007 /* --------------------------------------------------------------------------------------------- */
1009 static void
1010 edit_left_word_move (WEdit * edit, int s)
1012 for (;;)
1014 int c1, c2;
1015 if (edit->column_highlight
1016 && edit->mark1 != edit->mark2
1017 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1018 break;
1019 edit_cursor_move (edit, -1);
1020 if (!edit->curs1)
1021 break;
1022 c1 = edit_get_byte (edit, edit->curs1 - 1);
1023 c2 = edit_get_byte (edit, edit->curs1);
1024 if (!(my_type_of (c1) & my_type_of (c2)))
1025 break;
1026 if (isspace (c1) && !isspace (c2))
1027 break;
1028 if (s)
1029 if (!isspace (c1) && isspace (c2))
1030 break;
1034 /* --------------------------------------------------------------------------------------------- */
1036 static void
1037 edit_left_word_move_cmd (WEdit * edit)
1039 edit_left_word_move (edit, 0);
1040 edit->force |= REDRAW_PAGE;
1043 /* --------------------------------------------------------------------------------------------- */
1045 static void
1046 edit_right_word_move (WEdit * edit, int s)
1048 for (;;)
1050 int c1, c2;
1051 if (edit->column_highlight
1052 && edit->mark1 != edit->mark2
1053 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1054 break;
1055 edit_cursor_move (edit, 1);
1056 if (edit->curs1 >= edit->last_byte)
1057 break;
1058 c1 = edit_get_byte (edit, edit->curs1 - 1);
1059 c2 = edit_get_byte (edit, edit->curs1);
1060 if (!(my_type_of (c1) & my_type_of (c2)))
1061 break;
1062 if (isspace (c1) && !isspace (c2))
1063 break;
1064 if (s)
1065 if (!isspace (c1) && isspace (c2))
1066 break;
1070 /* --------------------------------------------------------------------------------------------- */
1072 static void
1073 edit_right_word_move_cmd (WEdit * edit)
1075 edit_right_word_move (edit, 0);
1076 edit->force |= REDRAW_PAGE;
1079 /* --------------------------------------------------------------------------------------------- */
1081 static void
1082 edit_right_char_move_cmd (WEdit * edit)
1084 int cw = 1;
1085 int c = 0;
1086 if (edit->utf8)
1088 c = edit_get_utf (edit, edit->curs1, &cw);
1089 if (cw < 1)
1090 cw = 1;
1092 else
1094 c = edit_get_byte (edit, edit->curs1);
1096 if (option_cursor_beyond_eol && c == '\n')
1098 edit->over_col++;
1100 else
1102 edit_cursor_move (edit, cw);
1106 /* --------------------------------------------------------------------------------------------- */
1108 static void
1109 edit_left_char_move_cmd (WEdit * edit)
1111 int cw = 1;
1112 if (edit->column_highlight
1113 && option_cursor_beyond_eol
1114 && edit->mark1 != edit->mark2
1115 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1116 return;
1117 if (edit->utf8)
1119 edit_get_prev_utf (edit, edit->curs1, &cw);
1120 if (cw < 1)
1121 cw = 1;
1123 if (option_cursor_beyond_eol && edit->over_col > 0)
1125 edit->over_col--;
1127 else
1129 edit_cursor_move (edit, -cw);
1133 /* --------------------------------------------------------------------------------------------- */
1134 /** Up or down cursor moving.
1135 direction = TRUE - move up
1136 = FALSE - move down
1139 static void
1140 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1142 unsigned long p;
1143 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1145 if (i > l)
1146 i = l;
1148 if (i == 0)
1149 return;
1151 if (i > 1)
1152 edit->force |= REDRAW_PAGE;
1153 if (do_scroll)
1155 if (direction)
1156 edit_scroll_upward (edit, i);
1157 else
1158 edit_scroll_downward (edit, i);
1160 p = edit_bol (edit, edit->curs1);
1162 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1164 edit_cursor_move (edit, p - edit->curs1);
1166 edit_move_to_prev_col (edit, p);
1168 /* search start of current multibyte char (like CJK) */
1169 if (edit->curs1 + 1 < edit->last_byte)
1171 edit_right_char_move_cmd (edit);
1172 edit_left_char_move_cmd (edit);
1175 edit->search_start = edit->curs1;
1176 edit->found_len = 0;
1179 /* --------------------------------------------------------------------------------------------- */
1181 static void
1182 edit_right_delete_word (WEdit * edit)
1184 int c1, c2;
1185 for (;;)
1187 if (edit->curs1 >= edit->last_byte)
1188 break;
1189 c1 = edit_delete (edit, 1);
1190 c2 = edit_get_byte (edit, edit->curs1);
1191 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1192 break;
1193 if (!(my_type_of (c1) & my_type_of (c2)))
1194 break;
1198 /* --------------------------------------------------------------------------------------------- */
1200 static void
1201 edit_left_delete_word (WEdit * edit)
1203 int c1, c2;
1204 for (;;)
1206 if (edit->curs1 <= 0)
1207 break;
1208 c1 = edit_backspace (edit, 1);
1209 c2 = edit_get_byte (edit, edit->curs1 - 1);
1210 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1211 break;
1212 if (!(my_type_of (c1) & my_type_of (c2)))
1213 break;
1217 /* --------------------------------------------------------------------------------------------- */
1219 the start column position is not recorded, and hence does not
1220 undo as it happed. But who would notice.
1223 static void
1224 edit_do_undo (WEdit * edit)
1226 long ac;
1227 long count = 0;
1229 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1230 edit->over_col = 0;
1231 while ((ac = pop_action (edit)) < KEY_PRESS)
1233 switch ((int) ac)
1235 case STACK_BOTTOM:
1236 goto done_undo;
1237 case CURS_RIGHT:
1238 edit_cursor_move (edit, 1);
1239 break;
1240 case CURS_LEFT:
1241 edit_cursor_move (edit, -1);
1242 break;
1243 case BACKSPACE:
1244 edit_backspace (edit, 1);
1245 break;
1246 case DELCHAR:
1247 edit_delete (edit, 1);
1248 break;
1249 case COLUMN_ON:
1250 edit->column_highlight = 1;
1251 break;
1252 case COLUMN_OFF:
1253 edit->column_highlight = 0;
1254 break;
1256 if (ac >= 256 && ac < 512)
1257 edit_insert_ahead (edit, ac - 256);
1258 if (ac >= 0 && ac < 256)
1259 edit_insert (edit, ac);
1261 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1263 edit->mark1 = ac - MARK_1;
1264 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1266 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1268 edit->mark2 = ac - MARK_2;
1269 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1271 if (count++)
1272 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1275 if (edit->start_display > ac - KEY_PRESS)
1277 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1278 edit->force |= REDRAW_PAGE;
1280 else if (edit->start_display < ac - KEY_PRESS)
1282 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1283 edit->force |= REDRAW_PAGE;
1285 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1286 edit_update_curs_row (edit);
1288 done_undo:;
1289 edit->stack_disable = 0;
1292 /* --------------------------------------------------------------------------------------------- */
1294 static void
1295 edit_delete_to_line_end (WEdit * edit)
1297 while (edit_get_byte (edit, edit->curs1) != '\n')
1299 if (!edit->curs2)
1300 break;
1301 edit_delete (edit, 1);
1305 /* --------------------------------------------------------------------------------------------- */
1307 static void
1308 edit_delete_to_line_begin (WEdit * edit)
1310 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1312 if (!edit->curs1)
1313 break;
1314 edit_backspace (edit, 1);
1318 /* --------------------------------------------------------------------------------------------- */
1320 static int
1321 is_aligned_on_a_tab (WEdit * edit)
1323 edit_update_curs_col (edit);
1324 return !((edit->curs_col % (TAB_SIZE * space_width))
1325 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1328 /* --------------------------------------------------------------------------------------------- */
1330 static int
1331 right_of_four_spaces (WEdit * edit)
1333 int i, ch = 0;
1334 for (i = 1; i <= HALF_TAB_SIZE; i++)
1335 ch |= edit_get_byte (edit, edit->curs1 - i);
1336 if (ch == ' ')
1337 return is_aligned_on_a_tab (edit);
1338 return 0;
1341 /* --------------------------------------------------------------------------------------------- */
1343 static int
1344 left_of_four_spaces (WEdit * edit)
1346 int i, ch = 0;
1347 for (i = 0; i < HALF_TAB_SIZE; i++)
1348 ch |= edit_get_byte (edit, edit->curs1 + i);
1349 if (ch == ' ')
1350 return is_aligned_on_a_tab (edit);
1351 return 0;
1354 /* --------------------------------------------------------------------------------------------- */
1356 static void
1357 edit_auto_indent (WEdit * edit)
1359 long p;
1360 char c;
1361 p = edit->curs1;
1362 /* use the previous line as a template */
1363 p = edit_move_backward (edit, p, 1);
1364 /* copy the leading whitespace of the line */
1365 for (;;)
1366 { /* no range check - the line _is_ \n-terminated */
1367 c = edit_get_byte (edit, p++);
1368 if (c != ' ' && c != '\t')
1369 break;
1370 edit_insert (edit, c);
1374 /* --------------------------------------------------------------------------------------------- */
1376 static inline void
1377 edit_double_newline (WEdit * edit)
1379 edit_insert (edit, '\n');
1380 if (edit_get_byte (edit, edit->curs1) == '\n')
1381 return;
1382 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1383 return;
1384 edit->force |= REDRAW_PAGE;
1385 edit_insert (edit, '\n');
1388 /* --------------------------------------------------------------------------------------------- */
1390 static inline void
1391 edit_tab_cmd (WEdit * edit)
1393 int i;
1395 if (option_fake_half_tabs)
1397 if (is_in_indent (edit))
1399 /*insert a half tab (usually four spaces) unless there is a
1400 half tab already behind, then delete it and insert a
1401 full tab. */
1402 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
1404 for (i = 1; i <= HALF_TAB_SIZE; i++)
1405 edit_backspace (edit, 1);
1406 edit_insert (edit, '\t');
1408 else
1410 insert_spaces_tab (edit, 1);
1412 return;
1415 if (option_fill_tabs_with_spaces)
1417 insert_spaces_tab (edit, 0);
1419 else
1421 edit_insert (edit, '\t');
1425 /* --------------------------------------------------------------------------------------------- */
1427 static void
1428 check_and_wrap_line (WEdit * edit)
1430 int curs, c;
1431 if (!option_typewriter_wrap)
1432 return;
1433 edit_update_curs_col (edit);
1434 if (edit->curs_col < option_word_wrap_line_length)
1435 return;
1436 curs = edit->curs1;
1437 for (;;)
1439 curs--;
1440 c = edit_get_byte (edit, curs);
1441 if (c == '\n' || curs <= 0)
1443 edit_insert (edit, '\n');
1444 return;
1446 if (c == ' ' || c == '\t')
1448 int current = edit->curs1;
1449 edit_cursor_move (edit, curs - edit->curs1 + 1);
1450 edit_insert (edit, '\n');
1451 edit_cursor_move (edit, current - edit->curs1 + 1);
1452 return;
1457 /* --------------------------------------------------------------------------------------------- */
1458 /** this find the matching bracket in either direction, and sets edit->bracket */
1460 static long
1461 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1463 const char *const b = "{}{[][()(", *p;
1464 int i = 1, a, inc = -1, c, d, n = 0;
1465 unsigned long j = 0;
1466 long q;
1467 edit_update_curs_row (edit);
1468 c = edit_get_byte (edit, edit->curs1);
1469 p = strchr (b, c);
1470 /* no limit */
1471 if (!furthest_bracket_search)
1472 furthest_bracket_search--;
1473 /* not on a bracket at all */
1474 if (!p)
1475 return -1;
1476 /* the matching bracket */
1477 d = p[1];
1478 /* going left or right? */
1479 if (strchr ("{[(", c))
1480 inc = 1;
1481 for (q = edit->curs1 + inc;; q += inc)
1483 /* out of buffer? */
1484 if (q >= edit->last_byte || q < 0)
1485 break;
1486 a = edit_get_byte (edit, q);
1487 /* don't want to eat CPU */
1488 if (j++ > furthest_bracket_search)
1489 break;
1490 /* out of screen? */
1491 if (in_screen)
1493 if (q < edit->start_display)
1494 break;
1495 /* count lines if searching downward */
1496 if (inc > 0 && a == '\n')
1497 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
1498 break;
1500 /* count bracket depth */
1501 i += (a == c) - (a == d);
1502 /* return if bracket depth is zero */
1503 if (!i)
1504 return q;
1506 /* no match */
1507 return -1;
1510 /* --------------------------------------------------------------------------------------------- */
1512 static inline void
1513 edit_goto_matching_bracket (WEdit * edit)
1515 long q;
1517 q = edit_get_bracket (edit, 0, 0);
1518 if (q >= 0)
1520 edit->bracket = edit->curs1;
1521 edit->force |= REDRAW_PAGE;
1522 edit_cursor_move (edit, q - edit->curs1);
1526 /* --------------------------------------------------------------------------------------------- */
1528 static void
1529 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
1531 int i = 0;
1533 if (edit->macro_depth++ > 256)
1535 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
1536 edit->macro_depth--;
1537 return;
1539 edit->force |= REDRAW_PAGE;
1540 for (; i < n; i++)
1542 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
1544 edit_update_screen (edit);
1545 edit->macro_depth--;
1548 /* --------------------------------------------------------------------------------------------- */
1549 /** User edit menu, like user menu (F2) but only in editor. */
1551 static void
1552 user_menu (WEdit * edit)
1554 char *block_file;
1555 int nomark;
1556 long start_mark, end_mark;
1557 struct stat status;
1559 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
1561 nomark = eval_marks (edit, &start_mark, &end_mark);
1562 if (nomark == 0)
1563 edit_save_block (edit, block_file, start_mark, end_mark);
1565 /* run shell scripts from menu */
1566 user_menu_cmd (edit);
1568 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
1570 int rc = 0;
1571 FILE *fd;
1573 if (nomark == 0)
1575 /* i.e. we have marked block */
1576 rc = edit_block_delete_cmd (edit);
1579 if (rc == 0)
1580 edit_insert_file (edit, block_file);
1582 /* truncate block file */
1583 fd = fopen (block_file, "w");
1584 if (fd != NULL)
1585 fclose (fd);
1587 edit_refresh_cmd (edit);
1588 edit->force |= REDRAW_COMPLETELY;
1590 g_free (block_file);
1593 /* --------------------------------------------------------------------------------------------- */
1594 /*** public functions ****************************************************************************/
1595 /* --------------------------------------------------------------------------------------------- */
1598 edit_get_byte (WEdit * edit, long byte_index)
1600 unsigned long p;
1601 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1602 return '\n';
1604 if (byte_index >= edit->curs1)
1606 p = edit->curs1 + edit->curs2 - byte_index - 1;
1607 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1609 else
1611 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1615 /* --------------------------------------------------------------------------------------------- */
1617 char *
1618 edit_get_byte_ptr (WEdit * edit, long byte_index)
1620 unsigned long p;
1621 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1622 return NULL;
1624 if (byte_index >= edit->curs1)
1626 p = edit->curs1 + edit->curs2 - byte_index - 1;
1627 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1628 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1630 else
1632 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
1633 (byte_index & M_EDIT_BUF_SIZE));
1637 /* --------------------------------------------------------------------------------------------- */
1639 char *
1640 edit_get_buf_ptr (WEdit * edit, long byte_index)
1642 unsigned long p;
1644 if (byte_index >= (edit->curs1 + edit->curs2))
1645 byte_index--;
1647 if (byte_index < 0)
1648 return NULL;
1650 if (byte_index >= edit->curs1)
1652 p = edit->curs1 + edit->curs2 - 1;
1653 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1654 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1656 else
1658 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
1662 /* --------------------------------------------------------------------------------------------- */
1665 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1667 gchar *str = NULL;
1668 int res = -1;
1669 gunichar ch;
1670 gchar *next_ch = NULL;
1671 int width = 0;
1673 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1675 *char_width = 0;
1676 return '\n';
1679 str = edit_get_byte_ptr (edit, byte_index);
1681 if (str == NULL)
1683 *char_width = 0;
1684 return 0;
1687 res = g_utf8_get_char_validated (str, -1);
1689 if (res < 0)
1691 ch = *str;
1692 width = 0;
1694 else
1696 ch = res;
1697 /* Calculate UTF-8 char width */
1698 next_ch = g_utf8_next_char (str);
1699 if (next_ch)
1701 width = next_ch - str;
1703 else
1705 ch = 0;
1706 width = 0;
1709 *char_width = width;
1710 return ch;
1713 /* --------------------------------------------------------------------------------------------- */
1716 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
1718 gchar *str, *buf = NULL;
1719 int res = -1;
1720 gunichar ch;
1721 gchar *next_ch = NULL;
1722 int width = 0;
1724 if (byte_index > 0)
1725 byte_index--;
1727 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1729 *char_width = 0;
1730 return 0;
1733 ch = edit_get_utf (edit, byte_index, &width);
1735 if (width == 1)
1737 *char_width = width;
1738 return ch;
1741 str = edit_get_byte_ptr (edit, byte_index);
1742 buf = edit_get_buf_ptr (edit, byte_index);
1743 if (str == NULL || buf == NULL)
1745 *char_width = 0;
1746 return 0;
1748 /* get prev utf8 char */
1749 if (str != buf)
1750 str = g_utf8_find_prev_char (buf, str);
1752 res = g_utf8_get_char_validated (str, -1);
1754 if (res < 0)
1756 ch = *str;
1757 width = 0;
1759 else
1761 ch = res;
1762 /* Calculate UTF-8 char width */
1763 next_ch = g_utf8_next_char (str);
1764 if (next_ch)
1766 width = next_ch - str;
1768 else
1770 ch = 0;
1771 width = 0;
1774 *char_width = width;
1775 return ch;
1778 /* --------------------------------------------------------------------------------------------- */
1780 char *
1781 edit_get_write_filter (const char *write_name, const char *filename)
1783 int i;
1784 char *p, *writename;
1786 i = edit_find_filter (filename);
1787 if (i < 0)
1788 return NULL;
1790 writename = name_quote (write_name, 0);
1791 p = g_strdup_printf (all_filters[i].write, writename);
1792 g_free (writename);
1793 return p;
1796 /* --------------------------------------------------------------------------------------------- */
1798 long
1799 edit_write_stream (WEdit * edit, FILE * f)
1801 long i;
1803 if (edit->lb == LB_ASIS)
1805 for (i = 0; i < edit->last_byte; i++)
1806 if (fputc (edit_get_byte (edit, i), f) < 0)
1807 break;
1808 return i;
1811 /* change line breaks */
1812 for (i = 0; i < edit->last_byte; i++)
1814 unsigned char c = edit_get_byte (edit, i);
1816 if (!(c == '\n' || c == '\r'))
1818 /* not line break */
1819 if (fputc (c, f) < 0)
1820 return i;
1822 else
1823 { /* (c == '\n' || c == '\r') */
1824 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1826 switch (edit->lb)
1828 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1829 /* put one line break unconditionally */
1830 if (fputc ('\n', f) < 0)
1831 return i;
1833 i++; /* 2 chars are processed */
1835 if (c == '\r' && c1 == '\n')
1836 /* Windows line break; go to the next char */
1837 break;
1839 if (c == '\r' && c1 == '\r')
1841 /* two Macintosh line breaks; put second line break */
1842 if (fputc ('\n', f) < 0)
1843 return i;
1844 break;
1847 if (fputc (c1, f) < 0)
1848 return i;
1849 break;
1851 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1852 /* put one line break unconditionally */
1853 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1854 return i;
1856 if (c == '\r' && c1 == '\n')
1857 /* Windows line break; go to the next char */
1858 i++;
1859 break;
1861 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1862 /* put one line break unconditionally */
1863 if (fputc ('\r', f) < 0)
1864 return i;
1866 i++; /* 2 chars are processed */
1868 if (c == '\r' && c1 == '\n')
1869 /* Windows line break; go to the next char */
1870 break;
1872 if (c == '\n' && c1 == '\n')
1874 /* two Windows line breaks; put second line break */
1875 if (fputc ('\r', f) < 0)
1876 return i;
1877 break;
1880 if (fputc (c1, f) < 0)
1881 return i;
1882 break;
1883 case LB_ASIS: /* default without changes */
1884 break;
1889 return edit->last_byte;
1892 /* --------------------------------------------------------------------------------------------- */
1893 /** inserts a file at the cursor, returns 1 on success */
1895 edit_insert_file (WEdit * edit, const char *filename)
1897 char *p;
1899 p = edit_get_filter (filename);
1900 if (p != NULL)
1902 FILE *f;
1903 long current = edit->curs1;
1904 f = (FILE *) popen (p, "r");
1905 if (f != NULL)
1907 edit_insert_stream (edit, f);
1908 edit_cursor_move (edit, current - edit->curs1);
1909 if (pclose (f) > 0)
1911 char *errmsg;
1912 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
1913 edit_error_dialog (_("Error"), errmsg);
1914 g_free (errmsg);
1915 g_free (p);
1916 return 0;
1919 else
1921 char *errmsg;
1922 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
1923 edit_error_dialog (_("Error"), errmsg);
1924 g_free (errmsg);
1925 g_free (p);
1926 return 0;
1928 g_free (p);
1930 else
1932 int i, file, blocklen;
1933 long current = edit->curs1;
1934 int vertical_insertion = 0;
1935 char *buf;
1936 file = mc_open (filename, O_RDONLY | O_BINARY);
1937 if (file == -1)
1938 return 0;
1939 buf = g_malloc0 (TEMP_BUF_LEN);
1940 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
1941 if (blocklen > 0)
1943 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
1944 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
1945 vertical_insertion = 1;
1946 else
1947 mc_lseek (file, 0, SEEK_SET);
1949 if (vertical_insertion)
1950 blocklen = edit_insert_column_of_text_from_file (edit, file);
1951 else
1952 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
1953 for (i = 0; i < blocklen; i++)
1954 edit_insert (edit, buf[i]);
1955 edit_cursor_move (edit, current - edit->curs1);
1956 g_free (buf);
1957 mc_close (file);
1958 if (blocklen != 0)
1959 return 0;
1961 return 1;
1964 /* --------------------------------------------------------------------------------------------- */
1966 * Fill in the edit structure. Return NULL on failure. Pass edit as
1967 * NULL to allocate a new structure.
1969 * If line is 0, try to restore saved position. Otherwise put the
1970 * cursor on that line and show it in the middle of the screen.
1973 WEdit *
1974 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
1976 int to_free = 0;
1977 option_auto_syntax = 1; /* Resetting to auto on every invokation */
1978 if (option_line_state)
1980 option_line_state_width = LINE_STATE_WIDTH;
1982 else
1984 option_line_state_width = 0;
1986 if (!edit)
1988 #ifdef ENABLE_NLS
1990 * Expand option_whole_chars_search by national letters using
1991 * current locale
1994 static char option_whole_chars_search_buf[256];
1996 if (option_whole_chars_search_buf != option_whole_chars_search)
1998 size_t i;
1999 size_t len = str_term_width1 (option_whole_chars_search);
2001 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2003 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2005 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2007 option_whole_chars_search_buf[len++] = i;
2011 option_whole_chars_search_buf[len] = 0;
2012 option_whole_chars_search = option_whole_chars_search_buf;
2014 #endif /* ENABLE_NLS */
2015 edit = g_malloc0 (sizeof (WEdit));
2016 edit->search = NULL;
2017 to_free = 1;
2019 edit_purge_widget (edit);
2020 edit->num_widget_lines = lines;
2021 edit->over_col = 0;
2022 edit->num_widget_columns = columns;
2023 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2024 edit->stat1.st_uid = getuid ();
2025 edit->stat1.st_gid = getgid ();
2026 edit->stat1.st_mtime = 0;
2027 edit->bracket = -1;
2028 edit->force |= REDRAW_PAGE;
2029 edit_set_filename (edit, filename);
2030 edit->stack_size = START_STACK_SIZE;
2031 edit->stack_size_mask = START_STACK_SIZE - 1;
2032 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
2034 edit->utf8 = 0;
2035 edit->converter = str_cnv_from_term;
2036 edit_set_codeset (edit);
2038 if (edit_load_file (edit))
2040 /* edit_load_file already gives an error message */
2041 if (to_free)
2042 g_free (edit);
2043 return 0;
2046 edit->loading_done = 1;
2047 edit->modified = 0;
2048 edit->locked = 0;
2049 edit_load_syntax (edit, NULL, NULL);
2051 int color;
2052 edit_get_syntax_color (edit, -1, &color);
2055 /* load saved cursor position */
2056 if ((line == 0) && option_save_position)
2058 edit_load_position (edit);
2060 else
2062 if (line <= 0)
2063 line = 1;
2064 edit_move_display (edit, line - 1);
2065 edit_move_to_line (edit, line - 1);
2068 edit_set_keymap ();
2070 return edit;
2073 /* --------------------------------------------------------------------------------------------- */
2074 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2077 edit_clean (WEdit * edit)
2079 int j = 0;
2081 if (!edit)
2082 return 0;
2084 /* a stale lock, remove it */
2085 if (edit->locked)
2086 edit->locked = edit_unlock_file (edit);
2088 /* save cursor position */
2089 if (option_save_position)
2090 edit_save_position (edit);
2091 else if (edit->serialized_bookmarks != NULL)
2092 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2094 /* File specified on the mcedit command line and never saved */
2095 if (edit->delete_file)
2096 unlink (edit->filename);
2098 edit_free_syntax_rules (edit);
2099 book_mark_flush (edit, -1);
2100 for (; j <= MAXBUFF; j++)
2102 g_free (edit->buffers1[j]);
2103 g_free (edit->buffers2[j]);
2106 g_free (edit->undo_stack);
2107 g_free (edit->filename);
2108 g_free (edit->dir);
2110 mc_search_free (edit->search);
2111 edit->search = NULL;
2113 if (edit->converter != str_cnv_from_term)
2114 str_close_conv (edit->converter);
2116 edit_purge_widget (edit);
2118 return 1;
2121 /* --------------------------------------------------------------------------------------------- */
2122 /** returns 1 on success */
2125 edit_renew (WEdit * edit)
2127 int lines = edit->num_widget_lines;
2128 int columns = edit->num_widget_columns;
2130 edit_clean (edit);
2131 return (edit_init (edit, lines, columns, "", 0) != NULL);
2134 /* --------------------------------------------------------------------------------------------- */
2136 * Load a new file into the editor. If it fails, preserve the old file.
2137 * To do it, allocate a new widget, initialize it and, if the new file
2138 * was loaded, copy the data to the old widget.
2139 * Return 1 on success, 0 on failure.
2143 edit_reload (WEdit * edit, const char *filename)
2145 WEdit *e;
2146 int lines = edit->num_widget_lines;
2147 int columns = edit->num_widget_columns;
2149 e = g_malloc0 (sizeof (WEdit));
2150 e->widget = edit->widget;
2151 if (!edit_init (e, lines, columns, filename, 0))
2153 g_free (e);
2154 return 0;
2156 edit_clean (edit);
2157 memcpy (edit, e, sizeof (WEdit));
2158 g_free (e);
2159 return 1;
2162 /* --------------------------------------------------------------------------------------------- */
2164 * Load a new file into the editor and set line. If it fails, preserve the old file.
2165 * To do it, allocate a new widget, initialize it and, if the new file
2166 * was loaded, copy the data to the old widget.
2167 * Return 1 on success, 0 on failure.
2171 edit_reload_line (WEdit * edit, const char *filename, long line)
2173 WEdit *e;
2174 int lines = edit->num_widget_lines;
2175 int columns = edit->num_widget_columns;
2177 e = g_malloc0 (sizeof (WEdit));
2178 e->widget = edit->widget;
2179 if (!edit_init (e, lines, columns, filename, line))
2181 g_free (e);
2182 return 0;
2184 edit_clean (edit);
2185 memcpy (edit, e, sizeof (WEdit));
2186 g_free (e);
2187 return 1;
2190 /* --------------------------------------------------------------------------------------------- */
2192 void
2193 edit_set_codeset (WEdit * edit)
2195 #ifdef HAVE_CHARSET
2196 const char *cp_id;
2198 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
2200 if (cp_id != NULL)
2202 GIConv conv;
2203 conv = str_crt_conv_from (cp_id);
2204 if (conv != INVALID_CONV)
2206 if (edit->converter != str_cnv_from_term)
2207 str_close_conv (edit->converter);
2208 edit->converter = conv;
2212 if (cp_id != NULL)
2213 edit->utf8 = str_isutf8 (cp_id);
2214 #else
2215 (void) edit;
2216 #endif
2220 /* --------------------------------------------------------------------------------------------- */
2222 Recording stack for undo:
2223 The following is an implementation of a compressed stack. Identical
2224 pushes are recorded by a negative prefix indicating the number of times the
2225 same char was pushed. This saves space for repeated curs-left or curs-right
2226 delete etc.
2230 pushed: stored:
2234 b -3
2236 c --> -4
2242 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2243 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2244 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2245 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2246 position.
2248 The only way the cursor moves or the buffer is changed is through the routines:
2249 insert, backspace, insert_ahead, delete, and cursor_move.
2250 These record the reverse undo movements onto the stack each time they are
2251 called.
2253 Each key press results in a set of actions (insert; delete ...). So each time
2254 a key is pressed the current position of start_display is pushed as
2255 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2256 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2257 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2261 void
2262 edit_push_action (WEdit * edit, long c, ...)
2264 unsigned long sp = edit->stack_pointer;
2265 unsigned long spm1;
2266 long *t;
2268 /* first enlarge the stack if necessary */
2269 if (sp > edit->stack_size - 10)
2270 { /* say */
2271 if (option_max_undo < 256)
2272 option_max_undo = 256;
2273 if (edit->stack_size < (unsigned long) option_max_undo)
2275 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
2276 if (t)
2278 edit->undo_stack = t;
2279 edit->stack_size <<= 1;
2280 edit->stack_size_mask = edit->stack_size - 1;
2284 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
2285 if (edit->stack_disable)
2286 return;
2288 #ifdef FAST_MOVE_CURSOR
2289 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
2291 va_list ap;
2292 edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
2293 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
2294 va_start (ap, c);
2295 c = -(va_arg (ap, int));
2296 va_end (ap);
2298 else
2299 #endif /* ! FAST_MOVE_CURSOR */
2300 if (edit->stack_bottom != sp
2301 && spm1 != edit->stack_bottom
2302 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
2304 int d;
2305 if (edit->undo_stack[spm1] < 0)
2307 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
2308 if (d == c)
2310 if (edit->undo_stack[spm1] > -1000000000)
2312 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2313 edit->undo_stack[spm1]--;
2314 return;
2317 /* #define NO_STACK_CURSMOVE_ANIHILATION */
2318 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2319 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
2320 { /* a left then a right anihilate each other */
2321 if (edit->undo_stack[spm1] == -2)
2322 edit->stack_pointer = spm1;
2323 else
2324 edit->undo_stack[spm1]++;
2325 return;
2327 #endif
2329 else
2331 d = edit->undo_stack[spm1];
2332 if (d == c)
2334 if (c >= KEY_PRESS)
2335 return; /* --> no need to push multiple do-nothings */
2336 edit->undo_stack[sp] = -2;
2337 goto check_bottom;
2339 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2340 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
2341 { /* a left then a right anihilate each other */
2342 edit->stack_pointer = spm1;
2343 return;
2345 #endif
2348 edit->undo_stack[sp] = c;
2350 check_bottom:
2351 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
2353 /* if the sp wraps round and catches the stack_bottom then erase
2354 * the first set of actions on the stack to make space - by moving
2355 * stack_bottom forward one "key press" */
2356 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
2357 if ((unsigned long) c == edit->stack_bottom ||
2358 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
2361 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
2363 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
2364 && edit->stack_bottom != edit->stack_pointer);
2366 /*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: */
2367 if (edit->stack_pointer != edit->stack_bottom
2368 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
2369 edit->stack_bottom = edit->stack_pointer = 0;
2372 /* --------------------------------------------------------------------------------------------- */
2374 Basic low level single character buffer alterations and movements at the cursor.
2375 Returns char passed over, inserted or removed.
2378 void
2379 edit_insert (WEdit * edit, int c)
2381 /* check if file has grown to large */
2382 if (edit->last_byte >= SIZE_LIMIT)
2383 return;
2385 /* first we must update the position of the display window */
2386 if (edit->curs1 < edit->start_display)
2388 edit->start_display++;
2389 if (c == '\n')
2390 edit->start_line++;
2393 /* Mark file as modified, unless the file hasn't been fully loaded */
2394 if (edit->loading_done)
2396 edit_modification (edit);
2399 /* now we must update some info on the file and check if a redraw is required */
2400 if (c == '\n')
2402 if (edit->book_mark)
2403 book_mark_inc (edit, edit->curs_line);
2404 edit->curs_line++;
2405 edit->total_lines++;
2406 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2409 /* save the reverse command onto the undo stack */
2410 edit_push_action (edit, BACKSPACE);
2412 /* update markers */
2413 edit->mark1 += (edit->mark1 > edit->curs1);
2414 edit->mark2 += (edit->mark2 > edit->curs1);
2415 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2417 /* add a new buffer if we've reached the end of the last one */
2418 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2419 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2421 /* perform the insertion */
2422 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2423 = (unsigned char) c;
2425 /* update file length */
2426 edit->last_byte++;
2428 /* update cursor position */
2429 edit->curs1++;
2432 /* --------------------------------------------------------------------------------------------- */
2433 /** same as edit_insert and move left */
2435 void
2436 edit_insert_ahead (WEdit * edit, int c)
2438 if (edit->last_byte >= SIZE_LIMIT)
2439 return;
2441 if (edit->curs1 < edit->start_display)
2443 edit->start_display++;
2444 if (c == '\n')
2445 edit->start_line++;
2447 edit_modification (edit);
2448 if (c == '\n')
2450 if (edit->book_mark)
2451 book_mark_inc (edit, edit->curs_line);
2452 edit->total_lines++;
2453 edit->force |= REDRAW_AFTER_CURSOR;
2455 edit_push_action (edit, DELCHAR);
2457 edit->mark1 += (edit->mark1 >= edit->curs1);
2458 edit->mark2 += (edit->mark2 >= edit->curs1);
2459 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2461 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2462 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2463 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2464 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2466 edit->last_byte++;
2467 edit->curs2++;
2471 /* --------------------------------------------------------------------------------------------- */
2474 edit_delete (WEdit * edit, const int byte_delete)
2476 int p = 0;
2477 int cw = 1;
2478 int i;
2480 if (!edit->curs2)
2481 return 0;
2483 cw = 1;
2484 /* if byte_delete = 1 then delete only one byte not multibyte char */
2485 if (edit->utf8 && byte_delete == 0)
2487 edit_get_utf (edit, edit->curs1, &cw);
2488 if (cw < 1)
2489 cw = 1;
2491 for (i = 1; i <= cw; i++)
2493 if (edit->mark1 > edit->curs1)
2495 edit->mark1--;
2496 edit->end_mark_curs--;
2498 if (edit->mark2 > edit->curs1)
2499 edit->mark2--;
2500 if (edit->last_get_rule > edit->curs1)
2501 edit->last_get_rule--;
2503 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2504 ((edit->curs2 -
2505 1) & M_EDIT_BUF_SIZE) - 1];
2507 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2509 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2510 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2512 edit->last_byte--;
2513 edit->curs2--;
2514 edit_push_action (edit, p + 256);
2517 edit_modification (edit);
2518 if (p == '\n')
2520 if (edit->book_mark)
2521 book_mark_dec (edit, edit->curs_line);
2522 edit->total_lines--;
2523 edit->force |= REDRAW_AFTER_CURSOR;
2525 if (edit->curs1 < edit->start_display)
2527 edit->start_display--;
2528 if (p == '\n')
2529 edit->start_line--;
2532 return p;
2535 /* --------------------------------------------------------------------------------------------- */
2537 #ifdef FAST_MOVE_CURSOR
2539 edit_move_backward_lots (WEdit * edit, long increment)
2541 int r, s, t;
2542 unsigned char *p = NULL;
2544 if (increment > edit->curs1)
2545 increment = edit->curs1;
2546 if (increment <= 0)
2547 return -1;
2548 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
2550 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
2551 if (r > increment)
2552 r = increment;
2553 s = edit->curs1 & M_EDIT_BUF_SIZE;
2555 if (s > r)
2557 memqcpy (edit,
2558 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
2559 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
2561 else
2563 if (s != 0)
2565 memqcpy (edit,
2566 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
2567 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
2568 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
2569 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
2571 memqcpy (edit,
2572 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
2573 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
2574 EDIT_BUF_SIZE - (r - s), r - s);
2576 increment -= r;
2577 edit->curs1 -= r;
2578 edit->curs2 += r;
2579 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2581 if (p)
2582 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
2583 else
2584 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2586 else
2588 g_free (p);
2591 s = edit->curs1 & M_EDIT_BUF_SIZE;
2592 while (increment)
2594 p = 0;
2595 r = EDIT_BUF_SIZE;
2596 if (r > increment)
2597 r = increment;
2598 t = s;
2599 if (r < t)
2600 t = r;
2601 memqcpy (edit,
2602 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
2603 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
2604 if (r >= s)
2606 if (t)
2608 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
2609 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
2611 memqcpy (edit,
2612 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
2613 EDIT_BUF_SIZE - r,
2614 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
2615 EDIT_BUF_SIZE - (r - s), r - s);
2617 increment -= r;
2618 edit->curs1 -= r;
2619 edit->curs2 += r;
2620 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2622 if (p)
2623 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
2624 else
2625 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2627 else
2628 g_free (p);
2630 return edit_get_byte (edit, edit->curs1);
2632 #endif /* ! FAST_MOVE_CURSOR */
2634 /* --------------------------------------------------------------------------------------------- */
2635 /** moves the cursor right or left: increment positive or negative respectively */
2637 void
2638 edit_cursor_move (WEdit * edit, long increment)
2640 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2641 int c;
2642 #ifdef FAST_MOVE_CURSOR
2643 if (increment < -256)
2645 edit->force |= REDRAW_PAGE;
2646 edit_move_backward_lots (edit, -increment);
2647 return;
2649 #endif /* ! FAST_MOVE_CURSOR */
2651 if (increment < 0)
2653 for (; increment < 0; increment++)
2655 if (!edit->curs1)
2656 return;
2658 edit_push_action (edit, CURS_RIGHT);
2660 c = edit_get_byte (edit, edit->curs1 - 1);
2661 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2662 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2663 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2664 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2665 edit->curs2++;
2666 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2667 1) & M_EDIT_BUF_SIZE];
2668 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2670 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2671 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2673 edit->curs1--;
2674 if (c == '\n')
2676 edit->curs_line--;
2677 edit->force |= REDRAW_LINE_BELOW;
2682 else if (increment > 0)
2684 for (; increment > 0; increment--)
2686 if (!edit->curs2)
2687 return;
2689 edit_push_action (edit, CURS_LEFT);
2691 c = edit_get_byte (edit, edit->curs1);
2692 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2693 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2694 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2695 edit->curs1++;
2696 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2697 ((edit->curs2 -
2698 1) & M_EDIT_BUF_SIZE) - 1];
2699 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2701 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2702 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2704 edit->curs2--;
2705 if (c == '\n')
2707 edit->curs_line++;
2708 edit->force |= REDRAW_LINE_ABOVE;
2714 /* These functions return positions relative to lines */
2716 /* --------------------------------------------------------------------------------------------- */
2717 /** returns index of last char on line + 1 */
2719 long
2720 edit_eol (WEdit * edit, long current)
2722 if (current >= edit->last_byte)
2723 return edit->last_byte;
2725 for (;; current++)
2726 if (edit_get_byte (edit, current) == '\n')
2727 break;
2728 return current;
2731 /* --------------------------------------------------------------------------------------------- */
2732 /** returns index of first char on line */
2734 long
2735 edit_bol (WEdit * edit, long current)
2737 if (current <= 0)
2738 return 0;
2740 for (;; current--)
2741 if (edit_get_byte (edit, current - 1) == '\n')
2742 break;
2743 return current;
2746 /* --------------------------------------------------------------------------------------------- */
2748 long
2749 edit_count_lines (WEdit * edit, long current, long upto)
2751 long lines = 0;
2752 if (upto > edit->last_byte)
2753 upto = edit->last_byte;
2754 if (current < 0)
2755 current = 0;
2756 while (current < upto)
2757 if (edit_get_byte (edit, current++) == '\n')
2758 lines++;
2759 return lines;
2762 /* --------------------------------------------------------------------------------------------- */
2763 /* If lines is zero this returns the count of lines from current to upto. */
2764 /* If upto is zero returns index of lines forward current. */
2766 long
2767 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2769 if (upto)
2771 return edit_count_lines (edit, current, upto);
2773 else
2775 long next;
2776 if (lines < 0)
2777 lines = 0;
2778 while (lines--)
2780 next = edit_eol (edit, current) + 1;
2781 if (next > edit->last_byte)
2782 break;
2783 else
2784 current = next;
2786 return current;
2790 /* --------------------------------------------------------------------------------------------- */
2791 /** Returns offset of 'lines' lines up from current */
2793 long
2794 edit_move_backward (WEdit * edit, long current, long lines)
2796 if (lines < 0)
2797 lines = 0;
2798 current = edit_bol (edit, current);
2799 while ((lines--) && current != 0)
2800 current = edit_bol (edit, current - 1);
2801 return current;
2804 /* --------------------------------------------------------------------------------------------- */
2805 /* If cols is zero this returns the count of columns from current to upto. */
2806 /* If upto is zero returns index of cols across from current. */
2808 long
2809 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
2811 long p, q;
2812 int col;
2814 if (upto)
2816 q = upto;
2817 cols = -10;
2819 else
2820 q = edit->last_byte + 2;
2822 for (col = 0, p = current; p < q; p++)
2824 int c, orig_c;
2825 int utf_ch = 0;
2826 #ifdef HAVE_CHARSET
2827 int cw = 1;
2828 #endif
2829 if (cols != -10)
2831 if (col == cols)
2832 return p;
2833 if (col > cols)
2834 return p - 1;
2836 orig_c = c = edit_get_byte (edit, p);
2837 #ifdef HAVE_CHARSET
2838 if (edit->utf8)
2840 utf_ch = edit_get_utf (edit, p, &cw);
2841 if (utf8_display)
2843 if (cw > 1)
2844 col -= cw - 1;
2845 if (g_unichar_iswide (utf_ch))
2846 col++;
2848 else if (cw > 1 && g_unichar_isprint (utf_ch))
2849 col -= cw - 1;
2851 #endif
2852 c = convert_to_display_c (c);
2853 if (c == '\t')
2854 col += TAB_SIZE - col % TAB_SIZE;
2855 else if (c == '\n')
2857 if (upto)
2858 return col;
2859 else
2860 return p;
2862 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
2863 /* '\r' is shown as ^M, so we must advance 2 characters */
2864 /* Caret notation for control characters */
2865 col += 2;
2866 else
2867 col++;
2869 return col;
2872 /* --------------------------------------------------------------------------------------------- */
2873 /** returns the current column position of the cursor */
2876 edit_get_col (WEdit * edit)
2878 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
2881 /* --------------------------------------------------------------------------------------------- */
2882 /* Scrolling functions */
2883 /* --------------------------------------------------------------------------------------------- */
2885 void
2886 edit_update_curs_row (WEdit * edit)
2888 edit->curs_row = edit->curs_line - edit->start_line;
2891 /* --------------------------------------------------------------------------------------------- */
2893 void
2894 edit_update_curs_col (WEdit * edit)
2896 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
2899 /* --------------------------------------------------------------------------------------------- */
2902 edit_get_curs_col (const WEdit * edit)
2904 return edit->curs_col;
2907 /* --------------------------------------------------------------------------------------------- */
2908 /** moves the display start position up by i lines */
2910 void
2911 edit_scroll_upward (WEdit * edit, unsigned long i)
2913 unsigned long lines_above = edit->start_line;
2914 if (i > lines_above)
2915 i = lines_above;
2916 if (i)
2918 edit->start_line -= i;
2919 edit->start_display = edit_move_backward (edit, edit->start_display, i);
2920 edit->force |= REDRAW_PAGE;
2921 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2923 edit_update_curs_row (edit);
2927 /* --------------------------------------------------------------------------------------------- */
2928 /** returns 1 if could scroll, 0 otherwise */
2930 void
2931 edit_scroll_downward (WEdit * edit, int i)
2933 int lines_below;
2934 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
2935 if (lines_below > 0)
2937 if (i > lines_below)
2938 i = lines_below;
2939 edit->start_line += i;
2940 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
2941 edit->force |= REDRAW_PAGE;
2942 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2944 edit_update_curs_row (edit);
2947 /* --------------------------------------------------------------------------------------------- */
2949 void
2950 edit_scroll_right (WEdit * edit, int i)
2952 edit->force |= REDRAW_PAGE;
2953 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2954 edit->start_col -= i;
2957 /* --------------------------------------------------------------------------------------------- */
2959 void
2960 edit_scroll_left (WEdit * edit, int i)
2962 if (edit->start_col)
2964 edit->start_col += i;
2965 if (edit->start_col > 0)
2966 edit->start_col = 0;
2967 edit->force |= REDRAW_PAGE;
2968 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2972 /* --------------------------------------------------------------------------------------------- */
2973 /* high level cursor movement commands */
2974 /* --------------------------------------------------------------------------------------------- */
2976 void
2977 edit_move_to_prev_col (WEdit * edit, long p)
2979 int prev = edit->prev_col;
2980 int over = edit->over_col;
2981 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
2983 if (option_cursor_beyond_eol)
2985 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
2986 edit_eol (edit, edit->curs1));
2988 if (line_len < prev + edit->over_col)
2990 edit->over_col = prev + over - line_len;
2991 edit->prev_col = line_len;
2992 edit->curs_col = line_len;
2994 else
2996 edit->curs_col = prev + over;
2997 edit->prev_col = edit->curs_col;
2998 edit->over_col = 0;
3001 else
3003 edit->over_col = 0;
3004 if (is_in_indent (edit) && option_fake_half_tabs)
3006 edit_update_curs_col (edit);
3007 if (space_width)
3008 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3010 int q = edit->curs_col;
3011 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3012 p = edit_bol (edit, edit->curs1);
3013 edit_cursor_move (edit,
3014 edit_move_forward3 (edit, p, edit->curs_col,
3015 0) - edit->curs1);
3016 if (!left_of_four_spaces (edit))
3017 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3023 /* --------------------------------------------------------------------------------------------- */
3026 line_is_blank (WEdit * edit, long line)
3028 return is_blank (edit, edit_find_line (edit, line));
3031 /* --------------------------------------------------------------------------------------------- */
3032 /** move cursor to line 'line' */
3034 void
3035 edit_move_to_line (WEdit * e, long line)
3037 if (line < e->curs_line)
3038 edit_move_up (e, e->curs_line - line, 0);
3039 else
3040 edit_move_down (e, line - e->curs_line, 0);
3041 edit_scroll_screen_over_cursor (e);
3044 /* --------------------------------------------------------------------------------------------- */
3045 /** scroll window so that first visible line is 'line' */
3047 void
3048 edit_move_display (WEdit * e, long line)
3050 if (line < e->start_line)
3051 edit_scroll_upward (e, e->start_line - line);
3052 else
3053 edit_scroll_downward (e, line - e->start_line);
3056 /* --------------------------------------------------------------------------------------------- */
3057 /** save markers onto undo stack */
3059 void
3060 edit_push_markers (WEdit * edit)
3062 edit_push_action (edit, MARK_1 + edit->mark1);
3063 edit_push_action (edit, MARK_2 + edit->mark2);
3066 /* --------------------------------------------------------------------------------------------- */
3068 void
3069 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3071 edit->mark1 = m1;
3072 edit->mark2 = m2;
3073 edit->column1 = c1;
3074 edit->column2 = c2;
3078 /* --------------------------------------------------------------------------------------------- */
3079 /** highlight marker toggle */
3081 void
3082 edit_mark_cmd (WEdit * edit, int unmark)
3084 edit_push_markers (edit);
3085 if (unmark)
3087 edit_set_markers (edit, 0, 0, 0, 0);
3088 edit->force |= REDRAW_PAGE;
3090 else
3092 if (edit->mark2 >= 0)
3094 edit->end_mark_curs = -1;
3095 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3096 edit->curs_col + edit->over_col);
3097 edit->force |= REDRAW_PAGE;
3099 else
3101 edit->end_mark_curs = edit->curs1;
3102 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3103 edit->curs_col + edit->over_col);
3108 /* --------------------------------------------------------------------------------------------- */
3110 void
3111 edit_delete_line (WEdit * edit)
3114 * Delete right part of the line.
3115 * Note that edit_get_byte() returns '\n' when byte position is
3116 * beyond EOF.
3118 while (edit_get_byte (edit, edit->curs1) != '\n')
3120 (void) edit_delete (edit, 1);
3124 * Delete '\n' char.
3125 * Note that edit_delete() will not corrupt anything if called while
3126 * cursor position is EOF.
3128 (void) edit_delete (edit, 1);
3131 * Delete left part of the line.
3132 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3134 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3136 (void) edit_backspace (edit, 1);
3140 /* --------------------------------------------------------------------------------------------- */
3142 void
3143 insert_spaces_tab (WEdit * edit, int half)
3145 int i;
3146 edit_update_curs_col (edit);
3147 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
3148 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
3149 while (i > 0)
3151 edit_insert (edit, ' ');
3152 i -= space_width;
3156 /* --------------------------------------------------------------------------------------------- */
3159 edit_indent_width (WEdit * edit, long p)
3161 long q = p;
3162 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3163 q++;
3164 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3167 /* --------------------------------------------------------------------------------------------- */
3169 void
3170 edit_insert_indent (WEdit * edit, int indent)
3172 if (!option_fill_tabs_with_spaces)
3174 while (indent >= TAB_SIZE)
3176 edit_insert (edit, '\t');
3177 indent -= TAB_SIZE;
3180 while (indent-- > 0)
3181 edit_insert (edit, ' ');
3184 /* --------------------------------------------------------------------------------------------- */
3186 void
3187 edit_push_key_press (WEdit * edit)
3189 edit_push_action (edit, KEY_PRESS + edit->start_display);
3190 if (edit->mark2 == -1)
3191 edit_push_action (edit, MARK_1 + edit->mark1);
3194 /* --------------------------------------------------------------------------------------------- */
3196 void
3197 edit_find_bracket (WEdit * edit)
3199 edit->bracket = edit_get_bracket (edit, 1, 10000);
3200 if (last_bracket != edit->bracket)
3201 edit->force |= REDRAW_PAGE;
3202 last_bracket = edit->bracket;
3205 /* --------------------------------------------------------------------------------------------- */
3207 * This executes a command as though the user initiated it through a key
3208 * press. Callback with WIDGET_KEY as a message calls this after
3209 * translating the key press. This function can be used to pass any
3210 * command to the editor. Note that the screen wouldn't update
3211 * automatically. Either of command or char_for_insertion must be
3212 * passed as -1. Commands are executed, and char_for_insertion is
3213 * inserted at the cursor.
3216 void
3217 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3219 if (command == CK_Begin_Record_Macro)
3221 edit->macro_i = 0;
3222 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3223 return;
3225 if (command == CK_End_Record_Macro && edit->macro_i != -1)
3227 edit->force |= REDRAW_COMPLETELY;
3228 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
3229 edit->macro_i = -1;
3230 return;
3232 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
3234 edit->macro[edit->macro_i].command = command;
3235 edit->macro[edit->macro_i++].ch = char_for_insertion;
3237 /* record the beginning of a set of editing actions initiated by a key press */
3238 if (command != CK_Undo && command != CK_Ext_Mode)
3239 edit_push_key_press (edit);
3241 edit_execute_cmd (edit, command, char_for_insertion);
3242 if (edit->column_highlight)
3243 edit->force |= REDRAW_PAGE;
3246 /* --------------------------------------------------------------------------------------------- */
3248 This executes a command at a lower level than macro recording.
3249 It also does not push a key_press onto the undo stack. This means
3250 that if it is called many times, a single undo command will undo
3251 all of them. It also does not check for the Undo command.
3253 void
3254 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3256 edit->force |= REDRAW_LINE;
3258 /* The next key press will unhighlight the found string, so update
3259 * the whole page */
3260 if (edit->found_len || edit->column_highlight)
3261 edit->force |= REDRAW_PAGE;
3263 if (command / 100 == 6)
3264 { /* a highlight command like shift-arrow */
3265 edit->column_highlight = 0;
3266 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3268 edit_mark_cmd (edit, 1); /* clear */
3269 edit_mark_cmd (edit, 0); /* marking on */
3271 edit->highlight = 1;
3273 else
3274 { /* any other command */
3275 if (edit->highlight)
3276 edit_mark_cmd (edit, 0); /* clear */
3277 edit->highlight = 0;
3280 /* first check for undo */
3281 if (command == CK_Undo)
3283 edit_do_undo (edit);
3284 edit->found_len = 0;
3285 edit->prev_col = edit_get_col (edit);
3286 edit->search_start = edit->curs1;
3287 return;
3290 /* An ordinary key press */
3291 if (char_for_insertion >= 0)
3293 /* if non persistent selection and text selected */
3294 if (!option_persistent_selections)
3296 if (edit->mark1 != edit->mark2)
3297 edit_block_delete_cmd (edit);
3299 if (edit->overwrite)
3301 /* remove char only one time, after input first byte, multibyte chars */
3302 if ((!utf8_display || edit->charpoint == 0)
3303 && edit_get_byte (edit, edit->curs1) != '\n')
3304 edit_delete (edit, 0);
3306 if (option_cursor_beyond_eol && edit->over_col > 0)
3307 edit_insert_over (edit);
3308 #ifdef HAVE_CHARSET
3309 if (char_for_insertion > 255 && utf8_display == 0)
3311 unsigned char str[6 + 1];
3312 size_t i = 0;
3313 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3314 if (res == 0)
3316 str[0] = '.';
3317 str[1] = '\0';
3319 else
3321 str[res] = '\0';
3323 while (str[i] != 0 && i <= 6)
3325 char_for_insertion = str[i];
3326 edit_insert (edit, char_for_insertion);
3327 i++;
3330 else
3331 #endif
3332 edit_insert (edit, char_for_insertion);
3334 if (option_auto_para_formatting)
3336 format_paragraph (edit, 0);
3337 edit->force |= REDRAW_PAGE;
3339 else
3340 check_and_wrap_line (edit);
3341 edit->found_len = 0;
3342 edit->prev_col = edit_get_col (edit);
3343 edit->search_start = edit->curs1;
3344 edit_find_bracket (edit);
3345 return;
3348 switch (command)
3350 case CK_Begin_Page:
3351 case CK_End_Page:
3352 case CK_Begin_Page_Highlight:
3353 case CK_End_Page_Highlight:
3354 case CK_Word_Left:
3355 case CK_Word_Right:
3356 case CK_Up:
3357 case CK_Down:
3358 case CK_Left:
3359 case CK_Right:
3360 if (edit->mark2 >= 0)
3362 if (!option_persistent_selections)
3364 if (edit->column_highlight)
3365 edit_push_action (edit, COLUMN_ON);
3366 edit->column_highlight = 0;
3367 edit_mark_cmd (edit, 1);
3372 switch (command)
3374 case CK_Begin_Page:
3375 case CK_End_Page:
3376 case CK_Begin_Page_Highlight:
3377 case CK_End_Page_Highlight:
3378 case CK_Word_Left:
3379 case CK_Word_Right:
3380 case CK_Up:
3381 case CK_Down:
3382 case CK_Word_Left_Highlight:
3383 case CK_Word_Right_Highlight:
3384 case CK_Up_Highlight:
3385 case CK_Down_Highlight:
3386 case CK_Up_Alt_Highlight:
3387 case CK_Down_Alt_Highlight:
3388 if (edit->mark2 == -1)
3389 break; /*marking is following the cursor: may need to highlight a whole line */
3390 case CK_Left:
3391 case CK_Right:
3392 case CK_Left_Highlight:
3393 case CK_Right_Highlight:
3394 edit->force |= REDRAW_CHAR_ONLY;
3397 /* basic cursor key commands */
3398 switch (command)
3400 case CK_BackSpace:
3401 /* if non persistent selection and text selected */
3402 if (!option_persistent_selections)
3404 if (edit->mark1 != edit->mark2)
3406 edit_block_delete_cmd (edit);
3407 break;
3410 if (option_cursor_beyond_eol && edit->over_col > 0)
3412 edit->over_col--;
3413 break;
3415 if (option_backspace_through_tabs && is_in_indent (edit))
3417 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3418 edit_backspace (edit, 1);
3419 break;
3421 else
3423 if (option_fake_half_tabs)
3425 int i;
3426 if (is_in_indent (edit) && right_of_four_spaces (edit))
3428 for (i = 0; i < HALF_TAB_SIZE; i++)
3429 edit_backspace (edit, 1);
3430 break;
3434 edit_backspace (edit, 0);
3435 break;
3436 case CK_Delete:
3437 /* if non persistent selection and text selected */
3438 if (!option_persistent_selections)
3440 if (edit->mark1 != edit->mark2)
3442 edit_block_delete_cmd (edit);
3443 break;
3447 if (option_cursor_beyond_eol && edit->over_col > 0)
3448 edit_insert_over (edit);
3450 if (option_fake_half_tabs)
3452 int i;
3453 if (is_in_indent (edit) && left_of_four_spaces (edit))
3455 for (i = 1; i <= HALF_TAB_SIZE; i++)
3456 edit_delete (edit, 1);
3457 break;
3460 edit_delete (edit, 0);
3461 break;
3462 case CK_Delete_Word_Left:
3463 edit->over_col = 0;
3464 edit_left_delete_word (edit);
3465 break;
3466 case CK_Delete_Word_Right:
3467 if (option_cursor_beyond_eol && edit->over_col > 0)
3468 edit_insert_over (edit);
3470 edit_right_delete_word (edit);
3471 break;
3472 case CK_Delete_Line:
3473 edit_delete_line (edit);
3474 break;
3475 case CK_Delete_To_Line_End:
3476 edit_delete_to_line_end (edit);
3477 break;
3478 case CK_Delete_To_Line_Begin:
3479 edit_delete_to_line_begin (edit);
3480 break;
3481 case CK_Enter:
3482 edit->over_col = 0;
3483 if (option_auto_para_formatting)
3485 edit_double_newline (edit);
3486 if (option_return_does_auto_indent)
3487 edit_auto_indent (edit);
3488 format_paragraph (edit, 0);
3490 else
3492 edit_insert (edit, '\n');
3493 if (option_return_does_auto_indent)
3495 edit_auto_indent (edit);
3498 break;
3499 case CK_Return:
3500 edit_insert (edit, '\n');
3501 break;
3503 case CK_Page_Up_Alt_Highlight:
3504 edit->column_highlight = 1;
3505 case CK_Page_Up:
3506 case CK_Page_Up_Highlight:
3507 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3508 break;
3509 case CK_Page_Down_Alt_Highlight:
3510 edit->column_highlight = 1;
3511 case CK_Page_Down:
3512 case CK_Page_Down_Highlight:
3513 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3514 break;
3515 case CK_Left_Alt_Highlight:
3516 edit->column_highlight = 1;
3517 case CK_Left:
3518 case CK_Left_Highlight:
3519 if (option_fake_half_tabs)
3521 if (is_in_indent (edit) && right_of_four_spaces (edit))
3523 if (option_cursor_beyond_eol && edit->over_col > 0)
3524 edit->over_col--;
3525 else
3526 edit_cursor_move (edit, -HALF_TAB_SIZE);
3527 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3528 break;
3531 edit_left_char_move_cmd (edit);
3532 break;
3533 case CK_Right_Alt_Highlight:
3534 edit->column_highlight = 1;
3535 case CK_Right:
3536 case CK_Right_Highlight:
3537 if (option_fake_half_tabs)
3539 if (is_in_indent (edit) && left_of_four_spaces (edit))
3541 edit_cursor_move (edit, HALF_TAB_SIZE);
3542 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3543 break;
3546 edit_right_char_move_cmd (edit);
3547 break;
3548 case CK_Begin_Page:
3549 case CK_Begin_Page_Highlight:
3550 edit_begin_page (edit);
3551 break;
3552 case CK_End_Page:
3553 case CK_End_Page_Highlight:
3554 edit_end_page (edit);
3555 break;
3556 case CK_Word_Left:
3557 case CK_Word_Left_Highlight:
3558 edit->over_col = 0;
3559 edit_left_word_move_cmd (edit);
3560 break;
3561 case CK_Word_Right:
3562 case CK_Word_Right_Highlight:
3563 edit->over_col = 0;
3564 edit_right_word_move_cmd (edit);
3565 break;
3566 case CK_Up_Alt_Highlight:
3567 edit->column_highlight = 1;
3568 case CK_Up:
3569 case CK_Up_Highlight:
3570 edit_move_up (edit, 1, 0);
3571 break;
3572 case CK_Down_Alt_Highlight:
3573 edit->column_highlight = 1;
3574 case CK_Down:
3575 case CK_Down_Highlight:
3576 edit_move_down (edit, 1, 0);
3577 break;
3578 case CK_Paragraph_Up_Alt_Highlight:
3579 edit->column_highlight = 1;
3580 case CK_Paragraph_Up:
3581 case CK_Paragraph_Up_Highlight:
3582 edit_move_up_paragraph (edit, 0);
3583 break;
3584 case CK_Paragraph_Down_Alt_Highlight:
3585 edit->column_highlight = 1;
3586 case CK_Paragraph_Down:
3587 case CK_Paragraph_Down_Highlight:
3588 edit_move_down_paragraph (edit, 0);
3589 break;
3590 case CK_Scroll_Up_Alt_Highlight:
3591 edit->column_highlight = 1;
3592 case CK_Scroll_Up:
3593 case CK_Scroll_Up_Highlight:
3594 edit_move_up (edit, 1, 1);
3595 break;
3596 case CK_Scroll_Down_Alt_Highlight:
3597 edit->column_highlight = 1;
3598 case CK_Scroll_Down:
3599 case CK_Scroll_Down_Highlight:
3600 edit_move_down (edit, 1, 1);
3601 break;
3602 case CK_Home:
3603 case CK_Home_Highlight:
3604 edit_cursor_to_bol (edit);
3605 break;
3606 case CK_End:
3607 case CK_End_Highlight:
3608 edit_cursor_to_eol (edit);
3609 break;
3610 case CK_Tab:
3611 /* if text marked shift block */
3612 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3614 if (edit->mark2 < 0)
3615 edit_mark_cmd (edit, 0);
3616 edit_move_block_to_right (edit);
3618 else
3620 if (option_cursor_beyond_eol)
3621 edit_insert_over (edit);
3622 edit_tab_cmd (edit);
3623 if (option_auto_para_formatting)
3625 format_paragraph (edit, 0);
3626 edit->force |= REDRAW_PAGE;
3628 else
3630 check_and_wrap_line (edit);
3633 break;
3635 case CK_Toggle_Insert:
3636 edit->overwrite = (edit->overwrite == 0);
3637 break;
3639 case CK_Mark:
3640 if (edit->mark2 >= 0)
3642 if (edit->column_highlight)
3643 edit_push_action (edit, COLUMN_ON);
3644 edit->column_highlight = 0;
3646 edit_mark_cmd (edit, 0);
3647 break;
3648 case CK_Column_Mark:
3649 if (!edit->column_highlight)
3650 edit_push_action (edit, COLUMN_OFF);
3651 edit->column_highlight = 1;
3652 edit_mark_cmd (edit, 0);
3653 break;
3654 case CK_Mark_All:
3655 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3656 edit->force |= REDRAW_PAGE;
3657 break;
3658 case CK_Unmark:
3659 if (edit->column_highlight)
3660 edit_push_action (edit, COLUMN_ON);
3661 edit->column_highlight = 0;
3662 edit_mark_cmd (edit, 1);
3663 break;
3665 case CK_Toggle_Line_State:
3666 option_line_state = !option_line_state;
3667 if (option_line_state)
3669 option_line_state_width = LINE_STATE_WIDTH;
3671 else
3673 option_line_state_width = 0;
3675 edit->force |= REDRAW_PAGE;
3676 break;
3678 case CK_Toggle_Show_Margin:
3679 show_right_margin = !show_right_margin;
3680 edit->force |= REDRAW_PAGE;
3681 break;
3683 case CK_Toggle_Bookmark:
3684 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3685 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3686 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3687 else
3688 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3689 break;
3690 case CK_Flush_Bookmarks:
3691 book_mark_flush (edit, BOOK_MARK_COLOR);
3692 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3693 edit->force |= REDRAW_PAGE;
3694 break;
3695 case CK_Next_Bookmark:
3696 if (edit->book_mark)
3698 struct _book_mark *p;
3699 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3700 if (p->next)
3702 p = p->next;
3703 if (p->line >= edit->start_line + edit->num_widget_lines
3704 || p->line < edit->start_line)
3705 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3706 edit_move_to_line (edit, p->line);
3709 break;
3710 case CK_Prev_Bookmark:
3711 if (edit->book_mark)
3713 struct _book_mark *p;
3714 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3715 while (p->line == edit->curs_line)
3716 if (p->prev)
3717 p = p->prev;
3718 if (p->line >= 0)
3720 if (p->line >= edit->start_line + edit->num_widget_lines
3721 || p->line < edit->start_line)
3722 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3723 edit_move_to_line (edit, p->line);
3726 break;
3728 case CK_Beginning_Of_Text:
3729 case CK_Beginning_Of_Text_Highlight:
3730 edit_move_to_top (edit);
3731 break;
3732 case CK_End_Of_Text:
3733 case CK_End_Of_Text_Highlight:
3734 edit_move_to_bottom (edit);
3735 break;
3737 case CK_Copy:
3738 if (option_cursor_beyond_eol && edit->over_col > 0)
3739 edit_insert_over (edit);
3740 edit_block_copy_cmd (edit);
3741 break;
3742 case CK_Remove:
3743 edit_block_delete_cmd (edit);
3744 break;
3745 case CK_Move:
3746 if (option_cursor_beyond_eol && edit->over_col > 0)
3747 edit_insert_over (edit);
3748 edit_block_move_cmd (edit);
3749 break;
3751 case CK_Shift_Block_Left:
3752 if (edit->mark1 != edit->mark2)
3753 edit_move_block_to_left (edit);
3754 break;
3755 case CK_Shift_Block_Right:
3756 if (edit->mark1 != edit->mark2)
3757 edit_move_block_to_right (edit);
3758 break;
3759 case CK_XStore:
3760 edit_copy_to_X_buf_cmd (edit);
3761 break;
3762 case CK_XCut:
3763 edit_cut_to_X_buf_cmd (edit);
3764 break;
3765 case CK_XPaste:
3766 /* if non persistent selection and text selected */
3767 if (!option_persistent_selections)
3769 if (edit->mark1 != edit->mark2)
3770 edit_block_delete_cmd (edit);
3772 if (option_cursor_beyond_eol && edit->over_col > 0)
3773 edit_insert_over (edit);
3774 edit_paste_from_X_buf_cmd (edit);
3775 break;
3776 case CK_Selection_History:
3777 edit_paste_from_history (edit);
3778 break;
3780 case CK_Save_As:
3781 edit_save_as_cmd (edit);
3782 break;
3783 case CK_Save:
3784 edit_save_confirm_cmd (edit);
3785 break;
3786 case CK_Load:
3787 edit_load_cmd (edit, EDIT_FILE_COMMON);
3788 break;
3789 case CK_Save_Block:
3790 edit_save_block_cmd (edit);
3791 break;
3792 case CK_Insert_File:
3793 edit_insert_file_cmd (edit);
3794 break;
3796 case CK_Load_Prev_File:
3797 edit_load_back_cmd (edit);
3798 break;
3799 case CK_Load_Next_File:
3800 edit_load_forward_cmd (edit);
3801 break;
3803 case CK_Load_Syntax_File:
3804 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3805 break;
3806 case CK_Choose_Syntax:
3807 edit_syntax_dialog (edit);
3808 break;
3810 case CK_Load_Menu_File:
3811 edit_load_cmd (edit, EDIT_FILE_MENU);
3812 break;
3814 case CK_Toggle_Syntax:
3815 option_syntax_highlighting ^= 1;
3816 if (option_syntax_highlighting == 1)
3817 edit_load_syntax (edit, NULL, edit->syntax_type);
3818 edit->force |= REDRAW_PAGE;
3819 break;
3821 case CK_Toggle_Tab_TWS:
3822 enable_show_tabs_tws ^= 1;
3823 edit->force |= REDRAW_PAGE;
3824 break;
3826 case CK_Find:
3827 edit_search_cmd (edit, FALSE);
3828 break;
3829 case CK_Find_Again:
3830 edit_search_cmd (edit, TRUE);
3831 break;
3832 case CK_Replace:
3833 edit_replace_cmd (edit, 0);
3834 break;
3835 case CK_Replace_Again:
3836 edit_replace_cmd (edit, 1);
3837 break;
3838 case CK_Complete_Word:
3839 /* if text marked shift block */
3840 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3842 edit_move_block_to_left (edit);
3844 else
3846 edit_complete_word_cmd (edit);
3848 break;
3849 case CK_Find_Definition:
3850 edit_get_match_keyword_cmd (edit);
3851 break;
3852 case CK_Quit:
3853 dlg_stop (edit->widget.owner);
3854 break;
3855 case CK_New:
3856 edit_new_cmd (edit);
3857 break;
3858 case CK_Help:
3859 edit_help_cmd (edit);
3860 break;
3861 case CK_Refresh:
3862 edit_refresh_cmd (edit);
3863 break;
3864 case CK_SaveSetupCmd:
3865 save_setup_cmd ();
3866 break;
3867 case CK_About:
3868 edit_about ();
3869 break;
3870 case CK_LearnKeys:
3871 learn_keys ();
3872 break;
3873 case CK_Edit_Options:
3874 edit_options_dialog (edit);
3875 break;
3876 case CK_Edit_Save_Mode:
3877 menu_save_mode_cmd ();
3878 break;
3879 case CK_Date:
3881 char s[BUF_MEDIUM];
3882 /* fool gcc to prevent a Y2K warning */
3883 char time_format[] = "_c";
3884 time_format[0] = '%';
3886 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3887 edit_print_string (edit, s);
3888 edit->force |= REDRAW_PAGE;
3889 break;
3891 break;
3892 case CK_Goto:
3893 edit_goto_cmd (edit);
3894 break;
3895 case CK_Paragraph_Format:
3896 format_paragraph (edit, 1);
3897 edit->force |= REDRAW_PAGE;
3898 break;
3899 case CK_Delete_Macro:
3900 edit_delete_macro_cmd (edit);
3901 break;
3902 case CK_Match_Bracket:
3903 edit_goto_matching_bracket (edit);
3904 break;
3905 case CK_User_Menu:
3906 user_menu (edit);
3907 break;
3908 case CK_Sort:
3909 edit_sort_cmd (edit);
3910 break;
3911 case CK_ExtCmd:
3912 edit_ext_cmd (edit);
3913 break;
3914 case CK_Mail:
3915 edit_mail_dialog (edit);
3916 break;
3917 case CK_Shell:
3918 view_other_cmd ();
3919 break;
3920 case CK_SelectCodepage:
3921 edit_select_codepage_cmd (edit);
3922 break;
3923 case CK_Insert_Literal:
3924 edit_insert_literal_cmd (edit);
3925 break;
3926 case CK_Execute_Macro:
3927 edit_execute_macro_cmd (edit);
3928 break;
3929 case CK_Begin_End_Macro:
3930 edit_begin_end_macro_cmd (edit);
3931 break;
3932 case CK_Ext_Mode:
3933 edit->extmod = 1;
3934 break;
3935 default:
3936 break;
3939 /* CK_Pipe_Block */
3940 if ((command / 1000) == 1) /* a shell command */
3941 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3942 if (command > CK_Macro (0) && command <= CK_Last_Macro)
3943 { /* a macro command */
3944 struct macro m[MAX_MACRO_LENGTH];
3945 int nm;
3946 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3947 edit_execute_macro (edit, m, nm);
3950 /* keys which must set the col position, and the search vars */
3951 switch (command)
3953 case CK_Find:
3954 case CK_Find_Again:
3955 case CK_Replace:
3956 case CK_Replace_Again:
3957 case CK_Complete_Word:
3958 edit->prev_col = edit_get_col (edit);
3959 break;
3960 case CK_Up:
3961 case CK_Up_Highlight:
3962 case CK_Up_Alt_Highlight:
3963 case CK_Down:
3964 case CK_Down_Highlight:
3965 case CK_Down_Alt_Highlight:
3966 case CK_Page_Up:
3967 case CK_Page_Up_Highlight:
3968 case CK_Page_Up_Alt_Highlight:
3969 case CK_Page_Down:
3970 case CK_Page_Down_Highlight:
3971 case CK_Page_Down_Alt_Highlight:
3972 case CK_Beginning_Of_Text:
3973 case CK_Beginning_Of_Text_Highlight:
3974 case CK_End_Of_Text:
3975 case CK_End_Of_Text_Highlight:
3976 case CK_Paragraph_Up:
3977 case CK_Paragraph_Up_Highlight:
3978 case CK_Paragraph_Up_Alt_Highlight:
3979 case CK_Paragraph_Down:
3980 case CK_Paragraph_Down_Highlight:
3981 case CK_Paragraph_Down_Alt_Highlight:
3982 case CK_Scroll_Up:
3983 case CK_Scroll_Up_Highlight:
3984 case CK_Scroll_Up_Alt_Highlight:
3985 case CK_Scroll_Down:
3986 case CK_Scroll_Down_Highlight:
3987 case CK_Scroll_Down_Alt_Highlight:
3988 edit->search_start = edit->curs1;
3989 edit->found_len = 0;
3990 break;
3991 default:
3992 edit->found_len = 0;
3993 edit->prev_col = edit_get_col (edit);
3994 edit->search_start = edit->curs1;
3996 edit_find_bracket (edit);
3998 if (option_auto_para_formatting)
4000 switch (command)
4002 case CK_BackSpace:
4003 case CK_Delete:
4004 case CK_Delete_Word_Left:
4005 case CK_Delete_Word_Right:
4006 case CK_Delete_To_Line_End:
4007 case CK_Delete_To_Line_Begin:
4008 format_paragraph (edit, 0);
4009 edit->force |= REDRAW_PAGE;
4014 /* --------------------------------------------------------------------------------------------- */
4016 void
4017 edit_stack_init (void)
4019 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4021 edit_history_moveto[edit_stack_iterator].filename = NULL;
4022 edit_history_moveto[edit_stack_iterator].line = -1;
4025 edit_stack_iterator = 0;
4028 /* --------------------------------------------------------------------------------------------- */
4030 void
4031 edit_stack_free (void)
4033 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4034 g_free (edit_history_moveto[edit_stack_iterator].filename);
4037 /* --------------------------------------------------------------------------------------------- */
4038 /** move i lines */
4040 void
4041 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4043 edit_move_updown (edit, i, do_scroll, TRUE);
4046 /* --------------------------------------------------------------------------------------------- */
4047 /** move i lines */
4049 void
4050 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4052 edit_move_updown (edit, i, do_scroll, FALSE);
4055 /* --------------------------------------------------------------------------------------------- */
4057 unsigned int
4058 edit_unlock_file (WEdit * edit)
4060 char *fullpath;
4061 unsigned int ret;
4063 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4064 ret = unlock_file (fullpath);
4065 g_free (fullpath);
4067 return ret;
4070 /* --------------------------------------------------------------------------------------------- */
4072 unsigned int
4073 edit_lock_file (WEdit * edit)
4075 char *fullpath;
4076 unsigned int ret;
4078 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4079 ret = lock_file (fullpath);
4080 g_free (fullpath);
4082 return ret;
4085 /* --------------------------------------------------------------------------------------------- */