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_fill_tabs_with_spaces
= 0;
72 int option_return_does_auto_indent
= 1;
73 int option_backspace_through_tabs
= 0;
74 int option_fake_half_tabs
= 1;
75 int option_save_mode
= EDIT_QUICK_SAVE
;
76 int option_save_position
= 1;
77 int option_max_undo
= 32768;
78 int option_persistent_selections
= 1;
79 int option_cursor_beyond_eol
= 1;
80 int option_line_state
= 0;
81 int option_line_state_width
= 0;
83 int option_edit_right_extreme
= 0;
84 int option_edit_left_extreme
= 0;
85 int option_edit_top_extreme
= 0;
86 int option_edit_bottom_extreme
= 0;
87 int enable_show_tabs_tws
= 1;
89 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext
= NULL
;
92 int edit_stack_iterator
= 0;
93 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC
[] = {'\1', '\1', '\1', '\1', '\n'};
98 * here's a quick sketch of the layout: (don't run this through indent.)
100 * (b1 is buffers1 and b2 is buffers2)
103 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
104 * ______________________________________|______________________________________
106 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
107 * |-> |-> |-> |-> |-> |-> |
109 * _<------------------------->|<----------------->_
110 * WEdit->curs2 | WEdit->curs1
115 * file end|||file beginning
125 static void user_menu (WEdit
*edit
);
127 int edit_get_byte (WEdit
* edit
, long byte_index
)
130 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
133 if (byte_index
>= edit
->curs1
) {
134 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
135 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
137 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
141 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
145 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
148 if (byte_index
>= edit
->curs1
) {
149 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
150 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
]+(EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
152 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
]+(byte_index
& M_EDIT_BUF_SIZE
));
156 char *edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
160 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) ) {
164 if ( byte_index
< 0 ) {
168 if (byte_index
>= edit
->curs1
) {
169 p
= edit
->curs1
+ edit
->curs2
- 1;
170 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
172 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
176 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
181 gchar
*next_ch
= NULL
;
184 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
190 str
= edit_get_byte_ptr (edit
, byte_index
);
191 res
= g_utf8_get_char_validated (str
, -1);
198 /* Calculate UTF-8 char width */
199 next_ch
= g_utf8_next_char(str
);
201 width
= next_ch
- str
;
211 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
213 gchar
*str
, *buf
= NULL
;
216 gchar
*next_ch
= NULL
;
219 if ( byte_index
> 0 ) {
223 ch
= edit_get_utf (edit
, byte_index
, &width
);
229 if ( byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0 ) {
234 str
= edit_get_byte_ptr (edit
, byte_index
);
235 buf
= edit_get_buf_ptr (edit
, byte_index
);
236 /* get prev utf8 char */
238 str
= g_utf8_find_prev_char (buf
, str
);
240 res
= g_utf8_get_char_validated (str
, -1);
246 /* Calculate UTF-8 char width */
247 next_ch
= g_utf8_next_char(str
);
249 width
= next_ch
- str
;
260 * Initialize the buffers for an empty files.
263 edit_init_buffers (WEdit
*edit
)
267 for (j
= 0; j
<= MAXBUFF
; j
++) {
268 edit
->buffers1
[j
] = NULL
;
269 edit
->buffers2
[j
] = NULL
;
274 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
278 * Load file OR text into buffers. Set cursor to the beginning of file.
282 edit_load_file_fast (WEdit
*edit
, const char *filename
)
286 edit
->curs2
= edit
->last_byte
;
287 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
289 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
290 GString
*errmsg
= g_string_new(NULL
);
291 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
292 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
293 g_string_free (errmsg
, TRUE
);
297 if (!edit
->buffers2
[buf2
])
298 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
301 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
302 (edit
->curs2
& M_EDIT_BUF_SIZE
),
303 edit
->curs2
& M_EDIT_BUF_SIZE
);
305 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
306 /* edit->buffers2[0] is already allocated */
307 if (!edit
->buffers2
[buf
])
308 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
309 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
316 /* detecting an error on save is easy: just check if every byte has been written. */
317 /* detecting an error on read, is not so easy 'cos there is not way to tell
318 whether you read everything or not. */
319 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
320 static const struct edit_filters
{
321 const char *read
, *write
, *extension
;
323 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
324 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
325 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
326 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
327 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
330 /* Return index of the filter or -1 is there is no appropriate filter */
331 static int edit_find_filter (const char *filename
)
336 l
= strlen (filename
);
337 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
338 e
= strlen (all_filters
[i
].extension
);
340 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
347 edit_get_filter (const char *filename
)
350 char *p
, *quoted_name
;
351 i
= edit_find_filter (filename
);
354 quoted_name
= name_quote (filename
, 0);
355 l
= str_term_width1 (quoted_name
);
356 p
= g_malloc (str_term_width1 (all_filters
[i
].read
) + l
+ 2);
357 sprintf (p
, all_filters
[i
].read
, quoted_name
);
358 g_free (quoted_name
);
363 edit_get_write_filter (const char *write_name
, const char *filename
)
367 i
= edit_find_filter (filename
);
370 writename
= name_quote (write_name
, 0);
371 l
= str_term_width1 (writename
);
372 p
= g_malloc (str_term_width1 (all_filters
[i
].write
) + l
+ 2);
373 sprintf (p
, all_filters
[i
].write
, writename
);
379 edit_insert_stream (WEdit
* edit
, FILE * f
)
383 while ((c
= fgetc (f
)) >= 0) {
384 edit_insert (edit
, c
);
390 long edit_write_stream (WEdit
* edit
, FILE * f
)
394 if (edit
->lb
== LB_ASIS
) {
395 for (i
= 0; i
< edit
->last_byte
; i
++)
396 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
401 /* change line breaks */
402 for (i
= 0; i
< edit
->last_byte
; i
++) {
403 unsigned char c
= edit_get_byte (edit
, i
);
405 if (!(c
== '\n' || c
== '\r')) {
407 if (fputc (c
, f
) < 0)
409 } else { /* (c == '\n' || c == '\r') */
410 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
413 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
414 /* put one line break unconditionally */
415 if (fputc ('\n', f
) < 0)
418 i
++; /* 2 chars are processed */
420 if (c
== '\r' && c1
== '\n')
421 /* Windows line break; go to the next char */
424 if (c
== '\r' && c1
== '\r') {
425 /* two Macintosh line breaks; put second line break */
426 if (fputc ('\n', f
) < 0)
431 if (fputc (c1
, f
) < 0)
435 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
436 /* put one line break unconditionally */
437 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
440 if (c
== '\r' && c1
== '\n')
441 /* Windows line break; go to the next char */
445 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
446 /* put one line break unconditionally */
447 if (fputc ('\r', f
) < 0)
450 i
++; /* 2 chars are processed */
452 if (c
== '\r' && c1
== '\n')
453 /* Windows line break; go to the next char */
456 if (c
== '\n' && c1
== '\n') {
457 /* two Windows line breaks; put second line break */
458 if (fputc ('\r', f
) < 0)
463 if (fputc (c1
, f
) < 0)
466 case LB_ASIS
: /* default without changes */
472 return edit
->last_byte
;
475 #define TEMP_BUF_LEN 1024
477 /* inserts a file at the cursor, returns 1 on success */
479 edit_insert_file (WEdit
*edit
, const char *filename
)
482 if ((p
= edit_get_filter (filename
))) {
484 long current
= edit
->curs1
;
485 f
= (FILE *) popen (p
, "r");
487 edit_insert_stream (edit
, f
);
488 edit_cursor_move (edit
, current
- edit
->curs1
);
489 if (pclose (f
) > 0) {
490 GString
*errmsg
= g_string_new (NULL
);
491 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
492 edit_error_dialog (_("Error"), errmsg
->str
);
493 g_string_free (errmsg
, TRUE
);
498 GString
*errmsg
= g_string_new (NULL
);
499 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
500 edit_error_dialog (_("Error"), errmsg
->str
);
501 g_string_free (errmsg
, TRUE
);
507 int i
, file
, blocklen
;
508 long current
= edit
->curs1
;
509 int vertical_insertion
= 0;
511 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
513 buf
= g_malloc (TEMP_BUF_LEN
);
514 blocklen
= mc_read (file
, buf
, sizeof(VERTICAL_MAGIC
));
516 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
517 if ( memcmp(buf
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
)) == 0 ) {
518 vertical_insertion
= 1;
520 mc_lseek (file
, 0, SEEK_SET
);
523 if (vertical_insertion
) {
524 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
526 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
527 for (i
= 0; i
< blocklen
; i
++)
528 edit_insert (edit
, buf
[i
]);
531 edit_cursor_move (edit
, current
- edit
->curs1
);
540 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
542 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
545 GString
*errmsg
= (GString
*) 0;
547 /* Try opening an existing file */
548 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
552 * Try creating the file. O_EXCL prevents following broken links
553 * and opening existing files.
557 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
560 g_string_sprintf (errmsg
= g_string_new (NULL
),
561 _(" Cannot open %s for reading "), filename
);
564 /* New file, delete it if it's not modified or saved */
565 edit
->delete_file
= 1;
569 /* Check what we have opened */
570 if (mc_fstat (file
, st
) < 0) {
571 g_string_sprintf (errmsg
= g_string_new (NULL
),
572 _(" Cannot get size/permissions for %s "), filename
);
576 /* We want to open regular files only */
577 if (!S_ISREG (st
->st_mode
)) {
578 g_string_sprintf (errmsg
= g_string_new (NULL
),
579 _(" %s is not a regular file "), filename
);
584 * Don't delete non-empty files.
585 * O_EXCL should prevent it, but let's be on the safe side.
587 if (st
->st_size
> 0) {
588 edit
->delete_file
= 0;
591 if (st
->st_size
>= SIZE_LIMIT
) {
592 g_string_sprintf (errmsg
= g_string_new (NULL
),
593 _(" File %s is too large "), filename
);
598 (void) mc_close (file
);
600 edit_error_dialog (_("Error"), errmsg
->str
);
601 g_string_free (errmsg
, TRUE
);
608 * Open the file and load it into the buffers, either directly or using
609 * a filter. Return 0 on success, 1 on error.
611 * Fast loading (edit_load_file_fast) is used when the file size is
612 * known. In this case the data is read into the buffers by blocks.
613 * If the file size is not known, the data is loaded byte by byte in
617 edit_load_file (WEdit
*edit
)
621 /* Cannot do fast load if a filter is used */
622 if (edit_find_filter (edit
->filename
) >= 0)
626 * VFS may report file size incorrectly, and slow load is not a big
627 * deal considering overhead in VFS.
629 if (!vfs_file_is_local (edit
->filename
))
633 * FIXME: line end translation should disable fast loading as well
634 * Consider doing fseek() to the end and ftell() for the real size.
637 if (*edit
->filename
) {
638 /* If we are dealing with a real file, check that it exists */
639 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
642 /* nothing to load */
646 edit_init_buffers (edit
);
649 edit
->last_byte
= edit
->stat1
.st_size
;
650 edit_load_file_fast (edit
, edit
->filename
);
651 /* If fast load was used, the number of lines wasn't calculated */
652 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
655 const char *codepage_id
;
658 if (*edit
->filename
) {
659 edit
->stack_disable
= 1;
660 if (!edit_insert_file (edit
, edit
->filename
)) {
664 edit
->stack_disable
= 0;
668 codepage_id
= get_codepage_id( source_codepage
);
670 edit
->utf8
= str_isutf8 ( codepage_id
);
677 /* Restore saved cursor position in the file */
679 edit_load_position (WEdit
*edit
)
684 if (!edit
->filename
|| !*edit
->filename
)
687 filename
= vfs_canon (edit
->filename
);
688 load_file_position (filename
, &line
, &column
);
691 edit_move_to_line (edit
, line
- 1);
692 edit
->prev_col
= column
;
693 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
694 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
697 /* Save cursor position in the file */
699 edit_save_position (WEdit
*edit
)
703 if (!edit
->filename
|| !*edit
->filename
)
706 filename
= vfs_canon (edit
->filename
);
707 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
711 /* Clean the WEdit stricture except the widget part */
713 edit_purge_widget (WEdit
*edit
)
715 int len
= sizeof (WEdit
) - sizeof (Widget
);
716 char *start
= (char *) edit
+ sizeof (Widget
);
717 memset (start
, 0, len
);
718 edit
->macro_i
= -1; /* not recording a macro */
722 edit_set_keymap (WEdit
*edit
)
724 edit
->user_map
= default_editor_keymap
;
725 if (editor_keymap
&& editor_keymap
->len
> 0)
726 edit
->user_map
= (global_key_map_t
*) editor_keymap
->data
;
730 #define space_width 1
733 * Fill in the edit structure. Return NULL on failure. Pass edit as
734 * NULL to allocate a new structure.
736 * If line is 0, try to restore saved position. Otherwise put the
737 * cursor on that line and show it in the middle of the screen.
740 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
744 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
745 if ( option_line_state
) {
746 option_line_state_width
= LINE_STATE_WIDTH
;
748 option_line_state_width
= 0;
753 * Expand option_whole_chars_search by national letters using
757 static char option_whole_chars_search_buf
[256];
759 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
761 size_t len
= str_term_width1 (option_whole_chars_search
);
763 strcpy (option_whole_chars_search_buf
,
764 option_whole_chars_search
);
766 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
767 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
768 option_whole_chars_search_buf
[len
++] = i
;
772 option_whole_chars_search_buf
[len
] = 0;
773 option_whole_chars_search
= option_whole_chars_search_buf
;
775 #endif /* ENABLE_NLS */
776 edit
= g_malloc0 (sizeof (WEdit
));
780 edit_purge_widget (edit
);
781 edit
->num_widget_lines
= lines
;
783 edit
->num_widget_columns
= columns
;
784 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
785 edit
->stat1
.st_uid
= getuid ();
786 edit
->stat1
.st_gid
= getgid ();
787 edit
->stat1
.st_mtime
= 0;
789 edit
->force
|= REDRAW_PAGE
;
790 edit_set_filename (edit
, filename
);
791 edit
->stack_size
= START_STACK_SIZE
;
792 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
793 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
794 if (edit_load_file (edit
)) {
795 /* edit_load_file already gives an error message */
801 edit
->converter
= str_cnv_from_term
;
803 const char *cp_id
= NULL
;
804 cp_id
= get_codepage_id (source_codepage
>= 0 ?
805 source_codepage
: display_codepage
);
809 conv
= str_crt_conv_from (cp_id
);
810 if (conv
!= INVALID_CONV
) {
811 if (edit
->converter
!= str_cnv_from_term
)
812 str_close_conv (edit
->converter
);
813 edit
->converter
= conv
;
817 edit
->utf8
= str_isutf8 (cp_id
);
820 edit
->loading_done
= 1;
823 edit_load_syntax (edit
, 0, 0);
826 edit_get_syntax_color (edit
, -1, &color
);
829 /* load saved cursor position */
830 if ((line
== 0) && option_save_position
) {
831 edit_load_position (edit
);
835 edit_move_display (edit
, line
- 1);
836 edit_move_to_line (edit
, line
- 1);
839 edit_set_keymap (edit
);
844 /* Clear the edit struct, freeing everything in it. Return 1 on success */
846 edit_clean (WEdit
*edit
)
853 /* a stale lock, remove it */
855 edit
->locked
= edit_unlock_file (edit
->filename
);
857 /* save cursor position */
858 if (option_save_position
)
859 edit_save_position (edit
);
861 /* File specified on the mcedit command line and never saved */
862 if (edit
->delete_file
)
863 unlink (edit
->filename
);
865 edit_free_syntax_rules (edit
);
866 book_mark_flush (edit
, -1);
867 for (; j
<= MAXBUFF
; j
++) {
868 g_free (edit
->buffers1
[j
]);
869 g_free (edit
->buffers2
[j
]);
872 g_free (edit
->undo_stack
);
873 g_free (edit
->filename
);
878 mc_search_free(edit
->search
);
881 edit_purge_widget (edit
);
887 /* returns 1 on success */
888 int edit_renew (WEdit
* edit
)
890 int lines
= edit
->num_widget_lines
;
891 int columns
= edit
->num_widget_columns
;
895 if (!edit_init (edit
, lines
, columns
, "", 0))
901 * Load a new file into the editor. If it fails, preserve the old file.
902 * To do it, allocate a new widget, initialize it and, if the new file
903 * was loaded, copy the data to the old widget.
904 * Return 1 on success, 0 on failure.
907 edit_reload (WEdit
*edit
, const char *filename
)
910 int lines
= edit
->num_widget_lines
;
911 int columns
= edit
->num_widget_columns
;
913 e
= g_malloc0 (sizeof (WEdit
));
914 e
->widget
= edit
->widget
;
915 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
920 memcpy (edit
, e
, sizeof (WEdit
));
926 * Load a new file into the editor and set line. If it fails, preserve the old file.
927 * To do it, allocate a new widget, initialize it and, if the new file
928 * was loaded, copy the data to the old widget.
929 * Return 1 on success, 0 on failure.
932 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
935 int lines
= edit
->num_widget_lines
;
936 int columns
= edit
->num_widget_columns
;
938 e
= g_malloc0 (sizeof (WEdit
));
939 e
->widget
= edit
->widget
;
940 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
945 memcpy (edit
, e
, sizeof (WEdit
));
952 Recording stack for undo:
953 The following is an implementation of a compressed stack. Identical
954 pushes are recorded by a negative prefix indicating the number of times the
955 same char was pushed. This saves space for repeated curs-left or curs-right
972 If the stack long int is 0-255 it represents a normal insert (from a backspace),
973 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
974 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
975 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
978 The only way the cursor moves or the buffer is changed is through the routines:
979 insert, backspace, insert_ahead, delete, and cursor_move.
980 These record the reverse undo movements onto the stack each time they are
983 Each key press results in a set of actions (insert; delete ...). So each time
984 a key is pressed the current position of start_display is pushed as
985 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
986 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
987 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
991 void edit_push_action (WEdit
* edit
, long c
,...)
993 unsigned long sp
= edit
->stack_pointer
;
997 /* first enlarge the stack if necessary */
998 if (sp
> edit
->stack_size
- 10) { /* say */
999 if (option_max_undo
< 256)
1000 option_max_undo
= 256;
1001 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1002 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1004 edit
->undo_stack
= t
;
1005 edit
->stack_size
<<= 1;
1006 edit
->stack_size_mask
= edit
->stack_size
- 1;
1010 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1011 if (edit
->stack_disable
)
1014 #ifdef FAST_MOVE_CURSOR
1015 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1017 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1018 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1020 c
= -(va_arg (ap
, int));
1023 #endif /* ! FAST_MOVE_CURSOR */
1024 if (edit
->stack_bottom
!= sp
1025 && spm1
!= edit
->stack_bottom
1026 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1028 if (edit
->undo_stack
[spm1
] < 0) {
1029 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1031 if (edit
->undo_stack
[spm1
] > -1000000000) {
1032 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1033 edit
->undo_stack
[spm1
]--;
1037 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1038 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1039 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1040 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1041 if (edit
->undo_stack
[spm1
] == -2)
1042 edit
->stack_pointer
= spm1
;
1044 edit
->undo_stack
[spm1
]++;
1049 d
= edit
->undo_stack
[spm1
];
1052 return; /* --> no need to push multiple do-nothings */
1053 edit
->undo_stack
[sp
] = -2;
1056 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1057 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1058 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1059 edit
->stack_pointer
= spm1
;
1065 edit
->undo_stack
[sp
] = c
;
1068 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1070 /* if the sp wraps round and catches the stack_bottom then erase
1071 * the first set of actions on the stack to make space - by moving
1072 * stack_bottom forward one "key press" */
1073 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1074 if ((unsigned long) c
== edit
->stack_bottom
||
1075 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1077 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1078 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1080 /*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: */
1081 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1082 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1086 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1087 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1090 pop_action (WEdit
* edit
)
1093 unsigned long sp
= edit
->stack_pointer
;
1094 if (sp
== edit
->stack_bottom
) {
1095 return STACK_BOTTOM
;
1097 sp
= (sp
- 1) & edit
->stack_size_mask
;
1098 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1099 /* edit->undo_stack[sp] = '@'; */
1100 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1103 if (sp
== edit
->stack_bottom
) {
1104 return STACK_BOTTOM
;
1106 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1107 if (edit
->undo_stack
[sp
] == -2) {
1108 /* edit->undo_stack[sp] = '@'; */
1109 edit
->stack_pointer
= sp
;
1111 edit
->undo_stack
[sp
]++;
1116 /* is called whenever a modification is made by one of the four routines below */
1117 static void edit_modification (WEdit
* edit
)
1119 edit
->caches_valid
= 0;
1120 edit
->screen_modified
= 1;
1122 /* raise lock when file modified */
1123 if (!edit
->modified
&& !edit
->delete_file
)
1124 edit
->locked
= edit_lock_file (edit
->filename
);
1129 Basic low level single character buffer alterations and movements at the cursor.
1130 Returns char passed over, inserted or removed.
1134 edit_insert (WEdit
*edit
, int c
)
1136 /* check if file has grown to large */
1137 if (edit
->last_byte
>= SIZE_LIMIT
)
1140 /* first we must update the position of the display window */
1141 if (edit
->curs1
< edit
->start_display
) {
1142 edit
->start_display
++;
1147 /* Mark file as modified, unless the file hasn't been fully loaded */
1148 if (edit
->loading_done
) {
1149 edit_modification (edit
);
1152 /* now we must update some info on the file and check if a redraw is required */
1154 if (edit
->book_mark
)
1155 book_mark_inc (edit
, edit
->curs_line
);
1157 edit
->total_lines
++;
1158 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1161 /* save the reverse command onto the undo stack */
1162 edit_push_action (edit
, BACKSPACE
);
1164 /* update markers */
1165 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1166 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1167 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1169 /* add a new buffer if we've reached the end of the last one */
1170 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1171 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1172 g_malloc (EDIT_BUF_SIZE
);
1174 /* perform the insertion */
1175 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1176 curs1
& M_EDIT_BUF_SIZE
]
1177 = (unsigned char) c
;
1179 /* update file length */
1182 /* update cursor position */
1187 edit_insert_over (WEdit
* edit
)
1191 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1192 edit_insert (edit
, ' ');
1197 /* same as edit_insert and move left */
1198 void edit_insert_ahead (WEdit
* edit
, int c
)
1200 if (edit
->last_byte
>= SIZE_LIMIT
)
1202 if (edit
->curs1
< edit
->start_display
) {
1203 edit
->start_display
++;
1207 edit_modification (edit
);
1209 if (edit
->book_mark
)
1210 book_mark_inc (edit
, edit
->curs_line
);
1211 edit
->total_lines
++;
1212 edit
->force
|= REDRAW_AFTER_CURSOR
;
1214 edit_push_action (edit
, DELCHAR
);
1216 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1217 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1218 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1220 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1221 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1222 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1229 int edit_delete (WEdit
* edit
, const int byte_delete
)
1238 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1239 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1240 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1243 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1244 if ( edit
->utf8
&& byte_delete
== 0 ) {
1245 edit_get_utf (edit
, edit
->curs1
, &cw
);
1249 for ( i
= 1; i
<= cw
; i
++ ) {
1250 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1252 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1253 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1254 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1258 edit_push_action (edit
, p
+ 256);
1261 edit_modification (edit
);
1263 if (edit
->book_mark
)
1264 book_mark_dec (edit
, edit
->curs_line
);
1265 edit
->total_lines
--;
1266 edit
->force
|= REDRAW_AFTER_CURSOR
;
1268 if (edit
->curs1
< edit
->start_display
) {
1269 edit
->start_display
--;
1279 edit_backspace (WEdit
* edit
, const int byte_delete
)
1288 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1289 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1290 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1293 if ( edit
->utf8
&& byte_delete
== 0 ) {
1294 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1298 for ( i
= 1; i
<= cw
; i
++ ) {
1299 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1300 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1301 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1302 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1306 edit_push_action (edit
, p
);
1308 edit_modification (edit
);
1310 if (edit
->book_mark
)
1311 book_mark_dec (edit
, edit
->curs_line
);
1313 edit
->total_lines
--;
1314 edit
->force
|= REDRAW_AFTER_CURSOR
;
1317 if (edit
->curs1
< edit
->start_display
) {
1318 edit
->start_display
--;
1326 #ifdef FAST_MOVE_CURSOR
1328 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1331 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1333 next
-= (unsigned long) dest
;
1341 edit_move_backward_lots (WEdit
*edit
, long increment
)
1346 if (increment
> edit
->curs1
)
1347 increment
= edit
->curs1
;
1350 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1352 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1355 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1360 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1361 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1366 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1367 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1368 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1369 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1372 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1373 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1374 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1379 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1381 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1383 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1384 g_malloc (EDIT_BUF_SIZE
);
1389 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1399 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1401 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1405 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1406 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1409 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1411 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1412 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1417 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1419 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1421 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1422 g_malloc (EDIT_BUF_SIZE
);
1427 return edit_get_byte (edit
, edit
->curs1
);
1430 #endif /* ! FAST_MOVE_CURSOR */
1432 /* moves the cursor right or left: increment positive or negative respectively */
1433 void edit_cursor_move (WEdit
* edit
, long increment
)
1435 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1438 #ifdef FAST_MOVE_CURSOR
1439 if (increment
< -256) {
1440 edit
->force
|= REDRAW_PAGE
;
1441 edit_move_backward_lots (edit
, -increment
);
1444 #endif /* ! FAST_MOVE_CURSOR */
1446 if (increment
< 0) {
1447 for (; increment
< 0; increment
++) {
1451 edit_push_action (edit
, CURS_RIGHT
);
1453 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1454 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1455 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1456 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1458 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1459 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1460 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1461 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1466 edit
->force
|= REDRAW_LINE_BELOW
;
1470 } else if (increment
> 0) {
1471 for (; increment
> 0; increment
--) {
1475 edit_push_action (edit
, CURS_LEFT
);
1477 c
= edit_get_byte (edit
, edit
->curs1
);
1478 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1479 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1480 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1482 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1483 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1484 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1485 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1490 edit
->force
|= REDRAW_LINE_ABOVE
;
1496 /* These functions return positions relative to lines */
1498 /* returns index of last char on line + 1 */
1499 long edit_eol (WEdit
* edit
, long current
)
1501 if (current
< edit
->last_byte
) {
1503 if (edit_get_byte (edit
, current
) == '\n')
1506 return edit
->last_byte
;
1510 /* returns index of first char on line */
1511 long edit_bol (WEdit
* edit
, long current
)
1515 if (edit_get_byte (edit
, current
- 1) == '\n')
1523 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1526 if (upto
> edit
->last_byte
)
1527 upto
= edit
->last_byte
;
1530 while (current
< upto
)
1531 if (edit_get_byte (edit
, current
++) == '\n')
1537 /* If lines is zero this returns the count of lines from current to upto. */
1538 /* If upto is zero returns index of lines forward current. */
1539 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1542 return edit_count_lines (edit
, current
, upto
);
1548 next
= edit_eol (edit
, current
) + 1;
1549 if (next
> edit
->last_byte
)
1559 /* Returns offset of 'lines' lines up from current */
1560 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1564 current
= edit_bol (edit
, current
);
1565 while((lines
--) && current
!= 0)
1566 current
= edit_bol (edit
, current
- 1);
1570 /* If cols is zero this returns the count of columns from current to upto. */
1571 /* If upto is zero returns index of cols across from current. */
1572 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1584 q
= edit
->last_byte
+ 2;
1585 for (col
= 0, p
= current
; p
< q
; p
++) {
1598 if ( !edit
->utf8
) {
1600 c
= edit_get_byte (edit
, p
);
1604 c
= edit_get_byte (edit
, p
);
1605 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1607 if ( utf8_display
) {
1608 if ( edit
->utf8
&& g_unichar_iswide(utf_ch
) )
1613 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1614 else if (c
== '\n') {
1619 } else if (c
< 32 || c
== 127)
1620 col
+= 2; /* Caret notation for control characters */
1631 /* returns the current column position of the cursor */
1632 int edit_get_col (WEdit
* edit
)
1634 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1638 /* Scrolling functions */
1640 void edit_update_curs_row (WEdit
* edit
)
1642 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1645 void edit_update_curs_col (WEdit
* edit
)
1647 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1651 edit_get_curs_col (const WEdit
*edit
)
1653 return edit
->curs_col
;
1656 /*moves the display start position up by i lines */
1657 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1659 unsigned long lines_above
= edit
->start_line
;
1660 if (i
> lines_above
)
1663 edit
->start_line
-= i
;
1664 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1665 edit
->force
|= REDRAW_PAGE
;
1666 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1668 edit_update_curs_row (edit
);
1672 /* returns 1 if could scroll, 0 otherwise */
1673 void edit_scroll_downward (WEdit
* edit
, int i
)
1676 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1677 if (lines_below
> 0) {
1678 if (i
> lines_below
)
1680 edit
->start_line
+= i
;
1681 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1682 edit
->force
|= REDRAW_PAGE
;
1683 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1685 edit_update_curs_row (edit
);
1688 void edit_scroll_right (WEdit
* edit
, int i
)
1690 edit
->force
|= REDRAW_PAGE
;
1691 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1692 edit
->start_col
-= i
;
1695 void edit_scroll_left (WEdit
* edit
, int i
)
1697 if (edit
->start_col
) {
1698 edit
->start_col
+= i
;
1699 if (edit
->start_col
> 0)
1700 edit
->start_col
= 0;
1701 edit
->force
|= REDRAW_PAGE
;
1702 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1706 /* high level cursor movement commands */
1708 static int is_in_indent (WEdit
*edit
)
1710 long p
= edit_bol (edit
, edit
->curs1
);
1711 while (p
< edit
->curs1
)
1712 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1717 static int left_of_four_spaces (WEdit
*edit
);
1720 edit_move_to_prev_col (WEdit
* edit
, long p
)
1722 int prev
= edit
->prev_col
;
1723 int over
= edit
->over_col
;
1725 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1727 if (option_cursor_beyond_eol
) {
1728 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1730 if (line_len
< prev
+ edit
->over_col
) {
1731 edit
->over_col
= prev
+ over
- line_len
;
1732 edit
->prev_col
= line_len
;
1733 edit
->curs_col
= line_len
;
1735 edit
->curs_col
= prev
+ over
;
1736 edit
->prev_col
= edit
->curs_col
;
1741 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1742 edit_update_curs_col (edit
);
1744 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1745 int q
= edit
->curs_col
;
1746 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1747 p
= edit_bol (edit
, edit
->curs1
);
1748 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1749 if (!left_of_four_spaces (edit
))
1750 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1757 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1759 unsigned long p
, l
= edit
->curs_line
;
1765 edit
->force
|= REDRAW_PAGE
;
1767 edit_scroll_upward (edit
, i
);
1769 p
= edit_bol (edit
, edit
->curs1
);
1770 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1771 edit_move_to_prev_col (edit
, p
);
1773 edit
->search_start
= edit
->curs1
;
1774 edit
->found_len
= 0;
1779 is_blank (WEdit
*edit
, long offset
)
1783 s
= edit_bol (edit
, offset
);
1784 f
= edit_eol (edit
, offset
) - 1;
1786 c
= edit_get_byte (edit
, s
++);
1794 /* returns the offset of line i */
1796 edit_find_line (WEdit
*edit
, int line
)
1800 if (!edit
->caches_valid
) {
1801 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1802 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1803 /* three offsets that we *know* are line 0 at 0 and these two: */
1804 edit
->line_numbers
[1] = edit
->curs_line
;
1805 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1806 edit
->line_numbers
[2] = edit
->total_lines
;
1807 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1808 edit
->caches_valid
= 1;
1810 if (line
>= edit
->total_lines
)
1811 return edit
->line_offsets
[2];
1814 /* find the closest known point */
1815 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1817 n
= abs (edit
->line_numbers
[i
] - line
);
1824 return edit
->line_offsets
[j
]; /* know the offset exactly */
1825 if (m
== 1 && j
>= 3)
1826 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1828 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1829 if (line
> edit
->line_numbers
[j
])
1830 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1832 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1833 edit
->line_numbers
[i
] = line
;
1834 return edit
->line_offsets
[i
];
1837 int line_is_blank (WEdit
* edit
, long line
)
1839 return is_blank (edit
, edit_find_line (edit
, line
));
1842 /* moves up until a blank line is reached, or until just
1843 before a non-blank line is reached */
1844 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1847 if (edit
->curs_line
<= 1) {
1850 if (line_is_blank (edit
, edit
->curs_line
)) {
1851 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1852 for (i
= edit
->curs_line
- 1; i
; i
--)
1853 if (!line_is_blank (edit
, i
)) {
1858 for (i
= edit
->curs_line
- 1; i
; i
--)
1859 if (line_is_blank (edit
, i
))
1863 for (i
= edit
->curs_line
- 1; i
; i
--)
1864 if (line_is_blank (edit
, i
))
1868 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1872 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1874 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1880 edit
->force
|= REDRAW_PAGE
;
1882 edit_scroll_downward (edit
, i
);
1883 p
= edit_bol (edit
, edit
->curs1
);
1884 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1885 edit_move_to_prev_col (edit
, p
);
1887 edit
->search_start
= edit
->curs1
;
1888 edit
->found_len
= 0;
1892 /* moves down until a blank line is reached, or until just
1893 before a non-blank line is reached */
1894 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1897 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1898 i
= edit
->total_lines
;
1900 if (line_is_blank (edit
, edit
->curs_line
)) {
1901 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1902 for (i
= edit
->curs_line
+ 1; i
; i
++)
1903 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1908 for (i
= edit
->curs_line
+ 1; i
; i
++)
1909 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1913 for (i
= edit
->curs_line
+ 1; i
; i
++)
1914 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1918 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1921 static void edit_begin_page (WEdit
*edit
)
1923 edit_update_curs_row (edit
);
1924 edit_move_up (edit
, edit
->curs_row
, 0);
1927 static void edit_end_page (WEdit
*edit
)
1929 edit_update_curs_row (edit
);
1930 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1934 /* goto beginning of text */
1935 static void edit_move_to_top (WEdit
* edit
)
1937 if (edit
->curs_line
) {
1938 edit_cursor_move (edit
, -edit
->curs1
);
1939 edit_move_to_prev_col (edit
, 0);
1940 edit
->force
|= REDRAW_PAGE
;
1941 edit
->search_start
= 0;
1942 edit_update_curs_row(edit
);
1947 /* goto end of text */
1948 static void edit_move_to_bottom (WEdit
* edit
)
1950 if (edit
->curs_line
< edit
->total_lines
) {
1951 edit_cursor_move (edit
, edit
->curs2
);
1952 edit
->start_display
= edit
->last_byte
;
1953 edit
->start_line
= edit
->total_lines
;
1954 edit_update_curs_row(edit
);
1955 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1956 edit
->force
|= REDRAW_PAGE
;
1960 /* goto beginning of line */
1961 static void edit_cursor_to_bol (WEdit
* edit
)
1963 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1964 edit
->search_start
= edit
->curs1
;
1965 edit
->prev_col
= edit_get_col (edit
);
1969 /* goto end of line */
1970 static void edit_cursor_to_eol (WEdit
* edit
)
1972 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1973 edit
->search_start
= edit
->curs1
;
1974 edit
->prev_col
= edit_get_col (edit
);
1978 /* move cursor to line 'line' */
1979 void edit_move_to_line (WEdit
* e
, long line
)
1981 if(line
< e
->curs_line
)
1982 edit_move_up (e
, e
->curs_line
- line
, 0);
1984 edit_move_down (e
, line
- e
->curs_line
, 0);
1985 edit_scroll_screen_over_cursor (e
);
1988 /* scroll window so that first visible line is 'line' */
1989 void edit_move_display (WEdit
* e
, long line
)
1991 if(line
< e
->start_line
)
1992 edit_scroll_upward (e
, e
->start_line
- line
);
1994 edit_scroll_downward (e
, line
- e
->start_line
);
1997 /* save markers onto undo stack */
1998 void edit_push_markers (WEdit
* edit
)
2000 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2001 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2004 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2013 /* highlight marker toggle */
2014 void edit_mark_cmd (WEdit
* edit
, int unmark
)
2016 edit_push_markers (edit
);
2018 edit_set_markers (edit
, 0, 0, 0, 0);
2019 edit
->force
|= REDRAW_PAGE
;
2021 if (edit
->mark2
>= 0) {
2022 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
2023 edit
->force
|= REDRAW_PAGE
;
2025 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
2029 static unsigned long
2034 const char option_chars_move_whole_word
[] =
2035 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2040 if (*option_chars_move_whole_word
== '!')
2042 return 0x80000000UL
;
2044 if (g_ascii_isupper ((gchar
) c
))
2046 else if (g_ascii_islower ((gchar
) c
))
2048 else if (g_ascii_isalpha (c
))
2050 else if (isdigit (c
))
2052 else if (isspace (c
))
2054 q
= strchr (option_chars_move_whole_word
, c
);
2056 return 0xFFFFFFFFUL
;
2058 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2062 } while ((q
= strchr (q
+ 1, c
)));
2067 edit_left_word_move (WEdit
*edit
, int s
)
2071 edit_cursor_move (edit
, -1);
2074 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2075 c2
= edit_get_byte (edit
, edit
->curs1
);
2076 if (!(my_type_of (c1
) & my_type_of (c2
)))
2078 if (isspace (c1
) && !isspace (c2
))
2081 if (!isspace (c1
) && isspace (c2
))
2086 static void edit_left_word_move_cmd (WEdit
* edit
)
2088 edit_left_word_move (edit
, 0);
2089 edit
->force
|= REDRAW_PAGE
;
2093 edit_right_word_move (WEdit
*edit
, int s
)
2097 edit_cursor_move (edit
, 1);
2098 if (edit
->curs1
>= edit
->last_byte
)
2100 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2101 c2
= edit_get_byte (edit
, edit
->curs1
);
2102 if (!(my_type_of (c1
) & my_type_of (c2
)))
2104 if (isspace (c1
) && !isspace (c2
))
2107 if (!isspace (c1
) && isspace (c2
))
2112 static void edit_right_word_move_cmd (WEdit
* edit
)
2114 edit_right_word_move (edit
, 0);
2115 edit
->force
|= REDRAW_PAGE
;
2118 static void edit_right_char_move_cmd (WEdit
* edit
)
2123 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2127 c
= edit_get_byte (edit
, edit
->curs1
);
2129 if (option_cursor_beyond_eol
&& c
== '\n') {
2132 edit_cursor_move (edit
, cw
);
2136 static void edit_left_char_move_cmd (WEdit
* edit
)
2140 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2144 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2147 edit_cursor_move (edit
, -cw
);
2152 static void edit_right_delete_word (WEdit
* edit
)
2156 if (edit
->curs1
>= edit
->last_byte
)
2158 c1
= edit_delete (edit
, 1);
2159 c2
= edit_get_byte (edit
, edit
->curs1
);
2160 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2162 if (!(my_type_of (c1
) & my_type_of (c2
)))
2167 static void edit_left_delete_word (WEdit
* edit
)
2171 if (edit
->curs1
<= 0)
2173 c1
= edit_backspace (edit
, 1);
2174 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2175 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2177 if (!(my_type_of (c1
) & my_type_of (c2
)))
2183 the start column position is not recorded, and hence does not
2184 undo as it happed. But who would notice.
2187 edit_do_undo (WEdit
* edit
)
2192 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2194 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2199 edit_cursor_move (edit
, 1);
2202 edit_cursor_move (edit
, -1);
2205 edit_backspace (edit
, 1);
2208 edit_delete (edit
, 1);
2211 column_highlighting
= 1;
2214 column_highlighting
= 0;
2217 if (ac
>= 256 && ac
< 512)
2218 edit_insert_ahead (edit
, ac
- 256);
2219 if (ac
>= 0 && ac
< 256)
2220 edit_insert (edit
, ac
);
2222 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2223 edit
->mark1
= ac
- MARK_1
;
2224 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2225 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2226 edit
->mark2
= ac
- MARK_2
;
2227 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2230 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2233 if (edit
->start_display
> ac
- KEY_PRESS
) {
2234 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2235 edit
->force
|= REDRAW_PAGE
;
2236 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2237 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2238 edit
->force
|= REDRAW_PAGE
;
2240 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2241 edit_update_curs_row (edit
);
2244 edit
->stack_disable
= 0;
2247 static void edit_delete_to_line_end (WEdit
* edit
)
2249 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2252 edit_delete (edit
, 1);
2256 static void edit_delete_to_line_begin (WEdit
* edit
)
2258 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2261 edit_backspace (edit
, 1);
2266 edit_delete_line (WEdit
*edit
)
2269 * Delete right part of the line.
2270 * Note that edit_get_byte() returns '\n' when byte position is
2273 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2274 (void) edit_delete (edit
, 1);
2279 * Note that edit_delete() will not corrupt anything if called while
2280 * cursor position is EOF.
2282 (void) edit_delete (edit
, 1);
2285 * Delete left part of the line.
2286 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2288 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2289 (void) edit_backspace (edit
, 1);
2293 void insert_spaces_tab (WEdit
* edit
, int half
)
2296 edit_update_curs_col (edit
);
2297 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2299 edit_insert (edit
, ' ');
2304 static int is_aligned_on_a_tab (WEdit
* edit
)
2306 edit_update_curs_col (edit
);
2307 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
2308 return 0; /* not alligned on a tab */
2312 static int right_of_four_spaces (WEdit
*edit
)
2315 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2316 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2318 return is_aligned_on_a_tab (edit
);
2322 static int left_of_four_spaces (WEdit
*edit
)
2325 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2326 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2328 return is_aligned_on_a_tab (edit
);
2332 int edit_indent_width (WEdit
* edit
, long p
)
2335 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2337 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2340 void edit_insert_indent (WEdit
* edit
, int indent
)
2342 if (!option_fill_tabs_with_spaces
) {
2343 while (indent
>= TAB_SIZE
) {
2344 edit_insert (edit
, '\t');
2348 while (indent
-- > 0)
2349 edit_insert (edit
, ' ');
2353 edit_auto_indent (WEdit
* edit
)
2358 /* use the previous line as a template */
2359 p
= edit_move_backward (edit
, p
, 1);
2360 /* copy the leading whitespace of the line */
2361 for (;;) { /* no range check - the line _is_ \n-terminated */
2362 c
= edit_get_byte (edit
, p
++);
2363 if (c
!= ' ' && c
!= '\t')
2365 edit_insert (edit
, c
);
2369 static void edit_double_newline (WEdit
* edit
)
2371 edit_insert (edit
, '\n');
2372 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2374 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2376 edit
->force
|= REDRAW_PAGE
;
2377 edit_insert (edit
, '\n');
2380 static void edit_tab_cmd (WEdit
* edit
)
2384 if (option_fake_half_tabs
) {
2385 if (is_in_indent (edit
)) {
2386 /*insert a half tab (usually four spaces) unless there is a
2387 half tab already behind, then delete it and insert a
2389 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2390 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2391 edit_backspace (edit
, 1);
2392 edit_insert (edit
, '\t');
2394 insert_spaces_tab (edit
, 1);
2399 if (option_fill_tabs_with_spaces
) {
2400 insert_spaces_tab (edit
, 0);
2402 edit_insert (edit
, '\t');
2407 static void check_and_wrap_line (WEdit
* edit
)
2410 if (!option_typewriter_wrap
)
2412 edit_update_curs_col (edit
);
2413 if (edit
->curs_col
< option_word_wrap_line_length
)
2418 c
= edit_get_byte (edit
, curs
);
2419 if (c
== '\n' || curs
<= 0) {
2420 edit_insert (edit
, '\n');
2423 if (c
== ' ' || c
== '\t') {
2424 int current
= edit
->curs1
;
2425 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2426 edit_insert (edit
, '\n');
2427 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2433 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2435 void edit_push_key_press (WEdit
* edit
)
2437 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2438 if (edit
->mark2
== -1)
2439 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2442 /* this find the matching bracket in either direction, and sets edit->bracket */
2443 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2445 const char * const b
= "{}{[][()(", *p
;
2446 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2447 unsigned long j
= 0;
2449 edit_update_curs_row (edit
);
2450 c
= edit_get_byte (edit
, edit
->curs1
);
2453 if (!furthest_bracket_search
)
2454 furthest_bracket_search
--;
2455 /* not on a bracket at all */
2458 /* the matching bracket */
2460 /* going left or right? */
2461 if (strchr ("{[(", c
))
2463 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2464 /* out of buffer? */
2465 if (q
>= edit
->last_byte
|| q
< 0)
2467 a
= edit_get_byte (edit
, q
);
2468 /* don't want to eat CPU */
2469 if (j
++ > furthest_bracket_search
)
2471 /* out of screen? */
2473 if (q
< edit
->start_display
)
2475 /* count lines if searching downward */
2476 if (inc
> 0 && a
== '\n')
2477 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2480 /* count bracket depth */
2481 i
+= (a
== c
) - (a
== d
);
2482 /* return if bracket depth is zero */
2490 static long last_bracket
= -1;
2492 void edit_find_bracket (WEdit
* edit
)
2494 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2495 if (last_bracket
!= edit
->bracket
)
2496 edit
->force
|= REDRAW_PAGE
;
2497 last_bracket
= edit
->bracket
;
2500 static void edit_goto_matching_bracket (WEdit
*edit
)
2503 q
= edit_get_bracket (edit
, 0, 0);
2506 edit
->bracket
= edit
->curs1
;
2507 edit
->force
|= REDRAW_PAGE
;
2508 edit_cursor_move (edit
, q
- edit
->curs1
);
2512 * This executes a command as though the user initiated it through a key
2513 * press. Callback with WIDGET_KEY as a message calls this after
2514 * translating the key press. This function can be used to pass any
2515 * command to the editor. Note that the screen wouldn't update
2516 * automatically. Either of command or char_for_insertion must be
2517 * passed as -1. Commands are executed, and char_for_insertion is
2518 * inserted at the cursor.
2520 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2522 if (command
== CK_Begin_Record_Macro
) {
2524 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2527 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2528 edit
->force
|= REDRAW_COMPLETELY
;
2529 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2533 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2534 edit
->macro
[edit
->macro_i
].command
= command
;
2535 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2537 /* record the beginning of a set of editing actions initiated by a key press */
2538 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2539 edit_push_key_press (edit
);
2541 edit_execute_cmd (edit
, command
, char_for_insertion
);
2542 if (column_highlighting
)
2543 edit
->force
|= REDRAW_PAGE
;
2546 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2549 This executes a command at a lower level than macro recording.
2550 It also does not push a key_press onto the undo stack. This means
2551 that if it is called many times, a single undo command will undo
2552 all of them. It also does not check for the Undo command.
2555 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2557 edit
->force
|= REDRAW_LINE
;
2559 /* The next key press will unhighlight the found string, so update
2561 if (edit
->found_len
|| column_highlighting
)
2562 edit
->force
|= REDRAW_PAGE
;
2564 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2565 column_highlighting
= 0;
2566 if (!edit
->highlight
2567 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2568 edit_mark_cmd (edit
, 1); /* clear */
2569 edit_mark_cmd (edit
, 0); /* marking on */
2571 edit
->highlight
= 1;
2572 } else { /* any other command */
2573 if (edit
->highlight
)
2574 edit_mark_cmd (edit
, 0); /* clear */
2575 edit
->highlight
= 0;
2578 /* first check for undo */
2579 if (command
== CK_Undo
) {
2580 edit_do_undo (edit
);
2581 edit
->found_len
= 0;
2582 edit
->prev_col
= edit_get_col (edit
);
2583 edit
->search_start
= edit
->curs1
;
2587 /* An ordinary key press */
2588 if (char_for_insertion
>= 0) {
2589 if (edit
->overwrite
) {
2590 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2591 edit_delete (edit
, 0);
2593 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2594 edit_insert_over (edit
);
2596 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2597 unsigned char str
[6 + 1];
2599 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2606 while ( str
[i
] != 0 && i
<=6) {
2607 char_for_insertion
= str
[i
];
2608 edit_insert (edit
, char_for_insertion
);
2613 edit_insert (edit
, char_for_insertion
);
2617 if (option_auto_para_formatting
) {
2618 format_paragraph (edit
, 0);
2619 edit
->force
|= REDRAW_PAGE
;
2621 check_and_wrap_line (edit
);
2622 edit
->found_len
= 0;
2623 edit
->prev_col
= edit_get_col (edit
);
2624 edit
->search_start
= edit
->curs1
;
2625 edit_find_bracket (edit
);
2632 case CK_Begin_Page_Highlight
:
2633 case CK_End_Page_Highlight
:
2640 if ( edit
->mark2
>= 0 ) {
2641 if ( !option_persistent_selections
) {
2642 if (column_highlighting
)
2643 edit_push_action (edit
, COLUMN_ON
);
2644 column_highlighting
= 0;
2645 edit_mark_cmd (edit
, 1);
2653 case CK_Begin_Page_Highlight
:
2654 case CK_End_Page_Highlight
:
2659 case CK_Word_Left_Highlight
:
2660 case CK_Word_Right_Highlight
:
2661 case CK_Up_Highlight
:
2662 case CK_Down_Highlight
:
2663 case CK_Up_Alt_Highlight
:
2664 case CK_Down_Alt_Highlight
:
2665 if (edit
->mark2
== -1)
2666 break; /*marking is following the cursor: may need to highlight a whole line */
2669 case CK_Left_Highlight
:
2670 case CK_Right_Highlight
:
2671 edit
->force
|= REDRAW_CHAR_ONLY
;
2674 /* basic cursor key commands */
2677 /* if non persistent selection and text selected */
2678 if ( !option_persistent_selections
) {
2679 if ( edit
->mark1
!= edit
->mark2
) {
2680 edit_block_delete_cmd (edit
);
2684 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2688 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2689 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2691 edit_backspace (edit
, 1);
2694 if (option_fake_half_tabs
) {
2696 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2697 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2698 edit_backspace (edit
, 1);
2703 edit_backspace (edit
, 0);
2706 /* if non persistent selection and text selected */
2707 if ( !option_persistent_selections
) {
2708 if ( edit
->mark1
!= edit
->mark2
) {
2709 edit_block_delete_cmd (edit
);
2714 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2715 edit_insert_over (edit
);
2717 if (option_fake_half_tabs
) {
2719 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2720 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2721 edit_delete (edit
, 1);
2725 edit_delete (edit
, 0);
2727 case CK_Delete_Word_Left
:
2729 edit_left_delete_word (edit
);
2731 case CK_Delete_Word_Right
:
2732 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2733 edit_insert_over (edit
);
2735 edit_right_delete_word (edit
);
2737 case CK_Delete_Line
:
2738 edit_delete_line (edit
);
2740 case CK_Delete_To_Line_End
:
2741 edit_delete_to_line_end (edit
);
2743 case CK_Delete_To_Line_Begin
:
2744 edit_delete_to_line_begin (edit
);
2748 if (option_auto_para_formatting
) {
2749 edit_double_newline (edit
);
2750 if (option_return_does_auto_indent
)
2751 edit_auto_indent (edit
);
2752 format_paragraph (edit
, 0);
2754 edit_insert (edit
, '\n');
2755 if (option_return_does_auto_indent
) {
2756 edit_auto_indent (edit
);
2761 edit_insert (edit
, '\n');
2764 case CK_Page_Up_Alt_Highlight
:
2765 column_highlighting
= 1;
2767 case CK_Page_Up_Highlight
:
2768 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2770 case CK_Page_Down_Alt_Highlight
:
2771 column_highlighting
= 1;
2773 case CK_Page_Down_Highlight
:
2774 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2776 case CK_Left_Alt_Highlight
:
2777 column_highlighting
= 1;
2779 case CK_Left_Highlight
:
2780 if (option_fake_half_tabs
) {
2781 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2782 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2785 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2786 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2790 edit_left_char_move_cmd (edit
);
2792 case CK_Right_Alt_Highlight
:
2793 column_highlighting
= 1;
2795 case CK_Right_Highlight
:
2796 if (option_fake_half_tabs
) {
2797 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2798 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2799 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2803 edit_right_char_move_cmd (edit
);
2806 case CK_Begin_Page_Highlight
:
2807 edit_begin_page (edit
);
2810 case CK_End_Page_Highlight
:
2811 edit_end_page (edit
);
2814 case CK_Word_Left_Highlight
:
2816 edit_left_word_move_cmd (edit
);
2819 case CK_Word_Right_Highlight
:
2821 edit_right_word_move_cmd (edit
);
2823 case CK_Up_Alt_Highlight
:
2824 column_highlighting
= 1;
2826 case CK_Up_Highlight
:
2827 edit_move_up (edit
, 1, 0);
2829 case CK_Down_Alt_Highlight
:
2830 column_highlighting
= 1;
2832 case CK_Down_Highlight
:
2833 edit_move_down (edit
, 1, 0);
2835 case CK_Paragraph_Up_Alt_Highlight
:
2836 column_highlighting
= 1;
2837 case CK_Paragraph_Up
:
2838 case CK_Paragraph_Up_Highlight
:
2839 edit_move_up_paragraph (edit
, 0);
2841 case CK_Paragraph_Down_Alt_Highlight
:
2842 column_highlighting
= 1;
2843 case CK_Paragraph_Down
:
2844 case CK_Paragraph_Down_Highlight
:
2845 edit_move_down_paragraph (edit
, 0);
2847 case CK_Scroll_Up_Alt_Highlight
:
2848 column_highlighting
= 1;
2850 case CK_Scroll_Up_Highlight
:
2851 edit_move_up (edit
, 1, 1);
2853 case CK_Scroll_Down_Alt_Highlight
:
2854 column_highlighting
= 1;
2855 case CK_Scroll_Down
:
2856 case CK_Scroll_Down_Highlight
:
2857 edit_move_down (edit
, 1, 1);
2860 case CK_Home_Highlight
:
2861 edit_cursor_to_bol (edit
);
2864 case CK_End_Highlight
:
2865 edit_cursor_to_eol (edit
);
2868 /* if text marked shift block */
2869 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2870 if (edit
->mark2
< 0)
2871 edit_mark_cmd (edit
, 0);
2872 edit_move_block_to_right (edit
);
2874 if ( option_cursor_beyond_eol
)
2875 edit_insert_over (edit
);
2876 edit_tab_cmd (edit
);
2877 if (option_auto_para_formatting
) {
2878 format_paragraph (edit
, 0);
2879 edit
->force
|= REDRAW_PAGE
;
2881 check_and_wrap_line (edit
);
2886 case CK_Toggle_Insert
:
2887 edit
->overwrite
= (edit
->overwrite
== 0);
2891 if (edit
->mark2
>= 0) {
2892 if (column_highlighting
)
2893 edit_push_action (edit
, COLUMN_ON
);
2894 column_highlighting
= 0;
2896 edit_mark_cmd (edit
, 0);
2898 case CK_Column_Mark
:
2899 if (!column_highlighting
)
2900 edit_push_action (edit
, COLUMN_OFF
);
2901 column_highlighting
= 1;
2902 edit_mark_cmd (edit
, 0);
2905 if (column_highlighting
)
2906 edit_push_action (edit
, COLUMN_ON
);
2907 column_highlighting
= 0;
2908 edit_mark_cmd (edit
, 1);
2911 case CK_Toggle_Line_State
:
2912 option_line_state
= !option_line_state
;
2913 if ( option_line_state
) {
2914 option_line_state_width
= LINE_STATE_WIDTH
;
2916 option_line_state_width
= 0;
2918 edit
->force
|= REDRAW_PAGE
;
2921 case CK_Toggle_Bookmark
:
2922 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2923 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2924 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2926 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2928 case CK_Flush_Bookmarks
:
2929 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2930 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2931 edit
->force
|= REDRAW_PAGE
;
2933 case CK_Next_Bookmark
:
2934 if (edit
->book_mark
) {
2935 struct _book_mark
*p
;
2936 p
= (struct _book_mark
*) book_mark_find (edit
,
2940 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2941 || p
->line
< edit
->start_line
)
2942 edit_move_display (edit
,
2944 edit
->num_widget_lines
/ 2);
2945 edit_move_to_line (edit
, p
->line
);
2949 case CK_Prev_Bookmark
:
2950 if (edit
->book_mark
) {
2951 struct _book_mark
*p
;
2952 p
= (struct _book_mark
*) book_mark_find (edit
,
2954 while (p
->line
== edit
->curs_line
)
2958 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2959 || p
->line
< edit
->start_line
)
2960 edit_move_display (edit
,
2962 edit
->num_widget_lines
/ 2);
2963 edit_move_to_line (edit
, p
->line
);
2968 case CK_Beginning_Of_Text
:
2969 case CK_Beginning_Of_Text_Highlight
:
2970 edit_move_to_top (edit
);
2972 case CK_End_Of_Text
:
2973 case CK_End_Of_Text_Highlight
:
2974 edit_move_to_bottom (edit
);
2978 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2979 edit_insert_over (edit
);
2980 edit_block_copy_cmd (edit
);
2983 edit_block_delete_cmd (edit
);
2986 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2987 edit_insert_over (edit
);
2988 edit_block_move_cmd (edit
);
2992 edit_copy_to_X_buf_cmd (edit
);
2995 edit_cut_to_X_buf_cmd (edit
);
2998 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2999 edit_insert_over (edit
);
3000 edit_paste_from_X_buf_cmd (edit
);
3002 case CK_Selection_History
:
3003 edit_paste_from_history (edit
);
3007 edit_save_as_cmd (edit
);
3010 edit_save_confirm_cmd (edit
);
3013 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3016 edit_save_block_cmd (edit
);
3018 case CK_Insert_File
:
3019 edit_insert_file_cmd (edit
);
3022 case CK_Load_Prev_File
:
3023 edit_load_back_cmd (edit
);
3025 case CK_Load_Next_File
:
3026 edit_load_forward_cmd (edit
);
3029 case CK_Load_Syntax_File
:
3030 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3032 case CK_Load_Menu_File
:
3033 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3036 case CK_Toggle_Syntax
:
3037 if ((option_syntax_highlighting
^= 1) == 1)
3038 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3039 edit
->force
|= REDRAW_PAGE
;
3042 case CK_Toggle_Tab_TWS
:
3043 enable_show_tabs_tws
^= 1;
3044 edit
->force
|= REDRAW_PAGE
;
3048 edit_search_cmd (edit
, 0);
3051 edit_search_cmd (edit
, 1);
3054 edit_replace_cmd (edit
, 0);
3056 case CK_Replace_Again
:
3057 edit_replace_cmd (edit
, 1);
3059 case CK_Complete_Word
:
3060 /* if text marked shift block */
3061 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3062 edit_move_block_to_left (edit
);
3064 edit_complete_word_cmd (edit
);
3067 case CK_Find_Definition
:
3068 edit_get_match_keyword_cmd (edit
);
3072 dlg_stop (edit
->widget
.parent
);
3075 edit_new_cmd (edit
);
3079 edit_help_cmd (edit
);
3083 edit_refresh_cmd (edit
);
3088 /* fool gcc to prevent a Y2K warning */
3089 char time_format
[] = "_c";
3090 time_format
[0] = '%';
3092 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3093 edit_print_string (edit
, s
);
3094 edit
->force
|= REDRAW_PAGE
;
3098 edit_goto_cmd (edit
);
3100 case CK_Paragraph_Format
:
3101 format_paragraph (edit
, 1);
3102 edit
->force
|= REDRAW_PAGE
;
3104 case CK_Delete_Macro
:
3105 edit_delete_macro_cmd (edit
);
3107 case CK_Match_Bracket
:
3108 edit_goto_matching_bracket (edit
);
3114 edit_sort_cmd (edit
);
3117 edit_ext_cmd (edit
);
3120 edit_mail_dialog (edit
);
3125 case CK_SelectCodepage
:
3126 edit_select_codepage_cmd (edit
);
3128 case CK_Insert_Literal
:
3129 edit_insert_literal_cmd (edit
);
3131 case CK_Execute_Macro
:
3132 edit_execute_macro_cmd (edit
);
3134 case CK_Begin_End_Macro
:
3135 edit_begin_end_macro_cmd (edit
);
3145 if ((command
/ 1000) == 1) /* a shell command */
3146 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3147 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3148 struct macro m
[MAX_MACRO_LENGTH
];
3150 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3151 edit_execute_macro (edit
, m
, nm
);
3154 /* keys which must set the col position, and the search vars */
3159 case CK_Replace_Again
:
3160 case CK_Complete_Word
:
3161 edit
->prev_col
= edit_get_col (edit
);
3164 case CK_Up_Highlight
:
3165 case CK_Up_Alt_Highlight
:
3167 case CK_Down_Highlight
:
3168 case CK_Down_Alt_Highlight
:
3170 case CK_Page_Up_Highlight
:
3171 case CK_Page_Up_Alt_Highlight
:
3173 case CK_Page_Down_Highlight
:
3174 case CK_Page_Down_Alt_Highlight
:
3175 case CK_Beginning_Of_Text
:
3176 case CK_Beginning_Of_Text_Highlight
:
3177 case CK_End_Of_Text
:
3178 case CK_End_Of_Text_Highlight
:
3179 case CK_Paragraph_Up
:
3180 case CK_Paragraph_Up_Highlight
:
3181 case CK_Paragraph_Up_Alt_Highlight
:
3182 case CK_Paragraph_Down
:
3183 case CK_Paragraph_Down_Highlight
:
3184 case CK_Paragraph_Down_Alt_Highlight
:
3186 case CK_Scroll_Up_Highlight
:
3187 case CK_Scroll_Up_Alt_Highlight
:
3188 case CK_Scroll_Down
:
3189 case CK_Scroll_Down_Highlight
:
3190 case CK_Scroll_Down_Alt_Highlight
:
3191 edit
->search_start
= edit
->curs1
;
3192 edit
->found_len
= 0;
3195 edit
->found_len
= 0;
3196 edit
->prev_col
= edit_get_col (edit
);
3197 edit
->search_start
= edit
->curs1
;
3199 edit_find_bracket (edit
);
3201 if (option_auto_para_formatting
) {
3205 case CK_Delete_Word_Left
:
3206 case CK_Delete_Word_Right
:
3207 case CK_Delete_To_Line_End
:
3208 case CK_Delete_To_Line_Begin
:
3209 format_paragraph (edit
, 0);
3210 edit
->force
|= REDRAW_PAGE
;
3217 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3221 if (edit
->macro_depth
++ > 256) {
3222 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3223 edit
->macro_depth
--;
3226 edit
->force
|= REDRAW_PAGE
;
3227 for (; i
< n
; i
++) {
3228 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3230 edit_update_screen (edit
);
3231 edit
->macro_depth
--;
3234 /* User edit menu, like user menu (F2) but only in editor. */
3236 user_menu (WEdit
* edit
)
3241 long start_mark
, end_mark
;
3242 char *block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3245 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3246 if (!nomark
) /* remember marked or not */
3247 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3249 /* run shell scripts from menu */
3250 user_menu_cmd (edit
);
3252 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
3253 /* no block messages */
3258 /* i.e. we have marked block */
3259 rc
= edit_block_delete_cmd (edit
);
3263 edit_insert_file (edit
, block_file
);
3266 /* truncate block file */
3267 if ((fd
= fopen (block_file
, "w"))) {
3271 edit_refresh_cmd (edit
);
3272 edit
->force
|= REDRAW_COMPLETELY
;
3275 g_free (block_file
);
3279 edit_stack_init (void)
3281 for (edit_stack_iterator
= 0;
3282 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3283 edit_stack_iterator
++ ) {
3284 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3285 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3288 edit_stack_iterator
= 0;
3292 edit_stack_free (void)
3294 for (edit_stack_iterator
= 0;
3295 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3296 edit_stack_iterator
++)
3297 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);