1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * \brief Source: editor low level data handling and cursor fundamentals
33 #include <sys/types.h>
42 #include "../src/global.h"
44 #include "../src/tty/color.h"
45 #include "../src/tty/tty.h" /* attrset() */
46 #include "../src/tty/key.h" /* is_idle() */
48 #include "../src/skin/skin.h" /* EDITOR_NORMAL_COLOR */
50 #include "../src/widget.h"
51 #include "../src/cmd.h" /* view_other_cmd() */
52 #include "../src/user.h" /* user_menu_cmd() */
53 #include "../src/wtools.h" /* query_dialog() */
54 #include "../src/timefmt.h" /* time formatting */
55 #include "../src/strutil.h" /* utf string functions */
56 #include "../src/charsets.h" /* get_codepage_id */
57 #include "../src/main.h" /* source_codepage */
58 #include "../src/learn.h" /* learn_keys */
59 #include "../src/cmddef.h"
61 #include "edit-impl.h"
63 #include "edit-widget.h"
65 #include "../vfs/vfs.h"
67 int option_word_wrap_line_length
= 72;
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
= 1;
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'};
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
116 * file end|||file beginning
125 const global_keymap_t
*editor_map
;
126 const global_keymap_t
*editor_x_map
;
128 static void user_menu (WEdit
*edit
);
130 int edit_get_byte (WEdit
* edit
, long byte_index
)
133 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
136 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];
140 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
144 char *edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
147 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
150 if (byte_index
>= edit
->curs1
) {
151 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
152 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
]+(EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
154 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
]+(byte_index
& M_EDIT_BUF_SIZE
));
158 char *edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
162 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
168 if (byte_index
>= edit
->curs1
) {
169 p
= edit
->curs1
+ edit
->curs2
- 1;
170 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] + (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
172 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
176 int edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
181 gchar
*next_ch
= NULL
;
184 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
189 str
= edit_get_byte_ptr (edit
, byte_index
);
196 res
= g_utf8_get_char_validated (str
, -1);
203 /* Calculate UTF-8 char width */
204 next_ch
= g_utf8_next_char (str
);
206 width
= next_ch
- str
;
216 int edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
218 gchar
*str
, *buf
= NULL
;
221 gchar
*next_ch
= NULL
;
227 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0) {
232 ch
= edit_get_utf (edit
, byte_index
, &width
);
239 str
= edit_get_byte_ptr (edit
, byte_index
);
240 buf
= edit_get_buf_ptr (edit
, byte_index
);
241 if (str
== NULL
|| buf
== NULL
) {
245 /* get prev utf8 char */
247 str
= g_utf8_find_prev_char (buf
, str
);
249 res
= g_utf8_get_char_validated (str
, -1);
256 /* Calculate UTF-8 char width */
257 next_ch
= g_utf8_next_char(str
);
259 width
= next_ch
- str
;
270 * Initialize the buffers for an empty files.
273 edit_init_buffers (WEdit
*edit
)
277 for (j
= 0; j
<= MAXBUFF
; j
++) {
278 edit
->buffers1
[j
] = NULL
;
279 edit
->buffers2
[j
] = NULL
;
284 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
288 * Load file OR text into buffers. Set cursor to the beginning of file.
292 edit_load_file_fast (WEdit
*edit
, const char *filename
)
296 edit
->curs2
= edit
->last_byte
;
297 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
299 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1) {
300 GString
*errmsg
= g_string_new(NULL
);
301 g_string_sprintf(errmsg
, _(" Cannot open %s for reading "), filename
);
302 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
303 g_string_free (errmsg
, TRUE
);
307 if (!edit
->buffers2
[buf2
])
308 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
311 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
312 (edit
->curs2
& M_EDIT_BUF_SIZE
),
313 edit
->curs2
& M_EDIT_BUF_SIZE
);
315 for (buf
= buf2
- 1; buf
>= 0; buf
--) {
316 /* edit->buffers2[0] is already allocated */
317 if (!edit
->buffers2
[buf
])
318 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
319 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
326 /* detecting an error on save is easy: just check if every byte has been written. */
327 /* detecting an error on read, is not so easy 'cos there is not way to tell
328 whether you read everything or not. */
329 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
330 static const struct edit_filters
{
331 const char *read
, *write
, *extension
;
333 { "xz -cd %s 2>&1", "xz > %s", ".xz" },
334 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
335 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
336 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
337 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
340 /* Return index of the filter or -1 is there is no appropriate filter */
341 static int edit_find_filter (const char *filename
)
346 l
= strlen (filename
);
347 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++) {
348 e
= strlen (all_filters
[i
].extension
);
350 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
357 edit_get_filter (const char *filename
)
360 char *p
, *quoted_name
;
361 i
= edit_find_filter (filename
);
364 quoted_name
= name_quote (filename
, 0);
365 l
= str_term_width1 (quoted_name
);
366 p
= g_malloc0 (str_term_width1 (all_filters
[i
].read
) + l
+ 2);
367 sprintf (p
, all_filters
[i
].read
, quoted_name
);
368 g_free (quoted_name
);
373 edit_get_write_filter (const char *write_name
, const char *filename
)
377 i
= edit_find_filter (filename
);
380 writename
= name_quote (write_name
, 0);
381 l
= str_term_width1 (writename
);
382 p
= g_malloc0 (str_term_width1 (all_filters
[i
].write
) + l
+ 2);
383 sprintf (p
, all_filters
[i
].write
, writename
);
389 edit_insert_stream (WEdit
* edit
, FILE * f
)
393 while ((c
= fgetc (f
)) >= 0) {
394 edit_insert (edit
, c
);
400 long edit_write_stream (WEdit
* edit
, FILE * f
)
404 if (edit
->lb
== LB_ASIS
) {
405 for (i
= 0; i
< edit
->last_byte
; i
++)
406 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
411 /* change line breaks */
412 for (i
= 0; i
< edit
->last_byte
; i
++) {
413 unsigned char c
= edit_get_byte (edit
, i
);
415 if (!(c
== '\n' || c
== '\r')) {
417 if (fputc (c
, f
) < 0)
419 } else { /* (c == '\n' || c == '\r') */
420 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
423 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
424 /* put one line break unconditionally */
425 if (fputc ('\n', f
) < 0)
428 i
++; /* 2 chars are processed */
430 if (c
== '\r' && c1
== '\n')
431 /* Windows line break; go to the next char */
434 if (c
== '\r' && c1
== '\r') {
435 /* two Macintosh line breaks; put second line break */
436 if (fputc ('\n', f
) < 0)
441 if (fputc (c1
, f
) < 0)
445 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
446 /* put one line break unconditionally */
447 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
450 if (c
== '\r' && c1
== '\n')
451 /* Windows line break; go to the next char */
455 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
456 /* put one line break unconditionally */
457 if (fputc ('\r', f
) < 0)
460 i
++; /* 2 chars are processed */
462 if (c
== '\r' && c1
== '\n')
463 /* Windows line break; go to the next char */
466 if (c
== '\n' && c1
== '\n') {
467 /* two Windows line breaks; put second line break */
468 if (fputc ('\r', f
) < 0)
473 if (fputc (c1
, f
) < 0)
476 case LB_ASIS
: /* default without changes */
482 return edit
->last_byte
;
485 #define TEMP_BUF_LEN 1024
487 /* inserts a file at the cursor, returns 1 on success */
489 edit_insert_file (WEdit
*edit
, const char *filename
)
492 if ((p
= edit_get_filter (filename
))) {
494 long current
= edit
->curs1
;
495 f
= (FILE *) popen (p
, "r");
497 edit_insert_stream (edit
, f
);
498 edit_cursor_move (edit
, current
- edit
->curs1
);
499 if (pclose (f
) > 0) {
500 GString
*errmsg
= g_string_new (NULL
);
501 g_string_sprintf (errmsg
, _(" Error reading from pipe: %s "), p
);
502 edit_error_dialog (_("Error"), errmsg
->str
);
503 g_string_free (errmsg
, TRUE
);
508 GString
*errmsg
= g_string_new (NULL
);
509 g_string_sprintf (errmsg
, _(" Cannot open pipe for reading: %s "), p
);
510 edit_error_dialog (_("Error"), errmsg
->str
);
511 g_string_free (errmsg
, TRUE
);
517 int i
, file
, blocklen
;
518 long current
= edit
->curs1
;
519 int vertical_insertion
= 0;
521 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
523 buf
= g_malloc0 (TEMP_BUF_LEN
);
524 blocklen
= mc_read (file
, buf
, sizeof(VERTICAL_MAGIC
));
526 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
527 if ( memcmp(buf
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
)) == 0 ) {
528 vertical_insertion
= 1;
530 mc_lseek (file
, 0, SEEK_SET
);
533 if (vertical_insertion
) {
534 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
536 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0) {
537 for (i
= 0; i
< blocklen
; i
++)
538 edit_insert (edit
, buf
[i
]);
541 edit_cursor_move (edit
, current
- edit
->curs1
);
550 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
552 check_file_access (WEdit
*edit
, const char *filename
, struct stat
*st
)
555 GString
*errmsg
= (GString
*) 0;
557 /* Try opening an existing file */
558 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
562 * Try creating the file. O_EXCL prevents following broken links
563 * and opening existing files.
567 O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
,
570 g_string_sprintf (errmsg
= g_string_new (NULL
),
571 _(" Cannot open %s for reading "), filename
);
574 /* New file, delete it if it's not modified or saved */
575 edit
->delete_file
= 1;
579 /* Check what we have opened */
580 if (mc_fstat (file
, st
) < 0) {
581 g_string_sprintf (errmsg
= g_string_new (NULL
),
582 _(" Cannot get size/permissions for %s "), filename
);
586 /* We want to open regular files only */
587 if (!S_ISREG (st
->st_mode
)) {
588 g_string_sprintf (errmsg
= g_string_new (NULL
),
589 _(" %s is not a regular file "), filename
);
594 * Don't delete non-empty files.
595 * O_EXCL should prevent it, but let's be on the safe side.
597 if (st
->st_size
> 0) {
598 edit
->delete_file
= 0;
601 if (st
->st_size
>= SIZE_LIMIT
) {
602 g_string_sprintf (errmsg
= g_string_new (NULL
),
603 _(" File %s is too large "), filename
);
607 (void) mc_close (file
);
609 edit_error_dialog (_("Error"), errmsg
->str
);
610 g_string_free (errmsg
, TRUE
);
617 * Open the file and load it into the buffers, either directly or using
618 * a filter. Return 0 on success, 1 on error.
620 * Fast loading (edit_load_file_fast) is used when the file size is
621 * known. In this case the data is read into the buffers by blocks.
622 * If the file size is not known, the data is loaded byte by byte in
626 edit_load_file (WEdit
*edit
)
630 /* Cannot do fast load if a filter is used */
631 if (edit_find_filter (edit
->filename
) >= 0)
635 * VFS may report file size incorrectly, and slow load is not a big
636 * deal considering overhead in VFS.
638 if (!vfs_file_is_local (edit
->filename
))
642 * FIXME: line end translation should disable fast loading as well
643 * Consider doing fseek() to the end and ftell() for the real size.
646 if (*edit
->filename
) {
647 /* If we are dealing with a real file, check that it exists */
648 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
651 /* nothing to load */
655 edit_init_buffers (edit
);
658 edit
->last_byte
= edit
->stat1
.st_size
;
659 edit_load_file_fast (edit
, edit
->filename
);
660 /* If fast load was used, the number of lines wasn't calculated */
661 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
664 const char *codepage_id
;
667 if (*edit
->filename
) {
668 edit
->stack_disable
= 1;
669 if (!edit_insert_file (edit
, edit
->filename
)) {
673 edit
->stack_disable
= 0;
677 codepage_id
= get_codepage_id( source_codepage
);
679 edit
->utf8
= str_isutf8 ( codepage_id
);
686 /* Restore saved cursor position in the file */
688 edit_load_position (WEdit
*edit
)
693 if (!edit
->filename
|| !*edit
->filename
)
696 filename
= vfs_canon (edit
->filename
);
697 load_file_position (filename
, &line
, &column
);
700 edit_move_to_line (edit
, line
- 1);
701 edit
->prev_col
= column
;
702 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
703 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
706 /* Save cursor position in the file */
708 edit_save_position (WEdit
*edit
)
712 if (!edit
->filename
|| !*edit
->filename
)
715 filename
= vfs_canon (edit
->filename
);
716 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
);
720 /* Clean the WEdit stricture except the widget part */
722 edit_purge_widget (WEdit
*edit
)
724 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
725 char *start
= (char *) edit
+ sizeof (Widget
);
726 memset (start
, 0, len
);
727 edit
->macro_i
= -1; /* not recording a macro */
731 edit_set_keymap (void)
733 editor_map
= default_editor_keymap
;
734 if (editor_keymap
&& editor_keymap
->len
> 0)
735 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
737 editor_x_map
= default_editor_x_keymap
;
738 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
739 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
743 #define space_width 1
746 * Fill in the edit structure. Return NULL on failure. Pass edit as
747 * NULL to allocate a new structure.
749 * If line is 0, try to restore saved position. Otherwise put the
750 * cursor on that line and show it in the middle of the screen.
753 edit_init (WEdit
*edit
, int lines
, int columns
, const char *filename
,
757 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
758 if ( option_line_state
) {
759 option_line_state_width
= LINE_STATE_WIDTH
;
761 option_line_state_width
= 0;
766 * Expand option_whole_chars_search by national letters using
770 static char option_whole_chars_search_buf
[256];
772 if (option_whole_chars_search_buf
!= option_whole_chars_search
) {
774 size_t len
= str_term_width1 (option_whole_chars_search
);
776 strcpy (option_whole_chars_search_buf
,
777 option_whole_chars_search
);
779 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++) {
780 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
)) {
781 option_whole_chars_search_buf
[len
++] = i
;
785 option_whole_chars_search_buf
[len
] = 0;
786 option_whole_chars_search
= option_whole_chars_search_buf
;
788 #endif /* ENABLE_NLS */
789 edit
= g_malloc0 (sizeof (WEdit
));
793 edit_purge_widget (edit
);
794 edit
->num_widget_lines
= lines
;
796 edit
->num_widget_columns
= columns
;
797 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
798 edit
->stat1
.st_uid
= getuid ();
799 edit
->stat1
.st_gid
= getgid ();
800 edit
->stat1
.st_mtime
= 0;
802 edit
->force
|= REDRAW_PAGE
;
803 edit_set_filename (edit
, filename
);
804 edit
->stack_size
= START_STACK_SIZE
;
805 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
806 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
807 if (edit_load_file (edit
)) {
808 /* edit_load_file already gives an error message */
814 edit
->converter
= str_cnv_from_term
;
817 const char *cp_id
= NULL
;
818 cp_id
= get_codepage_id (source_codepage
>= 0 ?
819 source_codepage
: display_codepage
);
823 conv
= str_crt_conv_from (cp_id
);
824 if (conv
!= INVALID_CONV
) {
825 if (edit
->converter
!= str_cnv_from_term
)
826 str_close_conv (edit
->converter
);
827 edit
->converter
= conv
;
831 edit
->utf8
= str_isutf8 (cp_id
);
835 edit
->loading_done
= 1;
838 edit_load_syntax (edit
, 0, 0);
841 edit_get_syntax_color (edit
, -1, &color
);
844 /* load saved cursor position */
845 if ((line
== 0) && option_save_position
) {
846 edit_load_position (edit
);
850 edit_move_display (edit
, line
- 1);
851 edit_move_to_line (edit
, line
- 1);
859 /* Clear the edit struct, freeing everything in it. Return 1 on success */
861 edit_clean (WEdit
*edit
)
868 /* a stale lock, remove it */
870 edit
->locked
= edit_unlock_file (edit
->filename
);
872 /* save cursor position */
873 if (option_save_position
)
874 edit_save_position (edit
);
876 /* File specified on the mcedit command line and never saved */
877 if (edit
->delete_file
)
878 unlink (edit
->filename
);
880 edit_free_syntax_rules (edit
);
881 book_mark_flush (edit
, -1);
882 for (; j
<= MAXBUFF
; j
++) {
883 g_free (edit
->buffers1
[j
]);
884 g_free (edit
->buffers2
[j
]);
887 g_free (edit
->undo_stack
);
888 g_free (edit
->filename
);
893 mc_search_free(edit
->search
);
896 edit_purge_widget (edit
);
901 /* returns 1 on success */
903 edit_renew (WEdit
* edit
)
905 int lines
= edit
->num_widget_lines
;
906 int columns
= edit
->num_widget_columns
;
909 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
913 * Load a new file into the editor. If it fails, preserve the old file.
914 * To do it, allocate a new widget, initialize it and, if the new file
915 * was loaded, copy the data to the old widget.
916 * Return 1 on success, 0 on failure.
919 edit_reload (WEdit
*edit
, const char *filename
)
922 int lines
= edit
->num_widget_lines
;
923 int columns
= edit
->num_widget_columns
;
925 e
= g_malloc0 (sizeof (WEdit
));
926 e
->widget
= edit
->widget
;
927 if (!edit_init (e
, lines
, columns
, filename
, 0)) {
932 memcpy (edit
, e
, sizeof (WEdit
));
938 * Load a new file into the editor and set line. If it fails, preserve the old file.
939 * To do it, allocate a new widget, initialize it and, if the new file
940 * was loaded, copy the data to the old widget.
941 * Return 1 on success, 0 on failure.
944 edit_reload_line (WEdit
*edit
, const char *filename
, long line
)
947 int lines
= edit
->num_widget_lines
;
948 int columns
= edit
->num_widget_columns
;
950 e
= g_malloc0 (sizeof (WEdit
));
951 e
->widget
= edit
->widget
;
952 if (!edit_init (e
, lines
, columns
, filename
, line
)) {
957 memcpy (edit
, e
, sizeof (WEdit
));
964 Recording stack for undo:
965 The following is an implementation of a compressed stack. Identical
966 pushes are recorded by a negative prefix indicating the number of times the
967 same char was pushed. This saves space for repeated curs-left or curs-right
984 If the stack long int is 0-255 it represents a normal insert (from a backspace),
985 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
986 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
987 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
990 The only way the cursor moves or the buffer is changed is through the routines:
991 insert, backspace, insert_ahead, delete, and cursor_move.
992 These record the reverse undo movements onto the stack each time they are
995 Each key press results in a set of actions (insert; delete ...). So each time
996 a key is pressed the current position of start_display is pushed as
997 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
998 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
999 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1003 void edit_push_action (WEdit
* edit
, long c
,...)
1005 unsigned long sp
= edit
->stack_pointer
;
1009 /* first enlarge the stack if necessary */
1010 if (sp
> edit
->stack_size
- 10) { /* say */
1011 if (option_max_undo
< 256)
1012 option_max_undo
= 256;
1013 if (edit
->stack_size
< (unsigned long) option_max_undo
) {
1014 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1016 edit
->undo_stack
= t
;
1017 edit
->stack_size
<<= 1;
1018 edit
->stack_size_mask
= edit
->stack_size
- 1;
1022 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1023 if (edit
->stack_disable
)
1026 #ifdef FAST_MOVE_CURSOR
1027 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
) {
1029 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1030 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1032 c
= -(va_arg (ap
, int));
1035 #endif /* ! FAST_MOVE_CURSOR */
1036 if (edit
->stack_bottom
!= sp
1037 && spm1
!= edit
->stack_bottom
1038 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
) {
1040 if (edit
->undo_stack
[spm1
] < 0) {
1041 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1043 if (edit
->undo_stack
[spm1
] > -1000000000) {
1044 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1045 edit
->undo_stack
[spm1
]--;
1049 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1050 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1051 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1052 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1053 if (edit
->undo_stack
[spm1
] == -2)
1054 edit
->stack_pointer
= spm1
;
1056 edit
->undo_stack
[spm1
]++;
1061 d
= edit
->undo_stack
[spm1
];
1064 return; /* --> no need to push multiple do-nothings */
1065 edit
->undo_stack
[sp
] = -2;
1068 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1069 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
)
1070 || (c
== CURS_RIGHT
&& d
== CURS_LEFT
)) { /* a left then a right anihilate each other */
1071 edit
->stack_pointer
= spm1
;
1077 edit
->undo_stack
[sp
] = c
;
1080 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1082 /* if the sp wraps round and catches the stack_bottom then erase
1083 * the first set of actions on the stack to make space - by moving
1084 * stack_bottom forward one "key press" */
1085 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1086 if ((unsigned long) c
== edit
->stack_bottom
||
1087 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1089 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1090 } while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
&& edit
->stack_bottom
!= edit
->stack_pointer
);
1092 /*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: */
1093 if (edit
->stack_pointer
!= edit
->stack_bottom
&& edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1094 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1098 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1099 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1102 pop_action (WEdit
* edit
)
1105 unsigned long sp
= edit
->stack_pointer
;
1106 if (sp
== edit
->stack_bottom
) {
1107 return STACK_BOTTOM
;
1109 sp
= (sp
- 1) & edit
->stack_size_mask
;
1110 if ((c
= edit
->undo_stack
[sp
]) >= 0) {
1111 /* edit->undo_stack[sp] = '@'; */
1112 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1115 if (sp
== edit
->stack_bottom
) {
1116 return STACK_BOTTOM
;
1118 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1119 if (edit
->undo_stack
[sp
] == -2) {
1120 /* edit->undo_stack[sp] = '@'; */
1121 edit
->stack_pointer
= sp
;
1123 edit
->undo_stack
[sp
]++;
1128 /* is called whenever a modification is made by one of the four routines below */
1129 static void edit_modification (WEdit
* edit
)
1131 edit
->caches_valid
= 0;
1132 edit
->screen_modified
= 1;
1134 /* raise lock when file modified */
1135 if (!edit
->modified
&& !edit
->delete_file
)
1136 edit
->locked
= edit_lock_file (edit
->filename
);
1141 Basic low level single character buffer alterations and movements at the cursor.
1142 Returns char passed over, inserted or removed.
1146 edit_insert (WEdit
*edit
, int c
)
1148 /* check if file has grown to large */
1149 if (edit
->last_byte
>= SIZE_LIMIT
)
1152 /* first we must update the position of the display window */
1153 if (edit
->curs1
< edit
->start_display
) {
1154 edit
->start_display
++;
1159 /* Mark file as modified, unless the file hasn't been fully loaded */
1160 if (edit
->loading_done
) {
1161 edit_modification (edit
);
1164 /* now we must update some info on the file and check if a redraw is required */
1166 if (edit
->book_mark
)
1167 book_mark_inc (edit
, edit
->curs_line
);
1169 edit
->total_lines
++;
1170 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1173 /* save the reverse command onto the undo stack */
1174 edit_push_action (edit
, BACKSPACE
);
1176 /* update markers */
1177 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1178 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1179 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1181 /* add a new buffer if we've reached the end of the last one */
1182 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1183 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] =
1184 g_malloc0 (EDIT_BUF_SIZE
);
1186 /* perform the insertion */
1187 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->
1188 curs1
& M_EDIT_BUF_SIZE
]
1189 = (unsigned char) c
;
1191 /* update file length */
1194 /* update cursor position */
1199 edit_insert_over (WEdit
* edit
)
1203 for ( i
= 0; i
< edit
->over_col
; i
++ ) {
1204 edit_insert (edit
, ' ');
1209 /* same as edit_insert and move left */
1210 void edit_insert_ahead (WEdit
* edit
, int c
)
1212 if (edit
->last_byte
>= SIZE_LIMIT
)
1214 if (edit
->curs1
< edit
->start_display
) {
1215 edit
->start_display
++;
1219 edit_modification (edit
);
1221 if (edit
->book_mark
)
1222 book_mark_inc (edit
, edit
->curs_line
);
1223 edit
->total_lines
++;
1224 edit
->force
|= REDRAW_AFTER_CURSOR
;
1226 edit_push_action (edit
, DELCHAR
);
1228 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1229 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1230 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1232 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1233 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1234 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1241 int edit_delete (WEdit
* edit
, const int byte_delete
)
1250 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1251 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1252 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1255 /* if byte_delete = 1 then delete only one byte not multibyte char*/
1256 if ( edit
->utf8
&& byte_delete
== 0 ) {
1257 edit_get_utf (edit
, edit
->curs1
, &cw
);
1261 for ( i
= 1; i
<= cw
; i
++ ) {
1262 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1264 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1265 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1266 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1270 edit_push_action (edit
, p
+ 256);
1273 edit_modification (edit
);
1275 if (edit
->book_mark
)
1276 book_mark_dec (edit
, edit
->curs_line
);
1277 edit
->total_lines
--;
1278 edit
->force
|= REDRAW_AFTER_CURSOR
;
1280 if (edit
->curs1
< edit
->start_display
) {
1281 edit
->start_display
--;
1291 edit_backspace (WEdit
* edit
, const int byte_delete
)
1300 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1301 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1302 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1305 if ( edit
->utf8
&& byte_delete
== 0 ) {
1306 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1310 for ( i
= 1; i
<= cw
; i
++ ) {
1311 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] + ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1312 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
)) {
1313 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1314 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1318 edit_push_action (edit
, p
);
1320 edit_modification (edit
);
1322 if (edit
->book_mark
)
1323 book_mark_dec (edit
, edit
->curs_line
);
1325 edit
->total_lines
--;
1326 edit
->force
|= REDRAW_AFTER_CURSOR
;
1329 if (edit
->curs1
< edit
->start_display
) {
1330 edit
->start_display
--;
1338 #ifdef FAST_MOVE_CURSOR
1340 static void memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1343 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
))) {
1345 next
-= (unsigned long) dest
;
1353 edit_move_backward_lots (WEdit
*edit
, long increment
)
1356 unsigned char *p
= NULL
;
1358 if (increment
> edit
->curs1
)
1359 increment
= edit
->curs1
;
1362 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1364 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1367 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1371 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1372 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
,
1377 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1378 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1379 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1380 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1383 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1384 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1385 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1390 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1392 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1394 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1395 g_malloc0 (EDIT_BUF_SIZE
);
1400 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1410 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1412 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
,
1416 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1417 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1420 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1422 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1423 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1428 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1430 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1432 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] =
1433 g_malloc0 (EDIT_BUF_SIZE
);
1438 return edit_get_byte (edit
, edit
->curs1
);
1441 #endif /* ! FAST_MOVE_CURSOR */
1443 /* moves the cursor right or left: increment positive or negative respectively */
1444 void edit_cursor_move (WEdit
* edit
, long increment
)
1446 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1448 #ifdef FAST_MOVE_CURSOR
1449 if (increment
< -256) {
1450 edit
->force
|= REDRAW_PAGE
;
1451 edit_move_backward_lots (edit
, -increment
);
1454 #endif /* ! FAST_MOVE_CURSOR */
1456 if (increment
< 0) {
1457 for (; increment
< 0; increment
++) {
1461 edit_push_action (edit
, CURS_RIGHT
);
1463 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1464 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1465 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1466 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1468 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1469 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
;
1476 edit
->force
|= REDRAW_LINE_BELOW
;
1480 } else if (increment
> 0) {
1481 for (; increment
> 0; increment
--) {
1485 edit_push_action (edit
, CURS_LEFT
);
1487 c
= edit_get_byte (edit
, edit
->curs1
);
1488 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1489 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1490 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1492 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- ((edit
->curs2
- 1) & M_EDIT_BUF_SIZE
) - 1];
1493 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
)) {
1494 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1495 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1500 edit
->force
|= REDRAW_LINE_ABOVE
;
1506 /* These functions return positions relative to lines */
1508 /* returns index of last char on line + 1 */
1509 long edit_eol (WEdit
* edit
, long current
)
1511 if (current
< edit
->last_byte
) {
1513 if (edit_get_byte (edit
, current
) == '\n')
1516 return edit
->last_byte
;
1520 /* returns index of first char on line */
1521 long edit_bol (WEdit
* edit
, long current
)
1525 if (edit_get_byte (edit
, current
- 1) == '\n')
1533 long edit_count_lines (WEdit
* edit
, long current
, long upto
)
1536 if (upto
> edit
->last_byte
)
1537 upto
= edit
->last_byte
;
1540 while (current
< upto
)
1541 if (edit_get_byte (edit
, current
++) == '\n')
1547 /* If lines is zero this returns the count of lines from current to upto. */
1548 /* If upto is zero returns index of lines forward current. */
1549 long edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1552 return edit_count_lines (edit
, current
, upto
);
1558 next
= edit_eol (edit
, current
) + 1;
1559 if (next
> edit
->last_byte
)
1569 /* Returns offset of 'lines' lines up from current */
1570 long edit_move_backward (WEdit
* edit
, long current
, long lines
)
1574 current
= edit_bol (edit
, current
);
1575 while((lines
--) && current
!= 0)
1576 current
= edit_bol (edit
, current
- 1);
1580 /* If cols is zero this returns the count of columns from current to upto. */
1581 /* If upto is zero returns index of cols across from current. */
1583 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1592 q
= edit
->last_byte
+ 2;
1594 for (col
= 0, p
= current
; p
< q
; p
++) {
1606 orig_c
= c
= edit_get_byte (edit
, p
);
1609 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1613 if (g_unichar_iswide (utf_ch
))
1615 } else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1619 c
= convert_to_display_c (c
);
1621 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1622 else if (c
== '\n') {
1627 } else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1628 /* '\r' is shown as ^M, so we must advance 2 characters */
1629 /* Caret notation for control characters */
1637 /* returns the current column position of the cursor */
1638 int edit_get_col (WEdit
* edit
)
1640 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1644 /* Scrolling functions */
1646 void edit_update_curs_row (WEdit
* edit
)
1648 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1651 void edit_update_curs_col (WEdit
* edit
)
1653 edit
->curs_col
= edit_move_forward3(edit
, edit_bol(edit
, edit
->curs1
), 0, edit
->curs1
);
1657 edit_get_curs_col (const WEdit
*edit
)
1659 return edit
->curs_col
;
1662 /*moves the display start position up by i lines */
1663 void edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1665 unsigned long lines_above
= edit
->start_line
;
1666 if (i
> lines_above
)
1669 edit
->start_line
-= i
;
1670 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1671 edit
->force
|= REDRAW_PAGE
;
1672 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1674 edit_update_curs_row (edit
);
1678 /* returns 1 if could scroll, 0 otherwise */
1679 void edit_scroll_downward (WEdit
* edit
, int i
)
1682 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1683 if (lines_below
> 0) {
1684 if (i
> lines_below
)
1686 edit
->start_line
+= i
;
1687 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1688 edit
->force
|= REDRAW_PAGE
;
1689 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1691 edit_update_curs_row (edit
);
1694 void edit_scroll_right (WEdit
* edit
, int i
)
1696 edit
->force
|= REDRAW_PAGE
;
1697 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1698 edit
->start_col
-= i
;
1701 void edit_scroll_left (WEdit
* edit
, int i
)
1703 if (edit
->start_col
) {
1704 edit
->start_col
+= i
;
1705 if (edit
->start_col
> 0)
1706 edit
->start_col
= 0;
1707 edit
->force
|= REDRAW_PAGE
;
1708 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1712 /* high level cursor movement commands */
1714 static int is_in_indent (WEdit
*edit
)
1716 long p
= edit_bol (edit
, edit
->curs1
);
1717 while (p
< edit
->curs1
)
1718 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1723 static int left_of_four_spaces (WEdit
*edit
);
1726 edit_move_to_prev_col (WEdit
* edit
, long p
)
1728 int prev
= edit
->prev_col
;
1729 int over
= edit
->over_col
;
1730 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1732 if (option_cursor_beyond_eol
) {
1733 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit_eol(edit
, edit
->curs1
));
1735 if (line_len
< prev
+ edit
->over_col
) {
1736 edit
->over_col
= prev
+ over
- line_len
;
1737 edit
->prev_col
= line_len
;
1738 edit
->curs_col
= line_len
;
1740 edit
->curs_col
= prev
+ over
;
1741 edit
->prev_col
= edit
->curs_col
;
1746 if (is_in_indent (edit
) && option_fake_half_tabs
) {
1747 edit_update_curs_col (edit
);
1749 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
)) {
1750 int q
= edit
->curs_col
;
1751 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1752 p
= edit_bol (edit
, edit
->curs1
);
1753 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, edit
->curs_col
, 0) - edit
->curs1
);
1754 if (!left_of_four_spaces (edit
))
1755 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1762 is_blank (WEdit
*edit
, long offset
)
1766 s
= edit_bol (edit
, offset
);
1767 f
= edit_eol (edit
, offset
) - 1;
1769 c
= edit_get_byte (edit
, s
++);
1777 /* returns the offset of line i */
1779 edit_find_line (WEdit
*edit
, int line
)
1783 if (!edit
->caches_valid
) {
1784 for (i
= 0; i
< N_LINE_CACHES
; i
++)
1785 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
1786 /* three offsets that we *know* are line 0 at 0 and these two: */
1787 edit
->line_numbers
[1] = edit
->curs_line
;
1788 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
1789 edit
->line_numbers
[2] = edit
->total_lines
;
1790 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
1791 edit
->caches_valid
= 1;
1793 if (line
>= edit
->total_lines
)
1794 return edit
->line_offsets
[2];
1797 /* find the closest known point */
1798 for (i
= 0; i
< N_LINE_CACHES
; i
++) {
1800 n
= abs (edit
->line_numbers
[i
] - line
);
1807 return edit
->line_offsets
[j
]; /* know the offset exactly */
1808 if (m
== 1 && j
>= 3)
1809 i
= j
; /* one line different - caller might be looping, so stay in this cache */
1811 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
1812 if (line
> edit
->line_numbers
[j
])
1813 edit
->line_offsets
[i
] = edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
1815 edit
->line_offsets
[i
] = edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
1816 edit
->line_numbers
[i
] = line
;
1817 return edit
->line_offsets
[i
];
1820 int line_is_blank (WEdit
* edit
, long line
)
1822 return is_blank (edit
, edit_find_line (edit
, line
));
1825 /* moves up until a blank line is reached, or until just
1826 before a non-blank line is reached */
1827 static void edit_move_up_paragraph (WEdit
* edit
, int scroll
)
1830 if (edit
->curs_line
> 1) {
1831 if (line_is_blank (edit
, edit
->curs_line
)) {
1832 if (line_is_blank (edit
, edit
->curs_line
- 1)) {
1833 for (i
= edit
->curs_line
- 1; i
; i
--)
1834 if (!line_is_blank (edit
, i
)) {
1839 for (i
= edit
->curs_line
- 1; i
; i
--)
1840 if (line_is_blank (edit
, i
))
1844 for (i
= edit
->curs_line
- 1; i
; i
--)
1845 if (line_is_blank (edit
, i
))
1849 edit_move_up (edit
, edit
->curs_line
- i
, scroll
);
1852 /* moves down until a blank line is reached, or until just
1853 before a non-blank line is reached */
1854 static void edit_move_down_paragraph (WEdit
* edit
, int scroll
)
1857 if (edit
->curs_line
>= edit
->total_lines
- 1) {
1858 i
= edit
->total_lines
;
1860 if (line_is_blank (edit
, edit
->curs_line
)) {
1861 if (line_is_blank (edit
, edit
->curs_line
+ 1)) {
1862 for (i
= edit
->curs_line
+ 1; i
; i
++)
1863 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
) {
1868 for (i
= edit
->curs_line
+ 1; i
; i
++)
1869 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1873 for (i
= edit
->curs_line
+ 1; i
; i
++)
1874 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
1878 edit_move_down (edit
, i
- edit
->curs_line
, scroll
);
1881 static void edit_begin_page (WEdit
*edit
)
1883 edit_update_curs_row (edit
);
1884 edit_move_up (edit
, edit
->curs_row
, 0);
1887 static void edit_end_page (WEdit
*edit
)
1889 edit_update_curs_row (edit
);
1890 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
1894 /* goto beginning of text */
1895 static void edit_move_to_top (WEdit
* edit
)
1897 if (edit
->curs_line
) {
1898 edit_cursor_move (edit
, -edit
->curs1
);
1899 edit_move_to_prev_col (edit
, 0);
1900 edit
->force
|= REDRAW_PAGE
;
1901 edit
->search_start
= 0;
1902 edit_update_curs_row(edit
);
1907 /* goto end of text */
1908 static void edit_move_to_bottom (WEdit
* edit
)
1910 if (edit
->curs_line
< edit
->total_lines
) {
1911 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
1912 edit
->start_display
= edit
->last_byte
;
1913 edit
->start_line
= edit
->total_lines
;
1914 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
1915 edit
->force
|= REDRAW_PAGE
;
1919 /* goto beginning of line */
1920 static void edit_cursor_to_bol (WEdit
* edit
)
1922 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
1923 edit
->search_start
= edit
->curs1
;
1924 edit
->prev_col
= edit_get_col (edit
);
1928 /* goto end of line */
1929 static void edit_cursor_to_eol (WEdit
* edit
)
1931 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
1932 edit
->search_start
= edit
->curs1
;
1933 edit
->prev_col
= edit_get_col (edit
);
1937 /* move cursor to line 'line' */
1938 void edit_move_to_line (WEdit
* e
, long line
)
1940 if(line
< e
->curs_line
)
1941 edit_move_up (e
, e
->curs_line
- line
, 0);
1943 edit_move_down (e
, line
- e
->curs_line
, 0);
1944 edit_scroll_screen_over_cursor (e
);
1947 /* scroll window so that first visible line is 'line' */
1948 void edit_move_display (WEdit
* e
, long line
)
1950 if(line
< e
->start_line
)
1951 edit_scroll_upward (e
, e
->start_line
- line
);
1953 edit_scroll_downward (e
, line
- e
->start_line
);
1956 /* save markers onto undo stack */
1957 void edit_push_markers (WEdit
* edit
)
1959 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
1960 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
1963 void edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
1972 /* highlight marker toggle */
1973 void edit_mark_cmd (WEdit
* edit
, int unmark
)
1975 edit_push_markers (edit
);
1977 edit_set_markers (edit
, 0, 0, 0, 0);
1978 edit
->force
|= REDRAW_PAGE
;
1980 if (edit
->mark2
>= 0) {
1981 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+edit
->over_col
, edit
->curs_col
+edit
->over_col
);
1982 edit
->force
|= REDRAW_PAGE
;
1984 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
, edit
->curs_col
+edit
->over_col
);
1988 static unsigned long
1993 const char option_chars_move_whole_word
[] =
1994 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
1999 if (*option_chars_move_whole_word
== '!')
2001 return 0x80000000UL
;
2003 if (g_ascii_isupper ((gchar
) c
))
2005 else if (g_ascii_islower ((gchar
) c
))
2007 else if (g_ascii_isalpha (c
))
2009 else if (isdigit (c
))
2011 else if (isspace (c
))
2013 q
= strchr (option_chars_move_whole_word
, c
);
2015 return 0xFFFFFFFFUL
;
2017 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2021 } while ((q
= strchr (q
+ 1, c
)));
2026 edit_left_word_move (WEdit
*edit
, int s
)
2030 if (column_highlighting
2031 && edit
->mark1
!= edit
->mark2
2032 && edit
->over_col
== 0
2033 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2035 edit_cursor_move (edit
, -1);
2038 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2039 c2
= edit_get_byte (edit
, edit
->curs1
);
2040 if (!(my_type_of (c1
) & my_type_of (c2
)))
2042 if (isspace (c1
) && !isspace (c2
))
2045 if (!isspace (c1
) && isspace (c2
))
2050 static void edit_left_word_move_cmd (WEdit
* edit
)
2052 edit_left_word_move (edit
, 0);
2053 edit
->force
|= REDRAW_PAGE
;
2057 edit_right_word_move (WEdit
*edit
, int s
)
2061 if (column_highlighting
2062 && edit
->mark1
!= edit
->mark2
2063 && edit
->over_col
== 0
2064 && edit
->curs1
== edit_eol(edit
, edit
->curs1
)
2067 edit_cursor_move (edit
, 1);
2068 if (edit
->curs1
>= edit
->last_byte
)
2070 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2071 c2
= edit_get_byte (edit
, edit
->curs1
);
2072 if (!(my_type_of (c1
) & my_type_of (c2
)))
2074 if (isspace (c1
) && !isspace (c2
))
2077 if (!isspace (c1
) && isspace (c2
))
2082 static void edit_right_word_move_cmd (WEdit
* edit
)
2084 edit_right_word_move (edit
, 0);
2085 edit
->force
|= REDRAW_PAGE
;
2088 static void edit_right_char_move_cmd (WEdit
* edit
)
2093 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2097 c
= edit_get_byte (edit
, edit
->curs1
);
2099 if (option_cursor_beyond_eol
&& c
== '\n') {
2102 edit_cursor_move (edit
, cw
);
2106 static void edit_left_char_move_cmd (WEdit
* edit
)
2109 if (column_highlighting
2110 && edit
->mark1
!= edit
->mark2
2111 && edit
->over_col
== 0
2112 && edit
->curs1
== edit_bol(edit
, edit
->curs1
))
2115 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2119 if (option_cursor_beyond_eol
&& edit
->over_col
> 0) {
2122 edit_cursor_move (edit
, -cw
);
2126 /** Up or down cursor moving.
2127 direction = TRUE - move up
2131 edit_move_updown (WEdit
* edit
, unsigned long i
, int scroll
, gboolean direction
) {
2133 unsigned long l
= (direction
)
2135 : edit
->total_lines
- edit
->curs_line
;
2144 edit
->force
|= REDRAW_PAGE
;
2147 edit_scroll_upward (edit
, i
);
2149 edit_scroll_downward (edit
, i
);
2151 p
= edit_bol (edit
, edit
->curs1
);
2154 ? edit_move_backward (edit
, p
, i
)
2155 : edit_move_forward (edit
, p
, i
, 0);
2157 edit_cursor_move (edit
, p
- edit
->curs1
);
2159 edit_move_to_prev_col (edit
, p
);
2161 /* search start of current multibyte char (like CJK) */
2162 edit_right_char_move_cmd (edit
);
2163 edit_left_char_move_cmd (edit
);
2165 edit
->search_start
= edit
->curs1
;
2166 edit
->found_len
= 0;
2169 static void edit_right_delete_word (WEdit
* edit
)
2173 if (edit
->curs1
>= edit
->last_byte
)
2175 c1
= edit_delete (edit
, 1);
2176 c2
= edit_get_byte (edit
, edit
->curs1
);
2177 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2179 if (!(my_type_of (c1
) & my_type_of (c2
)))
2184 static void edit_left_delete_word (WEdit
* edit
)
2188 if (edit
->curs1
<= 0)
2190 c1
= edit_backspace (edit
, 1);
2191 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2192 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2194 if (!(my_type_of (c1
) & my_type_of (c2
)))
2200 the start column position is not recorded, and hence does not
2201 undo as it happed. But who would notice.
2204 edit_do_undo (WEdit
* edit
)
2209 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2211 while ((ac
= pop_action (edit
)) < KEY_PRESS
) {
2216 edit_cursor_move (edit
, 1);
2219 edit_cursor_move (edit
, -1);
2222 edit_backspace (edit
, 1);
2225 edit_delete (edit
, 1);
2228 column_highlighting
= 1;
2231 column_highlighting
= 0;
2234 if (ac
>= 256 && ac
< 512)
2235 edit_insert_ahead (edit
, ac
- 256);
2236 if (ac
>= 0 && ac
< 256)
2237 edit_insert (edit
, ac
);
2239 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2) {
2240 edit
->mark1
= ac
- MARK_1
;
2241 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2242 } else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
) {
2243 edit
->mark2
= ac
- MARK_2
;
2244 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2247 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2250 if (edit
->start_display
> ac
- KEY_PRESS
) {
2251 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2252 edit
->force
|= REDRAW_PAGE
;
2253 } else if (edit
->start_display
< ac
- KEY_PRESS
) {
2254 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2255 edit
->force
|= REDRAW_PAGE
;
2257 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2258 edit_update_curs_row (edit
);
2261 edit
->stack_disable
= 0;
2264 static void edit_delete_to_line_end (WEdit
* edit
)
2266 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2269 edit_delete (edit
, 1);
2273 static void edit_delete_to_line_begin (WEdit
* edit
)
2275 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2278 edit_backspace (edit
, 1);
2283 edit_delete_line (WEdit
*edit
)
2286 * Delete right part of the line.
2287 * Note that edit_get_byte() returns '\n' when byte position is
2290 while (edit_get_byte (edit
, edit
->curs1
) != '\n') {
2291 (void) edit_delete (edit
, 1);
2296 * Note that edit_delete() will not corrupt anything if called while
2297 * cursor position is EOF.
2299 (void) edit_delete (edit
, 1);
2302 * Delete left part of the line.
2303 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2305 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n') {
2306 (void) edit_backspace (edit
, 1);
2310 void insert_spaces_tab (WEdit
* edit
, int half
)
2313 edit_update_curs_col (edit
);
2314 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) + 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2316 edit_insert (edit
, ' ');
2321 static int is_aligned_on_a_tab (WEdit
* edit
)
2323 edit_update_curs_col (edit
);
2324 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2325 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2328 static int right_of_four_spaces (WEdit
*edit
)
2331 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2332 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2334 return is_aligned_on_a_tab (edit
);
2338 static int left_of_four_spaces (WEdit
*edit
)
2341 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2342 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2344 return is_aligned_on_a_tab (edit
);
2348 int edit_indent_width (WEdit
* edit
, long p
)
2351 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2353 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2356 void edit_insert_indent (WEdit
* edit
, int indent
)
2358 if (!option_fill_tabs_with_spaces
) {
2359 while (indent
>= TAB_SIZE
) {
2360 edit_insert (edit
, '\t');
2364 while (indent
-- > 0)
2365 edit_insert (edit
, ' ');
2369 edit_auto_indent (WEdit
* edit
)
2374 /* use the previous line as a template */
2375 p
= edit_move_backward (edit
, p
, 1);
2376 /* copy the leading whitespace of the line */
2377 for (;;) { /* no range check - the line _is_ \n-terminated */
2378 c
= edit_get_byte (edit
, p
++);
2379 if (c
!= ' ' && c
!= '\t')
2381 edit_insert (edit
, c
);
2386 edit_double_newline (WEdit
* edit
)
2388 edit_insert (edit
, '\n');
2389 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2391 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2393 edit
->force
|= REDRAW_PAGE
;
2394 edit_insert (edit
, '\n');
2398 edit_tab_cmd (WEdit
* edit
)
2402 if (option_fake_half_tabs
) {
2403 if (is_in_indent (edit
)) {
2404 /*insert a half tab (usually four spaces) unless there is a
2405 half tab already behind, then delete it and insert a
2407 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
)) {
2408 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2409 edit_backspace (edit
, 1);
2410 edit_insert (edit
, '\t');
2412 insert_spaces_tab (edit
, 1);
2417 if (option_fill_tabs_with_spaces
) {
2418 insert_spaces_tab (edit
, 0);
2420 edit_insert (edit
, '\t');
2424 static void check_and_wrap_line (WEdit
* edit
)
2427 if (!option_typewriter_wrap
)
2429 edit_update_curs_col (edit
);
2430 if (edit
->curs_col
< option_word_wrap_line_length
)
2435 c
= edit_get_byte (edit
, curs
);
2436 if (c
== '\n' || curs
<= 0) {
2437 edit_insert (edit
, '\n');
2440 if (c
== ' ' || c
== '\t') {
2441 int current
= edit
->curs1
;
2442 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2443 edit_insert (edit
, '\n');
2444 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2450 static inline void edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
);
2452 void edit_push_key_press (WEdit
* edit
)
2454 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2455 if (edit
->mark2
== -1)
2456 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2459 /* this find the matching bracket in either direction, and sets edit->bracket */
2460 static long edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2462 const char * const b
= "{}{[][()(", *p
;
2463 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2464 unsigned long j
= 0;
2466 edit_update_curs_row (edit
);
2467 c
= edit_get_byte (edit
, edit
->curs1
);
2470 if (!furthest_bracket_search
)
2471 furthest_bracket_search
--;
2472 /* not on a bracket at all */
2475 /* the matching bracket */
2477 /* going left or right? */
2478 if (strchr ("{[(", c
))
2480 for (q
= edit
->curs1
+ inc
;; q
+= inc
) {
2481 /* out of buffer? */
2482 if (q
>= edit
->last_byte
|| q
< 0)
2484 a
= edit_get_byte (edit
, q
);
2485 /* don't want to eat CPU */
2486 if (j
++ > furthest_bracket_search
)
2488 /* out of screen? */
2490 if (q
< edit
->start_display
)
2492 /* count lines if searching downward */
2493 if (inc
> 0 && a
== '\n')
2494 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2497 /* count bracket depth */
2498 i
+= (a
== c
) - (a
== d
);
2499 /* return if bracket depth is zero */
2507 static long last_bracket
= -1;
2509 void edit_find_bracket (WEdit
* edit
)
2511 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2512 if (last_bracket
!= edit
->bracket
)
2513 edit
->force
|= REDRAW_PAGE
;
2514 last_bracket
= edit
->bracket
;
2518 edit_goto_matching_bracket (WEdit
*edit
)
2522 q
= edit_get_bracket (edit
, 0, 0);
2524 edit
->bracket
= edit
->curs1
;
2525 edit
->force
|= REDRAW_PAGE
;
2526 edit_cursor_move (edit
, q
- edit
->curs1
);
2531 * This executes a command as though the user initiated it through a key
2532 * press. Callback with WIDGET_KEY as a message calls this after
2533 * translating the key press. This function can be used to pass any
2534 * command to the editor. Note that the screen wouldn't update
2535 * automatically. Either of command or char_for_insertion must be
2536 * passed as -1. Commands are executed, and char_for_insertion is
2537 * inserted at the cursor.
2540 edit_execute_key_command (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2542 if (command
== CK_Begin_Record_Macro
) {
2544 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2547 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1) {
2548 edit
->force
|= REDRAW_COMPLETELY
;
2549 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2553 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1) {
2554 edit
->macro
[edit
->macro_i
].command
= command
;
2555 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2557 /* record the beginning of a set of editing actions initiated by a key press */
2558 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2559 edit_push_key_press (edit
);
2561 edit_execute_cmd (edit
, command
, char_for_insertion
);
2562 if (column_highlighting
)
2563 edit
->force
|= REDRAW_PAGE
;
2566 static const char * const shell_cmd
[] = SHELL_COMMANDS_i
;
2569 This executes a command at a lower level than macro recording.
2570 It also does not push a key_press onto the undo stack. This means
2571 that if it is called many times, a single undo command will undo
2572 all of them. It also does not check for the Undo command.
2575 edit_execute_cmd (WEdit
*edit
, unsigned long command
, int char_for_insertion
)
2577 edit
->force
|= REDRAW_LINE
;
2579 /* The next key press will unhighlight the found string, so update
2581 if (edit
->found_len
|| column_highlighting
)
2582 edit
->force
|= REDRAW_PAGE
;
2584 if (command
/ 100 == 6) { /* a highlight command like shift-arrow */
2585 column_highlighting
= 0;
2586 if (!edit
->highlight
2587 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
)) {
2588 edit_mark_cmd (edit
, 1); /* clear */
2589 edit_mark_cmd (edit
, 0); /* marking on */
2591 edit
->highlight
= 1;
2592 } else { /* any other command */
2593 if (edit
->highlight
)
2594 edit_mark_cmd (edit
, 0); /* clear */
2595 edit
->highlight
= 0;
2598 /* first check for undo */
2599 if (command
== CK_Undo
) {
2600 edit_do_undo (edit
);
2601 edit
->found_len
= 0;
2602 edit
->prev_col
= edit_get_col (edit
);
2603 edit
->search_start
= edit
->curs1
;
2607 /* An ordinary key press */
2608 if (char_for_insertion
>= 0) {
2609 if (edit
->overwrite
) {
2610 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2611 edit_delete (edit
, 0);
2613 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2614 edit_insert_over (edit
);
2616 if ( char_for_insertion
> 255 && utf8_display
== 0 ) {
2617 unsigned char str
[6 + 1];
2619 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *)str
);
2626 while ( str
[i
] != 0 && i
<=6) {
2627 char_for_insertion
= str
[i
];
2628 edit_insert (edit
, char_for_insertion
);
2633 edit_insert (edit
, char_for_insertion
);
2635 if (option_auto_para_formatting
) {
2636 format_paragraph (edit
, 0);
2637 edit
->force
|= REDRAW_PAGE
;
2639 check_and_wrap_line (edit
);
2640 edit
->found_len
= 0;
2641 edit
->prev_col
= edit_get_col (edit
);
2642 edit
->search_start
= edit
->curs1
;
2643 edit_find_bracket (edit
);
2650 case CK_Begin_Page_Highlight
:
2651 case CK_End_Page_Highlight
:
2658 if ( edit
->mark2
>= 0 ) {
2659 if ( !option_persistent_selections
) {
2660 if (column_highlighting
)
2661 edit_push_action (edit
, COLUMN_ON
);
2662 column_highlighting
= 0;
2663 edit_mark_cmd (edit
, 1);
2671 case CK_Begin_Page_Highlight
:
2672 case CK_End_Page_Highlight
:
2677 case CK_Word_Left_Highlight
:
2678 case CK_Word_Right_Highlight
:
2679 case CK_Up_Highlight
:
2680 case CK_Down_Highlight
:
2681 case CK_Up_Alt_Highlight
:
2682 case CK_Down_Alt_Highlight
:
2683 if (edit
->mark2
== -1)
2684 break; /*marking is following the cursor: may need to highlight a whole line */
2687 case CK_Left_Highlight
:
2688 case CK_Right_Highlight
:
2689 edit
->force
|= REDRAW_CHAR_ONLY
;
2692 /* basic cursor key commands */
2695 /* if non persistent selection and text selected */
2696 if ( !option_persistent_selections
) {
2697 if ( edit
->mark1
!= edit
->mark2
) {
2698 edit_block_delete_cmd (edit
);
2702 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 ) {
2706 if (option_backspace_through_tabs
&& is_in_indent (edit
)) {
2707 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n'
2709 edit_backspace (edit
, 1);
2712 if (option_fake_half_tabs
) {
2714 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2715 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2716 edit_backspace (edit
, 1);
2721 edit_backspace (edit
, 0);
2724 /* if non persistent selection and text selected */
2725 if ( !option_persistent_selections
) {
2726 if ( edit
->mark1
!= edit
->mark2
) {
2727 edit_block_delete_cmd (edit
);
2732 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2733 edit_insert_over (edit
);
2735 if (option_fake_half_tabs
) {
2737 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2738 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2739 edit_delete (edit
, 1);
2743 edit_delete (edit
, 0);
2745 case CK_Delete_Word_Left
:
2747 edit_left_delete_word (edit
);
2749 case CK_Delete_Word_Right
:
2750 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2751 edit_insert_over (edit
);
2753 edit_right_delete_word (edit
);
2755 case CK_Delete_Line
:
2756 edit_delete_line (edit
);
2758 case CK_Delete_To_Line_End
:
2759 edit_delete_to_line_end (edit
);
2761 case CK_Delete_To_Line_Begin
:
2762 edit_delete_to_line_begin (edit
);
2766 if (option_auto_para_formatting
) {
2767 edit_double_newline (edit
);
2768 if (option_return_does_auto_indent
)
2769 edit_auto_indent (edit
);
2770 format_paragraph (edit
, 0);
2772 edit_insert (edit
, '\n');
2773 if (option_return_does_auto_indent
) {
2774 edit_auto_indent (edit
);
2779 edit_insert (edit
, '\n');
2782 case CK_Page_Up_Alt_Highlight
:
2783 column_highlighting
= 1;
2785 case CK_Page_Up_Highlight
:
2786 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
2788 case CK_Page_Down_Alt_Highlight
:
2789 column_highlighting
= 1;
2791 case CK_Page_Down_Highlight
:
2792 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
2794 case CK_Left_Alt_Highlight
:
2795 column_highlighting
= 1;
2797 case CK_Left_Highlight
:
2798 if (option_fake_half_tabs
) {
2799 if (is_in_indent (edit
) && right_of_four_spaces (edit
)) {
2800 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0)
2803 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
2804 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2808 edit_left_char_move_cmd (edit
);
2810 case CK_Right_Alt_Highlight
:
2811 column_highlighting
= 1;
2813 case CK_Right_Highlight
:
2814 if (option_fake_half_tabs
) {
2815 if (is_in_indent (edit
) && left_of_four_spaces (edit
)) {
2816 edit_cursor_move (edit
, HALF_TAB_SIZE
);
2817 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
2821 edit_right_char_move_cmd (edit
);
2824 case CK_Begin_Page_Highlight
:
2825 edit_begin_page (edit
);
2828 case CK_End_Page_Highlight
:
2829 edit_end_page (edit
);
2832 case CK_Word_Left_Highlight
:
2834 edit_left_word_move_cmd (edit
);
2837 case CK_Word_Right_Highlight
:
2839 edit_right_word_move_cmd (edit
);
2841 case CK_Up_Alt_Highlight
:
2842 column_highlighting
= 1;
2844 case CK_Up_Highlight
:
2845 edit_move_up (edit
, 1, 0);
2847 case CK_Down_Alt_Highlight
:
2848 column_highlighting
= 1;
2850 case CK_Down_Highlight
:
2851 edit_move_down (edit
, 1, 0);
2853 case CK_Paragraph_Up_Alt_Highlight
:
2854 column_highlighting
= 1;
2855 case CK_Paragraph_Up
:
2856 case CK_Paragraph_Up_Highlight
:
2857 edit_move_up_paragraph (edit
, 0);
2859 case CK_Paragraph_Down_Alt_Highlight
:
2860 column_highlighting
= 1;
2861 case CK_Paragraph_Down
:
2862 case CK_Paragraph_Down_Highlight
:
2863 edit_move_down_paragraph (edit
, 0);
2865 case CK_Scroll_Up_Alt_Highlight
:
2866 column_highlighting
= 1;
2868 case CK_Scroll_Up_Highlight
:
2869 edit_move_up (edit
, 1, 1);
2871 case CK_Scroll_Down_Alt_Highlight
:
2872 column_highlighting
= 1;
2873 case CK_Scroll_Down
:
2874 case CK_Scroll_Down_Highlight
:
2875 edit_move_down (edit
, 1, 1);
2878 case CK_Home_Highlight
:
2879 edit_cursor_to_bol (edit
);
2882 case CK_End_Highlight
:
2883 edit_cursor_to_eol (edit
);
2886 /* if text marked shift block */
2887 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
2888 if (edit
->mark2
< 0)
2889 edit_mark_cmd (edit
, 0);
2890 edit_move_block_to_right (edit
);
2892 if ( option_cursor_beyond_eol
)
2893 edit_insert_over (edit
);
2894 edit_tab_cmd (edit
);
2895 if (option_auto_para_formatting
) {
2896 format_paragraph (edit
, 0);
2897 edit
->force
|= REDRAW_PAGE
;
2899 check_and_wrap_line (edit
);
2904 case CK_Toggle_Insert
:
2905 edit
->overwrite
= (edit
->overwrite
== 0);
2909 if (edit
->mark2
>= 0) {
2910 if (column_highlighting
)
2911 edit_push_action (edit
, COLUMN_ON
);
2912 column_highlighting
= 0;
2914 edit_mark_cmd (edit
, 0);
2916 case CK_Column_Mark
:
2917 if (!column_highlighting
)
2918 edit_push_action (edit
, COLUMN_OFF
);
2919 column_highlighting
= 1;
2920 edit_mark_cmd (edit
, 0);
2923 if (column_highlighting
)
2924 edit_push_action (edit
, COLUMN_ON
);
2925 column_highlighting
= 0;
2926 edit_mark_cmd (edit
, 1);
2929 case CK_Toggle_Line_State
:
2930 option_line_state
= !option_line_state
;
2931 if ( option_line_state
) {
2932 option_line_state_width
= LINE_STATE_WIDTH
;
2934 option_line_state_width
= 0;
2936 edit
->force
|= REDRAW_PAGE
;
2939 case CK_Toggle_Bookmark
:
2940 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
2941 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
2942 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2944 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
2946 case CK_Flush_Bookmarks
:
2947 book_mark_flush (edit
, BOOK_MARK_COLOR
);
2948 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
2949 edit
->force
|= REDRAW_PAGE
;
2951 case CK_Next_Bookmark
:
2952 if (edit
->book_mark
) {
2953 struct _book_mark
*p
;
2954 p
= (struct _book_mark
*) book_mark_find (edit
,
2958 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2959 || p
->line
< edit
->start_line
)
2960 edit_move_display (edit
,
2962 edit
->num_widget_lines
/ 2);
2963 edit_move_to_line (edit
, p
->line
);
2967 case CK_Prev_Bookmark
:
2968 if (edit
->book_mark
) {
2969 struct _book_mark
*p
;
2970 p
= (struct _book_mark
*) book_mark_find (edit
,
2972 while (p
->line
== edit
->curs_line
)
2976 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
2977 || p
->line
< edit
->start_line
)
2978 edit_move_display (edit
,
2980 edit
->num_widget_lines
/ 2);
2981 edit_move_to_line (edit
, p
->line
);
2986 case CK_Beginning_Of_Text
:
2987 case CK_Beginning_Of_Text_Highlight
:
2988 edit_move_to_top (edit
);
2990 case CK_End_Of_Text
:
2991 case CK_End_Of_Text_Highlight
:
2992 edit_move_to_bottom (edit
);
2996 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
2997 edit_insert_over (edit
);
2998 edit_block_copy_cmd (edit
);
3001 edit_block_delete_cmd (edit
);
3004 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3005 edit_insert_over (edit
);
3006 edit_block_move_cmd (edit
);
3009 case CK_Shift_Block_Left
:
3010 if (edit
->mark1
!= edit
->mark2
)
3011 edit_move_block_to_left (edit
);
3013 case CK_Shift_Block_Right
:
3014 if (edit
->mark1
!= edit
->mark2
)
3015 edit_move_block_to_right (edit
);
3018 edit_copy_to_X_buf_cmd (edit
);
3021 edit_cut_to_X_buf_cmd (edit
);
3024 if ( option_cursor_beyond_eol
&& edit
->over_col
> 0 )
3025 edit_insert_over (edit
);
3026 edit_paste_from_X_buf_cmd (edit
);
3028 case CK_Selection_History
:
3029 edit_paste_from_history (edit
);
3033 edit_save_as_cmd (edit
);
3036 edit_save_confirm_cmd (edit
);
3039 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3042 edit_save_block_cmd (edit
);
3044 case CK_Insert_File
:
3045 edit_insert_file_cmd (edit
);
3048 case CK_Load_Prev_File
:
3049 edit_load_back_cmd (edit
);
3051 case CK_Load_Next_File
:
3052 edit_load_forward_cmd (edit
);
3055 case CK_Load_Syntax_File
:
3056 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3058 case CK_Choose_Syntax
:
3059 edit_syntax_dialog ();
3062 case CK_Load_Menu_File
:
3063 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3066 case CK_Toggle_Syntax
:
3067 if ((option_syntax_highlighting
^= 1) == 1)
3068 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3069 edit
->force
|= REDRAW_PAGE
;
3072 case CK_Toggle_Tab_TWS
:
3073 enable_show_tabs_tws
^= 1;
3074 edit
->force
|= REDRAW_PAGE
;
3078 edit_search_cmd (edit
, 0);
3081 edit_search_cmd (edit
, 1);
3084 edit_replace_cmd (edit
, 0);
3086 case CK_Replace_Again
:
3087 edit_replace_cmd (edit
, 1);
3089 case CK_Complete_Word
:
3090 /* if text marked shift block */
3091 if ( edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
) {
3092 edit_move_block_to_left (edit
);
3094 edit_complete_word_cmd (edit
);
3097 case CK_Find_Definition
:
3098 edit_get_match_keyword_cmd (edit
);
3101 dlg_stop (edit
->widget
.parent
);
3104 edit_new_cmd (edit
);
3107 edit_help_cmd (edit
);
3110 edit_refresh_cmd (edit
);
3112 case CK_SaveSetupCmd
:
3116 query_dialog (_(" About "),
3117 _("\n Cooledit v3.11.5\n\n"
3118 " Copyright (C) 1996 the Free Software Foundation\n\n"
3119 " A user friendly text editor written\n"
3120 " for the Midnight Commander.\n"), D_NORMAL
,
3126 case CK_Edit_Options
:
3127 edit_options_dialog ();
3129 case CK_Edit_Save_Mode
:
3130 menu_save_mode_cmd ();
3135 /* fool gcc to prevent a Y2K warning */
3136 char time_format
[] = "_c";
3137 time_format
[0] = '%';
3139 FMT_LOCALTIME_CURRENT(s
, sizeof(s
), time_format
);
3140 edit_print_string (edit
, s
);
3141 edit
->force
|= REDRAW_PAGE
;
3146 edit_goto_cmd (edit
);
3148 case CK_Paragraph_Format
:
3149 format_paragraph (edit
, 1);
3150 edit
->force
|= REDRAW_PAGE
;
3152 case CK_Delete_Macro
:
3153 edit_delete_macro_cmd (edit
);
3155 case CK_Match_Bracket
:
3156 edit_goto_matching_bracket (edit
);
3162 edit_sort_cmd (edit
);
3165 edit_ext_cmd (edit
);
3168 edit_mail_dialog (edit
);
3173 case CK_SelectCodepage
:
3174 edit_select_codepage_cmd (edit
);
3176 case CK_Insert_Literal
:
3177 edit_insert_literal_cmd (edit
);
3179 case CK_Execute_Macro
:
3180 edit_execute_macro_cmd (edit
);
3182 case CK_Begin_End_Macro
:
3183 edit_begin_end_macro_cmd (edit
);
3193 if ((command
/ 1000) == 1) /* a shell command */
3194 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3195 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
) { /* a macro command */
3196 struct macro m
[MAX_MACRO_LENGTH
];
3198 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3199 edit_execute_macro (edit
, m
, nm
);
3202 /* keys which must set the col position, and the search vars */
3207 case CK_Replace_Again
:
3208 case CK_Complete_Word
:
3209 edit
->prev_col
= edit_get_col (edit
);
3212 case CK_Up_Highlight
:
3213 case CK_Up_Alt_Highlight
:
3215 case CK_Down_Highlight
:
3216 case CK_Down_Alt_Highlight
:
3218 case CK_Page_Up_Highlight
:
3219 case CK_Page_Up_Alt_Highlight
:
3221 case CK_Page_Down_Highlight
:
3222 case CK_Page_Down_Alt_Highlight
:
3223 case CK_Beginning_Of_Text
:
3224 case CK_Beginning_Of_Text_Highlight
:
3225 case CK_End_Of_Text
:
3226 case CK_End_Of_Text_Highlight
:
3227 case CK_Paragraph_Up
:
3228 case CK_Paragraph_Up_Highlight
:
3229 case CK_Paragraph_Up_Alt_Highlight
:
3230 case CK_Paragraph_Down
:
3231 case CK_Paragraph_Down_Highlight
:
3232 case CK_Paragraph_Down_Alt_Highlight
:
3234 case CK_Scroll_Up_Highlight
:
3235 case CK_Scroll_Up_Alt_Highlight
:
3236 case CK_Scroll_Down
:
3237 case CK_Scroll_Down_Highlight
:
3238 case CK_Scroll_Down_Alt_Highlight
:
3239 edit
->search_start
= edit
->curs1
;
3240 edit
->found_len
= 0;
3243 edit
->found_len
= 0;
3244 edit
->prev_col
= edit_get_col (edit
);
3245 edit
->search_start
= edit
->curs1
;
3247 edit_find_bracket (edit
);
3249 if (option_auto_para_formatting
) {
3253 case CK_Delete_Word_Left
:
3254 case CK_Delete_Word_Right
:
3255 case CK_Delete_To_Line_End
:
3256 case CK_Delete_To_Line_Begin
:
3257 format_paragraph (edit
, 0);
3258 edit
->force
|= REDRAW_PAGE
;
3265 edit_execute_macro (WEdit
*edit
, struct macro macro
[], int n
)
3269 if (edit
->macro_depth
++ > 256) {
3270 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3271 edit
->macro_depth
--;
3274 edit
->force
|= REDRAW_PAGE
;
3275 for (; i
< n
; i
++) {
3276 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3278 edit_update_screen (edit
);
3279 edit
->macro_depth
--;
3282 /* User edit menu, like user menu (F2) but only in editor. */
3284 user_menu (WEdit
* edit
)
3288 long start_mark
, end_mark
;
3291 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3293 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3295 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3297 /* run shell scripts from menu */
3298 user_menu_cmd (edit
);
3300 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0)) {
3305 /* i.e. we have marked block */
3306 rc
= edit_block_delete_cmd (edit
);
3310 edit_insert_file (edit
, block_file
);
3312 /* truncate block file */
3313 fd
= fopen (block_file
, "w");
3317 edit_refresh_cmd (edit
);
3318 edit
->force
|= REDRAW_COMPLETELY
;
3320 g_free (block_file
);
3324 edit_stack_init (void)
3326 for (edit_stack_iterator
= 0;
3327 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3328 edit_stack_iterator
++ ) {
3329 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3330 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3333 edit_stack_iterator
= 0;
3337 edit_stack_free (void)
3339 for (edit_stack_iterator
= 0;
3340 edit_stack_iterator
< MAX_HISTORY_MOVETO
;
3341 edit_stack_iterator
++)
3342 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3346 void edit_move_up (WEdit
* edit
, unsigned long i
, int scroll
)
3348 edit_move_updown (edit
, i
, scroll
, TRUE
);
3352 void edit_move_down (WEdit
* edit
, unsigned long i
, int scroll
)
3354 edit_move_updown (edit
, i
, scroll
, FALSE
);