Ticket #1703: SKIN: Make own colorpairs for buttonbar widget
[midnight-commander.git] / edit / edit.c
blobf6cfce06bb4c7c73fabb7a45d21cecacc9681738
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 "../src/global.h"
44 #include "edit-impl.h"
45 #include "editlock.h"
46 #include "edit-widget.h"
47 #include "../src/cmddef.h"
49 #include "../src/tty/color.h" /* EDITOR_NORMAL_COLOR */
50 #include "../src/tty/tty.h" /* attrset() */
51 #include "../src/tty/key.h" /* is_idle() */
52 #include "../src/skin/skin.h" /* mc_skin_color_get */
54 #include "../src/widget.h" /* buttonbar_redraw() */
55 #include "../src/cmd.h" /* view_other_cmd() */
56 #include "../src/user.h" /* user_menu_cmd() */
57 #include "../src/wtools.h" /* query_dialog() */
58 #include "../src/timefmt.h" /* time formatting */
59 #include "../src/strutil.h" /* utf string functions */
60 #include "../src/charsets.h" /* get_codepage_id */
61 #include "../src/main.h" /* source_codepage */
62 #include "../src/learn.h" /* learn_keys */
64 int option_word_wrap_line_length = 72;
65 int option_typewriter_wrap = 0;
66 int option_auto_para_formatting = 0;
67 int option_fill_tabs_with_spaces = 0;
68 int option_return_does_auto_indent = 1;
69 int option_backspace_through_tabs = 0;
70 int option_fake_half_tabs = 1;
71 int option_save_mode = EDIT_QUICK_SAVE;
72 int option_save_position = 1;
73 int option_max_undo = 32768;
74 int option_persistent_selections = 1;
75 int option_cursor_beyond_eol = 1;
76 int option_line_state = 0;
77 int option_line_state_width = 0;
79 int option_edit_right_extreme = 0;
80 int option_edit_left_extreme = 0;
81 int option_edit_top_extreme = 0;
82 int option_edit_bottom_extreme = 0;
83 int enable_show_tabs_tws = 1;
84 int option_check_nl_at_eof = 0;
86 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
87 char *option_backup_ext = NULL;
89 int edit_stack_iterator = 0;
90 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
91 /* magic sequense for say than block is vertical */
92 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
93 /*-
95 * here's a quick sketch of the layout: (don't run this through indent.)
97 * (b1 is buffers1 and b2 is buffers2)
99 * |
100 * \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
101 * ______________________________________|______________________________________
103 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
104 * |-> |-> |-> |-> |-> |-> |
106 * _<------------------------->|<----------------->_
107 * WEdit->curs2 | WEdit->curs1
108 * ^ | ^
109 * | ^|^ |
110 * cursor ||| cursor
111 * |||
112 * file end|||file beginning
117 * This_is_some_file
118 * fin.
121 const global_keymap_t *editor_map;
122 const global_keymap_t *editor_x_map;
124 static void user_menu (WEdit *edit);
126 int edit_get_byte (WEdit * edit, long byte_index)
128 unsigned long p;
129 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
130 return '\n';
132 if (byte_index >= edit->curs1) {
133 p = edit->curs1 + edit->curs2 - byte_index - 1;
134 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
135 } else {
136 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
140 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
142 unsigned long p;
144 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
145 return NULL;
147 if (byte_index >= edit->curs1) {
148 p = edit->curs1 + edit->curs2 - byte_index - 1;
149 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
150 } else {
151 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
155 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
157 unsigned long p;
159 if (byte_index >= (edit->curs1 + edit->curs2) ) {
160 byte_index -= 1;
163 if ( byte_index < 0 ) {
164 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';
189 str = edit_get_byte_ptr (edit, byte_index);
190 res = g_utf8_get_char_validated (str, -1);
192 if ( res < 0 ) {
193 ch = *str;
194 width = 0;
195 } else {
196 ch = res;
197 /* Calculate UTF-8 char width */
198 next_ch = g_utf8_next_char(str);
199 if ( next_ch ) {
200 width = next_ch - str;
201 } else {
202 ch = 0;
203 width = 0;
206 *char_width = width;
207 return ch;
210 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
212 gchar *str, *buf = NULL;
213 int res = -1;
214 gunichar ch;
215 gchar *next_ch = NULL;
216 int width = 0;
218 if ( byte_index > 0 ) {
219 byte_index--;
222 ch = edit_get_utf (edit, byte_index, &width);
223 if ( width == 1 ) {
224 *char_width = width;
225 return ch;
228 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
229 *char_width = 0;
230 return 0;
233 str = edit_get_byte_ptr (edit, byte_index);
234 buf = edit_get_buf_ptr (edit, byte_index);
235 /* get prev utf8 char */
236 if ( str != buf )
237 str = g_utf8_find_prev_char (buf, str);
239 res = g_utf8_get_char_validated (str, -1);
240 if ( res < 0 ) {
241 ch = *str;
242 width = 0;
243 } else {
244 ch = res;
245 /* Calculate UTF-8 char width */
246 next_ch = g_utf8_next_char(str);
247 if ( next_ch ) {
248 width = next_ch - str;
249 } else {
250 ch = 0;
251 width = 0;
254 *char_width = width;
255 return ch;
259 * Initialize the buffers for an empty files.
261 static void
262 edit_init_buffers (WEdit *edit)
264 int j;
266 for (j = 0; j <= MAXBUFF; j++) {
267 edit->buffers1[j] = NULL;
268 edit->buffers2[j] = NULL;
271 edit->curs1 = 0;
272 edit->curs2 = 0;
273 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
277 * Load file OR text into buffers. Set cursor to the beginning of file.
278 * Return 1 on error.
280 static int
281 edit_load_file_fast (WEdit *edit, const char *filename)
283 long buf, buf2;
284 int file = -1;
285 edit->curs2 = edit->last_byte;
286 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
287 edit->utf8 = 0;
288 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
289 GString *errmsg = g_string_new(NULL);
290 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
291 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
292 g_string_free (errmsg, TRUE);
293 return 1;
296 if (!edit->buffers2[buf2])
297 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
299 mc_read (file,
300 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
301 (edit->curs2 & M_EDIT_BUF_SIZE),
302 edit->curs2 & M_EDIT_BUF_SIZE);
304 for (buf = buf2 - 1; buf >= 0; buf--) {
305 /* edit->buffers2[0] is already allocated */
306 if (!edit->buffers2[buf])
307 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
308 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
311 mc_close (file);
312 return 0;
315 /* detecting an error on save is easy: just check if every byte has been written. */
316 /* detecting an error on read, is not so easy 'cos there is not way to tell
317 whether you read everything or not. */
318 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
319 static const struct edit_filters {
320 const char *read, *write, *extension;
321 } all_filters[] = {
322 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
323 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
324 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
325 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
326 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
329 /* Return index of the filter or -1 is there is no appropriate filter */
330 static int edit_find_filter (const char *filename)
332 size_t i, l, e;
333 if (!filename)
334 return -1;
335 l = strlen (filename);
336 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
337 e = strlen (all_filters[i].extension);
338 if (l > e)
339 if (!strcmp (all_filters[i].extension, filename + l - e))
340 return i;
342 return -1;
345 static char *
346 edit_get_filter (const char *filename)
348 int i, l;
349 char *p, *quoted_name;
350 i = edit_find_filter (filename);
351 if (i < 0)
352 return 0;
353 quoted_name = name_quote (filename, 0);
354 l = str_term_width1 (quoted_name);
355 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
356 sprintf (p, all_filters[i].read, quoted_name);
357 g_free (quoted_name);
358 return p;
361 char *
362 edit_get_write_filter (const char *write_name, const char *filename)
364 int i, l;
365 char *p, *writename;
366 i = edit_find_filter (filename);
367 if (i < 0)
368 return 0;
369 writename = name_quote (write_name, 0);
370 l = str_term_width1 (writename);
371 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
372 sprintf (p, all_filters[i].write, writename);
373 g_free (writename);
374 return p;
377 static long
378 edit_insert_stream (WEdit * edit, FILE * f)
380 int c;
381 long i = 0;
382 while ((c = fgetc (f)) >= 0) {
383 edit_insert (edit, c);
384 i++;
386 return i;
389 long edit_write_stream (WEdit * edit, FILE * f)
391 long i;
393 if (edit->lb == LB_ASIS) {
394 for (i = 0; i < edit->last_byte; i++)
395 if (fputc (edit_get_byte (edit, i), f) < 0)
396 break;
397 return i;
400 /* change line breaks */
401 for (i = 0; i < edit->last_byte; i++) {
402 unsigned char c = edit_get_byte (edit, i);
404 if (!(c == '\n' || c == '\r')) {
405 /* not line break */
406 if (fputc (c, f) < 0)
407 return i;
408 } else { /* (c == '\n' || c == '\r') */
409 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
411 switch (edit->lb) {
412 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
413 /* put one line break unconditionally */
414 if (fputc ('\n', f) < 0)
415 return i;
417 i++; /* 2 chars are processed */
419 if (c == '\r' && c1 == '\n')
420 /* Windows line break; go to the next char */
421 break;
423 if (c == '\r' && c1 == '\r') {
424 /* two Macintosh line breaks; put second line break */
425 if (fputc ('\n', f) < 0)
426 return i;
427 break;
430 if (fputc (c1, f) < 0)
431 return i;
432 break;
434 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
435 /* put one line break unconditionally */
436 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
437 return i;
439 if (c == '\r' && c1 == '\n')
440 /* Windows line break; go to the next char */
441 i++;
442 break;
444 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
445 /* put one line break unconditionally */
446 if (fputc ('\r', f) < 0)
447 return i;
449 i++; /* 2 chars are processed */
451 if (c == '\r' && c1 == '\n')
452 /* Windows line break; go to the next char */
453 break;
455 if (c == '\n' && c1 == '\n') {
456 /* two Windows line breaks; put second line break */
457 if (fputc ('\r', f) < 0)
458 return i;
459 break;
462 if (fputc (c1, f) < 0)
463 return i;
464 break;
465 case LB_ASIS: /* default without changes */
466 break;
471 return edit->last_byte;
474 #define TEMP_BUF_LEN 1024
476 /* inserts a file at the cursor, returns 1 on success */
478 edit_insert_file (WEdit *edit, const char *filename)
480 char *p;
481 if ((p = edit_get_filter (filename))) {
482 FILE *f;
483 long current = edit->curs1;
484 f = (FILE *) popen (p, "r");
485 if (f) {
486 edit_insert_stream (edit, f);
487 edit_cursor_move (edit, current - edit->curs1);
488 if (pclose (f) > 0) {
489 GString *errmsg = g_string_new (NULL);
490 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
491 edit_error_dialog (_("Error"), errmsg->str);
492 g_string_free (errmsg, TRUE);
493 g_free (p);
494 return 0;
496 } else {
497 GString *errmsg = g_string_new (NULL);
498 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
499 edit_error_dialog (_("Error"), errmsg->str);
500 g_string_free (errmsg, TRUE);
501 g_free (p);
502 return 0;
504 g_free (p);
505 } else {
506 int i, file, blocklen;
507 long current = edit->curs1;
508 int vertical_insertion = 0;
509 char *buf;
510 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
511 return 0;
512 buf = g_malloc (TEMP_BUF_LEN);
513 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
514 if (blocklen > 0) {
515 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
516 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
517 vertical_insertion = 1;
518 } else {
519 mc_lseek (file, 0, SEEK_SET);
522 if (vertical_insertion) {
523 blocklen = edit_insert_column_of_text_from_file (edit, file);
524 } else {
525 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
526 for (i = 0; i < blocklen; i++)
527 edit_insert (edit, buf[i]);
530 edit_cursor_move (edit, current - edit->curs1);
531 g_free (buf);
532 mc_close (file);
533 if (blocklen)
534 return 0;
536 return 1;
539 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
540 static int
541 check_file_access (WEdit *edit, const char *filename, struct stat *st)
543 int file;
544 GString *errmsg = (GString *) 0;
546 /* Try opening an existing file */
547 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
549 if (file < 0) {
551 * Try creating the file. O_EXCL prevents following broken links
552 * and opening existing files.
554 file =
555 mc_open (filename,
556 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
557 0666);
558 if (file < 0) {
559 g_string_sprintf (errmsg = g_string_new (NULL),
560 _(" Cannot open %s for reading "), filename);
561 goto cleanup;
562 } else {
563 /* New file, delete it if it's not modified or saved */
564 edit->delete_file = 1;
568 /* Check what we have opened */
569 if (mc_fstat (file, st) < 0) {
570 g_string_sprintf (errmsg = g_string_new (NULL),
571 _(" Cannot get size/permissions for %s "), filename);
572 goto cleanup;
575 /* We want to open regular files only */
576 if (!S_ISREG (st->st_mode)) {
577 g_string_sprintf (errmsg = g_string_new (NULL),
578 _(" %s is not a regular file "), filename);
579 goto cleanup;
583 * Don't delete non-empty files.
584 * O_EXCL should prevent it, but let's be on the safe side.
586 if (st->st_size > 0) {
587 edit->delete_file = 0;
590 if (st->st_size >= SIZE_LIMIT) {
591 g_string_sprintf (errmsg = g_string_new (NULL),
592 _(" File %s is too large "), filename);
593 goto cleanup;
596 cleanup:
597 (void) mc_close (file);
598 if (errmsg) {
599 edit_error_dialog (_("Error"), errmsg->str);
600 g_string_free (errmsg, TRUE);
601 return 1;
603 return 0;
607 * Open the file and load it into the buffers, either directly or using
608 * a filter. Return 0 on success, 1 on error.
610 * Fast loading (edit_load_file_fast) is used when the file size is
611 * known. In this case the data is read into the buffers by blocks.
612 * If the file size is not known, the data is loaded byte by byte in
613 * edit_insert_file.
615 static int
616 edit_load_file (WEdit *edit)
618 int fast_load = 1;
620 /* Cannot do fast load if a filter is used */
621 if (edit_find_filter (edit->filename) >= 0)
622 fast_load = 0;
625 * VFS may report file size incorrectly, and slow load is not a big
626 * deal considering overhead in VFS.
628 if (!vfs_file_is_local (edit->filename))
629 fast_load = 0;
632 * FIXME: line end translation should disable fast loading as well
633 * Consider doing fseek() to the end and ftell() for the real size.
636 if (*edit->filename) {
637 /* If we are dealing with a real file, check that it exists */
638 if (check_file_access (edit, edit->filename, &edit->stat1))
639 return 1;
640 } else {
641 /* nothing to load */
642 fast_load = 0;
645 edit_init_buffers (edit);
647 if (fast_load) {
648 edit->last_byte = edit->stat1.st_size;
649 edit_load_file_fast (edit, edit->filename);
650 /* If fast load was used, the number of lines wasn't calculated */
651 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
652 } else {
653 #ifdef HAVE_CHARSET
654 const char *codepage_id;
655 #endif
656 edit->last_byte = 0;
657 if (*edit->filename) {
658 edit->stack_disable = 1;
659 if (!edit_insert_file (edit, edit->filename)) {
660 edit_clean (edit);
661 return 1;
663 edit->stack_disable = 0;
666 #ifdef HAVE_CHARSET
667 codepage_id = get_codepage_id( source_codepage );
668 if ( codepage_id )
669 edit->utf8 = str_isutf8 ( codepage_id );
670 #endif
672 edit->lb = LB_ASIS;
673 return 0;
676 /* Restore saved cursor position in the file */
677 static void
678 edit_load_position (WEdit *edit)
680 char *filename;
681 long line, column;
683 if (!edit->filename || !*edit->filename)
684 return;
686 filename = vfs_canon (edit->filename);
687 load_file_position (filename, &line, &column);
688 g_free (filename);
690 edit_move_to_line (edit, line - 1);
691 edit->prev_col = column;
692 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
693 edit_move_display (edit, line - (edit->num_widget_lines / 2));
696 /* Save cursor position in the file */
697 static void
698 edit_save_position (WEdit *edit)
700 char *filename;
702 if (!edit->filename || !*edit->filename)
703 return;
705 filename = vfs_canon (edit->filename);
706 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
707 g_free (filename);
710 /* Clean the WEdit stricture except the widget part */
711 static void
712 edit_purge_widget (WEdit *edit)
714 int len = sizeof (WEdit) - sizeof (Widget);
715 char *start = (char *) edit + sizeof (Widget);
716 memset (start, 0, len);
717 edit->macro_i = -1; /* not recording a macro */
720 static void
721 edit_set_keymap (void)
723 editor_map = default_editor_keymap;
724 if (editor_keymap && editor_keymap->len > 0)
725 editor_map = (global_keymap_t *) editor_keymap->data;
727 editor_x_map = default_editor_x_keymap;
728 if (editor_x_keymap && editor_x_keymap->len > 0)
729 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
733 #define space_width 1
736 * Fill in the edit structure. Return NULL on failure. Pass edit as
737 * NULL to allocate a new structure.
739 * If line is 0, try to restore saved position. Otherwise put the
740 * cursor on that line and show it in the middle of the screen.
742 WEdit *
743 edit_init (WEdit *edit, int lines, int columns, const char *filename,
744 long line)
746 int to_free = 0;
747 option_auto_syntax = 1; /* Resetting to auto on every invokation */
748 if ( option_line_state ) {
749 option_line_state_width = LINE_STATE_WIDTH;
750 } else {
751 option_line_state_width = 0;
753 if (!edit) {
754 #ifdef ENABLE_NLS
756 * Expand option_whole_chars_search by national letters using
757 * current locale
760 static char option_whole_chars_search_buf[256];
762 if (option_whole_chars_search_buf != option_whole_chars_search) {
763 size_t i;
764 size_t len = str_term_width1 (option_whole_chars_search);
766 strcpy (option_whole_chars_search_buf,
767 option_whole_chars_search);
769 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
770 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
771 option_whole_chars_search_buf[len++] = i;
775 option_whole_chars_search_buf[len] = 0;
776 option_whole_chars_search = option_whole_chars_search_buf;
778 #endif /* ENABLE_NLS */
779 edit = g_malloc0 (sizeof (WEdit));
780 edit->search = NULL;
781 to_free = 1;
783 edit_purge_widget (edit);
784 edit->num_widget_lines = lines;
785 edit->over_col = 0;
786 edit->num_widget_columns = columns;
787 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
788 edit->stat1.st_uid = getuid ();
789 edit->stat1.st_gid = getgid ();
790 edit->stat1.st_mtime = 0;
791 edit->bracket = -1;
792 edit->force |= REDRAW_PAGE;
793 edit_set_filename (edit, filename);
794 edit->stack_size = START_STACK_SIZE;
795 edit->stack_size_mask = START_STACK_SIZE - 1;
796 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
797 if (edit_load_file (edit)) {
798 /* edit_load_file already gives an error message */
799 if (to_free)
800 g_free (edit);
801 return 0;
803 edit->utf8 = 0;
804 edit->converter = str_cnv_from_term;
805 #ifdef HAVE_CHARSET
806 const char *cp_id = NULL;
807 cp_id = get_codepage_id (source_codepage >= 0 ?
808 source_codepage : display_codepage);
810 if (cp_id != NULL) {
811 GIConv conv;
812 conv = str_crt_conv_from (cp_id);
813 if (conv != INVALID_CONV) {
814 if (edit->converter != str_cnv_from_term)
815 str_close_conv (edit->converter);
816 edit->converter = conv;
819 if (cp_id != NULL)
820 edit->utf8 = str_isutf8 (cp_id);
821 #endif
823 edit->loading_done = 1;
824 edit->modified = 0;
825 edit->locked = 0;
826 edit_load_syntax (edit, 0, 0);
828 int color;
829 edit_get_syntax_color (edit, -1, &color);
832 /* load saved cursor position */
833 if ((line == 0) && option_save_position) {
834 edit_load_position (edit);
835 } else {
836 if (line <= 0)
837 line = 1;
838 edit_move_display (edit, line - 1);
839 edit_move_to_line (edit, line - 1);
842 edit_set_keymap ();
844 return edit;
847 /* Clear the edit struct, freeing everything in it. Return 1 on success */
849 edit_clean (WEdit *edit)
851 int j = 0;
853 if (!edit)
854 return 0;
856 /* a stale lock, remove it */
857 if (edit->locked)
858 edit->locked = edit_unlock_file (edit->filename);
860 /* save cursor position */
861 if (option_save_position)
862 edit_save_position (edit);
864 /* File specified on the mcedit command line and never saved */
865 if (edit->delete_file)
866 unlink (edit->filename);
868 edit_free_syntax_rules (edit);
869 book_mark_flush (edit, -1);
870 for (; j <= MAXBUFF; j++) {
871 g_free (edit->buffers1[j]);
872 g_free (edit->buffers2[j]);
875 g_free (edit->undo_stack);
876 g_free (edit->filename);
877 g_free (edit->dir);
879 if (edit->search)
881 mc_search_free(edit->search);
882 edit->search = NULL;
884 edit_purge_widget (edit);
886 return 1;
890 /* returns 1 on success */
891 int edit_renew (WEdit * edit)
893 int lines = edit->num_widget_lines;
894 int columns = edit->num_widget_columns;
895 int retval = 1;
897 edit_clean (edit);
898 if (!edit_init (edit, lines, columns, "", 0))
899 retval = 0;
900 return retval;
904 * Load a new file into the editor. If it fails, preserve the old file.
905 * To do it, allocate a new widget, initialize it and, if the new file
906 * was loaded, copy the data to the old widget.
907 * Return 1 on success, 0 on failure.
910 edit_reload (WEdit *edit, const char *filename)
912 WEdit *e;
913 int lines = edit->num_widget_lines;
914 int columns = edit->num_widget_columns;
916 e = g_malloc0 (sizeof (WEdit));
917 e->widget = edit->widget;
918 if (!edit_init (e, lines, columns, filename, 0)) {
919 g_free (e);
920 return 0;
922 edit_clean (edit);
923 memcpy (edit, e, sizeof (WEdit));
924 g_free (e);
925 return 1;
929 * Load a new file into the editor and set line. If it fails, preserve the old file.
930 * To do it, allocate a new widget, initialize it and, if the new file
931 * was loaded, copy the data to the old widget.
932 * Return 1 on success, 0 on failure.
935 edit_reload_line (WEdit *edit, const char *filename, long line)
937 WEdit *e;
938 int lines = edit->num_widget_lines;
939 int columns = edit->num_widget_columns;
941 e = g_malloc0 (sizeof (WEdit));
942 e->widget = edit->widget;
943 if (!edit_init (e, lines, columns, filename, line)) {
944 g_free (e);
945 return 0;
947 edit_clean (edit);
948 memcpy (edit, e, sizeof (WEdit));
949 g_free (e);
950 return 1;
955 Recording stack for undo:
956 The following is an implementation of a compressed stack. Identical
957 pushes are recorded by a negative prefix indicating the number of times the
958 same char was pushed. This saves space for repeated curs-left or curs-right
959 delete etc.
963 pushed: stored:
967 b -3
969 c --> -4
975 If the stack long int is 0-255 it represents a normal insert (from a backspace),
976 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
977 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
978 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
979 position.
981 The only way the cursor moves or the buffer is changed is through the routines:
982 insert, backspace, insert_ahead, delete, and cursor_move.
983 These record the reverse undo movements onto the stack each time they are
984 called.
986 Each key press results in a set of actions (insert; delete ...). So each time
987 a key is pressed the current position of start_display is pushed as
988 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
989 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
990 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
994 void edit_push_action (WEdit * edit, long c,...)
996 unsigned long sp = edit->stack_pointer;
997 unsigned long spm1;
998 long *t;
1000 /* first enlarge the stack if necessary */
1001 if (sp > edit->stack_size - 10) { /* say */
1002 if (option_max_undo < 256)
1003 option_max_undo = 256;
1004 if (edit->stack_size < (unsigned long) option_max_undo) {
1005 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1006 if (t) {
1007 edit->undo_stack = t;
1008 edit->stack_size <<= 1;
1009 edit->stack_size_mask = edit->stack_size - 1;
1013 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1014 if (edit->stack_disable)
1015 return;
1017 #ifdef FAST_MOVE_CURSOR
1018 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1019 va_list ap;
1020 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1021 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1022 va_start (ap, c);
1023 c = -(va_arg (ap, int));
1024 va_end (ap);
1025 } else
1026 #endif /* ! FAST_MOVE_CURSOR */
1027 if (edit->stack_bottom != sp
1028 && spm1 != edit->stack_bottom
1029 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1030 int d;
1031 if (edit->undo_stack[spm1] < 0) {
1032 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1033 if (d == c) {
1034 if (edit->undo_stack[spm1] > -1000000000) {
1035 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1036 edit->undo_stack[spm1]--;
1037 return;
1040 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1041 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1042 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1043 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1044 if (edit->undo_stack[spm1] == -2)
1045 edit->stack_pointer = spm1;
1046 else
1047 edit->undo_stack[spm1]++;
1048 return;
1050 #endif
1051 } else {
1052 d = edit->undo_stack[spm1];
1053 if (d == c) {
1054 if (c >= KEY_PRESS)
1055 return; /* --> no need to push multiple do-nothings */
1056 edit->undo_stack[sp] = -2;
1057 goto check_bottom;
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 edit->stack_pointer = spm1;
1063 return;
1065 #endif
1068 edit->undo_stack[sp] = c;
1069 check_bottom:
1071 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1073 /* if the sp wraps round and catches the stack_bottom then erase
1074 * the first set of actions on the stack to make space - by moving
1075 * stack_bottom forward one "key press" */
1076 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1077 if ((unsigned long) c == edit->stack_bottom ||
1078 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1079 do {
1080 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1081 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1083 /*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: */
1084 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1085 edit->stack_bottom = edit->stack_pointer = 0;
1089 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1090 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1092 static long
1093 pop_action (WEdit * edit)
1095 long c;
1096 unsigned long sp = edit->stack_pointer;
1097 if (sp == edit->stack_bottom) {
1098 return STACK_BOTTOM;
1100 sp = (sp - 1) & edit->stack_size_mask;
1101 if ((c = edit->undo_stack[sp]) >= 0) {
1102 /* edit->undo_stack[sp] = '@'; */
1103 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1104 return c;
1106 if (sp == edit->stack_bottom) {
1107 return STACK_BOTTOM;
1109 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1110 if (edit->undo_stack[sp] == -2) {
1111 /* edit->undo_stack[sp] = '@'; */
1112 edit->stack_pointer = sp;
1113 } else
1114 edit->undo_stack[sp]++;
1116 return c;
1119 /* is called whenever a modification is made by one of the four routines below */
1120 static void edit_modification (WEdit * edit)
1122 edit->caches_valid = 0;
1123 edit->screen_modified = 1;
1125 /* raise lock when file modified */
1126 if (!edit->modified && !edit->delete_file)
1127 edit->locked = edit_lock_file (edit->filename);
1128 edit->modified = 1;
1132 Basic low level single character buffer alterations and movements at the cursor.
1133 Returns char passed over, inserted or removed.
1136 void
1137 edit_insert (WEdit *edit, int c)
1139 /* check if file has grown to large */
1140 if (edit->last_byte >= SIZE_LIMIT)
1141 return;
1143 /* first we must update the position of the display window */
1144 if (edit->curs1 < edit->start_display) {
1145 edit->start_display++;
1146 if (c == '\n')
1147 edit->start_line++;
1150 /* Mark file as modified, unless the file hasn't been fully loaded */
1151 if (edit->loading_done) {
1152 edit_modification (edit);
1155 /* now we must update some info on the file and check if a redraw is required */
1156 if (c == '\n') {
1157 if (edit->book_mark)
1158 book_mark_inc (edit, edit->curs_line);
1159 edit->curs_line++;
1160 edit->total_lines++;
1161 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1164 /* save the reverse command onto the undo stack */
1165 edit_push_action (edit, BACKSPACE);
1167 /* update markers */
1168 edit->mark1 += (edit->mark1 > edit->curs1);
1169 edit->mark2 += (edit->mark2 > edit->curs1);
1170 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1172 /* add a new buffer if we've reached the end of the last one */
1173 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1174 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1175 g_malloc (EDIT_BUF_SIZE);
1177 /* perform the insertion */
1178 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1179 curs1 & M_EDIT_BUF_SIZE]
1180 = (unsigned char) c;
1182 /* update file length */
1183 edit->last_byte++;
1185 /* update cursor position */
1186 edit->curs1++;
1189 void
1190 edit_insert_over (WEdit * edit)
1192 int i;
1194 for ( i = 0; i < edit->over_col; i++ ) {
1195 edit_insert (edit, ' ');
1197 edit->over_col = 0;
1200 /* same as edit_insert and move left */
1201 void edit_insert_ahead (WEdit * edit, int c)
1203 if (edit->last_byte >= SIZE_LIMIT)
1204 return;
1205 if (edit->curs1 < edit->start_display) {
1206 edit->start_display++;
1207 if (c == '\n')
1208 edit->start_line++;
1210 edit_modification (edit);
1211 if (c == '\n') {
1212 if (edit->book_mark)
1213 book_mark_inc (edit, edit->curs_line);
1214 edit->total_lines++;
1215 edit->force |= REDRAW_AFTER_CURSOR;
1217 edit_push_action (edit, DELCHAR);
1219 edit->mark1 += (edit->mark1 >= edit->curs1);
1220 edit->mark2 += (edit->mark2 >= edit->curs1);
1221 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1223 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1224 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1225 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1227 edit->last_byte++;
1228 edit->curs2++;
1232 int edit_delete (WEdit * edit, const int byte_delete)
1234 int p = 0;
1235 int cw = 1;
1236 int i;
1238 if (!edit->curs2)
1239 return 0;
1241 edit->mark1 -= (edit->mark1 > edit->curs1);
1242 edit->mark2 -= (edit->mark2 > edit->curs1);
1243 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1245 cw = 1;
1246 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1247 if ( edit->utf8 && byte_delete == 0 ) {
1248 edit_get_utf (edit, edit->curs1, &cw);
1249 if ( cw < 1 )
1250 cw = 1;
1252 for ( i = 1; i<= cw; i++ ) {
1253 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1255 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1256 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1257 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1259 edit->last_byte--;
1260 edit->curs2--;
1261 edit_push_action (edit, p + 256);
1264 edit_modification (edit);
1265 if (p == '\n') {
1266 if (edit->book_mark)
1267 book_mark_dec (edit, edit->curs_line);
1268 edit->total_lines--;
1269 edit->force |= REDRAW_AFTER_CURSOR;
1271 if (edit->curs1 < edit->start_display) {
1272 edit->start_display--;
1273 if (p == '\n')
1274 edit->start_line--;
1277 return p;
1281 static int
1282 edit_backspace (WEdit * edit, const int byte_delete)
1284 int p = 0;
1285 int cw = 1;
1286 int i;
1288 if (!edit->curs1)
1289 return 0;
1291 edit->mark1 -= (edit->mark1 >= edit->curs1);
1292 edit->mark2 -= (edit->mark2 >= edit->curs1);
1293 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1295 cw = 1;
1296 if ( edit->utf8 && byte_delete == 0 ) {
1297 edit_get_prev_utf (edit, edit->curs1, &cw);
1298 if ( cw < 1 )
1299 cw = 1;
1301 for ( i = 1; i<= cw; i++ ) {
1302 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1303 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1304 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1305 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1307 edit->last_byte--;
1308 edit->curs1--;
1309 edit_push_action (edit, p);
1311 edit_modification (edit);
1312 if (p == '\n') {
1313 if (edit->book_mark)
1314 book_mark_dec (edit, edit->curs_line);
1315 edit->curs_line--;
1316 edit->total_lines--;
1317 edit->force |= REDRAW_AFTER_CURSOR;
1320 if (edit->curs1 < edit->start_display) {
1321 edit->start_display--;
1322 if (p == '\n')
1323 edit->start_line--;
1326 return p;
1329 #ifdef FAST_MOVE_CURSOR
1331 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1333 unsigned long next;
1334 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1335 edit->curs_line--;
1336 next -= (unsigned long) dest;
1337 n -= next;
1338 src += next;
1339 dest += next;
1344 edit_move_backward_lots (WEdit *edit, long increment)
1346 int r, s, t;
1347 unsigned char *p;
1349 if (increment > edit->curs1)
1350 increment = edit->curs1;
1351 if (increment <= 0)
1352 return -1;
1353 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1355 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1356 if (r > increment)
1357 r = increment;
1358 s = edit->curs1 & M_EDIT_BUF_SIZE;
1360 p = 0;
1361 if (s > r) {
1362 memqcpy (edit,
1363 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1364 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1366 } else {
1367 if (s) {
1368 memqcpy (edit,
1369 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1370 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1371 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1372 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1374 memqcpy (edit,
1375 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1376 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1377 EDIT_BUF_SIZE - (r - s), r - s);
1379 increment -= r;
1380 edit->curs1 -= r;
1381 edit->curs2 += r;
1382 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1383 if (p)
1384 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1385 else
1386 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1387 g_malloc (EDIT_BUF_SIZE);
1388 } else {
1389 g_free (p);
1392 s = edit->curs1 & M_EDIT_BUF_SIZE;
1393 while (increment) {
1394 p = 0;
1395 r = EDIT_BUF_SIZE;
1396 if (r > increment)
1397 r = increment;
1398 t = s;
1399 if (r < t)
1400 t = r;
1401 memqcpy (edit,
1402 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1403 EDIT_BUF_SIZE - t,
1404 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1406 if (r >= s) {
1407 if (t) {
1408 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1409 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1411 memqcpy (edit,
1412 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1413 EDIT_BUF_SIZE - r,
1414 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1415 EDIT_BUF_SIZE - (r - s), r - s);
1417 increment -= r;
1418 edit->curs1 -= r;
1419 edit->curs2 += r;
1420 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1421 if (p)
1422 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1423 else
1424 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1425 g_malloc (EDIT_BUF_SIZE);
1426 } else {
1427 g_free (p);
1430 return edit_get_byte (edit, edit->curs1);
1433 #endif /* ! FAST_MOVE_CURSOR */
1435 /* moves the cursor right or left: increment positive or negative respectively */
1436 void edit_cursor_move (WEdit * edit, long increment)
1438 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1439 int c;
1441 #ifdef FAST_MOVE_CURSOR
1442 if (increment < -256) {
1443 edit->force |= REDRAW_PAGE;
1444 edit_move_backward_lots (edit, -increment);
1445 return;
1447 #endif /* ! FAST_MOVE_CURSOR */
1449 if (increment < 0) {
1450 for (; increment < 0; increment++) {
1451 if (!edit->curs1)
1452 return;
1454 edit_push_action (edit, CURS_RIGHT);
1456 c = edit_get_byte (edit, edit->curs1 - 1);
1457 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1458 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1459 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1460 edit->curs2++;
1461 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1462 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1463 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1464 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1466 edit->curs1--;
1467 if (c == '\n') {
1468 edit->curs_line--;
1469 edit->force |= REDRAW_LINE_BELOW;
1473 } else if (increment > 0) {
1474 for (; increment > 0; increment--) {
1475 if (!edit->curs2)
1476 return;
1478 edit_push_action (edit, CURS_LEFT);
1480 c = edit_get_byte (edit, edit->curs1);
1481 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1482 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1483 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1484 edit->curs1++;
1485 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1486 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1487 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1488 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1490 edit->curs2--;
1491 if (c == '\n') {
1492 edit->curs_line++;
1493 edit->force |= REDRAW_LINE_ABOVE;
1499 /* These functions return positions relative to lines */
1501 /* returns index of last char on line + 1 */
1502 long edit_eol (WEdit * edit, long current)
1504 if (current < edit->last_byte) {
1505 for (;; current++)
1506 if (edit_get_byte (edit, current) == '\n')
1507 break;
1508 } else
1509 return edit->last_byte;
1510 return current;
1513 /* returns index of first char on line */
1514 long edit_bol (WEdit * edit, long current)
1516 if (current > 0) {
1517 for (;; current--)
1518 if (edit_get_byte (edit, current - 1) == '\n')
1519 break;
1520 } else
1521 return 0;
1522 return current;
1526 int edit_count_lines (WEdit * edit, long current, int upto)
1528 int lines = 0;
1529 if (upto > edit->last_byte)
1530 upto = edit->last_byte;
1531 if (current < 0)
1532 current = 0;
1533 while (current < upto)
1534 if (edit_get_byte (edit, current++) == '\n')
1535 lines++;
1536 return lines;
1540 /* If lines is zero this returns the count of lines from current to upto. */
1541 /* If upto is zero returns index of lines forward current. */
1542 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1544 if (upto) {
1545 return edit_count_lines (edit, current, upto);
1546 } else {
1547 int next;
1548 if (lines < 0)
1549 lines = 0;
1550 while (lines--) {
1551 next = edit_eol (edit, current) + 1;
1552 if (next > edit->last_byte)
1553 break;
1554 else
1555 current = next;
1557 return current;
1562 /* Returns offset of 'lines' lines up from current */
1563 long edit_move_backward (WEdit * edit, long current, int lines)
1565 if (lines < 0)
1566 lines = 0;
1567 current = edit_bol (edit, current);
1568 while((lines--) && current != 0)
1569 current = edit_bol (edit, current - 1);
1570 return current;
1573 /* If cols is zero this returns the count of columns from current to upto. */
1574 /* If upto is zero returns index of cols across from current. */
1575 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1577 long p, q;
1578 int col = 0;
1579 #ifdef HAVE_CHARSET
1580 int cw = 1;
1581 int utf_ch = 0;
1582 #endif
1583 if (upto) {
1584 q = upto;
1585 cols = -10;
1586 } else
1587 q = edit->last_byte + 2;
1588 for (col = 0, p = current; p < q; p++) {
1589 int c;
1590 #ifdef HAVE_CHARSET
1591 cw = 1;
1592 utf_ch = 0;
1593 #endif
1594 if (cols != -10) {
1595 if (col == cols)
1596 return p;
1597 if (col > cols)
1598 return p - 1;
1600 #ifdef HAVE_CHARSET
1601 if ( !edit->utf8 ) {
1602 #endif
1603 c = edit_get_byte (edit, p);
1604 #ifdef HAVE_CHARSET
1605 } else {
1606 cw = 1;
1607 c = edit_get_byte (edit, p);
1608 utf_ch = edit_get_utf (edit, p, &cw);
1610 if ( utf8_display ) {
1611 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1612 col++;
1614 #endif
1615 if (c == '\t')
1616 col += TAB_SIZE - col % TAB_SIZE;
1617 else if (c == '\n') {
1618 if (upto)
1619 return col;
1620 else
1621 return p;
1622 } else if (c < 32 || c == 127)
1623 col += 2; /* Caret notation for control characters */
1624 else
1625 col++;
1626 #ifdef HAVE_CHARSET
1627 if ( cw > 1 )
1628 col -= cw-1;
1629 #endif
1631 return col;
1634 /* returns the current column position of the cursor */
1635 int edit_get_col (WEdit * edit)
1637 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1641 /* Scrolling functions */
1643 void edit_update_curs_row (WEdit * edit)
1645 edit->curs_row = edit->curs_line - edit->start_line;
1648 void edit_update_curs_col (WEdit * edit)
1650 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1654 edit_get_curs_col (const WEdit *edit)
1656 return edit->curs_col;
1659 /*moves the display start position up by i lines */
1660 void edit_scroll_upward (WEdit * edit, unsigned long i)
1662 unsigned long lines_above = edit->start_line;
1663 if (i > lines_above)
1664 i = lines_above;
1665 if (i) {
1666 edit->start_line -= i;
1667 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1668 edit->force |= REDRAW_PAGE;
1669 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1671 edit_update_curs_row (edit);
1675 /* returns 1 if could scroll, 0 otherwise */
1676 void edit_scroll_downward (WEdit * edit, int i)
1678 int lines_below;
1679 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1680 if (lines_below > 0) {
1681 if (i > lines_below)
1682 i = lines_below;
1683 edit->start_line += i;
1684 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1685 edit->force |= REDRAW_PAGE;
1686 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1688 edit_update_curs_row (edit);
1691 void edit_scroll_right (WEdit * edit, int i)
1693 edit->force |= REDRAW_PAGE;
1694 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1695 edit->start_col -= i;
1698 void edit_scroll_left (WEdit * edit, int i)
1700 if (edit->start_col) {
1701 edit->start_col += i;
1702 if (edit->start_col > 0)
1703 edit->start_col = 0;
1704 edit->force |= REDRAW_PAGE;
1705 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1709 /* high level cursor movement commands */
1711 static int is_in_indent (WEdit *edit)
1713 long p = edit_bol (edit, edit->curs1);
1714 while (p < edit->curs1)
1715 if (!strchr (" \t", edit_get_byte (edit, p++)))
1716 return 0;
1717 return 1;
1720 static int left_of_four_spaces (WEdit *edit);
1722 void
1723 edit_move_to_prev_col (WEdit * edit, long p)
1725 int prev = edit->prev_col;
1726 int over = edit->over_col;
1728 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1730 if (option_cursor_beyond_eol) {
1731 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1733 if (line_len < prev + edit->over_col) {
1734 edit->over_col = prev + over - line_len;
1735 edit->prev_col = line_len;
1736 edit->curs_col = line_len;
1737 } else {
1738 edit->curs_col = prev + over;
1739 edit->prev_col = edit->curs_col;
1740 edit->over_col = 0;
1742 } else {
1743 edit->over_col = 0;
1744 if (is_in_indent (edit) && option_fake_half_tabs) {
1745 edit_update_curs_col (edit);
1746 if (space_width)
1747 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1748 int q = edit->curs_col;
1749 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1750 p = edit_bol (edit, edit->curs1);
1751 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1752 if (!left_of_four_spaces (edit))
1753 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1759 /* move i lines */
1760 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1762 unsigned long p, l = edit->curs_line;
1764 if (i > l)
1765 i = l;
1766 if (i) {
1767 if (i > 1)
1768 edit->force |= REDRAW_PAGE;
1769 if (scroll)
1770 edit_scroll_upward (edit, i);
1772 p = edit_bol (edit, edit->curs1);
1773 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1774 edit_move_to_prev_col (edit, p);
1776 edit->search_start = edit->curs1;
1777 edit->found_len = 0;
1781 static int
1782 is_blank (WEdit *edit, long offset)
1784 long s, f;
1785 int c;
1786 s = edit_bol (edit, offset);
1787 f = edit_eol (edit, offset) - 1;
1788 while (s <= f) {
1789 c = edit_get_byte (edit, s++);
1790 if (!isspace (c))
1791 return 0;
1793 return 1;
1797 /* returns the offset of line i */
1798 static long
1799 edit_find_line (WEdit *edit, int line)
1801 int i, j = 0;
1802 int m = 2000000000;
1803 if (!edit->caches_valid) {
1804 for (i = 0; i < N_LINE_CACHES; i++)
1805 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1806 /* three offsets that we *know* are line 0 at 0 and these two: */
1807 edit->line_numbers[1] = edit->curs_line;
1808 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1809 edit->line_numbers[2] = edit->total_lines;
1810 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1811 edit->caches_valid = 1;
1813 if (line >= edit->total_lines)
1814 return edit->line_offsets[2];
1815 if (line <= 0)
1816 return 0;
1817 /* find the closest known point */
1818 for (i = 0; i < N_LINE_CACHES; i++) {
1819 int n;
1820 n = abs (edit->line_numbers[i] - line);
1821 if (n < m) {
1822 m = n;
1823 j = i;
1826 if (m == 0)
1827 return edit->line_offsets[j]; /* know the offset exactly */
1828 if (m == 1 && j >= 3)
1829 i = j; /* one line different - caller might be looping, so stay in this cache */
1830 else
1831 i = 3 + (rand () % (N_LINE_CACHES - 3));
1832 if (line > edit->line_numbers[j])
1833 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1834 else
1835 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1836 edit->line_numbers[i] = line;
1837 return edit->line_offsets[i];
1840 int line_is_blank (WEdit * edit, long line)
1842 return is_blank (edit, edit_find_line (edit, line));
1845 /* moves up until a blank line is reached, or until just
1846 before a non-blank line is reached */
1847 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1849 int i;
1850 if (edit->curs_line <= 1) {
1851 i = 0;
1852 } else {
1853 if (line_is_blank (edit, edit->curs_line)) {
1854 if (line_is_blank (edit, edit->curs_line - 1)) {
1855 for (i = edit->curs_line - 1; i; i--)
1856 if (!line_is_blank (edit, i)) {
1857 i++;
1858 break;
1860 } else {
1861 for (i = edit->curs_line - 1; i; i--)
1862 if (line_is_blank (edit, i))
1863 break;
1865 } else {
1866 for (i = edit->curs_line - 1; i; i--)
1867 if (line_is_blank (edit, i))
1868 break;
1871 edit_move_up (edit, edit->curs_line - i, scroll);
1874 /* move i lines */
1875 void edit_move_down (WEdit * edit, int i, int scroll)
1877 long p, l = edit->total_lines - edit->curs_line;
1879 if (i > l)
1880 i = l;
1881 if (i) {
1882 if (i > 1)
1883 edit->force |= REDRAW_PAGE;
1884 if (scroll)
1885 edit_scroll_downward (edit, i);
1886 p = edit_bol (edit, edit->curs1);
1887 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1888 edit_move_to_prev_col (edit, p);
1890 edit->search_start = edit->curs1;
1891 edit->found_len = 0;
1895 /* moves down until a blank line is reached, or until just
1896 before a non-blank line is reached */
1897 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1899 int i;
1900 if (edit->curs_line >= edit->total_lines - 1) {
1901 i = edit->total_lines;
1902 } else {
1903 if (line_is_blank (edit, edit->curs_line)) {
1904 if (line_is_blank (edit, edit->curs_line + 1)) {
1905 for (i = edit->curs_line + 1; i; i++)
1906 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1907 i--;
1908 break;
1910 } else {
1911 for (i = edit->curs_line + 1; i; i++)
1912 if (line_is_blank (edit, i) || i >= edit->total_lines)
1913 break;
1915 } else {
1916 for (i = edit->curs_line + 1; i; i++)
1917 if (line_is_blank (edit, i) || i >= edit->total_lines)
1918 break;
1921 edit_move_down (edit, i - edit->curs_line, scroll);
1924 static void edit_begin_page (WEdit *edit)
1926 edit_update_curs_row (edit);
1927 edit_move_up (edit, edit->curs_row, 0);
1930 static void edit_end_page (WEdit *edit)
1932 edit_update_curs_row (edit);
1933 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1937 /* goto beginning of text */
1938 static void edit_move_to_top (WEdit * edit)
1940 if (edit->curs_line) {
1941 edit_cursor_move (edit, -edit->curs1);
1942 edit_move_to_prev_col (edit, 0);
1943 edit->force |= REDRAW_PAGE;
1944 edit->search_start = 0;
1945 edit_update_curs_row(edit);
1950 /* goto end of text */
1951 static void edit_move_to_bottom (WEdit * edit)
1953 if (edit->curs_line < edit->total_lines) {
1954 edit_cursor_move (edit, edit->curs2);
1955 edit->start_display = edit->last_byte;
1956 edit->start_line = edit->total_lines;
1957 edit_update_curs_row(edit);
1958 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1959 edit->force |= REDRAW_PAGE;
1963 /* goto beginning of line */
1964 static void edit_cursor_to_bol (WEdit * edit)
1966 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1967 edit->search_start = edit->curs1;
1968 edit->prev_col = edit_get_col (edit);
1969 edit->over_col = 0;
1972 /* goto end of line */
1973 static void edit_cursor_to_eol (WEdit * edit)
1975 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1976 edit->search_start = edit->curs1;
1977 edit->prev_col = edit_get_col (edit);
1978 edit->over_col = 0;
1981 /* move cursor to line 'line' */
1982 void edit_move_to_line (WEdit * e, long line)
1984 if(line < e->curs_line)
1985 edit_move_up (e, e->curs_line - line, 0);
1986 else
1987 edit_move_down (e, line - e->curs_line, 0);
1988 edit_scroll_screen_over_cursor (e);
1991 /* scroll window so that first visible line is 'line' */
1992 void edit_move_display (WEdit * e, long line)
1994 if(line < e->start_line)
1995 edit_scroll_upward (e, e->start_line - line);
1996 else
1997 edit_scroll_downward (e, line - e->start_line);
2000 /* save markers onto undo stack */
2001 void edit_push_markers (WEdit * edit)
2003 edit_push_action (edit, MARK_1 + edit->mark1);
2004 edit_push_action (edit, MARK_2 + edit->mark2);
2007 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2009 edit->mark1 = m1;
2010 edit->mark2 = m2;
2011 edit->column1 = c1;
2012 edit->column2 = c2;
2016 /* highlight marker toggle */
2017 void edit_mark_cmd (WEdit * edit, int unmark)
2019 edit_push_markers (edit);
2020 if (unmark) {
2021 edit_set_markers (edit, 0, 0, 0, 0);
2022 edit->force |= REDRAW_PAGE;
2023 } else {
2024 if (edit->mark2 >= 0) {
2025 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
2026 edit->force |= REDRAW_PAGE;
2027 } else
2028 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2032 static unsigned long
2033 my_type_of (int c)
2035 int x, r = 0;
2036 const char *p, *q;
2037 const char option_chars_move_whole_word[] =
2038 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2040 if (!c)
2041 return 0;
2042 if (c == '!') {
2043 if (*option_chars_move_whole_word == '!')
2044 return 2;
2045 return 0x80000000UL;
2047 if (g_ascii_isupper ((gchar) c))
2048 c = 'A';
2049 else if (g_ascii_islower ((gchar) c))
2050 c = 'a';
2051 else if (g_ascii_isalpha (c))
2052 c = 'a';
2053 else if (isdigit (c))
2054 c = '0';
2055 else if (isspace (c))
2056 c = ' ';
2057 q = strchr (option_chars_move_whole_word, c);
2058 if (!q)
2059 return 0xFFFFFFFFUL;
2060 do {
2061 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2062 if (*p == '!')
2063 x <<= 1;
2064 r |= x;
2065 } while ((q = strchr (q + 1, c)));
2066 return r;
2069 static void
2070 edit_left_word_move (WEdit *edit, int s)
2072 for (;;) {
2073 int c1, c2;
2074 edit_cursor_move (edit, -1);
2075 if (!edit->curs1)
2076 break;
2077 c1 = edit_get_byte (edit, edit->curs1 - 1);
2078 c2 = edit_get_byte (edit, edit->curs1);
2079 if (!(my_type_of (c1) & my_type_of (c2)))
2080 break;
2081 if (isspace (c1) && !isspace (c2))
2082 break;
2083 if (s)
2084 if (!isspace (c1) && isspace (c2))
2085 break;
2089 static void edit_left_word_move_cmd (WEdit * edit)
2091 edit_left_word_move (edit, 0);
2092 edit->force |= REDRAW_PAGE;
2095 static void
2096 edit_right_word_move (WEdit *edit, int s)
2098 for (;;) {
2099 int c1, c2;
2100 edit_cursor_move (edit, 1);
2101 if (edit->curs1 >= edit->last_byte)
2102 break;
2103 c1 = edit_get_byte (edit, edit->curs1 - 1);
2104 c2 = edit_get_byte (edit, edit->curs1);
2105 if (!(my_type_of (c1) & my_type_of (c2)))
2106 break;
2107 if (isspace (c1) && !isspace (c2))
2108 break;
2109 if (s)
2110 if (!isspace (c1) && isspace (c2))
2111 break;
2115 static void edit_right_word_move_cmd (WEdit * edit)
2117 edit_right_word_move (edit, 0);
2118 edit->force |= REDRAW_PAGE;
2121 static void edit_right_char_move_cmd (WEdit * edit)
2123 int cw = 1;
2124 int c = 0;
2125 if ( edit->utf8 ) {
2126 c = edit_get_utf (edit, edit->curs1, &cw);
2127 if ( cw < 1 )
2128 cw = 1;
2129 } else {
2130 c = edit_get_byte (edit, edit->curs1);
2132 if (option_cursor_beyond_eol && c == '\n') {
2133 edit->over_col++;
2134 } else {
2135 edit_cursor_move (edit, cw);
2139 static void edit_left_char_move_cmd (WEdit * edit)
2141 int cw = 1;
2142 if ( edit->utf8 ) {
2143 edit_get_prev_utf (edit, edit->curs1, &cw);
2144 if ( cw < 1 )
2145 cw = 1;
2147 if (option_cursor_beyond_eol && edit->over_col > 0) {
2148 edit->over_col--;
2149 } else {
2150 edit_cursor_move (edit, -cw);
2155 static void edit_right_delete_word (WEdit * edit)
2157 int c1, c2;
2158 for (;;) {
2159 if (edit->curs1 >= edit->last_byte)
2160 break;
2161 c1 = edit_delete (edit, 1);
2162 c2 = edit_get_byte (edit, edit->curs1);
2163 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2164 break;
2165 if (!(my_type_of (c1) & my_type_of (c2)))
2166 break;
2170 static void edit_left_delete_word (WEdit * edit)
2172 int c1, c2;
2173 for (;;) {
2174 if (edit->curs1 <= 0)
2175 break;
2176 c1 = edit_backspace (edit, 1);
2177 c2 = edit_get_byte (edit, edit->curs1 - 1);
2178 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2179 break;
2180 if (!(my_type_of (c1) & my_type_of (c2)))
2181 break;
2186 the start column position is not recorded, and hence does not
2187 undo as it happed. But who would notice.
2189 static void
2190 edit_do_undo (WEdit * edit)
2192 long ac;
2193 long count = 0;
2195 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2196 edit->over_col = 0;
2197 while ((ac = pop_action (edit)) < KEY_PRESS) {
2198 switch ((int) ac) {
2199 case STACK_BOTTOM:
2200 goto done_undo;
2201 case CURS_RIGHT:
2202 edit_cursor_move (edit, 1);
2203 break;
2204 case CURS_LEFT:
2205 edit_cursor_move (edit, -1);
2206 break;
2207 case BACKSPACE:
2208 edit_backspace (edit, 1);
2209 break;
2210 case DELCHAR:
2211 edit_delete (edit, 1);
2212 break;
2213 case COLUMN_ON:
2214 column_highlighting = 1;
2215 break;
2216 case COLUMN_OFF:
2217 column_highlighting = 0;
2218 break;
2220 if (ac >= 256 && ac < 512)
2221 edit_insert_ahead (edit, ac - 256);
2222 if (ac >= 0 && ac < 256)
2223 edit_insert (edit, ac);
2225 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2226 edit->mark1 = ac - MARK_1;
2227 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2228 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2229 edit->mark2 = ac - MARK_2;
2230 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2232 if (count++)
2233 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2236 if (edit->start_display > ac - KEY_PRESS) {
2237 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2238 edit->force |= REDRAW_PAGE;
2239 } else if (edit->start_display < ac - KEY_PRESS) {
2240 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2241 edit->force |= REDRAW_PAGE;
2243 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2244 edit_update_curs_row (edit);
2246 done_undo:;
2247 edit->stack_disable = 0;
2250 static void edit_delete_to_line_end (WEdit * edit)
2252 while (edit_get_byte (edit, edit->curs1) != '\n') {
2253 if (!edit->curs2)
2254 break;
2255 edit_delete (edit, 1);
2259 static void edit_delete_to_line_begin (WEdit * edit)
2261 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2262 if (!edit->curs1)
2263 break;
2264 edit_backspace (edit, 1);
2268 void
2269 edit_delete_line (WEdit *edit)
2272 * Delete right part of the line.
2273 * Note that edit_get_byte() returns '\n' when byte position is
2274 * beyond EOF.
2276 while (edit_get_byte (edit, edit->curs1) != '\n') {
2277 (void) edit_delete (edit, 1);
2281 * Delete '\n' char.
2282 * Note that edit_delete() will not corrupt anything if called while
2283 * cursor position is EOF.
2285 (void) edit_delete (edit, 1);
2288 * Delete left part of the line.
2289 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2291 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2292 (void) edit_backspace (edit, 1);
2296 void insert_spaces_tab (WEdit * edit, int half)
2298 int i;
2299 edit_update_curs_col (edit);
2300 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2301 while (i > 0) {
2302 edit_insert (edit, ' ');
2303 i -= space_width;
2307 static int is_aligned_on_a_tab (WEdit * edit)
2309 edit_update_curs_col (edit);
2310 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2311 return 0; /* not alligned on a tab */
2312 return 1;
2315 static int right_of_four_spaces (WEdit *edit)
2317 int i, ch = 0;
2318 for (i = 1; i <= HALF_TAB_SIZE; i++)
2319 ch |= edit_get_byte (edit, edit->curs1 - i);
2320 if (ch == ' ')
2321 return is_aligned_on_a_tab (edit);
2322 return 0;
2325 static int left_of_four_spaces (WEdit *edit)
2327 int i, ch = 0;
2328 for (i = 0; i < HALF_TAB_SIZE; i++)
2329 ch |= edit_get_byte (edit, edit->curs1 + i);
2330 if (ch == ' ')
2331 return is_aligned_on_a_tab (edit);
2332 return 0;
2335 int edit_indent_width (WEdit * edit, long p)
2337 long q = p;
2338 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2339 q++;
2340 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2343 void edit_insert_indent (WEdit * edit, int indent)
2345 if (!option_fill_tabs_with_spaces) {
2346 while (indent >= TAB_SIZE) {
2347 edit_insert (edit, '\t');
2348 indent -= TAB_SIZE;
2351 while (indent-- > 0)
2352 edit_insert (edit, ' ');
2355 static void
2356 edit_auto_indent (WEdit * edit)
2358 long p;
2359 char c;
2360 p = edit->curs1;
2361 /* use the previous line as a template */
2362 p = edit_move_backward (edit, p, 1);
2363 /* copy the leading whitespace of the line */
2364 for (;;) { /* no range check - the line _is_ \n-terminated */
2365 c = edit_get_byte (edit, p++);
2366 if (c != ' ' && c != '\t')
2367 break;
2368 edit_insert (edit, c);
2372 static void edit_double_newline (WEdit * edit)
2374 edit_insert (edit, '\n');
2375 if (edit_get_byte (edit, edit->curs1) == '\n')
2376 return;
2377 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2378 return;
2379 edit->force |= REDRAW_PAGE;
2380 edit_insert (edit, '\n');
2383 static void edit_tab_cmd (WEdit * edit)
2385 int i;
2387 if (option_fake_half_tabs) {
2388 if (is_in_indent (edit)) {
2389 /*insert a half tab (usually four spaces) unless there is a
2390 half tab already behind, then delete it and insert a
2391 full tab. */
2392 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2393 for (i = 1; i <= HALF_TAB_SIZE; i++)
2394 edit_backspace (edit, 1);
2395 edit_insert (edit, '\t');
2396 } else {
2397 insert_spaces_tab (edit, 1);
2399 return;
2402 if (option_fill_tabs_with_spaces) {
2403 insert_spaces_tab (edit, 0);
2404 } else {
2405 edit_insert (edit, '\t');
2407 return;
2410 static void check_and_wrap_line (WEdit * edit)
2412 int curs, c;
2413 if (!option_typewriter_wrap)
2414 return;
2415 edit_update_curs_col (edit);
2416 if (edit->curs_col < option_word_wrap_line_length)
2417 return;
2418 curs = edit->curs1;
2419 for (;;) {
2420 curs--;
2421 c = edit_get_byte (edit, curs);
2422 if (c == '\n' || curs <= 0) {
2423 edit_insert (edit, '\n');
2424 return;
2426 if (c == ' ' || c == '\t') {
2427 int current = edit->curs1;
2428 edit_cursor_move (edit, curs - edit->curs1 + 1);
2429 edit_insert (edit, '\n');
2430 edit_cursor_move (edit, current - edit->curs1 + 1);
2431 return;
2436 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2438 void edit_push_key_press (WEdit * edit)
2440 edit_push_action (edit, KEY_PRESS + edit->start_display);
2441 if (edit->mark2 == -1)
2442 edit_push_action (edit, MARK_1 + edit->mark1);
2445 /* this find the matching bracket in either direction, and sets edit->bracket */
2446 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2448 const char * const b = "{}{[][()(", *p;
2449 int i = 1, a, inc = -1, c, d, n = 0;
2450 unsigned long j = 0;
2451 long q;
2452 edit_update_curs_row (edit);
2453 c = edit_get_byte (edit, edit->curs1);
2454 p = strchr (b, c);
2455 /* no limit */
2456 if (!furthest_bracket_search)
2457 furthest_bracket_search--;
2458 /* not on a bracket at all */
2459 if (!p)
2460 return -1;
2461 /* the matching bracket */
2462 d = p[1];
2463 /* going left or right? */
2464 if (strchr ("{[(", c))
2465 inc = 1;
2466 for (q = edit->curs1 + inc;; q += inc) {
2467 /* out of buffer? */
2468 if (q >= edit->last_byte || q < 0)
2469 break;
2470 a = edit_get_byte (edit, q);
2471 /* don't want to eat CPU */
2472 if (j++ > furthest_bracket_search)
2473 break;
2474 /* out of screen? */
2475 if (in_screen) {
2476 if (q < edit->start_display)
2477 break;
2478 /* count lines if searching downward */
2479 if (inc > 0 && a == '\n')
2480 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2481 break;
2483 /* count bracket depth */
2484 i += (a == c) - (a == d);
2485 /* return if bracket depth is zero */
2486 if (!i)
2487 return q;
2489 /* no match */
2490 return -1;
2493 static long last_bracket = -1;
2495 void edit_find_bracket (WEdit * edit)
2497 edit->bracket = edit_get_bracket (edit, 1, 10000);
2498 if (last_bracket != edit->bracket)
2499 edit->force |= REDRAW_PAGE;
2500 last_bracket = edit->bracket;
2503 static void edit_goto_matching_bracket (WEdit *edit)
2505 long q;
2506 q = edit_get_bracket (edit, 0, 0);
2507 if (q < 0)
2508 return;
2509 edit->bracket = edit->curs1;
2510 edit->force |= REDRAW_PAGE;
2511 edit_cursor_move (edit, q - edit->curs1);
2515 * This executes a command as though the user initiated it through a key
2516 * press. Callback with WIDGET_KEY as a message calls this after
2517 * translating the key press. This function can be used to pass any
2518 * command to the editor. Note that the screen wouldn't update
2519 * automatically. Either of command or char_for_insertion must be
2520 * passed as -1. Commands are executed, and char_for_insertion is
2521 * inserted at the cursor.
2523 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2525 if (command == CK_Begin_Record_Macro) {
2526 edit->macro_i = 0;
2527 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2528 return;
2530 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2531 edit->force |= REDRAW_COMPLETELY;
2532 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2533 edit->macro_i = -1;
2534 return;
2536 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2537 edit->macro[edit->macro_i].command = command;
2538 edit->macro[edit->macro_i++].ch = char_for_insertion;
2540 /* record the beginning of a set of editing actions initiated by a key press */
2541 if (command != CK_Undo && command != CK_Ext_Mode)
2542 edit_push_key_press (edit);
2544 edit_execute_cmd (edit, command, char_for_insertion);
2545 if (column_highlighting)
2546 edit->force |= REDRAW_PAGE;
2549 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2552 This executes a command at a lower level than macro recording.
2553 It also does not push a key_press onto the undo stack. This means
2554 that if it is called many times, a single undo command will undo
2555 all of them. It also does not check for the Undo command.
2557 void
2558 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2560 edit->force |= REDRAW_LINE;
2562 /* The next key press will unhighlight the found string, so update
2563 * the whole page */
2564 if (edit->found_len || column_highlighting)
2565 edit->force |= REDRAW_PAGE;
2567 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2568 column_highlighting = 0;
2569 if (!edit->highlight
2570 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2571 edit_mark_cmd (edit, 1); /* clear */
2572 edit_mark_cmd (edit, 0); /* marking on */
2574 edit->highlight = 1;
2575 } else { /* any other command */
2576 if (edit->highlight)
2577 edit_mark_cmd (edit, 0); /* clear */
2578 edit->highlight = 0;
2581 /* first check for undo */
2582 if (command == CK_Undo) {
2583 edit_do_undo (edit);
2584 edit->found_len = 0;
2585 edit->prev_col = edit_get_col (edit);
2586 edit->search_start = edit->curs1;
2587 return;
2590 /* An ordinary key press */
2591 if (char_for_insertion >= 0) {
2592 if (edit->overwrite) {
2593 if (edit_get_byte (edit, edit->curs1) != '\n')
2594 edit_delete (edit, 0);
2596 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2597 edit_insert_over (edit);
2598 #ifdef HAVE_CHARSET
2599 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2600 unsigned char str[6 + 1];
2601 size_t i = 0;
2602 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2603 if ( res == 0 ) {
2604 str[0] = '.';
2605 str[1] = '\0';
2606 } else {
2607 str[res] = '\0';
2609 while ( str[i] != 0 && i<=6) {
2610 char_for_insertion = str[i];
2611 edit_insert (edit, char_for_insertion);
2612 i++;
2614 } else {
2615 #endif
2616 edit_insert (edit, char_for_insertion);
2617 #ifdef HAVE_CHARSET
2619 #endif
2620 if (option_auto_para_formatting) {
2621 format_paragraph (edit, 0);
2622 edit->force |= REDRAW_PAGE;
2623 } else
2624 check_and_wrap_line (edit);
2625 edit->found_len = 0;
2626 edit->prev_col = edit_get_col (edit);
2627 edit->search_start = edit->curs1;
2628 edit_find_bracket (edit);
2629 return;
2632 switch (command) {
2633 case CK_Begin_Page:
2634 case CK_End_Page:
2635 case CK_Begin_Page_Highlight:
2636 case CK_End_Page_Highlight:
2637 case CK_Word_Left:
2638 case CK_Word_Right:
2639 case CK_Up:
2640 case CK_Down:
2641 case CK_Left:
2642 case CK_Right:
2643 if ( edit->mark2 >= 0 ) {
2644 if ( !option_persistent_selections ) {
2645 if (column_highlighting)
2646 edit_push_action (edit, COLUMN_ON);
2647 column_highlighting = 0;
2648 edit_mark_cmd (edit, 1);
2653 switch (command) {
2654 case CK_Begin_Page:
2655 case CK_End_Page:
2656 case CK_Begin_Page_Highlight:
2657 case CK_End_Page_Highlight:
2658 case CK_Word_Left:
2659 case CK_Word_Right:
2660 case CK_Up:
2661 case CK_Down:
2662 case CK_Word_Left_Highlight:
2663 case CK_Word_Right_Highlight:
2664 case CK_Up_Highlight:
2665 case CK_Down_Highlight:
2666 case CK_Up_Alt_Highlight:
2667 case CK_Down_Alt_Highlight:
2668 if (edit->mark2 == -1)
2669 break; /*marking is following the cursor: may need to highlight a whole line */
2670 case CK_Left:
2671 case CK_Right:
2672 case CK_Left_Highlight:
2673 case CK_Right_Highlight:
2674 edit->force |= REDRAW_CHAR_ONLY;
2677 /* basic cursor key commands */
2678 switch (command) {
2679 case CK_BackSpace:
2680 /* if non persistent selection and text selected */
2681 if ( !option_persistent_selections ) {
2682 if ( edit->mark1 != edit->mark2 ) {
2683 edit_block_delete_cmd (edit);
2684 break;
2687 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2688 edit->over_col--;
2689 break;
2691 if (option_backspace_through_tabs && is_in_indent (edit)) {
2692 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2693 && edit->curs1 > 0)
2694 edit_backspace (edit, 1);
2695 break;
2696 } else {
2697 if (option_fake_half_tabs) {
2698 int i;
2699 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2700 for (i = 0; i < HALF_TAB_SIZE; i++)
2701 edit_backspace (edit, 1);
2702 break;
2706 edit_backspace (edit, 0);
2707 break;
2708 case CK_Delete:
2709 /* if non persistent selection and text selected */
2710 if ( !option_persistent_selections ) {
2711 if ( edit->mark1 != edit->mark2 ) {
2712 edit_block_delete_cmd (edit);
2713 break;
2717 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2718 edit_insert_over (edit);
2720 if (option_fake_half_tabs) {
2721 int i;
2722 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2723 for (i = 1; i <= HALF_TAB_SIZE; i++)
2724 edit_delete (edit, 1);
2725 break;
2728 edit_delete (edit, 0);
2729 break;
2730 case CK_Delete_Word_Left:
2731 edit->over_col = 0;
2732 edit_left_delete_word (edit);
2733 break;
2734 case CK_Delete_Word_Right:
2735 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2736 edit_insert_over (edit);
2738 edit_right_delete_word (edit);
2739 break;
2740 case CK_Delete_Line:
2741 edit_delete_line (edit);
2742 break;
2743 case CK_Delete_To_Line_End:
2744 edit_delete_to_line_end (edit);
2745 break;
2746 case CK_Delete_To_Line_Begin:
2747 edit_delete_to_line_begin (edit);
2748 break;
2749 case CK_Enter:
2750 edit->over_col = 0;
2751 if (option_auto_para_formatting) {
2752 edit_double_newline (edit);
2753 if (option_return_does_auto_indent)
2754 edit_auto_indent (edit);
2755 format_paragraph (edit, 0);
2756 } else {
2757 edit_insert (edit, '\n');
2758 if (option_return_does_auto_indent) {
2759 edit_auto_indent (edit);
2762 break;
2763 case CK_Return:
2764 edit_insert (edit, '\n');
2765 break;
2767 case CK_Page_Up_Alt_Highlight:
2768 column_highlighting = 1;
2769 case CK_Page_Up:
2770 case CK_Page_Up_Highlight:
2771 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2772 break;
2773 case CK_Page_Down_Alt_Highlight:
2774 column_highlighting = 1;
2775 case CK_Page_Down:
2776 case CK_Page_Down_Highlight:
2777 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2778 break;
2779 case CK_Left_Alt_Highlight:
2780 column_highlighting = 1;
2781 case CK_Left:
2782 case CK_Left_Highlight:
2783 if (option_fake_half_tabs) {
2784 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2785 if ( option_cursor_beyond_eol && edit->over_col > 0)
2786 edit->over_col--;
2787 else
2788 edit_cursor_move (edit, -HALF_TAB_SIZE);
2789 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2790 break;
2793 edit_left_char_move_cmd (edit);
2794 break;
2795 case CK_Right_Alt_Highlight:
2796 column_highlighting = 1;
2797 case CK_Right:
2798 case CK_Right_Highlight:
2799 if (option_fake_half_tabs) {
2800 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2801 edit_cursor_move (edit, HALF_TAB_SIZE);
2802 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2803 break;
2806 edit_right_char_move_cmd (edit);
2807 break;
2808 case CK_Begin_Page:
2809 case CK_Begin_Page_Highlight:
2810 edit_begin_page (edit);
2811 break;
2812 case CK_End_Page:
2813 case CK_End_Page_Highlight:
2814 edit_end_page (edit);
2815 break;
2816 case CK_Word_Left:
2817 case CK_Word_Left_Highlight:
2818 edit->over_col = 0;
2819 edit_left_word_move_cmd (edit);
2820 break;
2821 case CK_Word_Right:
2822 case CK_Word_Right_Highlight:
2823 edit->over_col = 0;
2824 edit_right_word_move_cmd (edit);
2825 break;
2826 case CK_Up_Alt_Highlight:
2827 column_highlighting = 1;
2828 case CK_Up:
2829 case CK_Up_Highlight:
2830 edit_move_up (edit, 1, 0);
2831 break;
2832 case CK_Down_Alt_Highlight:
2833 column_highlighting = 1;
2834 case CK_Down:
2835 case CK_Down_Highlight:
2836 edit_move_down (edit, 1, 0);
2837 break;
2838 case CK_Paragraph_Up_Alt_Highlight:
2839 column_highlighting = 1;
2840 case CK_Paragraph_Up:
2841 case CK_Paragraph_Up_Highlight:
2842 edit_move_up_paragraph (edit, 0);
2843 break;
2844 case CK_Paragraph_Down_Alt_Highlight:
2845 column_highlighting = 1;
2846 case CK_Paragraph_Down:
2847 case CK_Paragraph_Down_Highlight:
2848 edit_move_down_paragraph (edit, 0);
2849 break;
2850 case CK_Scroll_Up_Alt_Highlight:
2851 column_highlighting = 1;
2852 case CK_Scroll_Up:
2853 case CK_Scroll_Up_Highlight:
2854 edit_move_up (edit, 1, 1);
2855 break;
2856 case CK_Scroll_Down_Alt_Highlight:
2857 column_highlighting = 1;
2858 case CK_Scroll_Down:
2859 case CK_Scroll_Down_Highlight:
2860 edit_move_down (edit, 1, 1);
2861 break;
2862 case CK_Home:
2863 case CK_Home_Highlight:
2864 edit_cursor_to_bol (edit);
2865 break;
2866 case CK_End:
2867 case CK_End_Highlight:
2868 edit_cursor_to_eol (edit);
2869 break;
2870 case CK_Tab:
2871 /* if text marked shift block */
2872 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2873 if (edit->mark2 < 0)
2874 edit_mark_cmd (edit, 0);
2875 edit_move_block_to_right (edit);
2876 } else {
2877 if ( option_cursor_beyond_eol )
2878 edit_insert_over (edit);
2879 edit_tab_cmd (edit);
2880 if (option_auto_para_formatting) {
2881 format_paragraph (edit, 0);
2882 edit->force |= REDRAW_PAGE;
2883 } else {
2884 check_and_wrap_line (edit);
2887 break;
2889 case CK_Toggle_Insert:
2890 edit->overwrite = (edit->overwrite == 0);
2891 break;
2893 case CK_Mark:
2894 if (edit->mark2 >= 0) {
2895 if (column_highlighting)
2896 edit_push_action (edit, COLUMN_ON);
2897 column_highlighting = 0;
2899 edit_mark_cmd (edit, 0);
2900 break;
2901 case CK_Column_Mark:
2902 if (!column_highlighting)
2903 edit_push_action (edit, COLUMN_OFF);
2904 column_highlighting = 1;
2905 edit_mark_cmd (edit, 0);
2906 break;
2907 case CK_Unmark:
2908 if (column_highlighting)
2909 edit_push_action (edit, COLUMN_ON);
2910 column_highlighting = 0;
2911 edit_mark_cmd (edit, 1);
2912 break;
2914 case CK_Toggle_Line_State:
2915 option_line_state = !option_line_state;
2916 if ( option_line_state ) {
2917 option_line_state_width = LINE_STATE_WIDTH;
2918 } else {
2919 option_line_state_width = 0;
2921 edit->force |= REDRAW_PAGE;
2922 break;
2924 case CK_Toggle_Bookmark:
2925 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2926 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2927 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2928 else
2929 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2930 break;
2931 case CK_Flush_Bookmarks:
2932 book_mark_flush (edit, BOOK_MARK_COLOR);
2933 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2934 edit->force |= REDRAW_PAGE;
2935 break;
2936 case CK_Next_Bookmark:
2937 if (edit->book_mark) {
2938 struct _book_mark *p;
2939 p = (struct _book_mark *) book_mark_find (edit,
2940 edit->curs_line);
2941 if (p->next) {
2942 p = p->next;
2943 if (p->line >= edit->start_line + edit->num_widget_lines
2944 || p->line < edit->start_line)
2945 edit_move_display (edit,
2946 p->line -
2947 edit->num_widget_lines / 2);
2948 edit_move_to_line (edit, p->line);
2951 break;
2952 case CK_Prev_Bookmark:
2953 if (edit->book_mark) {
2954 struct _book_mark *p;
2955 p = (struct _book_mark *) book_mark_find (edit,
2956 edit->curs_line);
2957 while (p->line == edit->curs_line)
2958 if (p->prev)
2959 p = p->prev;
2960 if (p->line >= 0) {
2961 if (p->line >= edit->start_line + edit->num_widget_lines
2962 || p->line < edit->start_line)
2963 edit_move_display (edit,
2964 p->line -
2965 edit->num_widget_lines / 2);
2966 edit_move_to_line (edit, p->line);
2969 break;
2971 case CK_Beginning_Of_Text:
2972 case CK_Beginning_Of_Text_Highlight:
2973 edit_move_to_top (edit);
2974 break;
2975 case CK_End_Of_Text:
2976 case CK_End_Of_Text_Highlight:
2977 edit_move_to_bottom (edit);
2978 break;
2980 case CK_Copy:
2981 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2982 edit_insert_over (edit);
2983 edit_block_copy_cmd (edit);
2984 break;
2985 case CK_Remove:
2986 edit_block_delete_cmd (edit);
2987 break;
2988 case CK_Move:
2989 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2990 edit_insert_over (edit);
2991 edit_block_move_cmd (edit);
2992 break;
2994 case CK_XStore:
2995 edit_copy_to_X_buf_cmd (edit);
2996 break;
2997 case CK_XCut:
2998 edit_cut_to_X_buf_cmd (edit);
2999 break;
3000 case CK_XPaste:
3001 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3002 edit_insert_over (edit);
3003 edit_paste_from_X_buf_cmd (edit);
3004 break;
3005 case CK_Selection_History:
3006 edit_paste_from_history (edit);
3007 break;
3009 case CK_Save_As:
3010 edit_save_as_cmd (edit);
3011 break;
3012 case CK_Save:
3013 edit_save_confirm_cmd (edit);
3014 break;
3015 case CK_Load:
3016 edit_load_cmd (edit, EDIT_FILE_COMMON);
3017 break;
3018 case CK_Save_Block:
3019 edit_save_block_cmd (edit);
3020 break;
3021 case CK_Insert_File:
3022 edit_insert_file_cmd (edit);
3023 break;
3025 case CK_Load_Prev_File:
3026 edit_load_back_cmd (edit);
3027 break;
3028 case CK_Load_Next_File:
3029 edit_load_forward_cmd (edit);
3030 break;
3032 case CK_Load_Syntax_File:
3033 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3034 break;
3035 case CK_Choose_Syntax:
3036 edit_syntax_dialog ();
3037 break;
3039 case CK_Load_Menu_File:
3040 edit_load_cmd (edit, EDIT_FILE_MENU);
3041 break;
3043 case CK_Toggle_Syntax:
3044 if ((option_syntax_highlighting ^= 1) == 1)
3045 edit_load_syntax (edit, NULL, option_syntax_type);
3046 edit->force |= REDRAW_PAGE;
3047 break;
3049 case CK_Toggle_Tab_TWS:
3050 enable_show_tabs_tws ^= 1;
3051 edit->force |= REDRAW_PAGE;
3052 break;
3054 case CK_Find:
3055 edit_search_cmd (edit, 0);
3056 break;
3057 case CK_Find_Again:
3058 edit_search_cmd (edit, 1);
3059 break;
3060 case CK_Replace:
3061 edit_replace_cmd (edit, 0);
3062 break;
3063 case CK_Replace_Again:
3064 edit_replace_cmd (edit, 1);
3065 break;
3066 case CK_Complete_Word:
3067 /* if text marked shift block */
3068 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3069 edit_move_block_to_left (edit);
3070 } else {
3071 edit_complete_word_cmd (edit);
3073 break;
3074 case CK_Find_Definition:
3075 edit_get_match_keyword_cmd (edit);
3076 break;
3077 case CK_Quit:
3078 dlg_stop (edit->widget.parent);
3079 break;
3080 case CK_New:
3081 edit_new_cmd (edit);
3082 break;
3083 case CK_Help:
3084 edit_help_cmd (edit);
3085 break;
3086 case CK_Refresh:
3087 edit_refresh_cmd (edit);
3088 break;
3089 case CK_SaveSetupCmd:
3090 save_setup_cmd ();
3091 break;
3092 case CK_About:
3093 query_dialog (_(" About "),
3094 _("\n Cooledit v3.11.5\n\n"
3095 " Copyright (C) 1996 the Free Software Foundation\n\n"
3096 " A user friendly text editor written\n"
3097 " for the Midnight Commander.\n"), D_NORMAL,
3098 1, _("&OK"));
3099 break;
3100 case CK_LearnKeys:
3101 learn_keys ();
3102 break;
3103 case CK_Edit_Options:
3104 edit_options_dialog ();
3105 break;
3106 case CK_Edit_Save_Mode:
3107 menu_save_mode_cmd ();
3108 case CK_Date:
3109 break;
3111 char s[1024];
3112 /* fool gcc to prevent a Y2K warning */
3113 char time_format[] = "_c";
3114 time_format[0] = '%';
3116 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3117 edit_print_string (edit, s);
3118 edit->force |= REDRAW_PAGE;
3119 break;
3121 case CK_Goto:
3122 edit_goto_cmd (edit);
3123 break;
3124 case CK_Paragraph_Format:
3125 format_paragraph (edit, 1);
3126 edit->force |= REDRAW_PAGE;
3127 break;
3128 case CK_Delete_Macro:
3129 edit_delete_macro_cmd (edit);
3130 break;
3131 case CK_Match_Bracket:
3132 edit_goto_matching_bracket (edit);
3133 break;
3134 case CK_User_Menu:
3135 user_menu (edit);
3136 break;
3137 case CK_Sort:
3138 edit_sort_cmd (edit);
3139 break;
3140 case CK_ExtCmd:
3141 edit_ext_cmd (edit);
3142 break;
3143 case CK_Mail:
3144 edit_mail_dialog (edit);
3145 break;
3146 case CK_Shell:
3147 view_other_cmd ();
3148 break;
3149 case CK_SelectCodepage:
3150 edit_select_codepage_cmd (edit);
3151 break;
3152 case CK_Insert_Literal:
3153 edit_insert_literal_cmd (edit);
3154 break;
3155 case CK_Execute_Macro:
3156 edit_execute_macro_cmd (edit);
3157 break;
3158 case CK_Begin_End_Macro:
3159 edit_begin_end_macro_cmd (edit);
3160 break;
3161 case CK_Ext_Mode:
3162 edit->extmod = 1;
3163 break;
3164 default:
3165 break;
3168 /* CK_Pipe_Block */
3169 if ((command / 1000) == 1) /* a shell command */
3170 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3171 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3172 struct macro m[MAX_MACRO_LENGTH];
3173 int nm;
3174 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3175 edit_execute_macro (edit, m, nm);
3178 /* keys which must set the col position, and the search vars */
3179 switch (command) {
3180 case CK_Find:
3181 case CK_Find_Again:
3182 case CK_Replace:
3183 case CK_Replace_Again:
3184 case CK_Complete_Word:
3185 edit->prev_col = edit_get_col (edit);
3186 break;
3187 case CK_Up:
3188 case CK_Up_Highlight:
3189 case CK_Up_Alt_Highlight:
3190 case CK_Down:
3191 case CK_Down_Highlight:
3192 case CK_Down_Alt_Highlight:
3193 case CK_Page_Up:
3194 case CK_Page_Up_Highlight:
3195 case CK_Page_Up_Alt_Highlight:
3196 case CK_Page_Down:
3197 case CK_Page_Down_Highlight:
3198 case CK_Page_Down_Alt_Highlight:
3199 case CK_Beginning_Of_Text:
3200 case CK_Beginning_Of_Text_Highlight:
3201 case CK_End_Of_Text:
3202 case CK_End_Of_Text_Highlight:
3203 case CK_Paragraph_Up:
3204 case CK_Paragraph_Up_Highlight:
3205 case CK_Paragraph_Up_Alt_Highlight:
3206 case CK_Paragraph_Down:
3207 case CK_Paragraph_Down_Highlight:
3208 case CK_Paragraph_Down_Alt_Highlight:
3209 case CK_Scroll_Up:
3210 case CK_Scroll_Up_Highlight:
3211 case CK_Scroll_Up_Alt_Highlight:
3212 case CK_Scroll_Down:
3213 case CK_Scroll_Down_Highlight:
3214 case CK_Scroll_Down_Alt_Highlight:
3215 edit->search_start = edit->curs1;
3216 edit->found_len = 0;
3217 break;
3218 default:
3219 edit->found_len = 0;
3220 edit->prev_col = edit_get_col (edit);
3221 edit->search_start = edit->curs1;
3223 edit_find_bracket (edit);
3225 if (option_auto_para_formatting) {
3226 switch (command) {
3227 case CK_BackSpace:
3228 case CK_Delete:
3229 case CK_Delete_Word_Left:
3230 case CK_Delete_Word_Right:
3231 case CK_Delete_To_Line_End:
3232 case CK_Delete_To_Line_Begin:
3233 format_paragraph (edit, 0);
3234 edit->force |= REDRAW_PAGE;
3240 static void
3241 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3243 int i = 0;
3245 if (edit->macro_depth++ > 256) {
3246 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3247 edit->macro_depth--;
3248 return;
3250 edit->force |= REDRAW_PAGE;
3251 for (; i < n; i++) {
3252 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3254 edit_update_screen (edit);
3255 edit->macro_depth--;
3258 /* User edit menu, like user menu (F2) but only in editor. */
3259 static void
3260 user_menu (WEdit * edit)
3262 FILE *fd;
3263 int nomark;
3264 struct stat status;
3265 long start_mark, end_mark;
3266 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3267 int rc = 0;
3269 nomark = eval_marks (edit, &start_mark, &end_mark);
3270 if (!nomark) /* remember marked or not */
3271 edit_save_block (edit, block_file, start_mark, end_mark);
3273 /* run shell scripts from menu */
3274 user_menu_cmd (edit);
3276 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3277 /* no block messages */
3278 goto cleanup;
3281 if (!nomark) {
3282 /* i.e. we have marked block */
3283 rc = edit_block_delete_cmd (edit);
3286 if (!rc) {
3287 edit_insert_file (edit, block_file);
3290 /* truncate block file */
3291 if ((fd = fopen (block_file, "w"))) {
3292 fclose (fd);
3295 edit_refresh_cmd (edit);
3296 edit->force |= REDRAW_COMPLETELY;
3298 cleanup:
3299 g_free (block_file);
3302 void
3303 edit_stack_init (void)
3305 for (edit_stack_iterator = 0;
3306 edit_stack_iterator < MAX_HISTORY_MOVETO;
3307 edit_stack_iterator++ ) {
3308 edit_history_moveto[edit_stack_iterator].filename = NULL;
3309 edit_history_moveto[edit_stack_iterator].line = -1;
3312 edit_stack_iterator = 0;
3315 void
3316 edit_stack_free (void)
3318 for (edit_stack_iterator = 0;
3319 edit_stack_iterator < MAX_HISTORY_MOVETO;
3320 edit_stack_iterator++)
3321 g_free (edit_history_moveto[edit_stack_iterator].filename);