Merge branch '1708_ftp_permission'
[midnight-commander.git] / edit / edit.c
blob9a51b7961a3f32640d180dad13098574b0f4c6f9
1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor low level data handling and cursor fundamentals
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
42 #include "../src/global.h"
44 #include "edit-impl.h"
45 #include "editlock.h"
46 #include "edit-widget.h"
47 #include "../src/cmddef.h"
49 #include "../src/tty/color.h" /* EDITOR_NORMAL_COLOR */
50 #include "../src/tty/tty.h" /* attrset() */
51 #include "../src/tty/key.h" /* is_idle() */
52 #include "../src/skin/skin.h" /* mc_skin_color_get */
54 #include "../src/widget.h" /* buttonbar_redraw() */
55 #include "../src/cmd.h" /* view_other_cmd() */
56 #include "../src/user.h" /* user_menu_cmd() */
57 #include "../src/wtools.h" /* query_dialog() */
58 #include "../src/timefmt.h" /* time formatting */
59 #include "../src/strutil.h" /* utf string functions */
60 #include "../src/charsets.h" /* get_codepage_id */
61 #include "../src/main.h" /* source_codepage */
62 #include "../src/learn.h" /* learn_keys */
64 int option_word_wrap_line_length = 72;
65 int option_typewriter_wrap = 0;
66 int option_auto_para_formatting = 0;
67 int option_fill_tabs_with_spaces = 0;
68 int option_return_does_auto_indent = 1;
69 int option_backspace_through_tabs = 0;
70 int option_fake_half_tabs = 1;
71 int option_save_mode = EDIT_QUICK_SAVE;
72 int option_save_position = 1;
73 int option_max_undo = 32768;
74 int option_persistent_selections = 1;
75 int option_cursor_beyond_eol = 1;
76 int option_line_state = 0;
77 int option_line_state_width = 0;
79 int option_edit_right_extreme = 0;
80 int option_edit_left_extreme = 0;
81 int option_edit_top_extreme = 0;
82 int option_edit_bottom_extreme = 0;
83 int enable_show_tabs_tws = 1;
84 int option_check_nl_at_eof = 0;
86 const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
87 char *option_backup_ext = NULL;
89 int edit_stack_iterator = 0;
90 edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO];
91 /* magic sequense for say than block is vertical */
92 const char VERTICAL_MAGIC[] = {'\1', '\1', '\1', '\1', '\n'};
93 /*-
95 * here's a quick sketch of the layout: (don't run this through indent.)
97 * (b1 is buffers1 and b2 is buffers2)
99 * |
100 * \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
101 * ______________________________________|______________________________________
103 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
104 * |-> |-> |-> |-> |-> |-> |
106 * _<------------------------->|<----------------->_
107 * WEdit->curs2 | WEdit->curs1
108 * ^ | ^
109 * | ^|^ |
110 * cursor ||| cursor
111 * |||
112 * file end|||file beginning
117 * This_is_some_file
118 * fin.
121 const global_keymap_t *editor_map;
122 const global_keymap_t *editor_x_map;
124 static void user_menu (WEdit *edit);
126 int edit_get_byte (WEdit * edit, long byte_index)
128 unsigned long p;
129 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
130 return '\n';
132 if (byte_index >= edit->curs1) {
133 p = edit->curs1 + edit->curs2 - byte_index - 1;
134 return edit->buffers2[p >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1];
135 } else {
136 return edit->buffers1[byte_index >> S_EDIT_BUF_SIZE][byte_index & M_EDIT_BUF_SIZE];
140 char *edit_get_byte_ptr (WEdit * edit, long byte_index)
142 unsigned long p;
144 if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
145 return NULL;
147 if (byte_index >= edit->curs1) {
148 p = edit->curs1 + edit->curs2 - byte_index - 1;
149 return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
150 } else {
151 return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE));
155 char *edit_get_buf_ptr (WEdit * edit, long byte_index)
157 unsigned long p;
159 if (byte_index >= (edit->curs1 + edit->curs2) ) {
160 byte_index -= 1;
163 if ( byte_index < 0 ) {
164 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';
189 str = edit_get_byte_ptr (edit, byte_index);
190 res = g_utf8_get_char_validated (str, -1);
192 if ( res < 0 ) {
193 ch = *str;
194 width = 0;
195 } else {
196 ch = res;
197 /* Calculate UTF-8 char width */
198 next_ch = g_utf8_next_char(str);
199 if ( next_ch ) {
200 width = next_ch - str;
201 } else {
202 ch = 0;
203 width = 0;
206 *char_width = width;
207 return ch;
210 int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
212 gchar *str, *buf = NULL;
213 int res = -1;
214 gunichar ch;
215 gchar *next_ch = NULL;
216 int width = 0;
218 if ( byte_index > 0 ) {
219 byte_index--;
222 ch = edit_get_utf (edit, byte_index, &width);
223 if ( width == 1 ) {
224 *char_width = width;
225 return ch;
228 if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) {
229 *char_width = 0;
230 return 0;
233 str = edit_get_byte_ptr (edit, byte_index);
234 buf = edit_get_buf_ptr (edit, byte_index);
235 /* get prev utf8 char */
236 if ( str != buf )
237 str = g_utf8_find_prev_char (buf, str);
239 res = g_utf8_get_char_validated (str, -1);
240 if ( res < 0 ) {
241 ch = *str;
242 width = 0;
243 } else {
244 ch = res;
245 /* Calculate UTF-8 char width */
246 next_ch = g_utf8_next_char(str);
247 if ( next_ch ) {
248 width = next_ch - str;
249 } else {
250 ch = 0;
251 width = 0;
254 *char_width = width;
255 return ch;
259 * Initialize the buffers for an empty files.
261 static void
262 edit_init_buffers (WEdit *edit)
264 int j;
266 for (j = 0; j <= MAXBUFF; j++) {
267 edit->buffers1[j] = NULL;
268 edit->buffers2[j] = NULL;
271 edit->curs1 = 0;
272 edit->curs2 = 0;
273 edit->buffers2[0] = g_malloc (EDIT_BUF_SIZE);
277 * Load file OR text into buffers. Set cursor to the beginning of file.
278 * Return 1 on error.
280 static int
281 edit_load_file_fast (WEdit *edit, const char *filename)
283 long buf, buf2;
284 int file = -1;
285 edit->curs2 = edit->last_byte;
286 buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
287 edit->utf8 = 0;
288 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
289 GString *errmsg = g_string_new(NULL);
290 g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
291 edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
292 g_string_free (errmsg, TRUE);
293 return 1;
296 if (!edit->buffers2[buf2])
297 edit->buffers2[buf2] = g_malloc (EDIT_BUF_SIZE);
299 mc_read (file,
300 (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE -
301 (edit->curs2 & M_EDIT_BUF_SIZE),
302 edit->curs2 & M_EDIT_BUF_SIZE);
304 for (buf = buf2 - 1; buf >= 0; buf--) {
305 /* edit->buffers2[0] is already allocated */
306 if (!edit->buffers2[buf])
307 edit->buffers2[buf] = g_malloc (EDIT_BUF_SIZE);
308 mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
311 mc_close (file);
312 return 0;
315 /* detecting an error on save is easy: just check if every byte has been written. */
316 /* detecting an error on read, is not so easy 'cos there is not way to tell
317 whether you read everything or not. */
318 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
319 static const struct edit_filters {
320 const char *read, *write, *extension;
321 } all_filters[] = {
322 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
323 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
324 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
325 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
326 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
329 /* Return index of the filter or -1 is there is no appropriate filter */
330 static int edit_find_filter (const char *filename)
332 size_t i, l, e;
333 if (!filename)
334 return -1;
335 l = strlen (filename);
336 for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) {
337 e = strlen (all_filters[i].extension);
338 if (l > e)
339 if (!strcmp (all_filters[i].extension, filename + l - e))
340 return i;
342 return -1;
345 static char *
346 edit_get_filter (const char *filename)
348 int i, l;
349 char *p, *quoted_name;
350 i = edit_find_filter (filename);
351 if (i < 0)
352 return 0;
353 quoted_name = name_quote (filename, 0);
354 l = str_term_width1 (quoted_name);
355 p = g_malloc (str_term_width1 (all_filters[i].read) + l + 2);
356 sprintf (p, all_filters[i].read, quoted_name);
357 g_free (quoted_name);
358 return p;
361 char *
362 edit_get_write_filter (const char *write_name, const char *filename)
364 int i, l;
365 char *p, *writename;
366 i = edit_find_filter (filename);
367 if (i < 0)
368 return 0;
369 writename = name_quote (write_name, 0);
370 l = str_term_width1 (writename);
371 p = g_malloc (str_term_width1 (all_filters[i].write) + l + 2);
372 sprintf (p, all_filters[i].write, writename);
373 g_free (writename);
374 return p;
377 static long
378 edit_insert_stream (WEdit * edit, FILE * f)
380 int c;
381 long i = 0;
382 while ((c = fgetc (f)) >= 0) {
383 edit_insert (edit, c);
384 i++;
386 return i;
389 long edit_write_stream (WEdit * edit, FILE * f)
391 long i;
393 if (edit->lb == LB_ASIS) {
394 for (i = 0; i < edit->last_byte; i++)
395 if (fputc (edit_get_byte (edit, i), f) < 0)
396 break;
397 return i;
400 /* change line breaks */
401 for (i = 0; i < edit->last_byte; i++) {
402 unsigned char c = edit_get_byte (edit, i);
404 if (!(c == '\n' || c == '\r')) {
405 /* not line break */
406 if (fputc (c, f) < 0)
407 return i;
408 } else { /* (c == '\n' || c == '\r') */
409 unsigned char c1 = edit_get_byte (edit, i + 1); /* next char */
411 switch (edit->lb) {
412 case LB_UNIX: /* replace "\r\n" or '\r' to '\n' */
413 /* put one line break unconditionally */
414 if (fputc ('\n', f) < 0)
415 return i;
417 i++; /* 2 chars are processed */
419 if (c == '\r' && c1 == '\n')
420 /* Windows line break; go to the next char */
421 break;
423 if (c == '\r' && c1 == '\r') {
424 /* two Macintosh line breaks; put second line break */
425 if (fputc ('\n', f) < 0)
426 return i;
427 break;
430 if (fputc (c1, f) < 0)
431 return i;
432 break;
434 case LB_WIN: /* replace '\n' or '\r' to "\r\n" */
435 /* put one line break unconditionally */
436 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
437 return i;
439 if (c == '\r' && c1 == '\n')
440 /* Windows line break; go to the next char */
441 i++;
442 break;
444 case LB_MAC: /* replace "\r\n" or '\n' to '\r' */
445 /* put one line break unconditionally */
446 if (fputc ('\r', f) < 0)
447 return i;
449 i++; /* 2 chars are processed */
451 if (c == '\r' && c1 == '\n')
452 /* Windows line break; go to the next char */
453 break;
455 if (c == '\n' && c1 == '\n') {
456 /* two Windows line breaks; put second line break */
457 if (fputc ('\r', f) < 0)
458 return i;
459 break;
462 if (fputc (c1, f) < 0)
463 return i;
464 break;
465 case LB_ASIS: /* default without changes */
466 break;
471 return edit->last_byte;
474 #define TEMP_BUF_LEN 1024
476 /* inserts a file at the cursor, returns 1 on success */
478 edit_insert_file (WEdit *edit, const char *filename)
480 char *p;
481 if ((p = edit_get_filter (filename))) {
482 FILE *f;
483 long current = edit->curs1;
484 f = (FILE *) popen (p, "r");
485 if (f) {
486 edit_insert_stream (edit, f);
487 edit_cursor_move (edit, current - edit->curs1);
488 if (pclose (f) > 0) {
489 GString *errmsg = g_string_new (NULL);
490 g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
491 edit_error_dialog (_("Error"), errmsg->str);
492 g_string_free (errmsg, TRUE);
493 g_free (p);
494 return 0;
496 } else {
497 GString *errmsg = g_string_new (NULL);
498 g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
499 edit_error_dialog (_("Error"), errmsg->str);
500 g_string_free (errmsg, TRUE);
501 g_free (p);
502 return 0;
504 g_free (p);
505 } else {
506 int i, file, blocklen;
507 long current = edit->curs1;
508 int vertical_insertion = 0;
509 char *buf;
510 if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1)
511 return 0;
512 buf = g_malloc (TEMP_BUF_LEN);
513 blocklen = mc_read (file, buf, sizeof(VERTICAL_MAGIC));
514 if (blocklen > 0) {
515 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
516 if ( memcmp(buf, VERTICAL_MAGIC, sizeof(VERTICAL_MAGIC)) == 0 ) {
517 vertical_insertion = 1;
518 } else {
519 mc_lseek (file, 0, SEEK_SET);
522 if (vertical_insertion) {
523 blocklen = edit_insert_column_of_text_from_file (edit, file);
524 } else {
525 while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
526 for (i = 0; i < blocklen; i++)
527 edit_insert (edit, buf[i]);
530 edit_cursor_move (edit, current - edit->curs1);
531 g_free (buf);
532 mc_close (file);
533 if (blocklen)
534 return 0;
536 return 1;
539 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
540 static int
541 check_file_access (WEdit *edit, const char *filename, struct stat *st)
543 int file;
544 GString *errmsg = (GString *) 0;
546 /* Try opening an existing file */
547 file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
549 if (file < 0) {
551 * Try creating the file. O_EXCL prevents following broken links
552 * and opening existing files.
554 file =
555 mc_open (filename,
556 O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
557 0666);
558 if (file < 0) {
559 g_string_sprintf (errmsg = g_string_new (NULL),
560 _(" Cannot open %s for reading "), filename);
561 goto cleanup;
562 } else {
563 /* New file, delete it if it's not modified or saved */
564 edit->delete_file = 1;
568 /* Check what we have opened */
569 if (mc_fstat (file, st) < 0) {
570 g_string_sprintf (errmsg = g_string_new (NULL),
571 _(" Cannot get size/permissions for %s "), filename);
572 goto cleanup;
575 /* We want to open regular files only */
576 if (!S_ISREG (st->st_mode)) {
577 g_string_sprintf (errmsg = g_string_new (NULL),
578 _(" %s is not a regular file "), filename);
579 goto cleanup;
583 * Don't delete non-empty files.
584 * O_EXCL should prevent it, but let's be on the safe side.
586 if (st->st_size > 0) {
587 edit->delete_file = 0;
590 if (st->st_size >= SIZE_LIMIT) {
591 g_string_sprintf (errmsg = g_string_new (NULL),
592 _(" File %s is too large "), filename);
593 goto cleanup;
596 cleanup:
597 (void) mc_close (file);
598 if (errmsg) {
599 edit_error_dialog (_("Error"), errmsg->str);
600 g_string_free (errmsg, TRUE);
601 return 1;
603 return 0;
607 * Open the file and load it into the buffers, either directly or using
608 * a filter. Return 0 on success, 1 on error.
610 * Fast loading (edit_load_file_fast) is used when the file size is
611 * known. In this case the data is read into the buffers by blocks.
612 * If the file size is not known, the data is loaded byte by byte in
613 * edit_insert_file.
615 static int
616 edit_load_file (WEdit *edit)
618 int fast_load = 1;
620 /* Cannot do fast load if a filter is used */
621 if (edit_find_filter (edit->filename) >= 0)
622 fast_load = 0;
625 * VFS may report file size incorrectly, and slow load is not a big
626 * deal considering overhead in VFS.
628 if (!vfs_file_is_local (edit->filename))
629 fast_load = 0;
632 * FIXME: line end translation should disable fast loading as well
633 * Consider doing fseek() to the end and ftell() for the real size.
636 if (*edit->filename) {
637 /* If we are dealing with a real file, check that it exists */
638 if (check_file_access (edit, edit->filename, &edit->stat1))
639 return 1;
640 } else {
641 /* nothing to load */
642 fast_load = 0;
645 edit_init_buffers (edit);
647 if (fast_load) {
648 edit->last_byte = edit->stat1.st_size;
649 edit_load_file_fast (edit, edit->filename);
650 /* If fast load was used, the number of lines wasn't calculated */
651 edit->total_lines = edit_count_lines (edit, 0, edit->last_byte);
652 } else {
653 #ifdef HAVE_CHARSET
654 const char *codepage_id;
655 #endif
656 edit->last_byte = 0;
657 if (*edit->filename) {
658 edit->stack_disable = 1;
659 if (!edit_insert_file (edit, edit->filename)) {
660 edit_clean (edit);
661 return 1;
663 edit->stack_disable = 0;
666 #ifdef HAVE_CHARSET
667 codepage_id = get_codepage_id( source_codepage );
668 if ( codepage_id )
669 edit->utf8 = str_isutf8 ( codepage_id );
670 #endif
672 edit->lb = LB_ASIS;
673 return 0;
676 /* Restore saved cursor position in the file */
677 static void
678 edit_load_position (WEdit *edit)
680 char *filename;
681 long line, column;
683 if (!edit->filename || !*edit->filename)
684 return;
686 filename = vfs_canon (edit->filename);
687 load_file_position (filename, &line, &column);
688 g_free (filename);
690 edit_move_to_line (edit, line - 1);
691 edit->prev_col = column;
692 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
693 edit_move_display (edit, line - (edit->num_widget_lines / 2));
696 /* Save cursor position in the file */
697 static void
698 edit_save_position (WEdit *edit)
700 char *filename;
702 if (!edit->filename || !*edit->filename)
703 return;
705 filename = vfs_canon (edit->filename);
706 save_file_position (filename, edit->curs_line + 1, edit->curs_col);
707 g_free (filename);
710 /* Clean the WEdit stricture except the widget part */
711 static void
712 edit_purge_widget (WEdit *edit)
714 int len = sizeof (WEdit) - sizeof (Widget);
715 char *start = (char *) edit + sizeof (Widget);
716 memset (start, 0, len);
717 edit->macro_i = -1; /* not recording a macro */
720 static void
721 edit_set_keymap (void)
723 editor_map = default_editor_keymap;
724 if (editor_keymap && editor_keymap->len > 0)
725 editor_map = (global_keymap_t *) editor_keymap->data;
727 editor_x_map = default_editor_x_keymap;
728 if (editor_x_keymap && editor_x_keymap->len > 0)
729 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
733 #define space_width 1
736 * Fill in the edit structure. Return NULL on failure. Pass edit as
737 * NULL to allocate a new structure.
739 * If line is 0, try to restore saved position. Otherwise put the
740 * cursor on that line and show it in the middle of the screen.
742 WEdit *
743 edit_init (WEdit *edit, int lines, int columns, const char *filename,
744 long line)
746 int to_free = 0;
747 option_auto_syntax = 1; /* Resetting to auto on every invokation */
748 if ( option_line_state ) {
749 option_line_state_width = LINE_STATE_WIDTH;
750 } else {
751 option_line_state_width = 0;
753 if (!edit) {
754 #ifdef ENABLE_NLS
756 * Expand option_whole_chars_search by national letters using
757 * current locale
760 static char option_whole_chars_search_buf[256];
762 if (option_whole_chars_search_buf != option_whole_chars_search) {
763 size_t i;
764 size_t len = str_term_width1 (option_whole_chars_search);
766 strcpy (option_whole_chars_search_buf,
767 option_whole_chars_search);
769 for (i = 1; i <= sizeof (option_whole_chars_search_buf); i++) {
770 if (g_ascii_islower ((gchar) i) && !strchr (option_whole_chars_search, i)) {
771 option_whole_chars_search_buf[len++] = i;
775 option_whole_chars_search_buf[len] = 0;
776 option_whole_chars_search = option_whole_chars_search_buf;
778 #endif /* ENABLE_NLS */
779 edit = g_malloc0 (sizeof (WEdit));
780 edit->search = NULL;
781 to_free = 1;
783 edit_purge_widget (edit);
784 edit->num_widget_lines = lines;
785 edit->over_col = 0;
786 edit->num_widget_columns = columns;
787 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
788 edit->stat1.st_uid = getuid ();
789 edit->stat1.st_gid = getgid ();
790 edit->stat1.st_mtime = 0;
791 edit->bracket = -1;
792 edit->force |= REDRAW_PAGE;
793 edit_set_filename (edit, filename);
794 edit->stack_size = START_STACK_SIZE;
795 edit->stack_size_mask = START_STACK_SIZE - 1;
796 edit->undo_stack = g_malloc ((edit->stack_size + 10) * sizeof (long));
797 if (edit_load_file (edit)) {
798 /* edit_load_file already gives an error message */
799 if (to_free)
800 g_free (edit);
801 return 0;
803 edit->utf8 = 0;
804 edit->converter = str_cnv_from_term;
805 #ifdef HAVE_CHARSET
807 const char *cp_id = NULL;
808 cp_id = get_codepage_id (source_codepage >= 0 ?
809 source_codepage : display_codepage);
811 if (cp_id != NULL) {
812 GIConv conv;
813 conv = str_crt_conv_from (cp_id);
814 if (conv != INVALID_CONV) {
815 if (edit->converter != str_cnv_from_term)
816 str_close_conv (edit->converter);
817 edit->converter = conv;
820 if (cp_id != NULL)
821 edit->utf8 = str_isutf8 (cp_id);
823 #endif
825 edit->loading_done = 1;
826 edit->modified = 0;
827 edit->locked = 0;
828 edit_load_syntax (edit, 0, 0);
830 int color;
831 edit_get_syntax_color (edit, -1, &color);
834 /* load saved cursor position */
835 if ((line == 0) && option_save_position) {
836 edit_load_position (edit);
837 } else {
838 if (line <= 0)
839 line = 1;
840 edit_move_display (edit, line - 1);
841 edit_move_to_line (edit, line - 1);
844 edit_set_keymap ();
846 return edit;
849 /* Clear the edit struct, freeing everything in it. Return 1 on success */
851 edit_clean (WEdit *edit)
853 int j = 0;
855 if (!edit)
856 return 0;
858 /* a stale lock, remove it */
859 if (edit->locked)
860 edit->locked = edit_unlock_file (edit->filename);
862 /* save cursor position */
863 if (option_save_position)
864 edit_save_position (edit);
866 /* File specified on the mcedit command line and never saved */
867 if (edit->delete_file)
868 unlink (edit->filename);
870 edit_free_syntax_rules (edit);
871 book_mark_flush (edit, -1);
872 for (; j <= MAXBUFF; j++) {
873 g_free (edit->buffers1[j]);
874 g_free (edit->buffers2[j]);
877 g_free (edit->undo_stack);
878 g_free (edit->filename);
879 g_free (edit->dir);
881 if (edit->search)
883 mc_search_free(edit->search);
884 edit->search = NULL;
886 edit_purge_widget (edit);
888 return 1;
892 /* returns 1 on success */
893 int edit_renew (WEdit * edit)
895 int lines = edit->num_widget_lines;
896 int columns = edit->num_widget_columns;
897 int retval = 1;
899 edit_clean (edit);
900 if (!edit_init (edit, lines, columns, "", 0))
901 retval = 0;
902 return retval;
906 * Load a new file into the editor. If it fails, preserve the old file.
907 * To do it, allocate a new widget, initialize it and, if the new file
908 * was loaded, copy the data to the old widget.
909 * Return 1 on success, 0 on failure.
912 edit_reload (WEdit *edit, const char *filename)
914 WEdit *e;
915 int lines = edit->num_widget_lines;
916 int columns = edit->num_widget_columns;
918 e = g_malloc0 (sizeof (WEdit));
919 e->widget = edit->widget;
920 if (!edit_init (e, lines, columns, filename, 0)) {
921 g_free (e);
922 return 0;
924 edit_clean (edit);
925 memcpy (edit, e, sizeof (WEdit));
926 g_free (e);
927 return 1;
931 * Load a new file into the editor and set line. If it fails, preserve the old file.
932 * To do it, allocate a new widget, initialize it and, if the new file
933 * was loaded, copy the data to the old widget.
934 * Return 1 on success, 0 on failure.
937 edit_reload_line (WEdit *edit, const char *filename, long line)
939 WEdit *e;
940 int lines = edit->num_widget_lines;
941 int columns = edit->num_widget_columns;
943 e = g_malloc0 (sizeof (WEdit));
944 e->widget = edit->widget;
945 if (!edit_init (e, lines, columns, filename, line)) {
946 g_free (e);
947 return 0;
949 edit_clean (edit);
950 memcpy (edit, e, sizeof (WEdit));
951 g_free (e);
952 return 1;
957 Recording stack for undo:
958 The following is an implementation of a compressed stack. Identical
959 pushes are recorded by a negative prefix indicating the number of times the
960 same char was pushed. This saves space for repeated curs-left or curs-right
961 delete etc.
965 pushed: stored:
969 b -3
971 c --> -4
977 If the stack long int is 0-255 it represents a normal insert (from a backspace),
978 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
979 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
980 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
981 position.
983 The only way the cursor moves or the buffer is changed is through the routines:
984 insert, backspace, insert_ahead, delete, and cursor_move.
985 These record the reverse undo movements onto the stack each time they are
986 called.
988 Each key press results in a set of actions (insert; delete ...). So each time
989 a key is pressed the current position of start_display is pushed as
990 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
991 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
992 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
996 void edit_push_action (WEdit * edit, long c,...)
998 unsigned long sp = edit->stack_pointer;
999 unsigned long spm1;
1000 long *t;
1002 /* first enlarge the stack if necessary */
1003 if (sp > edit->stack_size - 10) { /* say */
1004 if (option_max_undo < 256)
1005 option_max_undo = 256;
1006 if (edit->stack_size < (unsigned long) option_max_undo) {
1007 t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long));
1008 if (t) {
1009 edit->undo_stack = t;
1010 edit->stack_size <<= 1;
1011 edit->stack_size_mask = edit->stack_size - 1;
1015 spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask;
1016 if (edit->stack_disable)
1017 return;
1019 #ifdef FAST_MOVE_CURSOR
1020 if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) {
1021 va_list ap;
1022 edit->undo_stack[sp] = c == CURS_LEFT_LOTS ? CURS_LEFT : CURS_RIGHT;
1023 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1024 va_start (ap, c);
1025 c = -(va_arg (ap, int));
1026 va_end (ap);
1027 } else
1028 #endif /* ! FAST_MOVE_CURSOR */
1029 if (edit->stack_bottom != sp
1030 && spm1 != edit->stack_bottom
1031 && ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom) {
1032 int d;
1033 if (edit->undo_stack[spm1] < 0) {
1034 d = edit->undo_stack[(sp - 2) & edit->stack_size_mask];
1035 if (d == c) {
1036 if (edit->undo_stack[spm1] > -1000000000) {
1037 if (c < KEY_PRESS) /* --> no need to push multiple do-nothings */
1038 edit->undo_stack[spm1]--;
1039 return;
1042 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1043 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1044 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1045 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1046 if (edit->undo_stack[spm1] == -2)
1047 edit->stack_pointer = spm1;
1048 else
1049 edit->undo_stack[spm1]++;
1050 return;
1052 #endif
1053 } else {
1054 d = edit->undo_stack[spm1];
1055 if (d == c) {
1056 if (c >= KEY_PRESS)
1057 return; /* --> no need to push multiple do-nothings */
1058 edit->undo_stack[sp] = -2;
1059 goto check_bottom;
1061 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1062 else if ((c == CURS_LEFT && d == CURS_RIGHT)
1063 || (c == CURS_RIGHT && d == CURS_LEFT)) { /* a left then a right anihilate each other */
1064 edit->stack_pointer = spm1;
1065 return;
1067 #endif
1070 edit->undo_stack[sp] = c;
1071 check_bottom:
1073 edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
1075 /* if the sp wraps round and catches the stack_bottom then erase
1076 * the first set of actions on the stack to make space - by moving
1077 * stack_bottom forward one "key press" */
1078 c = (edit->stack_pointer + 2) & edit->stack_size_mask;
1079 if ((unsigned long) c == edit->stack_bottom ||
1080 (((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom)
1081 do {
1082 edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask;
1083 } while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS && edit->stack_bottom != edit->stack_pointer);
1085 /*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: */
1086 if (edit->stack_pointer != edit->stack_bottom && edit->undo_stack[edit->stack_bottom] < KEY_PRESS)
1087 edit->stack_bottom = edit->stack_pointer = 0;
1091 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1092 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1094 static long
1095 pop_action (WEdit * edit)
1097 long c;
1098 unsigned long sp = edit->stack_pointer;
1099 if (sp == edit->stack_bottom) {
1100 return STACK_BOTTOM;
1102 sp = (sp - 1) & edit->stack_size_mask;
1103 if ((c = edit->undo_stack[sp]) >= 0) {
1104 /* edit->undo_stack[sp] = '@'; */
1105 edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask;
1106 return c;
1108 if (sp == edit->stack_bottom) {
1109 return STACK_BOTTOM;
1111 c = edit->undo_stack[(sp - 1) & edit->stack_size_mask];
1112 if (edit->undo_stack[sp] == -2) {
1113 /* edit->undo_stack[sp] = '@'; */
1114 edit->stack_pointer = sp;
1115 } else
1116 edit->undo_stack[sp]++;
1118 return c;
1121 /* is called whenever a modification is made by one of the four routines below */
1122 static void edit_modification (WEdit * edit)
1124 edit->caches_valid = 0;
1125 edit->screen_modified = 1;
1127 /* raise lock when file modified */
1128 if (!edit->modified && !edit->delete_file)
1129 edit->locked = edit_lock_file (edit->filename);
1130 edit->modified = 1;
1134 Basic low level single character buffer alterations and movements at the cursor.
1135 Returns char passed over, inserted or removed.
1138 void
1139 edit_insert (WEdit *edit, int c)
1141 /* check if file has grown to large */
1142 if (edit->last_byte >= SIZE_LIMIT)
1143 return;
1145 /* first we must update the position of the display window */
1146 if (edit->curs1 < edit->start_display) {
1147 edit->start_display++;
1148 if (c == '\n')
1149 edit->start_line++;
1152 /* Mark file as modified, unless the file hasn't been fully loaded */
1153 if (edit->loading_done) {
1154 edit_modification (edit);
1157 /* now we must update some info on the file and check if a redraw is required */
1158 if (c == '\n') {
1159 if (edit->book_mark)
1160 book_mark_inc (edit, edit->curs_line);
1161 edit->curs_line++;
1162 edit->total_lines++;
1163 edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
1166 /* save the reverse command onto the undo stack */
1167 edit_push_action (edit, BACKSPACE);
1169 /* update markers */
1170 edit->mark1 += (edit->mark1 > edit->curs1);
1171 edit->mark2 += (edit->mark2 > edit->curs1);
1172 edit->last_get_rule += (edit->last_get_rule > edit->curs1);
1174 /* add a new buffer if we've reached the end of the last one */
1175 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1176 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] =
1177 g_malloc (EDIT_BUF_SIZE);
1179 /* perform the insertion */
1180 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->
1181 curs1 & M_EDIT_BUF_SIZE]
1182 = (unsigned char) c;
1184 /* update file length */
1185 edit->last_byte++;
1187 /* update cursor position */
1188 edit->curs1++;
1191 static void
1192 edit_insert_over (WEdit * edit)
1194 int i;
1196 for ( i = 0; i < edit->over_col; i++ ) {
1197 edit_insert (edit, ' ');
1199 edit->over_col = 0;
1202 /* same as edit_insert and move left */
1203 void edit_insert_ahead (WEdit * edit, int c)
1205 if (edit->last_byte >= SIZE_LIMIT)
1206 return;
1207 if (edit->curs1 < edit->start_display) {
1208 edit->start_display++;
1209 if (c == '\n')
1210 edit->start_line++;
1212 edit_modification (edit);
1213 if (c == '\n') {
1214 if (edit->book_mark)
1215 book_mark_inc (edit, edit->curs_line);
1216 edit->total_lines++;
1217 edit->force |= REDRAW_AFTER_CURSOR;
1219 edit_push_action (edit, DELCHAR);
1221 edit->mark1 += (edit->mark1 >= edit->curs1);
1222 edit->mark2 += (edit->mark2 >= edit->curs1);
1223 edit->last_get_rule += (edit->last_get_rule >= edit->curs1);
1225 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1226 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1227 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1229 edit->last_byte++;
1230 edit->curs2++;
1234 int edit_delete (WEdit * edit, const int byte_delete)
1236 int p = 0;
1237 int cw = 1;
1238 int i;
1240 if (!edit->curs2)
1241 return 0;
1243 edit->mark1 -= (edit->mark1 > edit->curs1);
1244 edit->mark2 -= (edit->mark2 > edit->curs1);
1245 edit->last_get_rule -= (edit->last_get_rule > edit->curs1);
1247 cw = 1;
1248 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1249 if ( edit->utf8 && byte_delete == 0 ) {
1250 edit_get_utf (edit, edit->curs1, &cw);
1251 if ( cw < 1 )
1252 cw = 1;
1254 for ( i = 1; i<= cw; i++ ) {
1255 p = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1257 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1258 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1259 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = NULL;
1261 edit->last_byte--;
1262 edit->curs2--;
1263 edit_push_action (edit, p + 256);
1266 edit_modification (edit);
1267 if (p == '\n') {
1268 if (edit->book_mark)
1269 book_mark_dec (edit, edit->curs_line);
1270 edit->total_lines--;
1271 edit->force |= REDRAW_AFTER_CURSOR;
1273 if (edit->curs1 < edit->start_display) {
1274 edit->start_display--;
1275 if (p == '\n')
1276 edit->start_line--;
1279 return p;
1283 static int
1284 edit_backspace (WEdit * edit, const int byte_delete)
1286 int p = 0;
1287 int cw = 1;
1288 int i;
1290 if (!edit->curs1)
1291 return 0;
1293 edit->mark1 -= (edit->mark1 >= edit->curs1);
1294 edit->mark2 -= (edit->mark2 >= edit->curs1);
1295 edit->last_get_rule -= (edit->last_get_rule >= edit->curs1);
1297 cw = 1;
1298 if ( edit->utf8 && byte_delete == 0 ) {
1299 edit_get_prev_utf (edit, edit->curs1, &cw);
1300 if ( cw < 1 )
1301 cw = 1;
1303 for ( i = 1; i<= cw; i++ ) {
1304 p = *(edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE] + ((edit->curs1 - 1) & M_EDIT_BUF_SIZE));
1305 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1306 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1307 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1309 edit->last_byte--;
1310 edit->curs1--;
1311 edit_push_action (edit, p);
1313 edit_modification (edit);
1314 if (p == '\n') {
1315 if (edit->book_mark)
1316 book_mark_dec (edit, edit->curs_line);
1317 edit->curs_line--;
1318 edit->total_lines--;
1319 edit->force |= REDRAW_AFTER_CURSOR;
1322 if (edit->curs1 < edit->start_display) {
1323 edit->start_display--;
1324 if (p == '\n')
1325 edit->start_line--;
1328 return p;
1331 #ifdef FAST_MOVE_CURSOR
1333 static void memqcpy (WEdit * edit, unsigned char *dest, unsigned char *src, int n)
1335 unsigned long next;
1336 while ((next = (unsigned long) memccpy (dest, src, '\n', n))) {
1337 edit->curs_line--;
1338 next -= (unsigned long) dest;
1339 n -= next;
1340 src += next;
1341 dest += next;
1346 edit_move_backward_lots (WEdit *edit, long increment)
1348 int r, s, t;
1349 unsigned char *p;
1351 if (increment > edit->curs1)
1352 increment = edit->curs1;
1353 if (increment <= 0)
1354 return -1;
1355 edit_push_action (edit, CURS_RIGHT_LOTS, increment);
1357 t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
1358 if (r > increment)
1359 r = increment;
1360 s = edit->curs1 & M_EDIT_BUF_SIZE;
1362 p = 0;
1363 if (s > r) {
1364 memqcpy (edit,
1365 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1366 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - r,
1368 } else {
1369 if (s) {
1370 memqcpy (edit,
1371 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t -
1372 s, edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE], s);
1373 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1374 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1376 memqcpy (edit,
1377 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] + t - r,
1378 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1379 EDIT_BUF_SIZE - (r - s), r - s);
1381 increment -= r;
1382 edit->curs1 -= r;
1383 edit->curs2 += r;
1384 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1385 if (p)
1386 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1387 else
1388 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1389 g_malloc (EDIT_BUF_SIZE);
1390 } else {
1391 g_free (p);
1394 s = edit->curs1 & M_EDIT_BUF_SIZE;
1395 while (increment) {
1396 p = 0;
1397 r = EDIT_BUF_SIZE;
1398 if (r > increment)
1399 r = increment;
1400 t = s;
1401 if (r < t)
1402 t = r;
1403 memqcpy (edit,
1404 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1405 EDIT_BUF_SIZE - t,
1406 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] + s - t,
1408 if (r >= s) {
1409 if (t) {
1410 p = edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE];
1411 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = 0;
1413 memqcpy (edit,
1414 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] +
1415 EDIT_BUF_SIZE - r,
1416 edit->buffers1[(edit->curs1 >> S_EDIT_BUF_SIZE) - 1] +
1417 EDIT_BUF_SIZE - (r - s), r - s);
1419 increment -= r;
1420 edit->curs1 -= r;
1421 edit->curs2 += r;
1422 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1423 if (p)
1424 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = p;
1425 else
1426 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] =
1427 g_malloc (EDIT_BUF_SIZE);
1428 } else {
1429 g_free (p);
1432 return edit_get_byte (edit, edit->curs1);
1435 #endif /* ! FAST_MOVE_CURSOR */
1437 /* moves the cursor right or left: increment positive or negative respectively */
1438 void edit_cursor_move (WEdit * edit, long increment)
1440 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1441 int c;
1442 #ifdef FAST_MOVE_CURSOR
1443 if (increment < -256) {
1444 edit->force |= REDRAW_PAGE;
1445 edit_move_backward_lots (edit, -increment);
1446 return;
1448 #endif /* ! FAST_MOVE_CURSOR */
1450 if (increment < 0) {
1451 for (; increment < 0; increment++) {
1452 if (!edit->curs1)
1453 return;
1455 edit_push_action (edit, CURS_RIGHT);
1457 c = edit_get_byte (edit, edit->curs1 - 1);
1458 if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
1459 edit->buffers2[(edit->curs2 + 1) >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1460 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE) - 1] = c;
1461 edit->curs2++;
1462 c = edit->buffers1[(edit->curs1 - 1) >> S_EDIT_BUF_SIZE][(edit->curs1 - 1) & M_EDIT_BUF_SIZE];
1463 if (!((edit->curs1 - 1) & M_EDIT_BUF_SIZE)) {
1464 g_free (edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE]);
1465 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = NULL;
1467 edit->curs1--;
1468 if (c == '\n') {
1469 edit->curs_line--;
1470 edit->force |= REDRAW_LINE_BELOW;
1474 } else if (increment > 0) {
1475 for (; increment > 0; increment--) {
1476 if (!edit->curs2)
1477 return;
1479 edit_push_action (edit, CURS_LEFT);
1481 c = edit_get_byte (edit, edit->curs1);
1482 if (!(edit->curs1 & M_EDIT_BUF_SIZE))
1483 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE] = g_malloc (EDIT_BUF_SIZE);
1484 edit->buffers1[edit->curs1 >> S_EDIT_BUF_SIZE][edit->curs1 & M_EDIT_BUF_SIZE] = c;
1485 edit->curs1++;
1486 c = edit->buffers2[(edit->curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - ((edit->curs2 - 1) & M_EDIT_BUF_SIZE) - 1];
1487 if (!(edit->curs2 & M_EDIT_BUF_SIZE)) {
1488 g_free (edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE]);
1489 edit->buffers2[edit->curs2 >> S_EDIT_BUF_SIZE] = 0;
1491 edit->curs2--;
1492 if (c == '\n') {
1493 edit->curs_line++;
1494 edit->force |= REDRAW_LINE_ABOVE;
1500 /* These functions return positions relative to lines */
1502 /* returns index of last char on line + 1 */
1503 long edit_eol (WEdit * edit, long current)
1505 if (current < edit->last_byte) {
1506 for (;; current++)
1507 if (edit_get_byte (edit, current) == '\n')
1508 break;
1509 } else
1510 return edit->last_byte;
1511 return current;
1514 /* returns index of first char on line */
1515 long edit_bol (WEdit * edit, long current)
1517 if (current > 0) {
1518 for (;; current--)
1519 if (edit_get_byte (edit, current - 1) == '\n')
1520 break;
1521 } else
1522 return 0;
1523 return current;
1527 int edit_count_lines (WEdit * edit, long current, int upto)
1529 int lines = 0;
1530 if (upto > edit->last_byte)
1531 upto = edit->last_byte;
1532 if (current < 0)
1533 current = 0;
1534 while (current < upto)
1535 if (edit_get_byte (edit, current++) == '\n')
1536 lines++;
1537 return lines;
1541 /* If lines is zero this returns the count of lines from current to upto. */
1542 /* If upto is zero returns index of lines forward current. */
1543 long edit_move_forward (WEdit * edit, long current, int lines, long upto)
1545 if (upto) {
1546 return edit_count_lines (edit, current, upto);
1547 } else {
1548 int next;
1549 if (lines < 0)
1550 lines = 0;
1551 while (lines--) {
1552 next = edit_eol (edit, current) + 1;
1553 if (next > edit->last_byte)
1554 break;
1555 else
1556 current = next;
1558 return current;
1563 /* Returns offset of 'lines' lines up from current */
1564 long edit_move_backward (WEdit * edit, long current, int lines)
1566 if (lines < 0)
1567 lines = 0;
1568 current = edit_bol (edit, current);
1569 while((lines--) && current != 0)
1570 current = edit_bol (edit, current - 1);
1571 return current;
1574 /* If cols is zero this returns the count of columns from current to upto. */
1575 /* If upto is zero returns index of cols across from current. */
1576 long edit_move_forward3 (WEdit * edit, long current, int cols, long upto)
1578 long p, q;
1579 int col = 0;
1580 int cw = 1;
1581 #ifdef HAVE_CHARSET
1582 int utf_ch = 0;
1583 #endif
1584 if (upto) {
1585 q = upto;
1586 cols = -10;
1587 } else
1588 q = edit->last_byte + 2;
1589 p = current;
1590 while (p < q) {
1591 int c;
1592 #ifdef HAVE_CHARSET
1593 cw = 1;
1594 utf_ch = 0;
1595 #endif
1596 if (cols != -10) {
1597 if (col == cols)
1598 return p;
1599 if (col > cols)
1600 return p - 1;
1602 #ifdef HAVE_CHARSET
1603 if ( !edit->utf8 ) {
1604 #endif
1605 c = edit_get_byte (edit, p);
1606 #ifdef HAVE_CHARSET
1607 } else {
1608 cw = 1;
1609 c = utf_ch = edit_get_utf (edit, p, &cw);
1611 #endif
1612 if (c == '\t')
1613 col += TAB_SIZE - col % TAB_SIZE;
1614 else if (c == '\n') {
1615 if (upto)
1616 return col;
1617 else
1618 return p;
1619 } else if (c < 32 || c == 127)
1620 col += 2; /* Caret notation for control characters */
1621 else
1622 col++;
1623 #ifdef HAVE_CHARSET
1624 if ( cw > 1 ) {
1625 if (edit->utf8 && g_unichar_iswide(utf_ch)) {
1626 col -= cw - 1;
1627 if (utf8_display)
1628 col += 1;
1629 } else {
1630 col -= 2;
1632 p += cw - 1;
1634 else
1635 #endif
1637 p++;
1640 return col;
1643 /* returns the current column position of the cursor */
1644 int edit_get_col (WEdit * edit)
1646 return edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit->curs1);
1650 /* Scrolling functions */
1652 void edit_update_curs_row (WEdit * edit)
1654 edit->curs_row = edit->curs_line - edit->start_line;
1657 void edit_update_curs_col (WEdit * edit)
1659 edit->curs_col = edit_move_forward3(edit, edit_bol(edit, edit->curs1), 0, edit->curs1);
1663 edit_get_curs_col (const WEdit *edit)
1665 return edit->curs_col;
1668 /*moves the display start position up by i lines */
1669 void edit_scroll_upward (WEdit * edit, unsigned long i)
1671 unsigned long lines_above = edit->start_line;
1672 if (i > lines_above)
1673 i = lines_above;
1674 if (i) {
1675 edit->start_line -= i;
1676 edit->start_display = edit_move_backward (edit, edit->start_display, i);
1677 edit->force |= REDRAW_PAGE;
1678 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1680 edit_update_curs_row (edit);
1684 /* returns 1 if could scroll, 0 otherwise */
1685 void edit_scroll_downward (WEdit * edit, int i)
1687 int lines_below;
1688 lines_below = edit->total_lines - edit->start_line - (edit->num_widget_lines - 1);
1689 if (lines_below > 0) {
1690 if (i > lines_below)
1691 i = lines_below;
1692 edit->start_line += i;
1693 edit->start_display = edit_move_forward (edit, edit->start_display, i, 0);
1694 edit->force |= REDRAW_PAGE;
1695 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1697 edit_update_curs_row (edit);
1700 void edit_scroll_right (WEdit * edit, int i)
1702 edit->force |= REDRAW_PAGE;
1703 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1704 edit->start_col -= i;
1707 void edit_scroll_left (WEdit * edit, int i)
1709 if (edit->start_col) {
1710 edit->start_col += i;
1711 if (edit->start_col > 0)
1712 edit->start_col = 0;
1713 edit->force |= REDRAW_PAGE;
1714 edit->force &= (0xfff - REDRAW_CHAR_ONLY);
1718 /* high level cursor movement commands */
1720 static int is_in_indent (WEdit *edit)
1722 long p = edit_bol (edit, edit->curs1);
1723 while (p < edit->curs1)
1724 if (!strchr (" \t", edit_get_byte (edit, p++)))
1725 return 0;
1726 return 1;
1729 static int left_of_four_spaces (WEdit *edit);
1731 void
1732 edit_move_to_prev_col (WEdit * edit, long p)
1734 int prev = edit->prev_col;
1735 int over = edit->over_col;
1736 edit_cursor_move (edit, edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->curs1);
1738 if (option_cursor_beyond_eol) {
1739 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0, edit_eol(edit, edit->curs1));
1741 if (line_len < prev + edit->over_col) {
1742 edit->over_col = prev + over - line_len;
1743 edit->prev_col = line_len;
1744 edit->curs_col = line_len;
1745 } else {
1746 edit->curs_col = prev + over;
1747 edit->prev_col = edit->curs_col;
1748 edit->over_col = 0;
1750 } else {
1751 edit->over_col = 0;
1752 if (is_in_indent (edit) && option_fake_half_tabs) {
1753 edit_update_curs_col (edit);
1754 if (space_width)
1755 if (edit->curs_col % (HALF_TAB_SIZE * space_width)) {
1756 int q = edit->curs_col;
1757 edit->curs_col -= (edit->curs_col % (HALF_TAB_SIZE * space_width));
1758 p = edit_bol (edit, edit->curs1);
1759 edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->curs_col, 0) - edit->curs1);
1760 if (!left_of_four_spaces (edit))
1761 edit_cursor_move (edit, edit_move_forward3 (edit, p, q, 0) - edit->curs1);
1767 /* move i lines */
1768 void edit_move_up (WEdit * edit, unsigned long i, int scroll)
1770 unsigned long p, l = edit->curs_line;
1772 if (i > l)
1773 i = l;
1774 if (i) {
1775 if (i > 1)
1776 edit->force |= REDRAW_PAGE;
1777 if (scroll)
1778 edit_scroll_upward (edit, i);
1780 p = edit_bol (edit, edit->curs1);
1781 edit_cursor_move (edit, (p = edit_move_backward (edit, p, i)) - edit->curs1);
1782 edit_move_to_prev_col (edit, p);
1784 edit->search_start = edit->curs1;
1785 edit->found_len = 0;
1789 static int
1790 is_blank (WEdit *edit, long offset)
1792 long s, f;
1793 int c;
1794 s = edit_bol (edit, offset);
1795 f = edit_eol (edit, offset) - 1;
1796 while (s <= f) {
1797 c = edit_get_byte (edit, s++);
1798 if (!isspace (c))
1799 return 0;
1801 return 1;
1805 /* returns the offset of line i */
1806 static long
1807 edit_find_line (WEdit *edit, int line)
1809 int i, j = 0;
1810 int m = 2000000000;
1811 if (!edit->caches_valid) {
1812 for (i = 0; i < N_LINE_CACHES; i++)
1813 edit->line_numbers[i] = edit->line_offsets[i] = 0;
1814 /* three offsets that we *know* are line 0 at 0 and these two: */
1815 edit->line_numbers[1] = edit->curs_line;
1816 edit->line_offsets[1] = edit_bol (edit, edit->curs1);
1817 edit->line_numbers[2] = edit->total_lines;
1818 edit->line_offsets[2] = edit_bol (edit, edit->last_byte);
1819 edit->caches_valid = 1;
1821 if (line >= edit->total_lines)
1822 return edit->line_offsets[2];
1823 if (line <= 0)
1824 return 0;
1825 /* find the closest known point */
1826 for (i = 0; i < N_LINE_CACHES; i++) {
1827 int n;
1828 n = abs (edit->line_numbers[i] - line);
1829 if (n < m) {
1830 m = n;
1831 j = i;
1834 if (m == 0)
1835 return edit->line_offsets[j]; /* know the offset exactly */
1836 if (m == 1 && j >= 3)
1837 i = j; /* one line different - caller might be looping, so stay in this cache */
1838 else
1839 i = 3 + (rand () % (N_LINE_CACHES - 3));
1840 if (line > edit->line_numbers[j])
1841 edit->line_offsets[i] = edit_move_forward (edit, edit->line_offsets[j], line - edit->line_numbers[j], 0);
1842 else
1843 edit->line_offsets[i] = edit_move_backward (edit, edit->line_offsets[j], edit->line_numbers[j] - line);
1844 edit->line_numbers[i] = line;
1845 return edit->line_offsets[i];
1848 int line_is_blank (WEdit * edit, long line)
1850 return is_blank (edit, edit_find_line (edit, line));
1853 /* moves up until a blank line is reached, or until just
1854 before a non-blank line is reached */
1855 static void edit_move_up_paragraph (WEdit * edit, int scroll)
1857 int i;
1858 if (edit->curs_line <= 1) {
1859 i = 0;
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)) {
1865 i++;
1866 break;
1868 } else {
1869 for (i = edit->curs_line - 1; i; i--)
1870 if (line_is_blank (edit, i))
1871 break;
1873 } else {
1874 for (i = edit->curs_line - 1; i; i--)
1875 if (line_is_blank (edit, i))
1876 break;
1879 edit_move_up (edit, edit->curs_line - i, scroll);
1882 /* move i lines */
1883 void edit_move_down (WEdit * edit, int i, int scroll)
1885 long p, l = edit->total_lines - edit->curs_line;
1886 if (i > l)
1887 i = l;
1888 if (i) {
1889 if (i > 1)
1890 edit->force |= REDRAW_PAGE;
1891 if (scroll)
1892 edit_scroll_downward (edit, i);
1893 p = edit_bol (edit, edit->curs1);
1894 edit_cursor_move (edit, (p = edit_move_forward (edit, p, i, 0)) - edit->curs1);
1895 edit_move_to_prev_col (edit, p);
1897 edit->search_start = edit->curs1;
1898 edit->found_len = 0;
1902 /* moves down until a blank line is reached, or until just
1903 before a non-blank line is reached */
1904 static void edit_move_down_paragraph (WEdit * edit, int scroll)
1906 int i;
1907 if (edit->curs_line >= edit->total_lines - 1) {
1908 i = edit->total_lines;
1909 } else {
1910 if (line_is_blank (edit, edit->curs_line)) {
1911 if (line_is_blank (edit, edit->curs_line + 1)) {
1912 for (i = edit->curs_line + 1; i; i++)
1913 if (!line_is_blank (edit, i) || i > edit->total_lines) {
1914 i--;
1915 break;
1917 } else {
1918 for (i = edit->curs_line + 1; i; i++)
1919 if (line_is_blank (edit, i) || i >= edit->total_lines)
1920 break;
1922 } else {
1923 for (i = edit->curs_line + 1; i; i++)
1924 if (line_is_blank (edit, i) || i >= edit->total_lines)
1925 break;
1928 edit_move_down (edit, i - edit->curs_line, scroll);
1931 static void edit_begin_page (WEdit *edit)
1933 edit_update_curs_row (edit);
1934 edit_move_up (edit, edit->curs_row, 0);
1937 static void edit_end_page (WEdit *edit)
1939 edit_update_curs_row (edit);
1940 edit_move_down (edit, edit->num_widget_lines - edit->curs_row - 1, 0);
1944 /* goto beginning of text */
1945 static void edit_move_to_top (WEdit * edit)
1947 if (edit->curs_line) {
1948 edit_cursor_move (edit, -edit->curs1);
1949 edit_move_to_prev_col (edit, 0);
1950 edit->force |= REDRAW_PAGE;
1951 edit->search_start = 0;
1952 edit_update_curs_row(edit);
1957 /* goto end of text */
1958 static void edit_move_to_bottom (WEdit * edit)
1960 if (edit->curs_line < edit->total_lines) {
1961 edit_cursor_move (edit, edit->curs2);
1962 edit->start_display = edit->last_byte;
1963 edit->start_line = edit->total_lines;
1964 edit_update_curs_row(edit);
1965 edit_scroll_upward (edit, edit->num_widget_lines - 1);
1966 edit->force |= REDRAW_PAGE;
1970 /* goto beginning of line */
1971 static void edit_cursor_to_bol (WEdit * edit)
1973 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
1974 edit->search_start = edit->curs1;
1975 edit->prev_col = edit_get_col (edit);
1976 edit->over_col = 0;
1979 /* goto end of line */
1980 static void edit_cursor_to_eol (WEdit * edit)
1982 edit_cursor_move (edit, edit_eol (edit, edit->curs1) - edit->curs1);
1983 edit->search_start = edit->curs1;
1984 edit->prev_col = edit_get_col (edit);
1985 edit->over_col = 0;
1988 /* move cursor to line 'line' */
1989 void edit_move_to_line (WEdit * e, long line)
1991 if(line < e->curs_line)
1992 edit_move_up (e, e->curs_line - line, 0);
1993 else
1994 edit_move_down (e, line - e->curs_line, 0);
1995 edit_scroll_screen_over_cursor (e);
1998 /* scroll window so that first visible line is 'line' */
1999 void edit_move_display (WEdit * e, long line)
2001 if(line < e->start_line)
2002 edit_scroll_upward (e, e->start_line - line);
2003 else
2004 edit_scroll_downward (e, line - e->start_line);
2007 /* save markers onto undo stack */
2008 void edit_push_markers (WEdit * edit)
2010 edit_push_action (edit, MARK_1 + edit->mark1);
2011 edit_push_action (edit, MARK_2 + edit->mark2);
2014 void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2)
2016 edit->mark1 = m1;
2017 edit->mark2 = m2;
2018 edit->column1 = c1;
2019 edit->column2 = c2;
2023 /* highlight marker toggle */
2024 void edit_mark_cmd (WEdit * edit, int unmark)
2026 edit_push_markers (edit);
2027 if (unmark) {
2028 edit_set_markers (edit, 0, 0, 0, 0);
2029 edit->force |= REDRAW_PAGE;
2030 } else {
2031 if (edit->mark2 >= 0) {
2032 edit_set_markers (edit, edit->curs1, -1, edit->curs_col+edit->over_col, edit->curs_col+edit->over_col);
2033 edit->force |= REDRAW_PAGE;
2034 } else
2035 edit_set_markers (edit, edit->mark1, edit->curs1, edit->column1, edit->curs_col+edit->over_col);
2039 static unsigned long
2040 my_type_of (int c)
2042 int x, r = 0;
2043 const char *p, *q;
2044 const char option_chars_move_whole_word[] =
2045 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2047 if (!c)
2048 return 0;
2049 if (c == '!') {
2050 if (*option_chars_move_whole_word == '!')
2051 return 2;
2052 return 0x80000000UL;
2054 if (g_ascii_isupper ((gchar) c))
2055 c = 'A';
2056 else if (g_ascii_islower ((gchar) c))
2057 c = 'a';
2058 else if (g_ascii_isalpha (c))
2059 c = 'a';
2060 else if (isdigit (c))
2061 c = '0';
2062 else if (isspace (c))
2063 c = ' ';
2064 q = strchr (option_chars_move_whole_word, c);
2065 if (!q)
2066 return 0xFFFFFFFFUL;
2067 do {
2068 for (x = 1, p = option_chars_move_whole_word; p < q; p++)
2069 if (*p == '!')
2070 x <<= 1;
2071 r |= x;
2072 } while ((q = strchr (q + 1, c)));
2073 return r;
2076 static void
2077 edit_left_word_move (WEdit *edit, int s)
2079 for (;;) {
2080 int c1, c2;
2081 edit_cursor_move (edit, -1);
2082 if (!edit->curs1)
2083 break;
2084 c1 = edit_get_byte (edit, edit->curs1 - 1);
2085 c2 = edit_get_byte (edit, edit->curs1);
2086 if (!(my_type_of (c1) & my_type_of (c2)))
2087 break;
2088 if (isspace (c1) && !isspace (c2))
2089 break;
2090 if (s)
2091 if (!isspace (c1) && isspace (c2))
2092 break;
2096 static void edit_left_word_move_cmd (WEdit * edit)
2098 edit_left_word_move (edit, 0);
2099 edit->force |= REDRAW_PAGE;
2102 static void
2103 edit_right_word_move (WEdit *edit, int s)
2105 for (;;) {
2106 int c1, c2;
2107 edit_cursor_move (edit, 1);
2108 if (edit->curs1 >= edit->last_byte)
2109 break;
2110 c1 = edit_get_byte (edit, edit->curs1 - 1);
2111 c2 = edit_get_byte (edit, edit->curs1);
2112 if (!(my_type_of (c1) & my_type_of (c2)))
2113 break;
2114 if (isspace (c1) && !isspace (c2))
2115 break;
2116 if (s)
2117 if (!isspace (c1) && isspace (c2))
2118 break;
2122 static void edit_right_word_move_cmd (WEdit * edit)
2124 edit_right_word_move (edit, 0);
2125 edit->force |= REDRAW_PAGE;
2128 static void edit_right_char_move_cmd (WEdit * edit)
2130 int cw = 1;
2131 int c = 0;
2132 if ( edit->utf8 ) {
2133 c = edit_get_utf (edit, edit->curs1, &cw);
2134 if ( cw < 1 )
2135 cw = 1;
2136 } else {
2137 c = edit_get_byte (edit, edit->curs1);
2139 if (option_cursor_beyond_eol && c == '\n') {
2140 edit->over_col++;
2141 } else {
2142 edit_cursor_move (edit, cw);
2146 static void edit_left_char_move_cmd (WEdit * edit)
2148 int cw = 1;
2149 if ( edit->utf8 ) {
2150 edit_get_prev_utf (edit, edit->curs1, &cw);
2151 if ( cw < 1 )
2152 cw = 1;
2154 if (option_cursor_beyond_eol && edit->over_col > 0) {
2155 edit->over_col--;
2156 } else {
2157 edit_cursor_move (edit, -cw);
2162 static void edit_right_delete_word (WEdit * edit)
2164 int c1, c2;
2165 for (;;) {
2166 if (edit->curs1 >= edit->last_byte)
2167 break;
2168 c1 = edit_delete (edit, 1);
2169 c2 = edit_get_byte (edit, edit->curs1);
2170 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2171 break;
2172 if (!(my_type_of (c1) & my_type_of (c2)))
2173 break;
2177 static void edit_left_delete_word (WEdit * edit)
2179 int c1, c2;
2180 for (;;) {
2181 if (edit->curs1 <= 0)
2182 break;
2183 c1 = edit_backspace (edit, 1);
2184 c2 = edit_get_byte (edit, edit->curs1 - 1);
2185 if ((isspace (c1) == 0) != (isspace (c2) == 0))
2186 break;
2187 if (!(my_type_of (c1) & my_type_of (c2)))
2188 break;
2193 the start column position is not recorded, and hence does not
2194 undo as it happed. But who would notice.
2196 static void
2197 edit_do_undo (WEdit * edit)
2199 long ac;
2200 long count = 0;
2202 edit->stack_disable = 1; /* don't record undo's onto undo stack! */
2203 edit->over_col = 0;
2204 while ((ac = pop_action (edit)) < KEY_PRESS) {
2205 switch ((int) ac) {
2206 case STACK_BOTTOM:
2207 goto done_undo;
2208 case CURS_RIGHT:
2209 edit_cursor_move (edit, 1);
2210 break;
2211 case CURS_LEFT:
2212 edit_cursor_move (edit, -1);
2213 break;
2214 case BACKSPACE:
2215 edit_backspace (edit, 1);
2216 break;
2217 case DELCHAR:
2218 edit_delete (edit, 1);
2219 break;
2220 case COLUMN_ON:
2221 column_highlighting = 1;
2222 break;
2223 case COLUMN_OFF:
2224 column_highlighting = 0;
2225 break;
2227 if (ac >= 256 && ac < 512)
2228 edit_insert_ahead (edit, ac - 256);
2229 if (ac >= 0 && ac < 256)
2230 edit_insert (edit, ac);
2232 if (ac >= MARK_1 - 2 && ac < MARK_2 - 2) {
2233 edit->mark1 = ac - MARK_1;
2234 edit->column1 = edit_move_forward3 (edit, edit_bol (edit, edit->mark1), 0, edit->mark1);
2235 } else if (ac >= MARK_2 - 2 && ac < KEY_PRESS) {
2236 edit->mark2 = ac - MARK_2;
2237 edit->column2 = edit_move_forward3 (edit, edit_bol (edit, edit->mark2), 0, edit->mark2);
2239 if (count++)
2240 edit->force |= REDRAW_PAGE; /* more than one pop usually means something big */
2243 if (edit->start_display > ac - KEY_PRESS) {
2244 edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display);
2245 edit->force |= REDRAW_PAGE;
2246 } else if (edit->start_display < ac - KEY_PRESS) {
2247 edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS);
2248 edit->force |= REDRAW_PAGE;
2250 edit->start_display = ac - KEY_PRESS; /* see push and pop above */
2251 edit_update_curs_row (edit);
2253 done_undo:;
2254 edit->stack_disable = 0;
2257 static void edit_delete_to_line_end (WEdit * edit)
2259 while (edit_get_byte (edit, edit->curs1) != '\n') {
2260 if (!edit->curs2)
2261 break;
2262 edit_delete (edit, 1);
2266 static void edit_delete_to_line_begin (WEdit * edit)
2268 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2269 if (!edit->curs1)
2270 break;
2271 edit_backspace (edit, 1);
2275 void
2276 edit_delete_line (WEdit *edit)
2279 * Delete right part of the line.
2280 * Note that edit_get_byte() returns '\n' when byte position is
2281 * beyond EOF.
2283 while (edit_get_byte (edit, edit->curs1) != '\n') {
2284 (void) edit_delete (edit, 1);
2288 * Delete '\n' char.
2289 * Note that edit_delete() will not corrupt anything if called while
2290 * cursor position is EOF.
2292 (void) edit_delete (edit, 1);
2295 * Delete left part of the line.
2296 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2298 while (edit_get_byte (edit, edit->curs1 - 1) != '\n') {
2299 (void) edit_backspace (edit, 1);
2303 void insert_spaces_tab (WEdit * edit, int half)
2305 int i;
2306 edit_update_curs_col (edit);
2307 i = ((edit->curs_col / (option_tab_spacing * space_width / (half + 1))) + 1) * (option_tab_spacing * space_width / (half + 1)) - edit->curs_col;
2308 while (i > 0) {
2309 edit_insert (edit, ' ');
2310 i -= space_width;
2314 static int is_aligned_on_a_tab (WEdit * edit)
2316 edit_update_curs_col (edit);
2317 if ((edit->curs_col % (TAB_SIZE * space_width)) && edit->curs_col % (TAB_SIZE * space_width) != (HALF_TAB_SIZE * space_width))
2318 return 0; /* not alligned on a tab */
2319 return 1;
2322 static int right_of_four_spaces (WEdit *edit)
2324 int i, ch = 0;
2325 for (i = 1; i <= HALF_TAB_SIZE; i++)
2326 ch |= edit_get_byte (edit, edit->curs1 - i);
2327 if (ch == ' ')
2328 return is_aligned_on_a_tab (edit);
2329 return 0;
2332 static int left_of_four_spaces (WEdit *edit)
2334 int i, ch = 0;
2335 for (i = 0; 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 int edit_indent_width (WEdit * edit, long p)
2344 long q = p;
2345 while (strchr ("\t ", edit_get_byte (edit, q)) && q < edit->last_byte - 1) /* move to the end of the leading whitespace of the line */
2346 q++;
2347 return edit_move_forward3 (edit, p, 0, q); /* count the number of columns of indentation */
2350 void edit_insert_indent (WEdit * edit, int indent)
2352 if (!option_fill_tabs_with_spaces) {
2353 while (indent >= TAB_SIZE) {
2354 edit_insert (edit, '\t');
2355 indent -= TAB_SIZE;
2358 while (indent-- > 0)
2359 edit_insert (edit, ' ');
2362 static void
2363 edit_auto_indent (WEdit * edit)
2365 long p;
2366 char c;
2367 p = edit->curs1;
2368 /* use the previous line as a template */
2369 p = edit_move_backward (edit, p, 1);
2370 /* copy the leading whitespace of the line */
2371 for (;;) { /* no range check - the line _is_ \n-terminated */
2372 c = edit_get_byte (edit, p++);
2373 if (c != ' ' && c != '\t')
2374 break;
2375 edit_insert (edit, c);
2379 static void edit_double_newline (WEdit * edit)
2381 edit_insert (edit, '\n');
2382 if (edit_get_byte (edit, edit->curs1) == '\n')
2383 return;
2384 if (edit_get_byte (edit, edit->curs1 - 2) == '\n')
2385 return;
2386 edit->force |= REDRAW_PAGE;
2387 edit_insert (edit, '\n');
2390 static void edit_tab_cmd (WEdit * edit)
2392 int i;
2394 if (option_fake_half_tabs) {
2395 if (is_in_indent (edit)) {
2396 /*insert a half tab (usually four spaces) unless there is a
2397 half tab already behind, then delete it and insert a
2398 full tab. */
2399 if (!option_fill_tabs_with_spaces && right_of_four_spaces (edit)) {
2400 for (i = 1; i <= HALF_TAB_SIZE; i++)
2401 edit_backspace (edit, 1);
2402 edit_insert (edit, '\t');
2403 } else {
2404 insert_spaces_tab (edit, 1);
2406 return;
2409 if (option_fill_tabs_with_spaces) {
2410 insert_spaces_tab (edit, 0);
2411 } else {
2412 edit_insert (edit, '\t');
2414 return;
2417 static void check_and_wrap_line (WEdit * edit)
2419 int curs, c;
2420 if (!option_typewriter_wrap)
2421 return;
2422 edit_update_curs_col (edit);
2423 if (edit->curs_col < option_word_wrap_line_length)
2424 return;
2425 curs = edit->curs1;
2426 for (;;) {
2427 curs--;
2428 c = edit_get_byte (edit, curs);
2429 if (c == '\n' || curs <= 0) {
2430 edit_insert (edit, '\n');
2431 return;
2433 if (c == ' ' || c == '\t') {
2434 int current = edit->curs1;
2435 edit_cursor_move (edit, curs - edit->curs1 + 1);
2436 edit_insert (edit, '\n');
2437 edit_cursor_move (edit, current - edit->curs1 + 1);
2438 return;
2443 static void edit_execute_macro (WEdit *edit, struct macro macro[], int n);
2445 void edit_push_key_press (WEdit * edit)
2447 edit_push_action (edit, KEY_PRESS + edit->start_display);
2448 if (edit->mark2 == -1)
2449 edit_push_action (edit, MARK_1 + edit->mark1);
2452 /* this find the matching bracket in either direction, and sets edit->bracket */
2453 static long edit_get_bracket (WEdit * edit, int in_screen, unsigned long furthest_bracket_search)
2455 const char * const b = "{}{[][()(", *p;
2456 int i = 1, a, inc = -1, c, d, n = 0;
2457 unsigned long j = 0;
2458 long q;
2459 edit_update_curs_row (edit);
2460 c = edit_get_byte (edit, edit->curs1);
2461 p = strchr (b, c);
2462 /* no limit */
2463 if (!furthest_bracket_search)
2464 furthest_bracket_search--;
2465 /* not on a bracket at all */
2466 if (!p)
2467 return -1;
2468 /* the matching bracket */
2469 d = p[1];
2470 /* going left or right? */
2471 if (strchr ("{[(", c))
2472 inc = 1;
2473 for (q = edit->curs1 + inc;; q += inc) {
2474 /* out of buffer? */
2475 if (q >= edit->last_byte || q < 0)
2476 break;
2477 a = edit_get_byte (edit, q);
2478 /* don't want to eat CPU */
2479 if (j++ > furthest_bracket_search)
2480 break;
2481 /* out of screen? */
2482 if (in_screen) {
2483 if (q < edit->start_display)
2484 break;
2485 /* count lines if searching downward */
2486 if (inc > 0 && a == '\n')
2487 if (n++ >= edit->num_widget_lines - edit->curs_row) /* out of screen */
2488 break;
2490 /* count bracket depth */
2491 i += (a == c) - (a == d);
2492 /* return if bracket depth is zero */
2493 if (!i)
2494 return q;
2496 /* no match */
2497 return -1;
2500 static long last_bracket = -1;
2502 void edit_find_bracket (WEdit * edit)
2504 edit->bracket = edit_get_bracket (edit, 1, 10000);
2505 if (last_bracket != edit->bracket)
2506 edit->force |= REDRAW_PAGE;
2507 last_bracket = edit->bracket;
2510 static void edit_goto_matching_bracket (WEdit *edit)
2512 long q;
2513 q = edit_get_bracket (edit, 0, 0);
2514 if (q < 0)
2515 return;
2516 edit->bracket = edit->curs1;
2517 edit->force |= REDRAW_PAGE;
2518 edit_cursor_move (edit, q - edit->curs1);
2522 * This executes a command as though the user initiated it through a key
2523 * press. Callback with WIDGET_KEY as a message calls this after
2524 * translating the key press. This function can be used to pass any
2525 * command to the editor. Note that the screen wouldn't update
2526 * automatically. Either of command or char_for_insertion must be
2527 * passed as -1. Commands are executed, and char_for_insertion is
2528 * inserted at the cursor.
2530 void edit_execute_key_command (WEdit *edit, int command, int char_for_insertion)
2532 if (command == CK_Begin_Record_Macro) {
2533 edit->macro_i = 0;
2534 edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
2535 return;
2537 if (command == CK_End_Record_Macro && edit->macro_i != -1) {
2538 edit->force |= REDRAW_COMPLETELY;
2539 edit_save_macro_cmd (edit, edit->macro, edit->macro_i);
2540 edit->macro_i = -1;
2541 return;
2543 if (edit->macro_i >= 0 && edit->macro_i < MAX_MACRO_LENGTH - 1) {
2544 edit->macro[edit->macro_i].command = command;
2545 edit->macro[edit->macro_i++].ch = char_for_insertion;
2547 /* record the beginning of a set of editing actions initiated by a key press */
2548 if (command != CK_Undo && command != CK_Ext_Mode)
2549 edit_push_key_press (edit);
2551 edit_execute_cmd (edit, command, char_for_insertion);
2552 if (column_highlighting)
2553 edit->force |= REDRAW_PAGE;
2556 static const char * const shell_cmd[] = SHELL_COMMANDS_i;
2559 This executes a command at a lower level than macro recording.
2560 It also does not push a key_press onto the undo stack. This means
2561 that if it is called many times, a single undo command will undo
2562 all of them. It also does not check for the Undo command.
2564 void
2565 edit_execute_cmd (WEdit *edit, int command, int char_for_insertion)
2567 edit->force |= REDRAW_LINE;
2569 /* The next key press will unhighlight the found string, so update
2570 * the whole page */
2571 if (edit->found_len || column_highlighting)
2572 edit->force |= REDRAW_PAGE;
2574 if (command / 100 == 6) { /* a highlight command like shift-arrow */
2575 column_highlighting = 0;
2576 if (!edit->highlight
2577 || (edit->mark2 != -1 && edit->mark1 != edit->mark2)) {
2578 edit_mark_cmd (edit, 1); /* clear */
2579 edit_mark_cmd (edit, 0); /* marking on */
2581 edit->highlight = 1;
2582 } else { /* any other command */
2583 if (edit->highlight)
2584 edit_mark_cmd (edit, 0); /* clear */
2585 edit->highlight = 0;
2588 /* first check for undo */
2589 if (command == CK_Undo) {
2590 edit_do_undo (edit);
2591 edit->found_len = 0;
2592 edit->prev_col = edit_get_col (edit);
2593 edit->search_start = edit->curs1;
2594 return;
2597 /* An ordinary key press */
2598 if (char_for_insertion >= 0) {
2599 if (edit->overwrite) {
2600 if (edit_get_byte (edit, edit->curs1) != '\n')
2601 edit_delete (edit, 0);
2603 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2604 edit_insert_over (edit);
2605 #ifdef HAVE_CHARSET
2606 if ( char_for_insertion > 255 && utf8_display == 0 ) {
2607 unsigned char str[6 + 1];
2608 size_t i = 0;
2609 int res = g_unichar_to_utf8 (char_for_insertion, (char *)str);
2610 if ( res == 0 ) {
2611 str[0] = '.';
2612 str[1] = '\0';
2613 } else {
2614 str[res] = '\0';
2616 while ( str[i] != 0 && i<=6) {
2617 char_for_insertion = str[i];
2618 edit_insert (edit, char_for_insertion);
2619 i++;
2621 } else {
2622 #endif
2623 edit_insert (edit, char_for_insertion);
2624 #ifdef HAVE_CHARSET
2626 #endif
2627 if (option_auto_para_formatting) {
2628 format_paragraph (edit, 0);
2629 edit->force |= REDRAW_PAGE;
2630 } else
2631 check_and_wrap_line (edit);
2632 edit->found_len = 0;
2633 edit->prev_col = edit_get_col (edit);
2634 edit->search_start = edit->curs1;
2635 edit_find_bracket (edit);
2636 return;
2639 switch (command) {
2640 case CK_Begin_Page:
2641 case CK_End_Page:
2642 case CK_Begin_Page_Highlight:
2643 case CK_End_Page_Highlight:
2644 case CK_Word_Left:
2645 case CK_Word_Right:
2646 case CK_Up:
2647 case CK_Down:
2648 case CK_Left:
2649 case CK_Right:
2650 if ( edit->mark2 >= 0 ) {
2651 if ( !option_persistent_selections ) {
2652 if (column_highlighting)
2653 edit_push_action (edit, COLUMN_ON);
2654 column_highlighting = 0;
2655 edit_mark_cmd (edit, 1);
2660 switch (command) {
2661 case CK_Begin_Page:
2662 case CK_End_Page:
2663 case CK_Begin_Page_Highlight:
2664 case CK_End_Page_Highlight:
2665 case CK_Word_Left:
2666 case CK_Word_Right:
2667 case CK_Up:
2668 case CK_Down:
2669 case CK_Word_Left_Highlight:
2670 case CK_Word_Right_Highlight:
2671 case CK_Up_Highlight:
2672 case CK_Down_Highlight:
2673 case CK_Up_Alt_Highlight:
2674 case CK_Down_Alt_Highlight:
2675 if (edit->mark2 == -1)
2676 break; /*marking is following the cursor: may need to highlight a whole line */
2677 case CK_Left:
2678 case CK_Right:
2679 case CK_Left_Highlight:
2680 case CK_Right_Highlight:
2681 edit->force |= REDRAW_CHAR_ONLY;
2684 /* basic cursor key commands */
2685 switch (command) {
2686 case CK_BackSpace:
2687 /* if non persistent selection and text selected */
2688 if ( !option_persistent_selections ) {
2689 if ( edit->mark1 != edit->mark2 ) {
2690 edit_block_delete_cmd (edit);
2691 break;
2694 if ( option_cursor_beyond_eol && edit->over_col > 0 ) {
2695 edit->over_col--;
2696 break;
2698 if (option_backspace_through_tabs && is_in_indent (edit)) {
2699 while (edit_get_byte (edit, edit->curs1 - 1) != '\n'
2700 && edit->curs1 > 0)
2701 edit_backspace (edit, 1);
2702 break;
2703 } else {
2704 if (option_fake_half_tabs) {
2705 int i;
2706 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2707 for (i = 0; i < HALF_TAB_SIZE; i++)
2708 edit_backspace (edit, 1);
2709 break;
2713 edit_backspace (edit, 0);
2714 break;
2715 case CK_Delete:
2716 /* if non persistent selection and text selected */
2717 if ( !option_persistent_selections ) {
2718 if ( edit->mark1 != edit->mark2 ) {
2719 edit_block_delete_cmd (edit);
2720 break;
2724 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2725 edit_insert_over (edit);
2727 if (option_fake_half_tabs) {
2728 int i;
2729 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2730 for (i = 1; i <= HALF_TAB_SIZE; i++)
2731 edit_delete (edit, 1);
2732 break;
2735 edit_delete (edit, 0);
2736 break;
2737 case CK_Delete_Word_Left:
2738 edit->over_col = 0;
2739 edit_left_delete_word (edit);
2740 break;
2741 case CK_Delete_Word_Right:
2742 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2743 edit_insert_over (edit);
2745 edit_right_delete_word (edit);
2746 break;
2747 case CK_Delete_Line:
2748 edit_delete_line (edit);
2749 break;
2750 case CK_Delete_To_Line_End:
2751 edit_delete_to_line_end (edit);
2752 break;
2753 case CK_Delete_To_Line_Begin:
2754 edit_delete_to_line_begin (edit);
2755 break;
2756 case CK_Enter:
2757 edit->over_col = 0;
2758 if (option_auto_para_formatting) {
2759 edit_double_newline (edit);
2760 if (option_return_does_auto_indent)
2761 edit_auto_indent (edit);
2762 format_paragraph (edit, 0);
2763 } else {
2764 edit_insert (edit, '\n');
2765 if (option_return_does_auto_indent) {
2766 edit_auto_indent (edit);
2769 break;
2770 case CK_Return:
2771 edit_insert (edit, '\n');
2772 break;
2774 case CK_Page_Up_Alt_Highlight:
2775 column_highlighting = 1;
2776 case CK_Page_Up:
2777 case CK_Page_Up_Highlight:
2778 edit_move_up (edit, edit->num_widget_lines - 1, 1);
2779 break;
2780 case CK_Page_Down_Alt_Highlight:
2781 column_highlighting = 1;
2782 case CK_Page_Down:
2783 case CK_Page_Down_Highlight:
2784 edit_move_down (edit, edit->num_widget_lines - 1, 1);
2785 break;
2786 case CK_Left_Alt_Highlight:
2787 column_highlighting = 1;
2788 case CK_Left:
2789 case CK_Left_Highlight:
2790 if (option_fake_half_tabs) {
2791 if (is_in_indent (edit) && right_of_four_spaces (edit)) {
2792 if ( option_cursor_beyond_eol && edit->over_col > 0)
2793 edit->over_col--;
2794 else
2795 edit_cursor_move (edit, -HALF_TAB_SIZE);
2796 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2797 break;
2800 edit_left_char_move_cmd (edit);
2801 break;
2802 case CK_Right_Alt_Highlight:
2803 column_highlighting = 1;
2804 case CK_Right:
2805 case CK_Right_Highlight:
2806 if (option_fake_half_tabs) {
2807 if (is_in_indent (edit) && left_of_four_spaces (edit)) {
2808 edit_cursor_move (edit, HALF_TAB_SIZE);
2809 edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
2810 break;
2813 edit_right_char_move_cmd (edit);
2814 break;
2815 case CK_Begin_Page:
2816 case CK_Begin_Page_Highlight:
2817 edit_begin_page (edit);
2818 break;
2819 case CK_End_Page:
2820 case CK_End_Page_Highlight:
2821 edit_end_page (edit);
2822 break;
2823 case CK_Word_Left:
2824 case CK_Word_Left_Highlight:
2825 edit->over_col = 0;
2826 edit_left_word_move_cmd (edit);
2827 break;
2828 case CK_Word_Right:
2829 case CK_Word_Right_Highlight:
2830 edit->over_col = 0;
2831 edit_right_word_move_cmd (edit);
2832 break;
2833 case CK_Up_Alt_Highlight:
2834 column_highlighting = 1;
2835 case CK_Up:
2836 case CK_Up_Highlight:
2837 edit_move_up (edit, 1, 0);
2838 break;
2839 case CK_Down_Alt_Highlight:
2840 column_highlighting = 1;
2841 case CK_Down:
2842 case CK_Down_Highlight:
2843 edit_move_down (edit, 1, 0);
2844 break;
2845 case CK_Paragraph_Up_Alt_Highlight:
2846 column_highlighting = 1;
2847 case CK_Paragraph_Up:
2848 case CK_Paragraph_Up_Highlight:
2849 edit_move_up_paragraph (edit, 0);
2850 break;
2851 case CK_Paragraph_Down_Alt_Highlight:
2852 column_highlighting = 1;
2853 case CK_Paragraph_Down:
2854 case CK_Paragraph_Down_Highlight:
2855 edit_move_down_paragraph (edit, 0);
2856 break;
2857 case CK_Scroll_Up_Alt_Highlight:
2858 column_highlighting = 1;
2859 case CK_Scroll_Up:
2860 case CK_Scroll_Up_Highlight:
2861 edit_move_up (edit, 1, 1);
2862 break;
2863 case CK_Scroll_Down_Alt_Highlight:
2864 column_highlighting = 1;
2865 case CK_Scroll_Down:
2866 case CK_Scroll_Down_Highlight:
2867 edit_move_down (edit, 1, 1);
2868 break;
2869 case CK_Home:
2870 case CK_Home_Highlight:
2871 edit_cursor_to_bol (edit);
2872 break;
2873 case CK_End:
2874 case CK_End_Highlight:
2875 edit_cursor_to_eol (edit);
2876 break;
2877 case CK_Tab:
2878 /* if text marked shift block */
2879 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
2880 if (edit->mark2 < 0)
2881 edit_mark_cmd (edit, 0);
2882 edit_move_block_to_right (edit);
2883 } else {
2884 if ( option_cursor_beyond_eol )
2885 edit_insert_over (edit);
2886 edit_tab_cmd (edit);
2887 if (option_auto_para_formatting) {
2888 format_paragraph (edit, 0);
2889 edit->force |= REDRAW_PAGE;
2890 } else {
2891 check_and_wrap_line (edit);
2894 break;
2896 case CK_Toggle_Insert:
2897 edit->overwrite = (edit->overwrite == 0);
2898 break;
2900 case CK_Mark:
2901 if (edit->mark2 >= 0) {
2902 if (column_highlighting)
2903 edit_push_action (edit, COLUMN_ON);
2904 column_highlighting = 0;
2906 edit_mark_cmd (edit, 0);
2907 break;
2908 case CK_Column_Mark:
2909 if (!column_highlighting)
2910 edit_push_action (edit, COLUMN_OFF);
2911 column_highlighting = 1;
2912 edit_mark_cmd (edit, 0);
2913 break;
2914 case CK_Unmark:
2915 if (column_highlighting)
2916 edit_push_action (edit, COLUMN_ON);
2917 column_highlighting = 0;
2918 edit_mark_cmd (edit, 1);
2919 break;
2921 case CK_Toggle_Line_State:
2922 option_line_state = !option_line_state;
2923 if ( option_line_state ) {
2924 option_line_state_width = LINE_STATE_WIDTH;
2925 } else {
2926 option_line_state_width = 0;
2928 edit->force |= REDRAW_PAGE;
2929 break;
2931 case CK_Toggle_Bookmark:
2932 book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR);
2933 if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR))
2934 book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR);
2935 else
2936 book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR);
2937 break;
2938 case CK_Flush_Bookmarks:
2939 book_mark_flush (edit, BOOK_MARK_COLOR);
2940 book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
2941 edit->force |= REDRAW_PAGE;
2942 break;
2943 case CK_Next_Bookmark:
2944 if (edit->book_mark) {
2945 struct _book_mark *p;
2946 p = (struct _book_mark *) book_mark_find (edit,
2947 edit->curs_line);
2948 if (p->next) {
2949 p = p->next;
2950 if (p->line >= edit->start_line + edit->num_widget_lines
2951 || p->line < edit->start_line)
2952 edit_move_display (edit,
2953 p->line -
2954 edit->num_widget_lines / 2);
2955 edit_move_to_line (edit, p->line);
2958 break;
2959 case CK_Prev_Bookmark:
2960 if (edit->book_mark) {
2961 struct _book_mark *p;
2962 p = (struct _book_mark *) book_mark_find (edit,
2963 edit->curs_line);
2964 while (p->line == edit->curs_line)
2965 if (p->prev)
2966 p = p->prev;
2967 if (p->line >= 0) {
2968 if (p->line >= edit->start_line + edit->num_widget_lines
2969 || p->line < edit->start_line)
2970 edit_move_display (edit,
2971 p->line -
2972 edit->num_widget_lines / 2);
2973 edit_move_to_line (edit, p->line);
2976 break;
2978 case CK_Beginning_Of_Text:
2979 case CK_Beginning_Of_Text_Highlight:
2980 edit_move_to_top (edit);
2981 break;
2982 case CK_End_Of_Text:
2983 case CK_End_Of_Text_Highlight:
2984 edit_move_to_bottom (edit);
2985 break;
2987 case CK_Copy:
2988 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2989 edit_insert_over (edit);
2990 edit_block_copy_cmd (edit);
2991 break;
2992 case CK_Remove:
2993 edit_block_delete_cmd (edit);
2994 break;
2995 case CK_Move:
2996 if ( option_cursor_beyond_eol && edit->over_col > 0 )
2997 edit_insert_over (edit);
2998 edit_block_move_cmd (edit);
2999 break;
3001 case CK_Shift_Block_Left:
3002 if (edit->mark1 != edit->mark2)
3003 edit_move_block_to_left (edit);
3004 break;
3005 case CK_Shift_Block_Right:
3006 if (edit->mark1 != edit->mark2)
3007 edit_move_block_to_right (edit);
3008 break;
3009 case CK_XStore:
3010 edit_copy_to_X_buf_cmd (edit);
3011 break;
3012 case CK_XCut:
3013 edit_cut_to_X_buf_cmd (edit);
3014 break;
3015 case CK_XPaste:
3016 if ( option_cursor_beyond_eol && edit->over_col > 0 )
3017 edit_insert_over (edit);
3018 edit_paste_from_X_buf_cmd (edit);
3019 break;
3020 case CK_Selection_History:
3021 edit_paste_from_history (edit);
3022 break;
3024 case CK_Save_As:
3025 edit_save_as_cmd (edit);
3026 break;
3027 case CK_Save:
3028 edit_save_confirm_cmd (edit);
3029 break;
3030 case CK_Load:
3031 edit_load_cmd (edit, EDIT_FILE_COMMON);
3032 break;
3033 case CK_Save_Block:
3034 edit_save_block_cmd (edit);
3035 break;
3036 case CK_Insert_File:
3037 edit_insert_file_cmd (edit);
3038 break;
3040 case CK_Load_Prev_File:
3041 edit_load_back_cmd (edit);
3042 break;
3043 case CK_Load_Next_File:
3044 edit_load_forward_cmd (edit);
3045 break;
3047 case CK_Load_Syntax_File:
3048 edit_load_cmd (edit, EDIT_FILE_SYNTAX);
3049 break;
3050 case CK_Choose_Syntax:
3051 edit_syntax_dialog ();
3052 break;
3054 case CK_Load_Menu_File:
3055 edit_load_cmd (edit, EDIT_FILE_MENU);
3056 break;
3058 case CK_Toggle_Syntax:
3059 if ((option_syntax_highlighting ^= 1) == 1)
3060 edit_load_syntax (edit, NULL, option_syntax_type);
3061 edit->force |= REDRAW_PAGE;
3062 break;
3064 case CK_Toggle_Tab_TWS:
3065 enable_show_tabs_tws ^= 1;
3066 edit->force |= REDRAW_PAGE;
3067 break;
3069 case CK_Find:
3070 edit_search_cmd (edit, 0);
3071 break;
3072 case CK_Find_Again:
3073 edit_search_cmd (edit, 1);
3074 break;
3075 case CK_Replace:
3076 edit_replace_cmd (edit, 0);
3077 break;
3078 case CK_Replace_Again:
3079 edit_replace_cmd (edit, 1);
3080 break;
3081 case CK_Complete_Word:
3082 /* if text marked shift block */
3083 if ( edit->mark1 != edit->mark2 && !option_persistent_selections ) {
3084 edit_move_block_to_left (edit);
3085 } else {
3086 edit_complete_word_cmd (edit);
3088 break;
3089 case CK_Find_Definition:
3090 edit_get_match_keyword_cmd (edit);
3091 break;
3092 case CK_Quit:
3093 dlg_stop (edit->widget.parent);
3094 break;
3095 case CK_New:
3096 edit_new_cmd (edit);
3097 break;
3098 case CK_Help:
3099 edit_help_cmd (edit);
3100 break;
3101 case CK_Refresh:
3102 edit_refresh_cmd (edit);
3103 break;
3104 case CK_SaveSetupCmd:
3105 save_setup_cmd ();
3106 break;
3107 case CK_About:
3108 query_dialog (_(" About "),
3109 _("\n Cooledit v3.11.5\n\n"
3110 " Copyright (C) 1996 the Free Software Foundation\n\n"
3111 " A user friendly text editor written\n"
3112 " for the Midnight Commander.\n"), D_NORMAL,
3113 1, _("&OK"));
3114 break;
3115 case CK_LearnKeys:
3116 learn_keys ();
3117 break;
3118 case CK_Edit_Options:
3119 edit_options_dialog ();
3120 break;
3121 case CK_Edit_Save_Mode:
3122 menu_save_mode_cmd ();
3123 break;
3124 case CK_Date:
3126 char s[BUF_MEDIUM];
3127 /* fool gcc to prevent a Y2K warning */
3128 char time_format[] = "_c";
3129 time_format[0] = '%';
3131 FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format);
3132 edit_print_string (edit, s);
3133 edit->force |= REDRAW_PAGE;
3134 break;
3136 break;
3137 case CK_Goto:
3138 edit_goto_cmd (edit);
3139 break;
3140 case CK_Paragraph_Format:
3141 format_paragraph (edit, 1);
3142 edit->force |= REDRAW_PAGE;
3143 break;
3144 case CK_Delete_Macro:
3145 edit_delete_macro_cmd (edit);
3146 break;
3147 case CK_Match_Bracket:
3148 edit_goto_matching_bracket (edit);
3149 break;
3150 case CK_User_Menu:
3151 user_menu (edit);
3152 break;
3153 case CK_Sort:
3154 edit_sort_cmd (edit);
3155 break;
3156 case CK_ExtCmd:
3157 edit_ext_cmd (edit);
3158 break;
3159 case CK_Mail:
3160 edit_mail_dialog (edit);
3161 break;
3162 case CK_Shell:
3163 view_other_cmd ();
3164 break;
3165 case CK_SelectCodepage:
3166 edit_select_codepage_cmd (edit);
3167 break;
3168 case CK_Insert_Literal:
3169 edit_insert_literal_cmd (edit);
3170 break;
3171 case CK_Execute_Macro:
3172 edit_execute_macro_cmd (edit);
3173 break;
3174 case CK_Begin_End_Macro:
3175 edit_begin_end_macro_cmd (edit);
3176 break;
3177 case CK_Ext_Mode:
3178 edit->extmod = 1;
3179 break;
3180 default:
3181 break;
3184 /* CK_Pipe_Block */
3185 if ((command / 1000) == 1) /* a shell command */
3186 edit_block_process_cmd (edit, shell_cmd[command - 1000], 1);
3187 if (command > CK_Macro (0) && command <= CK_Last_Macro) { /* a macro command */
3188 struct macro m[MAX_MACRO_LENGTH];
3189 int nm;
3190 if (edit_load_macro_cmd (edit, m, &nm, command - 2000))
3191 edit_execute_macro (edit, m, nm);
3194 /* keys which must set the col position, and the search vars */
3195 switch (command) {
3196 case CK_Find:
3197 case CK_Find_Again:
3198 case CK_Replace:
3199 case CK_Replace_Again:
3200 case CK_Complete_Word:
3201 edit->prev_col = edit_get_col (edit);
3202 break;
3203 case CK_Up:
3204 case CK_Up_Highlight:
3205 case CK_Up_Alt_Highlight:
3206 case CK_Down:
3207 case CK_Down_Highlight:
3208 case CK_Down_Alt_Highlight:
3209 case CK_Page_Up:
3210 case CK_Page_Up_Highlight:
3211 case CK_Page_Up_Alt_Highlight:
3212 case CK_Page_Down:
3213 case CK_Page_Down_Highlight:
3214 case CK_Page_Down_Alt_Highlight:
3215 case CK_Beginning_Of_Text:
3216 case CK_Beginning_Of_Text_Highlight:
3217 case CK_End_Of_Text:
3218 case CK_End_Of_Text_Highlight:
3219 case CK_Paragraph_Up:
3220 case CK_Paragraph_Up_Highlight:
3221 case CK_Paragraph_Up_Alt_Highlight:
3222 case CK_Paragraph_Down:
3223 case CK_Paragraph_Down_Highlight:
3224 case CK_Paragraph_Down_Alt_Highlight:
3225 case CK_Scroll_Up:
3226 case CK_Scroll_Up_Highlight:
3227 case CK_Scroll_Up_Alt_Highlight:
3228 case CK_Scroll_Down:
3229 case CK_Scroll_Down_Highlight:
3230 case CK_Scroll_Down_Alt_Highlight:
3231 edit->search_start = edit->curs1;
3232 edit->found_len = 0;
3233 break;
3234 default:
3235 edit->found_len = 0;
3236 edit->prev_col = edit_get_col (edit);
3237 edit->search_start = edit->curs1;
3239 edit_find_bracket (edit);
3241 if (option_auto_para_formatting) {
3242 switch (command) {
3243 case CK_BackSpace:
3244 case CK_Delete:
3245 case CK_Delete_Word_Left:
3246 case CK_Delete_Word_Right:
3247 case CK_Delete_To_Line_End:
3248 case CK_Delete_To_Line_Begin:
3249 format_paragraph (edit, 0);
3250 edit->force |= REDRAW_PAGE;
3256 static void
3257 edit_execute_macro (WEdit *edit, struct macro macro[], int n)
3259 int i = 0;
3261 if (edit->macro_depth++ > 256) {
3262 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3263 edit->macro_depth--;
3264 return;
3266 edit->force |= REDRAW_PAGE;
3267 for (; i < n; i++) {
3268 edit_execute_cmd (edit, macro[i].command, macro[i].ch);
3270 edit_update_screen (edit);
3271 edit->macro_depth--;
3274 /* User edit menu, like user menu (F2) but only in editor. */
3275 static void
3276 user_menu (WEdit * edit)
3278 FILE *fd;
3279 int nomark;
3280 struct stat status;
3281 long start_mark, end_mark;
3282 char *block_file = concat_dir_and_file (home_dir, EDIT_BLOCK_FILE);
3283 int rc = 0;
3285 nomark = eval_marks (edit, &start_mark, &end_mark);
3286 if (!nomark) /* remember marked or not */
3287 edit_save_block (edit, block_file, start_mark, end_mark);
3289 /* run shell scripts from menu */
3290 user_menu_cmd (edit);
3292 if (mc_stat (block_file, &status) != 0 || !status.st_size) {
3293 /* no block messages */
3294 goto cleanup;
3297 if (!nomark) {
3298 /* i.e. we have marked block */
3299 rc = edit_block_delete_cmd (edit);
3302 if (!rc) {
3303 edit_insert_file (edit, block_file);
3306 /* truncate block file */
3307 if ((fd = fopen (block_file, "w"))) {
3308 fclose (fd);
3311 edit_refresh_cmd (edit);
3312 edit->force |= REDRAW_COMPLETELY;
3314 cleanup:
3315 g_free (block_file);
3318 void
3319 edit_stack_init (void)
3321 for (edit_stack_iterator = 0;
3322 edit_stack_iterator < MAX_HISTORY_MOVETO;
3323 edit_stack_iterator++ ) {
3324 edit_history_moveto[edit_stack_iterator].filename = NULL;
3325 edit_history_moveto[edit_stack_iterator].line = -1;
3328 edit_stack_iterator = 0;
3331 void
3332 edit_stack_free (void)
3334 for (edit_stack_iterator = 0;
3335 edit_stack_iterator < MAX_HISTORY_MOVETO;
3336 edit_stack_iterator++)
3337 g_free (edit_history_moveto[edit_stack_iterator].filename);