extfs helpers: Replace all 'gawk' occurences with @AWK@ meta variable.
[midnight-commander.git] / src / editor / edit.c
blob3e4f366526ffaaf92bb1b08c4dee38b04d0a6393
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 Paul Sheer 1996, 1997
7 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
25 /** \file
26 * \brief Source: editor low level data handling and cursor fundamentals
27 * \author Paul Sheer
28 * \date 1996, 1997
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
41 #include <fcntl.h>
43 #include "lib/global.h"
45 #include "lib/tty/color.h"
46 #include "lib/tty/tty.h" /* attrset() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
49 #include "lib/vfs/mc-vfs/vfs.h"
50 #include "lib/strutil.h" /* utf string functions */
51 #include "lib/util.h" /* load_file_position(), save_file_position() */
52 #include "lib/timefmt.h" /* time formatting */
53 #include "lib/lock.h"
54 #include "lib/widget.h"
56 #ifdef HAVE_CHARSET
57 #include "lib/charsets.h" /* get_codepage_id */
58 #endif
60 #include "src/filemanager/cmd.h" /* view_other_cmd() */
61 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
63 #include "src/main.h" /* source_codepage */
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/learn.h" /* learn_keys */
66 #include "src/keybind-defaults.h"
68 #include "edit-impl.h"
69 #include "edit-widget.h"
71 /*** global variables ****************************************************************************/
73 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
74 int option_typewriter_wrap = 0;
75 int option_auto_para_formatting = 0;
76 int option_fill_tabs_with_spaces = 0;
77 int option_return_does_auto_indent = 1;
78 int option_backspace_through_tabs = 0;
79 int option_fake_half_tabs = 1;
80 int option_save_mode = EDIT_QUICK_SAVE;
81 int option_save_position = 1;
82 int option_max_undo = 32768;
83 int option_persistent_selections = 1;
84 int option_cursor_beyond_eol = 0;
85 int option_line_state = 0;
86 int option_line_state_width = 0;
88 int option_edit_right_extreme = 0;
89 int option_edit_left_extreme = 0;
90 int option_edit_top_extreme = 0;
91 int option_edit_bottom_extreme = 0;
92 int enable_show_tabs_tws = 1;
93 int option_check_nl_at_eof = 0;
94 int 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->stack_disable = 1;
483 if (!edit_insert_file (edit, edit->filename))
485 edit_clean (edit);
486 return 1;
488 edit->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->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 pop_action (WEdit * edit)
586 long c;
587 unsigned long sp = edit->stack_pointer;
589 if (sp == edit->stack_bottom)
590 return STACK_BOTTOM;
592 sp = (sp - 1) & edit->stack_size_mask;
593 c = edit->undo_stack[sp];
594 if (c >= 0)
596 /* edit->undo_stack[sp] = '@'; */
597 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
598 return c;
601 if (sp == edit->stack_bottom)
602 return STACK_BOTTOM;
604 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
605 if (edit->undo_stack[sp] == -2)
607 /* edit->undo_stack[sp] = '@'; */
608 edit->stack_pointer = sp;
610 else
611 edit->undo_stack[sp]++;
613 return c;
616 /* --------------------------------------------------------------------------------------------- */
617 /** is called whenever a modification is made by one of the four routines below */
619 static void
620 edit_modification (WEdit * edit)
622 edit->caches_valid = 0;
624 /* raise lock when file modified */
625 if (!edit->modified && !edit->delete_file)
626 edit->locked = edit_lock_file (edit);
627 edit->modified = 1;
630 /* --------------------------------------------------------------------------------------------- */
632 static void
633 edit_insert_over (WEdit * edit)
635 int i;
637 for (i = 0; i < edit->over_col; i++)
639 edit_insert (edit, ' ');
641 edit->over_col = 0;
644 /* --------------------------------------------------------------------------------------------- */
646 static int
647 edit_backspace (WEdit * edit, const int byte_delete)
649 int p = 0;
650 int cw = 1;
651 int i;
653 if (!edit->curs1)
654 return 0;
656 cw = 1;
658 if (edit->mark2 != edit->mark1)
659 edit_push_markers (edit);
661 if (edit->utf8 && byte_delete == 0)
663 edit_get_prev_utf (edit, edit->curs1, &cw);
664 if (cw < 1)
665 cw = 1;
667 for (i = 1; i <= cw; i++)
669 if (edit->mark1 >= edit->curs1)
671 edit->mark1--;
672 edit->end_mark_curs--;
674 if (edit->mark2 >= edit->curs1)
675 edit->mark2--;
676 if (edit->last_get_rule >= edit->curs1)
677 edit->last_get_rule--;
679 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
680 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
681 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
683 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
684 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
686 edit->last_byte--;
687 edit->curs1--;
688 edit_push_action (edit, p);
690 edit_modification (edit);
691 if (p == '\n')
693 if (edit->book_mark)
694 book_mark_dec (edit, edit->curs_line);
695 edit->curs_line--;
696 edit->total_lines--;
697 edit->force |= REDRAW_AFTER_CURSOR;
700 if (edit->curs1 < edit->start_display)
702 edit->start_display--;
703 if (p == '\n')
704 edit->start_line--;
707 return p;
711 /* --------------------------------------------------------------------------------------------- */
713 #ifdef FAST_MOVE_CURSOR
714 static void
715 memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
717 unsigned long next;
718 while ((next = (unsigned long) memccpy (dest, src, '\n', n)))
720 edit->curs_line--;
721 next -= (unsigned long) dest;
722 n -= next;
723 src += next;
724 dest += next;
727 #endif /* FAST_MOVE_CURSOR */
729 /* --------------------------------------------------------------------------------------------- */
730 /* high level cursor movement commands */
731 /* --------------------------------------------------------------------------------------------- */
733 static int
734 is_in_indent (WEdit * edit)
736 long p = edit_bol (edit, edit->curs1);
737 while (p < edit->curs1)
738 if (!strchr (" \t", edit_get_byte (edit, p++)))
739 return 0;
740 return 1;
743 /* --------------------------------------------------------------------------------------------- */
745 static int
746 is_blank (WEdit * edit, long offset)
748 long s, f;
749 int c;
750 s = edit_bol (edit, offset);
751 f = edit_eol (edit, offset) - 1;
752 while (s <= f)
754 c = edit_get_byte (edit, s++);
755 if (!isspace (c))
756 return 0;
758 return 1;
762 /* --------------------------------------------------------------------------------------------- */
763 /** returns the offset of line i */
765 static long
766 edit_find_line (WEdit * edit, int line)
768 int i, j = 0;
769 int m = 2000000000;
770 if (!edit->caches_valid)
772 for (i = 0; i < N_LINE_CACHES; i++)
773 edit->line_numbers[i] = edit->line_offsets[i] = 0;
774 /* three offsets that we *know* are line 0 at 0 and these two: */
775 edit->line_numbers[1] = edit->curs_line;
776 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
777 edit->line_numbers[2] = edit->total_lines;
778 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
779 edit->caches_valid = 1;
781 if (line >= edit->total_lines)
782 return edit->line_offsets[2];
783 if (line <= 0)
784 return 0;
785 /* find the closest known point */
786 for (i = 0; i < N_LINE_CACHES; i++)
788 int n;
789 n = abs (edit->line_numbers[i] - line);
790 if (n < m)
792 m = n;
793 j = i;
796 if (m == 0)
797 return edit->line_offsets[j]; /* know the offset exactly */
798 if (m == 1 && j >= 3)
799 i = j; /* one line different - caller might be looping, so stay in this cache */
800 else
801 i = 3 + (rand () % (N_LINE_CACHES - 3));
802 if (line > edit->line_numbers[j])
803 edit->line_offsets[i] =
804 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
805 else
806 edit->line_offsets[i] =
807 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
808 edit->line_numbers[i] = line;
809 return edit->line_offsets[i];
812 /* --------------------------------------------------------------------------------------------- */
813 /** moves up until a blank line is reached, or until just
814 before a non-blank line is reached */
816 static void
817 edit_move_up_paragraph (WEdit * edit, int do_scroll)
819 int i = 0;
820 if (edit->curs_line > 1)
822 if (line_is_blank (edit, edit->curs_line))
824 if (line_is_blank (edit, edit->curs_line - 1))
826 for (i = edit->curs_line - 1; i; i--)
827 if (!line_is_blank (edit, i))
829 i++;
830 break;
833 else
835 for (i = edit->curs_line - 1; i; i--)
836 if (line_is_blank (edit, i))
837 break;
840 else
842 for (i = edit->curs_line - 1; i; i--)
843 if (line_is_blank (edit, i))
844 break;
847 edit_move_up (edit, edit->curs_line - i, do_scroll);
850 /* --------------------------------------------------------------------------------------------- */
851 /** moves down until a blank line is reached, or until just
852 before a non-blank line is reached */
854 static void
855 edit_move_down_paragraph (WEdit * edit, int do_scroll)
857 int i;
858 if (edit->curs_line >= edit->total_lines - 1)
860 i = edit->total_lines;
862 else
864 if (line_is_blank (edit, edit->curs_line))
866 if (line_is_blank (edit, edit->curs_line + 1))
868 for (i = edit->curs_line + 1; i; i++)
869 if (!line_is_blank (edit, i) || i > edit->total_lines)
871 i--;
872 break;
875 else
877 for (i = edit->curs_line + 1; i; i++)
878 if (line_is_blank (edit, i) || i >= edit->total_lines)
879 break;
882 else
884 for (i = edit->curs_line + 1; i; i++)
885 if (line_is_blank (edit, i) || i >= edit->total_lines)
886 break;
889 edit_move_down (edit, i - edit->curs_line, do_scroll);
892 /* --------------------------------------------------------------------------------------------- */
894 static void
895 edit_begin_page (WEdit * edit)
897 edit_update_curs_row (edit);
898 edit_move_up (edit, edit->curs_row, 0);
901 /* --------------------------------------------------------------------------------------------- */
903 static void
904 edit_end_page (WEdit * edit)
906 edit_update_curs_row (edit);
907 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
911 /* --------------------------------------------------------------------------------------------- */
912 /** goto beginning of text */
914 static void
915 edit_move_to_top (WEdit * edit)
917 if (edit->curs_line)
919 edit_cursor_move (edit, -edit->curs1);
920 edit_move_to_prev_col (edit, 0);
921 edit->force |= REDRAW_PAGE;
922 edit->search_start = 0;
923 edit_update_curs_row (edit);
928 /* --------------------------------------------------------------------------------------------- */
929 /** goto end of text */
931 static void
932 edit_move_to_bottom (WEdit * edit)
934 if (edit->curs_line < edit->total_lines)
936 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
937 edit->start_display = edit->last_byte;
938 edit->start_line = edit->total_lines;
939 edit_scroll_upward (edit, edit->widget.lines - 1);
940 edit->force |= REDRAW_PAGE;
944 /* --------------------------------------------------------------------------------------------- */
945 /** goto beginning of line */
947 static void
948 edit_cursor_to_bol (WEdit * edit)
950 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
951 edit->search_start = edit->curs1;
952 edit->prev_col = edit_get_col (edit);
953 edit->over_col = 0;
956 /* --------------------------------------------------------------------------------------------- */
957 /** goto end of line */
959 static void
960 edit_cursor_to_eol (WEdit * edit)
962 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
963 edit->search_start = edit->curs1;
964 edit->prev_col = edit_get_col (edit);
965 edit->over_col = 0;
968 /* --------------------------------------------------------------------------------------------- */
970 static unsigned long
971 my_type_of (int c)
973 int x, r = 0;
974 const char *p, *q;
975 const char option_chars_move_whole_word[] =
976 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
978 if (!c)
979 return 0;
980 if (c == '!')
982 if (*option_chars_move_whole_word == '!')
983 return 2;
984 return 0x80000000UL;
986 if (g_ascii_isupper ((gchar) c))
987 c = 'A';
988 else if (g_ascii_islower ((gchar) c))
989 c = 'a';
990 else if (g_ascii_isalpha (c))
991 c = 'a';
992 else if (isdigit (c))
993 c = '0';
994 else if (isspace (c))
995 c = ' ';
996 q = strchr (option_chars_move_whole_word, c);
997 if (!q)
998 return 0xFFFFFFFFUL;
1001 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1002 if (*p == '!')
1003 x <<= 1;
1004 r |= x;
1006 while ((q = strchr (q + 1, c)));
1007 return r;
1010 /* --------------------------------------------------------------------------------------------- */
1012 static void
1013 edit_left_word_move (WEdit * edit, int s)
1015 for (;;)
1017 int c1, c2;
1018 if (edit->column_highlight
1019 && edit->mark1 != edit->mark2
1020 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1021 break;
1022 edit_cursor_move (edit, -1);
1023 if (!edit->curs1)
1024 break;
1025 c1 = edit_get_byte (edit, edit->curs1 - 1);
1026 c2 = edit_get_byte (edit, edit->curs1);
1027 if (c1 == '\n' || c2 == '\n')
1028 break;
1029 if (!(my_type_of (c1) & my_type_of (c2)))
1030 break;
1031 if (isspace (c1) && !isspace (c2))
1032 break;
1033 if (s)
1034 if (!isspace (c1) && isspace (c2))
1035 break;
1039 /* --------------------------------------------------------------------------------------------- */
1041 static void
1042 edit_left_word_move_cmd (WEdit * edit)
1044 edit_left_word_move (edit, 0);
1045 edit->force |= REDRAW_PAGE;
1048 /* --------------------------------------------------------------------------------------------- */
1050 static void
1051 edit_right_word_move (WEdit * edit, int s)
1053 for (;;)
1055 int c1, c2;
1056 if (edit->column_highlight
1057 && edit->mark1 != edit->mark2
1058 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1059 break;
1060 edit_cursor_move (edit, 1);
1061 if (edit->curs1 >= edit->last_byte)
1062 break;
1063 c1 = edit_get_byte (edit, edit->curs1 - 1);
1064 c2 = edit_get_byte (edit, edit->curs1);
1065 if (c1 == '\n' || c2 == '\n')
1066 break;
1067 if (!(my_type_of (c1) & my_type_of (c2)))
1068 break;
1069 if (isspace (c1) && !isspace (c2))
1070 break;
1071 if (s)
1072 if (!isspace (c1) && isspace (c2))
1073 break;
1077 /* --------------------------------------------------------------------------------------------- */
1079 static void
1080 edit_right_word_move_cmd (WEdit * edit)
1082 edit_right_word_move (edit, 0);
1083 edit->force |= REDRAW_PAGE;
1086 /* --------------------------------------------------------------------------------------------- */
1088 static void
1089 edit_right_char_move_cmd (WEdit * edit)
1091 int cw = 1;
1092 int c = 0;
1093 if (edit->utf8)
1095 c = edit_get_utf (edit, edit->curs1, &cw);
1096 if (cw < 1)
1097 cw = 1;
1099 else
1101 c = edit_get_byte (edit, edit->curs1);
1103 if (option_cursor_beyond_eol && c == '\n')
1105 edit->over_col++;
1107 else
1109 edit_cursor_move (edit, cw);
1113 /* --------------------------------------------------------------------------------------------- */
1115 static void
1116 edit_left_char_move_cmd (WEdit * edit)
1118 int cw = 1;
1119 if (edit->column_highlight
1120 && option_cursor_beyond_eol
1121 && edit->mark1 != edit->mark2
1122 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1123 return;
1124 if (edit->utf8)
1126 edit_get_prev_utf (edit, edit->curs1, &cw);
1127 if (cw < 1)
1128 cw = 1;
1130 if (option_cursor_beyond_eol && edit->over_col > 0)
1132 edit->over_col--;
1134 else
1136 edit_cursor_move (edit, -cw);
1140 /* --------------------------------------------------------------------------------------------- */
1141 /** Up or down cursor moving.
1142 direction = TRUE - move up
1143 = FALSE - move down
1146 static void
1147 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
1149 unsigned long p;
1150 unsigned long l = (direction) ? edit->curs_line : edit->total_lines - edit->curs_line;
1152 if (i > l)
1153 i = l;
1155 if (i == 0)
1156 return;
1158 if (i > 1)
1159 edit->force |= REDRAW_PAGE;
1160 if (do_scroll)
1162 if (direction)
1163 edit_scroll_upward (edit, i);
1164 else
1165 edit_scroll_downward (edit, i);
1167 p = edit_bol (edit, edit->curs1);
1169 p = (direction) ? edit_move_backward (edit, p, i) : edit_move_forward (edit, p, i, 0);
1171 edit_cursor_move (edit, p - edit->curs1);
1173 edit_move_to_prev_col (edit, p);
1175 /* search start of current multibyte char (like CJK) */
1176 if (edit->curs1 + 1 < edit->last_byte)
1178 edit_right_char_move_cmd (edit);
1179 edit_left_char_move_cmd (edit);
1182 edit->search_start = edit->curs1;
1183 edit->found_len = 0;
1186 /* --------------------------------------------------------------------------------------------- */
1188 static void
1189 edit_right_delete_word (WEdit * edit)
1191 int c1, c2;
1192 for (;;)
1194 if (edit->curs1 >= edit->last_byte)
1195 break;
1196 c1 = edit_delete (edit, 1);
1197 c2 = edit_get_byte (edit, edit->curs1);
1198 if (c1 == '\n' || c2 == '\n')
1199 break;
1200 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1201 break;
1202 if (!(my_type_of (c1) & my_type_of (c2)))
1203 break;
1207 /* --------------------------------------------------------------------------------------------- */
1209 static void
1210 edit_left_delete_word (WEdit * edit)
1212 int c1, c2;
1213 for (;;)
1215 if (edit->curs1 <= 0)
1216 break;
1217 c1 = edit_backspace (edit, 1);
1218 c2 = edit_get_byte (edit, edit->curs1 - 1);
1219 if (c1 == '\n' || c2 == '\n')
1220 break;
1221 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1222 break;
1223 if (!(my_type_of (c1) & my_type_of (c2)))
1224 break;
1228 /* --------------------------------------------------------------------------------------------- */
1230 the start column position is not recorded, and hence does not
1231 undo as it happed. But who would notice.
1234 static void
1235 edit_do_undo (WEdit * edit)
1237 long ac;
1238 long count = 0;
1240 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1241 edit->over_col = 0;
1242 while ((ac = pop_action (edit)) < KEY_PRESS)
1244 switch ((int) ac)
1246 case STACK_BOTTOM:
1247 goto done_undo;
1248 case CURS_RIGHT:
1249 edit_cursor_move (edit, 1);
1250 break;
1251 case CURS_LEFT:
1252 edit_cursor_move (edit, -1);
1253 break;
1254 case BACKSPACE:
1255 edit_backspace (edit, 1);
1256 break;
1257 case DELCHAR:
1258 edit_delete (edit, 1);
1259 break;
1260 case COLUMN_ON:
1261 edit->column_highlight = 1;
1262 break;
1263 case COLUMN_OFF:
1264 edit->column_highlight = 0;
1265 break;
1267 if (ac >= 256 && ac < 512)
1268 edit_insert_ahead (edit, ac - 256);
1269 if (ac >= 0 && ac < 256)
1270 edit_insert (edit, ac);
1272 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1274 edit->mark1 = ac - MARK_1;
1275 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1277 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1279 edit->mark2 = ac - MARK_2;
1280 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1282 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1284 edit->end_mark_curs = ac - MARK_CURS;
1286 if (count++)
1287 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1290 if (edit->start_display > ac - KEY_PRESS)
1292 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1293 edit->force |= REDRAW_PAGE;
1295 else if (edit->start_display < ac - KEY_PRESS)
1297 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1298 edit->force |= REDRAW_PAGE;
1300 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1301 edit_update_curs_row (edit);
1303 done_undo:;
1304 edit->stack_disable = 0;
1307 /* --------------------------------------------------------------------------------------------- */
1309 static void
1310 edit_delete_to_line_end (WEdit * edit)
1312 while (edit_get_byte (edit, edit->curs1) != '\n')
1314 if (!edit->curs2)
1315 break;
1316 edit_delete (edit, 1);
1320 /* --------------------------------------------------------------------------------------------- */
1322 static void
1323 edit_delete_to_line_begin (WEdit * edit)
1325 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
1327 if (!edit->curs1)
1328 break;
1329 edit_backspace (edit, 1);
1333 /* --------------------------------------------------------------------------------------------- */
1335 static int
1336 is_aligned_on_a_tab (WEdit * edit)
1338 edit_update_curs_col (edit);
1339 return !((edit->curs_col % (TAB_SIZE * space_width))
1340 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
1343 /* --------------------------------------------------------------------------------------------- */
1345 static int
1346 right_of_four_spaces (WEdit * edit)
1348 int i, ch = 0;
1349 for (i = 1; i <= HALF_TAB_SIZE; i++)
1350 ch |= edit_get_byte (edit, edit->curs1 - i);
1351 if (ch == ' ')
1352 return is_aligned_on_a_tab (edit);
1353 return 0;
1356 /* --------------------------------------------------------------------------------------------- */
1358 static int
1359 left_of_four_spaces (WEdit * edit)
1361 int i, ch = 0;
1362 for (i = 0; i < HALF_TAB_SIZE; i++)
1363 ch |= edit_get_byte (edit, edit->curs1 + i);
1364 if (ch == ' ')
1365 return is_aligned_on_a_tab (edit);
1366 return 0;
1369 /* --------------------------------------------------------------------------------------------- */
1371 static void
1372 edit_auto_indent (WEdit * edit)
1374 long p;
1375 char c;
1376 p = edit->curs1;
1377 /* use the previous line as a template */
1378 p = edit_move_backward (edit, p, 1);
1379 /* copy the leading whitespace of the line */
1380 for (;;)
1381 { /* no range check - the line _is_ \n-terminated */
1382 c = edit_get_byte (edit, p++);
1383 if (c != ' ' && c != '\t')
1384 break;
1385 edit_insert (edit, c);
1389 /* --------------------------------------------------------------------------------------------- */
1391 static inline void
1392 edit_double_newline (WEdit * edit)
1394 edit_insert (edit, '\n');
1395 if (edit_get_byte (edit, edit->curs1) == '\n')
1396 return;
1397 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1398 return;
1399 edit->force |= REDRAW_PAGE;
1400 edit_insert (edit, '\n');
1403 /* --------------------------------------------------------------------------------------------- */
1405 static inline void
1406 edit_tab_cmd (WEdit * edit)
1408 int i;
1410 if (option_fake_half_tabs)
1412 if (is_in_indent (edit))
1414 /*insert a half tab (usually four spaces) unless there is a
1415 half tab already behind, then delete it and insert a
1416 full tab. */
1417 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit))
1419 for (i = 1; i <= HALF_TAB_SIZE; i++)
1420 edit_backspace (edit, 1);
1421 edit_insert (edit, '\t');
1423 else
1425 insert_spaces_tab (edit, 1);
1427 return;
1430 if (option_fill_tabs_with_spaces)
1432 insert_spaces_tab (edit, 0);
1434 else
1436 edit_insert (edit, '\t');
1440 /* --------------------------------------------------------------------------------------------- */
1442 static void
1443 check_and_wrap_line (WEdit * edit)
1445 int curs, c;
1446 if (!option_typewriter_wrap)
1447 return;
1448 edit_update_curs_col (edit);
1449 if (edit->curs_col < option_word_wrap_line_length)
1450 return;
1451 curs = edit->curs1;
1452 for (;;)
1454 curs--;
1455 c = edit_get_byte (edit, curs);
1456 if (c == '\n' || curs <= 0)
1458 edit_insert (edit, '\n');
1459 return;
1461 if (c == ' ' || c == '\t')
1463 int current = edit->curs1;
1464 edit_cursor_move (edit, curs - edit->curs1 + 1);
1465 edit_insert (edit, '\n');
1466 edit_cursor_move (edit, current - edit->curs1 + 1);
1467 return;
1472 /* --------------------------------------------------------------------------------------------- */
1473 /** this find the matching bracket in either direction, and sets edit->bracket */
1475 static long
1476 edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1478 const char *const b = "{}{[][()(", *p;
1479 int i = 1, a, inc = -1, c, d, n = 0;
1480 unsigned long j = 0;
1481 long q;
1482 edit_update_curs_row (edit);
1483 c = edit_get_byte (edit, edit->curs1);
1484 p = strchr (b, c);
1485 /* no limit */
1486 if (!furthest_bracket_search)
1487 furthest_bracket_search--;
1488 /* not on a bracket at all */
1489 if (!p)
1490 return -1;
1491 /* the matching bracket */
1492 d = p[1];
1493 /* going left or right? */
1494 if (strchr ("{[(", c))
1495 inc = 1;
1496 for (q = edit->curs1 + inc;; q += inc)
1498 /* out of buffer? */
1499 if (q >= edit->last_byte || q < 0)
1500 break;
1501 a = edit_get_byte (edit, q);
1502 /* don't want to eat CPU */
1503 if (j++ > furthest_bracket_search)
1504 break;
1505 /* out of screen? */
1506 if (in_screen)
1508 if (q < edit->start_display)
1509 break;
1510 /* count lines if searching downward */
1511 if (inc > 0 && a == '\n')
1512 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1513 break;
1515 /* count bracket depth */
1516 i += (a == c) - (a == d);
1517 /* return if bracket depth is zero */
1518 if (!i)
1519 return q;
1521 /* no match */
1522 return -1;
1525 /* --------------------------------------------------------------------------------------------- */
1527 static inline void
1528 edit_goto_matching_bracket (WEdit * edit)
1530 long q;
1532 q = edit_get_bracket (edit, 0, 0);
1533 if (q >= 0)
1535 edit->bracket = edit->curs1;
1536 edit->force |= REDRAW_PAGE;
1537 edit_cursor_move (edit, q - edit->curs1);
1541 /* --------------------------------------------------------------------------------------------- */
1543 static void
1544 edit_execute_macro (WEdit * edit, struct macro macro[], int n)
1546 int i = 0;
1548 if (edit->macro_depth++ > 256)
1550 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
1551 edit->macro_depth--;
1552 return;
1554 edit->force |= REDRAW_PAGE;
1555 for (; i < n; i++)
1557 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
1559 edit_update_screen (edit);
1560 edit->macro_depth--;
1563 /* --------------------------------------------------------------------------------------------- */
1564 /** User edit menu, like user menu (F2) but only in editor. */
1566 static void
1567 user_menu (WEdit * edit)
1569 char *block_file;
1570 int nomark;
1571 long start_mark, end_mark;
1572 struct stat status;
1574 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
1576 nomark = eval_marks (edit, &start_mark, &end_mark);
1577 if (nomark == 0)
1578 edit_save_block (edit, block_file, start_mark, end_mark);
1580 /* run shell scripts from menu */
1581 user_menu_cmd (edit);
1583 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0))
1585 int rc = 0;
1586 FILE *fd;
1588 if (nomark == 0)
1590 /* i.e. we have marked block */
1591 rc = edit_block_delete_cmd (edit);
1594 if (rc == 0)
1595 edit_insert_file (edit, block_file);
1597 /* truncate block file */
1598 fd = fopen (block_file, "w");
1599 if (fd != NULL)
1600 fclose (fd);
1602 edit_refresh_cmd (edit);
1603 edit->force |= REDRAW_COMPLETELY;
1605 g_free (block_file);
1608 /* --------------------------------------------------------------------------------------------- */
1609 /*** public functions ****************************************************************************/
1610 /* --------------------------------------------------------------------------------------------- */
1613 edit_get_byte (WEdit * edit, long byte_index)
1615 unsigned long p;
1616 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1617 return '\n';
1619 if (byte_index >= edit->curs1)
1621 p = edit->curs1 + edit->curs2 - byte_index - 1;
1622 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1624 else
1626 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1630 /* --------------------------------------------------------------------------------------------- */
1632 char *
1633 edit_get_byte_ptr (WEdit * edit, long byte_index)
1635 unsigned long p;
1636 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1637 return NULL;
1639 if (byte_index >= edit->curs1)
1641 p = edit->curs1 + edit->curs2 - byte_index - 1;
1642 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1643 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1645 else
1647 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
1648 (byte_index & M_EDIT_BUF_SIZE));
1652 /* --------------------------------------------------------------------------------------------- */
1654 char *
1655 edit_get_buf_ptr (WEdit * edit, long byte_index)
1657 unsigned long p;
1659 if (byte_index >= (edit->curs1 + edit->curs2))
1660 byte_index--;
1662 if (byte_index < 0)
1663 return NULL;
1665 if (byte_index >= edit->curs1)
1667 p = edit->curs1 + edit->curs2 - 1;
1668 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
1669 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
1671 else
1673 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
1677 /* --------------------------------------------------------------------------------------------- */
1680 edit_get_utf (WEdit * edit, long byte_index, int *char_width)
1682 gchar *str = NULL;
1683 int res = -1;
1684 gunichar ch;
1685 gchar *next_ch = NULL;
1686 int width = 0;
1688 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1690 *char_width = 0;
1691 return '\n';
1694 str = edit_get_byte_ptr (edit, byte_index);
1696 if (str == NULL)
1698 *char_width = 0;
1699 return 0;
1702 res = g_utf8_get_char_validated (str, -1);
1704 if (res < 0)
1706 ch = *str;
1707 width = 0;
1709 else
1711 ch = res;
1712 /* Calculate UTF-8 char width */
1713 next_ch = g_utf8_next_char (str);
1714 if (next_ch)
1716 width = next_ch - str;
1718 else
1720 ch = 0;
1721 width = 0;
1724 *char_width = width;
1725 return ch;
1728 /* --------------------------------------------------------------------------------------------- */
1731 edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
1733 gchar *str, *buf = NULL;
1734 int res = -1;
1735 gunichar ch;
1736 gchar *next_ch = NULL;
1737 int width = 0;
1739 if (byte_index > 0)
1740 byte_index--;
1742 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1744 *char_width = 0;
1745 return 0;
1748 ch = edit_get_utf (edit, byte_index, &width);
1750 if (width == 1)
1752 *char_width = width;
1753 return ch;
1756 str = edit_get_byte_ptr (edit, byte_index);
1757 buf = edit_get_buf_ptr (edit, byte_index);
1758 if (str == NULL || buf == NULL)
1760 *char_width = 0;
1761 return 0;
1763 /* get prev utf8 char */
1764 if (str != buf)
1765 str = g_utf8_find_prev_char (buf, str);
1767 if (str == NULL)
1769 *char_width = 0;
1770 return 0;
1772 else
1773 res = g_utf8_get_char_validated (str, -1);
1775 if (res < 0)
1777 ch = *str;
1778 width = 0;
1780 else
1782 ch = res;
1783 /* Calculate UTF-8 char width */
1784 next_ch = g_utf8_next_char (str);
1785 if (next_ch)
1787 width = next_ch - str;
1789 else
1791 ch = 0;
1792 width = 0;
1795 *char_width = width;
1796 return ch;
1799 /* --------------------------------------------------------------------------------------------- */
1801 char *
1802 edit_get_write_filter (const char *write_name, const char *filename)
1804 int i;
1805 char *p, *writename;
1807 i = edit_find_filter (filename);
1808 if (i < 0)
1809 return NULL;
1811 writename = name_quote (write_name, 0);
1812 p = g_strdup_printf (all_filters[i].write, writename);
1813 g_free (writename);
1814 return p;
1817 /* --------------------------------------------------------------------------------------------- */
1819 long
1820 edit_write_stream (WEdit * edit, FILE * f)
1822 long i;
1824 if (edit->lb == LB_ASIS)
1826 for (i = 0; i < edit->last_byte; i++)
1827 if (fputc (edit_get_byte (edit, i), f) < 0)
1828 break;
1829 return i;
1832 /* change line breaks */
1833 for (i = 0; i < edit->last_byte; i++)
1835 unsigned char c = edit_get_byte (edit, i);
1837 if (!(c == '\n' || c == '\r'))
1839 /* not line break */
1840 if (fputc (c, f) < 0)
1841 return i;
1843 else
1844 { /* (c == '\n' || c == '\r') */
1845 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
1847 switch (edit->lb)
1849 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
1850 /* put one line break unconditionally */
1851 if (fputc ('\n', f) < 0)
1852 return i;
1854 i++; /* 2 chars are processed */
1856 if (c == '\r' && c1 == '\n')
1857 /* Windows line break; go to the next char */
1858 break;
1860 if (c == '\r' && c1 == '\r')
1862 /* two Macintosh line breaks; put second line break */
1863 if (fputc ('\n', f) < 0)
1864 return i;
1865 break;
1868 if (fputc (c1, f) < 0)
1869 return i;
1870 break;
1872 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
1873 /* put one line break unconditionally */
1874 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1875 return i;
1877 if (c == '\r' && c1 == '\n')
1878 /* Windows line break; go to the next char */
1879 i++;
1880 break;
1882 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
1883 /* put one line break unconditionally */
1884 if (fputc ('\r', f) < 0)
1885 return i;
1887 i++; /* 2 chars are processed */
1889 if (c == '\r' && c1 == '\n')
1890 /* Windows line break; go to the next char */
1891 break;
1893 if (c == '\n' && c1 == '\n')
1895 /* two Windows line breaks; put second line break */
1896 if (fputc ('\r', f) < 0)
1897 return i;
1898 break;
1901 if (fputc (c1, f) < 0)
1902 return i;
1903 break;
1904 case LB_ASIS: /* default without changes */
1905 break;
1910 return edit->last_byte;
1913 /* --------------------------------------------------------------------------------------------- */
1914 /** inserts a file at the cursor, returns 1 on success */
1916 edit_insert_file (WEdit * edit, const char *filename)
1918 char *p;
1920 p = edit_get_filter (filename);
1921 if (p != NULL)
1923 FILE *f;
1924 long current = edit->curs1;
1925 f = (FILE *) popen (p, "r");
1926 if (f != NULL)
1928 edit_insert_stream (edit, f);
1929 edit_cursor_move (edit, current - edit->curs1);
1930 if (pclose (f) > 0)
1932 char *errmsg;
1933 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
1934 edit_error_dialog (_("Error"), errmsg);
1935 g_free (errmsg);
1936 g_free (p);
1937 return 0;
1940 else
1942 char *errmsg;
1943 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
1944 edit_error_dialog (_("Error"), errmsg);
1945 g_free (errmsg);
1946 g_free (p);
1947 return 0;
1949 g_free (p);
1951 else
1953 int i, file, blocklen;
1954 long current = edit->curs1;
1955 int vertical_insertion = 0;
1956 char *buf;
1957 file = mc_open (filename, O_RDONLY | O_BINARY);
1958 if (file == -1)
1959 return 0;
1960 buf = g_malloc0 (TEMP_BUF_LEN);
1961 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
1962 if (blocklen > 0)
1964 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
1965 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
1966 vertical_insertion = 1;
1967 else
1968 mc_lseek (file, 0, SEEK_SET);
1970 if (vertical_insertion)
1971 blocklen = edit_insert_column_of_text_from_file (edit, file);
1972 else
1973 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
1974 for (i = 0; i < blocklen; i++)
1975 edit_insert (edit, buf[i]);
1976 edit_cursor_move (edit, current - edit->curs1);
1977 g_free (buf);
1978 mc_close (file);
1979 if (blocklen != 0)
1980 return 0;
1982 return 1;
1985 /* --------------------------------------------------------------------------------------------- */
1987 * Fill in the edit structure. Return NULL on failure. Pass edit as
1988 * NULL to allocate a new structure.
1990 * If line is 0, try to restore saved position. Otherwise put the
1991 * cursor on that line and show it in the middle of the screen.
1994 WEdit *
1995 edit_init (WEdit * edit, int lines, int columns, const char *filename, long line)
1997 int to_free = 0;
1998 option_auto_syntax = 1; /* Resetting to auto on every invokation */
1999 if (option_line_state)
2001 option_line_state_width = LINE_STATE_WIDTH;
2003 else
2005 option_line_state_width = 0;
2007 if (!edit)
2009 #ifdef ENABLE_NLS
2011 * Expand option_whole_chars_search by national letters using
2012 * current locale
2015 static char option_whole_chars_search_buf[256];
2017 if (option_whole_chars_search_buf != option_whole_chars_search)
2019 size_t i;
2020 size_t len = str_term_width1 (option_whole_chars_search);
2022 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2024 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2026 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2028 option_whole_chars_search_buf[len++] = i;
2032 option_whole_chars_search_buf[len] = 0;
2033 option_whole_chars_search = option_whole_chars_search_buf;
2035 #endif /* ENABLE_NLS */
2036 edit = g_malloc0 (sizeof (WEdit));
2037 edit->search = NULL;
2038 to_free = 1;
2040 edit_purge_widget (edit);
2041 edit->widget.lines = lines;
2042 edit->over_col = 0;
2043 edit->widget.cols = columns;
2044 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2045 edit->stat1.st_uid = getuid ();
2046 edit->stat1.st_gid = getgid ();
2047 edit->stat1.st_mtime = 0;
2048 edit->bracket = -1;
2049 edit->force |= REDRAW_PAGE;
2050 edit_set_filename (edit, filename);
2051 edit->stack_size = START_STACK_SIZE;
2052 edit->stack_size_mask = START_STACK_SIZE - 1;
2053 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
2055 edit->utf8 = 0;
2056 edit->converter = str_cnv_from_term;
2057 edit_set_codeset (edit);
2059 if (edit_load_file (edit))
2061 /* edit_load_file already gives an error message */
2062 if (to_free)
2063 g_free (edit);
2064 return 0;
2067 edit->loading_done = 1;
2068 edit->modified = 0;
2069 edit->locked = 0;
2070 edit_load_syntax (edit, NULL, NULL);
2072 int color;
2073 edit_get_syntax_color (edit, -1, &color);
2076 /* load saved cursor position */
2077 if ((line == 0) && option_save_position)
2079 edit_load_position (edit);
2081 else
2083 if (line <= 0)
2084 line = 1;
2085 edit_move_display (edit, line - 1);
2086 edit_move_to_line (edit, line - 1);
2089 edit_set_keymap ();
2091 return edit;
2094 /* --------------------------------------------------------------------------------------------- */
2095 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2098 edit_clean (WEdit * edit)
2100 int j = 0;
2102 if (!edit)
2103 return 0;
2105 /* a stale lock, remove it */
2106 if (edit->locked)
2107 edit->locked = edit_unlock_file (edit);
2109 /* save cursor position */
2110 if (option_save_position)
2111 edit_save_position (edit);
2112 else if (edit->serialized_bookmarks != NULL)
2113 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2115 /* File specified on the mcedit command line and never saved */
2116 if (edit->delete_file)
2117 unlink (edit->filename);
2119 edit_free_syntax_rules (edit);
2120 book_mark_flush (edit, -1);
2121 for (; j <= MAXBUFF; j++)
2123 g_free (edit->buffers1[j]);
2124 g_free (edit->buffers2[j]);
2127 g_free (edit->undo_stack);
2128 g_free (edit->filename);
2129 g_free (edit->dir);
2131 mc_search_free (edit->search);
2132 edit->search = NULL;
2134 if (edit->converter != str_cnv_from_term)
2135 str_close_conv (edit->converter);
2137 edit_purge_widget (edit);
2139 return 1;
2142 /* --------------------------------------------------------------------------------------------- */
2143 /** returns 1 on success */
2146 edit_renew (WEdit * edit)
2148 int lines = edit->widget.lines;
2149 int columns = edit->widget.cols;
2151 edit_clean (edit);
2152 return (edit_init (edit, lines, columns, "", 0) != NULL);
2155 /* --------------------------------------------------------------------------------------------- */
2157 * Load a new file into the editor. If it fails, preserve the old file.
2158 * To do it, allocate a new widget, initialize it and, if the new file
2159 * was loaded, copy the data to the old widget.
2160 * Return 1 on success, 0 on failure.
2164 edit_reload (WEdit * edit, const char *filename)
2166 WEdit *e;
2167 int lines = edit->widget.lines;
2168 int columns = edit->widget.cols;
2170 e = g_malloc0 (sizeof (WEdit));
2171 e->widget = edit->widget;
2172 if (!edit_init (e, lines, columns, filename, 0))
2174 g_free (e);
2175 return 0;
2177 edit_clean (edit);
2178 memcpy (edit, e, sizeof (WEdit));
2179 g_free (e);
2180 return 1;
2183 /* --------------------------------------------------------------------------------------------- */
2185 * Load a new file into the editor and set line. If it fails, preserve the old file.
2186 * To do it, allocate a new widget, initialize it and, if the new file
2187 * was loaded, copy the data to the old widget.
2188 * Return 1 on success, 0 on failure.
2192 edit_reload_line (WEdit * edit, const char *filename, long line)
2194 WEdit *e;
2195 int lines = edit->widget.lines;
2196 int columns = edit->widget.cols;
2198 e = g_malloc0 (sizeof (WEdit));
2199 e->widget = edit->widget;
2200 if (!edit_init (e, lines, columns, filename, line))
2202 g_free (e);
2203 return 0;
2205 edit_clean (edit);
2206 memcpy (edit, e, sizeof (WEdit));
2207 g_free (e);
2208 return 1;
2211 /* --------------------------------------------------------------------------------------------- */
2213 void
2214 edit_set_codeset (WEdit * edit)
2216 #ifdef HAVE_CHARSET
2217 const char *cp_id;
2219 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
2221 if (cp_id != NULL)
2223 GIConv conv;
2224 conv = str_crt_conv_from (cp_id);
2225 if (conv != INVALID_CONV)
2227 if (edit->converter != str_cnv_from_term)
2228 str_close_conv (edit->converter);
2229 edit->converter = conv;
2233 if (cp_id != NULL)
2234 edit->utf8 = str_isutf8 (cp_id);
2235 #else
2236 (void) edit;
2237 #endif
2241 /* --------------------------------------------------------------------------------------------- */
2243 Recording stack for undo:
2244 The following is an implementation of a compressed stack. Identical
2245 pushes are recorded by a negative prefix indicating the number of times the
2246 same char was pushed. This saves space for repeated curs-left or curs-right
2247 delete etc.
2251 pushed: stored:
2255 b -3
2257 c --> -4
2263 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2264 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2265 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2266 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2267 position.
2269 The only way the cursor moves or the buffer is changed is through the routines:
2270 insert, backspace, insert_ahead, delete, and cursor_move.
2271 These record the reverse undo movements onto the stack each time they are
2272 called.
2274 Each key press results in a set of actions (insert; delete ...). So each time
2275 a key is pressed the current position of start_display is pushed as
2276 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2277 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2278 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2282 void
2283 edit_push_action (WEdit * edit, long c, ...)
2285 unsigned long sp = edit->stack_pointer;
2286 unsigned long spm1;
2287 long *t;
2289 /* first enlarge the stack if necessary */
2290 if (sp > edit->stack_size - 10)
2291 { /* say */
2292 if (option_max_undo < 256)
2293 option_max_undo = 256;
2294 if (edit->stack_size < (unsigned long) option_max_undo)
2296 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
2297 if (t)
2299 edit->undo_stack = t;
2300 edit->stack_size <<= 1;
2301 edit->stack_size_mask = edit->stack_size - 1;
2305 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
2306 if (edit->stack_disable)
2307 return;
2309 #ifdef FAST_MOVE_CURSOR
2310 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS)
2312 va_list ap;
2313 edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
2314 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
2315 va_start (ap, c);
2316 c = -(va_arg (ap, int));
2317 va_end (ap);
2319 else
2320 #endif /* ! FAST_MOVE_CURSOR */
2321 if (edit->stack_bottom != sp
2322 && spm1 != edit->stack_bottom
2323 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
2325 int d;
2326 if (edit->undo_stack[spm1] < 0)
2328 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
2329 if (d == c)
2331 if (edit->undo_stack[spm1] > -1000000000)
2333 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2334 edit->undo_stack[spm1]--;
2335 return;
2338 /* #define NO_STACK_CURSMOVE_ANIHILATION */
2339 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2340 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
2341 { /* a left then a right anihilate each other */
2342 if (edit->undo_stack[spm1] == -2)
2343 edit->stack_pointer = spm1;
2344 else
2345 edit->undo_stack[spm1]++;
2346 return;
2348 #endif
2350 else
2352 d = edit->undo_stack[spm1];
2353 if (d == c)
2355 if (c >= KEY_PRESS)
2356 return; /* --> no need to push multiple do-nothings */
2357 edit->undo_stack[sp] = -2;
2358 goto check_bottom;
2360 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2361 else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
2362 { /* a left then a right anihilate each other */
2363 edit->stack_pointer = spm1;
2364 return;
2366 #endif
2369 edit->undo_stack[sp] = c;
2371 check_bottom:
2372 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
2374 /* if the sp wraps round and catches the stack_bottom then erase
2375 * the first set of actions on the stack to make space - by moving
2376 * stack_bottom forward one "key press" */
2377 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
2378 if ((unsigned long) c == edit->stack_bottom ||
2379 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
2382 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
2384 while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS
2385 && edit->stack_bottom != edit->stack_pointer);
2387 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2388 if (edit->stack_pointer != edit->stack_bottom
2389 && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
2390 edit->stack_bottom = edit->stack_pointer = 0;
2393 /* --------------------------------------------------------------------------------------------- */
2395 Basic low level single character buffer alterations and movements at the cursor.
2396 Returns char passed over, inserted or removed.
2399 void
2400 edit_insert (WEdit * edit, int c)
2402 /* check if file has grown to large */
2403 if (edit->last_byte >= SIZE_LIMIT)
2404 return;
2406 /* first we must update the position of the display window */
2407 if (edit->curs1 < edit->start_display)
2409 edit->start_display++;
2410 if (c == '\n')
2411 edit->start_line++;
2414 /* Mark file as modified, unless the file hasn't been fully loaded */
2415 if (edit->loading_done)
2417 edit_modification (edit);
2420 /* now we must update some info on the file and check if a redraw is required */
2421 if (c == '\n')
2423 if (edit->book_mark)
2424 book_mark_inc (edit, edit->curs_line);
2425 edit->curs_line++;
2426 edit->total_lines++;
2427 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2430 /* save the reverse command onto the undo stack */
2431 edit_push_action (edit, BACKSPACE);
2433 /* update markers */
2434 edit->mark1 += (edit->mark1 > edit->curs1);
2435 edit->mark2 += (edit->mark2 > edit->curs1);
2436 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2438 /* add a new buffer if we've reached the end of the last one */
2439 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2440 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2442 /* perform the insertion */
2443 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2444 = (unsigned char) c;
2446 /* update file length */
2447 edit->last_byte++;
2449 /* update cursor position */
2450 edit->curs1++;
2453 /* --------------------------------------------------------------------------------------------- */
2454 /** same as edit_insert and move left */
2456 void
2457 edit_insert_ahead (WEdit * edit, int c)
2459 if (edit->last_byte >= SIZE_LIMIT)
2460 return;
2462 if (edit->curs1 < edit->start_display)
2464 edit->start_display++;
2465 if (c == '\n')
2466 edit->start_line++;
2468 edit_modification (edit);
2469 if (c == '\n')
2471 if (edit->book_mark)
2472 book_mark_inc (edit, edit->curs_line);
2473 edit->total_lines++;
2474 edit->force |= REDRAW_AFTER_CURSOR;
2476 edit_push_action (edit, DELCHAR);
2478 edit->mark1 += (edit->mark1 >= edit->curs1);
2479 edit->mark2 += (edit->mark2 >= edit->curs1);
2480 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2482 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2483 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2484 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2485 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2487 edit->last_byte++;
2488 edit->curs2++;
2492 /* --------------------------------------------------------------------------------------------- */
2495 edit_delete (WEdit * edit, const int byte_delete)
2497 int p = 0;
2498 int cw = 1;
2499 int i;
2501 if (!edit->curs2)
2502 return 0;
2504 cw = 1;
2505 /* if byte_delete = 1 then delete only one byte not multibyte char */
2506 if (edit->utf8 && byte_delete == 0)
2508 edit_get_utf (edit, edit->curs1, &cw);
2509 if (cw < 1)
2510 cw = 1;
2513 if (edit->mark2 != edit->mark1)
2514 edit_push_markers (edit);
2516 for (i = 1; i <= cw; i++)
2518 if (edit->mark1 > edit->curs1)
2520 edit->mark1--;
2521 edit->end_mark_curs--;
2523 if (edit->mark2 > edit->curs1)
2524 edit->mark2--;
2525 if (edit->last_get_rule > edit->curs1)
2526 edit->last_get_rule--;
2528 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2529 ((edit->curs2 -
2530 1) & M_EDIT_BUF_SIZE) - 1];
2532 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2534 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2535 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2537 edit->last_byte--;
2538 edit->curs2--;
2539 edit_push_action (edit, p + 256);
2542 edit_modification (edit);
2543 if (p == '\n')
2545 if (edit->book_mark)
2546 book_mark_dec (edit, edit->curs_line);
2547 edit->total_lines--;
2548 edit->force |= REDRAW_AFTER_CURSOR;
2550 if (edit->curs1 < edit->start_display)
2552 edit->start_display--;
2553 if (p == '\n')
2554 edit->start_line--;
2557 return p;
2560 /* --------------------------------------------------------------------------------------------- */
2562 #ifdef FAST_MOVE_CURSOR
2564 edit_move_backward_lots (WEdit * edit, long increment)
2566 int r, s, t;
2567 unsigned char *p = NULL;
2569 if (increment > edit->curs1)
2570 increment = edit->curs1;
2571 if (increment <= 0)
2572 return -1;
2573 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
2575 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
2576 if (r > increment)
2577 r = increment;
2578 s = edit->curs1 & M_EDIT_BUF_SIZE;
2580 if (s > r)
2582 memqcpy (edit,
2583 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
2584 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
2586 else
2588 if (s != 0)
2590 memqcpy (edit,
2591 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
2592 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
2593 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
2594 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
2596 memqcpy (edit,
2597 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
2598 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
2599 EDIT_BUF_SIZE - (r - s), r - s);
2601 increment -= r;
2602 edit->curs1 -= r;
2603 edit->curs2 += r;
2604 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2606 if (p)
2607 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
2608 else
2609 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2611 else
2613 g_free (p);
2616 s = edit->curs1 & M_EDIT_BUF_SIZE;
2617 while (increment)
2619 p = 0;
2620 r = EDIT_BUF_SIZE;
2621 if (r > increment)
2622 r = increment;
2623 t = s;
2624 if (r < t)
2625 t = r;
2626 memqcpy (edit,
2627 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
2628 EDIT_BUF_SIZE - t, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t, t);
2629 if (r >= s)
2631 if (t)
2633 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
2634 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
2636 memqcpy (edit,
2637 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
2638 EDIT_BUF_SIZE - r,
2639 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
2640 EDIT_BUF_SIZE - (r - s), r - s);
2642 increment -= r;
2643 edit->curs1 -= r;
2644 edit->curs2 += r;
2645 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2647 if (p)
2648 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
2649 else
2650 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2652 else
2653 g_free (p);
2655 return edit_get_byte (edit, edit->curs1);
2657 #endif /* ! FAST_MOVE_CURSOR */
2659 /* --------------------------------------------------------------------------------------------- */
2660 /** moves the cursor right or left: increment positive or negative respectively */
2662 void
2663 edit_cursor_move (WEdit * edit, long increment)
2665 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2666 int c;
2667 #ifdef FAST_MOVE_CURSOR
2668 if (increment < -256)
2670 edit->force |= REDRAW_PAGE;
2671 edit_move_backward_lots (edit, -increment);
2672 return;
2674 #endif /* ! FAST_MOVE_CURSOR */
2676 if (increment < 0)
2678 for (; increment < 0; increment++)
2680 if (!edit->curs1)
2681 return;
2683 edit_push_action (edit, CURS_RIGHT);
2685 c = edit_get_byte (edit, edit->curs1 - 1);
2686 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
2687 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2688 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2689 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2690 edit->curs2++;
2691 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2692 1) & M_EDIT_BUF_SIZE];
2693 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE))
2695 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2696 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2698 edit->curs1--;
2699 if (c == '\n')
2701 edit->curs_line--;
2702 edit->force |= REDRAW_LINE_BELOW;
2707 else if (increment > 0)
2709 for (; increment > 0; increment--)
2711 if (!edit->curs2)
2712 return;
2714 edit_push_action (edit, CURS_LEFT);
2716 c = edit_get_byte (edit, edit->curs1);
2717 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
2718 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2719 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2720 edit->curs1++;
2721 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2722 ((edit->curs2 -
2723 1) & M_EDIT_BUF_SIZE) - 1];
2724 if (!(edit->curs2 & M_EDIT_BUF_SIZE))
2726 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2727 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2729 edit->curs2--;
2730 if (c == '\n')
2732 edit->curs_line++;
2733 edit->force |= REDRAW_LINE_ABOVE;
2739 /* These functions return positions relative to lines */
2741 /* --------------------------------------------------------------------------------------------- */
2742 /** returns index of last char on line + 1 */
2744 long
2745 edit_eol (WEdit * edit, long current)
2747 if (current >= edit->last_byte)
2748 return edit->last_byte;
2750 for (;; current++)
2751 if (edit_get_byte (edit, current) == '\n')
2752 break;
2753 return current;
2756 /* --------------------------------------------------------------------------------------------- */
2757 /** returns index of first char on line */
2759 long
2760 edit_bol (WEdit * edit, long current)
2762 if (current <= 0)
2763 return 0;
2765 for (;; current--)
2766 if (edit_get_byte (edit, current - 1) == '\n')
2767 break;
2768 return current;
2771 /* --------------------------------------------------------------------------------------------- */
2773 long
2774 edit_count_lines (WEdit * edit, long current, long upto)
2776 long lines = 0;
2777 if (upto > edit->last_byte)
2778 upto = edit->last_byte;
2779 if (current < 0)
2780 current = 0;
2781 while (current < upto)
2782 if (edit_get_byte (edit, current++) == '\n')
2783 lines++;
2784 return lines;
2787 /* --------------------------------------------------------------------------------------------- */
2788 /* If lines is zero this returns the count of lines from current to upto. */
2789 /* If upto is zero returns index of lines forward current. */
2791 long
2792 edit_move_forward (WEdit * edit, long current, long lines, long upto)
2794 if (upto)
2796 return edit_count_lines (edit, current, upto);
2798 else
2800 long next;
2801 if (lines < 0)
2802 lines = 0;
2803 while (lines--)
2805 next = edit_eol (edit, current) + 1;
2806 if (next > edit->last_byte)
2807 break;
2808 else
2809 current = next;
2811 return current;
2815 /* --------------------------------------------------------------------------------------------- */
2816 /** Returns offset of 'lines' lines up from current */
2818 long
2819 edit_move_backward (WEdit * edit, long current, long lines)
2821 if (lines < 0)
2822 lines = 0;
2823 current = edit_bol (edit, current);
2824 while ((lines--) && current != 0)
2825 current = edit_bol (edit, current - 1);
2826 return current;
2829 /* --------------------------------------------------------------------------------------------- */
2830 /* If cols is zero this returns the count of columns from current to upto. */
2831 /* If upto is zero returns index of cols across from current. */
2833 long
2834 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
2836 long p, q;
2837 int col;
2839 if (upto)
2841 q = upto;
2842 cols = -10;
2844 else
2845 q = edit->last_byte + 2;
2847 for (col = 0, p = current; p < q; p++)
2849 int c, orig_c;
2851 if (cols != -10)
2853 if (col == cols)
2854 return p;
2855 if (col > cols)
2856 return p - 1;
2859 orig_c = c = edit_get_byte (edit, p);
2861 #ifdef HAVE_CHARSET
2862 if (edit->utf8)
2864 int utf_ch;
2865 int cw = 1;
2867 utf_ch = edit_get_utf (edit, p, &cw);
2868 if (utf8_display)
2870 if (cw > 1)
2871 col -= cw - 1;
2872 if (g_unichar_iswide (utf_ch))
2873 col++;
2875 else if (cw > 1 && g_unichar_isprint (utf_ch))
2876 col -= cw - 1;
2879 c = convert_to_display_c (c);
2880 #endif
2882 if (c == '\t')
2883 col += TAB_SIZE - col % TAB_SIZE;
2884 else if (c == '\n')
2886 if (upto)
2887 return col;
2888 else
2889 return p;
2891 else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
2892 /* '\r' is shown as ^M, so we must advance 2 characters */
2893 /* Caret notation for control characters */
2894 col += 2;
2895 else
2896 col++;
2898 return col;
2901 /* --------------------------------------------------------------------------------------------- */
2902 /** returns the current column position of the cursor */
2905 edit_get_col (WEdit * edit)
2907 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
2910 /* --------------------------------------------------------------------------------------------- */
2911 /* Scrolling functions */
2912 /* --------------------------------------------------------------------------------------------- */
2914 void
2915 edit_update_curs_row (WEdit * edit)
2917 edit->curs_row = edit->curs_line - edit->start_line;
2920 /* --------------------------------------------------------------------------------------------- */
2922 void
2923 edit_update_curs_col (WEdit * edit)
2925 edit->curs_col = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
2928 /* --------------------------------------------------------------------------------------------- */
2931 edit_get_curs_col (const WEdit * edit)
2933 return edit->curs_col;
2936 /* --------------------------------------------------------------------------------------------- */
2937 /** moves the display start position up by i lines */
2939 void
2940 edit_scroll_upward (WEdit * edit, unsigned long i)
2942 unsigned long lines_above = edit->start_line;
2943 if (i > lines_above)
2944 i = lines_above;
2945 if (i)
2947 edit->start_line -= i;
2948 edit->start_display = edit_move_backward (edit, edit->start_display, i);
2949 edit->force |= REDRAW_PAGE;
2950 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2952 edit_update_curs_row (edit);
2956 /* --------------------------------------------------------------------------------------------- */
2957 /** returns 1 if could scroll, 0 otherwise */
2959 void
2960 edit_scroll_downward (WEdit * edit, int i)
2962 int lines_below;
2963 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
2964 if (lines_below > 0)
2966 if (i > lines_below)
2967 i = lines_below;
2968 edit->start_line += i;
2969 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
2970 edit->force |= REDRAW_PAGE;
2971 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2973 edit_update_curs_row (edit);
2976 /* --------------------------------------------------------------------------------------------- */
2978 void
2979 edit_scroll_right (WEdit * edit, int i)
2981 edit->force |= REDRAW_PAGE;
2982 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2983 edit->start_col -= i;
2986 /* --------------------------------------------------------------------------------------------- */
2988 void
2989 edit_scroll_left (WEdit * edit, int i)
2991 if (edit->start_col)
2993 edit->start_col += i;
2994 if (edit->start_col > 0)
2995 edit->start_col = 0;
2996 edit->force |= REDRAW_PAGE;
2997 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3001 /* --------------------------------------------------------------------------------------------- */
3002 /* high level cursor movement commands */
3003 /* --------------------------------------------------------------------------------------------- */
3005 void
3006 edit_move_to_prev_col (WEdit * edit, long p)
3008 int prev = edit->prev_col;
3009 int over = edit->over_col;
3010 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3012 if (option_cursor_beyond_eol)
3014 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3015 edit_eol (edit, edit->curs1));
3017 if (line_len < prev + edit->over_col)
3019 edit->over_col = prev + over - line_len;
3020 edit->prev_col = line_len;
3021 edit->curs_col = line_len;
3023 else
3025 edit->curs_col = prev + over;
3026 edit->prev_col = edit->curs_col;
3027 edit->over_col = 0;
3030 else
3032 edit->over_col = 0;
3033 if (is_in_indent (edit) && option_fake_half_tabs)
3035 edit_update_curs_col (edit);
3036 if (space_width)
3037 if (edit->curs_col % (HALF_TAB_SIZE * space_width))
3039 int q = edit->curs_col;
3040 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3041 p = edit_bol (edit, edit->curs1);
3042 edit_cursor_move (edit,
3043 edit_move_forward3 (edit, p, edit->curs_col,
3044 0) - edit->curs1);
3045 if (!left_of_four_spaces (edit))
3046 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3052 /* --------------------------------------------------------------------------------------------- */
3055 line_is_blank (WEdit * edit, long line)
3057 return is_blank (edit, edit_find_line (edit, line));
3060 /* --------------------------------------------------------------------------------------------- */
3061 /** move cursor to line 'line' */
3063 void
3064 edit_move_to_line (WEdit * e, long line)
3066 if (line < e->curs_line)
3067 edit_move_up (e, e->curs_line - line, 0);
3068 else
3069 edit_move_down (e, line - e->curs_line, 0);
3070 edit_scroll_screen_over_cursor (e);
3073 /* --------------------------------------------------------------------------------------------- */
3074 /** scroll window so that first visible line is 'line' */
3076 void
3077 edit_move_display (WEdit * e, long line)
3079 if (line < e->start_line)
3080 edit_scroll_upward (e, e->start_line - line);
3081 else
3082 edit_scroll_downward (e, line - e->start_line);
3085 /* --------------------------------------------------------------------------------------------- */
3086 /** save markers onto undo stack */
3088 void
3089 edit_push_markers (WEdit * edit)
3091 edit_push_action (edit, MARK_1 + edit->mark1);
3092 edit_push_action (edit, MARK_2 + edit->mark2);
3093 edit_push_action (edit, MARK_CURS + edit->end_mark_curs);
3096 /* --------------------------------------------------------------------------------------------- */
3098 void
3099 edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
3101 edit->mark1 = m1;
3102 edit->mark2 = m2;
3103 edit->column1 = c1;
3104 edit->column2 = c2;
3108 /* --------------------------------------------------------------------------------------------- */
3109 /** highlight marker toggle */
3111 void
3112 edit_mark_cmd (WEdit * edit, int unmark)
3114 edit_push_markers (edit);
3115 if (unmark)
3117 edit_set_markers (edit, 0, 0, 0, 0);
3118 edit->force |= REDRAW_PAGE;
3120 else
3122 if (edit->mark2 >= 0)
3124 edit->end_mark_curs = -1;
3125 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3126 edit->curs_col + edit->over_col);
3127 edit->force |= REDRAW_PAGE;
3129 else
3131 edit->end_mark_curs = edit->curs1;
3132 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3133 edit->curs_col + edit->over_col);
3138 /* --------------------------------------------------------------------------------------------- */
3139 /** highlight the word under cursor */
3141 void
3142 edit_mark_current_word_cmd (WEdit * edit)
3144 long pos;
3146 for (pos = edit->curs1; pos != 0; pos--)
3148 int c1, c2;
3150 c1 = edit_get_byte (edit, pos);
3151 c2 = edit_get_byte (edit, pos - 1);
3152 if (!isspace (c1) && isspace (c2))
3153 break;
3154 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3155 break;
3157 edit->mark1 = pos;
3159 for (; pos < edit->last_byte; pos++)
3161 int c1, c2;
3163 c1 = edit_get_byte (edit, pos);
3164 c2 = edit_get_byte (edit, pos + 1);
3165 if (!isspace (c1) && isspace (c2))
3166 break;
3167 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3168 break;
3170 edit->mark2 = min (pos + 1, edit->last_byte);
3172 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3175 /* --------------------------------------------------------------------------------------------- */
3177 void
3178 edit_mark_current_line_cmd (WEdit * edit)
3180 long pos = edit->curs1;
3182 edit->mark1 = edit_bol (edit, pos);
3183 edit->mark2 = edit_eol (edit, pos);
3185 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3188 /* --------------------------------------------------------------------------------------------- */
3190 void
3191 edit_delete_line (WEdit * edit)
3194 * Delete right part of the line.
3195 * Note that edit_get_byte() returns '\n' when byte position is
3196 * beyond EOF.
3198 while (edit_get_byte (edit, edit->curs1) != '\n')
3200 (void) edit_delete (edit, 1);
3204 * Delete '\n' char.
3205 * Note that edit_delete() will not corrupt anything if called while
3206 * cursor position is EOF.
3208 (void) edit_delete (edit, 1);
3211 * Delete left part of the line.
3212 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3214 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3216 (void) edit_backspace (edit, 1);
3220 /* --------------------------------------------------------------------------------------------- */
3222 void
3223 insert_spaces_tab (WEdit * edit, int half)
3225 int i;
3226 edit_update_curs_col (edit);
3227 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) +
3228 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
3229 while (i > 0)
3231 edit_insert (edit, ' ');
3232 i -= space_width;
3236 /* --------------------------------------------------------------------------------------------- */
3239 edit_indent_width (WEdit * edit, long p)
3241 long q = p;
3242 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
3243 q++;
3244 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
3247 /* --------------------------------------------------------------------------------------------- */
3249 void
3250 edit_insert_indent (WEdit * edit, int indent)
3252 if (!option_fill_tabs_with_spaces)
3254 while (indent >= TAB_SIZE)
3256 edit_insert (edit, '\t');
3257 indent -= TAB_SIZE;
3260 while (indent-- > 0)
3261 edit_insert (edit, ' ');
3264 /* --------------------------------------------------------------------------------------------- */
3266 void
3267 edit_push_key_press (WEdit * edit)
3269 edit_push_action (edit, KEY_PRESS + edit->start_display);
3270 if (edit->mark2 == -1)
3272 edit_push_action (edit, MARK_1 + edit->mark1);
3273 edit_push_action (edit, MARK_CURS + edit->end_mark_curs);
3277 /* --------------------------------------------------------------------------------------------- */
3279 void
3280 edit_find_bracket (WEdit * edit)
3282 edit->bracket = edit_get_bracket (edit, 1, 10000);
3283 if (last_bracket != edit->bracket)
3284 edit->force |= REDRAW_PAGE;
3285 last_bracket = edit->bracket;
3288 /* --------------------------------------------------------------------------------------------- */
3290 * This executes a command as though the user initiated it through a key
3291 * press. Callback with WIDGET_KEY as a message calls this after
3292 * translating the key press. This function can be used to pass any
3293 * command to the editor. Note that the screen wouldn't update
3294 * automatically. Either of command or char_for_insertion must be
3295 * passed as -1. Commands are executed, and char_for_insertion is
3296 * inserted at the cursor.
3299 void
3300 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3302 if (command == CK_Begin_Record_Macro)
3304 edit->macro_i = 0;
3305 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3306 return;
3308 if (command == CK_End_Record_Macro && edit->macro_i != -1)
3310 edit->force |= REDRAW_COMPLETELY;
3311 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
3312 edit->macro_i = -1;
3313 return;
3315 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1)
3317 edit->macro[edit->macro_i].command = command;
3318 edit->macro[edit->macro_i++].ch = char_for_insertion;
3320 /* record the beginning of a set of editing actions initiated by a key press */
3321 if (command != CK_Undo && command != CK_Ext_Mode)
3322 edit_push_key_press (edit);
3324 edit_execute_cmd (edit, command, char_for_insertion);
3325 if (edit->column_highlight)
3326 edit->force |= REDRAW_PAGE;
3329 /* --------------------------------------------------------------------------------------------- */
3331 This executes a command at a lower level than macro recording.
3332 It also does not push a key_press onto the undo stack. This means
3333 that if it is called many times, a single undo command will undo
3334 all of them. It also does not check for the Undo command.
3336 void
3337 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3339 edit->force |= REDRAW_LINE;
3341 /* The next key press will unhighlight the found string, so update
3342 * the whole page */
3343 if (edit->found_len || edit->column_highlight)
3344 edit->force |= REDRAW_PAGE;
3346 if (command / 100 == 6)
3347 { /* a highlight command like shift-arrow */
3348 edit->column_highlight = 0;
3349 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3351 edit_mark_cmd (edit, 1); /* clear */
3352 edit_mark_cmd (edit, 0); /* marking on */
3354 edit->highlight = 1;
3356 else
3357 { /* any other command */
3358 if (edit->highlight)
3359 edit_mark_cmd (edit, 0); /* clear */
3360 edit->highlight = 0;
3363 /* first check for undo */
3364 if (command == CK_Undo)
3366 edit_do_undo (edit);
3367 edit->found_len = 0;
3368 edit->prev_col = edit_get_col (edit);
3369 edit->search_start = edit->curs1;
3370 return;
3373 /* An ordinary key press */
3374 if (char_for_insertion >= 0)
3376 /* if non persistent selection and text selected */
3377 if (!option_persistent_selections)
3379 if (edit->mark1 != edit->mark2)
3380 edit_block_delete_cmd (edit);
3382 if (edit->overwrite)
3384 /* remove char only one time, after input first byte, multibyte chars */
3385 if ((!utf8_display || edit->charpoint == 0)
3386 && edit_get_byte (edit, edit->curs1) != '\n')
3387 edit_delete (edit, 0);
3389 if (option_cursor_beyond_eol && edit->over_col > 0)
3390 edit_insert_over (edit);
3391 #ifdef HAVE_CHARSET
3392 if (char_for_insertion > 255 && utf8_display == 0)
3394 unsigned char str[6 + 1];
3395 size_t i = 0;
3396 int res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3397 if (res == 0)
3399 str[0] = '.';
3400 str[1] = '\0';
3402 else
3404 str[res] = '\0';
3406 while (str[i] != 0 && i <= 6)
3408 char_for_insertion = str[i];
3409 edit_insert (edit, char_for_insertion);
3410 i++;
3413 else
3414 #endif
3415 edit_insert (edit, char_for_insertion);
3417 if (option_auto_para_formatting)
3419 format_paragraph (edit, 0);
3420 edit->force |= REDRAW_PAGE;
3422 else
3423 check_and_wrap_line (edit);
3424 edit->found_len = 0;
3425 edit->prev_col = edit_get_col (edit);
3426 edit->search_start = edit->curs1;
3427 edit_find_bracket (edit);
3428 return;
3431 switch (command)
3433 case CK_Begin_Page:
3434 case CK_End_Page:
3435 case CK_Begin_Page_Highlight:
3436 case CK_End_Page_Highlight:
3437 case CK_Word_Left:
3438 case CK_Word_Right:
3439 case CK_Up:
3440 case CK_Down:
3441 case CK_Left:
3442 case CK_Right:
3443 if (edit->mark2 >= 0)
3445 if (!option_persistent_selections)
3447 if (edit->column_highlight)
3448 edit_push_action (edit, COLUMN_ON);
3449 edit->column_highlight = 0;
3450 edit_mark_cmd (edit, 1);
3455 switch (command)
3457 case CK_Begin_Page:
3458 case CK_End_Page:
3459 case CK_Begin_Page_Highlight:
3460 case CK_End_Page_Highlight:
3461 case CK_Word_Left:
3462 case CK_Word_Right:
3463 case CK_Up:
3464 case CK_Down:
3465 case CK_Word_Left_Highlight:
3466 case CK_Word_Right_Highlight:
3467 case CK_Up_Highlight:
3468 case CK_Down_Highlight:
3469 case CK_Up_Alt_Highlight:
3470 case CK_Down_Alt_Highlight:
3471 if (edit->mark2 == -1)
3472 break; /*marking is following the cursor: may need to highlight a whole line */
3473 case CK_Left:
3474 case CK_Right:
3475 case CK_Left_Highlight:
3476 case CK_Right_Highlight:
3477 edit->force |= REDRAW_CHAR_ONLY;
3480 /* basic cursor key commands */
3481 switch (command)
3483 case CK_BackSpace:
3484 /* if non persistent selection and text selected */
3485 if (!option_persistent_selections)
3487 if (edit->mark1 != edit->mark2)
3489 edit_block_delete_cmd (edit);
3490 break;
3493 if (option_cursor_beyond_eol && edit->over_col > 0)
3495 edit->over_col--;
3496 break;
3498 if (option_backspace_through_tabs && is_in_indent (edit))
3500 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3501 edit_backspace (edit, 1);
3502 break;
3504 else
3506 if (option_fake_half_tabs)
3508 int i;
3509 if (is_in_indent (edit) && right_of_four_spaces (edit))
3511 for (i = 0; i < HALF_TAB_SIZE; i++)
3512 edit_backspace (edit, 1);
3513 break;
3517 edit_backspace (edit, 0);
3518 break;
3519 case CK_Delete:
3520 /* if non persistent selection and text selected */
3521 if (!option_persistent_selections)
3523 if (edit->mark1 != edit->mark2)
3525 edit_block_delete_cmd (edit);
3526 break;
3530 if (option_cursor_beyond_eol && edit->over_col > 0)
3531 edit_insert_over (edit);
3533 if (option_fake_half_tabs)
3535 int i;
3536 if (is_in_indent (edit) && left_of_four_spaces (edit))
3538 for (i = 1; i <= HALF_TAB_SIZE; i++)
3539 edit_delete (edit, 1);
3540 break;
3543 edit_delete (edit, 0);
3544 break;
3545 case CK_Delete_Word_Left:
3546 edit->over_col = 0;
3547 edit_left_delete_word (edit);
3548 break;
3549 case CK_Delete_Word_Right:
3550 if (option_cursor_beyond_eol && edit->over_col > 0)
3551 edit_insert_over (edit);
3553 edit_right_delete_word (edit);
3554 break;
3555 case CK_Delete_Line:
3556 edit_delete_line (edit);
3557 break;
3558 case CK_Delete_To_Line_End:
3559 edit_delete_to_line_end (edit);
3560 break;
3561 case CK_Delete_To_Line_Begin:
3562 edit_delete_to_line_begin (edit);
3563 break;
3564 case CK_Enter:
3565 edit->over_col = 0;
3566 if (option_auto_para_formatting)
3568 edit_double_newline (edit);
3569 if (option_return_does_auto_indent)
3570 edit_auto_indent (edit);
3571 format_paragraph (edit, 0);
3573 else
3575 edit_insert (edit, '\n');
3576 if (option_return_does_auto_indent)
3578 edit_auto_indent (edit);
3581 break;
3582 case CK_Return:
3583 edit_insert (edit, '\n');
3584 break;
3586 case CK_Page_Up_Alt_Highlight:
3587 edit->column_highlight = 1;
3588 case CK_Page_Up:
3589 case CK_Page_Up_Highlight:
3590 edit_move_up (edit, edit->widget.lines - 1, 1);
3591 break;
3592 case CK_Page_Down_Alt_Highlight:
3593 edit->column_highlight = 1;
3594 case CK_Page_Down:
3595 case CK_Page_Down_Highlight:
3596 edit_move_down (edit, edit->widget.lines - 1, 1);
3597 break;
3598 case CK_Left_Alt_Highlight:
3599 edit->column_highlight = 1;
3600 case CK_Left:
3601 case CK_Left_Highlight:
3602 if (option_fake_half_tabs)
3604 if (is_in_indent (edit) && right_of_four_spaces (edit))
3606 if (option_cursor_beyond_eol && edit->over_col > 0)
3607 edit->over_col--;
3608 else
3609 edit_cursor_move (edit, -HALF_TAB_SIZE);
3610 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3611 break;
3614 edit_left_char_move_cmd (edit);
3615 break;
3616 case CK_Right_Alt_Highlight:
3617 edit->column_highlight = 1;
3618 case CK_Right:
3619 case CK_Right_Highlight:
3620 if (option_fake_half_tabs)
3622 if (is_in_indent (edit) && left_of_four_spaces (edit))
3624 edit_cursor_move (edit, HALF_TAB_SIZE);
3625 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3626 break;
3629 edit_right_char_move_cmd (edit);
3630 break;
3631 case CK_Begin_Page:
3632 case CK_Begin_Page_Highlight:
3633 edit_begin_page (edit);
3634 break;
3635 case CK_End_Page:
3636 case CK_End_Page_Highlight:
3637 edit_end_page (edit);
3638 break;
3639 case CK_Word_Left:
3640 case CK_Word_Left_Highlight:
3641 edit->over_col = 0;
3642 edit_left_word_move_cmd (edit);
3643 break;
3644 case CK_Word_Right:
3645 case CK_Word_Right_Highlight:
3646 edit->over_col = 0;
3647 edit_right_word_move_cmd (edit);
3648 break;
3649 case CK_Up_Alt_Highlight:
3650 edit->column_highlight = 1;
3651 case CK_Up:
3652 case CK_Up_Highlight:
3653 edit_move_up (edit, 1, 0);
3654 break;
3655 case CK_Down_Alt_Highlight:
3656 edit->column_highlight = 1;
3657 case CK_Down:
3658 case CK_Down_Highlight:
3659 edit_move_down (edit, 1, 0);
3660 break;
3661 case CK_Paragraph_Up_Alt_Highlight:
3662 edit->column_highlight = 1;
3663 case CK_Paragraph_Up:
3664 case CK_Paragraph_Up_Highlight:
3665 edit_move_up_paragraph (edit, 0);
3666 break;
3667 case CK_Paragraph_Down_Alt_Highlight:
3668 edit->column_highlight = 1;
3669 case CK_Paragraph_Down:
3670 case CK_Paragraph_Down_Highlight:
3671 edit_move_down_paragraph (edit, 0);
3672 break;
3673 case CK_Scroll_Up_Alt_Highlight:
3674 edit->column_highlight = 1;
3675 case CK_Scroll_Up:
3676 case CK_Scroll_Up_Highlight:
3677 edit_move_up (edit, 1, 1);
3678 break;
3679 case CK_Scroll_Down_Alt_Highlight:
3680 edit->column_highlight = 1;
3681 case CK_Scroll_Down:
3682 case CK_Scroll_Down_Highlight:
3683 edit_move_down (edit, 1, 1);
3684 break;
3685 case CK_Home:
3686 case CK_Home_Highlight:
3687 edit_cursor_to_bol (edit);
3688 break;
3689 case CK_End:
3690 case CK_End_Highlight:
3691 edit_cursor_to_eol (edit);
3692 break;
3693 case CK_Tab:
3694 /* if text marked shift block */
3695 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3697 if (edit->mark2 < 0)
3698 edit_mark_cmd (edit, 0);
3699 edit_move_block_to_right (edit);
3701 else
3703 if (option_cursor_beyond_eol)
3704 edit_insert_over (edit);
3705 edit_tab_cmd (edit);
3706 if (option_auto_para_formatting)
3708 format_paragraph (edit, 0);
3709 edit->force |= REDRAW_PAGE;
3711 else
3713 check_and_wrap_line (edit);
3716 break;
3718 case CK_Toggle_Insert:
3719 edit->overwrite = (edit->overwrite == 0);
3720 break;
3722 case CK_Mark:
3723 if (edit->mark2 >= 0)
3725 if (edit->column_highlight)
3726 edit_push_action (edit, COLUMN_ON);
3727 edit->column_highlight = 0;
3729 edit_mark_cmd (edit, 0);
3730 break;
3731 case CK_Column_Mark:
3732 if (!edit->column_highlight)
3733 edit_push_action (edit, COLUMN_OFF);
3734 edit->column_highlight = 1;
3735 edit_mark_cmd (edit, 0);
3736 break;
3737 case CK_Mark_All:
3738 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3739 edit->force |= REDRAW_PAGE;
3740 break;
3741 case CK_Unmark:
3742 if (edit->column_highlight)
3743 edit_push_action (edit, COLUMN_ON);
3744 edit->column_highlight = 0;
3745 edit_mark_cmd (edit, 1);
3746 break;
3747 case CK_Mark_Word:
3748 if (edit->column_highlight)
3749 edit_push_action (edit, COLUMN_ON);
3750 edit->column_highlight = 0;
3751 edit_mark_current_word_cmd (edit);
3752 break;
3753 case CK_Mark_Line:
3754 if (edit->column_highlight)
3755 edit_push_action (edit, COLUMN_ON);
3756 edit->column_highlight = 0;
3757 edit_mark_current_line_cmd (edit);
3758 break;
3759 case CK_Toggle_Line_State:
3760 option_line_state = !option_line_state;
3761 if (option_line_state)
3763 option_line_state_width = LINE_STATE_WIDTH;
3765 else
3767 option_line_state_width = 0;
3769 edit->force |= REDRAW_PAGE;
3770 break;
3772 case CK_Toggle_Show_Margin:
3773 show_right_margin = !show_right_margin;
3774 edit->force |= REDRAW_PAGE;
3775 break;
3777 case CK_Toggle_Bookmark:
3778 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3779 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3780 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3781 else
3782 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3783 break;
3784 case CK_Flush_Bookmarks:
3785 book_mark_flush (edit, BOOK_MARK_COLOR);
3786 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3787 edit->force |= REDRAW_PAGE;
3788 break;
3789 case CK_Next_Bookmark:
3790 if (edit->book_mark)
3792 struct _book_mark *p;
3793 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3794 if (p->next)
3796 p = p->next;
3797 if (p->line >= edit->start_line + edit->widget.lines
3798 || p->line < edit->start_line)
3799 edit_move_display (edit, p->line - edit->widget.lines / 2);
3800 edit_move_to_line (edit, p->line);
3803 break;
3804 case CK_Prev_Bookmark:
3805 if (edit->book_mark)
3807 struct _book_mark *p;
3808 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
3809 while (p->line == edit->curs_line)
3810 if (p->prev)
3811 p = p->prev;
3812 if (p->line >= 0)
3814 if (p->line >= edit->start_line + edit->widget.lines
3815 || p->line < edit->start_line)
3816 edit_move_display (edit, p->line - edit->widget.lines / 2);
3817 edit_move_to_line (edit, p->line);
3820 break;
3822 case CK_Beginning_Of_Text:
3823 case CK_Beginning_Of_Text_Highlight:
3824 edit_move_to_top (edit);
3825 break;
3826 case CK_End_Of_Text:
3827 case CK_End_Of_Text_Highlight:
3828 edit_move_to_bottom (edit);
3829 break;
3831 case CK_Copy:
3832 if (option_cursor_beyond_eol && edit->over_col > 0)
3833 edit_insert_over (edit);
3834 edit_block_copy_cmd (edit);
3835 break;
3836 case CK_Remove:
3837 edit_block_delete_cmd (edit);
3838 break;
3839 case CK_Move:
3840 if (option_cursor_beyond_eol && edit->over_col > 0)
3841 edit_insert_over (edit);
3842 edit_block_move_cmd (edit);
3843 break;
3845 case CK_Shift_Block_Left:
3846 if (edit->mark1 != edit->mark2)
3847 edit_move_block_to_left (edit);
3848 break;
3849 case CK_Shift_Block_Right:
3850 if (edit->mark1 != edit->mark2)
3851 edit_move_block_to_right (edit);
3852 break;
3853 case CK_XStore:
3854 edit_copy_to_X_buf_cmd (edit);
3855 break;
3856 case CK_XCut:
3857 edit_cut_to_X_buf_cmd (edit);
3858 break;
3859 case CK_XPaste:
3860 /* if non persistent selection and text selected */
3861 if (!option_persistent_selections)
3863 if (edit->mark1 != edit->mark2)
3864 edit_block_delete_cmd (edit);
3866 if (option_cursor_beyond_eol && edit->over_col > 0)
3867 edit_insert_over (edit);
3868 edit_paste_from_X_buf_cmd (edit);
3869 break;
3870 case CK_Selection_History:
3871 edit_paste_from_history (edit);
3872 break;
3874 case CK_Save_As:
3875 edit_save_as_cmd (edit);
3876 break;
3877 case CK_Save:
3878 edit_save_confirm_cmd (edit);
3879 break;
3880 case CK_Load:
3881 edit_load_cmd (edit, EDIT_FILE_COMMON);
3882 break;
3883 case CK_Save_Block:
3884 edit_save_block_cmd (edit);
3885 break;
3886 case CK_Insert_File:
3887 edit_insert_file_cmd (edit);
3888 break;
3890 case CK_Load_Prev_File:
3891 edit_load_back_cmd (edit);
3892 break;
3893 case CK_Load_Next_File:
3894 edit_load_forward_cmd (edit);
3895 break;
3897 case CK_Load_Syntax_File:
3898 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3899 break;
3900 case CK_Choose_Syntax:
3901 edit_syntax_dialog (edit);
3902 break;
3904 case CK_Load_Menu_File:
3905 edit_load_cmd (edit, EDIT_FILE_MENU);
3906 break;
3908 case CK_Toggle_Syntax:
3909 option_syntax_highlighting ^= 1;
3910 if (option_syntax_highlighting == 1)
3911 edit_load_syntax (edit, NULL, edit->syntax_type);
3912 edit->force |= REDRAW_PAGE;
3913 break;
3915 case CK_Toggle_Tab_TWS:
3916 enable_show_tabs_tws ^= 1;
3917 edit->force |= REDRAW_PAGE;
3918 break;
3920 case CK_Find:
3921 edit_search_cmd (edit, FALSE);
3922 break;
3923 case CK_Find_Again:
3924 edit_search_cmd (edit, TRUE);
3925 break;
3926 case CK_Replace:
3927 edit_replace_cmd (edit, 0);
3928 break;
3929 case CK_Replace_Again:
3930 edit_replace_cmd (edit, 1);
3931 break;
3932 case CK_Complete_Word:
3933 /* if text marked shift block */
3934 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3936 edit_move_block_to_left (edit);
3938 else
3940 edit_complete_word_cmd (edit);
3942 break;
3943 case CK_Find_Definition:
3944 edit_get_match_keyword_cmd (edit);
3945 break;
3946 case CK_Quit:
3947 dlg_stop (edit->widget.owner);
3948 break;
3949 case CK_New:
3950 edit_new_cmd (edit);
3951 break;
3952 case CK_Help:
3953 edit_help_cmd (edit);
3954 break;
3955 case CK_Refresh:
3956 edit_refresh_cmd (edit);
3957 break;
3958 case CK_SaveSetupCmd:
3959 save_setup_cmd ();
3960 break;
3961 case CK_About:
3962 edit_about ();
3963 break;
3964 case CK_LearnKeys:
3965 learn_keys ();
3966 break;
3967 case CK_Edit_Options:
3968 edit_options_dialog (edit);
3969 break;
3970 case CK_Edit_Save_Mode:
3971 menu_save_mode_cmd ();
3972 break;
3973 case CK_Date:
3975 char s[BUF_MEDIUM];
3976 /* fool gcc to prevent a Y2K warning */
3977 char time_format[] = "_c";
3978 time_format[0] = '%';
3980 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3981 edit_print_string (edit, s);
3982 edit->force |= REDRAW_PAGE;
3983 break;
3985 break;
3986 case CK_Goto:
3987 edit_goto_cmd (edit);
3988 break;
3989 case CK_Paragraph_Format:
3990 format_paragraph (edit, 1);
3991 edit->force |= REDRAW_PAGE;
3992 break;
3993 case CK_Delete_Macro:
3994 edit_delete_macro_cmd (edit);
3995 break;
3996 case CK_Match_Bracket:
3997 edit_goto_matching_bracket (edit);
3998 break;
3999 case CK_User_Menu:
4000 user_menu (edit);
4001 break;
4002 case CK_Sort:
4003 edit_sort_cmd (edit);
4004 break;
4005 case CK_ExtCmd:
4006 edit_ext_cmd (edit);
4007 break;
4008 case CK_Mail:
4009 edit_mail_dialog (edit);
4010 break;
4011 case CK_Shell:
4012 view_other_cmd ();
4013 break;
4014 case CK_SelectCodepage:
4015 edit_select_codepage_cmd (edit);
4016 break;
4017 case CK_Insert_Literal:
4018 edit_insert_literal_cmd (edit);
4019 break;
4020 case CK_Execute_Macro:
4021 edit_execute_macro_cmd (edit);
4022 break;
4023 case CK_Begin_End_Macro:
4024 edit_begin_end_macro_cmd (edit);
4025 break;
4026 case CK_Ext_Mode:
4027 edit->extmod = 1;
4028 break;
4029 default:
4030 break;
4033 /* CK_Pipe_Block */
4034 if ((command / 1000) == 1) /* a shell command */
4035 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
4036 if (command > CK_Macro (0) && command <= CK_Last_Macro)
4037 { /* a macro command */
4038 struct macro m[MAX_MACRO_LENGTH];
4039 int nm;
4040 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
4041 edit_execute_macro (edit, m, nm);
4044 /* keys which must set the col position, and the search vars */
4045 switch (command)
4047 case CK_Find:
4048 case CK_Find_Again:
4049 case CK_Replace:
4050 case CK_Replace_Again:
4051 case CK_Complete_Word:
4052 edit->prev_col = edit_get_col (edit);
4053 break;
4054 case CK_Up:
4055 case CK_Up_Highlight:
4056 case CK_Up_Alt_Highlight:
4057 case CK_Down:
4058 case CK_Down_Highlight:
4059 case CK_Down_Alt_Highlight:
4060 case CK_Page_Up:
4061 case CK_Page_Up_Highlight:
4062 case CK_Page_Up_Alt_Highlight:
4063 case CK_Page_Down:
4064 case CK_Page_Down_Highlight:
4065 case CK_Page_Down_Alt_Highlight:
4066 case CK_Beginning_Of_Text:
4067 case CK_Beginning_Of_Text_Highlight:
4068 case CK_End_Of_Text:
4069 case CK_End_Of_Text_Highlight:
4070 case CK_Paragraph_Up:
4071 case CK_Paragraph_Up_Highlight:
4072 case CK_Paragraph_Up_Alt_Highlight:
4073 case CK_Paragraph_Down:
4074 case CK_Paragraph_Down_Highlight:
4075 case CK_Paragraph_Down_Alt_Highlight:
4076 case CK_Scroll_Up:
4077 case CK_Scroll_Up_Highlight:
4078 case CK_Scroll_Up_Alt_Highlight:
4079 case CK_Scroll_Down:
4080 case CK_Scroll_Down_Highlight:
4081 case CK_Scroll_Down_Alt_Highlight:
4082 edit->search_start = edit->curs1;
4083 edit->found_len = 0;
4084 break;
4085 default:
4086 edit->found_len = 0;
4087 edit->prev_col = edit_get_col (edit);
4088 edit->search_start = edit->curs1;
4090 edit_find_bracket (edit);
4092 if (option_auto_para_formatting)
4094 switch (command)
4096 case CK_BackSpace:
4097 case CK_Delete:
4098 case CK_Delete_Word_Left:
4099 case CK_Delete_Word_Right:
4100 case CK_Delete_To_Line_End:
4101 case CK_Delete_To_Line_Begin:
4102 format_paragraph (edit, 0);
4103 edit->force |= REDRAW_PAGE;
4108 /* --------------------------------------------------------------------------------------------- */
4110 void
4111 edit_stack_init (void)
4113 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4115 edit_history_moveto[edit_stack_iterator].filename = NULL;
4116 edit_history_moveto[edit_stack_iterator].line = -1;
4119 edit_stack_iterator = 0;
4122 /* --------------------------------------------------------------------------------------------- */
4124 void
4125 edit_stack_free (void)
4127 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4128 g_free (edit_history_moveto[edit_stack_iterator].filename);
4131 /* --------------------------------------------------------------------------------------------- */
4132 /** move i lines */
4134 void
4135 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
4137 edit_move_updown (edit, i, do_scroll, TRUE);
4140 /* --------------------------------------------------------------------------------------------- */
4141 /** move i lines */
4143 void
4144 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
4146 edit_move_updown (edit, i, do_scroll, FALSE);
4149 /* --------------------------------------------------------------------------------------------- */
4151 unsigned int
4152 edit_unlock_file (WEdit * edit)
4154 char *fullpath;
4155 unsigned int ret;
4157 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4158 ret = unlock_file (fullpath);
4159 g_free (fullpath);
4161 return ret;
4164 /* --------------------------------------------------------------------------------------------- */
4166 unsigned int
4167 edit_lock_file (WEdit * edit)
4169 char *fullpath;
4170 unsigned int ret;
4172 fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL);
4173 ret = lock_file (fullpath);
4174 g_free (fullpath);
4176 return ret;
4179 /* --------------------------------------------------------------------------------------------- */