Merge commit '315633ecce31cd5af92112ab9903f6e7e8ae8df7'
[free-mc.git] / edit / edit.c
blob4bce1196c5877e63916b8e13f47788ad864e0a29
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>
34 #include <stdlib.h>
36 #include "../src/global.h"
38 #include "edit.h"
39 #include "editlock.h"
40 #include "edit-widget.h"
41 #include "editcmddef.h"
42 #include "usermap.h"
44 #include "../src/cmd.h" /* view_other_cmd() */
45 #include "../src/user.h" /* user_menu_cmd() */
46 #include "../src/wtools.h" /* query_dialog() */
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 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
186 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
187 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
190 /* Return index of the filter or -1 is there is no appropriate filter */
191 static int edit_find_filter (const char *filename)
193 size_t i, l, e;
194 if (!filename)
195 return -1;
196 l = strlen (filename);
197 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
198 e = strlen (all_filters[i].extension);
199 if (l > e)
200 if (!strcmp (all_filters[i].extension, filename + l - e))
201 return i;
203 return -1;
206 static char *
207 edit_get_filter (const char *filename)
209 int i, l;
210 char *p, *quoted_name;
211 i = edit_find_filter (filename);
212 if (i < 0)
213 return 0;
214 quoted_name = name_quote (filename, 0);
215 l = strlen (quoted_name);
216 p = g_malloc (strlen (all_filters[i].read) + l + 2);
217 sprintf (p, all_filters[i].read, quoted_name);
218 g_free (quoted_name);
219 return p;
222 char *
223 edit_get_write_filter (const char *write_name, const char *filename)
225 int i, l;
226 char *p, *writename;
227 i = edit_find_filter (filename);
228 if (i < 0)
229 return 0;
230 writename = name_quote (write_name, 0);
231 l = strlen (writename);
232 p = g_malloc (strlen (all_filters[i].write) + l + 2);
233 sprintf (p, all_filters[i].write, writename);
234 g_free (writename);
235 return p;
238 static long
239 edit_insert_stream (WEdit * edit, FILE * f)
241 int c;
242 long i = 0;
243 while ((c = fgetc (f)) >= 0) {
244 edit_insert (edit, c);
245 i++;
247 return i;
250 long edit_write_stream (WEdit * edit, FILE * f)
252 long i;
253 for (i = 0; i < edit->last_byte; i++)
254 if (fputc (edit_get_byte (edit, i), f) < 0)
255 break;
256 return i;
259 #define TEMP_BUF_LEN 1024
261 /* inserts a file at the cursor, returns 1 on success */
263 edit_insert_file (WEdit *edit, const char *filename)
265 char *p;
266 if ((p = edit_get_filter (filename))) {
267 FILE *f;
268 long current = edit->curs1;
269 f = (FILE *) popen (p, "r");
270 if (f) {
271 edit_insert_stream (edit, f);
272 edit_cursor_move (edit, current - edit->curs1);
273 if (pclose (f) > 0) {
274 GString *errmsg = g_string_new (NULL);
275 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
276 edit_error_dialog (_("Error"), errmsg->str);
277 g_string_free (errmsg, TRUE);
278 g_free (p);
279 return 0;
281 } else {
282 GString *errmsg = g_string_new (NULL);
283 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
284 edit_error_dialog (_("Error"), errmsg->str);
285 g_string_free (errmsg, TRUE);
286 g_free (p);
287 return 0;
289 g_free (p);
290 } else {
291 int i, file, blocklen;
292 long current = edit->curs1;
293 unsigned char *buf;
294 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
295 return 0;
296 buf = g_malloc (TEMP_BUF_LEN);
297 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
298 for (i = 0; i < blocklen; i++)
299 edit_insert (edit, buf[i]);
301 edit_cursor_move (edit, current - edit->curs1);
302 g_free (buf);
303 mc_close (file);
304 if (blocklen)
305 return 0;
307 return 1;
310 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
311 static int
312 check_file_access (WEdit *edit, const char *filename, struct stat *st)
314 int file;
315 GString *errmsg = (GString *) 0;
317 /* Try opening an existing file */
318 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
320 if (file < 0) {
322 * Try creating the file. O_EXCL prevents following broken links
323 * and opening existing files.
325 file =
326 mc_open (filename,
327 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
328 0666);
329 if (file < 0) {
330 g_string_sprintf (errmsg = g_string_new (NULL),
331 _(" Cannot open %s for reading "), filename);
332 goto cleanup;
333 } else {
334 /* New file, delete it if it's not modified or saved */
335 edit->delete_file = 1;
339 /* Check what we have opened */
340 if (mc_fstat (file, st) < 0) {
341 g_string_sprintf (errmsg = g_string_new (NULL),
342 _(" Cannot get size/permissions for %s "), filename);
343 goto cleanup;
346 /* We want to open regular files only */
347 if (!S_ISREG (st->st_mode)) {
348 g_string_sprintf (errmsg = g_string_new (NULL),
349 _(" %s is not a regular file "), filename);
350 goto cleanup;
354 * Don't delete non-empty files.
355 * O_EXCL should prevent it, but let's be on the safe side.
357 if (st->st_size > 0) {
358 edit->delete_file = 0;
361 if (st->st_size >= SIZE_LIMIT) {
362 g_string_sprintf (errmsg = g_string_new (NULL),
363 _(" File %s is too large "), filename);
364 goto cleanup;
367 cleanup:
368 (void) mc_close (file);
369 if (errmsg) {
370 edit_error_dialog (_("Error"), errmsg->str);
371 g_string_free (errmsg, TRUE);
372 return 1;
374 return 0;
378 * Open the file and load it into the buffers, either directly or using
379 * a filter. Return 0 on success, 1 on error.
381 * Fast loading (edit_load_file_fast) is used when the file size is
382 * known. In this case the data is read into the buffers by blocks.
383 * If the file size is not known, the data is loaded byte by byte in
384 * edit_insert_file.
386 static int
387 edit_load_file (WEdit *edit)
389 int fast_load = 1;
391 /* Cannot do fast load if a filter is used */
392 if (edit_find_filter (edit->filename) >= 0)
393 fast_load = 0;
396 * VFS may report file size incorrectly, and slow load is not a big
397 * deal considering overhead in VFS.
399 if (!vfs_file_is_local (edit->filename))
400 fast_load = 0;
403 * FIXME: line end translation should disable fast loading as well
404 * Consider doing fseek() to the end and ftell() for the real size.
407 if (*edit->filename) {
408 /* If we are dealing with a real file, check that it exists */
409 if (check_file_access (edit, edit->filename, &edit->stat1))
410 return 1;
411 } else {
412 /* nothing to load */
413 fast_load = 0;
416 edit_init_buffers (edit);
418 if (fast_load) {
419 edit->last_byte = edit->stat1.st_size;
420 edit_load_file_fast (edit, edit->filename);
421 /* If fast load was used, the number of lines wasn't calculated */
422 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
423 } else {
424 edit->last_byte = 0;
425 if (*edit->filename) {
426 edit->stack_disable = 1;
427 if (!edit_insert_file (edit, edit->filename)) {
428 edit_clean (edit);
429 return 1;
431 edit->stack_disable = 0;
434 return 0;
437 /* Restore saved cursor position in the file */
438 static void
439 edit_load_position (WEdit *edit)
441 char *filename;
442 long line, column;
444 if (!edit->filename || !*edit->filename)
445 return;
447 filename = vfs_canon (edit->filename);
448 load_file_position (filename, &line, &column);
449 g_free (filename);
451 edit_move_to_line (edit, line - 1);
452 edit->prev_col = column;
453 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
454 edit_move_display (edit, line - (edit->num_widget_lines / 2));
457 /* Save cursor position in the file */
458 static void
459 edit_save_position (WEdit *edit)
461 char *filename;
463 if (!edit->filename || !*edit->filename)
464 return;
466 filename = vfs_canon (edit->filename);
467 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
468 g_free (filename);
471 /* Clean the WEdit stricture except the widget part */
472 static inline void
473 edit_purge_widget (WEdit *edit)
475 int len = sizeof (WEdit) - sizeof (Widget);
476 char *start = (char *) edit + sizeof (Widget);
477 memset (start, 0, len);
478 edit->macro_i = -1; /* not recording a macro */
481 #define space_width 1
484 * Fill in the edit structure. Return NULL on failure. Pass edit as
485 * NULL to allocate a new structure.
487 * If line is 0, try to restore saved position. Otherwise put the
488 * cursor on that line and show it in the middle of the screen.
490 WEdit *
491 edit_init (WEdit *edit, int lines, int columns, const char *filename,
492 long line)
494 int to_free = 0;
495 option_auto_syntax = 1; /* Resetting to auto on every invokation */
497 if (!edit) {
498 #ifdef ENABLE_NLS
500 * Expand option_whole_chars_search by national letters using
501 * current locale
504 static char option_whole_chars_search_buf[256];
506 if (option_whole_chars_search_buf != option_whole_chars_search) {
507 size_t i;
508 size_t len = strlen (option_whole_chars_search);
510 strcpy (option_whole_chars_search_buf,
511 option_whole_chars_search);
513 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
514 if (islower (i) && !strchr (option_whole_chars_search, i)) {
515 option_whole_chars_search_buf[len++] = i;
519 option_whole_chars_search_buf[len] = 0;
520 option_whole_chars_search = option_whole_chars_search_buf;
522 #endif /* ENABLE_NLS */
523 edit = g_malloc0 (sizeof (WEdit));
524 to_free = 1;
526 edit_purge_widget (edit);
527 edit->num_widget_lines = lines;
528 edit->num_widget_columns = columns;
529 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
530 edit->stat1.st_uid = getuid ();
531 edit->stat1.st_gid = getgid ();
532 edit->stat1.st_mtime = 0;
533 edit->bracket = -1;
534 edit->force |= REDRAW_PAGE;
535 edit_set_filename (edit, filename);
536 edit->stack_size = START_STACK_SIZE;
537 edit->stack_size_mask = START_STACK_SIZE - 1;
538 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
539 if (edit_load_file (edit)) {
540 /* edit_load_file already gives an error message */
541 if (to_free)
542 g_free (edit);
543 return 0;
545 edit->loading_done = 1;
546 edit->modified = 0;
547 edit->locked = 0;
548 edit_load_syntax (edit, 0, 0);
550 int color;
551 edit_get_syntax_color (edit, -1, &color);
554 /* load saved cursor position */
555 if ((line == 0) && option_save_position) {
556 edit_load_position (edit);
557 } else {
558 if (line <= 0)
559 line = 1;
560 edit_move_display (edit, line - 1);
561 edit_move_to_line (edit, line - 1);
564 edit_load_user_map(edit);
566 return edit;
569 /* Clear the edit struct, freeing everything in it. Return 1 on success */
571 edit_clean (WEdit *edit)
573 int j = 0;
575 if (!edit)
576 return 0;
578 /* a stale lock, remove it */
579 if (edit->locked)
580 edit->locked = edit_unlock_file (edit->filename);
582 /* save cursor position */
583 if (option_save_position)
584 edit_save_position (edit);
586 /* File specified on the mcedit command line and never saved */
587 if (edit->delete_file)
588 unlink (edit->filename);
590 edit_free_syntax_rules (edit);
591 book_mark_flush (edit, -1);
592 for (; j <= MAXBUFF; j++) {
593 g_free (edit->buffers1[j]);
594 g_free (edit->buffers2[j]);
597 g_free (edit->undo_stack);
598 g_free (edit->filename);
599 g_free (edit->dir);
601 edit_purge_widget (edit);
603 /* Free temporary strings used in catstrs() */
604 freestrs ();
606 return 1;
610 /* returns 1 on success */
611 int edit_renew (WEdit * edit)
613 int lines = edit->num_widget_lines;
614 int columns = edit->num_widget_columns;
615 int retval = 1;
617 edit_clean (edit);
618 if (!edit_init (edit, lines, columns, "", 0))
619 retval = 0;
620 return retval;
624 * Load a new file into the editor. If it fails, preserve the old file.
625 * To do it, allocate a new widget, initialize it and, if the new file
626 * was loaded, copy the data to the old widget.
627 * Return 1 on success, 0 on failure.
630 edit_reload (WEdit *edit, const char *filename)
632 WEdit *e;
633 int lines = edit->num_widget_lines;
634 int columns = edit->num_widget_columns;
636 e = g_malloc0 (sizeof (WEdit));
637 e->widget = edit->widget;
638 if (!edit_init (e, lines, columns, filename, 0)) {
639 g_free (e);
640 return 0;
642 edit_clean (edit);
643 memcpy (edit, e, sizeof (WEdit));
644 g_free (e);
645 return 1;
650 Recording stack for undo:
651 The following is an implementation of a compressed stack. Identical
652 pushes are recorded by a negative prefix indicating the number of times the
653 same char was pushed. This saves space for repeated curs-left or curs-right
654 delete etc.
658 pushed: stored:
662 b -3
664 c --> -4
670 If the stack long int is 0-255 it represents a normal insert (from a backspace),
671 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
672 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
673 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
674 position.
676 The only way the cursor moves or the buffer is changed is through the routines:
677 insert, backspace, insert_ahead, delete, and cursor_move.
678 These record the reverse undo movements onto the stack each time they are
679 called.
681 Each key press results in a set of actions (insert; delete ...). So each time
682 a key is pressed the current position of start_display is pushed as
683 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
684 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
685 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
689 void edit_push_action (WEdit * edit, long c,...)
691 unsigned long sp = edit->stack_pointer;
692 unsigned long spm1;
693 long *t;
695 /* first enlarge the stack if necessary */
696 if (sp > edit->stack_size - 10) { /* say */
697 if (option_max_undo < 256)
698 option_max_undo = 256;
699 if (edit->stack_size < (unsigned long) option_max_undo) {
700 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
701 if (t) {
702 edit->undo_stack = t;
703 edit->stack_size <<= 1;
704 edit->stack_size_mask = edit->stack_size - 1;
708 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
709 if (edit->stack_disable)
710 return;
712 #ifdef FAST_MOVE_CURSOR
713 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
714 va_list ap;
715 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
716 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
717 va_start (ap, c);
718 c = -(va_arg (ap, int));
719 va_end (ap);
720 } else
721 #endif /* ! FAST_MOVE_CURSOR */
722 if (edit->stack_bottom != sp
723 && spm1 != edit->stack_bottom
724 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
725 int d;
726 if (edit->undo_stack[spm1] < 0) {
727 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
728 if (d == c) {
729 if (edit->undo_stack[spm1] > -1000000000) {
730 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
731 edit->undo_stack[spm1]--;
732 return;
735 /* #define NO_STACK_CURSMOVE_ANIHILATION */
736 #ifndef NO_STACK_CURSMOVE_ANIHILATION
737 else if ((c == CURS_LEFT && d == CURS_RIGHT)
738 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
739 if (edit->undo_stack[spm1] == -2)
740 edit->stack_pointer = spm1;
741 else
742 edit->undo_stack[spm1]++;
743 return;
745 #endif
746 } else {
747 d = edit->undo_stack[spm1];
748 if (d == c) {
749 if (c >= KEY_PRESS)
750 return; /* --> no need to push multiple do-nothings */
751 edit->undo_stack[sp] = -2;
752 goto check_bottom;
754 #ifndef NO_STACK_CURSMOVE_ANIHILATION
755 else if ((c == CURS_LEFT && d == CURS_RIGHT)
756 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
757 edit->stack_pointer = spm1;
758 return;
760 #endif
763 edit->undo_stack[sp] = c;
764 check_bottom:
766 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
768 /* if the sp wraps round and catches the stack_bottom then erase
769 * the first set of actions on the stack to make space - by moving
770 * stack_bottom forward one "key press" */
771 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
772 if ((unsigned long) c == edit->stack_bottom ||
773 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
774 do {
775 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
776 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
778 /*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: */
779 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
780 edit->stack_bottom = edit->stack_pointer = 0;
784 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
785 then the file should be as it was when he loaded up. Then set edit->modified to 0.
787 static long
788 pop_action (WEdit * edit)
790 long c;
791 unsigned long sp = edit->stack_pointer;
792 if (sp == edit->stack_bottom) {
793 return STACK_BOTTOM;
795 sp = (sp - 1) & edit->stack_size_mask;
796 if ((c = edit->undo_stack[sp]) >= 0) {
797 /* edit->undo_stack[sp] = '@'; */
798 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
799 return c;
801 if (sp == edit->stack_bottom) {
802 return STACK_BOTTOM;
804 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
805 if (edit->undo_stack[sp] == -2) {
806 /* edit->undo_stack[sp] = '@'; */
807 edit->stack_pointer = sp;
808 } else
809 edit->undo_stack[sp]++;
811 return c;
814 /* is called whenever a modification is made by one of the four routines below */
815 static inline void edit_modification (WEdit * edit)
817 edit->caches_valid = 0;
818 edit->screen_modified = 1;
820 /* raise lock when file modified */
821 if (!edit->modified && !edit->delete_file)
822 edit->locked = edit_lock_file (edit->filename);
823 edit->modified = 1;
827 Basic low level single character buffer alterations and movements at the cursor.
828 Returns char passed over, inserted or removed.
831 void
832 edit_insert (WEdit *edit, int c)
834 /* check if file has grown to large */
835 if (edit->last_byte >= SIZE_LIMIT)
836 return;
838 /* first we must update the position of the display window */
839 if (edit->curs1 < edit->start_display) {
840 edit->start_display++;
841 if (c == '\n')
842 edit->start_line++;
845 /* Mark file as modified, unless the file hasn't been fully loaded */
846 if (edit->loading_done) {
847 edit_modification (edit);
850 /* now we must update some info on the file and check if a redraw is required */
851 if (c == '\n') {
852 if (edit->book_mark)
853 book_mark_inc (edit, edit->curs_line);
854 edit->curs_line++;
855 edit->total_lines++;
856 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
859 /* save the reverse command onto the undo stack */
860 edit_push_action (edit, BACKSPACE);
862 /* update markers */
863 edit->mark1 += (edit->mark1 > edit->curs1);
864 edit->mark2 += (edit->mark2 > edit->curs1);
865 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
867 /* add a new buffer if we've reached the end of the last one */
868 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
869 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
870 g_malloc (EDIT_BUF_SIZE);
872 /* perform the insertion */
873 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
874 curs1 & M_EDIT_BUF_SIZE]
875 = (unsigned char) c;
877 /* update file length */
878 edit->last_byte++;
880 /* update cursor position */
881 edit->curs1++;
885 /* same as edit_insert and move left */
886 void edit_insert_ahead (WEdit * edit, int c)
888 if (edit->last_byte >= SIZE_LIMIT)
889 return;
890 if (edit->curs1 < edit->start_display) {
891 edit->start_display++;
892 if (c == '\n')
893 edit->start_line++;
895 edit_modification (edit);
896 if (c == '\n') {
897 if (edit->book_mark)
898 book_mark_inc (edit, edit->curs_line);
899 edit->total_lines++;
900 edit->force |= REDRAW_AFTER_CURSOR;
902 edit_push_action (edit, DELCHAR);
904 edit->mark1 += (edit->mark1 >= edit->curs1);
905 edit->mark2 += (edit->mark2 >= edit->curs1);
906 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
908 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
909 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
910 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
912 edit->last_byte++;
913 edit->curs2++;
917 int edit_delete (WEdit * edit)
919 int p;
920 if (!edit->curs2)
921 return 0;
923 edit->mark1 -= (edit->mark1 > edit->curs1);
924 edit->mark2 -= (edit->mark2 > edit->curs1);
925 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
927 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
929 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
930 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
931 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
933 edit->last_byte--;
934 edit->curs2--;
936 edit_modification (edit);
937 if (p == '\n') {
938 if (edit->book_mark)
939 book_mark_dec (edit, edit->curs_line);
940 edit->total_lines--;
941 edit->force |= REDRAW_AFTER_CURSOR;
943 edit_push_action (edit, p + 256);
944 if (edit->curs1 < edit->start_display) {
945 edit->start_display--;
946 if (p == '\n')
947 edit->start_line--;
950 return p;
954 static int
955 edit_backspace (WEdit * edit)
957 int p;
958 if (!edit->curs1)
959 return 0;
961 edit->mark1 -= (edit->mark1 >= edit->curs1);
962 edit->mark2 -= (edit->mark2 >= edit->curs1);
963 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
965 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
966 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
967 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
968 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
970 edit->last_byte--;
971 edit->curs1--;
973 edit_modification (edit);
974 if (p == '\n') {
975 if (edit->book_mark)
976 book_mark_dec (edit, edit->curs_line);
977 edit->curs_line--;
978 edit->total_lines--;
979 edit->force |= REDRAW_AFTER_CURSOR;
981 edit_push_action (edit, p);
983 if (edit->curs1 < edit->start_display) {
984 edit->start_display--;
985 if (p == '\n')
986 edit->start_line--;
989 return p;
992 #ifdef FAST_MOVE_CURSOR
994 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
996 unsigned long next;
997 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
998 edit->curs_line--;
999 next -= (unsigned long) dest;
1000 n -= next;
1001 src += next;
1002 dest += next;
1007 edit_move_backward_lots (WEdit *edit, long increment)
1009 int r, s, t;
1010 unsigned char *p;
1012 if (increment > edit->curs1)
1013 increment = edit->curs1;
1014 if (increment <= 0)
1015 return -1;
1016 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1018 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1019 if (r > increment)
1020 r = increment;
1021 s = edit->curs1 & M_EDIT_BUF_SIZE;
1023 p = 0;
1024 if (s > r) {
1025 memqcpy (edit,
1026 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1027 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1029 } else {
1030 if (s) {
1031 memqcpy (edit,
1032 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1033 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1034 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1035 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1037 memqcpy (edit,
1038 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1039 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1040 EDIT_BUF_SIZE - (r - s), r - s);
1042 increment -= r;
1043 edit->curs1 -= r;
1044 edit->curs2 += r;
1045 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1046 if (p)
1047 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1048 else
1049 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1050 g_malloc (EDIT_BUF_SIZE);
1051 } else {
1052 g_free (p);
1055 s = edit->curs1 & M_EDIT_BUF_SIZE;
1056 while (increment) {
1057 p = 0;
1058 r = EDIT_BUF_SIZE;
1059 if (r > increment)
1060 r = increment;
1061 t = s;
1062 if (r < t)
1063 t = r;
1064 memqcpy (edit,
1065 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1066 EDIT_BUF_SIZE - t,
1067 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1069 if (r >= s) {
1070 if (t) {
1071 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1072 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1074 memqcpy (edit,
1075 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1076 EDIT_BUF_SIZE - r,
1077 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1078 EDIT_BUF_SIZE - (r - s), r - s);
1080 increment -= r;
1081 edit->curs1 -= r;
1082 edit->curs2 += r;
1083 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1084 if (p)
1085 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1086 else
1087 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1088 g_malloc (EDIT_BUF_SIZE);
1089 } else {
1090 g_free (p);
1093 return edit_get_byte (edit, edit->curs1);
1096 #endif /* ! FAST_MOVE_CURSOR */
1098 /* moves the cursor right or left: increment positive or negative respectively */
1099 void edit_cursor_move (WEdit * edit, long increment)
1101 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1102 int c;
1104 #ifdef FAST_MOVE_CURSOR
1105 if (increment < -256) {
1106 edit->force |= REDRAW_PAGE;
1107 edit_move_backward_lots (edit, -increment);
1108 return;
1110 #endif /* ! FAST_MOVE_CURSOR */
1112 if (increment < 0) {
1113 for (; increment < 0; increment++) {
1114 if (!edit->curs1)
1115 return;
1117 edit_push_action (edit, CURS_RIGHT);
1119 c = edit_get_byte (edit, edit->curs1 - 1);
1120 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1121 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1122 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1123 edit->curs2++;
1124 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1125 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1126 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1127 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1129 edit->curs1--;
1130 if (c == '\n') {
1131 edit->curs_line--;
1132 edit->force |= REDRAW_LINE_BELOW;
1136 } else if (increment > 0) {
1137 for (; increment > 0; increment--) {
1138 if (!edit->curs2)
1139 return;
1141 edit_push_action (edit, CURS_LEFT);
1143 c = edit_get_byte (edit, edit->curs1);
1144 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1145 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1146 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1147 edit->curs1++;
1148 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1149 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1150 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1151 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1153 edit->curs2--;
1154 if (c == '\n') {
1155 edit->curs_line++;
1156 edit->force |= REDRAW_LINE_ABOVE;
1162 /* These functions return positions relative to lines */
1164 /* returns index of last char on line + 1 */
1165 long edit_eol (WEdit * edit, long current)
1167 if (current < edit->last_byte) {
1168 for (;; current++)
1169 if (edit_get_byte (edit, current) == '\n')
1170 break;
1171 } else
1172 return edit->last_byte;
1173 return current;
1176 /* returns index of first char on line */
1177 long edit_bol (WEdit * edit, long current)
1179 if (current > 0) {
1180 for (;; current--)
1181 if (edit_get_byte (edit, current - 1) == '\n')
1182 break;
1183 } else
1184 return 0;
1185 return current;
1189 int edit_count_lines (WEdit * edit, long current, int upto)
1191 int lines = 0;
1192 if (upto > edit->last_byte)
1193 upto = edit->last_byte;
1194 if (current < 0)
1195 current = 0;
1196 while (current < upto)
1197 if (edit_get_byte (edit, current++) == '\n')
1198 lines++;
1199 return lines;
1203 /* If lines is zero this returns the count of lines from current to upto. */
1204 /* If upto is zero returns index of lines forward current. */
1205 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1207 if (upto) {
1208 return edit_count_lines (edit, current, upto);
1209 } else {
1210 int next;
1211 if (lines < 0)
1212 lines = 0;
1213 while (lines--) {
1214 next = edit_eol (edit, current) + 1;
1215 if (next > edit->last_byte)
1216 break;
1217 else
1218 current = next;
1220 return current;
1225 /* Returns offset of 'lines' lines up from current */
1226 long edit_move_backward (WEdit * edit, long current, int lines)
1228 if (lines < 0)
1229 lines = 0;
1230 current = edit_bol (edit, current);
1231 while((lines--) && current != 0)
1232 current = edit_bol (edit, current - 1);
1233 return current;
1236 /* If cols is zero this returns the count of columns from current to upto. */
1237 /* If upto is zero returns index of cols across from current. */
1238 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1240 long p, q;
1241 int col = 0;
1243 if (upto) {
1244 q = upto;
1245 cols = -10;
1246 } else
1247 q = edit->last_byte + 2;
1249 for (col = 0, p = current; p < q; p++) {
1250 int c;
1251 if (cols != -10) {
1252 if (col == cols)
1253 return p;
1254 if (col > cols)
1255 return p - 1;
1257 c = edit_get_byte (edit, p);
1258 if (c == '\t')
1259 col += TAB_SIZE - col % TAB_SIZE;
1260 else if (c == '\n') {
1261 if (upto)
1262 return col;
1263 else
1264 return p;
1265 } else if (c < 32 || c == 127)
1266 col += 2; /* Caret notation for control characters */
1267 else
1268 col++;
1270 return col;
1273 /* returns the current column position of the cursor */
1274 int edit_get_col (WEdit * edit)
1276 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1280 /* Scrolling functions */
1282 void edit_update_curs_row (WEdit * edit)
1284 edit->curs_row = edit->curs_line - edit->start_line;
1287 void edit_update_curs_col (WEdit * edit)
1289 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1292 /*moves the display start position up by i lines */
1293 void edit_scroll_upward (WEdit * edit, unsigned long i)
1295 unsigned long lines_above = edit->start_line;
1296 if (i > lines_above)
1297 i = lines_above;
1298 if (i) {
1299 edit->start_line -= i;
1300 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1301 edit->force |= REDRAW_PAGE;
1302 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1304 edit_update_curs_row (edit);
1308 /* returns 1 if could scroll, 0 otherwise */
1309 void edit_scroll_downward (WEdit * edit, int i)
1311 int lines_below;
1312 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1313 if (lines_below > 0) {
1314 if (i > lines_below)
1315 i = lines_below;
1316 edit->start_line += i;
1317 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1318 edit->force |= REDRAW_PAGE;
1319 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1321 edit_update_curs_row (edit);
1324 void edit_scroll_right (WEdit * edit, int i)
1326 edit->force |= REDRAW_PAGE;
1327 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1328 edit->start_col -= i;
1331 void edit_scroll_left (WEdit * edit, int i)
1333 if (edit->start_col) {
1334 edit->start_col += i;
1335 if (edit->start_col > 0)
1336 edit->start_col = 0;
1337 edit->force |= REDRAW_PAGE;
1338 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1342 /* high level cursor movement commands */
1344 static int is_in_indent (WEdit *edit)
1346 long p = edit_bol (edit, edit->curs1);
1347 while (p < edit->curs1)
1348 if (!strchr (" \t", edit_get_byte (edit, p++)))
1349 return 0;
1350 return 1;
1353 static int left_of_four_spaces (WEdit *edit);
1355 void
1356 edit_move_to_prev_col (WEdit * edit, long p)
1358 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1360 if (is_in_indent (edit) && option_fake_half_tabs) {
1361 edit_update_curs_col (edit);
1362 if (space_width)
1363 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1364 int q = edit->curs_col;
1365 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1366 p = edit_bol (edit, edit->curs1);
1367 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1368 if (!left_of_four_spaces (edit))
1369 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1375 /* move i lines */
1376 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1378 unsigned long p, l = edit->curs_line;
1380 if (i > l)
1381 i = l;
1382 if (i) {
1383 if (i > 1)
1384 edit->force |= REDRAW_PAGE;
1385 if (scroll)
1386 edit_scroll_upward (edit, i);
1388 p = edit_bol (edit, edit->curs1);
1389 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1390 edit_move_to_prev_col (edit, p);
1392 edit->search_start = edit->curs1;
1393 edit->found_len = 0;
1397 static int
1398 is_blank (WEdit *edit, long offset)
1400 long s, f;
1401 int c;
1402 s = edit_bol (edit, offset);
1403 f = edit_eol (edit, offset) - 1;
1404 while (s <= f) {
1405 c = edit_get_byte (edit, s++);
1406 if (!isspace (c))
1407 return 0;
1409 return 1;
1413 /* returns the offset of line i */
1414 static long
1415 edit_find_line (WEdit *edit, int line)
1417 int i, j = 0;
1418 int m = 2000000000;
1419 if (!edit->caches_valid) {
1420 for (i = 0; i < N_LINE_CACHES; i++)
1421 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1422 /* three offsets that we *know* are line 0 at 0 and these two: */
1423 edit->line_numbers[1] = edit->curs_line;
1424 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1425 edit->line_numbers[2] = edit->total_lines;
1426 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1427 edit->caches_valid = 1;
1429 if (line >= edit->total_lines)
1430 return edit->line_offsets[2];
1431 if (line <= 0)
1432 return 0;
1433 /* find the closest known point */
1434 for (i = 0; i < N_LINE_CACHES; i++) {
1435 int n;
1436 n = abs (edit->line_numbers[i] - line);
1437 if (n < m) {
1438 m = n;
1439 j = i;
1442 if (m == 0)
1443 return edit->line_offsets[j]; /* know the offset exactly */
1444 if (m == 1 && j >= 3)
1445 i = j; /* one line different - caller might be looping, so stay in this cache */
1446 else
1447 i = 3 + (rand () % (N_LINE_CACHES - 3));
1448 if (line > edit->line_numbers[j])
1449 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1450 else
1451 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1452 edit->line_numbers[i] = line;
1453 return edit->line_offsets[i];
1456 int line_is_blank (WEdit * edit, long line)
1458 return is_blank (edit, edit_find_line (edit, line));
1461 /* moves up until a blank line is reached, or until just
1462 before a non-blank line is reached */
1463 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1465 int i;
1466 if (edit->curs_line <= 1) {
1467 i = 0;
1468 } else {
1469 if (line_is_blank (edit, edit->curs_line)) {
1470 if (line_is_blank (edit, edit->curs_line - 1)) {
1471 for (i = edit->curs_line - 1; i; i--)
1472 if (!line_is_blank (edit, i)) {
1473 i++;
1474 break;
1476 } else {
1477 for (i = edit->curs_line - 1; i; i--)
1478 if (line_is_blank (edit, i))
1479 break;
1481 } else {
1482 for (i = edit->curs_line - 1; i; i--)
1483 if (line_is_blank (edit, i))
1484 break;
1487 edit_move_up (edit, edit->curs_line - i, scroll);
1490 /* move i lines */
1491 void edit_move_down (WEdit * edit, int i, int scroll)
1493 long p, l = edit->total_lines - edit->curs_line;
1495 if (i > l)
1496 i = l;
1497 if (i) {
1498 if (i > 1)
1499 edit->force |= REDRAW_PAGE;
1500 if (scroll)
1501 edit_scroll_downward (edit, i);
1502 p = edit_bol (edit, edit->curs1);
1503 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1504 edit_move_to_prev_col (edit, p);
1506 edit->search_start = edit->curs1;
1507 edit->found_len = 0;
1511 /* moves down until a blank line is reached, or until just
1512 before a non-blank line is reached */
1513 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1515 int i;
1516 if (edit->curs_line >= edit->total_lines - 1) {
1517 i = edit->total_lines;
1518 } else {
1519 if (line_is_blank (edit, edit->curs_line)) {
1520 if (line_is_blank (edit, edit->curs_line + 1)) {
1521 for (i = edit->curs_line + 1; i; i++)
1522 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1523 i--;
1524 break;
1526 } else {
1527 for (i = edit->curs_line + 1; i; i++)
1528 if (line_is_blank (edit, i) || i >= edit->total_lines)
1529 break;
1531 } else {
1532 for (i = edit->curs_line + 1; i; i++)
1533 if (line_is_blank (edit, i) || i >= edit->total_lines)
1534 break;
1537 edit_move_down (edit, i - edit->curs_line, scroll);
1540 static void edit_begin_page (WEdit *edit)
1542 edit_update_curs_row (edit);
1543 edit_move_up (edit, edit->curs_row, 0);
1546 static void edit_end_page (WEdit *edit)
1548 edit_update_curs_row (edit);
1549 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1553 /* goto beginning of text */
1554 static void edit_move_to_top (WEdit * edit)
1556 if (edit->curs_line) {
1557 edit_cursor_move (edit, -edit->curs1);
1558 edit_move_to_prev_col (edit, 0);
1559 edit->force |= REDRAW_PAGE;
1560 edit->search_start = 0;
1561 edit_update_curs_row(edit);
1566 /* goto end of text */
1567 static void edit_move_to_bottom (WEdit * edit)
1569 if (edit->curs_line < edit->total_lines) {
1570 edit_cursor_move (edit, edit->curs2);
1571 edit->start_display = edit->last_byte;
1572 edit->start_line = edit->total_lines;
1573 edit_update_curs_row(edit);
1574 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1575 edit->force |= REDRAW_PAGE;
1579 /* goto beginning of line */
1580 static void edit_cursor_to_bol (WEdit * edit)
1582 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1583 edit->search_start = edit->curs1;
1584 edit->prev_col = edit_get_col (edit);
1587 /* goto end of line */
1588 static void edit_cursor_to_eol (WEdit * edit)
1590 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1591 edit->search_start = edit->curs1;
1592 edit->prev_col = edit_get_col (edit);
1595 /* move cursor to line 'line' */
1596 void edit_move_to_line (WEdit * e, long line)
1598 if(line < e->curs_line)
1599 edit_move_up (e, e->curs_line - line, 0);
1600 else
1601 edit_move_down (e, line - e->curs_line, 0);
1602 edit_scroll_screen_over_cursor (e);
1605 /* scroll window so that first visible line is 'line' */
1606 void edit_move_display (WEdit * e, long line)
1608 if(line < e->start_line)
1609 edit_scroll_upward (e, e->start_line - line);
1610 else
1611 edit_scroll_downward (e, line - e->start_line);
1614 /* save markers onto undo stack */
1615 void edit_push_markers (WEdit * edit)
1617 edit_push_action (edit, MARK_1 + edit->mark1);
1618 edit_push_action (edit, MARK_2 + edit->mark2);
1621 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1623 edit->mark1 = m1;
1624 edit->mark2 = m2;
1625 edit->column1 = c1;
1626 edit->column2 = c2;
1630 /* highlight marker toggle */
1631 void edit_mark_cmd (WEdit * edit, int unmark)
1633 edit_push_markers (edit);
1634 if (unmark) {
1635 edit_set_markers (edit, 0, 0, 0, 0);
1636 edit->force |= REDRAW_PAGE;
1637 } else {
1638 if (edit->mark2 >= 0) {
1639 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1640 edit->force |= REDRAW_PAGE;
1641 } else
1642 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1646 static unsigned long
1647 my_type_of (int c)
1649 int x, r = 0;
1650 const char *p, *q;
1651 const char option_chars_move_whole_word[] =
1652 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1654 if (!c)
1655 return 0;
1656 if (c == '!') {
1657 if (*option_chars_move_whole_word == '!')
1658 return 2;
1659 return 0x80000000UL;
1661 if (isupper (c))
1662 c = 'A';
1663 else if (islower (c))
1664 c = 'a';
1665 else if (isalpha (c))
1666 c = 'a';
1667 else if (isdigit (c))
1668 c = '0';
1669 else if (isspace (c))
1670 c = ' ';
1671 q = strchr (option_chars_move_whole_word, c);
1672 if (!q)
1673 return 0xFFFFFFFFUL;
1674 do {
1675 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1676 if (*p == '!')
1677 x <<= 1;
1678 r |= x;
1679 } while ((q = strchr (q + 1, c)));
1680 return r;
1683 static void
1684 edit_left_word_move (WEdit *edit, int s)
1686 for (;;) {
1687 int c1, c2;
1688 edit_cursor_move (edit, -1);
1689 if (!edit->curs1)
1690 break;
1691 c1 = edit_get_byte (edit, edit->curs1 - 1);
1692 c2 = edit_get_byte (edit, edit->curs1);
1693 if (!(my_type_of (c1) & my_type_of (c2)))
1694 break;
1695 if (isspace (c1) && !isspace (c2))
1696 break;
1697 if (s)
1698 if (!isspace (c1) && isspace (c2))
1699 break;
1703 static void edit_left_word_move_cmd (WEdit * edit)
1705 edit_left_word_move (edit, 0);
1706 edit->force |= REDRAW_PAGE;
1709 static void
1710 edit_right_word_move (WEdit *edit, int s)
1712 for (;;) {
1713 int c1, c2;
1714 edit_cursor_move (edit, 1);
1715 if (edit->curs1 >= edit->last_byte)
1716 break;
1717 c1 = edit_get_byte (edit, edit->curs1 - 1);
1718 c2 = edit_get_byte (edit, edit->curs1);
1719 if (!(my_type_of (c1) & my_type_of (c2)))
1720 break;
1721 if (isspace (c1) && !isspace (c2))
1722 break;
1723 if (s)
1724 if (!isspace (c1) && isspace (c2))
1725 break;
1729 static void edit_right_word_move_cmd (WEdit * edit)
1731 edit_right_word_move (edit, 0);
1732 edit->force |= REDRAW_PAGE;
1736 static void edit_right_delete_word (WEdit * edit)
1738 int c1, c2;
1739 for (;;) {
1740 if (edit->curs1 >= edit->last_byte)
1741 break;
1742 c1 = edit_delete (edit);
1743 c2 = edit_get_byte (edit, edit->curs1);
1744 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1745 break;
1746 if (!(my_type_of (c1) & my_type_of (c2)))
1747 break;
1751 static void edit_left_delete_word (WEdit * edit)
1753 int c1, c2;
1754 for (;;) {
1755 if (edit->curs1 <= 0)
1756 break;
1757 c1 = edit_backspace (edit);
1758 c2 = edit_get_byte (edit, edit->curs1 - 1);
1759 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1760 break;
1761 if (!(my_type_of (c1) & my_type_of (c2)))
1762 break;
1767 the start column position is not recorded, and hence does not
1768 undo as it happed. But who would notice.
1770 static void
1771 edit_do_undo (WEdit * edit)
1773 long ac;
1774 long count = 0;
1776 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1778 while ((ac = pop_action (edit)) < KEY_PRESS) {
1779 switch ((int) ac) {
1780 case STACK_BOTTOM:
1781 goto done_undo;
1782 case CURS_RIGHT:
1783 edit_cursor_move (edit, 1);
1784 break;
1785 case CURS_LEFT:
1786 edit_cursor_move (edit, -1);
1787 break;
1788 case BACKSPACE:
1789 edit_backspace (edit);
1790 break;
1791 case DELCHAR:
1792 edit_delete (edit);
1793 break;
1794 case COLUMN_ON:
1795 column_highlighting = 1;
1796 break;
1797 case COLUMN_OFF:
1798 column_highlighting = 0;
1799 break;
1801 if (ac >= 256 && ac < 512)
1802 edit_insert_ahead (edit, ac - 256);
1803 if (ac >= 0 && ac < 256)
1804 edit_insert (edit, ac);
1806 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1807 edit->mark1 = ac - MARK_1;
1808 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1809 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1810 edit->mark2 = ac - MARK_2;
1811 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1813 if (count++)
1814 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1817 if (edit->start_display > ac - KEY_PRESS) {
1818 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1819 edit->force |= REDRAW_PAGE;
1820 } else if (edit->start_display < ac - KEY_PRESS) {
1821 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1822 edit->force |= REDRAW_PAGE;
1824 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1825 edit_update_curs_row (edit);
1827 done_undo:;
1828 edit->stack_disable = 0;
1831 static void edit_delete_to_line_end (WEdit * edit)
1833 while (edit_get_byte (edit, edit->curs1) != '\n') {
1834 if (!edit->curs2)
1835 break;
1836 edit_delete (edit);
1840 static void edit_delete_to_line_begin (WEdit * edit)
1842 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1843 if (!edit->curs1)
1844 break;
1845 edit_backspace (edit);
1849 void
1850 edit_delete_line (WEdit *edit)
1853 * Delete right part of the line.
1854 * Note that edit_get_byte() returns '\n' when byte position is
1855 * beyond EOF.
1857 while (edit_get_byte (edit, edit->curs1) != '\n') {
1858 (void) edit_delete (edit);
1862 * Delete '\n' char.
1863 * Note that edit_delete() will not corrupt anything if called while
1864 * cursor position is EOF.
1866 (void) edit_delete (edit);
1869 * Delete left part of the line.
1870 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1872 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1873 (void) edit_backspace (edit);
1877 static void insert_spaces_tab (WEdit * edit, int half)
1879 int i;
1880 edit_update_curs_col (edit);
1881 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1882 while (i > 0) {
1883 edit_insert (edit, ' ');
1884 i -= space_width;
1888 static int is_aligned_on_a_tab (WEdit * edit)
1890 edit_update_curs_col (edit);
1891 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1892 return 0; /* not alligned on a tab */
1893 return 1;
1896 static int right_of_four_spaces (WEdit *edit)
1898 int i, ch = 0;
1899 for (i = 1; i <= HALF_TAB_SIZE; i++)
1900 ch |= edit_get_byte (edit, edit->curs1 - i);
1901 if (ch == ' ')
1902 return is_aligned_on_a_tab (edit);
1903 return 0;
1906 static int left_of_four_spaces (WEdit *edit)
1908 int i, ch = 0;
1909 for (i = 0; i < HALF_TAB_SIZE; i++)
1910 ch |= edit_get_byte (edit, edit->curs1 + i);
1911 if (ch == ' ')
1912 return is_aligned_on_a_tab (edit);
1913 return 0;
1916 int edit_indent_width (WEdit * edit, long p)
1918 long q = p;
1919 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1920 q++;
1921 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1924 void edit_insert_indent (WEdit * edit, int indent)
1926 if (!option_fill_tabs_with_spaces) {
1927 while (indent >= TAB_SIZE) {
1928 edit_insert (edit, '\t');
1929 indent -= TAB_SIZE;
1932 while (indent-- > 0)
1933 edit_insert (edit, ' ');
1936 static void
1937 edit_auto_indent (WEdit * edit)
1939 long p;
1940 char c;
1941 p = edit->curs1;
1942 /* use the previous line as a template */
1943 p = edit_move_backward (edit, p, 1);
1944 /* copy the leading whitespace of the line */
1945 for (;;) { /* no range check - the line _is_ \n-terminated */
1946 c = edit_get_byte (edit, p++);
1947 if (c != ' ' && c != '\t')
1948 break;
1949 edit_insert (edit, c);
1953 static void edit_double_newline (WEdit * edit)
1955 edit_insert (edit, '\n');
1956 if (edit_get_byte (edit, edit->curs1) == '\n')
1957 return;
1958 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1959 return;
1960 edit->force |= REDRAW_PAGE;
1961 edit_insert (edit, '\n');
1964 static void edit_tab_cmd (WEdit * edit)
1966 int i;
1968 if (option_fake_half_tabs) {
1969 if (is_in_indent (edit)) {
1970 /*insert a half tab (usually four spaces) unless there is a
1971 half tab already behind, then delete it and insert a
1972 full tab. */
1973 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1974 for (i = 1; i <= HALF_TAB_SIZE; i++)
1975 edit_backspace (edit);
1976 edit_insert (edit, '\t');
1977 } else {
1978 insert_spaces_tab (edit, 1);
1980 return;
1983 if (option_fill_tabs_with_spaces) {
1984 insert_spaces_tab (edit, 0);
1985 } else {
1986 edit_insert (edit, '\t');
1988 return;
1991 static void check_and_wrap_line (WEdit * edit)
1993 int curs, c;
1994 if (!option_typewriter_wrap)
1995 return;
1996 edit_update_curs_col (edit);
1997 if (edit->curs_col < option_word_wrap_line_length)
1998 return;
1999 curs = edit->curs1;
2000 for (;;) {
2001 curs--;
2002 c = edit_get_byte (edit, curs);
2003 if (c == '\n' || curs <= 0) {
2004 edit_insert (edit, '\n');
2005 return;
2007 if (c == ' ' || c == '\t') {
2008 int current = edit->curs1;
2009 edit_cursor_move (edit, curs - edit->curs1 + 1);
2010 edit_insert (edit, '\n');
2011 edit_cursor_move (edit, current - edit->curs1 + 1);
2012 return;
2017 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2019 void edit_push_key_press (WEdit * edit)
2021 edit_push_action (edit, KEY_PRESS + edit->start_display);
2022 if (edit->mark2 == -1)
2023 edit_push_action (edit, MARK_1 + edit->mark1);
2026 /* this find the matching bracket in either direction, and sets edit->bracket */
2027 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2029 const char * const b = "{}{[][()(", *p;
2030 int i = 1, a, inc = -1, c, d, n = 0;
2031 unsigned long j = 0;
2032 long q;
2033 edit_update_curs_row (edit);
2034 c = edit_get_byte (edit, edit->curs1);
2035 p = strchr (b, c);
2036 /* no limit */
2037 if (!furthest_bracket_search)
2038 furthest_bracket_search--;
2039 /* not on a bracket at all */
2040 if (!p)
2041 return -1;
2042 /* the matching bracket */
2043 d = p[1];
2044 /* going left or right? */
2045 if (strchr ("{[(", c))
2046 inc = 1;
2047 for (q = edit->curs1 + inc;; q += inc) {
2048 /* out of buffer? */
2049 if (q >= edit->last_byte || q < 0)
2050 break;
2051 a = edit_get_byte (edit, q);
2052 /* don't want to eat CPU */
2053 if (j++ > furthest_bracket_search)
2054 break;
2055 /* out of screen? */
2056 if (in_screen) {
2057 if (q < edit->start_display)
2058 break;
2059 /* count lines if searching downward */
2060 if (inc > 0 && a == '\n')
2061 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2062 break;
2064 /* count bracket depth */
2065 i += (a == c) - (a == d);
2066 /* return if bracket depth is zero */
2067 if (!i)
2068 return q;
2070 /* no match */
2071 return -1;
2074 static long last_bracket = -1;
2076 void edit_find_bracket (WEdit * edit)
2078 edit->bracket = edit_get_bracket (edit, 1, 10000);
2079 if (last_bracket != edit->bracket)
2080 edit->force |= REDRAW_PAGE;
2081 last_bracket = edit->bracket;
2084 static void edit_goto_matching_bracket (WEdit *edit)
2086 long q;
2087 q = edit_get_bracket (edit, 0, 0);
2088 if (q < 0)
2089 return;
2090 edit->bracket = edit->curs1;
2091 edit->force |= REDRAW_PAGE;
2092 edit_cursor_move (edit, q - edit->curs1);
2096 * This executes a command as though the user initiated it through a key
2097 * press. Callback with WIDGET_KEY as a message calls this after
2098 * translating the key press. This function can be used to pass any
2099 * command to the editor. Note that the screen wouldn't update
2100 * automatically. Either of command or char_for_insertion must be
2101 * passed as -1. Commands are executed, and char_for_insertion is
2102 * inserted at the cursor.
2104 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2106 if (command == CK_Begin_Record_Macro) {
2107 edit->macro_i = 0;
2108 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2109 return;
2111 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2112 edit->force |= REDRAW_COMPLETELY;
2113 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2114 edit->macro_i = -1;
2115 return;
2117 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2118 edit->macro[edit->macro_i].command = command;
2119 edit->macro[edit->macro_i++].ch = char_for_insertion;
2121 /* record the beginning of a set of editing actions initiated by a key press */
2122 if (command != CK_Undo && command != CK_Ext_Mode)
2123 edit_push_key_press (edit);
2125 edit_execute_cmd (edit, command, char_for_insertion);
2126 if (column_highlighting)
2127 edit->force |= REDRAW_PAGE;
2130 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2133 This executes a command at a lower level than macro recording.
2134 It also does not push a key_press onto the undo stack. This means
2135 that if it is called many times, a single undo command will undo
2136 all of them. It also does not check for the Undo command.
2138 void
2139 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2141 edit->force |= REDRAW_LINE;
2143 /* The next key press will unhighlight the found string, so update
2144 * the whole page */
2145 if (edit->found_len || column_highlighting)
2146 edit->force |= REDRAW_PAGE;
2148 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2149 column_highlighting = 0;
2150 if (!edit->highlight
2151 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2152 edit_mark_cmd (edit, 1); /* clear */
2153 edit_mark_cmd (edit, 0); /* marking on */
2155 edit->highlight = 1;
2156 } else { /* any other command */
2157 if (edit->highlight)
2158 edit_mark_cmd (edit, 0); /* clear */
2159 edit->highlight = 0;
2162 /* first check for undo */
2163 if (command == CK_Undo) {
2164 edit_do_undo (edit);
2165 edit->found_len = 0;
2166 edit->prev_col = edit_get_col (edit);
2167 edit->search_start = edit->curs1;
2168 return;
2171 /* An ordinary key press */
2172 if (char_for_insertion >= 0) {
2173 if (edit->overwrite) {
2174 if (edit_get_byte (edit, edit->curs1) != '\n')
2175 edit_delete (edit);
2177 edit_insert (edit, char_for_insertion);
2178 if (option_auto_para_formatting) {
2179 format_paragraph (edit, 0);
2180 edit->force |= REDRAW_PAGE;
2181 } else
2182 check_and_wrap_line (edit);
2183 edit->found_len = 0;
2184 edit->prev_col = edit_get_col (edit);
2185 edit->search_start = edit->curs1;
2186 edit_find_bracket (edit);
2187 return;
2189 switch (command) {
2190 case CK_Begin_Page:
2191 case CK_End_Page:
2192 case CK_Begin_Page_Highlight:
2193 case CK_End_Page_Highlight:
2194 case CK_Word_Left:
2195 case CK_Word_Right:
2196 case CK_Up:
2197 case CK_Down:
2198 case CK_Word_Left_Highlight:
2199 case CK_Word_Right_Highlight:
2200 case CK_Up_Highlight:
2201 case CK_Down_Highlight:
2202 if (edit->mark2 == -1)
2203 break; /*marking is following the cursor: may need to highlight a whole line */
2204 case CK_Left:
2205 case CK_Right:
2206 case CK_Left_Highlight:
2207 case CK_Right_Highlight:
2208 edit->force |= REDRAW_CHAR_ONLY;
2211 /* basic cursor key commands */
2212 switch (command) {
2213 case CK_BackSpace:
2214 if (option_backspace_through_tabs && is_in_indent (edit)) {
2215 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2216 && edit->curs1 > 0)
2217 edit_backspace (edit);
2218 break;
2219 } else {
2220 if (option_fake_half_tabs) {
2221 int i;
2222 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2223 for (i = 0; i < HALF_TAB_SIZE; i++)
2224 edit_backspace (edit);
2225 break;
2229 edit_backspace (edit);
2230 break;
2231 case CK_Delete:
2232 if (option_fake_half_tabs) {
2233 int i;
2234 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2235 for (i = 1; i <= HALF_TAB_SIZE; i++)
2236 edit_delete (edit);
2237 break;
2240 edit_delete (edit);
2241 break;
2242 case CK_Delete_Word_Left:
2243 edit_left_delete_word (edit);
2244 break;
2245 case CK_Delete_Word_Right:
2246 edit_right_delete_word (edit);
2247 break;
2248 case CK_Delete_Line:
2249 edit_delete_line (edit);
2250 break;
2251 case CK_Delete_To_Line_End:
2252 edit_delete_to_line_end (edit);
2253 break;
2254 case CK_Delete_To_Line_Begin:
2255 edit_delete_to_line_begin (edit);
2256 break;
2257 case CK_Enter:
2258 if (option_auto_para_formatting) {
2259 edit_double_newline (edit);
2260 if (option_return_does_auto_indent)
2261 edit_auto_indent (edit);
2262 format_paragraph (edit, 0);
2263 } else {
2264 edit_insert (edit, '\n');
2265 if (option_return_does_auto_indent) {
2266 edit_auto_indent (edit);
2269 break;
2270 case CK_Return:
2271 edit_insert (edit, '\n');
2272 break;
2274 case CK_Page_Up:
2275 case CK_Page_Up_Highlight:
2276 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2277 break;
2278 case CK_Page_Down:
2279 case CK_Page_Down_Highlight:
2280 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2281 break;
2282 case CK_Left:
2283 case CK_Left_Highlight:
2284 if (option_fake_half_tabs) {
2285 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2286 edit_cursor_move (edit, -HALF_TAB_SIZE);
2287 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2288 break;
2291 edit_cursor_move (edit, -1);
2292 break;
2293 case CK_Right:
2294 case CK_Right_Highlight:
2295 if (option_fake_half_tabs) {
2296 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2297 edit_cursor_move (edit, HALF_TAB_SIZE);
2298 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2299 break;
2302 edit_cursor_move (edit, 1);
2303 break;
2304 case CK_Begin_Page:
2305 case CK_Begin_Page_Highlight:
2306 edit_begin_page (edit);
2307 break;
2308 case CK_End_Page:
2309 case CK_End_Page_Highlight:
2310 edit_end_page (edit);
2311 break;
2312 case CK_Word_Left:
2313 case CK_Word_Left_Highlight:
2314 edit_left_word_move_cmd (edit);
2315 break;
2316 case CK_Word_Right:
2317 case CK_Word_Right_Highlight:
2318 edit_right_word_move_cmd (edit);
2319 break;
2320 case CK_Up:
2321 case CK_Up_Highlight:
2322 edit_move_up (edit, 1, 0);
2323 break;
2324 case CK_Down:
2325 case CK_Down_Highlight:
2326 edit_move_down (edit, 1, 0);
2327 break;
2328 case CK_Paragraph_Up:
2329 case CK_Paragraph_Up_Highlight:
2330 edit_move_up_paragraph (edit, 0);
2331 break;
2332 case CK_Paragraph_Down:
2333 case CK_Paragraph_Down_Highlight:
2334 edit_move_down_paragraph (edit, 0);
2335 break;
2336 case CK_Scroll_Up:
2337 case CK_Scroll_Up_Highlight:
2338 edit_move_up (edit, 1, 1);
2339 break;
2340 case CK_Scroll_Down:
2341 case CK_Scroll_Down_Highlight:
2342 edit_move_down (edit, 1, 1);
2343 break;
2344 case CK_Home:
2345 case CK_Home_Highlight:
2346 edit_cursor_to_bol (edit);
2347 break;
2348 case CK_End:
2349 case CK_End_Highlight:
2350 edit_cursor_to_eol (edit);
2351 break;
2353 case CK_Tab:
2354 edit_tab_cmd (edit);
2355 if (option_auto_para_formatting) {
2356 format_paragraph (edit, 0);
2357 edit->force |= REDRAW_PAGE;
2358 } else
2359 check_and_wrap_line (edit);
2360 break;
2362 case CK_Toggle_Insert:
2363 edit->overwrite = (edit->overwrite == 0);
2364 break;
2366 case CK_Mark:
2367 if (edit->mark2 >= 0) {
2368 if (column_highlighting)
2369 edit_push_action (edit, COLUMN_ON);
2370 column_highlighting = 0;
2372 edit_mark_cmd (edit, 0);
2373 break;
2374 case CK_Column_Mark:
2375 if (!column_highlighting)
2376 edit_push_action (edit, COLUMN_OFF);
2377 column_highlighting = 1;
2378 edit_mark_cmd (edit, 0);
2379 break;
2380 case CK_Unmark:
2381 if (column_highlighting)
2382 edit_push_action (edit, COLUMN_ON);
2383 column_highlighting = 0;
2384 edit_mark_cmd (edit, 1);
2385 break;
2387 case CK_Toggle_Bookmark:
2388 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2389 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2390 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2391 else
2392 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2393 break;
2394 case CK_Flush_Bookmarks:
2395 book_mark_flush (edit, BOOK_MARK_COLOR);
2396 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2397 edit->force |= REDRAW_PAGE;
2398 break;
2399 case CK_Next_Bookmark:
2400 if (edit->book_mark) {
2401 struct _book_mark *p;
2402 p = (struct _book_mark *) book_mark_find (edit,
2403 edit->curs_line);
2404 if (p->next) {
2405 p = p->next;
2406 if (p->line >= edit->start_line + edit->num_widget_lines
2407 || p->line < edit->start_line)
2408 edit_move_display (edit,
2409 p->line -
2410 edit->num_widget_lines / 2);
2411 edit_move_to_line (edit, p->line);
2414 break;
2415 case CK_Prev_Bookmark:
2416 if (edit->book_mark) {
2417 struct _book_mark *p;
2418 p = (struct _book_mark *) book_mark_find (edit,
2419 edit->curs_line);
2420 while (p->line == edit->curs_line)
2421 if (p->prev)
2422 p = p->prev;
2423 if (p->line >= 0) {
2424 if (p->line >= edit->start_line + edit->num_widget_lines
2425 || p->line < edit->start_line)
2426 edit_move_display (edit,
2427 p->line -
2428 edit->num_widget_lines / 2);
2429 edit_move_to_line (edit, p->line);
2432 break;
2434 case CK_Beginning_Of_Text:
2435 case CK_Beginning_Of_Text_Highlight:
2436 edit_move_to_top (edit);
2437 break;
2438 case CK_End_Of_Text:
2439 case CK_End_Of_Text_Highlight:
2440 edit_move_to_bottom (edit);
2441 break;
2443 case CK_Copy:
2444 edit_block_copy_cmd (edit);
2445 break;
2446 case CK_Remove:
2447 edit_block_delete_cmd (edit);
2448 break;
2449 case CK_Move:
2450 edit_block_move_cmd (edit);
2451 break;
2453 case CK_XStore:
2454 edit_copy_to_X_buf_cmd (edit);
2455 break;
2456 case CK_XCut:
2457 edit_cut_to_X_buf_cmd (edit);
2458 break;
2459 case CK_XPaste:
2460 edit_paste_from_X_buf_cmd (edit);
2461 break;
2462 case CK_Selection_History:
2463 edit_paste_from_history (edit);
2464 break;
2466 case CK_Save_As:
2467 edit_save_as_cmd (edit);
2468 break;
2469 case CK_Save:
2470 edit_save_confirm_cmd (edit);
2471 break;
2472 case CK_Load:
2473 edit_load_cmd (edit);
2474 break;
2475 case CK_Save_Block:
2476 edit_save_block_cmd (edit);
2477 break;
2478 case CK_Insert_File:
2479 edit_insert_file_cmd (edit);
2480 break;
2482 case CK_Toggle_Syntax:
2483 if ((option_syntax_highlighting ^= 1) == 1)
2484 edit_load_syntax (edit, NULL, option_syntax_type);
2485 edit->force |= REDRAW_PAGE;
2486 break;
2488 case CK_Find:
2489 edit_search_cmd (edit, 0);
2490 break;
2491 case CK_Find_Again:
2492 edit_search_cmd (edit, 1);
2493 break;
2494 case CK_Replace:
2495 edit_replace_cmd (edit, 0);
2496 break;
2497 case CK_Replace_Again:
2498 edit_replace_cmd (edit, 1);
2499 break;
2500 case CK_Complete_Word:
2501 edit_complete_word_cmd (edit);
2502 break;
2504 case CK_Exit:
2505 dlg_stop (edit->widget.parent);
2506 break;
2507 case CK_New:
2508 edit_new_cmd (edit);
2509 break;
2511 case CK_Help:
2512 edit_help_cmd (edit);
2513 break;
2515 case CK_Refresh:
2516 edit_refresh_cmd (edit);
2517 break;
2519 case CK_Date:{
2520 time_t t;
2521 #ifdef HAVE_STRFTIME
2522 char s[1024];
2523 /* fool gcc to prevent a Y2K warning */
2524 char time_format[] = "_c";
2525 time_format[0] = '%';
2526 #endif
2527 time (&t);
2528 #ifdef HAVE_STRFTIME
2529 strftime (s, sizeof (s), time_format, localtime (&t));
2530 edit_print_string (edit, s);
2531 #else
2532 edit_print_string (edit, ctime (&t));
2533 #endif
2534 edit->force |= REDRAW_PAGE;
2535 break;
2537 case CK_Goto:
2538 edit_goto_cmd (edit);
2539 break;
2540 case CK_Paragraph_Format:
2541 format_paragraph (edit, 1);
2542 edit->force |= REDRAW_PAGE;
2543 break;
2544 case CK_Delete_Macro:
2545 edit_delete_macro_cmd (edit);
2546 break;
2547 case CK_Match_Bracket:
2548 edit_goto_matching_bracket (edit);
2549 break;
2550 case CK_User_Menu:
2551 user_menu (edit);
2552 break;
2553 case CK_Sort:
2554 edit_sort_cmd (edit);
2555 break;
2556 case CK_ExtCmd:
2557 edit_ext_cmd (edit);
2558 break;
2559 case CK_Mail:
2560 edit_mail_dialog (edit);
2561 break;
2562 case CK_Shell:
2563 view_other_cmd ();
2564 break;
2565 case CK_Select_Codepage:
2566 edit_select_codepage_cmd (edit);
2567 break;
2568 case CK_Insert_Literal:
2569 edit_insert_literal_cmd (edit);
2570 break;
2571 case CK_Execute_Macro:
2572 edit_execute_macro_cmd (edit);
2573 break;
2574 case CK_Begin_End_Macro:
2575 edit_begin_end_macro_cmd (edit);
2576 break;
2577 case CK_Ext_Mode:
2578 edit->extmod = 1;
2579 break;
2580 default:
2581 break;
2584 /* CK_Pipe_Block */
2585 if ((command / 1000) == 1) /* a shell command */
2586 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2587 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2588 struct macro m[MAX_MACRO_LENGTH];
2589 int nm;
2590 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
2591 edit_execute_macro (edit, m, nm);
2594 /* keys which must set the col position, and the search vars */
2595 switch (command) {
2596 case CK_Find:
2597 case CK_Find_Again:
2598 case CK_Replace:
2599 case CK_Replace_Again:
2600 case CK_Complete_Word:
2601 edit->prev_col = edit_get_col (edit);
2602 break;
2603 case CK_Up:
2604 case CK_Up_Highlight:
2605 case CK_Down:
2606 case CK_Down_Highlight:
2607 case CK_Page_Up:
2608 case CK_Page_Up_Highlight:
2609 case CK_Page_Down:
2610 case CK_Page_Down_Highlight:
2611 case CK_Beginning_Of_Text:
2612 case CK_Beginning_Of_Text_Highlight:
2613 case CK_End_Of_Text:
2614 case CK_End_Of_Text_Highlight:
2615 case CK_Paragraph_Up:
2616 case CK_Paragraph_Up_Highlight:
2617 case CK_Paragraph_Down:
2618 case CK_Paragraph_Down_Highlight:
2619 case CK_Scroll_Up:
2620 case CK_Scroll_Up_Highlight:
2621 case CK_Scroll_Down:
2622 case CK_Scroll_Down_Highlight:
2623 edit->search_start = edit->curs1;
2624 edit->found_len = 0;
2625 break;
2626 default:
2627 edit->found_len = 0;
2628 edit->prev_col = edit_get_col (edit);
2629 edit->search_start = edit->curs1;
2631 edit_find_bracket (edit);
2633 if (option_auto_para_formatting) {
2634 switch (command) {
2635 case CK_BackSpace:
2636 case CK_Delete:
2637 case CK_Delete_Word_Left:
2638 case CK_Delete_Word_Right:
2639 case CK_Delete_To_Line_End:
2640 case CK_Delete_To_Line_Begin:
2641 format_paragraph (edit, 0);
2642 edit->force |= REDRAW_PAGE;
2648 static void
2649 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2651 int i = 0;
2653 if (edit->macro_depth++ > 256) {
2654 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2655 edit->macro_depth--;
2656 return;
2658 edit->force |= REDRAW_PAGE;
2659 for (; i < n; i++) {
2660 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2662 edit_update_screen (edit);
2663 edit->macro_depth--;
2666 /* User edit menu, like user menu (F2) but only in editor. */
2667 static void
2668 user_menu (WEdit * edit)
2670 FILE *fd;
2671 int nomark;
2672 struct stat status;
2673 long start_mark, end_mark;
2674 char *block_file = concat_dir_and_file (home_dir, BLOCK_FILE);
2675 int rc = 0;
2677 nomark = eval_marks (edit, &start_mark, &end_mark);
2678 if (!nomark) /* remember marked or not */
2679 edit_save_block (edit, block_file, start_mark, end_mark);
2681 /* run shell scripts from menu */
2682 user_menu_cmd (edit);
2684 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2685 /* no block messages */
2686 goto cleanup;
2689 if (!nomark) {
2690 /* i.e. we have marked block */
2691 rc = edit_block_delete_cmd (edit);
2694 if (!rc) {
2695 edit_insert_file (edit, block_file);
2698 /* truncate block file */
2699 if ((fd = fopen (block_file, "w"))) {
2700 fclose (fd);
2703 edit_refresh_cmd (edit);
2704 edit->force |= REDRAW_COMPLETELY;
2706 cleanup:
2707 g_free (block_file);