Merge branch '277_konsole_256color'
[midnight-commander.git] / edit / edit.c
blob02d6cae8b5038d853bd3a9faa3c9acad9a42ab48
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 "editcmddef.h"
48 #include "usermap.h"
50 #include "../src/tty/color.h" /* EDITOR_NORMAL_COLOR */
51 #include "../src/tty/tty.h" /* attrset() */
52 #include "../src/tty/key.h" /* is_idle() */
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 */
64 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
65 or EDIT_KEY_EMULATION_EMACS
67 int edit_key_emulation = EDIT_KEY_EMULATION_NORMAL;
69 int option_word_wrap_line_length = 72;
70 int option_typewriter_wrap = 0;
71 int option_auto_para_formatting = 0;
72 int option_tab_spacing = 8;
73 int option_fill_tabs_with_spaces = 0;
74 int option_return_does_auto_indent = 1;
75 int option_backspace_through_tabs = 0;
76 int option_fake_half_tabs = 1;
77 int option_save_mode = EDIT_QUICK_SAVE;
78 int option_save_position = 1;
79 int option_max_undo = 32768;
80 int option_persistent_selections = 1;
81 int option_cursor_beyond_eol = 1;
82 int option_line_state = 0;
83 int option_line_state_width = 0;
85 int option_edit_right_extreme = 0;
86 int option_edit_left_extreme = 0;
87 int option_edit_top_extreme = 0;
88 int option_edit_bottom_extreme = 0;
89 int enable_show_tabs_tws = 1;
91 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
92 char *option_backup_ext = NULL;
94 int edit_stack_iterator = 0;
95 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
96 /* magic sequense for say than block is vertical */
97 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
98 /*-
100 * here's a quick sketch of the layout: (don't run this through indent.)
102 * (b1 is buffers1 and b2 is buffers2)
105 * \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
106 * ______________________________________|______________________________________
108 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
109 * |-> |-> |-> |-> |-> |-> |
111 * _<------------------------->|<----------------->_
112 * WEdit->curs2 | WEdit->curs1
113 * ^ | ^
114 * | ^|^ |
115 * cursor ||| cursor
116 * |||
117 * file end|||file beginning
122 * This_is_some_file
123 * fin.
127 static void user_menu (WEdit *edit);
129 int edit_get_byte (WEdit * edit, long byte_index)
131 unsigned long p;
132 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
133 return '\n';
135 if (byte_index >= edit->curs1) {
136 p = edit->curs1 + edit->curs2 - byte_index - 1;
137 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
138 } else {
139 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
143 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
145 unsigned long p;
147 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
148 return NULL;
150 if (byte_index >= edit->curs1) {
151 p = edit->curs1 + edit->curs2 - byte_index - 1;
152 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
153 } else {
154 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
158 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
160 unsigned long p;
162 if (byte_index >= (edit->curs1 + edit->curs2) ) {
163 byte_index -= 1;
166 if ( byte_index < 0 ) {
167 return NULL;
170 if (byte_index >= edit->curs1) {
171 p = edit->curs1 + edit->curs2 - 1;
172 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
173 } else {
174 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
178 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
180 gchar *str = NULL;
181 int res = -1;
182 gunichar ch;
183 gchar *next_ch = NULL;
184 int width = 0;
186 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
187 *char_width = 0;
188 return '\n';
192 str = edit_get_byte_ptr (edit, byte_index);
193 res = g_utf8_get_char_validated (str, -1);
195 if ( res < 0 ) {
196 ch = *str;
197 width = 0;
198 } else {
199 ch = res;
200 /* Calculate UTF-8 char width */
201 next_ch = g_utf8_next_char(str);
202 if ( next_ch ) {
203 width = next_ch - str;
204 } else {
205 ch = 0;
206 width = 0;
209 *char_width = width;
210 return ch;
213 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
215 gchar *str, *buf = NULL;
216 int res = -1;
217 gunichar ch;
218 gchar *next_ch = NULL;
219 int width = 0;
221 if ( byte_index > 0 ) {
222 byte_index--;
225 ch = edit_get_utf (edit, byte_index, &width);
226 if ( width == 1 ) {
227 *char_width = width;
228 return ch;
231 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
232 *char_width = 0;
233 return 0;
236 str = edit_get_byte_ptr (edit, byte_index);
237 buf = edit_get_buf_ptr (edit, byte_index);
238 /* get prev utf8 char */
239 if ( str != buf )
240 str = g_utf8_find_prev_char (buf, str);
242 res = g_utf8_get_char_validated (str, -1);
243 if ( res < 0 ) {
244 ch = *str;
245 width = 0;
246 } else {
247 ch = res;
248 /* Calculate UTF-8 char width */
249 next_ch = g_utf8_next_char(str);
250 if ( next_ch ) {
251 width = next_ch - str;
252 } else {
253 ch = 0;
254 width = 0;
257 *char_width = width;
258 return ch;
262 * Initialize the buffers for an empty files.
264 static void
265 edit_init_buffers (WEdit *edit)
267 int j;
269 for (j = 0; j <= MAXBUFF; j++) {
270 edit->buffers1[j] = NULL;
271 edit->buffers2[j] = NULL;
274 edit->curs1 = 0;
275 edit->curs2 = 0;
276 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
280 * Load file OR text into buffers. Set cursor to the beginning of file.
281 * Return 1 on error.
283 static int
284 edit_load_file_fast (WEdit *edit, const char *filename)
286 long buf, buf2;
287 int file = -1;
288 edit->curs2 = edit->last_byte;
289 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
290 edit->utf8 = 0;
291 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
292 GString *errmsg = g_string_new(NULL);
293 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
294 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
295 g_string_free (errmsg, TRUE);
296 return 1;
299 if (!edit->buffers2[buf2])
300 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
302 mc_read (file,
303 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
304 (edit->curs2 & M_EDIT_BUF_SIZE),
305 edit->curs2 & M_EDIT_BUF_SIZE);
307 for (buf = buf2 - 1; buf >= 0; buf--) {
308 /* edit->buffers2[0] is already allocated */
309 if (!edit->buffers2[buf])
310 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
311 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
314 mc_close (file);
315 return 0;
318 /* detecting an error on save is easy: just check if every byte has been written. */
319 /* detecting an error on read, is not so easy 'cos there is not way to tell
320 whether you read everything or not. */
321 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
322 static const struct edit_filters {
323 const char *read, *write, *extension;
324 } all_filters[] = {
325 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
326 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
327 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
328 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
329 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
332 /* Return index of the filter or -1 is there is no appropriate filter */
333 static int edit_find_filter (const char *filename)
335 size_t i, l, e;
336 if (!filename)
337 return -1;
338 l = strlen (filename);
339 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
340 e = strlen (all_filters[i].extension);
341 if (l > e)
342 if (!strcmp (all_filters[i].extension, filename + l - e))
343 return i;
345 return -1;
348 static char *
349 edit_get_filter (const char *filename)
351 int i, l;
352 char *p, *quoted_name;
353 i = edit_find_filter (filename);
354 if (i < 0)
355 return 0;
356 quoted_name = name_quote (filename, 0);
357 l = str_term_width1 (quoted_name);
358 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
359 sprintf (p, all_filters[i].read, quoted_name);
360 g_free (quoted_name);
361 return p;
364 char *
365 edit_get_write_filter (const char *write_name, const char *filename)
367 int i, l;
368 char *p, *writename;
369 i = edit_find_filter (filename);
370 if (i < 0)
371 return 0;
372 writename = name_quote (write_name, 0);
373 l = str_term_width1 (writename);
374 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
375 sprintf (p, all_filters[i].write, writename);
376 g_free (writename);
377 return p;
380 static long
381 edit_insert_stream (WEdit * edit, FILE * f)
383 int c;
384 long i = 0;
385 while ((c = fgetc (f)) >= 0) {
386 edit_insert (edit, c);
387 i++;
389 return i;
392 long edit_write_stream (WEdit * edit, FILE * f)
394 long i;
395 for (i = 0; i < edit->last_byte; i++)
396 if (fputc (edit_get_byte (edit, i), f) < 0)
397 break;
398 return i;
401 #define TEMP_BUF_LEN 1024
403 /* inserts a file at the cursor, returns 1 on success */
405 edit_insert_file (WEdit *edit, const char *filename)
407 char *p;
408 if ((p = edit_get_filter (filename))) {
409 FILE *f;
410 long current = edit->curs1;
411 f = (FILE *) popen (p, "r");
412 if (f) {
413 edit_insert_stream (edit, f);
414 edit_cursor_move (edit, current - edit->curs1);
415 if (pclose (f) > 0) {
416 GString *errmsg = g_string_new (NULL);
417 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
418 edit_error_dialog (_("Error"), errmsg->str);
419 g_string_free (errmsg, TRUE);
420 g_free (p);
421 return 0;
423 } else {
424 GString *errmsg = g_string_new (NULL);
425 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
426 edit_error_dialog (_("Error"), errmsg->str);
427 g_string_free (errmsg, TRUE);
428 g_free (p);
429 return 0;
431 g_free (p);
432 } else {
433 int i, file, blocklen;
434 long current = edit->curs1;
435 int vertical_insertion = 0;
436 char *buf;
437 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
438 return 0;
439 buf = g_malloc (TEMP_BUF_LEN);
440 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
441 if (blocklen > 0) {
442 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
443 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
444 vertical_insertion = 1;
445 } else {
446 mc_lseek (file, 0, SEEK_SET);
449 if (vertical_insertion) {
450 blocklen = edit_insert_column_of_text_from_file (edit, file);
451 } else {
452 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
453 for (i = 0; i < blocklen; i++)
454 edit_insert (edit, buf[i]);
457 edit_cursor_move (edit, current - edit->curs1);
458 g_free (buf);
459 mc_close (file);
460 if (blocklen)
461 return 0;
463 return 1;
466 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
467 static int
468 check_file_access (WEdit *edit, const char *filename, struct stat *st)
470 int file;
471 GString *errmsg = (GString *) 0;
473 /* Try opening an existing file */
474 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
476 if (file < 0) {
478 * Try creating the file. O_EXCL prevents following broken links
479 * and opening existing files.
481 file =
482 mc_open (filename,
483 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
484 0666);
485 if (file < 0) {
486 g_string_sprintf (errmsg = g_string_new (NULL),
487 _(" Cannot open %s for reading "), filename);
488 goto cleanup;
489 } else {
490 /* New file, delete it if it's not modified or saved */
491 edit->delete_file = 1;
495 /* Check what we have opened */
496 if (mc_fstat (file, st) < 0) {
497 g_string_sprintf (errmsg = g_string_new (NULL),
498 _(" Cannot get size/permissions for %s "), filename);
499 goto cleanup;
502 /* We want to open regular files only */
503 if (!S_ISREG (st->st_mode)) {
504 g_string_sprintf (errmsg = g_string_new (NULL),
505 _(" %s is not a regular file "), filename);
506 goto cleanup;
510 * Don't delete non-empty files.
511 * O_EXCL should prevent it, but let's be on the safe side.
513 if (st->st_size > 0) {
514 edit->delete_file = 0;
517 if (st->st_size >= SIZE_LIMIT) {
518 g_string_sprintf (errmsg = g_string_new (NULL),
519 _(" File %s is too large "), filename);
520 goto cleanup;
523 cleanup:
524 (void) mc_close (file);
525 if (errmsg) {
526 edit_error_dialog (_("Error"), errmsg->str);
527 g_string_free (errmsg, TRUE);
528 return 1;
530 return 0;
534 * Open the file and load it into the buffers, either directly or using
535 * a filter. Return 0 on success, 1 on error.
537 * Fast loading (edit_load_file_fast) is used when the file size is
538 * known. In this case the data is read into the buffers by blocks.
539 * If the file size is not known, the data is loaded byte by byte in
540 * edit_insert_file.
542 static int
543 edit_load_file (WEdit *edit)
545 int fast_load = 1;
547 /* Cannot do fast load if a filter is used */
548 if (edit_find_filter (edit->filename) >= 0)
549 fast_load = 0;
552 * VFS may report file size incorrectly, and slow load is not a big
553 * deal considering overhead in VFS.
555 if (!vfs_file_is_local (edit->filename))
556 fast_load = 0;
559 * FIXME: line end translation should disable fast loading as well
560 * Consider doing fseek() to the end and ftell() for the real size.
563 if (*edit->filename) {
564 /* If we are dealing with a real file, check that it exists */
565 if (check_file_access (edit, edit->filename, &edit->stat1))
566 return 1;
567 } else {
568 /* nothing to load */
569 fast_load = 0;
572 edit_init_buffers (edit);
574 if (fast_load) {
575 edit->last_byte = edit->stat1.st_size;
576 edit_load_file_fast (edit, edit->filename);
577 /* If fast load was used, the number of lines wasn't calculated */
578 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
579 } else {
580 #ifdef HAVE_CHARSET
581 const char *codepage_id;
582 #endif
583 edit->last_byte = 0;
584 if (*edit->filename) {
585 edit->stack_disable = 1;
586 if (!edit_insert_file (edit, edit->filename)) {
587 edit_clean (edit);
588 return 1;
590 edit->stack_disable = 0;
593 #ifdef HAVE_CHARSET
594 codepage_id = get_codepage_id( source_codepage );
595 if ( codepage_id )
596 edit->utf8 = str_isutf8 ( codepage_id );
597 #endif
599 return 0;
602 /* Restore saved cursor position in the file */
603 static void
604 edit_load_position (WEdit *edit)
606 char *filename;
607 long line, column;
609 if (!edit->filename || !*edit->filename)
610 return;
612 filename = vfs_canon (edit->filename);
613 load_file_position (filename, &line, &column);
614 g_free (filename);
616 edit_move_to_line (edit, line - 1);
617 edit->prev_col = column;
618 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
619 edit_move_display (edit, line - (edit->num_widget_lines / 2));
622 /* Save cursor position in the file */
623 static void
624 edit_save_position (WEdit *edit)
626 char *filename;
628 if (!edit->filename || !*edit->filename)
629 return;
631 filename = vfs_canon (edit->filename);
632 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
633 g_free (filename);
636 /* Clean the WEdit stricture except the widget part */
637 static inline void
638 edit_purge_widget (WEdit *edit)
640 int len = sizeof (WEdit) - sizeof (Widget);
641 char *start = (char *) edit + sizeof (Widget);
642 memset (start, 0, len);
643 edit->macro_i = -1; /* not recording a macro */
646 #define space_width 1
649 * Fill in the edit structure. Return NULL on failure. Pass edit as
650 * NULL to allocate a new structure.
652 * If line is 0, try to restore saved position. Otherwise put the
653 * cursor on that line and show it in the middle of the screen.
655 WEdit *
656 edit_init (WEdit *edit, int lines, int columns, const char *filename,
657 long line)
659 int to_free = 0;
660 option_auto_syntax = 1; /* Resetting to auto on every invokation */
661 if ( option_line_state ) {
662 option_line_state_width = LINE_STATE_WIDTH;
663 } else {
664 option_line_state_width = 0;
666 if (!edit) {
667 #ifdef ENABLE_NLS
669 * Expand option_whole_chars_search by national letters using
670 * current locale
673 static char option_whole_chars_search_buf[256];
675 if (option_whole_chars_search_buf != option_whole_chars_search) {
676 size_t i;
677 size_t len = str_term_width1 (option_whole_chars_search);
679 strcpy (option_whole_chars_search_buf,
680 option_whole_chars_search);
682 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
683 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
684 option_whole_chars_search_buf[len++] = i;
688 option_whole_chars_search_buf[len] = 0;
689 option_whole_chars_search = option_whole_chars_search_buf;
691 #endif /* ENABLE_NLS */
692 edit = g_malloc0 (sizeof (WEdit));
693 edit->search = NULL;
694 to_free = 1;
696 edit_purge_widget (edit);
697 edit->num_widget_lines = lines;
698 edit->over_col = 0;
699 edit->num_widget_columns = columns;
700 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
701 edit->stat1.st_uid = getuid ();
702 edit->stat1.st_gid = getgid ();
703 edit->stat1.st_mtime = 0;
704 edit->bracket = -1;
705 edit->force |= REDRAW_PAGE;
706 edit_set_filename (edit, filename);
707 edit->stack_size = START_STACK_SIZE;
708 edit->stack_size_mask = START_STACK_SIZE - 1;
709 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
710 if (edit_load_file (edit)) {
711 /* edit_load_file already gives an error message */
712 if (to_free)
713 g_free (edit);
714 return 0;
716 edit->utf8 = 0;
717 edit->converter = str_cnv_from_term;
718 #ifdef HAVE_CHARSET
719 const char *cp_id = NULL;
720 cp_id = get_codepage_id (source_codepage >= 0 ?
721 source_codepage : display_codepage);
723 if (cp_id != NULL) {
724 GIConv conv;
725 conv = str_crt_conv_from (cp_id);
726 if (conv != INVALID_CONV) {
727 if (edit->converter != str_cnv_from_term)
728 str_close_conv (edit->converter);
729 edit->converter = conv;
732 if (cp_id != NULL)
733 edit->utf8 = str_isutf8 (cp_id);
734 #endif
736 edit->loading_done = 1;
737 edit->modified = 0;
738 edit->locked = 0;
739 edit_load_syntax (edit, 0, 0);
741 int color;
742 edit_get_syntax_color (edit, -1, &color);
745 /* load saved cursor position */
746 if ((line == 0) && option_save_position) {
747 edit_load_position (edit);
748 } else {
749 if (line <= 0)
750 line = 1;
751 edit_move_display (edit, line - 1);
752 edit_move_to_line (edit, line - 1);
755 edit_load_user_map(edit);
757 return edit;
760 /* Clear the edit struct, freeing everything in it. Return 1 on success */
762 edit_clean (WEdit *edit)
764 int j = 0;
766 if (!edit)
767 return 0;
769 /* a stale lock, remove it */
770 if (edit->locked)
771 edit->locked = edit_unlock_file (edit->filename);
773 /* save cursor position */
774 if (option_save_position)
775 edit_save_position (edit);
777 /* File specified on the mcedit command line and never saved */
778 if (edit->delete_file)
779 unlink (edit->filename);
781 edit_free_syntax_rules (edit);
782 book_mark_flush (edit, -1);
783 for (; j <= MAXBUFF; j++) {
784 g_free (edit->buffers1[j]);
785 g_free (edit->buffers2[j]);
788 g_free (edit->undo_stack);
789 g_free (edit->filename);
790 g_free (edit->dir);
792 if (edit->search)
794 mc_search_free(edit->search);
795 edit->search = NULL;
797 edit_purge_widget (edit);
799 return 1;
803 /* returns 1 on success */
804 int edit_renew (WEdit * edit)
806 int lines = edit->num_widget_lines;
807 int columns = edit->num_widget_columns;
808 int retval = 1;
810 edit_clean (edit);
811 if (!edit_init (edit, lines, columns, "", 0))
812 retval = 0;
813 return retval;
817 * Load a new file into the editor. If it fails, preserve the old file.
818 * To do it, allocate a new widget, initialize it and, if the new file
819 * was loaded, copy the data to the old widget.
820 * Return 1 on success, 0 on failure.
823 edit_reload (WEdit *edit, const char *filename)
825 WEdit *e;
826 int lines = edit->num_widget_lines;
827 int columns = edit->num_widget_columns;
829 e = g_malloc0 (sizeof (WEdit));
830 e->widget = edit->widget;
831 if (!edit_init (e, lines, columns, filename, 0)) {
832 g_free (e);
833 return 0;
835 edit_clean (edit);
836 memcpy (edit, e, sizeof (WEdit));
837 g_free (e);
838 return 1;
842 * Load a new file into the editor and set line. If it fails, preserve the old file.
843 * To do it, allocate a new widget, initialize it and, if the new file
844 * was loaded, copy the data to the old widget.
845 * Return 1 on success, 0 on failure.
848 edit_reload_line (WEdit *edit, const char *filename, long line)
850 WEdit *e;
851 int lines = edit->num_widget_lines;
852 int columns = edit->num_widget_columns;
854 e = g_malloc0 (sizeof (WEdit));
855 e->widget = edit->widget;
856 if (!edit_init (e, lines, columns, filename, line)) {
857 g_free (e);
858 return 0;
860 edit_clean (edit);
861 memcpy (edit, e, sizeof (WEdit));
862 g_free (e);
863 return 1;
868 Recording stack for undo:
869 The following is an implementation of a compressed stack. Identical
870 pushes are recorded by a negative prefix indicating the number of times the
871 same char was pushed. This saves space for repeated curs-left or curs-right
872 delete etc.
876 pushed: stored:
880 b -3
882 c --> -4
888 If the stack long int is 0-255 it represents a normal insert (from a backspace),
889 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
890 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
891 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
892 position.
894 The only way the cursor moves or the buffer is changed is through the routines:
895 insert, backspace, insert_ahead, delete, and cursor_move.
896 These record the reverse undo movements onto the stack each time they are
897 called.
899 Each key press results in a set of actions (insert; delete ...). So each time
900 a key is pressed the current position of start_display is pushed as
901 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
902 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
903 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
907 void edit_push_action (WEdit * edit, long c,...)
909 unsigned long sp = edit->stack_pointer;
910 unsigned long spm1;
911 long *t;
913 /* first enlarge the stack if necessary */
914 if (sp > edit->stack_size - 10) { /* say */
915 if (option_max_undo < 256)
916 option_max_undo = 256;
917 if (edit->stack_size < (unsigned long) option_max_undo) {
918 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
919 if (t) {
920 edit->undo_stack = t;
921 edit->stack_size <<= 1;
922 edit->stack_size_mask = edit->stack_size - 1;
926 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
927 if (edit->stack_disable)
928 return;
930 #ifdef FAST_MOVE_CURSOR
931 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
932 va_list ap;
933 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
934 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
935 va_start (ap, c);
936 c = -(va_arg (ap, int));
937 va_end (ap);
938 } else
939 #endif /* ! FAST_MOVE_CURSOR */
940 if (edit->stack_bottom != sp
941 && spm1 != edit->stack_bottom
942 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
943 int d;
944 if (edit->undo_stack[spm1] < 0) {
945 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
946 if (d == c) {
947 if (edit->undo_stack[spm1] > -1000000000) {
948 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
949 edit->undo_stack[spm1]--;
950 return;
953 /* #define NO_STACK_CURSMOVE_ANIHILATION */
954 #ifndef NO_STACK_CURSMOVE_ANIHILATION
955 else if ((c == CURS_LEFT && d == CURS_RIGHT)
956 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
957 if (edit->undo_stack[spm1] == -2)
958 edit->stack_pointer = spm1;
959 else
960 edit->undo_stack[spm1]++;
961 return;
963 #endif
964 } else {
965 d = edit->undo_stack[spm1];
966 if (d == c) {
967 if (c >= KEY_PRESS)
968 return; /* --> no need to push multiple do-nothings */
969 edit->undo_stack[sp] = -2;
970 goto check_bottom;
972 #ifndef NO_STACK_CURSMOVE_ANIHILATION
973 else if ((c == CURS_LEFT && d == CURS_RIGHT)
974 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
975 edit->stack_pointer = spm1;
976 return;
978 #endif
981 edit->undo_stack[sp] = c;
982 check_bottom:
984 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
986 /* if the sp wraps round and catches the stack_bottom then erase
987 * the first set of actions on the stack to make space - by moving
988 * stack_bottom forward one "key press" */
989 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
990 if ((unsigned long) c == edit->stack_bottom ||
991 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
992 do {
993 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
994 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
996 /*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: */
997 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
998 edit->stack_bottom = edit->stack_pointer = 0;
1002 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1003 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1005 static long
1006 pop_action (WEdit * edit)
1008 long c;
1009 unsigned long sp = edit->stack_pointer;
1010 if (sp == edit->stack_bottom) {
1011 return STACK_BOTTOM;
1013 sp = (sp - 1) & edit->stack_size_mask;
1014 if ((c = edit->undo_stack[sp]) >= 0) {
1015 /* edit->undo_stack[sp] = '@'; */
1016 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1017 return c;
1019 if (sp == edit->stack_bottom) {
1020 return STACK_BOTTOM;
1022 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1023 if (edit->undo_stack[sp] == -2) {
1024 /* edit->undo_stack[sp] = '@'; */
1025 edit->stack_pointer = sp;
1026 } else
1027 edit->undo_stack[sp]++;
1029 return c;
1032 /* is called whenever a modification is made by one of the four routines below */
1033 static inline void edit_modification (WEdit * edit)
1035 edit->caches_valid = 0;
1036 edit->screen_modified = 1;
1038 /* raise lock when file modified */
1039 if (!edit->modified && !edit->delete_file)
1040 edit->locked = edit_lock_file (edit->filename);
1041 edit->modified = 1;
1045 Basic low level single character buffer alterations and movements at the cursor.
1046 Returns char passed over, inserted or removed.
1049 void
1050 edit_insert (WEdit *edit, int c)
1052 /* check if file has grown to large */
1053 if (edit->last_byte >= SIZE_LIMIT)
1054 return;
1056 /* first we must update the position of the display window */
1057 if (edit->curs1 < edit->start_display) {
1058 edit->start_display++;
1059 if (c == '\n')
1060 edit->start_line++;
1063 /* Mark file as modified, unless the file hasn't been fully loaded */
1064 if (edit->loading_done) {
1065 edit_modification (edit);
1068 /* now we must update some info on the file and check if a redraw is required */
1069 if (c == '\n') {
1070 if (edit->book_mark)
1071 book_mark_inc (edit, edit->curs_line);
1072 edit->curs_line++;
1073 edit->total_lines++;
1074 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1077 /* save the reverse command onto the undo stack */
1078 edit_push_action (edit, BACKSPACE);
1080 /* update markers */
1081 edit->mark1 += (edit->mark1 > edit->curs1);
1082 edit->mark2 += (edit->mark2 > edit->curs1);
1083 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1085 /* add a new buffer if we've reached the end of the last one */
1086 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1087 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1088 g_malloc (EDIT_BUF_SIZE);
1090 /* perform the insertion */
1091 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1092 curs1 & M_EDIT_BUF_SIZE]
1093 = (unsigned char) c;
1095 /* update file length */
1096 edit->last_byte++;
1098 /* update cursor position */
1099 edit->curs1++;
1102 void
1103 edit_insert_over (WEdit * edit)
1105 int i;
1107 for ( i = 0; i < edit->over_col; i++ ) {
1108 edit_insert (edit, ' ');
1110 edit->over_col = 0;
1113 /* same as edit_insert and move left */
1114 void edit_insert_ahead (WEdit * edit, int c)
1116 if (edit->last_byte >= SIZE_LIMIT)
1117 return;
1118 if (edit->curs1 < edit->start_display) {
1119 edit->start_display++;
1120 if (c == '\n')
1121 edit->start_line++;
1123 edit_modification (edit);
1124 if (c == '\n') {
1125 if (edit->book_mark)
1126 book_mark_inc (edit, edit->curs_line);
1127 edit->total_lines++;
1128 edit->force |= REDRAW_AFTER_CURSOR;
1130 edit_push_action (edit, DELCHAR);
1132 edit->mark1 += (edit->mark1 >= edit->curs1);
1133 edit->mark2 += (edit->mark2 >= edit->curs1);
1134 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1136 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1137 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1138 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1140 edit->last_byte++;
1141 edit->curs2++;
1145 int edit_delete (WEdit * edit, const int byte_delete)
1147 int p = 0;
1148 int cw = 1;
1149 int i;
1151 if (!edit->curs2)
1152 return 0;
1154 edit->mark1 -= (edit->mark1 > edit->curs1);
1155 edit->mark2 -= (edit->mark2 > edit->curs1);
1156 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1158 cw = 1;
1159 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1160 if ( edit->utf8 && byte_delete == 0 ) {
1161 edit_get_utf (edit, edit->curs1, &cw);
1162 if ( cw < 1 )
1163 cw = 1;
1165 for ( i = 1; i<= cw; i++ ) {
1166 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1168 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1169 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1170 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1172 edit->last_byte--;
1173 edit->curs2--;
1174 edit_push_action (edit, p + 256);
1177 edit_modification (edit);
1178 if (p == '\n') {
1179 if (edit->book_mark)
1180 book_mark_dec (edit, edit->curs_line);
1181 edit->total_lines--;
1182 edit->force |= REDRAW_AFTER_CURSOR;
1184 if (edit->curs1 < edit->start_display) {
1185 edit->start_display--;
1186 if (p == '\n')
1187 edit->start_line--;
1190 return p;
1194 static int
1195 edit_backspace (WEdit * edit, const int byte_delete)
1197 int p = 0;
1198 int cw = 1;
1199 int i;
1201 if (!edit->curs1)
1202 return 0;
1204 edit->mark1 -= (edit->mark1 >= edit->curs1);
1205 edit->mark2 -= (edit->mark2 >= edit->curs1);
1206 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1208 cw = 1;
1209 if ( edit->utf8 && byte_delete == 0 ) {
1210 edit_get_prev_utf (edit, edit->curs1, &cw);
1211 if ( cw < 1 )
1212 cw = 1;
1214 for ( i = 1; i<= cw; i++ ) {
1215 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1216 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1217 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1218 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1220 edit->last_byte--;
1221 edit->curs1--;
1222 edit_push_action (edit, p);
1224 edit_modification (edit);
1225 if (p == '\n') {
1226 if (edit->book_mark)
1227 book_mark_dec (edit, edit->curs_line);
1228 edit->curs_line--;
1229 edit->total_lines--;
1230 edit->force |= REDRAW_AFTER_CURSOR;
1233 if (edit->curs1 < edit->start_display) {
1234 edit->start_display--;
1235 if (p == '\n')
1236 edit->start_line--;
1239 return p;
1242 #ifdef FAST_MOVE_CURSOR
1244 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1246 unsigned long next;
1247 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1248 edit->curs_line--;
1249 next -= (unsigned long) dest;
1250 n -= next;
1251 src += next;
1252 dest += next;
1257 edit_move_backward_lots (WEdit *edit, long increment)
1259 int r, s, t;
1260 unsigned char *p;
1262 if (increment > edit->curs1)
1263 increment = edit->curs1;
1264 if (increment <= 0)
1265 return -1;
1266 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1268 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1269 if (r > increment)
1270 r = increment;
1271 s = edit->curs1 & M_EDIT_BUF_SIZE;
1273 p = 0;
1274 if (s > r) {
1275 memqcpy (edit,
1276 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1277 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1279 } else {
1280 if (s) {
1281 memqcpy (edit,
1282 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1283 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1284 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1285 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1287 memqcpy (edit,
1288 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1289 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1290 EDIT_BUF_SIZE - (r - s), r - s);
1292 increment -= r;
1293 edit->curs1 -= r;
1294 edit->curs2 += r;
1295 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1296 if (p)
1297 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1298 else
1299 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1300 g_malloc (EDIT_BUF_SIZE);
1301 } else {
1302 g_free (p);
1305 s = edit->curs1 & M_EDIT_BUF_SIZE;
1306 while (increment) {
1307 p = 0;
1308 r = EDIT_BUF_SIZE;
1309 if (r > increment)
1310 r = increment;
1311 t = s;
1312 if (r < t)
1313 t = r;
1314 memqcpy (edit,
1315 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1316 EDIT_BUF_SIZE - t,
1317 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1319 if (r >= s) {
1320 if (t) {
1321 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1322 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1324 memqcpy (edit,
1325 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1326 EDIT_BUF_SIZE - r,
1327 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1328 EDIT_BUF_SIZE - (r - s), r - s);
1330 increment -= r;
1331 edit->curs1 -= r;
1332 edit->curs2 += r;
1333 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1334 if (p)
1335 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1336 else
1337 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1338 g_malloc (EDIT_BUF_SIZE);
1339 } else {
1340 g_free (p);
1343 return edit_get_byte (edit, edit->curs1);
1346 #endif /* ! FAST_MOVE_CURSOR */
1348 /* moves the cursor right or left: increment positive or negative respectively */
1349 void edit_cursor_move (WEdit * edit, long increment)
1351 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1352 int c;
1354 #ifdef FAST_MOVE_CURSOR
1355 if (increment < -256) {
1356 edit->force |= REDRAW_PAGE;
1357 edit_move_backward_lots (edit, -increment);
1358 return;
1360 #endif /* ! FAST_MOVE_CURSOR */
1362 if (increment < 0) {
1363 for (; increment < 0; increment++) {
1364 if (!edit->curs1)
1365 return;
1367 edit_push_action (edit, CURS_RIGHT);
1369 c = edit_get_byte (edit, edit->curs1 - 1);
1370 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1371 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1372 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1373 edit->curs2++;
1374 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1375 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1376 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1377 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1379 edit->curs1--;
1380 if (c == '\n') {
1381 edit->curs_line--;
1382 edit->force |= REDRAW_LINE_BELOW;
1386 } else if (increment > 0) {
1387 for (; increment > 0; increment--) {
1388 if (!edit->curs2)
1389 return;
1391 edit_push_action (edit, CURS_LEFT);
1393 c = edit_get_byte (edit, edit->curs1);
1394 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1395 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1396 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1397 edit->curs1++;
1398 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1399 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1400 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1401 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1403 edit->curs2--;
1404 if (c == '\n') {
1405 edit->curs_line++;
1406 edit->force |= REDRAW_LINE_ABOVE;
1412 /* These functions return positions relative to lines */
1414 /* returns index of last char on line + 1 */
1415 long edit_eol (WEdit * edit, long current)
1417 if (current < edit->last_byte) {
1418 for (;; current++)
1419 if (edit_get_byte (edit, current) == '\n')
1420 break;
1421 } else
1422 return edit->last_byte;
1423 return current;
1426 /* returns index of first char on line */
1427 long edit_bol (WEdit * edit, long current)
1429 if (current > 0) {
1430 for (;; current--)
1431 if (edit_get_byte (edit, current - 1) == '\n')
1432 break;
1433 } else
1434 return 0;
1435 return current;
1439 int edit_count_lines (WEdit * edit, long current, int upto)
1441 int lines = 0;
1442 if (upto > edit->last_byte)
1443 upto = edit->last_byte;
1444 if (current < 0)
1445 current = 0;
1446 while (current < upto)
1447 if (edit_get_byte (edit, current++) == '\n')
1448 lines++;
1449 return lines;
1453 /* If lines is zero this returns the count of lines from current to upto. */
1454 /* If upto is zero returns index of lines forward current. */
1455 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1457 if (upto) {
1458 return edit_count_lines (edit, current, upto);
1459 } else {
1460 int next;
1461 if (lines < 0)
1462 lines = 0;
1463 while (lines--) {
1464 next = edit_eol (edit, current) + 1;
1465 if (next > edit->last_byte)
1466 break;
1467 else
1468 current = next;
1470 return current;
1475 /* Returns offset of 'lines' lines up from current */
1476 long edit_move_backward (WEdit * edit, long current, int lines)
1478 if (lines < 0)
1479 lines = 0;
1480 current = edit_bol (edit, current);
1481 while((lines--) && current != 0)
1482 current = edit_bol (edit, current - 1);
1483 return current;
1486 /* If cols is zero this returns the count of columns from current to upto. */
1487 /* If upto is zero returns index of cols across from current. */
1488 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1490 long p, q;
1491 int col = 0;
1492 #ifdef HAVE_CHARSET
1493 int cw = 1;
1494 int utf_ch = 0;
1495 #endif
1496 if (upto) {
1497 q = upto;
1498 cols = -10;
1499 } else
1500 q = edit->last_byte + 2;
1501 for (col = 0, p = current; p < q; p++) {
1502 int c;
1503 #ifdef HAVE_CHARSET
1504 cw = 1;
1505 utf_ch = 0;
1506 #endif
1507 if (cols != -10) {
1508 if (col == cols)
1509 return p;
1510 if (col > cols)
1511 return p - 1;
1513 #ifdef HAVE_CHARSET
1514 if ( !edit->utf8 ) {
1515 #endif
1516 c = edit_get_byte (edit, p);
1517 #ifdef HAVE_CHARSET
1518 } else {
1519 cw = 1;
1520 c = edit_get_byte (edit, p);
1521 utf_ch = edit_get_utf (edit, p, &cw);
1523 if ( utf8_display ) {
1524 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1525 col++;
1527 #endif
1528 if (c == '\t')
1529 col += TAB_SIZE - col % TAB_SIZE;
1530 else if (c == '\n') {
1531 if (upto)
1532 return col;
1533 else
1534 return p;
1535 } else if (c < 32 || c == 127)
1536 col += 2; /* Caret notation for control characters */
1537 else
1538 col++;
1539 #ifdef HAVE_CHARSET
1540 if ( cw > 1 )
1541 col -= cw-1;
1542 #endif
1544 return col;
1547 /* returns the current column position of the cursor */
1548 int edit_get_col (WEdit * edit)
1550 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1554 /* Scrolling functions */
1556 void edit_update_curs_row (WEdit * edit)
1558 edit->curs_row = edit->curs_line - edit->start_line;
1561 void edit_update_curs_col (WEdit * edit)
1563 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1567 edit_get_curs_col (const WEdit *edit)
1569 return edit->curs_col;
1572 /*moves the display start position up by i lines */
1573 void edit_scroll_upward (WEdit * edit, unsigned long i)
1575 unsigned long lines_above = edit->start_line;
1576 if (i > lines_above)
1577 i = lines_above;
1578 if (i) {
1579 edit->start_line -= i;
1580 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1581 edit->force |= REDRAW_PAGE;
1582 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1584 edit_update_curs_row (edit);
1588 /* returns 1 if could scroll, 0 otherwise */
1589 void edit_scroll_downward (WEdit * edit, int i)
1591 int lines_below;
1592 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1593 if (lines_below > 0) {
1594 if (i > lines_below)
1595 i = lines_below;
1596 edit->start_line += i;
1597 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1598 edit->force |= REDRAW_PAGE;
1599 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1601 edit_update_curs_row (edit);
1604 void edit_scroll_right (WEdit * edit, int i)
1606 edit->force |= REDRAW_PAGE;
1607 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1608 edit->start_col -= i;
1611 void edit_scroll_left (WEdit * edit, int i)
1613 if (edit->start_col) {
1614 edit->start_col += i;
1615 if (edit->start_col > 0)
1616 edit->start_col = 0;
1617 edit->force |= REDRAW_PAGE;
1618 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1622 /* high level cursor movement commands */
1624 static int is_in_indent (WEdit *edit)
1626 long p = edit_bol (edit, edit->curs1);
1627 while (p < edit->curs1)
1628 if (!strchr (" \t", edit_get_byte (edit, p++)))
1629 return 0;
1630 return 1;
1633 static int left_of_four_spaces (WEdit *edit);
1635 void
1636 edit_move_to_prev_col (WEdit * edit, long p)
1638 int prev = edit->prev_col;
1639 int over = edit->over_col;
1641 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1643 if (option_cursor_beyond_eol) {
1644 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1646 if (line_len < prev + edit->over_col) {
1647 edit->over_col = prev + over - line_len;
1648 edit->prev_col = line_len;
1649 edit->curs_col = line_len;
1650 } else {
1651 edit->curs_col = prev + over;
1652 edit->prev_col = edit->curs_col;
1653 edit->over_col = 0;
1655 } else {
1656 if (is_in_indent (edit) && option_fake_half_tabs) {
1657 edit_update_curs_col (edit);
1658 if (space_width)
1659 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1660 int q = edit->curs_col;
1661 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1662 p = edit_bol (edit, edit->curs1);
1663 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1664 if (!left_of_four_spaces (edit))
1665 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1671 /* move i lines */
1672 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1674 unsigned long p, l = edit->curs_line;
1676 if (i > l)
1677 i = l;
1678 if (i) {
1679 if (i > 1)
1680 edit->force |= REDRAW_PAGE;
1681 if (scroll)
1682 edit_scroll_upward (edit, i);
1684 p = edit_bol (edit, edit->curs1);
1685 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1686 edit_move_to_prev_col (edit, p);
1688 edit->search_start = edit->curs1;
1689 edit->found_len = 0;
1693 static int
1694 is_blank (WEdit *edit, long offset)
1696 long s, f;
1697 int c;
1698 s = edit_bol (edit, offset);
1699 f = edit_eol (edit, offset) - 1;
1700 while (s <= f) {
1701 c = edit_get_byte (edit, s++);
1702 if (!isspace (c))
1703 return 0;
1705 return 1;
1709 /* returns the offset of line i */
1710 static long
1711 edit_find_line (WEdit *edit, int line)
1713 int i, j = 0;
1714 int m = 2000000000;
1715 if (!edit->caches_valid) {
1716 for (i = 0; i < N_LINE_CACHES; i++)
1717 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1718 /* three offsets that we *know* are line 0 at 0 and these two: */
1719 edit->line_numbers[1] = edit->curs_line;
1720 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1721 edit->line_numbers[2] = edit->total_lines;
1722 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1723 edit->caches_valid = 1;
1725 if (line >= edit->total_lines)
1726 return edit->line_offsets[2];
1727 if (line <= 0)
1728 return 0;
1729 /* find the closest known point */
1730 for (i = 0; i < N_LINE_CACHES; i++) {
1731 int n;
1732 n = abs (edit->line_numbers[i] - line);
1733 if (n < m) {
1734 m = n;
1735 j = i;
1738 if (m == 0)
1739 return edit->line_offsets[j]; /* know the offset exactly */
1740 if (m == 1 && j >= 3)
1741 i = j; /* one line different - caller might be looping, so stay in this cache */
1742 else
1743 i = 3 + (rand () % (N_LINE_CACHES - 3));
1744 if (line > edit->line_numbers[j])
1745 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1746 else
1747 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1748 edit->line_numbers[i] = line;
1749 return edit->line_offsets[i];
1752 int line_is_blank (WEdit * edit, long line)
1754 return is_blank (edit, edit_find_line (edit, line));
1757 /* moves up until a blank line is reached, or until just
1758 before a non-blank line is reached */
1759 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1761 int i;
1762 if (edit->curs_line <= 1) {
1763 i = 0;
1764 } else {
1765 if (line_is_blank (edit, edit->curs_line)) {
1766 if (line_is_blank (edit, edit->curs_line - 1)) {
1767 for (i = edit->curs_line - 1; i; i--)
1768 if (!line_is_blank (edit, i)) {
1769 i++;
1770 break;
1772 } else {
1773 for (i = edit->curs_line - 1; i; i--)
1774 if (line_is_blank (edit, i))
1775 break;
1777 } else {
1778 for (i = edit->curs_line - 1; i; i--)
1779 if (line_is_blank (edit, i))
1780 break;
1783 edit_move_up (edit, edit->curs_line - i, scroll);
1786 /* move i lines */
1787 void edit_move_down (WEdit * edit, int i, int scroll)
1789 long p, l = edit->total_lines - edit->curs_line;
1791 if (i > l)
1792 i = l;
1793 if (i) {
1794 if (i > 1)
1795 edit->force |= REDRAW_PAGE;
1796 if (scroll)
1797 edit_scroll_downward (edit, i);
1798 p = edit_bol (edit, edit->curs1);
1799 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1800 edit_move_to_prev_col (edit, p);
1802 edit->search_start = edit->curs1;
1803 edit->found_len = 0;
1807 /* moves down until a blank line is reached, or until just
1808 before a non-blank line is reached */
1809 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1811 int i;
1812 if (edit->curs_line >= edit->total_lines - 1) {
1813 i = edit->total_lines;
1814 } else {
1815 if (line_is_blank (edit, edit->curs_line)) {
1816 if (line_is_blank (edit, edit->curs_line + 1)) {
1817 for (i = edit->curs_line + 1; i; i++)
1818 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1819 i--;
1820 break;
1822 } else {
1823 for (i = edit->curs_line + 1; i; i++)
1824 if (line_is_blank (edit, i) || i >= edit->total_lines)
1825 break;
1827 } else {
1828 for (i = edit->curs_line + 1; i; i++)
1829 if (line_is_blank (edit, i) || i >= edit->total_lines)
1830 break;
1833 edit_move_down (edit, i - edit->curs_line, scroll);
1836 static void edit_begin_page (WEdit *edit)
1838 edit_update_curs_row (edit);
1839 edit_move_up (edit, edit->curs_row, 0);
1842 static void edit_end_page (WEdit *edit)
1844 edit_update_curs_row (edit);
1845 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1849 /* goto beginning of text */
1850 static void edit_move_to_top (WEdit * edit)
1852 if (edit->curs_line) {
1853 edit_cursor_move (edit, -edit->curs1);
1854 edit_move_to_prev_col (edit, 0);
1855 edit->force |= REDRAW_PAGE;
1856 edit->search_start = 0;
1857 edit_update_curs_row(edit);
1862 /* goto end of text */
1863 static void edit_move_to_bottom (WEdit * edit)
1865 if (edit->curs_line < edit->total_lines) {
1866 edit_cursor_move (edit, edit->curs2);
1867 edit->start_display = edit->last_byte;
1868 edit->start_line = edit->total_lines;
1869 edit_update_curs_row(edit);
1870 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1871 edit->force |= REDRAW_PAGE;
1875 /* goto beginning of line */
1876 static void edit_cursor_to_bol (WEdit * edit)
1878 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1879 edit->search_start = edit->curs1;
1880 edit->prev_col = edit_get_col (edit);
1881 edit->over_col = 0;
1884 /* goto end of line */
1885 static void edit_cursor_to_eol (WEdit * edit)
1887 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1888 edit->search_start = edit->curs1;
1889 edit->prev_col = edit_get_col (edit);
1890 edit->over_col = 0;
1893 /* move cursor to line 'line' */
1894 void edit_move_to_line (WEdit * e, long line)
1896 if(line < e->curs_line)
1897 edit_move_up (e, e->curs_line - line, 0);
1898 else
1899 edit_move_down (e, line - e->curs_line, 0);
1900 edit_scroll_screen_over_cursor (e);
1903 /* scroll window so that first visible line is 'line' */
1904 void edit_move_display (WEdit * e, long line)
1906 if(line < e->start_line)
1907 edit_scroll_upward (e, e->start_line - line);
1908 else
1909 edit_scroll_downward (e, line - e->start_line);
1912 /* save markers onto undo stack */
1913 void edit_push_markers (WEdit * edit)
1915 edit_push_action (edit, MARK_1 + edit->mark1);
1916 edit_push_action (edit, MARK_2 + edit->mark2);
1919 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1921 edit->mark1 = m1;
1922 edit->mark2 = m2;
1923 edit->column1 = c1;
1924 edit->column2 = c2;
1928 /* highlight marker toggle */
1929 void edit_mark_cmd (WEdit * edit, int unmark)
1931 edit_push_markers (edit);
1932 if (unmark) {
1933 edit_set_markers (edit, 0, 0, 0, 0);
1934 edit->force |= REDRAW_PAGE;
1935 } else {
1936 if (edit->mark2 >= 0) {
1937 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
1938 edit->force |= REDRAW_PAGE;
1939 } else
1940 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
1944 static unsigned long
1945 my_type_of (int c)
1947 int x, r = 0;
1948 const char *p, *q;
1949 const char option_chars_move_whole_word[] =
1950 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1952 if (!c)
1953 return 0;
1954 if (c == '!') {
1955 if (*option_chars_move_whole_word == '!')
1956 return 2;
1957 return 0x80000000UL;
1959 if (g_ascii_isupper ((gchar) c))
1960 c = 'A';
1961 else if (g_ascii_islower ((gchar) c))
1962 c = 'a';
1963 else if (g_ascii_isalpha (c))
1964 c = 'a';
1965 else if (isdigit (c))
1966 c = '0';
1967 else if (isspace (c))
1968 c = ' ';
1969 q = strchr (option_chars_move_whole_word, c);
1970 if (!q)
1971 return 0xFFFFFFFFUL;
1972 do {
1973 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
1974 if (*p == '!')
1975 x <<= 1;
1976 r |= x;
1977 } while ((q = strchr (q + 1, c)));
1978 return r;
1981 static void
1982 edit_left_word_move (WEdit *edit, int s)
1984 for (;;) {
1985 int c1, c2;
1986 edit_cursor_move (edit, -1);
1987 if (!edit->curs1)
1988 break;
1989 c1 = edit_get_byte (edit, edit->curs1 - 1);
1990 c2 = edit_get_byte (edit, edit->curs1);
1991 if (!(my_type_of (c1) & my_type_of (c2)))
1992 break;
1993 if (isspace (c1) && !isspace (c2))
1994 break;
1995 if (s)
1996 if (!isspace (c1) && isspace (c2))
1997 break;
2001 static void edit_left_word_move_cmd (WEdit * edit)
2003 edit_left_word_move (edit, 0);
2004 edit->force |= REDRAW_PAGE;
2007 static void
2008 edit_right_word_move (WEdit *edit, int s)
2010 for (;;) {
2011 int c1, c2;
2012 edit_cursor_move (edit, 1);
2013 if (edit->curs1 >= edit->last_byte)
2014 break;
2015 c1 = edit_get_byte (edit, edit->curs1 - 1);
2016 c2 = edit_get_byte (edit, edit->curs1);
2017 if (!(my_type_of (c1) & my_type_of (c2)))
2018 break;
2019 if (isspace (c1) && !isspace (c2))
2020 break;
2021 if (s)
2022 if (!isspace (c1) && isspace (c2))
2023 break;
2027 static void edit_right_word_move_cmd (WEdit * edit)
2029 edit_right_word_move (edit, 0);
2030 edit->force |= REDRAW_PAGE;
2033 static void edit_right_char_move_cmd (WEdit * edit)
2035 int cw = 1;
2036 int c = 0;
2037 if ( edit->utf8 ) {
2038 c = edit_get_utf (edit, edit->curs1, &cw);
2039 if ( cw < 1 )
2040 cw = 1;
2041 } else {
2042 c = edit_get_byte (edit, edit->curs1);
2044 if (option_cursor_beyond_eol && c == '\n') {
2045 edit->over_col++;
2046 } else {
2047 edit_cursor_move (edit, cw);
2051 static void edit_left_char_move_cmd (WEdit * edit)
2053 int cw = 1;
2054 if ( edit->utf8 ) {
2055 edit_get_prev_utf (edit, edit->curs1, &cw);
2056 if ( cw < 1 )
2057 cw = 1;
2059 if (option_cursor_beyond_eol && edit->over_col > 0) {
2060 edit->over_col--;
2061 } else {
2062 edit_cursor_move (edit, -cw);
2067 static void edit_right_delete_word (WEdit * edit)
2069 int c1, c2;
2070 for (;;) {
2071 if (edit->curs1 >= edit->last_byte)
2072 break;
2073 c1 = edit_delete (edit, 1);
2074 c2 = edit_get_byte (edit, edit->curs1);
2075 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2076 break;
2077 if (!(my_type_of (c1) & my_type_of (c2)))
2078 break;
2082 static void edit_left_delete_word (WEdit * edit)
2084 int c1, c2;
2085 for (;;) {
2086 if (edit->curs1 <= 0)
2087 break;
2088 c1 = edit_backspace (edit, 1);
2089 c2 = edit_get_byte (edit, edit->curs1 - 1);
2090 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2091 break;
2092 if (!(my_type_of (c1) & my_type_of (c2)))
2093 break;
2098 the start column position is not recorded, and hence does not
2099 undo as it happed. But who would notice.
2101 static void
2102 edit_do_undo (WEdit * edit)
2104 long ac;
2105 long count = 0;
2107 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2109 while ((ac = pop_action (edit)) < KEY_PRESS) {
2110 switch ((int) ac) {
2111 case STACK_BOTTOM:
2112 goto done_undo;
2113 case CURS_RIGHT:
2114 edit_cursor_move (edit, 1);
2115 break;
2116 case CURS_LEFT:
2117 edit_cursor_move (edit, -1);
2118 break;
2119 case BACKSPACE:
2120 edit_backspace (edit, 1);
2121 break;
2122 case DELCHAR:
2123 edit_delete (edit, 1);
2124 break;
2125 case COLUMN_ON:
2126 column_highlighting = 1;
2127 break;
2128 case COLUMN_OFF:
2129 column_highlighting = 0;
2130 break;
2132 if (ac >= 256 && ac < 512)
2133 edit_insert_ahead (edit, ac - 256);
2134 if (ac >= 0 && ac < 256)
2135 edit_insert (edit, ac);
2137 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2138 edit->mark1 = ac - MARK_1;
2139 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2140 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2141 edit->mark2 = ac - MARK_2;
2142 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2144 if (count++)
2145 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2148 if (edit->start_display > ac - KEY_PRESS) {
2149 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2150 edit->force |= REDRAW_PAGE;
2151 } else if (edit->start_display < ac - KEY_PRESS) {
2152 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2153 edit->force |= REDRAW_PAGE;
2155 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2156 edit_update_curs_row (edit);
2158 done_undo:;
2159 edit->stack_disable = 0;
2162 static void edit_delete_to_line_end (WEdit * edit)
2164 while (edit_get_byte (edit, edit->curs1) != '\n') {
2165 if (!edit->curs2)
2166 break;
2167 edit_delete (edit, 1);
2171 static void edit_delete_to_line_begin (WEdit * edit)
2173 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2174 if (!edit->curs1)
2175 break;
2176 edit_backspace (edit, 1);
2180 void
2181 edit_delete_line (WEdit *edit)
2184 * Delete right part of the line.
2185 * Note that edit_get_byte() returns '\n' when byte position is
2186 * beyond EOF.
2188 while (edit_get_byte (edit, edit->curs1) != '\n') {
2189 (void) edit_delete (edit, 1);
2193 * Delete '\n' char.
2194 * Note that edit_delete() will not corrupt anything if called while
2195 * cursor position is EOF.
2197 (void) edit_delete (edit, 1);
2200 * Delete left part of the line.
2201 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2203 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2204 (void) edit_backspace (edit, 1);
2208 void insert_spaces_tab (WEdit * edit, int half)
2210 int i;
2211 edit_update_curs_col (edit);
2212 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2213 while (i > 0) {
2214 edit_insert (edit, ' ');
2215 i -= space_width;
2219 static int is_aligned_on_a_tab (WEdit * edit)
2221 edit_update_curs_col (edit);
2222 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2223 return 0; /* not alligned on a tab */
2224 return 1;
2227 static int right_of_four_spaces (WEdit *edit)
2229 int i, ch = 0;
2230 for (i = 1; i <= HALF_TAB_SIZE; i++)
2231 ch |= edit_get_byte (edit, edit->curs1 - i);
2232 if (ch == ' ')
2233 return is_aligned_on_a_tab (edit);
2234 return 0;
2237 static int left_of_four_spaces (WEdit *edit)
2239 int i, ch = 0;
2240 for (i = 0; i < HALF_TAB_SIZE; i++)
2241 ch |= edit_get_byte (edit, edit->curs1 + i);
2242 if (ch == ' ')
2243 return is_aligned_on_a_tab (edit);
2244 return 0;
2247 int edit_indent_width (WEdit * edit, long p)
2249 long q = p;
2250 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2251 q++;
2252 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2255 void edit_insert_indent (WEdit * edit, int indent)
2257 if (!option_fill_tabs_with_spaces) {
2258 while (indent >= TAB_SIZE) {
2259 edit_insert (edit, '\t');
2260 indent -= TAB_SIZE;
2263 while (indent-- > 0)
2264 edit_insert (edit, ' ');
2267 static void
2268 edit_auto_indent (WEdit * edit)
2270 long p;
2271 char c;
2272 p = edit->curs1;
2273 /* use the previous line as a template */
2274 p = edit_move_backward (edit, p, 1);
2275 /* copy the leading whitespace of the line */
2276 for (;;) { /* no range check - the line _is_ \n-terminated */
2277 c = edit_get_byte (edit, p++);
2278 if (c != ' ' && c != '\t')
2279 break;
2280 edit_insert (edit, c);
2284 static void edit_double_newline (WEdit * edit)
2286 edit_insert (edit, '\n');
2287 if (edit_get_byte (edit, edit->curs1) == '\n')
2288 return;
2289 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2290 return;
2291 edit->force |= REDRAW_PAGE;
2292 edit_insert (edit, '\n');
2295 static void edit_tab_cmd (WEdit * edit)
2297 int i;
2299 if (option_fake_half_tabs) {
2300 if (is_in_indent (edit)) {
2301 /*insert a half tab (usually four spaces) unless there is a
2302 half tab already behind, then delete it and insert a
2303 full tab. */
2304 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2305 for (i = 1; i <= HALF_TAB_SIZE; i++)
2306 edit_backspace (edit, 1);
2307 edit_insert (edit, '\t');
2308 } else {
2309 insert_spaces_tab (edit, 1);
2311 return;
2314 if (option_fill_tabs_with_spaces) {
2315 insert_spaces_tab (edit, 0);
2316 } else {
2317 edit_insert (edit, '\t');
2319 return;
2322 static void check_and_wrap_line (WEdit * edit)
2324 int curs, c;
2325 if (!option_typewriter_wrap)
2326 return;
2327 edit_update_curs_col (edit);
2328 if (edit->curs_col < option_word_wrap_line_length)
2329 return;
2330 curs = edit->curs1;
2331 for (;;) {
2332 curs--;
2333 c = edit_get_byte (edit, curs);
2334 if (c == '\n' || curs <= 0) {
2335 edit_insert (edit, '\n');
2336 return;
2338 if (c == ' ' || c == '\t') {
2339 int current = edit->curs1;
2340 edit_cursor_move (edit, curs - edit->curs1 + 1);
2341 edit_insert (edit, '\n');
2342 edit_cursor_move (edit, current - edit->curs1 + 1);
2343 return;
2348 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2350 void edit_push_key_press (WEdit * edit)
2352 edit_push_action (edit, KEY_PRESS + edit->start_display);
2353 if (edit->mark2 == -1)
2354 edit_push_action (edit, MARK_1 + edit->mark1);
2357 /* this find the matching bracket in either direction, and sets edit->bracket */
2358 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2360 const char * const b = "{}{[][()(", *p;
2361 int i = 1, a, inc = -1, c, d, n = 0;
2362 unsigned long j = 0;
2363 long q;
2364 edit_update_curs_row (edit);
2365 c = edit_get_byte (edit, edit->curs1);
2366 p = strchr (b, c);
2367 /* no limit */
2368 if (!furthest_bracket_search)
2369 furthest_bracket_search--;
2370 /* not on a bracket at all */
2371 if (!p)
2372 return -1;
2373 /* the matching bracket */
2374 d = p[1];
2375 /* going left or right? */
2376 if (strchr ("{[(", c))
2377 inc = 1;
2378 for (q = edit->curs1 + inc;; q += inc) {
2379 /* out of buffer? */
2380 if (q >= edit->last_byte || q < 0)
2381 break;
2382 a = edit_get_byte (edit, q);
2383 /* don't want to eat CPU */
2384 if (j++ > furthest_bracket_search)
2385 break;
2386 /* out of screen? */
2387 if (in_screen) {
2388 if (q < edit->start_display)
2389 break;
2390 /* count lines if searching downward */
2391 if (inc > 0 && a == '\n')
2392 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2393 break;
2395 /* count bracket depth */
2396 i += (a == c) - (a == d);
2397 /* return if bracket depth is zero */
2398 if (!i)
2399 return q;
2401 /* no match */
2402 return -1;
2405 static long last_bracket = -1;
2407 void edit_find_bracket (WEdit * edit)
2409 edit->bracket = edit_get_bracket (edit, 1, 10000);
2410 if (last_bracket != edit->bracket)
2411 edit->force |= REDRAW_PAGE;
2412 last_bracket = edit->bracket;
2415 static void edit_goto_matching_bracket (WEdit *edit)
2417 long q;
2418 q = edit_get_bracket (edit, 0, 0);
2419 if (q < 0)
2420 return;
2421 edit->bracket = edit->curs1;
2422 edit->force |= REDRAW_PAGE;
2423 edit_cursor_move (edit, q - edit->curs1);
2427 * This executes a command as though the user initiated it through a key
2428 * press. Callback with WIDGET_KEY as a message calls this after
2429 * translating the key press. This function can be used to pass any
2430 * command to the editor. Note that the screen wouldn't update
2431 * automatically. Either of command or char_for_insertion must be
2432 * passed as -1. Commands are executed, and char_for_insertion is
2433 * inserted at the cursor.
2435 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2437 if (command == CK_Begin_Record_Macro) {
2438 edit->macro_i = 0;
2439 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2440 return;
2442 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2443 edit->force |= REDRAW_COMPLETELY;
2444 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2445 edit->macro_i = -1;
2446 return;
2448 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2449 edit->macro[edit->macro_i].command = command;
2450 edit->macro[edit->macro_i++].ch = char_for_insertion;
2452 /* record the beginning of a set of editing actions initiated by a key press */
2453 if (command != CK_Undo && command != CK_Ext_Mode)
2454 edit_push_key_press (edit);
2456 edit_execute_cmd (edit, command, char_for_insertion);
2457 if (column_highlighting)
2458 edit->force |= REDRAW_PAGE;
2461 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2464 This executes a command at a lower level than macro recording.
2465 It also does not push a key_press onto the undo stack. This means
2466 that if it is called many times, a single undo command will undo
2467 all of them. It also does not check for the Undo command.
2469 void
2470 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2472 edit->force |= REDRAW_LINE;
2474 /* The next key press will unhighlight the found string, so update
2475 * the whole page */
2476 if (edit->found_len || column_highlighting)
2477 edit->force |= REDRAW_PAGE;
2479 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2480 column_highlighting = 0;
2481 if (!edit->highlight
2482 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2483 edit_mark_cmd (edit, 1); /* clear */
2484 edit_mark_cmd (edit, 0); /* marking on */
2486 edit->highlight = 1;
2487 } else { /* any other command */
2488 if (edit->highlight)
2489 edit_mark_cmd (edit, 0); /* clear */
2490 edit->highlight = 0;
2493 /* first check for undo */
2494 if (command == CK_Undo) {
2495 edit_do_undo (edit);
2496 edit->found_len = 0;
2497 edit->prev_col = edit_get_col (edit);
2498 edit->search_start = edit->curs1;
2499 return;
2502 /* An ordinary key press */
2503 if (char_for_insertion >= 0) {
2504 if (edit->overwrite) {
2505 if (edit_get_byte (edit, edit->curs1) != '\n')
2506 edit_delete (edit, 0);
2508 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2509 edit_insert_over (edit);
2510 #ifdef HAVE_CHARSET
2511 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2512 unsigned char str[6 + 1];
2513 size_t i = 0;
2514 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2515 if ( res == 0 ) {
2516 str[0] = '.';
2517 str[1] = '\0';
2518 } else {
2519 str[res] = '\0';
2521 while ( str[i] != 0 && i<=6) {
2522 char_for_insertion = str[i];
2523 edit_insert (edit, char_for_insertion);
2524 i++;
2526 } else {
2527 #endif
2528 edit_insert (edit, char_for_insertion);
2529 #ifdef HAVE_CHARSET
2531 #endif
2532 if (option_auto_para_formatting) {
2533 format_paragraph (edit, 0);
2534 edit->force |= REDRAW_PAGE;
2535 } else
2536 check_and_wrap_line (edit);
2537 edit->found_len = 0;
2538 edit->prev_col = edit_get_col (edit);
2539 edit->search_start = edit->curs1;
2540 edit_find_bracket (edit);
2541 return;
2544 switch (command) {
2545 case CK_Begin_Page:
2546 case CK_End_Page:
2547 case CK_Begin_Page_Highlight:
2548 case CK_End_Page_Highlight:
2549 case CK_Word_Left:
2550 case CK_Word_Right:
2551 case CK_Up:
2552 case CK_Down:
2553 case CK_Left:
2554 case CK_Right:
2555 if ( edit->mark2 >= 0 ) {
2556 if ( !option_persistent_selections ) {
2557 if (column_highlighting)
2558 edit_push_action (edit, COLUMN_ON);
2559 column_highlighting = 0;
2560 edit_mark_cmd (edit, 1);
2565 switch (command) {
2566 case CK_Begin_Page:
2567 case CK_End_Page:
2568 case CK_Begin_Page_Highlight:
2569 case CK_End_Page_Highlight:
2570 case CK_Word_Left:
2571 case CK_Word_Right:
2572 case CK_Up:
2573 case CK_Down:
2574 case CK_Word_Left_Highlight:
2575 case CK_Word_Right_Highlight:
2576 case CK_Up_Highlight:
2577 case CK_Down_Highlight:
2578 case CK_Up_Alt_Highlight:
2579 case CK_Down_Alt_Highlight:
2580 if (edit->mark2 == -1)
2581 break; /*marking is following the cursor: may need to highlight a whole line */
2582 case CK_Left:
2583 case CK_Right:
2584 case CK_Left_Highlight:
2585 case CK_Right_Highlight:
2586 edit->force |= REDRAW_CHAR_ONLY;
2589 /* basic cursor key commands */
2590 switch (command) {
2591 case CK_BackSpace:
2592 /* if non persistent selection and text selected */
2593 if ( !option_persistent_selections ) {
2594 if ( edit->mark1 != edit->mark2 ) {
2595 edit_block_delete_cmd (edit);
2596 break;
2599 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2600 edit->over_col--;
2601 break;
2603 if (option_backspace_through_tabs && is_in_indent (edit)) {
2604 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2605 && edit->curs1 > 0)
2606 edit_backspace (edit, 1);
2607 break;
2608 } else {
2609 if (option_fake_half_tabs) {
2610 int i;
2611 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2612 for (i = 0; i < HALF_TAB_SIZE; i++)
2613 edit_backspace (edit, 1);
2614 break;
2618 edit_backspace (edit, 0);
2619 break;
2620 case CK_Delete:
2621 /* if non persistent selection and text selected */
2622 if ( !option_persistent_selections ) {
2623 if ( edit->mark1 != edit->mark2 ) {
2624 edit_block_delete_cmd (edit);
2625 break;
2629 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2630 edit_insert_over (edit);
2632 if (option_fake_half_tabs) {
2633 int i;
2634 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2635 for (i = 1; i <= HALF_TAB_SIZE; i++)
2636 edit_delete (edit, 1);
2637 break;
2640 edit_delete (edit, 0);
2641 break;
2642 case CK_Delete_Word_Left:
2643 edit_left_delete_word (edit);
2644 break;
2645 case CK_Delete_Word_Right:
2646 edit_right_delete_word (edit);
2647 break;
2648 case CK_Delete_Line:
2649 edit_delete_line (edit);
2650 break;
2651 case CK_Delete_To_Line_End:
2652 edit_delete_to_line_end (edit);
2653 break;
2654 case CK_Delete_To_Line_Begin:
2655 edit_delete_to_line_begin (edit);
2656 break;
2657 case CK_Enter:
2658 if (option_auto_para_formatting) {
2659 edit_double_newline (edit);
2660 if (option_return_does_auto_indent)
2661 edit_auto_indent (edit);
2662 format_paragraph (edit, 0);
2663 } else {
2664 edit_insert (edit, '\n');
2665 if (option_return_does_auto_indent) {
2666 edit_auto_indent (edit);
2669 break;
2670 case CK_Return:
2671 edit_insert (edit, '\n');
2672 break;
2674 case CK_Page_Up_Alt_Highlight:
2675 column_highlighting = 1;
2676 case CK_Page_Up:
2677 case CK_Page_Up_Highlight:
2678 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2679 break;
2680 case CK_Page_Down_Alt_Highlight:
2681 column_highlighting = 1;
2682 case CK_Page_Down:
2683 case CK_Page_Down_Highlight:
2684 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2685 break;
2686 case CK_Left_Alt_Highlight:
2687 column_highlighting = 1;
2688 case CK_Left:
2689 case CK_Left_Highlight:
2690 if (option_fake_half_tabs) {
2691 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2692 if ( option_cursor_beyond_eol && edit->over_col > 0)
2693 edit->over_col--;
2694 else
2695 edit_cursor_move (edit, -HALF_TAB_SIZE);
2696 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2697 break;
2700 edit_left_char_move_cmd (edit);
2701 break;
2702 case CK_Right_Alt_Highlight:
2703 column_highlighting = 1;
2704 case CK_Right:
2705 case CK_Right_Highlight:
2706 if (option_fake_half_tabs) {
2707 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2708 edit_cursor_move (edit, HALF_TAB_SIZE);
2709 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2710 break;
2713 edit_right_char_move_cmd (edit);
2714 break;
2715 case CK_Begin_Page:
2716 case CK_Begin_Page_Highlight:
2717 edit_begin_page (edit);
2718 break;
2719 case CK_End_Page:
2720 case CK_End_Page_Highlight:
2721 edit_end_page (edit);
2722 break;
2723 case CK_Word_Left:
2724 case CK_Word_Left_Highlight:
2725 edit_left_word_move_cmd (edit);
2726 break;
2727 case CK_Word_Right:
2728 case CK_Word_Right_Highlight:
2729 edit_right_word_move_cmd (edit);
2730 break;
2731 case CK_Up_Alt_Highlight:
2732 column_highlighting = 1;
2733 case CK_Up:
2734 case CK_Up_Highlight:
2735 edit_move_up (edit, 1, 0);
2736 break;
2737 case CK_Down_Alt_Highlight:
2738 column_highlighting = 1;
2739 case CK_Down:
2740 case CK_Down_Highlight:
2741 edit_move_down (edit, 1, 0);
2742 break;
2743 case CK_Paragraph_Up_Alt_Highlight:
2744 column_highlighting = 1;
2745 case CK_Paragraph_Up:
2746 case CK_Paragraph_Up_Highlight:
2747 edit_move_up_paragraph (edit, 0);
2748 break;
2749 case CK_Paragraph_Down_Alt_Highlight:
2750 column_highlighting = 1;
2751 case CK_Paragraph_Down:
2752 case CK_Paragraph_Down_Highlight:
2753 edit_move_down_paragraph (edit, 0);
2754 break;
2755 case CK_Scroll_Up_Alt_Highlight:
2756 column_highlighting = 1;
2757 case CK_Scroll_Up:
2758 case CK_Scroll_Up_Highlight:
2759 edit_move_up (edit, 1, 1);
2760 break;
2761 case CK_Scroll_Down_Alt_Highlight:
2762 column_highlighting = 1;
2763 case CK_Scroll_Down:
2764 case CK_Scroll_Down_Highlight:
2765 edit_move_down (edit, 1, 1);
2766 break;
2767 case CK_Home:
2768 case CK_Home_Highlight:
2769 edit_cursor_to_bol (edit);
2770 break;
2771 case CK_End:
2772 case CK_End_Highlight:
2773 edit_cursor_to_eol (edit);
2774 break;
2775 case CK_Tab:
2776 /* if text marked shift block */
2777 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2778 edit_move_block_to_right (edit);
2779 } else {
2780 if ( option_cursor_beyond_eol )
2781 edit_insert_over (edit);
2782 edit_tab_cmd (edit);
2783 if (option_auto_para_formatting) {
2784 format_paragraph (edit, 0);
2785 edit->force |= REDRAW_PAGE;
2786 } else {
2787 check_and_wrap_line (edit);
2790 break;
2792 case CK_Toggle_Insert:
2793 edit->overwrite = (edit->overwrite == 0);
2794 break;
2796 case CK_Mark:
2797 if (edit->mark2 >= 0) {
2798 if (column_highlighting)
2799 edit_push_action (edit, COLUMN_ON);
2800 column_highlighting = 0;
2802 edit_mark_cmd (edit, 0);
2803 break;
2804 case CK_Column_Mark:
2805 if (!column_highlighting)
2806 edit_push_action (edit, COLUMN_OFF);
2807 column_highlighting = 1;
2808 edit_mark_cmd (edit, 0);
2809 break;
2810 case CK_Unmark:
2811 if (column_highlighting)
2812 edit_push_action (edit, COLUMN_ON);
2813 column_highlighting = 0;
2814 edit_mark_cmd (edit, 1);
2815 break;
2817 case CK_Toggle_Line_State:
2818 option_line_state = !option_line_state;
2819 if ( option_line_state ) {
2820 option_line_state_width = LINE_STATE_WIDTH;
2821 } else {
2822 option_line_state_width = 0;
2824 edit->force |= REDRAW_PAGE;
2825 break;
2827 case CK_Toggle_Bookmark:
2828 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2829 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2830 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2831 else
2832 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2833 break;
2834 case CK_Flush_Bookmarks:
2835 book_mark_flush (edit, BOOK_MARK_COLOR);
2836 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2837 edit->force |= REDRAW_PAGE;
2838 break;
2839 case CK_Next_Bookmark:
2840 if (edit->book_mark) {
2841 struct _book_mark *p;
2842 p = (struct _book_mark *) book_mark_find (edit,
2843 edit->curs_line);
2844 if (p->next) {
2845 p = p->next;
2846 if (p->line >= edit->start_line + edit->num_widget_lines
2847 || p->line < edit->start_line)
2848 edit_move_display (edit,
2849 p->line -
2850 edit->num_widget_lines / 2);
2851 edit_move_to_line (edit, p->line);
2854 break;
2855 case CK_Prev_Bookmark:
2856 if (edit->book_mark) {
2857 struct _book_mark *p;
2858 p = (struct _book_mark *) book_mark_find (edit,
2859 edit->curs_line);
2860 while (p->line == edit->curs_line)
2861 if (p->prev)
2862 p = p->prev;
2863 if (p->line >= 0) {
2864 if (p->line >= edit->start_line + edit->num_widget_lines
2865 || p->line < edit->start_line)
2866 edit_move_display (edit,
2867 p->line -
2868 edit->num_widget_lines / 2);
2869 edit_move_to_line (edit, p->line);
2872 break;
2874 case CK_Beginning_Of_Text:
2875 case CK_Beginning_Of_Text_Highlight:
2876 edit_move_to_top (edit);
2877 break;
2878 case CK_End_Of_Text:
2879 case CK_End_Of_Text_Highlight:
2880 edit_move_to_bottom (edit);
2881 break;
2883 case CK_Copy:
2884 edit_block_copy_cmd (edit);
2885 break;
2886 case CK_Remove:
2887 edit_block_delete_cmd (edit);
2888 break;
2889 case CK_Move:
2890 edit_block_move_cmd (edit);
2891 break;
2893 case CK_XStore:
2894 edit_copy_to_X_buf_cmd (edit);
2895 break;
2896 case CK_XCut:
2897 edit_cut_to_X_buf_cmd (edit);
2898 break;
2899 case CK_XPaste:
2900 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2901 edit_insert_over (edit);
2902 edit_paste_from_X_buf_cmd (edit);
2903 break;
2904 case CK_Selection_History:
2905 edit_paste_from_history (edit);
2906 break;
2908 case CK_Save_As:
2909 edit_save_as_cmd (edit);
2910 break;
2911 case CK_Save:
2912 edit_save_confirm_cmd (edit);
2913 break;
2914 case CK_Load:
2915 edit_load_cmd (edit, EDIT_FILE_COMMON);
2916 break;
2917 case CK_Save_Block:
2918 edit_save_block_cmd (edit);
2919 break;
2920 case CK_Insert_File:
2921 edit_insert_file_cmd (edit);
2922 break;
2924 case CK_Load_Prev_File:
2925 edit_load_back_cmd (edit);
2926 break;
2927 case CK_Load_Next_File:
2928 edit_load_forward_cmd (edit);
2929 break;
2931 case CK_Load_Syntax_File:
2932 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
2933 break;
2934 case CK_Load_Menu_File:
2935 edit_load_cmd (edit, EDIT_FILE_MENU);
2936 break;
2938 case CK_Toggle_Syntax:
2939 if ((option_syntax_highlighting ^= 1) == 1)
2940 edit_load_syntax (edit, NULL, option_syntax_type);
2941 edit->force |= REDRAW_PAGE;
2942 break;
2944 case CK_Toggle_Tab_TWS:
2945 enable_show_tabs_tws ^= 1;
2946 edit->force |= REDRAW_PAGE;
2947 break;
2949 case CK_Find:
2950 edit_search_cmd (edit, 0);
2951 break;
2952 case CK_Find_Again:
2953 edit_search_cmd (edit, 1);
2954 break;
2955 case CK_Replace:
2956 edit_replace_cmd (edit, 0);
2957 break;
2958 case CK_Replace_Again:
2959 edit_replace_cmd (edit, 1);
2960 break;
2961 case CK_Complete_Word:
2962 /* if text marked shift block */
2963 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2964 edit_move_block_to_left (edit);
2965 } else {
2966 edit_complete_word_cmd (edit);
2968 break;
2969 case CK_Find_Definition:
2970 edit_get_match_keyword_cmd (edit);
2971 break;
2973 case CK_Exit:
2974 dlg_stop (edit->widget.parent);
2975 break;
2976 case CK_New:
2977 edit_new_cmd (edit);
2978 break;
2980 case CK_Help:
2981 edit_help_cmd (edit);
2982 break;
2984 case CK_Refresh:
2985 edit_refresh_cmd (edit);
2986 break;
2988 case CK_Date:{
2989 char s[1024];
2990 /* fool gcc to prevent a Y2K warning */
2991 char time_format[] = "_c";
2992 time_format[0] = '%';
2994 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
2995 edit_print_string (edit, s);
2996 edit->force |= REDRAW_PAGE;
2997 break;
2999 case CK_Goto:
3000 edit_goto_cmd (edit);
3001 break;
3002 case CK_Paragraph_Format:
3003 format_paragraph (edit, 1);
3004 edit->force |= REDRAW_PAGE;
3005 break;
3006 case CK_Delete_Macro:
3007 edit_delete_macro_cmd (edit);
3008 break;
3009 case CK_Match_Bracket:
3010 edit_goto_matching_bracket (edit);
3011 break;
3012 case CK_User_Menu:
3013 user_menu (edit);
3014 break;
3015 case CK_Sort:
3016 edit_sort_cmd (edit);
3017 break;
3018 case CK_ExtCmd:
3019 edit_ext_cmd (edit);
3020 break;
3021 case CK_Mail:
3022 edit_mail_dialog (edit);
3023 break;
3024 case CK_Shell:
3025 view_other_cmd ();
3026 break;
3027 case CK_Select_Codepage:
3028 edit_select_codepage_cmd (edit);
3029 break;
3030 case CK_Insert_Literal:
3031 edit_insert_literal_cmd (edit);
3032 break;
3033 case CK_Execute_Macro:
3034 edit_execute_macro_cmd (edit);
3035 break;
3036 case CK_Begin_End_Macro:
3037 edit_begin_end_macro_cmd (edit);
3038 break;
3039 case CK_Ext_Mode:
3040 edit->extmod = 1;
3041 break;
3042 default:
3043 break;
3046 /* CK_Pipe_Block */
3047 if ((command / 1000) == 1) /* a shell command */
3048 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3049 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3050 struct macro m[MAX_MACRO_LENGTH];
3051 int nm;
3052 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3053 edit_execute_macro (edit, m, nm);
3056 /* keys which must set the col position, and the search vars */
3057 switch (command) {
3058 case CK_Find:
3059 case CK_Find_Again:
3060 case CK_Replace:
3061 case CK_Replace_Again:
3062 case CK_Complete_Word:
3063 edit->prev_col = edit_get_col (edit);
3064 break;
3065 case CK_Up:
3066 case CK_Up_Highlight:
3067 case CK_Up_Alt_Highlight:
3068 case CK_Down:
3069 case CK_Down_Highlight:
3070 case CK_Down_Alt_Highlight:
3071 case CK_Page_Up:
3072 case CK_Page_Up_Highlight:
3073 case CK_Page_Up_Alt_Highlight:
3074 case CK_Page_Down:
3075 case CK_Page_Down_Highlight:
3076 case CK_Page_Down_Alt_Highlight:
3077 case CK_Beginning_Of_Text:
3078 case CK_Beginning_Of_Text_Highlight:
3079 case CK_End_Of_Text:
3080 case CK_End_Of_Text_Highlight:
3081 case CK_Paragraph_Up:
3082 case CK_Paragraph_Up_Highlight:
3083 case CK_Paragraph_Up_Alt_Highlight:
3084 case CK_Paragraph_Down:
3085 case CK_Paragraph_Down_Highlight:
3086 case CK_Paragraph_Down_Alt_Highlight:
3087 case CK_Scroll_Up:
3088 case CK_Scroll_Up_Highlight:
3089 case CK_Scroll_Up_Alt_Highlight:
3090 case CK_Scroll_Down:
3091 case CK_Scroll_Down_Highlight:
3092 case CK_Scroll_Down_Alt_Highlight:
3093 edit->search_start = edit->curs1;
3094 edit->found_len = 0;
3095 break;
3096 default:
3097 edit->found_len = 0;
3098 edit->prev_col = edit_get_col (edit);
3099 edit->search_start = edit->curs1;
3101 edit_find_bracket (edit);
3103 if (option_auto_para_formatting) {
3104 switch (command) {
3105 case CK_BackSpace:
3106 case CK_Delete:
3107 case CK_Delete_Word_Left:
3108 case CK_Delete_Word_Right:
3109 case CK_Delete_To_Line_End:
3110 case CK_Delete_To_Line_Begin:
3111 format_paragraph (edit, 0);
3112 edit->force |= REDRAW_PAGE;
3118 static void
3119 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3121 int i = 0;
3123 if (edit->macro_depth++ > 256) {
3124 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3125 edit->macro_depth--;
3126 return;
3128 edit->force |= REDRAW_PAGE;
3129 for (; i < n; i++) {
3130 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3132 edit_update_screen (edit);
3133 edit->macro_depth--;
3136 /* User edit menu, like user menu (F2) but only in editor. */
3137 static void
3138 user_menu (WEdit * edit)
3140 FILE *fd;
3141 int nomark;
3142 struct stat status;
3143 long start_mark, end_mark;
3144 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3145 int rc = 0;
3147 nomark = eval_marks (edit, &start_mark, &end_mark);
3148 if (!nomark) /* remember marked or not */
3149 edit_save_block (edit, block_file, start_mark, end_mark);
3151 /* run shell scripts from menu */
3152 user_menu_cmd (edit);
3154 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3155 /* no block messages */
3156 goto cleanup;
3159 if (!nomark) {
3160 /* i.e. we have marked block */
3161 rc = edit_block_delete_cmd (edit);
3164 if (!rc) {
3165 edit_insert_file (edit, block_file);
3168 /* truncate block file */
3169 if ((fd = fopen (block_file, "w"))) {
3170 fclose (fd);
3173 edit_refresh_cmd (edit);
3174 edit->force |= REDRAW_COMPLETELY;
3176 cleanup:
3177 g_free (block_file);
3180 void
3181 edit_stack_init (void)
3183 for (edit_stack_iterator = 0;
3184 edit_stack_iterator < MAX_HISTORY_MOVETO;
3185 edit_stack_iterator++ ) {
3186 edit_history_moveto[edit_stack_iterator].filename = NULL;
3187 edit_history_moveto[edit_stack_iterator].line = -1;
3190 edit_stack_iterator = 0;
3193 void
3194 edit_stack_free (void)
3196 for (edit_stack_iterator = 0;
3197 edit_stack_iterator < MAX_HISTORY_MOVETO;
3198 edit_stack_iterator++)
3199 g_free (edit_history_moveto[edit_stack_iterator].filename);