* mcfs.c: Move config,h to the beginning.
[midnight-commander.git] / edit / edit.c
blob77f2540bab0e68bc0ad06451518902d00a0676bf
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 $Id$
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307, USA.
25 #include <config.h>
26 #if defined(NEEDS_IO_H)
27 # include <io.h>
28 # define CR_LF_TRANSLATION
29 #endif
30 #include "edit.h"
32 #ifdef HAVE_CHARSET
33 #include "src/charsets.h"
34 #include "src/selcodepage.h"
35 #endif
38 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
39 or EDIT_KEY_EMULATION_EMACS
41 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
43 int option_word_wrap_line_length = 72;
44 int option_typewriter_wrap = 0;
45 int option_auto_para_formatting = 0;
46 int option_tab_spacing = 8;
47 int option_fill_tabs_with_spaces = 0;
48 int option_return_does_auto_indent = 1;
49 int option_backspace_through_tabs = 0;
50 int option_fake_half_tabs = 1;
51 int option_save_mode = 0;
52 int option_backup_ext_int = -1;
53 int option_max_undo = 32768;
55 int option_edit_right_extreme = 0;
56 int option_edit_left_extreme = 0;
57 int option_edit_top_extreme = 0;
58 int option_edit_bottom_extreme = 0;
60 char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
61 char *option_backup_ext = "~";
63 static struct selection selection;
64 static int current_selection = 0;
65 /* Note: selection.text = selection_history[current_selection].text */
66 static struct selection selection_history[NUM_SELECTION_HISTORY];
68 static char *option_chars_move_whole_word =
69 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
73 * here's a quick sketch of the layout: (don't run this through indent.)
75 * (b1 is buffers1 and b2 is buffers2)
77 * |
78 * \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
79 * ______________________________________|______________________________________
80 * |
81 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
82 * |-> |-> |-> |-> |-> |-> |
83 * |
84 * _<------------------------->|<----------------->_
85 * WEdit->curs2 | WEdit->curs1
86 * ^ | ^
87 * | ^|^ |
88 * cursor ||| cursor
89 * |||
90 * file end|||file beginning
91 * |
92 * |
94 * _
95 * This_is_some_file
96 * fin.
102 returns a byte from any location in the file.
103 Returns '\n' if out of bounds.
106 static int push_action_disabled = 0;
108 #ifndef NO_INLINE_GETBYTE
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];
124 #endif
126 char *edit_get_buffer_as_text (WEdit * e)
128 int l, i;
129 char *t;
130 l = e->curs1 + e->curs2;
131 t = CMalloc (l + 1);
132 for (i = 0; i < l; i++)
133 t[i] = edit_get_byte (e, i);
134 t[l] = 0;
135 return t;
139 /* Note on CRLF->LF translation: */
141 #if MY_O_TEXT
142 #error "MY_O_TEXT is depreciated. CR_LF_TRANSLATION must be defined which does CR-LF translation internally. See note in source."
143 #endif
146 The edit_open_file (previously edit_load_file) function uses
147 init_dynamic_edit_buffers to load a file. This is unnecessary
148 since you can just as well fopen the file and insert the
149 characters one by one. The real reason for
150 init_dynamic_edit_buffers (besides allocating the buffers) is
151 as an optimisation - it uses raw block reads and inserts large
152 chunks at a time. It is hence extremely fast at loading files.
153 Where we might not want to use it is if we were doing
154 CRLF->LF translation or if we were reading from a pipe.
157 /* Initialisation routines */
159 /* returns 1 on error */
160 /* loads file OR text into buffers. Only one must be none-NULL. */
161 /* cursor set to start of file */
162 int init_dynamic_edit_buffers (WEdit * edit, const char *filename, const char *text)
164 long buf;
165 int j, file = -1, buf2;
167 for (j = 0; j <= MAXBUFF; j++) {
168 edit->buffers1[j] = NULL;
169 edit->buffers2[j] = NULL;
172 if (filename)
173 if ((file = mc_open (filename, O_RDONLY)) == -1) {
174 /* The file-name is printed after the ':' */
175 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename, " ", 0)));
176 return 1;
178 edit->curs2 = edit->last_byte;
180 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
182 edit->buffers2[buf2] = CMalloc (EDIT_BUF_SIZE);
184 if (filename) {
185 mc_read (file, (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE);
186 } else {
187 memcpy (edit->buffers2[buf2] + EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE), text, edit->curs2 & M_EDIT_BUF_SIZE);
188 text += edit->curs2 & M_EDIT_BUF_SIZE;
191 for (buf = buf2 - 1; buf >= 0; buf--) {
192 edit->buffers2[buf] = CMalloc (EDIT_BUF_SIZE);
193 if (filename) {
194 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
195 } else {
196 memcpy (edit->buffers2[buf], text, EDIT_BUF_SIZE);
197 text += EDIT_BUF_SIZE;
201 edit->curs1 = 0;
202 if (file != -1)
203 mc_close (file);
204 return 0;
207 /* detecting an error on save is easy: just check if every byte has been written. */
208 /* detecting an error on read, is not so easy 'cos there is not way to tell
209 whether you read everything or not. */
210 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
211 static const struct edit_filters {
212 char *read, *write, *extension;
213 } all_filters[] = {
216 "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2"
219 "gzip -cd %s 2>&1", "gzip > %s", ".gz"
222 "gzip -cd %s 2>&1", "gzip > %s", ".Z"
226 static int edit_find_filter (const char *filename)
228 int i, l;
229 if (!filename)
230 return -1;
231 l = strlen (filename);
232 for (i = 0; i < sizeof (all_filters) / sizeof (struct edit_filters); i++) {
233 int e;
234 e = strlen (all_filters[i].extension);
235 if (l > e)
236 if (!strcmp (all_filters[i].extension, filename + l - e))
237 return i;
239 return -1;
242 char *edit_get_filter (const char *filename)
244 int i, l;
245 char *p;
246 i = edit_find_filter (filename);
247 if (i < 0)
248 return 0;
249 l = strlen (filename);
250 p = malloc (strlen (all_filters[i].read) + l + 2);
251 sprintf (p, all_filters[i].read, filename);
252 return p;
255 char *edit_get_write_filter (char *writename, const char *filename)
257 int i, l;
258 char *p;
259 i = edit_find_filter (filename);
260 if (i < 0)
261 return 0;
262 l = strlen (writename);
263 p = malloc (strlen (all_filters[i].write) + l + 2);
264 sprintf (p, all_filters[i].write, writename);
265 return p;
268 #ifdef CR_LF_TRANSLATION
269 /* reads into buffer, replace \r\n with \n */
270 long edit_insert_stream (WEdit * edit, FILE * f)
272 int a = -1, b;
273 long i;
274 while ((b = fgetc (f))) {
275 if (a == '\r' && b == '\n') {
276 edit_insert (edit, '\n');
277 i++;
278 a = -1;
279 continue;
280 } else if (a >= 0) {
281 edit_insert (edit, a);
282 i++;
284 a = b;
286 if (a >= 0)
287 edit_insert (edit, a);
288 return i;
290 /* writes buffer, replaces, replace \n with \r\n */
291 long edit_write_stream (WEdit * edit, FILE * f)
293 long i;
294 int c;
295 for (i = 0; i < edit->last_byte; i++) {
296 c = edit_get_byte (edit, i);
297 if (c == '\n') {
298 if (fputc ('\r', f) < 0)
299 break;
301 if (fputc (c, f) < 0)
302 break;
304 return i;
306 #else /* CR_LF_TRANSLATION */
307 long edit_insert_stream (WEdit * edit, FILE * f)
309 int c;
310 long i = 0;
311 while ((c = fgetc (f)) >= 0) {
312 edit_insert (edit, c);
313 i++;
315 return i;
317 long edit_write_stream (WEdit * edit, FILE * f)
319 long i;
320 for (i = 0; i < edit->last_byte; i++)
321 if (fputc (edit_get_byte (edit, i), f) < 0)
322 break;
323 return i;
325 #endif /* CR_LF_TRANSLATION */
327 #define TEMP_BUF_LEN 1024
329 /* inserts a file at the cursor, returns 1 on success */
330 int edit_insert_file (WEdit * edit, const char *filename)
332 char *p;
333 if ((p = edit_get_filter (filename))) {
334 FILE *f;
335 long current = edit->curs1;
336 f = (FILE *) popen (p, "r");
337 if (f) {
338 edit_insert_stream (edit, f);
339 edit_cursor_move (edit, current - edit->curs1);
340 if (pclose (f) > 0) {
341 edit_error_dialog (_ (" Error "), catstrs (_ (" Error reading from pipe: "), p, " ", 0));
342 free (p);
343 return 0;
345 } else {
346 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open pipe for reading: "), p, " ", 0)));
347 free (p);
348 return 0;
350 free (p);
351 #ifdef CR_LF_TRANSLATION
352 } else {
353 FILE *f;
354 long current = edit->curs1;
355 f = fopen (filename, "r");
356 if (f) {
357 edit_insert_stream (edit, f);
358 edit_cursor_move (edit, current - edit->curs1);
359 if (fclose (f)) {
360 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Error reading file: "), filename, " ", 0)));
361 return 0;
363 } else {
364 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename, " ", 0)));
365 return 0;
367 #else
368 } else {
369 int i, file, blocklen;
370 long current = edit->curs1;
371 unsigned char *buf;
372 if ((file = mc_open (filename, O_RDONLY)) == -1)
373 return 0;
374 buf = malloc (TEMP_BUF_LEN);
375 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
376 for (i = 0; i < blocklen; i++)
377 edit_insert (edit, buf[i]);
379 edit_cursor_move (edit, current - edit->curs1);
380 free (buf);
381 mc_close (file);
382 if (blocklen)
383 return 0;
384 #endif
386 return 1;
389 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
390 static int check_file_access (WEdit *edit, const char *filename, struct stat *st)
392 int file;
393 int stat_ok = 0;
395 /* Try stat first to prevent getting stuck on pipes */
396 if (mc_stat ((char *) filename, st) == 0) {
397 stat_ok = 1;
400 /* Only regular files are allowed */
401 if (stat_ok && !S_ISREG (st->st_mode)) {
402 edit_error_dialog (_ (" Error "), catstrs (_ (" Not an ordinary file: "), filename, " ", 0));
403 return 1;
406 /* Open the file, create it if needed */
407 if ((file = mc_open (filename, O_RDONLY | O_CREAT, 0666)) < 0) {
408 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename, " ", 0)));
409 return 1;
412 /* If the file has just been created, we don't have valid stat yet, so do it now */
413 if (!stat_ok && mc_fstat (file, st) < 0) {
414 mc_close (file);
415 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Cannot get size/permissions info on file: "), filename, " ", 0)));
416 return 1;
418 mc_close (file);
420 /* If it's a new file, delete it if it's not modified or saved */
421 if (!stat_ok && st->st_size == 0) {
422 edit->delete_file = 1;
425 if (st->st_size >= SIZE_LIMIT) {
426 /* The file-name is printed after the ':' */
427 edit_error_dialog (_ (" Error "), catstrs (_ (" File is too large: "), \
428 filename, _ (" \n Increase edit.h:MAXBUF and recompile the editor. "), 0));
429 return 1;
431 return 0;
434 /* returns 1 on error */
435 int edit_open_file (WEdit * edit, const char *filename, const char *text, unsigned long text_size)
437 struct stat st;
438 if (text) {
439 edit->last_byte = text_size;
440 filename = 0;
441 } else {
442 int r;
443 r = check_file_access (edit, filename, &st);
444 if (r)
445 return 1;
446 edit->stat1 = st;
447 #ifndef CR_LF_TRANSLATION
448 edit->last_byte = st.st_size;
449 #else
450 /* going to read the file into the buffer later byte by byte */
451 edit->last_byte = 0;
452 filename = 0;
453 text = "";
454 #endif
456 return init_dynamic_edit_buffers (edit, filename, text);
459 #define space_width 1
461 /* fills in the edit struct. returns 0 on fail. Pass edit as NULL for this */
462 WEdit *edit_init (WEdit * edit, int lines, int columns, const char *filename, const char *text, const char *dir, unsigned long text_size)
464 const char *f;
465 int to_free = 0;
466 int use_filter = 0;
468 if (!edit) {
469 #ifdef ENABLE_NLS
471 * Expand option_whole_chars_search by national letters using
472 * current locale
475 static char option_whole_chars_search_buf [256];
477 if (option_whole_chars_search_buf != option_whole_chars_search) {
478 int i;
479 int len = strlen (option_whole_chars_search);
481 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
483 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
484 if (islower (i) && !strchr (option_whole_chars_search, i)) {
485 option_whole_chars_search_buf [len++] = i;
489 option_whole_chars_search_buf [len] = 0;
490 option_whole_chars_search = option_whole_chars_search_buf;
492 #endif /* ENABLE_NLS */
493 edit = g_malloc (sizeof (WEdit));
494 memset (edit, 0, sizeof (WEdit));
495 to_free = 1;
497 memset (&(edit->from_here), 0, (unsigned long)&(edit->to_here) - (unsigned long)&(edit->from_here));
498 edit->num_widget_lines = lines;
499 edit->num_widget_columns = columns;
500 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
501 edit->stat1.st_uid = getuid ();
502 edit->stat1.st_gid = getgid ();
503 edit->bracket = -1;
504 if (!dir)
505 dir = "";
506 f = filename;
507 if (filename) {
508 f = catstrs (dir, filename, 0);
510 if (edit_find_filter (f) < 0) {
511 #ifdef CR_LF_TRANSLATION
512 use_filter = 1;
513 #endif
514 if (edit_open_file (edit, f, text, text_size)) {
515 /* edit_load_file already gives an error message */
516 if (to_free)
517 g_free (edit);
518 return 0;
520 } else {
521 use_filter = 1;
522 if (edit_open_file (edit, 0, "", 0)) {
523 if (to_free)
524 g_free (edit);
525 return 0;
528 edit->force |= REDRAW_PAGE;
529 if (filename) {
530 filename = catstrs (dir, filename, 0);
531 edit_split_filename (edit, filename);
532 } else {
533 edit->filename = (char *) strdup ("");
534 edit->dir = (char *) strdup (dir);
536 edit->stack_size = START_STACK_SIZE;
537 edit->stack_size_mask = START_STACK_SIZE - 1;
538 edit->undo_stack = malloc ((edit->stack_size + 10) * sizeof (long));
539 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
540 if (use_filter) {
541 push_action_disabled = 1;
542 if (check_file_access (edit, filename, &(edit->stat1))
543 || !edit_insert_file (edit, f))
545 edit_clean (edit);
546 if (to_free)
547 g_free (edit);
548 return 0;
550 /* FIXME: this should be an unmodification() function */
551 push_action_disabled = 0;
553 edit->modified = 0;
554 edit_load_syntax (edit, 0, 0);
556 int color;
557 edit_get_syntax_color (edit, -1, &color);
559 return edit;
562 /* clear the edit struct, freeing everything in it. returns 1 on success */
563 int edit_clean (WEdit * edit)
565 if (edit) {
566 int j = 0;
567 edit_free_syntax_rules (edit);
568 book_mark_flush (edit, -1);
569 for (; j <= MAXBUFF; j++) {
570 if (edit->buffers1[j] != NULL)
571 free (edit->buffers1[j]);
572 if (edit->buffers2[j] != NULL)
573 free (edit->buffers2[j]);
576 if (edit->undo_stack)
577 free (edit->undo_stack);
578 if (edit->filename)
579 free (edit->filename);
580 if (edit->dir)
581 free (edit->dir);
582 /* we don't want to clear the widget */
583 memset (&(edit->from_here), 0, (unsigned long)&(edit->to_here) - (unsigned long)&(edit->from_here));
585 /* Free temporary strings used in catstrs() */
586 freestrs();
588 return 1;
590 return 0;
594 /* returns 1 on success */
595 int edit_renew (WEdit * edit)
597 int lines = edit->num_widget_lines;
598 int columns = edit->num_widget_columns;
599 char *dir;
600 int retval = 1;
602 if (edit->dir)
603 dir = (char *) strdup (edit->dir);
604 else
605 dir = 0;
607 edit_clean (edit);
608 if (!edit_init (edit, lines, columns, 0, "", dir, 0))
609 retval = 0;
610 if (dir)
611 free (dir);
612 return retval;
615 /* returns 1 on success, if returns 0, the edit struct would have been free'd */
616 int edit_reload (WEdit * edit, const char *filename, const char *text, const char *dir, unsigned long text_size)
618 WEdit *e;
619 int lines = edit->num_widget_lines;
620 int columns = edit->num_widget_columns;
621 e = g_malloc (sizeof (WEdit));
622 memset (e, 0, sizeof (WEdit));
623 e->widget = edit->widget;
624 e->macro_i = -1;
625 if (!edit_init (e, lines, columns, filename, text, dir, text_size)) {
626 g_free (e);
627 return 0;
629 edit_clean (edit);
630 memcpy (edit, e, sizeof (WEdit));
631 g_free (e);
632 return 1;
637 Recording stack for undo:
638 The following is an implementation of a compressed stack. Identical
639 pushes are recorded by a negative prefix indicating the number of times the
640 same char was pushed. This saves space for repeated curs-left or curs-right
641 delete etc.
645 pushed: stored:
649 b -3
651 c --> -4
657 If the stack long int is 0-255 it represents a normal insert (from a backspace),
658 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
659 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
660 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
661 position.
663 The only way the cursor moves or the buffer is changed is through the routines:
664 insert, backspace, insert_ahead, delete, and cursor_move.
665 These record the reverse undo movements onto the stack each time they are
666 called.
668 Each key press results in a set of actions (insert; delete ...). So each time
669 a key is pressed the current position of start_display is pushed as
670 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
671 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
672 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
676 void edit_push_action (WEdit * edit, long c,...)
678 unsigned long sp = edit->stack_pointer;
679 unsigned long spm1;
680 long *t;
681 /* first enlarge the stack if necessary */
682 if (sp > edit->stack_size - 10) { /* say */
683 if (option_max_undo < 256)
684 option_max_undo = 256;
685 if (edit->stack_size < option_max_undo) {
686 t = malloc ((edit->stack_size * 2 + 10) * sizeof (long));
687 if (t) {
688 memcpy (t, edit->undo_stack, sizeof (long) * edit->stack_size);
689 free (edit->undo_stack);
690 edit->undo_stack = t;
691 edit->stack_size <<= 1;
692 edit->stack_size_mask = edit->stack_size - 1;
696 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
697 if (push_action_disabled)
698 return;
700 #ifdef FAST_MOVE_CURSOR
701 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
702 va_list ap;
703 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
704 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
705 va_start (ap, c);
706 c = -(va_arg (ap, int));
707 va_end (ap);
708 } else
709 #endif /* ! FAST_MOVE_CURSOR */
710 if (edit->stack_bottom != sp
711 && spm1 != edit->stack_bottom
712 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
713 int d;
714 if (edit->undo_stack[spm1] < 0) {
715 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
716 if (d == c) {
717 if (edit->undo_stack[spm1] > -1000000000) {
718 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
719 edit->undo_stack[spm1]--;
720 return;
723 /* #define NO_STACK_CURSMOVE_ANIHILATION */
724 #ifndef NO_STACK_CURSMOVE_ANIHILATION
725 else if ((c == CURS_LEFT && d == CURS_RIGHT)
726 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
727 if (edit->undo_stack[spm1] == -2)
728 edit->stack_pointer = spm1;
729 else
730 edit->undo_stack[spm1]++;
731 return;
733 #endif
734 } else {
735 d = edit->undo_stack[spm1];
736 if (d == c) {
737 if (c >= KEY_PRESS)
738 return; /* --> no need to push multiple do-nothings */
739 edit->undo_stack[sp] = -2;
740 goto check_bottom;
742 #ifndef NO_STACK_CURSMOVE_ANIHILATION
743 else if ((c == CURS_LEFT && d == CURS_RIGHT)
744 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
745 edit->stack_pointer = spm1;
746 return;
748 #endif
751 edit->undo_stack[sp] = c;
752 check_bottom:
754 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
756 /*if the sp wraps round and catches the stack_bottom then erase the first set of actions on the stack to make space - by moving stack_bottom forward one "key press" */
757 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
758 if (c == edit->stack_bottom || ((c + 1) & edit->stack_size_mask) == edit->stack_bottom)
759 do {
760 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
761 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
763 /*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: */
764 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
765 edit->stack_bottom = edit->stack_pointer = 0;
769 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
770 then the file should be as it was when he loaded up. Then set edit->modified to 0.
772 long pop_action (WEdit * edit)
774 long c;
775 unsigned long sp = edit->stack_pointer;
776 if (sp == edit->stack_bottom) {
777 return STACK_BOTTOM;
779 sp = (sp - 1) & edit->stack_size_mask;
780 if ((c = edit->undo_stack[sp]) >= 0) {
781 /* edit->undo_stack[sp] = '@'; */
782 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
783 return c;
785 if (sp == edit->stack_bottom) {
786 return STACK_BOTTOM;
788 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
789 if (edit->undo_stack[sp] == -2) {
790 /* edit->undo_stack[sp] = '@'; */
791 edit->stack_pointer = sp;
792 } else
793 edit->undo_stack[sp]++;
795 return c;
798 /* is called whenever a modification is made by one of the four routines below */
799 static inline void edit_modification (WEdit * edit)
801 edit->caches_valid = 0;
802 edit->modified = 1;
803 edit->screen_modified = 1;
807 Basic low level single character buffer alterations and movements at the cursor.
808 Returns char passed over, inserted or removed.
811 void edit_insert (WEdit * edit, int c)
813 /* check if file has grown to large */
814 if (edit->last_byte >= SIZE_LIMIT)
815 return;
817 /* first we must update the position of the display window */
818 if (edit->curs1 < edit->start_display) {
819 edit->start_display++;
820 if (c == '\n')
821 edit->start_line++;
823 /* now we must update some info on the file and check if a redraw is required */
824 if (c == '\n') {
825 if (edit->book_mark)
826 book_mark_inc (edit, edit->curs_line);
827 edit->curs_line++;
828 edit->total_lines++;
829 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
831 /* tell that we've modified the file */
832 edit_modification (edit);
834 /* save the reverse command onto the undo stack */
835 edit_push_action (edit, BACKSPACE);
837 /* update markers */
838 edit->mark1 += (edit->mark1 > edit->curs1);
839 edit->mark2 += (edit->mark2 > edit->curs1);
840 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
842 /* add a new buffer if we've reached the end of the last one */
843 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
844 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
846 /* perfprm the insertion */
847 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = (unsigned char) c;
849 /* update file length */
850 edit->last_byte++;
852 /* update cursor position */
853 edit->curs1++;
857 /* same as edit_insert and move left */
858 void edit_insert_ahead (WEdit * edit, int c)
860 if (edit->last_byte >= SIZE_LIMIT)
861 return;
862 if (edit->curs1 < edit->start_display) {
863 edit->start_display++;
864 if (c == '\n')
865 edit->start_line++;
867 if (c == '\n') {
868 if (edit->book_mark)
869 book_mark_inc (edit, edit->curs_line);
870 edit->total_lines++;
871 edit->force |= REDRAW_AFTER_CURSOR;
873 edit_modification (edit);
874 edit_push_action (edit, DELCHAR);
876 edit->mark1 += (edit->mark1 >= edit->curs1);
877 edit->mark2 += (edit->mark2 >= edit->curs1);
878 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
880 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
881 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
882 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
884 edit->last_byte++;
885 edit->curs2++;
889 int edit_delete (WEdit * edit)
891 int p;
892 if (!edit->curs2)
893 return 0;
895 edit->mark1 -= (edit->mark1 > edit->curs1);
896 edit->mark2 -= (edit->mark2 > edit->curs1);
897 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
899 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
901 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
902 free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
903 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
905 edit->last_byte--;
906 edit->curs2--;
908 if (p == '\n') {
909 if (edit->book_mark)
910 book_mark_dec (edit, edit->curs_line);
911 edit->total_lines--;
912 edit->force |= REDRAW_AFTER_CURSOR;
914 edit_push_action (edit, p + 256);
915 if (edit->curs1 < edit->start_display) {
916 edit->start_display--;
917 if (p == '\n')
918 edit->start_line--;
920 edit_modification (edit);
922 return p;
926 int edit_backspace (WEdit * edit)
928 int p;
929 if (!edit->curs1)
930 return 0;
932 edit->mark1 -= (edit->mark1 >= edit->curs1);
933 edit->mark2 -= (edit->mark2 >= edit->curs1);
934 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
936 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
937 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
938 free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
939 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
941 edit->last_byte--;
942 edit->curs1--;
944 if (p == '\n') {
945 if (edit->book_mark)
946 book_mark_dec (edit, edit->curs_line);
947 edit->curs_line--;
948 edit->total_lines--;
949 edit->force |= REDRAW_AFTER_CURSOR;
951 edit_push_action (edit, p);
953 if (edit->curs1 < edit->start_display) {
954 edit->start_display--;
955 if (p == '\n')
956 edit->start_line--;
958 edit_modification (edit);
960 return p;
963 #ifdef FAST_MOVE_CURSOR
965 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
967 unsigned long next;
968 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
969 edit->curs_line--;
970 next -= (unsigned long) dest;
971 n -= next;
972 src += next;
973 dest += next;
977 int edit_move_backward_lots (WEdit * edit, long increment)
979 int r, s, t;
980 unsigned char *p;
982 if (increment > edit->curs1)
983 increment = edit->curs1;
984 if (increment <= 0)
985 return -1;
986 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
988 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
989 if (r > increment)
990 r = increment;
991 s = edit->curs1 & M_EDIT_BUF_SIZE;
993 p = 0;
994 if (s > r) {
995 memqcpy (edit, edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
996 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
997 } else {
998 if (s) {
999 memqcpy (edit, edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - s,
1000 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1001 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1002 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1004 memqcpy (edit, edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1005 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] + EDIT_BUF_SIZE - (r - s), r - s);
1007 increment -= r;
1008 edit->curs1 -= r;
1009 edit->curs2 += r;
1010 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1011 if (p)
1012 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1013 else
1014 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
1015 } else {
1016 if (p)
1017 free (p);
1020 s = edit->curs1 & M_EDIT_BUF_SIZE;
1021 while (increment) {
1022 p = 0;
1023 r = EDIT_BUF_SIZE;
1024 if (r > increment)
1025 r = increment;
1026 t = s;
1027 if (r < t)
1028 t = r;
1029 memqcpy (edit,
1030 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + EDIT_BUF_SIZE - t,
1031 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1033 if (r >= s) {
1034 if (t) {
1035 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1036 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1038 memqcpy (edit,
1039 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + EDIT_BUF_SIZE - r,
1040 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] + EDIT_BUF_SIZE - (r - s),
1041 r - s);
1043 increment -= r;
1044 edit->curs1 -= r;
1045 edit->curs2 += r;
1046 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1047 if (p)
1048 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1049 else
1050 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
1051 } else {
1052 if (p)
1053 free (p);
1056 return edit_get_byte (edit, edit->curs1);
1059 #endif /* ! FAST_MOVE_CURSOR */
1061 /* moves the cursor right or left: increment positive or negative respectively */
1062 int edit_cursor_move (WEdit * edit, long increment)
1064 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1065 int c;
1067 #ifdef FAST_MOVE_CURSOR
1068 if (increment < -256) {
1069 edit->force |= REDRAW_PAGE;
1070 return edit_move_backward_lots (edit, -increment);
1072 #endif /* ! FAST_MOVE_CURSOR */
1074 if (increment < 0) {
1075 for (; increment < 0; increment++) {
1076 if (!edit->curs1)
1077 return -1;
1079 edit_push_action (edit, CURS_RIGHT);
1081 c = edit_get_byte (edit, edit->curs1 - 1);
1082 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1083 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
1084 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1085 edit->curs2++;
1086 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1087 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1088 free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1089 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1091 edit->curs1--;
1092 if (c == '\n') {
1093 edit->curs_line--;
1094 edit->force |= REDRAW_LINE_BELOW;
1098 return c;
1099 } else if (increment > 0) {
1100 for (; increment > 0; increment--) {
1101 if (!edit->curs2)
1102 return -2;
1104 edit_push_action (edit, CURS_LEFT);
1106 c = edit_get_byte (edit, edit->curs1);
1107 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1108 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
1109 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1110 edit->curs1++;
1111 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1112 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1113 free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1114 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1116 edit->curs2--;
1117 if (c == '\n') {
1118 edit->curs_line++;
1119 edit->force |= REDRAW_LINE_ABOVE;
1122 return c;
1123 } else
1124 return -3;
1127 /* These functions return positions relative to lines */
1129 /* returns index of last char on line + 1 */
1130 long edit_eol (WEdit * edit, long current)
1132 if (current < edit->last_byte) {
1133 for (;; current++)
1134 if (edit_get_byte (edit, current) == '\n')
1135 break;
1136 } else
1137 return edit->last_byte;
1138 return current;
1141 /* returns index of first char on line */
1142 long edit_bol (WEdit * edit, long current)
1144 if (current > 0) {
1145 for (;; current--)
1146 if (edit_get_byte (edit, current - 1) == '\n')
1147 break;
1148 } else
1149 return 0;
1150 return current;
1154 int edit_count_lines (WEdit * edit, long current, int upto)
1156 int lines = 0;
1157 if (upto > edit->last_byte)
1158 upto = edit->last_byte;
1159 if (current < 0)
1160 current = 0;
1161 while (current < upto)
1162 if (edit_get_byte (edit, current++) == '\n')
1163 lines++;
1164 return lines;
1168 /* If lines is zero this returns the count of lines from current to upto. */
1169 /* If upto is zero returns index of lines forward current. */
1170 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1172 if (upto) {
1173 return edit_count_lines (edit, current, upto);
1174 } else {
1175 int next;
1176 if (lines < 0)
1177 lines = 0;
1178 while (lines--) {
1179 next = edit_eol (edit, current) + 1;
1180 if (next > edit->last_byte)
1181 break;
1182 else
1183 current = next;
1185 return current;
1190 /* Returns offset of 'lines' lines up from current */
1191 long edit_move_backward (WEdit * edit, long current, int lines)
1193 if (lines < 0)
1194 lines = 0;
1195 current = edit_bol (edit, current);
1196 while((lines--) && current != 0)
1197 current = edit_bol (edit, current - 1);
1198 return current;
1201 /* If cols is zero this returns the count of columns from current to upto. */
1202 /* If upto is zero returns index of cols across from current. */
1203 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1205 long p, q;
1206 int col = 0;
1208 if (upto) {
1209 q = upto;
1210 cols = -10;
1211 } else
1212 q = edit->last_byte + 2;
1214 for (col = 0, p = current; p < q; p++) {
1215 int c;
1216 if (cols != -10) {
1217 if (col == cols)
1218 return p;
1219 if (col > cols)
1220 return p - 1;
1222 c = edit_get_byte (edit, p);
1223 if (c == '\r')
1224 continue;
1225 else
1226 if (c == '\t')
1227 col += TAB_SIZE - col % TAB_SIZE;
1228 else
1229 col++;
1230 /*if(edit->nroff ... */
1231 if (c == '\n') {
1232 if (upto)
1233 return col;
1234 else
1235 return p;
1238 return col;
1241 /* returns the current column position of the cursor */
1242 int edit_get_col (WEdit * edit)
1244 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1248 /* Scrolling functions */
1250 void edit_update_curs_row (WEdit * edit)
1252 edit->curs_row = edit->curs_line - edit->start_line;
1255 void edit_update_curs_col (WEdit * edit)
1257 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1260 /*moves the display start position up by i lines */
1261 void edit_scroll_upward (WEdit * edit, unsigned long i)
1263 int lines_above = edit->start_line;
1264 if (i > lines_above)
1265 i = lines_above;
1266 if (i) {
1267 edit->start_line -= i;
1268 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1269 edit->force |= REDRAW_PAGE;
1270 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1272 edit_update_curs_row (edit);
1276 /* returns 1 if could scroll, 0 otherwise */
1277 void edit_scroll_downward (WEdit * edit, int i)
1279 int lines_below;
1280 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1281 if (lines_below > 0) {
1282 if (i > lines_below)
1283 i = lines_below;
1284 edit->start_line += i;
1285 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1286 edit->force |= REDRAW_PAGE;
1287 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1289 edit_update_curs_row (edit);
1292 void edit_scroll_right (WEdit * edit, int i)
1294 edit->force |= REDRAW_PAGE;
1295 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1296 edit->start_col -= i;
1299 void edit_scroll_left (WEdit * edit, int i)
1301 if (edit->start_col) {
1302 edit->start_col += i;
1303 if (edit->start_col > 0)
1304 edit->start_col = 0;
1305 edit->force |= REDRAW_PAGE;
1306 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1310 /* high level cursor movement commands */
1312 static int is_in_indent (WEdit *edit)
1314 long p = edit_bol (edit, edit->curs1);
1315 while (p < edit->curs1)
1316 if (!strchr (" \t", edit_get_byte (edit, p++)))
1317 return 0;
1318 return 1;
1321 static int left_of_four_spaces (WEdit *edit);
1323 void edit_move_to_prev_col (WEdit * edit, long p)
1325 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1327 if (is_in_indent (edit) && option_fake_half_tabs) {
1328 edit_update_curs_col (edit);
1329 if (space_width)
1330 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1331 int q = edit->curs_col;
1332 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1333 p = edit_bol (edit, edit->curs1);
1334 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1335 if (!left_of_four_spaces (edit))
1336 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1342 /* move i lines */
1343 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1345 long p, l = edit->curs_line;
1347 if (i > l)
1348 i = l;
1349 if (i) {
1350 if (i > 1)
1351 edit->force |= REDRAW_PAGE;
1352 if (scroll)
1353 edit_scroll_upward (edit, i);
1355 p = edit_bol (edit, edit->curs1);
1356 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1357 edit_move_to_prev_col (edit, p);
1359 edit->search_start = edit->curs1;
1360 edit->found_len = 0;
1364 int is_blank (WEdit * edit, long offset)
1366 long s, f;
1367 int c;
1368 s = edit_bol (edit, offset);
1369 f = edit_eol (edit, offset) - 1;
1370 while (s <= f) {
1371 c = edit_get_byte (edit, s++);
1372 if (!isspace (c))
1373 return 0;
1375 return 1;
1379 /* returns the offset of line i */
1380 long edit_find_line (WEdit * edit, int line)
1382 int i, j = 0;
1383 int m = 2000000000;
1384 if (!edit->caches_valid) {
1385 for (i = 0; i < N_LINE_CACHES; i++)
1386 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1387 /* three offsets that we *know* are line 0 at 0 and these two: */
1388 edit->line_numbers[1] = edit->curs_line;
1389 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1390 edit->line_numbers[2] = edit->total_lines;
1391 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1392 edit->caches_valid = 1;
1394 if (line >= edit->total_lines)
1395 return edit->line_offsets[2];
1396 if (line <= 0)
1397 return 0;
1398 /* find the closest known point */
1399 for (i = 0; i < N_LINE_CACHES; i++) {
1400 int n;
1401 n = abs (edit->line_numbers[i] - line);
1402 if (n < m) {
1403 m = n;
1404 j = i;
1407 if (m == 0)
1408 return edit->line_offsets[j]; /* know the offset exactly */
1409 if (m == 1 && j >= 3)
1410 i = j; /* one line different - caller might be looping, so stay in this cache */
1411 else
1412 i = 3 + (rand () % (N_LINE_CACHES - 3));
1413 if (line > edit->line_numbers[j])
1414 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1415 else
1416 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1417 edit->line_numbers[i] = line;
1418 return edit->line_offsets[i];
1421 int line_is_blank (WEdit * edit, long line)
1423 return is_blank (edit, edit_find_line (edit, line));
1426 /* moves up until a blank line is reached, or until just
1427 before a non-blank line is reached */
1428 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1430 int i;
1431 if (edit->curs_line <= 1) {
1432 i = 0;
1433 } else {
1434 if (line_is_blank (edit, edit->curs_line)) {
1435 if (line_is_blank (edit, edit->curs_line - 1)) {
1436 for (i = edit->curs_line - 1; i; i--)
1437 if (!line_is_blank (edit, i)) {
1438 i++;
1439 break;
1441 } else {
1442 for (i = edit->curs_line - 1; i; i--)
1443 if (line_is_blank (edit, i))
1444 break;
1446 } else {
1447 for (i = edit->curs_line - 1; i; i--)
1448 if (line_is_blank (edit, i))
1449 break;
1452 edit_move_up (edit, edit->curs_line - i, scroll);
1455 /* move i lines */
1456 void edit_move_down (WEdit * edit, int i, int scroll)
1458 long p, l = edit->total_lines - edit->curs_line;
1460 if (i > l)
1461 i = l;
1462 if (i) {
1463 if (i > 1)
1464 edit->force |= REDRAW_PAGE;
1465 if (scroll)
1466 edit_scroll_downward (edit, i);
1467 p = edit_bol (edit, edit->curs1);
1468 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1469 edit_move_to_prev_col (edit, p);
1471 edit->search_start = edit->curs1;
1472 edit->found_len = 0;
1476 /* moves down until a blank line is reached, or until just
1477 before a non-blank line is reached */
1478 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1480 int i;
1481 if (edit->curs_line >= edit->total_lines - 1) {
1482 i = edit->total_lines;
1483 } else {
1484 if (line_is_blank (edit, edit->curs_line)) {
1485 if (line_is_blank (edit, edit->curs_line + 1)) {
1486 for (i = edit->curs_line + 1; i; i++)
1487 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1488 i--;
1489 break;
1491 } else {
1492 for (i = edit->curs_line + 1; i; i++)
1493 if (line_is_blank (edit, i) || i >= edit->total_lines)
1494 break;
1496 } else {
1497 for (i = edit->curs_line + 1; i; i++)
1498 if (line_is_blank (edit, i) || i >= edit->total_lines)
1499 break;
1502 edit_move_down (edit, i - edit->curs_line, scroll);
1505 static void edit_begin_page (WEdit *edit)
1507 edit_update_curs_row (edit);
1508 edit_move_up (edit, edit->curs_row, 0);
1511 static void edit_end_page (WEdit *edit)
1513 edit_update_curs_row (edit);
1514 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1518 /* goto beginning of text */
1519 static void edit_move_to_top (WEdit * edit)
1521 if (edit->curs_line) {
1522 edit_cursor_move (edit, -edit->curs1);
1523 edit_move_to_prev_col (edit, 0);
1524 edit->force |= REDRAW_PAGE;
1525 edit->search_start = 0;
1526 edit_update_curs_row(edit);
1531 /* goto end of text */
1532 static void edit_move_to_bottom (WEdit * edit)
1534 if (edit->curs_line < edit->total_lines) {
1535 edit_cursor_move (edit, edit->curs2);
1536 edit->start_display = edit->last_byte;
1537 edit->start_line = edit->total_lines;
1538 edit_update_curs_row(edit);
1539 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1540 edit->force |= REDRAW_PAGE;
1544 /* goto beginning of line */
1545 static void edit_cursor_to_bol (WEdit * edit)
1547 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1548 edit->search_start = edit->curs1;
1549 edit->prev_col = edit_get_col (edit);
1552 /* goto end of line */
1553 static void edit_cursor_to_eol (WEdit * edit)
1555 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1556 edit->search_start = edit->curs1;
1557 edit->prev_col = edit_get_col (edit);
1560 /* move cursor to line 'line' */
1561 void edit_move_to_line (WEdit * e, long line)
1563 if(line < e->curs_line)
1564 edit_move_up (e, e->curs_line - line, 0);
1565 else
1566 edit_move_down (e, line - e->curs_line, 0);
1567 edit_scroll_screen_over_cursor (e);
1570 /* scroll window so that first visible line is 'line' */
1571 void edit_move_display (WEdit * e, long line)
1573 if(line < e->start_line)
1574 edit_scroll_upward (e, e->start_line - line);
1575 else
1576 edit_scroll_downward (e, line - e->start_line);
1579 /* save markers onto undo stack */
1580 void edit_push_markers (WEdit * edit)
1582 edit_push_action (edit, MARK_1 + edit->mark1);
1583 edit_push_action (edit, MARK_2 + edit->mark2);
1586 void free_selections (void)
1588 int i;
1589 for (i = 0; i < NUM_SELECTION_HISTORY; i++)
1590 if (selection_history[i].text) {
1591 free (selection_history[i].text);
1592 selection_history[i].text = 0;
1593 selection_history[i].len = 0;
1595 current_selection = 0;
1598 /* return -1 on nothing to store or error, zero otherwise */
1599 void edit_get_selection (WEdit * edit)
1601 long start_mark, end_mark;
1602 if (eval_marks (edit, &start_mark, &end_mark))
1603 return;
1604 if (selection_history[current_selection].len < 4096) /* large selections should not be held -- to save memory */
1605 current_selection = (current_selection + 1) % NUM_SELECTION_HISTORY;
1606 selection_history[current_selection].len = end_mark - start_mark;
1607 if (selection_history[current_selection].text)
1608 free (selection_history[current_selection].text);
1609 selection_history[current_selection].text = malloc (selection_history[current_selection].len + 1);
1610 if (!selection_history[current_selection].text) {
1611 selection_history[current_selection].text = malloc (1);
1612 *selection_history[current_selection].text = 0;
1613 selection_history[current_selection].len = 0;
1614 } else {
1615 unsigned char *p = selection_history[current_selection].text;
1616 for (; start_mark < end_mark; start_mark++)
1617 *p++ = edit_get_byte (edit, start_mark);
1618 *p = 0;
1620 selection.text = selection_history[current_selection].text;
1621 selection.len = selection_history[current_selection].len;
1624 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1626 edit->mark1 = m1;
1627 edit->mark2 = m2;
1628 edit->column1 = c1;
1629 edit->column2 = c2;
1633 /* highlight marker toggle */
1634 void edit_mark_cmd (WEdit * edit, int unmark)
1636 edit_push_markers (edit);
1637 if (unmark) {
1638 edit_set_markers (edit, 0, 0, 0, 0);
1639 edit->force |= REDRAW_PAGE;
1640 } else {
1641 if (edit->mark2 >= 0) {
1642 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1643 edit->force |= REDRAW_PAGE;
1644 } else
1645 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1649 static unsigned long my_type_of (int c)
1651 int x, r = 0;
1652 char *p, *q;
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 void 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 void edit_right_word_move (WEdit * edit, int s)
1709 for (;;) {
1710 int c1, c2;
1711 edit_cursor_move (edit, 1);
1712 if (edit->curs1 >= edit->last_byte)
1713 break;
1714 c1 = edit_get_byte (edit, edit->curs1 - 1);
1715 c2 = edit_get_byte (edit, edit->curs1);
1716 if (!(my_type_of (c1) & my_type_of (c2)))
1717 break;
1718 if (isspace (c1) && !isspace (c2))
1719 break;
1720 if (s)
1721 if (!isspace (c1) && isspace (c2))
1722 break;
1726 static void edit_right_word_move_cmd (WEdit * edit)
1728 edit_right_word_move (edit, 0);
1729 edit->force |= REDRAW_PAGE;
1733 static void edit_right_delete_word (WEdit * edit)
1735 int c1, c2;
1736 for (;;) {
1737 if (edit->curs1 >= edit->last_byte)
1738 break;
1739 c1 = edit_delete (edit);
1740 c2 = edit_get_byte (edit, edit->curs1);
1741 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1742 break;
1743 if (!(my_type_of (c1) & my_type_of (c2)))
1744 break;
1748 static void edit_left_delete_word (WEdit * edit)
1750 int c1, c2;
1751 for (;;) {
1752 if (edit->curs1 <= 0)
1753 break;
1754 c1 = edit_backspace (edit);
1755 c2 = edit_get_byte (edit, edit->curs1 - 1);
1756 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1757 break;
1758 if (!(my_type_of (c1) & my_type_of (c2)))
1759 break;
1764 the start column position is not recorded, and hence does not
1765 undo as it happed. But who would notice.
1767 void edit_do_undo (WEdit * edit)
1769 long ac;
1770 long count = 0;
1772 push_action_disabled = 1; /* don't record undo's onto undo stack! */
1774 while ((ac = pop_action (edit)) < KEY_PRESS) {
1775 switch ((int) ac) {
1776 case STACK_BOTTOM:
1777 goto done_undo;
1778 case CURS_RIGHT:
1779 edit_cursor_move (edit, 1);
1780 break;
1781 case CURS_LEFT:
1782 edit_cursor_move (edit, -1);
1783 break;
1784 case BACKSPACE:
1785 edit_backspace (edit);
1786 break;
1787 case DELCHAR:
1788 edit_delete (edit);
1789 break;
1790 case COLUMN_ON:
1791 column_highlighting = 1;
1792 break;
1793 case COLUMN_OFF:
1794 column_highlighting = 0;
1795 break;
1797 if (ac >= 256 && ac < 512)
1798 edit_insert_ahead (edit, ac - 256);
1799 if (ac >= 0 && ac < 256)
1800 edit_insert (edit, ac);
1802 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1803 edit->mark1 = ac - MARK_1;
1804 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1805 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1806 edit->mark2 = ac - MARK_2;
1807 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1809 if (count++)
1810 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1813 if (edit->start_display > ac - KEY_PRESS) {
1814 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1815 edit->force |= REDRAW_PAGE;
1816 } else if (edit->start_display < ac - KEY_PRESS) {
1817 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1818 edit->force |= REDRAW_PAGE;
1820 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1821 edit_update_curs_row (edit);
1823 done_undo:;
1824 push_action_disabled = 0;
1827 static void edit_delete_to_line_end (WEdit * edit)
1829 while (edit_get_byte (edit, edit->curs1) != '\n') {
1830 if (!edit->curs2)
1831 break;
1832 edit_delete (edit);
1836 static void edit_delete_to_line_begin (WEdit * edit)
1838 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1839 if (!edit->curs1)
1840 break;
1841 edit_backspace (edit);
1845 void edit_delete_line (WEdit * edit)
1847 int c;
1848 do {
1849 c = edit_delete (edit);
1850 } while (c != '\n' && c);
1851 do {
1852 c = edit_backspace (edit);
1853 } while (c != '\n' && c);
1854 if (c)
1855 edit_insert (edit, '\n');
1858 static void insert_spaces_tab (WEdit * edit, int half)
1860 int i;
1861 edit_update_curs_col (edit);
1862 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1863 while (i > 0) {
1864 edit_insert (edit, ' ');
1865 i -= space_width;
1869 static int is_aligned_on_a_tab (WEdit * edit)
1871 edit_update_curs_col (edit);
1872 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1873 return 0; /* not alligned on a tab */
1874 return 1;
1877 static int right_of_four_spaces (WEdit *edit)
1879 int i, ch = 0;
1880 for (i = 1; i <= HALF_TAB_SIZE; i++)
1881 ch |= edit_get_byte (edit, edit->curs1 - i);
1882 if (ch == ' ')
1883 return is_aligned_on_a_tab (edit);
1884 return 0;
1887 static int left_of_four_spaces (WEdit *edit)
1889 int i, ch = 0;
1890 for (i = 0; i < HALF_TAB_SIZE; i++)
1891 ch |= edit_get_byte (edit, edit->curs1 + i);
1892 if (ch == ' ')
1893 return is_aligned_on_a_tab (edit);
1894 return 0;
1897 int edit_indent_width (WEdit * edit, long p)
1899 long q = p;
1900 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1901 q++;
1902 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1905 void edit_insert_indent (WEdit * edit, int indent)
1907 if (!option_fill_tabs_with_spaces) {
1908 while (indent >= TAB_SIZE) {
1909 edit_insert (edit, '\t');
1910 indent -= TAB_SIZE;
1913 while (indent-- > 0)
1914 edit_insert (edit, ' ');
1917 void edit_auto_indent (WEdit * edit, int extra, int no_advance)
1919 long p;
1920 int indent;
1921 p = edit->curs1;
1922 while (isspace (edit_get_byte (edit, p - 1)) && p > 0) /* move back/up to a line with text */
1923 p--;
1924 indent = edit_indent_width (edit, edit_bol (edit, p));
1925 if (edit->curs_col < indent && no_advance)
1926 indent = edit->curs_col;
1927 edit_insert_indent (edit, indent + (option_fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE) * space_width * extra);
1930 static void edit_double_newline (WEdit * edit)
1932 edit_insert (edit, '\n');
1933 if (edit_get_byte (edit, edit->curs1) == '\n')
1934 return;
1935 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1936 return;
1937 edit->force |= REDRAW_PAGE;
1938 edit_insert (edit, '\n');
1941 static void edit_tab_cmd (WEdit * edit)
1943 int i;
1945 if (option_fake_half_tabs) {
1946 if (is_in_indent (edit)) {
1947 /*insert a half tab (usually four spaces) unless there is a
1948 half tab already behind, then delete it and insert a
1949 full tab. */
1950 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1951 for (i = 1; i <= HALF_TAB_SIZE; i++)
1952 edit_backspace (edit);
1953 edit_insert (edit, '\t');
1954 } else {
1955 insert_spaces_tab (edit, 1);
1957 return;
1960 if (option_fill_tabs_with_spaces) {
1961 insert_spaces_tab (edit, 0);
1962 } else {
1963 edit_insert (edit, '\t');
1965 return;
1968 void format_paragraph (WEdit * edit, int force);
1970 static void check_and_wrap_line (WEdit * edit)
1972 int curs, c;
1973 if (!option_typewriter_wrap)
1974 return;
1975 edit_update_curs_col (edit);
1976 if (edit->curs_col < option_word_wrap_line_length)
1977 return;
1978 curs = edit->curs1;
1979 for (;;) {
1980 curs--;
1981 c = edit_get_byte (edit, curs);
1982 if (c == '\n' || curs <= 0) {
1983 edit_insert (edit, '\n');
1984 return;
1986 if (c == ' ' || c == '\t') {
1987 int current = edit->curs1;
1988 edit_cursor_move (edit, curs - edit->curs1 + 1);
1989 edit_insert (edit, '\n');
1990 edit_cursor_move (edit, current - edit->curs1 + 1);
1991 return;
1996 void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
1998 int edit_translate_key (WEdit * edit, unsigned int x_keycode, long x_key, int x_state, int *cmd, int *ch)
2000 int command = -1;
2001 int char_for_insertion = -1;
2003 #include "edit_key_translator.c"
2005 *cmd = command;
2006 *ch = char_for_insertion;
2008 if((command == -1 || command == 0) && char_for_insertion == -1) /* unchanged, key has no function here */
2009 return 0;
2010 return 1;
2013 void edit_push_key_press (WEdit * edit)
2015 edit_push_action (edit, KEY_PRESS + edit->start_display);
2016 if (edit->mark2 == -1)
2017 edit_push_action (edit, MARK_1 + edit->mark1);
2020 /* this find the matching bracket in either direction, and sets edit->bracket */
2021 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2023 const char * const b = "{}{[][()(", *p;
2024 int i = 1, a, inc = -1, c, d, n = 0;
2025 unsigned long j = 0;
2026 long q;
2027 edit_update_curs_row (edit);
2028 c = edit_get_byte (edit, edit->curs1);
2029 p = strchr (b, c);
2030 /* no limit */
2031 if (!furthest_bracket_search)
2032 furthest_bracket_search--;
2033 /* not on a bracket at all */
2034 if (!p)
2035 return -1;
2036 /* the matching bracket */
2037 d = p[1];
2038 /* going left or right? */
2039 if (strchr ("{[(", c))
2040 inc = 1;
2041 for (q = edit->curs1 + inc;; q += inc) {
2042 /* out of buffer? */
2043 if (q >= edit->last_byte || q < 0)
2044 break;
2045 a = edit_get_byte (edit, q);
2046 /* don't want to eat CPU */
2047 if (j++ > furthest_bracket_search)
2048 break;
2049 /* out of screen? */
2050 if (in_screen) {
2051 if (q < edit->start_display)
2052 break;
2053 /* count lines if searching downward */
2054 if (inc > 0 && a == '\n')
2055 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2056 break;
2058 /* count bracket depth */
2059 i += (a == c) - (a == d);
2060 /* return if bracket depth is zero */
2061 if (!i)
2062 return q;
2064 /* no match */
2065 return -1;
2068 static long last_bracket = -1;
2070 static void edit_find_bracket (WEdit * edit)
2072 edit->bracket = edit_get_bracket (edit, 1, 10000);
2073 if (last_bracket != edit->bracket)
2074 edit->force |= REDRAW_PAGE;
2075 last_bracket = edit->bracket;
2078 static void edit_goto_matching_bracket (WEdit *edit)
2080 long q;
2081 q = edit_get_bracket (edit, 0, 0);
2082 if (q < 0)
2083 return;
2084 edit->bracket = edit->curs1;
2085 edit->force |= REDRAW_PAGE;
2086 edit_cursor_move (edit, q - edit->curs1);
2089 /* this executes a command as though the user initiated it through a key press. */
2090 /* callback with WIDGET_KEY as a message calls this after translating the key
2091 press */
2092 /* this can be used to pass any command to the editor. Same as sendevent with
2093 msg = WIDGET_COMMAND and par = command except the screen wouldn't update */
2094 /* one of command or char_for_insertion must be passed as -1 */
2095 /* commands are executed, and char_for_insertion is inserted at the cursor */
2096 /* returns 0 if the command is a macro that was not found, 1 otherwise */
2097 int edit_execute_key_command (WEdit * edit, int command, int char_for_insertion)
2099 int r;
2100 if (command == CK_Begin_Record_Macro) {
2101 edit->macro_i = 0;
2102 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2103 return command;
2105 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2106 edit->force |= REDRAW_COMPLETELY;
2107 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2108 edit->macro_i = -1;
2109 return command;
2111 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2112 edit->macro[edit->macro_i].command = command;
2113 edit->macro[edit->macro_i++].ch = char_for_insertion;
2115 /* record the beginning of a set of editing actions initiated by a key press */
2116 if (command != CK_Undo)
2117 edit_push_key_press (edit);
2119 r = edit_execute_cmd (edit, command, char_for_insertion);
2120 if (column_highlighting)
2121 edit->force |= REDRAW_PAGE;
2123 return r;
2126 static const char * const shell_cmd[] = SHELL_COMMANDS_i
2127 void edit_mail_dialog (WEdit * edit);
2130 This executes a command at a lower level than macro recording.
2131 It also does not push a key_press onto the undo stack. This means
2132 that if it is called many times, a single undo command will undo
2133 all of them. It also does not check for the Undo command.
2134 Returns 0 if the command is a macro that was not found, 1
2135 otherwise.
2137 int edit_execute_cmd (WEdit * edit, int command, int char_for_insertion)
2139 int result = 1;
2140 edit->force |= REDRAW_LINE;
2141 if (edit->found_len || column_highlighting)
2142 /* the next key press will unhighlight the found string, so update whole page */
2143 edit->force |= REDRAW_PAGE;
2145 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2146 column_highlighting = 0;
2147 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2148 edit_mark_cmd (edit, 1); /* clear */
2149 edit_mark_cmd (edit, 0); /* marking on */
2151 edit->highlight = 1;
2152 } else { /* any other command */
2153 if (edit->highlight)
2154 edit_mark_cmd (edit, 0); /* clear */
2155 edit->highlight = 0;
2158 /* first check for undo */
2159 if (command == CK_Undo) {
2160 edit_do_undo (edit);
2161 edit->found_len = 0;
2162 edit->prev_col = edit_get_col (edit);
2163 edit->search_start = edit->curs1;
2164 return 1;
2166 /* An ordinary key press */
2167 if (char_for_insertion >= 0) {
2168 if (edit->overwrite) {
2169 if (edit_get_byte (edit, edit->curs1) != '\n')
2170 edit_delete (edit);
2172 edit_insert (edit, char_for_insertion);
2173 if (option_auto_para_formatting) {
2174 format_paragraph (edit, 0);
2175 edit->force |= REDRAW_PAGE;
2176 } else
2177 check_and_wrap_line (edit);
2178 edit->found_len = 0;
2179 edit->prev_col = edit_get_col (edit);
2180 edit->search_start = edit->curs1;
2181 edit_find_bracket (edit);
2182 edit_check_spelling (edit);
2183 return 1;
2185 switch (command) {
2186 case CK_Begin_Page:
2187 case CK_End_Page:
2188 case CK_Begin_Page_Highlight:
2189 case CK_End_Page_Highlight:
2190 case CK_Word_Left:
2191 case CK_Word_Right:
2192 case CK_Up:
2193 case CK_Down:
2194 case CK_Word_Left_Highlight:
2195 case CK_Word_Right_Highlight:
2196 case CK_Up_Highlight:
2197 case CK_Down_Highlight:
2198 if (edit->mark2 == -1)
2199 break; /*marking is following the cursor: may need to highlight a whole line */
2200 case CK_Left:
2201 case CK_Right:
2202 case CK_Left_Highlight:
2203 case CK_Right_Highlight:
2204 edit->force |= REDRAW_CHAR_ONLY;
2207 /* basic cursor key commands */
2208 switch (command) {
2209 case CK_BackSpace:
2210 if (option_backspace_through_tabs && is_in_indent (edit)) {
2211 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2212 && edit->curs1 > 0)
2213 edit_backspace (edit);
2214 break;
2215 } else {
2216 if (option_fake_half_tabs) {
2217 int i;
2218 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2219 for (i = 0; i < HALF_TAB_SIZE; i++)
2220 edit_backspace (edit);
2221 break;
2225 edit_backspace (edit);
2226 break;
2227 case CK_Delete:
2228 if (option_fake_half_tabs) {
2229 int i;
2230 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2231 for (i = 1; i <= HALF_TAB_SIZE; i++)
2232 edit_delete (edit);
2233 break;
2236 edit_delete (edit);
2237 break;
2238 case CK_Delete_Word_Left:
2239 edit_left_delete_word (edit);
2240 break;
2241 case CK_Delete_Word_Right:
2242 edit_right_delete_word (edit);
2243 break;
2244 case CK_Delete_Line:
2245 edit_delete_line (edit);
2246 break;
2247 case CK_Delete_To_Line_End:
2248 edit_delete_to_line_end (edit);
2249 break;
2250 case CK_Delete_To_Line_Begin:
2251 edit_delete_to_line_begin (edit);
2252 break;
2253 case CK_Enter:
2254 if (option_auto_para_formatting) {
2255 edit_double_newline (edit);
2256 if (option_return_does_auto_indent)
2257 edit_auto_indent (edit, 0, 1);
2258 format_paragraph (edit, 0);
2259 } else {
2260 edit_insert (edit, '\n');
2261 if (option_return_does_auto_indent) {
2262 edit_auto_indent (edit, 0, 1);
2265 break;
2266 case CK_Return:
2267 edit_insert (edit, '\n');
2268 break;
2270 case CK_Page_Up:
2271 case CK_Page_Up_Highlight:
2272 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2273 break;
2274 case CK_Page_Down:
2275 case CK_Page_Down_Highlight:
2276 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2277 break;
2278 case CK_Left:
2279 case CK_Left_Highlight:
2280 if (option_fake_half_tabs) {
2281 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2282 edit_cursor_move (edit, -HALF_TAB_SIZE);
2283 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2284 break;
2287 edit_cursor_move (edit, -1);
2288 break;
2289 case CK_Right:
2290 case CK_Right_Highlight:
2291 if (option_fake_half_tabs) {
2292 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2293 edit_cursor_move (edit, HALF_TAB_SIZE);
2294 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2295 break;
2298 edit_cursor_move (edit, 1);
2299 break;
2300 case CK_Begin_Page:
2301 case CK_Begin_Page_Highlight:
2302 edit_begin_page (edit);
2303 break;
2304 case CK_End_Page:
2305 case CK_End_Page_Highlight:
2306 edit_end_page (edit);
2307 break;
2308 case CK_Word_Left:
2309 case CK_Word_Left_Highlight:
2310 edit_left_word_move_cmd (edit);
2311 break;
2312 case CK_Word_Right:
2313 case CK_Word_Right_Highlight:
2314 edit_right_word_move_cmd (edit);
2315 break;
2316 case CK_Up:
2317 case CK_Up_Highlight:
2318 edit_move_up (edit, 1, 0);
2319 break;
2320 case CK_Down:
2321 case CK_Down_Highlight:
2322 edit_move_down (edit, 1, 0);
2323 break;
2324 case CK_Paragraph_Up:
2325 case CK_Paragraph_Up_Highlight:
2326 edit_move_up_paragraph (edit, 0);
2327 break;
2328 case CK_Paragraph_Down:
2329 case CK_Paragraph_Down_Highlight:
2330 edit_move_down_paragraph (edit, 0);
2331 break;
2332 case CK_Scroll_Up:
2333 case CK_Scroll_Up_Highlight:
2334 edit_move_up (edit, 1, 1);
2335 break;
2336 case CK_Scroll_Down:
2337 case CK_Scroll_Down_Highlight:
2338 edit_move_down (edit, 1, 1);
2339 break;
2340 case CK_Home:
2341 case CK_Home_Highlight:
2342 edit_cursor_to_bol (edit);
2343 break;
2344 case CK_End:
2345 case CK_End_Highlight:
2346 edit_cursor_to_eol (edit);
2347 break;
2349 case CK_Tab:
2350 edit_tab_cmd (edit);
2351 if (option_auto_para_formatting) {
2352 format_paragraph (edit, 0);
2353 edit->force |= REDRAW_PAGE;
2354 } else
2355 check_and_wrap_line (edit);
2356 break;
2358 case CK_Toggle_Insert:
2359 edit->overwrite = (edit->overwrite == 0);
2360 break;
2362 case CK_Mark:
2363 if (edit->mark2 >= 0) {
2364 if (column_highlighting)
2365 edit_push_action (edit, COLUMN_ON);
2366 column_highlighting = 0;
2368 edit_mark_cmd (edit, 0);
2369 break;
2370 case CK_Column_Mark:
2371 if (!column_highlighting)
2372 edit_push_action (edit, COLUMN_OFF);
2373 column_highlighting = 1;
2374 edit_mark_cmd (edit, 0);
2375 break;
2376 case CK_Unmark:
2377 if (column_highlighting)
2378 edit_push_action (edit, COLUMN_ON);
2379 column_highlighting = 0;
2380 edit_mark_cmd (edit, 1);
2381 break;
2383 case CK_Toggle_Bookmark:
2384 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2385 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2386 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2387 else
2388 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2389 break;
2390 case CK_Flush_Bookmarks:
2391 book_mark_flush (edit, BOOK_MARK_COLOR);
2392 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2393 edit->force |= REDRAW_PAGE;
2394 break;
2395 case CK_Next_Bookmark:
2396 if (edit->book_mark) {
2397 struct _book_mark *p;
2398 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
2399 if (p->next) {
2400 p = p->next;
2401 if (p->line >= edit->start_line + edit->num_widget_lines || p->line < edit->start_line)
2402 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
2403 edit_move_to_line (edit, p->line);
2406 break;
2407 case CK_Prev_Bookmark:
2408 if (edit->book_mark) {
2409 struct _book_mark *p;
2410 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
2411 while (p->line == edit->curs_line)
2412 if (p->prev)
2413 p = p->prev;
2414 if (p->line >= 0) {
2415 if (p->line >= edit->start_line + edit->num_widget_lines || p->line < edit->start_line)
2416 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
2417 edit_move_to_line (edit, p->line);
2420 break;
2422 case CK_Beginning_Of_Text:
2423 case CK_Beginning_Of_Text_Highlight:
2424 edit_move_to_top (edit);
2425 break;
2426 case CK_End_Of_Text:
2427 case CK_End_Of_Text_Highlight:
2428 edit_move_to_bottom (edit);
2429 break;
2431 case CK_Copy:
2432 edit_block_copy_cmd (edit);
2433 break;
2434 case CK_Remove:
2435 edit_block_delete_cmd (edit);
2436 break;
2437 case CK_Move:
2438 edit_block_move_cmd (edit);
2439 break;
2441 case CK_XStore:
2442 edit_copy_to_X_buf_cmd (edit);
2443 break;
2444 case CK_XCut:
2445 edit_cut_to_X_buf_cmd (edit);
2446 break;
2447 case CK_XPaste:
2448 edit_paste_from_X_buf_cmd (edit);
2449 break;
2450 case CK_Selection_History:
2451 edit_paste_from_history (edit);
2452 break;
2454 case CK_Save_As:
2455 edit_save_as_cmd (edit);
2456 break;
2457 case CK_Save:
2458 edit_save_confirm_cmd (edit);
2459 break;
2460 case CK_Load:
2461 edit_load_cmd (edit);
2462 break;
2463 case CK_Save_Block:
2464 edit_save_block_cmd (edit);
2465 break;
2466 case CK_Insert_File:
2467 edit_insert_file_cmd (edit);
2468 break;
2470 case CK_Find:
2471 edit_search_cmd (edit, 0);
2472 break;
2473 case CK_Find_Again:
2474 edit_search_cmd (edit, 1);
2475 break;
2476 case CK_Replace:
2477 edit_replace_cmd (edit, 0);
2478 break;
2479 case CK_Replace_Again:
2480 edit_replace_cmd (edit, 1);
2481 break;
2482 case CK_Complete_Word:
2483 edit_complete_word_cmd (edit);
2484 break;
2486 case CK_Exit:
2487 edit_quit_cmd (edit);
2488 break;
2489 case CK_New:
2490 edit_new_cmd (edit);
2491 break;
2493 case CK_Help:
2494 edit_help_cmd (edit);
2495 break;
2497 case CK_Refresh:
2498 edit_refresh_cmd (edit);
2499 break;
2501 case CK_Date:{
2502 time_t t;
2503 #ifdef HAVE_STRFTIME
2504 char s[1024];
2505 static const char time_format[] = "%c";
2506 #endif
2507 time (&t);
2508 #ifdef HAVE_STRFTIME
2509 strftime (s, sizeof (s), time_format, localtime (&t));
2510 edit_print_string (edit, s);
2511 #else
2512 edit_print_string (edit, ctime (&t));
2513 #endif
2514 edit->force |= REDRAW_PAGE;
2515 break;
2517 case CK_Goto:
2518 edit_goto_cmd (edit);
2519 break;
2520 case CK_Paragraph_Format:
2521 format_paragraph (edit, 1);
2522 edit->force |= REDRAW_PAGE;
2523 break;
2524 case CK_Delete_Macro:
2525 edit_delete_macro_cmd (edit);
2526 break;
2527 case CK_Match_Bracket:
2528 edit_goto_matching_bracket (edit);
2529 break;
2530 case CK_User_Menu:
2531 if (edit_one_file) {
2532 message (1, MSG_ERROR, _("User menu available only in mcedit invoked from mc"));
2533 break;
2535 else
2536 user_menu (edit);
2537 break;
2538 case CK_Sort:
2539 edit_sort_cmd (edit);
2540 break;
2541 case CK_Mail:
2542 edit_mail_dialog (edit);
2543 break;
2544 case CK_Shell:
2545 view_other_cmd ();
2546 break;
2548 /* These commands are not handled and must be handled by the user application */
2549 #if 0
2550 case CK_Sort:
2551 case CK_Mail:
2552 case CK_Find_File:
2553 case CK_Ctags:
2554 case CK_Terminal:
2555 case CK_Terminal_App:
2556 #endif
2557 case CK_Complete:
2558 case CK_Cancel:
2559 case CK_Save_Desktop:
2560 case CK_New_Window:
2561 case CK_Cycle:
2562 case CK_Save_And_Quit:
2563 case CK_Check_Save_And_Quit:
2564 case CK_Run_Another:
2565 case CK_Debug_Start:
2566 case CK_Debug_Stop:
2567 case CK_Debug_Toggle_Break:
2568 case CK_Debug_Clear:
2569 case CK_Debug_Next:
2570 case CK_Debug_Step:
2571 case CK_Debug_Back_Trace:
2572 case CK_Debug_Continue:
2573 case CK_Debug_Enter_Command:
2574 case CK_Debug_Until_Curser:
2575 result = 0;
2576 break;
2577 case CK_Menu:
2578 result = 0;
2579 break;
2582 /* CK_Pipe_Block */
2583 if ((command / 1000) == 1) /* a shell command */
2584 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2585 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2586 struct macro m[MAX_MACRO_LENGTH];
2587 int nm;
2588 if ((result = edit_load_macro_cmd (edit, m, &nm, command - 2000)))
2589 edit_execute_macro (edit, m, nm);
2592 /* keys which must set the col position, and the search vars */
2593 switch (command) {
2594 case CK_Find:
2595 case CK_Find_Again:
2596 case CK_Replace:
2597 case CK_Replace_Again:
2598 case CK_Complete_Word:
2599 edit->prev_col = edit_get_col (edit);
2600 return 1;
2601 break;
2602 case CK_Up:
2603 case CK_Up_Highlight:
2604 case CK_Down:
2605 case CK_Down_Highlight:
2606 case CK_Page_Up:
2607 case CK_Page_Up_Highlight:
2608 case CK_Page_Down:
2609 case CK_Page_Down_Highlight:
2610 case CK_Beginning_Of_Text:
2611 case CK_Beginning_Of_Text_Highlight:
2612 case CK_End_Of_Text:
2613 case CK_End_Of_Text_Highlight:
2614 case CK_Paragraph_Up:
2615 case CK_Paragraph_Up_Highlight:
2616 case CK_Paragraph_Down:
2617 case CK_Paragraph_Down_Highlight:
2618 case CK_Scroll_Up:
2619 case CK_Scroll_Up_Highlight:
2620 case CK_Scroll_Down:
2621 case CK_Scroll_Down_Highlight:
2622 edit->search_start = edit->curs1;
2623 edit->found_len = 0;
2624 edit_find_bracket (edit);
2625 edit_check_spelling (edit);
2626 return 1;
2627 break;
2628 default:
2629 edit->found_len = 0;
2630 edit->prev_col = edit_get_col (edit);
2631 edit->search_start = edit->curs1;
2633 edit_find_bracket (edit);
2634 edit_check_spelling (edit);
2636 if (option_auto_para_formatting) {
2637 switch (command) {
2638 case CK_BackSpace:
2639 case CK_Delete:
2640 case CK_Delete_Word_Left:
2641 case CK_Delete_Word_Right:
2642 case CK_Delete_To_Line_End:
2643 case CK_Delete_To_Line_Begin:
2644 format_paragraph (edit, 0);
2645 edit->force |= REDRAW_PAGE;
2648 return result;
2652 /* either command or char_for_insertion must be passed as -1 */
2653 /* returns 0 if command is a macro that was not found, 1 otherwise */
2654 int edit_execute_command (WEdit * edit, int command, int char_for_insertion)
2656 int r;
2657 r = edit_execute_cmd (edit, command, char_for_insertion);
2658 edit_update_screen (edit);
2659 return r;
2662 void edit_execute_macro (WEdit * edit, struct macro macro[], int n)
2664 int i = 0;
2665 edit->force |= REDRAW_PAGE;
2666 for (; i < n; i++) {
2667 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2669 edit_update_screen (edit);
2672 /* User edit menu, like user menu (F2) but only in editor. */
2673 void
2674 user_menu (WEdit * edit)
2676 FILE *fd;
2677 int nomark;
2678 struct stat status;
2679 long start_mark, end_mark;
2680 char *block_file = catstrs (home_dir, BLOCK_FILE, 0);
2681 int rc = 0;
2683 nomark = eval_marks (edit, &start_mark, &end_mark);
2684 if (!nomark) /* remember marked or not */
2685 edit_save_block (edit, block_file, start_mark, end_mark);
2687 /* run shell scripts from menu */
2688 user_menu_cmd (edit);
2690 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2691 /* no block messages */
2692 return;
2695 if (!nomark) {
2696 /* i.e. we have marked block */
2697 rc = edit_block_delete_cmd (edit);
2700 if (!rc) {
2701 edit_insert_file (edit, block_file);
2704 /* truncate block file */
2705 if ((fd = fopen (block_file, "w"))) {
2706 fclose (fd);
2709 edit_refresh_cmd (edit);
2710 edit->force |= REDRAW_COMPLETELY;
2711 return;