1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * \brief Source: editor low level data handling and cursor fundamentals
33 #include <sys/types.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "src/charsets.h" /* get_codepage_id */
57 #include "src/main.h" /* source_codepage */
58 #include "src/learn.h" /* learn_keys */
59 #include "src/cmddef.h"
61 #include "edit-impl.h"
63 #include "edit-widget.h"
66 int option_word_wrap_line_length
= 72;
67 int option_typewriter_wrap
= 0;
68 int option_auto_para_formatting
= 0;
69 int option_fill_tabs_with_spaces
= 0;
70 int option_return_does_auto_indent
= 1;
71 int option_backspace_through_tabs
= 0;
72 int option_fake_half_tabs
= 1;
73 int option_save_mode
= EDIT_QUICK_SAVE
;
74 int option_save_position
= 1;
75 int option_max_undo
= 32768;
76 int option_persistent_selections
= 1;
77 int option_cursor_beyond_eol
= 0;
78 int option_line_state
= 0;
79 int option_line_state_width
= 0;
81 int option_edit_right_extreme
= 0;
82 int option_edit_left_extreme
= 0;
83 int option_edit_top_extreme
= 0;
84 int option_edit_bottom_extreme
= 0;
85 int enable_show_tabs_tws
= 1;
86 int option_check_nl_at_eof
= 0;
87 int show_right_margin
= 0;
89 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext
= NULL
;
92 int edit_stack_iterator
= 0;
93 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC
[] = {'\1', '\1', '\1', '\1', '\n'};
98 * here's a quick sketch of the layout: (don't run this through indent.)
100 * (b1 is buffers1 and b2 is buffers2)
103 * \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
104 * ______________________________________|______________________________________
106 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
107 * |-> |-> |-> |-> |-> |-> |
109 * _<------------------------->|<----------------->_
110 * WEdit->curs2 | WEdit->curs1
115 * file end|||file beginning
124 const global_keymap_t
*editor_map
;
125 const global_keymap_t
*editor_x_map
;
127 static void user_menu (WEdit
*edit
);
129 int edit_get_byte (WEdit
* edit
, long byte_index
)
132 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
135 if (byte_index
>= edit
->curs1
) {
136 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
137 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
139 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
143 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
146 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
149 if (byte_index
>= edit
->curs1
) {
150 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
151 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
]+(EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
153 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
]+(byte_index
& M_EDIT_BUF_SIZE
));
157 char *edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
161 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
167 if (byte_index
>= edit
->curs1
) {
168 p
= edit
->curs1
+ edit
->curs2
- 1;
169 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
171 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
175 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
180 gchar
*next_ch
= NULL
;
183 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
188 str
= edit_get_byte_ptr (edit
, byte_index
);
195 res
= g_utf8_get_char_validated (str
, -1);
202 /* Calculate UTF-8 char width */
203 next_ch
= g_utf8_next_char (str
);
205 width
= next_ch
- str
;
215 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
217 gchar
*str
, *buf
= NULL
;
220 gchar
*next_ch
= NULL
;
226 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
231 ch
= edit_get_utf (edit
, byte_index
, &width
);
238 str
= edit_get_byte_ptr (edit
, byte_index
);
239 buf
= edit_get_buf_ptr (edit
, byte_index
);
240 if (str
== NULL
|| buf
== NULL
) {
244 /* get prev utf8 char */
246 str
= g_utf8_find_prev_char (buf
, str
);
248 res
= g_utf8_get_char_validated (str
, -1);
255 /* Calculate UTF-8 char width */
256 next_ch
= g_utf8_next_char(str
);
258 width
= next_ch
- str
;
269 * Initialize the buffers for an empty files.
272 edit_init_buffers (WEdit
*edit
)
276 for (j
= 0; j
<= MAXBUFF
; j
++) {
277 edit
->buffers1
[j
] = NULL
;
278 edit
->buffers2
[j
] = NULL
;
283 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
287 * Load file OR text into buffers. Set cursor to the beginning of file.
291 edit_load_file_fast (WEdit
*edit
, const char *filename
)
295 edit
->curs2
= edit
->last_byte
;
296 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
298 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
299 GString
*errmsg
= g_string_new(NULL
);
300 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
301 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
302 g_string_free (errmsg
, TRUE
);
306 if (!edit
->buffers2
[buf2
])
307 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
310 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
311 (edit
->curs2
& M_EDIT_BUF_SIZE
),
312 edit
->curs2
& M_EDIT_BUF_SIZE
);
314 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
315 /* edit->buffers2[0] is already allocated */
316 if (!edit
->buffers2
[buf
])
317 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
318 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
325 /* detecting an error on save is easy: just check if every byte has been written. */
326 /* detecting an error on read, is not so easy 'cos there is not way to tell
327 whether you read everything or not. */
328 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
329 static const struct edit_filters
{
330 const char *read
, *write
, *extension
;
332 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
333 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
334 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
335 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
336 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
339 /* Return index of the filter or -1 is there is no appropriate filter */
340 static int edit_find_filter (const char *filename
)
345 l
= strlen (filename
);
346 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
347 e
= strlen (all_filters
[i
].extension
);
349 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
356 edit_get_filter (const char *filename
)
359 char *p
, *quoted_name
;
360 i
= edit_find_filter (filename
);
363 quoted_name
= name_quote (filename
, 0);
364 p
= g_strdup_printf(all_filters
[i
].read
, quoted_name
);
365 g_free (quoted_name
);
370 edit_get_write_filter (const char *write_name
, const char *filename
)
374 i
= edit_find_filter (filename
);
377 writename
= name_quote (write_name
, 0);
378 p
= g_strdup_printf(all_filters
[i
].write
, writename
);
384 edit_insert_stream (WEdit
* edit
, FILE * f
)
388 while ((c
= fgetc (f
)) >= 0) {
389 edit_insert (edit
, c
);
395 long edit_write_stream (WEdit
* edit
, FILE * f
)
399 if (edit
->lb
== LB_ASIS
) {
400 for (i
= 0; i
< edit
->last_byte
; i
++)
401 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
406 /* change line breaks */
407 for (i
= 0; i
< edit
->last_byte
; i
++) {
408 unsigned char c
= edit_get_byte (edit
, i
);
410 if (!(c
== '\n' || c
== '\r')) {
412 if (fputc (c
, f
) < 0)
414 } else { /* (c == '\n' || c == '\r') */
415 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
418 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
419 /* put one line break unconditionally */
420 if (fputc ('\n', f
) < 0)
423 i
++; /* 2 chars are processed */
425 if (c
== '\r' && c1
== '\n')
426 /* Windows line break; go to the next char */
429 if (c
== '\r' && c1
== '\r') {
430 /* two Macintosh line breaks; put second line break */
431 if (fputc ('\n', f
) < 0)
436 if (fputc (c1
, f
) < 0)
440 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
441 /* put one line break unconditionally */
442 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
445 if (c
== '\r' && c1
== '\n')
446 /* Windows line break; go to the next char */
450 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
451 /* put one line break unconditionally */
452 if (fputc ('\r', f
) < 0)
455 i
++; /* 2 chars are processed */
457 if (c
== '\r' && c1
== '\n')
458 /* Windows line break; go to the next char */
461 if (c
== '\n' && c1
== '\n') {
462 /* two Windows line breaks; put second line break */
463 if (fputc ('\r', f
) < 0)
468 if (fputc (c1
, f
) < 0)
471 case LB_ASIS
: /* default without changes */
477 return edit
->last_byte
;
480 #define TEMP_BUF_LEN 1024
482 /* inserts a file at the cursor, returns 1 on success */
484 edit_insert_file (WEdit
*edit
, const char *filename
)
487 if ((p
= edit_get_filter (filename
))) {
489 long current
= edit
->curs1
;
490 f
= (FILE *) popen (p
, "r");
492 edit_insert_stream (edit
, f
);
493 edit_cursor_move (edit
, current
- edit
->curs1
);
494 if (pclose (f
) > 0) {
495 GString
*errmsg
= g_string_new (NULL
);
496 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
497 edit_error_dialog (_("Error"), errmsg
->str
);
498 g_string_free (errmsg
, TRUE
);
503 GString
*errmsg
= g_string_new (NULL
);
504 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
505 edit_error_dialog (_("Error"), errmsg
->str
);
506 g_string_free (errmsg
, TRUE
);
512 int i
, file
, blocklen
;
513 long current
= edit
->curs1
;
514 int vertical_insertion
= 0;
516 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
518 buf
= g_malloc0 (TEMP_BUF_LEN
);
519 blocklen
= mc_read (file
, buf
, sizeof(VERTICAL_MAGIC
));
521 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
522 if ( memcmp(buf
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
)) == 0 ) {
523 vertical_insertion
= 1;
525 mc_lseek (file
, 0, SEEK_SET
);
528 if (vertical_insertion
) {
529 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
531 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
532 for (i
= 0; i
< blocklen
; i
++)
533 edit_insert (edit
, buf
[i
]);
536 edit_cursor_move (edit
, current
- edit
->curs1
);
545 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
547 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
550 GString
*errmsg
= (GString
*) 0;
552 /* Try opening an existing file */
553 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
557 * Try creating the file. O_EXCL prevents following broken links
558 * and opening existing files.
562 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
565 g_string_sprintf (errmsg
= g_string_new (NULL
),
566 _(" Cannot open %s for reading "), filename
);
569 /* New file, delete it if it's not modified or saved */
570 edit
->delete_file
= 1;
574 /* Check what we have opened */
575 if (mc_fstat (file
, st
) < 0) {
576 g_string_sprintf (errmsg
= g_string_new (NULL
),
577 _(" Cannot get size/permissions for %s "), filename
);
581 /* We want to open regular files only */
582 if (!S_ISREG (st
->st_mode
)) {
583 g_string_sprintf (errmsg
= g_string_new (NULL
),
584 _(" %s is not a regular file "), filename
);
589 * Don't delete non-empty files.
590 * O_EXCL should prevent it, but let's be on the safe side.
592 if (st
->st_size
> 0) {
593 edit
->delete_file
= 0;
596 if (st
->st_size
>= SIZE_LIMIT
) {
597 g_string_sprintf (errmsg
= g_string_new (NULL
),
598 _(" File %s is too large "), filename
);
602 (void) mc_close (file
);
604 edit_error_dialog (_("Error"), errmsg
->str
);
605 g_string_free (errmsg
, TRUE
);
612 * Open the file and load it into the buffers, either directly or using
613 * a filter. Return 0 on success, 1 on error.
615 * Fast loading (edit_load_file_fast) is used when the file size is
616 * known. In this case the data is read into the buffers by blocks.
617 * If the file size is not known, the data is loaded byte by byte in
621 edit_load_file (WEdit
*edit
)
625 /* Cannot do fast load if a filter is used */
626 if (edit_find_filter (edit
->filename
) >= 0)
630 * VFS may report file size incorrectly, and slow load is not a big
631 * deal considering overhead in VFS.
633 if (!vfs_file_is_local (edit
->filename
))
637 * FIXME: line end translation should disable fast loading as well
638 * Consider doing fseek() to the end and ftell() for the real size.
641 if (*edit
->filename
) {
642 /* If we are dealing with a real file, check that it exists */
643 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
646 /* nothing to load */
650 edit_init_buffers (edit
);
653 edit
->last_byte
= edit
->stat1
.st_size
;
654 edit_load_file_fast (edit
, edit
->filename
);
655 /* If fast load was used, the number of lines wasn't calculated */
656 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
659 const char *codepage_id
;
662 if (*edit
->filename
) {
663 edit
->stack_disable
= 1;
664 if (!edit_insert_file (edit
, edit
->filename
)) {
668 edit
->stack_disable
= 0;
672 codepage_id
= get_codepage_id( source_codepage
);
674 edit
->utf8
= str_isutf8 ( codepage_id
);
681 /* Restore saved cursor position in the file */
683 edit_load_position (WEdit
*edit
)
689 if (!edit
->filename
|| !*edit
->filename
)
692 filename
= vfs_canon (edit
->filename
);
693 load_file_position (filename
, &line
, &column
, &offset
);
697 edit_move_to_line (edit
, line
- 1);
698 edit
->prev_col
= column
;
699 } else if (offset
> 0) {
700 edit_cursor_move (edit
, offset
);
701 line
= edit
->curs_line
;
703 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
704 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
707 /* Save cursor position in the file */
709 edit_save_position (WEdit
*edit
)
713 if (!edit
->filename
|| !*edit
->filename
)
716 filename
= vfs_canon (edit
->filename
);
717 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
721 /* Clean the WEdit stricture except the widget part */
723 edit_purge_widget (WEdit
*edit
)
725 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
726 char *start
= (char *) edit
+ sizeof (Widget
);
727 memset (start
, 0, len
);
728 edit
->macro_i
= -1; /* not recording a macro */
732 edit_set_keymap (void)
734 editor_map
= default_editor_keymap
;
735 if (editor_keymap
&& editor_keymap
->len
> 0)
736 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
738 editor_x_map
= default_editor_x_keymap
;
739 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
740 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
744 #define space_width 1
747 * Fill in the edit structure. Return NULL on failure. Pass edit as
748 * NULL to allocate a new structure.
750 * If line is 0, try to restore saved position. Otherwise put the
751 * cursor on that line and show it in the middle of the screen.
754 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
758 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
759 if ( option_line_state
) {
760 option_line_state_width
= LINE_STATE_WIDTH
;
762 option_line_state_width
= 0;
767 * Expand option_whole_chars_search by national letters using
771 static char option_whole_chars_search_buf
[256];
773 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
775 size_t len
= str_term_width1 (option_whole_chars_search
);
777 strcpy (option_whole_chars_search_buf
,
778 option_whole_chars_search
);
780 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
781 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
782 option_whole_chars_search_buf
[len
++] = i
;
786 option_whole_chars_search_buf
[len
] = 0;
787 option_whole_chars_search
= option_whole_chars_search_buf
;
789 #endif /* ENABLE_NLS */
790 edit
= g_malloc0 (sizeof (WEdit
));
794 edit_purge_widget (edit
);
795 edit
->num_widget_lines
= lines
;
797 edit
->num_widget_columns
= columns
;
798 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
799 edit
->stat1
.st_uid
= getuid ();
800 edit
->stat1
.st_gid
= getgid ();
801 edit
->stat1
.st_mtime
= 0;
803 edit
->force
|= REDRAW_PAGE
;
804 edit_set_filename (edit
, filename
);
805 edit
->stack_size
= START_STACK_SIZE
;
806 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
807 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
808 if (edit_load_file (edit
)) {
809 /* edit_load_file already gives an error message */
815 edit
->converter
= str_cnv_from_term
;
818 const char *cp_id
= NULL
;
819 cp_id
= get_codepage_id (source_codepage
>= 0 ?
820 source_codepage
: display_codepage
);
824 conv
= str_crt_conv_from (cp_id
);
825 if (conv
!= INVALID_CONV
) {
826 if (edit
->converter
!= str_cnv_from_term
)
827 str_close_conv (edit
->converter
);
828 edit
->converter
= conv
;
832 edit
->utf8
= str_isutf8 (cp_id
);
836 edit
->loading_done
= 1;
839 edit_load_syntax (edit
, 0, 0);
842 edit_get_syntax_color (edit
, -1, &color
);
845 /* load saved cursor position */
846 if ((line
== 0) && option_save_position
) {
847 edit_load_position (edit
);
851 edit_move_display (edit
, line
- 1);
852 edit_move_to_line (edit
, line
- 1);
860 /* Clear the edit struct, freeing everything in it. Return 1 on success */
862 edit_clean (WEdit
*edit
)
869 /* a stale lock, remove it */
871 edit
->locked
= edit_unlock_file (edit
->filename
);
873 /* save cursor position */
874 if (option_save_position
)
875 edit_save_position (edit
);
877 /* File specified on the mcedit command line and never saved */
878 if (edit
->delete_file
)
879 unlink (edit
->filename
);
881 edit_free_syntax_rules (edit
);
882 book_mark_flush (edit
, -1);
883 for (; j
<= MAXBUFF
; j
++) {
884 g_free (edit
->buffers1
[j
]);
885 g_free (edit
->buffers2
[j
]);
888 g_free (edit
->undo_stack
);
889 g_free (edit
->filename
);
894 mc_search_free(edit
->search
);
897 edit_purge_widget (edit
);
902 /* returns 1 on success */
904 edit_renew (WEdit
* edit
)
906 int lines
= edit
->num_widget_lines
;
907 int columns
= edit
->num_widget_columns
;
910 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
914 * Load a new file into the editor. If it fails, preserve the old file.
915 * To do it, allocate a new widget, initialize it and, if the new file
916 * was loaded, copy the data to the old widget.
917 * Return 1 on success, 0 on failure.
920 edit_reload (WEdit
*edit
, const char *filename
)
923 int lines
= edit
->num_widget_lines
;
924 int columns
= edit
->num_widget_columns
;
926 e
= g_malloc0 (sizeof (WEdit
));
927 e
->widget
= edit
->widget
;
928 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
933 memcpy (edit
, e
, sizeof (WEdit
));
939 * Load a new file into the editor and set line. If it fails, preserve the old file.
940 * To do it, allocate a new widget, initialize it and, if the new file
941 * was loaded, copy the data to the old widget.
942 * Return 1 on success, 0 on failure.
945 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
948 int lines
= edit
->num_widget_lines
;
949 int columns
= edit
->num_widget_columns
;
951 e
= g_malloc0 (sizeof (WEdit
));
952 e
->widget
= edit
->widget
;
953 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
958 memcpy (edit
, e
, sizeof (WEdit
));
965 Recording stack for undo:
966 The following is an implementation of a compressed stack. Identical
967 pushes are recorded by a negative prefix indicating the number of times the
968 same char was pushed. This saves space for repeated curs-left or curs-right
985 If the stack long int is 0-255 it represents a normal insert (from a backspace),
986 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
987 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
988 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
991 The only way the cursor moves or the buffer is changed is through the routines:
992 insert, backspace, insert_ahead, delete, and cursor_move.
993 These record the reverse undo movements onto the stack each time they are
996 Each key press results in a set of actions (insert; delete ...). So each time
997 a key is pressed the current position of start_display is pushed as
998 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
999 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1000 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1004 void edit_push_action (WEdit
* edit
, long c
,...)
1006 unsigned long sp
= edit
->stack_pointer
;
1010 /* first enlarge the stack if necessary */
1011 if (sp
> edit
->stack_size
- 10) { /* say */
1012 if (option_max_undo
< 256)
1013 option_max_undo
= 256;
1014 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1015 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1017 edit
->undo_stack
= t
;
1018 edit
->stack_size
<<= 1;
1019 edit
->stack_size_mask
= edit
->stack_size
- 1;
1023 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1024 if (edit
->stack_disable
)
1027 #ifdef FAST_MOVE_CURSOR
1028 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1030 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1031 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1033 c
= -(va_arg (ap
, int));
1036 #endif /* ! FAST_MOVE_CURSOR */
1037 if (edit
->stack_bottom
!= sp
1038 && spm1
!= edit
->stack_bottom
1039 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1041 if (edit
->undo_stack
[spm1
] < 0) {
1042 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1044 if (edit
->undo_stack
[spm1
] > -1000000000) {
1045 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1046 edit
->undo_stack
[spm1
]--;
1050 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1051 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1052 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1053 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1054 if (edit
->undo_stack
[spm1
] == -2)
1055 edit
->stack_pointer
= spm1
;
1057 edit
->undo_stack
[spm1
]++;
1062 d
= edit
->undo_stack
[spm1
];
1065 return; /* --> no need to push multiple do-nothings */
1066 edit
->undo_stack
[sp
] = -2;
1069 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1070 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1071 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1072 edit
->stack_pointer
= spm1
;
1078 edit
->undo_stack
[sp
] = c
;
1081 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1083 /* if the sp wraps round and catches the stack_bottom then erase
1084 * the first set of actions on the stack to make space - by moving
1085 * stack_bottom forward one "key press" */
1086 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1087 if ((unsigned long) c
== edit
->stack_bottom
||
1088 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1090 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1091 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1093 /*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: */
1094 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1095 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1099 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1100 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1103 pop_action (WEdit
* edit
)
1106 unsigned long sp
= edit
->stack_pointer
;
1107 if (sp
== edit
->stack_bottom
) {
1108 return STACK_BOTTOM
;
1110 sp
= (sp
- 1) & edit
->stack_size_mask
;
1111 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1112 /* edit->undo_stack[sp] = '@'; */
1113 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1116 if (sp
== edit
->stack_bottom
) {
1117 return STACK_BOTTOM
;
1119 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1120 if (edit
->undo_stack
[sp
] == -2) {
1121 /* edit->undo_stack[sp] = '@'; */
1122 edit
->stack_pointer
= sp
;
1124 edit
->undo_stack
[sp
]++;
1129 /* is called whenever a modification is made by one of the four routines below */
1130 static void edit_modification (WEdit
* edit
)
1132 edit
->caches_valid
= 0;
1133 edit
->screen_modified
= 1;
1135 /* raise lock when file modified */
1136 if (!edit
->modified
&& !edit
->delete_file
)
1137 edit
->locked
= edit_lock_file (edit
->filename
);
1142 Basic low level single character buffer alterations and movements at the cursor.
1143 Returns char passed over, inserted or removed.
1147 edit_insert (WEdit
*edit
, int c
)
1149 /* check if file has grown to large */
1150 if (edit
->last_byte
>= SIZE_LIMIT
)
1153 /* first we must update the position of the display window */
1154 if (edit
->curs1
< edit
->start_display
) {
1155 edit
->start_display
++;
1160 /* Mark file as modified, unless the file hasn't been fully loaded */
1161 if (edit
->loading_done
) {
1162 edit_modification (edit
);
1165 /* now we must update some info on the file and check if a redraw is required */
1167 if (edit
->book_mark
)
1168 book_mark_inc (edit
, edit
->curs_line
);
1170 edit
->total_lines
++;
1171 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1174 /* save the reverse command onto the undo stack */
1175 edit_push_action (edit
, BACKSPACE
);
1177 /* update markers */
1178 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1179 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1180 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1182 /* add a new buffer if we've reached the end of the last one */
1183 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1184 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1185 g_malloc0 (EDIT_BUF_SIZE
);
1187 /* perform the insertion */
1188 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1189 curs1
& M_EDIT_BUF_SIZE
]
1190 = (unsigned char) c
;
1192 /* update file length */
1195 /* update cursor position */
1200 edit_insert_over (WEdit
* edit
)
1204 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1205 edit_insert (edit
, ' ');
1210 /* same as edit_insert and move left */
1211 void edit_insert_ahead (WEdit
* edit
, int c
)
1213 if (edit
->last_byte
>= SIZE_LIMIT
)
1215 if (edit
->curs1
< edit
->start_display
) {
1216 edit
->start_display
++;
1220 edit_modification (edit
);
1222 if (edit
->book_mark
)
1223 book_mark_inc (edit
, edit
->curs_line
);
1224 edit
->total_lines
++;
1225 edit
->force
|= REDRAW_AFTER_CURSOR
;
1227 edit_push_action (edit
, DELCHAR
);
1229 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1230 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1231 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1233 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1234 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1235 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1242 int edit_delete (WEdit
* edit
, const int byte_delete
)
1251 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1252 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1253 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1256 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1257 if ( edit
->utf8
&& byte_delete
== 0 ) {
1258 edit_get_utf (edit
, edit
->curs1
, &cw
);
1262 for ( i
= 1; i
<= cw
; i
++ ) {
1263 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1265 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1266 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1267 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1271 edit_push_action (edit
, p
+ 256);
1274 edit_modification (edit
);
1276 if (edit
->book_mark
)
1277 book_mark_dec (edit
, edit
->curs_line
);
1278 edit
->total_lines
--;
1279 edit
->force
|= REDRAW_AFTER_CURSOR
;
1281 if (edit
->curs1
< edit
->start_display
) {
1282 edit
->start_display
--;
1292 edit_backspace (WEdit
* edit
, const int byte_delete
)
1301 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1302 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1303 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1306 if ( edit
->utf8
&& byte_delete
== 0 ) {
1307 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1311 for ( i
= 1; i
<= cw
; i
++ ) {
1312 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1313 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1314 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1315 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1319 edit_push_action (edit
, p
);
1321 edit_modification (edit
);
1323 if (edit
->book_mark
)
1324 book_mark_dec (edit
, edit
->curs_line
);
1326 edit
->total_lines
--;
1327 edit
->force
|= REDRAW_AFTER_CURSOR
;
1330 if (edit
->curs1
< edit
->start_display
) {
1331 edit
->start_display
--;
1339 #ifdef FAST_MOVE_CURSOR
1341 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1344 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1346 next
-= (unsigned long) dest
;
1354 edit_move_backward_lots (WEdit
*edit
, long increment
)
1357 unsigned char *p
= NULL
;
1359 if (increment
> edit
->curs1
)
1360 increment
= edit
->curs1
;
1363 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1365 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1368 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1372 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1373 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1378 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1379 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1380 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1381 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1384 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1385 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1386 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1391 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1393 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1395 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1396 g_malloc0 (EDIT_BUF_SIZE
);
1401 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1411 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1413 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1417 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1418 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1421 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1423 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1424 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1429 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1431 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1433 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1434 g_malloc0 (EDIT_BUF_SIZE
);
1439 return edit_get_byte (edit
, edit
->curs1
);
1442 #endif /* ! FAST_MOVE_CURSOR */
1444 /* moves the cursor right or left: increment positive or negative respectively */
1445 void edit_cursor_move (WEdit
* edit
, long increment
)
1447 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1449 #ifdef FAST_MOVE_CURSOR
1450 if (increment
< -256) {
1451 edit
->force
|= REDRAW_PAGE
;
1452 edit_move_backward_lots (edit
, -increment
);
1455 #endif /* ! FAST_MOVE_CURSOR */
1457 if (increment
< 0) {
1458 for (; increment
< 0; increment
++) {
1462 edit_push_action (edit
, CURS_RIGHT
);
1464 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1465 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1466 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1467 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1469 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1470 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1471 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1472 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1477 edit
->force
|= REDRAW_LINE_BELOW
;
1481 } else if (increment
> 0) {
1482 for (; increment
> 0; increment
--) {
1486 edit_push_action (edit
, CURS_LEFT
);
1488 c
= edit_get_byte (edit
, edit
->curs1
);
1489 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1490 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1491 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1493 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1494 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1495 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1496 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1501 edit
->force
|= REDRAW_LINE_ABOVE
;
1507 /* These functions return positions relative to lines */
1509 /* returns index of last char on line + 1 */
1510 long edit_eol (WEdit
* edit
, long current
)
1512 if (current
< edit
->last_byte
) {
1514 if (edit_get_byte (edit
, current
) == '\n')
1517 return edit
->last_byte
;
1521 /* returns index of first char on line */
1522 long edit_bol (WEdit
* edit
, long current
)
1526 if (edit_get_byte (edit
, current
- 1) == '\n')
1534 long edit_count_lines (WEdit
* edit
, long current
, long upto
)
1537 if (upto
> edit
->last_byte
)
1538 upto
= edit
->last_byte
;
1541 while (current
< upto
)
1542 if (edit_get_byte (edit
, current
++) == '\n')
1548 /* If lines is zero this returns the count of lines from current to upto. */
1549 /* If upto is zero returns index of lines forward current. */
1550 long edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1553 return edit_count_lines (edit
, current
, upto
);
1559 next
= edit_eol (edit
, current
) + 1;
1560 if (next
> edit
->last_byte
)
1570 /* Returns offset of 'lines' lines up from current */
1571 long edit_move_backward (WEdit
* edit
, long current
, long lines
)
1575 current
= edit_bol (edit
, current
);
1576 while((lines
--) && current
!= 0)
1577 current
= edit_bol (edit
, current
- 1);
1581 /* If cols is zero this returns the count of columns from current to upto. */
1582 /* If upto is zero returns index of cols across from current. */
1584 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1593 q
= edit
->last_byte
+ 2;
1595 for (col
= 0, p
= current
; p
< q
; p
++) {
1607 orig_c
= c
= edit_get_byte (edit
, p
);
1610 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1614 if (g_unichar_iswide (utf_ch
))
1616 } else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1620 c
= convert_to_display_c (c
);
1622 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1623 else if (c
== '\n') {
1628 } else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1629 /* '\r' is shown as ^M, so we must advance 2 characters */
1630 /* Caret notation for control characters */
1638 /* returns the current column position of the cursor */
1639 int edit_get_col (WEdit
* edit
)
1641 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1645 /* Scrolling functions */
1647 void edit_update_curs_row (WEdit
* edit
)
1649 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1652 void edit_update_curs_col (WEdit
* edit
)
1654 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1658 edit_get_curs_col (const WEdit
*edit
)
1660 return edit
->curs_col
;
1663 /*moves the display start position up by i lines */
1664 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1666 unsigned long lines_above
= edit
->start_line
;
1667 if (i
> lines_above
)
1670 edit
->start_line
-= i
;
1671 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1672 edit
->force
|= REDRAW_PAGE
;
1673 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1675 edit_update_curs_row (edit
);
1679 /* returns 1 if could scroll, 0 otherwise */
1680 void edit_scroll_downward (WEdit
* edit
, int i
)
1683 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1684 if (lines_below
> 0) {
1685 if (i
> lines_below
)
1687 edit
->start_line
+= i
;
1688 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1689 edit
->force
|= REDRAW_PAGE
;
1690 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1692 edit_update_curs_row (edit
);
1695 void edit_scroll_right (WEdit
* edit
, int i
)
1697 edit
->force
|= REDRAW_PAGE
;
1698 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1699 edit
->start_col
-= i
;
1702 void edit_scroll_left (WEdit
* edit
, int i
)
1704 if (edit
->start_col
) {
1705 edit
->start_col
+= i
;
1706 if (edit
->start_col
> 0)
1707 edit
->start_col
= 0;
1708 edit
->force
|= REDRAW_PAGE
;
1709 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1713 /* high level cursor movement commands */
1715 static int is_in_indent (WEdit
*edit
)
1717 long p
= edit_bol (edit
, edit
->curs1
);
1718 while (p
< edit
->curs1
)
1719 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1724 static int left_of_four_spaces (WEdit
*edit
);
1727 edit_move_to_prev_col (WEdit
* edit
, long p
)
1729 int prev
= edit
->prev_col
;
1730 int over
= edit
->over_col
;
1731 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1733 if (option_cursor_beyond_eol
) {
1734 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1736 if (line_len
< prev
+ edit
->over_col
) {
1737 edit
->over_col
= prev
+ over
- line_len
;
1738 edit
->prev_col
= line_len
;
1739 edit
->curs_col
= line_len
;
1741 edit
->curs_col
= prev
+ over
;
1742 edit
->prev_col
= edit
->curs_col
;
1747 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1748 edit_update_curs_col (edit
);
1750 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1751 int q
= edit
->curs_col
;
1752 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1753 p
= edit_bol (edit
, edit
->curs1
);
1754 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1755 if (!left_of_four_spaces (edit
))
1756 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1763 is_blank (WEdit
*edit
, long offset
)
1767 s
= edit_bol (edit
, offset
);
1768 f
= edit_eol (edit
, offset
) - 1;
1770 c
= edit_get_byte (edit
, s
++);
1778 /* returns the offset of line i */
1780 edit_find_line (WEdit
*edit
, int line
)
1784 if (!edit
->caches_valid
) {
1785 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1786 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1787 /* three offsets that we *know* are line 0 at 0 and these two: */
1788 edit
->line_numbers
[1] = edit
->curs_line
;
1789 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1790 edit
->line_numbers
[2] = edit
->total_lines
;
1791 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1792 edit
->caches_valid
= 1;
1794 if (line
>= edit
->total_lines
)
1795 return edit
->line_offsets
[2];
1798 /* find the closest known point */
1799 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1801 n
= abs (edit
->line_numbers
[i
] - line
);
1808 return edit
->line_offsets
[j
]; /* know the offset exactly */
1809 if (m
== 1 && j
>= 3)
1810 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1812 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1813 if (line
> edit
->line_numbers
[j
])
1814 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1816 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1817 edit
->line_numbers
[i
] = line
;
1818 return edit
->line_offsets
[i
];
1821 int line_is_blank (WEdit
* edit
, long line
)
1823 return is_blank (edit
, edit_find_line (edit
, line
));
1826 /* moves up until a blank line is reached, or until just
1827 before a non-blank line is reached */
1828 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1831 if (edit
->curs_line
> 1) {
1832 if (line_is_blank (edit
, edit
->curs_line
)) {
1833 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1834 for (i
= edit
->curs_line
- 1; i
; i
--)
1835 if (!line_is_blank (edit
, i
)) {
1840 for (i
= edit
->curs_line
- 1; i
; i
--)
1841 if (line_is_blank (edit
, i
))
1845 for (i
= edit
->curs_line
- 1; i
; i
--)
1846 if (line_is_blank (edit
, i
))
1850 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1853 /* moves down until a blank line is reached, or until just
1854 before a non-blank line is reached */
1855 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1858 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1859 i
= edit
->total_lines
;
1861 if (line_is_blank (edit
, edit
->curs_line
)) {
1862 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1863 for (i
= edit
->curs_line
+ 1; i
; i
++)
1864 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1869 for (i
= edit
->curs_line
+ 1; i
; i
++)
1870 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1874 for (i
= edit
->curs_line
+ 1; i
; i
++)
1875 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1879 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1882 static void edit_begin_page (WEdit
*edit
)
1884 edit_update_curs_row (edit
);
1885 edit_move_up (edit
, edit
->curs_row
, 0);
1888 static void edit_end_page (WEdit
*edit
)
1890 edit_update_curs_row (edit
);
1891 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1895 /* goto beginning of text */
1896 static void edit_move_to_top (WEdit
* edit
)
1898 if (edit
->curs_line
) {
1899 edit_cursor_move (edit
, -edit
->curs1
);
1900 edit_move_to_prev_col (edit
, 0);
1901 edit
->force
|= REDRAW_PAGE
;
1902 edit
->search_start
= 0;
1903 edit_update_curs_row(edit
);
1908 /* goto end of text */
1909 static void edit_move_to_bottom (WEdit
* edit
)
1911 if (edit
->curs_line
< edit
->total_lines
) {
1912 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1913 edit
->start_display
= edit
->last_byte
;
1914 edit
->start_line
= edit
->total_lines
;
1915 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1916 edit
->force
|= REDRAW_PAGE
;
1920 /* goto beginning of line */
1921 static void edit_cursor_to_bol (WEdit
* edit
)
1923 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1924 edit
->search_start
= edit
->curs1
;
1925 edit
->prev_col
= edit_get_col (edit
);
1929 /* goto end of line */
1930 static void edit_cursor_to_eol (WEdit
* edit
)
1932 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1933 edit
->search_start
= edit
->curs1
;
1934 edit
->prev_col
= edit_get_col (edit
);
1938 /* move cursor to line 'line' */
1939 void edit_move_to_line (WEdit
* e
, long line
)
1941 if(line
< e
->curs_line
)
1942 edit_move_up (e
, e
->curs_line
- line
, 0);
1944 edit_move_down (e
, line
- e
->curs_line
, 0);
1945 edit_scroll_screen_over_cursor (e
);
1948 /* scroll window so that first visible line is 'line' */
1949 void edit_move_display (WEdit
* e
, long line
)
1951 if(line
< e
->start_line
)
1952 edit_scroll_upward (e
, e
->start_line
- line
);
1954 edit_scroll_downward (e
, line
- e
->start_line
);
1957 /* save markers onto undo stack */
1958 void edit_push_markers (WEdit
* edit
)
1960 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1961 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1964 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1973 /* highlight marker toggle */
1974 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1976 edit_push_markers (edit
);
1978 edit_set_markers (edit
, 0, 0, 0, 0);
1979 edit
->force
|= REDRAW_PAGE
;
1981 if (edit
->mark2
>= 0) {
1982 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
1983 edit
->force
|= REDRAW_PAGE
;
1985 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
1989 static unsigned long
1994 const char option_chars_move_whole_word
[] =
1995 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2000 if (*option_chars_move_whole_word
== '!')
2002 return 0x80000000UL
;
2004 if (g_ascii_isupper ((gchar
) c
))
2006 else if (g_ascii_islower ((gchar
) c
))
2008 else if (g_ascii_isalpha (c
))
2010 else if (isdigit (c
))
2012 else if (isspace (c
))
2014 q
= strchr (option_chars_move_whole_word
, c
);
2016 return 0xFFFFFFFFUL
;
2018 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2022 } while ((q
= strchr (q
+ 1, c
)));
2027 edit_left_word_move (WEdit
*edit
, int s
)
2031 if (column_highlighting
2032 && edit
->mark1
!= edit
->mark2
2033 && edit
->over_col
== 0
2034 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2036 edit_cursor_move (edit
, -1);
2039 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2040 c2
= edit_get_byte (edit
, edit
->curs1
);
2041 if (!(my_type_of (c1
) & my_type_of (c2
)))
2043 if (isspace (c1
) && !isspace (c2
))
2046 if (!isspace (c1
) && isspace (c2
))
2051 static void edit_left_word_move_cmd (WEdit
* edit
)
2053 edit_left_word_move (edit
, 0);
2054 edit
->force
|= REDRAW_PAGE
;
2058 edit_right_word_move (WEdit
*edit
, int s
)
2062 if (column_highlighting
2063 && edit
->mark1
!= edit
->mark2
2064 && edit
->over_col
== 0
2065 && edit
->curs1
== edit_eol(edit
, edit
->curs1
)
2068 edit_cursor_move (edit
, 1);
2069 if (edit
->curs1
>= edit
->last_byte
)
2071 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2072 c2
= edit_get_byte (edit
, edit
->curs1
);
2073 if (!(my_type_of (c1
) & my_type_of (c2
)))
2075 if (isspace (c1
) && !isspace (c2
))
2078 if (!isspace (c1
) && isspace (c2
))
2083 static void edit_right_word_move_cmd (WEdit
* edit
)
2085 edit_right_word_move (edit
, 0);
2086 edit
->force
|= REDRAW_PAGE
;
2089 static void edit_right_char_move_cmd (WEdit
* edit
)
2094 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2098 c
= edit_get_byte (edit
, edit
->curs1
);
2100 if (option_cursor_beyond_eol
&& c
== '\n') {
2103 edit_cursor_move (edit
, cw
);
2107 static void edit_left_char_move_cmd (WEdit
* edit
)
2110 if (column_highlighting
2111 && edit
->mark1
!= edit
->mark2
2112 && edit
->over_col
== 0
2113 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2116 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2120 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2123 edit_cursor_move (edit
, -cw
);
2127 /** Up or down cursor moving.
2128 direction = TRUE - move up
2132 edit_move_updown (WEdit
* edit
, unsigned long i
, int scroll
, gboolean direction
)
2135 unsigned long l
= (direction
)
2137 : edit
->total_lines
- edit
->curs_line
;
2146 edit
->force
|= REDRAW_PAGE
;
2149 edit_scroll_upward (edit
, i
);
2151 edit_scroll_downward (edit
, i
);
2153 p
= edit_bol (edit
, edit
->curs1
);
2156 ? edit_move_backward (edit
, p
, i
)
2157 : edit_move_forward (edit
, p
, i
, 0);
2159 edit_cursor_move (edit
, p
- edit
->curs1
);
2161 edit_move_to_prev_col (edit
, p
);
2163 /* search start of current multibyte char (like CJK) */
2164 if (edit
->curs1
+ 1 < edit
->last_byte
) {
2165 edit_right_char_move_cmd (edit
);
2166 edit_left_char_move_cmd (edit
);
2169 edit
->search_start
= edit
->curs1
;
2170 edit
->found_len
= 0;
2173 static void edit_right_delete_word (WEdit
* edit
)
2177 if (edit
->curs1
>= edit
->last_byte
)
2179 c1
= edit_delete (edit
, 1);
2180 c2
= edit_get_byte (edit
, edit
->curs1
);
2181 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2183 if (!(my_type_of (c1
) & my_type_of (c2
)))
2188 static void edit_left_delete_word (WEdit
* edit
)
2192 if (edit
->curs1
<= 0)
2194 c1
= edit_backspace (edit
, 1);
2195 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2196 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2198 if (!(my_type_of (c1
) & my_type_of (c2
)))
2204 the start column position is not recorded, and hence does not
2205 undo as it happed. But who would notice.
2208 edit_do_undo (WEdit
* edit
)
2213 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2215 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2220 edit_cursor_move (edit
, 1);
2223 edit_cursor_move (edit
, -1);
2226 edit_backspace (edit
, 1);
2229 edit_delete (edit
, 1);
2232 column_highlighting
= 1;
2235 column_highlighting
= 0;
2238 if (ac
>= 256 && ac
< 512)
2239 edit_insert_ahead (edit
, ac
- 256);
2240 if (ac
>= 0 && ac
< 256)
2241 edit_insert (edit
, ac
);
2243 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2244 edit
->mark1
= ac
- MARK_1
;
2245 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2246 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2247 edit
->mark2
= ac
- MARK_2
;
2248 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2251 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2254 if (edit
->start_display
> ac
- KEY_PRESS
) {
2255 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2256 edit
->force
|= REDRAW_PAGE
;
2257 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2258 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2259 edit
->force
|= REDRAW_PAGE
;
2261 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2262 edit_update_curs_row (edit
);
2265 edit
->stack_disable
= 0;
2268 static void edit_delete_to_line_end (WEdit
* edit
)
2270 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2273 edit_delete (edit
, 1);
2277 static void edit_delete_to_line_begin (WEdit
* edit
)
2279 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2282 edit_backspace (edit
, 1);
2287 edit_delete_line (WEdit
*edit
)
2290 * Delete right part of the line.
2291 * Note that edit_get_byte() returns '\n' when byte position is
2294 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2295 (void) edit_delete (edit
, 1);
2300 * Note that edit_delete() will not corrupt anything if called while
2301 * cursor position is EOF.
2303 (void) edit_delete (edit
, 1);
2306 * Delete left part of the line.
2307 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2309 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2310 (void) edit_backspace (edit
, 1);
2314 void insert_spaces_tab (WEdit
* edit
, int half
)
2317 edit_update_curs_col (edit
);
2318 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2320 edit_insert (edit
, ' ');
2325 static int is_aligned_on_a_tab (WEdit
* edit
)
2327 edit_update_curs_col (edit
);
2328 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2329 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2332 static int right_of_four_spaces (WEdit
*edit
)
2335 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2336 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2338 return is_aligned_on_a_tab (edit
);
2342 static int left_of_four_spaces (WEdit
*edit
)
2345 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2346 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2348 return is_aligned_on_a_tab (edit
);
2352 int edit_indent_width (WEdit
* edit
, long p
)
2355 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2357 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2360 void edit_insert_indent (WEdit
* edit
, int indent
)
2362 if (!option_fill_tabs_with_spaces
) {
2363 while (indent
>= TAB_SIZE
) {
2364 edit_insert (edit
, '\t');
2368 while (indent
-- > 0)
2369 edit_insert (edit
, ' ');
2373 edit_auto_indent (WEdit
* edit
)
2378 /* use the previous line as a template */
2379 p
= edit_move_backward (edit
, p
, 1);
2380 /* copy the leading whitespace of the line */
2381 for (;;) { /* no range check - the line _is_ \n-terminated */
2382 c
= edit_get_byte (edit
, p
++);
2383 if (c
!= ' ' && c
!= '\t')
2385 edit_insert (edit
, c
);
2390 edit_double_newline (WEdit
* edit
)
2392 edit_insert (edit
, '\n');
2393 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2395 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2397 edit
->force
|= REDRAW_PAGE
;
2398 edit_insert (edit
, '\n');
2402 edit_tab_cmd (WEdit
* edit
)
2406 if (option_fake_half_tabs
) {
2407 if (is_in_indent (edit
)) {
2408 /*insert a half tab (usually four spaces) unless there is a
2409 half tab already behind, then delete it and insert a
2411 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2412 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2413 edit_backspace (edit
, 1);
2414 edit_insert (edit
, '\t');
2416 insert_spaces_tab (edit
, 1);
2421 if (option_fill_tabs_with_spaces
) {
2422 insert_spaces_tab (edit
, 0);
2424 edit_insert (edit
, '\t');
2428 static void check_and_wrap_line (WEdit
* edit
)
2431 if (!option_typewriter_wrap
)
2433 edit_update_curs_col (edit
);
2434 if (edit
->curs_col
< option_word_wrap_line_length
)
2439 c
= edit_get_byte (edit
, curs
);
2440 if (c
== '\n' || curs
<= 0) {
2441 edit_insert (edit
, '\n');
2444 if (c
== ' ' || c
== '\t') {
2445 int current
= edit
->curs1
;
2446 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2447 edit_insert (edit
, '\n');
2448 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2454 static inline void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2456 void edit_push_key_press (WEdit
* edit
)
2458 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2459 if (edit
->mark2
== -1)
2460 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2463 /* this find the matching bracket in either direction, and sets edit->bracket */
2464 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2466 const char * const b
= "{}{[][()(", *p
;
2467 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2468 unsigned long j
= 0;
2470 edit_update_curs_row (edit
);
2471 c
= edit_get_byte (edit
, edit
->curs1
);
2474 if (!furthest_bracket_search
)
2475 furthest_bracket_search
--;
2476 /* not on a bracket at all */
2479 /* the matching bracket */
2481 /* going left or right? */
2482 if (strchr ("{[(", c
))
2484 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2485 /* out of buffer? */
2486 if (q
>= edit
->last_byte
|| q
< 0)
2488 a
= edit_get_byte (edit
, q
);
2489 /* don't want to eat CPU */
2490 if (j
++ > furthest_bracket_search
)
2492 /* out of screen? */
2494 if (q
< edit
->start_display
)
2496 /* count lines if searching downward */
2497 if (inc
> 0 && a
== '\n')
2498 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2501 /* count bracket depth */
2502 i
+= (a
== c
) - (a
== d
);
2503 /* return if bracket depth is zero */
2511 static long last_bracket
= -1;
2513 void edit_find_bracket (WEdit
* edit
)
2515 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2516 if (last_bracket
!= edit
->bracket
)
2517 edit
->force
|= REDRAW_PAGE
;
2518 last_bracket
= edit
->bracket
;
2522 edit_goto_matching_bracket (WEdit
*edit
)
2526 q
= edit_get_bracket (edit
, 0, 0);
2528 edit
->bracket
= edit
->curs1
;
2529 edit
->force
|= REDRAW_PAGE
;
2530 edit_cursor_move (edit
, q
- edit
->curs1
);
2535 * This executes a command as though the user initiated it through a key
2536 * press. Callback with WIDGET_KEY as a message calls this after
2537 * translating the key press. This function can be used to pass any
2538 * command to the editor. Note that the screen wouldn't update
2539 * automatically. Either of command or char_for_insertion must be
2540 * passed as -1. Commands are executed, and char_for_insertion is
2541 * inserted at the cursor.
2544 edit_execute_key_command (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2546 if (command
== CK_Begin_Record_Macro
) {
2548 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2551 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2552 edit
->force
|= REDRAW_COMPLETELY
;
2553 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2557 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2558 edit
->macro
[edit
->macro_i
].command
= command
;
2559 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2561 /* record the beginning of a set of editing actions initiated by a key press */
2562 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2563 edit_push_key_press (edit
);
2565 edit_execute_cmd (edit
, command
, char_for_insertion
);
2566 if (column_highlighting
)
2567 edit
->force
|= REDRAW_PAGE
;
2570 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2573 This executes a command at a lower level than macro recording.
2574 It also does not push a key_press onto the undo stack. This means
2575 that if it is called many times, a single undo command will undo
2576 all of them. It also does not check for the Undo command.
2579 edit_execute_cmd (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2581 edit
->force
|= REDRAW_LINE
;
2583 /* The next key press will unhighlight the found string, so update
2585 if (edit
->found_len
|| column_highlighting
)
2586 edit
->force
|= REDRAW_PAGE
;
2588 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2589 column_highlighting
= 0;
2590 if (!edit
->highlight
2591 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2592 edit_mark_cmd (edit
, 1); /* clear */
2593 edit_mark_cmd (edit
, 0); /* marking on */
2595 edit
->highlight
= 1;
2596 } else { /* any other command */
2597 if (edit
->highlight
)
2598 edit_mark_cmd (edit
, 0); /* clear */
2599 edit
->highlight
= 0;
2602 /* first check for undo */
2603 if (command
== CK_Undo
) {
2604 edit_do_undo (edit
);
2605 edit
->found_len
= 0;
2606 edit
->prev_col
= edit_get_col (edit
);
2607 edit
->search_start
= edit
->curs1
;
2611 /* An ordinary key press */
2612 if (char_for_insertion
>= 0) {
2613 if (edit
->overwrite
) {
2614 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2615 edit_delete (edit
, 0);
2617 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2618 edit_insert_over (edit
);
2620 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2621 unsigned char str
[6 + 1];
2623 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2630 while ( str
[i
] != 0 && i
<=6) {
2631 char_for_insertion
= str
[i
];
2632 edit_insert (edit
, char_for_insertion
);
2637 edit_insert (edit
, char_for_insertion
);
2639 if (option_auto_para_formatting
) {
2640 format_paragraph (edit
, 0);
2641 edit
->force
|= REDRAW_PAGE
;
2643 check_and_wrap_line (edit
);
2644 edit
->found_len
= 0;
2645 edit
->prev_col
= edit_get_col (edit
);
2646 edit
->search_start
= edit
->curs1
;
2647 edit_find_bracket (edit
);
2654 case CK_Begin_Page_Highlight
:
2655 case CK_End_Page_Highlight
:
2662 if ( edit
->mark2
>= 0 ) {
2663 if ( !option_persistent_selections
) {
2664 if (column_highlighting
)
2665 edit_push_action (edit
, COLUMN_ON
);
2666 column_highlighting
= 0;
2667 edit_mark_cmd (edit
, 1);
2675 case CK_Begin_Page_Highlight
:
2676 case CK_End_Page_Highlight
:
2681 case CK_Word_Left_Highlight
:
2682 case CK_Word_Right_Highlight
:
2683 case CK_Up_Highlight
:
2684 case CK_Down_Highlight
:
2685 case CK_Up_Alt_Highlight
:
2686 case CK_Down_Alt_Highlight
:
2687 if (edit
->mark2
== -1)
2688 break; /*marking is following the cursor: may need to highlight a whole line */
2691 case CK_Left_Highlight
:
2692 case CK_Right_Highlight
:
2693 edit
->force
|= REDRAW_CHAR_ONLY
;
2696 /* basic cursor key commands */
2699 /* if non persistent selection and text selected */
2700 if ( !option_persistent_selections
) {
2701 if ( edit
->mark1
!= edit
->mark2
) {
2702 edit_block_delete_cmd (edit
);
2706 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2710 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2711 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2713 edit_backspace (edit
, 1);
2716 if (option_fake_half_tabs
) {
2718 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2719 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2720 edit_backspace (edit
, 1);
2725 edit_backspace (edit
, 0);
2728 /* if non persistent selection and text selected */
2729 if ( !option_persistent_selections
) {
2730 if ( edit
->mark1
!= edit
->mark2
) {
2731 edit_block_delete_cmd (edit
);
2736 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2737 edit_insert_over (edit
);
2739 if (option_fake_half_tabs
) {
2741 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2742 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2743 edit_delete (edit
, 1);
2747 edit_delete (edit
, 0);
2749 case CK_Delete_Word_Left
:
2751 edit_left_delete_word (edit
);
2753 case CK_Delete_Word_Right
:
2754 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2755 edit_insert_over (edit
);
2757 edit_right_delete_word (edit
);
2759 case CK_Delete_Line
:
2760 edit_delete_line (edit
);
2762 case CK_Delete_To_Line_End
:
2763 edit_delete_to_line_end (edit
);
2765 case CK_Delete_To_Line_Begin
:
2766 edit_delete_to_line_begin (edit
);
2770 if (option_auto_para_formatting
) {
2771 edit_double_newline (edit
);
2772 if (option_return_does_auto_indent
)
2773 edit_auto_indent (edit
);
2774 format_paragraph (edit
, 0);
2776 edit_insert (edit
, '\n');
2777 if (option_return_does_auto_indent
) {
2778 edit_auto_indent (edit
);
2783 edit_insert (edit
, '\n');
2786 case CK_Page_Up_Alt_Highlight
:
2787 column_highlighting
= 1;
2789 case CK_Page_Up_Highlight
:
2790 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2792 case CK_Page_Down_Alt_Highlight
:
2793 column_highlighting
= 1;
2795 case CK_Page_Down_Highlight
:
2796 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2798 case CK_Left_Alt_Highlight
:
2799 column_highlighting
= 1;
2801 case CK_Left_Highlight
:
2802 if (option_fake_half_tabs
) {
2803 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2804 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2807 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2808 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2812 edit_left_char_move_cmd (edit
);
2814 case CK_Right_Alt_Highlight
:
2815 column_highlighting
= 1;
2817 case CK_Right_Highlight
:
2818 if (option_fake_half_tabs
) {
2819 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2820 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2821 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2825 edit_right_char_move_cmd (edit
);
2828 case CK_Begin_Page_Highlight
:
2829 edit_begin_page (edit
);
2832 case CK_End_Page_Highlight
:
2833 edit_end_page (edit
);
2836 case CK_Word_Left_Highlight
:
2838 edit_left_word_move_cmd (edit
);
2841 case CK_Word_Right_Highlight
:
2843 edit_right_word_move_cmd (edit
);
2845 case CK_Up_Alt_Highlight
:
2846 column_highlighting
= 1;
2848 case CK_Up_Highlight
:
2849 edit_move_up (edit
, 1, 0);
2851 case CK_Down_Alt_Highlight
:
2852 column_highlighting
= 1;
2854 case CK_Down_Highlight
:
2855 edit_move_down (edit
, 1, 0);
2857 case CK_Paragraph_Up_Alt_Highlight
:
2858 column_highlighting
= 1;
2859 case CK_Paragraph_Up
:
2860 case CK_Paragraph_Up_Highlight
:
2861 edit_move_up_paragraph (edit
, 0);
2863 case CK_Paragraph_Down_Alt_Highlight
:
2864 column_highlighting
= 1;
2865 case CK_Paragraph_Down
:
2866 case CK_Paragraph_Down_Highlight
:
2867 edit_move_down_paragraph (edit
, 0);
2869 case CK_Scroll_Up_Alt_Highlight
:
2870 column_highlighting
= 1;
2872 case CK_Scroll_Up_Highlight
:
2873 edit_move_up (edit
, 1, 1);
2875 case CK_Scroll_Down_Alt_Highlight
:
2876 column_highlighting
= 1;
2877 case CK_Scroll_Down
:
2878 case CK_Scroll_Down_Highlight
:
2879 edit_move_down (edit
, 1, 1);
2882 case CK_Home_Highlight
:
2883 edit_cursor_to_bol (edit
);
2886 case CK_End_Highlight
:
2887 edit_cursor_to_eol (edit
);
2890 /* if text marked shift block */
2891 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2892 if (edit
->mark2
< 0)
2893 edit_mark_cmd (edit
, 0);
2894 edit_move_block_to_right (edit
);
2896 if ( option_cursor_beyond_eol
)
2897 edit_insert_over (edit
);
2898 edit_tab_cmd (edit
);
2899 if (option_auto_para_formatting
) {
2900 format_paragraph (edit
, 0);
2901 edit
->force
|= REDRAW_PAGE
;
2903 check_and_wrap_line (edit
);
2908 case CK_Toggle_Insert
:
2909 edit
->overwrite
= (edit
->overwrite
== 0);
2913 if (edit
->mark2
>= 0) {
2914 if (column_highlighting
)
2915 edit_push_action (edit
, COLUMN_ON
);
2916 column_highlighting
= 0;
2918 edit_mark_cmd (edit
, 0);
2920 case CK_Column_Mark
:
2921 if (!column_highlighting
)
2922 edit_push_action (edit
, COLUMN_OFF
);
2923 column_highlighting
= 1;
2924 edit_mark_cmd (edit
, 0);
2927 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
2928 edit
->force
|= REDRAW_PAGE
;
2931 if (column_highlighting
)
2932 edit_push_action (edit
, COLUMN_ON
);
2933 column_highlighting
= 0;
2934 edit_mark_cmd (edit
, 1);
2937 case CK_Toggle_Line_State
:
2938 option_line_state
= !option_line_state
;
2939 if ( option_line_state
) {
2940 option_line_state_width
= LINE_STATE_WIDTH
;
2942 option_line_state_width
= 0;
2944 edit
->force
|= REDRAW_PAGE
;
2947 case CK_Toggle_Show_Margin
:
2948 show_right_margin
= !show_right_margin
;
2949 edit
->force
|= REDRAW_PAGE
;
2952 case CK_Toggle_Bookmark
:
2953 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2954 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2955 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2957 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2959 case CK_Flush_Bookmarks
:
2960 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2961 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2962 edit
->force
|= REDRAW_PAGE
;
2964 case CK_Next_Bookmark
:
2965 if (edit
->book_mark
) {
2966 struct _book_mark
*p
;
2967 p
= (struct _book_mark
*) book_mark_find (edit
,
2971 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2972 || p
->line
< edit
->start_line
)
2973 edit_move_display (edit
,
2975 edit
->num_widget_lines
/ 2);
2976 edit_move_to_line (edit
, p
->line
);
2980 case CK_Prev_Bookmark
:
2981 if (edit
->book_mark
) {
2982 struct _book_mark
*p
;
2983 p
= (struct _book_mark
*) book_mark_find (edit
,
2985 while (p
->line
== edit
->curs_line
)
2989 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2990 || p
->line
< edit
->start_line
)
2991 edit_move_display (edit
,
2993 edit
->num_widget_lines
/ 2);
2994 edit_move_to_line (edit
, p
->line
);
2999 case CK_Beginning_Of_Text
:
3000 case CK_Beginning_Of_Text_Highlight
:
3001 edit_move_to_top (edit
);
3003 case CK_End_Of_Text
:
3004 case CK_End_Of_Text_Highlight
:
3005 edit_move_to_bottom (edit
);
3009 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3010 edit_insert_over (edit
);
3011 edit_block_copy_cmd (edit
);
3014 edit_block_delete_cmd (edit
);
3017 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3018 edit_insert_over (edit
);
3019 edit_block_move_cmd (edit
);
3022 case CK_Shift_Block_Left
:
3023 if (edit
->mark1
!= edit
->mark2
)
3024 edit_move_block_to_left (edit
);
3026 case CK_Shift_Block_Right
:
3027 if (edit
->mark1
!= edit
->mark2
)
3028 edit_move_block_to_right (edit
);
3031 edit_copy_to_X_buf_cmd (edit
);
3034 edit_cut_to_X_buf_cmd (edit
);
3037 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3038 edit_insert_over (edit
);
3039 edit_paste_from_X_buf_cmd (edit
);
3041 case CK_Selection_History
:
3042 edit_paste_from_history (edit
);
3046 edit_save_as_cmd (edit
);
3049 edit_save_confirm_cmd (edit
);
3052 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3055 edit_save_block_cmd (edit
);
3057 case CK_Insert_File
:
3058 edit_insert_file_cmd (edit
);
3061 case CK_Load_Prev_File
:
3062 edit_load_back_cmd (edit
);
3064 case CK_Load_Next_File
:
3065 edit_load_forward_cmd (edit
);
3068 case CK_Load_Syntax_File
:
3069 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3071 case CK_Choose_Syntax
:
3072 edit_syntax_dialog ();
3075 case CK_Load_Menu_File
:
3076 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3079 case CK_Toggle_Syntax
:
3080 if ((option_syntax_highlighting
^= 1) == 1)
3081 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3082 edit
->force
|= REDRAW_PAGE
;
3085 case CK_Toggle_Tab_TWS
:
3086 enable_show_tabs_tws
^= 1;
3087 edit
->force
|= REDRAW_PAGE
;
3091 edit_search_cmd (edit
, 0);
3094 edit_search_cmd (edit
, 1);
3097 edit_replace_cmd (edit
, 0);
3099 case CK_Replace_Again
:
3100 edit_replace_cmd (edit
, 1);
3102 case CK_Complete_Word
:
3103 /* if text marked shift block */
3104 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3105 edit_move_block_to_left (edit
);
3107 edit_complete_word_cmd (edit
);
3110 case CK_Find_Definition
:
3111 edit_get_match_keyword_cmd (edit
);
3114 dlg_stop (edit
->widget
.parent
);
3117 edit_new_cmd (edit
);
3120 edit_help_cmd (edit
);
3123 edit_refresh_cmd (edit
);
3125 case CK_SaveSetupCmd
:
3129 query_dialog (_(" About "),
3130 _("\n Cooledit v3.11.5\n\n"
3131 " Copyright (C) 1996 the Free Software Foundation\n\n"
3132 " A user friendly text editor written\n"
3133 " for the Midnight Commander.\n"), D_NORMAL
,
3139 case CK_Edit_Options
:
3140 edit_options_dialog ();
3142 case CK_Edit_Save_Mode
:
3143 menu_save_mode_cmd ();
3148 /* fool gcc to prevent a Y2K warning */
3149 char time_format
[] = "_c";
3150 time_format
[0] = '%';
3152 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3153 edit_print_string (edit
, s
);
3154 edit
->force
|= REDRAW_PAGE
;
3159 edit_goto_cmd (edit
);
3161 case CK_Paragraph_Format
:
3162 format_paragraph (edit
, 1);
3163 edit
->force
|= REDRAW_PAGE
;
3165 case CK_Delete_Macro
:
3166 edit_delete_macro_cmd (edit
);
3168 case CK_Match_Bracket
:
3169 edit_goto_matching_bracket (edit
);
3175 edit_sort_cmd (edit
);
3178 edit_ext_cmd (edit
);
3181 edit_mail_dialog (edit
);
3186 case CK_SelectCodepage
:
3187 edit_select_codepage_cmd (edit
);
3189 case CK_Insert_Literal
:
3190 edit_insert_literal_cmd (edit
);
3192 case CK_Execute_Macro
:
3193 edit_execute_macro_cmd (edit
);
3195 case CK_Begin_End_Macro
:
3196 edit_begin_end_macro_cmd (edit
);
3206 if ((command
/ 1000) == 1) /* a shell command */
3207 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3208 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3209 struct macro m
[MAX_MACRO_LENGTH
];
3211 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3212 edit_execute_macro (edit
, m
, nm
);
3215 /* keys which must set the col position, and the search vars */
3220 case CK_Replace_Again
:
3221 case CK_Complete_Word
:
3222 edit
->prev_col
= edit_get_col (edit
);
3225 case CK_Up_Highlight
:
3226 case CK_Up_Alt_Highlight
:
3228 case CK_Down_Highlight
:
3229 case CK_Down_Alt_Highlight
:
3231 case CK_Page_Up_Highlight
:
3232 case CK_Page_Up_Alt_Highlight
:
3234 case CK_Page_Down_Highlight
:
3235 case CK_Page_Down_Alt_Highlight
:
3236 case CK_Beginning_Of_Text
:
3237 case CK_Beginning_Of_Text_Highlight
:
3238 case CK_End_Of_Text
:
3239 case CK_End_Of_Text_Highlight
:
3240 case CK_Paragraph_Up
:
3241 case CK_Paragraph_Up_Highlight
:
3242 case CK_Paragraph_Up_Alt_Highlight
:
3243 case CK_Paragraph_Down
:
3244 case CK_Paragraph_Down_Highlight
:
3245 case CK_Paragraph_Down_Alt_Highlight
:
3247 case CK_Scroll_Up_Highlight
:
3248 case CK_Scroll_Up_Alt_Highlight
:
3249 case CK_Scroll_Down
:
3250 case CK_Scroll_Down_Highlight
:
3251 case CK_Scroll_Down_Alt_Highlight
:
3252 edit
->search_start
= edit
->curs1
;
3253 edit
->found_len
= 0;
3256 edit
->found_len
= 0;
3257 edit
->prev_col
= edit_get_col (edit
);
3258 edit
->search_start
= edit
->curs1
;
3260 edit_find_bracket (edit
);
3262 if (option_auto_para_formatting
) {
3266 case CK_Delete_Word_Left
:
3267 case CK_Delete_Word_Right
:
3268 case CK_Delete_To_Line_End
:
3269 case CK_Delete_To_Line_Begin
:
3270 format_paragraph (edit
, 0);
3271 edit
->force
|= REDRAW_PAGE
;
3278 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3282 if (edit
->macro_depth
++ > 256) {
3283 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3284 edit
->macro_depth
--;
3287 edit
->force
|= REDRAW_PAGE
;
3288 for (; i
< n
; i
++) {
3289 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3291 edit_update_screen (edit
);
3292 edit
->macro_depth
--;
3295 /* User edit menu, like user menu (F2) but only in editor. */
3297 user_menu (WEdit
* edit
)
3301 long start_mark
, end_mark
;
3304 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3306 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3308 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3310 /* run shell scripts from menu */
3311 user_menu_cmd (edit
);
3313 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0)) {
3318 /* i.e. we have marked block */
3319 rc
= edit_block_delete_cmd (edit
);
3323 edit_insert_file (edit
, block_file
);
3325 /* truncate block file */
3326 fd
= fopen (block_file
, "w");
3330 edit_refresh_cmd (edit
);
3331 edit
->force
|= REDRAW_COMPLETELY
;
3333 g_free (block_file
);
3337 edit_stack_init (void)
3339 for (edit_stack_iterator
= 0;
3340 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3341 edit_stack_iterator
++ ) {
3342 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3343 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3346 edit_stack_iterator
= 0;
3350 edit_stack_free (void)
3352 for (edit_stack_iterator
= 0;
3353 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3354 edit_stack_iterator
++)
3355 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3359 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
3361 edit_move_updown (edit
, i
, scroll
, TRUE
);
3365 void edit_move_down (WEdit
* edit
, unsigned long i
, int scroll
)
3367 edit_move_updown (edit
, i
, scroll
, FALSE
);