translated some forgotten messages...
[midnight-commander.git] / edit / edit.c
blob60841f773a5bb202da22efb4b9e0060923f80c96
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 #include "edit.h"
28 #ifdef HAVE_CHARSET
29 #include "src/charsets.h"
30 #include "src/selcodepage.h"
31 #endif
34 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
35 or EDIT_KEY_EMULATION_EMACS
37 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
39 int option_word_wrap_line_length = 72;
40 int option_typewriter_wrap = 0;
41 int option_auto_para_formatting = 0;
42 int option_tab_spacing = 8;
43 int option_fill_tabs_with_spaces = 0;
44 int option_return_does_auto_indent = 1;
45 int option_backspace_through_tabs = 0;
46 int option_fake_half_tabs = 1;
47 int option_save_mode = 0;
48 int option_backup_ext_int = -1;
49 int option_max_undo = 32768;
51 int option_edit_right_extreme = 0;
52 int option_edit_left_extreme = 0;
53 int option_edit_top_extreme = 0;
54 int option_edit_bottom_extreme = 0;
56 char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
57 char *option_backup_ext = "~";
59 static struct selection selection;
60 static int current_selection = 0;
61 /* Note: selection.text = selection_history[current_selection].text */
62 static struct selection selection_history[NUM_SELECTION_HISTORY];
64 static char *option_chars_move_whole_word =
65 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
69 * here's a quick sketch of the layout: (don't run this through indent.)
71 * (b1 is buffers1 and b2 is buffers2)
73 * |
74 * \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
75 * ______________________________________|______________________________________
76 * |
77 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
78 * |-> |-> |-> |-> |-> |-> |
79 * |
80 * _<------------------------->|<----------------->_
81 * WEdit->curs2 | WEdit->curs1
82 * ^ | ^
83 * | ^|^ |
84 * cursor ||| cursor
85 * |||
86 * file end|||file beginning
87 * |
88 * |
90 * _
91 * This_is_some_file
92 * fin.
98 returns a byte from any location in the file.
99 Returns '\n' if out of bounds.
102 static int push_action_disabled = 0;
104 #ifndef NO_INLINE_GETBYTE
106 int edit_get_byte (WEdit * edit, long byte_index)
108 unsigned long p;
109 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
110 return '\n';
112 if (byte_index >= edit->curs1) {
113 p = edit->curs1 + edit->curs2 - byte_index - 1;
114 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
115 } else {
116 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
120 #endif
122 char *edit_get_buffer_as_text (WEdit * e)
124 int l, i;
125 char *t;
126 l = e->curs1 + e->curs2;
127 t = CMalloc (l + 1);
128 for (i = 0; i < l; i++)
129 t[i] = edit_get_byte (e, i);
130 t[l] = 0;
131 return t;
136 The edit_open_file (previously edit_load_file) function uses
137 init_dynamic_edit_buffers to load a file. This is unnecessary
138 since you can just as well fopen the file and insert the
139 characters one by one. The real reason for
140 init_dynamic_edit_buffers (besides allocating the buffers) is
141 as an optimisation - it uses raw block reads and inserts large
142 chunks at a time. It is hence extremely fast at loading files.
143 Where we might not want to use it is if we were doing
144 CRLF->LF translation or if we were reading from a pipe.
147 /* Initialisation routines */
149 /* returns 1 on error */
150 /* loads file OR text into buffers. Only one must be none-NULL. */
151 /* cursor set to start of file */
152 int init_dynamic_edit_buffers (WEdit * edit, const char *filename, const char *text)
154 long buf;
155 int j, file = -1, buf2;
157 for (j = 0; j <= MAXBUFF; j++) {
158 edit->buffers1[j] = NULL;
159 edit->buffers2[j] = NULL;
162 if (filename)
163 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
164 /* The file-name is printed after the ':' */
165 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename, " ", 0)));
166 return 1;
168 edit->curs2 = edit->last_byte;
170 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
172 edit->buffers2[buf2] = CMalloc (EDIT_BUF_SIZE);
174 if (filename) {
175 mc_read (file, (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE);
176 } else {
177 memcpy (edit->buffers2[buf2] + EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE), text, edit->curs2 & M_EDIT_BUF_SIZE);
178 text += edit->curs2 & M_EDIT_BUF_SIZE;
181 for (buf = buf2 - 1; buf >= 0; buf--) {
182 edit->buffers2[buf] = CMalloc (EDIT_BUF_SIZE);
183 if (filename) {
184 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
185 } else {
186 memcpy (edit->buffers2[buf], text, EDIT_BUF_SIZE);
187 text += EDIT_BUF_SIZE;
191 edit->curs1 = 0;
192 if (file != -1)
193 mc_close (file);
194 return 0;
197 /* detecting an error on save is easy: just check if every byte has been written. */
198 /* detecting an error on read, is not so easy 'cos there is not way to tell
199 whether you read everything or not. */
200 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
201 static const struct edit_filters {
202 char *read, *write, *extension;
203 } all_filters[] = {
206 "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2"
209 "gzip -cd %s 2>&1", "gzip > %s", ".gz"
212 "gzip -cd %s 2>&1", "gzip > %s", ".Z"
216 static int edit_find_filter (const char *filename)
218 int i, l;
219 if (!filename)
220 return -1;
221 l = strlen (filename);
222 for (i = 0; i < sizeof (all_filters) / sizeof (struct edit_filters); i++) {
223 int e;
224 e = strlen (all_filters[i].extension);
225 if (l > e)
226 if (!strcmp (all_filters[i].extension, filename + l - e))
227 return i;
229 return -1;
232 char *edit_get_filter (const char *filename)
234 int i, l;
235 char *p;
236 i = edit_find_filter (filename);
237 if (i < 0)
238 return 0;
239 l = strlen (filename);
240 p = malloc (strlen (all_filters[i].read) + l + 2);
241 sprintf (p, all_filters[i].read, filename);
242 return p;
245 char *edit_get_write_filter (char *writename, const char *filename)
247 int i, l;
248 char *p;
249 i = edit_find_filter (filename);
250 if (i < 0)
251 return 0;
252 l = strlen (writename);
253 p = malloc (strlen (all_filters[i].write) + l + 2);
254 sprintf (p, all_filters[i].write, writename);
255 return p;
258 long edit_insert_stream (WEdit * edit, FILE * f)
260 int c;
261 long i = 0;
262 while ((c = fgetc (f)) >= 0) {
263 edit_insert (edit, c);
264 i++;
266 return i;
268 long edit_write_stream (WEdit * edit, FILE * f)
270 long i;
271 for (i = 0; i < edit->last_byte; i++)
272 if (fputc (edit_get_byte (edit, i), f) < 0)
273 break;
274 return i;
277 #define TEMP_BUF_LEN 1024
279 /* inserts a file at the cursor, returns 1 on success */
280 int edit_insert_file (WEdit * edit, const char *filename)
282 char *p;
283 if ((p = edit_get_filter (filename))) {
284 FILE *f;
285 long current = edit->curs1;
286 f = (FILE *) popen (p, "r");
287 if (f) {
288 edit_insert_stream (edit, f);
289 edit_cursor_move (edit, current - edit->curs1);
290 if (pclose (f) > 0) {
291 edit_error_dialog (_ (" Error "), catstrs (_ (" Error reading from pipe: "), p, " ", 0));
292 free (p);
293 return 0;
295 } else {
296 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open pipe for reading: "), p, " ", 0)));
297 free (p);
298 return 0;
300 free (p);
301 } else {
302 int i, file, blocklen;
303 long current = edit->curs1;
304 unsigned char *buf;
305 if ((file = mc_open (filename, O_RDONLY | O_BINARY )) == -1)
306 return 0;
307 buf = malloc (TEMP_BUF_LEN);
308 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
309 for (i = 0; i < blocklen; i++)
310 edit_insert (edit, buf[i]);
312 edit_cursor_move (edit, current - edit->curs1);
313 free (buf);
314 mc_close (file);
315 if (blocklen)
316 return 0;
318 return 1;
321 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
322 static int check_file_access (WEdit *edit, const char *filename, struct stat *st)
324 int file;
325 int stat_ok = 0;
327 /* Try stat first to prevent getting stuck on pipes */
328 if (mc_stat ((char *) filename, st) == 0) {
329 stat_ok = 1;
332 /* Only regular files are allowed */
333 if (stat_ok && !S_ISREG (st->st_mode)) {
334 edit_error_dialog (_ (" Error "), catstrs (_ (" Not an ordinary file: "), filename, " ", 0));
335 return 1;
338 /* Open the file, create it if needed */
339 if ((file = mc_open (filename, O_RDONLY | O_CREAT | O_BINARY, 0666)) < 0) {
340 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename, " ", 0)));
341 return 1;
344 /* If the file has just been created, we don't have valid stat yet, so do it now */
345 if (!stat_ok && mc_fstat (file, st) < 0) {
346 mc_close (file);
347 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Cannot get size/permissions info on file: "), filename, " ", 0)));
348 return 1;
350 mc_close (file);
352 /* If it's a new file, delete it if it's not modified or saved */
353 if (!stat_ok && st->st_size == 0) {
354 edit->delete_file = 1;
357 if (st->st_size >= SIZE_LIMIT) {
358 /* The file-name is printed after the ':' */
359 edit_error_dialog (_ (" Error "), catstrs (_ (" File is too large: "), \
360 filename, _ (" \n Increase edit.h:MAXBUF and recompile the editor. "), 0));
361 return 1;
363 return 0;
366 /* returns 1 on error */
367 int edit_open_file (WEdit * edit, const char *filename, const char *text, unsigned long text_size)
369 struct stat st;
370 if (text) {
371 edit->last_byte = text_size;
372 filename = 0;
373 } else {
374 int r;
375 r = check_file_access (edit, filename, &st);
376 if (r)
377 return 1;
378 edit->stat1 = st;
379 #ifndef CR_LF_TRANSLATION
380 edit->last_byte = st.st_size;
381 #else
382 /* going to read the file into the buffer later byte by byte */
383 edit->last_byte = 0;
384 filename = 0;
385 text = "";
386 #endif
388 return init_dynamic_edit_buffers (edit, filename, text);
391 #define space_width 1
393 /* fills in the edit struct. returns 0 on fail. Pass edit as NULL for this */
394 WEdit *edit_init (WEdit * edit, int lines, int columns, const char *filename, const char *text, const char *dir, unsigned long text_size)
396 const char *f;
397 int to_free = 0;
398 int use_filter = 0;
400 if (!edit) {
401 #ifdef ENABLE_NLS
403 * Expand option_whole_chars_search by national letters using
404 * current locale
407 static char option_whole_chars_search_buf [256];
409 if (option_whole_chars_search_buf != option_whole_chars_search) {
410 int i;
411 int len = strlen (option_whole_chars_search);
413 strcpy (option_whole_chars_search_buf, option_whole_chars_search);
415 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
416 if (islower (i) && !strchr (option_whole_chars_search, i)) {
417 option_whole_chars_search_buf [len++] = i;
421 option_whole_chars_search_buf [len] = 0;
422 option_whole_chars_search = option_whole_chars_search_buf;
424 #endif /* ENABLE_NLS */
425 edit = g_malloc (sizeof (WEdit));
426 memset (edit, 0, sizeof (WEdit));
427 to_free = 1;
429 memset (&(edit->from_here), 0, (unsigned long)&(edit->to_here) - (unsigned long)&(edit->from_here));
430 edit->num_widget_lines = lines;
431 edit->num_widget_columns = columns;
432 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
433 edit->stat1.st_uid = getuid ();
434 edit->stat1.st_gid = getgid ();
435 edit->bracket = -1;
436 if (!dir)
437 dir = "";
438 f = filename;
439 if (filename) {
440 f = catstrs (dir, filename, 0);
442 if (edit_find_filter (f) < 0) {
443 #ifdef CR_LF_TRANSLATION
444 use_filter = 1;
445 #endif
446 if (edit_open_file (edit, f, text, text_size)) {
447 /* edit_load_file already gives an error message */
448 if (to_free)
449 g_free (edit);
450 return 0;
452 } else {
453 use_filter = 1;
454 if (edit_open_file (edit, 0, "", 0)) {
455 if (to_free)
456 g_free (edit);
457 return 0;
460 edit->force |= REDRAW_PAGE;
461 if (filename) {
462 filename = catstrs (dir, filename, 0);
463 edit_split_filename (edit, filename);
464 } else {
465 edit->filename = (char *) strdup ("");
466 edit->dir = (char *) strdup (dir);
468 edit->stack_size = START_STACK_SIZE;
469 edit->stack_size_mask = START_STACK_SIZE - 1;
470 edit->undo_stack = malloc ((edit->stack_size + 10) * sizeof (long));
471 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
472 if (use_filter) {
473 push_action_disabled = 1;
474 if (check_file_access (edit, filename, &(edit->stat1))
475 || !edit_insert_file (edit, f))
477 edit_clean (edit);
478 if (to_free)
479 g_free (edit);
480 return 0;
482 /* FIXME: this should be an unmodification() function */
483 push_action_disabled = 0;
485 edit->modified = 0;
486 edit_load_syntax (edit, 0, 0);
488 int color;
489 edit_get_syntax_color (edit, -1, &color);
491 return edit;
494 /* clear the edit struct, freeing everything in it. returns 1 on success */
495 int edit_clean (WEdit * edit)
497 if (edit) {
498 int j = 0;
499 edit_free_syntax_rules (edit);
500 book_mark_flush (edit, -1);
501 for (; j <= MAXBUFF; j++) {
502 if (edit->buffers1[j] != NULL)
503 free (edit->buffers1[j]);
504 if (edit->buffers2[j] != NULL)
505 free (edit->buffers2[j]);
508 if (edit->undo_stack)
509 free (edit->undo_stack);
510 if (edit->filename)
511 free (edit->filename);
512 if (edit->dir)
513 free (edit->dir);
514 /* we don't want to clear the widget */
515 memset (&(edit->from_here), 0, (unsigned long)&(edit->to_here) - (unsigned long)&(edit->from_here));
517 /* Free temporary strings used in catstrs() */
518 freestrs();
520 return 1;
522 return 0;
526 /* returns 1 on success */
527 int edit_renew (WEdit * edit)
529 int lines = edit->num_widget_lines;
530 int columns = edit->num_widget_columns;
531 char *dir;
532 int retval = 1;
534 if (edit->dir)
535 dir = (char *) strdup (edit->dir);
536 else
537 dir = 0;
539 edit_clean (edit);
540 if (!edit_init (edit, lines, columns, 0, "", dir, 0))
541 retval = 0;
542 if (dir)
543 free (dir);
544 return retval;
547 /* returns 1 on success, if returns 0, the edit struct would have been free'd */
548 int edit_reload (WEdit * edit, const char *filename, const char *text, const char *dir, unsigned long text_size)
550 WEdit *e;
551 int lines = edit->num_widget_lines;
552 int columns = edit->num_widget_columns;
553 e = g_malloc (sizeof (WEdit));
554 memset (e, 0, sizeof (WEdit));
555 e->widget = edit->widget;
556 e->macro_i = -1;
557 if (!edit_init (e, lines, columns, filename, text, dir, text_size)) {
558 g_free (e);
559 return 0;
561 edit_clean (edit);
562 memcpy (edit, e, sizeof (WEdit));
563 g_free (e);
564 return 1;
569 Recording stack for undo:
570 The following is an implementation of a compressed stack. Identical
571 pushes are recorded by a negative prefix indicating the number of times the
572 same char was pushed. This saves space for repeated curs-left or curs-right
573 delete etc.
577 pushed: stored:
581 b -3
583 c --> -4
589 If the stack long int is 0-255 it represents a normal insert (from a backspace),
590 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
591 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
592 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
593 position.
595 The only way the cursor moves or the buffer is changed is through the routines:
596 insert, backspace, insert_ahead, delete, and cursor_move.
597 These record the reverse undo movements onto the stack each time they are
598 called.
600 Each key press results in a set of actions (insert; delete ...). So each time
601 a key is pressed the current position of start_display is pushed as
602 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
603 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
604 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
608 void edit_push_action (WEdit * edit, long c,...)
610 unsigned long sp = edit->stack_pointer;
611 unsigned long spm1;
612 long *t;
613 /* first enlarge the stack if necessary */
614 if (sp > edit->stack_size - 10) { /* say */
615 if (option_max_undo < 256)
616 option_max_undo = 256;
617 if (edit->stack_size < option_max_undo) {
618 t = malloc ((edit->stack_size * 2 + 10) * sizeof (long));
619 if (t) {
620 memcpy (t, edit->undo_stack, sizeof (long) * edit->stack_size);
621 free (edit->undo_stack);
622 edit->undo_stack = t;
623 edit->stack_size <<= 1;
624 edit->stack_size_mask = edit->stack_size - 1;
628 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
629 if (push_action_disabled)
630 return;
632 #ifdef FAST_MOVE_CURSOR
633 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
634 va_list ap;
635 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
636 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
637 va_start (ap, c);
638 c = -(va_arg (ap, int));
639 va_end (ap);
640 } else
641 #endif /* ! FAST_MOVE_CURSOR */
642 if (edit->stack_bottom != sp
643 && spm1 != edit->stack_bottom
644 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
645 int d;
646 if (edit->undo_stack[spm1] < 0) {
647 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
648 if (d == c) {
649 if (edit->undo_stack[spm1] > -1000000000) {
650 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
651 edit->undo_stack[spm1]--;
652 return;
655 /* #define NO_STACK_CURSMOVE_ANIHILATION */
656 #ifndef NO_STACK_CURSMOVE_ANIHILATION
657 else if ((c == CURS_LEFT && d == CURS_RIGHT)
658 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
659 if (edit->undo_stack[spm1] == -2)
660 edit->stack_pointer = spm1;
661 else
662 edit->undo_stack[spm1]++;
663 return;
665 #endif
666 } else {
667 d = edit->undo_stack[spm1];
668 if (d == c) {
669 if (c >= KEY_PRESS)
670 return; /* --> no need to push multiple do-nothings */
671 edit->undo_stack[sp] = -2;
672 goto check_bottom;
674 #ifndef NO_STACK_CURSMOVE_ANIHILATION
675 else if ((c == CURS_LEFT && d == CURS_RIGHT)
676 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
677 edit->stack_pointer = spm1;
678 return;
680 #endif
683 edit->undo_stack[sp] = c;
684 check_bottom:
686 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
688 /*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" */
689 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
690 if (c == edit->stack_bottom || ((c + 1) & edit->stack_size_mask) == edit->stack_bottom)
691 do {
692 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
693 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
695 /*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: */
696 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
697 edit->stack_bottom = edit->stack_pointer = 0;
701 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
702 then the file should be as it was when he loaded up. Then set edit->modified to 0.
704 long pop_action (WEdit * edit)
706 long c;
707 unsigned long sp = edit->stack_pointer;
708 if (sp == edit->stack_bottom) {
709 return STACK_BOTTOM;
711 sp = (sp - 1) & edit->stack_size_mask;
712 if ((c = edit->undo_stack[sp]) >= 0) {
713 /* edit->undo_stack[sp] = '@'; */
714 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
715 return c;
717 if (sp == edit->stack_bottom) {
718 return STACK_BOTTOM;
720 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
721 if (edit->undo_stack[sp] == -2) {
722 /* edit->undo_stack[sp] = '@'; */
723 edit->stack_pointer = sp;
724 } else
725 edit->undo_stack[sp]++;
727 return c;
730 /* is called whenever a modification is made by one of the four routines below */
731 static inline void edit_modification (WEdit * edit)
733 edit->caches_valid = 0;
734 edit->modified = 1;
735 edit->screen_modified = 1;
739 Basic low level single character buffer alterations and movements at the cursor.
740 Returns char passed over, inserted or removed.
743 void edit_insert (WEdit * edit, int c)
745 /* check if file has grown to large */
746 if (edit->last_byte >= SIZE_LIMIT)
747 return;
749 /* first we must update the position of the display window */
750 if (edit->curs1 < edit->start_display) {
751 edit->start_display++;
752 if (c == '\n')
753 edit->start_line++;
755 /* now we must update some info on the file and check if a redraw is required */
756 if (c == '\n') {
757 if (edit->book_mark)
758 book_mark_inc (edit, edit->curs_line);
759 edit->curs_line++;
760 edit->total_lines++;
761 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
763 /* tell that we've modified the file */
764 edit_modification (edit);
766 /* save the reverse command onto the undo stack */
767 edit_push_action (edit, BACKSPACE);
769 /* update markers */
770 edit->mark1 += (edit->mark1 > edit->curs1);
771 edit->mark2 += (edit->mark2 > edit->curs1);
772 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
774 /* add a new buffer if we've reached the end of the last one */
775 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
776 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
778 /* perfprm the insertion */
779 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = (unsigned char) c;
781 /* update file length */
782 edit->last_byte++;
784 /* update cursor position */
785 edit->curs1++;
789 /* same as edit_insert and move left */
790 void edit_insert_ahead (WEdit * edit, int c)
792 if (edit->last_byte >= SIZE_LIMIT)
793 return;
794 if (edit->curs1 < edit->start_display) {
795 edit->start_display++;
796 if (c == '\n')
797 edit->start_line++;
799 if (c == '\n') {
800 if (edit->book_mark)
801 book_mark_inc (edit, edit->curs_line);
802 edit->total_lines++;
803 edit->force |= REDRAW_AFTER_CURSOR;
805 edit_modification (edit);
806 edit_push_action (edit, DELCHAR);
808 edit->mark1 += (edit->mark1 >= edit->curs1);
809 edit->mark2 += (edit->mark2 >= edit->curs1);
810 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
812 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
813 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
814 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
816 edit->last_byte++;
817 edit->curs2++;
821 int edit_delete (WEdit * edit)
823 int p;
824 if (!edit->curs2)
825 return 0;
827 edit->mark1 -= (edit->mark1 > edit->curs1);
828 edit->mark2 -= (edit->mark2 > edit->curs1);
829 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
831 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
833 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
834 free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
835 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
837 edit->last_byte--;
838 edit->curs2--;
840 if (p == '\n') {
841 if (edit->book_mark)
842 book_mark_dec (edit, edit->curs_line);
843 edit->total_lines--;
844 edit->force |= REDRAW_AFTER_CURSOR;
846 edit_push_action (edit, p + 256);
847 if (edit->curs1 < edit->start_display) {
848 edit->start_display--;
849 if (p == '\n')
850 edit->start_line--;
852 edit_modification (edit);
854 return p;
858 int edit_backspace (WEdit * edit)
860 int p;
861 if (!edit->curs1)
862 return 0;
864 edit->mark1 -= (edit->mark1 >= edit->curs1);
865 edit->mark2 -= (edit->mark2 >= edit->curs1);
866 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
868 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
869 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
870 free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
871 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
873 edit->last_byte--;
874 edit->curs1--;
876 if (p == '\n') {
877 if (edit->book_mark)
878 book_mark_dec (edit, edit->curs_line);
879 edit->curs_line--;
880 edit->total_lines--;
881 edit->force |= REDRAW_AFTER_CURSOR;
883 edit_push_action (edit, p);
885 if (edit->curs1 < edit->start_display) {
886 edit->start_display--;
887 if (p == '\n')
888 edit->start_line--;
890 edit_modification (edit);
892 return p;
895 #ifdef FAST_MOVE_CURSOR
897 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
899 unsigned long next;
900 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
901 edit->curs_line--;
902 next -= (unsigned long) dest;
903 n -= next;
904 src += next;
905 dest += next;
909 int edit_move_backward_lots (WEdit * edit, long increment)
911 int r, s, t;
912 unsigned char *p;
914 if (increment > edit->curs1)
915 increment = edit->curs1;
916 if (increment <= 0)
917 return -1;
918 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
920 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
921 if (r > increment)
922 r = increment;
923 s = edit->curs1 & M_EDIT_BUF_SIZE;
925 p = 0;
926 if (s > r) {
927 memqcpy (edit, edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
928 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r, r);
929 } else {
930 if (s) {
931 memqcpy (edit, edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - s,
932 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
933 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
934 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
936 memqcpy (edit, edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
937 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] + EDIT_BUF_SIZE - (r - s), r - s);
939 increment -= r;
940 edit->curs1 -= r;
941 edit->curs2 += r;
942 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
943 if (p)
944 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
945 else
946 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
947 } else {
948 if (p)
949 free (p);
952 s = edit->curs1 & M_EDIT_BUF_SIZE;
953 while (increment) {
954 p = 0;
955 r = EDIT_BUF_SIZE;
956 if (r > increment)
957 r = increment;
958 t = s;
959 if (r < t)
960 t = r;
961 memqcpy (edit,
962 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + EDIT_BUF_SIZE - t,
963 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
965 if (r >= s) {
966 if (t) {
967 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
968 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
970 memqcpy (edit,
971 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + EDIT_BUF_SIZE - r,
972 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] + EDIT_BUF_SIZE - (r - s),
973 r - s);
975 increment -= r;
976 edit->curs1 -= r;
977 edit->curs2 += r;
978 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
979 if (p)
980 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
981 else
982 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
983 } else {
984 if (p)
985 free (p);
988 return edit_get_byte (edit, edit->curs1);
991 #endif /* ! FAST_MOVE_CURSOR */
993 /* moves the cursor right or left: increment positive or negative respectively */
994 int edit_cursor_move (WEdit * edit, long increment)
996 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
997 int c;
999 #ifdef FAST_MOVE_CURSOR
1000 if (increment < -256) {
1001 edit->force |= REDRAW_PAGE;
1002 return edit_move_backward_lots (edit, -increment);
1004 #endif /* ! FAST_MOVE_CURSOR */
1006 if (increment < 0) {
1007 for (; increment < 0; increment++) {
1008 if (!edit->curs1)
1009 return -1;
1011 edit_push_action (edit, CURS_RIGHT);
1013 c = edit_get_byte (edit, edit->curs1 - 1);
1014 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1015 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
1016 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1017 edit->curs2++;
1018 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1019 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1020 free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1021 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1023 edit->curs1--;
1024 if (c == '\n') {
1025 edit->curs_line--;
1026 edit->force |= REDRAW_LINE_BELOW;
1030 return c;
1031 } else if (increment > 0) {
1032 for (; increment > 0; increment--) {
1033 if (!edit->curs2)
1034 return -2;
1036 edit_push_action (edit, CURS_LEFT);
1038 c = edit_get_byte (edit, edit->curs1);
1039 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1040 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = malloc (EDIT_BUF_SIZE);
1041 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1042 edit->curs1++;
1043 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1044 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1045 free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1046 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1048 edit->curs2--;
1049 if (c == '\n') {
1050 edit->curs_line++;
1051 edit->force |= REDRAW_LINE_ABOVE;
1054 return c;
1055 } else
1056 return -3;
1059 /* These functions return positions relative to lines */
1061 /* returns index of last char on line + 1 */
1062 long edit_eol (WEdit * edit, long current)
1064 if (current < edit->last_byte) {
1065 for (;; current++)
1066 if (edit_get_byte (edit, current) == '\n')
1067 break;
1068 } else
1069 return edit->last_byte;
1070 return current;
1073 /* returns index of first char on line */
1074 long edit_bol (WEdit * edit, long current)
1076 if (current > 0) {
1077 for (;; current--)
1078 if (edit_get_byte (edit, current - 1) == '\n')
1079 break;
1080 } else
1081 return 0;
1082 return current;
1086 int edit_count_lines (WEdit * edit, long current, int upto)
1088 int lines = 0;
1089 if (upto > edit->last_byte)
1090 upto = edit->last_byte;
1091 if (current < 0)
1092 current = 0;
1093 while (current < upto)
1094 if (edit_get_byte (edit, current++) == '\n')
1095 lines++;
1096 return lines;
1100 /* If lines is zero this returns the count of lines from current to upto. */
1101 /* If upto is zero returns index of lines forward current. */
1102 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1104 if (upto) {
1105 return edit_count_lines (edit, current, upto);
1106 } else {
1107 int next;
1108 if (lines < 0)
1109 lines = 0;
1110 while (lines--) {
1111 next = edit_eol (edit, current) + 1;
1112 if (next > edit->last_byte)
1113 break;
1114 else
1115 current = next;
1117 return current;
1122 /* Returns offset of 'lines' lines up from current */
1123 long edit_move_backward (WEdit * edit, long current, int lines)
1125 if (lines < 0)
1126 lines = 0;
1127 current = edit_bol (edit, current);
1128 while((lines--) && current != 0)
1129 current = edit_bol (edit, current - 1);
1130 return current;
1133 /* If cols is zero this returns the count of columns from current to upto. */
1134 /* If upto is zero returns index of cols across from current. */
1135 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1137 long p, q;
1138 int col = 0;
1140 if (upto) {
1141 q = upto;
1142 cols = -10;
1143 } else
1144 q = edit->last_byte + 2;
1146 for (col = 0, p = current; p < q; p++) {
1147 int c;
1148 if (cols != -10) {
1149 if (col == cols)
1150 return p;
1151 if (col > cols)
1152 return p - 1;
1154 c = edit_get_byte (edit, p);
1155 if (c == '\t')
1156 col += TAB_SIZE - col % TAB_SIZE;
1157 else if (c == '\n') {
1158 if (upto)
1159 return col;
1160 else
1161 return p;
1162 } else if (c < 32 || c == 127)
1163 col += 2; /* Caret notation for control characters */
1164 else
1165 col++;
1167 return col;
1170 /* returns the current column position of the cursor */
1171 int edit_get_col (WEdit * edit)
1173 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1177 /* Scrolling functions */
1179 void edit_update_curs_row (WEdit * edit)
1181 edit->curs_row = edit->curs_line - edit->start_line;
1184 void edit_update_curs_col (WEdit * edit)
1186 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1189 /*moves the display start position up by i lines */
1190 void edit_scroll_upward (WEdit * edit, unsigned long i)
1192 int lines_above = edit->start_line;
1193 if (i > lines_above)
1194 i = lines_above;
1195 if (i) {
1196 edit->start_line -= i;
1197 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1198 edit->force |= REDRAW_PAGE;
1199 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1201 edit_update_curs_row (edit);
1205 /* returns 1 if could scroll, 0 otherwise */
1206 void edit_scroll_downward (WEdit * edit, int i)
1208 int lines_below;
1209 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1210 if (lines_below > 0) {
1211 if (i > lines_below)
1212 i = lines_below;
1213 edit->start_line += i;
1214 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1215 edit->force |= REDRAW_PAGE;
1216 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1218 edit_update_curs_row (edit);
1221 void edit_scroll_right (WEdit * edit, int i)
1223 edit->force |= REDRAW_PAGE;
1224 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1225 edit->start_col -= i;
1228 void edit_scroll_left (WEdit * edit, int i)
1230 if (edit->start_col) {
1231 edit->start_col += i;
1232 if (edit->start_col > 0)
1233 edit->start_col = 0;
1234 edit->force |= REDRAW_PAGE;
1235 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1239 /* high level cursor movement commands */
1241 static int is_in_indent (WEdit *edit)
1243 long p = edit_bol (edit, edit->curs1);
1244 while (p < edit->curs1)
1245 if (!strchr (" \t", edit_get_byte (edit, p++)))
1246 return 0;
1247 return 1;
1250 static int left_of_four_spaces (WEdit *edit);
1252 void edit_move_to_prev_col (WEdit * edit, long p)
1254 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
1256 if (is_in_indent (edit) && option_fake_half_tabs) {
1257 edit_update_curs_col (edit);
1258 if (space_width)
1259 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1260 int q = edit->curs_col;
1261 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1262 p = edit_bol (edit, edit->curs1);
1263 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1264 if (!left_of_four_spaces (edit))
1265 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1271 /* move i lines */
1272 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1274 long p, l = edit->curs_line;
1276 if (i > l)
1277 i = l;
1278 if (i) {
1279 if (i > 1)
1280 edit->force |= REDRAW_PAGE;
1281 if (scroll)
1282 edit_scroll_upward (edit, i);
1284 p = edit_bol (edit, edit->curs1);
1285 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1286 edit_move_to_prev_col (edit, p);
1288 edit->search_start = edit->curs1;
1289 edit->found_len = 0;
1293 int is_blank (WEdit * edit, long offset)
1295 long s, f;
1296 int c;
1297 s = edit_bol (edit, offset);
1298 f = edit_eol (edit, offset) - 1;
1299 while (s <= f) {
1300 c = edit_get_byte (edit, s++);
1301 if (!isspace (c))
1302 return 0;
1304 return 1;
1308 /* returns the offset of line i */
1309 long edit_find_line (WEdit * edit, int line)
1311 int i, j = 0;
1312 int m = 2000000000;
1313 if (!edit->caches_valid) {
1314 for (i = 0; i < N_LINE_CACHES; i++)
1315 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1316 /* three offsets that we *know* are line 0 at 0 and these two: */
1317 edit->line_numbers[1] = edit->curs_line;
1318 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1319 edit->line_numbers[2] = edit->total_lines;
1320 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1321 edit->caches_valid = 1;
1323 if (line >= edit->total_lines)
1324 return edit->line_offsets[2];
1325 if (line <= 0)
1326 return 0;
1327 /* find the closest known point */
1328 for (i = 0; i < N_LINE_CACHES; i++) {
1329 int n;
1330 n = abs (edit->line_numbers[i] - line);
1331 if (n < m) {
1332 m = n;
1333 j = i;
1336 if (m == 0)
1337 return edit->line_offsets[j]; /* know the offset exactly */
1338 if (m == 1 && j >= 3)
1339 i = j; /* one line different - caller might be looping, so stay in this cache */
1340 else
1341 i = 3 + (rand () % (N_LINE_CACHES - 3));
1342 if (line > edit->line_numbers[j])
1343 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1344 else
1345 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1346 edit->line_numbers[i] = line;
1347 return edit->line_offsets[i];
1350 int line_is_blank (WEdit * edit, long line)
1352 return is_blank (edit, edit_find_line (edit, line));
1355 /* moves up until a blank line is reached, or until just
1356 before a non-blank line is reached */
1357 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1359 int i;
1360 if (edit->curs_line <= 1) {
1361 i = 0;
1362 } else {
1363 if (line_is_blank (edit, edit->curs_line)) {
1364 if (line_is_blank (edit, edit->curs_line - 1)) {
1365 for (i = edit->curs_line - 1; i; i--)
1366 if (!line_is_blank (edit, i)) {
1367 i++;
1368 break;
1370 } else {
1371 for (i = edit->curs_line - 1; i; i--)
1372 if (line_is_blank (edit, i))
1373 break;
1375 } else {
1376 for (i = edit->curs_line - 1; i; i--)
1377 if (line_is_blank (edit, i))
1378 break;
1381 edit_move_up (edit, edit->curs_line - i, scroll);
1384 /* move i lines */
1385 void edit_move_down (WEdit * edit, int i, int scroll)
1387 long p, l = edit->total_lines - edit->curs_line;
1389 if (i > l)
1390 i = l;
1391 if (i) {
1392 if (i > 1)
1393 edit->force |= REDRAW_PAGE;
1394 if (scroll)
1395 edit_scroll_downward (edit, i);
1396 p = edit_bol (edit, edit->curs1);
1397 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1398 edit_move_to_prev_col (edit, p);
1400 edit->search_start = edit->curs1;
1401 edit->found_len = 0;
1405 /* moves down until a blank line is reached, or until just
1406 before a non-blank line is reached */
1407 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1409 int i;
1410 if (edit->curs_line >= edit->total_lines - 1) {
1411 i = edit->total_lines;
1412 } else {
1413 if (line_is_blank (edit, edit->curs_line)) {
1414 if (line_is_blank (edit, edit->curs_line + 1)) {
1415 for (i = edit->curs_line + 1; i; i++)
1416 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1417 i--;
1418 break;
1420 } else {
1421 for (i = edit->curs_line + 1; i; i++)
1422 if (line_is_blank (edit, i) || i >= edit->total_lines)
1423 break;
1425 } else {
1426 for (i = edit->curs_line + 1; i; i++)
1427 if (line_is_blank (edit, i) || i >= edit->total_lines)
1428 break;
1431 edit_move_down (edit, i - edit->curs_line, scroll);
1434 static void edit_begin_page (WEdit *edit)
1436 edit_update_curs_row (edit);
1437 edit_move_up (edit, edit->curs_row, 0);
1440 static void edit_end_page (WEdit *edit)
1442 edit_update_curs_row (edit);
1443 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1447 /* goto beginning of text */
1448 static void edit_move_to_top (WEdit * edit)
1450 if (edit->curs_line) {
1451 edit_cursor_move (edit, -edit->curs1);
1452 edit_move_to_prev_col (edit, 0);
1453 edit->force |= REDRAW_PAGE;
1454 edit->search_start = 0;
1455 edit_update_curs_row(edit);
1460 /* goto end of text */
1461 static void edit_move_to_bottom (WEdit * edit)
1463 if (edit->curs_line < edit->total_lines) {
1464 edit_cursor_move (edit, edit->curs2);
1465 edit->start_display = edit->last_byte;
1466 edit->start_line = edit->total_lines;
1467 edit_update_curs_row(edit);
1468 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1469 edit->force |= REDRAW_PAGE;
1473 /* goto beginning of line */
1474 static void edit_cursor_to_bol (WEdit * edit)
1476 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1477 edit->search_start = edit->curs1;
1478 edit->prev_col = edit_get_col (edit);
1481 /* goto end of line */
1482 static void edit_cursor_to_eol (WEdit * edit)
1484 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1485 edit->search_start = edit->curs1;
1486 edit->prev_col = edit_get_col (edit);
1489 /* move cursor to line 'line' */
1490 void edit_move_to_line (WEdit * e, long line)
1492 if(line < e->curs_line)
1493 edit_move_up (e, e->curs_line - line, 0);
1494 else
1495 edit_move_down (e, line - e->curs_line, 0);
1496 edit_scroll_screen_over_cursor (e);
1499 /* scroll window so that first visible line is 'line' */
1500 void edit_move_display (WEdit * e, long line)
1502 if(line < e->start_line)
1503 edit_scroll_upward (e, e->start_line - line);
1504 else
1505 edit_scroll_downward (e, line - e->start_line);
1508 /* save markers onto undo stack */
1509 void edit_push_markers (WEdit * edit)
1511 edit_push_action (edit, MARK_1 + edit->mark1);
1512 edit_push_action (edit, MARK_2 + edit->mark2);
1515 void free_selections (void)
1517 int i;
1518 for (i = 0; i < NUM_SELECTION_HISTORY; i++)
1519 if (selection_history[i].text) {
1520 free (selection_history[i].text);
1521 selection_history[i].text = 0;
1522 selection_history[i].len = 0;
1524 current_selection = 0;
1527 /* return -1 on nothing to store or error, zero otherwise */
1528 void edit_get_selection (WEdit * edit)
1530 long start_mark, end_mark;
1531 if (eval_marks (edit, &start_mark, &end_mark))
1532 return;
1533 if (selection_history[current_selection].len < 4096) /* large selections should not be held -- to save memory */
1534 current_selection = (current_selection + 1) % NUM_SELECTION_HISTORY;
1535 selection_history[current_selection].len = end_mark - start_mark;
1536 if (selection_history[current_selection].text)
1537 free (selection_history[current_selection].text);
1538 selection_history[current_selection].text = malloc (selection_history[current_selection].len + 1);
1539 if (!selection_history[current_selection].text) {
1540 selection_history[current_selection].text = malloc (1);
1541 *selection_history[current_selection].text = 0;
1542 selection_history[current_selection].len = 0;
1543 } else {
1544 unsigned char *p = selection_history[current_selection].text;
1545 for (; start_mark < end_mark; start_mark++)
1546 *p++ = edit_get_byte (edit, start_mark);
1547 *p = 0;
1549 selection.text = selection_history[current_selection].text;
1550 selection.len = selection_history[current_selection].len;
1553 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1555 edit->mark1 = m1;
1556 edit->mark2 = m2;
1557 edit->column1 = c1;
1558 edit->column2 = c2;
1562 /* highlight marker toggle */
1563 void edit_mark_cmd (WEdit * edit, int unmark)
1565 edit_push_markers (edit);
1566 if (unmark) {
1567 edit_set_markers (edit, 0, 0, 0, 0);
1568 edit->force |= REDRAW_PAGE;
1569 } else {
1570 if (edit->mark2 >= 0) {
1571 edit_set_markers (edit, edit->curs1, -1, edit->curs_col, edit->curs_col);
1572 edit->force |= REDRAW_PAGE;
1573 } else
1574 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col);
1578 static unsigned long my_type_of (int c)
1580 int x, r = 0;
1581 char *p, *q;
1582 if (!c)
1583 return 0;
1584 if (c == '!') {
1585 if (*option_chars_move_whole_word == '!')
1586 return 2;
1587 return 0x80000000UL;
1589 if (isupper (c))
1590 c = 'A';
1591 else if (islower (c))
1592 c = 'a';
1593 else if (isalpha (c))
1594 c = 'a';
1595 else if (isdigit (c))
1596 c = '0';
1597 else if (isspace (c))
1598 c = ' ';
1599 q = strchr (option_chars_move_whole_word, c);
1600 if (!q)
1601 return 0xFFFFFFFFUL;
1602 do {
1603 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1604 if (*p == '!')
1605 x <<= 1;
1606 r |= x;
1607 } while ((q = strchr (q + 1, c)));
1608 return r;
1611 void edit_left_word_move (WEdit * edit, int s)
1613 for (;;) {
1614 int c1, c2;
1615 edit_cursor_move (edit, -1);
1616 if (!edit->curs1)
1617 break;
1618 c1 = edit_get_byte (edit, edit->curs1 - 1);
1619 c2 = edit_get_byte (edit, edit->curs1);
1620 if (!(my_type_of (c1) & my_type_of (c2)))
1621 break;
1622 if (isspace (c1) && !isspace (c2))
1623 break;
1624 if (s)
1625 if (!isspace (c1) && isspace (c2))
1626 break;
1630 static void edit_left_word_move_cmd (WEdit * edit)
1632 edit_left_word_move (edit, 0);
1633 edit->force |= REDRAW_PAGE;
1636 void edit_right_word_move (WEdit * edit, int s)
1638 for (;;) {
1639 int c1, c2;
1640 edit_cursor_move (edit, 1);
1641 if (edit->curs1 >= edit->last_byte)
1642 break;
1643 c1 = edit_get_byte (edit, edit->curs1 - 1);
1644 c2 = edit_get_byte (edit, edit->curs1);
1645 if (!(my_type_of (c1) & my_type_of (c2)))
1646 break;
1647 if (isspace (c1) && !isspace (c2))
1648 break;
1649 if (s)
1650 if (!isspace (c1) && isspace (c2))
1651 break;
1655 static void edit_right_word_move_cmd (WEdit * edit)
1657 edit_right_word_move (edit, 0);
1658 edit->force |= REDRAW_PAGE;
1662 static void edit_right_delete_word (WEdit * edit)
1664 int c1, c2;
1665 for (;;) {
1666 if (edit->curs1 >= edit->last_byte)
1667 break;
1668 c1 = edit_delete (edit);
1669 c2 = edit_get_byte (edit, edit->curs1);
1670 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1671 break;
1672 if (!(my_type_of (c1) & my_type_of (c2)))
1673 break;
1677 static void edit_left_delete_word (WEdit * edit)
1679 int c1, c2;
1680 for (;;) {
1681 if (edit->curs1 <= 0)
1682 break;
1683 c1 = edit_backspace (edit);
1684 c2 = edit_get_byte (edit, edit->curs1 - 1);
1685 if ((isspace (c1) == 0) != (isspace (c2) == 0))
1686 break;
1687 if (!(my_type_of (c1) & my_type_of (c2)))
1688 break;
1693 the start column position is not recorded, and hence does not
1694 undo as it happed. But who would notice.
1696 void edit_do_undo (WEdit * edit)
1698 long ac;
1699 long count = 0;
1701 push_action_disabled = 1; /* don't record undo's onto undo stack! */
1703 while ((ac = pop_action (edit)) < KEY_PRESS) {
1704 switch ((int) ac) {
1705 case STACK_BOTTOM:
1706 goto done_undo;
1707 case CURS_RIGHT:
1708 edit_cursor_move (edit, 1);
1709 break;
1710 case CURS_LEFT:
1711 edit_cursor_move (edit, -1);
1712 break;
1713 case BACKSPACE:
1714 edit_backspace (edit);
1715 break;
1716 case DELCHAR:
1717 edit_delete (edit);
1718 break;
1719 case COLUMN_ON:
1720 column_highlighting = 1;
1721 break;
1722 case COLUMN_OFF:
1723 column_highlighting = 0;
1724 break;
1726 if (ac >= 256 && ac < 512)
1727 edit_insert_ahead (edit, ac - 256);
1728 if (ac >= 0 && ac < 256)
1729 edit_insert (edit, ac);
1731 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
1732 edit->mark1 = ac - MARK_1;
1733 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
1734 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
1735 edit->mark2 = ac - MARK_2;
1736 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
1738 if (count++)
1739 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
1742 if (edit->start_display > ac - KEY_PRESS) {
1743 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
1744 edit->force |= REDRAW_PAGE;
1745 } else if (edit->start_display < ac - KEY_PRESS) {
1746 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
1747 edit->force |= REDRAW_PAGE;
1749 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
1750 edit_update_curs_row (edit);
1752 done_undo:;
1753 push_action_disabled = 0;
1756 static void edit_delete_to_line_end (WEdit * edit)
1758 while (edit_get_byte (edit, edit->curs1) != '\n') {
1759 if (!edit->curs2)
1760 break;
1761 edit_delete (edit);
1765 static void edit_delete_to_line_begin (WEdit * edit)
1767 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
1768 if (!edit->curs1)
1769 break;
1770 edit_backspace (edit);
1774 void edit_delete_line (WEdit * edit)
1776 int c;
1777 do {
1778 c = edit_delete (edit);
1779 } while (c != '\n' && c);
1780 do {
1781 c = edit_backspace (edit);
1782 } while (c != '\n' && c);
1783 if (c)
1784 edit_insert (edit, '\n');
1787 static void insert_spaces_tab (WEdit * edit, int half)
1789 int i;
1790 edit_update_curs_col (edit);
1791 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
1792 while (i > 0) {
1793 edit_insert (edit, ' ');
1794 i -= space_width;
1798 static int is_aligned_on_a_tab (WEdit * edit)
1800 edit_update_curs_col (edit);
1801 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
1802 return 0; /* not alligned on a tab */
1803 return 1;
1806 static int right_of_four_spaces (WEdit *edit)
1808 int i, ch = 0;
1809 for (i = 1; i <= HALF_TAB_SIZE; i++)
1810 ch |= edit_get_byte (edit, edit->curs1 - i);
1811 if (ch == ' ')
1812 return is_aligned_on_a_tab (edit);
1813 return 0;
1816 static int left_of_four_spaces (WEdit *edit)
1818 int i, ch = 0;
1819 for (i = 0; i < HALF_TAB_SIZE; i++)
1820 ch |= edit_get_byte (edit, edit->curs1 + i);
1821 if (ch == ' ')
1822 return is_aligned_on_a_tab (edit);
1823 return 0;
1826 int edit_indent_width (WEdit * edit, long p)
1828 long q = p;
1829 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
1830 q++;
1831 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
1834 void edit_insert_indent (WEdit * edit, int indent)
1836 if (!option_fill_tabs_with_spaces) {
1837 while (indent >= TAB_SIZE) {
1838 edit_insert (edit, '\t');
1839 indent -= TAB_SIZE;
1842 while (indent-- > 0)
1843 edit_insert (edit, ' ');
1846 void edit_auto_indent (WEdit * edit, int extra, int no_advance)
1848 long p;
1849 int indent;
1850 p = edit->curs1;
1851 while (isspace (edit_get_byte (edit, p - 1)) && p > 0) /* move back/up to a line with text */
1852 p--;
1853 indent = edit_indent_width (edit, edit_bol (edit, p));
1854 if (edit->curs_col < indent && no_advance)
1855 indent = edit->curs_col;
1856 edit_insert_indent (edit, indent + (option_fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE) * space_width * extra);
1859 static void edit_double_newline (WEdit * edit)
1861 edit_insert (edit, '\n');
1862 if (edit_get_byte (edit, edit->curs1) == '\n')
1863 return;
1864 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
1865 return;
1866 edit->force |= REDRAW_PAGE;
1867 edit_insert (edit, '\n');
1870 static void edit_tab_cmd (WEdit * edit)
1872 int i;
1874 if (option_fake_half_tabs) {
1875 if (is_in_indent (edit)) {
1876 /*insert a half tab (usually four spaces) unless there is a
1877 half tab already behind, then delete it and insert a
1878 full tab. */
1879 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
1880 for (i = 1; i <= HALF_TAB_SIZE; i++)
1881 edit_backspace (edit);
1882 edit_insert (edit, '\t');
1883 } else {
1884 insert_spaces_tab (edit, 1);
1886 return;
1889 if (option_fill_tabs_with_spaces) {
1890 insert_spaces_tab (edit, 0);
1891 } else {
1892 edit_insert (edit, '\t');
1894 return;
1897 void format_paragraph (WEdit * edit, int force);
1899 static void check_and_wrap_line (WEdit * edit)
1901 int curs, c;
1902 if (!option_typewriter_wrap)
1903 return;
1904 edit_update_curs_col (edit);
1905 if (edit->curs_col < option_word_wrap_line_length)
1906 return;
1907 curs = edit->curs1;
1908 for (;;) {
1909 curs--;
1910 c = edit_get_byte (edit, curs);
1911 if (c == '\n' || curs <= 0) {
1912 edit_insert (edit, '\n');
1913 return;
1915 if (c == ' ' || c == '\t') {
1916 int current = edit->curs1;
1917 edit_cursor_move (edit, curs - edit->curs1 + 1);
1918 edit_insert (edit, '\n');
1919 edit_cursor_move (edit, current - edit->curs1 + 1);
1920 return;
1925 void edit_execute_macro (WEdit * edit, struct macro macro[], int n);
1927 int edit_translate_key (WEdit * edit, unsigned int x_keycode, long x_key, int x_state, int *cmd, int *ch)
1929 int command = -1;
1930 int char_for_insertion = -1;
1932 #include "edit_key_translator.c"
1934 *cmd = command;
1935 *ch = char_for_insertion;
1937 if((command == -1 || command == 0) && char_for_insertion == -1) /* unchanged, key has no function here */
1938 return 0;
1939 return 1;
1942 void edit_push_key_press (WEdit * edit)
1944 edit_push_action (edit, KEY_PRESS + edit->start_display);
1945 if (edit->mark2 == -1)
1946 edit_push_action (edit, MARK_1 + edit->mark1);
1949 /* this find the matching bracket in either direction, and sets edit->bracket */
1950 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
1952 const char * const b = "{}{[][()(", *p;
1953 int i = 1, a, inc = -1, c, d, n = 0;
1954 unsigned long j = 0;
1955 long q;
1956 edit_update_curs_row (edit);
1957 c = edit_get_byte (edit, edit->curs1);
1958 p = strchr (b, c);
1959 /* no limit */
1960 if (!furthest_bracket_search)
1961 furthest_bracket_search--;
1962 /* not on a bracket at all */
1963 if (!p)
1964 return -1;
1965 /* the matching bracket */
1966 d = p[1];
1967 /* going left or right? */
1968 if (strchr ("{[(", c))
1969 inc = 1;
1970 for (q = edit->curs1 + inc;; q += inc) {
1971 /* out of buffer? */
1972 if (q >= edit->last_byte || q < 0)
1973 break;
1974 a = edit_get_byte (edit, q);
1975 /* don't want to eat CPU */
1976 if (j++ > furthest_bracket_search)
1977 break;
1978 /* out of screen? */
1979 if (in_screen) {
1980 if (q < edit->start_display)
1981 break;
1982 /* count lines if searching downward */
1983 if (inc > 0 && a == '\n')
1984 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
1985 break;
1987 /* count bracket depth */
1988 i += (a == c) - (a == d);
1989 /* return if bracket depth is zero */
1990 if (!i)
1991 return q;
1993 /* no match */
1994 return -1;
1997 static long last_bracket = -1;
1999 static void edit_find_bracket (WEdit * edit)
2001 edit->bracket = edit_get_bracket (edit, 1, 10000);
2002 if (last_bracket != edit->bracket)
2003 edit->force |= REDRAW_PAGE;
2004 last_bracket = edit->bracket;
2007 static void edit_goto_matching_bracket (WEdit *edit)
2009 long q;
2010 q = edit_get_bracket (edit, 0, 0);
2011 if (q < 0)
2012 return;
2013 edit->bracket = edit->curs1;
2014 edit->force |= REDRAW_PAGE;
2015 edit_cursor_move (edit, q - edit->curs1);
2018 /* this executes a command as though the user initiated it through a key press. */
2019 /* callback with WIDGET_KEY as a message calls this after translating the key
2020 press */
2021 /* this can be used to pass any command to the editor. Same as sendevent with
2022 msg = WIDGET_COMMAND and par = command except the screen wouldn't update */
2023 /* one of command or char_for_insertion must be passed as -1 */
2024 /* commands are executed, and char_for_insertion is inserted at the cursor */
2025 /* returns 0 if the command is a macro that was not found, 1 otherwise */
2026 int edit_execute_key_command (WEdit * edit, int command, int char_for_insertion)
2028 int r;
2029 if (command == CK_Begin_Record_Macro) {
2030 edit->macro_i = 0;
2031 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2032 return command;
2034 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2035 edit->force |= REDRAW_COMPLETELY;
2036 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2037 edit->macro_i = -1;
2038 return command;
2040 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2041 edit->macro[edit->macro_i].command = command;
2042 edit->macro[edit->macro_i++].ch = char_for_insertion;
2044 /* record the beginning of a set of editing actions initiated by a key press */
2045 if (command != CK_Undo)
2046 edit_push_key_press (edit);
2048 r = edit_execute_cmd (edit, command, char_for_insertion);
2049 if (column_highlighting)
2050 edit->force |= REDRAW_PAGE;
2052 return r;
2055 static const char * const shell_cmd[] = SHELL_COMMANDS_i
2056 void edit_mail_dialog (WEdit * edit);
2059 This executes a command at a lower level than macro recording.
2060 It also does not push a key_press onto the undo stack. This means
2061 that if it is called many times, a single undo command will undo
2062 all of them. It also does not check for the Undo command.
2063 Returns 0 if the command is a macro that was not found, 1
2064 otherwise.
2066 int edit_execute_cmd (WEdit * edit, int command, int char_for_insertion)
2068 int result = 1;
2069 edit->force |= REDRAW_LINE;
2070 if (edit->found_len || column_highlighting)
2071 /* the next key press will unhighlight the found string, so update whole page */
2072 edit->force |= REDRAW_PAGE;
2074 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2075 column_highlighting = 0;
2076 if (!edit->highlight || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2077 edit_mark_cmd (edit, 1); /* clear */
2078 edit_mark_cmd (edit, 0); /* marking on */
2080 edit->highlight = 1;
2081 } else { /* any other command */
2082 if (edit->highlight)
2083 edit_mark_cmd (edit, 0); /* clear */
2084 edit->highlight = 0;
2087 /* first check for undo */
2088 if (command == CK_Undo) {
2089 edit_do_undo (edit);
2090 edit->found_len = 0;
2091 edit->prev_col = edit_get_col (edit);
2092 edit->search_start = edit->curs1;
2093 return 1;
2095 /* An ordinary key press */
2096 if (char_for_insertion >= 0) {
2097 if (edit->overwrite) {
2098 if (edit_get_byte (edit, edit->curs1) != '\n')
2099 edit_delete (edit);
2101 edit_insert (edit, char_for_insertion);
2102 if (option_auto_para_formatting) {
2103 format_paragraph (edit, 0);
2104 edit->force |= REDRAW_PAGE;
2105 } else
2106 check_and_wrap_line (edit);
2107 edit->found_len = 0;
2108 edit->prev_col = edit_get_col (edit);
2109 edit->search_start = edit->curs1;
2110 edit_find_bracket (edit);
2111 edit_check_spelling (edit);
2112 return 1;
2114 switch (command) {
2115 case CK_Begin_Page:
2116 case CK_End_Page:
2117 case CK_Begin_Page_Highlight:
2118 case CK_End_Page_Highlight:
2119 case CK_Word_Left:
2120 case CK_Word_Right:
2121 case CK_Up:
2122 case CK_Down:
2123 case CK_Word_Left_Highlight:
2124 case CK_Word_Right_Highlight:
2125 case CK_Up_Highlight:
2126 case CK_Down_Highlight:
2127 if (edit->mark2 == -1)
2128 break; /*marking is following the cursor: may need to highlight a whole line */
2129 case CK_Left:
2130 case CK_Right:
2131 case CK_Left_Highlight:
2132 case CK_Right_Highlight:
2133 edit->force |= REDRAW_CHAR_ONLY;
2136 /* basic cursor key commands */
2137 switch (command) {
2138 case CK_BackSpace:
2139 if (option_backspace_through_tabs && is_in_indent (edit)) {
2140 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2141 && edit->curs1 > 0)
2142 edit_backspace (edit);
2143 break;
2144 } else {
2145 if (option_fake_half_tabs) {
2146 int i;
2147 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2148 for (i = 0; i < HALF_TAB_SIZE; i++)
2149 edit_backspace (edit);
2150 break;
2154 edit_backspace (edit);
2155 break;
2156 case CK_Delete:
2157 if (option_fake_half_tabs) {
2158 int i;
2159 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2160 for (i = 1; i <= HALF_TAB_SIZE; i++)
2161 edit_delete (edit);
2162 break;
2165 edit_delete (edit);
2166 break;
2167 case CK_Delete_Word_Left:
2168 edit_left_delete_word (edit);
2169 break;
2170 case CK_Delete_Word_Right:
2171 edit_right_delete_word (edit);
2172 break;
2173 case CK_Delete_Line:
2174 edit_delete_line (edit);
2175 break;
2176 case CK_Delete_To_Line_End:
2177 edit_delete_to_line_end (edit);
2178 break;
2179 case CK_Delete_To_Line_Begin:
2180 edit_delete_to_line_begin (edit);
2181 break;
2182 case CK_Enter:
2183 if (option_auto_para_formatting) {
2184 edit_double_newline (edit);
2185 if (option_return_does_auto_indent)
2186 edit_auto_indent (edit, 0, 1);
2187 format_paragraph (edit, 0);
2188 } else {
2189 edit_insert (edit, '\n');
2190 if (option_return_does_auto_indent) {
2191 edit_auto_indent (edit, 0, 1);
2194 break;
2195 case CK_Return:
2196 edit_insert (edit, '\n');
2197 break;
2199 case CK_Page_Up:
2200 case CK_Page_Up_Highlight:
2201 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2202 break;
2203 case CK_Page_Down:
2204 case CK_Page_Down_Highlight:
2205 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2206 break;
2207 case CK_Left:
2208 case CK_Left_Highlight:
2209 if (option_fake_half_tabs) {
2210 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2211 edit_cursor_move (edit, -HALF_TAB_SIZE);
2212 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2213 break;
2216 edit_cursor_move (edit, -1);
2217 break;
2218 case CK_Right:
2219 case CK_Right_Highlight:
2220 if (option_fake_half_tabs) {
2221 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2222 edit_cursor_move (edit, HALF_TAB_SIZE);
2223 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2224 break;
2227 edit_cursor_move (edit, 1);
2228 break;
2229 case CK_Begin_Page:
2230 case CK_Begin_Page_Highlight:
2231 edit_begin_page (edit);
2232 break;
2233 case CK_End_Page:
2234 case CK_End_Page_Highlight:
2235 edit_end_page (edit);
2236 break;
2237 case CK_Word_Left:
2238 case CK_Word_Left_Highlight:
2239 edit_left_word_move_cmd (edit);
2240 break;
2241 case CK_Word_Right:
2242 case CK_Word_Right_Highlight:
2243 edit_right_word_move_cmd (edit);
2244 break;
2245 case CK_Up:
2246 case CK_Up_Highlight:
2247 edit_move_up (edit, 1, 0);
2248 break;
2249 case CK_Down:
2250 case CK_Down_Highlight:
2251 edit_move_down (edit, 1, 0);
2252 break;
2253 case CK_Paragraph_Up:
2254 case CK_Paragraph_Up_Highlight:
2255 edit_move_up_paragraph (edit, 0);
2256 break;
2257 case CK_Paragraph_Down:
2258 case CK_Paragraph_Down_Highlight:
2259 edit_move_down_paragraph (edit, 0);
2260 break;
2261 case CK_Scroll_Up:
2262 case CK_Scroll_Up_Highlight:
2263 edit_move_up (edit, 1, 1);
2264 break;
2265 case CK_Scroll_Down:
2266 case CK_Scroll_Down_Highlight:
2267 edit_move_down (edit, 1, 1);
2268 break;
2269 case CK_Home:
2270 case CK_Home_Highlight:
2271 edit_cursor_to_bol (edit);
2272 break;
2273 case CK_End:
2274 case CK_End_Highlight:
2275 edit_cursor_to_eol (edit);
2276 break;
2278 case CK_Tab:
2279 edit_tab_cmd (edit);
2280 if (option_auto_para_formatting) {
2281 format_paragraph (edit, 0);
2282 edit->force |= REDRAW_PAGE;
2283 } else
2284 check_and_wrap_line (edit);
2285 break;
2287 case CK_Toggle_Insert:
2288 edit->overwrite = (edit->overwrite == 0);
2289 break;
2291 case CK_Mark:
2292 if (edit->mark2 >= 0) {
2293 if (column_highlighting)
2294 edit_push_action (edit, COLUMN_ON);
2295 column_highlighting = 0;
2297 edit_mark_cmd (edit, 0);
2298 break;
2299 case CK_Column_Mark:
2300 if (!column_highlighting)
2301 edit_push_action (edit, COLUMN_OFF);
2302 column_highlighting = 1;
2303 edit_mark_cmd (edit, 0);
2304 break;
2305 case CK_Unmark:
2306 if (column_highlighting)
2307 edit_push_action (edit, COLUMN_ON);
2308 column_highlighting = 0;
2309 edit_mark_cmd (edit, 1);
2310 break;
2312 case CK_Toggle_Bookmark:
2313 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2314 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2315 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2316 else
2317 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2318 break;
2319 case CK_Flush_Bookmarks:
2320 book_mark_flush (edit, BOOK_MARK_COLOR);
2321 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2322 edit->force |= REDRAW_PAGE;
2323 break;
2324 case CK_Next_Bookmark:
2325 if (edit->book_mark) {
2326 struct _book_mark *p;
2327 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
2328 if (p->next) {
2329 p = p->next;
2330 if (p->line >= edit->start_line + edit->num_widget_lines || p->line < edit->start_line)
2331 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
2332 edit_move_to_line (edit, p->line);
2335 break;
2336 case CK_Prev_Bookmark:
2337 if (edit->book_mark) {
2338 struct _book_mark *p;
2339 p = (struct _book_mark *) book_mark_find (edit, edit->curs_line);
2340 while (p->line == edit->curs_line)
2341 if (p->prev)
2342 p = p->prev;
2343 if (p->line >= 0) {
2344 if (p->line >= edit->start_line + edit->num_widget_lines || p->line < edit->start_line)
2345 edit_move_display (edit, p->line - edit->num_widget_lines / 2);
2346 edit_move_to_line (edit, p->line);
2349 break;
2351 case CK_Beginning_Of_Text:
2352 case CK_Beginning_Of_Text_Highlight:
2353 edit_move_to_top (edit);
2354 break;
2355 case CK_End_Of_Text:
2356 case CK_End_Of_Text_Highlight:
2357 edit_move_to_bottom (edit);
2358 break;
2360 case CK_Copy:
2361 edit_block_copy_cmd (edit);
2362 break;
2363 case CK_Remove:
2364 edit_block_delete_cmd (edit);
2365 break;
2366 case CK_Move:
2367 edit_block_move_cmd (edit);
2368 break;
2370 case CK_XStore:
2371 edit_copy_to_X_buf_cmd (edit);
2372 break;
2373 case CK_XCut:
2374 edit_cut_to_X_buf_cmd (edit);
2375 break;
2376 case CK_XPaste:
2377 edit_paste_from_X_buf_cmd (edit);
2378 break;
2379 case CK_Selection_History:
2380 edit_paste_from_history (edit);
2381 break;
2383 case CK_Save_As:
2384 edit_save_as_cmd (edit);
2385 break;
2386 case CK_Save:
2387 edit_save_confirm_cmd (edit);
2388 break;
2389 case CK_Load:
2390 edit_load_cmd (edit);
2391 break;
2392 case CK_Save_Block:
2393 edit_save_block_cmd (edit);
2394 break;
2395 case CK_Insert_File:
2396 edit_insert_file_cmd (edit);
2397 break;
2399 case CK_Find:
2400 edit_search_cmd (edit, 0);
2401 break;
2402 case CK_Find_Again:
2403 edit_search_cmd (edit, 1);
2404 break;
2405 case CK_Replace:
2406 edit_replace_cmd (edit, 0);
2407 break;
2408 case CK_Replace_Again:
2409 edit_replace_cmd (edit, 1);
2410 break;
2411 case CK_Complete_Word:
2412 edit_complete_word_cmd (edit);
2413 break;
2415 case CK_Exit:
2416 edit_quit_cmd (edit);
2417 break;
2418 case CK_New:
2419 edit_new_cmd (edit);
2420 break;
2422 case CK_Help:
2423 edit_help_cmd (edit);
2424 break;
2426 case CK_Refresh:
2427 edit_refresh_cmd (edit);
2428 break;
2430 case CK_Date:{
2431 time_t t;
2432 #ifdef HAVE_STRFTIME
2433 char s[1024];
2434 static const char time_format[] = "%c";
2435 #endif
2436 time (&t);
2437 #ifdef HAVE_STRFTIME
2438 strftime (s, sizeof (s), time_format, localtime (&t));
2439 edit_print_string (edit, s);
2440 #else
2441 edit_print_string (edit, ctime (&t));
2442 #endif
2443 edit->force |= REDRAW_PAGE;
2444 break;
2446 case CK_Goto:
2447 edit_goto_cmd (edit);
2448 break;
2449 case CK_Paragraph_Format:
2450 format_paragraph (edit, 1);
2451 edit->force |= REDRAW_PAGE;
2452 break;
2453 case CK_Delete_Macro:
2454 edit_delete_macro_cmd (edit);
2455 break;
2456 case CK_Match_Bracket:
2457 edit_goto_matching_bracket (edit);
2458 break;
2459 case CK_User_Menu:
2460 if (edit_one_file) {
2461 message (1, MSG_ERROR, _("User menu available only in mcedit invoked from mc"));
2462 break;
2464 else
2465 user_menu (edit);
2466 break;
2467 case CK_Sort:
2468 edit_sort_cmd (edit);
2469 break;
2470 case CK_Mail:
2471 edit_mail_dialog (edit);
2472 break;
2473 case CK_Shell:
2474 view_other_cmd ();
2475 break;
2477 /* These commands are not handled and must be handled by the user application */
2478 #if 0
2479 case CK_Sort:
2480 case CK_Mail:
2481 case CK_Find_File:
2482 case CK_Ctags:
2483 case CK_Terminal:
2484 case CK_Terminal_App:
2485 #endif
2486 case CK_Complete:
2487 case CK_Cancel:
2488 case CK_Save_Desktop:
2489 case CK_New_Window:
2490 case CK_Cycle:
2491 case CK_Save_And_Quit:
2492 case CK_Check_Save_And_Quit:
2493 case CK_Run_Another:
2494 case CK_Debug_Start:
2495 case CK_Debug_Stop:
2496 case CK_Debug_Toggle_Break:
2497 case CK_Debug_Clear:
2498 case CK_Debug_Next:
2499 case CK_Debug_Step:
2500 case CK_Debug_Back_Trace:
2501 case CK_Debug_Continue:
2502 case CK_Debug_Enter_Command:
2503 case CK_Debug_Until_Curser:
2504 result = 0;
2505 break;
2506 case CK_Menu:
2507 result = 0;
2508 break;
2511 /* CK_Pipe_Block */
2512 if ((command / 1000) == 1) /* a shell command */
2513 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
2514 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
2515 struct macro m[MAX_MACRO_LENGTH];
2516 int nm;
2517 if ((result = edit_load_macro_cmd (edit, m, &nm, command - 2000)))
2518 edit_execute_macro (edit, m, nm);
2521 /* keys which must set the col position, and the search vars */
2522 switch (command) {
2523 case CK_Find:
2524 case CK_Find_Again:
2525 case CK_Replace:
2526 case CK_Replace_Again:
2527 case CK_Complete_Word:
2528 edit->prev_col = edit_get_col (edit);
2529 return 1;
2530 break;
2531 case CK_Up:
2532 case CK_Up_Highlight:
2533 case CK_Down:
2534 case CK_Down_Highlight:
2535 case CK_Page_Up:
2536 case CK_Page_Up_Highlight:
2537 case CK_Page_Down:
2538 case CK_Page_Down_Highlight:
2539 case CK_Beginning_Of_Text:
2540 case CK_Beginning_Of_Text_Highlight:
2541 case CK_End_Of_Text:
2542 case CK_End_Of_Text_Highlight:
2543 case CK_Paragraph_Up:
2544 case CK_Paragraph_Up_Highlight:
2545 case CK_Paragraph_Down:
2546 case CK_Paragraph_Down_Highlight:
2547 case CK_Scroll_Up:
2548 case CK_Scroll_Up_Highlight:
2549 case CK_Scroll_Down:
2550 case CK_Scroll_Down_Highlight:
2551 edit->search_start = edit->curs1;
2552 edit->found_len = 0;
2553 edit_find_bracket (edit);
2554 edit_check_spelling (edit);
2555 return 1;
2556 break;
2557 default:
2558 edit->found_len = 0;
2559 edit->prev_col = edit_get_col (edit);
2560 edit->search_start = edit->curs1;
2562 edit_find_bracket (edit);
2563 edit_check_spelling (edit);
2565 if (option_auto_para_formatting) {
2566 switch (command) {
2567 case CK_BackSpace:
2568 case CK_Delete:
2569 case CK_Delete_Word_Left:
2570 case CK_Delete_Word_Right:
2571 case CK_Delete_To_Line_End:
2572 case CK_Delete_To_Line_Begin:
2573 format_paragraph (edit, 0);
2574 edit->force |= REDRAW_PAGE;
2577 return result;
2581 /* either command or char_for_insertion must be passed as -1 */
2582 /* returns 0 if command is a macro that was not found, 1 otherwise */
2583 int edit_execute_command (WEdit * edit, int command, int char_for_insertion)
2585 int r;
2586 r = edit_execute_cmd (edit, command, char_for_insertion);
2587 edit_update_screen (edit);
2588 return r;
2591 void edit_execute_macro (WEdit * edit, struct macro macro[], int n)
2593 int i = 0;
2594 edit->force |= REDRAW_PAGE;
2595 for (; i < n; i++) {
2596 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
2598 edit_update_screen (edit);
2601 /* User edit menu, like user menu (F2) but only in editor. */
2602 void
2603 user_menu (WEdit * edit)
2605 FILE *fd;
2606 int nomark;
2607 struct stat status;
2608 long start_mark, end_mark;
2609 char *block_file = catstrs (home_dir, BLOCK_FILE, 0);
2610 int rc = 0;
2612 nomark = eval_marks (edit, &start_mark, &end_mark);
2613 if (!nomark) /* remember marked or not */
2614 edit_save_block (edit, block_file, start_mark, end_mark);
2616 /* run shell scripts from menu */
2617 user_menu_cmd (edit);
2619 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
2620 /* no block messages */
2621 return;
2624 if (!nomark) {
2625 /* i.e. we have marked block */
2626 rc = edit_block_delete_cmd (edit);
2629 if (!rc) {
2630 edit_insert_file (edit, block_file);
2633 /* truncate block file */
2634 if ((fd = fopen (block_file, "w"))) {
2635 fclose (fd);
2638 edit_refresh_cmd (edit);
2639 edit->force |= REDRAW_COMPLETELY;
2640 return;