Updated italian translation.
[midnight-commander.git] / src / editor / edit.c
blobb88e2943c3b60677114aba7985b90ff617241c4d
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
1837 edit_move_up_paragraph (WEdit * edit, int do_scroll)
1839 int i = 0;
1840 if (edit->curs_line > 1) {
1841 if (line_is_blank (edit, edit->curs_line)) {
1842 if (line_is_blank (edit, edit->curs_line - 1)) {
1843 for (i = edit->curs_line - 1; i; i--)
1844 if (!line_is_blank (edit, i)) {
1845 i++;
1846 break;
1848 } else {
1849 for (i = edit->curs_line - 1; i; i--)
1850 if (line_is_blank (edit, i))
1851 break;
1853 } else {
1854 for (i = edit->curs_line - 1; i; i--)
1855 if (line_is_blank (edit, i))
1856 break;
1859 edit_move_up (edit, edit->curs_line - i, do_scroll);
1862 /* moves down until a blank line is reached, or until just
1863 before a non-blank line is reached */
1864 static void
1865 edit_move_down_paragraph (WEdit * edit, int do_scroll)
1867 int i;
1868 if (edit->curs_line >= edit->total_lines - 1) {
1869 i = edit->total_lines;
1870 } else {
1871 if (line_is_blank (edit, edit->curs_line)) {
1872 if (line_is_blank (edit, edit->curs_line + 1)) {
1873 for (i = edit->curs_line + 1; i; i++)
1874 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1875 i--;
1876 break;
1878 } else {
1879 for (i = edit->curs_line + 1; i; i++)
1880 if (line_is_blank (edit, i) || i >= edit->total_lines)
1881 break;
1883 } else {
1884 for (i = edit->curs_line + 1; i; i++)
1885 if (line_is_blank (edit, i) || i >= edit->total_lines)
1886 break;
1889 edit_move_down (edit, i - edit->curs_line, do_scroll);
1892 static void edit_begin_page (WEdit *edit)
1894 edit_update_curs_row (edit);
1895 edit_move_up (edit, edit->curs_row, 0);
1898 static void edit_end_page (WEdit *edit)
1900 edit_update_curs_row (edit);
1901 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1905 /* goto beginning of text */
1906 static void edit_move_to_top (WEdit * edit)
1908 if (edit->curs_line) {
1909 edit_cursor_move (edit, -edit->curs1);
1910 edit_move_to_prev_col (edit, 0);
1911 edit->force |= REDRAW_PAGE;
1912 edit->search_start = 0;
1913 edit_update_curs_row(edit);
1918 /* goto end of text */
1919 static void edit_move_to_bottom (WEdit * edit)
1921 if (edit->curs_line < edit->total_lines) {
1922 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1923 edit->start_display = edit->last_byte;
1924 edit->start_line = edit->total_lines;
1925 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1926 edit->force |= REDRAW_PAGE;
1930 /* goto beginning of line */
1931 static void edit_cursor_to_bol (WEdit * edit)
1933 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1934 edit->search_start = edit->curs1;
1935 edit->prev_col = edit_get_col (edit);
1936 edit->over_col = 0;
1939 /* goto end of line */
1940 static void edit_cursor_to_eol (WEdit * edit)
1942 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1943 edit->search_start = edit->curs1;
1944 edit->prev_col = edit_get_col (edit);
1945 edit->over_col = 0;
1948 /* move cursor to line 'line' */
1949 void edit_move_to_line (WEdit * e, long line)
1951 if(line < e->curs_line)
1952 edit_move_up (e, e->curs_line - line, 0);
1953 else
1954 edit_move_down (e, line - e->curs_line, 0);
1955 edit_scroll_screen_over_cursor (e);
1958 /* scroll window so that first visible line is 'line' */
1959 void edit_move_display (WEdit * e, long line)
1961 if(line < e->start_line)
1962 edit_scroll_upward (e, e->start_line - line);
1963 else
1964 edit_scroll_downward (e, line - e->start_line);
1967 /* save markers onto undo stack */
1968 void edit_push_markers (WEdit * edit)
1970 edit_push_action (edit, MARK_1 + edit->mark1);
1971 edit_push_action (edit, MARK_2 + edit->mark2);
1974 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1976 edit->mark1 = m1;
1977 edit->mark2 = m2;
1978 edit->column1 = c1;
1979 edit->column2 = c2;
1983 /* highlight marker toggle */
1984 void edit_mark_cmd (WEdit * edit, int unmark)
1986 edit_push_markers (edit);
1987 if (unmark) {
1988 edit_set_markers (edit, 0, 0, 0, 0);
1989 edit->force |= REDRAW_PAGE;
1990 } else {
1991 if (edit->mark2 >= 0) {
1992 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
1993 edit->force |= REDRAW_PAGE;
1994 } else
1995 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
1999 static unsigned long
2000 my_type_of (int c)
2002 int x, r = 0;
2003 const char *p, *q;
2004 const char option_chars_move_whole_word[] =
2005 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2007 if (!c)
2008 return 0;
2009 if (c == '!') {
2010 if (*option_chars_move_whole_word == '!')
2011 return 2;
2012 return 0x80000000UL;
2014 if (g_ascii_isupper ((gchar) c))
2015 c = 'A';
2016 else if (g_ascii_islower ((gchar) c))
2017 c = 'a';
2018 else if (g_ascii_isalpha (c))
2019 c = 'a';
2020 else if (isdigit (c))
2021 c = '0';
2022 else if (isspace (c))
2023 c = ' ';
2024 q = strchr (option_chars_move_whole_word, c);
2025 if (!q)
2026 return 0xFFFFFFFFUL;
2027 do {
2028 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2029 if (*p == '!')
2030 x <<= 1;
2031 r |= x;
2032 } while ((q = strchr (q + 1, c)));
2033 return r;
2036 static void
2037 edit_left_word_move (WEdit *edit, int s)
2039 for (;;) {
2040 int c1, c2;
2041 if (column_highlighting
2042 && edit->mark1 != edit->mark2
2043 && edit->over_col == 0
2044 && edit->curs1 == edit_bol(edit, edit->curs1))
2045 break;
2046 edit_cursor_move (edit, -1);
2047 if (!edit->curs1)
2048 break;
2049 c1 = edit_get_byte (edit, edit->curs1 - 1);
2050 c2 = edit_get_byte (edit, edit->curs1);
2051 if (!(my_type_of (c1) & my_type_of (c2)))
2052 break;
2053 if (isspace (c1) && !isspace (c2))
2054 break;
2055 if (s)
2056 if (!isspace (c1) && isspace (c2))
2057 break;
2061 static void edit_left_word_move_cmd (WEdit * edit)
2063 edit_left_word_move (edit, 0);
2064 edit->force |= REDRAW_PAGE;
2067 static void
2068 edit_right_word_move (WEdit *edit, int s)
2070 for (;;) {
2071 int c1, c2;
2072 if (column_highlighting
2073 && edit->mark1 != edit->mark2
2074 && edit->over_col == 0
2075 && edit->curs1 == edit_eol(edit, edit->curs1)
2077 break;
2078 edit_cursor_move (edit, 1);
2079 if (edit->curs1 >= edit->last_byte)
2080 break;
2081 c1 = edit_get_byte (edit, edit->curs1 - 1);
2082 c2 = edit_get_byte (edit, edit->curs1);
2083 if (!(my_type_of (c1) & my_type_of (c2)))
2084 break;
2085 if (isspace (c1) && !isspace (c2))
2086 break;
2087 if (s)
2088 if (!isspace (c1) && isspace (c2))
2089 break;
2093 static void edit_right_word_move_cmd (WEdit * edit)
2095 edit_right_word_move (edit, 0);
2096 edit->force |= REDRAW_PAGE;
2099 static void edit_right_char_move_cmd (WEdit * edit)
2101 int cw = 1;
2102 int c = 0;
2103 if ( edit->utf8 ) {
2104 c = edit_get_utf (edit, edit->curs1, &cw);
2105 if ( cw < 1 )
2106 cw = 1;
2107 } else {
2108 c = edit_get_byte (edit, edit->curs1);
2110 if (option_cursor_beyond_eol && c == '\n') {
2111 edit->over_col++;
2112 } else {
2113 edit_cursor_move (edit, cw);
2117 static void edit_left_char_move_cmd (WEdit * edit)
2119 int cw = 1;
2120 if (column_highlighting
2121 && option_cursor_beyond_eol
2122 && edit->mark1 != edit->mark2
2123 && edit->over_col == 0
2124 && edit->curs1 == edit_bol(edit, edit->curs1))
2125 return;
2126 if ( edit->utf8 ) {
2127 edit_get_prev_utf (edit, edit->curs1, &cw);
2128 if ( cw < 1 )
2129 cw = 1;
2131 if (option_cursor_beyond_eol && edit->over_col > 0) {
2132 edit->over_col--;
2133 } else {
2134 edit_cursor_move (edit, -cw);
2138 /** Up or down cursor moving.
2139 direction = TRUE - move up
2140 = FALSE - move down
2142 static void
2143 edit_move_updown (WEdit * edit, unsigned long i, int do_scroll, gboolean direction)
2145 unsigned long p;
2146 unsigned long l = (direction)
2147 ? edit->curs_line
2148 : edit->total_lines - edit->curs_line;
2150 if (i > l)
2151 i = l;
2153 if (i == 0)
2154 return;
2156 if (i > 1)
2157 edit->force |= REDRAW_PAGE;
2158 if (do_scroll) {
2159 if (direction)
2160 edit_scroll_upward (edit, i);
2161 else
2162 edit_scroll_downward (edit, i);
2164 p = edit_bol (edit, edit->curs1);
2166 p = (direction)
2167 ? edit_move_backward (edit, p, i)
2168 : edit_move_forward (edit, p, i, 0);
2170 edit_cursor_move (edit, p - edit->curs1);
2172 edit_move_to_prev_col (edit, p);
2174 /* search start of current multibyte char (like CJK) */
2175 if (edit->curs1 + 1 < edit->last_byte) {
2176 edit_right_char_move_cmd (edit);
2177 edit_left_char_move_cmd (edit);
2180 edit->search_start = edit->curs1;
2181 edit->found_len = 0;
2184 static void edit_right_delete_word (WEdit * edit)
2186 int c1, c2;
2187 for (;;) {
2188 if (edit->curs1 >= edit->last_byte)
2189 break;
2190 c1 = edit_delete (edit, 1);
2191 c2 = edit_get_byte (edit, edit->curs1);
2192 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2193 break;
2194 if (!(my_type_of (c1) & my_type_of (c2)))
2195 break;
2199 static void edit_left_delete_word (WEdit * edit)
2201 int c1, c2;
2202 for (;;) {
2203 if (edit->curs1 <= 0)
2204 break;
2205 c1 = edit_backspace (edit, 1);
2206 c2 = edit_get_byte (edit, edit->curs1 - 1);
2207 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2208 break;
2209 if (!(my_type_of (c1) & my_type_of (c2)))
2210 break;
2215 the start column position is not recorded, and hence does not
2216 undo as it happed. But who would notice.
2218 static void
2219 edit_do_undo (WEdit * edit)
2221 long ac;
2222 long count = 0;
2224 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2225 edit->over_col = 0;
2226 while ((ac = pop_action (edit)) < KEY_PRESS) {
2227 switch ((int) ac) {
2228 case STACK_BOTTOM:
2229 goto done_undo;
2230 case CURS_RIGHT:
2231 edit_cursor_move (edit, 1);
2232 break;
2233 case CURS_LEFT:
2234 edit_cursor_move (edit, -1);
2235 break;
2236 case BACKSPACE:
2237 edit_backspace (edit, 1);
2238 break;
2239 case DELCHAR:
2240 edit_delete (edit, 1);
2241 break;
2242 case COLUMN_ON:
2243 column_highlighting = 1;
2244 break;
2245 case COLUMN_OFF:
2246 column_highlighting = 0;
2247 break;
2249 if (ac >= 256 && ac < 512)
2250 edit_insert_ahead (edit, ac - 256);
2251 if (ac >= 0 && ac < 256)
2252 edit_insert (edit, ac);
2254 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2255 edit->mark1 = ac - MARK_1;
2256 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2257 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2258 edit->mark2 = ac - MARK_2;
2259 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2261 if (count++)
2262 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2265 if (edit->start_display > ac - KEY_PRESS) {
2266 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2267 edit->force |= REDRAW_PAGE;
2268 } else if (edit->start_display < ac - KEY_PRESS) {
2269 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2270 edit->force |= REDRAW_PAGE;
2272 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2273 edit_update_curs_row (edit);
2275 done_undo:;
2276 edit->stack_disable = 0;
2279 static void edit_delete_to_line_end (WEdit * edit)
2281 while (edit_get_byte (edit, edit->curs1) != '\n') {
2282 if (!edit->curs2)
2283 break;
2284 edit_delete (edit, 1);
2288 static void edit_delete_to_line_begin (WEdit * edit)
2290 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2291 if (!edit->curs1)
2292 break;
2293 edit_backspace (edit, 1);
2297 void
2298 edit_delete_line (WEdit *edit)
2301 * Delete right part of the line.
2302 * Note that edit_get_byte() returns '\n' when byte position is
2303 * beyond EOF.
2305 while (edit_get_byte (edit, edit->curs1) != '\n') {
2306 (void) edit_delete (edit, 1);
2310 * Delete '\n' char.
2311 * Note that edit_delete() will not corrupt anything if called while
2312 * cursor position is EOF.
2314 (void) edit_delete (edit, 1);
2317 * Delete left part of the line.
2318 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2320 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2321 (void) edit_backspace (edit, 1);
2325 void insert_spaces_tab (WEdit * edit, int half)
2327 int i;
2328 edit_update_curs_col (edit);
2329 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2330 while (i > 0) {
2331 edit_insert (edit, ' ');
2332 i -= space_width;
2336 static int is_aligned_on_a_tab (WEdit * edit)
2338 edit_update_curs_col (edit);
2339 return !((edit->curs_col % (TAB_SIZE * space_width))
2340 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2343 static int right_of_four_spaces (WEdit *edit)
2345 int i, ch = 0;
2346 for (i = 1; i <= HALF_TAB_SIZE; i++)
2347 ch |= edit_get_byte (edit, edit->curs1 - i);
2348 if (ch == ' ')
2349 return is_aligned_on_a_tab (edit);
2350 return 0;
2353 static int left_of_four_spaces (WEdit *edit)
2355 int i, ch = 0;
2356 for (i = 0; i < HALF_TAB_SIZE; i++)
2357 ch |= edit_get_byte (edit, edit->curs1 + i);
2358 if (ch == ' ')
2359 return is_aligned_on_a_tab (edit);
2360 return 0;
2363 int edit_indent_width (WEdit * edit, long p)
2365 long q = p;
2366 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2367 q++;
2368 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2371 void edit_insert_indent (WEdit * edit, int indent)
2373 if (!option_fill_tabs_with_spaces) {
2374 while (indent >= TAB_SIZE) {
2375 edit_insert (edit, '\t');
2376 indent -= TAB_SIZE;
2379 while (indent-- > 0)
2380 edit_insert (edit, ' ');
2383 static void
2384 edit_auto_indent (WEdit * edit)
2386 long p;
2387 char c;
2388 p = edit->curs1;
2389 /* use the previous line as a template */
2390 p = edit_move_backward (edit, p, 1);
2391 /* copy the leading whitespace of the line */
2392 for (;;) { /* no range check - the line _is_ \n-terminated */
2393 c = edit_get_byte (edit, p++);
2394 if (c != ' ' && c != '\t')
2395 break;
2396 edit_insert (edit, c);
2400 static inline void
2401 edit_double_newline (WEdit * edit)
2403 edit_insert (edit, '\n');
2404 if (edit_get_byte (edit, edit->curs1) == '\n')
2405 return;
2406 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2407 return;
2408 edit->force |= REDRAW_PAGE;
2409 edit_insert (edit, '\n');
2412 static inline void
2413 edit_tab_cmd (WEdit * edit)
2415 int i;
2417 if (option_fake_half_tabs) {
2418 if (is_in_indent (edit)) {
2419 /*insert a half tab (usually four spaces) unless there is a
2420 half tab already behind, then delete it and insert a
2421 full tab. */
2422 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2423 for (i = 1; i <= HALF_TAB_SIZE; i++)
2424 edit_backspace (edit, 1);
2425 edit_insert (edit, '\t');
2426 } else {
2427 insert_spaces_tab (edit, 1);
2429 return;
2432 if (option_fill_tabs_with_spaces) {
2433 insert_spaces_tab (edit, 0);
2434 } else {
2435 edit_insert (edit, '\t');
2439 static void check_and_wrap_line (WEdit * edit)
2441 int curs, c;
2442 if (!option_typewriter_wrap)
2443 return;
2444 edit_update_curs_col (edit);
2445 if (edit->curs_col < option_word_wrap_line_length)
2446 return;
2447 curs = edit->curs1;
2448 for (;;) {
2449 curs--;
2450 c = edit_get_byte (edit, curs);
2451 if (c == '\n' || curs <= 0) {
2452 edit_insert (edit, '\n');
2453 return;
2455 if (c == ' ' || c == '\t') {
2456 int current = edit->curs1;
2457 edit_cursor_move (edit, curs - edit->curs1 + 1);
2458 edit_insert (edit, '\n');
2459 edit_cursor_move (edit, current - edit->curs1 + 1);
2460 return;
2465 static inline void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2467 void edit_push_key_press (WEdit * edit)
2469 edit_push_action (edit, KEY_PRESS + edit->start_display);
2470 if (edit->mark2 == -1)
2471 edit_push_action (edit, MARK_1 + edit->mark1);
2474 /* this find the matching bracket in either direction, and sets edit->bracket */
2475 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2477 const char * const b = "{}{[][()(", *p;
2478 int i = 1, a, inc = -1, c, d, n = 0;
2479 unsigned long j = 0;
2480 long q;
2481 edit_update_curs_row (edit);
2482 c = edit_get_byte (edit, edit->curs1);
2483 p = strchr (b, c);
2484 /* no limit */
2485 if (!furthest_bracket_search)
2486 furthest_bracket_search--;
2487 /* not on a bracket at all */
2488 if (!p)
2489 return -1;
2490 /* the matching bracket */
2491 d = p[1];
2492 /* going left or right? */
2493 if (strchr ("{[(", c))
2494 inc = 1;
2495 for (q = edit->curs1 + inc;; q += inc) {
2496 /* out of buffer? */
2497 if (q >= edit->last_byte || q < 0)
2498 break;
2499 a = edit_get_byte (edit, q);
2500 /* don't want to eat CPU */
2501 if (j++ > furthest_bracket_search)
2502 break;
2503 /* out of screen? */
2504 if (in_screen) {
2505 if (q < edit->start_display)
2506 break;
2507 /* count lines if searching downward */
2508 if (inc > 0 && a == '\n')
2509 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2510 break;
2512 /* count bracket depth */
2513 i += (a == c) - (a == d);
2514 /* return if bracket depth is zero */
2515 if (!i)
2516 return q;
2518 /* no match */
2519 return -1;
2522 static long last_bracket = -1;
2524 void edit_find_bracket (WEdit * edit)
2526 edit->bracket = edit_get_bracket (edit, 1, 10000);
2527 if (last_bracket != edit->bracket)
2528 edit->force |= REDRAW_PAGE;
2529 last_bracket = edit->bracket;
2532 static inline void
2533 edit_goto_matching_bracket (WEdit *edit)
2535 long q;
2537 q = edit_get_bracket (edit, 0, 0);
2538 if (q >= 0) {
2539 edit->bracket = edit->curs1;
2540 edit->force |= REDRAW_PAGE;
2541 edit_cursor_move (edit, q - edit->curs1);
2546 * This executes a command as though the user initiated it through a key
2547 * press. Callback with WIDGET_KEY as a message calls this after
2548 * translating the key press. This function can be used to pass any
2549 * command to the editor. Note that the screen wouldn't update
2550 * automatically. Either of command or char_for_insertion must be
2551 * passed as -1. Commands are executed, and char_for_insertion is
2552 * inserted at the cursor.
2554 void
2555 edit_execute_key_command (WEdit *edit, unsigned long command, int char_for_insertion)
2557 if (command == CK_Begin_Record_Macro) {
2558 edit->macro_i = 0;
2559 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2560 return;
2562 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2563 edit->force |= REDRAW_COMPLETELY;
2564 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2565 edit->macro_i = -1;
2566 return;
2568 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2569 edit->macro[edit->macro_i].command = command;
2570 edit->macro[edit->macro_i++].ch = char_for_insertion;
2572 /* record the beginning of a set of editing actions initiated by a key press */
2573 if (command != CK_Undo && command != CK_Ext_Mode)
2574 edit_push_key_press (edit);
2576 edit_execute_cmd (edit, command, char_for_insertion);
2577 if (column_highlighting)
2578 edit->force |= REDRAW_PAGE;
2581 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2584 This executes a command at a lower level than macro recording.
2585 It also does not push a key_press onto the undo stack. This means
2586 that if it is called many times, a single undo command will undo
2587 all of them. It also does not check for the Undo command.
2589 void
2590 edit_execute_cmd (WEdit *edit, unsigned long command, int char_for_insertion)
2592 edit->force |= REDRAW_LINE;
2594 /* The next key press will unhighlight the found string, so update
2595 * the whole page */
2596 if (edit->found_len || column_highlighting)
2597 edit->force |= REDRAW_PAGE;
2599 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2600 column_highlighting = 0;
2601 if (!edit->highlight
2602 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2603 edit_mark_cmd (edit, 1); /* clear */
2604 edit_mark_cmd (edit, 0); /* marking on */
2606 edit->highlight = 1;
2607 } else { /* any other command */
2608 if (edit->highlight)
2609 edit_mark_cmd (edit, 0); /* clear */
2610 edit->highlight = 0;
2613 /* first check for undo */
2614 if (command == CK_Undo) {
2615 edit_do_undo (edit);
2616 edit->found_len = 0;
2617 edit->prev_col = edit_get_col (edit);
2618 edit->search_start = edit->curs1;
2619 return;
2622 /* An ordinary key press */
2623 if (char_for_insertion >= 0) {
2624 if (edit->overwrite) {
2625 if (edit_get_byte (edit, edit->curs1) != '\n')
2626 edit_delete (edit, 0);
2628 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2629 edit_insert_over (edit);
2630 #ifdef HAVE_CHARSET
2631 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2632 unsigned char str[6 + 1];
2633 size_t i = 0;
2634 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2635 if ( res == 0 ) {
2636 str[0] = '.';
2637 str[1] = '\0';
2638 } else {
2639 str[res] = '\0';
2641 while ( str[i] != 0 && i<=6) {
2642 char_for_insertion = str[i];
2643 edit_insert (edit, char_for_insertion);
2644 i++;
2646 } else
2647 #endif
2648 edit_insert (edit, char_for_insertion);
2650 if (option_auto_para_formatting) {
2651 format_paragraph (edit, 0);
2652 edit->force |= REDRAW_PAGE;
2653 } else
2654 check_and_wrap_line (edit);
2655 edit->found_len = 0;
2656 edit->prev_col = edit_get_col (edit);
2657 edit->search_start = edit->curs1;
2658 edit_find_bracket (edit);
2659 return;
2662 switch (command) {
2663 case CK_Begin_Page:
2664 case CK_End_Page:
2665 case CK_Begin_Page_Highlight:
2666 case CK_End_Page_Highlight:
2667 case CK_Word_Left:
2668 case CK_Word_Right:
2669 case CK_Up:
2670 case CK_Down:
2671 case CK_Left:
2672 case CK_Right:
2673 if ( edit->mark2 >= 0 ) {
2674 if ( !option_persistent_selections ) {
2675 if (column_highlighting)
2676 edit_push_action (edit, COLUMN_ON);
2677 column_highlighting = 0;
2678 edit_mark_cmd (edit, 1);
2683 switch (command) {
2684 case CK_Begin_Page:
2685 case CK_End_Page:
2686 case CK_Begin_Page_Highlight:
2687 case CK_End_Page_Highlight:
2688 case CK_Word_Left:
2689 case CK_Word_Right:
2690 case CK_Up:
2691 case CK_Down:
2692 case CK_Word_Left_Highlight:
2693 case CK_Word_Right_Highlight:
2694 case CK_Up_Highlight:
2695 case CK_Down_Highlight:
2696 case CK_Up_Alt_Highlight:
2697 case CK_Down_Alt_Highlight:
2698 if (edit->mark2 == -1)
2699 break; /*marking is following the cursor: may need to highlight a whole line */
2700 case CK_Left:
2701 case CK_Right:
2702 case CK_Left_Highlight:
2703 case CK_Right_Highlight:
2704 edit->force |= REDRAW_CHAR_ONLY;
2707 /* basic cursor key commands */
2708 switch (command) {
2709 case CK_BackSpace:
2710 /* if non persistent selection and text selected */
2711 if ( !option_persistent_selections ) {
2712 if ( edit->mark1 != edit->mark2 ) {
2713 edit_block_delete_cmd (edit);
2714 break;
2717 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2718 edit->over_col--;
2719 break;
2721 if (option_backspace_through_tabs && is_in_indent (edit)) {
2722 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2723 && edit->curs1 > 0)
2724 edit_backspace (edit, 1);
2725 break;
2726 } else {
2727 if (option_fake_half_tabs) {
2728 int i;
2729 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2730 for (i = 0; i < HALF_TAB_SIZE; i++)
2731 edit_backspace (edit, 1);
2732 break;
2736 edit_backspace (edit, 0);
2737 break;
2738 case CK_Delete:
2739 /* if non persistent selection and text selected */
2740 if ( !option_persistent_selections ) {
2741 if ( edit->mark1 != edit->mark2 ) {
2742 edit_block_delete_cmd (edit);
2743 break;
2747 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2748 edit_insert_over (edit);
2750 if (option_fake_half_tabs) {
2751 int i;
2752 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2753 for (i = 1; i <= HALF_TAB_SIZE; i++)
2754 edit_delete (edit, 1);
2755 break;
2758 edit_delete (edit, 0);
2759 break;
2760 case CK_Delete_Word_Left:
2761 edit->over_col = 0;
2762 edit_left_delete_word (edit);
2763 break;
2764 case CK_Delete_Word_Right:
2765 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2766 edit_insert_over (edit);
2768 edit_right_delete_word (edit);
2769 break;
2770 case CK_Delete_Line:
2771 edit_delete_line (edit);
2772 break;
2773 case CK_Delete_To_Line_End:
2774 edit_delete_to_line_end (edit);
2775 break;
2776 case CK_Delete_To_Line_Begin:
2777 edit_delete_to_line_begin (edit);
2778 break;
2779 case CK_Enter:
2780 edit->over_col = 0;
2781 if (option_auto_para_formatting) {
2782 edit_double_newline (edit);
2783 if (option_return_does_auto_indent)
2784 edit_auto_indent (edit);
2785 format_paragraph (edit, 0);
2786 } else {
2787 edit_insert (edit, '\n');
2788 if (option_return_does_auto_indent) {
2789 edit_auto_indent (edit);
2792 break;
2793 case CK_Return:
2794 edit_insert (edit, '\n');
2795 break;
2797 case CK_Page_Up_Alt_Highlight:
2798 column_highlighting = 1;
2799 case CK_Page_Up:
2800 case CK_Page_Up_Highlight:
2801 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2802 break;
2803 case CK_Page_Down_Alt_Highlight:
2804 column_highlighting = 1;
2805 case CK_Page_Down:
2806 case CK_Page_Down_Highlight:
2807 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2808 break;
2809 case CK_Left_Alt_Highlight:
2810 column_highlighting = 1;
2811 case CK_Left:
2812 case CK_Left_Highlight:
2813 if (option_fake_half_tabs) {
2814 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2815 if ( option_cursor_beyond_eol && edit->over_col > 0)
2816 edit->over_col--;
2817 else
2818 edit_cursor_move (edit, -HALF_TAB_SIZE);
2819 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2820 break;
2823 edit_left_char_move_cmd (edit);
2824 break;
2825 case CK_Right_Alt_Highlight:
2826 column_highlighting = 1;
2827 case CK_Right:
2828 case CK_Right_Highlight:
2829 if (option_fake_half_tabs) {
2830 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2831 edit_cursor_move (edit, HALF_TAB_SIZE);
2832 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2833 break;
2836 edit_right_char_move_cmd (edit);
2837 break;
2838 case CK_Begin_Page:
2839 case CK_Begin_Page_Highlight:
2840 edit_begin_page (edit);
2841 break;
2842 case CK_End_Page:
2843 case CK_End_Page_Highlight:
2844 edit_end_page (edit);
2845 break;
2846 case CK_Word_Left:
2847 case CK_Word_Left_Highlight:
2848 edit->over_col = 0;
2849 edit_left_word_move_cmd (edit);
2850 break;
2851 case CK_Word_Right:
2852 case CK_Word_Right_Highlight:
2853 edit->over_col = 0;
2854 edit_right_word_move_cmd (edit);
2855 break;
2856 case CK_Up_Alt_Highlight:
2857 column_highlighting = 1;
2858 case CK_Up:
2859 case CK_Up_Highlight:
2860 edit_move_up (edit, 1, 0);
2861 break;
2862 case CK_Down_Alt_Highlight:
2863 column_highlighting = 1;
2864 case CK_Down:
2865 case CK_Down_Highlight:
2866 edit_move_down (edit, 1, 0);
2867 break;
2868 case CK_Paragraph_Up_Alt_Highlight:
2869 column_highlighting = 1;
2870 case CK_Paragraph_Up:
2871 case CK_Paragraph_Up_Highlight:
2872 edit_move_up_paragraph (edit, 0);
2873 break;
2874 case CK_Paragraph_Down_Alt_Highlight:
2875 column_highlighting = 1;
2876 case CK_Paragraph_Down:
2877 case CK_Paragraph_Down_Highlight:
2878 edit_move_down_paragraph (edit, 0);
2879 break;
2880 case CK_Scroll_Up_Alt_Highlight:
2881 column_highlighting = 1;
2882 case CK_Scroll_Up:
2883 case CK_Scroll_Up_Highlight:
2884 edit_move_up (edit, 1, 1);
2885 break;
2886 case CK_Scroll_Down_Alt_Highlight:
2887 column_highlighting = 1;
2888 case CK_Scroll_Down:
2889 case CK_Scroll_Down_Highlight:
2890 edit_move_down (edit, 1, 1);
2891 break;
2892 case CK_Home:
2893 case CK_Home_Highlight:
2894 edit_cursor_to_bol (edit);
2895 break;
2896 case CK_End:
2897 case CK_End_Highlight:
2898 edit_cursor_to_eol (edit);
2899 break;
2900 case CK_Tab:
2901 /* if text marked shift block */
2902 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2903 if (edit->mark2 < 0)
2904 edit_mark_cmd (edit, 0);
2905 edit_move_block_to_right (edit);
2906 } else {
2907 if ( option_cursor_beyond_eol )
2908 edit_insert_over (edit);
2909 edit_tab_cmd (edit);
2910 if (option_auto_para_formatting) {
2911 format_paragraph (edit, 0);
2912 edit->force |= REDRAW_PAGE;
2913 } else {
2914 check_and_wrap_line (edit);
2917 break;
2919 case CK_Toggle_Insert:
2920 edit->overwrite = (edit->overwrite == 0);
2921 break;
2923 case CK_Mark:
2924 if (edit->mark2 >= 0) {
2925 if (column_highlighting)
2926 edit_push_action (edit, COLUMN_ON);
2927 column_highlighting = 0;
2929 edit_mark_cmd (edit, 0);
2930 break;
2931 case CK_Column_Mark:
2932 if (!column_highlighting)
2933 edit_push_action (edit, COLUMN_OFF);
2934 column_highlighting = 1;
2935 edit_mark_cmd (edit, 0);
2936 break;
2937 case CK_Mark_All:
2938 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
2939 edit->force |= REDRAW_PAGE;
2940 break;
2941 case CK_Unmark:
2942 if (column_highlighting)
2943 edit_push_action (edit, COLUMN_ON);
2944 column_highlighting = 0;
2945 edit_mark_cmd (edit, 1);
2946 break;
2948 case CK_Toggle_Line_State:
2949 option_line_state = !option_line_state;
2950 if ( option_line_state ) {
2951 option_line_state_width = LINE_STATE_WIDTH;
2952 } else {
2953 option_line_state_width = 0;
2955 edit->force |= REDRAW_PAGE;
2956 break;
2958 case CK_Toggle_Show_Margin:
2959 show_right_margin = !show_right_margin;
2960 edit->force |= REDRAW_PAGE;
2961 break;
2963 case CK_Toggle_Bookmark:
2964 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2965 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2966 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2967 else
2968 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2969 break;
2970 case CK_Flush_Bookmarks:
2971 book_mark_flush (edit, BOOK_MARK_COLOR);
2972 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2973 edit->force |= REDRAW_PAGE;
2974 break;
2975 case CK_Next_Bookmark:
2976 if (edit->book_mark) {
2977 struct _book_mark *p;
2978 p = (struct _book_mark *) book_mark_find (edit,
2979 edit->curs_line);
2980 if (p->next) {
2981 p = p->next;
2982 if (p->line >= edit->start_line + edit->num_widget_lines
2983 || p->line < edit->start_line)
2984 edit_move_display (edit,
2985 p->line -
2986 edit->num_widget_lines / 2);
2987 edit_move_to_line (edit, p->line);
2990 break;
2991 case CK_Prev_Bookmark:
2992 if (edit->book_mark) {
2993 struct _book_mark *p;
2994 p = (struct _book_mark *) book_mark_find (edit,
2995 edit->curs_line);
2996 while (p->line == edit->curs_line)
2997 if (p->prev)
2998 p = p->prev;
2999 if (p->line >= 0) {
3000 if (p->line >= edit->start_line + edit->num_widget_lines
3001 || p->line < edit->start_line)
3002 edit_move_display (edit,
3003 p->line -
3004 edit->num_widget_lines / 2);
3005 edit_move_to_line (edit, p->line);
3008 break;
3010 case CK_Beginning_Of_Text:
3011 case CK_Beginning_Of_Text_Highlight:
3012 edit_move_to_top (edit);
3013 break;
3014 case CK_End_Of_Text:
3015 case CK_End_Of_Text_Highlight:
3016 edit_move_to_bottom (edit);
3017 break;
3019 case CK_Copy:
3020 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3021 edit_insert_over (edit);
3022 edit_block_copy_cmd (edit);
3023 break;
3024 case CK_Remove:
3025 edit_block_delete_cmd (edit);
3026 break;
3027 case CK_Move:
3028 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3029 edit_insert_over (edit);
3030 edit_block_move_cmd (edit);
3031 break;
3033 case CK_Shift_Block_Left:
3034 if (edit->mark1 != edit->mark2)
3035 edit_move_block_to_left (edit);
3036 break;
3037 case CK_Shift_Block_Right:
3038 if (edit->mark1 != edit->mark2)
3039 edit_move_block_to_right (edit);
3040 break;
3041 case CK_XStore:
3042 edit_copy_to_X_buf_cmd (edit);
3043 break;
3044 case CK_XCut:
3045 edit_cut_to_X_buf_cmd (edit);
3046 break;
3047 case CK_XPaste:
3048 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3049 edit_insert_over (edit);
3050 edit_paste_from_X_buf_cmd (edit);
3051 break;
3052 case CK_Selection_History:
3053 edit_paste_from_history (edit);
3054 break;
3056 case CK_Save_As:
3057 edit_save_as_cmd (edit);
3058 break;
3059 case CK_Save:
3060 edit_save_confirm_cmd (edit);
3061 break;
3062 case CK_Load:
3063 edit_load_cmd (edit, EDIT_FILE_COMMON);
3064 break;
3065 case CK_Save_Block:
3066 edit_save_block_cmd (edit);
3067 break;
3068 case CK_Insert_File:
3069 edit_insert_file_cmd (edit);
3070 break;
3072 case CK_Load_Prev_File:
3073 edit_load_back_cmd (edit);
3074 break;
3075 case CK_Load_Next_File:
3076 edit_load_forward_cmd (edit);
3077 break;
3079 case CK_Load_Syntax_File:
3080 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3081 break;
3082 case CK_Choose_Syntax:
3083 edit_syntax_dialog ();
3084 break;
3086 case CK_Load_Menu_File:
3087 edit_load_cmd (edit, EDIT_FILE_MENU);
3088 break;
3090 case CK_Toggle_Syntax:
3091 if ((option_syntax_highlighting ^= 1) == 1)
3092 edit_load_syntax (edit, NULL, option_syntax_type);
3093 edit->force |= REDRAW_PAGE;
3094 break;
3096 case CK_Toggle_Tab_TWS:
3097 enable_show_tabs_tws ^= 1;
3098 edit->force |= REDRAW_PAGE;
3099 break;
3101 case CK_Find:
3102 edit_search_cmd (edit, 0);
3103 break;
3104 case CK_Find_Again:
3105 edit_search_cmd (edit, 1);
3106 break;
3107 case CK_Replace:
3108 edit_replace_cmd (edit, 0);
3109 break;
3110 case CK_Replace_Again:
3111 edit_replace_cmd (edit, 1);
3112 break;
3113 case CK_Complete_Word:
3114 /* if text marked shift block */
3115 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3116 edit_move_block_to_left (edit);
3117 } else {
3118 edit_complete_word_cmd (edit);
3120 break;
3121 case CK_Find_Definition:
3122 edit_get_match_keyword_cmd (edit);
3123 break;
3124 case CK_Quit:
3125 dlg_stop (edit->widget.parent);
3126 break;
3127 case CK_New:
3128 edit_new_cmd (edit);
3129 break;
3130 case CK_Help:
3131 edit_help_cmd (edit);
3132 break;
3133 case CK_Refresh:
3134 edit_refresh_cmd (edit);
3135 break;
3136 case CK_SaveSetupCmd:
3137 save_setup_cmd ();
3138 break;
3139 case CK_About:
3140 query_dialog (_(" About "),
3141 _("\n Cooledit v3.11.5\n\n"
3142 " Copyright (C) 1996 the Free Software Foundation\n\n"
3143 " A user friendly text editor written\n"
3144 " for the Midnight Commander.\n"), D_NORMAL,
3145 1, _("&OK"));
3146 break;
3147 case CK_LearnKeys:
3148 learn_keys ();
3149 break;
3150 case CK_Edit_Options:
3151 edit_options_dialog ();
3152 break;
3153 case CK_Edit_Save_Mode:
3154 menu_save_mode_cmd ();
3155 break;
3156 case CK_Date:
3158 char s[BUF_MEDIUM];
3159 /* fool gcc to prevent a Y2K warning */
3160 char time_format[] = "_c";
3161 time_format[0] = '%';
3163 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3164 edit_print_string (edit, s);
3165 edit->force |= REDRAW_PAGE;
3166 break;
3168 break;
3169 case CK_Goto:
3170 edit_goto_cmd (edit);
3171 break;
3172 case CK_Paragraph_Format:
3173 format_paragraph (edit, 1);
3174 edit->force |= REDRAW_PAGE;
3175 break;
3176 case CK_Delete_Macro:
3177 edit_delete_macro_cmd (edit);
3178 break;
3179 case CK_Match_Bracket:
3180 edit_goto_matching_bracket (edit);
3181 break;
3182 case CK_User_Menu:
3183 user_menu (edit);
3184 break;
3185 case CK_Sort:
3186 edit_sort_cmd (edit);
3187 break;
3188 case CK_ExtCmd:
3189 edit_ext_cmd (edit);
3190 break;
3191 case CK_Mail:
3192 edit_mail_dialog (edit);
3193 break;
3194 case CK_Shell:
3195 view_other_cmd ();
3196 break;
3197 case CK_SelectCodepage:
3198 edit_select_codepage_cmd (edit);
3199 break;
3200 case CK_Insert_Literal:
3201 edit_insert_literal_cmd (edit);
3202 break;
3203 case CK_Execute_Macro:
3204 edit_execute_macro_cmd (edit);
3205 break;
3206 case CK_Begin_End_Macro:
3207 edit_begin_end_macro_cmd (edit);
3208 break;
3209 case CK_Ext_Mode:
3210 edit->extmod = 1;
3211 break;
3212 default:
3213 break;
3216 /* CK_Pipe_Block */
3217 if ((command / 1000) == 1) /* a shell command */
3218 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3219 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3220 struct macro m[MAX_MACRO_LENGTH];
3221 int nm;
3222 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3223 edit_execute_macro (edit, m, nm);
3226 /* keys which must set the col position, and the search vars */
3227 switch (command) {
3228 case CK_Find:
3229 case CK_Find_Again:
3230 case CK_Replace:
3231 case CK_Replace_Again:
3232 case CK_Complete_Word:
3233 edit->prev_col = edit_get_col (edit);
3234 break;
3235 case CK_Up:
3236 case CK_Up_Highlight:
3237 case CK_Up_Alt_Highlight:
3238 case CK_Down:
3239 case CK_Down_Highlight:
3240 case CK_Down_Alt_Highlight:
3241 case CK_Page_Up:
3242 case CK_Page_Up_Highlight:
3243 case CK_Page_Up_Alt_Highlight:
3244 case CK_Page_Down:
3245 case CK_Page_Down_Highlight:
3246 case CK_Page_Down_Alt_Highlight:
3247 case CK_Beginning_Of_Text:
3248 case CK_Beginning_Of_Text_Highlight:
3249 case CK_End_Of_Text:
3250 case CK_End_Of_Text_Highlight:
3251 case CK_Paragraph_Up:
3252 case CK_Paragraph_Up_Highlight:
3253 case CK_Paragraph_Up_Alt_Highlight:
3254 case CK_Paragraph_Down:
3255 case CK_Paragraph_Down_Highlight:
3256 case CK_Paragraph_Down_Alt_Highlight:
3257 case CK_Scroll_Up:
3258 case CK_Scroll_Up_Highlight:
3259 case CK_Scroll_Up_Alt_Highlight:
3260 case CK_Scroll_Down:
3261 case CK_Scroll_Down_Highlight:
3262 case CK_Scroll_Down_Alt_Highlight:
3263 edit->search_start = edit->curs1;
3264 edit->found_len = 0;
3265 break;
3266 default:
3267 edit->found_len = 0;
3268 edit->prev_col = edit_get_col (edit);
3269 edit->search_start = edit->curs1;
3271 edit_find_bracket (edit);
3273 if (option_auto_para_formatting) {
3274 switch (command) {
3275 case CK_BackSpace:
3276 case CK_Delete:
3277 case CK_Delete_Word_Left:
3278 case CK_Delete_Word_Right:
3279 case CK_Delete_To_Line_End:
3280 case CK_Delete_To_Line_Begin:
3281 format_paragraph (edit, 0);
3282 edit->force |= REDRAW_PAGE;
3288 static void
3289 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3291 int i = 0;
3293 if (edit->macro_depth++ > 256) {
3294 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3295 edit->macro_depth--;
3296 return;
3298 edit->force |= REDRAW_PAGE;
3299 for (; i < n; i++) {
3300 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3302 edit_update_screen (edit);
3303 edit->macro_depth--;
3306 /* User edit menu, like user menu (F2) but only in editor. */
3307 static void
3308 user_menu (WEdit * edit)
3310 char *block_file;
3311 int nomark;
3312 long start_mark, end_mark;
3313 struct stat status;
3315 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3317 nomark = eval_marks (edit, &start_mark, &end_mark);
3318 if (nomark == 0)
3319 edit_save_block (edit, block_file, start_mark, end_mark);
3321 /* run shell scripts from menu */
3322 user_menu_cmd (edit);
3324 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0)) {
3325 int rc = 0;
3326 FILE *fd;
3328 if (nomark == 0) {
3329 /* i.e. we have marked block */
3330 rc = edit_block_delete_cmd (edit);
3333 if (rc == 0)
3334 edit_insert_file (edit, block_file);
3336 /* truncate block file */
3337 fd = fopen (block_file, "w");
3338 if (fd != NULL)
3339 fclose (fd);
3341 edit_refresh_cmd (edit);
3342 edit->force |= REDRAW_COMPLETELY;
3344 g_free (block_file);
3347 void
3348 edit_stack_init (void)
3350 for (edit_stack_iterator = 0;
3351 edit_stack_iterator < MAX_HISTORY_MOVETO;
3352 edit_stack_iterator++ ) {
3353 edit_history_moveto[edit_stack_iterator].filename = NULL;
3354 edit_history_moveto[edit_stack_iterator].line = -1;
3357 edit_stack_iterator = 0;
3360 void
3361 edit_stack_free (void)
3363 for (edit_stack_iterator = 0;
3364 edit_stack_iterator < MAX_HISTORY_MOVETO;
3365 edit_stack_iterator++)
3366 g_free (edit_history_moveto[edit_stack_iterator].filename);
3369 /* move i lines */
3370 void
3371 edit_move_up (WEdit * edit, unsigned long i, int do_scroll)
3373 edit_move_updown (edit, i, do_scroll, TRUE);
3376 /* move i lines */
3377 void
3378 edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
3380 edit_move_updown (edit, i, do_scroll, FALSE);