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' };
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \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
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
116 * file end|||file beginning
125 const global_keymap_t
*editor_map
;
126 const global_keymap_t
*editor_x_map
;
128 static void user_menu (WEdit
* edit
);
131 edit_get_byte (WEdit
* edit
, long byte_index
)
134 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
137 if (byte_index
>= edit
->curs1
)
139 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
140 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
144 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
149 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
152 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
155 if (byte_index
>= edit
->curs1
)
157 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
158 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
159 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
163 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
164 (byte_index
& M_EDIT_BUF_SIZE
));
169 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
173 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
179 if (byte_index
>= edit
->curs1
)
181 p
= edit
->curs1
+ edit
->curs2
- 1;
182 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
183 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
187 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
192 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
197 gchar
*next_ch
= NULL
;
200 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
206 str
= edit_get_byte_ptr (edit
, byte_index
);
214 res
= g_utf8_get_char_validated (str
, -1);
224 /* Calculate UTF-8 char width */
225 next_ch
= g_utf8_next_char (str
);
228 width
= next_ch
- str
;
241 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
243 gchar
*str
, *buf
= NULL
;
246 gchar
*next_ch
= NULL
;
252 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
258 ch
= edit_get_utf (edit
, byte_index
, &width
);
266 str
= edit_get_byte_ptr (edit
, byte_index
);
267 buf
= edit_get_buf_ptr (edit
, byte_index
);
268 if (str
== NULL
|| buf
== NULL
)
273 /* get prev utf8 char */
275 str
= g_utf8_find_prev_char (buf
, str
);
277 res
= g_utf8_get_char_validated (str
, -1);
287 /* Calculate UTF-8 char width */
288 next_ch
= g_utf8_next_char (str
);
291 width
= next_ch
- str
;
304 * Initialize the buffers for an empty files.
307 edit_init_buffers (WEdit
* edit
)
311 for (j
= 0; j
<= MAXBUFF
; j
++)
313 edit
->buffers1
[j
] = NULL
;
314 edit
->buffers2
[j
] = NULL
;
319 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
323 * Load file OR text into buffers. Set cursor to the beginning of file.
327 edit_load_file_fast (WEdit
* edit
, const char *filename
)
332 edit
->curs2
= edit
->last_byte
;
333 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
335 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
338 GString
*errmsg
= g_string_new (NULL
);
339 g_string_sprintf (errmsg
, _(" Cannot open %s for reading "), filename
);
340 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
341 g_string_free (errmsg
, TRUE
);
345 if (!edit
->buffers2
[buf2
])
346 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
351 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
352 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
355 for (buf
= buf2
- 1; buf
>= 0; buf
--)
357 /* edit->buffers2[0] is already allocated */
358 if (!edit
->buffers2
[buf
])
359 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
360 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
368 char *err_str
= g_strdup_printf (_(" Error reading %s "), filename
);
369 edit_error_dialog (_("Error"), err_str
);
376 /* detecting an error on save is easy: just check if every byte has been written. */
377 /* detecting an error on read, is not so easy 'cos there is not way to tell
378 whether you read everything or not. */
379 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
380 static const struct edit_filters
382 const char *read
, *write
, *extension
;
386 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
387 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
388 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
389 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
390 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
394 /* Return index of the filter or -1 is there is no appropriate filter */
396 edit_find_filter (const char *filename
)
400 if (filename
== NULL
)
403 l
= strlen (filename
);
404 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
406 e
= strlen (all_filters
[i
].extension
);
408 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
415 edit_get_filter (const char *filename
)
418 char *p
, *quoted_name
;
420 i
= edit_find_filter (filename
);
424 quoted_name
= name_quote (filename
, 0);
425 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
426 g_free (quoted_name
);
431 edit_get_write_filter (const char *write_name
, const char *filename
)
436 i
= edit_find_filter (filename
);
440 writename
= name_quote (write_name
, 0);
441 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
447 edit_insert_stream (WEdit
* edit
, FILE * f
)
451 while ((c
= fgetc (f
)) >= 0)
453 edit_insert (edit
, c
);
460 edit_write_stream (WEdit
* edit
, FILE * f
)
464 if (edit
->lb
== LB_ASIS
)
466 for (i
= 0; i
< edit
->last_byte
; i
++)
467 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
472 /* change line breaks */
473 for (i
= 0; i
< edit
->last_byte
; i
++)
475 unsigned char c
= edit_get_byte (edit
, i
);
477 if (!(c
== '\n' || c
== '\r'))
480 if (fputc (c
, f
) < 0)
484 { /* (c == '\n' || c == '\r') */
485 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
489 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
490 /* put one line break unconditionally */
491 if (fputc ('\n', f
) < 0)
494 i
++; /* 2 chars are processed */
496 if (c
== '\r' && c1
== '\n')
497 /* Windows line break; go to the next char */
500 if (c
== '\r' && c1
== '\r')
502 /* two Macintosh line breaks; put second line break */
503 if (fputc ('\n', f
) < 0)
508 if (fputc (c1
, f
) < 0)
512 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
513 /* put one line break unconditionally */
514 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
517 if (c
== '\r' && c1
== '\n')
518 /* Windows line break; go to the next char */
522 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
523 /* put one line break unconditionally */
524 if (fputc ('\r', f
) < 0)
527 i
++; /* 2 chars are processed */
529 if (c
== '\r' && c1
== '\n')
530 /* Windows line break; go to the next char */
533 if (c
== '\n' && c1
== '\n')
535 /* two Windows line breaks; put second line break */
536 if (fputc ('\r', f
) < 0)
541 if (fputc (c1
, f
) < 0)
544 case LB_ASIS
: /* default without changes */
550 return edit
->last_byte
;
553 #define TEMP_BUF_LEN 1024
555 /* inserts a file at the cursor, returns 1 on success */
557 edit_insert_file (WEdit
* edit
, const char *filename
)
561 p
= edit_get_filter (filename
);
565 long current
= edit
->curs1
;
566 f
= (FILE *) popen (p
, "r");
569 edit_insert_stream (edit
, f
);
570 edit_cursor_move (edit
, current
- edit
->curs1
);
574 errmsg
= g_strdup_printf (_(" Error reading from pipe: %s "), p
);
575 edit_error_dialog (_("Error"), errmsg
);
584 errmsg
= g_strdup_printf (_(" Cannot open pipe for reading: %s "), p
);
585 edit_error_dialog (_("Error"), errmsg
);
594 int i
, file
, blocklen
;
595 long current
= edit
->curs1
;
596 int vertical_insertion
= 0;
598 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
601 buf
= g_malloc0 (TEMP_BUF_LEN
);
602 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
605 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
606 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
607 vertical_insertion
= 1;
609 mc_lseek (file
, 0, SEEK_SET
);
611 if (vertical_insertion
)
612 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
614 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
615 for (i
= 0; i
< blocklen
; i
++)
616 edit_insert (edit
, buf
[i
]);
617 edit_cursor_move (edit
, current
- edit
->curs1
);
626 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
628 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
631 GString
*errmsg
= (GString
*) 0;
633 /* Try opening an existing file */
634 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
639 * Try creating the file. O_EXCL prevents following broken links
640 * and opening existing files.
642 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
645 g_string_sprintf (errmsg
= g_string_new (NULL
),
646 _(" Cannot open %s for reading "), filename
);
651 /* New file, delete it if it's not modified or saved */
652 edit
->delete_file
= 1;
656 /* Check what we have opened */
657 if (mc_fstat (file
, st
) < 0)
659 g_string_sprintf (errmsg
= g_string_new (NULL
),
660 _(" Cannot get size/permissions for %s "), filename
);
664 /* We want to open regular files only */
665 if (!S_ISREG (st
->st_mode
))
667 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" %s is not a regular file "), filename
);
672 * Don't delete non-empty files.
673 * O_EXCL should prevent it, but let's be on the safe side.
677 edit
->delete_file
= 0;
680 if (st
->st_size
>= SIZE_LIMIT
)
682 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" File %s is too large "), filename
);
686 (void) mc_close (file
);
689 edit_error_dialog (_("Error"), errmsg
->str
);
690 g_string_free (errmsg
, TRUE
);
697 * Open the file and load it into the buffers, either directly or using
698 * a filter. Return 0 on success, 1 on error.
700 * Fast loading (edit_load_file_fast) is used when the file size is
701 * known. In this case the data is read into the buffers by blocks.
702 * If the file size is not known, the data is loaded byte by byte in
706 edit_load_file (WEdit
* edit
)
710 /* Cannot do fast load if a filter is used */
711 if (edit_find_filter (edit
->filename
) >= 0)
715 * VFS may report file size incorrectly, and slow load is not a big
716 * deal considering overhead in VFS.
718 if (!vfs_file_is_local (edit
->filename
))
722 * FIXME: line end translation should disable fast loading as well
723 * Consider doing fseek() to the end and ftell() for the real size.
728 /* If we are dealing with a real file, check that it exists */
729 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
734 /* nothing to load */
738 edit_init_buffers (edit
);
742 edit
->last_byte
= edit
->stat1
.st_size
;
743 edit_load_file_fast (edit
, edit
->filename
);
744 /* If fast load was used, the number of lines wasn't calculated */
745 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
750 const char *codepage_id
;
755 edit
->stack_disable
= 1;
756 if (!edit_insert_file (edit
, edit
->filename
))
761 edit
->stack_disable
= 0;
765 codepage_id
= get_codepage_id (source_codepage
);
767 edit
->utf8
= str_isutf8 (codepage_id
);
774 /* Restore saved cursor position in the file */
776 edit_load_position (WEdit
* edit
)
782 if (!edit
->filename
|| !*edit
->filename
)
785 filename
= vfs_canon (edit
->filename
);
786 load_file_position (filename
, &line
, &column
, &offset
);
791 edit_move_to_line (edit
, line
- 1);
792 edit
->prev_col
= column
;
796 edit_cursor_move (edit
, offset
);
797 line
= edit
->curs_line
;
799 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
800 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
803 /* Save cursor position in the file */
805 edit_save_position (WEdit
* edit
)
809 if (!edit
->filename
|| !*edit
->filename
)
812 filename
= vfs_canon (edit
->filename
);
813 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
817 /* Clean the WEdit stricture except the widget part */
819 edit_purge_widget (WEdit
* edit
)
821 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
822 char *start
= (char *) edit
+ sizeof (Widget
);
823 memset (start
, 0, len
);
824 edit
->macro_i
= -1; /* not recording a macro */
828 edit_set_keymap (void)
830 editor_map
= default_editor_keymap
;
831 if (editor_keymap
&& editor_keymap
->len
> 0)
832 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
834 editor_x_map
= default_editor_x_keymap
;
835 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
836 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
840 #define space_width 1
843 * Fill in the edit structure. Return NULL on failure. Pass edit as
844 * NULL to allocate a new structure.
846 * If line is 0, try to restore saved position. Otherwise put the
847 * cursor on that line and show it in the middle of the screen.
850 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
853 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
854 if (option_line_state
)
856 option_line_state_width
= LINE_STATE_WIDTH
;
860 option_line_state_width
= 0;
866 * Expand option_whole_chars_search by national letters using
870 static char option_whole_chars_search_buf
[256];
872 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
875 size_t len
= str_term_width1 (option_whole_chars_search
);
877 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
879 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
881 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
883 option_whole_chars_search_buf
[len
++] = i
;
887 option_whole_chars_search_buf
[len
] = 0;
888 option_whole_chars_search
= option_whole_chars_search_buf
;
890 #endif /* ENABLE_NLS */
891 edit
= g_malloc0 (sizeof (WEdit
));
895 edit_purge_widget (edit
);
896 edit
->num_widget_lines
= lines
;
898 edit
->num_widget_columns
= columns
;
899 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
900 edit
->stat1
.st_uid
= getuid ();
901 edit
->stat1
.st_gid
= getgid ();
902 edit
->stat1
.st_mtime
= 0;
904 edit
->force
|= REDRAW_PAGE
;
905 edit_set_filename (edit
, filename
);
906 edit
->stack_size
= START_STACK_SIZE
;
907 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
908 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
909 if (edit_load_file (edit
))
911 /* edit_load_file already gives an error message */
917 edit
->converter
= str_cnv_from_term
;
920 const char *cp_id
= NULL
;
921 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
926 conv
= str_crt_conv_from (cp_id
);
927 if (conv
!= INVALID_CONV
)
929 if (edit
->converter
!= str_cnv_from_term
)
930 str_close_conv (edit
->converter
);
931 edit
->converter
= conv
;
935 edit
->utf8
= str_isutf8 (cp_id
);
939 edit
->loading_done
= 1;
942 edit_load_syntax (edit
, NULL
, NULL
);
945 edit_get_syntax_color (edit
, -1, &color
);
948 /* load saved cursor position */
949 if ((line
== 0) && option_save_position
)
951 edit_load_position (edit
);
957 edit_move_display (edit
, line
- 1);
958 edit_move_to_line (edit
, line
- 1);
966 /* Clear the edit struct, freeing everything in it. Return 1 on success */
968 edit_clean (WEdit
* edit
)
975 /* a stale lock, remove it */
977 edit
->locked
= edit_unlock_file (edit
->filename
);
979 /* save cursor position */
980 if (option_save_position
)
981 edit_save_position (edit
);
983 /* File specified on the mcedit command line and never saved */
984 if (edit
->delete_file
)
985 unlink (edit
->filename
);
987 edit_free_syntax_rules (edit
);
988 book_mark_flush (edit
, -1);
989 for (; j
<= MAXBUFF
; j
++)
991 g_free (edit
->buffers1
[j
]);
992 g_free (edit
->buffers2
[j
]);
995 g_free (edit
->undo_stack
);
996 g_free (edit
->filename
);
999 mc_search_free (edit
->search
);
1000 edit
->search
= NULL
;
1002 if (edit
->converter
!= str_cnv_from_term
)
1003 str_close_conv (edit
->converter
);
1005 edit_purge_widget (edit
);
1010 /* returns 1 on success */
1012 edit_renew (WEdit
* edit
)
1014 int lines
= edit
->num_widget_lines
;
1015 int columns
= edit
->num_widget_columns
;
1018 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
1022 * Load a new file into the editor. If it fails, preserve the old file.
1023 * To do it, allocate a new widget, initialize it and, if the new file
1024 * was loaded, copy the data to the old widget.
1025 * Return 1 on success, 0 on failure.
1028 edit_reload (WEdit
* edit
, const char *filename
)
1031 int lines
= edit
->num_widget_lines
;
1032 int columns
= edit
->num_widget_columns
;
1034 e
= g_malloc0 (sizeof (WEdit
));
1035 e
->widget
= edit
->widget
;
1036 if (!edit_init (e
, lines
, columns
, filename
, 0))
1042 memcpy (edit
, e
, sizeof (WEdit
));
1048 * Load a new file into the editor and set line. If it fails, preserve the old file.
1049 * To do it, allocate a new widget, initialize it and, if the new file
1050 * was loaded, copy the data to the old widget.
1051 * Return 1 on success, 0 on failure.
1054 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
1057 int lines
= edit
->num_widget_lines
;
1058 int columns
= edit
->num_widget_columns
;
1060 e
= g_malloc0 (sizeof (WEdit
));
1061 e
->widget
= edit
->widget
;
1062 if (!edit_init (e
, lines
, columns
, filename
, line
))
1068 memcpy (edit
, e
, sizeof (WEdit
));
1075 Recording stack for undo:
1076 The following is an implementation of a compressed stack. Identical
1077 pushes are recorded by a negative prefix indicating the number of times the
1078 same char was pushed. This saves space for repeated curs-left or curs-right
1095 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1096 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1097 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1098 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1101 The only way the cursor moves or the buffer is changed is through the routines:
1102 insert, backspace, insert_ahead, delete, and cursor_move.
1103 These record the reverse undo movements onto the stack each time they are
1106 Each key press results in a set of actions (insert; delete ...). So each time
1107 a key is pressed the current position of start_display is pushed as
1108 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1109 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1110 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1115 edit_push_action (WEdit
* edit
, long c
, ...)
1117 unsigned long sp
= edit
->stack_pointer
;
1121 /* first enlarge the stack if necessary */
1122 if (sp
> edit
->stack_size
- 10)
1124 if (option_max_undo
< 256)
1125 option_max_undo
= 256;
1126 if (edit
->stack_size
< (unsigned long) option_max_undo
)
1128 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1131 edit
->undo_stack
= t
;
1132 edit
->stack_size
<<= 1;
1133 edit
->stack_size_mask
= edit
->stack_size
- 1;
1137 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1138 if (edit
->stack_disable
)
1141 #ifdef FAST_MOVE_CURSOR
1142 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
1145 edit
->undo_stack
[sp
] = (c
== CURS_LEFT_LOTS
) ? CURS_LEFT
: CURS_RIGHT
;
1146 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1148 c
= -(va_arg (ap
, int));
1152 #endif /* ! FAST_MOVE_CURSOR */
1153 if (edit
->stack_bottom
!= sp
1154 && spm1
!= edit
->stack_bottom
1155 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
1158 if (edit
->undo_stack
[spm1
] < 0)
1160 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1163 if (edit
->undo_stack
[spm1
] > -1000000000)
1165 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1166 edit
->undo_stack
[spm1
]--;
1170 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1171 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1172 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1173 { /* a left then a right anihilate each other */
1174 if (edit
->undo_stack
[spm1
] == -2)
1175 edit
->stack_pointer
= spm1
;
1177 edit
->undo_stack
[spm1
]++;
1184 d
= edit
->undo_stack
[spm1
];
1188 return; /* --> no need to push multiple do-nothings */
1189 edit
->undo_stack
[sp
] = -2;
1192 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1193 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1194 { /* a left then a right anihilate each other */
1195 edit
->stack_pointer
= spm1
;
1201 edit
->undo_stack
[sp
] = c
;
1204 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1206 /* if the sp wraps round and catches the stack_bottom then erase
1207 * the first set of actions on the stack to make space - by moving
1208 * stack_bottom forward one "key press" */
1209 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1210 if ((unsigned long) c
== edit
->stack_bottom
||
1211 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1214 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1216 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
1217 && edit
->stack_bottom
!= edit
->stack_pointer
);
1219 /*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: */
1220 if (edit
->stack_pointer
!= edit
->stack_bottom
1221 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1222 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1226 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1227 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1230 pop_action (WEdit
* edit
)
1233 unsigned long sp
= edit
->stack_pointer
;
1235 if (sp
== edit
->stack_bottom
)
1236 return STACK_BOTTOM
;
1238 sp
= (sp
- 1) & edit
->stack_size_mask
;
1239 c
= edit
->undo_stack
[sp
];
1242 /* edit->undo_stack[sp] = '@'; */
1243 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1247 if (sp
== edit
->stack_bottom
)
1248 return STACK_BOTTOM
;
1250 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1251 if (edit
->undo_stack
[sp
] == -2)
1253 /* edit->undo_stack[sp] = '@'; */
1254 edit
->stack_pointer
= sp
;
1257 edit
->undo_stack
[sp
]++;
1262 /* is called whenever a modification is made by one of the four routines below */
1264 edit_modification (WEdit
* edit
)
1266 edit
->caches_valid
= 0;
1267 edit
->screen_modified
= 1;
1269 /* raise lock when file modified */
1270 if (!edit
->modified
&& !edit
->delete_file
)
1271 edit
->locked
= edit_lock_file (edit
->filename
);
1276 Basic low level single character buffer alterations and movements at the cursor.
1277 Returns char passed over, inserted or removed.
1281 edit_insert (WEdit
* edit
, int c
)
1283 /* check if file has grown to large */
1284 if (edit
->last_byte
>= SIZE_LIMIT
)
1287 /* first we must update the position of the display window */
1288 if (edit
->curs1
< edit
->start_display
)
1290 edit
->start_display
++;
1295 /* Mark file as modified, unless the file hasn't been fully loaded */
1296 if (edit
->loading_done
)
1298 edit_modification (edit
);
1301 /* now we must update some info on the file and check if a redraw is required */
1304 if (edit
->book_mark
)
1305 book_mark_inc (edit
, edit
->curs_line
);
1307 edit
->total_lines
++;
1308 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1311 /* save the reverse command onto the undo stack */
1312 edit_push_action (edit
, BACKSPACE
);
1314 /* update markers */
1315 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1316 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1317 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1319 /* add a new buffer if we've reached the end of the last one */
1320 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1321 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1323 /* perform the insertion */
1324 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
1325 = (unsigned char) c
;
1327 /* update file length */
1330 /* update cursor position */
1335 edit_insert_over (WEdit
* edit
)
1339 for (i
= 0; i
< edit
->over_col
; i
++)
1341 edit_insert (edit
, ' ');
1346 /* same as edit_insert and move left */
1348 edit_insert_ahead (WEdit
* edit
, int c
)
1350 if (edit
->last_byte
>= SIZE_LIMIT
)
1353 if (edit
->curs1
< edit
->start_display
)
1355 edit
->start_display
++;
1359 edit_modification (edit
);
1362 if (edit
->book_mark
)
1363 book_mark_inc (edit
, edit
->curs_line
);
1364 edit
->total_lines
++;
1365 edit
->force
|= REDRAW_AFTER_CURSOR
;
1367 edit_push_action (edit
, DELCHAR
);
1369 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1370 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1371 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1373 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1374 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1375 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
1376 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1384 edit_delete (WEdit
* edit
, const int byte_delete
)
1393 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1394 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1395 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1398 /* if byte_delete = 1 then delete only one byte not multibyte char */
1399 if (edit
->utf8
&& byte_delete
== 0)
1401 edit_get_utf (edit
, edit
->curs1
, &cw
);
1405 for (i
= 1; i
<= cw
; i
++)
1407 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1409 1) & M_EDIT_BUF_SIZE
) - 1];
1411 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1413 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1414 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1418 edit_push_action (edit
, p
+ 256);
1421 edit_modification (edit
);
1424 if (edit
->book_mark
)
1425 book_mark_dec (edit
, edit
->curs_line
);
1426 edit
->total_lines
--;
1427 edit
->force
|= REDRAW_AFTER_CURSOR
;
1429 if (edit
->curs1
< edit
->start_display
)
1431 edit
->start_display
--;
1441 edit_backspace (WEdit
* edit
, const int byte_delete
)
1450 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1451 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1452 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1455 if (edit
->utf8
&& byte_delete
== 0)
1457 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1461 for (i
= 1; i
<= cw
; i
++)
1463 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
1464 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1465 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1467 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1468 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1472 edit_push_action (edit
, p
);
1474 edit_modification (edit
);
1477 if (edit
->book_mark
)
1478 book_mark_dec (edit
, edit
->curs_line
);
1480 edit
->total_lines
--;
1481 edit
->force
|= REDRAW_AFTER_CURSOR
;
1484 if (edit
->curs1
< edit
->start_display
)
1486 edit
->start_display
--;
1494 #ifdef FAST_MOVE_CURSOR
1497 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1500 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
1503 next
-= (unsigned long) dest
;
1511 edit_move_backward_lots (WEdit
* edit
, long increment
)
1514 unsigned char *p
= NULL
;
1516 if (increment
> edit
->curs1
)
1517 increment
= edit
->curs1
;
1520 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1522 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1525 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1530 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1531 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
1538 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1539 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1540 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1541 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1544 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1545 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1546 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1551 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1554 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1556 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1563 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1574 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1575 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
1580 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1581 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1584 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1586 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1587 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1592 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1595 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1597 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1602 return edit_get_byte (edit
, edit
->curs1
);
1605 #endif /* ! FAST_MOVE_CURSOR */
1607 /* moves the cursor right or left: increment positive or negative respectively */
1609 edit_cursor_move (WEdit
* edit
, long increment
)
1611 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1613 #ifdef FAST_MOVE_CURSOR
1614 if (increment
< -256)
1616 edit
->force
|= REDRAW_PAGE
;
1617 edit_move_backward_lots (edit
, -increment
);
1620 #endif /* ! FAST_MOVE_CURSOR */
1624 for (; increment
< 0; increment
++)
1629 edit_push_action (edit
, CURS_RIGHT
);
1631 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1632 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1633 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1634 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1635 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1637 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
1638 1) & M_EDIT_BUF_SIZE
];
1639 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1641 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1642 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1648 edit
->force
|= REDRAW_LINE_BELOW
;
1653 else if (increment
> 0)
1655 for (; increment
> 0; increment
--)
1660 edit_push_action (edit
, CURS_LEFT
);
1662 c
= edit_get_byte (edit
, edit
->curs1
);
1663 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1664 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1665 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1667 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1669 1) & M_EDIT_BUF_SIZE
) - 1];
1670 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1672 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1673 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1679 edit
->force
|= REDRAW_LINE_ABOVE
;
1685 /* These functions return positions relative to lines */
1687 /* returns index of last char on line + 1 */
1689 edit_eol (WEdit
* edit
, long current
)
1691 if (current
>= edit
->last_byte
)
1692 return edit
->last_byte
;
1695 if (edit_get_byte (edit
, current
) == '\n')
1700 /* returns index of first char on line */
1702 edit_bol (WEdit
* edit
, long current
)
1708 if (edit_get_byte (edit
, current
- 1) == '\n')
1715 edit_count_lines (WEdit
* edit
, long current
, long upto
)
1718 if (upto
> edit
->last_byte
)
1719 upto
= edit
->last_byte
;
1722 while (current
< upto
)
1723 if (edit_get_byte (edit
, current
++) == '\n')
1729 /* If lines is zero this returns the count of lines from current to upto. */
1730 /* If upto is zero returns index of lines forward current. */
1732 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1736 return edit_count_lines (edit
, current
, upto
);
1745 next
= edit_eol (edit
, current
) + 1;
1746 if (next
> edit
->last_byte
)
1756 /* Returns offset of 'lines' lines up from current */
1758 edit_move_backward (WEdit
* edit
, long current
, long lines
)
1762 current
= edit_bol (edit
, current
);
1763 while ((lines
--) && current
!= 0)
1764 current
= edit_bol (edit
, current
- 1);
1768 /* If cols is zero this returns the count of columns from current to upto. */
1769 /* If upto is zero returns index of cols across from current. */
1771 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1782 q
= edit
->last_byte
+ 2;
1784 for (col
= 0, p
= current
; p
< q
; p
++)
1798 orig_c
= c
= edit_get_byte (edit
, p
);
1802 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1807 if (g_unichar_iswide (utf_ch
))
1810 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1814 c
= convert_to_display_c (c
);
1816 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1824 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1825 /* '\r' is shown as ^M, so we must advance 2 characters */
1826 /* Caret notation for control characters */
1834 /* returns the current column position of the cursor */
1836 edit_get_col (WEdit
* edit
)
1838 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1842 /* Scrolling functions */
1845 edit_update_curs_row (WEdit
* edit
)
1847 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1851 edit_update_curs_col (WEdit
* edit
)
1853 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1857 edit_get_curs_col (const WEdit
* edit
)
1859 return edit
->curs_col
;
1862 /*moves the display start position up by i lines */
1864 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1866 unsigned long lines_above
= edit
->start_line
;
1867 if (i
> lines_above
)
1871 edit
->start_line
-= i
;
1872 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1873 edit
->force
|= REDRAW_PAGE
;
1874 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1876 edit_update_curs_row (edit
);
1880 /* returns 1 if could scroll, 0 otherwise */
1882 edit_scroll_downward (WEdit
* edit
, int i
)
1885 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1886 if (lines_below
> 0)
1888 if (i
> lines_below
)
1890 edit
->start_line
+= i
;
1891 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1892 edit
->force
|= REDRAW_PAGE
;
1893 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1895 edit_update_curs_row (edit
);
1899 edit_scroll_right (WEdit
* edit
, int i
)
1901 edit
->force
|= REDRAW_PAGE
;
1902 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1903 edit
->start_col
-= i
;
1907 edit_scroll_left (WEdit
* edit
, int i
)
1909 if (edit
->start_col
)
1911 edit
->start_col
+= i
;
1912 if (edit
->start_col
> 0)
1913 edit
->start_col
= 0;
1914 edit
->force
|= REDRAW_PAGE
;
1915 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1919 /* high level cursor movement commands */
1922 is_in_indent (WEdit
* edit
)
1924 long p
= edit_bol (edit
, edit
->curs1
);
1925 while (p
< edit
->curs1
)
1926 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1931 static int left_of_four_spaces (WEdit
* edit
);
1934 edit_move_to_prev_col (WEdit
* edit
, long p
)
1936 int prev
= edit
->prev_col
;
1937 int over
= edit
->over_col
;
1938 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1940 if (option_cursor_beyond_eol
)
1942 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
1943 edit_eol (edit
, edit
->curs1
));
1945 if (line_len
< prev
+ edit
->over_col
)
1947 edit
->over_col
= prev
+ over
- line_len
;
1948 edit
->prev_col
= line_len
;
1949 edit
->curs_col
= line_len
;
1953 edit
->curs_col
= prev
+ over
;
1954 edit
->prev_col
= edit
->curs_col
;
1961 if (is_in_indent (edit
) && option_fake_half_tabs
)
1963 edit_update_curs_col (edit
);
1965 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
1967 int q
= edit
->curs_col
;
1968 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1969 p
= edit_bol (edit
, edit
->curs1
);
1970 edit_cursor_move (edit
,
1971 edit_move_forward3 (edit
, p
, edit
->curs_col
,
1973 if (!left_of_four_spaces (edit
))
1974 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1981 is_blank (WEdit
* edit
, long offset
)
1985 s
= edit_bol (edit
, offset
);
1986 f
= edit_eol (edit
, offset
) - 1;
1989 c
= edit_get_byte (edit
, s
++);
1997 /* returns the offset of line i */
1999 edit_find_line (WEdit
* edit
, int line
)
2003 if (!edit
->caches_valid
)
2005 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2006 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
2007 /* three offsets that we *know* are line 0 at 0 and these two: */
2008 edit
->line_numbers
[1] = edit
->curs_line
;
2009 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
2010 edit
->line_numbers
[2] = edit
->total_lines
;
2011 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
2012 edit
->caches_valid
= 1;
2014 if (line
>= edit
->total_lines
)
2015 return edit
->line_offsets
[2];
2018 /* find the closest known point */
2019 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2022 n
= abs (edit
->line_numbers
[i
] - line
);
2030 return edit
->line_offsets
[j
]; /* know the offset exactly */
2031 if (m
== 1 && j
>= 3)
2032 i
= j
; /* one line different - caller might be looping, so stay in this cache */
2034 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
2035 if (line
> edit
->line_numbers
[j
])
2036 edit
->line_offsets
[i
] =
2037 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
2039 edit
->line_offsets
[i
] =
2040 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
2041 edit
->line_numbers
[i
] = line
;
2042 return edit
->line_offsets
[i
];
2046 line_is_blank (WEdit
* edit
, long line
)
2048 return is_blank (edit
, edit_find_line (edit
, line
));
2051 /* moves up until a blank line is reached, or until just
2052 before a non-blank line is reached */
2054 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
2057 if (edit
->curs_line
> 1)
2059 if (line_is_blank (edit
, edit
->curs_line
))
2061 if (line_is_blank (edit
, edit
->curs_line
- 1))
2063 for (i
= edit
->curs_line
- 1; i
; i
--)
2064 if (!line_is_blank (edit
, i
))
2072 for (i
= edit
->curs_line
- 1; i
; i
--)
2073 if (line_is_blank (edit
, i
))
2079 for (i
= edit
->curs_line
- 1; i
; i
--)
2080 if (line_is_blank (edit
, i
))
2084 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
2087 /* moves down until a blank line is reached, or until just
2088 before a non-blank line is reached */
2090 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
2093 if (edit
->curs_line
>= edit
->total_lines
- 1)
2095 i
= edit
->total_lines
;
2099 if (line_is_blank (edit
, edit
->curs_line
))
2101 if (line_is_blank (edit
, edit
->curs_line
+ 1))
2103 for (i
= edit
->curs_line
+ 1; i
; i
++)
2104 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
2112 for (i
= edit
->curs_line
+ 1; i
; i
++)
2113 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2119 for (i
= edit
->curs_line
+ 1; i
; i
++)
2120 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2124 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
2128 edit_begin_page (WEdit
* edit
)
2130 edit_update_curs_row (edit
);
2131 edit_move_up (edit
, edit
->curs_row
, 0);
2135 edit_end_page (WEdit
* edit
)
2137 edit_update_curs_row (edit
);
2138 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
2142 /* goto beginning of text */
2144 edit_move_to_top (WEdit
* edit
)
2146 if (edit
->curs_line
)
2148 edit_cursor_move (edit
, -edit
->curs1
);
2149 edit_move_to_prev_col (edit
, 0);
2150 edit
->force
|= REDRAW_PAGE
;
2151 edit
->search_start
= 0;
2152 edit_update_curs_row (edit
);
2157 /* goto end of text */
2159 edit_move_to_bottom (WEdit
* edit
)
2161 if (edit
->curs_line
< edit
->total_lines
)
2163 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
2164 edit
->start_display
= edit
->last_byte
;
2165 edit
->start_line
= edit
->total_lines
;
2166 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
2167 edit
->force
|= REDRAW_PAGE
;
2171 /* goto beginning of line */
2173 edit_cursor_to_bol (WEdit
* edit
)
2175 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
2176 edit
->search_start
= edit
->curs1
;
2177 edit
->prev_col
= edit_get_col (edit
);
2181 /* goto end of line */
2183 edit_cursor_to_eol (WEdit
* edit
)
2185 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
2186 edit
->search_start
= edit
->curs1
;
2187 edit
->prev_col
= edit_get_col (edit
);
2191 /* move cursor to line 'line' */
2193 edit_move_to_line (WEdit
* e
, long line
)
2195 if (line
< e
->curs_line
)
2196 edit_move_up (e
, e
->curs_line
- line
, 0);
2198 edit_move_down (e
, line
- e
->curs_line
, 0);
2199 edit_scroll_screen_over_cursor (e
);
2202 /* scroll window so that first visible line is 'line' */
2204 edit_move_display (WEdit
* e
, long line
)
2206 if (line
< e
->start_line
)
2207 edit_scroll_upward (e
, e
->start_line
- line
);
2209 edit_scroll_downward (e
, line
- e
->start_line
);
2212 /* save markers onto undo stack */
2214 edit_push_markers (WEdit
* edit
)
2216 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2217 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2221 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2230 /* highlight marker toggle */
2232 edit_mark_cmd (WEdit
* edit
, int unmark
)
2234 edit_push_markers (edit
);
2237 edit_set_markers (edit
, 0, 0, 0, 0);
2238 edit
->force
|= REDRAW_PAGE
;
2242 if (edit
->mark2
>= 0)
2244 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
2245 edit
->curs_col
+ edit
->over_col
);
2246 edit
->force
|= REDRAW_PAGE
;
2249 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
2250 edit
->curs_col
+ edit
->over_col
);
2254 static unsigned long
2259 const char option_chars_move_whole_word
[] =
2260 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2266 if (*option_chars_move_whole_word
== '!')
2268 return 0x80000000UL
;
2270 if (g_ascii_isupper ((gchar
) c
))
2272 else if (g_ascii_islower ((gchar
) c
))
2274 else if (g_ascii_isalpha (c
))
2276 else if (isdigit (c
))
2278 else if (isspace (c
))
2280 q
= strchr (option_chars_move_whole_word
, c
);
2282 return 0xFFFFFFFFUL
;
2285 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2290 while ((q
= strchr (q
+ 1, c
)));
2295 edit_left_word_move (WEdit
* edit
, int s
)
2300 if (column_highlighting
2301 && edit
->mark1
!= edit
->mark2
2302 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2304 edit_cursor_move (edit
, -1);
2307 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2308 c2
= edit_get_byte (edit
, edit
->curs1
);
2309 if (!(my_type_of (c1
) & my_type_of (c2
)))
2311 if (isspace (c1
) && !isspace (c2
))
2314 if (!isspace (c1
) && isspace (c2
))
2320 edit_left_word_move_cmd (WEdit
* edit
)
2322 edit_left_word_move (edit
, 0);
2323 edit
->force
|= REDRAW_PAGE
;
2327 edit_right_word_move (WEdit
* edit
, int s
)
2332 if (column_highlighting
2333 && edit
->mark1
!= edit
->mark2
2334 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
2336 edit_cursor_move (edit
, 1);
2337 if (edit
->curs1
>= edit
->last_byte
)
2339 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2340 c2
= edit_get_byte (edit
, edit
->curs1
);
2341 if (!(my_type_of (c1
) & my_type_of (c2
)))
2343 if (isspace (c1
) && !isspace (c2
))
2346 if (!isspace (c1
) && isspace (c2
))
2352 edit_right_word_move_cmd (WEdit
* edit
)
2354 edit_right_word_move (edit
, 0);
2355 edit
->force
|= REDRAW_PAGE
;
2359 edit_right_char_move_cmd (WEdit
* edit
)
2365 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2371 c
= edit_get_byte (edit
, edit
->curs1
);
2373 if (option_cursor_beyond_eol
&& c
== '\n')
2379 edit_cursor_move (edit
, cw
);
2384 edit_left_char_move_cmd (WEdit
* edit
)
2387 if (column_highlighting
2388 && option_cursor_beyond_eol
2389 && edit
->mark1
!= edit
->mark2
2390 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2394 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2398 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2404 edit_cursor_move (edit
, -cw
);
2408 /** Up or down cursor moving.
2409 direction = TRUE - move up
2413 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2416 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
2425 edit
->force
|= REDRAW_PAGE
;
2429 edit_scroll_upward (edit
, i
);
2431 edit_scroll_downward (edit
, i
);
2433 p
= edit_bol (edit
, edit
->curs1
);
2435 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
2437 edit_cursor_move (edit
, p
- edit
->curs1
);
2439 edit_move_to_prev_col (edit
, p
);
2441 /* search start of current multibyte char (like CJK) */
2442 if (edit
->curs1
+ 1 < edit
->last_byte
)
2444 edit_right_char_move_cmd (edit
);
2445 edit_left_char_move_cmd (edit
);
2448 edit
->search_start
= edit
->curs1
;
2449 edit
->found_len
= 0;
2453 edit_right_delete_word (WEdit
* edit
)
2458 if (edit
->curs1
>= edit
->last_byte
)
2460 c1
= edit_delete (edit
, 1);
2461 c2
= edit_get_byte (edit
, edit
->curs1
);
2462 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2464 if (!(my_type_of (c1
) & my_type_of (c2
)))
2470 edit_left_delete_word (WEdit
* edit
)
2475 if (edit
->curs1
<= 0)
2477 c1
= edit_backspace (edit
, 1);
2478 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2479 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2481 if (!(my_type_of (c1
) & my_type_of (c2
)))
2487 the start column position is not recorded, and hence does not
2488 undo as it happed. But who would notice.
2491 edit_do_undo (WEdit
* edit
)
2496 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2498 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
2505 edit_cursor_move (edit
, 1);
2508 edit_cursor_move (edit
, -1);
2511 edit_backspace (edit
, 1);
2514 edit_delete (edit
, 1);
2517 column_highlighting
= 1;
2520 column_highlighting
= 0;
2523 if (ac
>= 256 && ac
< 512)
2524 edit_insert_ahead (edit
, ac
- 256);
2525 if (ac
>= 0 && ac
< 256)
2526 edit_insert (edit
, ac
);
2528 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
2530 edit
->mark1
= ac
- MARK_1
;
2531 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2533 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
2535 edit
->mark2
= ac
- MARK_2
;
2536 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2539 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2542 if (edit
->start_display
> ac
- KEY_PRESS
)
2544 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2545 edit
->force
|= REDRAW_PAGE
;
2547 else if (edit
->start_display
< ac
- KEY_PRESS
)
2549 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2550 edit
->force
|= REDRAW_PAGE
;
2552 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2553 edit_update_curs_row (edit
);
2556 edit
->stack_disable
= 0;
2560 edit_delete_to_line_end (WEdit
* edit
)
2562 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2566 edit_delete (edit
, 1);
2571 edit_delete_to_line_begin (WEdit
* edit
)
2573 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2577 edit_backspace (edit
, 1);
2582 edit_delete_line (WEdit
* edit
)
2585 * Delete right part of the line.
2586 * Note that edit_get_byte() returns '\n' when byte position is
2589 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2591 (void) edit_delete (edit
, 1);
2596 * Note that edit_delete() will not corrupt anything if called while
2597 * cursor position is EOF.
2599 (void) edit_delete (edit
, 1);
2602 * Delete left part of the line.
2603 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2605 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2607 (void) edit_backspace (edit
, 1);
2612 insert_spaces_tab (WEdit
* edit
, int half
)
2615 edit_update_curs_col (edit
);
2616 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
2617 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2620 edit_insert (edit
, ' ');
2626 is_aligned_on_a_tab (WEdit
* edit
)
2628 edit_update_curs_col (edit
);
2629 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2630 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2634 right_of_four_spaces (WEdit
* edit
)
2637 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2638 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2640 return is_aligned_on_a_tab (edit
);
2645 left_of_four_spaces (WEdit
* edit
)
2648 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2649 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2651 return is_aligned_on_a_tab (edit
);
2656 edit_indent_width (WEdit
* edit
, long p
)
2659 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2661 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2665 edit_insert_indent (WEdit
* edit
, int indent
)
2667 if (!option_fill_tabs_with_spaces
)
2669 while (indent
>= TAB_SIZE
)
2671 edit_insert (edit
, '\t');
2675 while (indent
-- > 0)
2676 edit_insert (edit
, ' ');
2680 edit_auto_indent (WEdit
* edit
)
2685 /* use the previous line as a template */
2686 p
= edit_move_backward (edit
, p
, 1);
2687 /* copy the leading whitespace of the line */
2689 { /* no range check - the line _is_ \n-terminated */
2690 c
= edit_get_byte (edit
, p
++);
2691 if (c
!= ' ' && c
!= '\t')
2693 edit_insert (edit
, c
);
2698 edit_double_newline (WEdit
* edit
)
2700 edit_insert (edit
, '\n');
2701 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2703 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2705 edit
->force
|= REDRAW_PAGE
;
2706 edit_insert (edit
, '\n');
2710 edit_tab_cmd (WEdit
* edit
)
2714 if (option_fake_half_tabs
)
2716 if (is_in_indent (edit
))
2718 /*insert a half tab (usually four spaces) unless there is a
2719 half tab already behind, then delete it and insert a
2721 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
2723 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2724 edit_backspace (edit
, 1);
2725 edit_insert (edit
, '\t');
2729 insert_spaces_tab (edit
, 1);
2734 if (option_fill_tabs_with_spaces
)
2736 insert_spaces_tab (edit
, 0);
2740 edit_insert (edit
, '\t');
2745 check_and_wrap_line (WEdit
* edit
)
2748 if (!option_typewriter_wrap
)
2750 edit_update_curs_col (edit
);
2751 if (edit
->curs_col
< option_word_wrap_line_length
)
2757 c
= edit_get_byte (edit
, curs
);
2758 if (c
== '\n' || curs
<= 0)
2760 edit_insert (edit
, '\n');
2763 if (c
== ' ' || c
== '\t')
2765 int current
= edit
->curs1
;
2766 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2767 edit_insert (edit
, '\n');
2768 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2774 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
2777 edit_push_key_press (WEdit
* edit
)
2779 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2780 if (edit
->mark2
== -1)
2781 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2784 /* this find the matching bracket in either direction, and sets edit->bracket */
2786 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2788 const char *const b
= "{}{[][()(", *p
;
2789 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2790 unsigned long j
= 0;
2792 edit_update_curs_row (edit
);
2793 c
= edit_get_byte (edit
, edit
->curs1
);
2796 if (!furthest_bracket_search
)
2797 furthest_bracket_search
--;
2798 /* not on a bracket at all */
2801 /* the matching bracket */
2803 /* going left or right? */
2804 if (strchr ("{[(", c
))
2806 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
2808 /* out of buffer? */
2809 if (q
>= edit
->last_byte
|| q
< 0)
2811 a
= edit_get_byte (edit
, q
);
2812 /* don't want to eat CPU */
2813 if (j
++ > furthest_bracket_search
)
2815 /* out of screen? */
2818 if (q
< edit
->start_display
)
2820 /* count lines if searching downward */
2821 if (inc
> 0 && a
== '\n')
2822 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2825 /* count bracket depth */
2826 i
+= (a
== c
) - (a
== d
);
2827 /* return if bracket depth is zero */
2835 static long last_bracket
= -1;
2838 edit_find_bracket (WEdit
* edit
)
2840 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2841 if (last_bracket
!= edit
->bracket
)
2842 edit
->force
|= REDRAW_PAGE
;
2843 last_bracket
= edit
->bracket
;
2847 edit_goto_matching_bracket (WEdit
* edit
)
2851 q
= edit_get_bracket (edit
, 0, 0);
2854 edit
->bracket
= edit
->curs1
;
2855 edit
->force
|= REDRAW_PAGE
;
2856 edit_cursor_move (edit
, q
- edit
->curs1
);
2861 * This executes a command as though the user initiated it through a key
2862 * press. Callback with WIDGET_KEY as a message calls this after
2863 * translating the key press. This function can be used to pass any
2864 * command to the editor. Note that the screen wouldn't update
2865 * automatically. Either of command or char_for_insertion must be
2866 * passed as -1. Commands are executed, and char_for_insertion is
2867 * inserted at the cursor.
2870 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2872 if (command
== CK_Begin_Record_Macro
)
2875 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2878 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
2880 edit
->force
|= REDRAW_COMPLETELY
;
2881 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2885 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
2887 edit
->macro
[edit
->macro_i
].command
= command
;
2888 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2890 /* record the beginning of a set of editing actions initiated by a key press */
2891 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2892 edit_push_key_press (edit
);
2894 edit_execute_cmd (edit
, command
, char_for_insertion
);
2895 if (column_highlighting
)
2896 edit
->force
|= REDRAW_PAGE
;
2899 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
2902 This executes a command at a lower level than macro recording.
2903 It also does not push a key_press onto the undo stack. This means
2904 that if it is called many times, a single undo command will undo
2905 all of them. It also does not check for the Undo command.
2908 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2910 edit
->force
|= REDRAW_LINE
;
2912 /* The next key press will unhighlight the found string, so update
2914 if (edit
->found_len
|| column_highlighting
)
2915 edit
->force
|= REDRAW_PAGE
;
2917 if (command
/ 100 == 6)
2918 { /* a highlight command like shift-arrow */
2919 column_highlighting
= 0;
2920 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
2922 edit_mark_cmd (edit
, 1); /* clear */
2923 edit_mark_cmd (edit
, 0); /* marking on */
2925 edit
->highlight
= 1;
2928 { /* any other command */
2929 if (edit
->highlight
)
2930 edit_mark_cmd (edit
, 0); /* clear */
2931 edit
->highlight
= 0;
2934 /* first check for undo */
2935 if (command
== CK_Undo
)
2937 edit_do_undo (edit
);
2938 edit
->found_len
= 0;
2939 edit
->prev_col
= edit_get_col (edit
);
2940 edit
->search_start
= edit
->curs1
;
2944 /* An ordinary key press */
2945 if (char_for_insertion
>= 0)
2947 /* if non persistent selection and text selected */
2948 if (!option_persistent_selections
)
2950 if (edit
->mark1
!= edit
->mark2
)
2951 edit_block_delete_cmd (edit
);
2953 if (edit
->overwrite
)
2955 /* remove char only one time, after input first byte, multibyte chars */
2956 if ((!utf8_display
|| edit
->charpoint
== 0)
2957 && edit_get_byte (edit
, edit
->curs1
) != '\n')
2958 edit_delete (edit
, 0);
2960 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2961 edit_insert_over (edit
);
2963 if (char_for_insertion
> 255 && utf8_display
== 0)
2965 unsigned char str
[6 + 1];
2967 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
2977 while (str
[i
] != 0 && i
<= 6)
2979 char_for_insertion
= str
[i
];
2980 edit_insert (edit
, char_for_insertion
);
2986 edit_insert (edit
, char_for_insertion
);
2988 if (option_auto_para_formatting
)
2990 format_paragraph (edit
, 0);
2991 edit
->force
|= REDRAW_PAGE
;
2994 check_and_wrap_line (edit
);
2995 edit
->found_len
= 0;
2996 edit
->prev_col
= edit_get_col (edit
);
2997 edit
->search_start
= edit
->curs1
;
2998 edit_find_bracket (edit
);
3006 case CK_Begin_Page_Highlight
:
3007 case CK_End_Page_Highlight
:
3014 if (edit
->mark2
>= 0)
3016 if (!option_persistent_selections
)
3018 if (column_highlighting
)
3019 edit_push_action (edit
, COLUMN_ON
);
3020 column_highlighting
= 0;
3021 edit_mark_cmd (edit
, 1);
3030 case CK_Begin_Page_Highlight
:
3031 case CK_End_Page_Highlight
:
3036 case CK_Word_Left_Highlight
:
3037 case CK_Word_Right_Highlight
:
3038 case CK_Up_Highlight
:
3039 case CK_Down_Highlight
:
3040 case CK_Up_Alt_Highlight
:
3041 case CK_Down_Alt_Highlight
:
3042 if (edit
->mark2
== -1)
3043 break; /*marking is following the cursor: may need to highlight a whole line */
3046 case CK_Left_Highlight
:
3047 case CK_Right_Highlight
:
3048 edit
->force
|= REDRAW_CHAR_ONLY
;
3051 /* basic cursor key commands */
3055 /* if non persistent selection and text selected */
3056 if (!option_persistent_selections
)
3058 if (edit
->mark1
!= edit
->mark2
)
3060 edit_block_delete_cmd (edit
);
3064 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3069 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3071 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3072 edit_backspace (edit
, 1);
3077 if (option_fake_half_tabs
)
3080 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3082 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3083 edit_backspace (edit
, 1);
3088 edit_backspace (edit
, 0);
3091 /* if non persistent selection and text selected */
3092 if (!option_persistent_selections
)
3094 if (edit
->mark1
!= edit
->mark2
)
3096 edit_block_delete_cmd (edit
);
3101 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3102 edit_insert_over (edit
);
3104 if (option_fake_half_tabs
)
3107 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3109 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3110 edit_delete (edit
, 1);
3114 edit_delete (edit
, 0);
3116 case CK_Delete_Word_Left
:
3118 edit_left_delete_word (edit
);
3120 case CK_Delete_Word_Right
:
3121 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3122 edit_insert_over (edit
);
3124 edit_right_delete_word (edit
);
3126 case CK_Delete_Line
:
3127 edit_delete_line (edit
);
3129 case CK_Delete_To_Line_End
:
3130 edit_delete_to_line_end (edit
);
3132 case CK_Delete_To_Line_Begin
:
3133 edit_delete_to_line_begin (edit
);
3137 if (option_auto_para_formatting
)
3139 edit_double_newline (edit
);
3140 if (option_return_does_auto_indent
)
3141 edit_auto_indent (edit
);
3142 format_paragraph (edit
, 0);
3146 edit_insert (edit
, '\n');
3147 if (option_return_does_auto_indent
)
3149 edit_auto_indent (edit
);
3154 edit_insert (edit
, '\n');
3157 case CK_Page_Up_Alt_Highlight
:
3158 column_highlighting
= 1;
3160 case CK_Page_Up_Highlight
:
3161 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3163 case CK_Page_Down_Alt_Highlight
:
3164 column_highlighting
= 1;
3166 case CK_Page_Down_Highlight
:
3167 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3169 case CK_Left_Alt_Highlight
:
3170 column_highlighting
= 1;
3172 case CK_Left_Highlight
:
3173 if (option_fake_half_tabs
)
3175 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3177 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3180 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3181 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3185 edit_left_char_move_cmd (edit
);
3187 case CK_Right_Alt_Highlight
:
3188 column_highlighting
= 1;
3190 case CK_Right_Highlight
:
3191 if (option_fake_half_tabs
)
3193 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3195 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3196 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3200 edit_right_char_move_cmd (edit
);
3203 case CK_Begin_Page_Highlight
:
3204 edit_begin_page (edit
);
3207 case CK_End_Page_Highlight
:
3208 edit_end_page (edit
);
3211 case CK_Word_Left_Highlight
:
3213 edit_left_word_move_cmd (edit
);
3216 case CK_Word_Right_Highlight
:
3218 edit_right_word_move_cmd (edit
);
3220 case CK_Up_Alt_Highlight
:
3221 column_highlighting
= 1;
3223 case CK_Up_Highlight
:
3224 edit_move_up (edit
, 1, 0);
3226 case CK_Down_Alt_Highlight
:
3227 column_highlighting
= 1;
3229 case CK_Down_Highlight
:
3230 edit_move_down (edit
, 1, 0);
3232 case CK_Paragraph_Up_Alt_Highlight
:
3233 column_highlighting
= 1;
3234 case CK_Paragraph_Up
:
3235 case CK_Paragraph_Up_Highlight
:
3236 edit_move_up_paragraph (edit
, 0);
3238 case CK_Paragraph_Down_Alt_Highlight
:
3239 column_highlighting
= 1;
3240 case CK_Paragraph_Down
:
3241 case CK_Paragraph_Down_Highlight
:
3242 edit_move_down_paragraph (edit
, 0);
3244 case CK_Scroll_Up_Alt_Highlight
:
3245 column_highlighting
= 1;
3247 case CK_Scroll_Up_Highlight
:
3248 edit_move_up (edit
, 1, 1);
3250 case CK_Scroll_Down_Alt_Highlight
:
3251 column_highlighting
= 1;
3252 case CK_Scroll_Down
:
3253 case CK_Scroll_Down_Highlight
:
3254 edit_move_down (edit
, 1, 1);
3257 case CK_Home_Highlight
:
3258 edit_cursor_to_bol (edit
);
3261 case CK_End_Highlight
:
3262 edit_cursor_to_eol (edit
);
3265 /* if text marked shift block */
3266 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3268 if (edit
->mark2
< 0)
3269 edit_mark_cmd (edit
, 0);
3270 edit_move_block_to_right (edit
);
3274 if (option_cursor_beyond_eol
)
3275 edit_insert_over (edit
);
3276 edit_tab_cmd (edit
);
3277 if (option_auto_para_formatting
)
3279 format_paragraph (edit
, 0);
3280 edit
->force
|= REDRAW_PAGE
;
3284 check_and_wrap_line (edit
);
3289 case CK_Toggle_Insert
:
3290 edit
->overwrite
= (edit
->overwrite
== 0);
3294 if (edit
->mark2
>= 0)
3296 if (column_highlighting
)
3297 edit_push_action (edit
, COLUMN_ON
);
3298 column_highlighting
= 0;
3300 edit_mark_cmd (edit
, 0);
3302 case CK_Column_Mark
:
3303 if (!column_highlighting
)
3304 edit_push_action (edit
, COLUMN_OFF
);
3305 column_highlighting
= 1;
3306 edit_mark_cmd (edit
, 0);
3309 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3310 edit
->force
|= REDRAW_PAGE
;
3313 if (column_highlighting
)
3314 edit_push_action (edit
, COLUMN_ON
);
3315 column_highlighting
= 0;
3316 edit_mark_cmd (edit
, 1);
3319 case CK_Toggle_Line_State
:
3320 option_line_state
= !option_line_state
;
3321 if (option_line_state
)
3323 option_line_state_width
= LINE_STATE_WIDTH
;
3327 option_line_state_width
= 0;
3329 edit
->force
|= REDRAW_PAGE
;
3332 case CK_Toggle_Show_Margin
:
3333 show_right_margin
= !show_right_margin
;
3334 edit
->force
|= REDRAW_PAGE
;
3337 case CK_Toggle_Bookmark
:
3338 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3339 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3340 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3342 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3344 case CK_Flush_Bookmarks
:
3345 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3346 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3347 edit
->force
|= REDRAW_PAGE
;
3349 case CK_Next_Bookmark
:
3350 if (edit
->book_mark
)
3352 struct _book_mark
*p
;
3353 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3357 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3358 || p
->line
< edit
->start_line
)
3359 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3360 edit_move_to_line (edit
, p
->line
);
3364 case CK_Prev_Bookmark
:
3365 if (edit
->book_mark
)
3367 struct _book_mark
*p
;
3368 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3369 while (p
->line
== edit
->curs_line
)
3374 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3375 || p
->line
< edit
->start_line
)
3376 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3377 edit_move_to_line (edit
, p
->line
);
3382 case CK_Beginning_Of_Text
:
3383 case CK_Beginning_Of_Text_Highlight
:
3384 edit_move_to_top (edit
);
3386 case CK_End_Of_Text
:
3387 case CK_End_Of_Text_Highlight
:
3388 edit_move_to_bottom (edit
);
3392 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3393 edit_insert_over (edit
);
3394 edit_block_copy_cmd (edit
);
3397 edit_block_delete_cmd (edit
);
3400 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3401 edit_insert_over (edit
);
3402 edit_block_move_cmd (edit
);
3405 case CK_Shift_Block_Left
:
3406 if (edit
->mark1
!= edit
->mark2
)
3407 edit_move_block_to_left (edit
);
3409 case CK_Shift_Block_Right
:
3410 if (edit
->mark1
!= edit
->mark2
)
3411 edit_move_block_to_right (edit
);
3414 edit_copy_to_X_buf_cmd (edit
);
3417 edit_cut_to_X_buf_cmd (edit
);
3420 /* if non persistent selection and text selected */
3421 if (!option_persistent_selections
)
3423 if (edit
->mark1
!= edit
->mark2
)
3424 edit_block_delete_cmd (edit
);
3426 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3427 edit_insert_over (edit
);
3428 edit_paste_from_X_buf_cmd (edit
);
3430 case CK_Selection_History
:
3431 edit_paste_from_history (edit
);
3435 edit_save_as_cmd (edit
);
3438 edit_save_confirm_cmd (edit
);
3441 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3444 edit_save_block_cmd (edit
);
3446 case CK_Insert_File
:
3447 edit_insert_file_cmd (edit
);
3450 case CK_Load_Prev_File
:
3451 edit_load_back_cmd (edit
);
3453 case CK_Load_Next_File
:
3454 edit_load_forward_cmd (edit
);
3457 case CK_Load_Syntax_File
:
3458 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3460 case CK_Choose_Syntax
:
3461 edit_syntax_dialog (edit
, edit
->syntax_type
);
3464 case CK_Load_Menu_File
:
3465 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3468 case CK_Toggle_Syntax
:
3469 if ((option_syntax_highlighting
^= 1) == 1)
3470 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3471 edit
->force
|= REDRAW_PAGE
;
3474 case CK_Toggle_Tab_TWS
:
3475 enable_show_tabs_tws
^= 1;
3476 edit
->force
|= REDRAW_PAGE
;
3480 edit_search_cmd (edit
, 0);
3483 edit_search_cmd (edit
, 1);
3486 edit_replace_cmd (edit
, 0);
3488 case CK_Replace_Again
:
3489 edit_replace_cmd (edit
, 1);
3491 case CK_Complete_Word
:
3492 /* if text marked shift block */
3493 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3495 edit_move_block_to_left (edit
);
3499 edit_complete_word_cmd (edit
);
3502 case CK_Find_Definition
:
3503 edit_get_match_keyword_cmd (edit
);
3506 dlg_stop (edit
->widget
.parent
);
3509 edit_new_cmd (edit
);
3512 edit_help_cmd (edit
);
3515 edit_refresh_cmd (edit
);
3517 case CK_SaveSetupCmd
:
3521 query_dialog (_(" About "),
3522 _("\n Cooledit v3.11.5\n\n"
3523 " Copyright (C) 1996 the Free Software Foundation\n\n"
3524 " A user friendly text editor written\n"
3525 " for the Midnight Commander.\n"), D_NORMAL
, 1, _("&OK"));
3530 case CK_Edit_Options
:
3531 edit_options_dialog (edit
);
3533 case CK_Edit_Save_Mode
:
3534 menu_save_mode_cmd ();
3539 /* fool gcc to prevent a Y2K warning */
3540 char time_format
[] = "_c";
3541 time_format
[0] = '%';
3543 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3544 edit_print_string (edit
, s
);
3545 edit
->force
|= REDRAW_PAGE
;
3550 edit_goto_cmd (edit
);
3552 case CK_Paragraph_Format
:
3553 format_paragraph (edit
, 1);
3554 edit
->force
|= REDRAW_PAGE
;
3556 case CK_Delete_Macro
:
3557 edit_delete_macro_cmd (edit
);
3559 case CK_Match_Bracket
:
3560 edit_goto_matching_bracket (edit
);
3566 edit_sort_cmd (edit
);
3569 edit_ext_cmd (edit
);
3572 edit_mail_dialog (edit
);
3577 case CK_SelectCodepage
:
3578 edit_select_codepage_cmd (edit
);
3580 case CK_Insert_Literal
:
3581 edit_insert_literal_cmd (edit
);
3583 case CK_Execute_Macro
:
3584 edit_execute_macro_cmd (edit
);
3586 case CK_Begin_End_Macro
:
3587 edit_begin_end_macro_cmd (edit
);
3597 if ((command
/ 1000) == 1) /* a shell command */
3598 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3599 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
3600 { /* a macro command */
3601 struct macro m
[MAX_MACRO_LENGTH
];
3603 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3604 edit_execute_macro (edit
, m
, nm
);
3607 /* keys which must set the col position, and the search vars */
3613 case CK_Replace_Again
:
3614 case CK_Complete_Word
:
3615 edit
->prev_col
= edit_get_col (edit
);
3618 case CK_Up_Highlight
:
3619 case CK_Up_Alt_Highlight
:
3621 case CK_Down_Highlight
:
3622 case CK_Down_Alt_Highlight
:
3624 case CK_Page_Up_Highlight
:
3625 case CK_Page_Up_Alt_Highlight
:
3627 case CK_Page_Down_Highlight
:
3628 case CK_Page_Down_Alt_Highlight
:
3629 case CK_Beginning_Of_Text
:
3630 case CK_Beginning_Of_Text_Highlight
:
3631 case CK_End_Of_Text
:
3632 case CK_End_Of_Text_Highlight
:
3633 case CK_Paragraph_Up
:
3634 case CK_Paragraph_Up_Highlight
:
3635 case CK_Paragraph_Up_Alt_Highlight
:
3636 case CK_Paragraph_Down
:
3637 case CK_Paragraph_Down_Highlight
:
3638 case CK_Paragraph_Down_Alt_Highlight
:
3640 case CK_Scroll_Up_Highlight
:
3641 case CK_Scroll_Up_Alt_Highlight
:
3642 case CK_Scroll_Down
:
3643 case CK_Scroll_Down_Highlight
:
3644 case CK_Scroll_Down_Alt_Highlight
:
3645 edit
->search_start
= edit
->curs1
;
3646 edit
->found_len
= 0;
3649 edit
->found_len
= 0;
3650 edit
->prev_col
= edit_get_col (edit
);
3651 edit
->search_start
= edit
->curs1
;
3653 edit_find_bracket (edit
);
3655 if (option_auto_para_formatting
)
3661 case CK_Delete_Word_Left
:
3662 case CK_Delete_Word_Right
:
3663 case CK_Delete_To_Line_End
:
3664 case CK_Delete_To_Line_Begin
:
3665 format_paragraph (edit
, 0);
3666 edit
->force
|= REDRAW_PAGE
;
3673 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
3677 if (edit
->macro_depth
++ > 256)
3679 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3680 edit
->macro_depth
--;
3683 edit
->force
|= REDRAW_PAGE
;
3686 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3688 edit_update_screen (edit
);
3689 edit
->macro_depth
--;
3692 /* User edit menu, like user menu (F2) but only in editor. */
3694 user_menu (WEdit
* edit
)
3698 long start_mark
, end_mark
;
3701 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3703 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3705 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3707 /* run shell scripts from menu */
3708 user_menu_cmd (edit
);
3710 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
3717 /* i.e. we have marked block */
3718 rc
= edit_block_delete_cmd (edit
);
3722 edit_insert_file (edit
, block_file
);
3724 /* truncate block file */
3725 fd
= fopen (block_file
, "w");
3729 edit_refresh_cmd (edit
);
3730 edit
->force
|= REDRAW_COMPLETELY
;
3732 g_free (block_file
);
3736 edit_stack_init (void)
3738 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3740 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3741 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3744 edit_stack_iterator
= 0;
3748 edit_stack_free (void)
3750 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3751 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3756 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3758 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3763 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3765 edit_move_updown (edit
, i
, do_scroll
, FALSE
);