Fixed show of viewer status bar.
[midnight-commander.git] / edit / edit.c
blobb8af0a361b9d73f66d9539e57322ff37d038dfaa
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 "../src/tty/color.h"
45 #include "../src/tty/tty.h" /* attrset() */
46 #include "../src/tty/key.h" /* is_idle() */
48 #include "../src/skin/skin.h" /* EDITOR_NORMAL_COLOR */
50 #include "../src/widget.h"
51 #include "../src/cmd.h" /* view_other_cmd() */
52 #include "../src/user.h" /* user_menu_cmd() */
53 #include "../src/wtools.h" /* query_dialog() */
54 #include "../src/timefmt.h" /* time formatting */
55 #include "../src/strutil.h" /* utf string functions */
56 #include "../src/charsets.h" /* get_codepage_id */
57 #include "../src/main.h" /* source_codepage */
58 #include "../src/learn.h" /* learn_keys */
59 #include "../src/cmddef.h"
61 #include "edit-impl.h"
62 #include "editlock.h"
63 #include "edit-widget.h"
65 #include "../vfs/vfs.h"
67 int option_word_wrap_line_length = 72;
68 int option_typewriter_wrap = 0;
69 int option_auto_para_formatting = 0;
70 int option_fill_tabs_with_spaces = 0;
71 int option_return_does_auto_indent = 1;
72 int option_backspace_through_tabs = 0;
73 int option_fake_half_tabs = 1;
74 int option_save_mode = EDIT_QUICK_SAVE;
75 int option_save_position = 1;
76 int option_max_undo = 32768;
77 int option_persistent_selections = 1;
78 int option_cursor_beyond_eol = 1;
79 int option_line_state = 0;
80 int option_line_state_width = 0;
82 int option_edit_right_extreme = 0;
83 int option_edit_left_extreme = 0;
84 int option_edit_top_extreme = 0;
85 int option_edit_bottom_extreme = 0;
86 int enable_show_tabs_tws = 1;
87 int option_check_nl_at_eof = 0;
88 int show_right_margin = 0;
90 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
91 char *option_backup_ext = NULL;
93 int edit_stack_iterator = 0;
94 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
95 /* magic sequense for say than block is vertical */
96 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
97 /*-
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \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
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
112 * ^ | ^
113 * | ^|^ |
114 * cursor ||| cursor
115 * |||
116 * file end|||file beginning
121 * This_is_some_file
122 * fin.
125 const global_keymap_t *editor_map;
126 const global_keymap_t *editor_x_map;
128 static void user_menu (WEdit *edit);
130 int edit_get_byte (WEdit * edit, long byte_index)
132 unsigned long p;
133 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
134 return '\n';
136 if (byte_index >= edit->curs1) {
137 p = edit->curs1 + edit->curs2 - byte_index - 1;
138 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
139 } else {
140 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
144 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
146 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--;
165 if (byte_index < 0)
166 return NULL;
168 if (byte_index >= edit->curs1) {
169 p = edit->curs1 + edit->curs2 - 1;
170 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
171 } else {
172 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
176 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
178 gchar *str = NULL;
179 int res = -1;
180 gunichar ch;
181 gchar *next_ch = NULL;
182 int width = 0;
184 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
185 *char_width = 0;
186 return '\n';
189 str = edit_get_byte_ptr (edit, byte_index);
191 if (str == NULL) {
192 *char_width = 0;
193 return 0;
196 res = g_utf8_get_char_validated (str, -1);
198 if (res < 0) {
199 ch = *str;
200 width = 0;
201 } else {
202 ch = res;
203 /* Calculate UTF-8 char width */
204 next_ch = g_utf8_next_char (str);
205 if (next_ch) {
206 width = next_ch - str;
207 } else {
208 ch = 0;
209 width = 0;
212 *char_width = width;
213 return ch;
216 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
218 gchar *str, *buf = NULL;
219 int res = -1;
220 gunichar ch;
221 gchar *next_ch = NULL;
222 int width = 0;
224 if (byte_index > 0)
225 byte_index--;
227 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
228 *char_width = 0;
229 return 0;
232 ch = edit_get_utf (edit, byte_index, &width);
234 if (width == 1) {
235 *char_width = width;
236 return ch;
239 str = edit_get_byte_ptr (edit, byte_index);
240 buf = edit_get_buf_ptr (edit, byte_index);
241 if (str == NULL || buf == NULL) {
242 *char_width = 0;
243 return 0;
245 /* get prev utf8 char */
246 if (str != buf)
247 str = g_utf8_find_prev_char (buf, str);
249 res = g_utf8_get_char_validated (str, -1);
251 if (res < 0) {
252 ch = *str;
253 width = 0;
254 } else {
255 ch = res;
256 /* Calculate UTF-8 char width */
257 next_ch = g_utf8_next_char(str);
258 if (next_ch) {
259 width = next_ch - str;
260 } else {
261 ch = 0;
262 width = 0;
265 *char_width = width;
266 return ch;
270 * Initialize the buffers for an empty files.
272 static void
273 edit_init_buffers (WEdit *edit)
275 int j;
277 for (j = 0; j <= MAXBUFF; j++) {
278 edit->buffers1[j] = NULL;
279 edit->buffers2[j] = NULL;
282 edit->curs1 = 0;
283 edit->curs2 = 0;
284 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
288 * Load file OR text into buffers. Set cursor to the beginning of file.
289 * Return 1 on error.
291 static int
292 edit_load_file_fast (WEdit *edit, const char *filename)
294 long buf, buf2;
295 int file = -1;
296 edit->curs2 = edit->last_byte;
297 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
298 edit->utf8 = 0;
299 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
300 GString *errmsg = g_string_new(NULL);
301 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
302 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
303 g_string_free (errmsg, TRUE);
304 return 1;
307 if (!edit->buffers2[buf2])
308 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
310 mc_read (file,
311 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
312 (edit->curs2 & M_EDIT_BUF_SIZE),
313 edit->curs2 & M_EDIT_BUF_SIZE);
315 for (buf = buf2 - 1; buf >= 0; buf--) {
316 /* edit->buffers2[0] is already allocated */
317 if (!edit->buffers2[buf])
318 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
319 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
322 mc_close (file);
323 return 0;
326 /* detecting an error on save is easy: just check if every byte has been written. */
327 /* detecting an error on read, is not so easy 'cos there is not way to tell
328 whether you read everything or not. */
329 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
330 static const struct edit_filters {
331 const char *read, *write, *extension;
332 } all_filters[] = {
333 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
334 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
335 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
336 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
337 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
340 /* Return index of the filter or -1 is there is no appropriate filter */
341 static int edit_find_filter (const char *filename)
343 size_t i, l, e;
344 if (!filename)
345 return -1;
346 l = strlen (filename);
347 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
348 e = strlen (all_filters[i].extension);
349 if (l > e)
350 if (!strcmp (all_filters[i].extension, filename + l - e))
351 return i;
353 return -1;
356 static char *
357 edit_get_filter (const char *filename)
359 int i, l;
360 char *p, *quoted_name;
361 i = edit_find_filter (filename);
362 if (i < 0)
363 return 0;
364 quoted_name = name_quote (filename, 0);
365 l = str_term_width1 (quoted_name);
366 p = g_malloc0 (str_term_width1 (all_filters[i].read) + l + 2);
367 sprintf (p, all_filters[i].read, quoted_name);
368 g_free (quoted_name);
369 return p;
372 char *
373 edit_get_write_filter (const char *write_name, const char *filename)
375 int i, l;
376 char *p, *writename;
377 i = edit_find_filter (filename);
378 if (i < 0)
379 return 0;
380 writename = name_quote (write_name, 0);
381 l = str_term_width1 (writename);
382 p = g_malloc0 (str_term_width1 (all_filters[i].write) + l + 2);
383 sprintf (p, all_filters[i].write, writename);
384 g_free (writename);
385 return p;
388 static long
389 edit_insert_stream (WEdit * edit, FILE * f)
391 int c;
392 long i = 0;
393 while ((c = fgetc (f)) >= 0) {
394 edit_insert (edit, c);
395 i++;
397 return i;
400 long edit_write_stream (WEdit * edit, FILE * f)
402 long i;
404 if (edit->lb == LB_ASIS) {
405 for (i = 0; i < edit->last_byte; i++)
406 if (fputc (edit_get_byte (edit, i), f) < 0)
407 break;
408 return i;
411 /* change line breaks */
412 for (i = 0; i < edit->last_byte; i++) {
413 unsigned char c = edit_get_byte (edit, i);
415 if (!(c == '\n' || c == '\r')) {
416 /* not line break */
417 if (fputc (c, f) < 0)
418 return i;
419 } else { /* (c == '\n' || c == '\r') */
420 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
422 switch (edit->lb) {
423 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
424 /* put one line break unconditionally */
425 if (fputc ('\n', f) < 0)
426 return i;
428 i++; /* 2 chars are processed */
430 if (c == '\r' && c1 == '\n')
431 /* Windows line break; go to the next char */
432 break;
434 if (c == '\r' && c1 == '\r') {
435 /* two Macintosh line breaks; put second line break */
436 if (fputc ('\n', f) < 0)
437 return i;
438 break;
441 if (fputc (c1, f) < 0)
442 return i;
443 break;
445 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
446 /* put one line break unconditionally */
447 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
448 return i;
450 if (c == '\r' && c1 == '\n')
451 /* Windows line break; go to the next char */
452 i++;
453 break;
455 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
456 /* put one line break unconditionally */
457 if (fputc ('\r', f) < 0)
458 return i;
460 i++; /* 2 chars are processed */
462 if (c == '\r' && c1 == '\n')
463 /* Windows line break; go to the next char */
464 break;
466 if (c == '\n' && c1 == '\n') {
467 /* two Windows line breaks; put second line break */
468 if (fputc ('\r', f) < 0)
469 return i;
470 break;
473 if (fputc (c1, f) < 0)
474 return i;
475 break;
476 case LB_ASIS: /* default without changes */
477 break;
482 return edit->last_byte;
485 #define TEMP_BUF_LEN 1024
487 /* inserts a file at the cursor, returns 1 on success */
489 edit_insert_file (WEdit *edit, const char *filename)
491 char *p;
492 if ((p = edit_get_filter (filename))) {
493 FILE *f;
494 long current = edit->curs1;
495 f = (FILE *) popen (p, "r");
496 if (f) {
497 edit_insert_stream (edit, f);
498 edit_cursor_move (edit, current - edit->curs1);
499 if (pclose (f) > 0) {
500 GString *errmsg = g_string_new (NULL);
501 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
502 edit_error_dialog (_("Error"), errmsg->str);
503 g_string_free (errmsg, TRUE);
504 g_free (p);
505 return 0;
507 } else {
508 GString *errmsg = g_string_new (NULL);
509 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
510 edit_error_dialog (_("Error"), errmsg->str);
511 g_string_free (errmsg, TRUE);
512 g_free (p);
513 return 0;
515 g_free (p);
516 } else {
517 int i, file, blocklen;
518 long current = edit->curs1;
519 int vertical_insertion = 0;
520 char *buf;
521 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
522 return 0;
523 buf = g_malloc0 (TEMP_BUF_LEN);
524 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
525 if (blocklen > 0) {
526 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
527 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
528 vertical_insertion = 1;
529 } else {
530 mc_lseek (file, 0, SEEK_SET);
533 if (vertical_insertion) {
534 blocklen = edit_insert_column_of_text_from_file (edit, file);
535 } else {
536 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
537 for (i = 0; i < blocklen; i++)
538 edit_insert (edit, buf[i]);
541 edit_cursor_move (edit, current - edit->curs1);
542 g_free (buf);
543 mc_close (file);
544 if (blocklen)
545 return 0;
547 return 1;
550 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
551 static int
552 check_file_access (WEdit *edit, const char *filename, struct stat *st)
554 int file;
555 GString *errmsg = (GString *) 0;
557 /* Try opening an existing file */
558 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
560 if (file < 0) {
562 * Try creating the file. O_EXCL prevents following broken links
563 * and opening existing files.
565 file =
566 mc_open (filename,
567 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
568 0666);
569 if (file < 0) {
570 g_string_sprintf (errmsg = g_string_new (NULL),
571 _(" Cannot open %s for reading "), filename);
572 goto cleanup;
573 } else {
574 /* New file, delete it if it's not modified or saved */
575 edit->delete_file = 1;
579 /* Check what we have opened */
580 if (mc_fstat (file, st) < 0) {
581 g_string_sprintf (errmsg = g_string_new (NULL),
582 _(" Cannot get size/permissions for %s "), filename);
583 goto cleanup;
586 /* We want to open regular files only */
587 if (!S_ISREG (st->st_mode)) {
588 g_string_sprintf (errmsg = g_string_new (NULL),
589 _(" %s is not a regular file "), filename);
590 goto cleanup;
594 * Don't delete non-empty files.
595 * O_EXCL should prevent it, but let's be on the safe side.
597 if (st->st_size > 0) {
598 edit->delete_file = 0;
601 if (st->st_size >= SIZE_LIMIT) {
602 g_string_sprintf (errmsg = g_string_new (NULL),
603 _(" File %s is too large "), filename);
606 cleanup:
607 (void) mc_close (file);
608 if (errmsg) {
609 edit_error_dialog (_("Error"), errmsg->str);
610 g_string_free (errmsg, TRUE);
611 return 1;
613 return 0;
617 * Open the file and load it into the buffers, either directly or using
618 * a filter. Return 0 on success, 1 on error.
620 * Fast loading (edit_load_file_fast) is used when the file size is
621 * known. In this case the data is read into the buffers by blocks.
622 * If the file size is not known, the data is loaded byte by byte in
623 * edit_insert_file.
625 static int
626 edit_load_file (WEdit *edit)
628 int fast_load = 1;
630 /* Cannot do fast load if a filter is used */
631 if (edit_find_filter (edit->filename) >= 0)
632 fast_load = 0;
635 * VFS may report file size incorrectly, and slow load is not a big
636 * deal considering overhead in VFS.
638 if (!vfs_file_is_local (edit->filename))
639 fast_load = 0;
642 * FIXME: line end translation should disable fast loading as well
643 * Consider doing fseek() to the end and ftell() for the real size.
646 if (*edit->filename) {
647 /* If we are dealing with a real file, check that it exists */
648 if (check_file_access (edit, edit->filename, &edit->stat1))
649 return 1;
650 } else {
651 /* nothing to load */
652 fast_load = 0;
655 edit_init_buffers (edit);
657 if (fast_load) {
658 edit->last_byte = edit->stat1.st_size;
659 edit_load_file_fast (edit, edit->filename);
660 /* If fast load was used, the number of lines wasn't calculated */
661 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
662 } else {
663 #ifdef HAVE_CHARSET
664 const char *codepage_id;
665 #endif
666 edit->last_byte = 0;
667 if (*edit->filename) {
668 edit->stack_disable = 1;
669 if (!edit_insert_file (edit, edit->filename)) {
670 edit_clean (edit);
671 return 1;
673 edit->stack_disable = 0;
676 #ifdef HAVE_CHARSET
677 codepage_id = get_codepage_id( source_codepage );
678 if ( codepage_id )
679 edit->utf8 = str_isutf8 ( codepage_id );
680 #endif
682 edit->lb = LB_ASIS;
683 return 0;
686 /* Restore saved cursor position in the file */
687 static void
688 edit_load_position (WEdit *edit)
690 char *filename;
691 long line, column;
693 if (!edit->filename || !*edit->filename)
694 return;
696 filename = vfs_canon (edit->filename);
697 load_file_position (filename, &line, &column);
698 g_free (filename);
700 edit_move_to_line (edit, line - 1);
701 edit->prev_col = column;
702 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
703 edit_move_display (edit, line - (edit->num_widget_lines / 2));
706 /* Save cursor position in the file */
707 static void
708 edit_save_position (WEdit *edit)
710 char *filename;
712 if (!edit->filename || !*edit->filename)
713 return;
715 filename = vfs_canon (edit->filename);
716 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
717 g_free (filename);
720 /* Clean the WEdit stricture except the widget part */
721 static void
722 edit_purge_widget (WEdit *edit)
724 size_t len = sizeof (WEdit) - sizeof (Widget);
725 char *start = (char *) edit + sizeof (Widget);
726 memset (start, 0, len);
727 edit->macro_i = -1; /* not recording a macro */
730 static void
731 edit_set_keymap (void)
733 editor_map = default_editor_keymap;
734 if (editor_keymap && editor_keymap->len > 0)
735 editor_map = (global_keymap_t *) editor_keymap->data;
737 editor_x_map = default_editor_x_keymap;
738 if (editor_x_keymap && editor_x_keymap->len > 0)
739 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
743 #define space_width 1
746 * Fill in the edit structure. Return NULL on failure. Pass edit as
747 * NULL to allocate a new structure.
749 * If line is 0, try to restore saved position. Otherwise put the
750 * cursor on that line and show it in the middle of the screen.
752 WEdit *
753 edit_init (WEdit *edit, int lines, int columns, const char *filename,
754 long line)
756 int to_free = 0;
757 option_auto_syntax = 1; /* Resetting to auto on every invokation */
758 if ( option_line_state ) {
759 option_line_state_width = LINE_STATE_WIDTH;
760 } else {
761 option_line_state_width = 0;
763 if (!edit) {
764 #ifdef ENABLE_NLS
766 * Expand option_whole_chars_search by national letters using
767 * current locale
770 static char option_whole_chars_search_buf[256];
772 if (option_whole_chars_search_buf != option_whole_chars_search) {
773 size_t i;
774 size_t len = str_term_width1 (option_whole_chars_search);
776 strcpy (option_whole_chars_search_buf,
777 option_whole_chars_search);
779 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
780 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
781 option_whole_chars_search_buf[len++] = i;
785 option_whole_chars_search_buf[len] = 0;
786 option_whole_chars_search = option_whole_chars_search_buf;
788 #endif /* ENABLE_NLS */
789 edit = g_malloc0 (sizeof (WEdit));
790 edit->search = NULL;
791 to_free = 1;
793 edit_purge_widget (edit);
794 edit->num_widget_lines = lines;
795 edit->over_col = 0;
796 edit->num_widget_columns = columns;
797 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
798 edit->stat1.st_uid = getuid ();
799 edit->stat1.st_gid = getgid ();
800 edit->stat1.st_mtime = 0;
801 edit->bracket = -1;
802 edit->force |= REDRAW_PAGE;
803 edit_set_filename (edit, filename);
804 edit->stack_size = START_STACK_SIZE;
805 edit->stack_size_mask = START_STACK_SIZE - 1;
806 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
807 if (edit_load_file (edit)) {
808 /* edit_load_file already gives an error message */
809 if (to_free)
810 g_free (edit);
811 return 0;
813 edit->utf8 = 0;
814 edit->converter = str_cnv_from_term;
815 #ifdef HAVE_CHARSET
817 const char *cp_id = NULL;
818 cp_id = get_codepage_id (source_codepage >= 0 ?
819 source_codepage : display_codepage);
821 if (cp_id != NULL) {
822 GIConv conv;
823 conv = str_crt_conv_from (cp_id);
824 if (conv != INVALID_CONV) {
825 if (edit->converter != str_cnv_from_term)
826 str_close_conv (edit->converter);
827 edit->converter = conv;
830 if (cp_id != NULL)
831 edit->utf8 = str_isutf8 (cp_id);
833 #endif
835 edit->loading_done = 1;
836 edit->modified = 0;
837 edit->locked = 0;
838 edit_load_syntax (edit, 0, 0);
840 int color;
841 edit_get_syntax_color (edit, -1, &color);
844 /* load saved cursor position */
845 if ((line == 0) && option_save_position) {
846 edit_load_position (edit);
847 } else {
848 if (line <= 0)
849 line = 1;
850 edit_move_display (edit, line - 1);
851 edit_move_to_line (edit, line - 1);
854 edit_set_keymap ();
856 return edit;
859 /* Clear the edit struct, freeing everything in it. Return 1 on success */
861 edit_clean (WEdit *edit)
863 int j = 0;
865 if (!edit)
866 return 0;
868 /* a stale lock, remove it */
869 if (edit->locked)
870 edit->locked = edit_unlock_file (edit->filename);
872 /* save cursor position */
873 if (option_save_position)
874 edit_save_position (edit);
876 /* File specified on the mcedit command line and never saved */
877 if (edit->delete_file)
878 unlink (edit->filename);
880 edit_free_syntax_rules (edit);
881 book_mark_flush (edit, -1);
882 for (; j <= MAXBUFF; j++) {
883 g_free (edit->buffers1[j]);
884 g_free (edit->buffers2[j]);
887 g_free (edit->undo_stack);
888 g_free (edit->filename);
889 g_free (edit->dir);
891 if (edit->search)
893 mc_search_free(edit->search);
894 edit->search = NULL;
896 edit_purge_widget (edit);
898 return 1;
901 /* returns 1 on success */
903 edit_renew (WEdit * edit)
905 int lines = edit->num_widget_lines;
906 int columns = edit->num_widget_columns;
908 edit_clean (edit);
909 return (edit_init (edit, lines, columns, "", 0) != NULL);
913 * Load a new file into the editor. If it fails, preserve the old file.
914 * To do it, allocate a new widget, initialize it and, if the new file
915 * was loaded, copy the data to the old widget.
916 * Return 1 on success, 0 on failure.
919 edit_reload (WEdit *edit, const char *filename)
921 WEdit *e;
922 int lines = edit->num_widget_lines;
923 int columns = edit->num_widget_columns;
925 e = g_malloc0 (sizeof (WEdit));
926 e->widget = edit->widget;
927 if (!edit_init (e, lines, columns, filename, 0)) {
928 g_free (e);
929 return 0;
931 edit_clean (edit);
932 memcpy (edit, e, sizeof (WEdit));
933 g_free (e);
934 return 1;
938 * Load a new file into the editor and set line. If it fails, preserve the old file.
939 * To do it, allocate a new widget, initialize it and, if the new file
940 * was loaded, copy the data to the old widget.
941 * Return 1 on success, 0 on failure.
944 edit_reload_line (WEdit *edit, const char *filename, long line)
946 WEdit *e;
947 int lines = edit->num_widget_lines;
948 int columns = edit->num_widget_columns;
950 e = g_malloc0 (sizeof (WEdit));
951 e->widget = edit->widget;
952 if (!edit_init (e, lines, columns, filename, line)) {
953 g_free (e);
954 return 0;
956 edit_clean (edit);
957 memcpy (edit, e, sizeof (WEdit));
958 g_free (e);
959 return 1;
964 Recording stack for undo:
965 The following is an implementation of a compressed stack. Identical
966 pushes are recorded by a negative prefix indicating the number of times the
967 same char was pushed. This saves space for repeated curs-left or curs-right
968 delete etc.
972 pushed: stored:
976 b -3
978 c --> -4
984 If the stack long int is 0-255 it represents a normal insert (from a backspace),
985 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
986 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
987 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
988 position.
990 The only way the cursor moves or the buffer is changed is through the routines:
991 insert, backspace, insert_ahead, delete, and cursor_move.
992 These record the reverse undo movements onto the stack each time they are
993 called.
995 Each key press results in a set of actions (insert; delete ...). So each time
996 a key is pressed the current position of start_display is pushed as
997 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
998 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
999 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1003 void edit_push_action (WEdit * edit, long c,...)
1005 unsigned long sp = edit->stack_pointer;
1006 unsigned long spm1;
1007 long *t;
1009 /* first enlarge the stack if necessary */
1010 if (sp > edit->stack_size - 10) { /* say */
1011 if (option_max_undo < 256)
1012 option_max_undo = 256;
1013 if (edit->stack_size < (unsigned long) option_max_undo) {
1014 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1015 if (t) {
1016 edit->undo_stack = t;
1017 edit->stack_size <<= 1;
1018 edit->stack_size_mask = edit->stack_size - 1;
1022 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1023 if (edit->stack_disable)
1024 return;
1026 #ifdef FAST_MOVE_CURSOR
1027 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1028 va_list ap;
1029 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1030 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1031 va_start (ap, c);
1032 c = -(va_arg (ap, int));
1033 va_end (ap);
1034 } else
1035 #endif /* ! FAST_MOVE_CURSOR */
1036 if (edit->stack_bottom != sp
1037 && spm1 != edit->stack_bottom
1038 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1039 int d;
1040 if (edit->undo_stack[spm1] < 0) {
1041 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1042 if (d == c) {
1043 if (edit->undo_stack[spm1] > -1000000000) {
1044 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1045 edit->undo_stack[spm1]--;
1046 return;
1049 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1050 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1051 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1052 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1053 if (edit->undo_stack[spm1] == -2)
1054 edit->stack_pointer = spm1;
1055 else
1056 edit->undo_stack[spm1]++;
1057 return;
1059 #endif
1060 } else {
1061 d = edit->undo_stack[spm1];
1062 if (d == c) {
1063 if (c >= KEY_PRESS)
1064 return; /* --> no need to push multiple do-nothings */
1065 edit->undo_stack[sp] = -2;
1066 goto check_bottom;
1068 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1069 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1070 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1071 edit->stack_pointer = spm1;
1072 return;
1074 #endif
1077 edit->undo_stack[sp] = c;
1079 check_bottom:
1080 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1082 /* if the sp wraps round and catches the stack_bottom then erase
1083 * the first set of actions on the stack to make space - by moving
1084 * stack_bottom forward one "key press" */
1085 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1086 if ((unsigned long) c == edit->stack_bottom ||
1087 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1088 do {
1089 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1090 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1092 /*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: */
1093 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1094 edit->stack_bottom = edit->stack_pointer = 0;
1098 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1099 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1101 static long
1102 pop_action (WEdit * edit)
1104 long c;
1105 unsigned long sp = edit->stack_pointer;
1106 if (sp == edit->stack_bottom) {
1107 return STACK_BOTTOM;
1109 sp = (sp - 1) & edit->stack_size_mask;
1110 if ((c = edit->undo_stack[sp]) >= 0) {
1111 /* edit->undo_stack[sp] = '@'; */
1112 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1113 return c;
1115 if (sp == edit->stack_bottom) {
1116 return STACK_BOTTOM;
1118 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1119 if (edit->undo_stack[sp] == -2) {
1120 /* edit->undo_stack[sp] = '@'; */
1121 edit->stack_pointer = sp;
1122 } else
1123 edit->undo_stack[sp]++;
1125 return c;
1128 /* is called whenever a modification is made by one of the four routines below */
1129 static void edit_modification (WEdit * edit)
1131 edit->caches_valid = 0;
1132 edit->screen_modified = 1;
1134 /* raise lock when file modified */
1135 if (!edit->modified && !edit->delete_file)
1136 edit->locked = edit_lock_file (edit->filename);
1137 edit->modified = 1;
1141 Basic low level single character buffer alterations and movements at the cursor.
1142 Returns char passed over, inserted or removed.
1145 void
1146 edit_insert (WEdit *edit, int c)
1148 /* check if file has grown to large */
1149 if (edit->last_byte >= SIZE_LIMIT)
1150 return;
1152 /* first we must update the position of the display window */
1153 if (edit->curs1 < edit->start_display) {
1154 edit->start_display++;
1155 if (c == '\n')
1156 edit->start_line++;
1159 /* Mark file as modified, unless the file hasn't been fully loaded */
1160 if (edit->loading_done) {
1161 edit_modification (edit);
1164 /* now we must update some info on the file and check if a redraw is required */
1165 if (c == '\n') {
1166 if (edit->book_mark)
1167 book_mark_inc (edit, edit->curs_line);
1168 edit->curs_line++;
1169 edit->total_lines++;
1170 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1173 /* save the reverse command onto the undo stack */
1174 edit_push_action (edit, BACKSPACE);
1176 /* update markers */
1177 edit->mark1 += (edit->mark1 > edit->curs1);
1178 edit->mark2 += (edit->mark2 > edit->curs1);
1179 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1181 /* add a new buffer if we've reached the end of the last one */
1182 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1183 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1184 g_malloc0 (EDIT_BUF_SIZE);
1186 /* perform the insertion */
1187 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1188 curs1 & M_EDIT_BUF_SIZE]
1189 = (unsigned char) c;
1191 /* update file length */
1192 edit->last_byte++;
1194 /* update cursor position */
1195 edit->curs1++;
1198 static void
1199 edit_insert_over (WEdit * edit)
1201 int i;
1203 for ( i = 0; i < edit->over_col; i++ ) {
1204 edit_insert (edit, ' ');
1206 edit->over_col = 0;
1209 /* same as edit_insert and move left */
1210 void edit_insert_ahead (WEdit * edit, int c)
1212 if (edit->last_byte >= SIZE_LIMIT)
1213 return;
1214 if (edit->curs1 < edit->start_display) {
1215 edit->start_display++;
1216 if (c == '\n')
1217 edit->start_line++;
1219 edit_modification (edit);
1220 if (c == '\n') {
1221 if (edit->book_mark)
1222 book_mark_inc (edit, edit->curs_line);
1223 edit->total_lines++;
1224 edit->force |= REDRAW_AFTER_CURSOR;
1226 edit_push_action (edit, DELCHAR);
1228 edit->mark1 += (edit->mark1 >= edit->curs1);
1229 edit->mark2 += (edit->mark2 >= edit->curs1);
1230 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1232 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1233 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1234 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1236 edit->last_byte++;
1237 edit->curs2++;
1241 int edit_delete (WEdit * edit, const int byte_delete)
1243 int p = 0;
1244 int cw = 1;
1245 int i;
1247 if (!edit->curs2)
1248 return 0;
1250 edit->mark1 -= (edit->mark1 > edit->curs1);
1251 edit->mark2 -= (edit->mark2 > edit->curs1);
1252 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1254 cw = 1;
1255 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1256 if ( edit->utf8 && byte_delete == 0 ) {
1257 edit_get_utf (edit, edit->curs1, &cw);
1258 if ( cw < 1 )
1259 cw = 1;
1261 for ( i = 1; i<= cw; i++ ) {
1262 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1264 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1265 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1266 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1268 edit->last_byte--;
1269 edit->curs2--;
1270 edit_push_action (edit, p + 256);
1273 edit_modification (edit);
1274 if (p == '\n') {
1275 if (edit->book_mark)
1276 book_mark_dec (edit, edit->curs_line);
1277 edit->total_lines--;
1278 edit->force |= REDRAW_AFTER_CURSOR;
1280 if (edit->curs1 < edit->start_display) {
1281 edit->start_display--;
1282 if (p == '\n')
1283 edit->start_line--;
1286 return p;
1290 static int
1291 edit_backspace (WEdit * edit, const int byte_delete)
1293 int p = 0;
1294 int cw = 1;
1295 int i;
1297 if (!edit->curs1)
1298 return 0;
1300 edit->mark1 -= (edit->mark1 >= edit->curs1);
1301 edit->mark2 -= (edit->mark2 >= edit->curs1);
1302 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1304 cw = 1;
1305 if ( edit->utf8 && byte_delete == 0 ) {
1306 edit_get_prev_utf (edit, edit->curs1, &cw);
1307 if ( cw < 1 )
1308 cw = 1;
1310 for ( i = 1; i<= cw; i++ ) {
1311 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1312 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1313 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1314 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1316 edit->last_byte--;
1317 edit->curs1--;
1318 edit_push_action (edit, p);
1320 edit_modification (edit);
1321 if (p == '\n') {
1322 if (edit->book_mark)
1323 book_mark_dec (edit, edit->curs_line);
1324 edit->curs_line--;
1325 edit->total_lines--;
1326 edit->force |= REDRAW_AFTER_CURSOR;
1329 if (edit->curs1 < edit->start_display) {
1330 edit->start_display--;
1331 if (p == '\n')
1332 edit->start_line--;
1335 return p;
1338 #ifdef FAST_MOVE_CURSOR
1340 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1342 unsigned long next;
1343 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1344 edit->curs_line--;
1345 next -= (unsigned long) dest;
1346 n -= next;
1347 src += next;
1348 dest += next;
1353 edit_move_backward_lots (WEdit *edit, long increment)
1355 int r, s, t;
1356 unsigned char *p = NULL;
1358 if (increment > edit->curs1)
1359 increment = edit->curs1;
1360 if (increment <= 0)
1361 return -1;
1362 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1364 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1365 if (r > increment)
1366 r = increment;
1367 s = edit->curs1 & M_EDIT_BUF_SIZE;
1369 if (s > r) {
1370 memqcpy (edit,
1371 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1372 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1374 } else {
1375 if (s) {
1376 memqcpy (edit,
1377 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1378 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1379 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1380 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1382 memqcpy (edit,
1383 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1384 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1385 EDIT_BUF_SIZE - (r - s), r - s);
1387 increment -= r;
1388 edit->curs1 -= r;
1389 edit->curs2 += r;
1390 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1391 if (p)
1392 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1393 else
1394 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1395 g_malloc0 (EDIT_BUF_SIZE);
1396 } else {
1397 g_free (p);
1400 s = edit->curs1 & M_EDIT_BUF_SIZE;
1401 while (increment) {
1402 p = 0;
1403 r = EDIT_BUF_SIZE;
1404 if (r > increment)
1405 r = increment;
1406 t = s;
1407 if (r < t)
1408 t = r;
1409 memqcpy (edit,
1410 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1411 EDIT_BUF_SIZE - t,
1412 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1414 if (r >= s) {
1415 if (t) {
1416 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1417 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1419 memqcpy (edit,
1420 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1421 EDIT_BUF_SIZE - r,
1422 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1423 EDIT_BUF_SIZE - (r - s), r - s);
1425 increment -= r;
1426 edit->curs1 -= r;
1427 edit->curs2 += r;
1428 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1429 if (p)
1430 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1431 else
1432 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1433 g_malloc0 (EDIT_BUF_SIZE);
1434 } else {
1435 g_free (p);
1438 return edit_get_byte (edit, edit->curs1);
1441 #endif /* ! FAST_MOVE_CURSOR */
1443 /* moves the cursor right or left: increment positive or negative respectively */
1444 void edit_cursor_move (WEdit * edit, long increment)
1446 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1447 int c;
1448 #ifdef FAST_MOVE_CURSOR
1449 if (increment < -256) {
1450 edit->force |= REDRAW_PAGE;
1451 edit_move_backward_lots (edit, -increment);
1452 return;
1454 #endif /* ! FAST_MOVE_CURSOR */
1456 if (increment < 0) {
1457 for (; increment < 0; increment++) {
1458 if (!edit->curs1)
1459 return;
1461 edit_push_action (edit, CURS_RIGHT);
1463 c = edit_get_byte (edit, edit->curs1 - 1);
1464 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1465 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1466 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1467 edit->curs2++;
1468 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1469 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1470 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1471 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1473 edit->curs1--;
1474 if (c == '\n') {
1475 edit->curs_line--;
1476 edit->force |= REDRAW_LINE_BELOW;
1480 } else if (increment > 0) {
1481 for (; increment > 0; increment--) {
1482 if (!edit->curs2)
1483 return;
1485 edit_push_action (edit, CURS_LEFT);
1487 c = edit_get_byte (edit, edit->curs1);
1488 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1489 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1490 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1491 edit->curs1++;
1492 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1493 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1494 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1495 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1497 edit->curs2--;
1498 if (c == '\n') {
1499 edit->curs_line++;
1500 edit->force |= REDRAW_LINE_ABOVE;
1506 /* These functions return positions relative to lines */
1508 /* returns index of last char on line + 1 */
1509 long edit_eol (WEdit * edit, long current)
1511 if (current < edit->last_byte) {
1512 for (;; current++)
1513 if (edit_get_byte (edit, current) == '\n')
1514 break;
1515 } else
1516 return edit->last_byte;
1517 return current;
1520 /* returns index of first char on line */
1521 long edit_bol (WEdit * edit, long current)
1523 if (current > 0) {
1524 for (;; current--)
1525 if (edit_get_byte (edit, current - 1) == '\n')
1526 break;
1527 } else
1528 return 0;
1529 return current;
1533 long edit_count_lines (WEdit * edit, long current, long upto)
1535 long lines = 0;
1536 if (upto > edit->last_byte)
1537 upto = edit->last_byte;
1538 if (current < 0)
1539 current = 0;
1540 while (current < upto)
1541 if (edit_get_byte (edit, current++) == '\n')
1542 lines++;
1543 return lines;
1547 /* If lines is zero this returns the count of lines from current to upto. */
1548 /* If upto is zero returns index of lines forward current. */
1549 long edit_move_forward (WEdit * edit, long current, long lines, long upto)
1551 if (upto) {
1552 return edit_count_lines (edit, current, upto);
1553 } else {
1554 long next;
1555 if (lines < 0)
1556 lines = 0;
1557 while (lines--) {
1558 next = edit_eol (edit, current) + 1;
1559 if (next > edit->last_byte)
1560 break;
1561 else
1562 current = next;
1564 return current;
1569 /* Returns offset of 'lines' lines up from current */
1570 long edit_move_backward (WEdit * edit, long current, long lines)
1572 if (lines < 0)
1573 lines = 0;
1574 current = edit_bol (edit, current);
1575 while((lines--) && current != 0)
1576 current = edit_bol (edit, current - 1);
1577 return current;
1580 /* If cols is zero this returns the count of columns from current to upto. */
1581 /* If upto is zero returns index of cols across from current. */
1582 long
1583 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1585 long p, q;
1586 int col;
1588 if (upto) {
1589 q = upto;
1590 cols = -10;
1591 } else
1592 q = edit->last_byte + 2;
1594 for (col = 0, p = current; p < q; p++) {
1595 int c, orig_c;
1596 int utf_ch = 0;
1597 #ifdef HAVE_CHARSET
1598 int cw = 1;
1599 #endif
1600 if (cols != -10) {
1601 if (col == cols)
1602 return p;
1603 if (col > cols)
1604 return p - 1;
1606 orig_c = c = edit_get_byte (edit, p);
1607 #ifdef HAVE_CHARSET
1608 if (edit->utf8) {
1609 utf_ch = edit_get_utf (edit, p, &cw);
1610 if (utf8_display) {
1611 if (cw > 1)
1612 col -= cw - 1;
1613 if (g_unichar_iswide (utf_ch))
1614 col++;
1615 } else if (cw > 1 && g_unichar_isprint (utf_ch))
1616 col -= cw - 1;
1618 #endif
1619 c = convert_to_display_c (c);
1620 if (c == '\t')
1621 col += TAB_SIZE - col % TAB_SIZE;
1622 else if (c == '\n') {
1623 if (upto)
1624 return col;
1625 else
1626 return p;
1627 } else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1628 /* '\r' is shown as ^M, so we must advance 2 characters */
1629 /* Caret notation for control characters */
1630 col += 2;
1631 else
1632 col++;
1634 return col;
1637 /* returns the current column position of the cursor */
1638 int edit_get_col (WEdit * edit)
1640 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1644 /* Scrolling functions */
1646 void edit_update_curs_row (WEdit * edit)
1648 edit->curs_row = edit->curs_line - edit->start_line;
1651 void edit_update_curs_col (WEdit * edit)
1653 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1657 edit_get_curs_col (const WEdit *edit)
1659 return edit->curs_col;
1662 /*moves the display start position up by i lines */
1663 void edit_scroll_upward (WEdit * edit, unsigned long i)
1665 unsigned long lines_above = edit->start_line;
1666 if (i > lines_above)
1667 i = lines_above;
1668 if (i) {
1669 edit->start_line -= i;
1670 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1671 edit->force |= REDRAW_PAGE;
1672 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1674 edit_update_curs_row (edit);
1678 /* returns 1 if could scroll, 0 otherwise */
1679 void edit_scroll_downward (WEdit * edit, int i)
1681 int lines_below;
1682 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1683 if (lines_below > 0) {
1684 if (i > lines_below)
1685 i = lines_below;
1686 edit->start_line += i;
1687 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1688 edit->force |= REDRAW_PAGE;
1689 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1691 edit_update_curs_row (edit);
1694 void edit_scroll_right (WEdit * edit, int i)
1696 edit->force |= REDRAW_PAGE;
1697 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1698 edit->start_col -= i;
1701 void edit_scroll_left (WEdit * edit, int i)
1703 if (edit->start_col) {
1704 edit->start_col += i;
1705 if (edit->start_col > 0)
1706 edit->start_col = 0;
1707 edit->force |= REDRAW_PAGE;
1708 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1712 /* high level cursor movement commands */
1714 static int is_in_indent (WEdit *edit)
1716 long p = edit_bol (edit, edit->curs1);
1717 while (p < edit->curs1)
1718 if (!strchr (" \t", edit_get_byte (edit, p++)))
1719 return 0;
1720 return 1;
1723 static int left_of_four_spaces (WEdit *edit);
1725 void
1726 edit_move_to_prev_col (WEdit * edit, long p)
1728 int prev = edit->prev_col;
1729 int over = edit->over_col;
1730 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1732 if (option_cursor_beyond_eol) {
1733 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1735 if (line_len < prev + edit->over_col) {
1736 edit->over_col = prev + over - line_len;
1737 edit->prev_col = line_len;
1738 edit->curs_col = line_len;
1739 } else {
1740 edit->curs_col = prev + over;
1741 edit->prev_col = edit->curs_col;
1742 edit->over_col = 0;
1744 } else {
1745 edit->over_col = 0;
1746 if (is_in_indent (edit) && option_fake_half_tabs) {
1747 edit_update_curs_col (edit);
1748 if (space_width)
1749 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1750 int q = edit->curs_col;
1751 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1752 p = edit_bol (edit, edit->curs1);
1753 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1754 if (!left_of_four_spaces (edit))
1755 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1761 static int
1762 is_blank (WEdit *edit, long offset)
1764 long s, f;
1765 int c;
1766 s = edit_bol (edit, offset);
1767 f = edit_eol (edit, offset) - 1;
1768 while (s <= f) {
1769 c = edit_get_byte (edit, s++);
1770 if (!isspace (c))
1771 return 0;
1773 return 1;
1777 /* returns the offset of line i */
1778 static long
1779 edit_find_line (WEdit *edit, int line)
1781 int i, j = 0;
1782 int m = 2000000000;
1783 if (!edit->caches_valid) {
1784 for (i = 0; i < N_LINE_CACHES; i++)
1785 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1786 /* three offsets that we *know* are line 0 at 0 and these two: */
1787 edit->line_numbers[1] = edit->curs_line;
1788 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1789 edit->line_numbers[2] = edit->total_lines;
1790 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1791 edit->caches_valid = 1;
1793 if (line >= edit->total_lines)
1794 return edit->line_offsets[2];
1795 if (line <= 0)
1796 return 0;
1797 /* find the closest known point */
1798 for (i = 0; i < N_LINE_CACHES; i++) {
1799 int n;
1800 n = abs (edit->line_numbers[i] - line);
1801 if (n < m) {
1802 m = n;
1803 j = i;
1806 if (m == 0)
1807 return edit->line_offsets[j]; /* know the offset exactly */
1808 if (m == 1 && j >= 3)
1809 i = j; /* one line different - caller might be looping, so stay in this cache */
1810 else
1811 i = 3 + (rand () % (N_LINE_CACHES - 3));
1812 if (line > edit->line_numbers[j])
1813 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1814 else
1815 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1816 edit->line_numbers[i] = line;
1817 return edit->line_offsets[i];
1820 int line_is_blank (WEdit * edit, long line)
1822 return is_blank (edit, edit_find_line (edit, line));
1825 /* moves up until a blank line is reached, or until just
1826 before a non-blank line is reached */
1827 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1829 int i = 0;
1830 if (edit->curs_line > 1) {
1831 if (line_is_blank (edit, edit->curs_line)) {
1832 if (line_is_blank (edit, edit->curs_line - 1)) {
1833 for (i = edit->curs_line - 1; i; i--)
1834 if (!line_is_blank (edit, i)) {
1835 i++;
1836 break;
1838 } else {
1839 for (i = edit->curs_line - 1; i; i--)
1840 if (line_is_blank (edit, i))
1841 break;
1843 } else {
1844 for (i = edit->curs_line - 1; i; i--)
1845 if (line_is_blank (edit, i))
1846 break;
1849 edit_move_up (edit, edit->curs_line - i, scroll);
1852 /* moves down until a blank line is reached, or until just
1853 before a non-blank line is reached */
1854 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1856 int i;
1857 if (edit->curs_line >= edit->total_lines - 1) {
1858 i = edit->total_lines;
1859 } else {
1860 if (line_is_blank (edit, edit->curs_line)) {
1861 if (line_is_blank (edit, edit->curs_line + 1)) {
1862 for (i = edit->curs_line + 1; i; i++)
1863 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1864 i--;
1865 break;
1867 } else {
1868 for (i = edit->curs_line + 1; i; i++)
1869 if (line_is_blank (edit, i) || i >= edit->total_lines)
1870 break;
1872 } else {
1873 for (i = edit->curs_line + 1; i; i++)
1874 if (line_is_blank (edit, i) || i >= edit->total_lines)
1875 break;
1878 edit_move_down (edit, i - edit->curs_line, scroll);
1881 static void edit_begin_page (WEdit *edit)
1883 edit_update_curs_row (edit);
1884 edit_move_up (edit, edit->curs_row, 0);
1887 static void edit_end_page (WEdit *edit)
1889 edit_update_curs_row (edit);
1890 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1894 /* goto beginning of text */
1895 static void edit_move_to_top (WEdit * edit)
1897 if (edit->curs_line) {
1898 edit_cursor_move (edit, -edit->curs1);
1899 edit_move_to_prev_col (edit, 0);
1900 edit->force |= REDRAW_PAGE;
1901 edit->search_start = 0;
1902 edit_update_curs_row(edit);
1907 /* goto end of text */
1908 static void edit_move_to_bottom (WEdit * edit)
1910 if (edit->curs_line < edit->total_lines) {
1911 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1912 edit->start_display = edit->last_byte;
1913 edit->start_line = edit->total_lines;
1914 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1915 edit->force |= REDRAW_PAGE;
1919 /* goto beginning of line */
1920 static void edit_cursor_to_bol (WEdit * edit)
1922 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1923 edit->search_start = edit->curs1;
1924 edit->prev_col = edit_get_col (edit);
1925 edit->over_col = 0;
1928 /* goto end of line */
1929 static void edit_cursor_to_eol (WEdit * edit)
1931 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1932 edit->search_start = edit->curs1;
1933 edit->prev_col = edit_get_col (edit);
1934 edit->over_col = 0;
1937 /* move cursor to line 'line' */
1938 void edit_move_to_line (WEdit * e, long line)
1940 if(line < e->curs_line)
1941 edit_move_up (e, e->curs_line - line, 0);
1942 else
1943 edit_move_down (e, line - e->curs_line, 0);
1944 edit_scroll_screen_over_cursor (e);
1947 /* scroll window so that first visible line is 'line' */
1948 void edit_move_display (WEdit * e, long line)
1950 if(line < e->start_line)
1951 edit_scroll_upward (e, e->start_line - line);
1952 else
1953 edit_scroll_downward (e, line - e->start_line);
1956 /* save markers onto undo stack */
1957 void edit_push_markers (WEdit * edit)
1959 edit_push_action (edit, MARK_1 + edit->mark1);
1960 edit_push_action (edit, MARK_2 + edit->mark2);
1963 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1965 edit->mark1 = m1;
1966 edit->mark2 = m2;
1967 edit->column1 = c1;
1968 edit->column2 = c2;
1972 /* highlight marker toggle */
1973 void edit_mark_cmd (WEdit * edit, int unmark)
1975 edit_push_markers (edit);
1976 if (unmark) {
1977 edit_set_markers (edit, 0, 0, 0, 0);
1978 edit->force |= REDRAW_PAGE;
1979 } else {
1980 if (edit->mark2 >= 0) {
1981 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
1982 edit->force |= REDRAW_PAGE;
1983 } else
1984 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
1988 static unsigned long
1989 my_type_of (int c)
1991 int x, r = 0;
1992 const char *p, *q;
1993 const char option_chars_move_whole_word[] =
1994 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1996 if (!c)
1997 return 0;
1998 if (c == '!') {
1999 if (*option_chars_move_whole_word == '!')
2000 return 2;
2001 return 0x80000000UL;
2003 if (g_ascii_isupper ((gchar) c))
2004 c = 'A';
2005 else if (g_ascii_islower ((gchar) c))
2006 c = 'a';
2007 else if (g_ascii_isalpha (c))
2008 c = 'a';
2009 else if (isdigit (c))
2010 c = '0';
2011 else if (isspace (c))
2012 c = ' ';
2013 q = strchr (option_chars_move_whole_word, c);
2014 if (!q)
2015 return 0xFFFFFFFFUL;
2016 do {
2017 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2018 if (*p == '!')
2019 x <<= 1;
2020 r |= x;
2021 } while ((q = strchr (q + 1, c)));
2022 return r;
2025 static void
2026 edit_left_word_move (WEdit *edit, int s)
2028 for (;;) {
2029 int c1, c2;
2030 if (column_highlighting
2031 && edit->mark1 != edit->mark2
2032 && edit->over_col == 0
2033 && edit->curs1 == edit_bol(edit, edit->curs1))
2034 break;
2035 edit_cursor_move (edit, -1);
2036 if (!edit->curs1)
2037 break;
2038 c1 = edit_get_byte (edit, edit->curs1 - 1);
2039 c2 = edit_get_byte (edit, edit->curs1);
2040 if (!(my_type_of (c1) & my_type_of (c2)))
2041 break;
2042 if (isspace (c1) && !isspace (c2))
2043 break;
2044 if (s)
2045 if (!isspace (c1) && isspace (c2))
2046 break;
2050 static void edit_left_word_move_cmd (WEdit * edit)
2052 edit_left_word_move (edit, 0);
2053 edit->force |= REDRAW_PAGE;
2056 static void
2057 edit_right_word_move (WEdit *edit, int s)
2059 for (;;) {
2060 int c1, c2;
2061 if (column_highlighting
2062 && edit->mark1 != edit->mark2
2063 && edit->over_col == 0
2064 && edit->curs1 == edit_eol(edit, edit->curs1)
2066 break;
2067 edit_cursor_move (edit, 1);
2068 if (edit->curs1 >= edit->last_byte)
2069 break;
2070 c1 = edit_get_byte (edit, edit->curs1 - 1);
2071 c2 = edit_get_byte (edit, edit->curs1);
2072 if (!(my_type_of (c1) & my_type_of (c2)))
2073 break;
2074 if (isspace (c1) && !isspace (c2))
2075 break;
2076 if (s)
2077 if (!isspace (c1) && isspace (c2))
2078 break;
2082 static void edit_right_word_move_cmd (WEdit * edit)
2084 edit_right_word_move (edit, 0);
2085 edit->force |= REDRAW_PAGE;
2088 static void edit_right_char_move_cmd (WEdit * edit)
2090 int cw = 1;
2091 int c = 0;
2092 if ( edit->utf8 ) {
2093 c = edit_get_utf (edit, edit->curs1, &cw);
2094 if ( cw < 1 )
2095 cw = 1;
2096 } else {
2097 c = edit_get_byte (edit, edit->curs1);
2099 if (option_cursor_beyond_eol && c == '\n') {
2100 edit->over_col++;
2101 } else {
2102 edit_cursor_move (edit, cw);
2106 static void edit_left_char_move_cmd (WEdit * edit)
2108 int cw = 1;
2109 if (column_highlighting
2110 && edit->mark1 != edit->mark2
2111 && edit->over_col == 0
2112 && edit->curs1 == edit_bol(edit, edit->curs1))
2113 return;
2114 if ( edit->utf8 ) {
2115 edit_get_prev_utf (edit, edit->curs1, &cw);
2116 if ( cw < 1 )
2117 cw = 1;
2119 if (option_cursor_beyond_eol && edit->over_col > 0) {
2120 edit->over_col--;
2121 } else {
2122 edit_cursor_move (edit, -cw);
2126 /** Up or down cursor moving.
2127 direction = TRUE - move up
2128 = FALSE - move down
2130 static void
2131 edit_move_updown (WEdit * edit, unsigned long i, int scroll, gboolean direction) {
2132 unsigned long p;
2133 unsigned long l = (direction)
2134 ? edit->curs_line
2135 : edit->total_lines - edit->curs_line;
2137 if (i > l)
2138 i = l;
2140 if (i == 0)
2141 return;
2143 if (i > 1)
2144 edit->force |= REDRAW_PAGE;
2145 if (scroll) {
2146 if (direction)
2147 edit_scroll_upward (edit, i);
2148 else
2149 edit_scroll_downward (edit, i);
2151 p = edit_bol (edit, edit->curs1);
2153 p = (direction)
2154 ? edit_move_backward (edit, p, i)
2155 : edit_move_forward (edit, p, i, 0);
2157 edit_cursor_move (edit, p - edit->curs1);
2159 edit_move_to_prev_col (edit, p);
2161 /* search start of current multibyte char (like CJK) */
2162 edit_right_char_move_cmd (edit);
2163 edit_left_char_move_cmd (edit);
2165 edit->search_start = edit->curs1;
2166 edit->found_len = 0;
2169 static void edit_right_delete_word (WEdit * edit)
2171 int c1, c2;
2172 for (;;) {
2173 if (edit->curs1 >= edit->last_byte)
2174 break;
2175 c1 = edit_delete (edit, 1);
2176 c2 = edit_get_byte (edit, edit->curs1);
2177 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2178 break;
2179 if (!(my_type_of (c1) & my_type_of (c2)))
2180 break;
2184 static void edit_left_delete_word (WEdit * edit)
2186 int c1, c2;
2187 for (;;) {
2188 if (edit->curs1 <= 0)
2189 break;
2190 c1 = edit_backspace (edit, 1);
2191 c2 = edit_get_byte (edit, edit->curs1 - 1);
2192 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2193 break;
2194 if (!(my_type_of (c1) & my_type_of (c2)))
2195 break;
2200 the start column position is not recorded, and hence does not
2201 undo as it happed. But who would notice.
2203 static void
2204 edit_do_undo (WEdit * edit)
2206 long ac;
2207 long count = 0;
2209 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2210 edit->over_col = 0;
2211 while ((ac = pop_action (edit)) < KEY_PRESS) {
2212 switch ((int) ac) {
2213 case STACK_BOTTOM:
2214 goto done_undo;
2215 case CURS_RIGHT:
2216 edit_cursor_move (edit, 1);
2217 break;
2218 case CURS_LEFT:
2219 edit_cursor_move (edit, -1);
2220 break;
2221 case BACKSPACE:
2222 edit_backspace (edit, 1);
2223 break;
2224 case DELCHAR:
2225 edit_delete (edit, 1);
2226 break;
2227 case COLUMN_ON:
2228 column_highlighting = 1;
2229 break;
2230 case COLUMN_OFF:
2231 column_highlighting = 0;
2232 break;
2234 if (ac >= 256 && ac < 512)
2235 edit_insert_ahead (edit, ac - 256);
2236 if (ac >= 0 && ac < 256)
2237 edit_insert (edit, ac);
2239 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2240 edit->mark1 = ac - MARK_1;
2241 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2242 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2243 edit->mark2 = ac - MARK_2;
2244 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2246 if (count++)
2247 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2250 if (edit->start_display > ac - KEY_PRESS) {
2251 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2252 edit->force |= REDRAW_PAGE;
2253 } else if (edit->start_display < ac - KEY_PRESS) {
2254 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2255 edit->force |= REDRAW_PAGE;
2257 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2258 edit_update_curs_row (edit);
2260 done_undo:;
2261 edit->stack_disable = 0;
2264 static void edit_delete_to_line_end (WEdit * edit)
2266 while (edit_get_byte (edit, edit->curs1) != '\n') {
2267 if (!edit->curs2)
2268 break;
2269 edit_delete (edit, 1);
2273 static void edit_delete_to_line_begin (WEdit * edit)
2275 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2276 if (!edit->curs1)
2277 break;
2278 edit_backspace (edit, 1);
2282 void
2283 edit_delete_line (WEdit *edit)
2286 * Delete right part of the line.
2287 * Note that edit_get_byte() returns '\n' when byte position is
2288 * beyond EOF.
2290 while (edit_get_byte (edit, edit->curs1) != '\n') {
2291 (void) edit_delete (edit, 1);
2295 * Delete '\n' char.
2296 * Note that edit_delete() will not corrupt anything if called while
2297 * cursor position is EOF.
2299 (void) edit_delete (edit, 1);
2302 * Delete left part of the line.
2303 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2305 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2306 (void) edit_backspace (edit, 1);
2310 void insert_spaces_tab (WEdit * edit, int half)
2312 int i;
2313 edit_update_curs_col (edit);
2314 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2315 while (i > 0) {
2316 edit_insert (edit, ' ');
2317 i -= space_width;
2321 static int is_aligned_on_a_tab (WEdit * edit)
2323 edit_update_curs_col (edit);
2324 return !((edit->curs_col % (TAB_SIZE * space_width))
2325 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2328 static int right_of_four_spaces (WEdit *edit)
2330 int i, ch = 0;
2331 for (i = 1; i <= HALF_TAB_SIZE; i++)
2332 ch |= edit_get_byte (edit, edit->curs1 - i);
2333 if (ch == ' ')
2334 return is_aligned_on_a_tab (edit);
2335 return 0;
2338 static int left_of_four_spaces (WEdit *edit)
2340 int i, ch = 0;
2341 for (i = 0; i < HALF_TAB_SIZE; i++)
2342 ch |= edit_get_byte (edit, edit->curs1 + i);
2343 if (ch == ' ')
2344 return is_aligned_on_a_tab (edit);
2345 return 0;
2348 int edit_indent_width (WEdit * edit, long p)
2350 long q = p;
2351 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2352 q++;
2353 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2356 void edit_insert_indent (WEdit * edit, int indent)
2358 if (!option_fill_tabs_with_spaces) {
2359 while (indent >= TAB_SIZE) {
2360 edit_insert (edit, '\t');
2361 indent -= TAB_SIZE;
2364 while (indent-- > 0)
2365 edit_insert (edit, ' ');
2368 static void
2369 edit_auto_indent (WEdit * edit)
2371 long p;
2372 char c;
2373 p = edit->curs1;
2374 /* use the previous line as a template */
2375 p = edit_move_backward (edit, p, 1);
2376 /* copy the leading whitespace of the line */
2377 for (;;) { /* no range check - the line _is_ \n-terminated */
2378 c = edit_get_byte (edit, p++);
2379 if (c != ' ' && c != '\t')
2380 break;
2381 edit_insert (edit, c);
2385 static inline void
2386 edit_double_newline (WEdit * edit)
2388 edit_insert (edit, '\n');
2389 if (edit_get_byte (edit, edit->curs1) == '\n')
2390 return;
2391 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2392 return;
2393 edit->force |= REDRAW_PAGE;
2394 edit_insert (edit, '\n');
2397 static inline void
2398 edit_tab_cmd (WEdit * edit)
2400 int i;
2402 if (option_fake_half_tabs) {
2403 if (is_in_indent (edit)) {
2404 /*insert a half tab (usually four spaces) unless there is a
2405 half tab already behind, then delete it and insert a
2406 full tab. */
2407 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2408 for (i = 1; i <= HALF_TAB_SIZE; i++)
2409 edit_backspace (edit, 1);
2410 edit_insert (edit, '\t');
2411 } else {
2412 insert_spaces_tab (edit, 1);
2414 return;
2417 if (option_fill_tabs_with_spaces) {
2418 insert_spaces_tab (edit, 0);
2419 } else {
2420 edit_insert (edit, '\t');
2424 static void check_and_wrap_line (WEdit * edit)
2426 int curs, c;
2427 if (!option_typewriter_wrap)
2428 return;
2429 edit_update_curs_col (edit);
2430 if (edit->curs_col < option_word_wrap_line_length)
2431 return;
2432 curs = edit->curs1;
2433 for (;;) {
2434 curs--;
2435 c = edit_get_byte (edit, curs);
2436 if (c == '\n' || curs <= 0) {
2437 edit_insert (edit, '\n');
2438 return;
2440 if (c == ' ' || c == '\t') {
2441 int current = edit->curs1;
2442 edit_cursor_move (edit, curs - edit->curs1 + 1);
2443 edit_insert (edit, '\n');
2444 edit_cursor_move (edit, current - edit->curs1 + 1);
2445 return;
2450 static inline void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2452 void edit_push_key_press (WEdit * edit)
2454 edit_push_action (edit, KEY_PRESS + edit->start_display);
2455 if (edit->mark2 == -1)
2456 edit_push_action (edit, MARK_1 + edit->mark1);
2459 /* this find the matching bracket in either direction, and sets edit->bracket */
2460 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2462 const char * const b = "{}{[][()(", *p;
2463 int i = 1, a, inc = -1, c, d, n = 0;
2464 unsigned long j = 0;
2465 long q;
2466 edit_update_curs_row (edit);
2467 c = edit_get_byte (edit, edit->curs1);
2468 p = strchr (b, c);
2469 /* no limit */
2470 if (!furthest_bracket_search)
2471 furthest_bracket_search--;
2472 /* not on a bracket at all */
2473 if (!p)
2474 return -1;
2475 /* the matching bracket */
2476 d = p[1];
2477 /* going left or right? */
2478 if (strchr ("{[(", c))
2479 inc = 1;
2480 for (q = edit->curs1 + inc;; q += inc) {
2481 /* out of buffer? */
2482 if (q >= edit->last_byte || q < 0)
2483 break;
2484 a = edit_get_byte (edit, q);
2485 /* don't want to eat CPU */
2486 if (j++ > furthest_bracket_search)
2487 break;
2488 /* out of screen? */
2489 if (in_screen) {
2490 if (q < edit->start_display)
2491 break;
2492 /* count lines if searching downward */
2493 if (inc > 0 && a == '\n')
2494 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2495 break;
2497 /* count bracket depth */
2498 i += (a == c) - (a == d);
2499 /* return if bracket depth is zero */
2500 if (!i)
2501 return q;
2503 /* no match */
2504 return -1;
2507 static long last_bracket = -1;
2509 void edit_find_bracket (WEdit * edit)
2511 edit->bracket = edit_get_bracket (edit, 1, 10000);
2512 if (last_bracket != edit->bracket)
2513 edit->force |= REDRAW_PAGE;
2514 last_bracket = edit->bracket;
2517 static inline void
2518 edit_goto_matching_bracket (WEdit *edit)
2520 long q;
2522 q = edit_get_bracket (edit, 0, 0);
2523 if (q >= 0) {
2524 edit->bracket = edit->curs1;
2525 edit->force |= REDRAW_PAGE;
2526 edit_cursor_move (edit, q - edit->curs1);
2531 * This executes a command as though the user initiated it through a key
2532 * press. Callback with WIDGET_KEY as a message calls this after
2533 * translating the key press. This function can be used to pass any
2534 * command to the editor. Note that the screen wouldn't update
2535 * automatically. Either of command or char_for_insertion must be
2536 * passed as -1. Commands are executed, and char_for_insertion is
2537 * inserted at the cursor.
2539 void
2540 edit_execute_key_command (WEdit *edit, unsigned long command, int char_for_insertion)
2542 if (command == CK_Begin_Record_Macro) {
2543 edit->macro_i = 0;
2544 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2545 return;
2547 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2548 edit->force |= REDRAW_COMPLETELY;
2549 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2550 edit->macro_i = -1;
2551 return;
2553 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2554 edit->macro[edit->macro_i].command = command;
2555 edit->macro[edit->macro_i++].ch = char_for_insertion;
2557 /* record the beginning of a set of editing actions initiated by a key press */
2558 if (command != CK_Undo && command != CK_Ext_Mode)
2559 edit_push_key_press (edit);
2561 edit_execute_cmd (edit, command, char_for_insertion);
2562 if (column_highlighting)
2563 edit->force |= REDRAW_PAGE;
2566 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2569 This executes a command at a lower level than macro recording.
2570 It also does not push a key_press onto the undo stack. This means
2571 that if it is called many times, a single undo command will undo
2572 all of them. It also does not check for the Undo command.
2574 void
2575 edit_execute_cmd (WEdit *edit, unsigned long command, int char_for_insertion)
2577 edit->force |= REDRAW_LINE;
2579 /* The next key press will unhighlight the found string, so update
2580 * the whole page */
2581 if (edit->found_len || column_highlighting)
2582 edit->force |= REDRAW_PAGE;
2584 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2585 column_highlighting = 0;
2586 if (!edit->highlight
2587 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2588 edit_mark_cmd (edit, 1); /* clear */
2589 edit_mark_cmd (edit, 0); /* marking on */
2591 edit->highlight = 1;
2592 } else { /* any other command */
2593 if (edit->highlight)
2594 edit_mark_cmd (edit, 0); /* clear */
2595 edit->highlight = 0;
2598 /* first check for undo */
2599 if (command == CK_Undo) {
2600 edit_do_undo (edit);
2601 edit->found_len = 0;
2602 edit->prev_col = edit_get_col (edit);
2603 edit->search_start = edit->curs1;
2604 return;
2607 /* An ordinary key press */
2608 if (char_for_insertion >= 0) {
2609 if (edit->overwrite) {
2610 if (edit_get_byte (edit, edit->curs1) != '\n')
2611 edit_delete (edit, 0);
2613 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2614 edit_insert_over (edit);
2615 #ifdef HAVE_CHARSET
2616 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2617 unsigned char str[6 + 1];
2618 size_t i = 0;
2619 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2620 if ( res == 0 ) {
2621 str[0] = '.';
2622 str[1] = '\0';
2623 } else {
2624 str[res] = '\0';
2626 while ( str[i] != 0 && i<=6) {
2627 char_for_insertion = str[i];
2628 edit_insert (edit, char_for_insertion);
2629 i++;
2631 } else
2632 #endif
2633 edit_insert (edit, char_for_insertion);
2635 if (option_auto_para_formatting) {
2636 format_paragraph (edit, 0);
2637 edit->force |= REDRAW_PAGE;
2638 } else
2639 check_and_wrap_line (edit);
2640 edit->found_len = 0;
2641 edit->prev_col = edit_get_col (edit);
2642 edit->search_start = edit->curs1;
2643 edit_find_bracket (edit);
2644 return;
2647 switch (command) {
2648 case CK_Begin_Page:
2649 case CK_End_Page:
2650 case CK_Begin_Page_Highlight:
2651 case CK_End_Page_Highlight:
2652 case CK_Word_Left:
2653 case CK_Word_Right:
2654 case CK_Up:
2655 case CK_Down:
2656 case CK_Left:
2657 case CK_Right:
2658 if ( edit->mark2 >= 0 ) {
2659 if ( !option_persistent_selections ) {
2660 if (column_highlighting)
2661 edit_push_action (edit, COLUMN_ON);
2662 column_highlighting = 0;
2663 edit_mark_cmd (edit, 1);
2668 switch (command) {
2669 case CK_Begin_Page:
2670 case CK_End_Page:
2671 case CK_Begin_Page_Highlight:
2672 case CK_End_Page_Highlight:
2673 case CK_Word_Left:
2674 case CK_Word_Right:
2675 case CK_Up:
2676 case CK_Down:
2677 case CK_Word_Left_Highlight:
2678 case CK_Word_Right_Highlight:
2679 case CK_Up_Highlight:
2680 case CK_Down_Highlight:
2681 case CK_Up_Alt_Highlight:
2682 case CK_Down_Alt_Highlight:
2683 if (edit->mark2 == -1)
2684 break; /*marking is following the cursor: may need to highlight a whole line */
2685 case CK_Left:
2686 case CK_Right:
2687 case CK_Left_Highlight:
2688 case CK_Right_Highlight:
2689 edit->force |= REDRAW_CHAR_ONLY;
2692 /* basic cursor key commands */
2693 switch (command) {
2694 case CK_BackSpace:
2695 /* if non persistent selection and text selected */
2696 if ( !option_persistent_selections ) {
2697 if ( edit->mark1 != edit->mark2 ) {
2698 edit_block_delete_cmd (edit);
2699 break;
2702 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2703 edit->over_col--;
2704 break;
2706 if (option_backspace_through_tabs && is_in_indent (edit)) {
2707 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2708 && edit->curs1 > 0)
2709 edit_backspace (edit, 1);
2710 break;
2711 } else {
2712 if (option_fake_half_tabs) {
2713 int i;
2714 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2715 for (i = 0; i < HALF_TAB_SIZE; i++)
2716 edit_backspace (edit, 1);
2717 break;
2721 edit_backspace (edit, 0);
2722 break;
2723 case CK_Delete:
2724 /* if non persistent selection and text selected */
2725 if ( !option_persistent_selections ) {
2726 if ( edit->mark1 != edit->mark2 ) {
2727 edit_block_delete_cmd (edit);
2728 break;
2732 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2733 edit_insert_over (edit);
2735 if (option_fake_half_tabs) {
2736 int i;
2737 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2738 for (i = 1; i <= HALF_TAB_SIZE; i++)
2739 edit_delete (edit, 1);
2740 break;
2743 edit_delete (edit, 0);
2744 break;
2745 case CK_Delete_Word_Left:
2746 edit->over_col = 0;
2747 edit_left_delete_word (edit);
2748 break;
2749 case CK_Delete_Word_Right:
2750 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2751 edit_insert_over (edit);
2753 edit_right_delete_word (edit);
2754 break;
2755 case CK_Delete_Line:
2756 edit_delete_line (edit);
2757 break;
2758 case CK_Delete_To_Line_End:
2759 edit_delete_to_line_end (edit);
2760 break;
2761 case CK_Delete_To_Line_Begin:
2762 edit_delete_to_line_begin (edit);
2763 break;
2764 case CK_Enter:
2765 edit->over_col = 0;
2766 if (option_auto_para_formatting) {
2767 edit_double_newline (edit);
2768 if (option_return_does_auto_indent)
2769 edit_auto_indent (edit);
2770 format_paragraph (edit, 0);
2771 } else {
2772 edit_insert (edit, '\n');
2773 if (option_return_does_auto_indent) {
2774 edit_auto_indent (edit);
2777 break;
2778 case CK_Return:
2779 edit_insert (edit, '\n');
2780 break;
2782 case CK_Page_Up_Alt_Highlight:
2783 column_highlighting = 1;
2784 case CK_Page_Up:
2785 case CK_Page_Up_Highlight:
2786 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2787 break;
2788 case CK_Page_Down_Alt_Highlight:
2789 column_highlighting = 1;
2790 case CK_Page_Down:
2791 case CK_Page_Down_Highlight:
2792 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2793 break;
2794 case CK_Left_Alt_Highlight:
2795 column_highlighting = 1;
2796 case CK_Left:
2797 case CK_Left_Highlight:
2798 if (option_fake_half_tabs) {
2799 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2800 if ( option_cursor_beyond_eol && edit->over_col > 0)
2801 edit->over_col--;
2802 else
2803 edit_cursor_move (edit, -HALF_TAB_SIZE);
2804 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2805 break;
2808 edit_left_char_move_cmd (edit);
2809 break;
2810 case CK_Right_Alt_Highlight:
2811 column_highlighting = 1;
2812 case CK_Right:
2813 case CK_Right_Highlight:
2814 if (option_fake_half_tabs) {
2815 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2816 edit_cursor_move (edit, HALF_TAB_SIZE);
2817 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2818 break;
2821 edit_right_char_move_cmd (edit);
2822 break;
2823 case CK_Begin_Page:
2824 case CK_Begin_Page_Highlight:
2825 edit_begin_page (edit);
2826 break;
2827 case CK_End_Page:
2828 case CK_End_Page_Highlight:
2829 edit_end_page (edit);
2830 break;
2831 case CK_Word_Left:
2832 case CK_Word_Left_Highlight:
2833 edit->over_col = 0;
2834 edit_left_word_move_cmd (edit);
2835 break;
2836 case CK_Word_Right:
2837 case CK_Word_Right_Highlight:
2838 edit->over_col = 0;
2839 edit_right_word_move_cmd (edit);
2840 break;
2841 case CK_Up_Alt_Highlight:
2842 column_highlighting = 1;
2843 case CK_Up:
2844 case CK_Up_Highlight:
2845 edit_move_up (edit, 1, 0);
2846 break;
2847 case CK_Down_Alt_Highlight:
2848 column_highlighting = 1;
2849 case CK_Down:
2850 case CK_Down_Highlight:
2851 edit_move_down (edit, 1, 0);
2852 break;
2853 case CK_Paragraph_Up_Alt_Highlight:
2854 column_highlighting = 1;
2855 case CK_Paragraph_Up:
2856 case CK_Paragraph_Up_Highlight:
2857 edit_move_up_paragraph (edit, 0);
2858 break;
2859 case CK_Paragraph_Down_Alt_Highlight:
2860 column_highlighting = 1;
2861 case CK_Paragraph_Down:
2862 case CK_Paragraph_Down_Highlight:
2863 edit_move_down_paragraph (edit, 0);
2864 break;
2865 case CK_Scroll_Up_Alt_Highlight:
2866 column_highlighting = 1;
2867 case CK_Scroll_Up:
2868 case CK_Scroll_Up_Highlight:
2869 edit_move_up (edit, 1, 1);
2870 break;
2871 case CK_Scroll_Down_Alt_Highlight:
2872 column_highlighting = 1;
2873 case CK_Scroll_Down:
2874 case CK_Scroll_Down_Highlight:
2875 edit_move_down (edit, 1, 1);
2876 break;
2877 case CK_Home:
2878 case CK_Home_Highlight:
2879 edit_cursor_to_bol (edit);
2880 break;
2881 case CK_End:
2882 case CK_End_Highlight:
2883 edit_cursor_to_eol (edit);
2884 break;
2885 case CK_Tab:
2886 /* if text marked shift block */
2887 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2888 if (edit->mark2 < 0)
2889 edit_mark_cmd (edit, 0);
2890 edit_move_block_to_right (edit);
2891 } else {
2892 if ( option_cursor_beyond_eol )
2893 edit_insert_over (edit);
2894 edit_tab_cmd (edit);
2895 if (option_auto_para_formatting) {
2896 format_paragraph (edit, 0);
2897 edit->force |= REDRAW_PAGE;
2898 } else {
2899 check_and_wrap_line (edit);
2902 break;
2904 case CK_Toggle_Insert:
2905 edit->overwrite = (edit->overwrite == 0);
2906 break;
2908 case CK_Mark:
2909 if (edit->mark2 >= 0) {
2910 if (column_highlighting)
2911 edit_push_action (edit, COLUMN_ON);
2912 column_highlighting = 0;
2914 edit_mark_cmd (edit, 0);
2915 break;
2916 case CK_Column_Mark:
2917 if (!column_highlighting)
2918 edit_push_action (edit, COLUMN_OFF);
2919 column_highlighting = 1;
2920 edit_mark_cmd (edit, 0);
2921 break;
2922 case CK_Unmark:
2923 if (column_highlighting)
2924 edit_push_action (edit, COLUMN_ON);
2925 column_highlighting = 0;
2926 edit_mark_cmd (edit, 1);
2927 break;
2929 case CK_Toggle_Line_State:
2930 option_line_state = !option_line_state;
2931 if ( option_line_state ) {
2932 option_line_state_width = LINE_STATE_WIDTH;
2933 } else {
2934 option_line_state_width = 0;
2936 edit->force |= REDRAW_PAGE;
2937 break;
2939 case CK_Toggle_Show_Margin:
2940 show_right_margin = !show_right_margin;
2941 edit->force |= REDRAW_PAGE;
2942 break;
2944 case CK_Toggle_Bookmark:
2945 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2946 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2947 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2948 else
2949 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2950 break;
2951 case CK_Flush_Bookmarks:
2952 book_mark_flush (edit, BOOK_MARK_COLOR);
2953 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2954 edit->force |= REDRAW_PAGE;
2955 break;
2956 case CK_Next_Bookmark:
2957 if (edit->book_mark) {
2958 struct _book_mark *p;
2959 p = (struct _book_mark *) book_mark_find (edit,
2960 edit->curs_line);
2961 if (p->next) {
2962 p = p->next;
2963 if (p->line >= edit->start_line + edit->num_widget_lines
2964 || p->line < edit->start_line)
2965 edit_move_display (edit,
2966 p->line -
2967 edit->num_widget_lines / 2);
2968 edit_move_to_line (edit, p->line);
2971 break;
2972 case CK_Prev_Bookmark:
2973 if (edit->book_mark) {
2974 struct _book_mark *p;
2975 p = (struct _book_mark *) book_mark_find (edit,
2976 edit->curs_line);
2977 while (p->line == edit->curs_line)
2978 if (p->prev)
2979 p = p->prev;
2980 if (p->line >= 0) {
2981 if (p->line >= edit->start_line + edit->num_widget_lines
2982 || p->line < edit->start_line)
2983 edit_move_display (edit,
2984 p->line -
2985 edit->num_widget_lines / 2);
2986 edit_move_to_line (edit, p->line);
2989 break;
2991 case CK_Beginning_Of_Text:
2992 case CK_Beginning_Of_Text_Highlight:
2993 edit_move_to_top (edit);
2994 break;
2995 case CK_End_Of_Text:
2996 case CK_End_Of_Text_Highlight:
2997 edit_move_to_bottom (edit);
2998 break;
3000 case CK_Copy:
3001 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3002 edit_insert_over (edit);
3003 edit_block_copy_cmd (edit);
3004 break;
3005 case CK_Remove:
3006 edit_block_delete_cmd (edit);
3007 break;
3008 case CK_Move:
3009 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3010 edit_insert_over (edit);
3011 edit_block_move_cmd (edit);
3012 break;
3014 case CK_Shift_Block_Left:
3015 if (edit->mark1 != edit->mark2)
3016 edit_move_block_to_left (edit);
3017 break;
3018 case CK_Shift_Block_Right:
3019 if (edit->mark1 != edit->mark2)
3020 edit_move_block_to_right (edit);
3021 break;
3022 case CK_XStore:
3023 edit_copy_to_X_buf_cmd (edit);
3024 break;
3025 case CK_XCut:
3026 edit_cut_to_X_buf_cmd (edit);
3027 break;
3028 case CK_XPaste:
3029 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3030 edit_insert_over (edit);
3031 edit_paste_from_X_buf_cmd (edit);
3032 break;
3033 case CK_Selection_History:
3034 edit_paste_from_history (edit);
3035 break;
3037 case CK_Save_As:
3038 edit_save_as_cmd (edit);
3039 break;
3040 case CK_Save:
3041 edit_save_confirm_cmd (edit);
3042 break;
3043 case CK_Load:
3044 edit_load_cmd (edit, EDIT_FILE_COMMON);
3045 break;
3046 case CK_Save_Block:
3047 edit_save_block_cmd (edit);
3048 break;
3049 case CK_Insert_File:
3050 edit_insert_file_cmd (edit);
3051 break;
3053 case CK_Load_Prev_File:
3054 edit_load_back_cmd (edit);
3055 break;
3056 case CK_Load_Next_File:
3057 edit_load_forward_cmd (edit);
3058 break;
3060 case CK_Load_Syntax_File:
3061 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3062 break;
3063 case CK_Choose_Syntax:
3064 edit_syntax_dialog ();
3065 break;
3067 case CK_Load_Menu_File:
3068 edit_load_cmd (edit, EDIT_FILE_MENU);
3069 break;
3071 case CK_Toggle_Syntax:
3072 if ((option_syntax_highlighting ^= 1) == 1)
3073 edit_load_syntax (edit, NULL, option_syntax_type);
3074 edit->force |= REDRAW_PAGE;
3075 break;
3077 case CK_Toggle_Tab_TWS:
3078 enable_show_tabs_tws ^= 1;
3079 edit->force |= REDRAW_PAGE;
3080 break;
3082 case CK_Find:
3083 edit_search_cmd (edit, 0);
3084 break;
3085 case CK_Find_Again:
3086 edit_search_cmd (edit, 1);
3087 break;
3088 case CK_Replace:
3089 edit_replace_cmd (edit, 0);
3090 break;
3091 case CK_Replace_Again:
3092 edit_replace_cmd (edit, 1);
3093 break;
3094 case CK_Complete_Word:
3095 /* if text marked shift block */
3096 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3097 edit_move_block_to_left (edit);
3098 } else {
3099 edit_complete_word_cmd (edit);
3101 break;
3102 case CK_Find_Definition:
3103 edit_get_match_keyword_cmd (edit);
3104 break;
3105 case CK_Quit:
3106 dlg_stop (edit->widget.parent);
3107 break;
3108 case CK_New:
3109 edit_new_cmd (edit);
3110 break;
3111 case CK_Help:
3112 edit_help_cmd (edit);
3113 break;
3114 case CK_Refresh:
3115 edit_refresh_cmd (edit);
3116 break;
3117 case CK_SaveSetupCmd:
3118 save_setup_cmd ();
3119 break;
3120 case CK_About:
3121 query_dialog (_(" About "),
3122 _("\n Cooledit v3.11.5\n\n"
3123 " Copyright (C) 1996 the Free Software Foundation\n\n"
3124 " A user friendly text editor written\n"
3125 " for the Midnight Commander.\n"), D_NORMAL,
3126 1, _("&OK"));
3127 break;
3128 case CK_LearnKeys:
3129 learn_keys ();
3130 break;
3131 case CK_Edit_Options:
3132 edit_options_dialog ();
3133 break;
3134 case CK_Edit_Save_Mode:
3135 menu_save_mode_cmd ();
3136 break;
3137 case CK_Date:
3139 char s[BUF_MEDIUM];
3140 /* fool gcc to prevent a Y2K warning */
3141 char time_format[] = "_c";
3142 time_format[0] = '%';
3144 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3145 edit_print_string (edit, s);
3146 edit->force |= REDRAW_PAGE;
3147 break;
3149 break;
3150 case CK_Goto:
3151 edit_goto_cmd (edit);
3152 break;
3153 case CK_Paragraph_Format:
3154 format_paragraph (edit, 1);
3155 edit->force |= REDRAW_PAGE;
3156 break;
3157 case CK_Delete_Macro:
3158 edit_delete_macro_cmd (edit);
3159 break;
3160 case CK_Match_Bracket:
3161 edit_goto_matching_bracket (edit);
3162 break;
3163 case CK_User_Menu:
3164 user_menu (edit);
3165 break;
3166 case CK_Sort:
3167 edit_sort_cmd (edit);
3168 break;
3169 case CK_ExtCmd:
3170 edit_ext_cmd (edit);
3171 break;
3172 case CK_Mail:
3173 edit_mail_dialog (edit);
3174 break;
3175 case CK_Shell:
3176 view_other_cmd ();
3177 break;
3178 case CK_SelectCodepage:
3179 edit_select_codepage_cmd (edit);
3180 break;
3181 case CK_Insert_Literal:
3182 edit_insert_literal_cmd (edit);
3183 break;
3184 case CK_Execute_Macro:
3185 edit_execute_macro_cmd (edit);
3186 break;
3187 case CK_Begin_End_Macro:
3188 edit_begin_end_macro_cmd (edit);
3189 break;
3190 case CK_Ext_Mode:
3191 edit->extmod = 1;
3192 break;
3193 default:
3194 break;
3197 /* CK_Pipe_Block */
3198 if ((command / 1000) == 1) /* a shell command */
3199 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3200 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3201 struct macro m[MAX_MACRO_LENGTH];
3202 int nm;
3203 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3204 edit_execute_macro (edit, m, nm);
3207 /* keys which must set the col position, and the search vars */
3208 switch (command) {
3209 case CK_Find:
3210 case CK_Find_Again:
3211 case CK_Replace:
3212 case CK_Replace_Again:
3213 case CK_Complete_Word:
3214 edit->prev_col = edit_get_col (edit);
3215 break;
3216 case CK_Up:
3217 case CK_Up_Highlight:
3218 case CK_Up_Alt_Highlight:
3219 case CK_Down:
3220 case CK_Down_Highlight:
3221 case CK_Down_Alt_Highlight:
3222 case CK_Page_Up:
3223 case CK_Page_Up_Highlight:
3224 case CK_Page_Up_Alt_Highlight:
3225 case CK_Page_Down:
3226 case CK_Page_Down_Highlight:
3227 case CK_Page_Down_Alt_Highlight:
3228 case CK_Beginning_Of_Text:
3229 case CK_Beginning_Of_Text_Highlight:
3230 case CK_End_Of_Text:
3231 case CK_End_Of_Text_Highlight:
3232 case CK_Paragraph_Up:
3233 case CK_Paragraph_Up_Highlight:
3234 case CK_Paragraph_Up_Alt_Highlight:
3235 case CK_Paragraph_Down:
3236 case CK_Paragraph_Down_Highlight:
3237 case CK_Paragraph_Down_Alt_Highlight:
3238 case CK_Scroll_Up:
3239 case CK_Scroll_Up_Highlight:
3240 case CK_Scroll_Up_Alt_Highlight:
3241 case CK_Scroll_Down:
3242 case CK_Scroll_Down_Highlight:
3243 case CK_Scroll_Down_Alt_Highlight:
3244 edit->search_start = edit->curs1;
3245 edit->found_len = 0;
3246 break;
3247 default:
3248 edit->found_len = 0;
3249 edit->prev_col = edit_get_col (edit);
3250 edit->search_start = edit->curs1;
3252 edit_find_bracket (edit);
3254 if (option_auto_para_formatting) {
3255 switch (command) {
3256 case CK_BackSpace:
3257 case CK_Delete:
3258 case CK_Delete_Word_Left:
3259 case CK_Delete_Word_Right:
3260 case CK_Delete_To_Line_End:
3261 case CK_Delete_To_Line_Begin:
3262 format_paragraph (edit, 0);
3263 edit->force |= REDRAW_PAGE;
3269 static void
3270 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3272 int i = 0;
3274 if (edit->macro_depth++ > 256) {
3275 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3276 edit->macro_depth--;
3277 return;
3279 edit->force |= REDRAW_PAGE;
3280 for (; i < n; i++) {
3281 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3283 edit_update_screen (edit);
3284 edit->macro_depth--;
3287 /* User edit menu, like user menu (F2) but only in editor. */
3288 static void
3289 user_menu (WEdit * edit)
3291 char *block_file;
3292 int nomark;
3293 long start_mark, end_mark;
3294 struct stat status;
3296 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3298 nomark = eval_marks (edit, &start_mark, &end_mark);
3299 if (nomark == 0)
3300 edit_save_block (edit, block_file, start_mark, end_mark);
3302 /* run shell scripts from menu */
3303 user_menu_cmd (edit);
3305 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0)) {
3306 int rc = 0;
3307 FILE *fd;
3309 if (nomark == 0) {
3310 /* i.e. we have marked block */
3311 rc = edit_block_delete_cmd (edit);
3314 if (rc == 0)
3315 edit_insert_file (edit, block_file);
3317 /* truncate block file */
3318 fd = fopen (block_file, "w");
3319 if (fd != NULL)
3320 fclose (fd);
3322 edit_refresh_cmd (edit);
3323 edit->force |= REDRAW_COMPLETELY;
3325 g_free (block_file);
3328 void
3329 edit_stack_init (void)
3331 for (edit_stack_iterator = 0;
3332 edit_stack_iterator < MAX_HISTORY_MOVETO;
3333 edit_stack_iterator++ ) {
3334 edit_history_moveto[edit_stack_iterator].filename = NULL;
3335 edit_history_moveto[edit_stack_iterator].line = -1;
3338 edit_stack_iterator = 0;
3341 void
3342 edit_stack_free (void)
3344 for (edit_stack_iterator = 0;
3345 edit_stack_iterator < MAX_HISTORY_MOVETO;
3346 edit_stack_iterator++)
3347 g_free (edit_history_moveto[edit_stack_iterator].filename);
3350 /* move i lines */
3351 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
3353 edit_move_updown (edit, i, scroll, TRUE);
3356 /* move i lines */
3357 void edit_move_down (WEdit * edit, unsigned long i, int scroll)
3359 edit_move_updown (edit, i, scroll, FALSE);