1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #if defined(NEEDS_IO_H)
28 # define CR_LF_TRANSLATION
33 #include "src/charsets.h"
34 #include "src/selcodepage.h"
38 what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
39 or EDIT_KEY_EMULATION_EMACS
41 int edit_key_emulation
= EDIT_KEY_EMULATION_NORMAL
;
43 int option_word_wrap_line_length
= 72;
44 int option_typewriter_wrap
= 0;
45 int option_auto_para_formatting
= 0;
46 int option_tab_spacing
= 8;
47 int option_fill_tabs_with_spaces
= 0;
48 int option_return_does_auto_indent
= 1;
49 int option_backspace_through_tabs
= 0;
50 int option_fake_half_tabs
= 1;
51 int option_save_mode
= 0;
52 int option_backup_ext_int
= -1;
53 int option_max_undo
= 32768;
55 int option_edit_right_extreme
= 0;
56 int option_edit_left_extreme
= 0;
57 int option_edit_top_extreme
= 0;
58 int option_edit_bottom_extreme
= 0;
60 char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
61 char *option_backup_ext
= "~";
63 static struct selection selection
;
64 static int current_selection
= 0;
65 /* Note: selection.text = selection_history[current_selection].text */
66 static struct selection selection_history
[NUM_SELECTION_HISTORY
];
68 static char *option_chars_move_whole_word
=
69 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
73 * here's a quick sketch of the layout: (don't run this through indent.)
75 * (b1 is buffers1 and b2 is buffers2)
78 * \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
79 * ______________________________________|______________________________________
81 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
82 * |-> |-> |-> |-> |-> |-> |
84 * _<------------------------->|<----------------->_
85 * WEdit->curs2 | WEdit->curs1
90 * file end|||file beginning
102 returns a byte from any location in the file.
103 Returns '\n' if out of bounds.
106 static int push_action_disabled
= 0;
108 #ifndef NO_INLINE_GETBYTE
110 int edit_get_byte (WEdit
* edit
, long byte_index
)
113 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
116 if (byte_index
>= edit
->curs1
) {
117 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
118 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
120 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
126 char *edit_get_buffer_as_text (WEdit
* e
)
130 l
= e
->curs1
+ e
->curs2
;
132 for (i
= 0; i
< l
; i
++)
133 t
[i
] = edit_get_byte (e
, i
);
139 /* Note on CRLF->LF translation: */
142 #error "MY_O_TEXT is depreciated. CR_LF_TRANSLATION must be defined which does CR-LF translation internally. See note in source."
146 The edit_open_file (previously edit_load_file) function uses
147 init_dynamic_edit_buffers to load a file. This is unnecessary
148 since you can just as well fopen the file and insert the
149 characters one by one. The real reason for
150 init_dynamic_edit_buffers (besides allocating the buffers) is
151 as an optimisation - it uses raw block reads and inserts large
152 chunks at a time. It is hence extremely fast at loading files.
153 Where we might not want to use it is if we were doing
154 CRLF->LF translation or if we were reading from a pipe.
157 /* Initialisation routines */
159 /* returns 1 on error */
160 /* loads file OR text into buffers. Only one must be none-NULL. */
161 /* cursor set to start of file */
162 int init_dynamic_edit_buffers (WEdit
* edit
, const char *filename
, const char *text
)
165 int j
, file
= -1, buf2
;
167 for (j
= 0; j
<= MAXBUFF
; j
++) {
168 edit
->buffers1
[j
] = NULL
;
169 edit
->buffers2
[j
] = NULL
;
173 if ((file
= mc_open (filename
, O_RDONLY
)) == -1) {
174 /* The file-name is printed after the ':' */
175 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename
, " ", 0)));
178 edit
->curs2
= edit
->last_byte
;
180 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
182 edit
->buffers2
[buf2
] = CMalloc (EDIT_BUF_SIZE
);
185 mc_read (file
, (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
);
187 memcpy (edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
), text
, edit
->curs2
& M_EDIT_BUF_SIZE
);
188 text
+= edit
->curs2
& M_EDIT_BUF_SIZE
;
191 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
192 edit
->buffers2
[buf
] = CMalloc (EDIT_BUF_SIZE
);
194 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
196 memcpy (edit
->buffers2
[buf
], text
, EDIT_BUF_SIZE
);
197 text
+= EDIT_BUF_SIZE
;
207 /* detecting an error on save is easy: just check if every byte has been written. */
208 /* detecting an error on read, is not so easy 'cos there is not way to tell
209 whether you read everything or not. */
210 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
211 static const struct edit_filters
{
212 char *read
, *write
, *extension
;
216 "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2"
219 "gzip -cd %s 2>&1", "gzip > %s", ".gz"
222 "gzip -cd %s 2>&1", "gzip > %s", ".Z"
226 static int edit_find_filter (const char *filename
)
231 l
= strlen (filename
);
232 for (i
= 0; i
< sizeof (all_filters
) / sizeof (struct edit_filters
); i
++) {
234 e
= strlen (all_filters
[i
].extension
);
236 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
242 char *edit_get_filter (const char *filename
)
246 i
= edit_find_filter (filename
);
249 l
= strlen (filename
);
250 p
= malloc (strlen (all_filters
[i
].read
) + l
+ 2);
251 sprintf (p
, all_filters
[i
].read
, filename
);
255 char *edit_get_write_filter (char *writename
, const char *filename
)
259 i
= edit_find_filter (filename
);
262 l
= strlen (writename
);
263 p
= malloc (strlen (all_filters
[i
].write
) + l
+ 2);
264 sprintf (p
, all_filters
[i
].write
, writename
);
268 #ifdef CR_LF_TRANSLATION
269 /* reads into buffer, replace \r\n with \n */
270 long edit_insert_stream (WEdit
* edit
, FILE * f
)
274 while ((b
= fgetc (f
))) {
275 if (a
== '\r' && b
== '\n') {
276 edit_insert (edit
, '\n');
281 edit_insert (edit
, a
);
287 edit_insert (edit
, a
);
290 /* writes buffer, replaces, replace \n with \r\n */
291 long edit_write_stream (WEdit
* edit
, FILE * f
)
295 for (i
= 0; i
< edit
->last_byte
; i
++) {
296 c
= edit_get_byte (edit
, i
);
298 if (fputc ('\r', f
) < 0)
301 if (fputc (c
, f
) < 0)
306 #else /* CR_LF_TRANSLATION */
307 long edit_insert_stream (WEdit
* edit
, FILE * f
)
311 while ((c
= fgetc (f
)) >= 0) {
312 edit_insert (edit
, c
);
317 long edit_write_stream (WEdit
* edit
, FILE * f
)
320 for (i
= 0; i
< edit
->last_byte
; i
++)
321 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
325 #endif /* CR_LF_TRANSLATION */
327 #define TEMP_BUF_LEN 1024
329 /* inserts a file at the cursor, returns 1 on success */
330 int edit_insert_file (WEdit
* edit
, const char *filename
)
333 if ((p
= edit_get_filter (filename
))) {
335 long current
= edit
->curs1
;
336 f
= (FILE *) popen (p
, "r");
338 edit_insert_stream (edit
, f
);
339 edit_cursor_move (edit
, current
- edit
->curs1
);
340 if (pclose (f
) > 0) {
341 edit_error_dialog (_ (" Error "), catstrs (_ (" Error reading from pipe: "), p
, " ", 0));
346 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open pipe for reading: "), p
, " ", 0)));
351 #ifdef CR_LF_TRANSLATION
354 long current
= edit
->curs1
;
355 f
= fopen (filename
, "r");
357 edit_insert_stream (edit
, f
);
358 edit_cursor_move (edit
, current
- edit
->curs1
);
360 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Error reading file: "), filename
, " ", 0)));
364 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename
, " ", 0)));
369 int i
, file
, blocklen
;
370 long current
= edit
->curs1
;
372 if ((file
= mc_open (filename
, O_RDONLY
)) == -1)
374 buf
= malloc (TEMP_BUF_LEN
);
375 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
376 for (i
= 0; i
< blocklen
; i
++)
377 edit_insert (edit
, buf
[i
]);
379 edit_cursor_move (edit
, current
- edit
->curs1
);
389 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
390 static int check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
395 /* Try stat first to prevent getting stuck on pipes */
396 if (mc_stat ((char *) filename
, st
) == 0) {
400 /* Only regular files are allowed */
401 if (stat_ok
&& !S_ISREG (st
->st_mode
)) {
402 edit_error_dialog (_ (" Error "), catstrs (_ (" Not an ordinary file: "), filename
, " ", 0));
406 /* Open the file, create it if needed */
407 if ((file
= mc_open (filename
, O_RDONLY
| O_CREAT
, 0666)) < 0) {
408 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename
, " ", 0)));
412 /* If the file has just been created, we don't have valid stat yet, so do it now */
413 if (!stat_ok
&& mc_fstat (file
, st
) < 0) {
415 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Cannot get size/permissions info on file: "), filename
, " ", 0)));
420 /* If it's a new file, delete it if it's not modified or saved */
421 if (!stat_ok
&& st
->st_size
== 0) {
422 edit
->delete_file
= 1;
425 if (st
->st_size
>= SIZE_LIMIT
) {
426 /* The file-name is printed after the ':' */
427 edit_error_dialog (_ (" Error "), catstrs (_ (" File is too large: "), \
428 filename
, _ (" \n Increase edit.h:MAXBUF and recompile the editor. "), 0));
434 /* returns 1 on error */
435 int edit_open_file (WEdit
* edit
, const char *filename
, const char *text
, unsigned long text_size
)
439 edit
->last_byte
= text_size
;
443 r
= check_file_access (edit
, filename
, &st
);
447 #ifndef CR_LF_TRANSLATION
448 edit
->last_byte
= st
.st_size
;
450 /* going to read the file into the buffer later byte by byte */
456 return init_dynamic_edit_buffers (edit
, filename
, text
);
459 #define space_width 1
461 /* fills in the edit struct. returns 0 on fail. Pass edit as NULL for this */
462 WEdit
*edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, const char *text
, const char *dir
, unsigned long text_size
)
471 * Expand option_whole_chars_search by national letters using
475 static char option_whole_chars_search_buf
[256];
477 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
479 int len
= strlen (option_whole_chars_search
);
481 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
483 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
484 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
485 option_whole_chars_search_buf
[len
++] = i
;
489 option_whole_chars_search_buf
[len
] = 0;
490 option_whole_chars_search
= option_whole_chars_search_buf
;
492 #endif /* ENABLE_NLS */
493 edit
= g_malloc (sizeof (WEdit
));
494 memset (edit
, 0, sizeof (WEdit
));
497 memset (&(edit
->from_here
), 0, (unsigned long)&(edit
->to_here
) - (unsigned long)&(edit
->from_here
));
498 edit
->num_widget_lines
= lines
;
499 edit
->num_widget_columns
= columns
;
500 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
501 edit
->stat1
.st_uid
= getuid ();
502 edit
->stat1
.st_gid
= getgid ();
508 f
= catstrs (dir
, filename
, 0);
510 if (edit_find_filter (f
) < 0) {
511 #ifdef CR_LF_TRANSLATION
514 if (edit_open_file (edit
, f
, text
, text_size
)) {
515 /* edit_load_file already gives an error message */
522 if (edit_open_file (edit
, 0, "", 0)) {
528 edit
->force
|= REDRAW_PAGE
;
530 filename
= catstrs (dir
, filename
, 0);
531 edit_split_filename (edit
, filename
);
533 edit
->filename
= (char *) strdup ("");
534 edit
->dir
= (char *) strdup (dir
);
536 edit
->stack_size
= START_STACK_SIZE
;
537 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
538 edit
->undo_stack
= malloc ((edit
->stack_size
+ 10) * sizeof (long));
539 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
541 push_action_disabled
= 1;
542 if (check_file_access (edit
, filename
, &(edit
->stat1
))
543 || !edit_insert_file (edit
, f
))
550 /* FIXME: this should be an unmodification() function */
551 push_action_disabled
= 0;
554 edit_load_syntax (edit
, 0, 0);
557 edit_get_syntax_color (edit
, -1, &color
);
562 /* clear the edit struct, freeing everything in it. returns 1 on success */
563 int edit_clean (WEdit
* edit
)
567 edit_free_syntax_rules (edit
);
568 book_mark_flush (edit
, -1);
569 for (; j
<= MAXBUFF
; j
++) {
570 if (edit
->buffers1
[j
] != NULL
)
571 free (edit
->buffers1
[j
]);
572 if (edit
->buffers2
[j
] != NULL
)
573 free (edit
->buffers2
[j
]);
576 if (edit
->undo_stack
)
577 free (edit
->undo_stack
);
579 free (edit
->filename
);
582 /* we don't want to clear the widget */
583 memset (&(edit
->from_here
), 0, (unsigned long)&(edit
->to_here
) - (unsigned long)&(edit
->from_here
));
585 /* Free temporary strings used in catstrs() */
594 /* returns 1 on success */
595 int edit_renew (WEdit
* edit
)
597 int lines
= edit
->num_widget_lines
;
598 int columns
= edit
->num_widget_columns
;
603 dir
= (char *) strdup (edit
->dir
);
608 if (!edit_init (edit
, lines
, columns
, 0, "", dir
, 0))
615 /* returns 1 on success, if returns 0, the edit struct would have been free'd */
616 int edit_reload (WEdit
* edit
, const char *filename
, const char *text
, const char *dir
, unsigned long text_size
)
619 int lines
= edit
->num_widget_lines
;
620 int columns
= edit
->num_widget_columns
;
621 e
= g_malloc (sizeof (WEdit
));
622 memset (e
, 0, sizeof (WEdit
));
623 e
->widget
= edit
->widget
;
625 if (!edit_init (e
, lines
, columns
, filename
, text
, dir
, text_size
)) {
630 memcpy (edit
, e
, sizeof (WEdit
));
637 Recording stack for undo:
638 The following is an implementation of a compressed stack. Identical
639 pushes are recorded by a negative prefix indicating the number of times the
640 same char was pushed. This saves space for repeated curs-left or curs-right
657 If the stack long int is 0-255 it represents a normal insert (from a backspace),
658 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
659 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
660 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
663 The only way the cursor moves or the buffer is changed is through the routines:
664 insert, backspace, insert_ahead, delete, and cursor_move.
665 These record the reverse undo movements onto the stack each time they are
668 Each key press results in a set of actions (insert; delete ...). So each time
669 a key is pressed the current position of start_display is pushed as
670 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
671 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
672 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
676 void edit_push_action (WEdit
* edit
, long c
,...)
678 unsigned long sp
= edit
->stack_pointer
;
681 /* first enlarge the stack if necessary */
682 if (sp
> edit
->stack_size
- 10) { /* say */
683 if (option_max_undo
< 256)
684 option_max_undo
= 256;
685 if (edit
->stack_size
< option_max_undo
) {
686 t
= malloc ((edit
->stack_size
* 2 + 10) * sizeof (long));
688 memcpy (t
, edit
->undo_stack
, sizeof (long) * edit
->stack_size
);
689 free (edit
->undo_stack
);
690 edit
->undo_stack
= t
;
691 edit
->stack_size
<<= 1;
692 edit
->stack_size_mask
= edit
->stack_size
- 1;
696 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
697 if (push_action_disabled
)
700 #ifdef FAST_MOVE_CURSOR
701 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
703 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
704 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
706 c
= -(va_arg (ap
, int));
709 #endif /* ! FAST_MOVE_CURSOR */
710 if (edit
->stack_bottom
!= sp
711 && spm1
!= edit
->stack_bottom
712 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
714 if (edit
->undo_stack
[spm1
] < 0) {
715 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
717 if (edit
->undo_stack
[spm1
] > -1000000000) {
718 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
719 edit
->undo_stack
[spm1
]--;
723 /* #define NO_STACK_CURSMOVE_ANIHILATION */
724 #ifndef NO_STACK_CURSMOVE_ANIHILATION
725 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
726 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
727 if (edit
->undo_stack
[spm1
] == -2)
728 edit
->stack_pointer
= spm1
;
730 edit
->undo_stack
[spm1
]++;
735 d
= edit
->undo_stack
[spm1
];
738 return; /* --> no need to push multiple do-nothings */
739 edit
->undo_stack
[sp
] = -2;
742 #ifndef NO_STACK_CURSMOVE_ANIHILATION
743 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
744 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
745 edit
->stack_pointer
= spm1
;
751 edit
->undo_stack
[sp
] = c
;
754 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
756 /*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" */
757 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
758 if (c
== edit
->stack_bottom
|| ((c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
760 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
761 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
763 /*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: */
764 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
765 edit
->stack_bottom
= edit
->stack_pointer
= 0;
769 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
770 then the file should be as it was when he loaded up. Then set edit->modified to 0.
772 long pop_action (WEdit
* edit
)
775 unsigned long sp
= edit
->stack_pointer
;
776 if (sp
== edit
->stack_bottom
) {
779 sp
= (sp
- 1) & edit
->stack_size_mask
;
780 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
781 /* edit->undo_stack[sp] = '@'; */
782 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
785 if (sp
== edit
->stack_bottom
) {
788 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
789 if (edit
->undo_stack
[sp
] == -2) {
790 /* edit->undo_stack[sp] = '@'; */
791 edit
->stack_pointer
= sp
;
793 edit
->undo_stack
[sp
]++;
798 /* is called whenever a modification is made by one of the four routines below */
799 static inline void edit_modification (WEdit
* edit
)
801 edit
->caches_valid
= 0;
803 edit
->screen_modified
= 1;
807 Basic low level single character buffer alterations and movements at the cursor.
808 Returns char passed over, inserted or removed.
811 void edit_insert (WEdit
* edit
, int c
)
813 /* check if file has grown to large */
814 if (edit
->last_byte
>= SIZE_LIMIT
)
817 /* first we must update the position of the display window */
818 if (edit
->curs1
< edit
->start_display
) {
819 edit
->start_display
++;
823 /* now we must update some info on the file and check if a redraw is required */
826 book_mark_inc (edit
, edit
->curs_line
);
829 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
831 /* tell that we've modified the file */
832 edit_modification (edit
);
834 /* save the reverse command onto the undo stack */
835 edit_push_action (edit
, BACKSPACE
);
838 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
839 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
840 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
842 /* add a new buffer if we've reached the end of the last one */
843 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
844 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
846 /* perfprm the insertion */
847 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = (unsigned char) c
;
849 /* update file length */
852 /* update cursor position */
857 /* same as edit_insert and move left */
858 void edit_insert_ahead (WEdit
* edit
, int c
)
860 if (edit
->last_byte
>= SIZE_LIMIT
)
862 if (edit
->curs1
< edit
->start_display
) {
863 edit
->start_display
++;
869 book_mark_inc (edit
, edit
->curs_line
);
871 edit
->force
|= REDRAW_AFTER_CURSOR
;
873 edit_modification (edit
);
874 edit_push_action (edit
, DELCHAR
);
876 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
877 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
878 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
880 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
881 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
882 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
889 int edit_delete (WEdit
* edit
)
895 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
896 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
897 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
899 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
901 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
902 free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
903 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
910 book_mark_dec (edit
, edit
->curs_line
);
912 edit
->force
|= REDRAW_AFTER_CURSOR
;
914 edit_push_action (edit
, p
+ 256);
915 if (edit
->curs1
< edit
->start_display
) {
916 edit
->start_display
--;
920 edit_modification (edit
);
926 int edit_backspace (WEdit
* edit
)
932 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
933 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
934 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
936 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
937 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
938 free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
939 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
946 book_mark_dec (edit
, edit
->curs_line
);
949 edit
->force
|= REDRAW_AFTER_CURSOR
;
951 edit_push_action (edit
, p
);
953 if (edit
->curs1
< edit
->start_display
) {
954 edit
->start_display
--;
958 edit_modification (edit
);
963 #ifdef FAST_MOVE_CURSOR
965 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
968 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
970 next
-= (unsigned long) dest
;
977 int edit_move_backward_lots (WEdit
* edit
, long increment
)
982 if (increment
> edit
->curs1
)
983 increment
= edit
->curs1
;
986 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
988 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
991 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
995 memqcpy (edit
, edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
996 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
999 memqcpy (edit
, edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- s
,
1000 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1001 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1002 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1004 memqcpy (edit
, edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1005 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] + EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1010 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1012 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1014 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1020 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1030 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + EDIT_BUF_SIZE
- t
,
1031 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1035 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1036 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1039 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + EDIT_BUF_SIZE
- r
,
1040 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] + EDIT_BUF_SIZE
- (r
- s
),
1046 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1048 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1050 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1056 return edit_get_byte (edit
, edit
->curs1
);
1059 #endif /* ! FAST_MOVE_CURSOR */
1061 /* moves the cursor right or left: increment positive or negative respectively */
1062 int edit_cursor_move (WEdit
* edit
, long increment
)
1064 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1067 #ifdef FAST_MOVE_CURSOR
1068 if (increment
< -256) {
1069 edit
->force
|= REDRAW_PAGE
;
1070 return edit_move_backward_lots (edit
, -increment
);
1072 #endif /* ! FAST_MOVE_CURSOR */
1074 if (increment
< 0) {
1075 for (; increment
< 0; increment
++) {
1079 edit_push_action (edit
, CURS_RIGHT
);
1081 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1082 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1083 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1084 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1086 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1087 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1088 free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1089 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1094 edit
->force
|= REDRAW_LINE_BELOW
;
1099 } else if (increment
> 0) {
1100 for (; increment
> 0; increment
--) {
1104 edit_push_action (edit
, CURS_LEFT
);
1106 c
= edit_get_byte (edit
, edit
->curs1
);
1107 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1108 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1109 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1111 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1112 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1113 free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1114 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1119 edit
->force
|= REDRAW_LINE_ABOVE
;
1127 /* These functions return positions relative to lines */
1129 /* returns index of last char on line + 1 */
1130 long edit_eol (WEdit
* edit
, long current
)
1132 if (current
< edit
->last_byte
) {
1134 if (edit_get_byte (edit
, current
) == '\n')
1137 return edit
->last_byte
;
1141 /* returns index of first char on line */
1142 long edit_bol (WEdit
* edit
, long current
)
1146 if (edit_get_byte (edit
, current
- 1) == '\n')
1154 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1157 if (upto
> edit
->last_byte
)
1158 upto
= edit
->last_byte
;
1161 while (current
< upto
)
1162 if (edit_get_byte (edit
, current
++) == '\n')
1168 /* If lines is zero this returns the count of lines from current to upto. */
1169 /* If upto is zero returns index of lines forward current. */
1170 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1173 return edit_count_lines (edit
, current
, upto
);
1179 next
= edit_eol (edit
, current
) + 1;
1180 if (next
> edit
->last_byte
)
1190 /* Returns offset of 'lines' lines up from current */
1191 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1195 current
= edit_bol (edit
, current
);
1196 while((lines
--) && current
!= 0)
1197 current
= edit_bol (edit
, current
- 1);
1201 /* If cols is zero this returns the count of columns from current to upto. */
1202 /* If upto is zero returns index of cols across from current. */
1203 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1212 q
= edit
->last_byte
+ 2;
1214 for (col
= 0, p
= current
; p
< q
; p
++) {
1222 c
= edit_get_byte (edit
, p
);
1227 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1230 /*if(edit->nroff ... */
1241 /* returns the current column position of the cursor */
1242 int edit_get_col (WEdit
* edit
)
1244 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1248 /* Scrolling functions */
1250 void edit_update_curs_row (WEdit
* edit
)
1252 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1255 void edit_update_curs_col (WEdit
* edit
)
1257 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1260 /*moves the display start position up by i lines */
1261 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1263 int lines_above
= edit
->start_line
;
1264 if (i
> lines_above
)
1267 edit
->start_line
-= i
;
1268 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1269 edit
->force
|= REDRAW_PAGE
;
1270 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1272 edit_update_curs_row (edit
);
1276 /* returns 1 if could scroll, 0 otherwise */
1277 void edit_scroll_downward (WEdit
* edit
, int i
)
1280 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1281 if (lines_below
> 0) {
1282 if (i
> lines_below
)
1284 edit
->start_line
+= i
;
1285 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1286 edit
->force
|= REDRAW_PAGE
;
1287 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1289 edit_update_curs_row (edit
);
1292 void edit_scroll_right (WEdit
* edit
, int i
)
1294 edit
->force
|= REDRAW_PAGE
;
1295 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1296 edit
->start_col
-= i
;
1299 void edit_scroll_left (WEdit
* edit
, int i
)
1301 if (edit
->start_col
) {
1302 edit
->start_col
+= i
;
1303 if (edit
->start_col
> 0)
1304 edit
->start_col
= 0;
1305 edit
->force
|= REDRAW_PAGE
;
1306 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1310 /* high level cursor movement commands */
1312 static int is_in_indent (WEdit
*edit
)
1314 long p
= edit_bol (edit
, edit
->curs1
);
1315 while (p
< edit
->curs1
)
1316 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1321 static int left_of_four_spaces (WEdit
*edit
);
1323 void edit_move_to_prev_col (WEdit
* edit
, long p
)
1325 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1327 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1328 edit_update_curs_col (edit
);
1330 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1331 int q
= edit
->curs_col
;
1332 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1333 p
= edit_bol (edit
, edit
->curs1
);
1334 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1335 if (!left_of_four_spaces (edit
))
1336 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1343 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1345 long p
, l
= edit
->curs_line
;
1351 edit
->force
|= REDRAW_PAGE
;
1353 edit_scroll_upward (edit
, i
);
1355 p
= edit_bol (edit
, edit
->curs1
);
1356 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1357 edit_move_to_prev_col (edit
, p
);
1359 edit
->search_start
= edit
->curs1
;
1360 edit
->found_len
= 0;
1364 int is_blank (WEdit
* edit
, long offset
)
1368 s
= edit_bol (edit
, offset
);
1369 f
= edit_eol (edit
, offset
) - 1;
1371 c
= edit_get_byte (edit
, s
++);
1379 /* returns the offset of line i */
1380 long edit_find_line (WEdit
* edit
, int line
)
1384 if (!edit
->caches_valid
) {
1385 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1386 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1387 /* three offsets that we *know* are line 0 at 0 and these two: */
1388 edit
->line_numbers
[1] = edit
->curs_line
;
1389 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1390 edit
->line_numbers
[2] = edit
->total_lines
;
1391 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1392 edit
->caches_valid
= 1;
1394 if (line
>= edit
->total_lines
)
1395 return edit
->line_offsets
[2];
1398 /* find the closest known point */
1399 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1401 n
= abs (edit
->line_numbers
[i
] - line
);
1408 return edit
->line_offsets
[j
]; /* know the offset exactly */
1409 if (m
== 1 && j
>= 3)
1410 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1412 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1413 if (line
> edit
->line_numbers
[j
])
1414 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1416 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1417 edit
->line_numbers
[i
] = line
;
1418 return edit
->line_offsets
[i
];
1421 int line_is_blank (WEdit
* edit
, long line
)
1423 return is_blank (edit
, edit_find_line (edit
, line
));
1426 /* moves up until a blank line is reached, or until just
1427 before a non-blank line is reached */
1428 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1431 if (edit
->curs_line
<= 1) {
1434 if (line_is_blank (edit
, edit
->curs_line
)) {
1435 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1436 for (i
= edit
->curs_line
- 1; i
; i
--)
1437 if (!line_is_blank (edit
, i
)) {
1442 for (i
= edit
->curs_line
- 1; i
; i
--)
1443 if (line_is_blank (edit
, i
))
1447 for (i
= edit
->curs_line
- 1; i
; i
--)
1448 if (line_is_blank (edit
, i
))
1452 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1456 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1458 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1464 edit
->force
|= REDRAW_PAGE
;
1466 edit_scroll_downward (edit
, i
);
1467 p
= edit_bol (edit
, edit
->curs1
);
1468 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1469 edit_move_to_prev_col (edit
, p
);
1471 edit
->search_start
= edit
->curs1
;
1472 edit
->found_len
= 0;
1476 /* moves down until a blank line is reached, or until just
1477 before a non-blank line is reached */
1478 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1481 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1482 i
= edit
->total_lines
;
1484 if (line_is_blank (edit
, edit
->curs_line
)) {
1485 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1486 for (i
= edit
->curs_line
+ 1; i
; i
++)
1487 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1492 for (i
= edit
->curs_line
+ 1; i
; i
++)
1493 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1497 for (i
= edit
->curs_line
+ 1; i
; i
++)
1498 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1502 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1505 static void edit_begin_page (WEdit
*edit
)
1507 edit_update_curs_row (edit
);
1508 edit_move_up (edit
, edit
->curs_row
, 0);
1511 static void edit_end_page (WEdit
*edit
)
1513 edit_update_curs_row (edit
);
1514 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1518 /* goto beginning of text */
1519 static void edit_move_to_top (WEdit
* edit
)
1521 if (edit
->curs_line
) {
1522 edit_cursor_move (edit
, -edit
->curs1
);
1523 edit_move_to_prev_col (edit
, 0);
1524 edit
->force
|= REDRAW_PAGE
;
1525 edit
->search_start
= 0;
1526 edit_update_curs_row(edit
);
1531 /* goto end of text */
1532 static void edit_move_to_bottom (WEdit
* edit
)
1534 if (edit
->curs_line
< edit
->total_lines
) {
1535 edit_cursor_move (edit
, edit
->curs2
);
1536 edit
->start_display
= edit
->last_byte
;
1537 edit
->start_line
= edit
->total_lines
;
1538 edit_update_curs_row(edit
);
1539 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1540 edit
->force
|= REDRAW_PAGE
;
1544 /* goto beginning of line */
1545 static void edit_cursor_to_bol (WEdit
* edit
)
1547 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1548 edit
->search_start
= edit
->curs1
;
1549 edit
->prev_col
= edit_get_col (edit
);
1552 /* goto end of line */
1553 static void edit_cursor_to_eol (WEdit
* edit
)
1555 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1556 edit
->search_start
= edit
->curs1
;
1557 edit
->prev_col
= edit_get_col (edit
);
1560 /* move cursor to line 'line' */
1561 void edit_move_to_line (WEdit
* e
, long line
)
1563 if(line
< e
->curs_line
)
1564 edit_move_up (e
, e
->curs_line
- line
, 0);
1566 edit_move_down (e
, line
- e
->curs_line
, 0);
1567 edit_scroll_screen_over_cursor (e
);
1570 /* scroll window so that first visible line is 'line' */
1571 void edit_move_display (WEdit
* e
, long line
)
1573 if(line
< e
->start_line
)
1574 edit_scroll_upward (e
, e
->start_line
- line
);
1576 edit_scroll_downward (e
, line
- e
->start_line
);
1579 /* save markers onto undo stack */
1580 void edit_push_markers (WEdit
* edit
)
1582 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1583 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1586 void free_selections (void)
1589 for (i
= 0; i
< NUM_SELECTION_HISTORY
; i
++)
1590 if (selection_history
[i
].text
) {
1591 free (selection_history
[i
].text
);
1592 selection_history
[i
].text
= 0;
1593 selection_history
[i
].len
= 0;
1595 current_selection
= 0;
1598 /* return -1 on nothing to store or error, zero otherwise */
1599 void edit_get_selection (WEdit
* edit
)
1601 long start_mark
, end_mark
;
1602 if (eval_marks (edit
, &start_mark
, &end_mark
))
1604 if (selection_history
[current_selection
].len
< 4096) /* large selections should not be held -- to save memory */
1605 current_selection
= (current_selection
+ 1) % NUM_SELECTION_HISTORY
;
1606 selection_history
[current_selection
].len
= end_mark
- start_mark
;
1607 if (selection_history
[current_selection
].text
)
1608 free (selection_history
[current_selection
].text
);
1609 selection_history
[current_selection
].text
= malloc (selection_history
[current_selection
].len
+ 1);
1610 if (!selection_history
[current_selection
].text
) {
1611 selection_history
[current_selection
].text
= malloc (1);
1612 *selection_history
[current_selection
].text
= 0;
1613 selection_history
[current_selection
].len
= 0;
1615 unsigned char *p
= selection_history
[current_selection
].text
;
1616 for (; start_mark
< end_mark
; start_mark
++)
1617 *p
++ = edit_get_byte (edit
, start_mark
);
1620 selection
.text
= selection_history
[current_selection
].text
;
1621 selection
.len
= selection_history
[current_selection
].len
;
1624 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1633 /* highlight marker toggle */
1634 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1636 edit_push_markers (edit
);
1638 edit_set_markers (edit
, 0, 0, 0, 0);
1639 edit
->force
|= REDRAW_PAGE
;
1641 if (edit
->mark2
>= 0) {
1642 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1643 edit
->force
|= REDRAW_PAGE
;
1645 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1649 static unsigned long my_type_of (int c
)
1656 if (*option_chars_move_whole_word
== '!')
1658 return 0x80000000UL
;
1662 else if (islower (c
))
1664 else if (isalpha (c
))
1666 else if (isdigit (c
))
1668 else if (isspace (c
))
1670 q
= strchr (option_chars_move_whole_word
, c
);
1672 return 0xFFFFFFFFUL
;
1674 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1678 } while ((q
= strchr (q
+ 1, c
)));
1682 void edit_left_word_move (WEdit
* edit
, int s
)
1686 edit_cursor_move (edit
, -1);
1689 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1690 c2
= edit_get_byte (edit
, edit
->curs1
);
1691 if (!(my_type_of (c1
) & my_type_of (c2
)))
1693 if (isspace (c1
) && !isspace (c2
))
1696 if (!isspace (c1
) && isspace (c2
))
1701 static void edit_left_word_move_cmd (WEdit
* edit
)
1703 edit_left_word_move (edit
, 0);
1704 edit
->force
|= REDRAW_PAGE
;
1707 void edit_right_word_move (WEdit
* edit
, int s
)
1711 edit_cursor_move (edit
, 1);
1712 if (edit
->curs1
>= edit
->last_byte
)
1714 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1715 c2
= edit_get_byte (edit
, edit
->curs1
);
1716 if (!(my_type_of (c1
) & my_type_of (c2
)))
1718 if (isspace (c1
) && !isspace (c2
))
1721 if (!isspace (c1
) && isspace (c2
))
1726 static void edit_right_word_move_cmd (WEdit
* edit
)
1728 edit_right_word_move (edit
, 0);
1729 edit
->force
|= REDRAW_PAGE
;
1733 static void edit_right_delete_word (WEdit
* edit
)
1737 if (edit
->curs1
>= edit
->last_byte
)
1739 c1
= edit_delete (edit
);
1740 c2
= edit_get_byte (edit
, edit
->curs1
);
1741 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1743 if (!(my_type_of (c1
) & my_type_of (c2
)))
1748 static void edit_left_delete_word (WEdit
* edit
)
1752 if (edit
->curs1
<= 0)
1754 c1
= edit_backspace (edit
);
1755 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1756 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1758 if (!(my_type_of (c1
) & my_type_of (c2
)))
1764 the start column position is not recorded, and hence does not
1765 undo as it happed. But who would notice.
1767 void edit_do_undo (WEdit
* edit
)
1772 push_action_disabled
= 1; /* don't record undo's onto undo stack! */
1774 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1779 edit_cursor_move (edit
, 1);
1782 edit_cursor_move (edit
, -1);
1785 edit_backspace (edit
);
1791 column_highlighting
= 1;
1794 column_highlighting
= 0;
1797 if (ac
>= 256 && ac
< 512)
1798 edit_insert_ahead (edit
, ac
- 256);
1799 if (ac
>= 0 && ac
< 256)
1800 edit_insert (edit
, ac
);
1802 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1803 edit
->mark1
= ac
- MARK_1
;
1804 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1805 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1806 edit
->mark2
= ac
- MARK_2
;
1807 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1810 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1813 if (edit
->start_display
> ac
- KEY_PRESS
) {
1814 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1815 edit
->force
|= REDRAW_PAGE
;
1816 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1817 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1818 edit
->force
|= REDRAW_PAGE
;
1820 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1821 edit_update_curs_row (edit
);
1824 push_action_disabled
= 0;
1827 static void edit_delete_to_line_end (WEdit
* edit
)
1829 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1836 static void edit_delete_to_line_begin (WEdit
* edit
)
1838 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1841 edit_backspace (edit
);
1845 void edit_delete_line (WEdit
* edit
)
1849 c
= edit_delete (edit
);
1850 } while (c
!= '\n' && c
);
1852 c
= edit_backspace (edit
);
1853 } while (c
!= '\n' && c
);
1855 edit_insert (edit
, '\n');
1858 static void insert_spaces_tab (WEdit
* edit
, int half
)
1861 edit_update_curs_col (edit
);
1862 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1864 edit_insert (edit
, ' ');
1869 static int is_aligned_on_a_tab (WEdit
* edit
)
1871 edit_update_curs_col (edit
);
1872 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1873 return 0; /* not alligned on a tab */
1877 static int right_of_four_spaces (WEdit
*edit
)
1880 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1881 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1883 return is_aligned_on_a_tab (edit
);
1887 static int left_of_four_spaces (WEdit
*edit
)
1890 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1891 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1893 return is_aligned_on_a_tab (edit
);
1897 int edit_indent_width (WEdit
* edit
, long p
)
1900 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1902 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1905 void edit_insert_indent (WEdit
* edit
, int indent
)
1907 if (!option_fill_tabs_with_spaces
) {
1908 while (indent
>= TAB_SIZE
) {
1909 edit_insert (edit
, '\t');
1913 while (indent
-- > 0)
1914 edit_insert (edit
, ' ');
1917 void edit_auto_indent (WEdit
* edit
, int extra
, int no_advance
)
1922 while (isspace (edit_get_byte (edit
, p
- 1)) && p
> 0) /* move back/up to a line with text */
1924 indent
= edit_indent_width (edit
, edit_bol (edit
, p
));
1925 if (edit
->curs_col
< indent
&& no_advance
)
1926 indent
= edit
->curs_col
;
1927 edit_insert_indent (edit
, indent
+ (option_fake_half_tabs
? HALF_TAB_SIZE
: TAB_SIZE
) * space_width
* extra
);
1930 static void edit_double_newline (WEdit
* edit
)
1932 edit_insert (edit
, '\n');
1933 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1935 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1937 edit
->force
|= REDRAW_PAGE
;
1938 edit_insert (edit
, '\n');
1941 static void edit_tab_cmd (WEdit
* edit
)
1945 if (option_fake_half_tabs
) {
1946 if (is_in_indent (edit
)) {
1947 /*insert a half tab (usually four spaces) unless there is a
1948 half tab already behind, then delete it and insert a
1950 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1951 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1952 edit_backspace (edit
);
1953 edit_insert (edit
, '\t');
1955 insert_spaces_tab (edit
, 1);
1960 if (option_fill_tabs_with_spaces
) {
1961 insert_spaces_tab (edit
, 0);
1963 edit_insert (edit
, '\t');
1968 void format_paragraph (WEdit
* edit
, int force
);
1970 static void check_and_wrap_line (WEdit
* edit
)
1973 if (!option_typewriter_wrap
)
1975 edit_update_curs_col (edit
);
1976 if (edit
->curs_col
< option_word_wrap_line_length
)
1981 c
= edit_get_byte (edit
, curs
);
1982 if (c
== '\n' || curs
<= 0) {
1983 edit_insert (edit
, '\n');
1986 if (c
== ' ' || c
== '\t') {
1987 int current
= edit
->curs1
;
1988 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1989 edit_insert (edit
, '\n');
1990 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1996 void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
1998 int edit_translate_key (WEdit
* edit
, unsigned int x_keycode
, long x_key
, int x_state
, int *cmd
, int *ch
)
2001 int char_for_insertion
= -1;
2003 #include "edit_key_translator.c"
2006 *ch
= char_for_insertion
;
2008 if((command
== -1 || command
== 0) && char_for_insertion
== -1) /* unchanged, key has no function here */
2013 void edit_push_key_press (WEdit
* edit
)
2015 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2016 if (edit
->mark2
== -1)
2017 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2020 /* this find the matching bracket in either direction, and sets edit->bracket */
2021 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2023 const char * const b
= "{}{[][()(", *p
;
2024 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2025 unsigned long j
= 0;
2027 edit_update_curs_row (edit
);
2028 c
= edit_get_byte (edit
, edit
->curs1
);
2031 if (!furthest_bracket_search
)
2032 furthest_bracket_search
--;
2033 /* not on a bracket at all */
2036 /* the matching bracket */
2038 /* going left or right? */
2039 if (strchr ("{[(", c
))
2041 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2042 /* out of buffer? */
2043 if (q
>= edit
->last_byte
|| q
< 0)
2045 a
= edit_get_byte (edit
, q
);
2046 /* don't want to eat CPU */
2047 if (j
++ > furthest_bracket_search
)
2049 /* out of screen? */
2051 if (q
< edit
->start_display
)
2053 /* count lines if searching downward */
2054 if (inc
> 0 && a
== '\n')
2055 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2058 /* count bracket depth */
2059 i
+= (a
== c
) - (a
== d
);
2060 /* return if bracket depth is zero */
2068 static long last_bracket
= -1;
2070 static void edit_find_bracket (WEdit
* edit
)
2072 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2073 if (last_bracket
!= edit
->bracket
)
2074 edit
->force
|= REDRAW_PAGE
;
2075 last_bracket
= edit
->bracket
;
2078 static void edit_goto_matching_bracket (WEdit
*edit
)
2081 q
= edit_get_bracket (edit
, 0, 0);
2084 edit
->bracket
= edit
->curs1
;
2085 edit
->force
|= REDRAW_PAGE
;
2086 edit_cursor_move (edit
, q
- edit
->curs1
);
2089 /* this executes a command as though the user initiated it through a key press. */
2090 /* callback with WIDGET_KEY as a message calls this after translating the key
2092 /* this can be used to pass any command to the editor. Same as sendevent with
2093 msg = WIDGET_COMMAND and par = command except the screen wouldn't update */
2094 /* one of command or char_for_insertion must be passed as -1 */
2095 /* commands are executed, and char_for_insertion is inserted at the cursor */
2096 /* returns 0 if the command is a macro that was not found, 1 otherwise */
2097 int edit_execute_key_command (WEdit
* edit
, int command
, int char_for_insertion
)
2100 if (command
== CK_Begin_Record_Macro
) {
2102 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2105 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2106 edit
->force
|= REDRAW_COMPLETELY
;
2107 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2111 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2112 edit
->macro
[edit
->macro_i
].command
= command
;
2113 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2115 /* record the beginning of a set of editing actions initiated by a key press */
2116 if (command
!= CK_Undo
)
2117 edit_push_key_press (edit
);
2119 r
= edit_execute_cmd (edit
, command
, char_for_insertion
);
2120 if (column_highlighting
)
2121 edit
->force
|= REDRAW_PAGE
;
2126 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
2127 void edit_mail_dialog (WEdit
* edit
);
2130 This executes a command at a lower level than macro recording.
2131 It also does not push a key_press onto the undo stack. This means
2132 that if it is called many times, a single undo command will undo
2133 all of them. It also does not check for the Undo command.
2134 Returns 0 if the command is a macro that was not found, 1
2137 int edit_execute_cmd (WEdit
* edit
, int command
, int char_for_insertion
)
2140 edit
->force
|= REDRAW_LINE
;
2141 if (edit
->found_len
|| column_highlighting
)
2142 /* the next key press will unhighlight the found string, so update whole page */
2143 edit
->force
|= REDRAW_PAGE
;
2145 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2146 column_highlighting
= 0;
2147 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2148 edit_mark_cmd (edit
, 1); /* clear */
2149 edit_mark_cmd (edit
, 0); /* marking on */
2151 edit
->highlight
= 1;
2152 } else { /* any other command */
2153 if (edit
->highlight
)
2154 edit_mark_cmd (edit
, 0); /* clear */
2155 edit
->highlight
= 0;
2158 /* first check for undo */
2159 if (command
== CK_Undo
) {
2160 edit_do_undo (edit
);
2161 edit
->found_len
= 0;
2162 edit
->prev_col
= edit_get_col (edit
);
2163 edit
->search_start
= edit
->curs1
;
2166 /* An ordinary key press */
2167 if (char_for_insertion
>= 0) {
2168 if (edit
->overwrite
) {
2169 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2172 edit_insert (edit
, char_for_insertion
);
2173 if (option_auto_para_formatting
) {
2174 format_paragraph (edit
, 0);
2175 edit
->force
|= REDRAW_PAGE
;
2177 check_and_wrap_line (edit
);
2178 edit
->found_len
= 0;
2179 edit
->prev_col
= edit_get_col (edit
);
2180 edit
->search_start
= edit
->curs1
;
2181 edit_find_bracket (edit
);
2182 edit_check_spelling (edit
);
2188 case CK_Begin_Page_Highlight
:
2189 case CK_End_Page_Highlight
:
2194 case CK_Word_Left_Highlight
:
2195 case CK_Word_Right_Highlight
:
2196 case CK_Up_Highlight
:
2197 case CK_Down_Highlight
:
2198 if (edit
->mark2
== -1)
2199 break; /*marking is following the cursor: may need to highlight a whole line */
2202 case CK_Left_Highlight
:
2203 case CK_Right_Highlight
:
2204 edit
->force
|= REDRAW_CHAR_ONLY
;
2207 /* basic cursor key commands */
2210 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2211 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2213 edit_backspace (edit
);
2216 if (option_fake_half_tabs
) {
2218 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2219 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2220 edit_backspace (edit
);
2225 edit_backspace (edit
);
2228 if (option_fake_half_tabs
) {
2230 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2231 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2238 case CK_Delete_Word_Left
:
2239 edit_left_delete_word (edit
);
2241 case CK_Delete_Word_Right
:
2242 edit_right_delete_word (edit
);
2244 case CK_Delete_Line
:
2245 edit_delete_line (edit
);
2247 case CK_Delete_To_Line_End
:
2248 edit_delete_to_line_end (edit
);
2250 case CK_Delete_To_Line_Begin
:
2251 edit_delete_to_line_begin (edit
);
2254 if (option_auto_para_formatting
) {
2255 edit_double_newline (edit
);
2256 if (option_return_does_auto_indent
)
2257 edit_auto_indent (edit
, 0, 1);
2258 format_paragraph (edit
, 0);
2260 edit_insert (edit
, '\n');
2261 if (option_return_does_auto_indent
) {
2262 edit_auto_indent (edit
, 0, 1);
2267 edit_insert (edit
, '\n');
2271 case CK_Page_Up_Highlight
:
2272 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2275 case CK_Page_Down_Highlight
:
2276 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2279 case CK_Left_Highlight
:
2280 if (option_fake_half_tabs
) {
2281 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2282 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2283 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2287 edit_cursor_move (edit
, -1);
2290 case CK_Right_Highlight
:
2291 if (option_fake_half_tabs
) {
2292 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2293 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2294 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2298 edit_cursor_move (edit
, 1);
2301 case CK_Begin_Page_Highlight
:
2302 edit_begin_page (edit
);
2305 case CK_End_Page_Highlight
:
2306 edit_end_page (edit
);
2309 case CK_Word_Left_Highlight
:
2310 edit_left_word_move_cmd (edit
);
2313 case CK_Word_Right_Highlight
:
2314 edit_right_word_move_cmd (edit
);
2317 case CK_Up_Highlight
:
2318 edit_move_up (edit
, 1, 0);
2321 case CK_Down_Highlight
:
2322 edit_move_down (edit
, 1, 0);
2324 case CK_Paragraph_Up
:
2325 case CK_Paragraph_Up_Highlight
:
2326 edit_move_up_paragraph (edit
, 0);
2328 case CK_Paragraph_Down
:
2329 case CK_Paragraph_Down_Highlight
:
2330 edit_move_down_paragraph (edit
, 0);
2333 case CK_Scroll_Up_Highlight
:
2334 edit_move_up (edit
, 1, 1);
2336 case CK_Scroll_Down
:
2337 case CK_Scroll_Down_Highlight
:
2338 edit_move_down (edit
, 1, 1);
2341 case CK_Home_Highlight
:
2342 edit_cursor_to_bol (edit
);
2345 case CK_End_Highlight
:
2346 edit_cursor_to_eol (edit
);
2350 edit_tab_cmd (edit
);
2351 if (option_auto_para_formatting
) {
2352 format_paragraph (edit
, 0);
2353 edit
->force
|= REDRAW_PAGE
;
2355 check_and_wrap_line (edit
);
2358 case CK_Toggle_Insert
:
2359 edit
->overwrite
= (edit
->overwrite
== 0);
2363 if (edit
->mark2
>= 0) {
2364 if (column_highlighting
)
2365 edit_push_action (edit
, COLUMN_ON
);
2366 column_highlighting
= 0;
2368 edit_mark_cmd (edit
, 0);
2370 case CK_Column_Mark
:
2371 if (!column_highlighting
)
2372 edit_push_action (edit
, COLUMN_OFF
);
2373 column_highlighting
= 1;
2374 edit_mark_cmd (edit
, 0);
2377 if (column_highlighting
)
2378 edit_push_action (edit
, COLUMN_ON
);
2379 column_highlighting
= 0;
2380 edit_mark_cmd (edit
, 1);
2383 case CK_Toggle_Bookmark
:
2384 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2385 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2386 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2388 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2390 case CK_Flush_Bookmarks
:
2391 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2392 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2393 edit
->force
|= REDRAW_PAGE
;
2395 case CK_Next_Bookmark
:
2396 if (edit
->book_mark
) {
2397 struct _book_mark
*p
;
2398 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
2401 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
|| p
->line
< edit
->start_line
)
2402 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
2403 edit_move_to_line (edit
, p
->line
);
2407 case CK_Prev_Bookmark
:
2408 if (edit
->book_mark
) {
2409 struct _book_mark
*p
;
2410 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
2411 while (p
->line
== edit
->curs_line
)
2415 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
|| p
->line
< edit
->start_line
)
2416 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
2417 edit_move_to_line (edit
, p
->line
);
2422 case CK_Beginning_Of_Text
:
2423 case CK_Beginning_Of_Text_Highlight
:
2424 edit_move_to_top (edit
);
2426 case CK_End_Of_Text
:
2427 case CK_End_Of_Text_Highlight
:
2428 edit_move_to_bottom (edit
);
2432 edit_block_copy_cmd (edit
);
2435 edit_block_delete_cmd (edit
);
2438 edit_block_move_cmd (edit
);
2442 edit_copy_to_X_buf_cmd (edit
);
2445 edit_cut_to_X_buf_cmd (edit
);
2448 edit_paste_from_X_buf_cmd (edit
);
2450 case CK_Selection_History
:
2451 edit_paste_from_history (edit
);
2455 edit_save_as_cmd (edit
);
2458 edit_save_confirm_cmd (edit
);
2461 edit_load_cmd (edit
);
2464 edit_save_block_cmd (edit
);
2466 case CK_Insert_File
:
2467 edit_insert_file_cmd (edit
);
2471 edit_search_cmd (edit
, 0);
2474 edit_search_cmd (edit
, 1);
2477 edit_replace_cmd (edit
, 0);
2479 case CK_Replace_Again
:
2480 edit_replace_cmd (edit
, 1);
2482 case CK_Complete_Word
:
2483 edit_complete_word_cmd (edit
);
2487 edit_quit_cmd (edit
);
2490 edit_new_cmd (edit
);
2494 edit_help_cmd (edit
);
2498 edit_refresh_cmd (edit
);
2503 #ifdef HAVE_STRFTIME
2505 static const char time_format
[] = "%c";
2508 #ifdef HAVE_STRFTIME
2509 strftime (s
, sizeof (s
), time_format
, localtime (&t
));
2510 edit_print_string (edit
, s
);
2512 edit_print_string (edit
, ctime (&t
));
2514 edit
->force
|= REDRAW_PAGE
;
2518 edit_goto_cmd (edit
);
2520 case CK_Paragraph_Format
:
2521 format_paragraph (edit
, 1);
2522 edit
->force
|= REDRAW_PAGE
;
2524 case CK_Delete_Macro
:
2525 edit_delete_macro_cmd (edit
);
2527 case CK_Match_Bracket
:
2528 edit_goto_matching_bracket (edit
);
2531 if (edit_one_file
) {
2532 message (1, MSG_ERROR
, _("User menu available only in mcedit invoked from mc"));
2539 edit_sort_cmd (edit
);
2542 edit_mail_dialog (edit
);
2548 /* These commands are not handled and must be handled by the user application */
2555 case CK_Terminal_App
:
2559 case CK_Save_Desktop
:
2562 case CK_Save_And_Quit
:
2563 case CK_Check_Save_And_Quit
:
2564 case CK_Run_Another
:
2565 case CK_Debug_Start
:
2567 case CK_Debug_Toggle_Break
:
2568 case CK_Debug_Clear
:
2571 case CK_Debug_Back_Trace
:
2572 case CK_Debug_Continue
:
2573 case CK_Debug_Enter_Command
:
2574 case CK_Debug_Until_Curser
:
2583 if ((command
/ 1000) == 1) /* a shell command */
2584 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2585 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2586 struct macro m
[MAX_MACRO_LENGTH
];
2588 if ((result
= edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000)))
2589 edit_execute_macro (edit
, m
, nm
);
2592 /* keys which must set the col position, and the search vars */
2597 case CK_Replace_Again
:
2598 case CK_Complete_Word
:
2599 edit
->prev_col
= edit_get_col (edit
);
2603 case CK_Up_Highlight
:
2605 case CK_Down_Highlight
:
2607 case CK_Page_Up_Highlight
:
2609 case CK_Page_Down_Highlight
:
2610 case CK_Beginning_Of_Text
:
2611 case CK_Beginning_Of_Text_Highlight
:
2612 case CK_End_Of_Text
:
2613 case CK_End_Of_Text_Highlight
:
2614 case CK_Paragraph_Up
:
2615 case CK_Paragraph_Up_Highlight
:
2616 case CK_Paragraph_Down
:
2617 case CK_Paragraph_Down_Highlight
:
2619 case CK_Scroll_Up_Highlight
:
2620 case CK_Scroll_Down
:
2621 case CK_Scroll_Down_Highlight
:
2622 edit
->search_start
= edit
->curs1
;
2623 edit
->found_len
= 0;
2624 edit_find_bracket (edit
);
2625 edit_check_spelling (edit
);
2629 edit
->found_len
= 0;
2630 edit
->prev_col
= edit_get_col (edit
);
2631 edit
->search_start
= edit
->curs1
;
2633 edit_find_bracket (edit
);
2634 edit_check_spelling (edit
);
2636 if (option_auto_para_formatting
) {
2640 case CK_Delete_Word_Left
:
2641 case CK_Delete_Word_Right
:
2642 case CK_Delete_To_Line_End
:
2643 case CK_Delete_To_Line_Begin
:
2644 format_paragraph (edit
, 0);
2645 edit
->force
|= REDRAW_PAGE
;
2652 /* either command or char_for_insertion must be passed as -1 */
2653 /* returns 0 if command is a macro that was not found, 1 otherwise */
2654 int edit_execute_command (WEdit
* edit
, int command
, int char_for_insertion
)
2657 r
= edit_execute_cmd (edit
, command
, char_for_insertion
);
2658 edit_update_screen (edit
);
2662 void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
2665 edit
->force
|= REDRAW_PAGE
;
2666 for (; i
< n
; i
++) {
2667 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2669 edit_update_screen (edit
);
2672 /* User edit menu, like user menu (F2) but only in editor. */
2674 user_menu (WEdit
* edit
)
2679 long start_mark
, end_mark
;
2680 char *block_file
= catstrs (home_dir
, BLOCK_FILE
, 0);
2683 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2684 if (!nomark
) /* remember marked or not */
2685 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2687 /* run shell scripts from menu */
2688 user_menu_cmd (edit
);
2690 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2691 /* no block messages */
2696 /* i.e. we have marked block */
2697 rc
= edit_block_delete_cmd (edit
);
2701 edit_insert_file (edit
, block_file
);
2704 /* truncate block file */
2705 if ((fd
= fopen (block_file
, "w"))) {
2709 edit_refresh_cmd (edit
);
2710 edit
->force
|= REDRAW_COMPLETELY
;