Refactoring of many conditions.
[midnight-commander.git] / src / editor / edit.c
blob37b7bcc3a724c194abc467454b01ff4530100fd4
1 /*
2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer 1996, 1997
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor low level data handling and cursor fundamentals
31 * \author Paul Sheer
32 * \date 1996, 1997
35 #include <config.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
47 #include "lib/global.h"
49 #include "lib/tty/color.h"
50 #include "lib/tty/tty.h" /* attrset() */
51 #include "lib/tty/key.h" /* is_idle() */
52 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
53 #include "lib/vfs/vfs.h"
54 #include "lib/strutil.h" /* utf string functions */
55 #include "lib/util.h" /* load_file_position(), save_file_position() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "lib/lock.h"
58 #include "lib/widget.h"
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h" /* get_codepage_id */
62 #endif
64 #include "src/filemanager/cmd.h" /* view_other_cmd() */
65 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* macro_index */
69 #include "src/learn.h" /* learn_keys */
70 #include "src/keybind-defaults.h"
72 #include "edit-impl.h"
73 #include "editwidget.h"
75 /*** global variables ****************************************************************************/
77 int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
78 int option_typewriter_wrap = 0;
79 int option_auto_para_formatting = 0;
80 int option_fill_tabs_with_spaces = 0;
81 int option_return_does_auto_indent = 1;
82 int option_backspace_through_tabs = 0;
83 int option_fake_half_tabs = 1;
84 int option_save_mode = EDIT_QUICK_SAVE;
85 int option_save_position = 1;
86 int option_max_undo = 32768;
87 int option_persistent_selections = 1;
88 int option_cursor_beyond_eol = 0;
89 int option_line_state = 0;
90 int option_line_state_width = 0;
92 int option_edit_right_extreme = 0;
93 int option_edit_left_extreme = 0;
94 int option_edit_top_extreme = 0;
95 int option_edit_bottom_extreme = 0;
96 int enable_show_tabs_tws = 1;
97 int option_check_nl_at_eof = 0;
98 int option_group_undo = 0;
99 int show_right_margin = 0;
101 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
102 char *option_backup_ext = NULL;
104 unsigned int edit_stack_iterator = 0;
105 edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
106 /* magic sequense for say than block is vertical */
107 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
109 /*** file scope macro definitions ****************************************************************/
111 #define TEMP_BUF_LEN 1024
113 #define space_width 1
115 /*** file scope type declarations ****************************************************************/
117 /*** file scope variables ************************************************************************/
119 /* detecting an error on save is easy: just check if every byte has been written. */
120 /* detecting an error on read, is not so easy 'cos there is not way to tell
121 whether you read everything or not. */
122 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
123 static const struct edit_filters
125 const char *read, *write, *extension;
126 } all_filters[] =
128 /* *INDENT-OFF* */
129 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
130 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
131 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
132 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
133 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
134 /* *INDENT-ON* */
137 static off_t last_bracket = -1;
139 /*** file scope functions ************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
144 * here's a quick sketch of the layout: (don't run this through indent.)
146 * (b1 is buffers1 and b2 is buffers2)
149 * \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
150 * ______________________________________|______________________________________
152 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
153 * |-> |-> |-> |-> |-> |-> |
155 * _<------------------------->|<----------------->_
156 * WEdit->curs2 | WEdit->curs1
157 * ^ | ^
158 * | ^|^ |
159 * cursor ||| cursor
160 * |||
161 * file end|||file beginning
166 * This_is_some_file
167 * fin.
170 /* --------------------------------------------------------------------------------------------- */
172 static void
173 edit_about (void)
175 const char *header = N_("About");
176 const char *button_name = N_("&OK");
177 const char *const version = "MCEdit " VERSION;
178 char text[BUF_LARGE];
180 int win_len, version_len, button_len;
181 int cols, lines;
183 Dlg_head *about_dlg;
185 #ifdef ENABLE_NLS
186 header = _(header);
187 button_name = _(button_name);
188 #endif
190 button_len = str_term_width1 (button_name) + 5;
191 version_len = str_term_width1 (version);
193 g_snprintf (text, sizeof (text),
194 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
195 " A user friendly text editor\n"
196 " written for the Midnight Commander"));
198 win_len = str_term_width1 (header);
199 win_len = max (win_len, version_len);
200 win_len = max (win_len, button_len);
202 /* count width and height of text */
203 str_msg_term_size (text, &lines, &cols);
204 lines += 9;
205 cols = max (win_len, cols) + 6;
207 /* dialog */
208 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL,
209 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
211 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
212 add_widget (about_dlg, label_new (5, 3, text));
213 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
214 B_ENTER, NORMAL_BUTTON, button_name, NULL));
216 run_dlg (about_dlg);
217 destroy_dlg (about_dlg);
220 /* --------------------------------------------------------------------------------------------- */
222 * Initialize the buffers for an empty files.
225 static void
226 edit_init_buffers (WEdit * edit)
228 int j;
230 for (j = 0; j <= MAXBUFF; j++)
232 edit->buffers1[j] = NULL;
233 edit->buffers2[j] = NULL;
236 edit->curs1 = 0;
237 edit->curs2 = 0;
238 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
241 /* --------------------------------------------------------------------------------------------- */
244 * Load file OR text into buffers. Set cursor to the beginning of file.
246 * @returns FALSE on error.
249 static gboolean
250 edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath)
252 off_t buf, buf2;
253 int file = -1;
254 gboolean ret = FALSE;
256 edit->curs2 = edit->last_byte;
257 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
259 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
260 if (file == -1)
262 gchar *errmsg, *filename;
264 filename = vfs_path_to_str (filename_vpath);
265 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
266 g_free (filename);
267 edit_error_dialog (_("Error"), errmsg);
268 g_free (errmsg);
269 return FALSE;
272 if (!edit->buffers2[buf2])
273 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
277 if (mc_read (file,
278 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
279 (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE) < 0)
280 break;
282 for (buf = buf2 - 1; buf >= 0; buf--)
284 /* edit->buffers2[0] is already allocated */
285 if (!edit->buffers2[buf])
286 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
287 if (mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) < 0)
288 break;
290 ret = TRUE;
292 while (FALSE);
294 if (!ret)
296 gchar *errmsg, *filename;
298 filename = vfs_path_to_str (filename_vpath);
299 errmsg = g_strdup_printf (_("Error reading %s"), filename);
300 g_free (filename);
301 edit_error_dialog (_("Error"), errmsg);
302 g_free (errmsg);
304 mc_close (file);
305 return ret;
308 /* --------------------------------------------------------------------------------------------- */
309 /** Return index of the filter or -1 is there is no appropriate filter */
311 static int
312 edit_find_filter (const vfs_path_t * filename_vpath)
314 size_t i, l, e;
315 char *filename;
317 if (filename_vpath == NULL)
318 return -1;
320 filename = vfs_path_to_str (filename_vpath);
321 l = strlen (filename);
322 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++)
324 e = strlen (all_filters[i].extension);
325 if (l > e)
326 if (!strcmp (all_filters[i].extension, filename + l - e))
328 g_free (filename);
329 return i;
332 g_free (filename);
333 return -1;
336 /* --------------------------------------------------------------------------------------------- */
338 static char *
339 edit_get_filter (const vfs_path_t * filename_vpath)
341 int i;
342 char *p, *quoted_name, *filename;
344 i = edit_find_filter (filename_vpath);
345 if (i < 0)
346 return NULL;
348 filename = vfs_path_to_str (filename_vpath);
349 quoted_name = name_quote (filename, 0);
350 g_free (filename);
351 p = g_strdup_printf (all_filters[i].read, quoted_name);
352 g_free (quoted_name);
353 return p;
356 /* --------------------------------------------------------------------------------------------- */
358 static off_t
359 edit_insert_stream (WEdit * edit, FILE * f)
361 int c;
362 off_t i = 0;
363 while ((c = fgetc (f)) >= 0)
365 edit_insert (edit, c);
366 i++;
368 return i;
371 /* --------------------------------------------------------------------------------------------- */
373 * Open file and create it if necessary.
375 * @param edit editor object
376 * @param filename_vpath file name
377 * @param st buffer for store stat info
378 * @returns TRUE for success, FALSE for error.
381 static gboolean
382 check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
384 int file;
385 gchar *errmsg = NULL;
387 /* Try opening an existing file */
388 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
389 if (file < 0)
392 * Try creating the file. O_EXCL prevents following broken links
393 * and opening existing files.
395 file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
396 if (file < 0)
398 char *filename;
400 filename = vfs_path_to_str (filename_vpath);
401 errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
402 g_free (filename);
403 goto cleanup;
406 /* New file, delete it if it's not modified or saved */
407 edit->delete_file = 1;
410 /* Check what we have opened */
411 if (mc_fstat (file, st) < 0)
413 char *filename;
415 filename = vfs_path_to_str (filename_vpath);
416 errmsg = g_strdup_printf (_("Cannot get size/permissions for %s"), filename);
417 g_free (filename);
418 goto cleanup;
421 /* We want to open regular files only */
422 if (!S_ISREG (st->st_mode))
424 char *filename;
426 filename = vfs_path_to_str (filename_vpath);
427 errmsg = g_strdup_printf (_("\"%s\" is not a regular file"), filename);
428 g_free (filename);
429 goto cleanup;
433 * Don't delete non-empty files.
434 * O_EXCL should prevent it, but let's be on the safe side.
436 if (st->st_size > 0)
437 edit->delete_file = 0;
439 if (st->st_size >= SIZE_LIMIT)
441 char *filename;
443 filename = vfs_path_to_str (filename_vpath);
444 errmsg = g_strdup_printf (_("File \"%s\" is too large"), filename);
445 g_free (filename);
448 cleanup:
449 (void) mc_close (file);
451 if (errmsg != NULL)
453 edit_error_dialog (_("Error"), errmsg);
454 g_free (errmsg);
455 return FALSE;
457 return TRUE;
460 /* --------------------------------------------------------------------------------------------- */
463 * Open the file and load it into the buffers, either directly or using
464 * a filter. Return TRUE on success, FALSE on error.
466 * Fast loading (edit_load_file_fast) is used when the file size is
467 * known. In this case the data is read into the buffers by blocks.
468 * If the file size is not known, the data is loaded byte by byte in
469 * edit_insert_file.
471 * @param edit editor object
472 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
474 static gboolean
475 edit_load_file (WEdit * edit)
477 gboolean fast_load = TRUE;
479 /* Cannot do fast load if a filter is used */
480 if (edit_find_filter (edit->filename_vpath) >= 0)
481 fast_load = FALSE;
484 * FIXME: line end translation should disable fast loading as well
485 * Consider doing fseek() to the end and ftell() for the real size.
487 if (edit->filename_vpath != NULL)
490 * VFS may report file size incorrectly, and slow load is not a big
491 * deal considering overhead in VFS.
493 if (!vfs_file_is_local (edit->filename_vpath))
494 fast_load = FALSE;
496 /* If we are dealing with a real file, check that it exists */
497 if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
498 return FALSE;
500 else
502 /* nothing to load */
503 fast_load = FALSE;
506 edit_init_buffers (edit);
508 if (fast_load)
510 edit->last_byte = edit->stat1.st_size;
511 edit_load_file_fast (edit, edit->filename_vpath);
512 /* If fast load was used, the number of lines wasn't calculated */
513 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
515 else
517 edit->last_byte = 0;
518 if (edit->filename_vpath != NULL
519 && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
521 edit->undo_stack_disable = 1;
522 if (edit_insert_file (edit, edit->filename_vpath) < 0)
524 edit_clean (edit);
525 return FALSE;
527 edit->undo_stack_disable = 0;
530 edit->lb = LB_ASIS;
531 return TRUE;
534 /* --------------------------------------------------------------------------------------------- */
535 /** Restore saved cursor position in the file */
537 static void
538 edit_load_position (WEdit * edit)
540 long line, column;
541 off_t offset;
543 if (edit->filename_vpath == NULL
544 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
545 return;
547 load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
549 if (line > 0)
551 edit_move_to_line (edit, line - 1);
552 edit->prev_col = column;
554 else if (offset > 0)
556 edit_cursor_move (edit, offset);
557 line = edit->curs_line;
558 edit->search_start = edit->curs1;
561 book_mark_restore (edit, BOOK_MARK_COLOR);
563 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
564 edit_move_display (edit, line - (edit->widget.lines / 2));
567 /* --------------------------------------------------------------------------------------------- */
568 /** Save cursor position in the file */
570 static void
571 edit_save_position (WEdit * edit)
573 if (edit->filename_vpath == NULL
574 || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
575 return;
577 book_mark_serialize (edit, BOOK_MARK_COLOR);
578 save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->curs1,
579 edit->serialized_bookmarks);
580 edit->serialized_bookmarks = NULL;
583 /* --------------------------------------------------------------------------------------------- */
584 /** Clean the WEdit stricture except the widget part */
586 static void
587 edit_purge_widget (WEdit * edit)
589 size_t len = sizeof (WEdit) - sizeof (Widget);
590 char *start = (char *) edit + sizeof (Widget);
591 memset (start, 0, len);
594 /* --------------------------------------------------------------------------------------------- */
597 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
598 then the file should be as it was when he loaded up. Then set edit->modified to 0.
601 static long
602 edit_pop_undo_action (WEdit * edit)
604 long c;
605 unsigned long sp = edit->undo_stack_pointer;
607 if (sp == edit->undo_stack_bottom)
608 return STACK_BOTTOM;
610 sp = (sp - 1) & edit->undo_stack_size_mask;
611 c = edit->undo_stack[sp];
612 if (c >= 0)
614 /* edit->undo_stack[sp] = '@'; */
615 edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
616 return c;
619 if (sp == edit->undo_stack_bottom)
620 return STACK_BOTTOM;
622 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
623 if (edit->undo_stack[sp] == -2)
625 /* edit->undo_stack[sp] = '@'; */
626 edit->undo_stack_pointer = sp;
628 else
629 edit->undo_stack[sp]++;
631 return c;
634 static long
635 edit_pop_redo_action (WEdit * edit)
637 long c;
638 unsigned long sp = edit->redo_stack_pointer;
640 if (sp == edit->redo_stack_bottom)
641 return STACK_BOTTOM;
643 sp = (sp - 1) & edit->redo_stack_size_mask;
644 c = edit->redo_stack[sp];
645 if (c >= 0)
647 edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
648 return c;
651 if (sp == edit->redo_stack_bottom)
652 return STACK_BOTTOM;
654 c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
655 if (edit->redo_stack[sp] == -2)
656 edit->redo_stack_pointer = sp;
657 else
658 edit->redo_stack[sp]++;
660 return c;
663 static long
664 get_prev_undo_action (WEdit * edit)
666 long c;
667 unsigned long sp = edit->undo_stack_pointer;
669 if (sp == edit->undo_stack_bottom)
670 return STACK_BOTTOM;
672 sp = (sp - 1) & edit->undo_stack_size_mask;
673 c = edit->undo_stack[sp];
674 if (c >= 0)
675 return c;
677 if (sp == edit->undo_stack_bottom)
678 return STACK_BOTTOM;
680 c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
681 return c;
684 /* --------------------------------------------------------------------------------------------- */
685 /** is called whenever a modification is made by one of the four routines below */
687 static void
688 edit_modification (WEdit * edit)
690 edit->caches_valid = FALSE;
692 /* raise lock when file modified */
693 if (!edit->modified && !edit->delete_file)
694 edit->locked = lock_file (edit->filename_vpath);
695 edit->modified = 1;
698 /* --------------------------------------------------------------------------------------------- */
700 #ifdef HAVE_CHARSET
701 static char *
702 edit_get_byte_ptr (WEdit * edit, off_t byte_index)
704 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
705 return NULL;
707 if (byte_index >= edit->curs1)
709 off_t p;
711 p = edit->curs1 + edit->curs2 - byte_index - 1;
712 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
713 (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
716 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] +
717 (byte_index & M_EDIT_BUF_SIZE));
719 #endif
721 /* --------------------------------------------------------------------------------------------- */
723 #ifdef HAVE_CHARSET
724 static int
725 edit_get_prev_utf (WEdit * edit, off_t byte_index, int *char_width)
727 int i, res;
728 gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
729 gchar *str;
730 gchar *cursor_buf_ptr;
732 if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
734 *char_width = 0;
735 return 0;
738 for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
739 utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
740 utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
742 cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
743 str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
745 if (str == NULL || g_utf8_next_char (str) != cursor_buf_ptr)
747 *char_width = 1;
748 return *(cursor_buf_ptr - 1);
750 else
752 res = g_utf8_get_char_validated (str, -1);
754 if (res < 0)
756 *char_width = 1;
757 return *(cursor_buf_ptr - 1);
759 else
761 *char_width = cursor_buf_ptr - str;
762 return res;
766 #endif
768 /* --------------------------------------------------------------------------------------------- */
770 static int
771 edit_backspace (WEdit * edit, const int byte_delete)
773 int p = 0;
774 int cw = 1;
775 int i;
777 if (edit->curs1 == 0)
778 return 0;
780 if (edit->mark2 != edit->mark1)
781 edit_push_markers (edit);
783 #ifdef HAVE_CHARSET
784 if (edit->utf8 && byte_delete == 0)
786 edit_get_prev_utf (edit, edit->curs1, &cw);
787 if (cw < 1)
788 cw = 1;
790 #else
791 (void) byte_delete;
792 #endif
794 for (i = 1; i <= cw; i++)
796 if (edit->mark1 >= edit->curs1)
798 edit->mark1--;
799 edit->end_mark_curs--;
801 if (edit->mark2 >= edit->curs1)
802 edit->mark2--;
803 if (edit->last_get_rule >= edit->curs1)
804 edit->last_get_rule--;
806 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] +
807 ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
808 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
810 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
811 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
813 edit->last_byte--;
814 edit->curs1--;
815 edit_push_undo_action (edit, p);
817 edit_modification (edit);
818 if (p == '\n')
820 book_mark_dec (edit, edit->curs_line);
821 edit->curs_line--;
822 edit->total_lines--;
823 edit->force |= REDRAW_AFTER_CURSOR;
826 if (edit->curs1 < edit->start_display)
828 edit->start_display--;
829 if (p == '\n')
830 edit->start_line--;
833 return p;
836 /* --------------------------------------------------------------------------------------------- */
837 /* high level cursor movement commands */
838 /* --------------------------------------------------------------------------------------------- */
839 /** check whether cursor is in indent part of line
841 * @param edit editor object
843 * @return TRUE if cursor is in indent, FALSE otherwise
846 static gboolean
847 is_in_indent (WEdit * edit)
849 off_t p;
851 for (p = edit_bol (edit, edit->curs1); p < edit->curs1; p++)
852 if (strchr (" \t", edit_get_byte (edit, p)) == NULL)
853 return FALSE;
855 return TRUE;
858 /* --------------------------------------------------------------------------------------------- */
859 /** check whether line in editor is blank or not
861 * @param edit editor object
862 * @param offset position in file
864 * @return TRUE if line in blank, FALSE otherwise
867 static gboolean
868 is_blank (WEdit * edit, off_t offset)
870 off_t s, f;
871 int c;
873 s = edit_bol (edit, offset);
874 f = edit_eol (edit, offset) - 1;
875 while (s <= f)
877 c = edit_get_byte (edit, s++);
878 if (!isspace (c))
879 return FALSE;
881 return TRUE;
884 /* --------------------------------------------------------------------------------------------- */
885 /** returns the offset of line i */
887 static off_t
888 edit_find_line (WEdit * edit, long line)
890 long i, j = 0;
891 long m = 2000000000; /* what is the magic number? */
893 if (!edit->caches_valid)
895 memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
896 memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
897 /* three offsets that we *know* are line 0 at 0 and these two: */
898 edit->line_numbers[1] = edit->curs_line;
899 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
900 edit->line_numbers[2] = edit->total_lines;
901 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
902 edit->caches_valid = TRUE;
904 if (line >= edit->total_lines)
905 return edit->line_offsets[2];
906 if (line <= 0)
907 return 0;
908 /* find the closest known point */
909 for (i = 0; i < N_LINE_CACHES; i++)
911 long n;
913 n = abs (edit->line_numbers[i] - line);
914 if (n < m)
916 m = n;
917 j = i;
920 if (m == 0)
921 return edit->line_offsets[j]; /* know the offset exactly */
922 if (m == 1 && j >= 3)
923 i = j; /* one line different - caller might be looping, so stay in this cache */
924 else
925 i = 3 + (rand () % (N_LINE_CACHES - 3));
926 if (line > edit->line_numbers[j])
927 edit->line_offsets[i] =
928 edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
929 else
930 edit->line_offsets[i] =
931 edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
932 edit->line_numbers[i] = line;
933 return edit->line_offsets[i];
936 /* --------------------------------------------------------------------------------------------- */
937 /** moves up until a blank line is reached, or until just
938 before a non-blank line is reached */
940 static void
941 edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
943 long i = 0;
945 if (edit->curs_line > 1)
947 if (!line_is_blank (edit, edit->curs_line))
949 for (i = edit->curs_line - 1; i != 0; i--)
950 if (line_is_blank (edit, i))
951 break;
953 else if (line_is_blank (edit, edit->curs_line - 1))
955 for (i = edit->curs_line - 1; i != 0; i--)
956 if (!line_is_blank (edit, i))
958 i++;
959 break;
962 else
964 for (i = edit->curs_line - 1; i != 0; i--)
965 if (line_is_blank (edit, i))
966 break;
969 edit_move_up (edit, edit->curs_line - i, do_scroll);
972 /* --------------------------------------------------------------------------------------------- */
973 /** moves down until a blank line is reached, or until just
974 before a non-blank line is reached */
976 static void
977 edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
979 long i;
981 if (edit->curs_line >= edit->total_lines - 1)
982 i = edit->total_lines;
983 else if (!line_is_blank (edit, edit->curs_line))
985 for (i = edit->curs_line + 1; i; i++)
986 if (line_is_blank (edit, i) || i >= edit->total_lines)
987 break;
989 else if (line_is_blank (edit, edit->curs_line + 1))
991 for (i = edit->curs_line + 1; i; i++)
992 if (!line_is_blank (edit, i) || i > edit->total_lines)
994 i--;
995 break;
998 else
1000 for (i = edit->curs_line + 1; i; i++)
1001 if (line_is_blank (edit, i) || i >= edit->total_lines)
1002 break;
1005 edit_move_down (edit, i - edit->curs_line, do_scroll);
1008 /* --------------------------------------------------------------------------------------------- */
1010 static void
1011 edit_begin_page (WEdit * edit)
1013 edit_update_curs_row (edit);
1014 edit_move_up (edit, edit->curs_row, 0);
1017 /* --------------------------------------------------------------------------------------------- */
1019 static void
1020 edit_end_page (WEdit * edit)
1022 edit_update_curs_row (edit);
1023 edit_move_down (edit, edit->widget.lines - edit->curs_row - 1, 0);
1027 /* --------------------------------------------------------------------------------------------- */
1028 /** goto beginning of text */
1030 static void
1031 edit_move_to_top (WEdit * edit)
1033 if (edit->curs_line)
1035 edit_cursor_move (edit, -edit->curs1);
1036 edit_move_to_prev_col (edit, 0);
1037 edit->force |= REDRAW_PAGE;
1038 edit->search_start = 0;
1039 edit_update_curs_row (edit);
1044 /* --------------------------------------------------------------------------------------------- */
1045 /** goto end of text */
1047 static void
1048 edit_move_to_bottom (WEdit * edit)
1050 if (edit->curs_line < edit->total_lines)
1052 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1053 edit->start_display = edit->last_byte;
1054 edit->start_line = edit->total_lines;
1055 edit_scroll_upward (edit, edit->widget.lines - 1);
1056 edit->force |= REDRAW_PAGE;
1060 /* --------------------------------------------------------------------------------------------- */
1061 /** goto beginning of line */
1063 static void
1064 edit_cursor_to_bol (WEdit * edit)
1066 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1067 edit->search_start = edit->curs1;
1068 edit->prev_col = edit_get_col (edit);
1069 edit->over_col = 0;
1072 /* --------------------------------------------------------------------------------------------- */
1073 /** goto end of line */
1075 static void
1076 edit_cursor_to_eol (WEdit * edit)
1078 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1079 edit->search_start = edit->curs1;
1080 edit->prev_col = edit_get_col (edit);
1081 edit->over_col = 0;
1084 /* --------------------------------------------------------------------------------------------- */
1086 static unsigned long
1087 my_type_of (int c)
1089 int x, r = 0;
1090 const char *p, *q;
1091 const char option_chars_move_whole_word[] =
1092 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1094 if (!c)
1095 return 0;
1096 if (c == '!')
1098 if (*option_chars_move_whole_word == '!')
1099 return 2;
1100 return 0x80000000UL;
1102 if (g_ascii_isupper ((gchar) c))
1103 c = 'A';
1104 else if (g_ascii_islower ((gchar) c))
1105 c = 'a';
1106 else if (g_ascii_isalpha (c))
1107 c = 'a';
1108 else if (isdigit (c))
1109 c = '0';
1110 else if (isspace (c))
1111 c = ' ';
1112 q = strchr (option_chars_move_whole_word, c);
1113 if (!q)
1114 return 0xFFFFFFFFUL;
1117 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1118 if (*p == '!')
1119 x <<= 1;
1120 r |= x;
1122 while ((q = strchr (q + 1, c)));
1123 return r;
1126 /* --------------------------------------------------------------------------------------------- */
1128 static void
1129 edit_left_word_move (WEdit * edit, int s)
1131 for (;;)
1133 int c1, c2;
1134 if (edit->column_highlight
1135 && edit->mark1 != edit->mark2
1136 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1137 break;
1138 edit_cursor_move (edit, -1);
1139 if (!edit->curs1)
1140 break;
1141 c1 = edit_get_byte (edit, edit->curs1 - 1);
1142 c2 = edit_get_byte (edit, edit->curs1);
1143 if (c1 == '\n' || c2 == '\n')
1144 break;
1145 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1146 break;
1147 if (isspace (c1) && !isspace (c2))
1148 break;
1149 if (s)
1150 if (!isspace (c1) && isspace (c2))
1151 break;
1155 /* --------------------------------------------------------------------------------------------- */
1157 static void
1158 edit_left_word_move_cmd (WEdit * edit)
1160 edit_left_word_move (edit, 0);
1161 edit->force |= REDRAW_PAGE;
1164 /* --------------------------------------------------------------------------------------------- */
1166 static void
1167 edit_right_word_move (WEdit * edit, int s)
1169 for (;;)
1171 int c1, c2;
1172 if (edit->column_highlight
1173 && edit->mark1 != edit->mark2
1174 && edit->over_col == 0 && edit->curs1 == edit_eol (edit, edit->curs1))
1175 break;
1176 edit_cursor_move (edit, 1);
1177 if (edit->curs1 >= edit->last_byte)
1178 break;
1179 c1 = edit_get_byte (edit, edit->curs1 - 1);
1180 c2 = edit_get_byte (edit, edit->curs1);
1181 if (c1 == '\n' || c2 == '\n')
1182 break;
1183 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1184 break;
1185 if (isspace (c1) && !isspace (c2))
1186 break;
1187 if (s)
1188 if (!isspace (c1) && isspace (c2))
1189 break;
1193 /* --------------------------------------------------------------------------------------------- */
1195 static void
1196 edit_right_word_move_cmd (WEdit * edit)
1198 edit_right_word_move (edit, 0);
1199 edit->force |= REDRAW_PAGE;
1202 /* --------------------------------------------------------------------------------------------- */
1204 static void
1205 edit_right_char_move_cmd (WEdit * edit)
1207 int cw = 1;
1208 int c = 0;
1210 #ifdef HAVE_CHARSET
1211 if (edit->utf8)
1213 c = edit_get_utf (edit, edit->curs1, &cw);
1214 if (cw < 1)
1215 cw = 1;
1217 else
1218 #endif
1220 c = edit_get_byte (edit, edit->curs1);
1222 if (option_cursor_beyond_eol && c == '\n')
1224 edit->over_col++;
1226 else
1228 edit_cursor_move (edit, cw);
1232 /* --------------------------------------------------------------------------------------------- */
1234 static void
1235 edit_left_char_move_cmd (WEdit * edit)
1237 int cw = 1;
1239 if (edit->column_highlight
1240 && option_cursor_beyond_eol
1241 && edit->mark1 != edit->mark2
1242 && edit->over_col == 0 && edit->curs1 == edit_bol (edit, edit->curs1))
1243 return;
1245 #ifdef HAVE_CHARSET
1246 if (edit->utf8)
1248 edit_get_prev_utf (edit, edit->curs1, &cw);
1249 if (cw < 1)
1250 cw = 1;
1252 #endif
1253 if (option_cursor_beyond_eol && edit->over_col > 0)
1255 edit->over_col--;
1257 else
1259 edit_cursor_move (edit, -cw);
1263 /* --------------------------------------------------------------------------------------------- */
1264 /** Up or down cursor moving.
1265 direction = TRUE - move up
1266 = FALSE - move down
1269 static void
1270 edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction)
1272 long p;
1273 long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line;
1275 if (lines > l)
1276 lines = l;
1278 if (lines == 0)
1279 return;
1281 if (lines > 1)
1282 edit->force |= REDRAW_PAGE;
1283 if (do_scroll)
1285 if (direction)
1286 edit_scroll_upward (edit, lines);
1287 else
1288 edit_scroll_downward (edit, lines);
1290 p = edit_bol (edit, edit->curs1);
1292 p = direction ? edit_move_backward (edit, p, lines) : edit_move_forward (edit, p, lines, 0);
1294 edit_cursor_move (edit, p - edit->curs1);
1296 edit_move_to_prev_col (edit, p);
1298 /* search start of current multibyte char (like CJK) */
1299 if (edit->curs1 + 1 < edit->last_byte)
1301 edit_right_char_move_cmd (edit);
1302 edit_left_char_move_cmd (edit);
1305 edit->search_start = edit->curs1;
1306 edit->found_len = 0;
1309 /* --------------------------------------------------------------------------------------------- */
1311 static void
1312 edit_right_delete_word (WEdit * edit)
1314 int c1, c2;
1315 for (;;)
1317 if (edit->curs1 >= edit->last_byte)
1318 break;
1319 c1 = edit_delete (edit, 1);
1320 c2 = edit_get_byte (edit, edit->curs1);
1321 if (c1 == '\n' || c2 == '\n')
1322 break;
1323 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1324 break;
1325 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1326 break;
1330 /* --------------------------------------------------------------------------------------------- */
1332 static void
1333 edit_left_delete_word (WEdit * edit)
1335 int c1, c2;
1336 for (;;)
1338 if (edit->curs1 <= 0)
1339 break;
1340 c1 = edit_backspace (edit, 1);
1341 c2 = edit_get_byte (edit, edit->curs1 - 1);
1342 if (c1 == '\n' || c2 == '\n')
1343 break;
1344 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1345 break;
1346 if ((my_type_of (c1) & my_type_of (c2)) == 0)
1347 break;
1351 /* --------------------------------------------------------------------------------------------- */
1353 the start column position is not recorded, and hence does not
1354 undo as it happed. But who would notice.
1357 static void
1358 edit_do_undo (WEdit * edit)
1360 long ac;
1361 long count = 0;
1363 edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
1364 edit->over_col = 0;
1365 while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1367 switch ((int) ac)
1369 case STACK_BOTTOM:
1370 goto done_undo;
1371 case CURS_RIGHT:
1372 edit_cursor_move (edit, 1);
1373 break;
1374 case CURS_LEFT:
1375 edit_cursor_move (edit, -1);
1376 break;
1377 case BACKSPACE:
1378 case BACKSPACE_BR:
1379 edit_backspace (edit, 1);
1380 break;
1381 case DELCHAR:
1382 case DELCHAR_BR:
1383 edit_delete (edit, 1);
1384 break;
1385 case COLUMN_ON:
1386 edit->column_highlight = 1;
1387 break;
1388 case COLUMN_OFF:
1389 edit->column_highlight = 0;
1390 break;
1392 if (ac >= 256 && ac < 512)
1393 edit_insert_ahead (edit, ac - 256);
1394 if (ac >= 0 && ac < 256)
1395 edit_insert (edit, ac);
1397 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1399 edit->mark1 = ac - MARK_1;
1400 edit->column1 =
1401 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1403 if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1405 edit->mark2 = ac - MARK_2;
1406 edit->column2 =
1407 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1409 else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1411 edit->end_mark_curs = ac - MARK_CURS;
1413 if (count++)
1414 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1417 if (edit->start_display > ac - KEY_PRESS)
1419 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1420 edit->force |= REDRAW_PAGE;
1422 else if (edit->start_display < ac - KEY_PRESS)
1424 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1425 edit->force |= REDRAW_PAGE;
1427 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1428 edit_update_curs_row (edit);
1430 done_undo:;
1431 edit->undo_stack_disable = 0;
1434 static void
1435 edit_do_redo (WEdit * edit)
1437 long ac;
1438 long count = 0;
1440 if (edit->redo_stack_reset)
1441 return;
1443 edit->over_col = 0;
1444 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1446 switch ((int) ac)
1448 case STACK_BOTTOM:
1449 goto done_redo;
1450 case CURS_RIGHT:
1451 edit_cursor_move (edit, 1);
1452 break;
1453 case CURS_LEFT:
1454 edit_cursor_move (edit, -1);
1455 break;
1456 case BACKSPACE:
1457 edit_backspace (edit, 1);
1458 break;
1459 case DELCHAR:
1460 edit_delete (edit, 1);
1461 break;
1462 case COLUMN_ON:
1463 edit->column_highlight = 1;
1464 break;
1465 case COLUMN_OFF:
1466 edit->column_highlight = 0;
1467 break;
1469 if (ac >= 256 && ac < 512)
1470 edit_insert_ahead (edit, ac - 256);
1471 if (ac >= 0 && ac < 256)
1472 edit_insert (edit, ac);
1474 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1476 edit->mark1 = ac - MARK_1;
1477 edit->column1 =
1478 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1480 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1482 edit->mark2 = ac - MARK_2;
1483 edit->column2 =
1484 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1486 /* more than one pop usually means something big */
1487 if (count++)
1488 edit->force |= REDRAW_PAGE;
1491 if (edit->start_display > ac - KEY_PRESS)
1493 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1494 edit->force |= REDRAW_PAGE;
1496 else if (edit->start_display < ac - KEY_PRESS)
1498 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1499 edit->force |= REDRAW_PAGE;
1501 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1502 edit_update_curs_row (edit);
1504 done_redo:;
1507 static void
1508 edit_group_undo (WEdit * edit)
1510 long ac = KEY_PRESS;
1511 long cur_ac = KEY_PRESS;
1512 while (ac != STACK_BOTTOM && ac == cur_ac)
1514 cur_ac = get_prev_undo_action (edit);
1515 edit_do_undo (edit);
1516 ac = get_prev_undo_action (edit);
1517 /* exit from cycle if option_group_undo is not set,
1518 * and make single UNDO operation
1520 if (!option_group_undo)
1521 ac = STACK_BOTTOM;
1525 /* --------------------------------------------------------------------------------------------- */
1527 static void
1528 edit_delete_to_line_end (WEdit * edit)
1530 while (edit_get_byte (edit, edit->curs1) != '\n' && edit->curs2 != 0)
1531 edit_delete (edit, 1);
1534 /* --------------------------------------------------------------------------------------------- */
1536 static void
1537 edit_delete_to_line_begin (WEdit * edit)
1539 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 != 0)
1540 edit_backspace (edit, 1);
1543 /* --------------------------------------------------------------------------------------------- */
1545 static gboolean
1546 is_aligned_on_a_tab (WEdit * edit)
1548 long curs_col;
1550 edit_update_curs_col (edit);
1551 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1552 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1555 /* --------------------------------------------------------------------------------------------- */
1557 static gboolean
1558 right_of_four_spaces (WEdit * edit)
1560 int i, ch = 0;
1562 for (i = 1; i <= HALF_TAB_SIZE; i++)
1563 ch |= edit_get_byte (edit, edit->curs1 - i);
1565 return (ch == ' ' && is_aligned_on_a_tab (edit));
1568 /* --------------------------------------------------------------------------------------------- */
1570 static gboolean
1571 left_of_four_spaces (WEdit * edit)
1573 int i, ch = 0;
1575 for (i = 0; i < HALF_TAB_SIZE; i++)
1576 ch |= edit_get_byte (edit, edit->curs1 + i);
1578 return (ch == ' ' && is_aligned_on_a_tab (edit));
1581 /* --------------------------------------------------------------------------------------------- */
1583 static void
1584 edit_auto_indent (WEdit * edit)
1586 off_t p;
1587 char c;
1588 p = edit->curs1;
1589 /* use the previous line as a template */
1590 p = edit_move_backward (edit, p, 1);
1591 /* copy the leading whitespace of the line */
1592 for (;;)
1593 { /* no range check - the line _is_ \n-terminated */
1594 c = edit_get_byte (edit, p++);
1595 if (c != ' ' && c != '\t')
1596 break;
1597 edit_insert (edit, c);
1601 /* --------------------------------------------------------------------------------------------- */
1603 static inline void
1604 edit_double_newline (WEdit * edit)
1606 edit_insert (edit, '\n');
1607 if (edit_get_byte (edit, edit->curs1) == '\n' || edit_get_byte (edit, edit->curs1 - 2) == '\n')
1608 return;
1609 edit->force |= REDRAW_PAGE;
1610 edit_insert (edit, '\n');
1614 /* --------------------------------------------------------------------------------------------- */
1616 static void
1617 insert_spaces_tab (WEdit * edit, gboolean half)
1619 long i;
1621 edit_update_curs_col (edit);
1622 i = option_tab_spacing * space_width;
1623 if (half)
1624 i /= 2;
1625 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1626 while (i > 0)
1628 edit_insert (edit, ' ');
1629 i -= space_width;
1633 /* --------------------------------------------------------------------------------------------- */
1635 static inline void
1636 edit_tab_cmd (WEdit * edit)
1638 if (option_fake_half_tabs && is_in_indent (edit))
1640 /*insert a half tab (usually four spaces) unless there is a
1641 half tab already behind, then delete it and insert a
1642 full tab. */
1643 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1644 insert_spaces_tab (edit, TRUE);
1645 else
1647 int i;
1649 for (i = 1; i <= HALF_TAB_SIZE; i++)
1650 edit_backspace (edit, 1);
1651 edit_insert (edit, '\t');
1654 else if (option_fill_tabs_with_spaces)
1655 insert_spaces_tab (edit, FALSE);
1656 else
1657 edit_insert (edit, '\t');
1660 /* --------------------------------------------------------------------------------------------- */
1662 static void
1663 check_and_wrap_line (WEdit * edit)
1665 off_t curs;
1666 int c;
1668 if (!option_typewriter_wrap)
1669 return;
1670 edit_update_curs_col (edit);
1671 if (edit->curs_col < option_word_wrap_line_length)
1672 return;
1673 curs = edit->curs1;
1674 for (;;)
1676 curs--;
1677 c = edit_get_byte (edit, curs);
1678 if (c == '\n' || curs <= 0)
1680 edit_insert (edit, '\n');
1681 return;
1683 if (c == ' ' || c == '\t')
1685 off_t current = edit->curs1;
1686 edit_cursor_move (edit, curs - edit->curs1 + 1);
1687 edit_insert (edit, '\n');
1688 edit_cursor_move (edit, current - edit->curs1 + 1);
1689 return;
1694 /* --------------------------------------------------------------------------------------------- */
1695 /** this find the matching bracket in either direction, and sets edit->bracket
1697 * @param edit editor object
1698 * @param in_screen seach only on the current screen
1699 * @param furthest_bracket_search count of the bytes for search
1701 * @return position of the found bracket (-1 if no match)
1704 static off_t
1705 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1707 const char *const b = "{}{[][()(", *p;
1708 int i = 1, a, inc = -1, c, d, n = 0;
1709 unsigned long j = 0;
1710 off_t q;
1711 edit_update_curs_row (edit);
1712 c = edit_get_byte (edit, edit->curs1);
1713 p = strchr (b, c);
1714 /* no limit */
1715 if (!furthest_bracket_search)
1716 furthest_bracket_search--;
1717 /* not on a bracket at all */
1718 if (p == NULL)
1719 return -1;
1720 /* the matching bracket */
1721 d = p[1];
1722 /* going left or right? */
1723 if (strchr ("{[(", c))
1724 inc = 1;
1725 for (q = edit->curs1 + inc;; q += inc)
1727 /* out of buffer? */
1728 if (q >= edit->last_byte || q < 0)
1729 break;
1730 a = edit_get_byte (edit, q);
1731 /* don't want to eat CPU */
1732 if (j++ > furthest_bracket_search)
1733 break;
1734 /* out of screen? */
1735 if (in_screen)
1737 if (q < edit->start_display)
1738 break;
1739 /* count lines if searching downward */
1740 if (inc > 0 && a == '\n')
1741 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1742 break;
1744 /* count bracket depth */
1745 i += (a == c) - (a == d);
1746 /* return if bracket depth is zero */
1747 if (i == 0)
1748 return q;
1750 /* no match */
1751 return -1;
1754 /* --------------------------------------------------------------------------------------------- */
1756 static inline void
1757 edit_goto_matching_bracket (WEdit * edit)
1759 off_t q;
1761 q = edit_get_bracket (edit, 0, 0);
1762 if (q >= 0)
1764 edit->bracket = edit->curs1;
1765 edit->force |= REDRAW_PAGE;
1766 edit_cursor_move (edit, q - edit->curs1);
1770 /* --------------------------------------------------------------------------------------------- */
1772 static void
1773 edit_move_block_to_right (WEdit * edit)
1775 off_t start_mark, end_mark;
1776 long cur_bol, start_bol;
1778 if (eval_marks (edit, &start_mark, &end_mark))
1779 return;
1781 start_bol = edit_bol (edit, start_mark);
1782 cur_bol = edit_bol (edit, end_mark - 1);
1786 edit_cursor_move (edit, cur_bol - edit->curs1);
1787 if (option_fill_tabs_with_spaces)
1788 insert_spaces_tab (edit, option_fake_half_tabs);
1789 else
1790 edit_insert (edit, '\t');
1791 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1793 if (cur_bol == 0)
1794 break;
1796 cur_bol = edit_bol (edit, cur_bol - 1);
1798 while (cur_bol >= start_bol);
1800 edit->force |= REDRAW_PAGE;
1803 /* --------------------------------------------------------------------------------------------- */
1805 static void
1806 edit_move_block_to_left (WEdit * edit)
1808 off_t start_mark, end_mark;
1809 off_t cur_bol, start_bol;
1810 int i;
1812 if (eval_marks (edit, &start_mark, &end_mark))
1813 return;
1815 start_bol = edit_bol (edit, start_mark);
1816 cur_bol = edit_bol (edit, end_mark - 1);
1820 int del_tab_width;
1821 int next_char;
1823 edit_cursor_move (edit, cur_bol - edit->curs1);
1825 if (option_fake_half_tabs)
1826 del_tab_width = HALF_TAB_SIZE;
1827 else
1828 del_tab_width = option_tab_spacing;
1830 next_char = edit_get_byte (edit, edit->curs1);
1831 if (next_char == '\t')
1832 edit_delete (edit, 1);
1833 else if (next_char == ' ')
1834 for (i = 1; i <= del_tab_width; i++)
1836 if (next_char == ' ')
1837 edit_delete (edit, 1);
1838 next_char = edit_get_byte (edit, edit->curs1);
1841 if (cur_bol == 0)
1842 break;
1844 cur_bol = edit_bol (edit, cur_bol - 1);
1846 while (cur_bol >= start_bol);
1848 edit->force |= REDRAW_PAGE;
1851 /* --------------------------------------------------------------------------------------------- */
1853 * prints at the cursor
1854 * @returns the number of chars printed
1857 static size_t
1858 edit_print_string (WEdit * e, const char *s)
1860 size_t i = 0;
1862 while (s[i] != '\0')
1863 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1864 e->force |= REDRAW_COMPLETELY;
1865 edit_update_screen (e);
1866 return i;
1869 /* --------------------------------------------------------------------------------------------- */
1870 /*** public functions ****************************************************************************/
1871 /* --------------------------------------------------------------------------------------------- */
1873 /** User edit menu, like user menu (F2) but only in editor. */
1875 void
1876 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1878 char *block_file;
1879 int nomark;
1880 off_t curs;
1881 off_t start_mark, end_mark;
1882 struct stat status;
1883 vfs_path_t *block_file_vpath;
1885 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1886 block_file_vpath = vfs_path_from_str (block_file);
1887 curs = edit->curs1;
1888 nomark = eval_marks (edit, &start_mark, &end_mark);
1889 if (nomark == 0)
1890 edit_save_block (edit, block_file, start_mark, end_mark);
1892 /* run shell scripts from menu */
1893 if (user_menu_cmd (edit, menu_file, selected_entry)
1894 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1896 int rc = 0;
1897 FILE *fd;
1899 /* i.e. we have marked block */
1900 if (nomark == 0)
1901 rc = edit_block_delete_cmd (edit);
1903 if (rc == 0)
1905 off_t ins_len;
1907 ins_len = edit_insert_file (edit, block_file_vpath);
1908 if (nomark == 0 && ins_len > 0)
1909 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1911 /* truncate block file */
1912 fd = fopen (block_file, "w");
1913 if (fd != NULL)
1914 fclose (fd);
1916 edit_cursor_move (edit, curs - edit->curs1);
1917 edit_refresh_cmd (edit);
1918 edit->force |= REDRAW_COMPLETELY;
1920 g_free (block_file);
1921 vfs_path_free (block_file_vpath);
1924 /* --------------------------------------------------------------------------------------------- */
1927 edit_get_byte (WEdit * edit, off_t byte_index)
1929 off_t p;
1931 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1932 return '\n';
1934 if (byte_index >= edit->curs1)
1936 p = edit->curs1 + edit->curs2 - byte_index - 1;
1937 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1940 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1943 /* --------------------------------------------------------------------------------------------- */
1945 #ifdef HAVE_CHARSET
1947 edit_get_utf (WEdit * edit, off_t byte_index, int *char_width)
1949 gchar *str = NULL;
1950 int res = -1;
1951 gunichar ch;
1952 gchar *next_ch = NULL;
1953 int width = 0;
1954 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1956 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1958 *char_width = 0;
1959 return '\n';
1962 str = edit_get_byte_ptr (edit, byte_index);
1964 if (str == NULL)
1966 *char_width = 0;
1967 return 0;
1970 res = g_utf8_get_char_validated (str, -1);
1972 if (res < 0)
1974 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1975 int i;
1976 for (i = 0; i < UTF8_CHAR_LEN; i++)
1977 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1978 utf8_buf[UTF8_CHAR_LEN] = '\0';
1979 str = utf8_buf;
1980 res = g_utf8_get_char_validated (str, -1);
1983 if (res < 0)
1985 ch = *str;
1986 width = 0;
1988 else
1990 ch = res;
1991 /* Calculate UTF-8 char width */
1992 next_ch = g_utf8_next_char (str);
1993 if (next_ch)
1995 width = next_ch - str;
1997 else
1999 ch = 0;
2000 width = 0;
2003 *char_width = width;
2004 return ch;
2006 #endif
2008 /* --------------------------------------------------------------------------------------------- */
2010 char *
2011 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
2013 int i;
2014 char *p, *writename;
2015 const vfs_path_element_t *path_element;
2017 i = edit_find_filter (filename_vpath);
2018 if (i < 0)
2019 return NULL;
2021 path_element = vfs_path_get_by_index (write_name_vpath, -1);
2022 writename = name_quote (path_element->path, 0);
2023 p = g_strdup_printf (all_filters[i].write, writename);
2024 g_free (writename);
2025 return p;
2028 /* --------------------------------------------------------------------------------------------- */
2030 * @param edit editor object
2031 * @param f value of stream file
2032 * @returns the length of the file
2035 off_t
2036 edit_write_stream (WEdit * edit, FILE * f)
2038 long i;
2040 if (edit->lb == LB_ASIS)
2042 for (i = 0; i < edit->last_byte; i++)
2043 if (fputc (edit_get_byte (edit, i), f) < 0)
2044 break;
2045 return i;
2048 /* change line breaks */
2049 for (i = 0; i < edit->last_byte; i++)
2051 unsigned char c = edit_get_byte (edit, i);
2053 if (!(c == '\n' || c == '\r'))
2055 /* not line break */
2056 if (fputc (c, f) < 0)
2057 return i;
2059 else
2060 { /* (c == '\n' || c == '\r') */
2061 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2063 switch (edit->lb)
2065 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2066 /* put one line break unconditionally */
2067 if (fputc ('\n', f) < 0)
2068 return i;
2070 i++; /* 2 chars are processed */
2072 if (c == '\r' && c1 == '\n')
2073 /* Windows line break; go to the next char */
2074 break;
2076 if (c == '\r' && c1 == '\r')
2078 /* two Macintosh line breaks; put second line break */
2079 if (fputc ('\n', f) < 0)
2080 return i;
2081 break;
2084 if (fputc (c1, f) < 0)
2085 return i;
2086 break;
2088 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2089 /* put one line break unconditionally */
2090 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2091 return i;
2093 if (c == '\r' && c1 == '\n')
2094 /* Windows line break; go to the next char */
2095 i++;
2096 break;
2098 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2099 /* put one line break unconditionally */
2100 if (fputc ('\r', f) < 0)
2101 return i;
2103 i++; /* 2 chars are processed */
2105 if (c == '\r' && c1 == '\n')
2106 /* Windows line break; go to the next char */
2107 break;
2109 if (c == '\n' && c1 == '\n')
2111 /* two Windows line breaks; put second line break */
2112 if (fputc ('\r', f) < 0)
2113 return i;
2114 break;
2117 if (fputc (c1, f) < 0)
2118 return i;
2119 break;
2120 case LB_ASIS: /* default without changes */
2121 break;
2126 return edit->last_byte;
2129 /* --------------------------------------------------------------------------------------------- */
2130 /** inserts a file at the cursor, returns count of inserted bytes on success */
2131 long
2132 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2134 char *p;
2135 off_t ins_len = 0;
2137 p = edit_get_filter (filename_vpath);
2138 if (p != NULL)
2140 FILE *f;
2141 off_t current = edit->curs1;
2143 f = (FILE *) popen (p, "r");
2144 if (f != NULL)
2146 edit_insert_stream (edit, f);
2147 ins_len = edit->curs1 - current;
2148 edit_cursor_move (edit, -ins_len);
2149 if (pclose (f) > 0)
2151 char *errmsg;
2153 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2154 edit_error_dialog (_("Error"), errmsg);
2155 g_free (errmsg);
2156 ins_len = -1;
2159 else
2161 char *errmsg;
2163 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2164 edit_error_dialog (_("Error"), errmsg);
2165 g_free (errmsg);
2166 ins_len = -1;
2168 g_free (p);
2170 else
2172 int file;
2173 off_t blocklen;
2174 off_t current = edit->curs1;
2175 int vertical_insertion = 0;
2176 char *buf;
2178 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2179 if (file == -1)
2180 return -1;
2182 buf = g_malloc0 (TEMP_BUF_LEN);
2183 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2184 if (blocklen > 0)
2186 /* if contain signature VERTICAL_MAGIC then it vertical block */
2187 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2188 vertical_insertion = 1;
2189 else
2190 mc_lseek (file, 0, SEEK_SET);
2193 if (vertical_insertion)
2195 off_t mark1, mark2;
2196 long c1, c2;
2198 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2199 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2200 /* highlight inserted text then not persistent blocks */
2201 if (!option_persistent_selections)
2203 if (!edit->column_highlight)
2204 edit_push_undo_action (edit, COLUMN_OFF);
2205 edit->column_highlight = 1;
2208 else
2210 off_t i;
2212 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2214 for (i = 0; i < blocklen; i++)
2215 edit_insert (edit, buf[i]);
2217 /* highlight inserted text then not persistent blocks */
2218 if (!option_persistent_selections && edit->modified)
2220 edit_set_markers (edit, edit->curs1, current, 0, 0);
2221 if (edit->column_highlight)
2222 edit_push_undo_action (edit, COLUMN_ON);
2223 edit->column_highlight = 0;
2227 edit->force |= REDRAW_PAGE;
2228 ins_len = edit->curs1 - current;
2229 edit_cursor_move (edit, -ins_len);
2230 g_free (buf);
2231 mc_close (file);
2232 if (blocklen != 0)
2233 ins_len = 0;
2236 return ins_len;
2239 /* --------------------------------------------------------------------------------------------- */
2241 * Fill in the edit structure. Return NULL on failure. Pass edit as
2242 * NULL to allocate a new structure.
2244 * If line is 0, try to restore saved position. Otherwise put the
2245 * cursor on that line and show it in the middle of the screen.
2248 WEdit *
2249 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2250 long line)
2252 gboolean to_free = FALSE;
2254 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2255 if (option_line_state)
2256 option_line_state_width = LINE_STATE_WIDTH;
2257 else
2258 option_line_state_width = 0;
2260 if (edit == NULL)
2262 #ifdef ENABLE_NLS
2264 * Expand option_whole_chars_search by national letters using
2265 * current locale
2268 static char option_whole_chars_search_buf[256];
2270 if (option_whole_chars_search_buf != option_whole_chars_search)
2272 size_t i;
2273 size_t len = str_term_width1 (option_whole_chars_search);
2275 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2277 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2279 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2281 option_whole_chars_search_buf[len++] = i;
2285 option_whole_chars_search_buf[len] = 0;
2286 option_whole_chars_search = option_whole_chars_search_buf;
2288 #endif /* ENABLE_NLS */
2289 edit = g_malloc0 (sizeof (WEdit));
2290 edit->search = NULL;
2291 to_free = TRUE;
2294 edit_purge_widget (edit);
2295 edit->widget.y = y;
2296 edit->widget.x = x;
2297 edit->widget.lines = lines;
2298 edit->widget.cols = cols;
2300 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2301 edit->stat1.st_uid = getuid ();
2302 edit->stat1.st_gid = getgid ();
2303 edit->stat1.st_mtime = 0;
2305 edit->over_col = 0;
2306 edit->bracket = -1;
2307 edit->force |= REDRAW_PAGE;
2308 edit_set_filename (edit, filename_vpath);
2310 edit->undo_stack_size = START_STACK_SIZE;
2311 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2312 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2314 edit->redo_stack_size = START_STACK_SIZE;
2315 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2316 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2318 #ifdef HAVE_CHARSET
2319 edit->utf8 = FALSE;
2320 edit->converter = str_cnv_from_term;
2321 edit_set_codeset (edit);
2322 #endif
2324 if (!edit_load_file (edit))
2326 /* edit_load_file already gives an error message */
2327 if (to_free)
2328 g_free (edit);
2329 return NULL;
2332 edit->loading_done = 1;
2333 edit->modified = 0;
2334 edit->locked = 0;
2335 edit_load_syntax (edit, NULL, NULL);
2337 int color;
2338 edit_get_syntax_color (edit, -1, &color);
2341 /* load saved cursor position */
2342 if ((line == 0) && option_save_position)
2343 edit_load_position (edit);
2344 else
2346 if (line <= 0)
2347 line = 1;
2348 edit_move_display (edit, line - 1);
2349 edit_move_to_line (edit, line - 1);
2352 edit_load_macro_cmd (edit);
2353 return edit;
2356 /* --------------------------------------------------------------------------------------------- */
2358 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2359 gboolean
2360 edit_clean (WEdit * edit)
2362 int j = 0;
2364 if (edit == NULL)
2365 return FALSE;
2367 /* a stale lock, remove it */
2368 if (edit->locked)
2369 edit->locked = unlock_file (edit->filename_vpath);
2371 /* save cursor position */
2372 if (option_save_position)
2373 edit_save_position (edit);
2374 else if (edit->serialized_bookmarks != NULL)
2375 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2377 /* File specified on the mcedit command line and never saved */
2378 if (edit->delete_file)
2379 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2381 edit_free_syntax_rules (edit);
2382 book_mark_flush (edit, -1);
2383 for (; j <= MAXBUFF; j++)
2385 g_free (edit->buffers1[j]);
2386 g_free (edit->buffers2[j]);
2389 g_free (edit->undo_stack);
2390 g_free (edit->redo_stack);
2391 vfs_path_free (edit->filename_vpath);
2392 vfs_path_free (edit->dir_vpath);
2393 mc_search_free (edit->search);
2394 edit->search = NULL;
2396 #ifdef HAVE_CHARSET
2397 if (edit->converter != str_cnv_from_term)
2398 str_close_conv (edit->converter);
2399 #endif
2401 edit_purge_widget (edit);
2403 return TRUE;
2406 /* --------------------------------------------------------------------------------------------- */
2408 /** returns TRUE on success */
2409 gboolean
2410 edit_renew (WEdit * edit)
2412 int y = edit->widget.y;
2413 int x = edit->widget.x;
2414 int lines = edit->widget.lines;
2415 int columns = edit->widget.cols;
2417 edit_clean (edit);
2418 return (edit_init (edit, y, x, lines, columns, NULL, 0) != NULL);
2421 /* --------------------------------------------------------------------------------------------- */
2424 * Load a new file into the editor and set line. If it fails, preserve the old file.
2425 * To do it, allocate a new widget, initialize it and, if the new file
2426 * was loaded, copy the data to the old widget.
2428 * @returns TRUE on success, FALSE on failure.
2430 gboolean
2431 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2433 WEdit *e;
2434 int y = edit->widget.y;
2435 int x = edit->widget.x;
2436 int lines = edit->widget.lines;
2437 int columns = edit->widget.cols;
2439 e = g_malloc0 (sizeof (WEdit));
2440 e->widget = edit->widget;
2442 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2444 g_free (e);
2445 return FALSE;
2448 edit_clean (edit);
2449 memcpy (edit, e, sizeof (WEdit));
2450 g_free (e);
2452 return TRUE;
2455 /* --------------------------------------------------------------------------------------------- */
2457 #ifdef HAVE_CHARSET
2458 void
2459 edit_set_codeset (WEdit * edit)
2461 const char *cp_id;
2463 cp_id =
2464 get_codepage_id (mc_global.source_codepage >=
2465 0 ? mc_global.source_codepage : mc_global.display_codepage);
2467 if (cp_id != NULL)
2469 GIConv conv;
2470 conv = str_crt_conv_from (cp_id);
2471 if (conv != INVALID_CONV)
2473 if (edit->converter != str_cnv_from_term)
2474 str_close_conv (edit->converter);
2475 edit->converter = conv;
2479 if (cp_id != NULL)
2480 edit->utf8 = str_isutf8 (cp_id);
2482 #endif
2484 /* --------------------------------------------------------------------------------------------- */
2486 Recording stack for undo:
2487 The following is an implementation of a compressed stack. Identical
2488 pushes are recorded by a negative prefix indicating the number of times the
2489 same char was pushed. This saves space for repeated curs-left or curs-right
2490 delete etc.
2494 pushed: stored:
2498 b -3
2500 c --> -4
2506 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2507 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2508 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2509 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2510 position.
2512 The only way the cursor moves or the buffer is changed is through the routines:
2513 insert, backspace, insert_ahead, delete, and cursor_move.
2514 These record the reverse undo movements onto the stack each time they are
2515 called.
2517 Each key press results in a set of actions (insert; delete ...). So each time
2518 a key is pressed the current position of start_display is pushed as
2519 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2520 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2521 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2525 void
2526 edit_push_undo_action (WEdit * edit, long c, ...)
2528 unsigned long sp = edit->undo_stack_pointer;
2529 unsigned long spm1;
2530 long *t;
2532 /* first enlarge the stack if necessary */
2533 if (sp > edit->undo_stack_size - 10)
2534 { /* say */
2535 if (option_max_undo < 256)
2536 option_max_undo = 256;
2537 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2539 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2540 if (t != NULL)
2542 edit->undo_stack = t;
2543 edit->undo_stack_size <<= 1;
2544 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2548 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2549 if (edit->undo_stack_disable)
2551 edit_push_redo_action (edit, KEY_PRESS);
2552 edit_push_redo_action (edit, c);
2553 return;
2555 if (edit->redo_stack_reset)
2556 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2558 if (edit->undo_stack_bottom != sp
2559 && spm1 != edit->undo_stack_bottom
2560 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2562 int d;
2564 if (edit->undo_stack[spm1] < 0)
2566 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2567 if (d == c && edit->undo_stack[spm1] > -1000000000)
2569 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2570 edit->undo_stack[spm1]--;
2571 return;
2574 else
2576 d = edit->undo_stack[spm1];
2577 if (d == c)
2579 if (c >= KEY_PRESS)
2580 return; /* --> no need to push multiple do-nothings */
2581 edit->undo_stack[sp] = -2;
2582 goto check_bottom;
2586 edit->undo_stack[sp] = c;
2588 check_bottom:
2589 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2591 /* if the sp wraps round and catches the undo_stack_bottom then erase
2592 * the first set of actions on the stack to make space - by moving
2593 * undo_stack_bottom forward one "key press" */
2594 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2595 if ((unsigned long) c == edit->undo_stack_bottom ||
2596 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2599 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2601 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2602 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2604 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2605 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2606 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2608 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2612 /* --------------------------------------------------------------------------------------------- */
2614 void
2615 edit_push_redo_action (WEdit * edit, long c, ...)
2617 unsigned long sp = edit->redo_stack_pointer;
2618 unsigned long spm1;
2619 long *t;
2621 /* first enlarge the stack if necessary */
2622 if (sp > edit->redo_stack_size - 10)
2623 { /* say */
2624 if (option_max_undo < 256)
2625 option_max_undo = 256;
2626 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2628 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2629 if (t != NULL)
2631 edit->redo_stack = t;
2632 edit->redo_stack_size <<= 1;
2633 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2637 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2639 if (edit->redo_stack_bottom != sp
2640 && spm1 != edit->redo_stack_bottom
2641 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2643 int d;
2645 if (edit->redo_stack[spm1] < 0)
2647 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2648 if (d == c && edit->redo_stack[spm1] > -1000000000)
2650 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2651 edit->redo_stack[spm1]--;
2652 return;
2655 else
2657 d = edit->redo_stack[spm1];
2658 if (d == c)
2660 if (c >= KEY_PRESS)
2661 return; /* --> no need to push multiple do-nothings */
2662 edit->redo_stack[sp] = -2;
2663 goto redo_check_bottom;
2667 edit->redo_stack[sp] = c;
2669 redo_check_bottom:
2670 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2672 /* if the sp wraps round and catches the redo_stack_bottom then erase
2673 * the first set of actions on the stack to make space - by moving
2674 * redo_stack_bottom forward one "key press" */
2675 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2676 if ((unsigned long) c == edit->redo_stack_bottom ||
2677 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2680 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2682 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2683 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2686 * If a single key produced enough pushes to wrap all the way round then
2687 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2688 * The stack is then initialised:
2691 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2692 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2693 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2697 /* --------------------------------------------------------------------------------------------- */
2699 Basic low level single character buffer alterations and movements at the cursor.
2700 Returns char passed over, inserted or removed.
2703 void
2704 edit_insert (WEdit * edit, int c)
2706 /* check if file has grown to large */
2707 if (edit->last_byte >= SIZE_LIMIT)
2708 return;
2710 /* first we must update the position of the display window */
2711 if (edit->curs1 < edit->start_display)
2713 edit->start_display++;
2714 if (c == '\n')
2715 edit->start_line++;
2718 /* Mark file as modified, unless the file hasn't been fully loaded */
2719 if (edit->loading_done)
2720 edit_modification (edit);
2722 /* now we must update some info on the file and check if a redraw is required */
2723 if (c == '\n')
2725 book_mark_inc (edit, edit->curs_line);
2726 edit->curs_line++;
2727 edit->total_lines++;
2728 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2731 /* save the reverse command onto the undo stack */
2732 /* ordinary char and not space */
2733 if (c > 32)
2734 edit_push_undo_action (edit, BACKSPACE);
2735 else
2736 edit_push_undo_action (edit, BACKSPACE_BR);
2737 /* update markers */
2738 edit->mark1 += (edit->mark1 > edit->curs1);
2739 edit->mark2 += (edit->mark2 > edit->curs1);
2740 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2742 /* add a new buffer if we've reached the end of the last one */
2743 if ((edit->curs1 & M_EDIT_BUF_SIZE) == 0)
2744 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2746 /* perform the insertion */
2747 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2748 = (unsigned char) c;
2750 /* update file length */
2751 edit->last_byte++;
2753 /* update cursor position */
2754 edit->curs1++;
2757 /* --------------------------------------------------------------------------------------------- */
2758 /** same as edit_insert and move left */
2760 void
2761 edit_insert_ahead (WEdit * edit, int c)
2763 if (edit->last_byte >= SIZE_LIMIT)
2764 return;
2766 if (edit->curs1 < edit->start_display)
2768 edit->start_display++;
2769 if (c == '\n')
2770 edit->start_line++;
2772 edit_modification (edit);
2773 if (c == '\n')
2775 book_mark_inc (edit, edit->curs_line);
2776 edit->total_lines++;
2777 edit->force |= REDRAW_AFTER_CURSOR;
2779 /* ordinary char and not space */
2780 if (c > 32)
2781 edit_push_undo_action (edit, DELCHAR);
2782 else
2783 edit_push_undo_action (edit, DELCHAR_BR);
2785 edit->mark1 += (edit->mark1 >= edit->curs1);
2786 edit->mark2 += (edit->mark2 >= edit->curs1);
2787 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2789 if (((edit->curs2 + 1) & M_EDIT_BUF_SIZE) == 0)
2790 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2791 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2792 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2794 edit->last_byte++;
2795 edit->curs2++;
2798 /* --------------------------------------------------------------------------------------------- */
2801 edit_delete (WEdit * edit, const int byte_delete)
2803 int p = 0;
2804 int cw = 1;
2805 int i;
2807 if (edit->curs2 == 0)
2808 return 0;
2810 #ifdef HAVE_CHARSET
2811 /* if byte_delete = 1 then delete only one byte not multibyte char */
2812 if (edit->utf8 && byte_delete == 0)
2814 edit_get_utf (edit, edit->curs1, &cw);
2815 if (cw < 1)
2816 cw = 1;
2818 #else
2819 (void) byte_delete;
2820 #endif
2822 if (edit->mark2 != edit->mark1)
2823 edit_push_markers (edit);
2825 for (i = 1; i <= cw; i++)
2827 if (edit->mark1 > edit->curs1)
2829 edit->mark1--;
2830 edit->end_mark_curs--;
2832 if (edit->mark2 > edit->curs1)
2833 edit->mark2--;
2834 if (edit->last_get_rule > edit->curs1)
2835 edit->last_get_rule--;
2837 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2838 ((edit->curs2 -
2839 1) & M_EDIT_BUF_SIZE) - 1];
2841 if ((edit->curs2 & M_EDIT_BUF_SIZE) == 0)
2843 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2844 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2846 edit->last_byte--;
2847 edit->curs2--;
2848 edit_push_undo_action (edit, p + 256);
2851 edit_modification (edit);
2852 if (p == '\n')
2854 book_mark_dec (edit, edit->curs_line);
2855 edit->total_lines--;
2856 edit->force |= REDRAW_AFTER_CURSOR;
2858 if (edit->curs1 < edit->start_display)
2860 edit->start_display--;
2861 if (p == '\n')
2862 edit->start_line--;
2865 return p;
2868 /* --------------------------------------------------------------------------------------------- */
2869 /** moves the cursor right or left: increment positive or negative respectively */
2871 void
2872 edit_cursor_move (WEdit * edit, off_t increment)
2874 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2875 int c;
2877 if (increment < 0)
2879 for (; increment < 0; increment++)
2881 if (edit->curs1 == 0)
2882 return;
2884 edit_push_undo_action (edit, CURS_RIGHT);
2886 c = edit_get_byte (edit, edit->curs1 - 1);
2887 if (((edit->curs2 + 1) & M_EDIT_BUF_SIZE) == 0)
2888 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2889 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2890 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2891 edit->curs2++;
2892 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2893 1) & M_EDIT_BUF_SIZE];
2894 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2896 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2897 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2899 edit->curs1--;
2900 if (c == '\n')
2902 edit->curs_line--;
2903 edit->force |= REDRAW_LINE_BELOW;
2908 else
2910 for (; increment > 0; increment--)
2912 if (edit->curs2 == 0)
2913 return;
2915 edit_push_undo_action (edit, CURS_LEFT);
2917 c = edit_get_byte (edit, edit->curs1);
2918 if ((edit->curs1 & M_EDIT_BUF_SIZE) == 0)
2919 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2920 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2921 edit->curs1++;
2922 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2923 ((edit->curs2 -
2924 1) & M_EDIT_BUF_SIZE) - 1];
2925 if ((edit->curs2 & M_EDIT_BUF_SIZE) == 0)
2927 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2928 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2930 edit->curs2--;
2931 if (c == '\n')
2933 edit->curs_line++;
2934 edit->force |= REDRAW_LINE_ABOVE;
2940 /* These functions return positions relative to lines */
2942 /* --------------------------------------------------------------------------------------------- */
2943 /** returns index of last char on line + 1 */
2945 off_t
2946 edit_eol (WEdit * edit, off_t current)
2948 if (current >= edit->last_byte)
2949 return edit->last_byte;
2951 for (; edit_get_byte (edit, current) != '\n'; current++)
2954 return current;
2957 /* --------------------------------------------------------------------------------------------- */
2958 /** returns index of first char on line */
2960 off_t
2961 edit_bol (WEdit * edit, off_t current)
2963 if (current <= 0)
2964 return 0;
2966 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
2969 return current;
2972 /* --------------------------------------------------------------------------------------------- */
2974 long
2975 edit_count_lines (WEdit * edit, off_t current, off_t upto)
2977 long lines = 0;
2978 if (upto > edit->last_byte)
2979 upto = edit->last_byte;
2980 if (current < 0)
2981 current = 0;
2982 while (current < upto)
2983 if (edit_get_byte (edit, current++) == '\n')
2984 lines++;
2985 return lines;
2988 /* --------------------------------------------------------------------------------------------- */
2989 /* If lines is zero this returns the count of lines from current to upto. */
2990 /* If upto is zero returns index of lines forward current. */
2992 off_t
2993 edit_move_forward (WEdit * edit, off_t current, long lines, off_t upto)
2995 if (upto != 0)
2997 return (off_t) edit_count_lines (edit, current, upto);
2999 else
3001 long next;
3003 if (lines < 0)
3004 lines = 0;
3005 while (lines-- != 0)
3007 next = edit_eol (edit, current) + 1;
3008 if (next > edit->last_byte)
3009 break;
3010 current = next;
3012 return current;
3016 /* --------------------------------------------------------------------------------------------- */
3017 /** Returns offset of 'lines' lines up from current */
3019 off_t
3020 edit_move_backward (WEdit * edit, off_t current, long lines)
3022 if (lines < 0)
3023 lines = 0;
3024 current = edit_bol (edit, current);
3025 while (lines-- != 0 && current != 0)
3026 current = edit_bol (edit, current - 1);
3027 return current;
3030 /* --------------------------------------------------------------------------------------------- */
3031 /* If cols is zero this returns the count of columns from current to upto. */
3032 /* If upto is zero returns index of cols across from current. */
3034 off_t
3035 edit_move_forward3 (WEdit * edit, off_t current, long cols, off_t upto)
3037 off_t p, q;
3038 long col;
3040 if (upto != 0)
3042 q = upto;
3043 cols = -10;
3045 else
3046 q = edit->last_byte + 2;
3048 for (col = 0, p = current; p < q; p++)
3050 int c, orig_c;
3052 if (cols != -10)
3054 if (col == cols)
3055 return p;
3056 if (col > cols)
3057 return p - 1;
3060 orig_c = c = edit_get_byte (edit, p);
3062 #ifdef HAVE_CHARSET
3063 if (edit->utf8)
3065 int utf_ch;
3066 int cw = 1;
3068 utf_ch = edit_get_utf (edit, p, &cw);
3069 if (mc_global.utf8_display)
3071 if (cw > 1)
3072 col -= cw - 1;
3073 if (g_unichar_iswide (utf_ch))
3074 col++;
3076 else if (cw > 1 && g_unichar_isprint (utf_ch))
3077 col -= cw - 1;
3080 c = convert_to_display_c (c);
3081 #endif
3083 if (c == '\t')
3084 col += TAB_SIZE - col % TAB_SIZE;
3085 else if (c == '\n')
3086 return (upto != 0 ? (off_t) col : p);
3088 if ((c < 32 || c == 127) && (orig_c == c
3089 #ifdef HAVE_CHARSET
3090 || (!mc_global.utf8_display && !edit->utf8)
3091 #endif
3093 /* '\r' is shown as ^M, so we must advance 2 characters */
3094 /* Caret notation for control characters */
3095 col += 2;
3096 else
3097 col++;
3099 return (off_t) col;
3102 /* --------------------------------------------------------------------------------------------- */
3103 /** returns the current column position of the cursor */
3105 long
3106 edit_get_col (WEdit * edit)
3108 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3111 /* --------------------------------------------------------------------------------------------- */
3112 /* Scrolling functions */
3113 /* --------------------------------------------------------------------------------------------- */
3115 void
3116 edit_update_curs_row (WEdit * edit)
3118 edit->curs_row = edit->curs_line - edit->start_line;
3121 /* --------------------------------------------------------------------------------------------- */
3123 void
3124 edit_update_curs_col (WEdit * edit)
3126 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3129 /* --------------------------------------------------------------------------------------------- */
3131 long
3132 edit_get_curs_col (const WEdit * edit)
3134 return edit->curs_col;
3137 /* --------------------------------------------------------------------------------------------- */
3138 /** moves the display start position up by i lines */
3140 void
3141 edit_scroll_upward (WEdit * edit, long i)
3143 long lines_above = edit->start_line;
3145 if (i > lines_above)
3146 i = lines_above;
3147 if (i != 0)
3149 edit->start_line -= i;
3150 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3151 edit->force |= REDRAW_PAGE;
3152 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3154 edit_update_curs_row (edit);
3158 /* --------------------------------------------------------------------------------------------- */
3160 void
3161 edit_scroll_downward (WEdit * edit, long i)
3163 long lines_below;
3165 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3166 if (lines_below > 0)
3168 if (i > lines_below)
3169 i = lines_below;
3170 edit->start_line += i;
3171 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3172 edit->force |= REDRAW_PAGE;
3173 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3175 edit_update_curs_row (edit);
3178 /* --------------------------------------------------------------------------------------------- */
3180 void
3181 edit_scroll_right (WEdit * edit, long i)
3183 edit->force |= REDRAW_PAGE;
3184 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3185 edit->start_col -= i;
3188 /* --------------------------------------------------------------------------------------------- */
3190 void
3191 edit_scroll_left (WEdit * edit, long i)
3193 if (edit->start_col)
3195 edit->start_col += i;
3196 if (edit->start_col > 0)
3197 edit->start_col = 0;
3198 edit->force |= REDRAW_PAGE;
3199 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3203 /* --------------------------------------------------------------------------------------------- */
3204 /* high level cursor movement commands */
3205 /* --------------------------------------------------------------------------------------------- */
3207 void
3208 edit_move_to_prev_col (WEdit * edit, off_t p)
3210 long prev = edit->prev_col;
3211 long over = edit->over_col;
3213 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3215 if (option_cursor_beyond_eol)
3217 long line_len;
3219 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3220 edit_eol (edit, edit->curs1));
3221 if (line_len < prev + edit->over_col)
3223 edit->over_col = prev + over - line_len;
3224 edit->prev_col = line_len;
3225 edit->curs_col = line_len;
3227 else
3229 edit->curs_col = prev + over;
3230 edit->prev_col = edit->curs_col;
3231 edit->over_col = 0;
3234 else
3236 edit->over_col = 0;
3237 if (option_fake_half_tabs && is_in_indent (edit))
3239 edit_update_curs_col (edit);
3240 if (space_width != 0 && edit->curs_col % (HALF_TAB_SIZE * space_width) != 0)
3242 int q;
3244 q = edit->curs_col;
3245 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3246 p = edit_bol (edit, edit->curs1);
3247 edit_cursor_move (edit,
3248 edit_move_forward3 (edit, p, edit->curs_col,
3249 0) - edit->curs1);
3250 if (!left_of_four_spaces (edit))
3251 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3257 /* --------------------------------------------------------------------------------------------- */
3258 /** check whether line in editor is blank or not
3260 * @param edit editor object
3261 * @param line number of line
3263 * @return TRUE if line in blank, FALSE otherwise
3266 gboolean
3267 line_is_blank (WEdit * edit, long line)
3269 return is_blank (edit, edit_find_line (edit, line));
3272 /* --------------------------------------------------------------------------------------------- */
3273 /** move cursor to line 'line' */
3275 void
3276 edit_move_to_line (WEdit * e, long line)
3278 if (line < e->curs_line)
3279 edit_move_up (e, e->curs_line - line, 0);
3280 else
3281 edit_move_down (e, line - e->curs_line, 0);
3282 edit_scroll_screen_over_cursor (e);
3285 /* --------------------------------------------------------------------------------------------- */
3286 /** scroll window so that first visible line is 'line' */
3288 void
3289 edit_move_display (WEdit * e, long line)
3291 if (line < e->start_line)
3292 edit_scroll_upward (e, e->start_line - line);
3293 else
3294 edit_scroll_downward (e, line - e->start_line);
3297 /* --------------------------------------------------------------------------------------------- */
3298 /** save markers onto undo stack */
3300 void
3301 edit_push_markers (WEdit * edit)
3303 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3304 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3305 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3308 /* --------------------------------------------------------------------------------------------- */
3310 void
3311 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3313 edit->mark1 = m1;
3314 edit->mark2 = m2;
3315 edit->column1 = c1;
3316 edit->column2 = c2;
3319 /* --------------------------------------------------------------------------------------------- */
3320 /** highlight marker toggle */
3322 void
3323 edit_mark_cmd (WEdit * edit, gboolean unmark)
3325 edit_push_markers (edit);
3326 if (unmark)
3328 edit_set_markers (edit, 0, 0, 0, 0);
3329 edit->force |= REDRAW_PAGE;
3331 else if (edit->mark2 >= 0)
3333 edit->end_mark_curs = -1;
3334 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3335 edit->curs_col + edit->over_col);
3336 edit->force |= REDRAW_PAGE;
3338 else
3340 edit->end_mark_curs = edit->curs1;
3341 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3342 edit->curs_col + edit->over_col);
3346 /* --------------------------------------------------------------------------------------------- */
3347 /** highlight the word under cursor */
3349 void
3350 edit_mark_current_word_cmd (WEdit * edit)
3352 long pos;
3354 for (pos = edit->curs1; pos != 0; pos--)
3356 int c1, c2;
3358 c1 = edit_get_byte (edit, pos);
3359 c2 = edit_get_byte (edit, pos - 1);
3360 if (!isspace (c1) && isspace (c2))
3361 break;
3362 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3363 break;
3365 edit->mark1 = pos;
3367 for (; pos < edit->last_byte; pos++)
3369 int c1, c2;
3371 c1 = edit_get_byte (edit, pos);
3372 c2 = edit_get_byte (edit, pos + 1);
3373 if (!isspace (c1) && isspace (c2))
3374 break;
3375 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3376 break;
3378 edit->mark2 = min (pos + 1, edit->last_byte);
3380 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3383 /* --------------------------------------------------------------------------------------------- */
3385 void
3386 edit_mark_current_line_cmd (WEdit * edit)
3388 long pos = edit->curs1;
3390 edit->mark1 = edit_bol (edit, pos);
3391 edit->mark2 = edit_eol (edit, pos);
3393 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3396 /* --------------------------------------------------------------------------------------------- */
3398 void
3399 edit_delete_line (WEdit * edit)
3402 * Delete right part of the line.
3403 * Note that edit_get_byte() returns '\n' when byte position is
3404 * beyond EOF.
3406 while (edit_get_byte (edit, edit->curs1) != '\n')
3407 (void) edit_delete (edit, 1);
3410 * Delete '\n' char.
3411 * Note that edit_delete() will not corrupt anything if called while
3412 * cursor position is EOF.
3414 (void) edit_delete (edit, 1);
3417 * Delete left part of the line.
3418 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3420 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3421 (void) edit_backspace (edit, 1);
3424 /* --------------------------------------------------------------------------------------------- */
3426 long
3427 edit_indent_width (WEdit * edit, off_t p)
3429 off_t q = p;
3431 /* move to the end of the leading whitespace of the line */
3432 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3433 q++;
3434 /* count the number of columns of indentation */
3435 return (long) edit_move_forward3 (edit, p, 0, q);
3438 /* --------------------------------------------------------------------------------------------- */
3440 void
3441 edit_insert_indent (WEdit * edit, int indent)
3443 if (!option_fill_tabs_with_spaces)
3445 while (indent >= TAB_SIZE)
3447 edit_insert (edit, '\t');
3448 indent -= TAB_SIZE;
3451 while (indent-- > 0)
3452 edit_insert (edit, ' ');
3455 /* --------------------------------------------------------------------------------------------- */
3457 void
3458 edit_push_key_press (WEdit * edit)
3460 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3461 if (edit->mark2 == -1)
3463 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3464 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3468 /* --------------------------------------------------------------------------------------------- */
3470 void
3471 edit_find_bracket (WEdit * edit)
3473 edit->bracket = edit_get_bracket (edit, 1, 10000);
3474 if (last_bracket != edit->bracket)
3475 edit->force |= REDRAW_PAGE;
3476 last_bracket = edit->bracket;
3479 /* --------------------------------------------------------------------------------------------- */
3481 * This executes a command as though the user initiated it through a key
3482 * press. Callback with WIDGET_KEY as a message calls this after
3483 * translating the key press. This function can be used to pass any
3484 * command to the editor. Note that the screen wouldn't update
3485 * automatically. Either of command or char_for_insertion must be
3486 * passed as -1. Commands are executed, and char_for_insertion is
3487 * inserted at the cursor.
3490 void
3491 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3493 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3494 || (macro_index < 0
3495 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3497 macro_index = 0;
3498 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3499 return;
3501 if (macro_index != -1)
3503 edit->force |= REDRAW_COMPLETELY;
3504 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3506 edit_store_macro_cmd (edit);
3507 macro_index = -1;
3508 return;
3510 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3512 edit_repeat_macro_cmd (edit);
3513 macro_index = -1;
3514 return;
3518 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3520 record_macro_buf[macro_index].action = command;
3521 record_macro_buf[macro_index++].ch = char_for_insertion;
3523 /* record the beginning of a set of editing actions initiated by a key press */
3524 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3525 edit_push_key_press (edit);
3527 edit_execute_cmd (edit, command, char_for_insertion);
3528 if (edit->column_highlight)
3529 edit->force |= REDRAW_PAGE;
3532 /* --------------------------------------------------------------------------------------------- */
3534 This executes a command at a lower level than macro recording.
3535 It also does not push a key_press onto the undo stack. This means
3536 that if it is called many times, a single undo command will undo
3537 all of them. It also does not check for the Undo command.
3539 void
3540 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3542 edit->force |= REDRAW_LINE;
3544 /* The next key press will unhighlight the found string, so update
3545 * the whole page */
3546 if (edit->found_len || edit->column_highlight)
3547 edit->force |= REDRAW_PAGE;
3549 switch (command)
3551 /* a mark command with shift-arrow */
3552 case CK_MarkLeft:
3553 case CK_MarkRight:
3554 case CK_MarkToWordBegin:
3555 case CK_MarkToWordEnd:
3556 case CK_MarkToHome:
3557 case CK_MarkToEnd:
3558 case CK_MarkUp:
3559 case CK_MarkDown:
3560 case CK_MarkPageUp:
3561 case CK_MarkPageDown:
3562 case CK_MarkToFileBegin:
3563 case CK_MarkToFileEnd:
3564 case CK_MarkToPageBegin:
3565 case CK_MarkToPageEnd:
3566 case CK_MarkScrollUp:
3567 case CK_MarkScrollDown:
3568 case CK_MarkParagraphUp:
3569 case CK_MarkParagraphDown:
3570 /* a mark command with alt-arrow */
3571 case CK_MarkColumnPageUp:
3572 case CK_MarkColumnPageDown:
3573 case CK_MarkColumnLeft:
3574 case CK_MarkColumnRight:
3575 case CK_MarkColumnUp:
3576 case CK_MarkColumnDown:
3577 case CK_MarkColumnScrollUp:
3578 case CK_MarkColumnScrollDown:
3579 case CK_MarkColumnParagraphUp:
3580 case CK_MarkColumnParagraphDown:
3581 edit->column_highlight = 0;
3582 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3584 edit_mark_cmd (edit, TRUE); /* clear */
3585 edit_mark_cmd (edit, FALSE); /* marking on */
3587 edit->highlight = 1;
3588 break;
3590 /* any other command */
3591 default:
3592 if (edit->highlight)
3593 edit_mark_cmd (edit, FALSE); /* clear */
3594 edit->highlight = 0;
3597 /* first check for undo */
3598 if (command == CK_Undo)
3600 edit->redo_stack_reset = 0;
3601 edit_group_undo (edit);
3602 edit->found_len = 0;
3603 edit->prev_col = edit_get_col (edit);
3604 edit->search_start = edit->curs1;
3605 return;
3607 /* check for redo */
3608 if (command == CK_Redo)
3610 edit->redo_stack_reset = 0;
3611 edit_do_redo (edit);
3612 edit->found_len = 0;
3613 edit->prev_col = edit_get_col (edit);
3614 edit->search_start = edit->curs1;
3615 return;
3618 edit->redo_stack_reset = 1;
3620 /* An ordinary key press */
3621 if (char_for_insertion >= 0)
3623 /* if non persistent selection and text selected */
3624 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3625 edit_block_delete_cmd (edit);
3627 if (edit->overwrite)
3629 /* remove char only one time, after input first byte, multibyte chars */
3630 #ifdef HAVE_CHARSET
3631 if (!mc_global.utf8_display || edit->charpoint == 0)
3632 #endif
3633 if (edit_get_byte (edit, edit->curs1) != '\n')
3634 edit_delete (edit, 0);
3636 if (option_cursor_beyond_eol && edit->over_col > 0)
3637 edit_insert_over (edit);
3638 #ifdef HAVE_CHARSET
3639 if (char_for_insertion > 255 && !mc_global.utf8_display)
3641 unsigned char str[6 + 1];
3642 size_t i = 0;
3643 int res;
3645 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3646 if (res == 0)
3648 str[0] = '.';
3649 str[1] = '\0';
3651 else
3653 str[res] = '\0';
3655 while (str[i] != 0 && i <= 6)
3657 char_for_insertion = str[i];
3658 edit_insert (edit, char_for_insertion);
3659 i++;
3662 else
3663 #endif
3664 edit_insert (edit, char_for_insertion);
3666 if (option_auto_para_formatting)
3668 format_paragraph (edit, 0);
3669 edit->force |= REDRAW_PAGE;
3671 else
3672 check_and_wrap_line (edit);
3673 edit->found_len = 0;
3674 edit->prev_col = edit_get_col (edit);
3675 edit->search_start = edit->curs1;
3676 edit_find_bracket (edit);
3677 return;
3680 switch (command)
3682 case CK_TopOnScreen:
3683 case CK_BottomOnScreen:
3684 case CK_Top:
3685 case CK_Bottom:
3686 case CK_PageUp:
3687 case CK_PageDown:
3688 case CK_Home:
3689 case CK_End:
3690 case CK_Up:
3691 case CK_Down:
3692 case CK_Left:
3693 case CK_Right:
3694 case CK_WordLeft:
3695 case CK_WordRight:
3696 if (!option_persistent_selections && edit->mark2 >= 0)
3698 if (edit->column_highlight)
3699 edit_push_undo_action (edit, COLUMN_ON);
3700 edit->column_highlight = 0;
3701 edit_mark_cmd (edit, TRUE);
3705 switch (command)
3707 case CK_TopOnScreen:
3708 case CK_BottomOnScreen:
3709 case CK_MarkToPageBegin:
3710 case CK_MarkToPageEnd:
3711 case CK_Up:
3712 case CK_Down:
3713 case CK_WordLeft:
3714 case CK_WordRight:
3715 case CK_MarkToWordBegin:
3716 case CK_MarkToWordEnd:
3717 case CK_MarkUp:
3718 case CK_MarkDown:
3719 case CK_MarkColumnUp:
3720 case CK_MarkColumnDown:
3721 if (edit->mark2 == -1)
3722 break; /*marking is following the cursor: may need to highlight a whole line */
3723 case CK_Left:
3724 case CK_Right:
3725 case CK_MarkLeft:
3726 case CK_MarkRight:
3727 edit->force |= REDRAW_CHAR_ONLY;
3730 /* basic cursor key commands */
3731 switch (command)
3733 case CK_BackSpace:
3734 /* if non persistent selection and text selected */
3735 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3736 edit_block_delete_cmd (edit);
3737 else if (option_cursor_beyond_eol && edit->over_col > 0)
3738 edit->over_col--;
3739 else if (option_backspace_through_tabs && is_in_indent (edit))
3740 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3741 edit_backspace (edit, 1);
3742 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3744 int i;
3746 for (i = 0; i < HALF_TAB_SIZE; i++)
3747 edit_backspace (edit, 1);
3749 else
3750 edit_backspace (edit, 0);
3751 break;
3752 case CK_Delete:
3753 /* if non persistent selection and text selected */
3754 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3755 edit_block_delete_cmd (edit);
3756 else
3758 if (option_cursor_beyond_eol && edit->over_col > 0)
3759 edit_insert_over (edit);
3761 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3763 int i;
3765 for (i = 1; i <= HALF_TAB_SIZE; i++)
3766 edit_delete (edit, 1);
3768 else
3769 edit_delete (edit, 0);
3771 break;
3772 case CK_DeleteToWordBegin:
3773 edit->over_col = 0;
3774 edit_left_delete_word (edit);
3775 break;
3776 case CK_DeleteToWordEnd:
3777 if (option_cursor_beyond_eol && edit->over_col > 0)
3778 edit_insert_over (edit);
3780 edit_right_delete_word (edit);
3781 break;
3782 case CK_DeleteLine:
3783 edit_delete_line (edit);
3784 break;
3785 case CK_DeleteToHome:
3786 edit_delete_to_line_begin (edit);
3787 break;
3788 case CK_DeleteToEnd:
3789 edit_delete_to_line_end (edit);
3790 break;
3791 case CK_Enter:
3792 edit->over_col = 0;
3793 if (option_auto_para_formatting)
3795 edit_double_newline (edit);
3796 if (option_return_does_auto_indent)
3797 edit_auto_indent (edit);
3798 format_paragraph (edit, 0);
3800 else
3802 edit_insert (edit, '\n');
3803 if (option_return_does_auto_indent)
3804 edit_auto_indent (edit);
3806 break;
3807 case CK_Return:
3808 edit_insert (edit, '\n');
3809 break;
3811 case CK_MarkColumnPageUp:
3812 edit->column_highlight = 1;
3813 case CK_PageUp:
3814 case CK_MarkPageUp:
3815 edit_move_up (edit, edit->widget.lines - 1, 1);
3816 break;
3817 case CK_MarkColumnPageDown:
3818 edit->column_highlight = 1;
3819 case CK_PageDown:
3820 case CK_MarkPageDown:
3821 edit_move_down (edit, edit->widget.lines - 1, 1);
3822 break;
3823 case CK_MarkColumnLeft:
3824 edit->column_highlight = 1;
3825 case CK_Left:
3826 case CK_MarkLeft:
3827 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3829 if (option_cursor_beyond_eol && edit->over_col > 0)
3830 edit->over_col--;
3831 else
3832 edit_cursor_move (edit, -HALF_TAB_SIZE);
3833 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3835 else
3836 edit_left_char_move_cmd (edit);
3837 break;
3838 case CK_MarkColumnRight:
3839 edit->column_highlight = 1;
3840 case CK_Right:
3841 case CK_MarkRight:
3842 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3844 edit_cursor_move (edit, HALF_TAB_SIZE);
3845 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3847 else
3848 edit_right_char_move_cmd (edit);
3849 break;
3850 case CK_TopOnScreen:
3851 case CK_MarkToPageBegin:
3852 edit_begin_page (edit);
3853 break;
3854 case CK_BottomOnScreen:
3855 case CK_MarkToPageEnd:
3856 edit_end_page (edit);
3857 break;
3858 case CK_WordLeft:
3859 case CK_MarkToWordBegin:
3860 edit->over_col = 0;
3861 edit_left_word_move_cmd (edit);
3862 break;
3863 case CK_WordRight:
3864 case CK_MarkToWordEnd:
3865 edit->over_col = 0;
3866 edit_right_word_move_cmd (edit);
3867 break;
3868 case CK_MarkColumnUp:
3869 edit->column_highlight = 1;
3870 case CK_Up:
3871 case CK_MarkUp:
3872 edit_move_up (edit, 1, 0);
3873 break;
3874 case CK_MarkColumnDown:
3875 edit->column_highlight = 1;
3876 case CK_Down:
3877 case CK_MarkDown:
3878 edit_move_down (edit, 1, 0);
3879 break;
3880 case CK_MarkColumnParagraphUp:
3881 edit->column_highlight = 1;
3882 case CK_ParagraphUp:
3883 case CK_MarkParagraphUp:
3884 edit_move_up_paragraph (edit, 0);
3885 break;
3886 case CK_MarkColumnParagraphDown:
3887 edit->column_highlight = 1;
3888 case CK_ParagraphDown:
3889 case CK_MarkParagraphDown:
3890 edit_move_down_paragraph (edit, 0);
3891 break;
3892 case CK_MarkColumnScrollUp:
3893 edit->column_highlight = 1;
3894 case CK_ScrollUp:
3895 case CK_MarkScrollUp:
3896 edit_move_up (edit, 1, 1);
3897 break;
3898 case CK_MarkColumnScrollDown:
3899 edit->column_highlight = 1;
3900 case CK_ScrollDown:
3901 case CK_MarkScrollDown:
3902 edit_move_down (edit, 1, 1);
3903 break;
3904 case CK_Home:
3905 case CK_MarkToHome:
3906 edit_cursor_to_bol (edit);
3907 break;
3908 case CK_End:
3909 case CK_MarkToEnd:
3910 edit_cursor_to_eol (edit);
3911 break;
3912 case CK_Tab:
3913 /* if text marked shift block */
3914 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3916 if (edit->mark2 < 0)
3917 edit_mark_cmd (edit, FALSE);
3918 edit_move_block_to_right (edit);
3920 else
3922 if (option_cursor_beyond_eol)
3923 edit_insert_over (edit);
3924 edit_tab_cmd (edit);
3925 if (option_auto_para_formatting)
3927 format_paragraph (edit, 0);
3928 edit->force |= REDRAW_PAGE;
3930 else
3931 check_and_wrap_line (edit);
3933 break;
3935 case CK_InsertOverwrite:
3936 edit->overwrite = !edit->overwrite;
3937 break;
3939 case CK_Mark:
3940 if (edit->mark2 >= 0)
3942 if (edit->column_highlight)
3943 edit_push_undo_action (edit, COLUMN_ON);
3944 edit->column_highlight = 0;
3946 edit_mark_cmd (edit, FALSE);
3947 break;
3948 case CK_MarkColumn:
3949 if (!edit->column_highlight)
3950 edit_push_undo_action (edit, COLUMN_OFF);
3951 edit->column_highlight = 1;
3952 edit_mark_cmd (edit, FALSE);
3953 break;
3954 case CK_MarkAll:
3955 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3956 edit->force |= REDRAW_PAGE;
3957 break;
3958 case CK_Unmark:
3959 if (edit->column_highlight)
3960 edit_push_undo_action (edit, COLUMN_ON);
3961 edit->column_highlight = 0;
3962 edit_mark_cmd (edit, TRUE);
3963 break;
3964 case CK_MarkWord:
3965 if (edit->column_highlight)
3966 edit_push_undo_action (edit, COLUMN_ON);
3967 edit->column_highlight = 0;
3968 edit_mark_current_word_cmd (edit);
3969 break;
3970 case CK_MarkLine:
3971 if (edit->column_highlight)
3972 edit_push_undo_action (edit, COLUMN_ON);
3973 edit->column_highlight = 0;
3974 edit_mark_current_line_cmd (edit);
3975 break;
3977 case CK_ShowNumbers:
3978 option_line_state = !option_line_state;
3979 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
3980 edit->force |= REDRAW_PAGE;
3981 break;
3983 case CK_ShowMargin:
3984 show_right_margin = !show_right_margin;
3985 edit->force |= REDRAW_PAGE;
3986 break;
3988 case CK_Bookmark:
3989 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3990 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3991 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3992 else
3993 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3994 break;
3995 case CK_BookmarkFlush:
3996 book_mark_flush (edit, BOOK_MARK_COLOR);
3997 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3998 edit->force |= REDRAW_PAGE;
3999 break;
4000 case CK_BookmarkNext:
4001 if (edit->book_mark != NULL)
4003 struct _book_mark *p;
4005 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4006 if (p->next != NULL)
4008 p = p->next;
4009 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4010 edit_move_display (edit, p->line - edit->widget.lines / 2);
4011 edit_move_to_line (edit, p->line);
4014 break;
4015 case CK_BookmarkPrev:
4016 if (edit->book_mark != NULL)
4018 struct _book_mark *p;
4020 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4021 while (p->line == edit->curs_line)
4022 if (p->prev != NULL)
4023 p = p->prev;
4024 if (p->line >= 0)
4026 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4027 edit_move_display (edit, p->line - edit->widget.lines / 2);
4028 edit_move_to_line (edit, p->line);
4031 break;
4033 case CK_Top:
4034 case CK_MarkToFileBegin:
4035 edit_move_to_top (edit);
4036 break;
4037 case CK_Bottom:
4038 case CK_MarkToFileEnd:
4039 edit_move_to_bottom (edit);
4040 break;
4042 case CK_Copy:
4043 if (option_cursor_beyond_eol && edit->over_col > 0)
4044 edit_insert_over (edit);
4045 edit_block_copy_cmd (edit);
4046 break;
4047 case CK_Remove:
4048 edit_block_delete_cmd (edit);
4049 break;
4050 case CK_Move:
4051 edit_block_move_cmd (edit);
4052 break;
4054 case CK_BlockShiftLeft:
4055 if (edit->mark1 != edit->mark2)
4056 edit_move_block_to_left (edit);
4057 break;
4058 case CK_BlockShiftRight:
4059 if (edit->mark1 != edit->mark2)
4060 edit_move_block_to_right (edit);
4061 break;
4062 case CK_Store:
4063 edit_copy_to_X_buf_cmd (edit);
4064 break;
4065 case CK_Cut:
4066 edit_cut_to_X_buf_cmd (edit);
4067 break;
4068 case CK_Paste:
4069 /* if non persistent selection and text selected */
4070 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4071 edit_block_delete_cmd (edit);
4072 if (option_cursor_beyond_eol && edit->over_col > 0)
4073 edit_insert_over (edit);
4074 edit_paste_from_X_buf_cmd (edit);
4075 break;
4076 case CK_History:
4077 edit_paste_from_history (edit);
4078 break;
4080 case CK_SaveAs:
4081 edit_save_as_cmd (edit);
4082 break;
4083 case CK_Save:
4084 edit_save_confirm_cmd (edit);
4085 break;
4086 case CK_EditFile:
4087 edit_load_cmd (edit, EDIT_FILE_COMMON);
4088 break;
4089 case CK_BlockSave:
4090 edit_save_block_cmd (edit);
4091 break;
4092 case CK_InsertFile:
4093 edit_insert_file_cmd (edit);
4094 break;
4096 case CK_FilePrev:
4097 edit_load_back_cmd (edit);
4098 break;
4099 case CK_FileNext:
4100 edit_load_forward_cmd (edit);
4101 break;
4103 case CK_EditSyntaxFile:
4104 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4105 break;
4106 case CK_SyntaxChoose:
4107 edit_syntax_dialog (edit);
4108 break;
4110 case CK_EditUserMenu:
4111 edit_load_cmd (edit, EDIT_FILE_MENU);
4112 break;
4114 case CK_SyntaxOnOff:
4115 option_syntax_highlighting ^= 1;
4116 if (option_syntax_highlighting == 1)
4117 edit_load_syntax (edit, NULL, edit->syntax_type);
4118 edit->force |= REDRAW_PAGE;
4119 break;
4121 case CK_ShowTabTws:
4122 enable_show_tabs_tws ^= 1;
4123 edit->force |= REDRAW_PAGE;
4124 break;
4126 case CK_Search:
4127 edit_search_cmd (edit, FALSE);
4128 break;
4129 case CK_SearchContinue:
4130 edit_search_cmd (edit, TRUE);
4131 break;
4132 case CK_Replace:
4133 edit_replace_cmd (edit, 0);
4134 break;
4135 case CK_ReplaceContinue:
4136 edit_replace_cmd (edit, 1);
4137 break;
4138 case CK_Complete:
4139 /* if text marked shift block */
4140 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4141 edit_move_block_to_left (edit);
4142 else
4143 edit_complete_word_cmd (edit);
4144 break;
4145 case CK_Find:
4146 edit_get_match_keyword_cmd (edit);
4147 break;
4148 case CK_Quit:
4149 dlg_stop (edit->widget.owner);
4150 break;
4151 case CK_EditNew:
4152 edit_new_cmd (edit);
4153 break;
4154 case CK_Help:
4155 edit_help_cmd (edit);
4156 break;
4157 case CK_Refresh:
4158 edit_refresh_cmd (edit);
4159 break;
4160 case CK_SaveSetup:
4161 save_setup_cmd ();
4162 break;
4163 case CK_About:
4164 edit_about ();
4165 break;
4166 case CK_LearnKeys:
4167 learn_keys ();
4168 break;
4169 case CK_Options:
4170 edit_options_dialog (edit);
4171 break;
4172 case CK_OptionsSaveMode:
4173 menu_save_mode_cmd ();
4174 break;
4175 case CK_Date:
4177 char s[BUF_MEDIUM];
4178 /* fool gcc to prevent a Y2K warning */
4179 char time_format[] = "_c";
4180 time_format[0] = '%';
4182 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4183 edit_print_string (edit, s);
4184 edit->force |= REDRAW_PAGE;
4186 break;
4187 case CK_Goto:
4188 edit_goto_cmd (edit);
4189 break;
4190 case CK_ParagraphFormat:
4191 format_paragraph (edit, 1);
4192 edit->force |= REDRAW_PAGE;
4193 break;
4194 case CK_MacroDelete:
4195 edit_delete_macro_cmd (edit);
4196 break;
4197 case CK_MatchBracket:
4198 edit_goto_matching_bracket (edit);
4199 break;
4200 case CK_UserMenu:
4201 user_menu (edit, NULL, -1);
4202 break;
4203 case CK_Sort:
4204 edit_sort_cmd (edit);
4205 break;
4206 case CK_ExternalCommand:
4207 edit_ext_cmd (edit);
4208 break;
4209 case CK_Mail:
4210 edit_mail_dialog (edit);
4211 break;
4212 case CK_Shell:
4213 view_other_cmd ();
4214 break;
4215 #ifdef HAVE_CHARSET
4216 case CK_SelectCodepage:
4217 edit_select_codepage_cmd (edit);
4218 break;
4219 #endif
4220 case CK_InsertLiteral:
4221 edit_insert_literal_cmd (edit);
4222 break;
4223 case CK_MacroStartStopRecord:
4224 edit_begin_end_macro_cmd (edit);
4225 break;
4226 case CK_RepeatStartStopRecord:
4227 edit_begin_end_repeat_cmd (edit);
4228 break;
4229 case CK_ExtendedKeyMap:
4230 edit->extmod = TRUE;
4231 break;
4232 default:
4233 break;
4236 /* CK_PipeBlock */
4237 if ((command / CK_PipeBlock (0)) == 1)
4238 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4240 /* keys which must set the col position, and the search vars */
4241 switch (command)
4243 case CK_Search:
4244 case CK_SearchContinue:
4245 case CK_Replace:
4246 case CK_ReplaceContinue:
4247 case CK_Complete:
4248 edit->prev_col = edit_get_col (edit);
4249 break;
4250 case CK_Up:
4251 case CK_MarkUp:
4252 case CK_MarkColumnUp:
4253 case CK_Down:
4254 case CK_MarkDown:
4255 case CK_MarkColumnDown:
4256 case CK_PageUp:
4257 case CK_MarkPageUp:
4258 case CK_MarkColumnPageUp:
4259 case CK_PageDown:
4260 case CK_MarkPageDown:
4261 case CK_MarkColumnPageDown:
4262 case CK_Top:
4263 case CK_MarkToFileBegin:
4264 case CK_Bottom:
4265 case CK_MarkToFileEnd:
4266 case CK_ParagraphUp:
4267 case CK_MarkParagraphUp:
4268 case CK_MarkColumnParagraphUp:
4269 case CK_ParagraphDown:
4270 case CK_MarkParagraphDown:
4271 case CK_MarkColumnParagraphDown:
4272 case CK_ScrollUp:
4273 case CK_MarkScrollUp:
4274 case CK_MarkColumnScrollUp:
4275 case CK_ScrollDown:
4276 case CK_MarkScrollDown:
4277 case CK_MarkColumnScrollDown:
4278 edit->search_start = edit->curs1;
4279 edit->found_len = 0;
4280 break;
4281 default:
4282 edit->found_len = 0;
4283 edit->prev_col = edit_get_col (edit);
4284 edit->search_start = edit->curs1;
4286 edit_find_bracket (edit);
4288 if (option_auto_para_formatting)
4290 switch (command)
4292 case CK_BackSpace:
4293 case CK_Delete:
4294 case CK_DeleteToWordBegin:
4295 case CK_DeleteToWordEnd:
4296 case CK_DeleteToHome:
4297 case CK_DeleteToEnd:
4298 format_paragraph (edit, 0);
4299 edit->force |= REDRAW_PAGE;
4304 /* --------------------------------------------------------------------------------------------- */
4306 void
4307 edit_stack_init (void)
4309 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4311 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4312 edit_history_moveto[edit_stack_iterator].line = -1;
4315 edit_stack_iterator = 0;
4318 /* --------------------------------------------------------------------------------------------- */
4320 void
4321 edit_stack_free (void)
4323 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4324 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4327 /* --------------------------------------------------------------------------------------------- */
4328 /** move i lines */
4330 void
4331 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4333 edit_move_updown (edit, i, do_scroll, TRUE);
4336 /* --------------------------------------------------------------------------------------------- */
4337 /** move i lines */
4339 void
4340 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4342 edit_move_updown (edit, i, do_scroll, FALSE);
4345 /* --------------------------------------------------------------------------------------------- */