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 */
52 #include "src/widget.h"
53 #include "src/cmd.h" /* view_other_cmd() */
54 #include "src/user.h" /* user_menu_cmd() */
55 #include "src/wtools.h" /* query_dialog() */
56 #include "lib/timefmt.h" /* time formatting */
57 #include "src/charsets.h" /* get_codepage_id */
58 #include "src/main.h" /* source_codepage */
59 #include "src/setup.h" /* option_tab_spacing */
60 #include "src/learn.h" /* learn_keys */
61 #include "src/cmddef.h"
62 #include "src/keybind.h"
64 #include "edit-impl.h"
65 #include "edit-widget.h"
67 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
68 int option_typewriter_wrap
= 0;
69 int option_auto_para_formatting
= 0;
70 int option_fill_tabs_with_spaces
= 0;
71 int option_return_does_auto_indent
= 1;
72 int option_backspace_through_tabs
= 0;
73 int option_fake_half_tabs
= 1;
74 int option_save_mode
= EDIT_QUICK_SAVE
;
75 int option_save_position
= 1;
76 int option_max_undo
= 32768;
77 int option_persistent_selections
= 1;
78 int option_cursor_beyond_eol
= 0;
79 int option_line_state
= 0;
80 int option_line_state_width
= 0;
82 int option_edit_right_extreme
= 0;
83 int option_edit_left_extreme
= 0;
84 int option_edit_top_extreme
= 0;
85 int option_edit_bottom_extreme
= 0;
86 int enable_show_tabs_tws
= 1;
87 int option_check_nl_at_eof
= 0;
88 int show_right_margin
= 0;
90 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
91 char *option_backup_ext
= NULL
;
93 int edit_stack_iterator
= 0;
94 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
95 /* magic sequense for say than block is vertical */
96 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
100 * here's a quick sketch of the layout: (don't run this through indent.)
102 * (b1 is buffers1 and b2 is buffers2)
105 * \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
106 * ______________________________________|______________________________________
108 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
109 * |-> |-> |-> |-> |-> |-> |
111 * _<------------------------->|<----------------->_
112 * WEdit->curs2 | WEdit->curs1
117 * file end|||file beginning
126 static void user_menu (WEdit
* edit
);
129 edit_get_byte (WEdit
* edit
, long byte_index
)
132 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
135 if (byte_index
>= edit
->curs1
)
137 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
138 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
142 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
147 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
150 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
153 if (byte_index
>= edit
->curs1
)
155 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
156 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
157 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
161 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
162 (byte_index
& M_EDIT_BUF_SIZE
));
167 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
171 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
177 if (byte_index
>= edit
->curs1
)
179 p
= edit
->curs1
+ edit
->curs2
- 1;
180 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
181 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
185 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
190 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
195 gchar
*next_ch
= NULL
;
198 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
204 str
= edit_get_byte_ptr (edit
, byte_index
);
212 res
= g_utf8_get_char_validated (str
, -1);
222 /* Calculate UTF-8 char width */
223 next_ch
= g_utf8_next_char (str
);
226 width
= next_ch
- str
;
239 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
241 gchar
*str
, *buf
= NULL
;
244 gchar
*next_ch
= NULL
;
250 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
256 ch
= edit_get_utf (edit
, byte_index
, &width
);
264 str
= edit_get_byte_ptr (edit
, byte_index
);
265 buf
= edit_get_buf_ptr (edit
, byte_index
);
266 if (str
== NULL
|| buf
== NULL
)
271 /* get prev utf8 char */
273 str
= g_utf8_find_prev_char (buf
, str
);
275 res
= g_utf8_get_char_validated (str
, -1);
285 /* Calculate UTF-8 char width */
286 next_ch
= g_utf8_next_char (str
);
289 width
= next_ch
- str
;
302 * Initialize the buffers for an empty files.
305 edit_init_buffers (WEdit
* edit
)
309 for (j
= 0; j
<= MAXBUFF
; j
++)
311 edit
->buffers1
[j
] = NULL
;
312 edit
->buffers2
[j
] = NULL
;
317 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
321 * Load file OR text into buffers. Set cursor to the beginning of file.
325 edit_load_file_fast (WEdit
* edit
, const char *filename
)
331 edit
->curs2
= edit
->last_byte
;
332 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
334 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
339 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
340 edit_error_dialog (_("Error"), errmsg
);
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 gchar
*errmsg
= NULL
;
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 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
650 /* New file, delete it if it's not modified or saved */
651 edit
->delete_file
= 1;
655 /* Check what we have opened */
656 if (mc_fstat (file
, st
) < 0)
658 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
662 /* We want to open regular files only */
663 if (!S_ISREG (st
->st_mode
))
665 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
670 * Don't delete non-empty files.
671 * O_EXCL should prevent it, but let's be on the safe side.
674 edit
->delete_file
= 0;
676 if (st
->st_size
>= SIZE_LIMIT
)
677 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
680 (void) mc_close (file
);
684 edit_error_dialog (_("Error"), errmsg
);
692 * Open the file and load it into the buffers, either directly or using
693 * a filter. Return 0 on success, 1 on error.
695 * Fast loading (edit_load_file_fast) is used when the file size is
696 * known. In this case the data is read into the buffers by blocks.
697 * If the file size is not known, the data is loaded byte by byte in
701 edit_load_file (WEdit
* edit
)
705 /* Cannot do fast load if a filter is used */
706 if (edit_find_filter (edit
->filename
) >= 0)
710 * VFS may report file size incorrectly, and slow load is not a big
711 * deal considering overhead in VFS.
713 if (!vfs_file_is_local (edit
->filename
))
717 * FIXME: line end translation should disable fast loading as well
718 * Consider doing fseek() to the end and ftell() for the real size.
723 /* If we are dealing with a real file, check that it exists */
724 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
729 /* nothing to load */
733 edit_init_buffers (edit
);
737 edit
->last_byte
= edit
->stat1
.st_size
;
738 edit_load_file_fast (edit
, edit
->filename
);
739 /* If fast load was used, the number of lines wasn't calculated */
740 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
747 edit
->stack_disable
= 1;
748 if (!edit_insert_file (edit
, edit
->filename
))
753 edit
->stack_disable
= 0;
760 /* Restore saved cursor position in the file */
762 edit_load_position (WEdit
* edit
)
768 if (!edit
->filename
|| !*edit
->filename
)
771 filename
= vfs_canon (edit
->filename
);
772 load_file_position (filename
, &line
, &column
, &offset
);
777 edit_move_to_line (edit
, line
- 1);
778 edit
->prev_col
= column
;
782 edit_cursor_move (edit
, offset
);
783 line
= edit
->curs_line
;
785 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
786 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
789 /* Save cursor position in the file */
791 edit_save_position (WEdit
* edit
)
795 if (!edit
->filename
|| !*edit
->filename
)
798 filename
= vfs_canon (edit
->filename
);
799 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
803 /* Clean the WEdit stricture except the widget part */
805 edit_purge_widget (WEdit
* edit
)
807 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
808 char *start
= (char *) edit
+ sizeof (Widget
);
809 memset (start
, 0, len
);
810 edit
->macro_i
= -1; /* not recording a macro */
814 edit_set_keymap (void)
816 editor_map
= default_editor_keymap
;
817 if (editor_keymap
&& editor_keymap
->len
> 0)
818 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
820 editor_x_map
= default_editor_x_keymap
;
821 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
822 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
826 #define space_width 1
829 * Fill in the edit structure. Return NULL on failure. Pass edit as
830 * NULL to allocate a new structure.
832 * If line is 0, try to restore saved position. Otherwise put the
833 * cursor on that line and show it in the middle of the screen.
836 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
839 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
840 if (option_line_state
)
842 option_line_state_width
= LINE_STATE_WIDTH
;
846 option_line_state_width
= 0;
852 * Expand option_whole_chars_search by national letters using
856 static char option_whole_chars_search_buf
[256];
858 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
861 size_t len
= str_term_width1 (option_whole_chars_search
);
863 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
865 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
867 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
869 option_whole_chars_search_buf
[len
++] = i
;
873 option_whole_chars_search_buf
[len
] = 0;
874 option_whole_chars_search
= option_whole_chars_search_buf
;
876 #endif /* ENABLE_NLS */
877 edit
= g_malloc0 (sizeof (WEdit
));
881 edit_purge_widget (edit
);
882 edit
->num_widget_lines
= lines
;
884 edit
->num_widget_columns
= columns
;
885 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
886 edit
->stat1
.st_uid
= getuid ();
887 edit
->stat1
.st_gid
= getgid ();
888 edit
->stat1
.st_mtime
= 0;
890 edit
->force
|= REDRAW_PAGE
;
891 edit_set_filename (edit
, filename
);
892 edit
->stack_size
= START_STACK_SIZE
;
893 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
894 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
897 edit
->converter
= str_cnv_from_term
;
898 edit_set_codeset (edit
);
900 if (edit_load_file (edit
))
902 /* edit_load_file already gives an error message */
908 edit
->loading_done
= 1;
911 edit_load_syntax (edit
, NULL
, NULL
);
914 edit_get_syntax_color (edit
, -1, &color
);
917 /* load saved cursor position */
918 if ((line
== 0) && option_save_position
)
920 edit_load_position (edit
);
926 edit_move_display (edit
, line
- 1);
927 edit_move_to_line (edit
, line
- 1);
935 /* Clear the edit struct, freeing everything in it. Return 1 on success */
937 edit_clean (WEdit
* edit
)
944 /* a stale lock, remove it */
946 edit
->locked
= edit_unlock_file (edit
);
948 /* save cursor position */
949 if (option_save_position
)
950 edit_save_position (edit
);
952 /* File specified on the mcedit command line and never saved */
953 if (edit
->delete_file
)
954 unlink (edit
->filename
);
956 edit_free_syntax_rules (edit
);
957 book_mark_flush (edit
, -1);
958 for (; j
<= MAXBUFF
; j
++)
960 g_free (edit
->buffers1
[j
]);
961 g_free (edit
->buffers2
[j
]);
964 g_free (edit
->undo_stack
);
965 g_free (edit
->filename
);
968 mc_search_free (edit
->search
);
971 if (edit
->converter
!= str_cnv_from_term
)
972 str_close_conv (edit
->converter
);
974 edit_purge_widget (edit
);
979 /* returns 1 on success */
981 edit_renew (WEdit
* edit
)
983 int lines
= edit
->num_widget_lines
;
984 int columns
= edit
->num_widget_columns
;
987 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
991 * Load a new file into the editor. If it fails, preserve the old file.
992 * To do it, allocate a new widget, initialize it and, if the new file
993 * was loaded, copy the data to the old widget.
994 * Return 1 on success, 0 on failure.
997 edit_reload (WEdit
* edit
, const char *filename
)
1000 int lines
= edit
->num_widget_lines
;
1001 int columns
= edit
->num_widget_columns
;
1003 e
= g_malloc0 (sizeof (WEdit
));
1004 e
->widget
= edit
->widget
;
1005 if (!edit_init (e
, lines
, columns
, filename
, 0))
1011 memcpy (edit
, e
, sizeof (WEdit
));
1017 * Load a new file into the editor and set line. If it fails, preserve the old file.
1018 * To do it, allocate a new widget, initialize it and, if the new file
1019 * was loaded, copy the data to the old widget.
1020 * Return 1 on success, 0 on failure.
1023 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
1026 int lines
= edit
->num_widget_lines
;
1027 int columns
= edit
->num_widget_columns
;
1029 e
= g_malloc0 (sizeof (WEdit
));
1030 e
->widget
= edit
->widget
;
1031 if (!edit_init (e
, lines
, columns
, filename
, line
))
1037 memcpy (edit
, e
, sizeof (WEdit
));
1043 edit_set_codeset (WEdit
*edit
)
1048 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
1053 conv
= str_crt_conv_from (cp_id
);
1054 if (conv
!= INVALID_CONV
)
1056 if (edit
->converter
!= str_cnv_from_term
)
1057 str_close_conv (edit
->converter
);
1058 edit
->converter
= conv
;
1063 edit
->utf8
= str_isutf8 (cp_id
);
1071 Recording stack for undo:
1072 The following is an implementation of a compressed stack. Identical
1073 pushes are recorded by a negative prefix indicating the number of times the
1074 same char was pushed. This saves space for repeated curs-left or curs-right
1091 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1092 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1093 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1094 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1097 The only way the cursor moves or the buffer is changed is through the routines:
1098 insert, backspace, insert_ahead, delete, and cursor_move.
1099 These record the reverse undo movements onto the stack each time they are
1102 Each key press results in a set of actions (insert; delete ...). So each time
1103 a key is pressed the current position of start_display is pushed as
1104 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1105 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1106 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1111 edit_push_action (WEdit
* edit
, long c
, ...)
1113 unsigned long sp
= edit
->stack_pointer
;
1117 /* first enlarge the stack if necessary */
1118 if (sp
> edit
->stack_size
- 10)
1120 if (option_max_undo
< 256)
1121 option_max_undo
= 256;
1122 if (edit
->stack_size
< (unsigned long) option_max_undo
)
1124 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1127 edit
->undo_stack
= t
;
1128 edit
->stack_size
<<= 1;
1129 edit
->stack_size_mask
= edit
->stack_size
- 1;
1133 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1134 if (edit
->stack_disable
)
1137 #ifdef FAST_MOVE_CURSOR
1138 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
1141 edit
->undo_stack
[sp
] = (c
== CURS_LEFT_LOTS
) ? CURS_LEFT
: CURS_RIGHT
;
1142 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1144 c
= -(va_arg (ap
, int));
1148 #endif /* ! FAST_MOVE_CURSOR */
1149 if (edit
->stack_bottom
!= sp
1150 && spm1
!= edit
->stack_bottom
1151 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
1154 if (edit
->undo_stack
[spm1
] < 0)
1156 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1159 if (edit
->undo_stack
[spm1
] > -1000000000)
1161 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1162 edit
->undo_stack
[spm1
]--;
1166 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1167 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1168 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1169 { /* a left then a right anihilate each other */
1170 if (edit
->undo_stack
[spm1
] == -2)
1171 edit
->stack_pointer
= spm1
;
1173 edit
->undo_stack
[spm1
]++;
1180 d
= edit
->undo_stack
[spm1
];
1184 return; /* --> no need to push multiple do-nothings */
1185 edit
->undo_stack
[sp
] = -2;
1188 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1189 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1190 { /* a left then a right anihilate each other */
1191 edit
->stack_pointer
= spm1
;
1197 edit
->undo_stack
[sp
] = c
;
1200 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1202 /* if the sp wraps round and catches the stack_bottom then erase
1203 * the first set of actions on the stack to make space - by moving
1204 * stack_bottom forward one "key press" */
1205 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1206 if ((unsigned long) c
== edit
->stack_bottom
||
1207 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1210 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1212 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
1213 && edit
->stack_bottom
!= edit
->stack_pointer
);
1215 /*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: */
1216 if (edit
->stack_pointer
!= edit
->stack_bottom
1217 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1218 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1222 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1223 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1226 pop_action (WEdit
* edit
)
1229 unsigned long sp
= edit
->stack_pointer
;
1231 if (sp
== edit
->stack_bottom
)
1232 return STACK_BOTTOM
;
1234 sp
= (sp
- 1) & edit
->stack_size_mask
;
1235 c
= edit
->undo_stack
[sp
];
1238 /* edit->undo_stack[sp] = '@'; */
1239 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1243 if (sp
== edit
->stack_bottom
)
1244 return STACK_BOTTOM
;
1246 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1247 if (edit
->undo_stack
[sp
] == -2)
1249 /* edit->undo_stack[sp] = '@'; */
1250 edit
->stack_pointer
= sp
;
1253 edit
->undo_stack
[sp
]++;
1258 /* is called whenever a modification is made by one of the four routines below */
1260 edit_modification (WEdit
* edit
)
1262 edit
->caches_valid
= 0;
1263 edit
->screen_modified
= 1;
1265 /* raise lock when file modified */
1266 if (!edit
->modified
&& !edit
->delete_file
)
1267 edit
->locked
= edit_lock_file (edit
);
1272 Basic low level single character buffer alterations and movements at the cursor.
1273 Returns char passed over, inserted or removed.
1277 edit_insert (WEdit
* edit
, int c
)
1279 /* check if file has grown to large */
1280 if (edit
->last_byte
>= SIZE_LIMIT
)
1283 /* first we must update the position of the display window */
1284 if (edit
->curs1
< edit
->start_display
)
1286 edit
->start_display
++;
1291 /* Mark file as modified, unless the file hasn't been fully loaded */
1292 if (edit
->loading_done
)
1294 edit_modification (edit
);
1297 /* now we must update some info on the file and check if a redraw is required */
1300 if (edit
->book_mark
)
1301 book_mark_inc (edit
, edit
->curs_line
);
1303 edit
->total_lines
++;
1304 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1307 /* save the reverse command onto the undo stack */
1308 edit_push_action (edit
, BACKSPACE
);
1310 /* update markers */
1311 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1312 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1313 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1315 /* add a new buffer if we've reached the end of the last one */
1316 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1317 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1319 /* perform the insertion */
1320 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
1321 = (unsigned char) c
;
1323 /* update file length */
1326 /* update cursor position */
1331 edit_insert_over (WEdit
* edit
)
1335 for (i
= 0; i
< edit
->over_col
; i
++)
1337 edit_insert (edit
, ' ');
1342 /* same as edit_insert and move left */
1344 edit_insert_ahead (WEdit
* edit
, int c
)
1346 if (edit
->last_byte
>= SIZE_LIMIT
)
1349 if (edit
->curs1
< edit
->start_display
)
1351 edit
->start_display
++;
1355 edit_modification (edit
);
1358 if (edit
->book_mark
)
1359 book_mark_inc (edit
, edit
->curs_line
);
1360 edit
->total_lines
++;
1361 edit
->force
|= REDRAW_AFTER_CURSOR
;
1363 edit_push_action (edit
, DELCHAR
);
1365 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1366 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1367 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1369 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1370 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1371 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
1372 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1380 edit_delete (WEdit
* edit
, const int byte_delete
)
1390 /* if byte_delete = 1 then delete only one byte not multibyte char */
1391 if (edit
->utf8
&& byte_delete
== 0)
1393 edit_get_utf (edit
, edit
->curs1
, &cw
);
1397 for (i
= 1; i
<= cw
; i
++)
1399 if (edit
->mark1
> edit
->curs1
)
1401 if (edit
->mark2
> edit
->curs1
)
1403 if (edit
->last_get_rule
> edit
->curs1
)
1404 edit
->last_get_rule
--;
1406 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1408 1) & M_EDIT_BUF_SIZE
) - 1];
1410 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1412 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1413 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1417 edit_push_action (edit
, p
+ 256);
1420 edit_modification (edit
);
1423 if (edit
->book_mark
)
1424 book_mark_dec (edit
, edit
->curs_line
);
1425 edit
->total_lines
--;
1426 edit
->force
|= REDRAW_AFTER_CURSOR
;
1428 if (edit
->curs1
< edit
->start_display
)
1430 edit
->start_display
--;
1440 edit_backspace (WEdit
* edit
, const int byte_delete
)
1451 if (edit
->utf8
&& byte_delete
== 0)
1453 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1457 for (i
= 1; i
<= cw
; i
++)
1459 if (edit
->mark1
>= edit
->curs1
)
1461 if (edit
->mark1
>= edit
->curs1
)
1463 if (edit
->last_get_rule
>= edit
->curs1
)
1464 edit
->last_get_rule
--;
1466 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
1467 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1468 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1470 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1471 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1475 edit_push_action (edit
, p
);
1477 edit_modification (edit
);
1480 if (edit
->book_mark
)
1481 book_mark_dec (edit
, edit
->curs_line
);
1483 edit
->total_lines
--;
1484 edit
->force
|= REDRAW_AFTER_CURSOR
;
1487 if (edit
->curs1
< edit
->start_display
)
1489 edit
->start_display
--;
1497 #ifdef FAST_MOVE_CURSOR
1500 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1503 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
1506 next
-= (unsigned long) dest
;
1514 edit_move_backward_lots (WEdit
* edit
, long increment
)
1517 unsigned char *p
= NULL
;
1519 if (increment
> edit
->curs1
)
1520 increment
= edit
->curs1
;
1523 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1525 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1528 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1533 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1534 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
1541 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1542 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1543 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1544 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1547 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1548 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1549 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1554 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1557 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1559 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1566 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1577 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1578 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
1583 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1584 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1587 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1589 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1590 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1595 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1598 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1600 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1605 return edit_get_byte (edit
, edit
->curs1
);
1608 #endif /* ! FAST_MOVE_CURSOR */
1610 /* moves the cursor right or left: increment positive or negative respectively */
1612 edit_cursor_move (WEdit
* edit
, long increment
)
1614 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1616 #ifdef FAST_MOVE_CURSOR
1617 if (increment
< -256)
1619 edit
->force
|= REDRAW_PAGE
;
1620 edit_move_backward_lots (edit
, -increment
);
1623 #endif /* ! FAST_MOVE_CURSOR */
1627 for (; increment
< 0; increment
++)
1632 edit_push_action (edit
, CURS_RIGHT
);
1634 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1635 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1636 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1637 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1638 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1640 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
1641 1) & M_EDIT_BUF_SIZE
];
1642 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1644 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1645 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1651 edit
->force
|= REDRAW_LINE_BELOW
;
1656 else if (increment
> 0)
1658 for (; increment
> 0; increment
--)
1663 edit_push_action (edit
, CURS_LEFT
);
1665 c
= edit_get_byte (edit
, edit
->curs1
);
1666 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1667 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1668 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1670 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1672 1) & M_EDIT_BUF_SIZE
) - 1];
1673 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1675 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1676 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1682 edit
->force
|= REDRAW_LINE_ABOVE
;
1688 /* These functions return positions relative to lines */
1690 /* returns index of last char on line + 1 */
1692 edit_eol (WEdit
* edit
, long current
)
1694 if (current
>= edit
->last_byte
)
1695 return edit
->last_byte
;
1698 if (edit_get_byte (edit
, current
) == '\n')
1703 /* returns index of first char on line */
1705 edit_bol (WEdit
* edit
, long current
)
1711 if (edit_get_byte (edit
, current
- 1) == '\n')
1718 edit_count_lines (WEdit
* edit
, long current
, long upto
)
1721 if (upto
> edit
->last_byte
)
1722 upto
= edit
->last_byte
;
1725 while (current
< upto
)
1726 if (edit_get_byte (edit
, current
++) == '\n')
1732 /* If lines is zero this returns the count of lines from current to upto. */
1733 /* If upto is zero returns index of lines forward current. */
1735 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1739 return edit_count_lines (edit
, current
, upto
);
1748 next
= edit_eol (edit
, current
) + 1;
1749 if (next
> edit
->last_byte
)
1759 /* Returns offset of 'lines' lines up from current */
1761 edit_move_backward (WEdit
* edit
, long current
, long lines
)
1765 current
= edit_bol (edit
, current
);
1766 while ((lines
--) && current
!= 0)
1767 current
= edit_bol (edit
, current
- 1);
1771 /* If cols is zero this returns the count of columns from current to upto. */
1772 /* If upto is zero returns index of cols across from current. */
1774 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1785 q
= edit
->last_byte
+ 2;
1787 for (col
= 0, p
= current
; p
< q
; p
++)
1801 orig_c
= c
= edit_get_byte (edit
, p
);
1805 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1810 if (g_unichar_iswide (utf_ch
))
1813 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1817 c
= convert_to_display_c (c
);
1819 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1827 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1828 /* '\r' is shown as ^M, so we must advance 2 characters */
1829 /* Caret notation for control characters */
1837 /* returns the current column position of the cursor */
1839 edit_get_col (WEdit
* edit
)
1841 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1845 /* Scrolling functions */
1848 edit_update_curs_row (WEdit
* edit
)
1850 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1854 edit_update_curs_col (WEdit
* edit
)
1856 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1860 edit_get_curs_col (const WEdit
* edit
)
1862 return edit
->curs_col
;
1865 /*moves the display start position up by i lines */
1867 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1869 unsigned long lines_above
= edit
->start_line
;
1870 if (i
> lines_above
)
1874 edit
->start_line
-= i
;
1875 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1876 edit
->force
|= REDRAW_PAGE
;
1877 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1879 edit_update_curs_row (edit
);
1883 /* returns 1 if could scroll, 0 otherwise */
1885 edit_scroll_downward (WEdit
* edit
, int i
)
1888 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1889 if (lines_below
> 0)
1891 if (i
> lines_below
)
1893 edit
->start_line
+= i
;
1894 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1895 edit
->force
|= REDRAW_PAGE
;
1896 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1898 edit_update_curs_row (edit
);
1902 edit_scroll_right (WEdit
* edit
, int i
)
1904 edit
->force
|= REDRAW_PAGE
;
1905 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1906 edit
->start_col
-= i
;
1910 edit_scroll_left (WEdit
* edit
, int i
)
1912 if (edit
->start_col
)
1914 edit
->start_col
+= i
;
1915 if (edit
->start_col
> 0)
1916 edit
->start_col
= 0;
1917 edit
->force
|= REDRAW_PAGE
;
1918 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1922 /* high level cursor movement commands */
1925 is_in_indent (WEdit
* edit
)
1927 long p
= edit_bol (edit
, edit
->curs1
);
1928 while (p
< edit
->curs1
)
1929 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1934 static int left_of_four_spaces (WEdit
* edit
);
1937 edit_move_to_prev_col (WEdit
* edit
, long p
)
1939 int prev
= edit
->prev_col
;
1940 int over
= edit
->over_col
;
1941 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1943 if (option_cursor_beyond_eol
)
1945 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
1946 edit_eol (edit
, edit
->curs1
));
1948 if (line_len
< prev
+ edit
->over_col
)
1950 edit
->over_col
= prev
+ over
- line_len
;
1951 edit
->prev_col
= line_len
;
1952 edit
->curs_col
= line_len
;
1956 edit
->curs_col
= prev
+ over
;
1957 edit
->prev_col
= edit
->curs_col
;
1964 if (is_in_indent (edit
) && option_fake_half_tabs
)
1966 edit_update_curs_col (edit
);
1968 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
1970 int q
= edit
->curs_col
;
1971 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1972 p
= edit_bol (edit
, edit
->curs1
);
1973 edit_cursor_move (edit
,
1974 edit_move_forward3 (edit
, p
, edit
->curs_col
,
1976 if (!left_of_four_spaces (edit
))
1977 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1984 is_blank (WEdit
* edit
, long offset
)
1988 s
= edit_bol (edit
, offset
);
1989 f
= edit_eol (edit
, offset
) - 1;
1992 c
= edit_get_byte (edit
, s
++);
2000 /* returns the offset of line i */
2002 edit_find_line (WEdit
* edit
, int line
)
2006 if (!edit
->caches_valid
)
2008 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2009 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
2010 /* three offsets that we *know* are line 0 at 0 and these two: */
2011 edit
->line_numbers
[1] = edit
->curs_line
;
2012 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
2013 edit
->line_numbers
[2] = edit
->total_lines
;
2014 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
2015 edit
->caches_valid
= 1;
2017 if (line
>= edit
->total_lines
)
2018 return edit
->line_offsets
[2];
2021 /* find the closest known point */
2022 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2025 n
= abs (edit
->line_numbers
[i
] - line
);
2033 return edit
->line_offsets
[j
]; /* know the offset exactly */
2034 if (m
== 1 && j
>= 3)
2035 i
= j
; /* one line different - caller might be looping, so stay in this cache */
2037 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
2038 if (line
> edit
->line_numbers
[j
])
2039 edit
->line_offsets
[i
] =
2040 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
2042 edit
->line_offsets
[i
] =
2043 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
2044 edit
->line_numbers
[i
] = line
;
2045 return edit
->line_offsets
[i
];
2049 line_is_blank (WEdit
* edit
, long line
)
2051 return is_blank (edit
, edit_find_line (edit
, line
));
2054 /* moves up until a blank line is reached, or until just
2055 before a non-blank line is reached */
2057 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
2060 if (edit
->curs_line
> 1)
2062 if (line_is_blank (edit
, edit
->curs_line
))
2064 if (line_is_blank (edit
, edit
->curs_line
- 1))
2066 for (i
= edit
->curs_line
- 1; i
; i
--)
2067 if (!line_is_blank (edit
, i
))
2075 for (i
= edit
->curs_line
- 1; i
; i
--)
2076 if (line_is_blank (edit
, i
))
2082 for (i
= edit
->curs_line
- 1; i
; i
--)
2083 if (line_is_blank (edit
, i
))
2087 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
2090 /* moves down until a blank line is reached, or until just
2091 before a non-blank line is reached */
2093 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
2096 if (edit
->curs_line
>= edit
->total_lines
- 1)
2098 i
= edit
->total_lines
;
2102 if (line_is_blank (edit
, edit
->curs_line
))
2104 if (line_is_blank (edit
, edit
->curs_line
+ 1))
2106 for (i
= edit
->curs_line
+ 1; i
; i
++)
2107 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
2115 for (i
= edit
->curs_line
+ 1; i
; i
++)
2116 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2122 for (i
= edit
->curs_line
+ 1; i
; i
++)
2123 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2127 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
2131 edit_begin_page (WEdit
* edit
)
2133 edit_update_curs_row (edit
);
2134 edit_move_up (edit
, edit
->curs_row
, 0);
2138 edit_end_page (WEdit
* edit
)
2140 edit_update_curs_row (edit
);
2141 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
2145 /* goto beginning of text */
2147 edit_move_to_top (WEdit
* edit
)
2149 if (edit
->curs_line
)
2151 edit_cursor_move (edit
, -edit
->curs1
);
2152 edit_move_to_prev_col (edit
, 0);
2153 edit
->force
|= REDRAW_PAGE
;
2154 edit
->search_start
= 0;
2155 edit_update_curs_row (edit
);
2160 /* goto end of text */
2162 edit_move_to_bottom (WEdit
* edit
)
2164 if (edit
->curs_line
< edit
->total_lines
)
2166 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
2167 edit
->start_display
= edit
->last_byte
;
2168 edit
->start_line
= edit
->total_lines
;
2169 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
2170 edit
->force
|= REDRAW_PAGE
;
2174 /* goto beginning of line */
2176 edit_cursor_to_bol (WEdit
* edit
)
2178 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
2179 edit
->search_start
= edit
->curs1
;
2180 edit
->prev_col
= edit_get_col (edit
);
2184 /* goto end of line */
2186 edit_cursor_to_eol (WEdit
* edit
)
2188 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
2189 edit
->search_start
= edit
->curs1
;
2190 edit
->prev_col
= edit_get_col (edit
);
2194 /* move cursor to line 'line' */
2196 edit_move_to_line (WEdit
* e
, long line
)
2198 if (line
< e
->curs_line
)
2199 edit_move_up (e
, e
->curs_line
- line
, 0);
2201 edit_move_down (e
, line
- e
->curs_line
, 0);
2202 edit_scroll_screen_over_cursor (e
);
2205 /* scroll window so that first visible line is 'line' */
2207 edit_move_display (WEdit
* e
, long line
)
2209 if (line
< e
->start_line
)
2210 edit_scroll_upward (e
, e
->start_line
- line
);
2212 edit_scroll_downward (e
, line
- e
->start_line
);
2215 /* save markers onto undo stack */
2217 edit_push_markers (WEdit
* edit
)
2219 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2220 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2224 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2233 /* highlight marker toggle */
2235 edit_mark_cmd (WEdit
* edit
, int unmark
)
2237 edit_push_markers (edit
);
2240 edit_set_markers (edit
, 0, 0, 0, 0);
2241 edit
->force
|= REDRAW_PAGE
;
2245 if (edit
->mark2
>= 0)
2247 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
2248 edit
->curs_col
+ edit
->over_col
);
2249 edit
->force
|= REDRAW_PAGE
;
2252 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
2253 edit
->curs_col
+ edit
->over_col
);
2257 static unsigned long
2262 const char option_chars_move_whole_word
[] =
2263 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2269 if (*option_chars_move_whole_word
== '!')
2271 return 0x80000000UL
;
2273 if (g_ascii_isupper ((gchar
) c
))
2275 else if (g_ascii_islower ((gchar
) c
))
2277 else if (g_ascii_isalpha (c
))
2279 else if (isdigit (c
))
2281 else if (isspace (c
))
2283 q
= strchr (option_chars_move_whole_word
, c
);
2285 return 0xFFFFFFFFUL
;
2288 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2293 while ((q
= strchr (q
+ 1, c
)));
2298 edit_left_word_move (WEdit
* edit
, int s
)
2303 if (edit
->column_highlight
2304 && edit
->mark1
!= edit
->mark2
2305 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2307 edit_cursor_move (edit
, -1);
2310 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2311 c2
= edit_get_byte (edit
, edit
->curs1
);
2312 if (!(my_type_of (c1
) & my_type_of (c2
)))
2314 if (isspace (c1
) && !isspace (c2
))
2317 if (!isspace (c1
) && isspace (c2
))
2323 edit_left_word_move_cmd (WEdit
* edit
)
2325 edit_left_word_move (edit
, 0);
2326 edit
->force
|= REDRAW_PAGE
;
2330 edit_right_word_move (WEdit
* edit
, int s
)
2335 if (edit
->column_highlight
2336 && edit
->mark1
!= edit
->mark2
2337 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
2339 edit_cursor_move (edit
, 1);
2340 if (edit
->curs1
>= edit
->last_byte
)
2342 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2343 c2
= edit_get_byte (edit
, edit
->curs1
);
2344 if (!(my_type_of (c1
) & my_type_of (c2
)))
2346 if (isspace (c1
) && !isspace (c2
))
2349 if (!isspace (c1
) && isspace (c2
))
2355 edit_right_word_move_cmd (WEdit
* edit
)
2357 edit_right_word_move (edit
, 0);
2358 edit
->force
|= REDRAW_PAGE
;
2362 edit_right_char_move_cmd (WEdit
* edit
)
2368 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2374 c
= edit_get_byte (edit
, edit
->curs1
);
2376 if (option_cursor_beyond_eol
&& c
== '\n')
2382 edit_cursor_move (edit
, cw
);
2387 edit_left_char_move_cmd (WEdit
* edit
)
2390 if (edit
->column_highlight
2391 && option_cursor_beyond_eol
2392 && edit
->mark1
!= edit
->mark2
2393 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2397 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2401 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2407 edit_cursor_move (edit
, -cw
);
2411 /** Up or down cursor moving.
2412 direction = TRUE - move up
2416 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2419 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
2428 edit
->force
|= REDRAW_PAGE
;
2432 edit_scroll_upward (edit
, i
);
2434 edit_scroll_downward (edit
, i
);
2436 p
= edit_bol (edit
, edit
->curs1
);
2438 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
2440 edit_cursor_move (edit
, p
- edit
->curs1
);
2442 edit_move_to_prev_col (edit
, p
);
2444 /* search start of current multibyte char (like CJK) */
2445 if (edit
->curs1
+ 1 < edit
->last_byte
)
2447 edit_right_char_move_cmd (edit
);
2448 edit_left_char_move_cmd (edit
);
2451 edit
->search_start
= edit
->curs1
;
2452 edit
->found_len
= 0;
2456 edit_right_delete_word (WEdit
* edit
)
2461 if (edit
->curs1
>= edit
->last_byte
)
2463 c1
= edit_delete (edit
, 1);
2464 c2
= edit_get_byte (edit
, edit
->curs1
);
2465 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2467 if (!(my_type_of (c1
) & my_type_of (c2
)))
2473 edit_left_delete_word (WEdit
* edit
)
2478 if (edit
->curs1
<= 0)
2480 c1
= edit_backspace (edit
, 1);
2481 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2482 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2484 if (!(my_type_of (c1
) & my_type_of (c2
)))
2490 the start column position is not recorded, and hence does not
2491 undo as it happed. But who would notice.
2494 edit_do_undo (WEdit
* edit
)
2499 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2501 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
2508 edit_cursor_move (edit
, 1);
2511 edit_cursor_move (edit
, -1);
2514 edit_backspace (edit
, 1);
2517 edit_delete (edit
, 1);
2520 edit
->column_highlight
= 1;
2523 edit
->column_highlight
= 0;
2526 if (ac
>= 256 && ac
< 512)
2527 edit_insert_ahead (edit
, ac
- 256);
2528 if (ac
>= 0 && ac
< 256)
2529 edit_insert (edit
, ac
);
2531 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
2533 edit
->mark1
= ac
- MARK_1
;
2534 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2536 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
2538 edit
->mark2
= ac
- MARK_2
;
2539 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2542 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2545 if (edit
->start_display
> ac
- KEY_PRESS
)
2547 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2548 edit
->force
|= REDRAW_PAGE
;
2550 else if (edit
->start_display
< ac
- KEY_PRESS
)
2552 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2553 edit
->force
|= REDRAW_PAGE
;
2555 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2556 edit_update_curs_row (edit
);
2559 edit
->stack_disable
= 0;
2563 edit_delete_to_line_end (WEdit
* edit
)
2565 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2569 edit_delete (edit
, 1);
2574 edit_delete_to_line_begin (WEdit
* edit
)
2576 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2580 edit_backspace (edit
, 1);
2585 edit_delete_line (WEdit
* edit
)
2588 * Delete right part of the line.
2589 * Note that edit_get_byte() returns '\n' when byte position is
2592 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2594 (void) edit_delete (edit
, 1);
2599 * Note that edit_delete() will not corrupt anything if called while
2600 * cursor position is EOF.
2602 (void) edit_delete (edit
, 1);
2605 * Delete left part of the line.
2606 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2608 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2610 (void) edit_backspace (edit
, 1);
2615 insert_spaces_tab (WEdit
* edit
, int half
)
2618 edit_update_curs_col (edit
);
2619 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
2620 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2623 edit_insert (edit
, ' ');
2629 is_aligned_on_a_tab (WEdit
* edit
)
2631 edit_update_curs_col (edit
);
2632 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2633 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2637 right_of_four_spaces (WEdit
* edit
)
2640 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2641 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2643 return is_aligned_on_a_tab (edit
);
2648 left_of_four_spaces (WEdit
* edit
)
2651 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2652 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2654 return is_aligned_on_a_tab (edit
);
2659 edit_indent_width (WEdit
* edit
, long p
)
2662 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2664 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2668 edit_insert_indent (WEdit
* edit
, int indent
)
2670 if (!option_fill_tabs_with_spaces
)
2672 while (indent
>= TAB_SIZE
)
2674 edit_insert (edit
, '\t');
2678 while (indent
-- > 0)
2679 edit_insert (edit
, ' ');
2683 edit_auto_indent (WEdit
* edit
)
2688 /* use the previous line as a template */
2689 p
= edit_move_backward (edit
, p
, 1);
2690 /* copy the leading whitespace of the line */
2692 { /* no range check - the line _is_ \n-terminated */
2693 c
= edit_get_byte (edit
, p
++);
2694 if (c
!= ' ' && c
!= '\t')
2696 edit_insert (edit
, c
);
2701 edit_double_newline (WEdit
* edit
)
2703 edit_insert (edit
, '\n');
2704 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2706 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2708 edit
->force
|= REDRAW_PAGE
;
2709 edit_insert (edit
, '\n');
2713 edit_tab_cmd (WEdit
* edit
)
2717 if (option_fake_half_tabs
)
2719 if (is_in_indent (edit
))
2721 /*insert a half tab (usually four spaces) unless there is a
2722 half tab already behind, then delete it and insert a
2724 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
2726 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2727 edit_backspace (edit
, 1);
2728 edit_insert (edit
, '\t');
2732 insert_spaces_tab (edit
, 1);
2737 if (option_fill_tabs_with_spaces
)
2739 insert_spaces_tab (edit
, 0);
2743 edit_insert (edit
, '\t');
2748 check_and_wrap_line (WEdit
* edit
)
2751 if (!option_typewriter_wrap
)
2753 edit_update_curs_col (edit
);
2754 if (edit
->curs_col
< option_word_wrap_line_length
)
2760 c
= edit_get_byte (edit
, curs
);
2761 if (c
== '\n' || curs
<= 0)
2763 edit_insert (edit
, '\n');
2766 if (c
== ' ' || c
== '\t')
2768 int current
= edit
->curs1
;
2769 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2770 edit_insert (edit
, '\n');
2771 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2777 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
2780 edit_push_key_press (WEdit
* edit
)
2782 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2783 if (edit
->mark2
== -1)
2784 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2787 /* this find the matching bracket in either direction, and sets edit->bracket */
2789 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2791 const char *const b
= "{}{[][()(", *p
;
2792 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2793 unsigned long j
= 0;
2795 edit_update_curs_row (edit
);
2796 c
= edit_get_byte (edit
, edit
->curs1
);
2799 if (!furthest_bracket_search
)
2800 furthest_bracket_search
--;
2801 /* not on a bracket at all */
2804 /* the matching bracket */
2806 /* going left or right? */
2807 if (strchr ("{[(", c
))
2809 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
2811 /* out of buffer? */
2812 if (q
>= edit
->last_byte
|| q
< 0)
2814 a
= edit_get_byte (edit
, q
);
2815 /* don't want to eat CPU */
2816 if (j
++ > furthest_bracket_search
)
2818 /* out of screen? */
2821 if (q
< edit
->start_display
)
2823 /* count lines if searching downward */
2824 if (inc
> 0 && a
== '\n')
2825 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2828 /* count bracket depth */
2829 i
+= (a
== c
) - (a
== d
);
2830 /* return if bracket depth is zero */
2838 static long last_bracket
= -1;
2841 edit_find_bracket (WEdit
* edit
)
2843 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2844 if (last_bracket
!= edit
->bracket
)
2845 edit
->force
|= REDRAW_PAGE
;
2846 last_bracket
= edit
->bracket
;
2850 edit_goto_matching_bracket (WEdit
* edit
)
2854 q
= edit_get_bracket (edit
, 0, 0);
2857 edit
->bracket
= edit
->curs1
;
2858 edit
->force
|= REDRAW_PAGE
;
2859 edit_cursor_move (edit
, q
- edit
->curs1
);
2864 * This executes a command as though the user initiated it through a key
2865 * press. Callback with WIDGET_KEY as a message calls this after
2866 * translating the key press. This function can be used to pass any
2867 * command to the editor. Note that the screen wouldn't update
2868 * automatically. Either of command or char_for_insertion must be
2869 * passed as -1. Commands are executed, and char_for_insertion is
2870 * inserted at the cursor.
2873 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2875 if (command
== CK_Begin_Record_Macro
)
2878 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2881 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
2883 edit
->force
|= REDRAW_COMPLETELY
;
2884 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2888 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
2890 edit
->macro
[edit
->macro_i
].command
= command
;
2891 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2893 /* record the beginning of a set of editing actions initiated by a key press */
2894 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2895 edit_push_key_press (edit
);
2897 edit_execute_cmd (edit
, command
, char_for_insertion
);
2898 if (edit
->column_highlight
)
2899 edit
->force
|= REDRAW_PAGE
;
2902 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
2905 This executes a command at a lower level than macro recording.
2906 It also does not push a key_press onto the undo stack. This means
2907 that if it is called many times, a single undo command will undo
2908 all of them. It also does not check for the Undo command.
2911 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2913 edit
->force
|= REDRAW_LINE
;
2915 /* The next key press will unhighlight the found string, so update
2917 if (edit
->found_len
|| edit
->column_highlight
)
2918 edit
->force
|= REDRAW_PAGE
;
2920 if (command
/ 100 == 6)
2921 { /* a highlight command like shift-arrow */
2922 edit
->column_highlight
= 0;
2923 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
2925 edit_mark_cmd (edit
, 1); /* clear */
2926 edit_mark_cmd (edit
, 0); /* marking on */
2928 edit
->highlight
= 1;
2931 { /* any other command */
2932 if (edit
->highlight
)
2933 edit_mark_cmd (edit
, 0); /* clear */
2934 edit
->highlight
= 0;
2937 /* first check for undo */
2938 if (command
== CK_Undo
)
2940 edit_do_undo (edit
);
2941 edit
->found_len
= 0;
2942 edit
->prev_col
= edit_get_col (edit
);
2943 edit
->search_start
= edit
->curs1
;
2947 /* An ordinary key press */
2948 if (char_for_insertion
>= 0)
2950 /* if non persistent selection and text selected */
2951 if (!option_persistent_selections
)
2953 if (edit
->mark1
!= edit
->mark2
)
2954 edit_block_delete_cmd (edit
);
2956 if (edit
->overwrite
)
2958 /* remove char only one time, after input first byte, multibyte chars */
2959 if ((!utf8_display
|| edit
->charpoint
== 0)
2960 && edit_get_byte (edit
, edit
->curs1
) != '\n')
2961 edit_delete (edit
, 0);
2963 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2964 edit_insert_over (edit
);
2966 if (char_for_insertion
> 255 && utf8_display
== 0)
2968 unsigned char str
[6 + 1];
2970 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
2980 while (str
[i
] != 0 && i
<= 6)
2982 char_for_insertion
= str
[i
];
2983 edit_insert (edit
, char_for_insertion
);
2989 edit_insert (edit
, char_for_insertion
);
2991 if (option_auto_para_formatting
)
2993 format_paragraph (edit
, 0);
2994 edit
->force
|= REDRAW_PAGE
;
2997 check_and_wrap_line (edit
);
2998 edit
->found_len
= 0;
2999 edit
->prev_col
= edit_get_col (edit
);
3000 edit
->search_start
= edit
->curs1
;
3001 edit_find_bracket (edit
);
3009 case CK_Begin_Page_Highlight
:
3010 case CK_End_Page_Highlight
:
3017 if (edit
->mark2
>= 0)
3019 if (!option_persistent_selections
)
3021 if (edit
->column_highlight
)
3022 edit_push_action (edit
, COLUMN_ON
);
3023 edit
->column_highlight
= 0;
3024 edit_mark_cmd (edit
, 1);
3033 case CK_Begin_Page_Highlight
:
3034 case CK_End_Page_Highlight
:
3039 case CK_Word_Left_Highlight
:
3040 case CK_Word_Right_Highlight
:
3041 case CK_Up_Highlight
:
3042 case CK_Down_Highlight
:
3043 case CK_Up_Alt_Highlight
:
3044 case CK_Down_Alt_Highlight
:
3045 if (edit
->mark2
== -1)
3046 break; /*marking is following the cursor: may need to highlight a whole line */
3049 case CK_Left_Highlight
:
3050 case CK_Right_Highlight
:
3051 edit
->force
|= REDRAW_CHAR_ONLY
;
3054 /* basic cursor key commands */
3058 /* if non persistent selection and text selected */
3059 if (!option_persistent_selections
)
3061 if (edit
->mark1
!= edit
->mark2
)
3063 edit_block_delete_cmd (edit
);
3067 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3072 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3074 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3075 edit_backspace (edit
, 1);
3080 if (option_fake_half_tabs
)
3083 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3085 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3086 edit_backspace (edit
, 1);
3091 edit_backspace (edit
, 0);
3094 /* if non persistent selection and text selected */
3095 if (!option_persistent_selections
)
3097 if (edit
->mark1
!= edit
->mark2
)
3099 edit_block_delete_cmd (edit
);
3104 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3105 edit_insert_over (edit
);
3107 if (option_fake_half_tabs
)
3110 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3112 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3113 edit_delete (edit
, 1);
3117 edit_delete (edit
, 0);
3119 case CK_Delete_Word_Left
:
3121 edit_left_delete_word (edit
);
3123 case CK_Delete_Word_Right
:
3124 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3125 edit_insert_over (edit
);
3127 edit_right_delete_word (edit
);
3129 case CK_Delete_Line
:
3130 edit_delete_line (edit
);
3132 case CK_Delete_To_Line_End
:
3133 edit_delete_to_line_end (edit
);
3135 case CK_Delete_To_Line_Begin
:
3136 edit_delete_to_line_begin (edit
);
3140 if (option_auto_para_formatting
)
3142 edit_double_newline (edit
);
3143 if (option_return_does_auto_indent
)
3144 edit_auto_indent (edit
);
3145 format_paragraph (edit
, 0);
3149 edit_insert (edit
, '\n');
3150 if (option_return_does_auto_indent
)
3152 edit_auto_indent (edit
);
3157 edit_insert (edit
, '\n');
3160 case CK_Page_Up_Alt_Highlight
:
3161 edit
->column_highlight
= 1;
3163 case CK_Page_Up_Highlight
:
3164 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3166 case CK_Page_Down_Alt_Highlight
:
3167 edit
->column_highlight
= 1;
3169 case CK_Page_Down_Highlight
:
3170 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3172 case CK_Left_Alt_Highlight
:
3173 edit
->column_highlight
= 1;
3175 case CK_Left_Highlight
:
3176 if (option_fake_half_tabs
)
3178 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3180 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3183 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3184 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3188 edit_left_char_move_cmd (edit
);
3190 case CK_Right_Alt_Highlight
:
3191 edit
->column_highlight
= 1;
3193 case CK_Right_Highlight
:
3194 if (option_fake_half_tabs
)
3196 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3198 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3199 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3203 edit_right_char_move_cmd (edit
);
3206 case CK_Begin_Page_Highlight
:
3207 edit_begin_page (edit
);
3210 case CK_End_Page_Highlight
:
3211 edit_end_page (edit
);
3214 case CK_Word_Left_Highlight
:
3216 edit_left_word_move_cmd (edit
);
3219 case CK_Word_Right_Highlight
:
3221 edit_right_word_move_cmd (edit
);
3223 case CK_Up_Alt_Highlight
:
3224 edit
->column_highlight
= 1;
3226 case CK_Up_Highlight
:
3227 edit_move_up (edit
, 1, 0);
3229 case CK_Down_Alt_Highlight
:
3230 edit
->column_highlight
= 1;
3232 case CK_Down_Highlight
:
3233 edit_move_down (edit
, 1, 0);
3235 case CK_Paragraph_Up_Alt_Highlight
:
3236 edit
->column_highlight
= 1;
3237 case CK_Paragraph_Up
:
3238 case CK_Paragraph_Up_Highlight
:
3239 edit_move_up_paragraph (edit
, 0);
3241 case CK_Paragraph_Down_Alt_Highlight
:
3242 edit
->column_highlight
= 1;
3243 case CK_Paragraph_Down
:
3244 case CK_Paragraph_Down_Highlight
:
3245 edit_move_down_paragraph (edit
, 0);
3247 case CK_Scroll_Up_Alt_Highlight
:
3248 edit
->column_highlight
= 1;
3250 case CK_Scroll_Up_Highlight
:
3251 edit_move_up (edit
, 1, 1);
3253 case CK_Scroll_Down_Alt_Highlight
:
3254 edit
->column_highlight
= 1;
3255 case CK_Scroll_Down
:
3256 case CK_Scroll_Down_Highlight
:
3257 edit_move_down (edit
, 1, 1);
3260 case CK_Home_Highlight
:
3261 edit_cursor_to_bol (edit
);
3264 case CK_End_Highlight
:
3265 edit_cursor_to_eol (edit
);
3268 /* if text marked shift block */
3269 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3271 if (edit
->mark2
< 0)
3272 edit_mark_cmd (edit
, 0);
3273 edit_move_block_to_right (edit
);
3277 if (option_cursor_beyond_eol
)
3278 edit_insert_over (edit
);
3279 edit_tab_cmd (edit
);
3280 if (option_auto_para_formatting
)
3282 format_paragraph (edit
, 0);
3283 edit
->force
|= REDRAW_PAGE
;
3287 check_and_wrap_line (edit
);
3292 case CK_Toggle_Insert
:
3293 edit
->overwrite
= (edit
->overwrite
== 0);
3297 if (edit
->mark2
>= 0)
3299 if (edit
->column_highlight
)
3300 edit_push_action (edit
, COLUMN_ON
);
3301 edit
->column_highlight
= 0;
3303 edit_mark_cmd (edit
, 0);
3305 case CK_Column_Mark
:
3306 if (!edit
->column_highlight
)
3307 edit_push_action (edit
, COLUMN_OFF
);
3308 edit
->column_highlight
= 1;
3309 edit_mark_cmd (edit
, 0);
3312 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3313 edit
->force
|= REDRAW_PAGE
;
3316 if (edit
->column_highlight
)
3317 edit_push_action (edit
, COLUMN_ON
);
3318 edit
->column_highlight
= 0;
3319 edit_mark_cmd (edit
, 1);
3322 case CK_Toggle_Line_State
:
3323 option_line_state
= !option_line_state
;
3324 if (option_line_state
)
3326 option_line_state_width
= LINE_STATE_WIDTH
;
3330 option_line_state_width
= 0;
3332 edit
->force
|= REDRAW_PAGE
;
3335 case CK_Toggle_Show_Margin
:
3336 show_right_margin
= !show_right_margin
;
3337 edit
->force
|= REDRAW_PAGE
;
3340 case CK_Toggle_Bookmark
:
3341 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3342 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3343 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3345 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3347 case CK_Flush_Bookmarks
:
3348 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3349 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3350 edit
->force
|= REDRAW_PAGE
;
3352 case CK_Next_Bookmark
:
3353 if (edit
->book_mark
)
3355 struct _book_mark
*p
;
3356 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3360 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3361 || p
->line
< edit
->start_line
)
3362 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3363 edit_move_to_line (edit
, p
->line
);
3367 case CK_Prev_Bookmark
:
3368 if (edit
->book_mark
)
3370 struct _book_mark
*p
;
3371 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3372 while (p
->line
== edit
->curs_line
)
3377 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3378 || p
->line
< edit
->start_line
)
3379 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3380 edit_move_to_line (edit
, p
->line
);
3385 case CK_Beginning_Of_Text
:
3386 case CK_Beginning_Of_Text_Highlight
:
3387 edit_move_to_top (edit
);
3389 case CK_End_Of_Text
:
3390 case CK_End_Of_Text_Highlight
:
3391 edit_move_to_bottom (edit
);
3395 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3396 edit_insert_over (edit
);
3397 edit_block_copy_cmd (edit
);
3400 edit_block_delete_cmd (edit
);
3403 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3404 edit_insert_over (edit
);
3405 edit_block_move_cmd (edit
);
3408 case CK_Shift_Block_Left
:
3409 if (edit
->mark1
!= edit
->mark2
)
3410 edit_move_block_to_left (edit
);
3412 case CK_Shift_Block_Right
:
3413 if (edit
->mark1
!= edit
->mark2
)
3414 edit_move_block_to_right (edit
);
3417 edit_copy_to_X_buf_cmd (edit
);
3420 edit_cut_to_X_buf_cmd (edit
);
3423 /* if non persistent selection and text selected */
3424 if (!option_persistent_selections
)
3426 if (edit
->mark1
!= edit
->mark2
)
3427 edit_block_delete_cmd (edit
);
3429 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3430 edit_insert_over (edit
);
3431 edit_paste_from_X_buf_cmd (edit
);
3433 case CK_Selection_History
:
3434 edit_paste_from_history (edit
);
3438 edit_save_as_cmd (edit
);
3441 edit_save_confirm_cmd (edit
);
3444 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3447 edit_save_block_cmd (edit
);
3449 case CK_Insert_File
:
3450 edit_insert_file_cmd (edit
);
3453 case CK_Load_Prev_File
:
3454 edit_load_back_cmd (edit
);
3456 case CK_Load_Next_File
:
3457 edit_load_forward_cmd (edit
);
3460 case CK_Load_Syntax_File
:
3461 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3463 case CK_Choose_Syntax
:
3464 edit_syntax_dialog (edit
, edit
->syntax_type
);
3467 case CK_Load_Menu_File
:
3468 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3471 case CK_Toggle_Syntax
:
3472 if ((option_syntax_highlighting
^= 1) == 1)
3473 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3474 edit
->force
|= REDRAW_PAGE
;
3477 case CK_Toggle_Tab_TWS
:
3478 enable_show_tabs_tws
^= 1;
3479 edit
->force
|= REDRAW_PAGE
;
3483 edit_search_cmd (edit
, 0);
3486 edit_search_cmd (edit
, 1);
3489 edit_replace_cmd (edit
, 0);
3491 case CK_Replace_Again
:
3492 edit_replace_cmd (edit
, 1);
3494 case CK_Complete_Word
:
3495 /* if text marked shift block */
3496 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3498 edit_move_block_to_left (edit
);
3502 edit_complete_word_cmd (edit
);
3505 case CK_Find_Definition
:
3506 edit_get_match_keyword_cmd (edit
);
3509 dlg_stop (edit
->widget
.owner
);
3512 edit_new_cmd (edit
);
3515 edit_help_cmd (edit
);
3518 edit_refresh_cmd (edit
);
3520 case CK_SaveSetupCmd
:
3524 query_dialog (_("About"),
3525 _("\n Cooledit v3.11.5\n\n"
3526 " Copyright (C) 1996 the Free Software Foundation\n\n"
3527 " A user friendly text editor written\n"
3528 " for the Midnight Commander.\n"), D_NORMAL
, 1, _("&OK"));
3533 case CK_Edit_Options
:
3534 edit_options_dialog (edit
);
3536 case CK_Edit_Save_Mode
:
3537 menu_save_mode_cmd ();
3542 /* fool gcc to prevent a Y2K warning */
3543 char time_format
[] = "_c";
3544 time_format
[0] = '%';
3546 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3547 edit_print_string (edit
, s
);
3548 edit
->force
|= REDRAW_PAGE
;
3553 edit_goto_cmd (edit
);
3555 case CK_Paragraph_Format
:
3556 format_paragraph (edit
, 1);
3557 edit
->force
|= REDRAW_PAGE
;
3559 case CK_Delete_Macro
:
3560 edit_delete_macro_cmd (edit
);
3562 case CK_Match_Bracket
:
3563 edit_goto_matching_bracket (edit
);
3569 edit_sort_cmd (edit
);
3572 edit_ext_cmd (edit
);
3575 edit_mail_dialog (edit
);
3580 case CK_SelectCodepage
:
3581 edit_select_codepage_cmd (edit
);
3583 case CK_Insert_Literal
:
3584 edit_insert_literal_cmd (edit
);
3586 case CK_Execute_Macro
:
3587 edit_execute_macro_cmd (edit
);
3589 case CK_Begin_End_Macro
:
3590 edit_begin_end_macro_cmd (edit
);
3600 if ((command
/ 1000) == 1) /* a shell command */
3601 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3602 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
3603 { /* a macro command */
3604 struct macro m
[MAX_MACRO_LENGTH
];
3606 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3607 edit_execute_macro (edit
, m
, nm
);
3610 /* keys which must set the col position, and the search vars */
3616 case CK_Replace_Again
:
3617 case CK_Complete_Word
:
3618 edit
->prev_col
= edit_get_col (edit
);
3621 case CK_Up_Highlight
:
3622 case CK_Up_Alt_Highlight
:
3624 case CK_Down_Highlight
:
3625 case CK_Down_Alt_Highlight
:
3627 case CK_Page_Up_Highlight
:
3628 case CK_Page_Up_Alt_Highlight
:
3630 case CK_Page_Down_Highlight
:
3631 case CK_Page_Down_Alt_Highlight
:
3632 case CK_Beginning_Of_Text
:
3633 case CK_Beginning_Of_Text_Highlight
:
3634 case CK_End_Of_Text
:
3635 case CK_End_Of_Text_Highlight
:
3636 case CK_Paragraph_Up
:
3637 case CK_Paragraph_Up_Highlight
:
3638 case CK_Paragraph_Up_Alt_Highlight
:
3639 case CK_Paragraph_Down
:
3640 case CK_Paragraph_Down_Highlight
:
3641 case CK_Paragraph_Down_Alt_Highlight
:
3643 case CK_Scroll_Up_Highlight
:
3644 case CK_Scroll_Up_Alt_Highlight
:
3645 case CK_Scroll_Down
:
3646 case CK_Scroll_Down_Highlight
:
3647 case CK_Scroll_Down_Alt_Highlight
:
3648 edit
->search_start
= edit
->curs1
;
3649 edit
->found_len
= 0;
3652 edit
->found_len
= 0;
3653 edit
->prev_col
= edit_get_col (edit
);
3654 edit
->search_start
= edit
->curs1
;
3656 edit_find_bracket (edit
);
3658 if (option_auto_para_formatting
)
3664 case CK_Delete_Word_Left
:
3665 case CK_Delete_Word_Right
:
3666 case CK_Delete_To_Line_End
:
3667 case CK_Delete_To_Line_Begin
:
3668 format_paragraph (edit
, 0);
3669 edit
->force
|= REDRAW_PAGE
;
3676 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
3680 if (edit
->macro_depth
++ > 256)
3682 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3683 edit
->macro_depth
--;
3686 edit
->force
|= REDRAW_PAGE
;
3689 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3691 edit_update_screen (edit
);
3692 edit
->macro_depth
--;
3695 /* User edit menu, like user menu (F2) but only in editor. */
3697 user_menu (WEdit
* edit
)
3701 long start_mark
, end_mark
;
3704 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3706 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3708 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3710 /* run shell scripts from menu */
3711 user_menu_cmd (edit
);
3713 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
3720 /* i.e. we have marked block */
3721 rc
= edit_block_delete_cmd (edit
);
3725 edit_insert_file (edit
, block_file
);
3727 /* truncate block file */
3728 fd
= fopen (block_file
, "w");
3732 edit_refresh_cmd (edit
);
3733 edit
->force
|= REDRAW_COMPLETELY
;
3735 g_free (block_file
);
3739 edit_stack_init (void)
3741 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3743 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3744 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3747 edit_stack_iterator
= 0;
3751 edit_stack_free (void)
3753 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3754 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3759 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3761 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3766 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3768 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
3772 edit_unlock_file (WEdit
* edit
)
3777 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
3778 ret
= unlock_file (fullpath
);
3785 edit_lock_file (WEdit
* edit
)
3790 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
3791 ret
= lock_file (fullpath
);