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>
36 #include "../src/global.h"
40 #include "edit-widget.h"
41 #include "editcmddef.h"
44 #include "../src/cmd.h" /* view_other_cmd() */
45 #include "../src/user.h" /* user_menu_cmd() */
46 #include "../src/wtools.h" /* query_dialog() */
47 #include "../src/timefmt.h" /* time formatting */
51 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
52 or EDIT_KEY_EMULATION_EMACS
54 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
56 int option_word_wrap_line_length
= 72;
57 int option_typewriter_wrap
= 0;
58 int option_auto_para_formatting
= 0;
59 int option_tab_spacing
= 8;
60 int option_fill_tabs_with_spaces
= 0;
61 int option_return_does_auto_indent
= 1;
62 int option_backspace_through_tabs
= 0;
63 int option_fake_half_tabs
= 1;
64 int option_save_mode
= EDIT_QUICK_SAVE
;
65 int option_save_position
= 1;
66 int option_max_undo
= 32768;
68 int option_edit_right_extreme
= 0;
69 int option_edit_left_extreme
= 0;
70 int option_edit_top_extreme
= 0;
71 int option_edit_bottom_extreme
= 0;
73 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
74 char *option_backup_ext
= NULL
;
78 * here's a quick sketch of the layout: (don't run this through indent.)
80 * (b1 is buffers1 and b2 is buffers2)
83 * \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
84 * ______________________________________|______________________________________
86 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
87 * |-> |-> |-> |-> |-> |-> |
89 * _<------------------------->|<----------------->_
90 * WEdit->curs2 | WEdit->curs1
95 * file end|||file beginning
105 static void user_menu (WEdit
*edit
);
107 int edit_get_byte (WEdit
* edit
, long byte_index
)
110 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
113 if (byte_index
>= edit
->curs1
) {
114 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
115 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
117 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
122 * Initialize the buffers for an empty files.
125 edit_init_buffers (WEdit
*edit
)
129 for (j
= 0; j
<= MAXBUFF
; j
++) {
130 edit
->buffers1
[j
] = NULL
;
131 edit
->buffers2
[j
] = NULL
;
136 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
140 * Load file OR text into buffers. Set cursor to the beginning of file.
144 edit_load_file_fast (WEdit
*edit
, const char *filename
)
149 edit
->curs2
= edit
->last_byte
;
150 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
152 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
153 GString
*errmsg
= g_string_new(NULL
);
154 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
155 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
156 g_string_free (errmsg
, TRUE
);
160 if (!edit
->buffers2
[buf2
])
161 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
164 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
165 (edit
->curs2
& M_EDIT_BUF_SIZE
),
166 edit
->curs2
& M_EDIT_BUF_SIZE
);
168 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
169 /* edit->buffers2[0] is already allocated */
170 if (!edit
->buffers2
[buf
])
171 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
172 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
179 /* detecting an error on save is easy: just check if every byte has been written. */
180 /* detecting an error on read, is not so easy 'cos there is not way to tell
181 whether you read everything or not. */
182 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
183 static const struct edit_filters
{
184 const char *read
, *write
, *extension
;
186 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
187 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
188 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
191 /* Return index of the filter or -1 is there is no appropriate filter */
192 static int edit_find_filter (const char *filename
)
197 l
= strlen (filename
);
198 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
199 e
= strlen (all_filters
[i
].extension
);
201 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
208 edit_get_filter (const char *filename
)
211 char *p
, *quoted_name
;
212 i
= edit_find_filter (filename
);
215 quoted_name
= name_quote (filename
, 0);
216 l
= strlen (quoted_name
);
217 p
= g_malloc (strlen (all_filters
[i
].read
) + l
+ 2);
218 sprintf (p
, all_filters
[i
].read
, quoted_name
);
219 g_free (quoted_name
);
224 edit_get_write_filter (const char *write_name
, const char *filename
)
228 i
= edit_find_filter (filename
);
231 writename
= name_quote (write_name
, 0);
232 l
= strlen (writename
);
233 p
= g_malloc (strlen (all_filters
[i
].write
) + l
+ 2);
234 sprintf (p
, all_filters
[i
].write
, writename
);
240 edit_insert_stream (WEdit
* edit
, FILE * f
)
244 while ((c
= fgetc (f
)) >= 0) {
245 edit_insert (edit
, c
);
251 long edit_write_stream (WEdit
* edit
, FILE * f
)
254 for (i
= 0; i
< edit
->last_byte
; i
++)
255 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
260 #define TEMP_BUF_LEN 1024
262 /* inserts a file at the cursor, returns 1 on success */
264 edit_insert_file (WEdit
*edit
, const char *filename
)
267 if ((p
= edit_get_filter (filename
))) {
269 long current
= edit
->curs1
;
270 f
= (FILE *) popen (p
, "r");
272 edit_insert_stream (edit
, f
);
273 edit_cursor_move (edit
, current
- edit
->curs1
);
274 if (pclose (f
) > 0) {
275 GString
*errmsg
= g_string_new (NULL
);
276 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
277 edit_error_dialog (_("Error"), errmsg
->str
);
278 g_string_free (errmsg
, TRUE
);
283 GString
*errmsg
= g_string_new (NULL
);
284 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
285 edit_error_dialog (_("Error"), errmsg
->str
);
286 g_string_free (errmsg
, TRUE
);
292 int i
, file
, blocklen
;
293 long current
= edit
->curs1
;
295 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
297 buf
= g_malloc (TEMP_BUF_LEN
);
298 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
299 for (i
= 0; i
< blocklen
; i
++)
300 edit_insert (edit
, buf
[i
]);
302 edit_cursor_move (edit
, current
- edit
->curs1
);
311 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
313 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
316 GString
*errmsg
= (GString
*) 0;
318 /* Try opening an existing file */
319 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
323 * Try creating the file. O_EXCL prevents following broken links
324 * and opening existing files.
328 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
331 g_string_sprintf (errmsg
= g_string_new (NULL
),
332 _(" Cannot open %s for reading "), filename
);
335 /* New file, delete it if it's not modified or saved */
336 edit
->delete_file
= 1;
340 /* Check what we have opened */
341 if (mc_fstat (file
, st
) < 0) {
342 g_string_sprintf (errmsg
= g_string_new (NULL
),
343 _(" Cannot get size/permissions for %s "), filename
);
347 /* We want to open regular files only */
348 if (!S_ISREG (st
->st_mode
)) {
349 g_string_sprintf (errmsg
= g_string_new (NULL
),
350 _(" %s is not a regular file "), filename
);
355 * Don't delete non-empty files.
356 * O_EXCL should prevent it, but let's be on the safe side.
358 if (st
->st_size
> 0) {
359 edit
->delete_file
= 0;
362 if (st
->st_size
>= SIZE_LIMIT
) {
363 g_string_sprintf (errmsg
= g_string_new (NULL
),
364 _(" File %s is too large "), filename
);
369 (void) mc_close (file
);
371 edit_error_dialog (_("Error"), errmsg
->str
);
372 g_string_free (errmsg
, TRUE
);
379 * Open the file and load it into the buffers, either directly or using
380 * a filter. Return 0 on success, 1 on error.
382 * Fast loading (edit_load_file_fast) is used when the file size is
383 * known. In this case the data is read into the buffers by blocks.
384 * If the file size is not known, the data is loaded byte by byte in
388 edit_load_file (WEdit
*edit
)
392 /* Cannot do fast load if a filter is used */
393 if (edit_find_filter (edit
->filename
) >= 0)
397 * VFS may report file size incorrectly, and slow load is not a big
398 * deal considering overhead in VFS.
400 if (!vfs_file_is_local (edit
->filename
))
404 * FIXME: line end translation should disable fast loading as well
405 * Consider doing fseek() to the end and ftell() for the real size.
408 if (*edit
->filename
) {
409 /* If we are dealing with a real file, check that it exists */
410 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
413 /* nothing to load */
417 edit_init_buffers (edit
);
420 edit
->last_byte
= edit
->stat1
.st_size
;
421 edit_load_file_fast (edit
, edit
->filename
);
422 /* If fast load was used, the number of lines wasn't calculated */
423 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
426 if (*edit
->filename
) {
427 edit
->stack_disable
= 1;
428 if (!edit_insert_file (edit
, edit
->filename
)) {
432 edit
->stack_disable
= 0;
438 /* Restore saved cursor position in the file */
440 edit_load_position (WEdit
*edit
)
445 if (!edit
->filename
|| !*edit
->filename
)
448 filename
= vfs_canon (edit
->filename
);
449 load_file_position (filename
, &line
, &column
);
452 edit_move_to_line (edit
, line
- 1);
453 edit
->prev_col
= column
;
454 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
455 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
458 /* Save cursor position in the file */
460 edit_save_position (WEdit
*edit
)
464 if (!edit
->filename
|| !*edit
->filename
)
467 filename
= vfs_canon (edit
->filename
);
468 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
472 /* Clean the WEdit stricture except the widget part */
474 edit_purge_widget (WEdit
*edit
)
476 int len
= sizeof (WEdit
) - sizeof (Widget
);
477 char *start
= (char *) edit
+ sizeof (Widget
);
478 memset (start
, 0, len
);
479 edit
->macro_i
= -1; /* not recording a macro */
482 #define space_width 1
485 * Fill in the edit structure. Return NULL on failure. Pass edit as
486 * NULL to allocate a new structure.
488 * If line is 0, try to restore saved position. Otherwise put the
489 * cursor on that line and show it in the middle of the screen.
492 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
496 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
501 * Expand option_whole_chars_search by national letters using
505 static char option_whole_chars_search_buf
[256];
507 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
509 size_t len
= strlen (option_whole_chars_search
);
511 strcpy (option_whole_chars_search_buf
,
512 option_whole_chars_search
);
514 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
515 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
516 option_whole_chars_search_buf
[len
++] = i
;
520 option_whole_chars_search_buf
[len
] = 0;
521 option_whole_chars_search
= option_whole_chars_search_buf
;
523 #endif /* ENABLE_NLS */
524 edit
= g_malloc0 (sizeof (WEdit
));
527 edit_purge_widget (edit
);
528 edit
->num_widget_lines
= lines
;
529 edit
->num_widget_columns
= columns
;
530 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
531 edit
->stat1
.st_uid
= getuid ();
532 edit
->stat1
.st_gid
= getgid ();
533 edit
->stat1
.st_mtime
= 0;
535 edit
->force
|= REDRAW_PAGE
;
536 edit_set_filename (edit
, filename
);
537 edit
->stack_size
= START_STACK_SIZE
;
538 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
539 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
540 if (edit_load_file (edit
)) {
541 /* edit_load_file already gives an error message */
546 edit
->loading_done
= 1;
549 edit_load_syntax (edit
, 0, 0);
552 edit_get_syntax_color (edit
, -1, &color
);
555 /* load saved cursor position */
556 if ((line
== 0) && option_save_position
) {
557 edit_load_position (edit
);
561 edit_move_display (edit
, line
- 1);
562 edit_move_to_line (edit
, line
- 1);
565 edit_load_user_map(edit
);
570 /* Clear the edit struct, freeing everything in it. Return 1 on success */
572 edit_clean (WEdit
*edit
)
579 /* a stale lock, remove it */
581 edit
->locked
= edit_unlock_file (edit
->filename
);
583 /* save cursor position */
584 if (option_save_position
)
585 edit_save_position (edit
);
587 /* File specified on the mcedit command line and never saved */
588 if (edit
->delete_file
)
589 unlink (edit
->filename
);
591 edit_free_syntax_rules (edit
);
592 book_mark_flush (edit
, -1);
593 for (; j
<= MAXBUFF
; j
++) {
594 g_free (edit
->buffers1
[j
]);
595 g_free (edit
->buffers2
[j
]);
598 g_free (edit
->undo_stack
);
599 g_free (edit
->filename
);
602 edit_purge_widget (edit
);
604 /* Free temporary strings used in catstrs() */
611 /* returns 1 on success */
612 int edit_renew (WEdit
* edit
)
614 int lines
= edit
->num_widget_lines
;
615 int columns
= edit
->num_widget_columns
;
619 if (!edit_init (edit
, lines
, columns
, "", 0))
625 * Load a new file into the editor. If it fails, preserve the old file.
626 * To do it, allocate a new widget, initialize it and, if the new file
627 * was loaded, copy the data to the old widget.
628 * Return 1 on success, 0 on failure.
631 edit_reload (WEdit
*edit
, const char *filename
)
634 int lines
= edit
->num_widget_lines
;
635 int columns
= edit
->num_widget_columns
;
637 e
= g_malloc0 (sizeof (WEdit
));
638 e
->widget
= edit
->widget
;
639 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
644 memcpy (edit
, e
, sizeof (WEdit
));
651 Recording stack for undo:
652 The following is an implementation of a compressed stack. Identical
653 pushes are recorded by a negative prefix indicating the number of times the
654 same char was pushed. This saves space for repeated curs-left or curs-right
671 If the stack long int is 0-255 it represents a normal insert (from a backspace),
672 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
673 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
674 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
677 The only way the cursor moves or the buffer is changed is through the routines:
678 insert, backspace, insert_ahead, delete, and cursor_move.
679 These record the reverse undo movements onto the stack each time they are
682 Each key press results in a set of actions (insert; delete ...). So each time
683 a key is pressed the current position of start_display is pushed as
684 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
685 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
686 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
690 void edit_push_action (WEdit
* edit
, long c
,...)
692 unsigned long sp
= edit
->stack_pointer
;
696 /* first enlarge the stack if necessary */
697 if (sp
> edit
->stack_size
- 10) { /* say */
698 if (option_max_undo
< 256)
699 option_max_undo
= 256;
700 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
701 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
703 edit
->undo_stack
= t
;
704 edit
->stack_size
<<= 1;
705 edit
->stack_size_mask
= edit
->stack_size
- 1;
709 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
710 if (edit
->stack_disable
)
713 #ifdef FAST_MOVE_CURSOR
714 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
716 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
717 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
719 c
= -(va_arg (ap
, int));
722 #endif /* ! FAST_MOVE_CURSOR */
723 if (edit
->stack_bottom
!= sp
724 && spm1
!= edit
->stack_bottom
725 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
727 if (edit
->undo_stack
[spm1
] < 0) {
728 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
730 if (edit
->undo_stack
[spm1
] > -1000000000) {
731 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
732 edit
->undo_stack
[spm1
]--;
736 /* #define NO_STACK_CURSMOVE_ANIHILATION */
737 #ifndef NO_STACK_CURSMOVE_ANIHILATION
738 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
739 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
740 if (edit
->undo_stack
[spm1
] == -2)
741 edit
->stack_pointer
= spm1
;
743 edit
->undo_stack
[spm1
]++;
748 d
= edit
->undo_stack
[spm1
];
751 return; /* --> no need to push multiple do-nothings */
752 edit
->undo_stack
[sp
] = -2;
755 #ifndef NO_STACK_CURSMOVE_ANIHILATION
756 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
757 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
758 edit
->stack_pointer
= spm1
;
764 edit
->undo_stack
[sp
] = c
;
767 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
769 /* if the sp wraps round and catches the stack_bottom then erase
770 * the first set of actions on the stack to make space - by moving
771 * stack_bottom forward one "key press" */
772 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
773 if ((unsigned long) c
== edit
->stack_bottom
||
774 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
776 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
777 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
779 /*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: */
780 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
781 edit
->stack_bottom
= edit
->stack_pointer
= 0;
785 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
786 then the file should be as it was when he loaded up. Then set edit->modified to 0.
789 pop_action (WEdit
* edit
)
792 unsigned long sp
= edit
->stack_pointer
;
793 if (sp
== edit
->stack_bottom
) {
796 sp
= (sp
- 1) & edit
->stack_size_mask
;
797 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
798 /* edit->undo_stack[sp] = '@'; */
799 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
802 if (sp
== edit
->stack_bottom
) {
805 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
806 if (edit
->undo_stack
[sp
] == -2) {
807 /* edit->undo_stack[sp] = '@'; */
808 edit
->stack_pointer
= sp
;
810 edit
->undo_stack
[sp
]++;
815 /* is called whenever a modification is made by one of the four routines below */
816 static inline void edit_modification (WEdit
* edit
)
818 edit
->caches_valid
= 0;
819 edit
->screen_modified
= 1;
821 /* raise lock when file modified */
822 if (!edit
->modified
&& !edit
->delete_file
)
823 edit
->locked
= edit_lock_file (edit
->filename
);
828 Basic low level single character buffer alterations and movements at the cursor.
829 Returns char passed over, inserted or removed.
833 edit_insert (WEdit
*edit
, int c
)
835 /* check if file has grown to large */
836 if (edit
->last_byte
>= SIZE_LIMIT
)
839 /* first we must update the position of the display window */
840 if (edit
->curs1
< edit
->start_display
) {
841 edit
->start_display
++;
846 /* Mark file as modified, unless the file hasn't been fully loaded */
847 if (edit
->loading_done
) {
848 edit_modification (edit
);
851 /* now we must update some info on the file and check if a redraw is required */
854 book_mark_inc (edit
, edit
->curs_line
);
857 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
860 /* save the reverse command onto the undo stack */
861 edit_push_action (edit
, BACKSPACE
);
864 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
865 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
866 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
868 /* add a new buffer if we've reached the end of the last one */
869 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
870 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
871 g_malloc (EDIT_BUF_SIZE
);
873 /* perform the insertion */
874 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
875 curs1
& M_EDIT_BUF_SIZE
]
878 /* update file length */
881 /* update cursor position */
886 /* same as edit_insert and move left */
887 void edit_insert_ahead (WEdit
* edit
, int c
)
889 if (edit
->last_byte
>= SIZE_LIMIT
)
891 if (edit
->curs1
< edit
->start_display
) {
892 edit
->start_display
++;
896 edit_modification (edit
);
899 book_mark_inc (edit
, edit
->curs_line
);
901 edit
->force
|= REDRAW_AFTER_CURSOR
;
903 edit_push_action (edit
, DELCHAR
);
905 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
906 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
907 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
909 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
910 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
911 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
918 int edit_delete (WEdit
* edit
)
924 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
925 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
926 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
928 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
930 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
931 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
932 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
937 edit_modification (edit
);
940 book_mark_dec (edit
, edit
->curs_line
);
942 edit
->force
|= REDRAW_AFTER_CURSOR
;
944 edit_push_action (edit
, p
+ 256);
945 if (edit
->curs1
< edit
->start_display
) {
946 edit
->start_display
--;
956 edit_backspace (WEdit
* edit
)
962 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
963 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
964 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
966 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
967 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
968 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
969 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
974 edit_modification (edit
);
977 book_mark_dec (edit
, edit
->curs_line
);
980 edit
->force
|= REDRAW_AFTER_CURSOR
;
982 edit_push_action (edit
, p
);
984 if (edit
->curs1
< edit
->start_display
) {
985 edit
->start_display
--;
993 #ifdef FAST_MOVE_CURSOR
995 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
998 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1000 next
-= (unsigned long) dest
;
1008 edit_move_backward_lots (WEdit
*edit
, long increment
)
1013 if (increment
> edit
->curs1
)
1014 increment
= edit
->curs1
;
1017 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1019 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1022 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1027 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1028 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1033 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1034 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1035 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1036 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1039 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1040 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1041 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1046 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1048 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1050 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1051 g_malloc (EDIT_BUF_SIZE
);
1056 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1066 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1068 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1072 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1073 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1076 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1078 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1079 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1084 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1086 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1088 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1089 g_malloc (EDIT_BUF_SIZE
);
1094 return edit_get_byte (edit
, edit
->curs1
);
1097 #endif /* ! FAST_MOVE_CURSOR */
1099 /* moves the cursor right or left: increment positive or negative respectively */
1100 void edit_cursor_move (WEdit
* edit
, long increment
)
1102 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1105 #ifdef FAST_MOVE_CURSOR
1106 if (increment
< -256) {
1107 edit
->force
|= REDRAW_PAGE
;
1108 edit_move_backward_lots (edit
, -increment
);
1111 #endif /* ! FAST_MOVE_CURSOR */
1113 if (increment
< 0) {
1114 for (; increment
< 0; increment
++) {
1118 edit_push_action (edit
, CURS_RIGHT
);
1120 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1121 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1122 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1123 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1125 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1126 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1127 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1128 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1133 edit
->force
|= REDRAW_LINE_BELOW
;
1137 } else if (increment
> 0) {
1138 for (; increment
> 0; increment
--) {
1142 edit_push_action (edit
, CURS_LEFT
);
1144 c
= edit_get_byte (edit
, edit
->curs1
);
1145 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1146 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1147 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1149 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1150 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1151 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1152 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1157 edit
->force
|= REDRAW_LINE_ABOVE
;
1163 /* These functions return positions relative to lines */
1165 /* returns index of last char on line + 1 */
1166 long edit_eol (WEdit
* edit
, long current
)
1168 if (current
< edit
->last_byte
) {
1170 if (edit_get_byte (edit
, current
) == '\n')
1173 return edit
->last_byte
;
1177 /* returns index of first char on line */
1178 long edit_bol (WEdit
* edit
, long current
)
1182 if (edit_get_byte (edit
, current
- 1) == '\n')
1190 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1193 if (upto
> edit
->last_byte
)
1194 upto
= edit
->last_byte
;
1197 while (current
< upto
)
1198 if (edit_get_byte (edit
, current
++) == '\n')
1204 /* If lines is zero this returns the count of lines from current to upto. */
1205 /* If upto is zero returns index of lines forward current. */
1206 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1209 return edit_count_lines (edit
, current
, upto
);
1215 next
= edit_eol (edit
, current
) + 1;
1216 if (next
> edit
->last_byte
)
1226 /* Returns offset of 'lines' lines up from current */
1227 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1231 current
= edit_bol (edit
, current
);
1232 while((lines
--) && current
!= 0)
1233 current
= edit_bol (edit
, current
- 1);
1237 /* If cols is zero this returns the count of columns from current to upto. */
1238 /* If upto is zero returns index of cols across from current. */
1239 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1248 q
= edit
->last_byte
+ 2;
1250 for (col
= 0, p
= current
; p
< q
; p
++) {
1258 c
= edit_get_byte (edit
, p
);
1260 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1261 else if (c
== '\n') {
1266 } else if (c
< 32 || c
== 127)
1267 col
+= 2; /* Caret notation for control characters */
1274 /* returns the current column position of the cursor */
1275 int edit_get_col (WEdit
* edit
)
1277 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1281 /* Scrolling functions */
1283 void edit_update_curs_row (WEdit
* edit
)
1285 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1288 void edit_update_curs_col (WEdit
* edit
)
1290 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1293 /*moves the display start position up by i lines */
1294 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1296 unsigned long lines_above
= edit
->start_line
;
1297 if (i
> lines_above
)
1300 edit
->start_line
-= i
;
1301 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1302 edit
->force
|= REDRAW_PAGE
;
1303 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1305 edit_update_curs_row (edit
);
1309 /* returns 1 if could scroll, 0 otherwise */
1310 void edit_scroll_downward (WEdit
* edit
, int i
)
1313 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1314 if (lines_below
> 0) {
1315 if (i
> lines_below
)
1317 edit
->start_line
+= i
;
1318 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1319 edit
->force
|= REDRAW_PAGE
;
1320 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1322 edit_update_curs_row (edit
);
1325 void edit_scroll_right (WEdit
* edit
, int i
)
1327 edit
->force
|= REDRAW_PAGE
;
1328 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1329 edit
->start_col
-= i
;
1332 void edit_scroll_left (WEdit
* edit
, int i
)
1334 if (edit
->start_col
) {
1335 edit
->start_col
+= i
;
1336 if (edit
->start_col
> 0)
1337 edit
->start_col
= 0;
1338 edit
->force
|= REDRAW_PAGE
;
1339 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1343 /* high level cursor movement commands */
1345 static int is_in_indent (WEdit
*edit
)
1347 long p
= edit_bol (edit
, edit
->curs1
);
1348 while (p
< edit
->curs1
)
1349 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1354 static int left_of_four_spaces (WEdit
*edit
);
1357 edit_move_to_prev_col (WEdit
* edit
, long p
)
1359 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1361 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1362 edit_update_curs_col (edit
);
1364 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1365 int q
= edit
->curs_col
;
1366 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1367 p
= edit_bol (edit
, edit
->curs1
);
1368 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1369 if (!left_of_four_spaces (edit
))
1370 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1377 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1379 unsigned long p
, l
= edit
->curs_line
;
1385 edit
->force
|= REDRAW_PAGE
;
1387 edit_scroll_upward (edit
, i
);
1389 p
= edit_bol (edit
, edit
->curs1
);
1390 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1391 edit_move_to_prev_col (edit
, p
);
1393 edit
->search_start
= edit
->curs1
;
1394 edit
->found_len
= 0;
1399 is_blank (WEdit
*edit
, long offset
)
1403 s
= edit_bol (edit
, offset
);
1404 f
= edit_eol (edit
, offset
) - 1;
1406 c
= edit_get_byte (edit
, s
++);
1414 /* returns the offset of line i */
1416 edit_find_line (WEdit
*edit
, int line
)
1420 if (!edit
->caches_valid
) {
1421 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1422 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1423 /* three offsets that we *know* are line 0 at 0 and these two: */
1424 edit
->line_numbers
[1] = edit
->curs_line
;
1425 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1426 edit
->line_numbers
[2] = edit
->total_lines
;
1427 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1428 edit
->caches_valid
= 1;
1430 if (line
>= edit
->total_lines
)
1431 return edit
->line_offsets
[2];
1434 /* find the closest known point */
1435 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1437 n
= abs (edit
->line_numbers
[i
] - line
);
1444 return edit
->line_offsets
[j
]; /* know the offset exactly */
1445 if (m
== 1 && j
>= 3)
1446 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1448 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1449 if (line
> edit
->line_numbers
[j
])
1450 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1452 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1453 edit
->line_numbers
[i
] = line
;
1454 return edit
->line_offsets
[i
];
1457 int line_is_blank (WEdit
* edit
, long line
)
1459 return is_blank (edit
, edit_find_line (edit
, line
));
1462 /* moves up until a blank line is reached, or until just
1463 before a non-blank line is reached */
1464 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1467 if (edit
->curs_line
<= 1) {
1470 if (line_is_blank (edit
, edit
->curs_line
)) {
1471 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1472 for (i
= edit
->curs_line
- 1; i
; i
--)
1473 if (!line_is_blank (edit
, i
)) {
1478 for (i
= edit
->curs_line
- 1; i
; i
--)
1479 if (line_is_blank (edit
, i
))
1483 for (i
= edit
->curs_line
- 1; i
; i
--)
1484 if (line_is_blank (edit
, i
))
1488 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1492 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1494 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1500 edit
->force
|= REDRAW_PAGE
;
1502 edit_scroll_downward (edit
, i
);
1503 p
= edit_bol (edit
, edit
->curs1
);
1504 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1505 edit_move_to_prev_col (edit
, p
);
1507 edit
->search_start
= edit
->curs1
;
1508 edit
->found_len
= 0;
1512 /* moves down until a blank line is reached, or until just
1513 before a non-blank line is reached */
1514 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1517 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1518 i
= edit
->total_lines
;
1520 if (line_is_blank (edit
, edit
->curs_line
)) {
1521 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1522 for (i
= edit
->curs_line
+ 1; i
; i
++)
1523 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 for (i
= edit
->curs_line
+ 1; i
; i
++)
1534 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1538 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1541 static void edit_begin_page (WEdit
*edit
)
1543 edit_update_curs_row (edit
);
1544 edit_move_up (edit
, edit
->curs_row
, 0);
1547 static void edit_end_page (WEdit
*edit
)
1549 edit_update_curs_row (edit
);
1550 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1554 /* goto beginning of text */
1555 static void edit_move_to_top (WEdit
* edit
)
1557 if (edit
->curs_line
) {
1558 edit_cursor_move (edit
, -edit
->curs1
);
1559 edit_move_to_prev_col (edit
, 0);
1560 edit
->force
|= REDRAW_PAGE
;
1561 edit
->search_start
= 0;
1562 edit_update_curs_row(edit
);
1567 /* goto end of text */
1568 static void edit_move_to_bottom (WEdit
* edit
)
1570 if (edit
->curs_line
< edit
->total_lines
) {
1571 edit_cursor_move (edit
, edit
->curs2
);
1572 edit
->start_display
= edit
->last_byte
;
1573 edit
->start_line
= edit
->total_lines
;
1574 edit_update_curs_row(edit
);
1575 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1576 edit
->force
|= REDRAW_PAGE
;
1580 /* goto beginning of line */
1581 static void edit_cursor_to_bol (WEdit
* edit
)
1583 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1584 edit
->search_start
= edit
->curs1
;
1585 edit
->prev_col
= edit_get_col (edit
);
1588 /* goto end of line */
1589 static void edit_cursor_to_eol (WEdit
* edit
)
1591 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1592 edit
->search_start
= edit
->curs1
;
1593 edit
->prev_col
= edit_get_col (edit
);
1596 /* move cursor to line 'line' */
1597 void edit_move_to_line (WEdit
* e
, long line
)
1599 if(line
< e
->curs_line
)
1600 edit_move_up (e
, e
->curs_line
- line
, 0);
1602 edit_move_down (e
, line
- e
->curs_line
, 0);
1603 edit_scroll_screen_over_cursor (e
);
1606 /* scroll window so that first visible line is 'line' */
1607 void edit_move_display (WEdit
* e
, long line
)
1609 if(line
< e
->start_line
)
1610 edit_scroll_upward (e
, e
->start_line
- line
);
1612 edit_scroll_downward (e
, line
- e
->start_line
);
1615 /* save markers onto undo stack */
1616 void edit_push_markers (WEdit
* edit
)
1618 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1619 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1622 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1631 /* highlight marker toggle */
1632 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1634 edit_push_markers (edit
);
1636 edit_set_markers (edit
, 0, 0, 0, 0);
1637 edit
->force
|= REDRAW_PAGE
;
1639 if (edit
->mark2
>= 0) {
1640 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1641 edit
->force
|= REDRAW_PAGE
;
1643 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1647 static unsigned long
1652 const char option_chars_move_whole_word
[] =
1653 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1658 if (*option_chars_move_whole_word
== '!')
1660 return 0x80000000UL
;
1664 else if (islower (c
))
1666 else if (isalpha (c
))
1668 else if (isdigit (c
))
1670 else if (isspace (c
))
1672 q
= strchr (option_chars_move_whole_word
, c
);
1674 return 0xFFFFFFFFUL
;
1676 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1680 } while ((q
= strchr (q
+ 1, c
)));
1685 edit_left_word_move (WEdit
*edit
, int s
)
1689 edit_cursor_move (edit
, -1);
1692 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1693 c2
= edit_get_byte (edit
, edit
->curs1
);
1694 if (!(my_type_of (c1
) & my_type_of (c2
)))
1696 if (isspace (c1
) && !isspace (c2
))
1699 if (!isspace (c1
) && isspace (c2
))
1704 static void edit_left_word_move_cmd (WEdit
* edit
)
1706 edit_left_word_move (edit
, 0);
1707 edit
->force
|= REDRAW_PAGE
;
1711 edit_right_word_move (WEdit
*edit
, int s
)
1715 edit_cursor_move (edit
, 1);
1716 if (edit
->curs1
>= edit
->last_byte
)
1718 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1719 c2
= edit_get_byte (edit
, edit
->curs1
);
1720 if (!(my_type_of (c1
) & my_type_of (c2
)))
1722 if (isspace (c1
) && !isspace (c2
))
1725 if (!isspace (c1
) && isspace (c2
))
1730 static void edit_right_word_move_cmd (WEdit
* edit
)
1732 edit_right_word_move (edit
, 0);
1733 edit
->force
|= REDRAW_PAGE
;
1737 static void edit_right_delete_word (WEdit
* edit
)
1741 if (edit
->curs1
>= edit
->last_byte
)
1743 c1
= edit_delete (edit
);
1744 c2
= edit_get_byte (edit
, edit
->curs1
);
1745 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1747 if (!(my_type_of (c1
) & my_type_of (c2
)))
1752 static void edit_left_delete_word (WEdit
* edit
)
1756 if (edit
->curs1
<= 0)
1758 c1
= edit_backspace (edit
);
1759 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1760 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1762 if (!(my_type_of (c1
) & my_type_of (c2
)))
1768 the start column position is not recorded, and hence does not
1769 undo as it happed. But who would notice.
1772 edit_do_undo (WEdit
* edit
)
1777 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1779 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1784 edit_cursor_move (edit
, 1);
1787 edit_cursor_move (edit
, -1);
1790 edit_backspace (edit
);
1796 column_highlighting
= 1;
1799 column_highlighting
= 0;
1802 if (ac
>= 256 && ac
< 512)
1803 edit_insert_ahead (edit
, ac
- 256);
1804 if (ac
>= 0 && ac
< 256)
1805 edit_insert (edit
, ac
);
1807 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1808 edit
->mark1
= ac
- MARK_1
;
1809 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1810 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1811 edit
->mark2
= ac
- MARK_2
;
1812 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1815 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1818 if (edit
->start_display
> ac
- KEY_PRESS
) {
1819 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1820 edit
->force
|= REDRAW_PAGE
;
1821 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1822 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1823 edit
->force
|= REDRAW_PAGE
;
1825 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1826 edit_update_curs_row (edit
);
1829 edit
->stack_disable
= 0;
1832 static void edit_delete_to_line_end (WEdit
* edit
)
1834 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1841 static void edit_delete_to_line_begin (WEdit
* edit
)
1843 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1846 edit_backspace (edit
);
1851 edit_delete_line (WEdit
*edit
)
1854 * Delete right part of the line.
1855 * Note that edit_get_byte() returns '\n' when byte position is
1858 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1859 (void) edit_delete (edit
);
1864 * Note that edit_delete() will not corrupt anything if called while
1865 * cursor position is EOF.
1867 (void) edit_delete (edit
);
1870 * Delete left part of the line.
1871 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1873 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1874 (void) edit_backspace (edit
);
1878 static void insert_spaces_tab (WEdit
* edit
, int half
)
1881 edit_update_curs_col (edit
);
1882 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1884 edit_insert (edit
, ' ');
1889 static int is_aligned_on_a_tab (WEdit
* edit
)
1891 edit_update_curs_col (edit
);
1892 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1893 return 0; /* not alligned on a tab */
1897 static int right_of_four_spaces (WEdit
*edit
)
1900 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1901 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1903 return is_aligned_on_a_tab (edit
);
1907 static int left_of_four_spaces (WEdit
*edit
)
1910 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1911 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1913 return is_aligned_on_a_tab (edit
);
1917 int edit_indent_width (WEdit
* edit
, long p
)
1920 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1922 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1925 void edit_insert_indent (WEdit
* edit
, int indent
)
1927 if (!option_fill_tabs_with_spaces
) {
1928 while (indent
>= TAB_SIZE
) {
1929 edit_insert (edit
, '\t');
1933 while (indent
-- > 0)
1934 edit_insert (edit
, ' ');
1938 edit_auto_indent (WEdit
* edit
)
1943 /* use the previous line as a template */
1944 p
= edit_move_backward (edit
, p
, 1);
1945 /* copy the leading whitespace of the line */
1946 for (;;) { /* no range check - the line _is_ \n-terminated */
1947 c
= edit_get_byte (edit
, p
++);
1948 if (c
!= ' ' && c
!= '\t')
1950 edit_insert (edit
, c
);
1954 static void edit_double_newline (WEdit
* edit
)
1956 edit_insert (edit
, '\n');
1957 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1959 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1961 edit
->force
|= REDRAW_PAGE
;
1962 edit_insert (edit
, '\n');
1965 static void edit_tab_cmd (WEdit
* edit
)
1969 if (option_fake_half_tabs
) {
1970 if (is_in_indent (edit
)) {
1971 /*insert a half tab (usually four spaces) unless there is a
1972 half tab already behind, then delete it and insert a
1974 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1975 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1976 edit_backspace (edit
);
1977 edit_insert (edit
, '\t');
1979 insert_spaces_tab (edit
, 1);
1984 if (option_fill_tabs_with_spaces
) {
1985 insert_spaces_tab (edit
, 0);
1987 edit_insert (edit
, '\t');
1992 static void check_and_wrap_line (WEdit
* edit
)
1995 if (!option_typewriter_wrap
)
1997 edit_update_curs_col (edit
);
1998 if (edit
->curs_col
< option_word_wrap_line_length
)
2003 c
= edit_get_byte (edit
, curs
);
2004 if (c
== '\n' || curs
<= 0) {
2005 edit_insert (edit
, '\n');
2008 if (c
== ' ' || c
== '\t') {
2009 int current
= edit
->curs1
;
2010 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2011 edit_insert (edit
, '\n');
2012 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2018 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2020 void edit_push_key_press (WEdit
* edit
)
2022 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2023 if (edit
->mark2
== -1)
2024 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2027 /* this find the matching bracket in either direction, and sets edit->bracket */
2028 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2030 const char * const b
= "{}{[][()(", *p
;
2031 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2032 unsigned long j
= 0;
2034 edit_update_curs_row (edit
);
2035 c
= edit_get_byte (edit
, edit
->curs1
);
2038 if (!furthest_bracket_search
)
2039 furthest_bracket_search
--;
2040 /* not on a bracket at all */
2043 /* the matching bracket */
2045 /* going left or right? */
2046 if (strchr ("{[(", c
))
2048 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2049 /* out of buffer? */
2050 if (q
>= edit
->last_byte
|| q
< 0)
2052 a
= edit_get_byte (edit
, q
);
2053 /* don't want to eat CPU */
2054 if (j
++ > furthest_bracket_search
)
2056 /* out of screen? */
2058 if (q
< edit
->start_display
)
2060 /* count lines if searching downward */
2061 if (inc
> 0 && a
== '\n')
2062 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2065 /* count bracket depth */
2066 i
+= (a
== c
) - (a
== d
);
2067 /* return if bracket depth is zero */
2075 static long last_bracket
= -1;
2077 void edit_find_bracket (WEdit
* edit
)
2079 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2080 if (last_bracket
!= edit
->bracket
)
2081 edit
->force
|= REDRAW_PAGE
;
2082 last_bracket
= edit
->bracket
;
2085 static void edit_goto_matching_bracket (WEdit
*edit
)
2088 q
= edit_get_bracket (edit
, 0, 0);
2091 edit
->bracket
= edit
->curs1
;
2092 edit
->force
|= REDRAW_PAGE
;
2093 edit_cursor_move (edit
, q
- edit
->curs1
);
2097 * This executes a command as though the user initiated it through a key
2098 * press. Callback with WIDGET_KEY as a message calls this after
2099 * translating the key press. This function can be used to pass any
2100 * command to the editor. Note that the screen wouldn't update
2101 * automatically. Either of command or char_for_insertion must be
2102 * passed as -1. Commands are executed, and char_for_insertion is
2103 * inserted at the cursor.
2105 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2107 if (command
== CK_Begin_Record_Macro
) {
2109 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2112 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2113 edit
->force
|= REDRAW_COMPLETELY
;
2114 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2118 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2119 edit
->macro
[edit
->macro_i
].command
= command
;
2120 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2122 /* record the beginning of a set of editing actions initiated by a key press */
2123 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2124 edit_push_key_press (edit
);
2126 edit_execute_cmd (edit
, command
, char_for_insertion
);
2127 if (column_highlighting
)
2128 edit
->force
|= REDRAW_PAGE
;
2131 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2134 This executes a command at a lower level than macro recording.
2135 It also does not push a key_press onto the undo stack. This means
2136 that if it is called many times, a single undo command will undo
2137 all of them. It also does not check for the Undo command.
2140 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2142 edit
->force
|= REDRAW_LINE
;
2144 /* The next key press will unhighlight the found string, so update
2146 if (edit
->found_len
|| column_highlighting
)
2147 edit
->force
|= REDRAW_PAGE
;
2149 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2150 column_highlighting
= 0;
2151 if (!edit
->highlight
2152 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2153 edit_mark_cmd (edit
, 1); /* clear */
2154 edit_mark_cmd (edit
, 0); /* marking on */
2156 edit
->highlight
= 1;
2157 } else { /* any other command */
2158 if (edit
->highlight
)
2159 edit_mark_cmd (edit
, 0); /* clear */
2160 edit
->highlight
= 0;
2163 /* first check for undo */
2164 if (command
== CK_Undo
) {
2165 edit_do_undo (edit
);
2166 edit
->found_len
= 0;
2167 edit
->prev_col
= edit_get_col (edit
);
2168 edit
->search_start
= edit
->curs1
;
2172 /* An ordinary key press */
2173 if (char_for_insertion
>= 0) {
2174 if (edit
->overwrite
) {
2175 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2178 edit_insert (edit
, char_for_insertion
);
2179 if (option_auto_para_formatting
) {
2180 format_paragraph (edit
, 0);
2181 edit
->force
|= REDRAW_PAGE
;
2183 check_and_wrap_line (edit
);
2184 edit
->found_len
= 0;
2185 edit
->prev_col
= edit_get_col (edit
);
2186 edit
->search_start
= edit
->curs1
;
2187 edit_find_bracket (edit
);
2193 case CK_Begin_Page_Highlight
:
2194 case CK_End_Page_Highlight
:
2199 case CK_Word_Left_Highlight
:
2200 case CK_Word_Right_Highlight
:
2201 case CK_Up_Highlight
:
2202 case CK_Down_Highlight
:
2203 if (edit
->mark2
== -1)
2204 break; /*marking is following the cursor: may need to highlight a whole line */
2207 case CK_Left_Highlight
:
2208 case CK_Right_Highlight
:
2209 edit
->force
|= REDRAW_CHAR_ONLY
;
2212 /* basic cursor key commands */
2215 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2216 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2218 edit_backspace (edit
);
2221 if (option_fake_half_tabs
) {
2223 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2224 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2225 edit_backspace (edit
);
2230 edit_backspace (edit
);
2233 if (option_fake_half_tabs
) {
2235 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2236 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2243 case CK_Delete_Word_Left
:
2244 edit_left_delete_word (edit
);
2246 case CK_Delete_Word_Right
:
2247 edit_right_delete_word (edit
);
2249 case CK_Delete_Line
:
2250 edit_delete_line (edit
);
2252 case CK_Delete_To_Line_End
:
2253 edit_delete_to_line_end (edit
);
2255 case CK_Delete_To_Line_Begin
:
2256 edit_delete_to_line_begin (edit
);
2259 if (option_auto_para_formatting
) {
2260 edit_double_newline (edit
);
2261 if (option_return_does_auto_indent
)
2262 edit_auto_indent (edit
);
2263 format_paragraph (edit
, 0);
2265 edit_insert (edit
, '\n');
2266 if (option_return_does_auto_indent
) {
2267 edit_auto_indent (edit
);
2272 edit_insert (edit
, '\n');
2276 case CK_Page_Up_Highlight
:
2277 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2280 case CK_Page_Down_Highlight
:
2281 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2284 case CK_Left_Highlight
:
2285 if (option_fake_half_tabs
) {
2286 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2287 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2288 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2292 edit_cursor_move (edit
, -1);
2295 case CK_Right_Highlight
:
2296 if (option_fake_half_tabs
) {
2297 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2298 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2299 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2303 edit_cursor_move (edit
, 1);
2306 case CK_Begin_Page_Highlight
:
2307 edit_begin_page (edit
);
2310 case CK_End_Page_Highlight
:
2311 edit_end_page (edit
);
2314 case CK_Word_Left_Highlight
:
2315 edit_left_word_move_cmd (edit
);
2318 case CK_Word_Right_Highlight
:
2319 edit_right_word_move_cmd (edit
);
2322 case CK_Up_Highlight
:
2323 edit_move_up (edit
, 1, 0);
2326 case CK_Down_Highlight
:
2327 edit_move_down (edit
, 1, 0);
2329 case CK_Paragraph_Up
:
2330 case CK_Paragraph_Up_Highlight
:
2331 edit_move_up_paragraph (edit
, 0);
2333 case CK_Paragraph_Down
:
2334 case CK_Paragraph_Down_Highlight
:
2335 edit_move_down_paragraph (edit
, 0);
2338 case CK_Scroll_Up_Highlight
:
2339 edit_move_up (edit
, 1, 1);
2341 case CK_Scroll_Down
:
2342 case CK_Scroll_Down_Highlight
:
2343 edit_move_down (edit
, 1, 1);
2346 case CK_Home_Highlight
:
2347 edit_cursor_to_bol (edit
);
2350 case CK_End_Highlight
:
2351 edit_cursor_to_eol (edit
);
2355 edit_tab_cmd (edit
);
2356 if (option_auto_para_formatting
) {
2357 format_paragraph (edit
, 0);
2358 edit
->force
|= REDRAW_PAGE
;
2360 check_and_wrap_line (edit
);
2363 case CK_Toggle_Insert
:
2364 edit
->overwrite
= (edit
->overwrite
== 0);
2368 if (edit
->mark2
>= 0) {
2369 if (column_highlighting
)
2370 edit_push_action (edit
, COLUMN_ON
);
2371 column_highlighting
= 0;
2373 edit_mark_cmd (edit
, 0);
2375 case CK_Column_Mark
:
2376 if (!column_highlighting
)
2377 edit_push_action (edit
, COLUMN_OFF
);
2378 column_highlighting
= 1;
2379 edit_mark_cmd (edit
, 0);
2382 if (column_highlighting
)
2383 edit_push_action (edit
, COLUMN_ON
);
2384 column_highlighting
= 0;
2385 edit_mark_cmd (edit
, 1);
2388 case CK_Toggle_Bookmark
:
2389 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2390 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2391 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2393 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2395 case CK_Flush_Bookmarks
:
2396 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2397 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2398 edit
->force
|= REDRAW_PAGE
;
2400 case CK_Next_Bookmark
:
2401 if (edit
->book_mark
) {
2402 struct _book_mark
*p
;
2403 p
= (struct _book_mark
*) book_mark_find (edit
,
2407 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2408 || p
->line
< edit
->start_line
)
2409 edit_move_display (edit
,
2411 edit
->num_widget_lines
/ 2);
2412 edit_move_to_line (edit
, p
->line
);
2416 case CK_Prev_Bookmark
:
2417 if (edit
->book_mark
) {
2418 struct _book_mark
*p
;
2419 p
= (struct _book_mark
*) book_mark_find (edit
,
2421 while (p
->line
== edit
->curs_line
)
2425 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2426 || p
->line
< edit
->start_line
)
2427 edit_move_display (edit
,
2429 edit
->num_widget_lines
/ 2);
2430 edit_move_to_line (edit
, p
->line
);
2435 case CK_Beginning_Of_Text
:
2436 case CK_Beginning_Of_Text_Highlight
:
2437 edit_move_to_top (edit
);
2439 case CK_End_Of_Text
:
2440 case CK_End_Of_Text_Highlight
:
2441 edit_move_to_bottom (edit
);
2445 edit_block_copy_cmd (edit
);
2448 edit_block_delete_cmd (edit
);
2451 edit_block_move_cmd (edit
);
2455 edit_copy_to_X_buf_cmd (edit
);
2458 edit_cut_to_X_buf_cmd (edit
);
2461 edit_paste_from_X_buf_cmd (edit
);
2463 case CK_Selection_History
:
2464 edit_paste_from_history (edit
);
2468 edit_save_as_cmd (edit
);
2471 edit_save_confirm_cmd (edit
);
2474 edit_load_cmd (edit
);
2477 edit_save_block_cmd (edit
);
2479 case CK_Insert_File
:
2480 edit_insert_file_cmd (edit
);
2483 case CK_Toggle_Syntax
:
2484 if ((option_syntax_highlighting
^= 1) == 1)
2485 edit_load_syntax (edit
, NULL
, option_syntax_type
);
2486 edit
->force
|= REDRAW_PAGE
;
2490 edit_search_cmd (edit
, 0);
2493 edit_search_cmd (edit
, 1);
2496 edit_replace_cmd (edit
, 0);
2498 case CK_Replace_Again
:
2499 edit_replace_cmd (edit
, 1);
2501 case CK_Complete_Word
:
2502 edit_complete_word_cmd (edit
);
2506 dlg_stop (edit
->widget
.parent
);
2509 edit_new_cmd (edit
);
2513 edit_help_cmd (edit
);
2517 edit_refresh_cmd (edit
);
2522 /* fool gcc to prevent a Y2K warning */
2523 char time_format
[] = "_c";
2524 time_format
[0] = '%';
2526 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
2527 edit_print_string (edit
, s
);
2528 edit
->force
|= REDRAW_PAGE
;
2532 edit_goto_cmd (edit
);
2534 case CK_Paragraph_Format
:
2535 format_paragraph (edit
, 1);
2536 edit
->force
|= REDRAW_PAGE
;
2538 case CK_Delete_Macro
:
2539 edit_delete_macro_cmd (edit
);
2541 case CK_Match_Bracket
:
2542 edit_goto_matching_bracket (edit
);
2548 edit_sort_cmd (edit
);
2551 edit_ext_cmd (edit
);
2554 edit_mail_dialog (edit
);
2559 case CK_Select_Codepage
:
2560 edit_select_codepage_cmd (edit
);
2562 case CK_Insert_Literal
:
2563 edit_insert_literal_cmd (edit
);
2565 case CK_Execute_Macro
:
2566 edit_execute_macro_cmd (edit
);
2568 case CK_Begin_End_Macro
:
2569 edit_begin_end_macro_cmd (edit
);
2579 if ((command
/ 1000) == 1) /* a shell command */
2580 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2581 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2582 struct macro m
[MAX_MACRO_LENGTH
];
2584 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
2585 edit_execute_macro (edit
, m
, nm
);
2588 /* keys which must set the col position, and the search vars */
2593 case CK_Replace_Again
:
2594 case CK_Complete_Word
:
2595 edit
->prev_col
= edit_get_col (edit
);
2598 case CK_Up_Highlight
:
2600 case CK_Down_Highlight
:
2602 case CK_Page_Up_Highlight
:
2604 case CK_Page_Down_Highlight
:
2605 case CK_Beginning_Of_Text
:
2606 case CK_Beginning_Of_Text_Highlight
:
2607 case CK_End_Of_Text
:
2608 case CK_End_Of_Text_Highlight
:
2609 case CK_Paragraph_Up
:
2610 case CK_Paragraph_Up_Highlight
:
2611 case CK_Paragraph_Down
:
2612 case CK_Paragraph_Down_Highlight
:
2614 case CK_Scroll_Up_Highlight
:
2615 case CK_Scroll_Down
:
2616 case CK_Scroll_Down_Highlight
:
2617 edit
->search_start
= edit
->curs1
;
2618 edit
->found_len
= 0;
2621 edit
->found_len
= 0;
2622 edit
->prev_col
= edit_get_col (edit
);
2623 edit
->search_start
= edit
->curs1
;
2625 edit_find_bracket (edit
);
2627 if (option_auto_para_formatting
) {
2631 case CK_Delete_Word_Left
:
2632 case CK_Delete_Word_Right
:
2633 case CK_Delete_To_Line_End
:
2634 case CK_Delete_To_Line_Begin
:
2635 format_paragraph (edit
, 0);
2636 edit
->force
|= REDRAW_PAGE
;
2643 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2647 if (edit
->macro_depth
++ > 256) {
2648 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2649 edit
->macro_depth
--;
2652 edit
->force
|= REDRAW_PAGE
;
2653 for (; i
< n
; i
++) {
2654 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2656 edit_update_screen (edit
);
2657 edit
->macro_depth
--;
2660 /* User edit menu, like user menu (F2) but only in editor. */
2662 user_menu (WEdit
* edit
)
2667 long start_mark
, end_mark
;
2668 char *block_file
= concat_dir_and_file (home_dir
, BLOCK_FILE
);
2671 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2672 if (!nomark
) /* remember marked or not */
2673 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2675 /* run shell scripts from menu */
2676 user_menu_cmd (edit
);
2678 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2679 /* no block messages */
2684 /* i.e. we have marked block */
2685 rc
= edit_block_delete_cmd (edit
);
2689 edit_insert_file (edit
, block_file
);
2692 /* truncate block file */
2693 if ((fd
= fopen (block_file
, "w"))) {
2697 edit_refresh_cmd (edit
);
2698 edit
->force
|= REDRAW_COMPLETELY
;
2701 g_free (block_file
);