fixed doxygen.cfg, excluded directory /tests/ from doxygen path's
[midnight-commander.git] / src / editor / edit.c
blobd326d0fe758c5e91e688fceed5f3a40552ff2835
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 /* --------------------------------------------------------------------------------------------- */
1436 static void
1437 edit_do_redo (WEdit * edit)
1439 long ac;
1440 long count = 0;
1442 if (edit->redo_stack_reset)
1443 return;
1445 edit->over_col = 0;
1446 while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1448 switch ((int) ac)
1450 case STACK_BOTTOM:
1451 goto done_redo;
1452 case CURS_RIGHT:
1453 edit_cursor_move (edit, 1);
1454 break;
1455 case CURS_LEFT:
1456 edit_cursor_move (edit, -1);
1457 break;
1458 case BACKSPACE:
1459 edit_backspace (edit, 1);
1460 break;
1461 case DELCHAR:
1462 edit_delete (edit, 1);
1463 break;
1464 case COLUMN_ON:
1465 edit->column_highlight = 1;
1466 break;
1467 case COLUMN_OFF:
1468 edit->column_highlight = 0;
1469 break;
1471 if (ac >= 256 && ac < 512)
1472 edit_insert_ahead (edit, ac - 256);
1473 if (ac >= 0 && ac < 256)
1474 edit_insert (edit, ac);
1476 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1478 edit->mark1 = ac - MARK_1;
1479 edit->column1 =
1480 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1482 else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1484 edit->mark2 = ac - MARK_2;
1485 edit->column2 =
1486 (long) edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1488 /* more than one pop usually means something big */
1489 if (count++)
1490 edit->force |= REDRAW_PAGE;
1493 if (edit->start_display > ac - KEY_PRESS)
1495 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1496 edit->force |= REDRAW_PAGE;
1498 else if (edit->start_display < ac - KEY_PRESS)
1500 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1501 edit->force |= REDRAW_PAGE;
1503 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1504 edit_update_curs_row (edit);
1506 done_redo:
1510 /* --------------------------------------------------------------------------------------------- */
1512 static void
1513 edit_group_undo (WEdit * edit)
1515 long ac = KEY_PRESS;
1516 long cur_ac = KEY_PRESS;
1517 while (ac != STACK_BOTTOM && ac == cur_ac)
1519 cur_ac = get_prev_undo_action (edit);
1520 edit_do_undo (edit);
1521 ac = get_prev_undo_action (edit);
1522 /* exit from cycle if option_group_undo is not set,
1523 * and make single UNDO operation
1525 if (!option_group_undo)
1526 ac = STACK_BOTTOM;
1530 /* --------------------------------------------------------------------------------------------- */
1532 static void
1533 edit_delete_to_line_end (WEdit * edit)
1535 while (edit_get_byte (edit, edit->curs1) != '\n' && edit->curs2 != 0)
1536 edit_delete (edit, 1);
1539 /* --------------------------------------------------------------------------------------------- */
1541 static void
1542 edit_delete_to_line_begin (WEdit * edit)
1544 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 != 0)
1545 edit_backspace (edit, 1);
1548 /* --------------------------------------------------------------------------------------------- */
1550 static gboolean
1551 is_aligned_on_a_tab (WEdit * edit)
1553 long curs_col;
1555 edit_update_curs_col (edit);
1556 curs_col = edit->curs_col % (TAB_SIZE * space_width);
1557 return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1560 /* --------------------------------------------------------------------------------------------- */
1562 static gboolean
1563 right_of_four_spaces (WEdit * edit)
1565 int i, ch = 0;
1567 for (i = 1; i <= HALF_TAB_SIZE; i++)
1568 ch |= edit_get_byte (edit, edit->curs1 - i);
1570 return (ch == ' ' && is_aligned_on_a_tab (edit));
1573 /* --------------------------------------------------------------------------------------------- */
1575 static gboolean
1576 left_of_four_spaces (WEdit * edit)
1578 int i, ch = 0;
1580 for (i = 0; i < HALF_TAB_SIZE; i++)
1581 ch |= edit_get_byte (edit, edit->curs1 + i);
1583 return (ch == ' ' && is_aligned_on_a_tab (edit));
1586 /* --------------------------------------------------------------------------------------------- */
1588 static void
1589 edit_auto_indent (WEdit * edit)
1591 off_t p;
1592 char c;
1593 p = edit->curs1;
1594 /* use the previous line as a template */
1595 p = edit_move_backward (edit, p, 1);
1596 /* copy the leading whitespace of the line */
1597 for (;;)
1598 { /* no range check - the line _is_ \n-terminated */
1599 c = edit_get_byte (edit, p++);
1600 if (c != ' ' && c != '\t')
1601 break;
1602 edit_insert (edit, c);
1606 /* --------------------------------------------------------------------------------------------- */
1608 static inline void
1609 edit_double_newline (WEdit * edit)
1611 edit_insert (edit, '\n');
1612 if (edit_get_byte (edit, edit->curs1) == '\n' || edit_get_byte (edit, edit->curs1 - 2) == '\n')
1613 return;
1614 edit->force |= REDRAW_PAGE;
1615 edit_insert (edit, '\n');
1619 /* --------------------------------------------------------------------------------------------- */
1621 static void
1622 insert_spaces_tab (WEdit * edit, gboolean half)
1624 long i;
1626 edit_update_curs_col (edit);
1627 i = option_tab_spacing * space_width;
1628 if (half)
1629 i /= 2;
1630 i = ((edit->curs_col / i) + 1) * i - edit->curs_col;
1631 while (i > 0)
1633 edit_insert (edit, ' ');
1634 i -= space_width;
1638 /* --------------------------------------------------------------------------------------------- */
1640 static inline void
1641 edit_tab_cmd (WEdit * edit)
1643 if (option_fake_half_tabs && is_in_indent (edit))
1645 /*insert a half tab (usually four spaces) unless there is a
1646 half tab already behind, then delete it and insert a
1647 full tab. */
1648 if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
1649 insert_spaces_tab (edit, TRUE);
1650 else
1652 int i;
1654 for (i = 1; i <= HALF_TAB_SIZE; i++)
1655 edit_backspace (edit, 1);
1656 edit_insert (edit, '\t');
1659 else if (option_fill_tabs_with_spaces)
1660 insert_spaces_tab (edit, FALSE);
1661 else
1662 edit_insert (edit, '\t');
1665 /* --------------------------------------------------------------------------------------------- */
1667 static void
1668 check_and_wrap_line (WEdit * edit)
1670 off_t curs;
1671 int c;
1673 if (!option_typewriter_wrap)
1674 return;
1675 edit_update_curs_col (edit);
1676 if (edit->curs_col < option_word_wrap_line_length)
1677 return;
1678 curs = edit->curs1;
1679 for (;;)
1681 curs--;
1682 c = edit_get_byte (edit, curs);
1683 if (c == '\n' || curs <= 0)
1685 edit_insert (edit, '\n');
1686 return;
1688 if (c == ' ' || c == '\t')
1690 off_t current = edit->curs1;
1691 edit_cursor_move (edit, curs - edit->curs1 + 1);
1692 edit_insert (edit, '\n');
1693 edit_cursor_move (edit, current - edit->curs1 + 1);
1694 return;
1699 /* --------------------------------------------------------------------------------------------- */
1700 /** this find the matching bracket in either direction, and sets edit->bracket
1702 * @param edit editor object
1703 * @param in_screen seach only on the current screen
1704 * @param furthest_bracket_search count of the bytes for search
1706 * @return position of the found bracket (-1 if no match)
1709 static off_t
1710 edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_bracket_search)
1712 const char *const b = "{}{[][()(", *p;
1713 int i = 1, a, inc = -1, c, d, n = 0;
1714 unsigned long j = 0;
1715 off_t q;
1716 edit_update_curs_row (edit);
1717 c = edit_get_byte (edit, edit->curs1);
1718 p = strchr (b, c);
1719 /* no limit */
1720 if (!furthest_bracket_search)
1721 furthest_bracket_search--;
1722 /* not on a bracket at all */
1723 if (p == NULL)
1724 return -1;
1725 /* the matching bracket */
1726 d = p[1];
1727 /* going left or right? */
1728 if (strchr ("{[(", c))
1729 inc = 1;
1730 for (q = edit->curs1 + inc;; q += inc)
1732 /* out of buffer? */
1733 if (q >= edit->last_byte || q < 0)
1734 break;
1735 a = edit_get_byte (edit, q);
1736 /* don't want to eat CPU */
1737 if (j++ > furthest_bracket_search)
1738 break;
1739 /* out of screen? */
1740 if (in_screen)
1742 if (q < edit->start_display)
1743 break;
1744 /* count lines if searching downward */
1745 if (inc > 0 && a == '\n')
1746 if (n++ >= edit->widget.lines - edit->curs_row) /* out of screen */
1747 break;
1749 /* count bracket depth */
1750 i += (a == c) - (a == d);
1751 /* return if bracket depth is zero */
1752 if (i == 0)
1753 return q;
1755 /* no match */
1756 return -1;
1759 /* --------------------------------------------------------------------------------------------- */
1761 static inline void
1762 edit_goto_matching_bracket (WEdit * edit)
1764 off_t q;
1766 q = edit_get_bracket (edit, 0, 0);
1767 if (q >= 0)
1769 edit->bracket = edit->curs1;
1770 edit->force |= REDRAW_PAGE;
1771 edit_cursor_move (edit, q - edit->curs1);
1775 /* --------------------------------------------------------------------------------------------- */
1777 static void
1778 edit_move_block_to_right (WEdit * edit)
1780 off_t start_mark, end_mark;
1781 long cur_bol, start_bol;
1783 if (eval_marks (edit, &start_mark, &end_mark))
1784 return;
1786 start_bol = edit_bol (edit, start_mark);
1787 cur_bol = edit_bol (edit, end_mark - 1);
1791 edit_cursor_move (edit, cur_bol - edit->curs1);
1792 if (option_fill_tabs_with_spaces)
1793 insert_spaces_tab (edit, option_fake_half_tabs);
1794 else
1795 edit_insert (edit, '\t');
1796 edit_cursor_move (edit, edit_bol (edit, cur_bol) - edit->curs1);
1798 if (cur_bol == 0)
1799 break;
1801 cur_bol = edit_bol (edit, cur_bol - 1);
1803 while (cur_bol >= start_bol);
1805 edit->force |= REDRAW_PAGE;
1808 /* --------------------------------------------------------------------------------------------- */
1810 static void
1811 edit_move_block_to_left (WEdit * edit)
1813 off_t start_mark, end_mark;
1814 off_t cur_bol, start_bol;
1815 int i;
1817 if (eval_marks (edit, &start_mark, &end_mark))
1818 return;
1820 start_bol = edit_bol (edit, start_mark);
1821 cur_bol = edit_bol (edit, end_mark - 1);
1825 int del_tab_width;
1826 int next_char;
1828 edit_cursor_move (edit, cur_bol - edit->curs1);
1830 if (option_fake_half_tabs)
1831 del_tab_width = HALF_TAB_SIZE;
1832 else
1833 del_tab_width = option_tab_spacing;
1835 next_char = edit_get_byte (edit, edit->curs1);
1836 if (next_char == '\t')
1837 edit_delete (edit, 1);
1838 else if (next_char == ' ')
1839 for (i = 1; i <= del_tab_width; i++)
1841 if (next_char == ' ')
1842 edit_delete (edit, 1);
1843 next_char = edit_get_byte (edit, edit->curs1);
1846 if (cur_bol == 0)
1847 break;
1849 cur_bol = edit_bol (edit, cur_bol - 1);
1851 while (cur_bol >= start_bol);
1853 edit->force |= REDRAW_PAGE;
1856 /* --------------------------------------------------------------------------------------------- */
1858 * prints at the cursor
1859 * @returns the number of chars printed
1862 static size_t
1863 edit_print_string (WEdit * e, const char *s)
1865 size_t i = 0;
1867 while (s[i] != '\0')
1868 edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i++]);
1869 e->force |= REDRAW_COMPLETELY;
1870 edit_update_screen (e);
1871 return i;
1874 /* --------------------------------------------------------------------------------------------- */
1875 /*** public functions ****************************************************************************/
1876 /* --------------------------------------------------------------------------------------------- */
1878 /** User edit menu, like user menu (F2) but only in editor. */
1880 void
1881 user_menu (WEdit * edit, const char *menu_file, int selected_entry)
1883 char *block_file;
1884 int nomark;
1885 off_t curs;
1886 off_t start_mark, end_mark;
1887 struct stat status;
1888 vfs_path_t *block_file_vpath;
1890 block_file = mc_config_get_full_path (EDIT_BLOCK_FILE);
1891 block_file_vpath = vfs_path_from_str (block_file);
1892 curs = edit->curs1;
1893 nomark = eval_marks (edit, &start_mark, &end_mark);
1894 if (nomark == 0)
1895 edit_save_block (edit, block_file, start_mark, end_mark);
1897 /* run shell scripts from menu */
1898 if (user_menu_cmd (edit, menu_file, selected_entry)
1899 && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1901 int rc = 0;
1902 FILE *fd;
1904 /* i.e. we have marked block */
1905 if (nomark == 0)
1906 rc = edit_block_delete_cmd (edit);
1908 if (rc == 0)
1910 off_t ins_len;
1912 ins_len = edit_insert_file (edit, block_file_vpath);
1913 if (nomark == 0 && ins_len > 0)
1914 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1916 /* truncate block file */
1917 fd = fopen (block_file, "w");
1918 if (fd != NULL)
1919 fclose (fd);
1921 edit_cursor_move (edit, curs - edit->curs1);
1922 edit_refresh_cmd (edit);
1923 edit->force |= REDRAW_COMPLETELY;
1925 g_free (block_file);
1926 vfs_path_free (block_file_vpath);
1929 /* --------------------------------------------------------------------------------------------- */
1932 edit_get_byte (WEdit * edit, off_t byte_index)
1934 off_t p;
1936 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1937 return '\n';
1939 if (byte_index >= edit->curs1)
1941 p = edit->curs1 + edit->curs2 - byte_index - 1;
1942 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
1945 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
1948 /* --------------------------------------------------------------------------------------------- */
1950 #ifdef HAVE_CHARSET
1952 edit_get_utf (WEdit * edit, off_t byte_index, int *char_width)
1954 gchar *str = NULL;
1955 int res = -1;
1956 gunichar ch;
1957 gchar *next_ch = NULL;
1958 int width = 0;
1959 gchar utf8_buf[UTF8_CHAR_LEN + 1];
1961 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
1963 *char_width = 0;
1964 return '\n';
1967 str = edit_get_byte_ptr (edit, byte_index);
1969 if (str == NULL)
1971 *char_width = 0;
1972 return 0;
1975 res = g_utf8_get_char_validated (str, -1);
1977 if (res < 0)
1979 /* Retry with explicit bytes to make sure it's not a buffer boundary */
1980 int i;
1981 for (i = 0; i < UTF8_CHAR_LEN; i++)
1982 utf8_buf[i] = edit_get_byte (edit, byte_index + i);
1983 utf8_buf[UTF8_CHAR_LEN] = '\0';
1984 str = utf8_buf;
1985 res = g_utf8_get_char_validated (str, -1);
1988 if (res < 0)
1990 ch = *str;
1991 width = 0;
1993 else
1995 ch = res;
1996 /* Calculate UTF-8 char width */
1997 next_ch = g_utf8_next_char (str);
1998 if (next_ch)
2000 width = next_ch - str;
2002 else
2004 ch = 0;
2005 width = 0;
2008 *char_width = width;
2009 return ch;
2011 #endif
2013 /* --------------------------------------------------------------------------------------------- */
2015 char *
2016 edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * filename_vpath)
2018 int i;
2019 char *p, *writename;
2020 const vfs_path_element_t *path_element;
2022 i = edit_find_filter (filename_vpath);
2023 if (i < 0)
2024 return NULL;
2026 path_element = vfs_path_get_by_index (write_name_vpath, -1);
2027 writename = name_quote (path_element->path, 0);
2028 p = g_strdup_printf (all_filters[i].write, writename);
2029 g_free (writename);
2030 return p;
2033 /* --------------------------------------------------------------------------------------------- */
2035 * @param edit editor object
2036 * @param f value of stream file
2037 * @returns the length of the file
2040 off_t
2041 edit_write_stream (WEdit * edit, FILE * f)
2043 long i;
2045 if (edit->lb == LB_ASIS)
2047 for (i = 0; i < edit->last_byte; i++)
2048 if (fputc (edit_get_byte (edit, i), f) < 0)
2049 break;
2050 return i;
2053 /* change line breaks */
2054 for (i = 0; i < edit->last_byte; i++)
2056 unsigned char c = edit_get_byte (edit, i);
2058 if (!(c == '\n' || c == '\r'))
2060 /* not line break */
2061 if (fputc (c, f) < 0)
2062 return i;
2064 else
2065 { /* (c == '\n' || c == '\r') */
2066 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
2068 switch (edit->lb)
2070 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
2071 /* put one line break unconditionally */
2072 if (fputc ('\n', f) < 0)
2073 return i;
2075 i++; /* 2 chars are processed */
2077 if (c == '\r' && c1 == '\n')
2078 /* Windows line break; go to the next char */
2079 break;
2081 if (c == '\r' && c1 == '\r')
2083 /* two Macintosh line breaks; put second line break */
2084 if (fputc ('\n', f) < 0)
2085 return i;
2086 break;
2089 if (fputc (c1, f) < 0)
2090 return i;
2091 break;
2093 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
2094 /* put one line break unconditionally */
2095 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
2096 return i;
2098 if (c == '\r' && c1 == '\n')
2099 /* Windows line break; go to the next char */
2100 i++;
2101 break;
2103 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
2104 /* put one line break unconditionally */
2105 if (fputc ('\r', f) < 0)
2106 return i;
2108 i++; /* 2 chars are processed */
2110 if (c == '\r' && c1 == '\n')
2111 /* Windows line break; go to the next char */
2112 break;
2114 if (c == '\n' && c1 == '\n')
2116 /* two Windows line breaks; put second line break */
2117 if (fputc ('\r', f) < 0)
2118 return i;
2119 break;
2122 if (fputc (c1, f) < 0)
2123 return i;
2124 break;
2125 case LB_ASIS: /* default without changes */
2126 break;
2131 return edit->last_byte;
2134 /* --------------------------------------------------------------------------------------------- */
2135 /** inserts a file at the cursor, returns count of inserted bytes on success */
2136 long
2137 edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
2139 char *p;
2140 off_t ins_len = 0;
2142 p = edit_get_filter (filename_vpath);
2143 if (p != NULL)
2145 FILE *f;
2146 off_t current = edit->curs1;
2148 f = (FILE *) popen (p, "r");
2149 if (f != NULL)
2151 edit_insert_stream (edit, f);
2152 ins_len = edit->curs1 - current;
2153 edit_cursor_move (edit, -ins_len);
2154 if (pclose (f) > 0)
2156 char *errmsg;
2158 errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
2159 edit_error_dialog (_("Error"), errmsg);
2160 g_free (errmsg);
2161 ins_len = -1;
2164 else
2166 char *errmsg;
2168 errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
2169 edit_error_dialog (_("Error"), errmsg);
2170 g_free (errmsg);
2171 ins_len = -1;
2173 g_free (p);
2175 else
2177 int file;
2178 off_t blocklen;
2179 off_t current = edit->curs1;
2180 int vertical_insertion = 0;
2181 char *buf;
2183 file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2184 if (file == -1)
2185 return -1;
2187 buf = g_malloc0 (TEMP_BUF_LEN);
2188 blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2189 if (blocklen > 0)
2191 /* if contain signature VERTICAL_MAGIC then it vertical block */
2192 if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2193 vertical_insertion = 1;
2194 else
2195 mc_lseek (file, 0, SEEK_SET);
2198 if (vertical_insertion)
2200 off_t mark1, mark2;
2201 long c1, c2;
2203 blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2204 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2205 /* highlight inserted text then not persistent blocks */
2206 if (!option_persistent_selections)
2208 if (!edit->column_highlight)
2209 edit_push_undo_action (edit, COLUMN_OFF);
2210 edit->column_highlight = 1;
2213 else
2215 off_t i;
2217 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2219 for (i = 0; i < blocklen; i++)
2220 edit_insert (edit, buf[i]);
2222 /* highlight inserted text then not persistent blocks */
2223 if (!option_persistent_selections && edit->modified)
2225 edit_set_markers (edit, edit->curs1, current, 0, 0);
2226 if (edit->column_highlight)
2227 edit_push_undo_action (edit, COLUMN_ON);
2228 edit->column_highlight = 0;
2232 edit->force |= REDRAW_PAGE;
2233 ins_len = edit->curs1 - current;
2234 edit_cursor_move (edit, -ins_len);
2235 g_free (buf);
2236 mc_close (file);
2237 if (blocklen != 0)
2238 ins_len = 0;
2241 return ins_len;
2244 /* --------------------------------------------------------------------------------------------- */
2246 * Fill in the edit structure. Return NULL on failure. Pass edit as
2247 * NULL to allocate a new structure.
2249 * If line is 0, try to restore saved position. Otherwise put the
2250 * cursor on that line and show it in the middle of the screen.
2253 WEdit *
2254 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
2255 long line)
2257 gboolean to_free = FALSE;
2259 option_auto_syntax = 1; /* Resetting to auto on every invokation */
2260 if (option_line_state)
2261 option_line_state_width = LINE_STATE_WIDTH;
2262 else
2263 option_line_state_width = 0;
2265 if (edit == NULL)
2267 #ifdef ENABLE_NLS
2269 * Expand option_whole_chars_search by national letters using
2270 * current locale
2273 static char option_whole_chars_search_buf[256];
2275 if (option_whole_chars_search_buf != option_whole_chars_search)
2277 size_t i;
2278 size_t len = str_term_width1 (option_whole_chars_search);
2280 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
2282 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++)
2284 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i))
2286 option_whole_chars_search_buf[len++] = i;
2290 option_whole_chars_search_buf[len] = 0;
2291 option_whole_chars_search = option_whole_chars_search_buf;
2293 #endif /* ENABLE_NLS */
2294 edit = g_malloc0 (sizeof (WEdit));
2295 edit->search = NULL;
2296 to_free = TRUE;
2299 edit_purge_widget (edit);
2300 edit->widget.y = y;
2301 edit->widget.x = x;
2302 edit->widget.lines = lines;
2303 edit->widget.cols = cols;
2305 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2306 edit->stat1.st_uid = getuid ();
2307 edit->stat1.st_gid = getgid ();
2308 edit->stat1.st_mtime = 0;
2310 edit->over_col = 0;
2311 edit->bracket = -1;
2312 edit->force |= REDRAW_PAGE;
2313 edit_set_filename (edit, filename_vpath);
2315 edit->undo_stack_size = START_STACK_SIZE;
2316 edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2317 edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2319 edit->redo_stack_size = START_STACK_SIZE;
2320 edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2321 edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2323 #ifdef HAVE_CHARSET
2324 edit->utf8 = FALSE;
2325 edit->converter = str_cnv_from_term;
2326 edit_set_codeset (edit);
2327 #endif
2329 if (!edit_load_file (edit))
2331 /* edit_load_file already gives an error message */
2332 if (to_free)
2333 g_free (edit);
2334 return NULL;
2337 edit->loading_done = 1;
2338 edit->modified = 0;
2339 edit->locked = 0;
2340 edit_load_syntax (edit, NULL, NULL);
2342 int color;
2343 edit_get_syntax_color (edit, -1, &color);
2346 /* load saved cursor position */
2347 if ((line == 0) && option_save_position)
2348 edit_load_position (edit);
2349 else
2351 if (line <= 0)
2352 line = 1;
2353 edit_move_display (edit, line - 1);
2354 edit_move_to_line (edit, line - 1);
2357 edit_load_macro_cmd (edit);
2358 return edit;
2361 /* --------------------------------------------------------------------------------------------- */
2363 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2364 gboolean
2365 edit_clean (WEdit * edit)
2367 int j = 0;
2369 if (edit == NULL)
2370 return FALSE;
2372 /* a stale lock, remove it */
2373 if (edit->locked)
2374 edit->locked = unlock_file (edit->filename_vpath);
2376 /* save cursor position */
2377 if (option_save_position)
2378 edit_save_position (edit);
2379 else if (edit->serialized_bookmarks != NULL)
2380 edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
2382 /* File specified on the mcedit command line and never saved */
2383 if (edit->delete_file)
2384 unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2386 edit_free_syntax_rules (edit);
2387 book_mark_flush (edit, -1);
2388 for (; j <= MAXBUFF; j++)
2390 g_free (edit->buffers1[j]);
2391 g_free (edit->buffers2[j]);
2394 g_free (edit->undo_stack);
2395 g_free (edit->redo_stack);
2396 vfs_path_free (edit->filename_vpath);
2397 vfs_path_free (edit->dir_vpath);
2398 mc_search_free (edit->search);
2399 edit->search = NULL;
2401 #ifdef HAVE_CHARSET
2402 if (edit->converter != str_cnv_from_term)
2403 str_close_conv (edit->converter);
2404 #endif
2406 edit_purge_widget (edit);
2408 return TRUE;
2411 /* --------------------------------------------------------------------------------------------- */
2413 /** returns TRUE on success */
2414 gboolean
2415 edit_renew (WEdit * edit)
2417 int y = edit->widget.y;
2418 int x = edit->widget.x;
2419 int lines = edit->widget.lines;
2420 int columns = edit->widget.cols;
2422 edit_clean (edit);
2423 return (edit_init (edit, y, x, lines, columns, NULL, 0) != NULL);
2426 /* --------------------------------------------------------------------------------------------- */
2429 * Load a new file into the editor and set line. If it fails, preserve the old file.
2430 * To do it, allocate a new widget, initialize it and, if the new file
2431 * was loaded, copy the data to the old widget.
2433 * @returns TRUE on success, FALSE on failure.
2435 gboolean
2436 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
2438 WEdit *e;
2439 int y = edit->widget.y;
2440 int x = edit->widget.x;
2441 int lines = edit->widget.lines;
2442 int columns = edit->widget.cols;
2444 e = g_malloc0 (sizeof (WEdit));
2445 e->widget = edit->widget;
2447 if (edit_init (e, y, x, lines, columns, filename_vpath, line) == NULL)
2449 g_free (e);
2450 return FALSE;
2453 edit_clean (edit);
2454 memcpy (edit, e, sizeof (WEdit));
2455 g_free (e);
2457 return TRUE;
2460 /* --------------------------------------------------------------------------------------------- */
2462 #ifdef HAVE_CHARSET
2463 void
2464 edit_set_codeset (WEdit * edit)
2466 const char *cp_id;
2468 cp_id =
2469 get_codepage_id (mc_global.source_codepage >=
2470 0 ? mc_global.source_codepage : mc_global.display_codepage);
2472 if (cp_id != NULL)
2474 GIConv conv;
2475 conv = str_crt_conv_from (cp_id);
2476 if (conv != INVALID_CONV)
2478 if (edit->converter != str_cnv_from_term)
2479 str_close_conv (edit->converter);
2480 edit->converter = conv;
2484 if (cp_id != NULL)
2485 edit->utf8 = str_isutf8 (cp_id);
2487 #endif
2489 /* --------------------------------------------------------------------------------------------- */
2491 Recording stack for undo:
2492 The following is an implementation of a compressed stack. Identical
2493 pushes are recorded by a negative prefix indicating the number of times the
2494 same char was pushed. This saves space for repeated curs-left or curs-right
2495 delete etc.
2499 pushed: stored:
2503 b -3
2505 c --> -4
2511 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2512 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2513 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2514 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2515 position.
2517 The only way the cursor moves or the buffer is changed is through the routines:
2518 insert, backspace, insert_ahead, delete, and cursor_move.
2519 These record the reverse undo movements onto the stack each time they are
2520 called.
2522 Each key press results in a set of actions (insert; delete ...). So each time
2523 a key is pressed the current position of start_display is pushed as
2524 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2525 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2526 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2530 void
2531 edit_push_undo_action (WEdit * edit, long c, ...)
2533 unsigned long sp = edit->undo_stack_pointer;
2534 unsigned long spm1;
2535 long *t;
2537 /* first enlarge the stack if necessary */
2538 if (sp > edit->undo_stack_size - 10)
2539 { /* say */
2540 if (option_max_undo < 256)
2541 option_max_undo = 256;
2542 if (edit->undo_stack_size < (unsigned long) option_max_undo)
2544 t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2545 if (t != NULL)
2547 edit->undo_stack = t;
2548 edit->undo_stack_size <<= 1;
2549 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2553 spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2554 if (edit->undo_stack_disable)
2556 edit_push_redo_action (edit, KEY_PRESS);
2557 edit_push_redo_action (edit, c);
2558 return;
2560 if (edit->redo_stack_reset)
2561 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2563 if (edit->undo_stack_bottom != sp
2564 && spm1 != edit->undo_stack_bottom
2565 && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2567 int d;
2569 if (edit->undo_stack[spm1] < 0)
2571 d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2572 if (d == c && edit->undo_stack[spm1] > -1000000000)
2574 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2575 edit->undo_stack[spm1]--;
2576 return;
2579 else
2581 d = edit->undo_stack[spm1];
2582 if (d == c)
2584 if (c >= KEY_PRESS)
2585 return; /* --> no need to push multiple do-nothings */
2586 edit->undo_stack[sp] = -2;
2587 goto check_bottom;
2591 edit->undo_stack[sp] = c;
2593 check_bottom:
2594 edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2596 /* if the sp wraps round and catches the undo_stack_bottom then erase
2597 * the first set of actions on the stack to make space - by moving
2598 * undo_stack_bottom forward one "key press" */
2599 c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2600 if ((unsigned long) c == edit->undo_stack_bottom ||
2601 (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2604 edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2606 while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2607 && edit->undo_stack_bottom != edit->undo_stack_pointer);
2609 /*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: */
2610 if (edit->undo_stack_pointer != edit->undo_stack_bottom
2611 && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2613 edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2617 /* --------------------------------------------------------------------------------------------- */
2619 void
2620 edit_push_redo_action (WEdit * edit, long c, ...)
2622 unsigned long sp = edit->redo_stack_pointer;
2623 unsigned long spm1;
2624 long *t;
2626 /* first enlarge the stack if necessary */
2627 if (sp > edit->redo_stack_size - 10)
2628 { /* say */
2629 if (option_max_undo < 256)
2630 option_max_undo = 256;
2631 if (edit->redo_stack_size < (unsigned long) option_max_undo)
2633 t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2634 if (t != NULL)
2636 edit->redo_stack = t;
2637 edit->redo_stack_size <<= 1;
2638 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2642 spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2644 if (edit->redo_stack_bottom != sp
2645 && spm1 != edit->redo_stack_bottom
2646 && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2648 int d;
2650 if (edit->redo_stack[spm1] < 0)
2652 d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2653 if (d == c && edit->redo_stack[spm1] > -1000000000)
2655 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
2656 edit->redo_stack[spm1]--;
2657 return;
2660 else
2662 d = edit->redo_stack[spm1];
2663 if (d == c)
2665 if (c >= KEY_PRESS)
2666 return; /* --> no need to push multiple do-nothings */
2667 edit->redo_stack[sp] = -2;
2668 goto redo_check_bottom;
2672 edit->redo_stack[sp] = c;
2674 redo_check_bottom:
2675 edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2677 /* if the sp wraps round and catches the redo_stack_bottom then erase
2678 * the first set of actions on the stack to make space - by moving
2679 * redo_stack_bottom forward one "key press" */
2680 c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2681 if ((unsigned long) c == edit->redo_stack_bottom ||
2682 (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2685 edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2687 while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2688 && edit->redo_stack_bottom != edit->redo_stack_pointer);
2691 * If a single key produced enough pushes to wrap all the way round then
2692 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2693 * The stack is then initialised:
2696 if (edit->redo_stack_pointer != edit->redo_stack_bottom
2697 && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2698 edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2702 /* --------------------------------------------------------------------------------------------- */
2704 Basic low level single character buffer alterations and movements at the cursor.
2705 Returns char passed over, inserted or removed.
2708 void
2709 edit_insert (WEdit * edit, int c)
2711 /* check if file has grown to large */
2712 if (edit->last_byte >= SIZE_LIMIT)
2713 return;
2715 /* first we must update the position of the display window */
2716 if (edit->curs1 < edit->start_display)
2718 edit->start_display++;
2719 if (c == '\n')
2720 edit->start_line++;
2723 /* Mark file as modified, unless the file hasn't been fully loaded */
2724 if (edit->loading_done)
2725 edit_modification (edit);
2727 /* now we must update some info on the file and check if a redraw is required */
2728 if (c == '\n')
2730 book_mark_inc (edit, edit->curs_line);
2731 edit->curs_line++;
2732 edit->total_lines++;
2733 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2736 /* save the reverse command onto the undo stack */
2737 /* ordinary char and not space */
2738 if (c > 32)
2739 edit_push_undo_action (edit, BACKSPACE);
2740 else
2741 edit_push_undo_action (edit, BACKSPACE_BR);
2742 /* update markers */
2743 edit->mark1 += (edit->mark1 > edit->curs1);
2744 edit->mark2 += (edit->mark2 > edit->curs1);
2745 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
2747 /* add a new buffer if we've reached the end of the last one */
2748 if ((edit->curs1 & M_EDIT_BUF_SIZE) == 0)
2749 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2751 /* perform the insertion */
2752 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE]
2753 = (unsigned char) c;
2755 /* update file length */
2756 edit->last_byte++;
2758 /* update cursor position */
2759 edit->curs1++;
2762 /* --------------------------------------------------------------------------------------------- */
2763 /** same as edit_insert and move left */
2765 void
2766 edit_insert_ahead (WEdit * edit, int c)
2768 if (edit->last_byte >= SIZE_LIMIT)
2769 return;
2771 if (edit->curs1 < edit->start_display)
2773 edit->start_display++;
2774 if (c == '\n')
2775 edit->start_line++;
2777 edit_modification (edit);
2778 if (c == '\n')
2780 book_mark_inc (edit, edit->curs_line);
2781 edit->total_lines++;
2782 edit->force |= REDRAW_AFTER_CURSOR;
2784 /* ordinary char and not space */
2785 if (c > 32)
2786 edit_push_undo_action (edit, DELCHAR);
2787 else
2788 edit_push_undo_action (edit, DELCHAR_BR);
2790 edit->mark1 += (edit->mark1 >= edit->curs1);
2791 edit->mark2 += (edit->mark2 >= edit->curs1);
2792 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
2794 if (((edit->curs2 + 1) & M_EDIT_BUF_SIZE) == 0)
2795 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2796 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]
2797 [EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2799 edit->last_byte++;
2800 edit->curs2++;
2803 /* --------------------------------------------------------------------------------------------- */
2806 edit_delete (WEdit * edit, const int byte_delete)
2808 int p = 0;
2809 int cw = 1;
2810 int i;
2812 if (edit->curs2 == 0)
2813 return 0;
2815 #ifdef HAVE_CHARSET
2816 /* if byte_delete = 1 then delete only one byte not multibyte char */
2817 if (edit->utf8 && byte_delete == 0)
2819 edit_get_utf (edit, edit->curs1, &cw);
2820 if (cw < 1)
2821 cw = 1;
2823 #else
2824 (void) byte_delete;
2825 #endif
2827 if (edit->mark2 != edit->mark1)
2828 edit_push_markers (edit);
2830 for (i = 1; i <= cw; i++)
2832 if (edit->mark1 > edit->curs1)
2834 edit->mark1--;
2835 edit->end_mark_curs--;
2837 if (edit->mark2 > edit->curs1)
2838 edit->mark2--;
2839 if (edit->last_get_rule > edit->curs1)
2840 edit->last_get_rule--;
2842 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2843 ((edit->curs2 -
2844 1) & M_EDIT_BUF_SIZE) - 1];
2846 if ((edit->curs2 & M_EDIT_BUF_SIZE) == 0)
2848 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2849 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
2851 edit->last_byte--;
2852 edit->curs2--;
2853 edit_push_undo_action (edit, p + 256);
2856 edit_modification (edit);
2857 if (p == '\n')
2859 book_mark_dec (edit, edit->curs_line);
2860 edit->total_lines--;
2861 edit->force |= REDRAW_AFTER_CURSOR;
2863 if (edit->curs1 < edit->start_display)
2865 edit->start_display--;
2866 if (p == '\n')
2867 edit->start_line--;
2870 return p;
2873 /* --------------------------------------------------------------------------------------------- */
2874 /** moves the cursor right or left: increment positive or negative respectively */
2876 void
2877 edit_cursor_move (WEdit * edit, off_t increment)
2879 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2880 int c;
2882 if (increment < 0)
2884 for (; increment < 0; increment++)
2886 if (edit->curs1 == 0)
2887 return;
2889 edit_push_undo_action (edit, CURS_RIGHT);
2891 c = edit_get_byte (edit, edit->curs1 - 1);
2892 if (((edit->curs2 + 1) & M_EDIT_BUF_SIZE) == 0)
2893 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2894 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2895 (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
2896 edit->curs2++;
2897 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 -
2898 1) & M_EDIT_BUF_SIZE];
2899 if (((edit->curs1 - 1) & M_EDIT_BUF_SIZE) == 0)
2901 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
2902 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
2904 edit->curs1--;
2905 if (c == '\n')
2907 edit->curs_line--;
2908 edit->force |= REDRAW_LINE_BELOW;
2913 else
2915 for (; increment > 0; increment--)
2917 if (edit->curs2 == 0)
2918 return;
2920 edit_push_undo_action (edit, CURS_LEFT);
2922 c = edit_get_byte (edit, edit->curs1);
2923 if ((edit->curs1 & M_EDIT_BUF_SIZE) == 0)
2924 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
2925 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
2926 edit->curs1++;
2927 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE -
2928 ((edit->curs2 -
2929 1) & M_EDIT_BUF_SIZE) - 1];
2930 if ((edit->curs2 & M_EDIT_BUF_SIZE) == 0)
2932 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
2933 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
2935 edit->curs2--;
2936 if (c == '\n')
2938 edit->curs_line++;
2939 edit->force |= REDRAW_LINE_ABOVE;
2945 /* These functions return positions relative to lines */
2947 /* --------------------------------------------------------------------------------------------- */
2948 /** returns index of last char on line + 1 */
2950 off_t
2951 edit_eol (WEdit * edit, off_t current)
2953 if (current >= edit->last_byte)
2954 return edit->last_byte;
2956 for (; edit_get_byte (edit, current) != '\n'; current++)
2959 return current;
2962 /* --------------------------------------------------------------------------------------------- */
2963 /** returns index of first char on line */
2965 off_t
2966 edit_bol (WEdit * edit, off_t current)
2968 if (current <= 0)
2969 return 0;
2971 for (; edit_get_byte (edit, current - 1) != '\n'; current--)
2974 return current;
2977 /* --------------------------------------------------------------------------------------------- */
2979 long
2980 edit_count_lines (WEdit * edit, off_t current, off_t upto)
2982 long lines = 0;
2983 if (upto > edit->last_byte)
2984 upto = edit->last_byte;
2985 if (current < 0)
2986 current = 0;
2987 while (current < upto)
2988 if (edit_get_byte (edit, current++) == '\n')
2989 lines++;
2990 return lines;
2993 /* --------------------------------------------------------------------------------------------- */
2994 /* If lines is zero this returns the count of lines from current to upto. */
2995 /* If upto is zero returns index of lines forward current. */
2997 off_t
2998 edit_move_forward (WEdit * edit, off_t current, long lines, off_t upto)
3000 if (upto != 0)
3002 return (off_t) edit_count_lines (edit, current, upto);
3004 else
3006 long next;
3008 if (lines < 0)
3009 lines = 0;
3010 while (lines-- != 0)
3012 next = edit_eol (edit, current) + 1;
3013 if (next > edit->last_byte)
3014 break;
3015 current = next;
3017 return current;
3021 /* --------------------------------------------------------------------------------------------- */
3022 /** Returns offset of 'lines' lines up from current */
3024 off_t
3025 edit_move_backward (WEdit * edit, off_t current, long lines)
3027 if (lines < 0)
3028 lines = 0;
3029 current = edit_bol (edit, current);
3030 while (lines-- != 0 && current != 0)
3031 current = edit_bol (edit, current - 1);
3032 return current;
3035 /* --------------------------------------------------------------------------------------------- */
3036 /* If cols is zero this returns the count of columns from current to upto. */
3037 /* If upto is zero returns index of cols across from current. */
3039 off_t
3040 edit_move_forward3 (WEdit * edit, off_t current, long cols, off_t upto)
3042 off_t p, q;
3043 long col;
3045 if (upto != 0)
3047 q = upto;
3048 cols = -10;
3050 else
3051 q = edit->last_byte + 2;
3053 for (col = 0, p = current; p < q; p++)
3055 int c, orig_c;
3057 if (cols != -10)
3059 if (col == cols)
3060 return p;
3061 if (col > cols)
3062 return p - 1;
3065 orig_c = c = edit_get_byte (edit, p);
3067 #ifdef HAVE_CHARSET
3068 if (edit->utf8)
3070 int utf_ch;
3071 int cw = 1;
3073 utf_ch = edit_get_utf (edit, p, &cw);
3074 if (mc_global.utf8_display)
3076 if (cw > 1)
3077 col -= cw - 1;
3078 if (g_unichar_iswide (utf_ch))
3079 col++;
3081 else if (cw > 1 && g_unichar_isprint (utf_ch))
3082 col -= cw - 1;
3085 c = convert_to_display_c (c);
3086 #endif
3088 if (c == '\t')
3089 col += TAB_SIZE - col % TAB_SIZE;
3090 else if (c == '\n')
3091 return (upto != 0 ? (off_t) col : p);
3093 if ((c < 32 || c == 127) && (orig_c == c
3094 #ifdef HAVE_CHARSET
3095 || (!mc_global.utf8_display && !edit->utf8)
3096 #endif
3098 /* '\r' is shown as ^M, so we must advance 2 characters */
3099 /* Caret notation for control characters */
3100 col += 2;
3101 else
3102 col++;
3104 return (off_t) col;
3107 /* --------------------------------------------------------------------------------------------- */
3108 /** returns the current column position of the cursor */
3110 long
3111 edit_get_col (WEdit * edit)
3113 return (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3116 /* --------------------------------------------------------------------------------------------- */
3117 /* Scrolling functions */
3118 /* --------------------------------------------------------------------------------------------- */
3120 void
3121 edit_update_curs_row (WEdit * edit)
3123 edit->curs_row = edit->curs_line - edit->start_line;
3126 /* --------------------------------------------------------------------------------------------- */
3128 void
3129 edit_update_curs_col (WEdit * edit)
3131 edit->curs_col = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
3134 /* --------------------------------------------------------------------------------------------- */
3136 long
3137 edit_get_curs_col (const WEdit * edit)
3139 return edit->curs_col;
3142 /* --------------------------------------------------------------------------------------------- */
3143 /** moves the display start position up by i lines */
3145 void
3146 edit_scroll_upward (WEdit * edit, long i)
3148 long lines_above = edit->start_line;
3150 if (i > lines_above)
3151 i = lines_above;
3152 if (i != 0)
3154 edit->start_line -= i;
3155 edit->start_display = edit_move_backward (edit, edit->start_display, i);
3156 edit->force |= REDRAW_PAGE;
3157 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3159 edit_update_curs_row (edit);
3163 /* --------------------------------------------------------------------------------------------- */
3165 void
3166 edit_scroll_downward (WEdit * edit, long i)
3168 long lines_below;
3170 lines_below = edit->total_lines - edit->start_line - (edit->widget.lines - 1);
3171 if (lines_below > 0)
3173 if (i > lines_below)
3174 i = lines_below;
3175 edit->start_line += i;
3176 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
3177 edit->force |= REDRAW_PAGE;
3178 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3180 edit_update_curs_row (edit);
3183 /* --------------------------------------------------------------------------------------------- */
3185 void
3186 edit_scroll_right (WEdit * edit, long i)
3188 edit->force |= REDRAW_PAGE;
3189 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3190 edit->start_col -= i;
3193 /* --------------------------------------------------------------------------------------------- */
3195 void
3196 edit_scroll_left (WEdit * edit, long i)
3198 if (edit->start_col)
3200 edit->start_col += i;
3201 if (edit->start_col > 0)
3202 edit->start_col = 0;
3203 edit->force |= REDRAW_PAGE;
3204 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
3208 /* --------------------------------------------------------------------------------------------- */
3209 /* high level cursor movement commands */
3210 /* --------------------------------------------------------------------------------------------- */
3212 void
3213 edit_move_to_prev_col (WEdit * edit, off_t p)
3215 long prev = edit->prev_col;
3216 long over = edit->over_col;
3218 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
3220 if (option_cursor_beyond_eol)
3222 long line_len;
3224 line_len = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
3225 edit_eol (edit, edit->curs1));
3226 if (line_len < prev + edit->over_col)
3228 edit->over_col = prev + over - line_len;
3229 edit->prev_col = line_len;
3230 edit->curs_col = line_len;
3232 else
3234 edit->curs_col = prev + over;
3235 edit->prev_col = edit->curs_col;
3236 edit->over_col = 0;
3239 else
3241 edit->over_col = 0;
3242 if (option_fake_half_tabs && is_in_indent (edit))
3244 edit_update_curs_col (edit);
3245 if (space_width != 0 && edit->curs_col % (HALF_TAB_SIZE * space_width) != 0)
3247 int q;
3249 q = edit->curs_col;
3250 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
3251 p = edit_bol (edit, edit->curs1);
3252 edit_cursor_move (edit,
3253 edit_move_forward3 (edit, p, edit->curs_col,
3254 0) - edit->curs1);
3255 if (!left_of_four_spaces (edit))
3256 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
3262 /* --------------------------------------------------------------------------------------------- */
3263 /** check whether line in editor is blank or not
3265 * @param edit editor object
3266 * @param line number of line
3268 * @return TRUE if line in blank, FALSE otherwise
3271 gboolean
3272 line_is_blank (WEdit * edit, long line)
3274 return is_blank (edit, edit_find_line (edit, line));
3277 /* --------------------------------------------------------------------------------------------- */
3278 /** move cursor to line 'line' */
3280 void
3281 edit_move_to_line (WEdit * e, long line)
3283 if (line < e->curs_line)
3284 edit_move_up (e, e->curs_line - line, 0);
3285 else
3286 edit_move_down (e, line - e->curs_line, 0);
3287 edit_scroll_screen_over_cursor (e);
3290 /* --------------------------------------------------------------------------------------------- */
3291 /** scroll window so that first visible line is 'line' */
3293 void
3294 edit_move_display (WEdit * e, long line)
3296 if (line < e->start_line)
3297 edit_scroll_upward (e, e->start_line - line);
3298 else
3299 edit_scroll_downward (e, line - e->start_line);
3302 /* --------------------------------------------------------------------------------------------- */
3303 /** save markers onto undo stack */
3305 void
3306 edit_push_markers (WEdit * edit)
3308 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3309 edit_push_undo_action (edit, MARK_2 + edit->mark2);
3310 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3313 /* --------------------------------------------------------------------------------------------- */
3315 void
3316 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
3318 edit->mark1 = m1;
3319 edit->mark2 = m2;
3320 edit->column1 = c1;
3321 edit->column2 = c2;
3324 /* --------------------------------------------------------------------------------------------- */
3325 /** highlight marker toggle */
3327 void
3328 edit_mark_cmd (WEdit * edit, gboolean unmark)
3330 edit_push_markers (edit);
3331 if (unmark)
3333 edit_set_markers (edit, 0, 0, 0, 0);
3334 edit->force |= REDRAW_PAGE;
3336 else if (edit->mark2 >= 0)
3338 edit->end_mark_curs = -1;
3339 edit_set_markers (edit, edit->curs1, -1, edit->curs_col + edit->over_col,
3340 edit->curs_col + edit->over_col);
3341 edit->force |= REDRAW_PAGE;
3343 else
3345 edit->end_mark_curs = edit->curs1;
3346 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1,
3347 edit->curs_col + edit->over_col);
3351 /* --------------------------------------------------------------------------------------------- */
3352 /** highlight the word under cursor */
3354 void
3355 edit_mark_current_word_cmd (WEdit * edit)
3357 long pos;
3359 for (pos = edit->curs1; pos != 0; pos--)
3361 int c1, c2;
3363 c1 = edit_get_byte (edit, pos);
3364 c2 = edit_get_byte (edit, pos - 1);
3365 if (!isspace (c1) && isspace (c2))
3366 break;
3367 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3368 break;
3370 edit->mark1 = pos;
3372 for (; pos < edit->last_byte; pos++)
3374 int c1, c2;
3376 c1 = edit_get_byte (edit, pos);
3377 c2 = edit_get_byte (edit, pos + 1);
3378 if (!isspace (c1) && isspace (c2))
3379 break;
3380 if ((my_type_of (c1) & my_type_of (c2)) == 0)
3381 break;
3383 edit->mark2 = min (pos + 1, edit->last_byte);
3385 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3388 /* --------------------------------------------------------------------------------------------- */
3390 void
3391 edit_mark_current_line_cmd (WEdit * edit)
3393 long pos = edit->curs1;
3395 edit->mark1 = edit_bol (edit, pos);
3396 edit->mark2 = edit_eol (edit, pos);
3398 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3401 /* --------------------------------------------------------------------------------------------- */
3403 void
3404 edit_delete_line (WEdit * edit)
3407 * Delete right part of the line.
3408 * Note that edit_get_byte() returns '\n' when byte position is
3409 * beyond EOF.
3411 while (edit_get_byte (edit, edit->curs1) != '\n')
3412 (void) edit_delete (edit, 1);
3415 * Delete '\n' char.
3416 * Note that edit_delete() will not corrupt anything if called while
3417 * cursor position is EOF.
3419 (void) edit_delete (edit, 1);
3422 * Delete left part of the line.
3423 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3425 while (edit_get_byte (edit, edit->curs1 - 1) != '\n')
3426 (void) edit_backspace (edit, 1);
3429 /* --------------------------------------------------------------------------------------------- */
3431 long
3432 edit_indent_width (WEdit * edit, off_t p)
3434 off_t q = p;
3436 /* move to the end of the leading whitespace of the line */
3437 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1)
3438 q++;
3439 /* count the number of columns of indentation */
3440 return (long) edit_move_forward3 (edit, p, 0, q);
3443 /* --------------------------------------------------------------------------------------------- */
3445 void
3446 edit_insert_indent (WEdit * edit, int indent)
3448 if (!option_fill_tabs_with_spaces)
3450 while (indent >= TAB_SIZE)
3452 edit_insert (edit, '\t');
3453 indent -= TAB_SIZE;
3456 while (indent-- > 0)
3457 edit_insert (edit, ' ');
3460 /* --------------------------------------------------------------------------------------------- */
3462 void
3463 edit_push_key_press (WEdit * edit)
3465 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3466 if (edit->mark2 == -1)
3468 edit_push_undo_action (edit, MARK_1 + edit->mark1);
3469 edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3473 /* --------------------------------------------------------------------------------------------- */
3475 void
3476 edit_find_bracket (WEdit * edit)
3478 edit->bracket = edit_get_bracket (edit, 1, 10000);
3479 if (last_bracket != edit->bracket)
3480 edit->force |= REDRAW_PAGE;
3481 last_bracket = edit->bracket;
3484 /* --------------------------------------------------------------------------------------------- */
3486 * This executes a command as though the user initiated it through a key
3487 * press. Callback with WIDGET_KEY as a message calls this after
3488 * translating the key press. This function can be used to pass any
3489 * command to the editor. Note that the screen wouldn't update
3490 * automatically. Either of command or char_for_insertion must be
3491 * passed as -1. Commands are executed, and char_for_insertion is
3492 * inserted at the cursor.
3495 void
3496 edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_insertion)
3498 if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3499 || (macro_index < 0
3500 && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3502 macro_index = 0;
3503 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3504 return;
3506 if (macro_index != -1)
3508 edit->force |= REDRAW_COMPLETELY;
3509 if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3511 edit_store_macro_cmd (edit);
3512 macro_index = -1;
3513 return;
3515 if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3517 edit_repeat_macro_cmd (edit);
3518 macro_index = -1;
3519 return;
3523 if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3525 record_macro_buf[macro_index].action = command;
3526 record_macro_buf[macro_index++].ch = char_for_insertion;
3528 /* record the beginning of a set of editing actions initiated by a key press */
3529 if (command != CK_Undo && command != CK_ExtendedKeyMap)
3530 edit_push_key_press (edit);
3532 edit_execute_cmd (edit, command, char_for_insertion);
3533 if (edit->column_highlight)
3534 edit->force |= REDRAW_PAGE;
3537 /* --------------------------------------------------------------------------------------------- */
3539 This executes a command at a lower level than macro recording.
3540 It also does not push a key_press onto the undo stack. This means
3541 that if it is called many times, a single undo command will undo
3542 all of them. It also does not check for the Undo command.
3544 void
3545 edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
3547 edit->force |= REDRAW_LINE;
3549 /* The next key press will unhighlight the found string, so update
3550 * the whole page */
3551 if (edit->found_len || edit->column_highlight)
3552 edit->force |= REDRAW_PAGE;
3554 switch (command)
3556 /* a mark command with shift-arrow */
3557 case CK_MarkLeft:
3558 case CK_MarkRight:
3559 case CK_MarkToWordBegin:
3560 case CK_MarkToWordEnd:
3561 case CK_MarkToHome:
3562 case CK_MarkToEnd:
3563 case CK_MarkUp:
3564 case CK_MarkDown:
3565 case CK_MarkPageUp:
3566 case CK_MarkPageDown:
3567 case CK_MarkToFileBegin:
3568 case CK_MarkToFileEnd:
3569 case CK_MarkToPageBegin:
3570 case CK_MarkToPageEnd:
3571 case CK_MarkScrollUp:
3572 case CK_MarkScrollDown:
3573 case CK_MarkParagraphUp:
3574 case CK_MarkParagraphDown:
3575 /* a mark command with alt-arrow */
3576 case CK_MarkColumnPageUp:
3577 case CK_MarkColumnPageDown:
3578 case CK_MarkColumnLeft:
3579 case CK_MarkColumnRight:
3580 case CK_MarkColumnUp:
3581 case CK_MarkColumnDown:
3582 case CK_MarkColumnScrollUp:
3583 case CK_MarkColumnScrollDown:
3584 case CK_MarkColumnParagraphUp:
3585 case CK_MarkColumnParagraphDown:
3586 edit->column_highlight = 0;
3587 if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3589 edit_mark_cmd (edit, TRUE); /* clear */
3590 edit_mark_cmd (edit, FALSE); /* marking on */
3592 edit->highlight = 1;
3593 break;
3595 /* any other command */
3596 default:
3597 if (edit->highlight)
3598 edit_mark_cmd (edit, FALSE); /* clear */
3599 edit->highlight = 0;
3602 /* first check for undo */
3603 if (command == CK_Undo)
3605 edit->redo_stack_reset = 0;
3606 edit_group_undo (edit);
3607 edit->found_len = 0;
3608 edit->prev_col = edit_get_col (edit);
3609 edit->search_start = edit->curs1;
3610 return;
3612 /* check for redo */
3613 if (command == CK_Redo)
3615 edit->redo_stack_reset = 0;
3616 edit_do_redo (edit);
3617 edit->found_len = 0;
3618 edit->prev_col = edit_get_col (edit);
3619 edit->search_start = edit->curs1;
3620 return;
3623 edit->redo_stack_reset = 1;
3625 /* An ordinary key press */
3626 if (char_for_insertion >= 0)
3628 /* if non persistent selection and text selected */
3629 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3630 edit_block_delete_cmd (edit);
3632 if (edit->overwrite)
3634 /* remove char only one time, after input first byte, multibyte chars */
3635 #ifdef HAVE_CHARSET
3636 if (!mc_global.utf8_display || edit->charpoint == 0)
3637 #endif
3638 if (edit_get_byte (edit, edit->curs1) != '\n')
3639 edit_delete (edit, 0);
3641 if (option_cursor_beyond_eol && edit->over_col > 0)
3642 edit_insert_over (edit);
3643 #ifdef HAVE_CHARSET
3644 if (char_for_insertion > 255 && !mc_global.utf8_display)
3646 unsigned char str[6 + 1];
3647 size_t i = 0;
3648 int res;
3650 res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3651 if (res == 0)
3653 str[0] = '.';
3654 str[1] = '\0';
3656 else
3658 str[res] = '\0';
3660 while (str[i] != 0 && i <= 6)
3662 char_for_insertion = str[i];
3663 edit_insert (edit, char_for_insertion);
3664 i++;
3667 else
3668 #endif
3669 edit_insert (edit, char_for_insertion);
3671 if (option_auto_para_formatting)
3673 format_paragraph (edit, 0);
3674 edit->force |= REDRAW_PAGE;
3676 else
3677 check_and_wrap_line (edit);
3678 edit->found_len = 0;
3679 edit->prev_col = edit_get_col (edit);
3680 edit->search_start = edit->curs1;
3681 edit_find_bracket (edit);
3682 return;
3685 switch (command)
3687 case CK_TopOnScreen:
3688 case CK_BottomOnScreen:
3689 case CK_Top:
3690 case CK_Bottom:
3691 case CK_PageUp:
3692 case CK_PageDown:
3693 case CK_Home:
3694 case CK_End:
3695 case CK_Up:
3696 case CK_Down:
3697 case CK_Left:
3698 case CK_Right:
3699 case CK_WordLeft:
3700 case CK_WordRight:
3701 if (!option_persistent_selections && edit->mark2 >= 0)
3703 if (edit->column_highlight)
3704 edit_push_undo_action (edit, COLUMN_ON);
3705 edit->column_highlight = 0;
3706 edit_mark_cmd (edit, TRUE);
3710 switch (command)
3712 case CK_TopOnScreen:
3713 case CK_BottomOnScreen:
3714 case CK_MarkToPageBegin:
3715 case CK_MarkToPageEnd:
3716 case CK_Up:
3717 case CK_Down:
3718 case CK_WordLeft:
3719 case CK_WordRight:
3720 case CK_MarkToWordBegin:
3721 case CK_MarkToWordEnd:
3722 case CK_MarkUp:
3723 case CK_MarkDown:
3724 case CK_MarkColumnUp:
3725 case CK_MarkColumnDown:
3726 if (edit->mark2 == -1)
3727 break; /*marking is following the cursor: may need to highlight a whole line */
3728 case CK_Left:
3729 case CK_Right:
3730 case CK_MarkLeft:
3731 case CK_MarkRight:
3732 edit->force |= REDRAW_CHAR_ONLY;
3735 /* basic cursor key commands */
3736 switch (command)
3738 case CK_BackSpace:
3739 /* if non persistent selection and text selected */
3740 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3741 edit_block_delete_cmd (edit);
3742 else if (option_cursor_beyond_eol && edit->over_col > 0)
3743 edit->over_col--;
3744 else if (option_backspace_through_tabs && is_in_indent (edit))
3745 while (edit_get_byte (edit, edit->curs1 - 1) != '\n' && edit->curs1 > 0)
3746 edit_backspace (edit, 1);
3747 else if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3749 int i;
3751 for (i = 0; i < HALF_TAB_SIZE; i++)
3752 edit_backspace (edit, 1);
3754 else
3755 edit_backspace (edit, 0);
3756 break;
3757 case CK_Delete:
3758 /* if non persistent selection and text selected */
3759 if (!option_persistent_selections && edit->mark1 != edit->mark2)
3760 edit_block_delete_cmd (edit);
3761 else
3763 if (option_cursor_beyond_eol && edit->over_col > 0)
3764 edit_insert_over (edit);
3766 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3768 int i;
3770 for (i = 1; i <= HALF_TAB_SIZE; i++)
3771 edit_delete (edit, 1);
3773 else
3774 edit_delete (edit, 0);
3776 break;
3777 case CK_DeleteToWordBegin:
3778 edit->over_col = 0;
3779 edit_left_delete_word (edit);
3780 break;
3781 case CK_DeleteToWordEnd:
3782 if (option_cursor_beyond_eol && edit->over_col > 0)
3783 edit_insert_over (edit);
3785 edit_right_delete_word (edit);
3786 break;
3787 case CK_DeleteLine:
3788 edit_delete_line (edit);
3789 break;
3790 case CK_DeleteToHome:
3791 edit_delete_to_line_begin (edit);
3792 break;
3793 case CK_DeleteToEnd:
3794 edit_delete_to_line_end (edit);
3795 break;
3796 case CK_Enter:
3797 edit->over_col = 0;
3798 if (option_auto_para_formatting)
3800 edit_double_newline (edit);
3801 if (option_return_does_auto_indent)
3802 edit_auto_indent (edit);
3803 format_paragraph (edit, 0);
3805 else
3807 edit_insert (edit, '\n');
3808 if (option_return_does_auto_indent)
3809 edit_auto_indent (edit);
3811 break;
3812 case CK_Return:
3813 edit_insert (edit, '\n');
3814 break;
3816 case CK_MarkColumnPageUp:
3817 edit->column_highlight = 1;
3818 case CK_PageUp:
3819 case CK_MarkPageUp:
3820 edit_move_up (edit, edit->widget.lines - 1, 1);
3821 break;
3822 case CK_MarkColumnPageDown:
3823 edit->column_highlight = 1;
3824 case CK_PageDown:
3825 case CK_MarkPageDown:
3826 edit_move_down (edit, edit->widget.lines - 1, 1);
3827 break;
3828 case CK_MarkColumnLeft:
3829 edit->column_highlight = 1;
3830 case CK_Left:
3831 case CK_MarkLeft:
3832 if (option_fake_half_tabs && is_in_indent (edit) && right_of_four_spaces (edit))
3834 if (option_cursor_beyond_eol && edit->over_col > 0)
3835 edit->over_col--;
3836 else
3837 edit_cursor_move (edit, -HALF_TAB_SIZE);
3838 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3840 else
3841 edit_left_char_move_cmd (edit);
3842 break;
3843 case CK_MarkColumnRight:
3844 edit->column_highlight = 1;
3845 case CK_Right:
3846 case CK_MarkRight:
3847 if (option_fake_half_tabs && is_in_indent (edit) && left_of_four_spaces (edit))
3849 edit_cursor_move (edit, HALF_TAB_SIZE);
3850 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3852 else
3853 edit_right_char_move_cmd (edit);
3854 break;
3855 case CK_TopOnScreen:
3856 case CK_MarkToPageBegin:
3857 edit_begin_page (edit);
3858 break;
3859 case CK_BottomOnScreen:
3860 case CK_MarkToPageEnd:
3861 edit_end_page (edit);
3862 break;
3863 case CK_WordLeft:
3864 case CK_MarkToWordBegin:
3865 edit->over_col = 0;
3866 edit_left_word_move_cmd (edit);
3867 break;
3868 case CK_WordRight:
3869 case CK_MarkToWordEnd:
3870 edit->over_col = 0;
3871 edit_right_word_move_cmd (edit);
3872 break;
3873 case CK_MarkColumnUp:
3874 edit->column_highlight = 1;
3875 case CK_Up:
3876 case CK_MarkUp:
3877 edit_move_up (edit, 1, 0);
3878 break;
3879 case CK_MarkColumnDown:
3880 edit->column_highlight = 1;
3881 case CK_Down:
3882 case CK_MarkDown:
3883 edit_move_down (edit, 1, 0);
3884 break;
3885 case CK_MarkColumnParagraphUp:
3886 edit->column_highlight = 1;
3887 case CK_ParagraphUp:
3888 case CK_MarkParagraphUp:
3889 edit_move_up_paragraph (edit, 0);
3890 break;
3891 case CK_MarkColumnParagraphDown:
3892 edit->column_highlight = 1;
3893 case CK_ParagraphDown:
3894 case CK_MarkParagraphDown:
3895 edit_move_down_paragraph (edit, 0);
3896 break;
3897 case CK_MarkColumnScrollUp:
3898 edit->column_highlight = 1;
3899 case CK_ScrollUp:
3900 case CK_MarkScrollUp:
3901 edit_move_up (edit, 1, 1);
3902 break;
3903 case CK_MarkColumnScrollDown:
3904 edit->column_highlight = 1;
3905 case CK_ScrollDown:
3906 case CK_MarkScrollDown:
3907 edit_move_down (edit, 1, 1);
3908 break;
3909 case CK_Home:
3910 case CK_MarkToHome:
3911 edit_cursor_to_bol (edit);
3912 break;
3913 case CK_End:
3914 case CK_MarkToEnd:
3915 edit_cursor_to_eol (edit);
3916 break;
3917 case CK_Tab:
3918 /* if text marked shift block */
3919 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3921 if (edit->mark2 < 0)
3922 edit_mark_cmd (edit, FALSE);
3923 edit_move_block_to_right (edit);
3925 else
3927 if (option_cursor_beyond_eol)
3928 edit_insert_over (edit);
3929 edit_tab_cmd (edit);
3930 if (option_auto_para_formatting)
3932 format_paragraph (edit, 0);
3933 edit->force |= REDRAW_PAGE;
3935 else
3936 check_and_wrap_line (edit);
3938 break;
3940 case CK_InsertOverwrite:
3941 edit->overwrite = !edit->overwrite;
3942 break;
3944 case CK_Mark:
3945 if (edit->mark2 >= 0)
3947 if (edit->column_highlight)
3948 edit_push_undo_action (edit, COLUMN_ON);
3949 edit->column_highlight = 0;
3951 edit_mark_cmd (edit, FALSE);
3952 break;
3953 case CK_MarkColumn:
3954 if (!edit->column_highlight)
3955 edit_push_undo_action (edit, COLUMN_OFF);
3956 edit->column_highlight = 1;
3957 edit_mark_cmd (edit, FALSE);
3958 break;
3959 case CK_MarkAll:
3960 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
3961 edit->force |= REDRAW_PAGE;
3962 break;
3963 case CK_Unmark:
3964 if (edit->column_highlight)
3965 edit_push_undo_action (edit, COLUMN_ON);
3966 edit->column_highlight = 0;
3967 edit_mark_cmd (edit, TRUE);
3968 break;
3969 case CK_MarkWord:
3970 if (edit->column_highlight)
3971 edit_push_undo_action (edit, COLUMN_ON);
3972 edit->column_highlight = 0;
3973 edit_mark_current_word_cmd (edit);
3974 break;
3975 case CK_MarkLine:
3976 if (edit->column_highlight)
3977 edit_push_undo_action (edit, COLUMN_ON);
3978 edit->column_highlight = 0;
3979 edit_mark_current_line_cmd (edit);
3980 break;
3982 case CK_ShowNumbers:
3983 option_line_state = !option_line_state;
3984 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
3985 edit->force |= REDRAW_PAGE;
3986 break;
3988 case CK_ShowMargin:
3989 show_right_margin = !show_right_margin;
3990 edit->force |= REDRAW_PAGE;
3991 break;
3993 case CK_Bookmark:
3994 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
3995 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
3996 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
3997 else
3998 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
3999 break;
4000 case CK_BookmarkFlush:
4001 book_mark_flush (edit, BOOK_MARK_COLOR);
4002 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
4003 edit->force |= REDRAW_PAGE;
4004 break;
4005 case CK_BookmarkNext:
4006 if (edit->book_mark != NULL)
4008 struct _book_mark *p;
4010 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4011 if (p->next != NULL)
4013 p = p->next;
4014 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4015 edit_move_display (edit, p->line - edit->widget.lines / 2);
4016 edit_move_to_line (edit, p->line);
4019 break;
4020 case CK_BookmarkPrev:
4021 if (edit->book_mark != NULL)
4023 struct _book_mark *p;
4025 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
4026 while (p->line == edit->curs_line)
4027 if (p->prev != NULL)
4028 p = p->prev;
4029 if (p->line >= 0)
4031 if (p->line >= edit->start_line + edit->widget.lines || p->line < edit->start_line)
4032 edit_move_display (edit, p->line - edit->widget.lines / 2);
4033 edit_move_to_line (edit, p->line);
4036 break;
4038 case CK_Top:
4039 case CK_MarkToFileBegin:
4040 edit_move_to_top (edit);
4041 break;
4042 case CK_Bottom:
4043 case CK_MarkToFileEnd:
4044 edit_move_to_bottom (edit);
4045 break;
4047 case CK_Copy:
4048 if (option_cursor_beyond_eol && edit->over_col > 0)
4049 edit_insert_over (edit);
4050 edit_block_copy_cmd (edit);
4051 break;
4052 case CK_Remove:
4053 edit_block_delete_cmd (edit);
4054 break;
4055 case CK_Move:
4056 edit_block_move_cmd (edit);
4057 break;
4059 case CK_BlockShiftLeft:
4060 if (edit->mark1 != edit->mark2)
4061 edit_move_block_to_left (edit);
4062 break;
4063 case CK_BlockShiftRight:
4064 if (edit->mark1 != edit->mark2)
4065 edit_move_block_to_right (edit);
4066 break;
4067 case CK_Store:
4068 edit_copy_to_X_buf_cmd (edit);
4069 break;
4070 case CK_Cut:
4071 edit_cut_to_X_buf_cmd (edit);
4072 break;
4073 case CK_Paste:
4074 /* if non persistent selection and text selected */
4075 if (!option_persistent_selections && edit->mark1 != edit->mark2)
4076 edit_block_delete_cmd (edit);
4077 if (option_cursor_beyond_eol && edit->over_col > 0)
4078 edit_insert_over (edit);
4079 edit_paste_from_X_buf_cmd (edit);
4080 break;
4081 case CK_History:
4082 edit_paste_from_history (edit);
4083 break;
4085 case CK_SaveAs:
4086 edit_save_as_cmd (edit);
4087 break;
4088 case CK_Save:
4089 edit_save_confirm_cmd (edit);
4090 break;
4091 case CK_EditFile:
4092 edit_load_cmd (edit, EDIT_FILE_COMMON);
4093 break;
4094 case CK_BlockSave:
4095 edit_save_block_cmd (edit);
4096 break;
4097 case CK_InsertFile:
4098 edit_insert_file_cmd (edit);
4099 break;
4101 case CK_FilePrev:
4102 edit_load_back_cmd (edit);
4103 break;
4104 case CK_FileNext:
4105 edit_load_forward_cmd (edit);
4106 break;
4108 case CK_EditSyntaxFile:
4109 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
4110 break;
4111 case CK_SyntaxChoose:
4112 edit_syntax_dialog (edit);
4113 break;
4115 case CK_EditUserMenu:
4116 edit_load_cmd (edit, EDIT_FILE_MENU);
4117 break;
4119 case CK_SyntaxOnOff:
4120 option_syntax_highlighting ^= 1;
4121 if (option_syntax_highlighting == 1)
4122 edit_load_syntax (edit, NULL, edit->syntax_type);
4123 edit->force |= REDRAW_PAGE;
4124 break;
4126 case CK_ShowTabTws:
4127 enable_show_tabs_tws ^= 1;
4128 edit->force |= REDRAW_PAGE;
4129 break;
4131 case CK_Search:
4132 edit_search_cmd (edit, FALSE);
4133 break;
4134 case CK_SearchContinue:
4135 edit_search_cmd (edit, TRUE);
4136 break;
4137 case CK_Replace:
4138 edit_replace_cmd (edit, 0);
4139 break;
4140 case CK_ReplaceContinue:
4141 edit_replace_cmd (edit, 1);
4142 break;
4143 case CK_Complete:
4144 /* if text marked shift block */
4145 if (edit->mark1 != edit->mark2 && !option_persistent_selections)
4146 edit_move_block_to_left (edit);
4147 else
4148 edit_complete_word_cmd (edit);
4149 break;
4150 case CK_Find:
4151 edit_get_match_keyword_cmd (edit);
4152 break;
4153 case CK_Quit:
4154 dlg_stop (edit->widget.owner);
4155 break;
4156 case CK_EditNew:
4157 edit_new_cmd (edit);
4158 break;
4159 case CK_Help:
4160 edit_help_cmd (edit);
4161 break;
4162 case CK_Refresh:
4163 edit_refresh_cmd (edit);
4164 break;
4165 case CK_SaveSetup:
4166 save_setup_cmd ();
4167 break;
4168 case CK_About:
4169 edit_about ();
4170 break;
4171 case CK_LearnKeys:
4172 learn_keys ();
4173 break;
4174 case CK_Options:
4175 edit_options_dialog (edit);
4176 break;
4177 case CK_OptionsSaveMode:
4178 menu_save_mode_cmd ();
4179 break;
4180 case CK_Date:
4182 char s[BUF_MEDIUM];
4183 /* fool gcc to prevent a Y2K warning */
4184 char time_format[] = "_c";
4185 time_format[0] = '%';
4187 FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
4188 edit_print_string (edit, s);
4189 edit->force |= REDRAW_PAGE;
4191 break;
4192 case CK_Goto:
4193 edit_goto_cmd (edit);
4194 break;
4195 case CK_ParagraphFormat:
4196 format_paragraph (edit, 1);
4197 edit->force |= REDRAW_PAGE;
4198 break;
4199 case CK_MacroDelete:
4200 edit_delete_macro_cmd (edit);
4201 break;
4202 case CK_MatchBracket:
4203 edit_goto_matching_bracket (edit);
4204 break;
4205 case CK_UserMenu:
4206 user_menu (edit, NULL, -1);
4207 break;
4208 case CK_Sort:
4209 edit_sort_cmd (edit);
4210 break;
4211 case CK_ExternalCommand:
4212 edit_ext_cmd (edit);
4213 break;
4214 case CK_Mail:
4215 edit_mail_dialog (edit);
4216 break;
4217 case CK_Shell:
4218 view_other_cmd ();
4219 break;
4220 #ifdef HAVE_CHARSET
4221 case CK_SelectCodepage:
4222 edit_select_codepage_cmd (edit);
4223 break;
4224 #endif
4225 case CK_InsertLiteral:
4226 edit_insert_literal_cmd (edit);
4227 break;
4228 case CK_MacroStartStopRecord:
4229 edit_begin_end_macro_cmd (edit);
4230 break;
4231 case CK_RepeatStartStopRecord:
4232 edit_begin_end_repeat_cmd (edit);
4233 break;
4234 case CK_ExtendedKeyMap:
4235 edit->extmod = TRUE;
4236 break;
4237 default:
4238 break;
4241 /* CK_PipeBlock */
4242 if ((command / CK_PipeBlock (0)) == 1)
4243 edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4245 /* keys which must set the col position, and the search vars */
4246 switch (command)
4248 case CK_Search:
4249 case CK_SearchContinue:
4250 case CK_Replace:
4251 case CK_ReplaceContinue:
4252 case CK_Complete:
4253 edit->prev_col = edit_get_col (edit);
4254 break;
4255 case CK_Up:
4256 case CK_MarkUp:
4257 case CK_MarkColumnUp:
4258 case CK_Down:
4259 case CK_MarkDown:
4260 case CK_MarkColumnDown:
4261 case CK_PageUp:
4262 case CK_MarkPageUp:
4263 case CK_MarkColumnPageUp:
4264 case CK_PageDown:
4265 case CK_MarkPageDown:
4266 case CK_MarkColumnPageDown:
4267 case CK_Top:
4268 case CK_MarkToFileBegin:
4269 case CK_Bottom:
4270 case CK_MarkToFileEnd:
4271 case CK_ParagraphUp:
4272 case CK_MarkParagraphUp:
4273 case CK_MarkColumnParagraphUp:
4274 case CK_ParagraphDown:
4275 case CK_MarkParagraphDown:
4276 case CK_MarkColumnParagraphDown:
4277 case CK_ScrollUp:
4278 case CK_MarkScrollUp:
4279 case CK_MarkColumnScrollUp:
4280 case CK_ScrollDown:
4281 case CK_MarkScrollDown:
4282 case CK_MarkColumnScrollDown:
4283 edit->search_start = edit->curs1;
4284 edit->found_len = 0;
4285 break;
4286 default:
4287 edit->found_len = 0;
4288 edit->prev_col = edit_get_col (edit);
4289 edit->search_start = edit->curs1;
4291 edit_find_bracket (edit);
4293 if (option_auto_para_formatting)
4295 switch (command)
4297 case CK_BackSpace:
4298 case CK_Delete:
4299 case CK_DeleteToWordBegin:
4300 case CK_DeleteToWordEnd:
4301 case CK_DeleteToHome:
4302 case CK_DeleteToEnd:
4303 format_paragraph (edit, 0);
4304 edit->force |= REDRAW_PAGE;
4309 /* --------------------------------------------------------------------------------------------- */
4311 void
4312 edit_stack_init (void)
4314 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4316 edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4317 edit_history_moveto[edit_stack_iterator].line = -1;
4320 edit_stack_iterator = 0;
4323 /* --------------------------------------------------------------------------------------------- */
4325 void
4326 edit_stack_free (void)
4328 for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4329 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4332 /* --------------------------------------------------------------------------------------------- */
4333 /** move i lines */
4335 void
4336 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
4338 edit_move_updown (edit, i, do_scroll, TRUE);
4341 /* --------------------------------------------------------------------------------------------- */
4342 /** move i lines */
4344 void
4345 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
4347 edit_move_updown (edit, i, do_scroll, FALSE);
4350 /* --------------------------------------------------------------------------------------------- */