Merge branch '1497_stickchars'
[pantumic.git] / edit / edit.c
blob3e20b0062ca9fc8a7792f062ee9576482514004c
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;
396 if (edit->lb == LB_ASIS) {
397 for (i = 0; i < edit->last_byte; i++)
398 if (fputc (edit_get_byte (edit, i), f) < 0)
399 break;
400 return i;
403 /* change line breaks */
404 for (i = 0; i < edit->last_byte; i++) {
405 unsigned char c = edit_get_byte (edit, i);
407 if (!(c == '\n' || c == '\r')) {
408 /* not line break */
409 if (fputc (c, f) < 0)
410 return i;
411 } else { /* (c == '\n' || c == '\r') */
412 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
414 switch (edit->lb) {
415 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
416 /* put one line break unconditionally */
417 if (fputc ('\n', f) < 0)
418 return i;
420 i++; /* 2 chars are processed */
422 if (c == '\r' && c1 == '\n')
423 /* Windows line break; go to the next char */
424 break;
426 if (c == '\r' && c1 == '\r') {
427 /* two Macintosh line breaks; put second line break */
428 if (fputc ('\n', f) < 0)
429 return i;
430 break;
433 if (fputc (c1, f) < 0)
434 return i;
435 break;
437 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
438 /* put one line break unconditionally */
439 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
440 return i;
442 if (c == '\r' && c1 == '\n')
443 /* Windows line break; go to the next char */
444 i++;
445 break;
447 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
448 /* put one line break unconditionally */
449 if (fputc ('\r', f) < 0)
450 return i;
452 i++; /* 2 chars are processed */
454 if (c == '\r' && c1 == '\n')
455 /* Windows line break; go to the next char */
456 break;
458 if (c == '\n' && c1 == '\n') {
459 /* two Windows line breaks; put second line break */
460 if (fputc ('\r', f) < 0)
461 return i;
462 break;
465 if (fputc (c1, f) < 0)
466 return i;
467 break;
468 case LB_ASIS: /* default without changes */
469 break;
474 return edit->last_byte;
477 #define TEMP_BUF_LEN 1024
479 /* inserts a file at the cursor, returns 1 on success */
481 edit_insert_file (WEdit *edit, const char *filename)
483 char *p;
484 if ((p = edit_get_filter (filename))) {
485 FILE *f;
486 long current = edit->curs1;
487 f = (FILE *) popen (p, "r");
488 if (f) {
489 edit_insert_stream (edit, f);
490 edit_cursor_move (edit, current - edit->curs1);
491 if (pclose (f) > 0) {
492 GString *errmsg = g_string_new (NULL);
493 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
494 edit_error_dialog (_("Error"), errmsg->str);
495 g_string_free (errmsg, TRUE);
496 g_free (p);
497 return 0;
499 } else {
500 GString *errmsg = g_string_new (NULL);
501 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
502 edit_error_dialog (_("Error"), errmsg->str);
503 g_string_free (errmsg, TRUE);
504 g_free (p);
505 return 0;
507 g_free (p);
508 } else {
509 int i, file, blocklen;
510 long current = edit->curs1;
511 int vertical_insertion = 0;
512 char *buf;
513 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
514 return 0;
515 buf = g_malloc (TEMP_BUF_LEN);
516 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
517 if (blocklen > 0) {
518 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
519 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
520 vertical_insertion = 1;
521 } else {
522 mc_lseek (file, 0, SEEK_SET);
525 if (vertical_insertion) {
526 blocklen = edit_insert_column_of_text_from_file (edit, file);
527 } else {
528 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
529 for (i = 0; i < blocklen; i++)
530 edit_insert (edit, buf[i]);
533 edit_cursor_move (edit, current - edit->curs1);
534 g_free (buf);
535 mc_close (file);
536 if (blocklen)
537 return 0;
539 return 1;
542 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
543 static int
544 check_file_access (WEdit *edit, const char *filename, struct stat *st)
546 int file;
547 GString *errmsg = (GString *) 0;
549 /* Try opening an existing file */
550 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
552 if (file < 0) {
554 * Try creating the file. O_EXCL prevents following broken links
555 * and opening existing files.
557 file =
558 mc_open (filename,
559 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
560 0666);
561 if (file < 0) {
562 g_string_sprintf (errmsg = g_string_new (NULL),
563 _(" Cannot open %s for reading "), filename);
564 goto cleanup;
565 } else {
566 /* New file, delete it if it's not modified or saved */
567 edit->delete_file = 1;
571 /* Check what we have opened */
572 if (mc_fstat (file, st) < 0) {
573 g_string_sprintf (errmsg = g_string_new (NULL),
574 _(" Cannot get size/permissions for %s "), filename);
575 goto cleanup;
578 /* We want to open regular files only */
579 if (!S_ISREG (st->st_mode)) {
580 g_string_sprintf (errmsg = g_string_new (NULL),
581 _(" %s is not a regular file "), filename);
582 goto cleanup;
586 * Don't delete non-empty files.
587 * O_EXCL should prevent it, but let's be on the safe side.
589 if (st->st_size > 0) {
590 edit->delete_file = 0;
593 if (st->st_size >= SIZE_LIMIT) {
594 g_string_sprintf (errmsg = g_string_new (NULL),
595 _(" File %s is too large "), filename);
596 goto cleanup;
599 cleanup:
600 (void) mc_close (file);
601 if (errmsg) {
602 edit_error_dialog (_("Error"), errmsg->str);
603 g_string_free (errmsg, TRUE);
604 return 1;
606 return 0;
610 * Open the file and load it into the buffers, either directly or using
611 * a filter. Return 0 on success, 1 on error.
613 * Fast loading (edit_load_file_fast) is used when the file size is
614 * known. In this case the data is read into the buffers by blocks.
615 * If the file size is not known, the data is loaded byte by byte in
616 * edit_insert_file.
618 static int
619 edit_load_file (WEdit *edit)
621 int fast_load = 1;
623 /* Cannot do fast load if a filter is used */
624 if (edit_find_filter (edit->filename) >= 0)
625 fast_load = 0;
628 * VFS may report file size incorrectly, and slow load is not a big
629 * deal considering overhead in VFS.
631 if (!vfs_file_is_local (edit->filename))
632 fast_load = 0;
635 * FIXME: line end translation should disable fast loading as well
636 * Consider doing fseek() to the end and ftell() for the real size.
639 if (*edit->filename) {
640 /* If we are dealing with a real file, check that it exists */
641 if (check_file_access (edit, edit->filename, &edit->stat1))
642 return 1;
643 } else {
644 /* nothing to load */
645 fast_load = 0;
648 edit_init_buffers (edit);
650 if (fast_load) {
651 edit->last_byte = edit->stat1.st_size;
652 edit_load_file_fast (edit, edit->filename);
653 /* If fast load was used, the number of lines wasn't calculated */
654 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
655 } else {
656 #ifdef HAVE_CHARSET
657 const char *codepage_id;
658 #endif
659 edit->last_byte = 0;
660 if (*edit->filename) {
661 edit->stack_disable = 1;
662 if (!edit_insert_file (edit, edit->filename)) {
663 edit_clean (edit);
664 return 1;
666 edit->stack_disable = 0;
669 #ifdef HAVE_CHARSET
670 codepage_id = get_codepage_id( source_codepage );
671 if ( codepage_id )
672 edit->utf8 = str_isutf8 ( codepage_id );
673 #endif
675 edit->lb = LB_ASIS;
676 return 0;
679 /* Restore saved cursor position in the file */
680 static void
681 edit_load_position (WEdit *edit)
683 char *filename;
684 long line, column;
686 if (!edit->filename || !*edit->filename)
687 return;
689 filename = vfs_canon (edit->filename);
690 load_file_position (filename, &line, &column);
691 g_free (filename);
693 edit_move_to_line (edit, line - 1);
694 edit->prev_col = column;
695 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
696 edit_move_display (edit, line - (edit->num_widget_lines / 2));
699 /* Save cursor position in the file */
700 static void
701 edit_save_position (WEdit *edit)
703 char *filename;
705 if (!edit->filename || !*edit->filename)
706 return;
708 filename = vfs_canon (edit->filename);
709 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
710 g_free (filename);
713 /* Clean the WEdit stricture except the widget part */
714 static void
715 edit_purge_widget (WEdit *edit)
717 int len = sizeof (WEdit) - sizeof (Widget);
718 char *start = (char *) edit + sizeof (Widget);
719 memset (start, 0, len);
720 edit->macro_i = -1; /* not recording a macro */
723 #define space_width 1
726 * Fill in the edit structure. Return NULL on failure. Pass edit as
727 * NULL to allocate a new structure.
729 * If line is 0, try to restore saved position. Otherwise put the
730 * cursor on that line and show it in the middle of the screen.
732 WEdit *
733 edit_init (WEdit *edit, int lines, int columns, const char *filename,
734 long line)
736 int to_free = 0;
737 option_auto_syntax = 1; /* Resetting to auto on every invokation */
738 if ( option_line_state ) {
739 option_line_state_width = LINE_STATE_WIDTH;
740 } else {
741 option_line_state_width = 0;
743 if (!edit) {
744 #ifdef ENABLE_NLS
746 * Expand option_whole_chars_search by national letters using
747 * current locale
750 static char option_whole_chars_search_buf[256];
752 if (option_whole_chars_search_buf != option_whole_chars_search) {
753 size_t i;
754 size_t len = str_term_width1 (option_whole_chars_search);
756 strcpy (option_whole_chars_search_buf,
757 option_whole_chars_search);
759 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
760 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
761 option_whole_chars_search_buf[len++] = i;
765 option_whole_chars_search_buf[len] = 0;
766 option_whole_chars_search = option_whole_chars_search_buf;
768 #endif /* ENABLE_NLS */
769 edit = g_malloc0 (sizeof (WEdit));
770 edit->search = NULL;
771 to_free = 1;
773 edit_purge_widget (edit);
774 edit->num_widget_lines = lines;
775 edit->over_col = 0;
776 edit->num_widget_columns = columns;
777 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
778 edit->stat1.st_uid = getuid ();
779 edit->stat1.st_gid = getgid ();
780 edit->stat1.st_mtime = 0;
781 edit->bracket = -1;
782 edit->force |= REDRAW_PAGE;
783 edit_set_filename (edit, filename);
784 edit->stack_size = START_STACK_SIZE;
785 edit->stack_size_mask = START_STACK_SIZE - 1;
786 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
787 if (edit_load_file (edit)) {
788 /* edit_load_file already gives an error message */
789 if (to_free)
790 g_free (edit);
791 return 0;
793 edit->utf8 = 0;
794 edit->converter = str_cnv_from_term;
795 #ifdef HAVE_CHARSET
796 const char *cp_id = NULL;
797 cp_id = get_codepage_id (source_codepage >= 0 ?
798 source_codepage : display_codepage);
800 if (cp_id != NULL) {
801 GIConv conv;
802 conv = str_crt_conv_from (cp_id);
803 if (conv != INVALID_CONV) {
804 if (edit->converter != str_cnv_from_term)
805 str_close_conv (edit->converter);
806 edit->converter = conv;
809 if (cp_id != NULL)
810 edit->utf8 = str_isutf8 (cp_id);
811 #endif
813 edit->loading_done = 1;
814 edit->modified = 0;
815 edit->locked = 0;
816 edit_load_syntax (edit, 0, 0);
818 int color;
819 edit_get_syntax_color (edit, -1, &color);
822 /* load saved cursor position */
823 if ((line == 0) && option_save_position) {
824 edit_load_position (edit);
825 } else {
826 if (line <= 0)
827 line = 1;
828 edit_move_display (edit, line - 1);
829 edit_move_to_line (edit, line - 1);
832 edit_load_user_map(edit);
834 return edit;
837 /* Clear the edit struct, freeing everything in it. Return 1 on success */
839 edit_clean (WEdit *edit)
841 int j = 0;
843 if (!edit)
844 return 0;
846 /* a stale lock, remove it */
847 if (edit->locked)
848 edit->locked = edit_unlock_file (edit->filename);
850 /* save cursor position */
851 if (option_save_position)
852 edit_save_position (edit);
854 /* File specified on the mcedit command line and never saved */
855 if (edit->delete_file)
856 unlink (edit->filename);
858 edit_free_syntax_rules (edit);
859 book_mark_flush (edit, -1);
860 for (; j <= MAXBUFF; j++) {
861 g_free (edit->buffers1[j]);
862 g_free (edit->buffers2[j]);
865 g_free (edit->undo_stack);
866 g_free (edit->filename);
867 g_free (edit->dir);
869 if (edit->search)
871 mc_search_free(edit->search);
872 edit->search = NULL;
874 edit_purge_widget (edit);
876 return 1;
880 /* returns 1 on success */
881 int edit_renew (WEdit * edit)
883 int lines = edit->num_widget_lines;
884 int columns = edit->num_widget_columns;
885 int retval = 1;
887 edit_clean (edit);
888 if (!edit_init (edit, lines, columns, "", 0))
889 retval = 0;
890 return retval;
894 * Load a new file into the editor. If it fails, preserve the old file.
895 * To do it, allocate a new widget, initialize it and, if the new file
896 * was loaded, copy the data to the old widget.
897 * Return 1 on success, 0 on failure.
900 edit_reload (WEdit *edit, const char *filename)
902 WEdit *e;
903 int lines = edit->num_widget_lines;
904 int columns = edit->num_widget_columns;
906 e = g_malloc0 (sizeof (WEdit));
907 e->widget = edit->widget;
908 if (!edit_init (e, lines, columns, filename, 0)) {
909 g_free (e);
910 return 0;
912 edit_clean (edit);
913 memcpy (edit, e, sizeof (WEdit));
914 g_free (e);
915 return 1;
919 * Load a new file into the editor and set line. If it fails, preserve the old file.
920 * To do it, allocate a new widget, initialize it and, if the new file
921 * was loaded, copy the data to the old widget.
922 * Return 1 on success, 0 on failure.
925 edit_reload_line (WEdit *edit, const char *filename, long line)
927 WEdit *e;
928 int lines = edit->num_widget_lines;
929 int columns = edit->num_widget_columns;
931 e = g_malloc0 (sizeof (WEdit));
932 e->widget = edit->widget;
933 if (!edit_init (e, lines, columns, filename, line)) {
934 g_free (e);
935 return 0;
937 edit_clean (edit);
938 memcpy (edit, e, sizeof (WEdit));
939 g_free (e);
940 return 1;
945 Recording stack for undo:
946 The following is an implementation of a compressed stack. Identical
947 pushes are recorded by a negative prefix indicating the number of times the
948 same char was pushed. This saves space for repeated curs-left or curs-right
949 delete etc.
953 pushed: stored:
957 b -3
959 c --> -4
965 If the stack long int is 0-255 it represents a normal insert (from a backspace),
966 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
967 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
968 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
969 position.
971 The only way the cursor moves or the buffer is changed is through the routines:
972 insert, backspace, insert_ahead, delete, and cursor_move.
973 These record the reverse undo movements onto the stack each time they are
974 called.
976 Each key press results in a set of actions (insert; delete ...). So each time
977 a key is pressed the current position of start_display is pushed as
978 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
979 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
980 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
984 void edit_push_action (WEdit * edit, long c,...)
986 unsigned long sp = edit->stack_pointer;
987 unsigned long spm1;
988 long *t;
990 /* first enlarge the stack if necessary */
991 if (sp > edit->stack_size - 10) { /* say */
992 if (option_max_undo < 256)
993 option_max_undo = 256;
994 if (edit->stack_size < (unsigned long) option_max_undo) {
995 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
996 if (t) {
997 edit->undo_stack = t;
998 edit->stack_size <<= 1;
999 edit->stack_size_mask = edit->stack_size - 1;
1003 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1004 if (edit->stack_disable)
1005 return;
1007 #ifdef FAST_MOVE_CURSOR
1008 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1009 va_list ap;
1010 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1011 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1012 va_start (ap, c);
1013 c = -(va_arg (ap, int));
1014 va_end (ap);
1015 } else
1016 #endif /* ! FAST_MOVE_CURSOR */
1017 if (edit->stack_bottom != sp
1018 && spm1 != edit->stack_bottom
1019 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1020 int d;
1021 if (edit->undo_stack[spm1] < 0) {
1022 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1023 if (d == c) {
1024 if (edit->undo_stack[spm1] > -1000000000) {
1025 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1026 edit->undo_stack[spm1]--;
1027 return;
1030 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1031 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1032 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1033 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1034 if (edit->undo_stack[spm1] == -2)
1035 edit->stack_pointer = spm1;
1036 else
1037 edit->undo_stack[spm1]++;
1038 return;
1040 #endif
1041 } else {
1042 d = edit->undo_stack[spm1];
1043 if (d == c) {
1044 if (c >= KEY_PRESS)
1045 return; /* --> no need to push multiple do-nothings */
1046 edit->undo_stack[sp] = -2;
1047 goto check_bottom;
1049 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1050 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1051 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1052 edit->stack_pointer = spm1;
1053 return;
1055 #endif
1058 edit->undo_stack[sp] = c;
1059 check_bottom:
1061 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1063 /* if the sp wraps round and catches the stack_bottom then erase
1064 * the first set of actions on the stack to make space - by moving
1065 * stack_bottom forward one "key press" */
1066 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1067 if ((unsigned long) c == edit->stack_bottom ||
1068 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1069 do {
1070 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1071 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1073 /*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: */
1074 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1075 edit->stack_bottom = edit->stack_pointer = 0;
1079 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1080 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1082 static long
1083 pop_action (WEdit * edit)
1085 long c;
1086 unsigned long sp = edit->stack_pointer;
1087 if (sp == edit->stack_bottom) {
1088 return STACK_BOTTOM;
1090 sp = (sp - 1) & edit->stack_size_mask;
1091 if ((c = edit->undo_stack[sp]) >= 0) {
1092 /* edit->undo_stack[sp] = '@'; */
1093 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1094 return c;
1096 if (sp == edit->stack_bottom) {
1097 return STACK_BOTTOM;
1099 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1100 if (edit->undo_stack[sp] == -2) {
1101 /* edit->undo_stack[sp] = '@'; */
1102 edit->stack_pointer = sp;
1103 } else
1104 edit->undo_stack[sp]++;
1106 return c;
1109 /* is called whenever a modification is made by one of the four routines below */
1110 static void edit_modification (WEdit * edit)
1112 edit->caches_valid = 0;
1113 edit->screen_modified = 1;
1115 /* raise lock when file modified */
1116 if (!edit->modified && !edit->delete_file)
1117 edit->locked = edit_lock_file (edit->filename);
1118 edit->modified = 1;
1122 Basic low level single character buffer alterations and movements at the cursor.
1123 Returns char passed over, inserted or removed.
1126 void
1127 edit_insert (WEdit *edit, int c)
1129 /* check if file has grown to large */
1130 if (edit->last_byte >= SIZE_LIMIT)
1131 return;
1133 /* first we must update the position of the display window */
1134 if (edit->curs1 < edit->start_display) {
1135 edit->start_display++;
1136 if (c == '\n')
1137 edit->start_line++;
1140 /* Mark file as modified, unless the file hasn't been fully loaded */
1141 if (edit->loading_done) {
1142 edit_modification (edit);
1145 /* now we must update some info on the file and check if a redraw is required */
1146 if (c == '\n') {
1147 if (edit->book_mark)
1148 book_mark_inc (edit, edit->curs_line);
1149 edit->curs_line++;
1150 edit->total_lines++;
1151 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1154 /* save the reverse command onto the undo stack */
1155 edit_push_action (edit, BACKSPACE);
1157 /* update markers */
1158 edit->mark1 += (edit->mark1 > edit->curs1);
1159 edit->mark2 += (edit->mark2 > edit->curs1);
1160 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1162 /* add a new buffer if we've reached the end of the last one */
1163 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1164 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1165 g_malloc (EDIT_BUF_SIZE);
1167 /* perform the insertion */
1168 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1169 curs1 & M_EDIT_BUF_SIZE]
1170 = (unsigned char) c;
1172 /* update file length */
1173 edit->last_byte++;
1175 /* update cursor position */
1176 edit->curs1++;
1179 void
1180 edit_insert_over (WEdit * edit)
1182 int i;
1184 for ( i = 0; i < edit->over_col; i++ ) {
1185 edit_insert (edit, ' ');
1187 edit->over_col = 0;
1190 /* same as edit_insert and move left */
1191 void edit_insert_ahead (WEdit * edit, int c)
1193 if (edit->last_byte >= SIZE_LIMIT)
1194 return;
1195 if (edit->curs1 < edit->start_display) {
1196 edit->start_display++;
1197 if (c == '\n')
1198 edit->start_line++;
1200 edit_modification (edit);
1201 if (c == '\n') {
1202 if (edit->book_mark)
1203 book_mark_inc (edit, edit->curs_line);
1204 edit->total_lines++;
1205 edit->force |= REDRAW_AFTER_CURSOR;
1207 edit_push_action (edit, DELCHAR);
1209 edit->mark1 += (edit->mark1 >= edit->curs1);
1210 edit->mark2 += (edit->mark2 >= edit->curs1);
1211 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1213 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1214 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1215 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1217 edit->last_byte++;
1218 edit->curs2++;
1222 int edit_delete (WEdit * edit, const int byte_delete)
1224 int p = 0;
1225 int cw = 1;
1226 int i;
1228 if (!edit->curs2)
1229 return 0;
1231 edit->mark1 -= (edit->mark1 > edit->curs1);
1232 edit->mark2 -= (edit->mark2 > edit->curs1);
1233 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1235 cw = 1;
1236 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1237 if ( edit->utf8 && byte_delete == 0 ) {
1238 edit_get_utf (edit, edit->curs1, &cw);
1239 if ( cw < 1 )
1240 cw = 1;
1242 for ( i = 1; i<= cw; i++ ) {
1243 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1245 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1246 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1247 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1249 edit->last_byte--;
1250 edit->curs2--;
1251 edit_push_action (edit, p + 256);
1254 edit_modification (edit);
1255 if (p == '\n') {
1256 if (edit->book_mark)
1257 book_mark_dec (edit, edit->curs_line);
1258 edit->total_lines--;
1259 edit->force |= REDRAW_AFTER_CURSOR;
1261 if (edit->curs1 < edit->start_display) {
1262 edit->start_display--;
1263 if (p == '\n')
1264 edit->start_line--;
1267 return p;
1271 static int
1272 edit_backspace (WEdit * edit, const int byte_delete)
1274 int p = 0;
1275 int cw = 1;
1276 int i;
1278 if (!edit->curs1)
1279 return 0;
1281 edit->mark1 -= (edit->mark1 >= edit->curs1);
1282 edit->mark2 -= (edit->mark2 >= edit->curs1);
1283 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1285 cw = 1;
1286 if ( edit->utf8 && byte_delete == 0 ) {
1287 edit_get_prev_utf (edit, edit->curs1, &cw);
1288 if ( cw < 1 )
1289 cw = 1;
1291 for ( i = 1; i<= cw; i++ ) {
1292 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1293 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1294 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1295 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1297 edit->last_byte--;
1298 edit->curs1--;
1299 edit_push_action (edit, p);
1301 edit_modification (edit);
1302 if (p == '\n') {
1303 if (edit->book_mark)
1304 book_mark_dec (edit, edit->curs_line);
1305 edit->curs_line--;
1306 edit->total_lines--;
1307 edit->force |= REDRAW_AFTER_CURSOR;
1310 if (edit->curs1 < edit->start_display) {
1311 edit->start_display--;
1312 if (p == '\n')
1313 edit->start_line--;
1316 return p;
1319 #ifdef FAST_MOVE_CURSOR
1321 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1323 unsigned long next;
1324 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1325 edit->curs_line--;
1326 next -= (unsigned long) dest;
1327 n -= next;
1328 src += next;
1329 dest += next;
1334 edit_move_backward_lots (WEdit *edit, long increment)
1336 int r, s, t;
1337 unsigned char *p;
1339 if (increment > edit->curs1)
1340 increment = edit->curs1;
1341 if (increment <= 0)
1342 return -1;
1343 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1345 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1346 if (r > increment)
1347 r = increment;
1348 s = edit->curs1 & M_EDIT_BUF_SIZE;
1350 p = 0;
1351 if (s > r) {
1352 memqcpy (edit,
1353 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1354 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1356 } else {
1357 if (s) {
1358 memqcpy (edit,
1359 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1360 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1361 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1362 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1364 memqcpy (edit,
1365 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1366 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1367 EDIT_BUF_SIZE - (r - s), r - s);
1369 increment -= r;
1370 edit->curs1 -= r;
1371 edit->curs2 += r;
1372 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1373 if (p)
1374 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1375 else
1376 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1377 g_malloc (EDIT_BUF_SIZE);
1378 } else {
1379 g_free (p);
1382 s = edit->curs1 & M_EDIT_BUF_SIZE;
1383 while (increment) {
1384 p = 0;
1385 r = EDIT_BUF_SIZE;
1386 if (r > increment)
1387 r = increment;
1388 t = s;
1389 if (r < t)
1390 t = r;
1391 memqcpy (edit,
1392 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1393 EDIT_BUF_SIZE - t,
1394 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1396 if (r >= s) {
1397 if (t) {
1398 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1399 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1401 memqcpy (edit,
1402 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1403 EDIT_BUF_SIZE - r,
1404 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1405 EDIT_BUF_SIZE - (r - s), r - s);
1407 increment -= r;
1408 edit->curs1 -= r;
1409 edit->curs2 += r;
1410 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1411 if (p)
1412 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1413 else
1414 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1415 g_malloc (EDIT_BUF_SIZE);
1416 } else {
1417 g_free (p);
1420 return edit_get_byte (edit, edit->curs1);
1423 #endif /* ! FAST_MOVE_CURSOR */
1425 /* moves the cursor right or left: increment positive or negative respectively */
1426 void edit_cursor_move (WEdit * edit, long increment)
1428 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1429 int c;
1431 #ifdef FAST_MOVE_CURSOR
1432 if (increment < -256) {
1433 edit->force |= REDRAW_PAGE;
1434 edit_move_backward_lots (edit, -increment);
1435 return;
1437 #endif /* ! FAST_MOVE_CURSOR */
1439 if (increment < 0) {
1440 for (; increment < 0; increment++) {
1441 if (!edit->curs1)
1442 return;
1444 edit_push_action (edit, CURS_RIGHT);
1446 c = edit_get_byte (edit, edit->curs1 - 1);
1447 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1448 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1449 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1450 edit->curs2++;
1451 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1452 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1453 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1454 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1456 edit->curs1--;
1457 if (c == '\n') {
1458 edit->curs_line--;
1459 edit->force |= REDRAW_LINE_BELOW;
1463 } else if (increment > 0) {
1464 for (; increment > 0; increment--) {
1465 if (!edit->curs2)
1466 return;
1468 edit_push_action (edit, CURS_LEFT);
1470 c = edit_get_byte (edit, edit->curs1);
1471 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1472 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1473 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1474 edit->curs1++;
1475 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1476 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1477 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1478 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1480 edit->curs2--;
1481 if (c == '\n') {
1482 edit->curs_line++;
1483 edit->force |= REDRAW_LINE_ABOVE;
1489 /* These functions return positions relative to lines */
1491 /* returns index of last char on line + 1 */
1492 long edit_eol (WEdit * edit, long current)
1494 if (current < edit->last_byte) {
1495 for (;; current++)
1496 if (edit_get_byte (edit, current) == '\n')
1497 break;
1498 } else
1499 return edit->last_byte;
1500 return current;
1503 /* returns index of first char on line */
1504 long edit_bol (WEdit * edit, long current)
1506 if (current > 0) {
1507 for (;; current--)
1508 if (edit_get_byte (edit, current - 1) == '\n')
1509 break;
1510 } else
1511 return 0;
1512 return current;
1516 int edit_count_lines (WEdit * edit, long current, int upto)
1518 int lines = 0;
1519 if (upto > edit->last_byte)
1520 upto = edit->last_byte;
1521 if (current < 0)
1522 current = 0;
1523 while (current < upto)
1524 if (edit_get_byte (edit, current++) == '\n')
1525 lines++;
1526 return lines;
1530 /* If lines is zero this returns the count of lines from current to upto. */
1531 /* If upto is zero returns index of lines forward current. */
1532 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1534 if (upto) {
1535 return edit_count_lines (edit, current, upto);
1536 } else {
1537 int next;
1538 if (lines < 0)
1539 lines = 0;
1540 while (lines--) {
1541 next = edit_eol (edit, current) + 1;
1542 if (next > edit->last_byte)
1543 break;
1544 else
1545 current = next;
1547 return current;
1552 /* Returns offset of 'lines' lines up from current */
1553 long edit_move_backward (WEdit * edit, long current, int lines)
1555 if (lines < 0)
1556 lines = 0;
1557 current = edit_bol (edit, current);
1558 while((lines--) && current != 0)
1559 current = edit_bol (edit, current - 1);
1560 return current;
1563 /* If cols is zero this returns the count of columns from current to upto. */
1564 /* If upto is zero returns index of cols across from current. */
1565 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1567 long p, q;
1568 int col = 0;
1569 #ifdef HAVE_CHARSET
1570 int cw = 1;
1571 int utf_ch = 0;
1572 #endif
1573 if (upto) {
1574 q = upto;
1575 cols = -10;
1576 } else
1577 q = edit->last_byte + 2;
1578 for (col = 0, p = current; p < q; p++) {
1579 int c;
1580 #ifdef HAVE_CHARSET
1581 cw = 1;
1582 utf_ch = 0;
1583 #endif
1584 if (cols != -10) {
1585 if (col == cols)
1586 return p;
1587 if (col > cols)
1588 return p - 1;
1590 #ifdef HAVE_CHARSET
1591 if ( !edit->utf8 ) {
1592 #endif
1593 c = edit_get_byte (edit, p);
1594 #ifdef HAVE_CHARSET
1595 } else {
1596 cw = 1;
1597 c = edit_get_byte (edit, p);
1598 utf_ch = edit_get_utf (edit, p, &cw);
1600 if ( utf8_display ) {
1601 if ( edit->utf8 && g_unichar_iswide(utf_ch) )
1602 col++;
1604 #endif
1605 if (c == '\t')
1606 col += TAB_SIZE - col % TAB_SIZE;
1607 else if (c == '\n') {
1608 if (upto)
1609 return col;
1610 else
1611 return p;
1612 } else if (c < 32 || c == 127)
1613 col += 2; /* Caret notation for control characters */
1614 else
1615 col++;
1616 #ifdef HAVE_CHARSET
1617 if ( cw > 1 )
1618 col -= cw-1;
1619 #endif
1621 return col;
1624 /* returns the current column position of the cursor */
1625 int edit_get_col (WEdit * edit)
1627 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1631 /* Scrolling functions */
1633 void edit_update_curs_row (WEdit * edit)
1635 edit->curs_row = edit->curs_line - edit->start_line;
1638 void edit_update_curs_col (WEdit * edit)
1640 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1644 edit_get_curs_col (const WEdit *edit)
1646 return edit->curs_col;
1649 /*moves the display start position up by i lines */
1650 void edit_scroll_upward (WEdit * edit, unsigned long i)
1652 unsigned long lines_above = edit->start_line;
1653 if (i > lines_above)
1654 i = lines_above;
1655 if (i) {
1656 edit->start_line -= i;
1657 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1658 edit->force |= REDRAW_PAGE;
1659 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1661 edit_update_curs_row (edit);
1665 /* returns 1 if could scroll, 0 otherwise */
1666 void edit_scroll_downward (WEdit * edit, int i)
1668 int lines_below;
1669 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1670 if (lines_below > 0) {
1671 if (i > lines_below)
1672 i = lines_below;
1673 edit->start_line += i;
1674 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1675 edit->force |= REDRAW_PAGE;
1676 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1678 edit_update_curs_row (edit);
1681 void edit_scroll_right (WEdit * edit, int i)
1683 edit->force |= REDRAW_PAGE;
1684 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1685 edit->start_col -= i;
1688 void edit_scroll_left (WEdit * edit, int i)
1690 if (edit->start_col) {
1691 edit->start_col += i;
1692 if (edit->start_col > 0)
1693 edit->start_col = 0;
1694 edit->force |= REDRAW_PAGE;
1695 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1699 /* high level cursor movement commands */
1701 static int is_in_indent (WEdit *edit)
1703 long p = edit_bol (edit, edit->curs1);
1704 while (p < edit->curs1)
1705 if (!strchr (" \t", edit_get_byte (edit, p++)))
1706 return 0;
1707 return 1;
1710 static int left_of_four_spaces (WEdit *edit);
1712 void
1713 edit_move_to_prev_col (WEdit * edit, long p)
1715 int prev = edit->prev_col;
1716 int over = edit->over_col;
1718 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1720 if (option_cursor_beyond_eol) {
1721 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1723 if (line_len < prev + edit->over_col) {
1724 edit->over_col = prev + over - line_len;
1725 edit->prev_col = line_len;
1726 edit->curs_col = line_len;
1727 } else {
1728 edit->curs_col = prev + over;
1729 edit->prev_col = edit->curs_col;
1730 edit->over_col = 0;
1732 } else {
1733 edit->over_col = 0;
1734 if (is_in_indent (edit) && option_fake_half_tabs) {
1735 edit_update_curs_col (edit);
1736 if (space_width)
1737 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1738 int q = edit->curs_col;
1739 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1740 p = edit_bol (edit, edit->curs1);
1741 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1742 if (!left_of_four_spaces (edit))
1743 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1749 /* move i lines */
1750 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1752 unsigned long p, l = edit->curs_line;
1754 if (i > l)
1755 i = l;
1756 if (i) {
1757 if (i > 1)
1758 edit->force |= REDRAW_PAGE;
1759 if (scroll)
1760 edit_scroll_upward (edit, i);
1762 p = edit_bol (edit, edit->curs1);
1763 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1764 edit_move_to_prev_col (edit, p);
1766 edit->search_start = edit->curs1;
1767 edit->found_len = 0;
1771 static int
1772 is_blank (WEdit *edit, long offset)
1774 long s, f;
1775 int c;
1776 s = edit_bol (edit, offset);
1777 f = edit_eol (edit, offset) - 1;
1778 while (s <= f) {
1779 c = edit_get_byte (edit, s++);
1780 if (!isspace (c))
1781 return 0;
1783 return 1;
1787 /* returns the offset of line i */
1788 static long
1789 edit_find_line (WEdit *edit, int line)
1791 int i, j = 0;
1792 int m = 2000000000;
1793 if (!edit->caches_valid) {
1794 for (i = 0; i < N_LINE_CACHES; i++)
1795 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1796 /* three offsets that we *know* are line 0 at 0 and these two: */
1797 edit->line_numbers[1] = edit->curs_line;
1798 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1799 edit->line_numbers[2] = edit->total_lines;
1800 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1801 edit->caches_valid = 1;
1803 if (line >= edit->total_lines)
1804 return edit->line_offsets[2];
1805 if (line <= 0)
1806 return 0;
1807 /* find the closest known point */
1808 for (i = 0; i < N_LINE_CACHES; i++) {
1809 int n;
1810 n = abs (edit->line_numbers[i] - line);
1811 if (n < m) {
1812 m = n;
1813 j = i;
1816 if (m == 0)
1817 return edit->line_offsets[j]; /* know the offset exactly */
1818 if (m == 1 && j >= 3)
1819 i = j; /* one line different - caller might be looping, so stay in this cache */
1820 else
1821 i = 3 + (rand () % (N_LINE_CACHES - 3));
1822 if (line > edit->line_numbers[j])
1823 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1824 else
1825 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1826 edit->line_numbers[i] = line;
1827 return edit->line_offsets[i];
1830 int line_is_blank (WEdit * edit, long line)
1832 return is_blank (edit, edit_find_line (edit, line));
1835 /* moves up until a blank line is reached, or until just
1836 before a non-blank line is reached */
1837 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1839 int i;
1840 if (edit->curs_line <= 1) {
1841 i = 0;
1842 } else {
1843 if (line_is_blank (edit, edit->curs_line)) {
1844 if (line_is_blank (edit, edit->curs_line - 1)) {
1845 for (i = edit->curs_line - 1; i; i--)
1846 if (!line_is_blank (edit, i)) {
1847 i++;
1848 break;
1850 } else {
1851 for (i = edit->curs_line - 1; i; i--)
1852 if (line_is_blank (edit, i))
1853 break;
1855 } else {
1856 for (i = edit->curs_line - 1; i; i--)
1857 if (line_is_blank (edit, i))
1858 break;
1861 edit_move_up (edit, edit->curs_line - i, scroll);
1864 /* move i lines */
1865 void edit_move_down (WEdit * edit, int i, int scroll)
1867 long p, l = edit->total_lines - edit->curs_line;
1869 if (i > l)
1870 i = l;
1871 if (i) {
1872 if (i > 1)
1873 edit->force |= REDRAW_PAGE;
1874 if (scroll)
1875 edit_scroll_downward (edit, i);
1876 p = edit_bol (edit, edit->curs1);
1877 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1878 edit_move_to_prev_col (edit, p);
1880 edit->search_start = edit->curs1;
1881 edit->found_len = 0;
1885 /* moves down until a blank line is reached, or until just
1886 before a non-blank line is reached */
1887 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1889 int i;
1890 if (edit->curs_line >= edit->total_lines - 1) {
1891 i = edit->total_lines;
1892 } else {
1893 if (line_is_blank (edit, edit->curs_line)) {
1894 if (line_is_blank (edit, edit->curs_line + 1)) {
1895 for (i = edit->curs_line + 1; i; i++)
1896 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1897 i--;
1898 break;
1900 } else {
1901 for (i = edit->curs_line + 1; i; i++)
1902 if (line_is_blank (edit, i) || i >= edit->total_lines)
1903 break;
1905 } else {
1906 for (i = edit->curs_line + 1; i; i++)
1907 if (line_is_blank (edit, i) || i >= edit->total_lines)
1908 break;
1911 edit_move_down (edit, i - edit->curs_line, scroll);
1914 static void edit_begin_page (WEdit *edit)
1916 edit_update_curs_row (edit);
1917 edit_move_up (edit, edit->curs_row, 0);
1920 static void edit_end_page (WEdit *edit)
1922 edit_update_curs_row (edit);
1923 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1927 /* goto beginning of text */
1928 static void edit_move_to_top (WEdit * edit)
1930 if (edit->curs_line) {
1931 edit_cursor_move (edit, -edit->curs1);
1932 edit_move_to_prev_col (edit, 0);
1933 edit->force |= REDRAW_PAGE;
1934 edit->search_start = 0;
1935 edit_update_curs_row(edit);
1940 /* goto end of text */
1941 static void edit_move_to_bottom (WEdit * edit)
1943 if (edit->curs_line < edit->total_lines) {
1944 edit_cursor_move (edit, edit->curs2);
1945 edit->start_display = edit->last_byte;
1946 edit->start_line = edit->total_lines;
1947 edit_update_curs_row(edit);
1948 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1949 edit->force |= REDRAW_PAGE;
1953 /* goto beginning of line */
1954 static void edit_cursor_to_bol (WEdit * edit)
1956 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1957 edit->search_start = edit->curs1;
1958 edit->prev_col = edit_get_col (edit);
1959 edit->over_col = 0;
1962 /* goto end of line */
1963 static void edit_cursor_to_eol (WEdit * edit)
1965 edit_cursor_move (edit, edit_eol (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 /* move cursor to line 'line' */
1972 void edit_move_to_line (WEdit * e, long line)
1974 if(line < e->curs_line)
1975 edit_move_up (e, e->curs_line - line, 0);
1976 else
1977 edit_move_down (e, line - e->curs_line, 0);
1978 edit_scroll_screen_over_cursor (e);
1981 /* scroll window so that first visible line is 'line' */
1982 void edit_move_display (WEdit * e, long line)
1984 if(line < e->start_line)
1985 edit_scroll_upward (e, e->start_line - line);
1986 else
1987 edit_scroll_downward (e, line - e->start_line);
1990 /* save markers onto undo stack */
1991 void edit_push_markers (WEdit * edit)
1993 edit_push_action (edit, MARK_1 + edit->mark1);
1994 edit_push_action (edit, MARK_2 + edit->mark2);
1997 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1999 edit->mark1 = m1;
2000 edit->mark2 = m2;
2001 edit->column1 = c1;
2002 edit->column2 = c2;
2006 /* highlight marker toggle */
2007 void edit_mark_cmd (WEdit * edit, int unmark)
2009 edit_push_markers (edit);
2010 if (unmark) {
2011 edit_set_markers (edit, 0, 0, 0, 0);
2012 edit->force |= REDRAW_PAGE;
2013 } else {
2014 if (edit->mark2 >= 0) {
2015 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
2016 edit->force |= REDRAW_PAGE;
2017 } else
2018 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2022 static unsigned long
2023 my_type_of (int c)
2025 int x, r = 0;
2026 const char *p, *q;
2027 const char option_chars_move_whole_word[] =
2028 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2030 if (!c)
2031 return 0;
2032 if (c == '!') {
2033 if (*option_chars_move_whole_word == '!')
2034 return 2;
2035 return 0x80000000UL;
2037 if (g_ascii_isupper ((gchar) c))
2038 c = 'A';
2039 else if (g_ascii_islower ((gchar) c))
2040 c = 'a';
2041 else if (g_ascii_isalpha (c))
2042 c = 'a';
2043 else if (isdigit (c))
2044 c = '0';
2045 else if (isspace (c))
2046 c = ' ';
2047 q = strchr (option_chars_move_whole_word, c);
2048 if (!q)
2049 return 0xFFFFFFFFUL;
2050 do {
2051 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2052 if (*p == '!')
2053 x <<= 1;
2054 r |= x;
2055 } while ((q = strchr (q + 1, c)));
2056 return r;
2059 static void
2060 edit_left_word_move (WEdit *edit, int s)
2062 for (;;) {
2063 int c1, c2;
2064 edit_cursor_move (edit, -1);
2065 if (!edit->curs1)
2066 break;
2067 c1 = edit_get_byte (edit, edit->curs1 - 1);
2068 c2 = edit_get_byte (edit, edit->curs1);
2069 if (!(my_type_of (c1) & my_type_of (c2)))
2070 break;
2071 if (isspace (c1) && !isspace (c2))
2072 break;
2073 if (s)
2074 if (!isspace (c1) && isspace (c2))
2075 break;
2079 static void edit_left_word_move_cmd (WEdit * edit)
2081 edit_left_word_move (edit, 0);
2082 edit->force |= REDRAW_PAGE;
2085 static void
2086 edit_right_word_move (WEdit *edit, int s)
2088 for (;;) {
2089 int c1, c2;
2090 edit_cursor_move (edit, 1);
2091 if (edit->curs1 >= edit->last_byte)
2092 break;
2093 c1 = edit_get_byte (edit, edit->curs1 - 1);
2094 c2 = edit_get_byte (edit, edit->curs1);
2095 if (!(my_type_of (c1) & my_type_of (c2)))
2096 break;
2097 if (isspace (c1) && !isspace (c2))
2098 break;
2099 if (s)
2100 if (!isspace (c1) && isspace (c2))
2101 break;
2105 static void edit_right_word_move_cmd (WEdit * edit)
2107 edit_right_word_move (edit, 0);
2108 edit->force |= REDRAW_PAGE;
2111 static void edit_right_char_move_cmd (WEdit * edit)
2113 int cw = 1;
2114 int c = 0;
2115 if ( edit->utf8 ) {
2116 c = edit_get_utf (edit, edit->curs1, &cw);
2117 if ( cw < 1 )
2118 cw = 1;
2119 } else {
2120 c = edit_get_byte (edit, edit->curs1);
2122 if (option_cursor_beyond_eol && c == '\n') {
2123 edit->over_col++;
2124 } else {
2125 edit_cursor_move (edit, cw);
2129 static void edit_left_char_move_cmd (WEdit * edit)
2131 int cw = 1;
2132 if ( edit->utf8 ) {
2133 edit_get_prev_utf (edit, edit->curs1, &cw);
2134 if ( cw < 1 )
2135 cw = 1;
2137 if (option_cursor_beyond_eol && edit->over_col > 0) {
2138 edit->over_col--;
2139 } else {
2140 edit_cursor_move (edit, -cw);
2145 static void edit_right_delete_word (WEdit * edit)
2147 int c1, c2;
2148 for (;;) {
2149 if (edit->curs1 >= edit->last_byte)
2150 break;
2151 c1 = edit_delete (edit, 1);
2152 c2 = edit_get_byte (edit, edit->curs1);
2153 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2154 break;
2155 if (!(my_type_of (c1) & my_type_of (c2)))
2156 break;
2160 static void edit_left_delete_word (WEdit * edit)
2162 int c1, c2;
2163 for (;;) {
2164 if (edit->curs1 <= 0)
2165 break;
2166 c1 = edit_backspace (edit, 1);
2167 c2 = edit_get_byte (edit, edit->curs1 - 1);
2168 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2169 break;
2170 if (!(my_type_of (c1) & my_type_of (c2)))
2171 break;
2176 the start column position is not recorded, and hence does not
2177 undo as it happed. But who would notice.
2179 static void
2180 edit_do_undo (WEdit * edit)
2182 long ac;
2183 long count = 0;
2185 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2187 while ((ac = pop_action (edit)) < KEY_PRESS) {
2188 switch ((int) ac) {
2189 case STACK_BOTTOM:
2190 goto done_undo;
2191 case CURS_RIGHT:
2192 edit_cursor_move (edit, 1);
2193 break;
2194 case CURS_LEFT:
2195 edit_cursor_move (edit, -1);
2196 break;
2197 case BACKSPACE:
2198 edit_backspace (edit, 1);
2199 break;
2200 case DELCHAR:
2201 edit_delete (edit, 1);
2202 break;
2203 case COLUMN_ON:
2204 column_highlighting = 1;
2205 break;
2206 case COLUMN_OFF:
2207 column_highlighting = 0;
2208 break;
2210 if (ac >= 256 && ac < 512)
2211 edit_insert_ahead (edit, ac - 256);
2212 if (ac >= 0 && ac < 256)
2213 edit_insert (edit, ac);
2215 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2216 edit->mark1 = ac - MARK_1;
2217 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2218 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2219 edit->mark2 = ac - MARK_2;
2220 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2222 if (count++)
2223 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2226 if (edit->start_display > ac - KEY_PRESS) {
2227 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2228 edit->force |= REDRAW_PAGE;
2229 } else if (edit->start_display < ac - KEY_PRESS) {
2230 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2231 edit->force |= REDRAW_PAGE;
2233 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2234 edit_update_curs_row (edit);
2236 done_undo:;
2237 edit->stack_disable = 0;
2240 static void edit_delete_to_line_end (WEdit * edit)
2242 while (edit_get_byte (edit, edit->curs1) != '\n') {
2243 if (!edit->curs2)
2244 break;
2245 edit_delete (edit, 1);
2249 static void edit_delete_to_line_begin (WEdit * edit)
2251 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2252 if (!edit->curs1)
2253 break;
2254 edit_backspace (edit, 1);
2258 void
2259 edit_delete_line (WEdit *edit)
2262 * Delete right part of the line.
2263 * Note that edit_get_byte() returns '\n' when byte position is
2264 * beyond EOF.
2266 while (edit_get_byte (edit, edit->curs1) != '\n') {
2267 (void) edit_delete (edit, 1);
2271 * Delete '\n' char.
2272 * Note that edit_delete() will not corrupt anything if called while
2273 * cursor position is EOF.
2275 (void) edit_delete (edit, 1);
2278 * Delete left part of the line.
2279 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2281 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2282 (void) edit_backspace (edit, 1);
2286 void insert_spaces_tab (WEdit * edit, int half)
2288 int i;
2289 edit_update_curs_col (edit);
2290 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2291 while (i > 0) {
2292 edit_insert (edit, ' ');
2293 i -= space_width;
2297 static int is_aligned_on_a_tab (WEdit * edit)
2299 edit_update_curs_col (edit);
2300 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2301 return 0; /* not alligned on a tab */
2302 return 1;
2305 static int right_of_four_spaces (WEdit *edit)
2307 int i, ch = 0;
2308 for (i = 1; i <= HALF_TAB_SIZE; i++)
2309 ch |= edit_get_byte (edit, edit->curs1 - i);
2310 if (ch == ' ')
2311 return is_aligned_on_a_tab (edit);
2312 return 0;
2315 static int left_of_four_spaces (WEdit *edit)
2317 int i, ch = 0;
2318 for (i = 0; i < HALF_TAB_SIZE; i++)
2319 ch |= edit_get_byte (edit, edit->curs1 + i);
2320 if (ch == ' ')
2321 return is_aligned_on_a_tab (edit);
2322 return 0;
2325 int edit_indent_width (WEdit * edit, long p)
2327 long q = p;
2328 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2329 q++;
2330 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2333 void edit_insert_indent (WEdit * edit, int indent)
2335 if (!option_fill_tabs_with_spaces) {
2336 while (indent >= TAB_SIZE) {
2337 edit_insert (edit, '\t');
2338 indent -= TAB_SIZE;
2341 while (indent-- > 0)
2342 edit_insert (edit, ' ');
2345 static void
2346 edit_auto_indent (WEdit * edit)
2348 long p;
2349 char c;
2350 p = edit->curs1;
2351 /* use the previous line as a template */
2352 p = edit_move_backward (edit, p, 1);
2353 /* copy the leading whitespace of the line */
2354 for (;;) { /* no range check - the line _is_ \n-terminated */
2355 c = edit_get_byte (edit, p++);
2356 if (c != ' ' && c != '\t')
2357 break;
2358 edit_insert (edit, c);
2362 static void edit_double_newline (WEdit * edit)
2364 edit_insert (edit, '\n');
2365 if (edit_get_byte (edit, edit->curs1) == '\n')
2366 return;
2367 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2368 return;
2369 edit->force |= REDRAW_PAGE;
2370 edit_insert (edit, '\n');
2373 static void edit_tab_cmd (WEdit * edit)
2375 int i;
2377 if (option_fake_half_tabs) {
2378 if (is_in_indent (edit)) {
2379 /*insert a half tab (usually four spaces) unless there is a
2380 half tab already behind, then delete it and insert a
2381 full tab. */
2382 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2383 for (i = 1; i <= HALF_TAB_SIZE; i++)
2384 edit_backspace (edit, 1);
2385 edit_insert (edit, '\t');
2386 } else {
2387 insert_spaces_tab (edit, 1);
2389 return;
2392 if (option_fill_tabs_with_spaces) {
2393 insert_spaces_tab (edit, 0);
2394 } else {
2395 edit_insert (edit, '\t');
2397 return;
2400 static void check_and_wrap_line (WEdit * edit)
2402 int curs, c;
2403 if (!option_typewriter_wrap)
2404 return;
2405 edit_update_curs_col (edit);
2406 if (edit->curs_col < option_word_wrap_line_length)
2407 return;
2408 curs = edit->curs1;
2409 for (;;) {
2410 curs--;
2411 c = edit_get_byte (edit, curs);
2412 if (c == '\n' || curs <= 0) {
2413 edit_insert (edit, '\n');
2414 return;
2416 if (c == ' ' || c == '\t') {
2417 int current = edit->curs1;
2418 edit_cursor_move (edit, curs - edit->curs1 + 1);
2419 edit_insert (edit, '\n');
2420 edit_cursor_move (edit, current - edit->curs1 + 1);
2421 return;
2426 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2428 void edit_push_key_press (WEdit * edit)
2430 edit_push_action (edit, KEY_PRESS + edit->start_display);
2431 if (edit->mark2 == -1)
2432 edit_push_action (edit, MARK_1 + edit->mark1);
2435 /* this find the matching bracket in either direction, and sets edit->bracket */
2436 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2438 const char * const b = "{}{[][()(", *p;
2439 int i = 1, a, inc = -1, c, d, n = 0;
2440 unsigned long j = 0;
2441 long q;
2442 edit_update_curs_row (edit);
2443 c = edit_get_byte (edit, edit->curs1);
2444 p = strchr (b, c);
2445 /* no limit */
2446 if (!furthest_bracket_search)
2447 furthest_bracket_search--;
2448 /* not on a bracket at all */
2449 if (!p)
2450 return -1;
2451 /* the matching bracket */
2452 d = p[1];
2453 /* going left or right? */
2454 if (strchr ("{[(", c))
2455 inc = 1;
2456 for (q = edit->curs1 + inc;; q += inc) {
2457 /* out of buffer? */
2458 if (q >= edit->last_byte || q < 0)
2459 break;
2460 a = edit_get_byte (edit, q);
2461 /* don't want to eat CPU */
2462 if (j++ > furthest_bracket_search)
2463 break;
2464 /* out of screen? */
2465 if (in_screen) {
2466 if (q < edit->start_display)
2467 break;
2468 /* count lines if searching downward */
2469 if (inc > 0 && a == '\n')
2470 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2471 break;
2473 /* count bracket depth */
2474 i += (a == c) - (a == d);
2475 /* return if bracket depth is zero */
2476 if (!i)
2477 return q;
2479 /* no match */
2480 return -1;
2483 static long last_bracket = -1;
2485 void edit_find_bracket (WEdit * edit)
2487 edit->bracket = edit_get_bracket (edit, 1, 10000);
2488 if (last_bracket != edit->bracket)
2489 edit->force |= REDRAW_PAGE;
2490 last_bracket = edit->bracket;
2493 static void edit_goto_matching_bracket (WEdit *edit)
2495 long q;
2496 q = edit_get_bracket (edit, 0, 0);
2497 if (q < 0)
2498 return;
2499 edit->bracket = edit->curs1;
2500 edit->force |= REDRAW_PAGE;
2501 edit_cursor_move (edit, q - edit->curs1);
2505 * This executes a command as though the user initiated it through a key
2506 * press. Callback with WIDGET_KEY as a message calls this after
2507 * translating the key press. This function can be used to pass any
2508 * command to the editor. Note that the screen wouldn't update
2509 * automatically. Either of command or char_for_insertion must be
2510 * passed as -1. Commands are executed, and char_for_insertion is
2511 * inserted at the cursor.
2513 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2515 if (command == CK_Begin_Record_Macro) {
2516 edit->macro_i = 0;
2517 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2518 return;
2520 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2521 edit->force |= REDRAW_COMPLETELY;
2522 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2523 edit->macro_i = -1;
2524 return;
2526 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2527 edit->macro[edit->macro_i].command = command;
2528 edit->macro[edit->macro_i++].ch = char_for_insertion;
2530 /* record the beginning of a set of editing actions initiated by a key press */
2531 if (command != CK_Undo && command != CK_Ext_Mode)
2532 edit_push_key_press (edit);
2534 edit_execute_cmd (edit, command, char_for_insertion);
2535 if (column_highlighting)
2536 edit->force |= REDRAW_PAGE;
2539 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2542 This executes a command at a lower level than macro recording.
2543 It also does not push a key_press onto the undo stack. This means
2544 that if it is called many times, a single undo command will undo
2545 all of them. It also does not check for the Undo command.
2547 void
2548 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2550 edit->force |= REDRAW_LINE;
2552 /* The next key press will unhighlight the found string, so update
2553 * the whole page */
2554 if (edit->found_len || column_highlighting)
2555 edit->force |= REDRAW_PAGE;
2557 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2558 column_highlighting = 0;
2559 if (!edit->highlight
2560 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2561 edit_mark_cmd (edit, 1); /* clear */
2562 edit_mark_cmd (edit, 0); /* marking on */
2564 edit->highlight = 1;
2565 } else { /* any other command */
2566 if (edit->highlight)
2567 edit_mark_cmd (edit, 0); /* clear */
2568 edit->highlight = 0;
2571 /* first check for undo */
2572 if (command == CK_Undo) {
2573 edit_do_undo (edit);
2574 edit->found_len = 0;
2575 edit->prev_col = edit_get_col (edit);
2576 edit->search_start = edit->curs1;
2577 return;
2580 /* An ordinary key press */
2581 if (char_for_insertion >= 0) {
2582 if (edit->overwrite) {
2583 if (edit_get_byte (edit, edit->curs1) != '\n')
2584 edit_delete (edit, 0);
2586 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2587 edit_insert_over (edit);
2588 #ifdef HAVE_CHARSET
2589 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2590 unsigned char str[6 + 1];
2591 size_t i = 0;
2592 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2593 if ( res == 0 ) {
2594 str[0] = '.';
2595 str[1] = '\0';
2596 } else {
2597 str[res] = '\0';
2599 while ( str[i] != 0 && i<=6) {
2600 char_for_insertion = str[i];
2601 edit_insert (edit, char_for_insertion);
2602 i++;
2604 } else {
2605 #endif
2606 edit_insert (edit, char_for_insertion);
2607 #ifdef HAVE_CHARSET
2609 #endif
2610 if (option_auto_para_formatting) {
2611 format_paragraph (edit, 0);
2612 edit->force |= REDRAW_PAGE;
2613 } else
2614 check_and_wrap_line (edit);
2615 edit->found_len = 0;
2616 edit->prev_col = edit_get_col (edit);
2617 edit->search_start = edit->curs1;
2618 edit_find_bracket (edit);
2619 return;
2622 switch (command) {
2623 case CK_Begin_Page:
2624 case CK_End_Page:
2625 case CK_Begin_Page_Highlight:
2626 case CK_End_Page_Highlight:
2627 case CK_Word_Left:
2628 case CK_Word_Right:
2629 case CK_Up:
2630 case CK_Down:
2631 case CK_Left:
2632 case CK_Right:
2633 if ( edit->mark2 >= 0 ) {
2634 if ( !option_persistent_selections ) {
2635 if (column_highlighting)
2636 edit_push_action (edit, COLUMN_ON);
2637 column_highlighting = 0;
2638 edit_mark_cmd (edit, 1);
2643 switch (command) {
2644 case CK_Begin_Page:
2645 case CK_End_Page:
2646 case CK_Begin_Page_Highlight:
2647 case CK_End_Page_Highlight:
2648 case CK_Word_Left:
2649 case CK_Word_Right:
2650 case CK_Up:
2651 case CK_Down:
2652 case CK_Word_Left_Highlight:
2653 case CK_Word_Right_Highlight:
2654 case CK_Up_Highlight:
2655 case CK_Down_Highlight:
2656 case CK_Up_Alt_Highlight:
2657 case CK_Down_Alt_Highlight:
2658 if (edit->mark2 == -1)
2659 break; /*marking is following the cursor: may need to highlight a whole line */
2660 case CK_Left:
2661 case CK_Right:
2662 case CK_Left_Highlight:
2663 case CK_Right_Highlight:
2664 edit->force |= REDRAW_CHAR_ONLY;
2667 /* basic cursor key commands */
2668 switch (command) {
2669 case CK_BackSpace:
2670 /* if non persistent selection and text selected */
2671 if ( !option_persistent_selections ) {
2672 if ( edit->mark1 != edit->mark2 ) {
2673 edit_block_delete_cmd (edit);
2674 break;
2677 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2678 edit->over_col--;
2679 break;
2681 if (option_backspace_through_tabs && is_in_indent (edit)) {
2682 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2683 && edit->curs1 > 0)
2684 edit_backspace (edit, 1);
2685 break;
2686 } else {
2687 if (option_fake_half_tabs) {
2688 int i;
2689 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2690 for (i = 0; i < HALF_TAB_SIZE; i++)
2691 edit_backspace (edit, 1);
2692 break;
2696 edit_backspace (edit, 0);
2697 break;
2698 case CK_Delete:
2699 /* if non persistent selection and text selected */
2700 if ( !option_persistent_selections ) {
2701 if ( edit->mark1 != edit->mark2 ) {
2702 edit_block_delete_cmd (edit);
2703 break;
2707 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2708 edit_insert_over (edit);
2710 if (option_fake_half_tabs) {
2711 int i;
2712 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2713 for (i = 1; i <= HALF_TAB_SIZE; i++)
2714 edit_delete (edit, 1);
2715 break;
2718 edit_delete (edit, 0);
2719 break;
2720 case CK_Delete_Word_Left:
2721 edit_left_delete_word (edit);
2722 break;
2723 case CK_Delete_Word_Right:
2724 edit_right_delete_word (edit);
2725 break;
2726 case CK_Delete_Line:
2727 edit_delete_line (edit);
2728 break;
2729 case CK_Delete_To_Line_End:
2730 edit_delete_to_line_end (edit);
2731 break;
2732 case CK_Delete_To_Line_Begin:
2733 edit_delete_to_line_begin (edit);
2734 break;
2735 case CK_Enter:
2736 edit->over_col = 0;
2737 if (option_auto_para_formatting) {
2738 edit_double_newline (edit);
2739 if (option_return_does_auto_indent)
2740 edit_auto_indent (edit);
2741 format_paragraph (edit, 0);
2742 } else {
2743 edit_insert (edit, '\n');
2744 if (option_return_does_auto_indent) {
2745 edit_auto_indent (edit);
2748 break;
2749 case CK_Return:
2750 edit_insert (edit, '\n');
2751 break;
2753 case CK_Page_Up_Alt_Highlight:
2754 column_highlighting = 1;
2755 case CK_Page_Up:
2756 case CK_Page_Up_Highlight:
2757 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2758 break;
2759 case CK_Page_Down_Alt_Highlight:
2760 column_highlighting = 1;
2761 case CK_Page_Down:
2762 case CK_Page_Down_Highlight:
2763 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2764 break;
2765 case CK_Left_Alt_Highlight:
2766 column_highlighting = 1;
2767 case CK_Left:
2768 case CK_Left_Highlight:
2769 if (option_fake_half_tabs) {
2770 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2771 if ( option_cursor_beyond_eol && edit->over_col > 0)
2772 edit->over_col--;
2773 else
2774 edit_cursor_move (edit, -HALF_TAB_SIZE);
2775 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2776 break;
2779 edit_left_char_move_cmd (edit);
2780 break;
2781 case CK_Right_Alt_Highlight:
2782 column_highlighting = 1;
2783 case CK_Right:
2784 case CK_Right_Highlight:
2785 if (option_fake_half_tabs) {
2786 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2787 edit_cursor_move (edit, HALF_TAB_SIZE);
2788 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2789 break;
2792 edit_right_char_move_cmd (edit);
2793 break;
2794 case CK_Begin_Page:
2795 case CK_Begin_Page_Highlight:
2796 edit_begin_page (edit);
2797 break;
2798 case CK_End_Page:
2799 case CK_End_Page_Highlight:
2800 edit_end_page (edit);
2801 break;
2802 case CK_Word_Left:
2803 case CK_Word_Left_Highlight:
2804 edit_left_word_move_cmd (edit);
2805 break;
2806 case CK_Word_Right:
2807 case CK_Word_Right_Highlight:
2808 edit_right_word_move_cmd (edit);
2809 break;
2810 case CK_Up_Alt_Highlight:
2811 column_highlighting = 1;
2812 case CK_Up:
2813 case CK_Up_Highlight:
2814 edit_move_up (edit, 1, 0);
2815 break;
2816 case CK_Down_Alt_Highlight:
2817 column_highlighting = 1;
2818 case CK_Down:
2819 case CK_Down_Highlight:
2820 edit_move_down (edit, 1, 0);
2821 break;
2822 case CK_Paragraph_Up_Alt_Highlight:
2823 column_highlighting = 1;
2824 case CK_Paragraph_Up:
2825 case CK_Paragraph_Up_Highlight:
2826 edit_move_up_paragraph (edit, 0);
2827 break;
2828 case CK_Paragraph_Down_Alt_Highlight:
2829 column_highlighting = 1;
2830 case CK_Paragraph_Down:
2831 case CK_Paragraph_Down_Highlight:
2832 edit_move_down_paragraph (edit, 0);
2833 break;
2834 case CK_Scroll_Up_Alt_Highlight:
2835 column_highlighting = 1;
2836 case CK_Scroll_Up:
2837 case CK_Scroll_Up_Highlight:
2838 edit_move_up (edit, 1, 1);
2839 break;
2840 case CK_Scroll_Down_Alt_Highlight:
2841 column_highlighting = 1;
2842 case CK_Scroll_Down:
2843 case CK_Scroll_Down_Highlight:
2844 edit_move_down (edit, 1, 1);
2845 break;
2846 case CK_Home:
2847 case CK_Home_Highlight:
2848 edit_cursor_to_bol (edit);
2849 break;
2850 case CK_End:
2851 case CK_End_Highlight:
2852 edit_cursor_to_eol (edit);
2853 break;
2854 case CK_Tab:
2855 /* if text marked shift block */
2856 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2857 edit_move_block_to_right (edit);
2858 } else {
2859 if ( option_cursor_beyond_eol )
2860 edit_insert_over (edit);
2861 edit_tab_cmd (edit);
2862 if (option_auto_para_formatting) {
2863 format_paragraph (edit, 0);
2864 edit->force |= REDRAW_PAGE;
2865 } else {
2866 check_and_wrap_line (edit);
2869 break;
2871 case CK_Toggle_Insert:
2872 edit->overwrite = (edit->overwrite == 0);
2873 break;
2875 case CK_Mark:
2876 if (edit->mark2 >= 0) {
2877 if (column_highlighting)
2878 edit_push_action (edit, COLUMN_ON);
2879 column_highlighting = 0;
2881 edit_mark_cmd (edit, 0);
2882 break;
2883 case CK_Column_Mark:
2884 if (!column_highlighting)
2885 edit_push_action (edit, COLUMN_OFF);
2886 column_highlighting = 1;
2887 edit_mark_cmd (edit, 0);
2888 break;
2889 case CK_Unmark:
2890 if (column_highlighting)
2891 edit_push_action (edit, COLUMN_ON);
2892 column_highlighting = 0;
2893 edit_mark_cmd (edit, 1);
2894 break;
2896 case CK_Toggle_Line_State:
2897 option_line_state = !option_line_state;
2898 if ( option_line_state ) {
2899 option_line_state_width = LINE_STATE_WIDTH;
2900 } else {
2901 option_line_state_width = 0;
2903 edit->force |= REDRAW_PAGE;
2904 break;
2906 case CK_Toggle_Bookmark:
2907 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2908 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2909 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2910 else
2911 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2912 break;
2913 case CK_Flush_Bookmarks:
2914 book_mark_flush (edit, BOOK_MARK_COLOR);
2915 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2916 edit->force |= REDRAW_PAGE;
2917 break;
2918 case CK_Next_Bookmark:
2919 if (edit->book_mark) {
2920 struct _book_mark *p;
2921 p = (struct _book_mark *) book_mark_find (edit,
2922 edit->curs_line);
2923 if (p->next) {
2924 p = p->next;
2925 if (p->line >= edit->start_line + edit->num_widget_lines
2926 || p->line < edit->start_line)
2927 edit_move_display (edit,
2928 p->line -
2929 edit->num_widget_lines / 2);
2930 edit_move_to_line (edit, p->line);
2933 break;
2934 case CK_Prev_Bookmark:
2935 if (edit->book_mark) {
2936 struct _book_mark *p;
2937 p = (struct _book_mark *) book_mark_find (edit,
2938 edit->curs_line);
2939 while (p->line == edit->curs_line)
2940 if (p->prev)
2941 p = p->prev;
2942 if (p->line >= 0) {
2943 if (p->line >= edit->start_line + edit->num_widget_lines
2944 || p->line < edit->start_line)
2945 edit_move_display (edit,
2946 p->line -
2947 edit->num_widget_lines / 2);
2948 edit_move_to_line (edit, p->line);
2951 break;
2953 case CK_Beginning_Of_Text:
2954 case CK_Beginning_Of_Text_Highlight:
2955 edit_move_to_top (edit);
2956 break;
2957 case CK_End_Of_Text:
2958 case CK_End_Of_Text_Highlight:
2959 edit_move_to_bottom (edit);
2960 break;
2962 case CK_Copy:
2963 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2964 edit_insert_over (edit);
2965 edit_block_copy_cmd (edit);
2966 break;
2967 case CK_Remove:
2968 edit_block_delete_cmd (edit);
2969 break;
2970 case CK_Move:
2971 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2972 edit_insert_over (edit);
2973 edit_block_move_cmd (edit);
2974 break;
2976 case CK_XStore:
2977 edit_copy_to_X_buf_cmd (edit);
2978 break;
2979 case CK_XCut:
2980 edit_cut_to_X_buf_cmd (edit);
2981 break;
2982 case CK_XPaste:
2983 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2984 edit_insert_over (edit);
2985 edit_paste_from_X_buf_cmd (edit);
2986 break;
2987 case CK_Selection_History:
2988 edit_paste_from_history (edit);
2989 break;
2991 case CK_Save_As:
2992 edit_save_as_cmd (edit);
2993 break;
2994 case CK_Save:
2995 edit_save_confirm_cmd (edit);
2996 break;
2997 case CK_Load:
2998 edit_load_cmd (edit, EDIT_FILE_COMMON);
2999 break;
3000 case CK_Save_Block:
3001 edit_save_block_cmd (edit);
3002 break;
3003 case CK_Insert_File:
3004 edit_insert_file_cmd (edit);
3005 break;
3007 case CK_Load_Prev_File:
3008 edit_load_back_cmd (edit);
3009 break;
3010 case CK_Load_Next_File:
3011 edit_load_forward_cmd (edit);
3012 break;
3014 case CK_Load_Syntax_File:
3015 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3016 break;
3017 case CK_Load_Menu_File:
3018 edit_load_cmd (edit, EDIT_FILE_MENU);
3019 break;
3021 case CK_Toggle_Syntax:
3022 if ((option_syntax_highlighting ^= 1) == 1)
3023 edit_load_syntax (edit, NULL, option_syntax_type);
3024 edit->force |= REDRAW_PAGE;
3025 break;
3027 case CK_Toggle_Tab_TWS:
3028 enable_show_tabs_tws ^= 1;
3029 edit->force |= REDRAW_PAGE;
3030 break;
3032 case CK_Find:
3033 edit_search_cmd (edit, 0);
3034 break;
3035 case CK_Find_Again:
3036 edit_search_cmd (edit, 1);
3037 break;
3038 case CK_Replace:
3039 edit_replace_cmd (edit, 0);
3040 break;
3041 case CK_Replace_Again:
3042 edit_replace_cmd (edit, 1);
3043 break;
3044 case CK_Complete_Word:
3045 /* if text marked shift block */
3046 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3047 edit_move_block_to_left (edit);
3048 } else {
3049 edit_complete_word_cmd (edit);
3051 break;
3052 case CK_Find_Definition:
3053 edit_get_match_keyword_cmd (edit);
3054 break;
3056 case CK_Exit:
3057 dlg_stop (edit->widget.parent);
3058 break;
3059 case CK_New:
3060 edit_new_cmd (edit);
3061 break;
3063 case CK_Help:
3064 edit_help_cmd (edit);
3065 break;
3067 case CK_Refresh:
3068 edit_refresh_cmd (edit);
3069 break;
3071 case CK_Date:{
3072 char s[1024];
3073 /* fool gcc to prevent a Y2K warning */
3074 char time_format[] = "_c";
3075 time_format[0] = '%';
3077 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3078 edit_print_string (edit, s);
3079 edit->force |= REDRAW_PAGE;
3080 break;
3082 case CK_Goto:
3083 edit_goto_cmd (edit);
3084 break;
3085 case CK_Paragraph_Format:
3086 format_paragraph (edit, 1);
3087 edit->force |= REDRAW_PAGE;
3088 break;
3089 case CK_Delete_Macro:
3090 edit_delete_macro_cmd (edit);
3091 break;
3092 case CK_Match_Bracket:
3093 edit_goto_matching_bracket (edit);
3094 break;
3095 case CK_User_Menu:
3096 user_menu (edit);
3097 break;
3098 case CK_Sort:
3099 edit_sort_cmd (edit);
3100 break;
3101 case CK_ExtCmd:
3102 edit_ext_cmd (edit);
3103 break;
3104 case CK_Mail:
3105 edit_mail_dialog (edit);
3106 break;
3107 case CK_Shell:
3108 view_other_cmd ();
3109 break;
3110 case CK_Select_Codepage:
3111 edit_select_codepage_cmd (edit);
3112 break;
3113 case CK_Insert_Literal:
3114 edit_insert_literal_cmd (edit);
3115 break;
3116 case CK_Execute_Macro:
3117 edit_execute_macro_cmd (edit);
3118 break;
3119 case CK_Begin_End_Macro:
3120 edit_begin_end_macro_cmd (edit);
3121 break;
3122 case CK_Ext_Mode:
3123 edit->extmod = 1;
3124 break;
3125 default:
3126 break;
3129 /* CK_Pipe_Block */
3130 if ((command / 1000) == 1) /* a shell command */
3131 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3132 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3133 struct macro m[MAX_MACRO_LENGTH];
3134 int nm;
3135 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3136 edit_execute_macro (edit, m, nm);
3139 /* keys which must set the col position, and the search vars */
3140 switch (command) {
3141 case CK_Find:
3142 case CK_Find_Again:
3143 case CK_Replace:
3144 case CK_Replace_Again:
3145 case CK_Complete_Word:
3146 edit->prev_col = edit_get_col (edit);
3147 break;
3148 case CK_Up:
3149 case CK_Up_Highlight:
3150 case CK_Up_Alt_Highlight:
3151 case CK_Down:
3152 case CK_Down_Highlight:
3153 case CK_Down_Alt_Highlight:
3154 case CK_Page_Up:
3155 case CK_Page_Up_Highlight:
3156 case CK_Page_Up_Alt_Highlight:
3157 case CK_Page_Down:
3158 case CK_Page_Down_Highlight:
3159 case CK_Page_Down_Alt_Highlight:
3160 case CK_Beginning_Of_Text:
3161 case CK_Beginning_Of_Text_Highlight:
3162 case CK_End_Of_Text:
3163 case CK_End_Of_Text_Highlight:
3164 case CK_Paragraph_Up:
3165 case CK_Paragraph_Up_Highlight:
3166 case CK_Paragraph_Up_Alt_Highlight:
3167 case CK_Paragraph_Down:
3168 case CK_Paragraph_Down_Highlight:
3169 case CK_Paragraph_Down_Alt_Highlight:
3170 case CK_Scroll_Up:
3171 case CK_Scroll_Up_Highlight:
3172 case CK_Scroll_Up_Alt_Highlight:
3173 case CK_Scroll_Down:
3174 case CK_Scroll_Down_Highlight:
3175 case CK_Scroll_Down_Alt_Highlight:
3176 edit->search_start = edit->curs1;
3177 edit->found_len = 0;
3178 break;
3179 default:
3180 edit->found_len = 0;
3181 edit->prev_col = edit_get_col (edit);
3182 edit->search_start = edit->curs1;
3184 edit_find_bracket (edit);
3186 if (option_auto_para_formatting) {
3187 switch (command) {
3188 case CK_BackSpace:
3189 case CK_Delete:
3190 case CK_Delete_Word_Left:
3191 case CK_Delete_Word_Right:
3192 case CK_Delete_To_Line_End:
3193 case CK_Delete_To_Line_Begin:
3194 format_paragraph (edit, 0);
3195 edit->force |= REDRAW_PAGE;
3201 static void
3202 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3204 int i = 0;
3206 if (edit->macro_depth++ > 256) {
3207 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3208 edit->macro_depth--;
3209 return;
3211 edit->force |= REDRAW_PAGE;
3212 for (; i < n; i++) {
3213 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3215 edit_update_screen (edit);
3216 edit->macro_depth--;
3219 /* User edit menu, like user menu (F2) but only in editor. */
3220 static void
3221 user_menu (WEdit * edit)
3223 FILE *fd;
3224 int nomark;
3225 struct stat status;
3226 long start_mark, end_mark;
3227 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3228 int rc = 0;
3230 nomark = eval_marks (edit, &start_mark, &end_mark);
3231 if (!nomark) /* remember marked or not */
3232 edit_save_block (edit, block_file, start_mark, end_mark);
3234 /* run shell scripts from menu */
3235 user_menu_cmd (edit);
3237 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3238 /* no block messages */
3239 goto cleanup;
3242 if (!nomark) {
3243 /* i.e. we have marked block */
3244 rc = edit_block_delete_cmd (edit);
3247 if (!rc) {
3248 edit_insert_file (edit, block_file);
3251 /* truncate block file */
3252 if ((fd = fopen (block_file, "w"))) {
3253 fclose (fd);
3256 edit_refresh_cmd (edit);
3257 edit->force |= REDRAW_COMPLETELY;
3259 cleanup:
3260 g_free (block_file);
3263 void
3264 edit_stack_init (void)
3266 for (edit_stack_iterator = 0;
3267 edit_stack_iterator < MAX_HISTORY_MOVETO;
3268 edit_stack_iterator++ ) {
3269 edit_history_moveto[edit_stack_iterator].filename = NULL;
3270 edit_history_moveto[edit_stack_iterator].line = -1;
3273 edit_stack_iterator = 0;
3276 void
3277 edit_stack_free (void)
3279 for (edit_stack_iterator = 0;
3280 edit_stack_iterator < MAX_HISTORY_MOVETO;
3281 edit_stack_iterator++)
3282 g_free (edit_history_moveto[edit_stack_iterator].filename);