* removed edit/usermap.c edit/usermap.h
[midnight-commander.git] / edit / edit.c
blob195e70d35fe48b9d36ece998f516fc18f1f75784
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor low level data handling and cursor fundamentals
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
42 #include "../src/global.h"
44 #include "edit-impl.h"
45 #include "editlock.h"
46 #include "edit-widget.h"
47 #include "../src/cmddef.h"
49 #include "../src/tty/color.h" /* EDITOR_NORMAL_COLOR */
50 #include "../src/tty/tty.h" /* attrset() */
51 #include "../src/tty/key.h" /* is_idle() */
53 #include "../src/widget.h" /* buttonbar_redraw() */
54 #include "../src/cmd.h" /* view_other_cmd() */
55 #include "../src/user.h" /* user_menu_cmd() */
56 #include "../src/wtools.h" /* query_dialog() */
57 #include "../src/timefmt.h" /* time formatting */
58 #include "../src/strutil.h" /* utf string functions */
59 #include "../src/charsets.h" /* get_codepage_id */
60 #include "../src/main.h" /* source_codepage */
63 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
64 or EDIT_KEY_EMULATION_EMACS
66 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
68 int option_word_wrap_line_length = 72;
69 int option_typewriter_wrap = 0;
70 int option_auto_para_formatting = 0;
71 int option_tab_spacing = 8;
72 int option_fill_tabs_with_spaces = 0;
73 int option_return_does_auto_indent = 1;
74 int option_backspace_through_tabs = 0;
75 int option_fake_half_tabs = 1;
76 int option_save_mode = EDIT_QUICK_SAVE;
77 int option_save_position = 1;
78 int option_max_undo = 32768;
79 int option_persistent_selections = 1;
80 int option_cursor_beyond_eol = 1;
81 int option_line_state = 0;
82 int option_line_state_width = 0;
84 int option_edit_right_extreme = 0;
85 int option_edit_left_extreme = 0;
86 int option_edit_top_extreme = 0;
87 int option_edit_bottom_extreme = 0;
88 int enable_show_tabs_tws = 1;
90 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
91 char *option_backup_ext = NULL;
93 int edit_stack_iterator = 0;
94 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
95 /* magic sequense for say than block is vertical */
96 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
97 /*-
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \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
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
112 * ^ | ^
113 * | ^|^ |
114 * cursor ||| cursor
115 * |||
116 * file end|||file beginning
121 * This_is_some_file
122 * fin.
126 static void user_menu (WEdit *edit);
128 int edit_get_byte (WEdit * edit, long byte_index)
130 unsigned long p;
131 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
132 return '\n';
134 if (byte_index >= edit->curs1) {
135 p = edit->curs1 + edit->curs2 - byte_index - 1;
136 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
137 } else {
138 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
142 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
144 unsigned long p;
146 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
147 return NULL;
149 if (byte_index >= edit->curs1) {
150 p = edit->curs1 + edit->curs2 - byte_index - 1;
151 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
152 } else {
153 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
157 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
159 unsigned long p;
161 if (byte_index >= (edit->curs1 + edit->curs2) ) {
162 byte_index -= 1;
165 if ( byte_index < 0 ) {
166 return NULL;
169 if (byte_index >= edit->curs1) {
170 p = edit->curs1 + edit->curs2 - 1;
171 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
172 } else {
173 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
177 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
179 gchar *str = NULL;
180 int res = -1;
181 gunichar ch;
182 gchar *next_ch = NULL;
183 int width = 0;
185 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
186 *char_width = 0;
187 return '\n';
191 str = edit_get_byte_ptr (edit, byte_index);
192 res = g_utf8_get_char_validated (str, -1);
194 if ( res < 0 ) {
195 ch = *str;
196 width = 0;
197 } else {
198 ch = res;
199 /* Calculate UTF-8 char width */
200 next_ch = g_utf8_next_char(str);
201 if ( next_ch ) {
202 width = next_ch - str;
203 } else {
204 ch = 0;
205 width = 0;
208 *char_width = width;
209 return ch;
212 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
214 gchar *str, *buf = NULL;
215 int res = -1;
216 gunichar ch;
217 gchar *next_ch = NULL;
218 int width = 0;
220 if ( byte_index > 0 ) {
221 byte_index--;
224 ch = edit_get_utf (edit, byte_index, &width);
225 if ( width == 1 ) {
226 *char_width = width;
227 return ch;
230 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
231 *char_width = 0;
232 return 0;
235 str = edit_get_byte_ptr (edit, byte_index);
236 buf = edit_get_buf_ptr (edit, byte_index);
237 /* get prev utf8 char */
238 if ( str != buf )
239 str = g_utf8_find_prev_char (buf, str);
241 res = g_utf8_get_char_validated (str, -1);
242 if ( res < 0 ) {
243 ch = *str;
244 width = 0;
245 } else {
246 ch = res;
247 /* Calculate UTF-8 char width */
248 next_ch = g_utf8_next_char(str);
249 if ( next_ch ) {
250 width = next_ch - str;
251 } else {
252 ch = 0;
253 width = 0;
256 *char_width = width;
257 return ch;
261 * Initialize the buffers for an empty files.
263 static void
264 edit_init_buffers (WEdit *edit)
266 int j;
268 for (j = 0; j <= MAXBUFF; j++) {
269 edit->buffers1[j] = NULL;
270 edit->buffers2[j] = NULL;
273 edit->curs1 = 0;
274 edit->curs2 = 0;
275 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
279 * Load file OR text into buffers. Set cursor to the beginning of file.
280 * Return 1 on error.
282 static int
283 edit_load_file_fast (WEdit *edit, const char *filename)
285 long buf, buf2;
286 int file = -1;
287 edit->curs2 = edit->last_byte;
288 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
289 edit->utf8 = 0;
290 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
291 GString *errmsg = g_string_new(NULL);
292 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
293 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
294 g_string_free (errmsg, TRUE);
295 return 1;
298 if (!edit->buffers2[buf2])
299 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
301 mc_read (file,
302 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
303 (edit->curs2 & M_EDIT_BUF_SIZE),
304 edit->curs2 & M_EDIT_BUF_SIZE);
306 for (buf = buf2 - 1; buf >= 0; buf--) {
307 /* edit->buffers2[0] is already allocated */
308 if (!edit->buffers2[buf])
309 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
310 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
313 mc_close (file);
314 return 0;
317 /* detecting an error on save is easy: just check if every byte has been written. */
318 /* detecting an error on read, is not so easy 'cos there is not way to tell
319 whether you read everything or not. */
320 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
321 static const struct edit_filters {
322 const char *read, *write, *extension;
323 } all_filters[] = {
324 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
325 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
326 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
327 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
328 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
331 /* Return index of the filter or -1 is there is no appropriate filter */
332 static int edit_find_filter (const char *filename)
334 size_t i, l, e;
335 if (!filename)
336 return -1;
337 l = strlen (filename);
338 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
339 e = strlen (all_filters[i].extension);
340 if (l > e)
341 if (!strcmp (all_filters[i].extension, filename + l - e))
342 return i;
344 return -1;
347 static char *
348 edit_get_filter (const char *filename)
350 int i, l;
351 char *p, *quoted_name;
352 i = edit_find_filter (filename);
353 if (i < 0)
354 return 0;
355 quoted_name = name_quote (filename, 0);
356 l = str_term_width1 (quoted_name);
357 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
358 sprintf (p, all_filters[i].read, quoted_name);
359 g_free (quoted_name);
360 return p;
363 char *
364 edit_get_write_filter (const char *write_name, const char *filename)
366 int i, l;
367 char *p, *writename;
368 i = edit_find_filter (filename);
369 if (i < 0)
370 return 0;
371 writename = name_quote (write_name, 0);
372 l = str_term_width1 (writename);
373 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
374 sprintf (p, all_filters[i].write, writename);
375 g_free (writename);
376 return p;
379 static long
380 edit_insert_stream (WEdit * edit, FILE * f)
382 int c;
383 long i = 0;
384 while ((c = fgetc (f)) >= 0) {
385 edit_insert (edit, c);
386 i++;
388 return i;
391 long edit_write_stream (WEdit * edit, FILE * f)
393 long i;
395 if (edit->lb == LB_ASIS) {
396 for (i = 0; i < edit->last_byte; i++)
397 if (fputc (edit_get_byte (edit, i), f) < 0)
398 break;
399 return i;
402 /* change line breaks */
403 for (i = 0; i < edit->last_byte; i++) {
404 unsigned char c = edit_get_byte (edit, i);
406 if (!(c == '\n' || c == '\r')) {
407 /* not line break */
408 if (fputc (c, f) < 0)
409 return i;
410 } else { /* (c == '\n' || c == '\r') */
411 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
413 switch (edit->lb) {
414 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
415 /* put one line break unconditionally */
416 if (fputc ('\n', f) < 0)
417 return i;
419 i++; /* 2 chars are processed */
421 if (c == '\r' && c1 == '\n')
422 /* Windows line break; go to the next char */
423 break;
425 if (c == '\r' && c1 == '\r') {
426 /* two Macintosh line breaks; put second line break */
427 if (fputc ('\n', f) < 0)
428 return i;
429 break;
432 if (fputc (c1, f) < 0)
433 return i;
434 break;
436 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
437 /* put one line break unconditionally */
438 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
439 return i;
441 if (c == '\r' && c1 == '\n')
442 /* Windows line break; go to the next char */
443 i++;
444 break;
446 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
447 /* put one line break unconditionally */
448 if (fputc ('\r', f) < 0)
449 return i;
451 i++; /* 2 chars are processed */
453 if (c == '\r' && c1 == '\n')
454 /* Windows line break; go to the next char */
455 break;
457 if (c == '\n' && c1 == '\n') {
458 /* two Windows line breaks; put second line break */
459 if (fputc ('\r', f) < 0)
460 return i;
461 break;
464 if (fputc (c1, f) < 0)
465 return i;
466 break;
467 case LB_ASIS: /* default without changes */
468 break;
473 return edit->last_byte;
476 #define TEMP_BUF_LEN 1024
478 /* inserts a file at the cursor, returns 1 on success */
480 edit_insert_file (WEdit *edit, const char *filename)
482 char *p;
483 if ((p = edit_get_filter (filename))) {
484 FILE *f;
485 long current = edit->curs1;
486 f = (FILE *) popen (p, "r");
487 if (f) {
488 edit_insert_stream (edit, f);
489 edit_cursor_move (edit, current - edit->curs1);
490 if (pclose (f) > 0) {
491 GString *errmsg = g_string_new (NULL);
492 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
493 edit_error_dialog (_("Error"), errmsg->str);
494 g_string_free (errmsg, TRUE);
495 g_free (p);
496 return 0;
498 } else {
499 GString *errmsg = g_string_new (NULL);
500 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
501 edit_error_dialog (_("Error"), errmsg->str);
502 g_string_free (errmsg, TRUE);
503 g_free (p);
504 return 0;
506 g_free (p);
507 } else {
508 int i, file, blocklen;
509 long current = edit->curs1;
510 int vertical_insertion = 0;
511 char *buf;
512 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
513 return 0;
514 buf = g_malloc (TEMP_BUF_LEN);
515 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
516 if (blocklen > 0) {
517 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
518 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
519 vertical_insertion = 1;
520 } else {
521 mc_lseek (file, 0, SEEK_SET);
524 if (vertical_insertion) {
525 blocklen = edit_insert_column_of_text_from_file (edit, file);
526 } else {
527 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
528 for (i = 0; i < blocklen; i++)
529 edit_insert (edit, buf[i]);
532 edit_cursor_move (edit, current - edit->curs1);
533 g_free (buf);
534 mc_close (file);
535 if (blocklen)
536 return 0;
538 return 1;
541 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
542 static int
543 check_file_access (WEdit *edit, const char *filename, struct stat *st)
545 int file;
546 GString *errmsg = (GString *) 0;
548 /* Try opening an existing file */
549 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
551 if (file < 0) {
553 * Try creating the file. O_EXCL prevents following broken links
554 * and opening existing files.
556 file =
557 mc_open (filename,
558 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
559 0666);
560 if (file < 0) {
561 g_string_sprintf (errmsg = g_string_new (NULL),
562 _(" Cannot open %s for reading "), filename);
563 goto cleanup;
564 } else {
565 /* New file, delete it if it's not modified or saved */
566 edit->delete_file = 1;
570 /* Check what we have opened */
571 if (mc_fstat (file, st) < 0) {
572 g_string_sprintf (errmsg = g_string_new (NULL),
573 _(" Cannot get size/permissions for %s "), filename);
574 goto cleanup;
577 /* We want to open regular files only */
578 if (!S_ISREG (st->st_mode)) {
579 g_string_sprintf (errmsg = g_string_new (NULL),
580 _(" %s is not a regular file "), filename);
581 goto cleanup;
585 * Don't delete non-empty files.
586 * O_EXCL should prevent it, but let's be on the safe side.
588 if (st->st_size > 0) {
589 edit->delete_file = 0;
592 if (st->st_size >= SIZE_LIMIT) {
593 g_string_sprintf (errmsg = g_string_new (NULL),
594 _(" File %s is too large "), filename);
595 goto cleanup;
598 cleanup:
599 (void) mc_close (file);
600 if (errmsg) {
601 edit_error_dialog (_("Error"), errmsg->str);
602 g_string_free (errmsg, TRUE);
603 return 1;
605 return 0;
609 * Open the file and load it into the buffers, either directly or using
610 * a filter. Return 0 on success, 1 on error.
612 * Fast loading (edit_load_file_fast) is used when the file size is
613 * known. In this case the data is read into the buffers by blocks.
614 * If the file size is not known, the data is loaded byte by byte in
615 * edit_insert_file.
617 static int
618 edit_load_file (WEdit *edit)
620 int fast_load = 1;
622 /* Cannot do fast load if a filter is used */
623 if (edit_find_filter (edit->filename) >= 0)
624 fast_load = 0;
627 * VFS may report file size incorrectly, and slow load is not a big
628 * deal considering overhead in VFS.
630 if (!vfs_file_is_local (edit->filename))
631 fast_load = 0;
634 * FIXME: line end translation should disable fast loading as well
635 * Consider doing fseek() to the end and ftell() for the real size.
638 if (*edit->filename) {
639 /* If we are dealing with a real file, check that it exists */
640 if (check_file_access (edit, edit->filename, &edit->stat1))
641 return 1;
642 } else {
643 /* nothing to load */
644 fast_load = 0;
647 edit_init_buffers (edit);
649 if (fast_load) {
650 edit->last_byte = edit->stat1.st_size;
651 edit_load_file_fast (edit, edit->filename);
652 /* If fast load was used, the number of lines wasn't calculated */
653 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
654 } else {
655 #ifdef HAVE_CHARSET
656 const char *codepage_id;
657 #endif
658 edit->last_byte = 0;
659 if (*edit->filename) {
660 edit->stack_disable = 1;
661 if (!edit_insert_file (edit, edit->filename)) {
662 edit_clean (edit);
663 return 1;
665 edit->stack_disable = 0;
668 #ifdef HAVE_CHARSET
669 codepage_id = get_codepage_id( source_codepage );
670 if ( codepage_id )
671 edit->utf8 = str_isutf8 ( codepage_id );
672 #endif
674 edit->lb = LB_ASIS;
675 return 0;
678 /* Restore saved cursor position in the file */
679 static void
680 edit_load_position (WEdit *edit)
682 char *filename;
683 long line, column;
685 if (!edit->filename || !*edit->filename)
686 return;
688 filename = vfs_canon (edit->filename);
689 load_file_position (filename, &line, &column);
690 g_free (filename);
692 edit_move_to_line (edit, line - 1);
693 edit->prev_col = column;
694 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
695 edit_move_display (edit, line - (edit->num_widget_lines / 2));
698 /* Save cursor position in the file */
699 static void
700 edit_save_position (WEdit *edit)
702 char *filename;
704 if (!edit->filename || !*edit->filename)
705 return;
707 filename = vfs_canon (edit->filename);
708 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
709 g_free (filename);
712 /* Clean the WEdit stricture except the widget part */
713 static void
714 edit_purge_widget (WEdit *edit)
716 int len = sizeof (WEdit) - sizeof (Widget);
717 char *start = (char *) edit + sizeof (Widget);
718 memset (start, 0, len);
719 edit->macro_i = -1; /* not recording a macro */
722 static void
723 edit_set_keymap (WEdit *edit)
725 edit->user_map = (global_key_map_t *) editor_keymap->data;
729 #define space_width 1
732 * Fill in the edit structure. Return NULL on failure. Pass edit as
733 * NULL to allocate a new structure.
735 * If line is 0, try to restore saved position. Otherwise put the
736 * cursor on that line and show it in the middle of the screen.
738 WEdit *
739 edit_init (WEdit *edit, int lines, int columns, const char *filename,
740 long line)
742 int to_free = 0;
743 option_auto_syntax = 1; /* Resetting to auto on every invokation */
744 if ( option_line_state ) {
745 option_line_state_width = LINE_STATE_WIDTH;
746 } else {
747 option_line_state_width = 0;
749 if (!edit) {
750 #ifdef ENABLE_NLS
752 * Expand option_whole_chars_search by national letters using
753 * current locale
756 static char option_whole_chars_search_buf[256];
758 if (option_whole_chars_search_buf != option_whole_chars_search) {
759 size_t i;
760 size_t len = str_term_width1 (option_whole_chars_search);
762 strcpy (option_whole_chars_search_buf,
763 option_whole_chars_search);
765 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
766 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
767 option_whole_chars_search_buf[len++] = i;
771 option_whole_chars_search_buf[len] = 0;
772 option_whole_chars_search = option_whole_chars_search_buf;
774 #endif /* ENABLE_NLS */
775 edit = g_malloc0 (sizeof (WEdit));
776 edit->search = NULL;
777 to_free = 1;
779 edit_purge_widget (edit);
780 edit->num_widget_lines = lines;
781 edit->over_col = 0;
782 edit->num_widget_columns = columns;
783 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
784 edit->stat1.st_uid = getuid ();
785 edit->stat1.st_gid = getgid ();
786 edit->stat1.st_mtime = 0;
787 edit->bracket = -1;
788 edit->force |= REDRAW_PAGE;
789 edit_set_filename (edit, filename);
790 edit->stack_size = START_STACK_SIZE;
791 edit->stack_size_mask = START_STACK_SIZE - 1;
792 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
793 if (edit_load_file (edit)) {
794 /* edit_load_file already gives an error message */
795 if (to_free)
796 g_free (edit);
797 return 0;
799 edit->utf8 = 0;
800 edit->converter = str_cnv_from_term;
801 #ifdef HAVE_CHARSET
802 const char *cp_id = NULL;
803 cp_id = get_codepage_id (source_codepage >= 0 ?
804 source_codepage : display_codepage);
806 if (cp_id != NULL) {
807 GIConv conv;
808 conv = str_crt_conv_from (cp_id);
809 if (conv != INVALID_CONV) {
810 if (edit->converter != str_cnv_from_term)
811 str_close_conv (edit->converter);
812 edit->converter = conv;
815 if (cp_id != NULL)
816 edit->utf8 = str_isutf8 (cp_id);
817 #endif
819 edit->loading_done = 1;
820 edit->modified = 0;
821 edit->locked = 0;
822 edit_load_syntax (edit, 0, 0);
824 int color;
825 edit_get_syntax_color (edit, -1, &color);
828 /* load saved cursor position */
829 if ((line == 0) && option_save_position) {
830 edit_load_position (edit);
831 } else {
832 if (line <= 0)
833 line = 1;
834 edit_move_display (edit, line - 1);
835 edit_move_to_line (edit, line - 1);
838 edit_set_keymap (edit);
840 return edit;
843 /* Clear the edit struct, freeing everything in it. Return 1 on success */
845 edit_clean (WEdit *edit)
847 int j = 0;
849 if (!edit)
850 return 0;
852 /* a stale lock, remove it */
853 if (edit->locked)
854 edit->locked = edit_unlock_file (edit->filename);
856 /* save cursor position */
857 if (option_save_position)
858 edit_save_position (edit);
860 /* File specified on the mcedit command line and never saved */
861 if (edit->delete_file)
862 unlink (edit->filename);
864 edit_free_syntax_rules (edit);
865 book_mark_flush (edit, -1);
866 for (; j <= MAXBUFF; j++) {
867 g_free (edit->buffers1[j]);
868 g_free (edit->buffers2[j]);
871 g_free (edit->undo_stack);
872 g_free (edit->filename);
873 g_free (edit->dir);
875 if (edit->search)
877 mc_search_free(edit->search);
878 edit->search = NULL;
880 edit_purge_widget (edit);
882 return 1;
886 /* returns 1 on success */
887 int edit_renew (WEdit * edit)
889 int lines = edit->num_widget_lines;
890 int columns = edit->num_widget_columns;
891 int retval = 1;
893 edit_clean (edit);
894 if (!edit_init (edit, lines, columns, "", 0))
895 retval = 0;
896 return retval;
900 * Load a new file into the editor. If it fails, preserve the old file.
901 * To do it, allocate a new widget, initialize it and, if the new file
902 * was loaded, copy the data to the old widget.
903 * Return 1 on success, 0 on failure.
906 edit_reload (WEdit *edit, const char *filename)
908 WEdit *e;
909 int lines = edit->num_widget_lines;
910 int columns = edit->num_widget_columns;
912 e = g_malloc0 (sizeof (WEdit));
913 e->widget = edit->widget;
914 if (!edit_init (e, lines, columns, filename, 0)) {
915 g_free (e);
916 return 0;
918 edit_clean (edit);
919 memcpy (edit, e, sizeof (WEdit));
920 g_free (e);
921 return 1;
925 * Load a new file into the editor and set line. If it fails, preserve the old file.
926 * To do it, allocate a new widget, initialize it and, if the new file
927 * was loaded, copy the data to the old widget.
928 * Return 1 on success, 0 on failure.
931 edit_reload_line (WEdit *edit, const char *filename, long line)
933 WEdit *e;
934 int lines = edit->num_widget_lines;
935 int columns = edit->num_widget_columns;
937 e = g_malloc0 (sizeof (WEdit));
938 e->widget = edit->widget;
939 if (!edit_init (e, lines, columns, filename, line)) {
940 g_free (e);
941 return 0;
943 edit_clean (edit);
944 memcpy (edit, e, sizeof (WEdit));
945 g_free (e);
946 return 1;
951 Recording stack for undo:
952 The following is an implementation of a compressed stack. Identical
953 pushes are recorded by a negative prefix indicating the number of times the
954 same char was pushed. This saves space for repeated curs-left or curs-right
955 delete etc.
959 pushed: stored:
963 b -3
965 c --> -4
971 If the stack long int is 0-255 it represents a normal insert (from a backspace),
972 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
973 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
974 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
975 position.
977 The only way the cursor moves or the buffer is changed is through the routines:
978 insert, backspace, insert_ahead, delete, and cursor_move.
979 These record the reverse undo movements onto the stack each time they are
980 called.
982 Each key press results in a set of actions (insert; delete ...). So each time
983 a key is pressed the current position of start_display is pushed as
984 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
985 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
986 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
990 void edit_push_action (WEdit * edit, long c,...)
992 unsigned long sp = edit->stack_pointer;
993 unsigned long spm1;
994 long *t;
996 /* first enlarge the stack if necessary */
997 if (sp > edit->stack_size - 10) { /* say */
998 if (option_max_undo < 256)
999 option_max_undo = 256;
1000 if (edit->stack_size < (unsigned long) option_max_undo) {
1001 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1002 if (t) {
1003 edit->undo_stack = t;
1004 edit->stack_size <<= 1;
1005 edit->stack_size_mask = edit->stack_size - 1;
1009 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1010 if (edit->stack_disable)
1011 return;
1013 #ifdef FAST_MOVE_CURSOR
1014 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1015 va_list ap;
1016 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1017 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1018 va_start (ap, c);
1019 c = -(va_arg (ap, int));
1020 va_end (ap);
1021 } else
1022 #endif /* ! FAST_MOVE_CURSOR */
1023 if (edit->stack_bottom != sp
1024 && spm1 != edit->stack_bottom
1025 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1026 int d;
1027 if (edit->undo_stack[spm1] < 0) {
1028 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1029 if (d == c) {
1030 if (edit->undo_stack[spm1] > -1000000000) {
1031 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1032 edit->undo_stack[spm1]--;
1033 return;
1036 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1037 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1038 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1039 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1040 if (edit->undo_stack[spm1] == -2)
1041 edit->stack_pointer = spm1;
1042 else
1043 edit->undo_stack[spm1]++;
1044 return;
1046 #endif
1047 } else {
1048 d = edit->undo_stack[spm1];
1049 if (d == c) {
1050 if (c >= KEY_PRESS)
1051 return; /* --> no need to push multiple do-nothings */
1052 edit->undo_stack[sp] = -2;
1053 goto check_bottom;
1055 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1056 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1057 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1058 edit->stack_pointer = spm1;
1059 return;
1061 #endif
1064 edit->undo_stack[sp] = c;
1065 check_bottom:
1067 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1069 /* if the sp wraps round and catches the stack_bottom then erase
1070 * the first set of actions on the stack to make space - by moving
1071 * stack_bottom forward one "key press" */
1072 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1073 if ((unsigned long) c == edit->stack_bottom ||
1074 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1075 do {
1076 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1077 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1079 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
1080 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1081 edit->stack_bottom = edit->stack_pointer = 0;
1085 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1086 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1088 static long
1089 pop_action (WEdit * edit)
1091 long c;
1092 unsigned long sp = edit->stack_pointer;
1093 if (sp == edit->stack_bottom) {
1094 return STACK_BOTTOM;
1096 sp = (sp - 1) & edit->stack_size_mask;
1097 if ((c = edit->undo_stack[sp]) >= 0) {
1098 /* edit->undo_stack[sp] = '@'; */
1099 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1100 return c;
1102 if (sp == edit->stack_bottom) {
1103 return STACK_BOTTOM;
1105 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1106 if (edit->undo_stack[sp] == -2) {
1107 /* edit->undo_stack[sp] = '@'; */
1108 edit->stack_pointer = sp;
1109 } else
1110 edit->undo_stack[sp]++;
1112 return c;
1115 /* is called whenever a modification is made by one of the four routines below */
1116 static void edit_modification (WEdit * edit)
1118 edit->caches_valid = 0;
1119 edit->screen_modified = 1;
1121 /* raise lock when file modified */
1122 if (!edit->modified && !edit->delete_file)
1123 edit->locked = edit_lock_file (edit->filename);
1124 edit->modified = 1;
1128 Basic low level single character buffer alterations and movements at the cursor.
1129 Returns char passed over, inserted or removed.
1132 void
1133 edit_insert (WEdit *edit, int c)
1135 /* check if file has grown to large */
1136 if (edit->last_byte >= SIZE_LIMIT)
1137 return;
1139 /* first we must update the position of the display window */
1140 if (edit->curs1 < edit->start_display) {
1141 edit->start_display++;
1142 if (c == '\n')
1143 edit->start_line++;
1146 /* Mark file as modified, unless the file hasn't been fully loaded */
1147 if (edit->loading_done) {
1148 edit_modification (edit);
1151 /* now we must update some info on the file and check if a redraw is required */
1152 if (c == '\n') {
1153 if (edit->book_mark)
1154 book_mark_inc (edit, edit->curs_line);
1155 edit->curs_line++;
1156 edit->total_lines++;
1157 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1160 /* save the reverse command onto the undo stack */
1161 edit_push_action (edit, BACKSPACE);
1163 /* update markers */
1164 edit->mark1 += (edit->mark1 > edit->curs1);
1165 edit->mark2 += (edit->mark2 > edit->curs1);
1166 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1168 /* add a new buffer if we've reached the end of the last one */
1169 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1170 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1171 g_malloc (EDIT_BUF_SIZE);
1173 /* perform the insertion */
1174 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1175 curs1 & M_EDIT_BUF_SIZE]
1176 = (unsigned char) c;
1178 /* update file length */
1179 edit->last_byte++;
1181 /* update cursor position */
1182 edit->curs1++;
1185 void
1186 edit_insert_over (WEdit * edit)
1188 int i;
1190 for ( i = 0; i < edit->over_col; i++ ) {
1191 edit_insert (edit, ' ');
1193 edit->over_col = 0;
1196 /* same as edit_insert and move left */
1197 void edit_insert_ahead (WEdit * edit, int c)
1199 if (edit->last_byte >= SIZE_LIMIT)
1200 return;
1201 if (edit->curs1 < edit->start_display) {
1202 edit->start_display++;
1203 if (c == '\n')
1204 edit->start_line++;
1206 edit_modification (edit);
1207 if (c == '\n') {
1208 if (edit->book_mark)
1209 book_mark_inc (edit, edit->curs_line);
1210 edit->total_lines++;
1211 edit->force |= REDRAW_AFTER_CURSOR;
1213 edit_push_action (edit, DELCHAR);
1215 edit->mark1 += (edit->mark1 >= edit->curs1);
1216 edit->mark2 += (edit->mark2 >= edit->curs1);
1217 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1219 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1220 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1221 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1223 edit->last_byte++;
1224 edit->curs2++;
1228 int edit_delete (WEdit * edit, const int byte_delete)
1230 int p = 0;
1231 int cw = 1;
1232 int i;
1234 if (!edit->curs2)
1235 return 0;
1237 edit->mark1 -= (edit->mark1 > edit->curs1);
1238 edit->mark2 -= (edit->mark2 > edit->curs1);
1239 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1241 cw = 1;
1242 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1243 if ( edit->utf8 && byte_delete == 0 ) {
1244 edit_get_utf (edit, edit->curs1, &cw);
1245 if ( cw < 1 )
1246 cw = 1;
1248 for ( i = 1; i<= cw; i++ ) {
1249 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1251 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1252 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1253 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1255 edit->last_byte--;
1256 edit->curs2--;
1257 edit_push_action (edit, p + 256);
1260 edit_modification (edit);
1261 if (p == '\n') {
1262 if (edit->book_mark)
1263 book_mark_dec (edit, edit->curs_line);
1264 edit->total_lines--;
1265 edit->force |= REDRAW_AFTER_CURSOR;
1267 if (edit->curs1 < edit->start_display) {
1268 edit->start_display--;
1269 if (p == '\n')
1270 edit->start_line--;
1273 return p;
1277 static int
1278 edit_backspace (WEdit * edit, const int byte_delete)
1280 int p = 0;
1281 int cw = 1;
1282 int i;
1284 if (!edit->curs1)
1285 return 0;
1287 edit->mark1 -= (edit->mark1 >= edit->curs1);
1288 edit->mark2 -= (edit->mark2 >= edit->curs1);
1289 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1291 cw = 1;
1292 if ( edit->utf8 && byte_delete == 0 ) {
1293 edit_get_prev_utf (edit, edit->curs1, &cw);
1294 if ( cw < 1 )
1295 cw = 1;
1297 for ( i = 1; i<= cw; i++ ) {
1298 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1299 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1300 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1301 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1303 edit->last_byte--;
1304 edit->curs1--;
1305 edit_push_action (edit, p);
1307 edit_modification (edit);
1308 if (p == '\n') {
1309 if (edit->book_mark)
1310 book_mark_dec (edit, edit->curs_line);
1311 edit->curs_line--;
1312 edit->total_lines--;
1313 edit->force |= REDRAW_AFTER_CURSOR;
1316 if (edit->curs1 < edit->start_display) {
1317 edit->start_display--;
1318 if (p == '\n')
1319 edit->start_line--;
1322 return p;
1325 #ifdef FAST_MOVE_CURSOR
1327 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1329 unsigned long next;
1330 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1331 edit->curs_line--;
1332 next -= (unsigned long) dest;
1333 n -= next;
1334 src += next;
1335 dest += next;
1340 edit_move_backward_lots (WEdit *edit, long increment)
1342 int r, s, t;
1343 unsigned char *p;
1345 if (increment > edit->curs1)
1346 increment = edit->curs1;
1347 if (increment <= 0)
1348 return -1;
1349 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1351 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1352 if (r > increment)
1353 r = increment;
1354 s = edit->curs1 & M_EDIT_BUF_SIZE;
1356 p = 0;
1357 if (s > r) {
1358 memqcpy (edit,
1359 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1360 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1362 } else {
1363 if (s) {
1364 memqcpy (edit,
1365 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1366 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1367 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1368 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1370 memqcpy (edit,
1371 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1372 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1373 EDIT_BUF_SIZE - (r - s), r - s);
1375 increment -= r;
1376 edit->curs1 -= r;
1377 edit->curs2 += r;
1378 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1379 if (p)
1380 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1381 else
1382 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1383 g_malloc (EDIT_BUF_SIZE);
1384 } else {
1385 g_free (p);
1388 s = edit->curs1 & M_EDIT_BUF_SIZE;
1389 while (increment) {
1390 p = 0;
1391 r = EDIT_BUF_SIZE;
1392 if (r > increment)
1393 r = increment;
1394 t = s;
1395 if (r < t)
1396 t = r;
1397 memqcpy (edit,
1398 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1399 EDIT_BUF_SIZE - t,
1400 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1402 if (r >= s) {
1403 if (t) {
1404 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1405 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1407 memqcpy (edit,
1408 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1409 EDIT_BUF_SIZE - r,
1410 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1411 EDIT_BUF_SIZE - (r - s), r - s);
1413 increment -= r;
1414 edit->curs1 -= r;
1415 edit->curs2 += r;
1416 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1417 if (p)
1418 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1419 else
1420 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1421 g_malloc (EDIT_BUF_SIZE);
1422 } else {
1423 g_free (p);
1426 return edit_get_byte (edit, edit->curs1);
1429 #endif /* ! FAST_MOVE_CURSOR */
1431 /* moves the cursor right or left: increment positive or negative respectively */
1432 void edit_cursor_move (WEdit * edit, long increment)
1434 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1435 int c;
1437 #ifdef FAST_MOVE_CURSOR
1438 if (increment < -256) {
1439 edit->force |= REDRAW_PAGE;
1440 edit_move_backward_lots (edit, -increment);
1441 return;
1443 #endif /* ! FAST_MOVE_CURSOR */
1445 if (increment < 0) {
1446 for (; increment < 0; increment++) {
1447 if (!edit->curs1)
1448 return;
1450 edit_push_action (edit, CURS_RIGHT);
1452 c = edit_get_byte (edit, edit->curs1 - 1);
1453 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1454 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1455 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1456 edit->curs2++;
1457 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1458 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1459 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1460 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1462 edit->curs1--;
1463 if (c == '\n') {
1464 edit->curs_line--;
1465 edit->force |= REDRAW_LINE_BELOW;
1469 } else if (increment > 0) {
1470 for (; increment > 0; increment--) {
1471 if (!edit->curs2)
1472 return;
1474 edit_push_action (edit, CURS_LEFT);
1476 c = edit_get_byte (edit, edit->curs1);
1477 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1478 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1479 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1480 edit->curs1++;
1481 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1482 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1483 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1484 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1486 edit->curs2--;
1487 if (c == '\n') {
1488 edit->curs_line++;
1489 edit->force |= REDRAW_LINE_ABOVE;
1495 /* These functions return positions relative to lines */
1497 /* returns index of last char on line + 1 */
1498 long edit_eol (WEdit * edit, long current)
1500 if (current < edit->last_byte) {
1501 for (;; current++)
1502 if (edit_get_byte (edit, current) == '\n')
1503 break;
1504 } else
1505 return edit->last_byte;
1506 return current;
1509 /* returns index of first char on line */
1510 long edit_bol (WEdit * edit, long current)
1512 if (current > 0) {
1513 for (;; current--)
1514 if (edit_get_byte (edit, current - 1) == '\n')
1515 break;
1516 } else
1517 return 0;
1518 return current;
1522 int edit_count_lines (WEdit * edit, long current, int upto)
1524 int lines = 0;
1525 if (upto > edit->last_byte)
1526 upto = edit->last_byte;
1527 if (current < 0)
1528 current = 0;
1529 while (current < upto)
1530 if (edit_get_byte (edit, current++) == '\n')
1531 lines++;
1532 return lines;
1536 /* If lines is zero this returns the count of lines from current to upto. */
1537 /* If upto is zero returns index of lines forward current. */
1538 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1540 if (upto) {
1541 return edit_count_lines (edit, current, upto);
1542 } else {
1543 int next;
1544 if (lines < 0)
1545 lines = 0;
1546 while (lines--) {
1547 next = edit_eol (edit, current) + 1;
1548 if (next > edit->last_byte)
1549 break;
1550 else
1551 current = next;
1553 return current;
1558 /* Returns offset of 'lines' lines up from current */
1559 long edit_move_backward (WEdit * edit, long current, int lines)
1561 if (lines < 0)
1562 lines = 0;
1563 current = edit_bol (edit, current);
1564 while((lines--) && current != 0)
1565 current = edit_bol (edit, current - 1);
1566 return current;
1569 /* If cols is zero this returns the count of columns from current to upto. */
1570 /* If upto is zero returns index of cols across from current. */
1571 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1573 long p, q;
1574 int col = 0;
1575 #ifdef HAVE_CHARSET
1576 int cw = 1;
1577 int utf_ch = 0;
1578 #endif
1579 if (upto) {
1580 q = upto;
1581 cols = -10;
1582 } else
1583 q = edit->last_byte + 2;
1584 for (col = 0, p = current; p < q; p++) {
1585 int c;
1586 #ifdef HAVE_CHARSET
1587 cw = 1;
1588 utf_ch = 0;
1589 #endif
1590 if (cols != -10) {
1591 if (col == cols)
1592 return p;
1593 if (col > cols)
1594 return p - 1;
1596 #ifdef HAVE_CHARSET
1597 if ( !edit->utf8 ) {
1598 #endif
1599 c = edit_get_byte (edit, p);
1600 #ifdef HAVE_CHARSET
1601 } else {
1602 cw = 1;
1603 c = edit_get_byte (edit, p);
1604 utf_ch = edit_get_utf (edit, p, &cw);
1606 if ( utf8_display ) {
1607 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1608 col++;
1610 #endif
1611 if (c == '\t')
1612 col += TAB_SIZE - col % TAB_SIZE;
1613 else if (c == '\n') {
1614 if (upto)
1615 return col;
1616 else
1617 return p;
1618 } else if (c < 32 || c == 127)
1619 col += 2; /* Caret notation for control characters */
1620 else
1621 col++;
1622 #ifdef HAVE_CHARSET
1623 if ( cw > 1 )
1624 col -= cw-1;
1625 #endif
1627 return col;
1630 /* returns the current column position of the cursor */
1631 int edit_get_col (WEdit * edit)
1633 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1637 /* Scrolling functions */
1639 void edit_update_curs_row (WEdit * edit)
1641 edit->curs_row = edit->curs_line - edit->start_line;
1644 void edit_update_curs_col (WEdit * edit)
1646 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1650 edit_get_curs_col (const WEdit *edit)
1652 return edit->curs_col;
1655 /*moves the display start position up by i lines */
1656 void edit_scroll_upward (WEdit * edit, unsigned long i)
1658 unsigned long lines_above = edit->start_line;
1659 if (i > lines_above)
1660 i = lines_above;
1661 if (i) {
1662 edit->start_line -= i;
1663 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1664 edit->force |= REDRAW_PAGE;
1665 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1667 edit_update_curs_row (edit);
1671 /* returns 1 if could scroll, 0 otherwise */
1672 void edit_scroll_downward (WEdit * edit, int i)
1674 int lines_below;
1675 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1676 if (lines_below > 0) {
1677 if (i > lines_below)
1678 i = lines_below;
1679 edit->start_line += i;
1680 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1681 edit->force |= REDRAW_PAGE;
1682 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1684 edit_update_curs_row (edit);
1687 void edit_scroll_right (WEdit * edit, int i)
1689 edit->force |= REDRAW_PAGE;
1690 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1691 edit->start_col -= i;
1694 void edit_scroll_left (WEdit * edit, int i)
1696 if (edit->start_col) {
1697 edit->start_col += i;
1698 if (edit->start_col > 0)
1699 edit->start_col = 0;
1700 edit->force |= REDRAW_PAGE;
1701 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1705 /* high level cursor movement commands */
1707 static int is_in_indent (WEdit *edit)
1709 long p = edit_bol (edit, edit->curs1);
1710 while (p < edit->curs1)
1711 if (!strchr (" \t", edit_get_byte (edit, p++)))
1712 return 0;
1713 return 1;
1716 static int left_of_four_spaces (WEdit *edit);
1718 void
1719 edit_move_to_prev_col (WEdit * edit, long p)
1721 int prev = edit->prev_col;
1722 int over = edit->over_col;
1724 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1726 if (option_cursor_beyond_eol) {
1727 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1729 if (line_len < prev + edit->over_col) {
1730 edit->over_col = prev + over - line_len;
1731 edit->prev_col = line_len;
1732 edit->curs_col = line_len;
1733 } else {
1734 edit->curs_col = prev + over;
1735 edit->prev_col = edit->curs_col;
1736 edit->over_col = 0;
1738 } else {
1739 edit->over_col = 0;
1740 if (is_in_indent (edit) && option_fake_half_tabs) {
1741 edit_update_curs_col (edit);
1742 if (space_width)
1743 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1744 int q = edit->curs_col;
1745 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1746 p = edit_bol (edit, edit->curs1);
1747 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1748 if (!left_of_four_spaces (edit))
1749 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1755 /* move i lines */
1756 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1758 unsigned long p, l = edit->curs_line;
1760 if (i > l)
1761 i = l;
1762 if (i) {
1763 if (i > 1)
1764 edit->force |= REDRAW_PAGE;
1765 if (scroll)
1766 edit_scroll_upward (edit, i);
1768 p = edit_bol (edit, edit->curs1);
1769 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1770 edit_move_to_prev_col (edit, p);
1772 edit->search_start = edit->curs1;
1773 edit->found_len = 0;
1777 static int
1778 is_blank (WEdit *edit, long offset)
1780 long s, f;
1781 int c;
1782 s = edit_bol (edit, offset);
1783 f = edit_eol (edit, offset) - 1;
1784 while (s <= f) {
1785 c = edit_get_byte (edit, s++);
1786 if (!isspace (c))
1787 return 0;
1789 return 1;
1793 /* returns the offset of line i */
1794 static long
1795 edit_find_line (WEdit *edit, int line)
1797 int i, j = 0;
1798 int m = 2000000000;
1799 if (!edit->caches_valid) {
1800 for (i = 0; i < N_LINE_CACHES; i++)
1801 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1802 /* three offsets that we *know* are line 0 at 0 and these two: */
1803 edit->line_numbers[1] = edit->curs_line;
1804 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1805 edit->line_numbers[2] = edit->total_lines;
1806 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1807 edit->caches_valid = 1;
1809 if (line >= edit->total_lines)
1810 return edit->line_offsets[2];
1811 if (line <= 0)
1812 return 0;
1813 /* find the closest known point */
1814 for (i = 0; i < N_LINE_CACHES; i++) {
1815 int n;
1816 n = abs (edit->line_numbers[i] - line);
1817 if (n < m) {
1818 m = n;
1819 j = i;
1822 if (m == 0)
1823 return edit->line_offsets[j]; /* know the offset exactly */
1824 if (m == 1 && j >= 3)
1825 i = j; /* one line different - caller might be looping, so stay in this cache */
1826 else
1827 i = 3 + (rand () % (N_LINE_CACHES - 3));
1828 if (line > edit->line_numbers[j])
1829 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1830 else
1831 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1832 edit->line_numbers[i] = line;
1833 return edit->line_offsets[i];
1836 int line_is_blank (WEdit * edit, long line)
1838 return is_blank (edit, edit_find_line (edit, line));
1841 /* moves up until a blank line is reached, or until just
1842 before a non-blank line is reached */
1843 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1845 int i;
1846 if (edit->curs_line <= 1) {
1847 i = 0;
1848 } else {
1849 if (line_is_blank (edit, edit->curs_line)) {
1850 if (line_is_blank (edit, edit->curs_line - 1)) {
1851 for (i = edit->curs_line - 1; i; i--)
1852 if (!line_is_blank (edit, i)) {
1853 i++;
1854 break;
1856 } else {
1857 for (i = edit->curs_line - 1; i; i--)
1858 if (line_is_blank (edit, i))
1859 break;
1861 } else {
1862 for (i = edit->curs_line - 1; i; i--)
1863 if (line_is_blank (edit, i))
1864 break;
1867 edit_move_up (edit, edit->curs_line - i, scroll);
1870 /* move i lines */
1871 void edit_move_down (WEdit * edit, int i, int scroll)
1873 long p, l = edit->total_lines - edit->curs_line;
1875 if (i > l)
1876 i = l;
1877 if (i) {
1878 if (i > 1)
1879 edit->force |= REDRAW_PAGE;
1880 if (scroll)
1881 edit_scroll_downward (edit, i);
1882 p = edit_bol (edit, edit->curs1);
1883 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1884 edit_move_to_prev_col (edit, p);
1886 edit->search_start = edit->curs1;
1887 edit->found_len = 0;
1891 /* moves down until a blank line is reached, or until just
1892 before a non-blank line is reached */
1893 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1895 int i;
1896 if (edit->curs_line >= edit->total_lines - 1) {
1897 i = edit->total_lines;
1898 } else {
1899 if (line_is_blank (edit, edit->curs_line)) {
1900 if (line_is_blank (edit, edit->curs_line + 1)) {
1901 for (i = edit->curs_line + 1; i; i++)
1902 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1903 i--;
1904 break;
1906 } else {
1907 for (i = edit->curs_line + 1; i; i++)
1908 if (line_is_blank (edit, i) || i >= edit->total_lines)
1909 break;
1911 } else {
1912 for (i = edit->curs_line + 1; i; i++)
1913 if (line_is_blank (edit, i) || i >= edit->total_lines)
1914 break;
1917 edit_move_down (edit, i - edit->curs_line, scroll);
1920 static void edit_begin_page (WEdit *edit)
1922 edit_update_curs_row (edit);
1923 edit_move_up (edit, edit->curs_row, 0);
1926 static void edit_end_page (WEdit *edit)
1928 edit_update_curs_row (edit);
1929 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1933 /* goto beginning of text */
1934 static void edit_move_to_top (WEdit * edit)
1936 if (edit->curs_line) {
1937 edit_cursor_move (edit, -edit->curs1);
1938 edit_move_to_prev_col (edit, 0);
1939 edit->force |= REDRAW_PAGE;
1940 edit->search_start = 0;
1941 edit_update_curs_row(edit);
1946 /* goto end of text */
1947 static void edit_move_to_bottom (WEdit * edit)
1949 if (edit->curs_line < edit->total_lines) {
1950 edit_cursor_move (edit, edit->curs2);
1951 edit->start_display = edit->last_byte;
1952 edit->start_line = edit->total_lines;
1953 edit_update_curs_row(edit);
1954 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1955 edit->force |= REDRAW_PAGE;
1959 /* goto beginning of line */
1960 static void edit_cursor_to_bol (WEdit * edit)
1962 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1963 edit->search_start = edit->curs1;
1964 edit->prev_col = edit_get_col (edit);
1965 edit->over_col = 0;
1968 /* goto end of line */
1969 static void edit_cursor_to_eol (WEdit * edit)
1971 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1972 edit->search_start = edit->curs1;
1973 edit->prev_col = edit_get_col (edit);
1974 edit->over_col = 0;
1977 /* move cursor to line 'line' */
1978 void edit_move_to_line (WEdit * e, long line)
1980 if(line < e->curs_line)
1981 edit_move_up (e, e->curs_line - line, 0);
1982 else
1983 edit_move_down (e, line - e->curs_line, 0);
1984 edit_scroll_screen_over_cursor (e);
1987 /* scroll window so that first visible line is 'line' */
1988 void edit_move_display (WEdit * e, long line)
1990 if(line < e->start_line)
1991 edit_scroll_upward (e, e->start_line - line);
1992 else
1993 edit_scroll_downward (e, line - e->start_line);
1996 /* save markers onto undo stack */
1997 void edit_push_markers (WEdit * edit)
1999 edit_push_action (edit, MARK_1 + edit->mark1);
2000 edit_push_action (edit, MARK_2 + edit->mark2);
2003 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2005 edit->mark1 = m1;
2006 edit->mark2 = m2;
2007 edit->column1 = c1;
2008 edit->column2 = c2;
2012 /* highlight marker toggle */
2013 void edit_mark_cmd (WEdit * edit, int unmark)
2015 edit_push_markers (edit);
2016 if (unmark) {
2017 edit_set_markers (edit, 0, 0, 0, 0);
2018 edit->force |= REDRAW_PAGE;
2019 } else {
2020 if (edit->mark2 >= 0) {
2021 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
2022 edit->force |= REDRAW_PAGE;
2023 } else
2024 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2028 static unsigned long
2029 my_type_of (int c)
2031 int x, r = 0;
2032 const char *p, *q;
2033 const char option_chars_move_whole_word[] =
2034 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2036 if (!c)
2037 return 0;
2038 if (c == '!') {
2039 if (*option_chars_move_whole_word == '!')
2040 return 2;
2041 return 0x80000000UL;
2043 if (g_ascii_isupper ((gchar) c))
2044 c = 'A';
2045 else if (g_ascii_islower ((gchar) c))
2046 c = 'a';
2047 else if (g_ascii_isalpha (c))
2048 c = 'a';
2049 else if (isdigit (c))
2050 c = '0';
2051 else if (isspace (c))
2052 c = ' ';
2053 q = strchr (option_chars_move_whole_word, c);
2054 if (!q)
2055 return 0xFFFFFFFFUL;
2056 do {
2057 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2058 if (*p == '!')
2059 x <<= 1;
2060 r |= x;
2061 } while ((q = strchr (q + 1, c)));
2062 return r;
2065 static void
2066 edit_left_word_move (WEdit *edit, int s)
2068 for (;;) {
2069 int c1, c2;
2070 edit_cursor_move (edit, -1);
2071 if (!edit->curs1)
2072 break;
2073 c1 = edit_get_byte (edit, edit->curs1 - 1);
2074 c2 = edit_get_byte (edit, edit->curs1);
2075 if (!(my_type_of (c1) & my_type_of (c2)))
2076 break;
2077 if (isspace (c1) && !isspace (c2))
2078 break;
2079 if (s)
2080 if (!isspace (c1) && isspace (c2))
2081 break;
2085 static void edit_left_word_move_cmd (WEdit * edit)
2087 edit_left_word_move (edit, 0);
2088 edit->force |= REDRAW_PAGE;
2091 static void
2092 edit_right_word_move (WEdit *edit, int s)
2094 for (;;) {
2095 int c1, c2;
2096 edit_cursor_move (edit, 1);
2097 if (edit->curs1 >= edit->last_byte)
2098 break;
2099 c1 = edit_get_byte (edit, edit->curs1 - 1);
2100 c2 = edit_get_byte (edit, edit->curs1);
2101 if (!(my_type_of (c1) & my_type_of (c2)))
2102 break;
2103 if (isspace (c1) && !isspace (c2))
2104 break;
2105 if (s)
2106 if (!isspace (c1) && isspace (c2))
2107 break;
2111 static void edit_right_word_move_cmd (WEdit * edit)
2113 edit_right_word_move (edit, 0);
2114 edit->force |= REDRAW_PAGE;
2117 static void edit_right_char_move_cmd (WEdit * edit)
2119 int cw = 1;
2120 int c = 0;
2121 if ( edit->utf8 ) {
2122 c = edit_get_utf (edit, edit->curs1, &cw);
2123 if ( cw < 1 )
2124 cw = 1;
2125 } else {
2126 c = edit_get_byte (edit, edit->curs1);
2128 if (option_cursor_beyond_eol && c == '\n') {
2129 edit->over_col++;
2130 } else {
2131 edit_cursor_move (edit, cw);
2135 static void edit_left_char_move_cmd (WEdit * edit)
2137 int cw = 1;
2138 if ( edit->utf8 ) {
2139 edit_get_prev_utf (edit, edit->curs1, &cw);
2140 if ( cw < 1 )
2141 cw = 1;
2143 if (option_cursor_beyond_eol && edit->over_col > 0) {
2144 edit->over_col--;
2145 } else {
2146 edit_cursor_move (edit, -cw);
2151 static void edit_right_delete_word (WEdit * edit)
2153 int c1, c2;
2154 for (;;) {
2155 if (edit->curs1 >= edit->last_byte)
2156 break;
2157 c1 = edit_delete (edit, 1);
2158 c2 = edit_get_byte (edit, edit->curs1);
2159 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2160 break;
2161 if (!(my_type_of (c1) & my_type_of (c2)))
2162 break;
2166 static void edit_left_delete_word (WEdit * edit)
2168 int c1, c2;
2169 for (;;) {
2170 if (edit->curs1 <= 0)
2171 break;
2172 c1 = edit_backspace (edit, 1);
2173 c2 = edit_get_byte (edit, edit->curs1 - 1);
2174 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2175 break;
2176 if (!(my_type_of (c1) & my_type_of (c2)))
2177 break;
2182 the start column position is not recorded, and hence does not
2183 undo as it happed. But who would notice.
2185 static void
2186 edit_do_undo (WEdit * edit)
2188 long ac;
2189 long count = 0;
2191 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2193 while ((ac = pop_action (edit)) < KEY_PRESS) {
2194 switch ((int) ac) {
2195 case STACK_BOTTOM:
2196 goto done_undo;
2197 case CURS_RIGHT:
2198 edit_cursor_move (edit, 1);
2199 break;
2200 case CURS_LEFT:
2201 edit_cursor_move (edit, -1);
2202 break;
2203 case BACKSPACE:
2204 edit_backspace (edit, 1);
2205 break;
2206 case DELCHAR:
2207 edit_delete (edit, 1);
2208 break;
2209 case COLUMN_ON:
2210 column_highlighting = 1;
2211 break;
2212 case COLUMN_OFF:
2213 column_highlighting = 0;
2214 break;
2216 if (ac >= 256 && ac < 512)
2217 edit_insert_ahead (edit, ac - 256);
2218 if (ac >= 0 && ac < 256)
2219 edit_insert (edit, ac);
2221 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2222 edit->mark1 = ac - MARK_1;
2223 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2224 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2225 edit->mark2 = ac - MARK_2;
2226 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2228 if (count++)
2229 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2232 if (edit->start_display > ac - KEY_PRESS) {
2233 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2234 edit->force |= REDRAW_PAGE;
2235 } else if (edit->start_display < ac - KEY_PRESS) {
2236 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2237 edit->force |= REDRAW_PAGE;
2239 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2240 edit_update_curs_row (edit);
2242 done_undo:;
2243 edit->stack_disable = 0;
2246 static void edit_delete_to_line_end (WEdit * edit)
2248 while (edit_get_byte (edit, edit->curs1) != '\n') {
2249 if (!edit->curs2)
2250 break;
2251 edit_delete (edit, 1);
2255 static void edit_delete_to_line_begin (WEdit * edit)
2257 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2258 if (!edit->curs1)
2259 break;
2260 edit_backspace (edit, 1);
2264 void
2265 edit_delete_line (WEdit *edit)
2268 * Delete right part of the line.
2269 * Note that edit_get_byte() returns '\n' when byte position is
2270 * beyond EOF.
2272 while (edit_get_byte (edit, edit->curs1) != '\n') {
2273 (void) edit_delete (edit, 1);
2277 * Delete '\n' char.
2278 * Note that edit_delete() will not corrupt anything if called while
2279 * cursor position is EOF.
2281 (void) edit_delete (edit, 1);
2284 * Delete left part of the line.
2285 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2287 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2288 (void) edit_backspace (edit, 1);
2292 void insert_spaces_tab (WEdit * edit, int half)
2294 int i;
2295 edit_update_curs_col (edit);
2296 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2297 while (i > 0) {
2298 edit_insert (edit, ' ');
2299 i -= space_width;
2303 static int is_aligned_on_a_tab (WEdit * edit)
2305 edit_update_curs_col (edit);
2306 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2307 return 0; /* not alligned on a tab */
2308 return 1;
2311 static int right_of_four_spaces (WEdit *edit)
2313 int i, ch = 0;
2314 for (i = 1; i <= HALF_TAB_SIZE; i++)
2315 ch |= edit_get_byte (edit, edit->curs1 - i);
2316 if (ch == ' ')
2317 return is_aligned_on_a_tab (edit);
2318 return 0;
2321 static int left_of_four_spaces (WEdit *edit)
2323 int i, ch = 0;
2324 for (i = 0; i < HALF_TAB_SIZE; i++)
2325 ch |= edit_get_byte (edit, edit->curs1 + i);
2326 if (ch == ' ')
2327 return is_aligned_on_a_tab (edit);
2328 return 0;
2331 int edit_indent_width (WEdit * edit, long p)
2333 long q = p;
2334 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2335 q++;
2336 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2339 void edit_insert_indent (WEdit * edit, int indent)
2341 if (!option_fill_tabs_with_spaces) {
2342 while (indent >= TAB_SIZE) {
2343 edit_insert (edit, '\t');
2344 indent -= TAB_SIZE;
2347 while (indent-- > 0)
2348 edit_insert (edit, ' ');
2351 static void
2352 edit_auto_indent (WEdit * edit)
2354 long p;
2355 char c;
2356 p = edit->curs1;
2357 /* use the previous line as a template */
2358 p = edit_move_backward (edit, p, 1);
2359 /* copy the leading whitespace of the line */
2360 for (;;) { /* no range check - the line _is_ \n-terminated */
2361 c = edit_get_byte (edit, p++);
2362 if (c != ' ' && c != '\t')
2363 break;
2364 edit_insert (edit, c);
2368 static void edit_double_newline (WEdit * edit)
2370 edit_insert (edit, '\n');
2371 if (edit_get_byte (edit, edit->curs1) == '\n')
2372 return;
2373 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2374 return;
2375 edit->force |= REDRAW_PAGE;
2376 edit_insert (edit, '\n');
2379 static void edit_tab_cmd (WEdit * edit)
2381 int i;
2383 if (option_fake_half_tabs) {
2384 if (is_in_indent (edit)) {
2385 /*insert a half tab (usually four spaces) unless there is a
2386 half tab already behind, then delete it and insert a
2387 full tab. */
2388 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2389 for (i = 1; i <= HALF_TAB_SIZE; i++)
2390 edit_backspace (edit, 1);
2391 edit_insert (edit, '\t');
2392 } else {
2393 insert_spaces_tab (edit, 1);
2395 return;
2398 if (option_fill_tabs_with_spaces) {
2399 insert_spaces_tab (edit, 0);
2400 } else {
2401 edit_insert (edit, '\t');
2403 return;
2406 static void check_and_wrap_line (WEdit * edit)
2408 int curs, c;
2409 if (!option_typewriter_wrap)
2410 return;
2411 edit_update_curs_col (edit);
2412 if (edit->curs_col < option_word_wrap_line_length)
2413 return;
2414 curs = edit->curs1;
2415 for (;;) {
2416 curs--;
2417 c = edit_get_byte (edit, curs);
2418 if (c == '\n' || curs <= 0) {
2419 edit_insert (edit, '\n');
2420 return;
2422 if (c == ' ' || c == '\t') {
2423 int current = edit->curs1;
2424 edit_cursor_move (edit, curs - edit->curs1 + 1);
2425 edit_insert (edit, '\n');
2426 edit_cursor_move (edit, current - edit->curs1 + 1);
2427 return;
2432 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2434 void edit_push_key_press (WEdit * edit)
2436 edit_push_action (edit, KEY_PRESS + edit->start_display);
2437 if (edit->mark2 == -1)
2438 edit_push_action (edit, MARK_1 + edit->mark1);
2441 /* this find the matching bracket in either direction, and sets edit->bracket */
2442 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2444 const char * const b = "{}{[][()(", *p;
2445 int i = 1, a, inc = -1, c, d, n = 0;
2446 unsigned long j = 0;
2447 long q;
2448 edit_update_curs_row (edit);
2449 c = edit_get_byte (edit, edit->curs1);
2450 p = strchr (b, c);
2451 /* no limit */
2452 if (!furthest_bracket_search)
2453 furthest_bracket_search--;
2454 /* not on a bracket at all */
2455 if (!p)
2456 return -1;
2457 /* the matching bracket */
2458 d = p[1];
2459 /* going left or right? */
2460 if (strchr ("{[(", c))
2461 inc = 1;
2462 for (q = edit->curs1 + inc;; q += inc) {
2463 /* out of buffer? */
2464 if (q >= edit->last_byte || q < 0)
2465 break;
2466 a = edit_get_byte (edit, q);
2467 /* don't want to eat CPU */
2468 if (j++ > furthest_bracket_search)
2469 break;
2470 /* out of screen? */
2471 if (in_screen) {
2472 if (q < edit->start_display)
2473 break;
2474 /* count lines if searching downward */
2475 if (inc > 0 && a == '\n')
2476 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2477 break;
2479 /* count bracket depth */
2480 i += (a == c) - (a == d);
2481 /* return if bracket depth is zero */
2482 if (!i)
2483 return q;
2485 /* no match */
2486 return -1;
2489 static long last_bracket = -1;
2491 void edit_find_bracket (WEdit * edit)
2493 edit->bracket = edit_get_bracket (edit, 1, 10000);
2494 if (last_bracket != edit->bracket)
2495 edit->force |= REDRAW_PAGE;
2496 last_bracket = edit->bracket;
2499 static void edit_goto_matching_bracket (WEdit *edit)
2501 long q;
2502 q = edit_get_bracket (edit, 0, 0);
2503 if (q < 0)
2504 return;
2505 edit->bracket = edit->curs1;
2506 edit->force |= REDRAW_PAGE;
2507 edit_cursor_move (edit, q - edit->curs1);
2511 * This executes a command as though the user initiated it through a key
2512 * press. Callback with WIDGET_KEY as a message calls this after
2513 * translating the key press. This function can be used to pass any
2514 * command to the editor. Note that the screen wouldn't update
2515 * automatically. Either of command or char_for_insertion must be
2516 * passed as -1. Commands are executed, and char_for_insertion is
2517 * inserted at the cursor.
2519 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2521 if (command == CK_Begin_Record_Macro) {
2522 edit->macro_i = 0;
2523 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2524 return;
2526 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2527 edit->force |= REDRAW_COMPLETELY;
2528 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2529 edit->macro_i = -1;
2530 return;
2532 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2533 edit->macro[edit->macro_i].command = command;
2534 edit->macro[edit->macro_i++].ch = char_for_insertion;
2536 /* record the beginning of a set of editing actions initiated by a key press */
2537 if (command != CK_Undo && command != CK_Ext_Mode)
2538 edit_push_key_press (edit);
2540 edit_execute_cmd (edit, command, char_for_insertion);
2541 if (column_highlighting)
2542 edit->force |= REDRAW_PAGE;
2545 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2548 This executes a command at a lower level than macro recording.
2549 It also does not push a key_press onto the undo stack. This means
2550 that if it is called many times, a single undo command will undo
2551 all of them. It also does not check for the Undo command.
2553 void
2554 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2556 edit->force |= REDRAW_LINE;
2558 /* The next key press will unhighlight the found string, so update
2559 * the whole page */
2560 if (edit->found_len || column_highlighting)
2561 edit->force |= REDRAW_PAGE;
2563 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2564 column_highlighting = 0;
2565 if (!edit->highlight
2566 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2567 edit_mark_cmd (edit, 1); /* clear */
2568 edit_mark_cmd (edit, 0); /* marking on */
2570 edit->highlight = 1;
2571 } else { /* any other command */
2572 if (edit->highlight)
2573 edit_mark_cmd (edit, 0); /* clear */
2574 edit->highlight = 0;
2577 /* first check for undo */
2578 if (command == CK_Undo) {
2579 edit_do_undo (edit);
2580 edit->found_len = 0;
2581 edit->prev_col = edit_get_col (edit);
2582 edit->search_start = edit->curs1;
2583 return;
2586 /* An ordinary key press */
2587 if (char_for_insertion >= 0) {
2588 if (edit->overwrite) {
2589 if (edit_get_byte (edit, edit->curs1) != '\n')
2590 edit_delete (edit, 0);
2592 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2593 edit_insert_over (edit);
2594 #ifdef HAVE_CHARSET
2595 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2596 unsigned char str[6 + 1];
2597 size_t i = 0;
2598 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2599 if ( res == 0 ) {
2600 str[0] = '.';
2601 str[1] = '\0';
2602 } else {
2603 str[res] = '\0';
2605 while ( str[i] != 0 && i<=6) {
2606 char_for_insertion = str[i];
2607 edit_insert (edit, char_for_insertion);
2608 i++;
2610 } else {
2611 #endif
2612 edit_insert (edit, char_for_insertion);
2613 #ifdef HAVE_CHARSET
2615 #endif
2616 if (option_auto_para_formatting) {
2617 format_paragraph (edit, 0);
2618 edit->force |= REDRAW_PAGE;
2619 } else
2620 check_and_wrap_line (edit);
2621 edit->found_len = 0;
2622 edit->prev_col = edit_get_col (edit);
2623 edit->search_start = edit->curs1;
2624 edit_find_bracket (edit);
2625 return;
2628 switch (command) {
2629 case CK_Begin_Page:
2630 case CK_End_Page:
2631 case CK_Begin_Page_Highlight:
2632 case CK_End_Page_Highlight:
2633 case CK_Word_Left:
2634 case CK_Word_Right:
2635 case CK_Up:
2636 case CK_Down:
2637 case CK_Left:
2638 case CK_Right:
2639 if ( edit->mark2 >= 0 ) {
2640 if ( !option_persistent_selections ) {
2641 if (column_highlighting)
2642 edit_push_action (edit, COLUMN_ON);
2643 column_highlighting = 0;
2644 edit_mark_cmd (edit, 1);
2649 switch (command) {
2650 case CK_Begin_Page:
2651 case CK_End_Page:
2652 case CK_Begin_Page_Highlight:
2653 case CK_End_Page_Highlight:
2654 case CK_Word_Left:
2655 case CK_Word_Right:
2656 case CK_Up:
2657 case CK_Down:
2658 case CK_Word_Left_Highlight:
2659 case CK_Word_Right_Highlight:
2660 case CK_Up_Highlight:
2661 case CK_Down_Highlight:
2662 case CK_Up_Alt_Highlight:
2663 case CK_Down_Alt_Highlight:
2664 if (edit->mark2 == -1)
2665 break; /*marking is following the cursor: may need to highlight a whole line */
2666 case CK_Left:
2667 case CK_Right:
2668 case CK_Left_Highlight:
2669 case CK_Right_Highlight:
2670 edit->force |= REDRAW_CHAR_ONLY;
2673 /* basic cursor key commands */
2674 switch (command) {
2675 case CK_BackSpace:
2676 /* if non persistent selection and text selected */
2677 if ( !option_persistent_selections ) {
2678 if ( edit->mark1 != edit->mark2 ) {
2679 edit_block_delete_cmd (edit);
2680 break;
2683 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2684 edit->over_col--;
2685 break;
2687 if (option_backspace_through_tabs && is_in_indent (edit)) {
2688 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2689 && edit->curs1 > 0)
2690 edit_backspace (edit, 1);
2691 break;
2692 } else {
2693 if (option_fake_half_tabs) {
2694 int i;
2695 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2696 for (i = 0; i < HALF_TAB_SIZE; i++)
2697 edit_backspace (edit, 1);
2698 break;
2702 edit_backspace (edit, 0);
2703 break;
2704 case CK_Delete:
2705 /* if non persistent selection and text selected */
2706 if ( !option_persistent_selections ) {
2707 if ( edit->mark1 != edit->mark2 ) {
2708 edit_block_delete_cmd (edit);
2709 break;
2713 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2714 edit_insert_over (edit);
2716 if (option_fake_half_tabs) {
2717 int i;
2718 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2719 for (i = 1; i <= HALF_TAB_SIZE; i++)
2720 edit_delete (edit, 1);
2721 break;
2724 edit_delete (edit, 0);
2725 break;
2726 case CK_Delete_Word_Left:
2727 edit->over_col = 0;
2728 edit_left_delete_word (edit);
2729 break;
2730 case CK_Delete_Word_Right:
2731 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2732 edit_insert_over (edit);
2734 edit_right_delete_word (edit);
2735 break;
2736 case CK_Delete_Line:
2737 edit_delete_line (edit);
2738 break;
2739 case CK_Delete_To_Line_End:
2740 edit_delete_to_line_end (edit);
2741 break;
2742 case CK_Delete_To_Line_Begin:
2743 edit_delete_to_line_begin (edit);
2744 break;
2745 case CK_Enter:
2746 edit->over_col = 0;
2747 if (option_auto_para_formatting) {
2748 edit_double_newline (edit);
2749 if (option_return_does_auto_indent)
2750 edit_auto_indent (edit);
2751 format_paragraph (edit, 0);
2752 } else {
2753 edit_insert (edit, '\n');
2754 if (option_return_does_auto_indent) {
2755 edit_auto_indent (edit);
2758 break;
2759 case CK_Return:
2760 edit_insert (edit, '\n');
2761 break;
2763 case CK_Page_Up_Alt_Highlight:
2764 column_highlighting = 1;
2765 case CK_Page_Up:
2766 case CK_Page_Up_Highlight:
2767 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2768 break;
2769 case CK_Page_Down_Alt_Highlight:
2770 column_highlighting = 1;
2771 case CK_Page_Down:
2772 case CK_Page_Down_Highlight:
2773 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2774 break;
2775 case CK_Left_Alt_Highlight:
2776 column_highlighting = 1;
2777 case CK_Left:
2778 case CK_Left_Highlight:
2779 if (option_fake_half_tabs) {
2780 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2781 if ( option_cursor_beyond_eol && edit->over_col > 0)
2782 edit->over_col--;
2783 else
2784 edit_cursor_move (edit, -HALF_TAB_SIZE);
2785 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2786 break;
2789 edit_left_char_move_cmd (edit);
2790 break;
2791 case CK_Right_Alt_Highlight:
2792 column_highlighting = 1;
2793 case CK_Right:
2794 case CK_Right_Highlight:
2795 if (option_fake_half_tabs) {
2796 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2797 edit_cursor_move (edit, HALF_TAB_SIZE);
2798 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2799 break;
2802 edit_right_char_move_cmd (edit);
2803 break;
2804 case CK_Begin_Page:
2805 case CK_Begin_Page_Highlight:
2806 edit_begin_page (edit);
2807 break;
2808 case CK_End_Page:
2809 case CK_End_Page_Highlight:
2810 edit_end_page (edit);
2811 break;
2812 case CK_Word_Left:
2813 case CK_Word_Left_Highlight:
2814 edit->over_col = 0;
2815 edit_left_word_move_cmd (edit);
2816 break;
2817 case CK_Word_Right:
2818 case CK_Word_Right_Highlight:
2819 edit->over_col = 0;
2820 edit_right_word_move_cmd (edit);
2821 break;
2822 case CK_Up_Alt_Highlight:
2823 column_highlighting = 1;
2824 case CK_Up:
2825 case CK_Up_Highlight:
2826 edit_move_up (edit, 1, 0);
2827 break;
2828 case CK_Down_Alt_Highlight:
2829 column_highlighting = 1;
2830 case CK_Down:
2831 case CK_Down_Highlight:
2832 edit_move_down (edit, 1, 0);
2833 break;
2834 case CK_Paragraph_Up_Alt_Highlight:
2835 column_highlighting = 1;
2836 case CK_Paragraph_Up:
2837 case CK_Paragraph_Up_Highlight:
2838 edit_move_up_paragraph (edit, 0);
2839 break;
2840 case CK_Paragraph_Down_Alt_Highlight:
2841 column_highlighting = 1;
2842 case CK_Paragraph_Down:
2843 case CK_Paragraph_Down_Highlight:
2844 edit_move_down_paragraph (edit, 0);
2845 break;
2846 case CK_Scroll_Up_Alt_Highlight:
2847 column_highlighting = 1;
2848 case CK_Scroll_Up:
2849 case CK_Scroll_Up_Highlight:
2850 edit_move_up (edit, 1, 1);
2851 break;
2852 case CK_Scroll_Down_Alt_Highlight:
2853 column_highlighting = 1;
2854 case CK_Scroll_Down:
2855 case CK_Scroll_Down_Highlight:
2856 edit_move_down (edit, 1, 1);
2857 break;
2858 case CK_Home:
2859 case CK_Home_Highlight:
2860 edit_cursor_to_bol (edit);
2861 break;
2862 case CK_End:
2863 case CK_End_Highlight:
2864 edit_cursor_to_eol (edit);
2865 break;
2866 case CK_Tab:
2867 /* if text marked shift block */
2868 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2869 edit_move_block_to_right (edit);
2870 } else {
2871 if ( option_cursor_beyond_eol )
2872 edit_insert_over (edit);
2873 edit_tab_cmd (edit);
2874 if (option_auto_para_formatting) {
2875 format_paragraph (edit, 0);
2876 edit->force |= REDRAW_PAGE;
2877 } else {
2878 check_and_wrap_line (edit);
2881 break;
2883 case CK_Toggle_Insert:
2884 edit->overwrite = (edit->overwrite == 0);
2885 break;
2887 case CK_Mark:
2888 if (edit->mark2 >= 0) {
2889 if (column_highlighting)
2890 edit_push_action (edit, COLUMN_ON);
2891 column_highlighting = 0;
2893 edit_mark_cmd (edit, 0);
2894 break;
2895 case CK_Column_Mark:
2896 if (!column_highlighting)
2897 edit_push_action (edit, COLUMN_OFF);
2898 column_highlighting = 1;
2899 edit_mark_cmd (edit, 0);
2900 break;
2901 case CK_Unmark:
2902 if (column_highlighting)
2903 edit_push_action (edit, COLUMN_ON);
2904 column_highlighting = 0;
2905 edit_mark_cmd (edit, 1);
2906 break;
2908 case CK_Toggle_Line_State:
2909 option_line_state = !option_line_state;
2910 if ( option_line_state ) {
2911 option_line_state_width = LINE_STATE_WIDTH;
2912 } else {
2913 option_line_state_width = 0;
2915 edit->force |= REDRAW_PAGE;
2916 break;
2918 case CK_Toggle_Bookmark:
2919 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2920 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2921 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2922 else
2923 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2924 break;
2925 case CK_Flush_Bookmarks:
2926 book_mark_flush (edit, BOOK_MARK_COLOR);
2927 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2928 edit->force |= REDRAW_PAGE;
2929 break;
2930 case CK_Next_Bookmark:
2931 if (edit->book_mark) {
2932 struct _book_mark *p;
2933 p = (struct _book_mark *) book_mark_find (edit,
2934 edit->curs_line);
2935 if (p->next) {
2936 p = p->next;
2937 if (p->line >= edit->start_line + edit->num_widget_lines
2938 || p->line < edit->start_line)
2939 edit_move_display (edit,
2940 p->line -
2941 edit->num_widget_lines / 2);
2942 edit_move_to_line (edit, p->line);
2945 break;
2946 case CK_Prev_Bookmark:
2947 if (edit->book_mark) {
2948 struct _book_mark *p;
2949 p = (struct _book_mark *) book_mark_find (edit,
2950 edit->curs_line);
2951 while (p->line == edit->curs_line)
2952 if (p->prev)
2953 p = p->prev;
2954 if (p->line >= 0) {
2955 if (p->line >= edit->start_line + edit->num_widget_lines
2956 || p->line < edit->start_line)
2957 edit_move_display (edit,
2958 p->line -
2959 edit->num_widget_lines / 2);
2960 edit_move_to_line (edit, p->line);
2963 break;
2965 case CK_Beginning_Of_Text:
2966 case CK_Beginning_Of_Text_Highlight:
2967 edit_move_to_top (edit);
2968 break;
2969 case CK_End_Of_Text:
2970 case CK_End_Of_Text_Highlight:
2971 edit_move_to_bottom (edit);
2972 break;
2974 case CK_Copy:
2975 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2976 edit_insert_over (edit);
2977 edit_block_copy_cmd (edit);
2978 break;
2979 case CK_Remove:
2980 edit_block_delete_cmd (edit);
2981 break;
2982 case CK_Move:
2983 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2984 edit_insert_over (edit);
2985 edit_block_move_cmd (edit);
2986 break;
2988 case CK_XStore:
2989 edit_copy_to_X_buf_cmd (edit);
2990 break;
2991 case CK_XCut:
2992 edit_cut_to_X_buf_cmd (edit);
2993 break;
2994 case CK_XPaste:
2995 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2996 edit_insert_over (edit);
2997 edit_paste_from_X_buf_cmd (edit);
2998 break;
2999 case CK_Selection_History:
3000 edit_paste_from_history (edit);
3001 break;
3003 case CK_Save_As:
3004 edit_save_as_cmd (edit);
3005 break;
3006 case CK_Save:
3007 edit_save_confirm_cmd (edit);
3008 break;
3009 case CK_Load:
3010 edit_load_cmd (edit, EDIT_FILE_COMMON);
3011 break;
3012 case CK_Save_Block:
3013 edit_save_block_cmd (edit);
3014 break;
3015 case CK_Insert_File:
3016 edit_insert_file_cmd (edit);
3017 break;
3019 case CK_Load_Prev_File:
3020 edit_load_back_cmd (edit);
3021 break;
3022 case CK_Load_Next_File:
3023 edit_load_forward_cmd (edit);
3024 break;
3026 case CK_Load_Syntax_File:
3027 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3028 break;
3029 case CK_Load_Menu_File:
3030 edit_load_cmd (edit, EDIT_FILE_MENU);
3031 break;
3033 case CK_Toggle_Syntax:
3034 if ((option_syntax_highlighting ^= 1) == 1)
3035 edit_load_syntax (edit, NULL, option_syntax_type);
3036 edit->force |= REDRAW_PAGE;
3037 break;
3039 case CK_Toggle_Tab_TWS:
3040 enable_show_tabs_tws ^= 1;
3041 edit->force |= REDRAW_PAGE;
3042 break;
3044 case CK_Find:
3045 edit_search_cmd (edit, 0);
3046 break;
3047 case CK_Find_Again:
3048 edit_search_cmd (edit, 1);
3049 break;
3050 case CK_Replace:
3051 edit_replace_cmd (edit, 0);
3052 break;
3053 case CK_Replace_Again:
3054 edit_replace_cmd (edit, 1);
3055 break;
3056 case CK_Complete_Word:
3057 /* if text marked shift block */
3058 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3059 edit_move_block_to_left (edit);
3060 } else {
3061 edit_complete_word_cmd (edit);
3063 break;
3064 case CK_Find_Definition:
3065 edit_get_match_keyword_cmd (edit);
3066 break;
3068 case CK_Exit:
3069 dlg_stop (edit->widget.parent);
3070 break;
3071 case CK_New:
3072 edit_new_cmd (edit);
3073 break;
3075 case CK_Help:
3076 edit_help_cmd (edit);
3077 break;
3079 case CK_Refresh:
3080 edit_refresh_cmd (edit);
3081 break;
3083 case CK_Date:{
3084 char s[1024];
3085 /* fool gcc to prevent a Y2K warning */
3086 char time_format[] = "_c";
3087 time_format[0] = '%';
3089 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3090 edit_print_string (edit, s);
3091 edit->force |= REDRAW_PAGE;
3092 break;
3094 case CK_Goto:
3095 edit_goto_cmd (edit);
3096 break;
3097 case CK_Paragraph_Format:
3098 format_paragraph (edit, 1);
3099 edit->force |= REDRAW_PAGE;
3100 break;
3101 case CK_Delete_Macro:
3102 edit_delete_macro_cmd (edit);
3103 break;
3104 case CK_Match_Bracket:
3105 edit_goto_matching_bracket (edit);
3106 break;
3107 case CK_User_Menu:
3108 user_menu (edit);
3109 break;
3110 case CK_Sort:
3111 edit_sort_cmd (edit);
3112 break;
3113 case CK_ExtCmd:
3114 edit_ext_cmd (edit);
3115 break;
3116 case CK_Mail:
3117 edit_mail_dialog (edit);
3118 break;
3119 case CK_Shell:
3120 view_other_cmd ();
3121 break;
3122 case CK_Select_Codepage:
3123 edit_select_codepage_cmd (edit);
3124 break;
3125 case CK_Insert_Literal:
3126 edit_insert_literal_cmd (edit);
3127 break;
3128 case CK_Execute_Macro:
3129 edit_execute_macro_cmd (edit);
3130 break;
3131 case CK_Begin_End_Macro:
3132 edit_begin_end_macro_cmd (edit);
3133 break;
3134 case CK_Ext_Mode:
3135 edit->extmod = 1;
3136 break;
3137 default:
3138 break;
3141 /* CK_Pipe_Block */
3142 if ((command / 1000) == 1) /* a shell command */
3143 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3144 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3145 struct macro m[MAX_MACRO_LENGTH];
3146 int nm;
3147 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3148 edit_execute_macro (edit, m, nm);
3151 /* keys which must set the col position, and the search vars */
3152 switch (command) {
3153 case CK_Find:
3154 case CK_Find_Again:
3155 case CK_Replace:
3156 case CK_Replace_Again:
3157 case CK_Complete_Word:
3158 edit->prev_col = edit_get_col (edit);
3159 break;
3160 case CK_Up:
3161 case CK_Up_Highlight:
3162 case CK_Up_Alt_Highlight:
3163 case CK_Down:
3164 case CK_Down_Highlight:
3165 case CK_Down_Alt_Highlight:
3166 case CK_Page_Up:
3167 case CK_Page_Up_Highlight:
3168 case CK_Page_Up_Alt_Highlight:
3169 case CK_Page_Down:
3170 case CK_Page_Down_Highlight:
3171 case CK_Page_Down_Alt_Highlight:
3172 case CK_Beginning_Of_Text:
3173 case CK_Beginning_Of_Text_Highlight:
3174 case CK_End_Of_Text:
3175 case CK_End_Of_Text_Highlight:
3176 case CK_Paragraph_Up:
3177 case CK_Paragraph_Up_Highlight:
3178 case CK_Paragraph_Up_Alt_Highlight:
3179 case CK_Paragraph_Down:
3180 case CK_Paragraph_Down_Highlight:
3181 case CK_Paragraph_Down_Alt_Highlight:
3182 case CK_Scroll_Up:
3183 case CK_Scroll_Up_Highlight:
3184 case CK_Scroll_Up_Alt_Highlight:
3185 case CK_Scroll_Down:
3186 case CK_Scroll_Down_Highlight:
3187 case CK_Scroll_Down_Alt_Highlight:
3188 edit->search_start = edit->curs1;
3189 edit->found_len = 0;
3190 break;
3191 default:
3192 edit->found_len = 0;
3193 edit->prev_col = edit_get_col (edit);
3194 edit->search_start = edit->curs1;
3196 edit_find_bracket (edit);
3198 if (option_auto_para_formatting) {
3199 switch (command) {
3200 case CK_BackSpace:
3201 case CK_Delete:
3202 case CK_Delete_Word_Left:
3203 case CK_Delete_Word_Right:
3204 case CK_Delete_To_Line_End:
3205 case CK_Delete_To_Line_Begin:
3206 format_paragraph (edit, 0);
3207 edit->force |= REDRAW_PAGE;
3213 static void
3214 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3216 int i = 0;
3218 if (edit->macro_depth++ > 256) {
3219 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3220 edit->macro_depth--;
3221 return;
3223 edit->force |= REDRAW_PAGE;
3224 for (; i < n; i++) {
3225 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3227 edit_update_screen (edit);
3228 edit->macro_depth--;
3231 /* User edit menu, like user menu (F2) but only in editor. */
3232 static void
3233 user_menu (WEdit * edit)
3235 FILE *fd;
3236 int nomark;
3237 struct stat status;
3238 long start_mark, end_mark;
3239 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3240 int rc = 0;
3242 nomark = eval_marks (edit, &start_mark, &end_mark);
3243 if (!nomark) /* remember marked or not */
3244 edit_save_block (edit, block_file, start_mark, end_mark);
3246 /* run shell scripts from menu */
3247 user_menu_cmd (edit);
3249 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3250 /* no block messages */
3251 goto cleanup;
3254 if (!nomark) {
3255 /* i.e. we have marked block */
3256 rc = edit_block_delete_cmd (edit);
3259 if (!rc) {
3260 edit_insert_file (edit, block_file);
3263 /* truncate block file */
3264 if ((fd = fopen (block_file, "w"))) {
3265 fclose (fd);
3268 edit_refresh_cmd (edit);
3269 edit->force |= REDRAW_COMPLETELY;
3271 cleanup:
3272 g_free (block_file);
3275 void
3276 edit_stack_init (void)
3278 for (edit_stack_iterator = 0;
3279 edit_stack_iterator < MAX_HISTORY_MOVETO;
3280 edit_stack_iterator++ ) {
3281 edit_history_moveto[edit_stack_iterator].filename = NULL;
3282 edit_history_moveto[edit_stack_iterator].line = -1;
3285 edit_stack_iterator = 0;
3288 void
3289 edit_stack_free (void)
3291 for (edit_stack_iterator = 0;
3292 edit_stack_iterator < MAX_HISTORY_MOVETO;
3293 edit_stack_iterator++)
3294 g_free (edit_history_moveto[edit_stack_iterator].filename);