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
= (global_key_map_t
*) editor_keymap
->data
;
729 #define space_width 1
732 * Fill in the edit structure. Return NULL on failure. Pass edit as
733 * NULL to allocate a new structure.
735 * If line is 0, try to restore saved position. Otherwise put the
736 * cursor on that line and show it in the middle of the screen.
739 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
743 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
744 if ( option_line_state
) {
745 option_line_state_width
= LINE_STATE_WIDTH
;
747 option_line_state_width
= 0;
752 * Expand option_whole_chars_search by national letters using
756 static char option_whole_chars_search_buf
[256];
758 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
760 size_t len
= str_term_width1 (option_whole_chars_search
);
762 strcpy (option_whole_chars_search_buf
,
763 option_whole_chars_search
);
765 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
766 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
767 option_whole_chars_search_buf
[len
++] = i
;
771 option_whole_chars_search_buf
[len
] = 0;
772 option_whole_chars_search
= option_whole_chars_search_buf
;
774 #endif /* ENABLE_NLS */
775 edit
= g_malloc0 (sizeof (WEdit
));
779 edit_purge_widget (edit
);
780 edit
->num_widget_lines
= lines
;
782 edit
->num_widget_columns
= columns
;
783 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
784 edit
->stat1
.st_uid
= getuid ();
785 edit
->stat1
.st_gid
= getgid ();
786 edit
->stat1
.st_mtime
= 0;
788 edit
->force
|= REDRAW_PAGE
;
789 edit_set_filename (edit
, filename
);
790 edit
->stack_size
= START_STACK_SIZE
;
791 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
792 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
793 if (edit_load_file (edit
)) {
794 /* edit_load_file already gives an error message */
800 edit
->converter
= str_cnv_from_term
;
802 const char *cp_id
= NULL
;
803 cp_id
= get_codepage_id (source_codepage
>= 0 ?
804 source_codepage
: display_codepage
);
808 conv
= str_crt_conv_from (cp_id
);
809 if (conv
!= INVALID_CONV
) {
810 if (edit
->converter
!= str_cnv_from_term
)
811 str_close_conv (edit
->converter
);
812 edit
->converter
= conv
;
816 edit
->utf8
= str_isutf8 (cp_id
);
819 edit
->loading_done
= 1;
822 edit_load_syntax (edit
, 0, 0);
825 edit_get_syntax_color (edit
, -1, &color
);
828 /* load saved cursor position */
829 if ((line
== 0) && option_save_position
) {
830 edit_load_position (edit
);
834 edit_move_display (edit
, line
- 1);
835 edit_move_to_line (edit
, line
- 1);
838 edit_set_keymap (edit
);
843 /* Clear the edit struct, freeing everything in it. Return 1 on success */
845 edit_clean (WEdit
*edit
)
852 /* a stale lock, remove it */
854 edit
->locked
= edit_unlock_file (edit
->filename
);
856 /* save cursor position */
857 if (option_save_position
)
858 edit_save_position (edit
);
860 /* File specified on the mcedit command line and never saved */
861 if (edit
->delete_file
)
862 unlink (edit
->filename
);
864 edit_free_syntax_rules (edit
);
865 book_mark_flush (edit
, -1);
866 for (; j
<= MAXBUFF
; j
++) {
867 g_free (edit
->buffers1
[j
]);
868 g_free (edit
->buffers2
[j
]);
871 g_free (edit
->undo_stack
);
872 g_free (edit
->filename
);
877 mc_search_free(edit
->search
);
880 edit_purge_widget (edit
);
886 /* returns 1 on success */
887 int edit_renew (WEdit
* edit
)
889 int lines
= edit
->num_widget_lines
;
890 int columns
= edit
->num_widget_columns
;
894 if (!edit_init (edit
, lines
, columns
, "", 0))
900 * Load a new file into the editor. If it fails, preserve the old file.
901 * To do it, allocate a new widget, initialize it and, if the new file
902 * was loaded, copy the data to the old widget.
903 * Return 1 on success, 0 on failure.
906 edit_reload (WEdit
*edit
, const char *filename
)
909 int lines
= edit
->num_widget_lines
;
910 int columns
= edit
->num_widget_columns
;
912 e
= g_malloc0 (sizeof (WEdit
));
913 e
->widget
= edit
->widget
;
914 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
919 memcpy (edit
, e
, sizeof (WEdit
));
925 * Load a new file into the editor and set line. If it fails, preserve the old file.
926 * To do it, allocate a new widget, initialize it and, if the new file
927 * was loaded, copy the data to the old widget.
928 * Return 1 on success, 0 on failure.
931 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
934 int lines
= edit
->num_widget_lines
;
935 int columns
= edit
->num_widget_columns
;
937 e
= g_malloc0 (sizeof (WEdit
));
938 e
->widget
= edit
->widget
;
939 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
944 memcpy (edit
, e
, sizeof (WEdit
));
951 Recording stack for undo:
952 The following is an implementation of a compressed stack. Identical
953 pushes are recorded by a negative prefix indicating the number of times the
954 same char was pushed. This saves space for repeated curs-left or curs-right
971 If the stack long int is 0-255 it represents a normal insert (from a backspace),
972 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
973 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
974 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
977 The only way the cursor moves or the buffer is changed is through the routines:
978 insert, backspace, insert_ahead, delete, and cursor_move.
979 These record the reverse undo movements onto the stack each time they are
982 Each key press results in a set of actions (insert; delete ...). So each time
983 a key is pressed the current position of start_display is pushed as
984 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
985 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
986 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
990 void edit_push_action (WEdit
* edit
, long c
,...)
992 unsigned long sp
= edit
->stack_pointer
;
996 /* first enlarge the stack if necessary */
997 if (sp
> edit
->stack_size
- 10) { /* say */
998 if (option_max_undo
< 256)
999 option_max_undo
= 256;
1000 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1001 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1003 edit
->undo_stack
= t
;
1004 edit
->stack_size
<<= 1;
1005 edit
->stack_size_mask
= edit
->stack_size
- 1;
1009 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1010 if (edit
->stack_disable
)
1013 #ifdef FAST_MOVE_CURSOR
1014 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1016 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1017 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1019 c
= -(va_arg (ap
, int));
1022 #endif /* ! FAST_MOVE_CURSOR */
1023 if (edit
->stack_bottom
!= sp
1024 && spm1
!= edit
->stack_bottom
1025 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1027 if (edit
->undo_stack
[spm1
] < 0) {
1028 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1030 if (edit
->undo_stack
[spm1
] > -1000000000) {
1031 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1032 edit
->undo_stack
[spm1
]--;
1036 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1037 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1038 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1039 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1040 if (edit
->undo_stack
[spm1
] == -2)
1041 edit
->stack_pointer
= spm1
;
1043 edit
->undo_stack
[spm1
]++;
1048 d
= edit
->undo_stack
[spm1
];
1051 return; /* --> no need to push multiple do-nothings */
1052 edit
->undo_stack
[sp
] = -2;
1055 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1056 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1057 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1058 edit
->stack_pointer
= spm1
;
1064 edit
->undo_stack
[sp
] = c
;
1067 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1069 /* if the sp wraps round and catches the stack_bottom then erase
1070 * the first set of actions on the stack to make space - by moving
1071 * stack_bottom forward one "key press" */
1072 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1073 if ((unsigned long) c
== edit
->stack_bottom
||
1074 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1076 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1077 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1079 /*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: */
1080 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1081 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1085 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1086 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1089 pop_action (WEdit
* edit
)
1092 unsigned long sp
= edit
->stack_pointer
;
1093 if (sp
== edit
->stack_bottom
) {
1094 return STACK_BOTTOM
;
1096 sp
= (sp
- 1) & edit
->stack_size_mask
;
1097 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1098 /* edit->undo_stack[sp] = '@'; */
1099 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1102 if (sp
== edit
->stack_bottom
) {
1103 return STACK_BOTTOM
;
1105 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1106 if (edit
->undo_stack
[sp
] == -2) {
1107 /* edit->undo_stack[sp] = '@'; */
1108 edit
->stack_pointer
= sp
;
1110 edit
->undo_stack
[sp
]++;
1115 /* is called whenever a modification is made by one of the four routines below */
1116 static void edit_modification (WEdit
* edit
)
1118 edit
->caches_valid
= 0;
1119 edit
->screen_modified
= 1;
1121 /* raise lock when file modified */
1122 if (!edit
->modified
&& !edit
->delete_file
)
1123 edit
->locked
= edit_lock_file (edit
->filename
);
1128 Basic low level single character buffer alterations and movements at the cursor.
1129 Returns char passed over, inserted or removed.
1133 edit_insert (WEdit
*edit
, int c
)
1135 /* check if file has grown to large */
1136 if (edit
->last_byte
>= SIZE_LIMIT
)
1139 /* first we must update the position of the display window */
1140 if (edit
->curs1
< edit
->start_display
) {
1141 edit
->start_display
++;
1146 /* Mark file as modified, unless the file hasn't been fully loaded */
1147 if (edit
->loading_done
) {
1148 edit_modification (edit
);
1151 /* now we must update some info on the file and check if a redraw is required */
1153 if (edit
->book_mark
)
1154 book_mark_inc (edit
, edit
->curs_line
);
1156 edit
->total_lines
++;
1157 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1160 /* save the reverse command onto the undo stack */
1161 edit_push_action (edit
, BACKSPACE
);
1163 /* update markers */
1164 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1165 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1166 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1168 /* add a new buffer if we've reached the end of the last one */
1169 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1170 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1171 g_malloc (EDIT_BUF_SIZE
);
1173 /* perform the insertion */
1174 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1175 curs1
& M_EDIT_BUF_SIZE
]
1176 = (unsigned char) c
;
1178 /* update file length */
1181 /* update cursor position */
1186 edit_insert_over (WEdit
* edit
)
1190 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1191 edit_insert (edit
, ' ');
1196 /* same as edit_insert and move left */
1197 void edit_insert_ahead (WEdit
* edit
, int c
)
1199 if (edit
->last_byte
>= SIZE_LIMIT
)
1201 if (edit
->curs1
< edit
->start_display
) {
1202 edit
->start_display
++;
1206 edit_modification (edit
);
1208 if (edit
->book_mark
)
1209 book_mark_inc (edit
, edit
->curs_line
);
1210 edit
->total_lines
++;
1211 edit
->force
|= REDRAW_AFTER_CURSOR
;
1213 edit_push_action (edit
, DELCHAR
);
1215 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1216 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1217 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1219 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1220 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1221 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1228 int edit_delete (WEdit
* edit
, const int byte_delete
)
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
);
1242 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1243 if ( edit
->utf8
&& byte_delete
== 0 ) {
1244 edit_get_utf (edit
, edit
->curs1
, &cw
);
1248 for ( i
= 1; i
<= cw
; i
++ ) {
1249 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1251 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1252 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1253 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1257 edit_push_action (edit
, p
+ 256);
1260 edit_modification (edit
);
1262 if (edit
->book_mark
)
1263 book_mark_dec (edit
, edit
->curs_line
);
1264 edit
->total_lines
--;
1265 edit
->force
|= REDRAW_AFTER_CURSOR
;
1267 if (edit
->curs1
< edit
->start_display
) {
1268 edit
->start_display
--;
1278 edit_backspace (WEdit
* edit
, const int byte_delete
)
1287 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1288 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1289 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1292 if ( edit
->utf8
&& byte_delete
== 0 ) {
1293 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1297 for ( i
= 1; i
<= cw
; i
++ ) {
1298 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1299 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1300 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1301 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1305 edit_push_action (edit
, p
);
1307 edit_modification (edit
);
1309 if (edit
->book_mark
)
1310 book_mark_dec (edit
, edit
->curs_line
);
1312 edit
->total_lines
--;
1313 edit
->force
|= REDRAW_AFTER_CURSOR
;
1316 if (edit
->curs1
< edit
->start_display
) {
1317 edit
->start_display
--;
1325 #ifdef FAST_MOVE_CURSOR
1327 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1330 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1332 next
-= (unsigned long) dest
;
1340 edit_move_backward_lots (WEdit
*edit
, long increment
)
1345 if (increment
> edit
->curs1
)
1346 increment
= edit
->curs1
;
1349 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1351 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1354 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1359 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1360 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1365 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1366 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1367 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1368 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1371 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1372 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1373 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1378 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1380 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1382 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1383 g_malloc (EDIT_BUF_SIZE
);
1388 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1398 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1400 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1404 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1405 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1408 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1410 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1411 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1416 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1418 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1420 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1421 g_malloc (EDIT_BUF_SIZE
);
1426 return edit_get_byte (edit
, edit
->curs1
);
1429 #endif /* ! FAST_MOVE_CURSOR */
1431 /* moves the cursor right or left: increment positive or negative respectively */
1432 void edit_cursor_move (WEdit
* edit
, long increment
)
1434 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1437 #ifdef FAST_MOVE_CURSOR
1438 if (increment
< -256) {
1439 edit
->force
|= REDRAW_PAGE
;
1440 edit_move_backward_lots (edit
, -increment
);
1443 #endif /* ! FAST_MOVE_CURSOR */
1445 if (increment
< 0) {
1446 for (; increment
< 0; increment
++) {
1450 edit_push_action (edit
, CURS_RIGHT
);
1452 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1453 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1454 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1455 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1457 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1458 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1459 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1460 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1465 edit
->force
|= REDRAW_LINE_BELOW
;
1469 } else if (increment
> 0) {
1470 for (; increment
> 0; increment
--) {
1474 edit_push_action (edit
, CURS_LEFT
);
1476 c
= edit_get_byte (edit
, edit
->curs1
);
1477 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1478 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1479 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1481 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1482 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1483 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1484 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1489 edit
->force
|= REDRAW_LINE_ABOVE
;
1495 /* These functions return positions relative to lines */
1497 /* returns index of last char on line + 1 */
1498 long edit_eol (WEdit
* edit
, long current
)
1500 if (current
< edit
->last_byte
) {
1502 if (edit_get_byte (edit
, current
) == '\n')
1505 return edit
->last_byte
;
1509 /* returns index of first char on line */
1510 long edit_bol (WEdit
* edit
, long current
)
1514 if (edit_get_byte (edit
, current
- 1) == '\n')
1522 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1525 if (upto
> edit
->last_byte
)
1526 upto
= edit
->last_byte
;
1529 while (current
< upto
)
1530 if (edit_get_byte (edit
, current
++) == '\n')
1536 /* If lines is zero this returns the count of lines from current to upto. */
1537 /* If upto is zero returns index of lines forward current. */
1538 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1541 return edit_count_lines (edit
, current
, upto
);
1547 next
= edit_eol (edit
, current
) + 1;
1548 if (next
> edit
->last_byte
)
1558 /* Returns offset of 'lines' lines up from current */
1559 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1563 current
= edit_bol (edit
, current
);
1564 while((lines
--) && current
!= 0)
1565 current
= edit_bol (edit
, current
- 1);
1569 /* If cols is zero this returns the count of columns from current to upto. */
1570 /* If upto is zero returns index of cols across from current. */
1571 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1583 q
= edit
->last_byte
+ 2;
1584 for (col
= 0, p
= current
; p
< q
; p
++) {
1597 if ( !edit
->utf8
) {
1599 c
= edit_get_byte (edit
, p
);
1603 c
= edit_get_byte (edit
, p
);
1604 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1606 if ( utf8_display
) {
1607 if ( edit
->utf8
&& g_unichar_iswide(utf_ch
) )
1612 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1613 else if (c
== '\n') {
1618 } else if (c
< 32 || c
== 127)
1619 col
+= 2; /* Caret notation for control characters */
1630 /* returns the current column position of the cursor */
1631 int edit_get_col (WEdit
* edit
)
1633 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1637 /* Scrolling functions */
1639 void edit_update_curs_row (WEdit
* edit
)
1641 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1644 void edit_update_curs_col (WEdit
* edit
)
1646 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1650 edit_get_curs_col (const WEdit
*edit
)
1652 return edit
->curs_col
;
1655 /*moves the display start position up by i lines */
1656 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1658 unsigned long lines_above
= edit
->start_line
;
1659 if (i
> lines_above
)
1662 edit
->start_line
-= i
;
1663 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1664 edit
->force
|= REDRAW_PAGE
;
1665 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1667 edit_update_curs_row (edit
);
1671 /* returns 1 if could scroll, 0 otherwise */
1672 void edit_scroll_downward (WEdit
* edit
, int i
)
1675 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1676 if (lines_below
> 0) {
1677 if (i
> lines_below
)
1679 edit
->start_line
+= i
;
1680 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1681 edit
->force
|= REDRAW_PAGE
;
1682 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1684 edit_update_curs_row (edit
);
1687 void edit_scroll_right (WEdit
* edit
, int i
)
1689 edit
->force
|= REDRAW_PAGE
;
1690 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1691 edit
->start_col
-= i
;
1694 void edit_scroll_left (WEdit
* edit
, int i
)
1696 if (edit
->start_col
) {
1697 edit
->start_col
+= i
;
1698 if (edit
->start_col
> 0)
1699 edit
->start_col
= 0;
1700 edit
->force
|= REDRAW_PAGE
;
1701 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1705 /* high level cursor movement commands */
1707 static int is_in_indent (WEdit
*edit
)
1709 long p
= edit_bol (edit
, edit
->curs1
);
1710 while (p
< edit
->curs1
)
1711 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1716 static int left_of_four_spaces (WEdit
*edit
);
1719 edit_move_to_prev_col (WEdit
* edit
, long p
)
1721 int prev
= edit
->prev_col
;
1722 int over
= edit
->over_col
;
1724 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1726 if (option_cursor_beyond_eol
) {
1727 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1729 if (line_len
< prev
+ edit
->over_col
) {
1730 edit
->over_col
= prev
+ over
- line_len
;
1731 edit
->prev_col
= line_len
;
1732 edit
->curs_col
= line_len
;
1734 edit
->curs_col
= prev
+ over
;
1735 edit
->prev_col
= edit
->curs_col
;
1740 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1741 edit_update_curs_col (edit
);
1743 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1744 int q
= edit
->curs_col
;
1745 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1746 p
= edit_bol (edit
, edit
->curs1
);
1747 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1748 if (!left_of_four_spaces (edit
))
1749 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1756 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1758 unsigned long p
, l
= edit
->curs_line
;
1764 edit
->force
|= REDRAW_PAGE
;
1766 edit_scroll_upward (edit
, i
);
1768 p
= edit_bol (edit
, edit
->curs1
);
1769 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1770 edit_move_to_prev_col (edit
, p
);
1772 edit
->search_start
= edit
->curs1
;
1773 edit
->found_len
= 0;
1778 is_blank (WEdit
*edit
, long offset
)
1782 s
= edit_bol (edit
, offset
);
1783 f
= edit_eol (edit
, offset
) - 1;
1785 c
= edit_get_byte (edit
, s
++);
1793 /* returns the offset of line i */
1795 edit_find_line (WEdit
*edit
, int line
)
1799 if (!edit
->caches_valid
) {
1800 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1801 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1802 /* three offsets that we *know* are line 0 at 0 and these two: */
1803 edit
->line_numbers
[1] = edit
->curs_line
;
1804 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1805 edit
->line_numbers
[2] = edit
->total_lines
;
1806 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1807 edit
->caches_valid
= 1;
1809 if (line
>= edit
->total_lines
)
1810 return edit
->line_offsets
[2];
1813 /* find the closest known point */
1814 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1816 n
= abs (edit
->line_numbers
[i
] - line
);
1823 return edit
->line_offsets
[j
]; /* know the offset exactly */
1824 if (m
== 1 && j
>= 3)
1825 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1827 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1828 if (line
> edit
->line_numbers
[j
])
1829 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1831 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1832 edit
->line_numbers
[i
] = line
;
1833 return edit
->line_offsets
[i
];
1836 int line_is_blank (WEdit
* edit
, long line
)
1838 return is_blank (edit
, edit_find_line (edit
, line
));
1841 /* moves up until a blank line is reached, or until just
1842 before a non-blank line is reached */
1843 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1846 if (edit
->curs_line
<= 1) {
1849 if (line_is_blank (edit
, edit
->curs_line
)) {
1850 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1851 for (i
= edit
->curs_line
- 1; i
; i
--)
1852 if (!line_is_blank (edit
, i
)) {
1857 for (i
= edit
->curs_line
- 1; i
; i
--)
1858 if (line_is_blank (edit
, i
))
1862 for (i
= edit
->curs_line
- 1; i
; i
--)
1863 if (line_is_blank (edit
, i
))
1867 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1871 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1873 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1879 edit
->force
|= REDRAW_PAGE
;
1881 edit_scroll_downward (edit
, i
);
1882 p
= edit_bol (edit
, edit
->curs1
);
1883 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1884 edit_move_to_prev_col (edit
, p
);
1886 edit
->search_start
= edit
->curs1
;
1887 edit
->found_len
= 0;
1891 /* moves down until a blank line is reached, or until just
1892 before a non-blank line is reached */
1893 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1896 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1897 i
= edit
->total_lines
;
1899 if (line_is_blank (edit
, edit
->curs_line
)) {
1900 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1901 for (i
= edit
->curs_line
+ 1; i
; i
++)
1902 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1907 for (i
= edit
->curs_line
+ 1; i
; i
++)
1908 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1912 for (i
= edit
->curs_line
+ 1; i
; i
++)
1913 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1917 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1920 static void edit_begin_page (WEdit
*edit
)
1922 edit_update_curs_row (edit
);
1923 edit_move_up (edit
, edit
->curs_row
, 0);
1926 static void edit_end_page (WEdit
*edit
)
1928 edit_update_curs_row (edit
);
1929 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1933 /* goto beginning of text */
1934 static void edit_move_to_top (WEdit
* edit
)
1936 if (edit
->curs_line
) {
1937 edit_cursor_move (edit
, -edit
->curs1
);
1938 edit_move_to_prev_col (edit
, 0);
1939 edit
->force
|= REDRAW_PAGE
;
1940 edit
->search_start
= 0;
1941 edit_update_curs_row(edit
);
1946 /* goto end of text */
1947 static void edit_move_to_bottom (WEdit
* edit
)
1949 if (edit
->curs_line
< edit
->total_lines
) {
1950 edit_cursor_move (edit
, edit
->curs2
);
1951 edit
->start_display
= edit
->last_byte
;
1952 edit
->start_line
= edit
->total_lines
;
1953 edit_update_curs_row(edit
);
1954 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1955 edit
->force
|= REDRAW_PAGE
;
1959 /* goto beginning of line */
1960 static void edit_cursor_to_bol (WEdit
* edit
)
1962 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1963 edit
->search_start
= edit
->curs1
;
1964 edit
->prev_col
= edit_get_col (edit
);
1968 /* goto end of line */
1969 static void edit_cursor_to_eol (WEdit
* edit
)
1971 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1972 edit
->search_start
= edit
->curs1
;
1973 edit
->prev_col
= edit_get_col (edit
);
1977 /* move cursor to line 'line' */
1978 void edit_move_to_line (WEdit
* e
, long line
)
1980 if(line
< e
->curs_line
)
1981 edit_move_up (e
, e
->curs_line
- line
, 0);
1983 edit_move_down (e
, line
- e
->curs_line
, 0);
1984 edit_scroll_screen_over_cursor (e
);
1987 /* scroll window so that first visible line is 'line' */
1988 void edit_move_display (WEdit
* e
, long line
)
1990 if(line
< e
->start_line
)
1991 edit_scroll_upward (e
, e
->start_line
- line
);
1993 edit_scroll_downward (e
, line
- e
->start_line
);
1996 /* save markers onto undo stack */
1997 void edit_push_markers (WEdit
* edit
)
1999 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2000 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2003 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2012 /* highlight marker toggle */
2013 void edit_mark_cmd (WEdit
* edit
, int unmark
)
2015 edit_push_markers (edit
);
2017 edit_set_markers (edit
, 0, 0, 0, 0);
2018 edit
->force
|= REDRAW_PAGE
;
2020 if (edit
->mark2
>= 0) {
2021 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
2022 edit
->force
|= REDRAW_PAGE
;
2024 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
2028 static unsigned long
2033 const char option_chars_move_whole_word
[] =
2034 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2039 if (*option_chars_move_whole_word
== '!')
2041 return 0x80000000UL
;
2043 if (g_ascii_isupper ((gchar
) c
))
2045 else if (g_ascii_islower ((gchar
) c
))
2047 else if (g_ascii_isalpha (c
))
2049 else if (isdigit (c
))
2051 else if (isspace (c
))
2053 q
= strchr (option_chars_move_whole_word
, c
);
2055 return 0xFFFFFFFFUL
;
2057 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2061 } while ((q
= strchr (q
+ 1, c
)));
2066 edit_left_word_move (WEdit
*edit
, int s
)
2070 edit_cursor_move (edit
, -1);
2073 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2074 c2
= edit_get_byte (edit
, edit
->curs1
);
2075 if (!(my_type_of (c1
) & my_type_of (c2
)))
2077 if (isspace (c1
) && !isspace (c2
))
2080 if (!isspace (c1
) && isspace (c2
))
2085 static void edit_left_word_move_cmd (WEdit
* edit
)
2087 edit_left_word_move (edit
, 0);
2088 edit
->force
|= REDRAW_PAGE
;
2092 edit_right_word_move (WEdit
*edit
, int s
)
2096 edit_cursor_move (edit
, 1);
2097 if (edit
->curs1
>= edit
->last_byte
)
2099 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2100 c2
= edit_get_byte (edit
, edit
->curs1
);
2101 if (!(my_type_of (c1
) & my_type_of (c2
)))
2103 if (isspace (c1
) && !isspace (c2
))
2106 if (!isspace (c1
) && isspace (c2
))
2111 static void edit_right_word_move_cmd (WEdit
* edit
)
2113 edit_right_word_move (edit
, 0);
2114 edit
->force
|= REDRAW_PAGE
;
2117 static void edit_right_char_move_cmd (WEdit
* edit
)
2122 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2126 c
= edit_get_byte (edit
, edit
->curs1
);
2128 if (option_cursor_beyond_eol
&& c
== '\n') {
2131 edit_cursor_move (edit
, cw
);
2135 static void edit_left_char_move_cmd (WEdit
* edit
)
2139 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2143 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2146 edit_cursor_move (edit
, -cw
);
2151 static void edit_right_delete_word (WEdit
* edit
)
2155 if (edit
->curs1
>= edit
->last_byte
)
2157 c1
= edit_delete (edit
, 1);
2158 c2
= edit_get_byte (edit
, edit
->curs1
);
2159 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2161 if (!(my_type_of (c1
) & my_type_of (c2
)))
2166 static void edit_left_delete_word (WEdit
* edit
)
2170 if (edit
->curs1
<= 0)
2172 c1
= edit_backspace (edit
, 1);
2173 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2174 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2176 if (!(my_type_of (c1
) & my_type_of (c2
)))
2182 the start column position is not recorded, and hence does not
2183 undo as it happed. But who would notice.
2186 edit_do_undo (WEdit
* edit
)
2191 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2193 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2198 edit_cursor_move (edit
, 1);
2201 edit_cursor_move (edit
, -1);
2204 edit_backspace (edit
, 1);
2207 edit_delete (edit
, 1);
2210 column_highlighting
= 1;
2213 column_highlighting
= 0;
2216 if (ac
>= 256 && ac
< 512)
2217 edit_insert_ahead (edit
, ac
- 256);
2218 if (ac
>= 0 && ac
< 256)
2219 edit_insert (edit
, ac
);
2221 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2222 edit
->mark1
= ac
- MARK_1
;
2223 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2224 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2225 edit
->mark2
= ac
- MARK_2
;
2226 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2229 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2232 if (edit
->start_display
> ac
- KEY_PRESS
) {
2233 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2234 edit
->force
|= REDRAW_PAGE
;
2235 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2236 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2237 edit
->force
|= REDRAW_PAGE
;
2239 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2240 edit_update_curs_row (edit
);
2243 edit
->stack_disable
= 0;
2246 static void edit_delete_to_line_end (WEdit
* edit
)
2248 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2251 edit_delete (edit
, 1);
2255 static void edit_delete_to_line_begin (WEdit
* edit
)
2257 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2260 edit_backspace (edit
, 1);
2265 edit_delete_line (WEdit
*edit
)
2268 * Delete right part of the line.
2269 * Note that edit_get_byte() returns '\n' when byte position is
2272 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2273 (void) edit_delete (edit
, 1);
2278 * Note that edit_delete() will not corrupt anything if called while
2279 * cursor position is EOF.
2281 (void) edit_delete (edit
, 1);
2284 * Delete left part of the line.
2285 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2287 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2288 (void) edit_backspace (edit
, 1);
2292 void insert_spaces_tab (WEdit
* edit
, int half
)
2295 edit_update_curs_col (edit
);
2296 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2298 edit_insert (edit
, ' ');
2303 static int is_aligned_on_a_tab (WEdit
* edit
)
2305 edit_update_curs_col (edit
);
2306 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
2307 return 0; /* not alligned on a tab */
2311 static int right_of_four_spaces (WEdit
*edit
)
2314 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2315 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2317 return is_aligned_on_a_tab (edit
);
2321 static int left_of_four_spaces (WEdit
*edit
)
2324 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2325 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2327 return is_aligned_on_a_tab (edit
);
2331 int edit_indent_width (WEdit
* edit
, long p
)
2334 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2336 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2339 void edit_insert_indent (WEdit
* edit
, int indent
)
2341 if (!option_fill_tabs_with_spaces
) {
2342 while (indent
>= TAB_SIZE
) {
2343 edit_insert (edit
, '\t');
2347 while (indent
-- > 0)
2348 edit_insert (edit
, ' ');
2352 edit_auto_indent (WEdit
* edit
)
2357 /* use the previous line as a template */
2358 p
= edit_move_backward (edit
, p
, 1);
2359 /* copy the leading whitespace of the line */
2360 for (;;) { /* no range check - the line _is_ \n-terminated */
2361 c
= edit_get_byte (edit
, p
++);
2362 if (c
!= ' ' && c
!= '\t')
2364 edit_insert (edit
, c
);
2368 static void edit_double_newline (WEdit
* edit
)
2370 edit_insert (edit
, '\n');
2371 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2373 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2375 edit
->force
|= REDRAW_PAGE
;
2376 edit_insert (edit
, '\n');
2379 static void edit_tab_cmd (WEdit
* edit
)
2383 if (option_fake_half_tabs
) {
2384 if (is_in_indent (edit
)) {
2385 /*insert a half tab (usually four spaces) unless there is a
2386 half tab already behind, then delete it and insert a
2388 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2389 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2390 edit_backspace (edit
, 1);
2391 edit_insert (edit
, '\t');
2393 insert_spaces_tab (edit
, 1);
2398 if (option_fill_tabs_with_spaces
) {
2399 insert_spaces_tab (edit
, 0);
2401 edit_insert (edit
, '\t');
2406 static void check_and_wrap_line (WEdit
* edit
)
2409 if (!option_typewriter_wrap
)
2411 edit_update_curs_col (edit
);
2412 if (edit
->curs_col
< option_word_wrap_line_length
)
2417 c
= edit_get_byte (edit
, curs
);
2418 if (c
== '\n' || curs
<= 0) {
2419 edit_insert (edit
, '\n');
2422 if (c
== ' ' || c
== '\t') {
2423 int current
= edit
->curs1
;
2424 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2425 edit_insert (edit
, '\n');
2426 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2432 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2434 void edit_push_key_press (WEdit
* edit
)
2436 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2437 if (edit
->mark2
== -1)
2438 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2441 /* this find the matching bracket in either direction, and sets edit->bracket */
2442 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2444 const char * const b
= "{}{[][()(", *p
;
2445 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2446 unsigned long j
= 0;
2448 edit_update_curs_row (edit
);
2449 c
= edit_get_byte (edit
, edit
->curs1
);
2452 if (!furthest_bracket_search
)
2453 furthest_bracket_search
--;
2454 /* not on a bracket at all */
2457 /* the matching bracket */
2459 /* going left or right? */
2460 if (strchr ("{[(", c
))
2462 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2463 /* out of buffer? */
2464 if (q
>= edit
->last_byte
|| q
< 0)
2466 a
= edit_get_byte (edit
, q
);
2467 /* don't want to eat CPU */
2468 if (j
++ > furthest_bracket_search
)
2470 /* out of screen? */
2472 if (q
< edit
->start_display
)
2474 /* count lines if searching downward */
2475 if (inc
> 0 && a
== '\n')
2476 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2479 /* count bracket depth */
2480 i
+= (a
== c
) - (a
== d
);
2481 /* return if bracket depth is zero */
2489 static long last_bracket
= -1;
2491 void edit_find_bracket (WEdit
* edit
)
2493 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2494 if (last_bracket
!= edit
->bracket
)
2495 edit
->force
|= REDRAW_PAGE
;
2496 last_bracket
= edit
->bracket
;
2499 static void edit_goto_matching_bracket (WEdit
*edit
)
2502 q
= edit_get_bracket (edit
, 0, 0);
2505 edit
->bracket
= edit
->curs1
;
2506 edit
->force
|= REDRAW_PAGE
;
2507 edit_cursor_move (edit
, q
- edit
->curs1
);
2511 * This executes a command as though the user initiated it through a key
2512 * press. Callback with WIDGET_KEY as a message calls this after
2513 * translating the key press. This function can be used to pass any
2514 * command to the editor. Note that the screen wouldn't update
2515 * automatically. Either of command or char_for_insertion must be
2516 * passed as -1. Commands are executed, and char_for_insertion is
2517 * inserted at the cursor.
2519 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2521 if (command
== CK_Begin_Record_Macro
) {
2523 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2526 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2527 edit
->force
|= REDRAW_COMPLETELY
;
2528 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2532 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2533 edit
->macro
[edit
->macro_i
].command
= command
;
2534 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2536 /* record the beginning of a set of editing actions initiated by a key press */
2537 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2538 edit_push_key_press (edit
);
2540 edit_execute_cmd (edit
, command
, char_for_insertion
);
2541 if (column_highlighting
)
2542 edit
->force
|= REDRAW_PAGE
;
2545 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2548 This executes a command at a lower level than macro recording.
2549 It also does not push a key_press onto the undo stack. This means
2550 that if it is called many times, a single undo command will undo
2551 all of them. It also does not check for the Undo command.
2554 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2556 edit
->force
|= REDRAW_LINE
;
2558 /* The next key press will unhighlight the found string, so update
2560 if (edit
->found_len
|| column_highlighting
)
2561 edit
->force
|= REDRAW_PAGE
;
2563 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2564 column_highlighting
= 0;
2565 if (!edit
->highlight
2566 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2567 edit_mark_cmd (edit
, 1); /* clear */
2568 edit_mark_cmd (edit
, 0); /* marking on */
2570 edit
->highlight
= 1;
2571 } else { /* any other command */
2572 if (edit
->highlight
)
2573 edit_mark_cmd (edit
, 0); /* clear */
2574 edit
->highlight
= 0;
2577 /* first check for undo */
2578 if (command
== CK_Undo
) {
2579 edit_do_undo (edit
);
2580 edit
->found_len
= 0;
2581 edit
->prev_col
= edit_get_col (edit
);
2582 edit
->search_start
= edit
->curs1
;
2586 /* An ordinary key press */
2587 if (char_for_insertion
>= 0) {
2588 if (edit
->overwrite
) {
2589 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2590 edit_delete (edit
, 0);
2592 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2593 edit_insert_over (edit
);
2595 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2596 unsigned char str
[6 + 1];
2598 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2605 while ( str
[i
] != 0 && i
<=6) {
2606 char_for_insertion
= str
[i
];
2607 edit_insert (edit
, char_for_insertion
);
2612 edit_insert (edit
, char_for_insertion
);
2616 if (option_auto_para_formatting
) {
2617 format_paragraph (edit
, 0);
2618 edit
->force
|= REDRAW_PAGE
;
2620 check_and_wrap_line (edit
);
2621 edit
->found_len
= 0;
2622 edit
->prev_col
= edit_get_col (edit
);
2623 edit
->search_start
= edit
->curs1
;
2624 edit_find_bracket (edit
);
2631 case CK_Begin_Page_Highlight
:
2632 case CK_End_Page_Highlight
:
2639 if ( edit
->mark2
>= 0 ) {
2640 if ( !option_persistent_selections
) {
2641 if (column_highlighting
)
2642 edit_push_action (edit
, COLUMN_ON
);
2643 column_highlighting
= 0;
2644 edit_mark_cmd (edit
, 1);
2652 case CK_Begin_Page_Highlight
:
2653 case CK_End_Page_Highlight
:
2658 case CK_Word_Left_Highlight
:
2659 case CK_Word_Right_Highlight
:
2660 case CK_Up_Highlight
:
2661 case CK_Down_Highlight
:
2662 case CK_Up_Alt_Highlight
:
2663 case CK_Down_Alt_Highlight
:
2664 if (edit
->mark2
== -1)
2665 break; /*marking is following the cursor: may need to highlight a whole line */
2668 case CK_Left_Highlight
:
2669 case CK_Right_Highlight
:
2670 edit
->force
|= REDRAW_CHAR_ONLY
;
2673 /* basic cursor key commands */
2676 /* if non persistent selection and text selected */
2677 if ( !option_persistent_selections
) {
2678 if ( edit
->mark1
!= edit
->mark2
) {
2679 edit_block_delete_cmd (edit
);
2683 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2687 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2688 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2690 edit_backspace (edit
, 1);
2693 if (option_fake_half_tabs
) {
2695 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2696 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2697 edit_backspace (edit
, 1);
2702 edit_backspace (edit
, 0);
2705 /* if non persistent selection and text selected */
2706 if ( !option_persistent_selections
) {
2707 if ( edit
->mark1
!= edit
->mark2
) {
2708 edit_block_delete_cmd (edit
);
2713 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2714 edit_insert_over (edit
);
2716 if (option_fake_half_tabs
) {
2718 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2719 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2720 edit_delete (edit
, 1);
2724 edit_delete (edit
, 0);
2726 case CK_Delete_Word_Left
:
2728 edit_left_delete_word (edit
);
2730 case CK_Delete_Word_Right
:
2731 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2732 edit_insert_over (edit
);
2734 edit_right_delete_word (edit
);
2736 case CK_Delete_Line
:
2737 edit_delete_line (edit
);
2739 case CK_Delete_To_Line_End
:
2740 edit_delete_to_line_end (edit
);
2742 case CK_Delete_To_Line_Begin
:
2743 edit_delete_to_line_begin (edit
);
2747 if (option_auto_para_formatting
) {
2748 edit_double_newline (edit
);
2749 if (option_return_does_auto_indent
)
2750 edit_auto_indent (edit
);
2751 format_paragraph (edit
, 0);
2753 edit_insert (edit
, '\n');
2754 if (option_return_does_auto_indent
) {
2755 edit_auto_indent (edit
);
2760 edit_insert (edit
, '\n');
2763 case CK_Page_Up_Alt_Highlight
:
2764 column_highlighting
= 1;
2766 case CK_Page_Up_Highlight
:
2767 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2769 case CK_Page_Down_Alt_Highlight
:
2770 column_highlighting
= 1;
2772 case CK_Page_Down_Highlight
:
2773 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2775 case CK_Left_Alt_Highlight
:
2776 column_highlighting
= 1;
2778 case CK_Left_Highlight
:
2779 if (option_fake_half_tabs
) {
2780 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2781 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2784 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2785 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2789 edit_left_char_move_cmd (edit
);
2791 case CK_Right_Alt_Highlight
:
2792 column_highlighting
= 1;
2794 case CK_Right_Highlight
:
2795 if (option_fake_half_tabs
) {
2796 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2797 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2798 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2802 edit_right_char_move_cmd (edit
);
2805 case CK_Begin_Page_Highlight
:
2806 edit_begin_page (edit
);
2809 case CK_End_Page_Highlight
:
2810 edit_end_page (edit
);
2813 case CK_Word_Left_Highlight
:
2815 edit_left_word_move_cmd (edit
);
2818 case CK_Word_Right_Highlight
:
2820 edit_right_word_move_cmd (edit
);
2822 case CK_Up_Alt_Highlight
:
2823 column_highlighting
= 1;
2825 case CK_Up_Highlight
:
2826 edit_move_up (edit
, 1, 0);
2828 case CK_Down_Alt_Highlight
:
2829 column_highlighting
= 1;
2831 case CK_Down_Highlight
:
2832 edit_move_down (edit
, 1, 0);
2834 case CK_Paragraph_Up_Alt_Highlight
:
2835 column_highlighting
= 1;
2836 case CK_Paragraph_Up
:
2837 case CK_Paragraph_Up_Highlight
:
2838 edit_move_up_paragraph (edit
, 0);
2840 case CK_Paragraph_Down_Alt_Highlight
:
2841 column_highlighting
= 1;
2842 case CK_Paragraph_Down
:
2843 case CK_Paragraph_Down_Highlight
:
2844 edit_move_down_paragraph (edit
, 0);
2846 case CK_Scroll_Up_Alt_Highlight
:
2847 column_highlighting
= 1;
2849 case CK_Scroll_Up_Highlight
:
2850 edit_move_up (edit
, 1, 1);
2852 case CK_Scroll_Down_Alt_Highlight
:
2853 column_highlighting
= 1;
2854 case CK_Scroll_Down
:
2855 case CK_Scroll_Down_Highlight
:
2856 edit_move_down (edit
, 1, 1);
2859 case CK_Home_Highlight
:
2860 edit_cursor_to_bol (edit
);
2863 case CK_End_Highlight
:
2864 edit_cursor_to_eol (edit
);
2867 /* if text marked shift block */
2868 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2869 edit_move_block_to_right (edit
);
2871 if ( option_cursor_beyond_eol
)
2872 edit_insert_over (edit
);
2873 edit_tab_cmd (edit
);
2874 if (option_auto_para_formatting
) {
2875 format_paragraph (edit
, 0);
2876 edit
->force
|= REDRAW_PAGE
;
2878 check_and_wrap_line (edit
);
2883 case CK_Toggle_Insert
:
2884 edit
->overwrite
= (edit
->overwrite
== 0);
2888 if (edit
->mark2
>= 0) {
2889 if (column_highlighting
)
2890 edit_push_action (edit
, COLUMN_ON
);
2891 column_highlighting
= 0;
2893 edit_mark_cmd (edit
, 0);
2895 case CK_Column_Mark
:
2896 if (!column_highlighting
)
2897 edit_push_action (edit
, COLUMN_OFF
);
2898 column_highlighting
= 1;
2899 edit_mark_cmd (edit
, 0);
2902 if (column_highlighting
)
2903 edit_push_action (edit
, COLUMN_ON
);
2904 column_highlighting
= 0;
2905 edit_mark_cmd (edit
, 1);
2908 case CK_Toggle_Line_State
:
2909 option_line_state
= !option_line_state
;
2910 if ( option_line_state
) {
2911 option_line_state_width
= LINE_STATE_WIDTH
;
2913 option_line_state_width
= 0;
2915 edit
->force
|= REDRAW_PAGE
;
2918 case CK_Toggle_Bookmark
:
2919 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2920 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2921 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2923 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2925 case CK_Flush_Bookmarks
:
2926 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2927 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2928 edit
->force
|= REDRAW_PAGE
;
2930 case CK_Next_Bookmark
:
2931 if (edit
->book_mark
) {
2932 struct _book_mark
*p
;
2933 p
= (struct _book_mark
*) book_mark_find (edit
,
2937 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2938 || p
->line
< edit
->start_line
)
2939 edit_move_display (edit
,
2941 edit
->num_widget_lines
/ 2);
2942 edit_move_to_line (edit
, p
->line
);
2946 case CK_Prev_Bookmark
:
2947 if (edit
->book_mark
) {
2948 struct _book_mark
*p
;
2949 p
= (struct _book_mark
*) book_mark_find (edit
,
2951 while (p
->line
== edit
->curs_line
)
2955 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2956 || p
->line
< edit
->start_line
)
2957 edit_move_display (edit
,
2959 edit
->num_widget_lines
/ 2);
2960 edit_move_to_line (edit
, p
->line
);
2965 case CK_Beginning_Of_Text
:
2966 case CK_Beginning_Of_Text_Highlight
:
2967 edit_move_to_top (edit
);
2969 case CK_End_Of_Text
:
2970 case CK_End_Of_Text_Highlight
:
2971 edit_move_to_bottom (edit
);
2975 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2976 edit_insert_over (edit
);
2977 edit_block_copy_cmd (edit
);
2980 edit_block_delete_cmd (edit
);
2983 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2984 edit_insert_over (edit
);
2985 edit_block_move_cmd (edit
);
2989 edit_copy_to_X_buf_cmd (edit
);
2992 edit_cut_to_X_buf_cmd (edit
);
2995 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2996 edit_insert_over (edit
);
2997 edit_paste_from_X_buf_cmd (edit
);
2999 case CK_Selection_History
:
3000 edit_paste_from_history (edit
);
3004 edit_save_as_cmd (edit
);
3007 edit_save_confirm_cmd (edit
);
3010 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3013 edit_save_block_cmd (edit
);
3015 case CK_Insert_File
:
3016 edit_insert_file_cmd (edit
);
3019 case CK_Load_Prev_File
:
3020 edit_load_back_cmd (edit
);
3022 case CK_Load_Next_File
:
3023 edit_load_forward_cmd (edit
);
3026 case CK_Load_Syntax_File
:
3027 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3029 case CK_Load_Menu_File
:
3030 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3033 case CK_Toggle_Syntax
:
3034 if ((option_syntax_highlighting
^= 1) == 1)
3035 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3036 edit
->force
|= REDRAW_PAGE
;
3039 case CK_Toggle_Tab_TWS
:
3040 enable_show_tabs_tws
^= 1;
3041 edit
->force
|= REDRAW_PAGE
;
3045 edit_search_cmd (edit
, 0);
3048 edit_search_cmd (edit
, 1);
3051 edit_replace_cmd (edit
, 0);
3053 case CK_Replace_Again
:
3054 edit_replace_cmd (edit
, 1);
3056 case CK_Complete_Word
:
3057 /* if text marked shift block */
3058 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3059 edit_move_block_to_left (edit
);
3061 edit_complete_word_cmd (edit
);
3064 case CK_Find_Definition
:
3065 edit_get_match_keyword_cmd (edit
);
3069 dlg_stop (edit
->widget
.parent
);
3072 edit_new_cmd (edit
);
3076 edit_help_cmd (edit
);
3080 edit_refresh_cmd (edit
);
3085 /* fool gcc to prevent a Y2K warning */
3086 char time_format
[] = "_c";
3087 time_format
[0] = '%';
3089 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3090 edit_print_string (edit
, s
);
3091 edit
->force
|= REDRAW_PAGE
;
3095 edit_goto_cmd (edit
);
3097 case CK_Paragraph_Format
:
3098 format_paragraph (edit
, 1);
3099 edit
->force
|= REDRAW_PAGE
;
3101 case CK_Delete_Macro
:
3102 edit_delete_macro_cmd (edit
);
3104 case CK_Match_Bracket
:
3105 edit_goto_matching_bracket (edit
);
3111 edit_sort_cmd (edit
);
3114 edit_ext_cmd (edit
);
3117 edit_mail_dialog (edit
);
3122 case CK_Select_Codepage
:
3123 edit_select_codepage_cmd (edit
);
3125 case CK_Insert_Literal
:
3126 edit_insert_literal_cmd (edit
);
3128 case CK_Execute_Macro
:
3129 edit_execute_macro_cmd (edit
);
3131 case CK_Begin_End_Macro
:
3132 edit_begin_end_macro_cmd (edit
);
3142 if ((command
/ 1000) == 1) /* a shell command */
3143 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3144 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3145 struct macro m
[MAX_MACRO_LENGTH
];
3147 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3148 edit_execute_macro (edit
, m
, nm
);
3151 /* keys which must set the col position, and the search vars */
3156 case CK_Replace_Again
:
3157 case CK_Complete_Word
:
3158 edit
->prev_col
= edit_get_col (edit
);
3161 case CK_Up_Highlight
:
3162 case CK_Up_Alt_Highlight
:
3164 case CK_Down_Highlight
:
3165 case CK_Down_Alt_Highlight
:
3167 case CK_Page_Up_Highlight
:
3168 case CK_Page_Up_Alt_Highlight
:
3170 case CK_Page_Down_Highlight
:
3171 case CK_Page_Down_Alt_Highlight
:
3172 case CK_Beginning_Of_Text
:
3173 case CK_Beginning_Of_Text_Highlight
:
3174 case CK_End_Of_Text
:
3175 case CK_End_Of_Text_Highlight
:
3176 case CK_Paragraph_Up
:
3177 case CK_Paragraph_Up_Highlight
:
3178 case CK_Paragraph_Up_Alt_Highlight
:
3179 case CK_Paragraph_Down
:
3180 case CK_Paragraph_Down_Highlight
:
3181 case CK_Paragraph_Down_Alt_Highlight
:
3183 case CK_Scroll_Up_Highlight
:
3184 case CK_Scroll_Up_Alt_Highlight
:
3185 case CK_Scroll_Down
:
3186 case CK_Scroll_Down_Highlight
:
3187 case CK_Scroll_Down_Alt_Highlight
:
3188 edit
->search_start
= edit
->curs1
;
3189 edit
->found_len
= 0;
3192 edit
->found_len
= 0;
3193 edit
->prev_col
= edit_get_col (edit
);
3194 edit
->search_start
= edit
->curs1
;
3196 edit_find_bracket (edit
);
3198 if (option_auto_para_formatting
) {
3202 case CK_Delete_Word_Left
:
3203 case CK_Delete_Word_Right
:
3204 case CK_Delete_To_Line_End
:
3205 case CK_Delete_To_Line_Begin
:
3206 format_paragraph (edit
, 0);
3207 edit
->force
|= REDRAW_PAGE
;
3214 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3218 if (edit
->macro_depth
++ > 256) {
3219 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3220 edit
->macro_depth
--;
3223 edit
->force
|= REDRAW_PAGE
;
3224 for (; i
< n
; i
++) {
3225 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3227 edit_update_screen (edit
);
3228 edit
->macro_depth
--;
3231 /* User edit menu, like user menu (F2) but only in editor. */
3233 user_menu (WEdit
* edit
)
3238 long start_mark
, end_mark
;
3239 char *block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3242 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3243 if (!nomark
) /* remember marked or not */
3244 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3246 /* run shell scripts from menu */
3247 user_menu_cmd (edit
);
3249 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
3250 /* no block messages */
3255 /* i.e. we have marked block */
3256 rc
= edit_block_delete_cmd (edit
);
3260 edit_insert_file (edit
, block_file
);
3263 /* truncate block file */
3264 if ((fd
= fopen (block_file
, "w"))) {
3268 edit_refresh_cmd (edit
);
3269 edit
->force
|= REDRAW_COMPLETELY
;
3272 g_free (block_file
);
3276 edit_stack_init (void)
3278 for (edit_stack_iterator
= 0;
3279 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3280 edit_stack_iterator
++ ) {
3281 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3282 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3285 edit_stack_iterator
= 0;
3289 edit_stack_free (void)
3291 for (edit_stack_iterator
= 0;
3292 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3293 edit_stack_iterator
++)
3294 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);