1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
7 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * \brief Source: editor low level data handling and cursor fundamentals
34 #include <sys/types.h>
43 #include "lib/global.h"
45 #include "lib/tty/color.h"
46 #include "lib/tty/tty.h" /* attrset() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
49 #include "lib/vfs/mc-vfs/vfs.h"
50 #include "lib/strutil.h" /* utf string functions */
51 #include "lib/util.h" /* load_file_position(), save_file_position() */
52 #include "lib/timefmt.h" /* time formatting */
54 #include "lib/widget.h"
57 #include "lib/charsets.h" /* get_codepage_id */
60 #include "src/filemanager/cmd.h" /* view_other_cmd() */
61 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
63 #include "src/main.h" /* source_codepage */
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/learn.h" /* learn_keys */
66 #include "src/keybind-defaults.h"
68 #include "edit-impl.h"
69 #include "edit-widget.h"
71 /*** global variables ****************************************************************************/
73 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
74 int option_typewriter_wrap
= 0;
75 int option_auto_para_formatting
= 0;
76 int option_fill_tabs_with_spaces
= 0;
77 int option_return_does_auto_indent
= 1;
78 int option_backspace_through_tabs
= 0;
79 int option_fake_half_tabs
= 1;
80 int option_save_mode
= EDIT_QUICK_SAVE
;
81 int option_save_position
= 1;
82 int option_max_undo
= 32768;
83 int option_persistent_selections
= 1;
84 int option_cursor_beyond_eol
= 0;
85 int option_line_state
= 0;
86 int option_line_state_width
= 0;
88 int option_edit_right_extreme
= 0;
89 int option_edit_left_extreme
= 0;
90 int option_edit_top_extreme
= 0;
91 int option_edit_bottom_extreme
= 0;
92 int enable_show_tabs_tws
= 1;
93 int option_check_nl_at_eof
= 0;
94 int show_right_margin
= 0;
96 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
97 char *option_backup_ext
= NULL
;
99 int edit_stack_iterator
= 0;
100 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
101 /* magic sequense for say than block is vertical */
102 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
104 /*** file scope macro definitions ****************************************************************/
106 #define TEMP_BUF_LEN 1024
108 #define space_width 1
110 /*** file scope type declarations ****************************************************************/
112 /*** file scope variables ************************************************************************/
114 /* detecting an error on save is easy: just check if every byte has been written. */
115 /* detecting an error on read, is not so easy 'cos there is not way to tell
116 whether you read everything or not. */
117 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
118 static const struct edit_filters
120 const char *read
, *write
, *extension
;
124 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
125 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
126 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
127 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
132 static long last_bracket
= -1;
134 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
136 /*** file scope functions ************************************************************************/
137 /* --------------------------------------------------------------------------------------------- */
141 * here's a quick sketch of the layout: (don't run this through indent.)
143 * (b1 is buffers1 and b2 is buffers2)
146 * \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
147 * ______________________________________|______________________________________
149 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
150 * |-> |-> |-> |-> |-> |-> |
152 * _<------------------------->|<----------------->_
153 * WEdit->curs2 | WEdit->curs1
158 * file end|||file beginning
167 /* --------------------------------------------------------------------------------------------- */
169 static void user_menu (WEdit
* edit
);
170 static int left_of_four_spaces (WEdit
* edit
);
171 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
173 /* --------------------------------------------------------------------------------------------- */
178 const char *header
= N_("About");
179 const char *button_name
= N_("&OK");
180 const char *const version
= "MCEdit " VERSION
;
181 char text
[BUF_LARGE
];
183 int win_len
, version_len
, button_len
;
190 button_name
= _(button_name
);
193 button_len
= str_term_width1 (button_name
) + 5;
194 version_len
= str_term_width1 (version
);
196 g_snprintf (text
, sizeof (text
),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len
= str_term_width1 (header
);
202 win_len
= max (win_len
, version_len
);
203 win_len
= max (win_len
, button_len
);
205 /* count width and height of text */
206 str_msg_term_size (text
, &lines
, &cols
);
208 cols
= max (win_len
, cols
) + 6;
211 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
212 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
214 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
215 add_widget (about_dlg
, label_new (5, 3, text
));
216 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
217 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
220 destroy_dlg (about_dlg
);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
229 edit_init_buffers (WEdit
* edit
)
233 for (j
= 0; j
<= MAXBUFF
; j
++)
235 edit
->buffers1
[j
] = NULL
;
236 edit
->buffers2
[j
] = NULL
;
241 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
251 edit_load_file_fast (WEdit
* edit
, const char *filename
)
257 edit
->curs2
= edit
->last_byte
;
258 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
260 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
265 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
266 edit_error_dialog (_("Error"), errmsg
);
271 if (!edit
->buffers2
[buf2
])
272 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
277 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
278 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
281 for (buf
= buf2
- 1; buf
>= 0; buf
--)
283 /* edit->buffers2[0] is already allocated */
284 if (!edit
->buffers2
[buf
])
285 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
286 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
294 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
295 edit_error_dialog (_("Error"), err_str
);
302 /* --------------------------------------------------------------------------------------------- */
303 /** Return index of the filter or -1 is there is no appropriate filter */
306 edit_find_filter (const char *filename
)
310 if (filename
== NULL
)
313 l
= strlen (filename
);
314 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
316 e
= strlen (all_filters
[i
].extension
);
318 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
324 /* --------------------------------------------------------------------------------------------- */
327 edit_get_filter (const char *filename
)
330 char *p
, *quoted_name
;
332 i
= edit_find_filter (filename
);
336 quoted_name
= name_quote (filename
, 0);
337 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
338 g_free (quoted_name
);
342 /* --------------------------------------------------------------------------------------------- */
345 edit_insert_stream (WEdit
* edit
, FILE * f
)
349 while ((c
= fgetc (f
)) >= 0)
351 edit_insert (edit
, c
);
357 /* --------------------------------------------------------------------------------------------- */
358 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
361 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
364 gchar
*errmsg
= NULL
;
366 /* Try opening an existing file */
367 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
372 * Try creating the file. O_EXCL prevents following broken links
373 * and opening existing files.
375 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
378 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
383 /* New file, delete it if it's not modified or saved */
384 edit
->delete_file
= 1;
388 /* Check what we have opened */
389 if (mc_fstat (file
, st
) < 0)
391 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
395 /* We want to open regular files only */
396 if (!S_ISREG (st
->st_mode
))
398 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
403 * Don't delete non-empty files.
404 * O_EXCL should prevent it, but let's be on the safe side.
407 edit
->delete_file
= 0;
409 if (st
->st_size
>= SIZE_LIMIT
)
410 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
413 (void) mc_close (file
);
417 edit_error_dialog (_("Error"), errmsg
);
424 /* --------------------------------------------------------------------------------------------- */
426 * Open the file and load it into the buffers, either directly or using
427 * a filter. Return 0 on success, 1 on error.
429 * Fast loading (edit_load_file_fast) is used when the file size is
430 * known. In this case the data is read into the buffers by blocks.
431 * If the file size is not known, the data is loaded byte by byte in
436 edit_load_file (WEdit
* edit
)
440 /* Cannot do fast load if a filter is used */
441 if (edit_find_filter (edit
->filename
) >= 0)
445 * VFS may report file size incorrectly, and slow load is not a big
446 * deal considering overhead in VFS.
448 if (!vfs_file_is_local (edit
->filename
))
452 * FIXME: line end translation should disable fast loading as well
453 * Consider doing fseek() to the end and ftell() for the real size.
458 /* If we are dealing with a real file, check that it exists */
459 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
464 /* nothing to load */
468 edit_init_buffers (edit
);
472 edit
->last_byte
= edit
->stat1
.st_size
;
473 edit_load_file_fast (edit
, edit
->filename
);
474 /* If fast load was used, the number of lines wasn't calculated */
475 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
482 edit
->stack_disable
= 1;
483 if (!edit_insert_file (edit
, edit
->filename
))
488 edit
->stack_disable
= 0;
495 /* --------------------------------------------------------------------------------------------- */
496 /** Restore saved cursor position in the file */
499 edit_load_position (WEdit
* edit
)
505 if (!edit
->filename
|| !*edit
->filename
)
508 filename
= vfs_canon (edit
->filename
);
509 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
514 edit_move_to_line (edit
, line
- 1);
515 edit
->prev_col
= column
;
519 edit_cursor_move (edit
, offset
);
520 line
= edit
->curs_line
;
521 edit
->search_start
= edit
->curs1
;
524 book_mark_restore (edit
, BOOK_MARK_COLOR
);
526 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
527 edit_move_display (edit
, line
- (edit
->widget
.lines
/ 2));
530 /* --------------------------------------------------------------------------------------------- */
531 /** Save cursor position in the file */
534 edit_save_position (WEdit
* edit
)
538 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
541 filename
= vfs_canon (edit
->filename
);
543 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
544 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
545 edit
->serialized_bookmarks
);
546 edit
->serialized_bookmarks
= NULL
;
551 /* --------------------------------------------------------------------------------------------- */
552 /** Clean the WEdit stricture except the widget part */
555 edit_purge_widget (WEdit
* edit
)
557 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
558 char *start
= (char *) edit
+ sizeof (Widget
);
559 memset (start
, 0, len
);
560 edit
->macro_i
= -1; /* not recording a macro */
563 /* --------------------------------------------------------------------------------------------- */
566 edit_set_keymap (void)
568 editor_map
= default_editor_keymap
;
569 if (editor_keymap
&& editor_keymap
->len
> 0)
570 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
572 editor_x_map
= default_editor_x_keymap
;
573 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
574 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
577 /* --------------------------------------------------------------------------------------------- */
579 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
580 then the file should be as it was when he loaded up. Then set edit->modified to 0.
584 pop_action (WEdit
* edit
)
587 unsigned long sp
= edit
->stack_pointer
;
589 if (sp
== edit
->stack_bottom
)
592 sp
= (sp
- 1) & edit
->stack_size_mask
;
593 c
= edit
->undo_stack
[sp
];
596 /* edit->undo_stack[sp] = '@'; */
597 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
601 if (sp
== edit
->stack_bottom
)
604 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
605 if (edit
->undo_stack
[sp
] == -2)
607 /* edit->undo_stack[sp] = '@'; */
608 edit
->stack_pointer
= sp
;
611 edit
->undo_stack
[sp
]++;
616 /* --------------------------------------------------------------------------------------------- */
617 /** is called whenever a modification is made by one of the four routines below */
620 edit_modification (WEdit
* edit
)
622 edit
->caches_valid
= 0;
624 /* raise lock when file modified */
625 if (!edit
->modified
&& !edit
->delete_file
)
626 edit
->locked
= edit_lock_file (edit
);
630 /* --------------------------------------------------------------------------------------------- */
633 edit_insert_over (WEdit
* edit
)
637 for (i
= 0; i
< edit
->over_col
; i
++)
639 edit_insert (edit
, ' ');
644 /* --------------------------------------------------------------------------------------------- */
647 edit_backspace (WEdit
* edit
, const int byte_delete
)
658 if (edit
->mark2
!= edit
->mark1
)
659 edit_push_markers (edit
);
661 if (edit
->utf8
&& byte_delete
== 0)
663 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
667 for (i
= 1; i
<= cw
; i
++)
669 if (edit
->mark1
>= edit
->curs1
)
672 edit
->end_mark_curs
--;
674 if (edit
->mark2
>= edit
->curs1
)
676 if (edit
->last_get_rule
>= edit
->curs1
)
677 edit
->last_get_rule
--;
679 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
680 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
681 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
683 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
684 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
688 edit_push_action (edit
, p
);
690 edit_modification (edit
);
694 book_mark_dec (edit
, edit
->curs_line
);
697 edit
->force
|= REDRAW_AFTER_CURSOR
;
700 if (edit
->curs1
< edit
->start_display
)
702 edit
->start_display
--;
711 /* --------------------------------------------------------------------------------------------- */
713 #ifdef FAST_MOVE_CURSOR
715 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
718 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
721 next
-= (unsigned long) dest
;
727 #endif /* FAST_MOVE_CURSOR */
729 /* --------------------------------------------------------------------------------------------- */
730 /* high level cursor movement commands */
731 /* --------------------------------------------------------------------------------------------- */
734 is_in_indent (WEdit
* edit
)
736 long p
= edit_bol (edit
, edit
->curs1
);
737 while (p
< edit
->curs1
)
738 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
743 /* --------------------------------------------------------------------------------------------- */
746 is_blank (WEdit
* edit
, long offset
)
750 s
= edit_bol (edit
, offset
);
751 f
= edit_eol (edit
, offset
) - 1;
754 c
= edit_get_byte (edit
, s
++);
762 /* --------------------------------------------------------------------------------------------- */
763 /** returns the offset of line i */
766 edit_find_line (WEdit
* edit
, int line
)
770 if (!edit
->caches_valid
)
772 for (i
= 0; i
< N_LINE_CACHES
; i
++)
773 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
774 /* three offsets that we *know* are line 0 at 0 and these two: */
775 edit
->line_numbers
[1] = edit
->curs_line
;
776 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
777 edit
->line_numbers
[2] = edit
->total_lines
;
778 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
779 edit
->caches_valid
= 1;
781 if (line
>= edit
->total_lines
)
782 return edit
->line_offsets
[2];
785 /* find the closest known point */
786 for (i
= 0; i
< N_LINE_CACHES
; i
++)
789 n
= abs (edit
->line_numbers
[i
] - line
);
797 return edit
->line_offsets
[j
]; /* know the offset exactly */
798 if (m
== 1 && j
>= 3)
799 i
= j
; /* one line different - caller might be looping, so stay in this cache */
801 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
802 if (line
> edit
->line_numbers
[j
])
803 edit
->line_offsets
[i
] =
804 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
806 edit
->line_offsets
[i
] =
807 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
808 edit
->line_numbers
[i
] = line
;
809 return edit
->line_offsets
[i
];
812 /* --------------------------------------------------------------------------------------------- */
813 /** moves up until a blank line is reached, or until just
814 before a non-blank line is reached */
817 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
820 if (edit
->curs_line
> 1)
822 if (line_is_blank (edit
, edit
->curs_line
))
824 if (line_is_blank (edit
, edit
->curs_line
- 1))
826 for (i
= edit
->curs_line
- 1; i
; i
--)
827 if (!line_is_blank (edit
, i
))
835 for (i
= edit
->curs_line
- 1; i
; i
--)
836 if (line_is_blank (edit
, i
))
842 for (i
= edit
->curs_line
- 1; i
; i
--)
843 if (line_is_blank (edit
, i
))
847 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
850 /* --------------------------------------------------------------------------------------------- */
851 /** moves down until a blank line is reached, or until just
852 before a non-blank line is reached */
855 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
858 if (edit
->curs_line
>= edit
->total_lines
- 1)
860 i
= edit
->total_lines
;
864 if (line_is_blank (edit
, edit
->curs_line
))
866 if (line_is_blank (edit
, edit
->curs_line
+ 1))
868 for (i
= edit
->curs_line
+ 1; i
; i
++)
869 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
877 for (i
= edit
->curs_line
+ 1; i
; i
++)
878 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
884 for (i
= edit
->curs_line
+ 1; i
; i
++)
885 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
889 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
892 /* --------------------------------------------------------------------------------------------- */
895 edit_begin_page (WEdit
* edit
)
897 edit_update_curs_row (edit
);
898 edit_move_up (edit
, edit
->curs_row
, 0);
901 /* --------------------------------------------------------------------------------------------- */
904 edit_end_page (WEdit
* edit
)
906 edit_update_curs_row (edit
);
907 edit_move_down (edit
, edit
->widget
.lines
- edit
->curs_row
- 1, 0);
911 /* --------------------------------------------------------------------------------------------- */
912 /** goto beginning of text */
915 edit_move_to_top (WEdit
* edit
)
919 edit_cursor_move (edit
, -edit
->curs1
);
920 edit_move_to_prev_col (edit
, 0);
921 edit
->force
|= REDRAW_PAGE
;
922 edit
->search_start
= 0;
923 edit_update_curs_row (edit
);
928 /* --------------------------------------------------------------------------------------------- */
929 /** goto end of text */
932 edit_move_to_bottom (WEdit
* edit
)
934 if (edit
->curs_line
< edit
->total_lines
)
936 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
937 edit
->start_display
= edit
->last_byte
;
938 edit
->start_line
= edit
->total_lines
;
939 edit_scroll_upward (edit
, edit
->widget
.lines
- 1);
940 edit
->force
|= REDRAW_PAGE
;
944 /* --------------------------------------------------------------------------------------------- */
945 /** goto beginning of line */
948 edit_cursor_to_bol (WEdit
* edit
)
950 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
951 edit
->search_start
= edit
->curs1
;
952 edit
->prev_col
= edit_get_col (edit
);
956 /* --------------------------------------------------------------------------------------------- */
957 /** goto end of line */
960 edit_cursor_to_eol (WEdit
* edit
)
962 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
963 edit
->search_start
= edit
->curs1
;
964 edit
->prev_col
= edit_get_col (edit
);
968 /* --------------------------------------------------------------------------------------------- */
975 const char option_chars_move_whole_word
[] =
976 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
982 if (*option_chars_move_whole_word
== '!')
986 if (g_ascii_isupper ((gchar
) c
))
988 else if (g_ascii_islower ((gchar
) c
))
990 else if (g_ascii_isalpha (c
))
992 else if (isdigit (c
))
994 else if (isspace (c
))
996 q
= strchr (option_chars_move_whole_word
, c
);
1001 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1006 while ((q
= strchr (q
+ 1, c
)));
1010 /* --------------------------------------------------------------------------------------------- */
1013 edit_left_word_move (WEdit
* edit
, int s
)
1018 if (edit
->column_highlight
1019 && edit
->mark1
!= edit
->mark2
1020 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1022 edit_cursor_move (edit
, -1);
1025 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1026 c2
= edit_get_byte (edit
, edit
->curs1
);
1027 if (c1
== '\n' || c2
== '\n')
1029 if (!(my_type_of (c1
) & my_type_of (c2
)))
1031 if (isspace (c1
) && !isspace (c2
))
1034 if (!isspace (c1
) && isspace (c2
))
1039 /* --------------------------------------------------------------------------------------------- */
1042 edit_left_word_move_cmd (WEdit
* edit
)
1044 edit_left_word_move (edit
, 0);
1045 edit
->force
|= REDRAW_PAGE
;
1048 /* --------------------------------------------------------------------------------------------- */
1051 edit_right_word_move (WEdit
* edit
, int s
)
1056 if (edit
->column_highlight
1057 && edit
->mark1
!= edit
->mark2
1058 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1060 edit_cursor_move (edit
, 1);
1061 if (edit
->curs1
>= edit
->last_byte
)
1063 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1064 c2
= edit_get_byte (edit
, edit
->curs1
);
1065 if (c1
== '\n' || c2
== '\n')
1067 if (!(my_type_of (c1
) & my_type_of (c2
)))
1069 if (isspace (c1
) && !isspace (c2
))
1072 if (!isspace (c1
) && isspace (c2
))
1077 /* --------------------------------------------------------------------------------------------- */
1080 edit_right_word_move_cmd (WEdit
* edit
)
1082 edit_right_word_move (edit
, 0);
1083 edit
->force
|= REDRAW_PAGE
;
1086 /* --------------------------------------------------------------------------------------------- */
1089 edit_right_char_move_cmd (WEdit
* edit
)
1095 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1101 c
= edit_get_byte (edit
, edit
->curs1
);
1103 if (option_cursor_beyond_eol
&& c
== '\n')
1109 edit_cursor_move (edit
, cw
);
1113 /* --------------------------------------------------------------------------------------------- */
1116 edit_left_char_move_cmd (WEdit
* edit
)
1119 if (edit
->column_highlight
1120 && option_cursor_beyond_eol
1121 && edit
->mark1
!= edit
->mark2
1122 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1126 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1130 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1136 edit_cursor_move (edit
, -cw
);
1140 /* --------------------------------------------------------------------------------------------- */
1141 /** Up or down cursor moving.
1142 direction = TRUE - move up
1147 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1150 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1159 edit
->force
|= REDRAW_PAGE
;
1163 edit_scroll_upward (edit
, i
);
1165 edit_scroll_downward (edit
, i
);
1167 p
= edit_bol (edit
, edit
->curs1
);
1169 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1171 edit_cursor_move (edit
, p
- edit
->curs1
);
1173 edit_move_to_prev_col (edit
, p
);
1175 /* search start of current multibyte char (like CJK) */
1176 if (edit
->curs1
+ 1 < edit
->last_byte
)
1178 edit_right_char_move_cmd (edit
);
1179 edit_left_char_move_cmd (edit
);
1182 edit
->search_start
= edit
->curs1
;
1183 edit
->found_len
= 0;
1186 /* --------------------------------------------------------------------------------------------- */
1189 edit_right_delete_word (WEdit
* edit
)
1194 if (edit
->curs1
>= edit
->last_byte
)
1196 c1
= edit_delete (edit
, 1);
1197 c2
= edit_get_byte (edit
, edit
->curs1
);
1198 if (c1
== '\n' || c2
== '\n')
1200 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1202 if (!(my_type_of (c1
) & my_type_of (c2
)))
1207 /* --------------------------------------------------------------------------------------------- */
1210 edit_left_delete_word (WEdit
* edit
)
1215 if (edit
->curs1
<= 0)
1217 c1
= edit_backspace (edit
, 1);
1218 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1219 if (c1
== '\n' || c2
== '\n')
1221 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1223 if (!(my_type_of (c1
) & my_type_of (c2
)))
1228 /* --------------------------------------------------------------------------------------------- */
1230 the start column position is not recorded, and hence does not
1231 undo as it happed. But who would notice.
1235 edit_do_undo (WEdit
* edit
)
1240 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1242 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
1249 edit_cursor_move (edit
, 1);
1252 edit_cursor_move (edit
, -1);
1255 edit_backspace (edit
, 1);
1258 edit_delete (edit
, 1);
1261 edit
->column_highlight
= 1;
1264 edit
->column_highlight
= 0;
1267 if (ac
>= 256 && ac
< 512)
1268 edit_insert_ahead (edit
, ac
- 256);
1269 if (ac
>= 0 && ac
< 256)
1270 edit_insert (edit
, ac
);
1272 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1274 edit
->mark1
= ac
- MARK_1
;
1275 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1277 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1279 edit
->mark2
= ac
- MARK_2
;
1280 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1282 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1284 edit
->end_mark_curs
= ac
- MARK_CURS
;
1287 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1290 if (edit
->start_display
> ac
- KEY_PRESS
)
1292 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1293 edit
->force
|= REDRAW_PAGE
;
1295 else if (edit
->start_display
< ac
- KEY_PRESS
)
1297 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1298 edit
->force
|= REDRAW_PAGE
;
1300 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1301 edit_update_curs_row (edit
);
1304 edit
->stack_disable
= 0;
1307 /* --------------------------------------------------------------------------------------------- */
1310 edit_delete_to_line_end (WEdit
* edit
)
1312 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1316 edit_delete (edit
, 1);
1320 /* --------------------------------------------------------------------------------------------- */
1323 edit_delete_to_line_begin (WEdit
* edit
)
1325 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1329 edit_backspace (edit
, 1);
1333 /* --------------------------------------------------------------------------------------------- */
1336 is_aligned_on_a_tab (WEdit
* edit
)
1338 edit_update_curs_col (edit
);
1339 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1340 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1343 /* --------------------------------------------------------------------------------------------- */
1346 right_of_four_spaces (WEdit
* edit
)
1349 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1350 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1352 return is_aligned_on_a_tab (edit
);
1356 /* --------------------------------------------------------------------------------------------- */
1359 left_of_four_spaces (WEdit
* edit
)
1362 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1363 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1365 return is_aligned_on_a_tab (edit
);
1369 /* --------------------------------------------------------------------------------------------- */
1372 edit_auto_indent (WEdit
* edit
)
1377 /* use the previous line as a template */
1378 p
= edit_move_backward (edit
, p
, 1);
1379 /* copy the leading whitespace of the line */
1381 { /* no range check - the line _is_ \n-terminated */
1382 c
= edit_get_byte (edit
, p
++);
1383 if (c
!= ' ' && c
!= '\t')
1385 edit_insert (edit
, c
);
1389 /* --------------------------------------------------------------------------------------------- */
1392 edit_double_newline (WEdit
* edit
)
1394 edit_insert (edit
, '\n');
1395 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1397 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1399 edit
->force
|= REDRAW_PAGE
;
1400 edit_insert (edit
, '\n');
1403 /* --------------------------------------------------------------------------------------------- */
1406 edit_tab_cmd (WEdit
* edit
)
1410 if (option_fake_half_tabs
)
1412 if (is_in_indent (edit
))
1414 /*insert a half tab (usually four spaces) unless there is a
1415 half tab already behind, then delete it and insert a
1417 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
1419 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1420 edit_backspace (edit
, 1);
1421 edit_insert (edit
, '\t');
1425 insert_spaces_tab (edit
, 1);
1430 if (option_fill_tabs_with_spaces
)
1432 insert_spaces_tab (edit
, 0);
1436 edit_insert (edit
, '\t');
1440 /* --------------------------------------------------------------------------------------------- */
1443 check_and_wrap_line (WEdit
* edit
)
1446 if (!option_typewriter_wrap
)
1448 edit_update_curs_col (edit
);
1449 if (edit
->curs_col
< option_word_wrap_line_length
)
1455 c
= edit_get_byte (edit
, curs
);
1456 if (c
== '\n' || curs
<= 0)
1458 edit_insert (edit
, '\n');
1461 if (c
== ' ' || c
== '\t')
1463 int current
= edit
->curs1
;
1464 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1465 edit_insert (edit
, '\n');
1466 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1472 /* --------------------------------------------------------------------------------------------- */
1473 /** this find the matching bracket in either direction, and sets edit->bracket */
1476 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1478 const char *const b
= "{}{[][()(", *p
;
1479 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1480 unsigned long j
= 0;
1482 edit_update_curs_row (edit
);
1483 c
= edit_get_byte (edit
, edit
->curs1
);
1486 if (!furthest_bracket_search
)
1487 furthest_bracket_search
--;
1488 /* not on a bracket at all */
1491 /* the matching bracket */
1493 /* going left or right? */
1494 if (strchr ("{[(", c
))
1496 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1498 /* out of buffer? */
1499 if (q
>= edit
->last_byte
|| q
< 0)
1501 a
= edit_get_byte (edit
, q
);
1502 /* don't want to eat CPU */
1503 if (j
++ > furthest_bracket_search
)
1505 /* out of screen? */
1508 if (q
< edit
->start_display
)
1510 /* count lines if searching downward */
1511 if (inc
> 0 && a
== '\n')
1512 if (n
++ >= edit
->widget
.lines
- edit
->curs_row
) /* out of screen */
1515 /* count bracket depth */
1516 i
+= (a
== c
) - (a
== d
);
1517 /* return if bracket depth is zero */
1525 /* --------------------------------------------------------------------------------------------- */
1528 edit_goto_matching_bracket (WEdit
* edit
)
1532 q
= edit_get_bracket (edit
, 0, 0);
1535 edit
->bracket
= edit
->curs1
;
1536 edit
->force
|= REDRAW_PAGE
;
1537 edit_cursor_move (edit
, q
- edit
->curs1
);
1541 /* --------------------------------------------------------------------------------------------- */
1544 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
1548 if (edit
->macro_depth
++ > 256)
1550 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
1551 edit
->macro_depth
--;
1554 edit
->force
|= REDRAW_PAGE
;
1557 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
1559 edit_update_screen (edit
);
1560 edit
->macro_depth
--;
1563 /* --------------------------------------------------------------------------------------------- */
1564 /** User edit menu, like user menu (F2) but only in editor. */
1567 user_menu (WEdit
* edit
)
1571 long start_mark
, end_mark
;
1574 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
1576 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1578 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1580 /* run shell scripts from menu */
1581 user_menu_cmd (edit
);
1583 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
1590 /* i.e. we have marked block */
1591 rc
= edit_block_delete_cmd (edit
);
1595 edit_insert_file (edit
, block_file
);
1597 /* truncate block file */
1598 fd
= fopen (block_file
, "w");
1602 edit_refresh_cmd (edit
);
1603 edit
->force
|= REDRAW_COMPLETELY
;
1605 g_free (block_file
);
1608 /* --------------------------------------------------------------------------------------------- */
1609 /*** public functions ****************************************************************************/
1610 /* --------------------------------------------------------------------------------------------- */
1613 edit_get_byte (WEdit
* edit
, long byte_index
)
1616 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1619 if (byte_index
>= edit
->curs1
)
1621 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1622 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1626 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1630 /* --------------------------------------------------------------------------------------------- */
1633 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
1636 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1639 if (byte_index
>= edit
->curs1
)
1641 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1642 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1643 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1647 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
1648 (byte_index
& M_EDIT_BUF_SIZE
));
1652 /* --------------------------------------------------------------------------------------------- */
1655 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
1659 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
1665 if (byte_index
>= edit
->curs1
)
1667 p
= edit
->curs1
+ edit
->curs2
- 1;
1668 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1669 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1673 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
1677 /* --------------------------------------------------------------------------------------------- */
1680 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1685 gchar
*next_ch
= NULL
;
1688 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1694 str
= edit_get_byte_ptr (edit
, byte_index
);
1702 res
= g_utf8_get_char_validated (str
, -1);
1712 /* Calculate UTF-8 char width */
1713 next_ch
= g_utf8_next_char (str
);
1716 width
= next_ch
- str
;
1724 *char_width
= width
;
1728 /* --------------------------------------------------------------------------------------------- */
1731 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1733 gchar
*str
, *buf
= NULL
;
1736 gchar
*next_ch
= NULL
;
1742 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1748 ch
= edit_get_utf (edit
, byte_index
, &width
);
1752 *char_width
= width
;
1756 str
= edit_get_byte_ptr (edit
, byte_index
);
1757 buf
= edit_get_buf_ptr (edit
, byte_index
);
1758 if (str
== NULL
|| buf
== NULL
)
1763 /* get prev utf8 char */
1765 str
= g_utf8_find_prev_char (buf
, str
);
1773 res
= g_utf8_get_char_validated (str
, -1);
1783 /* Calculate UTF-8 char width */
1784 next_ch
= g_utf8_next_char (str
);
1787 width
= next_ch
- str
;
1795 *char_width
= width
;
1799 /* --------------------------------------------------------------------------------------------- */
1802 edit_get_write_filter (const char *write_name
, const char *filename
)
1805 char *p
, *writename
;
1807 i
= edit_find_filter (filename
);
1811 writename
= name_quote (write_name
, 0);
1812 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1817 /* --------------------------------------------------------------------------------------------- */
1820 edit_write_stream (WEdit
* edit
, FILE * f
)
1824 if (edit
->lb
== LB_ASIS
)
1826 for (i
= 0; i
< edit
->last_byte
; i
++)
1827 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1832 /* change line breaks */
1833 for (i
= 0; i
< edit
->last_byte
; i
++)
1835 unsigned char c
= edit_get_byte (edit
, i
);
1837 if (!(c
== '\n' || c
== '\r'))
1839 /* not line break */
1840 if (fputc (c
, f
) < 0)
1844 { /* (c == '\n' || c == '\r') */
1845 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1849 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1850 /* put one line break unconditionally */
1851 if (fputc ('\n', f
) < 0)
1854 i
++; /* 2 chars are processed */
1856 if (c
== '\r' && c1
== '\n')
1857 /* Windows line break; go to the next char */
1860 if (c
== '\r' && c1
== '\r')
1862 /* two Macintosh line breaks; put second line break */
1863 if (fputc ('\n', f
) < 0)
1868 if (fputc (c1
, f
) < 0)
1872 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1873 /* put one line break unconditionally */
1874 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1877 if (c
== '\r' && c1
== '\n')
1878 /* Windows line break; go to the next char */
1882 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1883 /* put one line break unconditionally */
1884 if (fputc ('\r', f
) < 0)
1887 i
++; /* 2 chars are processed */
1889 if (c
== '\r' && c1
== '\n')
1890 /* Windows line break; go to the next char */
1893 if (c
== '\n' && c1
== '\n')
1895 /* two Windows line breaks; put second line break */
1896 if (fputc ('\r', f
) < 0)
1901 if (fputc (c1
, f
) < 0)
1904 case LB_ASIS
: /* default without changes */
1910 return edit
->last_byte
;
1913 /* --------------------------------------------------------------------------------------------- */
1914 /** inserts a file at the cursor, returns 1 on success */
1916 edit_insert_file (WEdit
* edit
, const char *filename
)
1920 p
= edit_get_filter (filename
);
1924 long current
= edit
->curs1
;
1925 f
= (FILE *) popen (p
, "r");
1928 edit_insert_stream (edit
, f
);
1929 edit_cursor_move (edit
, current
- edit
->curs1
);
1933 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
1934 edit_error_dialog (_("Error"), errmsg
);
1943 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
1944 edit_error_dialog (_("Error"), errmsg
);
1953 int i
, file
, blocklen
;
1954 long current
= edit
->curs1
;
1955 int vertical_insertion
= 0;
1957 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
1960 buf
= g_malloc0 (TEMP_BUF_LEN
);
1961 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
1964 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
1965 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
1966 vertical_insertion
= 1;
1968 mc_lseek (file
, 0, SEEK_SET
);
1970 if (vertical_insertion
)
1971 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
1973 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
1974 for (i
= 0; i
< blocklen
; i
++)
1975 edit_insert (edit
, buf
[i
]);
1976 edit_cursor_move (edit
, current
- edit
->curs1
);
1985 /* --------------------------------------------------------------------------------------------- */
1987 * Fill in the edit structure. Return NULL on failure. Pass edit as
1988 * NULL to allocate a new structure.
1990 * If line is 0, try to restore saved position. Otherwise put the
1991 * cursor on that line and show it in the middle of the screen.
1995 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
1998 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
1999 if (option_line_state
)
2001 option_line_state_width
= LINE_STATE_WIDTH
;
2005 option_line_state_width
= 0;
2011 * Expand option_whole_chars_search by national letters using
2015 static char option_whole_chars_search_buf
[256];
2017 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2020 size_t len
= str_term_width1 (option_whole_chars_search
);
2022 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2024 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2026 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2028 option_whole_chars_search_buf
[len
++] = i
;
2032 option_whole_chars_search_buf
[len
] = 0;
2033 option_whole_chars_search
= option_whole_chars_search_buf
;
2035 #endif /* ENABLE_NLS */
2036 edit
= g_malloc0 (sizeof (WEdit
));
2037 edit
->search
= NULL
;
2040 edit_purge_widget (edit
);
2041 edit
->widget
.lines
= lines
;
2043 edit
->widget
.cols
= columns
;
2044 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2045 edit
->stat1
.st_uid
= getuid ();
2046 edit
->stat1
.st_gid
= getgid ();
2047 edit
->stat1
.st_mtime
= 0;
2049 edit
->force
|= REDRAW_PAGE
;
2050 edit_set_filename (edit
, filename
);
2051 edit
->stack_size
= START_STACK_SIZE
;
2052 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
2053 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
2056 edit
->converter
= str_cnv_from_term
;
2057 edit_set_codeset (edit
);
2059 if (edit_load_file (edit
))
2061 /* edit_load_file already gives an error message */
2067 edit
->loading_done
= 1;
2070 edit_load_syntax (edit
, NULL
, NULL
);
2073 edit_get_syntax_color (edit
, -1, &color
);
2076 /* load saved cursor position */
2077 if ((line
== 0) && option_save_position
)
2079 edit_load_position (edit
);
2085 edit_move_display (edit
, line
- 1);
2086 edit_move_to_line (edit
, line
- 1);
2094 /* --------------------------------------------------------------------------------------------- */
2095 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2098 edit_clean (WEdit
* edit
)
2105 /* a stale lock, remove it */
2107 edit
->locked
= edit_unlock_file (edit
);
2109 /* save cursor position */
2110 if (option_save_position
)
2111 edit_save_position (edit
);
2112 else if (edit
->serialized_bookmarks
!= NULL
)
2113 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2115 /* File specified on the mcedit command line and never saved */
2116 if (edit
->delete_file
)
2117 unlink (edit
->filename
);
2119 edit_free_syntax_rules (edit
);
2120 book_mark_flush (edit
, -1);
2121 for (; j
<= MAXBUFF
; j
++)
2123 g_free (edit
->buffers1
[j
]);
2124 g_free (edit
->buffers2
[j
]);
2127 g_free (edit
->undo_stack
);
2128 g_free (edit
->filename
);
2131 mc_search_free (edit
->search
);
2132 edit
->search
= NULL
;
2134 if (edit
->converter
!= str_cnv_from_term
)
2135 str_close_conv (edit
->converter
);
2137 edit_purge_widget (edit
);
2142 /* --------------------------------------------------------------------------------------------- */
2143 /** returns 1 on success */
2146 edit_renew (WEdit
* edit
)
2148 int lines
= edit
->widget
.lines
;
2149 int columns
= edit
->widget
.cols
;
2152 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
2155 /* --------------------------------------------------------------------------------------------- */
2157 * Load a new file into the editor. If it fails, preserve the old file.
2158 * To do it, allocate a new widget, initialize it and, if the new file
2159 * was loaded, copy the data to the old widget.
2160 * Return 1 on success, 0 on failure.
2164 edit_reload (WEdit
* edit
, const char *filename
)
2167 int lines
= edit
->widget
.lines
;
2168 int columns
= edit
->widget
.cols
;
2170 e
= g_malloc0 (sizeof (WEdit
));
2171 e
->widget
= edit
->widget
;
2172 if (!edit_init (e
, lines
, columns
, filename
, 0))
2178 memcpy (edit
, e
, sizeof (WEdit
));
2183 /* --------------------------------------------------------------------------------------------- */
2185 * Load a new file into the editor and set line. If it fails, preserve the old file.
2186 * To do it, allocate a new widget, initialize it and, if the new file
2187 * was loaded, copy the data to the old widget.
2188 * Return 1 on success, 0 on failure.
2192 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2195 int lines
= edit
->widget
.lines
;
2196 int columns
= edit
->widget
.cols
;
2198 e
= g_malloc0 (sizeof (WEdit
));
2199 e
->widget
= edit
->widget
;
2200 if (!edit_init (e
, lines
, columns
, filename
, line
))
2206 memcpy (edit
, e
, sizeof (WEdit
));
2211 /* --------------------------------------------------------------------------------------------- */
2214 edit_set_codeset (WEdit
* edit
)
2219 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
2224 conv
= str_crt_conv_from (cp_id
);
2225 if (conv
!= INVALID_CONV
)
2227 if (edit
->converter
!= str_cnv_from_term
)
2228 str_close_conv (edit
->converter
);
2229 edit
->converter
= conv
;
2234 edit
->utf8
= str_isutf8 (cp_id
);
2241 /* --------------------------------------------------------------------------------------------- */
2243 Recording stack for undo:
2244 The following is an implementation of a compressed stack. Identical
2245 pushes are recorded by a negative prefix indicating the number of times the
2246 same char was pushed. This saves space for repeated curs-left or curs-right
2263 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2264 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2265 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2266 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2269 The only way the cursor moves or the buffer is changed is through the routines:
2270 insert, backspace, insert_ahead, delete, and cursor_move.
2271 These record the reverse undo movements onto the stack each time they are
2274 Each key press results in a set of actions (insert; delete ...). So each time
2275 a key is pressed the current position of start_display is pushed as
2276 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2277 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2278 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2283 edit_push_action (WEdit
* edit
, long c
, ...)
2285 unsigned long sp
= edit
->stack_pointer
;
2289 /* first enlarge the stack if necessary */
2290 if (sp
> edit
->stack_size
- 10)
2292 if (option_max_undo
< 256)
2293 option_max_undo
= 256;
2294 if (edit
->stack_size
< (unsigned long) option_max_undo
)
2296 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
2299 edit
->undo_stack
= t
;
2300 edit
->stack_size
<<= 1;
2301 edit
->stack_size_mask
= edit
->stack_size
- 1;
2305 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
2306 if (edit
->stack_disable
)
2309 #ifdef FAST_MOVE_CURSOR
2310 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
2313 edit
->undo_stack
[sp
] = (c
== CURS_LEFT_LOTS
) ? CURS_LEFT
: CURS_RIGHT
;
2314 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
2316 c
= -(va_arg (ap
, int));
2320 #endif /* ! FAST_MOVE_CURSOR */
2321 if (edit
->stack_bottom
!= sp
2322 && spm1
!= edit
->stack_bottom
2323 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
2326 if (edit
->undo_stack
[spm1
] < 0)
2328 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
2331 if (edit
->undo_stack
[spm1
] > -1000000000)
2333 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2334 edit
->undo_stack
[spm1
]--;
2338 /* #define NO_STACK_CURSMOVE_ANIHILATION */
2339 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2340 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
2341 { /* a left then a right anihilate each other */
2342 if (edit
->undo_stack
[spm1
] == -2)
2343 edit
->stack_pointer
= spm1
;
2345 edit
->undo_stack
[spm1
]++;
2352 d
= edit
->undo_stack
[spm1
];
2356 return; /* --> no need to push multiple do-nothings */
2357 edit
->undo_stack
[sp
] = -2;
2360 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2361 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
2362 { /* a left then a right anihilate each other */
2363 edit
->stack_pointer
= spm1
;
2369 edit
->undo_stack
[sp
] = c
;
2372 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
2374 /* if the sp wraps round and catches the stack_bottom then erase
2375 * the first set of actions on the stack to make space - by moving
2376 * stack_bottom forward one "key press" */
2377 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
2378 if ((unsigned long) c
== edit
->stack_bottom
||
2379 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
2382 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
2384 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
2385 && edit
->stack_bottom
!= edit
->stack_pointer
);
2387 /*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: */
2388 if (edit
->stack_pointer
!= edit
->stack_bottom
2389 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
2390 edit
->stack_bottom
= edit
->stack_pointer
= 0;
2393 /* --------------------------------------------------------------------------------------------- */
2395 Basic low level single character buffer alterations and movements at the cursor.
2396 Returns char passed over, inserted or removed.
2400 edit_insert (WEdit
* edit
, int c
)
2402 /* check if file has grown to large */
2403 if (edit
->last_byte
>= SIZE_LIMIT
)
2406 /* first we must update the position of the display window */
2407 if (edit
->curs1
< edit
->start_display
)
2409 edit
->start_display
++;
2414 /* Mark file as modified, unless the file hasn't been fully loaded */
2415 if (edit
->loading_done
)
2417 edit_modification (edit
);
2420 /* now we must update some info on the file and check if a redraw is required */
2423 if (edit
->book_mark
)
2424 book_mark_inc (edit
, edit
->curs_line
);
2426 edit
->total_lines
++;
2427 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2430 /* save the reverse command onto the undo stack */
2431 edit_push_action (edit
, BACKSPACE
);
2433 /* update markers */
2434 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2435 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2436 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2438 /* add a new buffer if we've reached the end of the last one */
2439 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2440 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2442 /* perform the insertion */
2443 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2444 = (unsigned char) c
;
2446 /* update file length */
2449 /* update cursor position */
2453 /* --------------------------------------------------------------------------------------------- */
2454 /** same as edit_insert and move left */
2457 edit_insert_ahead (WEdit
* edit
, int c
)
2459 if (edit
->last_byte
>= SIZE_LIMIT
)
2462 if (edit
->curs1
< edit
->start_display
)
2464 edit
->start_display
++;
2468 edit_modification (edit
);
2471 if (edit
->book_mark
)
2472 book_mark_inc (edit
, edit
->curs_line
);
2473 edit
->total_lines
++;
2474 edit
->force
|= REDRAW_AFTER_CURSOR
;
2476 edit_push_action (edit
, DELCHAR
);
2478 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2479 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2480 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2482 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2483 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2484 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2485 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2492 /* --------------------------------------------------------------------------------------------- */
2495 edit_delete (WEdit
* edit
, const int byte_delete
)
2505 /* if byte_delete = 1 then delete only one byte not multibyte char */
2506 if (edit
->utf8
&& byte_delete
== 0)
2508 edit_get_utf (edit
, edit
->curs1
, &cw
);
2513 if (edit
->mark2
!= edit
->mark1
)
2514 edit_push_markers (edit
);
2516 for (i
= 1; i
<= cw
; i
++)
2518 if (edit
->mark1
> edit
->curs1
)
2521 edit
->end_mark_curs
--;
2523 if (edit
->mark2
> edit
->curs1
)
2525 if (edit
->last_get_rule
> edit
->curs1
)
2526 edit
->last_get_rule
--;
2528 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2530 1) & M_EDIT_BUF_SIZE
) - 1];
2532 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2534 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2535 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2539 edit_push_action (edit
, p
+ 256);
2542 edit_modification (edit
);
2545 if (edit
->book_mark
)
2546 book_mark_dec (edit
, edit
->curs_line
);
2547 edit
->total_lines
--;
2548 edit
->force
|= REDRAW_AFTER_CURSOR
;
2550 if (edit
->curs1
< edit
->start_display
)
2552 edit
->start_display
--;
2560 /* --------------------------------------------------------------------------------------------- */
2562 #ifdef FAST_MOVE_CURSOR
2564 edit_move_backward_lots (WEdit
* edit
, long increment
)
2567 unsigned char *p
= NULL
;
2569 if (increment
> edit
->curs1
)
2570 increment
= edit
->curs1
;
2573 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
2575 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
2578 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
2583 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
2584 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
2591 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
2592 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
2593 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
2594 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
2597 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
2598 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
2599 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
2604 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2607 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
2609 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2616 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
2627 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
2628 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
2633 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
2634 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
2637 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
2639 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
2640 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
2645 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2648 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
2650 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2655 return edit_get_byte (edit
, edit
->curs1
);
2657 #endif /* ! FAST_MOVE_CURSOR */
2659 /* --------------------------------------------------------------------------------------------- */
2660 /** moves the cursor right or left: increment positive or negative respectively */
2663 edit_cursor_move (WEdit
* edit
, long increment
)
2665 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2667 #ifdef FAST_MOVE_CURSOR
2668 if (increment
< -256)
2670 edit
->force
|= REDRAW_PAGE
;
2671 edit_move_backward_lots (edit
, -increment
);
2674 #endif /* ! FAST_MOVE_CURSOR */
2678 for (; increment
< 0; increment
++)
2683 edit_push_action (edit
, CURS_RIGHT
);
2685 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2686 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2687 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2688 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2689 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2691 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2692 1) & M_EDIT_BUF_SIZE
];
2693 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2695 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2696 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2702 edit
->force
|= REDRAW_LINE_BELOW
;
2707 else if (increment
> 0)
2709 for (; increment
> 0; increment
--)
2714 edit_push_action (edit
, CURS_LEFT
);
2716 c
= edit_get_byte (edit
, edit
->curs1
);
2717 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2718 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2719 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2721 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2723 1) & M_EDIT_BUF_SIZE
) - 1];
2724 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2726 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2727 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2733 edit
->force
|= REDRAW_LINE_ABOVE
;
2739 /* These functions return positions relative to lines */
2741 /* --------------------------------------------------------------------------------------------- */
2742 /** returns index of last char on line + 1 */
2745 edit_eol (WEdit
* edit
, long current
)
2747 if (current
>= edit
->last_byte
)
2748 return edit
->last_byte
;
2751 if (edit_get_byte (edit
, current
) == '\n')
2756 /* --------------------------------------------------------------------------------------------- */
2757 /** returns index of first char on line */
2760 edit_bol (WEdit
* edit
, long current
)
2766 if (edit_get_byte (edit
, current
- 1) == '\n')
2771 /* --------------------------------------------------------------------------------------------- */
2774 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2777 if (upto
> edit
->last_byte
)
2778 upto
= edit
->last_byte
;
2781 while (current
< upto
)
2782 if (edit_get_byte (edit
, current
++) == '\n')
2787 /* --------------------------------------------------------------------------------------------- */
2788 /* If lines is zero this returns the count of lines from current to upto. */
2789 /* If upto is zero returns index of lines forward current. */
2792 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2796 return edit_count_lines (edit
, current
, upto
);
2805 next
= edit_eol (edit
, current
) + 1;
2806 if (next
> edit
->last_byte
)
2815 /* --------------------------------------------------------------------------------------------- */
2816 /** Returns offset of 'lines' lines up from current */
2819 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2823 current
= edit_bol (edit
, current
);
2824 while ((lines
--) && current
!= 0)
2825 current
= edit_bol (edit
, current
- 1);
2829 /* --------------------------------------------------------------------------------------------- */
2830 /* If cols is zero this returns the count of columns from current to upto. */
2831 /* If upto is zero returns index of cols across from current. */
2834 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
2845 q
= edit
->last_byte
+ 2;
2847 for (col
= 0, p
= current
; p
< q
; p
++)
2859 orig_c
= c
= edit_get_byte (edit
, p
);
2867 utf_ch
= edit_get_utf (edit
, p
, &cw
);
2872 if (g_unichar_iswide (utf_ch
))
2875 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2879 c
= convert_to_display_c (c
);
2883 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2891 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
2892 /* '\r' is shown as ^M, so we must advance 2 characters */
2893 /* Caret notation for control characters */
2901 /* --------------------------------------------------------------------------------------------- */
2902 /** returns the current column position of the cursor */
2905 edit_get_col (WEdit
* edit
)
2907 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
2910 /* --------------------------------------------------------------------------------------------- */
2911 /* Scrolling functions */
2912 /* --------------------------------------------------------------------------------------------- */
2915 edit_update_curs_row (WEdit
* edit
)
2917 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
2920 /* --------------------------------------------------------------------------------------------- */
2923 edit_update_curs_col (WEdit
* edit
)
2925 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
2928 /* --------------------------------------------------------------------------------------------- */
2931 edit_get_curs_col (const WEdit
* edit
)
2933 return edit
->curs_col
;
2936 /* --------------------------------------------------------------------------------------------- */
2937 /** moves the display start position up by i lines */
2940 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
2942 unsigned long lines_above
= edit
->start_line
;
2943 if (i
> lines_above
)
2947 edit
->start_line
-= i
;
2948 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
2949 edit
->force
|= REDRAW_PAGE
;
2950 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2952 edit_update_curs_row (edit
);
2956 /* --------------------------------------------------------------------------------------------- */
2957 /** returns 1 if could scroll, 0 otherwise */
2960 edit_scroll_downward (WEdit
* edit
, int i
)
2963 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->widget
.lines
- 1);
2964 if (lines_below
> 0)
2966 if (i
> lines_below
)
2968 edit
->start_line
+= i
;
2969 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
2970 edit
->force
|= REDRAW_PAGE
;
2971 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2973 edit_update_curs_row (edit
);
2976 /* --------------------------------------------------------------------------------------------- */
2979 edit_scroll_right (WEdit
* edit
, int i
)
2981 edit
->force
|= REDRAW_PAGE
;
2982 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2983 edit
->start_col
-= i
;
2986 /* --------------------------------------------------------------------------------------------- */
2989 edit_scroll_left (WEdit
* edit
, int i
)
2991 if (edit
->start_col
)
2993 edit
->start_col
+= i
;
2994 if (edit
->start_col
> 0)
2995 edit
->start_col
= 0;
2996 edit
->force
|= REDRAW_PAGE
;
2997 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3001 /* --------------------------------------------------------------------------------------------- */
3002 /* high level cursor movement commands */
3003 /* --------------------------------------------------------------------------------------------- */
3006 edit_move_to_prev_col (WEdit
* edit
, long p
)
3008 int prev
= edit
->prev_col
;
3009 int over
= edit
->over_col
;
3010 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3012 if (option_cursor_beyond_eol
)
3014 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3015 edit_eol (edit
, edit
->curs1
));
3017 if (line_len
< prev
+ edit
->over_col
)
3019 edit
->over_col
= prev
+ over
- line_len
;
3020 edit
->prev_col
= line_len
;
3021 edit
->curs_col
= line_len
;
3025 edit
->curs_col
= prev
+ over
;
3026 edit
->prev_col
= edit
->curs_col
;
3033 if (is_in_indent (edit
) && option_fake_half_tabs
)
3035 edit_update_curs_col (edit
);
3037 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3039 int q
= edit
->curs_col
;
3040 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3041 p
= edit_bol (edit
, edit
->curs1
);
3042 edit_cursor_move (edit
,
3043 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3045 if (!left_of_four_spaces (edit
))
3046 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3052 /* --------------------------------------------------------------------------------------------- */
3055 line_is_blank (WEdit
* edit
, long line
)
3057 return is_blank (edit
, edit_find_line (edit
, line
));
3060 /* --------------------------------------------------------------------------------------------- */
3061 /** move cursor to line 'line' */
3064 edit_move_to_line (WEdit
* e
, long line
)
3066 if (line
< e
->curs_line
)
3067 edit_move_up (e
, e
->curs_line
- line
, 0);
3069 edit_move_down (e
, line
- e
->curs_line
, 0);
3070 edit_scroll_screen_over_cursor (e
);
3073 /* --------------------------------------------------------------------------------------------- */
3074 /** scroll window so that first visible line is 'line' */
3077 edit_move_display (WEdit
* e
, long line
)
3079 if (line
< e
->start_line
)
3080 edit_scroll_upward (e
, e
->start_line
- line
);
3082 edit_scroll_downward (e
, line
- e
->start_line
);
3085 /* --------------------------------------------------------------------------------------------- */
3086 /** save markers onto undo stack */
3089 edit_push_markers (WEdit
* edit
)
3091 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
3092 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
3093 edit_push_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3096 /* --------------------------------------------------------------------------------------------- */
3099 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3108 /* --------------------------------------------------------------------------------------------- */
3109 /** highlight marker toggle */
3112 edit_mark_cmd (WEdit
* edit
, int unmark
)
3114 edit_push_markers (edit
);
3117 edit_set_markers (edit
, 0, 0, 0, 0);
3118 edit
->force
|= REDRAW_PAGE
;
3122 if (edit
->mark2
>= 0)
3124 edit
->end_mark_curs
= -1;
3125 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3126 edit
->curs_col
+ edit
->over_col
);
3127 edit
->force
|= REDRAW_PAGE
;
3131 edit
->end_mark_curs
= edit
->curs1
;
3132 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3133 edit
->curs_col
+ edit
->over_col
);
3138 /* --------------------------------------------------------------------------------------------- */
3139 /** highlight the word under cursor */
3142 edit_mark_current_word_cmd (WEdit
* edit
)
3146 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3150 c1
= edit_get_byte (edit
, pos
);
3151 c2
= edit_get_byte (edit
, pos
- 1);
3152 if (!isspace (c1
) && isspace (c2
))
3154 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3159 for (; pos
< edit
->last_byte
; pos
++)
3163 c1
= edit_get_byte (edit
, pos
);
3164 c2
= edit_get_byte (edit
, pos
+ 1);
3165 if (!isspace (c1
) && isspace (c2
))
3167 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3170 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3172 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3175 /* --------------------------------------------------------------------------------------------- */
3178 edit_mark_current_line_cmd (WEdit
* edit
)
3180 long pos
= edit
->curs1
;
3182 edit
->mark1
= edit_bol (edit
, pos
);
3183 edit
->mark2
= edit_eol (edit
, pos
);
3185 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3188 /* --------------------------------------------------------------------------------------------- */
3191 edit_delete_line (WEdit
* edit
)
3194 * Delete right part of the line.
3195 * Note that edit_get_byte() returns '\n' when byte position is
3198 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3200 (void) edit_delete (edit
, 1);
3205 * Note that edit_delete() will not corrupt anything if called while
3206 * cursor position is EOF.
3208 (void) edit_delete (edit
, 1);
3211 * Delete left part of the line.
3212 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3214 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3216 (void) edit_backspace (edit
, 1);
3220 /* --------------------------------------------------------------------------------------------- */
3223 insert_spaces_tab (WEdit
* edit
, int half
)
3226 edit_update_curs_col (edit
);
3227 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
3228 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
3231 edit_insert (edit
, ' ');
3236 /* --------------------------------------------------------------------------------------------- */
3239 edit_indent_width (WEdit
* edit
, long p
)
3242 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3244 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3247 /* --------------------------------------------------------------------------------------------- */
3250 edit_insert_indent (WEdit
* edit
, int indent
)
3252 if (!option_fill_tabs_with_spaces
)
3254 while (indent
>= TAB_SIZE
)
3256 edit_insert (edit
, '\t');
3260 while (indent
-- > 0)
3261 edit_insert (edit
, ' ');
3264 /* --------------------------------------------------------------------------------------------- */
3267 edit_push_key_press (WEdit
* edit
)
3269 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
3270 if (edit
->mark2
== -1)
3272 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
3273 edit_push_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3277 /* --------------------------------------------------------------------------------------------- */
3280 edit_find_bracket (WEdit
* edit
)
3282 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3283 if (last_bracket
!= edit
->bracket
)
3284 edit
->force
|= REDRAW_PAGE
;
3285 last_bracket
= edit
->bracket
;
3288 /* --------------------------------------------------------------------------------------------- */
3290 * This executes a command as though the user initiated it through a key
3291 * press. Callback with WIDGET_KEY as a message calls this after
3292 * translating the key press. This function can be used to pass any
3293 * command to the editor. Note that the screen wouldn't update
3294 * automatically. Either of command or char_for_insertion must be
3295 * passed as -1. Commands are executed, and char_for_insertion is
3296 * inserted at the cursor.
3300 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3302 if (command
== CK_Begin_Record_Macro
)
3305 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3308 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
3310 edit
->force
|= REDRAW_COMPLETELY
;
3311 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
3315 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
3317 edit
->macro
[edit
->macro_i
].command
= command
;
3318 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
3320 /* record the beginning of a set of editing actions initiated by a key press */
3321 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
3322 edit_push_key_press (edit
);
3324 edit_execute_cmd (edit
, command
, char_for_insertion
);
3325 if (edit
->column_highlight
)
3326 edit
->force
|= REDRAW_PAGE
;
3329 /* --------------------------------------------------------------------------------------------- */
3331 This executes a command at a lower level than macro recording.
3332 It also does not push a key_press onto the undo stack. This means
3333 that if it is called many times, a single undo command will undo
3334 all of them. It also does not check for the Undo command.
3337 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3339 edit
->force
|= REDRAW_LINE
;
3341 /* The next key press will unhighlight the found string, so update
3343 if (edit
->found_len
|| edit
->column_highlight
)
3344 edit
->force
|= REDRAW_PAGE
;
3346 if (command
/ 100 == 6)
3347 { /* a highlight command like shift-arrow */
3348 edit
->column_highlight
= 0;
3349 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3351 edit_mark_cmd (edit
, 1); /* clear */
3352 edit_mark_cmd (edit
, 0); /* marking on */
3354 edit
->highlight
= 1;
3357 { /* any other command */
3358 if (edit
->highlight
)
3359 edit_mark_cmd (edit
, 0); /* clear */
3360 edit
->highlight
= 0;
3363 /* first check for undo */
3364 if (command
== CK_Undo
)
3366 edit_do_undo (edit
);
3367 edit
->found_len
= 0;
3368 edit
->prev_col
= edit_get_col (edit
);
3369 edit
->search_start
= edit
->curs1
;
3373 /* An ordinary key press */
3374 if (char_for_insertion
>= 0)
3376 /* if non persistent selection and text selected */
3377 if (!option_persistent_selections
)
3379 if (edit
->mark1
!= edit
->mark2
)
3380 edit_block_delete_cmd (edit
);
3382 if (edit
->overwrite
)
3384 /* remove char only one time, after input first byte, multibyte chars */
3385 if ((!utf8_display
|| edit
->charpoint
== 0)
3386 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3387 edit_delete (edit
, 0);
3389 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3390 edit_insert_over (edit
);
3392 if (char_for_insertion
> 255 && utf8_display
== 0)
3394 unsigned char str
[6 + 1];
3396 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3406 while (str
[i
] != 0 && i
<= 6)
3408 char_for_insertion
= str
[i
];
3409 edit_insert (edit
, char_for_insertion
);
3415 edit_insert (edit
, char_for_insertion
);
3417 if (option_auto_para_formatting
)
3419 format_paragraph (edit
, 0);
3420 edit
->force
|= REDRAW_PAGE
;
3423 check_and_wrap_line (edit
);
3424 edit
->found_len
= 0;
3425 edit
->prev_col
= edit_get_col (edit
);
3426 edit
->search_start
= edit
->curs1
;
3427 edit_find_bracket (edit
);
3435 case CK_Begin_Page_Highlight
:
3436 case CK_End_Page_Highlight
:
3443 if (edit
->mark2
>= 0)
3445 if (!option_persistent_selections
)
3447 if (edit
->column_highlight
)
3448 edit_push_action (edit
, COLUMN_ON
);
3449 edit
->column_highlight
= 0;
3450 edit_mark_cmd (edit
, 1);
3459 case CK_Begin_Page_Highlight
:
3460 case CK_End_Page_Highlight
:
3465 case CK_Word_Left_Highlight
:
3466 case CK_Word_Right_Highlight
:
3467 case CK_Up_Highlight
:
3468 case CK_Down_Highlight
:
3469 case CK_Up_Alt_Highlight
:
3470 case CK_Down_Alt_Highlight
:
3471 if (edit
->mark2
== -1)
3472 break; /*marking is following the cursor: may need to highlight a whole line */
3475 case CK_Left_Highlight
:
3476 case CK_Right_Highlight
:
3477 edit
->force
|= REDRAW_CHAR_ONLY
;
3480 /* basic cursor key commands */
3484 /* if non persistent selection and text selected */
3485 if (!option_persistent_selections
)
3487 if (edit
->mark1
!= edit
->mark2
)
3489 edit_block_delete_cmd (edit
);
3493 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3498 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3500 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3501 edit_backspace (edit
, 1);
3506 if (option_fake_half_tabs
)
3509 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3511 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3512 edit_backspace (edit
, 1);
3517 edit_backspace (edit
, 0);
3520 /* if non persistent selection and text selected */
3521 if (!option_persistent_selections
)
3523 if (edit
->mark1
!= edit
->mark2
)
3525 edit_block_delete_cmd (edit
);
3530 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3531 edit_insert_over (edit
);
3533 if (option_fake_half_tabs
)
3536 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3538 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3539 edit_delete (edit
, 1);
3543 edit_delete (edit
, 0);
3545 case CK_Delete_Word_Left
:
3547 edit_left_delete_word (edit
);
3549 case CK_Delete_Word_Right
:
3550 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3551 edit_insert_over (edit
);
3553 edit_right_delete_word (edit
);
3555 case CK_Delete_Line
:
3556 edit_delete_line (edit
);
3558 case CK_Delete_To_Line_End
:
3559 edit_delete_to_line_end (edit
);
3561 case CK_Delete_To_Line_Begin
:
3562 edit_delete_to_line_begin (edit
);
3566 if (option_auto_para_formatting
)
3568 edit_double_newline (edit
);
3569 if (option_return_does_auto_indent
)
3570 edit_auto_indent (edit
);
3571 format_paragraph (edit
, 0);
3575 edit_insert (edit
, '\n');
3576 if (option_return_does_auto_indent
)
3578 edit_auto_indent (edit
);
3583 edit_insert (edit
, '\n');
3586 case CK_Page_Up_Alt_Highlight
:
3587 edit
->column_highlight
= 1;
3589 case CK_Page_Up_Highlight
:
3590 edit_move_up (edit
, edit
->widget
.lines
- 1, 1);
3592 case CK_Page_Down_Alt_Highlight
:
3593 edit
->column_highlight
= 1;
3595 case CK_Page_Down_Highlight
:
3596 edit_move_down (edit
, edit
->widget
.lines
- 1, 1);
3598 case CK_Left_Alt_Highlight
:
3599 edit
->column_highlight
= 1;
3601 case CK_Left_Highlight
:
3602 if (option_fake_half_tabs
)
3604 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3606 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3609 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3610 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3614 edit_left_char_move_cmd (edit
);
3616 case CK_Right_Alt_Highlight
:
3617 edit
->column_highlight
= 1;
3619 case CK_Right_Highlight
:
3620 if (option_fake_half_tabs
)
3622 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3624 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3625 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3629 edit_right_char_move_cmd (edit
);
3632 case CK_Begin_Page_Highlight
:
3633 edit_begin_page (edit
);
3636 case CK_End_Page_Highlight
:
3637 edit_end_page (edit
);
3640 case CK_Word_Left_Highlight
:
3642 edit_left_word_move_cmd (edit
);
3645 case CK_Word_Right_Highlight
:
3647 edit_right_word_move_cmd (edit
);
3649 case CK_Up_Alt_Highlight
:
3650 edit
->column_highlight
= 1;
3652 case CK_Up_Highlight
:
3653 edit_move_up (edit
, 1, 0);
3655 case CK_Down_Alt_Highlight
:
3656 edit
->column_highlight
= 1;
3658 case CK_Down_Highlight
:
3659 edit_move_down (edit
, 1, 0);
3661 case CK_Paragraph_Up_Alt_Highlight
:
3662 edit
->column_highlight
= 1;
3663 case CK_Paragraph_Up
:
3664 case CK_Paragraph_Up_Highlight
:
3665 edit_move_up_paragraph (edit
, 0);
3667 case CK_Paragraph_Down_Alt_Highlight
:
3668 edit
->column_highlight
= 1;
3669 case CK_Paragraph_Down
:
3670 case CK_Paragraph_Down_Highlight
:
3671 edit_move_down_paragraph (edit
, 0);
3673 case CK_Scroll_Up_Alt_Highlight
:
3674 edit
->column_highlight
= 1;
3676 case CK_Scroll_Up_Highlight
:
3677 edit_move_up (edit
, 1, 1);
3679 case CK_Scroll_Down_Alt_Highlight
:
3680 edit
->column_highlight
= 1;
3681 case CK_Scroll_Down
:
3682 case CK_Scroll_Down_Highlight
:
3683 edit_move_down (edit
, 1, 1);
3686 case CK_Home_Highlight
:
3687 edit_cursor_to_bol (edit
);
3690 case CK_End_Highlight
:
3691 edit_cursor_to_eol (edit
);
3694 /* if text marked shift block */
3695 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3697 if (edit
->mark2
< 0)
3698 edit_mark_cmd (edit
, 0);
3699 edit_move_block_to_right (edit
);
3703 if (option_cursor_beyond_eol
)
3704 edit_insert_over (edit
);
3705 edit_tab_cmd (edit
);
3706 if (option_auto_para_formatting
)
3708 format_paragraph (edit
, 0);
3709 edit
->force
|= REDRAW_PAGE
;
3713 check_and_wrap_line (edit
);
3718 case CK_Toggle_Insert
:
3719 edit
->overwrite
= (edit
->overwrite
== 0);
3723 if (edit
->mark2
>= 0)
3725 if (edit
->column_highlight
)
3726 edit_push_action (edit
, COLUMN_ON
);
3727 edit
->column_highlight
= 0;
3729 edit_mark_cmd (edit
, 0);
3731 case CK_Column_Mark
:
3732 if (!edit
->column_highlight
)
3733 edit_push_action (edit
, COLUMN_OFF
);
3734 edit
->column_highlight
= 1;
3735 edit_mark_cmd (edit
, 0);
3738 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3739 edit
->force
|= REDRAW_PAGE
;
3742 if (edit
->column_highlight
)
3743 edit_push_action (edit
, COLUMN_ON
);
3744 edit
->column_highlight
= 0;
3745 edit_mark_cmd (edit
, 1);
3748 if (edit
->column_highlight
)
3749 edit_push_action (edit
, COLUMN_ON
);
3750 edit
->column_highlight
= 0;
3751 edit_mark_current_word_cmd (edit
);
3754 if (edit
->column_highlight
)
3755 edit_push_action (edit
, COLUMN_ON
);
3756 edit
->column_highlight
= 0;
3757 edit_mark_current_line_cmd (edit
);
3759 case CK_Toggle_Line_State
:
3760 option_line_state
= !option_line_state
;
3761 if (option_line_state
)
3763 option_line_state_width
= LINE_STATE_WIDTH
;
3767 option_line_state_width
= 0;
3769 edit
->force
|= REDRAW_PAGE
;
3772 case CK_Toggle_Show_Margin
:
3773 show_right_margin
= !show_right_margin
;
3774 edit
->force
|= REDRAW_PAGE
;
3777 case CK_Toggle_Bookmark
:
3778 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3779 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3780 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3782 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3784 case CK_Flush_Bookmarks
:
3785 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3786 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3787 edit
->force
|= REDRAW_PAGE
;
3789 case CK_Next_Bookmark
:
3790 if (edit
->book_mark
)
3792 struct _book_mark
*p
;
3793 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3797 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
3798 || p
->line
< edit
->start_line
)
3799 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
3800 edit_move_to_line (edit
, p
->line
);
3804 case CK_Prev_Bookmark
:
3805 if (edit
->book_mark
)
3807 struct _book_mark
*p
;
3808 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3809 while (p
->line
== edit
->curs_line
)
3814 if (p
->line
>= edit
->start_line
+ edit
->widget
.lines
3815 || p
->line
< edit
->start_line
)
3816 edit_move_display (edit
, p
->line
- edit
->widget
.lines
/ 2);
3817 edit_move_to_line (edit
, p
->line
);
3822 case CK_Beginning_Of_Text
:
3823 case CK_Beginning_Of_Text_Highlight
:
3824 edit_move_to_top (edit
);
3826 case CK_End_Of_Text
:
3827 case CK_End_Of_Text_Highlight
:
3828 edit_move_to_bottom (edit
);
3832 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3833 edit_insert_over (edit
);
3834 edit_block_copy_cmd (edit
);
3837 edit_block_delete_cmd (edit
);
3840 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3841 edit_insert_over (edit
);
3842 edit_block_move_cmd (edit
);
3845 case CK_Shift_Block_Left
:
3846 if (edit
->mark1
!= edit
->mark2
)
3847 edit_move_block_to_left (edit
);
3849 case CK_Shift_Block_Right
:
3850 if (edit
->mark1
!= edit
->mark2
)
3851 edit_move_block_to_right (edit
);
3854 edit_copy_to_X_buf_cmd (edit
);
3857 edit_cut_to_X_buf_cmd (edit
);
3860 /* if non persistent selection and text selected */
3861 if (!option_persistent_selections
)
3863 if (edit
->mark1
!= edit
->mark2
)
3864 edit_block_delete_cmd (edit
);
3866 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3867 edit_insert_over (edit
);
3868 edit_paste_from_X_buf_cmd (edit
);
3870 case CK_Selection_History
:
3871 edit_paste_from_history (edit
);
3875 edit_save_as_cmd (edit
);
3878 edit_save_confirm_cmd (edit
);
3881 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3884 edit_save_block_cmd (edit
);
3886 case CK_Insert_File
:
3887 edit_insert_file_cmd (edit
);
3890 case CK_Load_Prev_File
:
3891 edit_load_back_cmd (edit
);
3893 case CK_Load_Next_File
:
3894 edit_load_forward_cmd (edit
);
3897 case CK_Load_Syntax_File
:
3898 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3900 case CK_Choose_Syntax
:
3901 edit_syntax_dialog (edit
);
3904 case CK_Load_Menu_File
:
3905 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3908 case CK_Toggle_Syntax
:
3909 option_syntax_highlighting
^= 1;
3910 if (option_syntax_highlighting
== 1)
3911 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3912 edit
->force
|= REDRAW_PAGE
;
3915 case CK_Toggle_Tab_TWS
:
3916 enable_show_tabs_tws
^= 1;
3917 edit
->force
|= REDRAW_PAGE
;
3921 edit_search_cmd (edit
, FALSE
);
3924 edit_search_cmd (edit
, TRUE
);
3927 edit_replace_cmd (edit
, 0);
3929 case CK_Replace_Again
:
3930 edit_replace_cmd (edit
, 1);
3932 case CK_Complete_Word
:
3933 /* if text marked shift block */
3934 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3936 edit_move_block_to_left (edit
);
3940 edit_complete_word_cmd (edit
);
3943 case CK_Find_Definition
:
3944 edit_get_match_keyword_cmd (edit
);
3947 dlg_stop (edit
->widget
.owner
);
3950 edit_new_cmd (edit
);
3953 edit_help_cmd (edit
);
3956 edit_refresh_cmd (edit
);
3958 case CK_SaveSetupCmd
:
3967 case CK_Edit_Options
:
3968 edit_options_dialog (edit
);
3970 case CK_Edit_Save_Mode
:
3971 menu_save_mode_cmd ();
3976 /* fool gcc to prevent a Y2K warning */
3977 char time_format
[] = "_c";
3978 time_format
[0] = '%';
3980 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3981 edit_print_string (edit
, s
);
3982 edit
->force
|= REDRAW_PAGE
;
3987 edit_goto_cmd (edit
);
3989 case CK_Paragraph_Format
:
3990 format_paragraph (edit
, 1);
3991 edit
->force
|= REDRAW_PAGE
;
3993 case CK_Delete_Macro
:
3994 edit_delete_macro_cmd (edit
);
3996 case CK_Match_Bracket
:
3997 edit_goto_matching_bracket (edit
);
4003 edit_sort_cmd (edit
);
4006 edit_ext_cmd (edit
);
4009 edit_mail_dialog (edit
);
4014 case CK_SelectCodepage
:
4015 edit_select_codepage_cmd (edit
);
4017 case CK_Insert_Literal
:
4018 edit_insert_literal_cmd (edit
);
4020 case CK_Execute_Macro
:
4021 edit_execute_macro_cmd (edit
);
4023 case CK_Begin_End_Macro
:
4024 edit_begin_end_macro_cmd (edit
);
4034 if ((command
/ 1000) == 1) /* a shell command */
4035 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
4036 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
4037 { /* a macro command */
4038 struct macro m
[MAX_MACRO_LENGTH
];
4040 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
4041 edit_execute_macro (edit
, m
, nm
);
4044 /* keys which must set the col position, and the search vars */
4050 case CK_Replace_Again
:
4051 case CK_Complete_Word
:
4052 edit
->prev_col
= edit_get_col (edit
);
4055 case CK_Up_Highlight
:
4056 case CK_Up_Alt_Highlight
:
4058 case CK_Down_Highlight
:
4059 case CK_Down_Alt_Highlight
:
4061 case CK_Page_Up_Highlight
:
4062 case CK_Page_Up_Alt_Highlight
:
4064 case CK_Page_Down_Highlight
:
4065 case CK_Page_Down_Alt_Highlight
:
4066 case CK_Beginning_Of_Text
:
4067 case CK_Beginning_Of_Text_Highlight
:
4068 case CK_End_Of_Text
:
4069 case CK_End_Of_Text_Highlight
:
4070 case CK_Paragraph_Up
:
4071 case CK_Paragraph_Up_Highlight
:
4072 case CK_Paragraph_Up_Alt_Highlight
:
4073 case CK_Paragraph_Down
:
4074 case CK_Paragraph_Down_Highlight
:
4075 case CK_Paragraph_Down_Alt_Highlight
:
4077 case CK_Scroll_Up_Highlight
:
4078 case CK_Scroll_Up_Alt_Highlight
:
4079 case CK_Scroll_Down
:
4080 case CK_Scroll_Down_Highlight
:
4081 case CK_Scroll_Down_Alt_Highlight
:
4082 edit
->search_start
= edit
->curs1
;
4083 edit
->found_len
= 0;
4086 edit
->found_len
= 0;
4087 edit
->prev_col
= edit_get_col (edit
);
4088 edit
->search_start
= edit
->curs1
;
4090 edit_find_bracket (edit
);
4092 if (option_auto_para_formatting
)
4098 case CK_Delete_Word_Left
:
4099 case CK_Delete_Word_Right
:
4100 case CK_Delete_To_Line_End
:
4101 case CK_Delete_To_Line_Begin
:
4102 format_paragraph (edit
, 0);
4103 edit
->force
|= REDRAW_PAGE
;
4108 /* --------------------------------------------------------------------------------------------- */
4111 edit_stack_init (void)
4113 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4115 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4116 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4119 edit_stack_iterator
= 0;
4122 /* --------------------------------------------------------------------------------------------- */
4125 edit_stack_free (void)
4127 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4128 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4131 /* --------------------------------------------------------------------------------------------- */
4135 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4137 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4140 /* --------------------------------------------------------------------------------------------- */
4144 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4146 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4149 /* --------------------------------------------------------------------------------------------- */
4152 edit_unlock_file (WEdit
* edit
)
4157 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4158 ret
= unlock_file (fullpath
);
4164 /* --------------------------------------------------------------------------------------------- */
4167 edit_lock_file (WEdit
* edit
)
4172 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4173 ret
= lock_file (fullpath
);
4179 /* --------------------------------------------------------------------------------------------- */