Merge branch '176_lzma'
[midnight-commander.git] / edit / edit.c
blob51810f793cbf098d32949c64a5d5806d34edbeed
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 */
50 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
51 or EDIT_KEY_EMULATION_EMACS
53 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
55 int option_word_wrap_line_length = 72;
56 int option_typewriter_wrap = 0;
57 int option_auto_para_formatting = 0;
58 int option_tab_spacing = 8;
59 int option_fill_tabs_with_spaces = 0;
60 int option_return_does_auto_indent = 1;
61 int option_backspace_through_tabs = 0;
62 int option_fake_half_tabs = 1;
63 int option_save_mode = EDIT_QUICK_SAVE;
64 int option_save_position = 1;
65 int option_max_undo = 32768;
67 int option_edit_right_extreme = 0;
68 int option_edit_left_extreme = 0;
69 int option_edit_top_extreme = 0;
70 int option_edit_bottom_extreme = 0;
72 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
73 char *option_backup_ext = NULL;
75 /*-
77 * here's a quick sketch of the layout: (don't run this through indent.)
79 * (b1 is buffers1 and b2 is buffers2)
81 * |
82 * \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
83 * ______________________________________|______________________________________
84 * |
85 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
86 * |-> |-> |-> |-> |-> |-> |
87 * |
88 * _<------------------------->|<----------------->_
89 * WEdit->curs2 | WEdit->curs1
90 * ^ | ^
91 * | ^|^ |
92 * cursor ||| cursor
93 * |||
94 * file end|||file beginning
95 * |
96 * |
98 * _
99 * This_is_some_file
100 * fin.
104 static void user_menu (WEdit *edit);
106 int edit_get_byte (WEdit * edit, long byte_index)
108 unsigned long p;
109 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
110 return '\n';
112 if (byte_index >= edit->curs1) {
113 p = edit->curs1 + edit->curs2 - byte_index - 1;
114 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
115 } else {
116 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
121 * Initialize the buffers for an empty files.
123 static void
124 edit_init_buffers (WEdit *edit)
126 int j;
128 for (j = 0; j <= MAXBUFF; j++) {
129 edit->buffers1[j] = NULL;
130 edit->buffers2[j] = NULL;
133 edit->curs1 = 0;
134 edit->curs2 = 0;
135 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
139 * Load file OR text into buffers. Set cursor to the beginning of file.
140 * Return 1 on error.
142 static int
143 edit_load_file_fast (WEdit *edit, const char *filename)
145 long buf, buf2;
146 int file = -1;
148 edit->curs2 = edit->last_byte;
149 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
151 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
152 GString *errmsg = g_string_new(NULL);
153 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
154 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
155 g_string_free (errmsg, TRUE);
156 return 1;
159 if (!edit->buffers2[buf2])
160 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
162 mc_read (file,
163 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
164 (edit->curs2 & M_EDIT_BUF_SIZE),
165 edit->curs2 & M_EDIT_BUF_SIZE);
167 for (buf = buf2 - 1; buf >= 0; buf--) {
168 /* edit->buffers2[0] is already allocated */
169 if (!edit->buffers2[buf])
170 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
171 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
174 mc_close (file);
175 return 0;
178 /* detecting an error on save is easy: just check if every byte has been written. */
179 /* detecting an error on read, is not so easy 'cos there is not way to tell
180 whether you read everything or not. */
181 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
182 static const struct edit_filters {
183 const char *read, *write, *extension;
184 } all_filters[] = {
185 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
186 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
187 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
188 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
191 /* Return index of the filter or -1 is there is no appropriate filter */
192 static int edit_find_filter (const char *filename)
194 size_t i, l, e;
195 if (!filename)
196 return -1;
197 l = strlen (filename);
198 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
199 e = strlen (all_filters[i].extension);
200 if (l > e)
201 if (!strcmp (all_filters[i].extension, filename + l - e))
202 return i;
204 return -1;
207 static char *
208 edit_get_filter (const char *filename)
210 int i, l;
211 char *p, *quoted_name;
212 i = edit_find_filter (filename);
213 if (i < 0)
214 return 0;
215 quoted_name = name_quote (filename, 0);
216 l = strlen (quoted_name);
217 p = g_malloc (strlen (all_filters[i].read) + l + 2);
218 sprintf (p, all_filters[i].read, quoted_name);
219 g_free (quoted_name);
220 return p;
223 char *
224 edit_get_write_filter (const char *write_name, const char *filename)
226 int i, l;
227 char *p, *writename;
228 i = edit_find_filter (filename);
229 if (i < 0)
230 return 0;
231 writename = name_quote (write_name, 0);
232 l = strlen (writename);
233 p = g_malloc (strlen (all_filters[i].write) + l + 2);
234 sprintf (p, all_filters[i].write, writename);
235 g_free (writename);
236 return p;
239 static long
240 edit_insert_stream (WEdit * edit, FILE * f)
242 int c;
243 long i = 0;
244 while ((c = fgetc (f)) >= 0) {
245 edit_insert (edit, c);
246 i++;
248 return i;
251 long edit_write_stream (WEdit * edit, FILE * f)
253 long i;
254 for (i = 0; i < edit->last_byte; i++)
255 if (fputc (edit_get_byte (edit, i), f) < 0)
256 break;
257 return i;
260 #define TEMP_BUF_LEN 1024
262 /* inserts a file at the cursor, returns 1 on success */
264 edit_insert_file (WEdit *edit, const char *filename)
266 char *p;
267 if ((p = edit_get_filter (filename))) {
268 FILE *f;
269 long current = edit->curs1;
270 f = (FILE *) popen (p, "r");
271 if (f) {
272 edit_insert_stream (edit, f);
273 edit_cursor_move (edit, current - edit->curs1);
274 if (pclose (f) > 0) {
275 GString *errmsg = g_string_new (NULL);
276 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
277 edit_error_dialog (_("Error"), errmsg->str);
278 g_string_free (errmsg, TRUE);
279 g_free (p);
280 return 0;
282 } else {
283 GString *errmsg = g_string_new (NULL);
284 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
285 edit_error_dialog (_("Error"), errmsg->str);
286 g_string_free (errmsg, TRUE);
287 g_free (p);
288 return 0;
290 g_free (p);
291 } else {
292 int i, file, blocklen;
293 long current = edit->curs1;
294 unsigned char *buf;
295 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
296 return 0;
297 buf = g_malloc (TEMP_BUF_LEN);
298 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
299 for (i = 0; i < blocklen; i++)
300 edit_insert (edit, buf[i]);
302 edit_cursor_move (edit, current - edit->curs1);
303 g_free (buf);
304 mc_close (file);
305 if (blocklen)
306 return 0;
308 return 1;
311 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
312 static int
313 check_file_access (WEdit *edit, const char *filename, struct stat *st)
315 int file;
316 GString *errmsg = (GString *) 0;
318 /* Try opening an existing file */
319 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
321 if (file < 0) {
323 * Try creating the file. O_EXCL prevents following broken links
324 * and opening existing files.
326 file =
327 mc_open (filename,
328 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
329 0666);
330 if (file < 0) {
331 g_string_sprintf (errmsg = g_string_new (NULL),
332 _(" Cannot open %s for reading "), filename);
333 goto cleanup;
334 } else {
335 /* New file, delete it if it's not modified or saved */
336 edit->delete_file = 1;
340 /* Check what we have opened */
341 if (mc_fstat (file, st) < 0) {
342 g_string_sprintf (errmsg = g_string_new (NULL),
343 _(" Cannot get size/permissions for %s "), filename);
344 goto cleanup;
347 /* We want to open regular files only */
348 if (!S_ISREG (st->st_mode)) {
349 g_string_sprintf (errmsg = g_string_new (NULL),
350 _(" %s is not a regular file "), filename);
351 goto cleanup;
355 * Don't delete non-empty files.
356 * O_EXCL should prevent it, but let's be on the safe side.
358 if (st->st_size > 0) {
359 edit->delete_file = 0;
362 if (st->st_size >= SIZE_LIMIT) {
363 g_string_sprintf (errmsg = g_string_new (NULL),
364 _(" File %s is too large "), filename);
365 goto cleanup;
368 cleanup:
369 (void) mc_close (file);
370 if (errmsg) {
371 edit_error_dialog (_("Error"), errmsg->str);
372 g_string_free (errmsg, TRUE);
373 return 1;
375 return 0;
379 * Open the file and load it into the buffers, either directly or using
380 * a filter. Return 0 on success, 1 on error.
382 * Fast loading (edit_load_file_fast) is used when the file size is
383 * known. In this case the data is read into the buffers by blocks.
384 * If the file size is not known, the data is loaded byte by byte in
385 * edit_insert_file.
387 static int
388 edit_load_file (WEdit *edit)
390 int fast_load = 1;
392 /* Cannot do fast load if a filter is used */
393 if (edit_find_filter (edit->filename) >= 0)
394 fast_load = 0;
397 * VFS may report file size incorrectly, and slow load is not a big
398 * deal considering overhead in VFS.
400 if (!vfs_file_is_local (edit->filename))
401 fast_load = 0;
404 * FIXME: line end translation should disable fast loading as well
405 * Consider doing fseek() to the end and ftell() for the real size.
408 if (*edit->filename) {
409 /* If we are dealing with a real file, check that it exists */
410 if (check_file_access (edit, edit->filename, &edit->stat1))
411 return 1;
412 } else {
413 /* nothing to load */
414 fast_load = 0;
417 edit_init_buffers (edit);
419 if (fast_load) {
420 edit->last_byte = edit->stat1.st_size;
421 edit_load_file_fast (edit, edit->filename);
422 /* If fast load was used, the number of lines wasn't calculated */
423 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
424 } else {
425 edit->last_byte = 0;
426 if (*edit->filename) {
427 edit->stack_disable = 1;
428 if (!edit_insert_file (edit, edit->filename)) {
429 edit_clean (edit);
430 return 1;
432 edit->stack_disable = 0;
435 return 0;
438 /* Restore saved cursor position in the file */
439 static void
440 edit_load_position (WEdit *edit)
442 char *filename;
443 long line, column;
445 if (!edit->filename || !*edit->filename)
446 return;
448 filename = vfs_canon (edit->filename);
449 load_file_position (filename, &line, &column);
450 g_free (filename);
452 edit_move_to_line (edit, line - 1);
453 edit->prev_col = column;
454 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
455 edit_move_display (edit, line - (edit->num_widget_lines / 2));
458 /* Save cursor position in the file */
459 static void
460 edit_save_position (WEdit *edit)
462 char *filename;
464 if (!edit->filename || !*edit->filename)
465 return;
467 filename = vfs_canon (edit->filename);
468 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
469 g_free (filename);
472 /* Clean the WEdit stricture except the widget part */
473 static inline void
474 edit_purge_widget (WEdit *edit)
476 int len = sizeof (WEdit) - sizeof (Widget);
477 char *start = (char *) edit + sizeof (Widget);
478 memset (start, 0, len);
479 edit->macro_i = -1; /* not recording a macro */
482 #define space_width 1
485 * Fill in the edit structure. Return NULL on failure. Pass edit as
486 * NULL to allocate a new structure.
488 * If line is 0, try to restore saved position. Otherwise put the
489 * cursor on that line and show it in the middle of the screen.
491 WEdit *
492 edit_init (WEdit *edit, int lines, int columns, const char *filename,
493 long line)
495 int to_free = 0;
496 option_auto_syntax = 1; /* Resetting to auto on every invokation */
498 if (!edit) {
499 #ifdef ENABLE_NLS
501 * Expand option_whole_chars_search by national letters using
502 * current locale
505 static char option_whole_chars_search_buf[256];
507 if (option_whole_chars_search_buf != option_whole_chars_search) {
508 size_t i;
509 size_t len = strlen (option_whole_chars_search);
511 strcpy (option_whole_chars_search_buf,
512 option_whole_chars_search);
514 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
515 if (islower (i) && !strchr (option_whole_chars_search, i)) {
516 option_whole_chars_search_buf[len++] = i;
520 option_whole_chars_search_buf[len] = 0;
521 option_whole_chars_search = option_whole_chars_search_buf;
523 #endif /* ENABLE_NLS */
524 edit = g_malloc0 (sizeof (WEdit));
525 to_free = 1;
527 edit_purge_widget (edit);
528 edit->num_widget_lines = lines;
529 edit->num_widget_columns = columns;
530 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
531 edit->stat1.st_uid = getuid ();
532 edit->stat1.st_gid = getgid ();
533 edit->stat1.st_mtime = 0;
534 edit->bracket = -1;
535 edit->force |= REDRAW_PAGE;
536 edit_set_filename (edit, filename);
537 edit->stack_size = START_STACK_SIZE;
538 edit->stack_size_mask = START_STACK_SIZE - 1;
539 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
540 if (edit_load_file (edit)) {
541 /* edit_load_file already gives an error message */
542 if (to_free)
543 g_free (edit);
544 return 0;
546 edit->loading_done = 1;
547 edit->modified = 0;
548 edit->locked = 0;
549 edit_load_syntax (edit, 0, 0);
551 int color;
552 edit_get_syntax_color (edit, -1, &color);
555 /* load saved cursor position */
556 if ((line == 0) && option_save_position) {
557 edit_load_position (edit);
558 } else {
559 if (line <= 0)
560 line = 1;
561 edit_move_display (edit, line - 1);
562 edit_move_to_line (edit, line - 1);
565 edit_load_user_map(edit);
567 return edit;
570 /* Clear the edit struct, freeing everything in it. Return 1 on success */
572 edit_clean (WEdit *edit)
574 int j = 0;
576 if (!edit)
577 return 0;
579 /* a stale lock, remove it */
580 if (edit->locked)
581 edit->locked = edit_unlock_file (edit->filename);
583 /* save cursor position */
584 if (option_save_position)
585 edit_save_position (edit);
587 /* File specified on the mcedit command line and never saved */
588 if (edit->delete_file)
589 unlink (edit->filename);
591 edit_free_syntax_rules (edit);
592 book_mark_flush (edit, -1);
593 for (; j <= MAXBUFF; j++) {
594 g_free (edit->buffers1[j]);
595 g_free (edit->buffers2[j]);
598 g_free (edit->undo_stack);
599 g_free (edit->filename);
600 g_free (edit->dir);
602 edit_purge_widget (edit);
604 /* Free temporary strings used in catstrs() */
605 freestrs ();
607 return 1;
611 /* returns 1 on success */
612 int edit_renew (WEdit * edit)
614 int lines = edit->num_widget_lines;
615 int columns = edit->num_widget_columns;
616 int retval = 1;
618 edit_clean (edit);
619 if (!edit_init (edit, lines, columns, "", 0))
620 retval = 0;
621 return retval;
625 * Load a new file into the editor. If it fails, preserve the old file.
626 * To do it, allocate a new widget, initialize it and, if the new file
627 * was loaded, copy the data to the old widget.
628 * Return 1 on success, 0 on failure.
631 edit_reload (WEdit *edit, const char *filename)
633 WEdit *e;
634 int lines = edit->num_widget_lines;
635 int columns = edit->num_widget_columns;
637 e = g_malloc0 (sizeof (WEdit));
638 e->widget = edit->widget;
639 if (!edit_init (e, lines, columns, filename, 0)) {
640 g_free (e);
641 return 0;
643 edit_clean (edit);
644 memcpy (edit, e, sizeof (WEdit));
645 g_free (e);
646 return 1;
651 Recording stack for undo:
652 The following is an implementation of a compressed stack. Identical
653 pushes are recorded by a negative prefix indicating the number of times the
654 same char was pushed. This saves space for repeated curs-left or curs-right
655 delete etc.
659 pushed: stored:
663 b -3
665 c --> -4
671 If the stack long int is 0-255 it represents a normal insert (from a backspace),
672 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
673 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
674 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
675 position.
677 The only way the cursor moves or the buffer is changed is through the routines:
678 insert, backspace, insert_ahead, delete, and cursor_move.
679 These record the reverse undo movements onto the stack each time they are
680 called.
682 Each key press results in a set of actions (insert; delete ...). So each time
683 a key is pressed the current position of start_display is pushed as
684 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
685 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
686 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
690 void edit_push_action (WEdit * edit, long c,...)
692 unsigned long sp = edit->stack_pointer;
693 unsigned long spm1;
694 long *t;
696 /* first enlarge the stack if necessary */
697 if (sp > edit->stack_size - 10) { /* say */
698 if (option_max_undo < 256)
699 option_max_undo = 256;
700 if (edit->stack_size < (unsigned long) option_max_undo) {
701 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
702 if (t) {
703 edit->undo_stack = t;
704 edit->stack_size <<= 1;
705 edit->stack_size_mask = edit->stack_size - 1;
709 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
710 if (edit->stack_disable)
711 return;
713 #ifdef FAST_MOVE_CURSOR
714 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
715 va_list ap;
716 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
717 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
718 va_start (ap, c);
719 c = -(va_arg (ap, int));
720 va_end (ap);
721 } else
722 #endif /* ! FAST_MOVE_CURSOR */
723 if (edit->stack_bottom != sp
724 && spm1 != edit->stack_bottom
725 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
726 int d;
727 if (edit->undo_stack[spm1] < 0) {
728 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
729 if (d == c) {
730 if (edit->undo_stack[spm1] > -1000000000) {
731 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
732 edit->undo_stack[spm1]--;
733 return;
736 /* #define NO_STACK_CURSMOVE_ANIHILATION */
737 #ifndef NO_STACK_CURSMOVE_ANIHILATION
738 else if ((c == CURS_LEFT && d == CURS_RIGHT)
739 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
740 if (edit->undo_stack[spm1] == -2)
741 edit->stack_pointer = spm1;
742 else
743 edit->undo_stack[spm1]++;
744 return;
746 #endif
747 } else {
748 d = edit->undo_stack[spm1];
749 if (d == c) {
750 if (c >= KEY_PRESS)
751 return; /* --> no need to push multiple do-nothings */
752 edit->undo_stack[sp] = -2;
753 goto check_bottom;
755 #ifndef NO_STACK_CURSMOVE_ANIHILATION
756 else if ((c == CURS_LEFT && d == CURS_RIGHT)
757 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
758 edit->stack_pointer = spm1;
759 return;
761 #endif
764 edit->undo_stack[sp] = c;
765 check_bottom:
767 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
769 /* if the sp wraps round and catches the stack_bottom then erase
770 * the first set of actions on the stack to make space - by moving
771 * stack_bottom forward one "key press" */
772 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
773 if ((unsigned long) c == edit->stack_bottom ||
774 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
775 do {
776 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
777 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
779 /*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: */
780 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
781 edit->stack_bottom = edit->stack_pointer = 0;
785 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
786 then the file should be as it was when he loaded up. Then set edit->modified to 0.
788 static long
789 pop_action (WEdit * edit)
791 long c;
792 unsigned long sp = edit->stack_pointer;
793 if (sp == edit->stack_bottom) {
794 return STACK_BOTTOM;
796 sp = (sp - 1) & edit->stack_size_mask;
797 if ((c = edit->undo_stack[sp]) >= 0) {
798 /* edit->undo_stack[sp] = '@'; */
799 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
800 return c;
802 if (sp == edit->stack_bottom) {
803 return STACK_BOTTOM;
805 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
806 if (edit->undo_stack[sp] == -2) {
807 /* edit->undo_stack[sp] = '@'; */
808 edit->stack_pointer = sp;
809 } else
810 edit->undo_stack[sp]++;
812 return c;
815 /* is called whenever a modification is made by one of the four routines below */
816 static inline void edit_modification (WEdit * edit)
818 edit->caches_valid = 0;
819 edit->screen_modified = 1;
821 /* raise lock when file modified */
822 if (!edit->modified && !edit->delete_file)
823 edit->locked = edit_lock_file (edit->filename);
824 edit->modified = 1;
828 Basic low level single character buffer alterations and movements at the cursor.
829 Returns char passed over, inserted or removed.
832 void
833 edit_insert (WEdit *edit, int c)
835 /* check if file has grown to large */
836 if (edit->last_byte >= SIZE_LIMIT)
837 return;
839 /* first we must update the position of the display window */
840 if (edit->curs1 < edit->start_display) {
841 edit->start_display++;
842 if (c == '\n')
843 edit->start_line++;
846 /* Mark file as modified, unless the file hasn't been fully loaded */
847 if (edit->loading_done) {
848 edit_modification (edit);
851 /* now we must update some info on the file and check if a redraw is required */
852 if (c == '\n') {
853 if (edit->book_mark)
854 book_mark_inc (edit, edit->curs_line);
855 edit->curs_line++;
856 edit->total_lines++;
857 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
860 /* save the reverse command onto the undo stack */
861 edit_push_action (edit, BACKSPACE);
863 /* update markers */
864 edit->mark1 += (edit->mark1 > edit->curs1);
865 edit->mark2 += (edit->mark2 > edit->curs1);
866 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
868 /* add a new buffer if we've reached the end of the last one */
869 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
870 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
871 g_malloc (EDIT_BUF_SIZE);
873 /* perform the insertion */
874 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
875 curs1 & M_EDIT_BUF_SIZE]
876 = (unsigned char) c;
878 /* update file length */
879 edit->last_byte++;
881 /* update cursor position */
882 edit->curs1++;
886 /* same as edit_insert and move left */
887 void edit_insert_ahead (WEdit * edit, int c)
889 if (edit->last_byte >= SIZE_LIMIT)
890 return;
891 if (edit->curs1 < edit->start_display) {
892 edit->start_display++;
893 if (c == '\n')
894 edit->start_line++;
896 edit_modification (edit);
897 if (c == '\n') {
898 if (edit->book_mark)
899 book_mark_inc (edit, edit->curs_line);
900 edit->total_lines++;
901 edit->force |= REDRAW_AFTER_CURSOR;
903 edit_push_action (edit, DELCHAR);
905 edit->mark1 += (edit->mark1 >= edit->curs1);
906 edit->mark2 += (edit->mark2 >= edit->curs1);
907 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
909 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
910 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
911 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
913 edit->last_byte++;
914 edit->curs2++;
918 int edit_delete (WEdit * edit)
920 int p;
921 if (!edit->curs2)
922 return 0;
924 edit->mark1 -= (edit->mark1 > edit->curs1);
925 edit->mark2 -= (edit->mark2 > edit->curs1);
926 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
928 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
930 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
931 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
932 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
934 edit->last_byte--;
935 edit->curs2--;
937 edit_modification (edit);
938 if (p == '\n') {
939 if (edit->book_mark)
940 book_mark_dec (edit, edit->curs_line);
941 edit->total_lines--;
942 edit->force |= REDRAW_AFTER_CURSOR;
944 edit_push_action (edit, p + 256);
945 if (edit->curs1 < edit->start_display) {
946 edit->start_display--;
947 if (p == '\n')
948 edit->start_line--;
951 return p;
955 static int
956 edit_backspace (WEdit * edit)
958 int p;
959 if (!edit->curs1)
960 return 0;
962 edit->mark1 -= (edit->mark1 >= edit->curs1);
963 edit->mark2 -= (edit->mark2 >= edit->curs1);
964 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
966 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
967 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
968 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
969 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
971 edit->last_byte--;
972 edit->curs1--;
974 edit_modification (edit);
975 if (p == '\n') {
976 if (edit->book_mark)
977 book_mark_dec (edit, edit->curs_line);
978 edit->curs_line--;
979 edit->total_lines--;
980 edit->force |= REDRAW_AFTER_CURSOR;
982 edit_push_action (edit, p);
984 if (edit->curs1 < edit->start_display) {
985 edit->start_display--;
986 if (p == '\n')
987 edit->start_line--;
990 return p;
993 #ifdef FAST_MOVE_CURSOR
995 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
997 unsigned long next;
998 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
999 edit->curs_line--;
1000 next -= (unsigned long) dest;
1001 n -= next;
1002 src += next;
1003 dest += next;
1008 edit_move_backward_lots (WEdit *edit, long increment)
1010 int r, s, t;
1011 unsigned char *p;
1013 if (increment > edit->curs1)
1014 increment = edit->curs1;
1015 if (increment <= 0)
1016 return -1;
1017 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1019 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1020 if (r > increment)
1021 r = increment;
1022 s = edit->curs1 & M_EDIT_BUF_SIZE;
1024 p = 0;
1025 if (s > r) {
1026 memqcpy (edit,
1027 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1028 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1030 } else {
1031 if (s) {
1032 memqcpy (edit,
1033 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1034 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1035 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1036 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1038 memqcpy (edit,
1039 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1040 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1041 EDIT_BUF_SIZE - (r - s), r - s);
1043 increment -= r;
1044 edit->curs1 -= r;
1045 edit->curs2 += r;
1046 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1047 if (p)
1048 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1049 else
1050 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1051 g_malloc (EDIT_BUF_SIZE);
1052 } else {
1053 g_free (p);
1056 s = edit->curs1 & M_EDIT_BUF_SIZE;
1057 while (increment) {
1058 p = 0;
1059 r = EDIT_BUF_SIZE;
1060 if (r > increment)
1061 r = increment;
1062 t = s;
1063 if (r < t)
1064 t = r;
1065 memqcpy (edit,
1066 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1067 EDIT_BUF_SIZE - t,
1068 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1070 if (r >= s) {
1071 if (t) {
1072 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1073 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1075 memqcpy (edit,
1076 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1077 EDIT_BUF_SIZE - r,
1078 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1079 EDIT_BUF_SIZE - (r - s), r - s);
1081 increment -= r;
1082 edit->curs1 -= r;
1083 edit->curs2 += r;
1084 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1085 if (p)
1086 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1087 else
1088 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1089 g_malloc (EDIT_BUF_SIZE);
1090 } else {
1091 g_free (p);
1094 return edit_get_byte (edit, edit->curs1);
1097 #endif /* ! FAST_MOVE_CURSOR */
1099 /* moves the cursor right or left: increment positive or negative respectively */
1100 void edit_cursor_move (WEdit * edit, long increment)
1102 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1103 int c;
1105 #ifdef FAST_MOVE_CURSOR
1106 if (increment < -256) {
1107 edit->force |= REDRAW_PAGE;
1108 edit_move_backward_lots (edit, -increment);
1109 return;
1111 #endif /* ! FAST_MOVE_CURSOR */
1113 if (increment < 0) {
1114 for (; increment < 0; increment++) {
1115 if (!edit->curs1)
1116 return;
1118 edit_push_action (edit, CURS_RIGHT);
1120 c = edit_get_byte (edit, edit->curs1 - 1);
1121 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1122 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1123 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1124 edit->curs2++;
1125 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1126 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1127 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1128 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1130 edit->curs1--;
1131 if (c == '\n') {
1132 edit->curs_line--;
1133 edit->force |= REDRAW_LINE_BELOW;
1137 } else if (increment > 0) {
1138 for (; increment > 0; increment--) {
1139 if (!edit->curs2)
1140 return;
1142 edit_push_action (edit, CURS_LEFT);
1144 c = edit_get_byte (edit, edit->curs1);
1145 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1146 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1147 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1148 edit->curs1++;
1149 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1150 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1151 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1152 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1154 edit->curs2--;
1155 if (c == '\n') {
1156 edit->curs_line++;
1157 edit->force |= REDRAW_LINE_ABOVE;
1163 /* These functions return positions relative to lines */
1165 /* returns index of last char on line + 1 */
1166 long edit_eol (WEdit * edit, long current)
1168 if (current < edit->last_byte) {
1169 for (;; current++)
1170 if (edit_get_byte (edit, current) == '\n')
1171 break;
1172 } else
1173 return edit->last_byte;
1174 return current;
1177 /* returns index of first char on line */
1178 long edit_bol (WEdit * edit, long current)
1180 if (current > 0) {
1181 for (;; current--)
1182 if (edit_get_byte (edit, current - 1) == '\n')
1183 break;
1184 } else
1185 return 0;
1186 return current;
1190 int edit_count_lines (WEdit * edit, long current, int upto)
1192 int lines = 0;
1193 if (upto > edit->last_byte)
1194 upto = edit->last_byte;
1195 if (current < 0)
1196 current = 0;
1197 while (current < upto)
1198 if (edit_get_byte (edit, current++) == '\n')
1199 lines++;
1200 return lines;
1204 /* If lines is zero this returns the count of lines from current to upto. */
1205 /* If upto is zero returns index of lines forward current. */
1206 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1208 if (upto) {
1209 return edit_count_lines (edit, current, upto);
1210 } else {
1211 int next;
1212 if (lines < 0)
1213 lines = 0;
1214 while (lines--) {
1215 next = edit_eol (edit, current) + 1;
1216 if (next > edit->last_byte)
1217 break;
1218 else
1219 current = next;
1221 return current;
1226 /* Returns offset of 'lines' lines up from current */
1227 long edit_move_backward (WEdit * edit, long current, int lines)
1229 if (lines < 0)
1230 lines = 0;
1231 current = edit_bol (edit, current);
1232 while((lines--) && current != 0)
1233 current = edit_bol (edit, current - 1);
1234 return current;
1237 /* If cols is zero this returns the count of columns from current to upto. */
1238 /* If upto is zero returns index of cols across from current. */
1239 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1241 long p, q;
1242 int col = 0;
1244 if (upto) {
1245 q = upto;
1246 cols = -10;
1247 } else
1248 q = edit->last_byte + 2;
1250 for (col = 0, p = current; p < q; p++) {
1251 int c;
1252 if (cols != -10) {
1253 if (col == cols)
1254 return p;
1255 if (col > cols)
1256 return p - 1;
1258 c = edit_get_byte (edit, p);
1259 if (c == '\t')
1260 col += TAB_SIZE - col % TAB_SIZE;
1261 else if (c == '\n') {
1262 if (upto)
1263 return col;
1264 else
1265 return p;
1266 } else if (c < 32 || c == 127)
1267 col += 2; /* Caret notation for control characters */
1268 else
1269 col++;
1271 return col;
1274 /* returns the current column position of the cursor */
1275 int edit_get_col (WEdit * edit)
1277 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1281 /* Scrolling functions */
1283 void edit_update_curs_row (WEdit * edit)
1285 edit->curs_row = edit->curs_line - edit->start_line;
1288 void edit_update_curs_col (WEdit * edit)
1290 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1293 /*moves the display start position up by i lines */
1294 void edit_scroll_upward (WEdit * edit, unsigned long i)
1296 unsigned long lines_above = edit->start_line;
1297 if (i > lines_above)
1298 i = lines_above;
1299 if (i) {
1300 edit->start_line -= i;
1301 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1302 edit->force |= REDRAW_PAGE;
1303 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1305 edit_update_curs_row (edit);
1309 /* returns 1 if could scroll, 0 otherwise */
1310 void edit_scroll_downward (WEdit * edit, int i)
1312 int lines_below;
1313 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1314 if (lines_below > 0) {
1315 if (i > lines_below)
1316 i = lines_below;
1317 edit->start_line += i;
1318 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1319 edit->force |= REDRAW_PAGE;
1320 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1322 edit_update_curs_row (edit);
1325 void edit_scroll_right (WEdit * edit, int i)
1327 edit->force |= REDRAW_PAGE;
1328 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1329 edit->start_col -= i;
1332 void edit_scroll_left (WEdit * edit, int i)
1334 if (edit->start_col) {
1335 edit->start_col += i;
1336 if (edit->start_col > 0)
1337 edit->start_col = 0;
1338 edit->force |= REDRAW_PAGE;
1339 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1343 /* high level cursor movement commands */
1345 static int is_in_indent (WEdit *edit)
1347 long p = edit_bol (edit, edit->curs1);
1348 while (p < edit->curs1)
1349 if (!strchr (" \t", edit_get_byte (edit, p++)))
1350 return 0;
1351 return 1;
1354 static int left_of_four_spaces (WEdit *edit);
1356 void
1357 edit_move_to_prev_col (WEdit * edit, long p)
1359 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1361 if (is_in_indent (edit) && option_fake_half_tabs) {
1362 edit_update_curs_col (edit);
1363 if (space_width)
1364 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1365 int q = edit->curs_col;
1366 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1367 p = edit_bol (edit, edit->curs1);
1368 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1369 if (!left_of_four_spaces (edit))
1370 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1376 /* move i lines */
1377 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1379 unsigned long p, l = edit->curs_line;
1381 if (i > l)
1382 i = l;
1383 if (i) {
1384 if (i > 1)
1385 edit->force |= REDRAW_PAGE;
1386 if (scroll)
1387 edit_scroll_upward (edit, i);
1389 p = edit_bol (edit, edit->curs1);
1390 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1391 edit_move_to_prev_col (edit, p);
1393 edit->search_start = edit->curs1;
1394 edit->found_len = 0;
1398 static int
1399 is_blank (WEdit *edit, long offset)
1401 long s, f;
1402 int c;
1403 s = edit_bol (edit, offset);
1404 f = edit_eol (edit, offset) - 1;
1405 while (s <= f) {
1406 c = edit_get_byte (edit, s++);
1407 if (!isspace (c))
1408 return 0;
1410 return 1;
1414 /* returns the offset of line i */
1415 static long
1416 edit_find_line (WEdit *edit, int line)
1418 int i, j = 0;
1419 int m = 2000000000;
1420 if (!edit->caches_valid) {
1421 for (i = 0; i < N_LINE_CACHES; i++)
1422 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1423 /* three offsets that we *know* are line 0 at 0 and these two: */
1424 edit->line_numbers[1] = edit->curs_line;
1425 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1426 edit->line_numbers[2] = edit->total_lines;
1427 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1428 edit->caches_valid = 1;
1430 if (line >= edit->total_lines)
1431 return edit->line_offsets[2];
1432 if (line <= 0)
1433 return 0;
1434 /* find the closest known point */
1435 for (i = 0; i < N_LINE_CACHES; i++) {
1436 int n;
1437 n = abs (edit->line_numbers[i] - line);
1438 if (n < m) {
1439 m = n;
1440 j = i;
1443 if (m == 0)
1444 return edit->line_offsets[j]; /* know the offset exactly */
1445 if (m == 1 && j >= 3)
1446 i = j; /* one line different - caller might be looping, so stay in this cache */
1447 else
1448 i = 3 + (rand () % (N_LINE_CACHES - 3));
1449 if (line > edit->line_numbers[j])
1450 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1451 else
1452 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1453 edit->line_numbers[i] = line;
1454 return edit->line_offsets[i];
1457 int line_is_blank (WEdit * edit, long line)
1459 return is_blank (edit, edit_find_line (edit, line));
1462 /* moves up until a blank line is reached, or until just
1463 before a non-blank line is reached */
1464 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1466 int i;
1467 if (edit->curs_line <= 1) {
1468 i = 0;
1469 } else {
1470 if (line_is_blank (edit, edit->curs_line)) {
1471 if (line_is_blank (edit, edit->curs_line - 1)) {
1472 for (i = edit->curs_line - 1; i; i--)
1473 if (!line_is_blank (edit, i)) {
1474 i++;
1475 break;
1477 } else {
1478 for (i = edit->curs_line - 1; i; i--)
1479 if (line_is_blank (edit, i))
1480 break;
1482 } else {
1483 for (i = edit->curs_line - 1; i; i--)
1484 if (line_is_blank (edit, i))
1485 break;
1488 edit_move_up (edit, edit->curs_line - i, scroll);
1491 /* move i lines */
1492 void edit_move_down (WEdit * edit, int i, int scroll)
1494 long p, l = edit->total_lines - edit->curs_line;
1496 if (i > l)
1497 i = l;
1498 if (i) {
1499 if (i > 1)
1500 edit->force |= REDRAW_PAGE;
1501 if (scroll)
1502 edit_scroll_downward (edit, i);
1503 p = edit_bol (edit, edit->curs1);
1504 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1505 edit_move_to_prev_col (edit, p);
1507 edit->search_start = edit->curs1;
1508 edit->found_len = 0;
1512 /* moves down until a blank line is reached, or until just
1513 before a non-blank line is reached */
1514 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1516 int i;
1517 if (edit->curs_line >= edit->total_lines - 1) {
1518 i = edit->total_lines;
1519 } else {
1520 if (line_is_blank (edit, edit->curs_line)) {
1521 if (line_is_blank (edit, edit->curs_line + 1)) {
1522 for (i = edit->curs_line + 1; i; i++)
1523 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1524 i--;
1525 break;
1527 } else {
1528 for (i = edit->curs_line + 1; i; i++)
1529 if (line_is_blank (edit, i) || i >= edit->total_lines)
1530 break;
1532 } else {
1533 for (i = edit->curs_line + 1; i; i++)
1534 if (line_is_blank (edit, i) || i >= edit->total_lines)
1535 break;
1538 edit_move_down (edit, i - edit->curs_line, scroll);
1541 static void edit_begin_page (WEdit *edit)
1543 edit_update_curs_row (edit);
1544 edit_move_up (edit, edit->curs_row, 0);
1547 static void edit_end_page (WEdit *edit)
1549 edit_update_curs_row (edit);
1550 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1554 /* goto beginning of text */
1555 static void edit_move_to_top (WEdit * edit)
1557 if (edit->curs_line) {
1558 edit_cursor_move (edit, -edit->curs1);
1559 edit_move_to_prev_col (edit, 0);
1560 edit->force |= REDRAW_PAGE;
1561 edit->search_start = 0;
1562 edit_update_curs_row(edit);
1567 /* goto end of text */
1568 static void edit_move_to_bottom (WEdit * edit)
1570 if (edit->curs_line < edit->total_lines) {
1571 edit_cursor_move (edit, edit->curs2);
1572 edit->start_display = edit->last_byte;
1573 edit->start_line = edit->total_lines;
1574 edit_update_curs_row(edit);
1575 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1576 edit->force |= REDRAW_PAGE;
1580 /* goto beginning of line */
1581 static void edit_cursor_to_bol (WEdit * edit)
1583 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1584 edit->search_start = edit->curs1;
1585 edit->prev_col = edit_get_col (edit);
1588 /* goto end of line */
1589 static void edit_cursor_to_eol (WEdit * edit)
1591 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1592 edit->search_start = edit->curs1;
1593 edit->prev_col = edit_get_col (edit);
1596 /* move cursor to line 'line' */
1597 void edit_move_to_line (WEdit * e, long line)
1599 if(line < e->curs_line)
1600 edit_move_up (e, e->curs_line - line, 0);
1601 else
1602 edit_move_down (e, line - e->curs_line, 0);
1603 edit_scroll_screen_over_cursor (e);
1606 /* scroll window so that first visible line is 'line' */
1607 void edit_move_display (WEdit * e, long line)
1609 if(line < e->start_line)
1610 edit_scroll_upward (e, e->start_line - line);
1611 else
1612 edit_scroll_downward (e, line - e->start_line);
1615 /* save markers onto undo stack */
1616 void edit_push_markers (WEdit * edit)
1618 edit_push_action (edit, MARK_1 + edit->mark1);
1619 edit_push_action (edit, MARK_2 + edit->mark2);
1622 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1624 edit->mark1 = m1;
1625 edit->mark2 = m2;
1626 edit->column1 = c1;
1627 edit->column2 = c2;
1631 /* highlight marker toggle */
1632 void edit_mark_cmd (WEdit * edit, int unmark)
1634 edit_push_markers (edit);
1635 if (unmark) {
1636 edit_set_markers (edit, 0, 0, 0, 0);
1637 edit->force |= REDRAW_PAGE;
1638 } else {
1639 if (edit->mark2 >= 0) {
1640 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1641 edit->force |= REDRAW_PAGE;
1642 } else
1643 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1647 static unsigned long
1648 my_type_of (int c)
1650 int x, r = 0;
1651 const char *p, *q;
1652 const char option_chars_move_whole_word[] =
1653 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1655 if (!c)
1656 return 0;
1657 if (c == '!') {
1658 if (*option_chars_move_whole_word == '!')
1659 return 2;
1660 return 0x80000000UL;
1662 if (isupper (c))
1663 c = 'A';
1664 else if (islower (c))
1665 c = 'a';
1666 else if (isalpha (c))
1667 c = 'a';
1668 else if (isdigit (c))
1669 c = '0';
1670 else if (isspace (c))
1671 c = ' ';
1672 q = strchr (option_chars_move_whole_word, c);
1673 if (!q)
1674 return 0xFFFFFFFFUL;
1675 do {
1676 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1677 if (*p == '!')
1678 x <<= 1;
1679 r |= x;
1680 } while ((q = strchr (q + 1, c)));
1681 return r;
1684 static void
1685 edit_left_word_move (WEdit *edit, int s)
1687 for (;;) {
1688 int c1, c2;
1689 edit_cursor_move (edit, -1);
1690 if (!edit->curs1)
1691 break;
1692 c1 = edit_get_byte (edit, edit->curs1 - 1);
1693 c2 = edit_get_byte (edit, edit->curs1);
1694 if (!(my_type_of (c1) & my_type_of (c2)))
1695 break;
1696 if (isspace (c1) && !isspace (c2))
1697 break;
1698 if (s)
1699 if (!isspace (c1) && isspace (c2))
1700 break;
1704 static void edit_left_word_move_cmd (WEdit * edit)
1706 edit_left_word_move (edit, 0);
1707 edit->force |= REDRAW_PAGE;
1710 static void
1711 edit_right_word_move (WEdit *edit, int s)
1713 for (;;) {
1714 int c1, c2;
1715 edit_cursor_move (edit, 1);
1716 if (edit->curs1 >= edit->last_byte)
1717 break;
1718 c1 = edit_get_byte (edit, edit->curs1 - 1);
1719 c2 = edit_get_byte (edit, edit->curs1);
1720 if (!(my_type_of (c1) & my_type_of (c2)))
1721 break;
1722 if (isspace (c1) && !isspace (c2))
1723 break;
1724 if (s)
1725 if (!isspace (c1) && isspace (c2))
1726 break;
1730 static void edit_right_word_move_cmd (WEdit * edit)
1732 edit_right_word_move (edit, 0);
1733 edit->force |= REDRAW_PAGE;
1737 static void edit_right_delete_word (WEdit * edit)
1739 int c1, c2;
1740 for (;;) {
1741 if (edit->curs1 >= edit->last_byte)
1742 break;
1743 c1 = edit_delete (edit);
1744 c2 = edit_get_byte (edit, edit->curs1);
1745 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1746 break;
1747 if (!(my_type_of (c1) & my_type_of (c2)))
1748 break;
1752 static void edit_left_delete_word (WEdit * edit)
1754 int c1, c2;
1755 for (;;) {
1756 if (edit->curs1 <= 0)
1757 break;
1758 c1 = edit_backspace (edit);
1759 c2 = edit_get_byte (edit, edit->curs1 - 1);
1760 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1761 break;
1762 if (!(my_type_of (c1) & my_type_of (c2)))
1763 break;
1768 the start column position is not recorded, and hence does not
1769 undo as it happed. But who would notice.
1771 static void
1772 edit_do_undo (WEdit * edit)
1774 long ac;
1775 long count = 0;
1777 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1779 while ((ac = pop_action (edit)) < KEY_PRESS) {
1780 switch ((int) ac) {
1781 case STACK_BOTTOM:
1782 goto done_undo;
1783 case CURS_RIGHT:
1784 edit_cursor_move (edit, 1);
1785 break;
1786 case CURS_LEFT:
1787 edit_cursor_move (edit, -1);
1788 break;
1789 case BACKSPACE:
1790 edit_backspace (edit);
1791 break;
1792 case DELCHAR:
1793 edit_delete (edit);
1794 break;
1795 case COLUMN_ON:
1796 column_highlighting = 1;
1797 break;
1798 case COLUMN_OFF:
1799 column_highlighting = 0;
1800 break;
1802 if (ac >= 256 && ac < 512)
1803 edit_insert_ahead (edit, ac - 256);
1804 if (ac >= 0 && ac < 256)
1805 edit_insert (edit, ac);
1807 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1808 edit->mark1 = ac - MARK_1;
1809 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1810 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1811 edit->mark2 = ac - MARK_2;
1812 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1814 if (count++)
1815 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1818 if (edit->start_display > ac - KEY_PRESS) {
1819 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1820 edit->force |= REDRAW_PAGE;
1821 } else if (edit->start_display < ac - KEY_PRESS) {
1822 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1823 edit->force |= REDRAW_PAGE;
1825 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1826 edit_update_curs_row (edit);
1828 done_undo:;
1829 edit->stack_disable = 0;
1832 static void edit_delete_to_line_end (WEdit * edit)
1834 while (edit_get_byte (edit, edit->curs1) != '\n') {
1835 if (!edit->curs2)
1836 break;
1837 edit_delete (edit);
1841 static void edit_delete_to_line_begin (WEdit * edit)
1843 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1844 if (!edit->curs1)
1845 break;
1846 edit_backspace (edit);
1850 void
1851 edit_delete_line (WEdit *edit)
1854 * Delete right part of the line.
1855 * Note that edit_get_byte() returns '\n' when byte position is
1856 * beyond EOF.
1858 while (edit_get_byte (edit, edit->curs1) != '\n') {
1859 (void) edit_delete (edit);
1863 * Delete '\n' char.
1864 * Note that edit_delete() will not corrupt anything if called while
1865 * cursor position is EOF.
1867 (void) edit_delete (edit);
1870 * Delete left part of the line.
1871 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1873 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1874 (void) edit_backspace (edit);
1878 static void insert_spaces_tab (WEdit * edit, int half)
1880 int i;
1881 edit_update_curs_col (edit);
1882 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1883 while (i > 0) {
1884 edit_insert (edit, ' ');
1885 i -= space_width;
1889 static int is_aligned_on_a_tab (WEdit * edit)
1891 edit_update_curs_col (edit);
1892 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1893 return 0; /* not alligned on a tab */
1894 return 1;
1897 static int right_of_four_spaces (WEdit *edit)
1899 int i, ch = 0;
1900 for (i = 1; i <= HALF_TAB_SIZE; i++)
1901 ch |= edit_get_byte (edit, edit->curs1 - i);
1902 if (ch == ' ')
1903 return is_aligned_on_a_tab (edit);
1904 return 0;
1907 static int left_of_four_spaces (WEdit *edit)
1909 int i, ch = 0;
1910 for (i = 0; i < HALF_TAB_SIZE; i++)
1911 ch |= edit_get_byte (edit, edit->curs1 + i);
1912 if (ch == ' ')
1913 return is_aligned_on_a_tab (edit);
1914 return 0;
1917 int edit_indent_width (WEdit * edit, long p)
1919 long q = p;
1920 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1921 q++;
1922 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1925 void edit_insert_indent (WEdit * edit, int indent)
1927 if (!option_fill_tabs_with_spaces) {
1928 while (indent >= TAB_SIZE) {
1929 edit_insert (edit, '\t');
1930 indent -= TAB_SIZE;
1933 while (indent-- > 0)
1934 edit_insert (edit, ' ');
1937 static void
1938 edit_auto_indent (WEdit * edit)
1940 long p;
1941 char c;
1942 p = edit->curs1;
1943 /* use the previous line as a template */
1944 p = edit_move_backward (edit, p, 1);
1945 /* copy the leading whitespace of the line */
1946 for (;;) { /* no range check - the line _is_ \n-terminated */
1947 c = edit_get_byte (edit, p++);
1948 if (c != ' ' && c != '\t')
1949 break;
1950 edit_insert (edit, c);
1954 static void edit_double_newline (WEdit * edit)
1956 edit_insert (edit, '\n');
1957 if (edit_get_byte (edit, edit->curs1) == '\n')
1958 return;
1959 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1960 return;
1961 edit->force |= REDRAW_PAGE;
1962 edit_insert (edit, '\n');
1965 static void edit_tab_cmd (WEdit * edit)
1967 int i;
1969 if (option_fake_half_tabs) {
1970 if (is_in_indent (edit)) {
1971 /*insert a half tab (usually four spaces) unless there is a
1972 half tab already behind, then delete it and insert a
1973 full tab. */
1974 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1975 for (i = 1; i <= HALF_TAB_SIZE; i++)
1976 edit_backspace (edit);
1977 edit_insert (edit, '\t');
1978 } else {
1979 insert_spaces_tab (edit, 1);
1981 return;
1984 if (option_fill_tabs_with_spaces) {
1985 insert_spaces_tab (edit, 0);
1986 } else {
1987 edit_insert (edit, '\t');
1989 return;
1992 static void check_and_wrap_line (WEdit * edit)
1994 int curs, c;
1995 if (!option_typewriter_wrap)
1996 return;
1997 edit_update_curs_col (edit);
1998 if (edit->curs_col < option_word_wrap_line_length)
1999 return;
2000 curs = edit->curs1;
2001 for (;;) {
2002 curs--;
2003 c = edit_get_byte (edit, curs);
2004 if (c == '\n' || curs <= 0) {
2005 edit_insert (edit, '\n');
2006 return;
2008 if (c == ' ' || c == '\t') {
2009 int current = edit->curs1;
2010 edit_cursor_move (edit, curs - edit->curs1 + 1);
2011 edit_insert (edit, '\n');
2012 edit_cursor_move (edit, current - edit->curs1 + 1);
2013 return;
2018 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2020 void edit_push_key_press (WEdit * edit)
2022 edit_push_action (edit, KEY_PRESS + edit->start_display);
2023 if (edit->mark2 == -1)
2024 edit_push_action (edit, MARK_1 + edit->mark1);
2027 /* this find the matching bracket in either direction, and sets edit->bracket */
2028 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2030 const char * const b = "{}{[][()(", *p;
2031 int i = 1, a, inc = -1, c, d, n = 0;
2032 unsigned long j = 0;
2033 long q;
2034 edit_update_curs_row (edit);
2035 c = edit_get_byte (edit, edit->curs1);
2036 p = strchr (b, c);
2037 /* no limit */
2038 if (!furthest_bracket_search)
2039 furthest_bracket_search--;
2040 /* not on a bracket at all */
2041 if (!p)
2042 return -1;
2043 /* the matching bracket */
2044 d = p[1];
2045 /* going left or right? */
2046 if (strchr ("{[(", c))
2047 inc = 1;
2048 for (q = edit->curs1 + inc;; q += inc) {
2049 /* out of buffer? */
2050 if (q >= edit->last_byte || q < 0)
2051 break;
2052 a = edit_get_byte (edit, q);
2053 /* don't want to eat CPU */
2054 if (j++ > furthest_bracket_search)
2055 break;
2056 /* out of screen? */
2057 if (in_screen) {
2058 if (q < edit->start_display)
2059 break;
2060 /* count lines if searching downward */
2061 if (inc > 0 && a == '\n')
2062 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2063 break;
2065 /* count bracket depth */
2066 i += (a == c) - (a == d);
2067 /* return if bracket depth is zero */
2068 if (!i)
2069 return q;
2071 /* no match */
2072 return -1;
2075 static long last_bracket = -1;
2077 void edit_find_bracket (WEdit * edit)
2079 edit->bracket = edit_get_bracket (edit, 1, 10000);
2080 if (last_bracket != edit->bracket)
2081 edit->force |= REDRAW_PAGE;
2082 last_bracket = edit->bracket;
2085 static void edit_goto_matching_bracket (WEdit *edit)
2087 long q;
2088 q = edit_get_bracket (edit, 0, 0);
2089 if (q < 0)
2090 return;
2091 edit->bracket = edit->curs1;
2092 edit->force |= REDRAW_PAGE;
2093 edit_cursor_move (edit, q - edit->curs1);
2097 * This executes a command as though the user initiated it through a key
2098 * press. Callback with WIDGET_KEY as a message calls this after
2099 * translating the key press. This function can be used to pass any
2100 * command to the editor. Note that the screen wouldn't update
2101 * automatically. Either of command or char_for_insertion must be
2102 * passed as -1. Commands are executed, and char_for_insertion is
2103 * inserted at the cursor.
2105 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2107 if (command == CK_Begin_Record_Macro) {
2108 edit->macro_i = 0;
2109 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2110 return;
2112 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2113 edit->force |= REDRAW_COMPLETELY;
2114 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2115 edit->macro_i = -1;
2116 return;
2118 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2119 edit->macro[edit->macro_i].command = command;
2120 edit->macro[edit->macro_i++].ch = char_for_insertion;
2122 /* record the beginning of a set of editing actions initiated by a key press */
2123 if (command != CK_Undo && command != CK_Ext_Mode)
2124 edit_push_key_press (edit);
2126 edit_execute_cmd (edit, command, char_for_insertion);
2127 if (column_highlighting)
2128 edit->force |= REDRAW_PAGE;
2131 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2134 This executes a command at a lower level than macro recording.
2135 It also does not push a key_press onto the undo stack. This means
2136 that if it is called many times, a single undo command will undo
2137 all of them. It also does not check for the Undo command.
2139 void
2140 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2142 edit->force |= REDRAW_LINE;
2144 /* The next key press will unhighlight the found string, so update
2145 * the whole page */
2146 if (edit->found_len || column_highlighting)
2147 edit->force |= REDRAW_PAGE;
2149 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2150 column_highlighting = 0;
2151 if (!edit->highlight
2152 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2153 edit_mark_cmd (edit, 1); /* clear */
2154 edit_mark_cmd (edit, 0); /* marking on */
2156 edit->highlight = 1;
2157 } else { /* any other command */
2158 if (edit->highlight)
2159 edit_mark_cmd (edit, 0); /* clear */
2160 edit->highlight = 0;
2163 /* first check for undo */
2164 if (command == CK_Undo) {
2165 edit_do_undo (edit);
2166 edit->found_len = 0;
2167 edit->prev_col = edit_get_col (edit);
2168 edit->search_start = edit->curs1;
2169 return;
2172 /* An ordinary key press */
2173 if (char_for_insertion >= 0) {
2174 if (edit->overwrite) {
2175 if (edit_get_byte (edit, edit->curs1) != '\n')
2176 edit_delete (edit);
2178 edit_insert (edit, char_for_insertion);
2179 if (option_auto_para_formatting) {
2180 format_paragraph (edit, 0);
2181 edit->force |= REDRAW_PAGE;
2182 } else
2183 check_and_wrap_line (edit);
2184 edit->found_len = 0;
2185 edit->prev_col = edit_get_col (edit);
2186 edit->search_start = edit->curs1;
2187 edit_find_bracket (edit);
2188 return;
2190 switch (command) {
2191 case CK_Begin_Page:
2192 case CK_End_Page:
2193 case CK_Begin_Page_Highlight:
2194 case CK_End_Page_Highlight:
2195 case CK_Word_Left:
2196 case CK_Word_Right:
2197 case CK_Up:
2198 case CK_Down:
2199 case CK_Word_Left_Highlight:
2200 case CK_Word_Right_Highlight:
2201 case CK_Up_Highlight:
2202 case CK_Down_Highlight:
2203 if (edit->mark2 == -1)
2204 break; /*marking is following the cursor: may need to highlight a whole line */
2205 case CK_Left:
2206 case CK_Right:
2207 case CK_Left_Highlight:
2208 case CK_Right_Highlight:
2209 edit->force |= REDRAW_CHAR_ONLY;
2212 /* basic cursor key commands */
2213 switch (command) {
2214 case CK_BackSpace:
2215 if (option_backspace_through_tabs && is_in_indent (edit)) {
2216 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2217 && edit->curs1 > 0)
2218 edit_backspace (edit);
2219 break;
2220 } else {
2221 if (option_fake_half_tabs) {
2222 int i;
2223 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2224 for (i = 0; i < HALF_TAB_SIZE; i++)
2225 edit_backspace (edit);
2226 break;
2230 edit_backspace (edit);
2231 break;
2232 case CK_Delete:
2233 if (option_fake_half_tabs) {
2234 int i;
2235 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2236 for (i = 1; i <= HALF_TAB_SIZE; i++)
2237 edit_delete (edit);
2238 break;
2241 edit_delete (edit);
2242 break;
2243 case CK_Delete_Word_Left:
2244 edit_left_delete_word (edit);
2245 break;
2246 case CK_Delete_Word_Right:
2247 edit_right_delete_word (edit);
2248 break;
2249 case CK_Delete_Line:
2250 edit_delete_line (edit);
2251 break;
2252 case CK_Delete_To_Line_End:
2253 edit_delete_to_line_end (edit);
2254 break;
2255 case CK_Delete_To_Line_Begin:
2256 edit_delete_to_line_begin (edit);
2257 break;
2258 case CK_Enter:
2259 if (option_auto_para_formatting) {
2260 edit_double_newline (edit);
2261 if (option_return_does_auto_indent)
2262 edit_auto_indent (edit);
2263 format_paragraph (edit, 0);
2264 } else {
2265 edit_insert (edit, '\n');
2266 if (option_return_does_auto_indent) {
2267 edit_auto_indent (edit);
2270 break;
2271 case CK_Return:
2272 edit_insert (edit, '\n');
2273 break;
2275 case CK_Page_Up:
2276 case CK_Page_Up_Highlight:
2277 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2278 break;
2279 case CK_Page_Down:
2280 case CK_Page_Down_Highlight:
2281 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2282 break;
2283 case CK_Left:
2284 case CK_Left_Highlight:
2285 if (option_fake_half_tabs) {
2286 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2287 edit_cursor_move (edit, -HALF_TAB_SIZE);
2288 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2289 break;
2292 edit_cursor_move (edit, -1);
2293 break;
2294 case CK_Right:
2295 case CK_Right_Highlight:
2296 if (option_fake_half_tabs) {
2297 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2298 edit_cursor_move (edit, HALF_TAB_SIZE);
2299 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2300 break;
2303 edit_cursor_move (edit, 1);
2304 break;
2305 case CK_Begin_Page:
2306 case CK_Begin_Page_Highlight:
2307 edit_begin_page (edit);
2308 break;
2309 case CK_End_Page:
2310 case CK_End_Page_Highlight:
2311 edit_end_page (edit);
2312 break;
2313 case CK_Word_Left:
2314 case CK_Word_Left_Highlight:
2315 edit_left_word_move_cmd (edit);
2316 break;
2317 case CK_Word_Right:
2318 case CK_Word_Right_Highlight:
2319 edit_right_word_move_cmd (edit);
2320 break;
2321 case CK_Up:
2322 case CK_Up_Highlight:
2323 edit_move_up (edit, 1, 0);
2324 break;
2325 case CK_Down:
2326 case CK_Down_Highlight:
2327 edit_move_down (edit, 1, 0);
2328 break;
2329 case CK_Paragraph_Up:
2330 case CK_Paragraph_Up_Highlight:
2331 edit_move_up_paragraph (edit, 0);
2332 break;
2333 case CK_Paragraph_Down:
2334 case CK_Paragraph_Down_Highlight:
2335 edit_move_down_paragraph (edit, 0);
2336 break;
2337 case CK_Scroll_Up:
2338 case CK_Scroll_Up_Highlight:
2339 edit_move_up (edit, 1, 1);
2340 break;
2341 case CK_Scroll_Down:
2342 case CK_Scroll_Down_Highlight:
2343 edit_move_down (edit, 1, 1);
2344 break;
2345 case CK_Home:
2346 case CK_Home_Highlight:
2347 edit_cursor_to_bol (edit);
2348 break;
2349 case CK_End:
2350 case CK_End_Highlight:
2351 edit_cursor_to_eol (edit);
2352 break;
2354 case CK_Tab:
2355 edit_tab_cmd (edit);
2356 if (option_auto_para_formatting) {
2357 format_paragraph (edit, 0);
2358 edit->force |= REDRAW_PAGE;
2359 } else
2360 check_and_wrap_line (edit);
2361 break;
2363 case CK_Toggle_Insert:
2364 edit->overwrite = (edit->overwrite == 0);
2365 break;
2367 case CK_Mark:
2368 if (edit->mark2 >= 0) {
2369 if (column_highlighting)
2370 edit_push_action (edit, COLUMN_ON);
2371 column_highlighting = 0;
2373 edit_mark_cmd (edit, 0);
2374 break;
2375 case CK_Column_Mark:
2376 if (!column_highlighting)
2377 edit_push_action (edit, COLUMN_OFF);
2378 column_highlighting = 1;
2379 edit_mark_cmd (edit, 0);
2380 break;
2381 case CK_Unmark:
2382 if (column_highlighting)
2383 edit_push_action (edit, COLUMN_ON);
2384 column_highlighting = 0;
2385 edit_mark_cmd (edit, 1);
2386 break;
2388 case CK_Toggle_Bookmark:
2389 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2390 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2391 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2392 else
2393 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2394 break;
2395 case CK_Flush_Bookmarks:
2396 book_mark_flush (edit, BOOK_MARK_COLOR);
2397 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2398 edit->force |= REDRAW_PAGE;
2399 break;
2400 case CK_Next_Bookmark:
2401 if (edit->book_mark) {
2402 struct _book_mark *p;
2403 p = (struct _book_mark *) book_mark_find (edit,
2404 edit->curs_line);
2405 if (p->next) {
2406 p = p->next;
2407 if (p->line >= edit->start_line + edit->num_widget_lines
2408 || p->line < edit->start_line)
2409 edit_move_display (edit,
2410 p->line -
2411 edit->num_widget_lines / 2);
2412 edit_move_to_line (edit, p->line);
2415 break;
2416 case CK_Prev_Bookmark:
2417 if (edit->book_mark) {
2418 struct _book_mark *p;
2419 p = (struct _book_mark *) book_mark_find (edit,
2420 edit->curs_line);
2421 while (p->line == edit->curs_line)
2422 if (p->prev)
2423 p = p->prev;
2424 if (p->line >= 0) {
2425 if (p->line >= edit->start_line + edit->num_widget_lines
2426 || p->line < edit->start_line)
2427 edit_move_display (edit,
2428 p->line -
2429 edit->num_widget_lines / 2);
2430 edit_move_to_line (edit, p->line);
2433 break;
2435 case CK_Beginning_Of_Text:
2436 case CK_Beginning_Of_Text_Highlight:
2437 edit_move_to_top (edit);
2438 break;
2439 case CK_End_Of_Text:
2440 case CK_End_Of_Text_Highlight:
2441 edit_move_to_bottom (edit);
2442 break;
2444 case CK_Copy:
2445 edit_block_copy_cmd (edit);
2446 break;
2447 case CK_Remove:
2448 edit_block_delete_cmd (edit);
2449 break;
2450 case CK_Move:
2451 edit_block_move_cmd (edit);
2452 break;
2454 case CK_XStore:
2455 edit_copy_to_X_buf_cmd (edit);
2456 break;
2457 case CK_XCut:
2458 edit_cut_to_X_buf_cmd (edit);
2459 break;
2460 case CK_XPaste:
2461 edit_paste_from_X_buf_cmd (edit);
2462 break;
2463 case CK_Selection_History:
2464 edit_paste_from_history (edit);
2465 break;
2467 case CK_Save_As:
2468 edit_save_as_cmd (edit);
2469 break;
2470 case CK_Save:
2471 edit_save_confirm_cmd (edit);
2472 break;
2473 case CK_Load:
2474 edit_load_cmd (edit);
2475 break;
2476 case CK_Save_Block:
2477 edit_save_block_cmd (edit);
2478 break;
2479 case CK_Insert_File:
2480 edit_insert_file_cmd (edit);
2481 break;
2483 case CK_Toggle_Syntax:
2484 if ((option_syntax_highlighting ^= 1) == 1)
2485 edit_load_syntax (edit, NULL, option_syntax_type);
2486 edit->force |= REDRAW_PAGE;
2487 break;
2489 case CK_Find:
2490 edit_search_cmd (edit, 0);
2491 break;
2492 case CK_Find_Again:
2493 edit_search_cmd (edit, 1);
2494 break;
2495 case CK_Replace:
2496 edit_replace_cmd (edit, 0);
2497 break;
2498 case CK_Replace_Again:
2499 edit_replace_cmd (edit, 1);
2500 break;
2501 case CK_Complete_Word:
2502 edit_complete_word_cmd (edit);
2503 break;
2505 case CK_Exit:
2506 dlg_stop (edit->widget.parent);
2507 break;
2508 case CK_New:
2509 edit_new_cmd (edit);
2510 break;
2512 case CK_Help:
2513 edit_help_cmd (edit);
2514 break;
2516 case CK_Refresh:
2517 edit_refresh_cmd (edit);
2518 break;
2520 case CK_Date:{
2521 char s[1024];
2522 /* fool gcc to prevent a Y2K warning */
2523 char time_format[] = "_c";
2524 time_format[0] = '%';
2526 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
2527 edit_print_string (edit, s);
2528 edit->force |= REDRAW_PAGE;
2529 break;
2531 case CK_Goto:
2532 edit_goto_cmd (edit);
2533 break;
2534 case CK_Paragraph_Format:
2535 format_paragraph (edit, 1);
2536 edit->force |= REDRAW_PAGE;
2537 break;
2538 case CK_Delete_Macro:
2539 edit_delete_macro_cmd (edit);
2540 break;
2541 case CK_Match_Bracket:
2542 edit_goto_matching_bracket (edit);
2543 break;
2544 case CK_User_Menu:
2545 user_menu (edit);
2546 break;
2547 case CK_Sort:
2548 edit_sort_cmd (edit);
2549 break;
2550 case CK_ExtCmd:
2551 edit_ext_cmd (edit);
2552 break;
2553 case CK_Mail:
2554 edit_mail_dialog (edit);
2555 break;
2556 case CK_Shell:
2557 view_other_cmd ();
2558 break;
2559 case CK_Select_Codepage:
2560 edit_select_codepage_cmd (edit);
2561 break;
2562 case CK_Insert_Literal:
2563 edit_insert_literal_cmd (edit);
2564 break;
2565 case CK_Execute_Macro:
2566 edit_execute_macro_cmd (edit);
2567 break;
2568 case CK_Begin_End_Macro:
2569 edit_begin_end_macro_cmd (edit);
2570 break;
2571 case CK_Ext_Mode:
2572 edit->extmod = 1;
2573 break;
2574 default:
2575 break;
2578 /* CK_Pipe_Block */
2579 if ((command / 1000) == 1) /* a shell command */
2580 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2581 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2582 struct macro m[MAX_MACRO_LENGTH];
2583 int nm;
2584 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
2585 edit_execute_macro (edit, m, nm);
2588 /* keys which must set the col position, and the search vars */
2589 switch (command) {
2590 case CK_Find:
2591 case CK_Find_Again:
2592 case CK_Replace:
2593 case CK_Replace_Again:
2594 case CK_Complete_Word:
2595 edit->prev_col = edit_get_col (edit);
2596 break;
2597 case CK_Up:
2598 case CK_Up_Highlight:
2599 case CK_Down:
2600 case CK_Down_Highlight:
2601 case CK_Page_Up:
2602 case CK_Page_Up_Highlight:
2603 case CK_Page_Down:
2604 case CK_Page_Down_Highlight:
2605 case CK_Beginning_Of_Text:
2606 case CK_Beginning_Of_Text_Highlight:
2607 case CK_End_Of_Text:
2608 case CK_End_Of_Text_Highlight:
2609 case CK_Paragraph_Up:
2610 case CK_Paragraph_Up_Highlight:
2611 case CK_Paragraph_Down:
2612 case CK_Paragraph_Down_Highlight:
2613 case CK_Scroll_Up:
2614 case CK_Scroll_Up_Highlight:
2615 case CK_Scroll_Down:
2616 case CK_Scroll_Down_Highlight:
2617 edit->search_start = edit->curs1;
2618 edit->found_len = 0;
2619 break;
2620 default:
2621 edit->found_len = 0;
2622 edit->prev_col = edit_get_col (edit);
2623 edit->search_start = edit->curs1;
2625 edit_find_bracket (edit);
2627 if (option_auto_para_formatting) {
2628 switch (command) {
2629 case CK_BackSpace:
2630 case CK_Delete:
2631 case CK_Delete_Word_Left:
2632 case CK_Delete_Word_Right:
2633 case CK_Delete_To_Line_End:
2634 case CK_Delete_To_Line_Begin:
2635 format_paragraph (edit, 0);
2636 edit->force |= REDRAW_PAGE;
2642 static void
2643 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2645 int i = 0;
2647 if (edit->macro_depth++ > 256) {
2648 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2649 edit->macro_depth--;
2650 return;
2652 edit->force |= REDRAW_PAGE;
2653 for (; i < n; i++) {
2654 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2656 edit_update_screen (edit);
2657 edit->macro_depth--;
2660 /* User edit menu, like user menu (F2) but only in editor. */
2661 static void
2662 user_menu (WEdit * edit)
2664 FILE *fd;
2665 int nomark;
2666 struct stat status;
2667 long start_mark, end_mark;
2668 char *block_file = concat_dir_and_file (home_dir, BLOCK_FILE);
2669 int rc = 0;
2671 nomark = eval_marks (edit, &start_mark, &end_mark);
2672 if (!nomark) /* remember marked or not */
2673 edit_save_block (edit, block_file, start_mark, end_mark);
2675 /* run shell scripts from menu */
2676 user_menu_cmd (edit);
2678 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2679 /* no block messages */
2680 goto cleanup;
2683 if (!nomark) {
2684 /* i.e. we have marked block */
2685 rc = edit_block_delete_cmd (edit);
2688 if (!rc) {
2689 edit_insert_file (edit, block_file);
2692 /* truncate block file */
2693 if ((fd = fopen (block_file, "w"))) {
2694 fclose (fd);
2697 edit_refresh_cmd (edit);
2698 edit->force |= REDRAW_COMPLETELY;
2700 cleanup:
2701 g_free (block_file);