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 */
1836 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1839 if (edit
->curs_line
> 1) {
1840 if (line_is_blank (edit
, edit
->curs_line
)) {
1841 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1842 for (i
= edit
->curs_line
- 1; i
; i
--)
1843 if (!line_is_blank (edit
, i
)) {
1848 for (i
= edit
->curs_line
- 1; i
; i
--)
1849 if (line_is_blank (edit
, i
))
1853 for (i
= edit
->curs_line
- 1; i
; i
--)
1854 if (line_is_blank (edit
, i
))
1858 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1861 /* moves down until a blank line is reached, or until just
1862 before a non-blank line is reached */
1863 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1866 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1867 i
= edit
->total_lines
;
1869 if (line_is_blank (edit
, edit
->curs_line
)) {
1870 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1871 for (i
= edit
->curs_line
+ 1; i
; i
++)
1872 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1877 for (i
= edit
->curs_line
+ 1; i
; i
++)
1878 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1882 for (i
= edit
->curs_line
+ 1; i
; i
++)
1883 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1887 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1890 static void edit_begin_page (WEdit
*edit
)
1892 edit_update_curs_row (edit
);
1893 edit_move_up (edit
, edit
->curs_row
, 0);
1896 static void edit_end_page (WEdit
*edit
)
1898 edit_update_curs_row (edit
);
1899 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1903 /* goto beginning of text */
1904 static void edit_move_to_top (WEdit
* edit
)
1906 if (edit
->curs_line
) {
1907 edit_cursor_move (edit
, -edit
->curs1
);
1908 edit_move_to_prev_col (edit
, 0);
1909 edit
->force
|= REDRAW_PAGE
;
1910 edit
->search_start
= 0;
1911 edit_update_curs_row(edit
);
1916 /* goto end of text */
1917 static void edit_move_to_bottom (WEdit
* edit
)
1919 if (edit
->curs_line
< edit
->total_lines
) {
1920 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1921 edit
->start_display
= edit
->last_byte
;
1922 edit
->start_line
= edit
->total_lines
;
1923 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1924 edit
->force
|= REDRAW_PAGE
;
1928 /* goto beginning of line */
1929 static void edit_cursor_to_bol (WEdit
* edit
)
1931 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1932 edit
->search_start
= edit
->curs1
;
1933 edit
->prev_col
= edit_get_col (edit
);
1937 /* goto end of line */
1938 static void edit_cursor_to_eol (WEdit
* edit
)
1940 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1941 edit
->search_start
= edit
->curs1
;
1942 edit
->prev_col
= edit_get_col (edit
);
1946 /* move cursor to line 'line' */
1947 void edit_move_to_line (WEdit
* e
, long line
)
1949 if(line
< e
->curs_line
)
1950 edit_move_up (e
, e
->curs_line
- line
, 0);
1952 edit_move_down (e
, line
- e
->curs_line
, 0);
1953 edit_scroll_screen_over_cursor (e
);
1956 /* scroll window so that first visible line is 'line' */
1957 void edit_move_display (WEdit
* e
, long line
)
1959 if(line
< e
->start_line
)
1960 edit_scroll_upward (e
, e
->start_line
- line
);
1962 edit_scroll_downward (e
, line
- e
->start_line
);
1965 /* save markers onto undo stack */
1966 void edit_push_markers (WEdit
* edit
)
1968 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1969 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1972 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1981 /* highlight marker toggle */
1982 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1984 edit_push_markers (edit
);
1986 edit_set_markers (edit
, 0, 0, 0, 0);
1987 edit
->force
|= REDRAW_PAGE
;
1989 if (edit
->mark2
>= 0) {
1990 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
1991 edit
->force
|= REDRAW_PAGE
;
1993 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
1997 static unsigned long
2002 const char option_chars_move_whole_word
[] =
2003 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2008 if (*option_chars_move_whole_word
== '!')
2010 return 0x80000000UL
;
2012 if (g_ascii_isupper ((gchar
) c
))
2014 else if (g_ascii_islower ((gchar
) c
))
2016 else if (g_ascii_isalpha (c
))
2018 else if (isdigit (c
))
2020 else if (isspace (c
))
2022 q
= strchr (option_chars_move_whole_word
, c
);
2024 return 0xFFFFFFFFUL
;
2026 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2030 } while ((q
= strchr (q
+ 1, c
)));
2035 edit_left_word_move (WEdit
*edit
, int s
)
2039 if (column_highlighting
2040 && edit
->mark1
!= edit
->mark2
2041 && edit
->over_col
== 0
2042 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2044 edit_cursor_move (edit
, -1);
2047 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2048 c2
= edit_get_byte (edit
, edit
->curs1
);
2049 if (!(my_type_of (c1
) & my_type_of (c2
)))
2051 if (isspace (c1
) && !isspace (c2
))
2054 if (!isspace (c1
) && isspace (c2
))
2059 static void edit_left_word_move_cmd (WEdit
* edit
)
2061 edit_left_word_move (edit
, 0);
2062 edit
->force
|= REDRAW_PAGE
;
2066 edit_right_word_move (WEdit
*edit
, int s
)
2070 if (column_highlighting
2071 && edit
->mark1
!= edit
->mark2
2072 && edit
->over_col
== 0
2073 && edit
->curs1
== edit_eol(edit
, edit
->curs1
)
2076 edit_cursor_move (edit
, 1);
2077 if (edit
->curs1
>= edit
->last_byte
)
2079 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2080 c2
= edit_get_byte (edit
, edit
->curs1
);
2081 if (!(my_type_of (c1
) & my_type_of (c2
)))
2083 if (isspace (c1
) && !isspace (c2
))
2086 if (!isspace (c1
) && isspace (c2
))
2091 static void edit_right_word_move_cmd (WEdit
* edit
)
2093 edit_right_word_move (edit
, 0);
2094 edit
->force
|= REDRAW_PAGE
;
2097 static void edit_right_char_move_cmd (WEdit
* edit
)
2102 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2106 c
= edit_get_byte (edit
, edit
->curs1
);
2108 if (option_cursor_beyond_eol
&& c
== '\n') {
2111 edit_cursor_move (edit
, cw
);
2115 static void edit_left_char_move_cmd (WEdit
* edit
)
2118 if (column_highlighting
2119 && edit
->mark1
!= edit
->mark2
2120 && edit
->over_col
== 0
2121 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2124 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2128 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2131 edit_cursor_move (edit
, -cw
);
2135 /** Up or down cursor moving.
2136 direction = TRUE - move up
2140 edit_move_updown (WEdit
* edit
, unsigned long i
, int scroll
, gboolean direction
)
2143 unsigned long l
= (direction
)
2145 : edit
->total_lines
- edit
->curs_line
;
2154 edit
->force
|= REDRAW_PAGE
;
2157 edit_scroll_upward (edit
, i
);
2159 edit_scroll_downward (edit
, i
);
2161 p
= edit_bol (edit
, edit
->curs1
);
2164 ? edit_move_backward (edit
, p
, i
)
2165 : edit_move_forward (edit
, p
, i
, 0);
2167 edit_cursor_move (edit
, p
- edit
->curs1
);
2169 edit_move_to_prev_col (edit
, p
);
2171 /* search start of current multibyte char (like CJK) */
2172 if (edit
->curs1
+ 1 < edit
->last_byte
) {
2173 edit_right_char_move_cmd (edit
);
2174 edit_left_char_move_cmd (edit
);
2177 edit
->search_start
= edit
->curs1
;
2178 edit
->found_len
= 0;
2181 static void edit_right_delete_word (WEdit
* edit
)
2185 if (edit
->curs1
>= edit
->last_byte
)
2187 c1
= edit_delete (edit
, 1);
2188 c2
= edit_get_byte (edit
, edit
->curs1
);
2189 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2191 if (!(my_type_of (c1
) & my_type_of (c2
)))
2196 static void edit_left_delete_word (WEdit
* edit
)
2200 if (edit
->curs1
<= 0)
2202 c1
= edit_backspace (edit
, 1);
2203 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2204 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2206 if (!(my_type_of (c1
) & my_type_of (c2
)))
2212 the start column position is not recorded, and hence does not
2213 undo as it happed. But who would notice.
2216 edit_do_undo (WEdit
* edit
)
2221 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2223 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2228 edit_cursor_move (edit
, 1);
2231 edit_cursor_move (edit
, -1);
2234 edit_backspace (edit
, 1);
2237 edit_delete (edit
, 1);
2240 column_highlighting
= 1;
2243 column_highlighting
= 0;
2246 if (ac
>= 256 && ac
< 512)
2247 edit_insert_ahead (edit
, ac
- 256);
2248 if (ac
>= 0 && ac
< 256)
2249 edit_insert (edit
, ac
);
2251 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2252 edit
->mark1
= ac
- MARK_1
;
2253 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2254 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2255 edit
->mark2
= ac
- MARK_2
;
2256 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2259 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2262 if (edit
->start_display
> ac
- KEY_PRESS
) {
2263 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2264 edit
->force
|= REDRAW_PAGE
;
2265 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2266 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2267 edit
->force
|= REDRAW_PAGE
;
2269 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2270 edit_update_curs_row (edit
);
2273 edit
->stack_disable
= 0;
2276 static void edit_delete_to_line_end (WEdit
* edit
)
2278 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2281 edit_delete (edit
, 1);
2285 static void edit_delete_to_line_begin (WEdit
* edit
)
2287 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2290 edit_backspace (edit
, 1);
2295 edit_delete_line (WEdit
*edit
)
2298 * Delete right part of the line.
2299 * Note that edit_get_byte() returns '\n' when byte position is
2302 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2303 (void) edit_delete (edit
, 1);
2308 * Note that edit_delete() will not corrupt anything if called while
2309 * cursor position is EOF.
2311 (void) edit_delete (edit
, 1);
2314 * Delete left part of the line.
2315 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2317 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2318 (void) edit_backspace (edit
, 1);
2322 void insert_spaces_tab (WEdit
* edit
, int half
)
2325 edit_update_curs_col (edit
);
2326 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2328 edit_insert (edit
, ' ');
2333 static int is_aligned_on_a_tab (WEdit
* edit
)
2335 edit_update_curs_col (edit
);
2336 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2337 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2340 static int right_of_four_spaces (WEdit
*edit
)
2343 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2344 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2346 return is_aligned_on_a_tab (edit
);
2350 static int left_of_four_spaces (WEdit
*edit
)
2353 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2354 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2356 return is_aligned_on_a_tab (edit
);
2360 int edit_indent_width (WEdit
* edit
, long p
)
2363 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2365 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2368 void edit_insert_indent (WEdit
* edit
, int indent
)
2370 if (!option_fill_tabs_with_spaces
) {
2371 while (indent
>= TAB_SIZE
) {
2372 edit_insert (edit
, '\t');
2376 while (indent
-- > 0)
2377 edit_insert (edit
, ' ');
2381 edit_auto_indent (WEdit
* edit
)
2386 /* use the previous line as a template */
2387 p
= edit_move_backward (edit
, p
, 1);
2388 /* copy the leading whitespace of the line */
2389 for (;;) { /* no range check - the line _is_ \n-terminated */
2390 c
= edit_get_byte (edit
, p
++);
2391 if (c
!= ' ' && c
!= '\t')
2393 edit_insert (edit
, c
);
2398 edit_double_newline (WEdit
* edit
)
2400 edit_insert (edit
, '\n');
2401 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2403 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2405 edit
->force
|= REDRAW_PAGE
;
2406 edit_insert (edit
, '\n');
2410 edit_tab_cmd (WEdit
* edit
)
2414 if (option_fake_half_tabs
) {
2415 if (is_in_indent (edit
)) {
2416 /*insert a half tab (usually four spaces) unless there is a
2417 half tab already behind, then delete it and insert a
2419 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2420 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2421 edit_backspace (edit
, 1);
2422 edit_insert (edit
, '\t');
2424 insert_spaces_tab (edit
, 1);
2429 if (option_fill_tabs_with_spaces
) {
2430 insert_spaces_tab (edit
, 0);
2432 edit_insert (edit
, '\t');
2436 static void check_and_wrap_line (WEdit
* edit
)
2439 if (!option_typewriter_wrap
)
2441 edit_update_curs_col (edit
);
2442 if (edit
->curs_col
< option_word_wrap_line_length
)
2447 c
= edit_get_byte (edit
, curs
);
2448 if (c
== '\n' || curs
<= 0) {
2449 edit_insert (edit
, '\n');
2452 if (c
== ' ' || c
== '\t') {
2453 int current
= edit
->curs1
;
2454 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2455 edit_insert (edit
, '\n');
2456 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2462 static inline void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2464 void edit_push_key_press (WEdit
* edit
)
2466 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2467 if (edit
->mark2
== -1)
2468 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2471 /* this find the matching bracket in either direction, and sets edit->bracket */
2472 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2474 const char * const b
= "{}{[][()(", *p
;
2475 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2476 unsigned long j
= 0;
2478 edit_update_curs_row (edit
);
2479 c
= edit_get_byte (edit
, edit
->curs1
);
2482 if (!furthest_bracket_search
)
2483 furthest_bracket_search
--;
2484 /* not on a bracket at all */
2487 /* the matching bracket */
2489 /* going left or right? */
2490 if (strchr ("{[(", c
))
2492 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2493 /* out of buffer? */
2494 if (q
>= edit
->last_byte
|| q
< 0)
2496 a
= edit_get_byte (edit
, q
);
2497 /* don't want to eat CPU */
2498 if (j
++ > furthest_bracket_search
)
2500 /* out of screen? */
2502 if (q
< edit
->start_display
)
2504 /* count lines if searching downward */
2505 if (inc
> 0 && a
== '\n')
2506 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2509 /* count bracket depth */
2510 i
+= (a
== c
) - (a
== d
);
2511 /* return if bracket depth is zero */
2519 static long last_bracket
= -1;
2521 void edit_find_bracket (WEdit
* edit
)
2523 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2524 if (last_bracket
!= edit
->bracket
)
2525 edit
->force
|= REDRAW_PAGE
;
2526 last_bracket
= edit
->bracket
;
2530 edit_goto_matching_bracket (WEdit
*edit
)
2534 q
= edit_get_bracket (edit
, 0, 0);
2536 edit
->bracket
= edit
->curs1
;
2537 edit
->force
|= REDRAW_PAGE
;
2538 edit_cursor_move (edit
, q
- edit
->curs1
);
2543 * This executes a command as though the user initiated it through a key
2544 * press. Callback with WIDGET_KEY as a message calls this after
2545 * translating the key press. This function can be used to pass any
2546 * command to the editor. Note that the screen wouldn't update
2547 * automatically. Either of command or char_for_insertion must be
2548 * passed as -1. Commands are executed, and char_for_insertion is
2549 * inserted at the cursor.
2552 edit_execute_key_command (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2554 if (command
== CK_Begin_Record_Macro
) {
2556 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2559 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2560 edit
->force
|= REDRAW_COMPLETELY
;
2561 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2565 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2566 edit
->macro
[edit
->macro_i
].command
= command
;
2567 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2569 /* record the beginning of a set of editing actions initiated by a key press */
2570 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2571 edit_push_key_press (edit
);
2573 edit_execute_cmd (edit
, command
, char_for_insertion
);
2574 if (column_highlighting
)
2575 edit
->force
|= REDRAW_PAGE
;
2578 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2581 This executes a command at a lower level than macro recording.
2582 It also does not push a key_press onto the undo stack. This means
2583 that if it is called many times, a single undo command will undo
2584 all of them. It also does not check for the Undo command.
2587 edit_execute_cmd (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2589 edit
->force
|= REDRAW_LINE
;
2591 /* The next key press will unhighlight the found string, so update
2593 if (edit
->found_len
|| column_highlighting
)
2594 edit
->force
|= REDRAW_PAGE
;
2596 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2597 column_highlighting
= 0;
2598 if (!edit
->highlight
2599 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2600 edit_mark_cmd (edit
, 1); /* clear */
2601 edit_mark_cmd (edit
, 0); /* marking on */
2603 edit
->highlight
= 1;
2604 } else { /* any other command */
2605 if (edit
->highlight
)
2606 edit_mark_cmd (edit
, 0); /* clear */
2607 edit
->highlight
= 0;
2610 /* first check for undo */
2611 if (command
== CK_Undo
) {
2612 edit_do_undo (edit
);
2613 edit
->found_len
= 0;
2614 edit
->prev_col
= edit_get_col (edit
);
2615 edit
->search_start
= edit
->curs1
;
2619 /* An ordinary key press */
2620 if (char_for_insertion
>= 0) {
2621 if (edit
->overwrite
) {
2622 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2623 edit_delete (edit
, 0);
2625 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2626 edit_insert_over (edit
);
2628 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2629 unsigned char str
[6 + 1];
2631 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2638 while ( str
[i
] != 0 && i
<=6) {
2639 char_for_insertion
= str
[i
];
2640 edit_insert (edit
, char_for_insertion
);
2645 edit_insert (edit
, char_for_insertion
);
2647 if (option_auto_para_formatting
) {
2648 format_paragraph (edit
, 0);
2649 edit
->force
|= REDRAW_PAGE
;
2651 check_and_wrap_line (edit
);
2652 edit
->found_len
= 0;
2653 edit
->prev_col
= edit_get_col (edit
);
2654 edit
->search_start
= edit
->curs1
;
2655 edit_find_bracket (edit
);
2662 case CK_Begin_Page_Highlight
:
2663 case CK_End_Page_Highlight
:
2670 if ( edit
->mark2
>= 0 ) {
2671 if ( !option_persistent_selections
) {
2672 if (column_highlighting
)
2673 edit_push_action (edit
, COLUMN_ON
);
2674 column_highlighting
= 0;
2675 edit_mark_cmd (edit
, 1);
2683 case CK_Begin_Page_Highlight
:
2684 case CK_End_Page_Highlight
:
2689 case CK_Word_Left_Highlight
:
2690 case CK_Word_Right_Highlight
:
2691 case CK_Up_Highlight
:
2692 case CK_Down_Highlight
:
2693 case CK_Up_Alt_Highlight
:
2694 case CK_Down_Alt_Highlight
:
2695 if (edit
->mark2
== -1)
2696 break; /*marking is following the cursor: may need to highlight a whole line */
2699 case CK_Left_Highlight
:
2700 case CK_Right_Highlight
:
2701 edit
->force
|= REDRAW_CHAR_ONLY
;
2704 /* basic cursor key commands */
2707 /* if non persistent selection and text selected */
2708 if ( !option_persistent_selections
) {
2709 if ( edit
->mark1
!= edit
->mark2
) {
2710 edit_block_delete_cmd (edit
);
2714 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2718 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2719 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2721 edit_backspace (edit
, 1);
2724 if (option_fake_half_tabs
) {
2726 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2727 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2728 edit_backspace (edit
, 1);
2733 edit_backspace (edit
, 0);
2736 /* if non persistent selection and text selected */
2737 if ( !option_persistent_selections
) {
2738 if ( edit
->mark1
!= edit
->mark2
) {
2739 edit_block_delete_cmd (edit
);
2744 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2745 edit_insert_over (edit
);
2747 if (option_fake_half_tabs
) {
2749 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2750 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2751 edit_delete (edit
, 1);
2755 edit_delete (edit
, 0);
2757 case CK_Delete_Word_Left
:
2759 edit_left_delete_word (edit
);
2761 case CK_Delete_Word_Right
:
2762 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2763 edit_insert_over (edit
);
2765 edit_right_delete_word (edit
);
2767 case CK_Delete_Line
:
2768 edit_delete_line (edit
);
2770 case CK_Delete_To_Line_End
:
2771 edit_delete_to_line_end (edit
);
2773 case CK_Delete_To_Line_Begin
:
2774 edit_delete_to_line_begin (edit
);
2778 if (option_auto_para_formatting
) {
2779 edit_double_newline (edit
);
2780 if (option_return_does_auto_indent
)
2781 edit_auto_indent (edit
);
2782 format_paragraph (edit
, 0);
2784 edit_insert (edit
, '\n');
2785 if (option_return_does_auto_indent
) {
2786 edit_auto_indent (edit
);
2791 edit_insert (edit
, '\n');
2794 case CK_Page_Up_Alt_Highlight
:
2795 column_highlighting
= 1;
2797 case CK_Page_Up_Highlight
:
2798 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2800 case CK_Page_Down_Alt_Highlight
:
2801 column_highlighting
= 1;
2803 case CK_Page_Down_Highlight
:
2804 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2806 case CK_Left_Alt_Highlight
:
2807 column_highlighting
= 1;
2809 case CK_Left_Highlight
:
2810 if (option_fake_half_tabs
) {
2811 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2812 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2815 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2816 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2820 edit_left_char_move_cmd (edit
);
2822 case CK_Right_Alt_Highlight
:
2823 column_highlighting
= 1;
2825 case CK_Right_Highlight
:
2826 if (option_fake_half_tabs
) {
2827 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2828 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2829 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2833 edit_right_char_move_cmd (edit
);
2836 case CK_Begin_Page_Highlight
:
2837 edit_begin_page (edit
);
2840 case CK_End_Page_Highlight
:
2841 edit_end_page (edit
);
2844 case CK_Word_Left_Highlight
:
2846 edit_left_word_move_cmd (edit
);
2849 case CK_Word_Right_Highlight
:
2851 edit_right_word_move_cmd (edit
);
2853 case CK_Up_Alt_Highlight
:
2854 column_highlighting
= 1;
2856 case CK_Up_Highlight
:
2857 edit_move_up (edit
, 1, 0);
2859 case CK_Down_Alt_Highlight
:
2860 column_highlighting
= 1;
2862 case CK_Down_Highlight
:
2863 edit_move_down (edit
, 1, 0);
2865 case CK_Paragraph_Up_Alt_Highlight
:
2866 column_highlighting
= 1;
2867 case CK_Paragraph_Up
:
2868 case CK_Paragraph_Up_Highlight
:
2869 edit_move_up_paragraph (edit
, 0);
2871 case CK_Paragraph_Down_Alt_Highlight
:
2872 column_highlighting
= 1;
2873 case CK_Paragraph_Down
:
2874 case CK_Paragraph_Down_Highlight
:
2875 edit_move_down_paragraph (edit
, 0);
2877 case CK_Scroll_Up_Alt_Highlight
:
2878 column_highlighting
= 1;
2880 case CK_Scroll_Up_Highlight
:
2881 edit_move_up (edit
, 1, 1);
2883 case CK_Scroll_Down_Alt_Highlight
:
2884 column_highlighting
= 1;
2885 case CK_Scroll_Down
:
2886 case CK_Scroll_Down_Highlight
:
2887 edit_move_down (edit
, 1, 1);
2890 case CK_Home_Highlight
:
2891 edit_cursor_to_bol (edit
);
2894 case CK_End_Highlight
:
2895 edit_cursor_to_eol (edit
);
2898 /* if text marked shift block */
2899 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2900 if (edit
->mark2
< 0)
2901 edit_mark_cmd (edit
, 0);
2902 edit_move_block_to_right (edit
);
2904 if ( option_cursor_beyond_eol
)
2905 edit_insert_over (edit
);
2906 edit_tab_cmd (edit
);
2907 if (option_auto_para_formatting
) {
2908 format_paragraph (edit
, 0);
2909 edit
->force
|= REDRAW_PAGE
;
2911 check_and_wrap_line (edit
);
2916 case CK_Toggle_Insert
:
2917 edit
->overwrite
= (edit
->overwrite
== 0);
2921 if (edit
->mark2
>= 0) {
2922 if (column_highlighting
)
2923 edit_push_action (edit
, COLUMN_ON
);
2924 column_highlighting
= 0;
2926 edit_mark_cmd (edit
, 0);
2928 case CK_Column_Mark
:
2929 if (!column_highlighting
)
2930 edit_push_action (edit
, COLUMN_OFF
);
2931 column_highlighting
= 1;
2932 edit_mark_cmd (edit
, 0);
2935 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
2936 edit
->force
|= REDRAW_PAGE
;
2939 if (column_highlighting
)
2940 edit_push_action (edit
, COLUMN_ON
);
2941 column_highlighting
= 0;
2942 edit_mark_cmd (edit
, 1);
2945 case CK_Toggle_Line_State
:
2946 option_line_state
= !option_line_state
;
2947 if ( option_line_state
) {
2948 option_line_state_width
= LINE_STATE_WIDTH
;
2950 option_line_state_width
= 0;
2952 edit
->force
|= REDRAW_PAGE
;
2955 case CK_Toggle_Show_Margin
:
2956 show_right_margin
= !show_right_margin
;
2957 edit
->force
|= REDRAW_PAGE
;
2960 case CK_Toggle_Bookmark
:
2961 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2962 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2963 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2965 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2967 case CK_Flush_Bookmarks
:
2968 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2969 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2970 edit
->force
|= REDRAW_PAGE
;
2972 case CK_Next_Bookmark
:
2973 if (edit
->book_mark
) {
2974 struct _book_mark
*p
;
2975 p
= (struct _book_mark
*) book_mark_find (edit
,
2979 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2980 || p
->line
< edit
->start_line
)
2981 edit_move_display (edit
,
2983 edit
->num_widget_lines
/ 2);
2984 edit_move_to_line (edit
, p
->line
);
2988 case CK_Prev_Bookmark
:
2989 if (edit
->book_mark
) {
2990 struct _book_mark
*p
;
2991 p
= (struct _book_mark
*) book_mark_find (edit
,
2993 while (p
->line
== edit
->curs_line
)
2997 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2998 || p
->line
< edit
->start_line
)
2999 edit_move_display (edit
,
3001 edit
->num_widget_lines
/ 2);
3002 edit_move_to_line (edit
, p
->line
);
3007 case CK_Beginning_Of_Text
:
3008 case CK_Beginning_Of_Text_Highlight
:
3009 edit_move_to_top (edit
);
3011 case CK_End_Of_Text
:
3012 case CK_End_Of_Text_Highlight
:
3013 edit_move_to_bottom (edit
);
3017 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3018 edit_insert_over (edit
);
3019 edit_block_copy_cmd (edit
);
3022 edit_block_delete_cmd (edit
);
3025 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3026 edit_insert_over (edit
);
3027 edit_block_move_cmd (edit
);
3030 case CK_Shift_Block_Left
:
3031 if (edit
->mark1
!= edit
->mark2
)
3032 edit_move_block_to_left (edit
);
3034 case CK_Shift_Block_Right
:
3035 if (edit
->mark1
!= edit
->mark2
)
3036 edit_move_block_to_right (edit
);
3039 edit_copy_to_X_buf_cmd (edit
);
3042 edit_cut_to_X_buf_cmd (edit
);
3045 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3046 edit_insert_over (edit
);
3047 edit_paste_from_X_buf_cmd (edit
);
3049 case CK_Selection_History
:
3050 edit_paste_from_history (edit
);
3054 edit_save_as_cmd (edit
);
3057 edit_save_confirm_cmd (edit
);
3060 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3063 edit_save_block_cmd (edit
);
3065 case CK_Insert_File
:
3066 edit_insert_file_cmd (edit
);
3069 case CK_Load_Prev_File
:
3070 edit_load_back_cmd (edit
);
3072 case CK_Load_Next_File
:
3073 edit_load_forward_cmd (edit
);
3076 case CK_Load_Syntax_File
:
3077 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3079 case CK_Choose_Syntax
:
3080 edit_syntax_dialog ();
3083 case CK_Load_Menu_File
:
3084 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3087 case CK_Toggle_Syntax
:
3088 if ((option_syntax_highlighting
^= 1) == 1)
3089 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3090 edit
->force
|= REDRAW_PAGE
;
3093 case CK_Toggle_Tab_TWS
:
3094 enable_show_tabs_tws
^= 1;
3095 edit
->force
|= REDRAW_PAGE
;
3099 edit_search_cmd (edit
, 0);
3102 edit_search_cmd (edit
, 1);
3105 edit_replace_cmd (edit
, 0);
3107 case CK_Replace_Again
:
3108 edit_replace_cmd (edit
, 1);
3110 case CK_Complete_Word
:
3111 /* if text marked shift block */
3112 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3113 edit_move_block_to_left (edit
);
3115 edit_complete_word_cmd (edit
);
3118 case CK_Find_Definition
:
3119 edit_get_match_keyword_cmd (edit
);
3122 dlg_stop (edit
->widget
.parent
);
3125 edit_new_cmd (edit
);
3128 edit_help_cmd (edit
);
3131 edit_refresh_cmd (edit
);
3133 case CK_SaveSetupCmd
:
3137 query_dialog (_(" About "),
3138 _("\n Cooledit v3.11.5\n\n"
3139 " Copyright (C) 1996 the Free Software Foundation\n\n"
3140 " A user friendly text editor written\n"
3141 " for the Midnight Commander.\n"), D_NORMAL
,
3147 case CK_Edit_Options
:
3148 edit_options_dialog ();
3150 case CK_Edit_Save_Mode
:
3151 menu_save_mode_cmd ();
3156 /* fool gcc to prevent a Y2K warning */
3157 char time_format
[] = "_c";
3158 time_format
[0] = '%';
3160 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3161 edit_print_string (edit
, s
);
3162 edit
->force
|= REDRAW_PAGE
;
3167 edit_goto_cmd (edit
);
3169 case CK_Paragraph_Format
:
3170 format_paragraph (edit
, 1);
3171 edit
->force
|= REDRAW_PAGE
;
3173 case CK_Delete_Macro
:
3174 edit_delete_macro_cmd (edit
);
3176 case CK_Match_Bracket
:
3177 edit_goto_matching_bracket (edit
);
3183 edit_sort_cmd (edit
);
3186 edit_ext_cmd (edit
);
3189 edit_mail_dialog (edit
);
3194 case CK_SelectCodepage
:
3195 edit_select_codepage_cmd (edit
);
3197 case CK_Insert_Literal
:
3198 edit_insert_literal_cmd (edit
);
3200 case CK_Execute_Macro
:
3201 edit_execute_macro_cmd (edit
);
3203 case CK_Begin_End_Macro
:
3204 edit_begin_end_macro_cmd (edit
);
3214 if ((command
/ 1000) == 1) /* a shell command */
3215 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3216 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3217 struct macro m
[MAX_MACRO_LENGTH
];
3219 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3220 edit_execute_macro (edit
, m
, nm
);
3223 /* keys which must set the col position, and the search vars */
3228 case CK_Replace_Again
:
3229 case CK_Complete_Word
:
3230 edit
->prev_col
= edit_get_col (edit
);
3233 case CK_Up_Highlight
:
3234 case CK_Up_Alt_Highlight
:
3236 case CK_Down_Highlight
:
3237 case CK_Down_Alt_Highlight
:
3239 case CK_Page_Up_Highlight
:
3240 case CK_Page_Up_Alt_Highlight
:
3242 case CK_Page_Down_Highlight
:
3243 case CK_Page_Down_Alt_Highlight
:
3244 case CK_Beginning_Of_Text
:
3245 case CK_Beginning_Of_Text_Highlight
:
3246 case CK_End_Of_Text
:
3247 case CK_End_Of_Text_Highlight
:
3248 case CK_Paragraph_Up
:
3249 case CK_Paragraph_Up_Highlight
:
3250 case CK_Paragraph_Up_Alt_Highlight
:
3251 case CK_Paragraph_Down
:
3252 case CK_Paragraph_Down_Highlight
:
3253 case CK_Paragraph_Down_Alt_Highlight
:
3255 case CK_Scroll_Up_Highlight
:
3256 case CK_Scroll_Up_Alt_Highlight
:
3257 case CK_Scroll_Down
:
3258 case CK_Scroll_Down_Highlight
:
3259 case CK_Scroll_Down_Alt_Highlight
:
3260 edit
->search_start
= edit
->curs1
;
3261 edit
->found_len
= 0;
3264 edit
->found_len
= 0;
3265 edit
->prev_col
= edit_get_col (edit
);
3266 edit
->search_start
= edit
->curs1
;
3268 edit_find_bracket (edit
);
3270 if (option_auto_para_formatting
) {
3274 case CK_Delete_Word_Left
:
3275 case CK_Delete_Word_Right
:
3276 case CK_Delete_To_Line_End
:
3277 case CK_Delete_To_Line_Begin
:
3278 format_paragraph (edit
, 0);
3279 edit
->force
|= REDRAW_PAGE
;
3286 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3290 if (edit
->macro_depth
++ > 256) {
3291 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3292 edit
->macro_depth
--;
3295 edit
->force
|= REDRAW_PAGE
;
3296 for (; i
< n
; i
++) {
3297 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3299 edit_update_screen (edit
);
3300 edit
->macro_depth
--;
3303 /* User edit menu, like user menu (F2) but only in editor. */
3305 user_menu (WEdit
* edit
)
3309 long start_mark
, end_mark
;
3312 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3314 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3316 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3318 /* run shell scripts from menu */
3319 user_menu_cmd (edit
);
3321 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0)) {
3326 /* i.e. we have marked block */
3327 rc
= edit_block_delete_cmd (edit
);
3331 edit_insert_file (edit
, block_file
);
3333 /* truncate block file */
3334 fd
= fopen (block_file
, "w");
3338 edit_refresh_cmd (edit
);
3339 edit
->force
|= REDRAW_COMPLETELY
;
3341 g_free (block_file
);
3345 edit_stack_init (void)
3347 for (edit_stack_iterator
= 0;
3348 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3349 edit_stack_iterator
++ ) {
3350 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3351 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3354 edit_stack_iterator
= 0;
3358 edit_stack_free (void)
3360 for (edit_stack_iterator
= 0;
3361 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3362 edit_stack_iterator
++)
3363 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3367 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
3369 edit_move_updown (edit
, i
, scroll
, TRUE
);
3373 void edit_move_down (WEdit
* edit
, unsigned long i
, int scroll
)
3375 edit_move_updown (edit
, i
, scroll
, FALSE
);