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
= {0, 0};
64 static int current_selection
= 0;
65 /* Note: selection.text = selection_history[current_selection].text */
66 static struct selection selection_history
[NUM_SELECTION_HISTORY
] =
80 static char *option_chars_move_whole_word
=
81 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
85 * here's a quick sketch of the layout: (don't run this through indent.)
87 * (b1 is buffers1 and b2 is buffers2)
90 * \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
91 * ______________________________________|______________________________________
93 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
94 * |-> |-> |-> |-> |-> |-> |
96 * _<------------------------->|<----------------->_
97 * WEdit->curs2 | WEdit->curs1
102 * file end|||file beginning
114 returns a byte from any location in the file.
115 Returns '\n' if out of bounds.
118 static int push_action_disabled
= 0;
120 #ifndef NO_INLINE_GETBYTE
122 int edit_get_byte (WEdit
* edit
, long byte_index
)
125 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
128 if (byte_index
>= edit
->curs1
) {
129 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
130 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
132 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
138 char *edit_get_buffer_as_text (WEdit
* e
)
142 l
= e
->curs1
+ e
->curs2
;
144 for (i
= 0; i
< l
; i
++)
145 t
[i
] = edit_get_byte (e
, i
);
151 /* Note on CRLF->LF translation: */
154 #error "MY_O_TEXT is depreciated. CR_LF_TRANSLATION must be defined which does CR-LF translation internally. See note in source."
158 The edit_open_file (previously edit_load_file) function uses
159 init_dynamic_edit_buffers to load a file. This is unnecessary
160 since you can just as well fopen the file and insert the
161 characters one by one. The real reason for
162 init_dynamic_edit_buffers (besides allocating the buffers) is
163 as an optimisation - it uses raw block reads and inserts large
164 chunks at a time. It is hence extremely fast at loading files.
165 Where we might not want to use it is if we were doing
166 CRLF->LF translation or if we were reading from a pipe.
169 /* Initialisation routines */
171 /* returns 1 on error */
172 /* loads file OR text into buffers. Only one must be none-NULL. */
173 /* cursor set to start of file */
174 int init_dynamic_edit_buffers (WEdit
* edit
, const char *filename
, const char *text
)
177 int j
, file
= -1, buf2
;
179 for (j
= 0; j
<= MAXBUFF
; j
++) {
180 edit
->buffers1
[j
] = NULL
;
181 edit
->buffers2
[j
] = NULL
;
185 if ((file
= mc_open (filename
, O_RDONLY
)) == -1) {
186 /* The file-name is printed after the ':' */
187 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename
, " ", 0)));
190 edit
->curs2
= edit
->last_byte
;
192 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
194 edit
->buffers2
[buf2
] = CMalloc (EDIT_BUF_SIZE
);
197 mc_read (file
, (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
);
199 memcpy (edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
), text
, edit
->curs2
& M_EDIT_BUF_SIZE
);
200 text
+= edit
->curs2
& M_EDIT_BUF_SIZE
;
203 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
204 edit
->buffers2
[buf
] = CMalloc (EDIT_BUF_SIZE
);
206 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
208 memcpy (edit
->buffers2
[buf
], text
, EDIT_BUF_SIZE
);
209 text
+= EDIT_BUF_SIZE
;
219 /* detecting an error on save is easy: just check if every byte has been written. */
220 /* detecting an error on read, is not so easy 'cos there is not way to tell
221 whether you read everything or not. */
222 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
223 static const struct edit_filters
{
224 char *read
, *write
, *extension
;
228 "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2"
231 "gzip -cd %s 2>&1", "gzip > %s", ".gz"
234 "compress -cd %s 2>&1", "compress > %s", ".Z"
238 static int edit_find_filter (const char *filename
)
243 l
= strlen (filename
);
244 for (i
= 0; i
< sizeof (all_filters
) / sizeof (struct edit_filters
); i
++) {
246 e
= strlen (all_filters
[i
].extension
);
248 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
254 char *edit_get_filter (const char *filename
)
258 i
= edit_find_filter (filename
);
261 l
= strlen (filename
);
262 p
= malloc (strlen (all_filters
[i
].read
) + l
+ 2);
263 sprintf (p
, all_filters
[i
].read
, filename
);
267 char *edit_get_write_filter (char *writename
, const char *filename
)
271 i
= edit_find_filter (filename
);
274 l
= strlen (writename
);
275 p
= malloc (strlen (all_filters
[i
].write
) + l
+ 2);
276 sprintf (p
, all_filters
[i
].write
, writename
);
280 #ifdef CR_LF_TRANSLATION
281 /* reads into buffer, replace \r\n with \n */
282 long edit_insert_stream (WEdit
* edit
, FILE * f
)
286 while ((b
= fgetc (f
))) {
287 if (a
== '\r' && b
== '\n') {
288 edit_insert (edit
, '\n');
293 edit_insert (edit
, a
);
299 edit_insert (edit
, a
);
302 /* writes buffer, replaces, replace \n with \r\n */
303 long edit_write_stream (WEdit
* edit
, FILE * f
)
307 for (i
= 0; i
< edit
->last_byte
; i
++) {
308 c
= edit_get_byte (edit
, i
);
310 if (fputc ('\r', f
) < 0)
313 if (fputc (c
, f
) < 0)
318 #else /* CR_LF_TRANSLATION */
319 long edit_insert_stream (WEdit
* edit
, FILE * f
)
323 while ((c
= fgetc (f
)) >= 0) {
324 edit_insert (edit
, c
);
329 long edit_write_stream (WEdit
* edit
, FILE * f
)
332 for (i
= 0; i
< edit
->last_byte
; i
++)
333 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
337 #endif /* CR_LF_TRANSLATION */
339 #define TEMP_BUF_LEN 1024
341 /* inserts a file at the cursor, returns 1 on success */
342 int edit_insert_file (WEdit
* edit
, const char *filename
)
345 if ((p
= edit_get_filter (filename
))) {
347 long current
= edit
->curs1
;
348 f
= (FILE *) popen (p
, "r");
350 edit_insert_stream (edit
, f
);
351 edit_cursor_move (edit
, current
- edit
->curs1
);
352 if (pclose (f
) > 0) {
353 edit_error_dialog (_ (" Error "), catstrs (_ (" Error reading from pipe: "), p
, " ", 0));
358 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open pipe for reading: "), p
, " ", 0)));
363 #ifdef CR_LF_TRANSLATION
366 long current
= edit
->curs1
;
367 f
= fopen (filename
, "r");
369 edit_insert_stream (edit
, f
);
370 edit_cursor_move (edit
, current
- edit
->curs1
);
372 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Error reading file: "), filename
, " ", 0)));
376 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename
, " ", 0)));
381 int i
, file
, blocklen
;
382 long current
= edit
->curs1
;
384 if ((file
= mc_open (filename
, O_RDONLY
)) == -1)
386 buf
= malloc (TEMP_BUF_LEN
);
387 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
388 for (i
= 0; i
< blocklen
; i
++)
389 edit_insert (edit
, buf
[i
]);
391 edit_cursor_move (edit
, current
- edit
->curs1
);
401 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
402 static int check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
407 /* Try stat first to prevent getting stuck on pipes */
408 if (mc_stat ((char *) filename
, st
) == 0) {
412 /* Only regular files are allowed */
413 if (stat_ok
&& !S_ISREG (st
->st_mode
)) {
414 edit_error_dialog (_ (" Error "), catstrs (_ (" Not an ordinary file: "), filename
, " ", 0));
418 /* Open the file, create it if needed */
419 if ((file
= mc_open (filename
, O_RDONLY
| O_CREAT
, 0666)) < 0) {
420 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename
, " ", 0)));
424 /* If the file has just been created, we don't have valid stat yet, so do it now */
425 if (!stat_ok
&& mc_fstat (file
, st
) < 0) {
427 edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Cannot get size/permissions info on file: "), filename
, " ", 0)));
432 /* If it's a new file, delete it if it's not modified or saved */
433 if (!stat_ok
&& st
->st_size
== 0) {
434 edit
->delete_file
= 1;
437 if (st
->st_size
>= SIZE_LIMIT
) {
438 /* The file-name is printed after the ':' */
439 edit_error_dialog (_ (" Error "), catstrs (_ (" File is too large: "), \
440 filename
, _ (" \n Increase edit.h:MAXBUF and recompile the editor. "), 0));
446 /* returns 1 on error */
447 int edit_open_file (WEdit
* edit
, const char *filename
, const char *text
, unsigned long text_size
)
451 edit
->last_byte
= text_size
;
455 r
= check_file_access (edit
, filename
, &st
);
459 #ifndef CR_LF_TRANSLATION
460 edit
->last_byte
= st
.st_size
;
462 /* going to read the file into the buffer later byte by byte */
468 return init_dynamic_edit_buffers (edit
, filename
, text
);
471 #define space_width 1
473 /* fills in the edit struct. returns 0 on fail. Pass edit as NULL for this */
474 WEdit
*edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, const char *text
, const char *dir
, unsigned long text_size
)
483 * Expand option_whole_chars_search by national letters using
487 static char option_whole_chars_search_buf
[256];
489 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
491 int len
= strlen (option_whole_chars_search
);
493 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
495 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
496 if (islower (i
) && !strchr (option_whole_chars_search
, i
)) {
497 option_whole_chars_search_buf
[len
++] = i
;
501 option_whole_chars_search_buf
[len
] = 0;
502 option_whole_chars_search
= option_whole_chars_search_buf
;
504 #endif /* ENABLE_NLS */
505 edit
= g_malloc (sizeof (WEdit
));
506 memset (edit
, 0, sizeof (WEdit
));
509 memset (&(edit
->from_here
), 0, (unsigned long)&(edit
->to_here
) - (unsigned long)&(edit
->from_here
));
510 edit
->num_widget_lines
= lines
;
511 edit
->num_widget_columns
= columns
;
512 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
513 edit
->stat1
.st_uid
= getuid ();
514 edit
->stat1
.st_gid
= getgid ();
520 f
= catstrs (dir
, filename
, 0);
522 if (edit_find_filter (f
) < 0) {
523 #ifdef CR_LF_TRANSLATION
526 if (edit_open_file (edit
, f
, text
, text_size
)) {
527 /* edit_load_file already gives an error message */
534 if (edit_open_file (edit
, 0, "", 0)) {
540 edit
->force
|= REDRAW_PAGE
;
542 filename
= catstrs (dir
, filename
, 0);
543 edit_split_filename (edit
, filename
);
545 edit
->filename
= (char *) strdup ("");
546 edit
->dir
= (char *) strdup (dir
);
548 edit
->stack_size
= START_STACK_SIZE
;
549 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
550 edit
->undo_stack
= malloc ((edit
->stack_size
+ 10) * sizeof (long));
551 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
553 push_action_disabled
= 1;
554 if (check_file_access (edit
, filename
, &(edit
->stat1
))
555 || !edit_insert_file (edit
, f
))
562 /* FIXME: this should be an unmodification() function */
563 push_action_disabled
= 0;
566 edit_load_syntax (edit
, 0, 0);
569 edit_get_syntax_color (edit
, -1, &color
);
574 /* clear the edit struct, freeing everything in it. returns 1 on success */
575 int edit_clean (WEdit
* edit
)
579 edit_free_syntax_rules (edit
);
580 book_mark_flush (edit
, -1);
581 for (; j
<= MAXBUFF
; j
++) {
582 if (edit
->buffers1
[j
] != NULL
)
583 free (edit
->buffers1
[j
]);
584 if (edit
->buffers2
[j
] != NULL
)
585 free (edit
->buffers2
[j
]);
588 if (edit
->undo_stack
)
589 free (edit
->undo_stack
);
591 free (edit
->filename
);
594 /* we don't want to clear the widget */
595 memset (&(edit
->from_here
), 0, (unsigned long)&(edit
->to_here
) - (unsigned long)&(edit
->from_here
));
597 /* Free temporary strings used in catstrs() */
606 /* returns 1 on success */
607 int edit_renew (WEdit
* edit
)
609 int lines
= edit
->num_widget_lines
;
610 int columns
= edit
->num_widget_columns
;
615 dir
= (char *) strdup (edit
->dir
);
620 if (!edit_init (edit
, lines
, columns
, 0, "", dir
, 0))
627 /* returns 1 on success, if returns 0, the edit struct would have been free'd */
628 int edit_reload (WEdit
* edit
, const char *filename
, const char *text
, const char *dir
, unsigned long text_size
)
631 int lines
= edit
->num_widget_lines
;
632 int columns
= edit
->num_widget_columns
;
633 e
= g_malloc (sizeof (WEdit
));
634 memset (e
, 0, sizeof (WEdit
));
635 e
->widget
= edit
->widget
;
637 if (!edit_init (e
, lines
, columns
, filename
, text
, dir
, text_size
)) {
642 memcpy (edit
, e
, sizeof (WEdit
));
649 Recording stack for undo:
650 The following is an implementation of a compressed stack. Identical
651 pushes are recorded by a negative prefix indicating the number of times the
652 same char was pushed. This saves space for repeated curs-left or curs-right
669 If the stack long int is 0-255 it represents a normal insert (from a backspace),
670 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
671 of the cursor functions #define'd in edit.h. 1000 through 700'000'000 is to
672 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
675 The only way the cursor moves or the buffer is changed is through the routines:
676 insert, backspace, insert_ahead, delete, and cursor_move.
677 These record the reverse undo movements onto the stack each time they are
680 Each key press results in a set of actions (insert; delete ...). So each time
681 a key is pressed the current position of start_display is pushed as
682 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
683 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
684 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
688 void edit_push_action (WEdit
* edit
, long c
,...)
690 unsigned long sp
= edit
->stack_pointer
;
693 /* first enlarge the stack if necessary */
694 if (sp
> edit
->stack_size
- 10) { /* say */
695 if (option_max_undo
< 256)
696 option_max_undo
= 256;
697 if (edit
->stack_size
< option_max_undo
) {
698 t
= malloc ((edit
->stack_size
* 2 + 10) * sizeof (long));
700 memcpy (t
, edit
->undo_stack
, sizeof (long) * edit
->stack_size
);
701 free (edit
->undo_stack
);
702 edit
->undo_stack
= t
;
703 edit
->stack_size
<<= 1;
704 edit
->stack_size_mask
= edit
->stack_size
- 1;
708 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
709 if (push_action_disabled
)
712 #ifdef FAST_MOVE_CURSOR
713 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
715 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
716 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
718 c
= -(va_arg (ap
, int));
721 #endif /* ! FAST_MOVE_CURSOR */
722 if (edit
->stack_bottom
!= sp
723 && spm1
!= edit
->stack_bottom
724 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
726 if (edit
->undo_stack
[spm1
] < 0) {
727 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
729 if (edit
->undo_stack
[spm1
] > -1000000000) {
730 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
731 edit
->undo_stack
[spm1
]--;
735 /* #define NO_STACK_CURSMOVE_ANIHILATION */
736 #ifndef NO_STACK_CURSMOVE_ANIHILATION
737 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
738 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
739 if (edit
->undo_stack
[spm1
] == -2)
740 edit
->stack_pointer
= spm1
;
742 edit
->undo_stack
[spm1
]++;
747 d
= edit
->undo_stack
[spm1
];
750 return; /* --> no need to push multiple do-nothings */
751 edit
->undo_stack
[sp
] = -2;
754 #ifndef NO_STACK_CURSMOVE_ANIHILATION
755 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
756 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
757 edit
->stack_pointer
= spm1
;
763 edit
->undo_stack
[sp
] = c
;
766 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
768 /*if the sp wraps round and catches the stack_bottom then erase the first set of actions on the stack to make space - by moving stack_bottom forward one "key press" */
769 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
770 if (c
== edit
->stack_bottom
|| ((c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
772 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
773 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
775 /*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: */
776 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
777 edit
->stack_bottom
= edit
->stack_pointer
= 0;
781 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
782 then the file should be as it was when he loaded up. Then set edit->modified to 0.
784 long pop_action (WEdit
* edit
)
787 unsigned long sp
= edit
->stack_pointer
;
788 if (sp
== edit
->stack_bottom
) {
791 sp
= (sp
- 1) & edit
->stack_size_mask
;
792 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
793 /* edit->undo_stack[sp] = '@'; */
794 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
797 if (sp
== edit
->stack_bottom
) {
800 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
801 if (edit
->undo_stack
[sp
] == -2) {
802 /* edit->undo_stack[sp] = '@'; */
803 edit
->stack_pointer
= sp
;
805 edit
->undo_stack
[sp
]++;
810 /* is called whenever a modification is made by one of the four routines below */
811 static inline void edit_modification (WEdit
* edit
)
813 edit
->caches_valid
= 0;
815 edit
->screen_modified
= 1;
819 Basic low level single character buffer alterations and movements at the cursor.
820 Returns char passed over, inserted or removed.
823 void edit_insert (WEdit
* edit
, int c
)
825 /* check if file has grown to large */
826 if (edit
->last_byte
>= SIZE_LIMIT
)
829 /* first we must update the position of the display window */
830 if (edit
->curs1
< edit
->start_display
) {
831 edit
->start_display
++;
835 /* now we must update some info on the file and check if a redraw is required */
838 book_mark_inc (edit
, edit
->curs_line
);
841 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
843 /* tell that we've modified the file */
844 edit_modification (edit
);
846 /* save the reverse command onto the undo stack */
847 edit_push_action (edit
, BACKSPACE
);
850 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
851 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
852 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
854 /* add a new buffer if we've reached the end of the last one */
855 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
856 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
858 /* perfprm the insertion */
859 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = (unsigned char) c
;
861 /* update file length */
864 /* update cursor position */
869 /* same as edit_insert and move left */
870 void edit_insert_ahead (WEdit
* edit
, int c
)
872 if (edit
->last_byte
>= SIZE_LIMIT
)
874 if (edit
->curs1
< edit
->start_display
) {
875 edit
->start_display
++;
881 book_mark_inc (edit
, edit
->curs_line
);
883 edit
->force
|= REDRAW_AFTER_CURSOR
;
885 edit_modification (edit
);
886 edit_push_action (edit
, DELCHAR
);
888 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
889 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
890 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
892 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
893 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
894 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
901 int edit_delete (WEdit
* edit
)
907 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
908 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
909 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
911 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
913 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
914 free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
915 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
922 book_mark_dec (edit
, edit
->curs_line
);
924 edit
->force
|= REDRAW_AFTER_CURSOR
;
926 edit_push_action (edit
, p
+ 256);
927 if (edit
->curs1
< edit
->start_display
) {
928 edit
->start_display
--;
932 edit_modification (edit
);
938 int edit_backspace (WEdit
* edit
)
944 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
945 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
946 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
948 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
949 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
950 free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
951 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
958 book_mark_dec (edit
, edit
->curs_line
);
961 edit
->force
|= REDRAW_AFTER_CURSOR
;
963 edit_push_action (edit
, p
);
965 if (edit
->curs1
< edit
->start_display
) {
966 edit
->start_display
--;
970 edit_modification (edit
);
975 #ifdef FAST_MOVE_CURSOR
977 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
980 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
982 next
-= (unsigned long) dest
;
989 int edit_move_backward_lots (WEdit
* edit
, long increment
)
994 if (increment
> edit
->curs1
)
995 increment
= edit
->curs1
;
998 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1000 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1003 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1007 memqcpy (edit
, edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1008 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
1011 memqcpy (edit
, edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- s
,
1012 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1013 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1014 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1016 memqcpy (edit
, edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1017 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] + EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1022 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1024 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1026 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1032 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1042 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + EDIT_BUF_SIZE
- t
,
1043 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1047 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1048 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1051 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + EDIT_BUF_SIZE
- r
,
1052 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] + EDIT_BUF_SIZE
- (r
- s
),
1058 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1060 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1062 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1068 return edit_get_byte (edit
, edit
->curs1
);
1071 #endif /* ! FAST_MOVE_CURSOR */
1073 /* moves the cursor right or left: increment positive or negative respectively */
1074 int edit_cursor_move (WEdit
* edit
, long increment
)
1076 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1079 #ifdef FAST_MOVE_CURSOR
1080 if (increment
< -256) {
1081 edit
->force
|= REDRAW_PAGE
;
1082 return edit_move_backward_lots (edit
, -increment
);
1084 #endif /* ! FAST_MOVE_CURSOR */
1086 if (increment
< 0) {
1087 for (; increment
< 0; increment
++) {
1091 edit_push_action (edit
, CURS_RIGHT
);
1093 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1094 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1095 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1096 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1098 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1099 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1100 free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1101 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1106 edit
->force
|= REDRAW_LINE_BELOW
;
1111 } else if (increment
> 0) {
1112 for (; increment
> 0; increment
--) {
1116 edit_push_action (edit
, CURS_LEFT
);
1118 c
= edit_get_byte (edit
, edit
->curs1
);
1119 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1120 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = malloc (EDIT_BUF_SIZE
);
1121 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1123 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1124 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1125 free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1126 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1131 edit
->force
|= REDRAW_LINE_ABOVE
;
1139 /* These functions return positions relative to lines */
1141 /* returns index of last char on line + 1 */
1142 long edit_eol (WEdit
* edit
, long current
)
1144 if (current
< edit
->last_byte
) {
1146 if (edit_get_byte (edit
, current
) == '\n')
1149 return edit
->last_byte
;
1153 /* returns index of first char on line */
1154 long edit_bol (WEdit
* edit
, long current
)
1158 if (edit_get_byte (edit
, current
- 1) == '\n')
1166 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1169 if (upto
> edit
->last_byte
)
1170 upto
= edit
->last_byte
;
1173 while (current
< upto
)
1174 if (edit_get_byte (edit
, current
++) == '\n')
1180 /* If lines is zero this returns the count of lines from current to upto. */
1181 /* If upto is zero returns index of lines forward current. */
1182 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1185 return edit_count_lines (edit
, current
, upto
);
1191 next
= edit_eol (edit
, current
) + 1;
1192 if (next
> edit
->last_byte
)
1202 /* Returns offset of 'lines' lines up from current */
1203 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1207 current
= edit_bol (edit
, current
);
1208 while((lines
--) && current
!= 0)
1209 current
= edit_bol (edit
, current
- 1);
1213 /* If cols is zero this returns the count of columns from current to upto. */
1214 /* If upto is zero returns index of cols across from current. */
1215 long edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1224 q
= edit
->last_byte
+ 2;
1226 for (col
= 0, p
= current
; p
< q
; p
++) {
1234 c
= edit_get_byte (edit
, p
);
1239 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1242 /*if(edit->nroff ... */
1253 /* returns the current column position of the cursor */
1254 int edit_get_col (WEdit
* edit
)
1256 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1260 /* Scrolling functions */
1262 void edit_update_curs_row (WEdit
* edit
)
1264 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1267 void edit_update_curs_col (WEdit
* edit
)
1269 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1272 /*moves the display start position up by i lines */
1273 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1275 int lines_above
= edit
->start_line
;
1276 if (i
> lines_above
)
1279 edit
->start_line
-= i
;
1280 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1281 edit
->force
|= REDRAW_PAGE
;
1282 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1284 edit_update_curs_row (edit
);
1288 /* returns 1 if could scroll, 0 otherwise */
1289 void edit_scroll_downward (WEdit
* edit
, int i
)
1292 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1293 if (lines_below
> 0) {
1294 if (i
> lines_below
)
1296 edit
->start_line
+= i
;
1297 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1298 edit
->force
|= REDRAW_PAGE
;
1299 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1301 edit_update_curs_row (edit
);
1304 void edit_scroll_right (WEdit
* edit
, int i
)
1306 edit
->force
|= REDRAW_PAGE
;
1307 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1308 edit
->start_col
-= i
;
1311 void edit_scroll_left (WEdit
* edit
, int i
)
1313 if (edit
->start_col
) {
1314 edit
->start_col
+= i
;
1315 if (edit
->start_col
> 0)
1316 edit
->start_col
= 0;
1317 edit
->force
|= REDRAW_PAGE
;
1318 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1322 /* high level cursor movement commands */
1324 static int is_in_indent (WEdit
*edit
)
1326 long p
= edit_bol (edit
, edit
->curs1
);
1327 while (p
< edit
->curs1
)
1328 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1333 static int left_of_four_spaces (WEdit
*edit
);
1335 void edit_move_to_prev_col (WEdit
* edit
, long p
)
1337 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->prev_col
, 0) - edit
->curs1
);
1339 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1340 edit_update_curs_col (edit
);
1342 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1343 int q
= edit
->curs_col
;
1344 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1345 p
= edit_bol (edit
, edit
->curs1
);
1346 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1347 if (!left_of_four_spaces (edit
))
1348 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1355 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
1357 long p
, l
= edit
->curs_line
;
1363 edit
->force
|= REDRAW_PAGE
;
1365 edit_scroll_upward (edit
, i
);
1367 p
= edit_bol (edit
, edit
->curs1
);
1368 edit_cursor_move (edit
, (p
= edit_move_backward (edit
, p
, i
)) - edit
->curs1
);
1369 edit_move_to_prev_col (edit
, p
);
1371 edit
->search_start
= edit
->curs1
;
1372 edit
->found_len
= 0;
1376 int is_blank (WEdit
* edit
, long offset
)
1380 s
= edit_bol (edit
, offset
);
1381 f
= edit_eol (edit
, offset
) - 1;
1383 c
= edit_get_byte (edit
, s
++);
1391 /* returns the offset of line i */
1392 long edit_find_line (WEdit
* edit
, int line
)
1396 if (!edit
->caches_valid
) {
1397 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1398 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1399 /* three offsets that we *know* are line 0 at 0 and these two: */
1400 edit
->line_numbers
[1] = edit
->curs_line
;
1401 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1402 edit
->line_numbers
[2] = edit
->total_lines
;
1403 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1404 edit
->caches_valid
= 1;
1406 if (line
>= edit
->total_lines
)
1407 return edit
->line_offsets
[2];
1410 /* find the closest known point */
1411 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1413 n
= abs (edit
->line_numbers
[i
] - line
);
1420 return edit
->line_offsets
[j
]; /* know the offset exactly */
1421 if (m
== 1 && j
>= 3)
1422 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1424 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1425 if (line
> edit
->line_numbers
[j
])
1426 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1428 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1429 edit
->line_numbers
[i
] = line
;
1430 return edit
->line_offsets
[i
];
1433 int line_is_blank (WEdit
* edit
, long line
)
1435 return is_blank (edit
, edit_find_line (edit
, line
));
1438 /* moves up until a blank line is reached, or until just
1439 before a non-blank line is reached */
1440 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1443 if (edit
->curs_line
<= 1) {
1446 if (line_is_blank (edit
, edit
->curs_line
)) {
1447 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1448 for (i
= edit
->curs_line
- 1; i
; i
--)
1449 if (!line_is_blank (edit
, i
)) {
1454 for (i
= edit
->curs_line
- 1; i
; i
--)
1455 if (line_is_blank (edit
, i
))
1459 for (i
= edit
->curs_line
- 1; i
; i
--)
1460 if (line_is_blank (edit
, i
))
1464 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1468 void edit_move_down (WEdit
* edit
, int i
, int scroll
)
1470 long p
, l
= edit
->total_lines
- edit
->curs_line
;
1476 edit
->force
|= REDRAW_PAGE
;
1478 edit_scroll_downward (edit
, i
);
1479 p
= edit_bol (edit
, edit
->curs1
);
1480 edit_cursor_move (edit
, (p
= edit_move_forward (edit
, p
, i
, 0)) - edit
->curs1
);
1481 edit_move_to_prev_col (edit
, p
);
1483 edit
->search_start
= edit
->curs1
;
1484 edit
->found_len
= 0;
1488 /* moves down until a blank line is reached, or until just
1489 before a non-blank line is reached */
1490 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1493 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1494 i
= edit
->total_lines
;
1496 if (line_is_blank (edit
, edit
->curs_line
)) {
1497 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1498 for (i
= edit
->curs_line
+ 1; i
; i
++)
1499 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1504 for (i
= edit
->curs_line
+ 1; i
; i
++)
1505 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1509 for (i
= edit
->curs_line
+ 1; i
; i
++)
1510 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1514 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1517 static void edit_begin_page (WEdit
*edit
)
1519 edit_update_curs_row (edit
);
1520 edit_move_up (edit
, edit
->curs_row
, 0);
1523 static void edit_end_page (WEdit
*edit
)
1525 edit_update_curs_row (edit
);
1526 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1530 /* goto beginning of text */
1531 static void edit_move_to_top (WEdit
* edit
)
1533 if (edit
->curs_line
) {
1534 edit_cursor_move (edit
, -edit
->curs1
);
1535 edit_move_to_prev_col (edit
, 0);
1536 edit
->force
|= REDRAW_PAGE
;
1537 edit
->search_start
= 0;
1538 edit_update_curs_row(edit
);
1543 /* goto end of text */
1544 static void edit_move_to_bottom (WEdit
* edit
)
1546 if (edit
->curs_line
< edit
->total_lines
) {
1547 edit_cursor_move (edit
, edit
->curs2
);
1548 edit
->start_display
= edit
->last_byte
;
1549 edit
->start_line
= edit
->total_lines
;
1550 edit_update_curs_row(edit
);
1551 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1552 edit
->force
|= REDRAW_PAGE
;
1556 /* goto beginning of line */
1557 static void edit_cursor_to_bol (WEdit
* edit
)
1559 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1560 edit
->search_start
= edit
->curs1
;
1561 edit
->prev_col
= edit_get_col (edit
);
1564 /* goto end of line */
1565 static void edit_cursor_to_eol (WEdit
* edit
)
1567 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1568 edit
->search_start
= edit
->curs1
;
1569 edit
->prev_col
= edit_get_col (edit
);
1572 /* move cursor to line 'line' */
1573 void edit_move_to_line (WEdit
* e
, long line
)
1575 if(line
< e
->curs_line
)
1576 edit_move_up (e
, e
->curs_line
- line
, 0);
1578 edit_move_down (e
, line
- e
->curs_line
, 0);
1579 edit_scroll_screen_over_cursor (e
);
1582 /* scroll window so that first visible line is 'line' */
1583 void edit_move_display (WEdit
* e
, long line
)
1585 if(line
< e
->start_line
)
1586 edit_scroll_upward (e
, e
->start_line
- line
);
1588 edit_scroll_downward (e
, line
- e
->start_line
);
1591 /* save markers onto undo stack */
1592 void edit_push_markers (WEdit
* edit
)
1594 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1595 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1598 void free_selections (void)
1601 for (i
= 0; i
< NUM_SELECTION_HISTORY
; i
++)
1602 if (selection_history
[i
].text
) {
1603 free (selection_history
[i
].text
);
1604 selection_history
[i
].text
= 0;
1605 selection_history
[i
].len
= 0;
1607 current_selection
= 0;
1610 /* return -1 on nothing to store or error, zero otherwise */
1611 void edit_get_selection (WEdit
* edit
)
1613 long start_mark
, end_mark
;
1614 if (eval_marks (edit
, &start_mark
, &end_mark
))
1616 if (selection_history
[current_selection
].len
< 4096) /* large selections should not be held -- to save memory */
1617 current_selection
= (current_selection
+ 1) % NUM_SELECTION_HISTORY
;
1618 selection_history
[current_selection
].len
= end_mark
- start_mark
;
1619 if (selection_history
[current_selection
].text
)
1620 free (selection_history
[current_selection
].text
);
1621 selection_history
[current_selection
].text
= malloc (selection_history
[current_selection
].len
+ 1);
1622 if (!selection_history
[current_selection
].text
) {
1623 selection_history
[current_selection
].text
= malloc (1);
1624 *selection_history
[current_selection
].text
= 0;
1625 selection_history
[current_selection
].len
= 0;
1627 unsigned char *p
= selection_history
[current_selection
].text
;
1628 for (; start_mark
< end_mark
; start_mark
++)
1629 *p
++ = edit_get_byte (edit
, start_mark
);
1632 selection
.text
= selection_history
[current_selection
].text
;
1633 selection
.len
= selection_history
[current_selection
].len
;
1636 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1645 /* highlight marker toggle */
1646 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1648 edit_push_markers (edit
);
1650 edit_set_markers (edit
, 0, 0, 0, 0);
1651 edit
->force
|= REDRAW_PAGE
;
1653 if (edit
->mark2
>= 0) {
1654 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
, edit
->curs_col
);
1655 edit
->force
|= REDRAW_PAGE
;
1657 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
);
1661 static unsigned long my_type_of (int c
)
1668 if (*option_chars_move_whole_word
== '!')
1670 return 0x80000000UL
;
1674 else if (islower (c
))
1676 else if (isalpha (c
))
1678 else if (isdigit (c
))
1680 else if (isspace (c
))
1682 q
= strchr (option_chars_move_whole_word
, c
);
1684 return 0xFFFFFFFFUL
;
1686 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1690 } while ((q
= strchr (q
+ 1, c
)));
1694 void edit_left_word_move (WEdit
* edit
, int s
)
1698 edit_cursor_move (edit
, -1);
1701 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1702 c2
= edit_get_byte (edit
, edit
->curs1
);
1703 if (!(my_type_of (c1
) & my_type_of (c2
)))
1705 if (isspace (c1
) && !isspace (c2
))
1708 if (!isspace (c1
) && isspace (c2
))
1713 static void edit_left_word_move_cmd (WEdit
* edit
)
1715 edit_left_word_move (edit
, 0);
1716 edit
->force
|= REDRAW_PAGE
;
1719 void edit_right_word_move (WEdit
* edit
, int s
)
1723 edit_cursor_move (edit
, 1);
1724 if (edit
->curs1
>= edit
->last_byte
)
1726 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1727 c2
= edit_get_byte (edit
, edit
->curs1
);
1728 if (!(my_type_of (c1
) & my_type_of (c2
)))
1730 if (isspace (c1
) && !isspace (c2
))
1733 if (!isspace (c1
) && isspace (c2
))
1738 static void edit_right_word_move_cmd (WEdit
* edit
)
1740 edit_right_word_move (edit
, 0);
1741 edit
->force
|= REDRAW_PAGE
;
1745 static void edit_right_delete_word (WEdit
* edit
)
1749 if (edit
->curs1
>= edit
->last_byte
)
1751 c1
= edit_delete (edit
);
1752 c2
= edit_get_byte (edit
, edit
->curs1
);
1753 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1755 if (!(my_type_of (c1
) & my_type_of (c2
)))
1760 static void edit_left_delete_word (WEdit
* edit
)
1764 if (edit
->curs1
<= 0)
1766 c1
= edit_backspace (edit
);
1767 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1768 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1770 if (!(my_type_of (c1
) & my_type_of (c2
)))
1776 the start column position is not recorded, and hence does not
1777 undo as it happed. But who would notice.
1779 void edit_do_undo (WEdit
* edit
)
1784 push_action_disabled
= 1; /* don't record undo's onto undo stack! */
1786 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
1791 edit_cursor_move (edit
, 1);
1794 edit_cursor_move (edit
, -1);
1797 edit_backspace (edit
);
1803 column_highlighting
= 1;
1806 column_highlighting
= 0;
1809 if (ac
>= 256 && ac
< 512)
1810 edit_insert_ahead (edit
, ac
- 256);
1811 if (ac
>= 0 && ac
< 256)
1812 edit_insert (edit
, ac
);
1814 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
1815 edit
->mark1
= ac
- MARK_1
;
1816 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1817 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
1818 edit
->mark2
= ac
- MARK_2
;
1819 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1822 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1825 if (edit
->start_display
> ac
- KEY_PRESS
) {
1826 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1827 edit
->force
|= REDRAW_PAGE
;
1828 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
1829 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1830 edit
->force
|= REDRAW_PAGE
;
1832 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1833 edit_update_curs_row (edit
);
1836 push_action_disabled
= 0;
1839 static void edit_delete_to_line_end (WEdit
* edit
)
1841 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1848 static void edit_delete_to_line_begin (WEdit
* edit
)
1850 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
1853 edit_backspace (edit
);
1857 void edit_delete_line (WEdit
* edit
)
1861 c
= edit_delete (edit
);
1862 } while (c
!= '\n' && c
);
1864 c
= edit_backspace (edit
);
1865 } while (c
!= '\n' && c
);
1867 edit_insert (edit
, '\n');
1870 static void insert_spaces_tab (WEdit
* edit
, int half
)
1873 edit_update_curs_col (edit
);
1874 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
1876 edit_insert (edit
, ' ');
1881 static int is_aligned_on_a_tab (WEdit
* edit
)
1883 edit_update_curs_col (edit
);
1884 if ((edit
->curs_col
% (TAB_SIZE
* space_width
)) && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
))
1885 return 0; /* not alligned on a tab */
1889 static int right_of_four_spaces (WEdit
*edit
)
1892 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1893 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1895 return is_aligned_on_a_tab (edit
);
1899 static int left_of_four_spaces (WEdit
*edit
)
1902 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1903 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1905 return is_aligned_on_a_tab (edit
);
1909 int edit_indent_width (WEdit
* edit
, long p
)
1912 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
1914 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
1917 void edit_insert_indent (WEdit
* edit
, int indent
)
1919 if (!option_fill_tabs_with_spaces
) {
1920 while (indent
>= TAB_SIZE
) {
1921 edit_insert (edit
, '\t');
1925 while (indent
-- > 0)
1926 edit_insert (edit
, ' ');
1929 void edit_auto_indent (WEdit
* edit
, int extra
, int no_advance
)
1934 while (isspace (edit_get_byte (edit
, p
- 1)) && p
> 0) /* move back/up to a line with text */
1936 indent
= edit_indent_width (edit
, edit_bol (edit
, p
));
1937 if (edit
->curs_col
< indent
&& no_advance
)
1938 indent
= edit
->curs_col
;
1939 edit_insert_indent (edit
, indent
+ (option_fake_half_tabs
? HALF_TAB_SIZE
: TAB_SIZE
) * space_width
* extra
);
1942 static void edit_double_newline (WEdit
* edit
)
1944 edit_insert (edit
, '\n');
1945 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1947 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1949 edit
->force
|= REDRAW_PAGE
;
1950 edit_insert (edit
, '\n');
1953 static void edit_tab_cmd (WEdit
* edit
)
1957 if (option_fake_half_tabs
) {
1958 if (is_in_indent (edit
)) {
1959 /*insert a half tab (usually four spaces) unless there is a
1960 half tab already behind, then delete it and insert a
1962 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
1963 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1964 edit_backspace (edit
);
1965 edit_insert (edit
, '\t');
1967 insert_spaces_tab (edit
, 1);
1972 if (option_fill_tabs_with_spaces
) {
1973 insert_spaces_tab (edit
, 0);
1975 edit_insert (edit
, '\t');
1980 void format_paragraph (WEdit
* edit
, int force
);
1982 static void check_and_wrap_line (WEdit
* edit
)
1985 if (!option_typewriter_wrap
)
1987 edit_update_curs_col (edit
);
1988 if (edit
->curs_col
< option_word_wrap_line_length
)
1993 c
= edit_get_byte (edit
, curs
);
1994 if (c
== '\n' || curs
<= 0) {
1995 edit_insert (edit
, '\n');
1998 if (c
== ' ' || c
== '\t') {
1999 int current
= edit
->curs1
;
2000 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2001 edit_insert (edit
, '\n');
2002 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2008 void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
2010 int edit_translate_key (WEdit
* edit
, unsigned int x_keycode
, long x_key
, int x_state
, int *cmd
, int *ch
)
2013 int char_for_insertion
= -1;
2015 #include "edit_key_translator.c"
2018 *ch
= char_for_insertion
;
2020 if((command
== -1 || command
== 0) && char_for_insertion
== -1) /* unchanged, key has no function here */
2025 void edit_push_key_press (WEdit
* edit
)
2027 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2028 if (edit
->mark2
== -1)
2029 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2032 /* this find the matching bracket in either direction, and sets edit->bracket */
2033 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2035 const char *b
= "{}{[][()(", *p
;
2036 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2037 unsigned long j
= 0;
2039 edit_update_curs_row (edit
);
2040 c
= edit_get_byte (edit
, edit
->curs1
);
2043 if (!furthest_bracket_search
)
2044 furthest_bracket_search
--;
2045 /* not on a bracket at all */
2048 /* the matching bracket */
2050 /* going left or right? */
2051 if (strchr ("{[(", c
))
2053 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2054 /* out of buffer? */
2055 if (q
>= edit
->last_byte
|| q
< 0)
2057 a
= edit_get_byte (edit
, q
);
2058 /* don't want to eat CPU */
2059 if (j
++ > furthest_bracket_search
)
2061 /* out of screen? */
2063 if (q
< edit
->start_display
)
2065 /* count lines if searching downward */
2066 if (inc
> 0 && a
== '\n')
2067 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2070 /* count bracket depth */
2071 i
+= (a
== c
) - (a
== d
);
2072 /* return if bracket depth is zero */
2080 static long last_bracket
= -1;
2082 static void edit_find_bracket (WEdit
* edit
)
2084 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2085 if (last_bracket
!= edit
->bracket
)
2086 edit
->force
|= REDRAW_PAGE
;
2087 last_bracket
= edit
->bracket
;
2090 static void edit_goto_matching_bracket (WEdit
*edit
)
2093 q
= edit_get_bracket (edit
, 0, 0);
2096 edit
->bracket
= edit
->curs1
;
2097 edit
->force
|= REDRAW_PAGE
;
2098 edit_cursor_move (edit
, q
- edit
->curs1
);
2101 /* this executes a command as though the user initiated it through a key press. */
2102 /* callback with WIDGET_KEY as a message calls this after translating the key
2104 /* this can be used to pass any command to the editor. Same as sendevent with
2105 msg = WIDGET_COMMAND and par = command except the screen wouldn't update */
2106 /* one of command or char_for_insertion must be passed as -1 */
2107 /* commands are executed, and char_for_insertion is inserted at the cursor */
2108 /* returns 0 if the command is a macro that was not found, 1 otherwise */
2109 int edit_execute_key_command (WEdit
* edit
, int command
, int char_for_insertion
)
2112 if (command
== CK_Begin_Record_Macro
) {
2114 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2117 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2118 edit
->force
|= REDRAW_COMPLETELY
;
2119 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2123 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2124 edit
->macro
[edit
->macro_i
].command
= command
;
2125 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2127 /* record the beginning of a set of editing actions initiated by a key press */
2128 if (command
!= CK_Undo
)
2129 edit_push_key_press (edit
);
2131 r
= edit_execute_cmd (edit
, command
, char_for_insertion
);
2132 if (column_highlighting
)
2133 edit
->force
|= REDRAW_PAGE
;
2138 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
2139 void edit_mail_dialog (WEdit
* edit
);
2142 This executes a command at a lower level than macro recording.
2143 It also does not push a key_press onto the undo stack. This means
2144 that if it is called many times, a single undo command will undo
2145 all of them. It also does not check for the Undo command.
2146 Returns 0 if the command is a macro that was not found, 1
2149 int edit_execute_cmd (WEdit
* edit
, int command
, int char_for_insertion
)
2152 edit
->force
|= REDRAW_LINE
;
2153 if (edit
->found_len
|| column_highlighting
)
2154 /* the next key press will unhighlight the found string, so update whole page */
2155 edit
->force
|= REDRAW_PAGE
;
2157 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2158 column_highlighting
= 0;
2159 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2160 edit_mark_cmd (edit
, 1); /* clear */
2161 edit_mark_cmd (edit
, 0); /* marking on */
2163 edit
->highlight
= 1;
2164 } else { /* any other command */
2165 if (edit
->highlight
)
2166 edit_mark_cmd (edit
, 0); /* clear */
2167 edit
->highlight
= 0;
2170 /* first check for undo */
2171 if (command
== CK_Undo
) {
2172 edit_do_undo (edit
);
2173 edit
->found_len
= 0;
2174 edit
->prev_col
= edit_get_col (edit
);
2175 edit
->search_start
= edit
->curs1
;
2178 /* An ordinary key press */
2179 if (char_for_insertion
>= 0) {
2180 if (edit
->overwrite
) {
2181 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2184 edit_insert (edit
, char_for_insertion
);
2185 if (option_auto_para_formatting
) {
2186 format_paragraph (edit
, 0);
2187 edit
->force
|= REDRAW_PAGE
;
2189 check_and_wrap_line (edit
);
2190 edit
->found_len
= 0;
2191 edit
->prev_col
= edit_get_col (edit
);
2192 edit
->search_start
= edit
->curs1
;
2193 edit_find_bracket (edit
);
2194 edit_check_spelling (edit
);
2200 case CK_Begin_Page_Highlight
:
2201 case CK_End_Page_Highlight
:
2206 case CK_Word_Left_Highlight
:
2207 case CK_Word_Right_Highlight
:
2208 case CK_Up_Highlight
:
2209 case CK_Down_Highlight
:
2210 if (edit
->mark2
== -1)
2211 break; /*marking is following the cursor: may need to highlight a whole line */
2214 case CK_Left_Highlight
:
2215 case CK_Right_Highlight
:
2216 edit
->force
|= REDRAW_CHAR_ONLY
;
2219 /* basic cursor key commands */
2222 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2223 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2225 edit_backspace (edit
);
2228 if (option_fake_half_tabs
) {
2230 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2231 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2232 edit_backspace (edit
);
2237 edit_backspace (edit
);
2240 if (option_fake_half_tabs
) {
2242 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2243 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2250 case CK_Delete_Word_Left
:
2251 edit_left_delete_word (edit
);
2253 case CK_Delete_Word_Right
:
2254 edit_right_delete_word (edit
);
2256 case CK_Delete_Line
:
2257 edit_delete_line (edit
);
2259 case CK_Delete_To_Line_End
:
2260 edit_delete_to_line_end (edit
);
2262 case CK_Delete_To_Line_Begin
:
2263 edit_delete_to_line_begin (edit
);
2266 if (option_auto_para_formatting
) {
2267 edit_double_newline (edit
);
2268 if (option_return_does_auto_indent
)
2269 edit_auto_indent (edit
, 0, 1);
2270 format_paragraph (edit
, 0);
2272 edit_insert (edit
, '\n');
2273 if (option_return_does_auto_indent
) {
2274 edit_auto_indent (edit
, 0, 1);
2279 edit_insert (edit
, '\n');
2283 case CK_Page_Up_Highlight
:
2284 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2287 case CK_Page_Down_Highlight
:
2288 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2291 case CK_Left_Highlight
:
2292 if (option_fake_half_tabs
) {
2293 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2294 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2295 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2299 edit_cursor_move (edit
, -1);
2302 case CK_Right_Highlight
:
2303 if (option_fake_half_tabs
) {
2304 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2305 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2306 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2310 edit_cursor_move (edit
, 1);
2313 case CK_Begin_Page_Highlight
:
2314 edit_begin_page (edit
);
2317 case CK_End_Page_Highlight
:
2318 edit_end_page (edit
);
2321 case CK_Word_Left_Highlight
:
2322 edit_left_word_move_cmd (edit
);
2325 case CK_Word_Right_Highlight
:
2326 edit_right_word_move_cmd (edit
);
2329 case CK_Up_Highlight
:
2330 edit_move_up (edit
, 1, 0);
2333 case CK_Down_Highlight
:
2334 edit_move_down (edit
, 1, 0);
2336 case CK_Paragraph_Up
:
2337 case CK_Paragraph_Up_Highlight
:
2338 edit_move_up_paragraph (edit
, 0);
2340 case CK_Paragraph_Down
:
2341 case CK_Paragraph_Down_Highlight
:
2342 edit_move_down_paragraph (edit
, 0);
2345 case CK_Scroll_Up_Highlight
:
2346 edit_move_up (edit
, 1, 1);
2348 case CK_Scroll_Down
:
2349 case CK_Scroll_Down_Highlight
:
2350 edit_move_down (edit
, 1, 1);
2353 case CK_Home_Highlight
:
2354 edit_cursor_to_bol (edit
);
2357 case CK_End_Highlight
:
2358 edit_cursor_to_eol (edit
);
2362 edit_tab_cmd (edit
);
2363 if (option_auto_para_formatting
) {
2364 format_paragraph (edit
, 0);
2365 edit
->force
|= REDRAW_PAGE
;
2367 check_and_wrap_line (edit
);
2370 case CK_Toggle_Insert
:
2371 edit
->overwrite
= (edit
->overwrite
== 0);
2375 if (edit
->mark2
>= 0) {
2376 if (column_highlighting
)
2377 edit_push_action (edit
, COLUMN_ON
);
2378 column_highlighting
= 0;
2380 edit_mark_cmd (edit
, 0);
2382 case CK_Column_Mark
:
2383 if (!column_highlighting
)
2384 edit_push_action (edit
, COLUMN_OFF
);
2385 column_highlighting
= 1;
2386 edit_mark_cmd (edit
, 0);
2389 if (column_highlighting
)
2390 edit_push_action (edit
, COLUMN_ON
);
2391 column_highlighting
= 0;
2392 edit_mark_cmd (edit
, 1);
2395 case CK_Toggle_Bookmark
:
2396 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2397 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2398 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2400 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2402 case CK_Flush_Bookmarks
:
2403 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2404 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2405 edit
->force
|= REDRAW_PAGE
;
2407 case CK_Next_Bookmark
:
2408 if (edit
->book_mark
) {
2409 struct _book_mark
*p
;
2410 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
2413 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
|| p
->line
< edit
->start_line
)
2414 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
2415 edit_move_to_line (edit
, p
->line
);
2419 case CK_Prev_Bookmark
:
2420 if (edit
->book_mark
) {
2421 struct _book_mark
*p
;
2422 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
2423 while (p
->line
== edit
->curs_line
)
2427 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
|| p
->line
< edit
->start_line
)
2428 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
2429 edit_move_to_line (edit
, p
->line
);
2434 case CK_Beginning_Of_Text
:
2435 case CK_Beginning_Of_Text_Highlight
:
2436 edit_move_to_top (edit
);
2438 case CK_End_Of_Text
:
2439 case CK_End_Of_Text_Highlight
:
2440 edit_move_to_bottom (edit
);
2444 edit_block_copy_cmd (edit
);
2447 edit_block_delete_cmd (edit
);
2450 edit_block_move_cmd (edit
);
2454 edit_copy_to_X_buf_cmd (edit
);
2457 edit_cut_to_X_buf_cmd (edit
);
2460 edit_paste_from_X_buf_cmd (edit
);
2462 case CK_Selection_History
:
2463 edit_paste_from_history (edit
);
2467 edit_save_as_cmd (edit
);
2470 edit_save_confirm_cmd (edit
);
2473 edit_load_cmd (edit
);
2476 edit_save_block_cmd (edit
);
2478 case CK_Insert_File
:
2479 edit_insert_file_cmd (edit
);
2483 edit_search_cmd (edit
, 0);
2486 edit_search_cmd (edit
, 1);
2489 edit_replace_cmd (edit
, 0);
2491 case CK_Replace_Again
:
2492 edit_replace_cmd (edit
, 1);
2494 case CK_Complete_Word
:
2495 edit_complete_word_cmd (edit
);
2499 edit_quit_cmd (edit
);
2502 edit_new_cmd (edit
);
2506 edit_help_cmd (edit
);
2510 edit_refresh_cmd (edit
);
2515 #ifdef HAVE_STRFTIME
2517 static const char time_format
[] = "%c";
2520 #ifdef HAVE_STRFTIME
2521 strftime (s
, sizeof (s
), time_format
, localtime (&t
));
2522 edit_print_string (edit
, s
);
2524 edit_print_string (edit
, ctime (&t
));
2526 edit
->force
|= REDRAW_PAGE
;
2530 edit_goto_cmd (edit
);
2532 case CK_Paragraph_Format
:
2533 format_paragraph (edit
, 1);
2534 edit
->force
|= REDRAW_PAGE
;
2536 case CK_Delete_Macro
:
2537 edit_delete_macro_cmd (edit
);
2539 case CK_Match_Bracket
:
2540 edit_goto_matching_bracket (edit
);
2543 if (edit_one_file
) {
2544 message (1, MSG_ERROR
, _("User menu available only in mcedit invoked from mc"));
2551 edit_sort_cmd (edit
);
2554 edit_mail_dialog (edit
);
2560 /* These commands are not handled and must be handled by the user application */
2567 case CK_Terminal_App
:
2571 case CK_Save_Desktop
:
2574 case CK_Save_And_Quit
:
2575 case CK_Check_Save_And_Quit
:
2576 case CK_Run_Another
:
2577 case CK_Debug_Start
:
2579 case CK_Debug_Toggle_Break
:
2580 case CK_Debug_Clear
:
2583 case CK_Debug_Back_Trace
:
2584 case CK_Debug_Continue
:
2585 case CK_Debug_Enter_Command
:
2586 case CK_Debug_Until_Curser
:
2595 if ((command
/ 1000) == 1) /* a shell command */
2596 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
2597 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
2598 struct macro m
[MAX_MACRO_LENGTH
];
2600 if ((result
= edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000)))
2601 edit_execute_macro (edit
, m
, nm
);
2604 /* keys which must set the col position, and the search vars */
2609 case CK_Replace_Again
:
2610 case CK_Complete_Word
:
2611 edit
->prev_col
= edit_get_col (edit
);
2615 case CK_Up_Highlight
:
2617 case CK_Down_Highlight
:
2619 case CK_Page_Up_Highlight
:
2621 case CK_Page_Down_Highlight
:
2622 case CK_Beginning_Of_Text
:
2623 case CK_Beginning_Of_Text_Highlight
:
2624 case CK_End_Of_Text
:
2625 case CK_End_Of_Text_Highlight
:
2626 case CK_Paragraph_Up
:
2627 case CK_Paragraph_Up_Highlight
:
2628 case CK_Paragraph_Down
:
2629 case CK_Paragraph_Down_Highlight
:
2631 case CK_Scroll_Up_Highlight
:
2632 case CK_Scroll_Down
:
2633 case CK_Scroll_Down_Highlight
:
2634 edit
->search_start
= edit
->curs1
;
2635 edit
->found_len
= 0;
2636 edit_find_bracket (edit
);
2637 edit_check_spelling (edit
);
2641 edit
->found_len
= 0;
2642 edit
->prev_col
= edit_get_col (edit
);
2643 edit
->search_start
= edit
->curs1
;
2645 edit_find_bracket (edit
);
2646 edit_check_spelling (edit
);
2648 if (option_auto_para_formatting
) {
2652 case CK_Delete_Word_Left
:
2653 case CK_Delete_Word_Right
:
2654 case CK_Delete_To_Line_End
:
2655 case CK_Delete_To_Line_Begin
:
2656 format_paragraph (edit
, 0);
2657 edit
->force
|= REDRAW_PAGE
;
2664 /* either command or char_for_insertion must be passed as -1 */
2665 /* returns 0 if command is a macro that was not found, 1 otherwise */
2666 int edit_execute_command (WEdit
* edit
, int command
, int char_for_insertion
)
2669 r
= edit_execute_cmd (edit
, command
, char_for_insertion
);
2670 edit_update_screen (edit
);
2674 void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
2677 edit
->force
|= REDRAW_PAGE
;
2678 for (; i
< n
; i
++) {
2679 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
2681 edit_update_screen (edit
);
2684 /* User edit menu, like user menu (F2) but only in editor. */
2686 user_menu (WEdit
* edit
)
2691 long start_mark
, end_mark
;
2692 char *block_file
= catstrs (home_dir
, BLOCK_FILE
, 0);
2695 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
2696 if (!nomark
) /* remember marked or not */
2697 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
2699 /* run shell scripts from menu */
2700 user_menu_cmd (edit
);
2702 if (mc_stat (block_file
, &status
) != 0 || !status
.st_size
) {
2703 /* no block messages */
2708 /* i.e. we have marked block */
2709 rc
= edit_block_delete_cmd (edit
);
2713 edit_insert_file (edit
, block_file
);
2716 /* truncate block file */
2717 if ((fd
= fopen (block_file
, "w"))) {
2721 edit_refresh_cmd (edit
);
2722 edit
->force
|= REDRAW_COMPLETELY
;