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
25 * \brief Source: editor low level data handling and cursor fundamentals
33 #include <sys/types.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"
60 #include "src/keybind.h"
62 #include "edit-impl.h"
64 #include "edit-widget.h"
67 int option_word_wrap_line_length
= 72;
68 int option_typewriter_wrap
= 0;
69 int option_auto_para_formatting
= 0;
70 int option_fill_tabs_with_spaces
= 0;
71 int option_return_does_auto_indent
= 1;
72 int option_backspace_through_tabs
= 0;
73 int option_fake_half_tabs
= 1;
74 int option_save_mode
= EDIT_QUICK_SAVE
;
75 int option_save_position
= 1;
76 int option_max_undo
= 32768;
77 int option_persistent_selections
= 1;
78 int option_cursor_beyond_eol
= 0;
79 int option_line_state
= 0;
80 int option_line_state_width
= 0;
82 int option_edit_right_extreme
= 0;
83 int option_edit_left_extreme
= 0;
84 int option_edit_top_extreme
= 0;
85 int option_edit_bottom_extreme
= 0;
86 int enable_show_tabs_tws
= 1;
87 int option_check_nl_at_eof
= 0;
88 int show_right_margin
= 0;
90 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
91 char *option_backup_ext
= NULL
;
93 int edit_stack_iterator
= 0;
94 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
95 /* magic sequense for say than block is vertical */
96 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
100 * here's a quick sketch of the layout: (don't run this through indent.)
102 * (b1 is buffers1 and b2 is buffers2)
105 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
106 * ______________________________________|______________________________________
108 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
109 * |-> |-> |-> |-> |-> |-> |
111 * _<------------------------->|<----------------->_
112 * WEdit->curs2 | WEdit->curs1
117 * file end|||file beginning
126 static void user_menu (WEdit
* edit
);
129 edit_get_byte (WEdit
* edit
, long byte_index
)
132 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
135 if (byte_index
>= edit
->curs1
)
137 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
138 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
142 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
147 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
150 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
153 if (byte_index
>= edit
->curs1
)
155 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
156 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
157 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
161 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
162 (byte_index
& M_EDIT_BUF_SIZE
));
167 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
171 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
177 if (byte_index
>= edit
->curs1
)
179 p
= edit
->curs1
+ edit
->curs2
- 1;
180 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
181 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
185 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
190 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
195 gchar
*next_ch
= NULL
;
198 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
204 str
= edit_get_byte_ptr (edit
, byte_index
);
212 res
= g_utf8_get_char_validated (str
, -1);
222 /* Calculate UTF-8 char width */
223 next_ch
= g_utf8_next_char (str
);
226 width
= next_ch
- str
;
239 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
241 gchar
*str
, *buf
= NULL
;
244 gchar
*next_ch
= NULL
;
250 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
256 ch
= edit_get_utf (edit
, byte_index
, &width
);
264 str
= edit_get_byte_ptr (edit
, byte_index
);
265 buf
= edit_get_buf_ptr (edit
, byte_index
);
266 if (str
== NULL
|| buf
== NULL
)
271 /* get prev utf8 char */
273 str
= g_utf8_find_prev_char (buf
, str
);
275 res
= g_utf8_get_char_validated (str
, -1);
285 /* Calculate UTF-8 char width */
286 next_ch
= g_utf8_next_char (str
);
289 width
= next_ch
- str
;
302 * Initialize the buffers for an empty files.
305 edit_init_buffers (WEdit
* edit
)
309 for (j
= 0; j
<= MAXBUFF
; j
++)
311 edit
->buffers1
[j
] = NULL
;
312 edit
->buffers2
[j
] = NULL
;
317 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
321 * Load file OR text into buffers. Set cursor to the beginning of file.
325 edit_load_file_fast (WEdit
* edit
, const char *filename
)
331 edit
->curs2
= edit
->last_byte
;
332 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
334 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
337 GString
*errmsg
= g_string_new (NULL
);
338 g_string_sprintf (errmsg
, _(" Cannot open %s for reading "), filename
);
339 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
340 g_string_free (errmsg
, TRUE
);
344 if (!edit
->buffers2
[buf2
])
345 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
350 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
351 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
354 for (buf
= buf2
- 1; buf
>= 0; buf
--)
356 /* edit->buffers2[0] is already allocated */
357 if (!edit
->buffers2
[buf
])
358 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
359 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
367 char *err_str
= g_strdup_printf (_(" Error reading %s "), filename
);
368 edit_error_dialog (_("Error"), err_str
);
375 /* detecting an error on save is easy: just check if every byte has been written. */
376 /* detecting an error on read, is not so easy 'cos there is not way to tell
377 whether you read everything or not. */
378 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
379 static const struct edit_filters
381 const char *read
, *write
, *extension
;
385 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
386 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
387 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
388 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
389 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
393 /* Return index of the filter or -1 is there is no appropriate filter */
395 edit_find_filter (const char *filename
)
399 if (filename
== NULL
)
402 l
= strlen (filename
);
403 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
405 e
= strlen (all_filters
[i
].extension
);
407 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
414 edit_get_filter (const char *filename
)
417 char *p
, *quoted_name
;
419 i
= edit_find_filter (filename
);
423 quoted_name
= name_quote (filename
, 0);
424 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
425 g_free (quoted_name
);
430 edit_get_write_filter (const char *write_name
, const char *filename
)
435 i
= edit_find_filter (filename
);
439 writename
= name_quote (write_name
, 0);
440 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
446 edit_insert_stream (WEdit
* edit
, FILE * f
)
450 while ((c
= fgetc (f
)) >= 0)
452 edit_insert (edit
, c
);
459 edit_write_stream (WEdit
* edit
, FILE * f
)
463 if (edit
->lb
== LB_ASIS
)
465 for (i
= 0; i
< edit
->last_byte
; i
++)
466 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
471 /* change line breaks */
472 for (i
= 0; i
< edit
->last_byte
; i
++)
474 unsigned char c
= edit_get_byte (edit
, i
);
476 if (!(c
== '\n' || c
== '\r'))
479 if (fputc (c
, f
) < 0)
483 { /* (c == '\n' || c == '\r') */
484 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
488 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
489 /* put one line break unconditionally */
490 if (fputc ('\n', f
) < 0)
493 i
++; /* 2 chars are processed */
495 if (c
== '\r' && c1
== '\n')
496 /* Windows line break; go to the next char */
499 if (c
== '\r' && c1
== '\r')
501 /* two Macintosh line breaks; put second line break */
502 if (fputc ('\n', f
) < 0)
507 if (fputc (c1
, f
) < 0)
511 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
512 /* put one line break unconditionally */
513 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
516 if (c
== '\r' && c1
== '\n')
517 /* Windows line break; go to the next char */
521 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
522 /* put one line break unconditionally */
523 if (fputc ('\r', f
) < 0)
526 i
++; /* 2 chars are processed */
528 if (c
== '\r' && c1
== '\n')
529 /* Windows line break; go to the next char */
532 if (c
== '\n' && c1
== '\n')
534 /* two Windows line breaks; put second line break */
535 if (fputc ('\r', f
) < 0)
540 if (fputc (c1
, f
) < 0)
543 case LB_ASIS
: /* default without changes */
549 return edit
->last_byte
;
552 #define TEMP_BUF_LEN 1024
554 /* inserts a file at the cursor, returns 1 on success */
556 edit_insert_file (WEdit
* edit
, const char *filename
)
560 p
= edit_get_filter (filename
);
564 long current
= edit
->curs1
;
565 f
= (FILE *) popen (p
, "r");
568 edit_insert_stream (edit
, f
);
569 edit_cursor_move (edit
, current
- edit
->curs1
);
573 errmsg
= g_strdup_printf (_(" Error reading from pipe: %s "), p
);
574 edit_error_dialog (_("Error"), errmsg
);
583 errmsg
= g_strdup_printf (_(" Cannot open pipe for reading: %s "), p
);
584 edit_error_dialog (_("Error"), errmsg
);
593 int i
, file
, blocklen
;
594 long current
= edit
->curs1
;
595 int vertical_insertion
= 0;
597 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
600 buf
= g_malloc0 (TEMP_BUF_LEN
);
601 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
604 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
605 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
606 vertical_insertion
= 1;
608 mc_lseek (file
, 0, SEEK_SET
);
610 if (vertical_insertion
)
611 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
613 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
614 for (i
= 0; i
< blocklen
; i
++)
615 edit_insert (edit
, buf
[i
]);
616 edit_cursor_move (edit
, current
- edit
->curs1
);
625 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
627 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
630 GString
*errmsg
= (GString
*) 0;
632 /* Try opening an existing file */
633 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
638 * Try creating the file. O_EXCL prevents following broken links
639 * and opening existing files.
641 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
644 g_string_sprintf (errmsg
= g_string_new (NULL
),
645 _(" Cannot open %s for reading "), filename
);
650 /* New file, delete it if it's not modified or saved */
651 edit
->delete_file
= 1;
655 /* Check what we have opened */
656 if (mc_fstat (file
, st
) < 0)
658 g_string_sprintf (errmsg
= g_string_new (NULL
),
659 _(" Cannot get size/permissions for %s "), filename
);
663 /* We want to open regular files only */
664 if (!S_ISREG (st
->st_mode
))
666 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" %s is not a regular file "), filename
);
671 * Don't delete non-empty files.
672 * O_EXCL should prevent it, but let's be on the safe side.
676 edit
->delete_file
= 0;
679 if (st
->st_size
>= SIZE_LIMIT
)
681 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" File %s is too large "), filename
);
685 (void) mc_close (file
);
688 edit_error_dialog (_("Error"), errmsg
->str
);
689 g_string_free (errmsg
, TRUE
);
696 * Open the file and load it into the buffers, either directly or using
697 * a filter. Return 0 on success, 1 on error.
699 * Fast loading (edit_load_file_fast) is used when the file size is
700 * known. In this case the data is read into the buffers by blocks.
701 * If the file size is not known, the data is loaded byte by byte in
705 edit_load_file (WEdit
* edit
)
709 /* Cannot do fast load if a filter is used */
710 if (edit_find_filter (edit
->filename
) >= 0)
714 * VFS may report file size incorrectly, and slow load is not a big
715 * deal considering overhead in VFS.
717 if (!vfs_file_is_local (edit
->filename
))
721 * FIXME: line end translation should disable fast loading as well
722 * Consider doing fseek() to the end and ftell() for the real size.
727 /* If we are dealing with a real file, check that it exists */
728 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
733 /* nothing to load */
737 edit_init_buffers (edit
);
741 edit
->last_byte
= edit
->stat1
.st_size
;
742 edit_load_file_fast (edit
, edit
->filename
);
743 /* If fast load was used, the number of lines wasn't calculated */
744 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
749 const char *codepage_id
;
754 edit
->stack_disable
= 1;
755 if (!edit_insert_file (edit
, edit
->filename
))
760 edit
->stack_disable
= 0;
764 codepage_id
= get_codepage_id (source_codepage
);
766 edit
->utf8
= str_isutf8 (codepage_id
);
773 /* Restore saved cursor position in the file */
775 edit_load_position (WEdit
* edit
)
781 if (!edit
->filename
|| !*edit
->filename
)
784 filename
= vfs_canon (edit
->filename
);
785 load_file_position (filename
, &line
, &column
, &offset
);
790 edit_move_to_line (edit
, line
- 1);
791 edit
->prev_col
= column
;
795 edit_cursor_move (edit
, offset
);
796 line
= edit
->curs_line
;
798 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
799 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
802 /* Save cursor position in the file */
804 edit_save_position (WEdit
* edit
)
808 if (!edit
->filename
|| !*edit
->filename
)
811 filename
= vfs_canon (edit
->filename
);
812 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
816 /* Clean the WEdit stricture except the widget part */
818 edit_purge_widget (WEdit
* edit
)
820 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
821 char *start
= (char *) edit
+ sizeof (Widget
);
822 memset (start
, 0, len
);
823 edit
->macro_i
= -1; /* not recording a macro */
827 edit_set_keymap (void)
829 editor_map
= default_editor_keymap
;
830 if (editor_keymap
&& editor_keymap
->len
> 0)
831 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
833 editor_x_map
= default_editor_x_keymap
;
834 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
835 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
839 #define space_width 1
842 * Fill in the edit structure. Return NULL on failure. Pass edit as
843 * NULL to allocate a new structure.
845 * If line is 0, try to restore saved position. Otherwise put the
846 * cursor on that line and show it in the middle of the screen.
849 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
852 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
853 if (option_line_state
)
855 option_line_state_width
= LINE_STATE_WIDTH
;
859 option_line_state_width
= 0;
865 * Expand option_whole_chars_search by national letters using
869 static char option_whole_chars_search_buf
[256];
871 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
874 size_t len
= str_term_width1 (option_whole_chars_search
);
876 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
878 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
880 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
882 option_whole_chars_search_buf
[len
++] = i
;
886 option_whole_chars_search_buf
[len
] = 0;
887 option_whole_chars_search
= option_whole_chars_search_buf
;
889 #endif /* ENABLE_NLS */
890 edit
= g_malloc0 (sizeof (WEdit
));
894 edit_purge_widget (edit
);
895 edit
->num_widget_lines
= lines
;
897 edit
->num_widget_columns
= columns
;
898 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
899 edit
->stat1
.st_uid
= getuid ();
900 edit
->stat1
.st_gid
= getgid ();
901 edit
->stat1
.st_mtime
= 0;
903 edit
->force
|= REDRAW_PAGE
;
904 edit_set_filename (edit
, filename
);
905 edit
->stack_size
= START_STACK_SIZE
;
906 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
907 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
910 edit
->converter
= str_cnv_from_term
;
911 edit_set_codeset (edit
);
913 if (edit_load_file (edit
))
915 /* edit_load_file already gives an error message */
921 edit
->loading_done
= 1;
924 edit_load_syntax (edit
, NULL
, NULL
);
927 edit_get_syntax_color (edit
, -1, &color
);
930 /* load saved cursor position */
931 if ((line
== 0) && option_save_position
)
933 edit_load_position (edit
);
939 edit_move_display (edit
, line
- 1);
940 edit_move_to_line (edit
, line
- 1);
948 /* Clear the edit struct, freeing everything in it. Return 1 on success */
950 edit_clean (WEdit
* edit
)
957 /* a stale lock, remove it */
959 edit
->locked
= edit_unlock_file (edit
->filename
);
961 /* save cursor position */
962 if (option_save_position
)
963 edit_save_position (edit
);
965 /* File specified on the mcedit command line and never saved */
966 if (edit
->delete_file
)
967 unlink (edit
->filename
);
969 edit_free_syntax_rules (edit
);
970 book_mark_flush (edit
, -1);
971 for (; j
<= MAXBUFF
; j
++)
973 g_free (edit
->buffers1
[j
]);
974 g_free (edit
->buffers2
[j
]);
977 g_free (edit
->undo_stack
);
978 g_free (edit
->filename
);
981 mc_search_free (edit
->search
);
984 if (edit
->converter
!= str_cnv_from_term
)
985 str_close_conv (edit
->converter
);
987 edit_purge_widget (edit
);
992 /* returns 1 on success */
994 edit_renew (WEdit
* edit
)
996 int lines
= edit
->num_widget_lines
;
997 int columns
= edit
->num_widget_columns
;
1000 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
1004 * Load a new file into the editor. If it fails, preserve the old file.
1005 * To do it, allocate a new widget, initialize it and, if the new file
1006 * was loaded, copy the data to the old widget.
1007 * Return 1 on success, 0 on failure.
1010 edit_reload (WEdit
* edit
, const char *filename
)
1013 int lines
= edit
->num_widget_lines
;
1014 int columns
= edit
->num_widget_columns
;
1016 e
= g_malloc0 (sizeof (WEdit
));
1017 e
->widget
= edit
->widget
;
1018 if (!edit_init (e
, lines
, columns
, filename
, 0))
1024 memcpy (edit
, e
, sizeof (WEdit
));
1030 * Load a new file into the editor and set line. If it fails, preserve the old file.
1031 * To do it, allocate a new widget, initialize it and, if the new file
1032 * was loaded, copy the data to the old widget.
1033 * Return 1 on success, 0 on failure.
1036 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
1039 int lines
= edit
->num_widget_lines
;
1040 int columns
= edit
->num_widget_columns
;
1042 e
= g_malloc0 (sizeof (WEdit
));
1043 e
->widget
= edit
->widget
;
1044 if (!edit_init (e
, lines
, columns
, filename
, line
))
1050 memcpy (edit
, e
, sizeof (WEdit
));
1056 edit_set_codeset (WEdit
*edit
)
1061 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
1066 conv
= str_crt_conv_from (cp_id
);
1067 if (conv
!= INVALID_CONV
)
1069 if (edit
->converter
!= str_cnv_from_term
)
1070 str_close_conv (edit
->converter
);
1071 edit
->converter
= conv
;
1076 edit
->utf8
= str_isutf8 (cp_id
);
1084 Recording stack for undo:
1085 The following is an implementation of a compressed stack. Identical
1086 pushes are recorded by a negative prefix indicating the number of times the
1087 same char was pushed. This saves space for repeated curs-left or curs-right
1104 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1105 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1106 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1107 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1110 The only way the cursor moves or the buffer is changed is through the routines:
1111 insert, backspace, insert_ahead, delete, and cursor_move.
1112 These record the reverse undo movements onto the stack each time they are
1115 Each key press results in a set of actions (insert; delete ...). So each time
1116 a key is pressed the current position of start_display is pushed as
1117 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1118 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1119 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1124 edit_push_action (WEdit
* edit
, long c
, ...)
1126 unsigned long sp
= edit
->stack_pointer
;
1130 /* first enlarge the stack if necessary */
1131 if (sp
> edit
->stack_size
- 10)
1133 if (option_max_undo
< 256)
1134 option_max_undo
= 256;
1135 if (edit
->stack_size
< (unsigned long) option_max_undo
)
1137 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1140 edit
->undo_stack
= t
;
1141 edit
->stack_size
<<= 1;
1142 edit
->stack_size_mask
= edit
->stack_size
- 1;
1146 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1147 if (edit
->stack_disable
)
1150 #ifdef FAST_MOVE_CURSOR
1151 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
1154 edit
->undo_stack
[sp
] = (c
== CURS_LEFT_LOTS
) ? CURS_LEFT
: CURS_RIGHT
;
1155 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1157 c
= -(va_arg (ap
, int));
1161 #endif /* ! FAST_MOVE_CURSOR */
1162 if (edit
->stack_bottom
!= sp
1163 && spm1
!= edit
->stack_bottom
1164 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
1167 if (edit
->undo_stack
[spm1
] < 0)
1169 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1172 if (edit
->undo_stack
[spm1
] > -1000000000)
1174 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1175 edit
->undo_stack
[spm1
]--;
1179 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1180 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1181 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1182 { /* a left then a right anihilate each other */
1183 if (edit
->undo_stack
[spm1
] == -2)
1184 edit
->stack_pointer
= spm1
;
1186 edit
->undo_stack
[spm1
]++;
1193 d
= edit
->undo_stack
[spm1
];
1197 return; /* --> no need to push multiple do-nothings */
1198 edit
->undo_stack
[sp
] = -2;
1201 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1202 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1203 { /* a left then a right anihilate each other */
1204 edit
->stack_pointer
= spm1
;
1210 edit
->undo_stack
[sp
] = c
;
1213 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1215 /* if the sp wraps round and catches the stack_bottom then erase
1216 * the first set of actions on the stack to make space - by moving
1217 * stack_bottom forward one "key press" */
1218 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1219 if ((unsigned long) c
== edit
->stack_bottom
||
1220 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1223 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1225 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
1226 && edit
->stack_bottom
!= edit
->stack_pointer
);
1228 /*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: */
1229 if (edit
->stack_pointer
!= edit
->stack_bottom
1230 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1231 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1235 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1236 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1239 pop_action (WEdit
* edit
)
1242 unsigned long sp
= edit
->stack_pointer
;
1244 if (sp
== edit
->stack_bottom
)
1245 return STACK_BOTTOM
;
1247 sp
= (sp
- 1) & edit
->stack_size_mask
;
1248 c
= edit
->undo_stack
[sp
];
1251 /* edit->undo_stack[sp] = '@'; */
1252 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1256 if (sp
== edit
->stack_bottom
)
1257 return STACK_BOTTOM
;
1259 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1260 if (edit
->undo_stack
[sp
] == -2)
1262 /* edit->undo_stack[sp] = '@'; */
1263 edit
->stack_pointer
= sp
;
1266 edit
->undo_stack
[sp
]++;
1271 /* is called whenever a modification is made by one of the four routines below */
1273 edit_modification (WEdit
* edit
)
1275 edit
->caches_valid
= 0;
1276 edit
->screen_modified
= 1;
1278 /* raise lock when file modified */
1279 if (!edit
->modified
&& !edit
->delete_file
)
1280 edit
->locked
= edit_lock_file (edit
->filename
);
1285 Basic low level single character buffer alterations and movements at the cursor.
1286 Returns char passed over, inserted or removed.
1290 edit_insert (WEdit
* edit
, int c
)
1292 /* check if file has grown to large */
1293 if (edit
->last_byte
>= SIZE_LIMIT
)
1296 /* first we must update the position of the display window */
1297 if (edit
->curs1
< edit
->start_display
)
1299 edit
->start_display
++;
1304 /* Mark file as modified, unless the file hasn't been fully loaded */
1305 if (edit
->loading_done
)
1307 edit_modification (edit
);
1310 /* now we must update some info on the file and check if a redraw is required */
1313 if (edit
->book_mark
)
1314 book_mark_inc (edit
, edit
->curs_line
);
1316 edit
->total_lines
++;
1317 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1320 /* save the reverse command onto the undo stack */
1321 edit_push_action (edit
, BACKSPACE
);
1323 /* update markers */
1324 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1325 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1326 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1328 /* add a new buffer if we've reached the end of the last one */
1329 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1330 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1332 /* perform the insertion */
1333 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
1334 = (unsigned char) c
;
1336 /* update file length */
1339 /* update cursor position */
1344 edit_insert_over (WEdit
* edit
)
1348 for (i
= 0; i
< edit
->over_col
; i
++)
1350 edit_insert (edit
, ' ');
1355 /* same as edit_insert and move left */
1357 edit_insert_ahead (WEdit
* edit
, int c
)
1359 if (edit
->last_byte
>= SIZE_LIMIT
)
1362 if (edit
->curs1
< edit
->start_display
)
1364 edit
->start_display
++;
1368 edit_modification (edit
);
1371 if (edit
->book_mark
)
1372 book_mark_inc (edit
, edit
->curs_line
);
1373 edit
->total_lines
++;
1374 edit
->force
|= REDRAW_AFTER_CURSOR
;
1376 edit_push_action (edit
, DELCHAR
);
1378 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1379 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1380 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1382 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1383 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1384 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
1385 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1393 edit_delete (WEdit
* edit
, const int byte_delete
)
1402 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1403 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1404 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1407 /* if byte_delete = 1 then delete only one byte not multibyte char */
1408 if (edit
->utf8
&& byte_delete
== 0)
1410 edit_get_utf (edit
, edit
->curs1
, &cw
);
1414 for (i
= 1; i
<= cw
; i
++)
1416 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1418 1) & M_EDIT_BUF_SIZE
) - 1];
1420 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1422 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1423 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1427 edit_push_action (edit
, p
+ 256);
1430 edit_modification (edit
);
1433 if (edit
->book_mark
)
1434 book_mark_dec (edit
, edit
->curs_line
);
1435 edit
->total_lines
--;
1436 edit
->force
|= REDRAW_AFTER_CURSOR
;
1438 if (edit
->curs1
< edit
->start_display
)
1440 edit
->start_display
--;
1450 edit_backspace (WEdit
* edit
, const int byte_delete
)
1459 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1460 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1461 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1464 if (edit
->utf8
&& byte_delete
== 0)
1466 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1470 for (i
= 1; i
<= cw
; i
++)
1472 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
1473 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1474 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1476 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1477 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1481 edit_push_action (edit
, p
);
1483 edit_modification (edit
);
1486 if (edit
->book_mark
)
1487 book_mark_dec (edit
, edit
->curs_line
);
1489 edit
->total_lines
--;
1490 edit
->force
|= REDRAW_AFTER_CURSOR
;
1493 if (edit
->curs1
< edit
->start_display
)
1495 edit
->start_display
--;
1503 #ifdef FAST_MOVE_CURSOR
1506 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1509 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
1512 next
-= (unsigned long) dest
;
1520 edit_move_backward_lots (WEdit
* edit
, long increment
)
1523 unsigned char *p
= NULL
;
1525 if (increment
> edit
->curs1
)
1526 increment
= edit
->curs1
;
1529 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1531 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1534 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1539 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1540 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
1547 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1548 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1549 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1550 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1553 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1554 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1555 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1560 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1563 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1565 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1572 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1583 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1584 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
1589 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1590 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1593 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1595 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1596 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1601 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1604 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1606 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1611 return edit_get_byte (edit
, edit
->curs1
);
1614 #endif /* ! FAST_MOVE_CURSOR */
1616 /* moves the cursor right or left: increment positive or negative respectively */
1618 edit_cursor_move (WEdit
* edit
, long increment
)
1620 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1622 #ifdef FAST_MOVE_CURSOR
1623 if (increment
< -256)
1625 edit
->force
|= REDRAW_PAGE
;
1626 edit_move_backward_lots (edit
, -increment
);
1629 #endif /* ! FAST_MOVE_CURSOR */
1633 for (; increment
< 0; increment
++)
1638 edit_push_action (edit
, CURS_RIGHT
);
1640 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1641 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1642 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1643 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1644 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1646 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
1647 1) & M_EDIT_BUF_SIZE
];
1648 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1650 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1651 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1657 edit
->force
|= REDRAW_LINE_BELOW
;
1662 else if (increment
> 0)
1664 for (; increment
> 0; increment
--)
1669 edit_push_action (edit
, CURS_LEFT
);
1671 c
= edit_get_byte (edit
, edit
->curs1
);
1672 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1673 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1674 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1676 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1678 1) & M_EDIT_BUF_SIZE
) - 1];
1679 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1681 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1682 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1688 edit
->force
|= REDRAW_LINE_ABOVE
;
1694 /* These functions return positions relative to lines */
1696 /* returns index of last char on line + 1 */
1698 edit_eol (WEdit
* edit
, long current
)
1700 if (current
>= edit
->last_byte
)
1701 return edit
->last_byte
;
1704 if (edit_get_byte (edit
, current
) == '\n')
1709 /* returns index of first char on line */
1711 edit_bol (WEdit
* edit
, long current
)
1717 if (edit_get_byte (edit
, current
- 1) == '\n')
1724 edit_count_lines (WEdit
* edit
, long current
, long upto
)
1727 if (upto
> edit
->last_byte
)
1728 upto
= edit
->last_byte
;
1731 while (current
< upto
)
1732 if (edit_get_byte (edit
, current
++) == '\n')
1738 /* If lines is zero this returns the count of lines from current to upto. */
1739 /* If upto is zero returns index of lines forward current. */
1741 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1745 return edit_count_lines (edit
, current
, upto
);
1754 next
= edit_eol (edit
, current
) + 1;
1755 if (next
> edit
->last_byte
)
1765 /* Returns offset of 'lines' lines up from current */
1767 edit_move_backward (WEdit
* edit
, long current
, long lines
)
1771 current
= edit_bol (edit
, current
);
1772 while ((lines
--) && current
!= 0)
1773 current
= edit_bol (edit
, current
- 1);
1777 /* If cols is zero this returns the count of columns from current to upto. */
1778 /* If upto is zero returns index of cols across from current. */
1780 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1791 q
= edit
->last_byte
+ 2;
1793 for (col
= 0, p
= current
; p
< q
; p
++)
1807 orig_c
= c
= edit_get_byte (edit
, p
);
1811 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1816 if (g_unichar_iswide (utf_ch
))
1819 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1823 c
= convert_to_display_c (c
);
1825 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1833 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1834 /* '\r' is shown as ^M, so we must advance 2 characters */
1835 /* Caret notation for control characters */
1843 /* returns the current column position of the cursor */
1845 edit_get_col (WEdit
* edit
)
1847 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1851 /* Scrolling functions */
1854 edit_update_curs_row (WEdit
* edit
)
1856 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1860 edit_update_curs_col (WEdit
* edit
)
1862 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1866 edit_get_curs_col (const WEdit
* edit
)
1868 return edit
->curs_col
;
1871 /*moves the display start position up by i lines */
1873 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1875 unsigned long lines_above
= edit
->start_line
;
1876 if (i
> lines_above
)
1880 edit
->start_line
-= i
;
1881 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1882 edit
->force
|= REDRAW_PAGE
;
1883 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1885 edit_update_curs_row (edit
);
1889 /* returns 1 if could scroll, 0 otherwise */
1891 edit_scroll_downward (WEdit
* edit
, int i
)
1894 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1895 if (lines_below
> 0)
1897 if (i
> lines_below
)
1899 edit
->start_line
+= i
;
1900 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1901 edit
->force
|= REDRAW_PAGE
;
1902 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1904 edit_update_curs_row (edit
);
1908 edit_scroll_right (WEdit
* edit
, int i
)
1910 edit
->force
|= REDRAW_PAGE
;
1911 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1912 edit
->start_col
-= i
;
1916 edit_scroll_left (WEdit
* edit
, int i
)
1918 if (edit
->start_col
)
1920 edit
->start_col
+= i
;
1921 if (edit
->start_col
> 0)
1922 edit
->start_col
= 0;
1923 edit
->force
|= REDRAW_PAGE
;
1924 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1928 /* high level cursor movement commands */
1931 is_in_indent (WEdit
* edit
)
1933 long p
= edit_bol (edit
, edit
->curs1
);
1934 while (p
< edit
->curs1
)
1935 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1940 static int left_of_four_spaces (WEdit
* edit
);
1943 edit_move_to_prev_col (WEdit
* edit
, long p
)
1945 int prev
= edit
->prev_col
;
1946 int over
= edit
->over_col
;
1947 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1949 if (option_cursor_beyond_eol
)
1951 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
1952 edit_eol (edit
, edit
->curs1
));
1954 if (line_len
< prev
+ edit
->over_col
)
1956 edit
->over_col
= prev
+ over
- line_len
;
1957 edit
->prev_col
= line_len
;
1958 edit
->curs_col
= line_len
;
1962 edit
->curs_col
= prev
+ over
;
1963 edit
->prev_col
= edit
->curs_col
;
1970 if (is_in_indent (edit
) && option_fake_half_tabs
)
1972 edit_update_curs_col (edit
);
1974 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
1976 int q
= edit
->curs_col
;
1977 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1978 p
= edit_bol (edit
, edit
->curs1
);
1979 edit_cursor_move (edit
,
1980 edit_move_forward3 (edit
, p
, edit
->curs_col
,
1982 if (!left_of_four_spaces (edit
))
1983 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1990 is_blank (WEdit
* edit
, long offset
)
1994 s
= edit_bol (edit
, offset
);
1995 f
= edit_eol (edit
, offset
) - 1;
1998 c
= edit_get_byte (edit
, s
++);
2006 /* returns the offset of line i */
2008 edit_find_line (WEdit
* edit
, int line
)
2012 if (!edit
->caches_valid
)
2014 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2015 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
2016 /* three offsets that we *know* are line 0 at 0 and these two: */
2017 edit
->line_numbers
[1] = edit
->curs_line
;
2018 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
2019 edit
->line_numbers
[2] = edit
->total_lines
;
2020 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
2021 edit
->caches_valid
= 1;
2023 if (line
>= edit
->total_lines
)
2024 return edit
->line_offsets
[2];
2027 /* find the closest known point */
2028 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2031 n
= abs (edit
->line_numbers
[i
] - line
);
2039 return edit
->line_offsets
[j
]; /* know the offset exactly */
2040 if (m
== 1 && j
>= 3)
2041 i
= j
; /* one line different - caller might be looping, so stay in this cache */
2043 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
2044 if (line
> edit
->line_numbers
[j
])
2045 edit
->line_offsets
[i
] =
2046 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
2048 edit
->line_offsets
[i
] =
2049 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
2050 edit
->line_numbers
[i
] = line
;
2051 return edit
->line_offsets
[i
];
2055 line_is_blank (WEdit
* edit
, long line
)
2057 return is_blank (edit
, edit_find_line (edit
, line
));
2060 /* moves up until a blank line is reached, or until just
2061 before a non-blank line is reached */
2063 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
2066 if (edit
->curs_line
> 1)
2068 if (line_is_blank (edit
, edit
->curs_line
))
2070 if (line_is_blank (edit
, edit
->curs_line
- 1))
2072 for (i
= edit
->curs_line
- 1; i
; i
--)
2073 if (!line_is_blank (edit
, i
))
2081 for (i
= edit
->curs_line
- 1; i
; i
--)
2082 if (line_is_blank (edit
, i
))
2088 for (i
= edit
->curs_line
- 1; i
; i
--)
2089 if (line_is_blank (edit
, i
))
2093 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
2096 /* moves down until a blank line is reached, or until just
2097 before a non-blank line is reached */
2099 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
2102 if (edit
->curs_line
>= edit
->total_lines
- 1)
2104 i
= edit
->total_lines
;
2108 if (line_is_blank (edit
, edit
->curs_line
))
2110 if (line_is_blank (edit
, edit
->curs_line
+ 1))
2112 for (i
= edit
->curs_line
+ 1; i
; i
++)
2113 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
2121 for (i
= edit
->curs_line
+ 1; i
; i
++)
2122 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2128 for (i
= edit
->curs_line
+ 1; i
; i
++)
2129 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2133 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
2137 edit_begin_page (WEdit
* edit
)
2139 edit_update_curs_row (edit
);
2140 edit_move_up (edit
, edit
->curs_row
, 0);
2144 edit_end_page (WEdit
* edit
)
2146 edit_update_curs_row (edit
);
2147 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
2151 /* goto beginning of text */
2153 edit_move_to_top (WEdit
* edit
)
2155 if (edit
->curs_line
)
2157 edit_cursor_move (edit
, -edit
->curs1
);
2158 edit_move_to_prev_col (edit
, 0);
2159 edit
->force
|= REDRAW_PAGE
;
2160 edit
->search_start
= 0;
2161 edit_update_curs_row (edit
);
2166 /* goto end of text */
2168 edit_move_to_bottom (WEdit
* edit
)
2170 if (edit
->curs_line
< edit
->total_lines
)
2172 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
2173 edit
->start_display
= edit
->last_byte
;
2174 edit
->start_line
= edit
->total_lines
;
2175 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
2176 edit
->force
|= REDRAW_PAGE
;
2180 /* goto beginning of line */
2182 edit_cursor_to_bol (WEdit
* edit
)
2184 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
2185 edit
->search_start
= edit
->curs1
;
2186 edit
->prev_col
= edit_get_col (edit
);
2190 /* goto end of line */
2192 edit_cursor_to_eol (WEdit
* edit
)
2194 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
2195 edit
->search_start
= edit
->curs1
;
2196 edit
->prev_col
= edit_get_col (edit
);
2200 /* move cursor to line 'line' */
2202 edit_move_to_line (WEdit
* e
, long line
)
2204 if (line
< e
->curs_line
)
2205 edit_move_up (e
, e
->curs_line
- line
, 0);
2207 edit_move_down (e
, line
- e
->curs_line
, 0);
2208 edit_scroll_screen_over_cursor (e
);
2211 /* scroll window so that first visible line is 'line' */
2213 edit_move_display (WEdit
* e
, long line
)
2215 if (line
< e
->start_line
)
2216 edit_scroll_upward (e
, e
->start_line
- line
);
2218 edit_scroll_downward (e
, line
- e
->start_line
);
2221 /* save markers onto undo stack */
2223 edit_push_markers (WEdit
* edit
)
2225 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2226 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2230 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2239 /* highlight marker toggle */
2241 edit_mark_cmd (WEdit
* edit
, int unmark
)
2243 edit_push_markers (edit
);
2246 edit_set_markers (edit
, 0, 0, 0, 0);
2247 edit
->force
|= REDRAW_PAGE
;
2251 if (edit
->mark2
>= 0)
2253 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
2254 edit
->curs_col
+ edit
->over_col
);
2255 edit
->force
|= REDRAW_PAGE
;
2258 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
2259 edit
->curs_col
+ edit
->over_col
);
2263 static unsigned long
2268 const char option_chars_move_whole_word
[] =
2269 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2275 if (*option_chars_move_whole_word
== '!')
2277 return 0x80000000UL
;
2279 if (g_ascii_isupper ((gchar
) c
))
2281 else if (g_ascii_islower ((gchar
) c
))
2283 else if (g_ascii_isalpha (c
))
2285 else if (isdigit (c
))
2287 else if (isspace (c
))
2289 q
= strchr (option_chars_move_whole_word
, c
);
2291 return 0xFFFFFFFFUL
;
2294 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2299 while ((q
= strchr (q
+ 1, c
)));
2304 edit_left_word_move (WEdit
* edit
, int s
)
2309 if (column_highlighting
2310 && edit
->mark1
!= edit
->mark2
2311 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2313 edit_cursor_move (edit
, -1);
2316 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2317 c2
= edit_get_byte (edit
, edit
->curs1
);
2318 if (!(my_type_of (c1
) & my_type_of (c2
)))
2320 if (isspace (c1
) && !isspace (c2
))
2323 if (!isspace (c1
) && isspace (c2
))
2329 edit_left_word_move_cmd (WEdit
* edit
)
2331 edit_left_word_move (edit
, 0);
2332 edit
->force
|= REDRAW_PAGE
;
2336 edit_right_word_move (WEdit
* edit
, int s
)
2341 if (column_highlighting
2342 && edit
->mark1
!= edit
->mark2
2343 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
2345 edit_cursor_move (edit
, 1);
2346 if (edit
->curs1
>= edit
->last_byte
)
2348 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2349 c2
= edit_get_byte (edit
, edit
->curs1
);
2350 if (!(my_type_of (c1
) & my_type_of (c2
)))
2352 if (isspace (c1
) && !isspace (c2
))
2355 if (!isspace (c1
) && isspace (c2
))
2361 edit_right_word_move_cmd (WEdit
* edit
)
2363 edit_right_word_move (edit
, 0);
2364 edit
->force
|= REDRAW_PAGE
;
2368 edit_right_char_move_cmd (WEdit
* edit
)
2374 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2380 c
= edit_get_byte (edit
, edit
->curs1
);
2382 if (option_cursor_beyond_eol
&& c
== '\n')
2388 edit_cursor_move (edit
, cw
);
2393 edit_left_char_move_cmd (WEdit
* edit
)
2396 if (column_highlighting
2397 && option_cursor_beyond_eol
2398 && edit
->mark1
!= edit
->mark2
2399 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2403 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2407 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2413 edit_cursor_move (edit
, -cw
);
2417 /** Up or down cursor moving.
2418 direction = TRUE - move up
2422 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2425 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
2434 edit
->force
|= REDRAW_PAGE
;
2438 edit_scroll_upward (edit
, i
);
2440 edit_scroll_downward (edit
, i
);
2442 p
= edit_bol (edit
, edit
->curs1
);
2444 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
2446 edit_cursor_move (edit
, p
- edit
->curs1
);
2448 edit_move_to_prev_col (edit
, p
);
2450 /* search start of current multibyte char (like CJK) */
2451 if (edit
->curs1
+ 1 < edit
->last_byte
)
2453 edit_right_char_move_cmd (edit
);
2454 edit_left_char_move_cmd (edit
);
2457 edit
->search_start
= edit
->curs1
;
2458 edit
->found_len
= 0;
2462 edit_right_delete_word (WEdit
* edit
)
2467 if (edit
->curs1
>= edit
->last_byte
)
2469 c1
= edit_delete (edit
, 1);
2470 c2
= edit_get_byte (edit
, edit
->curs1
);
2471 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2473 if (!(my_type_of (c1
) & my_type_of (c2
)))
2479 edit_left_delete_word (WEdit
* edit
)
2484 if (edit
->curs1
<= 0)
2486 c1
= edit_backspace (edit
, 1);
2487 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2488 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2490 if (!(my_type_of (c1
) & my_type_of (c2
)))
2496 the start column position is not recorded, and hence does not
2497 undo as it happed. But who would notice.
2500 edit_do_undo (WEdit
* edit
)
2505 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2507 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
2514 edit_cursor_move (edit
, 1);
2517 edit_cursor_move (edit
, -1);
2520 edit_backspace (edit
, 1);
2523 edit_delete (edit
, 1);
2526 column_highlighting
= 1;
2529 column_highlighting
= 0;
2532 if (ac
>= 256 && ac
< 512)
2533 edit_insert_ahead (edit
, ac
- 256);
2534 if (ac
>= 0 && ac
< 256)
2535 edit_insert (edit
, ac
);
2537 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
2539 edit
->mark1
= ac
- MARK_1
;
2540 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2542 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
2544 edit
->mark2
= ac
- MARK_2
;
2545 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2548 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2551 if (edit
->start_display
> ac
- KEY_PRESS
)
2553 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2554 edit
->force
|= REDRAW_PAGE
;
2556 else if (edit
->start_display
< ac
- KEY_PRESS
)
2558 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2559 edit
->force
|= REDRAW_PAGE
;
2561 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2562 edit_update_curs_row (edit
);
2565 edit
->stack_disable
= 0;
2569 edit_delete_to_line_end (WEdit
* edit
)
2571 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2575 edit_delete (edit
, 1);
2580 edit_delete_to_line_begin (WEdit
* edit
)
2582 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2586 edit_backspace (edit
, 1);
2591 edit_delete_line (WEdit
* edit
)
2594 * Delete right part of the line.
2595 * Note that edit_get_byte() returns '\n' when byte position is
2598 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2600 (void) edit_delete (edit
, 1);
2605 * Note that edit_delete() will not corrupt anything if called while
2606 * cursor position is EOF.
2608 (void) edit_delete (edit
, 1);
2611 * Delete left part of the line.
2612 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2614 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2616 (void) edit_backspace (edit
, 1);
2621 insert_spaces_tab (WEdit
* edit
, int half
)
2624 edit_update_curs_col (edit
);
2625 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
2626 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2629 edit_insert (edit
, ' ');
2635 is_aligned_on_a_tab (WEdit
* edit
)
2637 edit_update_curs_col (edit
);
2638 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2639 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2643 right_of_four_spaces (WEdit
* edit
)
2646 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2647 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2649 return is_aligned_on_a_tab (edit
);
2654 left_of_four_spaces (WEdit
* edit
)
2657 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2658 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2660 return is_aligned_on_a_tab (edit
);
2665 edit_indent_width (WEdit
* edit
, long p
)
2668 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2670 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2674 edit_insert_indent (WEdit
* edit
, int indent
)
2676 if (!option_fill_tabs_with_spaces
)
2678 while (indent
>= TAB_SIZE
)
2680 edit_insert (edit
, '\t');
2684 while (indent
-- > 0)
2685 edit_insert (edit
, ' ');
2689 edit_auto_indent (WEdit
* edit
)
2694 /* use the previous line as a template */
2695 p
= edit_move_backward (edit
, p
, 1);
2696 /* copy the leading whitespace of the line */
2698 { /* no range check - the line _is_ \n-terminated */
2699 c
= edit_get_byte (edit
, p
++);
2700 if (c
!= ' ' && c
!= '\t')
2702 edit_insert (edit
, c
);
2707 edit_double_newline (WEdit
* edit
)
2709 edit_insert (edit
, '\n');
2710 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2712 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2714 edit
->force
|= REDRAW_PAGE
;
2715 edit_insert (edit
, '\n');
2719 edit_tab_cmd (WEdit
* edit
)
2723 if (option_fake_half_tabs
)
2725 if (is_in_indent (edit
))
2727 /*insert a half tab (usually four spaces) unless there is a
2728 half tab already behind, then delete it and insert a
2730 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
2732 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2733 edit_backspace (edit
, 1);
2734 edit_insert (edit
, '\t');
2738 insert_spaces_tab (edit
, 1);
2743 if (option_fill_tabs_with_spaces
)
2745 insert_spaces_tab (edit
, 0);
2749 edit_insert (edit
, '\t');
2754 check_and_wrap_line (WEdit
* edit
)
2757 if (!option_typewriter_wrap
)
2759 edit_update_curs_col (edit
);
2760 if (edit
->curs_col
< option_word_wrap_line_length
)
2766 c
= edit_get_byte (edit
, curs
);
2767 if (c
== '\n' || curs
<= 0)
2769 edit_insert (edit
, '\n');
2772 if (c
== ' ' || c
== '\t')
2774 int current
= edit
->curs1
;
2775 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2776 edit_insert (edit
, '\n');
2777 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2783 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
2786 edit_push_key_press (WEdit
* edit
)
2788 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2789 if (edit
->mark2
== -1)
2790 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2793 /* this find the matching bracket in either direction, and sets edit->bracket */
2795 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2797 const char *const b
= "{}{[][()(", *p
;
2798 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2799 unsigned long j
= 0;
2801 edit_update_curs_row (edit
);
2802 c
= edit_get_byte (edit
, edit
->curs1
);
2805 if (!furthest_bracket_search
)
2806 furthest_bracket_search
--;
2807 /* not on a bracket at all */
2810 /* the matching bracket */
2812 /* going left or right? */
2813 if (strchr ("{[(", c
))
2815 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
2817 /* out of buffer? */
2818 if (q
>= edit
->last_byte
|| q
< 0)
2820 a
= edit_get_byte (edit
, q
);
2821 /* don't want to eat CPU */
2822 if (j
++ > furthest_bracket_search
)
2824 /* out of screen? */
2827 if (q
< edit
->start_display
)
2829 /* count lines if searching downward */
2830 if (inc
> 0 && a
== '\n')
2831 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2834 /* count bracket depth */
2835 i
+= (a
== c
) - (a
== d
);
2836 /* return if bracket depth is zero */
2844 static long last_bracket
= -1;
2847 edit_find_bracket (WEdit
* edit
)
2849 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2850 if (last_bracket
!= edit
->bracket
)
2851 edit
->force
|= REDRAW_PAGE
;
2852 last_bracket
= edit
->bracket
;
2856 edit_goto_matching_bracket (WEdit
* edit
)
2860 q
= edit_get_bracket (edit
, 0, 0);
2863 edit
->bracket
= edit
->curs1
;
2864 edit
->force
|= REDRAW_PAGE
;
2865 edit_cursor_move (edit
, q
- edit
->curs1
);
2870 * This executes a command as though the user initiated it through a key
2871 * press. Callback with WIDGET_KEY as a message calls this after
2872 * translating the key press. This function can be used to pass any
2873 * command to the editor. Note that the screen wouldn't update
2874 * automatically. Either of command or char_for_insertion must be
2875 * passed as -1. Commands are executed, and char_for_insertion is
2876 * inserted at the cursor.
2879 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2881 if (command
== CK_Begin_Record_Macro
)
2884 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2887 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
2889 edit
->force
|= REDRAW_COMPLETELY
;
2890 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2894 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
2896 edit
->macro
[edit
->macro_i
].command
= command
;
2897 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2899 /* record the beginning of a set of editing actions initiated by a key press */
2900 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2901 edit_push_key_press (edit
);
2903 edit_execute_cmd (edit
, command
, char_for_insertion
);
2904 if (column_highlighting
)
2905 edit
->force
|= REDRAW_PAGE
;
2908 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
2911 This executes a command at a lower level than macro recording.
2912 It also does not push a key_press onto the undo stack. This means
2913 that if it is called many times, a single undo command will undo
2914 all of them. It also does not check for the Undo command.
2917 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2919 edit
->force
|= REDRAW_LINE
;
2921 /* The next key press will unhighlight the found string, so update
2923 if (edit
->found_len
|| column_highlighting
)
2924 edit
->force
|= REDRAW_PAGE
;
2926 if (command
/ 100 == 6)
2927 { /* a highlight command like shift-arrow */
2928 column_highlighting
= 0;
2929 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
2931 edit_mark_cmd (edit
, 1); /* clear */
2932 edit_mark_cmd (edit
, 0); /* marking on */
2934 edit
->highlight
= 1;
2937 { /* any other command */
2938 if (edit
->highlight
)
2939 edit_mark_cmd (edit
, 0); /* clear */
2940 edit
->highlight
= 0;
2943 /* first check for undo */
2944 if (command
== CK_Undo
)
2946 edit_do_undo (edit
);
2947 edit
->found_len
= 0;
2948 edit
->prev_col
= edit_get_col (edit
);
2949 edit
->search_start
= edit
->curs1
;
2953 /* An ordinary key press */
2954 if (char_for_insertion
>= 0)
2956 /* if non persistent selection and text selected */
2957 if (!option_persistent_selections
)
2959 if (edit
->mark1
!= edit
->mark2
)
2960 edit_block_delete_cmd (edit
);
2962 if (edit
->overwrite
)
2964 /* remove char only one time, after input first byte, multibyte chars */
2965 if ((!utf8_display
|| edit
->charpoint
== 0)
2966 && edit_get_byte (edit
, edit
->curs1
) != '\n')
2967 edit_delete (edit
, 0);
2969 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2970 edit_insert_over (edit
);
2972 if (char_for_insertion
> 255 && utf8_display
== 0)
2974 unsigned char str
[6 + 1];
2976 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
2986 while (str
[i
] != 0 && i
<= 6)
2988 char_for_insertion
= str
[i
];
2989 edit_insert (edit
, char_for_insertion
);
2995 edit_insert (edit
, char_for_insertion
);
2997 if (option_auto_para_formatting
)
2999 format_paragraph (edit
, 0);
3000 edit
->force
|= REDRAW_PAGE
;
3003 check_and_wrap_line (edit
);
3004 edit
->found_len
= 0;
3005 edit
->prev_col
= edit_get_col (edit
);
3006 edit
->search_start
= edit
->curs1
;
3007 edit_find_bracket (edit
);
3015 case CK_Begin_Page_Highlight
:
3016 case CK_End_Page_Highlight
:
3023 if (edit
->mark2
>= 0)
3025 if (!option_persistent_selections
)
3027 if (column_highlighting
)
3028 edit_push_action (edit
, COLUMN_ON
);
3029 column_highlighting
= 0;
3030 edit_mark_cmd (edit
, 1);
3039 case CK_Begin_Page_Highlight
:
3040 case CK_End_Page_Highlight
:
3045 case CK_Word_Left_Highlight
:
3046 case CK_Word_Right_Highlight
:
3047 case CK_Up_Highlight
:
3048 case CK_Down_Highlight
:
3049 case CK_Up_Alt_Highlight
:
3050 case CK_Down_Alt_Highlight
:
3051 if (edit
->mark2
== -1)
3052 break; /*marking is following the cursor: may need to highlight a whole line */
3055 case CK_Left_Highlight
:
3056 case CK_Right_Highlight
:
3057 edit
->force
|= REDRAW_CHAR_ONLY
;
3060 /* basic cursor key commands */
3064 /* if non persistent selection and text selected */
3065 if (!option_persistent_selections
)
3067 if (edit
->mark1
!= edit
->mark2
)
3069 edit_block_delete_cmd (edit
);
3073 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3078 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3080 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3081 edit_backspace (edit
, 1);
3086 if (option_fake_half_tabs
)
3089 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3091 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3092 edit_backspace (edit
, 1);
3097 edit_backspace (edit
, 0);
3100 /* if non persistent selection and text selected */
3101 if (!option_persistent_selections
)
3103 if (edit
->mark1
!= edit
->mark2
)
3105 edit_block_delete_cmd (edit
);
3110 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3111 edit_insert_over (edit
);
3113 if (option_fake_half_tabs
)
3116 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3118 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3119 edit_delete (edit
, 1);
3123 edit_delete (edit
, 0);
3125 case CK_Delete_Word_Left
:
3127 edit_left_delete_word (edit
);
3129 case CK_Delete_Word_Right
:
3130 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3131 edit_insert_over (edit
);
3133 edit_right_delete_word (edit
);
3135 case CK_Delete_Line
:
3136 edit_delete_line (edit
);
3138 case CK_Delete_To_Line_End
:
3139 edit_delete_to_line_end (edit
);
3141 case CK_Delete_To_Line_Begin
:
3142 edit_delete_to_line_begin (edit
);
3146 if (option_auto_para_formatting
)
3148 edit_double_newline (edit
);
3149 if (option_return_does_auto_indent
)
3150 edit_auto_indent (edit
);
3151 format_paragraph (edit
, 0);
3155 edit_insert (edit
, '\n');
3156 if (option_return_does_auto_indent
)
3158 edit_auto_indent (edit
);
3163 edit_insert (edit
, '\n');
3166 case CK_Page_Up_Alt_Highlight
:
3167 column_highlighting
= 1;
3169 case CK_Page_Up_Highlight
:
3170 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3172 case CK_Page_Down_Alt_Highlight
:
3173 column_highlighting
= 1;
3175 case CK_Page_Down_Highlight
:
3176 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3178 case CK_Left_Alt_Highlight
:
3179 column_highlighting
= 1;
3181 case CK_Left_Highlight
:
3182 if (option_fake_half_tabs
)
3184 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3186 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3189 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3190 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3194 edit_left_char_move_cmd (edit
);
3196 case CK_Right_Alt_Highlight
:
3197 column_highlighting
= 1;
3199 case CK_Right_Highlight
:
3200 if (option_fake_half_tabs
)
3202 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3204 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3205 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3209 edit_right_char_move_cmd (edit
);
3212 case CK_Begin_Page_Highlight
:
3213 edit_begin_page (edit
);
3216 case CK_End_Page_Highlight
:
3217 edit_end_page (edit
);
3220 case CK_Word_Left_Highlight
:
3222 edit_left_word_move_cmd (edit
);
3225 case CK_Word_Right_Highlight
:
3227 edit_right_word_move_cmd (edit
);
3229 case CK_Up_Alt_Highlight
:
3230 column_highlighting
= 1;
3232 case CK_Up_Highlight
:
3233 edit_move_up (edit
, 1, 0);
3235 case CK_Down_Alt_Highlight
:
3236 column_highlighting
= 1;
3238 case CK_Down_Highlight
:
3239 edit_move_down (edit
, 1, 0);
3241 case CK_Paragraph_Up_Alt_Highlight
:
3242 column_highlighting
= 1;
3243 case CK_Paragraph_Up
:
3244 case CK_Paragraph_Up_Highlight
:
3245 edit_move_up_paragraph (edit
, 0);
3247 case CK_Paragraph_Down_Alt_Highlight
:
3248 column_highlighting
= 1;
3249 case CK_Paragraph_Down
:
3250 case CK_Paragraph_Down_Highlight
:
3251 edit_move_down_paragraph (edit
, 0);
3253 case CK_Scroll_Up_Alt_Highlight
:
3254 column_highlighting
= 1;
3256 case CK_Scroll_Up_Highlight
:
3257 edit_move_up (edit
, 1, 1);
3259 case CK_Scroll_Down_Alt_Highlight
:
3260 column_highlighting
= 1;
3261 case CK_Scroll_Down
:
3262 case CK_Scroll_Down_Highlight
:
3263 edit_move_down (edit
, 1, 1);
3266 case CK_Home_Highlight
:
3267 edit_cursor_to_bol (edit
);
3270 case CK_End_Highlight
:
3271 edit_cursor_to_eol (edit
);
3274 /* if text marked shift block */
3275 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3277 if (edit
->mark2
< 0)
3278 edit_mark_cmd (edit
, 0);
3279 edit_move_block_to_right (edit
);
3283 if (option_cursor_beyond_eol
)
3284 edit_insert_over (edit
);
3285 edit_tab_cmd (edit
);
3286 if (option_auto_para_formatting
)
3288 format_paragraph (edit
, 0);
3289 edit
->force
|= REDRAW_PAGE
;
3293 check_and_wrap_line (edit
);
3298 case CK_Toggle_Insert
:
3299 edit
->overwrite
= (edit
->overwrite
== 0);
3303 if (edit
->mark2
>= 0)
3305 if (column_highlighting
)
3306 edit_push_action (edit
, COLUMN_ON
);
3307 column_highlighting
= 0;
3309 edit_mark_cmd (edit
, 0);
3311 case CK_Column_Mark
:
3312 if (!column_highlighting
)
3313 edit_push_action (edit
, COLUMN_OFF
);
3314 column_highlighting
= 1;
3315 edit_mark_cmd (edit
, 0);
3318 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3319 edit
->force
|= REDRAW_PAGE
;
3322 if (column_highlighting
)
3323 edit_push_action (edit
, COLUMN_ON
);
3324 column_highlighting
= 0;
3325 edit_mark_cmd (edit
, 1);
3328 case CK_Toggle_Line_State
:
3329 option_line_state
= !option_line_state
;
3330 if (option_line_state
)
3332 option_line_state_width
= LINE_STATE_WIDTH
;
3336 option_line_state_width
= 0;
3338 edit
->force
|= REDRAW_PAGE
;
3341 case CK_Toggle_Show_Margin
:
3342 show_right_margin
= !show_right_margin
;
3343 edit
->force
|= REDRAW_PAGE
;
3346 case CK_Toggle_Bookmark
:
3347 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3348 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3349 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3351 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3353 case CK_Flush_Bookmarks
:
3354 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3355 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3356 edit
->force
|= REDRAW_PAGE
;
3358 case CK_Next_Bookmark
:
3359 if (edit
->book_mark
)
3361 struct _book_mark
*p
;
3362 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3366 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3367 || p
->line
< edit
->start_line
)
3368 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3369 edit_move_to_line (edit
, p
->line
);
3373 case CK_Prev_Bookmark
:
3374 if (edit
->book_mark
)
3376 struct _book_mark
*p
;
3377 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3378 while (p
->line
== edit
->curs_line
)
3383 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3384 || p
->line
< edit
->start_line
)
3385 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3386 edit_move_to_line (edit
, p
->line
);
3391 case CK_Beginning_Of_Text
:
3392 case CK_Beginning_Of_Text_Highlight
:
3393 edit_move_to_top (edit
);
3395 case CK_End_Of_Text
:
3396 case CK_End_Of_Text_Highlight
:
3397 edit_move_to_bottom (edit
);
3401 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3402 edit_insert_over (edit
);
3403 edit_block_copy_cmd (edit
);
3406 edit_block_delete_cmd (edit
);
3409 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3410 edit_insert_over (edit
);
3411 edit_block_move_cmd (edit
);
3414 case CK_Shift_Block_Left
:
3415 if (edit
->mark1
!= edit
->mark2
)
3416 edit_move_block_to_left (edit
);
3418 case CK_Shift_Block_Right
:
3419 if (edit
->mark1
!= edit
->mark2
)
3420 edit_move_block_to_right (edit
);
3423 edit_copy_to_X_buf_cmd (edit
);
3426 edit_cut_to_X_buf_cmd (edit
);
3429 /* if non persistent selection and text selected */
3430 if (!option_persistent_selections
)
3432 if (edit
->mark1
!= edit
->mark2
)
3433 edit_block_delete_cmd (edit
);
3435 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3436 edit_insert_over (edit
);
3437 edit_paste_from_X_buf_cmd (edit
);
3439 case CK_Selection_History
:
3440 edit_paste_from_history (edit
);
3444 edit_save_as_cmd (edit
);
3447 edit_save_confirm_cmd (edit
);
3450 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3453 edit_save_block_cmd (edit
);
3455 case CK_Insert_File
:
3456 edit_insert_file_cmd (edit
);
3459 case CK_Load_Prev_File
:
3460 edit_load_back_cmd (edit
);
3462 case CK_Load_Next_File
:
3463 edit_load_forward_cmd (edit
);
3466 case CK_Load_Syntax_File
:
3467 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3469 case CK_Choose_Syntax
:
3470 edit_syntax_dialog (edit
, edit
->syntax_type
);
3473 case CK_Load_Menu_File
:
3474 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3477 case CK_Toggle_Syntax
:
3478 if ((option_syntax_highlighting
^= 1) == 1)
3479 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3480 edit
->force
|= REDRAW_PAGE
;
3483 case CK_Toggle_Tab_TWS
:
3484 enable_show_tabs_tws
^= 1;
3485 edit
->force
|= REDRAW_PAGE
;
3489 edit_search_cmd (edit
, 0);
3492 edit_search_cmd (edit
, 1);
3495 edit_replace_cmd (edit
, 0);
3497 case CK_Replace_Again
:
3498 edit_replace_cmd (edit
, 1);
3500 case CK_Complete_Word
:
3501 /* if text marked shift block */
3502 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3504 edit_move_block_to_left (edit
);
3508 edit_complete_word_cmd (edit
);
3511 case CK_Find_Definition
:
3512 edit_get_match_keyword_cmd (edit
);
3515 dlg_stop (edit
->widget
.parent
);
3518 edit_new_cmd (edit
);
3521 edit_help_cmd (edit
);
3524 edit_refresh_cmd (edit
);
3526 case CK_SaveSetupCmd
:
3530 query_dialog (_(" About "),
3531 _("\n Cooledit v3.11.5\n\n"
3532 " Copyright (C) 1996 the Free Software Foundation\n\n"
3533 " A user friendly text editor written\n"
3534 " for the Midnight Commander.\n"), D_NORMAL
, 1, _("&OK"));
3539 case CK_Edit_Options
:
3540 edit_options_dialog (edit
);
3542 case CK_Edit_Save_Mode
:
3543 menu_save_mode_cmd ();
3548 /* fool gcc to prevent a Y2K warning */
3549 char time_format
[] = "_c";
3550 time_format
[0] = '%';
3552 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3553 edit_print_string (edit
, s
);
3554 edit
->force
|= REDRAW_PAGE
;
3559 edit_goto_cmd (edit
);
3561 case CK_Paragraph_Format
:
3562 format_paragraph (edit
, 1);
3563 edit
->force
|= REDRAW_PAGE
;
3565 case CK_Delete_Macro
:
3566 edit_delete_macro_cmd (edit
);
3568 case CK_Match_Bracket
:
3569 edit_goto_matching_bracket (edit
);
3575 edit_sort_cmd (edit
);
3578 edit_ext_cmd (edit
);
3581 edit_mail_dialog (edit
);
3586 case CK_SelectCodepage
:
3587 edit_select_codepage_cmd (edit
);
3589 case CK_Insert_Literal
:
3590 edit_insert_literal_cmd (edit
);
3592 case CK_Execute_Macro
:
3593 edit_execute_macro_cmd (edit
);
3595 case CK_Begin_End_Macro
:
3596 edit_begin_end_macro_cmd (edit
);
3606 if ((command
/ 1000) == 1) /* a shell command */
3607 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3608 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
3609 { /* a macro command */
3610 struct macro m
[MAX_MACRO_LENGTH
];
3612 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3613 edit_execute_macro (edit
, m
, nm
);
3616 /* keys which must set the col position, and the search vars */
3622 case CK_Replace_Again
:
3623 case CK_Complete_Word
:
3624 edit
->prev_col
= edit_get_col (edit
);
3627 case CK_Up_Highlight
:
3628 case CK_Up_Alt_Highlight
:
3630 case CK_Down_Highlight
:
3631 case CK_Down_Alt_Highlight
:
3633 case CK_Page_Up_Highlight
:
3634 case CK_Page_Up_Alt_Highlight
:
3636 case CK_Page_Down_Highlight
:
3637 case CK_Page_Down_Alt_Highlight
:
3638 case CK_Beginning_Of_Text
:
3639 case CK_Beginning_Of_Text_Highlight
:
3640 case CK_End_Of_Text
:
3641 case CK_End_Of_Text_Highlight
:
3642 case CK_Paragraph_Up
:
3643 case CK_Paragraph_Up_Highlight
:
3644 case CK_Paragraph_Up_Alt_Highlight
:
3645 case CK_Paragraph_Down
:
3646 case CK_Paragraph_Down_Highlight
:
3647 case CK_Paragraph_Down_Alt_Highlight
:
3649 case CK_Scroll_Up_Highlight
:
3650 case CK_Scroll_Up_Alt_Highlight
:
3651 case CK_Scroll_Down
:
3652 case CK_Scroll_Down_Highlight
:
3653 case CK_Scroll_Down_Alt_Highlight
:
3654 edit
->search_start
= edit
->curs1
;
3655 edit
->found_len
= 0;
3658 edit
->found_len
= 0;
3659 edit
->prev_col
= edit_get_col (edit
);
3660 edit
->search_start
= edit
->curs1
;
3662 edit_find_bracket (edit
);
3664 if (option_auto_para_formatting
)
3670 case CK_Delete_Word_Left
:
3671 case CK_Delete_Word_Right
:
3672 case CK_Delete_To_Line_End
:
3673 case CK_Delete_To_Line_Begin
:
3674 format_paragraph (edit
, 0);
3675 edit
->force
|= REDRAW_PAGE
;
3682 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
3686 if (edit
->macro_depth
++ > 256)
3688 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3689 edit
->macro_depth
--;
3692 edit
->force
|= REDRAW_PAGE
;
3695 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3697 edit_update_screen (edit
);
3698 edit
->macro_depth
--;
3701 /* User edit menu, like user menu (F2) but only in editor. */
3703 user_menu (WEdit
* edit
)
3707 long start_mark
, end_mark
;
3710 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3712 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3714 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3716 /* run shell scripts from menu */
3717 user_menu_cmd (edit
);
3719 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
3726 /* i.e. we have marked block */
3727 rc
= edit_block_delete_cmd (edit
);
3731 edit_insert_file (edit
, block_file
);
3733 /* truncate block file */
3734 fd
= fopen (block_file
, "w");
3738 edit_refresh_cmd (edit
);
3739 edit
->force
|= REDRAW_COMPLETELY
;
3741 g_free (block_file
);
3745 edit_stack_init (void)
3747 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3749 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3750 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3753 edit_stack_iterator
= 0;
3757 edit_stack_free (void)
3759 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3760 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3765 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3767 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3772 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3774 edit_move_updown (edit
, i
, do_scroll
, FALSE
);