Revert second bunch of mhl patches (see 9b9cab58749217101ab16504a77efb301812cfbf)
[midnight-commander.git] / edit / edit.c
blobe853a96e792ce1148518b309b0f68943a69e7ef7
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 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
189 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
190 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
193 /* Return index of the filter or -1 is there is no appropriate filter */
194 static int edit_find_filter (const char *filename)
196 size_t i, l, e;
197 if (!filename)
198 return -1;
199 l = strlen (filename);
200 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
201 e = strlen (all_filters[i].extension);
202 if (l > e)
203 if (!strcmp (all_filters[i].extension, filename + l - e))
204 return i;
206 return -1;
209 static char *
210 edit_get_filter (const char *filename)
212 int i, l;
213 char *p, *quoted_name;
214 i = edit_find_filter (filename);
215 if (i < 0)
216 return 0;
217 quoted_name = name_quote (filename, 0);
218 l = strlen (quoted_name);
219 p = g_malloc (strlen (all_filters[i].read) + l + 2);
220 sprintf (p, all_filters[i].read, quoted_name);
221 mhl_mem_free (quoted_name);
222 return p;
225 char *
226 edit_get_write_filter (const char *write_name, const char *filename)
228 int i, l;
229 char *p, *writename;
230 i = edit_find_filter (filename);
231 if (i < 0)
232 return 0;
233 writename = name_quote (write_name, 0);
234 l = strlen (writename);
235 p = g_malloc (strlen (all_filters[i].write) + l + 2);
236 sprintf (p, all_filters[i].write, writename);
237 mhl_mem_free (writename);
238 return p;
241 static long
242 edit_insert_stream (WEdit * edit, FILE * f)
244 int c;
245 long i = 0;
246 while ((c = fgetc (f)) >= 0) {
247 edit_insert (edit, c);
248 i++;
250 return i;
253 long edit_write_stream (WEdit * edit, FILE * f)
255 long i;
256 for (i = 0; i < edit->last_byte; i++)
257 if (fputc (edit_get_byte (edit, i), f) < 0)
258 break;
259 return i;
262 #define TEMP_BUF_LEN 1024
264 /* inserts a file at the cursor, returns 1 on success */
266 edit_insert_file (WEdit *edit, const char *filename)
268 char *p;
269 if ((p = edit_get_filter (filename))) {
270 FILE *f;
271 long current = edit->curs1;
272 f = (FILE *) popen (p, "r");
273 if (f) {
274 edit_insert_stream (edit, f);
275 edit_cursor_move (edit, current - edit->curs1);
276 if (pclose (f) > 0) {
277 GString *errmsg = g_string_new (NULL);
278 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
279 edit_error_dialog (_("Error"), errmsg->str);
280 g_string_free (errmsg, TRUE);
281 g_free (p);
282 return 0;
284 } else {
285 GString *errmsg = g_string_new (NULL);
286 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
287 edit_error_dialog (_("Error"), errmsg->str);
288 g_string_free (errmsg, TRUE);
289 g_free (p);
290 return 0;
292 g_free (p);
293 } else {
294 int i, file, blocklen;
295 long current = edit->curs1;
296 unsigned char *buf;
297 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
298 return 0;
299 buf = g_malloc (TEMP_BUF_LEN);
300 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
301 for (i = 0; i < blocklen; i++)
302 edit_insert (edit, buf[i]);
304 edit_cursor_move (edit, current - edit->curs1);
305 mhl_mem_free (buf);
306 mc_close (file);
307 if (blocklen)
308 return 0;
310 return 1;
313 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
314 static int
315 check_file_access (WEdit *edit, const char *filename, struct stat *st)
317 int file;
318 char errmsg[8192];
319 errmsg[0] = 0;
321 /* Try opening an existing file */
322 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
324 if (file < 0) {
326 * Try creating the file. O_EXCL prevents following broken links
327 * and opening existing files.
329 file =
330 mc_open (filename,
331 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
332 0666);
333 if (file < 0) {
334 snprintf (errmsg, sizeof(errmsg), _(" Cannot open %s for reading "), filename);
335 goto cleanup;
336 } else {
337 /* New file, delete it if it's not modified or saved */
338 edit->delete_file = 1;
342 /* Check what we have opened */
343 if (mc_fstat (file, st) < 0) {
344 snprintf (errmsg, sizeof(errmsg), _(" 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 snprintf (errmsg, sizeof(errmsg), _(" %s is not a regular file "), filename);
351 goto cleanup;
355 * Don't delete non-empty files.
356 * O_EXCL should prevent it, but let's be on the safe side.
358 if (st->st_size > 0) {
359 edit->delete_file = 0;
362 if (st->st_size >= SIZE_LIMIT) {
363 snprintf (errmsg, sizeof(errmsg), _(" File %s is too large "), filename);
364 goto cleanup;
367 cleanup:
368 (void) mc_close (file);
369 if (errmsg[0]) {
370 edit_error_dialog (_("Error"), errmsg);
371 return 1;
373 return 0;
377 * Open the file and load it into the buffers, either directly or using
378 * a filter. Return 0 on success, 1 on error.
380 * Fast loading (edit_load_file_fast) is used when the file size is
381 * known. In this case the data is read into the buffers by blocks.
382 * If the file size is not known, the data is loaded byte by byte in
383 * edit_insert_file.
385 static int
386 edit_load_file (WEdit *edit)
388 int fast_load = 1;
390 /* Cannot do fast load if a filter is used */
391 if (edit_find_filter (edit->filename) >= 0)
392 fast_load = 0;
395 * VFS may report file size incorrectly, and slow load is not a big
396 * deal considering overhead in VFS.
398 if (!vfs_file_is_local (edit->filename))
399 fast_load = 0;
402 * FIXME: line end translation should disable fast loading as well
403 * Consider doing fseek() to the end and ftell() for the real size.
406 if (*edit->filename) {
407 /* If we are dealing with a real file, check that it exists */
408 if (check_file_access (edit, edit->filename, &edit->stat1))
409 return 1;
410 } else {
411 /* nothing to load */
412 fast_load = 0;
415 edit_init_buffers (edit);
417 if (fast_load) {
418 edit->last_byte = edit->stat1.st_size;
419 edit_load_file_fast (edit, edit->filename);
420 /* If fast load was used, the number of lines wasn't calculated */
421 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
422 } else {
423 edit->last_byte = 0;
424 if (*edit->filename) {
425 edit->stack_disable = 1;
426 if (!edit_insert_file (edit, edit->filename)) {
427 edit_clean (edit);
428 return 1;
430 edit->stack_disable = 0;
433 return 0;
436 /* Restore saved cursor position in the file */
437 static void
438 edit_load_position (WEdit *edit)
440 char *filename;
441 long line, column;
443 if (!edit->filename || !*edit->filename)
444 return;
446 filename = vfs_canon (edit->filename);
447 load_file_position (filename, &line, &column);
448 mhl_mem_free (filename);
450 edit_move_to_line (edit, line - 1);
451 edit->prev_col = column;
452 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
453 edit_move_display (edit, line - (edit->num_widget_lines / 2));
456 /* Save cursor position in the file */
457 static void
458 edit_save_position (WEdit *edit)
460 char *filename;
462 if (!edit->filename || !*edit->filename)
463 return;
465 filename = vfs_canon (edit->filename);
466 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
467 mhl_mem_free (filename);
470 /* Clean the WEdit stricture except the widget part */
471 static inline void
472 edit_purge_widget (WEdit *edit)
474 int len = sizeof (WEdit) - sizeof (Widget);
475 char *start = (char *) edit + sizeof (Widget);
476 memset (start, 0, len);
477 edit->macro_i = -1; /* not recording a macro */
480 #define space_width 1
483 * Fill in the edit structure. Return NULL on failure. Pass edit as
484 * NULL to allocate a new structure.
486 * If line is 0, try to restore saved position. Otherwise put the
487 * cursor on that line and show it in the middle of the screen.
489 WEdit *
490 edit_init (WEdit *edit, int lines, int columns, const char *filename,
491 long line)
493 int to_free = 0;
494 option_auto_syntax = 1; /* Resetting to auto on every invokation */
496 if (!edit) {
497 #ifdef ENABLE_NLS
499 * Expand option_whole_chars_search by national letters using
500 * current locale
503 static char option_whole_chars_search_buf[256];
505 if (option_whole_chars_search_buf != option_whole_chars_search) {
506 size_t i;
507 size_t len = strlen (option_whole_chars_search);
509 strcpy (option_whole_chars_search_buf,
510 option_whole_chars_search);
512 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
513 if (islower (i) && !strchr (option_whole_chars_search, i)) {
514 option_whole_chars_search_buf[len++] = i;
518 option_whole_chars_search_buf[len] = 0;
519 option_whole_chars_search = option_whole_chars_search_buf;
521 #endif /* ENABLE_NLS */
522 edit = g_malloc0 (sizeof (WEdit));
523 to_free = 1;
525 edit_purge_widget (edit);
526 edit->num_widget_lines = lines;
527 edit->num_widget_columns = columns;
528 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
529 edit->stat1.st_uid = getuid ();
530 edit->stat1.st_gid = getgid ();
531 edit->stat1.st_mtime = 0;
532 edit->bracket = -1;
533 edit->force |= REDRAW_PAGE;
534 edit_set_filename (edit, filename);
535 edit->stack_size = START_STACK_SIZE;
536 edit->stack_size_mask = START_STACK_SIZE - 1;
537 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
538 if (edit_load_file (edit)) {
539 /* edit_load_file already gives an error message */
540 if (to_free)
541 mhl_mem_free (edit);
542 return 0;
544 edit->loading_done = 1;
545 edit->modified = 0;
546 edit->locked = 0;
547 edit_load_syntax (edit, 0, 0);
549 int color;
550 edit_get_syntax_color (edit, -1, &color);
553 /* load saved cursor position */
554 if ((line == 0) && option_save_position) {
555 edit_load_position (edit);
556 } else {
557 if (line <= 0)
558 line = 1;
559 edit_move_display (edit, line - 1);
560 edit_move_to_line (edit, line - 1);
563 edit_load_user_map(edit);
565 return edit;
568 /* Clear the edit struct, freeing everything in it. Return 1 on success */
570 edit_clean (WEdit *edit)
572 int j = 0;
574 if (!edit)
575 return 0;
577 /* a stale lock, remove it */
578 if (edit->locked)
579 edit->locked = edit_unlock_file (edit->filename);
581 /* save cursor position */
582 if (option_save_position)
583 edit_save_position (edit);
585 /* File specified on the mcedit command line and never saved */
586 if (edit->delete_file)
587 unlink (edit->filename);
589 edit_free_syntax_rules (edit);
590 book_mark_flush (edit, -1);
591 for (; j <= MAXBUFF; j++) {
592 mhl_mem_free (edit->buffers1[j]);
593 mhl_mem_free (edit->buffers2[j]);
596 mhl_mem_free (edit->undo_stack);
597 mhl_mem_free (edit->filename);
598 mhl_mem_free (edit->dir);
600 edit_purge_widget (edit);
602 /* Free temporary strings used in catstrs() */
603 freestrs ();
605 return 1;
609 /* returns 1 on success */
610 int edit_renew (WEdit * edit)
612 int lines = edit->num_widget_lines;
613 int columns = edit->num_widget_columns;
614 int retval = 1;
616 edit_clean (edit);
617 if (!edit_init (edit, lines, columns, "", 0))
618 retval = 0;
619 return retval;
623 * Load a new file into the editor. If it fails, preserve the old file.
624 * To do it, allocate a new widget, initialize it and, if the new file
625 * was loaded, copy the data to the old widget.
626 * Return 1 on success, 0 on failure.
629 edit_reload (WEdit *edit, const char *filename)
631 WEdit *e;
632 int lines = edit->num_widget_lines;
633 int columns = edit->num_widget_columns;
635 e = g_malloc0 (sizeof (WEdit));
636 e->widget = edit->widget;
637 if (!edit_init (e, lines, columns, filename, 0)) {
638 mhl_mem_free (e);
639 return 0;
641 edit_clean (edit);
642 memcpy (edit, e, sizeof (WEdit));
643 mhl_mem_free (e);
644 return 1;
649 Recording stack for undo:
650 The following is an implementation of a compressed stack. Identical
651 pushes are recorded by a negative prefix indicating the number of times the
652 same char was pushed. This saves space for repeated curs-left or curs-right
653 delete etc.
657 pushed: stored:
661 b -3
663 c --> -4
669 If the stack long int is 0-255 it represents a normal insert (from a backspace),
670 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
671 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
672 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
673 position.
675 The only way the cursor moves or the buffer is changed is through the routines:
676 insert, backspace, insert_ahead, delete, and cursor_move.
677 These record the reverse undo movements onto the stack each time they are
678 called.
680 Each key press results in a set of actions (insert; delete ...). So each time
681 a key is pressed the current position of start_display is pushed as
682 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
683 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
684 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
688 void edit_push_action (WEdit * edit, long c,...)
690 unsigned long sp = edit->stack_pointer;
691 unsigned long spm1;
692 long *t;
694 /* first enlarge the stack if necessary */
695 if (sp > edit->stack_size - 10) { /* say */
696 if (option_max_undo < 256)
697 option_max_undo = 256;
698 if (edit->stack_size < (unsigned long) option_max_undo) {
699 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
700 if (t) {
701 edit->undo_stack = t;
702 edit->stack_size <<= 1;
703 edit->stack_size_mask = edit->stack_size - 1;
707 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
708 if (edit->stack_disable)
709 return;
711 #ifdef FAST_MOVE_CURSOR
712 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
713 va_list ap;
714 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
715 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
716 va_start (ap, c);
717 c = -(va_arg (ap, int));
718 va_end (ap);
719 } else
720 #endif /* ! FAST_MOVE_CURSOR */
721 if (edit->stack_bottom != sp
722 && spm1 != edit->stack_bottom
723 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
724 int d;
725 if (edit->undo_stack[spm1] < 0) {
726 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
727 if (d == c) {
728 if (edit->undo_stack[spm1] > -1000000000) {
729 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
730 edit->undo_stack[spm1]--;
731 return;
734 /* #define NO_STACK_CURSMOVE_ANIHILATION */
735 #ifndef NO_STACK_CURSMOVE_ANIHILATION
736 else if ((c == CURS_LEFT && d == CURS_RIGHT)
737 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
738 if (edit->undo_stack[spm1] == -2)
739 edit->stack_pointer = spm1;
740 else
741 edit->undo_stack[spm1]++;
742 return;
744 #endif
745 } else {
746 d = edit->undo_stack[spm1];
747 if (d == c) {
748 if (c >= KEY_PRESS)
749 return; /* --> no need to push multiple do-nothings */
750 edit->undo_stack[sp] = -2;
751 goto check_bottom;
753 #ifndef NO_STACK_CURSMOVE_ANIHILATION
754 else if ((c == CURS_LEFT && d == CURS_RIGHT)
755 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
756 edit->stack_pointer = spm1;
757 return;
759 #endif
762 edit->undo_stack[sp] = c;
763 check_bottom:
765 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
767 /* if the sp wraps round and catches the stack_bottom then erase
768 * the first set of actions on the stack to make space - by moving
769 * stack_bottom forward one "key press" */
770 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
771 if ((unsigned long) c == edit->stack_bottom ||
772 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
773 do {
774 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
775 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
777 /*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: */
778 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
779 edit->stack_bottom = edit->stack_pointer = 0;
783 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
784 then the file should be as it was when he loaded up. Then set edit->modified to 0.
786 static long
787 pop_action (WEdit * edit)
789 long c;
790 unsigned long sp = edit->stack_pointer;
791 if (sp == edit->stack_bottom) {
792 return STACK_BOTTOM;
794 sp = (sp - 1) & edit->stack_size_mask;
795 if ((c = edit->undo_stack[sp]) >= 0) {
796 /* edit->undo_stack[sp] = '@'; */
797 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
798 return c;
800 if (sp == edit->stack_bottom) {
801 return STACK_BOTTOM;
803 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
804 if (edit->undo_stack[sp] == -2) {
805 /* edit->undo_stack[sp] = '@'; */
806 edit->stack_pointer = sp;
807 } else
808 edit->undo_stack[sp]++;
810 return c;
813 /* is called whenever a modification is made by one of the four routines below */
814 static inline void edit_modification (WEdit * edit)
816 edit->caches_valid = 0;
817 edit->screen_modified = 1;
819 /* raise lock when file modified */
820 if (!edit->modified && !edit->delete_file)
821 edit->locked = edit_lock_file (edit->filename);
822 edit->modified = 1;
826 Basic low level single character buffer alterations and movements at the cursor.
827 Returns char passed over, inserted or removed.
830 void
831 edit_insert (WEdit *edit, int c)
833 /* check if file has grown to large */
834 if (edit->last_byte >= SIZE_LIMIT)
835 return;
837 /* first we must update the position of the display window */
838 if (edit->curs1 < edit->start_display) {
839 edit->start_display++;
840 if (c == '\n')
841 edit->start_line++;
844 /* Mark file as modified, unless the file hasn't been fully loaded */
845 if (edit->loading_done) {
846 edit_modification (edit);
849 /* now we must update some info on the file and check if a redraw is required */
850 if (c == '\n') {
851 if (edit->book_mark)
852 book_mark_inc (edit, edit->curs_line);
853 edit->curs_line++;
854 edit->total_lines++;
855 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
858 /* save the reverse command onto the undo stack */
859 edit_push_action (edit, BACKSPACE);
861 /* update markers */
862 edit->mark1 += (edit->mark1 > edit->curs1);
863 edit->mark2 += (edit->mark2 > edit->curs1);
864 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
866 /* add a new buffer if we've reached the end of the last one */
867 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
868 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
869 g_malloc (EDIT_BUF_SIZE);
871 /* perform the insertion */
872 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
873 curs1 & M_EDIT_BUF_SIZE]
874 = (unsigned char) c;
876 /* update file length */
877 edit->last_byte++;
879 /* update cursor position */
880 edit->curs1++;
884 /* same as edit_insert and move left */
885 void edit_insert_ahead (WEdit * edit, int c)
887 if (edit->last_byte >= SIZE_LIMIT)
888 return;
889 if (edit->curs1 < edit->start_display) {
890 edit->start_display++;
891 if (c == '\n')
892 edit->start_line++;
894 edit_modification (edit);
895 if (c == '\n') {
896 if (edit->book_mark)
897 book_mark_inc (edit, edit->curs_line);
898 edit->total_lines++;
899 edit->force |= REDRAW_AFTER_CURSOR;
901 edit_push_action (edit, DELCHAR);
903 edit->mark1 += (edit->mark1 >= edit->curs1);
904 edit->mark2 += (edit->mark2 >= edit->curs1);
905 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
907 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
908 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
909 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
911 edit->last_byte++;
912 edit->curs2++;
916 int edit_delete (WEdit * edit)
918 int p;
919 if (!edit->curs2)
920 return 0;
922 edit->mark1 -= (edit->mark1 > edit->curs1);
923 edit->mark2 -= (edit->mark2 > edit->curs1);
924 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
926 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
928 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
929 mhl_mem_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
930 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
932 edit->last_byte--;
933 edit->curs2--;
935 edit_modification (edit);
936 if (p == '\n') {
937 if (edit->book_mark)
938 book_mark_dec (edit, edit->curs_line);
939 edit->total_lines--;
940 edit->force |= REDRAW_AFTER_CURSOR;
942 edit_push_action (edit, p + 256);
943 if (edit->curs1 < edit->start_display) {
944 edit->start_display--;
945 if (p == '\n')
946 edit->start_line--;
949 return p;
953 static int
954 edit_backspace (WEdit * edit)
956 int p;
957 if (!edit->curs1)
958 return 0;
960 edit->mark1 -= (edit->mark1 >= edit->curs1);
961 edit->mark2 -= (edit->mark2 >= edit->curs1);
962 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
964 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
965 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
966 mhl_mem_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
967 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
969 edit->last_byte--;
970 edit->curs1--;
972 edit_modification (edit);
973 if (p == '\n') {
974 if (edit->book_mark)
975 book_mark_dec (edit, edit->curs_line);
976 edit->curs_line--;
977 edit->total_lines--;
978 edit->force |= REDRAW_AFTER_CURSOR;
980 edit_push_action (edit, p);
982 if (edit->curs1 < edit->start_display) {
983 edit->start_display--;
984 if (p == '\n')
985 edit->start_line--;
988 return p;
991 #ifdef FAST_MOVE_CURSOR
993 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
995 unsigned long next;
996 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
997 edit->curs_line--;
998 next -= (unsigned long) dest;
999 n -= next;
1000 src += next;
1001 dest += next;
1006 edit_move_backward_lots (WEdit *edit, long increment)
1008 int r, s, t;
1009 unsigned char *p;
1011 if (increment > edit->curs1)
1012 increment = edit->curs1;
1013 if (increment <= 0)
1014 return -1;
1015 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1017 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1018 if (r > increment)
1019 r = increment;
1020 s = edit->curs1 & M_EDIT_BUF_SIZE;
1022 p = 0;
1023 if (s > r) {
1024 memqcpy (edit,
1025 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1026 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1028 } else {
1029 if (s) {
1030 memqcpy (edit,
1031 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1032 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1033 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1034 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1036 memqcpy (edit,
1037 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1038 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1039 EDIT_BUF_SIZE - (r - s), r - s);
1041 increment -= r;
1042 edit->curs1 -= r;
1043 edit->curs2 += r;
1044 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1045 if (p)
1046 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1047 else
1048 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1049 g_malloc (EDIT_BUF_SIZE);
1050 } else {
1051 mhl_mem_free (p);
1054 s = edit->curs1 & M_EDIT_BUF_SIZE;
1055 while (increment) {
1056 p = 0;
1057 r = EDIT_BUF_SIZE;
1058 if (r > increment)
1059 r = increment;
1060 t = s;
1061 if (r < t)
1062 t = r;
1063 memqcpy (edit,
1064 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1065 EDIT_BUF_SIZE - t,
1066 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1068 if (r >= s) {
1069 if (t) {
1070 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1071 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1073 memqcpy (edit,
1074 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1075 EDIT_BUF_SIZE - r,
1076 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1077 EDIT_BUF_SIZE - (r - s), r - s);
1079 increment -= r;
1080 edit->curs1 -= r;
1081 edit->curs2 += r;
1082 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1083 if (p)
1084 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1085 else
1086 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1087 g_malloc (EDIT_BUF_SIZE);
1088 } else {
1089 mhl_mem_free (p);
1092 return edit_get_byte (edit, edit->curs1);
1095 #endif /* ! FAST_MOVE_CURSOR */
1097 /* moves the cursor right or left: increment positive or negative respectively */
1098 void edit_cursor_move (WEdit * edit, long increment)
1100 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1101 int c;
1103 #ifdef FAST_MOVE_CURSOR
1104 if (increment < -256) {
1105 edit->force |= REDRAW_PAGE;
1106 edit_move_backward_lots (edit, -increment);
1107 return;
1109 #endif /* ! FAST_MOVE_CURSOR */
1111 if (increment < 0) {
1112 for (; increment < 0; increment++) {
1113 if (!edit->curs1)
1114 return;
1116 edit_push_action (edit, CURS_RIGHT);
1118 c = edit_get_byte (edit, edit->curs1 - 1);
1119 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1120 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1121 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1122 edit->curs2++;
1123 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1124 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1125 mhl_mem_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1126 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1128 edit->curs1--;
1129 if (c == '\n') {
1130 edit->curs_line--;
1131 edit->force |= REDRAW_LINE_BELOW;
1135 } else if (increment > 0) {
1136 for (; increment > 0; increment--) {
1137 if (!edit->curs2)
1138 return;
1140 edit_push_action (edit, CURS_LEFT);
1142 c = edit_get_byte (edit, edit->curs1);
1143 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1144 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1145 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1146 edit->curs1++;
1147 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1148 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1149 mhl_mem_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1150 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1152 edit->curs2--;
1153 if (c == '\n') {
1154 edit->curs_line++;
1155 edit->force |= REDRAW_LINE_ABOVE;
1161 /* These functions return positions relative to lines */
1163 /* returns index of last char on line + 1 */
1164 long edit_eol (WEdit * edit, long current)
1166 if (current < edit->last_byte) {
1167 for (;; current++)
1168 if (edit_get_byte (edit, current) == '\n')
1169 break;
1170 } else
1171 return edit->last_byte;
1172 return current;
1175 /* returns index of first char on line */
1176 long edit_bol (WEdit * edit, long current)
1178 if (current > 0) {
1179 for (;; current--)
1180 if (edit_get_byte (edit, current - 1) == '\n')
1181 break;
1182 } else
1183 return 0;
1184 return current;
1188 int edit_count_lines (WEdit * edit, long current, int upto)
1190 int lines = 0;
1191 if (upto > edit->last_byte)
1192 upto = edit->last_byte;
1193 if (current < 0)
1194 current = 0;
1195 while (current < upto)
1196 if (edit_get_byte (edit, current++) == '\n')
1197 lines++;
1198 return lines;
1202 /* If lines is zero this returns the count of lines from current to upto. */
1203 /* If upto is zero returns index of lines forward current. */
1204 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1206 if (upto) {
1207 return edit_count_lines (edit, current, upto);
1208 } else {
1209 int next;
1210 if (lines < 0)
1211 lines = 0;
1212 while (lines--) {
1213 next = edit_eol (edit, current) + 1;
1214 if (next > edit->last_byte)
1215 break;
1216 else
1217 current = next;
1219 return current;
1224 /* Returns offset of 'lines' lines up from current */
1225 long edit_move_backward (WEdit * edit, long current, int lines)
1227 if (lines < 0)
1228 lines = 0;
1229 current = edit_bol (edit, current);
1230 while((lines--) && current != 0)
1231 current = edit_bol (edit, current - 1);
1232 return current;
1235 /* If cols is zero this returns the count of columns from current to upto. */
1236 /* If upto is zero returns index of cols across from current. */
1237 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1239 long p, q;
1240 int col = 0;
1242 if (upto) {
1243 q = upto;
1244 cols = -10;
1245 } else
1246 q = edit->last_byte + 2;
1248 for (col = 0, p = current; p < q; p++) {
1249 int c;
1250 if (cols != -10) {
1251 if (col == cols)
1252 return p;
1253 if (col > cols)
1254 return p - 1;
1256 c = edit_get_byte (edit, p);
1257 if (c == '\t')
1258 col += TAB_SIZE - col % TAB_SIZE;
1259 else if (c == '\n') {
1260 if (upto)
1261 return col;
1262 else
1263 return p;
1264 } else if (c < 32 || c == 127)
1265 col += 2; /* Caret notation for control characters */
1266 else
1267 col++;
1269 return col;
1272 /* returns the current column position of the cursor */
1273 int edit_get_col (WEdit * edit)
1275 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1279 /* Scrolling functions */
1281 void edit_update_curs_row (WEdit * edit)
1283 edit->curs_row = edit->curs_line - edit->start_line;
1286 void edit_update_curs_col (WEdit * edit)
1288 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1291 /*moves the display start position up by i lines */
1292 void edit_scroll_upward (WEdit * edit, unsigned long i)
1294 unsigned long lines_above = edit->start_line;
1295 if (i > lines_above)
1296 i = lines_above;
1297 if (i) {
1298 edit->start_line -= i;
1299 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1300 edit->force |= REDRAW_PAGE;
1301 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1303 edit_update_curs_row (edit);
1307 /* returns 1 if could scroll, 0 otherwise */
1308 void edit_scroll_downward (WEdit * edit, int i)
1310 int lines_below;
1311 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1312 if (lines_below > 0) {
1313 if (i > lines_below)
1314 i = lines_below;
1315 edit->start_line += i;
1316 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1317 edit->force |= REDRAW_PAGE;
1318 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1320 edit_update_curs_row (edit);
1323 void edit_scroll_right (WEdit * edit, int i)
1325 edit->force |= REDRAW_PAGE;
1326 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1327 edit->start_col -= i;
1330 void edit_scroll_left (WEdit * edit, int i)
1332 if (edit->start_col) {
1333 edit->start_col += i;
1334 if (edit->start_col > 0)
1335 edit->start_col = 0;
1336 edit->force |= REDRAW_PAGE;
1337 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1341 /* high level cursor movement commands */
1343 static int is_in_indent (WEdit *edit)
1345 long p = edit_bol (edit, edit->curs1);
1346 while (p < edit->curs1)
1347 if (!strchr (" \t", edit_get_byte (edit, p++)))
1348 return 0;
1349 return 1;
1352 static int left_of_four_spaces (WEdit *edit);
1354 void
1355 edit_move_to_prev_col (WEdit * edit, long p)
1357 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1359 if (is_in_indent (edit) && option_fake_half_tabs) {
1360 edit_update_curs_col (edit);
1361 if (space_width)
1362 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1363 int q = edit->curs_col;
1364 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1365 p = edit_bol (edit, edit->curs1);
1366 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1367 if (!left_of_four_spaces (edit))
1368 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1374 /* move i lines */
1375 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1377 unsigned long p, l = edit->curs_line;
1379 if (i > l)
1380 i = l;
1381 if (i) {
1382 if (i > 1)
1383 edit->force |= REDRAW_PAGE;
1384 if (scroll)
1385 edit_scroll_upward (edit, i);
1387 p = edit_bol (edit, edit->curs1);
1388 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1389 edit_move_to_prev_col (edit, p);
1391 edit->search_start = edit->curs1;
1392 edit->found_len = 0;
1396 static int
1397 is_blank (WEdit *edit, long offset)
1399 long s, f;
1400 int c;
1401 s = edit_bol (edit, offset);
1402 f = edit_eol (edit, offset) - 1;
1403 while (s <= f) {
1404 c = edit_get_byte (edit, s++);
1405 if (!isspace (c))
1406 return 0;
1408 return 1;
1412 /* returns the offset of line i */
1413 static long
1414 edit_find_line (WEdit *edit, int line)
1416 int i, j = 0;
1417 int m = 2000000000;
1418 if (!edit->caches_valid) {
1419 for (i = 0; i < N_LINE_CACHES; i++)
1420 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1421 /* three offsets that we *know* are line 0 at 0 and these two: */
1422 edit->line_numbers[1] = edit->curs_line;
1423 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1424 edit->line_numbers[2] = edit->total_lines;
1425 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1426 edit->caches_valid = 1;
1428 if (line >= edit->total_lines)
1429 return edit->line_offsets[2];
1430 if (line <= 0)
1431 return 0;
1432 /* find the closest known point */
1433 for (i = 0; i < N_LINE_CACHES; i++) {
1434 int n;
1435 n = abs (edit->line_numbers[i] - line);
1436 if (n < m) {
1437 m = n;
1438 j = i;
1441 if (m == 0)
1442 return edit->line_offsets[j]; /* know the offset exactly */
1443 if (m == 1 && j >= 3)
1444 i = j; /* one line different - caller might be looping, so stay in this cache */
1445 else
1446 i = 3 + (rand () % (N_LINE_CACHES - 3));
1447 if (line > edit->line_numbers[j])
1448 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1449 else
1450 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1451 edit->line_numbers[i] = line;
1452 return edit->line_offsets[i];
1455 int line_is_blank (WEdit * edit, long line)
1457 return is_blank (edit, edit_find_line (edit, line));
1460 /* moves up until a blank line is reached, or until just
1461 before a non-blank line is reached */
1462 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1464 int i;
1465 if (edit->curs_line <= 1) {
1466 i = 0;
1467 } else {
1468 if (line_is_blank (edit, edit->curs_line)) {
1469 if (line_is_blank (edit, edit->curs_line - 1)) {
1470 for (i = edit->curs_line - 1; i; i--)
1471 if (!line_is_blank (edit, i)) {
1472 i++;
1473 break;
1475 } else {
1476 for (i = edit->curs_line - 1; i; i--)
1477 if (line_is_blank (edit, i))
1478 break;
1480 } else {
1481 for (i = edit->curs_line - 1; i; i--)
1482 if (line_is_blank (edit, i))
1483 break;
1486 edit_move_up (edit, edit->curs_line - i, scroll);
1489 /* move i lines */
1490 void edit_move_down (WEdit * edit, int i, int scroll)
1492 long p, l = edit->total_lines - edit->curs_line;
1494 if (i > l)
1495 i = l;
1496 if (i) {
1497 if (i > 1)
1498 edit->force |= REDRAW_PAGE;
1499 if (scroll)
1500 edit_scroll_downward (edit, i);
1501 p = edit_bol (edit, edit->curs1);
1502 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1503 edit_move_to_prev_col (edit, p);
1505 edit->search_start = edit->curs1;
1506 edit->found_len = 0;
1510 /* moves down until a blank line is reached, or until just
1511 before a non-blank line is reached */
1512 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1514 int i;
1515 if (edit->curs_line >= edit->total_lines - 1) {
1516 i = edit->total_lines;
1517 } else {
1518 if (line_is_blank (edit, edit->curs_line)) {
1519 if (line_is_blank (edit, edit->curs_line + 1)) {
1520 for (i = edit->curs_line + 1; i; i++)
1521 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1522 i--;
1523 break;
1525 } else {
1526 for (i = edit->curs_line + 1; i; i++)
1527 if (line_is_blank (edit, i) || i >= edit->total_lines)
1528 break;
1530 } else {
1531 for (i = edit->curs_line + 1; i; i++)
1532 if (line_is_blank (edit, i) || i >= edit->total_lines)
1533 break;
1536 edit_move_down (edit, i - edit->curs_line, scroll);
1539 static void edit_begin_page (WEdit *edit)
1541 edit_update_curs_row (edit);
1542 edit_move_up (edit, edit->curs_row, 0);
1545 static void edit_end_page (WEdit *edit)
1547 edit_update_curs_row (edit);
1548 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1552 /* goto beginning of text */
1553 static void edit_move_to_top (WEdit * edit)
1555 if (edit->curs_line) {
1556 edit_cursor_move (edit, -edit->curs1);
1557 edit_move_to_prev_col (edit, 0);
1558 edit->force |= REDRAW_PAGE;
1559 edit->search_start = 0;
1560 edit_update_curs_row(edit);
1565 /* goto end of text */
1566 static void edit_move_to_bottom (WEdit * edit)
1568 if (edit->curs_line < edit->total_lines) {
1569 edit_cursor_move (edit, edit->curs2);
1570 edit->start_display = edit->last_byte;
1571 edit->start_line = edit->total_lines;
1572 edit_update_curs_row(edit);
1573 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1574 edit->force |= REDRAW_PAGE;
1578 /* goto beginning of line */
1579 static void edit_cursor_to_bol (WEdit * edit)
1581 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1582 edit->search_start = edit->curs1;
1583 edit->prev_col = edit_get_col (edit);
1586 /* goto end of line */
1587 static void edit_cursor_to_eol (WEdit * edit)
1589 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1590 edit->search_start = edit->curs1;
1591 edit->prev_col = edit_get_col (edit);
1594 /* move cursor to line 'line' */
1595 void edit_move_to_line (WEdit * e, long line)
1597 if(line < e->curs_line)
1598 edit_move_up (e, e->curs_line - line, 0);
1599 else
1600 edit_move_down (e, line - e->curs_line, 0);
1601 edit_scroll_screen_over_cursor (e);
1604 /* scroll window so that first visible line is 'line' */
1605 void edit_move_display (WEdit * e, long line)
1607 if(line < e->start_line)
1608 edit_scroll_upward (e, e->start_line - line);
1609 else
1610 edit_scroll_downward (e, line - e->start_line);
1613 /* save markers onto undo stack */
1614 void edit_push_markers (WEdit * edit)
1616 edit_push_action (edit, MARK_1 + edit->mark1);
1617 edit_push_action (edit, MARK_2 + edit->mark2);
1620 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1622 edit->mark1 = m1;
1623 edit->mark2 = m2;
1624 edit->column1 = c1;
1625 edit->column2 = c2;
1629 /* highlight marker toggle */
1630 void edit_mark_cmd (WEdit * edit, int unmark)
1632 edit_push_markers (edit);
1633 if (unmark) {
1634 edit_set_markers (edit, 0, 0, 0, 0);
1635 edit->force |= REDRAW_PAGE;
1636 } else {
1637 if (edit->mark2 >= 0) {
1638 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1639 edit->force |= REDRAW_PAGE;
1640 } else
1641 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1645 static unsigned long
1646 my_type_of (int c)
1648 int x, r = 0;
1649 const char *p, *q;
1650 const char option_chars_move_whole_word[] =
1651 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1653 if (!c)
1654 return 0;
1655 if (c == '!') {
1656 if (*option_chars_move_whole_word == '!')
1657 return 2;
1658 return 0x80000000UL;
1660 if (isupper (c))
1661 c = 'A';
1662 else if (islower (c))
1663 c = 'a';
1664 else if (isalpha (c))
1665 c = 'a';
1666 else if (isdigit (c))
1667 c = '0';
1668 else if (isspace (c))
1669 c = ' ';
1670 q = strchr (option_chars_move_whole_word, c);
1671 if (!q)
1672 return 0xFFFFFFFFUL;
1673 do {
1674 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1675 if (*p == '!')
1676 x <<= 1;
1677 r |= x;
1678 } while ((q = strchr (q + 1, c)));
1679 return r;
1682 static void
1683 edit_left_word_move (WEdit *edit, int s)
1685 for (;;) {
1686 int c1, c2;
1687 edit_cursor_move (edit, -1);
1688 if (!edit->curs1)
1689 break;
1690 c1 = edit_get_byte (edit, edit->curs1 - 1);
1691 c2 = edit_get_byte (edit, edit->curs1);
1692 if (!(my_type_of (c1) & my_type_of (c2)))
1693 break;
1694 if (isspace (c1) && !isspace (c2))
1695 break;
1696 if (s)
1697 if (!isspace (c1) && isspace (c2))
1698 break;
1702 static void edit_left_word_move_cmd (WEdit * edit)
1704 edit_left_word_move (edit, 0);
1705 edit->force |= REDRAW_PAGE;
1708 static void
1709 edit_right_word_move (WEdit *edit, int s)
1711 for (;;) {
1712 int c1, c2;
1713 edit_cursor_move (edit, 1);
1714 if (edit->curs1 >= edit->last_byte)
1715 break;
1716 c1 = edit_get_byte (edit, edit->curs1 - 1);
1717 c2 = edit_get_byte (edit, edit->curs1);
1718 if (!(my_type_of (c1) & my_type_of (c2)))
1719 break;
1720 if (isspace (c1) && !isspace (c2))
1721 break;
1722 if (s)
1723 if (!isspace (c1) && isspace (c2))
1724 break;
1728 static void edit_right_word_move_cmd (WEdit * edit)
1730 edit_right_word_move (edit, 0);
1731 edit->force |= REDRAW_PAGE;
1735 static void edit_right_delete_word (WEdit * edit)
1737 int c1, c2;
1738 for (;;) {
1739 if (edit->curs1 >= edit->last_byte)
1740 break;
1741 c1 = edit_delete (edit);
1742 c2 = edit_get_byte (edit, edit->curs1);
1743 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1744 break;
1745 if (!(my_type_of (c1) & my_type_of (c2)))
1746 break;
1750 static void edit_left_delete_word (WEdit * edit)
1752 int c1, c2;
1753 for (;;) {
1754 if (edit->curs1 <= 0)
1755 break;
1756 c1 = edit_backspace (edit);
1757 c2 = edit_get_byte (edit, edit->curs1 - 1);
1758 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1759 break;
1760 if (!(my_type_of (c1) & my_type_of (c2)))
1761 break;
1766 the start column position is not recorded, and hence does not
1767 undo as it happed. But who would notice.
1769 static void
1770 edit_do_undo (WEdit * edit)
1772 long ac;
1773 long count = 0;
1775 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
1777 while ((ac = pop_action (edit)) < KEY_PRESS) {
1778 switch ((int) ac) {
1779 case STACK_BOTTOM:
1780 goto done_undo;
1781 case CURS_RIGHT:
1782 edit_cursor_move (edit, 1);
1783 break;
1784 case CURS_LEFT:
1785 edit_cursor_move (edit, -1);
1786 break;
1787 case BACKSPACE:
1788 edit_backspace (edit);
1789 break;
1790 case DELCHAR:
1791 edit_delete (edit);
1792 break;
1793 case COLUMN_ON:
1794 column_highlighting = 1;
1795 break;
1796 case COLUMN_OFF:
1797 column_highlighting = 0;
1798 break;
1800 if (ac >= 256 && ac < 512)
1801 edit_insert_ahead (edit, ac - 256);
1802 if (ac >= 0 && ac < 256)
1803 edit_insert (edit, ac);
1805 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1806 edit->mark1 = ac - MARK_1;
1807 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1808 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1809 edit->mark2 = ac - MARK_2;
1810 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1812 if (count++)
1813 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1816 if (edit->start_display > ac - KEY_PRESS) {
1817 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1818 edit->force |= REDRAW_PAGE;
1819 } else if (edit->start_display < ac - KEY_PRESS) {
1820 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1821 edit->force |= REDRAW_PAGE;
1823 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1824 edit_update_curs_row (edit);
1826 done_undo:;
1827 edit->stack_disable = 0;
1830 static void edit_delete_to_line_end (WEdit * edit)
1832 while (edit_get_byte (edit, edit->curs1) != '\n') {
1833 if (!edit->curs2)
1834 break;
1835 edit_delete (edit);
1839 static void edit_delete_to_line_begin (WEdit * edit)
1841 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1842 if (!edit->curs1)
1843 break;
1844 edit_backspace (edit);
1848 void
1849 edit_delete_line (WEdit *edit)
1852 * Delete right part of the line.
1853 * Note that edit_get_byte() returns '\n' when byte position is
1854 * beyond EOF.
1856 while (edit_get_byte (edit, edit->curs1) != '\n') {
1857 (void) edit_delete (edit);
1861 * Delete '\n' char.
1862 * Note that edit_delete() will not corrupt anything if called while
1863 * cursor position is EOF.
1865 (void) edit_delete (edit);
1868 * Delete left part of the line.
1869 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1871 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1872 (void) edit_backspace (edit);
1876 static void insert_spaces_tab (WEdit * edit, int half)
1878 int i;
1879 edit_update_curs_col (edit);
1880 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1881 while (i > 0) {
1882 edit_insert (edit, ' ');
1883 i -= space_width;
1887 static int is_aligned_on_a_tab (WEdit * edit)
1889 edit_update_curs_col (edit);
1890 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1891 return 0; /* not alligned on a tab */
1892 return 1;
1895 static int right_of_four_spaces (WEdit *edit)
1897 int i, ch = 0;
1898 for (i = 1; i <= HALF_TAB_SIZE; i++)
1899 ch |= edit_get_byte (edit, edit->curs1 - i);
1900 if (ch == ' ')
1901 return is_aligned_on_a_tab (edit);
1902 return 0;
1905 static int left_of_four_spaces (WEdit *edit)
1907 int i, ch = 0;
1908 for (i = 0; i < HALF_TAB_SIZE; i++)
1909 ch |= edit_get_byte (edit, edit->curs1 + i);
1910 if (ch == ' ')
1911 return is_aligned_on_a_tab (edit);
1912 return 0;
1915 int edit_indent_width (WEdit * edit, long p)
1917 long q = p;
1918 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1919 q++;
1920 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1923 void edit_insert_indent (WEdit * edit, int indent)
1925 if (!option_fill_tabs_with_spaces) {
1926 while (indent >= TAB_SIZE) {
1927 edit_insert (edit, '\t');
1928 indent -= TAB_SIZE;
1931 while (indent-- > 0)
1932 edit_insert (edit, ' ');
1935 static void
1936 edit_auto_indent (WEdit * edit)
1938 long p;
1939 char c;
1940 p = edit->curs1;
1941 /* use the previous line as a template */
1942 p = edit_move_backward (edit, p, 1);
1943 /* copy the leading whitespace of the line */
1944 for (;;) { /* no range check - the line _is_ \n-terminated */
1945 c = edit_get_byte (edit, p++);
1946 if (c != ' ' && c != '\t')
1947 break;
1948 edit_insert (edit, c);
1952 static void edit_double_newline (WEdit * edit)
1954 edit_insert (edit, '\n');
1955 if (edit_get_byte (edit, edit->curs1) == '\n')
1956 return;
1957 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1958 return;
1959 edit->force |= REDRAW_PAGE;
1960 edit_insert (edit, '\n');
1963 static void edit_tab_cmd (WEdit * edit)
1965 int i;
1967 if (option_fake_half_tabs) {
1968 if (is_in_indent (edit)) {
1969 /*insert a half tab (usually four spaces) unless there is a
1970 half tab already behind, then delete it and insert a
1971 full tab. */
1972 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1973 for (i = 1; i <= HALF_TAB_SIZE; i++)
1974 edit_backspace (edit);
1975 edit_insert (edit, '\t');
1976 } else {
1977 insert_spaces_tab (edit, 1);
1979 return;
1982 if (option_fill_tabs_with_spaces) {
1983 insert_spaces_tab (edit, 0);
1984 } else {
1985 edit_insert (edit, '\t');
1987 return;
1990 static void check_and_wrap_line (WEdit * edit)
1992 int curs, c;
1993 if (!option_typewriter_wrap)
1994 return;
1995 edit_update_curs_col (edit);
1996 if (edit->curs_col < option_word_wrap_line_length)
1997 return;
1998 curs = edit->curs1;
1999 for (;;) {
2000 curs--;
2001 c = edit_get_byte (edit, curs);
2002 if (c == '\n' || curs <= 0) {
2003 edit_insert (edit, '\n');
2004 return;
2006 if (c == ' ' || c == '\t') {
2007 int current = edit->curs1;
2008 edit_cursor_move (edit, curs - edit->curs1 + 1);
2009 edit_insert (edit, '\n');
2010 edit_cursor_move (edit, current - edit->curs1 + 1);
2011 return;
2016 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2018 void edit_push_key_press (WEdit * edit)
2020 edit_push_action (edit, KEY_PRESS + edit->start_display);
2021 if (edit->mark2 == -1)
2022 edit_push_action (edit, MARK_1 + edit->mark1);
2025 /* this find the matching bracket in either direction, and sets edit->bracket */
2026 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2028 const char * const b = "{}{[][()(", *p;
2029 int i = 1, a, inc = -1, c, d, n = 0;
2030 unsigned long j = 0;
2031 long q;
2032 edit_update_curs_row (edit);
2033 c = edit_get_byte (edit, edit->curs1);
2034 p = strchr (b, c);
2035 /* no limit */
2036 if (!furthest_bracket_search)
2037 furthest_bracket_search--;
2038 /* not on a bracket at all */
2039 if (!p)
2040 return -1;
2041 /* the matching bracket */
2042 d = p[1];
2043 /* going left or right? */
2044 if (strchr ("{[(", c))
2045 inc = 1;
2046 for (q = edit->curs1 + inc;; q += inc) {
2047 /* out of buffer? */
2048 if (q >= edit->last_byte || q < 0)
2049 break;
2050 a = edit_get_byte (edit, q);
2051 /* don't want to eat CPU */
2052 if (j++ > furthest_bracket_search)
2053 break;
2054 /* out of screen? */
2055 if (in_screen) {
2056 if (q < edit->start_display)
2057 break;
2058 /* count lines if searching downward */
2059 if (inc > 0 && a == '\n')
2060 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2061 break;
2063 /* count bracket depth */
2064 i += (a == c) - (a == d);
2065 /* return if bracket depth is zero */
2066 if (!i)
2067 return q;
2069 /* no match */
2070 return -1;
2073 static long last_bracket = -1;
2075 void edit_find_bracket (WEdit * edit)
2077 edit->bracket = edit_get_bracket (edit, 1, 10000);
2078 if (last_bracket != edit->bracket)
2079 edit->force |= REDRAW_PAGE;
2080 last_bracket = edit->bracket;
2083 static void edit_goto_matching_bracket (WEdit *edit)
2085 long q;
2086 q = edit_get_bracket (edit, 0, 0);
2087 if (q < 0)
2088 return;
2089 edit->bracket = edit->curs1;
2090 edit->force |= REDRAW_PAGE;
2091 edit_cursor_move (edit, q - edit->curs1);
2095 * This executes a command as though the user initiated it through a key
2096 * press. Callback with WIDGET_KEY as a message calls this after
2097 * translating the key press. This function can be used to pass any
2098 * command to the editor. Note that the screen wouldn't update
2099 * automatically. Either of command or char_for_insertion must be
2100 * passed as -1. Commands are executed, and char_for_insertion is
2101 * inserted at the cursor.
2103 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2105 if (command == CK_Begin_Record_Macro) {
2106 edit->macro_i = 0;
2107 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2108 return;
2110 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2111 edit->force |= REDRAW_COMPLETELY;
2112 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2113 edit->macro_i = -1;
2114 return;
2116 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2117 edit->macro[edit->macro_i].command = command;
2118 edit->macro[edit->macro_i++].ch = char_for_insertion;
2120 /* record the beginning of a set of editing actions initiated by a key press */
2121 if (command != CK_Undo && command != CK_Ext_Mode)
2122 edit_push_key_press (edit);
2124 edit_execute_cmd (edit, command, char_for_insertion);
2125 if (column_highlighting)
2126 edit->force |= REDRAW_PAGE;
2129 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2132 This executes a command at a lower level than macro recording.
2133 It also does not push a key_press onto the undo stack. This means
2134 that if it is called many times, a single undo command will undo
2135 all of them. It also does not check for the Undo command.
2137 void
2138 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2140 edit->force |= REDRAW_LINE;
2142 /* The next key press will unhighlight the found string, so update
2143 * the whole page */
2144 if (edit->found_len || column_highlighting)
2145 edit->force |= REDRAW_PAGE;
2147 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2148 column_highlighting = 0;
2149 if (!edit->highlight
2150 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2151 edit_mark_cmd (edit, 1); /* clear */
2152 edit_mark_cmd (edit, 0); /* marking on */
2154 edit->highlight = 1;
2155 } else { /* any other command */
2156 if (edit->highlight)
2157 edit_mark_cmd (edit, 0); /* clear */
2158 edit->highlight = 0;
2161 /* first check for undo */
2162 if (command == CK_Undo) {
2163 edit_do_undo (edit);
2164 edit->found_len = 0;
2165 edit->prev_col = edit_get_col (edit);
2166 edit->search_start = edit->curs1;
2167 return;
2170 /* An ordinary key press */
2171 if (char_for_insertion >= 0) {
2172 if (edit->overwrite) {
2173 if (edit_get_byte (edit, edit->curs1) != '\n')
2174 edit_delete (edit);
2176 edit_insert (edit, char_for_insertion);
2177 if (option_auto_para_formatting) {
2178 format_paragraph (edit, 0);
2179 edit->force |= REDRAW_PAGE;
2180 } else
2181 check_and_wrap_line (edit);
2182 edit->found_len = 0;
2183 edit->prev_col = edit_get_col (edit);
2184 edit->search_start = edit->curs1;
2185 edit_find_bracket (edit);
2186 return;
2188 switch (command) {
2189 case CK_Begin_Page:
2190 case CK_End_Page:
2191 case CK_Begin_Page_Highlight:
2192 case CK_End_Page_Highlight:
2193 case CK_Word_Left:
2194 case CK_Word_Right:
2195 case CK_Up:
2196 case CK_Down:
2197 case CK_Word_Left_Highlight:
2198 case CK_Word_Right_Highlight:
2199 case CK_Up_Highlight:
2200 case CK_Down_Highlight:
2201 if (edit->mark2 == -1)
2202 break; /*marking is following the cursor: may need to highlight a whole line */
2203 case CK_Left:
2204 case CK_Right:
2205 case CK_Left_Highlight:
2206 case CK_Right_Highlight:
2207 edit->force |= REDRAW_CHAR_ONLY;
2210 /* basic cursor key commands */
2211 switch (command) {
2212 case CK_BackSpace:
2213 if (option_backspace_through_tabs && is_in_indent (edit)) {
2214 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2215 && edit->curs1 > 0)
2216 edit_backspace (edit);
2217 break;
2218 } else {
2219 if (option_fake_half_tabs) {
2220 int i;
2221 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2222 for (i = 0; i < HALF_TAB_SIZE; i++)
2223 edit_backspace (edit);
2224 break;
2228 edit_backspace (edit);
2229 break;
2230 case CK_Delete:
2231 if (option_fake_half_tabs) {
2232 int i;
2233 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2234 for (i = 1; i <= HALF_TAB_SIZE; i++)
2235 edit_delete (edit);
2236 break;
2239 edit_delete (edit);
2240 break;
2241 case CK_Delete_Word_Left:
2242 edit_left_delete_word (edit);
2243 break;
2244 case CK_Delete_Word_Right:
2245 edit_right_delete_word (edit);
2246 break;
2247 case CK_Delete_Line:
2248 edit_delete_line (edit);
2249 break;
2250 case CK_Delete_To_Line_End:
2251 edit_delete_to_line_end (edit);
2252 break;
2253 case CK_Delete_To_Line_Begin:
2254 edit_delete_to_line_begin (edit);
2255 break;
2256 case CK_Enter:
2257 if (option_auto_para_formatting) {
2258 edit_double_newline (edit);
2259 if (option_return_does_auto_indent)
2260 edit_auto_indent (edit);
2261 format_paragraph (edit, 0);
2262 } else {
2263 edit_insert (edit, '\n');
2264 if (option_return_does_auto_indent) {
2265 edit_auto_indent (edit);
2268 break;
2269 case CK_Return:
2270 edit_insert (edit, '\n');
2271 break;
2273 case CK_Page_Up:
2274 case CK_Page_Up_Highlight:
2275 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2276 break;
2277 case CK_Page_Down:
2278 case CK_Page_Down_Highlight:
2279 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2280 break;
2281 case CK_Left:
2282 case CK_Left_Highlight:
2283 if (option_fake_half_tabs) {
2284 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2285 edit_cursor_move (edit, -HALF_TAB_SIZE);
2286 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2287 break;
2290 edit_cursor_move (edit, -1);
2291 break;
2292 case CK_Right:
2293 case CK_Right_Highlight:
2294 if (option_fake_half_tabs) {
2295 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2296 edit_cursor_move (edit, HALF_TAB_SIZE);
2297 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2298 break;
2301 edit_cursor_move (edit, 1);
2302 break;
2303 case CK_Begin_Page:
2304 case CK_Begin_Page_Highlight:
2305 edit_begin_page (edit);
2306 break;
2307 case CK_End_Page:
2308 case CK_End_Page_Highlight:
2309 edit_end_page (edit);
2310 break;
2311 case CK_Word_Left:
2312 case CK_Word_Left_Highlight:
2313 edit_left_word_move_cmd (edit);
2314 break;
2315 case CK_Word_Right:
2316 case CK_Word_Right_Highlight:
2317 edit_right_word_move_cmd (edit);
2318 break;
2319 case CK_Up:
2320 case CK_Up_Highlight:
2321 edit_move_up (edit, 1, 0);
2322 break;
2323 case CK_Down:
2324 case CK_Down_Highlight:
2325 edit_move_down (edit, 1, 0);
2326 break;
2327 case CK_Paragraph_Up:
2328 case CK_Paragraph_Up_Highlight:
2329 edit_move_up_paragraph (edit, 0);
2330 break;
2331 case CK_Paragraph_Down:
2332 case CK_Paragraph_Down_Highlight:
2333 edit_move_down_paragraph (edit, 0);
2334 break;
2335 case CK_Scroll_Up:
2336 case CK_Scroll_Up_Highlight:
2337 edit_move_up (edit, 1, 1);
2338 break;
2339 case CK_Scroll_Down:
2340 case CK_Scroll_Down_Highlight:
2341 edit_move_down (edit, 1, 1);
2342 break;
2343 case CK_Home:
2344 case CK_Home_Highlight:
2345 edit_cursor_to_bol (edit);
2346 break;
2347 case CK_End:
2348 case CK_End_Highlight:
2349 edit_cursor_to_eol (edit);
2350 break;
2352 case CK_Tab:
2353 edit_tab_cmd (edit);
2354 if (option_auto_para_formatting) {
2355 format_paragraph (edit, 0);
2356 edit->force |= REDRAW_PAGE;
2357 } else
2358 check_and_wrap_line (edit);
2359 break;
2361 case CK_Toggle_Insert:
2362 edit->overwrite = (edit->overwrite == 0);
2363 break;
2365 case CK_Mark:
2366 if (edit->mark2 >= 0) {
2367 if (column_highlighting)
2368 edit_push_action (edit, COLUMN_ON);
2369 column_highlighting = 0;
2371 edit_mark_cmd (edit, 0);
2372 break;
2373 case CK_Column_Mark:
2374 if (!column_highlighting)
2375 edit_push_action (edit, COLUMN_OFF);
2376 column_highlighting = 1;
2377 edit_mark_cmd (edit, 0);
2378 break;
2379 case CK_Unmark:
2380 if (column_highlighting)
2381 edit_push_action (edit, COLUMN_ON);
2382 column_highlighting = 0;
2383 edit_mark_cmd (edit, 1);
2384 break;
2386 case CK_Toggle_Bookmark:
2387 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2388 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2389 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2390 else
2391 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2392 break;
2393 case CK_Flush_Bookmarks:
2394 book_mark_flush (edit, BOOK_MARK_COLOR);
2395 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2396 edit->force |= REDRAW_PAGE;
2397 break;
2398 case CK_Next_Bookmark:
2399 if (edit->book_mark) {
2400 struct _book_mark *p;
2401 p = (struct _book_mark *) book_mark_find (edit,
2402 edit->curs_line);
2403 if (p->next) {
2404 p = p->next;
2405 if (p->line >= edit->start_line + edit->num_widget_lines
2406 || p->line < edit->start_line)
2407 edit_move_display (edit,
2408 p->line -
2409 edit->num_widget_lines / 2);
2410 edit_move_to_line (edit, p->line);
2413 break;
2414 case CK_Prev_Bookmark:
2415 if (edit->book_mark) {
2416 struct _book_mark *p;
2417 p = (struct _book_mark *) book_mark_find (edit,
2418 edit->curs_line);
2419 while (p->line == edit->curs_line)
2420 if (p->prev)
2421 p = p->prev;
2422 if (p->line >= 0) {
2423 if (p->line >= edit->start_line + edit->num_widget_lines
2424 || p->line < edit->start_line)
2425 edit_move_display (edit,
2426 p->line -
2427 edit->num_widget_lines / 2);
2428 edit_move_to_line (edit, p->line);
2431 break;
2433 case CK_Beginning_Of_Text:
2434 case CK_Beginning_Of_Text_Highlight:
2435 edit_move_to_top (edit);
2436 break;
2437 case CK_End_Of_Text:
2438 case CK_End_Of_Text_Highlight:
2439 edit_move_to_bottom (edit);
2440 break;
2442 case CK_Copy:
2443 edit_block_copy_cmd (edit);
2444 break;
2445 case CK_Remove:
2446 edit_block_delete_cmd (edit);
2447 break;
2448 case CK_Move:
2449 edit_block_move_cmd (edit);
2450 break;
2452 case CK_XStore:
2453 edit_copy_to_X_buf_cmd (edit);
2454 break;
2455 case CK_XCut:
2456 edit_cut_to_X_buf_cmd (edit);
2457 break;
2458 case CK_XPaste:
2459 edit_paste_from_X_buf_cmd (edit);
2460 break;
2461 case CK_Selection_History:
2462 edit_paste_from_history (edit);
2463 break;
2465 case CK_Save_As:
2466 edit_save_as_cmd (edit);
2467 break;
2468 case CK_Save:
2469 edit_save_confirm_cmd (edit);
2470 break;
2471 case CK_Load:
2472 edit_load_cmd (edit);
2473 break;
2474 case CK_Save_Block:
2475 edit_save_block_cmd (edit);
2476 break;
2477 case CK_Insert_File:
2478 edit_insert_file_cmd (edit);
2479 break;
2481 case CK_Toggle_Syntax:
2482 if ((option_syntax_highlighting ^= 1) == 1)
2483 edit_load_syntax (edit, NULL, option_syntax_type);
2484 edit->force |= REDRAW_PAGE;
2485 break;
2487 case CK_Find:
2488 edit_search_cmd (edit, 0);
2489 break;
2490 case CK_Find_Again:
2491 edit_search_cmd (edit, 1);
2492 break;
2493 case CK_Replace:
2494 edit_replace_cmd (edit, 0);
2495 break;
2496 case CK_Replace_Again:
2497 edit_replace_cmd (edit, 1);
2498 break;
2499 case CK_Complete_Word:
2500 edit_complete_word_cmd (edit);
2501 break;
2503 case CK_Exit:
2504 dlg_stop (edit->widget.parent);
2505 break;
2506 case CK_New:
2507 edit_new_cmd (edit);
2508 break;
2510 case CK_Help:
2511 edit_help_cmd (edit);
2512 break;
2514 case CK_Refresh:
2515 edit_refresh_cmd (edit);
2516 break;
2518 case CK_Date:{
2519 char s[1024];
2520 /* fool gcc to prevent a Y2K warning */
2521 char time_format[] = "_c";
2522 time_format[0] = '%';
2524 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
2525 edit_print_string (edit, s);
2526 edit->force |= REDRAW_PAGE;
2527 break;
2529 case CK_Goto:
2530 edit_goto_cmd (edit);
2531 break;
2532 case CK_Paragraph_Format:
2533 format_paragraph (edit, 1);
2534 edit->force |= REDRAW_PAGE;
2535 break;
2536 case CK_Delete_Macro:
2537 edit_delete_macro_cmd (edit);
2538 break;
2539 case CK_Match_Bracket:
2540 edit_goto_matching_bracket (edit);
2541 break;
2542 case CK_User_Menu:
2543 user_menu (edit);
2544 break;
2545 case CK_Sort:
2546 edit_sort_cmd (edit);
2547 break;
2548 case CK_ExtCmd:
2549 edit_ext_cmd (edit);
2550 break;
2551 case CK_Mail:
2552 edit_mail_dialog (edit);
2553 break;
2554 case CK_Shell:
2555 view_other_cmd ();
2556 break;
2557 case CK_Select_Codepage:
2558 edit_select_codepage_cmd (edit);
2559 break;
2560 case CK_Insert_Literal:
2561 edit_insert_literal_cmd (edit);
2562 break;
2563 case CK_Execute_Macro:
2564 edit_execute_macro_cmd (edit);
2565 break;
2566 case CK_Begin_End_Macro:
2567 edit_begin_end_macro_cmd (edit);
2568 break;
2569 case CK_Ext_Mode:
2570 edit->extmod = 1;
2571 break;
2572 default:
2573 break;
2576 /* CK_Pipe_Block */
2577 if ((command / 1000) == 1) /* a shell command */
2578 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2579 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2580 struct macro m[MAX_MACRO_LENGTH];
2581 int nm;
2582 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
2583 edit_execute_macro (edit, m, nm);
2586 /* keys which must set the col position, and the search vars */
2587 switch (command) {
2588 case CK_Find:
2589 case CK_Find_Again:
2590 case CK_Replace:
2591 case CK_Replace_Again:
2592 case CK_Complete_Word:
2593 edit->prev_col = edit_get_col (edit);
2594 break;
2595 case CK_Up:
2596 case CK_Up_Highlight:
2597 case CK_Down:
2598 case CK_Down_Highlight:
2599 case CK_Page_Up:
2600 case CK_Page_Up_Highlight:
2601 case CK_Page_Down:
2602 case CK_Page_Down_Highlight:
2603 case CK_Beginning_Of_Text:
2604 case CK_Beginning_Of_Text_Highlight:
2605 case CK_End_Of_Text:
2606 case CK_End_Of_Text_Highlight:
2607 case CK_Paragraph_Up:
2608 case CK_Paragraph_Up_Highlight:
2609 case CK_Paragraph_Down:
2610 case CK_Paragraph_Down_Highlight:
2611 case CK_Scroll_Up:
2612 case CK_Scroll_Up_Highlight:
2613 case CK_Scroll_Down:
2614 case CK_Scroll_Down_Highlight:
2615 edit->search_start = edit->curs1;
2616 edit->found_len = 0;
2617 break;
2618 default:
2619 edit->found_len = 0;
2620 edit->prev_col = edit_get_col (edit);
2621 edit->search_start = edit->curs1;
2623 edit_find_bracket (edit);
2625 if (option_auto_para_formatting) {
2626 switch (command) {
2627 case CK_BackSpace:
2628 case CK_Delete:
2629 case CK_Delete_Word_Left:
2630 case CK_Delete_Word_Right:
2631 case CK_Delete_To_Line_End:
2632 case CK_Delete_To_Line_Begin:
2633 format_paragraph (edit, 0);
2634 edit->force |= REDRAW_PAGE;
2640 static void
2641 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
2643 int i = 0;
2645 if (edit->macro_depth++ > 256) {
2646 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2647 edit->macro_depth--;
2648 return;
2650 edit->force |= REDRAW_PAGE;
2651 for (; i < n; i++) {
2652 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2654 edit_update_screen (edit);
2655 edit->macro_depth--;
2658 /* User edit menu, like user menu (F2) but only in editor. */
2659 static void
2660 user_menu (WEdit * edit)
2662 FILE *fd;
2663 int nomark;
2664 struct stat status;
2665 long start_mark, end_mark;
2666 char *block_file = mhl_str_dir_plus_file (home_dir, BLOCK_FILE);
2667 int rc = 0;
2669 nomark = eval_marks (edit, &start_mark, &end_mark);
2670 if (!nomark) /* remember marked or not */
2671 edit_save_block (edit, block_file, start_mark, end_mark);
2673 /* run shell scripts from menu */
2674 user_menu_cmd (edit);
2676 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2677 /* no block messages */
2678 goto cleanup;
2681 if (!nomark) {
2682 /* i.e. we have marked block */
2683 rc = edit_block_delete_cmd (edit);
2686 if (!rc) {
2687 edit_insert_file (edit, block_file);
2690 /* truncate block file */
2691 if ((fd = fopen (block_file, "w"))) {
2692 fclose (fd);
2695 edit_refresh_cmd (edit);
2696 edit->force |= REDRAW_COMPLETELY;
2698 cleanup:
2699 mhl_mem_free (block_file);