Add isc-posix.m4
[midnight-commander.git] / edit / edit.c
blob02fb4d4fbade07e336a3caaf14a2305229c891a1
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #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"
42 #include "../src/cmd.h" /* view_other_cmd() */
43 #include "../src/user.h" /* user_menu_cmd() */
44 #include "../src/wtools.h" /* query_dialog() */
47 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
48 or EDIT_KEY_EMULATION_EMACS
50 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
52 int option_word_wrap_line_length = 72;
53 int option_typewriter_wrap = 0;
54 int option_auto_para_formatting = 0;
55 int option_tab_spacing = 8;
56 int option_fill_tabs_with_spaces = 0;
57 int option_return_does_auto_indent = 1;
58 int option_backspace_through_tabs = 0;
59 int option_fake_half_tabs = 1;
60 int option_save_mode = EDIT_QUICK_SAVE;
61 int option_save_position = 1;
62 int option_backup_ext_int = -1;
63 int option_max_undo = 32768;
65 int option_edit_right_extreme = 0;
66 int option_edit_left_extreme = 0;
67 int option_edit_top_extreme = 0;
68 int option_edit_bottom_extreme = 0;
70 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
71 char *option_backup_ext = "~";
73 /*-
75 * here's a quick sketch of the layout: (don't run this through indent.)
77 * (b1 is buffers1 and b2 is buffers2)
79 * |
80 * \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
81 * ______________________________________|______________________________________
82 * |
83 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
84 * |-> |-> |-> |-> |-> |-> |
85 * |
86 * _<------------------------->|<----------------->_
87 * WEdit->curs2 | WEdit->curs1
88 * ^ | ^
89 * | ^|^ |
90 * cursor ||| cursor
91 * |||
92 * file end|||file beginning
93 * |
94 * |
96 * _
97 * This_is_some_file
98 * fin.
102 static void edit_move_to_prev_col (WEdit *edit, long p);
103 static void user_menu (WEdit *edit);
105 int edit_get_byte (WEdit * edit, long byte_index)
107 unsigned long p;
108 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
109 return '\n';
111 if (byte_index >= edit->curs1) {
112 p = edit->curs1 + edit->curs2 - byte_index - 1;
113 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
114 } else {
115 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
120 * Initialize the buffers for an empty files.
122 static void
123 edit_init_buffers (WEdit *edit)
125 int j;
127 for (j = 0; j <= MAXBUFF; j++) {
128 edit->buffers1[j] = NULL;
129 edit->buffers2[j] = NULL;
132 edit->curs1 = 0;
133 edit->curs2 = 0;
134 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
138 * Load file OR text into buffers. Set cursor to the beginning of file.
139 * Return 1 on error.
141 static int
142 edit_load_file_fast (WEdit *edit, const char *filename)
144 long buf, buf2;
145 int file = -1;
147 edit->curs2 = edit->last_byte;
148 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
150 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
151 GString *errmsg = g_string_new(NULL);
152 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
153 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
154 g_string_free (errmsg, TRUE);
155 return 1;
158 if (!edit->buffers2[buf2])
159 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
161 mc_read (file,
162 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
163 (edit->curs2 & M_EDIT_BUF_SIZE),
164 edit->curs2 & M_EDIT_BUF_SIZE);
166 for (buf = buf2 - 1; buf >= 0; buf--) {
167 /* edit->buffers2[0] is already allocated */
168 if (!edit->buffers2[buf])
169 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
170 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
173 mc_close (file);
174 return 0;
177 /* detecting an error on save is easy: just check if every byte has been written. */
178 /* detecting an error on read, is not so easy 'cos there is not way to tell
179 whether you read everything or not. */
180 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
181 static const struct edit_filters {
182 const char *read, *write, *extension;
183 } all_filters[] = {
184 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
185 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
186 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
189 /* Return index of the filter or -1 is there is no appropriate filter */
190 static int edit_find_filter (const char *filename)
192 size_t i, l, e;
193 if (!filename)
194 return -1;
195 l = strlen (filename);
196 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
197 e = strlen (all_filters[i].extension);
198 if (l > e)
199 if (!strcmp (all_filters[i].extension, filename + l - e))
200 return i;
202 return -1;
205 static char *
206 edit_get_filter (const char *filename)
208 int i, l;
209 char *p, *quoted_name;
210 i = edit_find_filter (filename);
211 if (i < 0)
212 return 0;
213 quoted_name = name_quote (filename, 0);
214 l = strlen (quoted_name);
215 p = g_malloc (strlen (all_filters[i].read) + l + 2);
216 sprintf (p, all_filters[i].read, quoted_name);
217 g_free (quoted_name);
218 return p;
221 char *
222 edit_get_write_filter (const char *write_name, const char *filename)
224 int i, l;
225 char *p, *writename;
226 i = edit_find_filter (filename);
227 if (i < 0)
228 return 0;
229 writename = name_quote (write_name, 0);
230 l = strlen (writename);
231 p = g_malloc (strlen (all_filters[i].write) + l + 2);
232 sprintf (p, all_filters[i].write, writename);
233 g_free (writename);
234 return p;
237 static long
238 edit_insert_stream (WEdit * edit, FILE * f)
240 int c;
241 long i = 0;
242 while ((c = fgetc (f)) >= 0) {
243 edit_insert (edit, c);
244 i++;
246 return i;
249 long edit_write_stream (WEdit * edit, FILE * f)
251 long i;
252 for (i = 0; i < edit->last_byte; i++)
253 if (fputc (edit_get_byte (edit, i), f) < 0)
254 break;
255 return i;
258 #define TEMP_BUF_LEN 1024
260 /* inserts a file at the cursor, returns 1 on success */
262 edit_insert_file (WEdit *edit, const char *filename)
264 char *p;
265 if ((p = edit_get_filter (filename))) {
266 FILE *f;
267 long current = edit->curs1;
268 f = (FILE *) popen (p, "r");
269 if (f) {
270 edit_insert_stream (edit, f);
271 edit_cursor_move (edit, current - edit->curs1);
272 if (pclose (f) > 0) {
273 GString *errmsg = g_string_new (NULL);
274 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
275 edit_error_dialog (_("Error"), errmsg->str);
276 g_string_free (errmsg, TRUE);
277 g_free (p);
278 return 0;
280 } else {
281 GString *errmsg = g_string_new (NULL);
282 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
283 edit_error_dialog (_("Error"), errmsg->str);
284 g_string_free (errmsg, TRUE);
285 g_free (p);
286 return 0;
288 g_free (p);
289 } else {
290 int i, file, blocklen;
291 long current = edit->curs1;
292 unsigned char *buf;
293 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
294 return 0;
295 buf = g_malloc (TEMP_BUF_LEN);
296 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
297 for (i = 0; i < blocklen; i++)
298 edit_insert (edit, buf[i]);
300 edit_cursor_move (edit, current - edit->curs1);
301 g_free (buf);
302 mc_close (file);
303 if (blocklen)
304 return 0;
306 return 1;
309 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
310 static int
311 check_file_access (WEdit *edit, const char *filename, struct stat *st)
313 int file;
314 GString *errmsg = (GString *) 0;
316 /* Try opening an existing file */
317 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
319 if (file < 0) {
321 * Try creating the file. O_EXCL prevents following broken links
322 * and opening existing files.
324 file =
325 mc_open (filename,
326 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
327 0666);
328 if (file < 0) {
329 g_string_sprintf (errmsg = g_string_new (NULL),
330 _(" Cannot open %s for reading "), filename);
331 goto cleanup;
332 } else {
333 /* New file, delete it if it's not modified or saved */
334 edit->delete_file = 1;
338 /* Check what we have opened */
339 if (mc_fstat (file, st) < 0) {
340 g_string_sprintf (errmsg = g_string_new (NULL),
341 _(" Cannot get size/permissions for %s "), filename);
342 goto cleanup;
345 /* We want to open regular files only */
346 if (!S_ISREG (st->st_mode)) {
347 g_string_sprintf (errmsg = g_string_new (NULL),
348 _(" %s is not a regular file "), filename);
349 goto cleanup;
353 * Don't delete non-empty files.
354 * O_EXCL should prevent it, but let's be on the safe side.
356 if (st->st_size > 0) {
357 edit->delete_file = 0;
360 if (st->st_size >= SIZE_LIMIT) {
361 g_string_sprintf (errmsg = g_string_new (NULL),
362 _(" File %s is too large "), filename);
363 goto cleanup;
366 cleanup:
367 (void) mc_close (file);
368 if (errmsg) {
369 edit_error_dialog (_("Error"), errmsg->str);
370 g_string_free (errmsg, TRUE);
371 return 1;
373 return 0;
377 * Open the file and load it into the buffers, either directly or using
378 * a filter. Return 0 on success, 1 on error.
380 * Fast loading (edit_load_file_fast) is used when the file size is
381 * known. In this case the data is read into the buffers by blocks.
382 * If the file size is not known, the data is loaded byte by byte in
383 * edit_insert_file.
385 static int
386 edit_load_file (WEdit *edit)
388 int fast_load = 1;
390 /* Cannot do fast load if a filter is used */
391 if (edit_find_filter (edit->filename) >= 0)
392 fast_load = 0;
395 * VFS may report file size incorrectly, and slow load is not a big
396 * deal considering overhead in VFS.
398 if (!vfs_file_is_local (edit->filename))
399 fast_load = 0;
402 * FIXME: line end translation should disable fast loading as well
403 * Consider doing fseek() to the end and ftell() for the real size.
406 if (*edit->filename) {
407 /* If we are dealing with a real file, check that it exists */
408 if (check_file_access (edit, edit->filename, &edit->stat1))
409 return 1;
410 } else {
411 /* nothing to load */
412 fast_load = 0;
415 edit_init_buffers (edit);
417 if (fast_load) {
418 edit->last_byte = edit->stat1.st_size;
419 edit_load_file_fast (edit, edit->filename);
420 /* If fast load was used, the number of lines wasn't calculated */
421 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
422 } else {
423 edit->last_byte = 0;
424 if (*edit->filename) {
425 edit->stack_disable = 1;
426 if (!edit_insert_file (edit, edit->filename)) {
427 edit_clean (edit);
428 return 1;
430 edit->stack_disable = 0;
433 return 0;
436 /* Restore saved cursor position in the file */
437 static void
438 edit_load_position (WEdit *edit)
440 char *filename;
441 long line, column;
443 if (!edit->filename || !*edit->filename)
444 return;
446 filename = vfs_canon (edit->filename);
447 load_file_position (filename, &line, &column);
448 g_free (filename);
450 edit_move_to_line (edit, line - 1);
451 edit->prev_col = column;
452 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
453 edit_move_display (edit, line - (edit->num_widget_lines / 2));
456 /* Save cursor position in the file */
457 static void
458 edit_save_position (WEdit *edit)
460 char *filename;
462 if (!edit->filename || !*edit->filename)
463 return;
465 filename = vfs_canon (edit->filename);
466 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
467 g_free (filename);
470 /* Clean the WEdit stricture except the widget part */
471 static inline void
472 edit_purge_widget (WEdit *edit)
474 int len = sizeof (WEdit) - sizeof (Widget);
475 char *start = (char *) edit + sizeof (Widget);
476 memset (start, 0, len);
477 edit->macro_i = -1; /* not recording a macro */
480 #define space_width 1
483 * Fill in the edit structure. Return NULL on failure. Pass edit as
484 * NULL to allocate a new structure.
486 * If line is 0, try to restore saved position. Otherwise put the
487 * cursor on that line and show it in the middle of the screen.
489 WEdit *
490 edit_init (WEdit *edit, int lines, int columns, const char *filename,
491 long line)
493 int to_free = 0;
495 if (!edit) {
496 #ifdef ENABLE_NLS
498 * Expand option_whole_chars_search by national letters using
499 * current locale
502 static char option_whole_chars_search_buf[256];
504 if (option_whole_chars_search_buf != option_whole_chars_search) {
505 size_t i;
506 size_t len = strlen (option_whole_chars_search);
508 strcpy (option_whole_chars_search_buf,
509 option_whole_chars_search);
511 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
512 if (islower (i) && !strchr (option_whole_chars_search, i)) {
513 option_whole_chars_search_buf[len++] = i;
517 option_whole_chars_search_buf[len] = 0;
518 option_whole_chars_search = option_whole_chars_search_buf;
520 #endif /* ENABLE_NLS */
521 edit = g_malloc0 (sizeof (WEdit));
522 to_free = 1;
524 edit_purge_widget (edit);
525 edit->num_widget_lines = lines;
526 edit->num_widget_columns = columns;
527 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
528 edit->stat1.st_uid = getuid ();
529 edit->stat1.st_gid = getgid ();
530 edit->bracket = -1;
531 edit->force |= REDRAW_PAGE;
532 edit_set_filename (edit, filename);
533 edit->stack_size = START_STACK_SIZE;
534 edit->stack_size_mask = START_STACK_SIZE - 1;
535 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
536 if (edit_load_file (edit)) {
537 /* edit_load_file already gives an error message */
538 if (to_free)
539 g_free (edit);
540 return 0;
542 edit->loading_done = 1;
543 edit->modified = 0;
544 edit->locked = 0;
545 edit_load_syntax (edit, 0, 0);
547 int color;
548 edit_get_syntax_color (edit, -1, &color);
551 /* load saved cursor position */
552 if ((line == 0) && option_save_position) {
553 edit_load_position (edit);
554 } else {
555 if (line <= 0)
556 line = 1;
557 edit_move_display (edit, line - 1);
558 edit_move_to_line (edit, line - 1);
561 return edit;
564 /* Clear the edit struct, freeing everything in it. Return 1 on success */
566 edit_clean (WEdit *edit)
568 int j = 0;
570 if (!edit)
571 return 0;
573 /* a stale lock, remove it */
574 if (edit->locked)
575 edit->locked = edit_unlock_file (edit->filename);
577 /* save cursor position */
578 if (option_save_position)
579 edit_save_position (edit);
581 /* File specified on the mcedit command line and never saved */
582 if (edit->delete_file)
583 unlink (edit->filename);
585 edit_free_syntax_rules (edit);
586 book_mark_flush (edit, -1);
587 for (; j <= MAXBUFF; j++) {
588 g_free (edit->buffers1[j]);
589 g_free (edit->buffers2[j]);
592 g_free (edit->undo_stack);
593 g_free (edit->filename);
594 g_free (edit->dir);
596 edit_purge_widget (edit);
598 /* Free temporary strings used in catstrs() */
599 freestrs ();
601 return 1;
605 /* returns 1 on success */
606 int edit_renew (WEdit * edit)
608 int lines = edit->num_widget_lines;
609 int columns = edit->num_widget_columns;
610 int retval = 1;
612 edit_clean (edit);
613 if (!edit_init (edit, lines, columns, "", 0))
614 retval = 0;
615 return retval;
619 * Load a new file into the editor. If it fails, preserve the old file.
620 * To do it, allocate a new widget, initialize it and, if the new file
621 * was loaded, copy the data to the old widget.
622 * Return 1 on success, 0 on failure.
625 edit_reload (WEdit *edit, const char *filename)
627 WEdit *e;
628 int lines = edit->num_widget_lines;
629 int columns = edit->num_widget_columns;
631 e = g_malloc0 (sizeof (WEdit));
632 e->widget = edit->widget;
633 if (!edit_init (e, lines, columns, filename, 0)) {
634 g_free (e);
635 return 0;
637 edit_clean (edit);
638 memcpy (edit, e, sizeof (WEdit));
639 g_free (e);
640 return 1;
645 Recording stack for undo:
646 The following is an implementation of a compressed stack. Identical
647 pushes are recorded by a negative prefix indicating the number of times the
648 same char was pushed. This saves space for repeated curs-left or curs-right
649 delete etc.
653 pushed: stored:
657 b -3
659 c --> -4
665 If the stack long int is 0-255 it represents a normal insert (from a backspace),
666 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
667 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
668 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
669 position.
671 The only way the cursor moves or the buffer is changed is through the routines:
672 insert, backspace, insert_ahead, delete, and cursor_move.
673 These record the reverse undo movements onto the stack each time they are
674 called.
676 Each key press results in a set of actions (insert; delete ...). So each time
677 a key is pressed the current position of start_display is pushed as
678 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
679 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
680 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
684 void edit_push_action (WEdit * edit, long c,...)
686 unsigned long sp = edit->stack_pointer;
687 unsigned long spm1;
688 long *t;
689 /* first enlarge the stack if necessary */
690 if (sp > edit->stack_size - 10) { /* say */
691 if (option_max_undo < 256)
692 option_max_undo = 256;
693 if (edit->stack_size < (unsigned long) option_max_undo) {
694 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
695 if (t) {
696 edit->undo_stack = t;
697 edit->stack_size <<= 1;
698 edit->stack_size_mask = edit->stack_size - 1;
702 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
703 if (edit->stack_disable)
704 return;
706 #ifdef FAST_MOVE_CURSOR
707 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
708 va_list ap;
709 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
710 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
711 va_start (ap, c);
712 c = -(va_arg (ap, int));
713 va_end (ap);
714 } else
715 #endif /* ! FAST_MOVE_CURSOR */
716 if (edit->stack_bottom != sp
717 && spm1 != edit->stack_bottom
718 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
719 int d;
720 if (edit->undo_stack[spm1] < 0) {
721 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
722 if (d == c) {
723 if (edit->undo_stack[spm1] > -1000000000) {
724 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
725 edit->undo_stack[spm1]--;
726 return;
729 /* #define NO_STACK_CURSMOVE_ANIHILATION */
730 #ifndef NO_STACK_CURSMOVE_ANIHILATION
731 else if ((c == CURS_LEFT && d == CURS_RIGHT)
732 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
733 if (edit->undo_stack[spm1] == -2)
734 edit->stack_pointer = spm1;
735 else
736 edit->undo_stack[spm1]++;
737 return;
739 #endif
740 } else {
741 d = edit->undo_stack[spm1];
742 if (d == c) {
743 if (c >= KEY_PRESS)
744 return; /* --> no need to push multiple do-nothings */
745 edit->undo_stack[sp] = -2;
746 goto check_bottom;
748 #ifndef NO_STACK_CURSMOVE_ANIHILATION
749 else if ((c == CURS_LEFT && d == CURS_RIGHT)
750 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
751 edit->stack_pointer = spm1;
752 return;
754 #endif
757 edit->undo_stack[sp] = c;
758 check_bottom:
760 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
762 /* if the sp wraps round and catches the stack_bottom then erase
763 * the first set of actions on the stack to make space - by moving
764 * stack_bottom forward one "key press" */
765 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
766 if ((unsigned long) c == edit->stack_bottom ||
767 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
768 do {
769 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
770 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
772 /*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: */
773 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
774 edit->stack_bottom = edit->stack_pointer = 0;
778 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
779 then the file should be as it was when he loaded up. Then set edit->modified to 0.
781 static long
782 pop_action (WEdit * edit)
784 long c;
785 unsigned long sp = edit->stack_pointer;
786 if (sp == edit->stack_bottom) {
787 return STACK_BOTTOM;
789 sp = (sp - 1) & edit->stack_size_mask;
790 if ((c = edit->undo_stack[sp]) >= 0) {
791 /* edit->undo_stack[sp] = '@'; */
792 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
793 return c;
795 if (sp == edit->stack_bottom) {
796 return STACK_BOTTOM;
798 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
799 if (edit->undo_stack[sp] == -2) {
800 /* edit->undo_stack[sp] = '@'; */
801 edit->stack_pointer = sp;
802 } else
803 edit->undo_stack[sp]++;
805 return c;
808 /* is called whenever a modification is made by one of the four routines below */
809 static inline void edit_modification (WEdit * edit)
811 edit->caches_valid = 0;
812 edit->screen_modified = 1;
814 /* raise lock when file modified */
815 if (!edit->modified && !edit->delete_file)
816 edit->locked = edit_lock_file (edit->filename);
817 edit->modified = 1;
821 Basic low level single character buffer alterations and movements at the cursor.
822 Returns char passed over, inserted or removed.
825 void
826 edit_insert (WEdit *edit, int c)
828 /* check if file has grown to large */
829 if (edit->last_byte >= SIZE_LIMIT)
830 return;
832 /* first we must update the position of the display window */
833 if (edit->curs1 < edit->start_display) {
834 edit->start_display++;
835 if (c == '\n')
836 edit->start_line++;
839 /* Mark file as modified, unless the file hasn't been fully loaded */
840 if (edit->loading_done) {
841 edit_modification (edit);
844 /* now we must update some info on the file and check if a redraw is required */
845 if (c == '\n') {
846 if (edit->book_mark)
847 book_mark_inc (edit, edit->curs_line);
848 edit->curs_line++;
849 edit->total_lines++;
850 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
853 /* save the reverse command onto the undo stack */
854 edit_push_action (edit, BACKSPACE);
856 /* update markers */
857 edit->mark1 += (edit->mark1 > edit->curs1);
858 edit->mark2 += (edit->mark2 > edit->curs1);
859 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
861 /* add a new buffer if we've reached the end of the last one */
862 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
863 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
864 g_malloc (EDIT_BUF_SIZE);
866 /* perform the insertion */
867 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
868 curs1 & M_EDIT_BUF_SIZE]
869 = (unsigned char) c;
871 /* update file length */
872 edit->last_byte++;
874 /* update cursor position */
875 edit->curs1++;
879 /* same as edit_insert and move left */
880 void edit_insert_ahead (WEdit * edit, int c)
882 if (edit->last_byte >= SIZE_LIMIT)
883 return;
884 if (edit->curs1 < edit->start_display) {
885 edit->start_display++;
886 if (c == '\n')
887 edit->start_line++;
889 edit_modification (edit);
890 if (c == '\n') {
891 if (edit->book_mark)
892 book_mark_inc (edit, edit->curs_line);
893 edit->total_lines++;
894 edit->force |= REDRAW_AFTER_CURSOR;
896 edit_push_action (edit, DELCHAR);
898 edit->mark1 += (edit->mark1 >= edit->curs1);
899 edit->mark2 += (edit->mark2 >= edit->curs1);
900 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
902 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
903 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
904 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
906 edit->last_byte++;
907 edit->curs2++;
911 int edit_delete (WEdit * edit)
913 int p;
914 if (!edit->curs2)
915 return 0;
917 edit->mark1 -= (edit->mark1 > edit->curs1);
918 edit->mark2 -= (edit->mark2 > edit->curs1);
919 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
921 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
923 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
924 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
925 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
927 edit->last_byte--;
928 edit->curs2--;
930 edit_modification (edit);
931 if (p == '\n') {
932 if (edit->book_mark)
933 book_mark_dec (edit, edit->curs_line);
934 edit->total_lines--;
935 edit->force |= REDRAW_AFTER_CURSOR;
937 edit_push_action (edit, p + 256);
938 if (edit->curs1 < edit->start_display) {
939 edit->start_display--;
940 if (p == '\n')
941 edit->start_line--;
944 return p;
948 static int
949 edit_backspace (WEdit * edit)
951 int p;
952 if (!edit->curs1)
953 return 0;
955 edit->mark1 -= (edit->mark1 >= edit->curs1);
956 edit->mark2 -= (edit->mark2 >= edit->curs1);
957 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
959 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
960 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
961 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
962 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
964 edit->last_byte--;
965 edit->curs1--;
967 edit_modification (edit);
968 if (p == '\n') {
969 if (edit->book_mark)
970 book_mark_dec (edit, edit->curs_line);
971 edit->curs_line--;
972 edit->total_lines--;
973 edit->force |= REDRAW_AFTER_CURSOR;
975 edit_push_action (edit, p);
977 if (edit->curs1 < edit->start_display) {
978 edit->start_display--;
979 if (p == '\n')
980 edit->start_line--;
983 return p;
986 #ifdef FAST_MOVE_CURSOR
988 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
990 unsigned long next;
991 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
992 edit->curs_line--;
993 next -= (unsigned long) dest;
994 n -= next;
995 src += next;
996 dest += next;
1001 edit_move_backward_lots (WEdit *edit, long increment)
1003 int r, s, t;
1004 unsigned char *p;
1006 if (increment > edit->curs1)
1007 increment = edit->curs1;
1008 if (increment <= 0)
1009 return -1;
1010 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1012 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1013 if (r > increment)
1014 r = increment;
1015 s = edit->curs1 & M_EDIT_BUF_SIZE;
1017 p = 0;
1018 if (s > r) {
1019 memqcpy (edit,
1020 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1021 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1023 } else {
1024 if (s) {
1025 memqcpy (edit,
1026 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1027 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1028 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1029 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1031 memqcpy (edit,
1032 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1033 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1034 EDIT_BUF_SIZE - (r - s), r - s);
1036 increment -= r;
1037 edit->curs1 -= r;
1038 edit->curs2 += r;
1039 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1040 if (p)
1041 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1042 else
1043 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1044 g_malloc (EDIT_BUF_SIZE);
1045 } else {
1046 g_free (p);
1049 s = edit->curs1 & M_EDIT_BUF_SIZE;
1050 while (increment) {
1051 p = 0;
1052 r = EDIT_BUF_SIZE;
1053 if (r > increment)
1054 r = increment;
1055 t = s;
1056 if (r < t)
1057 t = r;
1058 memqcpy (edit,
1059 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1060 EDIT_BUF_SIZE - t,
1061 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1063 if (r >= s) {
1064 if (t) {
1065 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1066 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1068 memqcpy (edit,
1069 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1070 EDIT_BUF_SIZE - r,
1071 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1072 EDIT_BUF_SIZE - (r - s), r - s);
1074 increment -= r;
1075 edit->curs1 -= r;
1076 edit->curs2 += r;
1077 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1078 if (p)
1079 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1080 else
1081 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1082 g_malloc (EDIT_BUF_SIZE);
1083 } else {
1084 g_free (p);
1087 return edit_get_byte (edit, edit->curs1);
1090 #endif /* ! FAST_MOVE_CURSOR */
1092 /* moves the cursor right or left: increment positive or negative respectively */
1093 int edit_cursor_move (WEdit * edit, long increment)
1095 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1096 int c;
1098 #ifdef FAST_MOVE_CURSOR
1099 if (increment < -256) {
1100 edit->force |= REDRAW_PAGE;
1101 return edit_move_backward_lots (edit, -increment);
1103 #endif /* ! FAST_MOVE_CURSOR */
1105 if (increment < 0) {
1106 for (; increment < 0; increment++) {
1107 if (!edit->curs1)
1108 return -1;
1110 edit_push_action (edit, CURS_RIGHT);
1112 c = edit_get_byte (edit, edit->curs1 - 1);
1113 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1114 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1115 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1116 edit->curs2++;
1117 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1118 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1119 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1120 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1122 edit->curs1--;
1123 if (c == '\n') {
1124 edit->curs_line--;
1125 edit->force |= REDRAW_LINE_BELOW;
1129 return c;
1130 } else if (increment > 0) {
1131 for (; increment > 0; increment--) {
1132 if (!edit->curs2)
1133 return -2;
1135 edit_push_action (edit, CURS_LEFT);
1137 c = edit_get_byte (edit, edit->curs1);
1138 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1139 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1140 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1141 edit->curs1++;
1142 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1143 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1144 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1145 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1147 edit->curs2--;
1148 if (c == '\n') {
1149 edit->curs_line++;
1150 edit->force |= REDRAW_LINE_ABOVE;
1153 return c;
1154 } else
1155 return -3;
1158 /* These functions return positions relative to lines */
1160 /* returns index of last char on line + 1 */
1161 long edit_eol (WEdit * edit, long current)
1163 if (current < edit->last_byte) {
1164 for (;; current++)
1165 if (edit_get_byte (edit, current) == '\n')
1166 break;
1167 } else
1168 return edit->last_byte;
1169 return current;
1172 /* returns index of first char on line */
1173 long edit_bol (WEdit * edit, long current)
1175 if (current > 0) {
1176 for (;; current--)
1177 if (edit_get_byte (edit, current - 1) == '\n')
1178 break;
1179 } else
1180 return 0;
1181 return current;
1185 int edit_count_lines (WEdit * edit, long current, int upto)
1187 int lines = 0;
1188 if (upto > edit->last_byte)
1189 upto = edit->last_byte;
1190 if (current < 0)
1191 current = 0;
1192 while (current < upto)
1193 if (edit_get_byte (edit, current++) == '\n')
1194 lines++;
1195 return lines;
1199 /* If lines is zero this returns the count of lines from current to upto. */
1200 /* If upto is zero returns index of lines forward current. */
1201 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1203 if (upto) {
1204 return edit_count_lines (edit, current, upto);
1205 } else {
1206 int next;
1207 if (lines < 0)
1208 lines = 0;
1209 while (lines--) {
1210 next = edit_eol (edit, current) + 1;
1211 if (next > edit->last_byte)
1212 break;
1213 else
1214 current = next;
1216 return current;
1221 /* Returns offset of 'lines' lines up from current */
1222 long edit_move_backward (WEdit * edit, long current, int lines)
1224 if (lines < 0)
1225 lines = 0;
1226 current = edit_bol (edit, current);
1227 while((lines--) && current != 0)
1228 current = edit_bol (edit, current - 1);
1229 return current;
1232 /* If cols is zero this returns the count of columns from current to upto. */
1233 /* If upto is zero returns index of cols across from current. */
1234 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1236 long p, q;
1237 int col = 0;
1239 if (upto) {
1240 q = upto;
1241 cols = -10;
1242 } else
1243 q = edit->last_byte + 2;
1245 for (col = 0, p = current; p < q; p++) {
1246 int c;
1247 if (cols != -10) {
1248 if (col == cols)
1249 return p;
1250 if (col > cols)
1251 return p - 1;
1253 c = edit_get_byte (edit, p);
1254 if (c == '\t')
1255 col += TAB_SIZE - col % TAB_SIZE;
1256 else if (c == '\n') {
1257 if (upto)
1258 return col;
1259 else
1260 return p;
1261 } else if (c < 32 || c == 127)
1262 col += 2; /* Caret notation for control characters */
1263 else
1264 col++;
1266 return col;
1269 /* returns the current column position of the cursor */
1270 int edit_get_col (WEdit * edit)
1272 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1276 /* Scrolling functions */
1278 void edit_update_curs_row (WEdit * edit)
1280 edit->curs_row = edit->curs_line - edit->start_line;
1283 void edit_update_curs_col (WEdit * edit)
1285 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1288 /*moves the display start position up by i lines */
1289 void edit_scroll_upward (WEdit * edit, unsigned long i)
1291 unsigned long lines_above = edit->start_line;
1292 if (i > lines_above)
1293 i = lines_above;
1294 if (i) {
1295 edit->start_line -= i;
1296 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1297 edit->force |= REDRAW_PAGE;
1298 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1300 edit_update_curs_row (edit);
1304 /* returns 1 if could scroll, 0 otherwise */
1305 void edit_scroll_downward (WEdit * edit, int i)
1307 int lines_below;
1308 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1309 if (lines_below > 0) {
1310 if (i > lines_below)
1311 i = lines_below;
1312 edit->start_line += i;
1313 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1314 edit->force |= REDRAW_PAGE;
1315 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1317 edit_update_curs_row (edit);
1320 void edit_scroll_right (WEdit * edit, int i)
1322 edit->force |= REDRAW_PAGE;
1323 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1324 edit->start_col -= i;
1327 void edit_scroll_left (WEdit * edit, int i)
1329 if (edit->start_col) {
1330 edit->start_col += i;
1331 if (edit->start_col > 0)
1332 edit->start_col = 0;
1333 edit->force |= REDRAW_PAGE;
1334 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1338 /* high level cursor movement commands */
1340 static int is_in_indent (WEdit *edit)
1342 long p = edit_bol (edit, edit->curs1);
1343 while (p < edit->curs1)
1344 if (!strchr (" \t", edit_get_byte (edit, p++)))
1345 return 0;
1346 return 1;
1349 static int left_of_four_spaces (WEdit *edit);
1351 static void
1352 edit_move_to_prev_col (WEdit * edit, long p)
1354 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1356 if (is_in_indent (edit) && option_fake_half_tabs) {
1357 edit_update_curs_col (edit);
1358 if (space_width)
1359 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1360 int q = edit->curs_col;
1361 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1362 p = edit_bol (edit, edit->curs1);
1363 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1364 if (!left_of_four_spaces (edit))
1365 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1371 /* move i lines */
1372 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1374 unsigned long p, l = edit->curs_line;
1376 if (i > l)
1377 i = l;
1378 if (i) {
1379 if (i > 1)
1380 edit->force |= REDRAW_PAGE;
1381 if (scroll)
1382 edit_scroll_upward (edit, i);
1384 p = edit_bol (edit, edit->curs1);
1385 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1386 edit_move_to_prev_col (edit, p);
1388 edit->search_start = edit->curs1;
1389 edit->found_len = 0;
1393 static int
1394 is_blank (WEdit *edit, long offset)
1396 long s, f;
1397 int c;
1398 s = edit_bol (edit, offset);
1399 f = edit_eol (edit, offset) - 1;
1400 while (s <= f) {
1401 c = edit_get_byte (edit, s++);
1402 if (!isspace (c))
1403 return 0;
1405 return 1;
1409 /* returns the offset of line i */
1410 static long
1411 edit_find_line (WEdit *edit, int line)
1413 int i, j = 0;
1414 int m = 2000000000;
1415 if (!edit->caches_valid) {
1416 for (i = 0; i < N_LINE_CACHES; i++)
1417 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1418 /* three offsets that we *know* are line 0 at 0 and these two: */
1419 edit->line_numbers[1] = edit->curs_line;
1420 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1421 edit->line_numbers[2] = edit->total_lines;
1422 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1423 edit->caches_valid = 1;
1425 if (line >= edit->total_lines)
1426 return edit->line_offsets[2];
1427 if (line <= 0)
1428 return 0;
1429 /* find the closest known point */
1430 for (i = 0; i < N_LINE_CACHES; i++) {
1431 int n;
1432 n = abs (edit->line_numbers[i] - line);
1433 if (n < m) {
1434 m = n;
1435 j = i;
1438 if (m == 0)
1439 return edit->line_offsets[j]; /* know the offset exactly */
1440 if (m == 1 && j >= 3)
1441 i = j; /* one line different - caller might be looping, so stay in this cache */
1442 else
1443 i = 3 + (rand () % (N_LINE_CACHES - 3));
1444 if (line > edit->line_numbers[j])
1445 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1446 else
1447 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1448 edit->line_numbers[i] = line;
1449 return edit->line_offsets[i];
1452 int line_is_blank (WEdit * edit, long line)
1454 return is_blank (edit, edit_find_line (edit, line));
1457 /* moves up until a blank line is reached, or until just
1458 before a non-blank line is reached */
1459 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1461 int i;
1462 if (edit->curs_line <= 1) {
1463 i = 0;
1464 } else {
1465 if (line_is_blank (edit, edit->curs_line)) {
1466 if (line_is_blank (edit, edit->curs_line - 1)) {
1467 for (i = edit->curs_line - 1; i; i--)
1468 if (!line_is_blank (edit, i)) {
1469 i++;
1470 break;
1472 } else {
1473 for (i = edit->curs_line - 1; i; i--)
1474 if (line_is_blank (edit, i))
1475 break;
1477 } else {
1478 for (i = edit->curs_line - 1; i; i--)
1479 if (line_is_blank (edit, i))
1480 break;
1483 edit_move_up (edit, edit->curs_line - i, scroll);
1486 /* move i lines */
1487 void edit_move_down (WEdit * edit, int i, int scroll)
1489 long p, l = edit->total_lines - edit->curs_line;
1491 if (i > l)
1492 i = l;
1493 if (i) {
1494 if (i > 1)
1495 edit->force |= REDRAW_PAGE;
1496 if (scroll)
1497 edit_scroll_downward (edit, i);
1498 p = edit_bol (edit, edit->curs1);
1499 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1500 edit_move_to_prev_col (edit, p);
1502 edit->search_start = edit->curs1;
1503 edit->found_len = 0;
1507 /* moves down until a blank line is reached, or until just
1508 before a non-blank line is reached */
1509 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1511 int i;
1512 if (edit->curs_line >= edit->total_lines - 1) {
1513 i = edit->total_lines;
1514 } else {
1515 if (line_is_blank (edit, edit->curs_line)) {
1516 if (line_is_blank (edit, edit->curs_line + 1)) {
1517 for (i = edit->curs_line + 1; i; i++)
1518 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1519 i--;
1520 break;
1522 } else {
1523 for (i = edit->curs_line + 1; i; i++)
1524 if (line_is_blank (edit, i) || i >= edit->total_lines)
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;
1533 edit_move_down (edit, i - edit->curs_line, scroll);
1536 static void edit_begin_page (WEdit *edit)
1538 edit_update_curs_row (edit);
1539 edit_move_up (edit, edit->curs_row, 0);
1542 static void edit_end_page (WEdit *edit)
1544 edit_update_curs_row (edit);
1545 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1549 /* goto beginning of text */
1550 static void edit_move_to_top (WEdit * edit)
1552 if (edit->curs_line) {
1553 edit_cursor_move (edit, -edit->curs1);
1554 edit_move_to_prev_col (edit, 0);
1555 edit->force |= REDRAW_PAGE;
1556 edit->search_start = 0;
1557 edit_update_curs_row(edit);
1562 /* goto end of text */
1563 static void edit_move_to_bottom (WEdit * edit)
1565 if (edit->curs_line < edit->total_lines) {
1566 edit_cursor_move (edit, edit->curs2);
1567 edit->start_display = edit->last_byte;
1568 edit->start_line = edit->total_lines;
1569 edit_update_curs_row(edit);
1570 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1571 edit->force |= REDRAW_PAGE;
1575 /* goto beginning of line */
1576 static void edit_cursor_to_bol (WEdit * edit)
1578 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1579 edit->search_start = edit->curs1;
1580 edit->prev_col = edit_get_col (edit);
1583 /* goto end of line */
1584 static void edit_cursor_to_eol (WEdit * edit)
1586 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1587 edit->search_start = edit->curs1;
1588 edit->prev_col = edit_get_col (edit);
1591 /* move cursor to line 'line' */
1592 void edit_move_to_line (WEdit * e, long line)
1594 if(line < e->curs_line)
1595 edit_move_up (e, e->curs_line - line, 0);
1596 else
1597 edit_move_down (e, line - e->curs_line, 0);
1598 edit_scroll_screen_over_cursor (e);
1601 /* scroll window so that first visible line is 'line' */
1602 void edit_move_display (WEdit * e, long line)
1604 if(line < e->start_line)
1605 edit_scroll_upward (e, e->start_line - line);
1606 else
1607 edit_scroll_downward (e, line - e->start_line);
1610 /* save markers onto undo stack */
1611 void edit_push_markers (WEdit * edit)
1613 edit_push_action (edit, MARK_1 + edit->mark1);
1614 edit_push_action (edit, MARK_2 + edit->mark2);
1617 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1619 edit->mark1 = m1;
1620 edit->mark2 = m2;
1621 edit->column1 = c1;
1622 edit->column2 = c2;
1626 /* highlight marker toggle */
1627 void edit_mark_cmd (WEdit * edit, int unmark)
1629 edit_push_markers (edit);
1630 if (unmark) {
1631 edit_set_markers (edit, 0, 0, 0, 0);
1632 edit->force |= REDRAW_PAGE;
1633 } else {
1634 if (edit->mark2 >= 0) {
1635 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1636 edit->force |= REDRAW_PAGE;
1637 } else
1638 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1642 static unsigned long
1643 my_type_of (int c)
1645 int x, r = 0;
1646 const char *p, *q;
1647 const char option_chars_move_whole_word[] =
1648 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1650 if (!c)
1651 return 0;
1652 if (c == '!') {
1653 if (*option_chars_move_whole_word == '!')
1654 return 2;
1655 return 0x80000000UL;
1657 if (isupper (c))
1658 c = 'A';
1659 else if (islower (c))
1660 c = 'a';
1661 else if (isalpha (c))
1662 c = 'a';
1663 else if (isdigit (c))
1664 c = '0';
1665 else if (isspace (c))
1666 c = ' ';
1667 q = strchr (option_chars_move_whole_word, c);
1668 if (!q)
1669 return 0xFFFFFFFFUL;
1670 do {
1671 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1672 if (*p == '!')
1673 x <<= 1;
1674 r |= x;
1675 } while ((q = strchr (q + 1, c)));
1676 return r;
1679 static void
1680 edit_left_word_move (WEdit *edit, int s)
1682 for (;;) {
1683 int c1, c2;
1684 edit_cursor_move (edit, -1);
1685 if (!edit->curs1)
1686 break;
1687 c1 = edit_get_byte (edit, edit->curs1 - 1);
1688 c2 = edit_get_byte (edit, edit->curs1);
1689 if (!(my_type_of (c1) & my_type_of (c2)))
1690 break;
1691 if (isspace (c1) && !isspace (c2))
1692 break;
1693 if (s)
1694 if (!isspace (c1) && isspace (c2))
1695 break;
1699 static void edit_left_word_move_cmd (WEdit * edit)
1701 edit_left_word_move (edit, 0);
1702 edit->force |= REDRAW_PAGE;
1705 static void
1706 edit_right_word_move (WEdit *edit, int s)
1708 for (;;) {
1709 int c1, c2;
1710 edit_cursor_move (edit, 1);
1711 if (edit->curs1 >= edit->last_byte)
1712 break;
1713 c1 = edit_get_byte (edit, edit->curs1 - 1);
1714 c2 = edit_get_byte (edit, edit->curs1);
1715 if (!(my_type_of (c1) & my_type_of (c2)))
1716 break;
1717 if (isspace (c1) && !isspace (c2))
1718 break;
1719 if (s)
1720 if (!isspace (c1) && isspace (c2))
1721 break;
1725 static void edit_right_word_move_cmd (WEdit * edit)
1727 edit_right_word_move (edit, 0);
1728 edit->force |= REDRAW_PAGE;
1732 static void edit_right_delete_word (WEdit * edit)
1734 int c1, c2;
1735 for (;;) {
1736 if (edit->curs1 >= edit->last_byte)
1737 break;
1738 c1 = edit_delete (edit);
1739 c2 = edit_get_byte (edit, edit->curs1);
1740 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1741 break;
1742 if (!(my_type_of (c1) & my_type_of (c2)))
1743 break;
1747 static void edit_left_delete_word (WEdit * edit)
1749 int c1, c2;
1750 for (;;) {
1751 if (edit->curs1 <= 0)
1752 break;
1753 c1 = edit_backspace (edit);
1754 c2 = edit_get_byte (edit, edit->curs1 - 1);
1755 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1756 break;
1757 if (!(my_type_of (c1) & my_type_of (c2)))
1758 break;
1763 the start column position is not recorded, and hence does not
1764 undo as it happed. But who would notice.
1766 static void
1767 edit_do_undo (WEdit * edit)
1769 long ac;
1770 long count = 0;
1772 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1774 while ((ac = pop_action (edit)) < KEY_PRESS) {
1775 switch ((int) ac) {
1776 case STACK_BOTTOM:
1777 goto done_undo;
1778 case CURS_RIGHT:
1779 edit_cursor_move (edit, 1);
1780 break;
1781 case CURS_LEFT:
1782 edit_cursor_move (edit, -1);
1783 break;
1784 case BACKSPACE:
1785 edit_backspace (edit);
1786 break;
1787 case DELCHAR:
1788 edit_delete (edit);
1789 break;
1790 case COLUMN_ON:
1791 column_highlighting = 1;
1792 break;
1793 case COLUMN_OFF:
1794 column_highlighting = 0;
1795 break;
1797 if (ac >= 256 && ac < 512)
1798 edit_insert_ahead (edit, ac - 256);
1799 if (ac >= 0 && ac < 256)
1800 edit_insert (edit, ac);
1802 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1803 edit->mark1 = ac - MARK_1;
1804 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1805 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1806 edit->mark2 = ac - MARK_2;
1807 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1809 if (count++)
1810 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1813 if (edit->start_display > ac - KEY_PRESS) {
1814 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1815 edit->force |= REDRAW_PAGE;
1816 } else if (edit->start_display < ac - KEY_PRESS) {
1817 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1818 edit->force |= REDRAW_PAGE;
1820 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1821 edit_update_curs_row (edit);
1823 done_undo:;
1824 edit->stack_disable = 0;
1827 static void edit_delete_to_line_end (WEdit * edit)
1829 while (edit_get_byte (edit, edit->curs1) != '\n') {
1830 if (!edit->curs2)
1831 break;
1832 edit_delete (edit);
1836 static void edit_delete_to_line_begin (WEdit * edit)
1838 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1839 if (!edit->curs1)
1840 break;
1841 edit_backspace (edit);
1845 void
1846 edit_delete_line (WEdit *edit)
1849 * Delete right part of the line.
1850 * Note that edit_get_byte() returns '\n' when byte position is
1851 * beyond EOF.
1853 while (edit_get_byte (edit, edit->curs1) != '\n') {
1854 (void) edit_delete (edit);
1858 * Delete '\n' char.
1859 * Note that edit_delete() will not corrupt anything if called while
1860 * cursor position is EOF.
1862 (void) edit_delete (edit);
1865 * Delete left part of the line.
1866 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1868 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1869 (void) edit_backspace (edit);
1873 static void insert_spaces_tab (WEdit * edit, int half)
1875 int i;
1876 edit_update_curs_col (edit);
1877 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1878 while (i > 0) {
1879 edit_insert (edit, ' ');
1880 i -= space_width;
1884 static int is_aligned_on_a_tab (WEdit * edit)
1886 edit_update_curs_col (edit);
1887 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1888 return 0; /* not alligned on a tab */
1889 return 1;
1892 static int right_of_four_spaces (WEdit *edit)
1894 int i, ch = 0;
1895 for (i = 1; i <= HALF_TAB_SIZE; i++)
1896 ch |= edit_get_byte (edit, edit->curs1 - i);
1897 if (ch == ' ')
1898 return is_aligned_on_a_tab (edit);
1899 return 0;
1902 static int left_of_four_spaces (WEdit *edit)
1904 int i, ch = 0;
1905 for (i = 0; i < HALF_TAB_SIZE; i++)
1906 ch |= edit_get_byte (edit, edit->curs1 + i);
1907 if (ch == ' ')
1908 return is_aligned_on_a_tab (edit);
1909 return 0;
1912 int edit_indent_width (WEdit * edit, long p)
1914 long q = p;
1915 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1916 q++;
1917 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1920 void edit_insert_indent (WEdit * edit, int indent)
1922 if (!option_fill_tabs_with_spaces) {
1923 while (indent >= TAB_SIZE) {
1924 edit_insert (edit, '\t');
1925 indent -= TAB_SIZE;
1928 while (indent-- > 0)
1929 edit_insert (edit, ' ');
1932 static void
1933 edit_auto_indent (WEdit * edit, int extra, int no_advance)
1935 long p;
1936 int indent;
1937 p = edit->curs1;
1938 while (isspace (edit_get_byte (edit, p - 1)) && p > 0) /* move back/up to a line with text */
1939 p--;
1940 indent = edit_indent_width (edit, edit_bol (edit, p));
1941 if (edit->curs_col < indent && no_advance)
1942 indent = edit->curs_col;
1943 edit_insert_indent (edit, indent + (option_fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE) * space_width * extra);
1946 static void edit_double_newline (WEdit * edit)
1948 edit_insert (edit, '\n');
1949 if (edit_get_byte (edit, edit->curs1) == '\n')
1950 return;
1951 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1952 return;
1953 edit->force |= REDRAW_PAGE;
1954 edit_insert (edit, '\n');
1957 static void edit_tab_cmd (WEdit * edit)
1959 int i;
1961 if (option_fake_half_tabs) {
1962 if (is_in_indent (edit)) {
1963 /*insert a half tab (usually four spaces) unless there is a
1964 half tab already behind, then delete it and insert a
1965 full tab. */
1966 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1967 for (i = 1; i <= HALF_TAB_SIZE; i++)
1968 edit_backspace (edit);
1969 edit_insert (edit, '\t');
1970 } else {
1971 insert_spaces_tab (edit, 1);
1973 return;
1976 if (option_fill_tabs_with_spaces) {
1977 insert_spaces_tab (edit, 0);
1978 } else {
1979 edit_insert (edit, '\t');
1981 return;
1984 static void check_and_wrap_line (WEdit * edit)
1986 int curs, c;
1987 if (!option_typewriter_wrap)
1988 return;
1989 edit_update_curs_col (edit);
1990 if (edit->curs_col < option_word_wrap_line_length)
1991 return;
1992 curs = edit->curs1;
1993 for (;;) {
1994 curs--;
1995 c = edit_get_byte (edit, curs);
1996 if (c == '\n' || curs <= 0) {
1997 edit_insert (edit, '\n');
1998 return;
2000 if (c == ' ' || c == '\t') {
2001 int current = edit->curs1;
2002 edit_cursor_move (edit, curs - edit->curs1 + 1);
2003 edit_insert (edit, '\n');
2004 edit_cursor_move (edit, current - edit->curs1 + 1);
2005 return;
2010 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2012 void edit_push_key_press (WEdit * edit)
2014 edit_push_action (edit, KEY_PRESS + edit->start_display);
2015 if (edit->mark2 == -1)
2016 edit_push_action (edit, MARK_1 + edit->mark1);
2019 /* this find the matching bracket in either direction, and sets edit->bracket */
2020 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2022 const char * const b = "{}{[][()(", *p;
2023 int i = 1, a, inc = -1, c, d, n = 0;
2024 unsigned long j = 0;
2025 long q;
2026 edit_update_curs_row (edit);
2027 c = edit_get_byte (edit, edit->curs1);
2028 p = strchr (b, c);
2029 /* no limit */
2030 if (!furthest_bracket_search)
2031 furthest_bracket_search--;
2032 /* not on a bracket at all */
2033 if (!p)
2034 return -1;
2035 /* the matching bracket */
2036 d = p[1];
2037 /* going left or right? */
2038 if (strchr ("{[(", c))
2039 inc = 1;
2040 for (q = edit->curs1 + inc;; q += inc) {
2041 /* out of buffer? */
2042 if (q >= edit->last_byte || q < 0)
2043 break;
2044 a = edit_get_byte (edit, q);
2045 /* don't want to eat CPU */
2046 if (j++ > furthest_bracket_search)
2047 break;
2048 /* out of screen? */
2049 if (in_screen) {
2050 if (q < edit->start_display)
2051 break;
2052 /* count lines if searching downward */
2053 if (inc > 0 && a == '\n')
2054 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2055 break;
2057 /* count bracket depth */
2058 i += (a == c) - (a == d);
2059 /* return if bracket depth is zero */
2060 if (!i)
2061 return q;
2063 /* no match */
2064 return -1;
2067 static long last_bracket = -1;
2069 static void edit_find_bracket (WEdit * edit)
2071 edit->bracket = edit_get_bracket (edit, 1, 10000);
2072 if (last_bracket != edit->bracket)
2073 edit->force |= REDRAW_PAGE;
2074 last_bracket = edit->bracket;
2077 static void edit_goto_matching_bracket (WEdit *edit)
2079 long q;
2080 q = edit_get_bracket (edit, 0, 0);
2081 if (q < 0)
2082 return;
2083 edit->bracket = edit->curs1;
2084 edit->force |= REDRAW_PAGE;
2085 edit_cursor_move (edit, q - edit->curs1);
2089 * This executes a command as though the user initiated it through a key
2090 * press. Callback with WIDGET_KEY as a message calls this after
2091 * translating the key press. This function can be used to pass any
2092 * command to the editor. Note that the screen wouldn't update
2093 * automatically. Either of command or char_for_insertion must be
2094 * passed as -1. Commands are executed, and char_for_insertion is
2095 * inserted at the cursor.
2097 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2099 if (command == CK_Begin_Record_Macro) {
2100 edit->macro_i = 0;
2101 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2102 return;
2104 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2105 edit->force |= REDRAW_COMPLETELY;
2106 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2107 edit->macro_i = -1;
2108 return;
2110 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2111 edit->macro[edit->macro_i].command = command;
2112 edit->macro[edit->macro_i++].ch = char_for_insertion;
2114 /* record the beginning of a set of editing actions initiated by a key press */
2115 if (command != CK_Undo)
2116 edit_push_key_press (edit);
2118 edit_execute_cmd (edit, command, char_for_insertion);
2119 if (column_highlighting)
2120 edit->force |= REDRAW_PAGE;
2123 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2126 This executes a command at a lower level than macro recording.
2127 It also does not push a key_press onto the undo stack. This means
2128 that if it is called many times, a single undo command will undo
2129 all of them. It also does not check for the Undo command.
2131 void
2132 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2134 edit->force |= REDRAW_LINE;
2136 /* The next key press will unhighlight the found string, so update
2137 * the whole page */
2138 if (edit->found_len || column_highlighting)
2139 edit->force |= REDRAW_PAGE;
2141 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2142 column_highlighting = 0;
2143 if (!edit->highlight
2144 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2145 edit_mark_cmd (edit, 1); /* clear */
2146 edit_mark_cmd (edit, 0); /* marking on */
2148 edit->highlight = 1;
2149 } else { /* any other command */
2150 if (edit->highlight)
2151 edit_mark_cmd (edit, 0); /* clear */
2152 edit->highlight = 0;
2155 /* first check for undo */
2156 if (command == CK_Undo) {
2157 edit_do_undo (edit);
2158 edit->found_len = 0;
2159 edit->prev_col = edit_get_col (edit);
2160 edit->search_start = edit->curs1;
2161 return;
2164 /* An ordinary key press */
2165 if (char_for_insertion >= 0) {
2166 if (edit->overwrite) {
2167 if (edit_get_byte (edit, edit->curs1) != '\n')
2168 edit_delete (edit);
2170 edit_insert (edit, char_for_insertion);
2171 if (option_auto_para_formatting) {
2172 format_paragraph (edit, 0);
2173 edit->force |= REDRAW_PAGE;
2174 } else
2175 check_and_wrap_line (edit);
2176 edit->found_len = 0;
2177 edit->prev_col = edit_get_col (edit);
2178 edit->search_start = edit->curs1;
2179 edit_find_bracket (edit);
2180 return;
2182 switch (command) {
2183 case CK_Begin_Page:
2184 case CK_End_Page:
2185 case CK_Begin_Page_Highlight:
2186 case CK_End_Page_Highlight:
2187 case CK_Word_Left:
2188 case CK_Word_Right:
2189 case CK_Up:
2190 case CK_Down:
2191 case CK_Word_Left_Highlight:
2192 case CK_Word_Right_Highlight:
2193 case CK_Up_Highlight:
2194 case CK_Down_Highlight:
2195 if (edit->mark2 == -1)
2196 break; /*marking is following the cursor: may need to highlight a whole line */
2197 case CK_Left:
2198 case CK_Right:
2199 case CK_Left_Highlight:
2200 case CK_Right_Highlight:
2201 edit->force |= REDRAW_CHAR_ONLY;
2204 /* basic cursor key commands */
2205 switch (command) {
2206 case CK_BackSpace:
2207 if (option_backspace_through_tabs && is_in_indent (edit)) {
2208 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2209 && edit->curs1 > 0)
2210 edit_backspace (edit);
2211 break;
2212 } else {
2213 if (option_fake_half_tabs) {
2214 int i;
2215 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2216 for (i = 0; i < HALF_TAB_SIZE; i++)
2217 edit_backspace (edit);
2218 break;
2222 edit_backspace (edit);
2223 break;
2224 case CK_Delete:
2225 if (option_fake_half_tabs) {
2226 int i;
2227 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2228 for (i = 1; i <= HALF_TAB_SIZE; i++)
2229 edit_delete (edit);
2230 break;
2233 edit_delete (edit);
2234 break;
2235 case CK_Delete_Word_Left:
2236 edit_left_delete_word (edit);
2237 break;
2238 case CK_Delete_Word_Right:
2239 edit_right_delete_word (edit);
2240 break;
2241 case CK_Delete_Line:
2242 edit_delete_line (edit);
2243 break;
2244 case CK_Delete_To_Line_End:
2245 edit_delete_to_line_end (edit);
2246 break;
2247 case CK_Delete_To_Line_Begin:
2248 edit_delete_to_line_begin (edit);
2249 break;
2250 case CK_Enter:
2251 if (option_auto_para_formatting) {
2252 edit_double_newline (edit);
2253 if (option_return_does_auto_indent)
2254 edit_auto_indent (edit, 0, 1);
2255 format_paragraph (edit, 0);
2256 } else {
2257 edit_insert (edit, '\n');
2258 if (option_return_does_auto_indent) {
2259 edit_auto_indent (edit, 0, 1);
2262 break;
2263 case CK_Return:
2264 edit_insert (edit, '\n');
2265 break;
2267 case CK_Page_Up:
2268 case CK_Page_Up_Highlight:
2269 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2270 break;
2271 case CK_Page_Down:
2272 case CK_Page_Down_Highlight:
2273 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2274 break;
2275 case CK_Left:
2276 case CK_Left_Highlight:
2277 if (option_fake_half_tabs) {
2278 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2279 edit_cursor_move (edit, -HALF_TAB_SIZE);
2280 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2281 break;
2284 edit_cursor_move (edit, -1);
2285 break;
2286 case CK_Right:
2287 case CK_Right_Highlight:
2288 if (option_fake_half_tabs) {
2289 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2290 edit_cursor_move (edit, HALF_TAB_SIZE);
2291 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2292 break;
2295 edit_cursor_move (edit, 1);
2296 break;
2297 case CK_Begin_Page:
2298 case CK_Begin_Page_Highlight:
2299 edit_begin_page (edit);
2300 break;
2301 case CK_End_Page:
2302 case CK_End_Page_Highlight:
2303 edit_end_page (edit);
2304 break;
2305 case CK_Word_Left:
2306 case CK_Word_Left_Highlight:
2307 edit_left_word_move_cmd (edit);
2308 break;
2309 case CK_Word_Right:
2310 case CK_Word_Right_Highlight:
2311 edit_right_word_move_cmd (edit);
2312 break;
2313 case CK_Up:
2314 case CK_Up_Highlight:
2315 edit_move_up (edit, 1, 0);
2316 break;
2317 case CK_Down:
2318 case CK_Down_Highlight:
2319 edit_move_down (edit, 1, 0);
2320 break;
2321 case CK_Paragraph_Up:
2322 case CK_Paragraph_Up_Highlight:
2323 edit_move_up_paragraph (edit, 0);
2324 break;
2325 case CK_Paragraph_Down:
2326 case CK_Paragraph_Down_Highlight:
2327 edit_move_down_paragraph (edit, 0);
2328 break;
2329 case CK_Scroll_Up:
2330 case CK_Scroll_Up_Highlight:
2331 edit_move_up (edit, 1, 1);
2332 break;
2333 case CK_Scroll_Down:
2334 case CK_Scroll_Down_Highlight:
2335 edit_move_down (edit, 1, 1);
2336 break;
2337 case CK_Home:
2338 case CK_Home_Highlight:
2339 edit_cursor_to_bol (edit);
2340 break;
2341 case CK_End:
2342 case CK_End_Highlight:
2343 edit_cursor_to_eol (edit);
2344 break;
2346 case CK_Tab:
2347 edit_tab_cmd (edit);
2348 if (option_auto_para_formatting) {
2349 format_paragraph (edit, 0);
2350 edit->force |= REDRAW_PAGE;
2351 } else
2352 check_and_wrap_line (edit);
2353 break;
2355 case CK_Toggle_Insert:
2356 edit->overwrite = (edit->overwrite == 0);
2357 break;
2359 case CK_Mark:
2360 if (edit->mark2 >= 0) {
2361 if (column_highlighting)
2362 edit_push_action (edit, COLUMN_ON);
2363 column_highlighting = 0;
2365 edit_mark_cmd (edit, 0);
2366 break;
2367 case CK_Column_Mark:
2368 if (!column_highlighting)
2369 edit_push_action (edit, COLUMN_OFF);
2370 column_highlighting = 1;
2371 edit_mark_cmd (edit, 0);
2372 break;
2373 case CK_Unmark:
2374 if (column_highlighting)
2375 edit_push_action (edit, COLUMN_ON);
2376 column_highlighting = 0;
2377 edit_mark_cmd (edit, 1);
2378 break;
2380 case CK_Toggle_Bookmark:
2381 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2382 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2383 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2384 else
2385 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2386 break;
2387 case CK_Flush_Bookmarks:
2388 book_mark_flush (edit, BOOK_MARK_COLOR);
2389 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2390 edit->force |= REDRAW_PAGE;
2391 break;
2392 case CK_Next_Bookmark:
2393 if (edit->book_mark) {
2394 struct _book_mark *p;
2395 p = (struct _book_mark *) book_mark_find (edit,
2396 edit->curs_line);
2397 if (p->next) {
2398 p = p->next;
2399 if (p->line >= edit->start_line + edit->num_widget_lines
2400 || p->line < edit->start_line)
2401 edit_move_display (edit,
2402 p->line -
2403 edit->num_widget_lines / 2);
2404 edit_move_to_line (edit, p->line);
2407 break;
2408 case CK_Prev_Bookmark:
2409 if (edit->book_mark) {
2410 struct _book_mark *p;
2411 p = (struct _book_mark *) book_mark_find (edit,
2412 edit->curs_line);
2413 while (p->line == edit->curs_line)
2414 if (p->prev)
2415 p = p->prev;
2416 if (p->line >= 0) {
2417 if (p->line >= edit->start_line + edit->num_widget_lines
2418 || p->line < edit->start_line)
2419 edit_move_display (edit,
2420 p->line -
2421 edit->num_widget_lines / 2);
2422 edit_move_to_line (edit, p->line);
2425 break;
2427 case CK_Beginning_Of_Text:
2428 case CK_Beginning_Of_Text_Highlight:
2429 edit_move_to_top (edit);
2430 break;
2431 case CK_End_Of_Text:
2432 case CK_End_Of_Text_Highlight:
2433 edit_move_to_bottom (edit);
2434 break;
2436 case CK_Copy:
2437 edit_block_copy_cmd (edit);
2438 break;
2439 case CK_Remove:
2440 edit_block_delete_cmd (edit);
2441 break;
2442 case CK_Move:
2443 edit_block_move_cmd (edit);
2444 break;
2446 case CK_XStore:
2447 edit_copy_to_X_buf_cmd (edit);
2448 break;
2449 case CK_XCut:
2450 edit_cut_to_X_buf_cmd (edit);
2451 break;
2452 case CK_XPaste:
2453 edit_paste_from_X_buf_cmd (edit);
2454 break;
2455 case CK_Selection_History:
2456 edit_paste_from_history (edit);
2457 break;
2459 case CK_Save_As:
2460 edit_save_as_cmd (edit);
2461 break;
2462 case CK_Save:
2463 edit_save_confirm_cmd (edit);
2464 break;
2465 case CK_Load:
2466 edit_load_cmd (edit);
2467 break;
2468 case CK_Save_Block:
2469 edit_save_block_cmd (edit);
2470 break;
2471 case CK_Insert_File:
2472 edit_insert_file_cmd (edit);
2473 break;
2475 case CK_Find:
2476 edit_search_cmd (edit, 0);
2477 break;
2478 case CK_Find_Again:
2479 edit_search_cmd (edit, 1);
2480 break;
2481 case CK_Replace:
2482 edit_replace_cmd (edit, 0);
2483 break;
2484 case CK_Replace_Again:
2485 edit_replace_cmd (edit, 1);
2486 break;
2487 case CK_Complete_Word:
2488 edit_complete_word_cmd (edit);
2489 break;
2491 case CK_Exit:
2492 dlg_stop (edit->widget.parent);
2493 break;
2494 case CK_New:
2495 edit_new_cmd (edit);
2496 break;
2498 case CK_Help:
2499 edit_help_cmd (edit);
2500 break;
2502 case CK_Refresh:
2503 edit_refresh_cmd (edit);
2504 break;
2506 case CK_Date:{
2507 time_t t;
2508 #ifdef HAVE_STRFTIME
2509 char s[1024];
2510 /* fool gcc to prevent a Y2K warning */
2511 char time_format[] = "_c";
2512 time_format[0] = '%';
2513 #endif
2514 time (&t);
2515 #ifdef HAVE_STRFTIME
2516 strftime (s, sizeof (s), time_format, localtime (&t));
2517 edit_print_string (edit, s);
2518 #else
2519 edit_print_string (edit, ctime (&t));
2520 #endif
2521 edit->force |= REDRAW_PAGE;
2522 break;
2524 case CK_Goto:
2525 edit_goto_cmd (edit);
2526 break;
2527 case CK_Paragraph_Format:
2528 format_paragraph (edit, 1);
2529 edit->force |= REDRAW_PAGE;
2530 break;
2531 case CK_Delete_Macro:
2532 edit_delete_macro_cmd (edit);
2533 break;
2534 case CK_Match_Bracket:
2535 edit_goto_matching_bracket (edit);
2536 break;
2537 case CK_User_Menu:
2538 user_menu (edit);
2539 break;
2540 case CK_Sort:
2541 edit_sort_cmd (edit);
2542 break;
2543 case CK_ExtCmd:
2544 edit_ext_cmd (edit);
2545 break;
2546 case CK_Mail:
2547 edit_mail_dialog (edit);
2548 break;
2549 case CK_Shell:
2550 view_other_cmd ();
2551 break;
2552 default:
2553 break;
2556 /* CK_Pipe_Block */
2557 if ((command / 1000) == 1) /* a shell command */
2558 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2559 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2560 struct macro m[MAX_MACRO_LENGTH];
2561 int nm;
2562 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
2563 edit_execute_macro (edit, m, nm);
2566 /* keys which must set the col position, and the search vars */
2567 switch (command) {
2568 case CK_Find:
2569 case CK_Find_Again:
2570 case CK_Replace:
2571 case CK_Replace_Again:
2572 case CK_Complete_Word:
2573 edit->prev_col = edit_get_col (edit);
2574 return;
2575 case CK_Up:
2576 case CK_Up_Highlight:
2577 case CK_Down:
2578 case CK_Down_Highlight:
2579 case CK_Page_Up:
2580 case CK_Page_Up_Highlight:
2581 case CK_Page_Down:
2582 case CK_Page_Down_Highlight:
2583 case CK_Beginning_Of_Text:
2584 case CK_Beginning_Of_Text_Highlight:
2585 case CK_End_Of_Text:
2586 case CK_End_Of_Text_Highlight:
2587 case CK_Paragraph_Up:
2588 case CK_Paragraph_Up_Highlight:
2589 case CK_Paragraph_Down:
2590 case CK_Paragraph_Down_Highlight:
2591 case CK_Scroll_Up:
2592 case CK_Scroll_Up_Highlight:
2593 case CK_Scroll_Down:
2594 case CK_Scroll_Down_Highlight:
2595 edit->search_start = edit->curs1;
2596 edit->found_len = 0;
2597 edit_find_bracket (edit);
2598 return;
2599 default:
2600 edit->found_len = 0;
2601 edit->prev_col = edit_get_col (edit);
2602 edit->search_start = edit->curs1;
2604 edit_find_bracket (edit);
2606 if (option_auto_para_formatting) {
2607 switch (command) {
2608 case CK_BackSpace:
2609 case CK_Delete:
2610 case CK_Delete_Word_Left:
2611 case CK_Delete_Word_Right:
2612 case CK_Delete_To_Line_End:
2613 case CK_Delete_To_Line_Begin:
2614 format_paragraph (edit, 0);
2615 edit->force |= REDRAW_PAGE;
2621 static void
2622 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2624 int i = 0;
2626 if (edit->macro_depth++ > 256) {
2627 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2628 edit->macro_depth--;
2629 return;
2631 edit->force |= REDRAW_PAGE;
2632 for (; i < n; i++) {
2633 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2635 edit_update_screen (edit);
2636 edit->macro_depth--;
2639 /* User edit menu, like user menu (F2) but only in editor. */
2640 static void
2641 user_menu (WEdit * edit)
2643 FILE *fd;
2644 int nomark;
2645 struct stat status;
2646 long start_mark, end_mark;
2647 char *block_file = concat_dir_and_file (home_dir, BLOCK_FILE);
2648 int rc = 0;
2650 nomark = eval_marks (edit, &start_mark, &end_mark);
2651 if (!nomark) /* remember marked or not */
2652 edit_save_block (edit, block_file, start_mark, end_mark);
2654 /* run shell scripts from menu */
2655 user_menu_cmd (edit);
2657 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2658 /* no block messages */
2659 goto cleanup;
2662 if (!nomark) {
2663 /* i.e. we have marked block */
2664 rc = edit_block_delete_cmd (edit);
2667 if (!rc) {
2668 edit_insert_file (edit, block_file);
2671 /* truncate block file */
2672 if ((fd = fopen (block_file, "w"))) {
2673 fclose (fd);
2676 edit_refresh_cmd (edit);
2677 edit->force |= REDRAW_COMPLETELY;
2679 cleanup:
2680 g_free (block_file);