Ticket #1627: glib macros fix.
[midnight-commander.git] / edit / edit.c
blob12fbf48ed478d3330cdbdd98824d18b1956c8a25
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;
85 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
86 char *option_backup_ext = NULL;
88 int edit_stack_iterator = 0;
89 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
90 /* magic sequense for say than block is vertical */
91 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
92 /*-
94 * here's a quick sketch of the layout: (don't run this through indent.)
96 * (b1 is buffers1 and b2 is buffers2)
98 * |
99 * \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
100 * ______________________________________|______________________________________
102 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
103 * |-> |-> |-> |-> |-> |-> |
105 * _<------------------------->|<----------------->_
106 * WEdit->curs2 | WEdit->curs1
107 * ^ | ^
108 * | ^|^ |
109 * cursor ||| cursor
110 * |||
111 * file end|||file beginning
116 * This_is_some_file
117 * fin.
120 const global_keymap_t *editor_map;
121 const global_keymap_t *editor_x_map;
123 static void user_menu (WEdit *edit);
125 int edit_get_byte (WEdit * edit, long byte_index)
127 unsigned long p;
128 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
129 return '\n';
131 if (byte_index >= edit->curs1) {
132 p = edit->curs1 + edit->curs2 - byte_index - 1;
133 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
134 } else {
135 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
139 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
141 unsigned long p;
143 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
144 return NULL;
146 if (byte_index >= edit->curs1) {
147 p = edit->curs1 + edit->curs2 - byte_index - 1;
148 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
149 } else {
150 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
154 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
156 unsigned long p;
158 if (byte_index >= (edit->curs1 + edit->curs2) ) {
159 byte_index -= 1;
162 if ( byte_index < 0 ) {
163 return NULL;
166 if (byte_index >= edit->curs1) {
167 p = edit->curs1 + edit->curs2 - 1;
168 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
169 } else {
170 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
174 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
176 gchar *str = NULL;
177 int res = -1;
178 gunichar ch;
179 gchar *next_ch = NULL;
180 int width = 0;
182 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
183 *char_width = 0;
184 return '\n';
188 str = edit_get_byte_ptr (edit, byte_index);
189 res = g_utf8_get_char_validated (str, -1);
191 if ( res < 0 ) {
192 ch = *str;
193 width = 0;
194 } else {
195 ch = res;
196 /* Calculate UTF-8 char width */
197 next_ch = g_utf8_next_char(str);
198 if ( next_ch ) {
199 width = next_ch - str;
200 } else {
201 ch = 0;
202 width = 0;
205 *char_width = width;
206 return ch;
209 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
211 gchar *str, *buf = NULL;
212 int res = -1;
213 gunichar ch;
214 gchar *next_ch = NULL;
215 int width = 0;
217 if ( byte_index > 0 ) {
218 byte_index--;
221 ch = edit_get_utf (edit, byte_index, &width);
222 if ( width == 1 ) {
223 *char_width = width;
224 return ch;
227 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
228 *char_width = 0;
229 return 0;
232 str = edit_get_byte_ptr (edit, byte_index);
233 buf = edit_get_buf_ptr (edit, byte_index);
234 /* get prev utf8 char */
235 if ( str != buf )
236 str = g_utf8_find_prev_char (buf, str);
238 res = g_utf8_get_char_validated (str, -1);
239 if ( res < 0 ) {
240 ch = *str;
241 width = 0;
242 } else {
243 ch = res;
244 /* Calculate UTF-8 char width */
245 next_ch = g_utf8_next_char(str);
246 if ( next_ch ) {
247 width = next_ch - str;
248 } else {
249 ch = 0;
250 width = 0;
253 *char_width = width;
254 return ch;
258 * Initialize the buffers for an empty files.
260 static void
261 edit_init_buffers (WEdit *edit)
263 int j;
265 for (j = 0; j <= MAXBUFF; j++) {
266 edit->buffers1[j] = NULL;
267 edit->buffers2[j] = NULL;
270 edit->curs1 = 0;
271 edit->curs2 = 0;
272 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
276 * Load file OR text into buffers. Set cursor to the beginning of file.
277 * Return 1 on error.
279 static int
280 edit_load_file_fast (WEdit *edit, const char *filename)
282 long buf, buf2;
283 int file = -1;
284 edit->curs2 = edit->last_byte;
285 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
286 edit->utf8 = 0;
287 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
288 GString *errmsg = g_string_new(NULL);
289 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
290 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
291 g_string_free (errmsg, TRUE);
292 return 1;
295 if (!edit->buffers2[buf2])
296 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
298 mc_read (file,
299 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
300 (edit->curs2 & M_EDIT_BUF_SIZE),
301 edit->curs2 & M_EDIT_BUF_SIZE);
303 for (buf = buf2 - 1; buf >= 0; buf--) {
304 /* edit->buffers2[0] is already allocated */
305 if (!edit->buffers2[buf])
306 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
307 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
310 mc_close (file);
311 return 0;
314 /* detecting an error on save is easy: just check if every byte has been written. */
315 /* detecting an error on read, is not so easy 'cos there is not way to tell
316 whether you read everything or not. */
317 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
318 static const struct edit_filters {
319 const char *read, *write, *extension;
320 } all_filters[] = {
321 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
322 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
323 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
324 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
325 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
328 /* Return index of the filter or -1 is there is no appropriate filter */
329 static int edit_find_filter (const char *filename)
331 size_t i, l, e;
332 if (!filename)
333 return -1;
334 l = strlen (filename);
335 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
336 e = strlen (all_filters[i].extension);
337 if (l > e)
338 if (!strcmp (all_filters[i].extension, filename + l - e))
339 return i;
341 return -1;
344 static char *
345 edit_get_filter (const char *filename)
347 int i, l;
348 char *p, *quoted_name;
349 i = edit_find_filter (filename);
350 if (i < 0)
351 return 0;
352 quoted_name = name_quote (filename, 0);
353 l = str_term_width1 (quoted_name);
354 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
355 sprintf (p, all_filters[i].read, quoted_name);
356 g_free (quoted_name);
357 return p;
360 char *
361 edit_get_write_filter (const char *write_name, const char *filename)
363 int i, l;
364 char *p, *writename;
365 i = edit_find_filter (filename);
366 if (i < 0)
367 return 0;
368 writename = name_quote (write_name, 0);
369 l = str_term_width1 (writename);
370 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
371 sprintf (p, all_filters[i].write, writename);
372 g_free (writename);
373 return p;
376 static long
377 edit_insert_stream (WEdit * edit, FILE * f)
379 int c;
380 long i = 0;
381 while ((c = fgetc (f)) >= 0) {
382 edit_insert (edit, c);
383 i++;
385 return i;
388 long edit_write_stream (WEdit * edit, FILE * f)
390 long i;
392 if (edit->lb == LB_ASIS) {
393 for (i = 0; i < edit->last_byte; i++)
394 if (fputc (edit_get_byte (edit, i), f) < 0)
395 break;
396 return i;
399 /* change line breaks */
400 for (i = 0; i < edit->last_byte; i++) {
401 unsigned char c = edit_get_byte (edit, i);
403 if (!(c == '\n' || c == '\r')) {
404 /* not line break */
405 if (fputc (c, f) < 0)
406 return i;
407 } else { /* (c == '\n' || c == '\r') */
408 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
410 switch (edit->lb) {
411 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
412 /* put one line break unconditionally */
413 if (fputc ('\n', f) < 0)
414 return i;
416 i++; /* 2 chars are processed */
418 if (c == '\r' && c1 == '\n')
419 /* Windows line break; go to the next char */
420 break;
422 if (c == '\r' && c1 == '\r') {
423 /* two Macintosh line breaks; put second line break */
424 if (fputc ('\n', f) < 0)
425 return i;
426 break;
429 if (fputc (c1, f) < 0)
430 return i;
431 break;
433 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
434 /* put one line break unconditionally */
435 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
436 return i;
438 if (c == '\r' && c1 == '\n')
439 /* Windows line break; go to the next char */
440 i++;
441 break;
443 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
444 /* put one line break unconditionally */
445 if (fputc ('\r', f) < 0)
446 return i;
448 i++; /* 2 chars are processed */
450 if (c == '\r' && c1 == '\n')
451 /* Windows line break; go to the next char */
452 break;
454 if (c == '\n' && c1 == '\n') {
455 /* two Windows line breaks; put second line break */
456 if (fputc ('\r', f) < 0)
457 return i;
458 break;
461 if (fputc (c1, f) < 0)
462 return i;
463 break;
464 case LB_ASIS: /* default without changes */
465 break;
470 return edit->last_byte;
473 #define TEMP_BUF_LEN 1024
475 /* inserts a file at the cursor, returns 1 on success */
477 edit_insert_file (WEdit *edit, const char *filename)
479 char *p;
480 if ((p = edit_get_filter (filename))) {
481 FILE *f;
482 long current = edit->curs1;
483 f = (FILE *) popen (p, "r");
484 if (f) {
485 edit_insert_stream (edit, f);
486 edit_cursor_move (edit, current - edit->curs1);
487 if (pclose (f) > 0) {
488 GString *errmsg = g_string_new (NULL);
489 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
490 edit_error_dialog (_("Error"), errmsg->str);
491 g_string_free (errmsg, TRUE);
492 g_free (p);
493 return 0;
495 } else {
496 GString *errmsg = g_string_new (NULL);
497 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
498 edit_error_dialog (_("Error"), errmsg->str);
499 g_string_free (errmsg, TRUE);
500 g_free (p);
501 return 0;
503 g_free (p);
504 } else {
505 int i, file, blocklen;
506 long current = edit->curs1;
507 int vertical_insertion = 0;
508 char *buf;
509 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
510 return 0;
511 buf = g_malloc (TEMP_BUF_LEN);
512 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
513 if (blocklen > 0) {
514 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
515 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
516 vertical_insertion = 1;
517 } else {
518 mc_lseek (file, 0, SEEK_SET);
521 if (vertical_insertion) {
522 blocklen = edit_insert_column_of_text_from_file (edit, file);
523 } else {
524 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
525 for (i = 0; i < blocklen; i++)
526 edit_insert (edit, buf[i]);
529 edit_cursor_move (edit, current - edit->curs1);
530 g_free (buf);
531 mc_close (file);
532 if (blocklen)
533 return 0;
535 return 1;
538 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
539 static int
540 check_file_access (WEdit *edit, const char *filename, struct stat *st)
542 int file;
543 GString *errmsg = (GString *) 0;
545 /* Try opening an existing file */
546 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
548 if (file < 0) {
550 * Try creating the file. O_EXCL prevents following broken links
551 * and opening existing files.
553 file =
554 mc_open (filename,
555 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
556 0666);
557 if (file < 0) {
558 g_string_sprintf (errmsg = g_string_new (NULL),
559 _(" Cannot open %s for reading "), filename);
560 goto cleanup;
561 } else {
562 /* New file, delete it if it's not modified or saved */
563 edit->delete_file = 1;
567 /* Check what we have opened */
568 if (mc_fstat (file, st) < 0) {
569 g_string_sprintf (errmsg = g_string_new (NULL),
570 _(" Cannot get size/permissions for %s "), filename);
571 goto cleanup;
574 /* We want to open regular files only */
575 if (!S_ISREG (st->st_mode)) {
576 g_string_sprintf (errmsg = g_string_new (NULL),
577 _(" %s is not a regular file "), filename);
578 goto cleanup;
582 * Don't delete non-empty files.
583 * O_EXCL should prevent it, but let's be on the safe side.
585 if (st->st_size > 0) {
586 edit->delete_file = 0;
589 if (st->st_size >= SIZE_LIMIT) {
590 g_string_sprintf (errmsg = g_string_new (NULL),
591 _(" File %s is too large "), filename);
592 goto cleanup;
595 cleanup:
596 (void) mc_close (file);
597 if (errmsg) {
598 edit_error_dialog (_("Error"), errmsg->str);
599 g_string_free (errmsg, TRUE);
600 return 1;
602 return 0;
606 * Open the file and load it into the buffers, either directly or using
607 * a filter. Return 0 on success, 1 on error.
609 * Fast loading (edit_load_file_fast) is used when the file size is
610 * known. In this case the data is read into the buffers by blocks.
611 * If the file size is not known, the data is loaded byte by byte in
612 * edit_insert_file.
614 static int
615 edit_load_file (WEdit *edit)
617 int fast_load = 1;
619 /* Cannot do fast load if a filter is used */
620 if (edit_find_filter (edit->filename) >= 0)
621 fast_load = 0;
624 * VFS may report file size incorrectly, and slow load is not a big
625 * deal considering overhead in VFS.
627 if (!vfs_file_is_local (edit->filename))
628 fast_load = 0;
631 * FIXME: line end translation should disable fast loading as well
632 * Consider doing fseek() to the end and ftell() for the real size.
635 if (*edit->filename) {
636 /* If we are dealing with a real file, check that it exists */
637 if (check_file_access (edit, edit->filename, &edit->stat1))
638 return 1;
639 } else {
640 /* nothing to load */
641 fast_load = 0;
644 edit_init_buffers (edit);
646 if (fast_load) {
647 edit->last_byte = edit->stat1.st_size;
648 edit_load_file_fast (edit, edit->filename);
649 /* If fast load was used, the number of lines wasn't calculated */
650 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
651 } else {
652 #ifdef HAVE_CHARSET
653 const char *codepage_id;
654 #endif
655 edit->last_byte = 0;
656 if (*edit->filename) {
657 edit->stack_disable = 1;
658 if (!edit_insert_file (edit, edit->filename)) {
659 edit_clean (edit);
660 return 1;
662 edit->stack_disable = 0;
665 #ifdef HAVE_CHARSET
666 codepage_id = get_codepage_id( source_codepage );
667 if ( codepage_id )
668 edit->utf8 = str_isutf8 ( codepage_id );
669 #endif
671 edit->lb = LB_ASIS;
672 return 0;
675 /* Restore saved cursor position in the file */
676 static void
677 edit_load_position (WEdit *edit)
679 char *filename;
680 long line, column;
682 if (!edit->filename || !*edit->filename)
683 return;
685 filename = vfs_canon (edit->filename);
686 load_file_position (filename, &line, &column);
687 g_free (filename);
689 edit_move_to_line (edit, line - 1);
690 edit->prev_col = column;
691 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
692 edit_move_display (edit, line - (edit->num_widget_lines / 2));
695 /* Save cursor position in the file */
696 static void
697 edit_save_position (WEdit *edit)
699 char *filename;
701 if (!edit->filename || !*edit->filename)
702 return;
704 filename = vfs_canon (edit->filename);
705 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
706 g_free (filename);
709 /* Clean the WEdit stricture except the widget part */
710 static void
711 edit_purge_widget (WEdit *edit)
713 int len = sizeof (WEdit) - sizeof (Widget);
714 char *start = (char *) edit + sizeof (Widget);
715 memset (start, 0, len);
716 edit->macro_i = -1; /* not recording a macro */
719 static void
720 edit_set_keymap (WEdit *edit)
722 editor_map = default_editor_keymap;
723 if (editor_keymap && editor_keymap->len > 0)
724 editor_map = (global_keymap_t *) editor_keymap->data;
726 editor_x_map = default_editor_x_keymap;
727 if (editor_x_keymap && editor_x_keymap->len > 0)
728 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
732 #define space_width 1
735 * Fill in the edit structure. Return NULL on failure. Pass edit as
736 * NULL to allocate a new structure.
738 * If line is 0, try to restore saved position. Otherwise put the
739 * cursor on that line and show it in the middle of the screen.
741 WEdit *
742 edit_init (WEdit *edit, int lines, int columns, const char *filename,
743 long line)
745 int to_free = 0;
746 option_auto_syntax = 1; /* Resetting to auto on every invokation */
747 if ( option_line_state ) {
748 option_line_state_width = LINE_STATE_WIDTH;
749 } else {
750 option_line_state_width = 0;
752 if (!edit) {
753 #ifdef ENABLE_NLS
755 * Expand option_whole_chars_search by national letters using
756 * current locale
759 static char option_whole_chars_search_buf[256];
761 if (option_whole_chars_search_buf != option_whole_chars_search) {
762 size_t i;
763 size_t len = str_term_width1 (option_whole_chars_search);
765 strcpy (option_whole_chars_search_buf,
766 option_whole_chars_search);
768 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
769 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
770 option_whole_chars_search_buf[len++] = i;
774 option_whole_chars_search_buf[len] = 0;
775 option_whole_chars_search = option_whole_chars_search_buf;
777 #endif /* ENABLE_NLS */
778 edit = g_malloc0 (sizeof (WEdit));
779 edit->search = NULL;
780 to_free = 1;
782 edit_purge_widget (edit);
783 edit->num_widget_lines = lines;
784 edit->over_col = 0;
785 edit->num_widget_columns = columns;
786 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
787 edit->stat1.st_uid = getuid ();
788 edit->stat1.st_gid = getgid ();
789 edit->stat1.st_mtime = 0;
790 edit->bracket = -1;
791 edit->force |= REDRAW_PAGE;
792 edit_set_filename (edit, filename);
793 edit->stack_size = START_STACK_SIZE;
794 edit->stack_size_mask = START_STACK_SIZE - 1;
795 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
796 if (edit_load_file (edit)) {
797 /* edit_load_file already gives an error message */
798 if (to_free)
799 g_free (edit);
800 return 0;
802 edit->utf8 = 0;
803 edit->converter = str_cnv_from_term;
804 #ifdef HAVE_CHARSET
805 const char *cp_id = NULL;
806 cp_id = get_codepage_id (source_codepage >= 0 ?
807 source_codepage : display_codepage);
809 if (cp_id != NULL) {
810 GIConv conv;
811 conv = str_crt_conv_from (cp_id);
812 if (conv != INVALID_CONV) {
813 if (edit->converter != str_cnv_from_term)
814 str_close_conv (edit->converter);
815 edit->converter = conv;
818 if (cp_id != NULL)
819 edit->utf8 = str_isutf8 (cp_id);
820 #endif
822 edit->loading_done = 1;
823 edit->modified = 0;
824 edit->locked = 0;
825 edit_load_syntax (edit, 0, 0);
827 int color;
828 edit_get_syntax_color (edit, -1, &color);
831 /* load saved cursor position */
832 if ((line == 0) && option_save_position) {
833 edit_load_position (edit);
834 } else {
835 if (line <= 0)
836 line = 1;
837 edit_move_display (edit, line - 1);
838 edit_move_to_line (edit, line - 1);
841 edit_set_keymap (edit);
843 return edit;
846 /* Clear the edit struct, freeing everything in it. Return 1 on success */
848 edit_clean (WEdit *edit)
850 int j = 0;
852 if (!edit)
853 return 0;
855 /* a stale lock, remove it */
856 if (edit->locked)
857 edit->locked = edit_unlock_file (edit->filename);
859 /* save cursor position */
860 if (option_save_position)
861 edit_save_position (edit);
863 /* File specified on the mcedit command line and never saved */
864 if (edit->delete_file)
865 unlink (edit->filename);
867 edit_free_syntax_rules (edit);
868 book_mark_flush (edit, -1);
869 for (; j <= MAXBUFF; j++) {
870 g_free (edit->buffers1[j]);
871 g_free (edit->buffers2[j]);
874 g_free (edit->undo_stack);
875 g_free (edit->filename);
876 g_free (edit->dir);
878 if (edit->search)
880 mc_search_free(edit->search);
881 edit->search = NULL;
883 edit_purge_widget (edit);
885 return 1;
889 /* returns 1 on success */
890 int edit_renew (WEdit * edit)
892 int lines = edit->num_widget_lines;
893 int columns = edit->num_widget_columns;
894 int retval = 1;
896 edit_clean (edit);
897 if (!edit_init (edit, lines, columns, "", 0))
898 retval = 0;
899 return retval;
903 * Load a new file into the editor. If it fails, preserve the old file.
904 * To do it, allocate a new widget, initialize it and, if the new file
905 * was loaded, copy the data to the old widget.
906 * Return 1 on success, 0 on failure.
909 edit_reload (WEdit *edit, const char *filename)
911 WEdit *e;
912 int lines = edit->num_widget_lines;
913 int columns = edit->num_widget_columns;
915 e = g_malloc0 (sizeof (WEdit));
916 e->widget = edit->widget;
917 if (!edit_init (e, lines, columns, filename, 0)) {
918 g_free (e);
919 return 0;
921 edit_clean (edit);
922 memcpy (edit, e, sizeof (WEdit));
923 g_free (e);
924 return 1;
928 * Load a new file into the editor and set line. If it fails, preserve the old file.
929 * To do it, allocate a new widget, initialize it and, if the new file
930 * was loaded, copy the data to the old widget.
931 * Return 1 on success, 0 on failure.
934 edit_reload_line (WEdit *edit, const char *filename, long line)
936 WEdit *e;
937 int lines = edit->num_widget_lines;
938 int columns = edit->num_widget_columns;
940 e = g_malloc0 (sizeof (WEdit));
941 e->widget = edit->widget;
942 if (!edit_init (e, lines, columns, filename, line)) {
943 g_free (e);
944 return 0;
946 edit_clean (edit);
947 memcpy (edit, e, sizeof (WEdit));
948 g_free (e);
949 return 1;
954 Recording stack for undo:
955 The following is an implementation of a compressed stack. Identical
956 pushes are recorded by a negative prefix indicating the number of times the
957 same char was pushed. This saves space for repeated curs-left or curs-right
958 delete etc.
962 pushed: stored:
966 b -3
968 c --> -4
974 If the stack long int is 0-255 it represents a normal insert (from a backspace),
975 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
976 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
977 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
978 position.
980 The only way the cursor moves or the buffer is changed is through the routines:
981 insert, backspace, insert_ahead, delete, and cursor_move.
982 These record the reverse undo movements onto the stack each time they are
983 called.
985 Each key press results in a set of actions (insert; delete ...). So each time
986 a key is pressed the current position of start_display is pushed as
987 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
988 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
989 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
993 void edit_push_action (WEdit * edit, long c,...)
995 unsigned long sp = edit->stack_pointer;
996 unsigned long spm1;
997 long *t;
999 /* first enlarge the stack if necessary */
1000 if (sp > edit->stack_size - 10) { /* say */
1001 if (option_max_undo < 256)
1002 option_max_undo = 256;
1003 if (edit->stack_size < (unsigned long) option_max_undo) {
1004 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1005 if (t) {
1006 edit->undo_stack = t;
1007 edit->stack_size <<= 1;
1008 edit->stack_size_mask = edit->stack_size - 1;
1012 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1013 if (edit->stack_disable)
1014 return;
1016 #ifdef FAST_MOVE_CURSOR
1017 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1018 va_list ap;
1019 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1020 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1021 va_start (ap, c);
1022 c = -(va_arg (ap, int));
1023 va_end (ap);
1024 } else
1025 #endif /* ! FAST_MOVE_CURSOR */
1026 if (edit->stack_bottom != sp
1027 && spm1 != edit->stack_bottom
1028 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1029 int d;
1030 if (edit->undo_stack[spm1] < 0) {
1031 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1032 if (d == c) {
1033 if (edit->undo_stack[spm1] > -1000000000) {
1034 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1035 edit->undo_stack[spm1]--;
1036 return;
1039 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1040 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1041 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1042 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1043 if (edit->undo_stack[spm1] == -2)
1044 edit->stack_pointer = spm1;
1045 else
1046 edit->undo_stack[spm1]++;
1047 return;
1049 #endif
1050 } else {
1051 d = edit->undo_stack[spm1];
1052 if (d == c) {
1053 if (c >= KEY_PRESS)
1054 return; /* --> no need to push multiple do-nothings */
1055 edit->undo_stack[sp] = -2;
1056 goto check_bottom;
1058 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1059 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1060 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1061 edit->stack_pointer = spm1;
1062 return;
1064 #endif
1067 edit->undo_stack[sp] = c;
1068 check_bottom:
1070 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1072 /* if the sp wraps round and catches the stack_bottom then erase
1073 * the first set of actions on the stack to make space - by moving
1074 * stack_bottom forward one "key press" */
1075 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1076 if ((unsigned long) c == edit->stack_bottom ||
1077 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1078 do {
1079 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1080 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1082 /*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: */
1083 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1084 edit->stack_bottom = edit->stack_pointer = 0;
1088 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1089 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1091 static long
1092 pop_action (WEdit * edit)
1094 long c;
1095 unsigned long sp = edit->stack_pointer;
1096 if (sp == edit->stack_bottom) {
1097 return STACK_BOTTOM;
1099 sp = (sp - 1) & edit->stack_size_mask;
1100 if ((c = edit->undo_stack[sp]) >= 0) {
1101 /* edit->undo_stack[sp] = '@'; */
1102 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1103 return c;
1105 if (sp == edit->stack_bottom) {
1106 return STACK_BOTTOM;
1108 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1109 if (edit->undo_stack[sp] == -2) {
1110 /* edit->undo_stack[sp] = '@'; */
1111 edit->stack_pointer = sp;
1112 } else
1113 edit->undo_stack[sp]++;
1115 return c;
1118 /* is called whenever a modification is made by one of the four routines below */
1119 static void edit_modification (WEdit * edit)
1121 edit->caches_valid = 0;
1122 edit->screen_modified = 1;
1124 /* raise lock when file modified */
1125 if (!edit->modified && !edit->delete_file)
1126 edit->locked = edit_lock_file (edit->filename);
1127 edit->modified = 1;
1131 Basic low level single character buffer alterations and movements at the cursor.
1132 Returns char passed over, inserted or removed.
1135 void
1136 edit_insert (WEdit *edit, int c)
1138 /* check if file has grown to large */
1139 if (edit->last_byte >= SIZE_LIMIT)
1140 return;
1142 /* first we must update the position of the display window */
1143 if (edit->curs1 < edit->start_display) {
1144 edit->start_display++;
1145 if (c == '\n')
1146 edit->start_line++;
1149 /* Mark file as modified, unless the file hasn't been fully loaded */
1150 if (edit->loading_done) {
1151 edit_modification (edit);
1154 /* now we must update some info on the file and check if a redraw is required */
1155 if (c == '\n') {
1156 if (edit->book_mark)
1157 book_mark_inc (edit, edit->curs_line);
1158 edit->curs_line++;
1159 edit->total_lines++;
1160 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1163 /* save the reverse command onto the undo stack */
1164 edit_push_action (edit, BACKSPACE);
1166 /* update markers */
1167 edit->mark1 += (edit->mark1 > edit->curs1);
1168 edit->mark2 += (edit->mark2 > edit->curs1);
1169 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1171 /* add a new buffer if we've reached the end of the last one */
1172 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1173 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1174 g_malloc (EDIT_BUF_SIZE);
1176 /* perform the insertion */
1177 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1178 curs1 & M_EDIT_BUF_SIZE]
1179 = (unsigned char) c;
1181 /* update file length */
1182 edit->last_byte++;
1184 /* update cursor position */
1185 edit->curs1++;
1188 void
1189 edit_insert_over (WEdit * edit)
1191 int i;
1193 for ( i = 0; i < edit->over_col; i++ ) {
1194 edit_insert (edit, ' ');
1196 edit->over_col = 0;
1199 /* same as edit_insert and move left */
1200 void edit_insert_ahead (WEdit * edit, int c)
1202 if (edit->last_byte >= SIZE_LIMIT)
1203 return;
1204 if (edit->curs1 < edit->start_display) {
1205 edit->start_display++;
1206 if (c == '\n')
1207 edit->start_line++;
1209 edit_modification (edit);
1210 if (c == '\n') {
1211 if (edit->book_mark)
1212 book_mark_inc (edit, edit->curs_line);
1213 edit->total_lines++;
1214 edit->force |= REDRAW_AFTER_CURSOR;
1216 edit_push_action (edit, DELCHAR);
1218 edit->mark1 += (edit->mark1 >= edit->curs1);
1219 edit->mark2 += (edit->mark2 >= edit->curs1);
1220 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1222 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1223 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1224 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1226 edit->last_byte++;
1227 edit->curs2++;
1231 int edit_delete (WEdit * edit, const int byte_delete)
1233 int p = 0;
1234 int cw = 1;
1235 int i;
1237 if (!edit->curs2)
1238 return 0;
1240 edit->mark1 -= (edit->mark1 > edit->curs1);
1241 edit->mark2 -= (edit->mark2 > edit->curs1);
1242 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1244 cw = 1;
1245 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1246 if ( edit->utf8 && byte_delete == 0 ) {
1247 edit_get_utf (edit, edit->curs1, &cw);
1248 if ( cw < 1 )
1249 cw = 1;
1251 for ( i = 1; i<= cw; i++ ) {
1252 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1254 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1255 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1256 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1258 edit->last_byte--;
1259 edit->curs2--;
1260 edit_push_action (edit, p + 256);
1263 edit_modification (edit);
1264 if (p == '\n') {
1265 if (edit->book_mark)
1266 book_mark_dec (edit, edit->curs_line);
1267 edit->total_lines--;
1268 edit->force |= REDRAW_AFTER_CURSOR;
1270 if (edit->curs1 < edit->start_display) {
1271 edit->start_display--;
1272 if (p == '\n')
1273 edit->start_line--;
1276 return p;
1280 static int
1281 edit_backspace (WEdit * edit, const int byte_delete)
1283 int p = 0;
1284 int cw = 1;
1285 int i;
1287 if (!edit->curs1)
1288 return 0;
1290 edit->mark1 -= (edit->mark1 >= edit->curs1);
1291 edit->mark2 -= (edit->mark2 >= edit->curs1);
1292 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1294 cw = 1;
1295 if ( edit->utf8 && byte_delete == 0 ) {
1296 edit_get_prev_utf (edit, edit->curs1, &cw);
1297 if ( cw < 1 )
1298 cw = 1;
1300 for ( i = 1; i<= cw; i++ ) {
1301 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1302 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1303 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1304 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1306 edit->last_byte--;
1307 edit->curs1--;
1308 edit_push_action (edit, p);
1310 edit_modification (edit);
1311 if (p == '\n') {
1312 if (edit->book_mark)
1313 book_mark_dec (edit, edit->curs_line);
1314 edit->curs_line--;
1315 edit->total_lines--;
1316 edit->force |= REDRAW_AFTER_CURSOR;
1319 if (edit->curs1 < edit->start_display) {
1320 edit->start_display--;
1321 if (p == '\n')
1322 edit->start_line--;
1325 return p;
1328 #ifdef FAST_MOVE_CURSOR
1330 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1332 unsigned long next;
1333 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1334 edit->curs_line--;
1335 next -= (unsigned long) dest;
1336 n -= next;
1337 src += next;
1338 dest += next;
1343 edit_move_backward_lots (WEdit *edit, long increment)
1345 int r, s, t;
1346 unsigned char *p;
1348 if (increment > edit->curs1)
1349 increment = edit->curs1;
1350 if (increment <= 0)
1351 return -1;
1352 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1354 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1355 if (r > increment)
1356 r = increment;
1357 s = edit->curs1 & M_EDIT_BUF_SIZE;
1359 p = 0;
1360 if (s > r) {
1361 memqcpy (edit,
1362 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1363 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1365 } else {
1366 if (s) {
1367 memqcpy (edit,
1368 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1369 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1370 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1371 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1373 memqcpy (edit,
1374 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1375 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1376 EDIT_BUF_SIZE - (r - s), r - s);
1378 increment -= r;
1379 edit->curs1 -= r;
1380 edit->curs2 += r;
1381 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1382 if (p)
1383 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1384 else
1385 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1386 g_malloc (EDIT_BUF_SIZE);
1387 } else {
1388 g_free (p);
1391 s = edit->curs1 & M_EDIT_BUF_SIZE;
1392 while (increment) {
1393 p = 0;
1394 r = EDIT_BUF_SIZE;
1395 if (r > increment)
1396 r = increment;
1397 t = s;
1398 if (r < t)
1399 t = r;
1400 memqcpy (edit,
1401 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1402 EDIT_BUF_SIZE - t,
1403 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1405 if (r >= s) {
1406 if (t) {
1407 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1408 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1410 memqcpy (edit,
1411 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1412 EDIT_BUF_SIZE - r,
1413 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1414 EDIT_BUF_SIZE - (r - s), r - s);
1416 increment -= r;
1417 edit->curs1 -= r;
1418 edit->curs2 += r;
1419 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1420 if (p)
1421 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1422 else
1423 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1424 g_malloc (EDIT_BUF_SIZE);
1425 } else {
1426 g_free (p);
1429 return edit_get_byte (edit, edit->curs1);
1432 #endif /* ! FAST_MOVE_CURSOR */
1434 /* moves the cursor right or left: increment positive or negative respectively */
1435 void edit_cursor_move (WEdit * edit, long increment)
1437 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1438 int c;
1440 #ifdef FAST_MOVE_CURSOR
1441 if (increment < -256) {
1442 edit->force |= REDRAW_PAGE;
1443 edit_move_backward_lots (edit, -increment);
1444 return;
1446 #endif /* ! FAST_MOVE_CURSOR */
1448 if (increment < 0) {
1449 for (; increment < 0; increment++) {
1450 if (!edit->curs1)
1451 return;
1453 edit_push_action (edit, CURS_RIGHT);
1455 c = edit_get_byte (edit, edit->curs1 - 1);
1456 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1457 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1458 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1459 edit->curs2++;
1460 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1461 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1462 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1463 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1465 edit->curs1--;
1466 if (c == '\n') {
1467 edit->curs_line--;
1468 edit->force |= REDRAW_LINE_BELOW;
1472 } else if (increment > 0) {
1473 for (; increment > 0; increment--) {
1474 if (!edit->curs2)
1475 return;
1477 edit_push_action (edit, CURS_LEFT);
1479 c = edit_get_byte (edit, edit->curs1);
1480 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1481 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1482 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1483 edit->curs1++;
1484 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1485 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1486 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1487 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1489 edit->curs2--;
1490 if (c == '\n') {
1491 edit->curs_line++;
1492 edit->force |= REDRAW_LINE_ABOVE;
1498 /* These functions return positions relative to lines */
1500 /* returns index of last char on line + 1 */
1501 long edit_eol (WEdit * edit, long current)
1503 if (current < edit->last_byte) {
1504 for (;; current++)
1505 if (edit_get_byte (edit, current) == '\n')
1506 break;
1507 } else
1508 return edit->last_byte;
1509 return current;
1512 /* returns index of first char on line */
1513 long edit_bol (WEdit * edit, long current)
1515 if (current > 0) {
1516 for (;; current--)
1517 if (edit_get_byte (edit, current - 1) == '\n')
1518 break;
1519 } else
1520 return 0;
1521 return current;
1525 int edit_count_lines (WEdit * edit, long current, int upto)
1527 int lines = 0;
1528 if (upto > edit->last_byte)
1529 upto = edit->last_byte;
1530 if (current < 0)
1531 current = 0;
1532 while (current < upto)
1533 if (edit_get_byte (edit, current++) == '\n')
1534 lines++;
1535 return lines;
1539 /* If lines is zero this returns the count of lines from current to upto. */
1540 /* If upto is zero returns index of lines forward current. */
1541 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1543 if (upto) {
1544 return edit_count_lines (edit, current, upto);
1545 } else {
1546 int next;
1547 if (lines < 0)
1548 lines = 0;
1549 while (lines--) {
1550 next = edit_eol (edit, current) + 1;
1551 if (next > edit->last_byte)
1552 break;
1553 else
1554 current = next;
1556 return current;
1561 /* Returns offset of 'lines' lines up from current */
1562 long edit_move_backward (WEdit * edit, long current, int lines)
1564 if (lines < 0)
1565 lines = 0;
1566 current = edit_bol (edit, current);
1567 while((lines--) && current != 0)
1568 current = edit_bol (edit, current - 1);
1569 return current;
1572 /* If cols is zero this returns the count of columns from current to upto. */
1573 /* If upto is zero returns index of cols across from current. */
1574 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1576 long p, q;
1577 int col = 0;
1578 #ifdef HAVE_CHARSET
1579 int cw = 1;
1580 int utf_ch = 0;
1581 #endif
1582 if (upto) {
1583 q = upto;
1584 cols = -10;
1585 } else
1586 q = edit->last_byte + 2;
1587 for (col = 0, p = current; p < q; p++) {
1588 int c;
1589 #ifdef HAVE_CHARSET
1590 cw = 1;
1591 utf_ch = 0;
1592 #endif
1593 if (cols != -10) {
1594 if (col == cols)
1595 return p;
1596 if (col > cols)
1597 return p - 1;
1599 #ifdef HAVE_CHARSET
1600 if ( !edit->utf8 ) {
1601 #endif
1602 c = edit_get_byte (edit, p);
1603 #ifdef HAVE_CHARSET
1604 } else {
1605 cw = 1;
1606 c = edit_get_byte (edit, p);
1607 utf_ch = edit_get_utf (edit, p, &cw);
1609 if ( utf8_display ) {
1610 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1611 col++;
1613 #endif
1614 if (c == '\t')
1615 col += TAB_SIZE - col % TAB_SIZE;
1616 else if (c == '\n') {
1617 if (upto)
1618 return col;
1619 else
1620 return p;
1621 } else if (c < 32 || c == 127)
1622 col += 2; /* Caret notation for control characters */
1623 else
1624 col++;
1625 #ifdef HAVE_CHARSET
1626 if ( cw > 1 )
1627 col -= cw-1;
1628 #endif
1630 return col;
1633 /* returns the current column position of the cursor */
1634 int edit_get_col (WEdit * edit)
1636 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1640 /* Scrolling functions */
1642 void edit_update_curs_row (WEdit * edit)
1644 edit->curs_row = edit->curs_line - edit->start_line;
1647 void edit_update_curs_col (WEdit * edit)
1649 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1653 edit_get_curs_col (const WEdit *edit)
1655 return edit->curs_col;
1658 /*moves the display start position up by i lines */
1659 void edit_scroll_upward (WEdit * edit, unsigned long i)
1661 unsigned long lines_above = edit->start_line;
1662 if (i > lines_above)
1663 i = lines_above;
1664 if (i) {
1665 edit->start_line -= i;
1666 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1667 edit->force |= REDRAW_PAGE;
1668 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1670 edit_update_curs_row (edit);
1674 /* returns 1 if could scroll, 0 otherwise */
1675 void edit_scroll_downward (WEdit * edit, int i)
1677 int lines_below;
1678 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1679 if (lines_below > 0) {
1680 if (i > lines_below)
1681 i = lines_below;
1682 edit->start_line += i;
1683 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1684 edit->force |= REDRAW_PAGE;
1685 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1687 edit_update_curs_row (edit);
1690 void edit_scroll_right (WEdit * edit, int i)
1692 edit->force |= REDRAW_PAGE;
1693 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1694 edit->start_col -= i;
1697 void edit_scroll_left (WEdit * edit, int i)
1699 if (edit->start_col) {
1700 edit->start_col += i;
1701 if (edit->start_col > 0)
1702 edit->start_col = 0;
1703 edit->force |= REDRAW_PAGE;
1704 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1708 /* high level cursor movement commands */
1710 static int is_in_indent (WEdit *edit)
1712 long p = edit_bol (edit, edit->curs1);
1713 while (p < edit->curs1)
1714 if (!strchr (" \t", edit_get_byte (edit, p++)))
1715 return 0;
1716 return 1;
1719 static int left_of_four_spaces (WEdit *edit);
1721 void
1722 edit_move_to_prev_col (WEdit * edit, long p)
1724 int prev = edit->prev_col;
1725 int over = edit->over_col;
1727 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1729 if (option_cursor_beyond_eol) {
1730 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1732 if (line_len < prev + edit->over_col) {
1733 edit->over_col = prev + over - line_len;
1734 edit->prev_col = line_len;
1735 edit->curs_col = line_len;
1736 } else {
1737 edit->curs_col = prev + over;
1738 edit->prev_col = edit->curs_col;
1739 edit->over_col = 0;
1741 } else {
1742 edit->over_col = 0;
1743 if (is_in_indent (edit) && option_fake_half_tabs) {
1744 edit_update_curs_col (edit);
1745 if (space_width)
1746 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1747 int q = edit->curs_col;
1748 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1749 p = edit_bol (edit, edit->curs1);
1750 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1751 if (!left_of_four_spaces (edit))
1752 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1758 /* move i lines */
1759 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1761 unsigned long p, l = edit->curs_line;
1763 if (i > l)
1764 i = l;
1765 if (i) {
1766 if (i > 1)
1767 edit->force |= REDRAW_PAGE;
1768 if (scroll)
1769 edit_scroll_upward (edit, i);
1771 p = edit_bol (edit, edit->curs1);
1772 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1773 edit_move_to_prev_col (edit, p);
1775 edit->search_start = edit->curs1;
1776 edit->found_len = 0;
1780 static int
1781 is_blank (WEdit *edit, long offset)
1783 long s, f;
1784 int c;
1785 s = edit_bol (edit, offset);
1786 f = edit_eol (edit, offset) - 1;
1787 while (s <= f) {
1788 c = edit_get_byte (edit, s++);
1789 if (!isspace (c))
1790 return 0;
1792 return 1;
1796 /* returns the offset of line i */
1797 static long
1798 edit_find_line (WEdit *edit, int line)
1800 int i, j = 0;
1801 int m = 2000000000;
1802 if (!edit->caches_valid) {
1803 for (i = 0; i < N_LINE_CACHES; i++)
1804 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1805 /* three offsets that we *know* are line 0 at 0 and these two: */
1806 edit->line_numbers[1] = edit->curs_line;
1807 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1808 edit->line_numbers[2] = edit->total_lines;
1809 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1810 edit->caches_valid = 1;
1812 if (line >= edit->total_lines)
1813 return edit->line_offsets[2];
1814 if (line <= 0)
1815 return 0;
1816 /* find the closest known point */
1817 for (i = 0; i < N_LINE_CACHES; i++) {
1818 int n;
1819 n = abs (edit->line_numbers[i] - line);
1820 if (n < m) {
1821 m = n;
1822 j = i;
1825 if (m == 0)
1826 return edit->line_offsets[j]; /* know the offset exactly */
1827 if (m == 1 && j >= 3)
1828 i = j; /* one line different - caller might be looping, so stay in this cache */
1829 else
1830 i = 3 + (rand () % (N_LINE_CACHES - 3));
1831 if (line > edit->line_numbers[j])
1832 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1833 else
1834 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1835 edit->line_numbers[i] = line;
1836 return edit->line_offsets[i];
1839 int line_is_blank (WEdit * edit, long line)
1841 return is_blank (edit, edit_find_line (edit, line));
1844 /* moves up until a blank line is reached, or until just
1845 before a non-blank line is reached */
1846 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1848 int i;
1849 if (edit->curs_line <= 1) {
1850 i = 0;
1851 } else {
1852 if (line_is_blank (edit, edit->curs_line)) {
1853 if (line_is_blank (edit, edit->curs_line - 1)) {
1854 for (i = edit->curs_line - 1; i; i--)
1855 if (!line_is_blank (edit, i)) {
1856 i++;
1857 break;
1859 } else {
1860 for (i = edit->curs_line - 1; i; i--)
1861 if (line_is_blank (edit, i))
1862 break;
1864 } else {
1865 for (i = edit->curs_line - 1; i; i--)
1866 if (line_is_blank (edit, i))
1867 break;
1870 edit_move_up (edit, edit->curs_line - i, scroll);
1873 /* move i lines */
1874 void edit_move_down (WEdit * edit, int i, int scroll)
1876 long p, l = edit->total_lines - edit->curs_line;
1878 if (i > l)
1879 i = l;
1880 if (i) {
1881 if (i > 1)
1882 edit->force |= REDRAW_PAGE;
1883 if (scroll)
1884 edit_scroll_downward (edit, i);
1885 p = edit_bol (edit, edit->curs1);
1886 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1887 edit_move_to_prev_col (edit, p);
1889 edit->search_start = edit->curs1;
1890 edit->found_len = 0;
1894 /* moves down until a blank line is reached, or until just
1895 before a non-blank line is reached */
1896 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1898 int i;
1899 if (edit->curs_line >= edit->total_lines - 1) {
1900 i = edit->total_lines;
1901 } else {
1902 if (line_is_blank (edit, edit->curs_line)) {
1903 if (line_is_blank (edit, edit->curs_line + 1)) {
1904 for (i = edit->curs_line + 1; i; i++)
1905 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1906 i--;
1907 break;
1909 } else {
1910 for (i = edit->curs_line + 1; i; i++)
1911 if (line_is_blank (edit, i) || i >= edit->total_lines)
1912 break;
1914 } else {
1915 for (i = edit->curs_line + 1; i; i++)
1916 if (line_is_blank (edit, i) || i >= edit->total_lines)
1917 break;
1920 edit_move_down (edit, i - edit->curs_line, scroll);
1923 static void edit_begin_page (WEdit *edit)
1925 edit_update_curs_row (edit);
1926 edit_move_up (edit, edit->curs_row, 0);
1929 static void edit_end_page (WEdit *edit)
1931 edit_update_curs_row (edit);
1932 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1936 /* goto beginning of text */
1937 static void edit_move_to_top (WEdit * edit)
1939 if (edit->curs_line) {
1940 edit_cursor_move (edit, -edit->curs1);
1941 edit_move_to_prev_col (edit, 0);
1942 edit->force |= REDRAW_PAGE;
1943 edit->search_start = 0;
1944 edit_update_curs_row(edit);
1949 /* goto end of text */
1950 static void edit_move_to_bottom (WEdit * edit)
1952 if (edit->curs_line < edit->total_lines) {
1953 edit_cursor_move (edit, edit->curs2);
1954 edit->start_display = edit->last_byte;
1955 edit->start_line = edit->total_lines;
1956 edit_update_curs_row(edit);
1957 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1958 edit->force |= REDRAW_PAGE;
1962 /* goto beginning of line */
1963 static void edit_cursor_to_bol (WEdit * edit)
1965 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1966 edit->search_start = edit->curs1;
1967 edit->prev_col = edit_get_col (edit);
1968 edit->over_col = 0;
1971 /* goto end of line */
1972 static void edit_cursor_to_eol (WEdit * edit)
1974 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1975 edit->search_start = edit->curs1;
1976 edit->prev_col = edit_get_col (edit);
1977 edit->over_col = 0;
1980 /* move cursor to line 'line' */
1981 void edit_move_to_line (WEdit * e, long line)
1983 if(line < e->curs_line)
1984 edit_move_up (e, e->curs_line - line, 0);
1985 else
1986 edit_move_down (e, line - e->curs_line, 0);
1987 edit_scroll_screen_over_cursor (e);
1990 /* scroll window so that first visible line is 'line' */
1991 void edit_move_display (WEdit * e, long line)
1993 if(line < e->start_line)
1994 edit_scroll_upward (e, e->start_line - line);
1995 else
1996 edit_scroll_downward (e, line - e->start_line);
1999 /* save markers onto undo stack */
2000 void edit_push_markers (WEdit * edit)
2002 edit_push_action (edit, MARK_1 + edit->mark1);
2003 edit_push_action (edit, MARK_2 + edit->mark2);
2006 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2008 edit->mark1 = m1;
2009 edit->mark2 = m2;
2010 edit->column1 = c1;
2011 edit->column2 = c2;
2015 /* highlight marker toggle */
2016 void edit_mark_cmd (WEdit * edit, int unmark)
2018 edit_push_markers (edit);
2019 if (unmark) {
2020 edit_set_markers (edit, 0, 0, 0, 0);
2021 edit->force |= REDRAW_PAGE;
2022 } else {
2023 if (edit->mark2 >= 0) {
2024 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
2025 edit->force |= REDRAW_PAGE;
2026 } else
2027 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2031 static unsigned long
2032 my_type_of (int c)
2034 int x, r = 0;
2035 const char *p, *q;
2036 const char option_chars_move_whole_word[] =
2037 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2039 if (!c)
2040 return 0;
2041 if (c == '!') {
2042 if (*option_chars_move_whole_word == '!')
2043 return 2;
2044 return 0x80000000UL;
2046 if (g_ascii_isupper ((gchar) c))
2047 c = 'A';
2048 else if (g_ascii_islower ((gchar) c))
2049 c = 'a';
2050 else if (g_ascii_isalpha (c))
2051 c = 'a';
2052 else if (isdigit (c))
2053 c = '0';
2054 else if (isspace (c))
2055 c = ' ';
2056 q = strchr (option_chars_move_whole_word, c);
2057 if (!q)
2058 return 0xFFFFFFFFUL;
2059 do {
2060 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2061 if (*p == '!')
2062 x <<= 1;
2063 r |= x;
2064 } while ((q = strchr (q + 1, c)));
2065 return r;
2068 static void
2069 edit_left_word_move (WEdit *edit, int s)
2071 for (;;) {
2072 int c1, c2;
2073 edit_cursor_move (edit, -1);
2074 if (!edit->curs1)
2075 break;
2076 c1 = edit_get_byte (edit, edit->curs1 - 1);
2077 c2 = edit_get_byte (edit, edit->curs1);
2078 if (!(my_type_of (c1) & my_type_of (c2)))
2079 break;
2080 if (isspace (c1) && !isspace (c2))
2081 break;
2082 if (s)
2083 if (!isspace (c1) && isspace (c2))
2084 break;
2088 static void edit_left_word_move_cmd (WEdit * edit)
2090 edit_left_word_move (edit, 0);
2091 edit->force |= REDRAW_PAGE;
2094 static void
2095 edit_right_word_move (WEdit *edit, int s)
2097 for (;;) {
2098 int c1, c2;
2099 edit_cursor_move (edit, 1);
2100 if (edit->curs1 >= edit->last_byte)
2101 break;
2102 c1 = edit_get_byte (edit, edit->curs1 - 1);
2103 c2 = edit_get_byte (edit, edit->curs1);
2104 if (!(my_type_of (c1) & my_type_of (c2)))
2105 break;
2106 if (isspace (c1) && !isspace (c2))
2107 break;
2108 if (s)
2109 if (!isspace (c1) && isspace (c2))
2110 break;
2114 static void edit_right_word_move_cmd (WEdit * edit)
2116 edit_right_word_move (edit, 0);
2117 edit->force |= REDRAW_PAGE;
2120 static void edit_right_char_move_cmd (WEdit * edit)
2122 int cw = 1;
2123 int c = 0;
2124 if ( edit->utf8 ) {
2125 c = edit_get_utf (edit, edit->curs1, &cw);
2126 if ( cw < 1 )
2127 cw = 1;
2128 } else {
2129 c = edit_get_byte (edit, edit->curs1);
2131 if (option_cursor_beyond_eol && c == '\n') {
2132 edit->over_col++;
2133 } else {
2134 edit_cursor_move (edit, cw);
2138 static void edit_left_char_move_cmd (WEdit * edit)
2140 int cw = 1;
2141 if ( edit->utf8 ) {
2142 edit_get_prev_utf (edit, edit->curs1, &cw);
2143 if ( cw < 1 )
2144 cw = 1;
2146 if (option_cursor_beyond_eol && edit->over_col > 0) {
2147 edit->over_col--;
2148 } else {
2149 edit_cursor_move (edit, -cw);
2154 static void edit_right_delete_word (WEdit * edit)
2156 int c1, c2;
2157 for (;;) {
2158 if (edit->curs1 >= edit->last_byte)
2159 break;
2160 c1 = edit_delete (edit, 1);
2161 c2 = edit_get_byte (edit, edit->curs1);
2162 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2163 break;
2164 if (!(my_type_of (c1) & my_type_of (c2)))
2165 break;
2169 static void edit_left_delete_word (WEdit * edit)
2171 int c1, c2;
2172 for (;;) {
2173 if (edit->curs1 <= 0)
2174 break;
2175 c1 = edit_backspace (edit, 1);
2176 c2 = edit_get_byte (edit, edit->curs1 - 1);
2177 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2178 break;
2179 if (!(my_type_of (c1) & my_type_of (c2)))
2180 break;
2185 the start column position is not recorded, and hence does not
2186 undo as it happed. But who would notice.
2188 static void
2189 edit_do_undo (WEdit * edit)
2191 long ac;
2192 long count = 0;
2194 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2195 edit->over_col = 0;
2196 while ((ac = pop_action (edit)) < KEY_PRESS) {
2197 switch ((int) ac) {
2198 case STACK_BOTTOM:
2199 goto done_undo;
2200 case CURS_RIGHT:
2201 edit_cursor_move (edit, 1);
2202 break;
2203 case CURS_LEFT:
2204 edit_cursor_move (edit, -1);
2205 break;
2206 case BACKSPACE:
2207 edit_backspace (edit, 1);
2208 break;
2209 case DELCHAR:
2210 edit_delete (edit, 1);
2211 break;
2212 case COLUMN_ON:
2213 column_highlighting = 1;
2214 break;
2215 case COLUMN_OFF:
2216 column_highlighting = 0;
2217 break;
2219 if (ac >= 256 && ac < 512)
2220 edit_insert_ahead (edit, ac - 256);
2221 if (ac >= 0 && ac < 256)
2222 edit_insert (edit, ac);
2224 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2225 edit->mark1 = ac - MARK_1;
2226 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2227 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2228 edit->mark2 = ac - MARK_2;
2229 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2231 if (count++)
2232 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2235 if (edit->start_display > ac - KEY_PRESS) {
2236 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2237 edit->force |= REDRAW_PAGE;
2238 } else if (edit->start_display < ac - KEY_PRESS) {
2239 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2240 edit->force |= REDRAW_PAGE;
2242 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2243 edit_update_curs_row (edit);
2245 done_undo:;
2246 edit->stack_disable = 0;
2249 static void edit_delete_to_line_end (WEdit * edit)
2251 while (edit_get_byte (edit, edit->curs1) != '\n') {
2252 if (!edit->curs2)
2253 break;
2254 edit_delete (edit, 1);
2258 static void edit_delete_to_line_begin (WEdit * edit)
2260 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2261 if (!edit->curs1)
2262 break;
2263 edit_backspace (edit, 1);
2267 void
2268 edit_delete_line (WEdit *edit)
2271 * Delete right part of the line.
2272 * Note that edit_get_byte() returns '\n' when byte position is
2273 * beyond EOF.
2275 while (edit_get_byte (edit, edit->curs1) != '\n') {
2276 (void) edit_delete (edit, 1);
2280 * Delete '\n' char.
2281 * Note that edit_delete() will not corrupt anything if called while
2282 * cursor position is EOF.
2284 (void) edit_delete (edit, 1);
2287 * Delete left part of the line.
2288 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2290 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2291 (void) edit_backspace (edit, 1);
2295 void insert_spaces_tab (WEdit * edit, int half)
2297 int i;
2298 edit_update_curs_col (edit);
2299 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2300 while (i > 0) {
2301 edit_insert (edit, ' ');
2302 i -= space_width;
2306 static int is_aligned_on_a_tab (WEdit * edit)
2308 edit_update_curs_col (edit);
2309 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2310 return 0; /* not alligned on a tab */
2311 return 1;
2314 static int right_of_four_spaces (WEdit *edit)
2316 int i, ch = 0;
2317 for (i = 1; i <= HALF_TAB_SIZE; i++)
2318 ch |= edit_get_byte (edit, edit->curs1 - i);
2319 if (ch == ' ')
2320 return is_aligned_on_a_tab (edit);
2321 return 0;
2324 static int left_of_four_spaces (WEdit *edit)
2326 int i, ch = 0;
2327 for (i = 0; i < HALF_TAB_SIZE; i++)
2328 ch |= edit_get_byte (edit, edit->curs1 + i);
2329 if (ch == ' ')
2330 return is_aligned_on_a_tab (edit);
2331 return 0;
2334 int edit_indent_width (WEdit * edit, long p)
2336 long q = p;
2337 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2338 q++;
2339 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2342 void edit_insert_indent (WEdit * edit, int indent)
2344 if (!option_fill_tabs_with_spaces) {
2345 while (indent >= TAB_SIZE) {
2346 edit_insert (edit, '\t');
2347 indent -= TAB_SIZE;
2350 while (indent-- > 0)
2351 edit_insert (edit, ' ');
2354 static void
2355 edit_auto_indent (WEdit * edit)
2357 long p;
2358 char c;
2359 p = edit->curs1;
2360 /* use the previous line as a template */
2361 p = edit_move_backward (edit, p, 1);
2362 /* copy the leading whitespace of the line */
2363 for (;;) { /* no range check - the line _is_ \n-terminated */
2364 c = edit_get_byte (edit, p++);
2365 if (c != ' ' && c != '\t')
2366 break;
2367 edit_insert (edit, c);
2371 static void edit_double_newline (WEdit * edit)
2373 edit_insert (edit, '\n');
2374 if (edit_get_byte (edit, edit->curs1) == '\n')
2375 return;
2376 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2377 return;
2378 edit->force |= REDRAW_PAGE;
2379 edit_insert (edit, '\n');
2382 static void edit_tab_cmd (WEdit * edit)
2384 int i;
2386 if (option_fake_half_tabs) {
2387 if (is_in_indent (edit)) {
2388 /*insert a half tab (usually four spaces) unless there is a
2389 half tab already behind, then delete it and insert a
2390 full tab. */
2391 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2392 for (i = 1; i <= HALF_TAB_SIZE; i++)
2393 edit_backspace (edit, 1);
2394 edit_insert (edit, '\t');
2395 } else {
2396 insert_spaces_tab (edit, 1);
2398 return;
2401 if (option_fill_tabs_with_spaces) {
2402 insert_spaces_tab (edit, 0);
2403 } else {
2404 edit_insert (edit, '\t');
2406 return;
2409 static void check_and_wrap_line (WEdit * edit)
2411 int curs, c;
2412 if (!option_typewriter_wrap)
2413 return;
2414 edit_update_curs_col (edit);
2415 if (edit->curs_col < option_word_wrap_line_length)
2416 return;
2417 curs = edit->curs1;
2418 for (;;) {
2419 curs--;
2420 c = edit_get_byte (edit, curs);
2421 if (c == '\n' || curs <= 0) {
2422 edit_insert (edit, '\n');
2423 return;
2425 if (c == ' ' || c == '\t') {
2426 int current = edit->curs1;
2427 edit_cursor_move (edit, curs - edit->curs1 + 1);
2428 edit_insert (edit, '\n');
2429 edit_cursor_move (edit, current - edit->curs1 + 1);
2430 return;
2435 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2437 void edit_push_key_press (WEdit * edit)
2439 edit_push_action (edit, KEY_PRESS + edit->start_display);
2440 if (edit->mark2 == -1)
2441 edit_push_action (edit, MARK_1 + edit->mark1);
2444 /* this find the matching bracket in either direction, and sets edit->bracket */
2445 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2447 const char * const b = "{}{[][()(", *p;
2448 int i = 1, a, inc = -1, c, d, n = 0;
2449 unsigned long j = 0;
2450 long q;
2451 edit_update_curs_row (edit);
2452 c = edit_get_byte (edit, edit->curs1);
2453 p = strchr (b, c);
2454 /* no limit */
2455 if (!furthest_bracket_search)
2456 furthest_bracket_search--;
2457 /* not on a bracket at all */
2458 if (!p)
2459 return -1;
2460 /* the matching bracket */
2461 d = p[1];
2462 /* going left or right? */
2463 if (strchr ("{[(", c))
2464 inc = 1;
2465 for (q = edit->curs1 + inc;; q += inc) {
2466 /* out of buffer? */
2467 if (q >= edit->last_byte || q < 0)
2468 break;
2469 a = edit_get_byte (edit, q);
2470 /* don't want to eat CPU */
2471 if (j++ > furthest_bracket_search)
2472 break;
2473 /* out of screen? */
2474 if (in_screen) {
2475 if (q < edit->start_display)
2476 break;
2477 /* count lines if searching downward */
2478 if (inc > 0 && a == '\n')
2479 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2480 break;
2482 /* count bracket depth */
2483 i += (a == c) - (a == d);
2484 /* return if bracket depth is zero */
2485 if (!i)
2486 return q;
2488 /* no match */
2489 return -1;
2492 static long last_bracket = -1;
2494 void edit_find_bracket (WEdit * edit)
2496 edit->bracket = edit_get_bracket (edit, 1, 10000);
2497 if (last_bracket != edit->bracket)
2498 edit->force |= REDRAW_PAGE;
2499 last_bracket = edit->bracket;
2502 static void edit_goto_matching_bracket (WEdit *edit)
2504 long q;
2505 q = edit_get_bracket (edit, 0, 0);
2506 if (q < 0)
2507 return;
2508 edit->bracket = edit->curs1;
2509 edit->force |= REDRAW_PAGE;
2510 edit_cursor_move (edit, q - edit->curs1);
2514 * This executes a command as though the user initiated it through a key
2515 * press. Callback with WIDGET_KEY as a message calls this after
2516 * translating the key press. This function can be used to pass any
2517 * command to the editor. Note that the screen wouldn't update
2518 * automatically. Either of command or char_for_insertion must be
2519 * passed as -1. Commands are executed, and char_for_insertion is
2520 * inserted at the cursor.
2522 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2524 if (command == CK_Begin_Record_Macro) {
2525 edit->macro_i = 0;
2526 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2527 return;
2529 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2530 edit->force |= REDRAW_COMPLETELY;
2531 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2532 edit->macro_i = -1;
2533 return;
2535 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2536 edit->macro[edit->macro_i].command = command;
2537 edit->macro[edit->macro_i++].ch = char_for_insertion;
2539 /* record the beginning of a set of editing actions initiated by a key press */
2540 if (command != CK_Undo && command != CK_Ext_Mode)
2541 edit_push_key_press (edit);
2543 edit_execute_cmd (edit, command, char_for_insertion);
2544 if (column_highlighting)
2545 edit->force |= REDRAW_PAGE;
2548 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2551 This executes a command at a lower level than macro recording.
2552 It also does not push a key_press onto the undo stack. This means
2553 that if it is called many times, a single undo command will undo
2554 all of them. It also does not check for the Undo command.
2556 void
2557 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2559 edit->force |= REDRAW_LINE;
2561 /* The next key press will unhighlight the found string, so update
2562 * the whole page */
2563 if (edit->found_len || column_highlighting)
2564 edit->force |= REDRAW_PAGE;
2566 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2567 column_highlighting = 0;
2568 if (!edit->highlight
2569 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2570 edit_mark_cmd (edit, 1); /* clear */
2571 edit_mark_cmd (edit, 0); /* marking on */
2573 edit->highlight = 1;
2574 } else { /* any other command */
2575 if (edit->highlight)
2576 edit_mark_cmd (edit, 0); /* clear */
2577 edit->highlight = 0;
2580 /* first check for undo */
2581 if (command == CK_Undo) {
2582 edit_do_undo (edit);
2583 edit->found_len = 0;
2584 edit->prev_col = edit_get_col (edit);
2585 edit->search_start = edit->curs1;
2586 return;
2589 /* An ordinary key press */
2590 if (char_for_insertion >= 0) {
2591 if (edit->overwrite) {
2592 if (edit_get_byte (edit, edit->curs1) != '\n')
2593 edit_delete (edit, 0);
2595 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2596 edit_insert_over (edit);
2597 #ifdef HAVE_CHARSET
2598 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2599 unsigned char str[6 + 1];
2600 size_t i = 0;
2601 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2602 if ( res == 0 ) {
2603 str[0] = '.';
2604 str[1] = '\0';
2605 } else {
2606 str[res] = '\0';
2608 while ( str[i] != 0 && i<=6) {
2609 char_for_insertion = str[i];
2610 edit_insert (edit, char_for_insertion);
2611 i++;
2613 } else {
2614 #endif
2615 edit_insert (edit, char_for_insertion);
2616 #ifdef HAVE_CHARSET
2618 #endif
2619 if (option_auto_para_formatting) {
2620 format_paragraph (edit, 0);
2621 edit->force |= REDRAW_PAGE;
2622 } else
2623 check_and_wrap_line (edit);
2624 edit->found_len = 0;
2625 edit->prev_col = edit_get_col (edit);
2626 edit->search_start = edit->curs1;
2627 edit_find_bracket (edit);
2628 return;
2631 switch (command) {
2632 case CK_Begin_Page:
2633 case CK_End_Page:
2634 case CK_Begin_Page_Highlight:
2635 case CK_End_Page_Highlight:
2636 case CK_Word_Left:
2637 case CK_Word_Right:
2638 case CK_Up:
2639 case CK_Down:
2640 case CK_Left:
2641 case CK_Right:
2642 if ( edit->mark2 >= 0 ) {
2643 if ( !option_persistent_selections ) {
2644 if (column_highlighting)
2645 edit_push_action (edit, COLUMN_ON);
2646 column_highlighting = 0;
2647 edit_mark_cmd (edit, 1);
2652 switch (command) {
2653 case CK_Begin_Page:
2654 case CK_End_Page:
2655 case CK_Begin_Page_Highlight:
2656 case CK_End_Page_Highlight:
2657 case CK_Word_Left:
2658 case CK_Word_Right:
2659 case CK_Up:
2660 case CK_Down:
2661 case CK_Word_Left_Highlight:
2662 case CK_Word_Right_Highlight:
2663 case CK_Up_Highlight:
2664 case CK_Down_Highlight:
2665 case CK_Up_Alt_Highlight:
2666 case CK_Down_Alt_Highlight:
2667 if (edit->mark2 == -1)
2668 break; /*marking is following the cursor: may need to highlight a whole line */
2669 case CK_Left:
2670 case CK_Right:
2671 case CK_Left_Highlight:
2672 case CK_Right_Highlight:
2673 edit->force |= REDRAW_CHAR_ONLY;
2676 /* basic cursor key commands */
2677 switch (command) {
2678 case CK_BackSpace:
2679 /* if non persistent selection and text selected */
2680 if ( !option_persistent_selections ) {
2681 if ( edit->mark1 != edit->mark2 ) {
2682 edit_block_delete_cmd (edit);
2683 break;
2686 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2687 edit->over_col--;
2688 break;
2690 if (option_backspace_through_tabs && is_in_indent (edit)) {
2691 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2692 && edit->curs1 > 0)
2693 edit_backspace (edit, 1);
2694 break;
2695 } else {
2696 if (option_fake_half_tabs) {
2697 int i;
2698 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2699 for (i = 0; i < HALF_TAB_SIZE; i++)
2700 edit_backspace (edit, 1);
2701 break;
2705 edit_backspace (edit, 0);
2706 break;
2707 case CK_Delete:
2708 /* if non persistent selection and text selected */
2709 if ( !option_persistent_selections ) {
2710 if ( edit->mark1 != edit->mark2 ) {
2711 edit_block_delete_cmd (edit);
2712 break;
2716 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2717 edit_insert_over (edit);
2719 if (option_fake_half_tabs) {
2720 int i;
2721 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2722 for (i = 1; i <= HALF_TAB_SIZE; i++)
2723 edit_delete (edit, 1);
2724 break;
2727 edit_delete (edit, 0);
2728 break;
2729 case CK_Delete_Word_Left:
2730 edit->over_col = 0;
2731 edit_left_delete_word (edit);
2732 break;
2733 case CK_Delete_Word_Right:
2734 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2735 edit_insert_over (edit);
2737 edit_right_delete_word (edit);
2738 break;
2739 case CK_Delete_Line:
2740 edit_delete_line (edit);
2741 break;
2742 case CK_Delete_To_Line_End:
2743 edit_delete_to_line_end (edit);
2744 break;
2745 case CK_Delete_To_Line_Begin:
2746 edit_delete_to_line_begin (edit);
2747 break;
2748 case CK_Enter:
2749 edit->over_col = 0;
2750 if (option_auto_para_formatting) {
2751 edit_double_newline (edit);
2752 if (option_return_does_auto_indent)
2753 edit_auto_indent (edit);
2754 format_paragraph (edit, 0);
2755 } else {
2756 edit_insert (edit, '\n');
2757 if (option_return_does_auto_indent) {
2758 edit_auto_indent (edit);
2761 break;
2762 case CK_Return:
2763 edit_insert (edit, '\n');
2764 break;
2766 case CK_Page_Up_Alt_Highlight:
2767 column_highlighting = 1;
2768 case CK_Page_Up:
2769 case CK_Page_Up_Highlight:
2770 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2771 break;
2772 case CK_Page_Down_Alt_Highlight:
2773 column_highlighting = 1;
2774 case CK_Page_Down:
2775 case CK_Page_Down_Highlight:
2776 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2777 break;
2778 case CK_Left_Alt_Highlight:
2779 column_highlighting = 1;
2780 case CK_Left:
2781 case CK_Left_Highlight:
2782 if (option_fake_half_tabs) {
2783 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2784 if ( option_cursor_beyond_eol && edit->over_col > 0)
2785 edit->over_col--;
2786 else
2787 edit_cursor_move (edit, -HALF_TAB_SIZE);
2788 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2789 break;
2792 edit_left_char_move_cmd (edit);
2793 break;
2794 case CK_Right_Alt_Highlight:
2795 column_highlighting = 1;
2796 case CK_Right:
2797 case CK_Right_Highlight:
2798 if (option_fake_half_tabs) {
2799 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2800 edit_cursor_move (edit, HALF_TAB_SIZE);
2801 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2802 break;
2805 edit_right_char_move_cmd (edit);
2806 break;
2807 case CK_Begin_Page:
2808 case CK_Begin_Page_Highlight:
2809 edit_begin_page (edit);
2810 break;
2811 case CK_End_Page:
2812 case CK_End_Page_Highlight:
2813 edit_end_page (edit);
2814 break;
2815 case CK_Word_Left:
2816 case CK_Word_Left_Highlight:
2817 edit->over_col = 0;
2818 edit_left_word_move_cmd (edit);
2819 break;
2820 case CK_Word_Right:
2821 case CK_Word_Right_Highlight:
2822 edit->over_col = 0;
2823 edit_right_word_move_cmd (edit);
2824 break;
2825 case CK_Up_Alt_Highlight:
2826 column_highlighting = 1;
2827 case CK_Up:
2828 case CK_Up_Highlight:
2829 edit_move_up (edit, 1, 0);
2830 break;
2831 case CK_Down_Alt_Highlight:
2832 column_highlighting = 1;
2833 case CK_Down:
2834 case CK_Down_Highlight:
2835 edit_move_down (edit, 1, 0);
2836 break;
2837 case CK_Paragraph_Up_Alt_Highlight:
2838 column_highlighting = 1;
2839 case CK_Paragraph_Up:
2840 case CK_Paragraph_Up_Highlight:
2841 edit_move_up_paragraph (edit, 0);
2842 break;
2843 case CK_Paragraph_Down_Alt_Highlight:
2844 column_highlighting = 1;
2845 case CK_Paragraph_Down:
2846 case CK_Paragraph_Down_Highlight:
2847 edit_move_down_paragraph (edit, 0);
2848 break;
2849 case CK_Scroll_Up_Alt_Highlight:
2850 column_highlighting = 1;
2851 case CK_Scroll_Up:
2852 case CK_Scroll_Up_Highlight:
2853 edit_move_up (edit, 1, 1);
2854 break;
2855 case CK_Scroll_Down_Alt_Highlight:
2856 column_highlighting = 1;
2857 case CK_Scroll_Down:
2858 case CK_Scroll_Down_Highlight:
2859 edit_move_down (edit, 1, 1);
2860 break;
2861 case CK_Home:
2862 case CK_Home_Highlight:
2863 edit_cursor_to_bol (edit);
2864 break;
2865 case CK_End:
2866 case CK_End_Highlight:
2867 edit_cursor_to_eol (edit);
2868 break;
2869 case CK_Tab:
2870 /* if text marked shift block */
2871 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2872 if (edit->mark2 < 0)
2873 edit_mark_cmd (edit, 0);
2874 edit_move_block_to_right (edit);
2875 } else {
2876 if ( option_cursor_beyond_eol )
2877 edit_insert_over (edit);
2878 edit_tab_cmd (edit);
2879 if (option_auto_para_formatting) {
2880 format_paragraph (edit, 0);
2881 edit->force |= REDRAW_PAGE;
2882 } else {
2883 check_and_wrap_line (edit);
2886 break;
2888 case CK_Toggle_Insert:
2889 edit->overwrite = (edit->overwrite == 0);
2890 break;
2892 case CK_Mark:
2893 if (edit->mark2 >= 0) {
2894 if (column_highlighting)
2895 edit_push_action (edit, COLUMN_ON);
2896 column_highlighting = 0;
2898 edit_mark_cmd (edit, 0);
2899 break;
2900 case CK_Column_Mark:
2901 if (!column_highlighting)
2902 edit_push_action (edit, COLUMN_OFF);
2903 column_highlighting = 1;
2904 edit_mark_cmd (edit, 0);
2905 break;
2906 case CK_Unmark:
2907 if (column_highlighting)
2908 edit_push_action (edit, COLUMN_ON);
2909 column_highlighting = 0;
2910 edit_mark_cmd (edit, 1);
2911 break;
2913 case CK_Toggle_Line_State:
2914 option_line_state = !option_line_state;
2915 if ( option_line_state ) {
2916 option_line_state_width = LINE_STATE_WIDTH;
2917 } else {
2918 option_line_state_width = 0;
2920 edit->force |= REDRAW_PAGE;
2921 break;
2923 case CK_Toggle_Bookmark:
2924 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2925 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2926 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2927 else
2928 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2929 break;
2930 case CK_Flush_Bookmarks:
2931 book_mark_flush (edit, BOOK_MARK_COLOR);
2932 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2933 edit->force |= REDRAW_PAGE;
2934 break;
2935 case CK_Next_Bookmark:
2936 if (edit->book_mark) {
2937 struct _book_mark *p;
2938 p = (struct _book_mark *) book_mark_find (edit,
2939 edit->curs_line);
2940 if (p->next) {
2941 p = p->next;
2942 if (p->line >= edit->start_line + edit->num_widget_lines
2943 || p->line < edit->start_line)
2944 edit_move_display (edit,
2945 p->line -
2946 edit->num_widget_lines / 2);
2947 edit_move_to_line (edit, p->line);
2950 break;
2951 case CK_Prev_Bookmark:
2952 if (edit->book_mark) {
2953 struct _book_mark *p;
2954 p = (struct _book_mark *) book_mark_find (edit,
2955 edit->curs_line);
2956 while (p->line == edit->curs_line)
2957 if (p->prev)
2958 p = p->prev;
2959 if (p->line >= 0) {
2960 if (p->line >= edit->start_line + edit->num_widget_lines
2961 || p->line < edit->start_line)
2962 edit_move_display (edit,
2963 p->line -
2964 edit->num_widget_lines / 2);
2965 edit_move_to_line (edit, p->line);
2968 break;
2970 case CK_Beginning_Of_Text:
2971 case CK_Beginning_Of_Text_Highlight:
2972 edit_move_to_top (edit);
2973 break;
2974 case CK_End_Of_Text:
2975 case CK_End_Of_Text_Highlight:
2976 edit_move_to_bottom (edit);
2977 break;
2979 case CK_Copy:
2980 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2981 edit_insert_over (edit);
2982 edit_block_copy_cmd (edit);
2983 break;
2984 case CK_Remove:
2985 edit_block_delete_cmd (edit);
2986 break;
2987 case CK_Move:
2988 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2989 edit_insert_over (edit);
2990 edit_block_move_cmd (edit);
2991 break;
2993 case CK_XStore:
2994 edit_copy_to_X_buf_cmd (edit);
2995 break;
2996 case CK_XCut:
2997 edit_cut_to_X_buf_cmd (edit);
2998 break;
2999 case CK_XPaste:
3000 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3001 edit_insert_over (edit);
3002 edit_paste_from_X_buf_cmd (edit);
3003 break;
3004 case CK_Selection_History:
3005 edit_paste_from_history (edit);
3006 break;
3008 case CK_Save_As:
3009 edit_save_as_cmd (edit);
3010 break;
3011 case CK_Save:
3012 edit_save_confirm_cmd (edit);
3013 break;
3014 case CK_Load:
3015 edit_load_cmd (edit, EDIT_FILE_COMMON);
3016 break;
3017 case CK_Save_Block:
3018 edit_save_block_cmd (edit);
3019 break;
3020 case CK_Insert_File:
3021 edit_insert_file_cmd (edit);
3022 break;
3024 case CK_Load_Prev_File:
3025 edit_load_back_cmd (edit);
3026 break;
3027 case CK_Load_Next_File:
3028 edit_load_forward_cmd (edit);
3029 break;
3031 case CK_Load_Syntax_File:
3032 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3033 break;
3034 case CK_Choose_Syntax:
3035 edit_syntax_dialog ();
3036 break;
3038 case CK_Load_Menu_File:
3039 edit_load_cmd (edit, EDIT_FILE_MENU);
3040 break;
3042 case CK_Toggle_Syntax:
3043 if ((option_syntax_highlighting ^= 1) == 1)
3044 edit_load_syntax (edit, NULL, option_syntax_type);
3045 edit->force |= REDRAW_PAGE;
3046 break;
3048 case CK_Toggle_Tab_TWS:
3049 enable_show_tabs_tws ^= 1;
3050 edit->force |= REDRAW_PAGE;
3051 break;
3053 case CK_Find:
3054 edit_search_cmd (edit, 0);
3055 break;
3056 case CK_Find_Again:
3057 edit_search_cmd (edit, 1);
3058 break;
3059 case CK_Replace:
3060 edit_replace_cmd (edit, 0);
3061 break;
3062 case CK_Replace_Again:
3063 edit_replace_cmd (edit, 1);
3064 break;
3065 case CK_Complete_Word:
3066 /* if text marked shift block */
3067 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3068 edit_move_block_to_left (edit);
3069 } else {
3070 edit_complete_word_cmd (edit);
3072 break;
3073 case CK_Find_Definition:
3074 edit_get_match_keyword_cmd (edit);
3075 break;
3076 case CK_Quit:
3077 dlg_stop (edit->widget.parent);
3078 break;
3079 case CK_New:
3080 edit_new_cmd (edit);
3081 break;
3082 case CK_Help:
3083 edit_help_cmd (edit);
3084 break;
3085 case CK_Refresh:
3086 edit_refresh_cmd (edit);
3087 break;
3088 case CK_SaveSetupCmd:
3089 save_setup_cmd ();
3090 break;
3091 case CK_About:
3092 query_dialog (_(" About "),
3093 _("\n Cooledit v3.11.5\n\n"
3094 " Copyright (C) 1996 the Free Software Foundation\n\n"
3095 " A user friendly text editor written\n"
3096 " for the Midnight Commander.\n"), D_NORMAL,
3097 1, _("&OK"));
3098 break;
3099 case CK_LearnKeys:
3100 learn_keys ();
3101 break;
3102 case CK_Edit_Options:
3103 edit_options_dialog ();
3104 break;
3105 case CK_Edit_Save_Mode:
3106 menu_save_mode_cmd ();
3107 case CK_Date:
3108 break;
3110 char s[1024];
3111 /* fool gcc to prevent a Y2K warning */
3112 char time_format[] = "_c";
3113 time_format[0] = '%';
3115 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3116 edit_print_string (edit, s);
3117 edit->force |= REDRAW_PAGE;
3118 break;
3120 case CK_Goto:
3121 edit_goto_cmd (edit);
3122 break;
3123 case CK_Paragraph_Format:
3124 format_paragraph (edit, 1);
3125 edit->force |= REDRAW_PAGE;
3126 break;
3127 case CK_Delete_Macro:
3128 edit_delete_macro_cmd (edit);
3129 break;
3130 case CK_Match_Bracket:
3131 edit_goto_matching_bracket (edit);
3132 break;
3133 case CK_User_Menu:
3134 user_menu (edit);
3135 break;
3136 case CK_Sort:
3137 edit_sort_cmd (edit);
3138 break;
3139 case CK_ExtCmd:
3140 edit_ext_cmd (edit);
3141 break;
3142 case CK_Mail:
3143 edit_mail_dialog (edit);
3144 break;
3145 case CK_Shell:
3146 view_other_cmd ();
3147 break;
3148 case CK_SelectCodepage:
3149 edit_select_codepage_cmd (edit);
3150 break;
3151 case CK_Insert_Literal:
3152 edit_insert_literal_cmd (edit);
3153 break;
3154 case CK_Execute_Macro:
3155 edit_execute_macro_cmd (edit);
3156 break;
3157 case CK_Begin_End_Macro:
3158 edit_begin_end_macro_cmd (edit);
3159 break;
3160 case CK_Ext_Mode:
3161 edit->extmod = 1;
3162 break;
3163 default:
3164 break;
3167 /* CK_Pipe_Block */
3168 if ((command / 1000) == 1) /* a shell command */
3169 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3170 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3171 struct macro m[MAX_MACRO_LENGTH];
3172 int nm;
3173 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3174 edit_execute_macro (edit, m, nm);
3177 /* keys which must set the col position, and the search vars */
3178 switch (command) {
3179 case CK_Find:
3180 case CK_Find_Again:
3181 case CK_Replace:
3182 case CK_Replace_Again:
3183 case CK_Complete_Word:
3184 edit->prev_col = edit_get_col (edit);
3185 break;
3186 case CK_Up:
3187 case CK_Up_Highlight:
3188 case CK_Up_Alt_Highlight:
3189 case CK_Down:
3190 case CK_Down_Highlight:
3191 case CK_Down_Alt_Highlight:
3192 case CK_Page_Up:
3193 case CK_Page_Up_Highlight:
3194 case CK_Page_Up_Alt_Highlight:
3195 case CK_Page_Down:
3196 case CK_Page_Down_Highlight:
3197 case CK_Page_Down_Alt_Highlight:
3198 case CK_Beginning_Of_Text:
3199 case CK_Beginning_Of_Text_Highlight:
3200 case CK_End_Of_Text:
3201 case CK_End_Of_Text_Highlight:
3202 case CK_Paragraph_Up:
3203 case CK_Paragraph_Up_Highlight:
3204 case CK_Paragraph_Up_Alt_Highlight:
3205 case CK_Paragraph_Down:
3206 case CK_Paragraph_Down_Highlight:
3207 case CK_Paragraph_Down_Alt_Highlight:
3208 case CK_Scroll_Up:
3209 case CK_Scroll_Up_Highlight:
3210 case CK_Scroll_Up_Alt_Highlight:
3211 case CK_Scroll_Down:
3212 case CK_Scroll_Down_Highlight:
3213 case CK_Scroll_Down_Alt_Highlight:
3214 edit->search_start = edit->curs1;
3215 edit->found_len = 0;
3216 break;
3217 default:
3218 edit->found_len = 0;
3219 edit->prev_col = edit_get_col (edit);
3220 edit->search_start = edit->curs1;
3222 edit_find_bracket (edit);
3224 if (option_auto_para_formatting) {
3225 switch (command) {
3226 case CK_BackSpace:
3227 case CK_Delete:
3228 case CK_Delete_Word_Left:
3229 case CK_Delete_Word_Right:
3230 case CK_Delete_To_Line_End:
3231 case CK_Delete_To_Line_Begin:
3232 format_paragraph (edit, 0);
3233 edit->force |= REDRAW_PAGE;
3239 static void
3240 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3242 int i = 0;
3244 if (edit->macro_depth++ > 256) {
3245 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3246 edit->macro_depth--;
3247 return;
3249 edit->force |= REDRAW_PAGE;
3250 for (; i < n; i++) {
3251 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3253 edit_update_screen (edit);
3254 edit->macro_depth--;
3257 /* User edit menu, like user menu (F2) but only in editor. */
3258 static void
3259 user_menu (WEdit * edit)
3261 FILE *fd;
3262 int nomark;
3263 struct stat status;
3264 long start_mark, end_mark;
3265 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3266 int rc = 0;
3268 nomark = eval_marks (edit, &start_mark, &end_mark);
3269 if (!nomark) /* remember marked or not */
3270 edit_save_block (edit, block_file, start_mark, end_mark);
3272 /* run shell scripts from menu */
3273 user_menu_cmd (edit);
3275 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3276 /* no block messages */
3277 goto cleanup;
3280 if (!nomark) {
3281 /* i.e. we have marked block */
3282 rc = edit_block_delete_cmd (edit);
3285 if (!rc) {
3286 edit_insert_file (edit, block_file);
3289 /* truncate block file */
3290 if ((fd = fopen (block_file, "w"))) {
3291 fclose (fd);
3294 edit_refresh_cmd (edit);
3295 edit->force |= REDRAW_COMPLETELY;
3297 cleanup:
3298 g_free (block_file);
3301 void
3302 edit_stack_init (void)
3304 for (edit_stack_iterator = 0;
3305 edit_stack_iterator < MAX_HISTORY_MOVETO;
3306 edit_stack_iterator++ ) {
3307 edit_history_moveto[edit_stack_iterator].filename = NULL;
3308 edit_history_moveto[edit_stack_iterator].line = -1;
3311 edit_stack_iterator = 0;
3314 void
3315 edit_stack_free (void)
3317 for (edit_stack_iterator = 0;
3318 edit_stack_iterator < MAX_HISTORY_MOVETO;
3319 edit_stack_iterator++)
3320 g_free (edit_history_moveto[edit_stack_iterator].filename);