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.
6 Authors: Paul Sheer 1996, 1997
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 option_group_undo
= 0;
95 int show_right_margin
= 0;
97 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
98 char *option_backup_ext
= NULL
;
100 int edit_stack_iterator
= 0;
101 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
102 /* magic sequense for say than block is vertical */
103 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
105 /*** file scope macro definitions ****************************************************************/
107 #define TEMP_BUF_LEN 1024
109 #define space_width 1
111 /*** file scope type declarations ****************************************************************/
113 /*** file scope variables ************************************************************************/
115 /* detecting an error on save is easy: just check if every byte has been written. */
116 /* detecting an error on read, is not so easy 'cos there is not way to tell
117 whether you read everything or not. */
118 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
119 static const struct edit_filters
121 const char *read
, *write
, *extension
;
125 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
126 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
127 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
129 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
133 static long last_bracket
= -1;
135 /*** file scope functions ************************************************************************/
136 /* --------------------------------------------------------------------------------------------- */
140 * here's a quick sketch of the layout: (don't run this through indent.)
142 * (b1 is buffers1 and b2 is buffers2)
145 * \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
146 * ______________________________________|______________________________________
148 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
149 * |-> |-> |-> |-> |-> |-> |
151 * _<------------------------->|<----------------->_
152 * WEdit->curs2 | WEdit->curs1
157 * file end|||file beginning
166 /* --------------------------------------------------------------------------------------------- */
168 static int left_of_four_spaces (WEdit
* edit
);
170 /* --------------------------------------------------------------------------------------------- */
175 const char *header
= N_("About");
176 const char *button_name
= N_("&OK");
177 const char *const version
= "MCEdit " VERSION
;
178 char text
[BUF_LARGE
];
180 int win_len
, version_len
, button_len
;
187 button_name
= _(button_name
);
190 button_len
= str_term_width1 (button_name
) + 5;
191 version_len
= str_term_width1 (version
);
193 g_snprintf (text
, sizeof (text
),
194 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
195 " A user friendly text editor\n"
196 " written for the Midnight Commander"));
198 win_len
= str_term_width1 (header
);
199 win_len
= max (win_len
, version_len
);
200 win_len
= max (win_len
, button_len
);
202 /* count width and height of text */
203 str_msg_term_size (text
, &lines
, &cols
);
205 cols
= max (win_len
, cols
) + 6;
208 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
209 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
211 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
212 add_widget (about_dlg
, label_new (5, 3, text
));
213 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
214 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
217 destroy_dlg (about_dlg
);
220 /* --------------------------------------------------------------------------------------------- */
222 * Initialize the buffers for an empty files.
226 edit_init_buffers (WEdit
* edit
)
230 for (j
= 0; j
<= MAXBUFF
; j
++)
232 edit
->buffers1
[j
] = NULL
;
233 edit
->buffers2
[j
] = NULL
;
238 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
241 /* --------------------------------------------------------------------------------------------- */
243 * Load file OR text into buffers. Set cursor to the beginning of file.
244 * @returns 1 on error.
248 edit_load_file_fast (WEdit
* edit
, const char *filename
)
254 edit
->curs2
= edit
->last_byte
;
255 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
257 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
262 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
263 edit_error_dialog (_("Error"), errmsg
);
268 if (!edit
->buffers2
[buf2
])
269 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
274 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
275 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
278 for (buf
= buf2
- 1; buf
>= 0; buf
--)
280 /* edit->buffers2[0] is already allocated */
281 if (!edit
->buffers2
[buf
])
282 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
283 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
291 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
292 edit_error_dialog (_("Error"), err_str
);
299 /* --------------------------------------------------------------------------------------------- */
300 /** Return index of the filter or -1 is there is no appropriate filter */
303 edit_find_filter (const char *filename
)
307 if (filename
== NULL
)
310 l
= strlen (filename
);
311 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
313 e
= strlen (all_filters
[i
].extension
);
315 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
321 /* --------------------------------------------------------------------------------------------- */
324 edit_get_filter (const char *filename
)
327 char *p
, *quoted_name
;
329 i
= edit_find_filter (filename
);
333 quoted_name
= name_quote (filename
, 0);
334 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
335 g_free (quoted_name
);
339 /* --------------------------------------------------------------------------------------------- */
342 edit_insert_stream (WEdit
* edit
, FILE * f
)
346 while ((c
= fgetc (f
)) >= 0)
348 edit_insert (edit
, c
);
354 /* --------------------------------------------------------------------------------------------- */
355 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
358 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
361 gchar
*errmsg
= NULL
;
363 /* Try opening an existing file */
364 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
369 * Try creating the file. O_EXCL prevents following broken links
370 * and opening existing files.
372 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
375 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
380 /* New file, delete it if it's not modified or saved */
381 edit
->delete_file
= 1;
385 /* Check what we have opened */
386 if (mc_fstat (file
, st
) < 0)
388 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
392 /* We want to open regular files only */
393 if (!S_ISREG (st
->st_mode
))
395 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
400 * Don't delete non-empty files.
401 * O_EXCL should prevent it, but let's be on the safe side.
404 edit
->delete_file
= 0;
406 if (st
->st_size
>= SIZE_LIMIT
)
407 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
410 (void) mc_close (file
);
414 edit_error_dialog (_("Error"), errmsg
);
421 /* --------------------------------------------------------------------------------------------- */
423 * Open the file and load it into the buffers, either directly or using
424 * a filter. Return 0 on success, 1 on error.
426 * Fast loading (edit_load_file_fast) is used when the file size is
427 * known. In this case the data is read into the buffers by blocks.
428 * If the file size is not known, the data is loaded byte by byte in
433 edit_load_file (WEdit
* edit
)
437 /* Cannot do fast load if a filter is used */
438 if (edit_find_filter (edit
->filename
) >= 0)
442 * VFS may report file size incorrectly, and slow load is not a big
443 * deal considering overhead in VFS.
445 if (!vfs_file_is_local (edit
->filename
))
449 * FIXME: line end translation should disable fast loading as well
450 * Consider doing fseek() to the end and ftell() for the real size.
455 /* If we are dealing with a real file, check that it exists */
456 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
461 /* nothing to load */
465 edit_init_buffers (edit
);
469 edit
->last_byte
= edit
->stat1
.st_size
;
470 edit_load_file_fast (edit
, edit
->filename
);
471 /* If fast load was used, the number of lines wasn't calculated */
472 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
479 edit
->undo_stack_disable
= 1;
480 if (edit_insert_file (edit
, edit
->filename
) == 0)
485 edit
->undo_stack_disable
= 0;
492 /* --------------------------------------------------------------------------------------------- */
493 /** Restore saved cursor position in the file */
496 edit_load_position (WEdit
* edit
)
502 if (!edit
->filename
|| !*edit
->filename
)
505 filename
= vfs_canon (edit
->filename
);
506 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
511 edit_move_to_line (edit
, line
- 1);
512 edit
->prev_col
= column
;
516 edit_cursor_move (edit
, offset
);
517 line
= edit
->curs_line
;
518 edit
->search_start
= edit
->curs1
;
521 book_mark_restore (edit
, BOOK_MARK_COLOR
);
523 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
524 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
527 /* --------------------------------------------------------------------------------------------- */
528 /** Save cursor position in the file */
531 edit_save_position (WEdit
* edit
)
535 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
538 filename
= vfs_canon (edit
->filename
);
540 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
541 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
542 edit
->serialized_bookmarks
);
543 edit
->serialized_bookmarks
= NULL
;
548 /* --------------------------------------------------------------------------------------------- */
549 /** Clean the WEdit stricture except the widget part */
552 edit_purge_widget (WEdit
* edit
)
554 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
555 char *start
= (char *) edit
+ sizeof (Widget
);
556 memset (start
, 0, len
);
559 /* --------------------------------------------------------------------------------------------- */
562 edit_set_keymap (void)
564 editor_map
= default_editor_keymap
;
565 if (editor_keymap
&& editor_keymap
->len
> 0)
566 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
568 editor_x_map
= default_editor_x_keymap
;
569 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
570 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
573 /* --------------------------------------------------------------------------------------------- */
575 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
576 then the file should be as it was when he loaded up. Then set edit->modified to 0.
580 edit_pop_undo_action (WEdit
* edit
)
583 unsigned long sp
= edit
->undo_stack_pointer
;
585 if (sp
== edit
->undo_stack_bottom
)
588 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
589 c
= edit
->undo_stack
[sp
];
592 /* edit->undo_stack[sp] = '@'; */
593 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
597 if (sp
== edit
->undo_stack_bottom
)
600 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
601 if (edit
->undo_stack
[sp
] == -2)
603 /* edit->undo_stack[sp] = '@'; */
604 edit
->undo_stack_pointer
= sp
;
607 edit
->undo_stack
[sp
]++;
613 edit_pop_redo_action (WEdit
* edit
)
616 unsigned long sp
= edit
->redo_stack_pointer
;
618 if (sp
== edit
->redo_stack_bottom
)
621 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
622 c
= edit
->redo_stack
[sp
];
625 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
629 if (sp
== edit
->redo_stack_bottom
)
632 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
633 if (edit
->redo_stack
[sp
] == -2)
634 edit
->redo_stack_pointer
= sp
;
636 edit
->redo_stack
[sp
]++;
642 get_prev_undo_action (WEdit
* edit
)
645 unsigned long sp
= edit
->undo_stack_pointer
;
647 if (sp
== edit
->undo_stack_bottom
)
650 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
651 c
= edit
->undo_stack
[sp
];
655 if (sp
== edit
->undo_stack_bottom
)
658 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
662 /* --------------------------------------------------------------------------------------------- */
663 /** is called whenever a modification is made by one of the four routines below */
666 edit_modification (WEdit
* edit
)
668 edit
->caches_valid
= 0;
669 edit
->screen_modified
= 1;
671 /* raise lock when file modified */
672 if (!edit
->modified
&& !edit
->delete_file
)
673 edit
->locked
= edit_lock_file (edit
);
677 /* --------------------------------------------------------------------------------------------- */
680 edit_insert_over (WEdit
* edit
)
684 for (i
= 0; i
< edit
->over_col
; i
++)
686 edit_insert (edit
, ' ');
691 /* --------------------------------------------------------------------------------------------- */
694 edit_backspace (WEdit
* edit
, const int byte_delete
)
705 if (edit
->mark2
!= edit
->mark1
)
706 edit_push_markers (edit
);
708 if (edit
->utf8
&& byte_delete
== 0)
710 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
714 for (i
= 1; i
<= cw
; i
++)
716 if (edit
->mark1
>= edit
->curs1
)
719 edit
->end_mark_curs
--;
721 if (edit
->mark2
>= edit
->curs1
)
723 if (edit
->last_get_rule
>= edit
->curs1
)
724 edit
->last_get_rule
--;
726 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
727 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
728 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
730 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
731 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
735 edit_push_undo_action (edit
, p
);
737 edit_modification (edit
);
741 book_mark_dec (edit
, edit
->curs_line
);
744 edit
->force
|= REDRAW_AFTER_CURSOR
;
747 if (edit
->curs1
< edit
->start_display
)
749 edit
->start_display
--;
757 /* --------------------------------------------------------------------------------------------- */
758 /* high level cursor movement commands */
759 /* --------------------------------------------------------------------------------------------- */
762 is_in_indent (WEdit
* edit
)
764 long p
= edit_bol (edit
, edit
->curs1
);
765 while (p
< edit
->curs1
)
766 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
771 /* --------------------------------------------------------------------------------------------- */
774 is_blank (WEdit
* edit
, long offset
)
778 s
= edit_bol (edit
, offset
);
779 f
= edit_eol (edit
, offset
) - 1;
782 c
= edit_get_byte (edit
, s
++);
790 /* --------------------------------------------------------------------------------------------- */
791 /** returns the offset of line i */
794 edit_find_line (WEdit
* edit
, int line
)
798 if (!edit
->caches_valid
)
800 for (i
= 0; i
< N_LINE_CACHES
; i
++)
801 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
802 /* three offsets that we *know* are line 0 at 0 and these two: */
803 edit
->line_numbers
[1] = edit
->curs_line
;
804 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
805 edit
->line_numbers
[2] = edit
->total_lines
;
806 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
807 edit
->caches_valid
= 1;
809 if (line
>= edit
->total_lines
)
810 return edit
->line_offsets
[2];
813 /* find the closest known point */
814 for (i
= 0; i
< N_LINE_CACHES
; i
++)
817 n
= abs (edit
->line_numbers
[i
] - line
);
825 return edit
->line_offsets
[j
]; /* know the offset exactly */
826 if (m
== 1 && j
>= 3)
827 i
= j
; /* one line different - caller might be looping, so stay in this cache */
829 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
830 if (line
> edit
->line_numbers
[j
])
831 edit
->line_offsets
[i
] =
832 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
834 edit
->line_offsets
[i
] =
835 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
836 edit
->line_numbers
[i
] = line
;
837 return edit
->line_offsets
[i
];
840 /* --------------------------------------------------------------------------------------------- */
841 /** moves up until a blank line is reached, or until just
842 before a non-blank line is reached */
845 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
848 if (edit
->curs_line
> 1)
850 if (line_is_blank (edit
, edit
->curs_line
))
852 if (line_is_blank (edit
, edit
->curs_line
- 1))
854 for (i
= edit
->curs_line
- 1; i
; i
--)
855 if (!line_is_blank (edit
, i
))
863 for (i
= edit
->curs_line
- 1; i
; i
--)
864 if (line_is_blank (edit
, i
))
870 for (i
= edit
->curs_line
- 1; i
; i
--)
871 if (line_is_blank (edit
, i
))
875 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
878 /* --------------------------------------------------------------------------------------------- */
879 /** moves down until a blank line is reached, or until just
880 before a non-blank line is reached */
883 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
886 if (edit
->curs_line
>= edit
->total_lines
- 1)
888 i
= edit
->total_lines
;
892 if (line_is_blank (edit
, edit
->curs_line
))
894 if (line_is_blank (edit
, edit
->curs_line
+ 1))
896 for (i
= edit
->curs_line
+ 1; i
; i
++)
897 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
905 for (i
= edit
->curs_line
+ 1; i
; i
++)
906 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
912 for (i
= edit
->curs_line
+ 1; i
; i
++)
913 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
917 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
920 /* --------------------------------------------------------------------------------------------- */
923 edit_begin_page (WEdit
* edit
)
925 edit_update_curs_row (edit
);
926 edit_move_up (edit
, edit
->curs_row
, 0);
929 /* --------------------------------------------------------------------------------------------- */
932 edit_end_page (WEdit
* edit
)
934 edit_update_curs_row (edit
);
935 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
939 /* --------------------------------------------------------------------------------------------- */
940 /** goto beginning of text */
943 edit_move_to_top (WEdit
* edit
)
947 edit_cursor_move (edit
, -edit
->curs1
);
948 edit_move_to_prev_col (edit
, 0);
949 edit
->force
|= REDRAW_PAGE
;
950 edit
->search_start
= 0;
951 edit_update_curs_row (edit
);
956 /* --------------------------------------------------------------------------------------------- */
957 /** goto end of text */
960 edit_move_to_bottom (WEdit
* edit
)
962 if (edit
->curs_line
< edit
->total_lines
)
964 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
965 edit
->start_display
= edit
->last_byte
;
966 edit
->start_line
= edit
->total_lines
;
967 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
968 edit
->force
|= REDRAW_PAGE
;
972 /* --------------------------------------------------------------------------------------------- */
973 /** goto beginning of line */
976 edit_cursor_to_bol (WEdit
* edit
)
978 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
979 edit
->search_start
= edit
->curs1
;
980 edit
->prev_col
= edit_get_col (edit
);
984 /* --------------------------------------------------------------------------------------------- */
985 /** goto end of line */
988 edit_cursor_to_eol (WEdit
* edit
)
990 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
991 edit
->search_start
= edit
->curs1
;
992 edit
->prev_col
= edit_get_col (edit
);
996 /* --------------------------------------------------------------------------------------------- */
1003 const char option_chars_move_whole_word
[] =
1004 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1010 if (*option_chars_move_whole_word
== '!')
1012 return 0x80000000UL
;
1014 if (g_ascii_isupper ((gchar
) c
))
1016 else if (g_ascii_islower ((gchar
) c
))
1018 else if (g_ascii_isalpha (c
))
1020 else if (isdigit (c
))
1022 else if (isspace (c
))
1024 q
= strchr (option_chars_move_whole_word
, c
);
1026 return 0xFFFFFFFFUL
;
1029 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1034 while ((q
= strchr (q
+ 1, c
)));
1038 /* --------------------------------------------------------------------------------------------- */
1041 edit_left_word_move (WEdit
* edit
, int s
)
1046 if (edit
->column_highlight
1047 && edit
->mark1
!= edit
->mark2
1048 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1050 edit_cursor_move (edit
, -1);
1053 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1054 c2
= edit_get_byte (edit
, edit
->curs1
);
1055 if (c1
== '\n' || c2
== '\n')
1057 if (!(my_type_of (c1
) & my_type_of (c2
)))
1059 if (isspace (c1
) && !isspace (c2
))
1062 if (!isspace (c1
) && isspace (c2
))
1067 /* --------------------------------------------------------------------------------------------- */
1070 edit_left_word_move_cmd (WEdit
* edit
)
1072 edit_left_word_move (edit
, 0);
1073 edit
->force
|= REDRAW_PAGE
;
1076 /* --------------------------------------------------------------------------------------------- */
1079 edit_right_word_move (WEdit
* edit
, int s
)
1084 if (edit
->column_highlight
1085 && edit
->mark1
!= edit
->mark2
1086 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1088 edit_cursor_move (edit
, 1);
1089 if (edit
->curs1
>= edit
->last_byte
)
1091 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1092 c2
= edit_get_byte (edit
, edit
->curs1
);
1093 if (c1
== '\n' || c2
== '\n')
1095 if (!(my_type_of (c1
) & my_type_of (c2
)))
1097 if (isspace (c1
) && !isspace (c2
))
1100 if (!isspace (c1
) && isspace (c2
))
1105 /* --------------------------------------------------------------------------------------------- */
1108 edit_right_word_move_cmd (WEdit
* edit
)
1110 edit_right_word_move (edit
, 0);
1111 edit
->force
|= REDRAW_PAGE
;
1114 /* --------------------------------------------------------------------------------------------- */
1117 edit_right_char_move_cmd (WEdit
* edit
)
1123 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1129 c
= edit_get_byte (edit
, edit
->curs1
);
1131 if (option_cursor_beyond_eol
&& c
== '\n')
1137 edit_cursor_move (edit
, cw
);
1141 /* --------------------------------------------------------------------------------------------- */
1144 edit_left_char_move_cmd (WEdit
* edit
)
1147 if (edit
->column_highlight
1148 && option_cursor_beyond_eol
1149 && edit
->mark1
!= edit
->mark2
1150 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1154 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1158 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1164 edit_cursor_move (edit
, -cw
);
1168 /* --------------------------------------------------------------------------------------------- */
1169 /** Up or down cursor moving.
1170 direction = TRUE - move up
1175 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1178 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1187 edit
->force
|= REDRAW_PAGE
;
1191 edit_scroll_upward (edit
, i
);
1193 edit_scroll_downward (edit
, i
);
1195 p
= edit_bol (edit
, edit
->curs1
);
1197 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1199 edit_cursor_move (edit
, p
- edit
->curs1
);
1201 edit_move_to_prev_col (edit
, p
);
1203 /* search start of current multibyte char (like CJK) */
1204 if (edit
->curs1
+ 1 < edit
->last_byte
)
1206 edit_right_char_move_cmd (edit
);
1207 edit_left_char_move_cmd (edit
);
1210 edit
->search_start
= edit
->curs1
;
1211 edit
->found_len
= 0;
1214 /* --------------------------------------------------------------------------------------------- */
1217 edit_right_delete_word (WEdit
* edit
)
1222 if (edit
->curs1
>= edit
->last_byte
)
1224 c1
= edit_delete (edit
, 1);
1225 c2
= edit_get_byte (edit
, edit
->curs1
);
1226 if (c1
== '\n' || c2
== '\n')
1228 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1230 if (!(my_type_of (c1
) & my_type_of (c2
)))
1235 /* --------------------------------------------------------------------------------------------- */
1238 edit_left_delete_word (WEdit
* edit
)
1243 if (edit
->curs1
<= 0)
1245 c1
= edit_backspace (edit
, 1);
1246 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1247 if (c1
== '\n' || c2
== '\n')
1249 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1251 if (!(my_type_of (c1
) & my_type_of (c2
)))
1256 /* --------------------------------------------------------------------------------------------- */
1258 the start column position is not recorded, and hence does not
1259 undo as it happed. But who would notice.
1263 edit_do_undo (WEdit
* edit
)
1268 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1270 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1277 edit_cursor_move (edit
, 1);
1280 edit_cursor_move (edit
, -1);
1284 edit_backspace (edit
, 1);
1288 edit_delete (edit
, 1);
1291 edit
->column_highlight
= 1;
1294 edit
->column_highlight
= 0;
1297 if (ac
>= 256 && ac
< 512)
1298 edit_insert_ahead (edit
, ac
- 256);
1299 if (ac
>= 0 && ac
< 256)
1300 edit_insert (edit
, ac
);
1302 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1304 edit
->mark1
= ac
- MARK_1
;
1305 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1307 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1309 edit
->mark2
= ac
- MARK_2
;
1310 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1312 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1314 edit
->end_mark_curs
= ac
- MARK_CURS
;
1317 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1320 if (edit
->start_display
> ac
- KEY_PRESS
)
1322 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1323 edit
->force
|= REDRAW_PAGE
;
1325 else if (edit
->start_display
< ac
- KEY_PRESS
)
1327 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1328 edit
->force
|= REDRAW_PAGE
;
1330 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1331 edit_update_curs_row (edit
);
1334 edit
->undo_stack_disable
= 0;
1338 edit_do_redo (WEdit
* edit
)
1343 if (edit
->redo_stack_reset
)
1347 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1354 edit_cursor_move (edit
, 1);
1357 edit_cursor_move (edit
, -1);
1360 edit_backspace (edit
, 1);
1363 edit_delete (edit
, 1);
1366 edit
->column_highlight
= 1;
1369 edit
->column_highlight
= 0;
1372 if (ac
>= 256 && ac
< 512)
1373 edit_insert_ahead (edit
, ac
- 256);
1374 if (ac
>= 0 && ac
< 256)
1375 edit_insert (edit
, ac
);
1377 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1379 edit
->mark1
= ac
- MARK_1
;
1380 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1382 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1384 edit
->mark2
= ac
- MARK_2
;
1385 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1387 /* more than one pop usually means something big */
1389 edit
->force
|= REDRAW_PAGE
;
1392 if (edit
->start_display
> ac
- KEY_PRESS
)
1394 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1395 edit
->force
|= REDRAW_PAGE
;
1397 else if (edit
->start_display
< ac
- KEY_PRESS
)
1399 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1400 edit
->force
|= REDRAW_PAGE
;
1402 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1403 edit_update_curs_row (edit
);
1409 edit_group_undo (WEdit
* edit
)
1411 long ac
= KEY_PRESS
;
1412 long cur_ac
= KEY_PRESS
;
1413 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1415 cur_ac
= get_prev_undo_action (edit
);
1416 edit_do_undo (edit
);
1417 ac
= get_prev_undo_action (edit
);
1418 /* exit from cycle if option_group_undo is not set,
1419 * and make single UNDO operation
1421 if (!option_group_undo
)
1426 /* --------------------------------------------------------------------------------------------- */
1429 edit_delete_to_line_end (WEdit
* edit
)
1431 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1435 edit_delete (edit
, 1);
1439 /* --------------------------------------------------------------------------------------------- */
1442 edit_delete_to_line_begin (WEdit
* edit
)
1444 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1448 edit_backspace (edit
, 1);
1452 /* --------------------------------------------------------------------------------------------- */
1455 is_aligned_on_a_tab (WEdit
* edit
)
1457 edit_update_curs_col (edit
);
1458 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1459 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1462 /* --------------------------------------------------------------------------------------------- */
1465 right_of_four_spaces (WEdit
* edit
)
1468 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1469 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1471 return is_aligned_on_a_tab (edit
);
1475 /* --------------------------------------------------------------------------------------------- */
1478 left_of_four_spaces (WEdit
* edit
)
1481 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1482 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1484 return is_aligned_on_a_tab (edit
);
1488 /* --------------------------------------------------------------------------------------------- */
1491 edit_auto_indent (WEdit
* edit
)
1496 /* use the previous line as a template */
1497 p
= edit_move_backward (edit
, p
, 1);
1498 /* copy the leading whitespace of the line */
1500 { /* no range check - the line _is_ \n-terminated */
1501 c
= edit_get_byte (edit
, p
++);
1502 if (c
!= ' ' && c
!= '\t')
1504 edit_insert (edit
, c
);
1508 /* --------------------------------------------------------------------------------------------- */
1511 edit_double_newline (WEdit
* edit
)
1513 edit_insert (edit
, '\n');
1514 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1516 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1518 edit
->force
|= REDRAW_PAGE
;
1519 edit_insert (edit
, '\n');
1522 /* --------------------------------------------------------------------------------------------- */
1525 edit_tab_cmd (WEdit
* edit
)
1529 if (option_fake_half_tabs
)
1531 if (is_in_indent (edit
))
1533 /*insert a half tab (usually four spaces) unless there is a
1534 half tab already behind, then delete it and insert a
1536 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
1538 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1539 edit_backspace (edit
, 1);
1540 edit_insert (edit
, '\t');
1544 insert_spaces_tab (edit
, 1);
1549 if (option_fill_tabs_with_spaces
)
1551 insert_spaces_tab (edit
, 0);
1555 edit_insert (edit
, '\t');
1559 /* --------------------------------------------------------------------------------------------- */
1562 check_and_wrap_line (WEdit
* edit
)
1565 if (!option_typewriter_wrap
)
1567 edit_update_curs_col (edit
);
1568 if (edit
->curs_col
< option_word_wrap_line_length
)
1574 c
= edit_get_byte (edit
, curs
);
1575 if (c
== '\n' || curs
<= 0)
1577 edit_insert (edit
, '\n');
1580 if (c
== ' ' || c
== '\t')
1582 int current
= edit
->curs1
;
1583 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1584 edit_insert (edit
, '\n');
1585 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1591 /* --------------------------------------------------------------------------------------------- */
1592 /** this find the matching bracket in either direction, and sets edit->bracket */
1595 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1597 const char *const b
= "{}{[][()(", *p
;
1598 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1599 unsigned long j
= 0;
1601 edit_update_curs_row (edit
);
1602 c
= edit_get_byte (edit
, edit
->curs1
);
1605 if (!furthest_bracket_search
)
1606 furthest_bracket_search
--;
1607 /* not on a bracket at all */
1610 /* the matching bracket */
1612 /* going left or right? */
1613 if (strchr ("{[(", c
))
1615 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1617 /* out of buffer? */
1618 if (q
>= edit
->last_byte
|| q
< 0)
1620 a
= edit_get_byte (edit
, q
);
1621 /* don't want to eat CPU */
1622 if (j
++ > furthest_bracket_search
)
1624 /* out of screen? */
1627 if (q
< edit
->start_display
)
1629 /* count lines if searching downward */
1630 if (inc
> 0 && a
== '\n')
1631 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
1634 /* count bracket depth */
1635 i
+= (a
== c
) - (a
== d
);
1636 /* return if bracket depth is zero */
1644 /* --------------------------------------------------------------------------------------------- */
1647 edit_goto_matching_bracket (WEdit
* edit
)
1651 q
= edit_get_bracket (edit
, 0, 0);
1654 edit
->bracket
= edit
->curs1
;
1655 edit
->force
|= REDRAW_PAGE
;
1656 edit_cursor_move (edit
, q
- edit
->curs1
);
1660 /* --------------------------------------------------------------------------------------------- */
1661 /*** public functions ****************************************************************************/
1662 /* --------------------------------------------------------------------------------------------- */
1664 /** User edit menu, like user menu (F2) but only in editor. */
1667 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1672 long start_mark
, end_mark
;
1675 block_file
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE
);
1677 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1679 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1681 /* run shell scripts from menu */
1682 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1683 && (mc_stat (block_file
, &status
) == 0)
1684 && (status
.st_size
!= 0))
1689 /* i.e. we have marked block */
1691 rc
= edit_block_delete_cmd (edit
);
1697 ins_len
= edit_insert_file (edit
, block_file
);
1698 if (nomark
== 0 && ins_len
> 0)
1699 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1701 /* truncate block file */
1702 fd
= fopen (block_file
, "w");
1706 edit_cursor_move (edit
, curs
- edit
->curs1
);
1707 edit_refresh_cmd (edit
);
1708 edit
->force
|= REDRAW_COMPLETELY
;
1710 g_free (block_file
);
1713 /* --------------------------------------------------------------------------------------------- */
1716 edit_get_byte (WEdit
* edit
, long byte_index
)
1719 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1722 if (byte_index
>= edit
->curs1
)
1724 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1725 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1729 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1733 /* --------------------------------------------------------------------------------------------- */
1736 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
1739 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1742 if (byte_index
>= edit
->curs1
)
1744 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1745 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1746 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1750 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
1751 (byte_index
& M_EDIT_BUF_SIZE
));
1755 /* --------------------------------------------------------------------------------------------- */
1758 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
1762 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
1768 if (byte_index
>= edit
->curs1
)
1770 p
= edit
->curs1
+ edit
->curs2
- 1;
1771 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1772 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1776 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
1780 /* --------------------------------------------------------------------------------------------- */
1783 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1788 gchar
*next_ch
= NULL
;
1791 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1797 str
= edit_get_byte_ptr (edit
, byte_index
);
1805 res
= g_utf8_get_char_validated (str
, -1);
1815 /* Calculate UTF-8 char width */
1816 next_ch
= g_utf8_next_char (str
);
1819 width
= next_ch
- str
;
1827 *char_width
= width
;
1831 /* --------------------------------------------------------------------------------------------- */
1834 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1836 gchar
*str
, *buf
= NULL
;
1839 gchar
*next_ch
= NULL
;
1845 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1851 ch
= edit_get_utf (edit
, byte_index
, &width
);
1855 *char_width
= width
;
1859 str
= edit_get_byte_ptr (edit
, byte_index
);
1860 buf
= edit_get_buf_ptr (edit
, byte_index
);
1861 if (str
== NULL
|| buf
== NULL
)
1866 /* get prev utf8 char */
1868 str
= g_utf8_find_prev_char (buf
, str
);
1876 res
= g_utf8_get_char_validated (str
, -1);
1886 /* Calculate UTF-8 char width */
1887 next_ch
= g_utf8_next_char (str
);
1890 width
= next_ch
- str
;
1898 *char_width
= width
;
1902 /* --------------------------------------------------------------------------------------------- */
1905 edit_get_write_filter (const char *write_name
, const char *filename
)
1908 char *p
, *writename
;
1910 i
= edit_find_filter (filename
);
1914 writename
= name_quote (write_name
, 0);
1915 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1920 /* --------------------------------------------------------------------------------------------- */
1923 edit_write_stream (WEdit
* edit
, FILE * f
)
1927 if (edit
->lb
== LB_ASIS
)
1929 for (i
= 0; i
< edit
->last_byte
; i
++)
1930 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1935 /* change line breaks */
1936 for (i
= 0; i
< edit
->last_byte
; i
++)
1938 unsigned char c
= edit_get_byte (edit
, i
);
1940 if (!(c
== '\n' || c
== '\r'))
1942 /* not line break */
1943 if (fputc (c
, f
) < 0)
1947 { /* (c == '\n' || c == '\r') */
1948 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1952 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1953 /* put one line break unconditionally */
1954 if (fputc ('\n', f
) < 0)
1957 i
++; /* 2 chars are processed */
1959 if (c
== '\r' && c1
== '\n')
1960 /* Windows line break; go to the next char */
1963 if (c
== '\r' && c1
== '\r')
1965 /* two Macintosh line breaks; put second line break */
1966 if (fputc ('\n', f
) < 0)
1971 if (fputc (c1
, f
) < 0)
1975 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1976 /* put one line break unconditionally */
1977 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1980 if (c
== '\r' && c1
== '\n')
1981 /* Windows line break; go to the next char */
1985 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1986 /* put one line break unconditionally */
1987 if (fputc ('\r', f
) < 0)
1990 i
++; /* 2 chars are processed */
1992 if (c
== '\r' && c1
== '\n')
1993 /* Windows line break; go to the next char */
1996 if (c
== '\n' && c1
== '\n')
1998 /* two Windows line breaks; put second line break */
1999 if (fputc ('\r', f
) < 0)
2004 if (fputc (c1
, f
) < 0)
2007 case LB_ASIS
: /* default without changes */
2013 return edit
->last_byte
;
2016 /* --------------------------------------------------------------------------------------------- */
2017 /** inserts a file at the cursor, returns count of inserted bytes on success */
2019 edit_insert_file (WEdit
* edit
, const char *filename
)
2024 p
= edit_get_filter (filename
);
2028 long current
= edit
->curs1
;
2029 f
= (FILE *) popen (p
, "r");
2032 edit_insert_stream (edit
, f
);
2033 ins_len
= edit
->curs1
- current
;
2034 edit_cursor_move (edit
, current
- edit
->curs1
);
2038 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2039 edit_error_dialog (_("Error"), errmsg
);
2048 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2049 edit_error_dialog (_("Error"), errmsg
);
2058 int i
, file
, blocklen
;
2059 long current
= edit
->curs1
;
2060 int vertical_insertion
= 0;
2062 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
2065 buf
= g_malloc0 (TEMP_BUF_LEN
);
2066 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2069 /* if contain signature VERTICAL_MAGIC then it vertical block */
2070 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2071 vertical_insertion
= 1;
2073 mc_lseek (file
, 0, SEEK_SET
);
2075 if (vertical_insertion
)
2077 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
2081 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2083 for (i
= 0; i
< blocklen
; i
++)
2084 edit_insert (edit
, buf
[i
]);
2087 ins_len
= edit
->curs1
- current
;
2088 edit_cursor_move (edit
, current
- edit
->curs1
);
2097 /* --------------------------------------------------------------------------------------------- */
2099 * Fill in the edit structure. Return NULL on failure. Pass edit as
2100 * NULL to allocate a new structure.
2102 * If line is 0, try to restore saved position. Otherwise put the
2103 * cursor on that line and show it in the middle of the screen.
2107 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
2110 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2111 if (option_line_state
)
2113 option_line_state_width
= LINE_STATE_WIDTH
;
2117 option_line_state_width
= 0;
2123 * Expand option_whole_chars_search by national letters using
2127 static char option_whole_chars_search_buf
[256];
2129 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2132 size_t len
= str_term_width1 (option_whole_chars_search
);
2134 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2136 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2138 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2140 option_whole_chars_search_buf
[len
++] = i
;
2144 option_whole_chars_search_buf
[len
] = 0;
2145 option_whole_chars_search
= option_whole_chars_search_buf
;
2147 #endif /* ENABLE_NLS */
2148 edit
= g_malloc0 (sizeof (WEdit
));
2149 edit
->search
= NULL
;
2152 edit_purge_widget (edit
);
2153 edit
->num_widget_lines
= lines
;
2155 edit
->num_widget_columns
= columns
;
2156 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2157 edit
->stat1
.st_uid
= getuid ();
2158 edit
->stat1
.st_gid
= getgid ();
2159 edit
->stat1
.st_mtime
= 0;
2161 edit
->force
|= REDRAW_PAGE
;
2162 edit_set_filename (edit
, filename
);
2163 edit
->undo_stack_size
= START_STACK_SIZE
;
2164 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2165 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2167 edit
->redo_stack_size
= START_STACK_SIZE
;
2168 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2169 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2172 edit
->converter
= str_cnv_from_term
;
2173 edit_set_codeset (edit
);
2175 if (edit_load_file (edit
))
2177 /* edit_load_file already gives an error message */
2183 edit
->loading_done
= 1;
2186 edit_load_syntax (edit
, NULL
, NULL
);
2189 edit_get_syntax_color (edit
, -1, &color
);
2192 /* load saved cursor position */
2193 if ((line
== 0) && option_save_position
)
2195 edit_load_position (edit
);
2201 edit_move_display (edit
, line
- 1);
2202 edit_move_to_line (edit
, line
- 1);
2206 edit_load_macro_cmd (edit
);
2210 /* --------------------------------------------------------------------------------------------- */
2211 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2214 edit_clean (WEdit
* edit
)
2221 /* a stale lock, remove it */
2223 edit
->locked
= edit_unlock_file (edit
);
2225 /* save cursor position */
2226 if (option_save_position
)
2227 edit_save_position (edit
);
2228 else if (edit
->serialized_bookmarks
!= NULL
)
2229 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2231 /* File specified on the mcedit command line and never saved */
2232 if (edit
->delete_file
)
2233 unlink (edit
->filename
);
2235 edit_free_syntax_rules (edit
);
2236 book_mark_flush (edit
, -1);
2237 for (; j
<= MAXBUFF
; j
++)
2239 g_free (edit
->buffers1
[j
]);
2240 g_free (edit
->buffers2
[j
]);
2243 g_free (edit
->undo_stack
);
2244 g_free (edit
->redo_stack
);
2245 g_free (edit
->filename
);
2247 mc_search_free (edit
->search
);
2248 edit
->search
= NULL
;
2250 if (edit
->converter
!= str_cnv_from_term
)
2251 str_close_conv (edit
->converter
);
2253 edit_purge_widget (edit
);
2258 /* --------------------------------------------------------------------------------------------- */
2259 /** returns 1 on success */
2262 edit_renew (WEdit
* edit
)
2264 int lines
= edit
->num_widget_lines
;
2265 int columns
= edit
->num_widget_columns
;
2268 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
2271 /* --------------------------------------------------------------------------------------------- */
2273 * Load a new file into the editor. If it fails, preserve the old file.
2274 * To do it, allocate a new widget, initialize it and, if the new file
2275 * was loaded, copy the data to the old widget.
2276 * Return 1 on success, 0 on failure.
2280 edit_reload (WEdit
* edit
, const char *filename
)
2283 int lines
= edit
->num_widget_lines
;
2284 int columns
= edit
->num_widget_columns
;
2286 e
= g_malloc0 (sizeof (WEdit
));
2287 e
->widget
= edit
->widget
;
2288 if (!edit_init (e
, lines
, columns
, filename
, 0))
2294 memcpy (edit
, e
, sizeof (WEdit
));
2299 /* --------------------------------------------------------------------------------------------- */
2301 * Load a new file into the editor and set line. If it fails, preserve the old file.
2302 * To do it, allocate a new widget, initialize it and, if the new file
2303 * was loaded, copy the data to the old widget.
2304 * Return 1 on success, 0 on failure.
2308 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2311 int lines
= edit
->num_widget_lines
;
2312 int columns
= edit
->num_widget_columns
;
2314 e
= g_malloc0 (sizeof (WEdit
));
2315 e
->widget
= edit
->widget
;
2316 if (!edit_init (e
, lines
, columns
, filename
, line
))
2322 memcpy (edit
, e
, sizeof (WEdit
));
2327 /* --------------------------------------------------------------------------------------------- */
2330 edit_set_codeset (WEdit
* edit
)
2335 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
2340 conv
= str_crt_conv_from (cp_id
);
2341 if (conv
!= INVALID_CONV
)
2343 if (edit
->converter
!= str_cnv_from_term
)
2344 str_close_conv (edit
->converter
);
2345 edit
->converter
= conv
;
2350 edit
->utf8
= str_isutf8 (cp_id
);
2357 /* --------------------------------------------------------------------------------------------- */
2359 Recording stack for undo:
2360 The following is an implementation of a compressed stack. Identical
2361 pushes are recorded by a negative prefix indicating the number of times the
2362 same char was pushed. This saves space for repeated curs-left or curs-right
2379 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2380 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2381 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2382 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2385 The only way the cursor moves or the buffer is changed is through the routines:
2386 insert, backspace, insert_ahead, delete, and cursor_move.
2387 These record the reverse undo movements onto the stack each time they are
2390 Each key press results in a set of actions (insert; delete ...). So each time
2391 a key is pressed the current position of start_display is pushed as
2392 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2393 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2394 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2399 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2401 unsigned long sp
= edit
->undo_stack_pointer
;
2405 /* first enlarge the stack if necessary */
2406 if (sp
> edit
->undo_stack_size
- 10)
2408 if (option_max_undo
< 256)
2409 option_max_undo
= 256;
2410 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2412 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2415 edit
->undo_stack
= t
;
2416 edit
->undo_stack_size
<<= 1;
2417 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2421 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2422 if (edit
->undo_stack_disable
)
2424 edit_push_redo_action (edit
, KEY_PRESS
);
2425 edit_push_redo_action (edit
, c
);
2428 else if (edit
->redo_stack_reset
)
2430 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2433 if (edit
->undo_stack_bottom
!= sp
2434 && spm1
!= edit
->undo_stack_bottom
2435 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2438 if (edit
->undo_stack
[spm1
] < 0)
2440 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2443 if (edit
->undo_stack
[spm1
] > -1000000000)
2445 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2447 edit
->undo_stack
[spm1
]--;
2455 d
= edit
->undo_stack
[spm1
];
2459 return; /* --> no need to push multiple do-nothings */
2460 edit
->undo_stack
[sp
] = -2;
2465 edit
->undo_stack
[sp
] = c
;
2468 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2470 /* if the sp wraps round and catches the undo_stack_bottom then erase
2471 * the first set of actions on the stack to make space - by moving
2472 * undo_stack_bottom forward one "key press" */
2473 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2474 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2475 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2478 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2480 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2481 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2483 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2484 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2485 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2487 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2492 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2494 unsigned long sp
= edit
->redo_stack_pointer
;
2497 /* first enlarge the stack if necessary */
2498 if (sp
> edit
->redo_stack_size
- 10)
2500 if (option_max_undo
< 256)
2501 option_max_undo
= 256;
2502 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2504 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2507 edit
->redo_stack
= t
;
2508 edit
->redo_stack_size
<<= 1;
2509 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2513 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2515 if (edit
->redo_stack_bottom
!= sp
2516 && spm1
!= edit
->redo_stack_bottom
2517 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2520 if (edit
->redo_stack
[spm1
] < 0)
2522 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2525 if (edit
->redo_stack
[spm1
] > -1000000000)
2527 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2528 edit
->redo_stack
[spm1
]--;
2535 d
= edit
->redo_stack
[spm1
];
2539 return; /* --> no need to push multiple do-nothings */
2540 edit
->redo_stack
[sp
] = -2;
2541 goto redo_check_bottom
;
2545 edit
->redo_stack
[sp
] = c
;
2548 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2550 /* if the sp wraps round and catches the redo_stack_bottom then erase
2551 * the first set of actions on the stack to make space - by moving
2552 * redo_stack_bottom forward one "key press" */
2553 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2554 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2555 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2558 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2560 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2561 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2564 * If a single key produced enough pushes to wrap all the way round then
2565 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2566 * The stack is then initialised:
2569 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2570 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2571 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2575 /* --------------------------------------------------------------------------------------------- */
2577 Basic low level single character buffer alterations and movements at the cursor.
2578 Returns char passed over, inserted or removed.
2582 edit_insert (WEdit
* edit
, int c
)
2584 /* check if file has grown to large */
2585 if (edit
->last_byte
>= SIZE_LIMIT
)
2588 /* first we must update the position of the display window */
2589 if (edit
->curs1
< edit
->start_display
)
2591 edit
->start_display
++;
2596 /* Mark file as modified, unless the file hasn't been fully loaded */
2597 if (edit
->loading_done
)
2599 edit_modification (edit
);
2602 /* now we must update some info on the file and check if a redraw is required */
2605 if (edit
->book_mark
)
2606 book_mark_inc (edit
, edit
->curs_line
);
2608 edit
->total_lines
++;
2609 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2612 /* save the reverse command onto the undo stack */
2613 /* ordinary char and not space */
2615 edit_push_undo_action (edit
, BACKSPACE
);
2617 edit_push_undo_action (edit
, BACKSPACE_BR
);
2618 /* update markers */
2619 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2620 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2621 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2623 /* add a new buffer if we've reached the end of the last one */
2624 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2625 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2627 /* perform the insertion */
2628 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2629 = (unsigned char) c
;
2631 /* update file length */
2634 /* update cursor position */
2638 /* --------------------------------------------------------------------------------------------- */
2639 /** same as edit_insert and move left */
2642 edit_insert_ahead (WEdit
* edit
, int c
)
2644 if (edit
->last_byte
>= SIZE_LIMIT
)
2647 if (edit
->curs1
< edit
->start_display
)
2649 edit
->start_display
++;
2653 edit_modification (edit
);
2656 if (edit
->book_mark
)
2657 book_mark_inc (edit
, edit
->curs_line
);
2658 edit
->total_lines
++;
2659 edit
->force
|= REDRAW_AFTER_CURSOR
;
2661 /* ordinary char and not space */
2663 edit_push_undo_action (edit
, DELCHAR
);
2665 edit_push_undo_action (edit
, DELCHAR_BR
);
2667 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2668 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2669 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2671 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2672 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2673 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2674 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2681 /* --------------------------------------------------------------------------------------------- */
2684 edit_delete (WEdit
* edit
, const int byte_delete
)
2694 /* if byte_delete = 1 then delete only one byte not multibyte char */
2695 if (edit
->utf8
&& byte_delete
== 0)
2697 edit_get_utf (edit
, edit
->curs1
, &cw
);
2702 if (edit
->mark2
!= edit
->mark1
)
2703 edit_push_markers (edit
);
2705 for (i
= 1; i
<= cw
; i
++)
2707 if (edit
->mark1
> edit
->curs1
)
2710 edit
->end_mark_curs
--;
2712 if (edit
->mark2
> edit
->curs1
)
2714 if (edit
->last_get_rule
> edit
->curs1
)
2715 edit
->last_get_rule
--;
2717 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2719 1) & M_EDIT_BUF_SIZE
) - 1];
2721 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2723 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2724 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2728 edit_push_undo_action (edit
, p
+ 256);
2731 edit_modification (edit
);
2734 if (edit
->book_mark
)
2735 book_mark_dec (edit
, edit
->curs_line
);
2736 edit
->total_lines
--;
2737 edit
->force
|= REDRAW_AFTER_CURSOR
;
2739 if (edit
->curs1
< edit
->start_display
)
2741 edit
->start_display
--;
2749 /* --------------------------------------------------------------------------------------------- */
2750 /** moves the cursor right or left: increment positive or negative respectively */
2753 edit_cursor_move (WEdit
* edit
, long increment
)
2755 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2760 for (; increment
< 0; increment
++)
2765 edit_push_undo_action (edit
, CURS_RIGHT
);
2767 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2768 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2769 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2770 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2771 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2773 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2774 1) & M_EDIT_BUF_SIZE
];
2775 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2777 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2778 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2784 edit
->force
|= REDRAW_LINE_BELOW
;
2789 else if (increment
> 0)
2791 for (; increment
> 0; increment
--)
2796 edit_push_undo_action (edit
, CURS_LEFT
);
2798 c
= edit_get_byte (edit
, edit
->curs1
);
2799 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2800 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2801 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2803 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2805 1) & M_EDIT_BUF_SIZE
) - 1];
2806 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2808 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2809 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2815 edit
->force
|= REDRAW_LINE_ABOVE
;
2821 /* These functions return positions relative to lines */
2823 /* --------------------------------------------------------------------------------------------- */
2824 /** returns index of last char on line + 1 */
2827 edit_eol (WEdit
* edit
, long current
)
2829 if (current
>= edit
->last_byte
)
2830 return edit
->last_byte
;
2833 if (edit_get_byte (edit
, current
) == '\n')
2838 /* --------------------------------------------------------------------------------------------- */
2839 /** returns index of first char on line */
2842 edit_bol (WEdit
* edit
, long current
)
2848 if (edit_get_byte (edit
, current
- 1) == '\n')
2853 /* --------------------------------------------------------------------------------------------- */
2856 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2859 if (upto
> edit
->last_byte
)
2860 upto
= edit
->last_byte
;
2863 while (current
< upto
)
2864 if (edit_get_byte (edit
, current
++) == '\n')
2869 /* --------------------------------------------------------------------------------------------- */
2870 /* If lines is zero this returns the count of lines from current to upto. */
2871 /* If upto is zero returns index of lines forward current. */
2874 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2878 return edit_count_lines (edit
, current
, upto
);
2887 next
= edit_eol (edit
, current
) + 1;
2888 if (next
> edit
->last_byte
)
2897 /* --------------------------------------------------------------------------------------------- */
2898 /** Returns offset of 'lines' lines up from current */
2901 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2905 current
= edit_bol (edit
, current
);
2906 while ((lines
--) && current
!= 0)
2907 current
= edit_bol (edit
, current
- 1);
2911 /* --------------------------------------------------------------------------------------------- */
2912 /* If cols is zero this returns the count of columns from current to upto. */
2913 /* If upto is zero returns index of cols across from current. */
2916 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
2927 q
= edit
->last_byte
+ 2;
2929 for (col
= 0, p
= current
; p
< q
; p
++)
2941 orig_c
= c
= edit_get_byte (edit
, p
);
2949 utf_ch
= edit_get_utf (edit
, p
, &cw
);
2954 if (g_unichar_iswide (utf_ch
))
2957 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2961 c
= convert_to_display_c (c
);
2965 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2973 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
2974 /* '\r' is shown as ^M, so we must advance 2 characters */
2975 /* Caret notation for control characters */
2983 /* --------------------------------------------------------------------------------------------- */
2984 /** returns the current column position of the cursor */
2987 edit_get_col (WEdit
* edit
)
2989 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
2992 /* --------------------------------------------------------------------------------------------- */
2993 /* Scrolling functions */
2994 /* --------------------------------------------------------------------------------------------- */
2997 edit_update_curs_row (WEdit
* edit
)
2999 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3002 /* --------------------------------------------------------------------------------------------- */
3005 edit_update_curs_col (WEdit
* edit
)
3007 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3010 /* --------------------------------------------------------------------------------------------- */
3013 edit_get_curs_col (const WEdit
* edit
)
3015 return edit
->curs_col
;
3018 /* --------------------------------------------------------------------------------------------- */
3019 /** moves the display start position up by i lines */
3022 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3024 unsigned long lines_above
= edit
->start_line
;
3025 if (i
> lines_above
)
3029 edit
->start_line
-= i
;
3030 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3031 edit
->force
|= REDRAW_PAGE
;
3032 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3034 edit_update_curs_row (edit
);
3038 /* --------------------------------------------------------------------------------------------- */
3039 /** returns 1 if could scroll, 0 otherwise */
3042 edit_scroll_downward (WEdit
* edit
, int i
)
3045 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
3046 if (lines_below
> 0)
3048 if (i
> lines_below
)
3050 edit
->start_line
+= i
;
3051 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3052 edit
->force
|= REDRAW_PAGE
;
3053 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3055 edit_update_curs_row (edit
);
3058 /* --------------------------------------------------------------------------------------------- */
3061 edit_scroll_right (WEdit
* edit
, int i
)
3063 edit
->force
|= REDRAW_PAGE
;
3064 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3065 edit
->start_col
-= i
;
3068 /* --------------------------------------------------------------------------------------------- */
3071 edit_scroll_left (WEdit
* edit
, int i
)
3073 if (edit
->start_col
)
3075 edit
->start_col
+= i
;
3076 if (edit
->start_col
> 0)
3077 edit
->start_col
= 0;
3078 edit
->force
|= REDRAW_PAGE
;
3079 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3083 /* --------------------------------------------------------------------------------------------- */
3084 /* high level cursor movement commands */
3085 /* --------------------------------------------------------------------------------------------- */
3088 edit_move_to_prev_col (WEdit
* edit
, long p
)
3090 int prev
= edit
->prev_col
;
3091 int over
= edit
->over_col
;
3092 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3094 if (option_cursor_beyond_eol
)
3096 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3097 edit_eol (edit
, edit
->curs1
));
3099 if (line_len
< prev
+ edit
->over_col
)
3101 edit
->over_col
= prev
+ over
- line_len
;
3102 edit
->prev_col
= line_len
;
3103 edit
->curs_col
= line_len
;
3107 edit
->curs_col
= prev
+ over
;
3108 edit
->prev_col
= edit
->curs_col
;
3115 if (is_in_indent (edit
) && option_fake_half_tabs
)
3117 edit_update_curs_col (edit
);
3119 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3121 int q
= edit
->curs_col
;
3122 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3123 p
= edit_bol (edit
, edit
->curs1
);
3124 edit_cursor_move (edit
,
3125 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3127 if (!left_of_four_spaces (edit
))
3128 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3134 /* --------------------------------------------------------------------------------------------- */
3137 line_is_blank (WEdit
* edit
, long line
)
3139 return is_blank (edit
, edit_find_line (edit
, line
));
3142 /* --------------------------------------------------------------------------------------------- */
3143 /** move cursor to line 'line' */
3146 edit_move_to_line (WEdit
* e
, long line
)
3148 if (line
< e
->curs_line
)
3149 edit_move_up (e
, e
->curs_line
- line
, 0);
3151 edit_move_down (e
, line
- e
->curs_line
, 0);
3152 edit_scroll_screen_over_cursor (e
);
3155 /* --------------------------------------------------------------------------------------------- */
3156 /** scroll window so that first visible line is 'line' */
3159 edit_move_display (WEdit
* e
, long line
)
3161 if (line
< e
->start_line
)
3162 edit_scroll_upward (e
, e
->start_line
- line
);
3164 edit_scroll_downward (e
, line
- e
->start_line
);
3167 /* --------------------------------------------------------------------------------------------- */
3168 /** save markers onto undo stack */
3171 edit_push_markers (WEdit
* edit
)
3173 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3174 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3175 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3178 /* --------------------------------------------------------------------------------------------- */
3181 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3190 /* --------------------------------------------------------------------------------------------- */
3191 /** highlight marker toggle */
3194 edit_mark_cmd (WEdit
* edit
, int unmark
)
3196 edit_push_markers (edit
);
3199 edit_set_markers (edit
, 0, 0, 0, 0);
3200 edit
->force
|= REDRAW_PAGE
;
3204 if (edit
->mark2
>= 0)
3206 edit
->end_mark_curs
= -1;
3207 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3208 edit
->curs_col
+ edit
->over_col
);
3209 edit
->force
|= REDRAW_PAGE
;
3213 edit
->end_mark_curs
= edit
->curs1
;
3214 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3215 edit
->curs_col
+ edit
->over_col
);
3220 /* --------------------------------------------------------------------------------------------- */
3221 /** highlight the word under cursor */
3224 edit_mark_current_word_cmd (WEdit
* edit
)
3228 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3232 c1
= edit_get_byte (edit
, pos
);
3233 c2
= edit_get_byte (edit
, pos
- 1);
3234 if (!isspace (c1
) && isspace (c2
))
3236 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3241 for (; pos
< edit
->last_byte
; pos
++)
3245 c1
= edit_get_byte (edit
, pos
);
3246 c2
= edit_get_byte (edit
, pos
+ 1);
3247 if (!isspace (c1
) && isspace (c2
))
3249 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3252 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3254 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3257 /* --------------------------------------------------------------------------------------------- */
3260 edit_mark_current_line_cmd (WEdit
* edit
)
3262 long pos
= edit
->curs1
;
3264 edit
->mark1
= edit_bol (edit
, pos
);
3265 edit
->mark2
= edit_eol (edit
, pos
);
3267 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3270 /* --------------------------------------------------------------------------------------------- */
3273 edit_delete_line (WEdit
* edit
)
3276 * Delete right part of the line.
3277 * Note that edit_get_byte() returns '\n' when byte position is
3280 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3282 (void) edit_delete (edit
, 1);
3287 * Note that edit_delete() will not corrupt anything if called while
3288 * cursor position is EOF.
3290 (void) edit_delete (edit
, 1);
3293 * Delete left part of the line.
3294 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3296 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3298 (void) edit_backspace (edit
, 1);
3302 /* --------------------------------------------------------------------------------------------- */
3305 insert_spaces_tab (WEdit
* edit
, int half
)
3308 edit_update_curs_col (edit
);
3309 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
3310 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
3313 edit_insert (edit
, ' ');
3318 /* --------------------------------------------------------------------------------------------- */
3321 edit_indent_width (WEdit
* edit
, long p
)
3324 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3326 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3329 /* --------------------------------------------------------------------------------------------- */
3332 edit_insert_indent (WEdit
* edit
, int indent
)
3334 if (!option_fill_tabs_with_spaces
)
3336 while (indent
>= TAB_SIZE
)
3338 edit_insert (edit
, '\t');
3342 while (indent
-- > 0)
3343 edit_insert (edit
, ' ');
3346 /* --------------------------------------------------------------------------------------------- */
3349 edit_push_key_press (WEdit
* edit
)
3351 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3352 if (edit
->mark2
== -1)
3354 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3355 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3359 /* --------------------------------------------------------------------------------------------- */
3362 edit_find_bracket (WEdit
* edit
)
3364 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3365 if (last_bracket
!= edit
->bracket
)
3366 edit
->force
|= REDRAW_PAGE
;
3367 last_bracket
= edit
->bracket
;
3370 /* --------------------------------------------------------------------------------------------- */
3372 * This executes a command as though the user initiated it through a key
3373 * press. Callback with WIDGET_KEY as a message calls this after
3374 * translating the key press. This function can be used to pass any
3375 * command to the editor. Note that the screen wouldn't update
3376 * automatically. Either of command or char_for_insertion must be
3377 * passed as -1. Commands are executed, and char_for_insertion is
3378 * inserted at the cursor.
3382 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3384 if (command
== CK_Begin_Record_Macro
|| command
== CK_Begin_Record_Repeat
3385 || (macro_index
< 0 && (command
== CK_Begin_End_Macro
|| command
== CK_Begin_End_Repeat
)))
3388 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3391 if (macro_index
!= -1)
3393 edit
->force
|= REDRAW_COMPLETELY
;
3394 if (command
== CK_End_Record_Macro
|| command
== CK_Begin_End_Macro
)
3396 edit_store_macro_cmd (edit
);
3400 else if (command
== CK_End_Record_Repeat
|| command
== CK_Begin_End_Repeat
)
3402 edit_repeat_macro_cmd (edit
);
3409 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3411 record_macro_buf
[macro_index
].action
= command
;
3412 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3414 /* record the beginning of a set of editing actions initiated by a key press */
3415 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
3416 edit_push_key_press (edit
);
3418 edit_execute_cmd (edit
, command
, char_for_insertion
);
3419 if (edit
->column_highlight
)
3420 edit
->force
|= REDRAW_PAGE
;
3423 /* --------------------------------------------------------------------------------------------- */
3425 This executes a command at a lower level than macro recording.
3426 It also does not push a key_press onto the undo stack. This means
3427 that if it is called many times, a single undo command will undo
3428 all of them. It also does not check for the Undo command.
3431 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3433 edit
->force
|= REDRAW_LINE
;
3435 /* The next key press will unhighlight the found string, so update
3437 if (edit
->found_len
|| edit
->column_highlight
)
3438 edit
->force
|= REDRAW_PAGE
;
3440 if (command
/ 100 == 6)
3441 { /* a highlight command like shift-arrow */
3442 edit
->column_highlight
= 0;
3443 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3445 edit_mark_cmd (edit
, 1); /* clear */
3446 edit_mark_cmd (edit
, 0); /* marking on */
3448 edit
->highlight
= 1;
3451 { /* any other command */
3452 if (edit
->highlight
)
3453 edit_mark_cmd (edit
, 0); /* clear */
3454 edit
->highlight
= 0;
3457 /* first check for undo */
3458 if (command
== CK_Undo
)
3460 edit
->redo_stack_reset
= 0;
3461 edit_group_undo (edit
);
3462 edit
->found_len
= 0;
3463 edit
->prev_col
= edit_get_col (edit
);
3464 edit
->search_start
= edit
->curs1
;
3467 /* check for redo */
3468 if (command
== CK_Redo
)
3470 edit
->redo_stack_reset
= 0;
3471 edit_do_redo (edit
);
3472 edit
->found_len
= 0;
3473 edit
->prev_col
= edit_get_col (edit
);
3474 edit
->search_start
= edit
->curs1
;
3478 edit
->redo_stack_reset
= 1;
3480 /* An ordinary key press */
3481 if (char_for_insertion
>= 0)
3483 /* if non persistent selection and text selected */
3484 if (!option_persistent_selections
)
3486 if (edit
->mark1
!= edit
->mark2
)
3487 edit_block_delete_cmd (edit
);
3489 if (edit
->overwrite
)
3491 /* remove char only one time, after input first byte, multibyte chars */
3492 if ((!utf8_display
|| edit
->charpoint
== 0)
3493 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3494 edit_delete (edit
, 0);
3496 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3497 edit_insert_over (edit
);
3499 if (char_for_insertion
> 255 && utf8_display
== 0)
3501 unsigned char str
[6 + 1];
3503 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3513 while (str
[i
] != 0 && i
<= 6)
3515 char_for_insertion
= str
[i
];
3516 edit_insert (edit
, char_for_insertion
);
3522 edit_insert (edit
, char_for_insertion
);
3524 if (option_auto_para_formatting
)
3526 format_paragraph (edit
, 0);
3527 edit
->force
|= REDRAW_PAGE
;
3530 check_and_wrap_line (edit
);
3531 edit
->found_len
= 0;
3532 edit
->prev_col
= edit_get_col (edit
);
3533 edit
->search_start
= edit
->curs1
;
3534 edit_find_bracket (edit
);
3542 case CK_Begin_Page_Highlight
:
3543 case CK_End_Page_Highlight
:
3550 if (edit
->mark2
>= 0)
3552 if (!option_persistent_selections
)
3554 if (edit
->column_highlight
)
3555 edit_push_undo_action (edit
, COLUMN_ON
);
3556 edit
->column_highlight
= 0;
3557 edit_mark_cmd (edit
, 1);
3566 case CK_Begin_Page_Highlight
:
3567 case CK_End_Page_Highlight
:
3572 case CK_Word_Left_Highlight
:
3573 case CK_Word_Right_Highlight
:
3574 case CK_Up_Highlight
:
3575 case CK_Down_Highlight
:
3576 case CK_Up_Alt_Highlight
:
3577 case CK_Down_Alt_Highlight
:
3578 if (edit
->mark2
== -1)
3579 break; /*marking is following the cursor: may need to highlight a whole line */
3582 case CK_Left_Highlight
:
3583 case CK_Right_Highlight
:
3584 edit
->force
|= REDRAW_CHAR_ONLY
;
3587 /* basic cursor key commands */
3591 /* if non persistent selection and text selected */
3592 if (!option_persistent_selections
)
3594 if (edit
->mark1
!= edit
->mark2
)
3596 edit_block_delete_cmd (edit
);
3600 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3605 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3607 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3608 edit_backspace (edit
, 1);
3613 if (option_fake_half_tabs
)
3616 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3618 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3619 edit_backspace (edit
, 1);
3624 edit_backspace (edit
, 0);
3627 /* if non persistent selection and text selected */
3628 if (!option_persistent_selections
)
3630 if (edit
->mark1
!= edit
->mark2
)
3632 edit_block_delete_cmd (edit
);
3637 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3638 edit_insert_over (edit
);
3640 if (option_fake_half_tabs
)
3643 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3645 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3646 edit_delete (edit
, 1);
3650 edit_delete (edit
, 0);
3652 case CK_Delete_Word_Left
:
3654 edit_left_delete_word (edit
);
3656 case CK_Delete_Word_Right
:
3657 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3658 edit_insert_over (edit
);
3660 edit_right_delete_word (edit
);
3662 case CK_Delete_Line
:
3663 edit_delete_line (edit
);
3665 case CK_Delete_To_Line_End
:
3666 edit_delete_to_line_end (edit
);
3668 case CK_Delete_To_Line_Begin
:
3669 edit_delete_to_line_begin (edit
);
3673 if (option_auto_para_formatting
)
3675 edit_double_newline (edit
);
3676 if (option_return_does_auto_indent
)
3677 edit_auto_indent (edit
);
3678 format_paragraph (edit
, 0);
3682 edit_insert (edit
, '\n');
3683 if (option_return_does_auto_indent
)
3685 edit_auto_indent (edit
);
3690 edit_insert (edit
, '\n');
3693 case CK_Page_Up_Alt_Highlight
:
3694 edit
->column_highlight
= 1;
3696 case CK_Page_Up_Highlight
:
3697 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3699 case CK_Page_Down_Alt_Highlight
:
3700 edit
->column_highlight
= 1;
3702 case CK_Page_Down_Highlight
:
3703 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3705 case CK_Left_Alt_Highlight
:
3706 edit
->column_highlight
= 1;
3708 case CK_Left_Highlight
:
3709 if (option_fake_half_tabs
)
3711 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3713 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3716 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3717 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3721 edit_left_char_move_cmd (edit
);
3723 case CK_Right_Alt_Highlight
:
3724 edit
->column_highlight
= 1;
3726 case CK_Right_Highlight
:
3727 if (option_fake_half_tabs
)
3729 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3731 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3732 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3736 edit_right_char_move_cmd (edit
);
3739 case CK_Begin_Page_Highlight
:
3740 edit_begin_page (edit
);
3743 case CK_End_Page_Highlight
:
3744 edit_end_page (edit
);
3747 case CK_Word_Left_Highlight
:
3749 edit_left_word_move_cmd (edit
);
3752 case CK_Word_Right_Highlight
:
3754 edit_right_word_move_cmd (edit
);
3756 case CK_Up_Alt_Highlight
:
3757 edit
->column_highlight
= 1;
3759 case CK_Up_Highlight
:
3760 edit_move_up (edit
, 1, 0);
3762 case CK_Down_Alt_Highlight
:
3763 edit
->column_highlight
= 1;
3765 case CK_Down_Highlight
:
3766 edit_move_down (edit
, 1, 0);
3768 case CK_Paragraph_Up_Alt_Highlight
:
3769 edit
->column_highlight
= 1;
3770 case CK_Paragraph_Up
:
3771 case CK_Paragraph_Up_Highlight
:
3772 edit_move_up_paragraph (edit
, 0);
3774 case CK_Paragraph_Down_Alt_Highlight
:
3775 edit
->column_highlight
= 1;
3776 case CK_Paragraph_Down
:
3777 case CK_Paragraph_Down_Highlight
:
3778 edit_move_down_paragraph (edit
, 0);
3780 case CK_Scroll_Up_Alt_Highlight
:
3781 edit
->column_highlight
= 1;
3783 case CK_Scroll_Up_Highlight
:
3784 edit_move_up (edit
, 1, 1);
3786 case CK_Scroll_Down_Alt_Highlight
:
3787 edit
->column_highlight
= 1;
3788 case CK_Scroll_Down
:
3789 case CK_Scroll_Down_Highlight
:
3790 edit_move_down (edit
, 1, 1);
3793 case CK_Home_Highlight
:
3794 edit_cursor_to_bol (edit
);
3797 case CK_End_Highlight
:
3798 edit_cursor_to_eol (edit
);
3801 /* if text marked shift block */
3802 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3804 if (edit
->mark2
< 0)
3805 edit_mark_cmd (edit
, 0);
3806 edit_move_block_to_right (edit
);
3810 if (option_cursor_beyond_eol
)
3811 edit_insert_over (edit
);
3812 edit_tab_cmd (edit
);
3813 if (option_auto_para_formatting
)
3815 format_paragraph (edit
, 0);
3816 edit
->force
|= REDRAW_PAGE
;
3820 check_and_wrap_line (edit
);
3825 case CK_Toggle_Insert
:
3826 edit
->overwrite
= (edit
->overwrite
== 0);
3830 if (edit
->mark2
>= 0)
3832 if (edit
->column_highlight
)
3833 edit_push_undo_action (edit
, COLUMN_ON
);
3834 edit
->column_highlight
= 0;
3836 edit_mark_cmd (edit
, 0);
3838 case CK_Column_Mark
:
3839 if (!edit
->column_highlight
)
3840 edit_push_undo_action (edit
, COLUMN_OFF
);
3841 edit
->column_highlight
= 1;
3842 edit_mark_cmd (edit
, 0);
3845 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3846 edit
->force
|= REDRAW_PAGE
;
3849 if (edit
->column_highlight
)
3850 edit_push_undo_action (edit
, COLUMN_ON
);
3851 edit
->column_highlight
= 0;
3852 edit_mark_cmd (edit
, 1);
3855 if (edit
->column_highlight
)
3856 edit_push_undo_action (edit
, COLUMN_ON
);
3857 edit
->column_highlight
= 0;
3858 edit_mark_current_word_cmd (edit
);
3861 if (edit
->column_highlight
)
3862 edit_push_undo_action (edit
, COLUMN_ON
);
3863 edit
->column_highlight
= 0;
3864 edit_mark_current_line_cmd (edit
);
3866 case CK_Toggle_Line_State
:
3867 option_line_state
= !option_line_state
;
3868 if (option_line_state
)
3870 option_line_state_width
= LINE_STATE_WIDTH
;
3874 option_line_state_width
= 0;
3876 edit
->force
|= REDRAW_PAGE
;
3879 case CK_Toggle_Show_Margin
:
3880 show_right_margin
= !show_right_margin
;
3881 edit
->force
|= REDRAW_PAGE
;
3884 case CK_Toggle_Bookmark
:
3885 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3886 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3887 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3889 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3891 case CK_Flush_Bookmarks
:
3892 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3893 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3894 edit
->force
|= REDRAW_PAGE
;
3896 case CK_Next_Bookmark
:
3897 if (edit
->book_mark
)
3899 struct _book_mark
*p
;
3900 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3904 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3905 || p
->line
< edit
->start_line
)
3906 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3907 edit_move_to_line (edit
, p
->line
);
3911 case CK_Prev_Bookmark
:
3912 if (edit
->book_mark
)
3914 struct _book_mark
*p
;
3915 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3916 while (p
->line
== edit
->curs_line
)
3921 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3922 || p
->line
< edit
->start_line
)
3923 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3924 edit_move_to_line (edit
, p
->line
);
3929 case CK_Beginning_Of_Text
:
3930 case CK_Beginning_Of_Text_Highlight
:
3931 edit_move_to_top (edit
);
3933 case CK_End_Of_Text
:
3934 case CK_End_Of_Text_Highlight
:
3935 edit_move_to_bottom (edit
);
3939 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3940 edit_insert_over (edit
);
3941 edit_block_copy_cmd (edit
);
3944 edit_block_delete_cmd (edit
);
3947 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3948 edit_insert_over (edit
);
3949 edit_block_move_cmd (edit
);
3952 case CK_Shift_Block_Left
:
3953 if (edit
->mark1
!= edit
->mark2
)
3954 edit_move_block_to_left (edit
);
3956 case CK_Shift_Block_Right
:
3957 if (edit
->mark1
!= edit
->mark2
)
3958 edit_move_block_to_right (edit
);
3961 edit_copy_to_X_buf_cmd (edit
);
3964 edit_cut_to_X_buf_cmd (edit
);
3967 /* if non persistent selection and text selected */
3968 if (!option_persistent_selections
)
3970 if (edit
->mark1
!= edit
->mark2
)
3971 edit_block_delete_cmd (edit
);
3973 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3974 edit_insert_over (edit
);
3975 edit_paste_from_X_buf_cmd (edit
);
3977 case CK_Selection_History
:
3978 edit_paste_from_history (edit
);
3982 edit_save_as_cmd (edit
);
3985 edit_save_confirm_cmd (edit
);
3988 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3991 edit_save_block_cmd (edit
);
3993 case CK_Insert_File
:
3994 edit_insert_file_cmd (edit
);
3997 case CK_Load_Prev_File
:
3998 edit_load_back_cmd (edit
);
4000 case CK_Load_Next_File
:
4001 edit_load_forward_cmd (edit
);
4004 case CK_Load_Syntax_File
:
4005 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4007 case CK_Choose_Syntax
:
4008 edit_syntax_dialog (edit
);
4011 case CK_Load_Menu_File
:
4012 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4015 case CK_Toggle_Syntax
:
4016 option_syntax_highlighting
^= 1;
4017 if (option_syntax_highlighting
== 1)
4018 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4019 edit
->force
|= REDRAW_PAGE
;
4022 case CK_Toggle_Tab_TWS
:
4023 enable_show_tabs_tws
^= 1;
4024 edit
->force
|= REDRAW_PAGE
;
4028 edit_search_cmd (edit
, FALSE
);
4031 edit_search_cmd (edit
, TRUE
);
4034 edit_replace_cmd (edit
, 0);
4036 case CK_Replace_Again
:
4037 edit_replace_cmd (edit
, 1);
4039 case CK_Complete_Word
:
4040 /* if text marked shift block */
4041 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4043 edit_move_block_to_left (edit
);
4047 edit_complete_word_cmd (edit
);
4050 case CK_Find_Definition
:
4051 edit_get_match_keyword_cmd (edit
);
4054 dlg_stop (edit
->widget
.owner
);
4057 edit_new_cmd (edit
);
4060 edit_help_cmd (edit
);
4063 edit_refresh_cmd (edit
);
4065 case CK_SaveSetupCmd
:
4074 case CK_Edit_Options
:
4075 edit_options_dialog (edit
);
4077 case CK_Edit_Save_Mode
:
4078 menu_save_mode_cmd ();
4083 /* fool gcc to prevent a Y2K warning */
4084 char time_format
[] = "_c";
4085 time_format
[0] = '%';
4087 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4088 edit_print_string (edit
, s
);
4089 edit
->force
|= REDRAW_PAGE
;
4094 edit_goto_cmd (edit
);
4096 case CK_Paragraph_Format
:
4097 format_paragraph (edit
, 1);
4098 edit
->force
|= REDRAW_PAGE
;
4100 case CK_Delete_Macro
:
4101 edit_delete_macro_cmd (edit
);
4103 case CK_Match_Bracket
:
4104 edit_goto_matching_bracket (edit
);
4107 user_menu (edit
, NULL
, -1);
4110 edit_sort_cmd (edit
);
4113 edit_ext_cmd (edit
);
4116 edit_mail_dialog (edit
);
4121 case CK_SelectCodepage
:
4122 edit_select_codepage_cmd (edit
);
4124 case CK_Insert_Literal
:
4125 edit_insert_literal_cmd (edit
);
4127 case CK_Begin_End_Macro
:
4128 edit_begin_end_macro_cmd (edit
);
4130 case CK_Begin_End_Repeat
:
4131 edit_begin_end_repeat_cmd (edit
);
4141 if ((command
/ CK_Pipe_Block (0)) == 1)
4142 edit_block_process_cmd (edit
, command
- CK_Pipe_Block (0));
4144 /* keys which must set the col position, and the search vars */
4150 case CK_Replace_Again
:
4151 case CK_Complete_Word
:
4152 edit
->prev_col
= edit_get_col (edit
);
4155 case CK_Up_Highlight
:
4156 case CK_Up_Alt_Highlight
:
4158 case CK_Down_Highlight
:
4159 case CK_Down_Alt_Highlight
:
4161 case CK_Page_Up_Highlight
:
4162 case CK_Page_Up_Alt_Highlight
:
4164 case CK_Page_Down_Highlight
:
4165 case CK_Page_Down_Alt_Highlight
:
4166 case CK_Beginning_Of_Text
:
4167 case CK_Beginning_Of_Text_Highlight
:
4168 case CK_End_Of_Text
:
4169 case CK_End_Of_Text_Highlight
:
4170 case CK_Paragraph_Up
:
4171 case CK_Paragraph_Up_Highlight
:
4172 case CK_Paragraph_Up_Alt_Highlight
:
4173 case CK_Paragraph_Down
:
4174 case CK_Paragraph_Down_Highlight
:
4175 case CK_Paragraph_Down_Alt_Highlight
:
4177 case CK_Scroll_Up_Highlight
:
4178 case CK_Scroll_Up_Alt_Highlight
:
4179 case CK_Scroll_Down
:
4180 case CK_Scroll_Down_Highlight
:
4181 case CK_Scroll_Down_Alt_Highlight
:
4182 edit
->search_start
= edit
->curs1
;
4183 edit
->found_len
= 0;
4186 edit
->found_len
= 0;
4187 edit
->prev_col
= edit_get_col (edit
);
4188 edit
->search_start
= edit
->curs1
;
4190 edit_find_bracket (edit
);
4192 if (option_auto_para_formatting
)
4198 case CK_Delete_Word_Left
:
4199 case CK_Delete_Word_Right
:
4200 case CK_Delete_To_Line_End
:
4201 case CK_Delete_To_Line_Begin
:
4202 format_paragraph (edit
, 0);
4203 edit
->force
|= REDRAW_PAGE
;
4208 /* --------------------------------------------------------------------------------------------- */
4211 edit_stack_init (void)
4213 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4215 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4216 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4219 edit_stack_iterator
= 0;
4222 /* --------------------------------------------------------------------------------------------- */
4225 edit_stack_free (void)
4227 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4228 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4231 /* --------------------------------------------------------------------------------------------- */
4235 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4237 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4240 /* --------------------------------------------------------------------------------------------- */
4244 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4246 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4249 /* --------------------------------------------------------------------------------------------- */
4252 edit_unlock_file (WEdit
* edit
)
4257 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4258 ret
= unlock_file (fullpath
);
4264 /* --------------------------------------------------------------------------------------------- */
4267 edit_lock_file (WEdit
* edit
)
4272 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4273 ret
= lock_file (fullpath
);
4279 /* --------------------------------------------------------------------------------------------- */