merged in lzma patch from mandriva
[midnight-commander.git] / edit / edit.c
blob153b549e3de4f3e4144e18bef7b214e60b852a06
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>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <stdlib.h>
36 #include <mhl/memory.h>
37 #include <mhl/string.h>
39 #include "../src/global.h"
41 #include "edit.h"
42 #include "editlock.h"
43 #include "edit-widget.h"
44 #include "editcmddef.h"
45 #include "usermap.h"
47 #include "../src/cmd.h" /* view_other_cmd() */
48 #include "../src/user.h" /* user_menu_cmd() */
49 #include "../src/wtools.h" /* query_dialog() */
50 #include "../src/timefmt.h" /* time formatting */
54 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
55 or EDIT_KEY_EMULATION_EMACS
57 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
59 int option_word_wrap_line_length = 72;
60 int option_typewriter_wrap = 0;
61 int option_auto_para_formatting = 0;
62 int option_tab_spacing = 8;
63 int option_fill_tabs_with_spaces = 0;
64 int option_return_does_auto_indent = 1;
65 int option_backspace_through_tabs = 0;
66 int option_fake_half_tabs = 1;
67 int option_save_mode = EDIT_QUICK_SAVE;
68 int option_save_position = 1;
69 int option_max_undo = 32768;
71 int option_edit_right_extreme = 0;
72 int option_edit_left_extreme = 0;
73 int option_edit_top_extreme = 0;
74 int option_edit_bottom_extreme = 0;
76 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
77 char *option_backup_ext = NULL;
79 /*-
81 * here's a quick sketch of the layout: (don't run this through indent.)
83 * (b1 is buffers1 and b2 is buffers2)
85 * |
86 * \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
87 * ______________________________________|______________________________________
88 * |
89 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
90 * |-> |-> |-> |-> |-> |-> |
91 * |
92 * _<------------------------->|<----------------->_
93 * WEdit->curs2 | WEdit->curs1
94 * ^ | ^
95 * | ^|^ |
96 * cursor ||| cursor
97 * |||
98 * file end|||file beginning
99 * |
103 * This_is_some_file
104 * fin.
108 static void user_menu (WEdit *edit);
110 int edit_get_byte (WEdit * edit, long byte_index)
112 unsigned long p;
113 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
114 return '\n';
116 if (byte_index >= edit->curs1) {
117 p = edit->curs1 + edit->curs2 - byte_index - 1;
118 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
119 } else {
120 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
125 * Initialize the buffers for an empty files.
127 static void
128 edit_init_buffers (WEdit *edit)
130 int j;
132 for (j = 0; j <= MAXBUFF; j++) {
133 edit->buffers1[j] = NULL;
134 edit->buffers2[j] = NULL;
137 edit->curs1 = 0;
138 edit->curs2 = 0;
139 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
143 * Load file OR text into buffers. Set cursor to the beginning of file.
144 * Return 1 on error.
146 static int
147 edit_load_file_fast (WEdit *edit, const char *filename)
149 long buf, buf2;
150 int file = -1;
152 edit->curs2 = edit->last_byte;
153 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
155 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
156 char errmsg[8192];
157 snprintf(errmsg, sizeof(errmsg), _(" Cannot open %s for reading "), filename);
158 edit_error_dialog (_("Error"), get_sys_error (errmsg));
159 return 1;
162 if (!edit->buffers2[buf2])
163 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
165 mc_read (file,
166 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
167 (edit->curs2 & M_EDIT_BUF_SIZE),
168 edit->curs2 & M_EDIT_BUF_SIZE);
170 for (buf = buf2 - 1; buf >= 0; buf--) {
171 /* edit->buffers2[0] is already allocated */
172 if (!edit->buffers2[buf])
173 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
174 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
177 mc_close (file);
178 return 0;
181 /* detecting an error on save is easy: just check if every byte has been written. */
182 /* detecting an error on read, is not so easy 'cos there is not way to tell
183 whether you read everything or not. */
184 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
185 static const struct edit_filters {
186 const char *read, *write, *extension;
187 } all_filters[] = {
188 { "lzma -cd %s 2>&1", "lzma > %s", ".bz2" },
189 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
190 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
191 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
194 /* Return index of the filter or -1 is there is no appropriate filter */
195 static int edit_find_filter (const char *filename)
197 size_t i, l, e;
198 if (!filename)
199 return -1;
200 l = strlen (filename);
201 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
202 e = strlen (all_filters[i].extension);
203 if (l > e)
204 if (!strcmp (all_filters[i].extension, filename + l - e))
205 return i;
207 return -1;
210 static char *
211 edit_get_filter (const char *filename)
213 int i, l;
214 char *p, *quoted_name;
215 i = edit_find_filter (filename);
216 if (i < 0)
217 return 0;
218 quoted_name = name_quote (filename, 0);
219 l = strlen (quoted_name);
220 p = g_malloc (strlen (all_filters[i].read) + l + 2);
221 sprintf (p, all_filters[i].read, quoted_name);
222 mhl_mem_free (quoted_name);
223 return p;
226 char *
227 edit_get_write_filter (const char *write_name, const char *filename)
229 int i, l;
230 char *p, *writename;
231 i = edit_find_filter (filename);
232 if (i < 0)
233 return 0;
234 writename = name_quote (write_name, 0);
235 l = strlen (writename);
236 p = g_malloc (strlen (all_filters[i].write) + l + 2);
237 sprintf (p, all_filters[i].write, writename);
238 mhl_mem_free (writename);
239 return p;
242 static long
243 edit_insert_stream (WEdit * edit, FILE * f)
245 int c;
246 long i = 0;
247 while ((c = fgetc (f)) >= 0) {
248 edit_insert (edit, c);
249 i++;
251 return i;
254 long edit_write_stream (WEdit * edit, FILE * f)
256 long i;
257 for (i = 0; i < edit->last_byte; i++)
258 if (fputc (edit_get_byte (edit, i), f) < 0)
259 break;
260 return i;
263 #define TEMP_BUF_LEN 1024
265 /* inserts a file at the cursor, returns 1 on success */
267 edit_insert_file (WEdit *edit, const char *filename)
269 char *p;
270 if ((p = edit_get_filter (filename))) {
271 FILE *f;
272 long current = edit->curs1;
273 f = (FILE *) popen (p, "r");
274 if (f) {
275 edit_insert_stream (edit, f);
276 edit_cursor_move (edit, current - edit->curs1);
277 if (pclose (f) > 0) {
278 char errmsg[8192];
279 snprintf(errmsg, sizeof(errmsg), _(" Error reading from pipe: %s "), p);
280 edit_error_dialog (_("Error"), errmsg);
281 mhl_mem_free (p);
282 return 0;
284 } else {
285 char errmsg[8192];
286 snprintf(errmsg, sizeof(errmsg), _(" Cannot open pipe for reading: %s "), p);
287 edit_error_dialog (_("Error"), errmsg);
288 mhl_mem_free (p);
289 return 0;
291 mhl_mem_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 mhl_mem_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 char errmsg[8192];
318 errmsg[0] = 0;
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 snprintf (errmsg, sizeof(errmsg), _(" 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 snprintf (errmsg, sizeof(errmsg), _(" Cannot get size/permissions for %s "), filename);
344 goto cleanup;
347 /* We want to open regular files only */
348 if (!S_ISREG (st->st_mode)) {
349 snprintf (errmsg, sizeof(errmsg), _(" %s is not a regular file "), filename);
350 goto cleanup;
354 * Don't delete non-empty files.
355 * O_EXCL should prevent it, but let's be on the safe side.
357 if (st->st_size > 0) {
358 edit->delete_file = 0;
361 if (st->st_size >= SIZE_LIMIT) {
362 snprintf (errmsg, sizeof(errmsg), _(" File %s is too large "), filename);
363 goto cleanup;
366 cleanup:
367 (void) mc_close (file);
368 if (errmsg[0]) {
369 edit_error_dialog (_("Error"), errmsg);
370 return 1;
372 return 0;
376 * Open the file and load it into the buffers, either directly or using
377 * a filter. Return 0 on success, 1 on error.
379 * Fast loading (edit_load_file_fast) is used when the file size is
380 * known. In this case the data is read into the buffers by blocks.
381 * If the file size is not known, the data is loaded byte by byte in
382 * edit_insert_file.
384 static int
385 edit_load_file (WEdit *edit)
387 int fast_load = 1;
389 /* Cannot do fast load if a filter is used */
390 if (edit_find_filter (edit->filename) >= 0)
391 fast_load = 0;
394 * VFS may report file size incorrectly, and slow load is not a big
395 * deal considering overhead in VFS.
397 if (!vfs_file_is_local (edit->filename))
398 fast_load = 0;
401 * FIXME: line end translation should disable fast loading as well
402 * Consider doing fseek() to the end and ftell() for the real size.
405 if (*edit->filename) {
406 /* If we are dealing with a real file, check that it exists */
407 if (check_file_access (edit, edit->filename, &edit->stat1))
408 return 1;
409 } else {
410 /* nothing to load */
411 fast_load = 0;
414 edit_init_buffers (edit);
416 if (fast_load) {
417 edit->last_byte = edit->stat1.st_size;
418 edit_load_file_fast (edit, edit->filename);
419 /* If fast load was used, the number of lines wasn't calculated */
420 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
421 } else {
422 edit->last_byte = 0;
423 if (*edit->filename) {
424 edit->stack_disable = 1;
425 if (!edit_insert_file (edit, edit->filename)) {
426 edit_clean (edit);
427 return 1;
429 edit->stack_disable = 0;
432 return 0;
435 /* Restore saved cursor position in the file */
436 static void
437 edit_load_position (WEdit *edit)
439 char *filename;
440 long line, column;
442 if (!edit->filename || !*edit->filename)
443 return;
445 filename = vfs_canon (edit->filename);
446 load_file_position (filename, &line, &column);
447 mhl_mem_free (filename);
449 edit_move_to_line (edit, line - 1);
450 edit->prev_col = column;
451 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
452 edit_move_display (edit, line - (edit->num_widget_lines / 2));
455 /* Save cursor position in the file */
456 static void
457 edit_save_position (WEdit *edit)
459 char *filename;
461 if (!edit->filename || !*edit->filename)
462 return;
464 filename = vfs_canon (edit->filename);
465 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
466 mhl_mem_free (filename);
469 /* Clean the WEdit stricture except the widget part */
470 static inline void
471 edit_purge_widget (WEdit *edit)
473 int len = sizeof (WEdit) - sizeof (Widget);
474 char *start = (char *) edit + sizeof (Widget);
475 memset (start, 0, len);
476 edit->macro_i = -1; /* not recording a macro */
479 #define space_width 1
482 * Fill in the edit structure. Return NULL on failure. Pass edit as
483 * NULL to allocate a new structure.
485 * If line is 0, try to restore saved position. Otherwise put the
486 * cursor on that line and show it in the middle of the screen.
488 WEdit *
489 edit_init (WEdit *edit, int lines, int columns, const char *filename,
490 long line)
492 int to_free = 0;
493 option_auto_syntax = 1; /* Resetting to auto on every invokation */
495 if (!edit) {
496 #ifdef ENABLE_NLS
498 * Expand option_whole_chars_search by national letters using
499 * current locale
502 static char option_whole_chars_search_buf[256];
504 if (option_whole_chars_search_buf != option_whole_chars_search) {
505 size_t i;
506 size_t len = strlen (option_whole_chars_search);
508 strcpy (option_whole_chars_search_buf,
509 option_whole_chars_search);
511 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
512 if (islower (i) && !strchr (option_whole_chars_search, i)) {
513 option_whole_chars_search_buf[len++] = i;
517 option_whole_chars_search_buf[len] = 0;
518 option_whole_chars_search = option_whole_chars_search_buf;
520 #endif /* ENABLE_NLS */
521 edit = g_malloc0 (sizeof (WEdit));
522 to_free = 1;
524 edit_purge_widget (edit);
525 edit->num_widget_lines = lines;
526 edit->num_widget_columns = columns;
527 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
528 edit->stat1.st_uid = getuid ();
529 edit->stat1.st_gid = getgid ();
530 edit->stat1.st_mtime = 0;
531 edit->bracket = -1;
532 edit->force |= REDRAW_PAGE;
533 edit_set_filename (edit, filename);
534 edit->stack_size = START_STACK_SIZE;
535 edit->stack_size_mask = START_STACK_SIZE - 1;
536 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
537 if (edit_load_file (edit)) {
538 /* edit_load_file already gives an error message */
539 if (to_free)
540 mhl_mem_free (edit);
541 return 0;
543 edit->loading_done = 1;
544 edit->modified = 0;
545 edit->locked = 0;
546 edit_load_syntax (edit, 0, 0);
548 int color;
549 edit_get_syntax_color (edit, -1, &color);
552 /* load saved cursor position */
553 if ((line == 0) && option_save_position) {
554 edit_load_position (edit);
555 } else {
556 if (line <= 0)
557 line = 1;
558 edit_move_display (edit, line - 1);
559 edit_move_to_line (edit, line - 1);
562 edit_load_user_map(edit);
564 return edit;
567 /* Clear the edit struct, freeing everything in it. Return 1 on success */
569 edit_clean (WEdit *edit)
571 int j = 0;
573 if (!edit)
574 return 0;
576 /* a stale lock, remove it */
577 if (edit->locked)
578 edit->locked = edit_unlock_file (edit->filename);
580 /* save cursor position */
581 if (option_save_position)
582 edit_save_position (edit);
584 /* File specified on the mcedit command line and never saved */
585 if (edit->delete_file)
586 unlink (edit->filename);
588 edit_free_syntax_rules (edit);
589 book_mark_flush (edit, -1);
590 for (; j <= MAXBUFF; j++) {
591 mhl_mem_free (edit->buffers1[j]);
592 mhl_mem_free (edit->buffers2[j]);
595 mhl_mem_free (edit->undo_stack);
596 mhl_mem_free (edit->filename);
597 mhl_mem_free (edit->dir);
599 edit_purge_widget (edit);
601 /* Free temporary strings used in catstrs() */
602 freestrs ();
604 return 1;
608 /* returns 1 on success */
609 int edit_renew (WEdit * edit)
611 int lines = edit->num_widget_lines;
612 int columns = edit->num_widget_columns;
613 int retval = 1;
615 edit_clean (edit);
616 if (!edit_init (edit, lines, columns, "", 0))
617 retval = 0;
618 return retval;
622 * Load a new file into the editor. If it fails, preserve the old file.
623 * To do it, allocate a new widget, initialize it and, if the new file
624 * was loaded, copy the data to the old widget.
625 * Return 1 on success, 0 on failure.
628 edit_reload (WEdit *edit, const char *filename)
630 WEdit *e;
631 int lines = edit->num_widget_lines;
632 int columns = edit->num_widget_columns;
634 e = g_malloc0 (sizeof (WEdit));
635 e->widget = edit->widget;
636 if (!edit_init (e, lines, columns, filename, 0)) {
637 mhl_mem_free (e);
638 return 0;
640 edit_clean (edit);
641 memcpy (edit, e, sizeof (WEdit));
642 mhl_mem_free (e);
643 return 1;
648 Recording stack for undo:
649 The following is an implementation of a compressed stack. Identical
650 pushes are recorded by a negative prefix indicating the number of times the
651 same char was pushed. This saves space for repeated curs-left or curs-right
652 delete etc.
656 pushed: stored:
660 b -3
662 c --> -4
668 If the stack long int is 0-255 it represents a normal insert (from a backspace),
669 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
670 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
671 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
672 position.
674 The only way the cursor moves or the buffer is changed is through the routines:
675 insert, backspace, insert_ahead, delete, and cursor_move.
676 These record the reverse undo movements onto the stack each time they are
677 called.
679 Each key press results in a set of actions (insert; delete ...). So each time
680 a key is pressed the current position of start_display is pushed as
681 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
682 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
683 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
687 void edit_push_action (WEdit * edit, long c,...)
689 unsigned long sp = edit->stack_pointer;
690 unsigned long spm1;
691 long *t;
693 /* first enlarge the stack if necessary */
694 if (sp > edit->stack_size - 10) { /* say */
695 if (option_max_undo < 256)
696 option_max_undo = 256;
697 if (edit->stack_size < (unsigned long) option_max_undo) {
698 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
699 if (t) {
700 edit->undo_stack = t;
701 edit->stack_size <<= 1;
702 edit->stack_size_mask = edit->stack_size - 1;
706 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
707 if (edit->stack_disable)
708 return;
710 #ifdef FAST_MOVE_CURSOR
711 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
712 va_list ap;
713 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
714 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
715 va_start (ap, c);
716 c = -(va_arg (ap, int));
717 va_end (ap);
718 } else
719 #endif /* ! FAST_MOVE_CURSOR */
720 if (edit->stack_bottom != sp
721 && spm1 != edit->stack_bottom
722 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
723 int d;
724 if (edit->undo_stack[spm1] < 0) {
725 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
726 if (d == c) {
727 if (edit->undo_stack[spm1] > -1000000000) {
728 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
729 edit->undo_stack[spm1]--;
730 return;
733 /* #define NO_STACK_CURSMOVE_ANIHILATION */
734 #ifndef NO_STACK_CURSMOVE_ANIHILATION
735 else if ((c == CURS_LEFT && d == CURS_RIGHT)
736 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
737 if (edit->undo_stack[spm1] == -2)
738 edit->stack_pointer = spm1;
739 else
740 edit->undo_stack[spm1]++;
741 return;
743 #endif
744 } else {
745 d = edit->undo_stack[spm1];
746 if (d == c) {
747 if (c >= KEY_PRESS)
748 return; /* --> no need to push multiple do-nothings */
749 edit->undo_stack[sp] = -2;
750 goto check_bottom;
752 #ifndef NO_STACK_CURSMOVE_ANIHILATION
753 else if ((c == CURS_LEFT && d == CURS_RIGHT)
754 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
755 edit->stack_pointer = spm1;
756 return;
758 #endif
761 edit->undo_stack[sp] = c;
762 check_bottom:
764 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
766 /* if the sp wraps round and catches the stack_bottom then erase
767 * the first set of actions on the stack to make space - by moving
768 * stack_bottom forward one "key press" */
769 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
770 if ((unsigned long) c == edit->stack_bottom ||
771 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
772 do {
773 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
774 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
776 /*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: */
777 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
778 edit->stack_bottom = edit->stack_pointer = 0;
782 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
783 then the file should be as it was when he loaded up. Then set edit->modified to 0.
785 static long
786 pop_action (WEdit * edit)
788 long c;
789 unsigned long sp = edit->stack_pointer;
790 if (sp == edit->stack_bottom) {
791 return STACK_BOTTOM;
793 sp = (sp - 1) & edit->stack_size_mask;
794 if ((c = edit->undo_stack[sp]) >= 0) {
795 /* edit->undo_stack[sp] = '@'; */
796 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
797 return c;
799 if (sp == edit->stack_bottom) {
800 return STACK_BOTTOM;
802 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
803 if (edit->undo_stack[sp] == -2) {
804 /* edit->undo_stack[sp] = '@'; */
805 edit->stack_pointer = sp;
806 } else
807 edit->undo_stack[sp]++;
809 return c;
812 /* is called whenever a modification is made by one of the four routines below */
813 static inline void edit_modification (WEdit * edit)
815 edit->caches_valid = 0;
816 edit->screen_modified = 1;
818 /* raise lock when file modified */
819 if (!edit->modified && !edit->delete_file)
820 edit->locked = edit_lock_file (edit->filename);
821 edit->modified = 1;
825 Basic low level single character buffer alterations and movements at the cursor.
826 Returns char passed over, inserted or removed.
829 void
830 edit_insert (WEdit *edit, int c)
832 /* check if file has grown to large */
833 if (edit->last_byte >= SIZE_LIMIT)
834 return;
836 /* first we must update the position of the display window */
837 if (edit->curs1 < edit->start_display) {
838 edit->start_display++;
839 if (c == '\n')
840 edit->start_line++;
843 /* Mark file as modified, unless the file hasn't been fully loaded */
844 if (edit->loading_done) {
845 edit_modification (edit);
848 /* now we must update some info on the file and check if a redraw is required */
849 if (c == '\n') {
850 if (edit->book_mark)
851 book_mark_inc (edit, edit->curs_line);
852 edit->curs_line++;
853 edit->total_lines++;
854 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
857 /* save the reverse command onto the undo stack */
858 edit_push_action (edit, BACKSPACE);
860 /* update markers */
861 edit->mark1 += (edit->mark1 > edit->curs1);
862 edit->mark2 += (edit->mark2 > edit->curs1);
863 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
865 /* add a new buffer if we've reached the end of the last one */
866 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
867 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
868 g_malloc (EDIT_BUF_SIZE);
870 /* perform the insertion */
871 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
872 curs1 & M_EDIT_BUF_SIZE]
873 = (unsigned char) c;
875 /* update file length */
876 edit->last_byte++;
878 /* update cursor position */
879 edit->curs1++;
883 /* same as edit_insert and move left */
884 void edit_insert_ahead (WEdit * edit, int c)
886 if (edit->last_byte >= SIZE_LIMIT)
887 return;
888 if (edit->curs1 < edit->start_display) {
889 edit->start_display++;
890 if (c == '\n')
891 edit->start_line++;
893 edit_modification (edit);
894 if (c == '\n') {
895 if (edit->book_mark)
896 book_mark_inc (edit, edit->curs_line);
897 edit->total_lines++;
898 edit->force |= REDRAW_AFTER_CURSOR;
900 edit_push_action (edit, DELCHAR);
902 edit->mark1 += (edit->mark1 >= edit->curs1);
903 edit->mark2 += (edit->mark2 >= edit->curs1);
904 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
906 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
907 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
908 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
910 edit->last_byte++;
911 edit->curs2++;
915 int edit_delete (WEdit * edit)
917 int p;
918 if (!edit->curs2)
919 return 0;
921 edit->mark1 -= (edit->mark1 > edit->curs1);
922 edit->mark2 -= (edit->mark2 > edit->curs1);
923 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
925 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
927 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
928 mhl_mem_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
929 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
931 edit->last_byte--;
932 edit->curs2--;
934 edit_modification (edit);
935 if (p == '\n') {
936 if (edit->book_mark)
937 book_mark_dec (edit, edit->curs_line);
938 edit->total_lines--;
939 edit->force |= REDRAW_AFTER_CURSOR;
941 edit_push_action (edit, p + 256);
942 if (edit->curs1 < edit->start_display) {
943 edit->start_display--;
944 if (p == '\n')
945 edit->start_line--;
948 return p;
952 static int
953 edit_backspace (WEdit * edit)
955 int p;
956 if (!edit->curs1)
957 return 0;
959 edit->mark1 -= (edit->mark1 >= edit->curs1);
960 edit->mark2 -= (edit->mark2 >= edit->curs1);
961 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
963 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
964 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
965 mhl_mem_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
966 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
968 edit->last_byte--;
969 edit->curs1--;
971 edit_modification (edit);
972 if (p == '\n') {
973 if (edit->book_mark)
974 book_mark_dec (edit, edit->curs_line);
975 edit->curs_line--;
976 edit->total_lines--;
977 edit->force |= REDRAW_AFTER_CURSOR;
979 edit_push_action (edit, p);
981 if (edit->curs1 < edit->start_display) {
982 edit->start_display--;
983 if (p == '\n')
984 edit->start_line--;
987 return p;
990 #ifdef FAST_MOVE_CURSOR
992 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
994 unsigned long next;
995 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
996 edit->curs_line--;
997 next -= (unsigned long) dest;
998 n -= next;
999 src += next;
1000 dest += next;
1005 edit_move_backward_lots (WEdit *edit, long increment)
1007 int r, s, t;
1008 unsigned char *p;
1010 if (increment > edit->curs1)
1011 increment = edit->curs1;
1012 if (increment <= 0)
1013 return -1;
1014 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1016 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1017 if (r > increment)
1018 r = increment;
1019 s = edit->curs1 & M_EDIT_BUF_SIZE;
1021 p = 0;
1022 if (s > r) {
1023 memqcpy (edit,
1024 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1025 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1027 } else {
1028 if (s) {
1029 memqcpy (edit,
1030 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1031 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1032 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1033 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1035 memqcpy (edit,
1036 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1037 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1038 EDIT_BUF_SIZE - (r - s), r - s);
1040 increment -= r;
1041 edit->curs1 -= r;
1042 edit->curs2 += r;
1043 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1044 if (p)
1045 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1046 else
1047 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1048 g_malloc (EDIT_BUF_SIZE);
1049 } else {
1050 mhl_mem_free (p);
1053 s = edit->curs1 & M_EDIT_BUF_SIZE;
1054 while (increment) {
1055 p = 0;
1056 r = EDIT_BUF_SIZE;
1057 if (r > increment)
1058 r = increment;
1059 t = s;
1060 if (r < t)
1061 t = r;
1062 memqcpy (edit,
1063 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1064 EDIT_BUF_SIZE - t,
1065 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1067 if (r >= s) {
1068 if (t) {
1069 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1070 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1072 memqcpy (edit,
1073 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1074 EDIT_BUF_SIZE - r,
1075 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1076 EDIT_BUF_SIZE - (r - s), r - s);
1078 increment -= r;
1079 edit->curs1 -= r;
1080 edit->curs2 += r;
1081 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1082 if (p)
1083 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1084 else
1085 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1086 g_malloc (EDIT_BUF_SIZE);
1087 } else {
1088 mhl_mem_free (p);
1091 return edit_get_byte (edit, edit->curs1);
1094 #endif /* ! FAST_MOVE_CURSOR */
1096 /* moves the cursor right or left: increment positive or negative respectively */
1097 void edit_cursor_move (WEdit * edit, long increment)
1099 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1100 int c;
1102 #ifdef FAST_MOVE_CURSOR
1103 if (increment < -256) {
1104 edit->force |= REDRAW_PAGE;
1105 edit_move_backward_lots (edit, -increment);
1106 return;
1108 #endif /* ! FAST_MOVE_CURSOR */
1110 if (increment < 0) {
1111 for (; increment < 0; increment++) {
1112 if (!edit->curs1)
1113 return;
1115 edit_push_action (edit, CURS_RIGHT);
1117 c = edit_get_byte (edit, edit->curs1 - 1);
1118 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1119 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1120 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1121 edit->curs2++;
1122 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1123 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1124 mhl_mem_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1125 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1127 edit->curs1--;
1128 if (c == '\n') {
1129 edit->curs_line--;
1130 edit->force |= REDRAW_LINE_BELOW;
1134 } else if (increment > 0) {
1135 for (; increment > 0; increment--) {
1136 if (!edit->curs2)
1137 return;
1139 edit_push_action (edit, CURS_LEFT);
1141 c = edit_get_byte (edit, edit->curs1);
1142 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1143 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1144 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1145 edit->curs1++;
1146 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1147 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1148 mhl_mem_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1149 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1151 edit->curs2--;
1152 if (c == '\n') {
1153 edit->curs_line++;
1154 edit->force |= REDRAW_LINE_ABOVE;
1160 /* These functions return positions relative to lines */
1162 /* returns index of last char on line + 1 */
1163 long edit_eol (WEdit * edit, long current)
1165 if (current < edit->last_byte) {
1166 for (;; current++)
1167 if (edit_get_byte (edit, current) == '\n')
1168 break;
1169 } else
1170 return edit->last_byte;
1171 return current;
1174 /* returns index of first char on line */
1175 long edit_bol (WEdit * edit, long current)
1177 if (current > 0) {
1178 for (;; current--)
1179 if (edit_get_byte (edit, current - 1) == '\n')
1180 break;
1181 } else
1182 return 0;
1183 return current;
1187 int edit_count_lines (WEdit * edit, long current, int upto)
1189 int lines = 0;
1190 if (upto > edit->last_byte)
1191 upto = edit->last_byte;
1192 if (current < 0)
1193 current = 0;
1194 while (current < upto)
1195 if (edit_get_byte (edit, current++) == '\n')
1196 lines++;
1197 return lines;
1201 /* If lines is zero this returns the count of lines from current to upto. */
1202 /* If upto is zero returns index of lines forward current. */
1203 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1205 if (upto) {
1206 return edit_count_lines (edit, current, upto);
1207 } else {
1208 int next;
1209 if (lines < 0)
1210 lines = 0;
1211 while (lines--) {
1212 next = edit_eol (edit, current) + 1;
1213 if (next > edit->last_byte)
1214 break;
1215 else
1216 current = next;
1218 return current;
1223 /* Returns offset of 'lines' lines up from current */
1224 long edit_move_backward (WEdit * edit, long current, int lines)
1226 if (lines < 0)
1227 lines = 0;
1228 current = edit_bol (edit, current);
1229 while((lines--) && current != 0)
1230 current = edit_bol (edit, current - 1);
1231 return current;
1234 /* If cols is zero this returns the count of columns from current to upto. */
1235 /* If upto is zero returns index of cols across from current. */
1236 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1238 long p, q;
1239 int col = 0;
1241 if (upto) {
1242 q = upto;
1243 cols = -10;
1244 } else
1245 q = edit->last_byte + 2;
1247 for (col = 0, p = current; p < q; p++) {
1248 int c;
1249 if (cols != -10) {
1250 if (col == cols)
1251 return p;
1252 if (col > cols)
1253 return p - 1;
1255 c = edit_get_byte (edit, p);
1256 if (c == '\t')
1257 col += TAB_SIZE - col % TAB_SIZE;
1258 else if (c == '\n') {
1259 if (upto)
1260 return col;
1261 else
1262 return p;
1263 } else if (c < 32 || c == 127)
1264 col += 2; /* Caret notation for control characters */
1265 else
1266 col++;
1268 return col;
1271 /* returns the current column position of the cursor */
1272 int edit_get_col (WEdit * edit)
1274 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1278 /* Scrolling functions */
1280 void edit_update_curs_row (WEdit * edit)
1282 edit->curs_row = edit->curs_line - edit->start_line;
1285 void edit_update_curs_col (WEdit * edit)
1287 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1290 /*moves the display start position up by i lines */
1291 void edit_scroll_upward (WEdit * edit, unsigned long i)
1293 unsigned long lines_above = edit->start_line;
1294 if (i > lines_above)
1295 i = lines_above;
1296 if (i) {
1297 edit->start_line -= i;
1298 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1299 edit->force |= REDRAW_PAGE;
1300 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1302 edit_update_curs_row (edit);
1306 /* returns 1 if could scroll, 0 otherwise */
1307 void edit_scroll_downward (WEdit * edit, int i)
1309 int lines_below;
1310 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1311 if (lines_below > 0) {
1312 if (i > lines_below)
1313 i = lines_below;
1314 edit->start_line += i;
1315 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1316 edit->force |= REDRAW_PAGE;
1317 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1319 edit_update_curs_row (edit);
1322 void edit_scroll_right (WEdit * edit, int i)
1324 edit->force |= REDRAW_PAGE;
1325 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1326 edit->start_col -= i;
1329 void edit_scroll_left (WEdit * edit, int i)
1331 if (edit->start_col) {
1332 edit->start_col += i;
1333 if (edit->start_col > 0)
1334 edit->start_col = 0;
1335 edit->force |= REDRAW_PAGE;
1336 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1340 /* high level cursor movement commands */
1342 static int is_in_indent (WEdit *edit)
1344 long p = edit_bol (edit, edit->curs1);
1345 while (p < edit->curs1)
1346 if (!strchr (" \t", edit_get_byte (edit, p++)))
1347 return 0;
1348 return 1;
1351 static int left_of_four_spaces (WEdit *edit);
1353 void
1354 edit_move_to_prev_col (WEdit * edit, long p)
1356 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1358 if (is_in_indent (edit) && option_fake_half_tabs) {
1359 edit_update_curs_col (edit);
1360 if (space_width)
1361 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1362 int q = edit->curs_col;
1363 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1364 p = edit_bol (edit, edit->curs1);
1365 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1366 if (!left_of_four_spaces (edit))
1367 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1373 /* move i lines */
1374 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1376 unsigned long p, l = edit->curs_line;
1378 if (i > l)
1379 i = l;
1380 if (i) {
1381 if (i > 1)
1382 edit->force |= REDRAW_PAGE;
1383 if (scroll)
1384 edit_scroll_upward (edit, i);
1386 p = edit_bol (edit, edit->curs1);
1387 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1388 edit_move_to_prev_col (edit, p);
1390 edit->search_start = edit->curs1;
1391 edit->found_len = 0;
1395 static int
1396 is_blank (WEdit *edit, long offset)
1398 long s, f;
1399 int c;
1400 s = edit_bol (edit, offset);
1401 f = edit_eol (edit, offset) - 1;
1402 while (s <= f) {
1403 c = edit_get_byte (edit, s++);
1404 if (!isspace (c))
1405 return 0;
1407 return 1;
1411 /* returns the offset of line i */
1412 static long
1413 edit_find_line (WEdit *edit, int line)
1415 int i, j = 0;
1416 int m = 2000000000;
1417 if (!edit->caches_valid) {
1418 for (i = 0; i < N_LINE_CACHES; i++)
1419 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1420 /* three offsets that we *know* are line 0 at 0 and these two: */
1421 edit->line_numbers[1] = edit->curs_line;
1422 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1423 edit->line_numbers[2] = edit->total_lines;
1424 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1425 edit->caches_valid = 1;
1427 if (line >= edit->total_lines)
1428 return edit->line_offsets[2];
1429 if (line <= 0)
1430 return 0;
1431 /* find the closest known point */
1432 for (i = 0; i < N_LINE_CACHES; i++) {
1433 int n;
1434 n = abs (edit->line_numbers[i] - line);
1435 if (n < m) {
1436 m = n;
1437 j = i;
1440 if (m == 0)
1441 return edit->line_offsets[j]; /* know the offset exactly */
1442 if (m == 1 && j >= 3)
1443 i = j; /* one line different - caller might be looping, so stay in this cache */
1444 else
1445 i = 3 + (rand () % (N_LINE_CACHES - 3));
1446 if (line > edit->line_numbers[j])
1447 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1448 else
1449 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1450 edit->line_numbers[i] = line;
1451 return edit->line_offsets[i];
1454 int line_is_blank (WEdit * edit, long line)
1456 return is_blank (edit, edit_find_line (edit, line));
1459 /* moves up until a blank line is reached, or until just
1460 before a non-blank line is reached */
1461 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1463 int i;
1464 if (edit->curs_line <= 1) {
1465 i = 0;
1466 } else {
1467 if (line_is_blank (edit, edit->curs_line)) {
1468 if (line_is_blank (edit, edit->curs_line - 1)) {
1469 for (i = edit->curs_line - 1; i; i--)
1470 if (!line_is_blank (edit, i)) {
1471 i++;
1472 break;
1474 } else {
1475 for (i = edit->curs_line - 1; i; i--)
1476 if (line_is_blank (edit, i))
1477 break;
1479 } else {
1480 for (i = edit->curs_line - 1; i; i--)
1481 if (line_is_blank (edit, i))
1482 break;
1485 edit_move_up (edit, edit->curs_line - i, scroll);
1488 /* move i lines */
1489 void edit_move_down (WEdit * edit, int i, int scroll)
1491 long p, l = edit->total_lines - edit->curs_line;
1493 if (i > l)
1494 i = l;
1495 if (i) {
1496 if (i > 1)
1497 edit->force |= REDRAW_PAGE;
1498 if (scroll)
1499 edit_scroll_downward (edit, i);
1500 p = edit_bol (edit, edit->curs1);
1501 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1502 edit_move_to_prev_col (edit, p);
1504 edit->search_start = edit->curs1;
1505 edit->found_len = 0;
1509 /* moves down until a blank line is reached, or until just
1510 before a non-blank line is reached */
1511 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1513 int i;
1514 if (edit->curs_line >= edit->total_lines - 1) {
1515 i = edit->total_lines;
1516 } else {
1517 if (line_is_blank (edit, edit->curs_line)) {
1518 if (line_is_blank (edit, edit->curs_line + 1)) {
1519 for (i = edit->curs_line + 1; i; i++)
1520 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1521 i--;
1522 break;
1524 } else {
1525 for (i = edit->curs_line + 1; i; i++)
1526 if (line_is_blank (edit, i) || i >= edit->total_lines)
1527 break;
1529 } else {
1530 for (i = edit->curs_line + 1; i; i++)
1531 if (line_is_blank (edit, i) || i >= edit->total_lines)
1532 break;
1535 edit_move_down (edit, i - edit->curs_line, scroll);
1538 static void edit_begin_page (WEdit *edit)
1540 edit_update_curs_row (edit);
1541 edit_move_up (edit, edit->curs_row, 0);
1544 static void edit_end_page (WEdit *edit)
1546 edit_update_curs_row (edit);
1547 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1551 /* goto beginning of text */
1552 static void edit_move_to_top (WEdit * edit)
1554 if (edit->curs_line) {
1555 edit_cursor_move (edit, -edit->curs1);
1556 edit_move_to_prev_col (edit, 0);
1557 edit->force |= REDRAW_PAGE;
1558 edit->search_start = 0;
1559 edit_update_curs_row(edit);
1564 /* goto end of text */
1565 static void edit_move_to_bottom (WEdit * edit)
1567 if (edit->curs_line < edit->total_lines) {
1568 edit_cursor_move (edit, edit->curs2);
1569 edit->start_display = edit->last_byte;
1570 edit->start_line = edit->total_lines;
1571 edit_update_curs_row(edit);
1572 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1573 edit->force |= REDRAW_PAGE;
1577 /* goto beginning of line */
1578 static void edit_cursor_to_bol (WEdit * edit)
1580 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1581 edit->search_start = edit->curs1;
1582 edit->prev_col = edit_get_col (edit);
1585 /* goto end of line */
1586 static void edit_cursor_to_eol (WEdit * edit)
1588 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1589 edit->search_start = edit->curs1;
1590 edit->prev_col = edit_get_col (edit);
1593 /* move cursor to line 'line' */
1594 void edit_move_to_line (WEdit * e, long line)
1596 if(line < e->curs_line)
1597 edit_move_up (e, e->curs_line - line, 0);
1598 else
1599 edit_move_down (e, line - e->curs_line, 0);
1600 edit_scroll_screen_over_cursor (e);
1603 /* scroll window so that first visible line is 'line' */
1604 void edit_move_display (WEdit * e, long line)
1606 if(line < e->start_line)
1607 edit_scroll_upward (e, e->start_line - line);
1608 else
1609 edit_scroll_downward (e, line - e->start_line);
1612 /* save markers onto undo stack */
1613 void edit_push_markers (WEdit * edit)
1615 edit_push_action (edit, MARK_1 + edit->mark1);
1616 edit_push_action (edit, MARK_2 + edit->mark2);
1619 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1621 edit->mark1 = m1;
1622 edit->mark2 = m2;
1623 edit->column1 = c1;
1624 edit->column2 = c2;
1628 /* highlight marker toggle */
1629 void edit_mark_cmd (WEdit * edit, int unmark)
1631 edit_push_markers (edit);
1632 if (unmark) {
1633 edit_set_markers (edit, 0, 0, 0, 0);
1634 edit->force |= REDRAW_PAGE;
1635 } else {
1636 if (edit->mark2 >= 0) {
1637 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1638 edit->force |= REDRAW_PAGE;
1639 } else
1640 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1644 static unsigned long
1645 my_type_of (int c)
1647 int x, r = 0;
1648 const char *p, *q;
1649 const char option_chars_move_whole_word[] =
1650 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1652 if (!c)
1653 return 0;
1654 if (c == '!') {
1655 if (*option_chars_move_whole_word == '!')
1656 return 2;
1657 return 0x80000000UL;
1659 if (isupper (c))
1660 c = 'A';
1661 else if (islower (c))
1662 c = 'a';
1663 else if (isalpha (c))
1664 c = 'a';
1665 else if (isdigit (c))
1666 c = '0';
1667 else if (isspace (c))
1668 c = ' ';
1669 q = strchr (option_chars_move_whole_word, c);
1670 if (!q)
1671 return 0xFFFFFFFFUL;
1672 do {
1673 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1674 if (*p == '!')
1675 x <<= 1;
1676 r |= x;
1677 } while ((q = strchr (q + 1, c)));
1678 return r;
1681 static void
1682 edit_left_word_move (WEdit *edit, int s)
1684 for (;;) {
1685 int c1, c2;
1686 edit_cursor_move (edit, -1);
1687 if (!edit->curs1)
1688 break;
1689 c1 = edit_get_byte (edit, edit->curs1 - 1);
1690 c2 = edit_get_byte (edit, edit->curs1);
1691 if (!(my_type_of (c1) & my_type_of (c2)))
1692 break;
1693 if (isspace (c1) && !isspace (c2))
1694 break;
1695 if (s)
1696 if (!isspace (c1) && isspace (c2))
1697 break;
1701 static void edit_left_word_move_cmd (WEdit * edit)
1703 edit_left_word_move (edit, 0);
1704 edit->force |= REDRAW_PAGE;
1707 static void
1708 edit_right_word_move (WEdit *edit, int s)
1710 for (;;) {
1711 int c1, c2;
1712 edit_cursor_move (edit, 1);
1713 if (edit->curs1 >= edit->last_byte)
1714 break;
1715 c1 = edit_get_byte (edit, edit->curs1 - 1);
1716 c2 = edit_get_byte (edit, edit->curs1);
1717 if (!(my_type_of (c1) & my_type_of (c2)))
1718 break;
1719 if (isspace (c1) && !isspace (c2))
1720 break;
1721 if (s)
1722 if (!isspace (c1) && isspace (c2))
1723 break;
1727 static void edit_right_word_move_cmd (WEdit * edit)
1729 edit_right_word_move (edit, 0);
1730 edit->force |= REDRAW_PAGE;
1734 static void edit_right_delete_word (WEdit * edit)
1736 int c1, c2;
1737 for (;;) {
1738 if (edit->curs1 >= edit->last_byte)
1739 break;
1740 c1 = edit_delete (edit);
1741 c2 = edit_get_byte (edit, edit->curs1);
1742 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1743 break;
1744 if (!(my_type_of (c1) & my_type_of (c2)))
1745 break;
1749 static void edit_left_delete_word (WEdit * edit)
1751 int c1, c2;
1752 for (;;) {
1753 if (edit->curs1 <= 0)
1754 break;
1755 c1 = edit_backspace (edit);
1756 c2 = edit_get_byte (edit, edit->curs1 - 1);
1757 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1758 break;
1759 if (!(my_type_of (c1) & my_type_of (c2)))
1760 break;
1765 the start column position is not recorded, and hence does not
1766 undo as it happed. But who would notice.
1768 static void
1769 edit_do_undo (WEdit * edit)
1771 long ac;
1772 long count = 0;
1774 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1776 while ((ac = pop_action (edit)) < KEY_PRESS) {
1777 switch ((int) ac) {
1778 case STACK_BOTTOM:
1779 goto done_undo;
1780 case CURS_RIGHT:
1781 edit_cursor_move (edit, 1);
1782 break;
1783 case CURS_LEFT:
1784 edit_cursor_move (edit, -1);
1785 break;
1786 case BACKSPACE:
1787 edit_backspace (edit);
1788 break;
1789 case DELCHAR:
1790 edit_delete (edit);
1791 break;
1792 case COLUMN_ON:
1793 column_highlighting = 1;
1794 break;
1795 case COLUMN_OFF:
1796 column_highlighting = 0;
1797 break;
1799 if (ac >= 256 && ac < 512)
1800 edit_insert_ahead (edit, ac - 256);
1801 if (ac >= 0 && ac < 256)
1802 edit_insert (edit, ac);
1804 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1805 edit->mark1 = ac - MARK_1;
1806 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1807 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1808 edit->mark2 = ac - MARK_2;
1809 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1811 if (count++)
1812 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1815 if (edit->start_display > ac - KEY_PRESS) {
1816 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1817 edit->force |= REDRAW_PAGE;
1818 } else if (edit->start_display < ac - KEY_PRESS) {
1819 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1820 edit->force |= REDRAW_PAGE;
1822 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1823 edit_update_curs_row (edit);
1825 done_undo:;
1826 edit->stack_disable = 0;
1829 static void edit_delete_to_line_end (WEdit * edit)
1831 while (edit_get_byte (edit, edit->curs1) != '\n') {
1832 if (!edit->curs2)
1833 break;
1834 edit_delete (edit);
1838 static void edit_delete_to_line_begin (WEdit * edit)
1840 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1841 if (!edit->curs1)
1842 break;
1843 edit_backspace (edit);
1847 void
1848 edit_delete_line (WEdit *edit)
1851 * Delete right part of the line.
1852 * Note that edit_get_byte() returns '\n' when byte position is
1853 * beyond EOF.
1855 while (edit_get_byte (edit, edit->curs1) != '\n') {
1856 (void) edit_delete (edit);
1860 * Delete '\n' char.
1861 * Note that edit_delete() will not corrupt anything if called while
1862 * cursor position is EOF.
1864 (void) edit_delete (edit);
1867 * Delete left part of the line.
1868 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1870 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1871 (void) edit_backspace (edit);
1875 static void insert_spaces_tab (WEdit * edit, int half)
1877 int i;
1878 edit_update_curs_col (edit);
1879 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1880 while (i > 0) {
1881 edit_insert (edit, ' ');
1882 i -= space_width;
1886 static int is_aligned_on_a_tab (WEdit * edit)
1888 edit_update_curs_col (edit);
1889 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1890 return 0; /* not alligned on a tab */
1891 return 1;
1894 static int right_of_four_spaces (WEdit *edit)
1896 int i, ch = 0;
1897 for (i = 1; i <= HALF_TAB_SIZE; i++)
1898 ch |= edit_get_byte (edit, edit->curs1 - i);
1899 if (ch == ' ')
1900 return is_aligned_on_a_tab (edit);
1901 return 0;
1904 static int left_of_four_spaces (WEdit *edit)
1906 int i, ch = 0;
1907 for (i = 0; i < HALF_TAB_SIZE; i++)
1908 ch |= edit_get_byte (edit, edit->curs1 + i);
1909 if (ch == ' ')
1910 return is_aligned_on_a_tab (edit);
1911 return 0;
1914 int edit_indent_width (WEdit * edit, long p)
1916 long q = p;
1917 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1918 q++;
1919 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1922 void edit_insert_indent (WEdit * edit, int indent)
1924 if (!option_fill_tabs_with_spaces) {
1925 while (indent >= TAB_SIZE) {
1926 edit_insert (edit, '\t');
1927 indent -= TAB_SIZE;
1930 while (indent-- > 0)
1931 edit_insert (edit, ' ');
1934 static void
1935 edit_auto_indent (WEdit * edit)
1937 long p;
1938 char c;
1939 p = edit->curs1;
1940 /* use the previous line as a template */
1941 p = edit_move_backward (edit, p, 1);
1942 /* copy the leading whitespace of the line */
1943 for (;;) { /* no range check - the line _is_ \n-terminated */
1944 c = edit_get_byte (edit, p++);
1945 if (c != ' ' && c != '\t')
1946 break;
1947 edit_insert (edit, c);
1951 static void edit_double_newline (WEdit * edit)
1953 edit_insert (edit, '\n');
1954 if (edit_get_byte (edit, edit->curs1) == '\n')
1955 return;
1956 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1957 return;
1958 edit->force |= REDRAW_PAGE;
1959 edit_insert (edit, '\n');
1962 static void edit_tab_cmd (WEdit * edit)
1964 int i;
1966 if (option_fake_half_tabs) {
1967 if (is_in_indent (edit)) {
1968 /*insert a half tab (usually four spaces) unless there is a
1969 half tab already behind, then delete it and insert a
1970 full tab. */
1971 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1972 for (i = 1; i <= HALF_TAB_SIZE; i++)
1973 edit_backspace (edit);
1974 edit_insert (edit, '\t');
1975 } else {
1976 insert_spaces_tab (edit, 1);
1978 return;
1981 if (option_fill_tabs_with_spaces) {
1982 insert_spaces_tab (edit, 0);
1983 } else {
1984 edit_insert (edit, '\t');
1986 return;
1989 static void check_and_wrap_line (WEdit * edit)
1991 int curs, c;
1992 if (!option_typewriter_wrap)
1993 return;
1994 edit_update_curs_col (edit);
1995 if (edit->curs_col < option_word_wrap_line_length)
1996 return;
1997 curs = edit->curs1;
1998 for (;;) {
1999 curs--;
2000 c = edit_get_byte (edit, curs);
2001 if (c == '\n' || curs <= 0) {
2002 edit_insert (edit, '\n');
2003 return;
2005 if (c == ' ' || c == '\t') {
2006 int current = edit->curs1;
2007 edit_cursor_move (edit, curs - edit->curs1 + 1);
2008 edit_insert (edit, '\n');
2009 edit_cursor_move (edit, current - edit->curs1 + 1);
2010 return;
2015 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2017 void edit_push_key_press (WEdit * edit)
2019 edit_push_action (edit, KEY_PRESS + edit->start_display);
2020 if (edit->mark2 == -1)
2021 edit_push_action (edit, MARK_1 + edit->mark1);
2024 /* this find the matching bracket in either direction, and sets edit->bracket */
2025 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2027 const char * const b = "{}{[][()(", *p;
2028 int i = 1, a, inc = -1, c, d, n = 0;
2029 unsigned long j = 0;
2030 long q;
2031 edit_update_curs_row (edit);
2032 c = edit_get_byte (edit, edit->curs1);
2033 p = strchr (b, c);
2034 /* no limit */
2035 if (!furthest_bracket_search)
2036 furthest_bracket_search--;
2037 /* not on a bracket at all */
2038 if (!p)
2039 return -1;
2040 /* the matching bracket */
2041 d = p[1];
2042 /* going left or right? */
2043 if (strchr ("{[(", c))
2044 inc = 1;
2045 for (q = edit->curs1 + inc;; q += inc) {
2046 /* out of buffer? */
2047 if (q >= edit->last_byte || q < 0)
2048 break;
2049 a = edit_get_byte (edit, q);
2050 /* don't want to eat CPU */
2051 if (j++ > furthest_bracket_search)
2052 break;
2053 /* out of screen? */
2054 if (in_screen) {
2055 if (q < edit->start_display)
2056 break;
2057 /* count lines if searching downward */
2058 if (inc > 0 && a == '\n')
2059 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2060 break;
2062 /* count bracket depth */
2063 i += (a == c) - (a == d);
2064 /* return if bracket depth is zero */
2065 if (!i)
2066 return q;
2068 /* no match */
2069 return -1;
2072 static long last_bracket = -1;
2074 void edit_find_bracket (WEdit * edit)
2076 edit->bracket = edit_get_bracket (edit, 1, 10000);
2077 if (last_bracket != edit->bracket)
2078 edit->force |= REDRAW_PAGE;
2079 last_bracket = edit->bracket;
2082 static void edit_goto_matching_bracket (WEdit *edit)
2084 long q;
2085 q = edit_get_bracket (edit, 0, 0);
2086 if (q < 0)
2087 return;
2088 edit->bracket = edit->curs1;
2089 edit->force |= REDRAW_PAGE;
2090 edit_cursor_move (edit, q - edit->curs1);
2094 * This executes a command as though the user initiated it through a key
2095 * press. Callback with WIDGET_KEY as a message calls this after
2096 * translating the key press. This function can be used to pass any
2097 * command to the editor. Note that the screen wouldn't update
2098 * automatically. Either of command or char_for_insertion must be
2099 * passed as -1. Commands are executed, and char_for_insertion is
2100 * inserted at the cursor.
2102 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2104 if (command == CK_Begin_Record_Macro) {
2105 edit->macro_i = 0;
2106 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2107 return;
2109 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2110 edit->force |= REDRAW_COMPLETELY;
2111 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2112 edit->macro_i = -1;
2113 return;
2115 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2116 edit->macro[edit->macro_i].command = command;
2117 edit->macro[edit->macro_i++].ch = char_for_insertion;
2119 /* record the beginning of a set of editing actions initiated by a key press */
2120 if (command != CK_Undo && command != CK_Ext_Mode)
2121 edit_push_key_press (edit);
2123 edit_execute_cmd (edit, command, char_for_insertion);
2124 if (column_highlighting)
2125 edit->force |= REDRAW_PAGE;
2128 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2131 This executes a command at a lower level than macro recording.
2132 It also does not push a key_press onto the undo stack. This means
2133 that if it is called many times, a single undo command will undo
2134 all of them. It also does not check for the Undo command.
2136 void
2137 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2139 edit->force |= REDRAW_LINE;
2141 /* The next key press will unhighlight the found string, so update
2142 * the whole page */
2143 if (edit->found_len || column_highlighting)
2144 edit->force |= REDRAW_PAGE;
2146 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2147 column_highlighting = 0;
2148 if (!edit->highlight
2149 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2150 edit_mark_cmd (edit, 1); /* clear */
2151 edit_mark_cmd (edit, 0); /* marking on */
2153 edit->highlight = 1;
2154 } else { /* any other command */
2155 if (edit->highlight)
2156 edit_mark_cmd (edit, 0); /* clear */
2157 edit->highlight = 0;
2160 /* first check for undo */
2161 if (command == CK_Undo) {
2162 edit_do_undo (edit);
2163 edit->found_len = 0;
2164 edit->prev_col = edit_get_col (edit);
2165 edit->search_start = edit->curs1;
2166 return;
2169 /* An ordinary key press */
2170 if (char_for_insertion >= 0) {
2171 if (edit->overwrite) {
2172 if (edit_get_byte (edit, edit->curs1) != '\n')
2173 edit_delete (edit);
2175 edit_insert (edit, char_for_insertion);
2176 if (option_auto_para_formatting) {
2177 format_paragraph (edit, 0);
2178 edit->force |= REDRAW_PAGE;
2179 } else
2180 check_and_wrap_line (edit);
2181 edit->found_len = 0;
2182 edit->prev_col = edit_get_col (edit);
2183 edit->search_start = edit->curs1;
2184 edit_find_bracket (edit);
2185 return;
2187 switch (command) {
2188 case CK_Begin_Page:
2189 case CK_End_Page:
2190 case CK_Begin_Page_Highlight:
2191 case CK_End_Page_Highlight:
2192 case CK_Word_Left:
2193 case CK_Word_Right:
2194 case CK_Up:
2195 case CK_Down:
2196 case CK_Word_Left_Highlight:
2197 case CK_Word_Right_Highlight:
2198 case CK_Up_Highlight:
2199 case CK_Down_Highlight:
2200 if (edit->mark2 == -1)
2201 break; /*marking is following the cursor: may need to highlight a whole line */
2202 case CK_Left:
2203 case CK_Right:
2204 case CK_Left_Highlight:
2205 case CK_Right_Highlight:
2206 edit->force |= REDRAW_CHAR_ONLY;
2209 /* basic cursor key commands */
2210 switch (command) {
2211 case CK_BackSpace:
2212 if (option_backspace_through_tabs && is_in_indent (edit)) {
2213 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2214 && edit->curs1 > 0)
2215 edit_backspace (edit);
2216 break;
2217 } else {
2218 if (option_fake_half_tabs) {
2219 int i;
2220 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2221 for (i = 0; i < HALF_TAB_SIZE; i++)
2222 edit_backspace (edit);
2223 break;
2227 edit_backspace (edit);
2228 break;
2229 case CK_Delete:
2230 if (option_fake_half_tabs) {
2231 int i;
2232 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2233 for (i = 1; i <= HALF_TAB_SIZE; i++)
2234 edit_delete (edit);
2235 break;
2238 edit_delete (edit);
2239 break;
2240 case CK_Delete_Word_Left:
2241 edit_left_delete_word (edit);
2242 break;
2243 case CK_Delete_Word_Right:
2244 edit_right_delete_word (edit);
2245 break;
2246 case CK_Delete_Line:
2247 edit_delete_line (edit);
2248 break;
2249 case CK_Delete_To_Line_End:
2250 edit_delete_to_line_end (edit);
2251 break;
2252 case CK_Delete_To_Line_Begin:
2253 edit_delete_to_line_begin (edit);
2254 break;
2255 case CK_Enter:
2256 if (option_auto_para_formatting) {
2257 edit_double_newline (edit);
2258 if (option_return_does_auto_indent)
2259 edit_auto_indent (edit);
2260 format_paragraph (edit, 0);
2261 } else {
2262 edit_insert (edit, '\n');
2263 if (option_return_does_auto_indent) {
2264 edit_auto_indent (edit);
2267 break;
2268 case CK_Return:
2269 edit_insert (edit, '\n');
2270 break;
2272 case CK_Page_Up:
2273 case CK_Page_Up_Highlight:
2274 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2275 break;
2276 case CK_Page_Down:
2277 case CK_Page_Down_Highlight:
2278 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2279 break;
2280 case CK_Left:
2281 case CK_Left_Highlight:
2282 if (option_fake_half_tabs) {
2283 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2284 edit_cursor_move (edit, -HALF_TAB_SIZE);
2285 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2286 break;
2289 edit_cursor_move (edit, -1);
2290 break;
2291 case CK_Right:
2292 case CK_Right_Highlight:
2293 if (option_fake_half_tabs) {
2294 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2295 edit_cursor_move (edit, HALF_TAB_SIZE);
2296 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2297 break;
2300 edit_cursor_move (edit, 1);
2301 break;
2302 case CK_Begin_Page:
2303 case CK_Begin_Page_Highlight:
2304 edit_begin_page (edit);
2305 break;
2306 case CK_End_Page:
2307 case CK_End_Page_Highlight:
2308 edit_end_page (edit);
2309 break;
2310 case CK_Word_Left:
2311 case CK_Word_Left_Highlight:
2312 edit_left_word_move_cmd (edit);
2313 break;
2314 case CK_Word_Right:
2315 case CK_Word_Right_Highlight:
2316 edit_right_word_move_cmd (edit);
2317 break;
2318 case CK_Up:
2319 case CK_Up_Highlight:
2320 edit_move_up (edit, 1, 0);
2321 break;
2322 case CK_Down:
2323 case CK_Down_Highlight:
2324 edit_move_down (edit, 1, 0);
2325 break;
2326 case CK_Paragraph_Up:
2327 case CK_Paragraph_Up_Highlight:
2328 edit_move_up_paragraph (edit, 0);
2329 break;
2330 case CK_Paragraph_Down:
2331 case CK_Paragraph_Down_Highlight:
2332 edit_move_down_paragraph (edit, 0);
2333 break;
2334 case CK_Scroll_Up:
2335 case CK_Scroll_Up_Highlight:
2336 edit_move_up (edit, 1, 1);
2337 break;
2338 case CK_Scroll_Down:
2339 case CK_Scroll_Down_Highlight:
2340 edit_move_down (edit, 1, 1);
2341 break;
2342 case CK_Home:
2343 case CK_Home_Highlight:
2344 edit_cursor_to_bol (edit);
2345 break;
2346 case CK_End:
2347 case CK_End_Highlight:
2348 edit_cursor_to_eol (edit);
2349 break;
2351 case CK_Tab:
2352 edit_tab_cmd (edit);
2353 if (option_auto_para_formatting) {
2354 format_paragraph (edit, 0);
2355 edit->force |= REDRAW_PAGE;
2356 } else
2357 check_and_wrap_line (edit);
2358 break;
2360 case CK_Toggle_Insert:
2361 edit->overwrite = (edit->overwrite == 0);
2362 break;
2364 case CK_Mark:
2365 if (edit->mark2 >= 0) {
2366 if (column_highlighting)
2367 edit_push_action (edit, COLUMN_ON);
2368 column_highlighting = 0;
2370 edit_mark_cmd (edit, 0);
2371 break;
2372 case CK_Column_Mark:
2373 if (!column_highlighting)
2374 edit_push_action (edit, COLUMN_OFF);
2375 column_highlighting = 1;
2376 edit_mark_cmd (edit, 0);
2377 break;
2378 case CK_Unmark:
2379 if (column_highlighting)
2380 edit_push_action (edit, COLUMN_ON);
2381 column_highlighting = 0;
2382 edit_mark_cmd (edit, 1);
2383 break;
2385 case CK_Toggle_Bookmark:
2386 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2387 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2388 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2389 else
2390 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2391 break;
2392 case CK_Flush_Bookmarks:
2393 book_mark_flush (edit, BOOK_MARK_COLOR);
2394 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2395 edit->force |= REDRAW_PAGE;
2396 break;
2397 case CK_Next_Bookmark:
2398 if (edit->book_mark) {
2399 struct _book_mark *p;
2400 p = (struct _book_mark *) book_mark_find (edit,
2401 edit->curs_line);
2402 if (p->next) {
2403 p = p->next;
2404 if (p->line >= edit->start_line + edit->num_widget_lines
2405 || p->line < edit->start_line)
2406 edit_move_display (edit,
2407 p->line -
2408 edit->num_widget_lines / 2);
2409 edit_move_to_line (edit, p->line);
2412 break;
2413 case CK_Prev_Bookmark:
2414 if (edit->book_mark) {
2415 struct _book_mark *p;
2416 p = (struct _book_mark *) book_mark_find (edit,
2417 edit->curs_line);
2418 while (p->line == edit->curs_line)
2419 if (p->prev)
2420 p = p->prev;
2421 if (p->line >= 0) {
2422 if (p->line >= edit->start_line + edit->num_widget_lines
2423 || p->line < edit->start_line)
2424 edit_move_display (edit,
2425 p->line -
2426 edit->num_widget_lines / 2);
2427 edit_move_to_line (edit, p->line);
2430 break;
2432 case CK_Beginning_Of_Text:
2433 case CK_Beginning_Of_Text_Highlight:
2434 edit_move_to_top (edit);
2435 break;
2436 case CK_End_Of_Text:
2437 case CK_End_Of_Text_Highlight:
2438 edit_move_to_bottom (edit);
2439 break;
2441 case CK_Copy:
2442 edit_block_copy_cmd (edit);
2443 break;
2444 case CK_Remove:
2445 edit_block_delete_cmd (edit);
2446 break;
2447 case CK_Move:
2448 edit_block_move_cmd (edit);
2449 break;
2451 case CK_XStore:
2452 edit_copy_to_X_buf_cmd (edit);
2453 break;
2454 case CK_XCut:
2455 edit_cut_to_X_buf_cmd (edit);
2456 break;
2457 case CK_XPaste:
2458 edit_paste_from_X_buf_cmd (edit);
2459 break;
2460 case CK_Selection_History:
2461 edit_paste_from_history (edit);
2462 break;
2464 case CK_Save_As:
2465 edit_save_as_cmd (edit);
2466 break;
2467 case CK_Save:
2468 edit_save_confirm_cmd (edit);
2469 break;
2470 case CK_Load:
2471 edit_load_cmd (edit);
2472 break;
2473 case CK_Save_Block:
2474 edit_save_block_cmd (edit);
2475 break;
2476 case CK_Insert_File:
2477 edit_insert_file_cmd (edit);
2478 break;
2480 case CK_Toggle_Syntax:
2481 if ((option_syntax_highlighting ^= 1) == 1)
2482 edit_load_syntax (edit, NULL, option_syntax_type);
2483 edit->force |= REDRAW_PAGE;
2484 break;
2486 case CK_Find:
2487 edit_search_cmd (edit, 0);
2488 break;
2489 case CK_Find_Again:
2490 edit_search_cmd (edit, 1);
2491 break;
2492 case CK_Replace:
2493 edit_replace_cmd (edit, 0);
2494 break;
2495 case CK_Replace_Again:
2496 edit_replace_cmd (edit, 1);
2497 break;
2498 case CK_Complete_Word:
2499 edit_complete_word_cmd (edit);
2500 break;
2502 case CK_Exit:
2503 dlg_stop (edit->widget.parent);
2504 break;
2505 case CK_New:
2506 edit_new_cmd (edit);
2507 break;
2509 case CK_Help:
2510 edit_help_cmd (edit);
2511 break;
2513 case CK_Refresh:
2514 edit_refresh_cmd (edit);
2515 break;
2517 case CK_Date:{
2518 char s[1024];
2519 /* fool gcc to prevent a Y2K warning */
2520 char time_format[] = "_c";
2521 time_format[0] = '%';
2523 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
2524 edit_print_string (edit, s);
2525 edit->force |= REDRAW_PAGE;
2526 break;
2528 case CK_Goto:
2529 edit_goto_cmd (edit);
2530 break;
2531 case CK_Paragraph_Format:
2532 format_paragraph (edit, 1);
2533 edit->force |= REDRAW_PAGE;
2534 break;
2535 case CK_Delete_Macro:
2536 edit_delete_macro_cmd (edit);
2537 break;
2538 case CK_Match_Bracket:
2539 edit_goto_matching_bracket (edit);
2540 break;
2541 case CK_User_Menu:
2542 user_menu (edit);
2543 break;
2544 case CK_Sort:
2545 edit_sort_cmd (edit);
2546 break;
2547 case CK_ExtCmd:
2548 edit_ext_cmd (edit);
2549 break;
2550 case CK_Mail:
2551 edit_mail_dialog (edit);
2552 break;
2553 case CK_Shell:
2554 view_other_cmd ();
2555 break;
2556 case CK_Select_Codepage:
2557 edit_select_codepage_cmd (edit);
2558 break;
2559 case CK_Insert_Literal:
2560 edit_insert_literal_cmd (edit);
2561 break;
2562 case CK_Execute_Macro:
2563 edit_execute_macro_cmd (edit);
2564 break;
2565 case CK_Begin_End_Macro:
2566 edit_begin_end_macro_cmd (edit);
2567 break;
2568 case CK_Ext_Mode:
2569 edit->extmod = 1;
2570 break;
2571 default:
2572 break;
2575 /* CK_Pipe_Block */
2576 if ((command / 1000) == 1) /* a shell command */
2577 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2578 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2579 struct macro m[MAX_MACRO_LENGTH];
2580 int nm;
2581 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
2582 edit_execute_macro (edit, m, nm);
2585 /* keys which must set the col position, and the search vars */
2586 switch (command) {
2587 case CK_Find:
2588 case CK_Find_Again:
2589 case CK_Replace:
2590 case CK_Replace_Again:
2591 case CK_Complete_Word:
2592 edit->prev_col = edit_get_col (edit);
2593 break;
2594 case CK_Up:
2595 case CK_Up_Highlight:
2596 case CK_Down:
2597 case CK_Down_Highlight:
2598 case CK_Page_Up:
2599 case CK_Page_Up_Highlight:
2600 case CK_Page_Down:
2601 case CK_Page_Down_Highlight:
2602 case CK_Beginning_Of_Text:
2603 case CK_Beginning_Of_Text_Highlight:
2604 case CK_End_Of_Text:
2605 case CK_End_Of_Text_Highlight:
2606 case CK_Paragraph_Up:
2607 case CK_Paragraph_Up_Highlight:
2608 case CK_Paragraph_Down:
2609 case CK_Paragraph_Down_Highlight:
2610 case CK_Scroll_Up:
2611 case CK_Scroll_Up_Highlight:
2612 case CK_Scroll_Down:
2613 case CK_Scroll_Down_Highlight:
2614 edit->search_start = edit->curs1;
2615 edit->found_len = 0;
2616 break;
2617 default:
2618 edit->found_len = 0;
2619 edit->prev_col = edit_get_col (edit);
2620 edit->search_start = edit->curs1;
2622 edit_find_bracket (edit);
2624 if (option_auto_para_formatting) {
2625 switch (command) {
2626 case CK_BackSpace:
2627 case CK_Delete:
2628 case CK_Delete_Word_Left:
2629 case CK_Delete_Word_Right:
2630 case CK_Delete_To_Line_End:
2631 case CK_Delete_To_Line_Begin:
2632 format_paragraph (edit, 0);
2633 edit->force |= REDRAW_PAGE;
2639 static void
2640 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2642 int i = 0;
2644 if (edit->macro_depth++ > 256) {
2645 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2646 edit->macro_depth--;
2647 return;
2649 edit->force |= REDRAW_PAGE;
2650 for (; i < n; i++) {
2651 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2653 edit_update_screen (edit);
2654 edit->macro_depth--;
2657 /* User edit menu, like user menu (F2) but only in editor. */
2658 static void
2659 user_menu (WEdit * edit)
2661 FILE *fd;
2662 int nomark;
2663 struct stat status;
2664 long start_mark, end_mark;
2665 char *block_file = mhl_str_dir_plus_file (home_dir, BLOCK_FILE);
2666 int rc = 0;
2668 nomark = eval_marks (edit, &start_mark, &end_mark);
2669 if (!nomark) /* remember marked or not */
2670 edit_save_block (edit, block_file, start_mark, end_mark);
2672 /* run shell scripts from menu */
2673 user_menu_cmd (edit);
2675 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2676 /* no block messages */
2677 goto cleanup;
2680 if (!nomark) {
2681 /* i.e. we have marked block */
2682 rc = edit_block_delete_cmd (edit);
2685 if (!rc) {
2686 edit_insert_file (edit, block_file);
2689 /* truncate block file */
2690 if ((fd = fopen (block_file, "w"))) {
2691 fclose (fd);
2694 edit_refresh_cmd (edit);
2695 edit->force |= REDRAW_COMPLETELY;
2697 cleanup:
2698 mhl_mem_free (block_file);