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
27 #include <sys/types.h>
35 #include "../src/global.h"
39 #include "edit-widget.h"
40 #include "editcmddef.h"
43 #include "../src/cmd.h" /* view_other_cmd() */
44 #include "../src/user.h" /* user_menu_cmd() */
45 #include "../src/wtools.h" /* query_dialog() */
46 #include "../src/timefmt.h" /* time formatting */
47 #include "../src/strutil.h" /* utf string functions */
48 #include "../src/charsets.h" /* get_codepage_id */
49 #include "../src/main.h" /* source_codepage */
52 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
53 or EDIT_KEY_EMULATION_EMACS
55 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
57 int option_word_wrap_line_length
= 72;
58 int option_typewriter_wrap
= 0;
59 int option_auto_para_formatting
= 0;
60 int option_tab_spacing
= 8;
61 int option_fill_tabs_with_spaces
= 0;
62 int option_return_does_auto_indent
= 1;
63 int option_backspace_through_tabs
= 0;
64 int option_fake_half_tabs
= 1;
65 int option_save_mode
= EDIT_QUICK_SAVE
;
66 int option_save_position
= 1;
67 int option_max_undo
= 32768;
68 int option_persistent_selections
= 1;
70 int option_edit_right_extreme
= 0;
71 int option_edit_left_extreme
= 0;
72 int option_edit_top_extreme
= 0;
73 int option_edit_bottom_extreme
= 0;
75 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
76 char *option_backup_ext
= NULL
;
80 * here's a quick sketch of the layout: (don't run this through indent.)
82 * (b1 is buffers1 and b2 is buffers2)
85 * \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
86 * ______________________________________|______________________________________
88 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
89 * |-> |-> |-> |-> |-> |-> |
91 * _<------------------------->|<----------------->_
92 * WEdit->curs2 | WEdit->curs1
97 * file end|||file beginning
107 static void user_menu (WEdit
*edit
);
109 int edit_get_byte (WEdit
* edit
, long byte_index
)
112 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
115 if (byte_index
>= edit
->curs1
) {
116 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
117 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
119 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
123 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
127 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
130 if (byte_index
>= edit
->curs1
) {
131 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
132 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
]+(EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
134 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
]+(byte_index
& M_EDIT_BUF_SIZE
));
138 char *edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
142 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) ) {
146 if ( byte_index
< 0 ) {
150 if (byte_index
>= edit
->curs1
) {
151 p
= edit
->curs1
+ edit
->curs2
- 1;
152 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
154 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
158 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
163 gchar
*next_ch
= NULL
;
166 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
172 str
= edit_get_byte_ptr (edit
, byte_index
);
173 res
= g_utf8_get_char_validated (str
, -1);
180 /* Calculate UTF-8 char width */
181 next_ch
= g_utf8_next_char(str
);
183 if ( next_ch
!= str
) {
184 width
= next_ch
- str
;
197 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
199 gchar
*str
, *buf
= NULL
;
202 gchar
*next_ch
= NULL
;
205 if ( byte_index
> 0 ) {
209 ch
= edit_get_utf (edit
, byte_index
, &width
);
215 if ( byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0 ) {
220 str
= edit_get_byte_ptr (edit
, byte_index
);
221 buf
= edit_get_buf_ptr (edit
, byte_index
);
222 /* get prev utf8 char */
224 str
= g_utf8_find_prev_char (buf
, str
);
226 res
= g_utf8_get_char_validated (str
, -1);
232 /* Calculate UTF-8 char width */
233 next_ch
= g_utf8_next_char(str
);
235 if ( next_ch
!= str
) {
236 width
= next_ch
- str
;
250 * Initialize the buffers for an empty files.
253 edit_init_buffers (WEdit
*edit
)
257 for (j
= 0; j
<= MAXBUFF
; j
++) {
258 edit
->buffers1
[j
] = NULL
;
259 edit
->buffers2
[j
] = NULL
;
264 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
268 * Load file OR text into buffers. Set cursor to the beginning of file.
272 edit_load_file_fast (WEdit
*edit
, const char *filename
)
277 edit
->curs2
= edit
->last_byte
;
278 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
281 if ( get_codepage_id( source_codepage
) )
282 edit
->utf8
= str_isutf8 (get_codepage_id( source_codepage
));
284 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
285 GString
*errmsg
= g_string_new(NULL
);
286 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
287 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
288 g_string_free (errmsg
, TRUE
);
292 if (!edit
->buffers2
[buf2
])
293 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
296 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
297 (edit
->curs2
& M_EDIT_BUF_SIZE
),
298 edit
->curs2
& M_EDIT_BUF_SIZE
);
300 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
301 /* edit->buffers2[0] is already allocated */
302 if (!edit
->buffers2
[buf
])
303 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
304 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
311 /* detecting an error on save is easy: just check if every byte has been written. */
312 /* detecting an error on read, is not so easy 'cos there is not way to tell
313 whether you read everything or not. */
314 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
315 static const struct edit_filters
{
316 const char *read
, *write
, *extension
;
318 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
319 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
320 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
321 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
324 /* Return index of the filter or -1 is there is no appropriate filter */
325 static int edit_find_filter (const char *filename
)
330 l
= strlen (filename
);
331 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
332 e
= strlen (all_filters
[i
].extension
);
334 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
341 edit_get_filter (const char *filename
)
344 char *p
, *quoted_name
;
345 i
= edit_find_filter (filename
);
348 quoted_name
= name_quote (filename
, 0);
349 l
= str_term_width1 (quoted_name
);
350 p
= g_malloc (str_term_width1 (all_filters
[i
].read
) + l
+ 2);
351 sprintf (p
, all_filters
[i
].read
, quoted_name
);
352 g_free (quoted_name
);
357 edit_get_write_filter (const char *write_name
, const char *filename
)
361 i
= edit_find_filter (filename
);
364 writename
= name_quote (write_name
, 0);
365 l
= str_term_width1 (writename
);
366 p
= g_malloc (str_term_width1 (all_filters
[i
].write
) + l
+ 2);
367 sprintf (p
, all_filters
[i
].write
, writename
);
373 edit_insert_stream (WEdit
* edit
, FILE * f
)
377 while ((c
= fgetc (f
)) >= 0) {
378 edit_insert (edit
, c
);
384 long edit_write_stream (WEdit
* edit
, FILE * f
)
387 for (i
= 0; i
< edit
->last_byte
; i
++)
388 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
393 #define TEMP_BUF_LEN 1024
395 /* inserts a file at the cursor, returns 1 on success */
397 edit_insert_file (WEdit
*edit
, const char *filename
)
400 if ((p
= edit_get_filter (filename
))) {
402 long current
= edit
->curs1
;
403 f
= (FILE *) popen (p
, "r");
405 edit_insert_stream (edit
, f
);
406 edit_cursor_move (edit
, current
- edit
->curs1
);
407 if (pclose (f
) > 0) {
408 GString
*errmsg
= g_string_new (NULL
);
409 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
410 edit_error_dialog (_("Error"), errmsg
->str
);
411 g_string_free (errmsg
, TRUE
);
416 GString
*errmsg
= g_string_new (NULL
);
417 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
418 edit_error_dialog (_("Error"), errmsg
->str
);
419 g_string_free (errmsg
, TRUE
);
425 int i
, file
, blocklen
;
426 long current
= edit
->curs1
;
428 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
430 buf
= g_malloc (TEMP_BUF_LEN
);
431 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
432 for (i
= 0; i
< blocklen
; i
++)
433 edit_insert (edit
, buf
[i
]);
435 edit_cursor_move (edit
, current
- edit
->curs1
);
444 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
446 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
449 GString
*errmsg
= (GString
*) 0;
451 /* Try opening an existing file */
452 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
456 * Try creating the file. O_EXCL prevents following broken links
457 * and opening existing files.
461 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
464 g_string_sprintf (errmsg
= g_string_new (NULL
),
465 _(" Cannot open %s for reading "), filename
);
468 /* New file, delete it if it's not modified or saved */
469 edit
->delete_file
= 1;
473 /* Check what we have opened */
474 if (mc_fstat (file
, st
) < 0) {
475 g_string_sprintf (errmsg
= g_string_new (NULL
),
476 _(" Cannot get size/permissions for %s "), filename
);
480 /* We want to open regular files only */
481 if (!S_ISREG (st
->st_mode
)) {
482 g_string_sprintf (errmsg
= g_string_new (NULL
),
483 _(" %s is not a regular file "), filename
);
488 * Don't delete non-empty files.
489 * O_EXCL should prevent it, but let's be on the safe side.
491 if (st
->st_size
> 0) {
492 edit
->delete_file
= 0;
495 if (st
->st_size
>= SIZE_LIMIT
) {
496 g_string_sprintf (errmsg
= g_string_new (NULL
),
497 _(" File %s is too large "), filename
);
502 (void) mc_close (file
);
504 edit_error_dialog (_("Error"), errmsg
->str
);
505 g_string_free (errmsg
, TRUE
);
512 * Open the file and load it into the buffers, either directly or using
513 * a filter. Return 0 on success, 1 on error.
515 * Fast loading (edit_load_file_fast) is used when the file size is
516 * known. In this case the data is read into the buffers by blocks.
517 * If the file size is not known, the data is loaded byte by byte in
521 edit_load_file (WEdit
*edit
)
525 /* Cannot do fast load if a filter is used */
526 if (edit_find_filter (edit
->filename
) >= 0)
530 * VFS may report file size incorrectly, and slow load is not a big
531 * deal considering overhead in VFS.
533 if (!vfs_file_is_local (edit
->filename
))
537 * FIXME: line end translation should disable fast loading as well
538 * Consider doing fseek() to the end and ftell() for the real size.
541 if (*edit
->filename
) {
542 /* If we are dealing with a real file, check that it exists */
543 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
546 /* nothing to load */
550 edit_init_buffers (edit
);
553 edit
->last_byte
= edit
->stat1
.st_size
;
554 edit_load_file_fast (edit
, edit
->filename
);
555 /* If fast load was used, the number of lines wasn't calculated */
556 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
559 if (*edit
->filename
) {
560 edit
->stack_disable
= 1;
561 if (!edit_insert_file (edit
, edit
->filename
)) {
565 edit
->stack_disable
= 0;
571 /* Restore saved cursor position in the file */
573 edit_load_position (WEdit
*edit
)
578 if (!edit
->filename
|| !*edit
->filename
)
581 filename
= vfs_canon (edit
->filename
);
582 load_file_position (filename
, &line
, &column
);
585 edit_move_to_line (edit
, line
- 1);
586 edit
->prev_col
= column
;
587 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
588 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
591 /* Save cursor position in the file */
593 edit_save_position (WEdit
*edit
)
597 if (!edit
->filename
|| !*edit
->filename
)
600 filename
= vfs_canon (edit
->filename
);
601 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
605 /* Clean the WEdit stricture except the widget part */
607 edit_purge_widget (WEdit
*edit
)
609 int len
= sizeof (WEdit
) - sizeof (Widget
);
610 char *start
= (char *) edit
+ sizeof (Widget
);
611 memset (start
, 0, len
);
612 edit
->macro_i
= -1; /* not recording a macro */
615 #define space_width 1
618 * Fill in the edit structure. Return NULL on failure. Pass edit as
619 * NULL to allocate a new structure.
621 * If line is 0, try to restore saved position. Otherwise put the
622 * cursor on that line and show it in the middle of the screen.
625 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
629 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
634 * Expand option_whole_chars_search by national letters using
638 static char option_whole_chars_search_buf
[256];
640 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
642 size_t len
= str_term_width1 (option_whole_chars_search
);
644 strcpy (option_whole_chars_search_buf
,
645 option_whole_chars_search
);
647 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
648 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
649 option_whole_chars_search_buf
[len
++] = i
;
653 option_whole_chars_search_buf
[len
] = 0;
654 option_whole_chars_search
= option_whole_chars_search_buf
;
656 #endif /* ENABLE_NLS */
657 edit
= g_malloc0 (sizeof (WEdit
));
660 edit_purge_widget (edit
);
661 edit
->num_widget_lines
= lines
;
662 edit
->num_widget_columns
= columns
;
663 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
664 edit
->stat1
.st_uid
= getuid ();
665 edit
->stat1
.st_gid
= getgid ();
666 edit
->stat1
.st_mtime
= 0;
668 edit
->force
|= REDRAW_PAGE
;
669 edit_set_filename (edit
, filename
);
670 edit
->stack_size
= START_STACK_SIZE
;
671 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
672 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
673 if (edit_load_file (edit
)) {
674 /* edit_load_file already gives an error message */
679 edit
->loading_done
= 1;
682 edit_load_syntax (edit
, 0, 0);
685 edit_get_syntax_color (edit
, -1, &color
);
688 /* load saved cursor position */
689 if ((line
== 0) && option_save_position
) {
690 edit_load_position (edit
);
694 edit_move_display (edit
, line
- 1);
695 edit_move_to_line (edit
, line
- 1);
698 edit_load_user_map(edit
);
703 /* Clear the edit struct, freeing everything in it. Return 1 on success */
705 edit_clean (WEdit
*edit
)
712 /* a stale lock, remove it */
714 edit
->locked
= edit_unlock_file (edit
->filename
);
716 /* save cursor position */
717 if (option_save_position
)
718 edit_save_position (edit
);
720 /* File specified on the mcedit command line and never saved */
721 if (edit
->delete_file
)
722 unlink (edit
->filename
);
724 edit_free_syntax_rules (edit
);
725 book_mark_flush (edit
, -1);
726 for (; j
<= MAXBUFF
; j
++) {
727 g_free (edit
->buffers1
[j
]);
728 g_free (edit
->buffers2
[j
]);
731 g_free (edit
->undo_stack
);
732 g_free (edit
->filename
);
735 edit_purge_widget (edit
);
741 /* returns 1 on success */
742 int edit_renew (WEdit
* edit
)
744 int lines
= edit
->num_widget_lines
;
745 int columns
= edit
->num_widget_columns
;
749 if (!edit_init (edit
, lines
, columns
, "", 0))
755 * Load a new file into the editor. If it fails, preserve the old file.
756 * To do it, allocate a new widget, initialize it and, if the new file
757 * was loaded, copy the data to the old widget.
758 * Return 1 on success, 0 on failure.
761 edit_reload (WEdit
*edit
, const char *filename
)
764 int lines
= edit
->num_widget_lines
;
765 int columns
= edit
->num_widget_columns
;
767 e
= g_malloc0 (sizeof (WEdit
));
768 e
->widget
= edit
->widget
;
769 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
774 memcpy (edit
, e
, sizeof (WEdit
));
780 * Load a new file into the editor and set line. If it fails, preserve the old file.
781 * To do it, allocate a new widget, initialize it and, if the new file
782 * was loaded, copy the data to the old widget.
783 * Return 1 on success, 0 on failure.
786 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
789 int lines
= edit
->num_widget_lines
;
790 int columns
= edit
->num_widget_columns
;
792 e
= g_malloc0 (sizeof (WEdit
));
793 e
->widget
= edit
->widget
;
794 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
799 memcpy (edit
, e
, sizeof (WEdit
));
806 Recording stack for undo:
807 The following is an implementation of a compressed stack. Identical
808 pushes are recorded by a negative prefix indicating the number of times the
809 same char was pushed. This saves space for repeated curs-left or curs-right
826 If the stack long int is 0-255 it represents a normal insert (from a backspace),
827 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
828 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
829 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
832 The only way the cursor moves or the buffer is changed is through the routines:
833 insert, backspace, insert_ahead, delete, and cursor_move.
834 These record the reverse undo movements onto the stack each time they are
837 Each key press results in a set of actions (insert; delete ...). So each time
838 a key is pressed the current position of start_display is pushed as
839 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
840 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
841 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
845 void edit_push_action (WEdit
* edit
, long c
,...)
847 unsigned long sp
= edit
->stack_pointer
;
851 /* first enlarge the stack if necessary */
852 if (sp
> edit
->stack_size
- 10) { /* say */
853 if (option_max_undo
< 256)
854 option_max_undo
= 256;
855 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
856 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
858 edit
->undo_stack
= t
;
859 edit
->stack_size
<<= 1;
860 edit
->stack_size_mask
= edit
->stack_size
- 1;
864 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
865 if (edit
->stack_disable
)
868 #ifdef FAST_MOVE_CURSOR
869 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
871 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
872 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
874 c
= -(va_arg (ap
, int));
877 #endif /* ! FAST_MOVE_CURSOR */
878 if (edit
->stack_bottom
!= sp
879 && spm1
!= edit
->stack_bottom
880 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
882 if (edit
->undo_stack
[spm1
] < 0) {
883 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
885 if (edit
->undo_stack
[spm1
] > -1000000000) {
886 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
887 edit
->undo_stack
[spm1
]--;
891 /* #define NO_STACK_CURSMOVE_ANIHILATION */
892 #ifndef NO_STACK_CURSMOVE_ANIHILATION
893 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
894 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
895 if (edit
->undo_stack
[spm1
] == -2)
896 edit
->stack_pointer
= spm1
;
898 edit
->undo_stack
[spm1
]++;
903 d
= edit
->undo_stack
[spm1
];
906 return; /* --> no need to push multiple do-nothings */
907 edit
->undo_stack
[sp
] = -2;
910 #ifndef NO_STACK_CURSMOVE_ANIHILATION
911 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
912 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
913 edit
->stack_pointer
= spm1
;
919 edit
->undo_stack
[sp
] = c
;
922 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
924 /* if the sp wraps round and catches the stack_bottom then erase
925 * the first set of actions on the stack to make space - by moving
926 * stack_bottom forward one "key press" */
927 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
928 if ((unsigned long) c
== edit
->stack_bottom
||
929 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
931 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
932 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
934 /*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: */
935 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
936 edit
->stack_bottom
= edit
->stack_pointer
= 0;
940 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
941 then the file should be as it was when he loaded up. Then set edit->modified to 0.
944 pop_action (WEdit
* edit
)
947 unsigned long sp
= edit
->stack_pointer
;
948 if (sp
== edit
->stack_bottom
) {
951 sp
= (sp
- 1) & edit
->stack_size_mask
;
952 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
953 /* edit->undo_stack[sp] = '@'; */
954 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
957 if (sp
== edit
->stack_bottom
) {
960 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
961 if (edit
->undo_stack
[sp
] == -2) {
962 /* edit->undo_stack[sp] = '@'; */
963 edit
->stack_pointer
= sp
;
965 edit
->undo_stack
[sp
]++;
970 /* is called whenever a modification is made by one of the four routines below */
971 static inline void edit_modification (WEdit
* edit
)
973 edit
->caches_valid
= 0;
974 edit
->screen_modified
= 1;
976 /* raise lock when file modified */
977 if (!edit
->modified
&& !edit
->delete_file
)
978 edit
->locked
= edit_lock_file (edit
->filename
);
983 Basic low level single character buffer alterations and movements at the cursor.
984 Returns char passed over, inserted or removed.
988 edit_insert (WEdit
*edit
, int c
)
990 /* check if file has grown to large */
991 if (edit
->last_byte
>= SIZE_LIMIT
)
994 /* first we must update the position of the display window */
995 if (edit
->curs1
< edit
->start_display
) {
996 edit
->start_display
++;
1001 /* Mark file as modified, unless the file hasn't been fully loaded */
1002 if (edit
->loading_done
) {
1003 edit_modification (edit
);
1006 /* now we must update some info on the file and check if a redraw is required */
1008 if (edit
->book_mark
)
1009 book_mark_inc (edit
, edit
->curs_line
);
1011 edit
->total_lines
++;
1012 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1015 /* save the reverse command onto the undo stack */
1016 edit_push_action (edit
, BACKSPACE
);
1018 /* update markers */
1019 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1020 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1021 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1023 /* add a new buffer if we've reached the end of the last one */
1024 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1025 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1026 g_malloc (EDIT_BUF_SIZE
);
1028 /* perform the insertion */
1029 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1030 curs1
& M_EDIT_BUF_SIZE
]
1031 = (unsigned char) c
;
1033 /* update file length */
1036 /* update cursor position */
1041 /* same as edit_insert and move left */
1042 void edit_insert_ahead (WEdit
* edit
, int c
)
1044 if (edit
->last_byte
>= SIZE_LIMIT
)
1046 if (edit
->curs1
< edit
->start_display
) {
1047 edit
->start_display
++;
1051 edit_modification (edit
);
1053 if (edit
->book_mark
)
1054 book_mark_inc (edit
, edit
->curs_line
);
1055 edit
->total_lines
++;
1056 edit
->force
|= REDRAW_AFTER_CURSOR
;
1058 edit_push_action (edit
, DELCHAR
);
1060 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1061 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1062 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1064 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1065 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1066 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1073 int edit_delete (WEdit
* edit
, const int byte_delete
)
1081 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1082 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1083 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1086 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1087 if ( edit
->utf8
&& byte_delete
== 0 ) {
1088 edit_get_utf (edit
, edit
->curs1
, &cw
);
1092 for ( int i
= 1; i
<= cw
; i
++ ) {
1093 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1095 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1096 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1097 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1103 edit_modification (edit
);
1105 if (edit
->book_mark
)
1106 book_mark_dec (edit
, edit
->curs_line
);
1107 edit
->total_lines
--;
1108 edit
->force
|= REDRAW_AFTER_CURSOR
;
1110 edit_push_action (edit
, p
+ 256);
1111 if (edit
->curs1
< edit
->start_display
) {
1112 edit
->start_display
--;
1122 edit_backspace (WEdit
* edit
)
1130 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1131 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1132 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1136 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1140 for ( int i
= 1; i
<= cw
; i
++ ) {
1141 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1142 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1143 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1144 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1149 edit_modification (edit
);
1151 if (edit
->book_mark
)
1152 book_mark_dec (edit
, edit
->curs_line
);
1154 edit
->total_lines
--;
1155 edit
->force
|= REDRAW_AFTER_CURSOR
;
1157 edit_push_action (edit
, p
);
1159 if (edit
->curs1
< edit
->start_display
) {
1160 edit
->start_display
--;
1168 #ifdef FAST_MOVE_CURSOR
1170 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1173 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1175 next
-= (unsigned long) dest
;
1183 edit_move_backward_lots (WEdit
*edit
, long increment
)
1188 if (increment
> edit
->curs1
)
1189 increment
= edit
->curs1
;
1192 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1194 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1197 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1202 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1203 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1208 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1209 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1210 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1211 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1214 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1215 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1216 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1221 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1223 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1225 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1226 g_malloc (EDIT_BUF_SIZE
);
1231 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1241 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1243 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1247 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1248 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1251 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1253 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1254 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1259 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1261 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1263 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1264 g_malloc (EDIT_BUF_SIZE
);
1269 return edit_get_byte (edit
, edit
->curs1
);
1272 #endif /* ! FAST_MOVE_CURSOR */
1274 /* moves the cursor right or left: increment positive or negative respectively */
1275 void edit_cursor_move (WEdit
* edit
, long increment
)
1277 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1279 long curs1
= edit
->curs1
;
1280 long curs2
= edit
->curs2
;
1285 if ( increment
== -1 || increment
== 1)
1288 #ifdef FAST_MOVE_CURSOR
1289 if (increment
< -256) {
1290 edit
->force
|= REDRAW_PAGE
;
1291 edit_move_backward_lots (edit
, -increment
);
1294 #endif /* ! FAST_MOVE_CURSOR */
1296 if (increment
< 0) {
1297 for (; increment
< 0; increment
++) {
1301 edit_push_action (edit
, CURS_RIGHT
);
1304 if ( edit
->utf8
&& char_step
) {
1305 edit_get_prev_utf (edit
, curs1
, &cw
);
1309 for ( int i
= 1; i
<= cw
; i
++ ) {
1310 c
= edit_get_byte (edit
, curs1
- 1);
1311 if (!((curs2
+ 1) & M_EDIT_BUF_SIZE
))
1312 edit
->buffers2
[(curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1313 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1315 c
= edit
->buffers1
[(curs1
- 1) >> S_EDIT_BUF_SIZE
][(curs1
- 1) & M_EDIT_BUF_SIZE
];
1316 if (!((curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1317 g_free (edit
->buffers1
[curs1
>> S_EDIT_BUF_SIZE
]);
1318 edit
->buffers1
[curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1322 edit
->curs1
= curs1
;
1323 edit
->curs2
= curs2
;
1326 edit
->force
|= REDRAW_LINE_BELOW
;
1330 } else if (increment
> 0) {
1331 for (; increment
> 0; increment
--) {
1335 edit_push_action (edit
, CURS_LEFT
);
1338 if ( edit
->utf8
&& char_step
) {
1339 edit_get_utf (edit
, curs1
, &cw
);
1343 for ( int i
= 1; i
<= cw
; i
++ ) {
1344 c
= edit_get_byte (edit
, curs1
);
1345 if (!(curs1
& M_EDIT_BUF_SIZE
))
1346 edit
->buffers1
[curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1347 edit
->buffers1
[curs1
>> S_EDIT_BUF_SIZE
][curs1
& M_EDIT_BUF_SIZE
] = c
;
1349 c
= edit
->buffers2
[(curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1350 if (!(curs2
& M_EDIT_BUF_SIZE
)) {
1351 g_free (edit
->buffers2
[curs2
>> S_EDIT_BUF_SIZE
]);
1352 edit
->buffers2
[curs2
>> S_EDIT_BUF_SIZE
] = 0;
1356 edit
->curs1
= curs1
;
1357 edit
->curs2
= curs2
;
1360 edit
->force
|= REDRAW_LINE_ABOVE
;
1366 /* These functions return positions relative to lines */
1368 /* returns index of last char on line + 1 */
1369 long edit_eol (WEdit
* edit
, long current
)
1371 if (current
< edit
->last_byte
) {
1373 if (edit_get_byte (edit
, current
) == '\n')
1376 return edit
->last_byte
;
1380 /* returns index of first char on line */
1381 long edit_bol (WEdit
* edit
, long current
)
1385 if (edit_get_byte (edit
, current
- 1) == '\n')
1393 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1396 if (upto
> edit
->last_byte
)
1397 upto
= edit
->last_byte
;
1400 while (current
< upto
)
1401 if (edit_get_byte (edit
, current
++) == '\n')
1407 /* If lines is zero this returns the count of lines from current to upto. */
1408 /* If upto is zero returns index of lines forward current. */
1409 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1412 return edit_count_lines (edit
, current
, upto
);
1418 next
= edit_eol (edit
, current
) + 1;
1419 if (next
> edit
->last_byte
)
1429 /* Returns offset of 'lines' lines up from current */
1430 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1434 current
= edit_bol (edit
, current
);
1435 while((lines
--) && current
!= 0)
1436 current
= edit_bol (edit
, current
- 1);
1440 /* If cols is zero this returns the count of columns from current to upto. */
1441 /* If upto is zero returns index of cols across from current. */
1442 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1451 q
= edit
->last_byte
+ 2;
1453 for (col
= 0, p
= current
; p
< q
; p
++) {
1461 if ( !edit
->utf8
) {
1462 c
= edit_get_byte (edit
, p
);
1465 c
= edit_get_byte (edit
, p
);
1466 edit_get_utf (edit
, p
, &cw
);
1469 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1470 else if (c
== '\n') {
1475 } else if (c
< 32 || c
== 127)
1476 col
+= 2; /* Caret notation for control characters */
1485 /* returns the current column position of the cursor */
1486 int edit_get_col (WEdit
* edit
)
1488 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1492 /* Scrolling functions */
1494 void edit_update_curs_row (WEdit
* edit
)
1496 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1499 void edit_update_curs_col (WEdit
* edit
)
1501 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1504 /*moves the display start position up by i lines */
1505 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1507 unsigned long lines_above
= edit
->start_line
;
1508 if (i
> lines_above
)
1511 edit
->start_line
-= i
;
1512 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1513 edit
->force
|= REDRAW_PAGE
;
1514 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1516 edit_update_curs_row (edit
);
1520 /* returns 1 if could scroll, 0 otherwise */
1521 void edit_scroll_downward (WEdit
* edit
, int i
)
1524 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1525 if (lines_below
> 0) {
1526 if (i
> lines_below
)
1528 edit
->start_line
+= i
;
1529 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1530 edit
->force
|= REDRAW_PAGE
;
1531 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1533 edit_update_curs_row (edit
);
1536 void edit_scroll_right (WEdit
* edit
, int i
)
1538 edit
->force
|= REDRAW_PAGE
;
1539 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1540 edit
->start_col
-= i
;
1543 void edit_scroll_left (WEdit
* edit
, int i
)
1545 if (edit
->start_col
) {
1546 edit
->start_col
+= i
;
1547 if (edit
->start_col
> 0)
1548 edit
->start_col
= 0;
1549 edit
->force
|= REDRAW_PAGE
;
1550 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1554 /* high level cursor movement commands */
1556 static int is_in_indent (WEdit
*edit
)
1558 long p
= edit_bol (edit
, edit
->curs1
);
1559 while (p
< edit
->curs1
)
1560 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1565 static int left_of_four_spaces (WEdit
*edit
);
1568 edit_move_to_prev_col (WEdit
* edit
, long p
)
1570 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1572 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1573 edit_update_curs_col (edit
);
1575 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1576 int q
= edit
->curs_col
;
1577 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1578 p
= edit_bol (edit
, edit
->curs1
);
1579 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1580 if (!left_of_four_spaces (edit
))
1581 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1588 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1590 unsigned long p
, l
= edit
->curs_line
;
1596 edit
->force
|= REDRAW_PAGE
;
1598 edit_scroll_upward (edit
, i
);
1600 p
= edit_bol (edit
, edit
->curs1
);
1601 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1602 edit_move_to_prev_col (edit
, p
);
1604 edit
->search_start
= edit
->curs1
;
1605 edit
->found_len
= 0;
1610 is_blank (WEdit
*edit
, long offset
)
1614 s
= edit_bol (edit
, offset
);
1615 f
= edit_eol (edit
, offset
) - 1;
1617 c
= edit_get_byte (edit
, s
++);
1625 /* returns the offset of line i */
1627 edit_find_line (WEdit
*edit
, int line
)
1631 if (!edit
->caches_valid
) {
1632 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1633 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1634 /* three offsets that we *know* are line 0 at 0 and these two: */
1635 edit
->line_numbers
[1] = edit
->curs_line
;
1636 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1637 edit
->line_numbers
[2] = edit
->total_lines
;
1638 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1639 edit
->caches_valid
= 1;
1641 if (line
>= edit
->total_lines
)
1642 return edit
->line_offsets
[2];
1645 /* find the closest known point */
1646 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1648 n
= abs (edit
->line_numbers
[i
] - line
);
1655 return edit
->line_offsets
[j
]; /* know the offset exactly */
1656 if (m
== 1 && j
>= 3)
1657 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1659 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1660 if (line
> edit
->line_numbers
[j
])
1661 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1663 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1664 edit
->line_numbers
[i
] = line
;
1665 return edit
->line_offsets
[i
];
1668 int line_is_blank (WEdit
* edit
, long line
)
1670 return is_blank (edit
, edit_find_line (edit
, line
));
1673 /* moves up until a blank line is reached, or until just
1674 before a non-blank line is reached */
1675 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1678 if (edit
->curs_line
<= 1) {
1681 if (line_is_blank (edit
, edit
->curs_line
)) {
1682 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1683 for (i
= edit
->curs_line
- 1; i
; i
--)
1684 if (!line_is_blank (edit
, i
)) {
1689 for (i
= edit
->curs_line
- 1; i
; i
--)
1690 if (line_is_blank (edit
, i
))
1694 for (i
= edit
->curs_line
- 1; i
; i
--)
1695 if (line_is_blank (edit
, i
))
1699 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1703 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1705 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1711 edit
->force
|= REDRAW_PAGE
;
1713 edit_scroll_downward (edit
, i
);
1714 p
= edit_bol (edit
, edit
->curs1
);
1715 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1716 edit_move_to_prev_col (edit
, p
);
1718 edit
->search_start
= edit
->curs1
;
1719 edit
->found_len
= 0;
1723 /* moves down until a blank line is reached, or until just
1724 before a non-blank line is reached */
1725 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1728 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1729 i
= edit
->total_lines
;
1731 if (line_is_blank (edit
, edit
->curs_line
)) {
1732 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1733 for (i
= edit
->curs_line
+ 1; i
; i
++)
1734 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1739 for (i
= edit
->curs_line
+ 1; i
; i
++)
1740 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1744 for (i
= edit
->curs_line
+ 1; i
; i
++)
1745 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1749 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1752 static void edit_begin_page (WEdit
*edit
)
1754 edit_update_curs_row (edit
);
1755 edit_move_up (edit
, edit
->curs_row
, 0);
1758 static void edit_end_page (WEdit
*edit
)
1760 edit_update_curs_row (edit
);
1761 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1765 /* goto beginning of text */
1766 static void edit_move_to_top (WEdit
* edit
)
1768 if (edit
->curs_line
) {
1769 edit_cursor_move (edit
, -edit
->curs1
);
1770 edit_move_to_prev_col (edit
, 0);
1771 edit
->force
|= REDRAW_PAGE
;
1772 edit
->search_start
= 0;
1773 edit_update_curs_row(edit
);
1778 /* goto end of text */
1779 static void edit_move_to_bottom (WEdit
* edit
)
1781 if (edit
->curs_line
< edit
->total_lines
) {
1782 edit_cursor_move (edit
, edit
->curs2
);
1783 edit
->start_display
= edit
->last_byte
;
1784 edit
->start_line
= edit
->total_lines
;
1785 edit_update_curs_row(edit
);
1786 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1787 edit
->force
|= REDRAW_PAGE
;
1791 /* goto beginning of line */
1792 static void edit_cursor_to_bol (WEdit
* edit
)
1794 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1795 edit
->search_start
= edit
->curs1
;
1796 edit
->prev_col
= edit_get_col (edit
);
1799 /* goto end of line */
1800 static void edit_cursor_to_eol (WEdit
* edit
)
1802 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1803 edit
->search_start
= edit
->curs1
;
1804 edit
->prev_col
= edit_get_col (edit
);
1807 /* move cursor to line 'line' */
1808 void edit_move_to_line (WEdit
* e
, long line
)
1810 if(line
< e
->curs_line
)
1811 edit_move_up (e
, e
->curs_line
- line
, 0);
1813 edit_move_down (e
, line
- e
->curs_line
, 0);
1814 edit_scroll_screen_over_cursor (e
);
1817 /* scroll window so that first visible line is 'line' */
1818 void edit_move_display (WEdit
* e
, long line
)
1820 if(line
< e
->start_line
)
1821 edit_scroll_upward (e
, e
->start_line
- line
);
1823 edit_scroll_downward (e
, line
- e
->start_line
);
1826 /* save markers onto undo stack */
1827 void edit_push_markers (WEdit
* edit
)
1829 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1830 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1833 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1842 /* highlight marker toggle */
1843 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1845 edit_push_markers (edit
);
1847 edit_set_markers (edit
, 0, 0, 0, 0);
1848 edit
->force
|= REDRAW_PAGE
;
1850 if (edit
->mark2
>= 0) {
1851 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1852 edit
->force
|= REDRAW_PAGE
;
1854 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1858 static unsigned long
1863 const char option_chars_move_whole_word
[] =
1864 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1869 if (*option_chars_move_whole_word
== '!')
1871 return 0x80000000UL
;
1873 if (g_ascii_isupper ((gchar
) c
))
1875 else if (g_ascii_islower ((gchar
) c
))
1877 else if (g_ascii_isalpha (c
))
1879 else if (isdigit (c
))
1881 else if (isspace (c
))
1883 q
= strchr (option_chars_move_whole_word
, c
);
1885 return 0xFFFFFFFFUL
;
1887 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1891 } while ((q
= strchr (q
+ 1, c
)));
1896 edit_left_word_move (WEdit
*edit
, int s
)
1900 edit_cursor_move (edit
, -1);
1903 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1904 c2
= edit_get_byte (edit
, edit
->curs1
);
1905 if (!(my_type_of (c1
) & my_type_of (c2
)))
1907 if (isspace (c1
) && !isspace (c2
))
1910 if (!isspace (c1
) && isspace (c2
))
1915 static void edit_left_word_move_cmd (WEdit
* edit
)
1917 edit_left_word_move (edit
, 0);
1918 edit
->force
|= REDRAW_PAGE
;
1922 edit_right_word_move (WEdit
*edit
, int s
)
1926 edit_cursor_move (edit
, 1);
1927 if (edit
->curs1
>= edit
->last_byte
)
1929 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1930 c2
= edit_get_byte (edit
, edit
->curs1
);
1931 if (!(my_type_of (c1
) & my_type_of (c2
)))
1933 if (isspace (c1
) && !isspace (c2
))
1936 if (!isspace (c1
) && isspace (c2
))
1941 static void edit_right_word_move_cmd (WEdit
* edit
)
1943 edit_right_word_move (edit
, 0);
1944 edit
->force
|= REDRAW_PAGE
;
1948 static void edit_right_delete_word (WEdit
* edit
)
1952 if (edit
->curs1
>= edit
->last_byte
)
1954 c1
= edit_delete (edit
, 1);
1955 c2
= edit_get_byte (edit
, edit
->curs1
);
1956 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1958 if (!(my_type_of (c1
) & my_type_of (c2
)))
1963 static void edit_left_delete_word (WEdit
* edit
)
1967 if (edit
->curs1
<= 0)
1969 c1
= edit_backspace (edit
);
1970 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1971 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1973 if (!(my_type_of (c1
) & my_type_of (c2
)))
1979 the start column position is not recorded, and hence does not
1980 undo as it happed. But who would notice.
1983 edit_do_undo (WEdit
* edit
)
1988 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1990 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1995 edit_cursor_move (edit
, 1);
1998 edit_cursor_move (edit
, -1);
2001 edit_backspace (edit
);
2004 edit_delete (edit
, 0);
2007 column_highlighting
= 1;
2010 column_highlighting
= 0;
2013 if (ac
>= 256 && ac
< 512)
2014 edit_insert_ahead (edit
, ac
- 256);
2015 if (ac
>= 0 && ac
< 256)
2016 edit_insert (edit
, ac
);
2018 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2019 edit
->mark1
= ac
- MARK_1
;
2020 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2021 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2022 edit
->mark2
= ac
- MARK_2
;
2023 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2026 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2029 if (edit
->start_display
> ac
- KEY_PRESS
) {
2030 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2031 edit
->force
|= REDRAW_PAGE
;
2032 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2033 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2034 edit
->force
|= REDRAW_PAGE
;
2036 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2037 edit_update_curs_row (edit
);
2040 edit
->stack_disable
= 0;
2043 static void edit_delete_to_line_end (WEdit
* edit
)
2045 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2048 edit_delete (edit
, 1);
2052 static void edit_delete_to_line_begin (WEdit
* edit
)
2054 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2057 edit_backspace (edit
);
2062 edit_delete_line (WEdit
*edit
)
2065 * Delete right part of the line.
2066 * Note that edit_get_byte() returns '\n' when byte position is
2069 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2070 (void) edit_delete (edit
, 1);
2075 * Note that edit_delete() will not corrupt anything if called while
2076 * cursor position is EOF.
2078 (void) edit_delete (edit
, 1);
2081 * Delete left part of the line.
2082 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2084 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2085 (void) edit_backspace (edit
);
2089 static void insert_spaces_tab (WEdit
* edit
, int half
)
2092 edit_update_curs_col (edit
);
2093 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2095 edit_insert (edit
, ' ');
2100 static int is_aligned_on_a_tab (WEdit
* edit
)
2102 edit_update_curs_col (edit
);
2103 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
2104 return 0; /* not alligned on a tab */
2108 static int right_of_four_spaces (WEdit
*edit
)
2111 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2112 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2114 return is_aligned_on_a_tab (edit
);
2118 static int left_of_four_spaces (WEdit
*edit
)
2121 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2122 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2124 return is_aligned_on_a_tab (edit
);
2128 int edit_indent_width (WEdit
* edit
, long p
)
2131 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2133 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2136 void edit_insert_indent (WEdit
* edit
, int indent
)
2138 if (!option_fill_tabs_with_spaces
) {
2139 while (indent
>= TAB_SIZE
) {
2140 edit_insert (edit
, '\t');
2144 while (indent
-- > 0)
2145 edit_insert (edit
, ' ');
2149 edit_auto_indent (WEdit
* edit
)
2154 /* use the previous line as a template */
2155 p
= edit_move_backward (edit
, p
, 1);
2156 /* copy the leading whitespace of the line */
2157 for (;;) { /* no range check - the line _is_ \n-terminated */
2158 c
= edit_get_byte (edit
, p
++);
2159 if (c
!= ' ' && c
!= '\t')
2161 edit_insert (edit
, c
);
2165 static void edit_double_newline (WEdit
* edit
)
2167 edit_insert (edit
, '\n');
2168 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2170 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2172 edit
->force
|= REDRAW_PAGE
;
2173 edit_insert (edit
, '\n');
2176 static void edit_tab_cmd (WEdit
* edit
)
2180 if (option_fake_half_tabs
) {
2181 if (is_in_indent (edit
)) {
2182 /*insert a half tab (usually four spaces) unless there is a
2183 half tab already behind, then delete it and insert a
2185 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2186 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2187 edit_backspace (edit
);
2188 edit_insert (edit
, '\t');
2190 insert_spaces_tab (edit
, 1);
2195 if (option_fill_tabs_with_spaces
) {
2196 insert_spaces_tab (edit
, 0);
2198 edit_insert (edit
, '\t');
2203 static void check_and_wrap_line (WEdit
* edit
)
2206 if (!option_typewriter_wrap
)
2208 edit_update_curs_col (edit
);
2209 if (edit
->curs_col
< option_word_wrap_line_length
)
2214 c
= edit_get_byte (edit
, curs
);
2215 if (c
== '\n' || curs
<= 0) {
2216 edit_insert (edit
, '\n');
2219 if (c
== ' ' || c
== '\t') {
2220 int current
= edit
->curs1
;
2221 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2222 edit_insert (edit
, '\n');
2223 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2229 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2231 void edit_push_key_press (WEdit
* edit
)
2233 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2234 if (edit
->mark2
== -1)
2235 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2238 /* this find the matching bracket in either direction, and sets edit->bracket */
2239 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2241 const char * const b
= "{}{[][()(", *p
;
2242 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2243 unsigned long j
= 0;
2245 edit_update_curs_row (edit
);
2246 c
= edit_get_byte (edit
, edit
->curs1
);
2249 if (!furthest_bracket_search
)
2250 furthest_bracket_search
--;
2251 /* not on a bracket at all */
2254 /* the matching bracket */
2256 /* going left or right? */
2257 if (strchr ("{[(", c
))
2259 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2260 /* out of buffer? */
2261 if (q
>= edit
->last_byte
|| q
< 0)
2263 a
= edit_get_byte (edit
, q
);
2264 /* don't want to eat CPU */
2265 if (j
++ > furthest_bracket_search
)
2267 /* out of screen? */
2269 if (q
< edit
->start_display
)
2271 /* count lines if searching downward */
2272 if (inc
> 0 && a
== '\n')
2273 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2276 /* count bracket depth */
2277 i
+= (a
== c
) - (a
== d
);
2278 /* return if bracket depth is zero */
2286 static long last_bracket
= -1;
2288 void edit_find_bracket (WEdit
* edit
)
2290 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2291 if (last_bracket
!= edit
->bracket
)
2292 edit
->force
|= REDRAW_PAGE
;
2293 last_bracket
= edit
->bracket
;
2296 static void edit_goto_matching_bracket (WEdit
*edit
)
2299 q
= edit_get_bracket (edit
, 0, 0);
2302 edit
->bracket
= edit
->curs1
;
2303 edit
->force
|= REDRAW_PAGE
;
2304 edit_cursor_move (edit
, q
- edit
->curs1
);
2308 * This executes a command as though the user initiated it through a key
2309 * press. Callback with WIDGET_KEY as a message calls this after
2310 * translating the key press. This function can be used to pass any
2311 * command to the editor. Note that the screen wouldn't update
2312 * automatically. Either of command or char_for_insertion must be
2313 * passed as -1. Commands are executed, and char_for_insertion is
2314 * inserted at the cursor.
2316 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2318 if (command
== CK_Begin_Record_Macro
) {
2320 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2323 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2324 edit
->force
|= REDRAW_COMPLETELY
;
2325 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2329 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2330 edit
->macro
[edit
->macro_i
].command
= command
;
2331 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2333 /* record the beginning of a set of editing actions initiated by a key press */
2334 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2335 edit_push_key_press (edit
);
2337 edit_execute_cmd (edit
, command
, char_for_insertion
);
2338 if (column_highlighting
)
2339 edit
->force
|= REDRAW_PAGE
;
2342 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2345 This executes a command at a lower level than macro recording.
2346 It also does not push a key_press onto the undo stack. This means
2347 that if it is called many times, a single undo command will undo
2348 all of them. It also does not check for the Undo command.
2351 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2353 edit
->force
|= REDRAW_LINE
;
2355 /* The next key press will unhighlight the found string, so update
2357 if (edit
->found_len
|| column_highlighting
)
2358 edit
->force
|= REDRAW_PAGE
;
2360 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2361 column_highlighting
= 0;
2362 if (!edit
->highlight
2363 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2364 edit_mark_cmd (edit
, 1); /* clear */
2365 edit_mark_cmd (edit
, 0); /* marking on */
2367 edit
->highlight
= 1;
2368 } else { /* any other command */
2369 if (edit
->highlight
)
2370 edit_mark_cmd (edit
, 0); /* clear */
2371 edit
->highlight
= 0;
2374 /* first check for undo */
2375 if (command
== CK_Undo
) {
2376 edit_do_undo (edit
);
2377 edit
->found_len
= 0;
2378 edit
->prev_col
= edit_get_col (edit
);
2379 edit
->search_start
= edit
->curs1
;
2383 /* An ordinary key press */
2384 if (char_for_insertion
>= 0) {
2385 if (edit
->overwrite
) {
2386 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2387 edit_delete (edit
, 1);
2390 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2391 unsigned char str
[6 + 1];
2392 int res
= g_unichar_to_utf8 (char_for_insertion
, str
);
2400 while ( str
[i
] != 0 && i
<=6) {
2401 char_for_insertion
= str
[i
];
2402 edit_insert (edit
, char_for_insertion
);
2407 edit_insert (edit
, char_for_insertion
);
2411 if (option_auto_para_formatting
) {
2412 format_paragraph (edit
, 0);
2413 edit
->force
|= REDRAW_PAGE
;
2415 check_and_wrap_line (edit
);
2416 edit
->found_len
= 0;
2417 edit
->prev_col
= edit_get_col (edit
);
2418 edit
->search_start
= edit
->curs1
;
2419 edit_find_bracket (edit
);
2426 case CK_Begin_Page_Highlight
:
2427 case CK_End_Page_Highlight
:
2434 if ( edit
->mark2
>= 0 ) {
2435 if ( !option_persistent_selections
) {
2436 if (column_highlighting
)
2437 edit_push_action (edit
, COLUMN_ON
);
2438 column_highlighting
= 0;
2439 edit_mark_cmd (edit
, 1);
2447 case CK_Begin_Page_Highlight
:
2448 case CK_End_Page_Highlight
:
2453 case CK_Word_Left_Highlight
:
2454 case CK_Word_Right_Highlight
:
2455 case CK_Up_Highlight
:
2456 case CK_Down_Highlight
:
2457 if (edit
->mark2
== -1)
2458 break; /*marking is following the cursor: may need to highlight a whole line */
2461 case CK_Left_Highlight
:
2462 case CK_Right_Highlight
:
2463 edit
->force
|= REDRAW_CHAR_ONLY
;
2466 /* basic cursor key commands */
2469 /* if non persistent selection and text selected */
2470 if ( !option_persistent_selections
) {
2471 if ( edit
->mark1
!= edit
->mark2
) {
2472 edit_block_delete_cmd (edit
);
2476 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2477 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2479 edit_backspace (edit
);
2482 if (option_fake_half_tabs
) {
2484 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2485 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2486 edit_backspace (edit
);
2491 edit_backspace (edit
);
2494 /* if non persistent selection and text selected */
2495 if ( !option_persistent_selections
) {
2496 if ( edit
->mark1
!= edit
->mark2
) {
2497 edit_block_delete_cmd (edit
);
2501 if (option_fake_half_tabs
) {
2503 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2504 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2505 edit_delete (edit
, 1);
2509 edit_delete (edit
, 0);
2511 case CK_Delete_Word_Left
:
2512 edit_left_delete_word (edit
);
2514 case CK_Delete_Word_Right
:
2515 edit_right_delete_word (edit
);
2517 case CK_Delete_Line
:
2518 edit_delete_line (edit
);
2520 case CK_Delete_To_Line_End
:
2521 edit_delete_to_line_end (edit
);
2523 case CK_Delete_To_Line_Begin
:
2524 edit_delete_to_line_begin (edit
);
2527 if (option_auto_para_formatting
) {
2528 edit_double_newline (edit
);
2529 if (option_return_does_auto_indent
)
2530 edit_auto_indent (edit
);
2531 format_paragraph (edit
, 0);
2533 edit_insert (edit
, '\n');
2534 if (option_return_does_auto_indent
) {
2535 edit_auto_indent (edit
);
2540 edit_insert (edit
, '\n');
2544 case CK_Page_Up_Highlight
:
2545 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2548 case CK_Page_Down_Highlight
:
2549 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2552 case CK_Left_Highlight
:
2553 if (option_fake_half_tabs
) {
2554 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2555 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2556 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2560 edit_cursor_move (edit
, -1);
2563 case CK_Right_Highlight
:
2564 if (option_fake_half_tabs
) {
2565 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2566 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2567 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2571 edit_cursor_move (edit
, 1);
2574 case CK_Begin_Page_Highlight
:
2575 edit_begin_page (edit
);
2578 case CK_End_Page_Highlight
:
2579 edit_end_page (edit
);
2582 case CK_Word_Left_Highlight
:
2583 edit_left_word_move_cmd (edit
);
2586 case CK_Word_Right_Highlight
:
2587 edit_right_word_move_cmd (edit
);
2590 case CK_Up_Highlight
:
2591 edit_move_up (edit
, 1, 0);
2594 case CK_Down_Highlight
:
2595 edit_move_down (edit
, 1, 0);
2597 case CK_Paragraph_Up
:
2598 case CK_Paragraph_Up_Highlight
:
2599 edit_move_up_paragraph (edit
, 0);
2601 case CK_Paragraph_Down
:
2602 case CK_Paragraph_Down_Highlight
:
2603 edit_move_down_paragraph (edit
, 0);
2606 case CK_Scroll_Up_Highlight
:
2607 edit_move_up (edit
, 1, 1);
2609 case CK_Scroll_Down
:
2610 case CK_Scroll_Down_Highlight
:
2611 edit_move_down (edit
, 1, 1);
2614 case CK_Home_Highlight
:
2615 edit_cursor_to_bol (edit
);
2618 case CK_End_Highlight
:
2619 edit_cursor_to_eol (edit
);
2623 edit_tab_cmd (edit
);
2624 if (option_auto_para_formatting
) {
2625 format_paragraph (edit
, 0);
2626 edit
->force
|= REDRAW_PAGE
;
2628 check_and_wrap_line (edit
);
2631 case CK_Toggle_Insert
:
2632 edit
->overwrite
= (edit
->overwrite
== 0);
2636 if (edit
->mark2
>= 0) {
2637 if (column_highlighting
)
2638 edit_push_action (edit
, COLUMN_ON
);
2639 column_highlighting
= 0;
2641 edit_mark_cmd (edit
, 0);
2643 case CK_Column_Mark
:
2644 if (!column_highlighting
)
2645 edit_push_action (edit
, COLUMN_OFF
);
2646 column_highlighting
= 1;
2647 edit_mark_cmd (edit
, 0);
2650 if (column_highlighting
)
2651 edit_push_action (edit
, COLUMN_ON
);
2652 column_highlighting
= 0;
2653 edit_mark_cmd (edit
, 1);
2656 case CK_Toggle_Bookmark
:
2657 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2658 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2659 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2661 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2663 case CK_Flush_Bookmarks
:
2664 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2665 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2666 edit
->force
|= REDRAW_PAGE
;
2668 case CK_Next_Bookmark
:
2669 if (edit
->book_mark
) {
2670 struct _book_mark
*p
;
2671 p
= (struct _book_mark
*) book_mark_find (edit
,
2675 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2676 || p
->line
< edit
->start_line
)
2677 edit_move_display (edit
,
2679 edit
->num_widget_lines
/ 2);
2680 edit_move_to_line (edit
, p
->line
);
2684 case CK_Prev_Bookmark
:
2685 if (edit
->book_mark
) {
2686 struct _book_mark
*p
;
2687 p
= (struct _book_mark
*) book_mark_find (edit
,
2689 while (p
->line
== edit
->curs_line
)
2693 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2694 || p
->line
< edit
->start_line
)
2695 edit_move_display (edit
,
2697 edit
->num_widget_lines
/ 2);
2698 edit_move_to_line (edit
, p
->line
);
2703 case CK_Beginning_Of_Text
:
2704 case CK_Beginning_Of_Text_Highlight
:
2705 edit_move_to_top (edit
);
2707 case CK_End_Of_Text
:
2708 case CK_End_Of_Text_Highlight
:
2709 edit_move_to_bottom (edit
);
2713 edit_block_copy_cmd (edit
);
2716 edit_block_delete_cmd (edit
);
2719 edit_block_move_cmd (edit
);
2723 edit_copy_to_X_buf_cmd (edit
);
2726 edit_cut_to_X_buf_cmd (edit
);
2729 edit_paste_from_X_buf_cmd (edit
);
2731 case CK_Selection_History
:
2732 edit_paste_from_history (edit
);
2736 edit_save_as_cmd (edit
);
2739 edit_save_confirm_cmd (edit
);
2742 edit_load_cmd (edit
);
2745 edit_save_block_cmd (edit
);
2747 case CK_Insert_File
:
2748 edit_insert_file_cmd (edit
);
2751 case CK_Load_Prev_File
:
2752 edit_load_back_cmd (edit
);
2754 case CK_Load_Next_File
:
2755 edit_load_forward_cmd (edit
);
2758 case CK_Toggle_Syntax
:
2759 if ((option_syntax_highlighting
^= 1) == 1)
2760 edit_load_syntax (edit
, NULL
, option_syntax_type
);
2761 edit
->force
|= REDRAW_PAGE
;
2765 edit_search_cmd (edit
, 0);
2768 edit_search_cmd (edit
, 1);
2771 edit_replace_cmd (edit
, 0);
2773 case CK_Replace_Again
:
2774 edit_replace_cmd (edit
, 1);
2776 case CK_Complete_Word
:
2777 edit_complete_word_cmd (edit
);
2779 case CK_Find_Definition
:
2780 edit_get_match_keyword_cmd (edit
);
2784 dlg_stop (edit
->widget
.parent
);
2787 edit_new_cmd (edit
);
2791 edit_help_cmd (edit
);
2795 edit_refresh_cmd (edit
);
2800 /* fool gcc to prevent a Y2K warning */
2801 char time_format
[] = "_c";
2802 time_format
[0] = '%';
2804 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
2805 edit_print_string (edit
, s
);
2806 edit
->force
|= REDRAW_PAGE
;
2810 edit_goto_cmd (edit
);
2812 case CK_Paragraph_Format
:
2813 format_paragraph (edit
, 1);
2814 edit
->force
|= REDRAW_PAGE
;
2816 case CK_Delete_Macro
:
2817 edit_delete_macro_cmd (edit
);
2819 case CK_Match_Bracket
:
2820 edit_goto_matching_bracket (edit
);
2826 edit_sort_cmd (edit
);
2829 edit_ext_cmd (edit
);
2832 edit_mail_dialog (edit
);
2837 case CK_Select_Codepage
:
2838 edit_select_codepage_cmd (edit
);
2840 case CK_Insert_Literal
:
2841 edit_insert_literal_cmd (edit
);
2843 case CK_Execute_Macro
:
2844 edit_execute_macro_cmd (edit
);
2846 case CK_Begin_End_Macro
:
2847 edit_begin_end_macro_cmd (edit
);
2857 if ((command
/ 1000) == 1) /* a shell command */
2858 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2859 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2860 struct macro m
[MAX_MACRO_LENGTH
];
2862 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
2863 edit_execute_macro (edit
, m
, nm
);
2866 /* keys which must set the col position, and the search vars */
2871 case CK_Replace_Again
:
2872 case CK_Complete_Word
:
2873 edit
->prev_col
= edit_get_col (edit
);
2876 case CK_Up_Highlight
:
2878 case CK_Down_Highlight
:
2880 case CK_Page_Up_Highlight
:
2882 case CK_Page_Down_Highlight
:
2883 case CK_Beginning_Of_Text
:
2884 case CK_Beginning_Of_Text_Highlight
:
2885 case CK_End_Of_Text
:
2886 case CK_End_Of_Text_Highlight
:
2887 case CK_Paragraph_Up
:
2888 case CK_Paragraph_Up_Highlight
:
2889 case CK_Paragraph_Down
:
2890 case CK_Paragraph_Down_Highlight
:
2892 case CK_Scroll_Up_Highlight
:
2893 case CK_Scroll_Down
:
2894 case CK_Scroll_Down_Highlight
:
2895 edit
->search_start
= edit
->curs1
;
2896 edit
->found_len
= 0;
2899 edit
->found_len
= 0;
2900 edit
->prev_col
= edit_get_col (edit
);
2901 edit
->search_start
= edit
->curs1
;
2903 edit_find_bracket (edit
);
2905 if (option_auto_para_formatting
) {
2909 case CK_Delete_Word_Left
:
2910 case CK_Delete_Word_Right
:
2911 case CK_Delete_To_Line_End
:
2912 case CK_Delete_To_Line_Begin
:
2913 format_paragraph (edit
, 0);
2914 edit
->force
|= REDRAW_PAGE
;
2921 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2925 if (edit
->macro_depth
++ > 256) {
2926 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2927 edit
->macro_depth
--;
2930 edit
->force
|= REDRAW_PAGE
;
2931 for (; i
< n
; i
++) {
2932 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2934 edit_update_screen (edit
);
2935 edit
->macro_depth
--;
2938 /* User edit menu, like user menu (F2) but only in editor. */
2940 user_menu (WEdit
* edit
)
2945 long start_mark
, end_mark
;
2946 char *block_file
= concat_dir_and_file (home_dir
, BLOCK_FILE
);
2949 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2950 if (!nomark
) /* remember marked or not */
2951 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2953 /* run shell scripts from menu */
2954 user_menu_cmd (edit
);
2956 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2957 /* no block messages */
2962 /* i.e. we have marked block */
2963 rc
= edit_block_delete_cmd (edit
);
2967 edit_insert_file (edit
, block_file
);
2970 /* truncate block file */
2971 if ((fd
= fopen (block_file
, "w"))) {
2975 edit_refresh_cmd (edit
);
2976 edit
->force
|= REDRAW_COMPLETELY
;
2979 g_free (block_file
);