1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include "edit-widget.h"
27 #include "editcmddef.h"
29 #include "../src/cmd.h" /* view_other_cmd() */
30 #include "../src/user.h" /* user_menu_cmd() */
31 #include "../src/wtools.h" /* query_dialog() */
34 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
35 or EDIT_KEY_EMULATION_EMACS
37 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
39 int option_word_wrap_line_length
= 72;
40 int option_typewriter_wrap
= 0;
41 int option_auto_para_formatting
= 0;
42 int option_tab_spacing
= 8;
43 int option_fill_tabs_with_spaces
= 0;
44 int option_return_does_auto_indent
= 1;
45 int option_backspace_through_tabs
= 0;
46 int option_fake_half_tabs
= 1;
47 int option_save_mode
= EDIT_QUICK_SAVE
;
48 int option_save_position
= 1;
49 int option_backup_ext_int
= -1;
50 int option_max_undo
= 32768;
52 int option_edit_right_extreme
= 0;
53 int option_edit_left_extreme
= 0;
54 int option_edit_top_extreme
= 0;
55 int option_edit_bottom_extreme
= 0;
57 char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
58 char *option_backup_ext
= "~";
62 * here's a quick sketch of the layout: (don't run this through indent.)
64 * (b1 is buffers1 and b2 is buffers2)
67 * \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
68 * ______________________________________|______________________________________
70 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
71 * |-> |-> |-> |-> |-> |-> |
73 * _<------------------------->|<----------------->_
74 * WEdit->curs2 | WEdit->curs1
79 * file end|||file beginning
91 static void edit_move_to_prev_col (WEdit
*edit
, long p
);
92 static void user_menu (WEdit
*edit
);
94 #ifndef NO_INLINE_GETBYTE
96 int edit_get_byte (WEdit
* edit
, long byte_index
)
99 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
102 if (byte_index
>= edit
->curs1
) {
103 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
104 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
106 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
114 * Initialize the buffers for an empty files.
117 edit_init_buffers (WEdit
*edit
)
121 for (j
= 0; j
<= MAXBUFF
; j
++) {
122 edit
->buffers1
[j
] = NULL
;
123 edit
->buffers2
[j
] = NULL
;
128 edit
->buffers2
[0] = g_malloc (EDIT_BUF_SIZE
);
132 * Load file OR text into buffers. Set cursor to the beginning of file.
136 edit_load_file_fast (WEdit
*edit
, const char *filename
)
141 edit
->curs2
= edit
->last_byte
;
142 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
144 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
145 /* The file-name is printed after the ':' */
146 edit_error_dialog (_("Error"),
147 get_sys_error (catstrs
149 (" Cannot open file for reading: "),
154 if (!edit
->buffers2
[buf2
])
155 edit
->buffers2
[buf2
] = g_malloc (EDIT_BUF_SIZE
);
158 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
159 (edit
->curs2
& M_EDIT_BUF_SIZE
),
160 edit
->curs2
& M_EDIT_BUF_SIZE
);
162 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
163 /* edit->buffers2[0] is already allocated */
164 if (!edit
->buffers2
[buf
])
165 edit
->buffers2
[buf
] = g_malloc (EDIT_BUF_SIZE
);
166 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
173 /* detecting an error on save is easy: just check if every byte has been written. */
174 /* detecting an error on read, is not so easy 'cos there is not way to tell
175 whether you read everything or not. */
176 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
177 static const struct edit_filters
{
178 char *read
, *write
, *extension
;
182 "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2"
185 "gzip -cd %s 2>&1", "gzip > %s", ".gz"
188 "gzip -cd %s 2>&1", "gzip > %s", ".Z"
192 /* Return index of the filter or -1 is there is no appropriate filter */
193 static int edit_find_filter (const char *filename
)
198 l
= strlen (filename
);
199 for (i
= 0; i
< sizeof (all_filters
) / sizeof (struct edit_filters
); i
++) {
201 e
= strlen (all_filters
[i
].extension
);
203 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
210 edit_get_filter (const char *filename
)
213 char *p
, *quoted_name
;
214 i
= edit_find_filter (filename
);
217 quoted_name
= name_quote (filename
, 0);
218 l
= strlen (quoted_name
);
219 p
= g_malloc (strlen (all_filters
[i
].read
) + l
+ 2);
220 sprintf (p
, all_filters
[i
].read
, quoted_name
);
221 g_free (quoted_name
);
226 edit_get_write_filter (const char *write_name
, const char *filename
)
230 i
= edit_find_filter (filename
);
233 writename
= name_quote (write_name
, 0);
234 l
= strlen (writename
);
235 p
= g_malloc (strlen (all_filters
[i
].write
) + l
+ 2);
236 sprintf (p
, all_filters
[i
].write
, writename
);
242 edit_insert_stream (WEdit
* edit
, FILE * f
)
246 while ((c
= fgetc (f
)) >= 0) {
247 edit_insert (edit
, c
);
253 long edit_write_stream (WEdit
* edit
, FILE * f
)
256 for (i
= 0; i
< edit
->last_byte
; i
++)
257 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
262 #define TEMP_BUF_LEN 1024
264 /* inserts a file at the cursor, returns 1 on success */
266 edit_insert_file (WEdit
*edit
, const char *filename
)
269 if ((p
= edit_get_filter (filename
))) {
271 long current
= edit
->curs1
;
272 f
= (FILE *) popen (p
, "r");
274 edit_insert_stream (edit
, f
);
275 edit_cursor_move (edit
, current
- edit
->curs1
);
276 if (pclose (f
) > 0) {
277 edit_error_dialog (_("Error"),
279 (" Error reading from pipe: "),
285 edit_error_dialog (_("Error"),
286 get_sys_error (catstrs
288 (" Cannot open pipe for reading: "),
295 int i
, file
, blocklen
;
296 long current
= edit
->curs1
;
298 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
300 buf
= g_malloc (TEMP_BUF_LEN
);
301 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
302 for (i
= 0; i
< blocklen
; i
++)
303 edit_insert (edit
, buf
[i
]);
305 edit_cursor_move (edit
, current
- edit
->curs1
);
314 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
316 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 edit_error_dialog (_("Error"),
334 get_sys_error (catstrs
336 (" Cannot open file for reading: "),
340 /* New file, delete it if it's not modified or saved */
341 edit
->delete_file
= 1;
345 /* Check what we have opened */
346 if (mc_fstat (file
, st
) < 0) {
348 edit_error_dialog (_("Error"),
349 get_sys_error (catstrs
351 (" Cannot get size/permissions info for file: "),
356 /* We want to open regular files only */
357 if (!S_ISREG (st
->st_mode
)) {
359 edit_error_dialog (_("Error"),
360 catstrs (_(" Not an ordinary file: "), filename
,
366 * Don't delete non-empty files.
367 * O_EXCL should prevent it, but let's be on the safe side.
369 if (st
->st_size
> 0) {
370 edit
->delete_file
= 0;
373 if (st
->st_size
>= SIZE_LIMIT
) {
375 edit_error_dialog (_("Error"),
376 catstrs (_(" File is too large: "), filename
,
386 * Open the file and load it into the buffers, either directly or using
387 * a filter. Return 0 on success, 1 on error.
389 * Fast loading (edit_load_file_fast) is used when the file size is
390 * known. In this case the data is read into the buffers by blocks.
391 * If the file size is not known, the data is loaded byte by byte in
395 edit_load_file (WEdit
*edit
)
399 /* Cannot do fast load if a filter is used */
400 if (edit_find_filter (edit
->filename
) >= 0)
404 * VFS may report file size incorrectly, and slow load is not a big
405 * deal considering overhead in VFS.
407 if (!vfs_file_is_local (edit
->filename
))
411 * FIXME: line end translation should disable fast loading as well
412 * Consider doing fseek() to the end and ftell() for the real size.
415 if (*edit
->filename
) {
416 /* If we are dealing with a real file, check that it exists */
417 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
420 /* nothing to load */
424 edit_init_buffers (edit
);
427 edit
->last_byte
= edit
->stat1
.st_size
;
428 edit_load_file_fast (edit
, edit
->filename
);
429 /* If fast load was used, the number of lines wasn't calculated */
430 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
433 if (*edit
->filename
) {
434 edit
->stack_disable
= 1;
435 if (!edit_insert_file (edit
, edit
->filename
)) {
439 edit
->stack_disable
= 0;
445 /* Restore saved cursor position in the file */
447 edit_load_position (WEdit
*edit
)
452 if (!edit
->filename
|| !*edit
->filename
)
455 filename
= vfs_canon (edit
->filename
);
456 load_file_position (filename
, &line
, &column
);
459 edit_move_to_line (edit
, line
- 1);
460 edit
->prev_col
= column
;
461 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
462 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
465 /* Save cursor position in the file */
467 edit_save_position (WEdit
*edit
)
471 if (!edit
->filename
|| !*edit
->filename
)
474 filename
= vfs_canon (edit
->filename
);
475 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
479 /* Clean the WEdit stricture except the widget part */
481 edit_purge_widget (WEdit
*edit
)
483 int len
= sizeof (WEdit
) - sizeof (Widget
);
484 char *start
= (char *) edit
+ sizeof (Widget
);
485 memset (start
, 0, len
);
486 edit
->macro_i
= -1; /* not recording a macro */
489 #define space_width 1
492 * Fill in the edit structure. Return NULL on failure. Pass edit as
493 * NULL to allocate a new structure.
495 * If line is 0, try to restore saved position. Otherwise put the
496 * cursor on that line and show it in the middle of the screen.
499 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
507 * Expand option_whole_chars_search by national letters using
511 static char option_whole_chars_search_buf
[256];
513 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
515 int len
= strlen (option_whole_chars_search
);
517 strcpy (option_whole_chars_search_buf
,
518 option_whole_chars_search
);
520 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
521 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
522 option_whole_chars_search_buf
[len
++] = i
;
526 option_whole_chars_search_buf
[len
] = 0;
527 option_whole_chars_search
= option_whole_chars_search_buf
;
529 #endif /* ENABLE_NLS */
530 edit
= g_malloc0 (sizeof (WEdit
));
533 edit_purge_widget (edit
);
534 edit
->num_widget_lines
= lines
;
535 edit
->num_widget_columns
= columns
;
536 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
537 edit
->stat1
.st_uid
= getuid ();
538 edit
->stat1
.st_gid
= getgid ();
540 edit
->force
|= REDRAW_PAGE
;
541 edit_set_filename (edit
, filename
);
542 edit
->stack_size
= START_STACK_SIZE
;
543 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
544 edit
->undo_stack
= g_malloc ((edit
->stack_size
+ 10) * sizeof (long));
545 if (edit_load_file (edit
)) {
546 /* edit_load_file already gives an error message */
551 edit
->loading_done
= 1;
554 edit_load_syntax (edit
, 0, 0);
557 edit_get_syntax_color (edit
, -1, &color
);
560 /* load saved cursor position */
561 if ((line
== 0) && option_save_position
) {
562 edit_load_position (edit
);
566 edit_move_display (edit
, line
- 1);
567 edit_move_to_line (edit
, line
- 1);
573 /* Clear the edit struct, freeing everything in it. Return 1 on success */
575 edit_clean (WEdit
*edit
)
582 /* a stale lock, remove it */
584 edit
->locked
= edit_unlock_file (edit
->filename
);
586 /* save cursor position */
587 if (option_save_position
)
588 edit_save_position (edit
);
590 /* File specified on the mcedit command line and never saved */
591 if (edit
->delete_file
)
592 unlink (edit
->filename
);
594 edit_free_syntax_rules (edit
);
595 book_mark_flush (edit
, -1);
596 for (; j
<= MAXBUFF
; j
++) {
597 if (edit
->buffers1
[j
] != NULL
)
598 g_free (edit
->buffers1
[j
]);
599 if (edit
->buffers2
[j
] != NULL
)
600 g_free (edit
->buffers2
[j
]);
603 g_free (edit
->undo_stack
);
604 g_free (edit
->filename
);
607 edit_purge_widget (edit
);
609 /* Free temporary strings used in catstrs() */
616 /* returns 1 on success */
617 int edit_renew (WEdit
* edit
)
619 int lines
= edit
->num_widget_lines
;
620 int columns
= edit
->num_widget_columns
;
624 if (!edit_init (edit
, lines
, columns
, "", 0))
630 * Load a new file into the editor. If it fails, preserve the old file.
631 * To do it, allocate a new widget, initialize it and, if the new file
632 * was loaded, copy the data to the old widget.
633 * Return 1 on success, 0 on failure.
636 edit_reload (WEdit
*edit
, const char *filename
)
639 int lines
= edit
->num_widget_lines
;
640 int columns
= edit
->num_widget_columns
;
642 e
= g_malloc0 (sizeof (WEdit
));
643 e
->widget
= edit
->widget
;
644 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
649 memcpy (edit
, e
, sizeof (WEdit
));
656 Recording stack for undo:
657 The following is an implementation of a compressed stack. Identical
658 pushes are recorded by a negative prefix indicating the number of times the
659 same char was pushed. This saves space for repeated curs-left or curs-right
676 If the stack long int is 0-255 it represents a normal insert (from a backspace),
677 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
678 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
679 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
682 The only way the cursor moves or the buffer is changed is through the routines:
683 insert, backspace, insert_ahead, delete, and cursor_move.
684 These record the reverse undo movements onto the stack each time they are
687 Each key press results in a set of actions (insert; delete ...). So each time
688 a key is pressed the current position of start_display is pushed as
689 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
690 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
691 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
695 void edit_push_action (WEdit
* edit
, long c
,...)
697 unsigned long sp
= edit
->stack_pointer
;
700 /* first enlarge the stack if necessary */
701 if (sp
> edit
->stack_size
- 10) { /* say */
702 if (option_max_undo
< 256)
703 option_max_undo
= 256;
704 if (edit
->stack_size
< option_max_undo
) {
705 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
707 edit
->undo_stack
= t
;
708 edit
->stack_size
<<= 1;
709 edit
->stack_size_mask
= edit
->stack_size
- 1;
713 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
714 if (edit
->stack_disable
)
717 #ifdef FAST_MOVE_CURSOR
718 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
720 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
721 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
723 c
= -(va_arg (ap
, int));
726 #endif /* ! FAST_MOVE_CURSOR */
727 if (edit
->stack_bottom
!= sp
728 && spm1
!= edit
->stack_bottom
729 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
731 if (edit
->undo_stack
[spm1
] < 0) {
732 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
734 if (edit
->undo_stack
[spm1
] > -1000000000) {
735 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
736 edit
->undo_stack
[spm1
]--;
740 /* #define NO_STACK_CURSMOVE_ANIHILATION */
741 #ifndef NO_STACK_CURSMOVE_ANIHILATION
742 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
743 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
744 if (edit
->undo_stack
[spm1
] == -2)
745 edit
->stack_pointer
= spm1
;
747 edit
->undo_stack
[spm1
]++;
752 d
= edit
->undo_stack
[spm1
];
755 return; /* --> no need to push multiple do-nothings */
756 edit
->undo_stack
[sp
] = -2;
759 #ifndef NO_STACK_CURSMOVE_ANIHILATION
760 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
761 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
762 edit
->stack_pointer
= spm1
;
768 edit
->undo_stack
[sp
] = c
;
771 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
773 /*if the sp wraps round and catches the stack_bottom then erase the first set of actions on the stack to make space - by moving stack_bottom forward one "key press" */
774 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
775 if (c
== edit
->stack_bottom
|| ((c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
777 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
778 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
780 /*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: */
781 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
782 edit
->stack_bottom
= edit
->stack_pointer
= 0;
786 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
787 then the file should be as it was when he loaded up. Then set edit->modified to 0.
790 pop_action (WEdit
* edit
)
793 unsigned long sp
= edit
->stack_pointer
;
794 if (sp
== edit
->stack_bottom
) {
797 sp
= (sp
- 1) & edit
->stack_size_mask
;
798 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
799 /* edit->undo_stack[sp] = '@'; */
800 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
803 if (sp
== edit
->stack_bottom
) {
806 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
807 if (edit
->undo_stack
[sp
] == -2) {
808 /* edit->undo_stack[sp] = '@'; */
809 edit
->stack_pointer
= sp
;
811 edit
->undo_stack
[sp
]++;
816 /* is called whenever a modification is made by one of the four routines below */
817 static inline void edit_modification (WEdit
* edit
)
819 edit
->caches_valid
= 0;
820 edit
->screen_modified
= 1;
822 /* raise lock when file modified */
823 if (!edit
->modified
&& !edit
->delete_file
)
824 edit
->locked
= edit_lock_file (edit
->filename
);
829 Basic low level single character buffer alterations and movements at the cursor.
830 Returns char passed over, inserted or removed.
834 edit_insert (WEdit
*edit
, int c
)
836 /* check if file has grown to large */
837 if (edit
->last_byte
>= SIZE_LIMIT
)
840 /* first we must update the position of the display window */
841 if (edit
->curs1
< edit
->start_display
) {
842 edit
->start_display
++;
847 /* Mark file as modified, unless the file hasn't been fully loaded */
848 if (edit
->loading_done
) {
849 edit_modification (edit
);
852 /* now we must update some info on the file and check if a redraw is required */
855 book_mark_inc (edit
, edit
->curs_line
);
858 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
861 /* save the reverse command onto the undo stack */
862 edit_push_action (edit
, BACKSPACE
);
865 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
866 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
867 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
869 /* add a new buffer if we've reached the end of the last one */
870 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
871 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
872 g_malloc (EDIT_BUF_SIZE
);
874 /* perform the insertion */
875 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
876 curs1
& M_EDIT_BUF_SIZE
]
879 /* update file length */
882 /* update cursor position */
887 /* same as edit_insert and move left */
888 void edit_insert_ahead (WEdit
* edit
, int c
)
890 if (edit
->last_byte
>= SIZE_LIMIT
)
892 if (edit
->curs1
< edit
->start_display
) {
893 edit
->start_display
++;
897 edit_modification (edit
);
900 book_mark_inc (edit
, edit
->curs_line
);
902 edit
->force
|= REDRAW_AFTER_CURSOR
;
904 edit_push_action (edit
, DELCHAR
);
906 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
907 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
908 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
910 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
911 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
912 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
919 int edit_delete (WEdit
* edit
)
925 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
926 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
927 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
929 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
931 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
932 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
933 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
938 edit_modification (edit
);
941 book_mark_dec (edit
, edit
->curs_line
);
943 edit
->force
|= REDRAW_AFTER_CURSOR
;
945 edit_push_action (edit
, p
+ 256);
946 if (edit
->curs1
< edit
->start_display
) {
947 edit
->start_display
--;
957 edit_backspace (WEdit
* edit
)
963 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
964 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
965 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
967 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
968 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
969 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
970 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
975 edit_modification (edit
);
978 book_mark_dec (edit
, edit
->curs_line
);
981 edit
->force
|= REDRAW_AFTER_CURSOR
;
983 edit_push_action (edit
, p
);
985 if (edit
->curs1
< edit
->start_display
) {
986 edit
->start_display
--;
994 #ifdef FAST_MOVE_CURSOR
996 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
999 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1001 next
-= (unsigned long) dest
;
1009 edit_move_backward_lots (WEdit
*edit
, long increment
)
1014 if (increment
> edit
->curs1
)
1015 increment
= edit
->curs1
;
1018 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1020 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1023 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1028 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1029 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1034 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1035 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1036 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1037 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1040 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1041 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1042 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1047 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1049 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1051 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1052 g_malloc (EDIT_BUF_SIZE
);
1058 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1068 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1070 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1074 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1075 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1078 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1080 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1081 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1086 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1088 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1090 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1091 g_malloc (EDIT_BUF_SIZE
);
1096 return edit_get_byte (edit
, edit
->curs1
);
1099 #endif /* ! FAST_MOVE_CURSOR */
1101 /* moves the cursor right or left: increment positive or negative respectively */
1102 int edit_cursor_move (WEdit
* edit
, long increment
)
1104 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1107 #ifdef FAST_MOVE_CURSOR
1108 if (increment
< -256) {
1109 edit
->force
|= REDRAW_PAGE
;
1110 return edit_move_backward_lots (edit
, -increment
);
1112 #endif /* ! FAST_MOVE_CURSOR */
1114 if (increment
< 0) {
1115 for (; increment
< 0; increment
++) {
1119 edit_push_action (edit
, CURS_RIGHT
);
1121 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1122 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1123 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1124 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1126 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1127 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1128 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1129 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1134 edit
->force
|= REDRAW_LINE_BELOW
;
1139 } else if (increment
> 0) {
1140 for (; increment
> 0; increment
--) {
1144 edit_push_action (edit
, CURS_LEFT
);
1146 c
= edit_get_byte (edit
, edit
->curs1
);
1147 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1148 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc (EDIT_BUF_SIZE
);
1149 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1151 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1152 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1153 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1154 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1159 edit
->force
|= REDRAW_LINE_ABOVE
;
1167 /* These functions return positions relative to lines */
1169 /* returns index of last char on line + 1 */
1170 long edit_eol (WEdit
* edit
, long current
)
1172 if (current
< edit
->last_byte
) {
1174 if (edit_get_byte (edit
, current
) == '\n')
1177 return edit
->last_byte
;
1181 /* returns index of first char on line */
1182 long edit_bol (WEdit
* edit
, long current
)
1186 if (edit_get_byte (edit
, current
- 1) == '\n')
1194 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1197 if (upto
> edit
->last_byte
)
1198 upto
= edit
->last_byte
;
1201 while (current
< upto
)
1202 if (edit_get_byte (edit
, current
++) == '\n')
1208 /* If lines is zero this returns the count of lines from current to upto. */
1209 /* If upto is zero returns index of lines forward current. */
1210 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1213 return edit_count_lines (edit
, current
, upto
);
1219 next
= edit_eol (edit
, current
) + 1;
1220 if (next
> edit
->last_byte
)
1230 /* Returns offset of 'lines' lines up from current */
1231 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1235 current
= edit_bol (edit
, current
);
1236 while((lines
--) && current
!= 0)
1237 current
= edit_bol (edit
, current
- 1);
1241 /* If cols is zero this returns the count of columns from current to upto. */
1242 /* If upto is zero returns index of cols across from current. */
1243 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1252 q
= edit
->last_byte
+ 2;
1254 for (col
= 0, p
= current
; p
< q
; p
++) {
1262 c
= edit_get_byte (edit
, p
);
1264 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1265 else if (c
== '\n') {
1270 } else if (c
< 32 || c
== 127)
1271 col
+= 2; /* Caret notation for control characters */
1278 /* returns the current column position of the cursor */
1279 int edit_get_col (WEdit
* edit
)
1281 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1285 /* Scrolling functions */
1287 void edit_update_curs_row (WEdit
* edit
)
1289 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1292 void edit_update_curs_col (WEdit
* edit
)
1294 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1297 /*moves the display start position up by i lines */
1298 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1300 int lines_above
= edit
->start_line
;
1301 if (i
> lines_above
)
1304 edit
->start_line
-= i
;
1305 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1306 edit
->force
|= REDRAW_PAGE
;
1307 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1309 edit_update_curs_row (edit
);
1313 /* returns 1 if could scroll, 0 otherwise */
1314 void edit_scroll_downward (WEdit
* edit
, int i
)
1317 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1318 if (lines_below
> 0) {
1319 if (i
> lines_below
)
1321 edit
->start_line
+= i
;
1322 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1323 edit
->force
|= REDRAW_PAGE
;
1324 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1326 edit_update_curs_row (edit
);
1329 void edit_scroll_right (WEdit
* edit
, int i
)
1331 edit
->force
|= REDRAW_PAGE
;
1332 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1333 edit
->start_col
-= i
;
1336 void edit_scroll_left (WEdit
* edit
, int i
)
1338 if (edit
->start_col
) {
1339 edit
->start_col
+= i
;
1340 if (edit
->start_col
> 0)
1341 edit
->start_col
= 0;
1342 edit
->force
|= REDRAW_PAGE
;
1343 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1347 /* high level cursor movement commands */
1349 static int is_in_indent (WEdit
*edit
)
1351 long p
= edit_bol (edit
, edit
->curs1
);
1352 while (p
< edit
->curs1
)
1353 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1358 static int left_of_four_spaces (WEdit
*edit
);
1361 edit_move_to_prev_col (WEdit
* edit
, long p
)
1363 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1365 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1366 edit_update_curs_col (edit
);
1368 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1369 int q
= edit
->curs_col
;
1370 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1371 p
= edit_bol (edit
, edit
->curs1
);
1372 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1373 if (!left_of_four_spaces (edit
))
1374 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1381 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1383 long p
, l
= edit
->curs_line
;
1389 edit
->force
|= REDRAW_PAGE
;
1391 edit_scroll_upward (edit
, i
);
1393 p
= edit_bol (edit
, edit
->curs1
);
1394 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1395 edit_move_to_prev_col (edit
, p
);
1397 edit
->search_start
= edit
->curs1
;
1398 edit
->found_len
= 0;
1403 is_blank (WEdit
*edit
, long offset
)
1407 s
= edit_bol (edit
, offset
);
1408 f
= edit_eol (edit
, offset
) - 1;
1410 c
= edit_get_byte (edit
, s
++);
1418 /* returns the offset of line i */
1420 edit_find_line (WEdit
*edit
, int line
)
1424 if (!edit
->caches_valid
) {
1425 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1426 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1427 /* three offsets that we *know* are line 0 at 0 and these two: */
1428 edit
->line_numbers
[1] = edit
->curs_line
;
1429 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1430 edit
->line_numbers
[2] = edit
->total_lines
;
1431 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1432 edit
->caches_valid
= 1;
1434 if (line
>= edit
->total_lines
)
1435 return edit
->line_offsets
[2];
1438 /* find the closest known point */
1439 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1441 n
= abs (edit
->line_numbers
[i
] - line
);
1448 return edit
->line_offsets
[j
]; /* know the offset exactly */
1449 if (m
== 1 && j
>= 3)
1450 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1452 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1453 if (line
> edit
->line_numbers
[j
])
1454 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1456 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1457 edit
->line_numbers
[i
] = line
;
1458 return edit
->line_offsets
[i
];
1461 int line_is_blank (WEdit
* edit
, long line
)
1463 return is_blank (edit
, edit_find_line (edit
, line
));
1466 /* moves up until a blank line is reached, or until just
1467 before a non-blank line is reached */
1468 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1471 if (edit
->curs_line
<= 1) {
1474 if (line_is_blank (edit
, edit
->curs_line
)) {
1475 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1476 for (i
= edit
->curs_line
- 1; i
; i
--)
1477 if (!line_is_blank (edit
, i
)) {
1482 for (i
= edit
->curs_line
- 1; i
; i
--)
1483 if (line_is_blank (edit
, i
))
1487 for (i
= edit
->curs_line
- 1; i
; i
--)
1488 if (line_is_blank (edit
, i
))
1492 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1496 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1498 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1504 edit
->force
|= REDRAW_PAGE
;
1506 edit_scroll_downward (edit
, i
);
1507 p
= edit_bol (edit
, edit
->curs1
);
1508 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1509 edit_move_to_prev_col (edit
, p
);
1511 edit
->search_start
= edit
->curs1
;
1512 edit
->found_len
= 0;
1516 /* moves down until a blank line is reached, or until just
1517 before a non-blank line is reached */
1518 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1521 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1522 i
= edit
->total_lines
;
1524 if (line_is_blank (edit
, edit
->curs_line
)) {
1525 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1526 for (i
= edit
->curs_line
+ 1; i
; i
++)
1527 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 for (i
= edit
->curs_line
+ 1; i
; i
++)
1538 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1542 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1545 static void edit_begin_page (WEdit
*edit
)
1547 edit_update_curs_row (edit
);
1548 edit_move_up (edit
, edit
->curs_row
, 0);
1551 static void edit_end_page (WEdit
*edit
)
1553 edit_update_curs_row (edit
);
1554 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1558 /* goto beginning of text */
1559 static void edit_move_to_top (WEdit
* edit
)
1561 if (edit
->curs_line
) {
1562 edit_cursor_move (edit
, -edit
->curs1
);
1563 edit_move_to_prev_col (edit
, 0);
1564 edit
->force
|= REDRAW_PAGE
;
1565 edit
->search_start
= 0;
1566 edit_update_curs_row(edit
);
1571 /* goto end of text */
1572 static void edit_move_to_bottom (WEdit
* edit
)
1574 if (edit
->curs_line
< edit
->total_lines
) {
1575 edit_cursor_move (edit
, edit
->curs2
);
1576 edit
->start_display
= edit
->last_byte
;
1577 edit
->start_line
= edit
->total_lines
;
1578 edit_update_curs_row(edit
);
1579 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1580 edit
->force
|= REDRAW_PAGE
;
1584 /* goto beginning of line */
1585 static void edit_cursor_to_bol (WEdit
* edit
)
1587 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1588 edit
->search_start
= edit
->curs1
;
1589 edit
->prev_col
= edit_get_col (edit
);
1592 /* goto end of line */
1593 static void edit_cursor_to_eol (WEdit
* edit
)
1595 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1596 edit
->search_start
= edit
->curs1
;
1597 edit
->prev_col
= edit_get_col (edit
);
1600 /* move cursor to line 'line' */
1601 void edit_move_to_line (WEdit
* e
, long line
)
1603 if(line
< e
->curs_line
)
1604 edit_move_up (e
, e
->curs_line
- line
, 0);
1606 edit_move_down (e
, line
- e
->curs_line
, 0);
1607 edit_scroll_screen_over_cursor (e
);
1610 /* scroll window so that first visible line is 'line' */
1611 void edit_move_display (WEdit
* e
, long line
)
1613 if(line
< e
->start_line
)
1614 edit_scroll_upward (e
, e
->start_line
- line
);
1616 edit_scroll_downward (e
, line
- e
->start_line
);
1619 /* save markers onto undo stack */
1620 void edit_push_markers (WEdit
* edit
)
1622 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1623 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1626 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1635 /* highlight marker toggle */
1636 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1638 edit_push_markers (edit
);
1640 edit_set_markers (edit
, 0, 0, 0, 0);
1641 edit
->force
|= REDRAW_PAGE
;
1643 if (edit
->mark2
>= 0) {
1644 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1645 edit
->force
|= REDRAW_PAGE
;
1647 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1651 static unsigned long
1656 const char option_chars_move_whole_word
[] =
1657 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1662 if (*option_chars_move_whole_word
== '!')
1664 return 0x80000000UL
;
1668 else if (islower (c
))
1670 else if (isalpha (c
))
1672 else if (isdigit (c
))
1674 else if (isspace (c
))
1676 q
= strchr (option_chars_move_whole_word
, c
);
1678 return 0xFFFFFFFFUL
;
1680 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1684 } while ((q
= strchr (q
+ 1, c
)));
1689 edit_left_word_move (WEdit
*edit
, int s
)
1693 edit_cursor_move (edit
, -1);
1696 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1697 c2
= edit_get_byte (edit
, edit
->curs1
);
1698 if (!(my_type_of (c1
) & my_type_of (c2
)))
1700 if (isspace (c1
) && !isspace (c2
))
1703 if (!isspace (c1
) && isspace (c2
))
1708 static void edit_left_word_move_cmd (WEdit
* edit
)
1710 edit_left_word_move (edit
, 0);
1711 edit
->force
|= REDRAW_PAGE
;
1715 edit_right_word_move (WEdit
*edit
, int s
)
1719 edit_cursor_move (edit
, 1);
1720 if (edit
->curs1
>= edit
->last_byte
)
1722 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1723 c2
= edit_get_byte (edit
, edit
->curs1
);
1724 if (!(my_type_of (c1
) & my_type_of (c2
)))
1726 if (isspace (c1
) && !isspace (c2
))
1729 if (!isspace (c1
) && isspace (c2
))
1734 static void edit_right_word_move_cmd (WEdit
* edit
)
1736 edit_right_word_move (edit
, 0);
1737 edit
->force
|= REDRAW_PAGE
;
1741 static void edit_right_delete_word (WEdit
* edit
)
1745 if (edit
->curs1
>= edit
->last_byte
)
1747 c1
= edit_delete (edit
);
1748 c2
= edit_get_byte (edit
, edit
->curs1
);
1749 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1751 if (!(my_type_of (c1
) & my_type_of (c2
)))
1756 static void edit_left_delete_word (WEdit
* edit
)
1760 if (edit
->curs1
<= 0)
1762 c1
= edit_backspace (edit
);
1763 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1764 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1766 if (!(my_type_of (c1
) & my_type_of (c2
)))
1772 the start column position is not recorded, and hence does not
1773 undo as it happed. But who would notice.
1776 edit_do_undo (WEdit
* edit
)
1781 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1783 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1788 edit_cursor_move (edit
, 1);
1791 edit_cursor_move (edit
, -1);
1794 edit_backspace (edit
);
1800 column_highlighting
= 1;
1803 column_highlighting
= 0;
1806 if (ac
>= 256 && ac
< 512)
1807 edit_insert_ahead (edit
, ac
- 256);
1808 if (ac
>= 0 && ac
< 256)
1809 edit_insert (edit
, ac
);
1811 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1812 edit
->mark1
= ac
- MARK_1
;
1813 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1814 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1815 edit
->mark2
= ac
- MARK_2
;
1816 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1819 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1822 if (edit
->start_display
> ac
- KEY_PRESS
) {
1823 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1824 edit
->force
|= REDRAW_PAGE
;
1825 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1826 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1827 edit
->force
|= REDRAW_PAGE
;
1829 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1830 edit_update_curs_row (edit
);
1833 edit
->stack_disable
= 0;
1836 static void edit_delete_to_line_end (WEdit
* edit
)
1838 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1845 static void edit_delete_to_line_begin (WEdit
* edit
)
1847 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1850 edit_backspace (edit
);
1855 edit_delete_line (WEdit
*edit
)
1858 * Delete right part of the line.
1859 * Note that edit_get_byte() returns '\n' when byte position is
1862 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1863 (void) edit_delete (edit
);
1868 * Note that edit_delete() will not corrupt anything if called while
1869 * cursor position is EOF.
1871 (void) edit_delete (edit
);
1874 * Delete left part of the line.
1875 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
1877 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1878 (void) edit_backspace (edit
);
1882 static void insert_spaces_tab (WEdit
* edit
, int half
)
1885 edit_update_curs_col (edit
);
1886 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1888 edit_insert (edit
, ' ');
1893 static int is_aligned_on_a_tab (WEdit
* edit
)
1895 edit_update_curs_col (edit
);
1896 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1897 return 0; /* not alligned on a tab */
1901 static int right_of_four_spaces (WEdit
*edit
)
1904 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1905 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1907 return is_aligned_on_a_tab (edit
);
1911 static int left_of_four_spaces (WEdit
*edit
)
1914 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1915 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1917 return is_aligned_on_a_tab (edit
);
1921 int edit_indent_width (WEdit
* edit
, long p
)
1924 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1926 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1929 void edit_insert_indent (WEdit
* edit
, int indent
)
1931 if (!option_fill_tabs_with_spaces
) {
1932 while (indent
>= TAB_SIZE
) {
1933 edit_insert (edit
, '\t');
1937 while (indent
-- > 0)
1938 edit_insert (edit
, ' ');
1942 edit_auto_indent (WEdit
* edit
, int extra
, int no_advance
)
1947 while (isspace (edit_get_byte (edit
, p
- 1)) && p
> 0) /* move back/up to a line with text */
1949 indent
= edit_indent_width (edit
, edit_bol (edit
, p
));
1950 if (edit
->curs_col
< indent
&& no_advance
)
1951 indent
= edit
->curs_col
;
1952 edit_insert_indent (edit
, indent
+ (option_fake_half_tabs
? HALF_TAB_SIZE
: TAB_SIZE
) * space_width
* extra
);
1955 static void edit_double_newline (WEdit
* edit
)
1957 edit_insert (edit
, '\n');
1958 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1960 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1962 edit
->force
|= REDRAW_PAGE
;
1963 edit_insert (edit
, '\n');
1966 static void edit_tab_cmd (WEdit
* edit
)
1970 if (option_fake_half_tabs
) {
1971 if (is_in_indent (edit
)) {
1972 /*insert a half tab (usually four spaces) unless there is a
1973 half tab already behind, then delete it and insert a
1975 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1976 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1977 edit_backspace (edit
);
1978 edit_insert (edit
, '\t');
1980 insert_spaces_tab (edit
, 1);
1985 if (option_fill_tabs_with_spaces
) {
1986 insert_spaces_tab (edit
, 0);
1988 edit_insert (edit
, '\t');
1993 static void check_and_wrap_line (WEdit
* edit
)
1996 if (!option_typewriter_wrap
)
1998 edit_update_curs_col (edit
);
1999 if (edit
->curs_col
< option_word_wrap_line_length
)
2004 c
= edit_get_byte (edit
, curs
);
2005 if (c
== '\n' || curs
<= 0) {
2006 edit_insert (edit
, '\n');
2009 if (c
== ' ' || c
== '\t') {
2010 int current
= edit
->curs1
;
2011 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2012 edit_insert (edit
, '\n');
2013 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2019 static void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2021 void edit_push_key_press (WEdit
* edit
)
2023 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2024 if (edit
->mark2
== -1)
2025 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2028 /* this find the matching bracket in either direction, and sets edit->bracket */
2029 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2031 const char * const b
= "{}{[][()(", *p
;
2032 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2033 unsigned long j
= 0;
2035 edit_update_curs_row (edit
);
2036 c
= edit_get_byte (edit
, edit
->curs1
);
2039 if (!furthest_bracket_search
)
2040 furthest_bracket_search
--;
2041 /* not on a bracket at all */
2044 /* the matching bracket */
2046 /* going left or right? */
2047 if (strchr ("{[(", c
))
2049 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2050 /* out of buffer? */
2051 if (q
>= edit
->last_byte
|| q
< 0)
2053 a
= edit_get_byte (edit
, q
);
2054 /* don't want to eat CPU */
2055 if (j
++ > furthest_bracket_search
)
2057 /* out of screen? */
2059 if (q
< edit
->start_display
)
2061 /* count lines if searching downward */
2062 if (inc
> 0 && a
== '\n')
2063 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2066 /* count bracket depth */
2067 i
+= (a
== c
) - (a
== d
);
2068 /* return if bracket depth is zero */
2076 static long last_bracket
= -1;
2078 static void edit_find_bracket (WEdit
* edit
)
2080 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2081 if (last_bracket
!= edit
->bracket
)
2082 edit
->force
|= REDRAW_PAGE
;
2083 last_bracket
= edit
->bracket
;
2086 static void edit_goto_matching_bracket (WEdit
*edit
)
2089 q
= edit_get_bracket (edit
, 0, 0);
2092 edit
->bracket
= edit
->curs1
;
2093 edit
->force
|= REDRAW_PAGE
;
2094 edit_cursor_move (edit
, q
- edit
->curs1
);
2098 * This executes a command as though the user initiated it through a key
2099 * press. Callback with WIDGET_KEY as a message calls this after
2100 * translating the key press. This function can be used to pass any
2101 * command to the editor. Note that the screen wouldn't update
2102 * automatically. Either of command or char_for_insertion must be
2103 * passed as -1. Commands are executed, and char_for_insertion is
2104 * inserted at the cursor. 0 is returned if the command is an undefined
2105 * macro, 1 otherwise.
2107 int edit_execute_key_command (WEdit
* edit
, int command
, int char_for_insertion
)
2110 if (command
== CK_Begin_Record_Macro
) {
2112 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2115 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2116 edit
->force
|= REDRAW_COMPLETELY
;
2117 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2121 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2122 edit
->macro
[edit
->macro_i
].command
= command
;
2123 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2125 /* record the beginning of a set of editing actions initiated by a key press */
2126 if (command
!= CK_Undo
)
2127 edit_push_key_press (edit
);
2129 r
= edit_execute_cmd (edit
, command
, char_for_insertion
);
2130 if (column_highlighting
)
2131 edit
->force
|= REDRAW_PAGE
;
2136 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
2139 This executes a command at a lower level than macro recording.
2140 It also does not push a key_press onto the undo stack. This means
2141 that if it is called many times, a single undo command will undo
2142 all of them. It also does not check for the Undo command.
2143 Returns 0 if the command is a macro that was not found, 1
2146 int edit_execute_cmd (WEdit
* edit
, int command
, int char_for_insertion
)
2149 edit
->force
|= REDRAW_LINE
;
2150 if (edit
->found_len
|| column_highlighting
)
2151 /* the next key press will unhighlight the found string, so update whole page */
2152 edit
->force
|= REDRAW_PAGE
;
2154 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2155 column_highlighting
= 0;
2156 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2157 edit_mark_cmd (edit
, 1); /* clear */
2158 edit_mark_cmd (edit
, 0); /* marking on */
2160 edit
->highlight
= 1;
2161 } else { /* any other command */
2162 if (edit
->highlight
)
2163 edit_mark_cmd (edit
, 0); /* clear */
2164 edit
->highlight
= 0;
2167 /* first check for undo */
2168 if (command
== CK_Undo
) {
2169 edit_do_undo (edit
);
2170 edit
->found_len
= 0;
2171 edit
->prev_col
= edit_get_col (edit
);
2172 edit
->search_start
= edit
->curs1
;
2175 /* An ordinary key press */
2176 if (char_for_insertion
>= 0) {
2177 if (edit
->overwrite
) {
2178 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2181 edit_insert (edit
, char_for_insertion
);
2182 if (option_auto_para_formatting
) {
2183 format_paragraph (edit
, 0);
2184 edit
->force
|= REDRAW_PAGE
;
2186 check_and_wrap_line (edit
);
2187 edit
->found_len
= 0;
2188 edit
->prev_col
= edit_get_col (edit
);
2189 edit
->search_start
= edit
->curs1
;
2190 edit_find_bracket (edit
);
2196 case CK_Begin_Page_Highlight
:
2197 case CK_End_Page_Highlight
:
2202 case CK_Word_Left_Highlight
:
2203 case CK_Word_Right_Highlight
:
2204 case CK_Up_Highlight
:
2205 case CK_Down_Highlight
:
2206 if (edit
->mark2
== -1)
2207 break; /*marking is following the cursor: may need to highlight a whole line */
2210 case CK_Left_Highlight
:
2211 case CK_Right_Highlight
:
2212 edit
->force
|= REDRAW_CHAR_ONLY
;
2215 /* basic cursor key commands */
2218 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2219 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2221 edit_backspace (edit
);
2224 if (option_fake_half_tabs
) {
2226 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2227 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2228 edit_backspace (edit
);
2233 edit_backspace (edit
);
2236 if (option_fake_half_tabs
) {
2238 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2239 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2246 case CK_Delete_Word_Left
:
2247 edit_left_delete_word (edit
);
2249 case CK_Delete_Word_Right
:
2250 edit_right_delete_word (edit
);
2252 case CK_Delete_Line
:
2253 edit_delete_line (edit
);
2255 case CK_Delete_To_Line_End
:
2256 edit_delete_to_line_end (edit
);
2258 case CK_Delete_To_Line_Begin
:
2259 edit_delete_to_line_begin (edit
);
2262 if (option_auto_para_formatting
) {
2263 edit_double_newline (edit
);
2264 if (option_return_does_auto_indent
)
2265 edit_auto_indent (edit
, 0, 1);
2266 format_paragraph (edit
, 0);
2268 edit_insert (edit
, '\n');
2269 if (option_return_does_auto_indent
) {
2270 edit_auto_indent (edit
, 0, 1);
2275 edit_insert (edit
, '\n');
2279 case CK_Page_Up_Highlight
:
2280 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2283 case CK_Page_Down_Highlight
:
2284 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2287 case CK_Left_Highlight
:
2288 if (option_fake_half_tabs
) {
2289 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2290 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2291 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2295 edit_cursor_move (edit
, -1);
2298 case CK_Right_Highlight
:
2299 if (option_fake_half_tabs
) {
2300 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2301 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2302 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2306 edit_cursor_move (edit
, 1);
2309 case CK_Begin_Page_Highlight
:
2310 edit_begin_page (edit
);
2313 case CK_End_Page_Highlight
:
2314 edit_end_page (edit
);
2317 case CK_Word_Left_Highlight
:
2318 edit_left_word_move_cmd (edit
);
2321 case CK_Word_Right_Highlight
:
2322 edit_right_word_move_cmd (edit
);
2325 case CK_Up_Highlight
:
2326 edit_move_up (edit
, 1, 0);
2329 case CK_Down_Highlight
:
2330 edit_move_down (edit
, 1, 0);
2332 case CK_Paragraph_Up
:
2333 case CK_Paragraph_Up_Highlight
:
2334 edit_move_up_paragraph (edit
, 0);
2336 case CK_Paragraph_Down
:
2337 case CK_Paragraph_Down_Highlight
:
2338 edit_move_down_paragraph (edit
, 0);
2341 case CK_Scroll_Up_Highlight
:
2342 edit_move_up (edit
, 1, 1);
2344 case CK_Scroll_Down
:
2345 case CK_Scroll_Down_Highlight
:
2346 edit_move_down (edit
, 1, 1);
2349 case CK_Home_Highlight
:
2350 edit_cursor_to_bol (edit
);
2353 case CK_End_Highlight
:
2354 edit_cursor_to_eol (edit
);
2358 edit_tab_cmd (edit
);
2359 if (option_auto_para_formatting
) {
2360 format_paragraph (edit
, 0);
2361 edit
->force
|= REDRAW_PAGE
;
2363 check_and_wrap_line (edit
);
2366 case CK_Toggle_Insert
:
2367 edit
->overwrite
= (edit
->overwrite
== 0);
2371 if (edit
->mark2
>= 0) {
2372 if (column_highlighting
)
2373 edit_push_action (edit
, COLUMN_ON
);
2374 column_highlighting
= 0;
2376 edit_mark_cmd (edit
, 0);
2378 case CK_Column_Mark
:
2379 if (!column_highlighting
)
2380 edit_push_action (edit
, COLUMN_OFF
);
2381 column_highlighting
= 1;
2382 edit_mark_cmd (edit
, 0);
2385 if (column_highlighting
)
2386 edit_push_action (edit
, COLUMN_ON
);
2387 column_highlighting
= 0;
2388 edit_mark_cmd (edit
, 1);
2391 case CK_Toggle_Bookmark
:
2392 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2393 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2394 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2396 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2398 case CK_Flush_Bookmarks
:
2399 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2400 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2401 edit
->force
|= REDRAW_PAGE
;
2403 case CK_Next_Bookmark
:
2404 if (edit
->book_mark
) {
2405 struct _book_mark
*p
;
2406 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
2409 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
|| p
->line
< edit
->start_line
)
2410 edit_move_display (edit
, p
->line
- 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
, edit
->curs_line
);
2419 while (p
->line
== edit
->curs_line
)
2423 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
|| p
->line
< edit
->start_line
)
2424 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
2425 edit_move_to_line (edit
, p
->line
);
2430 case CK_Beginning_Of_Text
:
2431 case CK_Beginning_Of_Text_Highlight
:
2432 edit_move_to_top (edit
);
2434 case CK_End_Of_Text
:
2435 case CK_End_Of_Text_Highlight
:
2436 edit_move_to_bottom (edit
);
2440 edit_block_copy_cmd (edit
);
2443 edit_block_delete_cmd (edit
);
2446 edit_block_move_cmd (edit
);
2450 edit_copy_to_X_buf_cmd (edit
);
2453 edit_cut_to_X_buf_cmd (edit
);
2456 edit_paste_from_X_buf_cmd (edit
);
2458 case CK_Selection_History
:
2459 edit_paste_from_history (edit
);
2463 edit_save_as_cmd (edit
);
2466 edit_save_confirm_cmd (edit
);
2469 edit_load_cmd (edit
);
2472 edit_save_block_cmd (edit
);
2474 case CK_Insert_File
:
2475 edit_insert_file_cmd (edit
);
2479 edit_search_cmd (edit
, 0);
2482 edit_search_cmd (edit
, 1);
2485 edit_replace_cmd (edit
, 0);
2487 case CK_Replace_Again
:
2488 edit_replace_cmd (edit
, 1);
2490 case CK_Complete_Word
:
2491 edit_complete_word_cmd (edit
);
2495 dlg_stop (edit
->widget
.parent
);
2498 edit_new_cmd (edit
);
2502 edit_help_cmd (edit
);
2506 edit_refresh_cmd (edit
);
2511 #ifdef HAVE_STRFTIME
2513 /* fool gcc to prevent a Y2K warning */
2514 char time_format
[] = "_c";
2515 time_format
[0] = '%';
2518 #ifdef HAVE_STRFTIME
2519 strftime (s
, sizeof (s
), time_format
, localtime (&t
));
2520 edit_print_string (edit
, s
);
2522 edit_print_string (edit
, ctime (&t
));
2524 edit
->force
|= REDRAW_PAGE
;
2528 edit_goto_cmd (edit
);
2530 case CK_Paragraph_Format
:
2531 format_paragraph (edit
, 1);
2532 edit
->force
|= REDRAW_PAGE
;
2534 case CK_Delete_Macro
:
2535 edit_delete_macro_cmd (edit
);
2537 case CK_Match_Bracket
:
2538 edit_goto_matching_bracket (edit
);
2544 edit_sort_cmd (edit
);
2547 edit_ext_cmd (edit
);
2550 edit_mail_dialog (edit
);
2556 /* These commands are not handled and must be handled by the user application */
2563 case CK_Terminal_App
:
2568 case CK_Save_Desktop
:
2571 case CK_Save_And_Quit
:
2572 case CK_Check_Save_And_Quit
:
2573 case CK_Run_Another
:
2574 case CK_Debug_Start
:
2576 case CK_Debug_Toggle_Break
:
2577 case CK_Debug_Clear
:
2580 case CK_Debug_Back_Trace
:
2581 case CK_Debug_Continue
:
2582 case CK_Debug_Enter_Command
:
2583 case CK_Debug_Until_Curser
:
2592 if ((command
/ 1000) == 1) /* a shell command */
2593 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2594 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2595 struct macro m
[MAX_MACRO_LENGTH
];
2597 if ((result
= edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000)))
2598 edit_execute_macro (edit
, m
, nm
);
2601 /* keys which must set the col position, and the search vars */
2606 case CK_Replace_Again
:
2607 case CK_Complete_Word
:
2608 edit
->prev_col
= edit_get_col (edit
);
2612 case CK_Up_Highlight
:
2614 case CK_Down_Highlight
:
2616 case CK_Page_Up_Highlight
:
2618 case CK_Page_Down_Highlight
:
2619 case CK_Beginning_Of_Text
:
2620 case CK_Beginning_Of_Text_Highlight
:
2621 case CK_End_Of_Text
:
2622 case CK_End_Of_Text_Highlight
:
2623 case CK_Paragraph_Up
:
2624 case CK_Paragraph_Up_Highlight
:
2625 case CK_Paragraph_Down
:
2626 case CK_Paragraph_Down_Highlight
:
2628 case CK_Scroll_Up_Highlight
:
2629 case CK_Scroll_Down
:
2630 case CK_Scroll_Down_Highlight
:
2631 edit
->search_start
= edit
->curs1
;
2632 edit
->found_len
= 0;
2633 edit_find_bracket (edit
);
2637 edit
->found_len
= 0;
2638 edit
->prev_col
= edit_get_col (edit
);
2639 edit
->search_start
= edit
->curs1
;
2641 edit_find_bracket (edit
);
2643 if (option_auto_para_formatting
) {
2647 case CK_Delete_Word_Left
:
2648 case CK_Delete_Word_Right
:
2649 case CK_Delete_To_Line_End
:
2650 case CK_Delete_To_Line_Begin
:
2651 format_paragraph (edit
, 0);
2652 edit
->force
|= REDRAW_PAGE
;
2660 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
2664 if (edit
->macro_depth
++ > 256) {
2665 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
2666 edit
->macro_depth
--;
2669 edit
->force
|= REDRAW_PAGE
;
2670 for (; i
< n
; i
++) {
2671 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2673 edit_update_screen (edit
);
2674 edit
->macro_depth
--;
2677 /* User edit menu, like user menu (F2) but only in editor. */
2679 user_menu (WEdit
* edit
)
2684 long start_mark
, end_mark
;
2685 char *block_file
= catstrs (home_dir
, BLOCK_FILE
, 0);
2688 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2689 if (!nomark
) /* remember marked or not */
2690 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2692 /* run shell scripts from menu */
2693 user_menu_cmd (edit
);
2695 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2696 /* no block messages */
2701 /* i.e. we have marked block */
2702 rc
= edit_block_delete_cmd (edit
);
2706 edit_insert_file (edit
, block_file
);
2709 /* truncate block file */
2710 if ((fd
= fopen (block_file
, "w"))) {
2714 edit_refresh_cmd (edit
);
2715 edit
->force
|= REDRAW_COMPLETELY
;