Ticket #1650
[midnight-commander.git] / edit / edit.c
blobd365ea54273331d1f75c00d8825c0dfd57df3727
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 = default_editor_keymap;
726 if (editor_keymap && editor_keymap->len > 0)
727 edit->user_map = (global_key_map_t *) editor_keymap->data;
731 #define space_width 1
734 * Fill in the edit structure. Return NULL on failure. Pass edit as
735 * NULL to allocate a new structure.
737 * If line is 0, try to restore saved position. Otherwise put the
738 * cursor on that line and show it in the middle of the screen.
740 WEdit *
741 edit_init (WEdit *edit, int lines, int columns, const char *filename,
742 long line)
744 int to_free = 0;
745 option_auto_syntax = 1; /* Resetting to auto on every invokation */
746 if ( option_line_state ) {
747 option_line_state_width = LINE_STATE_WIDTH;
748 } else {
749 option_line_state_width = 0;
751 if (!edit) {
752 #ifdef ENABLE_NLS
754 * Expand option_whole_chars_search by national letters using
755 * current locale
758 static char option_whole_chars_search_buf[256];
760 if (option_whole_chars_search_buf != option_whole_chars_search) {
761 size_t i;
762 size_t len = str_term_width1 (option_whole_chars_search);
764 strcpy (option_whole_chars_search_buf,
765 option_whole_chars_search);
767 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
768 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
769 option_whole_chars_search_buf[len++] = i;
773 option_whole_chars_search_buf[len] = 0;
774 option_whole_chars_search = option_whole_chars_search_buf;
776 #endif /* ENABLE_NLS */
777 edit = g_malloc0 (sizeof (WEdit));
778 edit->search = NULL;
779 to_free = 1;
781 edit_purge_widget (edit);
782 edit->num_widget_lines = lines;
783 edit->over_col = 0;
784 edit->num_widget_columns = columns;
785 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
786 edit->stat1.st_uid = getuid ();
787 edit->stat1.st_gid = getgid ();
788 edit->stat1.st_mtime = 0;
789 edit->bracket = -1;
790 edit->force |= REDRAW_PAGE;
791 edit_set_filename (edit, filename);
792 edit->stack_size = START_STACK_SIZE;
793 edit->stack_size_mask = START_STACK_SIZE - 1;
794 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
795 if (edit_load_file (edit)) {
796 /* edit_load_file already gives an error message */
797 if (to_free)
798 g_free (edit);
799 return 0;
801 edit->utf8 = 0;
802 edit->converter = str_cnv_from_term;
803 #ifdef HAVE_CHARSET
804 const char *cp_id = NULL;
805 cp_id = get_codepage_id (source_codepage >= 0 ?
806 source_codepage : display_codepage);
808 if (cp_id != NULL) {
809 GIConv conv;
810 conv = str_crt_conv_from (cp_id);
811 if (conv != INVALID_CONV) {
812 if (edit->converter != str_cnv_from_term)
813 str_close_conv (edit->converter);
814 edit->converter = conv;
817 if (cp_id != NULL)
818 edit->utf8 = str_isutf8 (cp_id);
819 #endif
821 edit->loading_done = 1;
822 edit->modified = 0;
823 edit->locked = 0;
824 edit_load_syntax (edit, 0, 0);
826 int color;
827 edit_get_syntax_color (edit, -1, &color);
830 /* load saved cursor position */
831 if ((line == 0) && option_save_position) {
832 edit_load_position (edit);
833 } else {
834 if (line <= 0)
835 line = 1;
836 edit_move_display (edit, line - 1);
837 edit_move_to_line (edit, line - 1);
840 edit_set_keymap (edit);
842 return edit;
845 /* Clear the edit struct, freeing everything in it. Return 1 on success */
847 edit_clean (WEdit *edit)
849 int j = 0;
851 if (!edit)
852 return 0;
854 /* a stale lock, remove it */
855 if (edit->locked)
856 edit->locked = edit_unlock_file (edit->filename);
858 /* save cursor position */
859 if (option_save_position)
860 edit_save_position (edit);
862 /* File specified on the mcedit command line and never saved */
863 if (edit->delete_file)
864 unlink (edit->filename);
866 edit_free_syntax_rules (edit);
867 book_mark_flush (edit, -1);
868 for (; j <= MAXBUFF; j++) {
869 g_free (edit->buffers1[j]);
870 g_free (edit->buffers2[j]);
873 g_free (edit->undo_stack);
874 g_free (edit->filename);
875 g_free (edit->dir);
877 if (edit->search)
879 mc_search_free(edit->search);
880 edit->search = NULL;
882 edit_purge_widget (edit);
884 return 1;
888 /* returns 1 on success */
889 int edit_renew (WEdit * edit)
891 int lines = edit->num_widget_lines;
892 int columns = edit->num_widget_columns;
893 int retval = 1;
895 edit_clean (edit);
896 if (!edit_init (edit, lines, columns, "", 0))
897 retval = 0;
898 return retval;
902 * Load a new file into the editor. If it fails, preserve the old file.
903 * To do it, allocate a new widget, initialize it and, if the new file
904 * was loaded, copy the data to the old widget.
905 * Return 1 on success, 0 on failure.
908 edit_reload (WEdit *edit, const char *filename)
910 WEdit *e;
911 int lines = edit->num_widget_lines;
912 int columns = edit->num_widget_columns;
914 e = g_malloc0 (sizeof (WEdit));
915 e->widget = edit->widget;
916 if (!edit_init (e, lines, columns, filename, 0)) {
917 g_free (e);
918 return 0;
920 edit_clean (edit);
921 memcpy (edit, e, sizeof (WEdit));
922 g_free (e);
923 return 1;
927 * Load a new file into the editor and set line. If it fails, preserve the old file.
928 * To do it, allocate a new widget, initialize it and, if the new file
929 * was loaded, copy the data to the old widget.
930 * Return 1 on success, 0 on failure.
933 edit_reload_line (WEdit *edit, const char *filename, long line)
935 WEdit *e;
936 int lines = edit->num_widget_lines;
937 int columns = edit->num_widget_columns;
939 e = g_malloc0 (sizeof (WEdit));
940 e->widget = edit->widget;
941 if (!edit_init (e, lines, columns, filename, line)) {
942 g_free (e);
943 return 0;
945 edit_clean (edit);
946 memcpy (edit, e, sizeof (WEdit));
947 g_free (e);
948 return 1;
953 Recording stack for undo:
954 The following is an implementation of a compressed stack. Identical
955 pushes are recorded by a negative prefix indicating the number of times the
956 same char was pushed. This saves space for repeated curs-left or curs-right
957 delete etc.
961 pushed: stored:
965 b -3
967 c --> -4
973 If the stack long int is 0-255 it represents a normal insert (from a backspace),
974 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
975 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
976 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
977 position.
979 The only way the cursor moves or the buffer is changed is through the routines:
980 insert, backspace, insert_ahead, delete, and cursor_move.
981 These record the reverse undo movements onto the stack each time they are
982 called.
984 Each key press results in a set of actions (insert; delete ...). So each time
985 a key is pressed the current position of start_display is pushed as
986 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
987 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
988 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
992 void edit_push_action (WEdit * edit, long c,...)
994 unsigned long sp = edit->stack_pointer;
995 unsigned long spm1;
996 long *t;
998 /* first enlarge the stack if necessary */
999 if (sp > edit->stack_size - 10) { /* say */
1000 if (option_max_undo < 256)
1001 option_max_undo = 256;
1002 if (edit->stack_size < (unsigned long) option_max_undo) {
1003 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1004 if (t) {
1005 edit->undo_stack = t;
1006 edit->stack_size <<= 1;
1007 edit->stack_size_mask = edit->stack_size - 1;
1011 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1012 if (edit->stack_disable)
1013 return;
1015 #ifdef FAST_MOVE_CURSOR
1016 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1017 va_list ap;
1018 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1019 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1020 va_start (ap, c);
1021 c = -(va_arg (ap, int));
1022 va_end (ap);
1023 } else
1024 #endif /* ! FAST_MOVE_CURSOR */
1025 if (edit->stack_bottom != sp
1026 && spm1 != edit->stack_bottom
1027 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1028 int d;
1029 if (edit->undo_stack[spm1] < 0) {
1030 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1031 if (d == c) {
1032 if (edit->undo_stack[spm1] > -1000000000) {
1033 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1034 edit->undo_stack[spm1]--;
1035 return;
1038 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1039 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1040 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1041 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1042 if (edit->undo_stack[spm1] == -2)
1043 edit->stack_pointer = spm1;
1044 else
1045 edit->undo_stack[spm1]++;
1046 return;
1048 #endif
1049 } else {
1050 d = edit->undo_stack[spm1];
1051 if (d == c) {
1052 if (c >= KEY_PRESS)
1053 return; /* --> no need to push multiple do-nothings */
1054 edit->undo_stack[sp] = -2;
1055 goto check_bottom;
1057 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1058 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1059 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1060 edit->stack_pointer = spm1;
1061 return;
1063 #endif
1066 edit->undo_stack[sp] = c;
1067 check_bottom:
1069 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1071 /* if the sp wraps round and catches the stack_bottom then erase
1072 * the first set of actions on the stack to make space - by moving
1073 * stack_bottom forward one "key press" */
1074 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1075 if ((unsigned long) c == edit->stack_bottom ||
1076 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1077 do {
1078 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1079 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1081 /*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: */
1082 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1083 edit->stack_bottom = edit->stack_pointer = 0;
1087 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1088 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1090 static long
1091 pop_action (WEdit * edit)
1093 long c;
1094 unsigned long sp = edit->stack_pointer;
1095 if (sp == edit->stack_bottom) {
1096 return STACK_BOTTOM;
1098 sp = (sp - 1) & edit->stack_size_mask;
1099 if ((c = edit->undo_stack[sp]) >= 0) {
1100 /* edit->undo_stack[sp] = '@'; */
1101 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1102 return c;
1104 if (sp == edit->stack_bottom) {
1105 return STACK_BOTTOM;
1107 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1108 if (edit->undo_stack[sp] == -2) {
1109 /* edit->undo_stack[sp] = '@'; */
1110 edit->stack_pointer = sp;
1111 } else
1112 edit->undo_stack[sp]++;
1114 return c;
1117 /* is called whenever a modification is made by one of the four routines below */
1118 static void edit_modification (WEdit * edit)
1120 edit->caches_valid = 0;
1121 edit->screen_modified = 1;
1123 /* raise lock when file modified */
1124 if (!edit->modified && !edit->delete_file)
1125 edit->locked = edit_lock_file (edit->filename);
1126 edit->modified = 1;
1130 Basic low level single character buffer alterations and movements at the cursor.
1131 Returns char passed over, inserted or removed.
1134 void
1135 edit_insert (WEdit *edit, int c)
1137 /* check if file has grown to large */
1138 if (edit->last_byte >= SIZE_LIMIT)
1139 return;
1141 /* first we must update the position of the display window */
1142 if (edit->curs1 < edit->start_display) {
1143 edit->start_display++;
1144 if (c == '\n')
1145 edit->start_line++;
1148 /* Mark file as modified, unless the file hasn't been fully loaded */
1149 if (edit->loading_done) {
1150 edit_modification (edit);
1153 /* now we must update some info on the file and check if a redraw is required */
1154 if (c == '\n') {
1155 if (edit->book_mark)
1156 book_mark_inc (edit, edit->curs_line);
1157 edit->curs_line++;
1158 edit->total_lines++;
1159 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1162 /* save the reverse command onto the undo stack */
1163 edit_push_action (edit, BACKSPACE);
1165 /* update markers */
1166 edit->mark1 += (edit->mark1 > edit->curs1);
1167 edit->mark2 += (edit->mark2 > edit->curs1);
1168 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1170 /* add a new buffer if we've reached the end of the last one */
1171 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1172 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1173 g_malloc (EDIT_BUF_SIZE);
1175 /* perform the insertion */
1176 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1177 curs1 & M_EDIT_BUF_SIZE]
1178 = (unsigned char) c;
1180 /* update file length */
1181 edit->last_byte++;
1183 /* update cursor position */
1184 edit->curs1++;
1187 void
1188 edit_insert_over (WEdit * edit)
1190 int i;
1192 for ( i = 0; i < edit->over_col; i++ ) {
1193 edit_insert (edit, ' ');
1195 edit->over_col = 0;
1198 /* same as edit_insert and move left */
1199 void edit_insert_ahead (WEdit * edit, int c)
1201 if (edit->last_byte >= SIZE_LIMIT)
1202 return;
1203 if (edit->curs1 < edit->start_display) {
1204 edit->start_display++;
1205 if (c == '\n')
1206 edit->start_line++;
1208 edit_modification (edit);
1209 if (c == '\n') {
1210 if (edit->book_mark)
1211 book_mark_inc (edit, edit->curs_line);
1212 edit->total_lines++;
1213 edit->force |= REDRAW_AFTER_CURSOR;
1215 edit_push_action (edit, DELCHAR);
1217 edit->mark1 += (edit->mark1 >= edit->curs1);
1218 edit->mark2 += (edit->mark2 >= edit->curs1);
1219 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1221 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1222 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1223 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1225 edit->last_byte++;
1226 edit->curs2++;
1230 int edit_delete (WEdit * edit, const int byte_delete)
1232 int p = 0;
1233 int cw = 1;
1234 int i;
1236 if (!edit->curs2)
1237 return 0;
1239 edit->mark1 -= (edit->mark1 > edit->curs1);
1240 edit->mark2 -= (edit->mark2 > edit->curs1);
1241 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1243 cw = 1;
1244 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1245 if ( edit->utf8 && byte_delete == 0 ) {
1246 edit_get_utf (edit, edit->curs1, &cw);
1247 if ( cw < 1 )
1248 cw = 1;
1250 for ( i = 1; i<= cw; i++ ) {
1251 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1253 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1254 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1255 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1257 edit->last_byte--;
1258 edit->curs2--;
1259 edit_push_action (edit, p + 256);
1262 edit_modification (edit);
1263 if (p == '\n') {
1264 if (edit->book_mark)
1265 book_mark_dec (edit, edit->curs_line);
1266 edit->total_lines--;
1267 edit->force |= REDRAW_AFTER_CURSOR;
1269 if (edit->curs1 < edit->start_display) {
1270 edit->start_display--;
1271 if (p == '\n')
1272 edit->start_line--;
1275 return p;
1279 static int
1280 edit_backspace (WEdit * edit, const int byte_delete)
1282 int p = 0;
1283 int cw = 1;
1284 int i;
1286 if (!edit->curs1)
1287 return 0;
1289 edit->mark1 -= (edit->mark1 >= edit->curs1);
1290 edit->mark2 -= (edit->mark2 >= edit->curs1);
1291 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1293 cw = 1;
1294 if ( edit->utf8 && byte_delete == 0 ) {
1295 edit_get_prev_utf (edit, edit->curs1, &cw);
1296 if ( cw < 1 )
1297 cw = 1;
1299 for ( i = 1; i<= cw; i++ ) {
1300 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1301 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1302 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1303 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1305 edit->last_byte--;
1306 edit->curs1--;
1307 edit_push_action (edit, p);
1309 edit_modification (edit);
1310 if (p == '\n') {
1311 if (edit->book_mark)
1312 book_mark_dec (edit, edit->curs_line);
1313 edit->curs_line--;
1314 edit->total_lines--;
1315 edit->force |= REDRAW_AFTER_CURSOR;
1318 if (edit->curs1 < edit->start_display) {
1319 edit->start_display--;
1320 if (p == '\n')
1321 edit->start_line--;
1324 return p;
1327 #ifdef FAST_MOVE_CURSOR
1329 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1331 unsigned long next;
1332 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1333 edit->curs_line--;
1334 next -= (unsigned long) dest;
1335 n -= next;
1336 src += next;
1337 dest += next;
1342 edit_move_backward_lots (WEdit *edit, long increment)
1344 int r, s, t;
1345 unsigned char *p;
1347 if (increment > edit->curs1)
1348 increment = edit->curs1;
1349 if (increment <= 0)
1350 return -1;
1351 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1353 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1354 if (r > increment)
1355 r = increment;
1356 s = edit->curs1 & M_EDIT_BUF_SIZE;
1358 p = 0;
1359 if (s > r) {
1360 memqcpy (edit,
1361 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1362 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1364 } else {
1365 if (s) {
1366 memqcpy (edit,
1367 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1368 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1369 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1370 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1372 memqcpy (edit,
1373 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1374 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1375 EDIT_BUF_SIZE - (r - s), r - s);
1377 increment -= r;
1378 edit->curs1 -= r;
1379 edit->curs2 += r;
1380 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1381 if (p)
1382 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1383 else
1384 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1385 g_malloc (EDIT_BUF_SIZE);
1386 } else {
1387 g_free (p);
1390 s = edit->curs1 & M_EDIT_BUF_SIZE;
1391 while (increment) {
1392 p = 0;
1393 r = EDIT_BUF_SIZE;
1394 if (r > increment)
1395 r = increment;
1396 t = s;
1397 if (r < t)
1398 t = r;
1399 memqcpy (edit,
1400 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1401 EDIT_BUF_SIZE - t,
1402 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1404 if (r >= s) {
1405 if (t) {
1406 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1407 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1409 memqcpy (edit,
1410 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1411 EDIT_BUF_SIZE - r,
1412 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1413 EDIT_BUF_SIZE - (r - s), r - s);
1415 increment -= r;
1416 edit->curs1 -= r;
1417 edit->curs2 += r;
1418 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1419 if (p)
1420 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1421 else
1422 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1423 g_malloc (EDIT_BUF_SIZE);
1424 } else {
1425 g_free (p);
1428 return edit_get_byte (edit, edit->curs1);
1431 #endif /* ! FAST_MOVE_CURSOR */
1433 /* moves the cursor right or left: increment positive or negative respectively */
1434 void edit_cursor_move (WEdit * edit, long increment)
1436 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1437 int c;
1439 #ifdef FAST_MOVE_CURSOR
1440 if (increment < -256) {
1441 edit->force |= REDRAW_PAGE;
1442 edit_move_backward_lots (edit, -increment);
1443 return;
1445 #endif /* ! FAST_MOVE_CURSOR */
1447 if (increment < 0) {
1448 for (; increment < 0; increment++) {
1449 if (!edit->curs1)
1450 return;
1452 edit_push_action (edit, CURS_RIGHT);
1454 c = edit_get_byte (edit, edit->curs1 - 1);
1455 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1456 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1457 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1458 edit->curs2++;
1459 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1460 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1461 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1462 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1464 edit->curs1--;
1465 if (c == '\n') {
1466 edit->curs_line--;
1467 edit->force |= REDRAW_LINE_BELOW;
1471 } else if (increment > 0) {
1472 for (; increment > 0; increment--) {
1473 if (!edit->curs2)
1474 return;
1476 edit_push_action (edit, CURS_LEFT);
1478 c = edit_get_byte (edit, edit->curs1);
1479 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1480 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1481 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1482 edit->curs1++;
1483 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1484 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1485 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1486 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1488 edit->curs2--;
1489 if (c == '\n') {
1490 edit->curs_line++;
1491 edit->force |= REDRAW_LINE_ABOVE;
1497 /* These functions return positions relative to lines */
1499 /* returns index of last char on line + 1 */
1500 long edit_eol (WEdit * edit, long current)
1502 if (current < edit->last_byte) {
1503 for (;; current++)
1504 if (edit_get_byte (edit, current) == '\n')
1505 break;
1506 } else
1507 return edit->last_byte;
1508 return current;
1511 /* returns index of first char on line */
1512 long edit_bol (WEdit * edit, long current)
1514 if (current > 0) {
1515 for (;; current--)
1516 if (edit_get_byte (edit, current - 1) == '\n')
1517 break;
1518 } else
1519 return 0;
1520 return current;
1524 int edit_count_lines (WEdit * edit, long current, int upto)
1526 int lines = 0;
1527 if (upto > edit->last_byte)
1528 upto = edit->last_byte;
1529 if (current < 0)
1530 current = 0;
1531 while (current < upto)
1532 if (edit_get_byte (edit, current++) == '\n')
1533 lines++;
1534 return lines;
1538 /* If lines is zero this returns the count of lines from current to upto. */
1539 /* If upto is zero returns index of lines forward current. */
1540 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1542 if (upto) {
1543 return edit_count_lines (edit, current, upto);
1544 } else {
1545 int next;
1546 if (lines < 0)
1547 lines = 0;
1548 while (lines--) {
1549 next = edit_eol (edit, current) + 1;
1550 if (next > edit->last_byte)
1551 break;
1552 else
1553 current = next;
1555 return current;
1560 /* Returns offset of 'lines' lines up from current */
1561 long edit_move_backward (WEdit * edit, long current, int lines)
1563 if (lines < 0)
1564 lines = 0;
1565 current = edit_bol (edit, current);
1566 while((lines--) && current != 0)
1567 current = edit_bol (edit, current - 1);
1568 return current;
1571 /* If cols is zero this returns the count of columns from current to upto. */
1572 /* If upto is zero returns index of cols across from current. */
1573 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1575 long p, q;
1576 int col = 0;
1577 #ifdef HAVE_CHARSET
1578 int cw = 1;
1579 int utf_ch = 0;
1580 #endif
1581 if (upto) {
1582 q = upto;
1583 cols = -10;
1584 } else
1585 q = edit->last_byte + 2;
1586 for (col = 0, p = current; p < q; p++) {
1587 int c;
1588 #ifdef HAVE_CHARSET
1589 cw = 1;
1590 utf_ch = 0;
1591 #endif
1592 if (cols != -10) {
1593 if (col == cols)
1594 return p;
1595 if (col > cols)
1596 return p - 1;
1598 #ifdef HAVE_CHARSET
1599 if ( !edit->utf8 ) {
1600 #endif
1601 c = edit_get_byte (edit, p);
1602 #ifdef HAVE_CHARSET
1603 } else {
1604 cw = 1;
1605 c = edit_get_byte (edit, p);
1606 utf_ch = edit_get_utf (edit, p, &cw);
1608 if ( utf8_display ) {
1609 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1610 col++;
1612 #endif
1613 if (c == '\t')
1614 col += TAB_SIZE - col % TAB_SIZE;
1615 else if (c == '\n') {
1616 if (upto)
1617 return col;
1618 else
1619 return p;
1620 } else if (c < 32 || c == 127)
1621 col += 2; /* Caret notation for control characters */
1622 else
1623 col++;
1624 #ifdef HAVE_CHARSET
1625 if ( cw > 1 )
1626 col -= cw-1;
1627 #endif
1629 return col;
1632 /* returns the current column position of the cursor */
1633 int edit_get_col (WEdit * edit)
1635 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1639 /* Scrolling functions */
1641 void edit_update_curs_row (WEdit * edit)
1643 edit->curs_row = edit->curs_line - edit->start_line;
1646 void edit_update_curs_col (WEdit * edit)
1648 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1652 edit_get_curs_col (const WEdit *edit)
1654 return edit->curs_col;
1657 /*moves the display start position up by i lines */
1658 void edit_scroll_upward (WEdit * edit, unsigned long i)
1660 unsigned long lines_above = edit->start_line;
1661 if (i > lines_above)
1662 i = lines_above;
1663 if (i) {
1664 edit->start_line -= i;
1665 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1666 edit->force |= REDRAW_PAGE;
1667 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1669 edit_update_curs_row (edit);
1673 /* returns 1 if could scroll, 0 otherwise */
1674 void edit_scroll_downward (WEdit * edit, int i)
1676 int lines_below;
1677 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1678 if (lines_below > 0) {
1679 if (i > lines_below)
1680 i = lines_below;
1681 edit->start_line += i;
1682 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1683 edit->force |= REDRAW_PAGE;
1684 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1686 edit_update_curs_row (edit);
1689 void edit_scroll_right (WEdit * edit, int i)
1691 edit->force |= REDRAW_PAGE;
1692 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1693 edit->start_col -= i;
1696 void edit_scroll_left (WEdit * edit, int i)
1698 if (edit->start_col) {
1699 edit->start_col += i;
1700 if (edit->start_col > 0)
1701 edit->start_col = 0;
1702 edit->force |= REDRAW_PAGE;
1703 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1707 /* high level cursor movement commands */
1709 static int is_in_indent (WEdit *edit)
1711 long p = edit_bol (edit, edit->curs1);
1712 while (p < edit->curs1)
1713 if (!strchr (" \t", edit_get_byte (edit, p++)))
1714 return 0;
1715 return 1;
1718 static int left_of_four_spaces (WEdit *edit);
1720 void
1721 edit_move_to_prev_col (WEdit * edit, long p)
1723 int prev = edit->prev_col;
1724 int over = edit->over_col;
1726 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1728 if (option_cursor_beyond_eol) {
1729 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1731 if (line_len < prev + edit->over_col) {
1732 edit->over_col = prev + over - line_len;
1733 edit->prev_col = line_len;
1734 edit->curs_col = line_len;
1735 } else {
1736 edit->curs_col = prev + over;
1737 edit->prev_col = edit->curs_col;
1738 edit->over_col = 0;
1740 } else {
1741 edit->over_col = 0;
1742 if (is_in_indent (edit) && option_fake_half_tabs) {
1743 edit_update_curs_col (edit);
1744 if (space_width)
1745 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1746 int q = edit->curs_col;
1747 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1748 p = edit_bol (edit, edit->curs1);
1749 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1750 if (!left_of_four_spaces (edit))
1751 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1757 /* move i lines */
1758 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1760 unsigned long p, l = edit->curs_line;
1762 if (i > l)
1763 i = l;
1764 if (i) {
1765 if (i > 1)
1766 edit->force |= REDRAW_PAGE;
1767 if (scroll)
1768 edit_scroll_upward (edit, i);
1770 p = edit_bol (edit, edit->curs1);
1771 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1772 edit_move_to_prev_col (edit, p);
1774 edit->search_start = edit->curs1;
1775 edit->found_len = 0;
1779 static int
1780 is_blank (WEdit *edit, long offset)
1782 long s, f;
1783 int c;
1784 s = edit_bol (edit, offset);
1785 f = edit_eol (edit, offset) - 1;
1786 while (s <= f) {
1787 c = edit_get_byte (edit, s++);
1788 if (!isspace (c))
1789 return 0;
1791 return 1;
1795 /* returns the offset of line i */
1796 static long
1797 edit_find_line (WEdit *edit, int line)
1799 int i, j = 0;
1800 int m = 2000000000;
1801 if (!edit->caches_valid) {
1802 for (i = 0; i < N_LINE_CACHES; i++)
1803 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1804 /* three offsets that we *know* are line 0 at 0 and these two: */
1805 edit->line_numbers[1] = edit->curs_line;
1806 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1807 edit->line_numbers[2] = edit->total_lines;
1808 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1809 edit->caches_valid = 1;
1811 if (line >= edit->total_lines)
1812 return edit->line_offsets[2];
1813 if (line <= 0)
1814 return 0;
1815 /* find the closest known point */
1816 for (i = 0; i < N_LINE_CACHES; i++) {
1817 int n;
1818 n = abs (edit->line_numbers[i] - line);
1819 if (n < m) {
1820 m = n;
1821 j = i;
1824 if (m == 0)
1825 return edit->line_offsets[j]; /* know the offset exactly */
1826 if (m == 1 && j >= 3)
1827 i = j; /* one line different - caller might be looping, so stay in this cache */
1828 else
1829 i = 3 + (rand () % (N_LINE_CACHES - 3));
1830 if (line > edit->line_numbers[j])
1831 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1832 else
1833 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1834 edit->line_numbers[i] = line;
1835 return edit->line_offsets[i];
1838 int line_is_blank (WEdit * edit, long line)
1840 return is_blank (edit, edit_find_line (edit, line));
1843 /* moves up until a blank line is reached, or until just
1844 before a non-blank line is reached */
1845 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1847 int i;
1848 if (edit->curs_line <= 1) {
1849 i = 0;
1850 } else {
1851 if (line_is_blank (edit, edit->curs_line)) {
1852 if (line_is_blank (edit, edit->curs_line - 1)) {
1853 for (i = edit->curs_line - 1; i; i--)
1854 if (!line_is_blank (edit, i)) {
1855 i++;
1856 break;
1858 } else {
1859 for (i = edit->curs_line - 1; i; i--)
1860 if (line_is_blank (edit, i))
1861 break;
1863 } else {
1864 for (i = edit->curs_line - 1; i; i--)
1865 if (line_is_blank (edit, i))
1866 break;
1869 edit_move_up (edit, edit->curs_line - i, scroll);
1872 /* move i lines */
1873 void edit_move_down (WEdit * edit, int i, int scroll)
1875 long p, l = edit->total_lines - edit->curs_line;
1877 if (i > l)
1878 i = l;
1879 if (i) {
1880 if (i > 1)
1881 edit->force |= REDRAW_PAGE;
1882 if (scroll)
1883 edit_scroll_downward (edit, i);
1884 p = edit_bol (edit, edit->curs1);
1885 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1886 edit_move_to_prev_col (edit, p);
1888 edit->search_start = edit->curs1;
1889 edit->found_len = 0;
1893 /* moves down until a blank line is reached, or until just
1894 before a non-blank line is reached */
1895 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1897 int i;
1898 if (edit->curs_line >= edit->total_lines - 1) {
1899 i = edit->total_lines;
1900 } else {
1901 if (line_is_blank (edit, edit->curs_line)) {
1902 if (line_is_blank (edit, edit->curs_line + 1)) {
1903 for (i = edit->curs_line + 1; i; i++)
1904 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1905 i--;
1906 break;
1908 } else {
1909 for (i = edit->curs_line + 1; i; i++)
1910 if (line_is_blank (edit, i) || i >= edit->total_lines)
1911 break;
1913 } else {
1914 for (i = edit->curs_line + 1; i; i++)
1915 if (line_is_blank (edit, i) || i >= edit->total_lines)
1916 break;
1919 edit_move_down (edit, i - edit->curs_line, scroll);
1922 static void edit_begin_page (WEdit *edit)
1924 edit_update_curs_row (edit);
1925 edit_move_up (edit, edit->curs_row, 0);
1928 static void edit_end_page (WEdit *edit)
1930 edit_update_curs_row (edit);
1931 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1935 /* goto beginning of text */
1936 static void edit_move_to_top (WEdit * edit)
1938 if (edit->curs_line) {
1939 edit_cursor_move (edit, -edit->curs1);
1940 edit_move_to_prev_col (edit, 0);
1941 edit->force |= REDRAW_PAGE;
1942 edit->search_start = 0;
1943 edit_update_curs_row(edit);
1948 /* goto end of text */
1949 static void edit_move_to_bottom (WEdit * edit)
1951 if (edit->curs_line < edit->total_lines) {
1952 edit_cursor_move (edit, edit->curs2);
1953 edit->start_display = edit->last_byte;
1954 edit->start_line = edit->total_lines;
1955 edit_update_curs_row(edit);
1956 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1957 edit->force |= REDRAW_PAGE;
1961 /* goto beginning of line */
1962 static void edit_cursor_to_bol (WEdit * edit)
1964 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1965 edit->search_start = edit->curs1;
1966 edit->prev_col = edit_get_col (edit);
1967 edit->over_col = 0;
1970 /* goto end of line */
1971 static void edit_cursor_to_eol (WEdit * edit)
1973 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1974 edit->search_start = edit->curs1;
1975 edit->prev_col = edit_get_col (edit);
1976 edit->over_col = 0;
1979 /* move cursor to line 'line' */
1980 void edit_move_to_line (WEdit * e, long line)
1982 if(line < e->curs_line)
1983 edit_move_up (e, e->curs_line - line, 0);
1984 else
1985 edit_move_down (e, line - e->curs_line, 0);
1986 edit_scroll_screen_over_cursor (e);
1989 /* scroll window so that first visible line is 'line' */
1990 void edit_move_display (WEdit * e, long line)
1992 if(line < e->start_line)
1993 edit_scroll_upward (e, e->start_line - line);
1994 else
1995 edit_scroll_downward (e, line - e->start_line);
1998 /* save markers onto undo stack */
1999 void edit_push_markers (WEdit * edit)
2001 edit_push_action (edit, MARK_1 + edit->mark1);
2002 edit_push_action (edit, MARK_2 + edit->mark2);
2005 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2007 edit->mark1 = m1;
2008 edit->mark2 = m2;
2009 edit->column1 = c1;
2010 edit->column2 = c2;
2014 /* highlight marker toggle */
2015 void edit_mark_cmd (WEdit * edit, int unmark)
2017 edit_push_markers (edit);
2018 if (unmark) {
2019 edit_set_markers (edit, 0, 0, 0, 0);
2020 edit->force |= REDRAW_PAGE;
2021 } else {
2022 if (edit->mark2 >= 0) {
2023 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
2024 edit->force |= REDRAW_PAGE;
2025 } else
2026 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2030 static unsigned long
2031 my_type_of (int c)
2033 int x, r = 0;
2034 const char *p, *q;
2035 const char option_chars_move_whole_word[] =
2036 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2038 if (!c)
2039 return 0;
2040 if (c == '!') {
2041 if (*option_chars_move_whole_word == '!')
2042 return 2;
2043 return 0x80000000UL;
2045 if (g_ascii_isupper ((gchar) c))
2046 c = 'A';
2047 else if (g_ascii_islower ((gchar) c))
2048 c = 'a';
2049 else if (g_ascii_isalpha (c))
2050 c = 'a';
2051 else if (isdigit (c))
2052 c = '0';
2053 else if (isspace (c))
2054 c = ' ';
2055 q = strchr (option_chars_move_whole_word, c);
2056 if (!q)
2057 return 0xFFFFFFFFUL;
2058 do {
2059 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2060 if (*p == '!')
2061 x <<= 1;
2062 r |= x;
2063 } while ((q = strchr (q + 1, c)));
2064 return r;
2067 static void
2068 edit_left_word_move (WEdit *edit, int s)
2070 for (;;) {
2071 int c1, c2;
2072 edit_cursor_move (edit, -1);
2073 if (!edit->curs1)
2074 break;
2075 c1 = edit_get_byte (edit, edit->curs1 - 1);
2076 c2 = edit_get_byte (edit, edit->curs1);
2077 if (!(my_type_of (c1) & my_type_of (c2)))
2078 break;
2079 if (isspace (c1) && !isspace (c2))
2080 break;
2081 if (s)
2082 if (!isspace (c1) && isspace (c2))
2083 break;
2087 static void edit_left_word_move_cmd (WEdit * edit)
2089 edit_left_word_move (edit, 0);
2090 edit->force |= REDRAW_PAGE;
2093 static void
2094 edit_right_word_move (WEdit *edit, int s)
2096 for (;;) {
2097 int c1, c2;
2098 edit_cursor_move (edit, 1);
2099 if (edit->curs1 >= edit->last_byte)
2100 break;
2101 c1 = edit_get_byte (edit, edit->curs1 - 1);
2102 c2 = edit_get_byte (edit, edit->curs1);
2103 if (!(my_type_of (c1) & my_type_of (c2)))
2104 break;
2105 if (isspace (c1) && !isspace (c2))
2106 break;
2107 if (s)
2108 if (!isspace (c1) && isspace (c2))
2109 break;
2113 static void edit_right_word_move_cmd (WEdit * edit)
2115 edit_right_word_move (edit, 0);
2116 edit->force |= REDRAW_PAGE;
2119 static void edit_right_char_move_cmd (WEdit * edit)
2121 int cw = 1;
2122 int c = 0;
2123 if ( edit->utf8 ) {
2124 c = edit_get_utf (edit, edit->curs1, &cw);
2125 if ( cw < 1 )
2126 cw = 1;
2127 } else {
2128 c = edit_get_byte (edit, edit->curs1);
2130 if (option_cursor_beyond_eol && c == '\n') {
2131 edit->over_col++;
2132 } else {
2133 edit_cursor_move (edit, cw);
2137 static void edit_left_char_move_cmd (WEdit * edit)
2139 int cw = 1;
2140 if ( edit->utf8 ) {
2141 edit_get_prev_utf (edit, edit->curs1, &cw);
2142 if ( cw < 1 )
2143 cw = 1;
2145 if (option_cursor_beyond_eol && edit->over_col > 0) {
2146 edit->over_col--;
2147 } else {
2148 edit_cursor_move (edit, -cw);
2153 static void edit_right_delete_word (WEdit * edit)
2155 int c1, c2;
2156 for (;;) {
2157 if (edit->curs1 >= edit->last_byte)
2158 break;
2159 c1 = edit_delete (edit, 1);
2160 c2 = edit_get_byte (edit, edit->curs1);
2161 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2162 break;
2163 if (!(my_type_of (c1) & my_type_of (c2)))
2164 break;
2168 static void edit_left_delete_word (WEdit * edit)
2170 int c1, c2;
2171 for (;;) {
2172 if (edit->curs1 <= 0)
2173 break;
2174 c1 = edit_backspace (edit, 1);
2175 c2 = edit_get_byte (edit, edit->curs1 - 1);
2176 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2177 break;
2178 if (!(my_type_of (c1) & my_type_of (c2)))
2179 break;
2184 the start column position is not recorded, and hence does not
2185 undo as it happed. But who would notice.
2187 static void
2188 edit_do_undo (WEdit * edit)
2190 long ac;
2191 long count = 0;
2193 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2194 edit->over_col = 0;
2195 while ((ac = pop_action (edit)) < KEY_PRESS) {
2196 switch ((int) ac) {
2197 case STACK_BOTTOM:
2198 goto done_undo;
2199 case CURS_RIGHT:
2200 edit_cursor_move (edit, 1);
2201 break;
2202 case CURS_LEFT:
2203 edit_cursor_move (edit, -1);
2204 break;
2205 case BACKSPACE:
2206 edit_backspace (edit, 1);
2207 break;
2208 case DELCHAR:
2209 edit_delete (edit, 1);
2210 break;
2211 case COLUMN_ON:
2212 column_highlighting = 1;
2213 break;
2214 case COLUMN_OFF:
2215 column_highlighting = 0;
2216 break;
2218 if (ac >= 256 && ac < 512)
2219 edit_insert_ahead (edit, ac - 256);
2220 if (ac >= 0 && ac < 256)
2221 edit_insert (edit, ac);
2223 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2224 edit->mark1 = ac - MARK_1;
2225 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2226 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2227 edit->mark2 = ac - MARK_2;
2228 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2230 if (count++)
2231 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2234 if (edit->start_display > ac - KEY_PRESS) {
2235 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2236 edit->force |= REDRAW_PAGE;
2237 } else if (edit->start_display < ac - KEY_PRESS) {
2238 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2239 edit->force |= REDRAW_PAGE;
2241 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2242 edit_update_curs_row (edit);
2244 done_undo:;
2245 edit->stack_disable = 0;
2248 static void edit_delete_to_line_end (WEdit * edit)
2250 while (edit_get_byte (edit, edit->curs1) != '\n') {
2251 if (!edit->curs2)
2252 break;
2253 edit_delete (edit, 1);
2257 static void edit_delete_to_line_begin (WEdit * edit)
2259 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2260 if (!edit->curs1)
2261 break;
2262 edit_backspace (edit, 1);
2266 void
2267 edit_delete_line (WEdit *edit)
2270 * Delete right part of the line.
2271 * Note that edit_get_byte() returns '\n' when byte position is
2272 * beyond EOF.
2274 while (edit_get_byte (edit, edit->curs1) != '\n') {
2275 (void) edit_delete (edit, 1);
2279 * Delete '\n' char.
2280 * Note that edit_delete() will not corrupt anything if called while
2281 * cursor position is EOF.
2283 (void) edit_delete (edit, 1);
2286 * Delete left part of the line.
2287 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2289 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2290 (void) edit_backspace (edit, 1);
2294 void insert_spaces_tab (WEdit * edit, int half)
2296 int i;
2297 edit_update_curs_col (edit);
2298 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2299 while (i > 0) {
2300 edit_insert (edit, ' ');
2301 i -= space_width;
2305 static int is_aligned_on_a_tab (WEdit * edit)
2307 edit_update_curs_col (edit);
2308 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2309 return 0; /* not alligned on a tab */
2310 return 1;
2313 static int right_of_four_spaces (WEdit *edit)
2315 int i, ch = 0;
2316 for (i = 1; i <= HALF_TAB_SIZE; i++)
2317 ch |= edit_get_byte (edit, edit->curs1 - i);
2318 if (ch == ' ')
2319 return is_aligned_on_a_tab (edit);
2320 return 0;
2323 static int left_of_four_spaces (WEdit *edit)
2325 int i, ch = 0;
2326 for (i = 0; i < HALF_TAB_SIZE; i++)
2327 ch |= edit_get_byte (edit, edit->curs1 + i);
2328 if (ch == ' ')
2329 return is_aligned_on_a_tab (edit);
2330 return 0;
2333 int edit_indent_width (WEdit * edit, long p)
2335 long q = p;
2336 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2337 q++;
2338 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2341 void edit_insert_indent (WEdit * edit, int indent)
2343 if (!option_fill_tabs_with_spaces) {
2344 while (indent >= TAB_SIZE) {
2345 edit_insert (edit, '\t');
2346 indent -= TAB_SIZE;
2349 while (indent-- > 0)
2350 edit_insert (edit, ' ');
2353 static void
2354 edit_auto_indent (WEdit * edit)
2356 long p;
2357 char c;
2358 p = edit->curs1;
2359 /* use the previous line as a template */
2360 p = edit_move_backward (edit, p, 1);
2361 /* copy the leading whitespace of the line */
2362 for (;;) { /* no range check - the line _is_ \n-terminated */
2363 c = edit_get_byte (edit, p++);
2364 if (c != ' ' && c != '\t')
2365 break;
2366 edit_insert (edit, c);
2370 static void edit_double_newline (WEdit * edit)
2372 edit_insert (edit, '\n');
2373 if (edit_get_byte (edit, edit->curs1) == '\n')
2374 return;
2375 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2376 return;
2377 edit->force |= REDRAW_PAGE;
2378 edit_insert (edit, '\n');
2381 static void edit_tab_cmd (WEdit * edit)
2383 int i;
2385 if (option_fake_half_tabs) {
2386 if (is_in_indent (edit)) {
2387 /*insert a half tab (usually four spaces) unless there is a
2388 half tab already behind, then delete it and insert a
2389 full tab. */
2390 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2391 for (i = 1; i <= HALF_TAB_SIZE; i++)
2392 edit_backspace (edit, 1);
2393 edit_insert (edit, '\t');
2394 } else {
2395 insert_spaces_tab (edit, 1);
2397 return;
2400 if (option_fill_tabs_with_spaces) {
2401 insert_spaces_tab (edit, 0);
2402 } else {
2403 edit_insert (edit, '\t');
2405 return;
2408 static void check_and_wrap_line (WEdit * edit)
2410 int curs, c;
2411 if (!option_typewriter_wrap)
2412 return;
2413 edit_update_curs_col (edit);
2414 if (edit->curs_col < option_word_wrap_line_length)
2415 return;
2416 curs = edit->curs1;
2417 for (;;) {
2418 curs--;
2419 c = edit_get_byte (edit, curs);
2420 if (c == '\n' || curs <= 0) {
2421 edit_insert (edit, '\n');
2422 return;
2424 if (c == ' ' || c == '\t') {
2425 int current = edit->curs1;
2426 edit_cursor_move (edit, curs - edit->curs1 + 1);
2427 edit_insert (edit, '\n');
2428 edit_cursor_move (edit, current - edit->curs1 + 1);
2429 return;
2434 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2436 void edit_push_key_press (WEdit * edit)
2438 edit_push_action (edit, KEY_PRESS + edit->start_display);
2439 if (edit->mark2 == -1)
2440 edit_push_action (edit, MARK_1 + edit->mark1);
2443 /* this find the matching bracket in either direction, and sets edit->bracket */
2444 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2446 const char * const b = "{}{[][()(", *p;
2447 int i = 1, a, inc = -1, c, d, n = 0;
2448 unsigned long j = 0;
2449 long q;
2450 edit_update_curs_row (edit);
2451 c = edit_get_byte (edit, edit->curs1);
2452 p = strchr (b, c);
2453 /* no limit */
2454 if (!furthest_bracket_search)
2455 furthest_bracket_search--;
2456 /* not on a bracket at all */
2457 if (!p)
2458 return -1;
2459 /* the matching bracket */
2460 d = p[1];
2461 /* going left or right? */
2462 if (strchr ("{[(", c))
2463 inc = 1;
2464 for (q = edit->curs1 + inc;; q += inc) {
2465 /* out of buffer? */
2466 if (q >= edit->last_byte || q < 0)
2467 break;
2468 a = edit_get_byte (edit, q);
2469 /* don't want to eat CPU */
2470 if (j++ > furthest_bracket_search)
2471 break;
2472 /* out of screen? */
2473 if (in_screen) {
2474 if (q < edit->start_display)
2475 break;
2476 /* count lines if searching downward */
2477 if (inc > 0 && a == '\n')
2478 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2479 break;
2481 /* count bracket depth */
2482 i += (a == c) - (a == d);
2483 /* return if bracket depth is zero */
2484 if (!i)
2485 return q;
2487 /* no match */
2488 return -1;
2491 static long last_bracket = -1;
2493 void edit_find_bracket (WEdit * edit)
2495 edit->bracket = edit_get_bracket (edit, 1, 10000);
2496 if (last_bracket != edit->bracket)
2497 edit->force |= REDRAW_PAGE;
2498 last_bracket = edit->bracket;
2501 static void edit_goto_matching_bracket (WEdit *edit)
2503 long q;
2504 q = edit_get_bracket (edit, 0, 0);
2505 if (q < 0)
2506 return;
2507 edit->bracket = edit->curs1;
2508 edit->force |= REDRAW_PAGE;
2509 edit_cursor_move (edit, q - edit->curs1);
2513 * This executes a command as though the user initiated it through a key
2514 * press. Callback with WIDGET_KEY as a message calls this after
2515 * translating the key press. This function can be used to pass any
2516 * command to the editor. Note that the screen wouldn't update
2517 * automatically. Either of command or char_for_insertion must be
2518 * passed as -1. Commands are executed, and char_for_insertion is
2519 * inserted at the cursor.
2521 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2523 if (command == CK_Begin_Record_Macro) {
2524 edit->macro_i = 0;
2525 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2526 return;
2528 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2529 edit->force |= REDRAW_COMPLETELY;
2530 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2531 edit->macro_i = -1;
2532 return;
2534 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2535 edit->macro[edit->macro_i].command = command;
2536 edit->macro[edit->macro_i++].ch = char_for_insertion;
2538 /* record the beginning of a set of editing actions initiated by a key press */
2539 if (command != CK_Undo && command != CK_Ext_Mode)
2540 edit_push_key_press (edit);
2542 edit_execute_cmd (edit, command, char_for_insertion);
2543 if (column_highlighting)
2544 edit->force |= REDRAW_PAGE;
2547 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2550 This executes a command at a lower level than macro recording.
2551 It also does not push a key_press onto the undo stack. This means
2552 that if it is called many times, a single undo command will undo
2553 all of them. It also does not check for the Undo command.
2555 void
2556 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2558 edit->force |= REDRAW_LINE;
2560 /* The next key press will unhighlight the found string, so update
2561 * the whole page */
2562 if (edit->found_len || column_highlighting)
2563 edit->force |= REDRAW_PAGE;
2565 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2566 column_highlighting = 0;
2567 if (!edit->highlight
2568 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2569 edit_mark_cmd (edit, 1); /* clear */
2570 edit_mark_cmd (edit, 0); /* marking on */
2572 edit->highlight = 1;
2573 } else { /* any other command */
2574 if (edit->highlight)
2575 edit_mark_cmd (edit, 0); /* clear */
2576 edit->highlight = 0;
2579 /* first check for undo */
2580 if (command == CK_Undo) {
2581 edit_do_undo (edit);
2582 edit->found_len = 0;
2583 edit->prev_col = edit_get_col (edit);
2584 edit->search_start = edit->curs1;
2585 return;
2588 /* An ordinary key press */
2589 if (char_for_insertion >= 0) {
2590 if (edit->overwrite) {
2591 if (edit_get_byte (edit, edit->curs1) != '\n')
2592 edit_delete (edit, 0);
2594 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2595 edit_insert_over (edit);
2596 #ifdef HAVE_CHARSET
2597 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2598 unsigned char str[6 + 1];
2599 size_t i = 0;
2600 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2601 if ( res == 0 ) {
2602 str[0] = '.';
2603 str[1] = '\0';
2604 } else {
2605 str[res] = '\0';
2607 while ( str[i] != 0 && i<=6) {
2608 char_for_insertion = str[i];
2609 edit_insert (edit, char_for_insertion);
2610 i++;
2612 } else {
2613 #endif
2614 edit_insert (edit, char_for_insertion);
2615 #ifdef HAVE_CHARSET
2617 #endif
2618 if (option_auto_para_formatting) {
2619 format_paragraph (edit, 0);
2620 edit->force |= REDRAW_PAGE;
2621 } else
2622 check_and_wrap_line (edit);
2623 edit->found_len = 0;
2624 edit->prev_col = edit_get_col (edit);
2625 edit->search_start = edit->curs1;
2626 edit_find_bracket (edit);
2627 return;
2630 switch (command) {
2631 case CK_Begin_Page:
2632 case CK_End_Page:
2633 case CK_Begin_Page_Highlight:
2634 case CK_End_Page_Highlight:
2635 case CK_Word_Left:
2636 case CK_Word_Right:
2637 case CK_Up:
2638 case CK_Down:
2639 case CK_Left:
2640 case CK_Right:
2641 if ( edit->mark2 >= 0 ) {
2642 if ( !option_persistent_selections ) {
2643 if (column_highlighting)
2644 edit_push_action (edit, COLUMN_ON);
2645 column_highlighting = 0;
2646 edit_mark_cmd (edit, 1);
2651 switch (command) {
2652 case CK_Begin_Page:
2653 case CK_End_Page:
2654 case CK_Begin_Page_Highlight:
2655 case CK_End_Page_Highlight:
2656 case CK_Word_Left:
2657 case CK_Word_Right:
2658 case CK_Up:
2659 case CK_Down:
2660 case CK_Word_Left_Highlight:
2661 case CK_Word_Right_Highlight:
2662 case CK_Up_Highlight:
2663 case CK_Down_Highlight:
2664 case CK_Up_Alt_Highlight:
2665 case CK_Down_Alt_Highlight:
2666 if (edit->mark2 == -1)
2667 break; /*marking is following the cursor: may need to highlight a whole line */
2668 case CK_Left:
2669 case CK_Right:
2670 case CK_Left_Highlight:
2671 case CK_Right_Highlight:
2672 edit->force |= REDRAW_CHAR_ONLY;
2675 /* basic cursor key commands */
2676 switch (command) {
2677 case CK_BackSpace:
2678 /* if non persistent selection and text selected */
2679 if ( !option_persistent_selections ) {
2680 if ( edit->mark1 != edit->mark2 ) {
2681 edit_block_delete_cmd (edit);
2682 break;
2685 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2686 edit->over_col--;
2687 break;
2689 if (option_backspace_through_tabs && is_in_indent (edit)) {
2690 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2691 && edit->curs1 > 0)
2692 edit_backspace (edit, 1);
2693 break;
2694 } else {
2695 if (option_fake_half_tabs) {
2696 int i;
2697 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2698 for (i = 0; i < HALF_TAB_SIZE; i++)
2699 edit_backspace (edit, 1);
2700 break;
2704 edit_backspace (edit, 0);
2705 break;
2706 case CK_Delete:
2707 /* if non persistent selection and text selected */
2708 if ( !option_persistent_selections ) {
2709 if ( edit->mark1 != edit->mark2 ) {
2710 edit_block_delete_cmd (edit);
2711 break;
2715 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2716 edit_insert_over (edit);
2718 if (option_fake_half_tabs) {
2719 int i;
2720 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2721 for (i = 1; i <= HALF_TAB_SIZE; i++)
2722 edit_delete (edit, 1);
2723 break;
2726 edit_delete (edit, 0);
2727 break;
2728 case CK_Delete_Word_Left:
2729 edit->over_col = 0;
2730 edit_left_delete_word (edit);
2731 break;
2732 case CK_Delete_Word_Right:
2733 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2734 edit_insert_over (edit);
2736 edit_right_delete_word (edit);
2737 break;
2738 case CK_Delete_Line:
2739 edit_delete_line (edit);
2740 break;
2741 case CK_Delete_To_Line_End:
2742 edit_delete_to_line_end (edit);
2743 break;
2744 case CK_Delete_To_Line_Begin:
2745 edit_delete_to_line_begin (edit);
2746 break;
2747 case CK_Enter:
2748 edit->over_col = 0;
2749 if (option_auto_para_formatting) {
2750 edit_double_newline (edit);
2751 if (option_return_does_auto_indent)
2752 edit_auto_indent (edit);
2753 format_paragraph (edit, 0);
2754 } else {
2755 edit_insert (edit, '\n');
2756 if (option_return_does_auto_indent) {
2757 edit_auto_indent (edit);
2760 break;
2761 case CK_Return:
2762 edit_insert (edit, '\n');
2763 break;
2765 case CK_Page_Up_Alt_Highlight:
2766 column_highlighting = 1;
2767 case CK_Page_Up:
2768 case CK_Page_Up_Highlight:
2769 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2770 break;
2771 case CK_Page_Down_Alt_Highlight:
2772 column_highlighting = 1;
2773 case CK_Page_Down:
2774 case CK_Page_Down_Highlight:
2775 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2776 break;
2777 case CK_Left_Alt_Highlight:
2778 column_highlighting = 1;
2779 case CK_Left:
2780 case CK_Left_Highlight:
2781 if (option_fake_half_tabs) {
2782 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2783 if ( option_cursor_beyond_eol && edit->over_col > 0)
2784 edit->over_col--;
2785 else
2786 edit_cursor_move (edit, -HALF_TAB_SIZE);
2787 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2788 break;
2791 edit_left_char_move_cmd (edit);
2792 break;
2793 case CK_Right_Alt_Highlight:
2794 column_highlighting = 1;
2795 case CK_Right:
2796 case CK_Right_Highlight:
2797 if (option_fake_half_tabs) {
2798 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2799 edit_cursor_move (edit, HALF_TAB_SIZE);
2800 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2801 break;
2804 edit_right_char_move_cmd (edit);
2805 break;
2806 case CK_Begin_Page:
2807 case CK_Begin_Page_Highlight:
2808 edit_begin_page (edit);
2809 break;
2810 case CK_End_Page:
2811 case CK_End_Page_Highlight:
2812 edit_end_page (edit);
2813 break;
2814 case CK_Word_Left:
2815 case CK_Word_Left_Highlight:
2816 edit->over_col = 0;
2817 edit_left_word_move_cmd (edit);
2818 break;
2819 case CK_Word_Right:
2820 case CK_Word_Right_Highlight:
2821 edit->over_col = 0;
2822 edit_right_word_move_cmd (edit);
2823 break;
2824 case CK_Up_Alt_Highlight:
2825 column_highlighting = 1;
2826 case CK_Up:
2827 case CK_Up_Highlight:
2828 edit_move_up (edit, 1, 0);
2829 break;
2830 case CK_Down_Alt_Highlight:
2831 column_highlighting = 1;
2832 case CK_Down:
2833 case CK_Down_Highlight:
2834 edit_move_down (edit, 1, 0);
2835 break;
2836 case CK_Paragraph_Up_Alt_Highlight:
2837 column_highlighting = 1;
2838 case CK_Paragraph_Up:
2839 case CK_Paragraph_Up_Highlight:
2840 edit_move_up_paragraph (edit, 0);
2841 break;
2842 case CK_Paragraph_Down_Alt_Highlight:
2843 column_highlighting = 1;
2844 case CK_Paragraph_Down:
2845 case CK_Paragraph_Down_Highlight:
2846 edit_move_down_paragraph (edit, 0);
2847 break;
2848 case CK_Scroll_Up_Alt_Highlight:
2849 column_highlighting = 1;
2850 case CK_Scroll_Up:
2851 case CK_Scroll_Up_Highlight:
2852 edit_move_up (edit, 1, 1);
2853 break;
2854 case CK_Scroll_Down_Alt_Highlight:
2855 column_highlighting = 1;
2856 case CK_Scroll_Down:
2857 case CK_Scroll_Down_Highlight:
2858 edit_move_down (edit, 1, 1);
2859 break;
2860 case CK_Home:
2861 case CK_Home_Highlight:
2862 edit_cursor_to_bol (edit);
2863 break;
2864 case CK_End:
2865 case CK_End_Highlight:
2866 edit_cursor_to_eol (edit);
2867 break;
2868 case CK_Tab:
2869 /* if text marked shift block */
2870 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2871 edit_move_block_to_right (edit);
2872 } else {
2873 if ( option_cursor_beyond_eol )
2874 edit_insert_over (edit);
2875 edit_tab_cmd (edit);
2876 if (option_auto_para_formatting) {
2877 format_paragraph (edit, 0);
2878 edit->force |= REDRAW_PAGE;
2879 } else {
2880 check_and_wrap_line (edit);
2883 break;
2885 case CK_Toggle_Insert:
2886 edit->overwrite = (edit->overwrite == 0);
2887 break;
2889 case CK_Mark:
2890 if (edit->mark2 >= 0) {
2891 if (column_highlighting)
2892 edit_push_action (edit, COLUMN_ON);
2893 column_highlighting = 0;
2895 edit_mark_cmd (edit, 0);
2896 break;
2897 case CK_Column_Mark:
2898 if (!column_highlighting)
2899 edit_push_action (edit, COLUMN_OFF);
2900 column_highlighting = 1;
2901 edit_mark_cmd (edit, 0);
2902 break;
2903 case CK_Unmark:
2904 if (column_highlighting)
2905 edit_push_action (edit, COLUMN_ON);
2906 column_highlighting = 0;
2907 edit_mark_cmd (edit, 1);
2908 break;
2910 case CK_Toggle_Line_State:
2911 option_line_state = !option_line_state;
2912 if ( option_line_state ) {
2913 option_line_state_width = LINE_STATE_WIDTH;
2914 } else {
2915 option_line_state_width = 0;
2917 edit->force |= REDRAW_PAGE;
2918 break;
2920 case CK_Toggle_Bookmark:
2921 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2922 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2923 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2924 else
2925 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2926 break;
2927 case CK_Flush_Bookmarks:
2928 book_mark_flush (edit, BOOK_MARK_COLOR);
2929 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2930 edit->force |= REDRAW_PAGE;
2931 break;
2932 case CK_Next_Bookmark:
2933 if (edit->book_mark) {
2934 struct _book_mark *p;
2935 p = (struct _book_mark *) book_mark_find (edit,
2936 edit->curs_line);
2937 if (p->next) {
2938 p = p->next;
2939 if (p->line >= edit->start_line + edit->num_widget_lines
2940 || p->line < edit->start_line)
2941 edit_move_display (edit,
2942 p->line -
2943 edit->num_widget_lines / 2);
2944 edit_move_to_line (edit, p->line);
2947 break;
2948 case CK_Prev_Bookmark:
2949 if (edit->book_mark) {
2950 struct _book_mark *p;
2951 p = (struct _book_mark *) book_mark_find (edit,
2952 edit->curs_line);
2953 while (p->line == edit->curs_line)
2954 if (p->prev)
2955 p = p->prev;
2956 if (p->line >= 0) {
2957 if (p->line >= edit->start_line + edit->num_widget_lines
2958 || p->line < edit->start_line)
2959 edit_move_display (edit,
2960 p->line -
2961 edit->num_widget_lines / 2);
2962 edit_move_to_line (edit, p->line);
2965 break;
2967 case CK_Beginning_Of_Text:
2968 case CK_Beginning_Of_Text_Highlight:
2969 edit_move_to_top (edit);
2970 break;
2971 case CK_End_Of_Text:
2972 case CK_End_Of_Text_Highlight:
2973 edit_move_to_bottom (edit);
2974 break;
2976 case CK_Copy:
2977 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2978 edit_insert_over (edit);
2979 edit_block_copy_cmd (edit);
2980 break;
2981 case CK_Remove:
2982 edit_block_delete_cmd (edit);
2983 break;
2984 case CK_Move:
2985 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2986 edit_insert_over (edit);
2987 edit_block_move_cmd (edit);
2988 break;
2990 case CK_XStore:
2991 edit_copy_to_X_buf_cmd (edit);
2992 break;
2993 case CK_XCut:
2994 edit_cut_to_X_buf_cmd (edit);
2995 break;
2996 case CK_XPaste:
2997 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2998 edit_insert_over (edit);
2999 edit_paste_from_X_buf_cmd (edit);
3000 break;
3001 case CK_Selection_History:
3002 edit_paste_from_history (edit);
3003 break;
3005 case CK_Save_As:
3006 edit_save_as_cmd (edit);
3007 break;
3008 case CK_Save:
3009 edit_save_confirm_cmd (edit);
3010 break;
3011 case CK_Load:
3012 edit_load_cmd (edit, EDIT_FILE_COMMON);
3013 break;
3014 case CK_Save_Block:
3015 edit_save_block_cmd (edit);
3016 break;
3017 case CK_Insert_File:
3018 edit_insert_file_cmd (edit);
3019 break;
3021 case CK_Load_Prev_File:
3022 edit_load_back_cmd (edit);
3023 break;
3024 case CK_Load_Next_File:
3025 edit_load_forward_cmd (edit);
3026 break;
3028 case CK_Load_Syntax_File:
3029 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3030 break;
3031 case CK_Load_Menu_File:
3032 edit_load_cmd (edit, EDIT_FILE_MENU);
3033 break;
3035 case CK_Toggle_Syntax:
3036 if ((option_syntax_highlighting ^= 1) == 1)
3037 edit_load_syntax (edit, NULL, option_syntax_type);
3038 edit->force |= REDRAW_PAGE;
3039 break;
3041 case CK_Toggle_Tab_TWS:
3042 enable_show_tabs_tws ^= 1;
3043 edit->force |= REDRAW_PAGE;
3044 break;
3046 case CK_Find:
3047 edit_search_cmd (edit, 0);
3048 break;
3049 case CK_Find_Again:
3050 edit_search_cmd (edit, 1);
3051 break;
3052 case CK_Replace:
3053 edit_replace_cmd (edit, 0);
3054 break;
3055 case CK_Replace_Again:
3056 edit_replace_cmd (edit, 1);
3057 break;
3058 case CK_Complete_Word:
3059 /* if text marked shift block */
3060 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3061 edit_move_block_to_left (edit);
3062 } else {
3063 edit_complete_word_cmd (edit);
3065 break;
3066 case CK_Find_Definition:
3067 edit_get_match_keyword_cmd (edit);
3068 break;
3070 case CK_Exit:
3071 dlg_stop (edit->widget.parent);
3072 break;
3073 case CK_New:
3074 edit_new_cmd (edit);
3075 break;
3077 case CK_Help:
3078 edit_help_cmd (edit);
3079 break;
3081 case CK_Refresh:
3082 edit_refresh_cmd (edit);
3083 break;
3085 case CK_Date:{
3086 char s[1024];
3087 /* fool gcc to prevent a Y2K warning */
3088 char time_format[] = "_c";
3089 time_format[0] = '%';
3091 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3092 edit_print_string (edit, s);
3093 edit->force |= REDRAW_PAGE;
3094 break;
3096 case CK_Goto:
3097 edit_goto_cmd (edit);
3098 break;
3099 case CK_Paragraph_Format:
3100 format_paragraph (edit, 1);
3101 edit->force |= REDRAW_PAGE;
3102 break;
3103 case CK_Delete_Macro:
3104 edit_delete_macro_cmd (edit);
3105 break;
3106 case CK_Match_Bracket:
3107 edit_goto_matching_bracket (edit);
3108 break;
3109 case CK_User_Menu:
3110 user_menu (edit);
3111 break;
3112 case CK_Sort:
3113 edit_sort_cmd (edit);
3114 break;
3115 case CK_ExtCmd:
3116 edit_ext_cmd (edit);
3117 break;
3118 case CK_Mail:
3119 edit_mail_dialog (edit);
3120 break;
3121 case CK_Shell:
3122 view_other_cmd ();
3123 break;
3124 case CK_SelectCodepage:
3125 edit_select_codepage_cmd (edit);
3126 break;
3127 case CK_Insert_Literal:
3128 edit_insert_literal_cmd (edit);
3129 break;
3130 case CK_Execute_Macro:
3131 edit_execute_macro_cmd (edit);
3132 break;
3133 case CK_Begin_End_Macro:
3134 edit_begin_end_macro_cmd (edit);
3135 break;
3136 case CK_Ext_Mode:
3137 edit->extmod = 1;
3138 break;
3139 default:
3140 break;
3143 /* CK_Pipe_Block */
3144 if ((command / 1000) == 1) /* a shell command */
3145 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3146 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3147 struct macro m[MAX_MACRO_LENGTH];
3148 int nm;
3149 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3150 edit_execute_macro (edit, m, nm);
3153 /* keys which must set the col position, and the search vars */
3154 switch (command) {
3155 case CK_Find:
3156 case CK_Find_Again:
3157 case CK_Replace:
3158 case CK_Replace_Again:
3159 case CK_Complete_Word:
3160 edit->prev_col = edit_get_col (edit);
3161 break;
3162 case CK_Up:
3163 case CK_Up_Highlight:
3164 case CK_Up_Alt_Highlight:
3165 case CK_Down:
3166 case CK_Down_Highlight:
3167 case CK_Down_Alt_Highlight:
3168 case CK_Page_Up:
3169 case CK_Page_Up_Highlight:
3170 case CK_Page_Up_Alt_Highlight:
3171 case CK_Page_Down:
3172 case CK_Page_Down_Highlight:
3173 case CK_Page_Down_Alt_Highlight:
3174 case CK_Beginning_Of_Text:
3175 case CK_Beginning_Of_Text_Highlight:
3176 case CK_End_Of_Text:
3177 case CK_End_Of_Text_Highlight:
3178 case CK_Paragraph_Up:
3179 case CK_Paragraph_Up_Highlight:
3180 case CK_Paragraph_Up_Alt_Highlight:
3181 case CK_Paragraph_Down:
3182 case CK_Paragraph_Down_Highlight:
3183 case CK_Paragraph_Down_Alt_Highlight:
3184 case CK_Scroll_Up:
3185 case CK_Scroll_Up_Highlight:
3186 case CK_Scroll_Up_Alt_Highlight:
3187 case CK_Scroll_Down:
3188 case CK_Scroll_Down_Highlight:
3189 case CK_Scroll_Down_Alt_Highlight:
3190 edit->search_start = edit->curs1;
3191 edit->found_len = 0;
3192 break;
3193 default:
3194 edit->found_len = 0;
3195 edit->prev_col = edit_get_col (edit);
3196 edit->search_start = edit->curs1;
3198 edit_find_bracket (edit);
3200 if (option_auto_para_formatting) {
3201 switch (command) {
3202 case CK_BackSpace:
3203 case CK_Delete:
3204 case CK_Delete_Word_Left:
3205 case CK_Delete_Word_Right:
3206 case CK_Delete_To_Line_End:
3207 case CK_Delete_To_Line_Begin:
3208 format_paragraph (edit, 0);
3209 edit->force |= REDRAW_PAGE;
3215 static void
3216 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3218 int i = 0;
3220 if (edit->macro_depth++ > 256) {
3221 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3222 edit->macro_depth--;
3223 return;
3225 edit->force |= REDRAW_PAGE;
3226 for (; i < n; i++) {
3227 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3229 edit_update_screen (edit);
3230 edit->macro_depth--;
3233 /* User edit menu, like user menu (F2) but only in editor. */
3234 static void
3235 user_menu (WEdit * edit)
3237 FILE *fd;
3238 int nomark;
3239 struct stat status;
3240 long start_mark, end_mark;
3241 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3242 int rc = 0;
3244 nomark = eval_marks (edit, &start_mark, &end_mark);
3245 if (!nomark) /* remember marked or not */
3246 edit_save_block (edit, block_file, start_mark, end_mark);
3248 /* run shell scripts from menu */
3249 user_menu_cmd (edit);
3251 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3252 /* no block messages */
3253 goto cleanup;
3256 if (!nomark) {
3257 /* i.e. we have marked block */
3258 rc = edit_block_delete_cmd (edit);
3261 if (!rc) {
3262 edit_insert_file (edit, block_file);
3265 /* truncate block file */
3266 if ((fd = fopen (block_file, "w"))) {
3267 fclose (fd);
3270 edit_refresh_cmd (edit);
3271 edit->force |= REDRAW_COMPLETELY;
3273 cleanup:
3274 g_free (block_file);
3277 void
3278 edit_stack_init (void)
3280 for (edit_stack_iterator = 0;
3281 edit_stack_iterator < MAX_HISTORY_MOVETO;
3282 edit_stack_iterator++ ) {
3283 edit_history_moveto[edit_stack_iterator].filename = NULL;
3284 edit_history_moveto[edit_stack_iterator].line = -1;
3287 edit_stack_iterator = 0;
3290 void
3291 edit_stack_free (void)
3293 for (edit_stack_iterator = 0;
3294 edit_stack_iterator < MAX_HISTORY_MOVETO;
3295 edit_stack_iterator++)
3296 g_free (edit_history_moveto[edit_stack_iterator].filename);