1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * \brief Source: editor low level data handling and cursor fundamentals
33 #include <sys/types.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
51 #include "src/widget.h"
52 #include "src/cmd.h" /* view_other_cmd() */
53 #include "src/user.h" /* user_menu_cmd() */
54 #include "src/wtools.h" /* query_dialog() */
55 #include "lib/timefmt.h" /* time formatting */
56 #include "src/charsets.h" /* get_codepage_id */
57 #include "src/main.h" /* source_codepage */
58 #include "src/learn.h" /* learn_keys */
59 #include "src/cmddef.h"
61 #include "edit-impl.h"
63 #include "edit-widget.h"
66 int option_word_wrap_line_length
= 72;
67 int option_typewriter_wrap
= 0;
68 int option_auto_para_formatting
= 0;
69 int option_fill_tabs_with_spaces
= 0;
70 int option_return_does_auto_indent
= 1;
71 int option_backspace_through_tabs
= 0;
72 int option_fake_half_tabs
= 1;
73 int option_save_mode
= EDIT_QUICK_SAVE
;
74 int option_save_position
= 1;
75 int option_max_undo
= 32768;
76 int option_persistent_selections
= 1;
77 int option_cursor_beyond_eol
= 0;
78 int option_line_state
= 0;
79 int option_line_state_width
= 0;
81 int option_edit_right_extreme
= 0;
82 int option_edit_left_extreme
= 0;
83 int option_edit_top_extreme
= 0;
84 int option_edit_bottom_extreme
= 0;
85 int enable_show_tabs_tws
= 1;
86 int option_check_nl_at_eof
= 0;
87 int show_right_margin
= 0;
89 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
90 char *option_backup_ext
= NULL
;
92 int edit_stack_iterator
= 0;
93 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
94 /* magic sequense for say than block is vertical */
95 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
99 * here's a quick sketch of the layout: (don't run this through indent.)
101 * (b1 is buffers1 and b2 is buffers2)
104 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
105 * ______________________________________|______________________________________
107 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
108 * |-> |-> |-> |-> |-> |-> |
110 * _<------------------------->|<----------------->_
111 * WEdit->curs2 | WEdit->curs1
116 * file end|||file beginning
125 const global_keymap_t
*editor_map
;
126 const global_keymap_t
*editor_x_map
;
128 static void user_menu (WEdit
* edit
);
131 edit_get_byte (WEdit
* edit
, long byte_index
)
134 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
137 if (byte_index
>= edit
->curs1
)
139 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
140 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
144 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
149 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
152 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
155 if (byte_index
>= edit
->curs1
)
157 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
158 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
159 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
163 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
164 (byte_index
& M_EDIT_BUF_SIZE
));
169 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
173 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
179 if (byte_index
>= edit
->curs1
)
181 p
= edit
->curs1
+ edit
->curs2
- 1;
182 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
183 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
187 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
192 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
197 gchar
*next_ch
= NULL
;
200 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
206 str
= edit_get_byte_ptr (edit
, byte_index
);
214 res
= g_utf8_get_char_validated (str
, -1);
224 /* Calculate UTF-8 char width */
225 next_ch
= g_utf8_next_char (str
);
228 width
= next_ch
- str
;
241 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
243 gchar
*str
, *buf
= NULL
;
246 gchar
*next_ch
= NULL
;
252 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
258 ch
= edit_get_utf (edit
, byte_index
, &width
);
266 str
= edit_get_byte_ptr (edit
, byte_index
);
267 buf
= edit_get_buf_ptr (edit
, byte_index
);
268 if (str
== NULL
|| buf
== NULL
)
273 /* get prev utf8 char */
275 str
= g_utf8_find_prev_char (buf
, str
);
277 res
= g_utf8_get_char_validated (str
, -1);
287 /* Calculate UTF-8 char width */
288 next_ch
= g_utf8_next_char (str
);
291 width
= next_ch
- str
;
304 * Initialize the buffers for an empty files.
307 edit_init_buffers (WEdit
* edit
)
311 for (j
= 0; j
<= MAXBUFF
; j
++)
313 edit
->buffers1
[j
] = NULL
;
314 edit
->buffers2
[j
] = NULL
;
319 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
323 * Load file OR text into buffers. Set cursor to the beginning of file.
327 edit_load_file_fast (WEdit
* edit
, const char *filename
)
331 edit
->curs2
= edit
->last_byte
;
332 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
334 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
336 GString
*errmsg
= g_string_new (NULL
);
337 g_string_sprintf (errmsg
, _(" Cannot open %s for reading "), filename
);
338 edit_error_dialog (_("Error"), get_sys_error (errmsg
->str
));
339 g_string_free (errmsg
, TRUE
);
343 if (!edit
->buffers2
[buf2
])
344 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
347 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
348 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
);
350 for (buf
= buf2
- 1; buf
>= 0; buf
--)
352 /* edit->buffers2[0] is already allocated */
353 if (!edit
->buffers2
[buf
])
354 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
355 mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
);
362 /* detecting an error on save is easy: just check if every byte has been written. */
363 /* detecting an error on read, is not so easy 'cos there is not way to tell
364 whether you read everything or not. */
365 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
366 static const struct edit_filters
368 const char *read
, *write
, *extension
;
372 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
373 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
374 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
375 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
376 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
380 /* Return index of the filter or -1 is there is no appropriate filter */
382 edit_find_filter (const char *filename
)
386 if (filename
== NULL
)
389 l
= strlen (filename
);
390 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
392 e
= strlen (all_filters
[i
].extension
);
394 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
401 edit_get_filter (const char *filename
)
404 char *p
, *quoted_name
;
406 i
= edit_find_filter (filename
);
410 quoted_name
= name_quote (filename
, 0);
411 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
412 g_free (quoted_name
);
417 edit_get_write_filter (const char *write_name
, const char *filename
)
422 i
= edit_find_filter (filename
);
426 writename
= name_quote (write_name
, 0);
427 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
433 edit_insert_stream (WEdit
* edit
, FILE * f
)
437 while ((c
= fgetc (f
)) >= 0)
439 edit_insert (edit
, c
);
446 edit_write_stream (WEdit
* edit
, FILE * f
)
450 if (edit
->lb
== LB_ASIS
)
452 for (i
= 0; i
< edit
->last_byte
; i
++)
453 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
458 /* change line breaks */
459 for (i
= 0; i
< edit
->last_byte
; i
++)
461 unsigned char c
= edit_get_byte (edit
, i
);
463 if (!(c
== '\n' || c
== '\r'))
466 if (fputc (c
, f
) < 0)
470 { /* (c == '\n' || c == '\r') */
471 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
475 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
476 /* put one line break unconditionally */
477 if (fputc ('\n', f
) < 0)
480 i
++; /* 2 chars are processed */
482 if (c
== '\r' && c1
== '\n')
483 /* Windows line break; go to the next char */
486 if (c
== '\r' && c1
== '\r')
488 /* two Macintosh line breaks; put second line break */
489 if (fputc ('\n', f
) < 0)
494 if (fputc (c1
, f
) < 0)
498 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
499 /* put one line break unconditionally */
500 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
503 if (c
== '\r' && c1
== '\n')
504 /* Windows line break; go to the next char */
508 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
509 /* put one line break unconditionally */
510 if (fputc ('\r', f
) < 0)
513 i
++; /* 2 chars are processed */
515 if (c
== '\r' && c1
== '\n')
516 /* Windows line break; go to the next char */
519 if (c
== '\n' && c1
== '\n')
521 /* two Windows line breaks; put second line break */
522 if (fputc ('\r', f
) < 0)
527 if (fputc (c1
, f
) < 0)
530 case LB_ASIS
: /* default without changes */
536 return edit
->last_byte
;
539 #define TEMP_BUF_LEN 1024
541 /* inserts a file at the cursor, returns 1 on success */
543 edit_insert_file (WEdit
* edit
, const char *filename
)
547 p
= edit_get_filter (filename
);
551 long current
= edit
->curs1
;
552 f
= (FILE *) popen (p
, "r");
555 edit_insert_stream (edit
, f
);
556 edit_cursor_move (edit
, current
- edit
->curs1
);
560 errmsg
= g_strdup_printf (_(" Error reading from pipe: %s "), p
);
561 edit_error_dialog (_("Error"), errmsg
);
570 errmsg
= g_strdup_printf (_(" Cannot open pipe for reading: %s "), p
);
571 edit_error_dialog (_("Error"), errmsg
);
580 int i
, file
, blocklen
;
581 long current
= edit
->curs1
;
582 int vertical_insertion
= 0;
584 if ((file
= mc_open (filename
, O_RDONLY
| O_BINARY
)) == -1)
586 buf
= g_malloc0 (TEMP_BUF_LEN
);
587 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
590 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
591 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
593 vertical_insertion
= 1;
597 mc_lseek (file
, 0, SEEK_SET
);
600 if (vertical_insertion
)
602 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
606 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
608 for (i
= 0; i
< blocklen
; i
++)
609 edit_insert (edit
, buf
[i
]);
612 edit_cursor_move (edit
, current
- edit
->curs1
);
621 /* Open file and create it if necessary. Return 0 for success, 1 for error. */
623 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
626 GString
*errmsg
= (GString
*) 0;
628 /* Try opening an existing file */
629 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
634 * Try creating the file. O_EXCL prevents following broken links
635 * and opening existing files.
637 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
640 g_string_sprintf (errmsg
= g_string_new (NULL
),
641 _(" Cannot open %s for reading "), filename
);
646 /* New file, delete it if it's not modified or saved */
647 edit
->delete_file
= 1;
651 /* Check what we have opened */
652 if (mc_fstat (file
, st
) < 0)
654 g_string_sprintf (errmsg
= g_string_new (NULL
),
655 _(" Cannot get size/permissions for %s "), filename
);
659 /* We want to open regular files only */
660 if (!S_ISREG (st
->st_mode
))
662 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" %s is not a regular file "), filename
);
667 * Don't delete non-empty files.
668 * O_EXCL should prevent it, but let's be on the safe side.
672 edit
->delete_file
= 0;
675 if (st
->st_size
>= SIZE_LIMIT
)
677 g_string_sprintf (errmsg
= g_string_new (NULL
), _(" File %s is too large "), filename
);
681 (void) mc_close (file
);
684 edit_error_dialog (_("Error"), errmsg
->str
);
685 g_string_free (errmsg
, TRUE
);
692 * Open the file and load it into the buffers, either directly or using
693 * a filter. Return 0 on success, 1 on error.
695 * Fast loading (edit_load_file_fast) is used when the file size is
696 * known. In this case the data is read into the buffers by blocks.
697 * If the file size is not known, the data is loaded byte by byte in
701 edit_load_file (WEdit
* edit
)
705 /* Cannot do fast load if a filter is used */
706 if (edit_find_filter (edit
->filename
) >= 0)
710 * VFS may report file size incorrectly, and slow load is not a big
711 * deal considering overhead in VFS.
713 if (!vfs_file_is_local (edit
->filename
))
717 * FIXME: line end translation should disable fast loading as well
718 * Consider doing fseek() to the end and ftell() for the real size.
723 /* If we are dealing with a real file, check that it exists */
724 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
729 /* nothing to load */
733 edit_init_buffers (edit
);
737 edit
->last_byte
= edit
->stat1
.st_size
;
738 edit_load_file_fast (edit
, edit
->filename
);
739 /* If fast load was used, the number of lines wasn't calculated */
740 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
745 const char *codepage_id
;
750 edit
->stack_disable
= 1;
751 if (!edit_insert_file (edit
, edit
->filename
))
756 edit
->stack_disable
= 0;
760 codepage_id
= get_codepage_id (source_codepage
);
762 edit
->utf8
= str_isutf8 (codepage_id
);
769 /* Restore saved cursor position in the file */
771 edit_load_position (WEdit
* edit
)
777 if (!edit
->filename
|| !*edit
->filename
)
780 filename
= vfs_canon (edit
->filename
);
781 load_file_position (filename
, &line
, &column
, &offset
);
786 edit_move_to_line (edit
, line
- 1);
787 edit
->prev_col
= column
;
791 edit_cursor_move (edit
, offset
);
792 line
= edit
->curs_line
;
794 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
795 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
798 /* Save cursor position in the file */
800 edit_save_position (WEdit
* edit
)
804 if (!edit
->filename
|| !*edit
->filename
)
807 filename
= vfs_canon (edit
->filename
);
808 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
);
812 /* Clean the WEdit stricture except the widget part */
814 edit_purge_widget (WEdit
* edit
)
816 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
817 char *start
= (char *) edit
+ sizeof (Widget
);
818 memset (start
, 0, len
);
819 edit
->macro_i
= -1; /* not recording a macro */
823 edit_set_keymap (void)
825 editor_map
= default_editor_keymap
;
826 if (editor_keymap
&& editor_keymap
->len
> 0)
827 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
829 editor_x_map
= default_editor_x_keymap
;
830 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
831 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
835 #define space_width 1
838 * Fill in the edit structure. Return NULL on failure. Pass edit as
839 * NULL to allocate a new structure.
841 * If line is 0, try to restore saved position. Otherwise put the
842 * cursor on that line and show it in the middle of the screen.
845 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
848 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
849 if (option_line_state
)
851 option_line_state_width
= LINE_STATE_WIDTH
;
855 option_line_state_width
= 0;
861 * Expand option_whole_chars_search by national letters using
865 static char option_whole_chars_search_buf
[256];
867 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
870 size_t len
= str_term_width1 (option_whole_chars_search
);
872 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
874 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
876 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
878 option_whole_chars_search_buf
[len
++] = i
;
882 option_whole_chars_search_buf
[len
] = 0;
883 option_whole_chars_search
= option_whole_chars_search_buf
;
885 #endif /* ENABLE_NLS */
886 edit
= g_malloc0 (sizeof (WEdit
));
890 edit_purge_widget (edit
);
891 edit
->num_widget_lines
= lines
;
893 edit
->num_widget_columns
= columns
;
894 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
895 edit
->stat1
.st_uid
= getuid ();
896 edit
->stat1
.st_gid
= getgid ();
897 edit
->stat1
.st_mtime
= 0;
899 edit
->force
|= REDRAW_PAGE
;
900 edit_set_filename (edit
, filename
);
901 edit
->stack_size
= START_STACK_SIZE
;
902 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
903 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
904 if (edit_load_file (edit
))
906 /* edit_load_file already gives an error message */
912 edit
->converter
= str_cnv_from_term
;
915 const char *cp_id
= NULL
;
916 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
921 conv
= str_crt_conv_from (cp_id
);
922 if (conv
!= INVALID_CONV
)
924 if (edit
->converter
!= str_cnv_from_term
)
925 str_close_conv (edit
->converter
);
926 edit
->converter
= conv
;
930 edit
->utf8
= str_isutf8 (cp_id
);
934 edit
->loading_done
= 1;
937 edit_load_syntax (edit
, 0, 0);
940 edit_get_syntax_color (edit
, -1, &color
);
943 /* load saved cursor position */
944 if ((line
== 0) && option_save_position
)
946 edit_load_position (edit
);
952 edit_move_display (edit
, line
- 1);
953 edit_move_to_line (edit
, line
- 1);
961 /* Clear the edit struct, freeing everything in it. Return 1 on success */
963 edit_clean (WEdit
* edit
)
970 /* a stale lock, remove it */
972 edit
->locked
= edit_unlock_file (edit
->filename
);
974 /* save cursor position */
975 if (option_save_position
)
976 edit_save_position (edit
);
978 /* File specified on the mcedit command line and never saved */
979 if (edit
->delete_file
)
980 unlink (edit
->filename
);
982 edit_free_syntax_rules (edit
);
983 book_mark_flush (edit
, -1);
984 for (; j
<= MAXBUFF
; j
++)
986 g_free (edit
->buffers1
[j
]);
987 g_free (edit
->buffers2
[j
]);
990 g_free (edit
->undo_stack
);
991 g_free (edit
->filename
);
994 mc_search_free (edit
->search
);
997 if (edit
->converter
!= str_cnv_from_term
)
998 str_close_conv (edit
->converter
);
1000 edit_purge_widget (edit
);
1005 /* returns 1 on success */
1007 edit_renew (WEdit
* edit
)
1009 int lines
= edit
->num_widget_lines
;
1010 int columns
= edit
->num_widget_columns
;
1013 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
1017 * Load a new file into the editor. If it fails, preserve the old file.
1018 * To do it, allocate a new widget, initialize it and, if the new file
1019 * was loaded, copy the data to the old widget.
1020 * Return 1 on success, 0 on failure.
1023 edit_reload (WEdit
* edit
, const char *filename
)
1026 int lines
= edit
->num_widget_lines
;
1027 int columns
= edit
->num_widget_columns
;
1029 e
= g_malloc0 (sizeof (WEdit
));
1030 e
->widget
= edit
->widget
;
1031 if (!edit_init (e
, lines
, columns
, filename
, 0))
1037 memcpy (edit
, e
, sizeof (WEdit
));
1043 * Load a new file into the editor and set line. If it fails, preserve the old file.
1044 * To do it, allocate a new widget, initialize it and, if the new file
1045 * was loaded, copy the data to the old widget.
1046 * Return 1 on success, 0 on failure.
1049 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
1052 int lines
= edit
->num_widget_lines
;
1053 int columns
= edit
->num_widget_columns
;
1055 e
= g_malloc0 (sizeof (WEdit
));
1056 e
->widget
= edit
->widget
;
1057 if (!edit_init (e
, lines
, columns
, filename
, line
))
1063 memcpy (edit
, e
, sizeof (WEdit
));
1070 Recording stack for undo:
1071 The following is an implementation of a compressed stack. Identical
1072 pushes are recorded by a negative prefix indicating the number of times the
1073 same char was pushed. This saves space for repeated curs-left or curs-right
1090 If the stack long int is 0-255 it represents a normal insert (from a backspace),
1091 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
1092 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
1093 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
1096 The only way the cursor moves or the buffer is changed is through the routines:
1097 insert, backspace, insert_ahead, delete, and cursor_move.
1098 These record the reverse undo movements onto the stack each time they are
1101 Each key press results in a set of actions (insert; delete ...). So each time
1102 a key is pressed the current position of start_display is pushed as
1103 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
1104 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
1105 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
1110 edit_push_action (WEdit
* edit
, long c
, ...)
1112 unsigned long sp
= edit
->stack_pointer
;
1116 /* first enlarge the stack if necessary */
1117 if (sp
> edit
->stack_size
- 10)
1119 if (option_max_undo
< 256)
1120 option_max_undo
= 256;
1121 if (edit
->stack_size
< (unsigned long) option_max_undo
)
1123 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
1126 edit
->undo_stack
= t
;
1127 edit
->stack_size
<<= 1;
1128 edit
->stack_size_mask
= edit
->stack_size
- 1;
1132 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1133 if (edit
->stack_disable
)
1136 #ifdef FAST_MOVE_CURSOR
1137 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
1140 edit
->undo_stack
[sp
] = c
== CURS_LEFT_LOTS
? CURS_LEFT
: CURS_RIGHT
;
1141 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1143 c
= -(va_arg (ap
, int));
1147 #endif /* ! FAST_MOVE_CURSOR */
1148 if (edit
->stack_bottom
!= sp
1149 && spm1
!= edit
->stack_bottom
1150 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
1153 if (edit
->undo_stack
[spm1
] < 0)
1155 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
1158 if (edit
->undo_stack
[spm1
] > -1000000000)
1160 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
1161 edit
->undo_stack
[spm1
]--;
1165 /* #define NO_STACK_CURSMOVE_ANIHILATION */
1166 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1167 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1168 { /* a left then a right anihilate each other */
1169 if (edit
->undo_stack
[spm1
] == -2)
1170 edit
->stack_pointer
= spm1
;
1172 edit
->undo_stack
[spm1
]++;
1179 d
= edit
->undo_stack
[spm1
];
1183 return; /* --> no need to push multiple do-nothings */
1184 edit
->undo_stack
[sp
] = -2;
1187 #ifndef NO_STACK_CURSMOVE_ANIHILATION
1188 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
1189 { /* a left then a right anihilate each other */
1190 edit
->stack_pointer
= spm1
;
1196 edit
->undo_stack
[sp
] = c
;
1199 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
1201 /* if the sp wraps round and catches the stack_bottom then erase
1202 * the first set of actions on the stack to make space - by moving
1203 * stack_bottom forward one "key press" */
1204 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
1205 if ((unsigned long) c
== edit
->stack_bottom
||
1206 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
1209 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
1211 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
1212 && edit
->stack_bottom
!= edit
->stack_pointer
);
1214 /*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: */
1215 if (edit
->stack_pointer
!= edit
->stack_bottom
1216 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
1217 edit
->stack_bottom
= edit
->stack_pointer
= 0;
1221 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
1222 then the file should be as it was when he loaded up. Then set edit->modified to 0.
1225 pop_action (WEdit
* edit
)
1228 unsigned long sp
= edit
->stack_pointer
;
1229 if (sp
== edit
->stack_bottom
)
1231 return STACK_BOTTOM
;
1233 sp
= (sp
- 1) & edit
->stack_size_mask
;
1234 if ((c
= edit
->undo_stack
[sp
]) >= 0)
1236 /* edit->undo_stack[sp] = '@'; */
1237 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
1240 if (sp
== edit
->stack_bottom
)
1242 return STACK_BOTTOM
;
1244 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
1245 if (edit
->undo_stack
[sp
] == -2)
1247 /* edit->undo_stack[sp] = '@'; */
1248 edit
->stack_pointer
= sp
;
1251 edit
->undo_stack
[sp
]++;
1256 /* is called whenever a modification is made by one of the four routines below */
1258 edit_modification (WEdit
* edit
)
1260 edit
->caches_valid
= 0;
1261 edit
->screen_modified
= 1;
1263 /* raise lock when file modified */
1264 if (!edit
->modified
&& !edit
->delete_file
)
1265 edit
->locked
= edit_lock_file (edit
->filename
);
1270 Basic low level single character buffer alterations and movements at the cursor.
1271 Returns char passed over, inserted or removed.
1275 edit_insert (WEdit
* edit
, int c
)
1277 /* check if file has grown to large */
1278 if (edit
->last_byte
>= SIZE_LIMIT
)
1281 /* first we must update the position of the display window */
1282 if (edit
->curs1
< edit
->start_display
)
1284 edit
->start_display
++;
1289 /* Mark file as modified, unless the file hasn't been fully loaded */
1290 if (edit
->loading_done
)
1292 edit_modification (edit
);
1295 /* now we must update some info on the file and check if a redraw is required */
1298 if (edit
->book_mark
)
1299 book_mark_inc (edit
, edit
->curs_line
);
1301 edit
->total_lines
++;
1302 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
1305 /* save the reverse command onto the undo stack */
1306 edit_push_action (edit
, BACKSPACE
);
1308 /* update markers */
1309 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
1310 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
1311 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
1313 /* add a new buffer if we've reached the end of the last one */
1314 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1315 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1317 /* perform the insertion */
1318 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
1319 = (unsigned char) c
;
1321 /* update file length */
1324 /* update cursor position */
1329 edit_insert_over (WEdit
* edit
)
1333 for (i
= 0; i
< edit
->over_col
; i
++)
1335 edit_insert (edit
, ' ');
1340 /* same as edit_insert and move left */
1342 edit_insert_ahead (WEdit
* edit
, int c
)
1344 if (edit
->last_byte
>= SIZE_LIMIT
)
1346 if (edit
->curs1
< edit
->start_display
)
1348 edit
->start_display
++;
1352 edit_modification (edit
);
1355 if (edit
->book_mark
)
1356 book_mark_inc (edit
, edit
->curs_line
);
1357 edit
->total_lines
++;
1358 edit
->force
|= REDRAW_AFTER_CURSOR
;
1360 edit_push_action (edit
, DELCHAR
);
1362 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
1363 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
1364 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
1366 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1367 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1368 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) -
1377 edit_delete (WEdit
* edit
, const int byte_delete
)
1386 edit
->mark1
-= (edit
->mark1
> edit
->curs1
);
1387 edit
->mark2
-= (edit
->mark2
> edit
->curs1
);
1388 edit
->last_get_rule
-= (edit
->last_get_rule
> edit
->curs1
);
1391 /* if byte_delete = 1 then delete only one byte not multibyte char */
1392 if (edit
->utf8
&& byte_delete
== 0)
1394 edit_get_utf (edit
, edit
->curs1
, &cw
);
1398 for (i
= 1; i
<= cw
; i
++)
1400 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1402 1) & M_EDIT_BUF_SIZE
) - 1];
1404 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1406 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1407 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
1411 edit_push_action (edit
, p
+ 256);
1414 edit_modification (edit
);
1417 if (edit
->book_mark
)
1418 book_mark_dec (edit
, edit
->curs_line
);
1419 edit
->total_lines
--;
1420 edit
->force
|= REDRAW_AFTER_CURSOR
;
1422 if (edit
->curs1
< edit
->start_display
)
1424 edit
->start_display
--;
1434 edit_backspace (WEdit
* edit
, const int byte_delete
)
1443 edit
->mark1
-= (edit
->mark1
>= edit
->curs1
);
1444 edit
->mark2
-= (edit
->mark2
>= edit
->curs1
);
1445 edit
->last_get_rule
-= (edit
->last_get_rule
>= edit
->curs1
);
1448 if (edit
->utf8
&& byte_delete
== 0)
1450 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1454 for (i
= 1; i
<= cw
; i
++)
1456 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
1457 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
1458 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1460 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1461 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1465 edit_push_action (edit
, p
);
1467 edit_modification (edit
);
1470 if (edit
->book_mark
)
1471 book_mark_dec (edit
, edit
->curs_line
);
1473 edit
->total_lines
--;
1474 edit
->force
|= REDRAW_AFTER_CURSOR
;
1477 if (edit
->curs1
< edit
->start_display
)
1479 edit
->start_display
--;
1487 #ifdef FAST_MOVE_CURSOR
1490 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
1493 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
1496 next
-= (unsigned long) dest
;
1504 edit_move_backward_lots (WEdit
* edit
, long increment
)
1507 unsigned char *p
= NULL
;
1509 if (increment
> edit
->curs1
)
1510 increment
= edit
->curs1
;
1513 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
1515 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
1518 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1523 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1524 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
1531 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
1532 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
1533 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1534 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1537 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
1538 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1539 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1544 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1547 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1549 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1556 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
1567 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1568 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
1573 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
1574 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
1577 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
1579 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
1580 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
1585 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1588 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
1590 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1597 return edit_get_byte (edit
, edit
->curs1
);
1600 #endif /* ! FAST_MOVE_CURSOR */
1602 /* moves the cursor right or left: increment positive or negative respectively */
1604 edit_cursor_move (WEdit
* edit
, long increment
)
1606 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
1608 #ifdef FAST_MOVE_CURSOR
1609 if (increment
< -256)
1611 edit
->force
|= REDRAW_PAGE
;
1612 edit_move_backward_lots (edit
, -increment
);
1615 #endif /* ! FAST_MOVE_CURSOR */
1619 for (; increment
< 0; increment
++)
1624 edit_push_action (edit
, CURS_RIGHT
);
1626 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1627 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
1628 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1629 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1630 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
1633 buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
- 1) & M_EDIT_BUF_SIZE
];
1634 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
1636 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
1637 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
1643 edit
->force
|= REDRAW_LINE_BELOW
;
1648 else if (increment
> 0)
1650 for (; increment
> 0; increment
--)
1655 edit_push_action (edit
, CURS_LEFT
);
1657 c
= edit_get_byte (edit
, edit
->curs1
);
1658 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
1659 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
1660 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
1662 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
1664 1) & M_EDIT_BUF_SIZE
) - 1];
1665 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
1667 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
1668 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
1674 edit
->force
|= REDRAW_LINE_ABOVE
;
1680 /* These functions return positions relative to lines */
1682 /* returns index of last char on line + 1 */
1684 edit_eol (WEdit
* edit
, long current
)
1686 if (current
< edit
->last_byte
)
1689 if (edit_get_byte (edit
, current
) == '\n')
1693 return edit
->last_byte
;
1697 /* returns index of first char on line */
1699 edit_bol (WEdit
* edit
, long current
)
1704 if (edit_get_byte (edit
, current
- 1) == '\n')
1714 edit_count_lines (WEdit
* edit
, long current
, long upto
)
1717 if (upto
> edit
->last_byte
)
1718 upto
= edit
->last_byte
;
1721 while (current
< upto
)
1722 if (edit_get_byte (edit
, current
++) == '\n')
1728 /* If lines is zero this returns the count of lines from current to upto. */
1729 /* If upto is zero returns index of lines forward current. */
1731 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
1735 return edit_count_lines (edit
, current
, upto
);
1744 next
= edit_eol (edit
, current
) + 1;
1745 if (next
> edit
->last_byte
)
1755 /* Returns offset of 'lines' lines up from current */
1757 edit_move_backward (WEdit
* edit
, long current
, long lines
)
1761 current
= edit_bol (edit
, current
);
1762 while ((lines
--) && current
!= 0)
1763 current
= edit_bol (edit
, current
- 1);
1767 /* If cols is zero this returns the count of columns from current to upto. */
1768 /* If upto is zero returns index of cols across from current. */
1770 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
1781 q
= edit
->last_byte
+ 2;
1783 for (col
= 0, p
= current
; p
< q
; p
++)
1797 orig_c
= c
= edit_get_byte (edit
, p
);
1801 utf_ch
= edit_get_utf (edit
, p
, &cw
);
1806 if (g_unichar_iswide (utf_ch
))
1809 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
1813 c
= convert_to_display_c (c
);
1815 col
+= TAB_SIZE
- col
% TAB_SIZE
;
1823 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
1824 /* '\r' is shown as ^M, so we must advance 2 characters */
1825 /* Caret notation for control characters */
1833 /* returns the current column position of the cursor */
1835 edit_get_col (WEdit
* edit
)
1837 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1841 /* Scrolling functions */
1844 edit_update_curs_row (WEdit
* edit
)
1846 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
1850 edit_update_curs_col (WEdit
* edit
)
1852 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
1856 edit_get_curs_col (const WEdit
* edit
)
1858 return edit
->curs_col
;
1861 /*moves the display start position up by i lines */
1863 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
1865 unsigned long lines_above
= edit
->start_line
;
1866 if (i
> lines_above
)
1870 edit
->start_line
-= i
;
1871 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
1872 edit
->force
|= REDRAW_PAGE
;
1873 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1875 edit_update_curs_row (edit
);
1879 /* returns 1 if could scroll, 0 otherwise */
1881 edit_scroll_downward (WEdit
* edit
, int i
)
1884 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
1885 if (lines_below
> 0)
1887 if (i
> lines_below
)
1889 edit
->start_line
+= i
;
1890 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
1891 edit
->force
|= REDRAW_PAGE
;
1892 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1894 edit_update_curs_row (edit
);
1898 edit_scroll_right (WEdit
* edit
, int i
)
1900 edit
->force
|= REDRAW_PAGE
;
1901 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1902 edit
->start_col
-= i
;
1906 edit_scroll_left (WEdit
* edit
, int i
)
1908 if (edit
->start_col
)
1910 edit
->start_col
+= i
;
1911 if (edit
->start_col
> 0)
1912 edit
->start_col
= 0;
1913 edit
->force
|= REDRAW_PAGE
;
1914 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
1918 /* high level cursor movement commands */
1921 is_in_indent (WEdit
* edit
)
1923 long p
= edit_bol (edit
, edit
->curs1
);
1924 while (p
< edit
->curs1
)
1925 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
1930 static int left_of_four_spaces (WEdit
* edit
);
1933 edit_move_to_prev_col (WEdit
* edit
, long p
)
1935 int prev
= edit
->prev_col
;
1936 int over
= edit
->over_col
;
1937 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
1939 if (option_cursor_beyond_eol
)
1942 edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
1943 edit_eol (edit
, edit
->curs1
));
1945 if (line_len
< prev
+ edit
->over_col
)
1947 edit
->over_col
= prev
+ over
- line_len
;
1948 edit
->prev_col
= line_len
;
1949 edit
->curs_col
= line_len
;
1953 edit
->curs_col
= prev
+ over
;
1954 edit
->prev_col
= edit
->curs_col
;
1961 if (is_in_indent (edit
) && option_fake_half_tabs
)
1963 edit_update_curs_col (edit
);
1965 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
1967 int q
= edit
->curs_col
;
1968 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
1969 p
= edit_bol (edit
, edit
->curs1
);
1970 edit_cursor_move (edit
,
1971 edit_move_forward3 (edit
, p
, edit
->curs_col
,
1973 if (!left_of_four_spaces (edit
))
1974 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
1981 is_blank (WEdit
* edit
, long offset
)
1985 s
= edit_bol (edit
, offset
);
1986 f
= edit_eol (edit
, offset
) - 1;
1989 c
= edit_get_byte (edit
, s
++);
1997 /* returns the offset of line i */
1999 edit_find_line (WEdit
* edit
, int line
)
2003 if (!edit
->caches_valid
)
2005 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2006 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
2007 /* three offsets that we *know* are line 0 at 0 and these two: */
2008 edit
->line_numbers
[1] = edit
->curs_line
;
2009 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
2010 edit
->line_numbers
[2] = edit
->total_lines
;
2011 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
2012 edit
->caches_valid
= 1;
2014 if (line
>= edit
->total_lines
)
2015 return edit
->line_offsets
[2];
2018 /* find the closest known point */
2019 for (i
= 0; i
< N_LINE_CACHES
; i
++)
2022 n
= abs (edit
->line_numbers
[i
] - line
);
2030 return edit
->line_offsets
[j
]; /* know the offset exactly */
2031 if (m
== 1 && j
>= 3)
2032 i
= j
; /* one line different - caller might be looping, so stay in this cache */
2034 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
2035 if (line
> edit
->line_numbers
[j
])
2036 edit
->line_offsets
[i
] =
2037 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
2039 edit
->line_offsets
[i
] =
2040 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
2041 edit
->line_numbers
[i
] = line
;
2042 return edit
->line_offsets
[i
];
2046 line_is_blank (WEdit
* edit
, long line
)
2048 return is_blank (edit
, edit_find_line (edit
, line
));
2051 /* moves up until a blank line is reached, or until just
2052 before a non-blank line is reached */
2054 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
2057 if (edit
->curs_line
> 1)
2059 if (line_is_blank (edit
, edit
->curs_line
))
2061 if (line_is_blank (edit
, edit
->curs_line
- 1))
2063 for (i
= edit
->curs_line
- 1; i
; i
--)
2064 if (!line_is_blank (edit
, i
))
2072 for (i
= edit
->curs_line
- 1; i
; i
--)
2073 if (line_is_blank (edit
, i
))
2079 for (i
= edit
->curs_line
- 1; i
; i
--)
2080 if (line_is_blank (edit
, i
))
2084 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
2087 /* moves down until a blank line is reached, or until just
2088 before a non-blank line is reached */
2090 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
2093 if (edit
->curs_line
>= edit
->total_lines
- 1)
2095 i
= edit
->total_lines
;
2099 if (line_is_blank (edit
, edit
->curs_line
))
2101 if (line_is_blank (edit
, edit
->curs_line
+ 1))
2103 for (i
= edit
->curs_line
+ 1; i
; i
++)
2104 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
2112 for (i
= edit
->curs_line
+ 1; i
; i
++)
2113 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2119 for (i
= edit
->curs_line
+ 1; i
; i
++)
2120 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
2124 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
2128 edit_begin_page (WEdit
* edit
)
2130 edit_update_curs_row (edit
);
2131 edit_move_up (edit
, edit
->curs_row
, 0);
2135 edit_end_page (WEdit
* edit
)
2137 edit_update_curs_row (edit
);
2138 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
2142 /* goto beginning of text */
2144 edit_move_to_top (WEdit
* edit
)
2146 if (edit
->curs_line
)
2148 edit_cursor_move (edit
, -edit
->curs1
);
2149 edit_move_to_prev_col (edit
, 0);
2150 edit
->force
|= REDRAW_PAGE
;
2151 edit
->search_start
= 0;
2152 edit_update_curs_row (edit
);
2157 /* goto end of text */
2159 edit_move_to_bottom (WEdit
* edit
)
2161 if (edit
->curs_line
< edit
->total_lines
)
2163 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
2164 edit
->start_display
= edit
->last_byte
;
2165 edit
->start_line
= edit
->total_lines
;
2166 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
2167 edit
->force
|= REDRAW_PAGE
;
2171 /* goto beginning of line */
2173 edit_cursor_to_bol (WEdit
* edit
)
2175 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
2176 edit
->search_start
= edit
->curs1
;
2177 edit
->prev_col
= edit_get_col (edit
);
2181 /* goto end of line */
2183 edit_cursor_to_eol (WEdit
* edit
)
2185 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
2186 edit
->search_start
= edit
->curs1
;
2187 edit
->prev_col
= edit_get_col (edit
);
2191 /* move cursor to line 'line' */
2193 edit_move_to_line (WEdit
* e
, long line
)
2195 if (line
< e
->curs_line
)
2196 edit_move_up (e
, e
->curs_line
- line
, 0);
2198 edit_move_down (e
, line
- e
->curs_line
, 0);
2199 edit_scroll_screen_over_cursor (e
);
2202 /* scroll window so that first visible line is 'line' */
2204 edit_move_display (WEdit
* e
, long line
)
2206 if (line
< e
->start_line
)
2207 edit_scroll_upward (e
, e
->start_line
- line
);
2209 edit_scroll_downward (e
, line
- e
->start_line
);
2212 /* save markers onto undo stack */
2214 edit_push_markers (WEdit
* edit
)
2216 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2217 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
2221 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
2230 /* highlight marker toggle */
2232 edit_mark_cmd (WEdit
* edit
, int unmark
)
2234 edit_push_markers (edit
);
2237 edit_set_markers (edit
, 0, 0, 0, 0);
2238 edit
->force
|= REDRAW_PAGE
;
2242 if (edit
->mark2
>= 0)
2244 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
2245 edit
->curs_col
+ edit
->over_col
);
2246 edit
->force
|= REDRAW_PAGE
;
2249 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
2250 edit
->curs_col
+ edit
->over_col
);
2254 static unsigned long
2259 const char option_chars_move_whole_word
[] =
2260 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
2266 if (*option_chars_move_whole_word
== '!')
2268 return 0x80000000UL
;
2270 if (g_ascii_isupper ((gchar
) c
))
2272 else if (g_ascii_islower ((gchar
) c
))
2274 else if (g_ascii_isalpha (c
))
2276 else if (isdigit (c
))
2278 else if (isspace (c
))
2280 q
= strchr (option_chars_move_whole_word
, c
);
2282 return 0xFFFFFFFFUL
;
2285 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
2290 while ((q
= strchr (q
+ 1, c
)));
2295 edit_left_word_move (WEdit
* edit
, int s
)
2300 if (column_highlighting
2301 && edit
->mark1
!= edit
->mark2
2302 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2304 edit_cursor_move (edit
, -1);
2307 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2308 c2
= edit_get_byte (edit
, edit
->curs1
);
2309 if (!(my_type_of (c1
) & my_type_of (c2
)))
2311 if (isspace (c1
) && !isspace (c2
))
2314 if (!isspace (c1
) && isspace (c2
))
2320 edit_left_word_move_cmd (WEdit
* edit
)
2322 edit_left_word_move (edit
, 0);
2323 edit
->force
|= REDRAW_PAGE
;
2327 edit_right_word_move (WEdit
* edit
, int s
)
2332 if (column_highlighting
2333 && edit
->mark1
!= edit
->mark2
2334 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
2336 edit_cursor_move (edit
, 1);
2337 if (edit
->curs1
>= edit
->last_byte
)
2339 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
2340 c2
= edit_get_byte (edit
, edit
->curs1
);
2341 if (!(my_type_of (c1
) & my_type_of (c2
)))
2343 if (isspace (c1
) && !isspace (c2
))
2346 if (!isspace (c1
) && isspace (c2
))
2352 edit_right_word_move_cmd (WEdit
* edit
)
2354 edit_right_word_move (edit
, 0);
2355 edit
->force
|= REDRAW_PAGE
;
2359 edit_right_char_move_cmd (WEdit
* edit
)
2365 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
2371 c
= edit_get_byte (edit
, edit
->curs1
);
2373 if (option_cursor_beyond_eol
&& c
== '\n')
2379 edit_cursor_move (edit
, cw
);
2384 edit_left_char_move_cmd (WEdit
* edit
)
2387 if (column_highlighting
2388 && option_cursor_beyond_eol
2389 && edit
->mark1
!= edit
->mark2
2390 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
2394 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
2398 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2404 edit_cursor_move (edit
, -cw
);
2408 /** Up or down cursor moving.
2409 direction = TRUE - move up
2413 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
2416 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
2425 edit
->force
|= REDRAW_PAGE
;
2429 edit_scroll_upward (edit
, i
);
2431 edit_scroll_downward (edit
, i
);
2433 p
= edit_bol (edit
, edit
->curs1
);
2435 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
2437 edit_cursor_move (edit
, p
- edit
->curs1
);
2439 edit_move_to_prev_col (edit
, p
);
2441 /* search start of current multibyte char (like CJK) */
2442 if (edit
->curs1
+ 1 < edit
->last_byte
)
2444 edit_right_char_move_cmd (edit
);
2445 edit_left_char_move_cmd (edit
);
2448 edit
->search_start
= edit
->curs1
;
2449 edit
->found_len
= 0;
2453 edit_right_delete_word (WEdit
* edit
)
2458 if (edit
->curs1
>= edit
->last_byte
)
2460 c1
= edit_delete (edit
, 1);
2461 c2
= edit_get_byte (edit
, edit
->curs1
);
2462 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2464 if (!(my_type_of (c1
) & my_type_of (c2
)))
2470 edit_left_delete_word (WEdit
* edit
)
2475 if (edit
->curs1
<= 0)
2477 c1
= edit_backspace (edit
, 1);
2478 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
2479 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
2481 if (!(my_type_of (c1
) & my_type_of (c2
)))
2487 the start column position is not recorded, and hence does not
2488 undo as it happed. But who would notice.
2491 edit_do_undo (WEdit
* edit
)
2496 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
2498 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
2505 edit_cursor_move (edit
, 1);
2508 edit_cursor_move (edit
, -1);
2511 edit_backspace (edit
, 1);
2514 edit_delete (edit
, 1);
2517 column_highlighting
= 1;
2520 column_highlighting
= 0;
2523 if (ac
>= 256 && ac
< 512)
2524 edit_insert_ahead (edit
, ac
- 256);
2525 if (ac
>= 0 && ac
< 256)
2526 edit_insert (edit
, ac
);
2528 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
2530 edit
->mark1
= ac
- MARK_1
;
2531 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
2533 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
2535 edit
->mark2
= ac
- MARK_2
;
2536 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
2539 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
2542 if (edit
->start_display
> ac
- KEY_PRESS
)
2544 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
2545 edit
->force
|= REDRAW_PAGE
;
2547 else if (edit
->start_display
< ac
- KEY_PRESS
)
2549 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
2550 edit
->force
|= REDRAW_PAGE
;
2552 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
2553 edit_update_curs_row (edit
);
2556 edit
->stack_disable
= 0;
2560 edit_delete_to_line_end (WEdit
* edit
)
2562 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2566 edit_delete (edit
, 1);
2571 edit_delete_to_line_begin (WEdit
* edit
)
2573 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2577 edit_backspace (edit
, 1);
2582 edit_delete_line (WEdit
* edit
)
2585 * Delete right part of the line.
2586 * Note that edit_get_byte() returns '\n' when byte position is
2589 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
2591 (void) edit_delete (edit
, 1);
2596 * Note that edit_delete() will not corrupt anything if called while
2597 * cursor position is EOF.
2599 (void) edit_delete (edit
, 1);
2602 * Delete left part of the line.
2603 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
2605 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
2607 (void) edit_backspace (edit
, 1);
2612 insert_spaces_tab (WEdit
* edit
, int half
)
2615 edit_update_curs_col (edit
);
2616 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
2617 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
2620 edit_insert (edit
, ' ');
2626 is_aligned_on_a_tab (WEdit
* edit
)
2628 edit_update_curs_col (edit
);
2629 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
2630 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
2634 right_of_four_spaces (WEdit
* edit
)
2637 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2638 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
2640 return is_aligned_on_a_tab (edit
);
2645 left_of_four_spaces (WEdit
* edit
)
2648 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
2649 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
2651 return is_aligned_on_a_tab (edit
);
2656 edit_indent_width (WEdit
* edit
, long p
)
2659 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
2661 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
2665 edit_insert_indent (WEdit
* edit
, int indent
)
2667 if (!option_fill_tabs_with_spaces
)
2669 while (indent
>= TAB_SIZE
)
2671 edit_insert (edit
, '\t');
2675 while (indent
-- > 0)
2676 edit_insert (edit
, ' ');
2680 edit_auto_indent (WEdit
* edit
)
2685 /* use the previous line as a template */
2686 p
= edit_move_backward (edit
, p
, 1);
2687 /* copy the leading whitespace of the line */
2689 { /* no range check - the line _is_ \n-terminated */
2690 c
= edit_get_byte (edit
, p
++);
2691 if (c
!= ' ' && c
!= '\t')
2693 edit_insert (edit
, c
);
2698 edit_double_newline (WEdit
* edit
)
2700 edit_insert (edit
, '\n');
2701 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
2703 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
2705 edit
->force
|= REDRAW_PAGE
;
2706 edit_insert (edit
, '\n');
2710 edit_tab_cmd (WEdit
* edit
)
2714 if (option_fake_half_tabs
)
2716 if (is_in_indent (edit
))
2718 /*insert a half tab (usually four spaces) unless there is a
2719 half tab already behind, then delete it and insert a
2721 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
2723 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
2724 edit_backspace (edit
, 1);
2725 edit_insert (edit
, '\t');
2729 insert_spaces_tab (edit
, 1);
2734 if (option_fill_tabs_with_spaces
)
2736 insert_spaces_tab (edit
, 0);
2740 edit_insert (edit
, '\t');
2745 check_and_wrap_line (WEdit
* edit
)
2748 if (!option_typewriter_wrap
)
2750 edit_update_curs_col (edit
);
2751 if (edit
->curs_col
< option_word_wrap_line_length
)
2757 c
= edit_get_byte (edit
, curs
);
2758 if (c
== '\n' || curs
<= 0)
2760 edit_insert (edit
, '\n');
2763 if (c
== ' ' || c
== '\t')
2765 int current
= edit
->curs1
;
2766 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
2767 edit_insert (edit
, '\n');
2768 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
2774 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
2777 edit_push_key_press (WEdit
* edit
)
2779 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
2780 if (edit
->mark2
== -1)
2781 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
2784 /* this find the matching bracket in either direction, and sets edit->bracket */
2786 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
2788 const char *const b
= "{}{[][()(", *p
;
2789 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
2790 unsigned long j
= 0;
2792 edit_update_curs_row (edit
);
2793 c
= edit_get_byte (edit
, edit
->curs1
);
2796 if (!furthest_bracket_search
)
2797 furthest_bracket_search
--;
2798 /* not on a bracket at all */
2801 /* the matching bracket */
2803 /* going left or right? */
2804 if (strchr ("{[(", c
))
2806 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
2808 /* out of buffer? */
2809 if (q
>= edit
->last_byte
|| q
< 0)
2811 a
= edit_get_byte (edit
, q
);
2812 /* don't want to eat CPU */
2813 if (j
++ > furthest_bracket_search
)
2815 /* out of screen? */
2818 if (q
< edit
->start_display
)
2820 /* count lines if searching downward */
2821 if (inc
> 0 && a
== '\n')
2822 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
2825 /* count bracket depth */
2826 i
+= (a
== c
) - (a
== d
);
2827 /* return if bracket depth is zero */
2835 static long last_bracket
= -1;
2838 edit_find_bracket (WEdit
* edit
)
2840 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
2841 if (last_bracket
!= edit
->bracket
)
2842 edit
->force
|= REDRAW_PAGE
;
2843 last_bracket
= edit
->bracket
;
2847 edit_goto_matching_bracket (WEdit
* edit
)
2851 q
= edit_get_bracket (edit
, 0, 0);
2854 edit
->bracket
= edit
->curs1
;
2855 edit
->force
|= REDRAW_PAGE
;
2856 edit_cursor_move (edit
, q
- edit
->curs1
);
2861 * This executes a command as though the user initiated it through a key
2862 * press. Callback with WIDGET_KEY as a message calls this after
2863 * translating the key press. This function can be used to pass any
2864 * command to the editor. Note that the screen wouldn't update
2865 * automatically. Either of command or char_for_insertion must be
2866 * passed as -1. Commands are executed, and char_for_insertion is
2867 * inserted at the cursor.
2870 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2872 if (command
== CK_Begin_Record_Macro
)
2875 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
2878 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
2880 edit
->force
|= REDRAW_COMPLETELY
;
2881 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
2885 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
2887 edit
->macro
[edit
->macro_i
].command
= command
;
2888 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
2890 /* record the beginning of a set of editing actions initiated by a key press */
2891 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
2892 edit_push_key_press (edit
);
2894 edit_execute_cmd (edit
, command
, char_for_insertion
);
2895 if (column_highlighting
)
2896 edit
->force
|= REDRAW_PAGE
;
2899 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
2902 This executes a command at a lower level than macro recording.
2903 It also does not push a key_press onto the undo stack. This means
2904 that if it is called many times, a single undo command will undo
2905 all of them. It also does not check for the Undo command.
2908 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
2910 edit
->force
|= REDRAW_LINE
;
2912 /* The next key press will unhighlight the found string, so update
2914 if (edit
->found_len
|| column_highlighting
)
2915 edit
->force
|= REDRAW_PAGE
;
2917 if (command
/ 100 == 6)
2918 { /* a highlight command like shift-arrow */
2919 column_highlighting
= 0;
2920 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
2922 edit_mark_cmd (edit
, 1); /* clear */
2923 edit_mark_cmd (edit
, 0); /* marking on */
2925 edit
->highlight
= 1;
2928 { /* any other command */
2929 if (edit
->highlight
)
2930 edit_mark_cmd (edit
, 0); /* clear */
2931 edit
->highlight
= 0;
2934 /* first check for undo */
2935 if (command
== CK_Undo
)
2937 edit_do_undo (edit
);
2938 edit
->found_len
= 0;
2939 edit
->prev_col
= edit_get_col (edit
);
2940 edit
->search_start
= edit
->curs1
;
2944 /* An ordinary key press */
2945 if (char_for_insertion
>= 0)
2947 if (edit
->overwrite
)
2949 /* remove char only one time, after input first byte, multibyte chars */
2950 if ((!utf8_display
|| edit
->charpoint
== 0) && edit_get_byte (edit
, edit
->curs1
) != '\n')
2951 edit_delete (edit
, 0);
2953 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2954 edit_insert_over (edit
);
2956 if (char_for_insertion
> 255 && utf8_display
== 0)
2958 unsigned char str
[6 + 1];
2960 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
2970 while (str
[i
] != 0 && i
<= 6)
2972 char_for_insertion
= str
[i
];
2973 edit_insert (edit
, char_for_insertion
);
2979 edit_insert (edit
, char_for_insertion
);
2981 if (option_auto_para_formatting
)
2983 format_paragraph (edit
, 0);
2984 edit
->force
|= REDRAW_PAGE
;
2987 check_and_wrap_line (edit
);
2988 edit
->found_len
= 0;
2989 edit
->prev_col
= edit_get_col (edit
);
2990 edit
->search_start
= edit
->curs1
;
2991 edit_find_bracket (edit
);
2999 case CK_Begin_Page_Highlight
:
3000 case CK_End_Page_Highlight
:
3007 if (edit
->mark2
>= 0)
3009 if (!option_persistent_selections
)
3011 if (column_highlighting
)
3012 edit_push_action (edit
, COLUMN_ON
);
3013 column_highlighting
= 0;
3014 edit_mark_cmd (edit
, 1);
3023 case CK_Begin_Page_Highlight
:
3024 case CK_End_Page_Highlight
:
3029 case CK_Word_Left_Highlight
:
3030 case CK_Word_Right_Highlight
:
3031 case CK_Up_Highlight
:
3032 case CK_Down_Highlight
:
3033 case CK_Up_Alt_Highlight
:
3034 case CK_Down_Alt_Highlight
:
3035 if (edit
->mark2
== -1)
3036 break; /*marking is following the cursor: may need to highlight a whole line */
3039 case CK_Left_Highlight
:
3040 case CK_Right_Highlight
:
3041 edit
->force
|= REDRAW_CHAR_ONLY
;
3044 /* basic cursor key commands */
3048 /* if non persistent selection and text selected */
3049 if (!option_persistent_selections
)
3051 if (edit
->mark1
!= edit
->mark2
)
3053 edit_block_delete_cmd (edit
);
3057 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3062 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3064 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3065 edit_backspace (edit
, 1);
3070 if (option_fake_half_tabs
)
3073 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3075 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3076 edit_backspace (edit
, 1);
3081 edit_backspace (edit
, 0);
3084 /* if non persistent selection and text selected */
3085 if (!option_persistent_selections
)
3087 if (edit
->mark1
!= edit
->mark2
)
3089 edit_block_delete_cmd (edit
);
3094 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3095 edit_insert_over (edit
);
3097 if (option_fake_half_tabs
)
3100 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3102 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3103 edit_delete (edit
, 1);
3107 edit_delete (edit
, 0);
3109 case CK_Delete_Word_Left
:
3111 edit_left_delete_word (edit
);
3113 case CK_Delete_Word_Right
:
3114 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3115 edit_insert_over (edit
);
3117 edit_right_delete_word (edit
);
3119 case CK_Delete_Line
:
3120 edit_delete_line (edit
);
3122 case CK_Delete_To_Line_End
:
3123 edit_delete_to_line_end (edit
);
3125 case CK_Delete_To_Line_Begin
:
3126 edit_delete_to_line_begin (edit
);
3130 if (option_auto_para_formatting
)
3132 edit_double_newline (edit
);
3133 if (option_return_does_auto_indent
)
3134 edit_auto_indent (edit
);
3135 format_paragraph (edit
, 0);
3139 edit_insert (edit
, '\n');
3140 if (option_return_does_auto_indent
)
3142 edit_auto_indent (edit
);
3147 edit_insert (edit
, '\n');
3150 case CK_Page_Up_Alt_Highlight
:
3151 column_highlighting
= 1;
3153 case CK_Page_Up_Highlight
:
3154 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3156 case CK_Page_Down_Alt_Highlight
:
3157 column_highlighting
= 1;
3159 case CK_Page_Down_Highlight
:
3160 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3162 case CK_Left_Alt_Highlight
:
3163 column_highlighting
= 1;
3165 case CK_Left_Highlight
:
3166 if (option_fake_half_tabs
)
3168 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3170 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3173 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3174 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3178 edit_left_char_move_cmd (edit
);
3180 case CK_Right_Alt_Highlight
:
3181 column_highlighting
= 1;
3183 case CK_Right_Highlight
:
3184 if (option_fake_half_tabs
)
3186 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3188 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3189 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3193 edit_right_char_move_cmd (edit
);
3196 case CK_Begin_Page_Highlight
:
3197 edit_begin_page (edit
);
3200 case CK_End_Page_Highlight
:
3201 edit_end_page (edit
);
3204 case CK_Word_Left_Highlight
:
3206 edit_left_word_move_cmd (edit
);
3209 case CK_Word_Right_Highlight
:
3211 edit_right_word_move_cmd (edit
);
3213 case CK_Up_Alt_Highlight
:
3214 column_highlighting
= 1;
3216 case CK_Up_Highlight
:
3217 edit_move_up (edit
, 1, 0);
3219 case CK_Down_Alt_Highlight
:
3220 column_highlighting
= 1;
3222 case CK_Down_Highlight
:
3223 edit_move_down (edit
, 1, 0);
3225 case CK_Paragraph_Up_Alt_Highlight
:
3226 column_highlighting
= 1;
3227 case CK_Paragraph_Up
:
3228 case CK_Paragraph_Up_Highlight
:
3229 edit_move_up_paragraph (edit
, 0);
3231 case CK_Paragraph_Down_Alt_Highlight
:
3232 column_highlighting
= 1;
3233 case CK_Paragraph_Down
:
3234 case CK_Paragraph_Down_Highlight
:
3235 edit_move_down_paragraph (edit
, 0);
3237 case CK_Scroll_Up_Alt_Highlight
:
3238 column_highlighting
= 1;
3240 case CK_Scroll_Up_Highlight
:
3241 edit_move_up (edit
, 1, 1);
3243 case CK_Scroll_Down_Alt_Highlight
:
3244 column_highlighting
= 1;
3245 case CK_Scroll_Down
:
3246 case CK_Scroll_Down_Highlight
:
3247 edit_move_down (edit
, 1, 1);
3250 case CK_Home_Highlight
:
3251 edit_cursor_to_bol (edit
);
3254 case CK_End_Highlight
:
3255 edit_cursor_to_eol (edit
);
3258 /* if text marked shift block */
3259 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3261 if (edit
->mark2
< 0)
3262 edit_mark_cmd (edit
, 0);
3263 edit_move_block_to_right (edit
);
3267 if (option_cursor_beyond_eol
)
3268 edit_insert_over (edit
);
3269 edit_tab_cmd (edit
);
3270 if (option_auto_para_formatting
)
3272 format_paragraph (edit
, 0);
3273 edit
->force
|= REDRAW_PAGE
;
3277 check_and_wrap_line (edit
);
3282 case CK_Toggle_Insert
:
3283 edit
->overwrite
= (edit
->overwrite
== 0);
3287 if (edit
->mark2
>= 0)
3289 if (column_highlighting
)
3290 edit_push_action (edit
, COLUMN_ON
);
3291 column_highlighting
= 0;
3293 edit_mark_cmd (edit
, 0);
3295 case CK_Column_Mark
:
3296 if (!column_highlighting
)
3297 edit_push_action (edit
, COLUMN_OFF
);
3298 column_highlighting
= 1;
3299 edit_mark_cmd (edit
, 0);
3302 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3303 edit
->force
|= REDRAW_PAGE
;
3306 if (column_highlighting
)
3307 edit_push_action (edit
, COLUMN_ON
);
3308 column_highlighting
= 0;
3309 edit_mark_cmd (edit
, 1);
3312 case CK_Toggle_Line_State
:
3313 option_line_state
= !option_line_state
;
3314 if (option_line_state
)
3316 option_line_state_width
= LINE_STATE_WIDTH
;
3320 option_line_state_width
= 0;
3322 edit
->force
|= REDRAW_PAGE
;
3325 case CK_Toggle_Show_Margin
:
3326 show_right_margin
= !show_right_margin
;
3327 edit
->force
|= REDRAW_PAGE
;
3330 case CK_Toggle_Bookmark
:
3331 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3332 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3333 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3335 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3337 case CK_Flush_Bookmarks
:
3338 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3339 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3340 edit
->force
|= REDRAW_PAGE
;
3342 case CK_Next_Bookmark
:
3343 if (edit
->book_mark
)
3345 struct _book_mark
*p
;
3346 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3350 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3351 || p
->line
< edit
->start_line
)
3352 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3353 edit_move_to_line (edit
, p
->line
);
3357 case CK_Prev_Bookmark
:
3358 if (edit
->book_mark
)
3360 struct _book_mark
*p
;
3361 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3362 while (p
->line
== edit
->curs_line
)
3367 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3368 || p
->line
< edit
->start_line
)
3369 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3370 edit_move_to_line (edit
, p
->line
);
3375 case CK_Beginning_Of_Text
:
3376 case CK_Beginning_Of_Text_Highlight
:
3377 edit_move_to_top (edit
);
3379 case CK_End_Of_Text
:
3380 case CK_End_Of_Text_Highlight
:
3381 edit_move_to_bottom (edit
);
3385 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3386 edit_insert_over (edit
);
3387 edit_block_copy_cmd (edit
);
3390 edit_block_delete_cmd (edit
);
3393 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3394 edit_insert_over (edit
);
3395 edit_block_move_cmd (edit
);
3398 case CK_Shift_Block_Left
:
3399 if (edit
->mark1
!= edit
->mark2
)
3400 edit_move_block_to_left (edit
);
3402 case CK_Shift_Block_Right
:
3403 if (edit
->mark1
!= edit
->mark2
)
3404 edit_move_block_to_right (edit
);
3407 edit_copy_to_X_buf_cmd (edit
);
3410 edit_cut_to_X_buf_cmd (edit
);
3413 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3414 edit_insert_over (edit
);
3415 edit_paste_from_X_buf_cmd (edit
);
3417 case CK_Selection_History
:
3418 edit_paste_from_history (edit
);
3422 edit_save_as_cmd (edit
);
3425 edit_save_confirm_cmd (edit
);
3428 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3431 edit_save_block_cmd (edit
);
3433 case CK_Insert_File
:
3434 edit_insert_file_cmd (edit
);
3437 case CK_Load_Prev_File
:
3438 edit_load_back_cmd (edit
);
3440 case CK_Load_Next_File
:
3441 edit_load_forward_cmd (edit
);
3444 case CK_Load_Syntax_File
:
3445 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3447 case CK_Choose_Syntax
:
3448 edit_syntax_dialog ();
3451 case CK_Load_Menu_File
:
3452 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3455 case CK_Toggle_Syntax
:
3456 if ((option_syntax_highlighting
^= 1) == 1)
3457 edit_load_syntax (edit
, NULL
, option_syntax_type
);
3458 edit
->force
|= REDRAW_PAGE
;
3461 case CK_Toggle_Tab_TWS
:
3462 enable_show_tabs_tws
^= 1;
3463 edit
->force
|= REDRAW_PAGE
;
3467 edit_search_cmd (edit
, 0);
3470 edit_search_cmd (edit
, 1);
3473 edit_replace_cmd (edit
, 0);
3475 case CK_Replace_Again
:
3476 edit_replace_cmd (edit
, 1);
3478 case CK_Complete_Word
:
3479 /* if text marked shift block */
3480 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3482 edit_move_block_to_left (edit
);
3486 edit_complete_word_cmd (edit
);
3489 case CK_Find_Definition
:
3490 edit_get_match_keyword_cmd (edit
);
3493 dlg_stop (edit
->widget
.parent
);
3496 edit_new_cmd (edit
);
3499 edit_help_cmd (edit
);
3502 edit_refresh_cmd (edit
);
3504 case CK_SaveSetupCmd
:
3508 query_dialog (_(" About "),
3509 _("\n Cooledit v3.11.5\n\n"
3510 " Copyright (C) 1996 the Free Software Foundation\n\n"
3511 " A user friendly text editor written\n"
3512 " for the Midnight Commander.\n"), D_NORMAL
, 1, _("&OK"));
3517 case CK_Edit_Options
:
3518 edit_options_dialog ();
3520 case CK_Edit_Save_Mode
:
3521 menu_save_mode_cmd ();
3526 /* fool gcc to prevent a Y2K warning */
3527 char time_format
[] = "_c";
3528 time_format
[0] = '%';
3530 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3531 edit_print_string (edit
, s
);
3532 edit
->force
|= REDRAW_PAGE
;
3537 edit_goto_cmd (edit
);
3539 case CK_Paragraph_Format
:
3540 format_paragraph (edit
, 1);
3541 edit
->force
|= REDRAW_PAGE
;
3543 case CK_Delete_Macro
:
3544 edit_delete_macro_cmd (edit
);
3546 case CK_Match_Bracket
:
3547 edit_goto_matching_bracket (edit
);
3553 edit_sort_cmd (edit
);
3556 edit_ext_cmd (edit
);
3559 edit_mail_dialog (edit
);
3564 case CK_SelectCodepage
:
3565 edit_select_codepage_cmd (edit
);
3567 case CK_Insert_Literal
:
3568 edit_insert_literal_cmd (edit
);
3570 case CK_Execute_Macro
:
3571 edit_execute_macro_cmd (edit
);
3573 case CK_Begin_End_Macro
:
3574 edit_begin_end_macro_cmd (edit
);
3584 if ((command
/ 1000) == 1) /* a shell command */
3585 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3586 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
3587 { /* a macro command */
3588 struct macro m
[MAX_MACRO_LENGTH
];
3590 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3591 edit_execute_macro (edit
, m
, nm
);
3594 /* keys which must set the col position, and the search vars */
3600 case CK_Replace_Again
:
3601 case CK_Complete_Word
:
3602 edit
->prev_col
= edit_get_col (edit
);
3605 case CK_Up_Highlight
:
3606 case CK_Up_Alt_Highlight
:
3608 case CK_Down_Highlight
:
3609 case CK_Down_Alt_Highlight
:
3611 case CK_Page_Up_Highlight
:
3612 case CK_Page_Up_Alt_Highlight
:
3614 case CK_Page_Down_Highlight
:
3615 case CK_Page_Down_Alt_Highlight
:
3616 case CK_Beginning_Of_Text
:
3617 case CK_Beginning_Of_Text_Highlight
:
3618 case CK_End_Of_Text
:
3619 case CK_End_Of_Text_Highlight
:
3620 case CK_Paragraph_Up
:
3621 case CK_Paragraph_Up_Highlight
:
3622 case CK_Paragraph_Up_Alt_Highlight
:
3623 case CK_Paragraph_Down
:
3624 case CK_Paragraph_Down_Highlight
:
3625 case CK_Paragraph_Down_Alt_Highlight
:
3627 case CK_Scroll_Up_Highlight
:
3628 case CK_Scroll_Up_Alt_Highlight
:
3629 case CK_Scroll_Down
:
3630 case CK_Scroll_Down_Highlight
:
3631 case CK_Scroll_Down_Alt_Highlight
:
3632 edit
->search_start
= edit
->curs1
;
3633 edit
->found_len
= 0;
3636 edit
->found_len
= 0;
3637 edit
->prev_col
= edit_get_col (edit
);
3638 edit
->search_start
= edit
->curs1
;
3640 edit_find_bracket (edit
);
3642 if (option_auto_para_formatting
)
3648 case CK_Delete_Word_Left
:
3649 case CK_Delete_Word_Right
:
3650 case CK_Delete_To_Line_End
:
3651 case CK_Delete_To_Line_Begin
:
3652 format_paragraph (edit
, 0);
3653 edit
->force
|= REDRAW_PAGE
;
3660 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
3664 if (edit
->macro_depth
++ > 256)
3666 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
3667 edit
->macro_depth
--;
3670 edit
->force
|= REDRAW_PAGE
;
3673 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
3675 edit_update_screen (edit
);
3676 edit
->macro_depth
--;
3679 /* User edit menu, like user menu (F2) but only in editor. */
3681 user_menu (WEdit
* edit
)
3685 long start_mark
, end_mark
;
3688 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
3690 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
3692 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
3694 /* run shell scripts from menu */
3695 user_menu_cmd (edit
);
3697 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
3704 /* i.e. we have marked block */
3705 rc
= edit_block_delete_cmd (edit
);
3709 edit_insert_file (edit
, block_file
);
3711 /* truncate block file */
3712 fd
= fopen (block_file
, "w");
3716 edit_refresh_cmd (edit
);
3717 edit
->force
|= REDRAW_COMPLETELY
;
3719 g_free (block_file
);
3723 edit_stack_init (void)
3725 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3727 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
3728 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3731 edit_stack_iterator
= 0;
3735 edit_stack_free (void)
3737 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3738 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
3743 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
3745 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3750 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
3752 edit_move_updown (edit
, i
, do_scroll
, FALSE
);