added a new action "Record and Repeat commands", added menu entry (Record/Repeat...
[midnight-commander.git] / src / editor / edit.c
blob84b1e3813a6b548826b7fd505434e83f3453bc4e
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 Authors: Paul Sheer 1996, 1997
7 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
25 /** \file
26 * \brief Source: editor low level data handling and cursor fundamentals
27 * \author Paul Sheer
28 * \date 1996, 1997
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
41 #include <fcntl.h>
43 #include "lib/global.h"
45 #include "lib/tty/color.h"
46 #include "lib/tty/tty.h" /* attrset() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
49 #include "lib/vfs/mc-vfs/vfs.h"
50 #include "lib/strutil.h" /* utf string functions */
51 #include "lib/util.h" /* load_file_position(), save_file_position() */
52 #include "lib/timefmt.h" /* time formatting */
53 #include "lib/lock.h"
54 #include "lib/widget.h"
56 #ifdef HAVE_CHARSET
57 #include "lib/charsets.h" /* get_codepage_id */
58 #endif
60 #include "src/filemanager/cmd.h" /* view_other_cmd() */
61 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
63 #include "src/main.h" /* source_codepage */
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/learn.h" /* learn_keys */
66 #include "src/keybind-defaults.h"
68 #include "edit-impl.h"
69 #include "edit-widget.h"
71 /*** global variables ****************************************************************************/
73 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
74 int option_typewriter_wrap = 0;
75 int option_auto_para_formatting = 0;
76 int option_fill_tabs_with_spaces = 0;
77 int option_return_does_auto_indent = 1;
78 int option_backspace_through_tabs = 0;
79 int option_fake_half_tabs = 1;
80 int option_save_mode = EDIT_QUICK_SAVE;
81 int option_save_position = 1;
82 int option_max_undo = 32768;
83 int option_persistent_selections = 1;
84 int option_cursor_beyond_eol = 0;
85 int option_line_state = 0;
86 int option_line_state_width = 0;
88 int option_edit_right_extreme = 0;
89 int option_edit_left_extreme = 0;
90 int option_edit_top_extreme = 0;
91 int option_edit_bottom_extreme = 0;
92 int enable_show_tabs_tws = 1;
93 int option_check_nl_at_eof = 0;
94 int option_group_undo = 0;
95 int show_right_margin = 0;
97 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
98 char *option_backup_ext = NULL;
100 int edit_stack_iterator = 0;
101 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
102 /* magic sequense for say than block is vertical */
103 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
105 /*** file scope macro definitions ****************************************************************/
107 #define TEMP_BUF_LEN 1024
109 #define space_width 1
111 /*** file scope type declarations ****************************************************************/
113 /*** file scope variables ************************************************************************/
115 /* detecting an error on save is easy: just check if every byte has been written. */
116 /* detecting an error on read, is not so easy 'cos there is not way to tell
117 whether you read everything or not. */
118 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
119 static const struct edit_filters
121 const char *read, *write, *extension;
122 } all_filters[] =
124 /* *INDENT-OFF* */
125 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
126 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
127 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
129 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
130 /* *INDENT-ON* */
133 static long last_bracket = -1;
135 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
137 /*** file scope functions ************************************************************************/
138 /* --------------------------------------------------------------------------------------------- */
142 * here's a quick sketch of the layout: (don't run this through indent.)
144 * (b1 is buffers1 and b2 is buffers2)
147 * \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
148 * ______________________________________|______________________________________
150 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
151 * |-> |-> |-> |-> |-> |-> |
153 * _<------------------------->|<----------------->_
154 * WEdit->curs2 | WEdit->curs1
155 * ^ | ^
156 * | ^|^ |
157 * cursor ||| cursor
158 * |||
159 * file end|||file beginning
164 * This_is_some_file
165 * fin.
168 /* --------------------------------------------------------------------------------------------- */
170 static void user_menu (WEdit * edit);
171 static int left_of_four_spaces (WEdit * edit);
173 /* --------------------------------------------------------------------------------------------- */
175 static void
176 edit_about (void)
178 const char *header = N_("About");
179 const char *button_name = N_("&OK");
180 const char *const version = "MCEdit " VERSION;
181 char text[BUF_LARGE];
183 int win_len, version_len, button_len;
184 int cols, lines;
186 Dlg_head *about_dlg;
188 #ifdef ENABLE_NLS
189 header = _(header);
190 button_name = _(button_name);
191 #endif
193 button_len = str_term_width1 (button_name) + 5;
194 version_len = str_term_width1 (version);
196 g_snprintf (text, sizeof (text),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len = str_term_width1 (header);
202 win_len = max (win_len, version_len);
203 win_len = max (win_len, button_len);
205 /* count width and height of text */
206 str_msg_term_size (text, &lines, &cols);
207 lines += 9;
208 cols = max (win_len, cols) + 6;
210 /* dialog */
211 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
212 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
214 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
215 add_widget (about_dlg, label_new (5, 3, text));
216 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
217 B_ENTER, NORMAL_BUTTON, button_name, NULL));
219 run_dlg (about_dlg);
220 destroy_dlg (about_dlg);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
228 static void
229 edit_init_buffers (WEdit * edit)
231 int j;
233 for (j = 0; j <= MAXBUFF; j++)
235 edit->buffers1[j] = NULL;
236 edit->buffers2[j] = NULL;
239 edit->curs1 = 0;
240 edit->curs2 = 0;
241 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
250 static int
251 edit_load_file_fast (WEdit * edit, const char *filename)
253 long buf, buf2;
254 int file = -1;
255 int ret = 1;
257 edit->curs2 = edit->last_byte;
258 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
260 file = mc_open (filename, O_RDONLY | O_BINARY);
261 if (file == -1)
263 gchar *errmsg;
265 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
266 edit_error_dialog (_("Error"), errmsg);
267 g_free (errmsg);
268 return 1;
271 if (!edit->buffers2[buf2])
272 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
276 if (mc_read (file,
277 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
278 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
279 break;
281 for (buf = buf2 - 1; buf >= 0; buf--)
283 /* edit->buffers2[0] is already allocated */
284 if (!edit->buffers2[buf])
285 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
286 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
287 break;
289 ret = 0;
291 while (0);
292 if (ret)
294 char *err_str = g_strdup_printf (_("Error reading %s"), filename);
295 edit_error_dialog (_("Error"), err_str);
296 g_free (err_str);
298 mc_close (file);
299 return ret;
302 /* --------------------------------------------------------------------------------------------- */
303 /** Return index of the filter or -1 is there is no appropriate filter */
305 static int
306 edit_find_filter (const char *filename)
308 size_t i, l, e;
310 if (filename == NULL)
311 return -1;
313 l = strlen (filename);
314 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
316 e = strlen (all_filters[i].extension);
317 if (l > e)
318 if (!strcmp (all_filters[i].extension, filename + l - e))
319 return i;
321 return -1;
324 /* --------------------------------------------------------------------------------------------- */
326 static char *
327 edit_get_filter (const char *filename)
329 int i;
330 char *p, *quoted_name;
332 i = edit_find_filter (filename);
333 if (i < 0)
334 return NULL;
336 quoted_name = name_quote (filename, 0);
337 p = g_strdup_printf (all_filters[i].read, quoted_name);
338 g_free (quoted_name);
339 return p;
342 /* --------------------------------------------------------------------------------------------- */
344 static long
345 edit_insert_stream (WEdit * edit, FILE * f)
347 int c;
348 long i = 0;
349 while ((c = fgetc (f)) >= 0)
351 edit_insert (edit, c);
352 i++;
354 return i;
357 /* --------------------------------------------------------------------------------------------- */
358 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
360 static int
361 check_file_access (WEdit * edit, const char *filename, struct stat *st)
363 int file;
364 gchar *errmsg = NULL;
366 /* Try opening an existing file */
367 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
369 if (file < 0)
372 * Try creating the file. O_EXCL prevents following broken links
373 * and opening existing files.
375 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
376 if (file < 0)
378 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
379 goto cleanup;
381 else
383 /* New file, delete it if it's not modified or saved */
384 edit->delete_file = 1;
388 /* Check what we have opened */
389 if (mc_fstat (file, st) < 0)
391 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
392 goto cleanup;
395 /* We want to open regular files only */
396 if (!S_ISREG (st->st_mode))
398 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
399 goto cleanup;
403 * Don't delete non-empty files.
404 * O_EXCL should prevent it, but let's be on the safe side.
406 if (st->st_size > 0)
407 edit->delete_file = 0;
409 if (st->st_size >= SIZE_LIMIT)
410 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
412 cleanup:
413 (void) mc_close (file);
415 if (errmsg != NULL)
417 edit_error_dialog (_("Error"), errmsg);
418 g_free (errmsg);
419 return 1;
421 return 0;
424 /* --------------------------------------------------------------------------------------------- */
426 * Open the file and load it into the buffers, either directly or using
427 * a filter. Return 0 on success, 1 on error.
429 * Fast loading (edit_load_file_fast) is used when the file size is
430 * known. In this case the data is read into the buffers by blocks.
431 * If the file size is not known, the data is loaded byte by byte in
432 * edit_insert_file.
435 static int
436 edit_load_file (WEdit * edit)
438 int fast_load = 1;
440 /* Cannot do fast load if a filter is used */
441 if (edit_find_filter (edit->filename) >= 0)
442 fast_load = 0;
445 * VFS may report file size incorrectly, and slow load is not a big
446 * deal considering overhead in VFS.
448 if (!vfs_file_is_local (edit->filename))
449 fast_load = 0;
452 * FIXME: line end translation should disable fast loading as well
453 * Consider doing fseek() to the end and ftell() for the real size.
456 if (*edit->filename)
458 /* If we are dealing with a real file, check that it exists */
459 if (check_file_access (edit, edit->filename, &edit->stat1))
460 return 1;
462 else
464 /* nothing to load */
465 fast_load = 0;
468 edit_init_buffers (edit);
470 if (fast_load)
472 edit->last_byte = edit->stat1.st_size;
473 edit_load_file_fast (edit, edit->filename);
474 /* If fast load was used, the number of lines wasn't calculated */
475 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
477 else
479 edit->last_byte = 0;
480 if (*edit->filename)
482 edit->undo_stack_disable = 1;
483 if (edit_insert_file (edit, edit->filename) == 0)
485 edit_clean (edit);
486 return 1;
488 edit->undo_stack_disable = 0;
491 edit->lb = LB_ASIS;
492 return 0;
495 /* --------------------------------------------------------------------------------------------- */
496 /** Restore saved cursor position in the file */
498 static void
499 edit_load_position (WEdit * edit)
501 char *filename;
502 long line, column;
503 off_t offset;
505 if (!edit->filename || !*edit->filename)
506 return;
508 filename = vfs_canon (edit->filename);
509 load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
510 g_free (filename);
512 if (line > 0)
514 edit_move_to_line (edit, line - 1);
515 edit->prev_col = column;
517 else if (offset > 0)
519 edit_cursor_move (edit, offset);
520 line = edit->curs_line;
521 edit->search_start = edit->curs1;
524 book_mark_restore (edit, BOOK_MARK_COLOR);
526 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
527 edit_move_display (edit, line - (edit->num_widget_lines / 2));
530 /* --------------------------------------------------------------------------------------------- */
531 /** Save cursor position in the file */
533 static void
534 edit_save_position (WEdit * edit)
536 char *filename;
538 if (edit->filename == NULL || *edit->filename == '\0')
539 return;
541 filename = vfs_canon (edit->filename);
543 book_mark_serialize (edit, BOOK_MARK_COLOR);
544 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1,
545 edit->serialized_bookmarks);
546 edit->serialized_bookmarks = NULL;
548 g_free (filename);
551 /* --------------------------------------------------------------------------------------------- */
552 /** Clean the WEdit stricture except the widget part */
554 static void
555 edit_purge_widget (WEdit * edit)
557 size_t len = sizeof (WEdit) - sizeof (Widget);
558 char *start = (char *) edit + sizeof (Widget);
559 memset (start, 0, len);
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 edit_pop_undo_action (WEdit * edit)
585 long c;
586 unsigned long sp = edit->undo_stack_pointer;
588 if (sp == edit->undo_stack_bottom)
589 return STACK_BOTTOM;
591 sp = (sp - 1) & edit->undo_stack_size_mask;
592 c = edit->undo_stack[sp];
593 if (c >= 0)
595 /* edit->undo_stack[sp] = '@'; */
596 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
597 return c;
600 if (sp == edit->undo_stack_bottom)
601 return STACK_BOTTOM;
603 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
604 if (edit->undo_stack[sp] == -2)
606 /* edit->undo_stack[sp] = '@'; */
607 edit->undo_stack_pointer = sp;
609 else
610 edit->undo_stack[sp]++;
612 return c;
615 static long
616 edit_pop_redo_action (WEdit * edit)
618 long c;
619 unsigned long sp = edit->redo_stack_pointer;
621 if (sp == edit->redo_stack_bottom)
622 return STACK_BOTTOM;
624 sp = (sp - 1) & edit->redo_stack_size_mask;
625 c = edit->redo_stack[sp];
626 if (c >= 0)
628 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
629 return c;
632 if (sp == edit->redo_stack_bottom)
633 return STACK_BOTTOM;
635 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
636 if (edit->redo_stack[sp] == -2)
637 edit->redo_stack_pointer = sp;
638 else
639 edit->redo_stack[sp]++;
641 return c;
644 static long
645 get_prev_undo_action (WEdit * edit)
647 long c;
648 unsigned long sp = edit->undo_stack_pointer;
650 if (sp == edit->undo_stack_bottom)
651 return STACK_BOTTOM;
653 sp = (sp - 1) & edit->undo_stack_size_mask;
654 c = edit->undo_stack[sp];
655 if (c >= 0)
656 return c;
658 if (sp == edit->undo_stack_bottom)
659 return STACK_BOTTOM;
661 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
662 return c;
665 /* --------------------------------------------------------------------------------------------- */
666 /** is called whenever a modification is made by one of the four routines below */
668 static void
669 edit_modification (WEdit * edit)
671 edit->caches_valid = 0;
672 edit->screen_modified = 1;
674 /* raise lock when file modified */
675 if (!edit->modified && !edit->delete_file)
676 edit->locked = edit_lock_file (edit);
677 edit->modified = 1;
680 /* --------------------------------------------------------------------------------------------- */
682 static void
683 edit_insert_over (WEdit * edit)
685 int i;
687 for (i = 0; i < edit->over_col; i++)
689 edit_insert (edit, ' ');
691 edit->over_col = 0;
694 /* --------------------------------------------------------------------------------------------- */
696 static int
697 edit_backspace (WEdit * edit, const int byte_delete)
699 int p = 0;
700 int cw = 1;
701 int i;
703 if (!edit->curs1)
704 return 0;
706 cw = 1;
708 if (edit->mark2 != edit->mark1)
709 edit_push_markers (edit);
711 if (edit->utf8 && byte_delete == 0)
713 edit_get_prev_utf (edit, edit->curs1, &cw);
714 if (cw < 1)
715 cw = 1;
717 for (i = 1; i <= cw; i++)
719 if (edit->mark1 >= edit->curs1)
721 edit->mark1--;
722 edit->end_mark_curs--;
724 if (edit->mark2 >= edit->curs1)
725 edit->mark2--;
726 if (edit->last_get_rule >= edit->curs1)
727 edit->last_get_rule--;
729 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
730 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
731 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
733 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
734 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
736 edit->last_byte--;
737 edit->curs1--;
738 edit_push_undo_action (edit, p);
740 edit_modification (edit);
741 if (p == '\n')
743 if (edit->book_mark)
744 book_mark_dec (edit, edit->curs_line);
745 edit->curs_line--;
746 edit->total_lines--;
747 edit->force |= REDRAW_AFTER_CURSOR;
750 if (edit->curs1 < edit->start_display)
752 edit->start_display--;
753 if (p == '\n')
754 edit->start_line--;
757 return p;
760 /* --------------------------------------------------------------------------------------------- */
761 /* high level cursor movement commands */
762 /* --------------------------------------------------------------------------------------------- */
764 static int
765 is_in_indent (WEdit * edit)
767 long p = edit_bol (edit, edit->curs1);
768 while (p < edit->curs1)
769 if (!strchr (" \t", edit_get_byte (edit, p++)))
770 return 0;
771 return 1;
774 /* --------------------------------------------------------------------------------------------- */
776 static int
777 is_blank (WEdit * edit, long offset)
779 long s, f;
780 int c;
781 s = edit_bol (edit, offset);
782 f = edit_eol (edit, offset) - 1;
783 while (s <= f)
785 c = edit_get_byte (edit, s++);
786 if (!isspace (c))
787 return 0;
789 return 1;
793 /* --------------------------------------------------------------------------------------------- */
794 /** returns the offset of line i */
796 static long
797 edit_find_line (WEdit * edit, int line)
799 int i, j = 0;
800 int m = 2000000000;
801 if (!edit->caches_valid)
803 for (i = 0; i < N_LINE_CACHES; i++)
804 edit->line_numbers[i] = edit->line_offsets[i] = 0;
805 /* three offsets that we *know* are line 0 at 0 and these two: */
806 edit->line_numbers[1] = edit->curs_line;
807 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
808 edit->line_numbers[2] = edit->total_lines;
809 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
810 edit->caches_valid = 1;
812 if (line >= edit->total_lines)
813 return edit->line_offsets[2];
814 if (line <= 0)
815 return 0;
816 /* find the closest known point */
817 for (i = 0; i < N_LINE_CACHES; i++)
819 int n;
820 n = abs (edit->line_numbers[i] - line);
821 if (n < m)
823 m = n;
824 j = i;
827 if (m == 0)
828 return edit->line_offsets[j]; /* know the offset exactly */
829 if (m == 1 && j >= 3)
830 i = j; /* one line different - caller might be looping, so stay in this cache */
831 else
832 i = 3 + (rand () % (N_LINE_CACHES - 3));
833 if (line > edit->line_numbers[j])
834 edit->line_offsets[i] =
835 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
836 else
837 edit->line_offsets[i] =
838 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
839 edit->line_numbers[i] = line;
840 return edit->line_offsets[i];
843 /* --------------------------------------------------------------------------------------------- */
844 /** moves up until a blank line is reached, or until just
845 before a non-blank line is reached */
847 static void
848 edit_move_up_paragraph (WEdit * edit, int do_scroll)
850 int i = 0;
851 if (edit->curs_line > 1)
853 if (line_is_blank (edit, edit->curs_line))
855 if (line_is_blank (edit, edit->curs_line - 1))
857 for (i = edit->curs_line - 1; i; i--)
858 if (!line_is_blank (edit, i))
860 i++;
861 break;
864 else
866 for (i = edit->curs_line - 1; i; i--)
867 if (line_is_blank (edit, i))
868 break;
871 else
873 for (i = edit->curs_line - 1; i; i--)
874 if (line_is_blank (edit, i))
875 break;
878 edit_move_up (edit, edit->curs_line - i, do_scroll);
881 /* --------------------------------------------------------------------------------------------- */
882 /** moves down until a blank line is reached, or until just
883 before a non-blank line is reached */
885 static void
886 edit_move_down_paragraph (WEdit * edit, int do_scroll)
888 int i;
889 if (edit->curs_line >= edit->total_lines - 1)
891 i = edit->total_lines;
893 else
895 if (line_is_blank (edit, edit->curs_line))
897 if (line_is_blank (edit, edit->curs_line + 1))
899 for (i = edit->curs_line + 1; i; i++)
900 if (!line_is_blank (edit, i) || i > edit->total_lines)
902 i--;
903 break;
906 else
908 for (i = edit->curs_line + 1; i; i++)
909 if (line_is_blank (edit, i) || i >= edit->total_lines)
910 break;
913 else
915 for (i = edit->curs_line + 1; i; i++)
916 if (line_is_blank (edit, i) || i >= edit->total_lines)
917 break;
920 edit_move_down (edit, i - edit->curs_line, do_scroll);
923 /* --------------------------------------------------------------------------------------------- */
925 static void
926 edit_begin_page (WEdit * edit)
928 edit_update_curs_row (edit);
929 edit_move_up (edit, edit->curs_row, 0);
932 /* --------------------------------------------------------------------------------------------- */
934 static void
935 edit_end_page (WEdit * edit)
937 edit_update_curs_row (edit);
938 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
942 /* --------------------------------------------------------------------------------------------- */
943 /** goto beginning of text */
945 static void
946 edit_move_to_top (WEdit * edit)
948 if (edit->curs_line)
950 edit_cursor_move (edit, -edit->curs1);
951 edit_move_to_prev_col (edit, 0);
952 edit->force |= REDRAW_PAGE;
953 edit->search_start = 0;
954 edit_update_curs_row (edit);
959 /* --------------------------------------------------------------------------------------------- */
960 /** goto end of text */
962 static void
963 edit_move_to_bottom (WEdit * edit)
965 if (edit->curs_line < edit->total_lines)
967 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
968 edit->start_display = edit->last_byte;
969 edit->start_line = edit->total_lines;
970 edit_scroll_upward (edit, edit->num_widget_lines - 1);
971 edit->force |= REDRAW_PAGE;
975 /* --------------------------------------------------------------------------------------------- */
976 /** goto beginning of line */
978 static void
979 edit_cursor_to_bol (WEdit * edit)
981 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
982 edit->search_start = edit->curs1;
983 edit->prev_col = edit_get_col (edit);
984 edit->over_col = 0;
987 /* --------------------------------------------------------------------------------------------- */
988 /** goto end of line */
990 static void
991 edit_cursor_to_eol (WEdit * edit)
993 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
994 edit->search_start = edit->curs1;
995 edit->prev_col = edit_get_col (edit);
996 edit->over_col = 0;
999 /* --------------------------------------------------------------------------------------------- */
1001 static unsigned long
1002 my_type_of (int c)
1004 int x, r = 0;
1005 const char *p, *q;
1006 const char option_chars_move_whole_word[] =
1007 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1009 if (!c)
1010 return 0;
1011 if (c == '!')
1013 if (*option_chars_move_whole_word == '!')
1014 return 2;
1015 return 0x80000000UL;
1017 if (g_ascii_isupper ((gchar) c))
1018 c = 'A';
1019 else if (g_ascii_islower ((gchar) c))
1020 c = 'a';
1021 else if (g_ascii_isalpha (c))
1022 c = 'a';
1023 else if (isdigit (c))
1024 c = '0';
1025 else if (isspace (c))
1026 c = ' ';
1027 q = strchr (option_chars_move_whole_word, c);
1028 if (!q)
1029 return 0xFFFFFFFFUL;
1032 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1033 if (*p == '!')
1034 x <<= 1;
1035 r |= x;
1037 while ((q = strchr (q + 1, c)));
1038 return r;
1041 /* --------------------------------------------------------------------------------------------- */
1043 static void
1044 edit_left_word_move (WEdit * edit, int s)
1046 for (;;)
1048 int c1, c2;
1049 if (edit->column_highlight
1050 && edit->mark1 != edit->mark2
1051 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1052 break;
1053 edit_cursor_move (edit, -1);
1054 if (!edit->curs1)
1055 break;
1056 c1 = edit_get_byte (edit, edit->curs1 - 1);
1057 c2 = edit_get_byte (edit, edit->curs1);
1058 if (c1 == '\n' || c2 == '\n')
1059 break;
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_left_word_move_cmd (WEdit * edit)
1075 edit_left_word_move (edit, 0);
1076 edit->force |= REDRAW_PAGE;
1079 /* --------------------------------------------------------------------------------------------- */
1081 static void
1082 edit_right_word_move (WEdit * edit, int s)
1084 for (;;)
1086 int c1, c2;
1087 if (edit->column_highlight
1088 && edit->mark1 != edit->mark2
1089 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1090 break;
1091 edit_cursor_move (edit, 1);
1092 if (edit->curs1 >= edit->last_byte)
1093 break;
1094 c1 = edit_get_byte (edit, edit->curs1 - 1);
1095 c2 = edit_get_byte (edit, edit->curs1);
1096 if (c1 == '\n' || c2 == '\n')
1097 break;
1098 if (!(my_type_of (c1) & my_type_of (c2)))
1099 break;
1100 if (isspace (c1) && !isspace (c2))
1101 break;
1102 if (s)
1103 if (!isspace (c1) && isspace (c2))
1104 break;
1108 /* --------------------------------------------------------------------------------------------- */
1110 static void
1111 edit_right_word_move_cmd (WEdit * edit)
1113 edit_right_word_move (edit, 0);
1114 edit->force |= REDRAW_PAGE;
1117 /* --------------------------------------------------------------------------------------------- */
1119 static void
1120 edit_right_char_move_cmd (WEdit * edit)
1122 int cw = 1;
1123 int c = 0;
1124 if (edit->utf8)
1126 c = edit_get_utf (edit, edit->curs1, &cw);
1127 if (cw < 1)
1128 cw = 1;
1130 else
1132 c = edit_get_byte (edit, edit->curs1);
1134 if (option_cursor_beyond_eol && c == '\n')
1136 edit->over_col++;
1138 else
1140 edit_cursor_move (edit, cw);
1144 /* --------------------------------------------------------------------------------------------- */
1146 static void
1147 edit_left_char_move_cmd (WEdit * edit)
1149 int cw = 1;
1150 if (edit->column_highlight
1151 && option_cursor_beyond_eol
1152 && edit->mark1 != edit->mark2
1153 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1154 return;
1155 if (edit->utf8)
1157 edit_get_prev_utf (edit, edit->curs1, &cw);
1158 if (cw < 1)
1159 cw = 1;
1161 if (option_cursor_beyond_eol && edit->over_col > 0)
1163 edit->over_col--;
1165 else
1167 edit_cursor_move (edit, -cw);
1171 /* --------------------------------------------------------------------------------------------- */
1172 /** Up or down cursor moving.
1173 direction = TRUE - move up
1174 = FALSE - move down
1177 static void
1178 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1180 unsigned long p;
1181 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1183 if (i > l)
1184 i = l;
1186 if (i == 0)
1187 return;
1189 if (i > 1)
1190 edit->force |= REDRAW_PAGE;
1191 if (do_scroll)
1193 if (direction)
1194 edit_scroll_upward (edit, i);
1195 else
1196 edit_scroll_downward (edit, i);
1198 p = edit_bol (edit, edit->curs1);
1200 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1202 edit_cursor_move (edit, p - edit->curs1);
1204 edit_move_to_prev_col (edit, p);
1206 /* search start of current multibyte char (like CJK) */
1207 if (edit->curs1 + 1 < edit->last_byte)
1209 edit_right_char_move_cmd (edit);
1210 edit_left_char_move_cmd (edit);
1213 edit->search_start = edit->curs1;
1214 edit->found_len = 0;
1217 /* --------------------------------------------------------------------------------------------- */
1219 static void
1220 edit_right_delete_word (WEdit * edit)
1222 int c1, c2;
1223 for (;;)
1225 if (edit->curs1 >= edit->last_byte)
1226 break;
1227 c1 = edit_delete (edit, 1);
1228 c2 = edit_get_byte (edit, edit->curs1);
1229 if (c1 == '\n' || c2 == '\n')
1230 break;
1231 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1232 break;
1233 if (!(my_type_of (c1) & my_type_of (c2)))
1234 break;
1238 /* --------------------------------------------------------------------------------------------- */
1240 static void
1241 edit_left_delete_word (WEdit * edit)
1243 int c1, c2;
1244 for (;;)
1246 if (edit->curs1 <= 0)
1247 break;
1248 c1 = edit_backspace (edit, 1);
1249 c2 = edit_get_byte (edit, edit->curs1 - 1);
1250 if (c1 == '\n' || c2 == '\n')
1251 break;
1252 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1253 break;
1254 if (!(my_type_of (c1) & my_type_of (c2)))
1255 break;
1259 /* --------------------------------------------------------------------------------------------- */
1261 the start column position is not recorded, and hence does not
1262 undo as it happed. But who would notice.
1265 static void
1266 edit_do_undo (WEdit * edit)
1268 long ac;
1269 long count = 0;
1271 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1272 edit->over_col = 0;
1273 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1275 switch ((int) ac)
1277 case STACK_BOTTOM:
1278 goto done_undo;
1279 case CURS_RIGHT:
1280 edit_cursor_move (edit, 1);
1281 break;
1282 case CURS_LEFT:
1283 edit_cursor_move (edit, -1);
1284 break;
1285 case BACKSPACE:
1286 case BACKSPACE_BR:
1287 edit_backspace (edit, 1);
1288 break;
1289 case DELCHAR:
1290 case DELCHAR_BR:
1291 edit_delete (edit, 1);
1292 break;
1293 case COLUMN_ON:
1294 edit->column_highlight = 1;
1295 break;
1296 case COLUMN_OFF:
1297 edit->column_highlight = 0;
1298 break;
1300 if (ac >= 256 && ac < 512)
1301 edit_insert_ahead (edit, ac - 256);
1302 if (ac >= 0 && ac < 256)
1303 edit_insert (edit, ac);
1305 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1307 edit->mark1 = ac - MARK_1;
1308 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1310 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1312 edit->mark2 = ac - MARK_2;
1313 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1315 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1317 edit->end_mark_curs = ac - MARK_CURS;
1319 if (count++)
1320 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1323 if (edit->start_display > ac - KEY_PRESS)
1325 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1326 edit->force |= REDRAW_PAGE;
1328 else if (edit->start_display < ac - KEY_PRESS)
1330 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1331 edit->force |= REDRAW_PAGE;
1333 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1334 edit_update_curs_row (edit);
1336 done_undo:;
1337 edit->undo_stack_disable = 0;
1340 static void
1341 edit_do_redo (WEdit * edit)
1343 long ac;
1344 long count = 0;
1346 if (edit->redo_stack_reset)
1347 return;
1349 edit->over_col = 0;
1350 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1352 switch ((int) ac)
1354 case STACK_BOTTOM:
1355 goto done_redo;
1356 case CURS_RIGHT:
1357 edit_cursor_move (edit, 1);
1358 break;
1359 case CURS_LEFT:
1360 edit_cursor_move (edit, -1);
1361 break;
1362 case BACKSPACE:
1363 edit_backspace (edit, 1);
1364 break;
1365 case DELCHAR:
1366 edit_delete (edit, 1);
1367 break;
1368 case COLUMN_ON:
1369 edit->column_highlight = 1;
1370 break;
1371 case COLUMN_OFF:
1372 edit->column_highlight = 0;
1373 break;
1375 if (ac >= 256 && ac < 512)
1376 edit_insert_ahead (edit, ac - 256);
1377 if (ac >= 0 && ac < 256)
1378 edit_insert (edit, ac);
1380 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1382 edit->mark1 = ac - MARK_1;
1383 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1385 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1387 edit->mark2 = ac - MARK_2;
1388 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1390 /* more than one pop usually means something big */
1391 if (count++)
1392 edit->force |= REDRAW_PAGE;
1395 if (edit->start_display > ac - KEY_PRESS)
1397 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1398 edit->force |= REDRAW_PAGE;
1400 else if (edit->start_display < ac - KEY_PRESS)
1402 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1403 edit->force |= REDRAW_PAGE;
1405 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1406 edit_update_curs_row (edit);
1408 done_redo:;
1411 static void
1412 edit_group_undo (WEdit * edit)
1414 long ac = KEY_PRESS;
1415 long cur_ac = KEY_PRESS;
1416 while (ac != STACK_BOTTOM && ac == cur_ac)
1418 cur_ac = get_prev_undo_action (edit);
1419 edit_do_undo (edit);
1420 ac = get_prev_undo_action (edit);
1421 /* exit from cycle if option_group_undo is not set,
1422 * and make single UNDO operation
1424 if (!option_group_undo)
1425 ac = STACK_BOTTOM;
1429 /* --------------------------------------------------------------------------------------------- */
1431 static void
1432 edit_delete_to_line_end (WEdit * edit)
1434 while (edit_get_byte (edit, edit->curs1) != '\n')
1436 if (!edit->curs2)
1437 break;
1438 edit_delete (edit, 1);
1442 /* --------------------------------------------------------------------------------------------- */
1444 static void
1445 edit_delete_to_line_begin (WEdit * edit)
1447 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1449 if (!edit->curs1)
1450 break;
1451 edit_backspace (edit, 1);
1455 /* --------------------------------------------------------------------------------------------- */
1457 static int
1458 is_aligned_on_a_tab (WEdit * edit)
1460 edit_update_curs_col (edit);
1461 return !((edit->curs_col % (TAB_SIZE * space_width))
1462 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1465 /* --------------------------------------------------------------------------------------------- */
1467 static int
1468 right_of_four_spaces (WEdit * edit)
1470 int i, ch = 0;
1471 for (i = 1; i <= HALF_TAB_SIZE; i++)
1472 ch |= edit_get_byte (edit, edit->curs1 - i);
1473 if (ch == ' ')
1474 return is_aligned_on_a_tab (edit);
1475 return 0;
1478 /* --------------------------------------------------------------------------------------------- */
1480 static int
1481 left_of_four_spaces (WEdit * edit)
1483 int i, ch = 0;
1484 for (i = 0; i < HALF_TAB_SIZE; i++)
1485 ch |= edit_get_byte (edit, edit->curs1 + i);
1486 if (ch == ' ')
1487 return is_aligned_on_a_tab (edit);
1488 return 0;
1491 /* --------------------------------------------------------------------------------------------- */
1493 static void
1494 edit_auto_indent (WEdit * edit)
1496 long p;
1497 char c;
1498 p = edit->curs1;
1499 /* use the previous line as a template */
1500 p = edit_move_backward (edit, p, 1);
1501 /* copy the leading whitespace of the line */
1502 for (;;)
1503 { /* no range check - the line _is_ \n-terminated */
1504 c = edit_get_byte (edit, p++);
1505 if (c != ' ' && c != '\t')
1506 break;
1507 edit_insert (edit, c);
1511 /* --------------------------------------------------------------------------------------------- */
1513 static inline void
1514 edit_double_newline (WEdit * edit)
1516 edit_insert (edit, '\n');
1517 if (edit_get_byte (edit, edit->curs1) == '\n')
1518 return;
1519 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1520 return;
1521 edit->force |= REDRAW_PAGE;
1522 edit_insert (edit, '\n');
1525 /* --------------------------------------------------------------------------------------------- */
1527 static inline void
1528 edit_tab_cmd (WEdit * edit)
1530 int i;
1532 if (option_fake_half_tabs)
1534 if (is_in_indent (edit))
1536 /*insert a half tab (usually four spaces) unless there is a
1537 half tab already behind, then delete it and insert a
1538 full tab. */
1539 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
1541 for (i = 1; i <= HALF_TAB_SIZE; i++)
1542 edit_backspace (edit, 1);
1543 edit_insert (edit, '\t');
1545 else
1547 insert_spaces_tab (edit, 1);
1549 return;
1552 if (option_fill_tabs_with_spaces)
1554 insert_spaces_tab (edit, 0);
1556 else
1558 edit_insert (edit, '\t');
1562 /* --------------------------------------------------------------------------------------------- */
1564 static void
1565 check_and_wrap_line (WEdit * edit)
1567 int curs, c;
1568 if (!option_typewriter_wrap)
1569 return;
1570 edit_update_curs_col (edit);
1571 if (edit->curs_col < option_word_wrap_line_length)
1572 return;
1573 curs = edit->curs1;
1574 for (;;)
1576 curs--;
1577 c = edit_get_byte (edit, curs);
1578 if (c == '\n' || curs <= 0)
1580 edit_insert (edit, '\n');
1581 return;
1583 if (c == ' ' || c == '\t')
1585 int current = edit->curs1;
1586 edit_cursor_move (edit, curs - edit->curs1 + 1);
1587 edit_insert (edit, '\n');
1588 edit_cursor_move (edit, current - edit->curs1 + 1);
1589 return;
1594 /* --------------------------------------------------------------------------------------------- */
1595 /** this find the matching bracket in either direction, and sets edit->bracket */
1597 static long
1598 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1600 const char *const b = "{}{[][()(", *p;
1601 int i = 1, a, inc = -1, c, d, n = 0;
1602 unsigned long j = 0;
1603 long q;
1604 edit_update_curs_row (edit);
1605 c = edit_get_byte (edit, edit->curs1);
1606 p = strchr (b, c);
1607 /* no limit */
1608 if (!furthest_bracket_search)
1609 furthest_bracket_search--;
1610 /* not on a bracket at all */
1611 if (!p)
1612 return -1;
1613 /* the matching bracket */
1614 d = p[1];
1615 /* going left or right? */
1616 if (strchr ("{[(", c))
1617 inc = 1;
1618 for (q = edit->curs1 + inc;; q += inc)
1620 /* out of buffer? */
1621 if (q >= edit->last_byte || q < 0)
1622 break;
1623 a = edit_get_byte (edit, q);
1624 /* don't want to eat CPU */
1625 if (j++ > furthest_bracket_search)
1626 break;
1627 /* out of screen? */
1628 if (in_screen)
1630 if (q < edit->start_display)
1631 break;
1632 /* count lines if searching downward */
1633 if (inc > 0 && a == '\n')
1634 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
1635 break;
1637 /* count bracket depth */
1638 i += (a == c) - (a == d);
1639 /* return if bracket depth is zero */
1640 if (!i)
1641 return q;
1643 /* no match */
1644 return -1;
1647 /* --------------------------------------------------------------------------------------------- */
1649 static inline void
1650 edit_goto_matching_bracket (WEdit * edit)
1652 long q;
1654 q = edit_get_bracket (edit, 0, 0);
1655 if (q >= 0)
1657 edit->bracket = edit->curs1;
1658 edit->force |= REDRAW_PAGE;
1659 edit_cursor_move (edit, q - edit->curs1);
1663 /* --------------------------------------------------------------------------------------------- */
1664 /** User edit menu, like user menu (F2) but only in editor. */
1666 static void
1667 user_menu (WEdit * edit)
1669 char *block_file;
1670 int nomark;
1671 long start_mark, end_mark;
1672 struct stat status;
1674 block_file = concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE);
1676 nomark = eval_marks (edit, &start_mark, &end_mark);
1677 if (nomark == 0)
1678 edit_save_block (edit, block_file, start_mark, end_mark);
1680 /* run shell scripts from menu */
1681 if (user_menu_cmd (edit) && (mc_stat (block_file, &status) == 0) && (status.st_size != 0))
1683 int rc = 0;
1684 FILE *fd;
1686 /* i.e. we have marked block */
1687 if (nomark == 0)
1688 rc = edit_block_delete_cmd (edit);
1690 if (rc == 0)
1692 long ins_len;
1694 ins_len = edit_insert_file (edit, block_file);
1695 if (nomark == 0 && ins_len > 0)
1696 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1698 /* truncate block file */
1699 fd = fopen (block_file, "w");
1700 if (fd != NULL)
1701 fclose (fd);
1703 edit_refresh_cmd (edit);
1704 edit->force |= REDRAW_COMPLETELY;
1706 g_free (block_file);
1709 /* --------------------------------------------------------------------------------------------- */
1710 /*** public functions ****************************************************************************/
1711 /* --------------------------------------------------------------------------------------------- */
1714 edit_get_byte (WEdit * edit, long byte_index)
1716 unsigned long p;
1717 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1718 return '\n';
1720 if (byte_index >= edit->curs1)
1722 p = edit->curs1 + edit->curs2 - byte_index - 1;
1723 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1725 else
1727 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1731 /* --------------------------------------------------------------------------------------------- */
1733 char *
1734 edit_get_byte_ptr (WEdit * edit, long byte_index)
1736 unsigned long p;
1737 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1738 return NULL;
1740 if (byte_index >= edit->curs1)
1742 p = edit->curs1 + edit->curs2 - byte_index - 1;
1743 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1744 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1746 else
1748 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
1749 (byte_index & M_EDIT_BUF_SIZE));
1753 /* --------------------------------------------------------------------------------------------- */
1755 char *
1756 edit_get_buf_ptr (WEdit * edit, long byte_index)
1758 unsigned long p;
1760 if (byte_index >= (edit->curs1 + edit->curs2))
1761 byte_index--;
1763 if (byte_index < 0)
1764 return NULL;
1766 if (byte_index >= edit->curs1)
1768 p = edit->curs1 + edit->curs2 - 1;
1769 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1770 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1772 else
1774 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
1778 /* --------------------------------------------------------------------------------------------- */
1781 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1783 gchar *str = NULL;
1784 int res = -1;
1785 gunichar ch;
1786 gchar *next_ch = NULL;
1787 int width = 0;
1789 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1791 *char_width = 0;
1792 return '\n';
1795 str = edit_get_byte_ptr (edit, byte_index);
1797 if (str == NULL)
1799 *char_width = 0;
1800 return 0;
1803 res = g_utf8_get_char_validated (str, -1);
1805 if (res < 0)
1807 ch = *str;
1808 width = 0;
1810 else
1812 ch = res;
1813 /* Calculate UTF-8 char width */
1814 next_ch = g_utf8_next_char (str);
1815 if (next_ch)
1817 width = next_ch - str;
1819 else
1821 ch = 0;
1822 width = 0;
1825 *char_width = width;
1826 return ch;
1829 /* --------------------------------------------------------------------------------------------- */
1832 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
1834 gchar *str, *buf = NULL;
1835 int res = -1;
1836 gunichar ch;
1837 gchar *next_ch = NULL;
1838 int width = 0;
1840 if (byte_index > 0)
1841 byte_index--;
1843 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1845 *char_width = 0;
1846 return 0;
1849 ch = edit_get_utf (edit, byte_index, &width);
1851 if (width == 1)
1853 *char_width = width;
1854 return ch;
1857 str = edit_get_byte_ptr (edit, byte_index);
1858 buf = edit_get_buf_ptr (edit, byte_index);
1859 if (str == NULL || buf == NULL)
1861 *char_width = 0;
1862 return 0;
1864 /* get prev utf8 char */
1865 if (str != buf)
1866 str = g_utf8_find_prev_char (buf, str);
1868 if (str == NULL)
1870 *char_width = 0;
1871 return 0;
1873 else
1874 res = g_utf8_get_char_validated (str, -1);
1876 if (res < 0)
1878 ch = *str;
1879 width = 0;
1881 else
1883 ch = res;
1884 /* Calculate UTF-8 char width */
1885 next_ch = g_utf8_next_char (str);
1886 if (next_ch)
1888 width = next_ch - str;
1890 else
1892 ch = 0;
1893 width = 0;
1896 *char_width = width;
1897 return ch;
1900 /* --------------------------------------------------------------------------------------------- */
1902 char *
1903 edit_get_write_filter (const char *write_name, const char *filename)
1905 int i;
1906 char *p, *writename;
1908 i = edit_find_filter (filename);
1909 if (i < 0)
1910 return NULL;
1912 writename = name_quote (write_name, 0);
1913 p = g_strdup_printf (all_filters[i].write, writename);
1914 g_free (writename);
1915 return p;
1918 /* --------------------------------------------------------------------------------------------- */
1920 long
1921 edit_write_stream (WEdit * edit, FILE * f)
1923 long i;
1925 if (edit->lb == LB_ASIS)
1927 for (i = 0; i < edit->last_byte; i++)
1928 if (fputc (edit_get_byte (edit, i), f) < 0)
1929 break;
1930 return i;
1933 /* change line breaks */
1934 for (i = 0; i < edit->last_byte; i++)
1936 unsigned char c = edit_get_byte (edit, i);
1938 if (!(c == '\n' || c == '\r'))
1940 /* not line break */
1941 if (fputc (c, f) < 0)
1942 return i;
1944 else
1945 { /* (c == '\n' || c == '\r') */
1946 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1948 switch (edit->lb)
1950 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1951 /* put one line break unconditionally */
1952 if (fputc ('\n', f) < 0)
1953 return i;
1955 i++; /* 2 chars are processed */
1957 if (c == '\r' && c1 == '\n')
1958 /* Windows line break; go to the next char */
1959 break;
1961 if (c == '\r' && c1 == '\r')
1963 /* two Macintosh line breaks; put second line break */
1964 if (fputc ('\n', f) < 0)
1965 return i;
1966 break;
1969 if (fputc (c1, f) < 0)
1970 return i;
1971 break;
1973 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1974 /* put one line break unconditionally */
1975 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1976 return i;
1978 if (c == '\r' && c1 == '\n')
1979 /* Windows line break; go to the next char */
1980 i++;
1981 break;
1983 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1984 /* put one line break unconditionally */
1985 if (fputc ('\r', f) < 0)
1986 return i;
1988 i++; /* 2 chars are processed */
1990 if (c == '\r' && c1 == '\n')
1991 /* Windows line break; go to the next char */
1992 break;
1994 if (c == '\n' && c1 == '\n')
1996 /* two Windows line breaks; put second line break */
1997 if (fputc ('\r', f) < 0)
1998 return i;
1999 break;
2002 if (fputc (c1, f) < 0)
2003 return i;
2004 break;
2005 case LB_ASIS: /* default without changes */
2006 break;
2011 return edit->last_byte;
2014 /* --------------------------------------------------------------------------------------------- */
2015 /** inserts a file at the cursor, returns count of inserted bytes on success */
2016 long
2017 edit_insert_file (WEdit * edit, const char *filename)
2019 char *p;
2020 long ins_len = 0;
2022 p = edit_get_filter (filename);
2023 if (p != NULL)
2025 FILE *f;
2026 long current = edit->curs1;
2027 f = (FILE *) popen (p, "r");
2028 if (f != NULL)
2030 edit_insert_stream (edit, f);
2031 ins_len = edit->curs1 - current;
2032 edit_cursor_move (edit, current - edit->curs1);
2033 if (pclose (f) > 0)
2035 char *errmsg;
2036 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2037 edit_error_dialog (_("Error"), errmsg);
2038 g_free (errmsg);
2039 g_free (p);
2040 return 0;
2043 else
2045 char *errmsg;
2046 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2047 edit_error_dialog (_("Error"), errmsg);
2048 g_free (errmsg);
2049 g_free (p);
2050 return 0;
2052 g_free (p);
2054 else
2056 int i, file, blocklen;
2057 long current = edit->curs1;
2058 int vertical_insertion = 0;
2059 char *buf;
2060 file = mc_open (filename, O_RDONLY | O_BINARY);
2061 if (file == -1)
2062 return 0;
2063 buf = g_malloc0 (TEMP_BUF_LEN);
2064 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2065 if (blocklen > 0)
2067 /* if contain signature VERTICAL_MAGIC then it vertical block */
2068 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2069 vertical_insertion = 1;
2070 else
2071 mc_lseek (file, 0, SEEK_SET);
2073 if (vertical_insertion)
2075 blocklen = edit_insert_column_of_text_from_file (edit, file);
2077 else
2079 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2081 for (i = 0; i < blocklen; i++)
2082 edit_insert (edit, buf[i]);
2085 ins_len = edit->curs1 - current;
2086 edit_cursor_move (edit, current - edit->curs1);
2087 g_free (buf);
2088 mc_close (file);
2089 if (blocklen != 0)
2090 return 0;
2092 return ins_len;
2095 /* --------------------------------------------------------------------------------------------- */
2097 * Fill in the edit structure. Return NULL on failure. Pass edit as
2098 * NULL to allocate a new structure.
2100 * If line is 0, try to restore saved position. Otherwise put the
2101 * cursor on that line and show it in the middle of the screen.
2104 WEdit *
2105 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
2107 int to_free = 0;
2108 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2109 if (option_line_state)
2111 option_line_state_width = LINE_STATE_WIDTH;
2113 else
2115 option_line_state_width = 0;
2117 if (!edit)
2119 #ifdef ENABLE_NLS
2121 * Expand option_whole_chars_search by national letters using
2122 * current locale
2125 static char option_whole_chars_search_buf[256];
2127 if (option_whole_chars_search_buf != option_whole_chars_search)
2129 size_t i;
2130 size_t len = str_term_width1 (option_whole_chars_search);
2132 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2134 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2136 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2138 option_whole_chars_search_buf[len++] = i;
2142 option_whole_chars_search_buf[len] = 0;
2143 option_whole_chars_search = option_whole_chars_search_buf;
2145 #endif /* ENABLE_NLS */
2146 edit = g_malloc0 (sizeof (WEdit));
2147 edit->search = NULL;
2148 to_free = 1;
2150 edit_purge_widget (edit);
2151 edit->num_widget_lines = lines;
2152 edit->over_col = 0;
2153 edit->num_widget_columns = columns;
2154 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2155 edit->stat1.st_uid = getuid ();
2156 edit->stat1.st_gid = getgid ();
2157 edit->stat1.st_mtime = 0;
2158 edit->bracket = -1;
2159 edit->force |= REDRAW_PAGE;
2160 edit_set_filename (edit, filename);
2161 edit->undo_stack_size = START_STACK_SIZE;
2162 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2163 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2165 edit->redo_stack_size = START_STACK_SIZE;
2166 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2167 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2169 edit->utf8 = 0;
2170 edit->converter = str_cnv_from_term;
2171 edit_set_codeset (edit);
2173 if (edit_load_file (edit))
2175 /* edit_load_file already gives an error message */
2176 if (to_free)
2177 g_free (edit);
2178 return 0;
2181 edit->loading_done = 1;
2182 edit->modified = 0;
2183 edit->locked = 0;
2184 edit_load_syntax (edit, NULL, NULL);
2186 int color;
2187 edit_get_syntax_color (edit, -1, &color);
2190 /* load saved cursor position */
2191 if ((line == 0) && option_save_position)
2193 edit_load_position (edit);
2195 else
2197 if (line <= 0)
2198 line = 1;
2199 edit_move_display (edit, line - 1);
2200 edit_move_to_line (edit, line - 1);
2203 edit_set_keymap ();
2204 edit_load_macro_cmd (edit);
2205 return edit;
2208 /* --------------------------------------------------------------------------------------------- */
2209 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2212 edit_clean (WEdit * edit)
2214 int j = 0;
2216 if (!edit)
2217 return 0;
2219 /* a stale lock, remove it */
2220 if (edit->locked)
2221 edit->locked = edit_unlock_file (edit);
2223 /* save cursor position */
2224 if (option_save_position)
2225 edit_save_position (edit);
2226 else if (edit->serialized_bookmarks != NULL)
2227 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2229 /* File specified on the mcedit command line and never saved */
2230 if (edit->delete_file)
2231 unlink (edit->filename);
2233 edit_free_syntax_rules (edit);
2234 book_mark_flush (edit, -1);
2235 for (; j <= MAXBUFF; j++)
2237 g_free (edit->buffers1[j]);
2238 g_free (edit->buffers2[j]);
2241 g_free (edit->undo_stack);
2242 g_free (edit->redo_stack);
2243 g_free (edit->filename);
2244 g_free (edit->dir);
2245 mc_search_free (edit->search);
2246 edit->search = NULL;
2248 if (edit->converter != str_cnv_from_term)
2249 str_close_conv (edit->converter);
2251 edit_purge_widget (edit);
2253 return 1;
2256 /* --------------------------------------------------------------------------------------------- */
2257 /** returns 1 on success */
2260 edit_renew (WEdit * edit)
2262 int lines = edit->num_widget_lines;
2263 int columns = edit->num_widget_columns;
2265 edit_clean (edit);
2266 return (edit_init (edit, lines, columns, "", 0) != NULL);
2269 /* --------------------------------------------------------------------------------------------- */
2271 * Load a new file into the editor. If it fails, preserve the old file.
2272 * To do it, allocate a new widget, initialize it and, if the new file
2273 * was loaded, copy the data to the old widget.
2274 * Return 1 on success, 0 on failure.
2278 edit_reload (WEdit * edit, const char *filename)
2280 WEdit *e;
2281 int lines = edit->num_widget_lines;
2282 int columns = edit->num_widget_columns;
2284 e = g_malloc0 (sizeof (WEdit));
2285 e->widget = edit->widget;
2286 if (!edit_init (e, lines, columns, filename, 0))
2288 g_free (e);
2289 return 0;
2291 edit_clean (edit);
2292 memcpy (edit, e, sizeof (WEdit));
2293 g_free (e);
2294 return 1;
2297 /* --------------------------------------------------------------------------------------------- */
2299 * Load a new file into the editor and set line. If it fails, preserve the old file.
2300 * To do it, allocate a new widget, initialize it and, if the new file
2301 * was loaded, copy the data to the old widget.
2302 * Return 1 on success, 0 on failure.
2306 edit_reload_line (WEdit * edit, const char *filename, long line)
2308 WEdit *e;
2309 int lines = edit->num_widget_lines;
2310 int columns = edit->num_widget_columns;
2312 e = g_malloc0 (sizeof (WEdit));
2313 e->widget = edit->widget;
2314 if (!edit_init (e, lines, columns, filename, line))
2316 g_free (e);
2317 return 0;
2319 edit_clean (edit);
2320 memcpy (edit, e, sizeof (WEdit));
2321 g_free (e);
2322 return 1;
2325 /* --------------------------------------------------------------------------------------------- */
2327 void
2328 edit_set_codeset (WEdit * edit)
2330 #ifdef HAVE_CHARSET
2331 const char *cp_id;
2333 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
2335 if (cp_id != NULL)
2337 GIConv conv;
2338 conv = str_crt_conv_from (cp_id);
2339 if (conv != INVALID_CONV)
2341 if (edit->converter != str_cnv_from_term)
2342 str_close_conv (edit->converter);
2343 edit->converter = conv;
2347 if (cp_id != NULL)
2348 edit->utf8 = str_isutf8 (cp_id);
2349 #else
2350 (void) edit;
2351 #endif
2355 /* --------------------------------------------------------------------------------------------- */
2357 Recording stack for undo:
2358 The following is an implementation of a compressed stack. Identical
2359 pushes are recorded by a negative prefix indicating the number of times the
2360 same char was pushed. This saves space for repeated curs-left or curs-right
2361 delete etc.
2365 pushed: stored:
2369 b -3
2371 c --> -4
2377 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2378 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2379 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2380 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2381 position.
2383 The only way the cursor moves or the buffer is changed is through the routines:
2384 insert, backspace, insert_ahead, delete, and cursor_move.
2385 These record the reverse undo movements onto the stack each time they are
2386 called.
2388 Each key press results in a set of actions (insert; delete ...). So each time
2389 a key is pressed the current position of start_display is pushed as
2390 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2391 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2392 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2396 void
2397 edit_push_undo_action (WEdit * edit, long c, ...)
2399 unsigned long sp = edit->undo_stack_pointer;
2400 unsigned long spm1;
2401 long *t;
2403 /* first enlarge the stack if necessary */
2404 if (sp > edit->undo_stack_size - 10)
2405 { /* say */
2406 if (option_max_undo < 256)
2407 option_max_undo = 256;
2408 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2410 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2411 if (t)
2413 edit->undo_stack = t;
2414 edit->undo_stack_size <<= 1;
2415 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2419 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2420 if (edit->undo_stack_disable)
2422 edit_push_redo_action (edit, KEY_PRESS);
2423 edit_push_redo_action (edit, c);
2424 return;
2426 else if (edit->redo_stack_reset)
2428 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2431 if (edit->undo_stack_bottom != sp
2432 && spm1 != edit->undo_stack_bottom
2433 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2435 int d;
2436 if (edit->undo_stack[spm1] < 0)
2438 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2439 if (d == c)
2441 if (edit->undo_stack[spm1] > -1000000000)
2443 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2445 edit->undo_stack[spm1]--;
2447 return;
2451 else
2453 d = edit->undo_stack[spm1];
2454 if (d == c)
2456 if (c >= KEY_PRESS)
2457 return; /* --> no need to push multiple do-nothings */
2458 edit->undo_stack[sp] = -2;
2459 goto check_bottom;
2463 edit->undo_stack[sp] = c;
2465 check_bottom:
2466 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2468 /* if the sp wraps round and catches the undo_stack_bottom then erase
2469 * the first set of actions on the stack to make space - by moving
2470 * undo_stack_bottom forward one "key press" */
2471 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2472 if ((unsigned long) c == edit->undo_stack_bottom ||
2473 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2476 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2478 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2479 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2481 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2482 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2483 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2485 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2489 void
2490 edit_push_redo_action (WEdit * edit, long c, ...)
2492 unsigned long sp = edit->redo_stack_pointer;
2493 unsigned long spm1;
2494 long *t;
2495 /* first enlarge the stack if necessary */
2496 if (sp > edit->redo_stack_size - 10)
2497 { /* say */
2498 if (option_max_undo < 256)
2499 option_max_undo = 256;
2500 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2502 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2503 if (t)
2505 edit->redo_stack = t;
2506 edit->redo_stack_size <<= 1;
2507 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2511 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2513 if (edit->redo_stack_bottom != sp
2514 && spm1 != edit->redo_stack_bottom
2515 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2517 int d;
2518 if (edit->redo_stack[spm1] < 0)
2520 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2521 if (d == c)
2523 if (edit->redo_stack[spm1] > -1000000000)
2525 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2526 edit->redo_stack[spm1]--;
2527 return;
2531 else
2533 d = edit->redo_stack[spm1];
2534 if (d == c)
2536 if (c >= KEY_PRESS)
2537 return; /* --> no need to push multiple do-nothings */
2538 edit->redo_stack[sp] = -2;
2539 goto redo_check_bottom;
2543 edit->redo_stack[sp] = c;
2545 redo_check_bottom:
2546 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2548 /* if the sp wraps round and catches the redo_stack_bottom then erase
2549 * the first set of actions on the stack to make space - by moving
2550 * redo_stack_bottom forward one "key press" */
2551 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2552 if ((unsigned long) c == edit->redo_stack_bottom ||
2553 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2556 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2558 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2559 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2562 * If a single key produced enough pushes to wrap all the way round then
2563 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2564 * The stack is then initialised:
2567 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2568 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2569 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2573 /* --------------------------------------------------------------------------------------------- */
2575 Basic low level single character buffer alterations and movements at the cursor.
2576 Returns char passed over, inserted or removed.
2579 void
2580 edit_insert (WEdit * edit, int c)
2582 /* check if file has grown to large */
2583 if (edit->last_byte >= SIZE_LIMIT)
2584 return;
2586 /* first we must update the position of the display window */
2587 if (edit->curs1 < edit->start_display)
2589 edit->start_display++;
2590 if (c == '\n')
2591 edit->start_line++;
2594 /* Mark file as modified, unless the file hasn't been fully loaded */
2595 if (edit->loading_done)
2597 edit_modification (edit);
2600 /* now we must update some info on the file and check if a redraw is required */
2601 if (c == '\n')
2603 if (edit->book_mark)
2604 book_mark_inc (edit, edit->curs_line);
2605 edit->curs_line++;
2606 edit->total_lines++;
2607 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2610 /* save the reverse command onto the undo stack */
2611 /* ordinary char and not space */
2612 if (c > 32)
2613 edit_push_undo_action (edit, BACKSPACE);
2614 else
2615 edit_push_undo_action (edit, BACKSPACE_BR);
2616 /* update markers */
2617 edit->mark1 += (edit->mark1 > edit->curs1);
2618 edit->mark2 += (edit->mark2 > edit->curs1);
2619 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2621 /* add a new buffer if we've reached the end of the last one */
2622 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2623 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2625 /* perform the insertion */
2626 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2627 = (unsigned char) c;
2629 /* update file length */
2630 edit->last_byte++;
2632 /* update cursor position */
2633 edit->curs1++;
2636 /* --------------------------------------------------------------------------------------------- */
2637 /** same as edit_insert and move left */
2639 void
2640 edit_insert_ahead (WEdit * edit, int c)
2642 if (edit->last_byte >= SIZE_LIMIT)
2643 return;
2645 if (edit->curs1 < edit->start_display)
2647 edit->start_display++;
2648 if (c == '\n')
2649 edit->start_line++;
2651 edit_modification (edit);
2652 if (c == '\n')
2654 if (edit->book_mark)
2655 book_mark_inc (edit, edit->curs_line);
2656 edit->total_lines++;
2657 edit->force |= REDRAW_AFTER_CURSOR;
2659 /* ordinary char and not space */
2660 if (c > 32)
2661 edit_push_undo_action (edit, DELCHAR);
2662 else
2663 edit_push_undo_action (edit, DELCHAR_BR);
2665 edit->mark1 += (edit->mark1 >= edit->curs1);
2666 edit->mark2 += (edit->mark2 >= edit->curs1);
2667 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2669 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2670 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2671 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2672 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2674 edit->last_byte++;
2675 edit->curs2++;
2679 /* --------------------------------------------------------------------------------------------- */
2682 edit_delete (WEdit * edit, const int byte_delete)
2684 int p = 0;
2685 int cw = 1;
2686 int i;
2688 if (!edit->curs2)
2689 return 0;
2691 cw = 1;
2692 /* if byte_delete = 1 then delete only one byte not multibyte char */
2693 if (edit->utf8 && byte_delete == 0)
2695 edit_get_utf (edit, edit->curs1, &cw);
2696 if (cw < 1)
2697 cw = 1;
2700 if (edit->mark2 != edit->mark1)
2701 edit_push_markers (edit);
2703 for (i = 1; i <= cw; i++)
2705 if (edit->mark1 > edit->curs1)
2707 edit->mark1--;
2708 edit->end_mark_curs--;
2710 if (edit->mark2 > edit->curs1)
2711 edit->mark2--;
2712 if (edit->last_get_rule > edit->curs1)
2713 edit->last_get_rule--;
2715 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2716 ((edit->curs2 -
2717 1) & M_EDIT_BUF_SIZE) - 1];
2719 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2721 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2722 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2724 edit->last_byte--;
2725 edit->curs2--;
2726 edit_push_undo_action (edit, p + 256);
2729 edit_modification (edit);
2730 if (p == '\n')
2732 if (edit->book_mark)
2733 book_mark_dec (edit, edit->curs_line);
2734 edit->total_lines--;
2735 edit->force |= REDRAW_AFTER_CURSOR;
2737 if (edit->curs1 < edit->start_display)
2739 edit->start_display--;
2740 if (p == '\n')
2741 edit->start_line--;
2744 return p;
2747 /* --------------------------------------------------------------------------------------------- */
2748 /** moves the cursor right or left: increment positive or negative respectively */
2750 void
2751 edit_cursor_move (WEdit * edit, long increment)
2753 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2754 int c;
2756 if (increment < 0)
2758 for (; increment < 0; increment++)
2760 if (!edit->curs1)
2761 return;
2763 edit_push_undo_action (edit, CURS_RIGHT);
2765 c = edit_get_byte (edit, edit->curs1 - 1);
2766 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2767 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2768 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2769 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2770 edit->curs2++;
2771 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2772 1) & M_EDIT_BUF_SIZE];
2773 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2775 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2776 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2778 edit->curs1--;
2779 if (c == '\n')
2781 edit->curs_line--;
2782 edit->force |= REDRAW_LINE_BELOW;
2787 else if (increment > 0)
2789 for (; increment > 0; increment--)
2791 if (!edit->curs2)
2792 return;
2794 edit_push_undo_action (edit, CURS_LEFT);
2796 c = edit_get_byte (edit, edit->curs1);
2797 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2798 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2799 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2800 edit->curs1++;
2801 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2802 ((edit->curs2 -
2803 1) & M_EDIT_BUF_SIZE) - 1];
2804 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2806 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2807 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2809 edit->curs2--;
2810 if (c == '\n')
2812 edit->curs_line++;
2813 edit->force |= REDRAW_LINE_ABOVE;
2819 /* These functions return positions relative to lines */
2821 /* --------------------------------------------------------------------------------------------- */
2822 /** returns index of last char on line + 1 */
2824 long
2825 edit_eol (WEdit * edit, long current)
2827 if (current >= edit->last_byte)
2828 return edit->last_byte;
2830 for (;; current++)
2831 if (edit_get_byte (edit, current) == '\n')
2832 break;
2833 return current;
2836 /* --------------------------------------------------------------------------------------------- */
2837 /** returns index of first char on line */
2839 long
2840 edit_bol (WEdit * edit, long current)
2842 if (current <= 0)
2843 return 0;
2845 for (;; current--)
2846 if (edit_get_byte (edit, current - 1) == '\n')
2847 break;
2848 return current;
2851 /* --------------------------------------------------------------------------------------------- */
2853 long
2854 edit_count_lines (WEdit * edit, long current, long upto)
2856 long lines = 0;
2857 if (upto > edit->last_byte)
2858 upto = edit->last_byte;
2859 if (current < 0)
2860 current = 0;
2861 while (current < upto)
2862 if (edit_get_byte (edit, current++) == '\n')
2863 lines++;
2864 return lines;
2867 /* --------------------------------------------------------------------------------------------- */
2868 /* If lines is zero this returns the count of lines from current to upto. */
2869 /* If upto is zero returns index of lines forward current. */
2871 long
2872 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2874 if (upto)
2876 return edit_count_lines (edit, current, upto);
2878 else
2880 long next;
2881 if (lines < 0)
2882 lines = 0;
2883 while (lines--)
2885 next = edit_eol (edit, current) + 1;
2886 if (next > edit->last_byte)
2887 break;
2888 else
2889 current = next;
2891 return current;
2895 /* --------------------------------------------------------------------------------------------- */
2896 /** Returns offset of 'lines' lines up from current */
2898 long
2899 edit_move_backward (WEdit * edit, long current, long lines)
2901 if (lines < 0)
2902 lines = 0;
2903 current = edit_bol (edit, current);
2904 while ((lines--) && current != 0)
2905 current = edit_bol (edit, current - 1);
2906 return current;
2909 /* --------------------------------------------------------------------------------------------- */
2910 /* If cols is zero this returns the count of columns from current to upto. */
2911 /* If upto is zero returns index of cols across from current. */
2913 long
2914 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
2916 long p, q;
2917 int col;
2919 if (upto)
2921 q = upto;
2922 cols = -10;
2924 else
2925 q = edit->last_byte + 2;
2927 for (col = 0, p = current; p < q; p++)
2929 int c, orig_c;
2931 if (cols != -10)
2933 if (col == cols)
2934 return p;
2935 if (col > cols)
2936 return p - 1;
2939 orig_c = c = edit_get_byte (edit, p);
2941 #ifdef HAVE_CHARSET
2942 if (edit->utf8)
2944 int utf_ch;
2945 int cw = 1;
2947 utf_ch = edit_get_utf (edit, p, &cw);
2948 if (utf8_display)
2950 if (cw > 1)
2951 col -= cw - 1;
2952 if (g_unichar_iswide (utf_ch))
2953 col++;
2955 else if (cw > 1 && g_unichar_isprint (utf_ch))
2956 col -= cw - 1;
2959 c = convert_to_display_c (c);
2960 #endif
2962 if (c == '\t')
2963 col += TAB_SIZE - col % TAB_SIZE;
2964 else if (c == '\n')
2966 if (upto)
2967 return col;
2968 else
2969 return p;
2971 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
2972 /* '\r' is shown as ^M, so we must advance 2 characters */
2973 /* Caret notation for control characters */
2974 col += 2;
2975 else
2976 col++;
2978 return col;
2981 /* --------------------------------------------------------------------------------------------- */
2982 /** returns the current column position of the cursor */
2985 edit_get_col (WEdit * edit)
2987 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
2990 /* --------------------------------------------------------------------------------------------- */
2991 /* Scrolling functions */
2992 /* --------------------------------------------------------------------------------------------- */
2994 void
2995 edit_update_curs_row (WEdit * edit)
2997 edit->curs_row = edit->curs_line - edit->start_line;
3000 /* --------------------------------------------------------------------------------------------- */
3002 void
3003 edit_update_curs_col (WEdit * edit)
3005 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3008 /* --------------------------------------------------------------------------------------------- */
3011 edit_get_curs_col (const WEdit * edit)
3013 return edit->curs_col;
3016 /* --------------------------------------------------------------------------------------------- */
3017 /** moves the display start position up by i lines */
3019 void
3020 edit_scroll_upward (WEdit * edit, unsigned long i)
3022 unsigned long lines_above = edit->start_line;
3023 if (i > lines_above)
3024 i = lines_above;
3025 if (i)
3027 edit->start_line -= i;
3028 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3029 edit->force |= REDRAW_PAGE;
3030 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3032 edit_update_curs_row (edit);
3036 /* --------------------------------------------------------------------------------------------- */
3037 /** returns 1 if could scroll, 0 otherwise */
3039 void
3040 edit_scroll_downward (WEdit * edit, int i)
3042 int lines_below;
3043 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
3044 if (lines_below > 0)
3046 if (i > lines_below)
3047 i = lines_below;
3048 edit->start_line += i;
3049 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3050 edit->force |= REDRAW_PAGE;
3051 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3053 edit_update_curs_row (edit);
3056 /* --------------------------------------------------------------------------------------------- */
3058 void
3059 edit_scroll_right (WEdit * edit, int i)
3061 edit->force |= REDRAW_PAGE;
3062 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3063 edit->start_col -= i;
3066 /* --------------------------------------------------------------------------------------------- */
3068 void
3069 edit_scroll_left (WEdit * edit, int i)
3071 if (edit->start_col)
3073 edit->start_col += i;
3074 if (edit->start_col > 0)
3075 edit->start_col = 0;
3076 edit->force |= REDRAW_PAGE;
3077 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3081 /* --------------------------------------------------------------------------------------------- */
3082 /* high level cursor movement commands */
3083 /* --------------------------------------------------------------------------------------------- */
3085 void
3086 edit_move_to_prev_col (WEdit * edit, long p)
3088 int prev = edit->prev_col;
3089 int over = edit->over_col;
3090 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3092 if (option_cursor_beyond_eol)
3094 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3095 edit_eol (edit, edit->curs1));
3097 if (line_len < prev + edit->over_col)
3099 edit->over_col = prev + over - line_len;
3100 edit->prev_col = line_len;
3101 edit->curs_col = line_len;
3103 else
3105 edit->curs_col = prev + over;
3106 edit->prev_col = edit->curs_col;
3107 edit->over_col = 0;
3110 else
3112 edit->over_col = 0;
3113 if (is_in_indent (edit) && option_fake_half_tabs)
3115 edit_update_curs_col (edit);
3116 if (space_width)
3117 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3119 int q = edit->curs_col;
3120 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3121 p = edit_bol (edit, edit->curs1);
3122 edit_cursor_move (edit,
3123 edit_move_forward3 (edit, p, edit->curs_col,
3124 0) - edit->curs1);
3125 if (!left_of_four_spaces (edit))
3126 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3132 /* --------------------------------------------------------------------------------------------- */
3135 line_is_blank (WEdit * edit, long line)
3137 return is_blank (edit, edit_find_line (edit, line));
3140 /* --------------------------------------------------------------------------------------------- */
3141 /** move cursor to line 'line' */
3143 void
3144 edit_move_to_line (WEdit * e, long line)
3146 if (line < e->curs_line)
3147 edit_move_up (e, e->curs_line - line, 0);
3148 else
3149 edit_move_down (e, line - e->curs_line, 0);
3150 edit_scroll_screen_over_cursor (e);
3153 /* --------------------------------------------------------------------------------------------- */
3154 /** scroll window so that first visible line is 'line' */
3156 void
3157 edit_move_display (WEdit * e, long line)
3159 if (line < e->start_line)
3160 edit_scroll_upward (e, e->start_line - line);
3161 else
3162 edit_scroll_downward (e, line - e->start_line);
3165 /* --------------------------------------------------------------------------------------------- */
3166 /** save markers onto undo stack */
3168 void
3169 edit_push_markers (WEdit * edit)
3171 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3172 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3173 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3176 /* --------------------------------------------------------------------------------------------- */
3178 void
3179 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3181 edit->mark1 = m1;
3182 edit->mark2 = m2;
3183 edit->column1 = c1;
3184 edit->column2 = c2;
3188 /* --------------------------------------------------------------------------------------------- */
3189 /** highlight marker toggle */
3191 void
3192 edit_mark_cmd (WEdit * edit, int unmark)
3194 edit_push_markers (edit);
3195 if (unmark)
3197 edit_set_markers (edit, 0, 0, 0, 0);
3198 edit->force |= REDRAW_PAGE;
3200 else
3202 if (edit->mark2 >= 0)
3204 edit->end_mark_curs = -1;
3205 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3206 edit->curs_col + edit->over_col);
3207 edit->force |= REDRAW_PAGE;
3209 else
3211 edit->end_mark_curs = edit->curs1;
3212 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3213 edit->curs_col + edit->over_col);
3218 /* --------------------------------------------------------------------------------------------- */
3219 /** highlight the word under cursor */
3221 void
3222 edit_mark_current_word_cmd (WEdit * edit)
3224 long pos;
3226 for (pos = edit->curs1; pos != 0; pos--)
3228 int c1, c2;
3230 c1 = edit_get_byte (edit, pos);
3231 c2 = edit_get_byte (edit, pos - 1);
3232 if (!isspace (c1) && isspace (c2))
3233 break;
3234 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3235 break;
3237 edit->mark1 = pos;
3239 for (; pos < edit->last_byte; pos++)
3241 int c1, c2;
3243 c1 = edit_get_byte (edit, pos);
3244 c2 = edit_get_byte (edit, pos + 1);
3245 if (!isspace (c1) && isspace (c2))
3246 break;
3247 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3248 break;
3250 edit->mark2 = min (pos + 1, edit->last_byte);
3252 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3255 /* --------------------------------------------------------------------------------------------- */
3257 void
3258 edit_mark_current_line_cmd (WEdit * edit)
3260 long pos = edit->curs1;
3262 edit->mark1 = edit_bol (edit, pos);
3263 edit->mark2 = edit_eol (edit, pos);
3265 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3268 /* --------------------------------------------------------------------------------------------- */
3270 void
3271 edit_delete_line (WEdit * edit)
3274 * Delete right part of the line.
3275 * Note that edit_get_byte() returns '\n' when byte position is
3276 * beyond EOF.
3278 while (edit_get_byte (edit, edit->curs1) != '\n')
3280 (void) edit_delete (edit, 1);
3284 * Delete '\n' char.
3285 * Note that edit_delete() will not corrupt anything if called while
3286 * cursor position is EOF.
3288 (void) edit_delete (edit, 1);
3291 * Delete left part of the line.
3292 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3294 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3296 (void) edit_backspace (edit, 1);
3300 /* --------------------------------------------------------------------------------------------- */
3302 void
3303 insert_spaces_tab (WEdit * edit, int half)
3305 int i;
3306 edit_update_curs_col (edit);
3307 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
3308 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
3309 while (i > 0)
3311 edit_insert (edit, ' ');
3312 i -= space_width;
3316 /* --------------------------------------------------------------------------------------------- */
3319 edit_indent_width (WEdit * edit, long p)
3321 long q = p;
3322 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3323 q++;
3324 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3327 /* --------------------------------------------------------------------------------------------- */
3329 void
3330 edit_insert_indent (WEdit * edit, int indent)
3332 if (!option_fill_tabs_with_spaces)
3334 while (indent >= TAB_SIZE)
3336 edit_insert (edit, '\t');
3337 indent -= TAB_SIZE;
3340 while (indent-- > 0)
3341 edit_insert (edit, ' ');
3344 /* --------------------------------------------------------------------------------------------- */
3346 void
3347 edit_push_key_press (WEdit * edit)
3349 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3350 if (edit->mark2 == -1)
3352 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3353 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3357 /* --------------------------------------------------------------------------------------------- */
3359 void
3360 edit_find_bracket (WEdit * edit)
3362 edit->bracket = edit_get_bracket (edit, 1, 10000);
3363 if (last_bracket != edit->bracket)
3364 edit->force |= REDRAW_PAGE;
3365 last_bracket = edit->bracket;
3368 /* --------------------------------------------------------------------------------------------- */
3370 * This executes a command as though the user initiated it through a key
3371 * press. Callback with WIDGET_KEY as a message calls this after
3372 * translating the key press. This function can be used to pass any
3373 * command to the editor. Note that the screen wouldn't update
3374 * automatically. Either of command or char_for_insertion must be
3375 * passed as -1. Commands are executed, and char_for_insertion is
3376 * inserted at the cursor.
3379 void
3380 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3382 if (command == CK_Begin_Record_Macro || command == CK_Begin_Record_Repeat
3383 || (macro_index < 0 && (command == CK_Begin_End_Macro || command == CK_Begin_End_Repeat)))
3385 macro_index = 0;
3386 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3387 return;
3389 if (macro_index != -1)
3391 edit->force |= REDRAW_COMPLETELY;
3392 if (command == CK_End_Record_Macro || command == CK_Begin_End_Macro)
3394 edit_store_macro_cmd (edit);
3395 macro_index = -1;
3396 return;
3398 else if (command == CK_End_Record_Repeat || command == CK_Begin_End_Repeat)
3400 edit_repeat_macro_cmd (edit);
3401 macro_index = -1;
3402 return;
3407 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3409 record_macro_buf[macro_index].action = command;
3410 record_macro_buf[macro_index++].ch = char_for_insertion;
3412 /* record the beginning of a set of editing actions initiated by a key press */
3413 if (command != CK_Undo && command != CK_Ext_Mode)
3414 edit_push_key_press (edit);
3416 edit_execute_cmd (edit, command, char_for_insertion);
3417 if (edit->column_highlight)
3418 edit->force |= REDRAW_PAGE;
3421 /* --------------------------------------------------------------------------------------------- */
3423 This executes a command at a lower level than macro recording.
3424 It also does not push a key_press onto the undo stack. This means
3425 that if it is called many times, a single undo command will undo
3426 all of them. It also does not check for the Undo command.
3428 void
3429 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3431 edit->force |= REDRAW_LINE;
3433 /* The next key press will unhighlight the found string, so update
3434 * the whole page */
3435 if (edit->found_len || edit->column_highlight)
3436 edit->force |= REDRAW_PAGE;
3438 if (command / 100 == 6)
3439 { /* a highlight command like shift-arrow */
3440 edit->column_highlight = 0;
3441 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3443 edit_mark_cmd (edit, 1); /* clear */
3444 edit_mark_cmd (edit, 0); /* marking on */
3446 edit->highlight = 1;
3448 else
3449 { /* any other command */
3450 if (edit->highlight)
3451 edit_mark_cmd (edit, 0); /* clear */
3452 edit->highlight = 0;
3455 /* first check for undo */
3456 if (command == CK_Undo)
3458 edit->redo_stack_reset = 0;
3459 edit_group_undo (edit);
3460 edit->found_len = 0;
3461 edit->prev_col = edit_get_col (edit);
3462 edit->search_start = edit->curs1;
3463 return;
3465 /* check for redo */
3466 if (command == CK_Redo)
3468 edit->redo_stack_reset = 0;
3469 edit_do_redo (edit);
3470 edit->found_len = 0;
3471 edit->prev_col = edit_get_col (edit);
3472 edit->search_start = edit->curs1;
3473 return;
3476 edit->redo_stack_reset = 1;
3478 /* An ordinary key press */
3479 if (char_for_insertion >= 0)
3481 /* if non persistent selection and text selected */
3482 if (!option_persistent_selections)
3484 if (edit->mark1 != edit->mark2)
3485 edit_block_delete_cmd (edit);
3487 if (edit->overwrite)
3489 /* remove char only one time, after input first byte, multibyte chars */
3490 if ((!utf8_display || edit->charpoint == 0)
3491 && edit_get_byte (edit, edit->curs1) != '\n')
3492 edit_delete (edit, 0);
3494 if (option_cursor_beyond_eol && edit->over_col > 0)
3495 edit_insert_over (edit);
3496 #ifdef HAVE_CHARSET
3497 if (char_for_insertion > 255 && utf8_display == 0)
3499 unsigned char str[6 + 1];
3500 size_t i = 0;
3501 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3502 if (res == 0)
3504 str[0] = '.';
3505 str[1] = '\0';
3507 else
3509 str[res] = '\0';
3511 while (str[i] != 0 && i <= 6)
3513 char_for_insertion = str[i];
3514 edit_insert (edit, char_for_insertion);
3515 i++;
3518 else
3519 #endif
3520 edit_insert (edit, char_for_insertion);
3522 if (option_auto_para_formatting)
3524 format_paragraph (edit, 0);
3525 edit->force |= REDRAW_PAGE;
3527 else
3528 check_and_wrap_line (edit);
3529 edit->found_len = 0;
3530 edit->prev_col = edit_get_col (edit);
3531 edit->search_start = edit->curs1;
3532 edit_find_bracket (edit);
3533 return;
3536 switch (command)
3538 case CK_Begin_Page:
3539 case CK_End_Page:
3540 case CK_Begin_Page_Highlight:
3541 case CK_End_Page_Highlight:
3542 case CK_Word_Left:
3543 case CK_Word_Right:
3544 case CK_Up:
3545 case CK_Down:
3546 case CK_Left:
3547 case CK_Right:
3548 if (edit->mark2 >= 0)
3550 if (!option_persistent_selections)
3552 if (edit->column_highlight)
3553 edit_push_undo_action (edit, COLUMN_ON);
3554 edit->column_highlight = 0;
3555 edit_mark_cmd (edit, 1);
3560 switch (command)
3562 case CK_Begin_Page:
3563 case CK_End_Page:
3564 case CK_Begin_Page_Highlight:
3565 case CK_End_Page_Highlight:
3566 case CK_Word_Left:
3567 case CK_Word_Right:
3568 case CK_Up:
3569 case CK_Down:
3570 case CK_Word_Left_Highlight:
3571 case CK_Word_Right_Highlight:
3572 case CK_Up_Highlight:
3573 case CK_Down_Highlight:
3574 case CK_Up_Alt_Highlight:
3575 case CK_Down_Alt_Highlight:
3576 if (edit->mark2 == -1)
3577 break; /*marking is following the cursor: may need to highlight a whole line */
3578 case CK_Left:
3579 case CK_Right:
3580 case CK_Left_Highlight:
3581 case CK_Right_Highlight:
3582 edit->force |= REDRAW_CHAR_ONLY;
3585 /* basic cursor key commands */
3586 switch (command)
3588 case CK_BackSpace:
3589 /* if non persistent selection and text selected */
3590 if (!option_persistent_selections)
3592 if (edit->mark1 != edit->mark2)
3594 edit_block_delete_cmd (edit);
3595 break;
3598 if (option_cursor_beyond_eol && edit->over_col > 0)
3600 edit->over_col--;
3601 break;
3603 if (option_backspace_through_tabs && is_in_indent (edit))
3605 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3606 edit_backspace (edit, 1);
3607 break;
3609 else
3611 if (option_fake_half_tabs)
3613 int i;
3614 if (is_in_indent (edit) && right_of_four_spaces (edit))
3616 for (i = 0; i < HALF_TAB_SIZE; i++)
3617 edit_backspace (edit, 1);
3618 break;
3622 edit_backspace (edit, 0);
3623 break;
3624 case CK_Delete:
3625 /* if non persistent selection and text selected */
3626 if (!option_persistent_selections)
3628 if (edit->mark1 != edit->mark2)
3630 edit_block_delete_cmd (edit);
3631 break;
3635 if (option_cursor_beyond_eol && edit->over_col > 0)
3636 edit_insert_over (edit);
3638 if (option_fake_half_tabs)
3640 int i;
3641 if (is_in_indent (edit) && left_of_four_spaces (edit))
3643 for (i = 1; i <= HALF_TAB_SIZE; i++)
3644 edit_delete (edit, 1);
3645 break;
3648 edit_delete (edit, 0);
3649 break;
3650 case CK_Delete_Word_Left:
3651 edit->over_col = 0;
3652 edit_left_delete_word (edit);
3653 break;
3654 case CK_Delete_Word_Right:
3655 if (option_cursor_beyond_eol && edit->over_col > 0)
3656 edit_insert_over (edit);
3658 edit_right_delete_word (edit);
3659 break;
3660 case CK_Delete_Line:
3661 edit_delete_line (edit);
3662 break;
3663 case CK_Delete_To_Line_End:
3664 edit_delete_to_line_end (edit);
3665 break;
3666 case CK_Delete_To_Line_Begin:
3667 edit_delete_to_line_begin (edit);
3668 break;
3669 case CK_Enter:
3670 edit->over_col = 0;
3671 if (option_auto_para_formatting)
3673 edit_double_newline (edit);
3674 if (option_return_does_auto_indent)
3675 edit_auto_indent (edit);
3676 format_paragraph (edit, 0);
3678 else
3680 edit_insert (edit, '\n');
3681 if (option_return_does_auto_indent)
3683 edit_auto_indent (edit);
3686 break;
3687 case CK_Return:
3688 edit_insert (edit, '\n');
3689 break;
3691 case CK_Page_Up_Alt_Highlight:
3692 edit->column_highlight = 1;
3693 case CK_Page_Up:
3694 case CK_Page_Up_Highlight:
3695 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3696 break;
3697 case CK_Page_Down_Alt_Highlight:
3698 edit->column_highlight = 1;
3699 case CK_Page_Down:
3700 case CK_Page_Down_Highlight:
3701 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3702 break;
3703 case CK_Left_Alt_Highlight:
3704 edit->column_highlight = 1;
3705 case CK_Left:
3706 case CK_Left_Highlight:
3707 if (option_fake_half_tabs)
3709 if (is_in_indent (edit) && right_of_four_spaces (edit))
3711 if (option_cursor_beyond_eol && edit->over_col > 0)
3712 edit->over_col--;
3713 else
3714 edit_cursor_move (edit, -HALF_TAB_SIZE);
3715 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3716 break;
3719 edit_left_char_move_cmd (edit);
3720 break;
3721 case CK_Right_Alt_Highlight:
3722 edit->column_highlight = 1;
3723 case CK_Right:
3724 case CK_Right_Highlight:
3725 if (option_fake_half_tabs)
3727 if (is_in_indent (edit) && left_of_four_spaces (edit))
3729 edit_cursor_move (edit, HALF_TAB_SIZE);
3730 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3731 break;
3734 edit_right_char_move_cmd (edit);
3735 break;
3736 case CK_Begin_Page:
3737 case CK_Begin_Page_Highlight:
3738 edit_begin_page (edit);
3739 break;
3740 case CK_End_Page:
3741 case CK_End_Page_Highlight:
3742 edit_end_page (edit);
3743 break;
3744 case CK_Word_Left:
3745 case CK_Word_Left_Highlight:
3746 edit->over_col = 0;
3747 edit_left_word_move_cmd (edit);
3748 break;
3749 case CK_Word_Right:
3750 case CK_Word_Right_Highlight:
3751 edit->over_col = 0;
3752 edit_right_word_move_cmd (edit);
3753 break;
3754 case CK_Up_Alt_Highlight:
3755 edit->column_highlight = 1;
3756 case CK_Up:
3757 case CK_Up_Highlight:
3758 edit_move_up (edit, 1, 0);
3759 break;
3760 case CK_Down_Alt_Highlight:
3761 edit->column_highlight = 1;
3762 case CK_Down:
3763 case CK_Down_Highlight:
3764 edit_move_down (edit, 1, 0);
3765 break;
3766 case CK_Paragraph_Up_Alt_Highlight:
3767 edit->column_highlight = 1;
3768 case CK_Paragraph_Up:
3769 case CK_Paragraph_Up_Highlight:
3770 edit_move_up_paragraph (edit, 0);
3771 break;
3772 case CK_Paragraph_Down_Alt_Highlight:
3773 edit->column_highlight = 1;
3774 case CK_Paragraph_Down:
3775 case CK_Paragraph_Down_Highlight:
3776 edit_move_down_paragraph (edit, 0);
3777 break;
3778 case CK_Scroll_Up_Alt_Highlight:
3779 edit->column_highlight = 1;
3780 case CK_Scroll_Up:
3781 case CK_Scroll_Up_Highlight:
3782 edit_move_up (edit, 1, 1);
3783 break;
3784 case CK_Scroll_Down_Alt_Highlight:
3785 edit->column_highlight = 1;
3786 case CK_Scroll_Down:
3787 case CK_Scroll_Down_Highlight:
3788 edit_move_down (edit, 1, 1);
3789 break;
3790 case CK_Home:
3791 case CK_Home_Highlight:
3792 edit_cursor_to_bol (edit);
3793 break;
3794 case CK_End:
3795 case CK_End_Highlight:
3796 edit_cursor_to_eol (edit);
3797 break;
3798 case CK_Tab:
3799 /* if text marked shift block */
3800 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3802 if (edit->mark2 < 0)
3803 edit_mark_cmd (edit, 0);
3804 edit_move_block_to_right (edit);
3806 else
3808 if (option_cursor_beyond_eol)
3809 edit_insert_over (edit);
3810 edit_tab_cmd (edit);
3811 if (option_auto_para_formatting)
3813 format_paragraph (edit, 0);
3814 edit->force |= REDRAW_PAGE;
3816 else
3818 check_and_wrap_line (edit);
3821 break;
3823 case CK_Toggle_Insert:
3824 edit->overwrite = (edit->overwrite == 0);
3825 break;
3827 case CK_Mark:
3828 if (edit->mark2 >= 0)
3830 if (edit->column_highlight)
3831 edit_push_undo_action (edit, COLUMN_ON);
3832 edit->column_highlight = 0;
3834 edit_mark_cmd (edit, 0);
3835 break;
3836 case CK_Column_Mark:
3837 if (!edit->column_highlight)
3838 edit_push_undo_action (edit, COLUMN_OFF);
3839 edit->column_highlight = 1;
3840 edit_mark_cmd (edit, 0);
3841 break;
3842 case CK_Mark_All:
3843 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3844 edit->force |= REDRAW_PAGE;
3845 break;
3846 case CK_Unmark:
3847 if (edit->column_highlight)
3848 edit_push_undo_action (edit, COLUMN_ON);
3849 edit->column_highlight = 0;
3850 edit_mark_cmd (edit, 1);
3851 break;
3852 case CK_Mark_Word:
3853 if (edit->column_highlight)
3854 edit_push_undo_action (edit, COLUMN_ON);
3855 edit->column_highlight = 0;
3856 edit_mark_current_word_cmd (edit);
3857 break;
3858 case CK_Mark_Line:
3859 if (edit->column_highlight)
3860 edit_push_undo_action (edit, COLUMN_ON);
3861 edit->column_highlight = 0;
3862 edit_mark_current_line_cmd (edit);
3863 break;
3864 case CK_Toggle_Line_State:
3865 option_line_state = !option_line_state;
3866 if (option_line_state)
3868 option_line_state_width = LINE_STATE_WIDTH;
3870 else
3872 option_line_state_width = 0;
3874 edit->force |= REDRAW_PAGE;
3875 break;
3877 case CK_Toggle_Show_Margin:
3878 show_right_margin = !show_right_margin;
3879 edit->force |= REDRAW_PAGE;
3880 break;
3882 case CK_Toggle_Bookmark:
3883 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3884 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3885 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3886 else
3887 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3888 break;
3889 case CK_Flush_Bookmarks:
3890 book_mark_flush (edit, BOOK_MARK_COLOR);
3891 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3892 edit->force |= REDRAW_PAGE;
3893 break;
3894 case CK_Next_Bookmark:
3895 if (edit->book_mark)
3897 struct _book_mark *p;
3898 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3899 if (p->next)
3901 p = p->next;
3902 if (p->line >= edit->start_line + edit->num_widget_lines
3903 || p->line < edit->start_line)
3904 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3905 edit_move_to_line (edit, p->line);
3908 break;
3909 case CK_Prev_Bookmark:
3910 if (edit->book_mark)
3912 struct _book_mark *p;
3913 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3914 while (p->line == edit->curs_line)
3915 if (p->prev)
3916 p = p->prev;
3917 if (p->line >= 0)
3919 if (p->line >= edit->start_line + edit->num_widget_lines
3920 || p->line < edit->start_line)
3921 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3922 edit_move_to_line (edit, p->line);
3925 break;
3927 case CK_Beginning_Of_Text:
3928 case CK_Beginning_Of_Text_Highlight:
3929 edit_move_to_top (edit);
3930 break;
3931 case CK_End_Of_Text:
3932 case CK_End_Of_Text_Highlight:
3933 edit_move_to_bottom (edit);
3934 break;
3936 case CK_Copy:
3937 if (option_cursor_beyond_eol && edit->over_col > 0)
3938 edit_insert_over (edit);
3939 edit_block_copy_cmd (edit);
3940 break;
3941 case CK_Remove:
3942 edit_block_delete_cmd (edit);
3943 break;
3944 case CK_Move:
3945 if (option_cursor_beyond_eol && edit->over_col > 0)
3946 edit_insert_over (edit);
3947 edit_block_move_cmd (edit);
3948 break;
3950 case CK_Shift_Block_Left:
3951 if (edit->mark1 != edit->mark2)
3952 edit_move_block_to_left (edit);
3953 break;
3954 case CK_Shift_Block_Right:
3955 if (edit->mark1 != edit->mark2)
3956 edit_move_block_to_right (edit);
3957 break;
3958 case CK_XStore:
3959 edit_copy_to_X_buf_cmd (edit);
3960 break;
3961 case CK_XCut:
3962 edit_cut_to_X_buf_cmd (edit);
3963 break;
3964 case CK_XPaste:
3965 /* if non persistent selection and text selected */
3966 if (!option_persistent_selections)
3968 if (edit->mark1 != edit->mark2)
3969 edit_block_delete_cmd (edit);
3971 if (option_cursor_beyond_eol && edit->over_col > 0)
3972 edit_insert_over (edit);
3973 edit_paste_from_X_buf_cmd (edit);
3974 break;
3975 case CK_Selection_History:
3976 edit_paste_from_history (edit);
3977 break;
3979 case CK_Save_As:
3980 edit_save_as_cmd (edit);
3981 break;
3982 case CK_Save:
3983 edit_save_confirm_cmd (edit);
3984 break;
3985 case CK_Load:
3986 edit_load_cmd (edit, EDIT_FILE_COMMON);
3987 break;
3988 case CK_Save_Block:
3989 edit_save_block_cmd (edit);
3990 break;
3991 case CK_Insert_File:
3992 edit_insert_file_cmd (edit);
3993 break;
3995 case CK_Load_Prev_File:
3996 edit_load_back_cmd (edit);
3997 break;
3998 case CK_Load_Next_File:
3999 edit_load_forward_cmd (edit);
4000 break;
4002 case CK_Load_Syntax_File:
4003 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4004 break;
4005 case CK_Choose_Syntax:
4006 edit_syntax_dialog (edit);
4007 break;
4009 case CK_Load_Menu_File:
4010 edit_load_cmd (edit, EDIT_FILE_MENU);
4011 break;
4013 case CK_Toggle_Syntax:
4014 option_syntax_highlighting ^= 1;
4015 if (option_syntax_highlighting == 1)
4016 edit_load_syntax (edit, NULL, edit->syntax_type);
4017 edit->force |= REDRAW_PAGE;
4018 break;
4020 case CK_Toggle_Tab_TWS:
4021 enable_show_tabs_tws ^= 1;
4022 edit->force |= REDRAW_PAGE;
4023 break;
4025 case CK_Find:
4026 edit_search_cmd (edit, FALSE);
4027 break;
4028 case CK_Find_Again:
4029 edit_search_cmd (edit, TRUE);
4030 break;
4031 case CK_Replace:
4032 edit_replace_cmd (edit, 0);
4033 break;
4034 case CK_Replace_Again:
4035 edit_replace_cmd (edit, 1);
4036 break;
4037 case CK_Complete_Word:
4038 /* if text marked shift block */
4039 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4041 edit_move_block_to_left (edit);
4043 else
4045 edit_complete_word_cmd (edit);
4047 break;
4048 case CK_Find_Definition:
4049 edit_get_match_keyword_cmd (edit);
4050 break;
4051 case CK_Quit:
4052 dlg_stop (edit->widget.owner);
4053 break;
4054 case CK_New:
4055 edit_new_cmd (edit);
4056 break;
4057 case CK_Help:
4058 edit_help_cmd (edit);
4059 break;
4060 case CK_Refresh:
4061 edit_refresh_cmd (edit);
4062 break;
4063 case CK_SaveSetupCmd:
4064 save_setup_cmd ();
4065 break;
4066 case CK_About:
4067 edit_about ();
4068 break;
4069 case CK_LearnKeys:
4070 learn_keys ();
4071 break;
4072 case CK_Edit_Options:
4073 edit_options_dialog (edit);
4074 break;
4075 case CK_Edit_Save_Mode:
4076 menu_save_mode_cmd ();
4077 break;
4078 case CK_Date:
4080 char s[BUF_MEDIUM];
4081 /* fool gcc to prevent a Y2K warning */
4082 char time_format[] = "_c";
4083 time_format[0] = '%';
4085 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4086 edit_print_string (edit, s);
4087 edit->force |= REDRAW_PAGE;
4088 break;
4090 break;
4091 case CK_Goto:
4092 edit_goto_cmd (edit);
4093 break;
4094 case CK_Paragraph_Format:
4095 format_paragraph (edit, 1);
4096 edit->force |= REDRAW_PAGE;
4097 break;
4098 case CK_Delete_Macro:
4099 edit_delete_macro_cmd (edit);
4100 break;
4101 case CK_Match_Bracket:
4102 edit_goto_matching_bracket (edit);
4103 break;
4104 case CK_User_Menu:
4105 user_menu (edit);
4106 break;
4107 case CK_Sort:
4108 edit_sort_cmd (edit);
4109 break;
4110 case CK_ExtCmd:
4111 edit_ext_cmd (edit);
4112 break;
4113 case CK_Mail:
4114 edit_mail_dialog (edit);
4115 break;
4116 case CK_Shell:
4117 view_other_cmd ();
4118 break;
4119 case CK_SelectCodepage:
4120 edit_select_codepage_cmd (edit);
4121 break;
4122 case CK_Insert_Literal:
4123 edit_insert_literal_cmd (edit);
4124 break;
4125 case CK_Execute_Macro:
4126 edit_execute_macro_cmd (edit);
4127 break;
4128 case CK_Begin_End_Macro:
4129 edit_begin_end_macro_cmd (edit);
4130 break;
4131 case CK_Begin_End_Repeat:
4132 edit_begin_end_repeat_cmd (edit);
4133 break;
4134 case CK_Ext_Mode:
4135 edit->extmod = 1;
4136 break;
4137 default:
4138 break;
4141 /* CK_Pipe_Block */
4142 if ((command / 10000) == 1) /* a shell command */
4143 edit_block_process_cmd (edit, shell_cmd[command - 10000], 1);
4145 /* keys which must set the col position, and the search vars */
4146 switch (command)
4148 case CK_Find:
4149 case CK_Find_Again:
4150 case CK_Replace:
4151 case CK_Replace_Again:
4152 case CK_Complete_Word:
4153 edit->prev_col = edit_get_col (edit);
4154 break;
4155 case CK_Up:
4156 case CK_Up_Highlight:
4157 case CK_Up_Alt_Highlight:
4158 case CK_Down:
4159 case CK_Down_Highlight:
4160 case CK_Down_Alt_Highlight:
4161 case CK_Page_Up:
4162 case CK_Page_Up_Highlight:
4163 case CK_Page_Up_Alt_Highlight:
4164 case CK_Page_Down:
4165 case CK_Page_Down_Highlight:
4166 case CK_Page_Down_Alt_Highlight:
4167 case CK_Beginning_Of_Text:
4168 case CK_Beginning_Of_Text_Highlight:
4169 case CK_End_Of_Text:
4170 case CK_End_Of_Text_Highlight:
4171 case CK_Paragraph_Up:
4172 case CK_Paragraph_Up_Highlight:
4173 case CK_Paragraph_Up_Alt_Highlight:
4174 case CK_Paragraph_Down:
4175 case CK_Paragraph_Down_Highlight:
4176 case CK_Paragraph_Down_Alt_Highlight:
4177 case CK_Scroll_Up:
4178 case CK_Scroll_Up_Highlight:
4179 case CK_Scroll_Up_Alt_Highlight:
4180 case CK_Scroll_Down:
4181 case CK_Scroll_Down_Highlight:
4182 case CK_Scroll_Down_Alt_Highlight:
4183 edit->search_start = edit->curs1;
4184 edit->found_len = 0;
4185 break;
4186 default:
4187 edit->found_len = 0;
4188 edit->prev_col = edit_get_col (edit);
4189 edit->search_start = edit->curs1;
4191 edit_find_bracket (edit);
4193 if (option_auto_para_formatting)
4195 switch (command)
4197 case CK_BackSpace:
4198 case CK_Delete:
4199 case CK_Delete_Word_Left:
4200 case CK_Delete_Word_Right:
4201 case CK_Delete_To_Line_End:
4202 case CK_Delete_To_Line_Begin:
4203 format_paragraph (edit, 0);
4204 edit->force |= REDRAW_PAGE;
4209 /* --------------------------------------------------------------------------------------------- */
4211 void
4212 edit_stack_init (void)
4214 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4216 edit_history_moveto[edit_stack_iterator].filename = NULL;
4217 edit_history_moveto[edit_stack_iterator].line = -1;
4220 edit_stack_iterator = 0;
4223 /* --------------------------------------------------------------------------------------------- */
4225 void
4226 edit_stack_free (void)
4228 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4229 g_free (edit_history_moveto[edit_stack_iterator].filename);
4232 /* --------------------------------------------------------------------------------------------- */
4233 /** move i lines */
4235 void
4236 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4238 edit_move_updown (edit, i, do_scroll, TRUE);
4241 /* --------------------------------------------------------------------------------------------- */
4242 /** move i lines */
4244 void
4245 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4247 edit_move_updown (edit, i, do_scroll, FALSE);
4250 /* --------------------------------------------------------------------------------------------- */
4252 unsigned int
4253 edit_unlock_file (WEdit * edit)
4255 char *fullpath;
4256 unsigned int ret;
4258 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4259 ret = unlock_file (fullpath);
4260 g_free (fullpath);
4262 return ret;
4265 /* --------------------------------------------------------------------------------------------- */
4267 unsigned int
4268 edit_lock_file (WEdit * edit)
4270 char *fullpath;
4271 unsigned int ret;
4273 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4274 ret = lock_file (fullpath);
4275 g_free (fullpath);
4277 return ret;
4280 /* --------------------------------------------------------------------------------------------- */