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 "../src/global.h"
44 #include "edit-impl.h"
46 #include "edit-widget.h"
47 #include "../src/cmddef.h"
49 #include "../src/tty/color.h" /* EDITOR_NORMAL_COLOR */
50 #include "../src/tty/tty.h" /* attrset() */
51 #include "../src/tty/key.h" /* is_idle() */
53 #include "../src/widget.h" /* buttonbar_redraw() */
54 #include "../src/cmd.h" /* view_other_cmd() */
55 #include "../src/user.h" /* user_menu_cmd() */
56 #include "../src/wtools.h" /* query_dialog() */
57 #include "../src/timefmt.h" /* time formatting */
58 #include "../src/strutil.h" /* utf string functions */
59 #include "../src/charsets.h" /* get_codepage_id */
60 #include "../src/main.h" /* source_codepage */
63 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
64 or EDIT_KEY_EMULATION_EMACS
66 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
68 int option_word_wrap_line_length
= 72;
69 int option_typewriter_wrap
= 0;
70 int option_auto_para_formatting
= 0;
71 int option_tab_spacing
= 8;
72 int option_fill_tabs_with_spaces
= 0;
73 int option_return_does_auto_indent
= 1;
74 int option_backspace_through_tabs
= 0;
75 int option_fake_half_tabs
= 1;
76 int option_save_mode
= EDIT_QUICK_SAVE
;
77 int option_save_position
= 1;
78 int option_max_undo
= 32768;
79 int option_persistent_selections
= 1;
80 int option_cursor_beyond_eol
= 1;
81 int option_line_state
= 0;
82 int option_line_state_width
= 0;
84 int option_edit_right_extreme
= 0;
85 int option_edit_left_extreme
= 0;
86 int option_edit_top_extreme
= 0;
87 int option_edit_bottom_extreme
= 0;
88 int enable_show_tabs_tws
= 1;
90 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
91 char *option_backup_ext
= NULL
;
93 int edit_stack_iterator
= 0;
94 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
95 /* magic sequense for say than block is vertical */
96 const char VERTICAL_MAGIC
[] = {'\1', '\1', '\1', '\1', '\n'};
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \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
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
116 * file end|||file beginning
126 static void user_menu (WEdit
*edit
);
128 int edit_get_byte (WEdit
* edit
, long byte_index
)
131 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
134 if (byte_index
>= edit
->curs1
) {
135 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
136 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
138 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
142 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
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
) ) {
165 if ( byte_index
< 0 ) {
169 if (byte_index
>= edit
->curs1
) {
170 p
= edit
->curs1
+ edit
->curs2
- 1;
171 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
173 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
177 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
182 gchar
*next_ch
= NULL
;
185 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
191 str
= edit_get_byte_ptr (edit
, byte_index
);
192 res
= g_utf8_get_char_validated (str
, -1);
199 /* Calculate UTF-8 char width */
200 next_ch
= g_utf8_next_char(str
);
202 width
= next_ch
- str
;
212 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
214 gchar
*str
, *buf
= NULL
;
217 gchar
*next_ch
= NULL
;
220 if ( byte_index
> 0 ) {
224 ch
= edit_get_utf (edit
, byte_index
, &width
);
230 if ( byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0 ) {
235 str
= edit_get_byte_ptr (edit
, byte_index
);
236 buf
= edit_get_buf_ptr (edit
, byte_index
);
237 /* get prev utf8 char */
239 str
= g_utf8_find_prev_char (buf
, str
);
241 res
= g_utf8_get_char_validated (str
, -1);
247 /* Calculate UTF-8 char width */
248 next_ch
= g_utf8_next_char(str
);
250 width
= next_ch
- str
;
261 * Initialize the buffers for an empty files.
264 edit_init_buffers (WEdit
*edit
)
268 for (j
= 0; j
<= MAXBUFF
; j
++) {
269 edit
->buffers1
[j
] = NULL
;
270 edit
->buffers2
[j
] = NULL
;
275 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
279 * Load file OR text into buffers. Set cursor to the beginning of file.
283 edit_load_file_fast (WEdit
*edit
, const char *filename
)
287 edit
->curs2
= edit
->last_byte
;
288 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
290 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
291 GString
*errmsg
= g_string_new(NULL
);
292 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
293 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
294 g_string_free (errmsg
, TRUE
);
298 if (!edit
->buffers2
[buf2
])
299 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
302 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
303 (edit
->curs2
& M_EDIT_BUF_SIZE
),
304 edit
->curs2
& M_EDIT_BUF_SIZE
);
306 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
307 /* edit->buffers2[0] is already allocated */
308 if (!edit
->buffers2
[buf
])
309 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
310 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
317 /* detecting an error on save is easy: just check if every byte has been written. */
318 /* detecting an error on read, is not so easy 'cos there is not way to tell
319 whether you read everything or not. */
320 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
321 static const struct edit_filters
{
322 const char *read
, *write
, *extension
;
324 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
325 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
326 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
327 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
328 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
331 /* Return index of the filter or -1 is there is no appropriate filter */
332 static int edit_find_filter (const char *filename
)
337 l
= strlen (filename
);
338 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
339 e
= strlen (all_filters
[i
].extension
);
341 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
348 edit_get_filter (const char *filename
)
351 char *p
, *quoted_name
;
352 i
= edit_find_filter (filename
);
355 quoted_name
= name_quote (filename
, 0);
356 l
= str_term_width1 (quoted_name
);
357 p
= g_malloc (str_term_width1 (all_filters
[i
].read
) + l
+ 2);
358 sprintf (p
, all_filters
[i
].read
, quoted_name
);
359 g_free (quoted_name
);
364 edit_get_write_filter (const char *write_name
, const char *filename
)
368 i
= edit_find_filter (filename
);
371 writename
= name_quote (write_name
, 0);
372 l
= str_term_width1 (writename
);
373 p
= g_malloc (str_term_width1 (all_filters
[i
].write
) + l
+ 2);
374 sprintf (p
, all_filters
[i
].write
, writename
);
380 edit_insert_stream (WEdit
* edit
, FILE * f
)
384 while ((c
= fgetc (f
)) >= 0) {
385 edit_insert (edit
, c
);
391 long edit_write_stream (WEdit
* edit
, FILE * f
)
395 if (edit
->lb
== LB_ASIS
) {
396 for (i
= 0; i
< edit
->last_byte
; i
++)
397 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
402 /* change line breaks */
403 for (i
= 0; i
< edit
->last_byte
; i
++) {
404 unsigned char c
= edit_get_byte (edit
, i
);
406 if (!(c
== '\n' || c
== '\r')) {
408 if (fputc (c
, f
) < 0)
410 } else { /* (c == '\n' || c == '\r') */
411 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
414 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
415 /* put one line break unconditionally */
416 if (fputc ('\n', f
) < 0)
419 i
++; /* 2 chars are processed */
421 if (c
== '\r' && c1
== '\n')
422 /* Windows line break; go to the next char */
425 if (c
== '\r' && c1
== '\r') {
426 /* two Macintosh line breaks; put second line break */
427 if (fputc ('\n', f
) < 0)
432 if (fputc (c1
, f
) < 0)
436 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
437 /* put one line break unconditionally */
438 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
441 if (c
== '\r' && c1
== '\n')
442 /* Windows line break; go to the next char */
446 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
447 /* put one line break unconditionally */
448 if (fputc ('\r', f
) < 0)
451 i
++; /* 2 chars are processed */
453 if (c
== '\r' && c1
== '\n')
454 /* Windows line break; go to the next char */
457 if (c
== '\n' && c1
== '\n') {
458 /* two Windows line breaks; put second line break */
459 if (fputc ('\r', f
) < 0)
464 if (fputc (c1
, f
) < 0)
467 case LB_ASIS
: /* default without changes */
473 return edit
->last_byte
;
476 #define TEMP_BUF_LEN 1024
478 /* inserts a file at the cursor, returns 1 on success */
480 edit_insert_file (WEdit
*edit
, const char *filename
)
483 if ((p
= edit_get_filter (filename
))) {
485 long current
= edit
->curs1
;
486 f
= (FILE *) popen (p
, "r");
488 edit_insert_stream (edit
, f
);
489 edit_cursor_move (edit
, current
- edit
->curs1
);
490 if (pclose (f
) > 0) {
491 GString
*errmsg
= g_string_new (NULL
);
492 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
493 edit_error_dialog (_("Error"), errmsg
->str
);
494 g_string_free (errmsg
, TRUE
);
499 GString
*errmsg
= g_string_new (NULL
);
500 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
501 edit_error_dialog (_("Error"), errmsg
->str
);
502 g_string_free (errmsg
, TRUE
);
508 int i
, file
, blocklen
;
509 long current
= edit
->curs1
;
510 int vertical_insertion
= 0;
512 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
514 buf
= g_malloc (TEMP_BUF_LEN
);
515 blocklen
= mc_read (file
, buf
, sizeof(VERTICAL_MAGIC
));
517 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
518 if ( memcmp(buf
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
)) == 0 ) {
519 vertical_insertion
= 1;
521 mc_lseek (file
, 0, SEEK_SET
);
524 if (vertical_insertion
) {
525 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
527 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
528 for (i
= 0; i
< blocklen
; i
++)
529 edit_insert (edit
, buf
[i
]);
532 edit_cursor_move (edit
, current
- edit
->curs1
);
541 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
543 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
546 GString
*errmsg
= (GString
*) 0;
548 /* Try opening an existing file */
549 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
553 * Try creating the file. O_EXCL prevents following broken links
554 * and opening existing files.
558 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
561 g_string_sprintf (errmsg
= g_string_new (NULL
),
562 _(" Cannot open %s for reading "), filename
);
565 /* New file, delete it if it's not modified or saved */
566 edit
->delete_file
= 1;
570 /* Check what we have opened */
571 if (mc_fstat (file
, st
) < 0) {
572 g_string_sprintf (errmsg
= g_string_new (NULL
),
573 _(" Cannot get size/permissions for %s "), filename
);
577 /* We want to open regular files only */
578 if (!S_ISREG (st
->st_mode
)) {
579 g_string_sprintf (errmsg
= g_string_new (NULL
),
580 _(" %s is not a regular file "), filename
);
585 * Don't delete non-empty files.
586 * O_EXCL should prevent it, but let's be on the safe side.
588 if (st
->st_size
> 0) {
589 edit
->delete_file
= 0;
592 if (st
->st_size
>= SIZE_LIMIT
) {
593 g_string_sprintf (errmsg
= g_string_new (NULL
),
594 _(" File %s is too large "), filename
);
599 (void) mc_close (file
);
601 edit_error_dialog (_("Error"), errmsg
->str
);
602 g_string_free (errmsg
, TRUE
);
609 * Open the file and load it into the buffers, either directly or using
610 * a filter. Return 0 on success, 1 on error.
612 * Fast loading (edit_load_file_fast) is used when the file size is
613 * known. In this case the data is read into the buffers by blocks.
614 * If the file size is not known, the data is loaded byte by byte in
618 edit_load_file (WEdit
*edit
)
622 /* Cannot do fast load if a filter is used */
623 if (edit_find_filter (edit
->filename
) >= 0)
627 * VFS may report file size incorrectly, and slow load is not a big
628 * deal considering overhead in VFS.
630 if (!vfs_file_is_local (edit
->filename
))
634 * FIXME: line end translation should disable fast loading as well
635 * Consider doing fseek() to the end and ftell() for the real size.
638 if (*edit
->filename
) {
639 /* If we are dealing with a real file, check that it exists */
640 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
643 /* nothing to load */
647 edit_init_buffers (edit
);
650 edit
->last_byte
= edit
->stat1
.st_size
;
651 edit_load_file_fast (edit
, edit
->filename
);
652 /* If fast load was used, the number of lines wasn't calculated */
653 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
656 const char *codepage_id
;
659 if (*edit
->filename
) {
660 edit
->stack_disable
= 1;
661 if (!edit_insert_file (edit
, edit
->filename
)) {
665 edit
->stack_disable
= 0;
669 codepage_id
= get_codepage_id( source_codepage
);
671 edit
->utf8
= str_isutf8 ( codepage_id
);
678 /* Restore saved cursor position in the file */
680 edit_load_position (WEdit
*edit
)
685 if (!edit
->filename
|| !*edit
->filename
)
688 filename
= vfs_canon (edit
->filename
);
689 load_file_position (filename
, &line
, &column
);
692 edit_move_to_line (edit
, line
- 1);
693 edit
->prev_col
= column
;
694 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
695 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
698 /* Save cursor position in the file */
700 edit_save_position (WEdit
*edit
)
704 if (!edit
->filename
|| !*edit
->filename
)
707 filename
= vfs_canon (edit
->filename
);
708 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
712 /* Clean the WEdit stricture except the widget part */
714 edit_purge_widget (WEdit
*edit
)
716 int len
= sizeof (WEdit
) - sizeof (Widget
);
717 char *start
= (char *) edit
+ sizeof (Widget
);
718 memset (start
, 0, len
);
719 edit
->macro_i
= -1; /* not recording a macro */
723 edit_set_keymap (WEdit
*edit
)
725 edit
->user_map
= default_editor_keymap
;
726 if (editor_keymap
&& editor_keymap
->len
> 0)
727 edit
->user_map
= (global_key_map_t
*) editor_keymap
->data
;
731 #define space_width 1
734 * Fill in the edit structure. Return NULL on failure. Pass edit as
735 * NULL to allocate a new structure.
737 * If line is 0, try to restore saved position. Otherwise put the
738 * cursor on that line and show it in the middle of the screen.
741 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
745 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
746 if ( option_line_state
) {
747 option_line_state_width
= LINE_STATE_WIDTH
;
749 option_line_state_width
= 0;
754 * Expand option_whole_chars_search by national letters using
758 static char option_whole_chars_search_buf
[256];
760 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
762 size_t len
= str_term_width1 (option_whole_chars_search
);
764 strcpy (option_whole_chars_search_buf
,
765 option_whole_chars_search
);
767 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
768 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
769 option_whole_chars_search_buf
[len
++] = i
;
773 option_whole_chars_search_buf
[len
] = 0;
774 option_whole_chars_search
= option_whole_chars_search_buf
;
776 #endif /* ENABLE_NLS */
777 edit
= g_malloc0 (sizeof (WEdit
));
781 edit_purge_widget (edit
);
782 edit
->num_widget_lines
= lines
;
784 edit
->num_widget_columns
= columns
;
785 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
786 edit
->stat1
.st_uid
= getuid ();
787 edit
->stat1
.st_gid
= getgid ();
788 edit
->stat1
.st_mtime
= 0;
790 edit
->force
|= REDRAW_PAGE
;
791 edit_set_filename (edit
, filename
);
792 edit
->stack_size
= START_STACK_SIZE
;
793 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
794 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
795 if (edit_load_file (edit
)) {
796 /* edit_load_file already gives an error message */
802 edit
->converter
= str_cnv_from_term
;
804 const char *cp_id
= NULL
;
805 cp_id
= get_codepage_id (source_codepage
>= 0 ?
806 source_codepage
: display_codepage
);
810 conv
= str_crt_conv_from (cp_id
);
811 if (conv
!= INVALID_CONV
) {
812 if (edit
->converter
!= str_cnv_from_term
)
813 str_close_conv (edit
->converter
);
814 edit
->converter
= conv
;
818 edit
->utf8
= str_isutf8 (cp_id
);
821 edit
->loading_done
= 1;
824 edit_load_syntax (edit
, 0, 0);
827 edit_get_syntax_color (edit
, -1, &color
);
830 /* load saved cursor position */
831 if ((line
== 0) && option_save_position
) {
832 edit_load_position (edit
);
836 edit_move_display (edit
, line
- 1);
837 edit_move_to_line (edit
, line
- 1);
840 edit_set_keymap (edit
);
845 /* Clear the edit struct, freeing everything in it. Return 1 on success */
847 edit_clean (WEdit
*edit
)
854 /* a stale lock, remove it */
856 edit
->locked
= edit_unlock_file (edit
->filename
);
858 /* save cursor position */
859 if (option_save_position
)
860 edit_save_position (edit
);
862 /* File specified on the mcedit command line and never saved */
863 if (edit
->delete_file
)
864 unlink (edit
->filename
);
866 edit_free_syntax_rules (edit
);
867 book_mark_flush (edit
, -1);
868 for (; j
<= MAXBUFF
; j
++) {
869 g_free (edit
->buffers1
[j
]);
870 g_free (edit
->buffers2
[j
]);
873 g_free (edit
->undo_stack
);
874 g_free (edit
->filename
);
879 mc_search_free(edit
->search
);
882 edit_purge_widget (edit
);
888 /* returns 1 on success */
889 int edit_renew (WEdit
* edit
)
891 int lines
= edit
->num_widget_lines
;
892 int columns
= edit
->num_widget_columns
;
896 if (!edit_init (edit
, lines
, columns
, "", 0))
902 * Load a new file into the editor. If it fails, preserve the old file.
903 * To do it, allocate a new widget, initialize it and, if the new file
904 * was loaded, copy the data to the old widget.
905 * Return 1 on success, 0 on failure.
908 edit_reload (WEdit
*edit
, const char *filename
)
911 int lines
= edit
->num_widget_lines
;
912 int columns
= edit
->num_widget_columns
;
914 e
= g_malloc0 (sizeof (WEdit
));
915 e
->widget
= edit
->widget
;
916 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
921 memcpy (edit
, e
, sizeof (WEdit
));
927 * Load a new file into the editor and set line. If it fails, preserve the old file.
928 * To do it, allocate a new widget, initialize it and, if the new file
929 * was loaded, copy the data to the old widget.
930 * Return 1 on success, 0 on failure.
933 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
936 int lines
= edit
->num_widget_lines
;
937 int columns
= edit
->num_widget_columns
;
939 e
= g_malloc0 (sizeof (WEdit
));
940 e
->widget
= edit
->widget
;
941 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
946 memcpy (edit
, e
, sizeof (WEdit
));
953 Recording stack for undo:
954 The following is an implementation of a compressed stack. Identical
955 pushes are recorded by a negative prefix indicating the number of times the
956 same char was pushed. This saves space for repeated curs-left or curs-right
973 If the stack long int is 0-255 it represents a normal insert (from a backspace),
974 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
975 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
976 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
979 The only way the cursor moves or the buffer is changed is through the routines:
980 insert, backspace, insert_ahead, delete, and cursor_move.
981 These record the reverse undo movements onto the stack each time they are
984 Each key press results in a set of actions (insert; delete ...). So each time
985 a key is pressed the current position of start_display is pushed as
986 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
987 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
988 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
992 void edit_push_action (WEdit
* edit
, long c
,...)
994 unsigned long sp
= edit
->stack_pointer
;
998 /* first enlarge the stack if necessary */
999 if (sp
> edit
->stack_size
- 10) { /* say */
1000 if (option_max_undo
< 256)
1001 option_max_undo
= 256;
1002 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1003 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1005 edit
->undo_stack
= t
;
1006 edit
->stack_size
<<= 1;
1007 edit
->stack_size_mask
= edit
->stack_size
- 1;
1011 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1012 if (edit
->stack_disable
)
1015 #ifdef FAST_MOVE_CURSOR
1016 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1018 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1019 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1021 c
= -(va_arg (ap
, int));
1024 #endif /* ! FAST_MOVE_CURSOR */
1025 if (edit
->stack_bottom
!= sp
1026 && spm1
!= edit
->stack_bottom
1027 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1029 if (edit
->undo_stack
[spm1
] < 0) {
1030 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1032 if (edit
->undo_stack
[spm1
] > -1000000000) {
1033 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1034 edit
->undo_stack
[spm1
]--;
1038 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1039 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1040 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1041 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1042 if (edit
->undo_stack
[spm1
] == -2)
1043 edit
->stack_pointer
= spm1
;
1045 edit
->undo_stack
[spm1
]++;
1050 d
= edit
->undo_stack
[spm1
];
1053 return; /* --> no need to push multiple do-nothings */
1054 edit
->undo_stack
[sp
] = -2;
1057 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1058 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1059 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1060 edit
->stack_pointer
= spm1
;
1066 edit
->undo_stack
[sp
] = c
;
1069 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1071 /* if the sp wraps round and catches the stack_bottom then erase
1072 * the first set of actions on the stack to make space - by moving
1073 * stack_bottom forward one "key press" */
1074 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1075 if ((unsigned long) c
== edit
->stack_bottom
||
1076 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1078 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1079 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1081 /*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: */
1082 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1083 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1087 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1088 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1091 pop_action (WEdit
* edit
)
1094 unsigned long sp
= edit
->stack_pointer
;
1095 if (sp
== edit
->stack_bottom
) {
1096 return STACK_BOTTOM
;
1098 sp
= (sp
- 1) & edit
->stack_size_mask
;
1099 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1100 /* edit->undo_stack[sp] = '@'; */
1101 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1104 if (sp
== edit
->stack_bottom
) {
1105 return STACK_BOTTOM
;
1107 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1108 if (edit
->undo_stack
[sp
] == -2) {
1109 /* edit->undo_stack[sp] = '@'; */
1110 edit
->stack_pointer
= sp
;
1112 edit
->undo_stack
[sp
]++;
1117 /* is called whenever a modification is made by one of the four routines below */
1118 static void edit_modification (WEdit
* edit
)
1120 edit
->caches_valid
= 0;
1121 edit
->screen_modified
= 1;
1123 /* raise lock when file modified */
1124 if (!edit
->modified
&& !edit
->delete_file
)
1125 edit
->locked
= edit_lock_file (edit
->filename
);
1130 Basic low level single character buffer alterations and movements at the cursor.
1131 Returns char passed over, inserted or removed.
1135 edit_insert (WEdit
*edit
, int c
)
1137 /* check if file has grown to large */
1138 if (edit
->last_byte
>= SIZE_LIMIT
)
1141 /* first we must update the position of the display window */
1142 if (edit
->curs1
< edit
->start_display
) {
1143 edit
->start_display
++;
1148 /* Mark file as modified, unless the file hasn't been fully loaded */
1149 if (edit
->loading_done
) {
1150 edit_modification (edit
);
1153 /* now we must update some info on the file and check if a redraw is required */
1155 if (edit
->book_mark
)
1156 book_mark_inc (edit
, edit
->curs_line
);
1158 edit
->total_lines
++;
1159 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1162 /* save the reverse command onto the undo stack */
1163 edit_push_action (edit
, BACKSPACE
);
1165 /* update markers */
1166 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1167 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1168 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1170 /* add a new buffer if we've reached the end of the last one */
1171 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1172 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1173 g_malloc (EDIT_BUF_SIZE
);
1175 /* perform the insertion */
1176 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1177 curs1
& M_EDIT_BUF_SIZE
]
1178 = (unsigned char) c
;
1180 /* update file length */
1183 /* update cursor position */
1188 edit_insert_over (WEdit
* edit
)
1192 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1193 edit_insert (edit
, ' ');
1198 /* same as edit_insert and move left */
1199 void edit_insert_ahead (WEdit
* edit
, int c
)
1201 if (edit
->last_byte
>= SIZE_LIMIT
)
1203 if (edit
->curs1
< edit
->start_display
) {
1204 edit
->start_display
++;
1208 edit_modification (edit
);
1210 if (edit
->book_mark
)
1211 book_mark_inc (edit
, edit
->curs_line
);
1212 edit
->total_lines
++;
1213 edit
->force
|= REDRAW_AFTER_CURSOR
;
1215 edit_push_action (edit
, DELCHAR
);
1217 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1218 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1219 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1221 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1222 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1223 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1230 int edit_delete (WEdit
* edit
, const int byte_delete
)
1239 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1240 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1241 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1244 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1245 if ( edit
->utf8
&& byte_delete
== 0 ) {
1246 edit_get_utf (edit
, edit
->curs1
, &cw
);
1250 for ( i
= 1; i
<= cw
; i
++ ) {
1251 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1253 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1254 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1255 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1259 edit_push_action (edit
, p
+ 256);
1262 edit_modification (edit
);
1264 if (edit
->book_mark
)
1265 book_mark_dec (edit
, edit
->curs_line
);
1266 edit
->total_lines
--;
1267 edit
->force
|= REDRAW_AFTER_CURSOR
;
1269 if (edit
->curs1
< edit
->start_display
) {
1270 edit
->start_display
--;
1280 edit_backspace (WEdit
* edit
, const int byte_delete
)
1289 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1290 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1291 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1294 if ( edit
->utf8
&& byte_delete
== 0 ) {
1295 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1299 for ( i
= 1; i
<= cw
; i
++ ) {
1300 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1301 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1302 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1303 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1307 edit_push_action (edit
, p
);
1309 edit_modification (edit
);
1311 if (edit
->book_mark
)
1312 book_mark_dec (edit
, edit
->curs_line
);
1314 edit
->total_lines
--;
1315 edit
->force
|= REDRAW_AFTER_CURSOR
;
1318 if (edit
->curs1
< edit
->start_display
) {
1319 edit
->start_display
--;
1327 #ifdef FAST_MOVE_CURSOR
1329 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1332 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1334 next
-= (unsigned long) dest
;
1342 edit_move_backward_lots (WEdit
*edit
, long increment
)
1347 if (increment
> edit
->curs1
)
1348 increment
= edit
->curs1
;
1351 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1353 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1356 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1361 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1362 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1367 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1368 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1369 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1370 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1373 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1374 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1375 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1380 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1382 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1384 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1385 g_malloc (EDIT_BUF_SIZE
);
1390 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1400 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1402 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1406 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1407 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1410 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1412 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1413 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1418 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1420 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1422 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1423 g_malloc (EDIT_BUF_SIZE
);
1428 return edit_get_byte (edit
, edit
->curs1
);
1431 #endif /* ! FAST_MOVE_CURSOR */
1433 /* moves the cursor right or left: increment positive or negative respectively */
1434 void edit_cursor_move (WEdit
* edit
, long increment
)
1436 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1439 #ifdef FAST_MOVE_CURSOR
1440 if (increment
< -256) {
1441 edit
->force
|= REDRAW_PAGE
;
1442 edit_move_backward_lots (edit
, -increment
);
1445 #endif /* ! FAST_MOVE_CURSOR */
1447 if (increment
< 0) {
1448 for (; increment
< 0; increment
++) {
1452 edit_push_action (edit
, CURS_RIGHT
);
1454 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1455 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1456 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1457 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1459 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1460 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1461 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1462 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1467 edit
->force
|= REDRAW_LINE_BELOW
;
1471 } else if (increment
> 0) {
1472 for (; increment
> 0; increment
--) {
1476 edit_push_action (edit
, CURS_LEFT
);
1478 c
= edit_get_byte (edit
, edit
->curs1
);
1479 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1480 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1481 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1483 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1484 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1485 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1486 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1491 edit
->force
|= REDRAW_LINE_ABOVE
;
1497 /* These functions return positions relative to lines */
1499 /* returns index of last char on line + 1 */
1500 long edit_eol (WEdit
* edit
, long current
)
1502 if (current
< edit
->last_byte
) {
1504 if (edit_get_byte (edit
, current
) == '\n')
1507 return edit
->last_byte
;
1511 /* returns index of first char on line */
1512 long edit_bol (WEdit
* edit
, long current
)
1516 if (edit_get_byte (edit
, current
- 1) == '\n')
1524 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1527 if (upto
> edit
->last_byte
)
1528 upto
= edit
->last_byte
;
1531 while (current
< upto
)
1532 if (edit_get_byte (edit
, current
++) == '\n')
1538 /* If lines is zero this returns the count of lines from current to upto. */
1539 /* If upto is zero returns index of lines forward current. */
1540 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1543 return edit_count_lines (edit
, current
, upto
);
1549 next
= edit_eol (edit
, current
) + 1;
1550 if (next
> edit
->last_byte
)
1560 /* Returns offset of 'lines' lines up from current */
1561 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1565 current
= edit_bol (edit
, current
);
1566 while((lines
--) && current
!= 0)
1567 current
= edit_bol (edit
, current
- 1);
1571 /* If cols is zero this returns the count of columns from current to upto. */
1572 /* If upto is zero returns index of cols across from current. */
1573 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1585 q
= edit
->last_byte
+ 2;
1586 for (col
= 0, p
= current
; p
< q
; p
++) {
1599 if ( !edit
->utf8
) {
1601 c
= edit_get_byte (edit
, p
);
1605 c
= edit_get_byte (edit
, p
);
1606 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1608 if ( utf8_display
) {
1609 if ( edit
->utf8
&& g_unichar_iswide(utf_ch
) )
1614 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1615 else if (c
== '\n') {
1620 } else if (c
< 32 || c
== 127)
1621 col
+= 2; /* Caret notation for control characters */
1632 /* returns the current column position of the cursor */
1633 int edit_get_col (WEdit
* edit
)
1635 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1639 /* Scrolling functions */
1641 void edit_update_curs_row (WEdit
* edit
)
1643 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1646 void edit_update_curs_col (WEdit
* edit
)
1648 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1652 edit_get_curs_col (const WEdit
*edit
)
1654 return edit
->curs_col
;
1657 /*moves the display start position up by i lines */
1658 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1660 unsigned long lines_above
= edit
->start_line
;
1661 if (i
> lines_above
)
1664 edit
->start_line
-= i
;
1665 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1666 edit
->force
|= REDRAW_PAGE
;
1667 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1669 edit_update_curs_row (edit
);
1673 /* returns 1 if could scroll, 0 otherwise */
1674 void edit_scroll_downward (WEdit
* edit
, int i
)
1677 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1678 if (lines_below
> 0) {
1679 if (i
> lines_below
)
1681 edit
->start_line
+= i
;
1682 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1683 edit
->force
|= REDRAW_PAGE
;
1684 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1686 edit_update_curs_row (edit
);
1689 void edit_scroll_right (WEdit
* edit
, int i
)
1691 edit
->force
|= REDRAW_PAGE
;
1692 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1693 edit
->start_col
-= i
;
1696 void edit_scroll_left (WEdit
* edit
, int i
)
1698 if (edit
->start_col
) {
1699 edit
->start_col
+= i
;
1700 if (edit
->start_col
> 0)
1701 edit
->start_col
= 0;
1702 edit
->force
|= REDRAW_PAGE
;
1703 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1707 /* high level cursor movement commands */
1709 static int is_in_indent (WEdit
*edit
)
1711 long p
= edit_bol (edit
, edit
->curs1
);
1712 while (p
< edit
->curs1
)
1713 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1718 static int left_of_four_spaces (WEdit
*edit
);
1721 edit_move_to_prev_col (WEdit
* edit
, long p
)
1723 int prev
= edit
->prev_col
;
1724 int over
= edit
->over_col
;
1726 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1728 if (option_cursor_beyond_eol
) {
1729 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1731 if (line_len
< prev
+ edit
->over_col
) {
1732 edit
->over_col
= prev
+ over
- line_len
;
1733 edit
->prev_col
= line_len
;
1734 edit
->curs_col
= line_len
;
1736 edit
->curs_col
= prev
+ over
;
1737 edit
->prev_col
= edit
->curs_col
;
1742 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1743 edit_update_curs_col (edit
);
1745 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1746 int q
= edit
->curs_col
;
1747 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1748 p
= edit_bol (edit
, edit
->curs1
);
1749 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1750 if (!left_of_four_spaces (edit
))
1751 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1758 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1760 unsigned long p
, l
= edit
->curs_line
;
1766 edit
->force
|= REDRAW_PAGE
;
1768 edit_scroll_upward (edit
, i
);
1770 p
= edit_bol (edit
, edit
->curs1
);
1771 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1772 edit_move_to_prev_col (edit
, p
);
1774 edit
->search_start
= edit
->curs1
;
1775 edit
->found_len
= 0;
1780 is_blank (WEdit
*edit
, long offset
)
1784 s
= edit_bol (edit
, offset
);
1785 f
= edit_eol (edit
, offset
) - 1;
1787 c
= edit_get_byte (edit
, s
++);
1795 /* returns the offset of line i */
1797 edit_find_line (WEdit
*edit
, int line
)
1801 if (!edit
->caches_valid
) {
1802 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1803 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1804 /* three offsets that we *know* are line 0 at 0 and these two: */
1805 edit
->line_numbers
[1] = edit
->curs_line
;
1806 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1807 edit
->line_numbers
[2] = edit
->total_lines
;
1808 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1809 edit
->caches_valid
= 1;
1811 if (line
>= edit
->total_lines
)
1812 return edit
->line_offsets
[2];
1815 /* find the closest known point */
1816 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1818 n
= abs (edit
->line_numbers
[i
] - line
);
1825 return edit
->line_offsets
[j
]; /* know the offset exactly */
1826 if (m
== 1 && j
>= 3)
1827 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1829 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1830 if (line
> edit
->line_numbers
[j
])
1831 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1833 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1834 edit
->line_numbers
[i
] = line
;
1835 return edit
->line_offsets
[i
];
1838 int line_is_blank (WEdit
* edit
, long line
)
1840 return is_blank (edit
, edit_find_line (edit
, line
));
1843 /* moves up until a blank line is reached, or until just
1844 before a non-blank line is reached */
1845 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1848 if (edit
->curs_line
<= 1) {
1851 if (line_is_blank (edit
, edit
->curs_line
)) {
1852 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1853 for (i
= edit
->curs_line
- 1; i
; i
--)
1854 if (!line_is_blank (edit
, i
)) {
1859 for (i
= edit
->curs_line
- 1; i
; i
--)
1860 if (line_is_blank (edit
, i
))
1864 for (i
= edit
->curs_line
- 1; i
; i
--)
1865 if (line_is_blank (edit
, i
))
1869 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1873 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1875 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1881 edit
->force
|= REDRAW_PAGE
;
1883 edit_scroll_downward (edit
, i
);
1884 p
= edit_bol (edit
, edit
->curs1
);
1885 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1886 edit_move_to_prev_col (edit
, p
);
1888 edit
->search_start
= edit
->curs1
;
1889 edit
->found_len
= 0;
1893 /* moves down until a blank line is reached, or until just
1894 before a non-blank line is reached */
1895 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1898 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1899 i
= edit
->total_lines
;
1901 if (line_is_blank (edit
, edit
->curs_line
)) {
1902 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1903 for (i
= edit
->curs_line
+ 1; i
; i
++)
1904 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1909 for (i
= edit
->curs_line
+ 1; i
; i
++)
1910 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1914 for (i
= edit
->curs_line
+ 1; i
; i
++)
1915 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1919 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1922 static void edit_begin_page (WEdit
*edit
)
1924 edit_update_curs_row (edit
);
1925 edit_move_up (edit
, edit
->curs_row
, 0);
1928 static void edit_end_page (WEdit
*edit
)
1930 edit_update_curs_row (edit
);
1931 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1935 /* goto beginning of text */
1936 static void edit_move_to_top (WEdit
* edit
)
1938 if (edit
->curs_line
) {
1939 edit_cursor_move (edit
, -edit
->curs1
);
1940 edit_move_to_prev_col (edit
, 0);
1941 edit
->force
|= REDRAW_PAGE
;
1942 edit
->search_start
= 0;
1943 edit_update_curs_row(edit
);
1948 /* goto end of text */
1949 static void edit_move_to_bottom (WEdit
* edit
)
1951 if (edit
->curs_line
< edit
->total_lines
) {
1952 edit_cursor_move (edit
, edit
->curs2
);
1953 edit
->start_display
= edit
->last_byte
;
1954 edit
->start_line
= edit
->total_lines
;
1955 edit_update_curs_row(edit
);
1956 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1957 edit
->force
|= REDRAW_PAGE
;
1961 /* goto beginning of line */
1962 static void edit_cursor_to_bol (WEdit
* edit
)
1964 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1965 edit
->search_start
= edit
->curs1
;
1966 edit
->prev_col
= edit_get_col (edit
);
1970 /* goto end of line */
1971 static void edit_cursor_to_eol (WEdit
* edit
)
1973 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1974 edit
->search_start
= edit
->curs1
;
1975 edit
->prev_col
= edit_get_col (edit
);
1979 /* move cursor to line 'line' */
1980 void edit_move_to_line (WEdit
* e
, long line
)
1982 if(line
< e
->curs_line
)
1983 edit_move_up (e
, e
->curs_line
- line
, 0);
1985 edit_move_down (e
, line
- e
->curs_line
, 0);
1986 edit_scroll_screen_over_cursor (e
);
1989 /* scroll window so that first visible line is 'line' */
1990 void edit_move_display (WEdit
* e
, long line
)
1992 if(line
< e
->start_line
)
1993 edit_scroll_upward (e
, e
->start_line
- line
);
1995 edit_scroll_downward (e
, line
- e
->start_line
);
1998 /* save markers onto undo stack */
1999 void edit_push_markers (WEdit
* edit
)
2001 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2002 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2005 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2014 /* highlight marker toggle */
2015 void edit_mark_cmd (WEdit
* edit
, int unmark
)
2017 edit_push_markers (edit
);
2019 edit_set_markers (edit
, 0, 0, 0, 0);
2020 edit
->force
|= REDRAW_PAGE
;
2022 if (edit
->mark2
>= 0) {
2023 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
2024 edit
->force
|= REDRAW_PAGE
;
2026 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
2030 static unsigned long
2035 const char option_chars_move_whole_word
[] =
2036 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2041 if (*option_chars_move_whole_word
== '!')
2043 return 0x80000000UL
;
2045 if (g_ascii_isupper ((gchar
) c
))
2047 else if (g_ascii_islower ((gchar
) c
))
2049 else if (g_ascii_isalpha (c
))
2051 else if (isdigit (c
))
2053 else if (isspace (c
))
2055 q
= strchr (option_chars_move_whole_word
, c
);
2057 return 0xFFFFFFFFUL
;
2059 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2063 } while ((q
= strchr (q
+ 1, c
)));
2068 edit_left_word_move (WEdit
*edit
, int s
)
2072 edit_cursor_move (edit
, -1);
2075 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2076 c2
= edit_get_byte (edit
, edit
->curs1
);
2077 if (!(my_type_of (c1
) & my_type_of (c2
)))
2079 if (isspace (c1
) && !isspace (c2
))
2082 if (!isspace (c1
) && isspace (c2
))
2087 static void edit_left_word_move_cmd (WEdit
* edit
)
2089 edit_left_word_move (edit
, 0);
2090 edit
->force
|= REDRAW_PAGE
;
2094 edit_right_word_move (WEdit
*edit
, int s
)
2098 edit_cursor_move (edit
, 1);
2099 if (edit
->curs1
>= edit
->last_byte
)
2101 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2102 c2
= edit_get_byte (edit
, edit
->curs1
);
2103 if (!(my_type_of (c1
) & my_type_of (c2
)))
2105 if (isspace (c1
) && !isspace (c2
))
2108 if (!isspace (c1
) && isspace (c2
))
2113 static void edit_right_word_move_cmd (WEdit
* edit
)
2115 edit_right_word_move (edit
, 0);
2116 edit
->force
|= REDRAW_PAGE
;
2119 static void edit_right_char_move_cmd (WEdit
* edit
)
2124 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2128 c
= edit_get_byte (edit
, edit
->curs1
);
2130 if (option_cursor_beyond_eol
&& c
== '\n') {
2133 edit_cursor_move (edit
, cw
);
2137 static void edit_left_char_move_cmd (WEdit
* edit
)
2141 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2145 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2148 edit_cursor_move (edit
, -cw
);
2153 static void edit_right_delete_word (WEdit
* edit
)
2157 if (edit
->curs1
>= edit
->last_byte
)
2159 c1
= edit_delete (edit
, 1);
2160 c2
= edit_get_byte (edit
, edit
->curs1
);
2161 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2163 if (!(my_type_of (c1
) & my_type_of (c2
)))
2168 static void edit_left_delete_word (WEdit
* edit
)
2172 if (edit
->curs1
<= 0)
2174 c1
= edit_backspace (edit
, 1);
2175 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2176 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2178 if (!(my_type_of (c1
) & my_type_of (c2
)))
2184 the start column position is not recorded, and hence does not
2185 undo as it happed. But who would notice.
2188 edit_do_undo (WEdit
* edit
)
2193 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2195 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2200 edit_cursor_move (edit
, 1);
2203 edit_cursor_move (edit
, -1);
2206 edit_backspace (edit
, 1);
2209 edit_delete (edit
, 1);
2212 column_highlighting
= 1;
2215 column_highlighting
= 0;
2218 if (ac
>= 256 && ac
< 512)
2219 edit_insert_ahead (edit
, ac
- 256);
2220 if (ac
>= 0 && ac
< 256)
2221 edit_insert (edit
, ac
);
2223 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2224 edit
->mark1
= ac
- MARK_1
;
2225 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2226 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2227 edit
->mark2
= ac
- MARK_2
;
2228 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2231 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2234 if (edit
->start_display
> ac
- KEY_PRESS
) {
2235 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2236 edit
->force
|= REDRAW_PAGE
;
2237 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2238 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2239 edit
->force
|= REDRAW_PAGE
;
2241 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2242 edit_update_curs_row (edit
);
2245 edit
->stack_disable
= 0;
2248 static void edit_delete_to_line_end (WEdit
* edit
)
2250 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2253 edit_delete (edit
, 1);
2257 static void edit_delete_to_line_begin (WEdit
* edit
)
2259 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2262 edit_backspace (edit
, 1);
2267 edit_delete_line (WEdit
*edit
)
2270 * Delete right part of the line.
2271 * Note that edit_get_byte() returns '\n' when byte position is
2274 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2275 (void) edit_delete (edit
, 1);
2280 * Note that edit_delete() will not corrupt anything if called while
2281 * cursor position is EOF.
2283 (void) edit_delete (edit
, 1);
2286 * Delete left part of the line.
2287 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2289 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2290 (void) edit_backspace (edit
, 1);
2294 void insert_spaces_tab (WEdit
* edit
, int half
)
2297 edit_update_curs_col (edit
);
2298 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2300 edit_insert (edit
, ' ');
2305 static int is_aligned_on_a_tab (WEdit
* edit
)
2307 edit_update_curs_col (edit
);
2308 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
2309 return 0; /* not alligned on a tab */
2313 static int right_of_four_spaces (WEdit
*edit
)
2316 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2317 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2319 return is_aligned_on_a_tab (edit
);
2323 static int left_of_four_spaces (WEdit
*edit
)
2326 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2327 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2329 return is_aligned_on_a_tab (edit
);
2333 int edit_indent_width (WEdit
* edit
, long p
)
2336 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2338 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2341 void edit_insert_indent (WEdit
* edit
, int indent
)
2343 if (!option_fill_tabs_with_spaces
) {
2344 while (indent
>= TAB_SIZE
) {
2345 edit_insert (edit
, '\t');
2349 while (indent
-- > 0)
2350 edit_insert (edit
, ' ');
2354 edit_auto_indent (WEdit
* edit
)
2359 /* use the previous line as a template */
2360 p
= edit_move_backward (edit
, p
, 1);
2361 /* copy the leading whitespace of the line */
2362 for (;;) { /* no range check - the line _is_ \n-terminated */
2363 c
= edit_get_byte (edit
, p
++);
2364 if (c
!= ' ' && c
!= '\t')
2366 edit_insert (edit
, c
);
2370 static void edit_double_newline (WEdit
* edit
)
2372 edit_insert (edit
, '\n');
2373 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2375 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2377 edit
->force
|= REDRAW_PAGE
;
2378 edit_insert (edit
, '\n');
2381 static void edit_tab_cmd (WEdit
* edit
)
2385 if (option_fake_half_tabs
) {
2386 if (is_in_indent (edit
)) {
2387 /*insert a half tab (usually four spaces) unless there is a
2388 half tab already behind, then delete it and insert a
2390 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2391 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2392 edit_backspace (edit
, 1);
2393 edit_insert (edit
, '\t');
2395 insert_spaces_tab (edit
, 1);
2400 if (option_fill_tabs_with_spaces
) {
2401 insert_spaces_tab (edit
, 0);
2403 edit_insert (edit
, '\t');
2408 static void check_and_wrap_line (WEdit
* edit
)
2411 if (!option_typewriter_wrap
)
2413 edit_update_curs_col (edit
);
2414 if (edit
->curs_col
< option_word_wrap_line_length
)
2419 c
= edit_get_byte (edit
, curs
);
2420 if (c
== '\n' || curs
<= 0) {
2421 edit_insert (edit
, '\n');
2424 if (c
== ' ' || c
== '\t') {
2425 int current
= edit
->curs1
;
2426 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2427 edit_insert (edit
, '\n');
2428 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2434 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2436 void edit_push_key_press (WEdit
* edit
)
2438 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2439 if (edit
->mark2
== -1)
2440 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2443 /* this find the matching bracket in either direction, and sets edit->bracket */
2444 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2446 const char * const b
= "{}{[][()(", *p
;
2447 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2448 unsigned long j
= 0;
2450 edit_update_curs_row (edit
);
2451 c
= edit_get_byte (edit
, edit
->curs1
);
2454 if (!furthest_bracket_search
)
2455 furthest_bracket_search
--;
2456 /* not on a bracket at all */
2459 /* the matching bracket */
2461 /* going left or right? */
2462 if (strchr ("{[(", c
))
2464 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2465 /* out of buffer? */
2466 if (q
>= edit
->last_byte
|| q
< 0)
2468 a
= edit_get_byte (edit
, q
);
2469 /* don't want to eat CPU */
2470 if (j
++ > furthest_bracket_search
)
2472 /* out of screen? */
2474 if (q
< edit
->start_display
)
2476 /* count lines if searching downward */
2477 if (inc
> 0 && a
== '\n')
2478 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2481 /* count bracket depth */
2482 i
+= (a
== c
) - (a
== d
);
2483 /* return if bracket depth is zero */
2491 static long last_bracket
= -1;
2493 void edit_find_bracket (WEdit
* edit
)
2495 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2496 if (last_bracket
!= edit
->bracket
)
2497 edit
->force
|= REDRAW_PAGE
;
2498 last_bracket
= edit
->bracket
;
2501 static void edit_goto_matching_bracket (WEdit
*edit
)
2504 q
= edit_get_bracket (edit
, 0, 0);
2507 edit
->bracket
= edit
->curs1
;
2508 edit
->force
|= REDRAW_PAGE
;
2509 edit_cursor_move (edit
, q
- edit
->curs1
);
2513 * This executes a command as though the user initiated it through a key
2514 * press. Callback with WIDGET_KEY as a message calls this after
2515 * translating the key press. This function can be used to pass any
2516 * command to the editor. Note that the screen wouldn't update
2517 * automatically. Either of command or char_for_insertion must be
2518 * passed as -1. Commands are executed, and char_for_insertion is
2519 * inserted at the cursor.
2521 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2523 if (command
== CK_Begin_Record_Macro
) {
2525 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2528 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2529 edit
->force
|= REDRAW_COMPLETELY
;
2530 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2534 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2535 edit
->macro
[edit
->macro_i
].command
= command
;
2536 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2538 /* record the beginning of a set of editing actions initiated by a key press */
2539 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2540 edit_push_key_press (edit
);
2542 edit_execute_cmd (edit
, command
, char_for_insertion
);
2543 if (column_highlighting
)
2544 edit
->force
|= REDRAW_PAGE
;
2547 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2550 This executes a command at a lower level than macro recording.
2551 It also does not push a key_press onto the undo stack. This means
2552 that if it is called many times, a single undo command will undo
2553 all of them. It also does not check for the Undo command.
2556 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2558 edit
->force
|= REDRAW_LINE
;
2560 /* The next key press will unhighlight the found string, so update
2562 if (edit
->found_len
|| column_highlighting
)
2563 edit
->force
|= REDRAW_PAGE
;
2565 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2566 column_highlighting
= 0;
2567 if (!edit
->highlight
2568 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2569 edit_mark_cmd (edit
, 1); /* clear */
2570 edit_mark_cmd (edit
, 0); /* marking on */
2572 edit
->highlight
= 1;
2573 } else { /* any other command */
2574 if (edit
->highlight
)
2575 edit_mark_cmd (edit
, 0); /* clear */
2576 edit
->highlight
= 0;
2579 /* first check for undo */
2580 if (command
== CK_Undo
) {
2581 edit_do_undo (edit
);
2582 edit
->found_len
= 0;
2583 edit
->prev_col
= edit_get_col (edit
);
2584 edit
->search_start
= edit
->curs1
;
2588 /* An ordinary key press */
2589 if (char_for_insertion
>= 0) {
2590 if (edit
->overwrite
) {
2591 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2592 edit_delete (edit
, 0);
2594 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2595 edit_insert_over (edit
);
2597 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2598 unsigned char str
[6 + 1];
2600 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2607 while ( str
[i
] != 0 && i
<=6) {
2608 char_for_insertion
= str
[i
];
2609 edit_insert (edit
, char_for_insertion
);
2614 edit_insert (edit
, char_for_insertion
);
2618 if (option_auto_para_formatting
) {
2619 format_paragraph (edit
, 0);
2620 edit
->force
|= REDRAW_PAGE
;
2622 check_and_wrap_line (edit
);
2623 edit
->found_len
= 0;
2624 edit
->prev_col
= edit_get_col (edit
);
2625 edit
->search_start
= edit
->curs1
;
2626 edit_find_bracket (edit
);
2633 case CK_Begin_Page_Highlight
:
2634 case CK_End_Page_Highlight
:
2641 if ( edit
->mark2
>= 0 ) {
2642 if ( !option_persistent_selections
) {
2643 if (column_highlighting
)
2644 edit_push_action (edit
, COLUMN_ON
);
2645 column_highlighting
= 0;
2646 edit_mark_cmd (edit
, 1);
2654 case CK_Begin_Page_Highlight
:
2655 case CK_End_Page_Highlight
:
2660 case CK_Word_Left_Highlight
:
2661 case CK_Word_Right_Highlight
:
2662 case CK_Up_Highlight
:
2663 case CK_Down_Highlight
:
2664 case CK_Up_Alt_Highlight
:
2665 case CK_Down_Alt_Highlight
:
2666 if (edit
->mark2
== -1)
2667 break; /*marking is following the cursor: may need to highlight a whole line */
2670 case CK_Left_Highlight
:
2671 case CK_Right_Highlight
:
2672 edit
->force
|= REDRAW_CHAR_ONLY
;
2675 /* basic cursor key commands */
2678 /* if non persistent selection and text selected */
2679 if ( !option_persistent_selections
) {
2680 if ( edit
->mark1
!= edit
->mark2
) {
2681 edit_block_delete_cmd (edit
);
2685 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2689 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2690 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2692 edit_backspace (edit
, 1);
2695 if (option_fake_half_tabs
) {
2697 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2698 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2699 edit_backspace (edit
, 1);
2704 edit_backspace (edit
, 0);
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
);
2715 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2716 edit_insert_over (edit
);
2718 if (option_fake_half_tabs
) {
2720 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2721 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2722 edit_delete (edit
, 1);
2726 edit_delete (edit
, 0);
2728 case CK_Delete_Word_Left
:
2730 edit_left_delete_word (edit
);
2732 case CK_Delete_Word_Right
:
2733 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2734 edit_insert_over (edit
);
2736 edit_right_delete_word (edit
);
2738 case CK_Delete_Line
:
2739 edit_delete_line (edit
);
2741 case CK_Delete_To_Line_End
:
2742 edit_delete_to_line_end (edit
);
2744 case CK_Delete_To_Line_Begin
:
2745 edit_delete_to_line_begin (edit
);
2749 if (option_auto_para_formatting
) {
2750 edit_double_newline (edit
);
2751 if (option_return_does_auto_indent
)
2752 edit_auto_indent (edit
);
2753 format_paragraph (edit
, 0);
2755 edit_insert (edit
, '\n');
2756 if (option_return_does_auto_indent
) {
2757 edit_auto_indent (edit
);
2762 edit_insert (edit
, '\n');
2765 case CK_Page_Up_Alt_Highlight
:
2766 column_highlighting
= 1;
2768 case CK_Page_Up_Highlight
:
2769 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2771 case CK_Page_Down_Alt_Highlight
:
2772 column_highlighting
= 1;
2774 case CK_Page_Down_Highlight
:
2775 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2777 case CK_Left_Alt_Highlight
:
2778 column_highlighting
= 1;
2780 case CK_Left_Highlight
:
2781 if (option_fake_half_tabs
) {
2782 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2783 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2786 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2787 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2791 edit_left_char_move_cmd (edit
);
2793 case CK_Right_Alt_Highlight
:
2794 column_highlighting
= 1;
2796 case CK_Right_Highlight
:
2797 if (option_fake_half_tabs
) {
2798 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2799 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2800 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2804 edit_right_char_move_cmd (edit
);
2807 case CK_Begin_Page_Highlight
:
2808 edit_begin_page (edit
);
2811 case CK_End_Page_Highlight
:
2812 edit_end_page (edit
);
2815 case CK_Word_Left_Highlight
:
2817 edit_left_word_move_cmd (edit
);
2820 case CK_Word_Right_Highlight
:
2822 edit_right_word_move_cmd (edit
);
2824 case CK_Up_Alt_Highlight
:
2825 column_highlighting
= 1;
2827 case CK_Up_Highlight
:
2828 edit_move_up (edit
, 1, 0);
2830 case CK_Down_Alt_Highlight
:
2831 column_highlighting
= 1;
2833 case CK_Down_Highlight
:
2834 edit_move_down (edit
, 1, 0);
2836 case CK_Paragraph_Up_Alt_Highlight
:
2837 column_highlighting
= 1;
2838 case CK_Paragraph_Up
:
2839 case CK_Paragraph_Up_Highlight
:
2840 edit_move_up_paragraph (edit
, 0);
2842 case CK_Paragraph_Down_Alt_Highlight
:
2843 column_highlighting
= 1;
2844 case CK_Paragraph_Down
:
2845 case CK_Paragraph_Down_Highlight
:
2846 edit_move_down_paragraph (edit
, 0);
2848 case CK_Scroll_Up_Alt_Highlight
:
2849 column_highlighting
= 1;
2851 case CK_Scroll_Up_Highlight
:
2852 edit_move_up (edit
, 1, 1);
2854 case CK_Scroll_Down_Alt_Highlight
:
2855 column_highlighting
= 1;
2856 case CK_Scroll_Down
:
2857 case CK_Scroll_Down_Highlight
:
2858 edit_move_down (edit
, 1, 1);
2861 case CK_Home_Highlight
:
2862 edit_cursor_to_bol (edit
);
2865 case CK_End_Highlight
:
2866 edit_cursor_to_eol (edit
);
2869 /* if text marked shift block */
2870 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2871 edit_move_block_to_right (edit
);
2873 if ( option_cursor_beyond_eol
)
2874 edit_insert_over (edit
);
2875 edit_tab_cmd (edit
);
2876 if (option_auto_para_formatting
) {
2877 format_paragraph (edit
, 0);
2878 edit
->force
|= REDRAW_PAGE
;
2880 check_and_wrap_line (edit
);
2885 case CK_Toggle_Insert
:
2886 edit
->overwrite
= (edit
->overwrite
== 0);
2890 if (edit
->mark2
>= 0) {
2891 if (column_highlighting
)
2892 edit_push_action (edit
, COLUMN_ON
);
2893 column_highlighting
= 0;
2895 edit_mark_cmd (edit
, 0);
2897 case CK_Column_Mark
:
2898 if (!column_highlighting
)
2899 edit_push_action (edit
, COLUMN_OFF
);
2900 column_highlighting
= 1;
2901 edit_mark_cmd (edit
, 0);
2904 if (column_highlighting
)
2905 edit_push_action (edit
, COLUMN_ON
);
2906 column_highlighting
= 0;
2907 edit_mark_cmd (edit
, 1);
2910 case CK_Toggle_Line_State
:
2911 option_line_state
= !option_line_state
;
2912 if ( option_line_state
) {
2913 option_line_state_width
= LINE_STATE_WIDTH
;
2915 option_line_state_width
= 0;
2917 edit
->force
|= REDRAW_PAGE
;
2920 case CK_Toggle_Bookmark
:
2921 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2922 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2923 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2925 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2927 case CK_Flush_Bookmarks
:
2928 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2929 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2930 edit
->force
|= REDRAW_PAGE
;
2932 case CK_Next_Bookmark
:
2933 if (edit
->book_mark
) {
2934 struct _book_mark
*p
;
2935 p
= (struct _book_mark
*) book_mark_find (edit
,
2939 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2940 || p
->line
< edit
->start_line
)
2941 edit_move_display (edit
,
2943 edit
->num_widget_lines
/ 2);
2944 edit_move_to_line (edit
, p
->line
);
2948 case CK_Prev_Bookmark
:
2949 if (edit
->book_mark
) {
2950 struct _book_mark
*p
;
2951 p
= (struct _book_mark
*) book_mark_find (edit
,
2953 while (p
->line
== edit
->curs_line
)
2957 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2958 || p
->line
< edit
->start_line
)
2959 edit_move_display (edit
,
2961 edit
->num_widget_lines
/ 2);
2962 edit_move_to_line (edit
, p
->line
);
2967 case CK_Beginning_Of_Text
:
2968 case CK_Beginning_Of_Text_Highlight
:
2969 edit_move_to_top (edit
);
2971 case CK_End_Of_Text
:
2972 case CK_End_Of_Text_Highlight
:
2973 edit_move_to_bottom (edit
);
2977 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2978 edit_insert_over (edit
);
2979 edit_block_copy_cmd (edit
);
2982 edit_block_delete_cmd (edit
);
2985 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2986 edit_insert_over (edit
);
2987 edit_block_move_cmd (edit
);
2991 edit_copy_to_X_buf_cmd (edit
);
2994 edit_cut_to_X_buf_cmd (edit
);
2997 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2998 edit_insert_over (edit
);
2999 edit_paste_from_X_buf_cmd (edit
);
3001 case CK_Selection_History
:
3002 edit_paste_from_history (edit
);
3006 edit_save_as_cmd (edit
);
3009 edit_save_confirm_cmd (edit
);
3012 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3015 edit_save_block_cmd (edit
);
3017 case CK_Insert_File
:
3018 edit_insert_file_cmd (edit
);
3021 case CK_Load_Prev_File
:
3022 edit_load_back_cmd (edit
);
3024 case CK_Load_Next_File
:
3025 edit_load_forward_cmd (edit
);
3028 case CK_Load_Syntax_File
:
3029 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3031 case CK_Load_Menu_File
:
3032 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3035 case CK_Toggle_Syntax
:
3036 if ((option_syntax_highlighting
^= 1) == 1)
3037 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3038 edit
->force
|= REDRAW_PAGE
;
3041 case CK_Toggle_Tab_TWS
:
3042 enable_show_tabs_tws
^= 1;
3043 edit
->force
|= REDRAW_PAGE
;
3047 edit_search_cmd (edit
, 0);
3050 edit_search_cmd (edit
, 1);
3053 edit_replace_cmd (edit
, 0);
3055 case CK_Replace_Again
:
3056 edit_replace_cmd (edit
, 1);
3058 case CK_Complete_Word
:
3059 /* if text marked shift block */
3060 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3061 edit_move_block_to_left (edit
);
3063 edit_complete_word_cmd (edit
);
3066 case CK_Find_Definition
:
3067 edit_get_match_keyword_cmd (edit
);
3071 dlg_stop (edit
->widget
.parent
);
3074 edit_new_cmd (edit
);
3078 edit_help_cmd (edit
);
3082 edit_refresh_cmd (edit
);
3087 /* fool gcc to prevent a Y2K warning */
3088 char time_format
[] = "_c";
3089 time_format
[0] = '%';
3091 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3092 edit_print_string (edit
, s
);
3093 edit
->force
|= REDRAW_PAGE
;
3097 edit_goto_cmd (edit
);
3099 case CK_Paragraph_Format
:
3100 format_paragraph (edit
, 1);
3101 edit
->force
|= REDRAW_PAGE
;
3103 case CK_Delete_Macro
:
3104 edit_delete_macro_cmd (edit
);
3106 case CK_Match_Bracket
:
3107 edit_goto_matching_bracket (edit
);
3113 edit_sort_cmd (edit
);
3116 edit_ext_cmd (edit
);
3119 edit_mail_dialog (edit
);
3124 case CK_SelectCodepage
:
3125 edit_select_codepage_cmd (edit
);
3127 case CK_Insert_Literal
:
3128 edit_insert_literal_cmd (edit
);
3130 case CK_Execute_Macro
:
3131 edit_execute_macro_cmd (edit
);
3133 case CK_Begin_End_Macro
:
3134 edit_begin_end_macro_cmd (edit
);
3144 if ((command
/ 1000) == 1) /* a shell command */
3145 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3146 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3147 struct macro m
[MAX_MACRO_LENGTH
];
3149 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3150 edit_execute_macro (edit
, m
, nm
);
3153 /* keys which must set the col position, and the search vars */
3158 case CK_Replace_Again
:
3159 case CK_Complete_Word
:
3160 edit
->prev_col
= edit_get_col (edit
);
3163 case CK_Up_Highlight
:
3164 case CK_Up_Alt_Highlight
:
3166 case CK_Down_Highlight
:
3167 case CK_Down_Alt_Highlight
:
3169 case CK_Page_Up_Highlight
:
3170 case CK_Page_Up_Alt_Highlight
:
3172 case CK_Page_Down_Highlight
:
3173 case CK_Page_Down_Alt_Highlight
:
3174 case CK_Beginning_Of_Text
:
3175 case CK_Beginning_Of_Text_Highlight
:
3176 case CK_End_Of_Text
:
3177 case CK_End_Of_Text_Highlight
:
3178 case CK_Paragraph_Up
:
3179 case CK_Paragraph_Up_Highlight
:
3180 case CK_Paragraph_Up_Alt_Highlight
:
3181 case CK_Paragraph_Down
:
3182 case CK_Paragraph_Down_Highlight
:
3183 case CK_Paragraph_Down_Alt_Highlight
:
3185 case CK_Scroll_Up_Highlight
:
3186 case CK_Scroll_Up_Alt_Highlight
:
3187 case CK_Scroll_Down
:
3188 case CK_Scroll_Down_Highlight
:
3189 case CK_Scroll_Down_Alt_Highlight
:
3190 edit
->search_start
= edit
->curs1
;
3191 edit
->found_len
= 0;
3194 edit
->found_len
= 0;
3195 edit
->prev_col
= edit_get_col (edit
);
3196 edit
->search_start
= edit
->curs1
;
3198 edit_find_bracket (edit
);
3200 if (option_auto_para_formatting
) {
3204 case CK_Delete_Word_Left
:
3205 case CK_Delete_Word_Right
:
3206 case CK_Delete_To_Line_End
:
3207 case CK_Delete_To_Line_Begin
:
3208 format_paragraph (edit
, 0);
3209 edit
->force
|= REDRAW_PAGE
;
3216 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3220 if (edit
->macro_depth
++ > 256) {
3221 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3222 edit
->macro_depth
--;
3225 edit
->force
|= REDRAW_PAGE
;
3226 for (; i
< n
; i
++) {
3227 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3229 edit_update_screen (edit
);
3230 edit
->macro_depth
--;
3233 /* User edit menu, like user menu (F2) but only in editor. */
3235 user_menu (WEdit
* edit
)
3240 long start_mark
, end_mark
;
3241 char *block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3244 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3245 if (!nomark
) /* remember marked or not */
3246 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3248 /* run shell scripts from menu */
3249 user_menu_cmd (edit
);
3251 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
3252 /* no block messages */
3257 /* i.e. we have marked block */
3258 rc
= edit_block_delete_cmd (edit
);
3262 edit_insert_file (edit
, block_file
);
3265 /* truncate block file */
3266 if ((fd
= fopen (block_file
, "w"))) {
3270 edit_refresh_cmd (edit
);
3271 edit
->force
|= REDRAW_COMPLETELY
;
3274 g_free (block_file
);
3278 edit_stack_init (void)
3280 for (edit_stack_iterator
= 0;
3281 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3282 edit_stack_iterator
++ ) {
3283 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3284 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3287 edit_stack_iterator
= 0;
3291 edit_stack_free (void)
3293 for (edit_stack_iterator
= 0;
3294 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3295 edit_stack_iterator
++)
3296 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);