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 "../src/global.h"
44 #include "edit-impl.h"
46 #include "edit-widget.h"
47 #include "../src/cmddef.h"
49 #include "../src/tty/color.h" /* EDITOR_NORMAL_COLOR */
50 #include "../src/tty/tty.h" /* attrset() */
51 #include "../src/tty/key.h" /* is_idle() */
52 #include "../src/skin/skin.h" /* mc_skin_color_get */
54 #include "../src/widget.h"
55 #include "../src/cmd.h" /* view_other_cmd() */
56 #include "../src/user.h" /* user_menu_cmd() */
57 #include "../src/wtools.h" /* query_dialog() */
58 #include "../src/timefmt.h" /* time formatting */
59 #include "../src/strutil.h" /* utf string functions */
60 #include "../src/charsets.h" /* get_codepage_id */
61 #include "../src/main.h" /* source_codepage */
62 #include "../src/learn.h" /* learn_keys */
64 int option_word_wrap_line_length
= 72;
65 int option_typewriter_wrap
= 0;
66 int option_auto_para_formatting
= 0;
67 int option_fill_tabs_with_spaces
= 0;
68 int option_return_does_auto_indent
= 1;
69 int option_backspace_through_tabs
= 0;
70 int option_fake_half_tabs
= 1;
71 int option_save_mode
= EDIT_QUICK_SAVE
;
72 int option_save_position
= 1;
73 int option_max_undo
= 32768;
74 int option_persistent_selections
= 1;
75 int option_cursor_beyond_eol
= 1;
76 int option_line_state
= 0;
77 int option_line_state_width
= 0;
79 int option_edit_right_extreme
= 0;
80 int option_edit_left_extreme
= 0;
81 int option_edit_top_extreme
= 0;
82 int option_edit_bottom_extreme
= 0;
83 int enable_show_tabs_tws
= 1;
84 int option_check_nl_at_eof
= 0;
86 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
87 char *option_backup_ext
= NULL
;
89 int edit_stack_iterator
= 0;
90 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
91 /* magic sequense for say than block is vertical */
92 const char VERTICAL_MAGIC
[] = {'\1', '\1', '\1', '\1', '\n'};
95 * here's a quick sketch of the layout: (don't run this through indent.)
97 * (b1 is buffers1 and b2 is buffers2)
100 * \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
101 * ______________________________________|______________________________________
103 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
104 * |-> |-> |-> |-> |-> |-> |
106 * _<------------------------->|<----------------->_
107 * WEdit->curs2 | WEdit->curs1
112 * file end|||file beginning
121 const global_keymap_t
*editor_map
;
122 const global_keymap_t
*editor_x_map
;
124 static void user_menu (WEdit
*edit
);
126 int edit_get_byte (WEdit
* edit
, long byte_index
)
129 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
132 if (byte_index
>= edit
->curs1
) {
133 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
134 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
136 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
140 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
143 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
146 if (byte_index
>= edit
->curs1
) {
147 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
148 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
]+(EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
150 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
]+(byte_index
& M_EDIT_BUF_SIZE
));
154 char *edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
158 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
164 if (byte_index
>= edit
->curs1
) {
165 p
= edit
->curs1
+ edit
->curs2
- 1;
166 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
168 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
172 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
177 gchar
*next_ch
= NULL
;
180 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
185 str
= edit_get_byte_ptr (edit
, byte_index
);
192 res
= g_utf8_get_char_validated (str
, -1);
199 /* Calculate UTF-8 char width */
200 next_ch
= g_utf8_next_char (str
);
202 width
= next_ch
- str
;
212 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
214 gchar
*str
, *buf
= NULL
;
217 gchar
*next_ch
= NULL
;
223 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
228 ch
= edit_get_utf (edit
, byte_index
, &width
);
235 str
= edit_get_byte_ptr (edit
, byte_index
);
236 buf
= edit_get_buf_ptr (edit
, byte_index
);
237 if (str
== NULL
|| buf
== NULL
) {
241 /* get prev utf8 char */
243 str
= g_utf8_find_prev_char (buf
, str
);
245 res
= g_utf8_get_char_validated (str
, -1);
252 /* Calculate UTF-8 char width */
253 next_ch
= g_utf8_next_char(str
);
255 width
= next_ch
- str
;
266 * Initialize the buffers for an empty files.
269 edit_init_buffers (WEdit
*edit
)
273 for (j
= 0; j
<= MAXBUFF
; j
++) {
274 edit
->buffers1
[j
] = NULL
;
275 edit
->buffers2
[j
] = NULL
;
280 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
284 * Load file OR text into buffers. Set cursor to the beginning of file.
288 edit_load_file_fast (WEdit
*edit
, const char *filename
)
292 edit
->curs2
= edit
->last_byte
;
293 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
295 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
296 GString
*errmsg
= g_string_new(NULL
);
297 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
298 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
299 g_string_free (errmsg
, TRUE
);
303 if (!edit
->buffers2
[buf2
])
304 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
307 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
308 (edit
->curs2
& M_EDIT_BUF_SIZE
),
309 edit
->curs2
& M_EDIT_BUF_SIZE
);
311 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
312 /* edit->buffers2[0] is already allocated */
313 if (!edit
->buffers2
[buf
])
314 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
315 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
322 /* detecting an error on save is easy: just check if every byte has been written. */
323 /* detecting an error on read, is not so easy 'cos there is not way to tell
324 whether you read everything or not. */
325 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
326 static const struct edit_filters
{
327 const char *read
, *write
, *extension
;
329 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
330 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
331 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
332 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
333 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
336 /* Return index of the filter or -1 is there is no appropriate filter */
337 static int edit_find_filter (const char *filename
)
342 l
= strlen (filename
);
343 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
344 e
= strlen (all_filters
[i
].extension
);
346 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
353 edit_get_filter (const char *filename
)
356 char *p
, *quoted_name
;
357 i
= edit_find_filter (filename
);
360 quoted_name
= name_quote (filename
, 0);
361 l
= str_term_width1 (quoted_name
);
362 p
= g_malloc0 (str_term_width1 (all_filters
[i
].read
) + l
+ 2);
363 sprintf (p
, all_filters
[i
].read
, quoted_name
);
364 g_free (quoted_name
);
369 edit_get_write_filter (const char *write_name
, const char *filename
)
373 i
= edit_find_filter (filename
);
376 writename
= name_quote (write_name
, 0);
377 l
= str_term_width1 (writename
);
378 p
= g_malloc0 (str_term_width1 (all_filters
[i
].write
) + l
+ 2);
379 sprintf (p
, all_filters
[i
].write
, writename
);
385 edit_insert_stream (WEdit
* edit
, FILE * f
)
389 while ((c
= fgetc (f
)) >= 0) {
390 edit_insert (edit
, c
);
396 long edit_write_stream (WEdit
* edit
, FILE * f
)
400 if (edit
->lb
== LB_ASIS
) {
401 for (i
= 0; i
< edit
->last_byte
; i
++)
402 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
407 /* change line breaks */
408 for (i
= 0; i
< edit
->last_byte
; i
++) {
409 unsigned char c
= edit_get_byte (edit
, i
);
411 if (!(c
== '\n' || c
== '\r')) {
413 if (fputc (c
, f
) < 0)
415 } else { /* (c == '\n' || c == '\r') */
416 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
419 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
420 /* put one line break unconditionally */
421 if (fputc ('\n', f
) < 0)
424 i
++; /* 2 chars are processed */
426 if (c
== '\r' && c1
== '\n')
427 /* Windows line break; go to the next char */
430 if (c
== '\r' && c1
== '\r') {
431 /* two Macintosh line breaks; put second line break */
432 if (fputc ('\n', f
) < 0)
437 if (fputc (c1
, f
) < 0)
441 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
442 /* put one line break unconditionally */
443 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
446 if (c
== '\r' && c1
== '\n')
447 /* Windows line break; go to the next char */
451 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
452 /* put one line break unconditionally */
453 if (fputc ('\r', f
) < 0)
456 i
++; /* 2 chars are processed */
458 if (c
== '\r' && c1
== '\n')
459 /* Windows line break; go to the next char */
462 if (c
== '\n' && c1
== '\n') {
463 /* two Windows line breaks; put second line break */
464 if (fputc ('\r', f
) < 0)
469 if (fputc (c1
, f
) < 0)
472 case LB_ASIS
: /* default without changes */
478 return edit
->last_byte
;
481 #define TEMP_BUF_LEN 1024
483 /* inserts a file at the cursor, returns 1 on success */
485 edit_insert_file (WEdit
*edit
, const char *filename
)
488 if ((p
= edit_get_filter (filename
))) {
490 long current
= edit
->curs1
;
491 f
= (FILE *) popen (p
, "r");
493 edit_insert_stream (edit
, f
);
494 edit_cursor_move (edit
, current
- edit
->curs1
);
495 if (pclose (f
) > 0) {
496 GString
*errmsg
= g_string_new (NULL
);
497 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
498 edit_error_dialog (_("Error"), errmsg
->str
);
499 g_string_free (errmsg
, TRUE
);
504 GString
*errmsg
= g_string_new (NULL
);
505 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
506 edit_error_dialog (_("Error"), errmsg
->str
);
507 g_string_free (errmsg
, TRUE
);
513 int i
, file
, blocklen
;
514 long current
= edit
->curs1
;
515 int vertical_insertion
= 0;
517 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
519 buf
= g_malloc0 (TEMP_BUF_LEN
);
520 blocklen
= mc_read (file
, buf
, sizeof(VERTICAL_MAGIC
));
522 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
523 if ( memcmp(buf
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
)) == 0 ) {
524 vertical_insertion
= 1;
526 mc_lseek (file
, 0, SEEK_SET
);
529 if (vertical_insertion
) {
530 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
532 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
533 for (i
= 0; i
< blocklen
; i
++)
534 edit_insert (edit
, buf
[i
]);
537 edit_cursor_move (edit
, current
- edit
->curs1
);
546 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
548 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
551 GString
*errmsg
= (GString
*) 0;
553 /* Try opening an existing file */
554 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
558 * Try creating the file. O_EXCL prevents following broken links
559 * and opening existing files.
563 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
566 g_string_sprintf (errmsg
= g_string_new (NULL
),
567 _(" Cannot open %s for reading "), filename
);
570 /* New file, delete it if it's not modified or saved */
571 edit
->delete_file
= 1;
575 /* Check what we have opened */
576 if (mc_fstat (file
, st
) < 0) {
577 g_string_sprintf (errmsg
= g_string_new (NULL
),
578 _(" Cannot get size/permissions for %s "), filename
);
582 /* We want to open regular files only */
583 if (!S_ISREG (st
->st_mode
)) {
584 g_string_sprintf (errmsg
= g_string_new (NULL
),
585 _(" %s is not a regular file "), filename
);
590 * Don't delete non-empty files.
591 * O_EXCL should prevent it, but let's be on the safe side.
593 if (st
->st_size
> 0) {
594 edit
->delete_file
= 0;
597 if (st
->st_size
>= SIZE_LIMIT
) {
598 g_string_sprintf (errmsg
= g_string_new (NULL
),
599 _(" File %s is too large "), filename
);
603 (void) mc_close (file
);
605 edit_error_dialog (_("Error"), errmsg
->str
);
606 g_string_free (errmsg
, TRUE
);
613 * Open the file and load it into the buffers, either directly or using
614 * a filter. Return 0 on success, 1 on error.
616 * Fast loading (edit_load_file_fast) is used when the file size is
617 * known. In this case the data is read into the buffers by blocks.
618 * If the file size is not known, the data is loaded byte by byte in
622 edit_load_file (WEdit
*edit
)
626 /* Cannot do fast load if a filter is used */
627 if (edit_find_filter (edit
->filename
) >= 0)
631 * VFS may report file size incorrectly, and slow load is not a big
632 * deal considering overhead in VFS.
634 if (!vfs_file_is_local (edit
->filename
))
638 * FIXME: line end translation should disable fast loading as well
639 * Consider doing fseek() to the end and ftell() for the real size.
642 if (*edit
->filename
) {
643 /* If we are dealing with a real file, check that it exists */
644 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
647 /* nothing to load */
651 edit_init_buffers (edit
);
654 edit
->last_byte
= edit
->stat1
.st_size
;
655 edit_load_file_fast (edit
, edit
->filename
);
656 /* If fast load was used, the number of lines wasn't calculated */
657 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
660 const char *codepage_id
;
663 if (*edit
->filename
) {
664 edit
->stack_disable
= 1;
665 if (!edit_insert_file (edit
, edit
->filename
)) {
669 edit
->stack_disable
= 0;
673 codepage_id
= get_codepage_id( source_codepage
);
675 edit
->utf8
= str_isutf8 ( codepage_id
);
682 /* Restore saved cursor position in the file */
684 edit_load_position (WEdit
*edit
)
689 if (!edit
->filename
|| !*edit
->filename
)
692 filename
= vfs_canon (edit
->filename
);
693 load_file_position (filename
, &line
, &column
);
696 edit_move_to_line (edit
, line
- 1);
697 edit
->prev_col
= column
;
698 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
699 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
702 /* Save cursor position in the file */
704 edit_save_position (WEdit
*edit
)
708 if (!edit
->filename
|| !*edit
->filename
)
711 filename
= vfs_canon (edit
->filename
);
712 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
716 /* Clean the WEdit stricture except the widget part */
718 edit_purge_widget (WEdit
*edit
)
720 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
721 char *start
= (char *) edit
+ sizeof (Widget
);
722 memset (start
, 0, len
);
723 edit
->macro_i
= -1; /* not recording a macro */
727 edit_set_keymap (void)
729 editor_map
= default_editor_keymap
;
730 if (editor_keymap
&& editor_keymap
->len
> 0)
731 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
733 editor_x_map
= default_editor_x_keymap
;
734 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
735 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
739 #define space_width 1
742 * Fill in the edit structure. Return NULL on failure. Pass edit as
743 * NULL to allocate a new structure.
745 * If line is 0, try to restore saved position. Otherwise put the
746 * cursor on that line and show it in the middle of the screen.
749 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
753 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
754 if ( option_line_state
) {
755 option_line_state_width
= LINE_STATE_WIDTH
;
757 option_line_state_width
= 0;
762 * Expand option_whole_chars_search by national letters using
766 static char option_whole_chars_search_buf
[256];
768 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
770 size_t len
= str_term_width1 (option_whole_chars_search
);
772 strcpy (option_whole_chars_search_buf
,
773 option_whole_chars_search
);
775 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
776 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
777 option_whole_chars_search_buf
[len
++] = i
;
781 option_whole_chars_search_buf
[len
] = 0;
782 option_whole_chars_search
= option_whole_chars_search_buf
;
784 #endif /* ENABLE_NLS */
785 edit
= g_malloc0 (sizeof (WEdit
));
789 edit_purge_widget (edit
);
790 edit
->num_widget_lines
= lines
;
792 edit
->num_widget_columns
= columns
;
793 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
794 edit
->stat1
.st_uid
= getuid ();
795 edit
->stat1
.st_gid
= getgid ();
796 edit
->stat1
.st_mtime
= 0;
798 edit
->force
|= REDRAW_PAGE
;
799 edit_set_filename (edit
, filename
);
800 edit
->stack_size
= START_STACK_SIZE
;
801 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
802 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
803 if (edit_load_file (edit
)) {
804 /* edit_load_file already gives an error message */
810 edit
->converter
= str_cnv_from_term
;
813 const char *cp_id
= NULL
;
814 cp_id
= get_codepage_id (source_codepage
>= 0 ?
815 source_codepage
: display_codepage
);
819 conv
= str_crt_conv_from (cp_id
);
820 if (conv
!= INVALID_CONV
) {
821 if (edit
->converter
!= str_cnv_from_term
)
822 str_close_conv (edit
->converter
);
823 edit
->converter
= conv
;
827 edit
->utf8
= str_isutf8 (cp_id
);
831 edit
->loading_done
= 1;
834 edit_load_syntax (edit
, 0, 0);
837 edit_get_syntax_color (edit
, -1, &color
);
840 /* load saved cursor position */
841 if ((line
== 0) && option_save_position
) {
842 edit_load_position (edit
);
846 edit_move_display (edit
, line
- 1);
847 edit_move_to_line (edit
, line
- 1);
855 /* Clear the edit struct, freeing everything in it. Return 1 on success */
857 edit_clean (WEdit
*edit
)
864 /* a stale lock, remove it */
866 edit
->locked
= edit_unlock_file (edit
->filename
);
868 /* save cursor position */
869 if (option_save_position
)
870 edit_save_position (edit
);
872 /* File specified on the mcedit command line and never saved */
873 if (edit
->delete_file
)
874 unlink (edit
->filename
);
876 edit_free_syntax_rules (edit
);
877 book_mark_flush (edit
, -1);
878 for (; j
<= MAXBUFF
; j
++) {
879 g_free (edit
->buffers1
[j
]);
880 g_free (edit
->buffers2
[j
]);
883 g_free (edit
->undo_stack
);
884 g_free (edit
->filename
);
889 mc_search_free(edit
->search
);
892 edit_purge_widget (edit
);
897 /* returns 1 on success */
899 edit_renew (WEdit
* edit
)
901 int lines
= edit
->num_widget_lines
;
902 int columns
= edit
->num_widget_columns
;
905 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
909 * Load a new file into the editor. If it fails, preserve the old file.
910 * To do it, allocate a new widget, initialize it and, if the new file
911 * was loaded, copy the data to the old widget.
912 * Return 1 on success, 0 on failure.
915 edit_reload (WEdit
*edit
, const char *filename
)
918 int lines
= edit
->num_widget_lines
;
919 int columns
= edit
->num_widget_columns
;
921 e
= g_malloc0 (sizeof (WEdit
));
922 e
->widget
= edit
->widget
;
923 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
928 memcpy (edit
, e
, sizeof (WEdit
));
934 * Load a new file into the editor and set line. If it fails, preserve the old file.
935 * To do it, allocate a new widget, initialize it and, if the new file
936 * was loaded, copy the data to the old widget.
937 * Return 1 on success, 0 on failure.
940 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
943 int lines
= edit
->num_widget_lines
;
944 int columns
= edit
->num_widget_columns
;
946 e
= g_malloc0 (sizeof (WEdit
));
947 e
->widget
= edit
->widget
;
948 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
953 memcpy (edit
, e
, sizeof (WEdit
));
960 Recording stack for undo:
961 The following is an implementation of a compressed stack. Identical
962 pushes are recorded by a negative prefix indicating the number of times the
963 same char was pushed. This saves space for repeated curs-left or curs-right
980 If the stack long int is 0-255 it represents a normal insert (from a backspace),
981 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
982 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
983 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
986 The only way the cursor moves or the buffer is changed is through the routines:
987 insert, backspace, insert_ahead, delete, and cursor_move.
988 These record the reverse undo movements onto the stack each time they are
991 Each key press results in a set of actions (insert; delete ...). So each time
992 a key is pressed the current position of start_display is pushed as
993 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
994 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
995 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
999 void edit_push_action (WEdit
* edit
, long c
,...)
1001 unsigned long sp
= edit
->stack_pointer
;
1005 /* first enlarge the stack if necessary */
1006 if (sp
> edit
->stack_size
- 10) { /* say */
1007 if (option_max_undo
< 256)
1008 option_max_undo
= 256;
1009 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1010 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1012 edit
->undo_stack
= t
;
1013 edit
->stack_size
<<= 1;
1014 edit
->stack_size_mask
= edit
->stack_size
- 1;
1018 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1019 if (edit
->stack_disable
)
1022 #ifdef FAST_MOVE_CURSOR
1023 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1025 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1026 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1028 c
= -(va_arg (ap
, int));
1031 #endif /* ! FAST_MOVE_CURSOR */
1032 if (edit
->stack_bottom
!= sp
1033 && spm1
!= edit
->stack_bottom
1034 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1036 if (edit
->undo_stack
[spm1
] < 0) {
1037 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1039 if (edit
->undo_stack
[spm1
] > -1000000000) {
1040 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1041 edit
->undo_stack
[spm1
]--;
1045 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1046 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1047 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1048 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1049 if (edit
->undo_stack
[spm1
] == -2)
1050 edit
->stack_pointer
= spm1
;
1052 edit
->undo_stack
[spm1
]++;
1057 d
= edit
->undo_stack
[spm1
];
1060 return; /* --> no need to push multiple do-nothings */
1061 edit
->undo_stack
[sp
] = -2;
1064 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1065 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1066 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1067 edit
->stack_pointer
= spm1
;
1073 edit
->undo_stack
[sp
] = c
;
1076 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1078 /* if the sp wraps round and catches the stack_bottom then erase
1079 * the first set of actions on the stack to make space - by moving
1080 * stack_bottom forward one "key press" */
1081 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1082 if ((unsigned long) c
== edit
->stack_bottom
||
1083 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1085 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1086 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1088 /*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: */
1089 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1090 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1094 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1095 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1098 pop_action (WEdit
* edit
)
1101 unsigned long sp
= edit
->stack_pointer
;
1102 if (sp
== edit
->stack_bottom
) {
1103 return STACK_BOTTOM
;
1105 sp
= (sp
- 1) & edit
->stack_size_mask
;
1106 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1107 /* edit->undo_stack[sp] = '@'; */
1108 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1111 if (sp
== edit
->stack_bottom
) {
1112 return STACK_BOTTOM
;
1114 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1115 if (edit
->undo_stack
[sp
] == -2) {
1116 /* edit->undo_stack[sp] = '@'; */
1117 edit
->stack_pointer
= sp
;
1119 edit
->undo_stack
[sp
]++;
1124 /* is called whenever a modification is made by one of the four routines below */
1125 static void edit_modification (WEdit
* edit
)
1127 edit
->caches_valid
= 0;
1128 edit
->screen_modified
= 1;
1130 /* raise lock when file modified */
1131 if (!edit
->modified
&& !edit
->delete_file
)
1132 edit
->locked
= edit_lock_file (edit
->filename
);
1137 Basic low level single character buffer alterations and movements at the cursor.
1138 Returns char passed over, inserted or removed.
1142 edit_insert (WEdit
*edit
, int c
)
1144 /* check if file has grown to large */
1145 if (edit
->last_byte
>= SIZE_LIMIT
)
1148 /* first we must update the position of the display window */
1149 if (edit
->curs1
< edit
->start_display
) {
1150 edit
->start_display
++;
1155 /* Mark file as modified, unless the file hasn't been fully loaded */
1156 if (edit
->loading_done
) {
1157 edit_modification (edit
);
1160 /* now we must update some info on the file and check if a redraw is required */
1162 if (edit
->book_mark
)
1163 book_mark_inc (edit
, edit
->curs_line
);
1165 edit
->total_lines
++;
1166 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1169 /* save the reverse command onto the undo stack */
1170 edit_push_action (edit
, BACKSPACE
);
1172 /* update markers */
1173 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1174 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1175 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1177 /* add a new buffer if we've reached the end of the last one */
1178 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1179 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1180 g_malloc0 (EDIT_BUF_SIZE
);
1182 /* perform the insertion */
1183 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1184 curs1
& M_EDIT_BUF_SIZE
]
1185 = (unsigned char) c
;
1187 /* update file length */
1190 /* update cursor position */
1195 edit_insert_over (WEdit
* edit
)
1199 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1200 edit_insert (edit
, ' ');
1205 /* same as edit_insert and move left */
1206 void edit_insert_ahead (WEdit
* edit
, int c
)
1208 if (edit
->last_byte
>= SIZE_LIMIT
)
1210 if (edit
->curs1
< edit
->start_display
) {
1211 edit
->start_display
++;
1215 edit_modification (edit
);
1217 if (edit
->book_mark
)
1218 book_mark_inc (edit
, edit
->curs_line
);
1219 edit
->total_lines
++;
1220 edit
->force
|= REDRAW_AFTER_CURSOR
;
1222 edit_push_action (edit
, DELCHAR
);
1224 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1225 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1226 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1228 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1229 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1230 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1237 int edit_delete (WEdit
* edit
, const int byte_delete
)
1246 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1247 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1248 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1251 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1252 if ( edit
->utf8
&& byte_delete
== 0 ) {
1253 edit_get_utf (edit
, edit
->curs1
, &cw
);
1257 for ( i
= 1; i
<= cw
; i
++ ) {
1258 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1260 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1261 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1262 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1266 edit_push_action (edit
, p
+ 256);
1269 edit_modification (edit
);
1271 if (edit
->book_mark
)
1272 book_mark_dec (edit
, edit
->curs_line
);
1273 edit
->total_lines
--;
1274 edit
->force
|= REDRAW_AFTER_CURSOR
;
1276 if (edit
->curs1
< edit
->start_display
) {
1277 edit
->start_display
--;
1287 edit_backspace (WEdit
* edit
, const int byte_delete
)
1296 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1297 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1298 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1301 if ( edit
->utf8
&& byte_delete
== 0 ) {
1302 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1306 for ( i
= 1; i
<= cw
; i
++ ) {
1307 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1308 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1309 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1310 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1314 edit_push_action (edit
, p
);
1316 edit_modification (edit
);
1318 if (edit
->book_mark
)
1319 book_mark_dec (edit
, edit
->curs_line
);
1321 edit
->total_lines
--;
1322 edit
->force
|= REDRAW_AFTER_CURSOR
;
1325 if (edit
->curs1
< edit
->start_display
) {
1326 edit
->start_display
--;
1334 #ifdef FAST_MOVE_CURSOR
1336 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1339 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1341 next
-= (unsigned long) dest
;
1349 edit_move_backward_lots (WEdit
*edit
, long increment
)
1352 unsigned char *p
= NULL
;
1354 if (increment
> edit
->curs1
)
1355 increment
= edit
->curs1
;
1358 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1360 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1363 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1367 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1368 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1373 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1374 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1375 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1376 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1379 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1380 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1381 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1386 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1388 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1390 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1391 g_malloc0 (EDIT_BUF_SIZE
);
1396 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1406 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1408 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1412 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1413 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1416 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1418 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1419 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1424 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1426 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1428 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1429 g_malloc0 (EDIT_BUF_SIZE
);
1434 return edit_get_byte (edit
, edit
->curs1
);
1437 #endif /* ! FAST_MOVE_CURSOR */
1439 /* moves the cursor right or left: increment positive or negative respectively */
1440 void edit_cursor_move (WEdit
* edit
, long increment
)
1442 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1444 #ifdef FAST_MOVE_CURSOR
1445 if (increment
< -256) {
1446 edit
->force
|= REDRAW_PAGE
;
1447 edit_move_backward_lots (edit
, -increment
);
1450 #endif /* ! FAST_MOVE_CURSOR */
1452 if (increment
< 0) {
1453 for (; increment
< 0; increment
++) {
1457 edit_push_action (edit
, CURS_RIGHT
);
1459 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1460 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1461 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1462 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1464 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1465 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1466 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1467 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1472 edit
->force
|= REDRAW_LINE_BELOW
;
1476 } else if (increment
> 0) {
1477 for (; increment
> 0; increment
--) {
1481 edit_push_action (edit
, CURS_LEFT
);
1483 c
= edit_get_byte (edit
, edit
->curs1
);
1484 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1485 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1486 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1488 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1489 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1490 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1491 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1496 edit
->force
|= REDRAW_LINE_ABOVE
;
1502 /* These functions return positions relative to lines */
1504 /* returns index of last char on line + 1 */
1505 long edit_eol (WEdit
* edit
, long current
)
1507 if (current
< edit
->last_byte
) {
1509 if (edit_get_byte (edit
, current
) == '\n')
1512 return edit
->last_byte
;
1516 /* returns index of first char on line */
1517 long edit_bol (WEdit
* edit
, long current
)
1521 if (edit_get_byte (edit
, current
- 1) == '\n')
1529 int edit_count_lines (WEdit
* edit
, long current
, int upto
)
1532 if (upto
> edit
->last_byte
)
1533 upto
= edit
->last_byte
;
1536 while (current
< upto
)
1537 if (edit_get_byte (edit
, current
++) == '\n')
1543 /* If lines is zero this returns the count of lines from current to upto. */
1544 /* If upto is zero returns index of lines forward current. */
1545 long edit_move_forward (WEdit
* edit
, long current
, int lines
, long upto
)
1548 return edit_count_lines (edit
, current
, upto
);
1554 next
= edit_eol (edit
, current
) + 1;
1555 if (next
> edit
->last_byte
)
1565 /* Returns offset of 'lines' lines up from current */
1566 long edit_move_backward (WEdit
* edit
, long current
, int lines
)
1570 current
= edit_bol (edit
, current
);
1571 while((lines
--) && current
!= 0)
1572 current
= edit_bol (edit
, current
- 1);
1576 /* If cols is zero this returns the count of columns from current to upto. */
1577 /* If upto is zero returns index of cols across from current. */
1579 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1588 q
= edit
->last_byte
+ 2;
1590 for (col
= 0, p
= current
; p
< q
; p
++) {
1602 orig_c
= c
= edit_get_byte (edit
, p
);
1605 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1609 if (g_unichar_iswide (utf_ch
))
1611 } else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1615 c
= convert_to_display_c (c
);
1617 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1618 else if (c
== '\n') {
1623 } else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1624 /* '\r' is shown as ^M, so we must advance 2 characters */
1625 /* Caret notation for control characters */
1633 /* returns the current column position of the cursor */
1634 int edit_get_col (WEdit
* edit
)
1636 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1640 /* Scrolling functions */
1642 void edit_update_curs_row (WEdit
* edit
)
1644 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1647 void edit_update_curs_col (WEdit
* edit
)
1649 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1653 edit_get_curs_col (const WEdit
*edit
)
1655 return edit
->curs_col
;
1658 /*moves the display start position up by i lines */
1659 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1661 unsigned long lines_above
= edit
->start_line
;
1662 if (i
> lines_above
)
1665 edit
->start_line
-= i
;
1666 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1667 edit
->force
|= REDRAW_PAGE
;
1668 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1670 edit_update_curs_row (edit
);
1674 /* returns 1 if could scroll, 0 otherwise */
1675 void edit_scroll_downward (WEdit
* edit
, int i
)
1678 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1679 if (lines_below
> 0) {
1680 if (i
> lines_below
)
1682 edit
->start_line
+= i
;
1683 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1684 edit
->force
|= REDRAW_PAGE
;
1685 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1687 edit_update_curs_row (edit
);
1690 void edit_scroll_right (WEdit
* edit
, int i
)
1692 edit
->force
|= REDRAW_PAGE
;
1693 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1694 edit
->start_col
-= i
;
1697 void edit_scroll_left (WEdit
* edit
, int i
)
1699 if (edit
->start_col
) {
1700 edit
->start_col
+= i
;
1701 if (edit
->start_col
> 0)
1702 edit
->start_col
= 0;
1703 edit
->force
|= REDRAW_PAGE
;
1704 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1708 /* high level cursor movement commands */
1710 static int is_in_indent (WEdit
*edit
)
1712 long p
= edit_bol (edit
, edit
->curs1
);
1713 while (p
< edit
->curs1
)
1714 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1719 static int left_of_four_spaces (WEdit
*edit
);
1722 edit_move_to_prev_col (WEdit
* edit
, long p
)
1724 int prev
= edit
->prev_col
;
1725 int over
= edit
->over_col
;
1726 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1728 if (option_cursor_beyond_eol
) {
1729 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1731 if (line_len
< prev
+ edit
->over_col
) {
1732 edit
->over_col
= prev
+ over
- line_len
;
1733 edit
->prev_col
= line_len
;
1734 edit
->curs_col
= line_len
;
1736 edit
->curs_col
= prev
+ over
;
1737 edit
->prev_col
= edit
->curs_col
;
1742 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1743 edit_update_curs_col (edit
);
1745 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1746 int q
= edit
->curs_col
;
1747 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1748 p
= edit_bol (edit
, edit
->curs1
);
1749 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1750 if (!left_of_four_spaces (edit
))
1751 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1758 is_blank (WEdit
*edit
, long offset
)
1762 s
= edit_bol (edit
, offset
);
1763 f
= edit_eol (edit
, offset
) - 1;
1765 c
= edit_get_byte (edit
, s
++);
1773 /* returns the offset of line i */
1775 edit_find_line (WEdit
*edit
, int line
)
1779 if (!edit
->caches_valid
) {
1780 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1781 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1782 /* three offsets that we *know* are line 0 at 0 and these two: */
1783 edit
->line_numbers
[1] = edit
->curs_line
;
1784 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1785 edit
->line_numbers
[2] = edit
->total_lines
;
1786 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1787 edit
->caches_valid
= 1;
1789 if (line
>= edit
->total_lines
)
1790 return edit
->line_offsets
[2];
1793 /* find the closest known point */
1794 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1796 n
= abs (edit
->line_numbers
[i
] - line
);
1803 return edit
->line_offsets
[j
]; /* know the offset exactly */
1804 if (m
== 1 && j
>= 3)
1805 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1807 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1808 if (line
> edit
->line_numbers
[j
])
1809 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1811 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1812 edit
->line_numbers
[i
] = line
;
1813 return edit
->line_offsets
[i
];
1816 int line_is_blank (WEdit
* edit
, long line
)
1818 return is_blank (edit
, edit_find_line (edit
, line
));
1821 /* moves up until a blank line is reached, or until just
1822 before a non-blank line is reached */
1823 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1826 if (edit
->curs_line
> 1) {
1827 if (line_is_blank (edit
, edit
->curs_line
)) {
1828 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1829 for (i
= edit
->curs_line
- 1; i
; i
--)
1830 if (!line_is_blank (edit
, i
)) {
1835 for (i
= edit
->curs_line
- 1; i
; i
--)
1836 if (line_is_blank (edit
, i
))
1840 for (i
= edit
->curs_line
- 1; i
; i
--)
1841 if (line_is_blank (edit
, i
))
1845 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1848 /* moves down until a blank line is reached, or until just
1849 before a non-blank line is reached */
1850 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1853 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1854 i
= edit
->total_lines
;
1856 if (line_is_blank (edit
, edit
->curs_line
)) {
1857 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1858 for (i
= edit
->curs_line
+ 1; i
; i
++)
1859 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1864 for (i
= edit
->curs_line
+ 1; i
; i
++)
1865 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 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1877 static void edit_begin_page (WEdit
*edit
)
1879 edit_update_curs_row (edit
);
1880 edit_move_up (edit
, edit
->curs_row
, 0);
1883 static void edit_end_page (WEdit
*edit
)
1885 edit_update_curs_row (edit
);
1886 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1890 /* goto beginning of text */
1891 static void edit_move_to_top (WEdit
* edit
)
1893 if (edit
->curs_line
) {
1894 edit_cursor_move (edit
, -edit
->curs1
);
1895 edit_move_to_prev_col (edit
, 0);
1896 edit
->force
|= REDRAW_PAGE
;
1897 edit
->search_start
= 0;
1898 edit_update_curs_row(edit
);
1903 /* goto end of text */
1904 static void edit_move_to_bottom (WEdit
* edit
)
1906 if (edit
->curs_line
< edit
->total_lines
) {
1907 edit_cursor_move (edit
, edit
->curs2
);
1908 edit
->start_display
= edit
->last_byte
;
1909 edit
->start_line
= edit
->total_lines
;
1910 edit_update_curs_row(edit
);
1911 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1912 edit
->force
|= REDRAW_PAGE
;
1916 /* goto beginning of line */
1917 static void edit_cursor_to_bol (WEdit
* edit
)
1919 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1920 edit
->search_start
= edit
->curs1
;
1921 edit
->prev_col
= edit_get_col (edit
);
1925 /* goto end of line */
1926 static void edit_cursor_to_eol (WEdit
* edit
)
1928 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1929 edit
->search_start
= edit
->curs1
;
1930 edit
->prev_col
= edit_get_col (edit
);
1934 /* move cursor to line 'line' */
1935 void edit_move_to_line (WEdit
* e
, long line
)
1937 if(line
< e
->curs_line
)
1938 edit_move_up (e
, e
->curs_line
- line
, 0);
1940 edit_move_down (e
, line
- e
->curs_line
, 0);
1941 edit_scroll_screen_over_cursor (e
);
1944 /* scroll window so that first visible line is 'line' */
1945 void edit_move_display (WEdit
* e
, long line
)
1947 if(line
< e
->start_line
)
1948 edit_scroll_upward (e
, e
->start_line
- line
);
1950 edit_scroll_downward (e
, line
- e
->start_line
);
1953 /* save markers onto undo stack */
1954 void edit_push_markers (WEdit
* edit
)
1956 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1957 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1960 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1969 /* highlight marker toggle */
1970 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1972 edit_push_markers (edit
);
1974 edit_set_markers (edit
, 0, 0, 0, 0);
1975 edit
->force
|= REDRAW_PAGE
;
1977 if (edit
->mark2
>= 0) {
1978 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
1979 edit
->force
|= REDRAW_PAGE
;
1981 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
1985 static unsigned long
1990 const char option_chars_move_whole_word
[] =
1991 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1996 if (*option_chars_move_whole_word
== '!')
1998 return 0x80000000UL
;
2000 if (g_ascii_isupper ((gchar
) c
))
2002 else if (g_ascii_islower ((gchar
) c
))
2004 else if (g_ascii_isalpha (c
))
2006 else if (isdigit (c
))
2008 else if (isspace (c
))
2010 q
= strchr (option_chars_move_whole_word
, c
);
2012 return 0xFFFFFFFFUL
;
2014 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2018 } while ((q
= strchr (q
+ 1, c
)));
2023 edit_left_word_move (WEdit
*edit
, int s
)
2027 if (column_highlighting
2028 && edit
->mark1
!= edit
->mark2
2029 && edit
->over_col
== 0
2030 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2032 edit_cursor_move (edit
, -1);
2035 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2036 c2
= edit_get_byte (edit
, edit
->curs1
);
2037 if (!(my_type_of (c1
) & my_type_of (c2
)))
2039 if (isspace (c1
) && !isspace (c2
))
2042 if (!isspace (c1
) && isspace (c2
))
2047 static void edit_left_word_move_cmd (WEdit
* edit
)
2049 edit_left_word_move (edit
, 0);
2050 edit
->force
|= REDRAW_PAGE
;
2054 edit_right_word_move (WEdit
*edit
, int s
)
2058 if (column_highlighting
2059 && edit
->mark1
!= edit
->mark2
2060 && edit
->over_col
== 0
2061 && edit
->curs1
== edit_eol(edit
, edit
->curs1
)
2064 edit_cursor_move (edit
, 1);
2065 if (edit
->curs1
>= edit
->last_byte
)
2067 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2068 c2
= edit_get_byte (edit
, edit
->curs1
);
2069 if (!(my_type_of (c1
) & my_type_of (c2
)))
2071 if (isspace (c1
) && !isspace (c2
))
2074 if (!isspace (c1
) && isspace (c2
))
2079 static void edit_right_word_move_cmd (WEdit
* edit
)
2081 edit_right_word_move (edit
, 0);
2082 edit
->force
|= REDRAW_PAGE
;
2085 static void edit_right_char_move_cmd (WEdit
* edit
)
2090 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2094 c
= edit_get_byte (edit
, edit
->curs1
);
2096 if (option_cursor_beyond_eol
&& c
== '\n') {
2099 edit_cursor_move (edit
, cw
);
2103 static void edit_left_char_move_cmd (WEdit
* edit
)
2106 if (column_highlighting
2107 && edit
->mark1
!= edit
->mark2
2108 && edit
->over_col
== 0
2109 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2112 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2116 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2119 edit_cursor_move (edit
, -cw
);
2123 /** Up or down cursor moving.
2124 direction = TRUE - move up
2128 edit_move_updown (WEdit
* edit
, unsigned long i
, int scroll
, gboolean direction
) {
2130 unsigned long l
= (direction
)
2132 : edit
->total_lines
- edit
->curs_line
;
2141 edit
->force
|= REDRAW_PAGE
;
2143 edit_scroll_upward (edit
, i
);
2145 p
= edit_bol (edit
, edit
->curs1
);
2148 ? edit_move_backward (edit
, p
, i
)
2149 : edit_move_forward (edit
, p
, i
, 0);
2151 edit_cursor_move (edit
, p
- edit
->curs1
);
2153 edit_move_to_prev_col (edit
, p
);
2155 /* search start of current multibyte char (like CJK) */
2156 edit_right_char_move_cmd (edit
);
2157 edit_left_char_move_cmd (edit
);
2159 edit
->search_start
= edit
->curs1
;
2160 edit
->found_len
= 0;
2163 static void edit_right_delete_word (WEdit
* edit
)
2167 if (edit
->curs1
>= edit
->last_byte
)
2169 c1
= edit_delete (edit
, 1);
2170 c2
= edit_get_byte (edit
, edit
->curs1
);
2171 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2173 if (!(my_type_of (c1
) & my_type_of (c2
)))
2178 static void edit_left_delete_word (WEdit
* edit
)
2182 if (edit
->curs1
<= 0)
2184 c1
= edit_backspace (edit
, 1);
2185 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2186 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2188 if (!(my_type_of (c1
) & my_type_of (c2
)))
2194 the start column position is not recorded, and hence does not
2195 undo as it happed. But who would notice.
2198 edit_do_undo (WEdit
* edit
)
2203 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2205 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2210 edit_cursor_move (edit
, 1);
2213 edit_cursor_move (edit
, -1);
2216 edit_backspace (edit
, 1);
2219 edit_delete (edit
, 1);
2222 column_highlighting
= 1;
2225 column_highlighting
= 0;
2228 if (ac
>= 256 && ac
< 512)
2229 edit_insert_ahead (edit
, ac
- 256);
2230 if (ac
>= 0 && ac
< 256)
2231 edit_insert (edit
, ac
);
2233 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2234 edit
->mark1
= ac
- MARK_1
;
2235 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2236 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2237 edit
->mark2
= ac
- MARK_2
;
2238 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2241 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2244 if (edit
->start_display
> ac
- KEY_PRESS
) {
2245 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2246 edit
->force
|= REDRAW_PAGE
;
2247 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2248 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2249 edit
->force
|= REDRAW_PAGE
;
2251 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2252 edit_update_curs_row (edit
);
2255 edit
->stack_disable
= 0;
2258 static void edit_delete_to_line_end (WEdit
* edit
)
2260 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2263 edit_delete (edit
, 1);
2267 static void edit_delete_to_line_begin (WEdit
* edit
)
2269 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2272 edit_backspace (edit
, 1);
2277 edit_delete_line (WEdit
*edit
)
2280 * Delete right part of the line.
2281 * Note that edit_get_byte() returns '\n' when byte position is
2284 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2285 (void) edit_delete (edit
, 1);
2290 * Note that edit_delete() will not corrupt anything if called while
2291 * cursor position is EOF.
2293 (void) edit_delete (edit
, 1);
2296 * Delete left part of the line.
2297 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2299 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2300 (void) edit_backspace (edit
, 1);
2304 void insert_spaces_tab (WEdit
* edit
, int half
)
2307 edit_update_curs_col (edit
);
2308 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2310 edit_insert (edit
, ' ');
2315 static int is_aligned_on_a_tab (WEdit
* edit
)
2317 edit_update_curs_col (edit
);
2318 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2319 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2322 static int right_of_four_spaces (WEdit
*edit
)
2325 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2326 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2328 return is_aligned_on_a_tab (edit
);
2332 static int left_of_four_spaces (WEdit
*edit
)
2335 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2336 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2338 return is_aligned_on_a_tab (edit
);
2342 int edit_indent_width (WEdit
* edit
, long p
)
2345 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2347 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2350 void edit_insert_indent (WEdit
* edit
, int indent
)
2352 if (!option_fill_tabs_with_spaces
) {
2353 while (indent
>= TAB_SIZE
) {
2354 edit_insert (edit
, '\t');
2358 while (indent
-- > 0)
2359 edit_insert (edit
, ' ');
2363 edit_auto_indent (WEdit
* edit
)
2368 /* use the previous line as a template */
2369 p
= edit_move_backward (edit
, p
, 1);
2370 /* copy the leading whitespace of the line */
2371 for (;;) { /* no range check - the line _is_ \n-terminated */
2372 c
= edit_get_byte (edit
, p
++);
2373 if (c
!= ' ' && c
!= '\t')
2375 edit_insert (edit
, c
);
2380 edit_double_newline (WEdit
* edit
)
2382 edit_insert (edit
, '\n');
2383 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2385 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2387 edit
->force
|= REDRAW_PAGE
;
2388 edit_insert (edit
, '\n');
2392 edit_tab_cmd (WEdit
* edit
)
2396 if (option_fake_half_tabs
) {
2397 if (is_in_indent (edit
)) {
2398 /*insert a half tab (usually four spaces) unless there is a
2399 half tab already behind, then delete it and insert a
2401 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2402 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2403 edit_backspace (edit
, 1);
2404 edit_insert (edit
, '\t');
2406 insert_spaces_tab (edit
, 1);
2411 if (option_fill_tabs_with_spaces
) {
2412 insert_spaces_tab (edit
, 0);
2414 edit_insert (edit
, '\t');
2418 static void check_and_wrap_line (WEdit
* edit
)
2421 if (!option_typewriter_wrap
)
2423 edit_update_curs_col (edit
);
2424 if (edit
->curs_col
< option_word_wrap_line_length
)
2429 c
= edit_get_byte (edit
, curs
);
2430 if (c
== '\n' || curs
<= 0) {
2431 edit_insert (edit
, '\n');
2434 if (c
== ' ' || c
== '\t') {
2435 int current
= edit
->curs1
;
2436 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2437 edit_insert (edit
, '\n');
2438 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2444 static inline void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2446 void edit_push_key_press (WEdit
* edit
)
2448 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2449 if (edit
->mark2
== -1)
2450 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2453 /* this find the matching bracket in either direction, and sets edit->bracket */
2454 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2456 const char * const b
= "{}{[][()(", *p
;
2457 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2458 unsigned long j
= 0;
2460 edit_update_curs_row (edit
);
2461 c
= edit_get_byte (edit
, edit
->curs1
);
2464 if (!furthest_bracket_search
)
2465 furthest_bracket_search
--;
2466 /* not on a bracket at all */
2469 /* the matching bracket */
2471 /* going left or right? */
2472 if (strchr ("{[(", c
))
2474 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2475 /* out of buffer? */
2476 if (q
>= edit
->last_byte
|| q
< 0)
2478 a
= edit_get_byte (edit
, q
);
2479 /* don't want to eat CPU */
2480 if (j
++ > furthest_bracket_search
)
2482 /* out of screen? */
2484 if (q
< edit
->start_display
)
2486 /* count lines if searching downward */
2487 if (inc
> 0 && a
== '\n')
2488 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2491 /* count bracket depth */
2492 i
+= (a
== c
) - (a
== d
);
2493 /* return if bracket depth is zero */
2501 static long last_bracket
= -1;
2503 void edit_find_bracket (WEdit
* edit
)
2505 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2506 if (last_bracket
!= edit
->bracket
)
2507 edit
->force
|= REDRAW_PAGE
;
2508 last_bracket
= edit
->bracket
;
2512 edit_goto_matching_bracket (WEdit
*edit
)
2516 q
= edit_get_bracket (edit
, 0, 0);
2518 edit
->bracket
= edit
->curs1
;
2519 edit
->force
|= REDRAW_PAGE
;
2520 edit_cursor_move (edit
, q
- edit
->curs1
);
2525 * This executes a command as though the user initiated it through a key
2526 * press. Callback with WIDGET_KEY as a message calls this after
2527 * translating the key press. This function can be used to pass any
2528 * command to the editor. Note that the screen wouldn't update
2529 * automatically. Either of command or char_for_insertion must be
2530 * passed as -1. Commands are executed, and char_for_insertion is
2531 * inserted at the cursor.
2534 edit_execute_key_command (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2536 if (command
== CK_Begin_Record_Macro
) {
2538 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2541 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2542 edit
->force
|= REDRAW_COMPLETELY
;
2543 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2547 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2548 edit
->macro
[edit
->macro_i
].command
= command
;
2549 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2551 /* record the beginning of a set of editing actions initiated by a key press */
2552 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2553 edit_push_key_press (edit
);
2555 edit_execute_cmd (edit
, command
, char_for_insertion
);
2556 if (column_highlighting
)
2557 edit
->force
|= REDRAW_PAGE
;
2560 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2563 This executes a command at a lower level than macro recording.
2564 It also does not push a key_press onto the undo stack. This means
2565 that if it is called many times, a single undo command will undo
2566 all of them. It also does not check for the Undo command.
2569 edit_execute_cmd (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2571 edit
->force
|= REDRAW_LINE
;
2573 /* The next key press will unhighlight the found string, so update
2575 if (edit
->found_len
|| column_highlighting
)
2576 edit
->force
|= REDRAW_PAGE
;
2578 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2579 column_highlighting
= 0;
2580 if (!edit
->highlight
2581 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2582 edit_mark_cmd (edit
, 1); /* clear */
2583 edit_mark_cmd (edit
, 0); /* marking on */
2585 edit
->highlight
= 1;
2586 } else { /* any other command */
2587 if (edit
->highlight
)
2588 edit_mark_cmd (edit
, 0); /* clear */
2589 edit
->highlight
= 0;
2592 /* first check for undo */
2593 if (command
== CK_Undo
) {
2594 edit_do_undo (edit
);
2595 edit
->found_len
= 0;
2596 edit
->prev_col
= edit_get_col (edit
);
2597 edit
->search_start
= edit
->curs1
;
2601 /* An ordinary key press */
2602 if (char_for_insertion
>= 0) {
2603 if (edit
->overwrite
) {
2604 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2605 edit_delete (edit
, 0);
2607 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2608 edit_insert_over (edit
);
2610 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2611 unsigned char str
[6 + 1];
2613 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2620 while ( str
[i
] != 0 && i
<=6) {
2621 char_for_insertion
= str
[i
];
2622 edit_insert (edit
, char_for_insertion
);
2627 edit_insert (edit
, char_for_insertion
);
2629 if (option_auto_para_formatting
) {
2630 format_paragraph (edit
, 0);
2631 edit
->force
|= REDRAW_PAGE
;
2633 check_and_wrap_line (edit
);
2634 edit
->found_len
= 0;
2635 edit
->prev_col
= edit_get_col (edit
);
2636 edit
->search_start
= edit
->curs1
;
2637 edit_find_bracket (edit
);
2644 case CK_Begin_Page_Highlight
:
2645 case CK_End_Page_Highlight
:
2652 if ( edit
->mark2
>= 0 ) {
2653 if ( !option_persistent_selections
) {
2654 if (column_highlighting
)
2655 edit_push_action (edit
, COLUMN_ON
);
2656 column_highlighting
= 0;
2657 edit_mark_cmd (edit
, 1);
2665 case CK_Begin_Page_Highlight
:
2666 case CK_End_Page_Highlight
:
2671 case CK_Word_Left_Highlight
:
2672 case CK_Word_Right_Highlight
:
2673 case CK_Up_Highlight
:
2674 case CK_Down_Highlight
:
2675 case CK_Up_Alt_Highlight
:
2676 case CK_Down_Alt_Highlight
:
2677 if (edit
->mark2
== -1)
2678 break; /*marking is following the cursor: may need to highlight a whole line */
2681 case CK_Left_Highlight
:
2682 case CK_Right_Highlight
:
2683 edit
->force
|= REDRAW_CHAR_ONLY
;
2686 /* basic cursor key commands */
2689 /* if non persistent selection and text selected */
2690 if ( !option_persistent_selections
) {
2691 if ( edit
->mark1
!= edit
->mark2
) {
2692 edit_block_delete_cmd (edit
);
2696 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2700 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2701 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2703 edit_backspace (edit
, 1);
2706 if (option_fake_half_tabs
) {
2708 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2709 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2710 edit_backspace (edit
, 1);
2715 edit_backspace (edit
, 0);
2718 /* if non persistent selection and text selected */
2719 if ( !option_persistent_selections
) {
2720 if ( edit
->mark1
!= edit
->mark2
) {
2721 edit_block_delete_cmd (edit
);
2726 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2727 edit_insert_over (edit
);
2729 if (option_fake_half_tabs
) {
2731 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2732 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2733 edit_delete (edit
, 1);
2737 edit_delete (edit
, 0);
2739 case CK_Delete_Word_Left
:
2741 edit_left_delete_word (edit
);
2743 case CK_Delete_Word_Right
:
2744 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2745 edit_insert_over (edit
);
2747 edit_right_delete_word (edit
);
2749 case CK_Delete_Line
:
2750 edit_delete_line (edit
);
2752 case CK_Delete_To_Line_End
:
2753 edit_delete_to_line_end (edit
);
2755 case CK_Delete_To_Line_Begin
:
2756 edit_delete_to_line_begin (edit
);
2760 if (option_auto_para_formatting
) {
2761 edit_double_newline (edit
);
2762 if (option_return_does_auto_indent
)
2763 edit_auto_indent (edit
);
2764 format_paragraph (edit
, 0);
2766 edit_insert (edit
, '\n');
2767 if (option_return_does_auto_indent
) {
2768 edit_auto_indent (edit
);
2773 edit_insert (edit
, '\n');
2776 case CK_Page_Up_Alt_Highlight
:
2777 column_highlighting
= 1;
2779 case CK_Page_Up_Highlight
:
2780 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2782 case CK_Page_Down_Alt_Highlight
:
2783 column_highlighting
= 1;
2785 case CK_Page_Down_Highlight
:
2786 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2788 case CK_Left_Alt_Highlight
:
2789 column_highlighting
= 1;
2791 case CK_Left_Highlight
:
2792 if (option_fake_half_tabs
) {
2793 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2794 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2797 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2798 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2802 edit_left_char_move_cmd (edit
);
2804 case CK_Right_Alt_Highlight
:
2805 column_highlighting
= 1;
2807 case CK_Right_Highlight
:
2808 if (option_fake_half_tabs
) {
2809 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2810 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2811 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2815 edit_right_char_move_cmd (edit
);
2818 case CK_Begin_Page_Highlight
:
2819 edit_begin_page (edit
);
2822 case CK_End_Page_Highlight
:
2823 edit_end_page (edit
);
2826 case CK_Word_Left_Highlight
:
2828 edit_left_word_move_cmd (edit
);
2831 case CK_Word_Right_Highlight
:
2833 edit_right_word_move_cmd (edit
);
2835 case CK_Up_Alt_Highlight
:
2836 column_highlighting
= 1;
2838 case CK_Up_Highlight
:
2839 edit_move_up (edit
, 1, 0);
2841 case CK_Down_Alt_Highlight
:
2842 column_highlighting
= 1;
2844 case CK_Down_Highlight
:
2845 edit_move_down (edit
, 1, 0);
2847 case CK_Paragraph_Up_Alt_Highlight
:
2848 column_highlighting
= 1;
2849 case CK_Paragraph_Up
:
2850 case CK_Paragraph_Up_Highlight
:
2851 edit_move_up_paragraph (edit
, 0);
2853 case CK_Paragraph_Down_Alt_Highlight
:
2854 column_highlighting
= 1;
2855 case CK_Paragraph_Down
:
2856 case CK_Paragraph_Down_Highlight
:
2857 edit_move_down_paragraph (edit
, 0);
2859 case CK_Scroll_Up_Alt_Highlight
:
2860 column_highlighting
= 1;
2862 case CK_Scroll_Up_Highlight
:
2863 edit_move_up (edit
, 1, 1);
2865 case CK_Scroll_Down_Alt_Highlight
:
2866 column_highlighting
= 1;
2867 case CK_Scroll_Down
:
2868 case CK_Scroll_Down_Highlight
:
2869 edit_move_down (edit
, 1, 1);
2872 case CK_Home_Highlight
:
2873 edit_cursor_to_bol (edit
);
2876 case CK_End_Highlight
:
2877 edit_cursor_to_eol (edit
);
2880 /* if text marked shift block */
2881 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2882 if (edit
->mark2
< 0)
2883 edit_mark_cmd (edit
, 0);
2884 edit_move_block_to_right (edit
);
2886 if ( option_cursor_beyond_eol
)
2887 edit_insert_over (edit
);
2888 edit_tab_cmd (edit
);
2889 if (option_auto_para_formatting
) {
2890 format_paragraph (edit
, 0);
2891 edit
->force
|= REDRAW_PAGE
;
2893 check_and_wrap_line (edit
);
2898 case CK_Toggle_Insert
:
2899 edit
->overwrite
= (edit
->overwrite
== 0);
2903 if (edit
->mark2
>= 0) {
2904 if (column_highlighting
)
2905 edit_push_action (edit
, COLUMN_ON
);
2906 column_highlighting
= 0;
2908 edit_mark_cmd (edit
, 0);
2910 case CK_Column_Mark
:
2911 if (!column_highlighting
)
2912 edit_push_action (edit
, COLUMN_OFF
);
2913 column_highlighting
= 1;
2914 edit_mark_cmd (edit
, 0);
2917 if (column_highlighting
)
2918 edit_push_action (edit
, COLUMN_ON
);
2919 column_highlighting
= 0;
2920 edit_mark_cmd (edit
, 1);
2923 case CK_Toggle_Line_State
:
2924 option_line_state
= !option_line_state
;
2925 if ( option_line_state
) {
2926 option_line_state_width
= LINE_STATE_WIDTH
;
2928 option_line_state_width
= 0;
2930 edit
->force
|= REDRAW_PAGE
;
2933 case CK_Toggle_Bookmark
:
2934 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2935 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2936 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2938 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2940 case CK_Flush_Bookmarks
:
2941 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2942 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2943 edit
->force
|= REDRAW_PAGE
;
2945 case CK_Next_Bookmark
:
2946 if (edit
->book_mark
) {
2947 struct _book_mark
*p
;
2948 p
= (struct _book_mark
*) book_mark_find (edit
,
2952 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2953 || p
->line
< edit
->start_line
)
2954 edit_move_display (edit
,
2956 edit
->num_widget_lines
/ 2);
2957 edit_move_to_line (edit
, p
->line
);
2961 case CK_Prev_Bookmark
:
2962 if (edit
->book_mark
) {
2963 struct _book_mark
*p
;
2964 p
= (struct _book_mark
*) book_mark_find (edit
,
2966 while (p
->line
== edit
->curs_line
)
2970 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2971 || p
->line
< edit
->start_line
)
2972 edit_move_display (edit
,
2974 edit
->num_widget_lines
/ 2);
2975 edit_move_to_line (edit
, p
->line
);
2980 case CK_Beginning_Of_Text
:
2981 case CK_Beginning_Of_Text_Highlight
:
2982 edit_move_to_top (edit
);
2984 case CK_End_Of_Text
:
2985 case CK_End_Of_Text_Highlight
:
2986 edit_move_to_bottom (edit
);
2990 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2991 edit_insert_over (edit
);
2992 edit_block_copy_cmd (edit
);
2995 edit_block_delete_cmd (edit
);
2998 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2999 edit_insert_over (edit
);
3000 edit_block_move_cmd (edit
);
3003 case CK_Shift_Block_Left
:
3004 if (edit
->mark1
!= edit
->mark2
)
3005 edit_move_block_to_left (edit
);
3007 case CK_Shift_Block_Right
:
3008 if (edit
->mark1
!= edit
->mark2
)
3009 edit_move_block_to_right (edit
);
3012 edit_copy_to_X_buf_cmd (edit
);
3015 edit_cut_to_X_buf_cmd (edit
);
3018 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3019 edit_insert_over (edit
);
3020 edit_paste_from_X_buf_cmd (edit
);
3022 case CK_Selection_History
:
3023 edit_paste_from_history (edit
);
3027 edit_save_as_cmd (edit
);
3030 edit_save_confirm_cmd (edit
);
3033 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3036 edit_save_block_cmd (edit
);
3038 case CK_Insert_File
:
3039 edit_insert_file_cmd (edit
);
3042 case CK_Load_Prev_File
:
3043 edit_load_back_cmd (edit
);
3045 case CK_Load_Next_File
:
3046 edit_load_forward_cmd (edit
);
3049 case CK_Load_Syntax_File
:
3050 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3052 case CK_Choose_Syntax
:
3053 edit_syntax_dialog ();
3056 case CK_Load_Menu_File
:
3057 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3060 case CK_Toggle_Syntax
:
3061 if ((option_syntax_highlighting
^= 1) == 1)
3062 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3063 edit
->force
|= REDRAW_PAGE
;
3066 case CK_Toggle_Tab_TWS
:
3067 enable_show_tabs_tws
^= 1;
3068 edit
->force
|= REDRAW_PAGE
;
3072 edit_search_cmd (edit
, 0);
3075 edit_search_cmd (edit
, 1);
3078 edit_replace_cmd (edit
, 0);
3080 case CK_Replace_Again
:
3081 edit_replace_cmd (edit
, 1);
3083 case CK_Complete_Word
:
3084 /* if text marked shift block */
3085 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3086 edit_move_block_to_left (edit
);
3088 edit_complete_word_cmd (edit
);
3091 case CK_Find_Definition
:
3092 edit_get_match_keyword_cmd (edit
);
3095 dlg_stop (edit
->widget
.parent
);
3098 edit_new_cmd (edit
);
3101 edit_help_cmd (edit
);
3104 edit_refresh_cmd (edit
);
3106 case CK_SaveSetupCmd
:
3110 query_dialog (_(" About "),
3111 _("\n Cooledit v3.11.5\n\n"
3112 " Copyright (C) 1996 the Free Software Foundation\n\n"
3113 " A user friendly text editor written\n"
3114 " for the Midnight Commander.\n"), D_NORMAL
,
3120 case CK_Edit_Options
:
3121 edit_options_dialog ();
3123 case CK_Edit_Save_Mode
:
3124 menu_save_mode_cmd ();
3129 /* fool gcc to prevent a Y2K warning */
3130 char time_format
[] = "_c";
3131 time_format
[0] = '%';
3133 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3134 edit_print_string (edit
, s
);
3135 edit
->force
|= REDRAW_PAGE
;
3140 edit_goto_cmd (edit
);
3142 case CK_Paragraph_Format
:
3143 format_paragraph (edit
, 1);
3144 edit
->force
|= REDRAW_PAGE
;
3146 case CK_Delete_Macro
:
3147 edit_delete_macro_cmd (edit
);
3149 case CK_Match_Bracket
:
3150 edit_goto_matching_bracket (edit
);
3156 edit_sort_cmd (edit
);
3159 edit_ext_cmd (edit
);
3162 edit_mail_dialog (edit
);
3167 case CK_SelectCodepage
:
3168 edit_select_codepage_cmd (edit
);
3170 case CK_Insert_Literal
:
3171 edit_insert_literal_cmd (edit
);
3173 case CK_Execute_Macro
:
3174 edit_execute_macro_cmd (edit
);
3176 case CK_Begin_End_Macro
:
3177 edit_begin_end_macro_cmd (edit
);
3187 if ((command
/ 1000) == 1) /* a shell command */
3188 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3189 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3190 struct macro m
[MAX_MACRO_LENGTH
];
3192 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3193 edit_execute_macro (edit
, m
, nm
);
3196 /* keys which must set the col position, and the search vars */
3201 case CK_Replace_Again
:
3202 case CK_Complete_Word
:
3203 edit
->prev_col
= edit_get_col (edit
);
3206 case CK_Up_Highlight
:
3207 case CK_Up_Alt_Highlight
:
3209 case CK_Down_Highlight
:
3210 case CK_Down_Alt_Highlight
:
3212 case CK_Page_Up_Highlight
:
3213 case CK_Page_Up_Alt_Highlight
:
3215 case CK_Page_Down_Highlight
:
3216 case CK_Page_Down_Alt_Highlight
:
3217 case CK_Beginning_Of_Text
:
3218 case CK_Beginning_Of_Text_Highlight
:
3219 case CK_End_Of_Text
:
3220 case CK_End_Of_Text_Highlight
:
3221 case CK_Paragraph_Up
:
3222 case CK_Paragraph_Up_Highlight
:
3223 case CK_Paragraph_Up_Alt_Highlight
:
3224 case CK_Paragraph_Down
:
3225 case CK_Paragraph_Down_Highlight
:
3226 case CK_Paragraph_Down_Alt_Highlight
:
3228 case CK_Scroll_Up_Highlight
:
3229 case CK_Scroll_Up_Alt_Highlight
:
3230 case CK_Scroll_Down
:
3231 case CK_Scroll_Down_Highlight
:
3232 case CK_Scroll_Down_Alt_Highlight
:
3233 edit
->search_start
= edit
->curs1
;
3234 edit
->found_len
= 0;
3237 edit
->found_len
= 0;
3238 edit
->prev_col
= edit_get_col (edit
);
3239 edit
->search_start
= edit
->curs1
;
3241 edit_find_bracket (edit
);
3243 if (option_auto_para_formatting
) {
3247 case CK_Delete_Word_Left
:
3248 case CK_Delete_Word_Right
:
3249 case CK_Delete_To_Line_End
:
3250 case CK_Delete_To_Line_Begin
:
3251 format_paragraph (edit
, 0);
3252 edit
->force
|= REDRAW_PAGE
;
3259 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3263 if (edit
->macro_depth
++ > 256) {
3264 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3265 edit
->macro_depth
--;
3268 edit
->force
|= REDRAW_PAGE
;
3269 for (; i
< n
; i
++) {
3270 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3272 edit_update_screen (edit
);
3273 edit
->macro_depth
--;
3276 /* User edit menu, like user menu (F2) but only in editor. */
3278 user_menu (WEdit
* edit
)
3282 long start_mark
, end_mark
;
3285 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3287 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3289 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3291 /* run shell scripts from menu */
3292 user_menu_cmd (edit
);
3294 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0)) {
3299 /* i.e. we have marked block */
3300 rc
= edit_block_delete_cmd (edit
);
3304 edit_insert_file (edit
, block_file
);
3306 /* truncate block file */
3307 fd
= fopen (block_file
, "w");
3311 edit_refresh_cmd (edit
);
3312 edit
->force
|= REDRAW_COMPLETELY
;
3314 g_free (block_file
);
3318 edit_stack_init (void)
3320 for (edit_stack_iterator
= 0;
3321 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3322 edit_stack_iterator
++ ) {
3323 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3324 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3327 edit_stack_iterator
= 0;
3331 edit_stack_free (void)
3333 for (edit_stack_iterator
= 0;
3334 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3335 edit_stack_iterator
++)
3336 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3340 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
3342 edit_move_updown (edit
, i
, scroll
, TRUE
);
3346 void edit_move_down (WEdit
* edit
, unsigned long i
, int scroll
)
3348 edit_move_updown (edit
, i
, scroll
, FALSE
);