Ticket #1982: Segfault while opening gzip archive
[midnight-commander.git] / src / editor / edit.c
blob5fa2ca543b0622759e9a8ec6a6d7dcf1192be440
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 "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
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"
66 int option_word_wrap_line_length = 72;
67 int option_typewriter_wrap = 0;
68 int option_auto_para_formatting = 0;
69 int option_fill_tabs_with_spaces = 0;
70 int option_return_does_auto_indent = 1;
71 int option_backspace_through_tabs = 0;
72 int option_fake_half_tabs = 1;
73 int option_save_mode = EDIT_QUICK_SAVE;
74 int option_save_position = 1;
75 int option_max_undo = 32768;
76 int option_persistent_selections = 1;
77 int option_cursor_beyond_eol = 0;
78 int option_line_state = 0;
79 int option_line_state_width = 0;
81 int option_edit_right_extreme = 0;
82 int option_edit_left_extreme = 0;
83 int option_edit_top_extreme = 0;
84 int option_edit_bottom_extreme = 0;
85 int enable_show_tabs_tws = 1;
86 int option_check_nl_at_eof = 0;
87 int show_right_margin = 0;
89 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext = NULL;
92 int edit_stack_iterator = 0;
93 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
96 /*-
98 * here's a quick sketch of the layout: (don't run this through indent.)
100 * (b1 is buffers1 and b2 is buffers2)
103 * \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
104 * ______________________________________|______________________________________
106 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
107 * |-> |-> |-> |-> |-> |-> |
109 * _<------------------------->|<----------------->_
110 * WEdit->curs2 | WEdit->curs1
111 * ^ | ^
112 * | ^|^ |
113 * cursor ||| cursor
114 * |||
115 * file end|||file beginning
120 * This_is_some_file
121 * fin.
124 const global_keymap_t *editor_map;
125 const global_keymap_t *editor_x_map;
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;
146 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
147 return NULL;
149 if (byte_index >= edit->curs1) {
150 p = edit->curs1 + edit->curs2 - byte_index - 1;
151 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
152 } else {
153 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
157 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
159 unsigned long p;
161 if (byte_index >= (edit->curs1 + edit->curs2))
162 byte_index--;
164 if (byte_index < 0)
165 return NULL;
167 if (byte_index >= edit->curs1) {
168 p = edit->curs1 + edit->curs2 - 1;
169 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
170 } else {
171 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
175 int edit_get_utf (WEdit * edit, long byte_index, int *char_width)
177 gchar *str = NULL;
178 int res = -1;
179 gunichar ch;
180 gchar *next_ch = NULL;
181 int width = 0;
183 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
184 *char_width = 0;
185 return '\n';
188 str = edit_get_byte_ptr (edit, byte_index);
190 if (str == NULL) {
191 *char_width = 0;
192 return 0;
195 res = g_utf8_get_char_validated (str, -1);
197 if (res < 0) {
198 ch = *str;
199 width = 0;
200 } else {
201 ch = res;
202 /* Calculate UTF-8 char width */
203 next_ch = g_utf8_next_char (str);
204 if (next_ch) {
205 width = next_ch - str;
206 } else {
207 ch = 0;
208 width = 0;
211 *char_width = width;
212 return ch;
215 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
217 gchar *str, *buf = NULL;
218 int res = -1;
219 gunichar ch;
220 gchar *next_ch = NULL;
221 int width = 0;
223 if (byte_index > 0)
224 byte_index--;
226 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) {
227 *char_width = 0;
228 return 0;
231 ch = edit_get_utf (edit, byte_index, &width);
233 if (width == 1) {
234 *char_width = width;
235 return ch;
238 str = edit_get_byte_ptr (edit, byte_index);
239 buf = edit_get_buf_ptr (edit, byte_index);
240 if (str == NULL || buf == NULL) {
241 *char_width = 0;
242 return 0;
244 /* get prev utf8 char */
245 if (str != buf)
246 str = g_utf8_find_prev_char (buf, str);
248 res = g_utf8_get_char_validated (str, -1);
250 if (res < 0) {
251 ch = *str;
252 width = 0;
253 } else {
254 ch = res;
255 /* Calculate UTF-8 char width */
256 next_ch = g_utf8_next_char(str);
257 if (next_ch) {
258 width = next_ch - str;
259 } else {
260 ch = 0;
261 width = 0;
264 *char_width = width;
265 return ch;
269 * Initialize the buffers for an empty files.
271 static void
272 edit_init_buffers (WEdit *edit)
274 int j;
276 for (j = 0; j <= MAXBUFF; j++) {
277 edit->buffers1[j] = NULL;
278 edit->buffers2[j] = NULL;
281 edit->curs1 = 0;
282 edit->curs2 = 0;
283 edit->buffers2[0] = g_malloc0 (EDIT_BUF_SIZE);
287 * Load file OR text into buffers. Set cursor to the beginning of file.
288 * Return 1 on error.
290 static int
291 edit_load_file_fast (WEdit *edit, const char *filename)
293 long buf, buf2;
294 int file = -1;
295 edit->curs2 = edit->last_byte;
296 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
297 edit->utf8 = 0;
298 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
299 GString *errmsg = g_string_new(NULL);
300 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
301 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
302 g_string_free (errmsg, TRUE);
303 return 1;
306 if (!edit->buffers2[buf2])
307 edit->buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE);
309 mc_read (file,
310 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
311 (edit->curs2 & M_EDIT_BUF_SIZE),
312 edit->curs2 & M_EDIT_BUF_SIZE);
314 for (buf = buf2 - 1; buf >= 0; buf--) {
315 /* edit->buffers2[0] is already allocated */
316 if (!edit->buffers2[buf])
317 edit->buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE);
318 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
321 mc_close (file);
322 return 0;
325 /* detecting an error on save is easy: just check if every byte has been written. */
326 /* detecting an error on read, is not so easy 'cos there is not way to tell
327 whether you read everything or not. */
328 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
329 static const struct edit_filters {
330 const char *read, *write, *extension;
331 } all_filters[] = {
332 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
333 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
334 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
335 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
336 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
339 /* Return index of the filter or -1 is there is no appropriate filter */
340 static int edit_find_filter (const char *filename)
342 size_t i, l, e;
343 if (!filename)
344 return -1;
345 l = strlen (filename);
346 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
347 e = strlen (all_filters[i].extension);
348 if (l > e)
349 if (!strcmp (all_filters[i].extension, filename + l - e))
350 return i;
352 return -1;
355 static char *
356 edit_get_filter (const char *filename)
358 int i;
359 char *p, *quoted_name;
360 i = edit_find_filter (filename);
361 if (i < 0)
362 return 0;
363 quoted_name = name_quote (filename, 0);
364 p = g_strdup_printf(all_filters[i].read, quoted_name);
365 g_free (quoted_name);
366 return p;
369 char *
370 edit_get_write_filter (const char *write_name, const char *filename)
372 int i;
373 char *p, *writename;
374 i = edit_find_filter (filename);
375 if (i < 0)
376 return 0;
377 writename = name_quote (write_name, 0);
378 p = g_strdup_printf(all_filters[i].write, writename);
379 g_free (writename);
380 return p;
383 static long
384 edit_insert_stream (WEdit * edit, FILE * f)
386 int c;
387 long i = 0;
388 while ((c = fgetc (f)) >= 0) {
389 edit_insert (edit, c);
390 i++;
392 return i;
395 long edit_write_stream (WEdit * edit, FILE * f)
397 long i;
399 if (edit->lb == LB_ASIS) {
400 for (i = 0; i < edit->last_byte; i++)
401 if (fputc (edit_get_byte (edit, i), f) < 0)
402 break;
403 return i;
406 /* change line breaks */
407 for (i = 0; i < edit->last_byte; i++) {
408 unsigned char c = edit_get_byte (edit, i);
410 if (!(c == '\n' || c == '\r')) {
411 /* not line break */
412 if (fputc (c, f) < 0)
413 return i;
414 } else { /* (c == '\n' || c == '\r') */
415 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
417 switch (edit->lb) {
418 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
419 /* put one line break unconditionally */
420 if (fputc ('\n', f) < 0)
421 return i;
423 i++; /* 2 chars are processed */
425 if (c == '\r' && c1 == '\n')
426 /* Windows line break; go to the next char */
427 break;
429 if (c == '\r' && c1 == '\r') {
430 /* two Macintosh line breaks; put second line break */
431 if (fputc ('\n', f) < 0)
432 return i;
433 break;
436 if (fputc (c1, f) < 0)
437 return i;
438 break;
440 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
441 /* put one line break unconditionally */
442 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
443 return i;
445 if (c == '\r' && c1 == '\n')
446 /* Windows line break; go to the next char */
447 i++;
448 break;
450 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
451 /* put one line break unconditionally */
452 if (fputc ('\r', f) < 0)
453 return i;
455 i++; /* 2 chars are processed */
457 if (c == '\r' && c1 == '\n')
458 /* Windows line break; go to the next char */
459 break;
461 if (c == '\n' && c1 == '\n') {
462 /* two Windows line breaks; put second line break */
463 if (fputc ('\r', f) < 0)
464 return i;
465 break;
468 if (fputc (c1, f) < 0)
469 return i;
470 break;
471 case LB_ASIS: /* default without changes */
472 break;
477 return edit->last_byte;
480 #define TEMP_BUF_LEN 1024
482 /* inserts a file at the cursor, returns 1 on success */
484 edit_insert_file (WEdit *edit, const char *filename)
486 char *p;
487 if ((p = edit_get_filter (filename))) {
488 FILE *f;
489 long current = edit->curs1;
490 f = (FILE *) popen (p, "r");
491 if (f) {
492 edit_insert_stream (edit, f);
493 edit_cursor_move (edit, current - edit->curs1);
494 if (pclose (f) > 0) {
495 GString *errmsg = g_string_new (NULL);
496 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
497 edit_error_dialog (_("Error"), errmsg->str);
498 g_string_free (errmsg, TRUE);
499 g_free (p);
500 return 0;
502 } else {
503 GString *errmsg = g_string_new (NULL);
504 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
505 edit_error_dialog (_("Error"), errmsg->str);
506 g_string_free (errmsg, TRUE);
507 g_free (p);
508 return 0;
510 g_free (p);
511 } else {
512 int i, file, blocklen;
513 long current = edit->curs1;
514 int vertical_insertion = 0;
515 char *buf;
516 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
517 return 0;
518 buf = g_malloc0 (TEMP_BUF_LEN);
519 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
520 if (blocklen > 0) {
521 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
522 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
523 vertical_insertion = 1;
524 } else {
525 mc_lseek (file, 0, SEEK_SET);
528 if (vertical_insertion) {
529 blocklen = edit_insert_column_of_text_from_file (edit, file);
530 } else {
531 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
532 for (i = 0; i < blocklen; i++)
533 edit_insert (edit, buf[i]);
536 edit_cursor_move (edit, current - edit->curs1);
537 g_free (buf);
538 mc_close (file);
539 if (blocklen)
540 return 0;
542 return 1;
545 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
546 static int
547 check_file_access (WEdit *edit, const char *filename, struct stat *st)
549 int file;
550 GString *errmsg = (GString *) 0;
552 /* Try opening an existing file */
553 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
555 if (file < 0) {
557 * Try creating the file. O_EXCL prevents following broken links
558 * and opening existing files.
560 file =
561 mc_open (filename,
562 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
563 0666);
564 if (file < 0) {
565 g_string_sprintf (errmsg = g_string_new (NULL),
566 _(" Cannot open %s for reading "), filename);
567 goto cleanup;
568 } else {
569 /* New file, delete it if it's not modified or saved */
570 edit->delete_file = 1;
574 /* Check what we have opened */
575 if (mc_fstat (file, st) < 0) {
576 g_string_sprintf (errmsg = g_string_new (NULL),
577 _(" Cannot get size/permissions for %s "), filename);
578 goto cleanup;
581 /* We want to open regular files only */
582 if (!S_ISREG (st->st_mode)) {
583 g_string_sprintf (errmsg = g_string_new (NULL),
584 _(" %s is not a regular file "), filename);
585 goto cleanup;
589 * Don't delete non-empty files.
590 * O_EXCL should prevent it, but let's be on the safe side.
592 if (st->st_size > 0) {
593 edit->delete_file = 0;
596 if (st->st_size >= SIZE_LIMIT) {
597 g_string_sprintf (errmsg = g_string_new (NULL),
598 _(" File %s is too large "), filename);
601 cleanup:
602 (void) mc_close (file);
603 if (errmsg) {
604 edit_error_dialog (_("Error"), errmsg->str);
605 g_string_free (errmsg, TRUE);
606 return 1;
608 return 0;
612 * Open the file and load it into the buffers, either directly or using
613 * a filter. Return 0 on success, 1 on error.
615 * Fast loading (edit_load_file_fast) is used when the file size is
616 * known. In this case the data is read into the buffers by blocks.
617 * If the file size is not known, the data is loaded byte by byte in
618 * edit_insert_file.
620 static int
621 edit_load_file (WEdit *edit)
623 int fast_load = 1;
625 /* Cannot do fast load if a filter is used */
626 if (edit_find_filter (edit->filename) >= 0)
627 fast_load = 0;
630 * VFS may report file size incorrectly, and slow load is not a big
631 * deal considering overhead in VFS.
633 if (!vfs_file_is_local (edit->filename))
634 fast_load = 0;
637 * FIXME: line end translation should disable fast loading as well
638 * Consider doing fseek() to the end and ftell() for the real size.
641 if (*edit->filename) {
642 /* If we are dealing with a real file, check that it exists */
643 if (check_file_access (edit, edit->filename, &edit->stat1))
644 return 1;
645 } else {
646 /* nothing to load */
647 fast_load = 0;
650 edit_init_buffers (edit);
652 if (fast_load) {
653 edit->last_byte = edit->stat1.st_size;
654 edit_load_file_fast (edit, edit->filename);
655 /* If fast load was used, the number of lines wasn't calculated */
656 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
657 } else {
658 #ifdef HAVE_CHARSET
659 const char *codepage_id;
660 #endif
661 edit->last_byte = 0;
662 if (*edit->filename) {
663 edit->stack_disable = 1;
664 if (!edit_insert_file (edit, edit->filename)) {
665 edit_clean (edit);
666 return 1;
668 edit->stack_disable = 0;
671 #ifdef HAVE_CHARSET
672 codepage_id = get_codepage_id( source_codepage );
673 if ( codepage_id )
674 edit->utf8 = str_isutf8 ( codepage_id );
675 #endif
677 edit->lb = LB_ASIS;
678 return 0;
681 /* Restore saved cursor position in the file */
682 static void
683 edit_load_position (WEdit *edit)
685 char *filename;
686 long line, column;
687 off_t offset;
689 if (!edit->filename || !*edit->filename)
690 return;
692 filename = vfs_canon (edit->filename);
693 load_file_position (filename, &line, &column, &offset);
694 g_free (filename);
696 if (line > 0) {
697 edit_move_to_line (edit, line - 1);
698 edit->prev_col = column;
699 } else if (offset > 0) {
700 edit_cursor_move (edit, offset);
701 line = edit->curs_line;
703 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
704 edit_move_display (edit, line - (edit->num_widget_lines / 2));
707 /* Save cursor position in the file */
708 static void
709 edit_save_position (WEdit *edit)
711 char *filename;
713 if (!edit->filename || !*edit->filename)
714 return;
716 filename = vfs_canon (edit->filename);
717 save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
718 g_free (filename);
721 /* Clean the WEdit stricture except the widget part */
722 static void
723 edit_purge_widget (WEdit *edit)
725 size_t len = sizeof (WEdit) - sizeof (Widget);
726 char *start = (char *) edit + sizeof (Widget);
727 memset (start, 0, len);
728 edit->macro_i = -1; /* not recording a macro */
731 static void
732 edit_set_keymap (void)
734 editor_map = default_editor_keymap;
735 if (editor_keymap && editor_keymap->len > 0)
736 editor_map = (global_keymap_t *) editor_keymap->data;
738 editor_x_map = default_editor_x_keymap;
739 if (editor_x_keymap && editor_x_keymap->len > 0)
740 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
744 #define space_width 1
747 * Fill in the edit structure. Return NULL on failure. Pass edit as
748 * NULL to allocate a new structure.
750 * If line is 0, try to restore saved position. Otherwise put the
751 * cursor on that line and show it in the middle of the screen.
753 WEdit *
754 edit_init (WEdit *edit, int lines, int columns, const char *filename,
755 long line)
757 int to_free = 0;
758 option_auto_syntax = 1; /* Resetting to auto on every invokation */
759 if ( option_line_state ) {
760 option_line_state_width = LINE_STATE_WIDTH;
761 } else {
762 option_line_state_width = 0;
764 if (!edit) {
765 #ifdef ENABLE_NLS
767 * Expand option_whole_chars_search by national letters using
768 * current locale
771 static char option_whole_chars_search_buf[256];
773 if (option_whole_chars_search_buf != option_whole_chars_search) {
774 size_t i;
775 size_t len = str_term_width1 (option_whole_chars_search);
777 strcpy (option_whole_chars_search_buf,
778 option_whole_chars_search);
780 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
781 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
782 option_whole_chars_search_buf[len++] = i;
786 option_whole_chars_search_buf[len] = 0;
787 option_whole_chars_search = option_whole_chars_search_buf;
789 #endif /* ENABLE_NLS */
790 edit = g_malloc0 (sizeof (WEdit));
791 edit->search = NULL;
792 to_free = 1;
794 edit_purge_widget (edit);
795 edit->num_widget_lines = lines;
796 edit->over_col = 0;
797 edit->num_widget_columns = columns;
798 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
799 edit->stat1.st_uid = getuid ();
800 edit->stat1.st_gid = getgid ();
801 edit->stat1.st_mtime = 0;
802 edit->bracket = -1;
803 edit->force |= REDRAW_PAGE;
804 edit_set_filename (edit, filename);
805 edit->stack_size = START_STACK_SIZE;
806 edit->stack_size_mask = START_STACK_SIZE - 1;
807 edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long));
808 if (edit_load_file (edit)) {
809 /* edit_load_file already gives an error message */
810 if (to_free)
811 g_free (edit);
812 return 0;
814 edit->utf8 = 0;
815 edit->converter = str_cnv_from_term;
816 #ifdef HAVE_CHARSET
818 const char *cp_id = NULL;
819 cp_id = get_codepage_id (source_codepage >= 0 ?
820 source_codepage : display_codepage);
822 if (cp_id != NULL) {
823 GIConv conv;
824 conv = str_crt_conv_from (cp_id);
825 if (conv != INVALID_CONV) {
826 if (edit->converter != str_cnv_from_term)
827 str_close_conv (edit->converter);
828 edit->converter = conv;
831 if (cp_id != NULL)
832 edit->utf8 = str_isutf8 (cp_id);
834 #endif
836 edit->loading_done = 1;
837 edit->modified = 0;
838 edit->locked = 0;
839 edit_load_syntax (edit, 0, 0);
841 int color;
842 edit_get_syntax_color (edit, -1, &color);
845 /* load saved cursor position */
846 if ((line == 0) && option_save_position) {
847 edit_load_position (edit);
848 } else {
849 if (line <= 0)
850 line = 1;
851 edit_move_display (edit, line - 1);
852 edit_move_to_line (edit, line - 1);
855 edit_set_keymap ();
857 return edit;
860 /* Clear the edit struct, freeing everything in it. Return 1 on success */
862 edit_clean (WEdit *edit)
864 int j = 0;
866 if (!edit)
867 return 0;
869 /* a stale lock, remove it */
870 if (edit->locked)
871 edit->locked = edit_unlock_file (edit->filename);
873 /* save cursor position */
874 if (option_save_position)
875 edit_save_position (edit);
877 /* File specified on the mcedit command line and never saved */
878 if (edit->delete_file)
879 unlink (edit->filename);
881 edit_free_syntax_rules (edit);
882 book_mark_flush (edit, -1);
883 for (; j <= MAXBUFF; j++) {
884 g_free (edit->buffers1[j]);
885 g_free (edit->buffers2[j]);
888 g_free (edit->undo_stack);
889 g_free (edit->filename);
890 g_free (edit->dir);
892 if (edit->search)
894 mc_search_free(edit->search);
895 edit->search = NULL;
897 edit_purge_widget (edit);
899 return 1;
902 /* returns 1 on success */
904 edit_renew (WEdit * edit)
906 int lines = edit->num_widget_lines;
907 int columns = edit->num_widget_columns;
909 edit_clean (edit);
910 return (edit_init (edit, lines, columns, "", 0) != NULL);
914 * Load a new file into the editor. If it fails, preserve the old file.
915 * To do it, allocate a new widget, initialize it and, if the new file
916 * was loaded, copy the data to the old widget.
917 * Return 1 on success, 0 on failure.
920 edit_reload (WEdit *edit, const char *filename)
922 WEdit *e;
923 int lines = edit->num_widget_lines;
924 int columns = edit->num_widget_columns;
926 e = g_malloc0 (sizeof (WEdit));
927 e->widget = edit->widget;
928 if (!edit_init (e, lines, columns, filename, 0)) {
929 g_free (e);
930 return 0;
932 edit_clean (edit);
933 memcpy (edit, e, sizeof (WEdit));
934 g_free (e);
935 return 1;
939 * Load a new file into the editor and set line. If it fails, preserve the old file.
940 * To do it, allocate a new widget, initialize it and, if the new file
941 * was loaded, copy the data to the old widget.
942 * Return 1 on success, 0 on failure.
945 edit_reload_line (WEdit *edit, const char *filename, long line)
947 WEdit *e;
948 int lines = edit->num_widget_lines;
949 int columns = edit->num_widget_columns;
951 e = g_malloc0 (sizeof (WEdit));
952 e->widget = edit->widget;
953 if (!edit_init (e, lines, columns, filename, line)) {
954 g_free (e);
955 return 0;
957 edit_clean (edit);
958 memcpy (edit, e, sizeof (WEdit));
959 g_free (e);
960 return 1;
965 Recording stack for undo:
966 The following is an implementation of a compressed stack. Identical
967 pushes are recorded by a negative prefix indicating the number of times the
968 same char was pushed. This saves space for repeated curs-left or curs-right
969 delete etc.
973 pushed: stored:
977 b -3
979 c --> -4
985 If the stack long int is 0-255 it represents a normal insert (from a backspace),
986 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
987 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
988 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
989 position.
991 The only way the cursor moves or the buffer is changed is through the routines:
992 insert, backspace, insert_ahead, delete, and cursor_move.
993 These record the reverse undo movements onto the stack each time they are
994 called.
996 Each key press results in a set of actions (insert; delete ...). So each time
997 a key is pressed the current position of start_display is pushed as
998 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
999 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1000 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1004 void edit_push_action (WEdit * edit, long c,...)
1006 unsigned long sp = edit->stack_pointer;
1007 unsigned long spm1;
1008 long *t;
1010 /* first enlarge the stack if necessary */
1011 if (sp > edit->stack_size - 10) { /* say */
1012 if (option_max_undo < 256)
1013 option_max_undo = 256;
1014 if (edit->stack_size < (unsigned long) option_max_undo) {
1015 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1016 if (t) {
1017 edit->undo_stack = t;
1018 edit->stack_size <<= 1;
1019 edit->stack_size_mask = edit->stack_size - 1;
1023 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1024 if (edit->stack_disable)
1025 return;
1027 #ifdef FAST_MOVE_CURSOR
1028 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1029 va_list ap;
1030 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1031 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1032 va_start (ap, c);
1033 c = -(va_arg (ap, int));
1034 va_end (ap);
1035 } else
1036 #endif /* ! FAST_MOVE_CURSOR */
1037 if (edit->stack_bottom != sp
1038 && spm1 != edit->stack_bottom
1039 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1040 int d;
1041 if (edit->undo_stack[spm1] < 0) {
1042 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1043 if (d == c) {
1044 if (edit->undo_stack[spm1] > -1000000000) {
1045 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1046 edit->undo_stack[spm1]--;
1047 return;
1050 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1051 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1052 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1053 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1054 if (edit->undo_stack[spm1] == -2)
1055 edit->stack_pointer = spm1;
1056 else
1057 edit->undo_stack[spm1]++;
1058 return;
1060 #endif
1061 } else {
1062 d = edit->undo_stack[spm1];
1063 if (d == c) {
1064 if (c >= KEY_PRESS)
1065 return; /* --> no need to push multiple do-nothings */
1066 edit->undo_stack[sp] = -2;
1067 goto check_bottom;
1069 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1070 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1071 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1072 edit->stack_pointer = spm1;
1073 return;
1075 #endif
1078 edit->undo_stack[sp] = c;
1080 check_bottom:
1081 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1083 /* if the sp wraps round and catches the stack_bottom then erase
1084 * the first set of actions on the stack to make space - by moving
1085 * stack_bottom forward one "key press" */
1086 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1087 if ((unsigned long) c == edit->stack_bottom ||
1088 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1089 do {
1090 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1091 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1093 /*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: */
1094 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1095 edit->stack_bottom = edit->stack_pointer = 0;
1099 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1100 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1102 static long
1103 pop_action (WEdit * edit)
1105 long c;
1106 unsigned long sp = edit->stack_pointer;
1107 if (sp == edit->stack_bottom) {
1108 return STACK_BOTTOM;
1110 sp = (sp - 1) & edit->stack_size_mask;
1111 if ((c = edit->undo_stack[sp]) >= 0) {
1112 /* edit->undo_stack[sp] = '@'; */
1113 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1114 return c;
1116 if (sp == edit->stack_bottom) {
1117 return STACK_BOTTOM;
1119 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1120 if (edit->undo_stack[sp] == -2) {
1121 /* edit->undo_stack[sp] = '@'; */
1122 edit->stack_pointer = sp;
1123 } else
1124 edit->undo_stack[sp]++;
1126 return c;
1129 /* is called whenever a modification is made by one of the four routines below */
1130 static void edit_modification (WEdit * edit)
1132 edit->caches_valid = 0;
1133 edit->screen_modified = 1;
1135 /* raise lock when file modified */
1136 if (!edit->modified && !edit->delete_file)
1137 edit->locked = edit_lock_file (edit->filename);
1138 edit->modified = 1;
1142 Basic low level single character buffer alterations and movements at the cursor.
1143 Returns char passed over, inserted or removed.
1146 void
1147 edit_insert (WEdit *edit, int c)
1149 /* check if file has grown to large */
1150 if (edit->last_byte >= SIZE_LIMIT)
1151 return;
1153 /* first we must update the position of the display window */
1154 if (edit->curs1 < edit->start_display) {
1155 edit->start_display++;
1156 if (c == '\n')
1157 edit->start_line++;
1160 /* Mark file as modified, unless the file hasn't been fully loaded */
1161 if (edit->loading_done) {
1162 edit_modification (edit);
1165 /* now we must update some info on the file and check if a redraw is required */
1166 if (c == '\n') {
1167 if (edit->book_mark)
1168 book_mark_inc (edit, edit->curs_line);
1169 edit->curs_line++;
1170 edit->total_lines++;
1171 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1174 /* save the reverse command onto the undo stack */
1175 edit_push_action (edit, BACKSPACE);
1177 /* update markers */
1178 edit->mark1 += (edit->mark1 > edit->curs1);
1179 edit->mark2 += (edit->mark2 > edit->curs1);
1180 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1182 /* add a new buffer if we've reached the end of the last one */
1183 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1184 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1185 g_malloc0 (EDIT_BUF_SIZE);
1187 /* perform the insertion */
1188 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1189 curs1 & M_EDIT_BUF_SIZE]
1190 = (unsigned char) c;
1192 /* update file length */
1193 edit->last_byte++;
1195 /* update cursor position */
1196 edit->curs1++;
1199 static void
1200 edit_insert_over (WEdit * edit)
1202 int i;
1204 for ( i = 0; i < edit->over_col; i++ ) {
1205 edit_insert (edit, ' ');
1207 edit->over_col = 0;
1210 /* same as edit_insert and move left */
1211 void edit_insert_ahead (WEdit * edit, int c)
1213 if (edit->last_byte >= SIZE_LIMIT)
1214 return;
1215 if (edit->curs1 < edit->start_display) {
1216 edit->start_display++;
1217 if (c == '\n')
1218 edit->start_line++;
1220 edit_modification (edit);
1221 if (c == '\n') {
1222 if (edit->book_mark)
1223 book_mark_inc (edit, edit->curs_line);
1224 edit->total_lines++;
1225 edit->force |= REDRAW_AFTER_CURSOR;
1227 edit_push_action (edit, DELCHAR);
1229 edit->mark1 += (edit->mark1 >= edit->curs1);
1230 edit->mark2 += (edit->mark2 >= edit->curs1);
1231 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1233 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1234 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1235 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1237 edit->last_byte++;
1238 edit->curs2++;
1242 int edit_delete (WEdit * edit, const int byte_delete)
1244 int p = 0;
1245 int cw = 1;
1246 int i;
1248 if (!edit->curs2)
1249 return 0;
1251 edit->mark1 -= (edit->mark1 > edit->curs1);
1252 edit->mark2 -= (edit->mark2 > edit->curs1);
1253 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1255 cw = 1;
1256 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1257 if ( edit->utf8 && byte_delete == 0 ) {
1258 edit_get_utf (edit, edit->curs1, &cw);
1259 if ( cw < 1 )
1260 cw = 1;
1262 for ( i = 1; i<= cw; i++ ) {
1263 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1265 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1266 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1267 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1269 edit->last_byte--;
1270 edit->curs2--;
1271 edit_push_action (edit, p + 256);
1274 edit_modification (edit);
1275 if (p == '\n') {
1276 if (edit->book_mark)
1277 book_mark_dec (edit, edit->curs_line);
1278 edit->total_lines--;
1279 edit->force |= REDRAW_AFTER_CURSOR;
1281 if (edit->curs1 < edit->start_display) {
1282 edit->start_display--;
1283 if (p == '\n')
1284 edit->start_line--;
1287 return p;
1291 static int
1292 edit_backspace (WEdit * edit, const int byte_delete)
1294 int p = 0;
1295 int cw = 1;
1296 int i;
1298 if (!edit->curs1)
1299 return 0;
1301 edit->mark1 -= (edit->mark1 >= edit->curs1);
1302 edit->mark2 -= (edit->mark2 >= edit->curs1);
1303 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1305 cw = 1;
1306 if ( edit->utf8 && byte_delete == 0 ) {
1307 edit_get_prev_utf (edit, edit->curs1, &cw);
1308 if ( cw < 1 )
1309 cw = 1;
1311 for ( i = 1; i<= cw; i++ ) {
1312 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1313 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1314 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1315 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1317 edit->last_byte--;
1318 edit->curs1--;
1319 edit_push_action (edit, p);
1321 edit_modification (edit);
1322 if (p == '\n') {
1323 if (edit->book_mark)
1324 book_mark_dec (edit, edit->curs_line);
1325 edit->curs_line--;
1326 edit->total_lines--;
1327 edit->force |= REDRAW_AFTER_CURSOR;
1330 if (edit->curs1 < edit->start_display) {
1331 edit->start_display--;
1332 if (p == '\n')
1333 edit->start_line--;
1336 return p;
1339 #ifdef FAST_MOVE_CURSOR
1341 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1343 unsigned long next;
1344 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1345 edit->curs_line--;
1346 next -= (unsigned long) dest;
1347 n -= next;
1348 src += next;
1349 dest += next;
1354 edit_move_backward_lots (WEdit *edit, long increment)
1356 int r, s, t;
1357 unsigned char *p = NULL;
1359 if (increment > edit->curs1)
1360 increment = edit->curs1;
1361 if (increment <= 0)
1362 return -1;
1363 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1365 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1366 if (r > increment)
1367 r = increment;
1368 s = edit->curs1 & M_EDIT_BUF_SIZE;
1370 if (s > r) {
1371 memqcpy (edit,
1372 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1373 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1375 } else {
1376 if (s) {
1377 memqcpy (edit,
1378 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1379 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1380 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1381 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1383 memqcpy (edit,
1384 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1385 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1386 EDIT_BUF_SIZE - (r - s), r - s);
1388 increment -= r;
1389 edit->curs1 -= r;
1390 edit->curs2 += r;
1391 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1392 if (p)
1393 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1394 else
1395 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1396 g_malloc0 (EDIT_BUF_SIZE);
1397 } else {
1398 g_free (p);
1401 s = edit->curs1 & M_EDIT_BUF_SIZE;
1402 while (increment) {
1403 p = 0;
1404 r = EDIT_BUF_SIZE;
1405 if (r > increment)
1406 r = increment;
1407 t = s;
1408 if (r < t)
1409 t = r;
1410 memqcpy (edit,
1411 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1412 EDIT_BUF_SIZE - t,
1413 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1415 if (r >= s) {
1416 if (t) {
1417 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1418 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1420 memqcpy (edit,
1421 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1422 EDIT_BUF_SIZE - r,
1423 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1424 EDIT_BUF_SIZE - (r - s), r - s);
1426 increment -= r;
1427 edit->curs1 -= r;
1428 edit->curs2 += r;
1429 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1430 if (p)
1431 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1432 else
1433 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1434 g_malloc0 (EDIT_BUF_SIZE);
1435 } else {
1436 g_free (p);
1439 return edit_get_byte (edit, edit->curs1);
1442 #endif /* ! FAST_MOVE_CURSOR */
1444 /* moves the cursor right or left: increment positive or negative respectively */
1445 void edit_cursor_move (WEdit * edit, long increment)
1447 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1448 int c;
1449 #ifdef FAST_MOVE_CURSOR
1450 if (increment < -256) {
1451 edit->force |= REDRAW_PAGE;
1452 edit_move_backward_lots (edit, -increment);
1453 return;
1455 #endif /* ! FAST_MOVE_CURSOR */
1457 if (increment < 0) {
1458 for (; increment < 0; increment++) {
1459 if (!edit->curs1)
1460 return;
1462 edit_push_action (edit, CURS_RIGHT);
1464 c = edit_get_byte (edit, edit->curs1 - 1);
1465 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1466 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1467 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1468 edit->curs2++;
1469 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1470 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1471 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1472 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1474 edit->curs1--;
1475 if (c == '\n') {
1476 edit->curs_line--;
1477 edit->force |= REDRAW_LINE_BELOW;
1481 } else if (increment > 0) {
1482 for (; increment > 0; increment--) {
1483 if (!edit->curs2)
1484 return;
1486 edit_push_action (edit, CURS_LEFT);
1488 c = edit_get_byte (edit, edit->curs1);
1489 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1490 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc0 (EDIT_BUF_SIZE);
1491 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1492 edit->curs1++;
1493 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1494 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1495 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1496 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1498 edit->curs2--;
1499 if (c == '\n') {
1500 edit->curs_line++;
1501 edit->force |= REDRAW_LINE_ABOVE;
1507 /* These functions return positions relative to lines */
1509 /* returns index of last char on line + 1 */
1510 long edit_eol (WEdit * edit, long current)
1512 if (current < edit->last_byte) {
1513 for (;; current++)
1514 if (edit_get_byte (edit, current) == '\n')
1515 break;
1516 } else
1517 return edit->last_byte;
1518 return current;
1521 /* returns index of first char on line */
1522 long edit_bol (WEdit * edit, long current)
1524 if (current > 0) {
1525 for (;; current--)
1526 if (edit_get_byte (edit, current - 1) == '\n')
1527 break;
1528 } else
1529 return 0;
1530 return current;
1534 long edit_count_lines (WEdit * edit, long current, long upto)
1536 long lines = 0;
1537 if (upto > edit->last_byte)
1538 upto = edit->last_byte;
1539 if (current < 0)
1540 current = 0;
1541 while (current < upto)
1542 if (edit_get_byte (edit, current++) == '\n')
1543 lines++;
1544 return lines;
1548 /* If lines is zero this returns the count of lines from current to upto. */
1549 /* If upto is zero returns index of lines forward current. */
1550 long edit_move_forward (WEdit * edit, long current, long lines, long upto)
1552 if (upto) {
1553 return edit_count_lines (edit, current, upto);
1554 } else {
1555 long next;
1556 if (lines < 0)
1557 lines = 0;
1558 while (lines--) {
1559 next = edit_eol (edit, current) + 1;
1560 if (next > edit->last_byte)
1561 break;
1562 else
1563 current = next;
1565 return current;
1570 /* Returns offset of 'lines' lines up from current */
1571 long edit_move_backward (WEdit * edit, long current, long lines)
1573 if (lines < 0)
1574 lines = 0;
1575 current = edit_bol (edit, current);
1576 while((lines--) && current != 0)
1577 current = edit_bol (edit, current - 1);
1578 return current;
1581 /* If cols is zero this returns the count of columns from current to upto. */
1582 /* If upto is zero returns index of cols across from current. */
1583 long
1584 edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1586 long p, q;
1587 int col;
1589 if (upto) {
1590 q = upto;
1591 cols = -10;
1592 } else
1593 q = edit->last_byte + 2;
1595 for (col = 0, p = current; p < q; p++) {
1596 int c, orig_c;
1597 int utf_ch = 0;
1598 #ifdef HAVE_CHARSET
1599 int cw = 1;
1600 #endif
1601 if (cols != -10) {
1602 if (col == cols)
1603 return p;
1604 if (col > cols)
1605 return p - 1;
1607 orig_c = c = edit_get_byte (edit, p);
1608 #ifdef HAVE_CHARSET
1609 if (edit->utf8) {
1610 utf_ch = edit_get_utf (edit, p, &cw);
1611 if (utf8_display) {
1612 if (cw > 1)
1613 col -= cw - 1;
1614 if (g_unichar_iswide (utf_ch))
1615 col++;
1616 } else if (cw > 1 && g_unichar_isprint (utf_ch))
1617 col -= cw - 1;
1619 #endif
1620 c = convert_to_display_c (c);
1621 if (c == '\t')
1622 col += TAB_SIZE - col % TAB_SIZE;
1623 else if (c == '\n') {
1624 if (upto)
1625 return col;
1626 else
1627 return p;
1628 } else if ((c < 32 || c == 127) && (orig_c == c || (!utf8_display && !edit->utf8)))
1629 /* '\r' is shown as ^M, so we must advance 2 characters */
1630 /* Caret notation for control characters */
1631 col += 2;
1632 else
1633 col++;
1635 return col;
1638 /* returns the current column position of the cursor */
1639 int edit_get_col (WEdit * edit)
1641 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1645 /* Scrolling functions */
1647 void edit_update_curs_row (WEdit * edit)
1649 edit->curs_row = edit->curs_line - edit->start_line;
1652 void edit_update_curs_col (WEdit * edit)
1654 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1658 edit_get_curs_col (const WEdit *edit)
1660 return edit->curs_col;
1663 /*moves the display start position up by i lines */
1664 void edit_scroll_upward (WEdit * edit, unsigned long i)
1666 unsigned long lines_above = edit->start_line;
1667 if (i > lines_above)
1668 i = lines_above;
1669 if (i) {
1670 edit->start_line -= i;
1671 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1672 edit->force |= REDRAW_PAGE;
1673 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1675 edit_update_curs_row (edit);
1679 /* returns 1 if could scroll, 0 otherwise */
1680 void edit_scroll_downward (WEdit * edit, int i)
1682 int lines_below;
1683 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1684 if (lines_below > 0) {
1685 if (i > lines_below)
1686 i = lines_below;
1687 edit->start_line += i;
1688 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1689 edit->force |= REDRAW_PAGE;
1690 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1692 edit_update_curs_row (edit);
1695 void edit_scroll_right (WEdit * edit, int i)
1697 edit->force |= REDRAW_PAGE;
1698 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1699 edit->start_col -= i;
1702 void edit_scroll_left (WEdit * edit, int i)
1704 if (edit->start_col) {
1705 edit->start_col += i;
1706 if (edit->start_col > 0)
1707 edit->start_col = 0;
1708 edit->force |= REDRAW_PAGE;
1709 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1713 /* high level cursor movement commands */
1715 static int is_in_indent (WEdit *edit)
1717 long p = edit_bol (edit, edit->curs1);
1718 while (p < edit->curs1)
1719 if (!strchr (" \t", edit_get_byte (edit, p++)))
1720 return 0;
1721 return 1;
1724 static int left_of_four_spaces (WEdit *edit);
1726 void
1727 edit_move_to_prev_col (WEdit * edit, long p)
1729 int prev = edit->prev_col;
1730 int over = edit->over_col;
1731 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1733 if (option_cursor_beyond_eol) {
1734 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1736 if (line_len < prev + edit->over_col) {
1737 edit->over_col = prev + over - line_len;
1738 edit->prev_col = line_len;
1739 edit->curs_col = line_len;
1740 } else {
1741 edit->curs_col = prev + over;
1742 edit->prev_col = edit->curs_col;
1743 edit->over_col = 0;
1745 } else {
1746 edit->over_col = 0;
1747 if (is_in_indent (edit) && option_fake_half_tabs) {
1748 edit_update_curs_col (edit);
1749 if (space_width)
1750 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1751 int q = edit->curs_col;
1752 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1753 p = edit_bol (edit, edit->curs1);
1754 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1755 if (!left_of_four_spaces (edit))
1756 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1762 static int
1763 is_blank (WEdit *edit, long offset)
1765 long s, f;
1766 int c;
1767 s = edit_bol (edit, offset);
1768 f = edit_eol (edit, offset) - 1;
1769 while (s <= f) {
1770 c = edit_get_byte (edit, s++);
1771 if (!isspace (c))
1772 return 0;
1774 return 1;
1778 /* returns the offset of line i */
1779 static long
1780 edit_find_line (WEdit *edit, int line)
1782 int i, j = 0;
1783 int m = 2000000000;
1784 if (!edit->caches_valid) {
1785 for (i = 0; i < N_LINE_CACHES; i++)
1786 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1787 /* three offsets that we *know* are line 0 at 0 and these two: */
1788 edit->line_numbers[1] = edit->curs_line;
1789 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1790 edit->line_numbers[2] = edit->total_lines;
1791 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1792 edit->caches_valid = 1;
1794 if (line >= edit->total_lines)
1795 return edit->line_offsets[2];
1796 if (line <= 0)
1797 return 0;
1798 /* find the closest known point */
1799 for (i = 0; i < N_LINE_CACHES; i++) {
1800 int n;
1801 n = abs (edit->line_numbers[i] - line);
1802 if (n < m) {
1803 m = n;
1804 j = i;
1807 if (m == 0)
1808 return edit->line_offsets[j]; /* know the offset exactly */
1809 if (m == 1 && j >= 3)
1810 i = j; /* one line different - caller might be looping, so stay in this cache */
1811 else
1812 i = 3 + (rand () % (N_LINE_CACHES - 3));
1813 if (line > edit->line_numbers[j])
1814 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1815 else
1816 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1817 edit->line_numbers[i] = line;
1818 return edit->line_offsets[i];
1821 int line_is_blank (WEdit * edit, long line)
1823 return is_blank (edit, edit_find_line (edit, line));
1826 /* moves up until a blank line is reached, or until just
1827 before a non-blank line is reached */
1828 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1830 int i = 0;
1831 if (edit->curs_line > 1) {
1832 if (line_is_blank (edit, edit->curs_line)) {
1833 if (line_is_blank (edit, edit->curs_line - 1)) {
1834 for (i = edit->curs_line - 1; i; i--)
1835 if (!line_is_blank (edit, i)) {
1836 i++;
1837 break;
1839 } else {
1840 for (i = edit->curs_line - 1; i; i--)
1841 if (line_is_blank (edit, i))
1842 break;
1844 } else {
1845 for (i = edit->curs_line - 1; i; i--)
1846 if (line_is_blank (edit, i))
1847 break;
1850 edit_move_up (edit, edit->curs_line - i, scroll);
1853 /* moves down until a blank line is reached, or until just
1854 before a non-blank line is reached */
1855 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1857 int i;
1858 if (edit->curs_line >= edit->total_lines - 1) {
1859 i = edit->total_lines;
1860 } else {
1861 if (line_is_blank (edit, edit->curs_line)) {
1862 if (line_is_blank (edit, edit->curs_line + 1)) {
1863 for (i = edit->curs_line + 1; i; i++)
1864 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1865 i--;
1866 break;
1868 } else {
1869 for (i = edit->curs_line + 1; i; i++)
1870 if (line_is_blank (edit, i) || i >= edit->total_lines)
1871 break;
1873 } else {
1874 for (i = edit->curs_line + 1; i; i++)
1875 if (line_is_blank (edit, i) || i >= edit->total_lines)
1876 break;
1879 edit_move_down (edit, i - edit->curs_line, scroll);
1882 static void edit_begin_page (WEdit *edit)
1884 edit_update_curs_row (edit);
1885 edit_move_up (edit, edit->curs_row, 0);
1888 static void edit_end_page (WEdit *edit)
1890 edit_update_curs_row (edit);
1891 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1895 /* goto beginning of text */
1896 static void edit_move_to_top (WEdit * edit)
1898 if (edit->curs_line) {
1899 edit_cursor_move (edit, -edit->curs1);
1900 edit_move_to_prev_col (edit, 0);
1901 edit->force |= REDRAW_PAGE;
1902 edit->search_start = 0;
1903 edit_update_curs_row(edit);
1908 /* goto end of text */
1909 static void edit_move_to_bottom (WEdit * edit)
1911 if (edit->curs_line < edit->total_lines) {
1912 edit_move_down (edit, edit->total_lines - edit->curs_row, 0);
1913 edit->start_display = edit->last_byte;
1914 edit->start_line = edit->total_lines;
1915 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1916 edit->force |= REDRAW_PAGE;
1920 /* goto beginning of line */
1921 static void edit_cursor_to_bol (WEdit * edit)
1923 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1924 edit->search_start = edit->curs1;
1925 edit->prev_col = edit_get_col (edit);
1926 edit->over_col = 0;
1929 /* goto end of line */
1930 static void edit_cursor_to_eol (WEdit * edit)
1932 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1933 edit->search_start = edit->curs1;
1934 edit->prev_col = edit_get_col (edit);
1935 edit->over_col = 0;
1938 /* move cursor to line 'line' */
1939 void edit_move_to_line (WEdit * e, long line)
1941 if(line < e->curs_line)
1942 edit_move_up (e, e->curs_line - line, 0);
1943 else
1944 edit_move_down (e, line - e->curs_line, 0);
1945 edit_scroll_screen_over_cursor (e);
1948 /* scroll window so that first visible line is 'line' */
1949 void edit_move_display (WEdit * e, long line)
1951 if(line < e->start_line)
1952 edit_scroll_upward (e, e->start_line - line);
1953 else
1954 edit_scroll_downward (e, line - e->start_line);
1957 /* save markers onto undo stack */
1958 void edit_push_markers (WEdit * edit)
1960 edit_push_action (edit, MARK_1 + edit->mark1);
1961 edit_push_action (edit, MARK_2 + edit->mark2);
1964 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
1966 edit->mark1 = m1;
1967 edit->mark2 = m2;
1968 edit->column1 = c1;
1969 edit->column2 = c2;
1973 /* highlight marker toggle */
1974 void edit_mark_cmd (WEdit * edit, int unmark)
1976 edit_push_markers (edit);
1977 if (unmark) {
1978 edit_set_markers (edit, 0, 0, 0, 0);
1979 edit->force |= REDRAW_PAGE;
1980 } else {
1981 if (edit->mark2 >= 0) {
1982 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
1983 edit->force |= REDRAW_PAGE;
1984 } else
1985 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
1989 static unsigned long
1990 my_type_of (int c)
1992 int x, r = 0;
1993 const char *p, *q;
1994 const char option_chars_move_whole_word[] =
1995 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1997 if (!c)
1998 return 0;
1999 if (c == '!') {
2000 if (*option_chars_move_whole_word == '!')
2001 return 2;
2002 return 0x80000000UL;
2004 if (g_ascii_isupper ((gchar) c))
2005 c = 'A';
2006 else if (g_ascii_islower ((gchar) c))
2007 c = 'a';
2008 else if (g_ascii_isalpha (c))
2009 c = 'a';
2010 else if (isdigit (c))
2011 c = '0';
2012 else if (isspace (c))
2013 c = ' ';
2014 q = strchr (option_chars_move_whole_word, c);
2015 if (!q)
2016 return 0xFFFFFFFFUL;
2017 do {
2018 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2019 if (*p == '!')
2020 x <<= 1;
2021 r |= x;
2022 } while ((q = strchr (q + 1, c)));
2023 return r;
2026 static void
2027 edit_left_word_move (WEdit *edit, int s)
2029 for (;;) {
2030 int c1, c2;
2031 if (column_highlighting
2032 && edit->mark1 != edit->mark2
2033 && edit->over_col == 0
2034 && edit->curs1 == edit_bol(edit, edit->curs1))
2035 break;
2036 edit_cursor_move (edit, -1);
2037 if (!edit->curs1)
2038 break;
2039 c1 = edit_get_byte (edit, edit->curs1 - 1);
2040 c2 = edit_get_byte (edit, edit->curs1);
2041 if (!(my_type_of (c1) & my_type_of (c2)))
2042 break;
2043 if (isspace (c1) && !isspace (c2))
2044 break;
2045 if (s)
2046 if (!isspace (c1) && isspace (c2))
2047 break;
2051 static void edit_left_word_move_cmd (WEdit * edit)
2053 edit_left_word_move (edit, 0);
2054 edit->force |= REDRAW_PAGE;
2057 static void
2058 edit_right_word_move (WEdit *edit, int s)
2060 for (;;) {
2061 int c1, c2;
2062 if (column_highlighting
2063 && edit->mark1 != edit->mark2
2064 && edit->over_col == 0
2065 && edit->curs1 == edit_eol(edit, edit->curs1)
2067 break;
2068 edit_cursor_move (edit, 1);
2069 if (edit->curs1 >= edit->last_byte)
2070 break;
2071 c1 = edit_get_byte (edit, edit->curs1 - 1);
2072 c2 = edit_get_byte (edit, edit->curs1);
2073 if (!(my_type_of (c1) & my_type_of (c2)))
2074 break;
2075 if (isspace (c1) && !isspace (c2))
2076 break;
2077 if (s)
2078 if (!isspace (c1) && isspace (c2))
2079 break;
2083 static void edit_right_word_move_cmd (WEdit * edit)
2085 edit_right_word_move (edit, 0);
2086 edit->force |= REDRAW_PAGE;
2089 static void edit_right_char_move_cmd (WEdit * edit)
2091 int cw = 1;
2092 int c = 0;
2093 if ( edit->utf8 ) {
2094 c = edit_get_utf (edit, edit->curs1, &cw);
2095 if ( cw < 1 )
2096 cw = 1;
2097 } else {
2098 c = edit_get_byte (edit, edit->curs1);
2100 if (option_cursor_beyond_eol && c == '\n') {
2101 edit->over_col++;
2102 } else {
2103 edit_cursor_move (edit, cw);
2107 static void edit_left_char_move_cmd (WEdit * edit)
2109 int cw = 1;
2110 if (column_highlighting
2111 && edit->mark1 != edit->mark2
2112 && edit->over_col == 0
2113 && edit->curs1 == edit_bol(edit, edit->curs1))
2114 return;
2115 if ( edit->utf8 ) {
2116 edit_get_prev_utf (edit, edit->curs1, &cw);
2117 if ( cw < 1 )
2118 cw = 1;
2120 if (option_cursor_beyond_eol && edit->over_col > 0) {
2121 edit->over_col--;
2122 } else {
2123 edit_cursor_move (edit, -cw);
2127 /** Up or down cursor moving.
2128 direction = TRUE - move up
2129 = FALSE - move down
2131 static void
2132 edit_move_updown (WEdit * edit, unsigned long i, int scroll, gboolean direction)
2134 unsigned long p;
2135 unsigned long l = (direction)
2136 ? edit->curs_line
2137 : edit->total_lines - edit->curs_line;
2139 if (i > l)
2140 i = l;
2142 if (i == 0)
2143 return;
2145 if (i > 1)
2146 edit->force |= REDRAW_PAGE;
2147 if (scroll) {
2148 if (direction)
2149 edit_scroll_upward (edit, i);
2150 else
2151 edit_scroll_downward (edit, i);
2153 p = edit_bol (edit, edit->curs1);
2155 p = (direction)
2156 ? edit_move_backward (edit, p, i)
2157 : edit_move_forward (edit, p, i, 0);
2159 edit_cursor_move (edit, p - edit->curs1);
2161 edit_move_to_prev_col (edit, p);
2163 /* search start of current multibyte char (like CJK) */
2164 if (edit->curs1 + 1 < edit->last_byte) {
2165 edit_right_char_move_cmd (edit);
2166 edit_left_char_move_cmd (edit);
2169 edit->search_start = edit->curs1;
2170 edit->found_len = 0;
2173 static void edit_right_delete_word (WEdit * edit)
2175 int c1, c2;
2176 for (;;) {
2177 if (edit->curs1 >= edit->last_byte)
2178 break;
2179 c1 = edit_delete (edit, 1);
2180 c2 = edit_get_byte (edit, edit->curs1);
2181 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2182 break;
2183 if (!(my_type_of (c1) & my_type_of (c2)))
2184 break;
2188 static void edit_left_delete_word (WEdit * edit)
2190 int c1, c2;
2191 for (;;) {
2192 if (edit->curs1 <= 0)
2193 break;
2194 c1 = edit_backspace (edit, 1);
2195 c2 = edit_get_byte (edit, edit->curs1 - 1);
2196 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2197 break;
2198 if (!(my_type_of (c1) & my_type_of (c2)))
2199 break;
2204 the start column position is not recorded, and hence does not
2205 undo as it happed. But who would notice.
2207 static void
2208 edit_do_undo (WEdit * edit)
2210 long ac;
2211 long count = 0;
2213 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2214 edit->over_col = 0;
2215 while ((ac = pop_action (edit)) < KEY_PRESS) {
2216 switch ((int) ac) {
2217 case STACK_BOTTOM:
2218 goto done_undo;
2219 case CURS_RIGHT:
2220 edit_cursor_move (edit, 1);
2221 break;
2222 case CURS_LEFT:
2223 edit_cursor_move (edit, -1);
2224 break;
2225 case BACKSPACE:
2226 edit_backspace (edit, 1);
2227 break;
2228 case DELCHAR:
2229 edit_delete (edit, 1);
2230 break;
2231 case COLUMN_ON:
2232 column_highlighting = 1;
2233 break;
2234 case COLUMN_OFF:
2235 column_highlighting = 0;
2236 break;
2238 if (ac >= 256 && ac < 512)
2239 edit_insert_ahead (edit, ac - 256);
2240 if (ac >= 0 && ac < 256)
2241 edit_insert (edit, ac);
2243 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2244 edit->mark1 = ac - MARK_1;
2245 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2246 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2247 edit->mark2 = ac - MARK_2;
2248 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2250 if (count++)
2251 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2254 if (edit->start_display > ac - KEY_PRESS) {
2255 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2256 edit->force |= REDRAW_PAGE;
2257 } else if (edit->start_display < ac - KEY_PRESS) {
2258 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2259 edit->force |= REDRAW_PAGE;
2261 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2262 edit_update_curs_row (edit);
2264 done_undo:;
2265 edit->stack_disable = 0;
2268 static void edit_delete_to_line_end (WEdit * edit)
2270 while (edit_get_byte (edit, edit->curs1) != '\n') {
2271 if (!edit->curs2)
2272 break;
2273 edit_delete (edit, 1);
2277 static void edit_delete_to_line_begin (WEdit * edit)
2279 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2280 if (!edit->curs1)
2281 break;
2282 edit_backspace (edit, 1);
2286 void
2287 edit_delete_line (WEdit *edit)
2290 * Delete right part of the line.
2291 * Note that edit_get_byte() returns '\n' when byte position is
2292 * beyond EOF.
2294 while (edit_get_byte (edit, edit->curs1) != '\n') {
2295 (void) edit_delete (edit, 1);
2299 * Delete '\n' char.
2300 * Note that edit_delete() will not corrupt anything if called while
2301 * cursor position is EOF.
2303 (void) edit_delete (edit, 1);
2306 * Delete left part of the line.
2307 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2309 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2310 (void) edit_backspace (edit, 1);
2314 void insert_spaces_tab (WEdit * edit, int half)
2316 int i;
2317 edit_update_curs_col (edit);
2318 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2319 while (i > 0) {
2320 edit_insert (edit, ' ');
2321 i -= space_width;
2325 static int is_aligned_on_a_tab (WEdit * edit)
2327 edit_update_curs_col (edit);
2328 return !((edit->curs_col % (TAB_SIZE * space_width))
2329 && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width));
2332 static int right_of_four_spaces (WEdit *edit)
2334 int i, ch = 0;
2335 for (i = 1; i <= HALF_TAB_SIZE; i++)
2336 ch |= edit_get_byte (edit, edit->curs1 - i);
2337 if (ch == ' ')
2338 return is_aligned_on_a_tab (edit);
2339 return 0;
2342 static int left_of_four_spaces (WEdit *edit)
2344 int i, ch = 0;
2345 for (i = 0; i < HALF_TAB_SIZE; i++)
2346 ch |= edit_get_byte (edit, edit->curs1 + i);
2347 if (ch == ' ')
2348 return is_aligned_on_a_tab (edit);
2349 return 0;
2352 int edit_indent_width (WEdit * edit, long p)
2354 long q = p;
2355 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2356 q++;
2357 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2360 void edit_insert_indent (WEdit * edit, int indent)
2362 if (!option_fill_tabs_with_spaces) {
2363 while (indent >= TAB_SIZE) {
2364 edit_insert (edit, '\t');
2365 indent -= TAB_SIZE;
2368 while (indent-- > 0)
2369 edit_insert (edit, ' ');
2372 static void
2373 edit_auto_indent (WEdit * edit)
2375 long p;
2376 char c;
2377 p = edit->curs1;
2378 /* use the previous line as a template */
2379 p = edit_move_backward (edit, p, 1);
2380 /* copy the leading whitespace of the line */
2381 for (;;) { /* no range check - the line _is_ \n-terminated */
2382 c = edit_get_byte (edit, p++);
2383 if (c != ' ' && c != '\t')
2384 break;
2385 edit_insert (edit, c);
2389 static inline void
2390 edit_double_newline (WEdit * edit)
2392 edit_insert (edit, '\n');
2393 if (edit_get_byte (edit, edit->curs1) == '\n')
2394 return;
2395 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2396 return;
2397 edit->force |= REDRAW_PAGE;
2398 edit_insert (edit, '\n');
2401 static inline void
2402 edit_tab_cmd (WEdit * edit)
2404 int i;
2406 if (option_fake_half_tabs) {
2407 if (is_in_indent (edit)) {
2408 /*insert a half tab (usually four spaces) unless there is a
2409 half tab already behind, then delete it and insert a
2410 full tab. */
2411 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2412 for (i = 1; i <= HALF_TAB_SIZE; i++)
2413 edit_backspace (edit, 1);
2414 edit_insert (edit, '\t');
2415 } else {
2416 insert_spaces_tab (edit, 1);
2418 return;
2421 if (option_fill_tabs_with_spaces) {
2422 insert_spaces_tab (edit, 0);
2423 } else {
2424 edit_insert (edit, '\t');
2428 static void check_and_wrap_line (WEdit * edit)
2430 int curs, c;
2431 if (!option_typewriter_wrap)
2432 return;
2433 edit_update_curs_col (edit);
2434 if (edit->curs_col < option_word_wrap_line_length)
2435 return;
2436 curs = edit->curs1;
2437 for (;;) {
2438 curs--;
2439 c = edit_get_byte (edit, curs);
2440 if (c == '\n' || curs <= 0) {
2441 edit_insert (edit, '\n');
2442 return;
2444 if (c == ' ' || c == '\t') {
2445 int current = edit->curs1;
2446 edit_cursor_move (edit, curs - edit->curs1 + 1);
2447 edit_insert (edit, '\n');
2448 edit_cursor_move (edit, current - edit->curs1 + 1);
2449 return;
2454 static inline void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2456 void edit_push_key_press (WEdit * edit)
2458 edit_push_action (edit, KEY_PRESS + edit->start_display);
2459 if (edit->mark2 == -1)
2460 edit_push_action (edit, MARK_1 + edit->mark1);
2463 /* this find the matching bracket in either direction, and sets edit->bracket */
2464 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2466 const char * const b = "{}{[][()(", *p;
2467 int i = 1, a, inc = -1, c, d, n = 0;
2468 unsigned long j = 0;
2469 long q;
2470 edit_update_curs_row (edit);
2471 c = edit_get_byte (edit, edit->curs1);
2472 p = strchr (b, c);
2473 /* no limit */
2474 if (!furthest_bracket_search)
2475 furthest_bracket_search--;
2476 /* not on a bracket at all */
2477 if (!p)
2478 return -1;
2479 /* the matching bracket */
2480 d = p[1];
2481 /* going left or right? */
2482 if (strchr ("{[(", c))
2483 inc = 1;
2484 for (q = edit->curs1 + inc;; q += inc) {
2485 /* out of buffer? */
2486 if (q >= edit->last_byte || q < 0)
2487 break;
2488 a = edit_get_byte (edit, q);
2489 /* don't want to eat CPU */
2490 if (j++ > furthest_bracket_search)
2491 break;
2492 /* out of screen? */
2493 if (in_screen) {
2494 if (q < edit->start_display)
2495 break;
2496 /* count lines if searching downward */
2497 if (inc > 0 && a == '\n')
2498 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2499 break;
2501 /* count bracket depth */
2502 i += (a == c) - (a == d);
2503 /* return if bracket depth is zero */
2504 if (!i)
2505 return q;
2507 /* no match */
2508 return -1;
2511 static long last_bracket = -1;
2513 void edit_find_bracket (WEdit * edit)
2515 edit->bracket = edit_get_bracket (edit, 1, 10000);
2516 if (last_bracket != edit->bracket)
2517 edit->force |= REDRAW_PAGE;
2518 last_bracket = edit->bracket;
2521 static inline void
2522 edit_goto_matching_bracket (WEdit *edit)
2524 long q;
2526 q = edit_get_bracket (edit, 0, 0);
2527 if (q >= 0) {
2528 edit->bracket = edit->curs1;
2529 edit->force |= REDRAW_PAGE;
2530 edit_cursor_move (edit, q - edit->curs1);
2535 * This executes a command as though the user initiated it through a key
2536 * press. Callback with WIDGET_KEY as a message calls this after
2537 * translating the key press. This function can be used to pass any
2538 * command to the editor. Note that the screen wouldn't update
2539 * automatically. Either of command or char_for_insertion must be
2540 * passed as -1. Commands are executed, and char_for_insertion is
2541 * inserted at the cursor.
2543 void
2544 edit_execute_key_command (WEdit *edit, unsigned long command, int char_for_insertion)
2546 if (command == CK_Begin_Record_Macro) {
2547 edit->macro_i = 0;
2548 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2549 return;
2551 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2552 edit->force |= REDRAW_COMPLETELY;
2553 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2554 edit->macro_i = -1;
2555 return;
2557 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2558 edit->macro[edit->macro_i].command = command;
2559 edit->macro[edit->macro_i++].ch = char_for_insertion;
2561 /* record the beginning of a set of editing actions initiated by a key press */
2562 if (command != CK_Undo && command != CK_Ext_Mode)
2563 edit_push_key_press (edit);
2565 edit_execute_cmd (edit, command, char_for_insertion);
2566 if (column_highlighting)
2567 edit->force |= REDRAW_PAGE;
2570 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2573 This executes a command at a lower level than macro recording.
2574 It also does not push a key_press onto the undo stack. This means
2575 that if it is called many times, a single undo command will undo
2576 all of them. It also does not check for the Undo command.
2578 void
2579 edit_execute_cmd (WEdit *edit, unsigned long command, int char_for_insertion)
2581 edit->force |= REDRAW_LINE;
2583 /* The next key press will unhighlight the found string, so update
2584 * the whole page */
2585 if (edit->found_len || column_highlighting)
2586 edit->force |= REDRAW_PAGE;
2588 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2589 column_highlighting = 0;
2590 if (!edit->highlight
2591 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2592 edit_mark_cmd (edit, 1); /* clear */
2593 edit_mark_cmd (edit, 0); /* marking on */
2595 edit->highlight = 1;
2596 } else { /* any other command */
2597 if (edit->highlight)
2598 edit_mark_cmd (edit, 0); /* clear */
2599 edit->highlight = 0;
2602 /* first check for undo */
2603 if (command == CK_Undo) {
2604 edit_do_undo (edit);
2605 edit->found_len = 0;
2606 edit->prev_col = edit_get_col (edit);
2607 edit->search_start = edit->curs1;
2608 return;
2611 /* An ordinary key press */
2612 if (char_for_insertion >= 0) {
2613 if (edit->overwrite) {
2614 if (edit_get_byte (edit, edit->curs1) != '\n')
2615 edit_delete (edit, 0);
2617 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2618 edit_insert_over (edit);
2619 #ifdef HAVE_CHARSET
2620 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2621 unsigned char str[6 + 1];
2622 size_t i = 0;
2623 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2624 if ( res == 0 ) {
2625 str[0] = '.';
2626 str[1] = '\0';
2627 } else {
2628 str[res] = '\0';
2630 while ( str[i] != 0 && i<=6) {
2631 char_for_insertion = str[i];
2632 edit_insert (edit, char_for_insertion);
2633 i++;
2635 } else
2636 #endif
2637 edit_insert (edit, char_for_insertion);
2639 if (option_auto_para_formatting) {
2640 format_paragraph (edit, 0);
2641 edit->force |= REDRAW_PAGE;
2642 } else
2643 check_and_wrap_line (edit);
2644 edit->found_len = 0;
2645 edit->prev_col = edit_get_col (edit);
2646 edit->search_start = edit->curs1;
2647 edit_find_bracket (edit);
2648 return;
2651 switch (command) {
2652 case CK_Begin_Page:
2653 case CK_End_Page:
2654 case CK_Begin_Page_Highlight:
2655 case CK_End_Page_Highlight:
2656 case CK_Word_Left:
2657 case CK_Word_Right:
2658 case CK_Up:
2659 case CK_Down:
2660 case CK_Left:
2661 case CK_Right:
2662 if ( edit->mark2 >= 0 ) {
2663 if ( !option_persistent_selections ) {
2664 if (column_highlighting)
2665 edit_push_action (edit, COLUMN_ON);
2666 column_highlighting = 0;
2667 edit_mark_cmd (edit, 1);
2672 switch (command) {
2673 case CK_Begin_Page:
2674 case CK_End_Page:
2675 case CK_Begin_Page_Highlight:
2676 case CK_End_Page_Highlight:
2677 case CK_Word_Left:
2678 case CK_Word_Right:
2679 case CK_Up:
2680 case CK_Down:
2681 case CK_Word_Left_Highlight:
2682 case CK_Word_Right_Highlight:
2683 case CK_Up_Highlight:
2684 case CK_Down_Highlight:
2685 case CK_Up_Alt_Highlight:
2686 case CK_Down_Alt_Highlight:
2687 if (edit->mark2 == -1)
2688 break; /*marking is following the cursor: may need to highlight a whole line */
2689 case CK_Left:
2690 case CK_Right:
2691 case CK_Left_Highlight:
2692 case CK_Right_Highlight:
2693 edit->force |= REDRAW_CHAR_ONLY;
2696 /* basic cursor key commands */
2697 switch (command) {
2698 case CK_BackSpace:
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;
2706 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2707 edit->over_col--;
2708 break;
2710 if (option_backspace_through_tabs && is_in_indent (edit)) {
2711 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2712 && edit->curs1 > 0)
2713 edit_backspace (edit, 1);
2714 break;
2715 } else {
2716 if (option_fake_half_tabs) {
2717 int i;
2718 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2719 for (i = 0; i < HALF_TAB_SIZE; i++)
2720 edit_backspace (edit, 1);
2721 break;
2725 edit_backspace (edit, 0);
2726 break;
2727 case CK_Delete:
2728 /* if non persistent selection and text selected */
2729 if ( !option_persistent_selections ) {
2730 if ( edit->mark1 != edit->mark2 ) {
2731 edit_block_delete_cmd (edit);
2732 break;
2736 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2737 edit_insert_over (edit);
2739 if (option_fake_half_tabs) {
2740 int i;
2741 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2742 for (i = 1; i <= HALF_TAB_SIZE; i++)
2743 edit_delete (edit, 1);
2744 break;
2747 edit_delete (edit, 0);
2748 break;
2749 case CK_Delete_Word_Left:
2750 edit->over_col = 0;
2751 edit_left_delete_word (edit);
2752 break;
2753 case CK_Delete_Word_Right:
2754 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2755 edit_insert_over (edit);
2757 edit_right_delete_word (edit);
2758 break;
2759 case CK_Delete_Line:
2760 edit_delete_line (edit);
2761 break;
2762 case CK_Delete_To_Line_End:
2763 edit_delete_to_line_end (edit);
2764 break;
2765 case CK_Delete_To_Line_Begin:
2766 edit_delete_to_line_begin (edit);
2767 break;
2768 case CK_Enter:
2769 edit->over_col = 0;
2770 if (option_auto_para_formatting) {
2771 edit_double_newline (edit);
2772 if (option_return_does_auto_indent)
2773 edit_auto_indent (edit);
2774 format_paragraph (edit, 0);
2775 } else {
2776 edit_insert (edit, '\n');
2777 if (option_return_does_auto_indent) {
2778 edit_auto_indent (edit);
2781 break;
2782 case CK_Return:
2783 edit_insert (edit, '\n');
2784 break;
2786 case CK_Page_Up_Alt_Highlight:
2787 column_highlighting = 1;
2788 case CK_Page_Up:
2789 case CK_Page_Up_Highlight:
2790 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2791 break;
2792 case CK_Page_Down_Alt_Highlight:
2793 column_highlighting = 1;
2794 case CK_Page_Down:
2795 case CK_Page_Down_Highlight:
2796 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2797 break;
2798 case CK_Left_Alt_Highlight:
2799 column_highlighting = 1;
2800 case CK_Left:
2801 case CK_Left_Highlight:
2802 if (option_fake_half_tabs) {
2803 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2804 if ( option_cursor_beyond_eol && edit->over_col > 0)
2805 edit->over_col--;
2806 else
2807 edit_cursor_move (edit, -HALF_TAB_SIZE);
2808 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2809 break;
2812 edit_left_char_move_cmd (edit);
2813 break;
2814 case CK_Right_Alt_Highlight:
2815 column_highlighting = 1;
2816 case CK_Right:
2817 case CK_Right_Highlight:
2818 if (option_fake_half_tabs) {
2819 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2820 edit_cursor_move (edit, HALF_TAB_SIZE);
2821 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2822 break;
2825 edit_right_char_move_cmd (edit);
2826 break;
2827 case CK_Begin_Page:
2828 case CK_Begin_Page_Highlight:
2829 edit_begin_page (edit);
2830 break;
2831 case CK_End_Page:
2832 case CK_End_Page_Highlight:
2833 edit_end_page (edit);
2834 break;
2835 case CK_Word_Left:
2836 case CK_Word_Left_Highlight:
2837 edit->over_col = 0;
2838 edit_left_word_move_cmd (edit);
2839 break;
2840 case CK_Word_Right:
2841 case CK_Word_Right_Highlight:
2842 edit->over_col = 0;
2843 edit_right_word_move_cmd (edit);
2844 break;
2845 case CK_Up_Alt_Highlight:
2846 column_highlighting = 1;
2847 case CK_Up:
2848 case CK_Up_Highlight:
2849 edit_move_up (edit, 1, 0);
2850 break;
2851 case CK_Down_Alt_Highlight:
2852 column_highlighting = 1;
2853 case CK_Down:
2854 case CK_Down_Highlight:
2855 edit_move_down (edit, 1, 0);
2856 break;
2857 case CK_Paragraph_Up_Alt_Highlight:
2858 column_highlighting = 1;
2859 case CK_Paragraph_Up:
2860 case CK_Paragraph_Up_Highlight:
2861 edit_move_up_paragraph (edit, 0);
2862 break;
2863 case CK_Paragraph_Down_Alt_Highlight:
2864 column_highlighting = 1;
2865 case CK_Paragraph_Down:
2866 case CK_Paragraph_Down_Highlight:
2867 edit_move_down_paragraph (edit, 0);
2868 break;
2869 case CK_Scroll_Up_Alt_Highlight:
2870 column_highlighting = 1;
2871 case CK_Scroll_Up:
2872 case CK_Scroll_Up_Highlight:
2873 edit_move_up (edit, 1, 1);
2874 break;
2875 case CK_Scroll_Down_Alt_Highlight:
2876 column_highlighting = 1;
2877 case CK_Scroll_Down:
2878 case CK_Scroll_Down_Highlight:
2879 edit_move_down (edit, 1, 1);
2880 break;
2881 case CK_Home:
2882 case CK_Home_Highlight:
2883 edit_cursor_to_bol (edit);
2884 break;
2885 case CK_End:
2886 case CK_End_Highlight:
2887 edit_cursor_to_eol (edit);
2888 break;
2889 case CK_Tab:
2890 /* if text marked shift block */
2891 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2892 if (edit->mark2 < 0)
2893 edit_mark_cmd (edit, 0);
2894 edit_move_block_to_right (edit);
2895 } else {
2896 if ( option_cursor_beyond_eol )
2897 edit_insert_over (edit);
2898 edit_tab_cmd (edit);
2899 if (option_auto_para_formatting) {
2900 format_paragraph (edit, 0);
2901 edit->force |= REDRAW_PAGE;
2902 } else {
2903 check_and_wrap_line (edit);
2906 break;
2908 case CK_Toggle_Insert:
2909 edit->overwrite = (edit->overwrite == 0);
2910 break;
2912 case CK_Mark:
2913 if (edit->mark2 >= 0) {
2914 if (column_highlighting)
2915 edit_push_action (edit, COLUMN_ON);
2916 column_highlighting = 0;
2918 edit_mark_cmd (edit, 0);
2919 break;
2920 case CK_Column_Mark:
2921 if (!column_highlighting)
2922 edit_push_action (edit, COLUMN_OFF);
2923 column_highlighting = 1;
2924 edit_mark_cmd (edit, 0);
2925 break;
2926 case CK_Mark_All:
2927 edit_set_markers (edit, 0, edit->last_byte, 0, 0);
2928 edit->force |= REDRAW_PAGE;
2929 break;
2930 case CK_Unmark:
2931 if (column_highlighting)
2932 edit_push_action (edit, COLUMN_ON);
2933 column_highlighting = 0;
2934 edit_mark_cmd (edit, 1);
2935 break;
2937 case CK_Toggle_Line_State:
2938 option_line_state = !option_line_state;
2939 if ( option_line_state ) {
2940 option_line_state_width = LINE_STATE_WIDTH;
2941 } else {
2942 option_line_state_width = 0;
2944 edit->force |= REDRAW_PAGE;
2945 break;
2947 case CK_Toggle_Show_Margin:
2948 show_right_margin = !show_right_margin;
2949 edit->force |= REDRAW_PAGE;
2950 break;
2952 case CK_Toggle_Bookmark:
2953 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2954 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2955 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2956 else
2957 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2958 break;
2959 case CK_Flush_Bookmarks:
2960 book_mark_flush (edit, BOOK_MARK_COLOR);
2961 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2962 edit->force |= REDRAW_PAGE;
2963 break;
2964 case CK_Next_Bookmark:
2965 if (edit->book_mark) {
2966 struct _book_mark *p;
2967 p = (struct _book_mark *) book_mark_find (edit,
2968 edit->curs_line);
2969 if (p->next) {
2970 p = p->next;
2971 if (p->line >= edit->start_line + edit->num_widget_lines
2972 || p->line < edit->start_line)
2973 edit_move_display (edit,
2974 p->line -
2975 edit->num_widget_lines / 2);
2976 edit_move_to_line (edit, p->line);
2979 break;
2980 case CK_Prev_Bookmark:
2981 if (edit->book_mark) {
2982 struct _book_mark *p;
2983 p = (struct _book_mark *) book_mark_find (edit,
2984 edit->curs_line);
2985 while (p->line == edit->curs_line)
2986 if (p->prev)
2987 p = p->prev;
2988 if (p->line >= 0) {
2989 if (p->line >= edit->start_line + edit->num_widget_lines
2990 || p->line < edit->start_line)
2991 edit_move_display (edit,
2992 p->line -
2993 edit->num_widget_lines / 2);
2994 edit_move_to_line (edit, p->line);
2997 break;
2999 case CK_Beginning_Of_Text:
3000 case CK_Beginning_Of_Text_Highlight:
3001 edit_move_to_top (edit);
3002 break;
3003 case CK_End_Of_Text:
3004 case CK_End_Of_Text_Highlight:
3005 edit_move_to_bottom (edit);
3006 break;
3008 case CK_Copy:
3009 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3010 edit_insert_over (edit);
3011 edit_block_copy_cmd (edit);
3012 break;
3013 case CK_Remove:
3014 edit_block_delete_cmd (edit);
3015 break;
3016 case CK_Move:
3017 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3018 edit_insert_over (edit);
3019 edit_block_move_cmd (edit);
3020 break;
3022 case CK_Shift_Block_Left:
3023 if (edit->mark1 != edit->mark2)
3024 edit_move_block_to_left (edit);
3025 break;
3026 case CK_Shift_Block_Right:
3027 if (edit->mark1 != edit->mark2)
3028 edit_move_block_to_right (edit);
3029 break;
3030 case CK_XStore:
3031 edit_copy_to_X_buf_cmd (edit);
3032 break;
3033 case CK_XCut:
3034 edit_cut_to_X_buf_cmd (edit);
3035 break;
3036 case CK_XPaste:
3037 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3038 edit_insert_over (edit);
3039 edit_paste_from_X_buf_cmd (edit);
3040 break;
3041 case CK_Selection_History:
3042 edit_paste_from_history (edit);
3043 break;
3045 case CK_Save_As:
3046 edit_save_as_cmd (edit);
3047 break;
3048 case CK_Save:
3049 edit_save_confirm_cmd (edit);
3050 break;
3051 case CK_Load:
3052 edit_load_cmd (edit, EDIT_FILE_COMMON);
3053 break;
3054 case CK_Save_Block:
3055 edit_save_block_cmd (edit);
3056 break;
3057 case CK_Insert_File:
3058 edit_insert_file_cmd (edit);
3059 break;
3061 case CK_Load_Prev_File:
3062 edit_load_back_cmd (edit);
3063 break;
3064 case CK_Load_Next_File:
3065 edit_load_forward_cmd (edit);
3066 break;
3068 case CK_Load_Syntax_File:
3069 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3070 break;
3071 case CK_Choose_Syntax:
3072 edit_syntax_dialog ();
3073 break;
3075 case CK_Load_Menu_File:
3076 edit_load_cmd (edit, EDIT_FILE_MENU);
3077 break;
3079 case CK_Toggle_Syntax:
3080 if ((option_syntax_highlighting ^= 1) == 1)
3081 edit_load_syntax (edit, NULL, option_syntax_type);
3082 edit->force |= REDRAW_PAGE;
3083 break;
3085 case CK_Toggle_Tab_TWS:
3086 enable_show_tabs_tws ^= 1;
3087 edit->force |= REDRAW_PAGE;
3088 break;
3090 case CK_Find:
3091 edit_search_cmd (edit, 0);
3092 break;
3093 case CK_Find_Again:
3094 edit_search_cmd (edit, 1);
3095 break;
3096 case CK_Replace:
3097 edit_replace_cmd (edit, 0);
3098 break;
3099 case CK_Replace_Again:
3100 edit_replace_cmd (edit, 1);
3101 break;
3102 case CK_Complete_Word:
3103 /* if text marked shift block */
3104 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3105 edit_move_block_to_left (edit);
3106 } else {
3107 edit_complete_word_cmd (edit);
3109 break;
3110 case CK_Find_Definition:
3111 edit_get_match_keyword_cmd (edit);
3112 break;
3113 case CK_Quit:
3114 dlg_stop (edit->widget.parent);
3115 break;
3116 case CK_New:
3117 edit_new_cmd (edit);
3118 break;
3119 case CK_Help:
3120 edit_help_cmd (edit);
3121 break;
3122 case CK_Refresh:
3123 edit_refresh_cmd (edit);
3124 break;
3125 case CK_SaveSetupCmd:
3126 save_setup_cmd ();
3127 break;
3128 case CK_About:
3129 query_dialog (_(" About "),
3130 _("\n Cooledit v3.11.5\n\n"
3131 " Copyright (C) 1996 the Free Software Foundation\n\n"
3132 " A user friendly text editor written\n"
3133 " for the Midnight Commander.\n"), D_NORMAL,
3134 1, _("&OK"));
3135 break;
3136 case CK_LearnKeys:
3137 learn_keys ();
3138 break;
3139 case CK_Edit_Options:
3140 edit_options_dialog ();
3141 break;
3142 case CK_Edit_Save_Mode:
3143 menu_save_mode_cmd ();
3144 break;
3145 case CK_Date:
3147 char s[BUF_MEDIUM];
3148 /* fool gcc to prevent a Y2K warning */
3149 char time_format[] = "_c";
3150 time_format[0] = '%';
3152 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3153 edit_print_string (edit, s);
3154 edit->force |= REDRAW_PAGE;
3155 break;
3157 break;
3158 case CK_Goto:
3159 edit_goto_cmd (edit);
3160 break;
3161 case CK_Paragraph_Format:
3162 format_paragraph (edit, 1);
3163 edit->force |= REDRAW_PAGE;
3164 break;
3165 case CK_Delete_Macro:
3166 edit_delete_macro_cmd (edit);
3167 break;
3168 case CK_Match_Bracket:
3169 edit_goto_matching_bracket (edit);
3170 break;
3171 case CK_User_Menu:
3172 user_menu (edit);
3173 break;
3174 case CK_Sort:
3175 edit_sort_cmd (edit);
3176 break;
3177 case CK_ExtCmd:
3178 edit_ext_cmd (edit);
3179 break;
3180 case CK_Mail:
3181 edit_mail_dialog (edit);
3182 break;
3183 case CK_Shell:
3184 view_other_cmd ();
3185 break;
3186 case CK_SelectCodepage:
3187 edit_select_codepage_cmd (edit);
3188 break;
3189 case CK_Insert_Literal:
3190 edit_insert_literal_cmd (edit);
3191 break;
3192 case CK_Execute_Macro:
3193 edit_execute_macro_cmd (edit);
3194 break;
3195 case CK_Begin_End_Macro:
3196 edit_begin_end_macro_cmd (edit);
3197 break;
3198 case CK_Ext_Mode:
3199 edit->extmod = 1;
3200 break;
3201 default:
3202 break;
3205 /* CK_Pipe_Block */
3206 if ((command / 1000) == 1) /* a shell command */
3207 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3208 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3209 struct macro m[MAX_MACRO_LENGTH];
3210 int nm;
3211 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3212 edit_execute_macro (edit, m, nm);
3215 /* keys which must set the col position, and the search vars */
3216 switch (command) {
3217 case CK_Find:
3218 case CK_Find_Again:
3219 case CK_Replace:
3220 case CK_Replace_Again:
3221 case CK_Complete_Word:
3222 edit->prev_col = edit_get_col (edit);
3223 break;
3224 case CK_Up:
3225 case CK_Up_Highlight:
3226 case CK_Up_Alt_Highlight:
3227 case CK_Down:
3228 case CK_Down_Highlight:
3229 case CK_Down_Alt_Highlight:
3230 case CK_Page_Up:
3231 case CK_Page_Up_Highlight:
3232 case CK_Page_Up_Alt_Highlight:
3233 case CK_Page_Down:
3234 case CK_Page_Down_Highlight:
3235 case CK_Page_Down_Alt_Highlight:
3236 case CK_Beginning_Of_Text:
3237 case CK_Beginning_Of_Text_Highlight:
3238 case CK_End_Of_Text:
3239 case CK_End_Of_Text_Highlight:
3240 case CK_Paragraph_Up:
3241 case CK_Paragraph_Up_Highlight:
3242 case CK_Paragraph_Up_Alt_Highlight:
3243 case CK_Paragraph_Down:
3244 case CK_Paragraph_Down_Highlight:
3245 case CK_Paragraph_Down_Alt_Highlight:
3246 case CK_Scroll_Up:
3247 case CK_Scroll_Up_Highlight:
3248 case CK_Scroll_Up_Alt_Highlight:
3249 case CK_Scroll_Down:
3250 case CK_Scroll_Down_Highlight:
3251 case CK_Scroll_Down_Alt_Highlight:
3252 edit->search_start = edit->curs1;
3253 edit->found_len = 0;
3254 break;
3255 default:
3256 edit->found_len = 0;
3257 edit->prev_col = edit_get_col (edit);
3258 edit->search_start = edit->curs1;
3260 edit_find_bracket (edit);
3262 if (option_auto_para_formatting) {
3263 switch (command) {
3264 case CK_BackSpace:
3265 case CK_Delete:
3266 case CK_Delete_Word_Left:
3267 case CK_Delete_Word_Right:
3268 case CK_Delete_To_Line_End:
3269 case CK_Delete_To_Line_Begin:
3270 format_paragraph (edit, 0);
3271 edit->force |= REDRAW_PAGE;
3277 static void
3278 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3280 int i = 0;
3282 if (edit->macro_depth++ > 256) {
3283 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3284 edit->macro_depth--;
3285 return;
3287 edit->force |= REDRAW_PAGE;
3288 for (; i < n; i++) {
3289 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3291 edit_update_screen (edit);
3292 edit->macro_depth--;
3295 /* User edit menu, like user menu (F2) but only in editor. */
3296 static void
3297 user_menu (WEdit * edit)
3299 char *block_file;
3300 int nomark;
3301 long start_mark, end_mark;
3302 struct stat status;
3304 block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3306 nomark = eval_marks (edit, &start_mark, &end_mark);
3307 if (nomark == 0)
3308 edit_save_block (edit, block_file, start_mark, end_mark);
3310 /* run shell scripts from menu */
3311 user_menu_cmd (edit);
3313 if ((mc_stat (block_file, &status) == 0) && (status.st_size != 0)) {
3314 int rc = 0;
3315 FILE *fd;
3317 if (nomark == 0) {
3318 /* i.e. we have marked block */
3319 rc = edit_block_delete_cmd (edit);
3322 if (rc == 0)
3323 edit_insert_file (edit, block_file);
3325 /* truncate block file */
3326 fd = fopen (block_file, "w");
3327 if (fd != NULL)
3328 fclose (fd);
3330 edit_refresh_cmd (edit);
3331 edit->force |= REDRAW_COMPLETELY;
3333 g_free (block_file);
3336 void
3337 edit_stack_init (void)
3339 for (edit_stack_iterator = 0;
3340 edit_stack_iterator < MAX_HISTORY_MOVETO;
3341 edit_stack_iterator++ ) {
3342 edit_history_moveto[edit_stack_iterator].filename = NULL;
3343 edit_history_moveto[edit_stack_iterator].line = -1;
3346 edit_stack_iterator = 0;
3349 void
3350 edit_stack_free (void)
3352 for (edit_stack_iterator = 0;
3353 edit_stack_iterator < MAX_HISTORY_MOVETO;
3354 edit_stack_iterator++)
3355 g_free (edit_history_moveto[edit_stack_iterator].filename);
3358 /* move i lines */
3359 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
3361 edit_move_updown (edit, i, scroll, TRUE);
3364 /* move i lines */
3365 void edit_move_down (WEdit * edit, unsigned long i, int scroll)
3367 edit_move_updown (edit, i, scroll, FALSE);