Fixup compile with --disable-charset.
[pantumic.git] / src / editor / edit.c
blob8551c8f91da7caa28f53cc646cbf67dc041e7356
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor low level data handling and cursor fundamentals
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
50 #include "lib/util.h" /* load_file_position(), save_file_position() */
51 #include "lib/timefmt.h" /* time formatting */
52 #include "lib/lock.h"
53 #include "lib/widget.h"
55 #ifdef HAVE_CHARSET
56 #include "lib/charsets.h" /* get_codepage_id */
57 #endif
59 #include "src/filemanager/cmd.h" /* view_other_cmd() */
60 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
62 #include "src/main.h" /* source_codepage */
63 #include "src/setup.h" /* option_tab_spacing */
64 #include "src/learn.h" /* learn_keys */
65 #include "src/keybind-defaults.h"
67 #include "edit-impl.h"
68 #include "edit-widget.h"
70 /*** global variables ****************************************************************************/
72 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
73 int option_typewriter_wrap = 0;
74 int option_auto_para_formatting = 0;
75 int option_fill_tabs_with_spaces = 0;
76 int option_return_does_auto_indent = 1;
77 int option_backspace_through_tabs = 0;
78 int option_fake_half_tabs = 1;
79 int option_save_mode = EDIT_QUICK_SAVE;
80 int option_save_position = 1;
81 int option_max_undo = 32768;
82 int option_persistent_selections = 1;
83 int option_cursor_beyond_eol = 0;
84 int option_line_state = 0;
85 int option_line_state_width = 0;
87 int option_edit_right_extreme = 0;
88 int option_edit_left_extreme = 0;
89 int option_edit_top_extreme = 0;
90 int option_edit_bottom_extreme = 0;
91 int enable_show_tabs_tws = 1;
92 int option_check_nl_at_eof = 0;
93 int option_group_undo = 0;
94 int show_right_margin = 0;
96 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
97 char *option_backup_ext = NULL;
99 int edit_stack_iterator = 0;
100 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
101 /* magic sequense for say than block is vertical */
102 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
104 /*** file scope macro definitions ****************************************************************/
106 #define TEMP_BUF_LEN 1024
108 #define space_width 1
110 /*** file scope type declarations ****************************************************************/
112 /*** file scope variables ************************************************************************/
114 /* detecting an error on save is easy: just check if every byte has been written. */
115 /* detecting an error on read, is not so easy 'cos there is not way to tell
116 whether you read everything or not. */
117 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
118 static const struct edit_filters
120 const char *read, *write, *extension;
121 } all_filters[] =
123 /* *INDENT-OFF* */
124 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
125 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
126 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
127 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
129 /* *INDENT-ON* */
132 static long last_bracket = -1;
134 static const char *const shell_cmd[] = SHELL_COMMANDS_i;
136 /*** file scope functions ************************************************************************/
137 /* --------------------------------------------------------------------------------------------- */
141 * here's a quick sketch of the layout: (don't run this through indent.)
143 * (b1 is buffers1 and b2 is buffers2)
146 * \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
147 * ______________________________________|______________________________________
149 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
150 * |-> |-> |-> |-> |-> |-> |
152 * _<------------------------->|<----------------->_
153 * WEdit->curs2 | WEdit->curs1
154 * ^ | ^
155 * | ^|^ |
156 * cursor ||| cursor
157 * |||
158 * file end|||file beginning
163 * This_is_some_file
164 * fin.
167 /* --------------------------------------------------------------------------------------------- */
169 static void user_menu (WEdit * edit);
170 static int left_of_four_spaces (WEdit * edit);
171 static inline void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
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);
560 edit->macro_i = -1; /* not recording a macro */
563 /* --------------------------------------------------------------------------------------------- */
565 static void
566 edit_set_keymap (void)
568 editor_map = default_editor_keymap;
569 if (editor_keymap && editor_keymap->len > 0)
570 editor_map = (global_keymap_t *) editor_keymap->data;
572 editor_x_map = default_editor_x_keymap;
573 if (editor_x_keymap && editor_x_keymap->len > 0)
574 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
577 /* --------------------------------------------------------------------------------------------- */
579 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
580 then the file should be as it was when he loaded up. Then set edit->modified to 0.
583 static long
584 edit_pop_undo_action (WEdit * edit)
586 long c;
587 unsigned long sp = edit->undo_stack_pointer;
589 if (sp == edit->undo_stack_bottom)
590 return STACK_BOTTOM;
592 sp = (sp - 1) & edit->undo_stack_size_mask;
593 c = edit->undo_stack[sp];
594 if (c >= 0)
596 /* edit->undo_stack[sp] = '@'; */
597 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
598 return c;
601 if (sp == edit->undo_stack_bottom)
602 return STACK_BOTTOM;
604 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
605 if (edit->undo_stack[sp] == -2)
607 /* edit->undo_stack[sp] = '@'; */
608 edit->undo_stack_pointer = sp;
610 else
611 edit->undo_stack[sp]++;
613 return c;
616 static long
617 edit_pop_redo_action (WEdit * edit)
619 long c;
620 unsigned long sp = edit->redo_stack_pointer;
622 if (sp == edit->redo_stack_bottom)
623 return STACK_BOTTOM;
625 sp = (sp - 1) & edit->redo_stack_size_mask;
626 c = edit->redo_stack[sp];
627 if (c >= 0)
629 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
630 return c;
633 if (sp == edit->redo_stack_bottom)
634 return STACK_BOTTOM;
636 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
637 if (edit->redo_stack[sp] == -2)
638 edit->redo_stack_pointer = sp;
639 else
640 edit->redo_stack[sp]++;
642 return c;
645 static long
646 get_prev_undo_action (WEdit * edit)
648 long c;
649 unsigned long sp = edit->undo_stack_pointer;
651 if (sp == edit->undo_stack_bottom)
652 return STACK_BOTTOM;
654 sp = (sp - 1) & edit->undo_stack_size_mask;
655 c = edit->undo_stack[sp];
656 if (c >= 0)
657 return c;
659 if (sp == edit->undo_stack_bottom)
660 return STACK_BOTTOM;
662 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
663 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 (!(my_type_of (c1) & my_type_of (c2)))
1059 break;
1060 if (isspace (c1) && !isspace (c2))
1061 break;
1062 if (s)
1063 if (!isspace (c1) && isspace (c2))
1064 break;
1068 /* --------------------------------------------------------------------------------------------- */
1070 static void
1071 edit_left_word_move_cmd (WEdit * edit)
1073 edit_left_word_move (edit, 0);
1074 edit->force |= REDRAW_PAGE;
1077 /* --------------------------------------------------------------------------------------------- */
1079 static void
1080 edit_right_word_move (WEdit * edit, int s)
1082 for (;;)
1084 int c1, c2;
1085 if (edit->column_highlight
1086 && edit->mark1 != edit->mark2
1087 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1088 break;
1089 edit_cursor_move (edit, 1);
1090 if (edit->curs1 >= edit->last_byte)
1091 break;
1092 c1 = edit_get_byte (edit, edit->curs1 - 1);
1093 c2 = edit_get_byte (edit, edit->curs1);
1094 if (!(my_type_of (c1) & my_type_of (c2)))
1095 break;
1096 if (isspace (c1) && !isspace (c2))
1097 break;
1098 if (s)
1099 if (!isspace (c1) && isspace (c2))
1100 break;
1104 /* --------------------------------------------------------------------------------------------- */
1106 static void
1107 edit_right_word_move_cmd (WEdit * edit)
1109 edit_right_word_move (edit, 0);
1110 edit->force |= REDRAW_PAGE;
1113 /* --------------------------------------------------------------------------------------------- */
1115 static void
1116 edit_right_char_move_cmd (WEdit * edit)
1118 int cw = 1;
1119 int c = 0;
1120 if (edit->utf8)
1122 c = edit_get_utf (edit, edit->curs1, &cw);
1123 if (cw < 1)
1124 cw = 1;
1126 else
1128 c = edit_get_byte (edit, edit->curs1);
1130 if (option_cursor_beyond_eol && c == '\n')
1132 edit->over_col++;
1134 else
1136 edit_cursor_move (edit, cw);
1140 /* --------------------------------------------------------------------------------------------- */
1142 static void
1143 edit_left_char_move_cmd (WEdit * edit)
1145 int cw = 1;
1146 if (edit->column_highlight
1147 && option_cursor_beyond_eol
1148 && edit->mark1 != edit->mark2
1149 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1150 return;
1151 if (edit->utf8)
1153 edit_get_prev_utf (edit, edit->curs1, &cw);
1154 if (cw < 1)
1155 cw = 1;
1157 if (option_cursor_beyond_eol && edit->over_col > 0)
1159 edit->over_col--;
1161 else
1163 edit_cursor_move (edit, -cw);
1167 /* --------------------------------------------------------------------------------------------- */
1168 /** Up or down cursor moving.
1169 direction = TRUE - move up
1170 = FALSE - move down
1173 static void
1174 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1176 unsigned long p;
1177 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1179 if (i > l)
1180 i = l;
1182 if (i == 0)
1183 return;
1185 if (i > 1)
1186 edit->force |= REDRAW_PAGE;
1187 if (do_scroll)
1189 if (direction)
1190 edit_scroll_upward (edit, i);
1191 else
1192 edit_scroll_downward (edit, i);
1194 p = edit_bol (edit, edit->curs1);
1196 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1198 edit_cursor_move (edit, p - edit->curs1);
1200 edit_move_to_prev_col (edit, p);
1202 /* search start of current multibyte char (like CJK) */
1203 if (edit->curs1 + 1 < edit->last_byte)
1205 edit_right_char_move_cmd (edit);
1206 edit_left_char_move_cmd (edit);
1209 edit->search_start = edit->curs1;
1210 edit->found_len = 0;
1213 /* --------------------------------------------------------------------------------------------- */
1215 static void
1216 edit_right_delete_word (WEdit * edit)
1218 int c1, c2;
1219 for (;;)
1221 if (edit->curs1 >= edit->last_byte)
1222 break;
1223 c1 = edit_delete (edit, 1);
1224 c2 = edit_get_byte (edit, edit->curs1);
1225 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1226 break;
1227 if (!(my_type_of (c1) & my_type_of (c2)))
1228 break;
1232 /* --------------------------------------------------------------------------------------------- */
1234 static void
1235 edit_left_delete_word (WEdit * edit)
1237 int c1, c2;
1238 for (;;)
1240 if (edit->curs1 <= 0)
1241 break;
1242 c1 = edit_backspace (edit, 1);
1243 c2 = edit_get_byte (edit, edit->curs1 - 1);
1244 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1245 break;
1246 if (!(my_type_of (c1) & my_type_of (c2)))
1247 break;
1251 /* --------------------------------------------------------------------------------------------- */
1253 the start column position is not recorded, and hence does not
1254 undo as it happed. But who would notice.
1257 static void
1258 edit_do_undo (WEdit * edit)
1260 long ac;
1261 long count = 0;
1263 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1264 edit->over_col = 0;
1265 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1267 switch ((int) ac)
1269 case STACK_BOTTOM:
1270 goto done_undo;
1271 case CURS_RIGHT:
1272 edit_cursor_move (edit, 1);
1273 break;
1274 case CURS_LEFT:
1275 edit_cursor_move (edit, -1);
1276 break;
1277 case BACKSPACE:
1278 case BACKSPACE_BR:
1279 edit_backspace (edit, 1);
1280 break;
1281 case DELCHAR:
1282 case DELCHAR_BR:
1283 edit_delete (edit, 1);
1284 break;
1285 case COLUMN_ON:
1286 edit->column_highlight = 1;
1287 break;
1288 case COLUMN_OFF:
1289 edit->column_highlight = 0;
1290 break;
1292 if (ac >= 256 && ac < 512)
1293 edit_insert_ahead (edit, ac - 256);
1294 if (ac >= 0 && ac < 256)
1295 edit_insert (edit, ac);
1297 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1299 edit->mark1 = ac - MARK_1;
1300 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1302 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1304 edit->mark2 = ac - MARK_2;
1305 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1307 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1309 edit->end_mark_curs = ac - MARK_CURS;
1311 if (count++)
1312 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1315 if (edit->start_display > ac - KEY_PRESS)
1317 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1318 edit->force |= REDRAW_PAGE;
1320 else if (edit->start_display < ac - KEY_PRESS)
1322 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1323 edit->force |= REDRAW_PAGE;
1325 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1326 edit_update_curs_row (edit);
1328 done_undo:;
1329 edit->undo_stack_disable = 0;
1332 static void
1333 edit_do_redo (WEdit * edit)
1335 long ac;
1336 long count = 0;
1338 if (edit->redo_stack_reset)
1339 return;
1341 edit->over_col = 0;
1342 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1344 switch ((int) ac)
1346 case STACK_BOTTOM:
1347 goto done_redo;
1348 case CURS_RIGHT:
1349 edit_cursor_move (edit, 1);
1350 break;
1351 case CURS_LEFT:
1352 edit_cursor_move (edit, -1);
1353 break;
1354 case BACKSPACE:
1355 edit_backspace (edit, 1);
1356 break;
1357 case DELCHAR:
1358 edit_delete (edit, 1);
1359 break;
1360 case COLUMN_ON:
1361 edit->column_highlight = 1;
1362 break;
1363 case COLUMN_OFF:
1364 edit->column_highlight = 0;
1365 break;
1367 if (ac >= 256 && ac < 512)
1368 edit_insert_ahead (edit, ac - 256);
1369 if (ac >= 0 && ac < 256)
1370 edit_insert (edit, ac);
1372 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1374 edit->mark1 = ac - MARK_1;
1375 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1377 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1379 edit->mark2 = ac - MARK_2;
1380 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1382 /* more than one pop usually means something big */
1383 if (count++)
1384 edit->force |= REDRAW_PAGE;
1387 if (edit->start_display > ac - KEY_PRESS)
1389 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1390 edit->force |= REDRAW_PAGE;
1392 else if (edit->start_display < ac - KEY_PRESS)
1394 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1395 edit->force |= REDRAW_PAGE;
1397 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1398 edit_update_curs_row (edit);
1400 done_redo:;
1403 static void
1404 edit_group_undo (WEdit * edit)
1406 long ac = KEY_PRESS;
1407 long cur_ac = KEY_PRESS;
1408 while (ac != STACK_BOTTOM && ac == cur_ac)
1410 cur_ac = get_prev_undo_action (edit);
1411 edit_do_undo (edit);
1412 ac = get_prev_undo_action (edit);
1413 /* exit from cycle if option_group_undo is not set,
1414 * and make single UNDO operation
1416 if (!option_group_undo)
1417 ac = STACK_BOTTOM;
1421 /* --------------------------------------------------------------------------------------------- */
1423 static void
1424 edit_delete_to_line_end (WEdit * edit)
1426 while (edit_get_byte (edit, edit->curs1) != '\n')
1428 if (!edit->curs2)
1429 break;
1430 edit_delete (edit, 1);
1434 /* --------------------------------------------------------------------------------------------- */
1436 static void
1437 edit_delete_to_line_begin (WEdit * edit)
1439 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1441 if (!edit->curs1)
1442 break;
1443 edit_backspace (edit, 1);
1447 /* --------------------------------------------------------------------------------------------- */
1449 static int
1450 is_aligned_on_a_tab (WEdit * edit)
1452 edit_update_curs_col (edit);
1453 return !((edit->curs_col % (TAB_SIZE * space_width))
1454 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1457 /* --------------------------------------------------------------------------------------------- */
1459 static int
1460 right_of_four_spaces (WEdit * edit)
1462 int i, ch = 0;
1463 for (i = 1; i <= HALF_TAB_SIZE; i++)
1464 ch |= edit_get_byte (edit, edit->curs1 - i);
1465 if (ch == ' ')
1466 return is_aligned_on_a_tab (edit);
1467 return 0;
1470 /* --------------------------------------------------------------------------------------------- */
1472 static int
1473 left_of_four_spaces (WEdit * edit)
1475 int i, ch = 0;
1476 for (i = 0; i < HALF_TAB_SIZE; i++)
1477 ch |= edit_get_byte (edit, edit->curs1 + i);
1478 if (ch == ' ')
1479 return is_aligned_on_a_tab (edit);
1480 return 0;
1483 /* --------------------------------------------------------------------------------------------- */
1485 static void
1486 edit_auto_indent (WEdit * edit)
1488 long p;
1489 char c;
1490 p = edit->curs1;
1491 /* use the previous line as a template */
1492 p = edit_move_backward (edit, p, 1);
1493 /* copy the leading whitespace of the line */
1494 for (;;)
1495 { /* no range check - the line _is_ \n-terminated */
1496 c = edit_get_byte (edit, p++);
1497 if (c != ' ' && c != '\t')
1498 break;
1499 edit_insert (edit, c);
1503 /* --------------------------------------------------------------------------------------------- */
1505 static inline void
1506 edit_double_newline (WEdit * edit)
1508 edit_insert (edit, '\n');
1509 if (edit_get_byte (edit, edit->curs1) == '\n')
1510 return;
1511 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1512 return;
1513 edit->force |= REDRAW_PAGE;
1514 edit_insert (edit, '\n');
1517 /* --------------------------------------------------------------------------------------------- */
1519 static inline void
1520 edit_tab_cmd (WEdit * edit)
1522 int i;
1524 if (option_fake_half_tabs)
1526 if (is_in_indent (edit))
1528 /*insert a half tab (usually four spaces) unless there is a
1529 half tab already behind, then delete it and insert a
1530 full tab. */
1531 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
1533 for (i = 1; i <= HALF_TAB_SIZE; i++)
1534 edit_backspace (edit, 1);
1535 edit_insert (edit, '\t');
1537 else
1539 insert_spaces_tab (edit, 1);
1541 return;
1544 if (option_fill_tabs_with_spaces)
1546 insert_spaces_tab (edit, 0);
1548 else
1550 edit_insert (edit, '\t');
1554 /* --------------------------------------------------------------------------------------------- */
1556 static void
1557 check_and_wrap_line (WEdit * edit)
1559 int curs, c;
1560 if (!option_typewriter_wrap)
1561 return;
1562 edit_update_curs_col (edit);
1563 if (edit->curs_col < option_word_wrap_line_length)
1564 return;
1565 curs = edit->curs1;
1566 for (;;)
1568 curs--;
1569 c = edit_get_byte (edit, curs);
1570 if (c == '\n' || curs <= 0)
1572 edit_insert (edit, '\n');
1573 return;
1575 if (c == ' ' || c == '\t')
1577 int current = edit->curs1;
1578 edit_cursor_move (edit, curs - edit->curs1 + 1);
1579 edit_insert (edit, '\n');
1580 edit_cursor_move (edit, current - edit->curs1 + 1);
1581 return;
1586 /* --------------------------------------------------------------------------------------------- */
1587 /** this find the matching bracket in either direction, and sets edit->bracket */
1589 static long
1590 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1592 const char *const b = "{}{[][()(", *p;
1593 int i = 1, a, inc = -1, c, d, n = 0;
1594 unsigned long j = 0;
1595 long q;
1596 edit_update_curs_row (edit);
1597 c = edit_get_byte (edit, edit->curs1);
1598 p = strchr (b, c);
1599 /* no limit */
1600 if (!furthest_bracket_search)
1601 furthest_bracket_search--;
1602 /* not on a bracket at all */
1603 if (!p)
1604 return -1;
1605 /* the matching bracket */
1606 d = p[1];
1607 /* going left or right? */
1608 if (strchr ("{[(", c))
1609 inc = 1;
1610 for (q = edit->curs1 + inc;; q += inc)
1612 /* out of buffer? */
1613 if (q >= edit->last_byte || q < 0)
1614 break;
1615 a = edit_get_byte (edit, q);
1616 /* don't want to eat CPU */
1617 if (j++ > furthest_bracket_search)
1618 break;
1619 /* out of screen? */
1620 if (in_screen)
1622 if (q < edit->start_display)
1623 break;
1624 /* count lines if searching downward */
1625 if (inc > 0 && a == '\n')
1626 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
1627 break;
1629 /* count bracket depth */
1630 i += (a == c) - (a == d);
1631 /* return if bracket depth is zero */
1632 if (!i)
1633 return q;
1635 /* no match */
1636 return -1;
1639 /* --------------------------------------------------------------------------------------------- */
1641 static inline void
1642 edit_goto_matching_bracket (WEdit * edit)
1644 long q;
1646 q = edit_get_bracket (edit, 0, 0);
1647 if (q >= 0)
1649 edit->bracket = edit->curs1;
1650 edit->force |= REDRAW_PAGE;
1651 edit_cursor_move (edit, q - edit->curs1);
1655 /* --------------------------------------------------------------------------------------------- */
1657 static void
1658 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
1660 int i = 0;
1662 if (edit->macro_depth++ > 256)
1664 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
1665 edit->macro_depth--;
1666 return;
1668 edit->force |= REDRAW_PAGE;
1669 for (; i < n; i++)
1671 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
1673 edit_update_screen (edit);
1674 edit->macro_depth--;
1677 /* --------------------------------------------------------------------------------------------- */
1678 /** User edit menu, like user menu (F2) but only in editor. */
1680 static void
1681 user_menu (WEdit * edit)
1683 char *block_file;
1684 int nomark;
1685 long start_mark, end_mark;
1686 struct stat status;
1688 block_file = concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE);
1690 nomark = eval_marks (edit, &start_mark, &end_mark);
1691 if (nomark == 0)
1692 edit_save_block (edit, block_file, start_mark, end_mark);
1694 /* run shell scripts from menu */
1695 if (user_menu_cmd (edit) && (mc_stat (block_file, &status) == 0) && (status.st_size != 0))
1697 int rc = 0;
1698 FILE *fd;
1700 /* i.e. we have marked block */
1701 if (nomark == 0)
1702 rc = edit_block_delete_cmd (edit);
1704 if (rc == 0)
1706 long ins_len;
1708 ins_len = edit_insert_file (edit, block_file);
1709 if (nomark == 0 && ins_len > 0)
1710 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1712 /* truncate block file */
1713 fd = fopen (block_file, "w");
1714 if (fd != NULL)
1715 fclose (fd);
1717 edit_refresh_cmd (edit);
1718 edit->force |= REDRAW_COMPLETELY;
1720 g_free (block_file);
1723 /* --------------------------------------------------------------------------------------------- */
1724 /*** public functions ****************************************************************************/
1725 /* --------------------------------------------------------------------------------------------- */
1728 edit_get_byte (WEdit * edit, long byte_index)
1730 unsigned long p;
1731 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1732 return '\n';
1734 if (byte_index >= edit->curs1)
1736 p = edit->curs1 + edit->curs2 - byte_index - 1;
1737 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1739 else
1741 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1745 /* --------------------------------------------------------------------------------------------- */
1747 char *
1748 edit_get_byte_ptr (WEdit * edit, long byte_index)
1750 unsigned long p;
1751 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1752 return NULL;
1754 if (byte_index >= edit->curs1)
1756 p = edit->curs1 + edit->curs2 - byte_index - 1;
1757 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1758 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1760 else
1762 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
1763 (byte_index & M_EDIT_BUF_SIZE));
1767 /* --------------------------------------------------------------------------------------------- */
1769 char *
1770 edit_get_buf_ptr (WEdit * edit, long byte_index)
1772 unsigned long p;
1774 if (byte_index >= (edit->curs1 + edit->curs2))
1775 byte_index--;
1777 if (byte_index < 0)
1778 return NULL;
1780 if (byte_index >= edit->curs1)
1782 p = edit->curs1 + edit->curs2 - 1;
1783 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1784 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1786 else
1788 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
1792 /* --------------------------------------------------------------------------------------------- */
1795 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1797 gchar *str = NULL;
1798 int res = -1;
1799 gunichar ch;
1800 gchar *next_ch = NULL;
1801 int width = 0;
1803 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1805 *char_width = 0;
1806 return '\n';
1809 str = edit_get_byte_ptr (edit, byte_index);
1811 if (str == NULL)
1813 *char_width = 0;
1814 return 0;
1817 res = g_utf8_get_char_validated (str, -1);
1819 if (res < 0)
1821 ch = *str;
1822 width = 0;
1824 else
1826 ch = res;
1827 /* Calculate UTF-8 char width */
1828 next_ch = g_utf8_next_char (str);
1829 if (next_ch)
1831 width = next_ch - str;
1833 else
1835 ch = 0;
1836 width = 0;
1839 *char_width = width;
1840 return ch;
1843 /* --------------------------------------------------------------------------------------------- */
1846 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
1848 gchar *str, *buf = NULL;
1849 int res = -1;
1850 gunichar ch;
1851 gchar *next_ch = NULL;
1852 int width = 0;
1854 if (byte_index > 0)
1855 byte_index--;
1857 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1859 *char_width = 0;
1860 return 0;
1863 ch = edit_get_utf (edit, byte_index, &width);
1865 if (width == 1)
1867 *char_width = width;
1868 return ch;
1871 str = edit_get_byte_ptr (edit, byte_index);
1872 buf = edit_get_buf_ptr (edit, byte_index);
1873 if (str == NULL || buf == NULL)
1875 *char_width = 0;
1876 return 0;
1878 /* get prev utf8 char */
1879 if (str != buf)
1880 str = g_utf8_find_prev_char (buf, str);
1882 if (str == NULL)
1884 *char_width = 0;
1885 return 0;
1887 else
1888 res = g_utf8_get_char_validated (str, -1);
1890 if (res < 0)
1892 ch = *str;
1893 width = 0;
1895 else
1897 ch = res;
1898 /* Calculate UTF-8 char width */
1899 next_ch = g_utf8_next_char (str);
1900 if (next_ch)
1902 width = next_ch - str;
1904 else
1906 ch = 0;
1907 width = 0;
1910 *char_width = width;
1911 return ch;
1914 /* --------------------------------------------------------------------------------------------- */
1916 char *
1917 edit_get_write_filter (const char *write_name, const char *filename)
1919 int i;
1920 char *p, *writename;
1922 i = edit_find_filter (filename);
1923 if (i < 0)
1924 return NULL;
1926 writename = name_quote (write_name, 0);
1927 p = g_strdup_printf (all_filters[i].write, writename);
1928 g_free (writename);
1929 return p;
1932 /* --------------------------------------------------------------------------------------------- */
1934 long
1935 edit_write_stream (WEdit * edit, FILE * f)
1937 long i;
1939 if (edit->lb == LB_ASIS)
1941 for (i = 0; i < edit->last_byte; i++)
1942 if (fputc (edit_get_byte (edit, i), f) < 0)
1943 break;
1944 return i;
1947 /* change line breaks */
1948 for (i = 0; i < edit->last_byte; i++)
1950 unsigned char c = edit_get_byte (edit, i);
1952 if (!(c == '\n' || c == '\r'))
1954 /* not line break */
1955 if (fputc (c, f) < 0)
1956 return i;
1958 else
1959 { /* (c == '\n' || c == '\r') */
1960 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1962 switch (edit->lb)
1964 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1965 /* put one line break unconditionally */
1966 if (fputc ('\n', f) < 0)
1967 return i;
1969 i++; /* 2 chars are processed */
1971 if (c == '\r' && c1 == '\n')
1972 /* Windows line break; go to the next char */
1973 break;
1975 if (c == '\r' && c1 == '\r')
1977 /* two Macintosh line breaks; put second line break */
1978 if (fputc ('\n', f) < 0)
1979 return i;
1980 break;
1983 if (fputc (c1, f) < 0)
1984 return i;
1985 break;
1987 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1988 /* put one line break unconditionally */
1989 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1990 return i;
1992 if (c == '\r' && c1 == '\n')
1993 /* Windows line break; go to the next char */
1994 i++;
1995 break;
1997 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1998 /* put one line break unconditionally */
1999 if (fputc ('\r', f) < 0)
2000 return i;
2002 i++; /* 2 chars are processed */
2004 if (c == '\r' && c1 == '\n')
2005 /* Windows line break; go to the next char */
2006 break;
2008 if (c == '\n' && c1 == '\n')
2010 /* two Windows line breaks; put second line break */
2011 if (fputc ('\r', f) < 0)
2012 return i;
2013 break;
2016 if (fputc (c1, f) < 0)
2017 return i;
2018 break;
2019 case LB_ASIS: /* default without changes */
2020 break;
2025 return edit->last_byte;
2028 /* --------------------------------------------------------------------------------------------- */
2029 /** inserts a file at the cursor, returns count of inserted bytes on success */
2030 long
2031 edit_insert_file (WEdit * edit, const char *filename)
2033 char *p;
2034 long ins_len = 0;
2036 p = edit_get_filter (filename);
2037 if (p != NULL)
2039 FILE *f;
2040 long current = edit->curs1;
2041 f = (FILE *) popen (p, "r");
2042 if (f != NULL)
2044 edit_insert_stream (edit, f);
2045 ins_len = edit->curs1 - current;
2046 edit_cursor_move (edit, current - edit->curs1);
2047 if (pclose (f) > 0)
2049 char *errmsg;
2050 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2051 edit_error_dialog (_("Error"), errmsg);
2052 g_free (errmsg);
2053 g_free (p);
2054 return 0;
2057 else
2059 char *errmsg;
2060 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2061 edit_error_dialog (_("Error"), errmsg);
2062 g_free (errmsg);
2063 g_free (p);
2064 return 0;
2066 g_free (p);
2068 else
2070 int i, file, blocklen;
2071 long current = edit->curs1;
2072 int vertical_insertion = 0;
2073 char *buf;
2074 file = mc_open (filename, O_RDONLY | O_BINARY);
2075 if (file == -1)
2076 return 0;
2077 buf = g_malloc0 (TEMP_BUF_LEN);
2078 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2079 if (blocklen > 0)
2081 /* if contain signature VERTICAL_MAGIC then it vertical block */
2082 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2083 vertical_insertion = 1;
2084 else
2085 mc_lseek (file, 0, SEEK_SET);
2087 if (vertical_insertion)
2089 blocklen = edit_insert_column_of_text_from_file (edit, file);
2091 else
2093 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2095 for (i = 0; i < blocklen; i++)
2096 edit_insert (edit, buf[i]);
2099 ins_len = edit->curs1 - current;
2100 edit_cursor_move (edit, current - edit->curs1);
2101 g_free (buf);
2102 mc_close (file);
2103 if (blocklen != 0)
2104 return 0;
2106 return ins_len;
2109 /* --------------------------------------------------------------------------------------------- */
2111 * Fill in the edit structure. Return NULL on failure. Pass edit as
2112 * NULL to allocate a new structure.
2114 * If line is 0, try to restore saved position. Otherwise put the
2115 * cursor on that line and show it in the middle of the screen.
2118 WEdit *
2119 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
2121 int to_free = 0;
2122 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2123 if (option_line_state)
2125 option_line_state_width = LINE_STATE_WIDTH;
2127 else
2129 option_line_state_width = 0;
2131 if (!edit)
2133 #ifdef ENABLE_NLS
2135 * Expand option_whole_chars_search by national letters using
2136 * current locale
2139 static char option_whole_chars_search_buf[256];
2141 if (option_whole_chars_search_buf != option_whole_chars_search)
2143 size_t i;
2144 size_t len = str_term_width1 (option_whole_chars_search);
2146 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2148 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2150 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2152 option_whole_chars_search_buf[len++] = i;
2156 option_whole_chars_search_buf[len] = 0;
2157 option_whole_chars_search = option_whole_chars_search_buf;
2159 #endif /* ENABLE_NLS */
2160 edit = g_malloc0 (sizeof (WEdit));
2161 edit->search = NULL;
2162 to_free = 1;
2164 edit_purge_widget (edit);
2165 edit->num_widget_lines = lines;
2166 edit->over_col = 0;
2167 edit->num_widget_columns = columns;
2168 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2169 edit->stat1.st_uid = getuid ();
2170 edit->stat1.st_gid = getgid ();
2171 edit->stat1.st_mtime = 0;
2172 edit->bracket = -1;
2173 edit->force |= REDRAW_PAGE;
2174 edit_set_filename (edit, filename);
2175 edit->undo_stack_size = START_STACK_SIZE;
2176 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2177 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2179 edit->redo_stack_size = START_STACK_SIZE;
2180 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2181 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2183 edit->utf8 = 0;
2184 edit->converter = str_cnv_from_term;
2185 edit_set_codeset (edit);
2187 if (edit_load_file (edit))
2189 /* edit_load_file already gives an error message */
2190 if (to_free)
2191 g_free (edit);
2192 return 0;
2195 edit->loading_done = 1;
2196 edit->modified = 0;
2197 edit->locked = 0;
2198 edit_load_syntax (edit, NULL, NULL);
2200 int color;
2201 edit_get_syntax_color (edit, -1, &color);
2204 /* load saved cursor position */
2205 if ((line == 0) && option_save_position)
2207 edit_load_position (edit);
2209 else
2211 if (line <= 0)
2212 line = 1;
2213 edit_move_display (edit, line - 1);
2214 edit_move_to_line (edit, line - 1);
2217 edit_set_keymap ();
2219 return edit;
2222 /* --------------------------------------------------------------------------------------------- */
2223 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2226 edit_clean (WEdit * edit)
2228 int j = 0;
2230 if (!edit)
2231 return 0;
2233 /* a stale lock, remove it */
2234 if (edit->locked)
2235 edit->locked = edit_unlock_file (edit);
2237 /* save cursor position */
2238 if (option_save_position)
2239 edit_save_position (edit);
2240 else if (edit->serialized_bookmarks != NULL)
2241 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2243 /* File specified on the mcedit command line and never saved */
2244 if (edit->delete_file)
2245 unlink (edit->filename);
2247 edit_free_syntax_rules (edit);
2248 book_mark_flush (edit, -1);
2249 for (; j <= MAXBUFF; j++)
2251 g_free (edit->buffers1[j]);
2252 g_free (edit->buffers2[j]);
2255 g_free (edit->undo_stack);
2256 g_free (edit->redo_stack);
2257 g_free (edit->filename);
2258 g_free (edit->dir);
2260 mc_search_free (edit->search);
2261 edit->search = NULL;
2263 if (edit->converter != str_cnv_from_term)
2264 str_close_conv (edit->converter);
2266 edit_purge_widget (edit);
2268 return 1;
2271 /* --------------------------------------------------------------------------------------------- */
2272 /** returns 1 on success */
2275 edit_renew (WEdit * edit)
2277 int lines = edit->num_widget_lines;
2278 int columns = edit->num_widget_columns;
2280 edit_clean (edit);
2281 return (edit_init (edit, lines, columns, "", 0) != NULL);
2284 /* --------------------------------------------------------------------------------------------- */
2286 * Load a new file into the editor. If it fails, preserve the old file.
2287 * To do it, allocate a new widget, initialize it and, if the new file
2288 * was loaded, copy the data to the old widget.
2289 * Return 1 on success, 0 on failure.
2293 edit_reload (WEdit * edit, const char *filename)
2295 WEdit *e;
2296 int lines = edit->num_widget_lines;
2297 int columns = edit->num_widget_columns;
2299 e = g_malloc0 (sizeof (WEdit));
2300 e->widget = edit->widget;
2301 if (!edit_init (e, lines, columns, filename, 0))
2303 g_free (e);
2304 return 0;
2306 edit_clean (edit);
2307 memcpy (edit, e, sizeof (WEdit));
2308 g_free (e);
2309 return 1;
2312 /* --------------------------------------------------------------------------------------------- */
2314 * Load a new file into the editor and set line. If it fails, preserve the old file.
2315 * To do it, allocate a new widget, initialize it and, if the new file
2316 * was loaded, copy the data to the old widget.
2317 * Return 1 on success, 0 on failure.
2321 edit_reload_line (WEdit * edit, const char *filename, long line)
2323 WEdit *e;
2324 int lines = edit->num_widget_lines;
2325 int columns = edit->num_widget_columns;
2327 e = g_malloc0 (sizeof (WEdit));
2328 e->widget = edit->widget;
2329 if (!edit_init (e, lines, columns, filename, line))
2331 g_free (e);
2332 return 0;
2334 edit_clean (edit);
2335 memcpy (edit, e, sizeof (WEdit));
2336 g_free (e);
2337 return 1;
2340 /* --------------------------------------------------------------------------------------------- */
2342 void
2343 edit_set_codeset (WEdit * edit)
2345 #ifdef HAVE_CHARSET
2346 const char *cp_id;
2348 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
2350 if (cp_id != NULL)
2352 GIConv conv;
2353 conv = str_crt_conv_from (cp_id);
2354 if (conv != INVALID_CONV)
2356 if (edit->converter != str_cnv_from_term)
2357 str_close_conv (edit->converter);
2358 edit->converter = conv;
2362 if (cp_id != NULL)
2363 edit->utf8 = str_isutf8 (cp_id);
2364 #else
2365 (void) edit;
2366 #endif
2370 /* --------------------------------------------------------------------------------------------- */
2372 Recording stack for undo:
2373 The following is an implementation of a compressed stack. Identical
2374 pushes are recorded by a negative prefix indicating the number of times the
2375 same char was pushed. This saves space for repeated curs-left or curs-right
2376 delete etc.
2380 pushed: stored:
2384 b -3
2386 c --> -4
2392 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2393 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2394 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2395 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2396 position.
2398 The only way the cursor moves or the buffer is changed is through the routines:
2399 insert, backspace, insert_ahead, delete, and cursor_move.
2400 These record the reverse undo movements onto the stack each time they are
2401 called.
2403 Each key press results in a set of actions (insert; delete ...). So each time
2404 a key is pressed the current position of start_display is pushed as
2405 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2406 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2407 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2411 void
2412 edit_push_undo_action (WEdit * edit, long c, ...)
2414 unsigned long sp = edit->undo_stack_pointer;
2415 unsigned long spm1;
2416 long *t;
2418 /* first enlarge the stack if necessary */
2419 if (sp > edit->undo_stack_size - 10)
2420 { /* say */
2421 if (option_max_undo < 256)
2422 option_max_undo = 256;
2423 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2425 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2426 if (t)
2428 edit->undo_stack = t;
2429 edit->undo_stack_size <<= 1;
2430 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2434 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2435 if (edit->undo_stack_disable)
2437 edit_push_redo_action (edit, KEY_PRESS);
2438 edit_push_redo_action (edit, c);
2439 return;
2441 else if (edit->redo_stack_reset)
2443 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2446 if (edit->undo_stack_bottom != sp
2447 && spm1 != edit->undo_stack_bottom
2448 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2450 int d;
2451 if (edit->undo_stack[spm1] < 0)
2453 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2454 if (d == c)
2456 if (edit->undo_stack[spm1] > -1000000000)
2458 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2460 edit->undo_stack[spm1]--;
2462 return;
2466 else
2468 d = edit->undo_stack[spm1];
2469 if (d == c)
2471 if (c >= KEY_PRESS)
2472 return; /* --> no need to push multiple do-nothings */
2473 edit->undo_stack[sp] = -2;
2474 goto check_bottom;
2478 edit->undo_stack[sp] = c;
2480 check_bottom:
2481 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2483 /* if the sp wraps round and catches the undo_stack_bottom then erase
2484 * the first set of actions on the stack to make space - by moving
2485 * undo_stack_bottom forward one "key press" */
2486 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2487 if ((unsigned long) c == edit->undo_stack_bottom ||
2488 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2491 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2493 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2494 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2496 /*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: */
2497 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2498 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2500 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2504 void
2505 edit_push_redo_action (WEdit * edit, long c, ...)
2507 unsigned long sp = edit->redo_stack_pointer;
2508 unsigned long spm1;
2509 long *t;
2510 /* first enlarge the stack if necessary */
2511 if (sp > edit->redo_stack_size - 10)
2512 { /* say */
2513 if (option_max_undo < 256)
2514 option_max_undo = 256;
2515 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2517 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2518 if (t)
2520 edit->redo_stack = t;
2521 edit->redo_stack_size <<= 1;
2522 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2526 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2528 if (edit->redo_stack_bottom != sp
2529 && spm1 != edit->redo_stack_bottom
2530 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2532 int d;
2533 if (edit->redo_stack[spm1] < 0)
2535 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2536 if (d == c)
2538 if (edit->redo_stack[spm1] > -1000000000)
2540 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2541 edit->redo_stack[spm1]--;
2542 return;
2546 else
2548 d = edit->redo_stack[spm1];
2549 if (d == c)
2551 if (c >= KEY_PRESS)
2552 return; /* --> no need to push multiple do-nothings */
2553 edit->redo_stack[sp] = -2;
2554 goto redo_check_bottom;
2558 edit->redo_stack[sp] = c;
2560 redo_check_bottom:
2561 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2563 /* if the sp wraps round and catches the redo_stack_bottom then erase
2564 * the first set of actions on the stack to make space - by moving
2565 * redo_stack_bottom forward one "key press" */
2566 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2567 if ((unsigned long) c == edit->redo_stack_bottom ||
2568 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2571 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2573 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2574 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2577 * If a single key produced enough pushes to wrap all the way round then
2578 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2579 * The stack is then initialised:
2582 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2583 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2584 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2588 /* --------------------------------------------------------------------------------------------- */
2590 Basic low level single character buffer alterations and movements at the cursor.
2591 Returns char passed over, inserted or removed.
2594 void
2595 edit_insert (WEdit * edit, int c)
2597 /* check if file has grown to large */
2598 if (edit->last_byte >= SIZE_LIMIT)
2599 return;
2601 /* first we must update the position of the display window */
2602 if (edit->curs1 < edit->start_display)
2604 edit->start_display++;
2605 if (c == '\n')
2606 edit->start_line++;
2609 /* Mark file as modified, unless the file hasn't been fully loaded */
2610 if (edit->loading_done)
2612 edit_modification (edit);
2615 /* now we must update some info on the file and check if a redraw is required */
2616 if (c == '\n')
2618 if (edit->book_mark)
2619 book_mark_inc (edit, edit->curs_line);
2620 edit->curs_line++;
2621 edit->total_lines++;
2622 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2625 /* save the reverse command onto the undo stack */
2626 /* ordinary char and not space */
2627 if (c > 32)
2628 edit_push_undo_action (edit, BACKSPACE);
2629 else
2630 edit_push_undo_action (edit, BACKSPACE_BR);
2631 /* update markers */
2632 edit->mark1 += (edit->mark1 > edit->curs1);
2633 edit->mark2 += (edit->mark2 > edit->curs1);
2634 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2636 /* add a new buffer if we've reached the end of the last one */
2637 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2638 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2640 /* perform the insertion */
2641 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2642 = (unsigned char) c;
2644 /* update file length */
2645 edit->last_byte++;
2647 /* update cursor position */
2648 edit->curs1++;
2651 /* --------------------------------------------------------------------------------------------- */
2652 /** same as edit_insert and move left */
2654 void
2655 edit_insert_ahead (WEdit * edit, int c)
2657 if (edit->last_byte >= SIZE_LIMIT)
2658 return;
2660 if (edit->curs1 < edit->start_display)
2662 edit->start_display++;
2663 if (c == '\n')
2664 edit->start_line++;
2666 edit_modification (edit);
2667 if (c == '\n')
2669 if (edit->book_mark)
2670 book_mark_inc (edit, edit->curs_line);
2671 edit->total_lines++;
2672 edit->force |= REDRAW_AFTER_CURSOR;
2674 /* ordinary char and not space */
2675 if (c > 32)
2676 edit_push_undo_action (edit, DELCHAR);
2677 else
2678 edit_push_undo_action (edit, DELCHAR_BR);
2680 edit->mark1 += (edit->mark1 >= edit->curs1);
2681 edit->mark2 += (edit->mark2 >= edit->curs1);
2682 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2684 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2685 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2686 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2687 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2689 edit->last_byte++;
2690 edit->curs2++;
2694 /* --------------------------------------------------------------------------------------------- */
2697 edit_delete (WEdit * edit, const int byte_delete)
2699 int p = 0;
2700 int cw = 1;
2701 int i;
2703 if (!edit->curs2)
2704 return 0;
2706 cw = 1;
2707 /* if byte_delete = 1 then delete only one byte not multibyte char */
2708 if (edit->utf8 && byte_delete == 0)
2710 edit_get_utf (edit, edit->curs1, &cw);
2711 if (cw < 1)
2712 cw = 1;
2715 if (edit->mark2 != edit->mark1)
2716 edit_push_markers (edit);
2718 for (i = 1; i <= cw; i++)
2720 if (edit->mark1 > edit->curs1)
2722 edit->mark1--;
2723 edit->end_mark_curs--;
2725 if (edit->mark2 > edit->curs1)
2726 edit->mark2--;
2727 if (edit->last_get_rule > edit->curs1)
2728 edit->last_get_rule--;
2730 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2731 ((edit->curs2 -
2732 1) & M_EDIT_BUF_SIZE) - 1];
2734 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2736 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2737 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2739 edit->last_byte--;
2740 edit->curs2--;
2741 edit_push_undo_action (edit, p + 256);
2744 edit_modification (edit);
2745 if (p == '\n')
2747 if (edit->book_mark)
2748 book_mark_dec (edit, edit->curs_line);
2749 edit->total_lines--;
2750 edit->force |= REDRAW_AFTER_CURSOR;
2752 if (edit->curs1 < edit->start_display)
2754 edit->start_display--;
2755 if (p == '\n')
2756 edit->start_line--;
2759 return p;
2762 /* --------------------------------------------------------------------------------------------- */
2763 /** moves the cursor right or left: increment positive or negative respectively */
2765 void
2766 edit_cursor_move (WEdit * edit, long increment)
2768 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2769 int c;
2771 if (increment < 0)
2773 for (; increment < 0; increment++)
2775 if (!edit->curs1)
2776 return;
2778 edit_push_undo_action (edit, CURS_RIGHT);
2780 c = edit_get_byte (edit, edit->curs1 - 1);
2781 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2782 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2783 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2784 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2785 edit->curs2++;
2786 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2787 1) & M_EDIT_BUF_SIZE];
2788 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2790 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2791 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2793 edit->curs1--;
2794 if (c == '\n')
2796 edit->curs_line--;
2797 edit->force |= REDRAW_LINE_BELOW;
2802 else if (increment > 0)
2804 for (; increment > 0; increment--)
2806 if (!edit->curs2)
2807 return;
2809 edit_push_undo_action (edit, CURS_LEFT);
2811 c = edit_get_byte (edit, edit->curs1);
2812 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2813 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2814 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2815 edit->curs1++;
2816 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2817 ((edit->curs2 -
2818 1) & M_EDIT_BUF_SIZE) - 1];
2819 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2821 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2822 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2824 edit->curs2--;
2825 if (c == '\n')
2827 edit->curs_line++;
2828 edit->force |= REDRAW_LINE_ABOVE;
2834 /* These functions return positions relative to lines */
2836 /* --------------------------------------------------------------------------------------------- */
2837 /** returns index of last char on line + 1 */
2839 long
2840 edit_eol (WEdit * edit, long current)
2842 if (current >= edit->last_byte)
2843 return edit->last_byte;
2845 for (;; current++)
2846 if (edit_get_byte (edit, current) == '\n')
2847 break;
2848 return current;
2851 /* --------------------------------------------------------------------------------------------- */
2852 /** returns index of first char on line */
2854 long
2855 edit_bol (WEdit * edit, long current)
2857 if (current <= 0)
2858 return 0;
2860 for (;; current--)
2861 if (edit_get_byte (edit, current - 1) == '\n')
2862 break;
2863 return current;
2866 /* --------------------------------------------------------------------------------------------- */
2868 long
2869 edit_count_lines (WEdit * edit, long current, long upto)
2871 long lines = 0;
2872 if (upto > edit->last_byte)
2873 upto = edit->last_byte;
2874 if (current < 0)
2875 current = 0;
2876 while (current < upto)
2877 if (edit_get_byte (edit, current++) == '\n')
2878 lines++;
2879 return lines;
2882 /* --------------------------------------------------------------------------------------------- */
2883 /* If lines is zero this returns the count of lines from current to upto. */
2884 /* If upto is zero returns index of lines forward current. */
2886 long
2887 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2889 if (upto)
2891 return edit_count_lines (edit, current, upto);
2893 else
2895 long next;
2896 if (lines < 0)
2897 lines = 0;
2898 while (lines--)
2900 next = edit_eol (edit, current) + 1;
2901 if (next > edit->last_byte)
2902 break;
2903 else
2904 current = next;
2906 return current;
2910 /* --------------------------------------------------------------------------------------------- */
2911 /** Returns offset of 'lines' lines up from current */
2913 long
2914 edit_move_backward (WEdit * edit, long current, long lines)
2916 if (lines < 0)
2917 lines = 0;
2918 current = edit_bol (edit, current);
2919 while ((lines--) && current != 0)
2920 current = edit_bol (edit, current - 1);
2921 return current;
2924 /* --------------------------------------------------------------------------------------------- */
2925 /* If cols is zero this returns the count of columns from current to upto. */
2926 /* If upto is zero returns index of cols across from current. */
2928 long
2929 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
2931 long p, q;
2932 int col;
2934 if (upto)
2936 q = upto;
2937 cols = -10;
2939 else
2940 q = edit->last_byte + 2;
2942 for (col = 0, p = current; p < q; p++)
2944 int c, orig_c;
2946 if (cols != -10)
2948 if (col == cols)
2949 return p;
2950 if (col > cols)
2951 return p - 1;
2954 orig_c = c = edit_get_byte (edit, p);
2956 #ifdef HAVE_CHARSET
2957 if (edit->utf8)
2959 int utf_ch;
2960 int cw = 1;
2962 utf_ch = edit_get_utf (edit, p, &cw);
2963 if (utf8_display)
2965 if (cw > 1)
2966 col -= cw - 1;
2967 if (g_unichar_iswide (utf_ch))
2968 col++;
2970 else if (cw > 1 && g_unichar_isprint (utf_ch))
2971 col -= cw - 1;
2974 c = convert_to_display_c (c);
2975 #endif
2977 if (c == '\t')
2978 col += TAB_SIZE - col % TAB_SIZE;
2979 else if (c == '\n')
2981 if (upto)
2982 return col;
2983 else
2984 return p;
2986 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
2987 /* '\r' is shown as ^M, so we must advance 2 characters */
2988 /* Caret notation for control characters */
2989 col += 2;
2990 else
2991 col++;
2993 return col;
2996 /* --------------------------------------------------------------------------------------------- */
2997 /** returns the current column position of the cursor */
3000 edit_get_col (WEdit * edit)
3002 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3005 /* --------------------------------------------------------------------------------------------- */
3006 /* Scrolling functions */
3007 /* --------------------------------------------------------------------------------------------- */
3009 void
3010 edit_update_curs_row (WEdit * edit)
3012 edit->curs_row = edit->curs_line - edit->start_line;
3015 /* --------------------------------------------------------------------------------------------- */
3017 void
3018 edit_update_curs_col (WEdit * edit)
3020 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3023 /* --------------------------------------------------------------------------------------------- */
3026 edit_get_curs_col (const WEdit * edit)
3028 return edit->curs_col;
3031 /* --------------------------------------------------------------------------------------------- */
3032 /** moves the display start position up by i lines */
3034 void
3035 edit_scroll_upward (WEdit * edit, unsigned long i)
3037 unsigned long lines_above = edit->start_line;
3038 if (i > lines_above)
3039 i = lines_above;
3040 if (i)
3042 edit->start_line -= i;
3043 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3044 edit->force |= REDRAW_PAGE;
3045 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3047 edit_update_curs_row (edit);
3051 /* --------------------------------------------------------------------------------------------- */
3052 /** returns 1 if could scroll, 0 otherwise */
3054 void
3055 edit_scroll_downward (WEdit * edit, int i)
3057 int lines_below;
3058 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
3059 if (lines_below > 0)
3061 if (i > lines_below)
3062 i = lines_below;
3063 edit->start_line += i;
3064 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3065 edit->force |= REDRAW_PAGE;
3066 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3068 edit_update_curs_row (edit);
3071 /* --------------------------------------------------------------------------------------------- */
3073 void
3074 edit_scroll_right (WEdit * edit, int i)
3076 edit->force |= REDRAW_PAGE;
3077 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3078 edit->start_col -= i;
3081 /* --------------------------------------------------------------------------------------------- */
3083 void
3084 edit_scroll_left (WEdit * edit, int i)
3086 if (edit->start_col)
3088 edit->start_col += i;
3089 if (edit->start_col > 0)
3090 edit->start_col = 0;
3091 edit->force |= REDRAW_PAGE;
3092 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3096 /* --------------------------------------------------------------------------------------------- */
3097 /* high level cursor movement commands */
3098 /* --------------------------------------------------------------------------------------------- */
3100 void
3101 edit_move_to_prev_col (WEdit * edit, long p)
3103 int prev = edit->prev_col;
3104 int over = edit->over_col;
3105 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3107 if (option_cursor_beyond_eol)
3109 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3110 edit_eol (edit, edit->curs1));
3112 if (line_len < prev + edit->over_col)
3114 edit->over_col = prev + over - line_len;
3115 edit->prev_col = line_len;
3116 edit->curs_col = line_len;
3118 else
3120 edit->curs_col = prev + over;
3121 edit->prev_col = edit->curs_col;
3122 edit->over_col = 0;
3125 else
3127 edit->over_col = 0;
3128 if (is_in_indent (edit) && option_fake_half_tabs)
3130 edit_update_curs_col (edit);
3131 if (space_width)
3132 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3134 int q = edit->curs_col;
3135 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3136 p = edit_bol (edit, edit->curs1);
3137 edit_cursor_move (edit,
3138 edit_move_forward3 (edit, p, edit->curs_col,
3139 0) - edit->curs1);
3140 if (!left_of_four_spaces (edit))
3141 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3147 /* --------------------------------------------------------------------------------------------- */
3150 line_is_blank (WEdit * edit, long line)
3152 return is_blank (edit, edit_find_line (edit, line));
3155 /* --------------------------------------------------------------------------------------------- */
3156 /** move cursor to line 'line' */
3158 void
3159 edit_move_to_line (WEdit * e, long line)
3161 if (line < e->curs_line)
3162 edit_move_up (e, e->curs_line - line, 0);
3163 else
3164 edit_move_down (e, line - e->curs_line, 0);
3165 edit_scroll_screen_over_cursor (e);
3168 /* --------------------------------------------------------------------------------------------- */
3169 /** scroll window so that first visible line is 'line' */
3171 void
3172 edit_move_display (WEdit * e, long line)
3174 if (line < e->start_line)
3175 edit_scroll_upward (e, e->start_line - line);
3176 else
3177 edit_scroll_downward (e, line - e->start_line);
3180 /* --------------------------------------------------------------------------------------------- */
3181 /** save markers onto undo stack */
3183 void
3184 edit_push_markers (WEdit * edit)
3186 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3187 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3188 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3191 /* --------------------------------------------------------------------------------------------- */
3193 void
3194 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3196 edit->mark1 = m1;
3197 edit->mark2 = m2;
3198 edit->column1 = c1;
3199 edit->column2 = c2;
3203 /* --------------------------------------------------------------------------------------------- */
3204 /** highlight marker toggle */
3206 void
3207 edit_mark_cmd (WEdit * edit, int unmark)
3209 edit_push_markers (edit);
3210 if (unmark)
3212 edit_set_markers (edit, 0, 0, 0, 0);
3213 edit->force |= REDRAW_PAGE;
3215 else
3217 if (edit->mark2 >= 0)
3219 edit->end_mark_curs = -1;
3220 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3221 edit->curs_col + edit->over_col);
3222 edit->force |= REDRAW_PAGE;
3224 else
3226 edit->end_mark_curs = edit->curs1;
3227 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3228 edit->curs_col + edit->over_col);
3233 /* --------------------------------------------------------------------------------------------- */
3235 void
3236 edit_delete_line (WEdit * edit)
3239 * Delete right part of the line.
3240 * Note that edit_get_byte() returns '\n' when byte position is
3241 * beyond EOF.
3243 while (edit_get_byte (edit, edit->curs1) != '\n')
3245 (void) edit_delete (edit, 1);
3249 * Delete '\n' char.
3250 * Note that edit_delete() will not corrupt anything if called while
3251 * cursor position is EOF.
3253 (void) edit_delete (edit, 1);
3256 * Delete left part of the line.
3257 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3259 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3261 (void) edit_backspace (edit, 1);
3265 /* --------------------------------------------------------------------------------------------- */
3267 void
3268 insert_spaces_tab (WEdit * edit, int half)
3270 int i;
3271 edit_update_curs_col (edit);
3272 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
3273 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
3274 while (i > 0)
3276 edit_insert (edit, ' ');
3277 i -= space_width;
3281 /* --------------------------------------------------------------------------------------------- */
3284 edit_indent_width (WEdit * edit, long p)
3286 long q = p;
3287 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3288 q++;
3289 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3292 /* --------------------------------------------------------------------------------------------- */
3294 void
3295 edit_insert_indent (WEdit * edit, int indent)
3297 if (!option_fill_tabs_with_spaces)
3299 while (indent >= TAB_SIZE)
3301 edit_insert (edit, '\t');
3302 indent -= TAB_SIZE;
3305 while (indent-- > 0)
3306 edit_insert (edit, ' ');
3309 /* --------------------------------------------------------------------------------------------- */
3311 void
3312 edit_push_key_press (WEdit * edit)
3314 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3315 if (edit->mark2 == -1)
3317 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3318 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3322 /* --------------------------------------------------------------------------------------------- */
3324 void
3325 edit_find_bracket (WEdit * edit)
3327 edit->bracket = edit_get_bracket (edit, 1, 10000);
3328 if (last_bracket != edit->bracket)
3329 edit->force |= REDRAW_PAGE;
3330 last_bracket = edit->bracket;
3333 /* --------------------------------------------------------------------------------------------- */
3335 * This executes a command as though the user initiated it through a key
3336 * press. Callback with WIDGET_KEY as a message calls this after
3337 * translating the key press. This function can be used to pass any
3338 * command to the editor. Note that the screen wouldn't update
3339 * automatically. Either of command or char_for_insertion must be
3340 * passed as -1. Commands are executed, and char_for_insertion is
3341 * inserted at the cursor.
3344 void
3345 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3347 if (command == CK_Begin_Record_Macro)
3349 edit->macro_i = 0;
3350 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3351 return;
3353 if (command == CK_End_Record_Macro && edit->macro_i != -1)
3355 edit->force |= REDRAW_COMPLETELY;
3356 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
3357 edit->macro_i = -1;
3358 return;
3360 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
3362 edit->macro[edit->macro_i].command = command;
3363 edit->macro[edit->macro_i++].ch = char_for_insertion;
3365 /* record the beginning of a set of editing actions initiated by a key press */
3366 if (command != CK_Undo && command != CK_Ext_Mode)
3367 edit_push_key_press (edit);
3369 edit_execute_cmd (edit, command, char_for_insertion);
3370 if (edit->column_highlight)
3371 edit->force |= REDRAW_PAGE;
3374 /* --------------------------------------------------------------------------------------------- */
3376 This executes a command at a lower level than macro recording.
3377 It also does not push a key_press onto the undo stack. This means
3378 that if it is called many times, a single undo command will undo
3379 all of them. It also does not check for the Undo command.
3381 void
3382 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3384 edit->force |= REDRAW_LINE;
3386 /* The next key press will unhighlight the found string, so update
3387 * the whole page */
3388 if (edit->found_len || edit->column_highlight)
3389 edit->force |= REDRAW_PAGE;
3391 if (command / 100 == 6)
3392 { /* a highlight command like shift-arrow */
3393 edit->column_highlight = 0;
3394 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3396 edit_mark_cmd (edit, 1); /* clear */
3397 edit_mark_cmd (edit, 0); /* marking on */
3399 edit->highlight = 1;
3401 else
3402 { /* any other command */
3403 if (edit->highlight)
3404 edit_mark_cmd (edit, 0); /* clear */
3405 edit->highlight = 0;
3408 /* first check for undo */
3409 if (command == CK_Undo)
3411 edit->redo_stack_reset = 0;
3412 edit_group_undo (edit);
3413 edit->found_len = 0;
3414 edit->prev_col = edit_get_col (edit);
3415 edit->search_start = edit->curs1;
3416 return;
3418 /* check for redo */
3419 if (command == CK_Redo)
3421 edit->redo_stack_reset = 0;
3422 edit_do_redo (edit);
3423 edit->found_len = 0;
3424 edit->prev_col = edit_get_col (edit);
3425 edit->search_start = edit->curs1;
3426 return;
3429 edit->redo_stack_reset = 1;
3431 /* An ordinary key press */
3432 if (char_for_insertion >= 0)
3434 /* if non persistent selection and text selected */
3435 if (!option_persistent_selections)
3437 if (edit->mark1 != edit->mark2)
3438 edit_block_delete_cmd (edit);
3440 if (edit->overwrite)
3442 /* remove char only one time, after input first byte, multibyte chars */
3443 if ((!utf8_display || edit->charpoint == 0)
3444 && edit_get_byte (edit, edit->curs1) != '\n')
3445 edit_delete (edit, 0);
3447 if (option_cursor_beyond_eol && edit->over_col > 0)
3448 edit_insert_over (edit);
3449 #ifdef HAVE_CHARSET
3450 if (char_for_insertion > 255 && utf8_display == 0)
3452 unsigned char str[6 + 1];
3453 size_t i = 0;
3454 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3455 if (res == 0)
3457 str[0] = '.';
3458 str[1] = '\0';
3460 else
3462 str[res] = '\0';
3464 while (str[i] != 0 && i <= 6)
3466 char_for_insertion = str[i];
3467 edit_insert (edit, char_for_insertion);
3468 i++;
3471 else
3472 #endif
3473 edit_insert (edit, char_for_insertion);
3475 if (option_auto_para_formatting)
3477 format_paragraph (edit, 0);
3478 edit->force |= REDRAW_PAGE;
3480 else
3481 check_and_wrap_line (edit);
3482 edit->found_len = 0;
3483 edit->prev_col = edit_get_col (edit);
3484 edit->search_start = edit->curs1;
3485 edit_find_bracket (edit);
3486 return;
3489 switch (command)
3491 case CK_Begin_Page:
3492 case CK_End_Page:
3493 case CK_Begin_Page_Highlight:
3494 case CK_End_Page_Highlight:
3495 case CK_Word_Left:
3496 case CK_Word_Right:
3497 case CK_Up:
3498 case CK_Down:
3499 case CK_Left:
3500 case CK_Right:
3501 if (edit->mark2 >= 0)
3503 if (!option_persistent_selections)
3505 if (edit->column_highlight)
3506 edit_push_undo_action (edit, COLUMN_ON);
3507 edit->column_highlight = 0;
3508 edit_mark_cmd (edit, 1);
3513 switch (command)
3515 case CK_Begin_Page:
3516 case CK_End_Page:
3517 case CK_Begin_Page_Highlight:
3518 case CK_End_Page_Highlight:
3519 case CK_Word_Left:
3520 case CK_Word_Right:
3521 case CK_Up:
3522 case CK_Down:
3523 case CK_Word_Left_Highlight:
3524 case CK_Word_Right_Highlight:
3525 case CK_Up_Highlight:
3526 case CK_Down_Highlight:
3527 case CK_Up_Alt_Highlight:
3528 case CK_Down_Alt_Highlight:
3529 if (edit->mark2 == -1)
3530 break; /*marking is following the cursor: may need to highlight a whole line */
3531 case CK_Left:
3532 case CK_Right:
3533 case CK_Left_Highlight:
3534 case CK_Right_Highlight:
3535 edit->force |= REDRAW_CHAR_ONLY;
3538 /* basic cursor key commands */
3539 switch (command)
3541 case CK_BackSpace:
3542 /* if non persistent selection and text selected */
3543 if (!option_persistent_selections)
3545 if (edit->mark1 != edit->mark2)
3547 edit_block_delete_cmd (edit);
3548 break;
3551 if (option_cursor_beyond_eol && edit->over_col > 0)
3553 edit->over_col--;
3554 break;
3556 if (option_backspace_through_tabs && is_in_indent (edit))
3558 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3559 edit_backspace (edit, 1);
3560 break;
3562 else
3564 if (option_fake_half_tabs)
3566 int i;
3567 if (is_in_indent (edit) && right_of_four_spaces (edit))
3569 for (i = 0; i < HALF_TAB_SIZE; i++)
3570 edit_backspace (edit, 1);
3571 break;
3575 edit_backspace (edit, 0);
3576 break;
3577 case CK_Delete:
3578 /* if non persistent selection and text selected */
3579 if (!option_persistent_selections)
3581 if (edit->mark1 != edit->mark2)
3583 edit_block_delete_cmd (edit);
3584 break;
3588 if (option_cursor_beyond_eol && edit->over_col > 0)
3589 edit_insert_over (edit);
3591 if (option_fake_half_tabs)
3593 int i;
3594 if (is_in_indent (edit) && left_of_four_spaces (edit))
3596 for (i = 1; i <= HALF_TAB_SIZE; i++)
3597 edit_delete (edit, 1);
3598 break;
3601 edit_delete (edit, 0);
3602 break;
3603 case CK_Delete_Word_Left:
3604 edit->over_col = 0;
3605 edit_left_delete_word (edit);
3606 break;
3607 case CK_Delete_Word_Right:
3608 if (option_cursor_beyond_eol && edit->over_col > 0)
3609 edit_insert_over (edit);
3611 edit_right_delete_word (edit);
3612 break;
3613 case CK_Delete_Line:
3614 edit_delete_line (edit);
3615 break;
3616 case CK_Delete_To_Line_End:
3617 edit_delete_to_line_end (edit);
3618 break;
3619 case CK_Delete_To_Line_Begin:
3620 edit_delete_to_line_begin (edit);
3621 break;
3622 case CK_Enter:
3623 edit->over_col = 0;
3624 if (option_auto_para_formatting)
3626 edit_double_newline (edit);
3627 if (option_return_does_auto_indent)
3628 edit_auto_indent (edit);
3629 format_paragraph (edit, 0);
3631 else
3633 edit_insert (edit, '\n');
3634 if (option_return_does_auto_indent)
3636 edit_auto_indent (edit);
3639 break;
3640 case CK_Return:
3641 edit_insert (edit, '\n');
3642 break;
3644 case CK_Page_Up_Alt_Highlight:
3645 edit->column_highlight = 1;
3646 case CK_Page_Up:
3647 case CK_Page_Up_Highlight:
3648 edit_move_up (edit, edit->num_widget_lines - 1, 1);
3649 break;
3650 case CK_Page_Down_Alt_Highlight:
3651 edit->column_highlight = 1;
3652 case CK_Page_Down:
3653 case CK_Page_Down_Highlight:
3654 edit_move_down (edit, edit->num_widget_lines - 1, 1);
3655 break;
3656 case CK_Left_Alt_Highlight:
3657 edit->column_highlight = 1;
3658 case CK_Left:
3659 case CK_Left_Highlight:
3660 if (option_fake_half_tabs)
3662 if (is_in_indent (edit) && right_of_four_spaces (edit))
3664 if (option_cursor_beyond_eol && edit->over_col > 0)
3665 edit->over_col--;
3666 else
3667 edit_cursor_move (edit, -HALF_TAB_SIZE);
3668 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3669 break;
3672 edit_left_char_move_cmd (edit);
3673 break;
3674 case CK_Right_Alt_Highlight:
3675 edit->column_highlight = 1;
3676 case CK_Right:
3677 case CK_Right_Highlight:
3678 if (option_fake_half_tabs)
3680 if (is_in_indent (edit) && left_of_four_spaces (edit))
3682 edit_cursor_move (edit, HALF_TAB_SIZE);
3683 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3684 break;
3687 edit_right_char_move_cmd (edit);
3688 break;
3689 case CK_Begin_Page:
3690 case CK_Begin_Page_Highlight:
3691 edit_begin_page (edit);
3692 break;
3693 case CK_End_Page:
3694 case CK_End_Page_Highlight:
3695 edit_end_page (edit);
3696 break;
3697 case CK_Word_Left:
3698 case CK_Word_Left_Highlight:
3699 edit->over_col = 0;
3700 edit_left_word_move_cmd (edit);
3701 break;
3702 case CK_Word_Right:
3703 case CK_Word_Right_Highlight:
3704 edit->over_col = 0;
3705 edit_right_word_move_cmd (edit);
3706 break;
3707 case CK_Up_Alt_Highlight:
3708 edit->column_highlight = 1;
3709 case CK_Up:
3710 case CK_Up_Highlight:
3711 edit_move_up (edit, 1, 0);
3712 break;
3713 case CK_Down_Alt_Highlight:
3714 edit->column_highlight = 1;
3715 case CK_Down:
3716 case CK_Down_Highlight:
3717 edit_move_down (edit, 1, 0);
3718 break;
3719 case CK_Paragraph_Up_Alt_Highlight:
3720 edit->column_highlight = 1;
3721 case CK_Paragraph_Up:
3722 case CK_Paragraph_Up_Highlight:
3723 edit_move_up_paragraph (edit, 0);
3724 break;
3725 case CK_Paragraph_Down_Alt_Highlight:
3726 edit->column_highlight = 1;
3727 case CK_Paragraph_Down:
3728 case CK_Paragraph_Down_Highlight:
3729 edit_move_down_paragraph (edit, 0);
3730 break;
3731 case CK_Scroll_Up_Alt_Highlight:
3732 edit->column_highlight = 1;
3733 case CK_Scroll_Up:
3734 case CK_Scroll_Up_Highlight:
3735 edit_move_up (edit, 1, 1);
3736 break;
3737 case CK_Scroll_Down_Alt_Highlight:
3738 edit->column_highlight = 1;
3739 case CK_Scroll_Down:
3740 case CK_Scroll_Down_Highlight:
3741 edit_move_down (edit, 1, 1);
3742 break;
3743 case CK_Home:
3744 case CK_Home_Highlight:
3745 edit_cursor_to_bol (edit);
3746 break;
3747 case CK_End:
3748 case CK_End_Highlight:
3749 edit_cursor_to_eol (edit);
3750 break;
3751 case CK_Tab:
3752 /* if text marked shift block */
3753 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3755 if (edit->mark2 < 0)
3756 edit_mark_cmd (edit, 0);
3757 edit_move_block_to_right (edit);
3759 else
3761 if (option_cursor_beyond_eol)
3762 edit_insert_over (edit);
3763 edit_tab_cmd (edit);
3764 if (option_auto_para_formatting)
3766 format_paragraph (edit, 0);
3767 edit->force |= REDRAW_PAGE;
3769 else
3771 check_and_wrap_line (edit);
3774 break;
3776 case CK_Toggle_Insert:
3777 edit->overwrite = (edit->overwrite == 0);
3778 break;
3780 case CK_Mark:
3781 if (edit->mark2 >= 0)
3783 if (edit->column_highlight)
3784 edit_push_undo_action (edit, COLUMN_ON);
3785 edit->column_highlight = 0;
3787 edit_mark_cmd (edit, 0);
3788 break;
3789 case CK_Column_Mark:
3790 if (!edit->column_highlight)
3791 edit_push_undo_action (edit, COLUMN_OFF);
3792 edit->column_highlight = 1;
3793 edit_mark_cmd (edit, 0);
3794 break;
3795 case CK_Mark_All:
3796 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3797 edit->force |= REDRAW_PAGE;
3798 break;
3799 case CK_Unmark:
3800 if (edit->column_highlight)
3801 edit_push_undo_action (edit, COLUMN_ON);
3802 edit->column_highlight = 0;
3803 edit_mark_cmd (edit, 1);
3804 break;
3806 case CK_Toggle_Line_State:
3807 option_line_state = !option_line_state;
3808 if (option_line_state)
3810 option_line_state_width = LINE_STATE_WIDTH;
3812 else
3814 option_line_state_width = 0;
3816 edit->force |= REDRAW_PAGE;
3817 break;
3819 case CK_Toggle_Show_Margin:
3820 show_right_margin = !show_right_margin;
3821 edit->force |= REDRAW_PAGE;
3822 break;
3824 case CK_Toggle_Bookmark:
3825 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3826 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3827 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3828 else
3829 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3830 break;
3831 case CK_Flush_Bookmarks:
3832 book_mark_flush (edit, BOOK_MARK_COLOR);
3833 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3834 edit->force |= REDRAW_PAGE;
3835 break;
3836 case CK_Next_Bookmark:
3837 if (edit->book_mark)
3839 struct _book_mark *p;
3840 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3841 if (p->next)
3843 p = p->next;
3844 if (p->line >= edit->start_line + edit->num_widget_lines
3845 || p->line < edit->start_line)
3846 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3847 edit_move_to_line (edit, p->line);
3850 break;
3851 case CK_Prev_Bookmark:
3852 if (edit->book_mark)
3854 struct _book_mark *p;
3855 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3856 while (p->line == edit->curs_line)
3857 if (p->prev)
3858 p = p->prev;
3859 if (p->line >= 0)
3861 if (p->line >= edit->start_line + edit->num_widget_lines
3862 || p->line < edit->start_line)
3863 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
3864 edit_move_to_line (edit, p->line);
3867 break;
3869 case CK_Beginning_Of_Text:
3870 case CK_Beginning_Of_Text_Highlight:
3871 edit_move_to_top (edit);
3872 break;
3873 case CK_End_Of_Text:
3874 case CK_End_Of_Text_Highlight:
3875 edit_move_to_bottom (edit);
3876 break;
3878 case CK_Copy:
3879 if (option_cursor_beyond_eol && edit->over_col > 0)
3880 edit_insert_over (edit);
3881 edit_block_copy_cmd (edit);
3882 break;
3883 case CK_Remove:
3884 edit_block_delete_cmd (edit);
3885 break;
3886 case CK_Move:
3887 if (option_cursor_beyond_eol && edit->over_col > 0)
3888 edit_insert_over (edit);
3889 edit_block_move_cmd (edit);
3890 break;
3892 case CK_Shift_Block_Left:
3893 if (edit->mark1 != edit->mark2)
3894 edit_move_block_to_left (edit);
3895 break;
3896 case CK_Shift_Block_Right:
3897 if (edit->mark1 != edit->mark2)
3898 edit_move_block_to_right (edit);
3899 break;
3900 case CK_XStore:
3901 edit_copy_to_X_buf_cmd (edit);
3902 break;
3903 case CK_XCut:
3904 edit_cut_to_X_buf_cmd (edit);
3905 break;
3906 case CK_XPaste:
3907 /* if non persistent selection and text selected */
3908 if (!option_persistent_selections)
3910 if (edit->mark1 != edit->mark2)
3911 edit_block_delete_cmd (edit);
3913 if (option_cursor_beyond_eol && edit->over_col > 0)
3914 edit_insert_over (edit);
3915 edit_paste_from_X_buf_cmd (edit);
3916 break;
3917 case CK_Selection_History:
3918 edit_paste_from_history (edit);
3919 break;
3921 case CK_Save_As:
3922 edit_save_as_cmd (edit);
3923 break;
3924 case CK_Save:
3925 edit_save_confirm_cmd (edit);
3926 break;
3927 case CK_Load:
3928 edit_load_cmd (edit, EDIT_FILE_COMMON);
3929 break;
3930 case CK_Save_Block:
3931 edit_save_block_cmd (edit);
3932 break;
3933 case CK_Insert_File:
3934 edit_insert_file_cmd (edit);
3935 break;
3937 case CK_Load_Prev_File:
3938 edit_load_back_cmd (edit);
3939 break;
3940 case CK_Load_Next_File:
3941 edit_load_forward_cmd (edit);
3942 break;
3944 case CK_Load_Syntax_File:
3945 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3946 break;
3947 case CK_Choose_Syntax:
3948 edit_syntax_dialog (edit);
3949 break;
3951 case CK_Load_Menu_File:
3952 edit_load_cmd (edit, EDIT_FILE_MENU);
3953 break;
3955 case CK_Toggle_Syntax:
3956 option_syntax_highlighting ^= 1;
3957 if (option_syntax_highlighting == 1)
3958 edit_load_syntax (edit, NULL, edit->syntax_type);
3959 edit->force |= REDRAW_PAGE;
3960 break;
3962 case CK_Toggle_Tab_TWS:
3963 enable_show_tabs_tws ^= 1;
3964 edit->force |= REDRAW_PAGE;
3965 break;
3967 case CK_Find:
3968 edit_search_cmd (edit, FALSE);
3969 break;
3970 case CK_Find_Again:
3971 edit_search_cmd (edit, TRUE);
3972 break;
3973 case CK_Replace:
3974 edit_replace_cmd (edit, 0);
3975 break;
3976 case CK_Replace_Again:
3977 edit_replace_cmd (edit, 1);
3978 break;
3979 case CK_Complete_Word:
3980 /* if text marked shift block */
3981 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3983 edit_move_block_to_left (edit);
3985 else
3987 edit_complete_word_cmd (edit);
3989 break;
3990 case CK_Find_Definition:
3991 edit_get_match_keyword_cmd (edit);
3992 break;
3993 case CK_Quit:
3994 dlg_stop (edit->widget.owner);
3995 break;
3996 case CK_New:
3997 edit_new_cmd (edit);
3998 break;
3999 case CK_Help:
4000 edit_help_cmd (edit);
4001 break;
4002 case CK_Refresh:
4003 edit_refresh_cmd (edit);
4004 break;
4005 case CK_SaveSetupCmd:
4006 save_setup_cmd ();
4007 break;
4008 case CK_About:
4009 edit_about ();
4010 break;
4011 case CK_LearnKeys:
4012 learn_keys ();
4013 break;
4014 case CK_Edit_Options:
4015 edit_options_dialog (edit);
4016 break;
4017 case CK_Edit_Save_Mode:
4018 menu_save_mode_cmd ();
4019 break;
4020 case CK_Date:
4022 char s[BUF_MEDIUM];
4023 /* fool gcc to prevent a Y2K warning */
4024 char time_format[] = "_c";
4025 time_format[0] = '%';
4027 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4028 edit_print_string (edit, s);
4029 edit->force |= REDRAW_PAGE;
4030 break;
4032 break;
4033 case CK_Goto:
4034 edit_goto_cmd (edit);
4035 break;
4036 case CK_Paragraph_Format:
4037 format_paragraph (edit, 1);
4038 edit->force |= REDRAW_PAGE;
4039 break;
4040 case CK_Delete_Macro:
4041 edit_delete_macro_cmd (edit);
4042 break;
4043 case CK_Match_Bracket:
4044 edit_goto_matching_bracket (edit);
4045 break;
4046 case CK_User_Menu:
4047 user_menu (edit);
4048 break;
4049 case CK_Sort:
4050 edit_sort_cmd (edit);
4051 break;
4052 case CK_ExtCmd:
4053 edit_ext_cmd (edit);
4054 break;
4055 case CK_Mail:
4056 edit_mail_dialog (edit);
4057 break;
4058 case CK_Shell:
4059 view_other_cmd ();
4060 break;
4061 case CK_SelectCodepage:
4062 edit_select_codepage_cmd (edit);
4063 break;
4064 case CK_Insert_Literal:
4065 edit_insert_literal_cmd (edit);
4066 break;
4067 case CK_Execute_Macro:
4068 edit_execute_macro_cmd (edit);
4069 break;
4070 case CK_Begin_End_Macro:
4071 edit_begin_end_macro_cmd (edit);
4072 break;
4073 case CK_Ext_Mode:
4074 edit->extmod = 1;
4075 break;
4076 default:
4077 break;
4080 /* CK_Pipe_Block */
4081 if ((command / 1000) == 1) /* a shell command */
4082 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
4083 if (command > CK_Macro (0) && command <= CK_Last_Macro)
4084 { /* a macro command */
4085 struct macro m[MAX_MACRO_LENGTH];
4086 int nm;
4087 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
4088 edit_execute_macro (edit, m, nm);
4091 /* keys which must set the col position, and the search vars */
4092 switch (command)
4094 case CK_Find:
4095 case CK_Find_Again:
4096 case CK_Replace:
4097 case CK_Replace_Again:
4098 case CK_Complete_Word:
4099 edit->prev_col = edit_get_col (edit);
4100 break;
4101 case CK_Up:
4102 case CK_Up_Highlight:
4103 case CK_Up_Alt_Highlight:
4104 case CK_Down:
4105 case CK_Down_Highlight:
4106 case CK_Down_Alt_Highlight:
4107 case CK_Page_Up:
4108 case CK_Page_Up_Highlight:
4109 case CK_Page_Up_Alt_Highlight:
4110 case CK_Page_Down:
4111 case CK_Page_Down_Highlight:
4112 case CK_Page_Down_Alt_Highlight:
4113 case CK_Beginning_Of_Text:
4114 case CK_Beginning_Of_Text_Highlight:
4115 case CK_End_Of_Text:
4116 case CK_End_Of_Text_Highlight:
4117 case CK_Paragraph_Up:
4118 case CK_Paragraph_Up_Highlight:
4119 case CK_Paragraph_Up_Alt_Highlight:
4120 case CK_Paragraph_Down:
4121 case CK_Paragraph_Down_Highlight:
4122 case CK_Paragraph_Down_Alt_Highlight:
4123 case CK_Scroll_Up:
4124 case CK_Scroll_Up_Highlight:
4125 case CK_Scroll_Up_Alt_Highlight:
4126 case CK_Scroll_Down:
4127 case CK_Scroll_Down_Highlight:
4128 case CK_Scroll_Down_Alt_Highlight:
4129 edit->search_start = edit->curs1;
4130 edit->found_len = 0;
4131 break;
4132 default:
4133 edit->found_len = 0;
4134 edit->prev_col = edit_get_col (edit);
4135 edit->search_start = edit->curs1;
4137 edit_find_bracket (edit);
4139 if (option_auto_para_formatting)
4141 switch (command)
4143 case CK_BackSpace:
4144 case CK_Delete:
4145 case CK_Delete_Word_Left:
4146 case CK_Delete_Word_Right:
4147 case CK_Delete_To_Line_End:
4148 case CK_Delete_To_Line_Begin:
4149 format_paragraph (edit, 0);
4150 edit->force |= REDRAW_PAGE;
4155 /* --------------------------------------------------------------------------------------------- */
4157 void
4158 edit_stack_init (void)
4160 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4162 edit_history_moveto[edit_stack_iterator].filename = NULL;
4163 edit_history_moveto[edit_stack_iterator].line = -1;
4166 edit_stack_iterator = 0;
4169 /* --------------------------------------------------------------------------------------------- */
4171 void
4172 edit_stack_free (void)
4174 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4175 g_free (edit_history_moveto[edit_stack_iterator].filename);
4178 /* --------------------------------------------------------------------------------------------- */
4179 /** move i lines */
4181 void
4182 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4184 edit_move_updown (edit, i, do_scroll, TRUE);
4187 /* --------------------------------------------------------------------------------------------- */
4188 /** move i lines */
4190 void
4191 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4193 edit_move_updown (edit, i, do_scroll, FALSE);
4196 /* --------------------------------------------------------------------------------------------- */
4198 unsigned int
4199 edit_unlock_file (WEdit * edit)
4201 char *fullpath;
4202 unsigned int ret;
4204 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4205 ret = unlock_file (fullpath);
4206 g_free (fullpath);
4208 return ret;
4211 /* --------------------------------------------------------------------------------------------- */
4213 unsigned int
4214 edit_lock_file (WEdit * edit)
4216 char *fullpath;
4217 unsigned int ret;
4219 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4220 ret = lock_file (fullpath);
4221 g_free (fullpath);
4223 return ret;
4226 /* --------------------------------------------------------------------------------------------- */