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 { "lzma -cd %s 2>&1", "lzma > %s", ".bz2" },
189 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
190 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
191 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
194 /* Return index of the filter or -1 is there is no appropriate filter */
195 static int edit_find_filter (const char *filename
)
200 l
= strlen (filename
);
201 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
202 e
= strlen (all_filters
[i
].extension
);
204 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
211 edit_get_filter (const char *filename
)
214 char *p
, *quoted_name
;
215 i
= edit_find_filter (filename
);
218 quoted_name
= name_quote (filename
, 0);
219 l
= strlen (quoted_name
);
220 p
= g_malloc (strlen (all_filters
[i
].read
) + l
+ 2);
221 sprintf (p
, all_filters
[i
].read
, quoted_name
);
222 mhl_mem_free (quoted_name
);
227 edit_get_write_filter (const char *write_name
, const char *filename
)
231 i
= edit_find_filter (filename
);
234 writename
= name_quote (write_name
, 0);
235 l
= strlen (writename
);
236 p
= g_malloc (strlen (all_filters
[i
].write
) + l
+ 2);
237 sprintf (p
, all_filters
[i
].write
, writename
);
238 mhl_mem_free (writename
);
243 edit_insert_stream (WEdit
* edit
, FILE * f
)
247 while ((c
= fgetc (f
)) >= 0) {
248 edit_insert (edit
, c
);
254 long edit_write_stream (WEdit
* edit
, FILE * f
)
257 for (i
= 0; i
< edit
->last_byte
; i
++)
258 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
263 #define TEMP_BUF_LEN 1024
265 /* inserts a file at the cursor, returns 1 on success */
267 edit_insert_file (WEdit
*edit
, const char *filename
)
270 if ((p
= edit_get_filter (filename
))) {
272 long current
= edit
->curs1
;
273 f
= (FILE *) popen (p
, "r");
275 edit_insert_stream (edit
, f
);
276 edit_cursor_move (edit
, current
- edit
->curs1
);
277 if (pclose (f
) > 0) {
279 snprintf(errmsg
, sizeof(errmsg
), _(" Error reading from pipe: %s "), p
);
280 edit_error_dialog (_("Error"), errmsg
);
286 snprintf(errmsg
, sizeof(errmsg
), _(" Cannot open pipe for reading: %s "), p
);
287 edit_error_dialog (_("Error"), errmsg
);
293 int i
, file
, blocklen
;
294 long current
= edit
->curs1
;
296 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
298 buf
= g_malloc (TEMP_BUF_LEN
);
299 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
300 for (i
= 0; i
< blocklen
; i
++)
301 edit_insert (edit
, buf
[i
]);
303 edit_cursor_move (edit
, current
- edit
->curs1
);
312 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
314 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
320 /* Try opening an existing file */
321 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
325 * Try creating the file. O_EXCL prevents following broken links
326 * and opening existing files.
330 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
333 snprintf (errmsg
, sizeof(errmsg
), _(" Cannot open %s for reading "), filename
);
336 /* New file, delete it if it's not modified or saved */
337 edit
->delete_file
= 1;
341 /* Check what we have opened */
342 if (mc_fstat (file
, st
) < 0) {
343 snprintf (errmsg
, sizeof(errmsg
), _(" Cannot get size/permissions for %s "), filename
);
347 /* We want to open regular files only */
348 if (!S_ISREG (st
->st_mode
)) {
349 snprintf (errmsg
, sizeof(errmsg
), _(" %s is not a regular file "), filename
);
354 * Don't delete non-empty files.
355 * O_EXCL should prevent it, but let's be on the safe side.
357 if (st
->st_size
> 0) {
358 edit
->delete_file
= 0;
361 if (st
->st_size
>= SIZE_LIMIT
) {
362 snprintf (errmsg
, sizeof(errmsg
), _(" File %s is too large "), filename
);
367 (void) mc_close (file
);
369 edit_error_dialog (_("Error"), errmsg
);
376 * Open the file and load it into the buffers, either directly or using
377 * a filter. Return 0 on success, 1 on error.
379 * Fast loading (edit_load_file_fast) is used when the file size is
380 * known. In this case the data is read into the buffers by blocks.
381 * If the file size is not known, the data is loaded byte by byte in
385 edit_load_file (WEdit
*edit
)
389 /* Cannot do fast load if a filter is used */
390 if (edit_find_filter (edit
->filename
) >= 0)
394 * VFS may report file size incorrectly, and slow load is not a big
395 * deal considering overhead in VFS.
397 if (!vfs_file_is_local (edit
->filename
))
401 * FIXME: line end translation should disable fast loading as well
402 * Consider doing fseek() to the end and ftell() for the real size.
405 if (*edit
->filename
) {
406 /* If we are dealing with a real file, check that it exists */
407 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
410 /* nothing to load */
414 edit_init_buffers (edit
);
417 edit
->last_byte
= edit
->stat1
.st_size
;
418 edit_load_file_fast (edit
, edit
->filename
);
419 /* If fast load was used, the number of lines wasn't calculated */
420 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
423 if (*edit
->filename
) {
424 edit
->stack_disable
= 1;
425 if (!edit_insert_file (edit
, edit
->filename
)) {
429 edit
->stack_disable
= 0;
435 /* Restore saved cursor position in the file */
437 edit_load_position (WEdit
*edit
)
442 if (!edit
->filename
|| !*edit
->filename
)
445 filename
= vfs_canon (edit
->filename
);
446 load_file_position (filename
, &line
, &column
);
447 mhl_mem_free (filename
);
449 edit_move_to_line (edit
, line
- 1);
450 edit
->prev_col
= column
;
451 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
452 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
455 /* Save cursor position in the file */
457 edit_save_position (WEdit
*edit
)
461 if (!edit
->filename
|| !*edit
->filename
)
464 filename
= vfs_canon (edit
->filename
);
465 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
466 mhl_mem_free (filename
);
469 /* Clean the WEdit stricture except the widget part */
471 edit_purge_widget (WEdit
*edit
)
473 int len
= sizeof (WEdit
) - sizeof (Widget
);
474 char *start
= (char *) edit
+ sizeof (Widget
);
475 memset (start
, 0, len
);
476 edit
->macro_i
= -1; /* not recording a macro */
479 #define space_width 1
482 * Fill in the edit structure. Return NULL on failure. Pass edit as
483 * NULL to allocate a new structure.
485 * If line is 0, try to restore saved position. Otherwise put the
486 * cursor on that line and show it in the middle of the screen.
489 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
493 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
498 * Expand option_whole_chars_search by national letters using
502 static char option_whole_chars_search_buf
[256];
504 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
506 size_t len
= strlen (option_whole_chars_search
);
508 strcpy (option_whole_chars_search_buf
,
509 option_whole_chars_search
);
511 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
512 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
513 option_whole_chars_search_buf
[len
++] = i
;
517 option_whole_chars_search_buf
[len
] = 0;
518 option_whole_chars_search
= option_whole_chars_search_buf
;
520 #endif /* ENABLE_NLS */
521 edit
= g_malloc0 (sizeof (WEdit
));
524 edit_purge_widget (edit
);
525 edit
->num_widget_lines
= lines
;
526 edit
->num_widget_columns
= columns
;
527 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
528 edit
->stat1
.st_uid
= getuid ();
529 edit
->stat1
.st_gid
= getgid ();
530 edit
->stat1
.st_mtime
= 0;
532 edit
->force
|= REDRAW_PAGE
;
533 edit_set_filename (edit
, filename
);
534 edit
->stack_size
= START_STACK_SIZE
;
535 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
536 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
537 if (edit_load_file (edit
)) {
538 /* edit_load_file already gives an error message */
543 edit
->loading_done
= 1;
546 edit_load_syntax (edit
, 0, 0);
549 edit_get_syntax_color (edit
, -1, &color
);
552 /* load saved cursor position */
553 if ((line
== 0) && option_save_position
) {
554 edit_load_position (edit
);
558 edit_move_display (edit
, line
- 1);
559 edit_move_to_line (edit
, line
- 1);
562 edit_load_user_map(edit
);
567 /* Clear the edit struct, freeing everything in it. Return 1 on success */
569 edit_clean (WEdit
*edit
)
576 /* a stale lock, remove it */
578 edit
->locked
= edit_unlock_file (edit
->filename
);
580 /* save cursor position */
581 if (option_save_position
)
582 edit_save_position (edit
);
584 /* File specified on the mcedit command line and never saved */
585 if (edit
->delete_file
)
586 unlink (edit
->filename
);
588 edit_free_syntax_rules (edit
);
589 book_mark_flush (edit
, -1);
590 for (; j
<= MAXBUFF
; j
++) {
591 mhl_mem_free (edit
->buffers1
[j
]);
592 mhl_mem_free (edit
->buffers2
[j
]);
595 mhl_mem_free (edit
->undo_stack
);
596 mhl_mem_free (edit
->filename
);
597 mhl_mem_free (edit
->dir
);
599 edit_purge_widget (edit
);
601 /* Free temporary strings used in catstrs() */
608 /* returns 1 on success */
609 int edit_renew (WEdit
* edit
)
611 int lines
= edit
->num_widget_lines
;
612 int columns
= edit
->num_widget_columns
;
616 if (!edit_init (edit
, lines
, columns
, "", 0))
622 * Load a new file into the editor. If it fails, preserve the old file.
623 * To do it, allocate a new widget, initialize it and, if the new file
624 * was loaded, copy the data to the old widget.
625 * Return 1 on success, 0 on failure.
628 edit_reload (WEdit
*edit
, const char *filename
)
631 int lines
= edit
->num_widget_lines
;
632 int columns
= edit
->num_widget_columns
;
634 e
= g_malloc0 (sizeof (WEdit
));
635 e
->widget
= edit
->widget
;
636 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
641 memcpy (edit
, e
, sizeof (WEdit
));
648 Recording stack for undo:
649 The following is an implementation of a compressed stack. Identical
650 pushes are recorded by a negative prefix indicating the number of times the
651 same char was pushed. This saves space for repeated curs-left or curs-right
668 If the stack long int is 0-255 it represents a normal insert (from a backspace),
669 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
670 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
671 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
674 The only way the cursor moves or the buffer is changed is through the routines:
675 insert, backspace, insert_ahead, delete, and cursor_move.
676 These record the reverse undo movements onto the stack each time they are
679 Each key press results in a set of actions (insert; delete ...). So each time
680 a key is pressed the current position of start_display is pushed as
681 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
682 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
683 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
687 void edit_push_action (WEdit
* edit
, long c
,...)
689 unsigned long sp
= edit
->stack_pointer
;
693 /* first enlarge the stack if necessary */
694 if (sp
> edit
->stack_size
- 10) { /* say */
695 if (option_max_undo
< 256)
696 option_max_undo
= 256;
697 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
698 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
700 edit
->undo_stack
= t
;
701 edit
->stack_size
<<= 1;
702 edit
->stack_size_mask
= edit
->stack_size
- 1;
706 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
707 if (edit
->stack_disable
)
710 #ifdef FAST_MOVE_CURSOR
711 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
713 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
714 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
716 c
= -(va_arg (ap
, int));
719 #endif /* ! FAST_MOVE_CURSOR */
720 if (edit
->stack_bottom
!= sp
721 && spm1
!= edit
->stack_bottom
722 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
724 if (edit
->undo_stack
[spm1
] < 0) {
725 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
727 if (edit
->undo_stack
[spm1
] > -1000000000) {
728 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
729 edit
->undo_stack
[spm1
]--;
733 /* #define NO_STACK_CURSMOVE_ANIHILATION */
734 #ifndef NO_STACK_CURSMOVE_ANIHILATION
735 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
736 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
737 if (edit
->undo_stack
[spm1
] == -2)
738 edit
->stack_pointer
= spm1
;
740 edit
->undo_stack
[spm1
]++;
745 d
= edit
->undo_stack
[spm1
];
748 return; /* --> no need to push multiple do-nothings */
749 edit
->undo_stack
[sp
] = -2;
752 #ifndef NO_STACK_CURSMOVE_ANIHILATION
753 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
754 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
755 edit
->stack_pointer
= spm1
;
761 edit
->undo_stack
[sp
] = c
;
764 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
766 /* if the sp wraps round and catches the stack_bottom then erase
767 * the first set of actions on the stack to make space - by moving
768 * stack_bottom forward one "key press" */
769 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
770 if ((unsigned long) c
== edit
->stack_bottom
||
771 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
773 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
774 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
776 /*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: */
777 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
778 edit
->stack_bottom
= edit
->stack_pointer
= 0;
782 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
783 then the file should be as it was when he loaded up. Then set edit->modified to 0.
786 pop_action (WEdit
* edit
)
789 unsigned long sp
= edit
->stack_pointer
;
790 if (sp
== edit
->stack_bottom
) {
793 sp
= (sp
- 1) & edit
->stack_size_mask
;
794 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
795 /* edit->undo_stack[sp] = '@'; */
796 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
799 if (sp
== edit
->stack_bottom
) {
802 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
803 if (edit
->undo_stack
[sp
] == -2) {
804 /* edit->undo_stack[sp] = '@'; */
805 edit
->stack_pointer
= sp
;
807 edit
->undo_stack
[sp
]++;
812 /* is called whenever a modification is made by one of the four routines below */
813 static inline void edit_modification (WEdit
* edit
)
815 edit
->caches_valid
= 0;
816 edit
->screen_modified
= 1;
818 /* raise lock when file modified */
819 if (!edit
->modified
&& !edit
->delete_file
)
820 edit
->locked
= edit_lock_file (edit
->filename
);
825 Basic low level single character buffer alterations and movements at the cursor.
826 Returns char passed over, inserted or removed.
830 edit_insert (WEdit
*edit
, int c
)
832 /* check if file has grown to large */
833 if (edit
->last_byte
>= SIZE_LIMIT
)
836 /* first we must update the position of the display window */
837 if (edit
->curs1
< edit
->start_display
) {
838 edit
->start_display
++;
843 /* Mark file as modified, unless the file hasn't been fully loaded */
844 if (edit
->loading_done
) {
845 edit_modification (edit
);
848 /* now we must update some info on the file and check if a redraw is required */
851 book_mark_inc (edit
, edit
->curs_line
);
854 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
857 /* save the reverse command onto the undo stack */
858 edit_push_action (edit
, BACKSPACE
);
861 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
862 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
863 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
865 /* add a new buffer if we've reached the end of the last one */
866 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
867 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
868 g_malloc (EDIT_BUF_SIZE
);
870 /* perform the insertion */
871 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
872 curs1
& M_EDIT_BUF_SIZE
]
875 /* update file length */
878 /* update cursor position */
883 /* same as edit_insert and move left */
884 void edit_insert_ahead (WEdit
* edit
, int c
)
886 if (edit
->last_byte
>= SIZE_LIMIT
)
888 if (edit
->curs1
< edit
->start_display
) {
889 edit
->start_display
++;
893 edit_modification (edit
);
896 book_mark_inc (edit
, edit
->curs_line
);
898 edit
->force
|= REDRAW_AFTER_CURSOR
;
900 edit_push_action (edit
, DELCHAR
);
902 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
903 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
904 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
906 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
907 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
908 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
915 int edit_delete (WEdit
* edit
)
921 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
922 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
923 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
925 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
927 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
928 mhl_mem_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
929 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
934 edit_modification (edit
);
937 book_mark_dec (edit
, edit
->curs_line
);
939 edit
->force
|= REDRAW_AFTER_CURSOR
;
941 edit_push_action (edit
, p
+ 256);
942 if (edit
->curs1
< edit
->start_display
) {
943 edit
->start_display
--;
953 edit_backspace (WEdit
* edit
)
959 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
960 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
961 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
963 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
964 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
965 mhl_mem_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
966 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
971 edit_modification (edit
);
974 book_mark_dec (edit
, edit
->curs_line
);
977 edit
->force
|= REDRAW_AFTER_CURSOR
;
979 edit_push_action (edit
, p
);
981 if (edit
->curs1
< edit
->start_display
) {
982 edit
->start_display
--;
990 #ifdef FAST_MOVE_CURSOR
992 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
995 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
997 next
-= (unsigned long) dest
;
1005 edit_move_backward_lots (WEdit
*edit
, long increment
)
1010 if (increment
> edit
->curs1
)
1011 increment
= edit
->curs1
;
1014 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1016 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1019 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1024 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1025 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1030 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1031 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1032 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1033 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1036 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1037 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1038 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1043 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1045 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1047 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1048 g_malloc (EDIT_BUF_SIZE
);
1053 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1063 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1065 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1069 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1070 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1073 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1075 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1076 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1081 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1083 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1085 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1086 g_malloc (EDIT_BUF_SIZE
);
1091 return edit_get_byte (edit
, edit
->curs1
);
1094 #endif /* ! FAST_MOVE_CURSOR */
1096 /* moves the cursor right or left: increment positive or negative respectively */
1097 void edit_cursor_move (WEdit
* edit
, long increment
)
1099 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1102 #ifdef FAST_MOVE_CURSOR
1103 if (increment
< -256) {
1104 edit
->force
|= REDRAW_PAGE
;
1105 edit_move_backward_lots (edit
, -increment
);
1108 #endif /* ! FAST_MOVE_CURSOR */
1110 if (increment
< 0) {
1111 for (; increment
< 0; increment
++) {
1115 edit_push_action (edit
, CURS_RIGHT
);
1117 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1118 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1119 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1120 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1122 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1123 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1124 mhl_mem_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1125 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1130 edit
->force
|= REDRAW_LINE_BELOW
;
1134 } else if (increment
> 0) {
1135 for (; increment
> 0; increment
--) {
1139 edit_push_action (edit
, CURS_LEFT
);
1141 c
= edit_get_byte (edit
, edit
->curs1
);
1142 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1143 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1144 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1146 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1147 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1148 mhl_mem_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1149 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1154 edit
->force
|= REDRAW_LINE_ABOVE
;
1160 /* These functions return positions relative to lines */
1162 /* returns index of last char on line + 1 */
1163 long edit_eol (WEdit
* edit
, long current
)
1165 if (current
< edit
->last_byte
) {
1167 if (edit_get_byte (edit
, current
) == '\n')
1170 return edit
->last_byte
;
1174 /* returns index of first char on line */
1175 long edit_bol (WEdit
* edit
, long current
)
1179 if (edit_get_byte (edit
, current
- 1) == '\n')
1187 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1190 if (upto
> edit
->last_byte
)
1191 upto
= edit
->last_byte
;
1194 while (current
< upto
)
1195 if (edit_get_byte (edit
, current
++) == '\n')
1201 /* If lines is zero this returns the count of lines from current to upto. */
1202 /* If upto is zero returns index of lines forward current. */
1203 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1206 return edit_count_lines (edit
, current
, upto
);
1212 next
= edit_eol (edit
, current
) + 1;
1213 if (next
> edit
->last_byte
)
1223 /* Returns offset of 'lines' lines up from current */
1224 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1228 current
= edit_bol (edit
, current
);
1229 while((lines
--) && current
!= 0)
1230 current
= edit_bol (edit
, current
- 1);
1234 /* If cols is zero this returns the count of columns from current to upto. */
1235 /* If upto is zero returns index of cols across from current. */
1236 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1245 q
= edit
->last_byte
+ 2;
1247 for (col
= 0, p
= current
; p
< q
; p
++) {
1255 c
= edit_get_byte (edit
, p
);
1257 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1258 else if (c
== '\n') {
1263 } else if (c
< 32 || c
== 127)
1264 col
+= 2; /* Caret notation for control characters */
1271 /* returns the current column position of the cursor */
1272 int edit_get_col (WEdit
* edit
)
1274 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1278 /* Scrolling functions */
1280 void edit_update_curs_row (WEdit
* edit
)
1282 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1285 void edit_update_curs_col (WEdit
* edit
)
1287 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1290 /*moves the display start position up by i lines */
1291 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1293 unsigned long lines_above
= edit
->start_line
;
1294 if (i
> lines_above
)
1297 edit
->start_line
-= i
;
1298 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1299 edit
->force
|= REDRAW_PAGE
;
1300 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1302 edit_update_curs_row (edit
);
1306 /* returns 1 if could scroll, 0 otherwise */
1307 void edit_scroll_downward (WEdit
* edit
, int i
)
1310 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1311 if (lines_below
> 0) {
1312 if (i
> lines_below
)
1314 edit
->start_line
+= i
;
1315 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1316 edit
->force
|= REDRAW_PAGE
;
1317 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1319 edit_update_curs_row (edit
);
1322 void edit_scroll_right (WEdit
* edit
, int i
)
1324 edit
->force
|= REDRAW_PAGE
;
1325 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1326 edit
->start_col
-= i
;
1329 void edit_scroll_left (WEdit
* edit
, int i
)
1331 if (edit
->start_col
) {
1332 edit
->start_col
+= i
;
1333 if (edit
->start_col
> 0)
1334 edit
->start_col
= 0;
1335 edit
->force
|= REDRAW_PAGE
;
1336 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1340 /* high level cursor movement commands */
1342 static int is_in_indent (WEdit
*edit
)
1344 long p
= edit_bol (edit
, edit
->curs1
);
1345 while (p
< edit
->curs1
)
1346 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1351 static int left_of_four_spaces (WEdit
*edit
);
1354 edit_move_to_prev_col (WEdit
* edit
, long p
)
1356 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1358 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1359 edit_update_curs_col (edit
);
1361 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1362 int q
= edit
->curs_col
;
1363 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1364 p
= edit_bol (edit
, edit
->curs1
);
1365 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1366 if (!left_of_four_spaces (edit
))
1367 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1374 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1376 unsigned long p
, l
= edit
->curs_line
;
1382 edit
->force
|= REDRAW_PAGE
;
1384 edit_scroll_upward (edit
, i
);
1386 p
= edit_bol (edit
, edit
->curs1
);
1387 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1388 edit_move_to_prev_col (edit
, p
);
1390 edit
->search_start
= edit
->curs1
;
1391 edit
->found_len
= 0;
1396 is_blank (WEdit
*edit
, long offset
)
1400 s
= edit_bol (edit
, offset
);
1401 f
= edit_eol (edit
, offset
) - 1;
1403 c
= edit_get_byte (edit
, s
++);
1411 /* returns the offset of line i */
1413 edit_find_line (WEdit
*edit
, int line
)
1417 if (!edit
->caches_valid
) {
1418 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1419 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1420 /* three offsets that we *know* are line 0 at 0 and these two: */
1421 edit
->line_numbers
[1] = edit
->curs_line
;
1422 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1423 edit
->line_numbers
[2] = edit
->total_lines
;
1424 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1425 edit
->caches_valid
= 1;
1427 if (line
>= edit
->total_lines
)
1428 return edit
->line_offsets
[2];
1431 /* find the closest known point */
1432 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1434 n
= abs (edit
->line_numbers
[i
] - line
);
1441 return edit
->line_offsets
[j
]; /* know the offset exactly */
1442 if (m
== 1 && j
>= 3)
1443 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1445 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1446 if (line
> edit
->line_numbers
[j
])
1447 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1449 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1450 edit
->line_numbers
[i
] = line
;
1451 return edit
->line_offsets
[i
];
1454 int line_is_blank (WEdit
* edit
, long line
)
1456 return is_blank (edit
, edit_find_line (edit
, line
));
1459 /* moves up until a blank line is reached, or until just
1460 before a non-blank line is reached */
1461 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1464 if (edit
->curs_line
<= 1) {
1467 if (line_is_blank (edit
, edit
->curs_line
)) {
1468 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1469 for (i
= edit
->curs_line
- 1; i
; i
--)
1470 if (!line_is_blank (edit
, i
)) {
1475 for (i
= edit
->curs_line
- 1; i
; i
--)
1476 if (line_is_blank (edit
, i
))
1480 for (i
= edit
->curs_line
- 1; i
; i
--)
1481 if (line_is_blank (edit
, i
))
1485 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1489 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1491 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1497 edit
->force
|= REDRAW_PAGE
;
1499 edit_scroll_downward (edit
, i
);
1500 p
= edit_bol (edit
, edit
->curs1
);
1501 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1502 edit_move_to_prev_col (edit
, p
);
1504 edit
->search_start
= edit
->curs1
;
1505 edit
->found_len
= 0;
1509 /* moves down until a blank line is reached, or until just
1510 before a non-blank line is reached */
1511 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1514 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1515 i
= edit
->total_lines
;
1517 if (line_is_blank (edit
, edit
->curs_line
)) {
1518 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1519 for (i
= edit
->curs_line
+ 1; i
; i
++)
1520 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1525 for (i
= edit
->curs_line
+ 1; i
; i
++)
1526 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1530 for (i
= edit
->curs_line
+ 1; i
; i
++)
1531 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1535 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1538 static void edit_begin_page (WEdit
*edit
)
1540 edit_update_curs_row (edit
);
1541 edit_move_up (edit
, edit
->curs_row
, 0);
1544 static void edit_end_page (WEdit
*edit
)
1546 edit_update_curs_row (edit
);
1547 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1551 /* goto beginning of text */
1552 static void edit_move_to_top (WEdit
* edit
)
1554 if (edit
->curs_line
) {
1555 edit_cursor_move (edit
, -edit
->curs1
);
1556 edit_move_to_prev_col (edit
, 0);
1557 edit
->force
|= REDRAW_PAGE
;
1558 edit
->search_start
= 0;
1559 edit_update_curs_row(edit
);
1564 /* goto end of text */
1565 static void edit_move_to_bottom (WEdit
* edit
)
1567 if (edit
->curs_line
< edit
->total_lines
) {
1568 edit_cursor_move (edit
, edit
->curs2
);
1569 edit
->start_display
= edit
->last_byte
;
1570 edit
->start_line
= edit
->total_lines
;
1571 edit_update_curs_row(edit
);
1572 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1573 edit
->force
|= REDRAW_PAGE
;
1577 /* goto beginning of line */
1578 static void edit_cursor_to_bol (WEdit
* edit
)
1580 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1581 edit
->search_start
= edit
->curs1
;
1582 edit
->prev_col
= edit_get_col (edit
);
1585 /* goto end of line */
1586 static void edit_cursor_to_eol (WEdit
* edit
)
1588 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1589 edit
->search_start
= edit
->curs1
;
1590 edit
->prev_col
= edit_get_col (edit
);
1593 /* move cursor to line 'line' */
1594 void edit_move_to_line (WEdit
* e
, long line
)
1596 if(line
< e
->curs_line
)
1597 edit_move_up (e
, e
->curs_line
- line
, 0);
1599 edit_move_down (e
, line
- e
->curs_line
, 0);
1600 edit_scroll_screen_over_cursor (e
);
1603 /* scroll window so that first visible line is 'line' */
1604 void edit_move_display (WEdit
* e
, long line
)
1606 if(line
< e
->start_line
)
1607 edit_scroll_upward (e
, e
->start_line
- line
);
1609 edit_scroll_downward (e
, line
- e
->start_line
);
1612 /* save markers onto undo stack */
1613 void edit_push_markers (WEdit
* edit
)
1615 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1616 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1619 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1628 /* highlight marker toggle */
1629 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1631 edit_push_markers (edit
);
1633 edit_set_markers (edit
, 0, 0, 0, 0);
1634 edit
->force
|= REDRAW_PAGE
;
1636 if (edit
->mark2
>= 0) {
1637 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1638 edit
->force
|= REDRAW_PAGE
;
1640 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1644 static unsigned long
1649 const char option_chars_move_whole_word
[] =
1650 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1655 if (*option_chars_move_whole_word
== '!')
1657 return 0x80000000UL
;
1661 else if (islower (c
))
1663 else if (isalpha (c
))
1665 else if (isdigit (c
))
1667 else if (isspace (c
))
1669 q
= strchr (option_chars_move_whole_word
, c
);
1671 return 0xFFFFFFFFUL
;
1673 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1677 } while ((q
= strchr (q
+ 1, c
)));
1682 edit_left_word_move (WEdit
*edit
, int s
)
1686 edit_cursor_move (edit
, -1);
1689 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1690 c2
= edit_get_byte (edit
, edit
->curs1
);
1691 if (!(my_type_of (c1
) & my_type_of (c2
)))
1693 if (isspace (c1
) && !isspace (c2
))
1696 if (!isspace (c1
) && isspace (c2
))
1701 static void edit_left_word_move_cmd (WEdit
* edit
)
1703 edit_left_word_move (edit
, 0);
1704 edit
->force
|= REDRAW_PAGE
;
1708 edit_right_word_move (WEdit
*edit
, int s
)
1712 edit_cursor_move (edit
, 1);
1713 if (edit
->curs1
>= edit
->last_byte
)
1715 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1716 c2
= edit_get_byte (edit
, edit
->curs1
);
1717 if (!(my_type_of (c1
) & my_type_of (c2
)))
1719 if (isspace (c1
) && !isspace (c2
))
1722 if (!isspace (c1
) && isspace (c2
))
1727 static void edit_right_word_move_cmd (WEdit
* edit
)
1729 edit_right_word_move (edit
, 0);
1730 edit
->force
|= REDRAW_PAGE
;
1734 static void edit_right_delete_word (WEdit
* edit
)
1738 if (edit
->curs1
>= edit
->last_byte
)
1740 c1
= edit_delete (edit
);
1741 c2
= edit_get_byte (edit
, edit
->curs1
);
1742 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1744 if (!(my_type_of (c1
) & my_type_of (c2
)))
1749 static void edit_left_delete_word (WEdit
* edit
)
1753 if (edit
->curs1
<= 0)
1755 c1
= edit_backspace (edit
);
1756 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1757 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1759 if (!(my_type_of (c1
) & my_type_of (c2
)))
1765 the start column position is not recorded, and hence does not
1766 undo as it happed. But who would notice.
1769 edit_do_undo (WEdit
* edit
)
1774 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1776 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1781 edit_cursor_move (edit
, 1);
1784 edit_cursor_move (edit
, -1);
1787 edit_backspace (edit
);
1793 column_highlighting
= 1;
1796 column_highlighting
= 0;
1799 if (ac
>= 256 && ac
< 512)
1800 edit_insert_ahead (edit
, ac
- 256);
1801 if (ac
>= 0 && ac
< 256)
1802 edit_insert (edit
, ac
);
1804 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1805 edit
->mark1
= ac
- MARK_1
;
1806 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1807 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1808 edit
->mark2
= ac
- MARK_2
;
1809 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1812 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1815 if (edit
->start_display
> ac
- KEY_PRESS
) {
1816 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1817 edit
->force
|= REDRAW_PAGE
;
1818 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1819 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1820 edit
->force
|= REDRAW_PAGE
;
1822 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1823 edit_update_curs_row (edit
);
1826 edit
->stack_disable
= 0;
1829 static void edit_delete_to_line_end (WEdit
* edit
)
1831 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1838 static void edit_delete_to_line_begin (WEdit
* edit
)
1840 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1843 edit_backspace (edit
);
1848 edit_delete_line (WEdit
*edit
)
1851 * Delete right part of the line.
1852 * Note that edit_get_byte() returns '\n' when byte position is
1855 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1856 (void) edit_delete (edit
);
1861 * Note that edit_delete() will not corrupt anything if called while
1862 * cursor position is EOF.
1864 (void) edit_delete (edit
);
1867 * Delete left part of the line.
1868 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1870 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1871 (void) edit_backspace (edit
);
1875 static void insert_spaces_tab (WEdit
* edit
, int half
)
1878 edit_update_curs_col (edit
);
1879 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1881 edit_insert (edit
, ' ');
1886 static int is_aligned_on_a_tab (WEdit
* edit
)
1888 edit_update_curs_col (edit
);
1889 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1890 return 0; /* not alligned on a tab */
1894 static int right_of_four_spaces (WEdit
*edit
)
1897 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1898 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1900 return is_aligned_on_a_tab (edit
);
1904 static int left_of_four_spaces (WEdit
*edit
)
1907 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1908 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1910 return is_aligned_on_a_tab (edit
);
1914 int edit_indent_width (WEdit
* edit
, long p
)
1917 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1919 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1922 void edit_insert_indent (WEdit
* edit
, int indent
)
1924 if (!option_fill_tabs_with_spaces
) {
1925 while (indent
>= TAB_SIZE
) {
1926 edit_insert (edit
, '\t');
1930 while (indent
-- > 0)
1931 edit_insert (edit
, ' ');
1935 edit_auto_indent (WEdit
* edit
)
1940 /* use the previous line as a template */
1941 p
= edit_move_backward (edit
, p
, 1);
1942 /* copy the leading whitespace of the line */
1943 for (;;) { /* no range check - the line _is_ \n-terminated */
1944 c
= edit_get_byte (edit
, p
++);
1945 if (c
!= ' ' && c
!= '\t')
1947 edit_insert (edit
, c
);
1951 static void edit_double_newline (WEdit
* edit
)
1953 edit_insert (edit
, '\n');
1954 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1956 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1958 edit
->force
|= REDRAW_PAGE
;
1959 edit_insert (edit
, '\n');
1962 static void edit_tab_cmd (WEdit
* edit
)
1966 if (option_fake_half_tabs
) {
1967 if (is_in_indent (edit
)) {
1968 /*insert a half tab (usually four spaces) unless there is a
1969 half tab already behind, then delete it and insert a
1971 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1972 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1973 edit_backspace (edit
);
1974 edit_insert (edit
, '\t');
1976 insert_spaces_tab (edit
, 1);
1981 if (option_fill_tabs_with_spaces
) {
1982 insert_spaces_tab (edit
, 0);
1984 edit_insert (edit
, '\t');
1989 static void check_and_wrap_line (WEdit
* edit
)
1992 if (!option_typewriter_wrap
)
1994 edit_update_curs_col (edit
);
1995 if (edit
->curs_col
< option_word_wrap_line_length
)
2000 c
= edit_get_byte (edit
, curs
);
2001 if (c
== '\n' || curs
<= 0) {
2002 edit_insert (edit
, '\n');
2005 if (c
== ' ' || c
== '\t') {
2006 int current
= edit
->curs1
;
2007 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2008 edit_insert (edit
, '\n');
2009 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2015 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2017 void edit_push_key_press (WEdit
* edit
)
2019 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2020 if (edit
->mark2
== -1)
2021 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2024 /* this find the matching bracket in either direction, and sets edit->bracket */
2025 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2027 const char * const b
= "{}{[][()(", *p
;
2028 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2029 unsigned long j
= 0;
2031 edit_update_curs_row (edit
);
2032 c
= edit_get_byte (edit
, edit
->curs1
);
2035 if (!furthest_bracket_search
)
2036 furthest_bracket_search
--;
2037 /* not on a bracket at all */
2040 /* the matching bracket */
2042 /* going left or right? */
2043 if (strchr ("{[(", c
))
2045 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2046 /* out of buffer? */
2047 if (q
>= edit
->last_byte
|| q
< 0)
2049 a
= edit_get_byte (edit
, q
);
2050 /* don't want to eat CPU */
2051 if (j
++ > furthest_bracket_search
)
2053 /* out of screen? */
2055 if (q
< edit
->start_display
)
2057 /* count lines if searching downward */
2058 if (inc
> 0 && a
== '\n')
2059 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2062 /* count bracket depth */
2063 i
+= (a
== c
) - (a
== d
);
2064 /* return if bracket depth is zero */
2072 static long last_bracket
= -1;
2074 void edit_find_bracket (WEdit
* edit
)
2076 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2077 if (last_bracket
!= edit
->bracket
)
2078 edit
->force
|= REDRAW_PAGE
;
2079 last_bracket
= edit
->bracket
;
2082 static void edit_goto_matching_bracket (WEdit
*edit
)
2085 q
= edit_get_bracket (edit
, 0, 0);
2088 edit
->bracket
= edit
->curs1
;
2089 edit
->force
|= REDRAW_PAGE
;
2090 edit_cursor_move (edit
, q
- edit
->curs1
);
2094 * This executes a command as though the user initiated it through a key
2095 * press. Callback with WIDGET_KEY as a message calls this after
2096 * translating the key press. This function can be used to pass any
2097 * command to the editor. Note that the screen wouldn't update
2098 * automatically. Either of command or char_for_insertion must be
2099 * passed as -1. Commands are executed, and char_for_insertion is
2100 * inserted at the cursor.
2102 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2104 if (command
== CK_Begin_Record_Macro
) {
2106 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2109 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2110 edit
->force
|= REDRAW_COMPLETELY
;
2111 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2115 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2116 edit
->macro
[edit
->macro_i
].command
= command
;
2117 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2119 /* record the beginning of a set of editing actions initiated by a key press */
2120 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2121 edit_push_key_press (edit
);
2123 edit_execute_cmd (edit
, command
, char_for_insertion
);
2124 if (column_highlighting
)
2125 edit
->force
|= REDRAW_PAGE
;
2128 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2131 This executes a command at a lower level than macro recording.
2132 It also does not push a key_press onto the undo stack. This means
2133 that if it is called many times, a single undo command will undo
2134 all of them. It also does not check for the Undo command.
2137 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2139 edit
->force
|= REDRAW_LINE
;
2141 /* The next key press will unhighlight the found string, so update
2143 if (edit
->found_len
|| column_highlighting
)
2144 edit
->force
|= REDRAW_PAGE
;
2146 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2147 column_highlighting
= 0;
2148 if (!edit
->highlight
2149 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2150 edit_mark_cmd (edit
, 1); /* clear */
2151 edit_mark_cmd (edit
, 0); /* marking on */
2153 edit
->highlight
= 1;
2154 } else { /* any other command */
2155 if (edit
->highlight
)
2156 edit_mark_cmd (edit
, 0); /* clear */
2157 edit
->highlight
= 0;
2160 /* first check for undo */
2161 if (command
== CK_Undo
) {
2162 edit_do_undo (edit
);
2163 edit
->found_len
= 0;
2164 edit
->prev_col
= edit_get_col (edit
);
2165 edit
->search_start
= edit
->curs1
;
2169 /* An ordinary key press */
2170 if (char_for_insertion
>= 0) {
2171 if (edit
->overwrite
) {
2172 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2175 edit_insert (edit
, char_for_insertion
);
2176 if (option_auto_para_formatting
) {
2177 format_paragraph (edit
, 0);
2178 edit
->force
|= REDRAW_PAGE
;
2180 check_and_wrap_line (edit
);
2181 edit
->found_len
= 0;
2182 edit
->prev_col
= edit_get_col (edit
);
2183 edit
->search_start
= edit
->curs1
;
2184 edit_find_bracket (edit
);
2190 case CK_Begin_Page_Highlight
:
2191 case CK_End_Page_Highlight
:
2196 case CK_Word_Left_Highlight
:
2197 case CK_Word_Right_Highlight
:
2198 case CK_Up_Highlight
:
2199 case CK_Down_Highlight
:
2200 if (edit
->mark2
== -1)
2201 break; /*marking is following the cursor: may need to highlight a whole line */
2204 case CK_Left_Highlight
:
2205 case CK_Right_Highlight
:
2206 edit
->force
|= REDRAW_CHAR_ONLY
;
2209 /* basic cursor key commands */
2212 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2213 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2215 edit_backspace (edit
);
2218 if (option_fake_half_tabs
) {
2220 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2221 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2222 edit_backspace (edit
);
2227 edit_backspace (edit
);
2230 if (option_fake_half_tabs
) {
2232 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2233 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2240 case CK_Delete_Word_Left
:
2241 edit_left_delete_word (edit
);
2243 case CK_Delete_Word_Right
:
2244 edit_right_delete_word (edit
);
2246 case CK_Delete_Line
:
2247 edit_delete_line (edit
);
2249 case CK_Delete_To_Line_End
:
2250 edit_delete_to_line_end (edit
);
2252 case CK_Delete_To_Line_Begin
:
2253 edit_delete_to_line_begin (edit
);
2256 if (option_auto_para_formatting
) {
2257 edit_double_newline (edit
);
2258 if (option_return_does_auto_indent
)
2259 edit_auto_indent (edit
);
2260 format_paragraph (edit
, 0);
2262 edit_insert (edit
, '\n');
2263 if (option_return_does_auto_indent
) {
2264 edit_auto_indent (edit
);
2269 edit_insert (edit
, '\n');
2273 case CK_Page_Up_Highlight
:
2274 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2277 case CK_Page_Down_Highlight
:
2278 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2281 case CK_Left_Highlight
:
2282 if (option_fake_half_tabs
) {
2283 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2284 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2285 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2289 edit_cursor_move (edit
, -1);
2292 case CK_Right_Highlight
:
2293 if (option_fake_half_tabs
) {
2294 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2295 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2296 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2300 edit_cursor_move (edit
, 1);
2303 case CK_Begin_Page_Highlight
:
2304 edit_begin_page (edit
);
2307 case CK_End_Page_Highlight
:
2308 edit_end_page (edit
);
2311 case CK_Word_Left_Highlight
:
2312 edit_left_word_move_cmd (edit
);
2315 case CK_Word_Right_Highlight
:
2316 edit_right_word_move_cmd (edit
);
2319 case CK_Up_Highlight
:
2320 edit_move_up (edit
, 1, 0);
2323 case CK_Down_Highlight
:
2324 edit_move_down (edit
, 1, 0);
2326 case CK_Paragraph_Up
:
2327 case CK_Paragraph_Up_Highlight
:
2328 edit_move_up_paragraph (edit
, 0);
2330 case CK_Paragraph_Down
:
2331 case CK_Paragraph_Down_Highlight
:
2332 edit_move_down_paragraph (edit
, 0);
2335 case CK_Scroll_Up_Highlight
:
2336 edit_move_up (edit
, 1, 1);
2338 case CK_Scroll_Down
:
2339 case CK_Scroll_Down_Highlight
:
2340 edit_move_down (edit
, 1, 1);
2343 case CK_Home_Highlight
:
2344 edit_cursor_to_bol (edit
);
2347 case CK_End_Highlight
:
2348 edit_cursor_to_eol (edit
);
2352 edit_tab_cmd (edit
);
2353 if (option_auto_para_formatting
) {
2354 format_paragraph (edit
, 0);
2355 edit
->force
|= REDRAW_PAGE
;
2357 check_and_wrap_line (edit
);
2360 case CK_Toggle_Insert
:
2361 edit
->overwrite
= (edit
->overwrite
== 0);
2365 if (edit
->mark2
>= 0) {
2366 if (column_highlighting
)
2367 edit_push_action (edit
, COLUMN_ON
);
2368 column_highlighting
= 0;
2370 edit_mark_cmd (edit
, 0);
2372 case CK_Column_Mark
:
2373 if (!column_highlighting
)
2374 edit_push_action (edit
, COLUMN_OFF
);
2375 column_highlighting
= 1;
2376 edit_mark_cmd (edit
, 0);
2379 if (column_highlighting
)
2380 edit_push_action (edit
, COLUMN_ON
);
2381 column_highlighting
= 0;
2382 edit_mark_cmd (edit
, 1);
2385 case CK_Toggle_Bookmark
:
2386 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2387 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2388 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2390 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2392 case CK_Flush_Bookmarks
:
2393 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2394 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2395 edit
->force
|= REDRAW_PAGE
;
2397 case CK_Next_Bookmark
:
2398 if (edit
->book_mark
) {
2399 struct _book_mark
*p
;
2400 p
= (struct _book_mark
*) book_mark_find (edit
,
2404 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2405 || p
->line
< edit
->start_line
)
2406 edit_move_display (edit
,
2408 edit
->num_widget_lines
/ 2);
2409 edit_move_to_line (edit
, p
->line
);
2413 case CK_Prev_Bookmark
:
2414 if (edit
->book_mark
) {
2415 struct _book_mark
*p
;
2416 p
= (struct _book_mark
*) book_mark_find (edit
,
2418 while (p
->line
== edit
->curs_line
)
2422 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2423 || p
->line
< edit
->start_line
)
2424 edit_move_display (edit
,
2426 edit
->num_widget_lines
/ 2);
2427 edit_move_to_line (edit
, p
->line
);
2432 case CK_Beginning_Of_Text
:
2433 case CK_Beginning_Of_Text_Highlight
:
2434 edit_move_to_top (edit
);
2436 case CK_End_Of_Text
:
2437 case CK_End_Of_Text_Highlight
:
2438 edit_move_to_bottom (edit
);
2442 edit_block_copy_cmd (edit
);
2445 edit_block_delete_cmd (edit
);
2448 edit_block_move_cmd (edit
);
2452 edit_copy_to_X_buf_cmd (edit
);
2455 edit_cut_to_X_buf_cmd (edit
);
2458 edit_paste_from_X_buf_cmd (edit
);
2460 case CK_Selection_History
:
2461 edit_paste_from_history (edit
);
2465 edit_save_as_cmd (edit
);
2468 edit_save_confirm_cmd (edit
);
2471 edit_load_cmd (edit
);
2474 edit_save_block_cmd (edit
);
2476 case CK_Insert_File
:
2477 edit_insert_file_cmd (edit
);
2480 case CK_Toggle_Syntax
:
2481 if ((option_syntax_highlighting
^= 1) == 1)
2482 edit_load_syntax (edit
, NULL
, option_syntax_type
);
2483 edit
->force
|= REDRAW_PAGE
;
2487 edit_search_cmd (edit
, 0);
2490 edit_search_cmd (edit
, 1);
2493 edit_replace_cmd (edit
, 0);
2495 case CK_Replace_Again
:
2496 edit_replace_cmd (edit
, 1);
2498 case CK_Complete_Word
:
2499 edit_complete_word_cmd (edit
);
2503 dlg_stop (edit
->widget
.parent
);
2506 edit_new_cmd (edit
);
2510 edit_help_cmd (edit
);
2514 edit_refresh_cmd (edit
);
2519 /* fool gcc to prevent a Y2K warning */
2520 char time_format
[] = "_c";
2521 time_format
[0] = '%';
2523 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
2524 edit_print_string (edit
, s
);
2525 edit
->force
|= REDRAW_PAGE
;
2529 edit_goto_cmd (edit
);
2531 case CK_Paragraph_Format
:
2532 format_paragraph (edit
, 1);
2533 edit
->force
|= REDRAW_PAGE
;
2535 case CK_Delete_Macro
:
2536 edit_delete_macro_cmd (edit
);
2538 case CK_Match_Bracket
:
2539 edit_goto_matching_bracket (edit
);
2545 edit_sort_cmd (edit
);
2548 edit_ext_cmd (edit
);
2551 edit_mail_dialog (edit
);
2556 case CK_Select_Codepage
:
2557 edit_select_codepage_cmd (edit
);
2559 case CK_Insert_Literal
:
2560 edit_insert_literal_cmd (edit
);
2562 case CK_Execute_Macro
:
2563 edit_execute_macro_cmd (edit
);
2565 case CK_Begin_End_Macro
:
2566 edit_begin_end_macro_cmd (edit
);
2576 if ((command
/ 1000) == 1) /* a shell command */
2577 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2578 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2579 struct macro m
[MAX_MACRO_LENGTH
];
2581 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
2582 edit_execute_macro (edit
, m
, nm
);
2585 /* keys which must set the col position, and the search vars */
2590 case CK_Replace_Again
:
2591 case CK_Complete_Word
:
2592 edit
->prev_col
= edit_get_col (edit
);
2595 case CK_Up_Highlight
:
2597 case CK_Down_Highlight
:
2599 case CK_Page_Up_Highlight
:
2601 case CK_Page_Down_Highlight
:
2602 case CK_Beginning_Of_Text
:
2603 case CK_Beginning_Of_Text_Highlight
:
2604 case CK_End_Of_Text
:
2605 case CK_End_Of_Text_Highlight
:
2606 case CK_Paragraph_Up
:
2607 case CK_Paragraph_Up_Highlight
:
2608 case CK_Paragraph_Down
:
2609 case CK_Paragraph_Down_Highlight
:
2611 case CK_Scroll_Up_Highlight
:
2612 case CK_Scroll_Down
:
2613 case CK_Scroll_Down_Highlight
:
2614 edit
->search_start
= edit
->curs1
;
2615 edit
->found_len
= 0;
2618 edit
->found_len
= 0;
2619 edit
->prev_col
= edit_get_col (edit
);
2620 edit
->search_start
= edit
->curs1
;
2622 edit_find_bracket (edit
);
2624 if (option_auto_para_formatting
) {
2628 case CK_Delete_Word_Left
:
2629 case CK_Delete_Word_Right
:
2630 case CK_Delete_To_Line_End
:
2631 case CK_Delete_To_Line_Begin
:
2632 format_paragraph (edit
, 0);
2633 edit
->force
|= REDRAW_PAGE
;
2640 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2644 if (edit
->macro_depth
++ > 256) {
2645 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2646 edit
->macro_depth
--;
2649 edit
->force
|= REDRAW_PAGE
;
2650 for (; i
< n
; i
++) {
2651 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2653 edit_update_screen (edit
);
2654 edit
->macro_depth
--;
2657 /* User edit menu, like user menu (F2) but only in editor. */
2659 user_menu (WEdit
* edit
)
2664 long start_mark
, end_mark
;
2665 char *block_file
= mhl_str_dir_plus_file (home_dir
, BLOCK_FILE
);
2668 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2669 if (!nomark
) /* remember marked or not */
2670 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2672 /* run shell scripts from menu */
2673 user_menu_cmd (edit
);
2675 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2676 /* no block messages */
2681 /* i.e. we have marked block */
2682 rc
= edit_block_delete_cmd (edit
);
2686 edit_insert_file (edit
, block_file
);
2689 /* truncate block file */
2690 if ((fd
= fopen (block_file
, "w"))) {
2694 edit_refresh_cmd (edit
);
2695 edit
->force
|= REDRAW_COMPLETELY
;
2698 mhl_mem_free (block_file
);