Updated italian translatio
[midnight-commander.git] / edit / edit.c
blobdb64f84409d6bb81ee62c315f7a0fafa6b4c2e04
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., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
23 #include <config.h>
24 #include "edit.h"
25 #include "editlock.h"
26 #include "edit-widget.h"
27 #include "editcmddef.h"
29 #include "../src/cmd.h" /* view_other_cmd() */
30 #include "../src/user.h" /* user_menu_cmd() */
31 #include "../src/wtools.h" /* query_dialog() */
34 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
35 or EDIT_KEY_EMULATION_EMACS
37 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
39 int option_word_wrap_line_length = 72;
40 int option_typewriter_wrap = 0;
41 int option_auto_para_formatting = 0;
42 int option_tab_spacing = 8;
43 int option_fill_tabs_with_spaces = 0;
44 int option_return_does_auto_indent = 1;
45 int option_backspace_through_tabs = 0;
46 int option_fake_half_tabs = 1;
47 int option_save_mode = EDIT_QUICK_SAVE;
48 int option_save_position = 1;
49 int option_backup_ext_int = -1;
50 int option_max_undo = 32768;
52 int option_edit_right_extreme = 0;
53 int option_edit_left_extreme = 0;
54 int option_edit_top_extreme = 0;
55 int option_edit_bottom_extreme = 0;
57 char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
58 char *option_backup_ext = "~";
62 * here's a quick sketch of the layout: (don't run this through indent.)
64 * (b1 is buffers1 and b2 is buffers2)
66 * |
67 * \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
68 * ______________________________________|______________________________________
69 * |
70 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
71 * |-> |-> |-> |-> |-> |-> |
72 * |
73 * _<------------------------->|<----------------->_
74 * WEdit->curs2 | WEdit->curs1
75 * ^ | ^
76 * | ^|^ |
77 * cursor ||| cursor
78 * |||
79 * file end|||file beginning
80 * |
81 * |
83 * _
84 * This_is_some_file
85 * fin.
91 static void edit_move_to_prev_col (WEdit *edit, long p);
92 static void user_menu (WEdit *edit);
94 #ifndef NO_INLINE_GETBYTE
96 int edit_get_byte (WEdit * edit, long byte_index)
98 unsigned long p;
99 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
100 return '\n';
102 if (byte_index >= edit->curs1) {
103 p = edit->curs1 + edit->curs2 - byte_index - 1;
104 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
105 } else {
106 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
110 #endif
114 * Initialize the buffers for an empty files.
116 static void
117 edit_init_buffers (WEdit *edit)
119 int j;
121 for (j = 0; j <= MAXBUFF; j++) {
122 edit->buffers1[j] = NULL;
123 edit->buffers2[j] = NULL;
126 edit->curs1 = 0;
127 edit->curs2 = 0;
128 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
132 * Load file OR text into buffers. Set cursor to the beginning of file.
133 * Return 1 on error.
135 static int
136 edit_load_file_fast (WEdit *edit, const char *filename)
138 long buf, buf2;
139 int file = -1;
141 edit->curs2 = edit->last_byte;
142 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
144 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
145 /* The file-name is printed after the ':' */
146 edit_error_dialog (_("Error"),
147 get_sys_error (catstrs
149 (" Cannot open file for reading: "),
150 filename, " ", 0)));
151 return 1;
154 if (!edit->buffers2[buf2])
155 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
157 mc_read (file,
158 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
159 (edit->curs2 & M_EDIT_BUF_SIZE),
160 edit->curs2 & M_EDIT_BUF_SIZE);
162 for (buf = buf2 - 1; buf >= 0; buf--) {
163 /* edit->buffers2[0] is already allocated */
164 if (!edit->buffers2[buf])
165 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
166 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
169 mc_close (file);
170 return 0;
173 /* detecting an error on save is easy: just check if every byte has been written. */
174 /* detecting an error on read, is not so easy 'cos there is not way to tell
175 whether you read everything or not. */
176 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
177 static const struct edit_filters {
178 char *read, *write, *extension;
179 } all_filters[] = {
182 "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2"
185 "gzip -cd %s 2>&1", "gzip > %s", ".gz"
188 "gzip -cd %s 2>&1", "gzip > %s", ".Z"
192 /* Return index of the filter or -1 is there is no appropriate filter */
193 static int edit_find_filter (const char *filename)
195 int i, l;
196 if (!filename)
197 return -1;
198 l = strlen (filename);
199 for (i = 0; i < sizeof (all_filters) / sizeof (struct edit_filters); i++) {
200 int e;
201 e = strlen (all_filters[i].extension);
202 if (l > e)
203 if (!strcmp (all_filters[i].extension, filename + l - e))
204 return i;
206 return -1;
209 static char *
210 edit_get_filter (const char *filename)
212 int i, l;
213 char *p, *quoted_name;
214 i = edit_find_filter (filename);
215 if (i < 0)
216 return 0;
217 quoted_name = name_quote (filename, 0);
218 l = strlen (quoted_name);
219 p = g_malloc (strlen (all_filters[i].read) + l + 2);
220 sprintf (p, all_filters[i].read, quoted_name);
221 g_free (quoted_name);
222 return p;
225 char *
226 edit_get_write_filter (const char *write_name, const char *filename)
228 int i, l;
229 char *p, *writename;
230 i = edit_find_filter (filename);
231 if (i < 0)
232 return 0;
233 writename = name_quote (write_name, 0);
234 l = strlen (writename);
235 p = g_malloc (strlen (all_filters[i].write) + l + 2);
236 sprintf (p, all_filters[i].write, writename);
237 g_free (writename);
238 return p;
241 static long
242 edit_insert_stream (WEdit * edit, FILE * f)
244 int c;
245 long i = 0;
246 while ((c = fgetc (f)) >= 0) {
247 edit_insert (edit, c);
248 i++;
250 return i;
253 long edit_write_stream (WEdit * edit, FILE * f)
255 long i;
256 for (i = 0; i < edit->last_byte; i++)
257 if (fputc (edit_get_byte (edit, i), f) < 0)
258 break;
259 return i;
262 #define TEMP_BUF_LEN 1024
264 /* inserts a file at the cursor, returns 1 on success */
266 edit_insert_file (WEdit *edit, const char *filename)
268 char *p;
269 if ((p = edit_get_filter (filename))) {
270 FILE *f;
271 long current = edit->curs1;
272 f = (FILE *) popen (p, "r");
273 if (f) {
274 edit_insert_stream (edit, f);
275 edit_cursor_move (edit, current - edit->curs1);
276 if (pclose (f) > 0) {
277 edit_error_dialog (_("Error"),
278 catstrs (_
279 (" Error reading from pipe: "),
280 p, " ", 0));
281 g_free (p);
282 return 0;
284 } else {
285 edit_error_dialog (_("Error"),
286 get_sys_error (catstrs
288 (" Cannot open pipe for reading: "),
289 p, " ", 0)));
290 g_free (p);
291 return 0;
293 g_free (p);
294 } else {
295 int i, file, blocklen;
296 long current = edit->curs1;
297 unsigned char *buf;
298 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
299 return 0;
300 buf = g_malloc (TEMP_BUF_LEN);
301 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
302 for (i = 0; i < blocklen; i++)
303 edit_insert (edit, buf[i]);
305 edit_cursor_move (edit, current - edit->curs1);
306 g_free (buf);
307 mc_close (file);
308 if (blocklen)
309 return 0;
311 return 1;
314 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
315 static int
316 check_file_access (WEdit *edit, const char *filename, struct stat *st)
318 int file;
320 /* Try opening an existing file */
321 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
323 if (file < 0) {
325 * Try creating the file. O_EXCL prevents following broken links
326 * and opening existing files.
328 file =
329 mc_open (filename,
330 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
331 0666);
332 if (file < 0) {
333 edit_error_dialog (_("Error"),
334 get_sys_error (catstrs
336 (" Cannot open file for reading: "),
337 filename, " ", 0)));
338 return 1;
339 } else {
340 /* New file, delete it if it's not modified or saved */
341 edit->delete_file = 1;
345 /* Check what we have opened */
346 if (mc_fstat (file, st) < 0) {
347 mc_close (file);
348 edit_error_dialog (_("Error"),
349 get_sys_error (catstrs
351 (" Cannot get size/permissions info for file: "),
352 filename, " ", 0)));
353 return 1;
356 /* We want to open regular files only */
357 if (!S_ISREG (st->st_mode)) {
358 mc_close (file);
359 edit_error_dialog (_("Error"),
360 catstrs (_(" Not an ordinary file: "), filename,
361 " ", 0));
362 return 1;
366 * Don't delete non-empty files.
367 * O_EXCL should prevent it, but let's be on the safe side.
369 if (st->st_size > 0) {
370 edit->delete_file = 0;
373 if (st->st_size >= SIZE_LIMIT) {
374 mc_close (file);
375 edit_error_dialog (_("Error"),
376 catstrs (_(" File is too large: "), filename,
377 0));
378 return 1;
381 mc_close (file);
382 return 0;
386 * Open the file and load it into the buffers, either directly or using
387 * a filter. Return 0 on success, 1 on error.
389 * Fast loading (edit_load_file_fast) is used when the file size is
390 * known. In this case the data is read into the buffers by blocks.
391 * If the file size is not known, the data is loaded byte by byte in
392 * edit_insert_file.
394 static int
395 edit_load_file (WEdit *edit)
397 int fast_load = 1;
399 /* Cannot do fast load if a filter is used */
400 if (edit_find_filter (edit->filename) >= 0)
401 fast_load = 0;
404 * VFS may report file size incorrectly, and slow load is not a big
405 * deal considering overhead in VFS.
407 if (!vfs_file_is_local (edit->filename))
408 fast_load = 0;
411 * FIXME: line end translation should disable fast loading as well
412 * Consider doing fseek() to the end and ftell() for the real size.
415 if (*edit->filename) {
416 /* If we are dealing with a real file, check that it exists */
417 if (check_file_access (edit, edit->filename, &edit->stat1))
418 return 1;
419 } else {
420 /* nothing to load */
421 fast_load = 0;
424 edit_init_buffers (edit);
426 if (fast_load) {
427 edit->last_byte = edit->stat1.st_size;
428 edit_load_file_fast (edit, edit->filename);
429 /* If fast load was used, the number of lines wasn't calculated */
430 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
431 } else {
432 edit->last_byte = 0;
433 if (*edit->filename) {
434 edit->stack_disable = 1;
435 if (!edit_insert_file (edit, edit->filename)) {
436 edit_clean (edit);
437 return 1;
439 edit->stack_disable = 0;
442 return 0;
445 /* Restore saved cursor position in the file */
446 static void
447 edit_load_position (WEdit *edit)
449 char *filename;
450 long line, column;
452 if (!edit->filename || !*edit->filename)
453 return;
455 filename = vfs_canon (edit->filename);
456 load_file_position (filename, &line, &column);
457 g_free (filename);
459 edit_move_to_line (edit, line - 1);
460 edit->prev_col = column;
461 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
462 edit_move_display (edit, line - (edit->num_widget_lines / 2));
465 /* Save cursor position in the file */
466 static void
467 edit_save_position (WEdit *edit)
469 char *filename;
471 if (!edit->filename || !*edit->filename)
472 return;
474 filename = vfs_canon (edit->filename);
475 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
476 g_free (filename);
479 /* Clean the WEdit stricture except the widget part */
480 static inline void
481 edit_purge_widget (WEdit *edit)
483 int len = sizeof (WEdit) - sizeof (Widget);
484 char *start = (char *) edit + sizeof (Widget);
485 memset (start, 0, len);
486 edit->macro_i = -1; /* not recording a macro */
489 #define space_width 1
492 * Fill in the edit structure. Return NULL on failure. Pass edit as
493 * NULL to allocate a new structure.
495 * If line is 0, try to restore saved position. Otherwise put the
496 * cursor on that line and show it in the middle of the screen.
498 WEdit *
499 edit_init (WEdit *edit, int lines, int columns, const char *filename,
500 long line)
502 int to_free = 0;
504 if (!edit) {
505 #ifdef ENABLE_NLS
507 * Expand option_whole_chars_search by national letters using
508 * current locale
511 static char option_whole_chars_search_buf[256];
513 if (option_whole_chars_search_buf != option_whole_chars_search) {
514 int i;
515 int len = strlen (option_whole_chars_search);
517 strcpy (option_whole_chars_search_buf,
518 option_whole_chars_search);
520 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
521 if (islower (i) && !strchr (option_whole_chars_search, i)) {
522 option_whole_chars_search_buf[len++] = i;
526 option_whole_chars_search_buf[len] = 0;
527 option_whole_chars_search = option_whole_chars_search_buf;
529 #endif /* ENABLE_NLS */
530 edit = g_malloc0 (sizeof (WEdit));
531 to_free = 1;
533 edit_purge_widget (edit);
534 edit->num_widget_lines = lines;
535 edit->num_widget_columns = columns;
536 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
537 edit->stat1.st_uid = getuid ();
538 edit->stat1.st_gid = getgid ();
539 edit->bracket = -1;
540 edit->force |= REDRAW_PAGE;
541 edit_set_filename (edit, filename);
542 edit->stack_size = START_STACK_SIZE;
543 edit->stack_size_mask = START_STACK_SIZE - 1;
544 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
545 if (edit_load_file (edit)) {
546 /* edit_load_file already gives an error message */
547 if (to_free)
548 g_free (edit);
549 return 0;
551 edit->loading_done = 1;
552 edit->modified = 0;
553 edit->locked = 0;
554 edit_load_syntax (edit, 0, 0);
556 int color;
557 edit_get_syntax_color (edit, -1, &color);
560 /* load saved cursor position */
561 if ((line == 0) && option_save_position) {
562 edit_load_position (edit);
563 } else {
564 if (line <= 0)
565 line = 1;
566 edit_move_display (edit, line - 1);
567 edit_move_to_line (edit, line - 1);
570 return edit;
573 /* Clear the edit struct, freeing everything in it. Return 1 on success */
575 edit_clean (WEdit *edit)
577 int j = 0;
579 if (!edit)
580 return 0;
582 /* a stale lock, remove it */
583 if (edit->locked)
584 edit->locked = edit_unlock_file (edit->filename);
586 /* save cursor position */
587 if (option_save_position)
588 edit_save_position (edit);
590 /* File specified on the mcedit command line and never saved */
591 if (edit->delete_file)
592 unlink (edit->filename);
594 edit_free_syntax_rules (edit);
595 book_mark_flush (edit, -1);
596 for (; j <= MAXBUFF; j++) {
597 if (edit->buffers1[j] != NULL)
598 g_free (edit->buffers1[j]);
599 if (edit->buffers2[j] != NULL)
600 g_free (edit->buffers2[j]);
603 g_free (edit->undo_stack);
604 g_free (edit->filename);
605 g_free (edit->dir);
607 edit_purge_widget (edit);
609 /* Free temporary strings used in catstrs() */
610 freestrs ();
612 return 1;
616 /* returns 1 on success */
617 int edit_renew (WEdit * edit)
619 int lines = edit->num_widget_lines;
620 int columns = edit->num_widget_columns;
621 int retval = 1;
623 edit_clean (edit);
624 if (!edit_init (edit, lines, columns, "", 0))
625 retval = 0;
626 return retval;
630 * Load a new file into the editor. If it fails, preserve the old file.
631 * To do it, allocate a new widget, initialize it and, if the new file
632 * was loaded, copy the data to the old widget.
633 * Return 1 on success, 0 on failure.
636 edit_reload (WEdit *edit, const char *filename)
638 WEdit *e;
639 int lines = edit->num_widget_lines;
640 int columns = edit->num_widget_columns;
642 e = g_malloc0 (sizeof (WEdit));
643 e->widget = edit->widget;
644 if (!edit_init (e, lines, columns, filename, 0)) {
645 g_free (e);
646 return 0;
648 edit_clean (edit);
649 memcpy (edit, e, sizeof (WEdit));
650 g_free (e);
651 return 1;
656 Recording stack for undo:
657 The following is an implementation of a compressed stack. Identical
658 pushes are recorded by a negative prefix indicating the number of times the
659 same char was pushed. This saves space for repeated curs-left or curs-right
660 delete etc.
664 pushed: stored:
668 b -3
670 c --> -4
676 If the stack long int is 0-255 it represents a normal insert (from a backspace),
677 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
678 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
679 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
680 position.
682 The only way the cursor moves or the buffer is changed is through the routines:
683 insert, backspace, insert_ahead, delete, and cursor_move.
684 These record the reverse undo movements onto the stack each time they are
685 called.
687 Each key press results in a set of actions (insert; delete ...). So each time
688 a key is pressed the current position of start_display is pushed as
689 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
690 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
691 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
695 void edit_push_action (WEdit * edit, long c,...)
697 unsigned long sp = edit->stack_pointer;
698 unsigned long spm1;
699 long *t;
700 /* first enlarge the stack if necessary */
701 if (sp > edit->stack_size - 10) { /* say */
702 if (option_max_undo < 256)
703 option_max_undo = 256;
704 if (edit->stack_size < option_max_undo) {
705 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
706 if (t) {
707 edit->undo_stack = t;
708 edit->stack_size <<= 1;
709 edit->stack_size_mask = edit->stack_size - 1;
713 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
714 if (edit->stack_disable)
715 return;
717 #ifdef FAST_MOVE_CURSOR
718 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
719 va_list ap;
720 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
721 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
722 va_start (ap, c);
723 c = -(va_arg (ap, int));
724 va_end (ap);
725 } else
726 #endif /* ! FAST_MOVE_CURSOR */
727 if (edit->stack_bottom != sp
728 && spm1 != edit->stack_bottom
729 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
730 int d;
731 if (edit->undo_stack[spm1] < 0) {
732 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
733 if (d == c) {
734 if (edit->undo_stack[spm1] > -1000000000) {
735 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
736 edit->undo_stack[spm1]--;
737 return;
740 /* #define NO_STACK_CURSMOVE_ANIHILATION */
741 #ifndef NO_STACK_CURSMOVE_ANIHILATION
742 else if ((c == CURS_LEFT && d == CURS_RIGHT)
743 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
744 if (edit->undo_stack[spm1] == -2)
745 edit->stack_pointer = spm1;
746 else
747 edit->undo_stack[spm1]++;
748 return;
750 #endif
751 } else {
752 d = edit->undo_stack[spm1];
753 if (d == c) {
754 if (c >= KEY_PRESS)
755 return; /* --> no need to push multiple do-nothings */
756 edit->undo_stack[sp] = -2;
757 goto check_bottom;
759 #ifndef NO_STACK_CURSMOVE_ANIHILATION
760 else if ((c == CURS_LEFT && d == CURS_RIGHT)
761 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
762 edit->stack_pointer = spm1;
763 return;
765 #endif
768 edit->undo_stack[sp] = c;
769 check_bottom:
771 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
773 /*if the sp wraps round and catches the stack_bottom then erase the first set of actions on the stack to make space - by moving stack_bottom forward one "key press" */
774 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
775 if (c == edit->stack_bottom || ((c + 1) & edit->stack_size_mask) == edit->stack_bottom)
776 do {
777 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
778 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
780 /*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: */
781 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
782 edit->stack_bottom = edit->stack_pointer = 0;
786 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
787 then the file should be as it was when he loaded up. Then set edit->modified to 0.
789 static long
790 pop_action (WEdit * edit)
792 long c;
793 unsigned long sp = edit->stack_pointer;
794 if (sp == edit->stack_bottom) {
795 return STACK_BOTTOM;
797 sp = (sp - 1) & edit->stack_size_mask;
798 if ((c = edit->undo_stack[sp]) >= 0) {
799 /* edit->undo_stack[sp] = '@'; */
800 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
801 return c;
803 if (sp == edit->stack_bottom) {
804 return STACK_BOTTOM;
806 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
807 if (edit->undo_stack[sp] == -2) {
808 /* edit->undo_stack[sp] = '@'; */
809 edit->stack_pointer = sp;
810 } else
811 edit->undo_stack[sp]++;
813 return c;
816 /* is called whenever a modification is made by one of the four routines below */
817 static inline void edit_modification (WEdit * edit)
819 edit->caches_valid = 0;
820 edit->screen_modified = 1;
822 /* raise lock when file modified */
823 if (!edit->modified && !edit->delete_file)
824 edit->locked = edit_lock_file (edit->filename);
825 edit->modified = 1;
829 Basic low level single character buffer alterations and movements at the cursor.
830 Returns char passed over, inserted or removed.
833 void
834 edit_insert (WEdit *edit, int c)
836 /* check if file has grown to large */
837 if (edit->last_byte >= SIZE_LIMIT)
838 return;
840 /* first we must update the position of the display window */
841 if (edit->curs1 < edit->start_display) {
842 edit->start_display++;
843 if (c == '\n')
844 edit->start_line++;
847 /* Mark file as modified, unless the file hasn't been fully loaded */
848 if (edit->loading_done) {
849 edit_modification (edit);
852 /* now we must update some info on the file and check if a redraw is required */
853 if (c == '\n') {
854 if (edit->book_mark)
855 book_mark_inc (edit, edit->curs_line);
856 edit->curs_line++;
857 edit->total_lines++;
858 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
861 /* save the reverse command onto the undo stack */
862 edit_push_action (edit, BACKSPACE);
864 /* update markers */
865 edit->mark1 += (edit->mark1 > edit->curs1);
866 edit->mark2 += (edit->mark2 > edit->curs1);
867 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
869 /* add a new buffer if we've reached the end of the last one */
870 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
871 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
872 g_malloc (EDIT_BUF_SIZE);
874 /* perform the insertion */
875 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
876 curs1 & M_EDIT_BUF_SIZE]
877 = (unsigned char) c;
879 /* update file length */
880 edit->last_byte++;
882 /* update cursor position */
883 edit->curs1++;
887 /* same as edit_insert and move left */
888 void edit_insert_ahead (WEdit * edit, int c)
890 if (edit->last_byte >= SIZE_LIMIT)
891 return;
892 if (edit->curs1 < edit->start_display) {
893 edit->start_display++;
894 if (c == '\n')
895 edit->start_line++;
897 edit_modification (edit);
898 if (c == '\n') {
899 if (edit->book_mark)
900 book_mark_inc (edit, edit->curs_line);
901 edit->total_lines++;
902 edit->force |= REDRAW_AFTER_CURSOR;
904 edit_push_action (edit, DELCHAR);
906 edit->mark1 += (edit->mark1 >= edit->curs1);
907 edit->mark2 += (edit->mark2 >= edit->curs1);
908 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
910 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
911 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
912 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
914 edit->last_byte++;
915 edit->curs2++;
919 int edit_delete (WEdit * edit)
921 int p;
922 if (!edit->curs2)
923 return 0;
925 edit->mark1 -= (edit->mark1 > edit->curs1);
926 edit->mark2 -= (edit->mark2 > edit->curs1);
927 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
929 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
931 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
932 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
933 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
935 edit->last_byte--;
936 edit->curs2--;
938 edit_modification (edit);
939 if (p == '\n') {
940 if (edit->book_mark)
941 book_mark_dec (edit, edit->curs_line);
942 edit->total_lines--;
943 edit->force |= REDRAW_AFTER_CURSOR;
945 edit_push_action (edit, p + 256);
946 if (edit->curs1 < edit->start_display) {
947 edit->start_display--;
948 if (p == '\n')
949 edit->start_line--;
952 return p;
956 static int
957 edit_backspace (WEdit * edit)
959 int p;
960 if (!edit->curs1)
961 return 0;
963 edit->mark1 -= (edit->mark1 >= edit->curs1);
964 edit->mark2 -= (edit->mark2 >= edit->curs1);
965 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
967 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
968 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
969 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
970 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
972 edit->last_byte--;
973 edit->curs1--;
975 edit_modification (edit);
976 if (p == '\n') {
977 if (edit->book_mark)
978 book_mark_dec (edit, edit->curs_line);
979 edit->curs_line--;
980 edit->total_lines--;
981 edit->force |= REDRAW_AFTER_CURSOR;
983 edit_push_action (edit, p);
985 if (edit->curs1 < edit->start_display) {
986 edit->start_display--;
987 if (p == '\n')
988 edit->start_line--;
991 return p;
994 #ifdef FAST_MOVE_CURSOR
996 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
998 unsigned long next;
999 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1000 edit->curs_line--;
1001 next -= (unsigned long) dest;
1002 n -= next;
1003 src += next;
1004 dest += next;
1009 edit_move_backward_lots (WEdit *edit, long increment)
1011 int r, s, t;
1012 unsigned char *p;
1014 if (increment > edit->curs1)
1015 increment = edit->curs1;
1016 if (increment <= 0)
1017 return -1;
1018 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1020 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1021 if (r > increment)
1022 r = increment;
1023 s = edit->curs1 & M_EDIT_BUF_SIZE;
1025 p = 0;
1026 if (s > r) {
1027 memqcpy (edit,
1028 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1029 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1031 } else {
1032 if (s) {
1033 memqcpy (edit,
1034 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1035 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1036 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1037 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1039 memqcpy (edit,
1040 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1041 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1042 EDIT_BUF_SIZE - (r - s), r - s);
1044 increment -= r;
1045 edit->curs1 -= r;
1046 edit->curs2 += r;
1047 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1048 if (p)
1049 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1050 else
1051 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1052 g_malloc (EDIT_BUF_SIZE);
1053 } else {
1054 if (p)
1055 g_free (p);
1058 s = edit->curs1 & M_EDIT_BUF_SIZE;
1059 while (increment) {
1060 p = 0;
1061 r = EDIT_BUF_SIZE;
1062 if (r > increment)
1063 r = increment;
1064 t = s;
1065 if (r < t)
1066 t = r;
1067 memqcpy (edit,
1068 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1069 EDIT_BUF_SIZE - t,
1070 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1072 if (r >= s) {
1073 if (t) {
1074 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1075 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1077 memqcpy (edit,
1078 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1079 EDIT_BUF_SIZE - r,
1080 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1081 EDIT_BUF_SIZE - (r - s), r - s);
1083 increment -= r;
1084 edit->curs1 -= r;
1085 edit->curs2 += r;
1086 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1087 if (p)
1088 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1089 else
1090 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1091 g_malloc (EDIT_BUF_SIZE);
1092 } else {
1093 g_free (p);
1096 return edit_get_byte (edit, edit->curs1);
1099 #endif /* ! FAST_MOVE_CURSOR */
1101 /* moves the cursor right or left: increment positive or negative respectively */
1102 int edit_cursor_move (WEdit * edit, long increment)
1104 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1105 int c;
1107 #ifdef FAST_MOVE_CURSOR
1108 if (increment < -256) {
1109 edit->force |= REDRAW_PAGE;
1110 return edit_move_backward_lots (edit, -increment);
1112 #endif /* ! FAST_MOVE_CURSOR */
1114 if (increment < 0) {
1115 for (; increment < 0; increment++) {
1116 if (!edit->curs1)
1117 return -1;
1119 edit_push_action (edit, CURS_RIGHT);
1121 c = edit_get_byte (edit, edit->curs1 - 1);
1122 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1123 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1124 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1125 edit->curs2++;
1126 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1127 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1128 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1129 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1131 edit->curs1--;
1132 if (c == '\n') {
1133 edit->curs_line--;
1134 edit->force |= REDRAW_LINE_BELOW;
1138 return c;
1139 } else if (increment > 0) {
1140 for (; increment > 0; increment--) {
1141 if (!edit->curs2)
1142 return -2;
1144 edit_push_action (edit, CURS_LEFT);
1146 c = edit_get_byte (edit, edit->curs1);
1147 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1148 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1149 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1150 edit->curs1++;
1151 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1152 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1153 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1154 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1156 edit->curs2--;
1157 if (c == '\n') {
1158 edit->curs_line++;
1159 edit->force |= REDRAW_LINE_ABOVE;
1162 return c;
1163 } else
1164 return -3;
1167 /* These functions return positions relative to lines */
1169 /* returns index of last char on line + 1 */
1170 long edit_eol (WEdit * edit, long current)
1172 if (current < edit->last_byte) {
1173 for (;; current++)
1174 if (edit_get_byte (edit, current) == '\n')
1175 break;
1176 } else
1177 return edit->last_byte;
1178 return current;
1181 /* returns index of first char on line */
1182 long edit_bol (WEdit * edit, long current)
1184 if (current > 0) {
1185 for (;; current--)
1186 if (edit_get_byte (edit, current - 1) == '\n')
1187 break;
1188 } else
1189 return 0;
1190 return current;
1194 int edit_count_lines (WEdit * edit, long current, int upto)
1196 int lines = 0;
1197 if (upto > edit->last_byte)
1198 upto = edit->last_byte;
1199 if (current < 0)
1200 current = 0;
1201 while (current < upto)
1202 if (edit_get_byte (edit, current++) == '\n')
1203 lines++;
1204 return lines;
1208 /* If lines is zero this returns the count of lines from current to upto. */
1209 /* If upto is zero returns index of lines forward current. */
1210 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1212 if (upto) {
1213 return edit_count_lines (edit, current, upto);
1214 } else {
1215 int next;
1216 if (lines < 0)
1217 lines = 0;
1218 while (lines--) {
1219 next = edit_eol (edit, current) + 1;
1220 if (next > edit->last_byte)
1221 break;
1222 else
1223 current = next;
1225 return current;
1230 /* Returns offset of 'lines' lines up from current */
1231 long edit_move_backward (WEdit * edit, long current, int lines)
1233 if (lines < 0)
1234 lines = 0;
1235 current = edit_bol (edit, current);
1236 while((lines--) && current != 0)
1237 current = edit_bol (edit, current - 1);
1238 return current;
1241 /* If cols is zero this returns the count of columns from current to upto. */
1242 /* If upto is zero returns index of cols across from current. */
1243 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1245 long p, q;
1246 int col = 0;
1248 if (upto) {
1249 q = upto;
1250 cols = -10;
1251 } else
1252 q = edit->last_byte + 2;
1254 for (col = 0, p = current; p < q; p++) {
1255 int c;
1256 if (cols != -10) {
1257 if (col == cols)
1258 return p;
1259 if (col > cols)
1260 return p - 1;
1262 c = edit_get_byte (edit, p);
1263 if (c == '\t')
1264 col += TAB_SIZE - col % TAB_SIZE;
1265 else if (c == '\n') {
1266 if (upto)
1267 return col;
1268 else
1269 return p;
1270 } else if (c < 32 || c == 127)
1271 col += 2; /* Caret notation for control characters */
1272 else
1273 col++;
1275 return col;
1278 /* returns the current column position of the cursor */
1279 int edit_get_col (WEdit * edit)
1281 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1285 /* Scrolling functions */
1287 void edit_update_curs_row (WEdit * edit)
1289 edit->curs_row = edit->curs_line - edit->start_line;
1292 void edit_update_curs_col (WEdit * edit)
1294 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1297 /*moves the display start position up by i lines */
1298 void edit_scroll_upward (WEdit * edit, unsigned long i)
1300 int lines_above = edit->start_line;
1301 if (i > lines_above)
1302 i = lines_above;
1303 if (i) {
1304 edit->start_line -= i;
1305 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1306 edit->force |= REDRAW_PAGE;
1307 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1309 edit_update_curs_row (edit);
1313 /* returns 1 if could scroll, 0 otherwise */
1314 void edit_scroll_downward (WEdit * edit, int i)
1316 int lines_below;
1317 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1318 if (lines_below > 0) {
1319 if (i > lines_below)
1320 i = lines_below;
1321 edit->start_line += i;
1322 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1323 edit->force |= REDRAW_PAGE;
1324 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1326 edit_update_curs_row (edit);
1329 void edit_scroll_right (WEdit * edit, int i)
1331 edit->force |= REDRAW_PAGE;
1332 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1333 edit->start_col -= i;
1336 void edit_scroll_left (WEdit * edit, int i)
1338 if (edit->start_col) {
1339 edit->start_col += i;
1340 if (edit->start_col > 0)
1341 edit->start_col = 0;
1342 edit->force |= REDRAW_PAGE;
1343 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1347 /* high level cursor movement commands */
1349 static int is_in_indent (WEdit *edit)
1351 long p = edit_bol (edit, edit->curs1);
1352 while (p < edit->curs1)
1353 if (!strchr (" \t", edit_get_byte (edit, p++)))
1354 return 0;
1355 return 1;
1358 static int left_of_four_spaces (WEdit *edit);
1360 static void
1361 edit_move_to_prev_col (WEdit * edit, long p)
1363 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1365 if (is_in_indent (edit) && option_fake_half_tabs) {
1366 edit_update_curs_col (edit);
1367 if (space_width)
1368 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1369 int q = edit->curs_col;
1370 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1371 p = edit_bol (edit, edit->curs1);
1372 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1373 if (!left_of_four_spaces (edit))
1374 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1380 /* move i lines */
1381 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1383 long p, l = edit->curs_line;
1385 if (i > l)
1386 i = l;
1387 if (i) {
1388 if (i > 1)
1389 edit->force |= REDRAW_PAGE;
1390 if (scroll)
1391 edit_scroll_upward (edit, i);
1393 p = edit_bol (edit, edit->curs1);
1394 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1395 edit_move_to_prev_col (edit, p);
1397 edit->search_start = edit->curs1;
1398 edit->found_len = 0;
1402 static int
1403 is_blank (WEdit *edit, long offset)
1405 long s, f;
1406 int c;
1407 s = edit_bol (edit, offset);
1408 f = edit_eol (edit, offset) - 1;
1409 while (s <= f) {
1410 c = edit_get_byte (edit, s++);
1411 if (!isspace (c))
1412 return 0;
1414 return 1;
1418 /* returns the offset of line i */
1419 static long
1420 edit_find_line (WEdit *edit, int line)
1422 int i, j = 0;
1423 int m = 2000000000;
1424 if (!edit->caches_valid) {
1425 for (i = 0; i < N_LINE_CACHES; i++)
1426 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1427 /* three offsets that we *know* are line 0 at 0 and these two: */
1428 edit->line_numbers[1] = edit->curs_line;
1429 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1430 edit->line_numbers[2] = edit->total_lines;
1431 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1432 edit->caches_valid = 1;
1434 if (line >= edit->total_lines)
1435 return edit->line_offsets[2];
1436 if (line <= 0)
1437 return 0;
1438 /* find the closest known point */
1439 for (i = 0; i < N_LINE_CACHES; i++) {
1440 int n;
1441 n = abs (edit->line_numbers[i] - line);
1442 if (n < m) {
1443 m = n;
1444 j = i;
1447 if (m == 0)
1448 return edit->line_offsets[j]; /* know the offset exactly */
1449 if (m == 1 && j >= 3)
1450 i = j; /* one line different - caller might be looping, so stay in this cache */
1451 else
1452 i = 3 + (rand () % (N_LINE_CACHES - 3));
1453 if (line > edit->line_numbers[j])
1454 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1455 else
1456 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1457 edit->line_numbers[i] = line;
1458 return edit->line_offsets[i];
1461 int line_is_blank (WEdit * edit, long line)
1463 return is_blank (edit, edit_find_line (edit, line));
1466 /* moves up until a blank line is reached, or until just
1467 before a non-blank line is reached */
1468 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1470 int i;
1471 if (edit->curs_line <= 1) {
1472 i = 0;
1473 } else {
1474 if (line_is_blank (edit, edit->curs_line)) {
1475 if (line_is_blank (edit, edit->curs_line - 1)) {
1476 for (i = edit->curs_line - 1; i; i--)
1477 if (!line_is_blank (edit, i)) {
1478 i++;
1479 break;
1481 } else {
1482 for (i = edit->curs_line - 1; i; i--)
1483 if (line_is_blank (edit, i))
1484 break;
1486 } else {
1487 for (i = edit->curs_line - 1; i; i--)
1488 if (line_is_blank (edit, i))
1489 break;
1492 edit_move_up (edit, edit->curs_line - i, scroll);
1495 /* move i lines */
1496 void edit_move_down (WEdit * edit, int i, int scroll)
1498 long p, l = edit->total_lines - edit->curs_line;
1500 if (i > l)
1501 i = l;
1502 if (i) {
1503 if (i > 1)
1504 edit->force |= REDRAW_PAGE;
1505 if (scroll)
1506 edit_scroll_downward (edit, i);
1507 p = edit_bol (edit, edit->curs1);
1508 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1509 edit_move_to_prev_col (edit, p);
1511 edit->search_start = edit->curs1;
1512 edit->found_len = 0;
1516 /* moves down until a blank line is reached, or until just
1517 before a non-blank line is reached */
1518 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1520 int i;
1521 if (edit->curs_line >= edit->total_lines - 1) {
1522 i = edit->total_lines;
1523 } else {
1524 if (line_is_blank (edit, edit->curs_line)) {
1525 if (line_is_blank (edit, edit->curs_line + 1)) {
1526 for (i = edit->curs_line + 1; i; i++)
1527 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1528 i--;
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;
1536 } else {
1537 for (i = edit->curs_line + 1; i; i++)
1538 if (line_is_blank (edit, i) || i >= edit->total_lines)
1539 break;
1542 edit_move_down (edit, i - edit->curs_line, scroll);
1545 static void edit_begin_page (WEdit *edit)
1547 edit_update_curs_row (edit);
1548 edit_move_up (edit, edit->curs_row, 0);
1551 static void edit_end_page (WEdit *edit)
1553 edit_update_curs_row (edit);
1554 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1558 /* goto beginning of text */
1559 static void edit_move_to_top (WEdit * edit)
1561 if (edit->curs_line) {
1562 edit_cursor_move (edit, -edit->curs1);
1563 edit_move_to_prev_col (edit, 0);
1564 edit->force |= REDRAW_PAGE;
1565 edit->search_start = 0;
1566 edit_update_curs_row(edit);
1571 /* goto end of text */
1572 static void edit_move_to_bottom (WEdit * edit)
1574 if (edit->curs_line < edit->total_lines) {
1575 edit_cursor_move (edit, edit->curs2);
1576 edit->start_display = edit->last_byte;
1577 edit->start_line = edit->total_lines;
1578 edit_update_curs_row(edit);
1579 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1580 edit->force |= REDRAW_PAGE;
1584 /* goto beginning of line */
1585 static void edit_cursor_to_bol (WEdit * edit)
1587 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1588 edit->search_start = edit->curs1;
1589 edit->prev_col = edit_get_col (edit);
1592 /* goto end of line */
1593 static void edit_cursor_to_eol (WEdit * edit)
1595 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1596 edit->search_start = edit->curs1;
1597 edit->prev_col = edit_get_col (edit);
1600 /* move cursor to line 'line' */
1601 void edit_move_to_line (WEdit * e, long line)
1603 if(line < e->curs_line)
1604 edit_move_up (e, e->curs_line - line, 0);
1605 else
1606 edit_move_down (e, line - e->curs_line, 0);
1607 edit_scroll_screen_over_cursor (e);
1610 /* scroll window so that first visible line is 'line' */
1611 void edit_move_display (WEdit * e, long line)
1613 if(line < e->start_line)
1614 edit_scroll_upward (e, e->start_line - line);
1615 else
1616 edit_scroll_downward (e, line - e->start_line);
1619 /* save markers onto undo stack */
1620 void edit_push_markers (WEdit * edit)
1622 edit_push_action (edit, MARK_1 + edit->mark1);
1623 edit_push_action (edit, MARK_2 + edit->mark2);
1626 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1628 edit->mark1 = m1;
1629 edit->mark2 = m2;
1630 edit->column1 = c1;
1631 edit->column2 = c2;
1635 /* highlight marker toggle */
1636 void edit_mark_cmd (WEdit * edit, int unmark)
1638 edit_push_markers (edit);
1639 if (unmark) {
1640 edit_set_markers (edit, 0, 0, 0, 0);
1641 edit->force |= REDRAW_PAGE;
1642 } else {
1643 if (edit->mark2 >= 0) {
1644 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1645 edit->force |= REDRAW_PAGE;
1646 } else
1647 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1651 static unsigned long
1652 my_type_of (int c)
1654 int x, r = 0;
1655 const char *p, *q;
1656 const char option_chars_move_whole_word[] =
1657 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1659 if (!c)
1660 return 0;
1661 if (c == '!') {
1662 if (*option_chars_move_whole_word == '!')
1663 return 2;
1664 return 0x80000000UL;
1666 if (isupper (c))
1667 c = 'A';
1668 else if (islower (c))
1669 c = 'a';
1670 else if (isalpha (c))
1671 c = 'a';
1672 else if (isdigit (c))
1673 c = '0';
1674 else if (isspace (c))
1675 c = ' ';
1676 q = strchr (option_chars_move_whole_word, c);
1677 if (!q)
1678 return 0xFFFFFFFFUL;
1679 do {
1680 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1681 if (*p == '!')
1682 x <<= 1;
1683 r |= x;
1684 } while ((q = strchr (q + 1, c)));
1685 return r;
1688 static void
1689 edit_left_word_move (WEdit *edit, int s)
1691 for (;;) {
1692 int c1, c2;
1693 edit_cursor_move (edit, -1);
1694 if (!edit->curs1)
1695 break;
1696 c1 = edit_get_byte (edit, edit->curs1 - 1);
1697 c2 = edit_get_byte (edit, edit->curs1);
1698 if (!(my_type_of (c1) & my_type_of (c2)))
1699 break;
1700 if (isspace (c1) && !isspace (c2))
1701 break;
1702 if (s)
1703 if (!isspace (c1) && isspace (c2))
1704 break;
1708 static void edit_left_word_move_cmd (WEdit * edit)
1710 edit_left_word_move (edit, 0);
1711 edit->force |= REDRAW_PAGE;
1714 static void
1715 edit_right_word_move (WEdit *edit, int s)
1717 for (;;) {
1718 int c1, c2;
1719 edit_cursor_move (edit, 1);
1720 if (edit->curs1 >= edit->last_byte)
1721 break;
1722 c1 = edit_get_byte (edit, edit->curs1 - 1);
1723 c2 = edit_get_byte (edit, edit->curs1);
1724 if (!(my_type_of (c1) & my_type_of (c2)))
1725 break;
1726 if (isspace (c1) && !isspace (c2))
1727 break;
1728 if (s)
1729 if (!isspace (c1) && isspace (c2))
1730 break;
1734 static void edit_right_word_move_cmd (WEdit * edit)
1736 edit_right_word_move (edit, 0);
1737 edit->force |= REDRAW_PAGE;
1741 static void edit_right_delete_word (WEdit * edit)
1743 int c1, c2;
1744 for (;;) {
1745 if (edit->curs1 >= edit->last_byte)
1746 break;
1747 c1 = edit_delete (edit);
1748 c2 = edit_get_byte (edit, edit->curs1);
1749 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1750 break;
1751 if (!(my_type_of (c1) & my_type_of (c2)))
1752 break;
1756 static void edit_left_delete_word (WEdit * edit)
1758 int c1, c2;
1759 for (;;) {
1760 if (edit->curs1 <= 0)
1761 break;
1762 c1 = edit_backspace (edit);
1763 c2 = edit_get_byte (edit, edit->curs1 - 1);
1764 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1765 break;
1766 if (!(my_type_of (c1) & my_type_of (c2)))
1767 break;
1772 the start column position is not recorded, and hence does not
1773 undo as it happed. But who would notice.
1775 static void
1776 edit_do_undo (WEdit * edit)
1778 long ac;
1779 long count = 0;
1781 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1783 while ((ac = pop_action (edit)) < KEY_PRESS) {
1784 switch ((int) ac) {
1785 case STACK_BOTTOM:
1786 goto done_undo;
1787 case CURS_RIGHT:
1788 edit_cursor_move (edit, 1);
1789 break;
1790 case CURS_LEFT:
1791 edit_cursor_move (edit, -1);
1792 break;
1793 case BACKSPACE:
1794 edit_backspace (edit);
1795 break;
1796 case DELCHAR:
1797 edit_delete (edit);
1798 break;
1799 case COLUMN_ON:
1800 column_highlighting = 1;
1801 break;
1802 case COLUMN_OFF:
1803 column_highlighting = 0;
1804 break;
1806 if (ac >= 256 && ac < 512)
1807 edit_insert_ahead (edit, ac - 256);
1808 if (ac >= 0 && ac < 256)
1809 edit_insert (edit, ac);
1811 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1812 edit->mark1 = ac - MARK_1;
1813 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1814 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1815 edit->mark2 = ac - MARK_2;
1816 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1818 if (count++)
1819 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1822 if (edit->start_display > ac - KEY_PRESS) {
1823 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1824 edit->force |= REDRAW_PAGE;
1825 } else if (edit->start_display < ac - KEY_PRESS) {
1826 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1827 edit->force |= REDRAW_PAGE;
1829 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1830 edit_update_curs_row (edit);
1832 done_undo:;
1833 edit->stack_disable = 0;
1836 static void edit_delete_to_line_end (WEdit * edit)
1838 while (edit_get_byte (edit, edit->curs1) != '\n') {
1839 if (!edit->curs2)
1840 break;
1841 edit_delete (edit);
1845 static void edit_delete_to_line_begin (WEdit * edit)
1847 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1848 if (!edit->curs1)
1849 break;
1850 edit_backspace (edit);
1854 void
1855 edit_delete_line (WEdit *edit)
1858 * Delete right part of the line.
1859 * Note that edit_get_byte() returns '\n' when byte position is
1860 * beyond EOF.
1862 while (edit_get_byte (edit, edit->curs1) != '\n') {
1863 (void) edit_delete (edit);
1867 * Delete '\n' char.
1868 * Note that edit_delete() will not corrupt anything if called while
1869 * cursor position is EOF.
1871 (void) edit_delete (edit);
1874 * Delete left part of the line.
1875 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1877 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1878 (void) edit_backspace (edit);
1882 static void insert_spaces_tab (WEdit * edit, int half)
1884 int i;
1885 edit_update_curs_col (edit);
1886 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1887 while (i > 0) {
1888 edit_insert (edit, ' ');
1889 i -= space_width;
1893 static int is_aligned_on_a_tab (WEdit * edit)
1895 edit_update_curs_col (edit);
1896 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1897 return 0; /* not alligned on a tab */
1898 return 1;
1901 static int right_of_four_spaces (WEdit *edit)
1903 int i, ch = 0;
1904 for (i = 1; i <= HALF_TAB_SIZE; i++)
1905 ch |= edit_get_byte (edit, edit->curs1 - i);
1906 if (ch == ' ')
1907 return is_aligned_on_a_tab (edit);
1908 return 0;
1911 static int left_of_four_spaces (WEdit *edit)
1913 int i, ch = 0;
1914 for (i = 0; i < HALF_TAB_SIZE; i++)
1915 ch |= edit_get_byte (edit, edit->curs1 + i);
1916 if (ch == ' ')
1917 return is_aligned_on_a_tab (edit);
1918 return 0;
1921 int edit_indent_width (WEdit * edit, long p)
1923 long q = p;
1924 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1925 q++;
1926 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1929 void edit_insert_indent (WEdit * edit, int indent)
1931 if (!option_fill_tabs_with_spaces) {
1932 while (indent >= TAB_SIZE) {
1933 edit_insert (edit, '\t');
1934 indent -= TAB_SIZE;
1937 while (indent-- > 0)
1938 edit_insert (edit, ' ');
1941 static void
1942 edit_auto_indent (WEdit * edit, int extra, int no_advance)
1944 long p;
1945 int indent;
1946 p = edit->curs1;
1947 while (isspace (edit_get_byte (edit, p - 1)) && p > 0) /* move back/up to a line with text */
1948 p--;
1949 indent = edit_indent_width (edit, edit_bol (edit, p));
1950 if (edit->curs_col < indent && no_advance)
1951 indent = edit->curs_col;
1952 edit_insert_indent (edit, indent + (option_fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE) * space_width * extra);
1955 static void edit_double_newline (WEdit * edit)
1957 edit_insert (edit, '\n');
1958 if (edit_get_byte (edit, edit->curs1) == '\n')
1959 return;
1960 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1961 return;
1962 edit->force |= REDRAW_PAGE;
1963 edit_insert (edit, '\n');
1966 static void edit_tab_cmd (WEdit * edit)
1968 int i;
1970 if (option_fake_half_tabs) {
1971 if (is_in_indent (edit)) {
1972 /*insert a half tab (usually four spaces) unless there is a
1973 half tab already behind, then delete it and insert a
1974 full tab. */
1975 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1976 for (i = 1; i <= HALF_TAB_SIZE; i++)
1977 edit_backspace (edit);
1978 edit_insert (edit, '\t');
1979 } else {
1980 insert_spaces_tab (edit, 1);
1982 return;
1985 if (option_fill_tabs_with_spaces) {
1986 insert_spaces_tab (edit, 0);
1987 } else {
1988 edit_insert (edit, '\t');
1990 return;
1993 static void check_and_wrap_line (WEdit * edit)
1995 int curs, c;
1996 if (!option_typewriter_wrap)
1997 return;
1998 edit_update_curs_col (edit);
1999 if (edit->curs_col < option_word_wrap_line_length)
2000 return;
2001 curs = edit->curs1;
2002 for (;;) {
2003 curs--;
2004 c = edit_get_byte (edit, curs);
2005 if (c == '\n' || curs <= 0) {
2006 edit_insert (edit, '\n');
2007 return;
2009 if (c == ' ' || c == '\t') {
2010 int current = edit->curs1;
2011 edit_cursor_move (edit, curs - edit->curs1 + 1);
2012 edit_insert (edit, '\n');
2013 edit_cursor_move (edit, current - edit->curs1 + 1);
2014 return;
2019 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2021 void edit_push_key_press (WEdit * edit)
2023 edit_push_action (edit, KEY_PRESS + edit->start_display);
2024 if (edit->mark2 == -1)
2025 edit_push_action (edit, MARK_1 + edit->mark1);
2028 /* this find the matching bracket in either direction, and sets edit->bracket */
2029 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2031 const char * const b = "{}{[][()(", *p;
2032 int i = 1, a, inc = -1, c, d, n = 0;
2033 unsigned long j = 0;
2034 long q;
2035 edit_update_curs_row (edit);
2036 c = edit_get_byte (edit, edit->curs1);
2037 p = strchr (b, c);
2038 /* no limit */
2039 if (!furthest_bracket_search)
2040 furthest_bracket_search--;
2041 /* not on a bracket at all */
2042 if (!p)
2043 return -1;
2044 /* the matching bracket */
2045 d = p[1];
2046 /* going left or right? */
2047 if (strchr ("{[(", c))
2048 inc = 1;
2049 for (q = edit->curs1 + inc;; q += inc) {
2050 /* out of buffer? */
2051 if (q >= edit->last_byte || q < 0)
2052 break;
2053 a = edit_get_byte (edit, q);
2054 /* don't want to eat CPU */
2055 if (j++ > furthest_bracket_search)
2056 break;
2057 /* out of screen? */
2058 if (in_screen) {
2059 if (q < edit->start_display)
2060 break;
2061 /* count lines if searching downward */
2062 if (inc > 0 && a == '\n')
2063 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2064 break;
2066 /* count bracket depth */
2067 i += (a == c) - (a == d);
2068 /* return if bracket depth is zero */
2069 if (!i)
2070 return q;
2072 /* no match */
2073 return -1;
2076 static long last_bracket = -1;
2078 static void edit_find_bracket (WEdit * edit)
2080 edit->bracket = edit_get_bracket (edit, 1, 10000);
2081 if (last_bracket != edit->bracket)
2082 edit->force |= REDRAW_PAGE;
2083 last_bracket = edit->bracket;
2086 static void edit_goto_matching_bracket (WEdit *edit)
2088 long q;
2089 q = edit_get_bracket (edit, 0, 0);
2090 if (q < 0)
2091 return;
2092 edit->bracket = edit->curs1;
2093 edit->force |= REDRAW_PAGE;
2094 edit_cursor_move (edit, q - edit->curs1);
2098 * This executes a command as though the user initiated it through a key
2099 * press. Callback with WIDGET_KEY as a message calls this after
2100 * translating the key press. This function can be used to pass any
2101 * command to the editor. Note that the screen wouldn't update
2102 * automatically. Either of command or char_for_insertion must be
2103 * passed as -1. Commands are executed, and char_for_insertion is
2104 * inserted at the cursor. 0 is returned if the command is an undefined
2105 * macro, 1 otherwise.
2107 int edit_execute_key_command (WEdit * edit, int command, int char_for_insertion)
2109 int r;
2110 if (command == CK_Begin_Record_Macro) {
2111 edit->macro_i = 0;
2112 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2113 return command;
2115 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2116 edit->force |= REDRAW_COMPLETELY;
2117 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2118 edit->macro_i = -1;
2119 return command;
2121 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2122 edit->macro[edit->macro_i].command = command;
2123 edit->macro[edit->macro_i++].ch = char_for_insertion;
2125 /* record the beginning of a set of editing actions initiated by a key press */
2126 if (command != CK_Undo)
2127 edit_push_key_press (edit);
2129 r = edit_execute_cmd (edit, command, char_for_insertion);
2130 if (column_highlighting)
2131 edit->force |= REDRAW_PAGE;
2133 return r;
2136 static const char * const shell_cmd[] = SHELL_COMMANDS_i
2139 This executes a command at a lower level than macro recording.
2140 It also does not push a key_press onto the undo stack. This means
2141 that if it is called many times, a single undo command will undo
2142 all of them. It also does not check for the Undo command.
2143 Returns 0 if the command is a macro that was not found, 1
2144 otherwise.
2146 int edit_execute_cmd (WEdit * edit, int command, int char_for_insertion)
2148 int result = 1;
2149 edit->force |= REDRAW_LINE;
2150 if (edit->found_len || column_highlighting)
2151 /* the next key press will unhighlight the found string, so update whole page */
2152 edit->force |= REDRAW_PAGE;
2154 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2155 column_highlighting = 0;
2156 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2157 edit_mark_cmd (edit, 1); /* clear */
2158 edit_mark_cmd (edit, 0); /* marking on */
2160 edit->highlight = 1;
2161 } else { /* any other command */
2162 if (edit->highlight)
2163 edit_mark_cmd (edit, 0); /* clear */
2164 edit->highlight = 0;
2167 /* first check for undo */
2168 if (command == CK_Undo) {
2169 edit_do_undo (edit);
2170 edit->found_len = 0;
2171 edit->prev_col = edit_get_col (edit);
2172 edit->search_start = edit->curs1;
2173 return 1;
2175 /* An ordinary key press */
2176 if (char_for_insertion >= 0) {
2177 if (edit->overwrite) {
2178 if (edit_get_byte (edit, edit->curs1) != '\n')
2179 edit_delete (edit);
2181 edit_insert (edit, char_for_insertion);
2182 if (option_auto_para_formatting) {
2183 format_paragraph (edit, 0);
2184 edit->force |= REDRAW_PAGE;
2185 } else
2186 check_and_wrap_line (edit);
2187 edit->found_len = 0;
2188 edit->prev_col = edit_get_col (edit);
2189 edit->search_start = edit->curs1;
2190 edit_find_bracket (edit);
2191 return 1;
2193 switch (command) {
2194 case CK_Begin_Page:
2195 case CK_End_Page:
2196 case CK_Begin_Page_Highlight:
2197 case CK_End_Page_Highlight:
2198 case CK_Word_Left:
2199 case CK_Word_Right:
2200 case CK_Up:
2201 case CK_Down:
2202 case CK_Word_Left_Highlight:
2203 case CK_Word_Right_Highlight:
2204 case CK_Up_Highlight:
2205 case CK_Down_Highlight:
2206 if (edit->mark2 == -1)
2207 break; /*marking is following the cursor: may need to highlight a whole line */
2208 case CK_Left:
2209 case CK_Right:
2210 case CK_Left_Highlight:
2211 case CK_Right_Highlight:
2212 edit->force |= REDRAW_CHAR_ONLY;
2215 /* basic cursor key commands */
2216 switch (command) {
2217 case CK_BackSpace:
2218 if (option_backspace_through_tabs && is_in_indent (edit)) {
2219 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2220 && edit->curs1 > 0)
2221 edit_backspace (edit);
2222 break;
2223 } else {
2224 if (option_fake_half_tabs) {
2225 int i;
2226 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2227 for (i = 0; i < HALF_TAB_SIZE; i++)
2228 edit_backspace (edit);
2229 break;
2233 edit_backspace (edit);
2234 break;
2235 case CK_Delete:
2236 if (option_fake_half_tabs) {
2237 int i;
2238 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2239 for (i = 1; i <= HALF_TAB_SIZE; i++)
2240 edit_delete (edit);
2241 break;
2244 edit_delete (edit);
2245 break;
2246 case CK_Delete_Word_Left:
2247 edit_left_delete_word (edit);
2248 break;
2249 case CK_Delete_Word_Right:
2250 edit_right_delete_word (edit);
2251 break;
2252 case CK_Delete_Line:
2253 edit_delete_line (edit);
2254 break;
2255 case CK_Delete_To_Line_End:
2256 edit_delete_to_line_end (edit);
2257 break;
2258 case CK_Delete_To_Line_Begin:
2259 edit_delete_to_line_begin (edit);
2260 break;
2261 case CK_Enter:
2262 if (option_auto_para_formatting) {
2263 edit_double_newline (edit);
2264 if (option_return_does_auto_indent)
2265 edit_auto_indent (edit, 0, 1);
2266 format_paragraph (edit, 0);
2267 } else {
2268 edit_insert (edit, '\n');
2269 if (option_return_does_auto_indent) {
2270 edit_auto_indent (edit, 0, 1);
2273 break;
2274 case CK_Return:
2275 edit_insert (edit, '\n');
2276 break;
2278 case CK_Page_Up:
2279 case CK_Page_Up_Highlight:
2280 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2281 break;
2282 case CK_Page_Down:
2283 case CK_Page_Down_Highlight:
2284 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2285 break;
2286 case CK_Left:
2287 case CK_Left_Highlight:
2288 if (option_fake_half_tabs) {
2289 if (is_in_indent (edit) && right_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_Right:
2298 case CK_Right_Highlight:
2299 if (option_fake_half_tabs) {
2300 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2301 edit_cursor_move (edit, HALF_TAB_SIZE);
2302 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2303 break;
2306 edit_cursor_move (edit, 1);
2307 break;
2308 case CK_Begin_Page:
2309 case CK_Begin_Page_Highlight:
2310 edit_begin_page (edit);
2311 break;
2312 case CK_End_Page:
2313 case CK_End_Page_Highlight:
2314 edit_end_page (edit);
2315 break;
2316 case CK_Word_Left:
2317 case CK_Word_Left_Highlight:
2318 edit_left_word_move_cmd (edit);
2319 break;
2320 case CK_Word_Right:
2321 case CK_Word_Right_Highlight:
2322 edit_right_word_move_cmd (edit);
2323 break;
2324 case CK_Up:
2325 case CK_Up_Highlight:
2326 edit_move_up (edit, 1, 0);
2327 break;
2328 case CK_Down:
2329 case CK_Down_Highlight:
2330 edit_move_down (edit, 1, 0);
2331 break;
2332 case CK_Paragraph_Up:
2333 case CK_Paragraph_Up_Highlight:
2334 edit_move_up_paragraph (edit, 0);
2335 break;
2336 case CK_Paragraph_Down:
2337 case CK_Paragraph_Down_Highlight:
2338 edit_move_down_paragraph (edit, 0);
2339 break;
2340 case CK_Scroll_Up:
2341 case CK_Scroll_Up_Highlight:
2342 edit_move_up (edit, 1, 1);
2343 break;
2344 case CK_Scroll_Down:
2345 case CK_Scroll_Down_Highlight:
2346 edit_move_down (edit, 1, 1);
2347 break;
2348 case CK_Home:
2349 case CK_Home_Highlight:
2350 edit_cursor_to_bol (edit);
2351 break;
2352 case CK_End:
2353 case CK_End_Highlight:
2354 edit_cursor_to_eol (edit);
2355 break;
2357 case CK_Tab:
2358 edit_tab_cmd (edit);
2359 if (option_auto_para_formatting) {
2360 format_paragraph (edit, 0);
2361 edit->force |= REDRAW_PAGE;
2362 } else
2363 check_and_wrap_line (edit);
2364 break;
2366 case CK_Toggle_Insert:
2367 edit->overwrite = (edit->overwrite == 0);
2368 break;
2370 case CK_Mark:
2371 if (edit->mark2 >= 0) {
2372 if (column_highlighting)
2373 edit_push_action (edit, COLUMN_ON);
2374 column_highlighting = 0;
2376 edit_mark_cmd (edit, 0);
2377 break;
2378 case CK_Column_Mark:
2379 if (!column_highlighting)
2380 edit_push_action (edit, COLUMN_OFF);
2381 column_highlighting = 1;
2382 edit_mark_cmd (edit, 0);
2383 break;
2384 case CK_Unmark:
2385 if (column_highlighting)
2386 edit_push_action (edit, COLUMN_ON);
2387 column_highlighting = 0;
2388 edit_mark_cmd (edit, 1);
2389 break;
2391 case CK_Toggle_Bookmark:
2392 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2393 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2394 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2395 else
2396 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2397 break;
2398 case CK_Flush_Bookmarks:
2399 book_mark_flush (edit, BOOK_MARK_COLOR);
2400 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2401 edit->force |= REDRAW_PAGE;
2402 break;
2403 case CK_Next_Bookmark:
2404 if (edit->book_mark) {
2405 struct _book_mark *p;
2406 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
2407 if (p->next) {
2408 p = p->next;
2409 if (p->line >= edit->start_line + edit->num_widget_lines || p->line < edit->start_line)
2410 edit_move_display (edit, p->line - 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, edit->curs_line);
2419 while (p->line == edit->curs_line)
2420 if (p->prev)
2421 p = p->prev;
2422 if (p->line >= 0) {
2423 if (p->line >= edit->start_line + edit->num_widget_lines || p->line < edit->start_line)
2424 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
2425 edit_move_to_line (edit, p->line);
2428 break;
2430 case CK_Beginning_Of_Text:
2431 case CK_Beginning_Of_Text_Highlight:
2432 edit_move_to_top (edit);
2433 break;
2434 case CK_End_Of_Text:
2435 case CK_End_Of_Text_Highlight:
2436 edit_move_to_bottom (edit);
2437 break;
2439 case CK_Copy:
2440 edit_block_copy_cmd (edit);
2441 break;
2442 case CK_Remove:
2443 edit_block_delete_cmd (edit);
2444 break;
2445 case CK_Move:
2446 edit_block_move_cmd (edit);
2447 break;
2449 case CK_XStore:
2450 edit_copy_to_X_buf_cmd (edit);
2451 break;
2452 case CK_XCut:
2453 edit_cut_to_X_buf_cmd (edit);
2454 break;
2455 case CK_XPaste:
2456 edit_paste_from_X_buf_cmd (edit);
2457 break;
2458 case CK_Selection_History:
2459 edit_paste_from_history (edit);
2460 break;
2462 case CK_Save_As:
2463 edit_save_as_cmd (edit);
2464 break;
2465 case CK_Save:
2466 edit_save_confirm_cmd (edit);
2467 break;
2468 case CK_Load:
2469 edit_load_cmd (edit);
2470 break;
2471 case CK_Save_Block:
2472 edit_save_block_cmd (edit);
2473 break;
2474 case CK_Insert_File:
2475 edit_insert_file_cmd (edit);
2476 break;
2478 case CK_Find:
2479 edit_search_cmd (edit, 0);
2480 break;
2481 case CK_Find_Again:
2482 edit_search_cmd (edit, 1);
2483 break;
2484 case CK_Replace:
2485 edit_replace_cmd (edit, 0);
2486 break;
2487 case CK_Replace_Again:
2488 edit_replace_cmd (edit, 1);
2489 break;
2490 case CK_Complete_Word:
2491 edit_complete_word_cmd (edit);
2492 break;
2494 case CK_Exit:
2495 dlg_stop (edit->widget.parent);
2496 break;
2497 case CK_New:
2498 edit_new_cmd (edit);
2499 break;
2501 case CK_Help:
2502 edit_help_cmd (edit);
2503 break;
2505 case CK_Refresh:
2506 edit_refresh_cmd (edit);
2507 break;
2509 case CK_Date:{
2510 time_t t;
2511 #ifdef HAVE_STRFTIME
2512 char s[1024];
2513 /* fool gcc to prevent a Y2K warning */
2514 char time_format[] = "_c";
2515 time_format[0] = '%';
2516 #endif
2517 time (&t);
2518 #ifdef HAVE_STRFTIME
2519 strftime (s, sizeof (s), time_format, localtime (&t));
2520 edit_print_string (edit, s);
2521 #else
2522 edit_print_string (edit, ctime (&t));
2523 #endif
2524 edit->force |= REDRAW_PAGE;
2525 break;
2527 case CK_Goto:
2528 edit_goto_cmd (edit);
2529 break;
2530 case CK_Paragraph_Format:
2531 format_paragraph (edit, 1);
2532 edit->force |= REDRAW_PAGE;
2533 break;
2534 case CK_Delete_Macro:
2535 edit_delete_macro_cmd (edit);
2536 break;
2537 case CK_Match_Bracket:
2538 edit_goto_matching_bracket (edit);
2539 break;
2540 case CK_User_Menu:
2541 user_menu (edit);
2542 break;
2543 case CK_Sort:
2544 edit_sort_cmd (edit);
2545 break;
2546 case CK_ExtCmd:
2547 edit_ext_cmd (edit);
2548 break;
2549 case CK_Mail:
2550 edit_mail_dialog (edit);
2551 break;
2552 case CK_Shell:
2553 view_other_cmd ();
2554 break;
2556 /* These commands are not handled and must be handled by the user application */
2557 #if 0
2558 case CK_Sort:
2559 case CK_Mail:
2560 case CK_Find_File:
2561 case CK_Ctags:
2562 case CK_Terminal:
2563 case CK_Terminal_App:
2564 case CK_ExtCmd:
2565 #endif
2566 case CK_Complete:
2567 case CK_Cancel:
2568 case CK_Save_Desktop:
2569 case CK_New_Window:
2570 case CK_Cycle:
2571 case CK_Save_And_Quit:
2572 case CK_Check_Save_And_Quit:
2573 case CK_Run_Another:
2574 case CK_Debug_Start:
2575 case CK_Debug_Stop:
2576 case CK_Debug_Toggle_Break:
2577 case CK_Debug_Clear:
2578 case CK_Debug_Next:
2579 case CK_Debug_Step:
2580 case CK_Debug_Back_Trace:
2581 case CK_Debug_Continue:
2582 case CK_Debug_Enter_Command:
2583 case CK_Debug_Until_Curser:
2584 result = 0;
2585 break;
2586 case CK_Menu:
2587 result = 0;
2588 break;
2591 /* CK_Pipe_Block */
2592 if ((command / 1000) == 1) /* a shell command */
2593 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2594 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2595 struct macro m[MAX_MACRO_LENGTH];
2596 int nm;
2597 if ((result = edit_load_macro_cmd (edit, m, &nm, command - 2000)))
2598 edit_execute_macro (edit, m, nm);
2601 /* keys which must set the col position, and the search vars */
2602 switch (command) {
2603 case CK_Find:
2604 case CK_Find_Again:
2605 case CK_Replace:
2606 case CK_Replace_Again:
2607 case CK_Complete_Word:
2608 edit->prev_col = edit_get_col (edit);
2609 return 1;
2610 break;
2611 case CK_Up:
2612 case CK_Up_Highlight:
2613 case CK_Down:
2614 case CK_Down_Highlight:
2615 case CK_Page_Up:
2616 case CK_Page_Up_Highlight:
2617 case CK_Page_Down:
2618 case CK_Page_Down_Highlight:
2619 case CK_Beginning_Of_Text:
2620 case CK_Beginning_Of_Text_Highlight:
2621 case CK_End_Of_Text:
2622 case CK_End_Of_Text_Highlight:
2623 case CK_Paragraph_Up:
2624 case CK_Paragraph_Up_Highlight:
2625 case CK_Paragraph_Down:
2626 case CK_Paragraph_Down_Highlight:
2627 case CK_Scroll_Up:
2628 case CK_Scroll_Up_Highlight:
2629 case CK_Scroll_Down:
2630 case CK_Scroll_Down_Highlight:
2631 edit->search_start = edit->curs1;
2632 edit->found_len = 0;
2633 edit_find_bracket (edit);
2634 return 1;
2635 break;
2636 default:
2637 edit->found_len = 0;
2638 edit->prev_col = edit_get_col (edit);
2639 edit->search_start = edit->curs1;
2641 edit_find_bracket (edit);
2643 if (option_auto_para_formatting) {
2644 switch (command) {
2645 case CK_BackSpace:
2646 case CK_Delete:
2647 case CK_Delete_Word_Left:
2648 case CK_Delete_Word_Right:
2649 case CK_Delete_To_Line_End:
2650 case CK_Delete_To_Line_Begin:
2651 format_paragraph (edit, 0);
2652 edit->force |= REDRAW_PAGE;
2655 return result;
2659 static void
2660 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2662 int i = 0;
2664 if (edit->macro_depth++ > 256) {
2665 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2666 edit->macro_depth--;
2667 return;
2669 edit->force |= REDRAW_PAGE;
2670 for (; i < n; i++) {
2671 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2673 edit_update_screen (edit);
2674 edit->macro_depth--;
2677 /* User edit menu, like user menu (F2) but only in editor. */
2678 static void
2679 user_menu (WEdit * edit)
2681 FILE *fd;
2682 int nomark;
2683 struct stat status;
2684 long start_mark, end_mark;
2685 char *block_file = catstrs (home_dir, BLOCK_FILE, 0);
2686 int rc = 0;
2688 nomark = eval_marks (edit, &start_mark, &end_mark);
2689 if (!nomark) /* remember marked or not */
2690 edit_save_block (edit, block_file, start_mark, end_mark);
2692 /* run shell scripts from menu */
2693 user_menu_cmd (edit);
2695 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2696 /* no block messages */
2697 return;
2700 if (!nomark) {
2701 /* i.e. we have marked block */
2702 rc = edit_block_delete_cmd (edit);
2705 if (!rc) {
2706 edit_insert_file (edit, block_file);
2709 /* truncate block file */
2710 if ((fd = fopen (block_file, "w"))) {
2711 fclose (fd);
2714 edit_refresh_cmd (edit);
2715 edit->force |= REDRAW_COMPLETELY;
2716 return;