1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <sys/types.h>
36 #include "../src/global.h"
40 #include "edit-widget.h"
41 #include "editcmddef.h"
44 #include "../src/cmd.h" /* view_other_cmd() */
45 #include "../src/user.h" /* user_menu_cmd() */
46 #include "../src/wtools.h" /* query_dialog() */
50 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
51 or EDIT_KEY_EMULATION_EMACS
53 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
55 int option_word_wrap_line_length
= 72;
56 int option_typewriter_wrap
= 0;
57 int option_auto_para_formatting
= 0;
58 int option_tab_spacing
= 8;
59 int option_fill_tabs_with_spaces
= 0;
60 int option_return_does_auto_indent
= 1;
61 int option_backspace_through_tabs
= 0;
62 int option_fake_half_tabs
= 1;
63 int option_save_mode
= EDIT_QUICK_SAVE
;
64 int option_save_position
= 1;
65 int option_max_undo
= 32768;
67 int option_edit_right_extreme
= 0;
68 int option_edit_left_extreme
= 0;
69 int option_edit_top_extreme
= 0;
70 int option_edit_bottom_extreme
= 0;
72 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
73 char *option_backup_ext
= NULL
;
77 * here's a quick sketch of the layout: (don't run this through indent.)
79 * (b1 is buffers1 and b2 is buffers2)
82 * \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
83 * ______________________________________|______________________________________
85 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
86 * |-> |-> |-> |-> |-> |-> |
88 * _<------------------------->|<----------------->_
89 * WEdit->curs2 | WEdit->curs1
94 * file end|||file beginning
104 static void user_menu (WEdit
*edit
);
106 int edit_get_byte (WEdit
* edit
, long byte_index
)
109 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
112 if (byte_index
>= edit
->curs1
) {
113 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
114 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
116 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
121 * Initialize the buffers for an empty files.
124 edit_init_buffers (WEdit
*edit
)
128 for (j
= 0; j
<= MAXBUFF
; j
++) {
129 edit
->buffers1
[j
] = NULL
;
130 edit
->buffers2
[j
] = NULL
;
135 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
139 * Load file OR text into buffers. Set cursor to the beginning of file.
143 edit_load_file_fast (WEdit
*edit
, const char *filename
)
148 edit
->curs2
= edit
->last_byte
;
149 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
151 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
152 GString
*errmsg
= g_string_new(NULL
);
153 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
154 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
155 g_string_free (errmsg
, TRUE
);
159 if (!edit
->buffers2
[buf2
])
160 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
163 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
164 (edit
->curs2
& M_EDIT_BUF_SIZE
),
165 edit
->curs2
& M_EDIT_BUF_SIZE
);
167 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
168 /* edit->buffers2[0] is already allocated */
169 if (!edit
->buffers2
[buf
])
170 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
171 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
178 /* detecting an error on save is easy: just check if every byte has been written. */
179 /* detecting an error on read, is not so easy 'cos there is not way to tell
180 whether you read everything or not. */
181 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
182 static const struct edit_filters
{
183 const char *read
, *write
, *extension
;
185 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
186 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
187 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
190 /* Return index of the filter or -1 is there is no appropriate filter */
191 static int edit_find_filter (const char *filename
)
196 l
= strlen (filename
);
197 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
198 e
= strlen (all_filters
[i
].extension
);
200 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
207 edit_get_filter (const char *filename
)
210 char *p
, *quoted_name
;
211 i
= edit_find_filter (filename
);
214 quoted_name
= name_quote (filename
, 0);
215 l
= strlen (quoted_name
);
216 p
= g_malloc (strlen (all_filters
[i
].read
) + l
+ 2);
217 sprintf (p
, all_filters
[i
].read
, quoted_name
);
218 g_free (quoted_name
);
223 edit_get_write_filter (const char *write_name
, const char *filename
)
227 i
= edit_find_filter (filename
);
230 writename
= name_quote (write_name
, 0);
231 l
= strlen (writename
);
232 p
= g_malloc (strlen (all_filters
[i
].write
) + l
+ 2);
233 sprintf (p
, all_filters
[i
].write
, writename
);
239 edit_insert_stream (WEdit
* edit
, FILE * f
)
243 while ((c
= fgetc (f
)) >= 0) {
244 edit_insert (edit
, c
);
250 long edit_write_stream (WEdit
* edit
, FILE * f
)
253 for (i
= 0; i
< edit
->last_byte
; i
++)
254 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
259 #define TEMP_BUF_LEN 1024
261 /* inserts a file at the cursor, returns 1 on success */
263 edit_insert_file (WEdit
*edit
, const char *filename
)
266 if ((p
= edit_get_filter (filename
))) {
268 long current
= edit
->curs1
;
269 f
= (FILE *) popen (p
, "r");
271 edit_insert_stream (edit
, f
);
272 edit_cursor_move (edit
, current
- edit
->curs1
);
273 if (pclose (f
) > 0) {
274 GString
*errmsg
= g_string_new (NULL
);
275 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
276 edit_error_dialog (_("Error"), errmsg
->str
);
277 g_string_free (errmsg
, TRUE
);
282 GString
*errmsg
= g_string_new (NULL
);
283 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
284 edit_error_dialog (_("Error"), errmsg
->str
);
285 g_string_free (errmsg
, TRUE
);
291 int i
, file
, blocklen
;
292 long current
= edit
->curs1
;
294 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
296 buf
= g_malloc (TEMP_BUF_LEN
);
297 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
298 for (i
= 0; i
< blocklen
; i
++)
299 edit_insert (edit
, buf
[i
]);
301 edit_cursor_move (edit
, current
- edit
->curs1
);
310 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
312 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
315 GString
*errmsg
= (GString
*) 0;
317 /* Try opening an existing file */
318 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
322 * Try creating the file. O_EXCL prevents following broken links
323 * and opening existing files.
327 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
330 g_string_sprintf (errmsg
= g_string_new (NULL
),
331 _(" Cannot open %s for reading "), filename
);
334 /* New file, delete it if it's not modified or saved */
335 edit
->delete_file
= 1;
339 /* Check what we have opened */
340 if (mc_fstat (file
, st
) < 0) {
341 g_string_sprintf (errmsg
= g_string_new (NULL
),
342 _(" Cannot get size/permissions for %s "), filename
);
346 /* We want to open regular files only */
347 if (!S_ISREG (st
->st_mode
)) {
348 g_string_sprintf (errmsg
= g_string_new (NULL
),
349 _(" %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 g_string_sprintf (errmsg
= g_string_new (NULL
),
363 _(" File %s is too large "), filename
);
368 (void) mc_close (file
);
370 edit_error_dialog (_("Error"), errmsg
->str
);
371 g_string_free (errmsg
, TRUE
);
378 * Open the file and load it into the buffers, either directly or using
379 * a filter. Return 0 on success, 1 on error.
381 * Fast loading (edit_load_file_fast) is used when the file size is
382 * known. In this case the data is read into the buffers by blocks.
383 * If the file size is not known, the data is loaded byte by byte in
387 edit_load_file (WEdit
*edit
)
391 /* Cannot do fast load if a filter is used */
392 if (edit_find_filter (edit
->filename
) >= 0)
396 * VFS may report file size incorrectly, and slow load is not a big
397 * deal considering overhead in VFS.
399 if (!vfs_file_is_local (edit
->filename
))
403 * FIXME: line end translation should disable fast loading as well
404 * Consider doing fseek() to the end and ftell() for the real size.
407 if (*edit
->filename
) {
408 /* If we are dealing with a real file, check that it exists */
409 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
412 /* nothing to load */
416 edit_init_buffers (edit
);
419 edit
->last_byte
= edit
->stat1
.st_size
;
420 edit_load_file_fast (edit
, edit
->filename
);
421 /* If fast load was used, the number of lines wasn't calculated */
422 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
425 if (*edit
->filename
) {
426 edit
->stack_disable
= 1;
427 if (!edit_insert_file (edit
, edit
->filename
)) {
431 edit
->stack_disable
= 0;
437 /* Restore saved cursor position in the file */
439 edit_load_position (WEdit
*edit
)
444 if (!edit
->filename
|| !*edit
->filename
)
447 filename
= vfs_canon (edit
->filename
);
448 load_file_position (filename
, &line
, &column
);
451 edit_move_to_line (edit
, line
- 1);
452 edit
->prev_col
= column
;
453 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
454 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
457 /* Save cursor position in the file */
459 edit_save_position (WEdit
*edit
)
463 if (!edit
->filename
|| !*edit
->filename
)
466 filename
= vfs_canon (edit
->filename
);
467 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
471 /* Clean the WEdit stricture except the widget part */
473 edit_purge_widget (WEdit
*edit
)
475 int len
= sizeof (WEdit
) - sizeof (Widget
);
476 char *start
= (char *) edit
+ sizeof (Widget
);
477 memset (start
, 0, len
);
478 edit
->macro_i
= -1; /* not recording a macro */
481 #define space_width 1
484 * Fill in the edit structure. Return NULL on failure. Pass edit as
485 * NULL to allocate a new structure.
487 * If line is 0, try to restore saved position. Otherwise put the
488 * cursor on that line and show it in the middle of the screen.
491 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
495 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
500 * Expand option_whole_chars_search by national letters using
504 static char option_whole_chars_search_buf
[256];
506 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
508 size_t len
= strlen (option_whole_chars_search
);
510 strcpy (option_whole_chars_search_buf
,
511 option_whole_chars_search
);
513 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
514 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
515 option_whole_chars_search_buf
[len
++] = i
;
519 option_whole_chars_search_buf
[len
] = 0;
520 option_whole_chars_search
= option_whole_chars_search_buf
;
522 #endif /* ENABLE_NLS */
523 edit
= g_malloc0 (sizeof (WEdit
));
526 edit_purge_widget (edit
);
527 edit
->num_widget_lines
= lines
;
528 edit
->num_widget_columns
= columns
;
529 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
530 edit
->stat1
.st_uid
= getuid ();
531 edit
->stat1
.st_gid
= getgid ();
532 edit
->stat1
.st_mtime
= 0;
534 edit
->force
|= REDRAW_PAGE
;
535 edit_set_filename (edit
, filename
);
536 edit
->stack_size
= START_STACK_SIZE
;
537 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
538 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
539 if (edit_load_file (edit
)) {
540 /* edit_load_file already gives an error message */
545 edit
->loading_done
= 1;
548 edit_load_syntax (edit
, 0, 0);
551 edit_get_syntax_color (edit
, -1, &color
);
554 /* load saved cursor position */
555 if ((line
== 0) && option_save_position
) {
556 edit_load_position (edit
);
560 edit_move_display (edit
, line
- 1);
561 edit_move_to_line (edit
, line
- 1);
564 edit_load_user_map(edit
);
569 /* Clear the edit struct, freeing everything in it. Return 1 on success */
571 edit_clean (WEdit
*edit
)
578 /* a stale lock, remove it */
580 edit
->locked
= edit_unlock_file (edit
->filename
);
582 /* save cursor position */
583 if (option_save_position
)
584 edit_save_position (edit
);
586 /* File specified on the mcedit command line and never saved */
587 if (edit
->delete_file
)
588 unlink (edit
->filename
);
590 edit_free_syntax_rules (edit
);
591 book_mark_flush (edit
, -1);
592 for (; j
<= MAXBUFF
; j
++) {
593 g_free (edit
->buffers1
[j
]);
594 g_free (edit
->buffers2
[j
]);
597 g_free (edit
->undo_stack
);
598 g_free (edit
->filename
);
601 edit_purge_widget (edit
);
603 /* Free temporary strings used in catstrs() */
610 /* returns 1 on success */
611 int edit_renew (WEdit
* edit
)
613 int lines
= edit
->num_widget_lines
;
614 int columns
= edit
->num_widget_columns
;
618 if (!edit_init (edit
, lines
, columns
, "", 0))
624 * Load a new file into the editor. If it fails, preserve the old file.
625 * To do it, allocate a new widget, initialize it and, if the new file
626 * was loaded, copy the data to the old widget.
627 * Return 1 on success, 0 on failure.
630 edit_reload (WEdit
*edit
, const char *filename
)
633 int lines
= edit
->num_widget_lines
;
634 int columns
= edit
->num_widget_columns
;
636 e
= g_malloc0 (sizeof (WEdit
));
637 e
->widget
= edit
->widget
;
638 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
643 memcpy (edit
, e
, sizeof (WEdit
));
650 Recording stack for undo:
651 The following is an implementation of a compressed stack. Identical
652 pushes are recorded by a negative prefix indicating the number of times the
653 same char was pushed. This saves space for repeated curs-left or curs-right
670 If the stack long int is 0-255 it represents a normal insert (from a backspace),
671 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
672 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
673 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
676 The only way the cursor moves or the buffer is changed is through the routines:
677 insert, backspace, insert_ahead, delete, and cursor_move.
678 These record the reverse undo movements onto the stack each time they are
681 Each key press results in a set of actions (insert; delete ...). So each time
682 a key is pressed the current position of start_display is pushed as
683 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
684 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
685 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
689 void edit_push_action (WEdit
* edit
, long c
,...)
691 unsigned long sp
= edit
->stack_pointer
;
695 /* first enlarge the stack if necessary */
696 if (sp
> edit
->stack_size
- 10) { /* say */
697 if (option_max_undo
< 256)
698 option_max_undo
= 256;
699 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
700 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
702 edit
->undo_stack
= t
;
703 edit
->stack_size
<<= 1;
704 edit
->stack_size_mask
= edit
->stack_size
- 1;
708 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
709 if (edit
->stack_disable
)
712 #ifdef FAST_MOVE_CURSOR
713 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
715 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
716 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
718 c
= -(va_arg (ap
, int));
721 #endif /* ! FAST_MOVE_CURSOR */
722 if (edit
->stack_bottom
!= sp
723 && spm1
!= edit
->stack_bottom
724 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
726 if (edit
->undo_stack
[spm1
] < 0) {
727 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
729 if (edit
->undo_stack
[spm1
] > -1000000000) {
730 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
731 edit
->undo_stack
[spm1
]--;
735 /* #define NO_STACK_CURSMOVE_ANIHILATION */
736 #ifndef NO_STACK_CURSMOVE_ANIHILATION
737 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
738 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
739 if (edit
->undo_stack
[spm1
] == -2)
740 edit
->stack_pointer
= spm1
;
742 edit
->undo_stack
[spm1
]++;
747 d
= edit
->undo_stack
[spm1
];
750 return; /* --> no need to push multiple do-nothings */
751 edit
->undo_stack
[sp
] = -2;
754 #ifndef NO_STACK_CURSMOVE_ANIHILATION
755 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
756 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
757 edit
->stack_pointer
= spm1
;
763 edit
->undo_stack
[sp
] = c
;
766 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
768 /* if the sp wraps round and catches the stack_bottom then erase
769 * the first set of actions on the stack to make space - by moving
770 * stack_bottom forward one "key press" */
771 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
772 if ((unsigned long) c
== edit
->stack_bottom
||
773 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
775 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
776 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
778 /*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: */
779 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
780 edit
->stack_bottom
= edit
->stack_pointer
= 0;
784 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
785 then the file should be as it was when he loaded up. Then set edit->modified to 0.
788 pop_action (WEdit
* edit
)
791 unsigned long sp
= edit
->stack_pointer
;
792 if (sp
== edit
->stack_bottom
) {
795 sp
= (sp
- 1) & edit
->stack_size_mask
;
796 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
797 /* edit->undo_stack[sp] = '@'; */
798 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
801 if (sp
== edit
->stack_bottom
) {
804 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
805 if (edit
->undo_stack
[sp
] == -2) {
806 /* edit->undo_stack[sp] = '@'; */
807 edit
->stack_pointer
= sp
;
809 edit
->undo_stack
[sp
]++;
814 /* is called whenever a modification is made by one of the four routines below */
815 static inline void edit_modification (WEdit
* edit
)
817 edit
->caches_valid
= 0;
818 edit
->screen_modified
= 1;
820 /* raise lock when file modified */
821 if (!edit
->modified
&& !edit
->delete_file
)
822 edit
->locked
= edit_lock_file (edit
->filename
);
827 Basic low level single character buffer alterations and movements at the cursor.
828 Returns char passed over, inserted or removed.
832 edit_insert (WEdit
*edit
, int c
)
834 /* check if file has grown to large */
835 if (edit
->last_byte
>= SIZE_LIMIT
)
838 /* first we must update the position of the display window */
839 if (edit
->curs1
< edit
->start_display
) {
840 edit
->start_display
++;
845 /* Mark file as modified, unless the file hasn't been fully loaded */
846 if (edit
->loading_done
) {
847 edit_modification (edit
);
850 /* now we must update some info on the file and check if a redraw is required */
853 book_mark_inc (edit
, edit
->curs_line
);
856 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
859 /* save the reverse command onto the undo stack */
860 edit_push_action (edit
, BACKSPACE
);
863 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
864 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
865 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
867 /* add a new buffer if we've reached the end of the last one */
868 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
869 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
870 g_malloc (EDIT_BUF_SIZE
);
872 /* perform the insertion */
873 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
874 curs1
& M_EDIT_BUF_SIZE
]
877 /* update file length */
880 /* update cursor position */
885 /* same as edit_insert and move left */
886 void edit_insert_ahead (WEdit
* edit
, int c
)
888 if (edit
->last_byte
>= SIZE_LIMIT
)
890 if (edit
->curs1
< edit
->start_display
) {
891 edit
->start_display
++;
895 edit_modification (edit
);
898 book_mark_inc (edit
, edit
->curs_line
);
900 edit
->force
|= REDRAW_AFTER_CURSOR
;
902 edit_push_action (edit
, DELCHAR
);
904 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
905 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
906 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
908 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
909 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
910 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
917 int edit_delete (WEdit
* edit
)
923 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
924 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
925 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
927 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
929 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
930 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
931 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
936 edit_modification (edit
);
939 book_mark_dec (edit
, edit
->curs_line
);
941 edit
->force
|= REDRAW_AFTER_CURSOR
;
943 edit_push_action (edit
, p
+ 256);
944 if (edit
->curs1
< edit
->start_display
) {
945 edit
->start_display
--;
955 edit_backspace (WEdit
* edit
)
961 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
962 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
963 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
965 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
966 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
967 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
968 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
973 edit_modification (edit
);
976 book_mark_dec (edit
, edit
->curs_line
);
979 edit
->force
|= REDRAW_AFTER_CURSOR
;
981 edit_push_action (edit
, p
);
983 if (edit
->curs1
< edit
->start_display
) {
984 edit
->start_display
--;
992 #ifdef FAST_MOVE_CURSOR
994 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
997 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
999 next
-= (unsigned long) dest
;
1007 edit_move_backward_lots (WEdit
*edit
, long increment
)
1012 if (increment
> edit
->curs1
)
1013 increment
= edit
->curs1
;
1016 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1018 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1021 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1026 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1027 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1032 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1033 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1034 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1035 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1038 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1039 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1040 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1045 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1047 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1049 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1050 g_malloc (EDIT_BUF_SIZE
);
1055 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1065 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1067 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1071 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1072 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1075 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1077 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1078 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1083 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1085 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1087 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1088 g_malloc (EDIT_BUF_SIZE
);
1093 return edit_get_byte (edit
, edit
->curs1
);
1096 #endif /* ! FAST_MOVE_CURSOR */
1098 /* moves the cursor right or left: increment positive or negative respectively */
1099 void edit_cursor_move (WEdit
* edit
, long increment
)
1101 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1104 #ifdef FAST_MOVE_CURSOR
1105 if (increment
< -256) {
1106 edit
->force
|= REDRAW_PAGE
;
1107 edit_move_backward_lots (edit
, -increment
);
1110 #endif /* ! FAST_MOVE_CURSOR */
1112 if (increment
< 0) {
1113 for (; increment
< 0; increment
++) {
1117 edit_push_action (edit
, CURS_RIGHT
);
1119 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1120 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1121 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1122 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1124 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1125 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1126 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1127 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1132 edit
->force
|= REDRAW_LINE_BELOW
;
1136 } else if (increment
> 0) {
1137 for (; increment
> 0; increment
--) {
1141 edit_push_action (edit
, CURS_LEFT
);
1143 c
= edit_get_byte (edit
, edit
->curs1
);
1144 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1145 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1146 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1148 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1149 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1150 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1151 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1156 edit
->force
|= REDRAW_LINE_ABOVE
;
1162 /* These functions return positions relative to lines */
1164 /* returns index of last char on line + 1 */
1165 long edit_eol (WEdit
* edit
, long current
)
1167 if (current
< edit
->last_byte
) {
1169 if (edit_get_byte (edit
, current
) == '\n')
1172 return edit
->last_byte
;
1176 /* returns index of first char on line */
1177 long edit_bol (WEdit
* edit
, long current
)
1181 if (edit_get_byte (edit
, current
- 1) == '\n')
1189 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1192 if (upto
> edit
->last_byte
)
1193 upto
= edit
->last_byte
;
1196 while (current
< upto
)
1197 if (edit_get_byte (edit
, current
++) == '\n')
1203 /* If lines is zero this returns the count of lines from current to upto. */
1204 /* If upto is zero returns index of lines forward current. */
1205 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1208 return edit_count_lines (edit
, current
, upto
);
1214 next
= edit_eol (edit
, current
) + 1;
1215 if (next
> edit
->last_byte
)
1225 /* Returns offset of 'lines' lines up from current */
1226 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1230 current
= edit_bol (edit
, current
);
1231 while((lines
--) && current
!= 0)
1232 current
= edit_bol (edit
, current
- 1);
1236 /* If cols is zero this returns the count of columns from current to upto. */
1237 /* If upto is zero returns index of cols across from current. */
1238 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1247 q
= edit
->last_byte
+ 2;
1249 for (col
= 0, p
= current
; p
< q
; p
++) {
1257 c
= edit_get_byte (edit
, p
);
1259 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1260 else if (c
== '\n') {
1265 } else if (c
< 32 || c
== 127)
1266 col
+= 2; /* Caret notation for control characters */
1273 /* returns the current column position of the cursor */
1274 int edit_get_col (WEdit
* edit
)
1276 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1280 /* Scrolling functions */
1282 void edit_update_curs_row (WEdit
* edit
)
1284 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1287 void edit_update_curs_col (WEdit
* edit
)
1289 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1292 /*moves the display start position up by i lines */
1293 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1295 unsigned long lines_above
= edit
->start_line
;
1296 if (i
> lines_above
)
1299 edit
->start_line
-= i
;
1300 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1301 edit
->force
|= REDRAW_PAGE
;
1302 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1304 edit_update_curs_row (edit
);
1308 /* returns 1 if could scroll, 0 otherwise */
1309 void edit_scroll_downward (WEdit
* edit
, int i
)
1312 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1313 if (lines_below
> 0) {
1314 if (i
> lines_below
)
1316 edit
->start_line
+= i
;
1317 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1318 edit
->force
|= REDRAW_PAGE
;
1319 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1321 edit_update_curs_row (edit
);
1324 void edit_scroll_right (WEdit
* edit
, int i
)
1326 edit
->force
|= REDRAW_PAGE
;
1327 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1328 edit
->start_col
-= i
;
1331 void edit_scroll_left (WEdit
* edit
, int i
)
1333 if (edit
->start_col
) {
1334 edit
->start_col
+= i
;
1335 if (edit
->start_col
> 0)
1336 edit
->start_col
= 0;
1337 edit
->force
|= REDRAW_PAGE
;
1338 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1342 /* high level cursor movement commands */
1344 static int is_in_indent (WEdit
*edit
)
1346 long p
= edit_bol (edit
, edit
->curs1
);
1347 while (p
< edit
->curs1
)
1348 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1353 static int left_of_four_spaces (WEdit
*edit
);
1356 edit_move_to_prev_col (WEdit
* edit
, long p
)
1358 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1360 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1361 edit_update_curs_col (edit
);
1363 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1364 int q
= edit
->curs_col
;
1365 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1366 p
= edit_bol (edit
, edit
->curs1
);
1367 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1368 if (!left_of_four_spaces (edit
))
1369 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1376 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1378 unsigned long p
, l
= edit
->curs_line
;
1384 edit
->force
|= REDRAW_PAGE
;
1386 edit_scroll_upward (edit
, i
);
1388 p
= edit_bol (edit
, edit
->curs1
);
1389 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1390 edit_move_to_prev_col (edit
, p
);
1392 edit
->search_start
= edit
->curs1
;
1393 edit
->found_len
= 0;
1398 is_blank (WEdit
*edit
, long offset
)
1402 s
= edit_bol (edit
, offset
);
1403 f
= edit_eol (edit
, offset
) - 1;
1405 c
= edit_get_byte (edit
, s
++);
1413 /* returns the offset of line i */
1415 edit_find_line (WEdit
*edit
, int line
)
1419 if (!edit
->caches_valid
) {
1420 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1421 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1422 /* three offsets that we *know* are line 0 at 0 and these two: */
1423 edit
->line_numbers
[1] = edit
->curs_line
;
1424 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1425 edit
->line_numbers
[2] = edit
->total_lines
;
1426 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1427 edit
->caches_valid
= 1;
1429 if (line
>= edit
->total_lines
)
1430 return edit
->line_offsets
[2];
1433 /* find the closest known point */
1434 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1436 n
= abs (edit
->line_numbers
[i
] - line
);
1443 return edit
->line_offsets
[j
]; /* know the offset exactly */
1444 if (m
== 1 && j
>= 3)
1445 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1447 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1448 if (line
> edit
->line_numbers
[j
])
1449 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1451 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1452 edit
->line_numbers
[i
] = line
;
1453 return edit
->line_offsets
[i
];
1456 int line_is_blank (WEdit
* edit
, long line
)
1458 return is_blank (edit
, edit_find_line (edit
, line
));
1461 /* moves up until a blank line is reached, or until just
1462 before a non-blank line is reached */
1463 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1466 if (edit
->curs_line
<= 1) {
1469 if (line_is_blank (edit
, edit
->curs_line
)) {
1470 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1471 for (i
= edit
->curs_line
- 1; i
; i
--)
1472 if (!line_is_blank (edit
, i
)) {
1477 for (i
= edit
->curs_line
- 1; i
; i
--)
1478 if (line_is_blank (edit
, i
))
1482 for (i
= edit
->curs_line
- 1; i
; i
--)
1483 if (line_is_blank (edit
, i
))
1487 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1491 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1493 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1499 edit
->force
|= REDRAW_PAGE
;
1501 edit_scroll_downward (edit
, i
);
1502 p
= edit_bol (edit
, edit
->curs1
);
1503 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1504 edit_move_to_prev_col (edit
, p
);
1506 edit
->search_start
= edit
->curs1
;
1507 edit
->found_len
= 0;
1511 /* moves down until a blank line is reached, or until just
1512 before a non-blank line is reached */
1513 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1516 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1517 i
= edit
->total_lines
;
1519 if (line_is_blank (edit
, edit
->curs_line
)) {
1520 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1521 for (i
= edit
->curs_line
+ 1; i
; i
++)
1522 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1527 for (i
= edit
->curs_line
+ 1; i
; i
++)
1528 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1532 for (i
= edit
->curs_line
+ 1; i
; i
++)
1533 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1537 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1540 static void edit_begin_page (WEdit
*edit
)
1542 edit_update_curs_row (edit
);
1543 edit_move_up (edit
, edit
->curs_row
, 0);
1546 static void edit_end_page (WEdit
*edit
)
1548 edit_update_curs_row (edit
);
1549 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1553 /* goto beginning of text */
1554 static void edit_move_to_top (WEdit
* edit
)
1556 if (edit
->curs_line
) {
1557 edit_cursor_move (edit
, -edit
->curs1
);
1558 edit_move_to_prev_col (edit
, 0);
1559 edit
->force
|= REDRAW_PAGE
;
1560 edit
->search_start
= 0;
1561 edit_update_curs_row(edit
);
1566 /* goto end of text */
1567 static void edit_move_to_bottom (WEdit
* edit
)
1569 if (edit
->curs_line
< edit
->total_lines
) {
1570 edit_cursor_move (edit
, edit
->curs2
);
1571 edit
->start_display
= edit
->last_byte
;
1572 edit
->start_line
= edit
->total_lines
;
1573 edit_update_curs_row(edit
);
1574 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1575 edit
->force
|= REDRAW_PAGE
;
1579 /* goto beginning of line */
1580 static void edit_cursor_to_bol (WEdit
* edit
)
1582 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1583 edit
->search_start
= edit
->curs1
;
1584 edit
->prev_col
= edit_get_col (edit
);
1587 /* goto end of line */
1588 static void edit_cursor_to_eol (WEdit
* edit
)
1590 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1591 edit
->search_start
= edit
->curs1
;
1592 edit
->prev_col
= edit_get_col (edit
);
1595 /* move cursor to line 'line' */
1596 void edit_move_to_line (WEdit
* e
, long line
)
1598 if(line
< e
->curs_line
)
1599 edit_move_up (e
, e
->curs_line
- line
, 0);
1601 edit_move_down (e
, line
- e
->curs_line
, 0);
1602 edit_scroll_screen_over_cursor (e
);
1605 /* scroll window so that first visible line is 'line' */
1606 void edit_move_display (WEdit
* e
, long line
)
1608 if(line
< e
->start_line
)
1609 edit_scroll_upward (e
, e
->start_line
- line
);
1611 edit_scroll_downward (e
, line
- e
->start_line
);
1614 /* save markers onto undo stack */
1615 void edit_push_markers (WEdit
* edit
)
1617 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1618 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1621 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1630 /* highlight marker toggle */
1631 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1633 edit_push_markers (edit
);
1635 edit_set_markers (edit
, 0, 0, 0, 0);
1636 edit
->force
|= REDRAW_PAGE
;
1638 if (edit
->mark2
>= 0) {
1639 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1640 edit
->force
|= REDRAW_PAGE
;
1642 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1646 static unsigned long
1651 const char option_chars_move_whole_word
[] =
1652 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1657 if (*option_chars_move_whole_word
== '!')
1659 return 0x80000000UL
;
1663 else if (islower (c
))
1665 else if (isalpha (c
))
1667 else if (isdigit (c
))
1669 else if (isspace (c
))
1671 q
= strchr (option_chars_move_whole_word
, c
);
1673 return 0xFFFFFFFFUL
;
1675 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1679 } while ((q
= strchr (q
+ 1, c
)));
1684 edit_left_word_move (WEdit
*edit
, int s
)
1688 edit_cursor_move (edit
, -1);
1691 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1692 c2
= edit_get_byte (edit
, edit
->curs1
);
1693 if (!(my_type_of (c1
) & my_type_of (c2
)))
1695 if (isspace (c1
) && !isspace (c2
))
1698 if (!isspace (c1
) && isspace (c2
))
1703 static void edit_left_word_move_cmd (WEdit
* edit
)
1705 edit_left_word_move (edit
, 0);
1706 edit
->force
|= REDRAW_PAGE
;
1710 edit_right_word_move (WEdit
*edit
, int s
)
1714 edit_cursor_move (edit
, 1);
1715 if (edit
->curs1
>= edit
->last_byte
)
1717 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1718 c2
= edit_get_byte (edit
, edit
->curs1
);
1719 if (!(my_type_of (c1
) & my_type_of (c2
)))
1721 if (isspace (c1
) && !isspace (c2
))
1724 if (!isspace (c1
) && isspace (c2
))
1729 static void edit_right_word_move_cmd (WEdit
* edit
)
1731 edit_right_word_move (edit
, 0);
1732 edit
->force
|= REDRAW_PAGE
;
1736 static void edit_right_delete_word (WEdit
* edit
)
1740 if (edit
->curs1
>= edit
->last_byte
)
1742 c1
= edit_delete (edit
);
1743 c2
= edit_get_byte (edit
, edit
->curs1
);
1744 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1746 if (!(my_type_of (c1
) & my_type_of (c2
)))
1751 static void edit_left_delete_word (WEdit
* edit
)
1755 if (edit
->curs1
<= 0)
1757 c1
= edit_backspace (edit
);
1758 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1759 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1761 if (!(my_type_of (c1
) & my_type_of (c2
)))
1767 the start column position is not recorded, and hence does not
1768 undo as it happed. But who would notice.
1771 edit_do_undo (WEdit
* edit
)
1776 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1778 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1783 edit_cursor_move (edit
, 1);
1786 edit_cursor_move (edit
, -1);
1789 edit_backspace (edit
);
1795 column_highlighting
= 1;
1798 column_highlighting
= 0;
1801 if (ac
>= 256 && ac
< 512)
1802 edit_insert_ahead (edit
, ac
- 256);
1803 if (ac
>= 0 && ac
< 256)
1804 edit_insert (edit
, ac
);
1806 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1807 edit
->mark1
= ac
- MARK_1
;
1808 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1809 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1810 edit
->mark2
= ac
- MARK_2
;
1811 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1814 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1817 if (edit
->start_display
> ac
- KEY_PRESS
) {
1818 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1819 edit
->force
|= REDRAW_PAGE
;
1820 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1821 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1822 edit
->force
|= REDRAW_PAGE
;
1824 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1825 edit_update_curs_row (edit
);
1828 edit
->stack_disable
= 0;
1831 static void edit_delete_to_line_end (WEdit
* edit
)
1833 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1840 static void edit_delete_to_line_begin (WEdit
* edit
)
1842 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1845 edit_backspace (edit
);
1850 edit_delete_line (WEdit
*edit
)
1853 * Delete right part of the line.
1854 * Note that edit_get_byte() returns '\n' when byte position is
1857 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1858 (void) edit_delete (edit
);
1863 * Note that edit_delete() will not corrupt anything if called while
1864 * cursor position is EOF.
1866 (void) edit_delete (edit
);
1869 * Delete left part of the line.
1870 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1872 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1873 (void) edit_backspace (edit
);
1877 static void insert_spaces_tab (WEdit
* edit
, int half
)
1880 edit_update_curs_col (edit
);
1881 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1883 edit_insert (edit
, ' ');
1888 static int is_aligned_on_a_tab (WEdit
* edit
)
1890 edit_update_curs_col (edit
);
1891 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1892 return 0; /* not alligned on a tab */
1896 static int right_of_four_spaces (WEdit
*edit
)
1899 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1900 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1902 return is_aligned_on_a_tab (edit
);
1906 static int left_of_four_spaces (WEdit
*edit
)
1909 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1910 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1912 return is_aligned_on_a_tab (edit
);
1916 int edit_indent_width (WEdit
* edit
, long p
)
1919 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1921 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1924 void edit_insert_indent (WEdit
* edit
, int indent
)
1926 if (!option_fill_tabs_with_spaces
) {
1927 while (indent
>= TAB_SIZE
) {
1928 edit_insert (edit
, '\t');
1932 while (indent
-- > 0)
1933 edit_insert (edit
, ' ');
1937 edit_auto_indent (WEdit
* edit
)
1942 /* use the previous line as a template */
1943 p
= edit_move_backward (edit
, p
, 1);
1944 /* copy the leading whitespace of the line */
1945 for (;;) { /* no range check - the line _is_ \n-terminated */
1946 c
= edit_get_byte (edit
, p
++);
1947 if (c
!= ' ' && c
!= '\t')
1949 edit_insert (edit
, c
);
1953 static void edit_double_newline (WEdit
* edit
)
1955 edit_insert (edit
, '\n');
1956 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1958 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1960 edit
->force
|= REDRAW_PAGE
;
1961 edit_insert (edit
, '\n');
1964 static void edit_tab_cmd (WEdit
* edit
)
1968 if (option_fake_half_tabs
) {
1969 if (is_in_indent (edit
)) {
1970 /*insert a half tab (usually four spaces) unless there is a
1971 half tab already behind, then delete it and insert a
1973 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1974 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1975 edit_backspace (edit
);
1976 edit_insert (edit
, '\t');
1978 insert_spaces_tab (edit
, 1);
1983 if (option_fill_tabs_with_spaces
) {
1984 insert_spaces_tab (edit
, 0);
1986 edit_insert (edit
, '\t');
1991 static void check_and_wrap_line (WEdit
* edit
)
1994 if (!option_typewriter_wrap
)
1996 edit_update_curs_col (edit
);
1997 if (edit
->curs_col
< option_word_wrap_line_length
)
2002 c
= edit_get_byte (edit
, curs
);
2003 if (c
== '\n' || curs
<= 0) {
2004 edit_insert (edit
, '\n');
2007 if (c
== ' ' || c
== '\t') {
2008 int current
= edit
->curs1
;
2009 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2010 edit_insert (edit
, '\n');
2011 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2017 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2019 void edit_push_key_press (WEdit
* edit
)
2021 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2022 if (edit
->mark2
== -1)
2023 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2026 /* this find the matching bracket in either direction, and sets edit->bracket */
2027 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2029 const char * const b
= "{}{[][()(", *p
;
2030 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2031 unsigned long j
= 0;
2033 edit_update_curs_row (edit
);
2034 c
= edit_get_byte (edit
, edit
->curs1
);
2037 if (!furthest_bracket_search
)
2038 furthest_bracket_search
--;
2039 /* not on a bracket at all */
2042 /* the matching bracket */
2044 /* going left or right? */
2045 if (strchr ("{[(", c
))
2047 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2048 /* out of buffer? */
2049 if (q
>= edit
->last_byte
|| q
< 0)
2051 a
= edit_get_byte (edit
, q
);
2052 /* don't want to eat CPU */
2053 if (j
++ > furthest_bracket_search
)
2055 /* out of screen? */
2057 if (q
< edit
->start_display
)
2059 /* count lines if searching downward */
2060 if (inc
> 0 && a
== '\n')
2061 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2064 /* count bracket depth */
2065 i
+= (a
== c
) - (a
== d
);
2066 /* return if bracket depth is zero */
2074 static long last_bracket
= -1;
2076 void edit_find_bracket (WEdit
* edit
)
2078 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2079 if (last_bracket
!= edit
->bracket
)
2080 edit
->force
|= REDRAW_PAGE
;
2081 last_bracket
= edit
->bracket
;
2084 static void edit_goto_matching_bracket (WEdit
*edit
)
2087 q
= edit_get_bracket (edit
, 0, 0);
2090 edit
->bracket
= edit
->curs1
;
2091 edit
->force
|= REDRAW_PAGE
;
2092 edit_cursor_move (edit
, q
- edit
->curs1
);
2096 * This executes a command as though the user initiated it through a key
2097 * press. Callback with WIDGET_KEY as a message calls this after
2098 * translating the key press. This function can be used to pass any
2099 * command to the editor. Note that the screen wouldn't update
2100 * automatically. Either of command or char_for_insertion must be
2101 * passed as -1. Commands are executed, and char_for_insertion is
2102 * inserted at the cursor.
2104 void edit_execute_key_command (WEdit
*edit
, int command
, int char_for_insertion
)
2106 if (command
== CK_Begin_Record_Macro
) {
2108 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2111 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2112 edit
->force
|= REDRAW_COMPLETELY
;
2113 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2117 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2118 edit
->macro
[edit
->macro_i
].command
= command
;
2119 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2121 /* record the beginning of a set of editing actions initiated by a key press */
2122 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2123 edit_push_key_press (edit
);
2125 edit_execute_cmd (edit
, command
, char_for_insertion
);
2126 if (column_highlighting
)
2127 edit
->force
|= REDRAW_PAGE
;
2130 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2133 This executes a command at a lower level than macro recording.
2134 It also does not push a key_press onto the undo stack. This means
2135 that if it is called many times, a single undo command will undo
2136 all of them. It also does not check for the Undo command.
2139 edit_execute_cmd (WEdit
*edit
, int command
, int char_for_insertion
)
2141 edit
->force
|= REDRAW_LINE
;
2143 /* The next key press will unhighlight the found string, so update
2145 if (edit
->found_len
|| column_highlighting
)
2146 edit
->force
|= REDRAW_PAGE
;
2148 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2149 column_highlighting
= 0;
2150 if (!edit
->highlight
2151 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2152 edit_mark_cmd (edit
, 1); /* clear */
2153 edit_mark_cmd (edit
, 0); /* marking on */
2155 edit
->highlight
= 1;
2156 } else { /* any other command */
2157 if (edit
->highlight
)
2158 edit_mark_cmd (edit
, 0); /* clear */
2159 edit
->highlight
= 0;
2162 /* first check for undo */
2163 if (command
== CK_Undo
) {
2164 edit_do_undo (edit
);
2165 edit
->found_len
= 0;
2166 edit
->prev_col
= edit_get_col (edit
);
2167 edit
->search_start
= edit
->curs1
;
2171 /* An ordinary key press */
2172 if (char_for_insertion
>= 0) {
2173 if (edit
->overwrite
) {
2174 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2177 edit_insert (edit
, char_for_insertion
);
2178 if (option_auto_para_formatting
) {
2179 format_paragraph (edit
, 0);
2180 edit
->force
|= REDRAW_PAGE
;
2182 check_and_wrap_line (edit
);
2183 edit
->found_len
= 0;
2184 edit
->prev_col
= edit_get_col (edit
);
2185 edit
->search_start
= edit
->curs1
;
2186 edit_find_bracket (edit
);
2192 case CK_Begin_Page_Highlight
:
2193 case CK_End_Page_Highlight
:
2198 case CK_Word_Left_Highlight
:
2199 case CK_Word_Right_Highlight
:
2200 case CK_Up_Highlight
:
2201 case CK_Down_Highlight
:
2202 if (edit
->mark2
== -1)
2203 break; /*marking is following the cursor: may need to highlight a whole line */
2206 case CK_Left_Highlight
:
2207 case CK_Right_Highlight
:
2208 edit
->force
|= REDRAW_CHAR_ONLY
;
2211 /* basic cursor key commands */
2214 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2215 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2217 edit_backspace (edit
);
2220 if (option_fake_half_tabs
) {
2222 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2223 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2224 edit_backspace (edit
);
2229 edit_backspace (edit
);
2232 if (option_fake_half_tabs
) {
2234 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2235 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2242 case CK_Delete_Word_Left
:
2243 edit_left_delete_word (edit
);
2245 case CK_Delete_Word_Right
:
2246 edit_right_delete_word (edit
);
2248 case CK_Delete_Line
:
2249 edit_delete_line (edit
);
2251 case CK_Delete_To_Line_End
:
2252 edit_delete_to_line_end (edit
);
2254 case CK_Delete_To_Line_Begin
:
2255 edit_delete_to_line_begin (edit
);
2258 if (option_auto_para_formatting
) {
2259 edit_double_newline (edit
);
2260 if (option_return_does_auto_indent
)
2261 edit_auto_indent (edit
);
2262 format_paragraph (edit
, 0);
2264 edit_insert (edit
, '\n');
2265 if (option_return_does_auto_indent
) {
2266 edit_auto_indent (edit
);
2271 edit_insert (edit
, '\n');
2275 case CK_Page_Up_Highlight
:
2276 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2279 case CK_Page_Down_Highlight
:
2280 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2283 case CK_Left_Highlight
:
2284 if (option_fake_half_tabs
) {
2285 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2286 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2287 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2291 edit_cursor_move (edit
, -1);
2294 case CK_Right_Highlight
:
2295 if (option_fake_half_tabs
) {
2296 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2297 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2298 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2302 edit_cursor_move (edit
, 1);
2305 case CK_Begin_Page_Highlight
:
2306 edit_begin_page (edit
);
2309 case CK_End_Page_Highlight
:
2310 edit_end_page (edit
);
2313 case CK_Word_Left_Highlight
:
2314 edit_left_word_move_cmd (edit
);
2317 case CK_Word_Right_Highlight
:
2318 edit_right_word_move_cmd (edit
);
2321 case CK_Up_Highlight
:
2322 edit_move_up (edit
, 1, 0);
2325 case CK_Down_Highlight
:
2326 edit_move_down (edit
, 1, 0);
2328 case CK_Paragraph_Up
:
2329 case CK_Paragraph_Up_Highlight
:
2330 edit_move_up_paragraph (edit
, 0);
2332 case CK_Paragraph_Down
:
2333 case CK_Paragraph_Down_Highlight
:
2334 edit_move_down_paragraph (edit
, 0);
2337 case CK_Scroll_Up_Highlight
:
2338 edit_move_up (edit
, 1, 1);
2340 case CK_Scroll_Down
:
2341 case CK_Scroll_Down_Highlight
:
2342 edit_move_down (edit
, 1, 1);
2345 case CK_Home_Highlight
:
2346 edit_cursor_to_bol (edit
);
2349 case CK_End_Highlight
:
2350 edit_cursor_to_eol (edit
);
2354 edit_tab_cmd (edit
);
2355 if (option_auto_para_formatting
) {
2356 format_paragraph (edit
, 0);
2357 edit
->force
|= REDRAW_PAGE
;
2359 check_and_wrap_line (edit
);
2362 case CK_Toggle_Insert
:
2363 edit
->overwrite
= (edit
->overwrite
== 0);
2367 if (edit
->mark2
>= 0) {
2368 if (column_highlighting
)
2369 edit_push_action (edit
, COLUMN_ON
);
2370 column_highlighting
= 0;
2372 edit_mark_cmd (edit
, 0);
2374 case CK_Column_Mark
:
2375 if (!column_highlighting
)
2376 edit_push_action (edit
, COLUMN_OFF
);
2377 column_highlighting
= 1;
2378 edit_mark_cmd (edit
, 0);
2381 if (column_highlighting
)
2382 edit_push_action (edit
, COLUMN_ON
);
2383 column_highlighting
= 0;
2384 edit_mark_cmd (edit
, 1);
2387 case CK_Toggle_Bookmark
:
2388 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2389 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2390 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2392 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2394 case CK_Flush_Bookmarks
:
2395 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2396 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2397 edit
->force
|= REDRAW_PAGE
;
2399 case CK_Next_Bookmark
:
2400 if (edit
->book_mark
) {
2401 struct _book_mark
*p
;
2402 p
= (struct _book_mark
*) book_mark_find (edit
,
2406 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2407 || p
->line
< edit
->start_line
)
2408 edit_move_display (edit
,
2410 edit
->num_widget_lines
/ 2);
2411 edit_move_to_line (edit
, p
->line
);
2415 case CK_Prev_Bookmark
:
2416 if (edit
->book_mark
) {
2417 struct _book_mark
*p
;
2418 p
= (struct _book_mark
*) book_mark_find (edit
,
2420 while (p
->line
== edit
->curs_line
)
2424 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2425 || p
->line
< edit
->start_line
)
2426 edit_move_display (edit
,
2428 edit
->num_widget_lines
/ 2);
2429 edit_move_to_line (edit
, p
->line
);
2434 case CK_Beginning_Of_Text
:
2435 case CK_Beginning_Of_Text_Highlight
:
2436 edit_move_to_top (edit
);
2438 case CK_End_Of_Text
:
2439 case CK_End_Of_Text_Highlight
:
2440 edit_move_to_bottom (edit
);
2444 edit_block_copy_cmd (edit
);
2447 edit_block_delete_cmd (edit
);
2450 edit_block_move_cmd (edit
);
2454 edit_copy_to_X_buf_cmd (edit
);
2457 edit_cut_to_X_buf_cmd (edit
);
2460 edit_paste_from_X_buf_cmd (edit
);
2462 case CK_Selection_History
:
2463 edit_paste_from_history (edit
);
2467 edit_save_as_cmd (edit
);
2470 edit_save_confirm_cmd (edit
);
2473 edit_load_cmd (edit
);
2476 edit_save_block_cmd (edit
);
2478 case CK_Insert_File
:
2479 edit_insert_file_cmd (edit
);
2482 case CK_Toggle_Syntax
:
2483 if ((option_syntax_highlighting
^= 1) == 1)
2484 edit_load_syntax (edit
, NULL
, option_syntax_type
);
2485 edit
->force
|= REDRAW_PAGE
;
2489 edit_search_cmd (edit
, 0);
2492 edit_search_cmd (edit
, 1);
2495 edit_replace_cmd (edit
, 0);
2497 case CK_Replace_Again
:
2498 edit_replace_cmd (edit
, 1);
2500 case CK_Complete_Word
:
2501 edit_complete_word_cmd (edit
);
2505 dlg_stop (edit
->widget
.parent
);
2508 edit_new_cmd (edit
);
2512 edit_help_cmd (edit
);
2516 edit_refresh_cmd (edit
);
2521 #ifdef HAVE_STRFTIME
2523 /* fool gcc to prevent a Y2K warning */
2524 char time_format
[] = "_c";
2525 time_format
[0] = '%';
2528 #ifdef HAVE_STRFTIME
2529 strftime (s
, sizeof (s
), time_format
, localtime (&t
));
2530 edit_print_string (edit
, s
);
2532 edit_print_string (edit
, ctime (&t
));
2534 edit
->force
|= REDRAW_PAGE
;
2538 edit_goto_cmd (edit
);
2540 case CK_Paragraph_Format
:
2541 format_paragraph (edit
, 1);
2542 edit
->force
|= REDRAW_PAGE
;
2544 case CK_Delete_Macro
:
2545 edit_delete_macro_cmd (edit
);
2547 case CK_Match_Bracket
:
2548 edit_goto_matching_bracket (edit
);
2554 edit_sort_cmd (edit
);
2557 edit_ext_cmd (edit
);
2560 edit_mail_dialog (edit
);
2565 case CK_Select_Codepage
:
2566 edit_select_codepage_cmd (edit
);
2568 case CK_Insert_Literal
:
2569 edit_insert_literal_cmd (edit
);
2571 case CK_Execute_Macro
:
2572 edit_execute_macro_cmd (edit
);
2574 case CK_Begin_End_Macro
:
2575 edit_begin_end_macro_cmd (edit
);
2585 if ((command
/ 1000) == 1) /* a shell command */
2586 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2587 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2588 struct macro m
[MAX_MACRO_LENGTH
];
2590 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
2591 edit_execute_macro (edit
, m
, nm
);
2594 /* keys which must set the col position, and the search vars */
2599 case CK_Replace_Again
:
2600 case CK_Complete_Word
:
2601 edit
->prev_col
= edit_get_col (edit
);
2604 case CK_Up_Highlight
:
2606 case CK_Down_Highlight
:
2608 case CK_Page_Up_Highlight
:
2610 case CK_Page_Down_Highlight
:
2611 case CK_Beginning_Of_Text
:
2612 case CK_Beginning_Of_Text_Highlight
:
2613 case CK_End_Of_Text
:
2614 case CK_End_Of_Text_Highlight
:
2615 case CK_Paragraph_Up
:
2616 case CK_Paragraph_Up_Highlight
:
2617 case CK_Paragraph_Down
:
2618 case CK_Paragraph_Down_Highlight
:
2620 case CK_Scroll_Up_Highlight
:
2621 case CK_Scroll_Down
:
2622 case CK_Scroll_Down_Highlight
:
2623 edit
->search_start
= edit
->curs1
;
2624 edit
->found_len
= 0;
2627 edit
->found_len
= 0;
2628 edit
->prev_col
= edit_get_col (edit
);
2629 edit
->search_start
= edit
->curs1
;
2631 edit_find_bracket (edit
);
2633 if (option_auto_para_formatting
) {
2637 case CK_Delete_Word_Left
:
2638 case CK_Delete_Word_Right
:
2639 case CK_Delete_To_Line_End
:
2640 case CK_Delete_To_Line_Begin
:
2641 format_paragraph (edit
, 0);
2642 edit
->force
|= REDRAW_PAGE
;
2649 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2653 if (edit
->macro_depth
++ > 256) {
2654 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2655 edit
->macro_depth
--;
2658 edit
->force
|= REDRAW_PAGE
;
2659 for (; i
< n
; i
++) {
2660 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2662 edit_update_screen (edit
);
2663 edit
->macro_depth
--;
2666 /* User edit menu, like user menu (F2) but only in editor. */
2668 user_menu (WEdit
* edit
)
2673 long start_mark
, end_mark
;
2674 char *block_file
= concat_dir_and_file (home_dir
, BLOCK_FILE
);
2677 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2678 if (!nomark
) /* remember marked or not */
2679 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2681 /* run shell scripts from menu */
2682 user_menu_cmd (edit
);
2684 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2685 /* no block messages */
2690 /* i.e. we have marked block */
2691 rc
= edit_block_delete_cmd (edit
);
2695 edit_insert_file (edit
, block_file
);
2698 /* truncate block file */
2699 if ((fd
= fopen (block_file
, "w"))) {
2703 edit_refresh_cmd (edit
);
2704 edit
->force
|= REDRAW_COMPLETELY
;
2707 g_free (block_file
);