Merge commit 'origin/mc-4.6'
[free-mc.git] / edit / edit.c
blobbec84d78ddc6fb79fa370f84d10b29d114908d9e
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 #include <config.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <stdlib.h>
35 #include <mhl/string.h>
37 #include "../src/global.h"
39 #include "edit.h"
40 #include "editlock.h"
41 #include "edit-widget.h"
42 #include "editcmddef.h"
43 #include "usermap.h"
45 #include "../src/cmd.h" /* view_other_cmd() */
46 #include "../src/user.h" /* user_menu_cmd() */
47 #include "../src/wtools.h" /* query_dialog() */
48 #include "../src/timefmt.h" /* time formatting */
52 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
53 or EDIT_KEY_EMULATION_EMACS
55 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
57 int option_word_wrap_line_length = 72;
58 int option_typewriter_wrap = 0;
59 int option_auto_para_formatting = 0;
60 int option_tab_spacing = 8;
61 int option_fill_tabs_with_spaces = 0;
62 int option_return_does_auto_indent = 1;
63 int option_backspace_through_tabs = 0;
64 int option_fake_half_tabs = 1;
65 int option_save_mode = EDIT_QUICK_SAVE;
66 int option_save_position = 1;
67 int option_max_undo = 32768;
69 int option_edit_right_extreme = 0;
70 int option_edit_left_extreme = 0;
71 int option_edit_top_extreme = 0;
72 int option_edit_bottom_extreme = 0;
74 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
75 char *option_backup_ext = NULL;
77 /*-
79 * here's a quick sketch of the layout: (don't run this through indent.)
81 * (b1 is buffers1 and b2 is buffers2)
83 * |
84 * \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
85 * ______________________________________|______________________________________
86 * |
87 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
88 * |-> |-> |-> |-> |-> |-> |
89 * |
90 * _<------------------------->|<----------------->_
91 * WEdit->curs2 | WEdit->curs1
92 * ^ | ^
93 * | ^|^ |
94 * cursor ||| cursor
95 * |||
96 * file end|||file beginning
97 * |
98 * |
101 * This_is_some_file
102 * fin.
106 static void user_menu (WEdit *edit);
108 int edit_get_byte (WEdit * edit, long byte_index)
110 unsigned long p;
111 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
112 return '\n';
114 if (byte_index >= edit->curs1) {
115 p = edit->curs1 + edit->curs2 - byte_index - 1;
116 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
117 } else {
118 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
123 * Initialize the buffers for an empty files.
125 static void
126 edit_init_buffers (WEdit *edit)
128 int j;
130 for (j = 0; j <= MAXBUFF; j++) {
131 edit->buffers1[j] = NULL;
132 edit->buffers2[j] = NULL;
135 edit->curs1 = 0;
136 edit->curs2 = 0;
137 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
141 * Load file OR text into buffers. Set cursor to the beginning of file.
142 * Return 1 on error.
144 static int
145 edit_load_file_fast (WEdit *edit, const char *filename)
147 long buf, buf2;
148 int file = -1;
150 edit->curs2 = edit->last_byte;
151 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
153 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
154 GString *errmsg = g_string_new(NULL);
155 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
156 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
157 g_string_free (errmsg, TRUE);
158 return 1;
161 if (!edit->buffers2[buf2])
162 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
164 mc_read (file,
165 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
166 (edit->curs2 & M_EDIT_BUF_SIZE),
167 edit->curs2 & M_EDIT_BUF_SIZE);
169 for (buf = buf2 - 1; buf >= 0; buf--) {
170 /* edit->buffers2[0] is already allocated */
171 if (!edit->buffers2[buf])
172 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
173 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
176 mc_close (file);
177 return 0;
180 /* detecting an error on save is easy: just check if every byte has been written. */
181 /* detecting an error on read, is not so easy 'cos there is not way to tell
182 whether you read everything or not. */
183 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
184 static const struct edit_filters {
185 const char *read, *write, *extension;
186 } all_filters[] = {
187 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
188 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
189 { "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 size_t i, l, e;
196 if (!filename)
197 return -1;
198 l = strlen (filename);
199 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
200 e = strlen (all_filters[i].extension);
201 if (l > e)
202 if (!strcmp (all_filters[i].extension, filename + l - e))
203 return i;
205 return -1;
208 static char *
209 edit_get_filter (const char *filename)
211 int i, l;
212 char *p, *quoted_name;
213 i = edit_find_filter (filename);
214 if (i < 0)
215 return 0;
216 quoted_name = name_quote (filename, 0);
217 l = strlen (quoted_name);
218 p = g_malloc (strlen (all_filters[i].read) + l + 2);
219 sprintf (p, all_filters[i].read, quoted_name);
220 g_free (quoted_name);
221 return p;
224 char *
225 edit_get_write_filter (const char *write_name, const char *filename)
227 int i, l;
228 char *p, *writename;
229 i = edit_find_filter (filename);
230 if (i < 0)
231 return 0;
232 writename = name_quote (write_name, 0);
233 l = strlen (writename);
234 p = g_malloc (strlen (all_filters[i].write) + l + 2);
235 sprintf (p, all_filters[i].write, writename);
236 g_free (writename);
237 return p;
240 static long
241 edit_insert_stream (WEdit * edit, FILE * f)
243 int c;
244 long i = 0;
245 while ((c = fgetc (f)) >= 0) {
246 edit_insert (edit, c);
247 i++;
249 return i;
252 long edit_write_stream (WEdit * edit, FILE * f)
254 long i;
255 for (i = 0; i < edit->last_byte; i++)
256 if (fputc (edit_get_byte (edit, i), f) < 0)
257 break;
258 return i;
261 #define TEMP_BUF_LEN 1024
263 /* inserts a file at the cursor, returns 1 on success */
265 edit_insert_file (WEdit *edit, const char *filename)
267 char *p;
268 if ((p = edit_get_filter (filename))) {
269 FILE *f;
270 long current = edit->curs1;
271 f = (FILE *) popen (p, "r");
272 if (f) {
273 edit_insert_stream (edit, f);
274 edit_cursor_move (edit, current - edit->curs1);
275 if (pclose (f) > 0) {
276 GString *errmsg = g_string_new (NULL);
277 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
278 edit_error_dialog (_("Error"), errmsg->str);
279 g_string_free (errmsg, TRUE);
280 g_free (p);
281 return 0;
283 } else {
284 GString *errmsg = g_string_new (NULL);
285 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
286 edit_error_dialog (_("Error"), errmsg->str);
287 g_string_free (errmsg, TRUE);
288 g_free (p);
289 return 0;
291 g_free (p);
292 } else {
293 int i, file, blocklen;
294 long current = edit->curs1;
295 unsigned char *buf;
296 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
297 return 0;
298 buf = g_malloc (TEMP_BUF_LEN);
299 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
300 for (i = 0; i < blocklen; i++)
301 edit_insert (edit, buf[i]);
303 edit_cursor_move (edit, current - edit->curs1);
304 g_free (buf);
305 mc_close (file);
306 if (blocklen)
307 return 0;
309 return 1;
312 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
313 static int
314 check_file_access (WEdit *edit, const char *filename, struct stat *st)
316 int file;
317 GString *errmsg = (GString *) 0;
319 /* Try opening an existing file */
320 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
322 if (file < 0) {
324 * Try creating the file. O_EXCL prevents following broken links
325 * and opening existing files.
327 file =
328 mc_open (filename,
329 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
330 0666);
331 if (file < 0) {
332 g_string_sprintf (errmsg = g_string_new (NULL),
333 _(" Cannot open %s for reading "), filename);
334 goto cleanup;
335 } else {
336 /* New file, delete it if it's not modified or saved */
337 edit->delete_file = 1;
341 /* Check what we have opened */
342 if (mc_fstat (file, st) < 0) {
343 g_string_sprintf (errmsg = g_string_new (NULL),
344 _(" Cannot get size/permissions for %s "), filename);
345 goto cleanup;
348 /* We want to open regular files only */
349 if (!S_ISREG (st->st_mode)) {
350 g_string_sprintf (errmsg = g_string_new (NULL),
351 _(" %s is not a regular file "), filename);
352 goto cleanup;
356 * Don't delete non-empty files.
357 * O_EXCL should prevent it, but let's be on the safe side.
359 if (st->st_size > 0) {
360 edit->delete_file = 0;
363 if (st->st_size >= SIZE_LIMIT) {
364 g_string_sprintf (errmsg = g_string_new (NULL),
365 _(" File %s is too large "), filename);
366 goto cleanup;
369 cleanup:
370 (void) mc_close (file);
371 if (errmsg) {
372 edit_error_dialog (_("Error"), errmsg->str);
373 g_string_free (errmsg, TRUE);
374 return 1;
376 return 0;
380 * Open the file and load it into the buffers, either directly or using
381 * a filter. Return 0 on success, 1 on error.
383 * Fast loading (edit_load_file_fast) is used when the file size is
384 * known. In this case the data is read into the buffers by blocks.
385 * If the file size is not known, the data is loaded byte by byte in
386 * edit_insert_file.
388 static int
389 edit_load_file (WEdit *edit)
391 int fast_load = 1;
393 /* Cannot do fast load if a filter is used */
394 if (edit_find_filter (edit->filename) >= 0)
395 fast_load = 0;
398 * VFS may report file size incorrectly, and slow load is not a big
399 * deal considering overhead in VFS.
401 if (!vfs_file_is_local (edit->filename))
402 fast_load = 0;
405 * FIXME: line end translation should disable fast loading as well
406 * Consider doing fseek() to the end and ftell() for the real size.
409 if (*edit->filename) {
410 /* If we are dealing with a real file, check that it exists */
411 if (check_file_access (edit, edit->filename, &edit->stat1))
412 return 1;
413 } else {
414 /* nothing to load */
415 fast_load = 0;
418 edit_init_buffers (edit);
420 if (fast_load) {
421 edit->last_byte = edit->stat1.st_size;
422 edit_load_file_fast (edit, edit->filename);
423 /* If fast load was used, the number of lines wasn't calculated */
424 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
425 } else {
426 edit->last_byte = 0;
427 if (*edit->filename) {
428 edit->stack_disable = 1;
429 if (!edit_insert_file (edit, edit->filename)) {
430 edit_clean (edit);
431 return 1;
433 edit->stack_disable = 0;
436 return 0;
439 /* Restore saved cursor position in the file */
440 static void
441 edit_load_position (WEdit *edit)
443 char *filename;
444 long line, column;
446 if (!edit->filename || !*edit->filename)
447 return;
449 filename = vfs_canon (edit->filename);
450 load_file_position (filename, &line, &column);
451 g_free (filename);
453 edit_move_to_line (edit, line - 1);
454 edit->prev_col = column;
455 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
456 edit_move_display (edit, line - (edit->num_widget_lines / 2));
459 /* Save cursor position in the file */
460 static void
461 edit_save_position (WEdit *edit)
463 char *filename;
465 if (!edit->filename || !*edit->filename)
466 return;
468 filename = vfs_canon (edit->filename);
469 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
470 g_free (filename);
473 /* Clean the WEdit stricture except the widget part */
474 static inline void
475 edit_purge_widget (WEdit *edit)
477 int len = sizeof (WEdit) - sizeof (Widget);
478 char *start = (char *) edit + sizeof (Widget);
479 memset (start, 0, len);
480 edit->macro_i = -1; /* not recording a macro */
483 #define space_width 1
486 * Fill in the edit structure. Return NULL on failure. Pass edit as
487 * NULL to allocate a new structure.
489 * If line is 0, try to restore saved position. Otherwise put the
490 * cursor on that line and show it in the middle of the screen.
492 WEdit *
493 edit_init (WEdit *edit, int lines, int columns, const char *filename,
494 long line)
496 int to_free = 0;
497 option_auto_syntax = 1; /* Resetting to auto on every invokation */
499 if (!edit) {
500 #ifdef ENABLE_NLS
502 * Expand option_whole_chars_search by national letters using
503 * current locale
506 static char option_whole_chars_search_buf[256];
508 if (option_whole_chars_search_buf != option_whole_chars_search) {
509 size_t i;
510 size_t len = strlen (option_whole_chars_search);
512 strcpy (option_whole_chars_search_buf,
513 option_whole_chars_search);
515 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
516 if (islower (i) && !strchr (option_whole_chars_search, i)) {
517 option_whole_chars_search_buf[len++] = i;
521 option_whole_chars_search_buf[len] = 0;
522 option_whole_chars_search = option_whole_chars_search_buf;
524 #endif /* ENABLE_NLS */
525 edit = g_malloc0 (sizeof (WEdit));
526 to_free = 1;
528 edit_purge_widget (edit);
529 edit->num_widget_lines = lines;
530 edit->num_widget_columns = columns;
531 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
532 edit->stat1.st_uid = getuid ();
533 edit->stat1.st_gid = getgid ();
534 edit->stat1.st_mtime = 0;
535 edit->bracket = -1;
536 edit->force |= REDRAW_PAGE;
537 edit_set_filename (edit, filename);
538 edit->stack_size = START_STACK_SIZE;
539 edit->stack_size_mask = START_STACK_SIZE - 1;
540 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
541 if (edit_load_file (edit)) {
542 /* edit_load_file already gives an error message */
543 if (to_free)
544 g_free (edit);
545 return 0;
547 edit->loading_done = 1;
548 edit->modified = 0;
549 edit->locked = 0;
550 edit_load_syntax (edit, 0, 0);
552 int color;
553 edit_get_syntax_color (edit, -1, &color);
556 /* load saved cursor position */
557 if ((line == 0) && option_save_position) {
558 edit_load_position (edit);
559 } else {
560 if (line <= 0)
561 line = 1;
562 edit_move_display (edit, line - 1);
563 edit_move_to_line (edit, line - 1);
566 edit_load_user_map(edit);
568 return edit;
571 /* Clear the edit struct, freeing everything in it. Return 1 on success */
573 edit_clean (WEdit *edit)
575 int j = 0;
577 if (!edit)
578 return 0;
580 /* a stale lock, remove it */
581 if (edit->locked)
582 edit->locked = edit_unlock_file (edit->filename);
584 /* save cursor position */
585 if (option_save_position)
586 edit_save_position (edit);
588 /* File specified on the mcedit command line and never saved */
589 if (edit->delete_file)
590 unlink (edit->filename);
592 edit_free_syntax_rules (edit);
593 book_mark_flush (edit, -1);
594 for (; j <= MAXBUFF; j++) {
595 g_free (edit->buffers1[j]);
596 g_free (edit->buffers2[j]);
599 g_free (edit->undo_stack);
600 g_free (edit->filename);
601 g_free (edit->dir);
603 edit_purge_widget (edit);
605 /* Free temporary strings used in catstrs() */
606 freestrs ();
608 return 1;
612 /* returns 1 on success */
613 int edit_renew (WEdit * edit)
615 int lines = edit->num_widget_lines;
616 int columns = edit->num_widget_columns;
617 int retval = 1;
619 edit_clean (edit);
620 if (!edit_init (edit, lines, columns, "", 0))
621 retval = 0;
622 return retval;
626 * Load a new file into the editor. If it fails, preserve the old file.
627 * To do it, allocate a new widget, initialize it and, if the new file
628 * was loaded, copy the data to the old widget.
629 * Return 1 on success, 0 on failure.
632 edit_reload (WEdit *edit, const char *filename)
634 WEdit *e;
635 int lines = edit->num_widget_lines;
636 int columns = edit->num_widget_columns;
638 e = g_malloc0 (sizeof (WEdit));
639 e->widget = edit->widget;
640 if (!edit_init (e, lines, columns, filename, 0)) {
641 g_free (e);
642 return 0;
644 edit_clean (edit);
645 memcpy (edit, e, sizeof (WEdit));
646 g_free (e);
647 return 1;
652 Recording stack for undo:
653 The following is an implementation of a compressed stack. Identical
654 pushes are recorded by a negative prefix indicating the number of times the
655 same char was pushed. This saves space for repeated curs-left or curs-right
656 delete etc.
660 pushed: stored:
664 b -3
666 c --> -4
672 If the stack long int is 0-255 it represents a normal insert (from a backspace),
673 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
674 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
675 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
676 position.
678 The only way the cursor moves or the buffer is changed is through the routines:
679 insert, backspace, insert_ahead, delete, and cursor_move.
680 These record the reverse undo movements onto the stack each time they are
681 called.
683 Each key press results in a set of actions (insert; delete ...). So each time
684 a key is pressed the current position of start_display is pushed as
685 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
686 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
687 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
691 void edit_push_action (WEdit * edit, long c,...)
693 unsigned long sp = edit->stack_pointer;
694 unsigned long spm1;
695 long *t;
697 /* first enlarge the stack if necessary */
698 if (sp > edit->stack_size - 10) { /* say */
699 if (option_max_undo < 256)
700 option_max_undo = 256;
701 if (edit->stack_size < (unsigned long) option_max_undo) {
702 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
703 if (t) {
704 edit->undo_stack = t;
705 edit->stack_size <<= 1;
706 edit->stack_size_mask = edit->stack_size - 1;
710 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
711 if (edit->stack_disable)
712 return;
714 #ifdef FAST_MOVE_CURSOR
715 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
716 va_list ap;
717 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
718 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
719 va_start (ap, c);
720 c = -(va_arg (ap, int));
721 va_end (ap);
722 } else
723 #endif /* ! FAST_MOVE_CURSOR */
724 if (edit->stack_bottom != sp
725 && spm1 != edit->stack_bottom
726 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
727 int d;
728 if (edit->undo_stack[spm1] < 0) {
729 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
730 if (d == c) {
731 if (edit->undo_stack[spm1] > -1000000000) {
732 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
733 edit->undo_stack[spm1]--;
734 return;
737 /* #define NO_STACK_CURSMOVE_ANIHILATION */
738 #ifndef NO_STACK_CURSMOVE_ANIHILATION
739 else if ((c == CURS_LEFT && d == CURS_RIGHT)
740 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
741 if (edit->undo_stack[spm1] == -2)
742 edit->stack_pointer = spm1;
743 else
744 edit->undo_stack[spm1]++;
745 return;
747 #endif
748 } else {
749 d = edit->undo_stack[spm1];
750 if (d == c) {
751 if (c >= KEY_PRESS)
752 return; /* --> no need to push multiple do-nothings */
753 edit->undo_stack[sp] = -2;
754 goto check_bottom;
756 #ifndef NO_STACK_CURSMOVE_ANIHILATION
757 else if ((c == CURS_LEFT && d == CURS_RIGHT)
758 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
759 edit->stack_pointer = spm1;
760 return;
762 #endif
765 edit->undo_stack[sp] = c;
766 check_bottom:
768 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
770 /* if the sp wraps round and catches the stack_bottom then erase
771 * the first set of actions on the stack to make space - by moving
772 * stack_bottom forward one "key press" */
773 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
774 if ((unsigned long) c == edit->stack_bottom ||
775 (((unsigned long) 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 g_free (p);
1057 s = edit->curs1 & M_EDIT_BUF_SIZE;
1058 while (increment) {
1059 p = 0;
1060 r = EDIT_BUF_SIZE;
1061 if (r > increment)
1062 r = increment;
1063 t = s;
1064 if (r < t)
1065 t = r;
1066 memqcpy (edit,
1067 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1068 EDIT_BUF_SIZE - t,
1069 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1071 if (r >= s) {
1072 if (t) {
1073 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1074 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1076 memqcpy (edit,
1077 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1078 EDIT_BUF_SIZE - r,
1079 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1080 EDIT_BUF_SIZE - (r - s), r - s);
1082 increment -= r;
1083 edit->curs1 -= r;
1084 edit->curs2 += r;
1085 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1086 if (p)
1087 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1088 else
1089 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1090 g_malloc (EDIT_BUF_SIZE);
1091 } else {
1092 g_free (p);
1095 return edit_get_byte (edit, edit->curs1);
1098 #endif /* ! FAST_MOVE_CURSOR */
1100 /* moves the cursor right or left: increment positive or negative respectively */
1101 void edit_cursor_move (WEdit * edit, long increment)
1103 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1104 int c;
1106 #ifdef FAST_MOVE_CURSOR
1107 if (increment < -256) {
1108 edit->force |= REDRAW_PAGE;
1109 edit_move_backward_lots (edit, -increment);
1110 return;
1112 #endif /* ! FAST_MOVE_CURSOR */
1114 if (increment < 0) {
1115 for (; increment < 0; increment++) {
1116 if (!edit->curs1)
1117 return;
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 } else if (increment > 0) {
1139 for (; increment > 0; increment--) {
1140 if (!edit->curs2)
1141 return;
1143 edit_push_action (edit, CURS_LEFT);
1145 c = edit_get_byte (edit, edit->curs1);
1146 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1147 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1148 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1149 edit->curs1++;
1150 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1151 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1152 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1153 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1155 edit->curs2--;
1156 if (c == '\n') {
1157 edit->curs_line++;
1158 edit->force |= REDRAW_LINE_ABOVE;
1164 /* These functions return positions relative to lines */
1166 /* returns index of last char on line + 1 */
1167 long edit_eol (WEdit * edit, long current)
1169 if (current < edit->last_byte) {
1170 for (;; current++)
1171 if (edit_get_byte (edit, current) == '\n')
1172 break;
1173 } else
1174 return edit->last_byte;
1175 return current;
1178 /* returns index of first char on line */
1179 long edit_bol (WEdit * edit, long current)
1181 if (current > 0) {
1182 for (;; current--)
1183 if (edit_get_byte (edit, current - 1) == '\n')
1184 break;
1185 } else
1186 return 0;
1187 return current;
1191 int edit_count_lines (WEdit * edit, long current, int upto)
1193 int lines = 0;
1194 if (upto > edit->last_byte)
1195 upto = edit->last_byte;
1196 if (current < 0)
1197 current = 0;
1198 while (current < upto)
1199 if (edit_get_byte (edit, current++) == '\n')
1200 lines++;
1201 return lines;
1205 /* If lines is zero this returns the count of lines from current to upto. */
1206 /* If upto is zero returns index of lines forward current. */
1207 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1209 if (upto) {
1210 return edit_count_lines (edit, current, upto);
1211 } else {
1212 int next;
1213 if (lines < 0)
1214 lines = 0;
1215 while (lines--) {
1216 next = edit_eol (edit, current) + 1;
1217 if (next > edit->last_byte)
1218 break;
1219 else
1220 current = next;
1222 return current;
1227 /* Returns offset of 'lines' lines up from current */
1228 long edit_move_backward (WEdit * edit, long current, int lines)
1230 if (lines < 0)
1231 lines = 0;
1232 current = edit_bol (edit, current);
1233 while((lines--) && current != 0)
1234 current = edit_bol (edit, current - 1);
1235 return current;
1238 /* If cols is zero this returns the count of columns from current to upto. */
1239 /* If upto is zero returns index of cols across from current. */
1240 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1242 long p, q;
1243 int col = 0;
1245 if (upto) {
1246 q = upto;
1247 cols = -10;
1248 } else
1249 q = edit->last_byte + 2;
1251 for (col = 0, p = current; p < q; p++) {
1252 int c;
1253 if (cols != -10) {
1254 if (col == cols)
1255 return p;
1256 if (col > cols)
1257 return p - 1;
1259 c = edit_get_byte (edit, p);
1260 if (c == '\t')
1261 col += TAB_SIZE - col % TAB_SIZE;
1262 else if (c == '\n') {
1263 if (upto)
1264 return col;
1265 else
1266 return p;
1267 } else if (c < 32 || c == 127)
1268 col += 2; /* Caret notation for control characters */
1269 else
1270 col++;
1272 return col;
1275 /* returns the current column position of the cursor */
1276 int edit_get_col (WEdit * edit)
1278 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1282 /* Scrolling functions */
1284 void edit_update_curs_row (WEdit * edit)
1286 edit->curs_row = edit->curs_line - edit->start_line;
1289 void edit_update_curs_col (WEdit * edit)
1291 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1294 /*moves the display start position up by i lines */
1295 void edit_scroll_upward (WEdit * edit, unsigned long i)
1297 unsigned long lines_above = edit->start_line;
1298 if (i > lines_above)
1299 i = lines_above;
1300 if (i) {
1301 edit->start_line -= i;
1302 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1303 edit->force |= REDRAW_PAGE;
1304 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1306 edit_update_curs_row (edit);
1310 /* returns 1 if could scroll, 0 otherwise */
1311 void edit_scroll_downward (WEdit * edit, int i)
1313 int lines_below;
1314 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1315 if (lines_below > 0) {
1316 if (i > lines_below)
1317 i = lines_below;
1318 edit->start_line += i;
1319 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1320 edit->force |= REDRAW_PAGE;
1321 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1323 edit_update_curs_row (edit);
1326 void edit_scroll_right (WEdit * edit, int i)
1328 edit->force |= REDRAW_PAGE;
1329 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1330 edit->start_col -= i;
1333 void edit_scroll_left (WEdit * edit, int i)
1335 if (edit->start_col) {
1336 edit->start_col += i;
1337 if (edit->start_col > 0)
1338 edit->start_col = 0;
1339 edit->force |= REDRAW_PAGE;
1340 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1344 /* high level cursor movement commands */
1346 static int is_in_indent (WEdit *edit)
1348 long p = edit_bol (edit, edit->curs1);
1349 while (p < edit->curs1)
1350 if (!strchr (" \t", edit_get_byte (edit, p++)))
1351 return 0;
1352 return 1;
1355 static int left_of_four_spaces (WEdit *edit);
1357 void
1358 edit_move_to_prev_col (WEdit * edit, long p)
1360 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1362 if (is_in_indent (edit) && option_fake_half_tabs) {
1363 edit_update_curs_col (edit);
1364 if (space_width)
1365 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1366 int q = edit->curs_col;
1367 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1368 p = edit_bol (edit, edit->curs1);
1369 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1370 if (!left_of_four_spaces (edit))
1371 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1377 /* move i lines */
1378 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1380 unsigned long p, l = edit->curs_line;
1382 if (i > l)
1383 i = l;
1384 if (i) {
1385 if (i > 1)
1386 edit->force |= REDRAW_PAGE;
1387 if (scroll)
1388 edit_scroll_upward (edit, i);
1390 p = edit_bol (edit, edit->curs1);
1391 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1392 edit_move_to_prev_col (edit, p);
1394 edit->search_start = edit->curs1;
1395 edit->found_len = 0;
1399 static int
1400 is_blank (WEdit *edit, long offset)
1402 long s, f;
1403 int c;
1404 s = edit_bol (edit, offset);
1405 f = edit_eol (edit, offset) - 1;
1406 while (s <= f) {
1407 c = edit_get_byte (edit, s++);
1408 if (!isspace (c))
1409 return 0;
1411 return 1;
1415 /* returns the offset of line i */
1416 static long
1417 edit_find_line (WEdit *edit, int line)
1419 int i, j = 0;
1420 int m = 2000000000;
1421 if (!edit->caches_valid) {
1422 for (i = 0; i < N_LINE_CACHES; i++)
1423 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1424 /* three offsets that we *know* are line 0 at 0 and these two: */
1425 edit->line_numbers[1] = edit->curs_line;
1426 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1427 edit->line_numbers[2] = edit->total_lines;
1428 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1429 edit->caches_valid = 1;
1431 if (line >= edit->total_lines)
1432 return edit->line_offsets[2];
1433 if (line <= 0)
1434 return 0;
1435 /* find the closest known point */
1436 for (i = 0; i < N_LINE_CACHES; i++) {
1437 int n;
1438 n = abs (edit->line_numbers[i] - line);
1439 if (n < m) {
1440 m = n;
1441 j = i;
1444 if (m == 0)
1445 return edit->line_offsets[j]; /* know the offset exactly */
1446 if (m == 1 && j >= 3)
1447 i = j; /* one line different - caller might be looping, so stay in this cache */
1448 else
1449 i = 3 + (rand () % (N_LINE_CACHES - 3));
1450 if (line > edit->line_numbers[j])
1451 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1452 else
1453 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1454 edit->line_numbers[i] = line;
1455 return edit->line_offsets[i];
1458 int line_is_blank (WEdit * edit, long line)
1460 return is_blank (edit, edit_find_line (edit, line));
1463 /* moves up until a blank line is reached, or until just
1464 before a non-blank line is reached */
1465 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1467 int i;
1468 if (edit->curs_line <= 1) {
1469 i = 0;
1470 } else {
1471 if (line_is_blank (edit, edit->curs_line)) {
1472 if (line_is_blank (edit, edit->curs_line - 1)) {
1473 for (i = edit->curs_line - 1; i; i--)
1474 if (!line_is_blank (edit, i)) {
1475 i++;
1476 break;
1478 } else {
1479 for (i = edit->curs_line - 1; i; i--)
1480 if (line_is_blank (edit, i))
1481 break;
1483 } else {
1484 for (i = edit->curs_line - 1; i; i--)
1485 if (line_is_blank (edit, i))
1486 break;
1489 edit_move_up (edit, edit->curs_line - i, scroll);
1492 /* move i lines */
1493 void edit_move_down (WEdit * edit, int i, int scroll)
1495 long p, l = edit->total_lines - edit->curs_line;
1497 if (i > l)
1498 i = l;
1499 if (i) {
1500 if (i > 1)
1501 edit->force |= REDRAW_PAGE;
1502 if (scroll)
1503 edit_scroll_downward (edit, i);
1504 p = edit_bol (edit, edit->curs1);
1505 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1506 edit_move_to_prev_col (edit, p);
1508 edit->search_start = edit->curs1;
1509 edit->found_len = 0;
1513 /* moves down until a blank line is reached, or until just
1514 before a non-blank line is reached */
1515 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1517 int i;
1518 if (edit->curs_line >= edit->total_lines - 1) {
1519 i = edit->total_lines;
1520 } else {
1521 if (line_is_blank (edit, edit->curs_line)) {
1522 if (line_is_blank (edit, edit->curs_line + 1)) {
1523 for (i = edit->curs_line + 1; i; i++)
1524 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1525 i--;
1526 break;
1528 } else {
1529 for (i = edit->curs_line + 1; i; i++)
1530 if (line_is_blank (edit, i) || i >= edit->total_lines)
1531 break;
1533 } else {
1534 for (i = edit->curs_line + 1; i; i++)
1535 if (line_is_blank (edit, i) || i >= edit->total_lines)
1536 break;
1539 edit_move_down (edit, i - edit->curs_line, scroll);
1542 static void edit_begin_page (WEdit *edit)
1544 edit_update_curs_row (edit);
1545 edit_move_up (edit, edit->curs_row, 0);
1548 static void edit_end_page (WEdit *edit)
1550 edit_update_curs_row (edit);
1551 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1555 /* goto beginning of text */
1556 static void edit_move_to_top (WEdit * edit)
1558 if (edit->curs_line) {
1559 edit_cursor_move (edit, -edit->curs1);
1560 edit_move_to_prev_col (edit, 0);
1561 edit->force |= REDRAW_PAGE;
1562 edit->search_start = 0;
1563 edit_update_curs_row(edit);
1568 /* goto end of text */
1569 static void edit_move_to_bottom (WEdit * edit)
1571 if (edit->curs_line < edit->total_lines) {
1572 edit_cursor_move (edit, edit->curs2);
1573 edit->start_display = edit->last_byte;
1574 edit->start_line = edit->total_lines;
1575 edit_update_curs_row(edit);
1576 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1577 edit->force |= REDRAW_PAGE;
1581 /* goto beginning of line */
1582 static void edit_cursor_to_bol (WEdit * edit)
1584 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1585 edit->search_start = edit->curs1;
1586 edit->prev_col = edit_get_col (edit);
1589 /* goto end of line */
1590 static void edit_cursor_to_eol (WEdit * edit)
1592 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1593 edit->search_start = edit->curs1;
1594 edit->prev_col = edit_get_col (edit);
1597 /* move cursor to line 'line' */
1598 void edit_move_to_line (WEdit * e, long line)
1600 if(line < e->curs_line)
1601 edit_move_up (e, e->curs_line - line, 0);
1602 else
1603 edit_move_down (e, line - e->curs_line, 0);
1604 edit_scroll_screen_over_cursor (e);
1607 /* scroll window so that first visible line is 'line' */
1608 void edit_move_display (WEdit * e, long line)
1610 if(line < e->start_line)
1611 edit_scroll_upward (e, e->start_line - line);
1612 else
1613 edit_scroll_downward (e, line - e->start_line);
1616 /* save markers onto undo stack */
1617 void edit_push_markers (WEdit * edit)
1619 edit_push_action (edit, MARK_1 + edit->mark1);
1620 edit_push_action (edit, MARK_2 + edit->mark2);
1623 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1625 edit->mark1 = m1;
1626 edit->mark2 = m2;
1627 edit->column1 = c1;
1628 edit->column2 = c2;
1632 /* highlight marker toggle */
1633 void edit_mark_cmd (WEdit * edit, int unmark)
1635 edit_push_markers (edit);
1636 if (unmark) {
1637 edit_set_markers (edit, 0, 0, 0, 0);
1638 edit->force |= REDRAW_PAGE;
1639 } else {
1640 if (edit->mark2 >= 0) {
1641 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1642 edit->force |= REDRAW_PAGE;
1643 } else
1644 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1648 static unsigned long
1649 my_type_of (int c)
1651 int x, r = 0;
1652 const char *p, *q;
1653 const char option_chars_move_whole_word[] =
1654 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1656 if (!c)
1657 return 0;
1658 if (c == '!') {
1659 if (*option_chars_move_whole_word == '!')
1660 return 2;
1661 return 0x80000000UL;
1663 if (isupper (c))
1664 c = 'A';
1665 else if (islower (c))
1666 c = 'a';
1667 else if (isalpha (c))
1668 c = 'a';
1669 else if (isdigit (c))
1670 c = '0';
1671 else if (isspace (c))
1672 c = ' ';
1673 q = strchr (option_chars_move_whole_word, c);
1674 if (!q)
1675 return 0xFFFFFFFFUL;
1676 do {
1677 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1678 if (*p == '!')
1679 x <<= 1;
1680 r |= x;
1681 } while ((q = strchr (q + 1, c)));
1682 return r;
1685 static void
1686 edit_left_word_move (WEdit *edit, int s)
1688 for (;;) {
1689 int c1, c2;
1690 edit_cursor_move (edit, -1);
1691 if (!edit->curs1)
1692 break;
1693 c1 = edit_get_byte (edit, edit->curs1 - 1);
1694 c2 = edit_get_byte (edit, edit->curs1);
1695 if (!(my_type_of (c1) & my_type_of (c2)))
1696 break;
1697 if (isspace (c1) && !isspace (c2))
1698 break;
1699 if (s)
1700 if (!isspace (c1) && isspace (c2))
1701 break;
1705 static void edit_left_word_move_cmd (WEdit * edit)
1707 edit_left_word_move (edit, 0);
1708 edit->force |= REDRAW_PAGE;
1711 static void
1712 edit_right_word_move (WEdit *edit, int s)
1714 for (;;) {
1715 int c1, c2;
1716 edit_cursor_move (edit, 1);
1717 if (edit->curs1 >= edit->last_byte)
1718 break;
1719 c1 = edit_get_byte (edit, edit->curs1 - 1);
1720 c2 = edit_get_byte (edit, edit->curs1);
1721 if (!(my_type_of (c1) & my_type_of (c2)))
1722 break;
1723 if (isspace (c1) && !isspace (c2))
1724 break;
1725 if (s)
1726 if (!isspace (c1) && isspace (c2))
1727 break;
1731 static void edit_right_word_move_cmd (WEdit * edit)
1733 edit_right_word_move (edit, 0);
1734 edit->force |= REDRAW_PAGE;
1738 static void edit_right_delete_word (WEdit * edit)
1740 int c1, c2;
1741 for (;;) {
1742 if (edit->curs1 >= edit->last_byte)
1743 break;
1744 c1 = edit_delete (edit);
1745 c2 = edit_get_byte (edit, edit->curs1);
1746 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1747 break;
1748 if (!(my_type_of (c1) & my_type_of (c2)))
1749 break;
1753 static void edit_left_delete_word (WEdit * edit)
1755 int c1, c2;
1756 for (;;) {
1757 if (edit->curs1 <= 0)
1758 break;
1759 c1 = edit_backspace (edit);
1760 c2 = edit_get_byte (edit, edit->curs1 - 1);
1761 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1762 break;
1763 if (!(my_type_of (c1) & my_type_of (c2)))
1764 break;
1769 the start column position is not recorded, and hence does not
1770 undo as it happed. But who would notice.
1772 static void
1773 edit_do_undo (WEdit * edit)
1775 long ac;
1776 long count = 0;
1778 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1780 while ((ac = pop_action (edit)) < KEY_PRESS) {
1781 switch ((int) ac) {
1782 case STACK_BOTTOM:
1783 goto done_undo;
1784 case CURS_RIGHT:
1785 edit_cursor_move (edit, 1);
1786 break;
1787 case CURS_LEFT:
1788 edit_cursor_move (edit, -1);
1789 break;
1790 case BACKSPACE:
1791 edit_backspace (edit);
1792 break;
1793 case DELCHAR:
1794 edit_delete (edit);
1795 break;
1796 case COLUMN_ON:
1797 column_highlighting = 1;
1798 break;
1799 case COLUMN_OFF:
1800 column_highlighting = 0;
1801 break;
1803 if (ac >= 256 && ac < 512)
1804 edit_insert_ahead (edit, ac - 256);
1805 if (ac >= 0 && ac < 256)
1806 edit_insert (edit, ac);
1808 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1809 edit->mark1 = ac - MARK_1;
1810 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1811 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1812 edit->mark2 = ac - MARK_2;
1813 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1815 if (count++)
1816 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1819 if (edit->start_display > ac - KEY_PRESS) {
1820 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1821 edit->force |= REDRAW_PAGE;
1822 } else if (edit->start_display < ac - KEY_PRESS) {
1823 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1824 edit->force |= REDRAW_PAGE;
1826 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1827 edit_update_curs_row (edit);
1829 done_undo:;
1830 edit->stack_disable = 0;
1833 static void edit_delete_to_line_end (WEdit * edit)
1835 while (edit_get_byte (edit, edit->curs1) != '\n') {
1836 if (!edit->curs2)
1837 break;
1838 edit_delete (edit);
1842 static void edit_delete_to_line_begin (WEdit * edit)
1844 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1845 if (!edit->curs1)
1846 break;
1847 edit_backspace (edit);
1851 void
1852 edit_delete_line (WEdit *edit)
1855 * Delete right part of the line.
1856 * Note that edit_get_byte() returns '\n' when byte position is
1857 * beyond EOF.
1859 while (edit_get_byte (edit, edit->curs1) != '\n') {
1860 (void) edit_delete (edit);
1864 * Delete '\n' char.
1865 * Note that edit_delete() will not corrupt anything if called while
1866 * cursor position is EOF.
1868 (void) edit_delete (edit);
1871 * Delete left part of the line.
1872 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1874 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1875 (void) edit_backspace (edit);
1879 static void insert_spaces_tab (WEdit * edit, int half)
1881 int i;
1882 edit_update_curs_col (edit);
1883 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1884 while (i > 0) {
1885 edit_insert (edit, ' ');
1886 i -= space_width;
1890 static int is_aligned_on_a_tab (WEdit * edit)
1892 edit_update_curs_col (edit);
1893 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1894 return 0; /* not alligned on a tab */
1895 return 1;
1898 static int right_of_four_spaces (WEdit *edit)
1900 int i, ch = 0;
1901 for (i = 1; i <= HALF_TAB_SIZE; i++)
1902 ch |= edit_get_byte (edit, edit->curs1 - i);
1903 if (ch == ' ')
1904 return is_aligned_on_a_tab (edit);
1905 return 0;
1908 static int left_of_four_spaces (WEdit *edit)
1910 int i, ch = 0;
1911 for (i = 0; i < HALF_TAB_SIZE; i++)
1912 ch |= edit_get_byte (edit, edit->curs1 + i);
1913 if (ch == ' ')
1914 return is_aligned_on_a_tab (edit);
1915 return 0;
1918 int edit_indent_width (WEdit * edit, long p)
1920 long q = p;
1921 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1922 q++;
1923 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1926 void edit_insert_indent (WEdit * edit, int indent)
1928 if (!option_fill_tabs_with_spaces) {
1929 while (indent >= TAB_SIZE) {
1930 edit_insert (edit, '\t');
1931 indent -= TAB_SIZE;
1934 while (indent-- > 0)
1935 edit_insert (edit, ' ');
1938 static void
1939 edit_auto_indent (WEdit * edit)
1941 long p;
1942 char c;
1943 p = edit->curs1;
1944 /* use the previous line as a template */
1945 p = edit_move_backward (edit, p, 1);
1946 /* copy the leading whitespace of the line */
1947 for (;;) { /* no range check - the line _is_ \n-terminated */
1948 c = edit_get_byte (edit, p++);
1949 if (c != ' ' && c != '\t')
1950 break;
1951 edit_insert (edit, c);
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 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.
2106 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2108 if (command == CK_Begin_Record_Macro) {
2109 edit->macro_i = 0;
2110 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2111 return;
2113 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2114 edit->force |= REDRAW_COMPLETELY;
2115 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2116 edit->macro_i = -1;
2117 return;
2119 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2120 edit->macro[edit->macro_i].command = command;
2121 edit->macro[edit->macro_i++].ch = char_for_insertion;
2123 /* record the beginning of a set of editing actions initiated by a key press */
2124 if (command != CK_Undo && command != CK_Ext_Mode)
2125 edit_push_key_press (edit);
2127 edit_execute_cmd (edit, command, char_for_insertion);
2128 if (column_highlighting)
2129 edit->force |= REDRAW_PAGE;
2132 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2135 This executes a command at a lower level than macro recording.
2136 It also does not push a key_press onto the undo stack. This means
2137 that if it is called many times, a single undo command will undo
2138 all of them. It also does not check for the Undo command.
2140 void
2141 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2143 edit->force |= REDRAW_LINE;
2145 /* The next key press will unhighlight the found string, so update
2146 * the whole page */
2147 if (edit->found_len || column_highlighting)
2148 edit->force |= REDRAW_PAGE;
2150 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2151 column_highlighting = 0;
2152 if (!edit->highlight
2153 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2154 edit_mark_cmd (edit, 1); /* clear */
2155 edit_mark_cmd (edit, 0); /* marking on */
2157 edit->highlight = 1;
2158 } else { /* any other command */
2159 if (edit->highlight)
2160 edit_mark_cmd (edit, 0); /* clear */
2161 edit->highlight = 0;
2164 /* first check for undo */
2165 if (command == CK_Undo) {
2166 edit_do_undo (edit);
2167 edit->found_len = 0;
2168 edit->prev_col = edit_get_col (edit);
2169 edit->search_start = edit->curs1;
2170 return;
2173 /* An ordinary key press */
2174 if (char_for_insertion >= 0) {
2175 if (edit->overwrite) {
2176 if (edit_get_byte (edit, edit->curs1) != '\n')
2177 edit_delete (edit);
2179 edit_insert (edit, char_for_insertion);
2180 if (option_auto_para_formatting) {
2181 format_paragraph (edit, 0);
2182 edit->force |= REDRAW_PAGE;
2183 } else
2184 check_and_wrap_line (edit);
2185 edit->found_len = 0;
2186 edit->prev_col = edit_get_col (edit);
2187 edit->search_start = edit->curs1;
2188 edit_find_bracket (edit);
2189 return;
2191 switch (command) {
2192 case CK_Begin_Page:
2193 case CK_End_Page:
2194 case CK_Begin_Page_Highlight:
2195 case CK_End_Page_Highlight:
2196 case CK_Word_Left:
2197 case CK_Word_Right:
2198 case CK_Up:
2199 case CK_Down:
2200 case CK_Word_Left_Highlight:
2201 case CK_Word_Right_Highlight:
2202 case CK_Up_Highlight:
2203 case CK_Down_Highlight:
2204 if (edit->mark2 == -1)
2205 break; /*marking is following the cursor: may need to highlight a whole line */
2206 case CK_Left:
2207 case CK_Right:
2208 case CK_Left_Highlight:
2209 case CK_Right_Highlight:
2210 edit->force |= REDRAW_CHAR_ONLY;
2213 /* basic cursor key commands */
2214 switch (command) {
2215 case CK_BackSpace:
2216 if (option_backspace_through_tabs && is_in_indent (edit)) {
2217 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2218 && edit->curs1 > 0)
2219 edit_backspace (edit);
2220 break;
2221 } else {
2222 if (option_fake_half_tabs) {
2223 int i;
2224 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2225 for (i = 0; i < HALF_TAB_SIZE; i++)
2226 edit_backspace (edit);
2227 break;
2231 edit_backspace (edit);
2232 break;
2233 case CK_Delete:
2234 if (option_fake_half_tabs) {
2235 int i;
2236 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2237 for (i = 1; i <= HALF_TAB_SIZE; i++)
2238 edit_delete (edit);
2239 break;
2242 edit_delete (edit);
2243 break;
2244 case CK_Delete_Word_Left:
2245 edit_left_delete_word (edit);
2246 break;
2247 case CK_Delete_Word_Right:
2248 edit_right_delete_word (edit);
2249 break;
2250 case CK_Delete_Line:
2251 edit_delete_line (edit);
2252 break;
2253 case CK_Delete_To_Line_End:
2254 edit_delete_to_line_end (edit);
2255 break;
2256 case CK_Delete_To_Line_Begin:
2257 edit_delete_to_line_begin (edit);
2258 break;
2259 case CK_Enter:
2260 if (option_auto_para_formatting) {
2261 edit_double_newline (edit);
2262 if (option_return_does_auto_indent)
2263 edit_auto_indent (edit);
2264 format_paragraph (edit, 0);
2265 } else {
2266 edit_insert (edit, '\n');
2267 if (option_return_does_auto_indent) {
2268 edit_auto_indent (edit);
2271 break;
2272 case CK_Return:
2273 edit_insert (edit, '\n');
2274 break;
2276 case CK_Page_Up:
2277 case CK_Page_Up_Highlight:
2278 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2279 break;
2280 case CK_Page_Down:
2281 case CK_Page_Down_Highlight:
2282 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2283 break;
2284 case CK_Left:
2285 case CK_Left_Highlight:
2286 if (option_fake_half_tabs) {
2287 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2288 edit_cursor_move (edit, -HALF_TAB_SIZE);
2289 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2290 break;
2293 edit_cursor_move (edit, -1);
2294 break;
2295 case CK_Right:
2296 case CK_Right_Highlight:
2297 if (option_fake_half_tabs) {
2298 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2299 edit_cursor_move (edit, HALF_TAB_SIZE);
2300 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2301 break;
2304 edit_cursor_move (edit, 1);
2305 break;
2306 case CK_Begin_Page:
2307 case CK_Begin_Page_Highlight:
2308 edit_begin_page (edit);
2309 break;
2310 case CK_End_Page:
2311 case CK_End_Page_Highlight:
2312 edit_end_page (edit);
2313 break;
2314 case CK_Word_Left:
2315 case CK_Word_Left_Highlight:
2316 edit_left_word_move_cmd (edit);
2317 break;
2318 case CK_Word_Right:
2319 case CK_Word_Right_Highlight:
2320 edit_right_word_move_cmd (edit);
2321 break;
2322 case CK_Up:
2323 case CK_Up_Highlight:
2324 edit_move_up (edit, 1, 0);
2325 break;
2326 case CK_Down:
2327 case CK_Down_Highlight:
2328 edit_move_down (edit, 1, 0);
2329 break;
2330 case CK_Paragraph_Up:
2331 case CK_Paragraph_Up_Highlight:
2332 edit_move_up_paragraph (edit, 0);
2333 break;
2334 case CK_Paragraph_Down:
2335 case CK_Paragraph_Down_Highlight:
2336 edit_move_down_paragraph (edit, 0);
2337 break;
2338 case CK_Scroll_Up:
2339 case CK_Scroll_Up_Highlight:
2340 edit_move_up (edit, 1, 1);
2341 break;
2342 case CK_Scroll_Down:
2343 case CK_Scroll_Down_Highlight:
2344 edit_move_down (edit, 1, 1);
2345 break;
2346 case CK_Home:
2347 case CK_Home_Highlight:
2348 edit_cursor_to_bol (edit);
2349 break;
2350 case CK_End:
2351 case CK_End_Highlight:
2352 edit_cursor_to_eol (edit);
2353 break;
2355 case CK_Tab:
2356 edit_tab_cmd (edit);
2357 if (option_auto_para_formatting) {
2358 format_paragraph (edit, 0);
2359 edit->force |= REDRAW_PAGE;
2360 } else
2361 check_and_wrap_line (edit);
2362 break;
2364 case CK_Toggle_Insert:
2365 edit->overwrite = (edit->overwrite == 0);
2366 break;
2368 case CK_Mark:
2369 if (edit->mark2 >= 0) {
2370 if (column_highlighting)
2371 edit_push_action (edit, COLUMN_ON);
2372 column_highlighting = 0;
2374 edit_mark_cmd (edit, 0);
2375 break;
2376 case CK_Column_Mark:
2377 if (!column_highlighting)
2378 edit_push_action (edit, COLUMN_OFF);
2379 column_highlighting = 1;
2380 edit_mark_cmd (edit, 0);
2381 break;
2382 case CK_Unmark:
2383 if (column_highlighting)
2384 edit_push_action (edit, COLUMN_ON);
2385 column_highlighting = 0;
2386 edit_mark_cmd (edit, 1);
2387 break;
2389 case CK_Toggle_Bookmark:
2390 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2391 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2392 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2393 else
2394 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2395 break;
2396 case CK_Flush_Bookmarks:
2397 book_mark_flush (edit, BOOK_MARK_COLOR);
2398 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2399 edit->force |= REDRAW_PAGE;
2400 break;
2401 case CK_Next_Bookmark:
2402 if (edit->book_mark) {
2403 struct _book_mark *p;
2404 p = (struct _book_mark *) book_mark_find (edit,
2405 edit->curs_line);
2406 if (p->next) {
2407 p = p->next;
2408 if (p->line >= edit->start_line + edit->num_widget_lines
2409 || p->line < edit->start_line)
2410 edit_move_display (edit,
2411 p->line -
2412 edit->num_widget_lines / 2);
2413 edit_move_to_line (edit, p->line);
2416 break;
2417 case CK_Prev_Bookmark:
2418 if (edit->book_mark) {
2419 struct _book_mark *p;
2420 p = (struct _book_mark *) book_mark_find (edit,
2421 edit->curs_line);
2422 while (p->line == edit->curs_line)
2423 if (p->prev)
2424 p = p->prev;
2425 if (p->line >= 0) {
2426 if (p->line >= edit->start_line + edit->num_widget_lines
2427 || p->line < edit->start_line)
2428 edit_move_display (edit,
2429 p->line -
2430 edit->num_widget_lines / 2);
2431 edit_move_to_line (edit, p->line);
2434 break;
2436 case CK_Beginning_Of_Text:
2437 case CK_Beginning_Of_Text_Highlight:
2438 edit_move_to_top (edit);
2439 break;
2440 case CK_End_Of_Text:
2441 case CK_End_Of_Text_Highlight:
2442 edit_move_to_bottom (edit);
2443 break;
2445 case CK_Copy:
2446 edit_block_copy_cmd (edit);
2447 break;
2448 case CK_Remove:
2449 edit_block_delete_cmd (edit);
2450 break;
2451 case CK_Move:
2452 edit_block_move_cmd (edit);
2453 break;
2455 case CK_XStore:
2456 edit_copy_to_X_buf_cmd (edit);
2457 break;
2458 case CK_XCut:
2459 edit_cut_to_X_buf_cmd (edit);
2460 break;
2461 case CK_XPaste:
2462 edit_paste_from_X_buf_cmd (edit);
2463 break;
2464 case CK_Selection_History:
2465 edit_paste_from_history (edit);
2466 break;
2468 case CK_Save_As:
2469 edit_save_as_cmd (edit);
2470 break;
2471 case CK_Save:
2472 edit_save_confirm_cmd (edit);
2473 break;
2474 case CK_Load:
2475 edit_load_cmd (edit);
2476 break;
2477 case CK_Save_Block:
2478 edit_save_block_cmd (edit);
2479 break;
2480 case CK_Insert_File:
2481 edit_insert_file_cmd (edit);
2482 break;
2484 case CK_Toggle_Syntax:
2485 if ((option_syntax_highlighting ^= 1) == 1)
2486 edit_load_syntax (edit, NULL, option_syntax_type);
2487 edit->force |= REDRAW_PAGE;
2488 break;
2490 case CK_Find:
2491 edit_search_cmd (edit, 0);
2492 break;
2493 case CK_Find_Again:
2494 edit_search_cmd (edit, 1);
2495 break;
2496 case CK_Replace:
2497 edit_replace_cmd (edit, 0);
2498 break;
2499 case CK_Replace_Again:
2500 edit_replace_cmd (edit, 1);
2501 break;
2502 case CK_Complete_Word:
2503 edit_complete_word_cmd (edit);
2504 break;
2506 case CK_Exit:
2507 dlg_stop (edit->widget.parent);
2508 break;
2509 case CK_New:
2510 edit_new_cmd (edit);
2511 break;
2513 case CK_Help:
2514 edit_help_cmd (edit);
2515 break;
2517 case CK_Refresh:
2518 edit_refresh_cmd (edit);
2519 break;
2521 case CK_Date:{
2522 char s[1024];
2523 /* fool gcc to prevent a Y2K warning */
2524 char time_format[] = "_c";
2525 time_format[0] = '%';
2527 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
2528 edit_print_string (edit, s);
2529 edit->force |= REDRAW_PAGE;
2530 break;
2532 case CK_Goto:
2533 edit_goto_cmd (edit);
2534 break;
2535 case CK_Paragraph_Format:
2536 format_paragraph (edit, 1);
2537 edit->force |= REDRAW_PAGE;
2538 break;
2539 case CK_Delete_Macro:
2540 edit_delete_macro_cmd (edit);
2541 break;
2542 case CK_Match_Bracket:
2543 edit_goto_matching_bracket (edit);
2544 break;
2545 case CK_User_Menu:
2546 user_menu (edit);
2547 break;
2548 case CK_Sort:
2549 edit_sort_cmd (edit);
2550 break;
2551 case CK_ExtCmd:
2552 edit_ext_cmd (edit);
2553 break;
2554 case CK_Mail:
2555 edit_mail_dialog (edit);
2556 break;
2557 case CK_Shell:
2558 view_other_cmd ();
2559 break;
2560 case CK_Select_Codepage:
2561 edit_select_codepage_cmd (edit);
2562 break;
2563 case CK_Insert_Literal:
2564 edit_insert_literal_cmd (edit);
2565 break;
2566 case CK_Execute_Macro:
2567 edit_execute_macro_cmd (edit);
2568 break;
2569 case CK_Begin_End_Macro:
2570 edit_begin_end_macro_cmd (edit);
2571 break;
2572 case CK_Ext_Mode:
2573 edit->extmod = 1;
2574 break;
2575 default:
2576 break;
2579 /* CK_Pipe_Block */
2580 if ((command / 1000) == 1) /* a shell command */
2581 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2582 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2583 struct macro m[MAX_MACRO_LENGTH];
2584 int nm;
2585 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
2586 edit_execute_macro (edit, m, nm);
2589 /* keys which must set the col position, and the search vars */
2590 switch (command) {
2591 case CK_Find:
2592 case CK_Find_Again:
2593 case CK_Replace:
2594 case CK_Replace_Again:
2595 case CK_Complete_Word:
2596 edit->prev_col = edit_get_col (edit);
2597 break;
2598 case CK_Up:
2599 case CK_Up_Highlight:
2600 case CK_Down:
2601 case CK_Down_Highlight:
2602 case CK_Page_Up:
2603 case CK_Page_Up_Highlight:
2604 case CK_Page_Down:
2605 case CK_Page_Down_Highlight:
2606 case CK_Beginning_Of_Text:
2607 case CK_Beginning_Of_Text_Highlight:
2608 case CK_End_Of_Text:
2609 case CK_End_Of_Text_Highlight:
2610 case CK_Paragraph_Up:
2611 case CK_Paragraph_Up_Highlight:
2612 case CK_Paragraph_Down:
2613 case CK_Paragraph_Down_Highlight:
2614 case CK_Scroll_Up:
2615 case CK_Scroll_Up_Highlight:
2616 case CK_Scroll_Down:
2617 case CK_Scroll_Down_Highlight:
2618 edit->search_start = edit->curs1;
2619 edit->found_len = 0;
2620 break;
2621 default:
2622 edit->found_len = 0;
2623 edit->prev_col = edit_get_col (edit);
2624 edit->search_start = edit->curs1;
2626 edit_find_bracket (edit);
2628 if (option_auto_para_formatting) {
2629 switch (command) {
2630 case CK_BackSpace:
2631 case CK_Delete:
2632 case CK_Delete_Word_Left:
2633 case CK_Delete_Word_Right:
2634 case CK_Delete_To_Line_End:
2635 case CK_Delete_To_Line_Begin:
2636 format_paragraph (edit, 0);
2637 edit->force |= REDRAW_PAGE;
2643 static void
2644 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2646 int i = 0;
2648 if (edit->macro_depth++ > 256) {
2649 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2650 edit->macro_depth--;
2651 return;
2653 edit->force |= REDRAW_PAGE;
2654 for (; i < n; i++) {
2655 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2657 edit_update_screen (edit);
2658 edit->macro_depth--;
2661 /* User edit menu, like user menu (F2) but only in editor. */
2662 static void
2663 user_menu (WEdit * edit)
2665 FILE *fd;
2666 int nomark;
2667 struct stat status;
2668 long start_mark, end_mark;
2669 char *block_file = mhl_str_dir_plus_file (home_dir, BLOCK_FILE);
2670 int rc = 0;
2672 nomark = eval_marks (edit, &start_mark, &end_mark);
2673 if (!nomark) /* remember marked or not */
2674 edit_save_block (edit, block_file, start_mark, end_mark);
2676 /* run shell scripts from menu */
2677 user_menu_cmd (edit);
2679 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2680 /* no block messages */
2681 goto cleanup;
2684 if (!nomark) {
2685 /* i.e. we have marked block */
2686 rc = edit_block_delete_cmd (edit);
2689 if (!rc) {
2690 edit_insert_file (edit, block_file);
2693 /* truncate block file */
2694 if ((fd = fopen (block_file, "w"))) {
2695 fclose (fd);
2698 edit_refresh_cmd (edit);
2699 edit->force |= REDRAW_COMPLETELY;
2701 cleanup:
2702 g_free (block_file);