1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <sys/types.h>
35 #include "../src/global.h"
39 #include "edit-widget.h"
40 #include "editcmddef.h"
42 #include "../src/cmd.h" /* view_other_cmd() */
43 #include "../src/user.h" /* user_menu_cmd() */
44 #include "../src/wtools.h" /* query_dialog() */
47 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
48 or EDIT_KEY_EMULATION_EMACS
50 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
52 int option_word_wrap_line_length
= 72;
53 int option_typewriter_wrap
= 0;
54 int option_auto_para_formatting
= 0;
55 int option_tab_spacing
= 8;
56 int option_fill_tabs_with_spaces
= 0;
57 int option_return_does_auto_indent
= 1;
58 int option_backspace_through_tabs
= 0;
59 int option_fake_half_tabs
= 1;
60 int option_save_mode
= EDIT_QUICK_SAVE
;
61 int option_save_position
= 1;
62 int option_backup_ext_int
= -1;
63 int option_max_undo
= 32768;
65 int option_edit_right_extreme
= 0;
66 int option_edit_left_extreme
= 0;
67 int option_edit_top_extreme
= 0;
68 int option_edit_bottom_extreme
= 0;
70 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
71 char *option_backup_ext
= "~";
75 * here's a quick sketch of the layout: (don't run this through indent.)
77 * (b1 is buffers1 and b2 is buffers2)
80 * \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
81 * ______________________________________|______________________________________
83 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
84 * |-> |-> |-> |-> |-> |-> |
86 * _<------------------------->|<----------------->_
87 * WEdit->curs2 | WEdit->curs1
92 * file end|||file beginning
102 static void edit_move_to_prev_col (WEdit
*edit
, long p
);
103 static void user_menu (WEdit
*edit
);
105 int edit_get_byte (WEdit
* edit
, long byte_index
)
108 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
111 if (byte_index
>= edit
->curs1
) {
112 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
113 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
115 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
120 * Initialize the buffers for an empty files.
123 edit_init_buffers (WEdit
*edit
)
127 for (j
= 0; j
<= MAXBUFF
; j
++) {
128 edit
->buffers1
[j
] = NULL
;
129 edit
->buffers2
[j
] = NULL
;
134 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
138 * Load file OR text into buffers. Set cursor to the beginning of file.
142 edit_load_file_fast (WEdit
*edit
, const char *filename
)
147 edit
->curs2
= edit
->last_byte
;
148 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
150 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
151 GString
*errmsg
= g_string_new(NULL
);
152 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
153 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
154 g_string_free (errmsg
, TRUE
);
158 if (!edit
->buffers2
[buf2
])
159 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
162 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
163 (edit
->curs2
& M_EDIT_BUF_SIZE
),
164 edit
->curs2
& M_EDIT_BUF_SIZE
);
166 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
167 /* edit->buffers2[0] is already allocated */
168 if (!edit
->buffers2
[buf
])
169 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
170 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
177 /* detecting an error on save is easy: just check if every byte has been written. */
178 /* detecting an error on read, is not so easy 'cos there is not way to tell
179 whether you read everything or not. */
180 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
181 static const struct edit_filters
{
182 const char *read
, *write
, *extension
;
184 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
185 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
186 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
189 /* Return index of the filter or -1 is there is no appropriate filter */
190 static int edit_find_filter (const char *filename
)
195 l
= strlen (filename
);
196 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
197 e
= strlen (all_filters
[i
].extension
);
199 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
206 edit_get_filter (const char *filename
)
209 char *p
, *quoted_name
;
210 i
= edit_find_filter (filename
);
213 quoted_name
= name_quote (filename
, 0);
214 l
= strlen (quoted_name
);
215 p
= g_malloc (strlen (all_filters
[i
].read
) + l
+ 2);
216 sprintf (p
, all_filters
[i
].read
, quoted_name
);
217 g_free (quoted_name
);
222 edit_get_write_filter (const char *write_name
, const char *filename
)
226 i
= edit_find_filter (filename
);
229 writename
= name_quote (write_name
, 0);
230 l
= strlen (writename
);
231 p
= g_malloc (strlen (all_filters
[i
].write
) + l
+ 2);
232 sprintf (p
, all_filters
[i
].write
, writename
);
238 edit_insert_stream (WEdit
* edit
, FILE * f
)
242 while ((c
= fgetc (f
)) >= 0) {
243 edit_insert (edit
, c
);
249 long edit_write_stream (WEdit
* edit
, FILE * f
)
252 for (i
= 0; i
< edit
->last_byte
; i
++)
253 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
258 #define TEMP_BUF_LEN 1024
260 /* inserts a file at the cursor, returns 1 on success */
262 edit_insert_file (WEdit
*edit
, const char *filename
)
265 if ((p
= edit_get_filter (filename
))) {
267 long current
= edit
->curs1
;
268 f
= (FILE *) popen (p
, "r");
270 edit_insert_stream (edit
, f
);
271 edit_cursor_move (edit
, current
- edit
->curs1
);
272 if (pclose (f
) > 0) {
273 GString
*errmsg
= g_string_new (NULL
);
274 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
275 edit_error_dialog (_("Error"), errmsg
->str
);
276 g_string_free (errmsg
, TRUE
);
281 GString
*errmsg
= g_string_new (NULL
);
282 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
283 edit_error_dialog (_("Error"), errmsg
->str
);
284 g_string_free (errmsg
, TRUE
);
290 int i
, file
, blocklen
;
291 long current
= edit
->curs1
;
293 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
295 buf
= g_malloc (TEMP_BUF_LEN
);
296 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
297 for (i
= 0; i
< blocklen
; i
++)
298 edit_insert (edit
, buf
[i
]);
300 edit_cursor_move (edit
, current
- edit
->curs1
);
309 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
311 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
314 GString
*errmsg
= (GString
*) 0;
316 /* Try opening an existing file */
317 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
321 * Try creating the file. O_EXCL prevents following broken links
322 * and opening existing files.
326 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
329 g_string_sprintf (errmsg
= g_string_new (NULL
),
330 _(" Cannot open %s for reading "), filename
);
333 /* New file, delete it if it's not modified or saved */
334 edit
->delete_file
= 1;
338 /* Check what we have opened */
339 if (mc_fstat (file
, st
) < 0) {
340 g_string_sprintf (errmsg
= g_string_new (NULL
),
341 _(" Cannot get size/permissions for %s "), filename
);
345 /* We want to open regular files only */
346 if (!S_ISREG (st
->st_mode
)) {
347 g_string_sprintf (errmsg
= g_string_new (NULL
),
348 _(" %s is not a regular file "), filename
);
353 * Don't delete non-empty files.
354 * O_EXCL should prevent it, but let's be on the safe side.
356 if (st
->st_size
> 0) {
357 edit
->delete_file
= 0;
360 if (st
->st_size
>= SIZE_LIMIT
) {
361 g_string_sprintf (errmsg
= g_string_new (NULL
),
362 _(" File %s is too large "), filename
);
367 (void) mc_close (file
);
369 edit_error_dialog (_("Error"), errmsg
->str
);
370 g_string_free (errmsg
, TRUE
);
377 * Open the file and load it into the buffers, either directly or using
378 * a filter. Return 0 on success, 1 on error.
380 * Fast loading (edit_load_file_fast) is used when the file size is
381 * known. In this case the data is read into the buffers by blocks.
382 * If the file size is not known, the data is loaded byte by byte in
386 edit_load_file (WEdit
*edit
)
390 /* Cannot do fast load if a filter is used */
391 if (edit_find_filter (edit
->filename
) >= 0)
395 * VFS may report file size incorrectly, and slow load is not a big
396 * deal considering overhead in VFS.
398 if (!vfs_file_is_local (edit
->filename
))
402 * FIXME: line end translation should disable fast loading as well
403 * Consider doing fseek() to the end and ftell() for the real size.
406 if (*edit
->filename
) {
407 /* If we are dealing with a real file, check that it exists */
408 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
411 /* nothing to load */
415 edit_init_buffers (edit
);
418 edit
->last_byte
= edit
->stat1
.st_size
;
419 edit_load_file_fast (edit
, edit
->filename
);
420 /* If fast load was used, the number of lines wasn't calculated */
421 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
424 if (*edit
->filename
) {
425 edit
->stack_disable
= 1;
426 if (!edit_insert_file (edit
, edit
->filename
)) {
430 edit
->stack_disable
= 0;
436 /* Restore saved cursor position in the file */
438 edit_load_position (WEdit
*edit
)
443 if (!edit
->filename
|| !*edit
->filename
)
446 filename
= vfs_canon (edit
->filename
);
447 load_file_position (filename
, &line
, &column
);
450 edit_move_to_line (edit
, line
- 1);
451 edit
->prev_col
= column
;
452 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
453 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
456 /* Save cursor position in the file */
458 edit_save_position (WEdit
*edit
)
462 if (!edit
->filename
|| !*edit
->filename
)
465 filename
= vfs_canon (edit
->filename
);
466 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
470 /* Clean the WEdit stricture except the widget part */
472 edit_purge_widget (WEdit
*edit
)
474 int len
= sizeof (WEdit
) - sizeof (Widget
);
475 char *start
= (char *) edit
+ sizeof (Widget
);
476 memset (start
, 0, len
);
477 edit
->macro_i
= -1; /* not recording a macro */
480 #define space_width 1
483 * Fill in the edit structure. Return NULL on failure. Pass edit as
484 * NULL to allocate a new structure.
486 * If line is 0, try to restore saved position. Otherwise put the
487 * cursor on that line and show it in the middle of the screen.
490 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
498 * Expand option_whole_chars_search by national letters using
502 static char option_whole_chars_search_buf
[256];
504 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
506 size_t len
= strlen (option_whole_chars_search
);
508 strcpy (option_whole_chars_search_buf
,
509 option_whole_chars_search
);
511 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
512 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
513 option_whole_chars_search_buf
[len
++] = i
;
517 option_whole_chars_search_buf
[len
] = 0;
518 option_whole_chars_search
= option_whole_chars_search_buf
;
520 #endif /* ENABLE_NLS */
521 edit
= g_malloc0 (sizeof (WEdit
));
524 edit_purge_widget (edit
);
525 edit
->num_widget_lines
= lines
;
526 edit
->num_widget_columns
= columns
;
527 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
528 edit
->stat1
.st_uid
= getuid ();
529 edit
->stat1
.st_gid
= getgid ();
531 edit
->force
|= REDRAW_PAGE
;
532 edit_set_filename (edit
, filename
);
533 edit
->stack_size
= START_STACK_SIZE
;
534 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
535 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
536 if (edit_load_file (edit
)) {
537 /* edit_load_file already gives an error message */
542 edit
->loading_done
= 1;
545 edit_load_syntax (edit
, 0, 0);
548 edit_get_syntax_color (edit
, -1, &color
);
551 /* load saved cursor position */
552 if ((line
== 0) && option_save_position
) {
553 edit_load_position (edit
);
557 edit_move_display (edit
, line
- 1);
558 edit_move_to_line (edit
, line
- 1);
564 /* Clear the edit struct, freeing everything in it. Return 1 on success */
566 edit_clean (WEdit
*edit
)
573 /* a stale lock, remove it */
575 edit
->locked
= edit_unlock_file (edit
->filename
);
577 /* save cursor position */
578 if (option_save_position
)
579 edit_save_position (edit
);
581 /* File specified on the mcedit command line and never saved */
582 if (edit
->delete_file
)
583 unlink (edit
->filename
);
585 edit_free_syntax_rules (edit
);
586 book_mark_flush (edit
, -1);
587 for (; j
<= MAXBUFF
; j
++) {
588 g_free (edit
->buffers1
[j
]);
589 g_free (edit
->buffers2
[j
]);
592 g_free (edit
->undo_stack
);
593 g_free (edit
->filename
);
596 edit_purge_widget (edit
);
598 /* Free temporary strings used in catstrs() */
605 /* returns 1 on success */
606 int edit_renew (WEdit
* edit
)
608 int lines
= edit
->num_widget_lines
;
609 int columns
= edit
->num_widget_columns
;
613 if (!edit_init (edit
, lines
, columns
, "", 0))
619 * Load a new file into the editor. If it fails, preserve the old file.
620 * To do it, allocate a new widget, initialize it and, if the new file
621 * was loaded, copy the data to the old widget.
622 * Return 1 on success, 0 on failure.
625 edit_reload (WEdit
*edit
, const char *filename
)
628 int lines
= edit
->num_widget_lines
;
629 int columns
= edit
->num_widget_columns
;
631 e
= g_malloc0 (sizeof (WEdit
));
632 e
->widget
= edit
->widget
;
633 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
638 memcpy (edit
, e
, sizeof (WEdit
));
645 Recording stack for undo:
646 The following is an implementation of a compressed stack. Identical
647 pushes are recorded by a negative prefix indicating the number of times the
648 same char was pushed. This saves space for repeated curs-left or curs-right
665 If the stack long int is 0-255 it represents a normal insert (from a backspace),
666 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
667 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
668 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
671 The only way the cursor moves or the buffer is changed is through the routines:
672 insert, backspace, insert_ahead, delete, and cursor_move.
673 These record the reverse undo movements onto the stack each time they are
676 Each key press results in a set of actions (insert; delete ...). So each time
677 a key is pressed the current position of start_display is pushed as
678 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
679 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
680 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
684 void edit_push_action (WEdit
* edit
, long c
,...)
686 unsigned long sp
= edit
->stack_pointer
;
689 /* first enlarge the stack if necessary */
690 if (sp
> edit
->stack_size
- 10) { /* say */
691 if (option_max_undo
< 256)
692 option_max_undo
= 256;
693 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
694 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
696 edit
->undo_stack
= t
;
697 edit
->stack_size
<<= 1;
698 edit
->stack_size_mask
= edit
->stack_size
- 1;
702 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
703 if (edit
->stack_disable
)
706 #ifdef FAST_MOVE_CURSOR
707 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
709 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
710 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
712 c
= -(va_arg (ap
, int));
715 #endif /* ! FAST_MOVE_CURSOR */
716 if (edit
->stack_bottom
!= sp
717 && spm1
!= edit
->stack_bottom
718 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
720 if (edit
->undo_stack
[spm1
] < 0) {
721 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
723 if (edit
->undo_stack
[spm1
] > -1000000000) {
724 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
725 edit
->undo_stack
[spm1
]--;
729 /* #define NO_STACK_CURSMOVE_ANIHILATION */
730 #ifndef NO_STACK_CURSMOVE_ANIHILATION
731 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
732 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
733 if (edit
->undo_stack
[spm1
] == -2)
734 edit
->stack_pointer
= spm1
;
736 edit
->undo_stack
[spm1
]++;
741 d
= edit
->undo_stack
[spm1
];
744 return; /* --> no need to push multiple do-nothings */
745 edit
->undo_stack
[sp
] = -2;
748 #ifndef NO_STACK_CURSMOVE_ANIHILATION
749 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
750 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
751 edit
->stack_pointer
= spm1
;
757 edit
->undo_stack
[sp
] = c
;
760 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
762 /* if the sp wraps round and catches the stack_bottom then erase
763 * the first set of actions on the stack to make space - by moving
764 * stack_bottom forward one "key press" */
765 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
766 if ((unsigned long) c
== edit
->stack_bottom
||
767 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
769 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
770 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
772 /*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: */
773 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
774 edit
->stack_bottom
= edit
->stack_pointer
= 0;
778 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
779 then the file should be as it was when he loaded up. Then set edit->modified to 0.
782 pop_action (WEdit
* edit
)
785 unsigned long sp
= edit
->stack_pointer
;
786 if (sp
== edit
->stack_bottom
) {
789 sp
= (sp
- 1) & edit
->stack_size_mask
;
790 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
791 /* edit->undo_stack[sp] = '@'; */
792 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
795 if (sp
== edit
->stack_bottom
) {
798 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
799 if (edit
->undo_stack
[sp
] == -2) {
800 /* edit->undo_stack[sp] = '@'; */
801 edit
->stack_pointer
= sp
;
803 edit
->undo_stack
[sp
]++;
808 /* is called whenever a modification is made by one of the four routines below */
809 static inline void edit_modification (WEdit
* edit
)
811 edit
->caches_valid
= 0;
812 edit
->screen_modified
= 1;
814 /* raise lock when file modified */
815 if (!edit
->modified
&& !edit
->delete_file
)
816 edit
->locked
= edit_lock_file (edit
->filename
);
821 Basic low level single character buffer alterations and movements at the cursor.
822 Returns char passed over, inserted or removed.
826 edit_insert (WEdit
*edit
, int c
)
828 /* check if file has grown to large */
829 if (edit
->last_byte
>= SIZE_LIMIT
)
832 /* first we must update the position of the display window */
833 if (edit
->curs1
< edit
->start_display
) {
834 edit
->start_display
++;
839 /* Mark file as modified, unless the file hasn't been fully loaded */
840 if (edit
->loading_done
) {
841 edit_modification (edit
);
844 /* now we must update some info on the file and check if a redraw is required */
847 book_mark_inc (edit
, edit
->curs_line
);
850 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
853 /* save the reverse command onto the undo stack */
854 edit_push_action (edit
, BACKSPACE
);
857 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
858 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
859 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
861 /* add a new buffer if we've reached the end of the last one */
862 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
863 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
864 g_malloc (EDIT_BUF_SIZE
);
866 /* perform the insertion */
867 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
868 curs1
& M_EDIT_BUF_SIZE
]
871 /* update file length */
874 /* update cursor position */
879 /* same as edit_insert and move left */
880 void edit_insert_ahead (WEdit
* edit
, int c
)
882 if (edit
->last_byte
>= SIZE_LIMIT
)
884 if (edit
->curs1
< edit
->start_display
) {
885 edit
->start_display
++;
889 edit_modification (edit
);
892 book_mark_inc (edit
, edit
->curs_line
);
894 edit
->force
|= REDRAW_AFTER_CURSOR
;
896 edit_push_action (edit
, DELCHAR
);
898 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
899 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
900 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
902 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
903 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
904 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
911 int edit_delete (WEdit
* edit
)
917 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
918 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
919 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
921 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
923 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
924 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
925 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
930 edit_modification (edit
);
933 book_mark_dec (edit
, edit
->curs_line
);
935 edit
->force
|= REDRAW_AFTER_CURSOR
;
937 edit_push_action (edit
, p
+ 256);
938 if (edit
->curs1
< edit
->start_display
) {
939 edit
->start_display
--;
949 edit_backspace (WEdit
* edit
)
955 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
956 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
957 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
959 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
960 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
961 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
962 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
967 edit_modification (edit
);
970 book_mark_dec (edit
, edit
->curs_line
);
973 edit
->force
|= REDRAW_AFTER_CURSOR
;
975 edit_push_action (edit
, p
);
977 if (edit
->curs1
< edit
->start_display
) {
978 edit
->start_display
--;
986 #ifdef FAST_MOVE_CURSOR
988 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
991 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
993 next
-= (unsigned long) dest
;
1001 edit_move_backward_lots (WEdit
*edit
, long increment
)
1006 if (increment
> edit
->curs1
)
1007 increment
= edit
->curs1
;
1010 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1012 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1015 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1020 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1021 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1026 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1027 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1028 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1029 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1032 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1033 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1034 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1039 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1041 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1043 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1044 g_malloc (EDIT_BUF_SIZE
);
1049 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1059 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1061 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1065 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1066 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1069 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1071 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1072 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1077 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1079 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1081 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1082 g_malloc (EDIT_BUF_SIZE
);
1087 return edit_get_byte (edit
, edit
->curs1
);
1090 #endif /* ! FAST_MOVE_CURSOR */
1092 /* moves the cursor right or left: increment positive or negative respectively */
1093 int edit_cursor_move (WEdit
* edit
, long increment
)
1095 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1098 #ifdef FAST_MOVE_CURSOR
1099 if (increment
< -256) {
1100 edit
->force
|= REDRAW_PAGE
;
1101 return edit_move_backward_lots (edit
, -increment
);
1103 #endif /* ! FAST_MOVE_CURSOR */
1105 if (increment
< 0) {
1106 for (; increment
< 0; increment
++) {
1110 edit_push_action (edit
, CURS_RIGHT
);
1112 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1113 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1114 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1115 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1117 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1118 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1119 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1120 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1125 edit
->force
|= REDRAW_LINE_BELOW
;
1130 } else if (increment
> 0) {
1131 for (; increment
> 0; increment
--) {
1135 edit_push_action (edit
, CURS_LEFT
);
1137 c
= edit_get_byte (edit
, edit
->curs1
);
1138 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1139 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1140 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1142 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1143 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1144 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1145 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1150 edit
->force
|= REDRAW_LINE_ABOVE
;
1158 /* These functions return positions relative to lines */
1160 /* returns index of last char on line + 1 */
1161 long edit_eol (WEdit
* edit
, long current
)
1163 if (current
< edit
->last_byte
) {
1165 if (edit_get_byte (edit
, current
) == '\n')
1168 return edit
->last_byte
;
1172 /* returns index of first char on line */
1173 long edit_bol (WEdit
* edit
, long current
)
1177 if (edit_get_byte (edit
, current
- 1) == '\n')
1185 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1188 if (upto
> edit
->last_byte
)
1189 upto
= edit
->last_byte
;
1192 while (current
< upto
)
1193 if (edit_get_byte (edit
, current
++) == '\n')
1199 /* If lines is zero this returns the count of lines from current to upto. */
1200 /* If upto is zero returns index of lines forward current. */
1201 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1204 return edit_count_lines (edit
, current
, upto
);
1210 next
= edit_eol (edit
, current
) + 1;
1211 if (next
> edit
->last_byte
)
1221 /* Returns offset of 'lines' lines up from current */
1222 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1226 current
= edit_bol (edit
, current
);
1227 while((lines
--) && current
!= 0)
1228 current
= edit_bol (edit
, current
- 1);
1232 /* If cols is zero this returns the count of columns from current to upto. */
1233 /* If upto is zero returns index of cols across from current. */
1234 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1243 q
= edit
->last_byte
+ 2;
1245 for (col
= 0, p
= current
; p
< q
; p
++) {
1253 c
= edit_get_byte (edit
, p
);
1255 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1256 else if (c
== '\n') {
1261 } else if (c
< 32 || c
== 127)
1262 col
+= 2; /* Caret notation for control characters */
1269 /* returns the current column position of the cursor */
1270 int edit_get_col (WEdit
* edit
)
1272 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1276 /* Scrolling functions */
1278 void edit_update_curs_row (WEdit
* edit
)
1280 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1283 void edit_update_curs_col (WEdit
* edit
)
1285 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1288 /*moves the display start position up by i lines */
1289 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1291 unsigned long lines_above
= edit
->start_line
;
1292 if (i
> lines_above
)
1295 edit
->start_line
-= i
;
1296 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1297 edit
->force
|= REDRAW_PAGE
;
1298 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1300 edit_update_curs_row (edit
);
1304 /* returns 1 if could scroll, 0 otherwise */
1305 void edit_scroll_downward (WEdit
* edit
, int i
)
1308 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1309 if (lines_below
> 0) {
1310 if (i
> lines_below
)
1312 edit
->start_line
+= i
;
1313 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1314 edit
->force
|= REDRAW_PAGE
;
1315 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1317 edit_update_curs_row (edit
);
1320 void edit_scroll_right (WEdit
* edit
, int i
)
1322 edit
->force
|= REDRAW_PAGE
;
1323 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1324 edit
->start_col
-= i
;
1327 void edit_scroll_left (WEdit
* edit
, int i
)
1329 if (edit
->start_col
) {
1330 edit
->start_col
+= i
;
1331 if (edit
->start_col
> 0)
1332 edit
->start_col
= 0;
1333 edit
->force
|= REDRAW_PAGE
;
1334 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1338 /* high level cursor movement commands */
1340 static int is_in_indent (WEdit
*edit
)
1342 long p
= edit_bol (edit
, edit
->curs1
);
1343 while (p
< edit
->curs1
)
1344 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1349 static int left_of_four_spaces (WEdit
*edit
);
1352 edit_move_to_prev_col (WEdit
* edit
, long p
)
1354 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1356 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1357 edit_update_curs_col (edit
);
1359 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1360 int q
= edit
->curs_col
;
1361 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1362 p
= edit_bol (edit
, edit
->curs1
);
1363 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1364 if (!left_of_four_spaces (edit
))
1365 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1372 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1374 unsigned long p
, l
= edit
->curs_line
;
1380 edit
->force
|= REDRAW_PAGE
;
1382 edit_scroll_upward (edit
, i
);
1384 p
= edit_bol (edit
, edit
->curs1
);
1385 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1386 edit_move_to_prev_col (edit
, p
);
1388 edit
->search_start
= edit
->curs1
;
1389 edit
->found_len
= 0;
1394 is_blank (WEdit
*edit
, long offset
)
1398 s
= edit_bol (edit
, offset
);
1399 f
= edit_eol (edit
, offset
) - 1;
1401 c
= edit_get_byte (edit
, s
++);
1409 /* returns the offset of line i */
1411 edit_find_line (WEdit
*edit
, int line
)
1415 if (!edit
->caches_valid
) {
1416 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1417 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1418 /* three offsets that we *know* are line 0 at 0 and these two: */
1419 edit
->line_numbers
[1] = edit
->curs_line
;
1420 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1421 edit
->line_numbers
[2] = edit
->total_lines
;
1422 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1423 edit
->caches_valid
= 1;
1425 if (line
>= edit
->total_lines
)
1426 return edit
->line_offsets
[2];
1429 /* find the closest known point */
1430 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1432 n
= abs (edit
->line_numbers
[i
] - line
);
1439 return edit
->line_offsets
[j
]; /* know the offset exactly */
1440 if (m
== 1 && j
>= 3)
1441 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1443 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1444 if (line
> edit
->line_numbers
[j
])
1445 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1447 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1448 edit
->line_numbers
[i
] = line
;
1449 return edit
->line_offsets
[i
];
1452 int line_is_blank (WEdit
* edit
, long line
)
1454 return is_blank (edit
, edit_find_line (edit
, line
));
1457 /* moves up until a blank line is reached, or until just
1458 before a non-blank line is reached */
1459 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1462 if (edit
->curs_line
<= 1) {
1465 if (line_is_blank (edit
, edit
->curs_line
)) {
1466 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1467 for (i
= edit
->curs_line
- 1; i
; i
--)
1468 if (!line_is_blank (edit
, i
)) {
1473 for (i
= edit
->curs_line
- 1; i
; i
--)
1474 if (line_is_blank (edit
, i
))
1478 for (i
= edit
->curs_line
- 1; i
; i
--)
1479 if (line_is_blank (edit
, i
))
1483 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1487 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1489 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1495 edit
->force
|= REDRAW_PAGE
;
1497 edit_scroll_downward (edit
, i
);
1498 p
= edit_bol (edit
, edit
->curs1
);
1499 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1500 edit_move_to_prev_col (edit
, p
);
1502 edit
->search_start
= edit
->curs1
;
1503 edit
->found_len
= 0;
1507 /* moves down until a blank line is reached, or until just
1508 before a non-blank line is reached */
1509 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1512 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1513 i
= edit
->total_lines
;
1515 if (line_is_blank (edit
, edit
->curs_line
)) {
1516 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1517 for (i
= edit
->curs_line
+ 1; i
; i
++)
1518 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1523 for (i
= edit
->curs_line
+ 1; i
; i
++)
1524 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1528 for (i
= edit
->curs_line
+ 1; i
; i
++)
1529 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1533 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1536 static void edit_begin_page (WEdit
*edit
)
1538 edit_update_curs_row (edit
);
1539 edit_move_up (edit
, edit
->curs_row
, 0);
1542 static void edit_end_page (WEdit
*edit
)
1544 edit_update_curs_row (edit
);
1545 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1549 /* goto beginning of text */
1550 static void edit_move_to_top (WEdit
* edit
)
1552 if (edit
->curs_line
) {
1553 edit_cursor_move (edit
, -edit
->curs1
);
1554 edit_move_to_prev_col (edit
, 0);
1555 edit
->force
|= REDRAW_PAGE
;
1556 edit
->search_start
= 0;
1557 edit_update_curs_row(edit
);
1562 /* goto end of text */
1563 static void edit_move_to_bottom (WEdit
* edit
)
1565 if (edit
->curs_line
< edit
->total_lines
) {
1566 edit_cursor_move (edit
, edit
->curs2
);
1567 edit
->start_display
= edit
->last_byte
;
1568 edit
->start_line
= edit
->total_lines
;
1569 edit_update_curs_row(edit
);
1570 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1571 edit
->force
|= REDRAW_PAGE
;
1575 /* goto beginning of line */
1576 static void edit_cursor_to_bol (WEdit
* edit
)
1578 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1579 edit
->search_start
= edit
->curs1
;
1580 edit
->prev_col
= edit_get_col (edit
);
1583 /* goto end of line */
1584 static void edit_cursor_to_eol (WEdit
* edit
)
1586 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1587 edit
->search_start
= edit
->curs1
;
1588 edit
->prev_col
= edit_get_col (edit
);
1591 /* move cursor to line 'line' */
1592 void edit_move_to_line (WEdit
* e
, long line
)
1594 if(line
< e
->curs_line
)
1595 edit_move_up (e
, e
->curs_line
- line
, 0);
1597 edit_move_down (e
, line
- e
->curs_line
, 0);
1598 edit_scroll_screen_over_cursor (e
);
1601 /* scroll window so that first visible line is 'line' */
1602 void edit_move_display (WEdit
* e
, long line
)
1604 if(line
< e
->start_line
)
1605 edit_scroll_upward (e
, e
->start_line
- line
);
1607 edit_scroll_downward (e
, line
- e
->start_line
);
1610 /* save markers onto undo stack */
1611 void edit_push_markers (WEdit
* edit
)
1613 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1614 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1617 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1626 /* highlight marker toggle */
1627 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1629 edit_push_markers (edit
);
1631 edit_set_markers (edit
, 0, 0, 0, 0);
1632 edit
->force
|= REDRAW_PAGE
;
1634 if (edit
->mark2
>= 0) {
1635 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1636 edit
->force
|= REDRAW_PAGE
;
1638 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1642 static unsigned long
1647 const char option_chars_move_whole_word
[] =
1648 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1653 if (*option_chars_move_whole_word
== '!')
1655 return 0x80000000UL
;
1659 else if (islower (c
))
1661 else if (isalpha (c
))
1663 else if (isdigit (c
))
1665 else if (isspace (c
))
1667 q
= strchr (option_chars_move_whole_word
, c
);
1669 return 0xFFFFFFFFUL
;
1671 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1675 } while ((q
= strchr (q
+ 1, c
)));
1680 edit_left_word_move (WEdit
*edit
, int s
)
1684 edit_cursor_move (edit
, -1);
1687 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1688 c2
= edit_get_byte (edit
, edit
->curs1
);
1689 if (!(my_type_of (c1
) & my_type_of (c2
)))
1691 if (isspace (c1
) && !isspace (c2
))
1694 if (!isspace (c1
) && isspace (c2
))
1699 static void edit_left_word_move_cmd (WEdit
* edit
)
1701 edit_left_word_move (edit
, 0);
1702 edit
->force
|= REDRAW_PAGE
;
1706 edit_right_word_move (WEdit
*edit
, int s
)
1710 edit_cursor_move (edit
, 1);
1711 if (edit
->curs1
>= edit
->last_byte
)
1713 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1714 c2
= edit_get_byte (edit
, edit
->curs1
);
1715 if (!(my_type_of (c1
) & my_type_of (c2
)))
1717 if (isspace (c1
) && !isspace (c2
))
1720 if (!isspace (c1
) && isspace (c2
))
1725 static void edit_right_word_move_cmd (WEdit
* edit
)
1727 edit_right_word_move (edit
, 0);
1728 edit
->force
|= REDRAW_PAGE
;
1732 static void edit_right_delete_word (WEdit
* edit
)
1736 if (edit
->curs1
>= edit
->last_byte
)
1738 c1
= edit_delete (edit
);
1739 c2
= edit_get_byte (edit
, edit
->curs1
);
1740 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1742 if (!(my_type_of (c1
) & my_type_of (c2
)))
1747 static void edit_left_delete_word (WEdit
* edit
)
1751 if (edit
->curs1
<= 0)
1753 c1
= edit_backspace (edit
);
1754 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1755 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1757 if (!(my_type_of (c1
) & my_type_of (c2
)))
1763 the start column position is not recorded, and hence does not
1764 undo as it happed. But who would notice.
1767 edit_do_undo (WEdit
* edit
)
1772 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1774 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1779 edit_cursor_move (edit
, 1);
1782 edit_cursor_move (edit
, -1);
1785 edit_backspace (edit
);
1791 column_highlighting
= 1;
1794 column_highlighting
= 0;
1797 if (ac
>= 256 && ac
< 512)
1798 edit_insert_ahead (edit
, ac
- 256);
1799 if (ac
>= 0 && ac
< 256)
1800 edit_insert (edit
, ac
);
1802 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1803 edit
->mark1
= ac
- MARK_1
;
1804 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1805 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1806 edit
->mark2
= ac
- MARK_2
;
1807 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1810 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1813 if (edit
->start_display
> ac
- KEY_PRESS
) {
1814 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1815 edit
->force
|= REDRAW_PAGE
;
1816 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1817 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1818 edit
->force
|= REDRAW_PAGE
;
1820 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1821 edit_update_curs_row (edit
);
1824 edit
->stack_disable
= 0;
1827 static void edit_delete_to_line_end (WEdit
* edit
)
1829 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1836 static void edit_delete_to_line_begin (WEdit
* edit
)
1838 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1841 edit_backspace (edit
);
1846 edit_delete_line (WEdit
*edit
)
1849 * Delete right part of the line.
1850 * Note that edit_get_byte() returns '\n' when byte position is
1853 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1854 (void) edit_delete (edit
);
1859 * Note that edit_delete() will not corrupt anything if called while
1860 * cursor position is EOF.
1862 (void) edit_delete (edit
);
1865 * Delete left part of the line.
1866 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1868 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1869 (void) edit_backspace (edit
);
1873 static void insert_spaces_tab (WEdit
* edit
, int half
)
1876 edit_update_curs_col (edit
);
1877 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1879 edit_insert (edit
, ' ');
1884 static int is_aligned_on_a_tab (WEdit
* edit
)
1886 edit_update_curs_col (edit
);
1887 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1888 return 0; /* not alligned on a tab */
1892 static int right_of_four_spaces (WEdit
*edit
)
1895 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1896 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1898 return is_aligned_on_a_tab (edit
);
1902 static int left_of_four_spaces (WEdit
*edit
)
1905 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1906 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1908 return is_aligned_on_a_tab (edit
);
1912 int edit_indent_width (WEdit
* edit
, long p
)
1915 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1917 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1920 void edit_insert_indent (WEdit
* edit
, int indent
)
1922 if (!option_fill_tabs_with_spaces
) {
1923 while (indent
>= TAB_SIZE
) {
1924 edit_insert (edit
, '\t');
1928 while (indent
-- > 0)
1929 edit_insert (edit
, ' ');
1933 edit_auto_indent (WEdit
* edit
, int extra
, int no_advance
)
1938 while (isspace (edit_get_byte (edit
, p
- 1)) && p
> 0) /* move back/up to a line with text */
1940 indent
= edit_indent_width (edit
, edit_bol (edit
, p
));
1941 if (edit
->curs_col
< indent
&& no_advance
)
1942 indent
= edit
->curs_col
;
1943 edit_insert_indent (edit
, indent
+ (option_fake_half_tabs
? HALF_TAB_SIZE
: TAB_SIZE
) * space_width
* extra
);
1946 static void edit_double_newline (WEdit
* edit
)
1948 edit_insert (edit
, '\n');
1949 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1951 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1953 edit
->force
|= REDRAW_PAGE
;
1954 edit_insert (edit
, '\n');
1957 static void edit_tab_cmd (WEdit
* edit
)
1961 if (option_fake_half_tabs
) {
1962 if (is_in_indent (edit
)) {
1963 /*insert a half tab (usually four spaces) unless there is a
1964 half tab already behind, then delete it and insert a
1966 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1967 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1968 edit_backspace (edit
);
1969 edit_insert (edit
, '\t');
1971 insert_spaces_tab (edit
, 1);
1976 if (option_fill_tabs_with_spaces
) {
1977 insert_spaces_tab (edit
, 0);
1979 edit_insert (edit
, '\t');
1984 static void check_and_wrap_line (WEdit
* edit
)
1987 if (!option_typewriter_wrap
)
1989 edit_update_curs_col (edit
);
1990 if (edit
->curs_col
< option_word_wrap_line_length
)
1995 c
= edit_get_byte (edit
, curs
);
1996 if (c
== '\n' || curs
<= 0) {
1997 edit_insert (edit
, '\n');
2000 if (c
== ' ' || c
== '\t') {
2001 int current
= edit
->curs1
;
2002 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2003 edit_insert (edit
, '\n');
2004 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2010 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2012 void edit_push_key_press (WEdit
* edit
)
2014 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2015 if (edit
->mark2
== -1)
2016 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2019 /* this find the matching bracket in either direction, and sets edit->bracket */
2020 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2022 const char * const b
= "{}{[][()(", *p
;
2023 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2024 unsigned long j
= 0;
2026 edit_update_curs_row (edit
);
2027 c
= edit_get_byte (edit
, edit
->curs1
);
2030 if (!furthest_bracket_search
)
2031 furthest_bracket_search
--;
2032 /* not on a bracket at all */
2035 /* the matching bracket */
2037 /* going left or right? */
2038 if (strchr ("{[(", c
))
2040 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2041 /* out of buffer? */
2042 if (q
>= edit
->last_byte
|| q
< 0)
2044 a
= edit_get_byte (edit
, q
);
2045 /* don't want to eat CPU */
2046 if (j
++ > furthest_bracket_search
)
2048 /* out of screen? */
2050 if (q
< edit
->start_display
)
2052 /* count lines if searching downward */
2053 if (inc
> 0 && a
== '\n')
2054 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2057 /* count bracket depth */
2058 i
+= (a
== c
) - (a
== d
);
2059 /* return if bracket depth is zero */
2067 static long last_bracket
= -1;
2069 static void edit_find_bracket (WEdit
* edit
)
2071 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2072 if (last_bracket
!= edit
->bracket
)
2073 edit
->force
|= REDRAW_PAGE
;
2074 last_bracket
= edit
->bracket
;
2077 static void edit_goto_matching_bracket (WEdit
*edit
)
2080 q
= edit_get_bracket (edit
, 0, 0);
2083 edit
->bracket
= edit
->curs1
;
2084 edit
->force
|= REDRAW_PAGE
;
2085 edit_cursor_move (edit
, q
- edit
->curs1
);
2089 * This executes a command as though the user initiated it through a key
2090 * press. Callback with WIDGET_KEY as a message calls this after
2091 * translating the key press. This function can be used to pass any
2092 * command to the editor. Note that the screen wouldn't update
2093 * automatically. Either of command or char_for_insertion must be
2094 * passed as -1. Commands are executed, and char_for_insertion is
2095 * inserted at the cursor.
2097 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2099 if (command
== CK_Begin_Record_Macro
) {
2101 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2104 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2105 edit
->force
|= REDRAW_COMPLETELY
;
2106 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2110 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2111 edit
->macro
[edit
->macro_i
].command
= command
;
2112 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2114 /* record the beginning of a set of editing actions initiated by a key press */
2115 if (command
!= CK_Undo
)
2116 edit_push_key_press (edit
);
2118 edit_execute_cmd (edit
, command
, char_for_insertion
);
2119 if (column_highlighting
)
2120 edit
->force
|= REDRAW_PAGE
;
2123 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2126 This executes a command at a lower level than macro recording.
2127 It also does not push a key_press onto the undo stack. This means
2128 that if it is called many times, a single undo command will undo
2129 all of them. It also does not check for the Undo command.
2132 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2134 edit
->force
|= REDRAW_LINE
;
2136 /* The next key press will unhighlight the found string, so update
2138 if (edit
->found_len
|| column_highlighting
)
2139 edit
->force
|= REDRAW_PAGE
;
2141 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2142 column_highlighting
= 0;
2143 if (!edit
->highlight
2144 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2145 edit_mark_cmd (edit
, 1); /* clear */
2146 edit_mark_cmd (edit
, 0); /* marking on */
2148 edit
->highlight
= 1;
2149 } else { /* any other command */
2150 if (edit
->highlight
)
2151 edit_mark_cmd (edit
, 0); /* clear */
2152 edit
->highlight
= 0;
2155 /* first check for undo */
2156 if (command
== CK_Undo
) {
2157 edit_do_undo (edit
);
2158 edit
->found_len
= 0;
2159 edit
->prev_col
= edit_get_col (edit
);
2160 edit
->search_start
= edit
->curs1
;
2164 /* An ordinary key press */
2165 if (char_for_insertion
>= 0) {
2166 if (edit
->overwrite
) {
2167 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2170 edit_insert (edit
, char_for_insertion
);
2171 if (option_auto_para_formatting
) {
2172 format_paragraph (edit
, 0);
2173 edit
->force
|= REDRAW_PAGE
;
2175 check_and_wrap_line (edit
);
2176 edit
->found_len
= 0;
2177 edit
->prev_col
= edit_get_col (edit
);
2178 edit
->search_start
= edit
->curs1
;
2179 edit_find_bracket (edit
);
2185 case CK_Begin_Page_Highlight
:
2186 case CK_End_Page_Highlight
:
2191 case CK_Word_Left_Highlight
:
2192 case CK_Word_Right_Highlight
:
2193 case CK_Up_Highlight
:
2194 case CK_Down_Highlight
:
2195 if (edit
->mark2
== -1)
2196 break; /*marking is following the cursor: may need to highlight a whole line */
2199 case CK_Left_Highlight
:
2200 case CK_Right_Highlight
:
2201 edit
->force
|= REDRAW_CHAR_ONLY
;
2204 /* basic cursor key commands */
2207 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2208 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2210 edit_backspace (edit
);
2213 if (option_fake_half_tabs
) {
2215 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2216 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2217 edit_backspace (edit
);
2222 edit_backspace (edit
);
2225 if (option_fake_half_tabs
) {
2227 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2228 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2235 case CK_Delete_Word_Left
:
2236 edit_left_delete_word (edit
);
2238 case CK_Delete_Word_Right
:
2239 edit_right_delete_word (edit
);
2241 case CK_Delete_Line
:
2242 edit_delete_line (edit
);
2244 case CK_Delete_To_Line_End
:
2245 edit_delete_to_line_end (edit
);
2247 case CK_Delete_To_Line_Begin
:
2248 edit_delete_to_line_begin (edit
);
2251 if (option_auto_para_formatting
) {
2252 edit_double_newline (edit
);
2253 if (option_return_does_auto_indent
)
2254 edit_auto_indent (edit
, 0, 1);
2255 format_paragraph (edit
, 0);
2257 edit_insert (edit
, '\n');
2258 if (option_return_does_auto_indent
) {
2259 edit_auto_indent (edit
, 0, 1);
2264 edit_insert (edit
, '\n');
2268 case CK_Page_Up_Highlight
:
2269 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2272 case CK_Page_Down_Highlight
:
2273 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2276 case CK_Left_Highlight
:
2277 if (option_fake_half_tabs
) {
2278 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2279 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2280 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2284 edit_cursor_move (edit
, -1);
2287 case CK_Right_Highlight
:
2288 if (option_fake_half_tabs
) {
2289 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2290 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2291 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2295 edit_cursor_move (edit
, 1);
2298 case CK_Begin_Page_Highlight
:
2299 edit_begin_page (edit
);
2302 case CK_End_Page_Highlight
:
2303 edit_end_page (edit
);
2306 case CK_Word_Left_Highlight
:
2307 edit_left_word_move_cmd (edit
);
2310 case CK_Word_Right_Highlight
:
2311 edit_right_word_move_cmd (edit
);
2314 case CK_Up_Highlight
:
2315 edit_move_up (edit
, 1, 0);
2318 case CK_Down_Highlight
:
2319 edit_move_down (edit
, 1, 0);
2321 case CK_Paragraph_Up
:
2322 case CK_Paragraph_Up_Highlight
:
2323 edit_move_up_paragraph (edit
, 0);
2325 case CK_Paragraph_Down
:
2326 case CK_Paragraph_Down_Highlight
:
2327 edit_move_down_paragraph (edit
, 0);
2330 case CK_Scroll_Up_Highlight
:
2331 edit_move_up (edit
, 1, 1);
2333 case CK_Scroll_Down
:
2334 case CK_Scroll_Down_Highlight
:
2335 edit_move_down (edit
, 1, 1);
2338 case CK_Home_Highlight
:
2339 edit_cursor_to_bol (edit
);
2342 case CK_End_Highlight
:
2343 edit_cursor_to_eol (edit
);
2347 edit_tab_cmd (edit
);
2348 if (option_auto_para_formatting
) {
2349 format_paragraph (edit
, 0);
2350 edit
->force
|= REDRAW_PAGE
;
2352 check_and_wrap_line (edit
);
2355 case CK_Toggle_Insert
:
2356 edit
->overwrite
= (edit
->overwrite
== 0);
2360 if (edit
->mark2
>= 0) {
2361 if (column_highlighting
)
2362 edit_push_action (edit
, COLUMN_ON
);
2363 column_highlighting
= 0;
2365 edit_mark_cmd (edit
, 0);
2367 case CK_Column_Mark
:
2368 if (!column_highlighting
)
2369 edit_push_action (edit
, COLUMN_OFF
);
2370 column_highlighting
= 1;
2371 edit_mark_cmd (edit
, 0);
2374 if (column_highlighting
)
2375 edit_push_action (edit
, COLUMN_ON
);
2376 column_highlighting
= 0;
2377 edit_mark_cmd (edit
, 1);
2380 case CK_Toggle_Bookmark
:
2381 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2382 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2383 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2385 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2387 case CK_Flush_Bookmarks
:
2388 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2389 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2390 edit
->force
|= REDRAW_PAGE
;
2392 case CK_Next_Bookmark
:
2393 if (edit
->book_mark
) {
2394 struct _book_mark
*p
;
2395 p
= (struct _book_mark
*) book_mark_find (edit
,
2399 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2400 || p
->line
< edit
->start_line
)
2401 edit_move_display (edit
,
2403 edit
->num_widget_lines
/ 2);
2404 edit_move_to_line (edit
, p
->line
);
2408 case CK_Prev_Bookmark
:
2409 if (edit
->book_mark
) {
2410 struct _book_mark
*p
;
2411 p
= (struct _book_mark
*) book_mark_find (edit
,
2413 while (p
->line
== edit
->curs_line
)
2417 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2418 || p
->line
< edit
->start_line
)
2419 edit_move_display (edit
,
2421 edit
->num_widget_lines
/ 2);
2422 edit_move_to_line (edit
, p
->line
);
2427 case CK_Beginning_Of_Text
:
2428 case CK_Beginning_Of_Text_Highlight
:
2429 edit_move_to_top (edit
);
2431 case CK_End_Of_Text
:
2432 case CK_End_Of_Text_Highlight
:
2433 edit_move_to_bottom (edit
);
2437 edit_block_copy_cmd (edit
);
2440 edit_block_delete_cmd (edit
);
2443 edit_block_move_cmd (edit
);
2447 edit_copy_to_X_buf_cmd (edit
);
2450 edit_cut_to_X_buf_cmd (edit
);
2453 edit_paste_from_X_buf_cmd (edit
);
2455 case CK_Selection_History
:
2456 edit_paste_from_history (edit
);
2460 edit_save_as_cmd (edit
);
2463 edit_save_confirm_cmd (edit
);
2466 edit_load_cmd (edit
);
2469 edit_save_block_cmd (edit
);
2471 case CK_Insert_File
:
2472 edit_insert_file_cmd (edit
);
2476 edit_search_cmd (edit
, 0);
2479 edit_search_cmd (edit
, 1);
2482 edit_replace_cmd (edit
, 0);
2484 case CK_Replace_Again
:
2485 edit_replace_cmd (edit
, 1);
2487 case CK_Complete_Word
:
2488 edit_complete_word_cmd (edit
);
2492 dlg_stop (edit
->widget
.parent
);
2495 edit_new_cmd (edit
);
2499 edit_help_cmd (edit
);
2503 edit_refresh_cmd (edit
);
2508 #ifdef HAVE_STRFTIME
2510 /* fool gcc to prevent a Y2K warning */
2511 char time_format
[] = "_c";
2512 time_format
[0] = '%';
2515 #ifdef HAVE_STRFTIME
2516 strftime (s
, sizeof (s
), time_format
, localtime (&t
));
2517 edit_print_string (edit
, s
);
2519 edit_print_string (edit
, ctime (&t
));
2521 edit
->force
|= REDRAW_PAGE
;
2525 edit_goto_cmd (edit
);
2527 case CK_Paragraph_Format
:
2528 format_paragraph (edit
, 1);
2529 edit
->force
|= REDRAW_PAGE
;
2531 case CK_Delete_Macro
:
2532 edit_delete_macro_cmd (edit
);
2534 case CK_Match_Bracket
:
2535 edit_goto_matching_bracket (edit
);
2541 edit_sort_cmd (edit
);
2544 edit_ext_cmd (edit
);
2547 edit_mail_dialog (edit
);
2557 if ((command
/ 1000) == 1) /* a shell command */
2558 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2559 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2560 struct macro m
[MAX_MACRO_LENGTH
];
2562 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
2563 edit_execute_macro (edit
, m
, nm
);
2566 /* keys which must set the col position, and the search vars */
2571 case CK_Replace_Again
:
2572 case CK_Complete_Word
:
2573 edit
->prev_col
= edit_get_col (edit
);
2576 case CK_Up_Highlight
:
2578 case CK_Down_Highlight
:
2580 case CK_Page_Up_Highlight
:
2582 case CK_Page_Down_Highlight
:
2583 case CK_Beginning_Of_Text
:
2584 case CK_Beginning_Of_Text_Highlight
:
2585 case CK_End_Of_Text
:
2586 case CK_End_Of_Text_Highlight
:
2587 case CK_Paragraph_Up
:
2588 case CK_Paragraph_Up_Highlight
:
2589 case CK_Paragraph_Down
:
2590 case CK_Paragraph_Down_Highlight
:
2592 case CK_Scroll_Up_Highlight
:
2593 case CK_Scroll_Down
:
2594 case CK_Scroll_Down_Highlight
:
2595 edit
->search_start
= edit
->curs1
;
2596 edit
->found_len
= 0;
2597 edit_find_bracket (edit
);
2600 edit
->found_len
= 0;
2601 edit
->prev_col
= edit_get_col (edit
);
2602 edit
->search_start
= edit
->curs1
;
2604 edit_find_bracket (edit
);
2606 if (option_auto_para_formatting
) {
2610 case CK_Delete_Word_Left
:
2611 case CK_Delete_Word_Right
:
2612 case CK_Delete_To_Line_End
:
2613 case CK_Delete_To_Line_Begin
:
2614 format_paragraph (edit
, 0);
2615 edit
->force
|= REDRAW_PAGE
;
2622 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2626 if (edit
->macro_depth
++ > 256) {
2627 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2628 edit
->macro_depth
--;
2631 edit
->force
|= REDRAW_PAGE
;
2632 for (; i
< n
; i
++) {
2633 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2635 edit_update_screen (edit
);
2636 edit
->macro_depth
--;
2639 /* User edit menu, like user menu (F2) but only in editor. */
2641 user_menu (WEdit
* edit
)
2646 long start_mark
, end_mark
;
2647 char *block_file
= concat_dir_and_file (home_dir
, BLOCK_FILE
);
2650 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2651 if (!nomark
) /* remember marked or not */
2652 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2654 /* run shell scripts from menu */
2655 user_menu_cmd (edit
);
2657 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2658 /* no block messages */
2663 /* i.e. we have marked block */
2664 rc
= edit_block_delete_cmd (edit
);
2668 edit_insert_file (edit
, block_file
);
2671 /* truncate block file */
2672 if ((fd
= fopen (block_file
, "w"))) {
2676 edit_refresh_cmd (edit
);
2677 edit
->force
|= REDRAW_COMPLETELY
;
2680 g_free (block_file
);