1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * \brief Source: editor low level data handling and cursor fundamentals
33 #include <sys/types.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "src/charsets.h" /* get_codepage_id */
57 #include "src/main.h" /* source_codepage */
58 #include "src/learn.h" /* learn_keys */
59 #include "src/cmddef.h"
61 #include "edit-impl.h"
63 #include "edit-widget.h"
66 int option_word_wrap_line_length
= 72;
67 int option_typewriter_wrap
= 0;
68 int option_auto_para_formatting
= 0;
69 int option_fill_tabs_with_spaces
= 0;
70 int option_return_does_auto_indent
= 1;
71 int option_backspace_through_tabs
= 0;
72 int option_fake_half_tabs
= 1;
73 int option_save_mode
= EDIT_QUICK_SAVE
;
74 int option_save_position
= 1;
75 int option_max_undo
= 32768;
76 int option_persistent_selections
= 1;
77 int option_cursor_beyond_eol
= 0;
78 int option_line_state
= 0;
79 int option_line_state_width
= 0;
81 int option_edit_right_extreme
= 0;
82 int option_edit_left_extreme
= 0;
83 int option_edit_top_extreme
= 0;
84 int option_edit_bottom_extreme
= 0;
85 int enable_show_tabs_tws
= 1;
86 int option_check_nl_at_eof
= 0;
87 int show_right_margin
= 0;
89 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext
= NULL
;
92 int edit_stack_iterator
= 0;
93 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
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
125 const global_keymap_t
*editor_map
;
126 const global_keymap_t
*editor_x_map
;
128 static void user_menu (WEdit
* edit
);
131 edit_get_byte (WEdit
* edit
, long byte_index
)
134 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
137 if (byte_index
>= edit
->curs1
)
139 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
140 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
144 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
149 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
152 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
155 if (byte_index
>= edit
->curs1
)
157 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
158 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
159 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
163 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
164 (byte_index
& M_EDIT_BUF_SIZE
));
169 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
173 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
179 if (byte_index
>= edit
->curs1
)
181 p
= edit
->curs1
+ edit
->curs2
- 1;
182 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
183 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
187 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
192 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
197 gchar
*next_ch
= NULL
;
200 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
206 str
= edit_get_byte_ptr (edit
, byte_index
);
214 res
= g_utf8_get_char_validated (str
, -1);
224 /* Calculate UTF-8 char width */
225 next_ch
= g_utf8_next_char (str
);
228 width
= next_ch
- str
;
241 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
243 gchar
*str
, *buf
= NULL
;
246 gchar
*next_ch
= NULL
;
252 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
258 ch
= edit_get_utf (edit
, byte_index
, &width
);
266 str
= edit_get_byte_ptr (edit
, byte_index
);
267 buf
= edit_get_buf_ptr (edit
, byte_index
);
268 if (str
== NULL
|| buf
== NULL
)
273 /* get prev utf8 char */
275 str
= g_utf8_find_prev_char (buf
, str
);
277 res
= g_utf8_get_char_validated (str
, -1);
287 /* Calculate UTF-8 char width */
288 next_ch
= g_utf8_next_char (str
);
291 width
= next_ch
- str
;
304 * Initialize the buffers for an empty files.
307 edit_init_buffers (WEdit
* edit
)
311 for (j
= 0; j
<= MAXBUFF
; j
++)
313 edit
->buffers1
[j
] = NULL
;
314 edit
->buffers2
[j
] = NULL
;
319 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
323 * Load file OR text into buffers. Set cursor to the beginning of file.
327 edit_load_file_fast (WEdit
* edit
, const char *filename
)
332 edit
->curs2
= edit
->last_byte
;
333 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
335 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
337 GString
*errmsg
= g_string_new (NULL
);
338 g_string_sprintf (errmsg
, _(" Cannot open %s for reading "), filename
);
339 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
340 g_string_free (errmsg
, TRUE
);
344 if (!edit
->buffers2
[buf2
])
345 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
350 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
351 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
354 for (buf
= buf2
- 1; buf
>= 0; buf
--)
356 /* edit->buffers2[0] is already allocated */
357 if (!edit
->buffers2
[buf
])
358 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
359 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
366 char *err_str
= g_strdup_printf(_(" Error reading %s "), filename
);
367 edit_error_dialog (_("Error"), err_str
);
374 /* detecting an error on save is easy: just check if every byte has been written. */
375 /* detecting an error on read, is not so easy 'cos there is not way to tell
376 whether you read everything or not. */
377 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
378 static const struct edit_filters
380 const char *read
, *write
, *extension
;
384 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
385 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
386 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
387 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
388 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
392 /* Return index of the filter or -1 is there is no appropriate filter */
394 edit_find_filter (const char *filename
)
398 if (filename
== NULL
)
401 l
= strlen (filename
);
402 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
404 e
= strlen (all_filters
[i
].extension
);
406 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
413 edit_get_filter (const char *filename
)
416 char *p
, *quoted_name
;
418 i
= edit_find_filter (filename
);
422 quoted_name
= name_quote (filename
, 0);
423 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
424 g_free (quoted_name
);
429 edit_get_write_filter (const char *write_name
, const char *filename
)
434 i
= edit_find_filter (filename
);
438 writename
= name_quote (write_name
, 0);
439 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
445 edit_insert_stream (WEdit
* edit
, FILE * f
)
449 while ((c
= fgetc (f
)) >= 0)
451 edit_insert (edit
, c
);
458 edit_write_stream (WEdit
* edit
, FILE * f
)
462 if (edit
->lb
== LB_ASIS
)
464 for (i
= 0; i
< edit
->last_byte
; i
++)
465 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
470 /* change line breaks */
471 for (i
= 0; i
< edit
->last_byte
; i
++)
473 unsigned char c
= edit_get_byte (edit
, i
);
475 if (!(c
== '\n' || c
== '\r'))
478 if (fputc (c
, f
) < 0)
482 { /* (c == '\n' || c == '\r') */
483 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
487 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
488 /* put one line break unconditionally */
489 if (fputc ('\n', f
) < 0)
492 i
++; /* 2 chars are processed */
494 if (c
== '\r' && c1
== '\n')
495 /* Windows line break; go to the next char */
498 if (c
== '\r' && c1
== '\r')
500 /* two Macintosh line breaks; put second line break */
501 if (fputc ('\n', f
) < 0)
506 if (fputc (c1
, f
) < 0)
510 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
511 /* put one line break unconditionally */
512 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
515 if (c
== '\r' && c1
== '\n')
516 /* Windows line break; go to the next char */
520 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
521 /* put one line break unconditionally */
522 if (fputc ('\r', f
) < 0)
525 i
++; /* 2 chars are processed */
527 if (c
== '\r' && c1
== '\n')
528 /* Windows line break; go to the next char */
531 if (c
== '\n' && c1
== '\n')
533 /* two Windows line breaks; put second line break */
534 if (fputc ('\r', f
) < 0)
539 if (fputc (c1
, f
) < 0)
542 case LB_ASIS
: /* default without changes */
548 return edit
->last_byte
;
551 #define TEMP_BUF_LEN 1024
553 /* inserts a file at the cursor, returns 1 on success */
555 edit_insert_file (WEdit
* edit
, const char *filename
)
559 p
= edit_get_filter (filename
);
563 long current
= edit
->curs1
;
564 f
= (FILE *) popen (p
, "r");
567 edit_insert_stream (edit
, f
);
568 edit_cursor_move (edit
, current
- edit
->curs1
);
572 errmsg
= g_strdup_printf (_(" Error reading from pipe: %s "), p
);
573 edit_error_dialog (_("Error"), errmsg
);
582 errmsg
= g_strdup_printf (_(" Cannot open pipe for reading: %s "), p
);
583 edit_error_dialog (_("Error"), errmsg
);
592 int i
, file
, blocklen
;
593 long current
= edit
->curs1
;
594 int vertical_insertion
= 0;
596 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
598 buf
= g_malloc0 (TEMP_BUF_LEN
);
599 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
602 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
603 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
605 vertical_insertion
= 1;
609 mc_lseek (file
, 0, SEEK_SET
);
612 if (vertical_insertion
)
614 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
618 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
620 for (i
= 0; i
< blocklen
; i
++)
621 edit_insert (edit
, buf
[i
]);
624 edit_cursor_move (edit
, current
- edit
->curs1
);
633 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
635 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
638 GString
*errmsg
= (GString
*) 0;
640 /* Try opening an existing file */
641 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
646 * Try creating the file. O_EXCL prevents following broken links
647 * and opening existing files.
649 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
652 g_string_sprintf (errmsg
= g_string_new (NULL
),
653 _(" Cannot open %s for reading "), filename
);
658 /* New file, delete it if it's not modified or saved */
659 edit
->delete_file
= 1;
663 /* Check what we have opened */
664 if (mc_fstat (file
, st
) < 0)
666 g_string_sprintf (errmsg
= g_string_new (NULL
),
667 _(" Cannot get size/permissions for %s "), filename
);
671 /* We want to open regular files only */
672 if (!S_ISREG (st
->st_mode
))
674 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" %s is not a regular file "), filename
);
679 * Don't delete non-empty files.
680 * O_EXCL should prevent it, but let's be on the safe side.
684 edit
->delete_file
= 0;
687 if (st
->st_size
>= SIZE_LIMIT
)
689 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" File %s is too large "), filename
);
693 (void) mc_close (file
);
696 edit_error_dialog (_("Error"), errmsg
->str
);
697 g_string_free (errmsg
, TRUE
);
704 * Open the file and load it into the buffers, either directly or using
705 * a filter. Return 0 on success, 1 on error.
707 * Fast loading (edit_load_file_fast) is used when the file size is
708 * known. In this case the data is read into the buffers by blocks.
709 * If the file size is not known, the data is loaded byte by byte in
713 edit_load_file (WEdit
* edit
)
717 /* Cannot do fast load if a filter is used */
718 if (edit_find_filter (edit
->filename
) >= 0)
722 * VFS may report file size incorrectly, and slow load is not a big
723 * deal considering overhead in VFS.
725 if (!vfs_file_is_local (edit
->filename
))
729 * FIXME: line end translation should disable fast loading as well
730 * Consider doing fseek() to the end and ftell() for the real size.
735 /* If we are dealing with a real file, check that it exists */
736 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
741 /* nothing to load */
745 edit_init_buffers (edit
);
749 edit
->last_byte
= edit
->stat1
.st_size
;
750 edit_load_file_fast (edit
, edit
->filename
);
751 /* If fast load was used, the number of lines wasn't calculated */
752 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
757 const char *codepage_id
;
762 edit
->stack_disable
= 1;
763 if (!edit_insert_file (edit
, edit
->filename
))
768 edit
->stack_disable
= 0;
772 codepage_id
= get_codepage_id (source_codepage
);
774 edit
->utf8
= str_isutf8 (codepage_id
);
781 /* Restore saved cursor position in the file */
783 edit_load_position (WEdit
* edit
)
789 if (!edit
->filename
|| !*edit
->filename
)
792 filename
= vfs_canon (edit
->filename
);
793 load_file_position (filename
, &line
, &column
, &offset
);
798 edit_move_to_line (edit
, line
- 1);
799 edit
->prev_col
= column
;
803 edit_cursor_move (edit
, offset
);
804 line
= edit
->curs_line
;
806 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
807 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
810 /* Save cursor position in the file */
812 edit_save_position (WEdit
* edit
)
816 if (!edit
->filename
|| !*edit
->filename
)
819 filename
= vfs_canon (edit
->filename
);
820 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
824 /* Clean the WEdit stricture except the widget part */
826 edit_purge_widget (WEdit
* edit
)
828 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
829 char *start
= (char *) edit
+ sizeof (Widget
);
830 memset (start
, 0, len
);
831 edit
->macro_i
= -1; /* not recording a macro */
835 edit_set_keymap (void)
837 editor_map
= default_editor_keymap
;
838 if (editor_keymap
&& editor_keymap
->len
> 0)
839 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
841 editor_x_map
= default_editor_x_keymap
;
842 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
843 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
847 #define space_width 1
850 * Fill in the edit structure. Return NULL on failure. Pass edit as
851 * NULL to allocate a new structure.
853 * If line is 0, try to restore saved position. Otherwise put the
854 * cursor on that line and show it in the middle of the screen.
857 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
860 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
861 if (option_line_state
)
863 option_line_state_width
= LINE_STATE_WIDTH
;
867 option_line_state_width
= 0;
873 * Expand option_whole_chars_search by national letters using
877 static char option_whole_chars_search_buf
[256];
879 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
882 size_t len
= str_term_width1 (option_whole_chars_search
);
884 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
886 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
888 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
890 option_whole_chars_search_buf
[len
++] = i
;
894 option_whole_chars_search_buf
[len
] = 0;
895 option_whole_chars_search
= option_whole_chars_search_buf
;
897 #endif /* ENABLE_NLS */
898 edit
= g_malloc0 (sizeof (WEdit
));
902 edit_purge_widget (edit
);
903 edit
->num_widget_lines
= lines
;
905 edit
->num_widget_columns
= columns
;
906 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
907 edit
->stat1
.st_uid
= getuid ();
908 edit
->stat1
.st_gid
= getgid ();
909 edit
->stat1
.st_mtime
= 0;
911 edit
->force
|= REDRAW_PAGE
;
912 edit_set_filename (edit
, filename
);
913 edit
->stack_size
= START_STACK_SIZE
;
914 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
915 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
916 if (edit_load_file (edit
))
918 /* edit_load_file already gives an error message */
924 edit
->converter
= str_cnv_from_term
;
927 const char *cp_id
= NULL
;
928 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
933 conv
= str_crt_conv_from (cp_id
);
934 if (conv
!= INVALID_CONV
)
936 if (edit
->converter
!= str_cnv_from_term
)
937 str_close_conv (edit
->converter
);
938 edit
->converter
= conv
;
942 edit
->utf8
= str_isutf8 (cp_id
);
946 edit
->loading_done
= 1;
949 edit_load_syntax (edit
, NULL
, NULL
);
952 edit_get_syntax_color (edit
, -1, &color
);
955 /* load saved cursor position */
956 if ((line
== 0) && option_save_position
)
958 edit_load_position (edit
);
964 edit_move_display (edit
, line
- 1);
965 edit_move_to_line (edit
, line
- 1);
973 /* Clear the edit struct, freeing everything in it. Return 1 on success */
975 edit_clean (WEdit
* edit
)
982 /* a stale lock, remove it */
984 edit
->locked
= edit_unlock_file (edit
->filename
);
986 /* save cursor position */
987 if (option_save_position
)
988 edit_save_position (edit
);
990 /* File specified on the mcedit command line and never saved */
991 if (edit
->delete_file
)
992 unlink (edit
->filename
);
994 edit_free_syntax_rules (edit
);
995 book_mark_flush (edit
, -1);
996 for (; j
<= MAXBUFF
; j
++)
998 g_free (edit
->buffers1
[j
]);
999 g_free (edit
->buffers2
[j
]);
1002 g_free (edit
->undo_stack
);
1003 g_free (edit
->filename
);
1006 mc_search_free (edit
->search
);
1007 edit
->search
= NULL
;
1009 if (edit
->converter
!= str_cnv_from_term
)
1010 str_close_conv (edit
->converter
);
1012 edit_purge_widget (edit
);
1017 /* returns 1 on success */
1019 edit_renew (WEdit
* edit
)
1021 int lines
= edit
->num_widget_lines
;
1022 int columns
= edit
->num_widget_columns
;
1025 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
1029 * Load a new file into the editor. If it fails, preserve the old file.
1030 * To do it, allocate a new widget, initialize it and, if the new file
1031 * was loaded, copy the data to the old widget.
1032 * Return 1 on success, 0 on failure.
1035 edit_reload (WEdit
* edit
, const char *filename
)
1038 int lines
= edit
->num_widget_lines
;
1039 int columns
= edit
->num_widget_columns
;
1041 e
= g_malloc0 (sizeof (WEdit
));
1042 e
->widget
= edit
->widget
;
1043 if (!edit_init (e
, lines
, columns
, filename
, 0))
1049 memcpy (edit
, e
, sizeof (WEdit
));
1055 * Load a new file into the editor and set line. If it fails, preserve the old file.
1056 * To do it, allocate a new widget, initialize it and, if the new file
1057 * was loaded, copy the data to the old widget.
1058 * Return 1 on success, 0 on failure.
1061 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
1064 int lines
= edit
->num_widget_lines
;
1065 int columns
= edit
->num_widget_columns
;
1067 e
= g_malloc0 (sizeof (WEdit
));
1068 e
->widget
= edit
->widget
;
1069 if (!edit_init (e
, lines
, columns
, filename
, line
))
1075 memcpy (edit
, e
, sizeof (WEdit
));
1082 Recording stack for undo:
1083 The following is an implementation of a compressed stack. Identical
1084 pushes are recorded by a negative prefix indicating the number of times the
1085 same char was pushed. This saves space for repeated curs-left or curs-right
1102 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1103 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1104 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1105 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1108 The only way the cursor moves or the buffer is changed is through the routines:
1109 insert, backspace, insert_ahead, delete, and cursor_move.
1110 These record the reverse undo movements onto the stack each time they are
1113 Each key press results in a set of actions (insert; delete ...). So each time
1114 a key is pressed the current position of start_display is pushed as
1115 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1116 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1117 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1122 edit_push_action (WEdit
* edit
, long c
, ...)
1124 unsigned long sp
= edit
->stack_pointer
;
1128 /* first enlarge the stack if necessary */
1129 if (sp
> edit
->stack_size
- 10)
1131 if (option_max_undo
< 256)
1132 option_max_undo
= 256;
1133 if (edit
->stack_size
< (unsigned long) option_max_undo
)
1135 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1138 edit
->undo_stack
= t
;
1139 edit
->stack_size
<<= 1;
1140 edit
->stack_size_mask
= edit
->stack_size
- 1;
1144 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1145 if (edit
->stack_disable
)
1148 #ifdef FAST_MOVE_CURSOR
1149 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
1152 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1153 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1155 c
= -(va_arg (ap
, int));
1159 #endif /* ! FAST_MOVE_CURSOR */
1160 if (edit
->stack_bottom
!= sp
1161 && spm1
!= edit
->stack_bottom
1162 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
1165 if (edit
->undo_stack
[spm1
] < 0)
1167 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1170 if (edit
->undo_stack
[spm1
] > -1000000000)
1172 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1173 edit
->undo_stack
[spm1
]--;
1177 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1178 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1179 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1180 { /* a left then a right anihilate each other */
1181 if (edit
->undo_stack
[spm1
] == -2)
1182 edit
->stack_pointer
= spm1
;
1184 edit
->undo_stack
[spm1
]++;
1191 d
= edit
->undo_stack
[spm1
];
1195 return; /* --> no need to push multiple do-nothings */
1196 edit
->undo_stack
[sp
] = -2;
1199 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1200 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1201 { /* a left then a right anihilate each other */
1202 edit
->stack_pointer
= spm1
;
1208 edit
->undo_stack
[sp
] = c
;
1211 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1213 /* if the sp wraps round and catches the stack_bottom then erase
1214 * the first set of actions on the stack to make space - by moving
1215 * stack_bottom forward one "key press" */
1216 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1217 if ((unsigned long) c
== edit
->stack_bottom
||
1218 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1221 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1223 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
1224 && edit
->stack_bottom
!= edit
->stack_pointer
);
1226 /*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: */
1227 if (edit
->stack_pointer
!= edit
->stack_bottom
1228 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1229 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1233 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1234 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1237 pop_action (WEdit
* edit
)
1240 unsigned long sp
= edit
->stack_pointer
;
1241 if (sp
== edit
->stack_bottom
)
1243 return STACK_BOTTOM
;
1245 sp
= (sp
- 1) & edit
->stack_size_mask
;
1246 if ((c
= edit
->undo_stack
[sp
]) >= 0)
1248 /* edit->undo_stack[sp] = '@'; */
1249 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1252 if (sp
== edit
->stack_bottom
)
1254 return STACK_BOTTOM
;
1256 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1257 if (edit
->undo_stack
[sp
] == -2)
1259 /* edit->undo_stack[sp] = '@'; */
1260 edit
->stack_pointer
= sp
;
1263 edit
->undo_stack
[sp
]++;
1268 /* is called whenever a modification is made by one of the four routines below */
1270 edit_modification (WEdit
* edit
)
1272 edit
->caches_valid
= 0;
1273 edit
->screen_modified
= 1;
1275 /* raise lock when file modified */
1276 if (!edit
->modified
&& !edit
->delete_file
)
1277 edit
->locked
= edit_lock_file (edit
->filename
);
1282 Basic low level single character buffer alterations and movements at the cursor.
1283 Returns char passed over, inserted or removed.
1287 edit_insert (WEdit
* edit
, int c
)
1289 /* check if file has grown to large */
1290 if (edit
->last_byte
>= SIZE_LIMIT
)
1293 /* first we must update the position of the display window */
1294 if (edit
->curs1
< edit
->start_display
)
1296 edit
->start_display
++;
1301 /* Mark file as modified, unless the file hasn't been fully loaded */
1302 if (edit
->loading_done
)
1304 edit_modification (edit
);
1307 /* now we must update some info on the file and check if a redraw is required */
1310 if (edit
->book_mark
)
1311 book_mark_inc (edit
, edit
->curs_line
);
1313 edit
->total_lines
++;
1314 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1317 /* save the reverse command onto the undo stack */
1318 edit_push_action (edit
, BACKSPACE
);
1320 /* update markers */
1321 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1322 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1323 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1325 /* add a new buffer if we've reached the end of the last one */
1326 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1327 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1329 /* perform the insertion */
1330 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
1331 = (unsigned char) c
;
1333 /* update file length */
1336 /* update cursor position */
1341 edit_insert_over (WEdit
* edit
)
1345 for (i
= 0; i
< edit
->over_col
; i
++)
1347 edit_insert (edit
, ' ');
1352 /* same as edit_insert and move left */
1354 edit_insert_ahead (WEdit
* edit
, int c
)
1356 if (edit
->last_byte
>= SIZE_LIMIT
)
1358 if (edit
->curs1
< edit
->start_display
)
1360 edit
->start_display
++;
1364 edit_modification (edit
);
1367 if (edit
->book_mark
)
1368 book_mark_inc (edit
, edit
->curs_line
);
1369 edit
->total_lines
++;
1370 edit
->force
|= REDRAW_AFTER_CURSOR
;
1372 edit_push_action (edit
, DELCHAR
);
1374 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1375 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1376 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1378 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1379 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1380 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) -
1389 edit_delete (WEdit
* edit
, const int byte_delete
)
1398 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1399 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1400 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1403 /* if byte_delete = 1 then delete only one byte not multibyte char */
1404 if (edit
->utf8
&& byte_delete
== 0)
1406 edit_get_utf (edit
, edit
->curs1
, &cw
);
1410 for (i
= 1; i
<= cw
; i
++)
1412 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1414 1) & M_EDIT_BUF_SIZE
) - 1];
1416 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1418 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1419 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1423 edit_push_action (edit
, p
+ 256);
1426 edit_modification (edit
);
1429 if (edit
->book_mark
)
1430 book_mark_dec (edit
, edit
->curs_line
);
1431 edit
->total_lines
--;
1432 edit
->force
|= REDRAW_AFTER_CURSOR
;
1434 if (edit
->curs1
< edit
->start_display
)
1436 edit
->start_display
--;
1446 edit_backspace (WEdit
* edit
, const int byte_delete
)
1455 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1456 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1457 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1460 if (edit
->utf8
&& byte_delete
== 0)
1462 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1466 for (i
= 1; i
<= cw
; i
++)
1468 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
1469 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1470 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1472 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1473 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1477 edit_push_action (edit
, p
);
1479 edit_modification (edit
);
1482 if (edit
->book_mark
)
1483 book_mark_dec (edit
, edit
->curs_line
);
1485 edit
->total_lines
--;
1486 edit
->force
|= REDRAW_AFTER_CURSOR
;
1489 if (edit
->curs1
< edit
->start_display
)
1491 edit
->start_display
--;
1499 #ifdef FAST_MOVE_CURSOR
1502 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1505 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
1508 next
-= (unsigned long) dest
;
1516 edit_move_backward_lots (WEdit
* edit
, long increment
)
1519 unsigned char *p
= NULL
;
1521 if (increment
> edit
->curs1
)
1522 increment
= edit
->curs1
;
1525 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1527 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1530 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1535 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1536 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
1543 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1544 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1545 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1546 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1549 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1550 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1551 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1556 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1559 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1561 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1568 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1579 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1580 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
1585 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1586 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1589 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1591 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1592 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1597 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1600 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1602 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1609 return edit_get_byte (edit
, edit
->curs1
);
1612 #endif /* ! FAST_MOVE_CURSOR */
1614 /* moves the cursor right or left: increment positive or negative respectively */
1616 edit_cursor_move (WEdit
* edit
, long increment
)
1618 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1620 #ifdef FAST_MOVE_CURSOR
1621 if (increment
< -256)
1623 edit
->force
|= REDRAW_PAGE
;
1624 edit_move_backward_lots (edit
, -increment
);
1627 #endif /* ! FAST_MOVE_CURSOR */
1631 for (; increment
< 0; increment
++)
1636 edit_push_action (edit
, CURS_RIGHT
);
1638 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1639 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1640 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1641 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1642 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1644 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
1645 1) & M_EDIT_BUF_SIZE
];
1646 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1648 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1649 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1655 edit
->force
|= REDRAW_LINE_BELOW
;
1660 else if (increment
> 0)
1662 for (; increment
> 0; increment
--)
1667 edit_push_action (edit
, CURS_LEFT
);
1669 c
= edit_get_byte (edit
, edit
->curs1
);
1670 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1671 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1672 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1674 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1676 1) & M_EDIT_BUF_SIZE
) - 1];
1677 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1679 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1680 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1686 edit
->force
|= REDRAW_LINE_ABOVE
;
1692 /* These functions return positions relative to lines */
1694 /* returns index of last char on line + 1 */
1696 edit_eol (WEdit
* edit
, long current
)
1698 if (current
< edit
->last_byte
)
1701 if (edit_get_byte (edit
, current
) == '\n')
1705 return edit
->last_byte
;
1709 /* returns index of first char on line */
1711 edit_bol (WEdit
* edit
, long current
)
1716 if (edit_get_byte (edit
, current
- 1) == '\n')
1726 edit_count_lines (WEdit
* edit
, long current
, long upto
)
1729 if (upto
> edit
->last_byte
)
1730 upto
= edit
->last_byte
;
1733 while (current
< upto
)
1734 if (edit_get_byte (edit
, current
++) == '\n')
1740 /* If lines is zero this returns the count of lines from current to upto. */
1741 /* If upto is zero returns index of lines forward current. */
1743 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1747 return edit_count_lines (edit
, current
, upto
);
1756 next
= edit_eol (edit
, current
) + 1;
1757 if (next
> edit
->last_byte
)
1767 /* Returns offset of 'lines' lines up from current */
1769 edit_move_backward (WEdit
* edit
, long current
, long lines
)
1773 current
= edit_bol (edit
, current
);
1774 while ((lines
--) && current
!= 0)
1775 current
= edit_bol (edit
, current
- 1);
1779 /* If cols is zero this returns the count of columns from current to upto. */
1780 /* If upto is zero returns index of cols across from current. */
1782 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1793 q
= edit
->last_byte
+ 2;
1795 for (col
= 0, p
= current
; p
< q
; p
++)
1809 orig_c
= c
= edit_get_byte (edit
, p
);
1813 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1818 if (g_unichar_iswide (utf_ch
))
1821 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1825 c
= convert_to_display_c (c
);
1827 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1835 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1836 /* '\r' is shown as ^M, so we must advance 2 characters */
1837 /* Caret notation for control characters */
1845 /* returns the current column position of the cursor */
1847 edit_get_col (WEdit
* edit
)
1849 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1853 /* Scrolling functions */
1856 edit_update_curs_row (WEdit
* edit
)
1858 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1862 edit_update_curs_col (WEdit
* edit
)
1864 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1868 edit_get_curs_col (const WEdit
* edit
)
1870 return edit
->curs_col
;
1873 /*moves the display start position up by i lines */
1875 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1877 unsigned long lines_above
= edit
->start_line
;
1878 if (i
> lines_above
)
1882 edit
->start_line
-= i
;
1883 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1884 edit
->force
|= REDRAW_PAGE
;
1885 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1887 edit_update_curs_row (edit
);
1891 /* returns 1 if could scroll, 0 otherwise */
1893 edit_scroll_downward (WEdit
* edit
, int i
)
1896 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1897 if (lines_below
> 0)
1899 if (i
> lines_below
)
1901 edit
->start_line
+= i
;
1902 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1903 edit
->force
|= REDRAW_PAGE
;
1904 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1906 edit_update_curs_row (edit
);
1910 edit_scroll_right (WEdit
* edit
, int i
)
1912 edit
->force
|= REDRAW_PAGE
;
1913 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1914 edit
->start_col
-= i
;
1918 edit_scroll_left (WEdit
* edit
, int i
)
1920 if (edit
->start_col
)
1922 edit
->start_col
+= i
;
1923 if (edit
->start_col
> 0)
1924 edit
->start_col
= 0;
1925 edit
->force
|= REDRAW_PAGE
;
1926 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1930 /* high level cursor movement commands */
1933 is_in_indent (WEdit
* edit
)
1935 long p
= edit_bol (edit
, edit
->curs1
);
1936 while (p
< edit
->curs1
)
1937 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1942 static int left_of_four_spaces (WEdit
* edit
);
1945 edit_move_to_prev_col (WEdit
* edit
, long p
)
1947 int prev
= edit
->prev_col
;
1948 int over
= edit
->over_col
;
1949 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1951 if (option_cursor_beyond_eol
)
1953 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
1954 edit_eol (edit
, edit
->curs1
));
1956 if (line_len
< prev
+ edit
->over_col
)
1958 edit
->over_col
= prev
+ over
- line_len
;
1959 edit
->prev_col
= line_len
;
1960 edit
->curs_col
= line_len
;
1964 edit
->curs_col
= prev
+ over
;
1965 edit
->prev_col
= edit
->curs_col
;
1972 if (is_in_indent (edit
) && option_fake_half_tabs
)
1974 edit_update_curs_col (edit
);
1976 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
1978 int q
= edit
->curs_col
;
1979 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1980 p
= edit_bol (edit
, edit
->curs1
);
1981 edit_cursor_move (edit
,
1982 edit_move_forward3 (edit
, p
, edit
->curs_col
,
1984 if (!left_of_four_spaces (edit
))
1985 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1992 is_blank (WEdit
* edit
, long offset
)
1996 s
= edit_bol (edit
, offset
);
1997 f
= edit_eol (edit
, offset
) - 1;
2000 c
= edit_get_byte (edit
, s
++);
2008 /* returns the offset of line i */
2010 edit_find_line (WEdit
* edit
, int line
)
2014 if (!edit
->caches_valid
)
2016 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2017 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
2018 /* three offsets that we *know* are line 0 at 0 and these two: */
2019 edit
->line_numbers
[1] = edit
->curs_line
;
2020 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
2021 edit
->line_numbers
[2] = edit
->total_lines
;
2022 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
2023 edit
->caches_valid
= 1;
2025 if (line
>= edit
->total_lines
)
2026 return edit
->line_offsets
[2];
2029 /* find the closest known point */
2030 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2033 n
= abs (edit
->line_numbers
[i
] - line
);
2041 return edit
->line_offsets
[j
]; /* know the offset exactly */
2042 if (m
== 1 && j
>= 3)
2043 i
= j
; /* one line different - caller might be looping, so stay in this cache */
2045 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
2046 if (line
> edit
->line_numbers
[j
])
2047 edit
->line_offsets
[i
] =
2048 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
2050 edit
->line_offsets
[i
] =
2051 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
2052 edit
->line_numbers
[i
] = line
;
2053 return edit
->line_offsets
[i
];
2057 line_is_blank (WEdit
* edit
, long line
)
2059 return is_blank (edit
, edit_find_line (edit
, line
));
2062 /* moves up until a blank line is reached, or until just
2063 before a non-blank line is reached */
2065 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
2068 if (edit
->curs_line
> 1)
2070 if (line_is_blank (edit
, edit
->curs_line
))
2072 if (line_is_blank (edit
, edit
->curs_line
- 1))
2074 for (i
= edit
->curs_line
- 1; i
; i
--)
2075 if (!line_is_blank (edit
, i
))
2083 for (i
= edit
->curs_line
- 1; i
; i
--)
2084 if (line_is_blank (edit
, i
))
2090 for (i
= edit
->curs_line
- 1; i
; i
--)
2091 if (line_is_blank (edit
, i
))
2095 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
2098 /* moves down until a blank line is reached, or until just
2099 before a non-blank line is reached */
2101 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
2104 if (edit
->curs_line
>= edit
->total_lines
- 1)
2106 i
= edit
->total_lines
;
2110 if (line_is_blank (edit
, edit
->curs_line
))
2112 if (line_is_blank (edit
, edit
->curs_line
+ 1))
2114 for (i
= edit
->curs_line
+ 1; i
; i
++)
2115 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
2123 for (i
= edit
->curs_line
+ 1; i
; i
++)
2124 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2130 for (i
= edit
->curs_line
+ 1; i
; i
++)
2131 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2135 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
2139 edit_begin_page (WEdit
* edit
)
2141 edit_update_curs_row (edit
);
2142 edit_move_up (edit
, edit
->curs_row
, 0);
2146 edit_end_page (WEdit
* edit
)
2148 edit_update_curs_row (edit
);
2149 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
2153 /* goto beginning of text */
2155 edit_move_to_top (WEdit
* edit
)
2157 if (edit
->curs_line
)
2159 edit_cursor_move (edit
, -edit
->curs1
);
2160 edit_move_to_prev_col (edit
, 0);
2161 edit
->force
|= REDRAW_PAGE
;
2162 edit
->search_start
= 0;
2163 edit_update_curs_row (edit
);
2168 /* goto end of text */
2170 edit_move_to_bottom (WEdit
* edit
)
2172 if (edit
->curs_line
< edit
->total_lines
)
2174 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
2175 edit
->start_display
= edit
->last_byte
;
2176 edit
->start_line
= edit
->total_lines
;
2177 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
2178 edit
->force
|= REDRAW_PAGE
;
2182 /* goto beginning of line */
2184 edit_cursor_to_bol (WEdit
* edit
)
2186 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
2187 edit
->search_start
= edit
->curs1
;
2188 edit
->prev_col
= edit_get_col (edit
);
2192 /* goto end of line */
2194 edit_cursor_to_eol (WEdit
* edit
)
2196 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
2197 edit
->search_start
= edit
->curs1
;
2198 edit
->prev_col
= edit_get_col (edit
);
2202 /* move cursor to line 'line' */
2204 edit_move_to_line (WEdit
* e
, long line
)
2206 if (line
< e
->curs_line
)
2207 edit_move_up (e
, e
->curs_line
- line
, 0);
2209 edit_move_down (e
, line
- e
->curs_line
, 0);
2210 edit_scroll_screen_over_cursor (e
);
2213 /* scroll window so that first visible line is 'line' */
2215 edit_move_display (WEdit
* e
, long line
)
2217 if (line
< e
->start_line
)
2218 edit_scroll_upward (e
, e
->start_line
- line
);
2220 edit_scroll_downward (e
, line
- e
->start_line
);
2223 /* save markers onto undo stack */
2225 edit_push_markers (WEdit
* edit
)
2227 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2228 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2232 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2241 /* highlight marker toggle */
2243 edit_mark_cmd (WEdit
* edit
, int unmark
)
2245 edit_push_markers (edit
);
2248 edit_set_markers (edit
, 0, 0, 0, 0);
2249 edit
->force
|= REDRAW_PAGE
;
2253 if (edit
->mark2
>= 0)
2255 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
2256 edit
->curs_col
+ edit
->over_col
);
2257 edit
->force
|= REDRAW_PAGE
;
2260 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
2261 edit
->curs_col
+ edit
->over_col
);
2265 static unsigned long
2270 const char option_chars_move_whole_word
[] =
2271 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2277 if (*option_chars_move_whole_word
== '!')
2279 return 0x80000000UL
;
2281 if (g_ascii_isupper ((gchar
) c
))
2283 else if (g_ascii_islower ((gchar
) c
))
2285 else if (g_ascii_isalpha (c
))
2287 else if (isdigit (c
))
2289 else if (isspace (c
))
2291 q
= strchr (option_chars_move_whole_word
, c
);
2293 return 0xFFFFFFFFUL
;
2296 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2301 while ((q
= strchr (q
+ 1, c
)));
2306 edit_left_word_move (WEdit
* edit
, int s
)
2311 if (column_highlighting
2312 && edit
->mark1
!= edit
->mark2
2313 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2315 edit_cursor_move (edit
, -1);
2318 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2319 c2
= edit_get_byte (edit
, edit
->curs1
);
2320 if (!(my_type_of (c1
) & my_type_of (c2
)))
2322 if (isspace (c1
) && !isspace (c2
))
2325 if (!isspace (c1
) && isspace (c2
))
2331 edit_left_word_move_cmd (WEdit
* edit
)
2333 edit_left_word_move (edit
, 0);
2334 edit
->force
|= REDRAW_PAGE
;
2338 edit_right_word_move (WEdit
* edit
, int s
)
2343 if (column_highlighting
2344 && edit
->mark1
!= edit
->mark2
2345 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
2347 edit_cursor_move (edit
, 1);
2348 if (edit
->curs1
>= edit
->last_byte
)
2350 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2351 c2
= edit_get_byte (edit
, edit
->curs1
);
2352 if (!(my_type_of (c1
) & my_type_of (c2
)))
2354 if (isspace (c1
) && !isspace (c2
))
2357 if (!isspace (c1
) && isspace (c2
))
2363 edit_right_word_move_cmd (WEdit
* edit
)
2365 edit_right_word_move (edit
, 0);
2366 edit
->force
|= REDRAW_PAGE
;
2370 edit_right_char_move_cmd (WEdit
* edit
)
2376 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2382 c
= edit_get_byte (edit
, edit
->curs1
);
2384 if (option_cursor_beyond_eol
&& c
== '\n')
2390 edit_cursor_move (edit
, cw
);
2395 edit_left_char_move_cmd (WEdit
* edit
)
2398 if (column_highlighting
2399 && option_cursor_beyond_eol
2400 && edit
->mark1
!= edit
->mark2
2401 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2405 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2409 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2415 edit_cursor_move (edit
, -cw
);
2419 /** Up or down cursor moving.
2420 direction = TRUE - move up
2424 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2427 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
2436 edit
->force
|= REDRAW_PAGE
;
2440 edit_scroll_upward (edit
, i
);
2442 edit_scroll_downward (edit
, i
);
2444 p
= edit_bol (edit
, edit
->curs1
);
2446 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
2448 edit_cursor_move (edit
, p
- edit
->curs1
);
2450 edit_move_to_prev_col (edit
, p
);
2452 /* search start of current multibyte char (like CJK) */
2453 if (edit
->curs1
+ 1 < edit
->last_byte
)
2455 edit_right_char_move_cmd (edit
);
2456 edit_left_char_move_cmd (edit
);
2459 edit
->search_start
= edit
->curs1
;
2460 edit
->found_len
= 0;
2464 edit_right_delete_word (WEdit
* edit
)
2469 if (edit
->curs1
>= edit
->last_byte
)
2471 c1
= edit_delete (edit
, 1);
2472 c2
= edit_get_byte (edit
, edit
->curs1
);
2473 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2475 if (!(my_type_of (c1
) & my_type_of (c2
)))
2481 edit_left_delete_word (WEdit
* edit
)
2486 if (edit
->curs1
<= 0)
2488 c1
= edit_backspace (edit
, 1);
2489 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2490 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2492 if (!(my_type_of (c1
) & my_type_of (c2
)))
2498 the start column position is not recorded, and hence does not
2499 undo as it happed. But who would notice.
2502 edit_do_undo (WEdit
* edit
)
2507 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2509 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
2516 edit_cursor_move (edit
, 1);
2519 edit_cursor_move (edit
, -1);
2522 edit_backspace (edit
, 1);
2525 edit_delete (edit
, 1);
2528 column_highlighting
= 1;
2531 column_highlighting
= 0;
2534 if (ac
>= 256 && ac
< 512)
2535 edit_insert_ahead (edit
, ac
- 256);
2536 if (ac
>= 0 && ac
< 256)
2537 edit_insert (edit
, ac
);
2539 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
2541 edit
->mark1
= ac
- MARK_1
;
2542 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2544 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
2546 edit
->mark2
= ac
- MARK_2
;
2547 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2550 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2553 if (edit
->start_display
> ac
- KEY_PRESS
)
2555 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2556 edit
->force
|= REDRAW_PAGE
;
2558 else if (edit
->start_display
< ac
- KEY_PRESS
)
2560 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2561 edit
->force
|= REDRAW_PAGE
;
2563 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2564 edit_update_curs_row (edit
);
2567 edit
->stack_disable
= 0;
2571 edit_delete_to_line_end (WEdit
* edit
)
2573 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2577 edit_delete (edit
, 1);
2582 edit_delete_to_line_begin (WEdit
* edit
)
2584 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2588 edit_backspace (edit
, 1);
2593 edit_delete_line (WEdit
* edit
)
2596 * Delete right part of the line.
2597 * Note that edit_get_byte() returns '\n' when byte position is
2600 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2602 (void) edit_delete (edit
, 1);
2607 * Note that edit_delete() will not corrupt anything if called while
2608 * cursor position is EOF.
2610 (void) edit_delete (edit
, 1);
2613 * Delete left part of the line.
2614 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2616 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2618 (void) edit_backspace (edit
, 1);
2623 insert_spaces_tab (WEdit
* edit
, int half
)
2626 edit_update_curs_col (edit
);
2627 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
2628 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2631 edit_insert (edit
, ' ');
2637 is_aligned_on_a_tab (WEdit
* edit
)
2639 edit_update_curs_col (edit
);
2640 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2641 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2645 right_of_four_spaces (WEdit
* edit
)
2648 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2649 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2651 return is_aligned_on_a_tab (edit
);
2656 left_of_four_spaces (WEdit
* edit
)
2659 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2660 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2662 return is_aligned_on_a_tab (edit
);
2667 edit_indent_width (WEdit
* edit
, long p
)
2670 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2672 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2676 edit_insert_indent (WEdit
* edit
, int indent
)
2678 if (!option_fill_tabs_with_spaces
)
2680 while (indent
>= TAB_SIZE
)
2682 edit_insert (edit
, '\t');
2686 while (indent
-- > 0)
2687 edit_insert (edit
, ' ');
2691 edit_auto_indent (WEdit
* edit
)
2696 /* use the previous line as a template */
2697 p
= edit_move_backward (edit
, p
, 1);
2698 /* copy the leading whitespace of the line */
2700 { /* no range check - the line _is_ \n-terminated */
2701 c
= edit_get_byte (edit
, p
++);
2702 if (c
!= ' ' && c
!= '\t')
2704 edit_insert (edit
, c
);
2709 edit_double_newline (WEdit
* edit
)
2711 edit_insert (edit
, '\n');
2712 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2714 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2716 edit
->force
|= REDRAW_PAGE
;
2717 edit_insert (edit
, '\n');
2721 edit_tab_cmd (WEdit
* edit
)
2725 if (option_fake_half_tabs
)
2727 if (is_in_indent (edit
))
2729 /*insert a half tab (usually four spaces) unless there is a
2730 half tab already behind, then delete it and insert a
2732 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
2734 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2735 edit_backspace (edit
, 1);
2736 edit_insert (edit
, '\t');
2740 insert_spaces_tab (edit
, 1);
2745 if (option_fill_tabs_with_spaces
)
2747 insert_spaces_tab (edit
, 0);
2751 edit_insert (edit
, '\t');
2756 check_and_wrap_line (WEdit
* edit
)
2759 if (!option_typewriter_wrap
)
2761 edit_update_curs_col (edit
);
2762 if (edit
->curs_col
< option_word_wrap_line_length
)
2768 c
= edit_get_byte (edit
, curs
);
2769 if (c
== '\n' || curs
<= 0)
2771 edit_insert (edit
, '\n');
2774 if (c
== ' ' || c
== '\t')
2776 int current
= edit
->curs1
;
2777 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2778 edit_insert (edit
, '\n');
2779 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2785 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
2788 edit_push_key_press (WEdit
* edit
)
2790 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2791 if (edit
->mark2
== -1)
2792 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2795 /* this find the matching bracket in either direction, and sets edit->bracket */
2797 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2799 const char *const b
= "{}{[][()(", *p
;
2800 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2801 unsigned long j
= 0;
2803 edit_update_curs_row (edit
);
2804 c
= edit_get_byte (edit
, edit
->curs1
);
2807 if (!furthest_bracket_search
)
2808 furthest_bracket_search
--;
2809 /* not on a bracket at all */
2812 /* the matching bracket */
2814 /* going left or right? */
2815 if (strchr ("{[(", c
))
2817 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
2819 /* out of buffer? */
2820 if (q
>= edit
->last_byte
|| q
< 0)
2822 a
= edit_get_byte (edit
, q
);
2823 /* don't want to eat CPU */
2824 if (j
++ > furthest_bracket_search
)
2826 /* out of screen? */
2829 if (q
< edit
->start_display
)
2831 /* count lines if searching downward */
2832 if (inc
> 0 && a
== '\n')
2833 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2836 /* count bracket depth */
2837 i
+= (a
== c
) - (a
== d
);
2838 /* return if bracket depth is zero */
2846 static long last_bracket
= -1;
2849 edit_find_bracket (WEdit
* edit
)
2851 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2852 if (last_bracket
!= edit
->bracket
)
2853 edit
->force
|= REDRAW_PAGE
;
2854 last_bracket
= edit
->bracket
;
2858 edit_goto_matching_bracket (WEdit
* edit
)
2862 q
= edit_get_bracket (edit
, 0, 0);
2865 edit
->bracket
= edit
->curs1
;
2866 edit
->force
|= REDRAW_PAGE
;
2867 edit_cursor_move (edit
, q
- edit
->curs1
);
2872 * This executes a command as though the user initiated it through a key
2873 * press. Callback with WIDGET_KEY as a message calls this after
2874 * translating the key press. This function can be used to pass any
2875 * command to the editor. Note that the screen wouldn't update
2876 * automatically. Either of command or char_for_insertion must be
2877 * passed as -1. Commands are executed, and char_for_insertion is
2878 * inserted at the cursor.
2881 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2883 if (command
== CK_Begin_Record_Macro
)
2886 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2889 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
2891 edit
->force
|= REDRAW_COMPLETELY
;
2892 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2896 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
2898 edit
->macro
[edit
->macro_i
].command
= command
;
2899 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2901 /* record the beginning of a set of editing actions initiated by a key press */
2902 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2903 edit_push_key_press (edit
);
2905 edit_execute_cmd (edit
, command
, char_for_insertion
);
2906 if (column_highlighting
)
2907 edit
->force
|= REDRAW_PAGE
;
2910 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
2913 This executes a command at a lower level than macro recording.
2914 It also does not push a key_press onto the undo stack. This means
2915 that if it is called many times, a single undo command will undo
2916 all of them. It also does not check for the Undo command.
2919 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2921 edit
->force
|= REDRAW_LINE
;
2923 /* The next key press will unhighlight the found string, so update
2925 if (edit
->found_len
|| column_highlighting
)
2926 edit
->force
|= REDRAW_PAGE
;
2928 if (command
/ 100 == 6)
2929 { /* a highlight command like shift-arrow */
2930 column_highlighting
= 0;
2931 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
2933 edit_mark_cmd (edit
, 1); /* clear */
2934 edit_mark_cmd (edit
, 0); /* marking on */
2936 edit
->highlight
= 1;
2939 { /* any other command */
2940 if (edit
->highlight
)
2941 edit_mark_cmd (edit
, 0); /* clear */
2942 edit
->highlight
= 0;
2945 /* first check for undo */
2946 if (command
== CK_Undo
)
2948 edit_do_undo (edit
);
2949 edit
->found_len
= 0;
2950 edit
->prev_col
= edit_get_col (edit
);
2951 edit
->search_start
= edit
->curs1
;
2955 /* An ordinary key press */
2956 if (char_for_insertion
>= 0)
2958 /* if non persistent selection and text selected */
2959 if (!option_persistent_selections
)
2961 if (edit
->mark1
!= edit
->mark2
)
2962 edit_block_delete_cmd (edit
);
2964 if (edit
->overwrite
)
2966 /* remove char only one time, after input first byte, multibyte chars */
2967 if ((!utf8_display
|| edit
->charpoint
== 0)
2968 && edit_get_byte (edit
, edit
->curs1
) != '\n')
2969 edit_delete (edit
, 0);
2971 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2972 edit_insert_over (edit
);
2974 if (char_for_insertion
> 255 && utf8_display
== 0)
2976 unsigned char str
[6 + 1];
2978 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
2988 while (str
[i
] != 0 && i
<= 6)
2990 char_for_insertion
= str
[i
];
2991 edit_insert (edit
, char_for_insertion
);
2997 edit_insert (edit
, char_for_insertion
);
2999 if (option_auto_para_formatting
)
3001 format_paragraph (edit
, 0);
3002 edit
->force
|= REDRAW_PAGE
;
3005 check_and_wrap_line (edit
);
3006 edit
->found_len
= 0;
3007 edit
->prev_col
= edit_get_col (edit
);
3008 edit
->search_start
= edit
->curs1
;
3009 edit_find_bracket (edit
);
3017 case CK_Begin_Page_Highlight
:
3018 case CK_End_Page_Highlight
:
3025 if (edit
->mark2
>= 0)
3027 if (!option_persistent_selections
)
3029 if (column_highlighting
)
3030 edit_push_action (edit
, COLUMN_ON
);
3031 column_highlighting
= 0;
3032 edit_mark_cmd (edit
, 1);
3041 case CK_Begin_Page_Highlight
:
3042 case CK_End_Page_Highlight
:
3047 case CK_Word_Left_Highlight
:
3048 case CK_Word_Right_Highlight
:
3049 case CK_Up_Highlight
:
3050 case CK_Down_Highlight
:
3051 case CK_Up_Alt_Highlight
:
3052 case CK_Down_Alt_Highlight
:
3053 if (edit
->mark2
== -1)
3054 break; /*marking is following the cursor: may need to highlight a whole line */
3057 case CK_Left_Highlight
:
3058 case CK_Right_Highlight
:
3059 edit
->force
|= REDRAW_CHAR_ONLY
;
3062 /* basic cursor key commands */
3066 /* if non persistent selection and text selected */
3067 if (!option_persistent_selections
)
3069 if (edit
->mark1
!= edit
->mark2
)
3071 edit_block_delete_cmd (edit
);
3075 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3080 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3082 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3083 edit_backspace (edit
, 1);
3088 if (option_fake_half_tabs
)
3091 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3093 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3094 edit_backspace (edit
, 1);
3099 edit_backspace (edit
, 0);
3102 /* if non persistent selection and text selected */
3103 if (!option_persistent_selections
)
3105 if (edit
->mark1
!= edit
->mark2
)
3107 edit_block_delete_cmd (edit
);
3112 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3113 edit_insert_over (edit
);
3115 if (option_fake_half_tabs
)
3118 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3120 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3121 edit_delete (edit
, 1);
3125 edit_delete (edit
, 0);
3127 case CK_Delete_Word_Left
:
3129 edit_left_delete_word (edit
);
3131 case CK_Delete_Word_Right
:
3132 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3133 edit_insert_over (edit
);
3135 edit_right_delete_word (edit
);
3137 case CK_Delete_Line
:
3138 edit_delete_line (edit
);
3140 case CK_Delete_To_Line_End
:
3141 edit_delete_to_line_end (edit
);
3143 case CK_Delete_To_Line_Begin
:
3144 edit_delete_to_line_begin (edit
);
3148 if (option_auto_para_formatting
)
3150 edit_double_newline (edit
);
3151 if (option_return_does_auto_indent
)
3152 edit_auto_indent (edit
);
3153 format_paragraph (edit
, 0);
3157 edit_insert (edit
, '\n');
3158 if (option_return_does_auto_indent
)
3160 edit_auto_indent (edit
);
3165 edit_insert (edit
, '\n');
3168 case CK_Page_Up_Alt_Highlight
:
3169 column_highlighting
= 1;
3171 case CK_Page_Up_Highlight
:
3172 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3174 case CK_Page_Down_Alt_Highlight
:
3175 column_highlighting
= 1;
3177 case CK_Page_Down_Highlight
:
3178 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3180 case CK_Left_Alt_Highlight
:
3181 column_highlighting
= 1;
3183 case CK_Left_Highlight
:
3184 if (option_fake_half_tabs
)
3186 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3188 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3191 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3192 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3196 edit_left_char_move_cmd (edit
);
3198 case CK_Right_Alt_Highlight
:
3199 column_highlighting
= 1;
3201 case CK_Right_Highlight
:
3202 if (option_fake_half_tabs
)
3204 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3206 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3207 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3211 edit_right_char_move_cmd (edit
);
3214 case CK_Begin_Page_Highlight
:
3215 edit_begin_page (edit
);
3218 case CK_End_Page_Highlight
:
3219 edit_end_page (edit
);
3222 case CK_Word_Left_Highlight
:
3224 edit_left_word_move_cmd (edit
);
3227 case CK_Word_Right_Highlight
:
3229 edit_right_word_move_cmd (edit
);
3231 case CK_Up_Alt_Highlight
:
3232 column_highlighting
= 1;
3234 case CK_Up_Highlight
:
3235 edit_move_up (edit
, 1, 0);
3237 case CK_Down_Alt_Highlight
:
3238 column_highlighting
= 1;
3240 case CK_Down_Highlight
:
3241 edit_move_down (edit
, 1, 0);
3243 case CK_Paragraph_Up_Alt_Highlight
:
3244 column_highlighting
= 1;
3245 case CK_Paragraph_Up
:
3246 case CK_Paragraph_Up_Highlight
:
3247 edit_move_up_paragraph (edit
, 0);
3249 case CK_Paragraph_Down_Alt_Highlight
:
3250 column_highlighting
= 1;
3251 case CK_Paragraph_Down
:
3252 case CK_Paragraph_Down_Highlight
:
3253 edit_move_down_paragraph (edit
, 0);
3255 case CK_Scroll_Up_Alt_Highlight
:
3256 column_highlighting
= 1;
3258 case CK_Scroll_Up_Highlight
:
3259 edit_move_up (edit
, 1, 1);
3261 case CK_Scroll_Down_Alt_Highlight
:
3262 column_highlighting
= 1;
3263 case CK_Scroll_Down
:
3264 case CK_Scroll_Down_Highlight
:
3265 edit_move_down (edit
, 1, 1);
3268 case CK_Home_Highlight
:
3269 edit_cursor_to_bol (edit
);
3272 case CK_End_Highlight
:
3273 edit_cursor_to_eol (edit
);
3276 /* if text marked shift block */
3277 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3279 if (edit
->mark2
< 0)
3280 edit_mark_cmd (edit
, 0);
3281 edit_move_block_to_right (edit
);
3285 if (option_cursor_beyond_eol
)
3286 edit_insert_over (edit
);
3287 edit_tab_cmd (edit
);
3288 if (option_auto_para_formatting
)
3290 format_paragraph (edit
, 0);
3291 edit
->force
|= REDRAW_PAGE
;
3295 check_and_wrap_line (edit
);
3300 case CK_Toggle_Insert
:
3301 edit
->overwrite
= (edit
->overwrite
== 0);
3305 if (edit
->mark2
>= 0)
3307 if (column_highlighting
)
3308 edit_push_action (edit
, COLUMN_ON
);
3309 column_highlighting
= 0;
3311 edit_mark_cmd (edit
, 0);
3313 case CK_Column_Mark
:
3314 if (!column_highlighting
)
3315 edit_push_action (edit
, COLUMN_OFF
);
3316 column_highlighting
= 1;
3317 edit_mark_cmd (edit
, 0);
3320 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3321 edit
->force
|= REDRAW_PAGE
;
3324 if (column_highlighting
)
3325 edit_push_action (edit
, COLUMN_ON
);
3326 column_highlighting
= 0;
3327 edit_mark_cmd (edit
, 1);
3330 case CK_Toggle_Line_State
:
3331 option_line_state
= !option_line_state
;
3332 if (option_line_state
)
3334 option_line_state_width
= LINE_STATE_WIDTH
;
3338 option_line_state_width
= 0;
3340 edit
->force
|= REDRAW_PAGE
;
3343 case CK_Toggle_Show_Margin
:
3344 show_right_margin
= !show_right_margin
;
3345 edit
->force
|= REDRAW_PAGE
;
3348 case CK_Toggle_Bookmark
:
3349 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3350 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3351 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3353 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3355 case CK_Flush_Bookmarks
:
3356 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3357 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3358 edit
->force
|= REDRAW_PAGE
;
3360 case CK_Next_Bookmark
:
3361 if (edit
->book_mark
)
3363 struct _book_mark
*p
;
3364 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3368 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3369 || p
->line
< edit
->start_line
)
3370 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3371 edit_move_to_line (edit
, p
->line
);
3375 case CK_Prev_Bookmark
:
3376 if (edit
->book_mark
)
3378 struct _book_mark
*p
;
3379 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3380 while (p
->line
== edit
->curs_line
)
3385 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3386 || p
->line
< edit
->start_line
)
3387 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3388 edit_move_to_line (edit
, p
->line
);
3393 case CK_Beginning_Of_Text
:
3394 case CK_Beginning_Of_Text_Highlight
:
3395 edit_move_to_top (edit
);
3397 case CK_End_Of_Text
:
3398 case CK_End_Of_Text_Highlight
:
3399 edit_move_to_bottom (edit
);
3403 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3404 edit_insert_over (edit
);
3405 edit_block_copy_cmd (edit
);
3408 edit_block_delete_cmd (edit
);
3411 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3412 edit_insert_over (edit
);
3413 edit_block_move_cmd (edit
);
3416 case CK_Shift_Block_Left
:
3417 if (edit
->mark1
!= edit
->mark2
)
3418 edit_move_block_to_left (edit
);
3420 case CK_Shift_Block_Right
:
3421 if (edit
->mark1
!= edit
->mark2
)
3422 edit_move_block_to_right (edit
);
3425 edit_copy_to_X_buf_cmd (edit
);
3428 edit_cut_to_X_buf_cmd (edit
);
3431 /* if non persistent selection and text selected */
3432 if (!option_persistent_selections
)
3434 if (edit
->mark1
!= edit
->mark2
)
3435 edit_block_delete_cmd (edit
);
3437 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3438 edit_insert_over (edit
);
3439 edit_paste_from_X_buf_cmd (edit
);
3441 case CK_Selection_History
:
3442 edit_paste_from_history (edit
);
3446 edit_save_as_cmd (edit
);
3449 edit_save_confirm_cmd (edit
);
3452 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3455 edit_save_block_cmd (edit
);
3457 case CK_Insert_File
:
3458 edit_insert_file_cmd (edit
);
3461 case CK_Load_Prev_File
:
3462 edit_load_back_cmd (edit
);
3464 case CK_Load_Next_File
:
3465 edit_load_forward_cmd (edit
);
3468 case CK_Load_Syntax_File
:
3469 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3471 case CK_Choose_Syntax
:
3472 edit_syntax_dialog (edit
, edit
->syntax_type
);
3475 case CK_Load_Menu_File
:
3476 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3479 case CK_Toggle_Syntax
:
3480 if ((option_syntax_highlighting
^= 1) == 1)
3481 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3482 edit
->force
|= REDRAW_PAGE
;
3485 case CK_Toggle_Tab_TWS
:
3486 enable_show_tabs_tws
^= 1;
3487 edit
->force
|= REDRAW_PAGE
;
3491 edit_search_cmd (edit
, 0);
3494 edit_search_cmd (edit
, 1);
3497 edit_replace_cmd (edit
, 0);
3499 case CK_Replace_Again
:
3500 edit_replace_cmd (edit
, 1);
3502 case CK_Complete_Word
:
3503 /* if text marked shift block */
3504 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3506 edit_move_block_to_left (edit
);
3510 edit_complete_word_cmd (edit
);
3513 case CK_Find_Definition
:
3514 edit_get_match_keyword_cmd (edit
);
3517 dlg_stop (edit
->widget
.parent
);
3520 edit_new_cmd (edit
);
3523 edit_help_cmd (edit
);
3526 edit_refresh_cmd (edit
);
3528 case CK_SaveSetupCmd
:
3532 query_dialog (_(" About "),
3533 _("\n Cooledit v3.11.5\n\n"
3534 " Copyright (C) 1996 the Free Software Foundation\n\n"
3535 " A user friendly text editor written\n"
3536 " for the Midnight Commander.\n"), D_NORMAL
, 1, _("&OK"));
3541 case CK_Edit_Options
:
3542 edit_options_dialog (edit
);
3544 case CK_Edit_Save_Mode
:
3545 menu_save_mode_cmd ();
3550 /* fool gcc to prevent a Y2K warning */
3551 char time_format
[] = "_c";
3552 time_format
[0] = '%';
3554 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3555 edit_print_string (edit
, s
);
3556 edit
->force
|= REDRAW_PAGE
;
3561 edit_goto_cmd (edit
);
3563 case CK_Paragraph_Format
:
3564 format_paragraph (edit
, 1);
3565 edit
->force
|= REDRAW_PAGE
;
3567 case CK_Delete_Macro
:
3568 edit_delete_macro_cmd (edit
);
3570 case CK_Match_Bracket
:
3571 edit_goto_matching_bracket (edit
);
3577 edit_sort_cmd (edit
);
3580 edit_ext_cmd (edit
);
3583 edit_mail_dialog (edit
);
3588 case CK_SelectCodepage
:
3589 edit_select_codepage_cmd (edit
);
3591 case CK_Insert_Literal
:
3592 edit_insert_literal_cmd (edit
);
3594 case CK_Execute_Macro
:
3595 edit_execute_macro_cmd (edit
);
3597 case CK_Begin_End_Macro
:
3598 edit_begin_end_macro_cmd (edit
);
3608 if ((command
/ 1000) == 1) /* a shell command */
3609 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3610 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
3611 { /* a macro command */
3612 struct macro m
[MAX_MACRO_LENGTH
];
3614 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3615 edit_execute_macro (edit
, m
, nm
);
3618 /* keys which must set the col position, and the search vars */
3624 case CK_Replace_Again
:
3625 case CK_Complete_Word
:
3626 edit
->prev_col
= edit_get_col (edit
);
3629 case CK_Up_Highlight
:
3630 case CK_Up_Alt_Highlight
:
3632 case CK_Down_Highlight
:
3633 case CK_Down_Alt_Highlight
:
3635 case CK_Page_Up_Highlight
:
3636 case CK_Page_Up_Alt_Highlight
:
3638 case CK_Page_Down_Highlight
:
3639 case CK_Page_Down_Alt_Highlight
:
3640 case CK_Beginning_Of_Text
:
3641 case CK_Beginning_Of_Text_Highlight
:
3642 case CK_End_Of_Text
:
3643 case CK_End_Of_Text_Highlight
:
3644 case CK_Paragraph_Up
:
3645 case CK_Paragraph_Up_Highlight
:
3646 case CK_Paragraph_Up_Alt_Highlight
:
3647 case CK_Paragraph_Down
:
3648 case CK_Paragraph_Down_Highlight
:
3649 case CK_Paragraph_Down_Alt_Highlight
:
3651 case CK_Scroll_Up_Highlight
:
3652 case CK_Scroll_Up_Alt_Highlight
:
3653 case CK_Scroll_Down
:
3654 case CK_Scroll_Down_Highlight
:
3655 case CK_Scroll_Down_Alt_Highlight
:
3656 edit
->search_start
= edit
->curs1
;
3657 edit
->found_len
= 0;
3660 edit
->found_len
= 0;
3661 edit
->prev_col
= edit_get_col (edit
);
3662 edit
->search_start
= edit
->curs1
;
3664 edit_find_bracket (edit
);
3666 if (option_auto_para_formatting
)
3672 case CK_Delete_Word_Left
:
3673 case CK_Delete_Word_Right
:
3674 case CK_Delete_To_Line_End
:
3675 case CK_Delete_To_Line_Begin
:
3676 format_paragraph (edit
, 0);
3677 edit
->force
|= REDRAW_PAGE
;
3684 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
3688 if (edit
->macro_depth
++ > 256)
3690 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3691 edit
->macro_depth
--;
3694 edit
->force
|= REDRAW_PAGE
;
3697 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3699 edit_update_screen (edit
);
3700 edit
->macro_depth
--;
3703 /* User edit menu, like user menu (F2) but only in editor. */
3705 user_menu (WEdit
* edit
)
3709 long start_mark
, end_mark
;
3712 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3714 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3716 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3718 /* run shell scripts from menu */
3719 user_menu_cmd (edit
);
3721 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
3728 /* i.e. we have marked block */
3729 rc
= edit_block_delete_cmd (edit
);
3733 edit_insert_file (edit
, block_file
);
3735 /* truncate block file */
3736 fd
= fopen (block_file
, "w");
3740 edit_refresh_cmd (edit
);
3741 edit
->force
|= REDRAW_COMPLETELY
;
3743 g_free (block_file
);
3747 edit_stack_init (void)
3749 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3751 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3752 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3755 edit_stack_iterator
= 0;
3759 edit_stack_free (void)
3761 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3762 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3767 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3769 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3774 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3776 edit_move_updown (edit
, i
, do_scroll
, FALSE
);