Merge branch '1618_rpm_spec_fix'
[midnight-commander.git] / edit / edit.c
blob1384da5efd8a6277d43aa960698d6e7c147f70ee
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_fill_tabs_with_spaces = 0;
72 int option_return_does_auto_indent = 1;
73 int option_backspace_through_tabs = 0;
74 int option_fake_half_tabs = 1;
75 int option_save_mode = EDIT_QUICK_SAVE;
76 int option_save_position = 1;
77 int option_max_undo = 32768;
78 int option_persistent_selections = 1;
79 int option_cursor_beyond_eol = 1;
80 int option_line_state = 0;
81 int option_line_state_width = 0;
83 int option_edit_right_extreme = 0;
84 int option_edit_left_extreme = 0;
85 int option_edit_top_extreme = 0;
86 int option_edit_bottom_extreme = 0;
87 int enable_show_tabs_tws = 1;
89 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext = NULL;
92 int edit_stack_iterator = 0;
93 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
96 /*-
98 * here's a quick sketch of the layout: (don't run this through indent.)
100 * (b1 is buffers1 and b2 is buffers2)
103 * \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
104 * ______________________________________|______________________________________
106 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
107 * |-> |-> |-> |-> |-> |-> |
109 * _<------------------------->|<----------------->_
110 * WEdit->curs2 | WEdit->curs1
111 * ^ | ^
112 * | ^|^ |
113 * cursor ||| cursor
114 * |||
115 * file end|||file beginning
120 * This_is_some_file
121 * fin.
125 static void user_menu (WEdit *edit);
127 int edit_get_byte (WEdit * edit, long byte_index)
129 unsigned long p;
130 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
131 return '\n';
133 if (byte_index >= edit->curs1) {
134 p = edit->curs1 + edit->curs2 - byte_index - 1;
135 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
136 } else {
137 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
141 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
143 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 -= 1;
164 if ( byte_index < 0 ) {
165 return NULL;
168 if (byte_index >= edit->curs1) {
169 p = edit->curs1 + edit->curs2 - 1;
170 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
171 } else {
172 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
176 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
178 gchar *str = NULL;
179 int res = -1;
180 gunichar ch;
181 gchar *next_ch = NULL;
182 int width = 0;
184 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
185 *char_width = 0;
186 return '\n';
190 str = edit_get_byte_ptr (edit, byte_index);
191 res = g_utf8_get_char_validated (str, -1);
193 if ( res < 0 ) {
194 ch = *str;
195 width = 0;
196 } else {
197 ch = res;
198 /* Calculate UTF-8 char width */
199 next_ch = g_utf8_next_char(str);
200 if ( next_ch ) {
201 width = next_ch - str;
202 } else {
203 ch = 0;
204 width = 0;
207 *char_width = width;
208 return ch;
211 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
213 gchar *str, *buf = NULL;
214 int res = -1;
215 gunichar ch;
216 gchar *next_ch = NULL;
217 int width = 0;
219 if ( byte_index > 0 ) {
220 byte_index--;
223 ch = edit_get_utf (edit, byte_index, &width);
224 if ( width == 1 ) {
225 *char_width = width;
226 return ch;
229 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
230 *char_width = 0;
231 return 0;
234 str = edit_get_byte_ptr (edit, byte_index);
235 buf = edit_get_buf_ptr (edit, byte_index);
236 /* get prev utf8 char */
237 if ( str != buf )
238 str = g_utf8_find_prev_char (buf, str);
240 res = g_utf8_get_char_validated (str, -1);
241 if ( res < 0 ) {
242 ch = *str;
243 width = 0;
244 } else {
245 ch = res;
246 /* Calculate UTF-8 char width */
247 next_ch = g_utf8_next_char(str);
248 if ( next_ch ) {
249 width = next_ch - str;
250 } else {
251 ch = 0;
252 width = 0;
255 *char_width = width;
256 return ch;
260 * Initialize the buffers for an empty files.
262 static void
263 edit_init_buffers (WEdit *edit)
265 int j;
267 for (j = 0; j <= MAXBUFF; j++) {
268 edit->buffers1[j] = NULL;
269 edit->buffers2[j] = NULL;
272 edit->curs1 = 0;
273 edit->curs2 = 0;
274 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
278 * Load file OR text into buffers. Set cursor to the beginning of file.
279 * Return 1 on error.
281 static int
282 edit_load_file_fast (WEdit *edit, const char *filename)
284 long buf, buf2;
285 int file = -1;
286 edit->curs2 = edit->last_byte;
287 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
288 edit->utf8 = 0;
289 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
290 GString *errmsg = g_string_new(NULL);
291 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
292 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
293 g_string_free (errmsg, TRUE);
294 return 1;
297 if (!edit->buffers2[buf2])
298 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
300 mc_read (file,
301 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
302 (edit->curs2 & M_EDIT_BUF_SIZE),
303 edit->curs2 & M_EDIT_BUF_SIZE);
305 for (buf = buf2 - 1; buf >= 0; buf--) {
306 /* edit->buffers2[0] is already allocated */
307 if (!edit->buffers2[buf])
308 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
309 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
312 mc_close (file);
313 return 0;
316 /* detecting an error on save is easy: just check if every byte has been written. */
317 /* detecting an error on read, is not so easy 'cos there is not way to tell
318 whether you read everything or not. */
319 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
320 static const struct edit_filters {
321 const char *read, *write, *extension;
322 } all_filters[] = {
323 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
324 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
325 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
326 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
327 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
330 /* Return index of the filter or -1 is there is no appropriate filter */
331 static int edit_find_filter (const char *filename)
333 size_t i, l, e;
334 if (!filename)
335 return -1;
336 l = strlen (filename);
337 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
338 e = strlen (all_filters[i].extension);
339 if (l > e)
340 if (!strcmp (all_filters[i].extension, filename + l - e))
341 return i;
343 return -1;
346 static char *
347 edit_get_filter (const char *filename)
349 int i, l;
350 char *p, *quoted_name;
351 i = edit_find_filter (filename);
352 if (i < 0)
353 return 0;
354 quoted_name = name_quote (filename, 0);
355 l = str_term_width1 (quoted_name);
356 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
357 sprintf (p, all_filters[i].read, quoted_name);
358 g_free (quoted_name);
359 return p;
362 char *
363 edit_get_write_filter (const char *write_name, const char *filename)
365 int i, l;
366 char *p, *writename;
367 i = edit_find_filter (filename);
368 if (i < 0)
369 return 0;
370 writename = name_quote (write_name, 0);
371 l = str_term_width1 (writename);
372 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
373 sprintf (p, all_filters[i].write, writename);
374 g_free (writename);
375 return p;
378 static long
379 edit_insert_stream (WEdit * edit, FILE * f)
381 int c;
382 long i = 0;
383 while ((c = fgetc (f)) >= 0) {
384 edit_insert (edit, c);
385 i++;
387 return i;
390 long edit_write_stream (WEdit * edit, FILE * f)
392 long i;
394 if (edit->lb == LB_ASIS) {
395 for (i = 0; i < edit->last_byte; i++)
396 if (fputc (edit_get_byte (edit, i), f) < 0)
397 break;
398 return i;
401 /* change line breaks */
402 for (i = 0; i < edit->last_byte; i++) {
403 unsigned char c = edit_get_byte (edit, i);
405 if (!(c == '\n' || c == '\r')) {
406 /* not line break */
407 if (fputc (c, f) < 0)
408 return i;
409 } else { /* (c == '\n' || c == '\r') */
410 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
412 switch (edit->lb) {
413 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
414 /* put one line break unconditionally */
415 if (fputc ('\n', f) < 0)
416 return i;
418 i++; /* 2 chars are processed */
420 if (c == '\r' && c1 == '\n')
421 /* Windows line break; go to the next char */
422 break;
424 if (c == '\r' && c1 == '\r') {
425 /* two Macintosh line breaks; put second line break */
426 if (fputc ('\n', f) < 0)
427 return i;
428 break;
431 if (fputc (c1, f) < 0)
432 return i;
433 break;
435 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
436 /* put one line break unconditionally */
437 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
438 return i;
440 if (c == '\r' && c1 == '\n')
441 /* Windows line break; go to the next char */
442 i++;
443 break;
445 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
446 /* put one line break unconditionally */
447 if (fputc ('\r', f) < 0)
448 return i;
450 i++; /* 2 chars are processed */
452 if (c == '\r' && c1 == '\n')
453 /* Windows line break; go to the next char */
454 break;
456 if (c == '\n' && c1 == '\n') {
457 /* two Windows line breaks; put second line break */
458 if (fputc ('\r', f) < 0)
459 return i;
460 break;
463 if (fputc (c1, f) < 0)
464 return i;
465 break;
466 case LB_ASIS: /* default without changes */
467 break;
472 return edit->last_byte;
475 #define TEMP_BUF_LEN 1024
477 /* inserts a file at the cursor, returns 1 on success */
479 edit_insert_file (WEdit *edit, const char *filename)
481 char *p;
482 if ((p = edit_get_filter (filename))) {
483 FILE *f;
484 long current = edit->curs1;
485 f = (FILE *) popen (p, "r");
486 if (f) {
487 edit_insert_stream (edit, f);
488 edit_cursor_move (edit, current - edit->curs1);
489 if (pclose (f) > 0) {
490 GString *errmsg = g_string_new (NULL);
491 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
492 edit_error_dialog (_("Error"), errmsg->str);
493 g_string_free (errmsg, TRUE);
494 g_free (p);
495 return 0;
497 } else {
498 GString *errmsg = g_string_new (NULL);
499 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
500 edit_error_dialog (_("Error"), errmsg->str);
501 g_string_free (errmsg, TRUE);
502 g_free (p);
503 return 0;
505 g_free (p);
506 } else {
507 int i, file, blocklen;
508 long current = edit->curs1;
509 int vertical_insertion = 0;
510 char *buf;
511 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
512 return 0;
513 buf = g_malloc (TEMP_BUF_LEN);
514 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
515 if (blocklen > 0) {
516 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
517 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
518 vertical_insertion = 1;
519 } else {
520 mc_lseek (file, 0, SEEK_SET);
523 if (vertical_insertion) {
524 blocklen = edit_insert_column_of_text_from_file (edit, file);
525 } else {
526 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
527 for (i = 0; i < blocklen; i++)
528 edit_insert (edit, buf[i]);
531 edit_cursor_move (edit, current - edit->curs1);
532 g_free (buf);
533 mc_close (file);
534 if (blocklen)
535 return 0;
537 return 1;
540 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
541 static int
542 check_file_access (WEdit *edit, const char *filename, struct stat *st)
544 int file;
545 GString *errmsg = (GString *) 0;
547 /* Try opening an existing file */
548 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
550 if (file < 0) {
552 * Try creating the file. O_EXCL prevents following broken links
553 * and opening existing files.
555 file =
556 mc_open (filename,
557 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
558 0666);
559 if (file < 0) {
560 g_string_sprintf (errmsg = g_string_new (NULL),
561 _(" Cannot open %s for reading "), filename);
562 goto cleanup;
563 } else {
564 /* New file, delete it if it's not modified or saved */
565 edit->delete_file = 1;
569 /* Check what we have opened */
570 if (mc_fstat (file, st) < 0) {
571 g_string_sprintf (errmsg = g_string_new (NULL),
572 _(" Cannot get size/permissions for %s "), filename);
573 goto cleanup;
576 /* We want to open regular files only */
577 if (!S_ISREG (st->st_mode)) {
578 g_string_sprintf (errmsg = g_string_new (NULL),
579 _(" %s is not a regular file "), filename);
580 goto cleanup;
584 * Don't delete non-empty files.
585 * O_EXCL should prevent it, but let's be on the safe side.
587 if (st->st_size > 0) {
588 edit->delete_file = 0;
591 if (st->st_size >= SIZE_LIMIT) {
592 g_string_sprintf (errmsg = g_string_new (NULL),
593 _(" File %s is too large "), filename);
594 goto cleanup;
597 cleanup:
598 (void) mc_close (file);
599 if (errmsg) {
600 edit_error_dialog (_("Error"), errmsg->str);
601 g_string_free (errmsg, TRUE);
602 return 1;
604 return 0;
608 * Open the file and load it into the buffers, either directly or using
609 * a filter. Return 0 on success, 1 on error.
611 * Fast loading (edit_load_file_fast) is used when the file size is
612 * known. In this case the data is read into the buffers by blocks.
613 * If the file size is not known, the data is loaded byte by byte in
614 * edit_insert_file.
616 static int
617 edit_load_file (WEdit *edit)
619 int fast_load = 1;
621 /* Cannot do fast load if a filter is used */
622 if (edit_find_filter (edit->filename) >= 0)
623 fast_load = 0;
626 * VFS may report file size incorrectly, and slow load is not a big
627 * deal considering overhead in VFS.
629 if (!vfs_file_is_local (edit->filename))
630 fast_load = 0;
633 * FIXME: line end translation should disable fast loading as well
634 * Consider doing fseek() to the end and ftell() for the real size.
637 if (*edit->filename) {
638 /* If we are dealing with a real file, check that it exists */
639 if (check_file_access (edit, edit->filename, &edit->stat1))
640 return 1;
641 } else {
642 /* nothing to load */
643 fast_load = 0;
646 edit_init_buffers (edit);
648 if (fast_load) {
649 edit->last_byte = edit->stat1.st_size;
650 edit_load_file_fast (edit, edit->filename);
651 /* If fast load was used, the number of lines wasn't calculated */
652 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
653 } else {
654 #ifdef HAVE_CHARSET
655 const char *codepage_id;
656 #endif
657 edit->last_byte = 0;
658 if (*edit->filename) {
659 edit->stack_disable = 1;
660 if (!edit_insert_file (edit, edit->filename)) {
661 edit_clean (edit);
662 return 1;
664 edit->stack_disable = 0;
667 #ifdef HAVE_CHARSET
668 codepage_id = get_codepage_id( source_codepage );
669 if ( codepage_id )
670 edit->utf8 = str_isutf8 ( codepage_id );
671 #endif
673 edit->lb = LB_ASIS;
674 return 0;
677 /* Restore saved cursor position in the file */
678 static void
679 edit_load_position (WEdit *edit)
681 char *filename;
682 long line, column;
684 if (!edit->filename || !*edit->filename)
685 return;
687 filename = vfs_canon (edit->filename);
688 load_file_position (filename, &line, &column);
689 g_free (filename);
691 edit_move_to_line (edit, line - 1);
692 edit->prev_col = column;
693 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
694 edit_move_display (edit, line - (edit->num_widget_lines / 2));
697 /* Save cursor position in the file */
698 static void
699 edit_save_position (WEdit *edit)
701 char *filename;
703 if (!edit->filename || !*edit->filename)
704 return;
706 filename = vfs_canon (edit->filename);
707 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
708 g_free (filename);
711 /* Clean the WEdit stricture except the widget part */
712 static void
713 edit_purge_widget (WEdit *edit)
715 int len = sizeof (WEdit) - sizeof (Widget);
716 char *start = (char *) edit + sizeof (Widget);
717 memset (start, 0, len);
718 edit->macro_i = -1; /* not recording a macro */
721 static void
722 edit_set_keymap (WEdit *edit)
724 edit->user_map = default_editor_keymap;
725 if (editor_keymap && editor_keymap->len > 0)
726 edit->user_map = (global_key_map_t *) editor_keymap->data;
730 #define space_width 1
733 * Fill in the edit structure. Return NULL on failure. Pass edit as
734 * NULL to allocate a new structure.
736 * If line is 0, try to restore saved position. Otherwise put the
737 * cursor on that line and show it in the middle of the screen.
739 WEdit *
740 edit_init (WEdit *edit, int lines, int columns, const char *filename,
741 long line)
743 int to_free = 0;
744 option_auto_syntax = 1; /* Resetting to auto on every invokation */
745 if ( option_line_state ) {
746 option_line_state_width = LINE_STATE_WIDTH;
747 } else {
748 option_line_state_width = 0;
750 if (!edit) {
751 #ifdef ENABLE_NLS
753 * Expand option_whole_chars_search by national letters using
754 * current locale
757 static char option_whole_chars_search_buf[256];
759 if (option_whole_chars_search_buf != option_whole_chars_search) {
760 size_t i;
761 size_t len = str_term_width1 (option_whole_chars_search);
763 strcpy (option_whole_chars_search_buf,
764 option_whole_chars_search);
766 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
767 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
768 option_whole_chars_search_buf[len++] = i;
772 option_whole_chars_search_buf[len] = 0;
773 option_whole_chars_search = option_whole_chars_search_buf;
775 #endif /* ENABLE_NLS */
776 edit = g_malloc0 (sizeof (WEdit));
777 edit->search = NULL;
778 to_free = 1;
780 edit_purge_widget (edit);
781 edit->num_widget_lines = lines;
782 edit->over_col = 0;
783 edit->num_widget_columns = columns;
784 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
785 edit->stat1.st_uid = getuid ();
786 edit->stat1.st_gid = getgid ();
787 edit->stat1.st_mtime = 0;
788 edit->bracket = -1;
789 edit->force |= REDRAW_PAGE;
790 edit_set_filename (edit, filename);
791 edit->stack_size = START_STACK_SIZE;
792 edit->stack_size_mask = START_STACK_SIZE - 1;
793 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
794 if (edit_load_file (edit)) {
795 /* edit_load_file already gives an error message */
796 if (to_free)
797 g_free (edit);
798 return 0;
800 edit->utf8 = 0;
801 edit->converter = str_cnv_from_term;
802 #ifdef HAVE_CHARSET
803 const char *cp_id = NULL;
804 cp_id = get_codepage_id (source_codepage >= 0 ?
805 source_codepage : display_codepage);
807 if (cp_id != NULL) {
808 GIConv conv;
809 conv = str_crt_conv_from (cp_id);
810 if (conv != INVALID_CONV) {
811 if (edit->converter != str_cnv_from_term)
812 str_close_conv (edit->converter);
813 edit->converter = conv;
816 if (cp_id != NULL)
817 edit->utf8 = str_isutf8 (cp_id);
818 #endif
820 edit->loading_done = 1;
821 edit->modified = 0;
822 edit->locked = 0;
823 edit_load_syntax (edit, 0, 0);
825 int color;
826 edit_get_syntax_color (edit, -1, &color);
829 /* load saved cursor position */
830 if ((line == 0) && option_save_position) {
831 edit_load_position (edit);
832 } else {
833 if (line <= 0)
834 line = 1;
835 edit_move_display (edit, line - 1);
836 edit_move_to_line (edit, line - 1);
839 edit_set_keymap (edit);
841 return edit;
844 /* Clear the edit struct, freeing everything in it. Return 1 on success */
846 edit_clean (WEdit *edit)
848 int j = 0;
850 if (!edit)
851 return 0;
853 /* a stale lock, remove it */
854 if (edit->locked)
855 edit->locked = edit_unlock_file (edit->filename);
857 /* save cursor position */
858 if (option_save_position)
859 edit_save_position (edit);
861 /* File specified on the mcedit command line and never saved */
862 if (edit->delete_file)
863 unlink (edit->filename);
865 edit_free_syntax_rules (edit);
866 book_mark_flush (edit, -1);
867 for (; j <= MAXBUFF; j++) {
868 g_free (edit->buffers1[j]);
869 g_free (edit->buffers2[j]);
872 g_free (edit->undo_stack);
873 g_free (edit->filename);
874 g_free (edit->dir);
876 if (edit->search)
878 mc_search_free(edit->search);
879 edit->search = NULL;
881 edit_purge_widget (edit);
883 return 1;
887 /* returns 1 on success */
888 int edit_renew (WEdit * edit)
890 int lines = edit->num_widget_lines;
891 int columns = edit->num_widget_columns;
892 int retval = 1;
894 edit_clean (edit);
895 if (!edit_init (edit, lines, columns, "", 0))
896 retval = 0;
897 return retval;
901 * Load a new file into the editor. If it fails, preserve the old file.
902 * To do it, allocate a new widget, initialize it and, if the new file
903 * was loaded, copy the data to the old widget.
904 * Return 1 on success, 0 on failure.
907 edit_reload (WEdit *edit, const char *filename)
909 WEdit *e;
910 int lines = edit->num_widget_lines;
911 int columns = edit->num_widget_columns;
913 e = g_malloc0 (sizeof (WEdit));
914 e->widget = edit->widget;
915 if (!edit_init (e, lines, columns, filename, 0)) {
916 g_free (e);
917 return 0;
919 edit_clean (edit);
920 memcpy (edit, e, sizeof (WEdit));
921 g_free (e);
922 return 1;
926 * Load a new file into the editor and set line. If it fails, preserve the old file.
927 * To do it, allocate a new widget, initialize it and, if the new file
928 * was loaded, copy the data to the old widget.
929 * Return 1 on success, 0 on failure.
932 edit_reload_line (WEdit *edit, const char *filename, long line)
934 WEdit *e;
935 int lines = edit->num_widget_lines;
936 int columns = edit->num_widget_columns;
938 e = g_malloc0 (sizeof (WEdit));
939 e->widget = edit->widget;
940 if (!edit_init (e, lines, columns, filename, line)) {
941 g_free (e);
942 return 0;
944 edit_clean (edit);
945 memcpy (edit, e, sizeof (WEdit));
946 g_free (e);
947 return 1;
952 Recording stack for undo:
953 The following is an implementation of a compressed stack. Identical
954 pushes are recorded by a negative prefix indicating the number of times the
955 same char was pushed. This saves space for repeated curs-left or curs-right
956 delete etc.
960 pushed: stored:
964 b -3
966 c --> -4
972 If the stack long int is 0-255 it represents a normal insert (from a backspace),
973 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
974 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
975 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
976 position.
978 The only way the cursor moves or the buffer is changed is through the routines:
979 insert, backspace, insert_ahead, delete, and cursor_move.
980 These record the reverse undo movements onto the stack each time they are
981 called.
983 Each key press results in a set of actions (insert; delete ...). So each time
984 a key is pressed the current position of start_display is pushed as
985 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
986 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
987 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
991 void edit_push_action (WEdit * edit, long c,...)
993 unsigned long sp = edit->stack_pointer;
994 unsigned long spm1;
995 long *t;
997 /* first enlarge the stack if necessary */
998 if (sp > edit->stack_size - 10) { /* say */
999 if (option_max_undo < 256)
1000 option_max_undo = 256;
1001 if (edit->stack_size < (unsigned long) option_max_undo) {
1002 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1003 if (t) {
1004 edit->undo_stack = t;
1005 edit->stack_size <<= 1;
1006 edit->stack_size_mask = edit->stack_size - 1;
1010 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1011 if (edit->stack_disable)
1012 return;
1014 #ifdef FAST_MOVE_CURSOR
1015 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1016 va_list ap;
1017 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1018 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1019 va_start (ap, c);
1020 c = -(va_arg (ap, int));
1021 va_end (ap);
1022 } else
1023 #endif /* ! FAST_MOVE_CURSOR */
1024 if (edit->stack_bottom != sp
1025 && spm1 != edit->stack_bottom
1026 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1027 int d;
1028 if (edit->undo_stack[spm1] < 0) {
1029 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1030 if (d == c) {
1031 if (edit->undo_stack[spm1] > -1000000000) {
1032 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1033 edit->undo_stack[spm1]--;
1034 return;
1037 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1038 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1039 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1040 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1041 if (edit->undo_stack[spm1] == -2)
1042 edit->stack_pointer = spm1;
1043 else
1044 edit->undo_stack[spm1]++;
1045 return;
1047 #endif
1048 } else {
1049 d = edit->undo_stack[spm1];
1050 if (d == c) {
1051 if (c >= KEY_PRESS)
1052 return; /* --> no need to push multiple do-nothings */
1053 edit->undo_stack[sp] = -2;
1054 goto check_bottom;
1056 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1057 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1058 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1059 edit->stack_pointer = spm1;
1060 return;
1062 #endif
1065 edit->undo_stack[sp] = c;
1066 check_bottom:
1068 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1070 /* if the sp wraps round and catches the stack_bottom then erase
1071 * the first set of actions on the stack to make space - by moving
1072 * stack_bottom forward one "key press" */
1073 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1074 if ((unsigned long) c == edit->stack_bottom ||
1075 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1076 do {
1077 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1078 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1080 /*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: */
1081 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1082 edit->stack_bottom = edit->stack_pointer = 0;
1086 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1087 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1089 static long
1090 pop_action (WEdit * edit)
1092 long c;
1093 unsigned long sp = edit->stack_pointer;
1094 if (sp == edit->stack_bottom) {
1095 return STACK_BOTTOM;
1097 sp = (sp - 1) & edit->stack_size_mask;
1098 if ((c = edit->undo_stack[sp]) >= 0) {
1099 /* edit->undo_stack[sp] = '@'; */
1100 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1101 return c;
1103 if (sp == edit->stack_bottom) {
1104 return STACK_BOTTOM;
1106 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1107 if (edit->undo_stack[sp] == -2) {
1108 /* edit->undo_stack[sp] = '@'; */
1109 edit->stack_pointer = sp;
1110 } else
1111 edit->undo_stack[sp]++;
1113 return c;
1116 /* is called whenever a modification is made by one of the four routines below */
1117 static void edit_modification (WEdit * edit)
1119 edit->caches_valid = 0;
1120 edit->screen_modified = 1;
1122 /* raise lock when file modified */
1123 if (!edit->modified && !edit->delete_file)
1124 edit->locked = edit_lock_file (edit->filename);
1125 edit->modified = 1;
1129 Basic low level single character buffer alterations and movements at the cursor.
1130 Returns char passed over, inserted or removed.
1133 void
1134 edit_insert (WEdit *edit, int c)
1136 /* check if file has grown to large */
1137 if (edit->last_byte >= SIZE_LIMIT)
1138 return;
1140 /* first we must update the position of the display window */
1141 if (edit->curs1 < edit->start_display) {
1142 edit->start_display++;
1143 if (c == '\n')
1144 edit->start_line++;
1147 /* Mark file as modified, unless the file hasn't been fully loaded */
1148 if (edit->loading_done) {
1149 edit_modification (edit);
1152 /* now we must update some info on the file and check if a redraw is required */
1153 if (c == '\n') {
1154 if (edit->book_mark)
1155 book_mark_inc (edit, edit->curs_line);
1156 edit->curs_line++;
1157 edit->total_lines++;
1158 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1161 /* save the reverse command onto the undo stack */
1162 edit_push_action (edit, BACKSPACE);
1164 /* update markers */
1165 edit->mark1 += (edit->mark1 > edit->curs1);
1166 edit->mark2 += (edit->mark2 > edit->curs1);
1167 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1169 /* add a new buffer if we've reached the end of the last one */
1170 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1171 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1172 g_malloc (EDIT_BUF_SIZE);
1174 /* perform the insertion */
1175 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1176 curs1 & M_EDIT_BUF_SIZE]
1177 = (unsigned char) c;
1179 /* update file length */
1180 edit->last_byte++;
1182 /* update cursor position */
1183 edit->curs1++;
1186 void
1187 edit_insert_over (WEdit * edit)
1189 int i;
1191 for ( i = 0; i < edit->over_col; i++ ) {
1192 edit_insert (edit, ' ');
1194 edit->over_col = 0;
1197 /* same as edit_insert and move left */
1198 void edit_insert_ahead (WEdit * edit, int c)
1200 if (edit->last_byte >= SIZE_LIMIT)
1201 return;
1202 if (edit->curs1 < edit->start_display) {
1203 edit->start_display++;
1204 if (c == '\n')
1205 edit->start_line++;
1207 edit_modification (edit);
1208 if (c == '\n') {
1209 if (edit->book_mark)
1210 book_mark_inc (edit, edit->curs_line);
1211 edit->total_lines++;
1212 edit->force |= REDRAW_AFTER_CURSOR;
1214 edit_push_action (edit, DELCHAR);
1216 edit->mark1 += (edit->mark1 >= edit->curs1);
1217 edit->mark2 += (edit->mark2 >= edit->curs1);
1218 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1220 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1221 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1222 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1224 edit->last_byte++;
1225 edit->curs2++;
1229 int edit_delete (WEdit * edit, const int byte_delete)
1231 int p = 0;
1232 int cw = 1;
1233 int i;
1235 if (!edit->curs2)
1236 return 0;
1238 edit->mark1 -= (edit->mark1 > edit->curs1);
1239 edit->mark2 -= (edit->mark2 > edit->curs1);
1240 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1242 cw = 1;
1243 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1244 if ( edit->utf8 && byte_delete == 0 ) {
1245 edit_get_utf (edit, edit->curs1, &cw);
1246 if ( cw < 1 )
1247 cw = 1;
1249 for ( i = 1; i<= cw; i++ ) {
1250 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1252 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1253 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1254 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1256 edit->last_byte--;
1257 edit->curs2--;
1258 edit_push_action (edit, p + 256);
1261 edit_modification (edit);
1262 if (p == '\n') {
1263 if (edit->book_mark)
1264 book_mark_dec (edit, edit->curs_line);
1265 edit->total_lines--;
1266 edit->force |= REDRAW_AFTER_CURSOR;
1268 if (edit->curs1 < edit->start_display) {
1269 edit->start_display--;
1270 if (p == '\n')
1271 edit->start_line--;
1274 return p;
1278 static int
1279 edit_backspace (WEdit * edit, const int byte_delete)
1281 int p = 0;
1282 int cw = 1;
1283 int i;
1285 if (!edit->curs1)
1286 return 0;
1288 edit->mark1 -= (edit->mark1 >= edit->curs1);
1289 edit->mark2 -= (edit->mark2 >= edit->curs1);
1290 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1292 cw = 1;
1293 if ( edit->utf8 && byte_delete == 0 ) {
1294 edit_get_prev_utf (edit, edit->curs1, &cw);
1295 if ( cw < 1 )
1296 cw = 1;
1298 for ( i = 1; i<= cw; i++ ) {
1299 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1300 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1301 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1302 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1304 edit->last_byte--;
1305 edit->curs1--;
1306 edit_push_action (edit, p);
1308 edit_modification (edit);
1309 if (p == '\n') {
1310 if (edit->book_mark)
1311 book_mark_dec (edit, edit->curs_line);
1312 edit->curs_line--;
1313 edit->total_lines--;
1314 edit->force |= REDRAW_AFTER_CURSOR;
1317 if (edit->curs1 < edit->start_display) {
1318 edit->start_display--;
1319 if (p == '\n')
1320 edit->start_line--;
1323 return p;
1326 #ifdef FAST_MOVE_CURSOR
1328 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1330 unsigned long next;
1331 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1332 edit->curs_line--;
1333 next -= (unsigned long) dest;
1334 n -= next;
1335 src += next;
1336 dest += next;
1341 edit_move_backward_lots (WEdit *edit, long increment)
1343 int r, s, t;
1344 unsigned char *p;
1346 if (increment > edit->curs1)
1347 increment = edit->curs1;
1348 if (increment <= 0)
1349 return -1;
1350 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1352 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1353 if (r > increment)
1354 r = increment;
1355 s = edit->curs1 & M_EDIT_BUF_SIZE;
1357 p = 0;
1358 if (s > r) {
1359 memqcpy (edit,
1360 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1361 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1363 } else {
1364 if (s) {
1365 memqcpy (edit,
1366 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1367 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1368 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1369 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1371 memqcpy (edit,
1372 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1373 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1374 EDIT_BUF_SIZE - (r - s), r - s);
1376 increment -= r;
1377 edit->curs1 -= r;
1378 edit->curs2 += r;
1379 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1380 if (p)
1381 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1382 else
1383 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1384 g_malloc (EDIT_BUF_SIZE);
1385 } else {
1386 g_free (p);
1389 s = edit->curs1 & M_EDIT_BUF_SIZE;
1390 while (increment) {
1391 p = 0;
1392 r = EDIT_BUF_SIZE;
1393 if (r > increment)
1394 r = increment;
1395 t = s;
1396 if (r < t)
1397 t = r;
1398 memqcpy (edit,
1399 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1400 EDIT_BUF_SIZE - t,
1401 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1403 if (r >= s) {
1404 if (t) {
1405 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1406 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1408 memqcpy (edit,
1409 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1410 EDIT_BUF_SIZE - r,
1411 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1412 EDIT_BUF_SIZE - (r - s), r - s);
1414 increment -= r;
1415 edit->curs1 -= r;
1416 edit->curs2 += r;
1417 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1418 if (p)
1419 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1420 else
1421 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1422 g_malloc (EDIT_BUF_SIZE);
1423 } else {
1424 g_free (p);
1427 return edit_get_byte (edit, edit->curs1);
1430 #endif /* ! FAST_MOVE_CURSOR */
1432 /* moves the cursor right or left: increment positive or negative respectively */
1433 void edit_cursor_move (WEdit * edit, long increment)
1435 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1436 int c;
1438 #ifdef FAST_MOVE_CURSOR
1439 if (increment < -256) {
1440 edit->force |= REDRAW_PAGE;
1441 edit_move_backward_lots (edit, -increment);
1442 return;
1444 #endif /* ! FAST_MOVE_CURSOR */
1446 if (increment < 0) {
1447 for (; increment < 0; increment++) {
1448 if (!edit->curs1)
1449 return;
1451 edit_push_action (edit, CURS_RIGHT);
1453 c = edit_get_byte (edit, edit->curs1 - 1);
1454 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1455 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1456 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1457 edit->curs2++;
1458 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1459 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1460 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1461 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1463 edit->curs1--;
1464 if (c == '\n') {
1465 edit->curs_line--;
1466 edit->force |= REDRAW_LINE_BELOW;
1470 } else if (increment > 0) {
1471 for (; increment > 0; increment--) {
1472 if (!edit->curs2)
1473 return;
1475 edit_push_action (edit, CURS_LEFT);
1477 c = edit_get_byte (edit, edit->curs1);
1478 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1479 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1480 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1481 edit->curs1++;
1482 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1483 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1484 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1485 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1487 edit->curs2--;
1488 if (c == '\n') {
1489 edit->curs_line++;
1490 edit->force |= REDRAW_LINE_ABOVE;
1496 /* These functions return positions relative to lines */
1498 /* returns index of last char on line + 1 */
1499 long edit_eol (WEdit * edit, long current)
1501 if (current < edit->last_byte) {
1502 for (;; current++)
1503 if (edit_get_byte (edit, current) == '\n')
1504 break;
1505 } else
1506 return edit->last_byte;
1507 return current;
1510 /* returns index of first char on line */
1511 long edit_bol (WEdit * edit, long current)
1513 if (current > 0) {
1514 for (;; current--)
1515 if (edit_get_byte (edit, current - 1) == '\n')
1516 break;
1517 } else
1518 return 0;
1519 return current;
1523 int edit_count_lines (WEdit * edit, long current, int upto)
1525 int lines = 0;
1526 if (upto > edit->last_byte)
1527 upto = edit->last_byte;
1528 if (current < 0)
1529 current = 0;
1530 while (current < upto)
1531 if (edit_get_byte (edit, current++) == '\n')
1532 lines++;
1533 return lines;
1537 /* If lines is zero this returns the count of lines from current to upto. */
1538 /* If upto is zero returns index of lines forward current. */
1539 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1541 if (upto) {
1542 return edit_count_lines (edit, current, upto);
1543 } else {
1544 int next;
1545 if (lines < 0)
1546 lines = 0;
1547 while (lines--) {
1548 next = edit_eol (edit, current) + 1;
1549 if (next > edit->last_byte)
1550 break;
1551 else
1552 current = next;
1554 return current;
1559 /* Returns offset of 'lines' lines up from current */
1560 long edit_move_backward (WEdit * edit, long current, int lines)
1562 if (lines < 0)
1563 lines = 0;
1564 current = edit_bol (edit, current);
1565 while((lines--) && current != 0)
1566 current = edit_bol (edit, current - 1);
1567 return current;
1570 /* If cols is zero this returns the count of columns from current to upto. */
1571 /* If upto is zero returns index of cols across from current. */
1572 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1574 long p, q;
1575 int col = 0;
1576 #ifdef HAVE_CHARSET
1577 int cw = 1;
1578 int utf_ch = 0;
1579 #endif
1580 if (upto) {
1581 q = upto;
1582 cols = -10;
1583 } else
1584 q = edit->last_byte + 2;
1585 for (col = 0, p = current; p < q; p++) {
1586 int c;
1587 #ifdef HAVE_CHARSET
1588 cw = 1;
1589 utf_ch = 0;
1590 #endif
1591 if (cols != -10) {
1592 if (col == cols)
1593 return p;
1594 if (col > cols)
1595 return p - 1;
1597 #ifdef HAVE_CHARSET
1598 if ( !edit->utf8 ) {
1599 #endif
1600 c = edit_get_byte (edit, p);
1601 #ifdef HAVE_CHARSET
1602 } else {
1603 cw = 1;
1604 c = edit_get_byte (edit, p);
1605 utf_ch = edit_get_utf (edit, p, &cw);
1607 if ( utf8_display ) {
1608 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1609 col++;
1611 #endif
1612 if (c == '\t')
1613 col += TAB_SIZE - col % TAB_SIZE;
1614 else if (c == '\n') {
1615 if (upto)
1616 return col;
1617 else
1618 return p;
1619 } else if (c < 32 || c == 127)
1620 col += 2; /* Caret notation for control characters */
1621 else
1622 col++;
1623 #ifdef HAVE_CHARSET
1624 if ( cw > 1 )
1625 col -= cw-1;
1626 #endif
1628 return col;
1631 /* returns the current column position of the cursor */
1632 int edit_get_col (WEdit * edit)
1634 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1638 /* Scrolling functions */
1640 void edit_update_curs_row (WEdit * edit)
1642 edit->curs_row = edit->curs_line - edit->start_line;
1645 void edit_update_curs_col (WEdit * edit)
1647 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1651 edit_get_curs_col (const WEdit *edit)
1653 return edit->curs_col;
1656 /*moves the display start position up by i lines */
1657 void edit_scroll_upward (WEdit * edit, unsigned long i)
1659 unsigned long lines_above = edit->start_line;
1660 if (i > lines_above)
1661 i = lines_above;
1662 if (i) {
1663 edit->start_line -= i;
1664 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1665 edit->force |= REDRAW_PAGE;
1666 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1668 edit_update_curs_row (edit);
1672 /* returns 1 if could scroll, 0 otherwise */
1673 void edit_scroll_downward (WEdit * edit, int i)
1675 int lines_below;
1676 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1677 if (lines_below > 0) {
1678 if (i > lines_below)
1679 i = lines_below;
1680 edit->start_line += i;
1681 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1682 edit->force |= REDRAW_PAGE;
1683 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1685 edit_update_curs_row (edit);
1688 void edit_scroll_right (WEdit * edit, int i)
1690 edit->force |= REDRAW_PAGE;
1691 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1692 edit->start_col -= i;
1695 void edit_scroll_left (WEdit * edit, int i)
1697 if (edit->start_col) {
1698 edit->start_col += i;
1699 if (edit->start_col > 0)
1700 edit->start_col = 0;
1701 edit->force |= REDRAW_PAGE;
1702 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1706 /* high level cursor movement commands */
1708 static int is_in_indent (WEdit *edit)
1710 long p = edit_bol (edit, edit->curs1);
1711 while (p < edit->curs1)
1712 if (!strchr (" \t", edit_get_byte (edit, p++)))
1713 return 0;
1714 return 1;
1717 static int left_of_four_spaces (WEdit *edit);
1719 void
1720 edit_move_to_prev_col (WEdit * edit, long p)
1722 int prev = edit->prev_col;
1723 int over = edit->over_col;
1725 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1727 if (option_cursor_beyond_eol) {
1728 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1730 if (line_len < prev + edit->over_col) {
1731 edit->over_col = prev + over - line_len;
1732 edit->prev_col = line_len;
1733 edit->curs_col = line_len;
1734 } else {
1735 edit->curs_col = prev + over;
1736 edit->prev_col = edit->curs_col;
1737 edit->over_col = 0;
1739 } else {
1740 edit->over_col = 0;
1741 if (is_in_indent (edit) && option_fake_half_tabs) {
1742 edit_update_curs_col (edit);
1743 if (space_width)
1744 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1745 int q = edit->curs_col;
1746 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1747 p = edit_bol (edit, edit->curs1);
1748 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1749 if (!left_of_four_spaces (edit))
1750 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1756 /* move i lines */
1757 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1759 unsigned long p, l = edit->curs_line;
1761 if (i > l)
1762 i = l;
1763 if (i) {
1764 if (i > 1)
1765 edit->force |= REDRAW_PAGE;
1766 if (scroll)
1767 edit_scroll_upward (edit, i);
1769 p = edit_bol (edit, edit->curs1);
1770 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1771 edit_move_to_prev_col (edit, p);
1773 edit->search_start = edit->curs1;
1774 edit->found_len = 0;
1778 static int
1779 is_blank (WEdit *edit, long offset)
1781 long s, f;
1782 int c;
1783 s = edit_bol (edit, offset);
1784 f = edit_eol (edit, offset) - 1;
1785 while (s <= f) {
1786 c = edit_get_byte (edit, s++);
1787 if (!isspace (c))
1788 return 0;
1790 return 1;
1794 /* returns the offset of line i */
1795 static long
1796 edit_find_line (WEdit *edit, int line)
1798 int i, j = 0;
1799 int m = 2000000000;
1800 if (!edit->caches_valid) {
1801 for (i = 0; i < N_LINE_CACHES; i++)
1802 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1803 /* three offsets that we *know* are line 0 at 0 and these two: */
1804 edit->line_numbers[1] = edit->curs_line;
1805 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1806 edit->line_numbers[2] = edit->total_lines;
1807 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1808 edit->caches_valid = 1;
1810 if (line >= edit->total_lines)
1811 return edit->line_offsets[2];
1812 if (line <= 0)
1813 return 0;
1814 /* find the closest known point */
1815 for (i = 0; i < N_LINE_CACHES; i++) {
1816 int n;
1817 n = abs (edit->line_numbers[i] - line);
1818 if (n < m) {
1819 m = n;
1820 j = i;
1823 if (m == 0)
1824 return edit->line_offsets[j]; /* know the offset exactly */
1825 if (m == 1 && j >= 3)
1826 i = j; /* one line different - caller might be looping, so stay in this cache */
1827 else
1828 i = 3 + (rand () % (N_LINE_CACHES - 3));
1829 if (line > edit->line_numbers[j])
1830 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1831 else
1832 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1833 edit->line_numbers[i] = line;
1834 return edit->line_offsets[i];
1837 int line_is_blank (WEdit * edit, long line)
1839 return is_blank (edit, edit_find_line (edit, line));
1842 /* moves up until a blank line is reached, or until just
1843 before a non-blank line is reached */
1844 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1846 int i;
1847 if (edit->curs_line <= 1) {
1848 i = 0;
1849 } else {
1850 if (line_is_blank (edit, edit->curs_line)) {
1851 if (line_is_blank (edit, edit->curs_line - 1)) {
1852 for (i = edit->curs_line - 1; i; i--)
1853 if (!line_is_blank (edit, i)) {
1854 i++;
1855 break;
1857 } else {
1858 for (i = edit->curs_line - 1; i; i--)
1859 if (line_is_blank (edit, i))
1860 break;
1862 } else {
1863 for (i = edit->curs_line - 1; i; i--)
1864 if (line_is_blank (edit, i))
1865 break;
1868 edit_move_up (edit, edit->curs_line - i, scroll);
1871 /* move i lines */
1872 void edit_move_down (WEdit * edit, int i, int scroll)
1874 long p, l = edit->total_lines - edit->curs_line;
1876 if (i > l)
1877 i = l;
1878 if (i) {
1879 if (i > 1)
1880 edit->force |= REDRAW_PAGE;
1881 if (scroll)
1882 edit_scroll_downward (edit, i);
1883 p = edit_bol (edit, edit->curs1);
1884 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1885 edit_move_to_prev_col (edit, p);
1887 edit->search_start = edit->curs1;
1888 edit->found_len = 0;
1892 /* moves down until a blank line is reached, or until just
1893 before a non-blank line is reached */
1894 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1896 int i;
1897 if (edit->curs_line >= edit->total_lines - 1) {
1898 i = edit->total_lines;
1899 } else {
1900 if (line_is_blank (edit, edit->curs_line)) {
1901 if (line_is_blank (edit, edit->curs_line + 1)) {
1902 for (i = edit->curs_line + 1; i; i++)
1903 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1904 i--;
1905 break;
1907 } else {
1908 for (i = edit->curs_line + 1; i; i++)
1909 if (line_is_blank (edit, i) || i >= edit->total_lines)
1910 break;
1912 } else {
1913 for (i = edit->curs_line + 1; i; i++)
1914 if (line_is_blank (edit, i) || i >= edit->total_lines)
1915 break;
1918 edit_move_down (edit, i - edit->curs_line, scroll);
1921 static void edit_begin_page (WEdit *edit)
1923 edit_update_curs_row (edit);
1924 edit_move_up (edit, edit->curs_row, 0);
1927 static void edit_end_page (WEdit *edit)
1929 edit_update_curs_row (edit);
1930 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1934 /* goto beginning of text */
1935 static void edit_move_to_top (WEdit * edit)
1937 if (edit->curs_line) {
1938 edit_cursor_move (edit, -edit->curs1);
1939 edit_move_to_prev_col (edit, 0);
1940 edit->force |= REDRAW_PAGE;
1941 edit->search_start = 0;
1942 edit_update_curs_row(edit);
1947 /* goto end of text */
1948 static void edit_move_to_bottom (WEdit * edit)
1950 if (edit->curs_line < edit->total_lines) {
1951 edit_cursor_move (edit, edit->curs2);
1952 edit->start_display = edit->last_byte;
1953 edit->start_line = edit->total_lines;
1954 edit_update_curs_row(edit);
1955 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1956 edit->force |= REDRAW_PAGE;
1960 /* goto beginning of line */
1961 static void edit_cursor_to_bol (WEdit * edit)
1963 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1964 edit->search_start = edit->curs1;
1965 edit->prev_col = edit_get_col (edit);
1966 edit->over_col = 0;
1969 /* goto end of line */
1970 static void edit_cursor_to_eol (WEdit * edit)
1972 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1973 edit->search_start = edit->curs1;
1974 edit->prev_col = edit_get_col (edit);
1975 edit->over_col = 0;
1978 /* move cursor to line 'line' */
1979 void edit_move_to_line (WEdit * e, long line)
1981 if(line < e->curs_line)
1982 edit_move_up (e, e->curs_line - line, 0);
1983 else
1984 edit_move_down (e, line - e->curs_line, 0);
1985 edit_scroll_screen_over_cursor (e);
1988 /* scroll window so that first visible line is 'line' */
1989 void edit_move_display (WEdit * e, long line)
1991 if(line < e->start_line)
1992 edit_scroll_upward (e, e->start_line - line);
1993 else
1994 edit_scroll_downward (e, line - e->start_line);
1997 /* save markers onto undo stack */
1998 void edit_push_markers (WEdit * edit)
2000 edit_push_action (edit, MARK_1 + edit->mark1);
2001 edit_push_action (edit, MARK_2 + edit->mark2);
2004 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2006 edit->mark1 = m1;
2007 edit->mark2 = m2;
2008 edit->column1 = c1;
2009 edit->column2 = c2;
2013 /* highlight marker toggle */
2014 void edit_mark_cmd (WEdit * edit, int unmark)
2016 edit_push_markers (edit);
2017 if (unmark) {
2018 edit_set_markers (edit, 0, 0, 0, 0);
2019 edit->force |= REDRAW_PAGE;
2020 } else {
2021 if (edit->mark2 >= 0) {
2022 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
2023 edit->force |= REDRAW_PAGE;
2024 } else
2025 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2029 static unsigned long
2030 my_type_of (int c)
2032 int x, r = 0;
2033 const char *p, *q;
2034 const char option_chars_move_whole_word[] =
2035 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2037 if (!c)
2038 return 0;
2039 if (c == '!') {
2040 if (*option_chars_move_whole_word == '!')
2041 return 2;
2042 return 0x80000000UL;
2044 if (g_ascii_isupper ((gchar) c))
2045 c = 'A';
2046 else if (g_ascii_islower ((gchar) c))
2047 c = 'a';
2048 else if (g_ascii_isalpha (c))
2049 c = 'a';
2050 else if (isdigit (c))
2051 c = '0';
2052 else if (isspace (c))
2053 c = ' ';
2054 q = strchr (option_chars_move_whole_word, c);
2055 if (!q)
2056 return 0xFFFFFFFFUL;
2057 do {
2058 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2059 if (*p == '!')
2060 x <<= 1;
2061 r |= x;
2062 } while ((q = strchr (q + 1, c)));
2063 return r;
2066 static void
2067 edit_left_word_move (WEdit *edit, int s)
2069 for (;;) {
2070 int c1, c2;
2071 edit_cursor_move (edit, -1);
2072 if (!edit->curs1)
2073 break;
2074 c1 = edit_get_byte (edit, edit->curs1 - 1);
2075 c2 = edit_get_byte (edit, edit->curs1);
2076 if (!(my_type_of (c1) & my_type_of (c2)))
2077 break;
2078 if (isspace (c1) && !isspace (c2))
2079 break;
2080 if (s)
2081 if (!isspace (c1) && isspace (c2))
2082 break;
2086 static void edit_left_word_move_cmd (WEdit * edit)
2088 edit_left_word_move (edit, 0);
2089 edit->force |= REDRAW_PAGE;
2092 static void
2093 edit_right_word_move (WEdit *edit, int s)
2095 for (;;) {
2096 int c1, c2;
2097 edit_cursor_move (edit, 1);
2098 if (edit->curs1 >= edit->last_byte)
2099 break;
2100 c1 = edit_get_byte (edit, edit->curs1 - 1);
2101 c2 = edit_get_byte (edit, edit->curs1);
2102 if (!(my_type_of (c1) & my_type_of (c2)))
2103 break;
2104 if (isspace (c1) && !isspace (c2))
2105 break;
2106 if (s)
2107 if (!isspace (c1) && isspace (c2))
2108 break;
2112 static void edit_right_word_move_cmd (WEdit * edit)
2114 edit_right_word_move (edit, 0);
2115 edit->force |= REDRAW_PAGE;
2118 static void edit_right_char_move_cmd (WEdit * edit)
2120 int cw = 1;
2121 int c = 0;
2122 if ( edit->utf8 ) {
2123 c = edit_get_utf (edit, edit->curs1, &cw);
2124 if ( cw < 1 )
2125 cw = 1;
2126 } else {
2127 c = edit_get_byte (edit, edit->curs1);
2129 if (option_cursor_beyond_eol && c == '\n') {
2130 edit->over_col++;
2131 } else {
2132 edit_cursor_move (edit, cw);
2136 static void edit_left_char_move_cmd (WEdit * edit)
2138 int cw = 1;
2139 if ( edit->utf8 ) {
2140 edit_get_prev_utf (edit, edit->curs1, &cw);
2141 if ( cw < 1 )
2142 cw = 1;
2144 if (option_cursor_beyond_eol && edit->over_col > 0) {
2145 edit->over_col--;
2146 } else {
2147 edit_cursor_move (edit, -cw);
2152 static void edit_right_delete_word (WEdit * edit)
2154 int c1, c2;
2155 for (;;) {
2156 if (edit->curs1 >= edit->last_byte)
2157 break;
2158 c1 = edit_delete (edit, 1);
2159 c2 = edit_get_byte (edit, edit->curs1);
2160 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2161 break;
2162 if (!(my_type_of (c1) & my_type_of (c2)))
2163 break;
2167 static void edit_left_delete_word (WEdit * edit)
2169 int c1, c2;
2170 for (;;) {
2171 if (edit->curs1 <= 0)
2172 break;
2173 c1 = edit_backspace (edit, 1);
2174 c2 = edit_get_byte (edit, edit->curs1 - 1);
2175 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2176 break;
2177 if (!(my_type_of (c1) & my_type_of (c2)))
2178 break;
2183 the start column position is not recorded, and hence does not
2184 undo as it happed. But who would notice.
2186 static void
2187 edit_do_undo (WEdit * edit)
2189 long ac;
2190 long count = 0;
2192 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2193 edit->over_col = 0;
2194 while ((ac = pop_action (edit)) < KEY_PRESS) {
2195 switch ((int) ac) {
2196 case STACK_BOTTOM:
2197 goto done_undo;
2198 case CURS_RIGHT:
2199 edit_cursor_move (edit, 1);
2200 break;
2201 case CURS_LEFT:
2202 edit_cursor_move (edit, -1);
2203 break;
2204 case BACKSPACE:
2205 edit_backspace (edit, 1);
2206 break;
2207 case DELCHAR:
2208 edit_delete (edit, 1);
2209 break;
2210 case COLUMN_ON:
2211 column_highlighting = 1;
2212 break;
2213 case COLUMN_OFF:
2214 column_highlighting = 0;
2215 break;
2217 if (ac >= 256 && ac < 512)
2218 edit_insert_ahead (edit, ac - 256);
2219 if (ac >= 0 && ac < 256)
2220 edit_insert (edit, ac);
2222 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2223 edit->mark1 = ac - MARK_1;
2224 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2225 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2226 edit->mark2 = ac - MARK_2;
2227 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2229 if (count++)
2230 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2233 if (edit->start_display > ac - KEY_PRESS) {
2234 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2235 edit->force |= REDRAW_PAGE;
2236 } else if (edit->start_display < ac - KEY_PRESS) {
2237 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2238 edit->force |= REDRAW_PAGE;
2240 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2241 edit_update_curs_row (edit);
2243 done_undo:;
2244 edit->stack_disable = 0;
2247 static void edit_delete_to_line_end (WEdit * edit)
2249 while (edit_get_byte (edit, edit->curs1) != '\n') {
2250 if (!edit->curs2)
2251 break;
2252 edit_delete (edit, 1);
2256 static void edit_delete_to_line_begin (WEdit * edit)
2258 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2259 if (!edit->curs1)
2260 break;
2261 edit_backspace (edit, 1);
2265 void
2266 edit_delete_line (WEdit *edit)
2269 * Delete right part of the line.
2270 * Note that edit_get_byte() returns '\n' when byte position is
2271 * beyond EOF.
2273 while (edit_get_byte (edit, edit->curs1) != '\n') {
2274 (void) edit_delete (edit, 1);
2278 * Delete '\n' char.
2279 * Note that edit_delete() will not corrupt anything if called while
2280 * cursor position is EOF.
2282 (void) edit_delete (edit, 1);
2285 * Delete left part of the line.
2286 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2288 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2289 (void) edit_backspace (edit, 1);
2293 void insert_spaces_tab (WEdit * edit, int half)
2295 int i;
2296 edit_update_curs_col (edit);
2297 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2298 while (i > 0) {
2299 edit_insert (edit, ' ');
2300 i -= space_width;
2304 static int is_aligned_on_a_tab (WEdit * edit)
2306 edit_update_curs_col (edit);
2307 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2308 return 0; /* not alligned on a tab */
2309 return 1;
2312 static int right_of_four_spaces (WEdit *edit)
2314 int i, ch = 0;
2315 for (i = 1; i <= HALF_TAB_SIZE; i++)
2316 ch |= edit_get_byte (edit, edit->curs1 - i);
2317 if (ch == ' ')
2318 return is_aligned_on_a_tab (edit);
2319 return 0;
2322 static int left_of_four_spaces (WEdit *edit)
2324 int i, ch = 0;
2325 for (i = 0; i < HALF_TAB_SIZE; i++)
2326 ch |= edit_get_byte (edit, edit->curs1 + i);
2327 if (ch == ' ')
2328 return is_aligned_on_a_tab (edit);
2329 return 0;
2332 int edit_indent_width (WEdit * edit, long p)
2334 long q = p;
2335 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2336 q++;
2337 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2340 void edit_insert_indent (WEdit * edit, int indent)
2342 if (!option_fill_tabs_with_spaces) {
2343 while (indent >= TAB_SIZE) {
2344 edit_insert (edit, '\t');
2345 indent -= TAB_SIZE;
2348 while (indent-- > 0)
2349 edit_insert (edit, ' ');
2352 static void
2353 edit_auto_indent (WEdit * edit)
2355 long p;
2356 char c;
2357 p = edit->curs1;
2358 /* use the previous line as a template */
2359 p = edit_move_backward (edit, p, 1);
2360 /* copy the leading whitespace of the line */
2361 for (;;) { /* no range check - the line _is_ \n-terminated */
2362 c = edit_get_byte (edit, p++);
2363 if (c != ' ' && c != '\t')
2364 break;
2365 edit_insert (edit, c);
2369 static void edit_double_newline (WEdit * edit)
2371 edit_insert (edit, '\n');
2372 if (edit_get_byte (edit, edit->curs1) == '\n')
2373 return;
2374 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2375 return;
2376 edit->force |= REDRAW_PAGE;
2377 edit_insert (edit, '\n');
2380 static void edit_tab_cmd (WEdit * edit)
2382 int i;
2384 if (option_fake_half_tabs) {
2385 if (is_in_indent (edit)) {
2386 /*insert a half tab (usually four spaces) unless there is a
2387 half tab already behind, then delete it and insert a
2388 full tab. */
2389 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2390 for (i = 1; i <= HALF_TAB_SIZE; i++)
2391 edit_backspace (edit, 1);
2392 edit_insert (edit, '\t');
2393 } else {
2394 insert_spaces_tab (edit, 1);
2396 return;
2399 if (option_fill_tabs_with_spaces) {
2400 insert_spaces_tab (edit, 0);
2401 } else {
2402 edit_insert (edit, '\t');
2404 return;
2407 static void check_and_wrap_line (WEdit * edit)
2409 int curs, c;
2410 if (!option_typewriter_wrap)
2411 return;
2412 edit_update_curs_col (edit);
2413 if (edit->curs_col < option_word_wrap_line_length)
2414 return;
2415 curs = edit->curs1;
2416 for (;;) {
2417 curs--;
2418 c = edit_get_byte (edit, curs);
2419 if (c == '\n' || curs <= 0) {
2420 edit_insert (edit, '\n');
2421 return;
2423 if (c == ' ' || c == '\t') {
2424 int current = edit->curs1;
2425 edit_cursor_move (edit, curs - edit->curs1 + 1);
2426 edit_insert (edit, '\n');
2427 edit_cursor_move (edit, current - edit->curs1 + 1);
2428 return;
2433 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2435 void edit_push_key_press (WEdit * edit)
2437 edit_push_action (edit, KEY_PRESS + edit->start_display);
2438 if (edit->mark2 == -1)
2439 edit_push_action (edit, MARK_1 + edit->mark1);
2442 /* this find the matching bracket in either direction, and sets edit->bracket */
2443 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2445 const char * const b = "{}{[][()(", *p;
2446 int i = 1, a, inc = -1, c, d, n = 0;
2447 unsigned long j = 0;
2448 long q;
2449 edit_update_curs_row (edit);
2450 c = edit_get_byte (edit, edit->curs1);
2451 p = strchr (b, c);
2452 /* no limit */
2453 if (!furthest_bracket_search)
2454 furthest_bracket_search--;
2455 /* not on a bracket at all */
2456 if (!p)
2457 return -1;
2458 /* the matching bracket */
2459 d = p[1];
2460 /* going left or right? */
2461 if (strchr ("{[(", c))
2462 inc = 1;
2463 for (q = edit->curs1 + inc;; q += inc) {
2464 /* out of buffer? */
2465 if (q >= edit->last_byte || q < 0)
2466 break;
2467 a = edit_get_byte (edit, q);
2468 /* don't want to eat CPU */
2469 if (j++ > furthest_bracket_search)
2470 break;
2471 /* out of screen? */
2472 if (in_screen) {
2473 if (q < edit->start_display)
2474 break;
2475 /* count lines if searching downward */
2476 if (inc > 0 && a == '\n')
2477 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2478 break;
2480 /* count bracket depth */
2481 i += (a == c) - (a == d);
2482 /* return if bracket depth is zero */
2483 if (!i)
2484 return q;
2486 /* no match */
2487 return -1;
2490 static long last_bracket = -1;
2492 void edit_find_bracket (WEdit * edit)
2494 edit->bracket = edit_get_bracket (edit, 1, 10000);
2495 if (last_bracket != edit->bracket)
2496 edit->force |= REDRAW_PAGE;
2497 last_bracket = edit->bracket;
2500 static void edit_goto_matching_bracket (WEdit *edit)
2502 long q;
2503 q = edit_get_bracket (edit, 0, 0);
2504 if (q < 0)
2505 return;
2506 edit->bracket = edit->curs1;
2507 edit->force |= REDRAW_PAGE;
2508 edit_cursor_move (edit, q - edit->curs1);
2512 * This executes a command as though the user initiated it through a key
2513 * press. Callback with WIDGET_KEY as a message calls this after
2514 * translating the key press. This function can be used to pass any
2515 * command to the editor. Note that the screen wouldn't update
2516 * automatically. Either of command or char_for_insertion must be
2517 * passed as -1. Commands are executed, and char_for_insertion is
2518 * inserted at the cursor.
2520 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2522 if (command == CK_Begin_Record_Macro) {
2523 edit->macro_i = 0;
2524 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2525 return;
2527 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2528 edit->force |= REDRAW_COMPLETELY;
2529 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2530 edit->macro_i = -1;
2531 return;
2533 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2534 edit->macro[edit->macro_i].command = command;
2535 edit->macro[edit->macro_i++].ch = char_for_insertion;
2537 /* record the beginning of a set of editing actions initiated by a key press */
2538 if (command != CK_Undo && command != CK_Ext_Mode)
2539 edit_push_key_press (edit);
2541 edit_execute_cmd (edit, command, char_for_insertion);
2542 if (column_highlighting)
2543 edit->force |= REDRAW_PAGE;
2546 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2549 This executes a command at a lower level than macro recording.
2550 It also does not push a key_press onto the undo stack. This means
2551 that if it is called many times, a single undo command will undo
2552 all of them. It also does not check for the Undo command.
2554 void
2555 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2557 edit->force |= REDRAW_LINE;
2559 /* The next key press will unhighlight the found string, so update
2560 * the whole page */
2561 if (edit->found_len || column_highlighting)
2562 edit->force |= REDRAW_PAGE;
2564 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2565 column_highlighting = 0;
2566 if (!edit->highlight
2567 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2568 edit_mark_cmd (edit, 1); /* clear */
2569 edit_mark_cmd (edit, 0); /* marking on */
2571 edit->highlight = 1;
2572 } else { /* any other command */
2573 if (edit->highlight)
2574 edit_mark_cmd (edit, 0); /* clear */
2575 edit->highlight = 0;
2578 /* first check for undo */
2579 if (command == CK_Undo) {
2580 edit_do_undo (edit);
2581 edit->found_len = 0;
2582 edit->prev_col = edit_get_col (edit);
2583 edit->search_start = edit->curs1;
2584 return;
2587 /* An ordinary key press */
2588 if (char_for_insertion >= 0) {
2589 if (edit->overwrite) {
2590 if (edit_get_byte (edit, edit->curs1) != '\n')
2591 edit_delete (edit, 0);
2593 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2594 edit_insert_over (edit);
2595 #ifdef HAVE_CHARSET
2596 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2597 unsigned char str[6 + 1];
2598 size_t i = 0;
2599 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2600 if ( res == 0 ) {
2601 str[0] = '.';
2602 str[1] = '\0';
2603 } else {
2604 str[res] = '\0';
2606 while ( str[i] != 0 && i<=6) {
2607 char_for_insertion = str[i];
2608 edit_insert (edit, char_for_insertion);
2609 i++;
2611 } else {
2612 #endif
2613 edit_insert (edit, char_for_insertion);
2614 #ifdef HAVE_CHARSET
2616 #endif
2617 if (option_auto_para_formatting) {
2618 format_paragraph (edit, 0);
2619 edit->force |= REDRAW_PAGE;
2620 } else
2621 check_and_wrap_line (edit);
2622 edit->found_len = 0;
2623 edit->prev_col = edit_get_col (edit);
2624 edit->search_start = edit->curs1;
2625 edit_find_bracket (edit);
2626 return;
2629 switch (command) {
2630 case CK_Begin_Page:
2631 case CK_End_Page:
2632 case CK_Begin_Page_Highlight:
2633 case CK_End_Page_Highlight:
2634 case CK_Word_Left:
2635 case CK_Word_Right:
2636 case CK_Up:
2637 case CK_Down:
2638 case CK_Left:
2639 case CK_Right:
2640 if ( edit->mark2 >= 0 ) {
2641 if ( !option_persistent_selections ) {
2642 if (column_highlighting)
2643 edit_push_action (edit, COLUMN_ON);
2644 column_highlighting = 0;
2645 edit_mark_cmd (edit, 1);
2650 switch (command) {
2651 case CK_Begin_Page:
2652 case CK_End_Page:
2653 case CK_Begin_Page_Highlight:
2654 case CK_End_Page_Highlight:
2655 case CK_Word_Left:
2656 case CK_Word_Right:
2657 case CK_Up:
2658 case CK_Down:
2659 case CK_Word_Left_Highlight:
2660 case CK_Word_Right_Highlight:
2661 case CK_Up_Highlight:
2662 case CK_Down_Highlight:
2663 case CK_Up_Alt_Highlight:
2664 case CK_Down_Alt_Highlight:
2665 if (edit->mark2 == -1)
2666 break; /*marking is following the cursor: may need to highlight a whole line */
2667 case CK_Left:
2668 case CK_Right:
2669 case CK_Left_Highlight:
2670 case CK_Right_Highlight:
2671 edit->force |= REDRAW_CHAR_ONLY;
2674 /* basic cursor key commands */
2675 switch (command) {
2676 case CK_BackSpace:
2677 /* if non persistent selection and text selected */
2678 if ( !option_persistent_selections ) {
2679 if ( edit->mark1 != edit->mark2 ) {
2680 edit_block_delete_cmd (edit);
2681 break;
2684 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2685 edit->over_col--;
2686 break;
2688 if (option_backspace_through_tabs && is_in_indent (edit)) {
2689 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2690 && edit->curs1 > 0)
2691 edit_backspace (edit, 1);
2692 break;
2693 } else {
2694 if (option_fake_half_tabs) {
2695 int i;
2696 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2697 for (i = 0; i < HALF_TAB_SIZE; i++)
2698 edit_backspace (edit, 1);
2699 break;
2703 edit_backspace (edit, 0);
2704 break;
2705 case CK_Delete:
2706 /* if non persistent selection and text selected */
2707 if ( !option_persistent_selections ) {
2708 if ( edit->mark1 != edit->mark2 ) {
2709 edit_block_delete_cmd (edit);
2710 break;
2714 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2715 edit_insert_over (edit);
2717 if (option_fake_half_tabs) {
2718 int i;
2719 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2720 for (i = 1; i <= HALF_TAB_SIZE; i++)
2721 edit_delete (edit, 1);
2722 break;
2725 edit_delete (edit, 0);
2726 break;
2727 case CK_Delete_Word_Left:
2728 edit->over_col = 0;
2729 edit_left_delete_word (edit);
2730 break;
2731 case CK_Delete_Word_Right:
2732 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2733 edit_insert_over (edit);
2735 edit_right_delete_word (edit);
2736 break;
2737 case CK_Delete_Line:
2738 edit_delete_line (edit);
2739 break;
2740 case CK_Delete_To_Line_End:
2741 edit_delete_to_line_end (edit);
2742 break;
2743 case CK_Delete_To_Line_Begin:
2744 edit_delete_to_line_begin (edit);
2745 break;
2746 case CK_Enter:
2747 edit->over_col = 0;
2748 if (option_auto_para_formatting) {
2749 edit_double_newline (edit);
2750 if (option_return_does_auto_indent)
2751 edit_auto_indent (edit);
2752 format_paragraph (edit, 0);
2753 } else {
2754 edit_insert (edit, '\n');
2755 if (option_return_does_auto_indent) {
2756 edit_auto_indent (edit);
2759 break;
2760 case CK_Return:
2761 edit_insert (edit, '\n');
2762 break;
2764 case CK_Page_Up_Alt_Highlight:
2765 column_highlighting = 1;
2766 case CK_Page_Up:
2767 case CK_Page_Up_Highlight:
2768 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2769 break;
2770 case CK_Page_Down_Alt_Highlight:
2771 column_highlighting = 1;
2772 case CK_Page_Down:
2773 case CK_Page_Down_Highlight:
2774 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2775 break;
2776 case CK_Left_Alt_Highlight:
2777 column_highlighting = 1;
2778 case CK_Left:
2779 case CK_Left_Highlight:
2780 if (option_fake_half_tabs) {
2781 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2782 if ( option_cursor_beyond_eol && edit->over_col > 0)
2783 edit->over_col--;
2784 else
2785 edit_cursor_move (edit, -HALF_TAB_SIZE);
2786 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2787 break;
2790 edit_left_char_move_cmd (edit);
2791 break;
2792 case CK_Right_Alt_Highlight:
2793 column_highlighting = 1;
2794 case CK_Right:
2795 case CK_Right_Highlight:
2796 if (option_fake_half_tabs) {
2797 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2798 edit_cursor_move (edit, HALF_TAB_SIZE);
2799 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2800 break;
2803 edit_right_char_move_cmd (edit);
2804 break;
2805 case CK_Begin_Page:
2806 case CK_Begin_Page_Highlight:
2807 edit_begin_page (edit);
2808 break;
2809 case CK_End_Page:
2810 case CK_End_Page_Highlight:
2811 edit_end_page (edit);
2812 break;
2813 case CK_Word_Left:
2814 case CK_Word_Left_Highlight:
2815 edit->over_col = 0;
2816 edit_left_word_move_cmd (edit);
2817 break;
2818 case CK_Word_Right:
2819 case CK_Word_Right_Highlight:
2820 edit->over_col = 0;
2821 edit_right_word_move_cmd (edit);
2822 break;
2823 case CK_Up_Alt_Highlight:
2824 column_highlighting = 1;
2825 case CK_Up:
2826 case CK_Up_Highlight:
2827 edit_move_up (edit, 1, 0);
2828 break;
2829 case CK_Down_Alt_Highlight:
2830 column_highlighting = 1;
2831 case CK_Down:
2832 case CK_Down_Highlight:
2833 edit_move_down (edit, 1, 0);
2834 break;
2835 case CK_Paragraph_Up_Alt_Highlight:
2836 column_highlighting = 1;
2837 case CK_Paragraph_Up:
2838 case CK_Paragraph_Up_Highlight:
2839 edit_move_up_paragraph (edit, 0);
2840 break;
2841 case CK_Paragraph_Down_Alt_Highlight:
2842 column_highlighting = 1;
2843 case CK_Paragraph_Down:
2844 case CK_Paragraph_Down_Highlight:
2845 edit_move_down_paragraph (edit, 0);
2846 break;
2847 case CK_Scroll_Up_Alt_Highlight:
2848 column_highlighting = 1;
2849 case CK_Scroll_Up:
2850 case CK_Scroll_Up_Highlight:
2851 edit_move_up (edit, 1, 1);
2852 break;
2853 case CK_Scroll_Down_Alt_Highlight:
2854 column_highlighting = 1;
2855 case CK_Scroll_Down:
2856 case CK_Scroll_Down_Highlight:
2857 edit_move_down (edit, 1, 1);
2858 break;
2859 case CK_Home:
2860 case CK_Home_Highlight:
2861 edit_cursor_to_bol (edit);
2862 break;
2863 case CK_End:
2864 case CK_End_Highlight:
2865 edit_cursor_to_eol (edit);
2866 break;
2867 case CK_Tab:
2868 /* if text marked shift block */
2869 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2870 if (edit->mark2 < 0)
2871 edit_mark_cmd (edit, 0);
2872 edit_move_block_to_right (edit);
2873 } else {
2874 if ( option_cursor_beyond_eol )
2875 edit_insert_over (edit);
2876 edit_tab_cmd (edit);
2877 if (option_auto_para_formatting) {
2878 format_paragraph (edit, 0);
2879 edit->force |= REDRAW_PAGE;
2880 } else {
2881 check_and_wrap_line (edit);
2884 break;
2886 case CK_Toggle_Insert:
2887 edit->overwrite = (edit->overwrite == 0);
2888 break;
2890 case CK_Mark:
2891 if (edit->mark2 >= 0) {
2892 if (column_highlighting)
2893 edit_push_action (edit, COLUMN_ON);
2894 column_highlighting = 0;
2896 edit_mark_cmd (edit, 0);
2897 break;
2898 case CK_Column_Mark:
2899 if (!column_highlighting)
2900 edit_push_action (edit, COLUMN_OFF);
2901 column_highlighting = 1;
2902 edit_mark_cmd (edit, 0);
2903 break;
2904 case CK_Unmark:
2905 if (column_highlighting)
2906 edit_push_action (edit, COLUMN_ON);
2907 column_highlighting = 0;
2908 edit_mark_cmd (edit, 1);
2909 break;
2911 case CK_Toggle_Line_State:
2912 option_line_state = !option_line_state;
2913 if ( option_line_state ) {
2914 option_line_state_width = LINE_STATE_WIDTH;
2915 } else {
2916 option_line_state_width = 0;
2918 edit->force |= REDRAW_PAGE;
2919 break;
2921 case CK_Toggle_Bookmark:
2922 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2923 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2924 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2925 else
2926 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2927 break;
2928 case CK_Flush_Bookmarks:
2929 book_mark_flush (edit, BOOK_MARK_COLOR);
2930 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2931 edit->force |= REDRAW_PAGE;
2932 break;
2933 case CK_Next_Bookmark:
2934 if (edit->book_mark) {
2935 struct _book_mark *p;
2936 p = (struct _book_mark *) book_mark_find (edit,
2937 edit->curs_line);
2938 if (p->next) {
2939 p = p->next;
2940 if (p->line >= edit->start_line + edit->num_widget_lines
2941 || p->line < edit->start_line)
2942 edit_move_display (edit,
2943 p->line -
2944 edit->num_widget_lines / 2);
2945 edit_move_to_line (edit, p->line);
2948 break;
2949 case CK_Prev_Bookmark:
2950 if (edit->book_mark) {
2951 struct _book_mark *p;
2952 p = (struct _book_mark *) book_mark_find (edit,
2953 edit->curs_line);
2954 while (p->line == edit->curs_line)
2955 if (p->prev)
2956 p = p->prev;
2957 if (p->line >= 0) {
2958 if (p->line >= edit->start_line + edit->num_widget_lines
2959 || p->line < edit->start_line)
2960 edit_move_display (edit,
2961 p->line -
2962 edit->num_widget_lines / 2);
2963 edit_move_to_line (edit, p->line);
2966 break;
2968 case CK_Beginning_Of_Text:
2969 case CK_Beginning_Of_Text_Highlight:
2970 edit_move_to_top (edit);
2971 break;
2972 case CK_End_Of_Text:
2973 case CK_End_Of_Text_Highlight:
2974 edit_move_to_bottom (edit);
2975 break;
2977 case CK_Copy:
2978 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2979 edit_insert_over (edit);
2980 edit_block_copy_cmd (edit);
2981 break;
2982 case CK_Remove:
2983 edit_block_delete_cmd (edit);
2984 break;
2985 case CK_Move:
2986 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2987 edit_insert_over (edit);
2988 edit_block_move_cmd (edit);
2989 break;
2991 case CK_XStore:
2992 edit_copy_to_X_buf_cmd (edit);
2993 break;
2994 case CK_XCut:
2995 edit_cut_to_X_buf_cmd (edit);
2996 break;
2997 case CK_XPaste:
2998 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2999 edit_insert_over (edit);
3000 edit_paste_from_X_buf_cmd (edit);
3001 break;
3002 case CK_Selection_History:
3003 edit_paste_from_history (edit);
3004 break;
3006 case CK_Save_As:
3007 edit_save_as_cmd (edit);
3008 break;
3009 case CK_Save:
3010 edit_save_confirm_cmd (edit);
3011 break;
3012 case CK_Load:
3013 edit_load_cmd (edit, EDIT_FILE_COMMON);
3014 break;
3015 case CK_Save_Block:
3016 edit_save_block_cmd (edit);
3017 break;
3018 case CK_Insert_File:
3019 edit_insert_file_cmd (edit);
3020 break;
3022 case CK_Load_Prev_File:
3023 edit_load_back_cmd (edit);
3024 break;
3025 case CK_Load_Next_File:
3026 edit_load_forward_cmd (edit);
3027 break;
3029 case CK_Load_Syntax_File:
3030 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3031 break;
3032 case CK_Load_Menu_File:
3033 edit_load_cmd (edit, EDIT_FILE_MENU);
3034 break;
3036 case CK_Toggle_Syntax:
3037 if ((option_syntax_highlighting ^= 1) == 1)
3038 edit_load_syntax (edit, NULL, option_syntax_type);
3039 edit->force |= REDRAW_PAGE;
3040 break;
3042 case CK_Toggle_Tab_TWS:
3043 enable_show_tabs_tws ^= 1;
3044 edit->force |= REDRAW_PAGE;
3045 break;
3047 case CK_Find:
3048 edit_search_cmd (edit, 0);
3049 break;
3050 case CK_Find_Again:
3051 edit_search_cmd (edit, 1);
3052 break;
3053 case CK_Replace:
3054 edit_replace_cmd (edit, 0);
3055 break;
3056 case CK_Replace_Again:
3057 edit_replace_cmd (edit, 1);
3058 break;
3059 case CK_Complete_Word:
3060 /* if text marked shift block */
3061 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3062 edit_move_block_to_left (edit);
3063 } else {
3064 edit_complete_word_cmd (edit);
3066 break;
3067 case CK_Find_Definition:
3068 edit_get_match_keyword_cmd (edit);
3069 break;
3071 case CK_Exit:
3072 dlg_stop (edit->widget.parent);
3073 break;
3074 case CK_New:
3075 edit_new_cmd (edit);
3076 break;
3078 case CK_Help:
3079 edit_help_cmd (edit);
3080 break;
3082 case CK_Refresh:
3083 edit_refresh_cmd (edit);
3084 break;
3086 case CK_Date:{
3087 char s[1024];
3088 /* fool gcc to prevent a Y2K warning */
3089 char time_format[] = "_c";
3090 time_format[0] = '%';
3092 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3093 edit_print_string (edit, s);
3094 edit->force |= REDRAW_PAGE;
3095 break;
3097 case CK_Goto:
3098 edit_goto_cmd (edit);
3099 break;
3100 case CK_Paragraph_Format:
3101 format_paragraph (edit, 1);
3102 edit->force |= REDRAW_PAGE;
3103 break;
3104 case CK_Delete_Macro:
3105 edit_delete_macro_cmd (edit);
3106 break;
3107 case CK_Match_Bracket:
3108 edit_goto_matching_bracket (edit);
3109 break;
3110 case CK_User_Menu:
3111 user_menu (edit);
3112 break;
3113 case CK_Sort:
3114 edit_sort_cmd (edit);
3115 break;
3116 case CK_ExtCmd:
3117 edit_ext_cmd (edit);
3118 break;
3119 case CK_Mail:
3120 edit_mail_dialog (edit);
3121 break;
3122 case CK_Shell:
3123 view_other_cmd ();
3124 break;
3125 case CK_SelectCodepage:
3126 edit_select_codepage_cmd (edit);
3127 break;
3128 case CK_Insert_Literal:
3129 edit_insert_literal_cmd (edit);
3130 break;
3131 case CK_Execute_Macro:
3132 edit_execute_macro_cmd (edit);
3133 break;
3134 case CK_Begin_End_Macro:
3135 edit_begin_end_macro_cmd (edit);
3136 break;
3137 case CK_Ext_Mode:
3138 edit->extmod = 1;
3139 break;
3140 default:
3141 break;
3144 /* CK_Pipe_Block */
3145 if ((command / 1000) == 1) /* a shell command */
3146 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3147 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3148 struct macro m[MAX_MACRO_LENGTH];
3149 int nm;
3150 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3151 edit_execute_macro (edit, m, nm);
3154 /* keys which must set the col position, and the search vars */
3155 switch (command) {
3156 case CK_Find:
3157 case CK_Find_Again:
3158 case CK_Replace:
3159 case CK_Replace_Again:
3160 case CK_Complete_Word:
3161 edit->prev_col = edit_get_col (edit);
3162 break;
3163 case CK_Up:
3164 case CK_Up_Highlight:
3165 case CK_Up_Alt_Highlight:
3166 case CK_Down:
3167 case CK_Down_Highlight:
3168 case CK_Down_Alt_Highlight:
3169 case CK_Page_Up:
3170 case CK_Page_Up_Highlight:
3171 case CK_Page_Up_Alt_Highlight:
3172 case CK_Page_Down:
3173 case CK_Page_Down_Highlight:
3174 case CK_Page_Down_Alt_Highlight:
3175 case CK_Beginning_Of_Text:
3176 case CK_Beginning_Of_Text_Highlight:
3177 case CK_End_Of_Text:
3178 case CK_End_Of_Text_Highlight:
3179 case CK_Paragraph_Up:
3180 case CK_Paragraph_Up_Highlight:
3181 case CK_Paragraph_Up_Alt_Highlight:
3182 case CK_Paragraph_Down:
3183 case CK_Paragraph_Down_Highlight:
3184 case CK_Paragraph_Down_Alt_Highlight:
3185 case CK_Scroll_Up:
3186 case CK_Scroll_Up_Highlight:
3187 case CK_Scroll_Up_Alt_Highlight:
3188 case CK_Scroll_Down:
3189 case CK_Scroll_Down_Highlight:
3190 case CK_Scroll_Down_Alt_Highlight:
3191 edit->search_start = edit->curs1;
3192 edit->found_len = 0;
3193 break;
3194 default:
3195 edit->found_len = 0;
3196 edit->prev_col = edit_get_col (edit);
3197 edit->search_start = edit->curs1;
3199 edit_find_bracket (edit);
3201 if (option_auto_para_formatting) {
3202 switch (command) {
3203 case CK_BackSpace:
3204 case CK_Delete:
3205 case CK_Delete_Word_Left:
3206 case CK_Delete_Word_Right:
3207 case CK_Delete_To_Line_End:
3208 case CK_Delete_To_Line_Begin:
3209 format_paragraph (edit, 0);
3210 edit->force |= REDRAW_PAGE;
3216 static void
3217 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3219 int i = 0;
3221 if (edit->macro_depth++ > 256) {
3222 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3223 edit->macro_depth--;
3224 return;
3226 edit->force |= REDRAW_PAGE;
3227 for (; i < n; i++) {
3228 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3230 edit_update_screen (edit);
3231 edit->macro_depth--;
3234 /* User edit menu, like user menu (F2) but only in editor. */
3235 static void
3236 user_menu (WEdit * edit)
3238 FILE *fd;
3239 int nomark;
3240 struct stat status;
3241 long start_mark, end_mark;
3242 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3243 int rc = 0;
3245 nomark = eval_marks (edit, &start_mark, &end_mark);
3246 if (!nomark) /* remember marked or not */
3247 edit_save_block (edit, block_file, start_mark, end_mark);
3249 /* run shell scripts from menu */
3250 user_menu_cmd (edit);
3252 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3253 /* no block messages */
3254 goto cleanup;
3257 if (!nomark) {
3258 /* i.e. we have marked block */
3259 rc = edit_block_delete_cmd (edit);
3262 if (!rc) {
3263 edit_insert_file (edit, block_file);
3266 /* truncate block file */
3267 if ((fd = fopen (block_file, "w"))) {
3268 fclose (fd);
3271 edit_refresh_cmd (edit);
3272 edit->force |= REDRAW_COMPLETELY;
3274 cleanup:
3275 g_free (block_file);
3278 void
3279 edit_stack_init (void)
3281 for (edit_stack_iterator = 0;
3282 edit_stack_iterator < MAX_HISTORY_MOVETO;
3283 edit_stack_iterator++ ) {
3284 edit_history_moveto[edit_stack_iterator].filename = NULL;
3285 edit_history_moveto[edit_stack_iterator].line = -1;
3288 edit_stack_iterator = 0;
3291 void
3292 edit_stack_free (void)
3294 for (edit_stack_iterator = 0;
3295 edit_stack_iterator < MAX_HISTORY_MOVETO;
3296 edit_stack_iterator++)
3297 g_free (edit_history_moveto[edit_stack_iterator].filename);