edit.c: Some code refactoring&cleanup
[midnight-commander.git] / src / editor / edit.c
blob4f1952515c4b911c549326a60cff2b2d439bca4d
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor low level data handling and cursor fundamentals
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "src/charsets.h" /* get_codepage_id */
57 #include "src/main.h" /* source_codepage */
58 #include "src/learn.h" /* learn_keys */
59 #include "src/cmddef.h"
61 #include "edit-impl.h"
62 #include "editlock.h"
63 #include "edit-widget.h"
66 int option_word_wrap_line_length = 72;
67 int option_typewriter_wrap = 0;
68 int option_auto_para_formatting = 0;
69 int option_fill_tabs_with_spaces = 0;
70 int option_return_does_auto_indent = 1;
71 int option_backspace_through_tabs = 0;
72 int option_fake_half_tabs = 1;
73 int option_save_mode = EDIT_QUICK_SAVE;
74 int option_save_position = 1;
75 int option_max_undo = 32768;
76 int option_persistent_selections = 1;
77 int option_cursor_beyond_eol = 0;
78 int option_line_state = 0;
79 int option_line_state_width = 0;
81 int option_edit_right_extreme = 0;
82 int option_edit_left_extreme = 0;
83 int option_edit_top_extreme = 0;
84 int option_edit_bottom_extreme = 0;
85 int enable_show_tabs_tws = 1;
86 int option_check_nl_at_eof = 0;
87 int show_right_margin = 0;
89 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext = NULL;
92 int edit_stack_iterator = 0;
93 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
96 /*-
98 * here's a quick sketch of the layout: (don't run this through indent.)
100 * (b1 is buffers1 and b2 is buffers2)
103 * \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
104 * ______________________________________|______________________________________
106 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
107 * |-> |-> |-> |-> |-> |-> |
109 * _<------------------------->|<----------------->_
110 * WEdit->curs2 | WEdit->curs1
111 * ^ | ^
112 * | ^|^ |
113 * cursor ||| cursor
114 * |||
115 * file end|||file beginning
120 * This_is_some_file
121 * fin.
124 const global_keymap_t *editor_map;
125 const global_keymap_t *editor_x_map;
127 static void user_menu (WEdit *edit);
129 int edit_get_byte (WEdit * edit, long byte_index)
131 unsigned long p;
132 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
133 return '\n';
135 if (byte_index >= edit->curs1) {
136 p = edit->curs1 + edit->curs2 - byte_index - 1;
137 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
138 } else {
139 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
143 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
145 unsigned long p;
146 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
147 return NULL;
149 if (byte_index >= edit->curs1) {
150 p = edit->curs1 + edit->curs2 - byte_index - 1;
151 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
152 } else {
153 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
157 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
159 unsigned long p;
161 if (byte_index >= (edit->curs1 + edit->curs2))
162 byte_index--;
164 if (byte_index < 0)
165 return NULL;
167 if (byte_index >= edit->curs1) {
168 p = edit->curs1 + edit->curs2 - 1;
169 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
170 } else {
171 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
175 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
177 gchar *str = NULL;
178 int res = -1;
179 gunichar ch;
180 gchar *next_ch = NULL;
181 int width = 0;
183 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
184 *char_width = 0;
185 return '\n';
188 str = edit_get_byte_ptr (edit, byte_index);
190 if (str == NULL) {
191 *char_width = 0;
192 return 0;
195 res = g_utf8_get_char_validated (str, -1);
197 if (res < 0) {
198 ch = *str;
199 width = 0;
200 } else {
201 ch = res;
202 /* Calculate UTF-8 char width */
203 next_ch = g_utf8_next_char (str);
204 if (next_ch) {
205 width = next_ch - str;
206 } else {
207 ch = 0;
208 width = 0;
211 *char_width = width;
212 return ch;
215 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
217 gchar *str, *buf = NULL;
218 int res = -1;
219 gunichar ch;
220 gchar *next_ch = NULL;
221 int width = 0;
223 if (byte_index > 0)
224 byte_index--;
226 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
227 *char_width = 0;
228 return 0;
231 ch = edit_get_utf (edit, byte_index, &width);
233 if (width == 1) {
234 *char_width = width;
235 return ch;
238 str = edit_get_byte_ptr (edit, byte_index);
239 buf = edit_get_buf_ptr (edit, byte_index);
240 if (str == NULL || buf == NULL) {
241 *char_width = 0;
242 return 0;
244 /* get prev utf8 char */
245 if (str != buf)
246 str = g_utf8_find_prev_char (buf, str);
248 res = g_utf8_get_char_validated (str, -1);
250 if (res < 0) {
251 ch = *str;
252 width = 0;
253 } else {
254 ch = res;
255 /* Calculate UTF-8 char width */
256 next_ch = g_utf8_next_char(str);
257 if (next_ch) {
258 width = next_ch - str;
259 } else {
260 ch = 0;
261 width = 0;
264 *char_width = width;
265 return ch;
269 * Initialize the buffers for an empty files.
271 static void
272 edit_init_buffers (WEdit *edit)
274 int j;
276 for (j = 0; j <= MAXBUFF; j++) {
277 edit->buffers1[j] = NULL;
278 edit->buffers2[j] = NULL;
281 edit->curs1 = 0;
282 edit->curs2 = 0;
283 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
287 * Load file OR text into buffers. Set cursor to the beginning of file.
288 * Return 1 on error.
290 static int
291 edit_load_file_fast (WEdit *edit, const char *filename)
293 long buf, buf2;
294 int file = -1;
295 edit->curs2 = edit->last_byte;
296 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
297 edit->utf8 = 0;
298 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
299 GString *errmsg = g_string_new(NULL);
300 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
301 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
302 g_string_free (errmsg, TRUE);
303 return 1;
306 if (!edit->buffers2[buf2])
307 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
309 mc_read (file,
310 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
311 (edit->curs2 & M_EDIT_BUF_SIZE),
312 edit->curs2 & M_EDIT_BUF_SIZE);
314 for (buf = buf2 - 1; buf >= 0; buf--) {
315 /* edit->buffers2[0] is already allocated */
316 if (!edit->buffers2[buf])
317 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
318 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
321 mc_close (file);
322 return 0;
325 /* detecting an error on save is easy: just check if every byte has been written. */
326 /* detecting an error on read, is not so easy 'cos there is not way to tell
327 whether you read everything or not. */
328 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
329 static const struct edit_filters {
330 const char *read, *write, *extension;
331 } all_filters[] = {
332 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
333 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
334 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
335 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
336 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
339 /* Return index of the filter or -1 is there is no appropriate filter */
340 static int edit_find_filter (const char *filename)
342 size_t i, l, e;
344 if (filename == NULL)
345 return -1;
347 l = strlen (filename);
348 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
349 e = strlen (all_filters[i].extension);
350 if (l > e)
351 if (!strcmp (all_filters[i].extension, filename + l - e))
352 return i;
354 return -1;
357 static char *
358 edit_get_filter (const char *filename)
360 int i;
361 char *p, *quoted_name;
363 i = edit_find_filter (filename);
364 if (i < 0)
365 return NULL;
367 quoted_name = name_quote (filename, 0);
368 p = g_strdup_printf(all_filters[i].read, quoted_name);
369 g_free (quoted_name);
370 return p;
373 char *
374 edit_get_write_filter (const char *write_name, const char *filename)
376 int i;
377 char *p, *writename;
379 i = edit_find_filter (filename);
380 if (i < 0)
381 return NULL;
383 writename = name_quote (write_name, 0);
384 p = g_strdup_printf(all_filters[i].write, writename);
385 g_free (writename);
386 return p;
389 static long
390 edit_insert_stream (WEdit * edit, FILE * f)
392 int c;
393 long i = 0;
394 while ((c = fgetc (f)) >= 0) {
395 edit_insert (edit, c);
396 i++;
398 return i;
401 long edit_write_stream (WEdit * edit, FILE * f)
403 long i;
405 if (edit->lb == LB_ASIS) {
406 for (i = 0; i < edit->last_byte; i++)
407 if (fputc (edit_get_byte (edit, i), f) < 0)
408 break;
409 return i;
412 /* change line breaks */
413 for (i = 0; i < edit->last_byte; i++) {
414 unsigned char c = edit_get_byte (edit, i);
416 if (!(c == '\n' || c == '\r')) {
417 /* not line break */
418 if (fputc (c, f) < 0)
419 return i;
420 } else { /* (c == '\n' || c == '\r') */
421 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
423 switch (edit->lb) {
424 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
425 /* put one line break unconditionally */
426 if (fputc ('\n', f) < 0)
427 return i;
429 i++; /* 2 chars are processed */
431 if (c == '\r' && c1 == '\n')
432 /* Windows line break; go to the next char */
433 break;
435 if (c == '\r' && c1 == '\r') {
436 /* two Macintosh line breaks; put second line break */
437 if (fputc ('\n', f) < 0)
438 return i;
439 break;
442 if (fputc (c1, f) < 0)
443 return i;
444 break;
446 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
447 /* put one line break unconditionally */
448 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
449 return i;
451 if (c == '\r' && c1 == '\n')
452 /* Windows line break; go to the next char */
453 i++;
454 break;
456 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
457 /* put one line break unconditionally */
458 if (fputc ('\r', f) < 0)
459 return i;
461 i++; /* 2 chars are processed */
463 if (c == '\r' && c1 == '\n')
464 /* Windows line break; go to the next char */
465 break;
467 if (c == '\n' && c1 == '\n') {
468 /* two Windows line breaks; put second line break */
469 if (fputc ('\r', f) < 0)
470 return i;
471 break;
474 if (fputc (c1, f) < 0)
475 return i;
476 break;
477 case LB_ASIS: /* default without changes */
478 break;
483 return edit->last_byte;
486 #define TEMP_BUF_LEN 1024
488 /* inserts a file at the cursor, returns 1 on success */
490 edit_insert_file (WEdit *edit, const char *filename)
492 char *p;
494 p = edit_get_filter (filename);
495 if (p != NULL) {
496 FILE *f;
497 long current = edit->curs1;
498 f = (FILE *) popen (p, "r");
499 if (f != NULL) {
500 edit_insert_stream (edit, f);
501 edit_cursor_move (edit, current - edit->curs1);
502 if (pclose (f) > 0) {
503 char *errmsg;
504 errmsg = g_strdup_printf(_(" Error reading from pipe: %s "), p);
505 edit_error_dialog (_("Error"), errmsg);
506 g_free (errmsg);
507 g_free (p);
508 return 0;
510 } else {
511 char *errmsg;
512 errmsg = g_strdup_printf (_(" Cannot open pipe for reading: %s "), p);
513 edit_error_dialog (_("Error"), errmsg);
514 g_free (errmsg);
515 g_free (p);
516 return 0;
518 g_free (p);
519 } else {
520 int i, file, blocklen;
521 long current = edit->curs1;
522 int vertical_insertion = 0;
523 char *buf;
524 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
525 return 0;
526 buf = g_malloc0 (TEMP_BUF_LEN);
527 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
528 if (blocklen > 0) {
529 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
530 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
531 vertical_insertion = 1;
532 } else {
533 mc_lseek (file, 0, SEEK_SET);
536 if (vertical_insertion) {
537 blocklen = edit_insert_column_of_text_from_file (edit, file);
538 } else {
539 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
540 for (i = 0; i < blocklen; i++)
541 edit_insert (edit, buf[i]);
544 edit_cursor_move (edit, current - edit->curs1);
545 g_free (buf);
546 mc_close (file);
547 if (blocklen)
548 return 0;
550 return 1;
553 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
554 static int
555 check_file_access (WEdit *edit, const char *filename, struct stat *st)
557 int file;
558 GString *errmsg = (GString *) 0;
560 /* Try opening an existing file */
561 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
563 if (file < 0) {
565 * Try creating the file. O_EXCL prevents following broken links
566 * and opening existing files.
568 file =
569 mc_open (filename,
570 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
571 0666);
572 if (file < 0) {
573 g_string_sprintf (errmsg = g_string_new (NULL),
574 _(" Cannot open %s for reading "), filename);
575 goto cleanup;
576 } else {
577 /* New file, delete it if it's not modified or saved */
578 edit->delete_file = 1;
582 /* Check what we have opened */
583 if (mc_fstat (file, st) < 0) {
584 g_string_sprintf (errmsg = g_string_new (NULL),
585 _(" Cannot get size/permissions for %s "), filename);
586 goto cleanup;
589 /* We want to open regular files only */
590 if (!S_ISREG (st->st_mode)) {
591 g_string_sprintf (errmsg = g_string_new (NULL),
592 _(" %s is not a regular file "), filename);
593 goto cleanup;
597 * Don't delete non-empty files.
598 * O_EXCL should prevent it, but let's be on the safe side.
600 if (st->st_size > 0) {
601 edit->delete_file = 0;
604 if (st->st_size >= SIZE_LIMIT) {
605 g_string_sprintf (errmsg = g_string_new (NULL),
606 _(" File %s is too large "), filename);
609 cleanup:
610 (void) mc_close (file);
611 if (errmsg) {
612 edit_error_dialog (_("Error"), errmsg->str);
613 g_string_free (errmsg, TRUE);
614 return 1;
616 return 0;
620 * Open the file and load it into the buffers, either directly or using
621 * a filter. Return 0 on success, 1 on error.
623 * Fast loading (edit_load_file_fast) is used when the file size is
624 * known. In this case the data is read into the buffers by blocks.
625 * If the file size is not known, the data is loaded byte by byte in
626 * edit_insert_file.
628 static int
629 edit_load_file (WEdit *edit)
631 int fast_load = 1;
633 /* Cannot do fast load if a filter is used */
634 if (edit_find_filter (edit->filename) >= 0)
635 fast_load = 0;
638 * VFS may report file size incorrectly, and slow load is not a big
639 * deal considering overhead in VFS.
641 if (!vfs_file_is_local (edit->filename))
642 fast_load = 0;
645 * FIXME: line end translation should disable fast loading as well
646 * Consider doing fseek() to the end and ftell() for the real size.
649 if (*edit->filename) {
650 /* If we are dealing with a real file, check that it exists */
651 if (check_file_access (edit, edit->filename, &edit->stat1))
652 return 1;
653 } else {
654 /* nothing to load */
655 fast_load = 0;
658 edit_init_buffers (edit);
660 if (fast_load) {
661 edit->last_byte = edit->stat1.st_size;
662 edit_load_file_fast (edit, edit->filename);
663 /* If fast load was used, the number of lines wasn't calculated */
664 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
665 } else {
666 #ifdef HAVE_CHARSET
667 const char *codepage_id;
668 #endif
669 edit->last_byte = 0;
670 if (*edit->filename) {
671 edit->stack_disable = 1;
672 if (!edit_insert_file (edit, edit->filename)) {
673 edit_clean (edit);
674 return 1;
676 edit->stack_disable = 0;
679 #ifdef HAVE_CHARSET
680 codepage_id = get_codepage_id( source_codepage );
681 if ( codepage_id )
682 edit->utf8 = str_isutf8 ( codepage_id );
683 #endif
685 edit->lb = LB_ASIS;
686 return 0;
689 /* Restore saved cursor position in the file */
690 static void
691 edit_load_position (WEdit *edit)
693 char *filename;
694 long line, column;
695 off_t offset;
697 if (!edit->filename || !*edit->filename)
698 return;
700 filename = vfs_canon (edit->filename);
701 load_file_position (filename, &line, &column, &offset);
702 g_free (filename);
704 if (line > 0) {
705 edit_move_to_line (edit, line - 1);
706 edit->prev_col = column;
707 } else if (offset > 0) {
708 edit_cursor_move (edit, offset);
709 line = edit->curs_line;
711 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
712 edit_move_display (edit, line - (edit->num_widget_lines / 2));
715 /* Save cursor position in the file */
716 static void
717 edit_save_position (WEdit *edit)
719 char *filename;
721 if (!edit->filename || !*edit->filename)
722 return;
724 filename = vfs_canon (edit->filename);
725 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
726 g_free (filename);
729 /* Clean the WEdit stricture except the widget part */
730 static void
731 edit_purge_widget (WEdit *edit)
733 size_t len = sizeof (WEdit) - sizeof (Widget);
734 char *start = (char *) edit + sizeof (Widget);
735 memset (start, 0, len);
736 edit->macro_i = -1; /* not recording a macro */
739 static void
740 edit_set_keymap (void)
742 editor_map = default_editor_keymap;
743 if (editor_keymap && editor_keymap->len > 0)
744 editor_map = (global_keymap_t *) editor_keymap->data;
746 editor_x_map = default_editor_x_keymap;
747 if (editor_x_keymap && editor_x_keymap->len > 0)
748 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
752 #define space_width 1
755 * Fill in the edit structure. Return NULL on failure. Pass edit as
756 * NULL to allocate a new structure.
758 * If line is 0, try to restore saved position. Otherwise put the
759 * cursor on that line and show it in the middle of the screen.
761 WEdit *
762 edit_init (WEdit *edit, int lines, int columns, const char *filename,
763 long line)
765 int to_free = 0;
766 option_auto_syntax = 1; /* Resetting to auto on every invokation */
767 if ( option_line_state ) {
768 option_line_state_width = LINE_STATE_WIDTH;
769 } else {
770 option_line_state_width = 0;
772 if (!edit) {
773 #ifdef ENABLE_NLS
775 * Expand option_whole_chars_search by national letters using
776 * current locale
779 static char option_whole_chars_search_buf[256];
781 if (option_whole_chars_search_buf != option_whole_chars_search) {
782 size_t i;
783 size_t len = str_term_width1 (option_whole_chars_search);
785 strcpy (option_whole_chars_search_buf,
786 option_whole_chars_search);
788 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
789 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
790 option_whole_chars_search_buf[len++] = i;
794 option_whole_chars_search_buf[len] = 0;
795 option_whole_chars_search = option_whole_chars_search_buf;
797 #endif /* ENABLE_NLS */
798 edit = g_malloc0 (sizeof (WEdit));
799 edit->search = NULL;
800 to_free = 1;
802 edit_purge_widget (edit);
803 edit->num_widget_lines = lines;
804 edit->over_col = 0;
805 edit->num_widget_columns = columns;
806 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
807 edit->stat1.st_uid = getuid ();
808 edit->stat1.st_gid = getgid ();
809 edit->stat1.st_mtime = 0;
810 edit->bracket = -1;
811 edit->force |= REDRAW_PAGE;
812 edit_set_filename (edit, filename);
813 edit->stack_size = START_STACK_SIZE;
814 edit->stack_size_mask = START_STACK_SIZE - 1;
815 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
816 if (edit_load_file (edit)) {
817 /* edit_load_file already gives an error message */
818 if (to_free)
819 g_free (edit);
820 return 0;
822 edit->utf8 = 0;
823 edit->converter = str_cnv_from_term;
824 #ifdef HAVE_CHARSET
826 const char *cp_id = NULL;
827 cp_id = get_codepage_id (source_codepage >= 0 ?
828 source_codepage : display_codepage);
830 if (cp_id != NULL) {
831 GIConv conv;
832 conv = str_crt_conv_from (cp_id);
833 if (conv != INVALID_CONV) {
834 if (edit->converter != str_cnv_from_term)
835 str_close_conv (edit->converter);
836 edit->converter = conv;
839 if (cp_id != NULL)
840 edit->utf8 = str_isutf8 (cp_id);
842 #endif
844 edit->loading_done = 1;
845 edit->modified = 0;
846 edit->locked = 0;
847 edit_load_syntax (edit, 0, 0);
849 int color;
850 edit_get_syntax_color (edit, -1, &color);
853 /* load saved cursor position */
854 if ((line == 0) && option_save_position) {
855 edit_load_position (edit);
856 } else {
857 if (line <= 0)
858 line = 1;
859 edit_move_display (edit, line - 1);
860 edit_move_to_line (edit, line - 1);
863 edit_set_keymap ();
865 return edit;
868 /* Clear the edit struct, freeing everything in it. Return 1 on success */
870 edit_clean (WEdit *edit)
872 int j = 0;
874 if (!edit)
875 return 0;
877 /* a stale lock, remove it */
878 if (edit->locked)
879 edit->locked = edit_unlock_file (edit->filename);
881 /* save cursor position */
882 if (option_save_position)
883 edit_save_position (edit);
885 /* File specified on the mcedit command line and never saved */
886 if (edit->delete_file)
887 unlink (edit->filename);
889 edit_free_syntax_rules (edit);
890 book_mark_flush (edit, -1);
891 for (; j <= MAXBUFF; j++) {
892 g_free (edit->buffers1[j]);
893 g_free (edit->buffers2[j]);
896 g_free (edit->undo_stack);
897 g_free (edit->filename);
898 g_free (edit->dir);
900 if (edit->search)
902 mc_search_free(edit->search);
903 edit->search = NULL;
905 edit_purge_widget (edit);
907 return 1;
910 /* returns 1 on success */
912 edit_renew (WEdit * edit)
914 int lines = edit->num_widget_lines;
915 int columns = edit->num_widget_columns;
917 edit_clean (edit);
918 return (edit_init (edit, lines, columns, "", 0) != NULL);
922 * Load a new file into the editor. If it fails, preserve the old file.
923 * To do it, allocate a new widget, initialize it and, if the new file
924 * was loaded, copy the data to the old widget.
925 * Return 1 on success, 0 on failure.
928 edit_reload (WEdit *edit, const char *filename)
930 WEdit *e;
931 int lines = edit->num_widget_lines;
932 int columns = edit->num_widget_columns;
934 e = g_malloc0 (sizeof (WEdit));
935 e->widget = edit->widget;
936 if (!edit_init (e, lines, columns, filename, 0)) {
937 g_free (e);
938 return 0;
940 edit_clean (edit);
941 memcpy (edit, e, sizeof (WEdit));
942 g_free (e);
943 return 1;
947 * Load a new file into the editor and set line. If it fails, preserve the old file.
948 * To do it, allocate a new widget, initialize it and, if the new file
949 * was loaded, copy the data to the old widget.
950 * Return 1 on success, 0 on failure.
953 edit_reload_line (WEdit *edit, const char *filename, long line)
955 WEdit *e;
956 int lines = edit->num_widget_lines;
957 int columns = edit->num_widget_columns;
959 e = g_malloc0 (sizeof (WEdit));
960 e->widget = edit->widget;
961 if (!edit_init (e, lines, columns, filename, line)) {
962 g_free (e);
963 return 0;
965 edit_clean (edit);
966 memcpy (edit, e, sizeof (WEdit));
967 g_free (e);
968 return 1;
973 Recording stack for undo:
974 The following is an implementation of a compressed stack. Identical
975 pushes are recorded by a negative prefix indicating the number of times the
976 same char was pushed. This saves space for repeated curs-left or curs-right
977 delete etc.
981 pushed: stored:
985 b -3
987 c --> -4
993 If the stack long int is 0-255 it represents a normal insert (from a backspace),
994 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
995 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
996 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
997 position.
999 The only way the cursor moves or the buffer is changed is through the routines:
1000 insert, backspace, insert_ahead, delete, and cursor_move.
1001 These record the reverse undo movements onto the stack each time they are
1002 called.
1004 Each key press results in a set of actions (insert; delete ...). So each time
1005 a key is pressed the current position of start_display is pushed as
1006 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1007 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1008 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1012 void edit_push_action (WEdit * edit, long c,...)
1014 unsigned long sp = edit->stack_pointer;
1015 unsigned long spm1;
1016 long *t;
1018 /* first enlarge the stack if necessary */
1019 if (sp > edit->stack_size - 10) { /* say */
1020 if (option_max_undo < 256)
1021 option_max_undo = 256;
1022 if (edit->stack_size < (unsigned long) option_max_undo) {
1023 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1024 if (t) {
1025 edit->undo_stack = t;
1026 edit->stack_size <<= 1;
1027 edit->stack_size_mask = edit->stack_size - 1;
1031 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1032 if (edit->stack_disable)
1033 return;
1035 #ifdef FAST_MOVE_CURSOR
1036 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1037 va_list ap;
1038 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1039 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1040 va_start (ap, c);
1041 c = -(va_arg (ap, int));
1042 va_end (ap);
1043 } else
1044 #endif /* ! FAST_MOVE_CURSOR */
1045 if (edit->stack_bottom != sp
1046 && spm1 != edit->stack_bottom
1047 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1048 int d;
1049 if (edit->undo_stack[spm1] < 0) {
1050 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1051 if (d == c) {
1052 if (edit->undo_stack[spm1] > -1000000000) {
1053 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1054 edit->undo_stack[spm1]--;
1055 return;
1058 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1059 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1060 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1061 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1062 if (edit->undo_stack[spm1] == -2)
1063 edit->stack_pointer = spm1;
1064 else
1065 edit->undo_stack[spm1]++;
1066 return;
1068 #endif
1069 } else {
1070 d = edit->undo_stack[spm1];
1071 if (d == c) {
1072 if (c >= KEY_PRESS)
1073 return; /* --> no need to push multiple do-nothings */
1074 edit->undo_stack[sp] = -2;
1075 goto check_bottom;
1077 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1078 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1079 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1080 edit->stack_pointer = spm1;
1081 return;
1083 #endif
1086 edit->undo_stack[sp] = c;
1088 check_bottom:
1089 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1091 /* if the sp wraps round and catches the stack_bottom then erase
1092 * the first set of actions on the stack to make space - by moving
1093 * stack_bottom forward one "key press" */
1094 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1095 if ((unsigned long) c == edit->stack_bottom ||
1096 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1097 do {
1098 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1099 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1101 /*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: */
1102 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1103 edit->stack_bottom = edit->stack_pointer = 0;
1107 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1108 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1110 static long
1111 pop_action (WEdit * edit)
1113 long c;
1114 unsigned long sp = edit->stack_pointer;
1115 if (sp == edit->stack_bottom) {
1116 return STACK_BOTTOM;
1118 sp = (sp - 1) & edit->stack_size_mask;
1119 if ((c = edit->undo_stack[sp]) >= 0) {
1120 /* edit->undo_stack[sp] = '@'; */
1121 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1122 return c;
1124 if (sp == edit->stack_bottom) {
1125 return STACK_BOTTOM;
1127 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1128 if (edit->undo_stack[sp] == -2) {
1129 /* edit->undo_stack[sp] = '@'; */
1130 edit->stack_pointer = sp;
1131 } else
1132 edit->undo_stack[sp]++;
1134 return c;
1137 /* is called whenever a modification is made by one of the four routines below */
1138 static void edit_modification (WEdit * edit)
1140 edit->caches_valid = 0;
1141 edit->screen_modified = 1;
1143 /* raise lock when file modified */
1144 if (!edit->modified && !edit->delete_file)
1145 edit->locked = edit_lock_file (edit->filename);
1146 edit->modified = 1;
1150 Basic low level single character buffer alterations and movements at the cursor.
1151 Returns char passed over, inserted or removed.
1154 void
1155 edit_insert (WEdit *edit, int c)
1157 /* check if file has grown to large */
1158 if (edit->last_byte >= SIZE_LIMIT)
1159 return;
1161 /* first we must update the position of the display window */
1162 if (edit->curs1 < edit->start_display) {
1163 edit->start_display++;
1164 if (c == '\n')
1165 edit->start_line++;
1168 /* Mark file as modified, unless the file hasn't been fully loaded */
1169 if (edit->loading_done) {
1170 edit_modification (edit);
1173 /* now we must update some info on the file and check if a redraw is required */
1174 if (c == '\n') {
1175 if (edit->book_mark)
1176 book_mark_inc (edit, edit->curs_line);
1177 edit->curs_line++;
1178 edit->total_lines++;
1179 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1182 /* save the reverse command onto the undo stack */
1183 edit_push_action (edit, BACKSPACE);
1185 /* update markers */
1186 edit->mark1 += (edit->mark1 > edit->curs1);
1187 edit->mark2 += (edit->mark2 > edit->curs1);
1188 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1190 /* add a new buffer if we've reached the end of the last one */
1191 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1192 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1193 g_malloc0 (EDIT_BUF_SIZE);
1195 /* perform the insertion */
1196 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1197 curs1 & M_EDIT_BUF_SIZE]
1198 = (unsigned char) c;
1200 /* update file length */
1201 edit->last_byte++;
1203 /* update cursor position */
1204 edit->curs1++;
1207 static void
1208 edit_insert_over (WEdit * edit)
1210 int i;
1212 for ( i = 0; i < edit->over_col; i++ ) {
1213 edit_insert (edit, ' ');
1215 edit->over_col = 0;
1218 /* same as edit_insert and move left */
1219 void edit_insert_ahead (WEdit * edit, int c)
1221 if (edit->last_byte >= SIZE_LIMIT)
1222 return;
1223 if (edit->curs1 < edit->start_display) {
1224 edit->start_display++;
1225 if (c == '\n')
1226 edit->start_line++;
1228 edit_modification (edit);
1229 if (c == '\n') {
1230 if (edit->book_mark)
1231 book_mark_inc (edit, edit->curs_line);
1232 edit->total_lines++;
1233 edit->force |= REDRAW_AFTER_CURSOR;
1235 edit_push_action (edit, DELCHAR);
1237 edit->mark1 += (edit->mark1 >= edit->curs1);
1238 edit->mark2 += (edit->mark2 >= edit->curs1);
1239 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1241 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1242 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1243 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1245 edit->last_byte++;
1246 edit->curs2++;
1250 int edit_delete (WEdit * edit, const int byte_delete)
1252 int p = 0;
1253 int cw = 1;
1254 int i;
1256 if (!edit->curs2)
1257 return 0;
1259 edit->mark1 -= (edit->mark1 > edit->curs1);
1260 edit->mark2 -= (edit->mark2 > edit->curs1);
1261 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1263 cw = 1;
1264 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1265 if ( edit->utf8 && byte_delete == 0 ) {
1266 edit_get_utf (edit, edit->curs1, &cw);
1267 if ( cw < 1 )
1268 cw = 1;
1270 for ( i = 1; i<= cw; i++ ) {
1271 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1273 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1274 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1275 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1277 edit->last_byte--;
1278 edit->curs2--;
1279 edit_push_action (edit, p + 256);
1282 edit_modification (edit);
1283 if (p == '\n') {
1284 if (edit->book_mark)
1285 book_mark_dec (edit, edit->curs_line);
1286 edit->total_lines--;
1287 edit->force |= REDRAW_AFTER_CURSOR;
1289 if (edit->curs1 < edit->start_display) {
1290 edit->start_display--;
1291 if (p == '\n')
1292 edit->start_line--;
1295 return p;
1299 static int
1300 edit_backspace (WEdit * edit, const int byte_delete)
1302 int p = 0;
1303 int cw = 1;
1304 int i;
1306 if (!edit->curs1)
1307 return 0;
1309 edit->mark1 -= (edit->mark1 >= edit->curs1);
1310 edit->mark2 -= (edit->mark2 >= edit->curs1);
1311 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1313 cw = 1;
1314 if ( edit->utf8 && byte_delete == 0 ) {
1315 edit_get_prev_utf (edit, edit->curs1, &cw);
1316 if ( cw < 1 )
1317 cw = 1;
1319 for ( i = 1; i<= cw; i++ ) {
1320 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1321 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1322 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1323 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1325 edit->last_byte--;
1326 edit->curs1--;
1327 edit_push_action (edit, p);
1329 edit_modification (edit);
1330 if (p == '\n') {
1331 if (edit->book_mark)
1332 book_mark_dec (edit, edit->curs_line);
1333 edit->curs_line--;
1334 edit->total_lines--;
1335 edit->force |= REDRAW_AFTER_CURSOR;
1338 if (edit->curs1 < edit->start_display) {
1339 edit->start_display--;
1340 if (p == '\n')
1341 edit->start_line--;
1344 return p;
1347 #ifdef FAST_MOVE_CURSOR
1349 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1351 unsigned long next;
1352 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1353 edit->curs_line--;
1354 next -= (unsigned long) dest;
1355 n -= next;
1356 src += next;
1357 dest += next;
1362 edit_move_backward_lots (WEdit *edit, long increment)
1364 int r, s, t;
1365 unsigned char *p = NULL;
1367 if (increment > edit->curs1)
1368 increment = edit->curs1;
1369 if (increment <= 0)
1370 return -1;
1371 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1373 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1374 if (r > increment)
1375 r = increment;
1376 s = edit->curs1 & M_EDIT_BUF_SIZE;
1378 if (s > r) {
1379 memqcpy (edit,
1380 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1381 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1383 } else {
1384 if (s) {
1385 memqcpy (edit,
1386 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1387 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1388 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1389 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1391 memqcpy (edit,
1392 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1393 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1394 EDIT_BUF_SIZE - (r - s), r - s);
1396 increment -= r;
1397 edit->curs1 -= r;
1398 edit->curs2 += r;
1399 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1400 if (p)
1401 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1402 else
1403 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1404 g_malloc0 (EDIT_BUF_SIZE);
1405 } else {
1406 g_free (p);
1409 s = edit->curs1 & M_EDIT_BUF_SIZE;
1410 while (increment) {
1411 p = 0;
1412 r = EDIT_BUF_SIZE;
1413 if (r > increment)
1414 r = increment;
1415 t = s;
1416 if (r < t)
1417 t = r;
1418 memqcpy (edit,
1419 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1420 EDIT_BUF_SIZE - t,
1421 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1423 if (r >= s) {
1424 if (t) {
1425 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1426 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1428 memqcpy (edit,
1429 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1430 EDIT_BUF_SIZE - r,
1431 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1432 EDIT_BUF_SIZE - (r - s), r - s);
1434 increment -= r;
1435 edit->curs1 -= r;
1436 edit->curs2 += r;
1437 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1438 if (p)
1439 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1440 else
1441 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1442 g_malloc0 (EDIT_BUF_SIZE);
1443 } else {
1444 g_free (p);
1447 return edit_get_byte (edit, edit->curs1);
1450 #endif /* ! FAST_MOVE_CURSOR */
1452 /* moves the cursor right or left: increment positive or negative respectively */
1453 void edit_cursor_move (WEdit * edit, long increment)
1455 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1456 int c;
1457 #ifdef FAST_MOVE_CURSOR
1458 if (increment < -256) {
1459 edit->force |= REDRAW_PAGE;
1460 edit_move_backward_lots (edit, -increment);
1461 return;
1463 #endif /* ! FAST_MOVE_CURSOR */
1465 if (increment < 0) {
1466 for (; increment < 0; increment++) {
1467 if (!edit->curs1)
1468 return;
1470 edit_push_action (edit, CURS_RIGHT);
1472 c = edit_get_byte (edit, edit->curs1 - 1);
1473 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1474 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1475 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1476 edit->curs2++;
1477 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1478 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1479 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1480 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1482 edit->curs1--;
1483 if (c == '\n') {
1484 edit->curs_line--;
1485 edit->force |= REDRAW_LINE_BELOW;
1489 } else if (increment > 0) {
1490 for (; increment > 0; increment--) {
1491 if (!edit->curs2)
1492 return;
1494 edit_push_action (edit, CURS_LEFT);
1496 c = edit_get_byte (edit, edit->curs1);
1497 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1498 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1499 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1500 edit->curs1++;
1501 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1502 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1503 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1504 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1506 edit->curs2--;
1507 if (c == '\n') {
1508 edit->curs_line++;
1509 edit->force |= REDRAW_LINE_ABOVE;
1515 /* These functions return positions relative to lines */
1517 /* returns index of last char on line + 1 */
1518 long edit_eol (WEdit * edit, long current)
1520 if (current < edit->last_byte) {
1521 for (;; current++)
1522 if (edit_get_byte (edit, current) == '\n')
1523 break;
1524 } else
1525 return edit->last_byte;
1526 return current;
1529 /* returns index of first char on line */
1530 long edit_bol (WEdit * edit, long current)
1532 if (current > 0) {
1533 for (;; current--)
1534 if (edit_get_byte (edit, current - 1) == '\n')
1535 break;
1536 } else
1537 return 0;
1538 return current;
1542 long edit_count_lines (WEdit * edit, long current, long upto)
1544 long lines = 0;
1545 if (upto > edit->last_byte)
1546 upto = edit->last_byte;
1547 if (current < 0)
1548 current = 0;
1549 while (current < upto)
1550 if (edit_get_byte (edit, current++) == '\n')
1551 lines++;
1552 return lines;
1556 /* If lines is zero this returns the count of lines from current to upto. */
1557 /* If upto is zero returns index of lines forward current. */
1558 long edit_move_forward (WEdit * edit, long current, long lines, long upto)
1560 if (upto) {
1561 return edit_count_lines (edit, current, upto);
1562 } else {
1563 long next;
1564 if (lines < 0)
1565 lines = 0;
1566 while (lines--) {
1567 next = edit_eol (edit, current) + 1;
1568 if (next > edit->last_byte)
1569 break;
1570 else
1571 current = next;
1573 return current;
1578 /* Returns offset of 'lines' lines up from current */
1579 long edit_move_backward (WEdit * edit, long current, long lines)
1581 if (lines < 0)
1582 lines = 0;
1583 current = edit_bol (edit, current);
1584 while((lines--) && current != 0)
1585 current = edit_bol (edit, current - 1);
1586 return current;
1589 /* If cols is zero this returns the count of columns from current to upto. */
1590 /* If upto is zero returns index of cols across from current. */
1591 long
1592 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1594 long p, q;
1595 int col;
1597 if (upto) {
1598 q = upto;
1599 cols = -10;
1600 } else
1601 q = edit->last_byte + 2;
1603 for (col = 0, p = current; p < q; p++) {
1604 int c, orig_c;
1605 int utf_ch = 0;
1606 #ifdef HAVE_CHARSET
1607 int cw = 1;
1608 #endif
1609 if (cols != -10) {
1610 if (col == cols)
1611 return p;
1612 if (col > cols)
1613 return p - 1;
1615 orig_c = c = edit_get_byte (edit, p);
1616 #ifdef HAVE_CHARSET
1617 if (edit->utf8) {
1618 utf_ch = edit_get_utf (edit, p, &cw);
1619 if (utf8_display) {
1620 if (cw > 1)
1621 col -= cw - 1;
1622 if (g_unichar_iswide (utf_ch))
1623 col++;
1624 } else if (cw > 1 && g_unichar_isprint (utf_ch))
1625 col -= cw - 1;
1627 #endif
1628 c = convert_to_display_c (c);
1629 if (c == '\t')
1630 col += TAB_SIZE - col % TAB_SIZE;
1631 else if (c == '\n') {
1632 if (upto)
1633 return col;
1634 else
1635 return p;
1636 } else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1637 /* '\r' is shown as ^M, so we must advance 2 characters */
1638 /* Caret notation for control characters */
1639 col += 2;
1640 else
1641 col++;
1643 return col;
1646 /* returns the current column position of the cursor */
1647 int edit_get_col (WEdit * edit)
1649 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1653 /* Scrolling functions */
1655 void edit_update_curs_row (WEdit * edit)
1657 edit->curs_row = edit->curs_line - edit->start_line;
1660 void edit_update_curs_col (WEdit * edit)
1662 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1666 edit_get_curs_col (const WEdit *edit)
1668 return edit->curs_col;
1671 /*moves the display start position up by i lines */
1672 void edit_scroll_upward (WEdit * edit, unsigned long i)
1674 unsigned long lines_above = edit->start_line;
1675 if (i > lines_above)
1676 i = lines_above;
1677 if (i) {
1678 edit->start_line -= i;
1679 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1680 edit->force |= REDRAW_PAGE;
1681 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1683 edit_update_curs_row (edit);
1687 /* returns 1 if could scroll, 0 otherwise */
1688 void edit_scroll_downward (WEdit * edit, int i)
1690 int lines_below;
1691 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1692 if (lines_below > 0) {
1693 if (i > lines_below)
1694 i = lines_below;
1695 edit->start_line += i;
1696 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1697 edit->force |= REDRAW_PAGE;
1698 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1700 edit_update_curs_row (edit);
1703 void edit_scroll_right (WEdit * edit, int i)
1705 edit->force |= REDRAW_PAGE;
1706 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1707 edit->start_col -= i;
1710 void edit_scroll_left (WEdit * edit, int i)
1712 if (edit->start_col) {
1713 edit->start_col += i;
1714 if (edit->start_col > 0)
1715 edit->start_col = 0;
1716 edit->force |= REDRAW_PAGE;
1717 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1721 /* high level cursor movement commands */
1723 static int is_in_indent (WEdit *edit)
1725 long p = edit_bol (edit, edit->curs1);
1726 while (p < edit->curs1)
1727 if (!strchr (" \t", edit_get_byte (edit, p++)))
1728 return 0;
1729 return 1;
1732 static int left_of_four_spaces (WEdit *edit);
1734 void
1735 edit_move_to_prev_col (WEdit * edit, long p)
1737 int prev = edit->prev_col;
1738 int over = edit->over_col;
1739 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1741 if (option_cursor_beyond_eol) {
1742 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1744 if (line_len < prev + edit->over_col) {
1745 edit->over_col = prev + over - line_len;
1746 edit->prev_col = line_len;
1747 edit->curs_col = line_len;
1748 } else {
1749 edit->curs_col = prev + over;
1750 edit->prev_col = edit->curs_col;
1751 edit->over_col = 0;
1753 } else {
1754 edit->over_col = 0;
1755 if (is_in_indent (edit) && option_fake_half_tabs) {
1756 edit_update_curs_col (edit);
1757 if (space_width)
1758 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1759 int q = edit->curs_col;
1760 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1761 p = edit_bol (edit, edit->curs1);
1762 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1763 if (!left_of_four_spaces (edit))
1764 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1770 static int
1771 is_blank (WEdit *edit, long offset)
1773 long s, f;
1774 int c;
1775 s = edit_bol (edit, offset);
1776 f = edit_eol (edit, offset) - 1;
1777 while (s <= f) {
1778 c = edit_get_byte (edit, s++);
1779 if (!isspace (c))
1780 return 0;
1782 return 1;
1786 /* returns the offset of line i */
1787 static long
1788 edit_find_line (WEdit *edit, int line)
1790 int i, j = 0;
1791 int m = 2000000000;
1792 if (!edit->caches_valid) {
1793 for (i = 0; i < N_LINE_CACHES; i++)
1794 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1795 /* three offsets that we *know* are line 0 at 0 and these two: */
1796 edit->line_numbers[1] = edit->curs_line;
1797 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1798 edit->line_numbers[2] = edit->total_lines;
1799 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1800 edit->caches_valid = 1;
1802 if (line >= edit->total_lines)
1803 return edit->line_offsets[2];
1804 if (line <= 0)
1805 return 0;
1806 /* find the closest known point */
1807 for (i = 0; i < N_LINE_CACHES; i++) {
1808 int n;
1809 n = abs (edit->line_numbers[i] - line);
1810 if (n < m) {
1811 m = n;
1812 j = i;
1815 if (m == 0)
1816 return edit->line_offsets[j]; /* know the offset exactly */
1817 if (m == 1 && j >= 3)
1818 i = j; /* one line different - caller might be looping, so stay in this cache */
1819 else
1820 i = 3 + (rand () % (N_LINE_CACHES - 3));
1821 if (line > edit->line_numbers[j])
1822 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1823 else
1824 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1825 edit->line_numbers[i] = line;
1826 return edit->line_offsets[i];
1829 int line_is_blank (WEdit * edit, long line)
1831 return is_blank (edit, edit_find_line (edit, line));
1834 /* moves up until a blank line is reached, or until just
1835 before a non-blank line is reached */
1836 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1838 int i = 0;
1839 if (edit->curs_line > 1) {
1840 if (line_is_blank (edit, edit->curs_line)) {
1841 if (line_is_blank (edit, edit->curs_line - 1)) {
1842 for (i = edit->curs_line - 1; i; i--)
1843 if (!line_is_blank (edit, i)) {
1844 i++;
1845 break;
1847 } else {
1848 for (i = edit->curs_line - 1; i; i--)
1849 if (line_is_blank (edit, i))
1850 break;
1852 } else {
1853 for (i = edit->curs_line - 1; i; i--)
1854 if (line_is_blank (edit, i))
1855 break;
1858 edit_move_up (edit, edit->curs_line - i, scroll);
1861 /* moves down until a blank line is reached, or until just
1862 before a non-blank line is reached */
1863 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1865 int i;
1866 if (edit->curs_line >= edit->total_lines - 1) {
1867 i = edit->total_lines;
1868 } else {
1869 if (line_is_blank (edit, edit->curs_line)) {
1870 if (line_is_blank (edit, edit->curs_line + 1)) {
1871 for (i = edit->curs_line + 1; i; i++)
1872 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1873 i--;
1874 break;
1876 } else {
1877 for (i = edit->curs_line + 1; i; i++)
1878 if (line_is_blank (edit, i) || i >= edit->total_lines)
1879 break;
1881 } else {
1882 for (i = edit->curs_line + 1; i; i++)
1883 if (line_is_blank (edit, i) || i >= edit->total_lines)
1884 break;
1887 edit_move_down (edit, i - edit->curs_line, scroll);
1890 static void edit_begin_page (WEdit *edit)
1892 edit_update_curs_row (edit);
1893 edit_move_up (edit, edit->curs_row, 0);
1896 static void edit_end_page (WEdit *edit)
1898 edit_update_curs_row (edit);
1899 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1903 /* goto beginning of text */
1904 static void edit_move_to_top (WEdit * edit)
1906 if (edit->curs_line) {
1907 edit_cursor_move (edit, -edit->curs1);
1908 edit_move_to_prev_col (edit, 0);
1909 edit->force |= REDRAW_PAGE;
1910 edit->search_start = 0;
1911 edit_update_curs_row(edit);
1916 /* goto end of text */
1917 static void edit_move_to_bottom (WEdit * edit)
1919 if (edit->curs_line < edit->total_lines) {
1920 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1921 edit->start_display = edit->last_byte;
1922 edit->start_line = edit->total_lines;
1923 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1924 edit->force |= REDRAW_PAGE;
1928 /* goto beginning of line */
1929 static void edit_cursor_to_bol (WEdit * edit)
1931 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1932 edit->search_start = edit->curs1;
1933 edit->prev_col = edit_get_col (edit);
1934 edit->over_col = 0;
1937 /* goto end of line */
1938 static void edit_cursor_to_eol (WEdit * edit)
1940 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1941 edit->search_start = edit->curs1;
1942 edit->prev_col = edit_get_col (edit);
1943 edit->over_col = 0;
1946 /* move cursor to line 'line' */
1947 void edit_move_to_line (WEdit * e, long line)
1949 if(line < e->curs_line)
1950 edit_move_up (e, e->curs_line - line, 0);
1951 else
1952 edit_move_down (e, line - e->curs_line, 0);
1953 edit_scroll_screen_over_cursor (e);
1956 /* scroll window so that first visible line is 'line' */
1957 void edit_move_display (WEdit * e, long line)
1959 if(line < e->start_line)
1960 edit_scroll_upward (e, e->start_line - line);
1961 else
1962 edit_scroll_downward (e, line - e->start_line);
1965 /* save markers onto undo stack */
1966 void edit_push_markers (WEdit * edit)
1968 edit_push_action (edit, MARK_1 + edit->mark1);
1969 edit_push_action (edit, MARK_2 + edit->mark2);
1972 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1974 edit->mark1 = m1;
1975 edit->mark2 = m2;
1976 edit->column1 = c1;
1977 edit->column2 = c2;
1981 /* highlight marker toggle */
1982 void edit_mark_cmd (WEdit * edit, int unmark)
1984 edit_push_markers (edit);
1985 if (unmark) {
1986 edit_set_markers (edit, 0, 0, 0, 0);
1987 edit->force |= REDRAW_PAGE;
1988 } else {
1989 if (edit->mark2 >= 0) {
1990 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
1991 edit->force |= REDRAW_PAGE;
1992 } else
1993 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
1997 static unsigned long
1998 my_type_of (int c)
2000 int x, r = 0;
2001 const char *p, *q;
2002 const char option_chars_move_whole_word[] =
2003 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2005 if (!c)
2006 return 0;
2007 if (c == '!') {
2008 if (*option_chars_move_whole_word == '!')
2009 return 2;
2010 return 0x80000000UL;
2012 if (g_ascii_isupper ((gchar) c))
2013 c = 'A';
2014 else if (g_ascii_islower ((gchar) c))
2015 c = 'a';
2016 else if (g_ascii_isalpha (c))
2017 c = 'a';
2018 else if (isdigit (c))
2019 c = '0';
2020 else if (isspace (c))
2021 c = ' ';
2022 q = strchr (option_chars_move_whole_word, c);
2023 if (!q)
2024 return 0xFFFFFFFFUL;
2025 do {
2026 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2027 if (*p == '!')
2028 x <<= 1;
2029 r |= x;
2030 } while ((q = strchr (q + 1, c)));
2031 return r;
2034 static void
2035 edit_left_word_move (WEdit *edit, int s)
2037 for (;;) {
2038 int c1, c2;
2039 if (column_highlighting
2040 && edit->mark1 != edit->mark2
2041 && edit->over_col == 0
2042 && edit->curs1 == edit_bol(edit, edit->curs1))
2043 break;
2044 edit_cursor_move (edit, -1);
2045 if (!edit->curs1)
2046 break;
2047 c1 = edit_get_byte (edit, edit->curs1 - 1);
2048 c2 = edit_get_byte (edit, edit->curs1);
2049 if (!(my_type_of (c1) & my_type_of (c2)))
2050 break;
2051 if (isspace (c1) && !isspace (c2))
2052 break;
2053 if (s)
2054 if (!isspace (c1) && isspace (c2))
2055 break;
2059 static void edit_left_word_move_cmd (WEdit * edit)
2061 edit_left_word_move (edit, 0);
2062 edit->force |= REDRAW_PAGE;
2065 static void
2066 edit_right_word_move (WEdit *edit, int s)
2068 for (;;) {
2069 int c1, c2;
2070 if (column_highlighting
2071 && edit->mark1 != edit->mark2
2072 && edit->over_col == 0
2073 && edit->curs1 == edit_eol(edit, edit->curs1)
2075 break;
2076 edit_cursor_move (edit, 1);
2077 if (edit->curs1 >= edit->last_byte)
2078 break;
2079 c1 = edit_get_byte (edit, edit->curs1 - 1);
2080 c2 = edit_get_byte (edit, edit->curs1);
2081 if (!(my_type_of (c1) & my_type_of (c2)))
2082 break;
2083 if (isspace (c1) && !isspace (c2))
2084 break;
2085 if (s)
2086 if (!isspace (c1) && isspace (c2))
2087 break;
2091 static void edit_right_word_move_cmd (WEdit * edit)
2093 edit_right_word_move (edit, 0);
2094 edit->force |= REDRAW_PAGE;
2097 static void edit_right_char_move_cmd (WEdit * edit)
2099 int cw = 1;
2100 int c = 0;
2101 if ( edit->utf8 ) {
2102 c = edit_get_utf (edit, edit->curs1, &cw);
2103 if ( cw < 1 )
2104 cw = 1;
2105 } else {
2106 c = edit_get_byte (edit, edit->curs1);
2108 if (option_cursor_beyond_eol && c == '\n') {
2109 edit->over_col++;
2110 } else {
2111 edit_cursor_move (edit, cw);
2115 static void edit_left_char_move_cmd (WEdit * edit)
2117 int cw = 1;
2118 if (column_highlighting
2119 && edit->mark1 != edit->mark2
2120 && edit->over_col == 0
2121 && edit->curs1 == edit_bol(edit, edit->curs1))
2122 return;
2123 if ( edit->utf8 ) {
2124 edit_get_prev_utf (edit, edit->curs1, &cw);
2125 if ( cw < 1 )
2126 cw = 1;
2128 if (option_cursor_beyond_eol && edit->over_col > 0) {
2129 edit->over_col--;
2130 } else {
2131 edit_cursor_move (edit, -cw);
2135 /** Up or down cursor moving.
2136 direction = TRUE - move up
2137 = FALSE - move down
2139 static void
2140 edit_move_updown (WEdit * edit, unsigned long i, int scroll, gboolean direction)
2142 unsigned long p;
2143 unsigned long l = (direction)
2144 ? edit->curs_line
2145 : edit->total_lines - edit->curs_line;
2147 if (i > l)
2148 i = l;
2150 if (i == 0)
2151 return;
2153 if (i > 1)
2154 edit->force |= REDRAW_PAGE;
2155 if (scroll) {
2156 if (direction)
2157 edit_scroll_upward (edit, i);
2158 else
2159 edit_scroll_downward (edit, i);
2161 p = edit_bol (edit, edit->curs1);
2163 p = (direction)
2164 ? edit_move_backward (edit, p, i)
2165 : edit_move_forward (edit, p, i, 0);
2167 edit_cursor_move (edit, p - edit->curs1);
2169 edit_move_to_prev_col (edit, p);
2171 /* search start of current multibyte char (like CJK) */
2172 if (edit->curs1 + 1 < edit->last_byte) {
2173 edit_right_char_move_cmd (edit);
2174 edit_left_char_move_cmd (edit);
2177 edit->search_start = edit->curs1;
2178 edit->found_len = 0;
2181 static void edit_right_delete_word (WEdit * edit)
2183 int c1, c2;
2184 for (;;) {
2185 if (edit->curs1 >= edit->last_byte)
2186 break;
2187 c1 = edit_delete (edit, 1);
2188 c2 = edit_get_byte (edit, edit->curs1);
2189 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2190 break;
2191 if (!(my_type_of (c1) & my_type_of (c2)))
2192 break;
2196 static void edit_left_delete_word (WEdit * edit)
2198 int c1, c2;
2199 for (;;) {
2200 if (edit->curs1 <= 0)
2201 break;
2202 c1 = edit_backspace (edit, 1);
2203 c2 = edit_get_byte (edit, edit->curs1 - 1);
2204 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2205 break;
2206 if (!(my_type_of (c1) & my_type_of (c2)))
2207 break;
2212 the start column position is not recorded, and hence does not
2213 undo as it happed. But who would notice.
2215 static void
2216 edit_do_undo (WEdit * edit)
2218 long ac;
2219 long count = 0;
2221 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2222 edit->over_col = 0;
2223 while ((ac = pop_action (edit)) < KEY_PRESS) {
2224 switch ((int) ac) {
2225 case STACK_BOTTOM:
2226 goto done_undo;
2227 case CURS_RIGHT:
2228 edit_cursor_move (edit, 1);
2229 break;
2230 case CURS_LEFT:
2231 edit_cursor_move (edit, -1);
2232 break;
2233 case BACKSPACE:
2234 edit_backspace (edit, 1);
2235 break;
2236 case DELCHAR:
2237 edit_delete (edit, 1);
2238 break;
2239 case COLUMN_ON:
2240 column_highlighting = 1;
2241 break;
2242 case COLUMN_OFF:
2243 column_highlighting = 0;
2244 break;
2246 if (ac >= 256 && ac < 512)
2247 edit_insert_ahead (edit, ac - 256);
2248 if (ac >= 0 && ac < 256)
2249 edit_insert (edit, ac);
2251 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2252 edit->mark1 = ac - MARK_1;
2253 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2254 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2255 edit->mark2 = ac - MARK_2;
2256 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2258 if (count++)
2259 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2262 if (edit->start_display > ac - KEY_PRESS) {
2263 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2264 edit->force |= REDRAW_PAGE;
2265 } else if (edit->start_display < ac - KEY_PRESS) {
2266 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2267 edit->force |= REDRAW_PAGE;
2269 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2270 edit_update_curs_row (edit);
2272 done_undo:;
2273 edit->stack_disable = 0;
2276 static void edit_delete_to_line_end (WEdit * edit)
2278 while (edit_get_byte (edit, edit->curs1) != '\n') {
2279 if (!edit->curs2)
2280 break;
2281 edit_delete (edit, 1);
2285 static void edit_delete_to_line_begin (WEdit * edit)
2287 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2288 if (!edit->curs1)
2289 break;
2290 edit_backspace (edit, 1);
2294 void
2295 edit_delete_line (WEdit *edit)
2298 * Delete right part of the line.
2299 * Note that edit_get_byte() returns '\n' when byte position is
2300 * beyond EOF.
2302 while (edit_get_byte (edit, edit->curs1) != '\n') {
2303 (void) edit_delete (edit, 1);
2307 * Delete '\n' char.
2308 * Note that edit_delete() will not corrupt anything if called while
2309 * cursor position is EOF.
2311 (void) edit_delete (edit, 1);
2314 * Delete left part of the line.
2315 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2317 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2318 (void) edit_backspace (edit, 1);
2322 void insert_spaces_tab (WEdit * edit, int half)
2324 int i;
2325 edit_update_curs_col (edit);
2326 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2327 while (i > 0) {
2328 edit_insert (edit, ' ');
2329 i -= space_width;
2333 static int is_aligned_on_a_tab (WEdit * edit)
2335 edit_update_curs_col (edit);
2336 return !((edit->curs_col % (TAB_SIZE * space_width))
2337 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2340 static int right_of_four_spaces (WEdit *edit)
2342 int i, ch = 0;
2343 for (i = 1; i <= HALF_TAB_SIZE; i++)
2344 ch |= edit_get_byte (edit, edit->curs1 - i);
2345 if (ch == ' ')
2346 return is_aligned_on_a_tab (edit);
2347 return 0;
2350 static int left_of_four_spaces (WEdit *edit)
2352 int i, ch = 0;
2353 for (i = 0; i < HALF_TAB_SIZE; i++)
2354 ch |= edit_get_byte (edit, edit->curs1 + i);
2355 if (ch == ' ')
2356 return is_aligned_on_a_tab (edit);
2357 return 0;
2360 int edit_indent_width (WEdit * edit, long p)
2362 long q = p;
2363 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2364 q++;
2365 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2368 void edit_insert_indent (WEdit * edit, int indent)
2370 if (!option_fill_tabs_with_spaces) {
2371 while (indent >= TAB_SIZE) {
2372 edit_insert (edit, '\t');
2373 indent -= TAB_SIZE;
2376 while (indent-- > 0)
2377 edit_insert (edit, ' ');
2380 static void
2381 edit_auto_indent (WEdit * edit)
2383 long p;
2384 char c;
2385 p = edit->curs1;
2386 /* use the previous line as a template */
2387 p = edit_move_backward (edit, p, 1);
2388 /* copy the leading whitespace of the line */
2389 for (;;) { /* no range check - the line _is_ \n-terminated */
2390 c = edit_get_byte (edit, p++);
2391 if (c != ' ' && c != '\t')
2392 break;
2393 edit_insert (edit, c);
2397 static inline void
2398 edit_double_newline (WEdit * edit)
2400 edit_insert (edit, '\n');
2401 if (edit_get_byte (edit, edit->curs1) == '\n')
2402 return;
2403 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2404 return;
2405 edit->force |= REDRAW_PAGE;
2406 edit_insert (edit, '\n');
2409 static inline void
2410 edit_tab_cmd (WEdit * edit)
2412 int i;
2414 if (option_fake_half_tabs) {
2415 if (is_in_indent (edit)) {
2416 /*insert a half tab (usually four spaces) unless there is a
2417 half tab already behind, then delete it and insert a
2418 full tab. */
2419 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2420 for (i = 1; i <= HALF_TAB_SIZE; i++)
2421 edit_backspace (edit, 1);
2422 edit_insert (edit, '\t');
2423 } else {
2424 insert_spaces_tab (edit, 1);
2426 return;
2429 if (option_fill_tabs_with_spaces) {
2430 insert_spaces_tab (edit, 0);
2431 } else {
2432 edit_insert (edit, '\t');
2436 static void check_and_wrap_line (WEdit * edit)
2438 int curs, c;
2439 if (!option_typewriter_wrap)
2440 return;
2441 edit_update_curs_col (edit);
2442 if (edit->curs_col < option_word_wrap_line_length)
2443 return;
2444 curs = edit->curs1;
2445 for (;;) {
2446 curs--;
2447 c = edit_get_byte (edit, curs);
2448 if (c == '\n' || curs <= 0) {
2449 edit_insert (edit, '\n');
2450 return;
2452 if (c == ' ' || c == '\t') {
2453 int current = edit->curs1;
2454 edit_cursor_move (edit, curs - edit->curs1 + 1);
2455 edit_insert (edit, '\n');
2456 edit_cursor_move (edit, current - edit->curs1 + 1);
2457 return;
2462 static inline void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2464 void edit_push_key_press (WEdit * edit)
2466 edit_push_action (edit, KEY_PRESS + edit->start_display);
2467 if (edit->mark2 == -1)
2468 edit_push_action (edit, MARK_1 + edit->mark1);
2471 /* this find the matching bracket in either direction, and sets edit->bracket */
2472 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2474 const char * const b = "{}{[][()(", *p;
2475 int i = 1, a, inc = -1, c, d, n = 0;
2476 unsigned long j = 0;
2477 long q;
2478 edit_update_curs_row (edit);
2479 c = edit_get_byte (edit, edit->curs1);
2480 p = strchr (b, c);
2481 /* no limit */
2482 if (!furthest_bracket_search)
2483 furthest_bracket_search--;
2484 /* not on a bracket at all */
2485 if (!p)
2486 return -1;
2487 /* the matching bracket */
2488 d = p[1];
2489 /* going left or right? */
2490 if (strchr ("{[(", c))
2491 inc = 1;
2492 for (q = edit->curs1 + inc;; q += inc) {
2493 /* out of buffer? */
2494 if (q >= edit->last_byte || q < 0)
2495 break;
2496 a = edit_get_byte (edit, q);
2497 /* don't want to eat CPU */
2498 if (j++ > furthest_bracket_search)
2499 break;
2500 /* out of screen? */
2501 if (in_screen) {
2502 if (q < edit->start_display)
2503 break;
2504 /* count lines if searching downward */
2505 if (inc > 0 && a == '\n')
2506 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2507 break;
2509 /* count bracket depth */
2510 i += (a == c) - (a == d);
2511 /* return if bracket depth is zero */
2512 if (!i)
2513 return q;
2515 /* no match */
2516 return -1;
2519 static long last_bracket = -1;
2521 void edit_find_bracket (WEdit * edit)
2523 edit->bracket = edit_get_bracket (edit, 1, 10000);
2524 if (last_bracket != edit->bracket)
2525 edit->force |= REDRAW_PAGE;
2526 last_bracket = edit->bracket;
2529 static inline void
2530 edit_goto_matching_bracket (WEdit *edit)
2532 long q;
2534 q = edit_get_bracket (edit, 0, 0);
2535 if (q >= 0) {
2536 edit->bracket = edit->curs1;
2537 edit->force |= REDRAW_PAGE;
2538 edit_cursor_move (edit, q - edit->curs1);
2543 * This executes a command as though the user initiated it through a key
2544 * press. Callback with WIDGET_KEY as a message calls this after
2545 * translating the key press. This function can be used to pass any
2546 * command to the editor. Note that the screen wouldn't update
2547 * automatically. Either of command or char_for_insertion must be
2548 * passed as -1. Commands are executed, and char_for_insertion is
2549 * inserted at the cursor.
2551 void
2552 edit_execute_key_command (WEdit *edit, unsigned long command, int char_for_insertion)
2554 if (command == CK_Begin_Record_Macro) {
2555 edit->macro_i = 0;
2556 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2557 return;
2559 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2560 edit->force |= REDRAW_COMPLETELY;
2561 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2562 edit->macro_i = -1;
2563 return;
2565 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2566 edit->macro[edit->macro_i].command = command;
2567 edit->macro[edit->macro_i++].ch = char_for_insertion;
2569 /* record the beginning of a set of editing actions initiated by a key press */
2570 if (command != CK_Undo && command != CK_Ext_Mode)
2571 edit_push_key_press (edit);
2573 edit_execute_cmd (edit, command, char_for_insertion);
2574 if (column_highlighting)
2575 edit->force |= REDRAW_PAGE;
2578 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2581 This executes a command at a lower level than macro recording.
2582 It also does not push a key_press onto the undo stack. This means
2583 that if it is called many times, a single undo command will undo
2584 all of them. It also does not check for the Undo command.
2586 void
2587 edit_execute_cmd (WEdit *edit, unsigned long command, int char_for_insertion)
2589 edit->force |= REDRAW_LINE;
2591 /* The next key press will unhighlight the found string, so update
2592 * the whole page */
2593 if (edit->found_len || column_highlighting)
2594 edit->force |= REDRAW_PAGE;
2596 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2597 column_highlighting = 0;
2598 if (!edit->highlight
2599 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2600 edit_mark_cmd (edit, 1); /* clear */
2601 edit_mark_cmd (edit, 0); /* marking on */
2603 edit->highlight = 1;
2604 } else { /* any other command */
2605 if (edit->highlight)
2606 edit_mark_cmd (edit, 0); /* clear */
2607 edit->highlight = 0;
2610 /* first check for undo */
2611 if (command == CK_Undo) {
2612 edit_do_undo (edit);
2613 edit->found_len = 0;
2614 edit->prev_col = edit_get_col (edit);
2615 edit->search_start = edit->curs1;
2616 return;
2619 /* An ordinary key press */
2620 if (char_for_insertion >= 0) {
2621 if (edit->overwrite) {
2622 if (edit_get_byte (edit, edit->curs1) != '\n')
2623 edit_delete (edit, 0);
2625 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2626 edit_insert_over (edit);
2627 #ifdef HAVE_CHARSET
2628 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2629 unsigned char str[6 + 1];
2630 size_t i = 0;
2631 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2632 if ( res == 0 ) {
2633 str[0] = '.';
2634 str[1] = '\0';
2635 } else {
2636 str[res] = '\0';
2638 while ( str[i] != 0 && i<=6) {
2639 char_for_insertion = str[i];
2640 edit_insert (edit, char_for_insertion);
2641 i++;
2643 } else
2644 #endif
2645 edit_insert (edit, char_for_insertion);
2647 if (option_auto_para_formatting) {
2648 format_paragraph (edit, 0);
2649 edit->force |= REDRAW_PAGE;
2650 } else
2651 check_and_wrap_line (edit);
2652 edit->found_len = 0;
2653 edit->prev_col = edit_get_col (edit);
2654 edit->search_start = edit->curs1;
2655 edit_find_bracket (edit);
2656 return;
2659 switch (command) {
2660 case CK_Begin_Page:
2661 case CK_End_Page:
2662 case CK_Begin_Page_Highlight:
2663 case CK_End_Page_Highlight:
2664 case CK_Word_Left:
2665 case CK_Word_Right:
2666 case CK_Up:
2667 case CK_Down:
2668 case CK_Left:
2669 case CK_Right:
2670 if ( edit->mark2 >= 0 ) {
2671 if ( !option_persistent_selections ) {
2672 if (column_highlighting)
2673 edit_push_action (edit, COLUMN_ON);
2674 column_highlighting = 0;
2675 edit_mark_cmd (edit, 1);
2680 switch (command) {
2681 case CK_Begin_Page:
2682 case CK_End_Page:
2683 case CK_Begin_Page_Highlight:
2684 case CK_End_Page_Highlight:
2685 case CK_Word_Left:
2686 case CK_Word_Right:
2687 case CK_Up:
2688 case CK_Down:
2689 case CK_Word_Left_Highlight:
2690 case CK_Word_Right_Highlight:
2691 case CK_Up_Highlight:
2692 case CK_Down_Highlight:
2693 case CK_Up_Alt_Highlight:
2694 case CK_Down_Alt_Highlight:
2695 if (edit->mark2 == -1)
2696 break; /*marking is following the cursor: may need to highlight a whole line */
2697 case CK_Left:
2698 case CK_Right:
2699 case CK_Left_Highlight:
2700 case CK_Right_Highlight:
2701 edit->force |= REDRAW_CHAR_ONLY;
2704 /* basic cursor key commands */
2705 switch (command) {
2706 case CK_BackSpace:
2707 /* if non persistent selection and text selected */
2708 if ( !option_persistent_selections ) {
2709 if ( edit->mark1 != edit->mark2 ) {
2710 edit_block_delete_cmd (edit);
2711 break;
2714 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2715 edit->over_col--;
2716 break;
2718 if (option_backspace_through_tabs && is_in_indent (edit)) {
2719 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2720 && edit->curs1 > 0)
2721 edit_backspace (edit, 1);
2722 break;
2723 } else {
2724 if (option_fake_half_tabs) {
2725 int i;
2726 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2727 for (i = 0; i < HALF_TAB_SIZE; i++)
2728 edit_backspace (edit, 1);
2729 break;
2733 edit_backspace (edit, 0);
2734 break;
2735 case CK_Delete:
2736 /* if non persistent selection and text selected */
2737 if ( !option_persistent_selections ) {
2738 if ( edit->mark1 != edit->mark2 ) {
2739 edit_block_delete_cmd (edit);
2740 break;
2744 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2745 edit_insert_over (edit);
2747 if (option_fake_half_tabs) {
2748 int i;
2749 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2750 for (i = 1; i <= HALF_TAB_SIZE; i++)
2751 edit_delete (edit, 1);
2752 break;
2755 edit_delete (edit, 0);
2756 break;
2757 case CK_Delete_Word_Left:
2758 edit->over_col = 0;
2759 edit_left_delete_word (edit);
2760 break;
2761 case CK_Delete_Word_Right:
2762 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2763 edit_insert_over (edit);
2765 edit_right_delete_word (edit);
2766 break;
2767 case CK_Delete_Line:
2768 edit_delete_line (edit);
2769 break;
2770 case CK_Delete_To_Line_End:
2771 edit_delete_to_line_end (edit);
2772 break;
2773 case CK_Delete_To_Line_Begin:
2774 edit_delete_to_line_begin (edit);
2775 break;
2776 case CK_Enter:
2777 edit->over_col = 0;
2778 if (option_auto_para_formatting) {
2779 edit_double_newline (edit);
2780 if (option_return_does_auto_indent)
2781 edit_auto_indent (edit);
2782 format_paragraph (edit, 0);
2783 } else {
2784 edit_insert (edit, '\n');
2785 if (option_return_does_auto_indent) {
2786 edit_auto_indent (edit);
2789 break;
2790 case CK_Return:
2791 edit_insert (edit, '\n');
2792 break;
2794 case CK_Page_Up_Alt_Highlight:
2795 column_highlighting = 1;
2796 case CK_Page_Up:
2797 case CK_Page_Up_Highlight:
2798 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2799 break;
2800 case CK_Page_Down_Alt_Highlight:
2801 column_highlighting = 1;
2802 case CK_Page_Down:
2803 case CK_Page_Down_Highlight:
2804 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2805 break;
2806 case CK_Left_Alt_Highlight:
2807 column_highlighting = 1;
2808 case CK_Left:
2809 case CK_Left_Highlight:
2810 if (option_fake_half_tabs) {
2811 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2812 if ( option_cursor_beyond_eol && edit->over_col > 0)
2813 edit->over_col--;
2814 else
2815 edit_cursor_move (edit, -HALF_TAB_SIZE);
2816 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2817 break;
2820 edit_left_char_move_cmd (edit);
2821 break;
2822 case CK_Right_Alt_Highlight:
2823 column_highlighting = 1;
2824 case CK_Right:
2825 case CK_Right_Highlight:
2826 if (option_fake_half_tabs) {
2827 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2828 edit_cursor_move (edit, HALF_TAB_SIZE);
2829 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2830 break;
2833 edit_right_char_move_cmd (edit);
2834 break;
2835 case CK_Begin_Page:
2836 case CK_Begin_Page_Highlight:
2837 edit_begin_page (edit);
2838 break;
2839 case CK_End_Page:
2840 case CK_End_Page_Highlight:
2841 edit_end_page (edit);
2842 break;
2843 case CK_Word_Left:
2844 case CK_Word_Left_Highlight:
2845 edit->over_col = 0;
2846 edit_left_word_move_cmd (edit);
2847 break;
2848 case CK_Word_Right:
2849 case CK_Word_Right_Highlight:
2850 edit->over_col = 0;
2851 edit_right_word_move_cmd (edit);
2852 break;
2853 case CK_Up_Alt_Highlight:
2854 column_highlighting = 1;
2855 case CK_Up:
2856 case CK_Up_Highlight:
2857 edit_move_up (edit, 1, 0);
2858 break;
2859 case CK_Down_Alt_Highlight:
2860 column_highlighting = 1;
2861 case CK_Down:
2862 case CK_Down_Highlight:
2863 edit_move_down (edit, 1, 0);
2864 break;
2865 case CK_Paragraph_Up_Alt_Highlight:
2866 column_highlighting = 1;
2867 case CK_Paragraph_Up:
2868 case CK_Paragraph_Up_Highlight:
2869 edit_move_up_paragraph (edit, 0);
2870 break;
2871 case CK_Paragraph_Down_Alt_Highlight:
2872 column_highlighting = 1;
2873 case CK_Paragraph_Down:
2874 case CK_Paragraph_Down_Highlight:
2875 edit_move_down_paragraph (edit, 0);
2876 break;
2877 case CK_Scroll_Up_Alt_Highlight:
2878 column_highlighting = 1;
2879 case CK_Scroll_Up:
2880 case CK_Scroll_Up_Highlight:
2881 edit_move_up (edit, 1, 1);
2882 break;
2883 case CK_Scroll_Down_Alt_Highlight:
2884 column_highlighting = 1;
2885 case CK_Scroll_Down:
2886 case CK_Scroll_Down_Highlight:
2887 edit_move_down (edit, 1, 1);
2888 break;
2889 case CK_Home:
2890 case CK_Home_Highlight:
2891 edit_cursor_to_bol (edit);
2892 break;
2893 case CK_End:
2894 case CK_End_Highlight:
2895 edit_cursor_to_eol (edit);
2896 break;
2897 case CK_Tab:
2898 /* if text marked shift block */
2899 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2900 if (edit->mark2 < 0)
2901 edit_mark_cmd (edit, 0);
2902 edit_move_block_to_right (edit);
2903 } else {
2904 if ( option_cursor_beyond_eol )
2905 edit_insert_over (edit);
2906 edit_tab_cmd (edit);
2907 if (option_auto_para_formatting) {
2908 format_paragraph (edit, 0);
2909 edit->force |= REDRAW_PAGE;
2910 } else {
2911 check_and_wrap_line (edit);
2914 break;
2916 case CK_Toggle_Insert:
2917 edit->overwrite = (edit->overwrite == 0);
2918 break;
2920 case CK_Mark:
2921 if (edit->mark2 >= 0) {
2922 if (column_highlighting)
2923 edit_push_action (edit, COLUMN_ON);
2924 column_highlighting = 0;
2926 edit_mark_cmd (edit, 0);
2927 break;
2928 case CK_Column_Mark:
2929 if (!column_highlighting)
2930 edit_push_action (edit, COLUMN_OFF);
2931 column_highlighting = 1;
2932 edit_mark_cmd (edit, 0);
2933 break;
2934 case CK_Mark_All:
2935 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
2936 edit->force |= REDRAW_PAGE;
2937 break;
2938 case CK_Unmark:
2939 if (column_highlighting)
2940 edit_push_action (edit, COLUMN_ON);
2941 column_highlighting = 0;
2942 edit_mark_cmd (edit, 1);
2943 break;
2945 case CK_Toggle_Line_State:
2946 option_line_state = !option_line_state;
2947 if ( option_line_state ) {
2948 option_line_state_width = LINE_STATE_WIDTH;
2949 } else {
2950 option_line_state_width = 0;
2952 edit->force |= REDRAW_PAGE;
2953 break;
2955 case CK_Toggle_Show_Margin:
2956 show_right_margin = !show_right_margin;
2957 edit->force |= REDRAW_PAGE;
2958 break;
2960 case CK_Toggle_Bookmark:
2961 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2962 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2963 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2964 else
2965 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2966 break;
2967 case CK_Flush_Bookmarks:
2968 book_mark_flush (edit, BOOK_MARK_COLOR);
2969 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2970 edit->force |= REDRAW_PAGE;
2971 break;
2972 case CK_Next_Bookmark:
2973 if (edit->book_mark) {
2974 struct _book_mark *p;
2975 p = (struct _book_mark *) book_mark_find (edit,
2976 edit->curs_line);
2977 if (p->next) {
2978 p = p->next;
2979 if (p->line >= edit->start_line + edit->num_widget_lines
2980 || p->line < edit->start_line)
2981 edit_move_display (edit,
2982 p->line -
2983 edit->num_widget_lines / 2);
2984 edit_move_to_line (edit, p->line);
2987 break;
2988 case CK_Prev_Bookmark:
2989 if (edit->book_mark) {
2990 struct _book_mark *p;
2991 p = (struct _book_mark *) book_mark_find (edit,
2992 edit->curs_line);
2993 while (p->line == edit->curs_line)
2994 if (p->prev)
2995 p = p->prev;
2996 if (p->line >= 0) {
2997 if (p->line >= edit->start_line + edit->num_widget_lines
2998 || p->line < edit->start_line)
2999 edit_move_display (edit,
3000 p->line -
3001 edit->num_widget_lines / 2);
3002 edit_move_to_line (edit, p->line);
3005 break;
3007 case CK_Beginning_Of_Text:
3008 case CK_Beginning_Of_Text_Highlight:
3009 edit_move_to_top (edit);
3010 break;
3011 case CK_End_Of_Text:
3012 case CK_End_Of_Text_Highlight:
3013 edit_move_to_bottom (edit);
3014 break;
3016 case CK_Copy:
3017 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3018 edit_insert_over (edit);
3019 edit_block_copy_cmd (edit);
3020 break;
3021 case CK_Remove:
3022 edit_block_delete_cmd (edit);
3023 break;
3024 case CK_Move:
3025 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3026 edit_insert_over (edit);
3027 edit_block_move_cmd (edit);
3028 break;
3030 case CK_Shift_Block_Left:
3031 if (edit->mark1 != edit->mark2)
3032 edit_move_block_to_left (edit);
3033 break;
3034 case CK_Shift_Block_Right:
3035 if (edit->mark1 != edit->mark2)
3036 edit_move_block_to_right (edit);
3037 break;
3038 case CK_XStore:
3039 edit_copy_to_X_buf_cmd (edit);
3040 break;
3041 case CK_XCut:
3042 edit_cut_to_X_buf_cmd (edit);
3043 break;
3044 case CK_XPaste:
3045 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3046 edit_insert_over (edit);
3047 edit_paste_from_X_buf_cmd (edit);
3048 break;
3049 case CK_Selection_History:
3050 edit_paste_from_history (edit);
3051 break;
3053 case CK_Save_As:
3054 edit_save_as_cmd (edit);
3055 break;
3056 case CK_Save:
3057 edit_save_confirm_cmd (edit);
3058 break;
3059 case CK_Load:
3060 edit_load_cmd (edit, EDIT_FILE_COMMON);
3061 break;
3062 case CK_Save_Block:
3063 edit_save_block_cmd (edit);
3064 break;
3065 case CK_Insert_File:
3066 edit_insert_file_cmd (edit);
3067 break;
3069 case CK_Load_Prev_File:
3070 edit_load_back_cmd (edit);
3071 break;
3072 case CK_Load_Next_File:
3073 edit_load_forward_cmd (edit);
3074 break;
3076 case CK_Load_Syntax_File:
3077 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3078 break;
3079 case CK_Choose_Syntax:
3080 edit_syntax_dialog ();
3081 break;
3083 case CK_Load_Menu_File:
3084 edit_load_cmd (edit, EDIT_FILE_MENU);
3085 break;
3087 case CK_Toggle_Syntax:
3088 if ((option_syntax_highlighting ^= 1) == 1)
3089 edit_load_syntax (edit, NULL, option_syntax_type);
3090 edit->force |= REDRAW_PAGE;
3091 break;
3093 case CK_Toggle_Tab_TWS:
3094 enable_show_tabs_tws ^= 1;
3095 edit->force |= REDRAW_PAGE;
3096 break;
3098 case CK_Find:
3099 edit_search_cmd (edit, 0);
3100 break;
3101 case CK_Find_Again:
3102 edit_search_cmd (edit, 1);
3103 break;
3104 case CK_Replace:
3105 edit_replace_cmd (edit, 0);
3106 break;
3107 case CK_Replace_Again:
3108 edit_replace_cmd (edit, 1);
3109 break;
3110 case CK_Complete_Word:
3111 /* if text marked shift block */
3112 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3113 edit_move_block_to_left (edit);
3114 } else {
3115 edit_complete_word_cmd (edit);
3117 break;
3118 case CK_Find_Definition:
3119 edit_get_match_keyword_cmd (edit);
3120 break;
3121 case CK_Quit:
3122 dlg_stop (edit->widget.parent);
3123 break;
3124 case CK_New:
3125 edit_new_cmd (edit);
3126 break;
3127 case CK_Help:
3128 edit_help_cmd (edit);
3129 break;
3130 case CK_Refresh:
3131 edit_refresh_cmd (edit);
3132 break;
3133 case CK_SaveSetupCmd:
3134 save_setup_cmd ();
3135 break;
3136 case CK_About:
3137 query_dialog (_(" About "),
3138 _("\n Cooledit v3.11.5\n\n"
3139 " Copyright (C) 1996 the Free Software Foundation\n\n"
3140 " A user friendly text editor written\n"
3141 " for the Midnight Commander.\n"), D_NORMAL,
3142 1, _("&OK"));
3143 break;
3144 case CK_LearnKeys:
3145 learn_keys ();
3146 break;
3147 case CK_Edit_Options:
3148 edit_options_dialog ();
3149 break;
3150 case CK_Edit_Save_Mode:
3151 menu_save_mode_cmd ();
3152 break;
3153 case CK_Date:
3155 char s[BUF_MEDIUM];
3156 /* fool gcc to prevent a Y2K warning */
3157 char time_format[] = "_c";
3158 time_format[0] = '%';
3160 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3161 edit_print_string (edit, s);
3162 edit->force |= REDRAW_PAGE;
3163 break;
3165 break;
3166 case CK_Goto:
3167 edit_goto_cmd (edit);
3168 break;
3169 case CK_Paragraph_Format:
3170 format_paragraph (edit, 1);
3171 edit->force |= REDRAW_PAGE;
3172 break;
3173 case CK_Delete_Macro:
3174 edit_delete_macro_cmd (edit);
3175 break;
3176 case CK_Match_Bracket:
3177 edit_goto_matching_bracket (edit);
3178 break;
3179 case CK_User_Menu:
3180 user_menu (edit);
3181 break;
3182 case CK_Sort:
3183 edit_sort_cmd (edit);
3184 break;
3185 case CK_ExtCmd:
3186 edit_ext_cmd (edit);
3187 break;
3188 case CK_Mail:
3189 edit_mail_dialog (edit);
3190 break;
3191 case CK_Shell:
3192 view_other_cmd ();
3193 break;
3194 case CK_SelectCodepage:
3195 edit_select_codepage_cmd (edit);
3196 break;
3197 case CK_Insert_Literal:
3198 edit_insert_literal_cmd (edit);
3199 break;
3200 case CK_Execute_Macro:
3201 edit_execute_macro_cmd (edit);
3202 break;
3203 case CK_Begin_End_Macro:
3204 edit_begin_end_macro_cmd (edit);
3205 break;
3206 case CK_Ext_Mode:
3207 edit->extmod = 1;
3208 break;
3209 default:
3210 break;
3213 /* CK_Pipe_Block */
3214 if ((command / 1000) == 1) /* a shell command */
3215 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3216 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3217 struct macro m[MAX_MACRO_LENGTH];
3218 int nm;
3219 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3220 edit_execute_macro (edit, m, nm);
3223 /* keys which must set the col position, and the search vars */
3224 switch (command) {
3225 case CK_Find:
3226 case CK_Find_Again:
3227 case CK_Replace:
3228 case CK_Replace_Again:
3229 case CK_Complete_Word:
3230 edit->prev_col = edit_get_col (edit);
3231 break;
3232 case CK_Up:
3233 case CK_Up_Highlight:
3234 case CK_Up_Alt_Highlight:
3235 case CK_Down:
3236 case CK_Down_Highlight:
3237 case CK_Down_Alt_Highlight:
3238 case CK_Page_Up:
3239 case CK_Page_Up_Highlight:
3240 case CK_Page_Up_Alt_Highlight:
3241 case CK_Page_Down:
3242 case CK_Page_Down_Highlight:
3243 case CK_Page_Down_Alt_Highlight:
3244 case CK_Beginning_Of_Text:
3245 case CK_Beginning_Of_Text_Highlight:
3246 case CK_End_Of_Text:
3247 case CK_End_Of_Text_Highlight:
3248 case CK_Paragraph_Up:
3249 case CK_Paragraph_Up_Highlight:
3250 case CK_Paragraph_Up_Alt_Highlight:
3251 case CK_Paragraph_Down:
3252 case CK_Paragraph_Down_Highlight:
3253 case CK_Paragraph_Down_Alt_Highlight:
3254 case CK_Scroll_Up:
3255 case CK_Scroll_Up_Highlight:
3256 case CK_Scroll_Up_Alt_Highlight:
3257 case CK_Scroll_Down:
3258 case CK_Scroll_Down_Highlight:
3259 case CK_Scroll_Down_Alt_Highlight:
3260 edit->search_start = edit->curs1;
3261 edit->found_len = 0;
3262 break;
3263 default:
3264 edit->found_len = 0;
3265 edit->prev_col = edit_get_col (edit);
3266 edit->search_start = edit->curs1;
3268 edit_find_bracket (edit);
3270 if (option_auto_para_formatting) {
3271 switch (command) {
3272 case CK_BackSpace:
3273 case CK_Delete:
3274 case CK_Delete_Word_Left:
3275 case CK_Delete_Word_Right:
3276 case CK_Delete_To_Line_End:
3277 case CK_Delete_To_Line_Begin:
3278 format_paragraph (edit, 0);
3279 edit->force |= REDRAW_PAGE;
3285 static void
3286 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3288 int i = 0;
3290 if (edit->macro_depth++ > 256) {
3291 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3292 edit->macro_depth--;
3293 return;
3295 edit->force |= REDRAW_PAGE;
3296 for (; i < n; i++) {
3297 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3299 edit_update_screen (edit);
3300 edit->macro_depth--;
3303 /* User edit menu, like user menu (F2) but only in editor. */
3304 static void
3305 user_menu (WEdit * edit)
3307 char *block_file;
3308 int nomark;
3309 long start_mark, end_mark;
3310 struct stat status;
3312 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3314 nomark = eval_marks (edit, &start_mark, &end_mark);
3315 if (nomark == 0)
3316 edit_save_block (edit, block_file, start_mark, end_mark);
3318 /* run shell scripts from menu */
3319 user_menu_cmd (edit);
3321 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0)) {
3322 int rc = 0;
3323 FILE *fd;
3325 if (nomark == 0) {
3326 /* i.e. we have marked block */
3327 rc = edit_block_delete_cmd (edit);
3330 if (rc == 0)
3331 edit_insert_file (edit, block_file);
3333 /* truncate block file */
3334 fd = fopen (block_file, "w");
3335 if (fd != NULL)
3336 fclose (fd);
3338 edit_refresh_cmd (edit);
3339 edit->force |= REDRAW_COMPLETELY;
3341 g_free (block_file);
3344 void
3345 edit_stack_init (void)
3347 for (edit_stack_iterator = 0;
3348 edit_stack_iterator < MAX_HISTORY_MOVETO;
3349 edit_stack_iterator++ ) {
3350 edit_history_moveto[edit_stack_iterator].filename = NULL;
3351 edit_history_moveto[edit_stack_iterator].line = -1;
3354 edit_stack_iterator = 0;
3357 void
3358 edit_stack_free (void)
3360 for (edit_stack_iterator = 0;
3361 edit_stack_iterator < MAX_HISTORY_MOVETO;
3362 edit_stack_iterator++)
3363 g_free (edit_history_moveto[edit_stack_iterator].filename);
3366 /* move i lines */
3367 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
3369 edit_move_updown (edit, i, scroll, TRUE);
3372 /* move i lines */
3373 void edit_move_down (WEdit * edit, unsigned long i, int scroll)
3375 edit_move_updown (edit, i, scroll, FALSE);