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
28 #include <sys/types.h>
36 #include <mhl/memory.h>
37 #include <mhl/string.h>
39 #include "../src/global.h"
43 #include "edit-widget.h"
44 #include "editcmddef.h"
47 #include "../src/cmd.h" /* view_other_cmd() */
48 #include "../src/user.h" /* user_menu_cmd() */
49 #include "../src/wtools.h" /* query_dialog() */
50 #include "../src/timefmt.h" /* time formatting */
54 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
55 or EDIT_KEY_EMULATION_EMACS
57 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
59 int option_word_wrap_line_length
= 72;
60 int option_typewriter_wrap
= 0;
61 int option_auto_para_formatting
= 0;
62 int option_tab_spacing
= 8;
63 int option_fill_tabs_with_spaces
= 0;
64 int option_return_does_auto_indent
= 1;
65 int option_backspace_through_tabs
= 0;
66 int option_fake_half_tabs
= 1;
67 int option_save_mode
= EDIT_QUICK_SAVE
;
68 int option_save_position
= 1;
69 int option_max_undo
= 32768;
71 int option_edit_right_extreme
= 0;
72 int option_edit_left_extreme
= 0;
73 int option_edit_top_extreme
= 0;
74 int option_edit_bottom_extreme
= 0;
76 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
77 char *option_backup_ext
= NULL
;
81 * here's a quick sketch of the layout: (don't run this through indent.)
83 * (b1 is buffers1 and b2 is buffers2)
86 * \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
87 * ______________________________________|______________________________________
89 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
90 * |-> |-> |-> |-> |-> |-> |
92 * _<------------------------->|<----------------->_
93 * WEdit->curs2 | WEdit->curs1
98 * file end|||file beginning
108 static void user_menu (WEdit
*edit
);
110 int edit_get_byte (WEdit
* edit
, long byte_index
)
113 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
116 if (byte_index
>= edit
->curs1
) {
117 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
118 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
120 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
125 * Initialize the buffers for an empty files.
128 edit_init_buffers (WEdit
*edit
)
132 for (j
= 0; j
<= MAXBUFF
; j
++) {
133 edit
->buffers1
[j
] = NULL
;
134 edit
->buffers2
[j
] = NULL
;
139 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
143 * Load file OR text into buffers. Set cursor to the beginning of file.
147 edit_load_file_fast (WEdit
*edit
, const char *filename
)
152 edit
->curs2
= edit
->last_byte
;
153 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
155 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
157 snprintf(errmsg
, sizeof(errmsg
), _(" Cannot open %s for reading "), filename
);
158 edit_error_dialog (_("Error"), get_sys_error (errmsg
));
162 if (!edit
->buffers2
[buf2
])
163 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
166 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
167 (edit
->curs2
& M_EDIT_BUF_SIZE
),
168 edit
->curs2
& M_EDIT_BUF_SIZE
);
170 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
171 /* edit->buffers2[0] is already allocated */
172 if (!edit
->buffers2
[buf
])
173 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
174 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
181 /* detecting an error on save is easy: just check if every byte has been written. */
182 /* detecting an error on read, is not so easy 'cos there is not way to tell
183 whether you read everything or not. */
184 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
185 static const struct edit_filters
{
186 const char *read
, *write
, *extension
;
188 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
189 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
190 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
193 /* Return index of the filter or -1 is there is no appropriate filter */
194 static int edit_find_filter (const char *filename
)
199 l
= strlen (filename
);
200 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
201 e
= strlen (all_filters
[i
].extension
);
203 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
210 edit_get_filter (const char *filename
)
213 char *p
, *quoted_name
;
214 i
= edit_find_filter (filename
);
217 quoted_name
= name_quote (filename
, 0);
218 l
= strlen (quoted_name
);
219 p
= g_malloc (strlen (all_filters
[i
].read
) + l
+ 2);
220 sprintf (p
, all_filters
[i
].read
, quoted_name
);
221 mhl_mem_free (quoted_name
);
226 edit_get_write_filter (const char *write_name
, const char *filename
)
230 i
= edit_find_filter (filename
);
233 writename
= name_quote (write_name
, 0);
234 l
= strlen (writename
);
235 p
= g_malloc (strlen (all_filters
[i
].write
) + l
+ 2);
236 sprintf (p
, all_filters
[i
].write
, writename
);
237 mhl_mem_free (writename
);
242 edit_insert_stream (WEdit
* edit
, FILE * f
)
246 while ((c
= fgetc (f
)) >= 0) {
247 edit_insert (edit
, c
);
253 long edit_write_stream (WEdit
* edit
, FILE * f
)
256 for (i
= 0; i
< edit
->last_byte
; i
++)
257 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
262 #define TEMP_BUF_LEN 1024
264 /* inserts a file at the cursor, returns 1 on success */
266 edit_insert_file (WEdit
*edit
, const char *filename
)
269 if ((p
= edit_get_filter (filename
))) {
271 long current
= edit
->curs1
;
272 f
= (FILE *) popen (p
, "r");
274 edit_insert_stream (edit
, f
);
275 edit_cursor_move (edit
, current
- edit
->curs1
);
276 if (pclose (f
) > 0) {
277 GString
*errmsg
= g_string_new (NULL
);
278 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
279 edit_error_dialog (_("Error"), errmsg
->str
);
280 g_string_free (errmsg
, TRUE
);
285 GString
*errmsg
= g_string_new (NULL
);
286 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
287 edit_error_dialog (_("Error"), errmsg
->str
);
288 g_string_free (errmsg
, TRUE
);
294 int i
, file
, blocklen
;
295 long current
= edit
->curs1
;
297 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
299 buf
= g_malloc (TEMP_BUF_LEN
);
300 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
301 for (i
= 0; i
< blocklen
; i
++)
302 edit_insert (edit
, buf
[i
]);
304 edit_cursor_move (edit
, current
- edit
->curs1
);
313 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
315 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
321 /* Try opening an existing file */
322 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
326 * Try creating the file. O_EXCL prevents following broken links
327 * and opening existing files.
331 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
334 snprintf (errmsg
, sizeof(errmsg
), _(" Cannot open %s for reading "), filename
);
337 /* New file, delete it if it's not modified or saved */
338 edit
->delete_file
= 1;
342 /* Check what we have opened */
343 if (mc_fstat (file
, st
) < 0) {
344 snprintf (errmsg
, sizeof(errmsg
), _(" Cannot get size/permissions for %s "), filename
);
348 /* We want to open regular files only */
349 if (!S_ISREG (st
->st_mode
)) {
350 snprintf (errmsg
, sizeof(errmsg
), _(" %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 snprintf (errmsg
, sizeof(errmsg
), _(" File %s is too large "), filename
);
368 (void) mc_close (file
);
370 edit_error_dialog (_("Error"), errmsg
);
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
);
448 mhl_mem_free (filename
);
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
);
467 mhl_mem_free (filename
);
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
,
494 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
499 * Expand option_whole_chars_search by national letters using
503 static char option_whole_chars_search_buf
[256];
505 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
507 size_t len
= strlen (option_whole_chars_search
);
509 strcpy (option_whole_chars_search_buf
,
510 option_whole_chars_search
);
512 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
513 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
514 option_whole_chars_search_buf
[len
++] = i
;
518 option_whole_chars_search_buf
[len
] = 0;
519 option_whole_chars_search
= option_whole_chars_search_buf
;
521 #endif /* ENABLE_NLS */
522 edit
= g_malloc0 (sizeof (WEdit
));
525 edit_purge_widget (edit
);
526 edit
->num_widget_lines
= lines
;
527 edit
->num_widget_columns
= columns
;
528 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
529 edit
->stat1
.st_uid
= getuid ();
530 edit
->stat1
.st_gid
= getgid ();
531 edit
->stat1
.st_mtime
= 0;
533 edit
->force
|= REDRAW_PAGE
;
534 edit_set_filename (edit
, filename
);
535 edit
->stack_size
= START_STACK_SIZE
;
536 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
537 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
538 if (edit_load_file (edit
)) {
539 /* edit_load_file already gives an error message */
544 edit
->loading_done
= 1;
547 edit_load_syntax (edit
, 0, 0);
550 edit_get_syntax_color (edit
, -1, &color
);
553 /* load saved cursor position */
554 if ((line
== 0) && option_save_position
) {
555 edit_load_position (edit
);
559 edit_move_display (edit
, line
- 1);
560 edit_move_to_line (edit
, line
- 1);
563 edit_load_user_map(edit
);
568 /* Clear the edit struct, freeing everything in it. Return 1 on success */
570 edit_clean (WEdit
*edit
)
577 /* a stale lock, remove it */
579 edit
->locked
= edit_unlock_file (edit
->filename
);
581 /* save cursor position */
582 if (option_save_position
)
583 edit_save_position (edit
);
585 /* File specified on the mcedit command line and never saved */
586 if (edit
->delete_file
)
587 unlink (edit
->filename
);
589 edit_free_syntax_rules (edit
);
590 book_mark_flush (edit
, -1);
591 for (; j
<= MAXBUFF
; j
++) {
592 mhl_mem_free (edit
->buffers1
[j
]);
593 mhl_mem_free (edit
->buffers2
[j
]);
596 mhl_mem_free (edit
->undo_stack
);
597 mhl_mem_free (edit
->filename
);
598 mhl_mem_free (edit
->dir
);
600 edit_purge_widget (edit
);
602 /* Free temporary strings used in catstrs() */
609 /* returns 1 on success */
610 int edit_renew (WEdit
* edit
)
612 int lines
= edit
->num_widget_lines
;
613 int columns
= edit
->num_widget_columns
;
617 if (!edit_init (edit
, lines
, columns
, "", 0))
623 * Load a new file into the editor. If it fails, preserve the old file.
624 * To do it, allocate a new widget, initialize it and, if the new file
625 * was loaded, copy the data to the old widget.
626 * Return 1 on success, 0 on failure.
629 edit_reload (WEdit
*edit
, const char *filename
)
632 int lines
= edit
->num_widget_lines
;
633 int columns
= edit
->num_widget_columns
;
635 e
= g_malloc0 (sizeof (WEdit
));
636 e
->widget
= edit
->widget
;
637 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
642 memcpy (edit
, e
, sizeof (WEdit
));
649 Recording stack for undo:
650 The following is an implementation of a compressed stack. Identical
651 pushes are recorded by a negative prefix indicating the number of times the
652 same char was pushed. This saves space for repeated curs-left or curs-right
669 If the stack long int is 0-255 it represents a normal insert (from a backspace),
670 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
671 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
672 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
675 The only way the cursor moves or the buffer is changed is through the routines:
676 insert, backspace, insert_ahead, delete, and cursor_move.
677 These record the reverse undo movements onto the stack each time they are
680 Each key press results in a set of actions (insert; delete ...). So each time
681 a key is pressed the current position of start_display is pushed as
682 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
683 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
684 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
688 void edit_push_action (WEdit
* edit
, long c
,...)
690 unsigned long sp
= edit
->stack_pointer
;
694 /* first enlarge the stack if necessary */
695 if (sp
> edit
->stack_size
- 10) { /* say */
696 if (option_max_undo
< 256)
697 option_max_undo
= 256;
698 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
699 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
701 edit
->undo_stack
= t
;
702 edit
->stack_size
<<= 1;
703 edit
->stack_size_mask
= edit
->stack_size
- 1;
707 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
708 if (edit
->stack_disable
)
711 #ifdef FAST_MOVE_CURSOR
712 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
714 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
715 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
717 c
= -(va_arg (ap
, int));
720 #endif /* ! FAST_MOVE_CURSOR */
721 if (edit
->stack_bottom
!= sp
722 && spm1
!= edit
->stack_bottom
723 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
725 if (edit
->undo_stack
[spm1
] < 0) {
726 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
728 if (edit
->undo_stack
[spm1
] > -1000000000) {
729 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
730 edit
->undo_stack
[spm1
]--;
734 /* #define NO_STACK_CURSMOVE_ANIHILATION */
735 #ifndef NO_STACK_CURSMOVE_ANIHILATION
736 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
737 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
738 if (edit
->undo_stack
[spm1
] == -2)
739 edit
->stack_pointer
= spm1
;
741 edit
->undo_stack
[spm1
]++;
746 d
= edit
->undo_stack
[spm1
];
749 return; /* --> no need to push multiple do-nothings */
750 edit
->undo_stack
[sp
] = -2;
753 #ifndef NO_STACK_CURSMOVE_ANIHILATION
754 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
755 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
756 edit
->stack_pointer
= spm1
;
762 edit
->undo_stack
[sp
] = c
;
765 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
767 /* if the sp wraps round and catches the stack_bottom then erase
768 * the first set of actions on the stack to make space - by moving
769 * stack_bottom forward one "key press" */
770 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
771 if ((unsigned long) c
== edit
->stack_bottom
||
772 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
774 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
775 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
777 /*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: */
778 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
779 edit
->stack_bottom
= edit
->stack_pointer
= 0;
783 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
784 then the file should be as it was when he loaded up. Then set edit->modified to 0.
787 pop_action (WEdit
* edit
)
790 unsigned long sp
= edit
->stack_pointer
;
791 if (sp
== edit
->stack_bottom
) {
794 sp
= (sp
- 1) & edit
->stack_size_mask
;
795 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
796 /* edit->undo_stack[sp] = '@'; */
797 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
800 if (sp
== edit
->stack_bottom
) {
803 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
804 if (edit
->undo_stack
[sp
] == -2) {
805 /* edit->undo_stack[sp] = '@'; */
806 edit
->stack_pointer
= sp
;
808 edit
->undo_stack
[sp
]++;
813 /* is called whenever a modification is made by one of the four routines below */
814 static inline void edit_modification (WEdit
* edit
)
816 edit
->caches_valid
= 0;
817 edit
->screen_modified
= 1;
819 /* raise lock when file modified */
820 if (!edit
->modified
&& !edit
->delete_file
)
821 edit
->locked
= edit_lock_file (edit
->filename
);
826 Basic low level single character buffer alterations and movements at the cursor.
827 Returns char passed over, inserted or removed.
831 edit_insert (WEdit
*edit
, int c
)
833 /* check if file has grown to large */
834 if (edit
->last_byte
>= SIZE_LIMIT
)
837 /* first we must update the position of the display window */
838 if (edit
->curs1
< edit
->start_display
) {
839 edit
->start_display
++;
844 /* Mark file as modified, unless the file hasn't been fully loaded */
845 if (edit
->loading_done
) {
846 edit_modification (edit
);
849 /* now we must update some info on the file and check if a redraw is required */
852 book_mark_inc (edit
, edit
->curs_line
);
855 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
858 /* save the reverse command onto the undo stack */
859 edit_push_action (edit
, BACKSPACE
);
862 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
863 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
864 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
866 /* add a new buffer if we've reached the end of the last one */
867 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
868 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
869 g_malloc (EDIT_BUF_SIZE
);
871 /* perform the insertion */
872 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
873 curs1
& M_EDIT_BUF_SIZE
]
876 /* update file length */
879 /* update cursor position */
884 /* same as edit_insert and move left */
885 void edit_insert_ahead (WEdit
* edit
, int c
)
887 if (edit
->last_byte
>= SIZE_LIMIT
)
889 if (edit
->curs1
< edit
->start_display
) {
890 edit
->start_display
++;
894 edit_modification (edit
);
897 book_mark_inc (edit
, edit
->curs_line
);
899 edit
->force
|= REDRAW_AFTER_CURSOR
;
901 edit_push_action (edit
, DELCHAR
);
903 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
904 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
905 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
907 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
908 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
909 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
916 int edit_delete (WEdit
* edit
)
922 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
923 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
924 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
926 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
928 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
929 mhl_mem_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
930 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
935 edit_modification (edit
);
938 book_mark_dec (edit
, edit
->curs_line
);
940 edit
->force
|= REDRAW_AFTER_CURSOR
;
942 edit_push_action (edit
, p
+ 256);
943 if (edit
->curs1
< edit
->start_display
) {
944 edit
->start_display
--;
954 edit_backspace (WEdit
* edit
)
960 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
961 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
962 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
964 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
965 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
966 mhl_mem_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
967 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
972 edit_modification (edit
);
975 book_mark_dec (edit
, edit
->curs_line
);
978 edit
->force
|= REDRAW_AFTER_CURSOR
;
980 edit_push_action (edit
, p
);
982 if (edit
->curs1
< edit
->start_display
) {
983 edit
->start_display
--;
991 #ifdef FAST_MOVE_CURSOR
993 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
996 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
998 next
-= (unsigned long) dest
;
1006 edit_move_backward_lots (WEdit
*edit
, long increment
)
1011 if (increment
> edit
->curs1
)
1012 increment
= edit
->curs1
;
1015 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1017 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1020 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1025 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1026 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1031 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1032 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1033 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1034 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1037 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1038 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1039 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1044 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1046 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1048 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1049 g_malloc (EDIT_BUF_SIZE
);
1054 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1064 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1066 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1070 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1071 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1074 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1076 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1077 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1082 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1084 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1086 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1087 g_malloc (EDIT_BUF_SIZE
);
1092 return edit_get_byte (edit
, edit
->curs1
);
1095 #endif /* ! FAST_MOVE_CURSOR */
1097 /* moves the cursor right or left: increment positive or negative respectively */
1098 void edit_cursor_move (WEdit
* edit
, long increment
)
1100 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1103 #ifdef FAST_MOVE_CURSOR
1104 if (increment
< -256) {
1105 edit
->force
|= REDRAW_PAGE
;
1106 edit_move_backward_lots (edit
, -increment
);
1109 #endif /* ! FAST_MOVE_CURSOR */
1111 if (increment
< 0) {
1112 for (; increment
< 0; increment
++) {
1116 edit_push_action (edit
, CURS_RIGHT
);
1118 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1119 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1120 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1121 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1123 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1124 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1125 mhl_mem_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1126 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1131 edit
->force
|= REDRAW_LINE_BELOW
;
1135 } else if (increment
> 0) {
1136 for (; increment
> 0; increment
--) {
1140 edit_push_action (edit
, CURS_LEFT
);
1142 c
= edit_get_byte (edit
, edit
->curs1
);
1143 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1144 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1145 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1147 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1148 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1149 mhl_mem_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1150 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1155 edit
->force
|= REDRAW_LINE_ABOVE
;
1161 /* These functions return positions relative to lines */
1163 /* returns index of last char on line + 1 */
1164 long edit_eol (WEdit
* edit
, long current
)
1166 if (current
< edit
->last_byte
) {
1168 if (edit_get_byte (edit
, current
) == '\n')
1171 return edit
->last_byte
;
1175 /* returns index of first char on line */
1176 long edit_bol (WEdit
* edit
, long current
)
1180 if (edit_get_byte (edit
, current
- 1) == '\n')
1188 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1191 if (upto
> edit
->last_byte
)
1192 upto
= edit
->last_byte
;
1195 while (current
< upto
)
1196 if (edit_get_byte (edit
, current
++) == '\n')
1202 /* If lines is zero this returns the count of lines from current to upto. */
1203 /* If upto is zero returns index of lines forward current. */
1204 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1207 return edit_count_lines (edit
, current
, upto
);
1213 next
= edit_eol (edit
, current
) + 1;
1214 if (next
> edit
->last_byte
)
1224 /* Returns offset of 'lines' lines up from current */
1225 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1229 current
= edit_bol (edit
, current
);
1230 while((lines
--) && current
!= 0)
1231 current
= edit_bol (edit
, current
- 1);
1235 /* If cols is zero this returns the count of columns from current to upto. */
1236 /* If upto is zero returns index of cols across from current. */
1237 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1246 q
= edit
->last_byte
+ 2;
1248 for (col
= 0, p
= current
; p
< q
; p
++) {
1256 c
= edit_get_byte (edit
, p
);
1258 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1259 else if (c
== '\n') {
1264 } else if (c
< 32 || c
== 127)
1265 col
+= 2; /* Caret notation for control characters */
1272 /* returns the current column position of the cursor */
1273 int edit_get_col (WEdit
* edit
)
1275 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1279 /* Scrolling functions */
1281 void edit_update_curs_row (WEdit
* edit
)
1283 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1286 void edit_update_curs_col (WEdit
* edit
)
1288 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1291 /*moves the display start position up by i lines */
1292 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1294 unsigned long lines_above
= edit
->start_line
;
1295 if (i
> lines_above
)
1298 edit
->start_line
-= i
;
1299 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1300 edit
->force
|= REDRAW_PAGE
;
1301 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1303 edit_update_curs_row (edit
);
1307 /* returns 1 if could scroll, 0 otherwise */
1308 void edit_scroll_downward (WEdit
* edit
, int i
)
1311 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1312 if (lines_below
> 0) {
1313 if (i
> lines_below
)
1315 edit
->start_line
+= i
;
1316 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1317 edit
->force
|= REDRAW_PAGE
;
1318 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1320 edit_update_curs_row (edit
);
1323 void edit_scroll_right (WEdit
* edit
, int i
)
1325 edit
->force
|= REDRAW_PAGE
;
1326 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1327 edit
->start_col
-= i
;
1330 void edit_scroll_left (WEdit
* edit
, int i
)
1332 if (edit
->start_col
) {
1333 edit
->start_col
+= i
;
1334 if (edit
->start_col
> 0)
1335 edit
->start_col
= 0;
1336 edit
->force
|= REDRAW_PAGE
;
1337 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1341 /* high level cursor movement commands */
1343 static int is_in_indent (WEdit
*edit
)
1345 long p
= edit_bol (edit
, edit
->curs1
);
1346 while (p
< edit
->curs1
)
1347 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1352 static int left_of_four_spaces (WEdit
*edit
);
1355 edit_move_to_prev_col (WEdit
* edit
, long p
)
1357 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1359 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1360 edit_update_curs_col (edit
);
1362 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1363 int q
= edit
->curs_col
;
1364 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1365 p
= edit_bol (edit
, edit
->curs1
);
1366 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1367 if (!left_of_four_spaces (edit
))
1368 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1375 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1377 unsigned long p
, l
= edit
->curs_line
;
1383 edit
->force
|= REDRAW_PAGE
;
1385 edit_scroll_upward (edit
, i
);
1387 p
= edit_bol (edit
, edit
->curs1
);
1388 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1389 edit_move_to_prev_col (edit
, p
);
1391 edit
->search_start
= edit
->curs1
;
1392 edit
->found_len
= 0;
1397 is_blank (WEdit
*edit
, long offset
)
1401 s
= edit_bol (edit
, offset
);
1402 f
= edit_eol (edit
, offset
) - 1;
1404 c
= edit_get_byte (edit
, s
++);
1412 /* returns the offset of line i */
1414 edit_find_line (WEdit
*edit
, int line
)
1418 if (!edit
->caches_valid
) {
1419 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1420 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1421 /* three offsets that we *know* are line 0 at 0 and these two: */
1422 edit
->line_numbers
[1] = edit
->curs_line
;
1423 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1424 edit
->line_numbers
[2] = edit
->total_lines
;
1425 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1426 edit
->caches_valid
= 1;
1428 if (line
>= edit
->total_lines
)
1429 return edit
->line_offsets
[2];
1432 /* find the closest known point */
1433 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1435 n
= abs (edit
->line_numbers
[i
] - line
);
1442 return edit
->line_offsets
[j
]; /* know the offset exactly */
1443 if (m
== 1 && j
>= 3)
1444 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1446 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1447 if (line
> edit
->line_numbers
[j
])
1448 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1450 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1451 edit
->line_numbers
[i
] = line
;
1452 return edit
->line_offsets
[i
];
1455 int line_is_blank (WEdit
* edit
, long line
)
1457 return is_blank (edit
, edit_find_line (edit
, line
));
1460 /* moves up until a blank line is reached, or until just
1461 before a non-blank line is reached */
1462 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1465 if (edit
->curs_line
<= 1) {
1468 if (line_is_blank (edit
, edit
->curs_line
)) {
1469 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1470 for (i
= edit
->curs_line
- 1; i
; i
--)
1471 if (!line_is_blank (edit
, i
)) {
1476 for (i
= edit
->curs_line
- 1; i
; i
--)
1477 if (line_is_blank (edit
, i
))
1481 for (i
= edit
->curs_line
- 1; i
; i
--)
1482 if (line_is_blank (edit
, i
))
1486 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1490 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1492 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1498 edit
->force
|= REDRAW_PAGE
;
1500 edit_scroll_downward (edit
, i
);
1501 p
= edit_bol (edit
, edit
->curs1
);
1502 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1503 edit_move_to_prev_col (edit
, p
);
1505 edit
->search_start
= edit
->curs1
;
1506 edit
->found_len
= 0;
1510 /* moves down until a blank line is reached, or until just
1511 before a non-blank line is reached */
1512 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1515 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1516 i
= edit
->total_lines
;
1518 if (line_is_blank (edit
, edit
->curs_line
)) {
1519 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1520 for (i
= edit
->curs_line
+ 1; i
; i
++)
1521 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1526 for (i
= edit
->curs_line
+ 1; i
; i
++)
1527 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1531 for (i
= edit
->curs_line
+ 1; i
; i
++)
1532 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1536 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1539 static void edit_begin_page (WEdit
*edit
)
1541 edit_update_curs_row (edit
);
1542 edit_move_up (edit
, edit
->curs_row
, 0);
1545 static void edit_end_page (WEdit
*edit
)
1547 edit_update_curs_row (edit
);
1548 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1552 /* goto beginning of text */
1553 static void edit_move_to_top (WEdit
* edit
)
1555 if (edit
->curs_line
) {
1556 edit_cursor_move (edit
, -edit
->curs1
);
1557 edit_move_to_prev_col (edit
, 0);
1558 edit
->force
|= REDRAW_PAGE
;
1559 edit
->search_start
= 0;
1560 edit_update_curs_row(edit
);
1565 /* goto end of text */
1566 static void edit_move_to_bottom (WEdit
* edit
)
1568 if (edit
->curs_line
< edit
->total_lines
) {
1569 edit_cursor_move (edit
, edit
->curs2
);
1570 edit
->start_display
= edit
->last_byte
;
1571 edit
->start_line
= edit
->total_lines
;
1572 edit_update_curs_row(edit
);
1573 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1574 edit
->force
|= REDRAW_PAGE
;
1578 /* goto beginning of line */
1579 static void edit_cursor_to_bol (WEdit
* edit
)
1581 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1582 edit
->search_start
= edit
->curs1
;
1583 edit
->prev_col
= edit_get_col (edit
);
1586 /* goto end of line */
1587 static void edit_cursor_to_eol (WEdit
* edit
)
1589 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1590 edit
->search_start
= edit
->curs1
;
1591 edit
->prev_col
= edit_get_col (edit
);
1594 /* move cursor to line 'line' */
1595 void edit_move_to_line (WEdit
* e
, long line
)
1597 if(line
< e
->curs_line
)
1598 edit_move_up (e
, e
->curs_line
- line
, 0);
1600 edit_move_down (e
, line
- e
->curs_line
, 0);
1601 edit_scroll_screen_over_cursor (e
);
1604 /* scroll window so that first visible line is 'line' */
1605 void edit_move_display (WEdit
* e
, long line
)
1607 if(line
< e
->start_line
)
1608 edit_scroll_upward (e
, e
->start_line
- line
);
1610 edit_scroll_downward (e
, line
- e
->start_line
);
1613 /* save markers onto undo stack */
1614 void edit_push_markers (WEdit
* edit
)
1616 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1617 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1620 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1629 /* highlight marker toggle */
1630 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1632 edit_push_markers (edit
);
1634 edit_set_markers (edit
, 0, 0, 0, 0);
1635 edit
->force
|= REDRAW_PAGE
;
1637 if (edit
->mark2
>= 0) {
1638 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1639 edit
->force
|= REDRAW_PAGE
;
1641 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1645 static unsigned long
1650 const char option_chars_move_whole_word
[] =
1651 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1656 if (*option_chars_move_whole_word
== '!')
1658 return 0x80000000UL
;
1662 else if (islower (c
))
1664 else if (isalpha (c
))
1666 else if (isdigit (c
))
1668 else if (isspace (c
))
1670 q
= strchr (option_chars_move_whole_word
, c
);
1672 return 0xFFFFFFFFUL
;
1674 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1678 } while ((q
= strchr (q
+ 1, c
)));
1683 edit_left_word_move (WEdit
*edit
, int s
)
1687 edit_cursor_move (edit
, -1);
1690 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1691 c2
= edit_get_byte (edit
, edit
->curs1
);
1692 if (!(my_type_of (c1
) & my_type_of (c2
)))
1694 if (isspace (c1
) && !isspace (c2
))
1697 if (!isspace (c1
) && isspace (c2
))
1702 static void edit_left_word_move_cmd (WEdit
* edit
)
1704 edit_left_word_move (edit
, 0);
1705 edit
->force
|= REDRAW_PAGE
;
1709 edit_right_word_move (WEdit
*edit
, int s
)
1713 edit_cursor_move (edit
, 1);
1714 if (edit
->curs1
>= edit
->last_byte
)
1716 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1717 c2
= edit_get_byte (edit
, edit
->curs1
);
1718 if (!(my_type_of (c1
) & my_type_of (c2
)))
1720 if (isspace (c1
) && !isspace (c2
))
1723 if (!isspace (c1
) && isspace (c2
))
1728 static void edit_right_word_move_cmd (WEdit
* edit
)
1730 edit_right_word_move (edit
, 0);
1731 edit
->force
|= REDRAW_PAGE
;
1735 static void edit_right_delete_word (WEdit
* edit
)
1739 if (edit
->curs1
>= edit
->last_byte
)
1741 c1
= edit_delete (edit
);
1742 c2
= edit_get_byte (edit
, edit
->curs1
);
1743 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1745 if (!(my_type_of (c1
) & my_type_of (c2
)))
1750 static void edit_left_delete_word (WEdit
* edit
)
1754 if (edit
->curs1
<= 0)
1756 c1
= edit_backspace (edit
);
1757 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1758 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1760 if (!(my_type_of (c1
) & my_type_of (c2
)))
1766 the start column position is not recorded, and hence does not
1767 undo as it happed. But who would notice.
1770 edit_do_undo (WEdit
* edit
)
1775 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1777 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1782 edit_cursor_move (edit
, 1);
1785 edit_cursor_move (edit
, -1);
1788 edit_backspace (edit
);
1794 column_highlighting
= 1;
1797 column_highlighting
= 0;
1800 if (ac
>= 256 && ac
< 512)
1801 edit_insert_ahead (edit
, ac
- 256);
1802 if (ac
>= 0 && ac
< 256)
1803 edit_insert (edit
, ac
);
1805 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1806 edit
->mark1
= ac
- MARK_1
;
1807 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1808 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1809 edit
->mark2
= ac
- MARK_2
;
1810 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1813 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1816 if (edit
->start_display
> ac
- KEY_PRESS
) {
1817 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1818 edit
->force
|= REDRAW_PAGE
;
1819 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1820 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1821 edit
->force
|= REDRAW_PAGE
;
1823 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1824 edit_update_curs_row (edit
);
1827 edit
->stack_disable
= 0;
1830 static void edit_delete_to_line_end (WEdit
* edit
)
1832 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1839 static void edit_delete_to_line_begin (WEdit
* edit
)
1841 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1844 edit_backspace (edit
);
1849 edit_delete_line (WEdit
*edit
)
1852 * Delete right part of the line.
1853 * Note that edit_get_byte() returns '\n' when byte position is
1856 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1857 (void) edit_delete (edit
);
1862 * Note that edit_delete() will not corrupt anything if called while
1863 * cursor position is EOF.
1865 (void) edit_delete (edit
);
1868 * Delete left part of the line.
1869 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1871 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1872 (void) edit_backspace (edit
);
1876 static void insert_spaces_tab (WEdit
* edit
, int half
)
1879 edit_update_curs_col (edit
);
1880 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1882 edit_insert (edit
, ' ');
1887 static int is_aligned_on_a_tab (WEdit
* edit
)
1889 edit_update_curs_col (edit
);
1890 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1891 return 0; /* not alligned on a tab */
1895 static int right_of_four_spaces (WEdit
*edit
)
1898 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1899 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1901 return is_aligned_on_a_tab (edit
);
1905 static int left_of_four_spaces (WEdit
*edit
)
1908 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1909 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1911 return is_aligned_on_a_tab (edit
);
1915 int edit_indent_width (WEdit
* edit
, long p
)
1918 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1920 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1923 void edit_insert_indent (WEdit
* edit
, int indent
)
1925 if (!option_fill_tabs_with_spaces
) {
1926 while (indent
>= TAB_SIZE
) {
1927 edit_insert (edit
, '\t');
1931 while (indent
-- > 0)
1932 edit_insert (edit
, ' ');
1936 edit_auto_indent (WEdit
* edit
)
1941 /* use the previous line as a template */
1942 p
= edit_move_backward (edit
, p
, 1);
1943 /* copy the leading whitespace of the line */
1944 for (;;) { /* no range check - the line _is_ \n-terminated */
1945 c
= edit_get_byte (edit
, p
++);
1946 if (c
!= ' ' && c
!= '\t')
1948 edit_insert (edit
, c
);
1952 static void edit_double_newline (WEdit
* edit
)
1954 edit_insert (edit
, '\n');
1955 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1957 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1959 edit
->force
|= REDRAW_PAGE
;
1960 edit_insert (edit
, '\n');
1963 static void edit_tab_cmd (WEdit
* edit
)
1967 if (option_fake_half_tabs
) {
1968 if (is_in_indent (edit
)) {
1969 /*insert a half tab (usually four spaces) unless there is a
1970 half tab already behind, then delete it and insert a
1972 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1973 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1974 edit_backspace (edit
);
1975 edit_insert (edit
, '\t');
1977 insert_spaces_tab (edit
, 1);
1982 if (option_fill_tabs_with_spaces
) {
1983 insert_spaces_tab (edit
, 0);
1985 edit_insert (edit
, '\t');
1990 static void check_and_wrap_line (WEdit
* edit
)
1993 if (!option_typewriter_wrap
)
1995 edit_update_curs_col (edit
);
1996 if (edit
->curs_col
< option_word_wrap_line_length
)
2001 c
= edit_get_byte (edit
, curs
);
2002 if (c
== '\n' || curs
<= 0) {
2003 edit_insert (edit
, '\n');
2006 if (c
== ' ' || c
== '\t') {
2007 int current
= edit
->curs1
;
2008 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2009 edit_insert (edit
, '\n');
2010 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2016 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2018 void edit_push_key_press (WEdit
* edit
)
2020 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2021 if (edit
->mark2
== -1)
2022 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2025 /* this find the matching bracket in either direction, and sets edit->bracket */
2026 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2028 const char * const b
= "{}{[][()(", *p
;
2029 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2030 unsigned long j
= 0;
2032 edit_update_curs_row (edit
);
2033 c
= edit_get_byte (edit
, edit
->curs1
);
2036 if (!furthest_bracket_search
)
2037 furthest_bracket_search
--;
2038 /* not on a bracket at all */
2041 /* the matching bracket */
2043 /* going left or right? */
2044 if (strchr ("{[(", c
))
2046 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2047 /* out of buffer? */
2048 if (q
>= edit
->last_byte
|| q
< 0)
2050 a
= edit_get_byte (edit
, q
);
2051 /* don't want to eat CPU */
2052 if (j
++ > furthest_bracket_search
)
2054 /* out of screen? */
2056 if (q
< edit
->start_display
)
2058 /* count lines if searching downward */
2059 if (inc
> 0 && a
== '\n')
2060 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2063 /* count bracket depth */
2064 i
+= (a
== c
) - (a
== d
);
2065 /* return if bracket depth is zero */
2073 static long last_bracket
= -1;
2075 void edit_find_bracket (WEdit
* edit
)
2077 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2078 if (last_bracket
!= edit
->bracket
)
2079 edit
->force
|= REDRAW_PAGE
;
2080 last_bracket
= edit
->bracket
;
2083 static void edit_goto_matching_bracket (WEdit
*edit
)
2086 q
= edit_get_bracket (edit
, 0, 0);
2089 edit
->bracket
= edit
->curs1
;
2090 edit
->force
|= REDRAW_PAGE
;
2091 edit_cursor_move (edit
, q
- edit
->curs1
);
2095 * This executes a command as though the user initiated it through a key
2096 * press. Callback with WIDGET_KEY as a message calls this after
2097 * translating the key press. This function can be used to pass any
2098 * command to the editor. Note that the screen wouldn't update
2099 * automatically. Either of command or char_for_insertion must be
2100 * passed as -1. Commands are executed, and char_for_insertion is
2101 * inserted at the cursor.
2103 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2105 if (command
== CK_Begin_Record_Macro
) {
2107 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2110 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2111 edit
->force
|= REDRAW_COMPLETELY
;
2112 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2116 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2117 edit
->macro
[edit
->macro_i
].command
= command
;
2118 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2120 /* record the beginning of a set of editing actions initiated by a key press */
2121 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2122 edit_push_key_press (edit
);
2124 edit_execute_cmd (edit
, command
, char_for_insertion
);
2125 if (column_highlighting
)
2126 edit
->force
|= REDRAW_PAGE
;
2129 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2132 This executes a command at a lower level than macro recording.
2133 It also does not push a key_press onto the undo stack. This means
2134 that if it is called many times, a single undo command will undo
2135 all of them. It also does not check for the Undo command.
2138 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2140 edit
->force
|= REDRAW_LINE
;
2142 /* The next key press will unhighlight the found string, so update
2144 if (edit
->found_len
|| column_highlighting
)
2145 edit
->force
|= REDRAW_PAGE
;
2147 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2148 column_highlighting
= 0;
2149 if (!edit
->highlight
2150 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2151 edit_mark_cmd (edit
, 1); /* clear */
2152 edit_mark_cmd (edit
, 0); /* marking on */
2154 edit
->highlight
= 1;
2155 } else { /* any other command */
2156 if (edit
->highlight
)
2157 edit_mark_cmd (edit
, 0); /* clear */
2158 edit
->highlight
= 0;
2161 /* first check for undo */
2162 if (command
== CK_Undo
) {
2163 edit_do_undo (edit
);
2164 edit
->found_len
= 0;
2165 edit
->prev_col
= edit_get_col (edit
);
2166 edit
->search_start
= edit
->curs1
;
2170 /* An ordinary key press */
2171 if (char_for_insertion
>= 0) {
2172 if (edit
->overwrite
) {
2173 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2176 edit_insert (edit
, char_for_insertion
);
2177 if (option_auto_para_formatting
) {
2178 format_paragraph (edit
, 0);
2179 edit
->force
|= REDRAW_PAGE
;
2181 check_and_wrap_line (edit
);
2182 edit
->found_len
= 0;
2183 edit
->prev_col
= edit_get_col (edit
);
2184 edit
->search_start
= edit
->curs1
;
2185 edit_find_bracket (edit
);
2191 case CK_Begin_Page_Highlight
:
2192 case CK_End_Page_Highlight
:
2197 case CK_Word_Left_Highlight
:
2198 case CK_Word_Right_Highlight
:
2199 case CK_Up_Highlight
:
2200 case CK_Down_Highlight
:
2201 if (edit
->mark2
== -1)
2202 break; /*marking is following the cursor: may need to highlight a whole line */
2205 case CK_Left_Highlight
:
2206 case CK_Right_Highlight
:
2207 edit
->force
|= REDRAW_CHAR_ONLY
;
2210 /* basic cursor key commands */
2213 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2214 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2216 edit_backspace (edit
);
2219 if (option_fake_half_tabs
) {
2221 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2222 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2223 edit_backspace (edit
);
2228 edit_backspace (edit
);
2231 if (option_fake_half_tabs
) {
2233 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2234 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2241 case CK_Delete_Word_Left
:
2242 edit_left_delete_word (edit
);
2244 case CK_Delete_Word_Right
:
2245 edit_right_delete_word (edit
);
2247 case CK_Delete_Line
:
2248 edit_delete_line (edit
);
2250 case CK_Delete_To_Line_End
:
2251 edit_delete_to_line_end (edit
);
2253 case CK_Delete_To_Line_Begin
:
2254 edit_delete_to_line_begin (edit
);
2257 if (option_auto_para_formatting
) {
2258 edit_double_newline (edit
);
2259 if (option_return_does_auto_indent
)
2260 edit_auto_indent (edit
);
2261 format_paragraph (edit
, 0);
2263 edit_insert (edit
, '\n');
2264 if (option_return_does_auto_indent
) {
2265 edit_auto_indent (edit
);
2270 edit_insert (edit
, '\n');
2274 case CK_Page_Up_Highlight
:
2275 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2278 case CK_Page_Down_Highlight
:
2279 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2282 case CK_Left_Highlight
:
2283 if (option_fake_half_tabs
) {
2284 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2285 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2286 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2290 edit_cursor_move (edit
, -1);
2293 case CK_Right_Highlight
:
2294 if (option_fake_half_tabs
) {
2295 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2296 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2297 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2301 edit_cursor_move (edit
, 1);
2304 case CK_Begin_Page_Highlight
:
2305 edit_begin_page (edit
);
2308 case CK_End_Page_Highlight
:
2309 edit_end_page (edit
);
2312 case CK_Word_Left_Highlight
:
2313 edit_left_word_move_cmd (edit
);
2316 case CK_Word_Right_Highlight
:
2317 edit_right_word_move_cmd (edit
);
2320 case CK_Up_Highlight
:
2321 edit_move_up (edit
, 1, 0);
2324 case CK_Down_Highlight
:
2325 edit_move_down (edit
, 1, 0);
2327 case CK_Paragraph_Up
:
2328 case CK_Paragraph_Up_Highlight
:
2329 edit_move_up_paragraph (edit
, 0);
2331 case CK_Paragraph_Down
:
2332 case CK_Paragraph_Down_Highlight
:
2333 edit_move_down_paragraph (edit
, 0);
2336 case CK_Scroll_Up_Highlight
:
2337 edit_move_up (edit
, 1, 1);
2339 case CK_Scroll_Down
:
2340 case CK_Scroll_Down_Highlight
:
2341 edit_move_down (edit
, 1, 1);
2344 case CK_Home_Highlight
:
2345 edit_cursor_to_bol (edit
);
2348 case CK_End_Highlight
:
2349 edit_cursor_to_eol (edit
);
2353 edit_tab_cmd (edit
);
2354 if (option_auto_para_formatting
) {
2355 format_paragraph (edit
, 0);
2356 edit
->force
|= REDRAW_PAGE
;
2358 check_and_wrap_line (edit
);
2361 case CK_Toggle_Insert
:
2362 edit
->overwrite
= (edit
->overwrite
== 0);
2366 if (edit
->mark2
>= 0) {
2367 if (column_highlighting
)
2368 edit_push_action (edit
, COLUMN_ON
);
2369 column_highlighting
= 0;
2371 edit_mark_cmd (edit
, 0);
2373 case CK_Column_Mark
:
2374 if (!column_highlighting
)
2375 edit_push_action (edit
, COLUMN_OFF
);
2376 column_highlighting
= 1;
2377 edit_mark_cmd (edit
, 0);
2380 if (column_highlighting
)
2381 edit_push_action (edit
, COLUMN_ON
);
2382 column_highlighting
= 0;
2383 edit_mark_cmd (edit
, 1);
2386 case CK_Toggle_Bookmark
:
2387 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2388 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2389 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2391 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2393 case CK_Flush_Bookmarks
:
2394 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2395 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2396 edit
->force
|= REDRAW_PAGE
;
2398 case CK_Next_Bookmark
:
2399 if (edit
->book_mark
) {
2400 struct _book_mark
*p
;
2401 p
= (struct _book_mark
*) book_mark_find (edit
,
2405 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2406 || p
->line
< edit
->start_line
)
2407 edit_move_display (edit
,
2409 edit
->num_widget_lines
/ 2);
2410 edit_move_to_line (edit
, p
->line
);
2414 case CK_Prev_Bookmark
:
2415 if (edit
->book_mark
) {
2416 struct _book_mark
*p
;
2417 p
= (struct _book_mark
*) book_mark_find (edit
,
2419 while (p
->line
== edit
->curs_line
)
2423 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2424 || p
->line
< edit
->start_line
)
2425 edit_move_display (edit
,
2427 edit
->num_widget_lines
/ 2);
2428 edit_move_to_line (edit
, p
->line
);
2433 case CK_Beginning_Of_Text
:
2434 case CK_Beginning_Of_Text_Highlight
:
2435 edit_move_to_top (edit
);
2437 case CK_End_Of_Text
:
2438 case CK_End_Of_Text_Highlight
:
2439 edit_move_to_bottom (edit
);
2443 edit_block_copy_cmd (edit
);
2446 edit_block_delete_cmd (edit
);
2449 edit_block_move_cmd (edit
);
2453 edit_copy_to_X_buf_cmd (edit
);
2456 edit_cut_to_X_buf_cmd (edit
);
2459 edit_paste_from_X_buf_cmd (edit
);
2461 case CK_Selection_History
:
2462 edit_paste_from_history (edit
);
2466 edit_save_as_cmd (edit
);
2469 edit_save_confirm_cmd (edit
);
2472 edit_load_cmd (edit
);
2475 edit_save_block_cmd (edit
);
2477 case CK_Insert_File
:
2478 edit_insert_file_cmd (edit
);
2481 case CK_Toggle_Syntax
:
2482 if ((option_syntax_highlighting
^= 1) == 1)
2483 edit_load_syntax (edit
, NULL
, option_syntax_type
);
2484 edit
->force
|= REDRAW_PAGE
;
2488 edit_search_cmd (edit
, 0);
2491 edit_search_cmd (edit
, 1);
2494 edit_replace_cmd (edit
, 0);
2496 case CK_Replace_Again
:
2497 edit_replace_cmd (edit
, 1);
2499 case CK_Complete_Word
:
2500 edit_complete_word_cmd (edit
);
2504 dlg_stop (edit
->widget
.parent
);
2507 edit_new_cmd (edit
);
2511 edit_help_cmd (edit
);
2515 edit_refresh_cmd (edit
);
2520 /* fool gcc to prevent a Y2K warning */
2521 char time_format
[] = "_c";
2522 time_format
[0] = '%';
2524 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
2525 edit_print_string (edit
, s
);
2526 edit
->force
|= REDRAW_PAGE
;
2530 edit_goto_cmd (edit
);
2532 case CK_Paragraph_Format
:
2533 format_paragraph (edit
, 1);
2534 edit
->force
|= REDRAW_PAGE
;
2536 case CK_Delete_Macro
:
2537 edit_delete_macro_cmd (edit
);
2539 case CK_Match_Bracket
:
2540 edit_goto_matching_bracket (edit
);
2546 edit_sort_cmd (edit
);
2549 edit_ext_cmd (edit
);
2552 edit_mail_dialog (edit
);
2557 case CK_Select_Codepage
:
2558 edit_select_codepage_cmd (edit
);
2560 case CK_Insert_Literal
:
2561 edit_insert_literal_cmd (edit
);
2563 case CK_Execute_Macro
:
2564 edit_execute_macro_cmd (edit
);
2566 case CK_Begin_End_Macro
:
2567 edit_begin_end_macro_cmd (edit
);
2577 if ((command
/ 1000) == 1) /* a shell command */
2578 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2579 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2580 struct macro m
[MAX_MACRO_LENGTH
];
2582 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
2583 edit_execute_macro (edit
, m
, nm
);
2586 /* keys which must set the col position, and the search vars */
2591 case CK_Replace_Again
:
2592 case CK_Complete_Word
:
2593 edit
->prev_col
= edit_get_col (edit
);
2596 case CK_Up_Highlight
:
2598 case CK_Down_Highlight
:
2600 case CK_Page_Up_Highlight
:
2602 case CK_Page_Down_Highlight
:
2603 case CK_Beginning_Of_Text
:
2604 case CK_Beginning_Of_Text_Highlight
:
2605 case CK_End_Of_Text
:
2606 case CK_End_Of_Text_Highlight
:
2607 case CK_Paragraph_Up
:
2608 case CK_Paragraph_Up_Highlight
:
2609 case CK_Paragraph_Down
:
2610 case CK_Paragraph_Down_Highlight
:
2612 case CK_Scroll_Up_Highlight
:
2613 case CK_Scroll_Down
:
2614 case CK_Scroll_Down_Highlight
:
2615 edit
->search_start
= edit
->curs1
;
2616 edit
->found_len
= 0;
2619 edit
->found_len
= 0;
2620 edit
->prev_col
= edit_get_col (edit
);
2621 edit
->search_start
= edit
->curs1
;
2623 edit_find_bracket (edit
);
2625 if (option_auto_para_formatting
) {
2629 case CK_Delete_Word_Left
:
2630 case CK_Delete_Word_Right
:
2631 case CK_Delete_To_Line_End
:
2632 case CK_Delete_To_Line_Begin
:
2633 format_paragraph (edit
, 0);
2634 edit
->force
|= REDRAW_PAGE
;
2641 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2645 if (edit
->macro_depth
++ > 256) {
2646 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2647 edit
->macro_depth
--;
2650 edit
->force
|= REDRAW_PAGE
;
2651 for (; i
< n
; i
++) {
2652 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2654 edit_update_screen (edit
);
2655 edit
->macro_depth
--;
2658 /* User edit menu, like user menu (F2) but only in editor. */
2660 user_menu (WEdit
* edit
)
2665 long start_mark
, end_mark
;
2666 char *block_file
= mhl_str_dir_plus_file (home_dir
, BLOCK_FILE
);
2669 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2670 if (!nomark
) /* remember marked or not */
2671 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2673 /* run shell scripts from menu */
2674 user_menu_cmd (edit
);
2676 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2677 /* no block messages */
2682 /* i.e. we have marked block */
2683 rc
= edit_block_delete_cmd (edit
);
2687 edit_insert_file (edit
, block_file
);
2690 /* truncate block file */
2691 if ((fd
= fopen (block_file
, "w"))) {
2695 edit_refresh_cmd (edit
);
2696 edit
->force
|= REDRAW_COMPLETELY
;
2699 mhl_mem_free (block_file
);