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 <mhl/string.h>
37 #include "../src/global.h"
41 #include "edit-widget.h"
42 #include "editcmddef.h"
45 #include "../src/cmd.h" /* view_other_cmd() */
46 #include "../src/user.h" /* user_menu_cmd() */
47 #include "../src/wtools.h" /* query_dialog() */
48 #include "../src/timefmt.h" /* time formatting */
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;
69 int option_edit_right_extreme
= 0;
70 int option_edit_left_extreme
= 0;
71 int option_edit_top_extreme
= 0;
72 int option_edit_bottom_extreme
= 0;
74 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
75 char *option_backup_ext
= NULL
;
79 * here's a quick sketch of the layout: (don't run this through indent.)
81 * (b1 is buffers1 and b2 is buffers2)
84 * \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
85 * ______________________________________|______________________________________
87 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
88 * |-> |-> |-> |-> |-> |-> |
90 * _<------------------------->|<----------------->_
91 * WEdit->curs2 | WEdit->curs1
96 * file end|||file beginning
106 static void user_menu (WEdit
*edit
);
108 int edit_get_byte (WEdit
* edit
, long byte_index
)
111 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
114 if (byte_index
>= edit
->curs1
) {
115 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
116 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
118 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
123 * Initialize the buffers for an empty files.
126 edit_init_buffers (WEdit
*edit
)
130 for (j
= 0; j
<= MAXBUFF
; j
++) {
131 edit
->buffers1
[j
] = NULL
;
132 edit
->buffers2
[j
] = NULL
;
137 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
141 * Load file OR text into buffers. Set cursor to the beginning of file.
145 edit_load_file_fast (WEdit
*edit
, const char *filename
)
150 edit
->curs2
= edit
->last_byte
;
151 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
153 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
154 GString
*errmsg
= g_string_new(NULL
);
155 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
156 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
157 g_string_free (errmsg
, TRUE
);
161 if (!edit
->buffers2
[buf2
])
162 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
165 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
166 (edit
->curs2
& M_EDIT_BUF_SIZE
),
167 edit
->curs2
& M_EDIT_BUF_SIZE
);
169 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
170 /* edit->buffers2[0] is already allocated */
171 if (!edit
->buffers2
[buf
])
172 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
173 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
180 /* detecting an error on save is easy: just check if every byte has been written. */
181 /* detecting an error on read, is not so easy 'cos there is not way to tell
182 whether you read everything or not. */
183 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
184 static const struct edit_filters
{
185 const char *read
, *write
, *extension
;
187 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
188 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
189 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
192 /* Return index of the filter or -1 is there is no appropriate filter */
193 static int edit_find_filter (const char *filename
)
198 l
= strlen (filename
);
199 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
200 e
= strlen (all_filters
[i
].extension
);
202 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
209 edit_get_filter (const char *filename
)
212 char *p
, *quoted_name
;
213 i
= edit_find_filter (filename
);
216 quoted_name
= name_quote (filename
, 0);
217 l
= strlen (quoted_name
);
218 p
= g_malloc (strlen (all_filters
[i
].read
) + l
+ 2);
219 sprintf (p
, all_filters
[i
].read
, quoted_name
);
220 g_free (quoted_name
);
225 edit_get_write_filter (const char *write_name
, const char *filename
)
229 i
= edit_find_filter (filename
);
232 writename
= name_quote (write_name
, 0);
233 l
= strlen (writename
);
234 p
= g_malloc (strlen (all_filters
[i
].write
) + l
+ 2);
235 sprintf (p
, all_filters
[i
].write
, writename
);
241 edit_insert_stream (WEdit
* edit
, FILE * f
)
245 while ((c
= fgetc (f
)) >= 0) {
246 edit_insert (edit
, c
);
252 long edit_write_stream (WEdit
* edit
, FILE * f
)
255 for (i
= 0; i
< edit
->last_byte
; i
++)
256 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
261 #define TEMP_BUF_LEN 1024
263 /* inserts a file at the cursor, returns 1 on success */
265 edit_insert_file (WEdit
*edit
, const char *filename
)
268 if ((p
= edit_get_filter (filename
))) {
270 long current
= edit
->curs1
;
271 f
= (FILE *) popen (p
, "r");
273 edit_insert_stream (edit
, f
);
274 edit_cursor_move (edit
, current
- edit
->curs1
);
275 if (pclose (f
) > 0) {
276 GString
*errmsg
= g_string_new (NULL
);
277 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
278 edit_error_dialog (_("Error"), errmsg
->str
);
279 g_string_free (errmsg
, TRUE
);
284 GString
*errmsg
= g_string_new (NULL
);
285 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
286 edit_error_dialog (_("Error"), errmsg
->str
);
287 g_string_free (errmsg
, TRUE
);
293 int i
, file
, blocklen
;
294 long current
= edit
->curs1
;
296 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
298 buf
= g_malloc (TEMP_BUF_LEN
);
299 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
300 for (i
= 0; i
< blocklen
; i
++)
301 edit_insert (edit
, buf
[i
]);
303 edit_cursor_move (edit
, current
- edit
->curs1
);
312 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
314 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
317 GString
*errmsg
= (GString
*) 0;
319 /* Try opening an existing file */
320 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
324 * Try creating the file. O_EXCL prevents following broken links
325 * and opening existing files.
329 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
332 g_string_sprintf (errmsg
= g_string_new (NULL
),
333 _(" Cannot open %s for reading "), filename
);
336 /* New file, delete it if it's not modified or saved */
337 edit
->delete_file
= 1;
341 /* Check what we have opened */
342 if (mc_fstat (file
, st
) < 0) {
343 g_string_sprintf (errmsg
= g_string_new (NULL
),
344 _(" Cannot get size/permissions for %s "), filename
);
348 /* We want to open regular files only */
349 if (!S_ISREG (st
->st_mode
)) {
350 g_string_sprintf (errmsg
= g_string_new (NULL
),
351 _(" %s is not a regular file "), filename
);
356 * Don't delete non-empty files.
357 * O_EXCL should prevent it, but let's be on the safe side.
359 if (st
->st_size
> 0) {
360 edit
->delete_file
= 0;
363 if (st
->st_size
>= SIZE_LIMIT
) {
364 g_string_sprintf (errmsg
= g_string_new (NULL
),
365 _(" File %s is too large "), filename
);
370 (void) mc_close (file
);
372 edit_error_dialog (_("Error"), errmsg
->str
);
373 g_string_free (errmsg
, TRUE
);
380 * Open the file and load it into the buffers, either directly or using
381 * a filter. Return 0 on success, 1 on error.
383 * Fast loading (edit_load_file_fast) is used when the file size is
384 * known. In this case the data is read into the buffers by blocks.
385 * If the file size is not known, the data is loaded byte by byte in
389 edit_load_file (WEdit
*edit
)
393 /* Cannot do fast load if a filter is used */
394 if (edit_find_filter (edit
->filename
) >= 0)
398 * VFS may report file size incorrectly, and slow load is not a big
399 * deal considering overhead in VFS.
401 if (!vfs_file_is_local (edit
->filename
))
405 * FIXME: line end translation should disable fast loading as well
406 * Consider doing fseek() to the end and ftell() for the real size.
409 if (*edit
->filename
) {
410 /* If we are dealing with a real file, check that it exists */
411 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
414 /* nothing to load */
418 edit_init_buffers (edit
);
421 edit
->last_byte
= edit
->stat1
.st_size
;
422 edit_load_file_fast (edit
, edit
->filename
);
423 /* If fast load was used, the number of lines wasn't calculated */
424 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
427 if (*edit
->filename
) {
428 edit
->stack_disable
= 1;
429 if (!edit_insert_file (edit
, edit
->filename
)) {
433 edit
->stack_disable
= 0;
439 /* Restore saved cursor position in the file */
441 edit_load_position (WEdit
*edit
)
446 if (!edit
->filename
|| !*edit
->filename
)
449 filename
= vfs_canon (edit
->filename
);
450 load_file_position (filename
, &line
, &column
);
453 edit_move_to_line (edit
, line
- 1);
454 edit
->prev_col
= column
;
455 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
456 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
459 /* Save cursor position in the file */
461 edit_save_position (WEdit
*edit
)
465 if (!edit
->filename
|| !*edit
->filename
)
468 filename
= vfs_canon (edit
->filename
);
469 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
473 /* Clean the WEdit stricture except the widget part */
475 edit_purge_widget (WEdit
*edit
)
477 int len
= sizeof (WEdit
) - sizeof (Widget
);
478 char *start
= (char *) edit
+ sizeof (Widget
);
479 memset (start
, 0, len
);
480 edit
->macro_i
= -1; /* not recording a macro */
483 #define space_width 1
486 * Fill in the edit structure. Return NULL on failure. Pass edit as
487 * NULL to allocate a new structure.
489 * If line is 0, try to restore saved position. Otherwise put the
490 * cursor on that line and show it in the middle of the screen.
493 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
497 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
502 * Expand option_whole_chars_search by national letters using
506 static char option_whole_chars_search_buf
[256];
508 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
510 size_t len
= strlen (option_whole_chars_search
);
512 strcpy (option_whole_chars_search_buf
,
513 option_whole_chars_search
);
515 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
516 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
517 option_whole_chars_search_buf
[len
++] = i
;
521 option_whole_chars_search_buf
[len
] = 0;
522 option_whole_chars_search
= option_whole_chars_search_buf
;
524 #endif /* ENABLE_NLS */
525 edit
= g_malloc0 (sizeof (WEdit
));
528 edit_purge_widget (edit
);
529 edit
->num_widget_lines
= lines
;
530 edit
->num_widget_columns
= columns
;
531 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
532 edit
->stat1
.st_uid
= getuid ();
533 edit
->stat1
.st_gid
= getgid ();
534 edit
->stat1
.st_mtime
= 0;
536 edit
->force
|= REDRAW_PAGE
;
537 edit_set_filename (edit
, filename
);
538 edit
->stack_size
= START_STACK_SIZE
;
539 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
540 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
541 if (edit_load_file (edit
)) {
542 /* edit_load_file already gives an error message */
547 edit
->loading_done
= 1;
550 edit_load_syntax (edit
, 0, 0);
553 edit_get_syntax_color (edit
, -1, &color
);
556 /* load saved cursor position */
557 if ((line
== 0) && option_save_position
) {
558 edit_load_position (edit
);
562 edit_move_display (edit
, line
- 1);
563 edit_move_to_line (edit
, line
- 1);
566 edit_load_user_map(edit
);
571 /* Clear the edit struct, freeing everything in it. Return 1 on success */
573 edit_clean (WEdit
*edit
)
580 /* a stale lock, remove it */
582 edit
->locked
= edit_unlock_file (edit
->filename
);
584 /* save cursor position */
585 if (option_save_position
)
586 edit_save_position (edit
);
588 /* File specified on the mcedit command line and never saved */
589 if (edit
->delete_file
)
590 unlink (edit
->filename
);
592 edit_free_syntax_rules (edit
);
593 book_mark_flush (edit
, -1);
594 for (; j
<= MAXBUFF
; j
++) {
595 g_free (edit
->buffers1
[j
]);
596 g_free (edit
->buffers2
[j
]);
599 g_free (edit
->undo_stack
);
600 g_free (edit
->filename
);
603 edit_purge_widget (edit
);
605 /* Free temporary strings used in catstrs() */
612 /* returns 1 on success */
613 int edit_renew (WEdit
* edit
)
615 int lines
= edit
->num_widget_lines
;
616 int columns
= edit
->num_widget_columns
;
620 if (!edit_init (edit
, lines
, columns
, "", 0))
626 * Load a new file into the editor. If it fails, preserve the old file.
627 * To do it, allocate a new widget, initialize it and, if the new file
628 * was loaded, copy the data to the old widget.
629 * Return 1 on success, 0 on failure.
632 edit_reload (WEdit
*edit
, const char *filename
)
635 int lines
= edit
->num_widget_lines
;
636 int columns
= edit
->num_widget_columns
;
638 e
= g_malloc0 (sizeof (WEdit
));
639 e
->widget
= edit
->widget
;
640 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
645 memcpy (edit
, e
, sizeof (WEdit
));
652 Recording stack for undo:
653 The following is an implementation of a compressed stack. Identical
654 pushes are recorded by a negative prefix indicating the number of times the
655 same char was pushed. This saves space for repeated curs-left or curs-right
672 If the stack long int is 0-255 it represents a normal insert (from a backspace),
673 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
674 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
675 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
678 The only way the cursor moves or the buffer is changed is through the routines:
679 insert, backspace, insert_ahead, delete, and cursor_move.
680 These record the reverse undo movements onto the stack each time they are
683 Each key press results in a set of actions (insert; delete ...). So each time
684 a key is pressed the current position of start_display is pushed as
685 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
686 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
687 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
691 void edit_push_action (WEdit
* edit
, long c
,...)
693 unsigned long sp
= edit
->stack_pointer
;
697 /* first enlarge the stack if necessary */
698 if (sp
> edit
->stack_size
- 10) { /* say */
699 if (option_max_undo
< 256)
700 option_max_undo
= 256;
701 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
702 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
704 edit
->undo_stack
= t
;
705 edit
->stack_size
<<= 1;
706 edit
->stack_size_mask
= edit
->stack_size
- 1;
710 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
711 if (edit
->stack_disable
)
714 #ifdef FAST_MOVE_CURSOR
715 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
717 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
718 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
720 c
= -(va_arg (ap
, int));
723 #endif /* ! FAST_MOVE_CURSOR */
724 if (edit
->stack_bottom
!= sp
725 && spm1
!= edit
->stack_bottom
726 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
728 if (edit
->undo_stack
[spm1
] < 0) {
729 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
731 if (edit
->undo_stack
[spm1
] > -1000000000) {
732 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
733 edit
->undo_stack
[spm1
]--;
737 /* #define NO_STACK_CURSMOVE_ANIHILATION */
738 #ifndef NO_STACK_CURSMOVE_ANIHILATION
739 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
740 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
741 if (edit
->undo_stack
[spm1
] == -2)
742 edit
->stack_pointer
= spm1
;
744 edit
->undo_stack
[spm1
]++;
749 d
= edit
->undo_stack
[spm1
];
752 return; /* --> no need to push multiple do-nothings */
753 edit
->undo_stack
[sp
] = -2;
756 #ifndef NO_STACK_CURSMOVE_ANIHILATION
757 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
758 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
759 edit
->stack_pointer
= spm1
;
765 edit
->undo_stack
[sp
] = c
;
768 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
770 /* if the sp wraps round and catches the stack_bottom then erase
771 * the first set of actions on the stack to make space - by moving
772 * stack_bottom forward one "key press" */
773 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
774 if ((unsigned long) c
== edit
->stack_bottom
||
775 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
777 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
778 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
780 /*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: */
781 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
782 edit
->stack_bottom
= edit
->stack_pointer
= 0;
786 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
787 then the file should be as it was when he loaded up. Then set edit->modified to 0.
790 pop_action (WEdit
* edit
)
793 unsigned long sp
= edit
->stack_pointer
;
794 if (sp
== edit
->stack_bottom
) {
797 sp
= (sp
- 1) & edit
->stack_size_mask
;
798 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
799 /* edit->undo_stack[sp] = '@'; */
800 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
803 if (sp
== edit
->stack_bottom
) {
806 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
807 if (edit
->undo_stack
[sp
] == -2) {
808 /* edit->undo_stack[sp] = '@'; */
809 edit
->stack_pointer
= sp
;
811 edit
->undo_stack
[sp
]++;
816 /* is called whenever a modification is made by one of the four routines below */
817 static inline void edit_modification (WEdit
* edit
)
819 edit
->caches_valid
= 0;
820 edit
->screen_modified
= 1;
822 /* raise lock when file modified */
823 if (!edit
->modified
&& !edit
->delete_file
)
824 edit
->locked
= edit_lock_file (edit
->filename
);
829 Basic low level single character buffer alterations and movements at the cursor.
830 Returns char passed over, inserted or removed.
834 edit_insert (WEdit
*edit
, int c
)
836 /* check if file has grown to large */
837 if (edit
->last_byte
>= SIZE_LIMIT
)
840 /* first we must update the position of the display window */
841 if (edit
->curs1
< edit
->start_display
) {
842 edit
->start_display
++;
847 /* Mark file as modified, unless the file hasn't been fully loaded */
848 if (edit
->loading_done
) {
849 edit_modification (edit
);
852 /* now we must update some info on the file and check if a redraw is required */
855 book_mark_inc (edit
, edit
->curs_line
);
858 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
861 /* save the reverse command onto the undo stack */
862 edit_push_action (edit
, BACKSPACE
);
865 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
866 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
867 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
869 /* add a new buffer if we've reached the end of the last one */
870 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
871 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
872 g_malloc (EDIT_BUF_SIZE
);
874 /* perform the insertion */
875 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
876 curs1
& M_EDIT_BUF_SIZE
]
879 /* update file length */
882 /* update cursor position */
887 /* same as edit_insert and move left */
888 void edit_insert_ahead (WEdit
* edit
, int c
)
890 if (edit
->last_byte
>= SIZE_LIMIT
)
892 if (edit
->curs1
< edit
->start_display
) {
893 edit
->start_display
++;
897 edit_modification (edit
);
900 book_mark_inc (edit
, edit
->curs_line
);
902 edit
->force
|= REDRAW_AFTER_CURSOR
;
904 edit_push_action (edit
, DELCHAR
);
906 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
907 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
908 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
910 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
911 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
912 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
919 int edit_delete (WEdit
* edit
)
925 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
926 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
927 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
929 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
931 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
932 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
933 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
938 edit_modification (edit
);
941 book_mark_dec (edit
, edit
->curs_line
);
943 edit
->force
|= REDRAW_AFTER_CURSOR
;
945 edit_push_action (edit
, p
+ 256);
946 if (edit
->curs1
< edit
->start_display
) {
947 edit
->start_display
--;
957 edit_backspace (WEdit
* edit
)
963 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
964 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
965 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
967 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
968 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
969 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
970 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
975 edit_modification (edit
);
978 book_mark_dec (edit
, edit
->curs_line
);
981 edit
->force
|= REDRAW_AFTER_CURSOR
;
983 edit_push_action (edit
, p
);
985 if (edit
->curs1
< edit
->start_display
) {
986 edit
->start_display
--;
994 #ifdef FAST_MOVE_CURSOR
996 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
999 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1001 next
-= (unsigned long) dest
;
1009 edit_move_backward_lots (WEdit
*edit
, long increment
)
1014 if (increment
> edit
->curs1
)
1015 increment
= edit
->curs1
;
1018 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1020 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1023 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1028 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1029 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1034 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1035 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1036 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1037 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1040 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1041 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1042 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1047 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1049 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1051 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1052 g_malloc (EDIT_BUF_SIZE
);
1057 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1067 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1069 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1073 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1074 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1077 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1079 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1080 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1085 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1087 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1089 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1090 g_malloc (EDIT_BUF_SIZE
);
1095 return edit_get_byte (edit
, edit
->curs1
);
1098 #endif /* ! FAST_MOVE_CURSOR */
1100 /* moves the cursor right or left: increment positive or negative respectively */
1101 void edit_cursor_move (WEdit
* edit
, long increment
)
1103 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1106 #ifdef FAST_MOVE_CURSOR
1107 if (increment
< -256) {
1108 edit
->force
|= REDRAW_PAGE
;
1109 edit_move_backward_lots (edit
, -increment
);
1112 #endif /* ! FAST_MOVE_CURSOR */
1114 if (increment
< 0) {
1115 for (; increment
< 0; increment
++) {
1119 edit_push_action (edit
, CURS_RIGHT
);
1121 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1122 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1123 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1124 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1126 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1127 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1128 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1129 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1134 edit
->force
|= REDRAW_LINE_BELOW
;
1138 } else if (increment
> 0) {
1139 for (; increment
> 0; increment
--) {
1143 edit_push_action (edit
, CURS_LEFT
);
1145 c
= edit_get_byte (edit
, edit
->curs1
);
1146 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1147 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1148 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1150 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1151 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1152 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1153 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1158 edit
->force
|= REDRAW_LINE_ABOVE
;
1164 /* These functions return positions relative to lines */
1166 /* returns index of last char on line + 1 */
1167 long edit_eol (WEdit
* edit
, long current
)
1169 if (current
< edit
->last_byte
) {
1171 if (edit_get_byte (edit
, current
) == '\n')
1174 return edit
->last_byte
;
1178 /* returns index of first char on line */
1179 long edit_bol (WEdit
* edit
, long current
)
1183 if (edit_get_byte (edit
, current
- 1) == '\n')
1191 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1194 if (upto
> edit
->last_byte
)
1195 upto
= edit
->last_byte
;
1198 while (current
< upto
)
1199 if (edit_get_byte (edit
, current
++) == '\n')
1205 /* If lines is zero this returns the count of lines from current to upto. */
1206 /* If upto is zero returns index of lines forward current. */
1207 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1210 return edit_count_lines (edit
, current
, upto
);
1216 next
= edit_eol (edit
, current
) + 1;
1217 if (next
> edit
->last_byte
)
1227 /* Returns offset of 'lines' lines up from current */
1228 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1232 current
= edit_bol (edit
, current
);
1233 while((lines
--) && current
!= 0)
1234 current
= edit_bol (edit
, current
- 1);
1238 /* If cols is zero this returns the count of columns from current to upto. */
1239 /* If upto is zero returns index of cols across from current. */
1240 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1249 q
= edit
->last_byte
+ 2;
1251 for (col
= 0, p
= current
; p
< q
; p
++) {
1259 c
= edit_get_byte (edit
, p
);
1261 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1262 else if (c
== '\n') {
1267 } else if (c
< 32 || c
== 127)
1268 col
+= 2; /* Caret notation for control characters */
1275 /* returns the current column position of the cursor */
1276 int edit_get_col (WEdit
* edit
)
1278 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1282 /* Scrolling functions */
1284 void edit_update_curs_row (WEdit
* edit
)
1286 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1289 void edit_update_curs_col (WEdit
* edit
)
1291 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1294 /*moves the display start position up by i lines */
1295 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1297 unsigned long lines_above
= edit
->start_line
;
1298 if (i
> lines_above
)
1301 edit
->start_line
-= i
;
1302 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1303 edit
->force
|= REDRAW_PAGE
;
1304 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1306 edit_update_curs_row (edit
);
1310 /* returns 1 if could scroll, 0 otherwise */
1311 void edit_scroll_downward (WEdit
* edit
, int i
)
1314 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1315 if (lines_below
> 0) {
1316 if (i
> lines_below
)
1318 edit
->start_line
+= i
;
1319 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1320 edit
->force
|= REDRAW_PAGE
;
1321 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1323 edit_update_curs_row (edit
);
1326 void edit_scroll_right (WEdit
* edit
, int i
)
1328 edit
->force
|= REDRAW_PAGE
;
1329 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1330 edit
->start_col
-= i
;
1333 void edit_scroll_left (WEdit
* edit
, int i
)
1335 if (edit
->start_col
) {
1336 edit
->start_col
+= i
;
1337 if (edit
->start_col
> 0)
1338 edit
->start_col
= 0;
1339 edit
->force
|= REDRAW_PAGE
;
1340 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1344 /* high level cursor movement commands */
1346 static int is_in_indent (WEdit
*edit
)
1348 long p
= edit_bol (edit
, edit
->curs1
);
1349 while (p
< edit
->curs1
)
1350 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1355 static int left_of_four_spaces (WEdit
*edit
);
1358 edit_move_to_prev_col (WEdit
* edit
, long p
)
1360 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1362 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1363 edit_update_curs_col (edit
);
1365 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1366 int q
= edit
->curs_col
;
1367 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1368 p
= edit_bol (edit
, edit
->curs1
);
1369 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1370 if (!left_of_four_spaces (edit
))
1371 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1378 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1380 unsigned long p
, l
= edit
->curs_line
;
1386 edit
->force
|= REDRAW_PAGE
;
1388 edit_scroll_upward (edit
, i
);
1390 p
= edit_bol (edit
, edit
->curs1
);
1391 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1392 edit_move_to_prev_col (edit
, p
);
1394 edit
->search_start
= edit
->curs1
;
1395 edit
->found_len
= 0;
1400 is_blank (WEdit
*edit
, long offset
)
1404 s
= edit_bol (edit
, offset
);
1405 f
= edit_eol (edit
, offset
) - 1;
1407 c
= edit_get_byte (edit
, s
++);
1415 /* returns the offset of line i */
1417 edit_find_line (WEdit
*edit
, int line
)
1421 if (!edit
->caches_valid
) {
1422 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1423 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1424 /* three offsets that we *know* are line 0 at 0 and these two: */
1425 edit
->line_numbers
[1] = edit
->curs_line
;
1426 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1427 edit
->line_numbers
[2] = edit
->total_lines
;
1428 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1429 edit
->caches_valid
= 1;
1431 if (line
>= edit
->total_lines
)
1432 return edit
->line_offsets
[2];
1435 /* find the closest known point */
1436 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1438 n
= abs (edit
->line_numbers
[i
] - line
);
1445 return edit
->line_offsets
[j
]; /* know the offset exactly */
1446 if (m
== 1 && j
>= 3)
1447 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1449 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1450 if (line
> edit
->line_numbers
[j
])
1451 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1453 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1454 edit
->line_numbers
[i
] = line
;
1455 return edit
->line_offsets
[i
];
1458 int line_is_blank (WEdit
* edit
, long line
)
1460 return is_blank (edit
, edit_find_line (edit
, line
));
1463 /* moves up until a blank line is reached, or until just
1464 before a non-blank line is reached */
1465 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1468 if (edit
->curs_line
<= 1) {
1471 if (line_is_blank (edit
, edit
->curs_line
)) {
1472 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1473 for (i
= edit
->curs_line
- 1; i
; i
--)
1474 if (!line_is_blank (edit
, i
)) {
1479 for (i
= edit
->curs_line
- 1; i
; i
--)
1480 if (line_is_blank (edit
, i
))
1484 for (i
= edit
->curs_line
- 1; i
; i
--)
1485 if (line_is_blank (edit
, i
))
1489 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1493 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1495 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1501 edit
->force
|= REDRAW_PAGE
;
1503 edit_scroll_downward (edit
, i
);
1504 p
= edit_bol (edit
, edit
->curs1
);
1505 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1506 edit_move_to_prev_col (edit
, p
);
1508 edit
->search_start
= edit
->curs1
;
1509 edit
->found_len
= 0;
1513 /* moves down until a blank line is reached, or until just
1514 before a non-blank line is reached */
1515 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1518 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1519 i
= edit
->total_lines
;
1521 if (line_is_blank (edit
, edit
->curs_line
)) {
1522 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1523 for (i
= edit
->curs_line
+ 1; i
; i
++)
1524 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1529 for (i
= edit
->curs_line
+ 1; i
; i
++)
1530 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1534 for (i
= edit
->curs_line
+ 1; i
; i
++)
1535 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1539 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1542 static void edit_begin_page (WEdit
*edit
)
1544 edit_update_curs_row (edit
);
1545 edit_move_up (edit
, edit
->curs_row
, 0);
1548 static void edit_end_page (WEdit
*edit
)
1550 edit_update_curs_row (edit
);
1551 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1555 /* goto beginning of text */
1556 static void edit_move_to_top (WEdit
* edit
)
1558 if (edit
->curs_line
) {
1559 edit_cursor_move (edit
, -edit
->curs1
);
1560 edit_move_to_prev_col (edit
, 0);
1561 edit
->force
|= REDRAW_PAGE
;
1562 edit
->search_start
= 0;
1563 edit_update_curs_row(edit
);
1568 /* goto end of text */
1569 static void edit_move_to_bottom (WEdit
* edit
)
1571 if (edit
->curs_line
< edit
->total_lines
) {
1572 edit_cursor_move (edit
, edit
->curs2
);
1573 edit
->start_display
= edit
->last_byte
;
1574 edit
->start_line
= edit
->total_lines
;
1575 edit_update_curs_row(edit
);
1576 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1577 edit
->force
|= REDRAW_PAGE
;
1581 /* goto beginning of line */
1582 static void edit_cursor_to_bol (WEdit
* edit
)
1584 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1585 edit
->search_start
= edit
->curs1
;
1586 edit
->prev_col
= edit_get_col (edit
);
1589 /* goto end of line */
1590 static void edit_cursor_to_eol (WEdit
* edit
)
1592 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1593 edit
->search_start
= edit
->curs1
;
1594 edit
->prev_col
= edit_get_col (edit
);
1597 /* move cursor to line 'line' */
1598 void edit_move_to_line (WEdit
* e
, long line
)
1600 if(line
< e
->curs_line
)
1601 edit_move_up (e
, e
->curs_line
- line
, 0);
1603 edit_move_down (e
, line
- e
->curs_line
, 0);
1604 edit_scroll_screen_over_cursor (e
);
1607 /* scroll window so that first visible line is 'line' */
1608 void edit_move_display (WEdit
* e
, long line
)
1610 if(line
< e
->start_line
)
1611 edit_scroll_upward (e
, e
->start_line
- line
);
1613 edit_scroll_downward (e
, line
- e
->start_line
);
1616 /* save markers onto undo stack */
1617 void edit_push_markers (WEdit
* edit
)
1619 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1620 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1623 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1632 /* highlight marker toggle */
1633 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1635 edit_push_markers (edit
);
1637 edit_set_markers (edit
, 0, 0, 0, 0);
1638 edit
->force
|= REDRAW_PAGE
;
1640 if (edit
->mark2
>= 0) {
1641 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1642 edit
->force
|= REDRAW_PAGE
;
1644 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1648 static unsigned long
1653 const char option_chars_move_whole_word
[] =
1654 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1659 if (*option_chars_move_whole_word
== '!')
1661 return 0x80000000UL
;
1665 else if (islower (c
))
1667 else if (isalpha (c
))
1669 else if (isdigit (c
))
1671 else if (isspace (c
))
1673 q
= strchr (option_chars_move_whole_word
, c
);
1675 return 0xFFFFFFFFUL
;
1677 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1681 } while ((q
= strchr (q
+ 1, c
)));
1686 edit_left_word_move (WEdit
*edit
, int s
)
1690 edit_cursor_move (edit
, -1);
1693 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1694 c2
= edit_get_byte (edit
, edit
->curs1
);
1695 if (!(my_type_of (c1
) & my_type_of (c2
)))
1697 if (isspace (c1
) && !isspace (c2
))
1700 if (!isspace (c1
) && isspace (c2
))
1705 static void edit_left_word_move_cmd (WEdit
* edit
)
1707 edit_left_word_move (edit
, 0);
1708 edit
->force
|= REDRAW_PAGE
;
1712 edit_right_word_move (WEdit
*edit
, int s
)
1716 edit_cursor_move (edit
, 1);
1717 if (edit
->curs1
>= edit
->last_byte
)
1719 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1720 c2
= edit_get_byte (edit
, edit
->curs1
);
1721 if (!(my_type_of (c1
) & my_type_of (c2
)))
1723 if (isspace (c1
) && !isspace (c2
))
1726 if (!isspace (c1
) && isspace (c2
))
1731 static void edit_right_word_move_cmd (WEdit
* edit
)
1733 edit_right_word_move (edit
, 0);
1734 edit
->force
|= REDRAW_PAGE
;
1738 static void edit_right_delete_word (WEdit
* edit
)
1742 if (edit
->curs1
>= edit
->last_byte
)
1744 c1
= edit_delete (edit
);
1745 c2
= edit_get_byte (edit
, edit
->curs1
);
1746 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1748 if (!(my_type_of (c1
) & my_type_of (c2
)))
1753 static void edit_left_delete_word (WEdit
* edit
)
1757 if (edit
->curs1
<= 0)
1759 c1
= edit_backspace (edit
);
1760 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1761 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1763 if (!(my_type_of (c1
) & my_type_of (c2
)))
1769 the start column position is not recorded, and hence does not
1770 undo as it happed. But who would notice.
1773 edit_do_undo (WEdit
* edit
)
1778 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1780 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1785 edit_cursor_move (edit
, 1);
1788 edit_cursor_move (edit
, -1);
1791 edit_backspace (edit
);
1797 column_highlighting
= 1;
1800 column_highlighting
= 0;
1803 if (ac
>= 256 && ac
< 512)
1804 edit_insert_ahead (edit
, ac
- 256);
1805 if (ac
>= 0 && ac
< 256)
1806 edit_insert (edit
, ac
);
1808 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1809 edit
->mark1
= ac
- MARK_1
;
1810 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1811 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1812 edit
->mark2
= ac
- MARK_2
;
1813 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1816 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1819 if (edit
->start_display
> ac
- KEY_PRESS
) {
1820 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1821 edit
->force
|= REDRAW_PAGE
;
1822 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1823 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1824 edit
->force
|= REDRAW_PAGE
;
1826 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1827 edit_update_curs_row (edit
);
1830 edit
->stack_disable
= 0;
1833 static void edit_delete_to_line_end (WEdit
* edit
)
1835 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1842 static void edit_delete_to_line_begin (WEdit
* edit
)
1844 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1847 edit_backspace (edit
);
1852 edit_delete_line (WEdit
*edit
)
1855 * Delete right part of the line.
1856 * Note that edit_get_byte() returns '\n' when byte position is
1859 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1860 (void) edit_delete (edit
);
1865 * Note that edit_delete() will not corrupt anything if called while
1866 * cursor position is EOF.
1868 (void) edit_delete (edit
);
1871 * Delete left part of the line.
1872 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1874 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1875 (void) edit_backspace (edit
);
1879 static void insert_spaces_tab (WEdit
* edit
, int half
)
1882 edit_update_curs_col (edit
);
1883 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1885 edit_insert (edit
, ' ');
1890 static int is_aligned_on_a_tab (WEdit
* edit
)
1892 edit_update_curs_col (edit
);
1893 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1894 return 0; /* not alligned on a tab */
1898 static int right_of_four_spaces (WEdit
*edit
)
1901 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1902 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1904 return is_aligned_on_a_tab (edit
);
1908 static int left_of_four_spaces (WEdit
*edit
)
1911 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1912 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1914 return is_aligned_on_a_tab (edit
);
1918 int edit_indent_width (WEdit
* edit
, long p
)
1921 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1923 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1926 void edit_insert_indent (WEdit
* edit
, int indent
)
1928 if (!option_fill_tabs_with_spaces
) {
1929 while (indent
>= TAB_SIZE
) {
1930 edit_insert (edit
, '\t');
1934 while (indent
-- > 0)
1935 edit_insert (edit
, ' ');
1939 edit_auto_indent (WEdit
* edit
)
1944 /* use the previous line as a template */
1945 p
= edit_move_backward (edit
, p
, 1);
1946 /* copy the leading whitespace of the line */
1947 for (;;) { /* no range check - the line _is_ \n-terminated */
1948 c
= edit_get_byte (edit
, p
++);
1949 if (c
!= ' ' && c
!= '\t')
1951 edit_insert (edit
, c
);
1955 static void edit_double_newline (WEdit
* edit
)
1957 edit_insert (edit
, '\n');
1958 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1960 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1962 edit
->force
|= REDRAW_PAGE
;
1963 edit_insert (edit
, '\n');
1966 static void edit_tab_cmd (WEdit
* edit
)
1970 if (option_fake_half_tabs
) {
1971 if (is_in_indent (edit
)) {
1972 /*insert a half tab (usually four spaces) unless there is a
1973 half tab already behind, then delete it and insert a
1975 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1976 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1977 edit_backspace (edit
);
1978 edit_insert (edit
, '\t');
1980 insert_spaces_tab (edit
, 1);
1985 if (option_fill_tabs_with_spaces
) {
1986 insert_spaces_tab (edit
, 0);
1988 edit_insert (edit
, '\t');
1993 static void check_and_wrap_line (WEdit
* edit
)
1996 if (!option_typewriter_wrap
)
1998 edit_update_curs_col (edit
);
1999 if (edit
->curs_col
< option_word_wrap_line_length
)
2004 c
= edit_get_byte (edit
, curs
);
2005 if (c
== '\n' || curs
<= 0) {
2006 edit_insert (edit
, '\n');
2009 if (c
== ' ' || c
== '\t') {
2010 int current
= edit
->curs1
;
2011 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2012 edit_insert (edit
, '\n');
2013 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2019 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2021 void edit_push_key_press (WEdit
* edit
)
2023 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2024 if (edit
->mark2
== -1)
2025 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2028 /* this find the matching bracket in either direction, and sets edit->bracket */
2029 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2031 const char * const b
= "{}{[][()(", *p
;
2032 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2033 unsigned long j
= 0;
2035 edit_update_curs_row (edit
);
2036 c
= edit_get_byte (edit
, edit
->curs1
);
2039 if (!furthest_bracket_search
)
2040 furthest_bracket_search
--;
2041 /* not on a bracket at all */
2044 /* the matching bracket */
2046 /* going left or right? */
2047 if (strchr ("{[(", c
))
2049 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2050 /* out of buffer? */
2051 if (q
>= edit
->last_byte
|| q
< 0)
2053 a
= edit_get_byte (edit
, q
);
2054 /* don't want to eat CPU */
2055 if (j
++ > furthest_bracket_search
)
2057 /* out of screen? */
2059 if (q
< edit
->start_display
)
2061 /* count lines if searching downward */
2062 if (inc
> 0 && a
== '\n')
2063 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2066 /* count bracket depth */
2067 i
+= (a
== c
) - (a
== d
);
2068 /* return if bracket depth is zero */
2076 static long last_bracket
= -1;
2078 void edit_find_bracket (WEdit
* edit
)
2080 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2081 if (last_bracket
!= edit
->bracket
)
2082 edit
->force
|= REDRAW_PAGE
;
2083 last_bracket
= edit
->bracket
;
2086 static void edit_goto_matching_bracket (WEdit
*edit
)
2089 q
= edit_get_bracket (edit
, 0, 0);
2092 edit
->bracket
= edit
->curs1
;
2093 edit
->force
|= REDRAW_PAGE
;
2094 edit_cursor_move (edit
, q
- edit
->curs1
);
2098 * This executes a command as though the user initiated it through a key
2099 * press. Callback with WIDGET_KEY as a message calls this after
2100 * translating the key press. This function can be used to pass any
2101 * command to the editor. Note that the screen wouldn't update
2102 * automatically. Either of command or char_for_insertion must be
2103 * passed as -1. Commands are executed, and char_for_insertion is
2104 * inserted at the cursor.
2106 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2108 if (command
== CK_Begin_Record_Macro
) {
2110 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2113 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2114 edit
->force
|= REDRAW_COMPLETELY
;
2115 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2119 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2120 edit
->macro
[edit
->macro_i
].command
= command
;
2121 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2123 /* record the beginning of a set of editing actions initiated by a key press */
2124 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2125 edit_push_key_press (edit
);
2127 edit_execute_cmd (edit
, command
, char_for_insertion
);
2128 if (column_highlighting
)
2129 edit
->force
|= REDRAW_PAGE
;
2132 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2135 This executes a command at a lower level than macro recording.
2136 It also does not push a key_press onto the undo stack. This means
2137 that if it is called many times, a single undo command will undo
2138 all of them. It also does not check for the Undo command.
2141 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2143 edit
->force
|= REDRAW_LINE
;
2145 /* The next key press will unhighlight the found string, so update
2147 if (edit
->found_len
|| column_highlighting
)
2148 edit
->force
|= REDRAW_PAGE
;
2150 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2151 column_highlighting
= 0;
2152 if (!edit
->highlight
2153 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2154 edit_mark_cmd (edit
, 1); /* clear */
2155 edit_mark_cmd (edit
, 0); /* marking on */
2157 edit
->highlight
= 1;
2158 } else { /* any other command */
2159 if (edit
->highlight
)
2160 edit_mark_cmd (edit
, 0); /* clear */
2161 edit
->highlight
= 0;
2164 /* first check for undo */
2165 if (command
== CK_Undo
) {
2166 edit_do_undo (edit
);
2167 edit
->found_len
= 0;
2168 edit
->prev_col
= edit_get_col (edit
);
2169 edit
->search_start
= edit
->curs1
;
2173 /* An ordinary key press */
2174 if (char_for_insertion
>= 0) {
2175 if (edit
->overwrite
) {
2176 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2179 edit_insert (edit
, char_for_insertion
);
2180 if (option_auto_para_formatting
) {
2181 format_paragraph (edit
, 0);
2182 edit
->force
|= REDRAW_PAGE
;
2184 check_and_wrap_line (edit
);
2185 edit
->found_len
= 0;
2186 edit
->prev_col
= edit_get_col (edit
);
2187 edit
->search_start
= edit
->curs1
;
2188 edit_find_bracket (edit
);
2194 case CK_Begin_Page_Highlight
:
2195 case CK_End_Page_Highlight
:
2200 case CK_Word_Left_Highlight
:
2201 case CK_Word_Right_Highlight
:
2202 case CK_Up_Highlight
:
2203 case CK_Down_Highlight
:
2204 if (edit
->mark2
== -1)
2205 break; /*marking is following the cursor: may need to highlight a whole line */
2208 case CK_Left_Highlight
:
2209 case CK_Right_Highlight
:
2210 edit
->force
|= REDRAW_CHAR_ONLY
;
2213 /* basic cursor key commands */
2216 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2217 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2219 edit_backspace (edit
);
2222 if (option_fake_half_tabs
) {
2224 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2225 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2226 edit_backspace (edit
);
2231 edit_backspace (edit
);
2234 if (option_fake_half_tabs
) {
2236 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2237 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2244 case CK_Delete_Word_Left
:
2245 edit_left_delete_word (edit
);
2247 case CK_Delete_Word_Right
:
2248 edit_right_delete_word (edit
);
2250 case CK_Delete_Line
:
2251 edit_delete_line (edit
);
2253 case CK_Delete_To_Line_End
:
2254 edit_delete_to_line_end (edit
);
2256 case CK_Delete_To_Line_Begin
:
2257 edit_delete_to_line_begin (edit
);
2260 if (option_auto_para_formatting
) {
2261 edit_double_newline (edit
);
2262 if (option_return_does_auto_indent
)
2263 edit_auto_indent (edit
);
2264 format_paragraph (edit
, 0);
2266 edit_insert (edit
, '\n');
2267 if (option_return_does_auto_indent
) {
2268 edit_auto_indent (edit
);
2273 edit_insert (edit
, '\n');
2277 case CK_Page_Up_Highlight
:
2278 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2281 case CK_Page_Down_Highlight
:
2282 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2285 case CK_Left_Highlight
:
2286 if (option_fake_half_tabs
) {
2287 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2288 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2289 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2293 edit_cursor_move (edit
, -1);
2296 case CK_Right_Highlight
:
2297 if (option_fake_half_tabs
) {
2298 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2299 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2300 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2304 edit_cursor_move (edit
, 1);
2307 case CK_Begin_Page_Highlight
:
2308 edit_begin_page (edit
);
2311 case CK_End_Page_Highlight
:
2312 edit_end_page (edit
);
2315 case CK_Word_Left_Highlight
:
2316 edit_left_word_move_cmd (edit
);
2319 case CK_Word_Right_Highlight
:
2320 edit_right_word_move_cmd (edit
);
2323 case CK_Up_Highlight
:
2324 edit_move_up (edit
, 1, 0);
2327 case CK_Down_Highlight
:
2328 edit_move_down (edit
, 1, 0);
2330 case CK_Paragraph_Up
:
2331 case CK_Paragraph_Up_Highlight
:
2332 edit_move_up_paragraph (edit
, 0);
2334 case CK_Paragraph_Down
:
2335 case CK_Paragraph_Down_Highlight
:
2336 edit_move_down_paragraph (edit
, 0);
2339 case CK_Scroll_Up_Highlight
:
2340 edit_move_up (edit
, 1, 1);
2342 case CK_Scroll_Down
:
2343 case CK_Scroll_Down_Highlight
:
2344 edit_move_down (edit
, 1, 1);
2347 case CK_Home_Highlight
:
2348 edit_cursor_to_bol (edit
);
2351 case CK_End_Highlight
:
2352 edit_cursor_to_eol (edit
);
2356 edit_tab_cmd (edit
);
2357 if (option_auto_para_formatting
) {
2358 format_paragraph (edit
, 0);
2359 edit
->force
|= REDRAW_PAGE
;
2361 check_and_wrap_line (edit
);
2364 case CK_Toggle_Insert
:
2365 edit
->overwrite
= (edit
->overwrite
== 0);
2369 if (edit
->mark2
>= 0) {
2370 if (column_highlighting
)
2371 edit_push_action (edit
, COLUMN_ON
);
2372 column_highlighting
= 0;
2374 edit_mark_cmd (edit
, 0);
2376 case CK_Column_Mark
:
2377 if (!column_highlighting
)
2378 edit_push_action (edit
, COLUMN_OFF
);
2379 column_highlighting
= 1;
2380 edit_mark_cmd (edit
, 0);
2383 if (column_highlighting
)
2384 edit_push_action (edit
, COLUMN_ON
);
2385 column_highlighting
= 0;
2386 edit_mark_cmd (edit
, 1);
2389 case CK_Toggle_Bookmark
:
2390 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2391 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2392 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2394 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2396 case CK_Flush_Bookmarks
:
2397 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2398 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2399 edit
->force
|= REDRAW_PAGE
;
2401 case CK_Next_Bookmark
:
2402 if (edit
->book_mark
) {
2403 struct _book_mark
*p
;
2404 p
= (struct _book_mark
*) book_mark_find (edit
,
2408 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2409 || p
->line
< edit
->start_line
)
2410 edit_move_display (edit
,
2412 edit
->num_widget_lines
/ 2);
2413 edit_move_to_line (edit
, p
->line
);
2417 case CK_Prev_Bookmark
:
2418 if (edit
->book_mark
) {
2419 struct _book_mark
*p
;
2420 p
= (struct _book_mark
*) book_mark_find (edit
,
2422 while (p
->line
== edit
->curs_line
)
2426 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2427 || p
->line
< edit
->start_line
)
2428 edit_move_display (edit
,
2430 edit
->num_widget_lines
/ 2);
2431 edit_move_to_line (edit
, p
->line
);
2436 case CK_Beginning_Of_Text
:
2437 case CK_Beginning_Of_Text_Highlight
:
2438 edit_move_to_top (edit
);
2440 case CK_End_Of_Text
:
2441 case CK_End_Of_Text_Highlight
:
2442 edit_move_to_bottom (edit
);
2446 edit_block_copy_cmd (edit
);
2449 edit_block_delete_cmd (edit
);
2452 edit_block_move_cmd (edit
);
2456 edit_copy_to_X_buf_cmd (edit
);
2459 edit_cut_to_X_buf_cmd (edit
);
2462 edit_paste_from_X_buf_cmd (edit
);
2464 case CK_Selection_History
:
2465 edit_paste_from_history (edit
);
2469 edit_save_as_cmd (edit
);
2472 edit_save_confirm_cmd (edit
);
2475 edit_load_cmd (edit
);
2478 edit_save_block_cmd (edit
);
2480 case CK_Insert_File
:
2481 edit_insert_file_cmd (edit
);
2484 case CK_Toggle_Syntax
:
2485 if ((option_syntax_highlighting
^= 1) == 1)
2486 edit_load_syntax (edit
, NULL
, option_syntax_type
);
2487 edit
->force
|= REDRAW_PAGE
;
2491 edit_search_cmd (edit
, 0);
2494 edit_search_cmd (edit
, 1);
2497 edit_replace_cmd (edit
, 0);
2499 case CK_Replace_Again
:
2500 edit_replace_cmd (edit
, 1);
2502 case CK_Complete_Word
:
2503 edit_complete_word_cmd (edit
);
2507 dlg_stop (edit
->widget
.parent
);
2510 edit_new_cmd (edit
);
2514 edit_help_cmd (edit
);
2518 edit_refresh_cmd (edit
);
2523 /* fool gcc to prevent a Y2K warning */
2524 char time_format
[] = "_c";
2525 time_format
[0] = '%';
2527 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
2528 edit_print_string (edit
, s
);
2529 edit
->force
|= REDRAW_PAGE
;
2533 edit_goto_cmd (edit
);
2535 case CK_Paragraph_Format
:
2536 format_paragraph (edit
, 1);
2537 edit
->force
|= REDRAW_PAGE
;
2539 case CK_Delete_Macro
:
2540 edit_delete_macro_cmd (edit
);
2542 case CK_Match_Bracket
:
2543 edit_goto_matching_bracket (edit
);
2549 edit_sort_cmd (edit
);
2552 edit_ext_cmd (edit
);
2555 edit_mail_dialog (edit
);
2560 case CK_Select_Codepage
:
2561 edit_select_codepage_cmd (edit
);
2563 case CK_Insert_Literal
:
2564 edit_insert_literal_cmd (edit
);
2566 case CK_Execute_Macro
:
2567 edit_execute_macro_cmd (edit
);
2569 case CK_Begin_End_Macro
:
2570 edit_begin_end_macro_cmd (edit
);
2580 if ((command
/ 1000) == 1) /* a shell command */
2581 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2582 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2583 struct macro m
[MAX_MACRO_LENGTH
];
2585 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
2586 edit_execute_macro (edit
, m
, nm
);
2589 /* keys which must set the col position, and the search vars */
2594 case CK_Replace_Again
:
2595 case CK_Complete_Word
:
2596 edit
->prev_col
= edit_get_col (edit
);
2599 case CK_Up_Highlight
:
2601 case CK_Down_Highlight
:
2603 case CK_Page_Up_Highlight
:
2605 case CK_Page_Down_Highlight
:
2606 case CK_Beginning_Of_Text
:
2607 case CK_Beginning_Of_Text_Highlight
:
2608 case CK_End_Of_Text
:
2609 case CK_End_Of_Text_Highlight
:
2610 case CK_Paragraph_Up
:
2611 case CK_Paragraph_Up_Highlight
:
2612 case CK_Paragraph_Down
:
2613 case CK_Paragraph_Down_Highlight
:
2615 case CK_Scroll_Up_Highlight
:
2616 case CK_Scroll_Down
:
2617 case CK_Scroll_Down_Highlight
:
2618 edit
->search_start
= edit
->curs1
;
2619 edit
->found_len
= 0;
2622 edit
->found_len
= 0;
2623 edit
->prev_col
= edit_get_col (edit
);
2624 edit
->search_start
= edit
->curs1
;
2626 edit_find_bracket (edit
);
2628 if (option_auto_para_formatting
) {
2632 case CK_Delete_Word_Left
:
2633 case CK_Delete_Word_Right
:
2634 case CK_Delete_To_Line_End
:
2635 case CK_Delete_To_Line_Begin
:
2636 format_paragraph (edit
, 0);
2637 edit
->force
|= REDRAW_PAGE
;
2644 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2648 if (edit
->macro_depth
++ > 256) {
2649 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2650 edit
->macro_depth
--;
2653 edit
->force
|= REDRAW_PAGE
;
2654 for (; i
< n
; i
++) {
2655 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2657 edit_update_screen (edit
);
2658 edit
->macro_depth
--;
2661 /* User edit menu, like user menu (F2) but only in editor. */
2663 user_menu (WEdit
* edit
)
2668 long start_mark
, end_mark
;
2669 char *block_file
= mhl_str_dir_plus_file (home_dir
, BLOCK_FILE
);
2672 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2673 if (!nomark
) /* remember marked or not */
2674 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2676 /* run shell scripts from menu */
2677 user_menu_cmd (edit
);
2679 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2680 /* no block messages */
2685 /* i.e. we have marked block */
2686 rc
= edit_block_delete_cmd (edit
);
2690 edit_insert_file (edit
, block_file
);
2693 /* truncate block file */
2694 if ((fd
= fopen (block_file
, "w"))) {
2698 edit_refresh_cmd (edit
);
2699 edit
->force
|= REDRAW_COMPLETELY
;
2702 g_free (block_file
);