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;
87 int show_right_margin
= 0;
89 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext
= NULL
;
92 int edit_stack_iterator
= 0;
93 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC
[] = {'\1', '\1', '\1', '\1', '\n'};
98 * here's a quick sketch of the layout: (don't run this through indent.)
100 * (b1 is buffers1 and b2 is buffers2)
103 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
104 * ______________________________________|______________________________________
106 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
107 * |-> |-> |-> |-> |-> |-> |
109 * _<------------------------->|<----------------->_
110 * WEdit->curs2 | WEdit->curs1
115 * file end|||file beginning
124 const global_keymap_t
*editor_map
;
125 const global_keymap_t
*editor_x_map
;
127 static void user_menu (WEdit
*edit
);
129 int edit_get_byte (WEdit
* edit
, long byte_index
)
132 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
135 if (byte_index
>= edit
->curs1
) {
136 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
137 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
139 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
143 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
146 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
149 if (byte_index
>= edit
->curs1
) {
150 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
151 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
]+(EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
153 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
]+(byte_index
& M_EDIT_BUF_SIZE
));
157 char *edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
161 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
167 if (byte_index
>= edit
->curs1
) {
168 p
= edit
->curs1
+ edit
->curs2
- 1;
169 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
171 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
175 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
180 gchar
*next_ch
= NULL
;
183 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
188 str
= edit_get_byte_ptr (edit
, byte_index
);
195 res
= g_utf8_get_char_validated (str
, -1);
202 /* Calculate UTF-8 char width */
203 next_ch
= g_utf8_next_char (str
);
205 width
= next_ch
- str
;
215 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
217 gchar
*str
, *buf
= NULL
;
220 gchar
*next_ch
= NULL
;
226 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
231 ch
= edit_get_utf (edit
, byte_index
, &width
);
238 str
= edit_get_byte_ptr (edit
, byte_index
);
239 buf
= edit_get_buf_ptr (edit
, byte_index
);
240 if (str
== NULL
|| buf
== NULL
) {
244 /* get prev utf8 char */
246 str
= g_utf8_find_prev_char (buf
, str
);
248 res
= g_utf8_get_char_validated (str
, -1);
255 /* Calculate UTF-8 char width */
256 next_ch
= g_utf8_next_char(str
);
258 width
= next_ch
- str
;
269 * Initialize the buffers for an empty files.
272 edit_init_buffers (WEdit
*edit
)
276 for (j
= 0; j
<= MAXBUFF
; j
++) {
277 edit
->buffers1
[j
] = NULL
;
278 edit
->buffers2
[j
] = NULL
;
283 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
287 * Load file OR text into buffers. Set cursor to the beginning of file.
291 edit_load_file_fast (WEdit
*edit
, const char *filename
)
295 edit
->curs2
= edit
->last_byte
;
296 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
298 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
299 GString
*errmsg
= g_string_new(NULL
);
300 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
301 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
302 g_string_free (errmsg
, TRUE
);
306 if (!edit
->buffers2
[buf2
])
307 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
310 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
311 (edit
->curs2
& M_EDIT_BUF_SIZE
),
312 edit
->curs2
& M_EDIT_BUF_SIZE
);
314 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
315 /* edit->buffers2[0] is already allocated */
316 if (!edit
->buffers2
[buf
])
317 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
318 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
325 /* detecting an error on save is easy: just check if every byte has been written. */
326 /* detecting an error on read, is not so easy 'cos there is not way to tell
327 whether you read everything or not. */
328 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
329 static const struct edit_filters
{
330 const char *read
, *write
, *extension
;
332 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
333 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
334 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
335 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
336 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
339 /* Return index of the filter or -1 is there is no appropriate filter */
340 static int edit_find_filter (const char *filename
)
344 if (filename
== NULL
)
347 l
= strlen (filename
);
348 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
349 e
= strlen (all_filters
[i
].extension
);
351 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
358 edit_get_filter (const char *filename
)
361 char *p
, *quoted_name
;
363 i
= edit_find_filter (filename
);
367 quoted_name
= name_quote (filename
, 0);
368 p
= g_strdup_printf(all_filters
[i
].read
, quoted_name
);
369 g_free (quoted_name
);
374 edit_get_write_filter (const char *write_name
, const char *filename
)
379 i
= edit_find_filter (filename
);
383 writename
= name_quote (write_name
, 0);
384 p
= g_strdup_printf(all_filters
[i
].write
, writename
);
390 edit_insert_stream (WEdit
* edit
, FILE * f
)
394 while ((c
= fgetc (f
)) >= 0) {
395 edit_insert (edit
, c
);
401 long edit_write_stream (WEdit
* edit
, FILE * f
)
405 if (edit
->lb
== LB_ASIS
) {
406 for (i
= 0; i
< edit
->last_byte
; i
++)
407 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
412 /* change line breaks */
413 for (i
= 0; i
< edit
->last_byte
; i
++) {
414 unsigned char c
= edit_get_byte (edit
, i
);
416 if (!(c
== '\n' || c
== '\r')) {
418 if (fputc (c
, f
) < 0)
420 } else { /* (c == '\n' || c == '\r') */
421 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
424 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
425 /* put one line break unconditionally */
426 if (fputc ('\n', f
) < 0)
429 i
++; /* 2 chars are processed */
431 if (c
== '\r' && c1
== '\n')
432 /* Windows line break; go to the next char */
435 if (c
== '\r' && c1
== '\r') {
436 /* two Macintosh line breaks; put second line break */
437 if (fputc ('\n', f
) < 0)
442 if (fputc (c1
, f
) < 0)
446 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
447 /* put one line break unconditionally */
448 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
451 if (c
== '\r' && c1
== '\n')
452 /* Windows line break; go to the next char */
456 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
457 /* put one line break unconditionally */
458 if (fputc ('\r', f
) < 0)
461 i
++; /* 2 chars are processed */
463 if (c
== '\r' && c1
== '\n')
464 /* Windows line break; go to the next char */
467 if (c
== '\n' && c1
== '\n') {
468 /* two Windows line breaks; put second line break */
469 if (fputc ('\r', f
) < 0)
474 if (fputc (c1
, f
) < 0)
477 case LB_ASIS
: /* default without changes */
483 return edit
->last_byte
;
486 #define TEMP_BUF_LEN 1024
488 /* inserts a file at the cursor, returns 1 on success */
490 edit_insert_file (WEdit
*edit
, const char *filename
)
494 p
= edit_get_filter (filename
);
497 long current
= edit
->curs1
;
498 f
= (FILE *) popen (p
, "r");
500 edit_insert_stream (edit
, f
);
501 edit_cursor_move (edit
, current
- edit
->curs1
);
502 if (pclose (f
) > 0) {
504 errmsg
= g_strdup_printf(_(" Error reading from pipe: %s "), p
);
505 edit_error_dialog (_("Error"), errmsg
);
512 errmsg
= g_strdup_printf (_(" Cannot open pipe for reading: %s "), p
);
513 edit_error_dialog (_("Error"), errmsg
);
520 int i
, file
, blocklen
;
521 long current
= edit
->curs1
;
522 int vertical_insertion
= 0;
524 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
526 buf
= g_malloc0 (TEMP_BUF_LEN
);
527 blocklen
= mc_read (file
, buf
, sizeof(VERTICAL_MAGIC
));
529 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
530 if ( memcmp(buf
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
)) == 0 ) {
531 vertical_insertion
= 1;
533 mc_lseek (file
, 0, SEEK_SET
);
536 if (vertical_insertion
) {
537 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
539 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
540 for (i
= 0; i
< blocklen
; i
++)
541 edit_insert (edit
, buf
[i
]);
544 edit_cursor_move (edit
, current
- edit
->curs1
);
553 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
555 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
558 GString
*errmsg
= (GString
*) 0;
560 /* Try opening an existing file */
561 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
565 * Try creating the file. O_EXCL prevents following broken links
566 * and opening existing files.
570 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
573 g_string_sprintf (errmsg
= g_string_new (NULL
),
574 _(" Cannot open %s for reading "), filename
);
577 /* New file, delete it if it's not modified or saved */
578 edit
->delete_file
= 1;
582 /* Check what we have opened */
583 if (mc_fstat (file
, st
) < 0) {
584 g_string_sprintf (errmsg
= g_string_new (NULL
),
585 _(" Cannot get size/permissions for %s "), filename
);
589 /* We want to open regular files only */
590 if (!S_ISREG (st
->st_mode
)) {
591 g_string_sprintf (errmsg
= g_string_new (NULL
),
592 _(" %s is not a regular file "), filename
);
597 * Don't delete non-empty files.
598 * O_EXCL should prevent it, but let's be on the safe side.
600 if (st
->st_size
> 0) {
601 edit
->delete_file
= 0;
604 if (st
->st_size
>= SIZE_LIMIT
) {
605 g_string_sprintf (errmsg
= g_string_new (NULL
),
606 _(" File %s is too large "), filename
);
610 (void) mc_close (file
);
612 edit_error_dialog (_("Error"), errmsg
->str
);
613 g_string_free (errmsg
, TRUE
);
620 * Open the file and load it into the buffers, either directly or using
621 * a filter. Return 0 on success, 1 on error.
623 * Fast loading (edit_load_file_fast) is used when the file size is
624 * known. In this case the data is read into the buffers by blocks.
625 * If the file size is not known, the data is loaded byte by byte in
629 edit_load_file (WEdit
*edit
)
633 /* Cannot do fast load if a filter is used */
634 if (edit_find_filter (edit
->filename
) >= 0)
638 * VFS may report file size incorrectly, and slow load is not a big
639 * deal considering overhead in VFS.
641 if (!vfs_file_is_local (edit
->filename
))
645 * FIXME: line end translation should disable fast loading as well
646 * Consider doing fseek() to the end and ftell() for the real size.
649 if (*edit
->filename
) {
650 /* If we are dealing with a real file, check that it exists */
651 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
654 /* nothing to load */
658 edit_init_buffers (edit
);
661 edit
->last_byte
= edit
->stat1
.st_size
;
662 edit_load_file_fast (edit
, edit
->filename
);
663 /* If fast load was used, the number of lines wasn't calculated */
664 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
667 const char *codepage_id
;
670 if (*edit
->filename
) {
671 edit
->stack_disable
= 1;
672 if (!edit_insert_file (edit
, edit
->filename
)) {
676 edit
->stack_disable
= 0;
680 codepage_id
= get_codepage_id( source_codepage
);
682 edit
->utf8
= str_isutf8 ( codepage_id
);
689 /* Restore saved cursor position in the file */
691 edit_load_position (WEdit
*edit
)
697 if (!edit
->filename
|| !*edit
->filename
)
700 filename
= vfs_canon (edit
->filename
);
701 load_file_position (filename
, &line
, &column
, &offset
);
705 edit_move_to_line (edit
, line
- 1);
706 edit
->prev_col
= column
;
707 } else if (offset
> 0) {
708 edit_cursor_move (edit
, offset
);
709 line
= edit
->curs_line
;
711 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
712 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
715 /* Save cursor position in the file */
717 edit_save_position (WEdit
*edit
)
721 if (!edit
->filename
|| !*edit
->filename
)
724 filename
= vfs_canon (edit
->filename
);
725 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
729 /* Clean the WEdit stricture except the widget part */
731 edit_purge_widget (WEdit
*edit
)
733 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
734 char *start
= (char *) edit
+ sizeof (Widget
);
735 memset (start
, 0, len
);
736 edit
->macro_i
= -1; /* not recording a macro */
740 edit_set_keymap (void)
742 editor_map
= default_editor_keymap
;
743 if (editor_keymap
&& editor_keymap
->len
> 0)
744 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
746 editor_x_map
= default_editor_x_keymap
;
747 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
748 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
752 #define space_width 1
755 * Fill in the edit structure. Return NULL on failure. Pass edit as
756 * NULL to allocate a new structure.
758 * If line is 0, try to restore saved position. Otherwise put the
759 * cursor on that line and show it in the middle of the screen.
762 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
766 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
767 if ( option_line_state
) {
768 option_line_state_width
= LINE_STATE_WIDTH
;
770 option_line_state_width
= 0;
775 * Expand option_whole_chars_search by national letters using
779 static char option_whole_chars_search_buf
[256];
781 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
783 size_t len
= str_term_width1 (option_whole_chars_search
);
785 strcpy (option_whole_chars_search_buf
,
786 option_whole_chars_search
);
788 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
789 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
790 option_whole_chars_search_buf
[len
++] = i
;
794 option_whole_chars_search_buf
[len
] = 0;
795 option_whole_chars_search
= option_whole_chars_search_buf
;
797 #endif /* ENABLE_NLS */
798 edit
= g_malloc0 (sizeof (WEdit
));
802 edit_purge_widget (edit
);
803 edit
->num_widget_lines
= lines
;
805 edit
->num_widget_columns
= columns
;
806 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
807 edit
->stat1
.st_uid
= getuid ();
808 edit
->stat1
.st_gid
= getgid ();
809 edit
->stat1
.st_mtime
= 0;
811 edit
->force
|= REDRAW_PAGE
;
812 edit_set_filename (edit
, filename
);
813 edit
->stack_size
= START_STACK_SIZE
;
814 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
815 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
816 if (edit_load_file (edit
)) {
817 /* edit_load_file already gives an error message */
823 edit
->converter
= str_cnv_from_term
;
826 const char *cp_id
= NULL
;
827 cp_id
= get_codepage_id (source_codepage
>= 0 ?
828 source_codepage
: display_codepage
);
832 conv
= str_crt_conv_from (cp_id
);
833 if (conv
!= INVALID_CONV
) {
834 if (edit
->converter
!= str_cnv_from_term
)
835 str_close_conv (edit
->converter
);
836 edit
->converter
= conv
;
840 edit
->utf8
= str_isutf8 (cp_id
);
844 edit
->loading_done
= 1;
847 edit_load_syntax (edit
, 0, 0);
850 edit_get_syntax_color (edit
, -1, &color
);
853 /* load saved cursor position */
854 if ((line
== 0) && option_save_position
) {
855 edit_load_position (edit
);
859 edit_move_display (edit
, line
- 1);
860 edit_move_to_line (edit
, line
- 1);
868 /* Clear the edit struct, freeing everything in it. Return 1 on success */
870 edit_clean (WEdit
*edit
)
877 /* a stale lock, remove it */
879 edit
->locked
= edit_unlock_file (edit
->filename
);
881 /* save cursor position */
882 if (option_save_position
)
883 edit_save_position (edit
);
885 /* File specified on the mcedit command line and never saved */
886 if (edit
->delete_file
)
887 unlink (edit
->filename
);
889 edit_free_syntax_rules (edit
);
890 book_mark_flush (edit
, -1);
891 for (; j
<= MAXBUFF
; j
++) {
892 g_free (edit
->buffers1
[j
]);
893 g_free (edit
->buffers2
[j
]);
896 g_free (edit
->undo_stack
);
897 g_free (edit
->filename
);
902 mc_search_free(edit
->search
);
905 edit_purge_widget (edit
);
910 /* returns 1 on success */
912 edit_renew (WEdit
* edit
)
914 int lines
= edit
->num_widget_lines
;
915 int columns
= edit
->num_widget_columns
;
918 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
922 * Load a new file into the editor. If it fails, preserve the old file.
923 * To do it, allocate a new widget, initialize it and, if the new file
924 * was loaded, copy the data to the old widget.
925 * Return 1 on success, 0 on failure.
928 edit_reload (WEdit
*edit
, const char *filename
)
931 int lines
= edit
->num_widget_lines
;
932 int columns
= edit
->num_widget_columns
;
934 e
= g_malloc0 (sizeof (WEdit
));
935 e
->widget
= edit
->widget
;
936 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
941 memcpy (edit
, e
, sizeof (WEdit
));
947 * Load a new file into the editor and set line. If it fails, preserve the old file.
948 * To do it, allocate a new widget, initialize it and, if the new file
949 * was loaded, copy the data to the old widget.
950 * Return 1 on success, 0 on failure.
953 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
956 int lines
= edit
->num_widget_lines
;
957 int columns
= edit
->num_widget_columns
;
959 e
= g_malloc0 (sizeof (WEdit
));
960 e
->widget
= edit
->widget
;
961 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
966 memcpy (edit
, e
, sizeof (WEdit
));
973 Recording stack for undo:
974 The following is an implementation of a compressed stack. Identical
975 pushes are recorded by a negative prefix indicating the number of times the
976 same char was pushed. This saves space for repeated curs-left or curs-right
993 If the stack long int is 0-255 it represents a normal insert (from a backspace),
994 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
995 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
996 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
999 The only way the cursor moves or the buffer is changed is through the routines:
1000 insert, backspace, insert_ahead, delete, and cursor_move.
1001 These record the reverse undo movements onto the stack each time they are
1004 Each key press results in a set of actions (insert; delete ...). So each time
1005 a key is pressed the current position of start_display is pushed as
1006 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1007 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1008 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1012 void edit_push_action (WEdit
* edit
, long c
,...)
1014 unsigned long sp
= edit
->stack_pointer
;
1018 /* first enlarge the stack if necessary */
1019 if (sp
> edit
->stack_size
- 10) { /* say */
1020 if (option_max_undo
< 256)
1021 option_max_undo
= 256;
1022 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1023 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1025 edit
->undo_stack
= t
;
1026 edit
->stack_size
<<= 1;
1027 edit
->stack_size_mask
= edit
->stack_size
- 1;
1031 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1032 if (edit
->stack_disable
)
1035 #ifdef FAST_MOVE_CURSOR
1036 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1038 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1039 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1041 c
= -(va_arg (ap
, int));
1044 #endif /* ! FAST_MOVE_CURSOR */
1045 if (edit
->stack_bottom
!= sp
1046 && spm1
!= edit
->stack_bottom
1047 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1049 if (edit
->undo_stack
[spm1
] < 0) {
1050 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1052 if (edit
->undo_stack
[spm1
] > -1000000000) {
1053 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1054 edit
->undo_stack
[spm1
]--;
1058 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1059 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1060 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1061 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1062 if (edit
->undo_stack
[spm1
] == -2)
1063 edit
->stack_pointer
= spm1
;
1065 edit
->undo_stack
[spm1
]++;
1070 d
= edit
->undo_stack
[spm1
];
1073 return; /* --> no need to push multiple do-nothings */
1074 edit
->undo_stack
[sp
] = -2;
1077 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1078 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1079 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1080 edit
->stack_pointer
= spm1
;
1086 edit
->undo_stack
[sp
] = c
;
1089 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1091 /* if the sp wraps round and catches the stack_bottom then erase
1092 * the first set of actions on the stack to make space - by moving
1093 * stack_bottom forward one "key press" */
1094 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1095 if ((unsigned long) c
== edit
->stack_bottom
||
1096 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1098 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1099 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1101 /*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: */
1102 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1103 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1107 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1108 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1111 pop_action (WEdit
* edit
)
1114 unsigned long sp
= edit
->stack_pointer
;
1115 if (sp
== edit
->stack_bottom
) {
1116 return STACK_BOTTOM
;
1118 sp
= (sp
- 1) & edit
->stack_size_mask
;
1119 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1120 /* edit->undo_stack[sp] = '@'; */
1121 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1124 if (sp
== edit
->stack_bottom
) {
1125 return STACK_BOTTOM
;
1127 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1128 if (edit
->undo_stack
[sp
] == -2) {
1129 /* edit->undo_stack[sp] = '@'; */
1130 edit
->stack_pointer
= sp
;
1132 edit
->undo_stack
[sp
]++;
1137 /* is called whenever a modification is made by one of the four routines below */
1138 static void edit_modification (WEdit
* edit
)
1140 edit
->caches_valid
= 0;
1141 edit
->screen_modified
= 1;
1143 /* raise lock when file modified */
1144 if (!edit
->modified
&& !edit
->delete_file
)
1145 edit
->locked
= edit_lock_file (edit
->filename
);
1150 Basic low level single character buffer alterations and movements at the cursor.
1151 Returns char passed over, inserted or removed.
1155 edit_insert (WEdit
*edit
, int c
)
1157 /* check if file has grown to large */
1158 if (edit
->last_byte
>= SIZE_LIMIT
)
1161 /* first we must update the position of the display window */
1162 if (edit
->curs1
< edit
->start_display
) {
1163 edit
->start_display
++;
1168 /* Mark file as modified, unless the file hasn't been fully loaded */
1169 if (edit
->loading_done
) {
1170 edit_modification (edit
);
1173 /* now we must update some info on the file and check if a redraw is required */
1175 if (edit
->book_mark
)
1176 book_mark_inc (edit
, edit
->curs_line
);
1178 edit
->total_lines
++;
1179 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1182 /* save the reverse command onto the undo stack */
1183 edit_push_action (edit
, BACKSPACE
);
1185 /* update markers */
1186 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1187 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1188 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1190 /* add a new buffer if we've reached the end of the last one */
1191 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1192 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1193 g_malloc0 (EDIT_BUF_SIZE
);
1195 /* perform the insertion */
1196 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1197 curs1
& M_EDIT_BUF_SIZE
]
1198 = (unsigned char) c
;
1200 /* update file length */
1203 /* update cursor position */
1208 edit_insert_over (WEdit
* edit
)
1212 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1213 edit_insert (edit
, ' ');
1218 /* same as edit_insert and move left */
1219 void edit_insert_ahead (WEdit
* edit
, int c
)
1221 if (edit
->last_byte
>= SIZE_LIMIT
)
1223 if (edit
->curs1
< edit
->start_display
) {
1224 edit
->start_display
++;
1228 edit_modification (edit
);
1230 if (edit
->book_mark
)
1231 book_mark_inc (edit
, edit
->curs_line
);
1232 edit
->total_lines
++;
1233 edit
->force
|= REDRAW_AFTER_CURSOR
;
1235 edit_push_action (edit
, DELCHAR
);
1237 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1238 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1239 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1241 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1242 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1243 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1250 int edit_delete (WEdit
* edit
, const int byte_delete
)
1259 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1260 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1261 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1264 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1265 if ( edit
->utf8
&& byte_delete
== 0 ) {
1266 edit_get_utf (edit
, edit
->curs1
, &cw
);
1270 for ( i
= 1; i
<= cw
; i
++ ) {
1271 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1273 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1274 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1275 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1279 edit_push_action (edit
, p
+ 256);
1282 edit_modification (edit
);
1284 if (edit
->book_mark
)
1285 book_mark_dec (edit
, edit
->curs_line
);
1286 edit
->total_lines
--;
1287 edit
->force
|= REDRAW_AFTER_CURSOR
;
1289 if (edit
->curs1
< edit
->start_display
) {
1290 edit
->start_display
--;
1300 edit_backspace (WEdit
* edit
, const int byte_delete
)
1309 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1310 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1311 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1314 if ( edit
->utf8
&& byte_delete
== 0 ) {
1315 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1319 for ( i
= 1; i
<= cw
; i
++ ) {
1320 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1321 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1322 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1323 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1327 edit_push_action (edit
, p
);
1329 edit_modification (edit
);
1331 if (edit
->book_mark
)
1332 book_mark_dec (edit
, edit
->curs_line
);
1334 edit
->total_lines
--;
1335 edit
->force
|= REDRAW_AFTER_CURSOR
;
1338 if (edit
->curs1
< edit
->start_display
) {
1339 edit
->start_display
--;
1347 #ifdef FAST_MOVE_CURSOR
1349 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1352 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1354 next
-= (unsigned long) dest
;
1362 edit_move_backward_lots (WEdit
*edit
, long increment
)
1365 unsigned char *p
= NULL
;
1367 if (increment
> edit
->curs1
)
1368 increment
= edit
->curs1
;
1371 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1373 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1376 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1380 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1381 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1386 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1387 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1388 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1389 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1392 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1393 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1394 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1399 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1401 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1403 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1404 g_malloc0 (EDIT_BUF_SIZE
);
1409 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1419 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1421 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1425 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1426 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1429 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1431 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1432 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1437 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1439 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1441 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1442 g_malloc0 (EDIT_BUF_SIZE
);
1447 return edit_get_byte (edit
, edit
->curs1
);
1450 #endif /* ! FAST_MOVE_CURSOR */
1452 /* moves the cursor right or left: increment positive or negative respectively */
1453 void edit_cursor_move (WEdit
* edit
, long increment
)
1455 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1457 #ifdef FAST_MOVE_CURSOR
1458 if (increment
< -256) {
1459 edit
->force
|= REDRAW_PAGE
;
1460 edit_move_backward_lots (edit
, -increment
);
1463 #endif /* ! FAST_MOVE_CURSOR */
1465 if (increment
< 0) {
1466 for (; increment
< 0; increment
++) {
1470 edit_push_action (edit
, CURS_RIGHT
);
1472 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1473 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1474 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1475 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1477 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1478 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1479 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1480 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1485 edit
->force
|= REDRAW_LINE_BELOW
;
1489 } else if (increment
> 0) {
1490 for (; increment
> 0; increment
--) {
1494 edit_push_action (edit
, CURS_LEFT
);
1496 c
= edit_get_byte (edit
, edit
->curs1
);
1497 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1498 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1499 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1501 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1502 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1503 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1504 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1509 edit
->force
|= REDRAW_LINE_ABOVE
;
1515 /* These functions return positions relative to lines */
1517 /* returns index of last char on line + 1 */
1518 long edit_eol (WEdit
* edit
, long current
)
1520 if (current
< edit
->last_byte
) {
1522 if (edit_get_byte (edit
, current
) == '\n')
1525 return edit
->last_byte
;
1529 /* returns index of first char on line */
1530 long edit_bol (WEdit
* edit
, long current
)
1534 if (edit_get_byte (edit
, current
- 1) == '\n')
1542 long edit_count_lines (WEdit
* edit
, long current
, long upto
)
1545 if (upto
> edit
->last_byte
)
1546 upto
= edit
->last_byte
;
1549 while (current
< upto
)
1550 if (edit_get_byte (edit
, current
++) == '\n')
1556 /* If lines is zero this returns the count of lines from current to upto. */
1557 /* If upto is zero returns index of lines forward current. */
1558 long edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1561 return edit_count_lines (edit
, current
, upto
);
1567 next
= edit_eol (edit
, current
) + 1;
1568 if (next
> edit
->last_byte
)
1578 /* Returns offset of 'lines' lines up from current */
1579 long edit_move_backward (WEdit
* edit
, long current
, long lines
)
1583 current
= edit_bol (edit
, current
);
1584 while((lines
--) && current
!= 0)
1585 current
= edit_bol (edit
, current
- 1);
1589 /* If cols is zero this returns the count of columns from current to upto. */
1590 /* If upto is zero returns index of cols across from current. */
1592 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1601 q
= edit
->last_byte
+ 2;
1603 for (col
= 0, p
= current
; p
< q
; p
++) {
1615 orig_c
= c
= edit_get_byte (edit
, p
);
1618 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1622 if (g_unichar_iswide (utf_ch
))
1624 } else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1628 c
= convert_to_display_c (c
);
1630 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1631 else if (c
== '\n') {
1636 } else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1637 /* '\r' is shown as ^M, so we must advance 2 characters */
1638 /* Caret notation for control characters */
1646 /* returns the current column position of the cursor */
1647 int edit_get_col (WEdit
* edit
)
1649 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1653 /* Scrolling functions */
1655 void edit_update_curs_row (WEdit
* edit
)
1657 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1660 void edit_update_curs_col (WEdit
* edit
)
1662 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1666 edit_get_curs_col (const WEdit
*edit
)
1668 return edit
->curs_col
;
1671 /*moves the display start position up by i lines */
1672 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1674 unsigned long lines_above
= edit
->start_line
;
1675 if (i
> lines_above
)
1678 edit
->start_line
-= i
;
1679 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1680 edit
->force
|= REDRAW_PAGE
;
1681 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1683 edit_update_curs_row (edit
);
1687 /* returns 1 if could scroll, 0 otherwise */
1688 void edit_scroll_downward (WEdit
* edit
, int i
)
1691 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1692 if (lines_below
> 0) {
1693 if (i
> lines_below
)
1695 edit
->start_line
+= i
;
1696 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1697 edit
->force
|= REDRAW_PAGE
;
1698 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1700 edit_update_curs_row (edit
);
1703 void edit_scroll_right (WEdit
* edit
, int i
)
1705 edit
->force
|= REDRAW_PAGE
;
1706 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1707 edit
->start_col
-= i
;
1710 void edit_scroll_left (WEdit
* edit
, int i
)
1712 if (edit
->start_col
) {
1713 edit
->start_col
+= i
;
1714 if (edit
->start_col
> 0)
1715 edit
->start_col
= 0;
1716 edit
->force
|= REDRAW_PAGE
;
1717 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1721 /* high level cursor movement commands */
1723 static int is_in_indent (WEdit
*edit
)
1725 long p
= edit_bol (edit
, edit
->curs1
);
1726 while (p
< edit
->curs1
)
1727 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1732 static int left_of_four_spaces (WEdit
*edit
);
1735 edit_move_to_prev_col (WEdit
* edit
, long p
)
1737 int prev
= edit
->prev_col
;
1738 int over
= edit
->over_col
;
1739 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1741 if (option_cursor_beyond_eol
) {
1742 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1744 if (line_len
< prev
+ edit
->over_col
) {
1745 edit
->over_col
= prev
+ over
- line_len
;
1746 edit
->prev_col
= line_len
;
1747 edit
->curs_col
= line_len
;
1749 edit
->curs_col
= prev
+ over
;
1750 edit
->prev_col
= edit
->curs_col
;
1755 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1756 edit_update_curs_col (edit
);
1758 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1759 int q
= edit
->curs_col
;
1760 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1761 p
= edit_bol (edit
, edit
->curs1
);
1762 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1763 if (!left_of_four_spaces (edit
))
1764 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1771 is_blank (WEdit
*edit
, long offset
)
1775 s
= edit_bol (edit
, offset
);
1776 f
= edit_eol (edit
, offset
) - 1;
1778 c
= edit_get_byte (edit
, s
++);
1786 /* returns the offset of line i */
1788 edit_find_line (WEdit
*edit
, int line
)
1792 if (!edit
->caches_valid
) {
1793 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1794 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1795 /* three offsets that we *know* are line 0 at 0 and these two: */
1796 edit
->line_numbers
[1] = edit
->curs_line
;
1797 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1798 edit
->line_numbers
[2] = edit
->total_lines
;
1799 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1800 edit
->caches_valid
= 1;
1802 if (line
>= edit
->total_lines
)
1803 return edit
->line_offsets
[2];
1806 /* find the closest known point */
1807 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1809 n
= abs (edit
->line_numbers
[i
] - line
);
1816 return edit
->line_offsets
[j
]; /* know the offset exactly */
1817 if (m
== 1 && j
>= 3)
1818 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1820 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1821 if (line
> edit
->line_numbers
[j
])
1822 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1824 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1825 edit
->line_numbers
[i
] = line
;
1826 return edit
->line_offsets
[i
];
1829 int line_is_blank (WEdit
* edit
, long line
)
1831 return is_blank (edit
, edit_find_line (edit
, line
));
1834 /* moves up until a blank line is reached, or until just
1835 before a non-blank line is reached */
1837 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
1840 if (edit
->curs_line
> 1) {
1841 if (line_is_blank (edit
, edit
->curs_line
)) {
1842 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1843 for (i
= edit
->curs_line
- 1; i
; i
--)
1844 if (!line_is_blank (edit
, i
)) {
1849 for (i
= edit
->curs_line
- 1; i
; i
--)
1850 if (line_is_blank (edit
, i
))
1854 for (i
= edit
->curs_line
- 1; i
; i
--)
1855 if (line_is_blank (edit
, i
))
1859 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
1862 /* moves down until a blank line is reached, or until just
1863 before a non-blank line is reached */
1865 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
1868 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1869 i
= edit
->total_lines
;
1871 if (line_is_blank (edit
, edit
->curs_line
)) {
1872 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1873 for (i
= edit
->curs_line
+ 1; i
; i
++)
1874 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1879 for (i
= edit
->curs_line
+ 1; i
; i
++)
1880 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 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
1892 static void edit_begin_page (WEdit
*edit
)
1894 edit_update_curs_row (edit
);
1895 edit_move_up (edit
, edit
->curs_row
, 0);
1898 static void edit_end_page (WEdit
*edit
)
1900 edit_update_curs_row (edit
);
1901 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1905 /* goto beginning of text */
1906 static void edit_move_to_top (WEdit
* edit
)
1908 if (edit
->curs_line
) {
1909 edit_cursor_move (edit
, -edit
->curs1
);
1910 edit_move_to_prev_col (edit
, 0);
1911 edit
->force
|= REDRAW_PAGE
;
1912 edit
->search_start
= 0;
1913 edit_update_curs_row(edit
);
1918 /* goto end of text */
1919 static void edit_move_to_bottom (WEdit
* edit
)
1921 if (edit
->curs_line
< edit
->total_lines
) {
1922 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1923 edit
->start_display
= edit
->last_byte
;
1924 edit
->start_line
= edit
->total_lines
;
1925 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1926 edit
->force
|= REDRAW_PAGE
;
1930 /* goto beginning of line */
1931 static void edit_cursor_to_bol (WEdit
* edit
)
1933 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1934 edit
->search_start
= edit
->curs1
;
1935 edit
->prev_col
= edit_get_col (edit
);
1939 /* goto end of line */
1940 static void edit_cursor_to_eol (WEdit
* edit
)
1942 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1943 edit
->search_start
= edit
->curs1
;
1944 edit
->prev_col
= edit_get_col (edit
);
1948 /* move cursor to line 'line' */
1949 void edit_move_to_line (WEdit
* e
, long line
)
1951 if(line
< e
->curs_line
)
1952 edit_move_up (e
, e
->curs_line
- line
, 0);
1954 edit_move_down (e
, line
- e
->curs_line
, 0);
1955 edit_scroll_screen_over_cursor (e
);
1958 /* scroll window so that first visible line is 'line' */
1959 void edit_move_display (WEdit
* e
, long line
)
1961 if(line
< e
->start_line
)
1962 edit_scroll_upward (e
, e
->start_line
- line
);
1964 edit_scroll_downward (e
, line
- e
->start_line
);
1967 /* save markers onto undo stack */
1968 void edit_push_markers (WEdit
* edit
)
1970 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1971 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1974 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1983 /* highlight marker toggle */
1984 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1986 edit_push_markers (edit
);
1988 edit_set_markers (edit
, 0, 0, 0, 0);
1989 edit
->force
|= REDRAW_PAGE
;
1991 if (edit
->mark2
>= 0) {
1992 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
1993 edit
->force
|= REDRAW_PAGE
;
1995 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
1999 static unsigned long
2004 const char option_chars_move_whole_word
[] =
2005 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2010 if (*option_chars_move_whole_word
== '!')
2012 return 0x80000000UL
;
2014 if (g_ascii_isupper ((gchar
) c
))
2016 else if (g_ascii_islower ((gchar
) c
))
2018 else if (g_ascii_isalpha (c
))
2020 else if (isdigit (c
))
2022 else if (isspace (c
))
2024 q
= strchr (option_chars_move_whole_word
, c
);
2026 return 0xFFFFFFFFUL
;
2028 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2032 } while ((q
= strchr (q
+ 1, c
)));
2037 edit_left_word_move (WEdit
*edit
, int s
)
2041 if (column_highlighting
2042 && edit
->mark1
!= edit
->mark2
2043 && edit
->over_col
== 0
2044 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2046 edit_cursor_move (edit
, -1);
2049 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2050 c2
= edit_get_byte (edit
, edit
->curs1
);
2051 if (!(my_type_of (c1
) & my_type_of (c2
)))
2053 if (isspace (c1
) && !isspace (c2
))
2056 if (!isspace (c1
) && isspace (c2
))
2061 static void edit_left_word_move_cmd (WEdit
* edit
)
2063 edit_left_word_move (edit
, 0);
2064 edit
->force
|= REDRAW_PAGE
;
2068 edit_right_word_move (WEdit
*edit
, int s
)
2072 if (column_highlighting
2073 && edit
->mark1
!= edit
->mark2
2074 && edit
->over_col
== 0
2075 && edit
->curs1
== edit_eol(edit
, edit
->curs1
)
2078 edit_cursor_move (edit
, 1);
2079 if (edit
->curs1
>= edit
->last_byte
)
2081 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2082 c2
= edit_get_byte (edit
, edit
->curs1
);
2083 if (!(my_type_of (c1
) & my_type_of (c2
)))
2085 if (isspace (c1
) && !isspace (c2
))
2088 if (!isspace (c1
) && isspace (c2
))
2093 static void edit_right_word_move_cmd (WEdit
* edit
)
2095 edit_right_word_move (edit
, 0);
2096 edit
->force
|= REDRAW_PAGE
;
2099 static void edit_right_char_move_cmd (WEdit
* edit
)
2104 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2108 c
= edit_get_byte (edit
, edit
->curs1
);
2110 if (option_cursor_beyond_eol
&& c
== '\n') {
2113 edit_cursor_move (edit
, cw
);
2117 static void edit_left_char_move_cmd (WEdit
* edit
)
2120 if (column_highlighting
2121 && option_cursor_beyond_eol
2122 && edit
->mark1
!= edit
->mark2
2123 && edit
->over_col
== 0
2124 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2127 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2131 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2134 edit_cursor_move (edit
, -cw
);
2138 /** Up or down cursor moving.
2139 direction = TRUE - move up
2143 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2146 unsigned long l
= (direction
)
2148 : edit
->total_lines
- edit
->curs_line
;
2157 edit
->force
|= REDRAW_PAGE
;
2160 edit_scroll_upward (edit
, i
);
2162 edit_scroll_downward (edit
, i
);
2164 p
= edit_bol (edit
, edit
->curs1
);
2167 ? edit_move_backward (edit
, p
, i
)
2168 : edit_move_forward (edit
, p
, i
, 0);
2170 edit_cursor_move (edit
, p
- edit
->curs1
);
2172 edit_move_to_prev_col (edit
, p
);
2174 /* search start of current multibyte char (like CJK) */
2175 if (edit
->curs1
+ 1 < edit
->last_byte
) {
2176 edit_right_char_move_cmd (edit
);
2177 edit_left_char_move_cmd (edit
);
2180 edit
->search_start
= edit
->curs1
;
2181 edit
->found_len
= 0;
2184 static void edit_right_delete_word (WEdit
* edit
)
2188 if (edit
->curs1
>= edit
->last_byte
)
2190 c1
= edit_delete (edit
, 1);
2191 c2
= edit_get_byte (edit
, edit
->curs1
);
2192 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2194 if (!(my_type_of (c1
) & my_type_of (c2
)))
2199 static void edit_left_delete_word (WEdit
* edit
)
2203 if (edit
->curs1
<= 0)
2205 c1
= edit_backspace (edit
, 1);
2206 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2207 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2209 if (!(my_type_of (c1
) & my_type_of (c2
)))
2215 the start column position is not recorded, and hence does not
2216 undo as it happed. But who would notice.
2219 edit_do_undo (WEdit
* edit
)
2224 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2226 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2231 edit_cursor_move (edit
, 1);
2234 edit_cursor_move (edit
, -1);
2237 edit_backspace (edit
, 1);
2240 edit_delete (edit
, 1);
2243 column_highlighting
= 1;
2246 column_highlighting
= 0;
2249 if (ac
>= 256 && ac
< 512)
2250 edit_insert_ahead (edit
, ac
- 256);
2251 if (ac
>= 0 && ac
< 256)
2252 edit_insert (edit
, ac
);
2254 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2255 edit
->mark1
= ac
- MARK_1
;
2256 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2257 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2258 edit
->mark2
= ac
- MARK_2
;
2259 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2262 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2265 if (edit
->start_display
> ac
- KEY_PRESS
) {
2266 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2267 edit
->force
|= REDRAW_PAGE
;
2268 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2269 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2270 edit
->force
|= REDRAW_PAGE
;
2272 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2273 edit_update_curs_row (edit
);
2276 edit
->stack_disable
= 0;
2279 static void edit_delete_to_line_end (WEdit
* edit
)
2281 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2284 edit_delete (edit
, 1);
2288 static void edit_delete_to_line_begin (WEdit
* edit
)
2290 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2293 edit_backspace (edit
, 1);
2298 edit_delete_line (WEdit
*edit
)
2301 * Delete right part of the line.
2302 * Note that edit_get_byte() returns '\n' when byte position is
2305 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2306 (void) edit_delete (edit
, 1);
2311 * Note that edit_delete() will not corrupt anything if called while
2312 * cursor position is EOF.
2314 (void) edit_delete (edit
, 1);
2317 * Delete left part of the line.
2318 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2320 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2321 (void) edit_backspace (edit
, 1);
2325 void insert_spaces_tab (WEdit
* edit
, int half
)
2328 edit_update_curs_col (edit
);
2329 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2331 edit_insert (edit
, ' ');
2336 static int is_aligned_on_a_tab (WEdit
* edit
)
2338 edit_update_curs_col (edit
);
2339 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2340 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2343 static int right_of_four_spaces (WEdit
*edit
)
2346 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2347 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2349 return is_aligned_on_a_tab (edit
);
2353 static int left_of_four_spaces (WEdit
*edit
)
2356 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2357 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2359 return is_aligned_on_a_tab (edit
);
2363 int edit_indent_width (WEdit
* edit
, long p
)
2366 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2368 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2371 void edit_insert_indent (WEdit
* edit
, int indent
)
2373 if (!option_fill_tabs_with_spaces
) {
2374 while (indent
>= TAB_SIZE
) {
2375 edit_insert (edit
, '\t');
2379 while (indent
-- > 0)
2380 edit_insert (edit
, ' ');
2384 edit_auto_indent (WEdit
* edit
)
2389 /* use the previous line as a template */
2390 p
= edit_move_backward (edit
, p
, 1);
2391 /* copy the leading whitespace of the line */
2392 for (;;) { /* no range check - the line _is_ \n-terminated */
2393 c
= edit_get_byte (edit
, p
++);
2394 if (c
!= ' ' && c
!= '\t')
2396 edit_insert (edit
, c
);
2401 edit_double_newline (WEdit
* edit
)
2403 edit_insert (edit
, '\n');
2404 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2406 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2408 edit
->force
|= REDRAW_PAGE
;
2409 edit_insert (edit
, '\n');
2413 edit_tab_cmd (WEdit
* edit
)
2417 if (option_fake_half_tabs
) {
2418 if (is_in_indent (edit
)) {
2419 /*insert a half tab (usually four spaces) unless there is a
2420 half tab already behind, then delete it and insert a
2422 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2423 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2424 edit_backspace (edit
, 1);
2425 edit_insert (edit
, '\t');
2427 insert_spaces_tab (edit
, 1);
2432 if (option_fill_tabs_with_spaces
) {
2433 insert_spaces_tab (edit
, 0);
2435 edit_insert (edit
, '\t');
2439 static void check_and_wrap_line (WEdit
* edit
)
2442 if (!option_typewriter_wrap
)
2444 edit_update_curs_col (edit
);
2445 if (edit
->curs_col
< option_word_wrap_line_length
)
2450 c
= edit_get_byte (edit
, curs
);
2451 if (c
== '\n' || curs
<= 0) {
2452 edit_insert (edit
, '\n');
2455 if (c
== ' ' || c
== '\t') {
2456 int current
= edit
->curs1
;
2457 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2458 edit_insert (edit
, '\n');
2459 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2465 static inline void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2467 void edit_push_key_press (WEdit
* edit
)
2469 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2470 if (edit
->mark2
== -1)
2471 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2474 /* this find the matching bracket in either direction, and sets edit->bracket */
2475 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2477 const char * const b
= "{}{[][()(", *p
;
2478 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2479 unsigned long j
= 0;
2481 edit_update_curs_row (edit
);
2482 c
= edit_get_byte (edit
, edit
->curs1
);
2485 if (!furthest_bracket_search
)
2486 furthest_bracket_search
--;
2487 /* not on a bracket at all */
2490 /* the matching bracket */
2492 /* going left or right? */
2493 if (strchr ("{[(", c
))
2495 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2496 /* out of buffer? */
2497 if (q
>= edit
->last_byte
|| q
< 0)
2499 a
= edit_get_byte (edit
, q
);
2500 /* don't want to eat CPU */
2501 if (j
++ > furthest_bracket_search
)
2503 /* out of screen? */
2505 if (q
< edit
->start_display
)
2507 /* count lines if searching downward */
2508 if (inc
> 0 && a
== '\n')
2509 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2512 /* count bracket depth */
2513 i
+= (a
== c
) - (a
== d
);
2514 /* return if bracket depth is zero */
2522 static long last_bracket
= -1;
2524 void edit_find_bracket (WEdit
* edit
)
2526 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2527 if (last_bracket
!= edit
->bracket
)
2528 edit
->force
|= REDRAW_PAGE
;
2529 last_bracket
= edit
->bracket
;
2533 edit_goto_matching_bracket (WEdit
*edit
)
2537 q
= edit_get_bracket (edit
, 0, 0);
2539 edit
->bracket
= edit
->curs1
;
2540 edit
->force
|= REDRAW_PAGE
;
2541 edit_cursor_move (edit
, q
- edit
->curs1
);
2546 * This executes a command as though the user initiated it through a key
2547 * press. Callback with WIDGET_KEY as a message calls this after
2548 * translating the key press. This function can be used to pass any
2549 * command to the editor. Note that the screen wouldn't update
2550 * automatically. Either of command or char_for_insertion must be
2551 * passed as -1. Commands are executed, and char_for_insertion is
2552 * inserted at the cursor.
2555 edit_execute_key_command (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2557 if (command
== CK_Begin_Record_Macro
) {
2559 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2562 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2563 edit
->force
|= REDRAW_COMPLETELY
;
2564 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2568 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2569 edit
->macro
[edit
->macro_i
].command
= command
;
2570 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2572 /* record the beginning of a set of editing actions initiated by a key press */
2573 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2574 edit_push_key_press (edit
);
2576 edit_execute_cmd (edit
, command
, char_for_insertion
);
2577 if (column_highlighting
)
2578 edit
->force
|= REDRAW_PAGE
;
2581 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2584 This executes a command at a lower level than macro recording.
2585 It also does not push a key_press onto the undo stack. This means
2586 that if it is called many times, a single undo command will undo
2587 all of them. It also does not check for the Undo command.
2590 edit_execute_cmd (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2592 edit
->force
|= REDRAW_LINE
;
2594 /* The next key press will unhighlight the found string, so update
2596 if (edit
->found_len
|| column_highlighting
)
2597 edit
->force
|= REDRAW_PAGE
;
2599 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2600 column_highlighting
= 0;
2601 if (!edit
->highlight
2602 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2603 edit_mark_cmd (edit
, 1); /* clear */
2604 edit_mark_cmd (edit
, 0); /* marking on */
2606 edit
->highlight
= 1;
2607 } else { /* any other command */
2608 if (edit
->highlight
)
2609 edit_mark_cmd (edit
, 0); /* clear */
2610 edit
->highlight
= 0;
2613 /* first check for undo */
2614 if (command
== CK_Undo
) {
2615 edit_do_undo (edit
);
2616 edit
->found_len
= 0;
2617 edit
->prev_col
= edit_get_col (edit
);
2618 edit
->search_start
= edit
->curs1
;
2622 /* An ordinary key press */
2623 if (char_for_insertion
>= 0) {
2624 if (edit
->overwrite
) {
2625 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2626 edit_delete (edit
, 0);
2628 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2629 edit_insert_over (edit
);
2631 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2632 unsigned char str
[6 + 1];
2634 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2641 while ( str
[i
] != 0 && i
<=6) {
2642 char_for_insertion
= str
[i
];
2643 edit_insert (edit
, char_for_insertion
);
2648 edit_insert (edit
, char_for_insertion
);
2650 if (option_auto_para_formatting
) {
2651 format_paragraph (edit
, 0);
2652 edit
->force
|= REDRAW_PAGE
;
2654 check_and_wrap_line (edit
);
2655 edit
->found_len
= 0;
2656 edit
->prev_col
= edit_get_col (edit
);
2657 edit
->search_start
= edit
->curs1
;
2658 edit_find_bracket (edit
);
2665 case CK_Begin_Page_Highlight
:
2666 case CK_End_Page_Highlight
:
2673 if ( edit
->mark2
>= 0 ) {
2674 if ( !option_persistent_selections
) {
2675 if (column_highlighting
)
2676 edit_push_action (edit
, COLUMN_ON
);
2677 column_highlighting
= 0;
2678 edit_mark_cmd (edit
, 1);
2686 case CK_Begin_Page_Highlight
:
2687 case CK_End_Page_Highlight
:
2692 case CK_Word_Left_Highlight
:
2693 case CK_Word_Right_Highlight
:
2694 case CK_Up_Highlight
:
2695 case CK_Down_Highlight
:
2696 case CK_Up_Alt_Highlight
:
2697 case CK_Down_Alt_Highlight
:
2698 if (edit
->mark2
== -1)
2699 break; /*marking is following the cursor: may need to highlight a whole line */
2702 case CK_Left_Highlight
:
2703 case CK_Right_Highlight
:
2704 edit
->force
|= REDRAW_CHAR_ONLY
;
2707 /* basic cursor key commands */
2710 /* if non persistent selection and text selected */
2711 if ( !option_persistent_selections
) {
2712 if ( edit
->mark1
!= edit
->mark2
) {
2713 edit_block_delete_cmd (edit
);
2717 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2721 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2722 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2724 edit_backspace (edit
, 1);
2727 if (option_fake_half_tabs
) {
2729 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2730 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2731 edit_backspace (edit
, 1);
2736 edit_backspace (edit
, 0);
2739 /* if non persistent selection and text selected */
2740 if ( !option_persistent_selections
) {
2741 if ( edit
->mark1
!= edit
->mark2
) {
2742 edit_block_delete_cmd (edit
);
2747 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2748 edit_insert_over (edit
);
2750 if (option_fake_half_tabs
) {
2752 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2753 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2754 edit_delete (edit
, 1);
2758 edit_delete (edit
, 0);
2760 case CK_Delete_Word_Left
:
2762 edit_left_delete_word (edit
);
2764 case CK_Delete_Word_Right
:
2765 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2766 edit_insert_over (edit
);
2768 edit_right_delete_word (edit
);
2770 case CK_Delete_Line
:
2771 edit_delete_line (edit
);
2773 case CK_Delete_To_Line_End
:
2774 edit_delete_to_line_end (edit
);
2776 case CK_Delete_To_Line_Begin
:
2777 edit_delete_to_line_begin (edit
);
2781 if (option_auto_para_formatting
) {
2782 edit_double_newline (edit
);
2783 if (option_return_does_auto_indent
)
2784 edit_auto_indent (edit
);
2785 format_paragraph (edit
, 0);
2787 edit_insert (edit
, '\n');
2788 if (option_return_does_auto_indent
) {
2789 edit_auto_indent (edit
);
2794 edit_insert (edit
, '\n');
2797 case CK_Page_Up_Alt_Highlight
:
2798 column_highlighting
= 1;
2800 case CK_Page_Up_Highlight
:
2801 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2803 case CK_Page_Down_Alt_Highlight
:
2804 column_highlighting
= 1;
2806 case CK_Page_Down_Highlight
:
2807 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2809 case CK_Left_Alt_Highlight
:
2810 column_highlighting
= 1;
2812 case CK_Left_Highlight
:
2813 if (option_fake_half_tabs
) {
2814 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2815 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2818 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2819 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2823 edit_left_char_move_cmd (edit
);
2825 case CK_Right_Alt_Highlight
:
2826 column_highlighting
= 1;
2828 case CK_Right_Highlight
:
2829 if (option_fake_half_tabs
) {
2830 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2831 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2832 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2836 edit_right_char_move_cmd (edit
);
2839 case CK_Begin_Page_Highlight
:
2840 edit_begin_page (edit
);
2843 case CK_End_Page_Highlight
:
2844 edit_end_page (edit
);
2847 case CK_Word_Left_Highlight
:
2849 edit_left_word_move_cmd (edit
);
2852 case CK_Word_Right_Highlight
:
2854 edit_right_word_move_cmd (edit
);
2856 case CK_Up_Alt_Highlight
:
2857 column_highlighting
= 1;
2859 case CK_Up_Highlight
:
2860 edit_move_up (edit
, 1, 0);
2862 case CK_Down_Alt_Highlight
:
2863 column_highlighting
= 1;
2865 case CK_Down_Highlight
:
2866 edit_move_down (edit
, 1, 0);
2868 case CK_Paragraph_Up_Alt_Highlight
:
2869 column_highlighting
= 1;
2870 case CK_Paragraph_Up
:
2871 case CK_Paragraph_Up_Highlight
:
2872 edit_move_up_paragraph (edit
, 0);
2874 case CK_Paragraph_Down_Alt_Highlight
:
2875 column_highlighting
= 1;
2876 case CK_Paragraph_Down
:
2877 case CK_Paragraph_Down_Highlight
:
2878 edit_move_down_paragraph (edit
, 0);
2880 case CK_Scroll_Up_Alt_Highlight
:
2881 column_highlighting
= 1;
2883 case CK_Scroll_Up_Highlight
:
2884 edit_move_up (edit
, 1, 1);
2886 case CK_Scroll_Down_Alt_Highlight
:
2887 column_highlighting
= 1;
2888 case CK_Scroll_Down
:
2889 case CK_Scroll_Down_Highlight
:
2890 edit_move_down (edit
, 1, 1);
2893 case CK_Home_Highlight
:
2894 edit_cursor_to_bol (edit
);
2897 case CK_End_Highlight
:
2898 edit_cursor_to_eol (edit
);
2901 /* if text marked shift block */
2902 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2903 if (edit
->mark2
< 0)
2904 edit_mark_cmd (edit
, 0);
2905 edit_move_block_to_right (edit
);
2907 if ( option_cursor_beyond_eol
)
2908 edit_insert_over (edit
);
2909 edit_tab_cmd (edit
);
2910 if (option_auto_para_formatting
) {
2911 format_paragraph (edit
, 0);
2912 edit
->force
|= REDRAW_PAGE
;
2914 check_and_wrap_line (edit
);
2919 case CK_Toggle_Insert
:
2920 edit
->overwrite
= (edit
->overwrite
== 0);
2924 if (edit
->mark2
>= 0) {
2925 if (column_highlighting
)
2926 edit_push_action (edit
, COLUMN_ON
);
2927 column_highlighting
= 0;
2929 edit_mark_cmd (edit
, 0);
2931 case CK_Column_Mark
:
2932 if (!column_highlighting
)
2933 edit_push_action (edit
, COLUMN_OFF
);
2934 column_highlighting
= 1;
2935 edit_mark_cmd (edit
, 0);
2938 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
2939 edit
->force
|= REDRAW_PAGE
;
2942 if (column_highlighting
)
2943 edit_push_action (edit
, COLUMN_ON
);
2944 column_highlighting
= 0;
2945 edit_mark_cmd (edit
, 1);
2948 case CK_Toggle_Line_State
:
2949 option_line_state
= !option_line_state
;
2950 if ( option_line_state
) {
2951 option_line_state_width
= LINE_STATE_WIDTH
;
2953 option_line_state_width
= 0;
2955 edit
->force
|= REDRAW_PAGE
;
2958 case CK_Toggle_Show_Margin
:
2959 show_right_margin
= !show_right_margin
;
2960 edit
->force
|= REDRAW_PAGE
;
2963 case CK_Toggle_Bookmark
:
2964 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2965 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2966 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2968 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2970 case CK_Flush_Bookmarks
:
2971 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2972 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2973 edit
->force
|= REDRAW_PAGE
;
2975 case CK_Next_Bookmark
:
2976 if (edit
->book_mark
) {
2977 struct _book_mark
*p
;
2978 p
= (struct _book_mark
*) book_mark_find (edit
,
2982 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2983 || p
->line
< edit
->start_line
)
2984 edit_move_display (edit
,
2986 edit
->num_widget_lines
/ 2);
2987 edit_move_to_line (edit
, p
->line
);
2991 case CK_Prev_Bookmark
:
2992 if (edit
->book_mark
) {
2993 struct _book_mark
*p
;
2994 p
= (struct _book_mark
*) book_mark_find (edit
,
2996 while (p
->line
== edit
->curs_line
)
3000 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3001 || p
->line
< edit
->start_line
)
3002 edit_move_display (edit
,
3004 edit
->num_widget_lines
/ 2);
3005 edit_move_to_line (edit
, p
->line
);
3010 case CK_Beginning_Of_Text
:
3011 case CK_Beginning_Of_Text_Highlight
:
3012 edit_move_to_top (edit
);
3014 case CK_End_Of_Text
:
3015 case CK_End_Of_Text_Highlight
:
3016 edit_move_to_bottom (edit
);
3020 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3021 edit_insert_over (edit
);
3022 edit_block_copy_cmd (edit
);
3025 edit_block_delete_cmd (edit
);
3028 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3029 edit_insert_over (edit
);
3030 edit_block_move_cmd (edit
);
3033 case CK_Shift_Block_Left
:
3034 if (edit
->mark1
!= edit
->mark2
)
3035 edit_move_block_to_left (edit
);
3037 case CK_Shift_Block_Right
:
3038 if (edit
->mark1
!= edit
->mark2
)
3039 edit_move_block_to_right (edit
);
3042 edit_copy_to_X_buf_cmd (edit
);
3045 edit_cut_to_X_buf_cmd (edit
);
3048 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3049 edit_insert_over (edit
);
3050 edit_paste_from_X_buf_cmd (edit
);
3052 case CK_Selection_History
:
3053 edit_paste_from_history (edit
);
3057 edit_save_as_cmd (edit
);
3060 edit_save_confirm_cmd (edit
);
3063 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3066 edit_save_block_cmd (edit
);
3068 case CK_Insert_File
:
3069 edit_insert_file_cmd (edit
);
3072 case CK_Load_Prev_File
:
3073 edit_load_back_cmd (edit
);
3075 case CK_Load_Next_File
:
3076 edit_load_forward_cmd (edit
);
3079 case CK_Load_Syntax_File
:
3080 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3082 case CK_Choose_Syntax
:
3083 edit_syntax_dialog ();
3086 case CK_Load_Menu_File
:
3087 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3090 case CK_Toggle_Syntax
:
3091 if ((option_syntax_highlighting
^= 1) == 1)
3092 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3093 edit
->force
|= REDRAW_PAGE
;
3096 case CK_Toggle_Tab_TWS
:
3097 enable_show_tabs_tws
^= 1;
3098 edit
->force
|= REDRAW_PAGE
;
3102 edit_search_cmd (edit
, 0);
3105 edit_search_cmd (edit
, 1);
3108 edit_replace_cmd (edit
, 0);
3110 case CK_Replace_Again
:
3111 edit_replace_cmd (edit
, 1);
3113 case CK_Complete_Word
:
3114 /* if text marked shift block */
3115 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3116 edit_move_block_to_left (edit
);
3118 edit_complete_word_cmd (edit
);
3121 case CK_Find_Definition
:
3122 edit_get_match_keyword_cmd (edit
);
3125 dlg_stop (edit
->widget
.parent
);
3128 edit_new_cmd (edit
);
3131 edit_help_cmd (edit
);
3134 edit_refresh_cmd (edit
);
3136 case CK_SaveSetupCmd
:
3140 query_dialog (_(" About "),
3141 _("\n Cooledit v3.11.5\n\n"
3142 " Copyright (C) 1996 the Free Software Foundation\n\n"
3143 " A user friendly text editor written\n"
3144 " for the Midnight Commander.\n"), D_NORMAL
,
3150 case CK_Edit_Options
:
3151 edit_options_dialog ();
3153 case CK_Edit_Save_Mode
:
3154 menu_save_mode_cmd ();
3159 /* fool gcc to prevent a Y2K warning */
3160 char time_format
[] = "_c";
3161 time_format
[0] = '%';
3163 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3164 edit_print_string (edit
, s
);
3165 edit
->force
|= REDRAW_PAGE
;
3170 edit_goto_cmd (edit
);
3172 case CK_Paragraph_Format
:
3173 format_paragraph (edit
, 1);
3174 edit
->force
|= REDRAW_PAGE
;
3176 case CK_Delete_Macro
:
3177 edit_delete_macro_cmd (edit
);
3179 case CK_Match_Bracket
:
3180 edit_goto_matching_bracket (edit
);
3186 edit_sort_cmd (edit
);
3189 edit_ext_cmd (edit
);
3192 edit_mail_dialog (edit
);
3197 case CK_SelectCodepage
:
3198 edit_select_codepage_cmd (edit
);
3200 case CK_Insert_Literal
:
3201 edit_insert_literal_cmd (edit
);
3203 case CK_Execute_Macro
:
3204 edit_execute_macro_cmd (edit
);
3206 case CK_Begin_End_Macro
:
3207 edit_begin_end_macro_cmd (edit
);
3217 if ((command
/ 1000) == 1) /* a shell command */
3218 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3219 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3220 struct macro m
[MAX_MACRO_LENGTH
];
3222 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3223 edit_execute_macro (edit
, m
, nm
);
3226 /* keys which must set the col position, and the search vars */
3231 case CK_Replace_Again
:
3232 case CK_Complete_Word
:
3233 edit
->prev_col
= edit_get_col (edit
);
3236 case CK_Up_Highlight
:
3237 case CK_Up_Alt_Highlight
:
3239 case CK_Down_Highlight
:
3240 case CK_Down_Alt_Highlight
:
3242 case CK_Page_Up_Highlight
:
3243 case CK_Page_Up_Alt_Highlight
:
3245 case CK_Page_Down_Highlight
:
3246 case CK_Page_Down_Alt_Highlight
:
3247 case CK_Beginning_Of_Text
:
3248 case CK_Beginning_Of_Text_Highlight
:
3249 case CK_End_Of_Text
:
3250 case CK_End_Of_Text_Highlight
:
3251 case CK_Paragraph_Up
:
3252 case CK_Paragraph_Up_Highlight
:
3253 case CK_Paragraph_Up_Alt_Highlight
:
3254 case CK_Paragraph_Down
:
3255 case CK_Paragraph_Down_Highlight
:
3256 case CK_Paragraph_Down_Alt_Highlight
:
3258 case CK_Scroll_Up_Highlight
:
3259 case CK_Scroll_Up_Alt_Highlight
:
3260 case CK_Scroll_Down
:
3261 case CK_Scroll_Down_Highlight
:
3262 case CK_Scroll_Down_Alt_Highlight
:
3263 edit
->search_start
= edit
->curs1
;
3264 edit
->found_len
= 0;
3267 edit
->found_len
= 0;
3268 edit
->prev_col
= edit_get_col (edit
);
3269 edit
->search_start
= edit
->curs1
;
3271 edit_find_bracket (edit
);
3273 if (option_auto_para_formatting
) {
3277 case CK_Delete_Word_Left
:
3278 case CK_Delete_Word_Right
:
3279 case CK_Delete_To_Line_End
:
3280 case CK_Delete_To_Line_Begin
:
3281 format_paragraph (edit
, 0);
3282 edit
->force
|= REDRAW_PAGE
;
3289 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3293 if (edit
->macro_depth
++ > 256) {
3294 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3295 edit
->macro_depth
--;
3298 edit
->force
|= REDRAW_PAGE
;
3299 for (; i
< n
; i
++) {
3300 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3302 edit_update_screen (edit
);
3303 edit
->macro_depth
--;
3306 /* User edit menu, like user menu (F2) but only in editor. */
3308 user_menu (WEdit
* edit
)
3312 long start_mark
, end_mark
;
3315 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3317 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3319 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3321 /* run shell scripts from menu */
3322 user_menu_cmd (edit
);
3324 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0)) {
3329 /* i.e. we have marked block */
3330 rc
= edit_block_delete_cmd (edit
);
3334 edit_insert_file (edit
, block_file
);
3336 /* truncate block file */
3337 fd
= fopen (block_file
, "w");
3341 edit_refresh_cmd (edit
);
3342 edit
->force
|= REDRAW_COMPLETELY
;
3344 g_free (block_file
);
3348 edit_stack_init (void)
3350 for (edit_stack_iterator
= 0;
3351 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3352 edit_stack_iterator
++ ) {
3353 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3354 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3357 edit_stack_iterator
= 0;
3361 edit_stack_free (void)
3363 for (edit_stack_iterator
= 0;
3364 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3365 edit_stack_iterator
++)
3366 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3371 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3373 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3378 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3380 edit_move_updown (edit
, i
, do_scroll
, FALSE
);