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"
61 #include "edit-impl.h"
63 #include "edit-widget.h"
66 int option_word_wrap_line_length
= 72;
67 int option_typewriter_wrap
= 0;
68 int option_auto_para_formatting
= 0;
69 int option_fill_tabs_with_spaces
= 0;
70 int option_return_does_auto_indent
= 1;
71 int option_backspace_through_tabs
= 0;
72 int option_fake_half_tabs
= 1;
73 int option_save_mode
= EDIT_QUICK_SAVE
;
74 int option_save_position
= 1;
75 int option_max_undo
= 32768;
76 int option_persistent_selections
= 1;
77 int option_cursor_beyond_eol
= 0;
78 int option_line_state
= 0;
79 int option_line_state_width
= 0;
81 int option_edit_right_extreme
= 0;
82 int option_edit_left_extreme
= 0;
83 int option_edit_top_extreme
= 0;
84 int option_edit_bottom_extreme
= 0;
85 int enable_show_tabs_tws
= 1;
86 int option_check_nl_at_eof
= 0;
88 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
89 char *option_backup_ext
= NULL
;
91 int edit_stack_iterator
= 0;
92 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
93 /* magic sequense for say than block is vertical */
94 const char VERTICAL_MAGIC
[] = {'\1', '\1', '\1', '\1', '\n'};
97 * here's a quick sketch of the layout: (don't run this through indent.)
99 * (b1 is buffers1 and b2 is buffers2)
102 * \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
103 * ______________________________________|______________________________________
105 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
106 * |-> |-> |-> |-> |-> |-> |
108 * _<------------------------->|<----------------->_
109 * WEdit->curs2 | WEdit->curs1
114 * file end|||file beginning
123 const global_keymap_t
*editor_map
;
124 const global_keymap_t
*editor_x_map
;
126 static void user_menu (WEdit
*edit
);
128 int edit_get_byte (WEdit
* edit
, long byte_index
)
131 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
134 if (byte_index
>= edit
->curs1
) {
135 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
136 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
138 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
142 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
145 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
148 if (byte_index
>= edit
->curs1
) {
149 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
150 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
]+(EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
152 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
]+(byte_index
& M_EDIT_BUF_SIZE
));
156 char *edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
160 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
166 if (byte_index
>= edit
->curs1
) {
167 p
= edit
->curs1
+ edit
->curs2
- 1;
168 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
170 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
174 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
179 gchar
*next_ch
= NULL
;
182 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
187 str
= edit_get_byte_ptr (edit
, byte_index
);
194 res
= g_utf8_get_char_validated (str
, -1);
201 /* Calculate UTF-8 char width */
202 next_ch
= g_utf8_next_char (str
);
204 width
= next_ch
- str
;
214 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
216 gchar
*str
, *buf
= NULL
;
219 gchar
*next_ch
= NULL
;
225 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
230 ch
= edit_get_utf (edit
, byte_index
, &width
);
237 str
= edit_get_byte_ptr (edit
, byte_index
);
238 buf
= edit_get_buf_ptr (edit
, byte_index
);
239 if (str
== NULL
|| buf
== NULL
) {
243 /* get prev utf8 char */
245 str
= g_utf8_find_prev_char (buf
, str
);
247 res
= g_utf8_get_char_validated (str
, -1);
254 /* Calculate UTF-8 char width */
255 next_ch
= g_utf8_next_char(str
);
257 width
= next_ch
- str
;
268 * Initialize the buffers for an empty files.
271 edit_init_buffers (WEdit
*edit
)
275 for (j
= 0; j
<= MAXBUFF
; j
++) {
276 edit
->buffers1
[j
] = NULL
;
277 edit
->buffers2
[j
] = NULL
;
282 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
286 * Load file OR text into buffers. Set cursor to the beginning of file.
290 edit_load_file_fast (WEdit
*edit
, const char *filename
)
294 edit
->curs2
= edit
->last_byte
;
295 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
297 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
298 GString
*errmsg
= g_string_new(NULL
);
299 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
300 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
301 g_string_free (errmsg
, TRUE
);
305 if (!edit
->buffers2
[buf2
])
306 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
309 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
310 (edit
->curs2
& M_EDIT_BUF_SIZE
),
311 edit
->curs2
& M_EDIT_BUF_SIZE
);
313 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
314 /* edit->buffers2[0] is already allocated */
315 if (!edit
->buffers2
[buf
])
316 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
317 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
324 /* detecting an error on save is easy: just check if every byte has been written. */
325 /* detecting an error on read, is not so easy 'cos there is not way to tell
326 whether you read everything or not. */
327 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
328 static const struct edit_filters
{
329 const char *read
, *write
, *extension
;
331 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
332 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
333 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
334 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
335 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
338 /* Return index of the filter or -1 is there is no appropriate filter */
339 static int edit_find_filter (const char *filename
)
343 if (filename
== NULL
)
346 l
= strlen (filename
);
347 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
348 e
= strlen (all_filters
[i
].extension
);
350 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
357 edit_get_filter (const char *filename
)
360 char *p
, *quoted_name
;
362 i
= edit_find_filter (filename
);
366 quoted_name
= name_quote (filename
, 0);
367 p
= g_strdup_printf(all_filters
[i
].read
, quoted_name
);
368 g_free (quoted_name
);
373 edit_get_write_filter (const char *write_name
, const char *filename
)
378 i
= edit_find_filter (filename
);
382 writename
= name_quote (write_name
, 0);
383 p
= g_strdup_printf(all_filters
[i
].write
, writename
);
389 edit_insert_stream (WEdit
* edit
, FILE * f
)
393 while ((c
= fgetc (f
)) >= 0) {
394 edit_insert (edit
, c
);
400 long edit_write_stream (WEdit
* edit
, FILE * f
)
404 if (edit
->lb
== LB_ASIS
) {
405 for (i
= 0; i
< edit
->last_byte
; i
++)
406 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
411 /* change line breaks */
412 for (i
= 0; i
< edit
->last_byte
; i
++) {
413 unsigned char c
= edit_get_byte (edit
, i
);
415 if (!(c
== '\n' || c
== '\r')) {
417 if (fputc (c
, f
) < 0)
419 } else { /* (c == '\n' || c == '\r') */
420 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
423 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
424 /* put one line break unconditionally */
425 if (fputc ('\n', f
) < 0)
428 i
++; /* 2 chars are processed */
430 if (c
== '\r' && c1
== '\n')
431 /* Windows line break; go to the next char */
434 if (c
== '\r' && c1
== '\r') {
435 /* two Macintosh line breaks; put second line break */
436 if (fputc ('\n', f
) < 0)
441 if (fputc (c1
, f
) < 0)
445 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
446 /* put one line break unconditionally */
447 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
450 if (c
== '\r' && c1
== '\n')
451 /* Windows line break; go to the next char */
455 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
456 /* put one line break unconditionally */
457 if (fputc ('\r', f
) < 0)
460 i
++; /* 2 chars are processed */
462 if (c
== '\r' && c1
== '\n')
463 /* Windows line break; go to the next char */
466 if (c
== '\n' && c1
== '\n') {
467 /* two Windows line breaks; put second line break */
468 if (fputc ('\r', f
) < 0)
473 if (fputc (c1
, f
) < 0)
476 case LB_ASIS
: /* default without changes */
482 return edit
->last_byte
;
485 #define TEMP_BUF_LEN 1024
487 /* inserts a file at the cursor, returns 1 on success */
489 edit_insert_file (WEdit
*edit
, const char *filename
)
493 p
= edit_get_filter (filename
);
496 long current
= edit
->curs1
;
497 f
= (FILE *) popen (p
, "r");
499 edit_insert_stream (edit
, f
);
500 edit_cursor_move (edit
, current
- edit
->curs1
);
501 if (pclose (f
) > 0) {
503 errmsg
= g_strdup_printf(_(" Error reading from pipe: %s "), p
);
504 edit_error_dialog (_("Error"), errmsg
);
511 errmsg
= g_strdup_printf (_(" Cannot open pipe for reading: %s "), p
);
512 edit_error_dialog (_("Error"), errmsg
);
519 int i
, file
, blocklen
;
520 long current
= edit
->curs1
;
521 int vertical_insertion
= 0;
523 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
525 buf
= g_malloc0 (TEMP_BUF_LEN
);
526 blocklen
= mc_read (file
, buf
, sizeof(VERTICAL_MAGIC
));
528 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
529 if ( memcmp(buf
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
)) == 0 ) {
530 vertical_insertion
= 1;
532 mc_lseek (file
, 0, SEEK_SET
);
535 if (vertical_insertion
) {
536 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
538 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
539 for (i
= 0; i
< blocklen
; i
++)
540 edit_insert (edit
, buf
[i
]);
543 edit_cursor_move (edit
, current
- edit
->curs1
);
552 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
554 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
557 GString
*errmsg
= (GString
*) 0;
559 /* Try opening an existing file */
560 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
564 * Try creating the file. O_EXCL prevents following broken links
565 * and opening existing files.
569 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
572 g_string_sprintf (errmsg
= g_string_new (NULL
),
573 _(" Cannot open %s for reading "), filename
);
576 /* New file, delete it if it's not modified or saved */
577 edit
->delete_file
= 1;
581 /* Check what we have opened */
582 if (mc_fstat (file
, st
) < 0) {
583 g_string_sprintf (errmsg
= g_string_new (NULL
),
584 _(" Cannot get size/permissions for %s "), filename
);
588 /* We want to open regular files only */
589 if (!S_ISREG (st
->st_mode
)) {
590 g_string_sprintf (errmsg
= g_string_new (NULL
),
591 _(" %s is not a regular file "), filename
);
596 * Don't delete non-empty files.
597 * O_EXCL should prevent it, but let's be on the safe side.
599 if (st
->st_size
> 0) {
600 edit
->delete_file
= 0;
603 if (st
->st_size
>= SIZE_LIMIT
) {
604 g_string_sprintf (errmsg
= g_string_new (NULL
),
605 _(" File %s is too large "), filename
);
609 (void) mc_close (file
);
611 edit_error_dialog (_("Error"), errmsg
->str
);
612 g_string_free (errmsg
, TRUE
);
619 * Open the file and load it into the buffers, either directly or using
620 * a filter. Return 0 on success, 1 on error.
622 * Fast loading (edit_load_file_fast) is used when the file size is
623 * known. In this case the data is read into the buffers by blocks.
624 * If the file size is not known, the data is loaded byte by byte in
628 edit_load_file (WEdit
*edit
)
632 /* Cannot do fast load if a filter is used */
633 if (edit_find_filter (edit
->filename
) >= 0)
637 * VFS may report file size incorrectly, and slow load is not a big
638 * deal considering overhead in VFS.
640 if (!vfs_file_is_local (edit
->filename
))
644 * FIXME: line end translation should disable fast loading as well
645 * Consider doing fseek() to the end and ftell() for the real size.
648 if (*edit
->filename
) {
649 /* If we are dealing with a real file, check that it exists */
650 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
653 /* nothing to load */
657 edit_init_buffers (edit
);
660 edit
->last_byte
= edit
->stat1
.st_size
;
661 edit_load_file_fast (edit
, edit
->filename
);
662 /* If fast load was used, the number of lines wasn't calculated */
663 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
666 const char *codepage_id
;
669 if (*edit
->filename
) {
670 edit
->stack_disable
= 1;
671 if (!edit_insert_file (edit
, edit
->filename
)) {
675 edit
->stack_disable
= 0;
679 codepage_id
= get_codepage_id( source_codepage
);
681 edit
->utf8
= str_isutf8 ( codepage_id
);
688 /* Restore saved cursor position in the file */
690 edit_load_position (WEdit
*edit
)
695 if (!edit
->filename
|| !*edit
->filename
)
698 filename
= vfs_canon (edit
->filename
);
699 load_file_position (filename
, &line
, &column
);
702 edit_move_to_line (edit
, line
- 1);
703 edit
->prev_col
= column
;
704 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
705 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
708 /* Save cursor position in the file */
710 edit_save_position (WEdit
*edit
)
714 if (!edit
->filename
|| !*edit
->filename
)
717 filename
= vfs_canon (edit
->filename
);
718 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
722 /* Clean the WEdit stricture except the widget part */
724 edit_purge_widget (WEdit
*edit
)
726 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
727 char *start
= (char *) edit
+ sizeof (Widget
);
728 memset (start
, 0, len
);
729 edit
->macro_i
= -1; /* not recording a macro */
733 edit_set_keymap (void)
735 editor_map
= default_editor_keymap
;
736 if (editor_keymap
&& editor_keymap
->len
> 0)
737 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
739 editor_x_map
= default_editor_x_keymap
;
740 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
741 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
745 #define space_width 1
748 * Fill in the edit structure. Return NULL on failure. Pass edit as
749 * NULL to allocate a new structure.
751 * If line is 0, try to restore saved position. Otherwise put the
752 * cursor on that line and show it in the middle of the screen.
755 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
759 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
760 if ( option_line_state
) {
761 option_line_state_width
= LINE_STATE_WIDTH
;
763 option_line_state_width
= 0;
768 * Expand option_whole_chars_search by national letters using
772 static char option_whole_chars_search_buf
[256];
774 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
776 size_t len
= str_term_width1 (option_whole_chars_search
);
778 strcpy (option_whole_chars_search_buf
,
779 option_whole_chars_search
);
781 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
782 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
783 option_whole_chars_search_buf
[len
++] = i
;
787 option_whole_chars_search_buf
[len
] = 0;
788 option_whole_chars_search
= option_whole_chars_search_buf
;
790 #endif /* ENABLE_NLS */
791 edit
= g_malloc0 (sizeof (WEdit
));
795 edit_purge_widget (edit
);
796 edit
->num_widget_lines
= lines
;
798 edit
->num_widget_columns
= columns
;
799 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
800 edit
->stat1
.st_uid
= getuid ();
801 edit
->stat1
.st_gid
= getgid ();
802 edit
->stat1
.st_mtime
= 0;
804 edit
->force
|= REDRAW_PAGE
;
805 edit_set_filename (edit
, filename
);
806 edit
->stack_size
= START_STACK_SIZE
;
807 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
808 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
811 edit
->converter
= str_cnv_from_term
;
812 edit_set_codeset (edit
);
814 if (edit_load_file (edit
)) {
815 /* edit_load_file already gives an error message */
821 edit
->loading_done
= 1;
824 edit_load_syntax (edit
, 0, 0);
827 edit_get_syntax_color (edit
, -1, &color
);
830 /* load saved cursor position */
831 if ((line
== 0) && option_save_position
) {
832 edit_load_position (edit
);
836 edit_move_display (edit
, line
- 1);
837 edit_move_to_line (edit
, line
- 1);
845 /* Clear the edit struct, freeing everything in it. Return 1 on success */
847 edit_clean (WEdit
*edit
)
854 /* a stale lock, remove it */
856 edit
->locked
= edit_unlock_file (edit
->filename
);
858 /* save cursor position */
859 if (option_save_position
)
860 edit_save_position (edit
);
862 /* File specified on the mcedit command line and never saved */
863 if (edit
->delete_file
)
864 unlink (edit
->filename
);
866 edit_free_syntax_rules (edit
);
867 book_mark_flush (edit
, -1);
868 for (; j
<= MAXBUFF
; j
++) {
869 g_free (edit
->buffers1
[j
]);
870 g_free (edit
->buffers2
[j
]);
873 g_free (edit
->undo_stack
);
874 g_free (edit
->filename
);
877 mc_search_free (edit
->search
);
880 if (edit
->converter
!= str_cnv_from_term
)
881 str_close_conv (edit
->converter
);
883 edit_purge_widget (edit
);
888 /* returns 1 on success */
890 edit_renew (WEdit
* edit
)
892 int lines
= edit
->num_widget_lines
;
893 int columns
= edit
->num_widget_columns
;
896 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
900 * Load a new file into the editor. If it fails, preserve the old file.
901 * To do it, allocate a new widget, initialize it and, if the new file
902 * was loaded, copy the data to the old widget.
903 * Return 1 on success, 0 on failure.
906 edit_reload (WEdit
*edit
, const char *filename
)
909 int lines
= edit
->num_widget_lines
;
910 int columns
= edit
->num_widget_columns
;
912 e
= g_malloc0 (sizeof (WEdit
));
913 e
->widget
= edit
->widget
;
914 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
919 memcpy (edit
, e
, sizeof (WEdit
));
925 * Load a new file into the editor and set line. If it fails, preserve the old file.
926 * To do it, allocate a new widget, initialize it and, if the new file
927 * was loaded, copy the data to the old widget.
928 * Return 1 on success, 0 on failure.
931 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
934 int lines
= edit
->num_widget_lines
;
935 int columns
= edit
->num_widget_columns
;
937 e
= g_malloc0 (sizeof (WEdit
));
938 e
->widget
= edit
->widget
;
939 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
944 memcpy (edit
, e
, sizeof (WEdit
));
950 edit_set_codeset (WEdit
*edit
)
955 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
960 conv
= str_crt_conv_from (cp_id
);
961 if (conv
!= INVALID_CONV
)
963 if (edit
->converter
!= str_cnv_from_term
)
964 str_close_conv (edit
->converter
);
965 edit
->converter
= conv
;
970 edit
->utf8
= str_isutf8 (cp_id
);
978 Recording stack for undo:
979 The following is an implementation of a compressed stack. Identical
980 pushes are recorded by a negative prefix indicating the number of times the
981 same char was pushed. This saves space for repeated curs-left or curs-right
998 If the stack long int is 0-255 it represents a normal insert (from a backspace),
999 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1000 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1001 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1004 The only way the cursor moves or the buffer is changed is through the routines:
1005 insert, backspace, insert_ahead, delete, and cursor_move.
1006 These record the reverse undo movements onto the stack each time they are
1009 Each key press results in a set of actions (insert; delete ...). So each time
1010 a key is pressed the current position of start_display is pushed as
1011 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1012 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1013 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1017 void edit_push_action (WEdit
* edit
, long c
,...)
1019 unsigned long sp
= edit
->stack_pointer
;
1023 /* first enlarge the stack if necessary */
1024 if (sp
> edit
->stack_size
- 10) { /* say */
1025 if (option_max_undo
< 256)
1026 option_max_undo
= 256;
1027 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1028 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1030 edit
->undo_stack
= t
;
1031 edit
->stack_size
<<= 1;
1032 edit
->stack_size_mask
= edit
->stack_size
- 1;
1036 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1037 if (edit
->stack_disable
)
1040 #ifdef FAST_MOVE_CURSOR
1041 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1043 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1044 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1046 c
= -(va_arg (ap
, int));
1049 #endif /* ! FAST_MOVE_CURSOR */
1050 if (edit
->stack_bottom
!= sp
1051 && spm1
!= edit
->stack_bottom
1052 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1054 if (edit
->undo_stack
[spm1
] < 0) {
1055 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1057 if (edit
->undo_stack
[spm1
] > -1000000000) {
1058 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1059 edit
->undo_stack
[spm1
]--;
1063 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1064 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1065 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1066 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1067 if (edit
->undo_stack
[spm1
] == -2)
1068 edit
->stack_pointer
= spm1
;
1070 edit
->undo_stack
[spm1
]++;
1075 d
= edit
->undo_stack
[spm1
];
1078 return; /* --> no need to push multiple do-nothings */
1079 edit
->undo_stack
[sp
] = -2;
1082 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1083 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1084 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1085 edit
->stack_pointer
= spm1
;
1091 edit
->undo_stack
[sp
] = c
;
1094 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1096 /* if the sp wraps round and catches the stack_bottom then erase
1097 * the first set of actions on the stack to make space - by moving
1098 * stack_bottom forward one "key press" */
1099 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1100 if ((unsigned long) c
== edit
->stack_bottom
||
1101 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1103 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1104 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1106 /*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: */
1107 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1108 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1112 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1113 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1116 pop_action (WEdit
* edit
)
1119 unsigned long sp
= edit
->stack_pointer
;
1120 if (sp
== edit
->stack_bottom
) {
1121 return STACK_BOTTOM
;
1123 sp
= (sp
- 1) & edit
->stack_size_mask
;
1124 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1125 /* edit->undo_stack[sp] = '@'; */
1126 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1129 if (sp
== edit
->stack_bottom
) {
1130 return STACK_BOTTOM
;
1132 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1133 if (edit
->undo_stack
[sp
] == -2) {
1134 /* edit->undo_stack[sp] = '@'; */
1135 edit
->stack_pointer
= sp
;
1137 edit
->undo_stack
[sp
]++;
1142 /* is called whenever a modification is made by one of the four routines below */
1143 static void edit_modification (WEdit
* edit
)
1145 edit
->caches_valid
= 0;
1146 edit
->screen_modified
= 1;
1148 /* raise lock when file modified */
1149 if (!edit
->modified
&& !edit
->delete_file
)
1150 edit
->locked
= edit_lock_file (edit
->filename
);
1155 Basic low level single character buffer alterations and movements at the cursor.
1156 Returns char passed over, inserted or removed.
1160 edit_insert (WEdit
*edit
, int c
)
1162 /* check if file has grown to large */
1163 if (edit
->last_byte
>= SIZE_LIMIT
)
1166 /* first we must update the position of the display window */
1167 if (edit
->curs1
< edit
->start_display
) {
1168 edit
->start_display
++;
1173 /* Mark file as modified, unless the file hasn't been fully loaded */
1174 if (edit
->loading_done
) {
1175 edit_modification (edit
);
1178 /* now we must update some info on the file and check if a redraw is required */
1180 if (edit
->book_mark
)
1181 book_mark_inc (edit
, edit
->curs_line
);
1183 edit
->total_lines
++;
1184 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1187 /* save the reverse command onto the undo stack */
1188 edit_push_action (edit
, BACKSPACE
);
1190 /* update markers */
1191 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1192 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1193 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1195 /* add a new buffer if we've reached the end of the last one */
1196 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1197 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1198 g_malloc0 (EDIT_BUF_SIZE
);
1200 /* perform the insertion */
1201 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1202 curs1
& M_EDIT_BUF_SIZE
]
1203 = (unsigned char) c
;
1205 /* update file length */
1208 /* update cursor position */
1213 edit_insert_over (WEdit
* edit
)
1217 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1218 edit_insert (edit
, ' ');
1223 /* same as edit_insert and move left */
1224 void edit_insert_ahead (WEdit
* edit
, int c
)
1226 if (edit
->last_byte
>= SIZE_LIMIT
)
1228 if (edit
->curs1
< edit
->start_display
) {
1229 edit
->start_display
++;
1233 edit_modification (edit
);
1235 if (edit
->book_mark
)
1236 book_mark_inc (edit
, edit
->curs_line
);
1237 edit
->total_lines
++;
1238 edit
->force
|= REDRAW_AFTER_CURSOR
;
1240 edit_push_action (edit
, DELCHAR
);
1242 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1243 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1244 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1246 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1247 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1248 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1255 int edit_delete (WEdit
* edit
, const int byte_delete
)
1264 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1265 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1266 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1269 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1270 if ( edit
->utf8
&& byte_delete
== 0 ) {
1271 edit_get_utf (edit
, edit
->curs1
, &cw
);
1275 for ( i
= 1; i
<= cw
; i
++ ) {
1276 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1278 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1279 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1280 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1284 edit_push_action (edit
, p
+ 256);
1287 edit_modification (edit
);
1289 if (edit
->book_mark
)
1290 book_mark_dec (edit
, edit
->curs_line
);
1291 edit
->total_lines
--;
1292 edit
->force
|= REDRAW_AFTER_CURSOR
;
1294 if (edit
->curs1
< edit
->start_display
) {
1295 edit
->start_display
--;
1305 edit_backspace (WEdit
* edit
, const int byte_delete
)
1314 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1315 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1316 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1319 if ( edit
->utf8
&& byte_delete
== 0 ) {
1320 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1324 for ( i
= 1; i
<= cw
; i
++ ) {
1325 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1326 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1327 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1328 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1332 edit_push_action (edit
, p
);
1334 edit_modification (edit
);
1336 if (edit
->book_mark
)
1337 book_mark_dec (edit
, edit
->curs_line
);
1339 edit
->total_lines
--;
1340 edit
->force
|= REDRAW_AFTER_CURSOR
;
1343 if (edit
->curs1
< edit
->start_display
) {
1344 edit
->start_display
--;
1352 #ifdef FAST_MOVE_CURSOR
1354 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1357 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1359 next
-= (unsigned long) dest
;
1367 edit_move_backward_lots (WEdit
*edit
, long increment
)
1370 unsigned char *p
= NULL
;
1372 if (increment
> edit
->curs1
)
1373 increment
= edit
->curs1
;
1376 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1378 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1381 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1385 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1386 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1391 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1392 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1393 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1394 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1397 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1398 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1399 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1404 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1406 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1408 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1409 g_malloc0 (EDIT_BUF_SIZE
);
1414 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1424 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1426 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1430 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1431 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1434 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1436 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1437 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1442 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1444 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1446 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1447 g_malloc0 (EDIT_BUF_SIZE
);
1452 return edit_get_byte (edit
, edit
->curs1
);
1455 #endif /* ! FAST_MOVE_CURSOR */
1457 /* moves the cursor right or left: increment positive or negative respectively */
1458 void edit_cursor_move (WEdit
* edit
, long increment
)
1460 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1462 #ifdef FAST_MOVE_CURSOR
1463 if (increment
< -256) {
1464 edit
->force
|= REDRAW_PAGE
;
1465 edit_move_backward_lots (edit
, -increment
);
1468 #endif /* ! FAST_MOVE_CURSOR */
1470 if (increment
< 0) {
1471 for (; increment
< 0; increment
++) {
1475 edit_push_action (edit
, CURS_RIGHT
);
1477 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1478 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1479 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1480 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1482 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1483 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1484 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1485 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1490 edit
->force
|= REDRAW_LINE_BELOW
;
1494 } else if (increment
> 0) {
1495 for (; increment
> 0; increment
--) {
1499 edit_push_action (edit
, CURS_LEFT
);
1501 c
= edit_get_byte (edit
, edit
->curs1
);
1502 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1503 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1504 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1506 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1507 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1508 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1509 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1514 edit
->force
|= REDRAW_LINE_ABOVE
;
1520 /* These functions return positions relative to lines */
1522 /* returns index of last char on line + 1 */
1523 long edit_eol (WEdit
* edit
, long current
)
1525 if (current
< edit
->last_byte
) {
1527 if (edit_get_byte (edit
, current
) == '\n')
1530 return edit
->last_byte
;
1534 /* returns index of first char on line */
1535 long edit_bol (WEdit
* edit
, long current
)
1539 if (edit_get_byte (edit
, current
- 1) == '\n')
1547 long edit_count_lines (WEdit
* edit
, long current
, long upto
)
1550 if (upto
> edit
->last_byte
)
1551 upto
= edit
->last_byte
;
1554 while (current
< upto
)
1555 if (edit_get_byte (edit
, current
++) == '\n')
1561 /* If lines is zero this returns the count of lines from current to upto. */
1562 /* If upto is zero returns index of lines forward current. */
1563 long edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1566 return edit_count_lines (edit
, current
, upto
);
1572 next
= edit_eol (edit
, current
) + 1;
1573 if (next
> edit
->last_byte
)
1583 /* Returns offset of 'lines' lines up from current */
1584 long edit_move_backward (WEdit
* edit
, long current
, long lines
)
1588 current
= edit_bol (edit
, current
);
1589 while((lines
--) && current
!= 0)
1590 current
= edit_bol (edit
, current
- 1);
1594 /* If cols is zero this returns the count of columns from current to upto. */
1595 /* If upto is zero returns index of cols across from current. */
1597 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1606 q
= edit
->last_byte
+ 2;
1608 for (col
= 0, p
= current
; p
< q
; p
++) {
1620 orig_c
= c
= edit_get_byte (edit
, p
);
1623 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1627 if (g_unichar_iswide (utf_ch
))
1629 } else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1633 c
= convert_to_display_c (c
);
1635 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1636 else if (c
== '\n') {
1641 } else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1642 /* '\r' is shown as ^M, so we must advance 2 characters */
1643 /* Caret notation for control characters */
1651 /* returns the current column position of the cursor */
1652 int edit_get_col (WEdit
* edit
)
1654 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1658 /* Scrolling functions */
1660 void edit_update_curs_row (WEdit
* edit
)
1662 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1665 void edit_update_curs_col (WEdit
* edit
)
1667 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1671 edit_get_curs_col (const WEdit
*edit
)
1673 return edit
->curs_col
;
1676 /*moves the display start position up by i lines */
1677 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1679 unsigned long lines_above
= edit
->start_line
;
1680 if (i
> lines_above
)
1683 edit
->start_line
-= i
;
1684 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1685 edit
->force
|= REDRAW_PAGE
;
1686 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1688 edit_update_curs_row (edit
);
1692 /* returns 1 if could scroll, 0 otherwise */
1693 void edit_scroll_downward (WEdit
* edit
, int i
)
1696 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1697 if (lines_below
> 0) {
1698 if (i
> lines_below
)
1700 edit
->start_line
+= i
;
1701 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1702 edit
->force
|= REDRAW_PAGE
;
1703 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1705 edit_update_curs_row (edit
);
1708 void edit_scroll_right (WEdit
* edit
, int i
)
1710 edit
->force
|= REDRAW_PAGE
;
1711 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1712 edit
->start_col
-= i
;
1715 void edit_scroll_left (WEdit
* edit
, int i
)
1717 if (edit
->start_col
) {
1718 edit
->start_col
+= i
;
1719 if (edit
->start_col
> 0)
1720 edit
->start_col
= 0;
1721 edit
->force
|= REDRAW_PAGE
;
1722 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1726 /* high level cursor movement commands */
1728 static int is_in_indent (WEdit
*edit
)
1730 long p
= edit_bol (edit
, edit
->curs1
);
1731 while (p
< edit
->curs1
)
1732 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1737 static int left_of_four_spaces (WEdit
*edit
);
1740 edit_move_to_prev_col (WEdit
* edit
, long p
)
1742 int prev
= edit
->prev_col
;
1743 int over
= edit
->over_col
;
1744 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1746 if (option_cursor_beyond_eol
) {
1747 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1749 if (line_len
< prev
+ edit
->over_col
) {
1750 edit
->over_col
= prev
+ over
- line_len
;
1751 edit
->prev_col
= line_len
;
1752 edit
->curs_col
= line_len
;
1754 edit
->curs_col
= prev
+ over
;
1755 edit
->prev_col
= edit
->curs_col
;
1760 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1761 edit_update_curs_col (edit
);
1763 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1764 int q
= edit
->curs_col
;
1765 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1766 p
= edit_bol (edit
, edit
->curs1
);
1767 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1768 if (!left_of_four_spaces (edit
))
1769 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1776 is_blank (WEdit
*edit
, long offset
)
1780 s
= edit_bol (edit
, offset
);
1781 f
= edit_eol (edit
, offset
) - 1;
1783 c
= edit_get_byte (edit
, s
++);
1791 /* returns the offset of line i */
1793 edit_find_line (WEdit
*edit
, int line
)
1797 if (!edit
->caches_valid
) {
1798 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1799 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1800 /* three offsets that we *know* are line 0 at 0 and these two: */
1801 edit
->line_numbers
[1] = edit
->curs_line
;
1802 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1803 edit
->line_numbers
[2] = edit
->total_lines
;
1804 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1805 edit
->caches_valid
= 1;
1807 if (line
>= edit
->total_lines
)
1808 return edit
->line_offsets
[2];
1811 /* find the closest known point */
1812 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1814 n
= abs (edit
->line_numbers
[i
] - line
);
1821 return edit
->line_offsets
[j
]; /* know the offset exactly */
1822 if (m
== 1 && j
>= 3)
1823 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1825 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1826 if (line
> edit
->line_numbers
[j
])
1827 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1829 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1830 edit
->line_numbers
[i
] = line
;
1831 return edit
->line_offsets
[i
];
1834 int line_is_blank (WEdit
* edit
, long line
)
1836 return is_blank (edit
, edit_find_line (edit
, line
));
1839 /* moves up until a blank line is reached, or until just
1840 before a non-blank line is reached */
1842 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
1845 if (edit
->curs_line
> 1) {
1846 if (line_is_blank (edit
, edit
->curs_line
)) {
1847 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1848 for (i
= edit
->curs_line
- 1; i
; i
--)
1849 if (!line_is_blank (edit
, i
)) {
1854 for (i
= edit
->curs_line
- 1; i
; i
--)
1855 if (line_is_blank (edit
, i
))
1859 for (i
= edit
->curs_line
- 1; i
; i
--)
1860 if (line_is_blank (edit
, i
))
1864 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
1867 /* moves down until a blank line is reached, or until just
1868 before a non-blank line is reached */
1870 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
1873 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1874 i
= edit
->total_lines
;
1876 if (line_is_blank (edit
, edit
->curs_line
)) {
1877 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1878 for (i
= edit
->curs_line
+ 1; i
; i
++)
1879 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1884 for (i
= edit
->curs_line
+ 1; i
; i
++)
1885 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1889 for (i
= edit
->curs_line
+ 1; i
; i
++)
1890 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1894 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
1897 static void edit_begin_page (WEdit
*edit
)
1899 edit_update_curs_row (edit
);
1900 edit_move_up (edit
, edit
->curs_row
, 0);
1903 static void edit_end_page (WEdit
*edit
)
1905 edit_update_curs_row (edit
);
1906 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1910 /* goto beginning of text */
1911 static void edit_move_to_top (WEdit
* edit
)
1913 if (edit
->curs_line
) {
1914 edit_cursor_move (edit
, -edit
->curs1
);
1915 edit_move_to_prev_col (edit
, 0);
1916 edit
->force
|= REDRAW_PAGE
;
1917 edit
->search_start
= 0;
1918 edit_update_curs_row(edit
);
1923 /* goto end of text */
1924 static void edit_move_to_bottom (WEdit
* edit
)
1926 if (edit
->curs_line
< edit
->total_lines
) {
1927 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1928 edit
->start_display
= edit
->last_byte
;
1929 edit
->start_line
= edit
->total_lines
;
1930 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1931 edit
->force
|= REDRAW_PAGE
;
1935 /* goto beginning of line */
1936 static void edit_cursor_to_bol (WEdit
* edit
)
1938 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1939 edit
->search_start
= edit
->curs1
;
1940 edit
->prev_col
= edit_get_col (edit
);
1944 /* goto end of line */
1945 static void edit_cursor_to_eol (WEdit
* edit
)
1947 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1948 edit
->search_start
= edit
->curs1
;
1949 edit
->prev_col
= edit_get_col (edit
);
1953 /* move cursor to line 'line' */
1954 void edit_move_to_line (WEdit
* e
, long line
)
1956 if(line
< e
->curs_line
)
1957 edit_move_up (e
, e
->curs_line
- line
, 0);
1959 edit_move_down (e
, line
- e
->curs_line
, 0);
1960 edit_scroll_screen_over_cursor (e
);
1963 /* scroll window so that first visible line is 'line' */
1964 void edit_move_display (WEdit
* e
, long line
)
1966 if(line
< e
->start_line
)
1967 edit_scroll_upward (e
, e
->start_line
- line
);
1969 edit_scroll_downward (e
, line
- e
->start_line
);
1972 /* save markers onto undo stack */
1973 void edit_push_markers (WEdit
* edit
)
1975 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1976 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1979 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1988 /* highlight marker toggle */
1989 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1991 edit_push_markers (edit
);
1993 edit_set_markers (edit
, 0, 0, 0, 0);
1994 edit
->force
|= REDRAW_PAGE
;
1996 if (edit
->mark2
>= 0) {
1997 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
1998 edit
->force
|= REDRAW_PAGE
;
2000 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
2004 static unsigned long
2009 const char option_chars_move_whole_word
[] =
2010 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2015 if (*option_chars_move_whole_word
== '!')
2017 return 0x80000000UL
;
2019 if (g_ascii_isupper ((gchar
) c
))
2021 else if (g_ascii_islower ((gchar
) c
))
2023 else if (g_ascii_isalpha (c
))
2025 else if (isdigit (c
))
2027 else if (isspace (c
))
2029 q
= strchr (option_chars_move_whole_word
, c
);
2031 return 0xFFFFFFFFUL
;
2033 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2037 } while ((q
= strchr (q
+ 1, c
)));
2042 edit_left_word_move (WEdit
*edit
, int s
)
2046 if (column_highlighting
2047 && edit
->mark1
!= edit
->mark2
2048 && edit
->over_col
== 0
2049 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2051 edit_cursor_move (edit
, -1);
2054 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2055 c2
= edit_get_byte (edit
, edit
->curs1
);
2056 if (!(my_type_of (c1
) & my_type_of (c2
)))
2058 if (isspace (c1
) && !isspace (c2
))
2061 if (!isspace (c1
) && isspace (c2
))
2066 static void edit_left_word_move_cmd (WEdit
* edit
)
2068 edit_left_word_move (edit
, 0);
2069 edit
->force
|= REDRAW_PAGE
;
2073 edit_right_word_move (WEdit
*edit
, int s
)
2077 if (column_highlighting
2078 && edit
->mark1
!= edit
->mark2
2079 && edit
->over_col
== 0
2080 && edit
->curs1
== edit_eol(edit
, edit
->curs1
)
2083 edit_cursor_move (edit
, 1);
2084 if (edit
->curs1
>= edit
->last_byte
)
2086 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2087 c2
= edit_get_byte (edit
, edit
->curs1
);
2088 if (!(my_type_of (c1
) & my_type_of (c2
)))
2090 if (isspace (c1
) && !isspace (c2
))
2093 if (!isspace (c1
) && isspace (c2
))
2098 static void edit_right_word_move_cmd (WEdit
* edit
)
2100 edit_right_word_move (edit
, 0);
2101 edit
->force
|= REDRAW_PAGE
;
2104 static void edit_right_char_move_cmd (WEdit
* edit
)
2109 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2113 c
= edit_get_byte (edit
, edit
->curs1
);
2115 if (option_cursor_beyond_eol
&& c
== '\n') {
2118 edit_cursor_move (edit
, cw
);
2122 static void edit_left_char_move_cmd (WEdit
* edit
)
2125 if (column_highlighting
2126 && option_cursor_beyond_eol
2127 && edit
->mark1
!= edit
->mark2
2128 && edit
->over_col
== 0
2129 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2132 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2136 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2139 edit_cursor_move (edit
, -cw
);
2143 /** Up or down cursor moving.
2144 direction = TRUE - move up
2148 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2151 unsigned long l
= (direction
)
2153 : edit
->total_lines
- edit
->curs_line
;
2162 edit
->force
|= REDRAW_PAGE
;
2165 edit_scroll_upward (edit
, i
);
2167 edit_scroll_downward (edit
, i
);
2169 p
= edit_bol (edit
, edit
->curs1
);
2172 ? edit_move_backward (edit
, p
, i
)
2173 : edit_move_forward (edit
, p
, i
, 0);
2175 edit_cursor_move (edit
, p
- edit
->curs1
);
2177 edit_move_to_prev_col (edit
, p
);
2179 /* search start of current multibyte char (like CJK) */
2180 if (edit
->curs1
+ 1 < edit
->last_byte
) {
2181 edit_right_char_move_cmd (edit
);
2182 edit_left_char_move_cmd (edit
);
2185 edit
->search_start
= edit
->curs1
;
2186 edit
->found_len
= 0;
2189 static void edit_right_delete_word (WEdit
* edit
)
2193 if (edit
->curs1
>= edit
->last_byte
)
2195 c1
= edit_delete (edit
, 1);
2196 c2
= edit_get_byte (edit
, edit
->curs1
);
2197 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2199 if (!(my_type_of (c1
) & my_type_of (c2
)))
2204 static void edit_left_delete_word (WEdit
* edit
)
2208 if (edit
->curs1
<= 0)
2210 c1
= edit_backspace (edit
, 1);
2211 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2212 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2214 if (!(my_type_of (c1
) & my_type_of (c2
)))
2220 the start column position is not recorded, and hence does not
2221 undo as it happed. But who would notice.
2224 edit_do_undo (WEdit
* edit
)
2229 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2231 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2236 edit_cursor_move (edit
, 1);
2239 edit_cursor_move (edit
, -1);
2242 edit_backspace (edit
, 1);
2245 edit_delete (edit
, 1);
2248 column_highlighting
= 1;
2251 column_highlighting
= 0;
2254 if (ac
>= 256 && ac
< 512)
2255 edit_insert_ahead (edit
, ac
- 256);
2256 if (ac
>= 0 && ac
< 256)
2257 edit_insert (edit
, ac
);
2259 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2260 edit
->mark1
= ac
- MARK_1
;
2261 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2262 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2263 edit
->mark2
= ac
- MARK_2
;
2264 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2267 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2270 if (edit
->start_display
> ac
- KEY_PRESS
) {
2271 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2272 edit
->force
|= REDRAW_PAGE
;
2273 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2274 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2275 edit
->force
|= REDRAW_PAGE
;
2277 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2278 edit_update_curs_row (edit
);
2281 edit
->stack_disable
= 0;
2284 static void edit_delete_to_line_end (WEdit
* edit
)
2286 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2289 edit_delete (edit
, 1);
2293 static void edit_delete_to_line_begin (WEdit
* edit
)
2295 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2298 edit_backspace (edit
, 1);
2303 edit_delete_line (WEdit
*edit
)
2306 * Delete right part of the line.
2307 * Note that edit_get_byte() returns '\n' when byte position is
2310 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2311 (void) edit_delete (edit
, 1);
2316 * Note that edit_delete() will not corrupt anything if called while
2317 * cursor position is EOF.
2319 (void) edit_delete (edit
, 1);
2322 * Delete left part of the line.
2323 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2325 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2326 (void) edit_backspace (edit
, 1);
2330 void insert_spaces_tab (WEdit
* edit
, int half
)
2333 edit_update_curs_col (edit
);
2334 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2336 edit_insert (edit
, ' ');
2341 static int is_aligned_on_a_tab (WEdit
* edit
)
2343 edit_update_curs_col (edit
);
2344 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2345 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2348 static int right_of_four_spaces (WEdit
*edit
)
2351 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2352 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2354 return is_aligned_on_a_tab (edit
);
2358 static int left_of_four_spaces (WEdit
*edit
)
2361 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2362 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2364 return is_aligned_on_a_tab (edit
);
2368 int edit_indent_width (WEdit
* edit
, long p
)
2371 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2373 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2376 void edit_insert_indent (WEdit
* edit
, int indent
)
2378 if (!option_fill_tabs_with_spaces
) {
2379 while (indent
>= TAB_SIZE
) {
2380 edit_insert (edit
, '\t');
2384 while (indent
-- > 0)
2385 edit_insert (edit
, ' ');
2389 edit_auto_indent (WEdit
* edit
)
2394 /* use the previous line as a template */
2395 p
= edit_move_backward (edit
, p
, 1);
2396 /* copy the leading whitespace of the line */
2397 for (;;) { /* no range check - the line _is_ \n-terminated */
2398 c
= edit_get_byte (edit
, p
++);
2399 if (c
!= ' ' && c
!= '\t')
2401 edit_insert (edit
, c
);
2406 edit_double_newline (WEdit
* edit
)
2408 edit_insert (edit
, '\n');
2409 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2411 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2413 edit
->force
|= REDRAW_PAGE
;
2414 edit_insert (edit
, '\n');
2418 edit_tab_cmd (WEdit
* edit
)
2422 if (option_fake_half_tabs
) {
2423 if (is_in_indent (edit
)) {
2424 /*insert a half tab (usually four spaces) unless there is a
2425 half tab already behind, then delete it and insert a
2427 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2428 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2429 edit_backspace (edit
, 1);
2430 edit_insert (edit
, '\t');
2432 insert_spaces_tab (edit
, 1);
2437 if (option_fill_tabs_with_spaces
) {
2438 insert_spaces_tab (edit
, 0);
2440 edit_insert (edit
, '\t');
2444 static void check_and_wrap_line (WEdit
* edit
)
2447 if (!option_typewriter_wrap
)
2449 edit_update_curs_col (edit
);
2450 if (edit
->curs_col
< option_word_wrap_line_length
)
2455 c
= edit_get_byte (edit
, curs
);
2456 if (c
== '\n' || curs
<= 0) {
2457 edit_insert (edit
, '\n');
2460 if (c
== ' ' || c
== '\t') {
2461 int current
= edit
->curs1
;
2462 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2463 edit_insert (edit
, '\n');
2464 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2470 static inline void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2472 void edit_push_key_press (WEdit
* edit
)
2474 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2475 if (edit
->mark2
== -1)
2476 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2479 /* this find the matching bracket in either direction, and sets edit->bracket */
2480 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2482 const char * const b
= "{}{[][()(", *p
;
2483 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2484 unsigned long j
= 0;
2486 edit_update_curs_row (edit
);
2487 c
= edit_get_byte (edit
, edit
->curs1
);
2490 if (!furthest_bracket_search
)
2491 furthest_bracket_search
--;
2492 /* not on a bracket at all */
2495 /* the matching bracket */
2497 /* going left or right? */
2498 if (strchr ("{[(", c
))
2500 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2501 /* out of buffer? */
2502 if (q
>= edit
->last_byte
|| q
< 0)
2504 a
= edit_get_byte (edit
, q
);
2505 /* don't want to eat CPU */
2506 if (j
++ > furthest_bracket_search
)
2508 /* out of screen? */
2510 if (q
< edit
->start_display
)
2512 /* count lines if searching downward */
2513 if (inc
> 0 && a
== '\n')
2514 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2517 /* count bracket depth */
2518 i
+= (a
== c
) - (a
== d
);
2519 /* return if bracket depth is zero */
2527 static long last_bracket
= -1;
2529 void edit_find_bracket (WEdit
* edit
)
2531 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2532 if (last_bracket
!= edit
->bracket
)
2533 edit
->force
|= REDRAW_PAGE
;
2534 last_bracket
= edit
->bracket
;
2538 edit_goto_matching_bracket (WEdit
*edit
)
2542 q
= edit_get_bracket (edit
, 0, 0);
2544 edit
->bracket
= edit
->curs1
;
2545 edit
->force
|= REDRAW_PAGE
;
2546 edit_cursor_move (edit
, q
- edit
->curs1
);
2551 * This executes a command as though the user initiated it through a key
2552 * press. Callback with WIDGET_KEY as a message calls this after
2553 * translating the key press. This function can be used to pass any
2554 * command to the editor. Note that the screen wouldn't update
2555 * automatically. Either of command or char_for_insertion must be
2556 * passed as -1. Commands are executed, and char_for_insertion is
2557 * inserted at the cursor.
2560 edit_execute_key_command (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2562 if (command
== CK_Begin_Record_Macro
) {
2564 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2567 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2568 edit
->force
|= REDRAW_COMPLETELY
;
2569 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2573 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2574 edit
->macro
[edit
->macro_i
].command
= command
;
2575 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2577 /* record the beginning of a set of editing actions initiated by a key press */
2578 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2579 edit_push_key_press (edit
);
2581 edit_execute_cmd (edit
, command
, char_for_insertion
);
2582 if (column_highlighting
)
2583 edit
->force
|= REDRAW_PAGE
;
2586 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2589 This executes a command at a lower level than macro recording.
2590 It also does not push a key_press onto the undo stack. This means
2591 that if it is called many times, a single undo command will undo
2592 all of them. It also does not check for the Undo command.
2595 edit_execute_cmd (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2597 edit
->force
|= REDRAW_LINE
;
2599 /* The next key press will unhighlight the found string, so update
2601 if (edit
->found_len
|| column_highlighting
)
2602 edit
->force
|= REDRAW_PAGE
;
2604 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2605 column_highlighting
= 0;
2606 if (!edit
->highlight
2607 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2608 edit_mark_cmd (edit
, 1); /* clear */
2609 edit_mark_cmd (edit
, 0); /* marking on */
2611 edit
->highlight
= 1;
2612 } else { /* any other command */
2613 if (edit
->highlight
)
2614 edit_mark_cmd (edit
, 0); /* clear */
2615 edit
->highlight
= 0;
2618 /* first check for undo */
2619 if (command
== CK_Undo
) {
2620 edit_do_undo (edit
);
2621 edit
->found_len
= 0;
2622 edit
->prev_col
= edit_get_col (edit
);
2623 edit
->search_start
= edit
->curs1
;
2627 /* An ordinary key press */
2628 if (char_for_insertion
>= 0) {
2629 if (edit
->overwrite
) {
2630 /* remove char only one time, after input first byte, multibyte chars */
2631 if ((!utf8_display
|| edit
->charpoint
== 0) && edit_get_byte (edit
, edit
->curs1
) != '\n')
2632 edit_delete (edit
, 0);
2634 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2635 edit_insert_over (edit
);
2637 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2638 unsigned char str
[6 + 1];
2640 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2647 while ( str
[i
] != 0 && i
<=6) {
2648 char_for_insertion
= str
[i
];
2649 edit_insert (edit
, char_for_insertion
);
2654 edit_insert (edit
, char_for_insertion
);
2656 if (option_auto_para_formatting
) {
2657 format_paragraph (edit
, 0);
2658 edit
->force
|= REDRAW_PAGE
;
2660 check_and_wrap_line (edit
);
2661 edit
->found_len
= 0;
2662 edit
->prev_col
= edit_get_col (edit
);
2663 edit
->search_start
= edit
->curs1
;
2664 edit_find_bracket (edit
);
2671 case CK_Begin_Page_Highlight
:
2672 case CK_End_Page_Highlight
:
2679 if ( edit
->mark2
>= 0 ) {
2680 if ( !option_persistent_selections
) {
2681 if (column_highlighting
)
2682 edit_push_action (edit
, COLUMN_ON
);
2683 column_highlighting
= 0;
2684 edit_mark_cmd (edit
, 1);
2692 case CK_Begin_Page_Highlight
:
2693 case CK_End_Page_Highlight
:
2698 case CK_Word_Left_Highlight
:
2699 case CK_Word_Right_Highlight
:
2700 case CK_Up_Highlight
:
2701 case CK_Down_Highlight
:
2702 case CK_Up_Alt_Highlight
:
2703 case CK_Down_Alt_Highlight
:
2704 if (edit
->mark2
== -1)
2705 break; /*marking is following the cursor: may need to highlight a whole line */
2708 case CK_Left_Highlight
:
2709 case CK_Right_Highlight
:
2710 edit
->force
|= REDRAW_CHAR_ONLY
;
2713 /* basic cursor key commands */
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
);
2723 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2727 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2728 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2730 edit_backspace (edit
, 1);
2733 if (option_fake_half_tabs
) {
2735 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2736 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2737 edit_backspace (edit
, 1);
2742 edit_backspace (edit
, 0);
2745 /* if non persistent selection and text selected */
2746 if ( !option_persistent_selections
) {
2747 if ( edit
->mark1
!= edit
->mark2
) {
2748 edit_block_delete_cmd (edit
);
2753 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2754 edit_insert_over (edit
);
2756 if (option_fake_half_tabs
) {
2758 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2759 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2760 edit_delete (edit
, 1);
2764 edit_delete (edit
, 0);
2766 case CK_Delete_Word_Left
:
2768 edit_left_delete_word (edit
);
2770 case CK_Delete_Word_Right
:
2771 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2772 edit_insert_over (edit
);
2774 edit_right_delete_word (edit
);
2776 case CK_Delete_Line
:
2777 edit_delete_line (edit
);
2779 case CK_Delete_To_Line_End
:
2780 edit_delete_to_line_end (edit
);
2782 case CK_Delete_To_Line_Begin
:
2783 edit_delete_to_line_begin (edit
);
2787 if (option_auto_para_formatting
) {
2788 edit_double_newline (edit
);
2789 if (option_return_does_auto_indent
)
2790 edit_auto_indent (edit
);
2791 format_paragraph (edit
, 0);
2793 edit_insert (edit
, '\n');
2794 if (option_return_does_auto_indent
) {
2795 edit_auto_indent (edit
);
2800 edit_insert (edit
, '\n');
2803 case CK_Page_Up_Alt_Highlight
:
2804 column_highlighting
= 1;
2806 case CK_Page_Up_Highlight
:
2807 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2809 case CK_Page_Down_Alt_Highlight
:
2810 column_highlighting
= 1;
2812 case CK_Page_Down_Highlight
:
2813 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2815 case CK_Left_Alt_Highlight
:
2816 column_highlighting
= 1;
2818 case CK_Left_Highlight
:
2819 if (option_fake_half_tabs
) {
2820 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2821 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2824 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2825 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2829 edit_left_char_move_cmd (edit
);
2831 case CK_Right_Alt_Highlight
:
2832 column_highlighting
= 1;
2834 case CK_Right_Highlight
:
2835 if (option_fake_half_tabs
) {
2836 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2837 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2838 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2842 edit_right_char_move_cmd (edit
);
2845 case CK_Begin_Page_Highlight
:
2846 edit_begin_page (edit
);
2849 case CK_End_Page_Highlight
:
2850 edit_end_page (edit
);
2853 case CK_Word_Left_Highlight
:
2855 edit_left_word_move_cmd (edit
);
2858 case CK_Word_Right_Highlight
:
2860 edit_right_word_move_cmd (edit
);
2862 case CK_Up_Alt_Highlight
:
2863 column_highlighting
= 1;
2865 case CK_Up_Highlight
:
2866 edit_move_up (edit
, 1, 0);
2868 case CK_Down_Alt_Highlight
:
2869 column_highlighting
= 1;
2871 case CK_Down_Highlight
:
2872 edit_move_down (edit
, 1, 0);
2874 case CK_Paragraph_Up_Alt_Highlight
:
2875 column_highlighting
= 1;
2876 case CK_Paragraph_Up
:
2877 case CK_Paragraph_Up_Highlight
:
2878 edit_move_up_paragraph (edit
, 0);
2880 case CK_Paragraph_Down_Alt_Highlight
:
2881 column_highlighting
= 1;
2882 case CK_Paragraph_Down
:
2883 case CK_Paragraph_Down_Highlight
:
2884 edit_move_down_paragraph (edit
, 0);
2886 case CK_Scroll_Up_Alt_Highlight
:
2887 column_highlighting
= 1;
2889 case CK_Scroll_Up_Highlight
:
2890 edit_move_up (edit
, 1, 1);
2892 case CK_Scroll_Down_Alt_Highlight
:
2893 column_highlighting
= 1;
2894 case CK_Scroll_Down
:
2895 case CK_Scroll_Down_Highlight
:
2896 edit_move_down (edit
, 1, 1);
2899 case CK_Home_Highlight
:
2900 edit_cursor_to_bol (edit
);
2903 case CK_End_Highlight
:
2904 edit_cursor_to_eol (edit
);
2907 /* if text marked shift block */
2908 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2909 if (edit
->mark2
< 0)
2910 edit_mark_cmd (edit
, 0);
2911 edit_move_block_to_right (edit
);
2913 if ( option_cursor_beyond_eol
)
2914 edit_insert_over (edit
);
2915 edit_tab_cmd (edit
);
2916 if (option_auto_para_formatting
) {
2917 format_paragraph (edit
, 0);
2918 edit
->force
|= REDRAW_PAGE
;
2920 check_and_wrap_line (edit
);
2925 case CK_Toggle_Insert
:
2926 edit
->overwrite
= (edit
->overwrite
== 0);
2930 if (edit
->mark2
>= 0) {
2931 if (column_highlighting
)
2932 edit_push_action (edit
, COLUMN_ON
);
2933 column_highlighting
= 0;
2935 edit_mark_cmd (edit
, 0);
2937 case CK_Column_Mark
:
2938 if (!column_highlighting
)
2939 edit_push_action (edit
, COLUMN_OFF
);
2940 column_highlighting
= 1;
2941 edit_mark_cmd (edit
, 0);
2944 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
2945 edit
->force
|= REDRAW_PAGE
;
2948 if (column_highlighting
)
2949 edit_push_action (edit
, COLUMN_ON
);
2950 column_highlighting
= 0;
2951 edit_mark_cmd (edit
, 1);
2954 case CK_Toggle_Line_State
:
2955 option_line_state
= !option_line_state
;
2956 if ( option_line_state
) {
2957 option_line_state_width
= LINE_STATE_WIDTH
;
2959 option_line_state_width
= 0;
2961 edit
->force
|= REDRAW_PAGE
;
2964 case CK_Toggle_Bookmark
:
2965 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2966 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2967 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2969 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2971 case CK_Flush_Bookmarks
:
2972 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2973 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2974 edit
->force
|= REDRAW_PAGE
;
2976 case CK_Next_Bookmark
:
2977 if (edit
->book_mark
) {
2978 struct _book_mark
*p
;
2979 p
= (struct _book_mark
*) book_mark_find (edit
,
2983 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2984 || p
->line
< edit
->start_line
)
2985 edit_move_display (edit
,
2987 edit
->num_widget_lines
/ 2);
2988 edit_move_to_line (edit
, p
->line
);
2992 case CK_Prev_Bookmark
:
2993 if (edit
->book_mark
) {
2994 struct _book_mark
*p
;
2995 p
= (struct _book_mark
*) book_mark_find (edit
,
2997 while (p
->line
== edit
->curs_line
)
3001 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3002 || p
->line
< edit
->start_line
)
3003 edit_move_display (edit
,
3005 edit
->num_widget_lines
/ 2);
3006 edit_move_to_line (edit
, p
->line
);
3011 case CK_Beginning_Of_Text
:
3012 case CK_Beginning_Of_Text_Highlight
:
3013 edit_move_to_top (edit
);
3015 case CK_End_Of_Text
:
3016 case CK_End_Of_Text_Highlight
:
3017 edit_move_to_bottom (edit
);
3021 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3022 edit_insert_over (edit
);
3023 edit_block_copy_cmd (edit
);
3026 edit_block_delete_cmd (edit
);
3029 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3030 edit_insert_over (edit
);
3031 edit_block_move_cmd (edit
);
3034 case CK_Shift_Block_Left
:
3035 if (edit
->mark1
!= edit
->mark2
)
3036 edit_move_block_to_left (edit
);
3038 case CK_Shift_Block_Right
:
3039 if (edit
->mark1
!= edit
->mark2
)
3040 edit_move_block_to_right (edit
);
3043 edit_copy_to_X_buf_cmd (edit
);
3046 edit_cut_to_X_buf_cmd (edit
);
3049 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3050 edit_insert_over (edit
);
3051 edit_paste_from_X_buf_cmd (edit
);
3053 case CK_Selection_History
:
3054 edit_paste_from_history (edit
);
3058 edit_save_as_cmd (edit
);
3061 edit_save_confirm_cmd (edit
);
3064 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3067 edit_save_block_cmd (edit
);
3069 case CK_Insert_File
:
3070 edit_insert_file_cmd (edit
);
3073 case CK_Load_Prev_File
:
3074 edit_load_back_cmd (edit
);
3076 case CK_Load_Next_File
:
3077 edit_load_forward_cmd (edit
);
3080 case CK_Load_Syntax_File
:
3081 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3083 case CK_Choose_Syntax
:
3084 edit_syntax_dialog ();
3087 case CK_Load_Menu_File
:
3088 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3091 case CK_Toggle_Syntax
:
3092 if ((option_syntax_highlighting
^= 1) == 1)
3093 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3094 edit
->force
|= REDRAW_PAGE
;
3097 case CK_Toggle_Tab_TWS
:
3098 enable_show_tabs_tws
^= 1;
3099 edit
->force
|= REDRAW_PAGE
;
3103 edit_search_cmd (edit
, 0);
3106 edit_search_cmd (edit
, 1);
3109 edit_replace_cmd (edit
, 0);
3111 case CK_Replace_Again
:
3112 edit_replace_cmd (edit
, 1);
3114 case CK_Complete_Word
:
3115 /* if text marked shift block */
3116 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3117 edit_move_block_to_left (edit
);
3119 edit_complete_word_cmd (edit
);
3122 case CK_Find_Definition
:
3123 edit_get_match_keyword_cmd (edit
);
3126 dlg_stop (edit
->widget
.parent
);
3129 edit_new_cmd (edit
);
3132 edit_help_cmd (edit
);
3135 edit_refresh_cmd (edit
);
3137 case CK_SaveSetupCmd
:
3141 query_dialog (_(" About "),
3142 _("\n Cooledit v3.11.5\n\n"
3143 " Copyright (C) 1996 the Free Software Foundation\n\n"
3144 " A user friendly text editor written\n"
3145 " for the Midnight Commander.\n"), D_NORMAL
,
3151 case CK_Edit_Options
:
3152 edit_options_dialog ();
3154 case CK_Edit_Save_Mode
:
3155 menu_save_mode_cmd ();
3160 /* fool gcc to prevent a Y2K warning */
3161 char time_format
[] = "_c";
3162 time_format
[0] = '%';
3164 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3165 edit_print_string (edit
, s
);
3166 edit
->force
|= REDRAW_PAGE
;
3171 edit_goto_cmd (edit
);
3173 case CK_Paragraph_Format
:
3174 format_paragraph (edit
, 1);
3175 edit
->force
|= REDRAW_PAGE
;
3177 case CK_Delete_Macro
:
3178 edit_delete_macro_cmd (edit
);
3180 case CK_Match_Bracket
:
3181 edit_goto_matching_bracket (edit
);
3187 edit_sort_cmd (edit
);
3190 edit_ext_cmd (edit
);
3193 edit_mail_dialog (edit
);
3198 case CK_SelectCodepage
:
3199 edit_select_codepage_cmd (edit
);
3201 case CK_Insert_Literal
:
3202 edit_insert_literal_cmd (edit
);
3204 case CK_Execute_Macro
:
3205 edit_execute_macro_cmd (edit
);
3207 case CK_Begin_End_Macro
:
3208 edit_begin_end_macro_cmd (edit
);
3218 if ((command
/ 1000) == 1) /* a shell command */
3219 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3220 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3221 struct macro m
[MAX_MACRO_LENGTH
];
3223 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3224 edit_execute_macro (edit
, m
, nm
);
3227 /* keys which must set the col position, and the search vars */
3232 case CK_Replace_Again
:
3233 case CK_Complete_Word
:
3234 edit
->prev_col
= edit_get_col (edit
);
3237 case CK_Up_Highlight
:
3238 case CK_Up_Alt_Highlight
:
3240 case CK_Down_Highlight
:
3241 case CK_Down_Alt_Highlight
:
3243 case CK_Page_Up_Highlight
:
3244 case CK_Page_Up_Alt_Highlight
:
3246 case CK_Page_Down_Highlight
:
3247 case CK_Page_Down_Alt_Highlight
:
3248 case CK_Beginning_Of_Text
:
3249 case CK_Beginning_Of_Text_Highlight
:
3250 case CK_End_Of_Text
:
3251 case CK_End_Of_Text_Highlight
:
3252 case CK_Paragraph_Up
:
3253 case CK_Paragraph_Up_Highlight
:
3254 case CK_Paragraph_Up_Alt_Highlight
:
3255 case CK_Paragraph_Down
:
3256 case CK_Paragraph_Down_Highlight
:
3257 case CK_Paragraph_Down_Alt_Highlight
:
3259 case CK_Scroll_Up_Highlight
:
3260 case CK_Scroll_Up_Alt_Highlight
:
3261 case CK_Scroll_Down
:
3262 case CK_Scroll_Down_Highlight
:
3263 case CK_Scroll_Down_Alt_Highlight
:
3264 edit
->search_start
= edit
->curs1
;
3265 edit
->found_len
= 0;
3268 edit
->found_len
= 0;
3269 edit
->prev_col
= edit_get_col (edit
);
3270 edit
->search_start
= edit
->curs1
;
3272 edit_find_bracket (edit
);
3274 if (option_auto_para_formatting
) {
3278 case CK_Delete_Word_Left
:
3279 case CK_Delete_Word_Right
:
3280 case CK_Delete_To_Line_End
:
3281 case CK_Delete_To_Line_Begin
:
3282 format_paragraph (edit
, 0);
3283 edit
->force
|= REDRAW_PAGE
;
3290 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3294 if (edit
->macro_depth
++ > 256) {
3295 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3296 edit
->macro_depth
--;
3299 edit
->force
|= REDRAW_PAGE
;
3300 for (; i
< n
; i
++) {
3301 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3303 edit_update_screen (edit
);
3304 edit
->macro_depth
--;
3307 /* User edit menu, like user menu (F2) but only in editor. */
3309 user_menu (WEdit
* edit
)
3313 long start_mark
, end_mark
;
3316 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3318 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3320 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3322 /* run shell scripts from menu */
3323 user_menu_cmd (edit
);
3325 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0)) {
3330 /* i.e. we have marked block */
3331 rc
= edit_block_delete_cmd (edit
);
3335 edit_insert_file (edit
, block_file
);
3337 /* truncate block file */
3338 fd
= fopen (block_file
, "w");
3342 edit_refresh_cmd (edit
);
3343 edit
->force
|= REDRAW_COMPLETELY
;
3345 g_free (block_file
);
3349 edit_stack_init (void)
3351 for (edit_stack_iterator
= 0;
3352 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3353 edit_stack_iterator
++ ) {
3354 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3355 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3358 edit_stack_iterator
= 0;
3362 edit_stack_free (void)
3364 for (edit_stack_iterator
= 0;
3365 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3366 edit_stack_iterator
++)
3367 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3372 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3374 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3379 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3381 edit_move_updown (edit
, i
, do_scroll
, FALSE
);