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"
60 #include "src/keybind.h"
62 #include "edit-impl.h"
64 #include "edit-widget.h"
66 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
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 static void user_menu (WEdit
* edit
);
128 edit_get_byte (WEdit
* edit
, long byte_index
)
131 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
134 if (byte_index
>= edit
->curs1
)
136 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
137 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
141 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
146 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
149 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
152 if (byte_index
>= edit
->curs1
)
154 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
155 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
156 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
160 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
161 (byte_index
& M_EDIT_BUF_SIZE
));
166 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
170 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
176 if (byte_index
>= edit
->curs1
)
178 p
= edit
->curs1
+ edit
->curs2
- 1;
179 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
180 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
184 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
189 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
194 gchar
*next_ch
= NULL
;
197 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
203 str
= edit_get_byte_ptr (edit
, byte_index
);
211 res
= g_utf8_get_char_validated (str
, -1);
221 /* Calculate UTF-8 char width */
222 next_ch
= g_utf8_next_char (str
);
225 width
= next_ch
- str
;
238 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
240 gchar
*str
, *buf
= NULL
;
243 gchar
*next_ch
= NULL
;
249 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
255 ch
= edit_get_utf (edit
, byte_index
, &width
);
263 str
= edit_get_byte_ptr (edit
, byte_index
);
264 buf
= edit_get_buf_ptr (edit
, byte_index
);
265 if (str
== NULL
|| buf
== NULL
)
270 /* get prev utf8 char */
272 str
= g_utf8_find_prev_char (buf
, str
);
274 res
= g_utf8_get_char_validated (str
, -1);
284 /* Calculate UTF-8 char width */
285 next_ch
= g_utf8_next_char (str
);
288 width
= next_ch
- str
;
301 * Initialize the buffers for an empty files.
304 edit_init_buffers (WEdit
* edit
)
308 for (j
= 0; j
<= MAXBUFF
; j
++)
310 edit
->buffers1
[j
] = NULL
;
311 edit
->buffers2
[j
] = NULL
;
316 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
320 * Load file OR text into buffers. Set cursor to the beginning of file.
324 edit_load_file_fast (WEdit
* edit
, const char *filename
)
330 edit
->curs2
= edit
->last_byte
;
331 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
333 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
338 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
339 edit_error_dialog (_("Error"), errmsg
);
344 if (!edit
->buffers2
[buf2
])
345 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
350 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
351 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
354 for (buf
= buf2
- 1; buf
>= 0; buf
--)
356 /* edit->buffers2[0] is already allocated */
357 if (!edit
->buffers2
[buf
])
358 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
359 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
367 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
368 edit_error_dialog (_("Error"), err_str
);
375 /* detecting an error on save is easy: just check if every byte has been written. */
376 /* detecting an error on read, is not so easy 'cos there is not way to tell
377 whether you read everything or not. */
378 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
379 static const struct edit_filters
381 const char *read
, *write
, *extension
;
385 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
386 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
387 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
388 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
389 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
393 /* Return index of the filter or -1 is there is no appropriate filter */
395 edit_find_filter (const char *filename
)
399 if (filename
== NULL
)
402 l
= strlen (filename
);
403 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
405 e
= strlen (all_filters
[i
].extension
);
407 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
414 edit_get_filter (const char *filename
)
417 char *p
, *quoted_name
;
419 i
= edit_find_filter (filename
);
423 quoted_name
= name_quote (filename
, 0);
424 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
425 g_free (quoted_name
);
430 edit_get_write_filter (const char *write_name
, const char *filename
)
435 i
= edit_find_filter (filename
);
439 writename
= name_quote (write_name
, 0);
440 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
446 edit_insert_stream (WEdit
* edit
, FILE * f
)
450 while ((c
= fgetc (f
)) >= 0)
452 edit_insert (edit
, c
);
459 edit_write_stream (WEdit
* edit
, FILE * f
)
463 if (edit
->lb
== LB_ASIS
)
465 for (i
= 0; i
< edit
->last_byte
; i
++)
466 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
471 /* change line breaks */
472 for (i
= 0; i
< edit
->last_byte
; i
++)
474 unsigned char c
= edit_get_byte (edit
, i
);
476 if (!(c
== '\n' || c
== '\r'))
479 if (fputc (c
, f
) < 0)
483 { /* (c == '\n' || c == '\r') */
484 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
488 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
489 /* put one line break unconditionally */
490 if (fputc ('\n', f
) < 0)
493 i
++; /* 2 chars are processed */
495 if (c
== '\r' && c1
== '\n')
496 /* Windows line break; go to the next char */
499 if (c
== '\r' && c1
== '\r')
501 /* two Macintosh line breaks; put second line break */
502 if (fputc ('\n', f
) < 0)
507 if (fputc (c1
, f
) < 0)
511 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
512 /* put one line break unconditionally */
513 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
516 if (c
== '\r' && c1
== '\n')
517 /* Windows line break; go to the next char */
521 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
522 /* put one line break unconditionally */
523 if (fputc ('\r', f
) < 0)
526 i
++; /* 2 chars are processed */
528 if (c
== '\r' && c1
== '\n')
529 /* Windows line break; go to the next char */
532 if (c
== '\n' && c1
== '\n')
534 /* two Windows line breaks; put second line break */
535 if (fputc ('\r', f
) < 0)
540 if (fputc (c1
, f
) < 0)
543 case LB_ASIS
: /* default without changes */
549 return edit
->last_byte
;
552 #define TEMP_BUF_LEN 1024
554 /* inserts a file at the cursor, returns 1 on success */
556 edit_insert_file (WEdit
* edit
, const char *filename
)
560 p
= edit_get_filter (filename
);
564 long current
= edit
->curs1
;
565 f
= (FILE *) popen (p
, "r");
568 edit_insert_stream (edit
, f
);
569 edit_cursor_move (edit
, current
- edit
->curs1
);
573 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
574 edit_error_dialog (_("Error"), errmsg
);
583 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
584 edit_error_dialog (_("Error"), errmsg
);
593 int i
, file
, blocklen
;
594 long current
= edit
->curs1
;
595 int vertical_insertion
= 0;
597 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
600 buf
= g_malloc0 (TEMP_BUF_LEN
);
601 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
604 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
605 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
606 vertical_insertion
= 1;
608 mc_lseek (file
, 0, SEEK_SET
);
610 if (vertical_insertion
)
611 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
613 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
614 for (i
= 0; i
< blocklen
; i
++)
615 edit_insert (edit
, buf
[i
]);
616 edit_cursor_move (edit
, current
- edit
->curs1
);
625 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
627 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
630 gchar
*errmsg
= NULL
;
632 /* Try opening an existing file */
633 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
638 * Try creating the file. O_EXCL prevents following broken links
639 * and opening existing files.
641 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
644 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
649 /* New file, delete it if it's not modified or saved */
650 edit
->delete_file
= 1;
654 /* Check what we have opened */
655 if (mc_fstat (file
, st
) < 0)
657 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
661 /* We want to open regular files only */
662 if (!S_ISREG (st
->st_mode
))
664 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
669 * Don't delete non-empty files.
670 * O_EXCL should prevent it, but let's be on the safe side.
673 edit
->delete_file
= 0;
675 if (st
->st_size
>= SIZE_LIMIT
)
676 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
679 (void) mc_close (file
);
683 edit_error_dialog (_("Error"), errmsg
);
691 * Open the file and load it into the buffers, either directly or using
692 * a filter. Return 0 on success, 1 on error.
694 * Fast loading (edit_load_file_fast) is used when the file size is
695 * known. In this case the data is read into the buffers by blocks.
696 * If the file size is not known, the data is loaded byte by byte in
700 edit_load_file (WEdit
* edit
)
704 /* Cannot do fast load if a filter is used */
705 if (edit_find_filter (edit
->filename
) >= 0)
709 * VFS may report file size incorrectly, and slow load is not a big
710 * deal considering overhead in VFS.
712 if (!vfs_file_is_local (edit
->filename
))
716 * FIXME: line end translation should disable fast loading as well
717 * Consider doing fseek() to the end and ftell() for the real size.
722 /* If we are dealing with a real file, check that it exists */
723 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
728 /* nothing to load */
732 edit_init_buffers (edit
);
736 edit
->last_byte
= edit
->stat1
.st_size
;
737 edit_load_file_fast (edit
, edit
->filename
);
738 /* If fast load was used, the number of lines wasn't calculated */
739 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
744 const char *codepage_id
;
749 edit
->stack_disable
= 1;
750 if (!edit_insert_file (edit
, edit
->filename
))
755 edit
->stack_disable
= 0;
759 codepage_id
= get_codepage_id (source_codepage
);
761 edit
->utf8
= str_isutf8 (codepage_id
);
768 /* Restore saved cursor position in the file */
770 edit_load_position (WEdit
* edit
)
776 if (!edit
->filename
|| !*edit
->filename
)
779 filename
= vfs_canon (edit
->filename
);
780 load_file_position (filename
, &line
, &column
, &offset
);
785 edit_move_to_line (edit
, line
- 1);
786 edit
->prev_col
= column
;
790 edit_cursor_move (edit
, offset
);
791 line
= edit
->curs_line
;
793 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
794 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
797 /* Save cursor position in the file */
799 edit_save_position (WEdit
* edit
)
803 if (!edit
->filename
|| !*edit
->filename
)
806 filename
= vfs_canon (edit
->filename
);
807 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
811 /* Clean the WEdit stricture except the widget part */
813 edit_purge_widget (WEdit
* edit
)
815 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
816 char *start
= (char *) edit
+ sizeof (Widget
);
817 memset (start
, 0, len
);
818 edit
->macro_i
= -1; /* not recording a macro */
822 edit_set_keymap (void)
824 editor_map
= default_editor_keymap
;
825 if (editor_keymap
&& editor_keymap
->len
> 0)
826 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
828 editor_x_map
= default_editor_x_keymap
;
829 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
830 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
834 #define space_width 1
837 * Fill in the edit structure. Return NULL on failure. Pass edit as
838 * NULL to allocate a new structure.
840 * If line is 0, try to restore saved position. Otherwise put the
841 * cursor on that line and show it in the middle of the screen.
844 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
847 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
848 if (option_line_state
)
850 option_line_state_width
= LINE_STATE_WIDTH
;
854 option_line_state_width
= 0;
860 * Expand option_whole_chars_search by national letters using
864 static char option_whole_chars_search_buf
[256];
866 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
869 size_t len
= str_term_width1 (option_whole_chars_search
);
871 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
873 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
875 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
877 option_whole_chars_search_buf
[len
++] = i
;
881 option_whole_chars_search_buf
[len
] = 0;
882 option_whole_chars_search
= option_whole_chars_search_buf
;
884 #endif /* ENABLE_NLS */
885 edit
= g_malloc0 (sizeof (WEdit
));
889 edit_purge_widget (edit
);
890 edit
->num_widget_lines
= lines
;
892 edit
->num_widget_columns
= columns
;
893 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
894 edit
->stat1
.st_uid
= getuid ();
895 edit
->stat1
.st_gid
= getgid ();
896 edit
->stat1
.st_mtime
= 0;
898 edit
->force
|= REDRAW_PAGE
;
899 edit_set_filename (edit
, filename
);
900 edit
->stack_size
= START_STACK_SIZE
;
901 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
902 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
905 edit
->converter
= str_cnv_from_term
;
906 edit_set_codeset (edit
);
908 if (edit_load_file (edit
))
910 /* edit_load_file already gives an error message */
916 edit
->loading_done
= 1;
919 edit_load_syntax (edit
, NULL
, NULL
);
922 edit_get_syntax_color (edit
, -1, &color
);
925 /* load saved cursor position */
926 if ((line
== 0) && option_save_position
)
928 edit_load_position (edit
);
934 edit_move_display (edit
, line
- 1);
935 edit_move_to_line (edit
, line
- 1);
943 /* Clear the edit struct, freeing everything in it. Return 1 on success */
945 edit_clean (WEdit
* edit
)
952 /* a stale lock, remove it */
954 edit
->locked
= edit_unlock_file (edit
->filename
);
956 /* save cursor position */
957 if (option_save_position
)
958 edit_save_position (edit
);
960 /* File specified on the mcedit command line and never saved */
961 if (edit
->delete_file
)
962 unlink (edit
->filename
);
964 edit_free_syntax_rules (edit
);
965 book_mark_flush (edit
, -1);
966 for (; j
<= MAXBUFF
; j
++)
968 g_free (edit
->buffers1
[j
]);
969 g_free (edit
->buffers2
[j
]);
972 g_free (edit
->undo_stack
);
973 g_free (edit
->filename
);
976 mc_search_free (edit
->search
);
979 if (edit
->converter
!= str_cnv_from_term
)
980 str_close_conv (edit
->converter
);
982 edit_purge_widget (edit
);
987 /* returns 1 on success */
989 edit_renew (WEdit
* edit
)
991 int lines
= edit
->num_widget_lines
;
992 int columns
= edit
->num_widget_columns
;
995 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
999 * Load a new file into the editor. If it fails, preserve the old file.
1000 * To do it, allocate a new widget, initialize it and, if the new file
1001 * was loaded, copy the data to the old widget.
1002 * Return 1 on success, 0 on failure.
1005 edit_reload (WEdit
* edit
, const char *filename
)
1008 int lines
= edit
->num_widget_lines
;
1009 int columns
= edit
->num_widget_columns
;
1011 e
= g_malloc0 (sizeof (WEdit
));
1012 e
->widget
= edit
->widget
;
1013 if (!edit_init (e
, lines
, columns
, filename
, 0))
1019 memcpy (edit
, e
, sizeof (WEdit
));
1025 * Load a new file into the editor and set line. If it fails, preserve the old file.
1026 * To do it, allocate a new widget, initialize it and, if the new file
1027 * was loaded, copy the data to the old widget.
1028 * Return 1 on success, 0 on failure.
1031 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
1034 int lines
= edit
->num_widget_lines
;
1035 int columns
= edit
->num_widget_columns
;
1037 e
= g_malloc0 (sizeof (WEdit
));
1038 e
->widget
= edit
->widget
;
1039 if (!edit_init (e
, lines
, columns
, filename
, line
))
1045 memcpy (edit
, e
, sizeof (WEdit
));
1051 edit_set_codeset (WEdit
*edit
)
1056 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
1061 conv
= str_crt_conv_from (cp_id
);
1062 if (conv
!= INVALID_CONV
)
1064 if (edit
->converter
!= str_cnv_from_term
)
1065 str_close_conv (edit
->converter
);
1066 edit
->converter
= conv
;
1071 edit
->utf8
= str_isutf8 (cp_id
);
1079 Recording stack for undo:
1080 The following is an implementation of a compressed stack. Identical
1081 pushes are recorded by a negative prefix indicating the number of times the
1082 same char was pushed. This saves space for repeated curs-left or curs-right
1099 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1100 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1101 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1102 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1105 The only way the cursor moves or the buffer is changed is through the routines:
1106 insert, backspace, insert_ahead, delete, and cursor_move.
1107 These record the reverse undo movements onto the stack each time they are
1110 Each key press results in a set of actions (insert; delete ...). So each time
1111 a key is pressed the current position of start_display is pushed as
1112 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1113 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1114 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1119 edit_push_action (WEdit
* edit
, long c
, ...)
1121 unsigned long sp
= edit
->stack_pointer
;
1125 /* first enlarge the stack if necessary */
1126 if (sp
> edit
->stack_size
- 10)
1128 if (option_max_undo
< 256)
1129 option_max_undo
= 256;
1130 if (edit
->stack_size
< (unsigned long) option_max_undo
)
1132 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1135 edit
->undo_stack
= t
;
1136 edit
->stack_size
<<= 1;
1137 edit
->stack_size_mask
= edit
->stack_size
- 1;
1141 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1142 if (edit
->stack_disable
)
1145 #ifdef FAST_MOVE_CURSOR
1146 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
1149 edit
->undo_stack
[sp
] = (c
== CURS_LEFT_LOTS
) ? CURS_LEFT
: CURS_RIGHT
;
1150 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1152 c
= -(va_arg (ap
, int));
1156 #endif /* ! FAST_MOVE_CURSOR */
1157 if (edit
->stack_bottom
!= sp
1158 && spm1
!= edit
->stack_bottom
1159 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
1162 if (edit
->undo_stack
[spm1
] < 0)
1164 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1167 if (edit
->undo_stack
[spm1
] > -1000000000)
1169 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1170 edit
->undo_stack
[spm1
]--;
1174 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1175 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1176 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1177 { /* a left then a right anihilate each other */
1178 if (edit
->undo_stack
[spm1
] == -2)
1179 edit
->stack_pointer
= spm1
;
1181 edit
->undo_stack
[spm1
]++;
1188 d
= edit
->undo_stack
[spm1
];
1192 return; /* --> no need to push multiple do-nothings */
1193 edit
->undo_stack
[sp
] = -2;
1196 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1197 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1198 { /* a left then a right anihilate each other */
1199 edit
->stack_pointer
= spm1
;
1205 edit
->undo_stack
[sp
] = c
;
1208 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1210 /* if the sp wraps round and catches the stack_bottom then erase
1211 * the first set of actions on the stack to make space - by moving
1212 * stack_bottom forward one "key press" */
1213 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1214 if ((unsigned long) c
== edit
->stack_bottom
||
1215 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1218 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1220 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
1221 && edit
->stack_bottom
!= edit
->stack_pointer
);
1223 /*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: */
1224 if (edit
->stack_pointer
!= edit
->stack_bottom
1225 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1226 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1230 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1231 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1234 pop_action (WEdit
* edit
)
1237 unsigned long sp
= edit
->stack_pointer
;
1239 if (sp
== edit
->stack_bottom
)
1240 return STACK_BOTTOM
;
1242 sp
= (sp
- 1) & edit
->stack_size_mask
;
1243 c
= edit
->undo_stack
[sp
];
1246 /* edit->undo_stack[sp] = '@'; */
1247 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1251 if (sp
== edit
->stack_bottom
)
1252 return STACK_BOTTOM
;
1254 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1255 if (edit
->undo_stack
[sp
] == -2)
1257 /* edit->undo_stack[sp] = '@'; */
1258 edit
->stack_pointer
= sp
;
1261 edit
->undo_stack
[sp
]++;
1266 /* is called whenever a modification is made by one of the four routines below */
1268 edit_modification (WEdit
* edit
)
1270 edit
->caches_valid
= 0;
1271 edit
->screen_modified
= 1;
1273 /* raise lock when file modified */
1274 if (!edit
->modified
&& !edit
->delete_file
)
1275 edit
->locked
= edit_lock_file (edit
->filename
);
1280 Basic low level single character buffer alterations and movements at the cursor.
1281 Returns char passed over, inserted or removed.
1285 edit_insert (WEdit
* edit
, int c
)
1287 /* check if file has grown to large */
1288 if (edit
->last_byte
>= SIZE_LIMIT
)
1291 /* first we must update the position of the display window */
1292 if (edit
->curs1
< edit
->start_display
)
1294 edit
->start_display
++;
1299 /* Mark file as modified, unless the file hasn't been fully loaded */
1300 if (edit
->loading_done
)
1302 edit_modification (edit
);
1305 /* now we must update some info on the file and check if a redraw is required */
1308 if (edit
->book_mark
)
1309 book_mark_inc (edit
, edit
->curs_line
);
1311 edit
->total_lines
++;
1312 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1315 /* save the reverse command onto the undo stack */
1316 edit_push_action (edit
, BACKSPACE
);
1318 /* update markers */
1319 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1320 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1321 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1323 /* add a new buffer if we've reached the end of the last one */
1324 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1325 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1327 /* perform the insertion */
1328 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
1329 = (unsigned char) c
;
1331 /* update file length */
1334 /* update cursor position */
1339 edit_insert_over (WEdit
* edit
)
1343 for (i
= 0; i
< edit
->over_col
; i
++)
1345 edit_insert (edit
, ' ');
1350 /* same as edit_insert and move left */
1352 edit_insert_ahead (WEdit
* edit
, int c
)
1354 if (edit
->last_byte
>= SIZE_LIMIT
)
1357 if (edit
->curs1
< edit
->start_display
)
1359 edit
->start_display
++;
1363 edit_modification (edit
);
1366 if (edit
->book_mark
)
1367 book_mark_inc (edit
, edit
->curs_line
);
1368 edit
->total_lines
++;
1369 edit
->force
|= REDRAW_AFTER_CURSOR
;
1371 edit_push_action (edit
, DELCHAR
);
1373 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1374 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1375 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1377 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1378 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1379 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
1380 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1388 edit_delete (WEdit
* edit
, const int byte_delete
)
1397 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1398 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1399 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1402 /* if byte_delete = 1 then delete only one byte not multibyte char */
1403 if (edit
->utf8
&& byte_delete
== 0)
1405 edit_get_utf (edit
, edit
->curs1
, &cw
);
1409 for (i
= 1; i
<= cw
; i
++)
1411 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1413 1) & M_EDIT_BUF_SIZE
) - 1];
1415 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1417 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1418 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1422 edit_push_action (edit
, p
+ 256);
1425 edit_modification (edit
);
1428 if (edit
->book_mark
)
1429 book_mark_dec (edit
, edit
->curs_line
);
1430 edit
->total_lines
--;
1431 edit
->force
|= REDRAW_AFTER_CURSOR
;
1433 if (edit
->curs1
< edit
->start_display
)
1435 edit
->start_display
--;
1445 edit_backspace (WEdit
* edit
, const int byte_delete
)
1454 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1455 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1456 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1459 if (edit
->utf8
&& byte_delete
== 0)
1461 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1465 for (i
= 1; i
<= cw
; i
++)
1467 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
1468 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1469 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1471 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1472 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1476 edit_push_action (edit
, p
);
1478 edit_modification (edit
);
1481 if (edit
->book_mark
)
1482 book_mark_dec (edit
, edit
->curs_line
);
1484 edit
->total_lines
--;
1485 edit
->force
|= REDRAW_AFTER_CURSOR
;
1488 if (edit
->curs1
< edit
->start_display
)
1490 edit
->start_display
--;
1498 #ifdef FAST_MOVE_CURSOR
1501 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1504 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
1507 next
-= (unsigned long) dest
;
1515 edit_move_backward_lots (WEdit
* edit
, long increment
)
1518 unsigned char *p
= NULL
;
1520 if (increment
> edit
->curs1
)
1521 increment
= edit
->curs1
;
1524 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1526 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1529 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1534 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1535 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
1542 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1543 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1544 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1545 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1548 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1549 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1550 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1555 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1558 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1560 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1567 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1578 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1579 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
1584 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1585 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1588 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1590 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1591 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1596 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1599 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1601 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1606 return edit_get_byte (edit
, edit
->curs1
);
1609 #endif /* ! FAST_MOVE_CURSOR */
1611 /* moves the cursor right or left: increment positive or negative respectively */
1613 edit_cursor_move (WEdit
* edit
, long increment
)
1615 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1617 #ifdef FAST_MOVE_CURSOR
1618 if (increment
< -256)
1620 edit
->force
|= REDRAW_PAGE
;
1621 edit_move_backward_lots (edit
, -increment
);
1624 #endif /* ! FAST_MOVE_CURSOR */
1628 for (; increment
< 0; increment
++)
1633 edit_push_action (edit
, CURS_RIGHT
);
1635 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1636 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1637 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1638 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1639 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1641 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
1642 1) & M_EDIT_BUF_SIZE
];
1643 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1645 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1646 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1652 edit
->force
|= REDRAW_LINE_BELOW
;
1657 else if (increment
> 0)
1659 for (; increment
> 0; increment
--)
1664 edit_push_action (edit
, CURS_LEFT
);
1666 c
= edit_get_byte (edit
, edit
->curs1
);
1667 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1668 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1669 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1671 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1673 1) & M_EDIT_BUF_SIZE
) - 1];
1674 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1676 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1677 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1683 edit
->force
|= REDRAW_LINE_ABOVE
;
1689 /* These functions return positions relative to lines */
1691 /* returns index of last char on line + 1 */
1693 edit_eol (WEdit
* edit
, long current
)
1695 if (current
>= edit
->last_byte
)
1696 return edit
->last_byte
;
1699 if (edit_get_byte (edit
, current
) == '\n')
1704 /* returns index of first char on line */
1706 edit_bol (WEdit
* edit
, long current
)
1712 if (edit_get_byte (edit
, current
- 1) == '\n')
1719 edit_count_lines (WEdit
* edit
, long current
, long upto
)
1722 if (upto
> edit
->last_byte
)
1723 upto
= edit
->last_byte
;
1726 while (current
< upto
)
1727 if (edit_get_byte (edit
, current
++) == '\n')
1733 /* If lines is zero this returns the count of lines from current to upto. */
1734 /* If upto is zero returns index of lines forward current. */
1736 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1740 return edit_count_lines (edit
, current
, upto
);
1749 next
= edit_eol (edit
, current
) + 1;
1750 if (next
> edit
->last_byte
)
1760 /* Returns offset of 'lines' lines up from current */
1762 edit_move_backward (WEdit
* edit
, long current
, long lines
)
1766 current
= edit_bol (edit
, current
);
1767 while ((lines
--) && current
!= 0)
1768 current
= edit_bol (edit
, current
- 1);
1772 /* If cols is zero this returns the count of columns from current to upto. */
1773 /* If upto is zero returns index of cols across from current. */
1775 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1786 q
= edit
->last_byte
+ 2;
1788 for (col
= 0, p
= current
; p
< q
; p
++)
1802 orig_c
= c
= edit_get_byte (edit
, p
);
1806 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1811 if (g_unichar_iswide (utf_ch
))
1814 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1818 c
= convert_to_display_c (c
);
1820 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1828 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1829 /* '\r' is shown as ^M, so we must advance 2 characters */
1830 /* Caret notation for control characters */
1838 /* returns the current column position of the cursor */
1840 edit_get_col (WEdit
* edit
)
1842 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1846 /* Scrolling functions */
1849 edit_update_curs_row (WEdit
* edit
)
1851 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1855 edit_update_curs_col (WEdit
* edit
)
1857 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1861 edit_get_curs_col (const WEdit
* edit
)
1863 return edit
->curs_col
;
1866 /*moves the display start position up by i lines */
1868 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1870 unsigned long lines_above
= edit
->start_line
;
1871 if (i
> lines_above
)
1875 edit
->start_line
-= i
;
1876 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1877 edit
->force
|= REDRAW_PAGE
;
1878 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1880 edit_update_curs_row (edit
);
1884 /* returns 1 if could scroll, 0 otherwise */
1886 edit_scroll_downward (WEdit
* edit
, int i
)
1889 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1890 if (lines_below
> 0)
1892 if (i
> lines_below
)
1894 edit
->start_line
+= i
;
1895 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1896 edit
->force
|= REDRAW_PAGE
;
1897 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1899 edit_update_curs_row (edit
);
1903 edit_scroll_right (WEdit
* edit
, int i
)
1905 edit
->force
|= REDRAW_PAGE
;
1906 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1907 edit
->start_col
-= i
;
1911 edit_scroll_left (WEdit
* edit
, int i
)
1913 if (edit
->start_col
)
1915 edit
->start_col
+= i
;
1916 if (edit
->start_col
> 0)
1917 edit
->start_col
= 0;
1918 edit
->force
|= REDRAW_PAGE
;
1919 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1923 /* high level cursor movement commands */
1926 is_in_indent (WEdit
* edit
)
1928 long p
= edit_bol (edit
, edit
->curs1
);
1929 while (p
< edit
->curs1
)
1930 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1935 static int left_of_four_spaces (WEdit
* edit
);
1938 edit_move_to_prev_col (WEdit
* edit
, long p
)
1940 int prev
= edit
->prev_col
;
1941 int over
= edit
->over_col
;
1942 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1944 if (option_cursor_beyond_eol
)
1946 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
1947 edit_eol (edit
, edit
->curs1
));
1949 if (line_len
< prev
+ edit
->over_col
)
1951 edit
->over_col
= prev
+ over
- line_len
;
1952 edit
->prev_col
= line_len
;
1953 edit
->curs_col
= line_len
;
1957 edit
->curs_col
= prev
+ over
;
1958 edit
->prev_col
= edit
->curs_col
;
1965 if (is_in_indent (edit
) && option_fake_half_tabs
)
1967 edit_update_curs_col (edit
);
1969 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
1971 int q
= edit
->curs_col
;
1972 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1973 p
= edit_bol (edit
, edit
->curs1
);
1974 edit_cursor_move (edit
,
1975 edit_move_forward3 (edit
, p
, edit
->curs_col
,
1977 if (!left_of_four_spaces (edit
))
1978 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1985 is_blank (WEdit
* edit
, long offset
)
1989 s
= edit_bol (edit
, offset
);
1990 f
= edit_eol (edit
, offset
) - 1;
1993 c
= edit_get_byte (edit
, s
++);
2001 /* returns the offset of line i */
2003 edit_find_line (WEdit
* edit
, int line
)
2007 if (!edit
->caches_valid
)
2009 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2010 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
2011 /* three offsets that we *know* are line 0 at 0 and these two: */
2012 edit
->line_numbers
[1] = edit
->curs_line
;
2013 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
2014 edit
->line_numbers
[2] = edit
->total_lines
;
2015 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
2016 edit
->caches_valid
= 1;
2018 if (line
>= edit
->total_lines
)
2019 return edit
->line_offsets
[2];
2022 /* find the closest known point */
2023 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2026 n
= abs (edit
->line_numbers
[i
] - line
);
2034 return edit
->line_offsets
[j
]; /* know the offset exactly */
2035 if (m
== 1 && j
>= 3)
2036 i
= j
; /* one line different - caller might be looping, so stay in this cache */
2038 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
2039 if (line
> edit
->line_numbers
[j
])
2040 edit
->line_offsets
[i
] =
2041 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
2043 edit
->line_offsets
[i
] =
2044 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
2045 edit
->line_numbers
[i
] = line
;
2046 return edit
->line_offsets
[i
];
2050 line_is_blank (WEdit
* edit
, long line
)
2052 return is_blank (edit
, edit_find_line (edit
, line
));
2055 /* moves up until a blank line is reached, or until just
2056 before a non-blank line is reached */
2058 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
2061 if (edit
->curs_line
> 1)
2063 if (line_is_blank (edit
, edit
->curs_line
))
2065 if (line_is_blank (edit
, edit
->curs_line
- 1))
2067 for (i
= edit
->curs_line
- 1; i
; i
--)
2068 if (!line_is_blank (edit
, i
))
2076 for (i
= edit
->curs_line
- 1; i
; i
--)
2077 if (line_is_blank (edit
, i
))
2083 for (i
= edit
->curs_line
- 1; i
; i
--)
2084 if (line_is_blank (edit
, i
))
2088 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
2091 /* moves down until a blank line is reached, or until just
2092 before a non-blank line is reached */
2094 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
2097 if (edit
->curs_line
>= edit
->total_lines
- 1)
2099 i
= edit
->total_lines
;
2103 if (line_is_blank (edit
, edit
->curs_line
))
2105 if (line_is_blank (edit
, edit
->curs_line
+ 1))
2107 for (i
= edit
->curs_line
+ 1; i
; i
++)
2108 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
2116 for (i
= edit
->curs_line
+ 1; i
; i
++)
2117 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2123 for (i
= edit
->curs_line
+ 1; i
; i
++)
2124 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2128 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
2132 edit_begin_page (WEdit
* edit
)
2134 edit_update_curs_row (edit
);
2135 edit_move_up (edit
, edit
->curs_row
, 0);
2139 edit_end_page (WEdit
* edit
)
2141 edit_update_curs_row (edit
);
2142 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
2146 /* goto beginning of text */
2148 edit_move_to_top (WEdit
* edit
)
2150 if (edit
->curs_line
)
2152 edit_cursor_move (edit
, -edit
->curs1
);
2153 edit_move_to_prev_col (edit
, 0);
2154 edit
->force
|= REDRAW_PAGE
;
2155 edit
->search_start
= 0;
2156 edit_update_curs_row (edit
);
2161 /* goto end of text */
2163 edit_move_to_bottom (WEdit
* edit
)
2165 if (edit
->curs_line
< edit
->total_lines
)
2167 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
2168 edit
->start_display
= edit
->last_byte
;
2169 edit
->start_line
= edit
->total_lines
;
2170 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
2171 edit
->force
|= REDRAW_PAGE
;
2175 /* goto beginning of line */
2177 edit_cursor_to_bol (WEdit
* edit
)
2179 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
2180 edit
->search_start
= edit
->curs1
;
2181 edit
->prev_col
= edit_get_col (edit
);
2185 /* goto end of line */
2187 edit_cursor_to_eol (WEdit
* edit
)
2189 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
2190 edit
->search_start
= edit
->curs1
;
2191 edit
->prev_col
= edit_get_col (edit
);
2195 /* move cursor to line 'line' */
2197 edit_move_to_line (WEdit
* e
, long line
)
2199 if (line
< e
->curs_line
)
2200 edit_move_up (e
, e
->curs_line
- line
, 0);
2202 edit_move_down (e
, line
- e
->curs_line
, 0);
2203 edit_scroll_screen_over_cursor (e
);
2206 /* scroll window so that first visible line is 'line' */
2208 edit_move_display (WEdit
* e
, long line
)
2210 if (line
< e
->start_line
)
2211 edit_scroll_upward (e
, e
->start_line
- line
);
2213 edit_scroll_downward (e
, line
- e
->start_line
);
2216 /* save markers onto undo stack */
2218 edit_push_markers (WEdit
* edit
)
2220 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2221 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2225 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2234 /* highlight marker toggle */
2236 edit_mark_cmd (WEdit
* edit
, int unmark
)
2238 edit_push_markers (edit
);
2241 edit_set_markers (edit
, 0, 0, 0, 0);
2242 edit
->force
|= REDRAW_PAGE
;
2246 if (edit
->mark2
>= 0)
2248 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
2249 edit
->curs_col
+ edit
->over_col
);
2250 edit
->force
|= REDRAW_PAGE
;
2253 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
2254 edit
->curs_col
+ edit
->over_col
);
2258 static unsigned long
2263 const char option_chars_move_whole_word
[] =
2264 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2270 if (*option_chars_move_whole_word
== '!')
2272 return 0x80000000UL
;
2274 if (g_ascii_isupper ((gchar
) c
))
2276 else if (g_ascii_islower ((gchar
) c
))
2278 else if (g_ascii_isalpha (c
))
2280 else if (isdigit (c
))
2282 else if (isspace (c
))
2284 q
= strchr (option_chars_move_whole_word
, c
);
2286 return 0xFFFFFFFFUL
;
2289 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2294 while ((q
= strchr (q
+ 1, c
)));
2299 edit_left_word_move (WEdit
* edit
, int s
)
2304 if (column_highlighting
2305 && edit
->mark1
!= edit
->mark2
2306 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2308 edit_cursor_move (edit
, -1);
2311 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2312 c2
= edit_get_byte (edit
, edit
->curs1
);
2313 if (!(my_type_of (c1
) & my_type_of (c2
)))
2315 if (isspace (c1
) && !isspace (c2
))
2318 if (!isspace (c1
) && isspace (c2
))
2324 edit_left_word_move_cmd (WEdit
* edit
)
2326 edit_left_word_move (edit
, 0);
2327 edit
->force
|= REDRAW_PAGE
;
2331 edit_right_word_move (WEdit
* edit
, int s
)
2336 if (column_highlighting
2337 && edit
->mark1
!= edit
->mark2
2338 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
2340 edit_cursor_move (edit
, 1);
2341 if (edit
->curs1
>= edit
->last_byte
)
2343 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2344 c2
= edit_get_byte (edit
, edit
->curs1
);
2345 if (!(my_type_of (c1
) & my_type_of (c2
)))
2347 if (isspace (c1
) && !isspace (c2
))
2350 if (!isspace (c1
) && isspace (c2
))
2356 edit_right_word_move_cmd (WEdit
* edit
)
2358 edit_right_word_move (edit
, 0);
2359 edit
->force
|= REDRAW_PAGE
;
2363 edit_right_char_move_cmd (WEdit
* edit
)
2369 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2375 c
= edit_get_byte (edit
, edit
->curs1
);
2377 if (option_cursor_beyond_eol
&& c
== '\n')
2383 edit_cursor_move (edit
, cw
);
2388 edit_left_char_move_cmd (WEdit
* edit
)
2391 if (column_highlighting
2392 && option_cursor_beyond_eol
2393 && edit
->mark1
!= edit
->mark2
2394 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2398 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2402 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2408 edit_cursor_move (edit
, -cw
);
2412 /** Up or down cursor moving.
2413 direction = TRUE - move up
2417 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2420 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
2429 edit
->force
|= REDRAW_PAGE
;
2433 edit_scroll_upward (edit
, i
);
2435 edit_scroll_downward (edit
, i
);
2437 p
= edit_bol (edit
, edit
->curs1
);
2439 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
2441 edit_cursor_move (edit
, p
- edit
->curs1
);
2443 edit_move_to_prev_col (edit
, p
);
2445 /* search start of current multibyte char (like CJK) */
2446 if (edit
->curs1
+ 1 < edit
->last_byte
)
2448 edit_right_char_move_cmd (edit
);
2449 edit_left_char_move_cmd (edit
);
2452 edit
->search_start
= edit
->curs1
;
2453 edit
->found_len
= 0;
2457 edit_right_delete_word (WEdit
* edit
)
2462 if (edit
->curs1
>= edit
->last_byte
)
2464 c1
= edit_delete (edit
, 1);
2465 c2
= edit_get_byte (edit
, edit
->curs1
);
2466 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2468 if (!(my_type_of (c1
) & my_type_of (c2
)))
2474 edit_left_delete_word (WEdit
* edit
)
2479 if (edit
->curs1
<= 0)
2481 c1
= edit_backspace (edit
, 1);
2482 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2483 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2485 if (!(my_type_of (c1
) & my_type_of (c2
)))
2491 the start column position is not recorded, and hence does not
2492 undo as it happed. But who would notice.
2495 edit_do_undo (WEdit
* edit
)
2500 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2502 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
2509 edit_cursor_move (edit
, 1);
2512 edit_cursor_move (edit
, -1);
2515 edit_backspace (edit
, 1);
2518 edit_delete (edit
, 1);
2521 column_highlighting
= 1;
2524 column_highlighting
= 0;
2527 if (ac
>= 256 && ac
< 512)
2528 edit_insert_ahead (edit
, ac
- 256);
2529 if (ac
>= 0 && ac
< 256)
2530 edit_insert (edit
, ac
);
2532 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
2534 edit
->mark1
= ac
- MARK_1
;
2535 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2537 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
2539 edit
->mark2
= ac
- MARK_2
;
2540 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2543 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2546 if (edit
->start_display
> ac
- KEY_PRESS
)
2548 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2549 edit
->force
|= REDRAW_PAGE
;
2551 else if (edit
->start_display
< ac
- KEY_PRESS
)
2553 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2554 edit
->force
|= REDRAW_PAGE
;
2556 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2557 edit_update_curs_row (edit
);
2560 edit
->stack_disable
= 0;
2564 edit_delete_to_line_end (WEdit
* edit
)
2566 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2570 edit_delete (edit
, 1);
2575 edit_delete_to_line_begin (WEdit
* edit
)
2577 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2581 edit_backspace (edit
, 1);
2586 edit_delete_line (WEdit
* edit
)
2589 * Delete right part of the line.
2590 * Note that edit_get_byte() returns '\n' when byte position is
2593 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2595 (void) edit_delete (edit
, 1);
2600 * Note that edit_delete() will not corrupt anything if called while
2601 * cursor position is EOF.
2603 (void) edit_delete (edit
, 1);
2606 * Delete left part of the line.
2607 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2609 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2611 (void) edit_backspace (edit
, 1);
2616 insert_spaces_tab (WEdit
* edit
, int half
)
2619 edit_update_curs_col (edit
);
2620 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
2621 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2624 edit_insert (edit
, ' ');
2630 is_aligned_on_a_tab (WEdit
* edit
)
2632 edit_update_curs_col (edit
);
2633 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2634 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2638 right_of_four_spaces (WEdit
* edit
)
2641 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2642 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2644 return is_aligned_on_a_tab (edit
);
2649 left_of_four_spaces (WEdit
* edit
)
2652 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2653 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2655 return is_aligned_on_a_tab (edit
);
2660 edit_indent_width (WEdit
* edit
, long p
)
2663 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2665 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2669 edit_insert_indent (WEdit
* edit
, int indent
)
2671 if (!option_fill_tabs_with_spaces
)
2673 while (indent
>= TAB_SIZE
)
2675 edit_insert (edit
, '\t');
2679 while (indent
-- > 0)
2680 edit_insert (edit
, ' ');
2684 edit_auto_indent (WEdit
* edit
)
2689 /* use the previous line as a template */
2690 p
= edit_move_backward (edit
, p
, 1);
2691 /* copy the leading whitespace of the line */
2693 { /* no range check - the line _is_ \n-terminated */
2694 c
= edit_get_byte (edit
, p
++);
2695 if (c
!= ' ' && c
!= '\t')
2697 edit_insert (edit
, c
);
2702 edit_double_newline (WEdit
* edit
)
2704 edit_insert (edit
, '\n');
2705 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2707 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2709 edit
->force
|= REDRAW_PAGE
;
2710 edit_insert (edit
, '\n');
2714 edit_tab_cmd (WEdit
* edit
)
2718 if (option_fake_half_tabs
)
2720 if (is_in_indent (edit
))
2722 /*insert a half tab (usually four spaces) unless there is a
2723 half tab already behind, then delete it and insert a
2725 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
2727 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2728 edit_backspace (edit
, 1);
2729 edit_insert (edit
, '\t');
2733 insert_spaces_tab (edit
, 1);
2738 if (option_fill_tabs_with_spaces
)
2740 insert_spaces_tab (edit
, 0);
2744 edit_insert (edit
, '\t');
2749 check_and_wrap_line (WEdit
* edit
)
2752 if (!option_typewriter_wrap
)
2754 edit_update_curs_col (edit
);
2755 if (edit
->curs_col
< option_word_wrap_line_length
)
2761 c
= edit_get_byte (edit
, curs
);
2762 if (c
== '\n' || curs
<= 0)
2764 edit_insert (edit
, '\n');
2767 if (c
== ' ' || c
== '\t')
2769 int current
= edit
->curs1
;
2770 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2771 edit_insert (edit
, '\n');
2772 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2778 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
2781 edit_push_key_press (WEdit
* edit
)
2783 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2784 if (edit
->mark2
== -1)
2785 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2788 /* this find the matching bracket in either direction, and sets edit->bracket */
2790 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2792 const char *const b
= "{}{[][()(", *p
;
2793 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2794 unsigned long j
= 0;
2796 edit_update_curs_row (edit
);
2797 c
= edit_get_byte (edit
, edit
->curs1
);
2800 if (!furthest_bracket_search
)
2801 furthest_bracket_search
--;
2802 /* not on a bracket at all */
2805 /* the matching bracket */
2807 /* going left or right? */
2808 if (strchr ("{[(", c
))
2810 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
2812 /* out of buffer? */
2813 if (q
>= edit
->last_byte
|| q
< 0)
2815 a
= edit_get_byte (edit
, q
);
2816 /* don't want to eat CPU */
2817 if (j
++ > furthest_bracket_search
)
2819 /* out of screen? */
2822 if (q
< edit
->start_display
)
2824 /* count lines if searching downward */
2825 if (inc
> 0 && a
== '\n')
2826 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2829 /* count bracket depth */
2830 i
+= (a
== c
) - (a
== d
);
2831 /* return if bracket depth is zero */
2839 static long last_bracket
= -1;
2842 edit_find_bracket (WEdit
* edit
)
2844 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2845 if (last_bracket
!= edit
->bracket
)
2846 edit
->force
|= REDRAW_PAGE
;
2847 last_bracket
= edit
->bracket
;
2851 edit_goto_matching_bracket (WEdit
* edit
)
2855 q
= edit_get_bracket (edit
, 0, 0);
2858 edit
->bracket
= edit
->curs1
;
2859 edit
->force
|= REDRAW_PAGE
;
2860 edit_cursor_move (edit
, q
- edit
->curs1
);
2865 * This executes a command as though the user initiated it through a key
2866 * press. Callback with WIDGET_KEY as a message calls this after
2867 * translating the key press. This function can be used to pass any
2868 * command to the editor. Note that the screen wouldn't update
2869 * automatically. Either of command or char_for_insertion must be
2870 * passed as -1. Commands are executed, and char_for_insertion is
2871 * inserted at the cursor.
2874 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2876 if (command
== CK_Begin_Record_Macro
)
2879 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2882 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
2884 edit
->force
|= REDRAW_COMPLETELY
;
2885 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2889 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
2891 edit
->macro
[edit
->macro_i
].command
= command
;
2892 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2894 /* record the beginning of a set of editing actions initiated by a key press */
2895 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2896 edit_push_key_press (edit
);
2898 edit_execute_cmd (edit
, command
, char_for_insertion
);
2899 if (column_highlighting
)
2900 edit
->force
|= REDRAW_PAGE
;
2903 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
2906 This executes a command at a lower level than macro recording.
2907 It also does not push a key_press onto the undo stack. This means
2908 that if it is called many times, a single undo command will undo
2909 all of them. It also does not check for the Undo command.
2912 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2914 edit
->force
|= REDRAW_LINE
;
2916 /* The next key press will unhighlight the found string, so update
2918 if (edit
->found_len
|| column_highlighting
)
2919 edit
->force
|= REDRAW_PAGE
;
2921 if (command
/ 100 == 6)
2922 { /* a highlight command like shift-arrow */
2923 column_highlighting
= 0;
2924 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
2926 edit_mark_cmd (edit
, 1); /* clear */
2927 edit_mark_cmd (edit
, 0); /* marking on */
2929 edit
->highlight
= 1;
2932 { /* any other command */
2933 if (edit
->highlight
)
2934 edit_mark_cmd (edit
, 0); /* clear */
2935 edit
->highlight
= 0;
2938 /* first check for undo */
2939 if (command
== CK_Undo
)
2941 edit_do_undo (edit
);
2942 edit
->found_len
= 0;
2943 edit
->prev_col
= edit_get_col (edit
);
2944 edit
->search_start
= edit
->curs1
;
2948 /* An ordinary key press */
2949 if (char_for_insertion
>= 0)
2951 /* if non persistent selection and text selected */
2952 if (!option_persistent_selections
)
2954 if (edit
->mark1
!= edit
->mark2
)
2955 edit_block_delete_cmd (edit
);
2957 if (edit
->overwrite
)
2959 /* remove char only one time, after input first byte, multibyte chars */
2960 if ((!utf8_display
|| edit
->charpoint
== 0)
2961 && edit_get_byte (edit
, edit
->curs1
) != '\n')
2962 edit_delete (edit
, 0);
2964 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2965 edit_insert_over (edit
);
2967 if (char_for_insertion
> 255 && utf8_display
== 0)
2969 unsigned char str
[6 + 1];
2971 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
2981 while (str
[i
] != 0 && i
<= 6)
2983 char_for_insertion
= str
[i
];
2984 edit_insert (edit
, char_for_insertion
);
2990 edit_insert (edit
, char_for_insertion
);
2992 if (option_auto_para_formatting
)
2994 format_paragraph (edit
, 0);
2995 edit
->force
|= REDRAW_PAGE
;
2998 check_and_wrap_line (edit
);
2999 edit
->found_len
= 0;
3000 edit
->prev_col
= edit_get_col (edit
);
3001 edit
->search_start
= edit
->curs1
;
3002 edit_find_bracket (edit
);
3010 case CK_Begin_Page_Highlight
:
3011 case CK_End_Page_Highlight
:
3018 if (edit
->mark2
>= 0)
3020 if (!option_persistent_selections
)
3022 if (column_highlighting
)
3023 edit_push_action (edit
, COLUMN_ON
);
3024 column_highlighting
= 0;
3025 edit_mark_cmd (edit
, 1);
3034 case CK_Begin_Page_Highlight
:
3035 case CK_End_Page_Highlight
:
3040 case CK_Word_Left_Highlight
:
3041 case CK_Word_Right_Highlight
:
3042 case CK_Up_Highlight
:
3043 case CK_Down_Highlight
:
3044 case CK_Up_Alt_Highlight
:
3045 case CK_Down_Alt_Highlight
:
3046 if (edit
->mark2
== -1)
3047 break; /*marking is following the cursor: may need to highlight a whole line */
3050 case CK_Left_Highlight
:
3051 case CK_Right_Highlight
:
3052 edit
->force
|= REDRAW_CHAR_ONLY
;
3055 /* basic cursor key commands */
3059 /* if non persistent selection and text selected */
3060 if (!option_persistent_selections
)
3062 if (edit
->mark1
!= edit
->mark2
)
3064 edit_block_delete_cmd (edit
);
3068 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3073 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3075 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3076 edit_backspace (edit
, 1);
3081 if (option_fake_half_tabs
)
3084 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3086 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3087 edit_backspace (edit
, 1);
3092 edit_backspace (edit
, 0);
3095 /* if non persistent selection and text selected */
3096 if (!option_persistent_selections
)
3098 if (edit
->mark1
!= edit
->mark2
)
3100 edit_block_delete_cmd (edit
);
3105 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3106 edit_insert_over (edit
);
3108 if (option_fake_half_tabs
)
3111 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3113 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3114 edit_delete (edit
, 1);
3118 edit_delete (edit
, 0);
3120 case CK_Delete_Word_Left
:
3122 edit_left_delete_word (edit
);
3124 case CK_Delete_Word_Right
:
3125 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3126 edit_insert_over (edit
);
3128 edit_right_delete_word (edit
);
3130 case CK_Delete_Line
:
3131 edit_delete_line (edit
);
3133 case CK_Delete_To_Line_End
:
3134 edit_delete_to_line_end (edit
);
3136 case CK_Delete_To_Line_Begin
:
3137 edit_delete_to_line_begin (edit
);
3141 if (option_auto_para_formatting
)
3143 edit_double_newline (edit
);
3144 if (option_return_does_auto_indent
)
3145 edit_auto_indent (edit
);
3146 format_paragraph (edit
, 0);
3150 edit_insert (edit
, '\n');
3151 if (option_return_does_auto_indent
)
3153 edit_auto_indent (edit
);
3158 edit_insert (edit
, '\n');
3161 case CK_Page_Up_Alt_Highlight
:
3162 column_highlighting
= 1;
3164 case CK_Page_Up_Highlight
:
3165 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3167 case CK_Page_Down_Alt_Highlight
:
3168 column_highlighting
= 1;
3170 case CK_Page_Down_Highlight
:
3171 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3173 case CK_Left_Alt_Highlight
:
3174 column_highlighting
= 1;
3176 case CK_Left_Highlight
:
3177 if (option_fake_half_tabs
)
3179 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3181 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3184 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3185 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3189 edit_left_char_move_cmd (edit
);
3191 case CK_Right_Alt_Highlight
:
3192 column_highlighting
= 1;
3194 case CK_Right_Highlight
:
3195 if (option_fake_half_tabs
)
3197 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3199 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3200 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3204 edit_right_char_move_cmd (edit
);
3207 case CK_Begin_Page_Highlight
:
3208 edit_begin_page (edit
);
3211 case CK_End_Page_Highlight
:
3212 edit_end_page (edit
);
3215 case CK_Word_Left_Highlight
:
3217 edit_left_word_move_cmd (edit
);
3220 case CK_Word_Right_Highlight
:
3222 edit_right_word_move_cmd (edit
);
3224 case CK_Up_Alt_Highlight
:
3225 column_highlighting
= 1;
3227 case CK_Up_Highlight
:
3228 edit_move_up (edit
, 1, 0);
3230 case CK_Down_Alt_Highlight
:
3231 column_highlighting
= 1;
3233 case CK_Down_Highlight
:
3234 edit_move_down (edit
, 1, 0);
3236 case CK_Paragraph_Up_Alt_Highlight
:
3237 column_highlighting
= 1;
3238 case CK_Paragraph_Up
:
3239 case CK_Paragraph_Up_Highlight
:
3240 edit_move_up_paragraph (edit
, 0);
3242 case CK_Paragraph_Down_Alt_Highlight
:
3243 column_highlighting
= 1;
3244 case CK_Paragraph_Down
:
3245 case CK_Paragraph_Down_Highlight
:
3246 edit_move_down_paragraph (edit
, 0);
3248 case CK_Scroll_Up_Alt_Highlight
:
3249 column_highlighting
= 1;
3251 case CK_Scroll_Up_Highlight
:
3252 edit_move_up (edit
, 1, 1);
3254 case CK_Scroll_Down_Alt_Highlight
:
3255 column_highlighting
= 1;
3256 case CK_Scroll_Down
:
3257 case CK_Scroll_Down_Highlight
:
3258 edit_move_down (edit
, 1, 1);
3261 case CK_Home_Highlight
:
3262 edit_cursor_to_bol (edit
);
3265 case CK_End_Highlight
:
3266 edit_cursor_to_eol (edit
);
3269 /* if text marked shift block */
3270 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3272 if (edit
->mark2
< 0)
3273 edit_mark_cmd (edit
, 0);
3274 edit_move_block_to_right (edit
);
3278 if (option_cursor_beyond_eol
)
3279 edit_insert_over (edit
);
3280 edit_tab_cmd (edit
);
3281 if (option_auto_para_formatting
)
3283 format_paragraph (edit
, 0);
3284 edit
->force
|= REDRAW_PAGE
;
3288 check_and_wrap_line (edit
);
3293 case CK_Toggle_Insert
:
3294 edit
->overwrite
= (edit
->overwrite
== 0);
3298 if (edit
->mark2
>= 0)
3300 if (column_highlighting
)
3301 edit_push_action (edit
, COLUMN_ON
);
3302 column_highlighting
= 0;
3304 edit_mark_cmd (edit
, 0);
3306 case CK_Column_Mark
:
3307 if (!column_highlighting
)
3308 edit_push_action (edit
, COLUMN_OFF
);
3309 column_highlighting
= 1;
3310 edit_mark_cmd (edit
, 0);
3313 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3314 edit
->force
|= REDRAW_PAGE
;
3317 if (column_highlighting
)
3318 edit_push_action (edit
, COLUMN_ON
);
3319 column_highlighting
= 0;
3320 edit_mark_cmd (edit
, 1);
3323 case CK_Toggle_Line_State
:
3324 option_line_state
= !option_line_state
;
3325 if (option_line_state
)
3327 option_line_state_width
= LINE_STATE_WIDTH
;
3331 option_line_state_width
= 0;
3333 edit
->force
|= REDRAW_PAGE
;
3336 case CK_Toggle_Show_Margin
:
3337 show_right_margin
= !show_right_margin
;
3338 edit
->force
|= REDRAW_PAGE
;
3341 case CK_Toggle_Bookmark
:
3342 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3343 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3344 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3346 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3348 case CK_Flush_Bookmarks
:
3349 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3350 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3351 edit
->force
|= REDRAW_PAGE
;
3353 case CK_Next_Bookmark
:
3354 if (edit
->book_mark
)
3356 struct _book_mark
*p
;
3357 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3361 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3362 || p
->line
< edit
->start_line
)
3363 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3364 edit_move_to_line (edit
, p
->line
);
3368 case CK_Prev_Bookmark
:
3369 if (edit
->book_mark
)
3371 struct _book_mark
*p
;
3372 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3373 while (p
->line
== edit
->curs_line
)
3378 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3379 || p
->line
< edit
->start_line
)
3380 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3381 edit_move_to_line (edit
, p
->line
);
3386 case CK_Beginning_Of_Text
:
3387 case CK_Beginning_Of_Text_Highlight
:
3388 edit_move_to_top (edit
);
3390 case CK_End_Of_Text
:
3391 case CK_End_Of_Text_Highlight
:
3392 edit_move_to_bottom (edit
);
3396 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3397 edit_insert_over (edit
);
3398 edit_block_copy_cmd (edit
);
3401 edit_block_delete_cmd (edit
);
3404 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3405 edit_insert_over (edit
);
3406 edit_block_move_cmd (edit
);
3409 case CK_Shift_Block_Left
:
3410 if (edit
->mark1
!= edit
->mark2
)
3411 edit_move_block_to_left (edit
);
3413 case CK_Shift_Block_Right
:
3414 if (edit
->mark1
!= edit
->mark2
)
3415 edit_move_block_to_right (edit
);
3418 edit_copy_to_X_buf_cmd (edit
);
3421 edit_cut_to_X_buf_cmd (edit
);
3424 /* if non persistent selection and text selected */
3425 if (!option_persistent_selections
)
3427 if (edit
->mark1
!= edit
->mark2
)
3428 edit_block_delete_cmd (edit
);
3430 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3431 edit_insert_over (edit
);
3432 edit_paste_from_X_buf_cmd (edit
);
3434 case CK_Selection_History
:
3435 edit_paste_from_history (edit
);
3439 edit_save_as_cmd (edit
);
3442 edit_save_confirm_cmd (edit
);
3445 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3448 edit_save_block_cmd (edit
);
3450 case CK_Insert_File
:
3451 edit_insert_file_cmd (edit
);
3454 case CK_Load_Prev_File
:
3455 edit_load_back_cmd (edit
);
3457 case CK_Load_Next_File
:
3458 edit_load_forward_cmd (edit
);
3461 case CK_Load_Syntax_File
:
3462 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3464 case CK_Choose_Syntax
:
3465 edit_syntax_dialog (edit
, edit
->syntax_type
);
3468 case CK_Load_Menu_File
:
3469 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3472 case CK_Toggle_Syntax
:
3473 if ((option_syntax_highlighting
^= 1) == 1)
3474 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3475 edit
->force
|= REDRAW_PAGE
;
3478 case CK_Toggle_Tab_TWS
:
3479 enable_show_tabs_tws
^= 1;
3480 edit
->force
|= REDRAW_PAGE
;
3484 edit_search_cmd (edit
, 0);
3487 edit_search_cmd (edit
, 1);
3490 edit_replace_cmd (edit
, 0);
3492 case CK_Replace_Again
:
3493 edit_replace_cmd (edit
, 1);
3495 case CK_Complete_Word
:
3496 /* if text marked shift block */
3497 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3499 edit_move_block_to_left (edit
);
3503 edit_complete_word_cmd (edit
);
3506 case CK_Find_Definition
:
3507 edit_get_match_keyword_cmd (edit
);
3510 dlg_stop (edit
->widget
.parent
);
3513 edit_new_cmd (edit
);
3516 edit_help_cmd (edit
);
3519 edit_refresh_cmd (edit
);
3521 case CK_SaveSetupCmd
:
3525 query_dialog (_("About"),
3526 _("\n Cooledit v3.11.5\n\n"
3527 " Copyright (C) 1996 the Free Software Foundation\n\n"
3528 " A user friendly text editor written\n"
3529 " for the Midnight Commander.\n"), D_NORMAL
, 1, _("&OK"));
3534 case CK_Edit_Options
:
3535 edit_options_dialog (edit
);
3537 case CK_Edit_Save_Mode
:
3538 menu_save_mode_cmd ();
3543 /* fool gcc to prevent a Y2K warning */
3544 char time_format
[] = "_c";
3545 time_format
[0] = '%';
3547 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3548 edit_print_string (edit
, s
);
3549 edit
->force
|= REDRAW_PAGE
;
3554 edit_goto_cmd (edit
);
3556 case CK_Paragraph_Format
:
3557 format_paragraph (edit
, 1);
3558 edit
->force
|= REDRAW_PAGE
;
3560 case CK_Delete_Macro
:
3561 edit_delete_macro_cmd (edit
);
3563 case CK_Match_Bracket
:
3564 edit_goto_matching_bracket (edit
);
3570 edit_sort_cmd (edit
);
3573 edit_ext_cmd (edit
);
3576 edit_mail_dialog (edit
);
3581 case CK_SelectCodepage
:
3582 edit_select_codepage_cmd (edit
);
3584 case CK_Insert_Literal
:
3585 edit_insert_literal_cmd (edit
);
3587 case CK_Execute_Macro
:
3588 edit_execute_macro_cmd (edit
);
3590 case CK_Begin_End_Macro
:
3591 edit_begin_end_macro_cmd (edit
);
3601 if ((command
/ 1000) == 1) /* a shell command */
3602 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3603 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
3604 { /* a macro command */
3605 struct macro m
[MAX_MACRO_LENGTH
];
3607 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3608 edit_execute_macro (edit
, m
, nm
);
3611 /* keys which must set the col position, and the search vars */
3617 case CK_Replace_Again
:
3618 case CK_Complete_Word
:
3619 edit
->prev_col
= edit_get_col (edit
);
3622 case CK_Up_Highlight
:
3623 case CK_Up_Alt_Highlight
:
3625 case CK_Down_Highlight
:
3626 case CK_Down_Alt_Highlight
:
3628 case CK_Page_Up_Highlight
:
3629 case CK_Page_Up_Alt_Highlight
:
3631 case CK_Page_Down_Highlight
:
3632 case CK_Page_Down_Alt_Highlight
:
3633 case CK_Beginning_Of_Text
:
3634 case CK_Beginning_Of_Text_Highlight
:
3635 case CK_End_Of_Text
:
3636 case CK_End_Of_Text_Highlight
:
3637 case CK_Paragraph_Up
:
3638 case CK_Paragraph_Up_Highlight
:
3639 case CK_Paragraph_Up_Alt_Highlight
:
3640 case CK_Paragraph_Down
:
3641 case CK_Paragraph_Down_Highlight
:
3642 case CK_Paragraph_Down_Alt_Highlight
:
3644 case CK_Scroll_Up_Highlight
:
3645 case CK_Scroll_Up_Alt_Highlight
:
3646 case CK_Scroll_Down
:
3647 case CK_Scroll_Down_Highlight
:
3648 case CK_Scroll_Down_Alt_Highlight
:
3649 edit
->search_start
= edit
->curs1
;
3650 edit
->found_len
= 0;
3653 edit
->found_len
= 0;
3654 edit
->prev_col
= edit_get_col (edit
);
3655 edit
->search_start
= edit
->curs1
;
3657 edit_find_bracket (edit
);
3659 if (option_auto_para_formatting
)
3665 case CK_Delete_Word_Left
:
3666 case CK_Delete_Word_Right
:
3667 case CK_Delete_To_Line_End
:
3668 case CK_Delete_To_Line_Begin
:
3669 format_paragraph (edit
, 0);
3670 edit
->force
|= REDRAW_PAGE
;
3677 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
3681 if (edit
->macro_depth
++ > 256)
3683 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3684 edit
->macro_depth
--;
3687 edit
->force
|= REDRAW_PAGE
;
3690 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3692 edit_update_screen (edit
);
3693 edit
->macro_depth
--;
3696 /* User edit menu, like user menu (F2) but only in editor. */
3698 user_menu (WEdit
* edit
)
3702 long start_mark
, end_mark
;
3705 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3707 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3709 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3711 /* run shell scripts from menu */
3712 user_menu_cmd (edit
);
3714 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
3721 /* i.e. we have marked block */
3722 rc
= edit_block_delete_cmd (edit
);
3726 edit_insert_file (edit
, block_file
);
3728 /* truncate block file */
3729 fd
= fopen (block_file
, "w");
3733 edit_refresh_cmd (edit
);
3734 edit
->force
|= REDRAW_COMPLETELY
;
3736 g_free (block_file
);
3740 edit_stack_init (void)
3742 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3744 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3745 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3748 edit_stack_iterator
= 0;
3752 edit_stack_free (void)
3754 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3755 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3760 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3762 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3767 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3769 edit_move_updown (edit
, i
, do_scroll
, FALSE
);