cleanup: build warnings: 'char *' -> 'const char *'
[midnight-commander.git] / edit / edit.c
blobf5cb6287413ae7cf29e49d75f0f8d80717bef891
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 #include <config.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <stdlib.h>
35 #include "../src/global.h"
37 #include "edit.h"
38 #include "editlock.h"
39 #include "edit-widget.h"
40 #include "editcmddef.h"
41 #include "usermap.h"
43 #include "../src/cmd.h" /* view_other_cmd() */
44 #include "../src/user.h" /* user_menu_cmd() */
45 #include "../src/wtools.h" /* query_dialog() */
46 #include "../src/timefmt.h" /* time formatting */
47 #include "../src/strutil.h" /* utf string functions */
48 #include "../src/charsets.h" /* get_codepage_id */
49 #include "../src/main.h" /* source_codepage */
52 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
53 or EDIT_KEY_EMULATION_EMACS
55 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
57 int option_word_wrap_line_length = 72;
58 int option_typewriter_wrap = 0;
59 int option_auto_para_formatting = 0;
60 int option_tab_spacing = 8;
61 int option_fill_tabs_with_spaces = 0;
62 int option_return_does_auto_indent = 1;
63 int option_backspace_through_tabs = 0;
64 int option_fake_half_tabs = 1;
65 int option_save_mode = EDIT_QUICK_SAVE;
66 int option_save_position = 1;
67 int option_max_undo = 32768;
68 int option_persistent_selections = 1;
70 int option_edit_right_extreme = 0;
71 int option_edit_left_extreme = 0;
72 int option_edit_top_extreme = 0;
73 int option_edit_bottom_extreme = 0;
75 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
76 char *option_backup_ext = NULL;
78 /*-
80 * here's a quick sketch of the layout: (don't run this through indent.)
82 * (b1 is buffers1 and b2 is buffers2)
84 * |
85 * \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
86 * ______________________________________|______________________________________
87 * |
88 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
89 * |-> |-> |-> |-> |-> |-> |
90 * |
91 * _<------------------------->|<----------------->_
92 * WEdit->curs2 | WEdit->curs1
93 * ^ | ^
94 * | ^|^ |
95 * cursor ||| cursor
96 * |||
97 * file end|||file beginning
98 * |
99 * |
102 * This_is_some_file
103 * fin.
107 static void user_menu (WEdit *edit);
109 int edit_get_byte (WEdit * edit, long byte_index)
111 unsigned long p;
112 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
113 return '\n';
115 if (byte_index >= edit->curs1) {
116 p = edit->curs1 + edit->curs2 - byte_index - 1;
117 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
118 } else {
119 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
123 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
125 unsigned long p;
127 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
128 return NULL;
130 if (byte_index >= edit->curs1) {
131 p = edit->curs1 + edit->curs2 - byte_index - 1;
132 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
133 } else {
134 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
138 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
140 unsigned long p;
142 if (byte_index >= (edit->curs1 + edit->curs2) ) {
143 byte_index -= 1;
146 if ( byte_index < 0 ) {
147 return NULL;
150 if (byte_index >= edit->curs1) {
151 p = edit->curs1 + edit->curs2 - 1;
152 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
153 } else {
154 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
158 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
160 gchar *str = NULL;
161 int res = -1;
162 gunichar ch;
163 gchar *next_ch = NULL;
164 int width = 0;
166 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
167 *char_width = 0;
168 return '\n';
172 str = edit_get_byte_ptr (edit, byte_index);
173 res = g_utf8_get_char_validated (str, -1);
175 if ( res < 0 ) {
176 ch = *str;
177 width = 0;
178 } else {
179 ch = res;
180 /* Calculate UTF-8 char width */
181 next_ch = g_utf8_next_char(str);
182 if ( next_ch ) {
183 if ( next_ch != str ) {
184 width = next_ch - str;
185 } else {
186 width = 0;
188 } else {
189 ch = 0;
190 width = 0;
193 *char_width = width;
194 return ch;
197 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
199 gchar *str, *buf = NULL;
200 int res = -1;
201 gunichar ch;
202 gchar *next_ch = NULL;
203 int width = 0;
205 if ( byte_index > 0 ) {
206 byte_index--;
209 ch = edit_get_utf (edit, byte_index, &width);
210 if ( width == 1 ) {
211 *char_width = width;
212 return ch;
215 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
216 *char_width = 0;
217 return 0;
220 str = edit_get_byte_ptr (edit, byte_index);
221 buf = edit_get_buf_ptr (edit, byte_index);
222 /* get prev utf8 char */
223 if ( str != buf )
224 str = g_utf8_find_prev_char (buf, str);
226 res = g_utf8_get_char_validated (str, -1);
227 if ( res < 0 ) {
228 ch = *str;
229 width = 0;
230 } else {
231 ch = res;
232 /* Calculate UTF-8 char width */
233 next_ch = g_utf8_next_char(str);
234 if ( next_ch ) {
235 if ( next_ch != str ) {
236 width = next_ch - str;
237 } else {
238 width = 0;
240 } else {
241 ch = 0;
242 width = 0;
245 *char_width = width;
246 return ch;
250 * Initialize the buffers for an empty files.
252 static void
253 edit_init_buffers (WEdit *edit)
255 int j;
257 for (j = 0; j <= MAXBUFF; j++) {
258 edit->buffers1[j] = NULL;
259 edit->buffers2[j] = NULL;
262 edit->curs1 = 0;
263 edit->curs2 = 0;
264 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
268 * Load file OR text into buffers. Set cursor to the beginning of file.
269 * Return 1 on error.
271 static int
272 edit_load_file_fast (WEdit *edit, const char *filename)
274 long buf, buf2;
275 int file = -1;
277 edit->curs2 = edit->last_byte;
278 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
279 edit->utf8 = 0;
280 #ifdef HAVE_CHARSET
281 if ( get_codepage_id( source_codepage ) )
282 edit->utf8 = str_isutf8 (get_codepage_id( source_codepage ));
283 #endif
284 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
285 GString *errmsg = g_string_new(NULL);
286 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
287 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
288 g_string_free (errmsg, TRUE);
289 return 1;
292 if (!edit->buffers2[buf2])
293 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
295 mc_read (file,
296 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
297 (edit->curs2 & M_EDIT_BUF_SIZE),
298 edit->curs2 & M_EDIT_BUF_SIZE);
300 for (buf = buf2 - 1; buf >= 0; buf--) {
301 /* edit->buffers2[0] is already allocated */
302 if (!edit->buffers2[buf])
303 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
304 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
307 mc_close (file);
308 return 0;
311 /* detecting an error on save is easy: just check if every byte has been written. */
312 /* detecting an error on read, is not so easy 'cos there is not way to tell
313 whether you read everything or not. */
314 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
315 static const struct edit_filters {
316 const char *read, *write, *extension;
317 } all_filters[] = {
318 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
319 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
320 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
321 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
324 /* Return index of the filter or -1 is there is no appropriate filter */
325 static int edit_find_filter (const char *filename)
327 size_t i, l, e;
328 if (!filename)
329 return -1;
330 l = strlen (filename);
331 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
332 e = strlen (all_filters[i].extension);
333 if (l > e)
334 if (!strcmp (all_filters[i].extension, filename + l - e))
335 return i;
337 return -1;
340 static char *
341 edit_get_filter (const char *filename)
343 int i, l;
344 char *p, *quoted_name;
345 i = edit_find_filter (filename);
346 if (i < 0)
347 return 0;
348 quoted_name = name_quote (filename, 0);
349 l = str_term_width1 (quoted_name);
350 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
351 sprintf (p, all_filters[i].read, quoted_name);
352 g_free (quoted_name);
353 return p;
356 char *
357 edit_get_write_filter (const char *write_name, const char *filename)
359 int i, l;
360 char *p, *writename;
361 i = edit_find_filter (filename);
362 if (i < 0)
363 return 0;
364 writename = name_quote (write_name, 0);
365 l = str_term_width1 (writename);
366 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
367 sprintf (p, all_filters[i].write, writename);
368 g_free (writename);
369 return p;
372 static long
373 edit_insert_stream (WEdit * edit, FILE * f)
375 int c;
376 long i = 0;
377 while ((c = fgetc (f)) >= 0) {
378 edit_insert (edit, c);
379 i++;
381 return i;
384 long edit_write_stream (WEdit * edit, FILE * f)
386 long i;
387 for (i = 0; i < edit->last_byte; i++)
388 if (fputc (edit_get_byte (edit, i), f) < 0)
389 break;
390 return i;
393 #define TEMP_BUF_LEN 1024
395 /* inserts a file at the cursor, returns 1 on success */
397 edit_insert_file (WEdit *edit, const char *filename)
399 char *p;
400 if ((p = edit_get_filter (filename))) {
401 FILE *f;
402 long current = edit->curs1;
403 f = (FILE *) popen (p, "r");
404 if (f) {
405 edit_insert_stream (edit, f);
406 edit_cursor_move (edit, current - edit->curs1);
407 if (pclose (f) > 0) {
408 GString *errmsg = g_string_new (NULL);
409 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
410 edit_error_dialog (_("Error"), errmsg->str);
411 g_string_free (errmsg, TRUE);
412 g_free (p);
413 return 0;
415 } else {
416 GString *errmsg = g_string_new (NULL);
417 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
418 edit_error_dialog (_("Error"), errmsg->str);
419 g_string_free (errmsg, TRUE);
420 g_free (p);
421 return 0;
423 g_free (p);
424 } else {
425 int i, file, blocklen;
426 long current = edit->curs1;
427 unsigned char *buf;
428 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
429 return 0;
430 buf = g_malloc (TEMP_BUF_LEN);
431 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
432 for (i = 0; i < blocklen; i++)
433 edit_insert (edit, buf[i]);
435 edit_cursor_move (edit, current - edit->curs1);
436 g_free (buf);
437 mc_close (file);
438 if (blocklen)
439 return 0;
441 return 1;
444 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
445 static int
446 check_file_access (WEdit *edit, const char *filename, struct stat *st)
448 int file;
449 GString *errmsg = (GString *) 0;
451 /* Try opening an existing file */
452 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
454 if (file < 0) {
456 * Try creating the file. O_EXCL prevents following broken links
457 * and opening existing files.
459 file =
460 mc_open (filename,
461 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
462 0666);
463 if (file < 0) {
464 g_string_sprintf (errmsg = g_string_new (NULL),
465 _(" Cannot open %s for reading "), filename);
466 goto cleanup;
467 } else {
468 /* New file, delete it if it's not modified or saved */
469 edit->delete_file = 1;
473 /* Check what we have opened */
474 if (mc_fstat (file, st) < 0) {
475 g_string_sprintf (errmsg = g_string_new (NULL),
476 _(" Cannot get size/permissions for %s "), filename);
477 goto cleanup;
480 /* We want to open regular files only */
481 if (!S_ISREG (st->st_mode)) {
482 g_string_sprintf (errmsg = g_string_new (NULL),
483 _(" %s is not a regular file "), filename);
484 goto cleanup;
488 * Don't delete non-empty files.
489 * O_EXCL should prevent it, but let's be on the safe side.
491 if (st->st_size > 0) {
492 edit->delete_file = 0;
495 if (st->st_size >= SIZE_LIMIT) {
496 g_string_sprintf (errmsg = g_string_new (NULL),
497 _(" File %s is too large "), filename);
498 goto cleanup;
501 cleanup:
502 (void) mc_close (file);
503 if (errmsg) {
504 edit_error_dialog (_("Error"), errmsg->str);
505 g_string_free (errmsg, TRUE);
506 return 1;
508 return 0;
512 * Open the file and load it into the buffers, either directly or using
513 * a filter. Return 0 on success, 1 on error.
515 * Fast loading (edit_load_file_fast) is used when the file size is
516 * known. In this case the data is read into the buffers by blocks.
517 * If the file size is not known, the data is loaded byte by byte in
518 * edit_insert_file.
520 static int
521 edit_load_file (WEdit *edit)
523 int fast_load = 1;
525 /* Cannot do fast load if a filter is used */
526 if (edit_find_filter (edit->filename) >= 0)
527 fast_load = 0;
530 * VFS may report file size incorrectly, and slow load is not a big
531 * deal considering overhead in VFS.
533 if (!vfs_file_is_local (edit->filename))
534 fast_load = 0;
537 * FIXME: line end translation should disable fast loading as well
538 * Consider doing fseek() to the end and ftell() for the real size.
541 if (*edit->filename) {
542 /* If we are dealing with a real file, check that it exists */
543 if (check_file_access (edit, edit->filename, &edit->stat1))
544 return 1;
545 } else {
546 /* nothing to load */
547 fast_load = 0;
550 edit_init_buffers (edit);
552 if (fast_load) {
553 edit->last_byte = edit->stat1.st_size;
554 edit_load_file_fast (edit, edit->filename);
555 /* If fast load was used, the number of lines wasn't calculated */
556 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
557 } else {
558 edit->last_byte = 0;
559 if (*edit->filename) {
560 edit->stack_disable = 1;
561 if (!edit_insert_file (edit, edit->filename)) {
562 edit_clean (edit);
563 return 1;
565 edit->stack_disable = 0;
568 return 0;
571 /* Restore saved cursor position in the file */
572 static void
573 edit_load_position (WEdit *edit)
575 char *filename;
576 long line, column;
578 if (!edit->filename || !*edit->filename)
579 return;
581 filename = vfs_canon (edit->filename);
582 load_file_position (filename, &line, &column);
583 g_free (filename);
585 edit_move_to_line (edit, line - 1);
586 edit->prev_col = column;
587 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
588 edit_move_display (edit, line - (edit->num_widget_lines / 2));
591 /* Save cursor position in the file */
592 static void
593 edit_save_position (WEdit *edit)
595 char *filename;
597 if (!edit->filename || !*edit->filename)
598 return;
600 filename = vfs_canon (edit->filename);
601 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
602 g_free (filename);
605 /* Clean the WEdit stricture except the widget part */
606 static inline void
607 edit_purge_widget (WEdit *edit)
609 int len = sizeof (WEdit) - sizeof (Widget);
610 char *start = (char *) edit + sizeof (Widget);
611 memset (start, 0, len);
612 edit->macro_i = -1; /* not recording a macro */
615 #define space_width 1
618 * Fill in the edit structure. Return NULL on failure. Pass edit as
619 * NULL to allocate a new structure.
621 * If line is 0, try to restore saved position. Otherwise put the
622 * cursor on that line and show it in the middle of the screen.
624 WEdit *
625 edit_init (WEdit *edit, int lines, int columns, const char *filename,
626 long line)
628 int to_free = 0;
629 option_auto_syntax = 1; /* Resetting to auto on every invokation */
631 if (!edit) {
632 #ifdef ENABLE_NLS
634 * Expand option_whole_chars_search by national letters using
635 * current locale
638 static char option_whole_chars_search_buf[256];
640 if (option_whole_chars_search_buf != option_whole_chars_search) {
641 size_t i;
642 size_t len = str_term_width1 (option_whole_chars_search);
644 strcpy (option_whole_chars_search_buf,
645 option_whole_chars_search);
647 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
648 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
649 option_whole_chars_search_buf[len++] = i;
653 option_whole_chars_search_buf[len] = 0;
654 option_whole_chars_search = option_whole_chars_search_buf;
656 #endif /* ENABLE_NLS */
657 edit = g_malloc0 (sizeof (WEdit));
658 to_free = 1;
660 edit_purge_widget (edit);
661 edit->num_widget_lines = lines;
662 edit->num_widget_columns = columns;
663 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
664 edit->stat1.st_uid = getuid ();
665 edit->stat1.st_gid = getgid ();
666 edit->stat1.st_mtime = 0;
667 edit->bracket = -1;
668 edit->force |= REDRAW_PAGE;
669 edit_set_filename (edit, filename);
670 edit->stack_size = START_STACK_SIZE;
671 edit->stack_size_mask = START_STACK_SIZE - 1;
672 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
673 if (edit_load_file (edit)) {
674 /* edit_load_file already gives an error message */
675 if (to_free)
676 g_free (edit);
677 return 0;
679 edit->loading_done = 1;
680 edit->modified = 0;
681 edit->locked = 0;
682 edit_load_syntax (edit, 0, 0);
684 int color;
685 edit_get_syntax_color (edit, -1, &color);
688 /* load saved cursor position */
689 if ((line == 0) && option_save_position) {
690 edit_load_position (edit);
691 } else {
692 if (line <= 0)
693 line = 1;
694 edit_move_display (edit, line - 1);
695 edit_move_to_line (edit, line - 1);
698 edit_load_user_map(edit);
700 return edit;
703 /* Clear the edit struct, freeing everything in it. Return 1 on success */
705 edit_clean (WEdit *edit)
707 int j = 0;
709 if (!edit)
710 return 0;
712 /* a stale lock, remove it */
713 if (edit->locked)
714 edit->locked = edit_unlock_file (edit->filename);
716 /* save cursor position */
717 if (option_save_position)
718 edit_save_position (edit);
720 /* File specified on the mcedit command line and never saved */
721 if (edit->delete_file)
722 unlink (edit->filename);
724 edit_free_syntax_rules (edit);
725 book_mark_flush (edit, -1);
726 for (; j <= MAXBUFF; j++) {
727 g_free (edit->buffers1[j]);
728 g_free (edit->buffers2[j]);
731 g_free (edit->undo_stack);
732 g_free (edit->filename);
733 g_free (edit->dir);
735 edit_purge_widget (edit);
737 return 1;
741 /* returns 1 on success */
742 int edit_renew (WEdit * edit)
744 int lines = edit->num_widget_lines;
745 int columns = edit->num_widget_columns;
746 int retval = 1;
748 edit_clean (edit);
749 if (!edit_init (edit, lines, columns, "", 0))
750 retval = 0;
751 return retval;
755 * Load a new file into the editor. If it fails, preserve the old file.
756 * To do it, allocate a new widget, initialize it and, if the new file
757 * was loaded, copy the data to the old widget.
758 * Return 1 on success, 0 on failure.
761 edit_reload (WEdit *edit, const char *filename)
763 WEdit *e;
764 int lines = edit->num_widget_lines;
765 int columns = edit->num_widget_columns;
767 e = g_malloc0 (sizeof (WEdit));
768 e->widget = edit->widget;
769 if (!edit_init (e, lines, columns, filename, 0)) {
770 g_free (e);
771 return 0;
773 edit_clean (edit);
774 memcpy (edit, e, sizeof (WEdit));
775 g_free (e);
776 return 1;
780 * Load a new file into the editor and set line. If it fails, preserve the old file.
781 * To do it, allocate a new widget, initialize it and, if the new file
782 * was loaded, copy the data to the old widget.
783 * Return 1 on success, 0 on failure.
786 edit_reload_line (WEdit *edit, const char *filename, long line)
788 WEdit *e;
789 int lines = edit->num_widget_lines;
790 int columns = edit->num_widget_columns;
792 e = g_malloc0 (sizeof (WEdit));
793 e->widget = edit->widget;
794 if (!edit_init (e, lines, columns, filename, line)) {
795 g_free (e);
796 return 0;
798 edit_clean (edit);
799 memcpy (edit, e, sizeof (WEdit));
800 g_free (e);
801 return 1;
806 Recording stack for undo:
807 The following is an implementation of a compressed stack. Identical
808 pushes are recorded by a negative prefix indicating the number of times the
809 same char was pushed. This saves space for repeated curs-left or curs-right
810 delete etc.
814 pushed: stored:
818 b -3
820 c --> -4
826 If the stack long int is 0-255 it represents a normal insert (from a backspace),
827 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
828 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
829 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
830 position.
832 The only way the cursor moves or the buffer is changed is through the routines:
833 insert, backspace, insert_ahead, delete, and cursor_move.
834 These record the reverse undo movements onto the stack each time they are
835 called.
837 Each key press results in a set of actions (insert; delete ...). So each time
838 a key is pressed the current position of start_display is pushed as
839 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
840 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
841 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
845 void edit_push_action (WEdit * edit, long c,...)
847 unsigned long sp = edit->stack_pointer;
848 unsigned long spm1;
849 long *t;
851 /* first enlarge the stack if necessary */
852 if (sp > edit->stack_size - 10) { /* say */
853 if (option_max_undo < 256)
854 option_max_undo = 256;
855 if (edit->stack_size < (unsigned long) option_max_undo) {
856 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
857 if (t) {
858 edit->undo_stack = t;
859 edit->stack_size <<= 1;
860 edit->stack_size_mask = edit->stack_size - 1;
864 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
865 if (edit->stack_disable)
866 return;
868 #ifdef FAST_MOVE_CURSOR
869 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
870 va_list ap;
871 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
872 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
873 va_start (ap, c);
874 c = -(va_arg (ap, int));
875 va_end (ap);
876 } else
877 #endif /* ! FAST_MOVE_CURSOR */
878 if (edit->stack_bottom != sp
879 && spm1 != edit->stack_bottom
880 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
881 int d;
882 if (edit->undo_stack[spm1] < 0) {
883 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
884 if (d == c) {
885 if (edit->undo_stack[spm1] > -1000000000) {
886 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
887 edit->undo_stack[spm1]--;
888 return;
891 /* #define NO_STACK_CURSMOVE_ANIHILATION */
892 #ifndef NO_STACK_CURSMOVE_ANIHILATION
893 else if ((c == CURS_LEFT && d == CURS_RIGHT)
894 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
895 if (edit->undo_stack[spm1] == -2)
896 edit->stack_pointer = spm1;
897 else
898 edit->undo_stack[spm1]++;
899 return;
901 #endif
902 } else {
903 d = edit->undo_stack[spm1];
904 if (d == c) {
905 if (c >= KEY_PRESS)
906 return; /* --> no need to push multiple do-nothings */
907 edit->undo_stack[sp] = -2;
908 goto check_bottom;
910 #ifndef NO_STACK_CURSMOVE_ANIHILATION
911 else if ((c == CURS_LEFT && d == CURS_RIGHT)
912 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
913 edit->stack_pointer = spm1;
914 return;
916 #endif
919 edit->undo_stack[sp] = c;
920 check_bottom:
922 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
924 /* if the sp wraps round and catches the stack_bottom then erase
925 * the first set of actions on the stack to make space - by moving
926 * stack_bottom forward one "key press" */
927 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
928 if ((unsigned long) c == edit->stack_bottom ||
929 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
930 do {
931 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
932 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
934 /*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: */
935 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
936 edit->stack_bottom = edit->stack_pointer = 0;
940 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
941 then the file should be as it was when he loaded up. Then set edit->modified to 0.
943 static long
944 pop_action (WEdit * edit)
946 long c;
947 unsigned long sp = edit->stack_pointer;
948 if (sp == edit->stack_bottom) {
949 return STACK_BOTTOM;
951 sp = (sp - 1) & edit->stack_size_mask;
952 if ((c = edit->undo_stack[sp]) >= 0) {
953 /* edit->undo_stack[sp] = '@'; */
954 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
955 return c;
957 if (sp == edit->stack_bottom) {
958 return STACK_BOTTOM;
960 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
961 if (edit->undo_stack[sp] == -2) {
962 /* edit->undo_stack[sp] = '@'; */
963 edit->stack_pointer = sp;
964 } else
965 edit->undo_stack[sp]++;
967 return c;
970 /* is called whenever a modification is made by one of the four routines below */
971 static inline void edit_modification (WEdit * edit)
973 edit->caches_valid = 0;
974 edit->screen_modified = 1;
976 /* raise lock when file modified */
977 if (!edit->modified && !edit->delete_file)
978 edit->locked = edit_lock_file (edit->filename);
979 edit->modified = 1;
983 Basic low level single character buffer alterations and movements at the cursor.
984 Returns char passed over, inserted or removed.
987 void
988 edit_insert (WEdit *edit, int c)
990 /* check if file has grown to large */
991 if (edit->last_byte >= SIZE_LIMIT)
992 return;
994 /* first we must update the position of the display window */
995 if (edit->curs1 < edit->start_display) {
996 edit->start_display++;
997 if (c == '\n')
998 edit->start_line++;
1001 /* Mark file as modified, unless the file hasn't been fully loaded */
1002 if (edit->loading_done) {
1003 edit_modification (edit);
1006 /* now we must update some info on the file and check if a redraw is required */
1007 if (c == '\n') {
1008 if (edit->book_mark)
1009 book_mark_inc (edit, edit->curs_line);
1010 edit->curs_line++;
1011 edit->total_lines++;
1012 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1015 /* save the reverse command onto the undo stack */
1016 edit_push_action (edit, BACKSPACE);
1018 /* update markers */
1019 edit->mark1 += (edit->mark1 > edit->curs1);
1020 edit->mark2 += (edit->mark2 > edit->curs1);
1021 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1023 /* add a new buffer if we've reached the end of the last one */
1024 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1025 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1026 g_malloc (EDIT_BUF_SIZE);
1028 /* perform the insertion */
1029 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1030 curs1 & M_EDIT_BUF_SIZE]
1031 = (unsigned char) c;
1033 /* update file length */
1034 edit->last_byte++;
1036 /* update cursor position */
1037 edit->curs1++;
1041 /* same as edit_insert and move left */
1042 void edit_insert_ahead (WEdit * edit, int c)
1044 if (edit->last_byte >= SIZE_LIMIT)
1045 return;
1046 if (edit->curs1 < edit->start_display) {
1047 edit->start_display++;
1048 if (c == '\n')
1049 edit->start_line++;
1051 edit_modification (edit);
1052 if (c == '\n') {
1053 if (edit->book_mark)
1054 book_mark_inc (edit, edit->curs_line);
1055 edit->total_lines++;
1056 edit->force |= REDRAW_AFTER_CURSOR;
1058 edit_push_action (edit, DELCHAR);
1060 edit->mark1 += (edit->mark1 >= edit->curs1);
1061 edit->mark2 += (edit->mark2 >= edit->curs1);
1062 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1064 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1065 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1066 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1068 edit->last_byte++;
1069 edit->curs2++;
1073 int edit_delete (WEdit * edit, const int byte_delete)
1075 int p = 0;
1076 int cw = 1;
1078 if (!edit->curs2)
1079 return 0;
1081 edit->mark1 -= (edit->mark1 > edit->curs1);
1082 edit->mark2 -= (edit->mark2 > edit->curs1);
1083 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1085 cw = 1;
1086 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1087 if ( edit->utf8 && byte_delete == 0 ) {
1088 edit_get_utf (edit, edit->curs1, &cw);
1089 if ( cw < 1 )
1090 cw = 1;
1092 for ( int i = 1; i<= cw; i++ ) {
1093 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1095 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1096 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1097 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1099 edit->last_byte--;
1100 edit->curs2--;
1103 edit_modification (edit);
1104 if (p == '\n') {
1105 if (edit->book_mark)
1106 book_mark_dec (edit, edit->curs_line);
1107 edit->total_lines--;
1108 edit->force |= REDRAW_AFTER_CURSOR;
1110 edit_push_action (edit, p + 256);
1111 if (edit->curs1 < edit->start_display) {
1112 edit->start_display--;
1113 if (p == '\n')
1114 edit->start_line--;
1117 return p;
1121 static int
1122 edit_backspace (WEdit * edit)
1124 int p = 0;
1125 int cw = 1;
1127 if (!edit->curs1)
1128 return 0;
1130 edit->mark1 -= (edit->mark1 >= edit->curs1);
1131 edit->mark2 -= (edit->mark2 >= edit->curs1);
1132 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1134 cw = 1;
1135 if ( edit->utf8 ) {
1136 edit_get_prev_utf (edit, edit->curs1, &cw);
1137 if ( cw < 1 )
1138 cw = 1;
1140 for ( int i = 1; i<= cw; i++ ) {
1141 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1142 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1143 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1144 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1146 edit->last_byte--;
1147 edit->curs1--;
1149 edit_modification (edit);
1150 if (p == '\n') {
1151 if (edit->book_mark)
1152 book_mark_dec (edit, edit->curs_line);
1153 edit->curs_line--;
1154 edit->total_lines--;
1155 edit->force |= REDRAW_AFTER_CURSOR;
1157 edit_push_action (edit, p);
1159 if (edit->curs1 < edit->start_display) {
1160 edit->start_display--;
1161 if (p == '\n')
1162 edit->start_line--;
1165 return p;
1168 #ifdef FAST_MOVE_CURSOR
1170 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1172 unsigned long next;
1173 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1174 edit->curs_line--;
1175 next -= (unsigned long) dest;
1176 n -= next;
1177 src += next;
1178 dest += next;
1183 edit_move_backward_lots (WEdit *edit, long increment)
1185 int r, s, t;
1186 unsigned char *p;
1188 if (increment > edit->curs1)
1189 increment = edit->curs1;
1190 if (increment <= 0)
1191 return -1;
1192 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1194 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1195 if (r > increment)
1196 r = increment;
1197 s = edit->curs1 & M_EDIT_BUF_SIZE;
1199 p = 0;
1200 if (s > r) {
1201 memqcpy (edit,
1202 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1203 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1205 } else {
1206 if (s) {
1207 memqcpy (edit,
1208 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1209 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1210 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1211 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1213 memqcpy (edit,
1214 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1215 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1216 EDIT_BUF_SIZE - (r - s), r - s);
1218 increment -= r;
1219 edit->curs1 -= r;
1220 edit->curs2 += r;
1221 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1222 if (p)
1223 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1224 else
1225 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1226 g_malloc (EDIT_BUF_SIZE);
1227 } else {
1228 g_free (p);
1231 s = edit->curs1 & M_EDIT_BUF_SIZE;
1232 while (increment) {
1233 p = 0;
1234 r = EDIT_BUF_SIZE;
1235 if (r > increment)
1236 r = increment;
1237 t = s;
1238 if (r < t)
1239 t = r;
1240 memqcpy (edit,
1241 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1242 EDIT_BUF_SIZE - t,
1243 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1245 if (r >= s) {
1246 if (t) {
1247 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1248 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1250 memqcpy (edit,
1251 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1252 EDIT_BUF_SIZE - r,
1253 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1254 EDIT_BUF_SIZE - (r - s), r - s);
1256 increment -= r;
1257 edit->curs1 -= r;
1258 edit->curs2 += r;
1259 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1260 if (p)
1261 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1262 else
1263 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1264 g_malloc (EDIT_BUF_SIZE);
1265 } else {
1266 g_free (p);
1269 return edit_get_byte (edit, edit->curs1);
1272 #endif /* ! FAST_MOVE_CURSOR */
1274 /* moves the cursor right or left: increment positive or negative respectively */
1275 void edit_cursor_move (WEdit * edit, long increment)
1277 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1278 int c = 0;
1279 long curs1 = edit->curs1;
1280 long curs2 = edit->curs2;
1281 int cw = 1;
1282 int char_step = 0;
1284 /* one char move*/
1285 if ( increment == -1 || increment == 1)
1286 char_step = 1;
1288 #ifdef FAST_MOVE_CURSOR
1289 if (increment < -256) {
1290 edit->force |= REDRAW_PAGE;
1291 edit_move_backward_lots (edit, -increment);
1292 return;
1294 #endif /* ! FAST_MOVE_CURSOR */
1296 if (increment < 0) {
1297 for (; increment < 0; increment++) {
1298 if (!edit->curs1)
1299 return;
1301 edit_push_action (edit, CURS_RIGHT);
1303 cw = 1;
1304 if ( edit->utf8 && char_step ) {
1305 edit_get_prev_utf (edit, curs1, &cw);
1306 if ( cw < 1 )
1307 cw = 1;
1309 for ( int i = 1; i<= cw; i++ ) {
1310 c = edit_get_byte (edit, curs1 - 1);
1311 if (!((curs2 + 1) & M_EDIT_BUF_SIZE))
1312 edit->buffers2[(curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1313 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1314 curs2++;
1315 c = edit->buffers1[(curs1 - 1) >> S_EDIT_BUF_SIZE][(curs1 - 1) & M_EDIT_BUF_SIZE];
1316 if (!((curs1 - 1) & M_EDIT_BUF_SIZE)) {
1317 g_free (edit->buffers1[curs1 >> S_EDIT_BUF_SIZE]);
1318 edit->buffers1[curs1 >> S_EDIT_BUF_SIZE] = NULL;
1320 curs1--;
1322 edit->curs1 = curs1;
1323 edit->curs2 = curs2;
1324 if (c == '\n') {
1325 edit->curs_line--;
1326 edit->force |= REDRAW_LINE_BELOW;
1330 } else if (increment > 0) {
1331 for (; increment > 0; increment--) {
1332 if (!edit->curs2)
1333 return;
1335 edit_push_action (edit, CURS_LEFT);
1337 cw = 1;
1338 if ( edit->utf8 && char_step ) {
1339 edit_get_utf (edit, curs1, &cw);
1340 if ( cw < 1 )
1341 cw = 1;
1343 for ( int i = 1; i<= cw; i++ ) {
1344 c = edit_get_byte (edit, curs1);
1345 if (!(curs1 & M_EDIT_BUF_SIZE))
1346 edit->buffers1[curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1347 edit->buffers1[curs1 >> S_EDIT_BUF_SIZE][curs1 & M_EDIT_BUF_SIZE] = c;
1348 curs1++;
1349 c = edit->buffers2[(curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1350 if (!(curs2 & M_EDIT_BUF_SIZE)) {
1351 g_free (edit->buffers2[curs2 >> S_EDIT_BUF_SIZE]);
1352 edit->buffers2[curs2 >> S_EDIT_BUF_SIZE] = 0;
1354 curs2--;
1356 edit->curs1 = curs1;
1357 edit->curs2 = curs2;
1358 if (c == '\n') {
1359 edit->curs_line++;
1360 edit->force |= REDRAW_LINE_ABOVE;
1366 /* These functions return positions relative to lines */
1368 /* returns index of last char on line + 1 */
1369 long edit_eol (WEdit * edit, long current)
1371 if (current < edit->last_byte) {
1372 for (;; current++)
1373 if (edit_get_byte (edit, current) == '\n')
1374 break;
1375 } else
1376 return edit->last_byte;
1377 return current;
1380 /* returns index of first char on line */
1381 long edit_bol (WEdit * edit, long current)
1383 if (current > 0) {
1384 for (;; current--)
1385 if (edit_get_byte (edit, current - 1) == '\n')
1386 break;
1387 } else
1388 return 0;
1389 return current;
1393 int edit_count_lines (WEdit * edit, long current, int upto)
1395 int lines = 0;
1396 if (upto > edit->last_byte)
1397 upto = edit->last_byte;
1398 if (current < 0)
1399 current = 0;
1400 while (current < upto)
1401 if (edit_get_byte (edit, current++) == '\n')
1402 lines++;
1403 return lines;
1407 /* If lines is zero this returns the count of lines from current to upto. */
1408 /* If upto is zero returns index of lines forward current. */
1409 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1411 if (upto) {
1412 return edit_count_lines (edit, current, upto);
1413 } else {
1414 int next;
1415 if (lines < 0)
1416 lines = 0;
1417 while (lines--) {
1418 next = edit_eol (edit, current) + 1;
1419 if (next > edit->last_byte)
1420 break;
1421 else
1422 current = next;
1424 return current;
1429 /* Returns offset of 'lines' lines up from current */
1430 long edit_move_backward (WEdit * edit, long current, int lines)
1432 if (lines < 0)
1433 lines = 0;
1434 current = edit_bol (edit, current);
1435 while((lines--) && current != 0)
1436 current = edit_bol (edit, current - 1);
1437 return current;
1440 /* If cols is zero this returns the count of columns from current to upto. */
1441 /* If upto is zero returns index of cols across from current. */
1442 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1444 long p, q;
1445 int col = 0;
1446 int cw = 1;
1447 if (upto) {
1448 q = upto;
1449 cols = -10;
1450 } else
1451 q = edit->last_byte + 2;
1453 for (col = 0, p = current; p < q; p++) {
1454 int c;
1455 if (cols != -10) {
1456 if (col == cols)
1457 return p;
1458 if (col > cols)
1459 return p - 1;
1461 if ( !edit->utf8 ) {
1462 c = edit_get_byte (edit, p);
1463 } else {
1464 cw = 1;
1465 c = edit_get_byte (edit, p);
1466 edit_get_utf (edit, p, &cw);
1468 if (c == '\t')
1469 col += TAB_SIZE - col % TAB_SIZE;
1470 else if (c == '\n') {
1471 if (upto)
1472 return col;
1473 else
1474 return p;
1475 } else if (c < 32 || c == 127)
1476 col += 2; /* Caret notation for control characters */
1477 else
1478 col++;
1479 if ( cw > 1 )
1480 col -= cw-1;
1482 return col;
1485 /* returns the current column position of the cursor */
1486 int edit_get_col (WEdit * edit)
1488 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1492 /* Scrolling functions */
1494 void edit_update_curs_row (WEdit * edit)
1496 edit->curs_row = edit->curs_line - edit->start_line;
1499 void edit_update_curs_col (WEdit * edit)
1501 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1504 /*moves the display start position up by i lines */
1505 void edit_scroll_upward (WEdit * edit, unsigned long i)
1507 unsigned long lines_above = edit->start_line;
1508 if (i > lines_above)
1509 i = lines_above;
1510 if (i) {
1511 edit->start_line -= i;
1512 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1513 edit->force |= REDRAW_PAGE;
1514 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1516 edit_update_curs_row (edit);
1520 /* returns 1 if could scroll, 0 otherwise */
1521 void edit_scroll_downward (WEdit * edit, int i)
1523 int lines_below;
1524 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1525 if (lines_below > 0) {
1526 if (i > lines_below)
1527 i = lines_below;
1528 edit->start_line += i;
1529 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1530 edit->force |= REDRAW_PAGE;
1531 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1533 edit_update_curs_row (edit);
1536 void edit_scroll_right (WEdit * edit, int i)
1538 edit->force |= REDRAW_PAGE;
1539 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1540 edit->start_col -= i;
1543 void edit_scroll_left (WEdit * edit, int i)
1545 if (edit->start_col) {
1546 edit->start_col += i;
1547 if (edit->start_col > 0)
1548 edit->start_col = 0;
1549 edit->force |= REDRAW_PAGE;
1550 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1554 /* high level cursor movement commands */
1556 static int is_in_indent (WEdit *edit)
1558 long p = edit_bol (edit, edit->curs1);
1559 while (p < edit->curs1)
1560 if (!strchr (" \t", edit_get_byte (edit, p++)))
1561 return 0;
1562 return 1;
1565 static int left_of_four_spaces (WEdit *edit);
1567 void
1568 edit_move_to_prev_col (WEdit * edit, long p)
1570 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1572 if (is_in_indent (edit) && option_fake_half_tabs) {
1573 edit_update_curs_col (edit);
1574 if (space_width)
1575 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1576 int q = edit->curs_col;
1577 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1578 p = edit_bol (edit, edit->curs1);
1579 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1580 if (!left_of_four_spaces (edit))
1581 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1587 /* move i lines */
1588 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1590 unsigned long p, l = edit->curs_line;
1592 if (i > l)
1593 i = l;
1594 if (i) {
1595 if (i > 1)
1596 edit->force |= REDRAW_PAGE;
1597 if (scroll)
1598 edit_scroll_upward (edit, i);
1600 p = edit_bol (edit, edit->curs1);
1601 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1602 edit_move_to_prev_col (edit, p);
1604 edit->search_start = edit->curs1;
1605 edit->found_len = 0;
1609 static int
1610 is_blank (WEdit *edit, long offset)
1612 long s, f;
1613 int c;
1614 s = edit_bol (edit, offset);
1615 f = edit_eol (edit, offset) - 1;
1616 while (s <= f) {
1617 c = edit_get_byte (edit, s++);
1618 if (!isspace (c))
1619 return 0;
1621 return 1;
1625 /* returns the offset of line i */
1626 static long
1627 edit_find_line (WEdit *edit, int line)
1629 int i, j = 0;
1630 int m = 2000000000;
1631 if (!edit->caches_valid) {
1632 for (i = 0; i < N_LINE_CACHES; i++)
1633 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1634 /* three offsets that we *know* are line 0 at 0 and these two: */
1635 edit->line_numbers[1] = edit->curs_line;
1636 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1637 edit->line_numbers[2] = edit->total_lines;
1638 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1639 edit->caches_valid = 1;
1641 if (line >= edit->total_lines)
1642 return edit->line_offsets[2];
1643 if (line <= 0)
1644 return 0;
1645 /* find the closest known point */
1646 for (i = 0; i < N_LINE_CACHES; i++) {
1647 int n;
1648 n = abs (edit->line_numbers[i] - line);
1649 if (n < m) {
1650 m = n;
1651 j = i;
1654 if (m == 0)
1655 return edit->line_offsets[j]; /* know the offset exactly */
1656 if (m == 1 && j >= 3)
1657 i = j; /* one line different - caller might be looping, so stay in this cache */
1658 else
1659 i = 3 + (rand () % (N_LINE_CACHES - 3));
1660 if (line > edit->line_numbers[j])
1661 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1662 else
1663 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1664 edit->line_numbers[i] = line;
1665 return edit->line_offsets[i];
1668 int line_is_blank (WEdit * edit, long line)
1670 return is_blank (edit, edit_find_line (edit, line));
1673 /* moves up until a blank line is reached, or until just
1674 before a non-blank line is reached */
1675 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1677 int i;
1678 if (edit->curs_line <= 1) {
1679 i = 0;
1680 } else {
1681 if (line_is_blank (edit, edit->curs_line)) {
1682 if (line_is_blank (edit, edit->curs_line - 1)) {
1683 for (i = edit->curs_line - 1; i; i--)
1684 if (!line_is_blank (edit, i)) {
1685 i++;
1686 break;
1688 } else {
1689 for (i = edit->curs_line - 1; i; i--)
1690 if (line_is_blank (edit, i))
1691 break;
1693 } else {
1694 for (i = edit->curs_line - 1; i; i--)
1695 if (line_is_blank (edit, i))
1696 break;
1699 edit_move_up (edit, edit->curs_line - i, scroll);
1702 /* move i lines */
1703 void edit_move_down (WEdit * edit, int i, int scroll)
1705 long p, l = edit->total_lines - edit->curs_line;
1707 if (i > l)
1708 i = l;
1709 if (i) {
1710 if (i > 1)
1711 edit->force |= REDRAW_PAGE;
1712 if (scroll)
1713 edit_scroll_downward (edit, i);
1714 p = edit_bol (edit, edit->curs1);
1715 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1716 edit_move_to_prev_col (edit, p);
1718 edit->search_start = edit->curs1;
1719 edit->found_len = 0;
1723 /* moves down until a blank line is reached, or until just
1724 before a non-blank line is reached */
1725 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1727 int i;
1728 if (edit->curs_line >= edit->total_lines - 1) {
1729 i = edit->total_lines;
1730 } else {
1731 if (line_is_blank (edit, edit->curs_line)) {
1732 if (line_is_blank (edit, edit->curs_line + 1)) {
1733 for (i = edit->curs_line + 1; i; i++)
1734 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1735 i--;
1736 break;
1738 } else {
1739 for (i = edit->curs_line + 1; i; i++)
1740 if (line_is_blank (edit, i) || i >= edit->total_lines)
1741 break;
1743 } else {
1744 for (i = edit->curs_line + 1; i; i++)
1745 if (line_is_blank (edit, i) || i >= edit->total_lines)
1746 break;
1749 edit_move_down (edit, i - edit->curs_line, scroll);
1752 static void edit_begin_page (WEdit *edit)
1754 edit_update_curs_row (edit);
1755 edit_move_up (edit, edit->curs_row, 0);
1758 static void edit_end_page (WEdit *edit)
1760 edit_update_curs_row (edit);
1761 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1765 /* goto beginning of text */
1766 static void edit_move_to_top (WEdit * edit)
1768 if (edit->curs_line) {
1769 edit_cursor_move (edit, -edit->curs1);
1770 edit_move_to_prev_col (edit, 0);
1771 edit->force |= REDRAW_PAGE;
1772 edit->search_start = 0;
1773 edit_update_curs_row(edit);
1778 /* goto end of text */
1779 static void edit_move_to_bottom (WEdit * edit)
1781 if (edit->curs_line < edit->total_lines) {
1782 edit_cursor_move (edit, edit->curs2);
1783 edit->start_display = edit->last_byte;
1784 edit->start_line = edit->total_lines;
1785 edit_update_curs_row(edit);
1786 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1787 edit->force |= REDRAW_PAGE;
1791 /* goto beginning of line */
1792 static void edit_cursor_to_bol (WEdit * edit)
1794 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1795 edit->search_start = edit->curs1;
1796 edit->prev_col = edit_get_col (edit);
1799 /* goto end of line */
1800 static void edit_cursor_to_eol (WEdit * edit)
1802 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1803 edit->search_start = edit->curs1;
1804 edit->prev_col = edit_get_col (edit);
1807 /* move cursor to line 'line' */
1808 void edit_move_to_line (WEdit * e, long line)
1810 if(line < e->curs_line)
1811 edit_move_up (e, e->curs_line - line, 0);
1812 else
1813 edit_move_down (e, line - e->curs_line, 0);
1814 edit_scroll_screen_over_cursor (e);
1817 /* scroll window so that first visible line is 'line' */
1818 void edit_move_display (WEdit * e, long line)
1820 if(line < e->start_line)
1821 edit_scroll_upward (e, e->start_line - line);
1822 else
1823 edit_scroll_downward (e, line - e->start_line);
1826 /* save markers onto undo stack */
1827 void edit_push_markers (WEdit * edit)
1829 edit_push_action (edit, MARK_1 + edit->mark1);
1830 edit_push_action (edit, MARK_2 + edit->mark2);
1833 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1835 edit->mark1 = m1;
1836 edit->mark2 = m2;
1837 edit->column1 = c1;
1838 edit->column2 = c2;
1842 /* highlight marker toggle */
1843 void edit_mark_cmd (WEdit * edit, int unmark)
1845 edit_push_markers (edit);
1846 if (unmark) {
1847 edit_set_markers (edit, 0, 0, 0, 0);
1848 edit->force |= REDRAW_PAGE;
1849 } else {
1850 if (edit->mark2 >= 0) {
1851 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1852 edit->force |= REDRAW_PAGE;
1853 } else
1854 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1858 static unsigned long
1859 my_type_of (int c)
1861 int x, r = 0;
1862 const char *p, *q;
1863 const char option_chars_move_whole_word[] =
1864 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1866 if (!c)
1867 return 0;
1868 if (c == '!') {
1869 if (*option_chars_move_whole_word == '!')
1870 return 2;
1871 return 0x80000000UL;
1873 if (g_ascii_isupper ((gchar) c))
1874 c = 'A';
1875 else if (g_ascii_islower ((gchar) c))
1876 c = 'a';
1877 else if (g_ascii_isalpha (c))
1878 c = 'a';
1879 else if (isdigit (c))
1880 c = '0';
1881 else if (isspace (c))
1882 c = ' ';
1883 q = strchr (option_chars_move_whole_word, c);
1884 if (!q)
1885 return 0xFFFFFFFFUL;
1886 do {
1887 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1888 if (*p == '!')
1889 x <<= 1;
1890 r |= x;
1891 } while ((q = strchr (q + 1, c)));
1892 return r;
1895 static void
1896 edit_left_word_move (WEdit *edit, int s)
1898 for (;;) {
1899 int c1, c2;
1900 edit_cursor_move (edit, -1);
1901 if (!edit->curs1)
1902 break;
1903 c1 = edit_get_byte (edit, edit->curs1 - 1);
1904 c2 = edit_get_byte (edit, edit->curs1);
1905 if (!(my_type_of (c1) & my_type_of (c2)))
1906 break;
1907 if (isspace (c1) && !isspace (c2))
1908 break;
1909 if (s)
1910 if (!isspace (c1) && isspace (c2))
1911 break;
1915 static void edit_left_word_move_cmd (WEdit * edit)
1917 edit_left_word_move (edit, 0);
1918 edit->force |= REDRAW_PAGE;
1921 static void
1922 edit_right_word_move (WEdit *edit, int s)
1924 for (;;) {
1925 int c1, c2;
1926 edit_cursor_move (edit, 1);
1927 if (edit->curs1 >= edit->last_byte)
1928 break;
1929 c1 = edit_get_byte (edit, edit->curs1 - 1);
1930 c2 = edit_get_byte (edit, edit->curs1);
1931 if (!(my_type_of (c1) & my_type_of (c2)))
1932 break;
1933 if (isspace (c1) && !isspace (c2))
1934 break;
1935 if (s)
1936 if (!isspace (c1) && isspace (c2))
1937 break;
1941 static void edit_right_word_move_cmd (WEdit * edit)
1943 edit_right_word_move (edit, 0);
1944 edit->force |= REDRAW_PAGE;
1948 static void edit_right_delete_word (WEdit * edit)
1950 int c1, c2;
1951 for (;;) {
1952 if (edit->curs1 >= edit->last_byte)
1953 break;
1954 c1 = edit_delete (edit, 1);
1955 c2 = edit_get_byte (edit, edit->curs1);
1956 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1957 break;
1958 if (!(my_type_of (c1) & my_type_of (c2)))
1959 break;
1963 static void edit_left_delete_word (WEdit * edit)
1965 int c1, c2;
1966 for (;;) {
1967 if (edit->curs1 <= 0)
1968 break;
1969 c1 = edit_backspace (edit);
1970 c2 = edit_get_byte (edit, edit->curs1 - 1);
1971 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1972 break;
1973 if (!(my_type_of (c1) & my_type_of (c2)))
1974 break;
1979 the start column position is not recorded, and hence does not
1980 undo as it happed. But who would notice.
1982 static void
1983 edit_do_undo (WEdit * edit)
1985 long ac;
1986 long count = 0;
1988 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1990 while ((ac = pop_action (edit)) < KEY_PRESS) {
1991 switch ((int) ac) {
1992 case STACK_BOTTOM:
1993 goto done_undo;
1994 case CURS_RIGHT:
1995 edit_cursor_move (edit, 1);
1996 break;
1997 case CURS_LEFT:
1998 edit_cursor_move (edit, -1);
1999 break;
2000 case BACKSPACE:
2001 edit_backspace (edit);
2002 break;
2003 case DELCHAR:
2004 edit_delete (edit, 0);
2005 break;
2006 case COLUMN_ON:
2007 column_highlighting = 1;
2008 break;
2009 case COLUMN_OFF:
2010 column_highlighting = 0;
2011 break;
2013 if (ac >= 256 && ac < 512)
2014 edit_insert_ahead (edit, ac - 256);
2015 if (ac >= 0 && ac < 256)
2016 edit_insert (edit, ac);
2018 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2019 edit->mark1 = ac - MARK_1;
2020 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2021 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2022 edit->mark2 = ac - MARK_2;
2023 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2025 if (count++)
2026 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2029 if (edit->start_display > ac - KEY_PRESS) {
2030 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2031 edit->force |= REDRAW_PAGE;
2032 } else if (edit->start_display < ac - KEY_PRESS) {
2033 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2034 edit->force |= REDRAW_PAGE;
2036 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2037 edit_update_curs_row (edit);
2039 done_undo:;
2040 edit->stack_disable = 0;
2043 static void edit_delete_to_line_end (WEdit * edit)
2045 while (edit_get_byte (edit, edit->curs1) != '\n') {
2046 if (!edit->curs2)
2047 break;
2048 edit_delete (edit, 1);
2052 static void edit_delete_to_line_begin (WEdit * edit)
2054 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2055 if (!edit->curs1)
2056 break;
2057 edit_backspace (edit);
2061 void
2062 edit_delete_line (WEdit *edit)
2065 * Delete right part of the line.
2066 * Note that edit_get_byte() returns '\n' when byte position is
2067 * beyond EOF.
2069 while (edit_get_byte (edit, edit->curs1) != '\n') {
2070 (void) edit_delete (edit, 1);
2074 * Delete '\n' char.
2075 * Note that edit_delete() will not corrupt anything if called while
2076 * cursor position is EOF.
2078 (void) edit_delete (edit, 1);
2081 * Delete left part of the line.
2082 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2084 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2085 (void) edit_backspace (edit);
2089 static void insert_spaces_tab (WEdit * edit, int half)
2091 int i;
2092 edit_update_curs_col (edit);
2093 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2094 while (i > 0) {
2095 edit_insert (edit, ' ');
2096 i -= space_width;
2100 static int is_aligned_on_a_tab (WEdit * edit)
2102 edit_update_curs_col (edit);
2103 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2104 return 0; /* not alligned on a tab */
2105 return 1;
2108 static int right_of_four_spaces (WEdit *edit)
2110 int i, ch = 0;
2111 for (i = 1; i <= HALF_TAB_SIZE; i++)
2112 ch |= edit_get_byte (edit, edit->curs1 - i);
2113 if (ch == ' ')
2114 return is_aligned_on_a_tab (edit);
2115 return 0;
2118 static int left_of_four_spaces (WEdit *edit)
2120 int i, ch = 0;
2121 for (i = 0; i < HALF_TAB_SIZE; i++)
2122 ch |= edit_get_byte (edit, edit->curs1 + i);
2123 if (ch == ' ')
2124 return is_aligned_on_a_tab (edit);
2125 return 0;
2128 int edit_indent_width (WEdit * edit, long p)
2130 long q = p;
2131 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2132 q++;
2133 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2136 void edit_insert_indent (WEdit * edit, int indent)
2138 if (!option_fill_tabs_with_spaces) {
2139 while (indent >= TAB_SIZE) {
2140 edit_insert (edit, '\t');
2141 indent -= TAB_SIZE;
2144 while (indent-- > 0)
2145 edit_insert (edit, ' ');
2148 static void
2149 edit_auto_indent (WEdit * edit)
2151 long p;
2152 char c;
2153 p = edit->curs1;
2154 /* use the previous line as a template */
2155 p = edit_move_backward (edit, p, 1);
2156 /* copy the leading whitespace of the line */
2157 for (;;) { /* no range check - the line _is_ \n-terminated */
2158 c = edit_get_byte (edit, p++);
2159 if (c != ' ' && c != '\t')
2160 break;
2161 edit_insert (edit, c);
2165 static void edit_double_newline (WEdit * edit)
2167 edit_insert (edit, '\n');
2168 if (edit_get_byte (edit, edit->curs1) == '\n')
2169 return;
2170 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2171 return;
2172 edit->force |= REDRAW_PAGE;
2173 edit_insert (edit, '\n');
2176 static void edit_tab_cmd (WEdit * edit)
2178 int i;
2180 if (option_fake_half_tabs) {
2181 if (is_in_indent (edit)) {
2182 /*insert a half tab (usually four spaces) unless there is a
2183 half tab already behind, then delete it and insert a
2184 full tab. */
2185 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2186 for (i = 1; i <= HALF_TAB_SIZE; i++)
2187 edit_backspace (edit);
2188 edit_insert (edit, '\t');
2189 } else {
2190 insert_spaces_tab (edit, 1);
2192 return;
2195 if (option_fill_tabs_with_spaces) {
2196 insert_spaces_tab (edit, 0);
2197 } else {
2198 edit_insert (edit, '\t');
2200 return;
2203 static void check_and_wrap_line (WEdit * edit)
2205 int curs, c;
2206 if (!option_typewriter_wrap)
2207 return;
2208 edit_update_curs_col (edit);
2209 if (edit->curs_col < option_word_wrap_line_length)
2210 return;
2211 curs = edit->curs1;
2212 for (;;) {
2213 curs--;
2214 c = edit_get_byte (edit, curs);
2215 if (c == '\n' || curs <= 0) {
2216 edit_insert (edit, '\n');
2217 return;
2219 if (c == ' ' || c == '\t') {
2220 int current = edit->curs1;
2221 edit_cursor_move (edit, curs - edit->curs1 + 1);
2222 edit_insert (edit, '\n');
2223 edit_cursor_move (edit, current - edit->curs1 + 1);
2224 return;
2229 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2231 void edit_push_key_press (WEdit * edit)
2233 edit_push_action (edit, KEY_PRESS + edit->start_display);
2234 if (edit->mark2 == -1)
2235 edit_push_action (edit, MARK_1 + edit->mark1);
2238 /* this find the matching bracket in either direction, and sets edit->bracket */
2239 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2241 const char * const b = "{}{[][()(", *p;
2242 int i = 1, a, inc = -1, c, d, n = 0;
2243 unsigned long j = 0;
2244 long q;
2245 edit_update_curs_row (edit);
2246 c = edit_get_byte (edit, edit->curs1);
2247 p = strchr (b, c);
2248 /* no limit */
2249 if (!furthest_bracket_search)
2250 furthest_bracket_search--;
2251 /* not on a bracket at all */
2252 if (!p)
2253 return -1;
2254 /* the matching bracket */
2255 d = p[1];
2256 /* going left or right? */
2257 if (strchr ("{[(", c))
2258 inc = 1;
2259 for (q = edit->curs1 + inc;; q += inc) {
2260 /* out of buffer? */
2261 if (q >= edit->last_byte || q < 0)
2262 break;
2263 a = edit_get_byte (edit, q);
2264 /* don't want to eat CPU */
2265 if (j++ > furthest_bracket_search)
2266 break;
2267 /* out of screen? */
2268 if (in_screen) {
2269 if (q < edit->start_display)
2270 break;
2271 /* count lines if searching downward */
2272 if (inc > 0 && a == '\n')
2273 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2274 break;
2276 /* count bracket depth */
2277 i += (a == c) - (a == d);
2278 /* return if bracket depth is zero */
2279 if (!i)
2280 return q;
2282 /* no match */
2283 return -1;
2286 static long last_bracket = -1;
2288 void edit_find_bracket (WEdit * edit)
2290 edit->bracket = edit_get_bracket (edit, 1, 10000);
2291 if (last_bracket != edit->bracket)
2292 edit->force |= REDRAW_PAGE;
2293 last_bracket = edit->bracket;
2296 static void edit_goto_matching_bracket (WEdit *edit)
2298 long q;
2299 q = edit_get_bracket (edit, 0, 0);
2300 if (q < 0)
2301 return;
2302 edit->bracket = edit->curs1;
2303 edit->force |= REDRAW_PAGE;
2304 edit_cursor_move (edit, q - edit->curs1);
2308 * This executes a command as though the user initiated it through a key
2309 * press. Callback with WIDGET_KEY as a message calls this after
2310 * translating the key press. This function can be used to pass any
2311 * command to the editor. Note that the screen wouldn't update
2312 * automatically. Either of command or char_for_insertion must be
2313 * passed as -1. Commands are executed, and char_for_insertion is
2314 * inserted at the cursor.
2316 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2318 if (command == CK_Begin_Record_Macro) {
2319 edit->macro_i = 0;
2320 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2321 return;
2323 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2324 edit->force |= REDRAW_COMPLETELY;
2325 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2326 edit->macro_i = -1;
2327 return;
2329 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2330 edit->macro[edit->macro_i].command = command;
2331 edit->macro[edit->macro_i++].ch = char_for_insertion;
2333 /* record the beginning of a set of editing actions initiated by a key press */
2334 if (command != CK_Undo && command != CK_Ext_Mode)
2335 edit_push_key_press (edit);
2337 edit_execute_cmd (edit, command, char_for_insertion);
2338 if (column_highlighting)
2339 edit->force |= REDRAW_PAGE;
2342 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2345 This executes a command at a lower level than macro recording.
2346 It also does not push a key_press onto the undo stack. This means
2347 that if it is called many times, a single undo command will undo
2348 all of them. It also does not check for the Undo command.
2350 void
2351 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2353 edit->force |= REDRAW_LINE;
2355 /* The next key press will unhighlight the found string, so update
2356 * the whole page */
2357 if (edit->found_len || column_highlighting)
2358 edit->force |= REDRAW_PAGE;
2360 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2361 column_highlighting = 0;
2362 if (!edit->highlight
2363 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2364 edit_mark_cmd (edit, 1); /* clear */
2365 edit_mark_cmd (edit, 0); /* marking on */
2367 edit->highlight = 1;
2368 } else { /* any other command */
2369 if (edit->highlight)
2370 edit_mark_cmd (edit, 0); /* clear */
2371 edit->highlight = 0;
2374 /* first check for undo */
2375 if (command == CK_Undo) {
2376 edit_do_undo (edit);
2377 edit->found_len = 0;
2378 edit->prev_col = edit_get_col (edit);
2379 edit->search_start = edit->curs1;
2380 return;
2383 /* An ordinary key press */
2384 if (char_for_insertion >= 0) {
2385 if (edit->overwrite) {
2386 if (edit_get_byte (edit, edit->curs1) != '\n')
2387 edit_delete (edit, 1);
2389 #ifdef HAVE_CHARSET
2390 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2391 unsigned char str[6 + 1];
2392 int res = g_unichar_to_utf8 (char_for_insertion, str);
2393 if ( res == 0 ) {
2394 str[0] = '.';
2395 str[1] = '\0';
2396 } else {
2397 str[res] = '\0';
2399 int i = 0;
2400 while ( str[i] != 0 && i<=6) {
2401 char_for_insertion = str[i];
2402 edit_insert (edit, char_for_insertion);
2403 i++;
2405 } else {
2406 #endif
2407 edit_insert (edit, char_for_insertion);
2408 #ifdef HAVE_CHARSET
2410 #endif
2411 if (option_auto_para_formatting) {
2412 format_paragraph (edit, 0);
2413 edit->force |= REDRAW_PAGE;
2414 } else
2415 check_and_wrap_line (edit);
2416 edit->found_len = 0;
2417 edit->prev_col = edit_get_col (edit);
2418 edit->search_start = edit->curs1;
2419 edit_find_bracket (edit);
2420 return;
2423 switch (command) {
2424 case CK_Begin_Page:
2425 case CK_End_Page:
2426 case CK_Begin_Page_Highlight:
2427 case CK_End_Page_Highlight:
2428 case CK_Word_Left:
2429 case CK_Word_Right:
2430 case CK_Up:
2431 case CK_Down:
2432 case CK_Left:
2433 case CK_Right:
2434 if ( edit->mark2 >= 0 ) {
2435 if ( !option_persistent_selections ) {
2436 if (column_highlighting)
2437 edit_push_action (edit, COLUMN_ON);
2438 column_highlighting = 0;
2439 edit_mark_cmd (edit, 1);
2444 switch (command) {
2445 case CK_Begin_Page:
2446 case CK_End_Page:
2447 case CK_Begin_Page_Highlight:
2448 case CK_End_Page_Highlight:
2449 case CK_Word_Left:
2450 case CK_Word_Right:
2451 case CK_Up:
2452 case CK_Down:
2453 case CK_Word_Left_Highlight:
2454 case CK_Word_Right_Highlight:
2455 case CK_Up_Highlight:
2456 case CK_Down_Highlight:
2457 if (edit->mark2 == -1)
2458 break; /*marking is following the cursor: may need to highlight a whole line */
2459 case CK_Left:
2460 case CK_Right:
2461 case CK_Left_Highlight:
2462 case CK_Right_Highlight:
2463 edit->force |= REDRAW_CHAR_ONLY;
2466 /* basic cursor key commands */
2467 switch (command) {
2468 case CK_BackSpace:
2469 /* if non persistent selection and text selected */
2470 if ( !option_persistent_selections ) {
2471 if ( edit->mark1 != edit->mark2 ) {
2472 edit_block_delete_cmd (edit);
2473 break;
2476 if (option_backspace_through_tabs && is_in_indent (edit)) {
2477 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2478 && edit->curs1 > 0)
2479 edit_backspace (edit);
2480 break;
2481 } else {
2482 if (option_fake_half_tabs) {
2483 int i;
2484 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2485 for (i = 0; i < HALF_TAB_SIZE; i++)
2486 edit_backspace (edit);
2487 break;
2491 edit_backspace (edit);
2492 break;
2493 case CK_Delete:
2494 /* if non persistent selection and text selected */
2495 if ( !option_persistent_selections ) {
2496 if ( edit->mark1 != edit->mark2 ) {
2497 edit_block_delete_cmd (edit);
2498 break;
2501 if (option_fake_half_tabs) {
2502 int i;
2503 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2504 for (i = 1; i <= HALF_TAB_SIZE; i++)
2505 edit_delete (edit, 1);
2506 break;
2509 edit_delete (edit, 0);
2510 break;
2511 case CK_Delete_Word_Left:
2512 edit_left_delete_word (edit);
2513 break;
2514 case CK_Delete_Word_Right:
2515 edit_right_delete_word (edit);
2516 break;
2517 case CK_Delete_Line:
2518 edit_delete_line (edit);
2519 break;
2520 case CK_Delete_To_Line_End:
2521 edit_delete_to_line_end (edit);
2522 break;
2523 case CK_Delete_To_Line_Begin:
2524 edit_delete_to_line_begin (edit);
2525 break;
2526 case CK_Enter:
2527 if (option_auto_para_formatting) {
2528 edit_double_newline (edit);
2529 if (option_return_does_auto_indent)
2530 edit_auto_indent (edit);
2531 format_paragraph (edit, 0);
2532 } else {
2533 edit_insert (edit, '\n');
2534 if (option_return_does_auto_indent) {
2535 edit_auto_indent (edit);
2538 break;
2539 case CK_Return:
2540 edit_insert (edit, '\n');
2541 break;
2543 case CK_Page_Up:
2544 case CK_Page_Up_Highlight:
2545 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2546 break;
2547 case CK_Page_Down:
2548 case CK_Page_Down_Highlight:
2549 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2550 break;
2551 case CK_Left:
2552 case CK_Left_Highlight:
2553 if (option_fake_half_tabs) {
2554 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2555 edit_cursor_move (edit, -HALF_TAB_SIZE);
2556 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2557 break;
2560 edit_cursor_move (edit, -1);
2561 break;
2562 case CK_Right:
2563 case CK_Right_Highlight:
2564 if (option_fake_half_tabs) {
2565 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2566 edit_cursor_move (edit, HALF_TAB_SIZE);
2567 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2568 break;
2571 edit_cursor_move (edit, 1);
2572 break;
2573 case CK_Begin_Page:
2574 case CK_Begin_Page_Highlight:
2575 edit_begin_page (edit);
2576 break;
2577 case CK_End_Page:
2578 case CK_End_Page_Highlight:
2579 edit_end_page (edit);
2580 break;
2581 case CK_Word_Left:
2582 case CK_Word_Left_Highlight:
2583 edit_left_word_move_cmd (edit);
2584 break;
2585 case CK_Word_Right:
2586 case CK_Word_Right_Highlight:
2587 edit_right_word_move_cmd (edit);
2588 break;
2589 case CK_Up:
2590 case CK_Up_Highlight:
2591 edit_move_up (edit, 1, 0);
2592 break;
2593 case CK_Down:
2594 case CK_Down_Highlight:
2595 edit_move_down (edit, 1, 0);
2596 break;
2597 case CK_Paragraph_Up:
2598 case CK_Paragraph_Up_Highlight:
2599 edit_move_up_paragraph (edit, 0);
2600 break;
2601 case CK_Paragraph_Down:
2602 case CK_Paragraph_Down_Highlight:
2603 edit_move_down_paragraph (edit, 0);
2604 break;
2605 case CK_Scroll_Up:
2606 case CK_Scroll_Up_Highlight:
2607 edit_move_up (edit, 1, 1);
2608 break;
2609 case CK_Scroll_Down:
2610 case CK_Scroll_Down_Highlight:
2611 edit_move_down (edit, 1, 1);
2612 break;
2613 case CK_Home:
2614 case CK_Home_Highlight:
2615 edit_cursor_to_bol (edit);
2616 break;
2617 case CK_End:
2618 case CK_End_Highlight:
2619 edit_cursor_to_eol (edit);
2620 break;
2622 case CK_Tab:
2623 edit_tab_cmd (edit);
2624 if (option_auto_para_formatting) {
2625 format_paragraph (edit, 0);
2626 edit->force |= REDRAW_PAGE;
2627 } else
2628 check_and_wrap_line (edit);
2629 break;
2631 case CK_Toggle_Insert:
2632 edit->overwrite = (edit->overwrite == 0);
2633 break;
2635 case CK_Mark:
2636 if (edit->mark2 >= 0) {
2637 if (column_highlighting)
2638 edit_push_action (edit, COLUMN_ON);
2639 column_highlighting = 0;
2641 edit_mark_cmd (edit, 0);
2642 break;
2643 case CK_Column_Mark:
2644 if (!column_highlighting)
2645 edit_push_action (edit, COLUMN_OFF);
2646 column_highlighting = 1;
2647 edit_mark_cmd (edit, 0);
2648 break;
2649 case CK_Unmark:
2650 if (column_highlighting)
2651 edit_push_action (edit, COLUMN_ON);
2652 column_highlighting = 0;
2653 edit_mark_cmd (edit, 1);
2654 break;
2656 case CK_Toggle_Bookmark:
2657 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2658 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2659 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2660 else
2661 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2662 break;
2663 case CK_Flush_Bookmarks:
2664 book_mark_flush (edit, BOOK_MARK_COLOR);
2665 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2666 edit->force |= REDRAW_PAGE;
2667 break;
2668 case CK_Next_Bookmark:
2669 if (edit->book_mark) {
2670 struct _book_mark *p;
2671 p = (struct _book_mark *) book_mark_find (edit,
2672 edit->curs_line);
2673 if (p->next) {
2674 p = p->next;
2675 if (p->line >= edit->start_line + edit->num_widget_lines
2676 || p->line < edit->start_line)
2677 edit_move_display (edit,
2678 p->line -
2679 edit->num_widget_lines / 2);
2680 edit_move_to_line (edit, p->line);
2683 break;
2684 case CK_Prev_Bookmark:
2685 if (edit->book_mark) {
2686 struct _book_mark *p;
2687 p = (struct _book_mark *) book_mark_find (edit,
2688 edit->curs_line);
2689 while (p->line == edit->curs_line)
2690 if (p->prev)
2691 p = p->prev;
2692 if (p->line >= 0) {
2693 if (p->line >= edit->start_line + edit->num_widget_lines
2694 || p->line < edit->start_line)
2695 edit_move_display (edit,
2696 p->line -
2697 edit->num_widget_lines / 2);
2698 edit_move_to_line (edit, p->line);
2701 break;
2703 case CK_Beginning_Of_Text:
2704 case CK_Beginning_Of_Text_Highlight:
2705 edit_move_to_top (edit);
2706 break;
2707 case CK_End_Of_Text:
2708 case CK_End_Of_Text_Highlight:
2709 edit_move_to_bottom (edit);
2710 break;
2712 case CK_Copy:
2713 edit_block_copy_cmd (edit);
2714 break;
2715 case CK_Remove:
2716 edit_block_delete_cmd (edit);
2717 break;
2718 case CK_Move:
2719 edit_block_move_cmd (edit);
2720 break;
2722 case CK_XStore:
2723 edit_copy_to_X_buf_cmd (edit);
2724 break;
2725 case CK_XCut:
2726 edit_cut_to_X_buf_cmd (edit);
2727 break;
2728 case CK_XPaste:
2729 edit_paste_from_X_buf_cmd (edit);
2730 break;
2731 case CK_Selection_History:
2732 edit_paste_from_history (edit);
2733 break;
2735 case CK_Save_As:
2736 edit_save_as_cmd (edit);
2737 break;
2738 case CK_Save:
2739 edit_save_confirm_cmd (edit);
2740 break;
2741 case CK_Load:
2742 edit_load_cmd (edit);
2743 break;
2744 case CK_Save_Block:
2745 edit_save_block_cmd (edit);
2746 break;
2747 case CK_Insert_File:
2748 edit_insert_file_cmd (edit);
2749 break;
2751 case CK_Load_Prev_File:
2752 edit_load_back_cmd (edit);
2753 break;
2754 case CK_Load_Next_File:
2755 edit_load_forward_cmd (edit);
2756 break;
2758 case CK_Toggle_Syntax:
2759 if ((option_syntax_highlighting ^= 1) == 1)
2760 edit_load_syntax (edit, NULL, option_syntax_type);
2761 edit->force |= REDRAW_PAGE;
2762 break;
2764 case CK_Find:
2765 edit_search_cmd (edit, 0);
2766 break;
2767 case CK_Find_Again:
2768 edit_search_cmd (edit, 1);
2769 break;
2770 case CK_Replace:
2771 edit_replace_cmd (edit, 0);
2772 break;
2773 case CK_Replace_Again:
2774 edit_replace_cmd (edit, 1);
2775 break;
2776 case CK_Complete_Word:
2777 edit_complete_word_cmd (edit);
2778 break;
2779 case CK_Find_Definition:
2780 edit_get_match_keyword_cmd (edit);
2781 break;
2783 case CK_Exit:
2784 dlg_stop (edit->widget.parent);
2785 break;
2786 case CK_New:
2787 edit_new_cmd (edit);
2788 break;
2790 case CK_Help:
2791 edit_help_cmd (edit);
2792 break;
2794 case CK_Refresh:
2795 edit_refresh_cmd (edit);
2796 break;
2798 case CK_Date:{
2799 char s[1024];
2800 /* fool gcc to prevent a Y2K warning */
2801 char time_format[] = "_c";
2802 time_format[0] = '%';
2804 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
2805 edit_print_string (edit, s);
2806 edit->force |= REDRAW_PAGE;
2807 break;
2809 case CK_Goto:
2810 edit_goto_cmd (edit);
2811 break;
2812 case CK_Paragraph_Format:
2813 format_paragraph (edit, 1);
2814 edit->force |= REDRAW_PAGE;
2815 break;
2816 case CK_Delete_Macro:
2817 edit_delete_macro_cmd (edit);
2818 break;
2819 case CK_Match_Bracket:
2820 edit_goto_matching_bracket (edit);
2821 break;
2822 case CK_User_Menu:
2823 user_menu (edit);
2824 break;
2825 case CK_Sort:
2826 edit_sort_cmd (edit);
2827 break;
2828 case CK_ExtCmd:
2829 edit_ext_cmd (edit);
2830 break;
2831 case CK_Mail:
2832 edit_mail_dialog (edit);
2833 break;
2834 case CK_Shell:
2835 view_other_cmd ();
2836 break;
2837 case CK_Select_Codepage:
2838 edit_select_codepage_cmd (edit);
2839 break;
2840 case CK_Insert_Literal:
2841 edit_insert_literal_cmd (edit);
2842 break;
2843 case CK_Execute_Macro:
2844 edit_execute_macro_cmd (edit);
2845 break;
2846 case CK_Begin_End_Macro:
2847 edit_begin_end_macro_cmd (edit);
2848 break;
2849 case CK_Ext_Mode:
2850 edit->extmod = 1;
2851 break;
2852 default:
2853 break;
2856 /* CK_Pipe_Block */
2857 if ((command / 1000) == 1) /* a shell command */
2858 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2859 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2860 struct macro m[MAX_MACRO_LENGTH];
2861 int nm;
2862 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
2863 edit_execute_macro (edit, m, nm);
2866 /* keys which must set the col position, and the search vars */
2867 switch (command) {
2868 case CK_Find:
2869 case CK_Find_Again:
2870 case CK_Replace:
2871 case CK_Replace_Again:
2872 case CK_Complete_Word:
2873 edit->prev_col = edit_get_col (edit);
2874 break;
2875 case CK_Up:
2876 case CK_Up_Highlight:
2877 case CK_Down:
2878 case CK_Down_Highlight:
2879 case CK_Page_Up:
2880 case CK_Page_Up_Highlight:
2881 case CK_Page_Down:
2882 case CK_Page_Down_Highlight:
2883 case CK_Beginning_Of_Text:
2884 case CK_Beginning_Of_Text_Highlight:
2885 case CK_End_Of_Text:
2886 case CK_End_Of_Text_Highlight:
2887 case CK_Paragraph_Up:
2888 case CK_Paragraph_Up_Highlight:
2889 case CK_Paragraph_Down:
2890 case CK_Paragraph_Down_Highlight:
2891 case CK_Scroll_Up:
2892 case CK_Scroll_Up_Highlight:
2893 case CK_Scroll_Down:
2894 case CK_Scroll_Down_Highlight:
2895 edit->search_start = edit->curs1;
2896 edit->found_len = 0;
2897 break;
2898 default:
2899 edit->found_len = 0;
2900 edit->prev_col = edit_get_col (edit);
2901 edit->search_start = edit->curs1;
2903 edit_find_bracket (edit);
2905 if (option_auto_para_formatting) {
2906 switch (command) {
2907 case CK_BackSpace:
2908 case CK_Delete:
2909 case CK_Delete_Word_Left:
2910 case CK_Delete_Word_Right:
2911 case CK_Delete_To_Line_End:
2912 case CK_Delete_To_Line_Begin:
2913 format_paragraph (edit, 0);
2914 edit->force |= REDRAW_PAGE;
2920 static void
2921 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2923 int i = 0;
2925 if (edit->macro_depth++ > 256) {
2926 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2927 edit->macro_depth--;
2928 return;
2930 edit->force |= REDRAW_PAGE;
2931 for (; i < n; i++) {
2932 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2934 edit_update_screen (edit);
2935 edit->macro_depth--;
2938 /* User edit menu, like user menu (F2) but only in editor. */
2939 static void
2940 user_menu (WEdit * edit)
2942 FILE *fd;
2943 int nomark;
2944 struct stat status;
2945 long start_mark, end_mark;
2946 char *block_file = concat_dir_and_file (home_dir, BLOCK_FILE);
2947 int rc = 0;
2949 nomark = eval_marks (edit, &start_mark, &end_mark);
2950 if (!nomark) /* remember marked or not */
2951 edit_save_block (edit, block_file, start_mark, end_mark);
2953 /* run shell scripts from menu */
2954 user_menu_cmd (edit);
2956 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2957 /* no block messages */
2958 goto cleanup;
2961 if (!nomark) {
2962 /* i.e. we have marked block */
2963 rc = edit_block_delete_cmd (edit);
2966 if (!rc) {
2967 edit_insert_file (edit, block_file);
2970 /* truncate block file */
2971 if ((fd = fopen (block_file, "w"))) {
2972 fclose (fd);
2975 edit_refresh_cmd (edit);
2976 edit->force |= REDRAW_COMPLETELY;
2978 cleanup:
2979 g_free (block_file);