edit_set_codeset(): new function to set codeset in MC editor.
[midnight-commander.git] / src / editor / edit.c
blob481838fe4fa8d41dd31087c576f804290668ab0b
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 "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "src/charsets.h" /* get_codepage_id */
57 #include "src/main.h" /* source_codepage */
58 #include "src/learn.h" /* learn_keys */
59 #include "src/cmddef.h"
61 #include "edit-impl.h"
62 #include "editlock.h"
63 #include "edit-widget.h"
66 int option_word_wrap_line_length = 72;
67 int option_typewriter_wrap = 0;
68 int option_auto_para_formatting = 0;
69 int option_fill_tabs_with_spaces = 0;
70 int option_return_does_auto_indent = 1;
71 int option_backspace_through_tabs = 0;
72 int option_fake_half_tabs = 1;
73 int option_save_mode = EDIT_QUICK_SAVE;
74 int option_save_position = 1;
75 int option_max_undo = 32768;
76 int option_persistent_selections = 1;
77 int option_cursor_beyond_eol = 0;
78 int option_line_state = 0;
79 int option_line_state_width = 0;
81 int option_edit_right_extreme = 0;
82 int option_edit_left_extreme = 0;
83 int option_edit_top_extreme = 0;
84 int option_edit_bottom_extreme = 0;
85 int enable_show_tabs_tws = 1;
86 int option_check_nl_at_eof = 0;
88 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
89 char *option_backup_ext = NULL;
91 int edit_stack_iterator = 0;
92 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
93 /* magic sequense for say than block is vertical */
94 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
95 /*-
97 * here's a quick sketch of the layout: (don't run this through indent.)
99 * (b1 is buffers1 and b2 is buffers2)
102 * \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
103 * ______________________________________|______________________________________
105 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
106 * |-> |-> |-> |-> |-> |-> |
108 * _<------------------------->|<----------------->_
109 * WEdit->curs2 | WEdit->curs1
110 * ^ | ^
111 * | ^|^ |
112 * cursor ||| cursor
113 * |||
114 * file end|||file beginning
119 * This_is_some_file
120 * fin.
123 const global_keymap_t *editor_map;
124 const global_keymap_t *editor_x_map;
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;
145 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
146 return NULL;
148 if (byte_index >= edit->curs1) {
149 p = edit->curs1 + edit->curs2 - byte_index - 1;
150 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
151 } else {
152 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
156 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
158 unsigned long p;
160 if (byte_index >= (edit->curs1 + edit->curs2))
161 byte_index--;
163 if (byte_index < 0)
164 return NULL;
166 if (byte_index >= edit->curs1) {
167 p = edit->curs1 + edit->curs2 - 1;
168 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
169 } else {
170 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
174 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
176 gchar *str = NULL;
177 int res = -1;
178 gunichar ch;
179 gchar *next_ch = NULL;
180 int width = 0;
182 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
183 *char_width = 0;
184 return '\n';
187 str = edit_get_byte_ptr (edit, byte_index);
189 if (str == NULL) {
190 *char_width = 0;
191 return 0;
194 res = g_utf8_get_char_validated (str, -1);
196 if (res < 0) {
197 ch = *str;
198 width = 0;
199 } else {
200 ch = res;
201 /* Calculate UTF-8 char width */
202 next_ch = g_utf8_next_char (str);
203 if (next_ch) {
204 width = next_ch - str;
205 } else {
206 ch = 0;
207 width = 0;
210 *char_width = width;
211 return ch;
214 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
216 gchar *str, *buf = NULL;
217 int res = -1;
218 gunichar ch;
219 gchar *next_ch = NULL;
220 int width = 0;
222 if (byte_index > 0)
223 byte_index--;
225 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
226 *char_width = 0;
227 return 0;
230 ch = edit_get_utf (edit, byte_index, &width);
232 if (width == 1) {
233 *char_width = width;
234 return ch;
237 str = edit_get_byte_ptr (edit, byte_index);
238 buf = edit_get_buf_ptr (edit, byte_index);
239 if (str == NULL || buf == NULL) {
240 *char_width = 0;
241 return 0;
243 /* get prev utf8 char */
244 if (str != buf)
245 str = g_utf8_find_prev_char (buf, str);
247 res = g_utf8_get_char_validated (str, -1);
249 if (res < 0) {
250 ch = *str;
251 width = 0;
252 } else {
253 ch = res;
254 /* Calculate UTF-8 char width */
255 next_ch = g_utf8_next_char(str);
256 if (next_ch) {
257 width = next_ch - str;
258 } else {
259 ch = 0;
260 width = 0;
263 *char_width = width;
264 return ch;
268 * Initialize the buffers for an empty files.
270 static void
271 edit_init_buffers (WEdit *edit)
273 int j;
275 for (j = 0; j <= MAXBUFF; j++) {
276 edit->buffers1[j] = NULL;
277 edit->buffers2[j] = NULL;
280 edit->curs1 = 0;
281 edit->curs2 = 0;
282 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
286 * Load file OR text into buffers. Set cursor to the beginning of file.
287 * Return 1 on error.
289 static int
290 edit_load_file_fast (WEdit *edit, const char *filename)
292 long buf, buf2;
293 int file = -1;
294 edit->curs2 = edit->last_byte;
295 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
296 edit->utf8 = 0;
297 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
298 GString *errmsg = g_string_new(NULL);
299 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
300 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
301 g_string_free (errmsg, TRUE);
302 return 1;
305 if (!edit->buffers2[buf2])
306 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
308 mc_read (file,
309 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
310 (edit->curs2 & M_EDIT_BUF_SIZE),
311 edit->curs2 & M_EDIT_BUF_SIZE);
313 for (buf = buf2 - 1; buf >= 0; buf--) {
314 /* edit->buffers2[0] is already allocated */
315 if (!edit->buffers2[buf])
316 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
317 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
320 mc_close (file);
321 return 0;
324 /* detecting an error on save is easy: just check if every byte has been written. */
325 /* detecting an error on read, is not so easy 'cos there is not way to tell
326 whether you read everything or not. */
327 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
328 static const struct edit_filters {
329 const char *read, *write, *extension;
330 } all_filters[] = {
331 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
332 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
333 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
334 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
335 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
338 /* Return index of the filter or -1 is there is no appropriate filter */
339 static int edit_find_filter (const char *filename)
341 size_t i, l, e;
343 if (filename == NULL)
344 return -1;
346 l = strlen (filename);
347 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
348 e = strlen (all_filters[i].extension);
349 if (l > e)
350 if (!strcmp (all_filters[i].extension, filename + l - e))
351 return i;
353 return -1;
356 static char *
357 edit_get_filter (const char *filename)
359 int i;
360 char *p, *quoted_name;
362 i = edit_find_filter (filename);
363 if (i < 0)
364 return NULL;
366 quoted_name = name_quote (filename, 0);
367 p = g_strdup_printf(all_filters[i].read, quoted_name);
368 g_free (quoted_name);
369 return p;
372 char *
373 edit_get_write_filter (const char *write_name, const char *filename)
375 int i;
376 char *p, *writename;
378 i = edit_find_filter (filename);
379 if (i < 0)
380 return NULL;
382 writename = name_quote (write_name, 0);
383 p = g_strdup_printf(all_filters[i].write, writename);
384 g_free (writename);
385 return p;
388 static long
389 edit_insert_stream (WEdit * edit, FILE * f)
391 int c;
392 long i = 0;
393 while ((c = fgetc (f)) >= 0) {
394 edit_insert (edit, c);
395 i++;
397 return i;
400 long edit_write_stream (WEdit * edit, FILE * f)
402 long i;
404 if (edit->lb == LB_ASIS) {
405 for (i = 0; i < edit->last_byte; i++)
406 if (fputc (edit_get_byte (edit, i), f) < 0)
407 break;
408 return i;
411 /* change line breaks */
412 for (i = 0; i < edit->last_byte; i++) {
413 unsigned char c = edit_get_byte (edit, i);
415 if (!(c == '\n' || c == '\r')) {
416 /* not line break */
417 if (fputc (c, f) < 0)
418 return i;
419 } else { /* (c == '\n' || c == '\r') */
420 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
422 switch (edit->lb) {
423 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
424 /* put one line break unconditionally */
425 if (fputc ('\n', f) < 0)
426 return i;
428 i++; /* 2 chars are processed */
430 if (c == '\r' && c1 == '\n')
431 /* Windows line break; go to the next char */
432 break;
434 if (c == '\r' && c1 == '\r') {
435 /* two Macintosh line breaks; put second line break */
436 if (fputc ('\n', f) < 0)
437 return i;
438 break;
441 if (fputc (c1, f) < 0)
442 return i;
443 break;
445 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
446 /* put one line break unconditionally */
447 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
448 return i;
450 if (c == '\r' && c1 == '\n')
451 /* Windows line break; go to the next char */
452 i++;
453 break;
455 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
456 /* put one line break unconditionally */
457 if (fputc ('\r', f) < 0)
458 return i;
460 i++; /* 2 chars are processed */
462 if (c == '\r' && c1 == '\n')
463 /* Windows line break; go to the next char */
464 break;
466 if (c == '\n' && c1 == '\n') {
467 /* two Windows line breaks; put second line break */
468 if (fputc ('\r', f) < 0)
469 return i;
470 break;
473 if (fputc (c1, f) < 0)
474 return i;
475 break;
476 case LB_ASIS: /* default without changes */
477 break;
482 return edit->last_byte;
485 #define TEMP_BUF_LEN 1024
487 /* inserts a file at the cursor, returns 1 on success */
489 edit_insert_file (WEdit *edit, const char *filename)
491 char *p;
493 p = edit_get_filter (filename);
494 if (p != NULL) {
495 FILE *f;
496 long current = edit->curs1;
497 f = (FILE *) popen (p, "r");
498 if (f != NULL) {
499 edit_insert_stream (edit, f);
500 edit_cursor_move (edit, current - edit->curs1);
501 if (pclose (f) > 0) {
502 char *errmsg;
503 errmsg = g_strdup_printf(_(" Error reading from pipe: %s "), p);
504 edit_error_dialog (_("Error"), errmsg);
505 g_free (errmsg);
506 g_free (p);
507 return 0;
509 } else {
510 char *errmsg;
511 errmsg = g_strdup_printf (_(" Cannot open pipe for reading: %s "), p);
512 edit_error_dialog (_("Error"), errmsg);
513 g_free (errmsg);
514 g_free (p);
515 return 0;
517 g_free (p);
518 } else {
519 int i, file, blocklen;
520 long current = edit->curs1;
521 int vertical_insertion = 0;
522 char *buf;
523 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
524 return 0;
525 buf = g_malloc0 (TEMP_BUF_LEN);
526 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
527 if (blocklen > 0) {
528 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
529 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
530 vertical_insertion = 1;
531 } else {
532 mc_lseek (file, 0, SEEK_SET);
535 if (vertical_insertion) {
536 blocklen = edit_insert_column_of_text_from_file (edit, file);
537 } else {
538 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
539 for (i = 0; i < blocklen; i++)
540 edit_insert (edit, buf[i]);
543 edit_cursor_move (edit, current - edit->curs1);
544 g_free (buf);
545 mc_close (file);
546 if (blocklen)
547 return 0;
549 return 1;
552 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
553 static int
554 check_file_access (WEdit *edit, const char *filename, struct stat *st)
556 int file;
557 GString *errmsg = (GString *) 0;
559 /* Try opening an existing file */
560 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
562 if (file < 0) {
564 * Try creating the file. O_EXCL prevents following broken links
565 * and opening existing files.
567 file =
568 mc_open (filename,
569 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
570 0666);
571 if (file < 0) {
572 g_string_sprintf (errmsg = g_string_new (NULL),
573 _(" Cannot open %s for reading "), filename);
574 goto cleanup;
575 } else {
576 /* New file, delete it if it's not modified or saved */
577 edit->delete_file = 1;
581 /* Check what we have opened */
582 if (mc_fstat (file, st) < 0) {
583 g_string_sprintf (errmsg = g_string_new (NULL),
584 _(" Cannot get size/permissions for %s "), filename);
585 goto cleanup;
588 /* We want to open regular files only */
589 if (!S_ISREG (st->st_mode)) {
590 g_string_sprintf (errmsg = g_string_new (NULL),
591 _(" %s is not a regular file "), filename);
592 goto cleanup;
596 * Don't delete non-empty files.
597 * O_EXCL should prevent it, but let's be on the safe side.
599 if (st->st_size > 0) {
600 edit->delete_file = 0;
603 if (st->st_size >= SIZE_LIMIT) {
604 g_string_sprintf (errmsg = g_string_new (NULL),
605 _(" File %s is too large "), filename);
608 cleanup:
609 (void) mc_close (file);
610 if (errmsg) {
611 edit_error_dialog (_("Error"), errmsg->str);
612 g_string_free (errmsg, TRUE);
613 return 1;
615 return 0;
619 * Open the file and load it into the buffers, either directly or using
620 * a filter. Return 0 on success, 1 on error.
622 * Fast loading (edit_load_file_fast) is used when the file size is
623 * known. In this case the data is read into the buffers by blocks.
624 * If the file size is not known, the data is loaded byte by byte in
625 * edit_insert_file.
627 static int
628 edit_load_file (WEdit *edit)
630 int fast_load = 1;
632 /* Cannot do fast load if a filter is used */
633 if (edit_find_filter (edit->filename) >= 0)
634 fast_load = 0;
637 * VFS may report file size incorrectly, and slow load is not a big
638 * deal considering overhead in VFS.
640 if (!vfs_file_is_local (edit->filename))
641 fast_load = 0;
644 * FIXME: line end translation should disable fast loading as well
645 * Consider doing fseek() to the end and ftell() for the real size.
648 if (*edit->filename) {
649 /* If we are dealing with a real file, check that it exists */
650 if (check_file_access (edit, edit->filename, &edit->stat1))
651 return 1;
652 } else {
653 /* nothing to load */
654 fast_load = 0;
657 edit_init_buffers (edit);
659 if (fast_load) {
660 edit->last_byte = edit->stat1.st_size;
661 edit_load_file_fast (edit, edit->filename);
662 /* If fast load was used, the number of lines wasn't calculated */
663 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
664 } else {
665 #ifdef HAVE_CHARSET
666 const char *codepage_id;
667 #endif
668 edit->last_byte = 0;
669 if (*edit->filename) {
670 edit->stack_disable = 1;
671 if (!edit_insert_file (edit, edit->filename)) {
672 edit_clean (edit);
673 return 1;
675 edit->stack_disable = 0;
678 #ifdef HAVE_CHARSET
679 codepage_id = get_codepage_id( source_codepage );
680 if ( codepage_id )
681 edit->utf8 = str_isutf8 ( codepage_id );
682 #endif
684 edit->lb = LB_ASIS;
685 return 0;
688 /* Restore saved cursor position in the file */
689 static void
690 edit_load_position (WEdit *edit)
692 char *filename;
693 long line, column;
695 if (!edit->filename || !*edit->filename)
696 return;
698 filename = vfs_canon (edit->filename);
699 load_file_position (filename, &line, &column);
700 g_free (filename);
702 edit_move_to_line (edit, line - 1);
703 edit->prev_col = column;
704 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
705 edit_move_display (edit, line - (edit->num_widget_lines / 2));
708 /* Save cursor position in the file */
709 static void
710 edit_save_position (WEdit *edit)
712 char *filename;
714 if (!edit->filename || !*edit->filename)
715 return;
717 filename = vfs_canon (edit->filename);
718 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
719 g_free (filename);
722 /* Clean the WEdit stricture except the widget part */
723 static void
724 edit_purge_widget (WEdit *edit)
726 size_t len = sizeof (WEdit) - sizeof (Widget);
727 char *start = (char *) edit + sizeof (Widget);
728 memset (start, 0, len);
729 edit->macro_i = -1; /* not recording a macro */
732 static void
733 edit_set_keymap (void)
735 editor_map = default_editor_keymap;
736 if (editor_keymap && editor_keymap->len > 0)
737 editor_map = (global_keymap_t *) editor_keymap->data;
739 editor_x_map = default_editor_x_keymap;
740 if (editor_x_keymap && editor_x_keymap->len > 0)
741 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
745 #define space_width 1
748 * Fill in the edit structure. Return NULL on failure. Pass edit as
749 * NULL to allocate a new structure.
751 * If line is 0, try to restore saved position. Otherwise put the
752 * cursor on that line and show it in the middle of the screen.
754 WEdit *
755 edit_init (WEdit *edit, int lines, int columns, const char *filename,
756 long line)
758 int to_free = 0;
759 option_auto_syntax = 1; /* Resetting to auto on every invokation */
760 if ( option_line_state ) {
761 option_line_state_width = LINE_STATE_WIDTH;
762 } else {
763 option_line_state_width = 0;
765 if (!edit) {
766 #ifdef ENABLE_NLS
768 * Expand option_whole_chars_search by national letters using
769 * current locale
772 static char option_whole_chars_search_buf[256];
774 if (option_whole_chars_search_buf != option_whole_chars_search) {
775 size_t i;
776 size_t len = str_term_width1 (option_whole_chars_search);
778 strcpy (option_whole_chars_search_buf,
779 option_whole_chars_search);
781 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
782 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
783 option_whole_chars_search_buf[len++] = i;
787 option_whole_chars_search_buf[len] = 0;
788 option_whole_chars_search = option_whole_chars_search_buf;
790 #endif /* ENABLE_NLS */
791 edit = g_malloc0 (sizeof (WEdit));
792 edit->search = NULL;
793 to_free = 1;
795 edit_purge_widget (edit);
796 edit->num_widget_lines = lines;
797 edit->over_col = 0;
798 edit->num_widget_columns = columns;
799 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
800 edit->stat1.st_uid = getuid ();
801 edit->stat1.st_gid = getgid ();
802 edit->stat1.st_mtime = 0;
803 edit->bracket = -1;
804 edit->force |= REDRAW_PAGE;
805 edit_set_filename (edit, filename);
806 edit->stack_size = START_STACK_SIZE;
807 edit->stack_size_mask = START_STACK_SIZE - 1;
808 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
810 edit->utf8 = 0;
811 edit->converter = str_cnv_from_term;
812 edit_set_codeset (edit);
814 if (edit_load_file (edit)) {
815 /* edit_load_file already gives an error message */
816 if (to_free)
817 g_free (edit);
818 return 0;
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 ();
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 mc_search_free (edit->search);
878 edit->search = NULL;
880 if (edit->converter != str_cnv_from_term)
881 str_close_conv (edit->converter);
883 edit_purge_widget (edit);
885 return 1;
888 /* returns 1 on success */
890 edit_renew (WEdit * edit)
892 int lines = edit->num_widget_lines;
893 int columns = edit->num_widget_columns;
895 edit_clean (edit);
896 return (edit_init (edit, lines, columns, "", 0) != NULL);
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;
949 void
950 edit_set_codeset (WEdit *edit)
952 #ifdef HAVE_CHARSET
953 const char *cp_id;
955 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
957 if (cp_id != NULL)
959 GIConv conv;
960 conv = str_crt_conv_from (cp_id);
961 if (conv != INVALID_CONV)
963 if (edit->converter != str_cnv_from_term)
964 str_close_conv (edit->converter);
965 edit->converter = conv;
969 if (cp_id != NULL)
970 edit->utf8 = str_isutf8 (cp_id);
971 #else
972 (void) edit;
973 #endif
978 Recording stack for undo:
979 The following is an implementation of a compressed stack. Identical
980 pushes are recorded by a negative prefix indicating the number of times the
981 same char was pushed. This saves space for repeated curs-left or curs-right
982 delete etc.
986 pushed: stored:
990 b -3
992 c --> -4
998 If the stack long int is 0-255 it represents a normal insert (from a backspace),
999 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1000 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1001 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1002 position.
1004 The only way the cursor moves or the buffer is changed is through the routines:
1005 insert, backspace, insert_ahead, delete, and cursor_move.
1006 These record the reverse undo movements onto the stack each time they are
1007 called.
1009 Each key press results in a set of actions (insert; delete ...). So each time
1010 a key is pressed the current position of start_display is pushed as
1011 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1012 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1013 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1017 void edit_push_action (WEdit * edit, long c,...)
1019 unsigned long sp = edit->stack_pointer;
1020 unsigned long spm1;
1021 long *t;
1023 /* first enlarge the stack if necessary */
1024 if (sp > edit->stack_size - 10) { /* say */
1025 if (option_max_undo < 256)
1026 option_max_undo = 256;
1027 if (edit->stack_size < (unsigned long) option_max_undo) {
1028 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1029 if (t) {
1030 edit->undo_stack = t;
1031 edit->stack_size <<= 1;
1032 edit->stack_size_mask = edit->stack_size - 1;
1036 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1037 if (edit->stack_disable)
1038 return;
1040 #ifdef FAST_MOVE_CURSOR
1041 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1042 va_list ap;
1043 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1044 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1045 va_start (ap, c);
1046 c = -(va_arg (ap, int));
1047 va_end (ap);
1048 } else
1049 #endif /* ! FAST_MOVE_CURSOR */
1050 if (edit->stack_bottom != sp
1051 && spm1 != edit->stack_bottom
1052 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1053 int d;
1054 if (edit->undo_stack[spm1] < 0) {
1055 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1056 if (d == c) {
1057 if (edit->undo_stack[spm1] > -1000000000) {
1058 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1059 edit->undo_stack[spm1]--;
1060 return;
1063 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1064 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1065 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1066 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1067 if (edit->undo_stack[spm1] == -2)
1068 edit->stack_pointer = spm1;
1069 else
1070 edit->undo_stack[spm1]++;
1071 return;
1073 #endif
1074 } else {
1075 d = edit->undo_stack[spm1];
1076 if (d == c) {
1077 if (c >= KEY_PRESS)
1078 return; /* --> no need to push multiple do-nothings */
1079 edit->undo_stack[sp] = -2;
1080 goto check_bottom;
1082 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1083 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1084 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1085 edit->stack_pointer = spm1;
1086 return;
1088 #endif
1091 edit->undo_stack[sp] = c;
1093 check_bottom:
1094 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1096 /* if the sp wraps round and catches the stack_bottom then erase
1097 * the first set of actions on the stack to make space - by moving
1098 * stack_bottom forward one "key press" */
1099 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1100 if ((unsigned long) c == edit->stack_bottom ||
1101 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1102 do {
1103 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1104 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1106 /*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: */
1107 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1108 edit->stack_bottom = edit->stack_pointer = 0;
1112 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1113 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1115 static long
1116 pop_action (WEdit * edit)
1118 long c;
1119 unsigned long sp = edit->stack_pointer;
1120 if (sp == edit->stack_bottom) {
1121 return STACK_BOTTOM;
1123 sp = (sp - 1) & edit->stack_size_mask;
1124 if ((c = edit->undo_stack[sp]) >= 0) {
1125 /* edit->undo_stack[sp] = '@'; */
1126 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1127 return c;
1129 if (sp == edit->stack_bottom) {
1130 return STACK_BOTTOM;
1132 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1133 if (edit->undo_stack[sp] == -2) {
1134 /* edit->undo_stack[sp] = '@'; */
1135 edit->stack_pointer = sp;
1136 } else
1137 edit->undo_stack[sp]++;
1139 return c;
1142 /* is called whenever a modification is made by one of the four routines below */
1143 static void edit_modification (WEdit * edit)
1145 edit->caches_valid = 0;
1146 edit->screen_modified = 1;
1148 /* raise lock when file modified */
1149 if (!edit->modified && !edit->delete_file)
1150 edit->locked = edit_lock_file (edit->filename);
1151 edit->modified = 1;
1155 Basic low level single character buffer alterations and movements at the cursor.
1156 Returns char passed over, inserted or removed.
1159 void
1160 edit_insert (WEdit *edit, int c)
1162 /* check if file has grown to large */
1163 if (edit->last_byte >= SIZE_LIMIT)
1164 return;
1166 /* first we must update the position of the display window */
1167 if (edit->curs1 < edit->start_display) {
1168 edit->start_display++;
1169 if (c == '\n')
1170 edit->start_line++;
1173 /* Mark file as modified, unless the file hasn't been fully loaded */
1174 if (edit->loading_done) {
1175 edit_modification (edit);
1178 /* now we must update some info on the file and check if a redraw is required */
1179 if (c == '\n') {
1180 if (edit->book_mark)
1181 book_mark_inc (edit, edit->curs_line);
1182 edit->curs_line++;
1183 edit->total_lines++;
1184 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1187 /* save the reverse command onto the undo stack */
1188 edit_push_action (edit, BACKSPACE);
1190 /* update markers */
1191 edit->mark1 += (edit->mark1 > edit->curs1);
1192 edit->mark2 += (edit->mark2 > edit->curs1);
1193 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1195 /* add a new buffer if we've reached the end of the last one */
1196 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1197 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1198 g_malloc0 (EDIT_BUF_SIZE);
1200 /* perform the insertion */
1201 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1202 curs1 & M_EDIT_BUF_SIZE]
1203 = (unsigned char) c;
1205 /* update file length */
1206 edit->last_byte++;
1208 /* update cursor position */
1209 edit->curs1++;
1212 static void
1213 edit_insert_over (WEdit * edit)
1215 int i;
1217 for ( i = 0; i < edit->over_col; i++ ) {
1218 edit_insert (edit, ' ');
1220 edit->over_col = 0;
1223 /* same as edit_insert and move left */
1224 void edit_insert_ahead (WEdit * edit, int c)
1226 if (edit->last_byte >= SIZE_LIMIT)
1227 return;
1228 if (edit->curs1 < edit->start_display) {
1229 edit->start_display++;
1230 if (c == '\n')
1231 edit->start_line++;
1233 edit_modification (edit);
1234 if (c == '\n') {
1235 if (edit->book_mark)
1236 book_mark_inc (edit, edit->curs_line);
1237 edit->total_lines++;
1238 edit->force |= REDRAW_AFTER_CURSOR;
1240 edit_push_action (edit, DELCHAR);
1242 edit->mark1 += (edit->mark1 >= edit->curs1);
1243 edit->mark2 += (edit->mark2 >= edit->curs1);
1244 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1246 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1247 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1248 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1250 edit->last_byte++;
1251 edit->curs2++;
1255 int edit_delete (WEdit * edit, const int byte_delete)
1257 int p = 0;
1258 int cw = 1;
1259 int i;
1261 if (!edit->curs2)
1262 return 0;
1264 edit->mark1 -= (edit->mark1 > edit->curs1);
1265 edit->mark2 -= (edit->mark2 > edit->curs1);
1266 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1268 cw = 1;
1269 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1270 if ( edit->utf8 && byte_delete == 0 ) {
1271 edit_get_utf (edit, edit->curs1, &cw);
1272 if ( cw < 1 )
1273 cw = 1;
1275 for ( i = 1; i<= cw; i++ ) {
1276 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1278 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1279 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1280 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1282 edit->last_byte--;
1283 edit->curs2--;
1284 edit_push_action (edit, p + 256);
1287 edit_modification (edit);
1288 if (p == '\n') {
1289 if (edit->book_mark)
1290 book_mark_dec (edit, edit->curs_line);
1291 edit->total_lines--;
1292 edit->force |= REDRAW_AFTER_CURSOR;
1294 if (edit->curs1 < edit->start_display) {
1295 edit->start_display--;
1296 if (p == '\n')
1297 edit->start_line--;
1300 return p;
1304 static int
1305 edit_backspace (WEdit * edit, const int byte_delete)
1307 int p = 0;
1308 int cw = 1;
1309 int i;
1311 if (!edit->curs1)
1312 return 0;
1314 edit->mark1 -= (edit->mark1 >= edit->curs1);
1315 edit->mark2 -= (edit->mark2 >= edit->curs1);
1316 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1318 cw = 1;
1319 if ( edit->utf8 && byte_delete == 0 ) {
1320 edit_get_prev_utf (edit, edit->curs1, &cw);
1321 if ( cw < 1 )
1322 cw = 1;
1324 for ( i = 1; i<= cw; i++ ) {
1325 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1326 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1327 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1328 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1330 edit->last_byte--;
1331 edit->curs1--;
1332 edit_push_action (edit, p);
1334 edit_modification (edit);
1335 if (p == '\n') {
1336 if (edit->book_mark)
1337 book_mark_dec (edit, edit->curs_line);
1338 edit->curs_line--;
1339 edit->total_lines--;
1340 edit->force |= REDRAW_AFTER_CURSOR;
1343 if (edit->curs1 < edit->start_display) {
1344 edit->start_display--;
1345 if (p == '\n')
1346 edit->start_line--;
1349 return p;
1352 #ifdef FAST_MOVE_CURSOR
1354 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1356 unsigned long next;
1357 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1358 edit->curs_line--;
1359 next -= (unsigned long) dest;
1360 n -= next;
1361 src += next;
1362 dest += next;
1367 edit_move_backward_lots (WEdit *edit, long increment)
1369 int r, s, t;
1370 unsigned char *p = NULL;
1372 if (increment > edit->curs1)
1373 increment = edit->curs1;
1374 if (increment <= 0)
1375 return -1;
1376 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1378 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1379 if (r > increment)
1380 r = increment;
1381 s = edit->curs1 & M_EDIT_BUF_SIZE;
1383 if (s > r) {
1384 memqcpy (edit,
1385 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1386 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1388 } else {
1389 if (s) {
1390 memqcpy (edit,
1391 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1392 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1393 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1394 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1396 memqcpy (edit,
1397 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1398 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1399 EDIT_BUF_SIZE - (r - s), r - s);
1401 increment -= r;
1402 edit->curs1 -= r;
1403 edit->curs2 += r;
1404 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1405 if (p)
1406 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1407 else
1408 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1409 g_malloc0 (EDIT_BUF_SIZE);
1410 } else {
1411 g_free (p);
1414 s = edit->curs1 & M_EDIT_BUF_SIZE;
1415 while (increment) {
1416 p = 0;
1417 r = EDIT_BUF_SIZE;
1418 if (r > increment)
1419 r = increment;
1420 t = s;
1421 if (r < t)
1422 t = r;
1423 memqcpy (edit,
1424 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1425 EDIT_BUF_SIZE - t,
1426 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1428 if (r >= s) {
1429 if (t) {
1430 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1431 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1433 memqcpy (edit,
1434 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1435 EDIT_BUF_SIZE - r,
1436 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1437 EDIT_BUF_SIZE - (r - s), r - s);
1439 increment -= r;
1440 edit->curs1 -= r;
1441 edit->curs2 += r;
1442 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1443 if (p)
1444 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1445 else
1446 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1447 g_malloc0 (EDIT_BUF_SIZE);
1448 } else {
1449 g_free (p);
1452 return edit_get_byte (edit, edit->curs1);
1455 #endif /* ! FAST_MOVE_CURSOR */
1457 /* moves the cursor right or left: increment positive or negative respectively */
1458 void edit_cursor_move (WEdit * edit, long increment)
1460 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1461 int c;
1462 #ifdef FAST_MOVE_CURSOR
1463 if (increment < -256) {
1464 edit->force |= REDRAW_PAGE;
1465 edit_move_backward_lots (edit, -increment);
1466 return;
1468 #endif /* ! FAST_MOVE_CURSOR */
1470 if (increment < 0) {
1471 for (; increment < 0; increment++) {
1472 if (!edit->curs1)
1473 return;
1475 edit_push_action (edit, CURS_RIGHT);
1477 c = edit_get_byte (edit, edit->curs1 - 1);
1478 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1479 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1480 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1481 edit->curs2++;
1482 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1483 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1484 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1485 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1487 edit->curs1--;
1488 if (c == '\n') {
1489 edit->curs_line--;
1490 edit->force |= REDRAW_LINE_BELOW;
1494 } else if (increment > 0) {
1495 for (; increment > 0; increment--) {
1496 if (!edit->curs2)
1497 return;
1499 edit_push_action (edit, CURS_LEFT);
1501 c = edit_get_byte (edit, edit->curs1);
1502 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1503 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1504 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1505 edit->curs1++;
1506 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1507 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1508 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1509 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1511 edit->curs2--;
1512 if (c == '\n') {
1513 edit->curs_line++;
1514 edit->force |= REDRAW_LINE_ABOVE;
1520 /* These functions return positions relative to lines */
1522 /* returns index of last char on line + 1 */
1523 long edit_eol (WEdit * edit, long current)
1525 if (current < edit->last_byte) {
1526 for (;; current++)
1527 if (edit_get_byte (edit, current) == '\n')
1528 break;
1529 } else
1530 return edit->last_byte;
1531 return current;
1534 /* returns index of first char on line */
1535 long edit_bol (WEdit * edit, long current)
1537 if (current > 0) {
1538 for (;; current--)
1539 if (edit_get_byte (edit, current - 1) == '\n')
1540 break;
1541 } else
1542 return 0;
1543 return current;
1547 long edit_count_lines (WEdit * edit, long current, long upto)
1549 long lines = 0;
1550 if (upto > edit->last_byte)
1551 upto = edit->last_byte;
1552 if (current < 0)
1553 current = 0;
1554 while (current < upto)
1555 if (edit_get_byte (edit, current++) == '\n')
1556 lines++;
1557 return lines;
1561 /* If lines is zero this returns the count of lines from current to upto. */
1562 /* If upto is zero returns index of lines forward current. */
1563 long edit_move_forward (WEdit * edit, long current, long lines, long upto)
1565 if (upto) {
1566 return edit_count_lines (edit, current, upto);
1567 } else {
1568 long next;
1569 if (lines < 0)
1570 lines = 0;
1571 while (lines--) {
1572 next = edit_eol (edit, current) + 1;
1573 if (next > edit->last_byte)
1574 break;
1575 else
1576 current = next;
1578 return current;
1583 /* Returns offset of 'lines' lines up from current */
1584 long edit_move_backward (WEdit * edit, long current, long lines)
1586 if (lines < 0)
1587 lines = 0;
1588 current = edit_bol (edit, current);
1589 while((lines--) && current != 0)
1590 current = edit_bol (edit, current - 1);
1591 return current;
1594 /* If cols is zero this returns the count of columns from current to upto. */
1595 /* If upto is zero returns index of cols across from current. */
1596 long
1597 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1599 long p, q;
1600 int col;
1602 if (upto) {
1603 q = upto;
1604 cols = -10;
1605 } else
1606 q = edit->last_byte + 2;
1608 for (col = 0, p = current; p < q; p++) {
1609 int c, orig_c;
1610 int utf_ch = 0;
1611 #ifdef HAVE_CHARSET
1612 int cw = 1;
1613 #endif
1614 if (cols != -10) {
1615 if (col == cols)
1616 return p;
1617 if (col > cols)
1618 return p - 1;
1620 orig_c = c = edit_get_byte (edit, p);
1621 #ifdef HAVE_CHARSET
1622 if (edit->utf8) {
1623 utf_ch = edit_get_utf (edit, p, &cw);
1624 if (utf8_display) {
1625 if (cw > 1)
1626 col -= cw - 1;
1627 if (g_unichar_iswide (utf_ch))
1628 col++;
1629 } else if (cw > 1 && g_unichar_isprint (utf_ch))
1630 col -= cw - 1;
1632 #endif
1633 c = convert_to_display_c (c);
1634 if (c == '\t')
1635 col += TAB_SIZE - col % TAB_SIZE;
1636 else if (c == '\n') {
1637 if (upto)
1638 return col;
1639 else
1640 return p;
1641 } else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1642 /* '\r' is shown as ^M, so we must advance 2 characters */
1643 /* Caret notation for control characters */
1644 col += 2;
1645 else
1646 col++;
1648 return col;
1651 /* returns the current column position of the cursor */
1652 int edit_get_col (WEdit * edit)
1654 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1658 /* Scrolling functions */
1660 void edit_update_curs_row (WEdit * edit)
1662 edit->curs_row = edit->curs_line - edit->start_line;
1665 void edit_update_curs_col (WEdit * edit)
1667 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1671 edit_get_curs_col (const WEdit *edit)
1673 return edit->curs_col;
1676 /*moves the display start position up by i lines */
1677 void edit_scroll_upward (WEdit * edit, unsigned long i)
1679 unsigned long lines_above = edit->start_line;
1680 if (i > lines_above)
1681 i = lines_above;
1682 if (i) {
1683 edit->start_line -= i;
1684 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1685 edit->force |= REDRAW_PAGE;
1686 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1688 edit_update_curs_row (edit);
1692 /* returns 1 if could scroll, 0 otherwise */
1693 void edit_scroll_downward (WEdit * edit, int i)
1695 int lines_below;
1696 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1697 if (lines_below > 0) {
1698 if (i > lines_below)
1699 i = lines_below;
1700 edit->start_line += i;
1701 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1702 edit->force |= REDRAW_PAGE;
1703 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1705 edit_update_curs_row (edit);
1708 void edit_scroll_right (WEdit * edit, int i)
1710 edit->force |= REDRAW_PAGE;
1711 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1712 edit->start_col -= i;
1715 void edit_scroll_left (WEdit * edit, int i)
1717 if (edit->start_col) {
1718 edit->start_col += i;
1719 if (edit->start_col > 0)
1720 edit->start_col = 0;
1721 edit->force |= REDRAW_PAGE;
1722 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1726 /* high level cursor movement commands */
1728 static int is_in_indent (WEdit *edit)
1730 long p = edit_bol (edit, edit->curs1);
1731 while (p < edit->curs1)
1732 if (!strchr (" \t", edit_get_byte (edit, p++)))
1733 return 0;
1734 return 1;
1737 static int left_of_four_spaces (WEdit *edit);
1739 void
1740 edit_move_to_prev_col (WEdit * edit, long p)
1742 int prev = edit->prev_col;
1743 int over = edit->over_col;
1744 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1746 if (option_cursor_beyond_eol) {
1747 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1749 if (line_len < prev + edit->over_col) {
1750 edit->over_col = prev + over - line_len;
1751 edit->prev_col = line_len;
1752 edit->curs_col = line_len;
1753 } else {
1754 edit->curs_col = prev + over;
1755 edit->prev_col = edit->curs_col;
1756 edit->over_col = 0;
1758 } else {
1759 edit->over_col = 0;
1760 if (is_in_indent (edit) && option_fake_half_tabs) {
1761 edit_update_curs_col (edit);
1762 if (space_width)
1763 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1764 int q = edit->curs_col;
1765 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1766 p = edit_bol (edit, edit->curs1);
1767 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1768 if (!left_of_four_spaces (edit))
1769 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1775 static int
1776 is_blank (WEdit *edit, long offset)
1778 long s, f;
1779 int c;
1780 s = edit_bol (edit, offset);
1781 f = edit_eol (edit, offset) - 1;
1782 while (s <= f) {
1783 c = edit_get_byte (edit, s++);
1784 if (!isspace (c))
1785 return 0;
1787 return 1;
1791 /* returns the offset of line i */
1792 static long
1793 edit_find_line (WEdit *edit, int line)
1795 int i, j = 0;
1796 int m = 2000000000;
1797 if (!edit->caches_valid) {
1798 for (i = 0; i < N_LINE_CACHES; i++)
1799 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1800 /* three offsets that we *know* are line 0 at 0 and these two: */
1801 edit->line_numbers[1] = edit->curs_line;
1802 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1803 edit->line_numbers[2] = edit->total_lines;
1804 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1805 edit->caches_valid = 1;
1807 if (line >= edit->total_lines)
1808 return edit->line_offsets[2];
1809 if (line <= 0)
1810 return 0;
1811 /* find the closest known point */
1812 for (i = 0; i < N_LINE_CACHES; i++) {
1813 int n;
1814 n = abs (edit->line_numbers[i] - line);
1815 if (n < m) {
1816 m = n;
1817 j = i;
1820 if (m == 0)
1821 return edit->line_offsets[j]; /* know the offset exactly */
1822 if (m == 1 && j >= 3)
1823 i = j; /* one line different - caller might be looping, so stay in this cache */
1824 else
1825 i = 3 + (rand () % (N_LINE_CACHES - 3));
1826 if (line > edit->line_numbers[j])
1827 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1828 else
1829 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1830 edit->line_numbers[i] = line;
1831 return edit->line_offsets[i];
1834 int line_is_blank (WEdit * edit, long line)
1836 return is_blank (edit, edit_find_line (edit, line));
1839 /* moves up until a blank line is reached, or until just
1840 before a non-blank line is reached */
1841 static void
1842 edit_move_up_paragraph (WEdit * edit, int do_scroll)
1844 int i = 0;
1845 if (edit->curs_line > 1) {
1846 if (line_is_blank (edit, edit->curs_line)) {
1847 if (line_is_blank (edit, edit->curs_line - 1)) {
1848 for (i = edit->curs_line - 1; i; i--)
1849 if (!line_is_blank (edit, i)) {
1850 i++;
1851 break;
1853 } else {
1854 for (i = edit->curs_line - 1; i; i--)
1855 if (line_is_blank (edit, i))
1856 break;
1858 } else {
1859 for (i = edit->curs_line - 1; i; i--)
1860 if (line_is_blank (edit, i))
1861 break;
1864 edit_move_up (edit, edit->curs_line - i, do_scroll);
1867 /* moves down until a blank line is reached, or until just
1868 before a non-blank line is reached */
1869 static void
1870 edit_move_down_paragraph (WEdit * edit, int do_scroll)
1872 int i;
1873 if (edit->curs_line >= edit->total_lines - 1) {
1874 i = edit->total_lines;
1875 } else {
1876 if (line_is_blank (edit, edit->curs_line)) {
1877 if (line_is_blank (edit, edit->curs_line + 1)) {
1878 for (i = edit->curs_line + 1; i; i++)
1879 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1880 i--;
1881 break;
1883 } else {
1884 for (i = edit->curs_line + 1; i; i++)
1885 if (line_is_blank (edit, i) || i >= edit->total_lines)
1886 break;
1888 } else {
1889 for (i = edit->curs_line + 1; i; i++)
1890 if (line_is_blank (edit, i) || i >= edit->total_lines)
1891 break;
1894 edit_move_down (edit, i - edit->curs_line, do_scroll);
1897 static void edit_begin_page (WEdit *edit)
1899 edit_update_curs_row (edit);
1900 edit_move_up (edit, edit->curs_row, 0);
1903 static void edit_end_page (WEdit *edit)
1905 edit_update_curs_row (edit);
1906 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1910 /* goto beginning of text */
1911 static void edit_move_to_top (WEdit * edit)
1913 if (edit->curs_line) {
1914 edit_cursor_move (edit, -edit->curs1);
1915 edit_move_to_prev_col (edit, 0);
1916 edit->force |= REDRAW_PAGE;
1917 edit->search_start = 0;
1918 edit_update_curs_row(edit);
1923 /* goto end of text */
1924 static void edit_move_to_bottom (WEdit * edit)
1926 if (edit->curs_line < edit->total_lines) {
1927 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1928 edit->start_display = edit->last_byte;
1929 edit->start_line = edit->total_lines;
1930 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1931 edit->force |= REDRAW_PAGE;
1935 /* goto beginning of line */
1936 static void edit_cursor_to_bol (WEdit * edit)
1938 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1939 edit->search_start = edit->curs1;
1940 edit->prev_col = edit_get_col (edit);
1941 edit->over_col = 0;
1944 /* goto end of line */
1945 static void edit_cursor_to_eol (WEdit * edit)
1947 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1948 edit->search_start = edit->curs1;
1949 edit->prev_col = edit_get_col (edit);
1950 edit->over_col = 0;
1953 /* move cursor to line 'line' */
1954 void edit_move_to_line (WEdit * e, long line)
1956 if(line < e->curs_line)
1957 edit_move_up (e, e->curs_line - line, 0);
1958 else
1959 edit_move_down (e, line - e->curs_line, 0);
1960 edit_scroll_screen_over_cursor (e);
1963 /* scroll window so that first visible line is 'line' */
1964 void edit_move_display (WEdit * e, long line)
1966 if(line < e->start_line)
1967 edit_scroll_upward (e, e->start_line - line);
1968 else
1969 edit_scroll_downward (e, line - e->start_line);
1972 /* save markers onto undo stack */
1973 void edit_push_markers (WEdit * edit)
1975 edit_push_action (edit, MARK_1 + edit->mark1);
1976 edit_push_action (edit, MARK_2 + edit->mark2);
1979 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1981 edit->mark1 = m1;
1982 edit->mark2 = m2;
1983 edit->column1 = c1;
1984 edit->column2 = c2;
1988 /* highlight marker toggle */
1989 void edit_mark_cmd (WEdit * edit, int unmark)
1991 edit_push_markers (edit);
1992 if (unmark) {
1993 edit_set_markers (edit, 0, 0, 0, 0);
1994 edit->force |= REDRAW_PAGE;
1995 } else {
1996 if (edit->mark2 >= 0) {
1997 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
1998 edit->force |= REDRAW_PAGE;
1999 } else
2000 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2004 static unsigned long
2005 my_type_of (int c)
2007 int x, r = 0;
2008 const char *p, *q;
2009 const char option_chars_move_whole_word[] =
2010 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2012 if (!c)
2013 return 0;
2014 if (c == '!') {
2015 if (*option_chars_move_whole_word == '!')
2016 return 2;
2017 return 0x80000000UL;
2019 if (g_ascii_isupper ((gchar) c))
2020 c = 'A';
2021 else if (g_ascii_islower ((gchar) c))
2022 c = 'a';
2023 else if (g_ascii_isalpha (c))
2024 c = 'a';
2025 else if (isdigit (c))
2026 c = '0';
2027 else if (isspace (c))
2028 c = ' ';
2029 q = strchr (option_chars_move_whole_word, c);
2030 if (!q)
2031 return 0xFFFFFFFFUL;
2032 do {
2033 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2034 if (*p == '!')
2035 x <<= 1;
2036 r |= x;
2037 } while ((q = strchr (q + 1, c)));
2038 return r;
2041 static void
2042 edit_left_word_move (WEdit *edit, int s)
2044 for (;;) {
2045 int c1, c2;
2046 if (column_highlighting
2047 && edit->mark1 != edit->mark2
2048 && edit->over_col == 0
2049 && edit->curs1 == edit_bol(edit, edit->curs1))
2050 break;
2051 edit_cursor_move (edit, -1);
2052 if (!edit->curs1)
2053 break;
2054 c1 = edit_get_byte (edit, edit->curs1 - 1);
2055 c2 = edit_get_byte (edit, edit->curs1);
2056 if (!(my_type_of (c1) & my_type_of (c2)))
2057 break;
2058 if (isspace (c1) && !isspace (c2))
2059 break;
2060 if (s)
2061 if (!isspace (c1) && isspace (c2))
2062 break;
2066 static void edit_left_word_move_cmd (WEdit * edit)
2068 edit_left_word_move (edit, 0);
2069 edit->force |= REDRAW_PAGE;
2072 static void
2073 edit_right_word_move (WEdit *edit, int s)
2075 for (;;) {
2076 int c1, c2;
2077 if (column_highlighting
2078 && edit->mark1 != edit->mark2
2079 && edit->over_col == 0
2080 && edit->curs1 == edit_eol(edit, edit->curs1)
2082 break;
2083 edit_cursor_move (edit, 1);
2084 if (edit->curs1 >= edit->last_byte)
2085 break;
2086 c1 = edit_get_byte (edit, edit->curs1 - 1);
2087 c2 = edit_get_byte (edit, edit->curs1);
2088 if (!(my_type_of (c1) & my_type_of (c2)))
2089 break;
2090 if (isspace (c1) && !isspace (c2))
2091 break;
2092 if (s)
2093 if (!isspace (c1) && isspace (c2))
2094 break;
2098 static void edit_right_word_move_cmd (WEdit * edit)
2100 edit_right_word_move (edit, 0);
2101 edit->force |= REDRAW_PAGE;
2104 static void edit_right_char_move_cmd (WEdit * edit)
2106 int cw = 1;
2107 int c = 0;
2108 if ( edit->utf8 ) {
2109 c = edit_get_utf (edit, edit->curs1, &cw);
2110 if ( cw < 1 )
2111 cw = 1;
2112 } else {
2113 c = edit_get_byte (edit, edit->curs1);
2115 if (option_cursor_beyond_eol && c == '\n') {
2116 edit->over_col++;
2117 } else {
2118 edit_cursor_move (edit, cw);
2122 static void edit_left_char_move_cmd (WEdit * edit)
2124 int cw = 1;
2125 if (column_highlighting
2126 && option_cursor_beyond_eol
2127 && edit->mark1 != edit->mark2
2128 && edit->over_col == 0
2129 && edit->curs1 == edit_bol(edit, edit->curs1))
2130 return;
2131 if ( edit->utf8 ) {
2132 edit_get_prev_utf (edit, edit->curs1, &cw);
2133 if ( cw < 1 )
2134 cw = 1;
2136 if (option_cursor_beyond_eol && edit->over_col > 0) {
2137 edit->over_col--;
2138 } else {
2139 edit_cursor_move (edit, -cw);
2143 /** Up or down cursor moving.
2144 direction = TRUE - move up
2145 = FALSE - move down
2147 static void
2148 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2150 unsigned long p;
2151 unsigned long l = (direction)
2152 ? edit->curs_line
2153 : edit->total_lines - edit->curs_line;
2155 if (i > l)
2156 i = l;
2158 if (i == 0)
2159 return;
2161 if (i > 1)
2162 edit->force |= REDRAW_PAGE;
2163 if (do_scroll) {
2164 if (direction)
2165 edit_scroll_upward (edit, i);
2166 else
2167 edit_scroll_downward (edit, i);
2169 p = edit_bol (edit, edit->curs1);
2171 p = (direction)
2172 ? edit_move_backward (edit, p, i)
2173 : edit_move_forward (edit, p, i, 0);
2175 edit_cursor_move (edit, p - edit->curs1);
2177 edit_move_to_prev_col (edit, p);
2179 /* search start of current multibyte char (like CJK) */
2180 if (edit->curs1 + 1 < edit->last_byte) {
2181 edit_right_char_move_cmd (edit);
2182 edit_left_char_move_cmd (edit);
2185 edit->search_start = edit->curs1;
2186 edit->found_len = 0;
2189 static void edit_right_delete_word (WEdit * edit)
2191 int c1, c2;
2192 for (;;) {
2193 if (edit->curs1 >= edit->last_byte)
2194 break;
2195 c1 = edit_delete (edit, 1);
2196 c2 = edit_get_byte (edit, edit->curs1);
2197 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2198 break;
2199 if (!(my_type_of (c1) & my_type_of (c2)))
2200 break;
2204 static void edit_left_delete_word (WEdit * edit)
2206 int c1, c2;
2207 for (;;) {
2208 if (edit->curs1 <= 0)
2209 break;
2210 c1 = edit_backspace (edit, 1);
2211 c2 = edit_get_byte (edit, edit->curs1 - 1);
2212 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2213 break;
2214 if (!(my_type_of (c1) & my_type_of (c2)))
2215 break;
2220 the start column position is not recorded, and hence does not
2221 undo as it happed. But who would notice.
2223 static void
2224 edit_do_undo (WEdit * edit)
2226 long ac;
2227 long count = 0;
2229 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2230 edit->over_col = 0;
2231 while ((ac = pop_action (edit)) < KEY_PRESS) {
2232 switch ((int) ac) {
2233 case STACK_BOTTOM:
2234 goto done_undo;
2235 case CURS_RIGHT:
2236 edit_cursor_move (edit, 1);
2237 break;
2238 case CURS_LEFT:
2239 edit_cursor_move (edit, -1);
2240 break;
2241 case BACKSPACE:
2242 edit_backspace (edit, 1);
2243 break;
2244 case DELCHAR:
2245 edit_delete (edit, 1);
2246 break;
2247 case COLUMN_ON:
2248 column_highlighting = 1;
2249 break;
2250 case COLUMN_OFF:
2251 column_highlighting = 0;
2252 break;
2254 if (ac >= 256 && ac < 512)
2255 edit_insert_ahead (edit, ac - 256);
2256 if (ac >= 0 && ac < 256)
2257 edit_insert (edit, ac);
2259 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2260 edit->mark1 = ac - MARK_1;
2261 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2262 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2263 edit->mark2 = ac - MARK_2;
2264 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2266 if (count++)
2267 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2270 if (edit->start_display > ac - KEY_PRESS) {
2271 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2272 edit->force |= REDRAW_PAGE;
2273 } else if (edit->start_display < ac - KEY_PRESS) {
2274 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2275 edit->force |= REDRAW_PAGE;
2277 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2278 edit_update_curs_row (edit);
2280 done_undo:;
2281 edit->stack_disable = 0;
2284 static void edit_delete_to_line_end (WEdit * edit)
2286 while (edit_get_byte (edit, edit->curs1) != '\n') {
2287 if (!edit->curs2)
2288 break;
2289 edit_delete (edit, 1);
2293 static void edit_delete_to_line_begin (WEdit * edit)
2295 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2296 if (!edit->curs1)
2297 break;
2298 edit_backspace (edit, 1);
2302 void
2303 edit_delete_line (WEdit *edit)
2306 * Delete right part of the line.
2307 * Note that edit_get_byte() returns '\n' when byte position is
2308 * beyond EOF.
2310 while (edit_get_byte (edit, edit->curs1) != '\n') {
2311 (void) edit_delete (edit, 1);
2315 * Delete '\n' char.
2316 * Note that edit_delete() will not corrupt anything if called while
2317 * cursor position is EOF.
2319 (void) edit_delete (edit, 1);
2322 * Delete left part of the line.
2323 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2325 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2326 (void) edit_backspace (edit, 1);
2330 void insert_spaces_tab (WEdit * edit, int half)
2332 int i;
2333 edit_update_curs_col (edit);
2334 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2335 while (i > 0) {
2336 edit_insert (edit, ' ');
2337 i -= space_width;
2341 static int is_aligned_on_a_tab (WEdit * edit)
2343 edit_update_curs_col (edit);
2344 return !((edit->curs_col % (TAB_SIZE * space_width))
2345 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2348 static int right_of_four_spaces (WEdit *edit)
2350 int i, ch = 0;
2351 for (i = 1; i <= HALF_TAB_SIZE; i++)
2352 ch |= edit_get_byte (edit, edit->curs1 - i);
2353 if (ch == ' ')
2354 return is_aligned_on_a_tab (edit);
2355 return 0;
2358 static int left_of_four_spaces (WEdit *edit)
2360 int i, ch = 0;
2361 for (i = 0; i < HALF_TAB_SIZE; i++)
2362 ch |= edit_get_byte (edit, edit->curs1 + i);
2363 if (ch == ' ')
2364 return is_aligned_on_a_tab (edit);
2365 return 0;
2368 int edit_indent_width (WEdit * edit, long p)
2370 long q = p;
2371 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2372 q++;
2373 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2376 void edit_insert_indent (WEdit * edit, int indent)
2378 if (!option_fill_tabs_with_spaces) {
2379 while (indent >= TAB_SIZE) {
2380 edit_insert (edit, '\t');
2381 indent -= TAB_SIZE;
2384 while (indent-- > 0)
2385 edit_insert (edit, ' ');
2388 static void
2389 edit_auto_indent (WEdit * edit)
2391 long p;
2392 char c;
2393 p = edit->curs1;
2394 /* use the previous line as a template */
2395 p = edit_move_backward (edit, p, 1);
2396 /* copy the leading whitespace of the line */
2397 for (;;) { /* no range check - the line _is_ \n-terminated */
2398 c = edit_get_byte (edit, p++);
2399 if (c != ' ' && c != '\t')
2400 break;
2401 edit_insert (edit, c);
2405 static inline void
2406 edit_double_newline (WEdit * edit)
2408 edit_insert (edit, '\n');
2409 if (edit_get_byte (edit, edit->curs1) == '\n')
2410 return;
2411 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2412 return;
2413 edit->force |= REDRAW_PAGE;
2414 edit_insert (edit, '\n');
2417 static inline void
2418 edit_tab_cmd (WEdit * edit)
2420 int i;
2422 if (option_fake_half_tabs) {
2423 if (is_in_indent (edit)) {
2424 /*insert a half tab (usually four spaces) unless there is a
2425 half tab already behind, then delete it and insert a
2426 full tab. */
2427 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2428 for (i = 1; i <= HALF_TAB_SIZE; i++)
2429 edit_backspace (edit, 1);
2430 edit_insert (edit, '\t');
2431 } else {
2432 insert_spaces_tab (edit, 1);
2434 return;
2437 if (option_fill_tabs_with_spaces) {
2438 insert_spaces_tab (edit, 0);
2439 } else {
2440 edit_insert (edit, '\t');
2444 static void check_and_wrap_line (WEdit * edit)
2446 int curs, c;
2447 if (!option_typewriter_wrap)
2448 return;
2449 edit_update_curs_col (edit);
2450 if (edit->curs_col < option_word_wrap_line_length)
2451 return;
2452 curs = edit->curs1;
2453 for (;;) {
2454 curs--;
2455 c = edit_get_byte (edit, curs);
2456 if (c == '\n' || curs <= 0) {
2457 edit_insert (edit, '\n');
2458 return;
2460 if (c == ' ' || c == '\t') {
2461 int current = edit->curs1;
2462 edit_cursor_move (edit, curs - edit->curs1 + 1);
2463 edit_insert (edit, '\n');
2464 edit_cursor_move (edit, current - edit->curs1 + 1);
2465 return;
2470 static inline void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2472 void edit_push_key_press (WEdit * edit)
2474 edit_push_action (edit, KEY_PRESS + edit->start_display);
2475 if (edit->mark2 == -1)
2476 edit_push_action (edit, MARK_1 + edit->mark1);
2479 /* this find the matching bracket in either direction, and sets edit->bracket */
2480 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2482 const char * const b = "{}{[][()(", *p;
2483 int i = 1, a, inc = -1, c, d, n = 0;
2484 unsigned long j = 0;
2485 long q;
2486 edit_update_curs_row (edit);
2487 c = edit_get_byte (edit, edit->curs1);
2488 p = strchr (b, c);
2489 /* no limit */
2490 if (!furthest_bracket_search)
2491 furthest_bracket_search--;
2492 /* not on a bracket at all */
2493 if (!p)
2494 return -1;
2495 /* the matching bracket */
2496 d = p[1];
2497 /* going left or right? */
2498 if (strchr ("{[(", c))
2499 inc = 1;
2500 for (q = edit->curs1 + inc;; q += inc) {
2501 /* out of buffer? */
2502 if (q >= edit->last_byte || q < 0)
2503 break;
2504 a = edit_get_byte (edit, q);
2505 /* don't want to eat CPU */
2506 if (j++ > furthest_bracket_search)
2507 break;
2508 /* out of screen? */
2509 if (in_screen) {
2510 if (q < edit->start_display)
2511 break;
2512 /* count lines if searching downward */
2513 if (inc > 0 && a == '\n')
2514 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2515 break;
2517 /* count bracket depth */
2518 i += (a == c) - (a == d);
2519 /* return if bracket depth is zero */
2520 if (!i)
2521 return q;
2523 /* no match */
2524 return -1;
2527 static long last_bracket = -1;
2529 void edit_find_bracket (WEdit * edit)
2531 edit->bracket = edit_get_bracket (edit, 1, 10000);
2532 if (last_bracket != edit->bracket)
2533 edit->force |= REDRAW_PAGE;
2534 last_bracket = edit->bracket;
2537 static inline void
2538 edit_goto_matching_bracket (WEdit *edit)
2540 long q;
2542 q = edit_get_bracket (edit, 0, 0);
2543 if (q >= 0) {
2544 edit->bracket = edit->curs1;
2545 edit->force |= REDRAW_PAGE;
2546 edit_cursor_move (edit, q - edit->curs1);
2551 * This executes a command as though the user initiated it through a key
2552 * press. Callback with WIDGET_KEY as a message calls this after
2553 * translating the key press. This function can be used to pass any
2554 * command to the editor. Note that the screen wouldn't update
2555 * automatically. Either of command or char_for_insertion must be
2556 * passed as -1. Commands are executed, and char_for_insertion is
2557 * inserted at the cursor.
2559 void
2560 edit_execute_key_command (WEdit *edit, unsigned long command, int char_for_insertion)
2562 if (command == CK_Begin_Record_Macro) {
2563 edit->macro_i = 0;
2564 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2565 return;
2567 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2568 edit->force |= REDRAW_COMPLETELY;
2569 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2570 edit->macro_i = -1;
2571 return;
2573 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2574 edit->macro[edit->macro_i].command = command;
2575 edit->macro[edit->macro_i++].ch = char_for_insertion;
2577 /* record the beginning of a set of editing actions initiated by a key press */
2578 if (command != CK_Undo && command != CK_Ext_Mode)
2579 edit_push_key_press (edit);
2581 edit_execute_cmd (edit, command, char_for_insertion);
2582 if (column_highlighting)
2583 edit->force |= REDRAW_PAGE;
2586 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2589 This executes a command at a lower level than macro recording.
2590 It also does not push a key_press onto the undo stack. This means
2591 that if it is called many times, a single undo command will undo
2592 all of them. It also does not check for the Undo command.
2594 void
2595 edit_execute_cmd (WEdit *edit, unsigned long command, int char_for_insertion)
2597 edit->force |= REDRAW_LINE;
2599 /* The next key press will unhighlight the found string, so update
2600 * the whole page */
2601 if (edit->found_len || column_highlighting)
2602 edit->force |= REDRAW_PAGE;
2604 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2605 column_highlighting = 0;
2606 if (!edit->highlight
2607 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2608 edit_mark_cmd (edit, 1); /* clear */
2609 edit_mark_cmd (edit, 0); /* marking on */
2611 edit->highlight = 1;
2612 } else { /* any other command */
2613 if (edit->highlight)
2614 edit_mark_cmd (edit, 0); /* clear */
2615 edit->highlight = 0;
2618 /* first check for undo */
2619 if (command == CK_Undo) {
2620 edit_do_undo (edit);
2621 edit->found_len = 0;
2622 edit->prev_col = edit_get_col (edit);
2623 edit->search_start = edit->curs1;
2624 return;
2627 /* An ordinary key press */
2628 if (char_for_insertion >= 0) {
2629 if (edit->overwrite) {
2630 /* remove char only one time, after input first byte, multibyte chars */
2631 if ((!utf8_display || edit->charpoint == 0) && edit_get_byte (edit, edit->curs1) != '\n')
2632 edit_delete (edit, 0);
2634 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2635 edit_insert_over (edit);
2636 #ifdef HAVE_CHARSET
2637 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2638 unsigned char str[6 + 1];
2639 size_t i = 0;
2640 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2641 if ( res == 0 ) {
2642 str[0] = '.';
2643 str[1] = '\0';
2644 } else {
2645 str[res] = '\0';
2647 while ( str[i] != 0 && i<=6) {
2648 char_for_insertion = str[i];
2649 edit_insert (edit, char_for_insertion);
2650 i++;
2652 } else
2653 #endif
2654 edit_insert (edit, char_for_insertion);
2656 if (option_auto_para_formatting) {
2657 format_paragraph (edit, 0);
2658 edit->force |= REDRAW_PAGE;
2659 } else
2660 check_and_wrap_line (edit);
2661 edit->found_len = 0;
2662 edit->prev_col = edit_get_col (edit);
2663 edit->search_start = edit->curs1;
2664 edit_find_bracket (edit);
2665 return;
2668 switch (command) {
2669 case CK_Begin_Page:
2670 case CK_End_Page:
2671 case CK_Begin_Page_Highlight:
2672 case CK_End_Page_Highlight:
2673 case CK_Word_Left:
2674 case CK_Word_Right:
2675 case CK_Up:
2676 case CK_Down:
2677 case CK_Left:
2678 case CK_Right:
2679 if ( edit->mark2 >= 0 ) {
2680 if ( !option_persistent_selections ) {
2681 if (column_highlighting)
2682 edit_push_action (edit, COLUMN_ON);
2683 column_highlighting = 0;
2684 edit_mark_cmd (edit, 1);
2689 switch (command) {
2690 case CK_Begin_Page:
2691 case CK_End_Page:
2692 case CK_Begin_Page_Highlight:
2693 case CK_End_Page_Highlight:
2694 case CK_Word_Left:
2695 case CK_Word_Right:
2696 case CK_Up:
2697 case CK_Down:
2698 case CK_Word_Left_Highlight:
2699 case CK_Word_Right_Highlight:
2700 case CK_Up_Highlight:
2701 case CK_Down_Highlight:
2702 case CK_Up_Alt_Highlight:
2703 case CK_Down_Alt_Highlight:
2704 if (edit->mark2 == -1)
2705 break; /*marking is following the cursor: may need to highlight a whole line */
2706 case CK_Left:
2707 case CK_Right:
2708 case CK_Left_Highlight:
2709 case CK_Right_Highlight:
2710 edit->force |= REDRAW_CHAR_ONLY;
2713 /* basic cursor key commands */
2714 switch (command) {
2715 case CK_BackSpace:
2716 /* if non persistent selection and text selected */
2717 if ( !option_persistent_selections ) {
2718 if ( edit->mark1 != edit->mark2 ) {
2719 edit_block_delete_cmd (edit);
2720 break;
2723 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2724 edit->over_col--;
2725 break;
2727 if (option_backspace_through_tabs && is_in_indent (edit)) {
2728 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2729 && edit->curs1 > 0)
2730 edit_backspace (edit, 1);
2731 break;
2732 } else {
2733 if (option_fake_half_tabs) {
2734 int i;
2735 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2736 for (i = 0; i < HALF_TAB_SIZE; i++)
2737 edit_backspace (edit, 1);
2738 break;
2742 edit_backspace (edit, 0);
2743 break;
2744 case CK_Delete:
2745 /* if non persistent selection and text selected */
2746 if ( !option_persistent_selections ) {
2747 if ( edit->mark1 != edit->mark2 ) {
2748 edit_block_delete_cmd (edit);
2749 break;
2753 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2754 edit_insert_over (edit);
2756 if (option_fake_half_tabs) {
2757 int i;
2758 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2759 for (i = 1; i <= HALF_TAB_SIZE; i++)
2760 edit_delete (edit, 1);
2761 break;
2764 edit_delete (edit, 0);
2765 break;
2766 case CK_Delete_Word_Left:
2767 edit->over_col = 0;
2768 edit_left_delete_word (edit);
2769 break;
2770 case CK_Delete_Word_Right:
2771 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2772 edit_insert_over (edit);
2774 edit_right_delete_word (edit);
2775 break;
2776 case CK_Delete_Line:
2777 edit_delete_line (edit);
2778 break;
2779 case CK_Delete_To_Line_End:
2780 edit_delete_to_line_end (edit);
2781 break;
2782 case CK_Delete_To_Line_Begin:
2783 edit_delete_to_line_begin (edit);
2784 break;
2785 case CK_Enter:
2786 edit->over_col = 0;
2787 if (option_auto_para_formatting) {
2788 edit_double_newline (edit);
2789 if (option_return_does_auto_indent)
2790 edit_auto_indent (edit);
2791 format_paragraph (edit, 0);
2792 } else {
2793 edit_insert (edit, '\n');
2794 if (option_return_does_auto_indent) {
2795 edit_auto_indent (edit);
2798 break;
2799 case CK_Return:
2800 edit_insert (edit, '\n');
2801 break;
2803 case CK_Page_Up_Alt_Highlight:
2804 column_highlighting = 1;
2805 case CK_Page_Up:
2806 case CK_Page_Up_Highlight:
2807 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2808 break;
2809 case CK_Page_Down_Alt_Highlight:
2810 column_highlighting = 1;
2811 case CK_Page_Down:
2812 case CK_Page_Down_Highlight:
2813 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2814 break;
2815 case CK_Left_Alt_Highlight:
2816 column_highlighting = 1;
2817 case CK_Left:
2818 case CK_Left_Highlight:
2819 if (option_fake_half_tabs) {
2820 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2821 if ( option_cursor_beyond_eol && edit->over_col > 0)
2822 edit->over_col--;
2823 else
2824 edit_cursor_move (edit, -HALF_TAB_SIZE);
2825 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2826 break;
2829 edit_left_char_move_cmd (edit);
2830 break;
2831 case CK_Right_Alt_Highlight:
2832 column_highlighting = 1;
2833 case CK_Right:
2834 case CK_Right_Highlight:
2835 if (option_fake_half_tabs) {
2836 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2837 edit_cursor_move (edit, HALF_TAB_SIZE);
2838 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2839 break;
2842 edit_right_char_move_cmd (edit);
2843 break;
2844 case CK_Begin_Page:
2845 case CK_Begin_Page_Highlight:
2846 edit_begin_page (edit);
2847 break;
2848 case CK_End_Page:
2849 case CK_End_Page_Highlight:
2850 edit_end_page (edit);
2851 break;
2852 case CK_Word_Left:
2853 case CK_Word_Left_Highlight:
2854 edit->over_col = 0;
2855 edit_left_word_move_cmd (edit);
2856 break;
2857 case CK_Word_Right:
2858 case CK_Word_Right_Highlight:
2859 edit->over_col = 0;
2860 edit_right_word_move_cmd (edit);
2861 break;
2862 case CK_Up_Alt_Highlight:
2863 column_highlighting = 1;
2864 case CK_Up:
2865 case CK_Up_Highlight:
2866 edit_move_up (edit, 1, 0);
2867 break;
2868 case CK_Down_Alt_Highlight:
2869 column_highlighting = 1;
2870 case CK_Down:
2871 case CK_Down_Highlight:
2872 edit_move_down (edit, 1, 0);
2873 break;
2874 case CK_Paragraph_Up_Alt_Highlight:
2875 column_highlighting = 1;
2876 case CK_Paragraph_Up:
2877 case CK_Paragraph_Up_Highlight:
2878 edit_move_up_paragraph (edit, 0);
2879 break;
2880 case CK_Paragraph_Down_Alt_Highlight:
2881 column_highlighting = 1;
2882 case CK_Paragraph_Down:
2883 case CK_Paragraph_Down_Highlight:
2884 edit_move_down_paragraph (edit, 0);
2885 break;
2886 case CK_Scroll_Up_Alt_Highlight:
2887 column_highlighting = 1;
2888 case CK_Scroll_Up:
2889 case CK_Scroll_Up_Highlight:
2890 edit_move_up (edit, 1, 1);
2891 break;
2892 case CK_Scroll_Down_Alt_Highlight:
2893 column_highlighting = 1;
2894 case CK_Scroll_Down:
2895 case CK_Scroll_Down_Highlight:
2896 edit_move_down (edit, 1, 1);
2897 break;
2898 case CK_Home:
2899 case CK_Home_Highlight:
2900 edit_cursor_to_bol (edit);
2901 break;
2902 case CK_End:
2903 case CK_End_Highlight:
2904 edit_cursor_to_eol (edit);
2905 break;
2906 case CK_Tab:
2907 /* if text marked shift block */
2908 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2909 if (edit->mark2 < 0)
2910 edit_mark_cmd (edit, 0);
2911 edit_move_block_to_right (edit);
2912 } else {
2913 if ( option_cursor_beyond_eol )
2914 edit_insert_over (edit);
2915 edit_tab_cmd (edit);
2916 if (option_auto_para_formatting) {
2917 format_paragraph (edit, 0);
2918 edit->force |= REDRAW_PAGE;
2919 } else {
2920 check_and_wrap_line (edit);
2923 break;
2925 case CK_Toggle_Insert:
2926 edit->overwrite = (edit->overwrite == 0);
2927 break;
2929 case CK_Mark:
2930 if (edit->mark2 >= 0) {
2931 if (column_highlighting)
2932 edit_push_action (edit, COLUMN_ON);
2933 column_highlighting = 0;
2935 edit_mark_cmd (edit, 0);
2936 break;
2937 case CK_Column_Mark:
2938 if (!column_highlighting)
2939 edit_push_action (edit, COLUMN_OFF);
2940 column_highlighting = 1;
2941 edit_mark_cmd (edit, 0);
2942 break;
2943 case CK_Mark_All:
2944 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
2945 edit->force |= REDRAW_PAGE;
2946 break;
2947 case CK_Unmark:
2948 if (column_highlighting)
2949 edit_push_action (edit, COLUMN_ON);
2950 column_highlighting = 0;
2951 edit_mark_cmd (edit, 1);
2952 break;
2954 case CK_Toggle_Line_State:
2955 option_line_state = !option_line_state;
2956 if ( option_line_state ) {
2957 option_line_state_width = LINE_STATE_WIDTH;
2958 } else {
2959 option_line_state_width = 0;
2961 edit->force |= REDRAW_PAGE;
2962 break;
2964 case CK_Toggle_Bookmark:
2965 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2966 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2967 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2968 else
2969 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2970 break;
2971 case CK_Flush_Bookmarks:
2972 book_mark_flush (edit, BOOK_MARK_COLOR);
2973 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2974 edit->force |= REDRAW_PAGE;
2975 break;
2976 case CK_Next_Bookmark:
2977 if (edit->book_mark) {
2978 struct _book_mark *p;
2979 p = (struct _book_mark *) book_mark_find (edit,
2980 edit->curs_line);
2981 if (p->next) {
2982 p = p->next;
2983 if (p->line >= edit->start_line + edit->num_widget_lines
2984 || p->line < edit->start_line)
2985 edit_move_display (edit,
2986 p->line -
2987 edit->num_widget_lines / 2);
2988 edit_move_to_line (edit, p->line);
2991 break;
2992 case CK_Prev_Bookmark:
2993 if (edit->book_mark) {
2994 struct _book_mark *p;
2995 p = (struct _book_mark *) book_mark_find (edit,
2996 edit->curs_line);
2997 while (p->line == edit->curs_line)
2998 if (p->prev)
2999 p = p->prev;
3000 if (p->line >= 0) {
3001 if (p->line >= edit->start_line + edit->num_widget_lines
3002 || p->line < edit->start_line)
3003 edit_move_display (edit,
3004 p->line -
3005 edit->num_widget_lines / 2);
3006 edit_move_to_line (edit, p->line);
3009 break;
3011 case CK_Beginning_Of_Text:
3012 case CK_Beginning_Of_Text_Highlight:
3013 edit_move_to_top (edit);
3014 break;
3015 case CK_End_Of_Text:
3016 case CK_End_Of_Text_Highlight:
3017 edit_move_to_bottom (edit);
3018 break;
3020 case CK_Copy:
3021 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3022 edit_insert_over (edit);
3023 edit_block_copy_cmd (edit);
3024 break;
3025 case CK_Remove:
3026 edit_block_delete_cmd (edit);
3027 break;
3028 case CK_Move:
3029 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3030 edit_insert_over (edit);
3031 edit_block_move_cmd (edit);
3032 break;
3034 case CK_Shift_Block_Left:
3035 if (edit->mark1 != edit->mark2)
3036 edit_move_block_to_left (edit);
3037 break;
3038 case CK_Shift_Block_Right:
3039 if (edit->mark1 != edit->mark2)
3040 edit_move_block_to_right (edit);
3041 break;
3042 case CK_XStore:
3043 edit_copy_to_X_buf_cmd (edit);
3044 break;
3045 case CK_XCut:
3046 edit_cut_to_X_buf_cmd (edit);
3047 break;
3048 case CK_XPaste:
3049 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3050 edit_insert_over (edit);
3051 edit_paste_from_X_buf_cmd (edit);
3052 break;
3053 case CK_Selection_History:
3054 edit_paste_from_history (edit);
3055 break;
3057 case CK_Save_As:
3058 edit_save_as_cmd (edit);
3059 break;
3060 case CK_Save:
3061 edit_save_confirm_cmd (edit);
3062 break;
3063 case CK_Load:
3064 edit_load_cmd (edit, EDIT_FILE_COMMON);
3065 break;
3066 case CK_Save_Block:
3067 edit_save_block_cmd (edit);
3068 break;
3069 case CK_Insert_File:
3070 edit_insert_file_cmd (edit);
3071 break;
3073 case CK_Load_Prev_File:
3074 edit_load_back_cmd (edit);
3075 break;
3076 case CK_Load_Next_File:
3077 edit_load_forward_cmd (edit);
3078 break;
3080 case CK_Load_Syntax_File:
3081 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3082 break;
3083 case CK_Choose_Syntax:
3084 edit_syntax_dialog ();
3085 break;
3087 case CK_Load_Menu_File:
3088 edit_load_cmd (edit, EDIT_FILE_MENU);
3089 break;
3091 case CK_Toggle_Syntax:
3092 if ((option_syntax_highlighting ^= 1) == 1)
3093 edit_load_syntax (edit, NULL, option_syntax_type);
3094 edit->force |= REDRAW_PAGE;
3095 break;
3097 case CK_Toggle_Tab_TWS:
3098 enable_show_tabs_tws ^= 1;
3099 edit->force |= REDRAW_PAGE;
3100 break;
3102 case CK_Find:
3103 edit_search_cmd (edit, 0);
3104 break;
3105 case CK_Find_Again:
3106 edit_search_cmd (edit, 1);
3107 break;
3108 case CK_Replace:
3109 edit_replace_cmd (edit, 0);
3110 break;
3111 case CK_Replace_Again:
3112 edit_replace_cmd (edit, 1);
3113 break;
3114 case CK_Complete_Word:
3115 /* if text marked shift block */
3116 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3117 edit_move_block_to_left (edit);
3118 } else {
3119 edit_complete_word_cmd (edit);
3121 break;
3122 case CK_Find_Definition:
3123 edit_get_match_keyword_cmd (edit);
3124 break;
3125 case CK_Quit:
3126 dlg_stop (edit->widget.parent);
3127 break;
3128 case CK_New:
3129 edit_new_cmd (edit);
3130 break;
3131 case CK_Help:
3132 edit_help_cmd (edit);
3133 break;
3134 case CK_Refresh:
3135 edit_refresh_cmd (edit);
3136 break;
3137 case CK_SaveSetupCmd:
3138 save_setup_cmd ();
3139 break;
3140 case CK_About:
3141 query_dialog (_(" About "),
3142 _("\n Cooledit v3.11.5\n\n"
3143 " Copyright (C) 1996 the Free Software Foundation\n\n"
3144 " A user friendly text editor written\n"
3145 " for the Midnight Commander.\n"), D_NORMAL,
3146 1, _("&OK"));
3147 break;
3148 case CK_LearnKeys:
3149 learn_keys ();
3150 break;
3151 case CK_Edit_Options:
3152 edit_options_dialog ();
3153 break;
3154 case CK_Edit_Save_Mode:
3155 menu_save_mode_cmd ();
3156 break;
3157 case CK_Date:
3159 char s[BUF_MEDIUM];
3160 /* fool gcc to prevent a Y2K warning */
3161 char time_format[] = "_c";
3162 time_format[0] = '%';
3164 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3165 edit_print_string (edit, s);
3166 edit->force |= REDRAW_PAGE;
3167 break;
3169 break;
3170 case CK_Goto:
3171 edit_goto_cmd (edit);
3172 break;
3173 case CK_Paragraph_Format:
3174 format_paragraph (edit, 1);
3175 edit->force |= REDRAW_PAGE;
3176 break;
3177 case CK_Delete_Macro:
3178 edit_delete_macro_cmd (edit);
3179 break;
3180 case CK_Match_Bracket:
3181 edit_goto_matching_bracket (edit);
3182 break;
3183 case CK_User_Menu:
3184 user_menu (edit);
3185 break;
3186 case CK_Sort:
3187 edit_sort_cmd (edit);
3188 break;
3189 case CK_ExtCmd:
3190 edit_ext_cmd (edit);
3191 break;
3192 case CK_Mail:
3193 edit_mail_dialog (edit);
3194 break;
3195 case CK_Shell:
3196 view_other_cmd ();
3197 break;
3198 case CK_SelectCodepage:
3199 edit_select_codepage_cmd (edit);
3200 break;
3201 case CK_Insert_Literal:
3202 edit_insert_literal_cmd (edit);
3203 break;
3204 case CK_Execute_Macro:
3205 edit_execute_macro_cmd (edit);
3206 break;
3207 case CK_Begin_End_Macro:
3208 edit_begin_end_macro_cmd (edit);
3209 break;
3210 case CK_Ext_Mode:
3211 edit->extmod = 1;
3212 break;
3213 default:
3214 break;
3217 /* CK_Pipe_Block */
3218 if ((command / 1000) == 1) /* a shell command */
3219 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3220 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3221 struct macro m[MAX_MACRO_LENGTH];
3222 int nm;
3223 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3224 edit_execute_macro (edit, m, nm);
3227 /* keys which must set the col position, and the search vars */
3228 switch (command) {
3229 case CK_Find:
3230 case CK_Find_Again:
3231 case CK_Replace:
3232 case CK_Replace_Again:
3233 case CK_Complete_Word:
3234 edit->prev_col = edit_get_col (edit);
3235 break;
3236 case CK_Up:
3237 case CK_Up_Highlight:
3238 case CK_Up_Alt_Highlight:
3239 case CK_Down:
3240 case CK_Down_Highlight:
3241 case CK_Down_Alt_Highlight:
3242 case CK_Page_Up:
3243 case CK_Page_Up_Highlight:
3244 case CK_Page_Up_Alt_Highlight:
3245 case CK_Page_Down:
3246 case CK_Page_Down_Highlight:
3247 case CK_Page_Down_Alt_Highlight:
3248 case CK_Beginning_Of_Text:
3249 case CK_Beginning_Of_Text_Highlight:
3250 case CK_End_Of_Text:
3251 case CK_End_Of_Text_Highlight:
3252 case CK_Paragraph_Up:
3253 case CK_Paragraph_Up_Highlight:
3254 case CK_Paragraph_Up_Alt_Highlight:
3255 case CK_Paragraph_Down:
3256 case CK_Paragraph_Down_Highlight:
3257 case CK_Paragraph_Down_Alt_Highlight:
3258 case CK_Scroll_Up:
3259 case CK_Scroll_Up_Highlight:
3260 case CK_Scroll_Up_Alt_Highlight:
3261 case CK_Scroll_Down:
3262 case CK_Scroll_Down_Highlight:
3263 case CK_Scroll_Down_Alt_Highlight:
3264 edit->search_start = edit->curs1;
3265 edit->found_len = 0;
3266 break;
3267 default:
3268 edit->found_len = 0;
3269 edit->prev_col = edit_get_col (edit);
3270 edit->search_start = edit->curs1;
3272 edit_find_bracket (edit);
3274 if (option_auto_para_formatting) {
3275 switch (command) {
3276 case CK_BackSpace:
3277 case CK_Delete:
3278 case CK_Delete_Word_Left:
3279 case CK_Delete_Word_Right:
3280 case CK_Delete_To_Line_End:
3281 case CK_Delete_To_Line_Begin:
3282 format_paragraph (edit, 0);
3283 edit->force |= REDRAW_PAGE;
3289 static void
3290 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3292 int i = 0;
3294 if (edit->macro_depth++ > 256) {
3295 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3296 edit->macro_depth--;
3297 return;
3299 edit->force |= REDRAW_PAGE;
3300 for (; i < n; i++) {
3301 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3303 edit_update_screen (edit);
3304 edit->macro_depth--;
3307 /* User edit menu, like user menu (F2) but only in editor. */
3308 static void
3309 user_menu (WEdit * edit)
3311 char *block_file;
3312 int nomark;
3313 long start_mark, end_mark;
3314 struct stat status;
3316 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3318 nomark = eval_marks (edit, &start_mark, &end_mark);
3319 if (nomark == 0)
3320 edit_save_block (edit, block_file, start_mark, end_mark);
3322 /* run shell scripts from menu */
3323 user_menu_cmd (edit);
3325 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0)) {
3326 int rc = 0;
3327 FILE *fd;
3329 if (nomark == 0) {
3330 /* i.e. we have marked block */
3331 rc = edit_block_delete_cmd (edit);
3334 if (rc == 0)
3335 edit_insert_file (edit, block_file);
3337 /* truncate block file */
3338 fd = fopen (block_file, "w");
3339 if (fd != NULL)
3340 fclose (fd);
3342 edit_refresh_cmd (edit);
3343 edit->force |= REDRAW_COMPLETELY;
3345 g_free (block_file);
3348 void
3349 edit_stack_init (void)
3351 for (edit_stack_iterator = 0;
3352 edit_stack_iterator < MAX_HISTORY_MOVETO;
3353 edit_stack_iterator++ ) {
3354 edit_history_moveto[edit_stack_iterator].filename = NULL;
3355 edit_history_moveto[edit_stack_iterator].line = -1;
3358 edit_stack_iterator = 0;
3361 void
3362 edit_stack_free (void)
3364 for (edit_stack_iterator = 0;
3365 edit_stack_iterator < MAX_HISTORY_MOVETO;
3366 edit_stack_iterator++)
3367 g_free (edit_history_moveto[edit_stack_iterator].filename);
3370 /* move i lines */
3371 void
3372 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3374 edit_move_updown (edit, i, do_scroll, TRUE);
3377 /* move i lines */
3378 void
3379 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3381 edit_move_updown (edit, i, do_scroll, FALSE);