Ticket #1534 (cool cooledit)
[midnight-commander.git] / edit / edit.c
blob2bba5b591296bbc06e0eae5805ea484cadde76ad
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 "editcmddef.h"
48 #include "usermap.h"
50 #include "../src/tty/color.h" /* EDITOR_NORMAL_COLOR */
51 #include "../src/tty/tty.h" /* attrset() */
52 #include "../src/tty/key.h" /* is_idle() */
54 #include "../src/widget.h" /* buttonbar_redraw() */
55 #include "../src/cmd.h" /* view_other_cmd() */
56 #include "../src/user.h" /* user_menu_cmd() */
57 #include "../src/wtools.h" /* query_dialog() */
58 #include "../src/timefmt.h" /* time formatting */
59 #include "../src/strutil.h" /* utf string functions */
60 #include "../src/charsets.h" /* get_codepage_id */
61 #include "../src/main.h" /* source_codepage */
64 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
65 or EDIT_KEY_EMULATION_EMACS
67 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
69 int option_word_wrap_line_length = 72;
70 int option_typewriter_wrap = 0;
71 int option_auto_para_formatting = 0;
72 int option_tab_spacing = 8;
73 int option_fill_tabs_with_spaces = 0;
74 int option_return_does_auto_indent = 1;
75 int option_backspace_through_tabs = 0;
76 int option_fake_half_tabs = 1;
77 int option_save_mode = EDIT_QUICK_SAVE;
78 int option_save_position = 1;
79 int option_max_undo = 32768;
80 int option_persistent_selections = 1;
81 int option_line_state = 0;
82 int option_line_state_width = 0;
84 int option_edit_right_extreme = 0;
85 int option_edit_left_extreme = 0;
86 int option_edit_top_extreme = 0;
87 int option_edit_bottom_extreme = 0;
88 int enable_show_tabs_tws = 1;
90 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
91 char *option_backup_ext = NULL;
93 int edit_stack_iterator = 0;
94 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
95 /* magic sequense for say than block is vertical */
96 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
97 /*-
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
112 * ^ | ^
113 * | ^|^ |
114 * cursor ||| cursor
115 * |||
116 * file end|||file beginning
121 * This_is_some_file
122 * fin.
126 static void user_menu (WEdit *edit);
128 int edit_get_byte (WEdit * edit, long byte_index)
130 unsigned long p;
131 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
132 return '\n';
134 if (byte_index >= edit->curs1) {
135 p = edit->curs1 + edit->curs2 - byte_index - 1;
136 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
137 } else {
138 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
142 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
144 unsigned long p;
146 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
147 return NULL;
149 if (byte_index >= edit->curs1) {
150 p = edit->curs1 + edit->curs2 - byte_index - 1;
151 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
152 } else {
153 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
157 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
159 unsigned long p;
161 if (byte_index >= (edit->curs1 + edit->curs2) ) {
162 byte_index -= 1;
165 if ( byte_index < 0 ) {
166 return NULL;
169 if (byte_index >= edit->curs1) {
170 p = edit->curs1 + edit->curs2 - 1;
171 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
172 } else {
173 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
177 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
179 gchar *str = NULL;
180 int res = -1;
181 gunichar ch;
182 gchar *next_ch = NULL;
183 int width = 0;
185 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
186 *char_width = 0;
187 return '\n';
191 str = edit_get_byte_ptr (edit, byte_index);
192 res = g_utf8_get_char_validated (str, -1);
194 if ( res < 0 ) {
195 ch = *str;
196 width = 0;
197 } else {
198 ch = res;
199 /* Calculate UTF-8 char width */
200 next_ch = g_utf8_next_char(str);
201 if ( next_ch ) {
202 width = next_ch - str;
203 } else {
204 ch = 0;
205 width = 0;
208 *char_width = width;
209 return ch;
212 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
214 gchar *str, *buf = NULL;
215 int res = -1;
216 gunichar ch;
217 gchar *next_ch = NULL;
218 int width = 0;
220 if ( byte_index > 0 ) {
221 byte_index--;
224 ch = edit_get_utf (edit, byte_index, &width);
225 if ( width == 1 ) {
226 *char_width = width;
227 return ch;
230 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
231 *char_width = 0;
232 return 0;
235 str = edit_get_byte_ptr (edit, byte_index);
236 buf = edit_get_buf_ptr (edit, byte_index);
237 /* get prev utf8 char */
238 if ( str != buf )
239 str = g_utf8_find_prev_char (buf, str);
241 res = g_utf8_get_char_validated (str, -1);
242 if ( res < 0 ) {
243 ch = *str;
244 width = 0;
245 } else {
246 ch = res;
247 /* Calculate UTF-8 char width */
248 next_ch = g_utf8_next_char(str);
249 if ( next_ch ) {
250 width = next_ch - str;
251 } else {
252 ch = 0;
253 width = 0;
256 *char_width = width;
257 return ch;
261 * Initialize the buffers for an empty files.
263 static void
264 edit_init_buffers (WEdit *edit)
266 int j;
268 for (j = 0; j <= MAXBUFF; j++) {
269 edit->buffers1[j] = NULL;
270 edit->buffers2[j] = NULL;
273 edit->curs1 = 0;
274 edit->curs2 = 0;
275 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
279 * Load file OR text into buffers. Set cursor to the beginning of file.
280 * Return 1 on error.
282 static int
283 edit_load_file_fast (WEdit *edit, const char *filename)
285 long buf, buf2;
286 int file = -1;
287 #ifdef HAVE_CHARSET
288 const char *cp_id;
289 #endif
291 edit->curs2 = edit->last_byte;
292 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
293 edit->utf8 = 0;
294 #ifdef HAVE_CHARSET
295 cp_id = get_codepage_id (source_codepage);
297 if (cp_id != NULL)
298 edit->utf8 = str_isutf8 (cp_id);
299 #endif
300 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
301 GString *errmsg = g_string_new(NULL);
302 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
303 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
304 g_string_free (errmsg, TRUE);
305 return 1;
308 if (!edit->buffers2[buf2])
309 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
311 mc_read (file,
312 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
313 (edit->curs2 & M_EDIT_BUF_SIZE),
314 edit->curs2 & M_EDIT_BUF_SIZE);
316 for (buf = buf2 - 1; buf >= 0; buf--) {
317 /* edit->buffers2[0] is already allocated */
318 if (!edit->buffers2[buf])
319 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
320 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
323 mc_close (file);
324 return 0;
327 /* detecting an error on save is easy: just check if every byte has been written. */
328 /* detecting an error on read, is not so easy 'cos there is not way to tell
329 whether you read everything or not. */
330 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
331 static const struct edit_filters {
332 const char *read, *write, *extension;
333 } all_filters[] = {
334 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
335 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
336 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
337 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
338 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
341 /* Return index of the filter or -1 is there is no appropriate filter */
342 static int edit_find_filter (const char *filename)
344 size_t i, l, e;
345 if (!filename)
346 return -1;
347 l = strlen (filename);
348 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
349 e = strlen (all_filters[i].extension);
350 if (l > e)
351 if (!strcmp (all_filters[i].extension, filename + l - e))
352 return i;
354 return -1;
357 static char *
358 edit_get_filter (const char *filename)
360 int i, l;
361 char *p, *quoted_name;
362 i = edit_find_filter (filename);
363 if (i < 0)
364 return 0;
365 quoted_name = name_quote (filename, 0);
366 l = str_term_width1 (quoted_name);
367 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
368 sprintf (p, all_filters[i].read, quoted_name);
369 g_free (quoted_name);
370 return p;
373 char *
374 edit_get_write_filter (const char *write_name, const char *filename)
376 int i, l;
377 char *p, *writename;
378 i = edit_find_filter (filename);
379 if (i < 0)
380 return 0;
381 writename = name_quote (write_name, 0);
382 l = str_term_width1 (writename);
383 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
384 sprintf (p, all_filters[i].write, writename);
385 g_free (writename);
386 return p;
389 static long
390 edit_insert_stream (WEdit * edit, FILE * f)
392 int c;
393 long i = 0;
394 while ((c = fgetc (f)) >= 0) {
395 edit_insert (edit, c);
396 i++;
398 return i;
401 long edit_write_stream (WEdit * edit, FILE * f)
403 long i;
404 for (i = 0; i < edit->last_byte; i++)
405 if (fputc (edit_get_byte (edit, i), f) < 0)
406 break;
407 return i;
410 #define TEMP_BUF_LEN 1024
412 /* inserts a file at the cursor, returns 1 on success */
414 edit_insert_file (WEdit *edit, const char *filename)
416 char *p;
417 if ((p = edit_get_filter (filename))) {
418 FILE *f;
419 long current = edit->curs1;
420 f = (FILE *) popen (p, "r");
421 if (f) {
422 edit_insert_stream (edit, f);
423 edit_cursor_move (edit, current - edit->curs1);
424 if (pclose (f) > 0) {
425 GString *errmsg = g_string_new (NULL);
426 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
427 edit_error_dialog (_("Error"), errmsg->str);
428 g_string_free (errmsg, TRUE);
429 g_free (p);
430 return 0;
432 } else {
433 GString *errmsg = g_string_new (NULL);
434 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
435 edit_error_dialog (_("Error"), errmsg->str);
436 g_string_free (errmsg, TRUE);
437 g_free (p);
438 return 0;
440 g_free (p);
441 } else {
442 int i, file, blocklen;
443 long current = edit->curs1;
444 int vertical_insertion = 0;
445 char *buf;
446 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
447 return 0;
448 buf = g_malloc (TEMP_BUF_LEN);
449 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
450 if (blocklen > 0) {
451 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
452 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
453 vertical_insertion = 1;
454 } else {
455 mc_lseek (file, 0, SEEK_SET);
458 if (vertical_insertion) {
459 blocklen = edit_insert_column_of_text_from_file (edit, file);
460 } else {
461 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
462 for (i = 0; i < blocklen; i++)
463 edit_insert (edit, buf[i]);
466 edit_cursor_move (edit, current - edit->curs1);
467 g_free (buf);
468 mc_close (file);
469 if (blocklen)
470 return 0;
472 return 1;
475 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
476 static int
477 check_file_access (WEdit *edit, const char *filename, struct stat *st)
479 int file;
480 GString *errmsg = (GString *) 0;
482 /* Try opening an existing file */
483 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
485 if (file < 0) {
487 * Try creating the file. O_EXCL prevents following broken links
488 * and opening existing files.
490 file =
491 mc_open (filename,
492 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
493 0666);
494 if (file < 0) {
495 g_string_sprintf (errmsg = g_string_new (NULL),
496 _(" Cannot open %s for reading "), filename);
497 goto cleanup;
498 } else {
499 /* New file, delete it if it's not modified or saved */
500 edit->delete_file = 1;
504 /* Check what we have opened */
505 if (mc_fstat (file, st) < 0) {
506 g_string_sprintf (errmsg = g_string_new (NULL),
507 _(" Cannot get size/permissions for %s "), filename);
508 goto cleanup;
511 /* We want to open regular files only */
512 if (!S_ISREG (st->st_mode)) {
513 g_string_sprintf (errmsg = g_string_new (NULL),
514 _(" %s is not a regular file "), filename);
515 goto cleanup;
519 * Don't delete non-empty files.
520 * O_EXCL should prevent it, but let's be on the safe side.
522 if (st->st_size > 0) {
523 edit->delete_file = 0;
526 if (st->st_size >= SIZE_LIMIT) {
527 g_string_sprintf (errmsg = g_string_new (NULL),
528 _(" File %s is too large "), filename);
529 goto cleanup;
532 cleanup:
533 (void) mc_close (file);
534 if (errmsg) {
535 edit_error_dialog (_("Error"), errmsg->str);
536 g_string_free (errmsg, TRUE);
537 return 1;
539 return 0;
543 * Open the file and load it into the buffers, either directly or using
544 * a filter. Return 0 on success, 1 on error.
546 * Fast loading (edit_load_file_fast) is used when the file size is
547 * known. In this case the data is read into the buffers by blocks.
548 * If the file size is not known, the data is loaded byte by byte in
549 * edit_insert_file.
551 static int
552 edit_load_file (WEdit *edit)
554 int fast_load = 1;
556 /* Cannot do fast load if a filter is used */
557 if (edit_find_filter (edit->filename) >= 0)
558 fast_load = 0;
561 * VFS may report file size incorrectly, and slow load is not a big
562 * deal considering overhead in VFS.
564 if (!vfs_file_is_local (edit->filename))
565 fast_load = 0;
568 * FIXME: line end translation should disable fast loading as well
569 * Consider doing fseek() to the end and ftell() for the real size.
572 if (*edit->filename) {
573 /* If we are dealing with a real file, check that it exists */
574 if (check_file_access (edit, edit->filename, &edit->stat1))
575 return 1;
576 } else {
577 /* nothing to load */
578 fast_load = 0;
581 edit_init_buffers (edit);
583 if (fast_load) {
584 edit->last_byte = edit->stat1.st_size;
585 edit_load_file_fast (edit, edit->filename);
586 /* If fast load was used, the number of lines wasn't calculated */
587 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
588 } else {
589 #ifdef HAVE_CHARSET
590 const char *codepage_id;
591 #endif
592 edit->last_byte = 0;
593 if (*edit->filename) {
594 edit->stack_disable = 1;
595 if (!edit_insert_file (edit, edit->filename)) {
596 edit_clean (edit);
597 return 1;
599 edit->stack_disable = 0;
602 #ifdef HAVE_CHARSET
603 codepage_id = get_codepage_id( source_codepage );
604 if ( codepage_id )
605 edit->utf8 = str_isutf8 ( codepage_id );
606 #endif
608 return 0;
611 /* Restore saved cursor position in the file */
612 static void
613 edit_load_position (WEdit *edit)
615 char *filename;
616 long line, column;
618 if (!edit->filename || !*edit->filename)
619 return;
621 filename = vfs_canon (edit->filename);
622 load_file_position (filename, &line, &column);
623 g_free (filename);
625 edit_move_to_line (edit, line - 1);
626 edit->prev_col = column;
627 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
628 edit_move_display (edit, line - (edit->num_widget_lines / 2));
631 /* Save cursor position in the file */
632 static void
633 edit_save_position (WEdit *edit)
635 char *filename;
637 if (!edit->filename || !*edit->filename)
638 return;
640 filename = vfs_canon (edit->filename);
641 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
642 g_free (filename);
645 /* Clean the WEdit stricture except the widget part */
646 static inline void
647 edit_purge_widget (WEdit *edit)
649 int len = sizeof (WEdit) - sizeof (Widget);
650 char *start = (char *) edit + sizeof (Widget);
651 memset (start, 0, len);
652 edit->macro_i = -1; /* not recording a macro */
655 #define space_width 1
658 * Fill in the edit structure. Return NULL on failure. Pass edit as
659 * NULL to allocate a new structure.
661 * If line is 0, try to restore saved position. Otherwise put the
662 * cursor on that line and show it in the middle of the screen.
664 WEdit *
665 edit_init (WEdit *edit, int lines, int columns, const char *filename,
666 long line)
668 int to_free = 0;
669 option_auto_syntax = 1; /* Resetting to auto on every invokation */
670 if ( option_line_state ) {
671 option_line_state_width = LINE_STATE_WIDTH;
672 } else {
673 option_line_state_width = 0;
675 if (!edit) {
676 #ifdef ENABLE_NLS
678 * Expand option_whole_chars_search by national letters using
679 * current locale
682 static char option_whole_chars_search_buf[256];
684 if (option_whole_chars_search_buf != option_whole_chars_search) {
685 size_t i;
686 size_t len = str_term_width1 (option_whole_chars_search);
688 strcpy (option_whole_chars_search_buf,
689 option_whole_chars_search);
691 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
692 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
693 option_whole_chars_search_buf[len++] = i;
697 option_whole_chars_search_buf[len] = 0;
698 option_whole_chars_search = option_whole_chars_search_buf;
700 #endif /* ENABLE_NLS */
701 edit = g_malloc0 (sizeof (WEdit));
702 edit->search = NULL;
703 to_free = 1;
705 edit_purge_widget (edit);
706 edit->num_widget_lines = lines;
707 edit->over_col = 0;
708 edit->num_widget_columns = columns;
709 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
710 edit->stat1.st_uid = getuid ();
711 edit->stat1.st_gid = getgid ();
712 edit->stat1.st_mtime = 0;
713 edit->bracket = -1;
714 edit->force |= REDRAW_PAGE;
715 edit_set_filename (edit, filename);
716 edit->stack_size = START_STACK_SIZE;
717 edit->stack_size_mask = START_STACK_SIZE - 1;
718 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
719 if (edit_load_file (edit)) {
720 /* edit_load_file already gives an error message */
721 if (to_free)
722 g_free (edit);
723 return 0;
725 edit->loading_done = 1;
726 edit->modified = 0;
727 edit->locked = 0;
728 edit_load_syntax (edit, 0, 0);
730 int color;
731 edit_get_syntax_color (edit, -1, &color);
734 /* load saved cursor position */
735 if ((line == 0) && option_save_position) {
736 edit_load_position (edit);
737 } else {
738 if (line <= 0)
739 line = 1;
740 edit_move_display (edit, line - 1);
741 edit_move_to_line (edit, line - 1);
744 edit_load_user_map(edit);
746 return edit;
749 /* Clear the edit struct, freeing everything in it. Return 1 on success */
751 edit_clean (WEdit *edit)
753 int j = 0;
755 if (!edit)
756 return 0;
758 /* a stale lock, remove it */
759 if (edit->locked)
760 edit->locked = edit_unlock_file (edit->filename);
762 /* save cursor position */
763 if (option_save_position)
764 edit_save_position (edit);
766 /* File specified on the mcedit command line and never saved */
767 if (edit->delete_file)
768 unlink (edit->filename);
770 edit_free_syntax_rules (edit);
771 book_mark_flush (edit, -1);
772 for (; j <= MAXBUFF; j++) {
773 g_free (edit->buffers1[j]);
774 g_free (edit->buffers2[j]);
777 g_free (edit->undo_stack);
778 g_free (edit->filename);
779 g_free (edit->dir);
781 if (edit->search)
783 mc_search_free(edit->search);
784 edit->search = NULL;
786 edit_purge_widget (edit);
788 return 1;
792 /* returns 1 on success */
793 int edit_renew (WEdit * edit)
795 int lines = edit->num_widget_lines;
796 int columns = edit->num_widget_columns;
797 int retval = 1;
799 edit_clean (edit);
800 if (!edit_init (edit, lines, columns, "", 0))
801 retval = 0;
802 return retval;
806 * Load a new file into the editor. If it fails, preserve the old file.
807 * To do it, allocate a new widget, initialize it and, if the new file
808 * was loaded, copy the data to the old widget.
809 * Return 1 on success, 0 on failure.
812 edit_reload (WEdit *edit, const char *filename)
814 WEdit *e;
815 int lines = edit->num_widget_lines;
816 int columns = edit->num_widget_columns;
818 e = g_malloc0 (sizeof (WEdit));
819 e->widget = edit->widget;
820 if (!edit_init (e, lines, columns, filename, 0)) {
821 g_free (e);
822 return 0;
824 edit_clean (edit);
825 memcpy (edit, e, sizeof (WEdit));
826 g_free (e);
827 return 1;
831 * Load a new file into the editor and set line. If it fails, preserve the old file.
832 * To do it, allocate a new widget, initialize it and, if the new file
833 * was loaded, copy the data to the old widget.
834 * Return 1 on success, 0 on failure.
837 edit_reload_line (WEdit *edit, const char *filename, long line)
839 WEdit *e;
840 int lines = edit->num_widget_lines;
841 int columns = edit->num_widget_columns;
843 e = g_malloc0 (sizeof (WEdit));
844 e->widget = edit->widget;
845 if (!edit_init (e, lines, columns, filename, line)) {
846 g_free (e);
847 return 0;
849 edit_clean (edit);
850 memcpy (edit, e, sizeof (WEdit));
851 g_free (e);
852 return 1;
857 Recording stack for undo:
858 The following is an implementation of a compressed stack. Identical
859 pushes are recorded by a negative prefix indicating the number of times the
860 same char was pushed. This saves space for repeated curs-left or curs-right
861 delete etc.
865 pushed: stored:
869 b -3
871 c --> -4
877 If the stack long int is 0-255 it represents a normal insert (from a backspace),
878 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
879 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
880 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
881 position.
883 The only way the cursor moves or the buffer is changed is through the routines:
884 insert, backspace, insert_ahead, delete, and cursor_move.
885 These record the reverse undo movements onto the stack each time they are
886 called.
888 Each key press results in a set of actions (insert; delete ...). So each time
889 a key is pressed the current position of start_display is pushed as
890 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
891 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
892 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
896 void edit_push_action (WEdit * edit, long c,...)
898 unsigned long sp = edit->stack_pointer;
899 unsigned long spm1;
900 long *t;
902 /* first enlarge the stack if necessary */
903 if (sp > edit->stack_size - 10) { /* say */
904 if (option_max_undo < 256)
905 option_max_undo = 256;
906 if (edit->stack_size < (unsigned long) option_max_undo) {
907 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
908 if (t) {
909 edit->undo_stack = t;
910 edit->stack_size <<= 1;
911 edit->stack_size_mask = edit->stack_size - 1;
915 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
916 if (edit->stack_disable)
917 return;
919 #ifdef FAST_MOVE_CURSOR
920 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
921 va_list ap;
922 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
923 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
924 va_start (ap, c);
925 c = -(va_arg (ap, int));
926 va_end (ap);
927 } else
928 #endif /* ! FAST_MOVE_CURSOR */
929 if (edit->stack_bottom != sp
930 && spm1 != edit->stack_bottom
931 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
932 int d;
933 if (edit->undo_stack[spm1] < 0) {
934 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
935 if (d == c) {
936 if (edit->undo_stack[spm1] > -1000000000) {
937 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
938 edit->undo_stack[spm1]--;
939 return;
942 /* #define NO_STACK_CURSMOVE_ANIHILATION */
943 #ifndef NO_STACK_CURSMOVE_ANIHILATION
944 else if ((c == CURS_LEFT && d == CURS_RIGHT)
945 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
946 if (edit->undo_stack[spm1] == -2)
947 edit->stack_pointer = spm1;
948 else
949 edit->undo_stack[spm1]++;
950 return;
952 #endif
953 } else {
954 d = edit->undo_stack[spm1];
955 if (d == c) {
956 if (c >= KEY_PRESS)
957 return; /* --> no need to push multiple do-nothings */
958 edit->undo_stack[sp] = -2;
959 goto check_bottom;
961 #ifndef NO_STACK_CURSMOVE_ANIHILATION
962 else if ((c == CURS_LEFT && d == CURS_RIGHT)
963 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
964 edit->stack_pointer = spm1;
965 return;
967 #endif
970 edit->undo_stack[sp] = c;
971 check_bottom:
973 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
975 /* if the sp wraps round and catches the stack_bottom then erase
976 * the first set of actions on the stack to make space - by moving
977 * stack_bottom forward one "key press" */
978 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
979 if ((unsigned long) c == edit->stack_bottom ||
980 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
981 do {
982 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
983 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
985 /*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: */
986 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
987 edit->stack_bottom = edit->stack_pointer = 0;
991 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
992 then the file should be as it was when he loaded up. Then set edit->modified to 0.
994 static long
995 pop_action (WEdit * edit)
997 long c;
998 unsigned long sp = edit->stack_pointer;
999 if (sp == edit->stack_bottom) {
1000 return STACK_BOTTOM;
1002 sp = (sp - 1) & edit->stack_size_mask;
1003 if ((c = edit->undo_stack[sp]) >= 0) {
1004 /* edit->undo_stack[sp] = '@'; */
1005 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1006 return c;
1008 if (sp == edit->stack_bottom) {
1009 return STACK_BOTTOM;
1011 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1012 if (edit->undo_stack[sp] == -2) {
1013 /* edit->undo_stack[sp] = '@'; */
1014 edit->stack_pointer = sp;
1015 } else
1016 edit->undo_stack[sp]++;
1018 return c;
1021 /* is called whenever a modification is made by one of the four routines below */
1022 static inline void edit_modification (WEdit * edit)
1024 edit->caches_valid = 0;
1025 edit->screen_modified = 1;
1027 /* raise lock when file modified */
1028 if (!edit->modified && !edit->delete_file)
1029 edit->locked = edit_lock_file (edit->filename);
1030 edit->modified = 1;
1034 Basic low level single character buffer alterations and movements at the cursor.
1035 Returns char passed over, inserted or removed.
1038 void
1039 edit_insert (WEdit *edit, int c)
1041 /* check if file has grown to large */
1042 if (edit->last_byte >= SIZE_LIMIT)
1043 return;
1045 /* first we must update the position of the display window */
1046 if (edit->curs1 < edit->start_display) {
1047 edit->start_display++;
1048 if (c == '\n')
1049 edit->start_line++;
1052 /* Mark file as modified, unless the file hasn't been fully loaded */
1053 if (edit->loading_done) {
1054 edit_modification (edit);
1057 /* now we must update some info on the file and check if a redraw is required */
1058 if (c == '\n') {
1059 if (edit->book_mark)
1060 book_mark_inc (edit, edit->curs_line);
1061 edit->curs_line++;
1062 edit->total_lines++;
1063 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1066 /* save the reverse command onto the undo stack */
1067 edit_push_action (edit, BACKSPACE);
1069 /* update markers */
1070 edit->mark1 += (edit->mark1 > edit->curs1);
1071 edit->mark2 += (edit->mark2 > edit->curs1);
1072 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1074 /* add a new buffer if we've reached the end of the last one */
1075 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1076 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1077 g_malloc (EDIT_BUF_SIZE);
1079 /* perform the insertion */
1080 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1081 curs1 & M_EDIT_BUF_SIZE]
1082 = (unsigned char) c;
1084 /* update file length */
1085 edit->last_byte++;
1087 /* update cursor position */
1088 edit->curs1++;
1091 void
1092 edit_insert_over (WEdit * edit)
1094 for (int i = 0; i < edit->over_col; i++ ) {
1095 edit_insert (edit, ' ');
1097 edit->over_col = 0;
1100 /* same as edit_insert and move left */
1101 void edit_insert_ahead (WEdit * edit, int c)
1103 if (edit->last_byte >= SIZE_LIMIT)
1104 return;
1105 if (edit->curs1 < edit->start_display) {
1106 edit->start_display++;
1107 if (c == '\n')
1108 edit->start_line++;
1110 edit_modification (edit);
1111 if (c == '\n') {
1112 if (edit->book_mark)
1113 book_mark_inc (edit, edit->curs_line);
1114 edit->total_lines++;
1115 edit->force |= REDRAW_AFTER_CURSOR;
1117 edit_push_action (edit, DELCHAR);
1119 edit->mark1 += (edit->mark1 >= edit->curs1);
1120 edit->mark2 += (edit->mark2 >= edit->curs1);
1121 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1123 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1124 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1125 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1127 edit->last_byte++;
1128 edit->curs2++;
1132 int edit_delete (WEdit * edit, const int byte_delete)
1134 int p = 0;
1135 int cw = 1;
1136 int i;
1138 if (!edit->curs2)
1139 return 0;
1141 edit->mark1 -= (edit->mark1 > edit->curs1);
1142 edit->mark2 -= (edit->mark2 > edit->curs1);
1143 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1145 cw = 1;
1146 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1147 if ( edit->utf8 && byte_delete == 0 ) {
1148 edit_get_utf (edit, edit->curs1, &cw);
1149 if ( cw < 1 )
1150 cw = 1;
1152 for ( i = 1; i<= cw; i++ ) {
1153 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1155 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1156 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1157 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1159 edit->last_byte--;
1160 edit->curs2--;
1161 edit_push_action (edit, p + 256);
1164 edit_modification (edit);
1165 if (p == '\n') {
1166 if (edit->book_mark)
1167 book_mark_dec (edit, edit->curs_line);
1168 edit->total_lines--;
1169 edit->force |= REDRAW_AFTER_CURSOR;
1171 if (edit->curs1 < edit->start_display) {
1172 edit->start_display--;
1173 if (p == '\n')
1174 edit->start_line--;
1177 return p;
1181 static int
1182 edit_backspace (WEdit * edit, const int byte_delete)
1184 int p = 0;
1185 int cw = 1;
1186 int i;
1188 if (!edit->curs1)
1189 return 0;
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 cw = 1;
1196 if ( edit->utf8 && byte_delete == 0 ) {
1197 edit_get_prev_utf (edit, edit->curs1, &cw);
1198 if ( cw < 1 )
1199 cw = 1;
1201 for ( i = 1; i<= cw; i++ ) {
1202 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1203 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1204 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1205 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1207 edit->last_byte--;
1208 edit->curs1--;
1209 edit_push_action (edit, p);
1211 edit_modification (edit);
1212 if (p == '\n') {
1213 if (edit->book_mark)
1214 book_mark_dec (edit, edit->curs_line);
1215 edit->curs_line--;
1216 edit->total_lines--;
1217 edit->force |= REDRAW_AFTER_CURSOR;
1220 if (edit->curs1 < edit->start_display) {
1221 edit->start_display--;
1222 if (p == '\n')
1223 edit->start_line--;
1226 return p;
1229 #ifdef FAST_MOVE_CURSOR
1231 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1233 unsigned long next;
1234 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1235 edit->curs_line--;
1236 next -= (unsigned long) dest;
1237 n -= next;
1238 src += next;
1239 dest += next;
1244 edit_move_backward_lots (WEdit *edit, long increment)
1246 int r, s, t;
1247 unsigned char *p;
1249 if (increment > edit->curs1)
1250 increment = edit->curs1;
1251 if (increment <= 0)
1252 return -1;
1253 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1255 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1256 if (r > increment)
1257 r = increment;
1258 s = edit->curs1 & M_EDIT_BUF_SIZE;
1260 p = 0;
1261 if (s > r) {
1262 memqcpy (edit,
1263 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1264 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1266 } else {
1267 if (s) {
1268 memqcpy (edit,
1269 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1270 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1271 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1272 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1274 memqcpy (edit,
1275 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1276 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1277 EDIT_BUF_SIZE - (r - s), r - s);
1279 increment -= r;
1280 edit->curs1 -= r;
1281 edit->curs2 += r;
1282 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1283 if (p)
1284 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1285 else
1286 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1287 g_malloc (EDIT_BUF_SIZE);
1288 } else {
1289 g_free (p);
1292 s = edit->curs1 & M_EDIT_BUF_SIZE;
1293 while (increment) {
1294 p = 0;
1295 r = EDIT_BUF_SIZE;
1296 if (r > increment)
1297 r = increment;
1298 t = s;
1299 if (r < t)
1300 t = r;
1301 memqcpy (edit,
1302 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1303 EDIT_BUF_SIZE - t,
1304 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1306 if (r >= s) {
1307 if (t) {
1308 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1309 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1311 memqcpy (edit,
1312 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1313 EDIT_BUF_SIZE - r,
1314 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1315 EDIT_BUF_SIZE - (r - s), r - s);
1317 increment -= r;
1318 edit->curs1 -= r;
1319 edit->curs2 += r;
1320 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1321 if (p)
1322 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1323 else
1324 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1325 g_malloc (EDIT_BUF_SIZE);
1326 } else {
1327 g_free (p);
1330 return edit_get_byte (edit, edit->curs1);
1333 #endif /* ! FAST_MOVE_CURSOR */
1335 /* moves the cursor right or left: increment positive or negative respectively */
1336 void edit_cursor_move (WEdit * edit, long increment)
1338 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1339 int c;
1341 #ifdef FAST_MOVE_CURSOR
1342 if (increment < -256) {
1343 edit->force |= REDRAW_PAGE;
1344 edit_move_backward_lots (edit, -increment);
1345 return;
1347 #endif /* ! FAST_MOVE_CURSOR */
1349 if (increment < 0) {
1350 for (; increment < 0; increment++) {
1351 if (!edit->curs1)
1352 return;
1354 edit_push_action (edit, CURS_RIGHT);
1356 c = edit_get_byte (edit, edit->curs1 - 1);
1357 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1358 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1359 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1360 edit->curs2++;
1361 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1362 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1363 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1364 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1366 edit->curs1--;
1367 if (c == '\n') {
1368 edit->curs_line--;
1369 edit->force |= REDRAW_LINE_BELOW;
1373 } else if (increment > 0) {
1374 for (; increment > 0; increment--) {
1375 if (!edit->curs2)
1376 return;
1378 edit_push_action (edit, CURS_LEFT);
1380 c = edit_get_byte (edit, edit->curs1);
1381 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1382 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1383 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1384 edit->curs1++;
1385 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1386 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1387 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1388 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1390 edit->curs2--;
1391 if (c == '\n') {
1392 edit->curs_line++;
1393 edit->force |= REDRAW_LINE_ABOVE;
1399 /* These functions return positions relative to lines */
1401 /* returns index of last char on line + 1 */
1402 long edit_eol (WEdit * edit, long current)
1404 if (current < edit->last_byte) {
1405 for (;; current++)
1406 if (edit_get_byte (edit, current) == '\n')
1407 break;
1408 } else
1409 return edit->last_byte;
1410 return current;
1413 /* returns index of first char on line */
1414 long edit_bol (WEdit * edit, long current)
1416 if (current > 0) {
1417 for (;; current--)
1418 if (edit_get_byte (edit, current - 1) == '\n')
1419 break;
1420 } else
1421 return 0;
1422 return current;
1426 int edit_count_lines (WEdit * edit, long current, int upto)
1428 int lines = 0;
1429 if (upto > edit->last_byte)
1430 upto = edit->last_byte;
1431 if (current < 0)
1432 current = 0;
1433 while (current < upto)
1434 if (edit_get_byte (edit, current++) == '\n')
1435 lines++;
1436 return lines;
1440 /* If lines is zero this returns the count of lines from current to upto. */
1441 /* If upto is zero returns index of lines forward current. */
1442 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1444 if (upto) {
1445 return edit_count_lines (edit, current, upto);
1446 } else {
1447 int next;
1448 if (lines < 0)
1449 lines = 0;
1450 while (lines--) {
1451 next = edit_eol (edit, current) + 1;
1452 if (next > edit->last_byte)
1453 break;
1454 else
1455 current = next;
1457 return current;
1462 /* Returns offset of 'lines' lines up from current */
1463 long edit_move_backward (WEdit * edit, long current, int lines)
1465 if (lines < 0)
1466 lines = 0;
1467 current = edit_bol (edit, current);
1468 while((lines--) && current != 0)
1469 current = edit_bol (edit, current - 1);
1470 return current;
1473 /* If cols is zero this returns the count of columns from current to upto. */
1474 /* If upto is zero returns index of cols across from current. */
1475 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1477 long p, q;
1478 int col = 0;
1479 #ifdef HAVE_CHARSET
1480 int cw = 1;
1481 int utf_ch = 0;
1482 #endif
1483 if (upto) {
1484 q = upto;
1485 cols = -10;
1486 } else
1487 q = edit->last_byte + 2;
1488 for (col = 0, p = current; p < q; p++) {
1489 int c;
1490 #ifdef HAVE_CHARSET
1491 cw = 1;
1492 utf_ch = 0;
1493 #endif
1494 if (cols != -10) {
1495 if (col == cols)
1496 return p;
1497 if (col > cols)
1498 return p - 1;
1500 #ifdef HAVE_CHARSET
1501 if ( !edit->utf8 ) {
1502 #endif
1503 c = edit_get_byte (edit, p);
1504 #ifdef HAVE_CHARSET
1505 } else {
1506 cw = 1;
1507 c = edit_get_byte (edit, p);
1508 utf_ch = edit_get_utf (edit, p, &cw);
1510 if ( utf8_display ) {
1511 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1512 col++;
1514 #endif
1515 if (c == '\t')
1516 col += TAB_SIZE - col % TAB_SIZE;
1517 else if (c == '\n') {
1518 if (upto)
1519 return col;
1520 else
1521 return p;
1522 } else if (c < 32 || c == 127)
1523 col += 2; /* Caret notation for control characters */
1524 else
1525 col++;
1526 #ifdef HAVE_CHARSET
1527 if ( cw > 1 )
1528 col -= cw-1;
1529 #endif
1531 return col;
1534 /* returns the current column position of the cursor */
1535 int edit_get_col (WEdit * edit)
1537 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1541 /* Scrolling functions */
1543 void edit_update_curs_row (WEdit * edit)
1545 edit->curs_row = edit->curs_line - edit->start_line;
1548 void edit_update_curs_col (WEdit * edit)
1550 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1) + option_line_state_width;
1554 edit_get_curs_col (const WEdit *edit)
1556 return edit->curs_col;
1559 /*moves the display start position up by i lines */
1560 void edit_scroll_upward (WEdit * edit, unsigned long i)
1562 unsigned long lines_above = edit->start_line;
1563 if (i > lines_above)
1564 i = lines_above;
1565 if (i) {
1566 edit->start_line -= i;
1567 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1568 edit->force |= REDRAW_PAGE;
1569 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1571 edit_update_curs_row (edit);
1575 /* returns 1 if could scroll, 0 otherwise */
1576 void edit_scroll_downward (WEdit * edit, int i)
1578 int lines_below;
1579 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1580 if (lines_below > 0) {
1581 if (i > lines_below)
1582 i = lines_below;
1583 edit->start_line += i;
1584 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1585 edit->force |= REDRAW_PAGE;
1586 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1588 edit_update_curs_row (edit);
1591 void edit_scroll_right (WEdit * edit, int i)
1593 edit->force |= REDRAW_PAGE;
1594 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1595 edit->start_col -= i;
1598 void edit_scroll_left (WEdit * edit, int i)
1600 if (edit->start_col) {
1601 edit->start_col += i;
1602 if (edit->start_col > 0)
1603 edit->start_col = 0;
1604 edit->force |= REDRAW_PAGE;
1605 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1609 /* high level cursor movement commands */
1611 static int is_in_indent (WEdit *edit)
1613 long p = edit_bol (edit, edit->curs1);
1614 while (p < edit->curs1)
1615 if (!strchr (" \t", edit_get_byte (edit, p++)))
1616 return 0;
1617 return 1;
1620 static int left_of_four_spaces (WEdit *edit);
1622 void
1623 edit_move_to_prev_col (WEdit * edit, long p)
1625 int prev = edit->prev_col;
1626 int over = edit->over_col;
1628 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1630 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1632 if (line_len < prev + edit->over_col) {
1633 edit->over_col = prev + over - line_len;
1634 edit->prev_col = line_len;
1635 edit->curs_col = line_len;
1636 } else {
1637 edit->curs_col = prev + over;
1638 edit->prev_col = edit->curs_col;
1639 edit->over_col = 0;
1643 /* move i lines */
1644 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1646 unsigned long p, l = edit->curs_line;
1648 if (i > l)
1649 i = l;
1650 if (i) {
1651 if (i > 1)
1652 edit->force |= REDRAW_PAGE;
1653 if (scroll)
1654 edit_scroll_upward (edit, i);
1656 p = edit_bol (edit, edit->curs1);
1657 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1658 edit_move_to_prev_col (edit, p);
1660 edit->search_start = edit->curs1;
1661 edit->found_len = 0;
1665 static int
1666 is_blank (WEdit *edit, long offset)
1668 long s, f;
1669 int c;
1670 s = edit_bol (edit, offset);
1671 f = edit_eol (edit, offset) - 1;
1672 while (s <= f) {
1673 c = edit_get_byte (edit, s++);
1674 if (!isspace (c))
1675 return 0;
1677 return 1;
1681 /* returns the offset of line i */
1682 static long
1683 edit_find_line (WEdit *edit, int line)
1685 int i, j = 0;
1686 int m = 2000000000;
1687 if (!edit->caches_valid) {
1688 for (i = 0; i < N_LINE_CACHES; i++)
1689 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1690 /* three offsets that we *know* are line 0 at 0 and these two: */
1691 edit->line_numbers[1] = edit->curs_line;
1692 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1693 edit->line_numbers[2] = edit->total_lines;
1694 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1695 edit->caches_valid = 1;
1697 if (line >= edit->total_lines)
1698 return edit->line_offsets[2];
1699 if (line <= 0)
1700 return 0;
1701 /* find the closest known point */
1702 for (i = 0; i < N_LINE_CACHES; i++) {
1703 int n;
1704 n = abs (edit->line_numbers[i] - line);
1705 if (n < m) {
1706 m = n;
1707 j = i;
1710 if (m == 0)
1711 return edit->line_offsets[j]; /* know the offset exactly */
1712 if (m == 1 && j >= 3)
1713 i = j; /* one line different - caller might be looping, so stay in this cache */
1714 else
1715 i = 3 + (rand () % (N_LINE_CACHES - 3));
1716 if (line > edit->line_numbers[j])
1717 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1718 else
1719 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1720 edit->line_numbers[i] = line;
1721 return edit->line_offsets[i];
1724 int line_is_blank (WEdit * edit, long line)
1726 return is_blank (edit, edit_find_line (edit, line));
1729 /* moves up until a blank line is reached, or until just
1730 before a non-blank line is reached */
1731 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1733 int i;
1734 if (edit->curs_line <= 1) {
1735 i = 0;
1736 } else {
1737 if (line_is_blank (edit, edit->curs_line)) {
1738 if (line_is_blank (edit, edit->curs_line - 1)) {
1739 for (i = edit->curs_line - 1; i; i--)
1740 if (!line_is_blank (edit, i)) {
1741 i++;
1742 break;
1744 } else {
1745 for (i = edit->curs_line - 1; i; i--)
1746 if (line_is_blank (edit, i))
1747 break;
1749 } else {
1750 for (i = edit->curs_line - 1; i; i--)
1751 if (line_is_blank (edit, i))
1752 break;
1755 edit_move_up (edit, edit->curs_line - i, scroll);
1758 /* move i lines */
1759 void edit_move_down (WEdit * edit, int i, int scroll)
1761 long p, l = edit->total_lines - edit->curs_line;
1763 if (i > l)
1764 i = l;
1765 if (i) {
1766 if (i > 1)
1767 edit->force |= REDRAW_PAGE;
1768 if (scroll)
1769 edit_scroll_downward (edit, i);
1770 p = edit_bol (edit, edit->curs1);
1771 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1772 edit_move_to_prev_col (edit, p);
1774 edit->search_start = edit->curs1;
1775 edit->found_len = 0;
1779 /* moves down until a blank line is reached, or until just
1780 before a non-blank line is reached */
1781 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1783 int i;
1784 if (edit->curs_line >= edit->total_lines - 1) {
1785 i = edit->total_lines;
1786 } else {
1787 if (line_is_blank (edit, edit->curs_line)) {
1788 if (line_is_blank (edit, edit->curs_line + 1)) {
1789 for (i = edit->curs_line + 1; i; i++)
1790 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1791 i--;
1792 break;
1794 } else {
1795 for (i = edit->curs_line + 1; i; i++)
1796 if (line_is_blank (edit, i) || i >= edit->total_lines)
1797 break;
1799 } else {
1800 for (i = edit->curs_line + 1; i; i++)
1801 if (line_is_blank (edit, i) || i >= edit->total_lines)
1802 break;
1805 edit_move_down (edit, i - edit->curs_line, scroll);
1808 static void edit_begin_page (WEdit *edit)
1810 edit_update_curs_row (edit);
1811 edit_move_up (edit, edit->curs_row, 0);
1814 static void edit_end_page (WEdit *edit)
1816 edit_update_curs_row (edit);
1817 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1821 /* goto beginning of text */
1822 static void edit_move_to_top (WEdit * edit)
1824 if (edit->curs_line) {
1825 edit_cursor_move (edit, -edit->curs1);
1826 edit_move_to_prev_col (edit, 0);
1827 edit->force |= REDRAW_PAGE;
1828 edit->search_start = 0;
1829 edit_update_curs_row(edit);
1834 /* goto end of text */
1835 static void edit_move_to_bottom (WEdit * edit)
1837 if (edit->curs_line < edit->total_lines) {
1838 edit_cursor_move (edit, edit->curs2);
1839 edit->start_display = edit->last_byte;
1840 edit->start_line = edit->total_lines;
1841 edit_update_curs_row(edit);
1842 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1843 edit->force |= REDRAW_PAGE;
1847 /* goto beginning of line */
1848 static void edit_cursor_to_bol (WEdit * edit)
1850 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1851 edit->search_start = edit->curs1;
1852 edit->prev_col = edit_get_col (edit);
1853 edit->over_col = 0;
1856 /* goto end of line */
1857 static void edit_cursor_to_eol (WEdit * edit)
1859 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1860 edit->search_start = edit->curs1;
1861 edit->prev_col = edit_get_col (edit);
1862 edit->over_col = 0;
1865 /* move cursor to line 'line' */
1866 void edit_move_to_line (WEdit * e, long line)
1868 if(line < e->curs_line)
1869 edit_move_up (e, e->curs_line - line, 0);
1870 else
1871 edit_move_down (e, line - e->curs_line, 0);
1872 edit_scroll_screen_over_cursor (e);
1875 /* scroll window so that first visible line is 'line' */
1876 void edit_move_display (WEdit * e, long line)
1878 if(line < e->start_line)
1879 edit_scroll_upward (e, e->start_line - line);
1880 else
1881 edit_scroll_downward (e, line - e->start_line);
1884 /* save markers onto undo stack */
1885 void edit_push_markers (WEdit * edit)
1887 edit_push_action (edit, MARK_1 + edit->mark1);
1888 edit_push_action (edit, MARK_2 + edit->mark2);
1891 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1893 edit->mark1 = m1;
1894 edit->mark2 = m2;
1895 edit->column1 = c1;
1896 edit->column2 = c2;
1900 /* highlight marker toggle */
1901 void edit_mark_cmd (WEdit * edit, int unmark)
1903 edit_push_markers (edit);
1904 if (unmark) {
1905 edit_set_markers (edit, 0, 0, 0, 0);
1906 edit->force |= REDRAW_PAGE;
1907 } else {
1908 if (edit->mark2 >= 0) {
1909 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
1910 edit->force |= REDRAW_PAGE;
1911 } else
1912 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
1916 static unsigned long
1917 my_type_of (int c)
1919 int x, r = 0;
1920 const char *p, *q;
1921 const char option_chars_move_whole_word[] =
1922 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1924 if (!c)
1925 return 0;
1926 if (c == '!') {
1927 if (*option_chars_move_whole_word == '!')
1928 return 2;
1929 return 0x80000000UL;
1931 if (g_ascii_isupper ((gchar) c))
1932 c = 'A';
1933 else if (g_ascii_islower ((gchar) c))
1934 c = 'a';
1935 else if (g_ascii_isalpha (c))
1936 c = 'a';
1937 else if (isdigit (c))
1938 c = '0';
1939 else if (isspace (c))
1940 c = ' ';
1941 q = strchr (option_chars_move_whole_word, c);
1942 if (!q)
1943 return 0xFFFFFFFFUL;
1944 do {
1945 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1946 if (*p == '!')
1947 x <<= 1;
1948 r |= x;
1949 } while ((q = strchr (q + 1, c)));
1950 return r;
1953 static void
1954 edit_left_word_move (WEdit *edit, int s)
1956 for (;;) {
1957 int c1, c2;
1958 edit_cursor_move (edit, -1);
1959 if (!edit->curs1)
1960 break;
1961 c1 = edit_get_byte (edit, edit->curs1 - 1);
1962 c2 = edit_get_byte (edit, edit->curs1);
1963 if (!(my_type_of (c1) & my_type_of (c2)))
1964 break;
1965 if (isspace (c1) && !isspace (c2))
1966 break;
1967 if (s)
1968 if (!isspace (c1) && isspace (c2))
1969 break;
1973 static void edit_left_word_move_cmd (WEdit * edit)
1975 edit_left_word_move (edit, 0);
1976 edit->force |= REDRAW_PAGE;
1979 static void
1980 edit_right_word_move (WEdit *edit, int s)
1982 for (;;) {
1983 int c1, c2;
1984 edit_cursor_move (edit, 1);
1985 if (edit->curs1 >= edit->last_byte)
1986 break;
1987 c1 = edit_get_byte (edit, edit->curs1 - 1);
1988 c2 = edit_get_byte (edit, edit->curs1);
1989 if (!(my_type_of (c1) & my_type_of (c2)))
1990 break;
1991 if (isspace (c1) && !isspace (c2))
1992 break;
1993 if (s)
1994 if (!isspace (c1) && isspace (c2))
1995 break;
1999 static void edit_right_word_move_cmd (WEdit * edit)
2001 edit_right_word_move (edit, 0);
2002 edit->force |= REDRAW_PAGE;
2005 static void edit_right_char_move_cmd (WEdit * edit)
2007 int cw = 1;
2008 int c = 0;
2009 if ( edit->utf8 ) {
2010 c = edit_get_utf (edit, edit->curs1, &cw);
2011 if ( cw < 1 )
2012 cw = 1;
2013 } else {
2014 c = edit_get_byte (edit, edit->curs1);
2016 if (c == '\n') {
2017 edit->over_col++;
2018 } else {
2019 edit_cursor_move (edit, cw);
2023 static void edit_left_char_move_cmd (WEdit * edit)
2025 int cw = 1;
2026 if ( edit->utf8 ) {
2027 edit_get_prev_utf (edit, edit->curs1, &cw);
2028 if ( cw < 1 )
2029 cw = 1;
2031 if (edit->over_col > 0) {
2032 edit->over_col--;
2033 } else {
2034 edit_cursor_move (edit, -cw);
2039 static void edit_right_delete_word (WEdit * edit)
2041 int c1, c2;
2042 for (;;) {
2043 if (edit->curs1 >= edit->last_byte)
2044 break;
2045 c1 = edit_delete (edit, 1);
2046 c2 = edit_get_byte (edit, edit->curs1);
2047 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2048 break;
2049 if (!(my_type_of (c1) & my_type_of (c2)))
2050 break;
2054 static void edit_left_delete_word (WEdit * edit)
2056 int c1, c2;
2057 for (;;) {
2058 if (edit->curs1 <= 0)
2059 break;
2060 c1 = edit_backspace (edit, 1);
2061 c2 = edit_get_byte (edit, edit->curs1 - 1);
2062 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2063 break;
2064 if (!(my_type_of (c1) & my_type_of (c2)))
2065 break;
2070 the start column position is not recorded, and hence does not
2071 undo as it happed. But who would notice.
2073 static void
2074 edit_do_undo (WEdit * edit)
2076 long ac;
2077 long count = 0;
2079 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2081 while ((ac = pop_action (edit)) < KEY_PRESS) {
2082 switch ((int) ac) {
2083 case STACK_BOTTOM:
2084 goto done_undo;
2085 case CURS_RIGHT:
2086 edit_cursor_move (edit, 1);
2087 break;
2088 case CURS_LEFT:
2089 edit_cursor_move (edit, -1);
2090 break;
2091 case BACKSPACE:
2092 edit_backspace (edit, 1);
2093 break;
2094 case DELCHAR:
2095 edit_delete (edit, 1);
2096 break;
2097 case COLUMN_ON:
2098 column_highlighting = 1;
2099 break;
2100 case COLUMN_OFF:
2101 column_highlighting = 0;
2102 break;
2104 if (ac >= 256 && ac < 512)
2105 edit_insert_ahead (edit, ac - 256);
2106 if (ac >= 0 && ac < 256)
2107 edit_insert (edit, ac);
2109 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2110 edit->mark1 = ac - MARK_1;
2111 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2112 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2113 edit->mark2 = ac - MARK_2;
2114 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2116 if (count++)
2117 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2120 if (edit->start_display > ac - KEY_PRESS) {
2121 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2122 edit->force |= REDRAW_PAGE;
2123 } else if (edit->start_display < ac - KEY_PRESS) {
2124 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2125 edit->force |= REDRAW_PAGE;
2127 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2128 edit_update_curs_row (edit);
2130 done_undo:;
2131 edit->stack_disable = 0;
2134 static void edit_delete_to_line_end (WEdit * edit)
2136 while (edit_get_byte (edit, edit->curs1) != '\n') {
2137 if (!edit->curs2)
2138 break;
2139 edit_delete (edit, 1);
2143 static void edit_delete_to_line_begin (WEdit * edit)
2145 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2146 if (!edit->curs1)
2147 break;
2148 edit_backspace (edit, 1);
2152 void
2153 edit_delete_line (WEdit *edit)
2156 * Delete right part of the line.
2157 * Note that edit_get_byte() returns '\n' when byte position is
2158 * beyond EOF.
2160 while (edit_get_byte (edit, edit->curs1) != '\n') {
2161 (void) edit_delete (edit, 1);
2165 * Delete '\n' char.
2166 * Note that edit_delete() will not corrupt anything if called while
2167 * cursor position is EOF.
2169 (void) edit_delete (edit, 1);
2172 * Delete left part of the line.
2173 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2175 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2176 (void) edit_backspace (edit, 1);
2180 void insert_spaces_tab (WEdit * edit, int half)
2182 int i;
2183 edit_update_curs_col (edit);
2184 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2185 while (i > 0) {
2186 edit_insert (edit, ' ');
2187 i -= space_width;
2191 static int is_aligned_on_a_tab (WEdit * edit)
2193 edit_update_curs_col (edit);
2194 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2195 return 0; /* not alligned on a tab */
2196 return 1;
2199 static int right_of_four_spaces (WEdit *edit)
2201 int i, ch = 0;
2202 for (i = 1; i <= HALF_TAB_SIZE; i++)
2203 ch |= edit_get_byte (edit, edit->curs1 - i);
2204 if (ch == ' ')
2205 return is_aligned_on_a_tab (edit);
2206 return 0;
2209 static int left_of_four_spaces (WEdit *edit)
2211 int i, ch = 0;
2212 for (i = 0; i < HALF_TAB_SIZE; i++)
2213 ch |= edit_get_byte (edit, edit->curs1 + i);
2214 if (ch == ' ')
2215 return is_aligned_on_a_tab (edit);
2216 return 0;
2219 int edit_indent_width (WEdit * edit, long p)
2221 long q = p;
2222 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2223 q++;
2224 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2227 void edit_insert_indent (WEdit * edit, int indent)
2229 if (!option_fill_tabs_with_spaces) {
2230 while (indent >= TAB_SIZE) {
2231 edit_insert (edit, '\t');
2232 indent -= TAB_SIZE;
2235 while (indent-- > 0)
2236 edit_insert (edit, ' ');
2239 static void
2240 edit_auto_indent (WEdit * edit)
2242 long p;
2243 char c;
2244 p = edit->curs1;
2245 /* use the previous line as a template */
2246 p = edit_move_backward (edit, p, 1);
2247 /* copy the leading whitespace of the line */
2248 for (;;) { /* no range check - the line _is_ \n-terminated */
2249 c = edit_get_byte (edit, p++);
2250 if (c != ' ' && c != '\t')
2251 break;
2252 edit_insert (edit, c);
2256 static void edit_double_newline (WEdit * edit)
2258 edit_insert (edit, '\n');
2259 if (edit_get_byte (edit, edit->curs1) == '\n')
2260 return;
2261 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2262 return;
2263 edit->force |= REDRAW_PAGE;
2264 edit_insert (edit, '\n');
2267 static void edit_tab_cmd (WEdit * edit)
2269 int i;
2271 if (option_fake_half_tabs) {
2272 if (is_in_indent (edit)) {
2273 /*insert a half tab (usually four spaces) unless there is a
2274 half tab already behind, then delete it and insert a
2275 full tab. */
2276 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2277 for (i = 1; i <= HALF_TAB_SIZE; i++)
2278 edit_backspace (edit, 1);
2279 edit_insert (edit, '\t');
2280 } else {
2281 insert_spaces_tab (edit, 1);
2283 return;
2286 if (option_fill_tabs_with_spaces) {
2287 insert_spaces_tab (edit, 0);
2288 } else {
2289 edit_insert (edit, '\t');
2291 return;
2294 static void check_and_wrap_line (WEdit * edit)
2296 int curs, c;
2297 if (!option_typewriter_wrap)
2298 return;
2299 edit_update_curs_col (edit);
2300 if (edit->curs_col < option_word_wrap_line_length)
2301 return;
2302 curs = edit->curs1;
2303 for (;;) {
2304 curs--;
2305 c = edit_get_byte (edit, curs);
2306 if (c == '\n' || curs <= 0) {
2307 edit_insert (edit, '\n');
2308 return;
2310 if (c == ' ' || c == '\t') {
2311 int current = edit->curs1;
2312 edit_cursor_move (edit, curs - edit->curs1 + 1);
2313 edit_insert (edit, '\n');
2314 edit_cursor_move (edit, current - edit->curs1 + 1);
2315 return;
2320 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2322 void edit_push_key_press (WEdit * edit)
2324 edit_push_action (edit, KEY_PRESS + edit->start_display);
2325 if (edit->mark2 == -1)
2326 edit_push_action (edit, MARK_1 + edit->mark1);
2329 /* this find the matching bracket in either direction, and sets edit->bracket */
2330 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2332 const char * const b = "{}{[][()(", *p;
2333 int i = 1, a, inc = -1, c, d, n = 0;
2334 unsigned long j = 0;
2335 long q;
2336 edit_update_curs_row (edit);
2337 c = edit_get_byte (edit, edit->curs1);
2338 p = strchr (b, c);
2339 /* no limit */
2340 if (!furthest_bracket_search)
2341 furthest_bracket_search--;
2342 /* not on a bracket at all */
2343 if (!p)
2344 return -1;
2345 /* the matching bracket */
2346 d = p[1];
2347 /* going left or right? */
2348 if (strchr ("{[(", c))
2349 inc = 1;
2350 for (q = edit->curs1 + inc;; q += inc) {
2351 /* out of buffer? */
2352 if (q >= edit->last_byte || q < 0)
2353 break;
2354 a = edit_get_byte (edit, q);
2355 /* don't want to eat CPU */
2356 if (j++ > furthest_bracket_search)
2357 break;
2358 /* out of screen? */
2359 if (in_screen) {
2360 if (q < edit->start_display)
2361 break;
2362 /* count lines if searching downward */
2363 if (inc > 0 && a == '\n')
2364 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2365 break;
2367 /* count bracket depth */
2368 i += (a == c) - (a == d);
2369 /* return if bracket depth is zero */
2370 if (!i)
2371 return q;
2373 /* no match */
2374 return -1;
2377 static long last_bracket = -1;
2379 void edit_find_bracket (WEdit * edit)
2381 edit->bracket = edit_get_bracket (edit, 1, 10000);
2382 if (last_bracket != edit->bracket)
2383 edit->force |= REDRAW_PAGE;
2384 last_bracket = edit->bracket;
2387 static void edit_goto_matching_bracket (WEdit *edit)
2389 long q;
2390 q = edit_get_bracket (edit, 0, 0);
2391 if (q < 0)
2392 return;
2393 edit->bracket = edit->curs1;
2394 edit->force |= REDRAW_PAGE;
2395 edit_cursor_move (edit, q - edit->curs1);
2399 * This executes a command as though the user initiated it through a key
2400 * press. Callback with WIDGET_KEY as a message calls this after
2401 * translating the key press. This function can be used to pass any
2402 * command to the editor. Note that the screen wouldn't update
2403 * automatically. Either of command or char_for_insertion must be
2404 * passed as -1. Commands are executed, and char_for_insertion is
2405 * inserted at the cursor.
2407 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2409 if (command == CK_Begin_Record_Macro) {
2410 edit->macro_i = 0;
2411 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2412 return;
2414 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2415 edit->force |= REDRAW_COMPLETELY;
2416 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2417 edit->macro_i = -1;
2418 return;
2420 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2421 edit->macro[edit->macro_i].command = command;
2422 edit->macro[edit->macro_i++].ch = char_for_insertion;
2424 /* record the beginning of a set of editing actions initiated by a key press */
2425 if (command != CK_Undo && command != CK_Ext_Mode)
2426 edit_push_key_press (edit);
2428 edit_execute_cmd (edit, command, char_for_insertion);
2429 if (column_highlighting)
2430 edit->force |= REDRAW_PAGE;
2433 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2436 This executes a command at a lower level than macro recording.
2437 It also does not push a key_press onto the undo stack. This means
2438 that if it is called many times, a single undo command will undo
2439 all of them. It also does not check for the Undo command.
2441 void
2442 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2444 edit->force |= REDRAW_LINE;
2446 /* The next key press will unhighlight the found string, so update
2447 * the whole page */
2448 if (edit->found_len || column_highlighting)
2449 edit->force |= REDRAW_PAGE;
2451 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2452 column_highlighting = 0;
2453 if (!edit->highlight
2454 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2455 edit_mark_cmd (edit, 1); /* clear */
2456 edit_mark_cmd (edit, 0); /* marking on */
2458 edit->highlight = 1;
2459 } else { /* any other command */
2460 if (edit->highlight)
2461 edit_mark_cmd (edit, 0); /* clear */
2462 edit->highlight = 0;
2465 /* first check for undo */
2466 if (command == CK_Undo) {
2467 edit_do_undo (edit);
2468 edit->found_len = 0;
2469 edit->prev_col = edit_get_col (edit);
2470 edit->search_start = edit->curs1;
2471 return;
2474 /* An ordinary key press */
2475 if (char_for_insertion >= 0) {
2476 if (edit->overwrite) {
2477 if (edit_get_byte (edit, edit->curs1) != '\n')
2478 edit_delete (edit, 0);
2480 if ( edit->over_col > 0 )
2481 edit_insert_over (edit);
2482 #ifdef HAVE_CHARSET
2483 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2484 unsigned char str[6 + 1];
2485 size_t i = 0;
2486 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2487 if ( res == 0 ) {
2488 str[0] = '.';
2489 str[1] = '\0';
2490 } else {
2491 str[res] = '\0';
2493 while ( str[i] != 0 && i<=6) {
2494 char_for_insertion = str[i];
2495 edit_insert (edit, char_for_insertion);
2496 i++;
2498 } else {
2499 #endif
2500 edit_insert (edit, char_for_insertion);
2501 #ifdef HAVE_CHARSET
2503 #endif
2504 if (option_auto_para_formatting) {
2505 format_paragraph (edit, 0);
2506 edit->force |= REDRAW_PAGE;
2507 } else
2508 check_and_wrap_line (edit);
2509 edit->found_len = 0;
2510 edit->prev_col = edit_get_col (edit);
2511 edit->search_start = edit->curs1;
2512 edit_find_bracket (edit);
2513 return;
2516 switch (command) {
2517 case CK_Begin_Page:
2518 case CK_End_Page:
2519 case CK_Begin_Page_Highlight:
2520 case CK_End_Page_Highlight:
2521 case CK_Word_Left:
2522 case CK_Word_Right:
2523 case CK_Up:
2524 case CK_Down:
2525 case CK_Left:
2526 case CK_Right:
2527 if ( edit->mark2 >= 0 ) {
2528 if ( !option_persistent_selections ) {
2529 if (column_highlighting)
2530 edit_push_action (edit, COLUMN_ON);
2531 column_highlighting = 0;
2532 edit_mark_cmd (edit, 1);
2537 switch (command) {
2538 case CK_Begin_Page:
2539 case CK_End_Page:
2540 case CK_Begin_Page_Highlight:
2541 case CK_End_Page_Highlight:
2542 case CK_Word_Left:
2543 case CK_Word_Right:
2544 case CK_Up:
2545 case CK_Down:
2546 case CK_Word_Left_Highlight:
2547 case CK_Word_Right_Highlight:
2548 case CK_Up_Highlight:
2549 case CK_Down_Highlight:
2550 case CK_Up_Alt_Highlight:
2551 case CK_Down_Alt_Highlight:
2552 if (edit->mark2 == -1)
2553 break; /*marking is following the cursor: may need to highlight a whole line */
2554 case CK_Left:
2555 case CK_Right:
2556 case CK_Left_Highlight:
2557 case CK_Right_Highlight:
2558 edit->force |= REDRAW_CHAR_ONLY;
2561 /* basic cursor key commands */
2562 switch (command) {
2563 case CK_BackSpace:
2564 /* if non persistent selection and text selected */
2565 if ( !option_persistent_selections ) {
2566 if ( edit->mark1 != edit->mark2 ) {
2567 edit_block_delete_cmd (edit);
2568 break;
2571 if ( edit->over_col > 0 ) {
2572 edit->over_col--;
2573 break;
2575 if (option_backspace_through_tabs && is_in_indent (edit)) {
2576 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2577 && edit->curs1 > 0)
2578 edit_backspace (edit, 1);
2579 break;
2580 } else {
2581 if (option_fake_half_tabs) {
2582 int i;
2583 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2584 for (i = 0; i < HALF_TAB_SIZE; i++)
2585 edit_backspace (edit, 1);
2586 break;
2590 edit_backspace (edit, 0);
2591 break;
2592 case CK_Delete:
2593 /* if non persistent selection and text selected */
2594 if ( !option_persistent_selections ) {
2595 if ( edit->mark1 != edit->mark2 ) {
2596 edit_block_delete_cmd (edit);
2597 break;
2601 if ( edit->over_col > 0 )
2602 edit_insert_over (edit);
2604 if (option_fake_half_tabs) {
2605 int i;
2606 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2607 for (i = 1; i <= HALF_TAB_SIZE; i++)
2608 edit_delete (edit, 1);
2609 break;
2612 edit_delete (edit, 0);
2613 break;
2614 case CK_Delete_Word_Left:
2615 edit_left_delete_word (edit);
2616 break;
2617 case CK_Delete_Word_Right:
2618 edit_right_delete_word (edit);
2619 break;
2620 case CK_Delete_Line:
2621 edit_delete_line (edit);
2622 break;
2623 case CK_Delete_To_Line_End:
2624 edit_delete_to_line_end (edit);
2625 break;
2626 case CK_Delete_To_Line_Begin:
2627 edit_delete_to_line_begin (edit);
2628 break;
2629 case CK_Enter:
2630 if (option_auto_para_formatting) {
2631 edit_double_newline (edit);
2632 if (option_return_does_auto_indent)
2633 edit_auto_indent (edit);
2634 format_paragraph (edit, 0);
2635 } else {
2636 edit_insert (edit, '\n');
2637 if (option_return_does_auto_indent) {
2638 edit_auto_indent (edit);
2641 break;
2642 case CK_Return:
2643 edit_insert (edit, '\n');
2644 break;
2646 case CK_Page_Up_Alt_Highlight:
2647 column_highlighting = 1;
2648 case CK_Page_Up:
2649 case CK_Page_Up_Highlight:
2650 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2651 break;
2652 case CK_Page_Down_Alt_Highlight:
2653 column_highlighting = 1;
2654 case CK_Page_Down:
2655 case CK_Page_Down_Highlight:
2656 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2657 break;
2658 case CK_Left_Alt_Highlight:
2659 column_highlighting = 1;
2660 case CK_Left:
2661 case CK_Left_Highlight:
2662 if (option_fake_half_tabs) {
2663 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2664 if ( edit->over_col > 0)
2665 edit->over_col--;
2666 else
2667 edit_cursor_move (edit, -HALF_TAB_SIZE);
2668 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2669 break;
2672 edit_left_char_move_cmd (edit);
2673 break;
2674 case CK_Right_Alt_Highlight:
2675 column_highlighting = 1;
2676 case CK_Right:
2677 case CK_Right_Highlight:
2678 if (option_fake_half_tabs) {
2679 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2680 edit_cursor_move (edit, HALF_TAB_SIZE);
2681 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2682 break;
2685 edit_right_char_move_cmd (edit);
2686 break;
2687 case CK_Begin_Page:
2688 case CK_Begin_Page_Highlight:
2689 edit_begin_page (edit);
2690 break;
2691 case CK_End_Page:
2692 case CK_End_Page_Highlight:
2693 edit_end_page (edit);
2694 break;
2695 case CK_Word_Left:
2696 case CK_Word_Left_Highlight:
2697 edit_left_word_move_cmd (edit);
2698 break;
2699 case CK_Word_Right:
2700 case CK_Word_Right_Highlight:
2701 edit_right_word_move_cmd (edit);
2702 break;
2703 case CK_Up_Alt_Highlight:
2704 column_highlighting = 1;
2705 case CK_Up:
2706 case CK_Up_Highlight:
2707 edit_move_up (edit, 1, 0);
2708 break;
2709 case CK_Down_Alt_Highlight:
2710 column_highlighting = 1;
2711 case CK_Down:
2712 case CK_Down_Highlight:
2713 edit_move_down (edit, 1, 0);
2714 break;
2715 case CK_Paragraph_Up_Alt_Highlight:
2716 column_highlighting = 1;
2717 case CK_Paragraph_Up:
2718 case CK_Paragraph_Up_Highlight:
2719 edit_move_up_paragraph (edit, 0);
2720 break;
2721 case CK_Paragraph_Down_Alt_Highlight:
2722 column_highlighting = 1;
2723 case CK_Paragraph_Down:
2724 case CK_Paragraph_Down_Highlight:
2725 edit_move_down_paragraph (edit, 0);
2726 break;
2727 case CK_Scroll_Up_Alt_Highlight:
2728 column_highlighting = 1;
2729 case CK_Scroll_Up:
2730 case CK_Scroll_Up_Highlight:
2731 edit_move_up (edit, 1, 1);
2732 break;
2733 case CK_Scroll_Down_Alt_Highlight:
2734 column_highlighting = 1;
2735 case CK_Scroll_Down:
2736 case CK_Scroll_Down_Highlight:
2737 edit_move_down (edit, 1, 1);
2738 break;
2739 case CK_Home:
2740 case CK_Home_Highlight:
2741 edit_cursor_to_bol (edit);
2742 break;
2743 case CK_End:
2744 case CK_End_Highlight:
2745 edit_cursor_to_eol (edit);
2746 break;
2747 case CK_Tab:
2748 /* if text marked shift block */
2749 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2750 edit_move_block_to_right (edit);
2751 } else {
2752 edit_insert_over (edit);
2753 edit_tab_cmd (edit);
2754 if (option_auto_para_formatting) {
2755 format_paragraph (edit, 0);
2756 edit->force |= REDRAW_PAGE;
2757 } else {
2758 check_and_wrap_line (edit);
2761 break;
2763 case CK_Toggle_Insert:
2764 edit->overwrite = (edit->overwrite == 0);
2765 break;
2767 case CK_Mark:
2768 if (edit->mark2 >= 0) {
2769 if (column_highlighting)
2770 edit_push_action (edit, COLUMN_ON);
2771 column_highlighting = 0;
2773 edit_mark_cmd (edit, 0);
2774 break;
2775 case CK_Column_Mark:
2776 if (!column_highlighting)
2777 edit_push_action (edit, COLUMN_OFF);
2778 column_highlighting = 1;
2779 edit_mark_cmd (edit, 0);
2780 break;
2781 case CK_Unmark:
2782 if (column_highlighting)
2783 edit_push_action (edit, COLUMN_ON);
2784 column_highlighting = 0;
2785 edit_mark_cmd (edit, 1);
2786 break;
2788 case CK_Toggle_Line_State:
2789 option_line_state = !option_line_state;
2790 if ( option_line_state ) {
2791 option_line_state_width = LINE_STATE_WIDTH;
2792 } else {
2793 option_line_state_width = 0;
2795 edit->force |= REDRAW_PAGE;
2796 break;
2798 case CK_Toggle_Bookmark:
2799 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2800 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2801 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2802 else
2803 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2804 break;
2805 case CK_Flush_Bookmarks:
2806 book_mark_flush (edit, BOOK_MARK_COLOR);
2807 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2808 edit->force |= REDRAW_PAGE;
2809 break;
2810 case CK_Next_Bookmark:
2811 if (edit->book_mark) {
2812 struct _book_mark *p;
2813 p = (struct _book_mark *) book_mark_find (edit,
2814 edit->curs_line);
2815 if (p->next) {
2816 p = p->next;
2817 if (p->line >= edit->start_line + edit->num_widget_lines
2818 || p->line < edit->start_line)
2819 edit_move_display (edit,
2820 p->line -
2821 edit->num_widget_lines / 2);
2822 edit_move_to_line (edit, p->line);
2825 break;
2826 case CK_Prev_Bookmark:
2827 if (edit->book_mark) {
2828 struct _book_mark *p;
2829 p = (struct _book_mark *) book_mark_find (edit,
2830 edit->curs_line);
2831 while (p->line == edit->curs_line)
2832 if (p->prev)
2833 p = p->prev;
2834 if (p->line >= 0) {
2835 if (p->line >= edit->start_line + edit->num_widget_lines
2836 || p->line < edit->start_line)
2837 edit_move_display (edit,
2838 p->line -
2839 edit->num_widget_lines / 2);
2840 edit_move_to_line (edit, p->line);
2843 break;
2845 case CK_Beginning_Of_Text:
2846 case CK_Beginning_Of_Text_Highlight:
2847 edit_move_to_top (edit);
2848 break;
2849 case CK_End_Of_Text:
2850 case CK_End_Of_Text_Highlight:
2851 edit_move_to_bottom (edit);
2852 break;
2854 case CK_Copy:
2855 edit_block_copy_cmd (edit);
2856 break;
2857 case CK_Remove:
2858 edit_block_delete_cmd (edit);
2859 break;
2860 case CK_Move:
2861 edit_block_move_cmd (edit);
2862 break;
2864 case CK_XStore:
2865 edit_copy_to_X_buf_cmd (edit);
2866 break;
2867 case CK_XCut:
2868 edit_cut_to_X_buf_cmd (edit);
2869 break;
2870 case CK_XPaste:
2871 if ( edit->over_col > 0 )
2872 edit_insert_over (edit);
2873 edit_paste_from_X_buf_cmd (edit);
2874 break;
2875 case CK_Selection_History:
2876 edit_paste_from_history (edit);
2877 break;
2879 case CK_Save_As:
2880 edit_save_as_cmd (edit);
2881 break;
2882 case CK_Save:
2883 edit_save_confirm_cmd (edit);
2884 break;
2885 case CK_Load:
2886 edit_load_cmd (edit, EDIT_FILE_COMMON);
2887 break;
2888 case CK_Save_Block:
2889 edit_save_block_cmd (edit);
2890 break;
2891 case CK_Insert_File:
2892 edit_insert_file_cmd (edit);
2893 break;
2895 case CK_Load_Prev_File:
2896 edit_load_back_cmd (edit);
2897 break;
2898 case CK_Load_Next_File:
2899 edit_load_forward_cmd (edit);
2900 break;
2902 case CK_Load_Syntax_File:
2903 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
2904 break;
2905 case CK_Load_Menu_File:
2906 edit_load_cmd (edit, EDIT_FILE_MENU);
2907 break;
2909 case CK_Toggle_Syntax:
2910 if ((option_syntax_highlighting ^= 1) == 1)
2911 edit_load_syntax (edit, NULL, option_syntax_type);
2912 edit->force |= REDRAW_PAGE;
2913 break;
2915 case CK_Toggle_Tab_TWS:
2916 enable_show_tabs_tws ^= 1;
2917 edit->force |= REDRAW_PAGE;
2918 break;
2920 case CK_Find:
2921 edit_search_cmd (edit, 0);
2922 break;
2923 case CK_Find_Again:
2924 edit_search_cmd (edit, 1);
2925 break;
2926 case CK_Replace:
2927 edit_replace_cmd (edit, 0);
2928 break;
2929 case CK_Replace_Again:
2930 edit_replace_cmd (edit, 1);
2931 break;
2932 case CK_Complete_Word:
2933 /* if text marked shift block */
2934 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2935 edit_move_block_to_left (edit);
2936 } else {
2937 edit_complete_word_cmd (edit);
2939 break;
2940 case CK_Find_Definition:
2941 edit_get_match_keyword_cmd (edit);
2942 break;
2944 case CK_Exit:
2945 dlg_stop (edit->widget.parent);
2946 break;
2947 case CK_New:
2948 edit_new_cmd (edit);
2949 break;
2951 case CK_Help:
2952 edit_help_cmd (edit);
2953 break;
2955 case CK_Refresh:
2956 edit_refresh_cmd (edit);
2957 break;
2959 case CK_Date:{
2960 char s[1024];
2961 /* fool gcc to prevent a Y2K warning */
2962 char time_format[] = "_c";
2963 time_format[0] = '%';
2965 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
2966 edit_print_string (edit, s);
2967 edit->force |= REDRAW_PAGE;
2968 break;
2970 case CK_Goto:
2971 edit_goto_cmd (edit);
2972 break;
2973 case CK_Paragraph_Format:
2974 format_paragraph (edit, 1);
2975 edit->force |= REDRAW_PAGE;
2976 break;
2977 case CK_Delete_Macro:
2978 edit_delete_macro_cmd (edit);
2979 break;
2980 case CK_Match_Bracket:
2981 edit_goto_matching_bracket (edit);
2982 break;
2983 case CK_User_Menu:
2984 user_menu (edit);
2985 break;
2986 case CK_Sort:
2987 edit_sort_cmd (edit);
2988 break;
2989 case CK_ExtCmd:
2990 edit_ext_cmd (edit);
2991 break;
2992 case CK_Mail:
2993 edit_mail_dialog (edit);
2994 break;
2995 case CK_Shell:
2996 view_other_cmd ();
2997 break;
2998 case CK_Select_Codepage:
2999 edit_select_codepage_cmd (edit);
3000 break;
3001 case CK_Insert_Literal:
3002 edit_insert_literal_cmd (edit);
3003 break;
3004 case CK_Execute_Macro:
3005 edit_execute_macro_cmd (edit);
3006 break;
3007 case CK_Begin_End_Macro:
3008 edit_begin_end_macro_cmd (edit);
3009 break;
3010 case CK_Ext_Mode:
3011 edit->extmod = 1;
3012 break;
3013 default:
3014 break;
3017 /* CK_Pipe_Block */
3018 if ((command / 1000) == 1) /* a shell command */
3019 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3020 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3021 struct macro m[MAX_MACRO_LENGTH];
3022 int nm;
3023 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3024 edit_execute_macro (edit, m, nm);
3027 /* keys which must set the col position, and the search vars */
3028 switch (command) {
3029 case CK_Find:
3030 case CK_Find_Again:
3031 case CK_Replace:
3032 case CK_Replace_Again:
3033 case CK_Complete_Word:
3034 edit->prev_col = edit_get_col (edit);
3035 break;
3036 case CK_Up:
3037 case CK_Up_Highlight:
3038 case CK_Up_Alt_Highlight:
3039 case CK_Down:
3040 case CK_Down_Highlight:
3041 case CK_Down_Alt_Highlight:
3042 case CK_Page_Up:
3043 case CK_Page_Up_Highlight:
3044 case CK_Page_Up_Alt_Highlight:
3045 case CK_Page_Down:
3046 case CK_Page_Down_Highlight:
3047 case CK_Page_Down_Alt_Highlight:
3048 case CK_Beginning_Of_Text:
3049 case CK_Beginning_Of_Text_Highlight:
3050 case CK_End_Of_Text:
3051 case CK_End_Of_Text_Highlight:
3052 case CK_Paragraph_Up:
3053 case CK_Paragraph_Up_Highlight:
3054 case CK_Paragraph_Up_Alt_Highlight:
3055 case CK_Paragraph_Down:
3056 case CK_Paragraph_Down_Highlight:
3057 case CK_Paragraph_Down_Alt_Highlight:
3058 case CK_Scroll_Up:
3059 case CK_Scroll_Up_Highlight:
3060 case CK_Scroll_Up_Alt_Highlight:
3061 case CK_Scroll_Down:
3062 case CK_Scroll_Down_Highlight:
3063 case CK_Scroll_Down_Alt_Highlight:
3064 edit->search_start = edit->curs1;
3065 edit->found_len = 0;
3066 break;
3067 default:
3068 edit->found_len = 0;
3069 edit->prev_col = edit_get_col (edit);
3070 edit->search_start = edit->curs1;
3072 edit_find_bracket (edit);
3074 if (option_auto_para_formatting) {
3075 switch (command) {
3076 case CK_BackSpace:
3077 case CK_Delete:
3078 case CK_Delete_Word_Left:
3079 case CK_Delete_Word_Right:
3080 case CK_Delete_To_Line_End:
3081 case CK_Delete_To_Line_Begin:
3082 format_paragraph (edit, 0);
3083 edit->force |= REDRAW_PAGE;
3089 static void
3090 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3092 int i = 0;
3094 if (edit->macro_depth++ > 256) {
3095 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3096 edit->macro_depth--;
3097 return;
3099 edit->force |= REDRAW_PAGE;
3100 for (; i < n; i++) {
3101 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3103 edit_update_screen (edit);
3104 edit->macro_depth--;
3107 /* User edit menu, like user menu (F2) but only in editor. */
3108 static void
3109 user_menu (WEdit * edit)
3111 FILE *fd;
3112 int nomark;
3113 struct stat status;
3114 long start_mark, end_mark;
3115 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3116 int rc = 0;
3118 nomark = eval_marks (edit, &start_mark, &end_mark);
3119 if (!nomark) /* remember marked or not */
3120 edit_save_block (edit, block_file, start_mark, end_mark);
3122 /* run shell scripts from menu */
3123 user_menu_cmd (edit);
3125 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3126 /* no block messages */
3127 goto cleanup;
3130 if (!nomark) {
3131 /* i.e. we have marked block */
3132 rc = edit_block_delete_cmd (edit);
3135 if (!rc) {
3136 edit_insert_file (edit, block_file);
3139 /* truncate block file */
3140 if ((fd = fopen (block_file, "w"))) {
3141 fclose (fd);
3144 edit_refresh_cmd (edit);
3145 edit->force |= REDRAW_COMPLETELY;
3147 cleanup:
3148 g_free (block_file);
3151 void
3152 edit_stack_init (void)
3154 for (edit_stack_iterator = 0;
3155 edit_stack_iterator < MAX_HISTORY_MOVETO;
3156 edit_stack_iterator++ ) {
3157 edit_history_moveto[edit_stack_iterator].filename = NULL;
3158 edit_history_moveto[edit_stack_iterator].line = -1;
3161 edit_stack_iterator = 0;
3164 void
3165 edit_stack_free (void)
3167 for (edit_stack_iterator = 0;
3168 edit_stack_iterator < MAX_HISTORY_MOVETO;
3169 edit_stack_iterator++)
3170 g_free (edit_history_moveto[edit_stack_iterator].filename);